drm: Add library for shmem backed GEM objects
Subsystems (4)
PATCH v8 1/1 drm: Add library for shmem backed GEM objects
From: Noralf Trønnes <[email protected]>
This adds a library for shmem backed GEM objects.
v8:
- export drm_gem_shmem_create_with_handle
- call mapping_set_gfp_mask to set default zone to GFP_HIGHUSER
- Add helper drm_gem_shmem_get_pages_sgt()
v7:
- Use write-combine for mmap instead. This is the more common case. (robher)
v6:
- Fix uninitialized variable issue in an error path (anholt).
- Add a drm_gem_shmem_vm_open() to the fops to get proper refcounting of the pages (anholt).
v5:
- Drop drm_gem_shmem_prime_mmap() (Daniel Vetter)
- drm_gem_shmem_mmap(): Subtract drm_vma_node_start() to get the real vma->vm_pgoff
- drm_gem_shmem_fault(): Use vmf->pgoff now that vma->vm_pgoff is correct
v4:
- Drop cache modes (Thomas Hellstrom)
- Add a GEM attached vtable
v3:
- Grammar (Sam Ravnborg)
- s/drm_gem_shmem_put_pages_unlocked/drm_gem_shmem_put_pages_locked/ (Sam Ravnborg)
- Add debug output in error path (Sam Ravnborg)
--- Documentation/gpu/drm-kms-helpers.rst | 12 + drivers/gpu/drm/Kconfig | 6 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_gem_shmem_helper.c | 625 +++++++++++++++++++++++++ include/drm/drm_gem_shmem_helper.h | 159 +++++++ 5 files changed, 803 insertions(+) create mode 100644 drivers/gpu/drm/drm_gem_shmem_helper.c create mode 100644 include/drm/drm_gem_shmem_helper.h
Documentation/gpu/drm-kms-helpers.rst +12 -0
@@ -369,3 +369,15 @@ Legacy CRTC/Modeset Helper Functions Reference .. kernel-doc:: drivers/gpu/drm/drm_crtc_helper.c :export: + +SHMEM GEM Helper Reference +========================== + +.. kernel-doc:: drivers/gpu/drm/drm_gem_shmem_helper.c + :doc: overview + +.. kernel-doc:: include/drm/drm_gem_shmem_helper.h + :internal: + +.. kernel-doc:: drivers/gpu/drm/drm_gem_shmem_helper.c + :export:
drivers/gpu/drm/Kconfig +6 -0
@@ -173,6 +173,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
@@ -25,6 +25,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 +625 -0
625 lines not on this page · raw message
include/drm/drm_gem_shmem_helper.h new file +159 -0
159 lines not on this page · raw message
Signature · 6 lines
2.19.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 3 lines · Rob Herring
From: Noralf Trønnes <[email protected]>
This adds a library for shmem backed GEM objects.
I read through the whole thing, and the only bit I didn't like (drm_gem_shmem_create_with_handle returns an unrefcounted object pointer) was a copy-and-paste from CMA that we should fix in both places, and has no downside in the current usage since the pointer value is unused. Just one question left:
Quoted 3 lines
v7:
- Use write-combine for mmap instead. This is the more common case. (robher)
I don't actually see this in the code. Wasn't there supposed to be a:
vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 1/1 drm/v3d: Use the new shmem helpers to reduce driver boilerplate.
In reply to Rob HerringThe new shmem helpers from Noralf and Rob abstract out a bunch of our BO creation and mapping code.
v2: Use the new sgt getter, and flag pages as dirty before freeing.
--- drivers/gpu/drm/v3d/Kconfig | 1 + drivers/gpu/drm/v3d/v3d_bo.c | 317 ++++++++++------------------------ drivers/gpu/drm/v3d/v3d_drv.c | 27 +-- drivers/gpu/drm/v3d/v3d_drv.h | 14 +- drivers/gpu/drm/v3d/v3d_gem.c | 12 +- drivers/gpu/drm/v3d/v3d_irq.c | 8 +- drivers/gpu/drm/v3d/v3d_mmu.c | 11 +- 7 files changed, 120 insertions(+), 270 deletions(-)
drivers/gpu/drm/v3d/Kconfig +1 -0
@@ -5,6 +5,7 @@ config DRM_V3D depends on COMMON_CLK depends on MMU select DRM_SCHED + select DRM_GEM_SHMEM_HELPER help Choose this option if you have a system that has a Broadcom V3D 3.x or newer GPU, such as BCM7268.
drivers/gpu/drm/v3d/v3d_bo.c +95 -222
390 lines not on this page · raw message
drivers/gpu/drm/v3d/v3d_drv.c +3 -24
@@ -161,17 +161,7 @@ v3d_postclose(struct drm_device *dev, struct drm_file *file)kfree (v3d_priv); } -static const struct file_operations v3d_drm_fops = { - .owner =THIS_MODULE , - .open = drm_open, - .release = drm_release, - .unlocked_ioctl = drm_ioctl, - .mmap = v3d_mmap, - .poll = drm_poll, - .read = drm_read, - .compat_ioctl = drm_compat_ioctl, - .llseek = noop_llseek, -}; +DEFINE_DRM_GEM_SHMEM_FOPS (v3d_drm_fops);/* DRM_AUTH is required on SUBMIT_CL for now, while we don't have GMP * protection between clients. Note that render nodes would be be
@@ -189,12 +179,6 @@ static const struct drm_ioctl_desc v3d_drm_ioctls[] = { DRM_IOCTL_DEF_DRV(V3D_SUBMIT_TFU, v3d_submit_tfu_ioctl, DRM_RENDER_ALLOW | DRM_AUTH), }; -static const struct vm_operations_struct v3d_vm_ops = { - .fault = v3d_gem_fault, - .open = drm_gem_vm_open, - .close = drm_gem_vm_close, -}; -static struct drm_driver v3d_drm_driver = { .driver_features = (DRIVER_GEM | DRIVER_RENDER |
@@ -208,16 +192,11 @@ static struct drm_driver v3d_drm_driver = { .debugfs_init = v3d_debugfs_init, #endif - .gem_free_object_unlocked = v3d_free_object, - .gem_vm_ops = &v3d_vm_ops, - + .gem_create_object = v3d_create_object, .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 = v3d_prime_get_sg_table, .gem_prime_import_sg_table = v3d_prime_import_sg_table, - .gem_prime_mmap = v3d_prime_mmap, + .gem_prime_mmap = drm_gem_prime_mmap, .ioctls = v3d_drm_ioctls, .num_ioctls = ARRAY_SIZE(v3d_drm_ioctls),
drivers/gpu/drm/v3d/v3d_drv.h +3 -11
@@ -5,6 +5,7 @@#include <drm/drmP.h> #include <drm/drm_encoder.h> #include <drm/drm_gem.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/gpu_scheduler.h> #include "uapi/drm/v3d_drm.h"
@@ -111,16 +112,10 @@ struct v3d_file_priv { };struct v3d_bo { -struct drm_gem_object base; - -struct mutex lock; +struct drm_gem_shmem_object base;struct drm_mm_node node; -u32 pages_refcount; -struct page **pages; -struct sg_table *sgt; -/* List entry for the BO's position in * v3d_exec_info->unref_list */
@@ -258,6 +253,7 @@ static inline unsigned long nsecs_to_jiffies_timeout(const u64 n) }/* v3d_bo.c */ +struct drm_gem_object *v3d_create_object (struct drm_device *dev,size_t size);void v3d_free_object (struct drm_gem_object *gem_obj);struct v3d_bo *v3d_bo_create (struct drm_device *dev,struct drm_file *file_priv,size_t size);
@@ -267,10 +263,6 @@ int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);int v3d_get_bo_offset_ioctl (struct drm_device *dev,void *data,struct drm_file *file_priv); -vm_fault_t v3d_gem_fault (struct vm_fault *vmf); -int v3d_mmap (struct file *filp,struct vm_area_struct *vma); -int v3d_prime_mmap (struct drm_gem_object *obj,struct vm_area_struct *vma); -struct sg_table *v3d_prime_get_sg_table (struct drm_gem_object *obj);struct drm_gem_object *v3d_prime_import_sg_table (struct drm_device *dev,struct dma_buf_attachment *attach,struct sg_table *sgt);
drivers/gpu/drm/v3d/v3d_gem.c +7 -5
@@ -201,7 +201,8 @@ v3d_attach_object_fences(struct v3d_bo **bos, int bo_count,for (i =0 ; i < bo_count; i++) {/* XXX: Use shared fences for read-only objects. */ -reservation_object_add_excl_fence (bos[i]->base.resv, fence); +reservation_object_add_excl_fence (bos[i]->base.base.resv, + fence); } }
@@ -237,7 +238,8 @@ v3d_lock_bo_reservations(struct v3d_bo **bos,* before we commit the CL to the hardware. */ for (i =0 ; i < bo_count; i++) { - ret =reservation_object_reserve_shared (bos[i]->base.resv,1 ); + ret =reservation_object_reserve_shared (bos[i]->base.base.resv, +1 );if (ret) {v3d_unlock_bo_reservations (bos, bo_count, acquire_ctx);
@@ -345,11 +347,11 @@ v3d_exec_cleanup(struct kref *ref)dma_fence_put (exec->render_done_fence);for (i =0 ; i < exec->bo_count; i++) -drm_gem_object_put_unlocked (&exec->bo[i]->base); +drm_gem_object_put_unlocked (&exec->bo[i]->base.base);kvfree (exec->bo);list_for_each_entry_safe (bo, save, &exec->unref_list, unref_head) { -drm_gem_object_put_unlocked (&bo->base); +drm_gem_object_put_unlocked (&bo->base.base); }pm_runtime_mark_last_busy (v3d->dev);
@@ -376,7 +378,7 @@ v3d_tfu_job_cleanup(struct kref *ref)for (i =0 ; i <ARRAY_SIZE (job->bo); i++) {if (job->bo[i]) -drm_gem_object_put_unlocked (&job->bo[i]->base); +drm_gem_object_put_unlocked (&job->bo[i]->base.base); }pm_runtime_mark_last_busy (v3d->dev);
drivers/gpu/drm/v3d/v3d_irq.c +5 -3
@@ -37,12 +37,14 @@ v3d_overflow_mem_work(struct work_struct *work) container_of(work, struct v3d_dev, overflow_mem_work);struct drm_device *dev = &v3d->drm;struct v3d_bo *bo =v3d_bo_create (dev,NULL /* XXX: GMP */ ,256 *1024 ); +struct drm_gem_object *obj;unsigned long irqflags;if (IS_ERR (bo)) {DRM_ERROR ("Couldn't allocate binner overflow mem\n" );return ; } + obj = &bo->base.base;/* We lost a race, and our work task came in after the bin job * completed and exited. This can happen because the HW
@@ -59,15 +61,15 @@ v3d_overflow_mem_work(struct work_struct *work) goto out; } -drm_gem_object_get (&bo->base); +drm_gem_object_get (obj);list_add_tail (&bo->unref_head, &v3d->bin_job->unref_list);spin_unlock_irqrestore (&v3d->job_lock, irqflags);V3D_CORE_WRITE (0 ,V3D_PTB_BPOA , bo->node.start <<PAGE_SHIFT ); -V3D_CORE_WRITE (0 ,V3D_PTB_BPOS , bo->base.size); +V3D_CORE_WRITE (0 ,V3D_PTB_BPOS , obj->size); out: -drm_gem_object_put_unlocked (&bo->base); +drm_gem_object_put_unlocked (obj); } static irqreturn_t
drivers/gpu/drm/v3d/v3d_mmu.c +6 -5
@@ -83,13 +83,14 @@ int v3d_mmu_set_page_table(struct v3d_dev *v3d)void v3d_mmu_insert_ptes (struct v3d_bo *bo) { -struct v3d_dev *v3d =to_v3d_dev (bo->base.dev); +struct drm_gem_shmem_object *shmem_obj = &bo->base; +struct v3d_dev *v3d =to_v3d_dev (shmem_obj->base.dev);u32 page = bo->node.start;u32 page_prot =V3D_PTE_WRITEABLE |V3D_PTE_VALID ;unsigned int count;struct scatterlist *sgl; -for_each_sg (bo->sgt->sgl, sgl, bo->sgt->nents, count) { +for_each_sg (shmem_obj->sgt->sgl, sgl, shmem_obj->sgt->nents, count) {u32 page_address =sg_dma_address (sgl) >>V3D_MMU_PAGE_SHIFT ;u32 pte = page_prot | page_address;u32 i;
@@ -102,7 +103,7 @@ void v3d_mmu_insert_ptes(struct v3d_bo *bo) }WARN_ON_ONCE (page - bo->node.start != - bo->base.size >>V3D_MMU_PAGE_SHIFT ); + shmem_obj->base.size >>V3D_MMU_PAGE_SHIFT );if (v3d_mmu_flush_all (v3d))dev_err (v3d->dev,"MMU flush timeout\n" );
@@ -110,8 +111,8 @@ void v3d_mmu_insert_ptes(struct v3d_bo *bo)void v3d_mmu_remove_ptes (struct v3d_bo *bo) { -struct v3d_dev *v3d =to_v3d_dev (bo->base.dev); -u32 npages = bo->base.size >>V3D_MMU_PAGE_SHIFT ; +struct v3d_dev *v3d =to_v3d_dev (bo->base.base.dev); +u32 npages = bo->base.base.size >>V3D_MMU_PAGE_SHIFT ;u32 page;for (page = bo->node.start; page < bo->node.start + npages; page++)
Signature · 6 lines
2.20.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH] drm/v3d: Use the new shmem helpers to reduce driver boilerplate.
Quoted 20 lines · Eric Anholt
The new shmem helpers from Noralf and Rob abstract out a bunch of our BO creation and mapping code.
v2: Use the new sgt getter, and flag pages as dirty before freeing.
Signed-off-by Eric Anholt <[email protected]>drivers/gpu/drm/v3d/v3d_bo.c +2
-2@@ -185,83 +33,120 @@ void v3d_free_object(struct drm_gem_object *obj)struct v3d_dev *v3d =to_v3d_dev (obj->dev);struct v3d_bo *bo =to_v3d_bo (obj); +v3d_mmu_remove_ptes (bo); +mutex_lock (&v3d->bo_lock); v3d->bo_stats.num_allocated--; v3d->bo_stats.pages_allocated -= obj->size >>PAGE_SHIFT ;mutex_unlock (&v3d->bo_lock); -v3d_bo_put_pages (bo); -
drivers/gpu/drm/v3d/v3d_bo.c void v3d_free_object(struct drm_gem_object *obj) · line 47
Quoted 10 lines
drivers/gpu/drm/v3d/v3d_bo.c +1
-5-if (obj->import_attach) -drm_prime_gem_destroy (obj, bo->sgt); - -v3d_mmu_remove_ptes (bo);spin_lock (&v3d->mm_lock);drm_mm_remove_node (&bo->node);spin_unlock (&v3d->mm_lock); -mutex_destroy (&bo->lock); +drm_gem_shmem_put_pages (&bo->base);
This put_pages() should be dropped -- it generated a WARN because the shared helpers already do a put_pages after freeing the sgt. The CTS had passed, so I missed it until after I'd sent the patch.
Quoted 13 lines · Eric Anholt
Quoted 3 lines · Rob Herring
From: Noralf Trønnes <[email protected]>
This adds a library for shmem backed GEM objects.
I read through the whole thing, and the only bit I didn't like (
drm_gem_shmem_create_with_handlereturns an unrefcounted object pointer) was a copy-and-paste from CMA that we should fix in both places, and has no downside in the current usage since the pointer value is unused. Just one question left:
Quoted 7 lines
Quoted 3 lines
v7:
- Use write-combine for mmap instead. This is the more common case. (robher)I don't actually see this in the code. Wasn't there supposed to be a:
vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
That's the default if you trace drm_gem_mmap() -> drm_gem_mmap_obj(). My wording here should have been more clear.
Rob
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH] drm/v3d: Use the new shmem helpers to reduce driver boilerplate.
In reply to Eric AnholtQuoted 22 lines · Eric Anholt
The new shmem helpers from Noralf and Rob abstract out a bunch of our BO creation and mapping code.
v2: Use the new sgt getter, and flag pages as dirty before freeing.
Signed-off-by Eric Anholt <[email protected]>--- drivers/gpu/drm/v3d/Kconfig | 1 + drivers/gpu/drm/v3d/v3d_bo.c | 317 ++++++++++------------------------ drivers/gpu/drm/v3d/v3d_drv.c | 27 +-- drivers/gpu/drm/v3d/v3d_drv.h | 14 +- drivers/gpu/drm/v3d/v3d_gem.c | 12 +- drivers/gpu/drm/v3d/v3d_irq.c | 8 +- drivers/gpu/drm/v3d/v3d_mmu.c | 11 +- 7 files changed, 120 insertions(+), 270 deletions(-)drivers/gpu/drm/v3d/v3d_gem.c +0
-0
drivers/gpu/drm/v3d/v3d_gem.c v3d_attach_object_fences(struct v3d_bo **bos, int bo_count, · line 205
Quoted 6 lines
drivers/gpu/drm/v3d/v3d_gem.c +2
-1@@ -201,7 +201,8 @@ v3d_attach_object_fences(struct v3d_bo **bos, int bo_count,for (i =0 ; i < bo_count; i++) {/* XXX: Use shared fences for read-only objects. */ -reservation_object_add_excl_fence (bos[i]->base.resv, fence); +reservation_object_add_excl_fence (bos[i]->base.base.resv, + fence);
All these 'bos[i]->base.base' occurrences are not the prettiest. Changing your bos array to a struct drm_gem_object ** instead would help. That's what I did for panfrost. Though I've not looked at were you actually need the v3d_bo.
Either way,
Rob
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 18 lines · Rob Herring
Quoted 17 lines · Eric Anholt
Quoted 3 lines · Rob Herring
From: Noralf Trønnes <[email protected]>
This adds a library for shmem backed GEM objects.
I read through the whole thing, and the only bit I didn't like (
drm_gem_shmem_create_with_handlereturns an unrefcounted object pointer) was a copy-and-paste from CMA that we should fix in both places, and has no downside in the current usage since the pointer value is unused. Just one question left:Quoted 3 lines
v7:
- Use write-combine for mmap instead. This is the more common case. (robher)
Quoted 6 lines
Quoted 3 lines
I don't actually see this in the code. Wasn't there supposed to be a:
vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));That's the default if you trace
drm_gem_mmap()->drm_gem_mmap_obj(). My wording here should have been more clear.
Ah, there it is! Added review to the helpers, and I've pushed both patches now.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH] drm/v3d: Use the new shmem helpers to reduce driver boilerplate.
In reply to Rob HerringQuoted 24 lines · Rob Herring
Quoted 23 lines · Eric Anholt
The new shmem helpers from Noralf and Rob abstract out a bunch of our BO creation and mapping code.
v2: Use the new sgt getter, and flag pages as dirty before freeing.
Signed-off-by Eric Anholt <[email protected]>--- drivers/gpu/drm/v3d/Kconfig | 1 + drivers/gpu/drm/v3d/v3d_bo.c | 317 ++++++++++------------------------ drivers/gpu/drm/v3d/v3d_drv.c | 27 +-- drivers/gpu/drm/v3d/v3d_drv.h | 14 +- drivers/gpu/drm/v3d/v3d_gem.c | 12 +- drivers/gpu/drm/v3d/v3d_irq.c | 8 +- drivers/gpu/drm/v3d/v3d_mmu.c | 11 +- 7 files changed, 120 insertions(+), 270 deletions(-)drivers/gpu/drm/v3d/v3d_gem.c +0
-0@@ -201,7 +201,8 @@ v3d_attach_object_fences(struct v3d_bo **bos, int bo_count,
Quoted 10 lines
drivers/gpu/drm/v3d/v3d_gem.c v3d_attach_object_fences(struct v3d_bo **bos, int bo_count, · line 205
Quoted 5 lines
drivers/gpu/drm/v3d/v3d_gem.c +2
-1for (i =0 ; i < bo_count; i++) {/* XXX: Use shared fences for read-only objects. */ -reservation_object_add_excl_fence (bos[i]->base.resv, fence); +reservation_object_add_excl_fence (bos[i]->base.base.resv, + fence);All these 'bos[i]->base.base' occurrences are not the prettiest. Changing your bos array to a struct
drm_gem_object** instead would help. That's what I did for panfrost. Though I've not looked at were you actually need thev3d_bo.
Agreed that would probably be a goodcleanup, but I'm hoping I can land the compute shaders first. I've been doing a lot of rebasing of that series and I want to just get it done.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel