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/module.h>
9 #include <linux/mutex.h>
10 #include <linux/shmem_fs.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
13 #include <linux/module.h>
16 #include <asm/set_memory.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_gem_shmem_helper.h>
23 #include <drm/drm_prime.h>
24 #include <drm/drm_print.h>
26 MODULE_IMPORT_NS(DMA_BUF);
31 * This library provides helpers for GEM objects backed by shmem buffers
32 * allocated using anonymous pageable memory.
34 * Functions that operate on the GEM object receive struct &drm_gem_shmem_object.
35 * For GEM callback helpers in struct &drm_gem_object functions, see likewise
36 * named functions with an _object_ infix (e.g., drm_gem_shmem_object_vmap() wraps
37 * drm_gem_shmem_vmap()). These helpers perform the necessary type conversion.
40 static const struct drm_gem_object_funcs drm_gem_shmem_funcs = {
41 .free = drm_gem_shmem_object_free,
42 .print_info = drm_gem_shmem_object_print_info,
43 .pin = drm_gem_shmem_object_pin,
44 .unpin = drm_gem_shmem_object_unpin,
45 .get_sg_table = drm_gem_shmem_object_get_sg_table,
46 .vmap = drm_gem_shmem_object_vmap,
47 .vunmap = drm_gem_shmem_object_vunmap,
48 .mmap = drm_gem_shmem_object_mmap,
49 .vm_ops = &drm_gem_shmem_vm_ops,
52 static struct drm_gem_shmem_object *
53 __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
55 struct drm_gem_shmem_object *shmem;
56 struct drm_gem_object *obj;
59 size = PAGE_ALIGN(size);
61 if (dev->driver->gem_create_object) {
62 obj = dev->driver->gem_create_object(dev, size);
65 shmem = to_drm_gem_shmem_obj(obj);
67 shmem = kzalloc(sizeof(*shmem), GFP_KERNEL);
69 return ERR_PTR(-ENOMEM);
74 obj->funcs = &drm_gem_shmem_funcs;
77 drm_gem_private_object_init(dev, obj, size);
78 shmem->map_wc = false; /* dma-buf mappings use always writecombine */
80 ret = drm_gem_object_init(dev, obj, size);
83 drm_gem_private_object_fini(obj);
87 ret = drm_gem_create_mmap_offset(obj);
91 mutex_init(&shmem->pages_lock);
92 mutex_init(&shmem->vmap_lock);
93 INIT_LIST_HEAD(&shmem->madv_list);
97 * Our buffers are kept pinned, so allocating them
98 * from the MOVABLE zone is a really bad idea, and
99 * conflicts with CMA. See comments above new_inode()
100 * why this is required _and_ expected if you're
101 * going to pin these pages.
103 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER |
104 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
110 drm_gem_object_release(obj);
117 * drm_gem_shmem_create - Allocate an object with the given size
119 * @size: Size of the object to allocate
121 * This function creates a shmem GEM object.
124 * A struct drm_gem_shmem_object * on success or an ERR_PTR()-encoded negative
125 * error code on failure.
127 struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size)
129 return __drm_gem_shmem_create(dev, size, false);
131 EXPORT_SYMBOL_GPL(drm_gem_shmem_create);
134 * drm_gem_shmem_free - Free resources associated with a shmem GEM object
135 * @shmem: shmem GEM object to free
137 * This function cleans up the GEM object state and frees the memory used to
138 * store the object itself.
140 void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem)
142 struct drm_gem_object *obj = &shmem->base;
144 drm_WARN_ON(obj->dev, shmem->vmap_use_count);
146 if (obj->import_attach) {
147 drm_prime_gem_destroy(obj, shmem->sgt);
150 dma_unmap_sgtable(obj->dev->dev, shmem->sgt,
151 DMA_BIDIRECTIONAL, 0);
152 sg_free_table(shmem->sgt);
156 drm_gem_shmem_put_pages(shmem);
159 drm_WARN_ON(obj->dev, shmem->pages_use_count);
161 drm_gem_object_release(obj);
162 mutex_destroy(&shmem->pages_lock);
163 mutex_destroy(&shmem->vmap_lock);
166 EXPORT_SYMBOL_GPL(drm_gem_shmem_free);
168 static int drm_gem_shmem_get_pages_locked(struct drm_gem_shmem_object *shmem)
170 struct drm_gem_object *obj = &shmem->base;
173 if (shmem->pages_use_count++ > 0)
176 pages = drm_gem_get_pages(obj);
178 drm_dbg_kms(obj->dev, "Failed to get pages (%ld)\n",
180 shmem->pages_use_count = 0;
181 return PTR_ERR(pages);
185 * TODO: Allocating WC pages which are correctly flushed is only
186 * supported on x86. Ideal solution would be a GFP_WC flag, which also
187 * ttm_pool.c could use.
191 set_pages_array_wc(pages, obj->size >> PAGE_SHIFT);
194 shmem->pages = pages;
200 * drm_gem_shmem_get_pages - Allocate backing pages for a shmem GEM object
201 * @shmem: shmem GEM object
203 * This function makes sure that backing pages exists for the shmem GEM object
204 * and increases the use count.
207 * 0 on success or a negative error code on failure.
209 int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem)
211 struct drm_gem_object *obj = &shmem->base;
214 drm_WARN_ON(obj->dev, obj->import_attach);
216 ret = mutex_lock_interruptible(&shmem->pages_lock);
219 ret = drm_gem_shmem_get_pages_locked(shmem);
220 mutex_unlock(&shmem->pages_lock);
224 EXPORT_SYMBOL(drm_gem_shmem_get_pages);
226 static void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object *shmem)
228 struct drm_gem_object *obj = &shmem->base;
230 if (drm_WARN_ON_ONCE(obj->dev, !shmem->pages_use_count))
233 if (--shmem->pages_use_count > 0)
238 set_pages_array_wb(shmem->pages, obj->size >> PAGE_SHIFT);
241 drm_gem_put_pages(obj, shmem->pages,
242 shmem->pages_mark_dirty_on_put,
243 shmem->pages_mark_accessed_on_put);
248 * drm_gem_shmem_put_pages - Decrease use count on the backing pages for a shmem GEM object
249 * @shmem: shmem GEM object
251 * This function decreases the use count and puts the backing pages when use drops to zero.
253 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem)
255 mutex_lock(&shmem->pages_lock);
256 drm_gem_shmem_put_pages_locked(shmem);
257 mutex_unlock(&shmem->pages_lock);
259 EXPORT_SYMBOL(drm_gem_shmem_put_pages);
262 * drm_gem_shmem_pin - Pin backing pages for a shmem GEM object
263 * @shmem: shmem GEM object
265 * This function makes sure the backing pages are pinned in memory while the
266 * buffer is exported.
269 * 0 on success or a negative error code on failure.
271 int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem)
273 struct drm_gem_object *obj = &shmem->base;
275 drm_WARN_ON(obj->dev, obj->import_attach);
277 return drm_gem_shmem_get_pages(shmem);
279 EXPORT_SYMBOL(drm_gem_shmem_pin);
282 * drm_gem_shmem_unpin - Unpin backing pages for a shmem GEM object
283 * @shmem: shmem GEM object
285 * This function removes the requirement that the backing pages are pinned in
288 void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem)
290 struct drm_gem_object *obj = &shmem->base;
292 drm_WARN_ON(obj->dev, obj->import_attach);
294 drm_gem_shmem_put_pages(shmem);
296 EXPORT_SYMBOL(drm_gem_shmem_unpin);
298 static int drm_gem_shmem_vmap_locked(struct drm_gem_shmem_object *shmem,
299 struct iosys_map *map)
301 struct drm_gem_object *obj = &shmem->base;
304 if (shmem->vmap_use_count++ > 0) {
305 iosys_map_set_vaddr(map, shmem->vaddr);
309 if (obj->import_attach) {
310 ret = dma_buf_vmap(obj->import_attach->dmabuf, map);
312 if (drm_WARN_ON(obj->dev, map->is_iomem)) {
313 dma_buf_vunmap(obj->import_attach->dmabuf, map);
317 shmem->vaddr = map->vaddr;
320 pgprot_t prot = PAGE_KERNEL;
322 ret = drm_gem_shmem_get_pages(shmem);
327 prot = pgprot_writecombine(prot);
328 shmem->vaddr = vmap(shmem->pages, obj->size >> PAGE_SHIFT,
333 iosys_map_set_vaddr(map, shmem->vaddr);
337 drm_dbg_kms(obj->dev, "Failed to vmap pages, error %d\n", ret);
344 if (!obj->import_attach)
345 drm_gem_shmem_put_pages(shmem);
347 shmem->vmap_use_count = 0;
353 * drm_gem_shmem_vmap - Create a virtual mapping for a shmem GEM object
354 * @shmem: shmem GEM object
355 * @map: Returns the kernel virtual address of the SHMEM GEM object's backing
358 * This function makes sure that a contiguous kernel virtual address mapping
359 * exists for the buffer backing the shmem GEM object. It hides the differences
360 * between dma-buf imported and natively allocated objects.
362 * Acquired mappings should be cleaned up by calling drm_gem_shmem_vunmap().
365 * 0 on success or a negative error code on failure.
367 int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem,
368 struct iosys_map *map)
372 ret = mutex_lock_interruptible(&shmem->vmap_lock);
375 ret = drm_gem_shmem_vmap_locked(shmem, map);
376 mutex_unlock(&shmem->vmap_lock);
380 EXPORT_SYMBOL(drm_gem_shmem_vmap);
382 static void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object *shmem,
383 struct iosys_map *map)
385 struct drm_gem_object *obj = &shmem->base;
387 if (drm_WARN_ON_ONCE(obj->dev, !shmem->vmap_use_count))
390 if (--shmem->vmap_use_count > 0)
393 if (obj->import_attach) {
394 dma_buf_vunmap(obj->import_attach->dmabuf, map);
396 vunmap(shmem->vaddr);
397 drm_gem_shmem_put_pages(shmem);
404 * drm_gem_shmem_vunmap - Unmap a virtual mapping for a shmem GEM object
405 * @shmem: shmem GEM object
406 * @map: Kernel virtual address where the SHMEM GEM object was mapped
408 * This function cleans up a kernel virtual address mapping acquired by
409 * drm_gem_shmem_vmap(). The mapping is only removed when the use count drops to
412 * This function hides the differences between dma-buf imported and natively
415 void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem,
416 struct iosys_map *map)
418 mutex_lock(&shmem->vmap_lock);
419 drm_gem_shmem_vunmap_locked(shmem, map);
420 mutex_unlock(&shmem->vmap_lock);
422 EXPORT_SYMBOL(drm_gem_shmem_vunmap);
425 drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
426 struct drm_device *dev, size_t size,
429 struct drm_gem_shmem_object *shmem;
432 shmem = drm_gem_shmem_create(dev, size);
434 return PTR_ERR(shmem);
437 * Allocate an id of idr table where the obj is registered
438 * and handle has the id what user can see.
440 ret = drm_gem_handle_create(file_priv, &shmem->base, handle);
441 /* drop reference from allocate - handle holds it now. */
442 drm_gem_object_put(&shmem->base);
447 /* Update madvise status, returns true if not purged, else
450 int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv)
452 mutex_lock(&shmem->pages_lock);
454 if (shmem->madv >= 0)
459 mutex_unlock(&shmem->pages_lock);
463 EXPORT_SYMBOL(drm_gem_shmem_madvise);
465 void drm_gem_shmem_purge_locked(struct drm_gem_shmem_object *shmem)
467 struct drm_gem_object *obj = &shmem->base;
468 struct drm_device *dev = obj->dev;
470 drm_WARN_ON(obj->dev, !drm_gem_shmem_is_purgeable(shmem));
472 dma_unmap_sgtable(dev->dev, shmem->sgt, DMA_BIDIRECTIONAL, 0);
473 sg_free_table(shmem->sgt);
477 drm_gem_shmem_put_pages_locked(shmem);
481 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
482 drm_gem_free_mmap_offset(obj);
484 /* Our goal here is to return as much of the memory as
485 * is possible back to the system as we are called from OOM.
486 * To do this we must instruct the shmfs to drop all of its
487 * backing pages, *now*.
489 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
491 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping, 0, (loff_t)-1);
493 EXPORT_SYMBOL(drm_gem_shmem_purge_locked);
495 bool drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem)
497 if (!mutex_trylock(&shmem->pages_lock))
499 drm_gem_shmem_purge_locked(shmem);
500 mutex_unlock(&shmem->pages_lock);
504 EXPORT_SYMBOL(drm_gem_shmem_purge);
507 * drm_gem_shmem_dumb_create - Create a dumb shmem buffer object
508 * @file: DRM file structure to create the dumb buffer for
512 * This function computes the pitch of the dumb buffer and rounds it up to an
513 * integer number of bytes per pixel. Drivers for hardware that doesn't have
514 * any additional restrictions on the pitch can directly use this function as
515 * their &drm_driver.dumb_create callback.
517 * For hardware with additional restrictions, drivers can adjust the fields
518 * set up by userspace before calling into this function.
521 * 0 on success or a negative error code on failure.
523 int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
524 struct drm_mode_create_dumb *args)
526 u32 min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
528 if (!args->pitch || !args->size) {
529 args->pitch = min_pitch;
530 args->size = PAGE_ALIGN(args->pitch * args->height);
532 /* ensure sane minimum values */
533 if (args->pitch < min_pitch)
534 args->pitch = min_pitch;
535 if (args->size < args->pitch * args->height)
536 args->size = PAGE_ALIGN(args->pitch * args->height);
539 return drm_gem_shmem_create_with_handle(file, dev, args->size, &args->handle);
541 EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
543 static vm_fault_t drm_gem_shmem_fault(struct vm_fault *vmf)
545 struct vm_area_struct *vma = vmf->vma;
546 struct drm_gem_object *obj = vma->vm_private_data;
547 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
548 loff_t num_pages = obj->size >> PAGE_SHIFT;
553 /* We don't use vmf->pgoff since that has the fake offset */
554 page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
556 mutex_lock(&shmem->pages_lock);
558 if (page_offset >= num_pages ||
559 drm_WARN_ON_ONCE(obj->dev, !shmem->pages) ||
561 ret = VM_FAULT_SIGBUS;
563 page = shmem->pages[page_offset];
565 ret = vmf_insert_pfn(vma, vmf->address, page_to_pfn(page));
568 mutex_unlock(&shmem->pages_lock);
573 static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
575 struct drm_gem_object *obj = vma->vm_private_data;
576 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
578 drm_WARN_ON(obj->dev, obj->import_attach);
580 mutex_lock(&shmem->pages_lock);
583 * We should have already pinned the pages when the buffer was first
584 * mmap'd, vm_open() just grabs an additional reference for the new
585 * mm the vma is getting copied into (ie. on fork()).
587 if (!drm_WARN_ON_ONCE(obj->dev, !shmem->pages_use_count))
588 shmem->pages_use_count++;
590 mutex_unlock(&shmem->pages_lock);
592 drm_gem_vm_open(vma);
595 static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
597 struct drm_gem_object *obj = vma->vm_private_data;
598 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
600 drm_gem_shmem_put_pages(shmem);
601 drm_gem_vm_close(vma);
604 const struct vm_operations_struct drm_gem_shmem_vm_ops = {
605 .fault = drm_gem_shmem_fault,
606 .open = drm_gem_shmem_vm_open,
607 .close = drm_gem_shmem_vm_close,
609 EXPORT_SYMBOL_GPL(drm_gem_shmem_vm_ops);
612 * drm_gem_shmem_mmap - Memory-map a shmem GEM object
613 * @shmem: shmem GEM object
614 * @vma: VMA for the area to be mapped
616 * This function implements an augmented version of the GEM DRM file mmap
617 * operation for shmem objects.
620 * 0 on success or a negative error code on failure.
622 int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma)
624 struct drm_gem_object *obj = &shmem->base;
627 if (obj->import_attach) {
628 /* Drop the reference drm_gem_mmap_obj() acquired.*/
629 drm_gem_object_put(obj);
630 vma->vm_private_data = NULL;
632 return dma_buf_mmap(obj->dma_buf, vma, 0);
635 ret = drm_gem_shmem_get_pages(shmem);
639 vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
640 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
642 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
646 EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap);
649 * drm_gem_shmem_print_info() - Print &drm_gem_shmem_object info for debugfs
650 * @shmem: shmem GEM object
652 * @indent: Tab indentation level
654 void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
655 struct drm_printer *p, unsigned int indent)
657 drm_printf_indent(p, indent, "pages_use_count=%u\n", shmem->pages_use_count);
658 drm_printf_indent(p, indent, "vmap_use_count=%u\n", shmem->vmap_use_count);
659 drm_printf_indent(p, indent, "vaddr=%p\n", shmem->vaddr);
661 EXPORT_SYMBOL(drm_gem_shmem_print_info);
664 * drm_gem_shmem_get_sg_table - Provide a scatter/gather table of pinned
665 * pages for a shmem GEM object
666 * @shmem: shmem GEM object
668 * This function exports a scatter/gather table suitable for PRIME usage by
669 * calling the standard DMA mapping API.
671 * Drivers who need to acquire an scatter/gather table for objects need to call
672 * drm_gem_shmem_get_pages_sgt() instead.
675 * A pointer to the scatter/gather table of pinned pages or error pointer on failure.
677 struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem)
679 struct drm_gem_object *obj = &shmem->base;
681 drm_WARN_ON(obj->dev, obj->import_attach);
683 return drm_prime_pages_to_sg(obj->dev, shmem->pages, obj->size >> PAGE_SHIFT);
685 EXPORT_SYMBOL_GPL(drm_gem_shmem_get_sg_table);
687 static struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_object *shmem)
689 struct drm_gem_object *obj = &shmem->base;
691 struct sg_table *sgt;
696 drm_WARN_ON(obj->dev, obj->import_attach);
698 ret = drm_gem_shmem_get_pages_locked(shmem);
702 sgt = drm_gem_shmem_get_sg_table(shmem);
707 /* Map the pages for use by the h/w. */
708 ret = dma_map_sgtable(obj->dev->dev, sgt, DMA_BIDIRECTIONAL, 0);
720 drm_gem_shmem_put_pages_locked(shmem);
725 * drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a
726 * scatter/gather table for a shmem GEM object.
727 * @shmem: shmem GEM object
729 * This function returns a scatter/gather table suitable for driver usage. If
730 * the sg table doesn't exist, the pages are pinned, dma-mapped, and a sg
733 * This is the main function for drivers to get at backing storage, and it hides
734 * and difference between dma-buf imported and natively allocated objects.
735 * drm_gem_shmem_get_sg_table() should not be directly called by drivers.
738 * A pointer to the scatter/gather table of pinned pages or errno on failure.
740 struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
743 struct sg_table *sgt;
745 ret = mutex_lock_interruptible(&shmem->pages_lock);
748 sgt = drm_gem_shmem_get_pages_sgt_locked(shmem);
749 mutex_unlock(&shmem->pages_lock);
753 EXPORT_SYMBOL(drm_gem_shmem_get_pages_sgt);
756 * drm_gem_shmem_prime_import_sg_table - Produce a shmem GEM object from
757 * another driver's scatter/gather table of pinned pages
758 * @dev: Device to import into
759 * @attach: DMA-BUF attachment
760 * @sgt: Scatter/gather table of pinned pages
762 * This function imports a scatter/gather table exported via DMA-BUF by
763 * another driver. Drivers that use the shmem helpers should set this as their
764 * &drm_driver.gem_prime_import_sg_table callback.
767 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
768 * error code on failure.
770 struct drm_gem_object *
771 drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
772 struct dma_buf_attachment *attach,
773 struct sg_table *sgt)
775 size_t size = PAGE_ALIGN(attach->dmabuf->size);
776 struct drm_gem_shmem_object *shmem;
778 shmem = __drm_gem_shmem_create(dev, size, true);
780 return ERR_CAST(shmem);
784 drm_dbg_prime(dev, "size = %zu\n", size);
788 EXPORT_SYMBOL_GPL(drm_gem_shmem_prime_import_sg_table);
790 MODULE_DESCRIPTION("DRM SHMEM memory-management helpers");
791 MODULE_IMPORT_NS(DMA_BUF);
792 MODULE_LICENSE("GPL v2");