1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018 Noralf Trønnes
6 #include <linux/dma-buf.h>
7 #include <linux/export.h>
8 #include <linux/mutex.h>
9 #include <linux/shmem_fs.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
14 #include <drm/drm_device.h>
15 #include <drm/drm_drv.h>
16 #include <drm/drm_gem_shmem_helper.h>
17 #include <drm/drm_prime.h>
18 #include <drm/drm_print.h>
23 * This library provides helpers for GEM objects backed by shmem buffers
24 * allocated using anonymous pageable memory.
26 * Functions that operate on the GEM object receive struct &drm_gem_shmem_object.
27 * For GEM callback helpers in struct &drm_gem_object functions, see likewise
28 * named functions with an _object_ infix (e.g., drm_gem_shmem_object_vmap() wraps
29 * drm_gem_shmem_vmap()). These helpers perform the necessary type conversion.
32 static const struct drm_gem_object_funcs drm_gem_shmem_funcs
= {
33 .free
= drm_gem_shmem_object_free
,
34 .print_info
= drm_gem_shmem_object_print_info
,
35 .pin
= drm_gem_shmem_object_pin
,
36 .unpin
= drm_gem_shmem_object_unpin
,
37 .get_sg_table
= drm_gem_shmem_object_get_sg_table
,
38 .vmap
= drm_gem_shmem_object_vmap
,
39 .vunmap
= drm_gem_shmem_object_vunmap
,
40 .mmap
= drm_gem_shmem_object_mmap
,
43 static struct drm_gem_shmem_object
*
44 __drm_gem_shmem_create(struct drm_device
*dev
, size_t size
, bool private)
46 struct drm_gem_shmem_object
*shmem
;
47 struct drm_gem_object
*obj
;
50 size
= PAGE_ALIGN(size
);
52 if (dev
->driver
->gem_create_object
)
53 obj
= dev
->driver
->gem_create_object(dev
, size
);
55 obj
= kzalloc(sizeof(*shmem
), GFP_KERNEL
);
57 return ERR_PTR(-ENOMEM
);
59 shmem
= to_drm_gem_shmem_obj(obj
);
62 obj
->funcs
= &drm_gem_shmem_funcs
;
65 drm_gem_private_object_init(dev
, obj
, size
);
66 shmem
->map_wc
= false; /* dma-buf mappings use always writecombine */
68 ret
= drm_gem_object_init(dev
, obj
, size
);
73 ret
= drm_gem_create_mmap_offset(obj
);
77 mutex_init(&shmem
->pages_lock
);
78 mutex_init(&shmem
->vmap_lock
);
79 INIT_LIST_HEAD(&shmem
->madv_list
);
83 * Our buffers are kept pinned, so allocating them
84 * from the MOVABLE zone is a really bad idea, and
85 * conflicts with CMA. See comments above new_inode()
86 * why this is required _and_ expected if you're
87 * going to pin these pages.
89 mapping_set_gfp_mask(obj
->filp
->f_mapping
, GFP_HIGHUSER
|
90 __GFP_RETRY_MAYFAIL
| __GFP_NOWARN
);
96 drm_gem_object_release(obj
);
103 * drm_gem_shmem_create - Allocate an object with the given size
105 * @size: Size of the object to allocate
107 * This function creates a shmem GEM object.
110 * A struct drm_gem_shmem_object * on success or an ERR_PTR()-encoded negative
111 * error code on failure.
113 struct drm_gem_shmem_object
*drm_gem_shmem_create(struct drm_device
*dev
, size_t size
)
115 return __drm_gem_shmem_create(dev
, size
, false);
117 EXPORT_SYMBOL_GPL(drm_gem_shmem_create
);
120 * drm_gem_shmem_free - Free resources associated with a shmem GEM object
121 * @shmem: shmem GEM object to free
123 * This function cleans up the GEM object state and frees the memory used to
124 * store the object itself.
126 void drm_gem_shmem_free(struct drm_gem_shmem_object
*shmem
)
128 struct drm_gem_object
*obj
= &shmem
->base
;
130 WARN_ON(shmem
->vmap_use_count
);
132 if (obj
->import_attach
) {
133 drm_prime_gem_destroy(obj
, shmem
->sgt
);
136 dma_unmap_sgtable(obj
->dev
->dev
, shmem
->sgt
,
137 DMA_BIDIRECTIONAL
, 0);
138 sg_free_table(shmem
->sgt
);
142 drm_gem_shmem_put_pages(shmem
);
145 WARN_ON(shmem
->pages_use_count
);
147 drm_gem_object_release(obj
);
148 mutex_destroy(&shmem
->pages_lock
);
149 mutex_destroy(&shmem
->vmap_lock
);
152 EXPORT_SYMBOL_GPL(drm_gem_shmem_free
);
154 static int drm_gem_shmem_get_pages_locked(struct drm_gem_shmem_object
*shmem
)
156 struct drm_gem_object
*obj
= &shmem
->base
;
159 if (shmem
->pages_use_count
++ > 0)
162 pages
= drm_gem_get_pages(obj
);
164 DRM_DEBUG_KMS("Failed to get pages (%ld)\n", PTR_ERR(pages
));
165 shmem
->pages_use_count
= 0;
166 return PTR_ERR(pages
);
169 shmem
->pages
= pages
;
175 * drm_gem_shmem_get_pages - Allocate backing pages for a shmem GEM object
176 * @shmem: shmem GEM object
178 * This function makes sure that backing pages exists for the shmem GEM object
179 * and increases the use count.
182 * 0 on success or a negative error code on failure.
184 int drm_gem_shmem_get_pages(struct drm_gem_shmem_object
*shmem
)
188 WARN_ON(shmem
->base
.import_attach
);
190 ret
= mutex_lock_interruptible(&shmem
->pages_lock
);
193 ret
= drm_gem_shmem_get_pages_locked(shmem
);
194 mutex_unlock(&shmem
->pages_lock
);
198 EXPORT_SYMBOL(drm_gem_shmem_get_pages
);
200 static void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object
*shmem
)
202 struct drm_gem_object
*obj
= &shmem
->base
;
204 if (WARN_ON_ONCE(!shmem
->pages_use_count
))
207 if (--shmem
->pages_use_count
> 0)
210 drm_gem_put_pages(obj
, shmem
->pages
,
211 shmem
->pages_mark_dirty_on_put
,
212 shmem
->pages_mark_accessed_on_put
);
217 * drm_gem_shmem_put_pages - Decrease use count on the backing pages for a shmem GEM object
218 * @shmem: shmem GEM object
220 * This function decreases the use count and puts the backing pages when use drops to zero.
222 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object
*shmem
)
224 mutex_lock(&shmem
->pages_lock
);
225 drm_gem_shmem_put_pages_locked(shmem
);
226 mutex_unlock(&shmem
->pages_lock
);
228 EXPORT_SYMBOL(drm_gem_shmem_put_pages
);
231 * drm_gem_shmem_pin - Pin backing pages for a shmem GEM object
232 * @shmem: shmem GEM object
234 * This function makes sure the backing pages are pinned in memory while the
235 * buffer is exported.
238 * 0 on success or a negative error code on failure.
240 int drm_gem_shmem_pin(struct drm_gem_shmem_object
*shmem
)
242 WARN_ON(shmem
->base
.import_attach
);
244 return drm_gem_shmem_get_pages(shmem
);
246 EXPORT_SYMBOL(drm_gem_shmem_pin
);
249 * drm_gem_shmem_unpin - Unpin backing pages for a shmem GEM object
250 * @shmem: shmem GEM object
252 * This function removes the requirement that the backing pages are pinned in
255 void drm_gem_shmem_unpin(struct drm_gem_shmem_object
*shmem
)
257 WARN_ON(shmem
->base
.import_attach
);
259 drm_gem_shmem_put_pages(shmem
);
261 EXPORT_SYMBOL(drm_gem_shmem_unpin
);
263 static int drm_gem_shmem_vmap_locked(struct drm_gem_shmem_object
*shmem
, struct dma_buf_map
*map
)
265 struct drm_gem_object
*obj
= &shmem
->base
;
268 if (shmem
->vmap_use_count
++ > 0) {
269 dma_buf_map_set_vaddr(map
, shmem
->vaddr
);
273 if (obj
->import_attach
) {
274 ret
= dma_buf_vmap(obj
->import_attach
->dmabuf
, map
);
276 if (WARN_ON(map
->is_iomem
)) {
277 dma_buf_vunmap(obj
->import_attach
->dmabuf
, map
);
281 shmem
->vaddr
= map
->vaddr
;
284 pgprot_t prot
= PAGE_KERNEL
;
286 ret
= drm_gem_shmem_get_pages(shmem
);
291 prot
= pgprot_writecombine(prot
);
292 shmem
->vaddr
= vmap(shmem
->pages
, obj
->size
>> PAGE_SHIFT
,
297 dma_buf_map_set_vaddr(map
, shmem
->vaddr
);
301 DRM_DEBUG_KMS("Failed to vmap pages, error %d\n", ret
);
308 if (!obj
->import_attach
)
309 drm_gem_shmem_put_pages(shmem
);
311 shmem
->vmap_use_count
= 0;
317 * drm_gem_shmem_vmap - Create a virtual mapping for a shmem GEM object
318 * @shmem: shmem GEM object
319 * @map: Returns the kernel virtual address of the SHMEM GEM object's backing
322 * This function makes sure that a contiguous kernel virtual address mapping
323 * exists for the buffer backing the shmem GEM object. It hides the differences
324 * between dma-buf imported and natively allocated objects.
326 * Acquired mappings should be cleaned up by calling drm_gem_shmem_vunmap().
329 * 0 on success or a negative error code on failure.
331 int drm_gem_shmem_vmap(struct drm_gem_shmem_object
*shmem
, struct dma_buf_map
*map
)
335 ret
= mutex_lock_interruptible(&shmem
->vmap_lock
);
338 ret
= drm_gem_shmem_vmap_locked(shmem
, map
);
339 mutex_unlock(&shmem
->vmap_lock
);
343 EXPORT_SYMBOL(drm_gem_shmem_vmap
);
345 static void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object
*shmem
,
346 struct dma_buf_map
*map
)
348 struct drm_gem_object
*obj
= &shmem
->base
;
350 if (WARN_ON_ONCE(!shmem
->vmap_use_count
))
353 if (--shmem
->vmap_use_count
> 0)
356 if (obj
->import_attach
) {
357 dma_buf_vunmap(obj
->import_attach
->dmabuf
, map
);
359 vunmap(shmem
->vaddr
);
360 drm_gem_shmem_put_pages(shmem
);
367 * drm_gem_shmem_vunmap - Unmap a virtual mapping for a shmem GEM object
368 * @shmem: shmem GEM object
369 * @map: Kernel virtual address where the SHMEM GEM object was mapped
371 * This function cleans up a kernel virtual address mapping acquired by
372 * drm_gem_shmem_vmap(). The mapping is only removed when the use count drops to
375 * This function hides the differences between dma-buf imported and natively
378 void drm_gem_shmem_vunmap(struct drm_gem_shmem_object
*shmem
, struct dma_buf_map
*map
)
380 mutex_lock(&shmem
->vmap_lock
);
381 drm_gem_shmem_vunmap_locked(shmem
, map
);
382 mutex_unlock(&shmem
->vmap_lock
);
384 EXPORT_SYMBOL(drm_gem_shmem_vunmap
);
386 static struct drm_gem_shmem_object
*
387 drm_gem_shmem_create_with_handle(struct drm_file
*file_priv
,
388 struct drm_device
*dev
, size_t size
,
391 struct drm_gem_shmem_object
*shmem
;
394 shmem
= drm_gem_shmem_create(dev
, size
);
399 * Allocate an id of idr table where the obj is registered
400 * and handle has the id what user can see.
402 ret
= drm_gem_handle_create(file_priv
, &shmem
->base
, handle
);
403 /* drop reference from allocate - handle holds it now. */
404 drm_gem_object_put(&shmem
->base
);
411 /* Update madvise status, returns true if not purged, else
414 int drm_gem_shmem_madvise(struct drm_gem_shmem_object
*shmem
, int madv
)
416 mutex_lock(&shmem
->pages_lock
);
418 if (shmem
->madv
>= 0)
423 mutex_unlock(&shmem
->pages_lock
);
427 EXPORT_SYMBOL(drm_gem_shmem_madvise
);
429 void drm_gem_shmem_purge_locked(struct drm_gem_shmem_object
*shmem
)
431 struct drm_gem_object
*obj
= &shmem
->base
;
432 struct drm_device
*dev
= obj
->dev
;
434 WARN_ON(!drm_gem_shmem_is_purgeable(shmem
));
436 dma_unmap_sgtable(dev
->dev
, shmem
->sgt
, DMA_BIDIRECTIONAL
, 0);
437 sg_free_table(shmem
->sgt
);
441 drm_gem_shmem_put_pages_locked(shmem
);
445 drm_vma_node_unmap(&obj
->vma_node
, dev
->anon_inode
->i_mapping
);
446 drm_gem_free_mmap_offset(obj
);
448 /* Our goal here is to return as much of the memory as
449 * is possible back to the system as we are called from OOM.
450 * To do this we must instruct the shmfs to drop all of its
451 * backing pages, *now*.
453 shmem_truncate_range(file_inode(obj
->filp
), 0, (loff_t
)-1);
455 invalidate_mapping_pages(file_inode(obj
->filp
)->i_mapping
, 0, (loff_t
)-1);
457 EXPORT_SYMBOL(drm_gem_shmem_purge_locked
);
459 bool drm_gem_shmem_purge(struct drm_gem_shmem_object
*shmem
)
461 if (!mutex_trylock(&shmem
->pages_lock
))
463 drm_gem_shmem_purge_locked(shmem
);
464 mutex_unlock(&shmem
->pages_lock
);
468 EXPORT_SYMBOL(drm_gem_shmem_purge
);
471 * drm_gem_shmem_dumb_create - Create a dumb shmem buffer object
472 * @file: DRM file structure to create the dumb buffer for
476 * This function computes the pitch of the dumb buffer and rounds it up to an
477 * integer number of bytes per pixel. Drivers for hardware that doesn't have
478 * any additional restrictions on the pitch can directly use this function as
479 * their &drm_driver.dumb_create callback.
481 * For hardware with additional restrictions, drivers can adjust the fields
482 * set up by userspace before calling into this function.
485 * 0 on success or a negative error code on failure.
487 int drm_gem_shmem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
488 struct drm_mode_create_dumb
*args
)
490 u32 min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
491 struct drm_gem_shmem_object
*shmem
;
493 if (!args
->pitch
|| !args
->size
) {
494 args
->pitch
= min_pitch
;
495 args
->size
= PAGE_ALIGN(args
->pitch
* args
->height
);
497 /* ensure sane minimum values */
498 if (args
->pitch
< min_pitch
)
499 args
->pitch
= min_pitch
;
500 if (args
->size
< args
->pitch
* args
->height
)
501 args
->size
= PAGE_ALIGN(args
->pitch
* args
->height
);
504 shmem
= drm_gem_shmem_create_with_handle(file
, dev
, args
->size
, &args
->handle
);
506 return PTR_ERR_OR_ZERO(shmem
);
508 EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create
);
510 static vm_fault_t
drm_gem_shmem_fault(struct vm_fault
*vmf
)
512 struct vm_area_struct
*vma
= vmf
->vma
;
513 struct drm_gem_object
*obj
= vma
->vm_private_data
;
514 struct drm_gem_shmem_object
*shmem
= to_drm_gem_shmem_obj(obj
);
515 loff_t num_pages
= obj
->size
>> PAGE_SHIFT
;
520 /* We don't use vmf->pgoff since that has the fake offset */
521 page_offset
= (vmf
->address
- vma
->vm_start
) >> PAGE_SHIFT
;
523 mutex_lock(&shmem
->pages_lock
);
525 if (page_offset
>= num_pages
||
526 WARN_ON_ONCE(!shmem
->pages
) ||
528 ret
= VM_FAULT_SIGBUS
;
530 page
= shmem
->pages
[page_offset
];
532 ret
= vmf_insert_page(vma
, vmf
->address
, page
);
535 mutex_unlock(&shmem
->pages_lock
);
540 static void drm_gem_shmem_vm_open(struct vm_area_struct
*vma
)
542 struct drm_gem_object
*obj
= vma
->vm_private_data
;
543 struct drm_gem_shmem_object
*shmem
= to_drm_gem_shmem_obj(obj
);
545 WARN_ON(shmem
->base
.import_attach
);
547 mutex_lock(&shmem
->pages_lock
);
550 * We should have already pinned the pages when the buffer was first
551 * mmap'd, vm_open() just grabs an additional reference for the new
552 * mm the vma is getting copied into (ie. on fork()).
554 if (!WARN_ON_ONCE(!shmem
->pages_use_count
))
555 shmem
->pages_use_count
++;
557 mutex_unlock(&shmem
->pages_lock
);
559 drm_gem_vm_open(vma
);
562 static void drm_gem_shmem_vm_close(struct vm_area_struct
*vma
)
564 struct drm_gem_object
*obj
= vma
->vm_private_data
;
565 struct drm_gem_shmem_object
*shmem
= to_drm_gem_shmem_obj(obj
);
567 drm_gem_shmem_put_pages(shmem
);
568 drm_gem_vm_close(vma
);
571 static const struct vm_operations_struct drm_gem_shmem_vm_ops
= {
572 .fault
= drm_gem_shmem_fault
,
573 .open
= drm_gem_shmem_vm_open
,
574 .close
= drm_gem_shmem_vm_close
,
578 * drm_gem_shmem_mmap - Memory-map a shmem GEM object
579 * @shmem: shmem GEM object
580 * @vma: VMA for the area to be mapped
582 * This function implements an augmented version of the GEM DRM file mmap
583 * operation for shmem objects.
586 * 0 on success or a negative error code on failure.
588 int drm_gem_shmem_mmap(struct drm_gem_shmem_object
*shmem
, struct vm_area_struct
*vma
)
590 struct drm_gem_object
*obj
= &shmem
->base
;
593 if (obj
->import_attach
) {
594 /* Drop the reference drm_gem_mmap_obj() acquired.*/
595 drm_gem_object_put(obj
);
596 vma
->vm_private_data
= NULL
;
598 return dma_buf_mmap(obj
->dma_buf
, vma
, 0);
601 ret
= drm_gem_shmem_get_pages(shmem
);
605 vma
->vm_flags
|= VM_MIXEDMAP
| VM_DONTEXPAND
;
606 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
608 vma
->vm_page_prot
= pgprot_writecombine(vma
->vm_page_prot
);
609 vma
->vm_ops
= &drm_gem_shmem_vm_ops
;
613 EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap
);
616 * drm_gem_shmem_print_info() - Print &drm_gem_shmem_object info for debugfs
617 * @shmem: shmem GEM object
619 * @indent: Tab indentation level
621 void drm_gem_shmem_print_info(const struct drm_gem_shmem_object
*shmem
,
622 struct drm_printer
*p
, unsigned int indent
)
624 drm_printf_indent(p
, indent
, "pages_use_count=%u\n", shmem
->pages_use_count
);
625 drm_printf_indent(p
, indent
, "vmap_use_count=%u\n", shmem
->vmap_use_count
);
626 drm_printf_indent(p
, indent
, "vaddr=%p\n", shmem
->vaddr
);
628 EXPORT_SYMBOL(drm_gem_shmem_print_info
);
631 * drm_gem_shmem_get_sg_table - Provide a scatter/gather table of pinned
632 * pages for a shmem GEM object
633 * @shmem: shmem GEM object
635 * This function exports a scatter/gather table suitable for PRIME usage by
636 * calling the standard DMA mapping API.
638 * Drivers who need to acquire an scatter/gather table for objects need to call
639 * drm_gem_shmem_get_pages_sgt() instead.
642 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
644 struct sg_table
*drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object
*shmem
)
646 struct drm_gem_object
*obj
= &shmem
->base
;
648 WARN_ON(shmem
->base
.import_attach
);
650 return drm_prime_pages_to_sg(obj
->dev
, shmem
->pages
, obj
->size
>> PAGE_SHIFT
);
652 EXPORT_SYMBOL_GPL(drm_gem_shmem_get_sg_table
);
655 * drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a
656 * scatter/gather table for a shmem GEM object.
657 * @shmem: shmem GEM object
659 * This function returns a scatter/gather table suitable for driver usage. If
660 * the sg table doesn't exist, the pages are pinned, dma-mapped, and a sg
663 * This is the main function for drivers to get at backing storage, and it hides
664 * and difference between dma-buf imported and natively allocated objects.
665 * drm_gem_shmem_get_sg_table() should not be directly called by drivers.
668 * A pointer to the scatter/gather table of pinned pages or errno on failure.
670 struct sg_table
*drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object
*shmem
)
672 struct drm_gem_object
*obj
= &shmem
->base
;
674 struct sg_table
*sgt
;
679 WARN_ON(obj
->import_attach
);
681 ret
= drm_gem_shmem_get_pages(shmem
);
685 sgt
= drm_gem_shmem_get_sg_table(shmem
);
690 /* Map the pages for use by the h/w. */
691 ret
= dma_map_sgtable(obj
->dev
->dev
, sgt
, DMA_BIDIRECTIONAL
, 0);
703 drm_gem_shmem_put_pages(shmem
);
706 EXPORT_SYMBOL_GPL(drm_gem_shmem_get_pages_sgt
);
709 * drm_gem_shmem_prime_import_sg_table - Produce a shmem GEM object from
710 * another driver's scatter/gather table of pinned pages
711 * @dev: Device to import into
712 * @attach: DMA-BUF attachment
713 * @sgt: Scatter/gather table of pinned pages
715 * This function imports a scatter/gather table exported via DMA-BUF by
716 * another driver. Drivers that use the shmem helpers should set this as their
717 * &drm_driver.gem_prime_import_sg_table callback.
720 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
721 * error code on failure.
723 struct drm_gem_object
*
724 drm_gem_shmem_prime_import_sg_table(struct drm_device
*dev
,
725 struct dma_buf_attachment
*attach
,
726 struct sg_table
*sgt
)
728 size_t size
= PAGE_ALIGN(attach
->dmabuf
->size
);
729 struct drm_gem_shmem_object
*shmem
;
731 shmem
= __drm_gem_shmem_create(dev
, size
, true);
733 return ERR_CAST(shmem
);
737 DRM_DEBUG_PRIME("size = %zu\n", size
);
741 EXPORT_SYMBOL_GPL(drm_gem_shmem_prime_import_sg_table
);