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