diff mbox series

[9/9] drm/virtio: Implement dumb_create_fbdev with GEM SHMEM helpers

Message ID 20220303205839.28484-10-tzimmermann@suse.de (mailing list archive)
State Handled Elsewhere
Headers show
Series drm: Support GEM SHMEM fbdev without shadow FB | expand

Commit Message

Thomas Zimmermann March 3, 2022, 8:58 p.m. UTC
Implement struct drm_driver.dumb_create_fbdev with the helpers
provided by GEM SHMEM. Fbdev deferred I/O will now work without
an intermediate shadow buffer for mmap.

As the virtio driver replaces several of the regular GEM SHMEM
functions with its own implementation, some additional code is
necessary make fbdev optimization work. Especially, the driver
has to provide buffer-object functions for fbdev. Add the hook
struct drm_driver.gem_create_object_fbdev, which is similar to
struct drm_driver.gem_create_object and allows for the creation
of dedicated fbdev buffer objects. Wire things up within GEM
SHMEM.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
---
 drivers/gpu/drm/drm_gem_shmem_helper.c  |  7 +++-
 drivers/gpu/drm/virtio/virtgpu_drv.c    |  2 +
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  2 +
 drivers/gpu/drm/virtio/virtgpu_object.c | 54 +++++++++++++++++++++++--
 include/drm/drm_drv.h                   | 10 +++++
 5 files changed, 71 insertions(+), 4 deletions(-)

Comments

Javier Martinez Canillas March 8, 2022, 7:37 p.m. UTC | #1
On 3/3/22 21:58, Thomas Zimmermann wrote:
> Implement struct drm_driver.dumb_create_fbdev with the helpers
> provided by GEM SHMEM. Fbdev deferred I/O will now work without
> an intermediate shadow buffer for mmap.
> 
> As the virtio driver replaces several of the regular GEM SHMEM
> functions with its own implementation, some additional code is
> necessary make fbdev optimization work. Especially, the driver
> has to provide buffer-object functions for fbdev. Add the hook
> struct drm_driver.gem_create_object_fbdev, which is similar to
> struct drm_driver.gem_create_object and allows for the creation
> of dedicated fbdev buffer objects. Wire things up within GEM
> SHMEM.
> 
> Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
> ---
>  drivers/gpu/drm/drm_gem_shmem_helper.c  |  7 +++-
>  drivers/gpu/drm/virtio/virtgpu_drv.c    |  2 +
>  drivers/gpu/drm/virtio/virtgpu_drv.h    |  2 +
>  drivers/gpu/drm/virtio/virtgpu_object.c | 54 +++++++++++++++++++++++--
>  include/drm/drm_drv.h                   | 10 +++++
>  5 files changed, 71 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index ab7cb7d896c3..225aa17895bd 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -71,7 +71,12 @@ __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private, bool f
>  
>  	size = PAGE_ALIGN(size);
>  
> -	if (dev->driver->gem_create_object) {
> +	if (dev->driver->gem_create_object_fbdev && fbdev) {

Same comment here to check if fbdev emulation is enabled or maybe is not
worht it ? But I think this hints the compiler to optimize the if branch.

[snip]

> +}
> +#else
> +struct drm_gem_object *virtio_gpu_create_object_fbdev(struct drm_device *dev,
> +						      size_t size)
> +{
> +	return ERR_PTR(-ENOSYS);
> +}

As mentioned, I believe this should be ERR_PTR(-ENOTSUPP) instead.

Feel free to ignore all this nits if you consider that are not applicable.

Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
Thomas Zimmermann March 9, 2022, 8:52 a.m. UTC | #2
Hi

Am 08.03.22 um 20:37 schrieb Javier Martinez Canillas:
> On 3/3/22 21:58, Thomas Zimmermann wrote:
>> Implement struct drm_driver.dumb_create_fbdev with the helpers
>> provided by GEM SHMEM. Fbdev deferred I/O will now work without
>> an intermediate shadow buffer for mmap.
>>
>> As the virtio driver replaces several of the regular GEM SHMEM
>> functions with its own implementation, some additional code is
>> necessary make fbdev optimization work. Especially, the driver
>> has to provide buffer-object functions for fbdev. Add the hook
>> struct drm_driver.gem_create_object_fbdev, which is similar to
>> struct drm_driver.gem_create_object and allows for the creation
>> of dedicated fbdev buffer objects. Wire things up within GEM
>> SHMEM.
>>
>> Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
>> ---
>>   drivers/gpu/drm/drm_gem_shmem_helper.c  |  7 +++-
>>   drivers/gpu/drm/virtio/virtgpu_drv.c    |  2 +
>>   drivers/gpu/drm/virtio/virtgpu_drv.h    |  2 +
>>   drivers/gpu/drm/virtio/virtgpu_object.c | 54 +++++++++++++++++++++++--
>>   include/drm/drm_drv.h                   | 10 +++++
>>   5 files changed, 71 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
>> index ab7cb7d896c3..225aa17895bd 100644
>> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
>> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
>> @@ -71,7 +71,12 @@ __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private, bool f
>>   
>>   	size = PAGE_ALIGN(size);
>>   
>> -	if (dev->driver->gem_create_object) {
>> +	if (dev->driver->gem_create_object_fbdev && fbdev) {
> 
> Same comment here to check if fbdev emulation is enabled or maybe is not
> worht it ? But I think this hints the compiler to optimize the if branch.
> 
> [snip]
> 
>> +}
>> +#else
>> +struct drm_gem_object *virtio_gpu_create_object_fbdev(struct drm_device *dev,
>> +						      size_t size)
>> +{
>> +	return ERR_PTR(-ENOSYS);
>> +}
> 
> As mentioned, I believe this should be ERR_PTR(-ENOTSUPP) instead.

I've been wondering about this as well. I finally went with the rules at 
[1].  All the variants of ENOTOP/ENOTSUPP seem to be for specific use 
cases, such as a certain feature is not implemented be a specific 
interface (e.g., sockets for EOPNOTSUPP).  ENOSYS is the only general 
error that indicates that an entire interface is missing. Even though 
checkpatch.pl warns that it's only for system calls.

Best regards
Thomas

[1] https://www.cs.helsinki.fi/linux/linux-kernel/2002-30/1135.html

> 
> Feel free to ignore all this nits if you consider that are not applicable.
> 
> Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
>
Javier Martinez Canillas March 9, 2022, 11:29 a.m. UTC | #3
On 3/9/22 09:52, Thomas Zimmermann wrote:

[snip]

>>> +struct drm_gem_object *virtio_gpu_create_object_fbdev(struct drm_device *dev,
>>> +						      size_t size)
>>> +{
>>> +	return ERR_PTR(-ENOSYS);
>>> +}
>>
>> As mentioned, I believe this should be ERR_PTR(-ENOTSUPP) instead.
> 
> I've been wondering about this as well. I finally went with the rules at 
> [1].  All the variants of ENOTOP/ENOTSUPP seem to be for specific use 
> cases, such as a certain feature is not implemented be a specific 
> interface (e.g., sockets for EOPNOTSUPP).  ENOSYS is the only general 
> error that indicates that an entire interface is missing. Even though 
> checkpatch.pl warns that it's only for system calls.
> 
> Best regards
> Thomas
> 
> [1] https://www.cs.helsinki.fi/linux/linux-kernel/2002-30/1135.html
>

Thanks for the link. It would be good to have an authoritative guideline
about this in the kernel documentation (and make checkpatch.pl aware).
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index ab7cb7d896c3..225aa17895bd 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -71,7 +71,12 @@  __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private, bool f
 
 	size = PAGE_ALIGN(size);
 
-	if (dev->driver->gem_create_object) {
+	if (dev->driver->gem_create_object_fbdev && fbdev) {
+		obj = dev->driver->gem_create_object_fbdev(dev, size);
+		if (IS_ERR(obj))
+			return ERR_CAST(obj);
+		shmem = to_drm_gem_shmem_obj(obj);
+	} else if (dev->driver->gem_create_object) {
 		obj = dev->driver->gem_create_object(dev, size);
 		if (IS_ERR(obj))
 			return ERR_CAST(obj);
diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.c b/drivers/gpu/drm/virtio/virtgpu_drv.c
index 5f25a8d15464..ee414f6e785a 100644
--- a/drivers/gpu/drm/virtio/virtgpu_drv.c
+++ b/drivers/gpu/drm/virtio/virtgpu_drv.c
@@ -203,6 +203,7 @@  static const struct drm_driver driver = {
 	.postclose = virtio_gpu_driver_postclose,
 
 	.dumb_create = virtio_gpu_mode_dumb_create,
+	.dumb_create_fbdev = virtio_gpu_mode_dumb_create, // same as dumb_create
 	.dumb_map_offset = virtio_gpu_mode_dumb_mmap,
 
 #if defined(CONFIG_DEBUG_FS)
@@ -215,6 +216,7 @@  static const struct drm_driver driver = {
 	.gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table,
 
 	.gem_create_object = virtio_gpu_create_object,
+	.gem_create_object_fbdev = virtio_gpu_create_object_fbdev,
 	.fops = &virtio_gpu_driver_fops,
 
 	.ioctls = virtio_gpu_ioctls,
diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h
index 0a194aaad419..71556c21c029 100644
--- a/drivers/gpu/drm/virtio/virtgpu_drv.h
+++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
@@ -449,6 +449,8 @@  void virtio_gpu_fence_event_process(struct virtio_gpu_device *vdev,
 void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo);
 struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
 						size_t size);
+struct drm_gem_object *virtio_gpu_create_object_fbdev(struct drm_device *dev,
+						      size_t size);
 int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
 			     struct virtio_gpu_object_params *params,
 			     struct virtio_gpu_object **bo_ptr,
diff --git a/drivers/gpu/drm/virtio/virtgpu_object.c b/drivers/gpu/drm/virtio/virtgpu_object.c
index f293e6ad52da..255f2d650451 100644
--- a/drivers/gpu/drm/virtio/virtgpu_object.c
+++ b/drivers/gpu/drm/virtio/virtgpu_object.c
@@ -132,8 +132,8 @@  bool virtio_gpu_is_shmem(struct virtio_gpu_object *bo)
 	return bo->base.base.funcs == &virtio_gpu_shmem_funcs;
 }
 
-struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
-						size_t size)
+static struct drm_gem_object *__virtio_gpu_create_object(struct drm_device *dev,
+							 size_t size)
 {
 	struct virtio_gpu_object_shmem *shmem;
 	struct drm_gem_shmem_object *dshmem;
@@ -143,10 +143,58 @@  struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
 		return ERR_PTR(-ENOMEM);
 
 	dshmem = &shmem->base.base;
-	dshmem->base.funcs = &virtio_gpu_shmem_funcs;
 	return &dshmem->base;
 }
 
+struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
+						size_t size)
+{
+	struct drm_gem_object *obj;
+
+	obj = __virtio_gpu_create_object(dev, size);
+	if (IS_ERR(obj))
+		return obj;
+	obj->funcs = &virtio_gpu_shmem_funcs;
+
+	return obj;
+}
+
+#if defined(CONFIG_DRM_FBDEV_EMULATION)
+static const struct drm_gem_object_funcs virtio_gpu_shmem_funcs_fbdev = {
+	.free = virtio_gpu_free_object,
+	.open = virtio_gpu_gem_object_open,
+	.close = virtio_gpu_gem_object_close,
+	.print_info = drm_gem_shmem_object_print_info,
+	.export = virtgpu_gem_prime_export,
+	.pin = drm_gem_shmem_object_pin,
+	.unpin = drm_gem_shmem_object_unpin,
+	.get_sg_table = drm_gem_shmem_object_get_sg_table,
+	.vmap = drm_gem_shmem_object_vmap,
+	.vunmap = drm_gem_shmem_object_vunmap,
+	.mmap = drm_gem_shmem_object_mmap_fbdev,
+	.vm_ops = &drm_gem_shmem_vm_ops_fbdev,
+};
+
+struct drm_gem_object *virtio_gpu_create_object_fbdev(struct drm_device *dev,
+						      size_t size)
+{
+	struct drm_gem_object *obj;
+
+	obj = __virtio_gpu_create_object(dev, size);
+	if (IS_ERR(obj))
+		return obj;
+	obj->funcs = &virtio_gpu_shmem_funcs_fbdev;
+
+	return obj;
+}
+#else
+struct drm_gem_object *virtio_gpu_create_object_fbdev(struct drm_device *dev,
+						      size_t size)
+{
+	return ERR_PTR(-ENOSYS);
+}
+#endif
+
 static int virtio_gpu_object_shmem_init(struct virtio_gpu_device *vgdev,
 					struct virtio_gpu_object *bo,
 					struct virtio_gpu_mem_entry **ents,
diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h
index da4a095de1e7..9081281c1f39 100644
--- a/include/drm/drm_drv.h
+++ b/include/drm/drm_drv.h
@@ -298,6 +298,16 @@  struct drm_driver {
 	struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
 						    size_t size);
 
+	/**
+	 * @gem_create_object_fbdev: constructor for gem objects that back fbdev
+	 *
+	 * Hook for allocating the GEM object struct, for use by the CMA
+	 * and SHMEM GEM helpers. Returns a GEM object on success, or an
+	 * ERR_PTR()-encoded error code otherwise.
+	 */
+	struct drm_gem_object *(*gem_create_object_fbdev)(struct drm_device *dev,
+							  size_t size);
+
 	/**
 	 * @prime_handle_to_fd:
 	 *