Skip to contentSkip to conversation

drm: Add shmem GEM library

RFC In review Noralf Trønnes 3 participants 21 messages Jul 12, 2017 – Jul 25, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover drm: Add shmem GEM library

This patchset adds a library for shmem backed GEM objects. I'm putting out an rfc to find out if I'm on the right track with this. The shmem library is very similar to the cma library, so I have tried to pull out the common bits.

A couple of questions:
- What should the default cache mode be?
- Do I need locking around vmap/vunmap?
See TODO in [RFC 5/7] drm: Add library for shmem backed GEM objects

Noralf.

Noralf Trønnes (7):
drm/gem: Add drm_gem_dumb_map_offset()
drm: Add GEM backed framebuffer library drm/fb-helper: Support shadow buffer with deferred io drm/fb-helper: Add simple init/fini functions
drm: Add library for shmem backed GEM objects
drm: Add kms library for shmem backed GEM drm/tinydrm: Switch from CMA to shmem buffers

 drivers/gpu/drm/Kconfig                     |  17 +
 drivers/gpu/drm/Makefile                    |   4 +-
 drivers/gpu/drm/drm_fb_gem_helper.c         | 248 +++++++++++
 drivers/gpu/drm/drm_fb_helper.c             | 229 +++++++++-
 drivers/gpu/drm/drm_fb_shmem_helper.c       | 168 ++++++++
 drivers/gpu/drm/drm_gem.c                   |  35 ++
 drivers/gpu/drm/drm_gem_shmem_helper.c      | 628 ++++++++++++++++++++++++++++
 drivers/gpu/drm/tinydrm/Kconfig             |   2 +-
 drivers/gpu/drm/tinydrm/core/tinydrm-core.c | 147 +++----
 drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c |   4 +-
 drivers/gpu/drm/tinydrm/mi0283qt.c          |   2 +-
 drivers/gpu/drm/tinydrm/mipi-dbi.c          |  41 +-
 include/drm/drm_fb_gem_helper.h             |  64 +++
 include/drm/drm_fb_helper.h                 |  50 +++
 include/drm/drm_fb_shmem_helper.h           |  18 +
 include/drm/drm_gem.h                       |   2 +
 include/drm/drm_gem_shmem_helper.h          | 131 ++++++
 include/drm/tinydrm/tinydrm.h               |  32 +-
 18 files changed, 1683 insertions(+), 139 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_fb_gem_helper.c
 create mode 100644 drivers/gpu/drm/drm_fb_shmem_helper.c
 create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c
 create mode 100644 include/drm/drm_fb_gem_helper.h
 create mode 100644 include/drm/drm_fb_shmem_helper.h
 create mode 100644 include/drm/drm_gem_shmem_helper.h

--
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#1 lore raw

RFC 1/7 drm/gem: Add drm_gem_dumb_map_offset()

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

---
 drivers/gpu/drm/drm_gem.c | 35 +++++++++++++++++++++++++++++++++++
 include/drm/drm_gem.h     |  2 ++
 2 files changed, 37 insertions(+)
drivers/gpu/drm/drm_gem.c +35 -0
index 8dc1106..44ecbaa 100644
@@ -311,6 +311,41 @@ drm_gem_handle_delete(struct drm_file *filp, u32 handle)
 EXPORT_SYMBOL(drm_gem_handle_delete);
 
 /**
+ * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
+ * @file: drm file-private structure containing the gem object
+ * @dev: corresponding drm_device
+ * @handle: gem object handle
+ * @offset: return location for the fake mmap offset
+ *
+ * This implements the &drm_driver.dumb_map_offset kms driver callback for
+ * drivers which use gem to manage their backing storage.
+ *
+ * Returns:
+ * 0 on success or a negative error code on failure.
+ */
+int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
+			    u32 handle, u64 *offset)
+{
+	struct drm_gem_object *obj;
+	int ret;
+
+	obj = drm_gem_object_lookup(file, handle);
+	if (!obj)
+		return -ENOENT;
+
+	ret = drm_gem_create_mmap_offset(obj);
+	if (ret)
+		goto out;
+
+	*offset = drm_vma_node_offset_addr(&obj->vma_node);
+out:
+	drm_gem_object_put_unlocked(obj);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
+
+/**
  * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
  * @file: drm file-private structure to remove the dumb handle from
  * @dev: corresponding drm_device
include/drm/drm_gem.h +2 -0
index 4a9d231..9c55c2a 100644
@@ -302,6 +302,8 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
 		bool dirty, bool accessed);
 
 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
+int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
+			    u32 handle, u64 *offset);
 int drm_gem_dumb_destroy(struct drm_file *file,
 			 struct drm_device *dev,
 			 uint32_t handle);
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

Re: [RFC 1/7] drm/gem: Add drm_gem_dumb_map_offset()

Den 12.07.2017 15.45, skrev Noralf Trønnes:

Quoted 29 lines

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

---
  drivers/gpu/drm/drm_gem.c | 35 +++++++++++++++++++++++++++++++++++
  include/drm/drm_gem.h     |  2 ++
  2 files changed, 37 insertions(+)
drivers/gpu/drm/drm_gem.c +13 -0
index 8dc1106..44ecbaa 100644
@@ -311,6 +311,41 @@ drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  EXPORT_SYMBOL(drm_gem_handle_delete);
 
  /**
+ * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
+ * @file: drm file-private structure containing the gem object
+ * @dev: corresponding drm_device
+ * @handle: gem object handle
+ * @offset: return location for the fake mmap offset
+ *
+ * This implements the &drm_driver.dumb_map_offset kms driver callback for
+ * drivers which use gem to manage their backing storage.
+ *
+ * Returns:
+ * 0 on success or a negative error code on failure.
+ */
+int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,

drivers/gpu/drm/drm_gem.c drm_gem_handle_delete(struct drm_file *filp, u32 handle) · line 336

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +10 -0
+			    u32 handle, u64 *offset)
+{
+	struct drm_gem_object *obj;
+	int ret;
+
+	obj = drm_gem_object_lookup(file, handle);
+	if (!obj)
+		return -ENOENT;
+
+	ret = drm_gem_create_mmap_offset(obj);

There are 3 drm_driver->dumb_map_offset implementations that don't call drm_gem_create_mmap_offset():
- drm_gem_cma_dumb_map_offset()
- exynos_drm_gem_dumb_map_offset()
- tegra_bo_dumb_map_offset()

They do it during object creation.

exynos have this commit:
drm/exynos: create a fake mmap offset with gem creation 48cf53f4343ae12ddc1c60dbe116161ecf7a2885

I looked at the discussion but didn't understand the rationale:
https://lists.freedesktop.org/archives/dri-devel/2015-August/088541.html

I see that it's ok to call drm_gem_create_mmap_offset() multiple times, so it's not really a problem for this function, but it would be nice to know why for my shmem gem library which is based on the cma library.

Noralf.

Quoted 20 lines
drivers/gpu/drm/drm_gem.c +12 -0
+	if (ret)
+		goto out;
+
+	*offset = drm_vma_node_offset_addr(&obj->vma_node);
+out:
+	drm_gem_object_put_unlocked(obj);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
+
+/**
   * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
   * @file: drm file-private structure to remove the dumb handle from
   * @dev: corresponding drm_device
include/drm/drm_gem.h +0 -0
index 4a9d231..9c55c2a 100644

include/drm/drm_gem.h void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, · line 309

Quoted 8 lines
include/drm/drm_gem.h +2 -0
@@ -302,6 +302,8 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  		bool dirty, bool accessed);
 
  struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
+int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
+			    u32 handle, u64 *offset);
  int drm_gem_dumb_destroy(struct drm_file *file,
  			 struct drm_device *dev,
  			 uint32_t handle);

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

Re: [RFC 1/7] drm/gem: Add drm_gem_dumb_map_offset()

Quoted 47 lines · Noralf Trønnes

Den 12.07.2017 15.45, skrev Noralf Trønnes:

Quoted 28 lines

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

---
  drivers/gpu/drm/drm_gem.c | 35 +++++++++++++++++++++++++++++++++++
  include/drm/drm_gem.h     |  2 ++
  2 files changed, 37 insertions(+)
drivers/gpu/drm/drm_gem.c +13 -0
index 8dc1106..44ecbaa 100644
@@ -311,6 +311,41 @@ drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  EXPORT_SYMBOL(drm_gem_handle_delete);
  /**
+ * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
+ * @file: drm file-private structure containing the gem object
+ * @dev: corresponding drm_device
+ * @handle: gem object handle
+ * @offset: return location for the fake mmap offset
+ *
+ * This implements the &drm_driver.dumb_map_offset kms driver callback for
+ * drivers which use gem to manage their backing storage.
+ *
+ * Returns:
+ * 0 on success or a negative error code on failure.
+ */
+int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,

drivers/gpu/drm/drm_gem.c drm_gem_handle_delete(struct drm_file *filp, u32 handle) · line 335

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +10 -0
+			    u32 handle, u64 *offset)
+{
+	struct drm_gem_object *obj;
+	int ret;
+
+	obj = drm_gem_object_lookup(file, handle);
+	if (!obj)
+		return -ENOENT;
+
+	ret = drm_gem_create_mmap_offset(obj);

There are 3 drm_driver->dumb_map_offset implementations that don't call drm_gem_create_mmap_offset():
- drm_gem_cma_dumb_map_offset()
- exynos_drm_gem_dumb_map_offset()
- tegra_bo_dumb_map_offset()

Quoted 8 lines

They do it during object creation.

exynos have this commit:
drm/exynos: create a fake mmap offset with gem creation 48cf53f4343ae12ddc1c60dbe116161ecf7a2885

I looked at the discussion but didn't understand the rationale:
https://lists.freedesktop.org/archives/dri-devel/2015-August/088541.html

This discussion indeed doesn't make sense - Inki says the patch doesn't make sense and then still goes ahead and merges it.

Quoted 3 lines

I see that it's ok to call drm_gem_create_mmap_offset() multiple times, so it's not really a problem for this function, but it would be nice to know why for my shmem gem library which is based on the cma library.

I don't think there's any reasonable reason for this. Lazy creating of offsets is perfectly fine imo, we might as well bake that in as the standard. And for some drivers it's required (you'll run out of mmap space), but modern userspace should be all using mmap64.

Only thing to make sure is that the driver-specific mmap also creates the offset when needed, when we remove it from gem_init functions. -Daniel

Quoted 36 lines

Noralf.

Quoted 20 lines
drivers/gpu/drm/drm_gem.c +12 -0
+	if (ret)
+		goto out;
+
+	*offset = drm_vma_node_offset_addr(&obj->vma_node);
+out:
+	drm_gem_object_put_unlocked(obj);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
+
+/**
   * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
   * @file: drm file-private structure to remove the dumb handle from
   * @dev: corresponding drm_device
include/drm/drm_gem.h +0 -0
index 4a9d231..9c55c2a 100644

include/drm/drm_gem.h void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, · line 308

Quoted 7 lines
include/drm/drm_gem.h +2 -0
@@ -302,6 +302,8 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  		bool dirty, bool accessed);
  struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
+int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
+			    u32 handle, u64 *offset);
  int drm_gem_dumb_destroy(struct drm_file *file,
  			 struct drm_device *dev,
  			 uint32_t handle);

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw

Re: [RFC 1/7] drm/gem: Add drm_gem_dumb_map_offset()

In reply to Noralf Trønnes
Quoted 3 lines · Noralf Trønnes

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

Instead of just introducing the new code, could we replace CMA's code with calls to this at the same time?

I suspect that if we had CMA subclass from drm_fb_gem (or we move the gem objects to the base class), we could remove a lot of its code that you're copying in patch 2, as well.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

Re: [RFC 1/7] drm/gem: Add drm_gem_dumb_map_offset()

Den 19.07.2017 23.01, skrev Eric Anholt:

Quoted 7 lines
Quoted 3 lines · Noralf Trønnes

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

Instead of just introducing the new code, could we replace CMA's code with calls to this at the same time?

I have gone through all the drm_driver->dumb_map_offset implementations and found 23 drivers that could use it:
- 18 drivers use drm_gem_cma_dumb_map_offset()
- exynos_drm_gem_dumb_map_offset()
- mtk_drm_gem_dumb_map_offset()
- psb_gem_dumb_map_gtt()
- rockchip_gem_dumb_map_offset()
- tegra_bo_dumb_map_offset()

vgem_gem_dumb_map() can't use it because of this check:

    if (!obj->filp) {
        ret = -EINVAL;
        goto unref;
    }

armada_gem_dumb_map_offset() can't use it because of this check:

    /* Don't allow imported objects to be mapped */
    if (obj->obj.import_attach) {
        ret = -EINVAL;
        goto err_unref;
    }

I see that drivers must implement all 3 .dumb_* callbacks:

* To support dumb objects drivers must implement the
&drm_driver.dumb_create,
* &drm_driver.dumb_destroy and &drm_driver.dumb_map_offset operations. See
* there for further details.

I'm a fan of defaults, is there any reason we can't do this:

int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
void *data, struct drm_file *file_priv)

 {
     struct drm_mode_map_dumb *args = data;

     /* call driver ioctl to get mmap offset */
-     if (!dev->driver->dumb_map_offset)
+    if (!dev->driver->dumb_create)
        return -ENOSYS;

- return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
+ if (dev->driver->dumb_map_offset)
+ return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
+ else
+ return drm_gem_dumb_map_offset(file_priv, dev, args->handle, &args->offset);
}

int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
void *data, struct drm_file *file_priv)

 {
     struct drm_mode_destroy_dumb *args = data;

- if (!dev->driver->dumb_destroy)
+ if (!dev->driver->dumb_create)

         return -ENOSYS;

- return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+ if (dev->driver->dumb_destroy)
+ return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+ else
+ return drm_gem_dumb_destroy(file_priv, dev, args->handle);
}

There are 36 drivers that use drm_gem_dumb_destroy() directly. vgem violates the docs and doesn't set .dumb_destroy.

Quoted 3 lines

I suspect that if we had CMA subclass from drm_fb_gem (or we move the gem objects to the base class), we could remove a lot of its code that you're copying in patch 2, as well.

Yes, that was the intention.

Noralf.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw

Re: [RFC 1/7] drm/gem: Add drm_gem_dumb_map_offset()

Quoted 32 lines · Noralf Trønnes

Den 19.07.2017 23.01, skrev Eric Anholt:

Quoted 7 lines
Quoted 3 lines · Noralf Trønnes

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

Instead of just introducing the new code, could we replace CMA's code with calls to this at the same time?

I have gone through all the drm_driver->dumb_map_offset implementations and found 23 drivers that could use it:
- 18 drivers use drm_gem_cma_dumb_map_offset()
- exynos_drm_gem_dumb_map_offset()
- mtk_drm_gem_dumb_map_offset()
- psb_gem_dumb_map_gtt()
- rockchip_gem_dumb_map_offset()
- tegra_bo_dumb_map_offset()

vgem_gem_dumb_map() can't use it because of this check:

    if (!obj->filp) {
        ret = -EINVAL;
        goto unref;
    }

armada_gem_dumb_map_offset() can't use it because of this check:

    /* Don't allow imported objects to be mapped */
    if (obj->obj.import_attach) {
        ret = -EINVAL;
        goto err_unref;
    }
Quoted 8 lines

I see that drivers must implement all 3 .dumb_* callbacks:

* To support dumb objects drivers must implement the
&drm_driver.dumb_create,
* &drm_driver.dumb_destroy and &drm_driver.dumb_map_offset operations. See
* there for further details.

I'm a fan of defaults, is there any reason we can't do this:

So in general we try not to set defaults for the main driver entry hooks, to avoid the helper functions leaking into core and becoming mandatory. So e.g. ->atomic_commit should never have a default of drm_atomic_helper_commit.

Same thought applied here for the dumb buffers - the idea is that drivers using any kind of buffer manager scheme could have dumb buffers, including maybe not having a buffer manager at all (and you get some cookie to direct vram allocations or whatever). But everyone ended up with gem, just with different kinds of backing storage implementations (cma, shmem or ttm).

I think it makes sense to make these the defaults and rip out the default assignment from all drivers.

Quoted 37 lines

int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
void *data, struct drm_file *file_priv)

 {
     struct drm_mode_map_dumb *args = data;

     /* call driver ioctl to get mmap offset */
(quoted diff) +1 -2
-     if (!dev->driver->dumb_map_offset)
+    if (!dev->driver->dumb_create)
        return -ENOSYS;
 
-     return dev->driver->dumb_map_offset(file_priv, dev, args->handle,

&args->offset);

(quoted diff) +2 -0
+     if (dev->driver->dumb_map_offset)
+        return dev->driver->dumb_map_offset(file_priv, dev, args->handle,

&args->offset);

(quoted diff) +2 -0
+    else
+        return drm_gem_dumb_map_offset(file_priv, dev, args->handle,

&args->offset);
}

int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
void *data, struct drm_file *file_priv)

 {
     struct drm_mode_destroy_dumb *args = data;
(quoted diff) +5 -2
-     if (!dev->driver->dumb_destroy)
+    if (!dev->driver->dumb_create)
         return -ENOSYS;
 
-    return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+     if (dev->driver->dumb_destroy)
+        return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+    else
+        return drm_gem_dumb_destroy(file_priv, dev, args->handle);
 }
Quoted 2 lines

There are 36 drivers that use drm_gem_dumb_destroy() directly. vgem violates the docs and doesn't set .dumb_destroy.

Interesting, suprising it doesn't leak like mad.

Quoted 5 lines
Quoted 3 lines

I suspect that if we had CMA subclass from drm_fb_gem (or we move the gem objects to the base class), we could remove a lot of its code that you're copying in patch 2, as well.

Yes, that was the intention.

I guess we'd need to see more of the grand plan ... -Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#7 lore raw

Re: [RFC 1/7] drm/gem: Add drm_gem_dumb_map_offset()

Den 20.07.2017 10.00, skrev Daniel Vetter:

Quoted 91 lines · Noralf Trønnes
Quoted 30 lines

Den 19.07.2017 23.01, skrev Eric Anholt:

Quoted 7 lines
Quoted 3 lines · Noralf Trønnes

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

Instead of just introducing the new code, could we replace CMA's code with calls to this at the same time?

I have gone through all the drm_driver->dumb_map_offset implementations and found 23 drivers that could use it:
- 18 drivers use drm_gem_cma_dumb_map_offset()
- exynos_drm_gem_dumb_map_offset()
- mtk_drm_gem_dumb_map_offset()
- psb_gem_dumb_map_gtt()
- rockchip_gem_dumb_map_offset()
- tegra_bo_dumb_map_offset()

vgem_gem_dumb_map() can't use it because of this check:

     if (!obj->filp) {
         ret = -EINVAL;
         goto unref;
     }

armada_gem_dumb_map_offset() can't use it because of this check:

     /* Don't allow imported objects to be mapped */
     if (obj->obj.import_attach) {
         ret = -EINVAL;
         goto err_unref;
     }
Quoted 8 lines

I see that drivers must implement all 3 .dumb_* callbacks:

* To support dumb objects drivers must implement the
&drm_driver.dumb_create,
* &drm_driver.dumb_destroy and &drm_driver.dumb_map_offset operations. See
* there for further details.

I'm a fan of defaults, is there any reason we can't do this:

So in general we try not to set defaults for the main driver entry hooks, to avoid the helper functions leaking into core and becoming mandatory. So e.g. ->atomic_commit should never have a default of drm_atomic_helper_commit.

Same thought applied here for the dumb buffers - the idea is that drivers using any kind of buffer manager scheme could have dumb buffers, including maybe not having a buffer manager at all (and you get some cookie to direct vram allocations or whatever). But everyone ended up with gem, just with different kinds of backing storage implementations (cma, shmem or ttm).

I think it makes sense to make these the defaults and rip out the default assignment from all drivers.

Quoted 36 lines
  int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
                   void *data, struct drm_file *file_priv)
  {
      struct drm_mode_map_dumb *args = data;
      /* call driver ioctl to get mmap offset */
(quoted diff) +1 -2
-     if (!dev->driver->dumb_map_offset)
+    if (!dev->driver->dumb_create)
         return -ENOSYS;
 
-     return dev->driver->dumb_map_offset(file_priv, dev, args->handle,

&args->offset);

(quoted diff) +2 -0
+     if (dev->driver->dumb_map_offset)
+        return dev->driver->dumb_map_offset(file_priv, dev, args->handle,

&args->offset);

(quoted diff) +2 -0
+    else
+        return drm_gem_dumb_map_offset(file_priv, dev, args->handle,

&args->offset);
}

  int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
                  void *data, struct drm_file *file_priv)
  {
      struct drm_mode_destroy_dumb *args = data;
(quoted diff) +5 -2
-     if (!dev->driver->dumb_destroy)
+    if (!dev->driver->dumb_create)
          return -ENOSYS;
 
-    return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+     if (dev->driver->dumb_destroy)
+        return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+    else
+        return drm_gem_dumb_destroy(file_priv, dev, args->handle);
  }
Quoted 2 lines

There are 36 drivers that use drm_gem_dumb_destroy() directly. vgem violates the docs and doesn't set .dumb_destroy.

Quoted 7 lines

Interesting, suprising it doesn't leak like mad.

Quoted 4 lines
Quoted 3 lines

I suspect that if we had CMA subclass from drm_fb_gem (or we move the gem objects to the base class), we could remove a lot of its code that you're copying in patch 2, as well.

Yes, that was the intention.

I guess we'd need to see more of the grand plan ...

Currently it looks like 4 patchsets:

1. Add drm_gem_dumb_map_offset() and implement defaults as discussed above.

2. Add drm_gem_object pointers to drm_framebuffer and create matching helpers for:

     drm_framebuffer_funcs->create_handle
     drm_framebuffer_funcs->destroy
     drm_mode_config_funcs->fb_create
     Should I put the functions in drm_framebuffer_helper.[h,c] ?

Use these helpers in the cma library

3. Add drm_fb_helper_simple_init() and drm_fb_helper_simple_fini() Use them in drivers where applicable.

4. Add shmem library
Convert tinydrm to shmem.

Noralf.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw

Re: [RFC 1/7] drm/gem: Add drm_gem_dumb_map_offset()

Quoted 102 lines · Noralf Trønnes

Den 20.07.2017 10.00, skrev Daniel Vetter:

Quoted 89 lines · Noralf Trønnes
Quoted 30 lines

Den 19.07.2017 23.01, skrev Eric Anholt:

Quoted 7 lines
Quoted 3 lines · Noralf Trønnes

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

Instead of just introducing the new code, could we replace CMA's code with calls to this at the same time?

I have gone through all the drm_driver->dumb_map_offset implementations and found 23 drivers that could use it:
- 18 drivers use drm_gem_cma_dumb_map_offset()
- exynos_drm_gem_dumb_map_offset()
- mtk_drm_gem_dumb_map_offset()
- psb_gem_dumb_map_gtt()
- rockchip_gem_dumb_map_offset()
- tegra_bo_dumb_map_offset()

vgem_gem_dumb_map() can't use it because of this check:

     if (!obj->filp) {
         ret = -EINVAL;
         goto unref;
     }

armada_gem_dumb_map_offset() can't use it because of this check:

     /* Don't allow imported objects to be mapped */
     if (obj->obj.import_attach) {
         ret = -EINVAL;
         goto err_unref;
     }
Quoted 8 lines

I see that drivers must implement all 3 .dumb_* callbacks:

* To support dumb objects drivers must implement the
&drm_driver.dumb_create,
* &drm_driver.dumb_destroy and &drm_driver.dumb_map_offset operations. See
* there for further details.

I'm a fan of defaults, is there any reason we can't do this:

So in general we try not to set defaults for the main driver entry hooks, to avoid the helper functions leaking into core and becoming mandatory. So e.g. ->atomic_commit should never have a default of drm_atomic_helper_commit.

Same thought applied here for the dumb buffers - the idea is that drivers using any kind of buffer manager scheme could have dumb buffers, including maybe not having a buffer manager at all (and you get some cookie to direct vram allocations or whatever). But everyone ended up with gem, just with different kinds of backing storage implementations (cma, shmem or ttm).

I think it makes sense to make these the defaults and rip out the default assignment from all drivers.

Quoted 36 lines
  int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
                   void *data, struct drm_file *file_priv)
  {
      struct drm_mode_map_dumb *args = data;
      /* call driver ioctl to get mmap offset */
(quoted diff) +1 -2
-     if (!dev->driver->dumb_map_offset)
+    if (!dev->driver->dumb_create)
         return -ENOSYS;
 
-     return dev->driver->dumb_map_offset(file_priv, dev, args->handle,

&args->offset);

(quoted diff) +2 -0
+     if (dev->driver->dumb_map_offset)
+        return dev->driver->dumb_map_offset(file_priv, dev, args->handle,

&args->offset);

(quoted diff) +2 -0
+    else
+        return drm_gem_dumb_map_offset(file_priv, dev, args->handle,

&args->offset);
}

  int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
                  void *data, struct drm_file *file_priv)
  {
      struct drm_mode_destroy_dumb *args = data;
(quoted diff) +5 -2
-     if (!dev->driver->dumb_destroy)
+    if (!dev->driver->dumb_create)
          return -ENOSYS;
 
-    return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+     if (dev->driver->dumb_destroy)
+        return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+    else
+        return drm_gem_dumb_destroy(file_priv, dev, args->handle);
  }
Quoted 8 lines
Quoted 2 lines

There are 36 drivers that use drm_gem_dumb_destroy() directly. vgem violates the docs and doesn't set .dumb_destroy.

Interesting, suprising it doesn't leak like mad.

Quoted 4 lines
Quoted 3 lines

I suspect that if we had CMA subclass from drm_fb_gem (or we move the gem objects to the base class), we could remove a lot of its code that you're copying in patch 2, as well.

Yes, that was the intention.

I guess we'd need to see more of the grand plan ...

Currently it looks like 4 patchsets:

Quoted 8 lines

1. Add drm_gem_dumb_map_offset() and implement defaults as discussed above.

2. Add drm_gem_object pointers to drm_framebuffer and create matching helpers for:

     drm_framebuffer_funcs->create_handle
     drm_framebuffer_funcs->destroy
     drm_mode_config_funcs->fb_create
     Should I put the functions in drm_framebuffer_helper.[h,c] ?

I'd call them drm_gem_framebuffer_helper.[hc], just to highlight the gem<->kms connection a bit more.

Quoted 7 lines

Use these helpers in the cma library

3. Add drm_fb_helper_simple_init() and drm_fb_helper_simple_fini() Use them in drivers where applicable.

4. Add shmem library
Convert tinydrm to shmem.

Sounds like a good plan. I'll try to scrape away some review bandwidth to look at your patches.
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw

Re: [RFC 1/7] drm/gem: Add drm_gem_dumb_map_offset()

Den 24.07.2017 10.28, skrev Daniel Vetter:

Quoted 114 lines · Noralf Trønnes
Quoted 100 lines

Den 20.07.2017 10.00, skrev Daniel Vetter:

Quoted 89 lines · Noralf Trønnes
Quoted 30 lines

Den 19.07.2017 23.01, skrev Eric Anholt:

Quoted 7 lines
Quoted 3 lines · Noralf Trønnes

Add a common drm_driver.dumb_map_offset function for GEM backed drivers.

Instead of just introducing the new code, could we replace CMA's code with calls to this at the same time?

I have gone through all the drm_driver->dumb_map_offset implementations and found 23 drivers that could use it:
- 18 drivers use drm_gem_cma_dumb_map_offset()
- exynos_drm_gem_dumb_map_offset()
- mtk_drm_gem_dumb_map_offset()
- psb_gem_dumb_map_gtt()
- rockchip_gem_dumb_map_offset()
- tegra_bo_dumb_map_offset()

vgem_gem_dumb_map() can't use it because of this check:

      if (!obj->filp) {
          ret = -EINVAL;
          goto unref;
      }

armada_gem_dumb_map_offset() can't use it because of this check:

      /* Don't allow imported objects to be mapped */
      if (obj->obj.import_attach) {
          ret = -EINVAL;
          goto err_unref;
      }
Quoted 8 lines

I see that drivers must implement all 3 .dumb_* callbacks:

* To support dumb objects drivers must implement the
&drm_driver.dumb_create,
* &drm_driver.dumb_destroy and &drm_driver.dumb_map_offset operations. See
* there for further details.

I'm a fan of defaults, is there any reason we can't do this:

So in general we try not to set defaults for the main driver entry hooks, to avoid the helper functions leaking into core and becoming mandatory. So e.g. ->atomic_commit should never have a default of drm_atomic_helper_commit.

Same thought applied here for the dumb buffers - the idea is that drivers using any kind of buffer manager scheme could have dumb buffers, including maybe not having a buffer manager at all (and you get some cookie to direct vram allocations or whatever). But everyone ended up with gem, just with different kinds of backing storage implementations (cma, shmem or ttm).

I think it makes sense to make these the defaults and rip out the default assignment from all drivers.

Quoted 36 lines
   int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
                    void *data, struct drm_file *file_priv)
   {
       struct drm_mode_map_dumb *args = data;
       /* call driver ioctl to get mmap offset */
(quoted diff) +1 -2
-     if (!dev->driver->dumb_map_offset)
+    if (!dev->driver->dumb_create)
          return -ENOSYS;
 
-     return dev->driver->dumb_map_offset(file_priv, dev, args->handle,

&args->offset);

(quoted diff) +2 -0
+     if (dev->driver->dumb_map_offset)
+        return dev->driver->dumb_map_offset(file_priv, dev, args->handle,

&args->offset);

(quoted diff) +2 -0
+    else
+        return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
&args->offset);
   }

   int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
                   void *data, struct drm_file *file_priv)
   {
       struct drm_mode_destroy_dumb *args = data;
(quoted diff) +5 -2
-     if (!dev->driver->dumb_destroy)
+    if (!dev->driver->dumb_create)
           return -ENOSYS;
 
-    return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+     if (dev->driver->dumb_destroy)
+        return dev->driver->dumb_destroy(file_priv, dev, args->handle);
+    else
+        return drm_gem_dumb_destroy(file_priv, dev, args->handle);
   }
Quoted 8 lines
Quoted 2 lines

There are 36 drivers that use drm_gem_dumb_destroy() directly. vgem violates the docs and doesn't set .dumb_destroy.

Interesting, suprising it doesn't leak like mad.

Quoted 4 lines
Quoted 3 lines

I suspect that if we had CMA subclass from drm_fb_gem (or we move the gem objects to the base class), we could remove a lot of its code that you're copying in patch 2, as well.

Yes, that was the intention.

I guess we'd need to see more of the grand plan ...

Currently it looks like 4 patchsets:

Quoted 8 lines

1. Add drm_gem_dumb_map_offset() and implement defaults as discussed above.

2. Add drm_gem_object pointers to drm_framebuffer and create matching

    helpers for:
      drm_framebuffer_funcs->create_handle
      drm_framebuffer_funcs->destroy
      drm_mode_config_funcs->fb_create
      Should I put the functions in drm_framebuffer_helper.[h,c] ?

I'd call them drm_gem_framebuffer_helper.[hc], just to highlight the gem<->kms connection a bit more.

Quoted 2 lines

Use these helpers in the cma library

Quoted 7 lines
Quoted 5 lines

3. Add drm_fb_helper_simple_init() and drm_fb_helper_simple_fini() Use them in drivers where applicable.

4. Add shmem library
Convert tinydrm to shmem.

Sounds like a good plan. I'll try to scrape away some review bandwidth to look at your patches.

Thanks, this fanned out a bit more than first expected.

Noralf.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw

RFC 2/7 drm: Add GEM backed framebuffer library

In reply to Noralf Trønnes

Add a library for drivers that can use a simple representation of a GEM backed framebuffer.

---
 drivers/gpu/drm/Makefile            |   2 +-
 drivers/gpu/drm/drm_fb_gem_helper.c | 248 ++++++++++++++++++++++++++++++++++++
 include/drm/drm_fb_gem_helper.h     |  64 ++++++++++
 3 files changed, 313 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/drm_fb_gem_helper.c
 create mode 100644 include/drm/drm_fb_gem_helper.h
drivers/gpu/drm/Makefile +1 -1
index 24a066e..83d8b09 100644
@@ -33,7 +33,7 @@ drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_probe_helper.o \
 		drm_plane_helper.o drm_dp_mst_topology.o drm_atomic_helper.o \
 		drm_kms_helper_common.o drm_dp_dual_mode_helper.o \
 		drm_simple_kms_helper.o drm_modeset_helper.o \
-		drm_scdc_helper.o
+		drm_scdc_helper.o drm_fb_gem_helper.o
 
 drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
 drm_kms_helper-$(CONFIG_DRM_LOAD_EDID_FIRMWARE) += drm_edid_load.o
drivers/gpu/drm/drm_fb_gem_helper.c new file +248 -0

248 lines not on this page · raw message

include/drm/drm_fb_gem_helper.h new file +64 -0

64 lines not on this page · raw message

Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#11 lore raw

Re: [RFC 2/7] drm: Add GEM backed framebuffer library

Den 12.07.2017 15.46, skrev Noralf Trønnes:

Quoted 5 lines

Add a library for drivers that can use a simple representation of a GEM backed framebuffer.

---

This patch adds a gem backed drm_framebuffer like this:

struct drm_fb_gem {
    /**
     * @base: Base DRM framebuffer
     */
    struct drm_framebuffer base;
    /**
     * @obj: GEM object array backing the framebuffer. One object per
     * plane.
     */
    struct drm_gem_object *obj[4];
};

Now I wonder if it would be better to extend drm_framebuffer instead:

struct drm_framebuffer {
+ /**
+ * @obj: GEM objects backing the framebuffer, one per plane (optional).
+ */
+ struct drm_gem_object *obj[4];
};

The reason for this, is that I have looked through all drivers that subclass drm_framebuffer and found this:
- 11 drivers just adds drm_gem_object
- 2 drivers adds drm_gem_object and some more
- 6 drivers adds drm_gem_object indirectly through their subclassed drm_gem_object
- 3 drivers doesn't add drm_gem_object

Full details below:

Adds only drm_gem_object
------------------------

struct amdgpu_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct ast_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct bochs_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct cirrus_framebuffer {
    struct drm_framebuffer        base;
    struct drm_gem_object *obj;
};
struct drm_fb_cma {
    struct drm_framebuffer        fb;
    struct drm_gem_cma_object    *obj[4];
};
struct hibmc_framebuffer {
    struct drm_framebuffer fb;
    struct drm_gem_object *obj;
};
struct mga_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct mtk_drm_fb {
    struct drm_framebuffer    base;
    /* For now we only support a single plane */
    struct drm_gem_object    *gem_obj;
};
struct qxl_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct radeon_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct rockchip_drm_fb {
    struct drm_framebuffer fb;
    struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
};

#define ROCKCHIP_MAX_FB_BUFFER 3

Adds drm_gem_object and some more
---------------------------------

struct msm_framebuffer {
    struct drm_framebuffer base;
    const struct msm_format *format;
    struct drm_gem_object *planes[MAX_PLANE];
};

#define MAX_PLANE 4

struct virtio_gpu_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
    int x1, y1, x2, y2; /* dirty rect */
    spinlock_t dirty_lock;
    uint32_t hw_res_handle;
};

Indirectly adds a drm_gem_object
--------------------------------

struct armada_framebuffer {
    struct drm_framebuffer    fb;
    struct armada_gem_object *obj;
    uint8_t            fmt;
    uint8_t            mod;
};
struct exynos_drm_fb {
    struct drm_framebuffer    fb;
    struct exynos_drm_gem    *exynos_gem[MAX_FB_BUFFER];
    dma_addr_t            dma_addr[MAX_FB_BUFFER];
};

#define MAX_FB_BUFFER 4

struct intel_framebuffer {
    struct drm_framebuffer base;
    struct drm_i915_gem_object *obj;
    struct intel_rotation_info rot_info;

    /* for each plane in the normal GTT view */
    struct {
        unsigned int x, y;
    } normal[2];
    /* for each plane in the rotated GTT view */
    struct {
        unsigned int x, y;
        unsigned int pitch; /* pixels */
    } rotated[2];
};
struct nouveau_framebuffer {
    struct drm_framebuffer base;
    struct nouveau_bo *nvbo;
    struct nvkm_vma vma;
    u32 r_handle;
    u32 r_format;
    u32 r_pitch;
    struct nvif_object h_base[4];
    struct nvif_object h_core;
};
struct tegra_fb {
    struct drm_framebuffer base;
    struct tegra_bo **planes;
    unsigned int num_planes;
};
struct udl_framebuffer {
    struct drm_framebuffer base;
    struct udl_gem_object *obj;
    bool active_16; /* active on the 16-bit channel */
};

Does not add drm_gem_object
---------------------------

struct omap_framebuffer {
    struct drm_framebuffer base;
    int pin_count;
    const struct drm_format_info *format;
    enum omap_color_mode dss_format;
    struct plane planes[2];
    /* lock for pinning (pin_count and planes.paddr) */
    struct mutex lock;
};
struct psb_framebuffer {
    struct drm_framebuffer base;
    struct address_space *addr_space;
    struct fb_info *fbdev;
    struct gtt_range *gtt;
};
struct vmw_framebuffer {
    struct drm_framebuffer base;
    int (*pin)(struct vmw_framebuffer *fb);
    int (*unpin)(struct vmw_framebuffer *fb);
    bool dmabuf;
    struct ttm_base_object *user_obj;
    uint32_t user_handle;
};

Noralf.

Quoted 334 lines
  drivers/gpu/drm/Makefile            |   2 +-
  drivers/gpu/drm/drm_fb_gem_helper.c | 248 ++++++++++++++++++++++++++++++++++++
  include/drm/drm_fb_gem_helper.h     |  64 ++++++++++
  3 files changed, 313 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/drm_fb_gem_helper.c create mode 100644 include/drm/drm_fb_gem_helper.h

drivers/gpu/drm/Makefile +1 -1
index 24a066e..83d8b09 100644
@@ -33,7 +33,7 @@ drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_probe_helper.o \
  		drm_plane_helper.o drm_dp_mst_topology.o drm_atomic_helper.o \
  		drm_kms_helper_common.o drm_dp_dual_mode_helper.o \
  		drm_simple_kms_helper.o drm_modeset_helper.o \
-		drm_scdc_helper.o
+		drm_scdc_helper.o drm_fb_gem_helper.o
 
  drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
  drm_kms_helper-$(CONFIG_DRM_LOAD_EDID_FIRMWARE) += drm_edid_load.o
drivers/gpu/drm/drm_fb_gem_helper.c new file +248 -0
new file mode 100644
index 0000000..9a0da09
@@ -0,0 +1,248 @@
+/*
+ * drm fb gem helper functions
+ *
+ * Copyright (C) 2017 Noralf Trønnes
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/dma-buf.h>
+#include <linux/dma-mapping.h>
+#include <linux/module.h>
+#include <linux/reservation.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_atomic.h>
+#include <drm/drm_crtc.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_fb_helper.h>
+#include <drm/drm_fb_gem_helper.h>
+#include <drm/drm_gem.h>
+
+/**
+ * drm_fb_gem_get_obj() - Get GEM object for framebuffer
+ * @fb: The framebuffer
+ * @plane: Which plane
+ *
+ * Returns the GEM object for given framebuffer.
+ */
+struct drm_gem_object *drm_fb_gem_get_obj(struct drm_framebuffer *fb,
+					  unsigned int plane)
+{
+	struct drm_fb_gem *fb_gem = to_fb_gem(fb);
+
+	if (plane >= 4)
+		return NULL;
+
+	return fb_gem->obj[plane];
+}
+EXPORT_SYMBOL_GPL(drm_fb_gem_get_obj);
+
+/**
+ * drm_fb_gem_alloc - Allocate GEM backed framebuffer
+ * @dev: DRM device
+ * @mode_cmd: metadata from the userspace fb creation request
+ * @obj: GEM object nacking the framebuffer
+ * @num_planes: Number of planes
+ * @funcs: vtable to be used for the new framebuffer object
+ *
+ * Returns:
+ * Allocated struct drm_fb_gem * or error encoded pointer.
+ */
+struct drm_fb_gem *
+drm_fb_gem_alloc(struct drm_device *dev,
+		 const struct drm_mode_fb_cmd2 *mode_cmd,
+		 struct drm_gem_object **obj, unsigned int num_planes,
+		 const struct drm_framebuffer_funcs *funcs)
+{
+	struct drm_fb_gem *fb_gem;
+	int ret, i;
+
+	fb_gem = kzalloc(sizeof(*fb_gem), GFP_KERNEL);
+	if (!fb_gem)
+		return ERR_PTR(-ENOMEM);
+
+	drm_helper_mode_fill_fb_struct(dev, &fb_gem->base, mode_cmd);
+
+	for (i = 0; i < num_planes; i++)
+		fb_gem->obj[i] = obj[i];
+
+	ret = drm_framebuffer_init(dev, &fb_gem->base, funcs);
+	if (ret) {
+		dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret);
+		kfree(fb_gem);
+		return ERR_PTR(ret);
+	}
+
+	return fb_gem;
+}
+EXPORT_SYMBOL(drm_fb_gem_alloc);
+
+/**
+ * drm_fb_gem_destroy - Free GEM backed framebuffer
+ * @fb: DRM framebuffer
+ *
+ * Frees a GEM backed framebuffer with it's backing buffer(s) and the structure
+ * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
+ * callback.
+ */
+void drm_fb_gem_destroy(struct drm_framebuffer *fb)
+{
+	struct drm_fb_gem *fb_gem = to_fb_gem(fb);
+	int i;
+
+	for (i = 0; i < 4; i++) {
+		if (fb_gem->obj[i])
+			drm_gem_object_put_unlocked(fb_gem->obj[i]);
+	}
+
+	drm_framebuffer_cleanup(fb);
+	kfree(fb_gem);
+}
+EXPORT_SYMBOL(drm_fb_gem_destroy);
+
+/**
+ * drm_fb_gem_create_handle - Create handle for GEM backed framebuffer
+ * @fb: DRM framebuffer
+ * @file: drm file
+ * @handle: handle created
+ *
+ * Drivers can use this as their &drm_framebuffer_funcs->create_handle
+ * callback.
+ *
+ * Returns:
+ * 0 on success or a negative error code on failure.
+ */
+int drm_fb_gem_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
+			     unsigned int *handle)
+{
+	struct drm_fb_gem *fb_gem = to_fb_gem(fb);
+
+	return drm_gem_handle_create(file, fb_gem->obj[0], handle);
+}
+EXPORT_SYMBOL(drm_fb_gem_create_handle);
+
+/**
+ * drm_fb_gem_create_with_funcs() - helper function for the
+ *                                  &drm_mode_config_funcs.fb_create
+ *                                  callback
+ * @dev: DRM device
+ * @file: drm file for the ioctl call
+ * @mode_cmd: metadata from the userspace fb creation request
+ * @funcs: vtable to be used for the new framebuffer object
+ *
+ * This can be used to set &drm_framebuffer_funcs for drivers that need the
+ * &drm_framebuffer_funcs.dirty callback. Use drm_fb_gem_create() if you don't
+ * need to change &drm_framebuffer_funcs.
+ */
+struct drm_framebuffer *
+drm_fb_gem_create_with_funcs(struct drm_device *dev, struct drm_file *file,
+			     const struct drm_mode_fb_cmd2 *mode_cmd,
+			     const struct drm_framebuffer_funcs *funcs)
+{
+	const struct drm_format_info *info;
+	struct drm_gem_object *objs[4];
+	struct drm_fb_gem *fb_gem;
+	int ret, i;
+
+	info = drm_get_format_info(dev, mode_cmd);
+	if (!info)
+		return ERR_PTR(-EINVAL);
+
+	for (i = 0; i < info->num_planes; i++) {
+		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
+		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
+		unsigned int min_size;
+
+		objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
+		if (!objs[i]) {
+			dev_err(dev->dev, "Failed to lookup GEM object\n");
+			ret = -ENOENT;
+			goto err_gem_object_put;
+		}
+
+		min_size = (height - 1) * mode_cmd->pitches[i]
+			 + width * info->cpp[i]
+			 + mode_cmd->offsets[i];
+
+		if (objs[i]->size < min_size) {
+			drm_gem_object_put_unlocked(objs[i]);
+			ret = -EINVAL;
+			goto err_gem_object_put;
+		}
+	}
+
+	fb_gem = drm_fb_gem_alloc(dev, mode_cmd, objs, i, funcs);
+	if (IS_ERR(fb_gem)) {
+		ret = PTR_ERR(fb_gem);
+		goto err_gem_object_put;
+	}
+
+	return &fb_gem->base;
+
+err_gem_object_put:
+	for (i--; i >= 0; i--)
+		drm_gem_object_put_unlocked(objs[i]);
+
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(drm_fb_gem_create_with_funcs);
+
+static struct drm_framebuffer_funcs drm_fb_gem_fb_funcs = {
+	.destroy	= drm_fb_gem_destroy,
+	.create_handle	= drm_fb_gem_create_handle,
+};
+
+/**
+ * drm_fb_gem_create() - &drm_mode_config_funcs.fb_create callback function
+ * @dev: DRM device
+ * @file: drm file for the ioctl call
+ * @mode_cmd: metadata from the userspace fb creation request
+ *
+ * If your hardware has special alignment or pitch requirements these should be
+ * checked before calling this function. Use drm_fb_gem_create_with_funcs() if
+ * you need to set &drm_framebuffer_funcs.dirty.
+ */
+struct drm_framebuffer *
+drm_fb_gem_create(struct drm_device *dev, struct drm_file *file,
+		  const struct drm_mode_fb_cmd2 *mode_cmd)
+{
+	return drm_fb_gem_create_with_funcs(dev, file, mode_cmd,
+					    &drm_fb_gem_fb_funcs);
+}
+EXPORT_SYMBOL_GPL(drm_fb_gem_create);
+
+/**
+ * drm_fb_gem_prepare_fb() - Prepare gem framebuffer
+ * @plane: Which plane
+ * @state: Plane state attach fence to
+ *
+ * This should be set as the &struct drm_plane_helper_funcs.prepare_fb hook.
+ *
+ * This function checks if the plane FB has an dma-buf attached, extracts
+ * the exclusive fence and attaches it to plane state for the atomic helper
+ * to wait on.
+ *
+ * There is no need for cleanup_fb for gem based framebuffer drivers.
+ */
+int drm_fb_gem_prepare_fb(struct drm_plane *plane,
+			  struct drm_plane_state *state)
+{
+	struct dma_buf *dma_buf;
+	struct dma_fence *fence;
+
+	if ((plane->state->fb == state->fb) || !state->fb)
+		return 0;
+
+	dma_buf = drm_fb_gem_get_obj(state->fb, 0)->dma_buf;
+	if (dma_buf) {
+		fence = reservation_object_get_excl_rcu(dma_buf->resv);
+		drm_atomic_set_fence_for_plane(state, fence);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(drm_fb_gem_prepare_fb);
include/drm/drm_fb_gem_helper.h new file +54 -0
new file mode 100644
index 0000000..405a1e1
@@ -0,0 +1,64 @@
+#ifndef __DRM_FB_GEM_HELPER_H__
+#define __DRM_FB_GEM_HELPER_H__
+
+#include <drm/drm_framebuffer.h>
+
+struct drm_gem_shmem_object;
+struct drm_mode_fb_cmd2;
+struct drm_plane;
+struct drm_plane_state;
+
+/**
+ * struct drm_fb_gem - GEM backed framebuffer
+ */
+struct drm_fb_gem {
+	/**
+	 * @base: Base DRM framebuffer
+	 */
+	struct drm_framebuffer base;
+	/**
+	 * @obj: GEM object array backing the framebuffer. One object per
+	 * plane.
+	 */
+	struct drm_gem_object *obj[4];
+};
+
+static inline struct drm_fb_gem *to_fb_gem(struct drm_framebuffer *fb)
+{
+	return container_of(fb, struct drm_fb_gem, base);
+}
+
+struct drm_gem_object *drm_fb_gem_get_obj(struct drm_framebuffer *fb,
+					  unsigned int plane);
+struct drm_fb_gem *
+drm_fb_gem_alloc(struct drm_device *dev,
+		 const struct drm_mode_fb_cmd2 *mode_cmd,
+		 struct drm_gem_object **obj, unsigned int num_planes,
+		 const struct drm_framebuffer_funcs *funcs);
+void drm_fb_gem_destroy(struct drm_framebuffer *fb);
+int drm_fb_gem_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
+			     unsigned int *handle);
+
+struct drm_framebuffer *
+drm_fb_gem_create_with_funcs(struct drm_device *dev, struct drm_file *file,
+			     const struct drm_mode_fb_cmd2 *mode_cmd,
+			     const struct drm_framebuffer_funcs *funcs);
+struct drm_framebuffer *
+drm_fb_gem_create(struct drm_device *dev, struct drm_file *file,
+		  const struct drm_mode_fb_cmd2 *mode_cmd);
+
+
+int drm_fb_gem_prepare_fb(struct drm_plane *plane,
+			  struct drm_plane_state *state);
+
+

include/drm/drm_fb_gem_helper.h line 64

Quoted 10 lines
include/drm/drm_fb_gem_helper.h new file +10 -0
+
+
+#ifdef CONFIG_DEBUG_FS
+struct seq_file;
+
+int drm_fb_gem_debugfs_show(struct seq_file *m, void *arg);
+#endif
+
+#endif
+

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#12 lore raw

Re: [RFC 2/7] drm: Add GEM backed framebuffer library

Quoted 24 lines · Noralf Trønnes

Den 12.07.2017 15.46, skrev Noralf Trønnes:

Quoted 5 lines

Add a library for drivers that can use a simple representation of a GEM backed framebuffer.

---

This patch adds a gem backed drm_framebuffer like this:

struct drm_fb_gem {
     /**
      * @base: Base DRM framebuffer
      */
     struct drm_framebuffer base;
     /**
      * @obj: GEM object array backing the framebuffer. One object per
      * plane.
      */
     struct drm_gem_object *obj[4];
};

Now I wonder if it would be better to extend drm_framebuffer instead:

  struct drm_framebuffer {

include/drm/drm_fb_gem_helper.h

Quoted 5 lines
include/drm/drm_fb_gem_helper.h new file +4 -0
+    /**
+     * @obj: GEM objects backing the framebuffer, one per plane (optional).
+     */
+    struct drm_gem_object *obj[4];
  };

FWIW, I would love to see this tried. I think we would end up with some nice cleanups if we did so.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#13 lore raw

Re: [RFC 2/7] drm: Add GEM backed framebuffer library

In reply to Noralf Trønnes
Quoted 25 lines · Noralf Trønnes

Den 12.07.2017 15.46, skrev Noralf Trønnes:

Quoted 5 lines

Add a library for drivers that can use a simple representation of a GEM backed framebuffer.

---

This patch adds a gem backed drm_framebuffer like this:

struct drm_fb_gem {
    /**
     * @base: Base DRM framebuffer
     */
    struct drm_framebuffer base;
    /**
     * @obj: GEM object array backing the framebuffer. One object per
     * plane.
     */
    struct drm_gem_object *obj[4];
};

Now I wonder if it would be better to extend drm_framebuffer instead:

struct drm_framebuffer {

include/drm/drm_fb_gem_helper.h

Quoted 5 lines
include/drm/drm_fb_gem_helper.h new file +4 -0
+    /**
+     * @obj: GEM objects backing the framebuffer, one per plane (optional).
+     */
+    struct drm_gem_object *obj[4];
 };

I think we can directly embedd the gem obj pointers into the drm_framebuffer. Again the idea that there would be anything else than gem kinda didn't pan out, except for vmwgfx.

We could then have a gem version of drm_helper_mode_fill_fb_struct(), which also does the gem bo lookups.

Quoted 3 lines

The reason for this, is that I have looked through all drivers that subclass drm_framebuffer and found this:
- 11 drivers just adds drm_gem_object

Usually that's because they don't support yuv, so only need one plane.

Quoted 4 lines

- 2 drivers adds drm_gem_object and some more
- 6 drivers adds drm_gem_object indirectly through their subclassed drm_gem_object
- 3 drivers doesn't add drm_gem_object

I think for easier conversion we can leave all the driver-specific stuff intact, and just provide helpers that work on the core bits.

I guess this would also help a lot with unifying the cma fb helpers by essentially making them fully generic gem fb helpers? -Daniel

Quoted 522 lines

Full details below:

Adds only drm_gem_object
------------------------

struct amdgpu_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct ast_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct bochs_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct cirrus_framebuffer {
    struct drm_framebuffer        base;
    struct drm_gem_object *obj;
};
struct drm_fb_cma {
    struct drm_framebuffer        fb;
    struct drm_gem_cma_object    *obj[4];
};
struct hibmc_framebuffer {
    struct drm_framebuffer fb;
    struct drm_gem_object *obj;
};
struct mga_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct mtk_drm_fb {
    struct drm_framebuffer    base;
    /* For now we only support a single plane */
    struct drm_gem_object    *gem_obj;
};
struct qxl_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct radeon_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
};
struct rockchip_drm_fb {
    struct drm_framebuffer fb;
    struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
};

#define ROCKCHIP_MAX_FB_BUFFER 3

Adds drm_gem_object and some more
---------------------------------

struct msm_framebuffer {
    struct drm_framebuffer base;
    const struct msm_format *format;
    struct drm_gem_object *planes[MAX_PLANE];
};

#define MAX_PLANE 4

struct virtio_gpu_framebuffer {
    struct drm_framebuffer base;
    struct drm_gem_object *obj;
    int x1, y1, x2, y2; /* dirty rect */
    spinlock_t dirty_lock;
    uint32_t hw_res_handle;
};

Indirectly adds a drm_gem_object
--------------------------------

struct armada_framebuffer {
    struct drm_framebuffer    fb;
    struct armada_gem_object *obj;
    uint8_t            fmt;
    uint8_t            mod;
};
struct exynos_drm_fb {
    struct drm_framebuffer    fb;
    struct exynos_drm_gem    *exynos_gem[MAX_FB_BUFFER];
    dma_addr_t            dma_addr[MAX_FB_BUFFER];
};

#define MAX_FB_BUFFER 4

struct intel_framebuffer {
    struct drm_framebuffer base;
    struct drm_i915_gem_object *obj;
    struct intel_rotation_info rot_info;

    /* for each plane in the normal GTT view */
    struct {
        unsigned int x, y;
    } normal[2];
    /* for each plane in the rotated GTT view */
    struct {
        unsigned int x, y;
        unsigned int pitch; /* pixels */
    } rotated[2];
};
struct nouveau_framebuffer {
    struct drm_framebuffer base;
    struct nouveau_bo *nvbo;
    struct nvkm_vma vma;
    u32 r_handle;
    u32 r_format;
    u32 r_pitch;
    struct nvif_object h_base[4];
    struct nvif_object h_core;
};
struct tegra_fb {
    struct drm_framebuffer base;
    struct tegra_bo **planes;
    unsigned int num_planes;
};
struct udl_framebuffer {
    struct drm_framebuffer base;
    struct udl_gem_object *obj;
    bool active_16; /* active on the 16-bit channel */
};

Does not add drm_gem_object
---------------------------

struct omap_framebuffer {
    struct drm_framebuffer base;
    int pin_count;
    const struct drm_format_info *format;
    enum omap_color_mode dss_format;
    struct plane planes[2];
    /* lock for pinning (pin_count and planes.paddr) */
    struct mutex lock;
};
struct psb_framebuffer {
    struct drm_framebuffer base;
    struct address_space *addr_space;
    struct fb_info *fbdev;
    struct gtt_range *gtt;
};
struct vmw_framebuffer {
    struct drm_framebuffer base;
    int (*pin)(struct vmw_framebuffer *fb);
    int (*unpin)(struct vmw_framebuffer *fb);
    bool dmabuf;
    struct ttm_base_object *user_obj;
    uint32_t user_handle;
};

Noralf.

Quoted 333 lines
  drivers/gpu/drm/Makefile            |   2 +-
  drivers/gpu/drm/drm_fb_gem_helper.c | 248 ++++++++++++++++++++++++++++++++++++
  include/drm/drm_fb_gem_helper.h     |  64 ++++++++++
  3 files changed, 313 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/drm_fb_gem_helper.c create mode 100644 include/drm/drm_fb_gem_helper.h

drivers/gpu/drm/Makefile +1 -1
index 24a066e..83d8b09 100644
@@ -33,7 +33,7 @@ drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_probe_helper.o \
  		drm_plane_helper.o drm_dp_mst_topology.o drm_atomic_helper.o \
  		drm_kms_helper_common.o drm_dp_dual_mode_helper.o \
  		drm_simple_kms_helper.o drm_modeset_helper.o \
-		drm_scdc_helper.o
+		drm_scdc_helper.o drm_fb_gem_helper.o
  drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
  drm_kms_helper-$(CONFIG_DRM_LOAD_EDID_FIRMWARE) += drm_edid_load.o
drivers/gpu/drm/drm_fb_gem_helper.c new file +248 -0
new file mode 100644
index 0000000..9a0da09
@@ -0,0 +1,248 @@
+/*
+ * drm fb gem helper functions
+ *
+ * Copyright (C) 2017 Noralf Trønnes
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/dma-buf.h>
+#include <linux/dma-mapping.h>
+#include <linux/module.h>
+#include <linux/reservation.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_atomic.h>
+#include <drm/drm_crtc.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_fb_helper.h>
+#include <drm/drm_fb_gem_helper.h>
+#include <drm/drm_gem.h>
+
+/**
+ * drm_fb_gem_get_obj() - Get GEM object for framebuffer
+ * @fb: The framebuffer
+ * @plane: Which plane
+ *
+ * Returns the GEM object for given framebuffer.
+ */
+struct drm_gem_object *drm_fb_gem_get_obj(struct drm_framebuffer *fb,
+					  unsigned int plane)
+{
+	struct drm_fb_gem *fb_gem = to_fb_gem(fb);
+
+	if (plane >= 4)
+		return NULL;
+
+	return fb_gem->obj[plane];
+}
+EXPORT_SYMBOL_GPL(drm_fb_gem_get_obj);
+
+/**
+ * drm_fb_gem_alloc - Allocate GEM backed framebuffer
+ * @dev: DRM device
+ * @mode_cmd: metadata from the userspace fb creation request
+ * @obj: GEM object nacking the framebuffer
+ * @num_planes: Number of planes
+ * @funcs: vtable to be used for the new framebuffer object
+ *
+ * Returns:
+ * Allocated struct drm_fb_gem * or error encoded pointer.
+ */
+struct drm_fb_gem *
+drm_fb_gem_alloc(struct drm_device *dev,
+		 const struct drm_mode_fb_cmd2 *mode_cmd,
+		 struct drm_gem_object **obj, unsigned int num_planes,
+		 const struct drm_framebuffer_funcs *funcs)
+{
+	struct drm_fb_gem *fb_gem;
+	int ret, i;
+
+	fb_gem = kzalloc(sizeof(*fb_gem), GFP_KERNEL);
+	if (!fb_gem)
+		return ERR_PTR(-ENOMEM);
+
+	drm_helper_mode_fill_fb_struct(dev, &fb_gem->base, mode_cmd);
+
+	for (i = 0; i < num_planes; i++)
+		fb_gem->obj[i] = obj[i];
+
+	ret = drm_framebuffer_init(dev, &fb_gem->base, funcs);
+	if (ret) {
+		dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret);
+		kfree(fb_gem);
+		return ERR_PTR(ret);
+	}
+
+	return fb_gem;
+}
+EXPORT_SYMBOL(drm_fb_gem_alloc);
+
+/**
+ * drm_fb_gem_destroy - Free GEM backed framebuffer
+ * @fb: DRM framebuffer
+ *
+ * Frees a GEM backed framebuffer with it's backing buffer(s) and the structure
+ * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
+ * callback.
+ */
+void drm_fb_gem_destroy(struct drm_framebuffer *fb)
+{
+	struct drm_fb_gem *fb_gem = to_fb_gem(fb);
+	int i;
+
+	for (i = 0; i < 4; i++) {
+		if (fb_gem->obj[i])
+			drm_gem_object_put_unlocked(fb_gem->obj[i]);
+	}
+
+	drm_framebuffer_cleanup(fb);
+	kfree(fb_gem);
+}
+EXPORT_SYMBOL(drm_fb_gem_destroy);
+
+/**
+ * drm_fb_gem_create_handle - Create handle for GEM backed framebuffer
+ * @fb: DRM framebuffer
+ * @file: drm file
+ * @handle: handle created
+ *
+ * Drivers can use this as their &drm_framebuffer_funcs->create_handle
+ * callback.
+ *
+ * Returns:
+ * 0 on success or a negative error code on failure.
+ */
+int drm_fb_gem_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
+			     unsigned int *handle)
+{
+	struct drm_fb_gem *fb_gem = to_fb_gem(fb);
+
+	return drm_gem_handle_create(file, fb_gem->obj[0], handle);
+}
+EXPORT_SYMBOL(drm_fb_gem_create_handle);
+
+/**
+ * drm_fb_gem_create_with_funcs() - helper function for the
+ *                                  &drm_mode_config_funcs.fb_create
+ *                                  callback
+ * @dev: DRM device
+ * @file: drm file for the ioctl call
+ * @mode_cmd: metadata from the userspace fb creation request
+ * @funcs: vtable to be used for the new framebuffer object
+ *
+ * This can be used to set &drm_framebuffer_funcs for drivers that need the
+ * &drm_framebuffer_funcs.dirty callback. Use drm_fb_gem_create() if you don't
+ * need to change &drm_framebuffer_funcs.
+ */
+struct drm_framebuffer *
+drm_fb_gem_create_with_funcs(struct drm_device *dev, struct drm_file *file,
+			     const struct drm_mode_fb_cmd2 *mode_cmd,
+			     const struct drm_framebuffer_funcs *funcs)
+{
+	const struct drm_format_info *info;
+	struct drm_gem_object *objs[4];
+	struct drm_fb_gem *fb_gem;
+	int ret, i;
+
+	info = drm_get_format_info(dev, mode_cmd);
+	if (!info)
+		return ERR_PTR(-EINVAL);
+
+	for (i = 0; i < info->num_planes; i++) {
+		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
+		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
+		unsigned int min_size;
+
+		objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
+		if (!objs[i]) {
+			dev_err(dev->dev, "Failed to lookup GEM object\n");
+			ret = -ENOENT;
+			goto err_gem_object_put;
+		}
+
+		min_size = (height - 1) * mode_cmd->pitches[i]
+			 + width * info->cpp[i]
+			 + mode_cmd->offsets[i];
+
+		if (objs[i]->size < min_size) {
+			drm_gem_object_put_unlocked(objs[i]);
+			ret = -EINVAL;
+			goto err_gem_object_put;
+		}
+	}
+
+	fb_gem = drm_fb_gem_alloc(dev, mode_cmd, objs, i, funcs);
+	if (IS_ERR(fb_gem)) {
+		ret = PTR_ERR(fb_gem);
+		goto err_gem_object_put;
+	}
+
+	return &fb_gem->base;
+
+err_gem_object_put:
+	for (i--; i >= 0; i--)
+		drm_gem_object_put_unlocked(objs[i]);
+
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(drm_fb_gem_create_with_funcs);
+
+static struct drm_framebuffer_funcs drm_fb_gem_fb_funcs = {
+	.destroy	= drm_fb_gem_destroy,
+	.create_handle	= drm_fb_gem_create_handle,
+};
+
+/**
+ * drm_fb_gem_create() - &drm_mode_config_funcs.fb_create callback function
+ * @dev: DRM device
+ * @file: drm file for the ioctl call
+ * @mode_cmd: metadata from the userspace fb creation request
+ *
+ * If your hardware has special alignment or pitch requirements these should be
+ * checked before calling this function. Use drm_fb_gem_create_with_funcs() if
+ * you need to set &drm_framebuffer_funcs.dirty.
+ */
+struct drm_framebuffer *
+drm_fb_gem_create(struct drm_device *dev, struct drm_file *file,
+		  const struct drm_mode_fb_cmd2 *mode_cmd)
+{
+	return drm_fb_gem_create_with_funcs(dev, file, mode_cmd,
+					    &drm_fb_gem_fb_funcs);
+}
+EXPORT_SYMBOL_GPL(drm_fb_gem_create);
+
+/**
+ * drm_fb_gem_prepare_fb() - Prepare gem framebuffer
+ * @plane: Which plane
+ * @state: Plane state attach fence to
+ *
+ * This should be set as the &struct drm_plane_helper_funcs.prepare_fb hook.
+ *
+ * This function checks if the plane FB has an dma-buf attached, extracts
+ * the exclusive fence and attaches it to plane state for the atomic helper
+ * to wait on.
+ *
+ * There is no need for cleanup_fb for gem based framebuffer drivers.
+ */
+int drm_fb_gem_prepare_fb(struct drm_plane *plane,
+			  struct drm_plane_state *state)
+{
+	struct dma_buf *dma_buf;
+	struct dma_fence *fence;
+
+	if ((plane->state->fb == state->fb) || !state->fb)
+		return 0;
+
+	dma_buf = drm_fb_gem_get_obj(state->fb, 0)->dma_buf;
+	if (dma_buf) {
+		fence = reservation_object_get_excl_rcu(dma_buf->resv);
+		drm_atomic_set_fence_for_plane(state, fence);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(drm_fb_gem_prepare_fb);
include/drm/drm_fb_gem_helper.h new file +54 -0
new file mode 100644
index 0000000..405a1e1
@@ -0,0 +1,64 @@
+#ifndef __DRM_FB_GEM_HELPER_H__
+#define __DRM_FB_GEM_HELPER_H__
+
+#include <drm/drm_framebuffer.h>
+
+struct drm_gem_shmem_object;
+struct drm_mode_fb_cmd2;
+struct drm_plane;
+struct drm_plane_state;
+
+/**
+ * struct drm_fb_gem - GEM backed framebuffer
+ */
+struct drm_fb_gem {
+	/**
+	 * @base: Base DRM framebuffer
+	 */
+	struct drm_framebuffer base;
+	/**
+	 * @obj: GEM object array backing the framebuffer. One object per
+	 * plane.
+	 */
+	struct drm_gem_object *obj[4];
+};
+
+static inline struct drm_fb_gem *to_fb_gem(struct drm_framebuffer *fb)
+{
+	return container_of(fb, struct drm_fb_gem, base);
+}
+
+struct drm_gem_object *drm_fb_gem_get_obj(struct drm_framebuffer *fb,
+					  unsigned int plane);
+struct drm_fb_gem *
+drm_fb_gem_alloc(struct drm_device *dev,
+		 const struct drm_mode_fb_cmd2 *mode_cmd,
+		 struct drm_gem_object **obj, unsigned int num_planes,
+		 const struct drm_framebuffer_funcs *funcs);
+void drm_fb_gem_destroy(struct drm_framebuffer *fb);
+int drm_fb_gem_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
+			     unsigned int *handle);
+
+struct drm_framebuffer *
+drm_fb_gem_create_with_funcs(struct drm_device *dev, struct drm_file *file,
+			     const struct drm_mode_fb_cmd2 *mode_cmd,
+			     const struct drm_framebuffer_funcs *funcs);
+struct drm_framebuffer *
+drm_fb_gem_create(struct drm_device *dev, struct drm_file *file,
+		  const struct drm_mode_fb_cmd2 *mode_cmd);
+
+
+int drm_fb_gem_prepare_fb(struct drm_plane *plane,
+			  struct drm_plane_state *state);
+
+

include/drm/drm_fb_gem_helper.h line 64

Quoted 10 lines
include/drm/drm_fb_gem_helper.h new file +10 -0
+
+
+#ifdef CONFIG_DEBUG_FS
+struct seq_file;
+
+int drm_fb_gem_debugfs_show(struct seq_file *m, void *arg);
+#endif
+
+#endif
+

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#14 lore raw

Re: [RFC 2/7] drm: Add GEM backed framebuffer library

Den 20.07.2017 10.10, skrev Daniel Vetter:

Quoted 29 lines · Noralf Trønnes
Quoted 23 lines

Den 12.07.2017 15.46, skrev Noralf Trønnes:

Quoted 5 lines

Add a library for drivers that can use a simple representation of a GEM backed framebuffer.

---

This patch adds a gem backed drm_framebuffer like this:

struct drm_fb_gem {
     /**
      * @base: Base DRM framebuffer
      */
     struct drm_framebuffer base;
     /**
      * @obj: GEM object array backing the framebuffer. One object per
      * plane.
      */
     struct drm_gem_object *obj[4];
};

Now I wonder if it would be better to extend drm_framebuffer instead:

  struct drm_framebuffer {

include/drm/drm_fb_gem_helper.h

Quoted 5 lines
include/drm/drm_fb_gem_helper.h new file +4 -0
+    /**
+     * @obj: GEM objects backing the framebuffer, one per plane (optional).
+     */
+    struct drm_gem_object *obj[4];
  };
Quoted 6 lines

I think we can directly embedd the gem obj pointers into the drm_framebuffer. Again the idea that there would be anything else than gem kinda didn't pan out, except for vmwgfx.

We could then have a gem version of drm_helper_mode_fill_fb_struct(), which also does the gem bo lookups.

How about this, I've copied from drm_fb_cma_create_with_funcs():

/**
* drm_helper_mode_fill_fb_gem - fill out framebuffer metadata and
lookup GEM
* @dev: DRM device
* @file_priv: DRM file to lookup buffers from
* @fb: drm_framebuffer object to fill out
* @mode_cmd: metadata from the userspace fb creation request *
* This helper can be used in a drivers fb_create callback to pre-fill
the fb's
* metadata fields and lookup the GEM buffer object(s).
*
* Returns:
* 0 on success or a negative error code on failure.
*/
int drm_helper_mode_fill_fb_gem(struct drm_device *dev,
struct drm_file *file_priv,
struct drm_framebuffer *fb,
const struct drm_mode_fb_cmd2 *mode_cmd)

{
    int i, ret;

    drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
    if (!fb->format)
        return -EINVAL;

    for (i = 0; i < fb->format->num_planes; i++) {
        unsigned int width = mode_cmd->width /
                     (i ? fb->format->hsub : 1);
        unsigned int height = mode_cmd->height /
                      (i ? fb->format->vsub : 1);
        unsigned int min_size;

        fb->objs[i] = drm_gem_object_lookup(file_priv,
                            mode_cmd->handles[i]);
        if (!fb->objs[i]) {
            ret = -ENOENT;
            goto err_gem_object_put;
        }

        min_size = (height - 1) * mode_cmd->pitches[i] +
               width * fb->format->cpp[i] + mode_cmd->offsets[i];

        if (fb->objs[i]->size < min_size) {
            drm_gem_object_put_unlocked(fb->objs[i]);
            fb->objs[i] = NULL;
            ret = -EINVAL;
            goto err_gem_object_put;
        }
    }

err_gem_object_put:

    for (i--; i >= 0; i--) {
        drm_gem_object_put_unlocked(fb->objs[i]);
        fb->objs[i] = NULL;
    }

    return ret;
}

EXPORT_SYMBOL(drm_helper_mode_fill_fb_gem);

Quoted 9 lines
Quoted 3 lines

The reason for this, is that I have looked through all drivers that subclass drm_framebuffer and found this:
- 11 drivers just adds drm_gem_object

Usually that's because they don't support yuv, so only need one plane.

Quoted 4 lines

- 2 drivers adds drm_gem_object and some more
- 6 drivers adds drm_gem_object indirectly through their subclassed drm_gem_object
- 3 drivers doesn't add drm_gem_object

Quoted 5 lines

I think for easier conversion we can leave all the driver-specific stuff intact, and just provide helpers that work on the core bits.

I guess this would also help a lot with unifying the cma fb helpers by essentially making them fully generic gem fb helpers?

Yes both cma and shmem library can now use these helpers. But pushing the helpers all the way out to the cma drivers will be too much for me.

Any chance the fbdev helper could get a struct drm_file? It would be nice to use drm_driver.dumb_create and drm_mode_config_funcs.fb_create to get a framebuffer for fbdev.

Noralf.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#15 lore raw

Re: [RFC 2/7] drm: Add GEM backed framebuffer library

Quoted 31 lines · Noralf Trønnes

Den 20.07.2017 10.10, skrev Daniel Vetter:

Quoted 29 lines · Noralf Trønnes
Quoted 28 lines

Den 12.07.2017 15.46, skrev Noralf Trønnes:

Quoted 5 lines

Add a library for drivers that can use a simple representation of a GEM backed framebuffer.

---

This patch adds a gem backed drm_framebuffer like this:

struct drm_fb_gem {
     /**
      * @base: Base DRM framebuffer
      */
     struct drm_framebuffer base;
     /**
      * @obj: GEM object array backing the framebuffer. One object per
      * plane.
      */
     struct drm_gem_object *obj[4];
};

Now I wonder if it would be better to extend drm_framebuffer instead:

  struct drm_framebuffer {
include/drm/drm_fb_gem_helper.h new file +4 -0
+    /**
+     * @obj: GEM objects backing the framebuffer, one per plane (optional).
+     */
+    struct drm_gem_object *obj[4];
  };
Quoted 8 lines
Quoted 6 lines

I think we can directly embedd the gem obj pointers into the drm_framebuffer. Again the idea that there would be anything else than gem kinda didn't pan out, except for vmwgfx.

We could then have a gem version of drm_helper_mode_fill_fb_struct(), which also does the gem bo lookups.

How about this, I've copied from drm_fb_cma_create_with_funcs():

Hm, we might as well call it drm_fb_gem_create_with_funcs, to stay in line with the naming scheme we have.

Quoted 70 lines

/**
* drm_helper_mode_fill_fb_gem - fill out framebuffer metadata and lookup
GEM
* @dev: DRM device
* @file_priv: DRM file to lookup buffers from
* @fb: drm_framebuffer object to fill out
* @mode_cmd: metadata from the userspace fb creation request *
* This helper can be used in a drivers fb_create callback to pre-fill the
fb's
* metadata fields and lookup the GEM buffer object(s).
*
* Returns:
* 0 on success or a negative error code on failure.
*/
int drm_helper_mode_fill_fb_gem(struct drm_device *dev,
struct drm_file *file_priv,
struct drm_framebuffer *fb,
const struct drm_mode_fb_cmd2 *mode_cmd)

{
    int i, ret;

    drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
    if (!fb->format)
        return -EINVAL;

    for (i = 0; i < fb->format->num_planes; i++) {
        unsigned int width = mode_cmd->width /
                     (i ? fb->format->hsub : 1);
        unsigned int height = mode_cmd->height /
                      (i ? fb->format->vsub : 1);
        unsigned int min_size;

        fb->objs[i] = drm_gem_object_lookup(file_priv,
                            mode_cmd->handles[i]);
        if (!fb->objs[i]) {
            ret = -ENOENT;
            goto err_gem_object_put;
        }

        min_size = (height - 1) * mode_cmd->pitches[i] +
               width * fb->format->cpp[i] + mode_cmd->offsets[i];

        if (fb->objs[i]->size < min_size) {
            drm_gem_object_put_unlocked(fb->objs[i]);
            fb->objs[i] = NULL;
            ret = -EINVAL;
            goto err_gem_object_put;
        }
    }

err_gem_object_put:

    for (i--; i >= 0; i--) {
        drm_gem_object_put_unlocked(fb->objs[i]);
        fb->objs[i] = NULL;
    }

    return ret;
}

EXPORT_SYMBOL(drm_helper_mode_fill_fb_gem);

Quoted 9 lines
Quoted 3 lines

The reason for this, is that I have looked through all drivers that subclass drm_framebuffer and found this:
- 11 drivers just adds drm_gem_object

Usually that's because they don't support yuv, so only need one plane.

Quoted 4 lines

- 2 drivers adds drm_gem_object and some more
- 6 drivers adds drm_gem_object indirectly through their subclassed drm_gem_object
- 3 drivers doesn't add drm_gem_object

Quoted 8 lines
Quoted 5 lines

I think for easier conversion we can leave all the driver-specific stuff intact, and just provide helpers that work on the core bits.

I guess this would also help a lot with unifying the cma fb helpers by essentially making them fully generic gem fb helpers?

Yes both cma and shmem library can now use these helpers. But pushing the helpers all the way out to the cma drivers will be too much for me.

No worries, we can do this one by one across different people.

Quoted 4 lines

Any chance the fbdev helper could get a struct drm_file? It would be nice to use drm_driver.dumb_create and drm_mode_config_funcs.fb_create to get a framebuffer for fbdev.

David Herrmann had a patch series to allow the kernel to create a drm_file without a real struct file attached behind it. I think we landed the prep work, but not any of the work to move the fbdev helper over to it. I'll ping him whether he still has some of those patches somewhere. -Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#16 lore raw

RFC 3/7 drm/fb-helper: Support shadow buffer with deferred io

In reply to Noralf Trønnes

This adds support for using a shadow buffer for fbdev that is copied to the real buffer during dirty flushing.
shmem buffers doesn't work with the fbdev deferred io functions, because it touches page->mapping and page->lru.

---
 drivers/gpu/drm/drm_fb_helper.c | 21 +++++++++++++++++++--
 include/drm/drm_fb_helper.h     |  8 ++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_fb_helper.c +19 -2
index 721511d..d8b2690 100644
@@ -739,8 +739,25 @@ static void drm_fb_helper_dirty_work(struct work_struct *work)
 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
 
 	/* call dirty callback only when it has been really touched */
-	if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
-		helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
+	if (clip_copy.x1 > clip_copy.x2 || clip_copy.y1 > clip_copy.y2)
+		return;
+
+	/* using shadow buffer? */
+	if (helper->defio_vaddr) {
+		unsigned int pitch = helper->fb->pitches[0];
+		u8 cpp = helper->fb->format->cpp[0];
+		unsigned int y;
+
+		for (y = clip_copy.y1; y < clip_copy.y2; y++) {
+			size_t offset = (y * pitch) + (clip_copy.x1 * cpp);
+
+			memcpy(helper->defio_vaddr + offset,
+			       helper->fbdev->screen_buffer + offset,
+			       (clip_copy.x2 - clip_copy.x1) * cpp);
+		}
+	}
+
+	helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
 }
 
 /**
include/drm/drm_fb_helper.h +8 -0
index ea170b9..159d5dd 100644
@@ -203,6 +203,14 @@ struct drm_fb_helper {
 	struct drm_clip_rect dirty_clip;
 	spinlock_t dirty_lock;
 	struct work_struct dirty_work;
+	/**
+	 * @defio_vaddr:
+	 *
+	 * Destination address for shadowed and deferred framebuffer. If this
+	 * is set, the dirty area is copied from &fb_info->screen_buffer to
+	 * this address before calling &drm_framebuffer_funcs->dirty.
+	 */
+	void *defio_vaddr;
 	struct work_struct resume_work;
 
 	/**
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#17 lore raw

RFC 4/7 drm/fb-helper: Add simple init/fini functions

In reply to Noralf Trønnes

This adds some simple init/fini/fb_probe helpers for drivers that don't need anything special in their fbdev emulation.

---
 drivers/gpu/drm/drm_fb_helper.c | 208 ++++++++++++++++++++++++++++++++++++++++
 include/drm/drm_fb_helper.h     |  42 ++++++++
 2 files changed, 250 insertions(+)
drivers/gpu/drm/drm_fb_helper.c +208 -0
index d8b2690..77f2943 100644
@@ -2515,3 +2515,211 @@ int __init drm_fb_helper_modinit(void)
 	return 0;
 }
 EXPORT_SYMBOL(drm_fb_helper_modinit);
+
+static int drm_fb_helper_defio_init(struct drm_fb_helper *helper)
+{
+	struct fb_info *fbi = helper->fbdev;
+	struct fb_deferred_io *fbdefio;
+	struct fb_ops *fbops;
+
+	/*
+	 * Per device structures are needed because:
+	 * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
+	 * fbdefio: individual delays
+	 */
+	fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
+	fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
+	if (!fbdefio || !fbops) {
+		kfree(fbdefio);
+		kfree(fbops);
+		return -ENOMEM;
+	}
+
+	*fbops = *fbi->fbops;
+	fbi->fbops = fbops;
+
+	fbdefio->delay = msecs_to_jiffies(50);
+	fbdefio->deferred_io = drm_fb_helper_deferred_io;
+	fbi->fbdefio = fbdefio;
+	fb_deferred_io_init(fbi);
+
+	return 0;
+}
+
+static void drm_fb_helper_defio_fini(struct drm_fb_helper *helper)
+{
+	if (!helper->fbdev || !helper->fbdev->fbdefio)
+		return;
+
+	fb_deferred_io_cleanup(helper->fbdev);
+	kfree(helper->fbdev->fbdefio);
+	kfree(helper->fbdev->fbops);
+}
+
+/**
+ * @drm_fb_helper_mode_cmd - Convert fbdev to fb description
+ * @mode_cmd: addfb description
+ * @sizes: fbdev description
+ *
+ * Returns: Size of framebuffer.
+ */
+size_t drm_fb_helper_mode_cmd(struct drm_mode_fb_cmd2 *mode_cmd,
+			      struct drm_fb_helper_surface_size *sizes)
+{
+	memset(mode_cmd, 0, sizeof(*mode_cmd));
+	mode_cmd->width = sizes->surface_width;
+	mode_cmd->height = sizes->surface_height;
+	mode_cmd->pitches[0] = sizes->surface_width *
+			       DIV_ROUND_UP(sizes->surface_bpp, 8);
+	mode_cmd->pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
+							sizes->surface_depth);
+
+	return mode_cmd->pitches[0] * mode_cmd->height;
+}
+
+/**
+ * drm_fb_helper_simple_fb_probe - simple &drm_fb_helper_funcs->fb_probe helper
+ * @helper: fbdev emulation structure
+ * @sizes: fbdev description
+ * @fb: Attached DRM framebuffer
+ * @fbops: fbdev operations
+ * @vaddr: Virtual address for &fb_info->screen_buffer
+ * @paddr: Address for &fb_fix_screeninfo->smem_start.
+ * @size: Framebuffer size
+ *
+ * Drivers can use this in their &drm_fb_helper_funcs->fb_probe function.
+ *
+ * Returns:
+ * 0 on success or a negative error code on failure.
+ */
+int drm_fb_helper_simple_fb_probe(struct drm_fb_helper *helper,
+				  struct drm_fb_helper_surface_size *sizes,
+				  struct drm_framebuffer *fb,
+				  struct fb_ops *fbops, void *vaddr,
+				  unsigned long paddr, size_t size)
+{
+	struct fb_info *fbi;
+	int ret;
+
+	DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
+		      sizes->surface_width, sizes->surface_height,
+		      sizes->surface_bpp);
+
+	fbi = drm_fb_helper_alloc_fbi(helper);
+	if (IS_ERR(fbi))
+		return PTR_ERR(fbi);
+
+	helper->fb = fb;
+	fbi->par = helper;
+	fbi->fbops = fbops;
+
+	drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
+	drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
+
+	fbi->screen_base = vaddr;
+	fbi->fix.smem_start = paddr;
+	fbi->screen_size = size;
+	fbi->fix.smem_len = size;
+
+	if (fb->funcs->dirty) {
+		ret = drm_fb_helper_defio_init(helper);
+		if (ret)
+			goto err_helper_fini;
+	}
+
+	return 0;
+
+err_helper_fini:
+	drm_fb_helper_fini(helper);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_fb_helper_simple_fb_probe);
+
+/**
+ * drm_fb_helper_simple_init - Simple fbdev emulation init
+ * @dev: drm device
+ * @helper: driver-allocated fbdev helper structure to initialize
+ * @bpp_sel: bpp value to use for the framebuffer configuration
+ * @max_conn_count: max connector count
+ * @funcs: pointer to structure of functions associate with this helper
+ *
+ * Simple fbdev emulation initialization. This function calls:
+ * drm_fb_helper_prepare(), drm_fb_helper_init(),
+ * drm_fb_helper_single_add_all_connectors() and
+ * drm_fb_helper_initial_config().
+ *
+ * Returns:
+ * 0 on success or a negative error code on failure.
+ */
+int drm_fb_helper_simple_init(struct drm_device *dev,
+			      struct drm_fb_helper *helper, int bpp_sel,
+			      int max_conn_count,
+			      const struct drm_fb_helper_funcs *funcs)
+{
+	int ret;
+
+	drm_fb_helper_prepare(dev, helper, funcs);
+
+	ret = drm_fb_helper_init(dev, helper, max_conn_count);
+	if (ret < 0) {
+		DRM_DEV_ERROR(dev->dev, "Failed to initialize fb helper.\n");
+		return ret;
+	}
+
+	ret = drm_fb_helper_single_add_all_connectors(helper);
+	if (ret < 0) {
+		DRM_DEV_ERROR(dev->dev, "Failed to add connectors.\n");
+		goto err_drm_fb_helper_fini;
+
+	}
+
+	ret = drm_fb_helper_initial_config(helper, bpp_sel);
+	if (ret < 0) {
+		DRM_DEV_ERROR(dev->dev, "Failed to set initial hw config.\n");
+		goto err_drm_fb_helper_fini;
+	}
+
+	return 0;
+
+err_drm_fb_helper_fini:
+	drm_fb_helper_fini(helper);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(drm_fb_helper_simple_init);
+
+/**
+ * drm_fb_helper_simple_fini - Simple fbdev cleanup
+ * @helper: fbdev emulation structure
+ *
+ * Simple fbdev emulation cleanup. This function unregisters fbdev, cleans up
+ * deferred io if necessary, removes framebuffer, frees shadow buffer if used
+ * and finally cleans up @helper. The driver if responsible for freeing the
+ * @helper structure.
+ */
+void drm_fb_helper_simple_fini(struct drm_fb_helper *helper)
+{
+	if (!helper)
+		return;
+
+	drm_fb_helper_unregister_fbi(helper);
+
+	drm_fb_helper_defio_fini(helper);
+
+	if (helper->fb) {
+		drm_framebuffer_remove(helper->fb);
+		/*
+		 * for some reason if drm_fb_helper_set_par() is called we end
+		 * up with an extra ref taken on the framebuffer...
+		 */
+		if (drm_framebuffer_read_refcount(helper->fb))
+			drm_framebuffer_put(helper->fb);
+	}
+
+	if (helper->defio_vaddr)
+		kvfree(helper->fbdev->screen_buffer);
+
+	drm_fb_helper_fini(helper);
+}
+EXPORT_SYMBOL_GPL(drm_fb_helper_simple_fini);
include/drm/drm_fb_helper.h +42 -0

54 lines not on this page · raw message

Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#18 lore raw

RFC 5/7 drm: Add library for shmem backed GEM objects

In reply to Noralf Trønnes

This adds a library for shmem backed GEM objects with the necessary drm_driver callbacks.

---
 drivers/gpu/drm/Kconfig                |   6 +
 drivers/gpu/drm/Makefile               |   1 +
 drivers/gpu/drm/drm_gem_shmem_helper.c | 628 +++++++++++++++++++++++++++++++++
 include/drm/drm_gem_shmem_helper.h     | 131 +++++++
 4 files changed, 766 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c
 create mode 100644 include/drm/drm_gem_shmem_helper.h
drivers/gpu/drm/Kconfig +6 -0
index 83cb2a8..8e709c2 100644
@@ -145,6 +145,12 @@ config DRM_KMS_CMA_HELPER
 	help
 	  Choose this if you need the KMS CMA helper functions
 
+config DRM_GEM_SHMEM_HELPER
+	bool
+	depends on DRM
+	help
+	  Choose this if you need the GEM SHMEM helper functions
+
 config DRM_VM
 	bool
 	depends on DRM && MMU
drivers/gpu/drm/Makefile +1 -0
index 83d8b09..598c247 100644
@@ -23,6 +23,7 @@ drm-$(CONFIG_DRM_LIB_RANDOM) += lib/drm_random.o
 drm-$(CONFIG_DRM_VM) += drm_vm.o
 drm-$(CONFIG_COMPAT) += drm_ioc32.o
 drm-$(CONFIG_DRM_GEM_CMA_HELPER) += drm_gem_cma_helper.o
+drm-$(CONFIG_DRM_GEM_SHMEM_HELPER) += drm_gem_shmem_helper.o
 drm-$(CONFIG_PCI) += ati_pcigart.o
 drm-$(CONFIG_DRM_PANEL) += drm_panel.o
 drm-$(CONFIG_OF) += drm_of.o
drivers/gpu/drm/drm_gem_shmem_helper.c new file +628 -0

628 lines not on this page · raw message

include/drm/drm_gem_shmem_helper.h new file +131 -0

131 lines not on this page · raw message

Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#19 lore raw

RFC 6/7 drm: Add kms library for shmem backed GEM

In reply to Noralf Trønnes

This adds kms helpers for the shmem gem library.

---
 drivers/gpu/drm/Kconfig               |  11 +++
 drivers/gpu/drm/Makefile              |   1 +
 drivers/gpu/drm/drm_fb_shmem_helper.c | 168 ++++++++++++++++++++++++++++++++++
 include/drm/drm_fb_shmem_helper.h     |  18 ++++
 4 files changed, 198 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_fb_shmem_helper.c
 create mode 100644 include/drm/drm_fb_shmem_helper.h
drivers/gpu/drm/Kconfig +11 -0
index 8e709c2..4c9010d 100644
@@ -151,6 +151,17 @@ config DRM_GEM_SHMEM_HELPER
 	help
 	  Choose this if you need the GEM SHMEM helper functions
 
+config DRM_KMS_SHMEM_HELPER
+	bool
+	depends on DRM
+	select DRM_GEM_SHMEM_HELPER
+	select DRM_KMS_FB_HELPER
+	select FB_SYS_FILLRECT
+	select FB_SYS_COPYAREA
+	select FB_SYS_IMAGEBLIT
+	help
+	  Choose this if you need the KMS SHMEM helper functions
+
 config DRM_VM
 	bool
 	depends on DRM && MMU
drivers/gpu/drm/Makefile +1 -0
index 598c247..706037c 100644
@@ -40,6 +40,7 @@ drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
 drm_kms_helper-$(CONFIG_DRM_LOAD_EDID_FIRMWARE) += drm_edid_load.o
 drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
 drm_kms_helper-$(CONFIG_DRM_KMS_CMA_HELPER) += drm_fb_cma_helper.o
+drm_kms_helper-$(CONFIG_DRM_KMS_SHMEM_HELPER) += drm_fb_shmem_helper.o
 drm_kms_helper-$(CONFIG_DRM_DP_AUX_CHARDEV) += drm_dp_aux_dev.o
 
 obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
drivers/gpu/drm/drm_fb_shmem_helper.c new file +168 -0

168 lines not on this page · raw message

include/drm/drm_fb_shmem_helper.h new file +18 -0
new file mode 100644
index 0000000..5c57131
@@ -0,0 +1,18 @@
+#ifndef __DRM_FB_SHMEM_HELPER_H__
+#define __DRM_FB_SHMEM_HELPER_H__
+
+struct drm_fb_helper_surface_size;
+struct drm_framebuffer_funcs;
+struct drm_fb_helper;
+
+int drm_fb_shmem_fbdev_probe(struct drm_fb_helper *helper,
+			     struct drm_fb_helper_surface_size *sizes,
+			     const struct drm_framebuffer_funcs *fb_funcs);
+
+#ifdef CONFIG_DEBUG_FS
+struct seq_file;
+
+int drm_fb_shmem_debugfs_show(struct seq_file *m, void *arg);
+#endif
+
+#endif
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#20 lore raw

RFC 7/7 drm/tinydrm: Switch from CMA to shmem buffers

In reply to Noralf Trønnes

This move makes tinydrm useful for more drivers. tinydrm doesn't need continous memory, but at the time it was convinient to use the CMA library. The spi core can do dma on vmalloc addresses making this possible.

---
 drivers/gpu/drm/tinydrm/Kconfig             |   2 +-
 drivers/gpu/drm/tinydrm/core/tinydrm-core.c | 147 ++++++++++++----------------
 drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c |   4 +-
 drivers/gpu/drm/tinydrm/mi0283qt.c          |   2 +-
 drivers/gpu/drm/tinydrm/mipi-dbi.c          |  41 +++-----
 include/drm/tinydrm/tinydrm.h               |  32 ++----
 6 files changed, 92 insertions(+), 136 deletions(-)
drivers/gpu/drm/tinydrm/Kconfig +1 -1
index 3504c53..da9b8b8 100644
@@ -2,7 +2,7 @@ menuconfig DRM_TINYDRM
 	tristate "Support for simple displays"
 	depends on DRM
 	select DRM_KMS_HELPER
-	select DRM_KMS_CMA_HELPER
+	select DRM_KMS_SHMEM_HELPER
 	select BACKLIGHT_LCD_SUPPORT
 	select BACKLIGHT_CLASS_DEVICE
 	help
drivers/gpu/drm/tinydrm/core/tinydrm-core.c +65 -82

220 lines not on this page · raw message

drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c +2 -2
index ec43fb7..a30518c 100644
@@ -145,7 +145,7 @@ EXPORT_SYMBOL(tinydrm_display_pipe_update);
  * @pipe: Simple display pipe
  * @plane_state: Plane state
  *
- * This function uses drm_fb_cma_prepare_fb() to check if the plane FB has an
+ * This function uses drm_fb_shmem_prepare_fb() to check if the plane FB has a
  * dma-buf attached, extracts the exclusive fence and attaches it to plane
  * state for the atomic helper to wait on. Drivers can use this as their
  * &drm_simple_display_pipe_funcs->prepare_fb callback.
@@ -153,7 +153,7 @@ EXPORT_SYMBOL(tinydrm_display_pipe_update);
 int tinydrm_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
 				    struct drm_plane_state *plane_state)
 {
-	return drm_fb_cma_prepare_fb(&pipe->plane, plane_state);
+	return drm_fb_gem_prepare_fb(&pipe->plane, plane_state);
 }
 EXPORT_SYMBOL(tinydrm_display_pipe_prepare_fb);
 
drivers/gpu/drm/tinydrm/mi0283qt.c +1 -1
index 482ff1c3..8d11cd0 100644
@@ -132,7 +132,7 @@ static const struct drm_display_mode mi0283qt_mode = {
 	TINYDRM_MODE(320, 240, 58, 43),
 };
 
-DEFINE_DRM_GEM_CMA_FOPS(mi0283qt_fops);
+DEFINE_DRM_GEM_SHMEM_FOPS(mi0283qt_fops);
 
 static struct drm_driver mi0283qt_driver = {
 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
drivers/gpu/drm/tinydrm/mipi-dbi.c +14 -27

90 lines not on this page · raw message

include/drm/tinydrm/tinydrm.h +9 -23
index 00b800d..93d7a03 100644
@@ -10,8 +10,9 @@
 #ifndef __LINUX_TINYDRM_H
 #define __LINUX_TINYDRM_H
 
-#include <drm/drm_gem_cma_helper.h>
-#include <drm/drm_fb_cma_helper.h>
+#include <drm/drm_gem_shmem_helper.h>
+#include <drm/drm_fb_gem_helper.h>
+#include <drm/drm_fb_shmem_helper.h>
 #include <drm/drm_simple_kms_helper.h>
 
 /**
@@ -19,7 +20,7 @@
  * @drm: DRM device
  * @pipe: Display pipe structure
  * @dirty_lock: Serializes framebuffer flushing
- * @fbdev_cma: CMA fbdev structure
+ * @fbdev: fbdev helper
  * @suspend_state: Atomic state when suspended
  * @fb_funcs: Framebuffer functions used when creating framebuffers
  */
@@ -27,7 +28,7 @@ struct tinydrm_device {
 	struct drm_device *drm;
 	struct drm_simple_display_pipe pipe;
 	struct mutex dirty_lock;
-	struct drm_fbdev_cma *fbdev_cma;
+	struct drm_fb_helper *fbdev;
 	struct drm_atomic_state *suspend_state;
 	const struct drm_framebuffer_funcs *fb_funcs;
 };
@@ -45,20 +46,8 @@ pipe_to_tinydrm(struct drm_simple_display_pipe *pipe)
  * the &drm_driver structure.
  */
 #define TINYDRM_GEM_DRIVER_OPS \
-	.gem_free_object	= tinydrm_gem_cma_free_object, \
-	.gem_vm_ops		= &drm_gem_cma_vm_ops, \
-	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd, \
-	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle, \
-	.gem_prime_import	= drm_gem_prime_import, \
-	.gem_prime_export	= drm_gem_prime_export, \
-	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table, \
-	.gem_prime_import_sg_table = tinydrm_gem_cma_prime_import_sg_table, \
-	.gem_prime_vmap		= drm_gem_cma_prime_vmap, \
-	.gem_prime_vunmap	= drm_gem_cma_prime_vunmap, \
-	.gem_prime_mmap		= drm_gem_cma_prime_mmap, \
-	.dumb_create		= drm_gem_cma_dumb_create, \
-	.dumb_map_offset	= drm_gem_cma_dumb_map_offset, \
-	.dumb_destroy		= drm_gem_dumb_destroy
+	.gem_create_object = tinydrm_gem_create_object, \
+	DRM_GEM_SHMEM_DRIVER_OPS
 
 /**
  * TINYDRM_MODE - tinydrm display mode
@@ -84,11 +73,8 @@ pipe_to_tinydrm(struct drm_simple_display_pipe *pipe)
 	.clock = 1 /* pass validation */
 
 void tinydrm_lastclose(struct drm_device *drm);
-void tinydrm_gem_cma_free_object(struct drm_gem_object *gem_obj);
-struct drm_gem_object *
-tinydrm_gem_cma_prime_import_sg_table(struct drm_device *drm,
-				      struct dma_buf_attachment *attach,
-				      struct sg_table *sgt);
+struct drm_gem_object *tinydrm_gem_create_object(struct drm_device *drm,
+						 size_t size);
 int devm_tinydrm_init(struct device *parent, struct tinydrm_device *tdev,
 		      const struct drm_framebuffer_funcs *fb_funcs,
 		      struct drm_driver *driver);
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel