diff mbox series

[1/3] drm/amdgpu: create amdgpu_vkms

Message ID 20210712194629.4569-2-Ryan.Taylor@amd.com (mailing list archive)
State New, archived
Headers show
Series drm/amdgpu: modernize virtual display feature | expand

Commit Message

Ryan Taylor July 12, 2021, 7:46 p.m. UTC
Modify the VKMS driver into an api that dce_virtual can use to create
virtual displays that obey drm's atomic modesetting api.

Signed-off-by: Ryan Taylor <Ryan.Taylor@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/Makefile      |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu.h      |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c  |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c   |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c | 410 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.h |  29 ++
 drivers/gpu/drm/amd/amdgpu/dce_virtual.c |  23 +-
 7 files changed, 457 insertions(+), 11 deletions(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.h

Comments

kernel test robot July 13, 2021, 12:44 a.m. UTC | #1
Hi Ryan,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on next-20210712]
[also build test WARNING on v5.14-rc1]
[cannot apply to drm-intel/for-linux-next drm-tip/drm-tip drm-exynos/exynos-drm-next tegra-drm/drm/tegra/for-next linus/master drm/drm-next v5.14-rc1 v5.13 v5.13-rc7]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Ryan-Taylor/drm-amdgpu-modernize-virtual-display-feature/20210713-034827
base:    db503865b9ba6284edfee3825846a464cc4f4c61
config: i386-randconfig-r013-20210712 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/96f64e3b91a195cc37720de206b86c3f0378abbb
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Ryan-Taylor/drm-amdgpu-modernize-virtual-display-feature/20210713-034827
        git checkout 96f64e3b91a195cc37720de206b86c3f0378abbb
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:156:5: warning: no previous prototype for 'amdgpu_vkms_crtc_init' [-Wmissing-prototypes]
     156 | int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
         |     ^~~~~~~~~~~~~~~~~~~~~
   drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:323:20: error: initialization of 'void (*)(struct drm_plane *, struct drm_atomic_state *)' from incompatible pointer type 'void (*)(struct drm_plane *, struct drm_plane_state *)' [-Werror=incompatible-pointer-types]
     323 |  .atomic_update  = amdgpu_vkms_plane_atomic_update,
         |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:323:20: note: (near initialization for 'amdgpu_vkms_primary_helper_funcs.atomic_update')
   drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:324:19: error: initialization of 'int (*)(struct drm_plane *, struct drm_atomic_state *)' from incompatible pointer type 'int (*)(struct drm_plane *, struct drm_plane_state *)' [-Werror=incompatible-pointer-types]
     324 |  .atomic_check  = amdgpu_vkms_plane_atomic_check,
         |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:324:19: note: (near initialization for 'amdgpu_vkms_primary_helper_funcs.atomic_check')
>> drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:329:19: warning: no previous prototype for 'amdgpu_vkms_plane_init' [-Wmissing-prototypes]
     329 | struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
         |                   ^~~~~~~~~~~~~~~~~~~~~~
   cc1: some warnings being treated as errors


vim +/amdgpu_vkms_crtc_init +156 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c

   155	
 > 156	int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
   157				  struct drm_plane *primary, struct drm_plane *cursor)
   158	{
   159		int ret;
   160	
   161		ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
   162						&amdgpu_vkms_crtc_funcs, NULL);
   163		if (ret) {
   164			DRM_ERROR("Failed to init CRTC\n");
   165			return ret;
   166		}
   167	
   168		drm_crtc_helper_add(crtc, &amdgpu_vkms_crtc_helper_funcs);
   169	
   170		return ret;
   171	}
   172	
   173	static const struct drm_connector_funcs amdgpu_vkms_connector_funcs = {
   174		.fill_modes = drm_helper_probe_single_connector_modes,
   175		.destroy = drm_connector_cleanup,
   176		.reset = drm_atomic_helper_connector_reset,
   177		.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
   178		.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
   179	};
   180	
   181	static int amdgpu_vkms_conn_get_modes(struct drm_connector *connector)
   182	{
   183		int count;
   184	
   185		count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
   186		drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
   187	
   188		return count;
   189	}
   190	
   191	static const struct drm_connector_helper_funcs amdgpu_vkms_conn_helper_funcs = {
   192		.get_modes    = amdgpu_vkms_conn_get_modes,
   193	};
   194	
   195	static const struct drm_plane_funcs amdgpu_vkms_plane_funcs = {
   196		.update_plane		= drm_atomic_helper_update_plane,
   197		.disable_plane		= drm_atomic_helper_disable_plane,
   198		.destroy		= drm_plane_cleanup,
   199		.reset			= drm_atomic_helper_plane_reset,
   200		.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
   201		.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
   202	};
   203	
   204	static void amdgpu_vkms_plane_atomic_update(struct drm_plane *plane,
   205					     struct drm_plane_state *old_state)
   206	{
   207		return;
   208	}
   209	
   210	static int amdgpu_vkms_plane_atomic_check(struct drm_plane *plane,
   211						  struct drm_plane_state *state)
   212	{
   213		struct drm_crtc_state *crtc_state;
   214		bool can_position = false;
   215		int ret;
   216	
   217		if (!state->fb || WARN_ON(!state->crtc))
   218			return 0;
   219	
   220		crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
   221		if (IS_ERR(crtc_state))
   222			return PTR_ERR(crtc_state);
   223	
   224		ret = drm_atomic_helper_check_plane_state(state, crtc_state,
   225							  DRM_PLANE_HELPER_NO_SCALING,
   226							  DRM_PLANE_HELPER_NO_SCALING,
   227							  can_position, true);
   228		if (ret != 0)
   229			return ret;
   230	
   231		/* for now primary plane must be visible and full screen */
   232		if (!state->visible && !can_position)
   233			return -EINVAL;
   234	
   235		return 0;
   236	}
   237	
   238	static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
   239					  struct drm_plane_state *new_state)
   240	{
   241		struct amdgpu_framebuffer *afb;
   242		struct drm_gem_object *obj;
   243		struct amdgpu_device *adev;
   244		struct amdgpu_bo *rbo;
   245		struct list_head list;
   246		struct ttm_validate_buffer tv;
   247		struct ww_acquire_ctx ticket;
   248		uint32_t domain;
   249		int r;
   250	
   251		if (!new_state->fb) {
   252			DRM_DEBUG_KMS("No FB bound\n");
   253			return 0;
   254		}
   255		afb = to_amdgpu_framebuffer(new_state->fb);
   256		obj = new_state->fb->obj[0];
   257		rbo = gem_to_amdgpu_bo(obj);
   258		adev = amdgpu_ttm_adev(rbo->tbo.bdev);
   259		INIT_LIST_HEAD(&list);
   260	
   261		tv.bo = &rbo->tbo;
   262		tv.num_shared = 1;
   263		list_add(&tv.head, &list);
   264	
   265		r = ttm_eu_reserve_buffers(&ticket, &list, false, NULL);
   266		if (r) {
   267			dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
   268			return r;
   269		}
   270	
   271		if (plane->type != DRM_PLANE_TYPE_CURSOR)
   272			domain = amdgpu_display_supported_domains(adev, rbo->flags);
   273		else
   274			domain = AMDGPU_GEM_DOMAIN_VRAM;
   275	
   276		r = amdgpu_bo_pin(rbo, domain);
   277		if (unlikely(r != 0)) {
   278			if (r != -ERESTARTSYS)
   279				DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
   280			ttm_eu_backoff_reservation(&ticket, &list);
   281			return r;
   282		}
   283	
   284		r = amdgpu_ttm_alloc_gart(&rbo->tbo);
   285		if (unlikely(r != 0)) {
   286			amdgpu_bo_unpin(rbo);
   287			ttm_eu_backoff_reservation(&ticket, &list);
   288			DRM_ERROR("%p bind failed\n", rbo);
   289			return r;
   290		}
   291	
   292		ttm_eu_backoff_reservation(&ticket, &list);
   293	
   294		afb->address = amdgpu_bo_gpu_offset(rbo);
   295	
   296		amdgpu_bo_ref(rbo);
   297	
   298		return 0;
   299	}
   300	
   301	static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
   302					   struct drm_plane_state *old_state)
   303	{
   304		struct amdgpu_bo *rbo;
   305		int r;
   306	
   307		if (!old_state->fb)
   308			return;
   309	
   310		rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
   311		r = amdgpu_bo_reserve(rbo, false);
   312		if (unlikely(r)) {
   313			DRM_ERROR("failed to reserve rbo before unpin\n");
   314			return;
   315		}
   316	
   317		amdgpu_bo_unpin(rbo);
   318		amdgpu_bo_unreserve(rbo);
   319		amdgpu_bo_unref(&rbo);
   320	}
   321	
   322	static const struct drm_plane_helper_funcs amdgpu_vkms_primary_helper_funcs = {
   323		.atomic_update		= amdgpu_vkms_plane_atomic_update,
   324		.atomic_check		= amdgpu_vkms_plane_atomic_check,
   325		.prepare_fb		= amdgpu_vkms_prepare_fb,
   326		.cleanup_fb		= amdgpu_vkms_cleanup_fb,
   327	};
   328	
 > 329	struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
   330						 enum drm_plane_type type, int index)
   331	{
   332		struct drm_plane *plane;
   333		int ret;
   334	
   335		plane = kzalloc(sizeof(*plane), GFP_KERNEL);
   336		if (!plane)
   337			return ERR_PTR(-ENOMEM);
   338	
   339		ret = drm_universal_plane_init(dev, plane, 1 << index,
   340					       &amdgpu_vkms_plane_funcs,
   341					       amdgpu_vkms_formats,
   342					       ARRAY_SIZE(amdgpu_vkms_formats),
   343					       NULL, type, NULL);
   344		if (ret) {
   345			kfree(plane);
   346			return ERR_PTR(ret);
   347		}
   348	
   349		drm_plane_helper_add(plane, &amdgpu_vkms_primary_helper_funcs);
   350	
   351		return plane;
   352	}
   353	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot July 13, 2021, 1:01 a.m. UTC | #2
Hi Ryan,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on next-20210712]
[also build test ERROR on v5.14-rc1]
[cannot apply to drm-intel/for-linux-next drm-tip/drm-tip drm-exynos/exynos-drm-next tegra-drm/drm/tegra/for-next linus/master drm/drm-next v5.14-rc1 v5.13 v5.13-rc7]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Ryan-Taylor/drm-amdgpu-modernize-virtual-display-feature/20210713-034827
base:    db503865b9ba6284edfee3825846a464cc4f4c61
config: x86_64-randconfig-a013-20210712 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 8d69635ed9ecf36fd0ca85906bfde17949671cbe)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/96f64e3b91a195cc37720de206b86c3f0378abbb
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Ryan-Taylor/drm-amdgpu-modernize-virtual-display-feature/20210713-034827
        git checkout 96f64e3b91a195cc37720de206b86c3f0378abbb
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All error/warnings (new ones prefixed by >>):

>> drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:156:5: warning: no previous prototype for function 'amdgpu_vkms_crtc_init' [-Wmissing-prototypes]
   int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
       ^
   drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:156:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
   ^
   static 
>> drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:323:20: error: incompatible function pointer types initializing 'void (*)(struct drm_plane *, struct drm_atomic_state *)' with an expression of type 'void (struct drm_plane *, struct drm_plane_state *)' [-Werror,-Wincompatible-function-pointer-types]
           .atomic_update          = amdgpu_vkms_plane_atomic_update,
                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:324:19: error: incompatible function pointer types initializing 'int (*)(struct drm_plane *, struct drm_atomic_state *)' with an expression of type 'int (struct drm_plane *, struct drm_plane_state *)' [-Werror,-Wincompatible-function-pointer-types]
           .atomic_check           = amdgpu_vkms_plane_atomic_check,
                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:329:19: warning: no previous prototype for function 'amdgpu_vkms_plane_init' [-Wmissing-prototypes]
   struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
                     ^
   drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c:329:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
   ^
   static 
   2 warnings and 2 errors generated.


vim +323 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c

   155	
 > 156	int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
   157				  struct drm_plane *primary, struct drm_plane *cursor)
   158	{
   159		int ret;
   160	
   161		ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
   162						&amdgpu_vkms_crtc_funcs, NULL);
   163		if (ret) {
   164			DRM_ERROR("Failed to init CRTC\n");
   165			return ret;
   166		}
   167	
   168		drm_crtc_helper_add(crtc, &amdgpu_vkms_crtc_helper_funcs);
   169	
   170		return ret;
   171	}
   172	
   173	static const struct drm_connector_funcs amdgpu_vkms_connector_funcs = {
   174		.fill_modes = drm_helper_probe_single_connector_modes,
   175		.destroy = drm_connector_cleanup,
   176		.reset = drm_atomic_helper_connector_reset,
   177		.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
   178		.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
   179	};
   180	
   181	static int amdgpu_vkms_conn_get_modes(struct drm_connector *connector)
   182	{
   183		int count;
   184	
   185		count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
   186		drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
   187	
   188		return count;
   189	}
   190	
   191	static const struct drm_connector_helper_funcs amdgpu_vkms_conn_helper_funcs = {
   192		.get_modes    = amdgpu_vkms_conn_get_modes,
   193	};
   194	
   195	static const struct drm_plane_funcs amdgpu_vkms_plane_funcs = {
   196		.update_plane		= drm_atomic_helper_update_plane,
   197		.disable_plane		= drm_atomic_helper_disable_plane,
   198		.destroy		= drm_plane_cleanup,
   199		.reset			= drm_atomic_helper_plane_reset,
   200		.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
   201		.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
   202	};
   203	
   204	static void amdgpu_vkms_plane_atomic_update(struct drm_plane *plane,
   205					     struct drm_plane_state *old_state)
   206	{
   207		return;
   208	}
   209	
   210	static int amdgpu_vkms_plane_atomic_check(struct drm_plane *plane,
   211						  struct drm_plane_state *state)
   212	{
   213		struct drm_crtc_state *crtc_state;
   214		bool can_position = false;
   215		int ret;
   216	
   217		if (!state->fb || WARN_ON(!state->crtc))
   218			return 0;
   219	
   220		crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
   221		if (IS_ERR(crtc_state))
   222			return PTR_ERR(crtc_state);
   223	
   224		ret = drm_atomic_helper_check_plane_state(state, crtc_state,
   225							  DRM_PLANE_HELPER_NO_SCALING,
   226							  DRM_PLANE_HELPER_NO_SCALING,
   227							  can_position, true);
   228		if (ret != 0)
   229			return ret;
   230	
   231		/* for now primary plane must be visible and full screen */
   232		if (!state->visible && !can_position)
   233			return -EINVAL;
   234	
   235		return 0;
   236	}
   237	
   238	static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
   239					  struct drm_plane_state *new_state)
   240	{
   241		struct amdgpu_framebuffer *afb;
   242		struct drm_gem_object *obj;
   243		struct amdgpu_device *adev;
   244		struct amdgpu_bo *rbo;
   245		struct list_head list;
   246		struct ttm_validate_buffer tv;
   247		struct ww_acquire_ctx ticket;
   248		uint32_t domain;
   249		int r;
   250	
   251		if (!new_state->fb) {
   252			DRM_DEBUG_KMS("No FB bound\n");
   253			return 0;
   254		}
   255		afb = to_amdgpu_framebuffer(new_state->fb);
   256		obj = new_state->fb->obj[0];
   257		rbo = gem_to_amdgpu_bo(obj);
   258		adev = amdgpu_ttm_adev(rbo->tbo.bdev);
   259		INIT_LIST_HEAD(&list);
   260	
   261		tv.bo = &rbo->tbo;
   262		tv.num_shared = 1;
   263		list_add(&tv.head, &list);
   264	
   265		r = ttm_eu_reserve_buffers(&ticket, &list, false, NULL);
   266		if (r) {
   267			dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
   268			return r;
   269		}
   270	
   271		if (plane->type != DRM_PLANE_TYPE_CURSOR)
   272			domain = amdgpu_display_supported_domains(adev, rbo->flags);
   273		else
   274			domain = AMDGPU_GEM_DOMAIN_VRAM;
   275	
   276		r = amdgpu_bo_pin(rbo, domain);
   277		if (unlikely(r != 0)) {
   278			if (r != -ERESTARTSYS)
   279				DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
   280			ttm_eu_backoff_reservation(&ticket, &list);
   281			return r;
   282		}
   283	
   284		r = amdgpu_ttm_alloc_gart(&rbo->tbo);
   285		if (unlikely(r != 0)) {
   286			amdgpu_bo_unpin(rbo);
   287			ttm_eu_backoff_reservation(&ticket, &list);
   288			DRM_ERROR("%p bind failed\n", rbo);
   289			return r;
   290		}
   291	
   292		ttm_eu_backoff_reservation(&ticket, &list);
   293	
   294		afb->address = amdgpu_bo_gpu_offset(rbo);
   295	
   296		amdgpu_bo_ref(rbo);
   297	
   298		return 0;
   299	}
   300	
   301	static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
   302					   struct drm_plane_state *old_state)
   303	{
   304		struct amdgpu_bo *rbo;
   305		int r;
   306	
   307		if (!old_state->fb)
   308			return;
   309	
   310		rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
   311		r = amdgpu_bo_reserve(rbo, false);
   312		if (unlikely(r)) {
   313			DRM_ERROR("failed to reserve rbo before unpin\n");
   314			return;
   315		}
   316	
   317		amdgpu_bo_unpin(rbo);
   318		amdgpu_bo_unreserve(rbo);
   319		amdgpu_bo_unref(&rbo);
   320	}
   321	
   322	static const struct drm_plane_helper_funcs amdgpu_vkms_primary_helper_funcs = {
 > 323		.atomic_update		= amdgpu_vkms_plane_atomic_update,
 > 324		.atomic_check		= amdgpu_vkms_plane_atomic_check,
   325		.prepare_fb		= amdgpu_vkms_prepare_fb,
   326		.cleanup_fb		= amdgpu_vkms_cleanup_fb,
   327	};
   328	
 > 329	struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
   330						 enum drm_plane_type type, int index)
   331	{
   332		struct drm_plane *plane;
   333		int ret;
   334	
   335		plane = kzalloc(sizeof(*plane), GFP_KERNEL);
   336		if (!plane)
   337			return ERR_PTR(-ENOMEM);
   338	
   339		ret = drm_universal_plane_init(dev, plane, 1 << index,
   340					       &amdgpu_vkms_plane_funcs,
   341					       amdgpu_vkms_formats,
   342					       ARRAY_SIZE(amdgpu_vkms_formats),
   343					       NULL, type, NULL);
   344		if (ret) {
   345			kfree(plane);
   346			return ERR_PTR(ret);
   347		}
   348	
   349		drm_plane_helper_add(plane, &amdgpu_vkms_primary_helper_funcs);
   350	
   351		return plane;
   352	}
   353	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile b/drivers/gpu/drm/amd/amdgpu/Makefile
index 7d292485ca7c..dfcf3b39a2f6 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -119,6 +119,7 @@  amdgpu-y += \
 amdgpu-y += \
 	dce_v10_0.o \
 	dce_v11_0.o \
+	amdgpu_vkms.o \
 	dce_virtual.o
 
 # add GFX block
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index d14b4968a026..a0198963fc8a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -912,6 +912,7 @@  struct amdgpu_device {
 
 	/* display */
 	bool				enable_virtual_display;
+	struct amdgpu_vkms_output       *amdgpu_vkms_output;
 	struct amdgpu_mode_info		mode_info;
 	/* For pre-DCE11. DCE11 and later are in "struct amdgpu_device->dm" */
 	struct work_struct		hotplug_work;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
index 179f2d01a082..5c774d6625e4 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
@@ -1222,7 +1222,7 @@  static int amdgpu_pci_probe(struct pci_dev *pdev,
 	int ret, retry = 0;
 	bool supports_atomic = false;
 
-	if (!amdgpu_virtual_display &&
+	if (amdgpu_virtual_display ||
 	    amdgpu_device_asic_has_dc_support(flags & AMD_ASIC_MASK))
 		supports_atomic = true;
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c
index 09b048647523..5a143ca02cf9 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c
@@ -344,7 +344,7 @@  int amdgpu_fbdev_init(struct amdgpu_device *adev)
 	}
 
 	/* disable all the possible outputs/crtcs before entering KMS mode */
-	if (!amdgpu_device_has_dc_support(adev))
+	if (!amdgpu_device_has_dc_support(adev) && !amdgpu_virtual_display)
 		drm_helper_disable_unused_functions(adev_to_drm(adev));
 
 	drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c
new file mode 100644
index 000000000000..58bd0d7b4602
--- /dev/null
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c
@@ -0,0 +1,410 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <drm/drm_atomic_helper.h>
+#include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+
+#include "amdgpu.h"
+#include "amdgpu_vkms.h"
+#include "amdgpu_display.h"
+
+/**
+ * DOC: amdgpu_vkms
+ *
+ * The amdgpu vkms interface provides a virtual KMS interface for several use
+ * cases: devices without display hardware, platforms where the actual display
+ * hardware is not useful (e.g., servers), SR-IOV virtual functions, device
+ * emulation/simulation, and device bring up prior to display hardware being
+ * usable. We previously emulated a legacy KMS interface, but there was a desire
+ * to move to the atomic KMS interface. The vkms driver did everything we
+ * needed, but we wanted KMS support natively in the driver without buffer
+ * sharing and the ability to support an instance of VKMS per device. We first
+ * looked at splitting vkms into a stub driver and a helper module that other
+ * drivers could use to implement a virtual display, but this strategy ended up
+ * being messy due to driver specific callbacks needed for buffer management.
+ * Ultimately, it proved easier to import the vkms code as it mostly used core
+ * drm helpers anyway.
+ */
+
+static const u32 amdgpu_vkms_formats[] = {
+	DRM_FORMAT_XRGB8888,
+};
+
+static enum hrtimer_restart amdgpu_vkms_vblank_simulate(struct hrtimer *timer)
+{
+	struct amdgpu_vkms_output *output = container_of(timer,
+							 struct amdgpu_vkms_output,
+							 vblank_hrtimer);
+	struct drm_crtc *crtc = &output->crtc;
+	u64 ret_overrun;
+	bool ret;
+
+	ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer,
+					  output->period_ns);
+	WARN_ON(ret_overrun != 1);
+
+	ret = drm_crtc_handle_vblank(crtc);
+	if (!ret)
+		DRM_ERROR("amdgpu_vkms failure on handling vblank");
+
+	return HRTIMER_RESTART;
+}
+
+static int amdgpu_vkms_enable_vblank(struct drm_crtc *crtc)
+{
+	struct drm_device *dev = crtc->dev;
+	unsigned int pipe = drm_crtc_index(crtc);
+	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
+	struct amdgpu_vkms_output *out = drm_crtc_to_amdgpu_vkms_output(crtc);
+
+	drm_calc_timestamping_constants(crtc, &crtc->mode);
+
+	hrtimer_init(&out->vblank_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	out->vblank_hrtimer.function = &amdgpu_vkms_vblank_simulate;
+	out->period_ns = ktime_set(0, vblank->framedur_ns);
+	hrtimer_start(&out->vblank_hrtimer, out->period_ns, HRTIMER_MODE_REL);
+
+	return 0;
+}
+
+static void amdgpu_vkms_disable_vblank(struct drm_crtc *crtc)
+{
+	struct amdgpu_vkms_output *out = drm_crtc_to_amdgpu_vkms_output(crtc);
+
+	hrtimer_cancel(&out->vblank_hrtimer);
+}
+
+static bool amdgpu_vkms_get_vblank_timestamp(struct drm_crtc *crtc,
+					     int *max_error,
+					     ktime_t *vblank_time,
+					     bool in_vblank_irq)
+{
+	struct drm_device *dev = crtc->dev;
+	unsigned int pipe = crtc->index;
+	struct amdgpu_vkms_output *output = drm_crtc_to_amdgpu_vkms_output(crtc);
+	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
+
+	if (!READ_ONCE(vblank->enabled)) {
+		*vblank_time = ktime_get();
+		return true;
+	}
+
+	*vblank_time = READ_ONCE(output->vblank_hrtimer.node.expires);
+
+	if (WARN_ON(*vblank_time == vblank->time))
+		return true;
+
+	/*
+	 * To prevent races we roll the hrtimer forward before we do any
+	 * interrupt processing - this is how real hw works (the interrupt is
+	 * only generated after all the vblank registers are updated) and what
+	 * the vblank core expects. Therefore we need to always correct the
+	 * timestampe by one frame.
+	 */
+	*vblank_time -= output->period_ns;
+
+	return true;
+}
+
+static const struct drm_crtc_funcs amdgpu_vkms_crtc_funcs = {
+	.set_config             = drm_atomic_helper_set_config,
+	.destroy                = drm_crtc_cleanup,
+	.page_flip              = drm_atomic_helper_page_flip,
+	.reset                  = drm_atomic_helper_crtc_reset,
+	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
+	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
+	.enable_vblank		= amdgpu_vkms_enable_vblank,
+	.disable_vblank		= amdgpu_vkms_disable_vblank,
+	.get_vblank_timestamp	= amdgpu_vkms_get_vblank_timestamp,
+};
+
+static void amdgpu_vkms_crtc_atomic_enable(struct drm_crtc *crtc,
+					   struct drm_atomic_state *state)
+{
+	drm_crtc_vblank_on(crtc);
+}
+
+static void amdgpu_vkms_crtc_atomic_disable(struct drm_crtc *crtc,
+					    struct drm_atomic_state *state)
+{
+	drm_crtc_vblank_off(crtc);
+}
+
+static void amdgpu_vkms_crtc_atomic_flush(struct drm_crtc *crtc,
+					  struct drm_atomic_state *state)
+{
+	if (crtc->state->event) {
+		spin_lock(&crtc->dev->event_lock);
+
+		if (drm_crtc_vblank_get(crtc) != 0)
+			drm_crtc_send_vblank_event(crtc, crtc->state->event);
+		else
+			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
+
+		spin_unlock(&crtc->dev->event_lock);
+
+		crtc->state->event = NULL;
+	}
+}
+
+static const struct drm_crtc_helper_funcs amdgpu_vkms_crtc_helper_funcs = {
+	.atomic_flush	= amdgpu_vkms_crtc_atomic_flush,
+	.atomic_enable	= amdgpu_vkms_crtc_atomic_enable,
+	.atomic_disable	= amdgpu_vkms_crtc_atomic_disable,
+};
+
+int amdgpu_vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
+			  struct drm_plane *primary, struct drm_plane *cursor)
+{
+	int ret;
+
+	ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
+					&amdgpu_vkms_crtc_funcs, NULL);
+	if (ret) {
+		DRM_ERROR("Failed to init CRTC\n");
+		return ret;
+	}
+
+	drm_crtc_helper_add(crtc, &amdgpu_vkms_crtc_helper_funcs);
+
+	return ret;
+}
+
+static const struct drm_connector_funcs amdgpu_vkms_connector_funcs = {
+	.fill_modes = drm_helper_probe_single_connector_modes,
+	.destroy = drm_connector_cleanup,
+	.reset = drm_atomic_helper_connector_reset,
+	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
+	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
+};
+
+static int amdgpu_vkms_conn_get_modes(struct drm_connector *connector)
+{
+	int count;
+
+	count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
+	drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
+
+	return count;
+}
+
+static const struct drm_connector_helper_funcs amdgpu_vkms_conn_helper_funcs = {
+	.get_modes    = amdgpu_vkms_conn_get_modes,
+};
+
+static const struct drm_plane_funcs amdgpu_vkms_plane_funcs = {
+	.update_plane		= drm_atomic_helper_update_plane,
+	.disable_plane		= drm_atomic_helper_disable_plane,
+	.destroy		= drm_plane_cleanup,
+	.reset			= drm_atomic_helper_plane_reset,
+	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
+	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
+};
+
+static void amdgpu_vkms_plane_atomic_update(struct drm_plane *plane,
+				     struct drm_plane_state *old_state)
+{
+	return;
+}
+
+static int amdgpu_vkms_plane_atomic_check(struct drm_plane *plane,
+					  struct drm_plane_state *state)
+{
+	struct drm_crtc_state *crtc_state;
+	bool can_position = false;
+	int ret;
+
+	if (!state->fb || WARN_ON(!state->crtc))
+		return 0;
+
+	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
+	if (IS_ERR(crtc_state))
+		return PTR_ERR(crtc_state);
+
+	ret = drm_atomic_helper_check_plane_state(state, crtc_state,
+						  DRM_PLANE_HELPER_NO_SCALING,
+						  DRM_PLANE_HELPER_NO_SCALING,
+						  can_position, true);
+	if (ret != 0)
+		return ret;
+
+	/* for now primary plane must be visible and full screen */
+	if (!state->visible && !can_position)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
+				  struct drm_plane_state *new_state)
+{
+	struct amdgpu_framebuffer *afb;
+	struct drm_gem_object *obj;
+	struct amdgpu_device *adev;
+	struct amdgpu_bo *rbo;
+	struct list_head list;
+	struct ttm_validate_buffer tv;
+	struct ww_acquire_ctx ticket;
+	uint32_t domain;
+	int r;
+
+	if (!new_state->fb) {
+		DRM_DEBUG_KMS("No FB bound\n");
+		return 0;
+	}
+	afb = to_amdgpu_framebuffer(new_state->fb);
+	obj = new_state->fb->obj[0];
+	rbo = gem_to_amdgpu_bo(obj);
+	adev = amdgpu_ttm_adev(rbo->tbo.bdev);
+	INIT_LIST_HEAD(&list);
+
+	tv.bo = &rbo->tbo;
+	tv.num_shared = 1;
+	list_add(&tv.head, &list);
+
+	r = ttm_eu_reserve_buffers(&ticket, &list, false, NULL);
+	if (r) {
+		dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
+		return r;
+	}
+
+	if (plane->type != DRM_PLANE_TYPE_CURSOR)
+		domain = amdgpu_display_supported_domains(adev, rbo->flags);
+	else
+		domain = AMDGPU_GEM_DOMAIN_VRAM;
+
+	r = amdgpu_bo_pin(rbo, domain);
+	if (unlikely(r != 0)) {
+		if (r != -ERESTARTSYS)
+			DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
+		ttm_eu_backoff_reservation(&ticket, &list);
+		return r;
+	}
+
+	r = amdgpu_ttm_alloc_gart(&rbo->tbo);
+	if (unlikely(r != 0)) {
+		amdgpu_bo_unpin(rbo);
+		ttm_eu_backoff_reservation(&ticket, &list);
+		DRM_ERROR("%p bind failed\n", rbo);
+		return r;
+	}
+
+	ttm_eu_backoff_reservation(&ticket, &list);
+
+	afb->address = amdgpu_bo_gpu_offset(rbo);
+
+	amdgpu_bo_ref(rbo);
+
+	return 0;
+}
+
+static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
+				   struct drm_plane_state *old_state)
+{
+	struct amdgpu_bo *rbo;
+	int r;
+
+	if (!old_state->fb)
+		return;
+
+	rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
+	r = amdgpu_bo_reserve(rbo, false);
+	if (unlikely(r)) {
+		DRM_ERROR("failed to reserve rbo before unpin\n");
+		return;
+	}
+
+	amdgpu_bo_unpin(rbo);
+	amdgpu_bo_unreserve(rbo);
+	amdgpu_bo_unref(&rbo);
+}
+
+static const struct drm_plane_helper_funcs amdgpu_vkms_primary_helper_funcs = {
+	.atomic_update		= amdgpu_vkms_plane_atomic_update,
+	.atomic_check		= amdgpu_vkms_plane_atomic_check,
+	.prepare_fb		= amdgpu_vkms_prepare_fb,
+	.cleanup_fb		= amdgpu_vkms_cleanup_fb,
+};
+
+struct drm_plane *amdgpu_vkms_plane_init(struct drm_device *dev,
+					 enum drm_plane_type type, int index)
+{
+	struct drm_plane *plane;
+	int ret;
+
+	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
+	if (!plane)
+		return ERR_PTR(-ENOMEM);
+
+	ret = drm_universal_plane_init(dev, plane, 1 << index,
+				       &amdgpu_vkms_plane_funcs,
+				       amdgpu_vkms_formats,
+				       ARRAY_SIZE(amdgpu_vkms_formats),
+				       NULL, type, NULL);
+	if (ret) {
+		kfree(plane);
+		return ERR_PTR(ret);
+	}
+
+	drm_plane_helper_add(plane, &amdgpu_vkms_primary_helper_funcs);
+
+	return plane;
+}
+
+int amdgpu_vkms_output_init(struct drm_device *dev,
+			    struct amdgpu_vkms_output *output, int index)
+{
+	struct drm_connector *connector = &output->connector;
+	struct drm_encoder *encoder = &output->encoder;
+	struct drm_crtc *crtc = &output->crtc;
+	struct drm_plane *primary, *cursor = NULL;
+	int ret;
+
+	primary = amdgpu_vkms_plane_init(dev, DRM_PLANE_TYPE_PRIMARY, index);
+	if (IS_ERR(primary))
+		return PTR_ERR(primary);
+
+	ret = amdgpu_vkms_crtc_init(dev, crtc, primary, cursor);
+	if (ret)
+		goto err_crtc;
+
+	ret = drm_connector_init(dev, connector, &amdgpu_vkms_connector_funcs,
+				 DRM_MODE_CONNECTOR_VIRTUAL);
+	if (ret) {
+		DRM_ERROR("Failed to init connector\n");
+		goto err_connector;
+	}
+
+	drm_connector_helper_add(connector, &amdgpu_vkms_conn_helper_funcs);
+
+	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
+	if (ret) {
+		DRM_ERROR("Failed to init encoder\n");
+		goto err_encoder;
+	}
+	encoder->possible_crtcs = 1 << index;
+
+	ret = drm_connector_attach_encoder(connector, encoder);
+	if (ret) {
+		DRM_ERROR("Failed to attach connector to encoder\n");
+		goto err_attach;
+	}
+
+	drm_mode_config_reset(dev);
+
+	return 0;
+
+err_attach:
+	drm_encoder_cleanup(encoder);
+
+err_encoder:
+	drm_connector_cleanup(connector);
+
+err_connector:
+	drm_crtc_cleanup(crtc);
+
+err_crtc:
+	drm_plane_cleanup(primary);
+
+	return ret;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.h
new file mode 100644
index 000000000000..5dab51fbecf3
--- /dev/null
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.h
@@ -0,0 +1,29 @@ 
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#ifndef _AMDGPU_VKMS_H_
+#define _AMDGPU_VKMS_H_
+
+#define XRES_DEF  1024
+#define YRES_DEF   764
+
+#define XRES_MAX  16384
+#define YRES_MAX  16384
+
+#define drm_crtc_to_amdgpu_vkms_output(target) \
+	container_of(target, struct amdgpu_vkms_output, crtc)
+
+extern const struct amdgpu_ip_block_version amdgpu_vkms_ip_block;
+
+struct amdgpu_vkms_output {
+	struct drm_crtc crtc;
+	struct drm_encoder encoder;
+	struct drm_connector connector;
+	struct hrtimer vblank_hrtimer;
+	ktime_t period_ns;
+	struct drm_pending_vblank_event *event;
+};
+
+int amdgpu_vkms_output_init(struct drm_device *dev,
+			    struct amdgpu_vkms_output *output, int index);
+
+#endif /* _AMDGPU_VKMS_H_ */
diff --git a/drivers/gpu/drm/amd/amdgpu/dce_virtual.c b/drivers/gpu/drm/amd/amdgpu/dce_virtual.c
index 7e0d8c092c7e..642c77533157 100644
--- a/drivers/gpu/drm/amd/amdgpu/dce_virtual.c
+++ b/drivers/gpu/drm/amd/amdgpu/dce_virtual.c
@@ -22,6 +22,7 @@ 
  */
 
 #include <drm/drm_vblank.h>
+#include <drm/drm_atomic_helper.h>
 
 #include "amdgpu.h"
 #include "amdgpu_pm.h"
@@ -40,6 +41,7 @@ 
 #include "dce_virtual.h"
 #include "ivsrcid/ivsrcid_vislands30.h"
 #include "amdgpu_display.h"
+#include "amdgpu_vkms.h"
 
 #define DCE_VIRTUAL_VBLANK_PERIOD 16666666
 
@@ -374,6 +376,12 @@  static const struct drm_connector_funcs dce_virtual_connector_funcs = {
 	.force = dce_virtual_force,
 };
 
+const struct drm_mode_config_funcs dce_virtual_mode_funcs = {
+	.fb_create = amdgpu_display_user_framebuffer_create,
+	.atomic_check = drm_atomic_helper_check,
+	.atomic_commit = drm_atomic_helper_commit,
+};
+
 static int dce_virtual_sw_init(void *handle)
 {
 	int r, i;
@@ -385,10 +393,10 @@  static int dce_virtual_sw_init(void *handle)
 
 	adev_to_drm(adev)->max_vblank_count = 0;
 
-	adev_to_drm(adev)->mode_config.funcs = &amdgpu_mode_funcs;
+	adev_to_drm(adev)->mode_config.funcs = &dce_virtual_mode_funcs;
 
-	adev_to_drm(adev)->mode_config.max_width = 16384;
-	adev_to_drm(adev)->mode_config.max_height = 16384;
+	adev_to_drm(adev)->mode_config.max_width = XRES_MAX;
+	adev_to_drm(adev)->mode_config.max_height = YRES_MAX;
 
 	adev_to_drm(adev)->mode_config.preferred_depth = 24;
 	adev_to_drm(adev)->mode_config.prefer_shadow = 1;
@@ -399,15 +407,11 @@  static int dce_virtual_sw_init(void *handle)
 	if (r)
 		return r;
 
-	adev_to_drm(adev)->mode_config.max_width = 16384;
-	adev_to_drm(adev)->mode_config.max_height = 16384;
+	adev->amdgpu_vkms_output = kzalloc(sizeof(struct amdgpu_vkms_output) * adev->mode_info.num_crtc, GFP_KERNEL);
 
 	/* allocate crtcs, encoders, connectors */
 	for (i = 0; i < adev->mode_info.num_crtc; i++) {
-		r = dce_virtual_crtc_init(adev, i);
-		if (r)
-			return r;
-		r = dce_virtual_connector_encoder_init(adev, i);
+		r = amdgpu_vkms_output_init(adev_to_drm(adev), &adev->amdgpu_vkms_output[i], i);
 		if (r)
 			return r;
 	}
@@ -428,6 +432,7 @@  static int dce_virtual_sw_fini(void *handle)
 			hrtimer_cancel(&adev->mode_info.crtcs[i]->vblank_timer);
 
 	kfree(adev->mode_info.bios_hardcoded_edid);
+	kfree(adev->amdgpu_vkms_output);
 
 	drm_kms_helper_poll_fini(adev_to_drm(adev));