drm_gem.c revision 1.20 1 /* $NetBSD: drm_gem.c,v 1.20 2021/12/19 11:07:28 riastradh Exp $ */
2
3 /*
4 * Copyright 2008 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 * Authors:
26 * Eric Anholt <eric (at) anholt.net>
27 *
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: drm_gem.c,v 1.20 2021/12/19 11:07:28 riastradh Exp $");
32
33 #include <linux/types.h>
34 #include <linux/slab.h>
35 #include <linux/mm.h>
36 #include <linux/uaccess.h>
37 #include <linux/fs.h>
38 #include <linux/file.h>
39 #include <linux/module.h>
40 #include <linux/mman.h>
41 #include <linux/pagemap.h>
42 #include <linux/shmem_fs.h>
43 #include <linux/dma-buf.h>
44 #include <linux/mem_encrypt.h>
45 #include <linux/pagevec.h>
46
47 #include <drm/drm.h>
48 #include <drm/drm_device.h>
49 #include <drm/drm_drv.h>
50 #include <drm/drm_file.h>
51 #include <drm/drm_gem.h>
52 #include <drm/drm_print.h>
53 #include <drm/drm_vma_manager.h>
54
55 #include "drm_internal.h"
56
57 #ifdef __NetBSD__
58 #include <uvm/uvm_extern.h>
59 #include <linux/nbsd-namespace.h>
60 #endif
61
62 /** @file drm_gem.c
63 *
64 * This file provides some of the base ioctls and library routines for
65 * the graphics memory manager implemented by each device driver.
66 *
67 * Because various devices have different requirements in terms of
68 * synchronization and migration strategies, implementing that is left up to
69 * the driver, and all that the general API provides should be generic --
70 * allocating objects, reading/writing data with the cpu, freeing objects.
71 * Even there, platform-dependent optimizations for reading/writing data with
72 * the CPU mean we'll likely hook those out to driver-specific calls. However,
73 * the DRI2 implementation wants to have at least allocate/mmap be generic.
74 *
75 * The goal was to have swap-backed object allocation managed through
76 * struct file. However, file descriptors as handles to a struct file have
77 * two major failings:
78 * - Process limits prevent more than 1024 or so being used at a time by
79 * default.
80 * - Inability to allocate high fds will aggravate the X Server's select()
81 * handling, and likely that of many GL client applications as well.
82 *
83 * This led to a plan of using our own integer IDs (called handles, following
84 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
85 * ioctls. The objects themselves will still include the struct file so
86 * that we can transition to fds if the required kernel infrastructure shows
87 * up at a later date, and as our interface with shmfs for memory allocation.
88 */
89
90 /**
91 * drm_gem_init - Initialize the GEM device fields
92 * @dev: drm_devic structure to initialize
93 */
94 int
95 drm_gem_init(struct drm_device *dev)
96 {
97 struct drm_vma_offset_manager *vma_offset_manager;
98
99 mutex_init(&dev->object_name_lock);
100 idr_init_base(&dev->object_name_idr, 1);
101
102 vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
103 if (!vma_offset_manager) {
104 DRM_ERROR("out of memory\n");
105 return -ENOMEM;
106 }
107
108 dev->vma_offset_manager = vma_offset_manager;
109 drm_vma_offset_manager_init(vma_offset_manager,
110 DRM_FILE_PAGE_OFFSET_START,
111 DRM_FILE_PAGE_OFFSET_SIZE);
112
113 return 0;
114 }
115
116 void
117 drm_gem_destroy(struct drm_device *dev)
118 {
119
120 drm_vma_offset_manager_destroy(dev->vma_offset_manager);
121 kfree(dev->vma_offset_manager);
122 dev->vma_offset_manager = NULL;
123
124 idr_destroy(&dev->object_name_idr);
125 mutex_destroy(&dev->object_name_lock);
126 }
127
128 /**
129 * drm_gem_object_init - initialize an allocated shmem-backed GEM object
130 * @dev: drm_device the object should be initialized for
131 * @obj: drm_gem_object to initialize
132 * @size: object size
133 *
134 * Initialize an already allocated GEM object of the specified size with
135 * shmfs backing store.
136 */
137 int drm_gem_object_init(struct drm_device *dev,
138 struct drm_gem_object *obj, size_t size)
139 {
140 #ifndef __NetBSD__
141 struct file *filp;
142 #endif
143
144 drm_gem_private_object_init(dev, obj, size);
145
146 #ifdef __NetBSD__
147 /*
148 * A uao may not have size 0, but a gem object may. Allocate a
149 * spurious page so we needn't teach uao how to have size 0.
150 */
151 obj->filp = uao_create(MAX(size, PAGE_SIZE), 0);
152 /*
153 * XXX This is gross. We ought to do it the other way around:
154 * set the uao to have the main uvm object's lock. However,
155 * uvm_obj_setlock is not safe on uvm_aobjs.
156 */
157 rw_obj_hold(obj->filp->vmobjlock);
158 uvm_obj_setlock(&obj->gemo_uvmobj, obj->filp->vmobjlock);
159 #else
160 filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
161 if (IS_ERR(filp))
162 return PTR_ERR(filp);
163
164 obj->filp = filp;
165 #endif
166
167 return 0;
168 }
169 EXPORT_SYMBOL(drm_gem_object_init);
170
171 /**
172 * drm_gem_private_object_init - initialize an allocated private GEM object
173 * @dev: drm_device the object should be initialized for
174 * @obj: drm_gem_object to initialize
175 * @size: object size
176 *
177 * Initialize an already allocated GEM object of the specified size with
178 * no GEM provided backing store. Instead the caller is responsible for
179 * backing the object and handling it.
180 */
181 void drm_gem_private_object_init(struct drm_device *dev,
182 struct drm_gem_object *obj, size_t size)
183 {
184 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
185
186 obj->dev = dev;
187 #ifdef __NetBSD__
188 obj->filp = NULL;
189 KASSERT(drm_core_check_feature(dev, DRIVER_GEM));
190 KASSERT(dev->driver->gem_uvm_ops != NULL);
191 uvm_obj_init(&obj->gemo_uvmobj, dev->driver->gem_uvm_ops, true, 1);
192 #else
193 obj->filp = NULL;
194 #endif
195
196 kref_init(&obj->refcount);
197 obj->handle_count = 0;
198 obj->size = size;
199 dma_resv_init(&obj->_resv);
200 if (!obj->resv)
201 obj->resv = &obj->_resv;
202
203 #ifdef __NetBSD__
204 drm_vma_node_init(&obj->vma_node);
205 #else
206 drm_vma_node_reset(&obj->vma_node);
207 #endif
208 }
209 EXPORT_SYMBOL(drm_gem_private_object_init);
210
211 static void
212 drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
213 {
214 /*
215 * Note: obj->dma_buf can't disappear as long as we still hold a
216 * handle reference in obj->handle_count.
217 */
218 mutex_lock(&filp->prime.lock);
219 if (obj->dma_buf) {
220 drm_prime_remove_buf_handle_locked(&filp->prime,
221 obj->dma_buf);
222 }
223 mutex_unlock(&filp->prime.lock);
224 }
225
226 /**
227 * drm_gem_object_handle_free - release resources bound to userspace handles
228 * @obj: GEM object to clean up.
229 *
230 * Called after the last handle to the object has been closed
231 *
232 * Removes any name for the object. Note that this must be
233 * called before drm_gem_object_free or we'll be touching
234 * freed memory
235 */
236 static void drm_gem_object_handle_free(struct drm_gem_object *obj)
237 {
238 struct drm_device *dev = obj->dev;
239
240 /* Remove any name for this object */
241 if (obj->name) {
242 idr_remove(&dev->object_name_idr, obj->name);
243 obj->name = 0;
244 }
245 }
246
247 static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
248 {
249 #ifndef __NetBSD__
250 /* Unbreak the reference cycle if we have an exported dma_buf. */
251 if (obj->dma_buf) {
252 dma_buf_put(obj->dma_buf);
253 obj->dma_buf = NULL;
254 }
255 #endif
256 }
257
258 static void
259 drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
260 {
261 struct drm_device *dev = obj->dev;
262 bool final = false;
263
264 if (WARN_ON(obj->handle_count == 0))
265 return;
266
267 /*
268 * Must bump handle count first as this may be the last
269 * ref, in which case the object would disappear before we
270 * checked for a name
271 */
272
273 mutex_lock(&dev->object_name_lock);
274 if (--obj->handle_count == 0) {
275 drm_gem_object_handle_free(obj);
276 drm_gem_object_exported_dma_buf_free(obj);
277 final = true;
278 }
279 mutex_unlock(&dev->object_name_lock);
280
281 if (final)
282 drm_gem_object_put_unlocked(obj);
283 }
284
285 /*
286 * Called at device or object close to release the file's
287 * handle references on objects.
288 */
289 static int
290 drm_gem_object_release_handle(int id, void *ptr, void *data)
291 {
292 struct drm_file *file_priv = data;
293 struct drm_gem_object *obj = ptr;
294 struct drm_device *dev = obj->dev;
295
296 if (obj->funcs && obj->funcs->close)
297 obj->funcs->close(obj, file_priv);
298 else if (dev->driver->gem_close_object)
299 dev->driver->gem_close_object(obj, file_priv);
300
301 drm_gem_remove_prime_handles(obj, file_priv);
302 drm_vma_node_revoke(&obj->vma_node, file_priv);
303
304 drm_gem_object_handle_put_unlocked(obj);
305
306 return 0;
307 }
308
309 /**
310 * drm_gem_handle_delete - deletes the given file-private handle
311 * @filp: drm file-private structure to use for the handle look up
312 * @handle: userspace handle to delete
313 *
314 * Removes the GEM handle from the @filp lookup table which has been added with
315 * drm_gem_handle_create(). If this is the last handle also cleans up linked
316 * resources like GEM names.
317 */
318 int
319 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
320 {
321 struct drm_gem_object *obj;
322
323 spin_lock(&filp->table_lock);
324
325 /* Check if we currently have a reference on the object */
326 obj = idr_replace(&filp->object_idr, NULL, handle);
327 spin_unlock(&filp->table_lock);
328 if (IS_ERR_OR_NULL(obj))
329 return -EINVAL;
330
331 /* Release driver's reference and decrement refcount. */
332 drm_gem_object_release_handle(handle, obj, filp);
333
334 /* And finally make the handle available for future allocations. */
335 spin_lock(&filp->table_lock);
336 idr_remove(&filp->object_idr, handle);
337 spin_unlock(&filp->table_lock);
338
339 return 0;
340 }
341 EXPORT_SYMBOL(drm_gem_handle_delete);
342
343 /**
344 * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
345 * @file: drm file-private structure containing the gem object
346 * @dev: corresponding drm_device
347 * @handle: gem object handle
348 * @offset: return location for the fake mmap offset
349 *
350 * This implements the &drm_driver.dumb_map_offset kms driver callback for
351 * drivers which use gem to manage their backing storage.
352 *
353 * Returns:
354 * 0 on success or a negative error code on failure.
355 */
356 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
357 u32 handle, u64 *offset)
358 {
359 struct drm_gem_object *obj;
360 int ret;
361
362 obj = drm_gem_object_lookup(file, handle);
363 if (!obj)
364 return -ENOENT;
365
366 /* Don't allow imported objects to be mapped */
367 if (obj->import_attach) {
368 ret = -EINVAL;
369 goto out;
370 }
371
372 ret = drm_gem_create_mmap_offset(obj);
373 if (ret)
374 goto out;
375
376 *offset = drm_vma_node_offset_addr(&obj->vma_node);
377 out:
378 drm_gem_object_put_unlocked(obj);
379
380 return ret;
381 }
382 EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
383
384 /**
385 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
386 * @file: drm file-private structure to remove the dumb handle from
387 * @dev: corresponding drm_device
388 * @handle: the dumb handle to remove
389 *
390 * This implements the &drm_driver.dumb_destroy kms driver callback for drivers
391 * which use gem to manage their backing storage.
392 */
393 int drm_gem_dumb_destroy(struct drm_file *file,
394 struct drm_device *dev,
395 uint32_t handle)
396 {
397 return drm_gem_handle_delete(file, handle);
398 }
399 EXPORT_SYMBOL(drm_gem_dumb_destroy);
400
401 /**
402 * drm_gem_handle_create_tail - internal functions to create a handle
403 * @file_priv: drm file-private structure to register the handle for
404 * @obj: object to register
405 * @handlep: pointer to return the created handle to the caller
406 *
407 * This expects the &drm_device.object_name_lock to be held already and will
408 * drop it before returning. Used to avoid races in establishing new handles
409 * when importing an object from either an flink name or a dma-buf.
410 *
411 * Handles must be release again through drm_gem_handle_delete(). This is done
412 * when userspace closes @file_priv for all attached handles, or through the
413 * GEM_CLOSE ioctl for individual handles.
414 */
415 int
416 drm_gem_handle_create_tail(struct drm_file *file_priv,
417 struct drm_gem_object *obj,
418 u32 *handlep)
419 {
420 struct drm_device *dev = obj->dev;
421 u32 handle;
422 int ret;
423
424 WARN_ON(!mutex_is_locked(&dev->object_name_lock));
425 if (obj->handle_count++ == 0)
426 drm_gem_object_get(obj);
427
428 /*
429 * Get the user-visible handle using idr. Preload and perform
430 * allocation under our spinlock.
431 */
432 idr_preload(GFP_KERNEL);
433 spin_lock(&file_priv->table_lock);
434
435 ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
436
437 spin_unlock(&file_priv->table_lock);
438 idr_preload_end();
439
440 mutex_unlock(&dev->object_name_lock);
441 if (ret < 0)
442 goto err_unref;
443
444 handle = ret;
445
446 ret = drm_vma_node_allow(&obj->vma_node, file_priv);
447 if (ret)
448 goto err_remove;
449
450 if (obj->funcs && obj->funcs->open) {
451 ret = obj->funcs->open(obj, file_priv);
452 if (ret)
453 goto err_revoke;
454 } else if (dev->driver->gem_open_object) {
455 ret = dev->driver->gem_open_object(obj, file_priv);
456 if (ret)
457 goto err_revoke;
458 }
459
460 *handlep = handle;
461 return 0;
462
463 err_revoke:
464 drm_vma_node_revoke(&obj->vma_node, file_priv);
465 err_remove:
466 spin_lock(&file_priv->table_lock);
467 idr_remove(&file_priv->object_idr, handle);
468 spin_unlock(&file_priv->table_lock);
469 err_unref:
470 drm_gem_object_handle_put_unlocked(obj);
471 return ret;
472 }
473
474 /**
475 * drm_gem_handle_create - create a gem handle for an object
476 * @file_priv: drm file-private structure to register the handle for
477 * @obj: object to register
478 * @handlep: pionter to return the created handle to the caller
479 *
480 * Create a handle for this object. This adds a handle reference to the object,
481 * which includes a regular reference count. Callers will likely want to
482 * dereference the object afterwards.
483 *
484 * Since this publishes @obj to userspace it must be fully set up by this point,
485 * drivers must call this last in their buffer object creation callbacks.
486 */
487 int drm_gem_handle_create(struct drm_file *file_priv,
488 struct drm_gem_object *obj,
489 u32 *handlep)
490 {
491 mutex_lock(&obj->dev->object_name_lock);
492
493 return drm_gem_handle_create_tail(file_priv, obj, handlep);
494 }
495 EXPORT_SYMBOL(drm_gem_handle_create);
496
497
498 /**
499 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
500 * @obj: obj in question
501 *
502 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
503 *
504 * Note that drm_gem_object_release() already calls this function, so drivers
505 * don't have to take care of releasing the mmap offset themselves when freeing
506 * the GEM object.
507 */
508 void
509 drm_gem_free_mmap_offset(struct drm_gem_object *obj)
510 {
511 struct drm_device *dev = obj->dev;
512
513 drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
514 }
515 EXPORT_SYMBOL(drm_gem_free_mmap_offset);
516
517 /**
518 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
519 * @obj: obj in question
520 * @size: the virtual size
521 *
522 * GEM memory mapping works by handing back to userspace a fake mmap offset
523 * it can use in a subsequent mmap(2) call. The DRM core code then looks
524 * up the object based on the offset and sets up the various memory mapping
525 * structures.
526 *
527 * This routine allocates and attaches a fake offset for @obj, in cases where
528 * the virtual size differs from the physical size (ie. &drm_gem_object.size).
529 * Otherwise just use drm_gem_create_mmap_offset().
530 *
531 * This function is idempotent and handles an already allocated mmap offset
532 * transparently. Drivers do not need to check for this case.
533 */
534 int
535 drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
536 {
537 struct drm_device *dev = obj->dev;
538
539 return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
540 size / PAGE_SIZE);
541 }
542 EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
543
544 /**
545 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
546 * @obj: obj in question
547 *
548 * GEM memory mapping works by handing back to userspace a fake mmap offset
549 * it can use in a subsequent mmap(2) call. The DRM core code then looks
550 * up the object based on the offset and sets up the various memory mapping
551 * structures.
552 *
553 * This routine allocates and attaches a fake offset for @obj.
554 *
555 * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
556 * the fake offset again.
557 */
558 int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
559 {
560 return drm_gem_create_mmap_offset_size(obj, obj->size);
561 }
562 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
563
564 /*
565 * Move pages to appropriate lru and release the pagevec, decrementing the
566 * ref count of those pages.
567 */
568 #ifndef __NetBSD__
569 static void drm_gem_check_release_pagevec(struct pagevec *pvec)
570 {
571 check_move_unevictable_pages(pvec);
572 __pagevec_release(pvec);
573 cond_resched();
574 }
575 #endif
576
577 /**
578 * drm_gem_get_pages - helper to allocate backing pages for a GEM object
579 * from shmem
580 * @obj: obj in question
581 *
582 * This reads the page-array of the shmem-backing storage of the given gem
583 * object. An array of pages is returned. If a page is not allocated or
584 * swapped-out, this will allocate/swap-in the required pages. Note that the
585 * whole object is covered by the page-array and pinned in memory.
586 *
587 * Use drm_gem_put_pages() to release the array and unpin all pages.
588 *
589 * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
590 * If you require other GFP-masks, you have to do those allocations yourself.
591 *
592 * Note that you are not allowed to change gfp-zones during runtime. That is,
593 * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
594 * set during initialization. If you have special zone constraints, set them
595 * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care
596 * to keep pages in the required zone during swap-in.
597 */
598 #ifdef __NetBSD__
599 struct page **
600 drm_gem_get_pages(struct drm_gem_object *obj)
601 {
602 struct pglist pglist;
603 struct vm_page *vm_page;
604 struct page **pages;
605 unsigned i, npages;
606 int ret;
607
608 KASSERT((obj->size & (PAGE_SIZE - 1)) != 0);
609
610 npages = obj->size >> PAGE_SHIFT;
611 pages = kvmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
612 if (pages == NULL) {
613 ret = -ENOMEM;
614 goto fail0;
615 }
616
617 TAILQ_INIT(&pglist);
618 /* XXX errno NetBSD->Linux */
619 ret = -uvm_obj_wirepages(obj->filp, 0, obj->size, &pglist);
620 if (ret)
621 goto fail1;
622
623 i = 0;
624 TAILQ_FOREACH(vm_page, &pglist, pageq.queue)
625 pages[i++] = container_of(vm_page, struct page, p_vmp);
626
627 return pages;
628
629 fail1: kvfree(pages);
630 fail0: return ERR_PTR(ret);
631 }
632 #else
633 struct page **drm_gem_get_pages(struct drm_gem_object *obj)
634 {
635 struct address_space *mapping;
636 struct page *p, **pages;
637 struct pagevec pvec;
638 int i, npages;
639
640 /* This is the shared memory object that backs the GEM resource */
641 mapping = obj->filp->f_mapping;
642
643 /* We already BUG_ON() for non-page-aligned sizes in
644 * drm_gem_object_init(), so we should never hit this unless
645 * driver author is doing something really wrong:
646 */
647 WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
648
649 npages = obj->size >> PAGE_SHIFT;
650
651 pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
652 if (pages == NULL)
653 return ERR_PTR(-ENOMEM);
654
655 mapping_set_unevictable(mapping);
656
657 for (i = 0; i < npages; i++) {
658 p = shmem_read_mapping_page(mapping, i);
659 if (IS_ERR(p))
660 goto fail;
661 pages[i] = p;
662
663 /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
664 * correct region during swapin. Note that this requires
665 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
666 * so shmem can relocate pages during swapin if required.
667 */
668 BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
669 (page_to_pfn(p) >= 0x00100000UL));
670 }
671
672 return pages;
673
674 fail:
675 mapping_clear_unevictable(mapping);
676 pagevec_init(&pvec);
677 while (i--) {
678 if (!pagevec_add(&pvec, pages[i]))
679 drm_gem_check_release_pagevec(&pvec);
680 }
681 if (pagevec_count(&pvec))
682 drm_gem_check_release_pagevec(&pvec);
683
684 kvfree(pages);
685 return ERR_CAST(p);
686 }
687 #endif
688 EXPORT_SYMBOL(drm_gem_get_pages);
689
690 /**
691 * drm_gem_put_pages - helper to free backing pages for a GEM object
692 * @obj: obj in question
693 * @pages: pages to free
694 * @dirty: if true, pages will be marked as dirty
695 * @accessed: if true, the pages will be marked as accessed
696 */
697 #ifdef __NetBSD__
698 void
699 drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, bool dirty,
700 bool accessed __unused /* XXX */)
701 {
702 unsigned i;
703
704 for (i = 0; i < (obj->size >> PAGE_SHIFT); i++) {
705 if (dirty) {
706 rw_enter(obj->filp->vmobjlock, RW_WRITER);
707 uvm_pagemarkdirty(&pages[i]->p_vmp,
708 UVM_PAGE_STATUS_DIRTY);
709 rw_exit(obj->filp->vmobjlock);
710 }
711 }
712
713 uvm_obj_unwirepages(obj->filp, 0, obj->size);
714 }
715 #else
716 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
717 bool dirty, bool accessed)
718 {
719 int i, npages;
720 struct address_space *mapping;
721 struct pagevec pvec;
722
723 mapping = file_inode(obj->filp)->i_mapping;
724 mapping_clear_unevictable(mapping);
725
726 /* We already BUG_ON() for non-page-aligned sizes in
727 * drm_gem_object_init(), so we should never hit this unless
728 * driver author is doing something really wrong:
729 */
730 WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
731
732 npages = obj->size >> PAGE_SHIFT;
733
734 pagevec_init(&pvec);
735 for (i = 0; i < npages; i++) {
736 if (!pages[i])
737 continue;
738
739 if (dirty)
740 set_page_dirty(pages[i]);
741
742 if (accessed)
743 mark_page_accessed(pages[i]);
744
745 /* Undo the reference we took when populating the table */
746 if (!pagevec_add(&pvec, pages[i]))
747 drm_gem_check_release_pagevec(&pvec);
748 }
749 if (pagevec_count(&pvec))
750 drm_gem_check_release_pagevec(&pvec);
751
752 kvfree(pages);
753 }
754 #endif
755 EXPORT_SYMBOL(drm_gem_put_pages);
756
757 static int objects_lookup(struct drm_file *filp, u32 *handle, int count,
758 struct drm_gem_object **objs)
759 {
760 int i, ret = 0;
761 struct drm_gem_object *obj;
762
763 spin_lock(&filp->table_lock);
764
765 for (i = 0; i < count; i++) {
766 /* Check if we currently have a reference on the object */
767 obj = idr_find(&filp->object_idr, handle[i]);
768 if (!obj) {
769 ret = -ENOENT;
770 break;
771 }
772 drm_gem_object_get(obj);
773 objs[i] = obj;
774 }
775 spin_unlock(&filp->table_lock);
776
777 return ret;
778 }
779
780 /**
781 * drm_gem_objects_lookup - look up GEM objects from an array of handles
782 * @filp: DRM file private date
783 * @bo_handles: user pointer to array of userspace handle
784 * @count: size of handle array
785 * @objs_out: returned pointer to array of drm_gem_object pointers
786 *
787 * Takes an array of userspace handles and returns a newly allocated array of
788 * GEM objects.
789 *
790 * For a single handle lookup, use drm_gem_object_lookup().
791 *
792 * Returns:
793 *
794 * @objs filled in with GEM object pointers. Returned GEM objects need to be
795 * released with drm_gem_object_put(). -ENOENT is returned on a lookup
796 * failure. 0 is returned on success.
797 *
798 */
799 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
800 int count, struct drm_gem_object ***objs_out)
801 {
802 int ret;
803 u32 *handles;
804 struct drm_gem_object **objs;
805
806 if (!count)
807 return 0;
808
809 objs = kvmalloc_array(count, sizeof(struct drm_gem_object *),
810 GFP_KERNEL | __GFP_ZERO);
811 if (!objs)
812 return -ENOMEM;
813
814 handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
815 if (!handles) {
816 ret = -ENOMEM;
817 goto out;
818 }
819
820 if (copy_from_user(handles, bo_handles, count * sizeof(u32))) {
821 ret = -EFAULT;
822 DRM_DEBUG("Failed to copy in GEM handles\n");
823 goto out;
824 }
825
826 ret = objects_lookup(filp, handles, count, objs);
827 *objs_out = objs;
828
829 out:
830 kvfree(handles);
831 return ret;
832
833 }
834 EXPORT_SYMBOL(drm_gem_objects_lookup);
835
836 /**
837 * drm_gem_object_lookup - look up a GEM object from its handle
838 * @filp: DRM file private date
839 * @handle: userspace handle
840 *
841 * Returns:
842 *
843 * A reference to the object named by the handle if such exists on @filp, NULL
844 * otherwise.
845 *
846 * If looking up an array of handles, use drm_gem_objects_lookup().
847 */
848 struct drm_gem_object *
849 drm_gem_object_lookup(struct drm_file *filp, u32 handle)
850 {
851 struct drm_gem_object *obj = NULL;
852
853 objects_lookup(filp, &handle, 1, &obj);
854 return obj;
855 }
856 EXPORT_SYMBOL(drm_gem_object_lookup);
857
858 /**
859 * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects
860 * shared and/or exclusive fences.
861 * @filep: DRM file private date
862 * @handle: userspace handle
863 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
864 * @timeout: timeout value in jiffies or zero to return immediately
865 *
866 * Returns:
867 *
868 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
869 * greater than 0 on success.
870 */
871 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
872 bool wait_all, unsigned long timeout)
873 {
874 long ret;
875 struct drm_gem_object *obj;
876
877 obj = drm_gem_object_lookup(filep, handle);
878 if (!obj) {
879 DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
880 return -EINVAL;
881 }
882
883 ret = dma_resv_wait_timeout_rcu(obj->resv, wait_all,
884 true, timeout);
885 if (ret == 0)
886 ret = -ETIME;
887 else if (ret > 0)
888 ret = 0;
889
890 drm_gem_object_put_unlocked(obj);
891
892 return ret;
893 }
894 EXPORT_SYMBOL(drm_gem_dma_resv_wait);
895
896 /**
897 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
898 * @dev: drm_device
899 * @data: ioctl data
900 * @file_priv: drm file-private structure
901 *
902 * Releases the handle to an mm object.
903 */
904 int
905 drm_gem_close_ioctl(struct drm_device *dev, void *data,
906 struct drm_file *file_priv)
907 {
908 struct drm_gem_close *args = data;
909 int ret;
910
911 if (!drm_core_check_feature(dev, DRIVER_GEM))
912 return -EOPNOTSUPP;
913
914 ret = drm_gem_handle_delete(file_priv, args->handle);
915
916 return ret;
917 }
918
919 /**
920 * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
921 * @dev: drm_device
922 * @data: ioctl data
923 * @file_priv: drm file-private structure
924 *
925 * Create a global name for an object, returning the name.
926 *
927 * Note that the name does not hold a reference; when the object
928 * is freed, the name goes away.
929 */
930 int
931 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
932 struct drm_file *file_priv)
933 {
934 struct drm_gem_flink *args = data;
935 struct drm_gem_object *obj;
936 int ret;
937
938 if (!drm_core_check_feature(dev, DRIVER_GEM))
939 return -EOPNOTSUPP;
940
941 obj = drm_gem_object_lookup(file_priv, args->handle);
942 if (obj == NULL)
943 return -ENOENT;
944
945 idr_preload(GFP_KERNEL);
946 mutex_lock(&dev->object_name_lock);
947 /* prevent races with concurrent gem_close. */
948 if (obj->handle_count == 0) {
949 ret = -ENOENT;
950 goto err;
951 }
952
953 if (!obj->name) {
954 ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
955 if (ret < 0)
956 goto err;
957
958 obj->name = ret;
959 }
960
961 args->name = (uint64_t) obj->name;
962 ret = 0;
963
964 err:
965 mutex_unlock(&dev->object_name_lock);
966 idr_preload_end();
967 drm_gem_object_put_unlocked(obj);
968 return ret;
969 }
970
971 /**
972 * drm_gem_open - implementation of the GEM_OPEN ioctl
973 * @dev: drm_device
974 * @data: ioctl data
975 * @file_priv: drm file-private structure
976 *
977 * Open an object using the global name, returning a handle and the size.
978 *
979 * This handle (of course) holds a reference to the object, so the object
980 * will not go away until the handle is deleted.
981 */
982 int
983 drm_gem_open_ioctl(struct drm_device *dev, void *data,
984 struct drm_file *file_priv)
985 {
986 struct drm_gem_open *args = data;
987 struct drm_gem_object *obj;
988 int ret;
989 u32 handle;
990
991 if (!drm_core_check_feature(dev, DRIVER_GEM))
992 return -EOPNOTSUPP;
993
994 mutex_lock(&dev->object_name_lock);
995 obj = idr_find(&dev->object_name_idr, (int) args->name);
996 if (obj) {
997 drm_gem_object_get(obj);
998 } else {
999 mutex_unlock(&dev->object_name_lock);
1000 return -ENOENT;
1001 }
1002
1003 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
1004 ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
1005 drm_gem_object_put_unlocked(obj);
1006 if (ret)
1007 return ret;
1008
1009 args->handle = handle;
1010 args->size = obj->size;
1011
1012 return 0;
1013 }
1014
1015 /**
1016 * gem_gem_open - initalizes GEM file-private structures at devnode open time
1017 * @dev: drm_device which is being opened by userspace
1018 * @file_private: drm file-private structure to set up
1019 *
1020 * Called at device open time, sets up the structure for handling refcounting
1021 * of mm objects.
1022 */
1023 void
1024 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
1025 {
1026 idr_init_base(&file_private->object_idr, 1);
1027 spin_lock_init(&file_private->table_lock);
1028 }
1029
1030 /**
1031 * drm_gem_release - release file-private GEM resources
1032 * @dev: drm_device which is being closed by userspace
1033 * @file_private: drm file-private structure to clean up
1034 *
1035 * Called at close time when the filp is going away.
1036 *
1037 * Releases any remaining references on objects by this filp.
1038 */
1039 void
1040 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
1041 {
1042 idr_for_each(&file_private->object_idr,
1043 &drm_gem_object_release_handle, file_private);
1044 idr_destroy(&file_private->object_idr);
1045 #ifdef __NetBSD__
1046 spin_lock_destroy(&file_private->table_lock);
1047 #endif
1048 }
1049
1050 /**
1051 * drm_gem_object_release - release GEM buffer object resources
1052 * @obj: GEM buffer object
1053 *
1054 * This releases any structures and resources used by @obj and is the invers of
1055 * drm_gem_object_init().
1056 */
1057 void
1058 drm_gem_object_release(struct drm_gem_object *obj)
1059 {
1060 #ifndef __NetBSD__
1061 WARN_ON(obj->dma_buf);
1062 #endif
1063
1064 #ifdef __NetBSD__
1065 drm_vma_node_destroy(&obj->vma_node);
1066 if (obj->filp)
1067 uao_detach(obj->filp);
1068 uvm_obj_destroy(&obj->gemo_uvmobj, true);
1069 #else
1070 if (obj->filp)
1071 fput(obj->filp);
1072 #endif
1073
1074 dma_resv_fini(&obj->_resv);
1075 drm_gem_free_mmap_offset(obj);
1076 }
1077 EXPORT_SYMBOL(drm_gem_object_release);
1078
1079 /**
1080 * drm_gem_object_free - free a GEM object
1081 * @kref: kref of the object to free
1082 *
1083 * Called after the last reference to the object has been lost.
1084 * Must be called holding &drm_device.struct_mutex.
1085 *
1086 * Frees the object
1087 */
1088 void
1089 drm_gem_object_free(struct kref *kref)
1090 {
1091 struct drm_gem_object *obj =
1092 container_of(kref, struct drm_gem_object, refcount);
1093 struct drm_device *dev = obj->dev;
1094
1095 if (obj->funcs) {
1096 obj->funcs->free(obj);
1097 } else if (dev->driver->gem_free_object_unlocked) {
1098 dev->driver->gem_free_object_unlocked(obj);
1099 } else if (dev->driver->gem_free_object) {
1100 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1101
1102 dev->driver->gem_free_object(obj);
1103 }
1104 }
1105 EXPORT_SYMBOL(drm_gem_object_free);
1106
1107 /**
1108 * drm_gem_object_put_unlocked - drop a GEM buffer object reference
1109 * @obj: GEM buffer object
1110 *
1111 * This releases a reference to @obj. Callers must not hold the
1112 * &drm_device.struct_mutex lock when calling this function.
1113 *
1114 * See also __drm_gem_object_put().
1115 */
1116 void
1117 drm_gem_object_put_unlocked(struct drm_gem_object *obj)
1118 {
1119 struct drm_device *dev;
1120
1121 if (!obj)
1122 return;
1123
1124 dev = obj->dev;
1125
1126 if (dev->driver->gem_free_object) {
1127 might_lock(&dev->struct_mutex);
1128 if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
1129 &dev->struct_mutex))
1130 mutex_unlock(&dev->struct_mutex);
1131 } else {
1132 kref_put(&obj->refcount, drm_gem_object_free);
1133 }
1134 }
1135 EXPORT_SYMBOL(drm_gem_object_put_unlocked);
1136
1137 /**
1138 * drm_gem_object_put - release a GEM buffer object reference
1139 * @obj: GEM buffer object
1140 *
1141 * This releases a reference to @obj. Callers must hold the
1142 * &drm_device.struct_mutex lock when calling this function, even when the
1143 * driver doesn't use &drm_device.struct_mutex for anything.
1144 *
1145 * For drivers not encumbered with legacy locking use
1146 * drm_gem_object_put_unlocked() instead.
1147 */
1148 void
1149 drm_gem_object_put(struct drm_gem_object *obj)
1150 {
1151 if (obj) {
1152 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
1153
1154 kref_put(&obj->refcount, drm_gem_object_free);
1155 }
1156 }
1157 EXPORT_SYMBOL(drm_gem_object_put);
1158
1159 #ifndef __NetBSD__
1160 /**
1161 * drm_gem_vm_open - vma->ops->open implementation for GEM
1162 * @vma: VM area structure
1163 *
1164 * This function implements the #vm_operations_struct open() callback for GEM
1165 * drivers. This must be used together with drm_gem_vm_close().
1166 */
1167 void drm_gem_vm_open(struct vm_area_struct *vma)
1168 {
1169 struct drm_gem_object *obj = vma->vm_private_data;
1170
1171 drm_gem_object_get(obj);
1172 }
1173 EXPORT_SYMBOL(drm_gem_vm_open);
1174
1175 /**
1176 * drm_gem_vm_close - vma->ops->close implementation for GEM
1177 * @vma: VM area structure
1178 *
1179 * This function implements the #vm_operations_struct close() callback for GEM
1180 * drivers. This must be used together with drm_gem_vm_open().
1181 */
1182 void drm_gem_vm_close(struct vm_area_struct *vma)
1183 {
1184 struct drm_gem_object *obj = vma->vm_private_data;
1185
1186 drm_gem_object_put_unlocked(obj);
1187 }
1188 EXPORT_SYMBOL(drm_gem_vm_close);
1189
1190 /**
1191 * drm_gem_mmap_obj - memory map a GEM object
1192 * @obj: the GEM object to map
1193 * @obj_size: the object size to be mapped, in bytes
1194 * @vma: VMA for the area to be mapped
1195 *
1196 * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
1197 * provided by the driver. Depending on their requirements, drivers can either
1198 * provide a fault handler in their gem_vm_ops (in which case any accesses to
1199 * the object will be trapped, to perform migration, GTT binding, surface
1200 * register allocation, or performance monitoring), or mmap the buffer memory
1201 * synchronously after calling drm_gem_mmap_obj.
1202 *
1203 * This function is mainly intended to implement the DMABUF mmap operation, when
1204 * the GEM object is not looked up based on its fake offset. To implement the
1205 * DRM mmap operation, drivers should use the drm_gem_mmap() function.
1206 *
1207 * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
1208 * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
1209 * callers must verify access restrictions before calling this helper.
1210 *
1211 * Return 0 or success or -EINVAL if the object size is smaller than the VMA
1212 * size, or if no gem_vm_ops are provided.
1213 */
1214 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
1215 struct vm_area_struct *vma)
1216 {
1217 struct drm_device *dev = obj->dev;
1218 int ret;
1219
1220 /* Check for valid size. */
1221 if (obj_size < vma->vm_end - vma->vm_start)
1222 return -EINVAL;
1223
1224 /* Take a ref for this mapping of the object, so that the fault
1225 * handler can dereference the mmap offset's pointer to the object.
1226 * This reference is cleaned up by the corresponding vm_close
1227 * (which should happen whether the vma was created by this call, or
1228 * by a vm_open due to mremap or partial unmap or whatever).
1229 */
1230 drm_gem_object_get(obj);
1231
1232 if (obj->funcs && obj->funcs->mmap) {
1233 ret = obj->funcs->mmap(obj, vma);
1234 if (ret) {
1235 drm_gem_object_put_unlocked(obj);
1236 return ret;
1237 }
1238 WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
1239 } else {
1240 if (obj->funcs && obj->funcs->vm_ops)
1241 vma->vm_ops = obj->funcs->vm_ops;
1242 else if (dev->driver->gem_vm_ops)
1243 vma->vm_ops = dev->driver->gem_vm_ops;
1244 else {
1245 drm_gem_object_put_unlocked(obj);
1246 return -EINVAL;
1247 }
1248
1249 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1250 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1251 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1252 }
1253
1254 vma->vm_private_data = obj;
1255
1256 return 0;
1257 }
1258 EXPORT_SYMBOL(drm_gem_mmap_obj);
1259
1260 /**
1261 * drm_gem_mmap - memory map routine for GEM objects
1262 * @filp: DRM file pointer
1263 * @vma: VMA for the area to be mapped
1264 *
1265 * If a driver supports GEM object mapping, mmap calls on the DRM file
1266 * descriptor will end up here.
1267 *
1268 * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
1269 * contain the fake offset we created when the GTT map ioctl was called on
1270 * the object) and map it with a call to drm_gem_mmap_obj().
1271 *
1272 * If the caller is not granted access to the buffer object, the mmap will fail
1273 * with EACCES. Please see the vma manager for more information.
1274 */
1275 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
1276 {
1277 struct drm_file *priv = filp->private_data;
1278 struct drm_device *dev = priv->minor->dev;
1279 struct drm_gem_object *obj = NULL;
1280 struct drm_vma_offset_node *node;
1281 int ret;
1282
1283 if (drm_dev_is_unplugged(dev))
1284 return -ENODEV;
1285
1286 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
1287 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1288 vma->vm_pgoff,
1289 vma_pages(vma));
1290 if (likely(node)) {
1291 obj = container_of(node, struct drm_gem_object, vma_node);
1292 /*
1293 * When the object is being freed, after it hits 0-refcnt it
1294 * proceeds to tear down the object. In the process it will
1295 * attempt to remove the VMA offset and so acquire this
1296 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
1297 * that matches our range, we know it is in the process of being
1298 * destroyed and will be freed as soon as we release the lock -
1299 * so we have to check for the 0-refcnted object and treat it as
1300 * invalid.
1301 */
1302 if (!kref_get_unless_zero(&obj->refcount))
1303 obj = NULL;
1304 }
1305 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
1306
1307 if (!obj)
1308 return -EINVAL;
1309
1310 if (!drm_vma_node_is_allowed(node, priv)) {
1311 drm_gem_object_put_unlocked(obj);
1312 return -EACCES;
1313 }
1314
1315 if (node->readonly) {
1316 if (vma->vm_flags & VM_WRITE) {
1317 drm_gem_object_put_unlocked(obj);
1318 return -EINVAL;
1319 }
1320
1321 vma->vm_flags &= ~VM_MAYWRITE;
1322 }
1323
1324 ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
1325 vma);
1326
1327 drm_gem_object_put_unlocked(obj);
1328
1329 return ret;
1330 }
1331 EXPORT_SYMBOL(drm_gem_mmap);
1332 #endif /* defined(__NetBSD__) */
1333
1334 void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
1335 const struct drm_gem_object *obj)
1336 {
1337 drm_printf_indent(p, indent, "name=%d\n", obj->name);
1338 drm_printf_indent(p, indent, "refcount=%u\n",
1339 kref_read(&obj->refcount));
1340 drm_printf_indent(p, indent, "start=%08lx\n",
1341 drm_vma_node_start(&obj->vma_node));
1342 drm_printf_indent(p, indent, "size=%zu\n", obj->size);
1343 drm_printf_indent(p, indent, "imported=%s\n",
1344 obj->import_attach ? "yes" : "no");
1345
1346 if (obj->funcs && obj->funcs->print_info)
1347 obj->funcs->print_info(p, indent, obj);
1348 else if (obj->dev->driver->gem_print_info)
1349 obj->dev->driver->gem_print_info(p, indent, obj);
1350 }
1351
1352 int drm_gem_pin(struct drm_gem_object *obj)
1353 {
1354 if (obj->funcs && obj->funcs->pin)
1355 return obj->funcs->pin(obj);
1356 else if (obj->dev->driver->gem_prime_pin)
1357 return obj->dev->driver->gem_prime_pin(obj);
1358 else
1359 return 0;
1360 }
1361
1362 void drm_gem_unpin(struct drm_gem_object *obj)
1363 {
1364 if (obj->funcs && obj->funcs->unpin)
1365 obj->funcs->unpin(obj);
1366 else if (obj->dev->driver->gem_prime_unpin)
1367 obj->dev->driver->gem_prime_unpin(obj);
1368 }
1369
1370 void *drm_gem_vmap(struct drm_gem_object *obj)
1371 {
1372 void *vaddr;
1373
1374 if (obj->funcs && obj->funcs->vmap)
1375 vaddr = obj->funcs->vmap(obj);
1376 else if (obj->dev->driver->gem_prime_vmap)
1377 vaddr = obj->dev->driver->gem_prime_vmap(obj);
1378 else
1379 vaddr = ERR_PTR(-EOPNOTSUPP);
1380
1381 if (!vaddr)
1382 vaddr = ERR_PTR(-ENOMEM);
1383
1384 return vaddr;
1385 }
1386
1387 void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr)
1388 {
1389 if (!vaddr)
1390 return;
1391
1392 if (obj->funcs && obj->funcs->vunmap)
1393 obj->funcs->vunmap(obj, vaddr);
1394 else if (obj->dev->driver->gem_prime_vunmap)
1395 obj->dev->driver->gem_prime_vunmap(obj, vaddr);
1396 }
1397
1398 /**
1399 * drm_gem_lock_reservations - Sets up the ww context and acquires
1400 * the lock on an array of GEM objects.
1401 *
1402 * Once you've locked your reservations, you'll want to set up space
1403 * for your shared fences (if applicable), submit your job, then
1404 * drm_gem_unlock_reservations().
1405 *
1406 * @objs: drm_gem_objects to lock
1407 * @count: Number of objects in @objs
1408 * @acquire_ctx: struct ww_acquire_ctx that will be initialized as
1409 * part of tracking this set of locked reservations.
1410 */
1411 int
1412 drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
1413 struct ww_acquire_ctx *acquire_ctx)
1414 {
1415 int contended = -1;
1416 int i, ret;
1417
1418 ww_acquire_init(acquire_ctx, &reservation_ww_class);
1419
1420 retry:
1421 if (contended != -1) {
1422 struct drm_gem_object *obj = objs[contended];
1423
1424 ret = dma_resv_lock_slow_interruptible(obj->resv,
1425 acquire_ctx);
1426 if (ret) {
1427 ww_acquire_done(acquire_ctx);
1428 return ret;
1429 }
1430 }
1431
1432 for (i = 0; i < count; i++) {
1433 if (i == contended)
1434 continue;
1435
1436 ret = dma_resv_lock_interruptible(objs[i]->resv,
1437 acquire_ctx);
1438 if (ret) {
1439 int j;
1440
1441 for (j = 0; j < i; j++)
1442 dma_resv_unlock(objs[j]->resv);
1443
1444 if (contended != -1 && contended >= i)
1445 dma_resv_unlock(objs[contended]->resv);
1446
1447 if (ret == -EDEADLK) {
1448 contended = i;
1449 goto retry;
1450 }
1451
1452 ww_acquire_done(acquire_ctx);
1453 return ret;
1454 }
1455 }
1456
1457 ww_acquire_done(acquire_ctx);
1458
1459 return 0;
1460 }
1461 EXPORT_SYMBOL(drm_gem_lock_reservations);
1462
1463 void
1464 drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
1465 struct ww_acquire_ctx *acquire_ctx)
1466 {
1467 int i;
1468
1469 for (i = 0; i < count; i++)
1470 dma_resv_unlock(objs[i]->resv);
1471
1472 ww_acquire_fini(acquire_ctx);
1473 }
1474 EXPORT_SYMBOL(drm_gem_unlock_reservations);
1475
1476 #ifndef __NetBSD__ /* XXX xarray */
1477
1478 /**
1479 * drm_gem_fence_array_add - Adds the fence to an array of fences to be
1480 * waited on, deduplicating fences from the same context.
1481 *
1482 * @fence_array: array of dma_fence * for the job to block on.
1483 * @fence: the dma_fence to add to the list of dependencies.
1484 *
1485 * Returns:
1486 * 0 on success, or an error on failing to expand the array.
1487 */
1488 int drm_gem_fence_array_add(struct xarray *fence_array,
1489 struct dma_fence *fence)
1490 {
1491 struct dma_fence *entry;
1492 unsigned long index;
1493 u32 id = 0;
1494 int ret;
1495
1496 if (!fence)
1497 return 0;
1498
1499 /* Deduplicate if we already depend on a fence from the same context.
1500 * This lets the size of the array of deps scale with the number of
1501 * engines involved, rather than the number of BOs.
1502 */
1503 xa_for_each(fence_array, index, entry) {
1504 if (entry->context != fence->context)
1505 continue;
1506
1507 if (dma_fence_is_later(fence, entry)) {
1508 dma_fence_put(entry);
1509 xa_store(fence_array, index, fence, GFP_KERNEL);
1510 } else {
1511 dma_fence_put(fence);
1512 }
1513 return 0;
1514 }
1515
1516 ret = xa_alloc(fence_array, &id, fence, xa_limit_32b, GFP_KERNEL);
1517 if (ret != 0)
1518 dma_fence_put(fence);
1519
1520 return ret;
1521 }
1522 EXPORT_SYMBOL(drm_gem_fence_array_add);
1523
1524 /**
1525 * drm_gem_fence_array_add_implicit - Adds the implicit dependencies tracked
1526 * in the GEM object's reservation object to an array of dma_fences for use in
1527 * scheduling a rendering job.
1528 *
1529 * This should be called after drm_gem_lock_reservations() on your array of
1530 * GEM objects used in the job but before updating the reservations with your
1531 * own fences.
1532 *
1533 * @fence_array: array of dma_fence * for the job to block on.
1534 * @obj: the gem object to add new dependencies from.
1535 * @write: whether the job might write the object (so we need to depend on
1536 * shared fences in the reservation object).
1537 */
1538 int drm_gem_fence_array_add_implicit(struct xarray *fence_array,
1539 struct drm_gem_object *obj,
1540 bool write)
1541 {
1542 int ret;
1543 struct dma_fence **fences;
1544 unsigned int i, fence_count;
1545
1546 if (!write) {
1547 struct dma_fence *fence =
1548 dma_resv_get_excl_rcu(obj->resv);
1549
1550 return drm_gem_fence_array_add(fence_array, fence);
1551 }
1552
1553 ret = dma_resv_get_fences_rcu(obj->resv, NULL,
1554 &fence_count, &fences);
1555 if (ret || !fence_count)
1556 return ret;
1557
1558 for (i = 0; i < fence_count; i++) {
1559 ret = drm_gem_fence_array_add(fence_array, fences[i]);
1560 if (ret)
1561 break;
1562 }
1563
1564 for (; i < fence_count; i++)
1565 dma_fence_put(fences[i]);
1566 kfree(fences);
1567 return ret;
1568 }
1569 EXPORT_SYMBOL(drm_gem_fence_array_add_implicit);
1570
1571 #endif
1572