Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2018 Noralf Trønnes
4 */
5
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>
14
15#ifdef CONFIG_X86
16#include <asm/set_memory.h>
17#endif
18
19#include <drm/drm.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>
25
26MODULE_IMPORT_NS(DMA_BUF);
27
28/**
29 * DOC: overview
30 *
31 * This library provides helpers for GEM objects backed by shmem buffers
32 * allocated using anonymous pageable memory.
33 *
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.
38 */
39
40static 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,
50};
51
52static struct drm_gem_shmem_object *
53__drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
54{
55 struct drm_gem_shmem_object *shmem;
56 struct drm_gem_object *obj;
57 int ret = 0;
58
59 size = PAGE_ALIGN(size);
60
61 if (dev->driver->gem_create_object) {
62 obj = dev->driver->gem_create_object(dev, size);
63 if (IS_ERR(obj))
64 return ERR_CAST(obj);
65 shmem = to_drm_gem_shmem_obj(obj);
66 } else {
67 shmem = kzalloc(sizeof(*shmem), GFP_KERNEL);
68 if (!shmem)
69 return ERR_PTR(-ENOMEM);
70 obj = &shmem->base;
71 }
72
73 if (!obj->funcs)
74 obj->funcs = &drm_gem_shmem_funcs;
75
76 if (private) {
77 drm_gem_private_object_init(dev, obj, size);
78 shmem->map_wc = false; /* dma-buf mappings use always writecombine */
79 } else {
80 ret = drm_gem_object_init(dev, obj, size);
81 }
82 if (ret)
83 goto err_free;
84
85 ret = drm_gem_create_mmap_offset(obj);
86 if (ret)
87 goto err_release;
88
89 mutex_init(&shmem->pages_lock);
90 mutex_init(&shmem->vmap_lock);
91 INIT_LIST_HEAD(&shmem->madv_list);
92
93 if (!private) {
94 /*
95 * Our buffers are kept pinned, so allocating them
96 * from the MOVABLE zone is a really bad idea, and
97 * conflicts with CMA. See comments above new_inode()
98 * why this is required _and_ expected if you're
99 * going to pin these pages.
100 */
101 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER |
102 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
103 }
104
105 return shmem;
106
107err_release:
108 drm_gem_object_release(obj);
109err_free:
110 kfree(obj);
111
112 return ERR_PTR(ret);
113}
114/**
115 * drm_gem_shmem_create - Allocate an object with the given size
116 * @dev: DRM device
117 * @size: Size of the object to allocate
118 *
119 * This function creates a shmem GEM object.
120 *
121 * Returns:
122 * A struct drm_gem_shmem_object * on success or an ERR_PTR()-encoded negative
123 * error code on failure.
124 */
125struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size)
126{
127 return __drm_gem_shmem_create(dev, size, false);
128}
129EXPORT_SYMBOL_GPL(drm_gem_shmem_create);
130
131/**
132 * drm_gem_shmem_free - Free resources associated with a shmem GEM object
133 * @shmem: shmem GEM object to free
134 *
135 * This function cleans up the GEM object state and frees the memory used to
136 * store the object itself.
137 */
138void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem)
139{
140 struct drm_gem_object *obj = &shmem->base;
141
142 WARN_ON(shmem->vmap_use_count);
143
144 if (obj->import_attach) {
145 drm_prime_gem_destroy(obj, shmem->sgt);
146 } else {
147 if (shmem->sgt) {
148 dma_unmap_sgtable(obj->dev->dev, shmem->sgt,
149 DMA_BIDIRECTIONAL, 0);
150 sg_free_table(shmem->sgt);
151 kfree(shmem->sgt);
152 }
153 if (shmem->pages)
154 drm_gem_shmem_put_pages(shmem);
155 }
156
157 WARN_ON(shmem->pages_use_count);
158
159 drm_gem_object_release(obj);
160 mutex_destroy(&shmem->pages_lock);
161 mutex_destroy(&shmem->vmap_lock);
162 kfree(shmem);
163}
164EXPORT_SYMBOL_GPL(drm_gem_shmem_free);
165
166static int drm_gem_shmem_get_pages_locked(struct drm_gem_shmem_object *shmem)
167{
168 struct drm_gem_object *obj = &shmem->base;
169 struct page **pages;
170
171 if (shmem->pages_use_count++ > 0)
172 return 0;
173
174 pages = drm_gem_get_pages(obj);
175 if (IS_ERR(pages)) {
176 DRM_DEBUG_KMS("Failed to get pages (%ld)\n", PTR_ERR(pages));
177 shmem->pages_use_count = 0;
178 return PTR_ERR(pages);
179 }
180
181 /*
182 * TODO: Allocating WC pages which are correctly flushed is only
183 * supported on x86. Ideal solution would be a GFP_WC flag, which also
184 * ttm_pool.c could use.
185 */
186#ifdef CONFIG_X86
187 if (shmem->map_wc)
188 set_pages_array_wc(pages, obj->size >> PAGE_SHIFT);
189#endif
190
191 shmem->pages = pages;
192
193 return 0;
194}
195
196/*
197 * drm_gem_shmem_get_pages - Allocate backing pages for a shmem GEM object
198 * @shmem: shmem GEM object
199 *
200 * This function makes sure that backing pages exists for the shmem GEM object
201 * and increases the use count.
202 *
203 * Returns:
204 * 0 on success or a negative error code on failure.
205 */
206int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem)
207{
208 int ret;
209
210 WARN_ON(shmem->base.import_attach);
211
212 ret = mutex_lock_interruptible(&shmem->pages_lock);
213 if (ret)
214 return ret;
215 ret = drm_gem_shmem_get_pages_locked(shmem);
216 mutex_unlock(&shmem->pages_lock);
217
218 return ret;
219}
220EXPORT_SYMBOL(drm_gem_shmem_get_pages);
221
222static void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object *shmem)
223{
224 struct drm_gem_object *obj = &shmem->base;
225
226 if (WARN_ON_ONCE(!shmem->pages_use_count))
227 return;
228
229 if (--shmem->pages_use_count > 0)
230 return;
231
232#ifdef CONFIG_X86
233 if (shmem->map_wc)
234 set_pages_array_wb(shmem->pages, obj->size >> PAGE_SHIFT);
235#endif
236
237 drm_gem_put_pages(obj, shmem->pages,
238 shmem->pages_mark_dirty_on_put,
239 shmem->pages_mark_accessed_on_put);
240 shmem->pages = NULL;
241}
242
243/*
244 * drm_gem_shmem_put_pages - Decrease use count on the backing pages for a shmem GEM object
245 * @shmem: shmem GEM object
246 *
247 * This function decreases the use count and puts the backing pages when use drops to zero.
248 */
249void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem)
250{
251 mutex_lock(&shmem->pages_lock);
252 drm_gem_shmem_put_pages_locked(shmem);
253 mutex_unlock(&shmem->pages_lock);
254}
255EXPORT_SYMBOL(drm_gem_shmem_put_pages);
256
257/**
258 * drm_gem_shmem_pin - Pin backing pages for a shmem GEM object
259 * @shmem: shmem GEM object
260 *
261 * This function makes sure the backing pages are pinned in memory while the
262 * buffer is exported.
263 *
264 * Returns:
265 * 0 on success or a negative error code on failure.
266 */
267int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem)
268{
269 WARN_ON(shmem->base.import_attach);
270
271 return drm_gem_shmem_get_pages(shmem);
272}
273EXPORT_SYMBOL(drm_gem_shmem_pin);
274
275/**
276 * drm_gem_shmem_unpin - Unpin backing pages for a shmem GEM object
277 * @shmem: shmem GEM object
278 *
279 * This function removes the requirement that the backing pages are pinned in
280 * memory.
281 */
282void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem)
283{
284 WARN_ON(shmem->base.import_attach);
285
286 drm_gem_shmem_put_pages(shmem);
287}
288EXPORT_SYMBOL(drm_gem_shmem_unpin);
289
290static int drm_gem_shmem_vmap_locked(struct drm_gem_shmem_object *shmem,
291 struct iosys_map *map)
292{
293 struct drm_gem_object *obj = &shmem->base;
294 int ret = 0;
295
296 if (shmem->vmap_use_count++ > 0) {
297 iosys_map_set_vaddr(map, shmem->vaddr);
298 return 0;
299 }
300
301 if (obj->import_attach) {
302 ret = dma_buf_vmap(obj->import_attach->dmabuf, map);
303 if (!ret) {
304 if (WARN_ON(map->is_iomem)) {
305 dma_buf_vunmap(obj->import_attach->dmabuf, map);
306 ret = -EIO;
307 goto err_put_pages;
308 }
309 shmem->vaddr = map->vaddr;
310 }
311 } else {
312 pgprot_t prot = PAGE_KERNEL;
313
314 ret = drm_gem_shmem_get_pages(shmem);
315 if (ret)
316 goto err_zero_use;
317
318 if (shmem->map_wc)
319 prot = pgprot_writecombine(prot);
320 shmem->vaddr = vmap(shmem->pages, obj->size >> PAGE_SHIFT,
321 VM_MAP, prot);
322 if (!shmem->vaddr)
323 ret = -ENOMEM;
324 else
325 iosys_map_set_vaddr(map, shmem->vaddr);
326 }
327
328 if (ret) {
329 DRM_DEBUG_KMS("Failed to vmap pages, error %d\n", ret);
330 goto err_put_pages;
331 }
332
333 return 0;
334
335err_put_pages:
336 if (!obj->import_attach)
337 drm_gem_shmem_put_pages(shmem);
338err_zero_use:
339 shmem->vmap_use_count = 0;
340
341 return ret;
342}
343
344/*
345 * drm_gem_shmem_vmap - Create a virtual mapping for a shmem GEM object
346 * @shmem: shmem GEM object
347 * @map: Returns the kernel virtual address of the SHMEM GEM object's backing
348 * store.
349 *
350 * This function makes sure that a contiguous kernel virtual address mapping
351 * exists for the buffer backing the shmem GEM object. It hides the differences
352 * between dma-buf imported and natively allocated objects.
353 *
354 * Acquired mappings should be cleaned up by calling drm_gem_shmem_vunmap().
355 *
356 * Returns:
357 * 0 on success or a negative error code on failure.
358 */
359int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem,
360 struct iosys_map *map)
361{
362 int ret;
363
364 ret = mutex_lock_interruptible(&shmem->vmap_lock);
365 if (ret)
366 return ret;
367 ret = drm_gem_shmem_vmap_locked(shmem, map);
368 mutex_unlock(&shmem->vmap_lock);
369
370 return ret;
371}
372EXPORT_SYMBOL(drm_gem_shmem_vmap);
373
374static void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object *shmem,
375 struct iosys_map *map)
376{
377 struct drm_gem_object *obj = &shmem->base;
378
379 if (WARN_ON_ONCE(!shmem->vmap_use_count))
380 return;
381
382 if (--shmem->vmap_use_count > 0)
383 return;
384
385 if (obj->import_attach) {
386 dma_buf_vunmap(obj->import_attach->dmabuf, map);
387 } else {
388 vunmap(shmem->vaddr);
389 drm_gem_shmem_put_pages(shmem);
390 }
391
392 shmem->vaddr = NULL;
393}
394
395/*
396 * drm_gem_shmem_vunmap - Unmap a virtual mapping for a shmem GEM object
397 * @shmem: shmem GEM object
398 * @map: Kernel virtual address where the SHMEM GEM object was mapped
399 *
400 * This function cleans up a kernel virtual address mapping acquired by
401 * drm_gem_shmem_vmap(). The mapping is only removed when the use count drops to
402 * zero.
403 *
404 * This function hides the differences between dma-buf imported and natively
405 * allocated objects.
406 */
407void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem,
408 struct iosys_map *map)
409{
410 mutex_lock(&shmem->vmap_lock);
411 drm_gem_shmem_vunmap_locked(shmem, map);
412 mutex_unlock(&shmem->vmap_lock);
413}
414EXPORT_SYMBOL(drm_gem_shmem_vunmap);
415
416static struct drm_gem_shmem_object *
417drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
418 struct drm_device *dev, size_t size,
419 uint32_t *handle)
420{
421 struct drm_gem_shmem_object *shmem;
422 int ret;
423
424 shmem = drm_gem_shmem_create(dev, size);
425 if (IS_ERR(shmem))
426 return shmem;
427
428 /*
429 * Allocate an id of idr table where the obj is registered
430 * and handle has the id what user can see.
431 */
432 ret = drm_gem_handle_create(file_priv, &shmem->base, handle);
433 /* drop reference from allocate - handle holds it now. */
434 drm_gem_object_put(&shmem->base);
435 if (ret)
436 return ERR_PTR(ret);
437
438 return shmem;
439}
440
441/* Update madvise status, returns true if not purged, else
442 * false or -errno.
443 */
444int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv)
445{
446 mutex_lock(&shmem->pages_lock);
447
448 if (shmem->madv >= 0)
449 shmem->madv = madv;
450
451 madv = shmem->madv;
452
453 mutex_unlock(&shmem->pages_lock);
454
455 return (madv >= 0);
456}
457EXPORT_SYMBOL(drm_gem_shmem_madvise);
458
459void drm_gem_shmem_purge_locked(struct drm_gem_shmem_object *shmem)
460{
461 struct drm_gem_object *obj = &shmem->base;
462 struct drm_device *dev = obj->dev;
463
464 WARN_ON(!drm_gem_shmem_is_purgeable(shmem));
465
466 dma_unmap_sgtable(dev->dev, shmem->sgt, DMA_BIDIRECTIONAL, 0);
467 sg_free_table(shmem->sgt);
468 kfree(shmem->sgt);
469 shmem->sgt = NULL;
470
471 drm_gem_shmem_put_pages_locked(shmem);
472
473 shmem->madv = -1;
474
475 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
476 drm_gem_free_mmap_offset(obj);
477
478 /* Our goal here is to return as much of the memory as
479 * is possible back to the system as we are called from OOM.
480 * To do this we must instruct the shmfs to drop all of its
481 * backing pages, *now*.
482 */
483 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
484
485 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping, 0, (loff_t)-1);
486}
487EXPORT_SYMBOL(drm_gem_shmem_purge_locked);
488
489bool drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem)
490{
491 if (!mutex_trylock(&shmem->pages_lock))
492 return false;
493 drm_gem_shmem_purge_locked(shmem);
494 mutex_unlock(&shmem->pages_lock);
495
496 return true;
497}
498EXPORT_SYMBOL(drm_gem_shmem_purge);
499
500/**
501 * drm_gem_shmem_dumb_create - Create a dumb shmem buffer object
502 * @file: DRM file structure to create the dumb buffer for
503 * @dev: DRM device
504 * @args: IOCTL data
505 *
506 * This function computes the pitch of the dumb buffer and rounds it up to an
507 * integer number of bytes per pixel. Drivers for hardware that doesn't have
508 * any additional restrictions on the pitch can directly use this function as
509 * their &drm_driver.dumb_create callback.
510 *
511 * For hardware with additional restrictions, drivers can adjust the fields
512 * set up by userspace before calling into this function.
513 *
514 * Returns:
515 * 0 on success or a negative error code on failure.
516 */
517int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
518 struct drm_mode_create_dumb *args)
519{
520 u32 min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
521 struct drm_gem_shmem_object *shmem;
522
523 if (!args->pitch || !args->size) {
524 args->pitch = min_pitch;
525 args->size = PAGE_ALIGN(args->pitch * args->height);
526 } else {
527 /* ensure sane minimum values */
528 if (args->pitch < min_pitch)
529 args->pitch = min_pitch;
530 if (args->size < args->pitch * args->height)
531 args->size = PAGE_ALIGN(args->pitch * args->height);
532 }
533
534 shmem = drm_gem_shmem_create_with_handle(file, dev, args->size, &args->handle);
535
536 return PTR_ERR_OR_ZERO(shmem);
537}
538EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
539
540static vm_fault_t drm_gem_shmem_fault(struct vm_fault *vmf)
541{
542 struct vm_area_struct *vma = vmf->vma;
543 struct drm_gem_object *obj = vma->vm_private_data;
544 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
545 loff_t num_pages = obj->size >> PAGE_SHIFT;
546 vm_fault_t ret;
547 struct page *page;
548 pgoff_t page_offset;
549
550 /* We don't use vmf->pgoff since that has the fake offset */
551 page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
552
553 mutex_lock(&shmem->pages_lock);
554
555 if (page_offset >= num_pages ||
556 WARN_ON_ONCE(!shmem->pages) ||
557 shmem->madv < 0) {
558 ret = VM_FAULT_SIGBUS;
559 } else {
560 page = shmem->pages[page_offset];
561
562 ret = vmf_insert_pfn(vma, vmf->address, page_to_pfn(page));
563 }
564
565 mutex_unlock(&shmem->pages_lock);
566
567 return ret;
568}
569
570static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
571{
572 struct drm_gem_object *obj = vma->vm_private_data;
573 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
574
575 WARN_ON(shmem->base.import_attach);
576
577 mutex_lock(&shmem->pages_lock);
578
579 /*
580 * We should have already pinned the pages when the buffer was first
581 * mmap'd, vm_open() just grabs an additional reference for the new
582 * mm the vma is getting copied into (ie. on fork()).
583 */
584 if (!WARN_ON_ONCE(!shmem->pages_use_count))
585 shmem->pages_use_count++;
586
587 mutex_unlock(&shmem->pages_lock);
588
589 drm_gem_vm_open(vma);
590}
591
592static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
593{
594 struct drm_gem_object *obj = vma->vm_private_data;
595 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
596
597 drm_gem_shmem_put_pages(shmem);
598 drm_gem_vm_close(vma);
599}
600
601const struct vm_operations_struct drm_gem_shmem_vm_ops = {
602 .fault = drm_gem_shmem_fault,
603 .open = drm_gem_shmem_vm_open,
604 .close = drm_gem_shmem_vm_close,
605};
606EXPORT_SYMBOL_GPL(drm_gem_shmem_vm_ops);
607
608/**
609 * drm_gem_shmem_mmap - Memory-map a shmem GEM object
610 * @shmem: shmem GEM object
611 * @vma: VMA for the area to be mapped
612 *
613 * This function implements an augmented version of the GEM DRM file mmap
614 * operation for shmem objects.
615 *
616 * Returns:
617 * 0 on success or a negative error code on failure.
618 */
619int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma)
620{
621 struct drm_gem_object *obj = &shmem->base;
622 int ret;
623
624 if (obj->import_attach) {
625 /* Drop the reference drm_gem_mmap_obj() acquired.*/
626 drm_gem_object_put(obj);
627 vma->vm_private_data = NULL;
628
629 return dma_buf_mmap(obj->dma_buf, vma, 0);
630 }
631
632 ret = drm_gem_shmem_get_pages(shmem);
633 if (ret)
634 return ret;
635
636 vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
637 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
638 if (shmem->map_wc)
639 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
640
641 return 0;
642}
643EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap);
644
645/**
646 * drm_gem_shmem_print_info() - Print &drm_gem_shmem_object info for debugfs
647 * @shmem: shmem GEM object
648 * @p: DRM printer
649 * @indent: Tab indentation level
650 */
651void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
652 struct drm_printer *p, unsigned int indent)
653{
654 drm_printf_indent(p, indent, "pages_use_count=%u\n", shmem->pages_use_count);
655 drm_printf_indent(p, indent, "vmap_use_count=%u\n", shmem->vmap_use_count);
656 drm_printf_indent(p, indent, "vaddr=%p\n", shmem->vaddr);
657}
658EXPORT_SYMBOL(drm_gem_shmem_print_info);
659
660/**
661 * drm_gem_shmem_get_sg_table - Provide a scatter/gather table of pinned
662 * pages for a shmem GEM object
663 * @shmem: shmem GEM object
664 *
665 * This function exports a scatter/gather table suitable for PRIME usage by
666 * calling the standard DMA mapping API.
667 *
668 * Drivers who need to acquire an scatter/gather table for objects need to call
669 * drm_gem_shmem_get_pages_sgt() instead.
670 *
671 * Returns:
672 * A pointer to the scatter/gather table of pinned pages or error pointer on failure.
673 */
674struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem)
675{
676 struct drm_gem_object *obj = &shmem->base;
677
678 WARN_ON(shmem->base.import_attach);
679
680 return drm_prime_pages_to_sg(obj->dev, shmem->pages, obj->size >> PAGE_SHIFT);
681}
682EXPORT_SYMBOL_GPL(drm_gem_shmem_get_sg_table);
683
684/**
685 * drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a
686 * scatter/gather table for a shmem GEM object.
687 * @shmem: shmem GEM object
688 *
689 * This function returns a scatter/gather table suitable for driver usage. If
690 * the sg table doesn't exist, the pages are pinned, dma-mapped, and a sg
691 * table created.
692 *
693 * This is the main function for drivers to get at backing storage, and it hides
694 * and difference between dma-buf imported and natively allocated objects.
695 * drm_gem_shmem_get_sg_table() should not be directly called by drivers.
696 *
697 * Returns:
698 * A pointer to the scatter/gather table of pinned pages or errno on failure.
699 */
700struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
701{
702 struct drm_gem_object *obj = &shmem->base;
703 int ret;
704 struct sg_table *sgt;
705
706 if (shmem->sgt)
707 return shmem->sgt;
708
709 WARN_ON(obj->import_attach);
710
711 ret = drm_gem_shmem_get_pages(shmem);
712 if (ret)
713 return ERR_PTR(ret);
714
715 sgt = drm_gem_shmem_get_sg_table(shmem);
716 if (IS_ERR(sgt)) {
717 ret = PTR_ERR(sgt);
718 goto err_put_pages;
719 }
720 /* Map the pages for use by the h/w. */
721 ret = dma_map_sgtable(obj->dev->dev, sgt, DMA_BIDIRECTIONAL, 0);
722 if (ret)
723 goto err_free_sgt;
724
725 shmem->sgt = sgt;
726
727 return sgt;
728
729err_free_sgt:
730 sg_free_table(sgt);
731 kfree(sgt);
732err_put_pages:
733 drm_gem_shmem_put_pages(shmem);
734 return ERR_PTR(ret);
735}
736EXPORT_SYMBOL_GPL(drm_gem_shmem_get_pages_sgt);
737
738/**
739 * drm_gem_shmem_prime_import_sg_table - Produce a shmem GEM object from
740 * another driver's scatter/gather table of pinned pages
741 * @dev: Device to import into
742 * @attach: DMA-BUF attachment
743 * @sgt: Scatter/gather table of pinned pages
744 *
745 * This function imports a scatter/gather table exported via DMA-BUF by
746 * another driver. Drivers that use the shmem helpers should set this as their
747 * &drm_driver.gem_prime_import_sg_table callback.
748 *
749 * Returns:
750 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
751 * error code on failure.
752 */
753struct drm_gem_object *
754drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
755 struct dma_buf_attachment *attach,
756 struct sg_table *sgt)
757{
758 size_t size = PAGE_ALIGN(attach->dmabuf->size);
759 struct drm_gem_shmem_object *shmem;
760
761 shmem = __drm_gem_shmem_create(dev, size, true);
762 if (IS_ERR(shmem))
763 return ERR_CAST(shmem);
764
765 shmem->sgt = sgt;
766
767 DRM_DEBUG_PRIME("size = %zu\n", size);
768
769 return &shmem->base;
770}
771EXPORT_SYMBOL_GPL(drm_gem_shmem_prime_import_sg_table);
772
773MODULE_DESCRIPTION("DRM SHMEM memory-management helpers");
774MODULE_IMPORT_NS(DMA_BUF);
775MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2018 Noralf Trønnes
4 */
5
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>
12
13#include <drm/drm.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>
19
20/**
21 * DOC: overview
22 *
23 * This library provides helpers for GEM objects backed by shmem buffers
24 * allocated using anonymous pageable memory.
25 */
26
27static const struct drm_gem_object_funcs drm_gem_shmem_funcs = {
28 .free = drm_gem_shmem_free_object,
29 .print_info = drm_gem_shmem_print_info,
30 .pin = drm_gem_shmem_pin,
31 .unpin = drm_gem_shmem_unpin,
32 .get_sg_table = drm_gem_shmem_get_sg_table,
33 .vmap = drm_gem_shmem_vmap,
34 .vunmap = drm_gem_shmem_vunmap,
35 .mmap = drm_gem_shmem_mmap,
36};
37
38static struct drm_gem_shmem_object *
39__drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
40{
41 struct drm_gem_shmem_object *shmem;
42 struct drm_gem_object *obj;
43 int ret = 0;
44
45 size = PAGE_ALIGN(size);
46
47 if (dev->driver->gem_create_object)
48 obj = dev->driver->gem_create_object(dev, size);
49 else
50 obj = kzalloc(sizeof(*shmem), GFP_KERNEL);
51 if (!obj)
52 return ERR_PTR(-ENOMEM);
53
54 if (!obj->funcs)
55 obj->funcs = &drm_gem_shmem_funcs;
56
57 if (private)
58 drm_gem_private_object_init(dev, obj, size);
59 else
60 ret = drm_gem_object_init(dev, obj, size);
61 if (ret)
62 goto err_free;
63
64 ret = drm_gem_create_mmap_offset(obj);
65 if (ret)
66 goto err_release;
67
68 shmem = to_drm_gem_shmem_obj(obj);
69 mutex_init(&shmem->pages_lock);
70 mutex_init(&shmem->vmap_lock);
71 INIT_LIST_HEAD(&shmem->madv_list);
72
73 if (!private) {
74 /*
75 * Our buffers are kept pinned, so allocating them
76 * from the MOVABLE zone is a really bad idea, and
77 * conflicts with CMA. See comments above new_inode()
78 * why this is required _and_ expected if you're
79 * going to pin these pages.
80 */
81 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER |
82 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
83 }
84
85 return shmem;
86
87err_release:
88 drm_gem_object_release(obj);
89err_free:
90 kfree(obj);
91
92 return ERR_PTR(ret);
93}
94/**
95 * drm_gem_shmem_create - Allocate an object with the given size
96 * @dev: DRM device
97 * @size: Size of the object to allocate
98 *
99 * This function creates a shmem GEM object.
100 *
101 * Returns:
102 * A struct drm_gem_shmem_object * on success or an ERR_PTR()-encoded negative
103 * error code on failure.
104 */
105struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size)
106{
107 return __drm_gem_shmem_create(dev, size, false);
108}
109EXPORT_SYMBOL_GPL(drm_gem_shmem_create);
110
111/**
112 * drm_gem_shmem_free_object - Free resources associated with a shmem GEM object
113 * @obj: GEM object to free
114 *
115 * This function cleans up the GEM object state and frees the memory used to
116 * store the object itself. It should be used to implement
117 * &drm_gem_object_funcs.free.
118 */
119void drm_gem_shmem_free_object(struct drm_gem_object *obj)
120{
121 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
122
123 WARN_ON(shmem->vmap_use_count);
124
125 if (obj->import_attach) {
126 drm_prime_gem_destroy(obj, shmem->sgt);
127 } else {
128 if (shmem->sgt) {
129 dma_unmap_sg(obj->dev->dev, shmem->sgt->sgl,
130 shmem->sgt->nents, DMA_BIDIRECTIONAL);
131 sg_free_table(shmem->sgt);
132 kfree(shmem->sgt);
133 }
134 if (shmem->pages)
135 drm_gem_shmem_put_pages(shmem);
136 }
137
138 WARN_ON(shmem->pages_use_count);
139
140 drm_gem_object_release(obj);
141 mutex_destroy(&shmem->pages_lock);
142 mutex_destroy(&shmem->vmap_lock);
143 kfree(shmem);
144}
145EXPORT_SYMBOL_GPL(drm_gem_shmem_free_object);
146
147static int drm_gem_shmem_get_pages_locked(struct drm_gem_shmem_object *shmem)
148{
149 struct drm_gem_object *obj = &shmem->base;
150 struct page **pages;
151
152 if (shmem->pages_use_count++ > 0)
153 return 0;
154
155 pages = drm_gem_get_pages(obj);
156 if (IS_ERR(pages)) {
157 DRM_DEBUG_KMS("Failed to get pages (%ld)\n", PTR_ERR(pages));
158 shmem->pages_use_count = 0;
159 return PTR_ERR(pages);
160 }
161
162 shmem->pages = pages;
163
164 return 0;
165}
166
167/*
168 * drm_gem_shmem_get_pages - Allocate backing pages for a shmem GEM object
169 * @shmem: shmem GEM object
170 *
171 * This function makes sure that backing pages exists for the shmem GEM object
172 * and increases the use count.
173 *
174 * Returns:
175 * 0 on success or a negative error code on failure.
176 */
177int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem)
178{
179 int ret;
180
181 WARN_ON(shmem->base.import_attach);
182
183 ret = mutex_lock_interruptible(&shmem->pages_lock);
184 if (ret)
185 return ret;
186 ret = drm_gem_shmem_get_pages_locked(shmem);
187 mutex_unlock(&shmem->pages_lock);
188
189 return ret;
190}
191EXPORT_SYMBOL(drm_gem_shmem_get_pages);
192
193static void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object *shmem)
194{
195 struct drm_gem_object *obj = &shmem->base;
196
197 if (WARN_ON_ONCE(!shmem->pages_use_count))
198 return;
199
200 if (--shmem->pages_use_count > 0)
201 return;
202
203 drm_gem_put_pages(obj, shmem->pages,
204 shmem->pages_mark_dirty_on_put,
205 shmem->pages_mark_accessed_on_put);
206 shmem->pages = NULL;
207}
208
209/*
210 * drm_gem_shmem_put_pages - Decrease use count on the backing pages for a shmem GEM object
211 * @shmem: shmem GEM object
212 *
213 * This function decreases the use count and puts the backing pages when use drops to zero.
214 */
215void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem)
216{
217 mutex_lock(&shmem->pages_lock);
218 drm_gem_shmem_put_pages_locked(shmem);
219 mutex_unlock(&shmem->pages_lock);
220}
221EXPORT_SYMBOL(drm_gem_shmem_put_pages);
222
223/**
224 * drm_gem_shmem_pin - Pin backing pages for a shmem GEM object
225 * @obj: GEM object
226 *
227 * This function makes sure the backing pages are pinned in memory while the
228 * buffer is exported. It should only be used to implement
229 * &drm_gem_object_funcs.pin.
230 *
231 * Returns:
232 * 0 on success or a negative error code on failure.
233 */
234int drm_gem_shmem_pin(struct drm_gem_object *obj)
235{
236 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
237
238 WARN_ON(shmem->base.import_attach);
239
240 return drm_gem_shmem_get_pages(shmem);
241}
242EXPORT_SYMBOL(drm_gem_shmem_pin);
243
244/**
245 * drm_gem_shmem_unpin - Unpin backing pages for a shmem GEM object
246 * @obj: GEM object
247 *
248 * This function removes the requirement that the backing pages are pinned in
249 * memory. It should only be used to implement &drm_gem_object_funcs.unpin.
250 */
251void drm_gem_shmem_unpin(struct drm_gem_object *obj)
252{
253 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
254
255 WARN_ON(shmem->base.import_attach);
256
257 drm_gem_shmem_put_pages(shmem);
258}
259EXPORT_SYMBOL(drm_gem_shmem_unpin);
260
261static void *drm_gem_shmem_vmap_locked(struct drm_gem_shmem_object *shmem)
262{
263 struct drm_gem_object *obj = &shmem->base;
264 int ret;
265
266 if (shmem->vmap_use_count++ > 0)
267 return shmem->vaddr;
268
269 if (obj->import_attach) {
270 shmem->vaddr = dma_buf_vmap(obj->import_attach->dmabuf);
271 } else {
272 pgprot_t prot = PAGE_KERNEL;
273
274 ret = drm_gem_shmem_get_pages(shmem);
275 if (ret)
276 goto err_zero_use;
277
278 if (!shmem->map_cached)
279 prot = pgprot_writecombine(prot);
280 shmem->vaddr = vmap(shmem->pages, obj->size >> PAGE_SHIFT,
281 VM_MAP, prot);
282 }
283
284 if (!shmem->vaddr) {
285 DRM_DEBUG_KMS("Failed to vmap pages\n");
286 ret = -ENOMEM;
287 goto err_put_pages;
288 }
289
290 return shmem->vaddr;
291
292err_put_pages:
293 if (!obj->import_attach)
294 drm_gem_shmem_put_pages(shmem);
295err_zero_use:
296 shmem->vmap_use_count = 0;
297
298 return ERR_PTR(ret);
299}
300
301/*
302 * drm_gem_shmem_vmap - Create a virtual mapping for a shmem GEM object
303 * @shmem: shmem GEM object
304 *
305 * This function makes sure that a contiguous kernel virtual address mapping
306 * exists for the buffer backing the shmem GEM object.
307 *
308 * This function can be used to implement &drm_gem_object_funcs.vmap. But it can
309 * also be called by drivers directly, in which case it will hide the
310 * differences between dma-buf imported and natively allocated objects.
311 *
312 * Acquired mappings should be cleaned up by calling drm_gem_shmem_vunmap().
313 *
314 * Returns:
315 * 0 on success or a negative error code on failure.
316 */
317void *drm_gem_shmem_vmap(struct drm_gem_object *obj)
318{
319 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
320 void *vaddr;
321 int ret;
322
323 ret = mutex_lock_interruptible(&shmem->vmap_lock);
324 if (ret)
325 return ERR_PTR(ret);
326 vaddr = drm_gem_shmem_vmap_locked(shmem);
327 mutex_unlock(&shmem->vmap_lock);
328
329 return vaddr;
330}
331EXPORT_SYMBOL(drm_gem_shmem_vmap);
332
333static void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object *shmem)
334{
335 struct drm_gem_object *obj = &shmem->base;
336
337 if (WARN_ON_ONCE(!shmem->vmap_use_count))
338 return;
339
340 if (--shmem->vmap_use_count > 0)
341 return;
342
343 if (obj->import_attach)
344 dma_buf_vunmap(obj->import_attach->dmabuf, shmem->vaddr);
345 else
346 vunmap(shmem->vaddr);
347
348 shmem->vaddr = NULL;
349 drm_gem_shmem_put_pages(shmem);
350}
351
352/*
353 * drm_gem_shmem_vunmap - Unmap a virtual mapping fo a shmem GEM object
354 * @shmem: shmem GEM object
355 *
356 * This function cleans up a kernel virtual address mapping acquired by
357 * drm_gem_shmem_vmap(). The mapping is only removed when the use count drops to
358 * zero.
359 *
360 * This function can be used to implement &drm_gem_object_funcs.vmap. But it can
361 * also be called by drivers directly, in which case it will hide the
362 * differences between dma-buf imported and natively allocated objects.
363 */
364void drm_gem_shmem_vunmap(struct drm_gem_object *obj, void *vaddr)
365{
366 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
367
368 mutex_lock(&shmem->vmap_lock);
369 drm_gem_shmem_vunmap_locked(shmem);
370 mutex_unlock(&shmem->vmap_lock);
371}
372EXPORT_SYMBOL(drm_gem_shmem_vunmap);
373
374struct drm_gem_shmem_object *
375drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
376 struct drm_device *dev, size_t size,
377 uint32_t *handle)
378{
379 struct drm_gem_shmem_object *shmem;
380 int ret;
381
382 shmem = drm_gem_shmem_create(dev, size);
383 if (IS_ERR(shmem))
384 return shmem;
385
386 /*
387 * Allocate an id of idr table where the obj is registered
388 * and handle has the id what user can see.
389 */
390 ret = drm_gem_handle_create(file_priv, &shmem->base, handle);
391 /* drop reference from allocate - handle holds it now. */
392 drm_gem_object_put(&shmem->base);
393 if (ret)
394 return ERR_PTR(ret);
395
396 return shmem;
397}
398EXPORT_SYMBOL(drm_gem_shmem_create_with_handle);
399
400/* Update madvise status, returns true if not purged, else
401 * false or -errno.
402 */
403int drm_gem_shmem_madvise(struct drm_gem_object *obj, int madv)
404{
405 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
406
407 mutex_lock(&shmem->pages_lock);
408
409 if (shmem->madv >= 0)
410 shmem->madv = madv;
411
412 madv = shmem->madv;
413
414 mutex_unlock(&shmem->pages_lock);
415
416 return (madv >= 0);
417}
418EXPORT_SYMBOL(drm_gem_shmem_madvise);
419
420void drm_gem_shmem_purge_locked(struct drm_gem_object *obj)
421{
422 struct drm_device *dev = obj->dev;
423 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
424
425 WARN_ON(!drm_gem_shmem_is_purgeable(shmem));
426
427 dma_unmap_sg(obj->dev->dev, shmem->sgt->sgl,
428 shmem->sgt->nents, DMA_BIDIRECTIONAL);
429 sg_free_table(shmem->sgt);
430 kfree(shmem->sgt);
431 shmem->sgt = NULL;
432
433 drm_gem_shmem_put_pages_locked(shmem);
434
435 shmem->madv = -1;
436
437 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
438 drm_gem_free_mmap_offset(obj);
439
440 /* Our goal here is to return as much of the memory as
441 * is possible back to the system as we are called from OOM.
442 * To do this we must instruct the shmfs to drop all of its
443 * backing pages, *now*.
444 */
445 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
446
447 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
448 0, (loff_t)-1);
449}
450EXPORT_SYMBOL(drm_gem_shmem_purge_locked);
451
452bool drm_gem_shmem_purge(struct drm_gem_object *obj)
453{
454 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
455
456 if (!mutex_trylock(&shmem->pages_lock))
457 return false;
458 drm_gem_shmem_purge_locked(obj);
459 mutex_unlock(&shmem->pages_lock);
460
461 return true;
462}
463EXPORT_SYMBOL(drm_gem_shmem_purge);
464
465/**
466 * drm_gem_shmem_create_object_cached - Create a shmem buffer object with
467 * cached mappings
468 * @dev: DRM device
469 * @size: Size of the object to allocate
470 *
471 * By default, shmem buffer objects use writecombine mappings. This
472 * function implements struct drm_driver.gem_create_object for shmem
473 * buffer objects with cached mappings.
474 *
475 * Returns:
476 * A struct drm_gem_shmem_object * on success or NULL negative on failure.
477 */
478struct drm_gem_object *
479drm_gem_shmem_create_object_cached(struct drm_device *dev, size_t size)
480{
481 struct drm_gem_shmem_object *shmem;
482
483 shmem = kzalloc(sizeof(*shmem), GFP_KERNEL);
484 if (!shmem)
485 return NULL;
486 shmem->map_cached = true;
487
488 return &shmem->base;
489}
490EXPORT_SYMBOL(drm_gem_shmem_create_object_cached);
491
492/**
493 * drm_gem_shmem_dumb_create - Create a dumb shmem buffer object
494 * @file: DRM file structure to create the dumb buffer for
495 * @dev: DRM device
496 * @args: IOCTL data
497 *
498 * This function computes the pitch of the dumb buffer and rounds it up to an
499 * integer number of bytes per pixel. Drivers for hardware that doesn't have
500 * any additional restrictions on the pitch can directly use this function as
501 * their &drm_driver.dumb_create callback.
502 *
503 * For hardware with additional restrictions, drivers can adjust the fields
504 * set up by userspace before calling into this function.
505 *
506 * Returns:
507 * 0 on success or a negative error code on failure.
508 */
509int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
510 struct drm_mode_create_dumb *args)
511{
512 u32 min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
513 struct drm_gem_shmem_object *shmem;
514
515 if (!args->pitch || !args->size) {
516 args->pitch = min_pitch;
517 args->size = args->pitch * args->height;
518 } else {
519 /* ensure sane minimum values */
520 if (args->pitch < min_pitch)
521 args->pitch = min_pitch;
522 if (args->size < args->pitch * args->height)
523 args->size = args->pitch * args->height;
524 }
525
526 shmem = drm_gem_shmem_create_with_handle(file, dev, args->size, &args->handle);
527
528 return PTR_ERR_OR_ZERO(shmem);
529}
530EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
531
532static vm_fault_t drm_gem_shmem_fault(struct vm_fault *vmf)
533{
534 struct vm_area_struct *vma = vmf->vma;
535 struct drm_gem_object *obj = vma->vm_private_data;
536 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
537 loff_t num_pages = obj->size >> PAGE_SHIFT;
538 struct page *page;
539
540 if (vmf->pgoff >= num_pages || WARN_ON_ONCE(!shmem->pages))
541 return VM_FAULT_SIGBUS;
542
543 page = shmem->pages[vmf->pgoff];
544
545 return vmf_insert_page(vma, vmf->address, page);
546}
547
548static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
549{
550 struct drm_gem_object *obj = vma->vm_private_data;
551 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
552 int ret;
553
554 WARN_ON(shmem->base.import_attach);
555
556 ret = drm_gem_shmem_get_pages(shmem);
557 WARN_ON_ONCE(ret != 0);
558
559 drm_gem_vm_open(vma);
560}
561
562static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
563{
564 struct drm_gem_object *obj = vma->vm_private_data;
565 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
566
567 drm_gem_shmem_put_pages(shmem);
568 drm_gem_vm_close(vma);
569}
570
571static 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,
575};
576
577/**
578 * drm_gem_shmem_mmap - Memory-map a shmem GEM object
579 * @obj: gem object
580 * @vma: VMA for the area to be mapped
581 *
582 * This function implements an augmented version of the GEM DRM file mmap
583 * operation for shmem objects. Drivers which employ the shmem helpers should
584 * use this function as their &drm_gem_object_funcs.mmap handler.
585 *
586 * Returns:
587 * 0 on success or a negative error code on failure.
588 */
589int drm_gem_shmem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
590{
591 struct drm_gem_shmem_object *shmem;
592 int ret;
593
594 /* Remove the fake offset */
595 vma->vm_pgoff -= drm_vma_node_start(&obj->vma_node);
596
597 if (obj->import_attach)
598 return dma_buf_mmap(obj->dma_buf, vma, 0);
599
600 shmem = to_drm_gem_shmem_obj(obj);
601
602 ret = drm_gem_shmem_get_pages(shmem);
603 if (ret) {
604 drm_gem_vm_close(vma);
605 return ret;
606 }
607
608 vma->vm_flags |= VM_MIXEDMAP | VM_DONTEXPAND;
609 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
610 if (!shmem->map_cached)
611 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
612 vma->vm_ops = &drm_gem_shmem_vm_ops;
613
614 return 0;
615}
616EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap);
617
618/**
619 * drm_gem_shmem_print_info() - Print &drm_gem_shmem_object info for debugfs
620 * @p: DRM printer
621 * @indent: Tab indentation level
622 * @obj: GEM object
623 *
624 * This implements the &drm_gem_object_funcs.info callback.
625 */
626void drm_gem_shmem_print_info(struct drm_printer *p, unsigned int indent,
627 const struct drm_gem_object *obj)
628{
629 const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
630
631 drm_printf_indent(p, indent, "pages_use_count=%u\n", shmem->pages_use_count);
632 drm_printf_indent(p, indent, "vmap_use_count=%u\n", shmem->vmap_use_count);
633 drm_printf_indent(p, indent, "vaddr=%p\n", shmem->vaddr);
634}
635EXPORT_SYMBOL(drm_gem_shmem_print_info);
636
637/**
638 * drm_gem_shmem_get_sg_table - Provide a scatter/gather table of pinned
639 * pages for a shmem GEM object
640 * @obj: GEM object
641 *
642 * This function exports a scatter/gather table suitable for PRIME usage by
643 * calling the standard DMA mapping API. Drivers should not call this function
644 * directly, instead it should only be used as an implementation for
645 * &drm_gem_object_funcs.get_sg_table.
646 *
647 * Drivers who need to acquire an scatter/gather table for objects need to call
648 * drm_gem_shmem_get_pages_sgt() instead.
649 *
650 * Returns:
651 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
652 */
653struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_object *obj)
654{
655 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
656
657 WARN_ON(shmem->base.import_attach);
658
659 return drm_prime_pages_to_sg(shmem->pages, obj->size >> PAGE_SHIFT);
660}
661EXPORT_SYMBOL_GPL(drm_gem_shmem_get_sg_table);
662
663/**
664 * drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a
665 * scatter/gather table for a shmem GEM object.
666 * @obj: GEM object
667 *
668 * This function returns a scatter/gather table suitable for driver usage. If
669 * the sg table doesn't exist, the pages are pinned, dma-mapped, and a sg
670 * table created.
671 *
672 * This is the main function for drivers to get at backing storage, and it hides
673 * and difference between dma-buf imported and natively allocated objects.
674 * drm_gem_shmem_get_sg_table() should not be directly called by drivers.
675 *
676 * Returns:
677 * A pointer to the scatter/gather table of pinned pages or errno on failure.
678 */
679struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_object *obj)
680{
681 int ret;
682 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
683 struct sg_table *sgt;
684
685 if (shmem->sgt)
686 return shmem->sgt;
687
688 WARN_ON(obj->import_attach);
689
690 ret = drm_gem_shmem_get_pages(shmem);
691 if (ret)
692 return ERR_PTR(ret);
693
694 sgt = drm_gem_shmem_get_sg_table(&shmem->base);
695 if (IS_ERR(sgt)) {
696 ret = PTR_ERR(sgt);
697 goto err_put_pages;
698 }
699 /* Map the pages for use by the h/w. */
700 dma_map_sg(obj->dev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL);
701
702 shmem->sgt = sgt;
703
704 return sgt;
705
706err_put_pages:
707 drm_gem_shmem_put_pages(shmem);
708 return ERR_PTR(ret);
709}
710EXPORT_SYMBOL_GPL(drm_gem_shmem_get_pages_sgt);
711
712/**
713 * drm_gem_shmem_prime_import_sg_table - Produce a shmem GEM object from
714 * another driver's scatter/gather table of pinned pages
715 * @dev: Device to import into
716 * @attach: DMA-BUF attachment
717 * @sgt: Scatter/gather table of pinned pages
718 *
719 * This function imports a scatter/gather table exported via DMA-BUF by
720 * another driver. Drivers that use the shmem helpers should set this as their
721 * &drm_driver.gem_prime_import_sg_table callback.
722 *
723 * Returns:
724 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
725 * error code on failure.
726 */
727struct drm_gem_object *
728drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
729 struct dma_buf_attachment *attach,
730 struct sg_table *sgt)
731{
732 size_t size = PAGE_ALIGN(attach->dmabuf->size);
733 struct drm_gem_shmem_object *shmem;
734
735 shmem = __drm_gem_shmem_create(dev, size, true);
736 if (IS_ERR(shmem))
737 return ERR_CAST(shmem);
738
739 shmem->sgt = sgt;
740
741 DRM_DEBUG_PRIME("size = %zu\n", size);
742
743 return &shmem->base;
744}
745EXPORT_SYMBOL_GPL(drm_gem_shmem_prime_import_sg_table);