drm_gem.c revision 1.13 1 /* $NetBSD: drm_gem.c,v 1.13 2020/02/14 04:35:19 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.13 2020/02/14 04:35:19 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/err.h>
45 #include <linux/export.h>
46 #include <asm/bug.h>
47 #include <drm/drmP.h>
48 #include <drm/drm_vma_manager.h>
49 #include <drm/drm_gem.h>
50 #include "drm_internal.h"
51
52 #ifdef __NetBSD__
53 #include <uvm/uvm_extern.h>
54 #include <linux/nbsd-namespace.h>
55 #endif
56
57 /** @file drm_gem.c
58 *
59 * This file provides some of the base ioctls and library routines for
60 * the graphics memory manager implemented by each device driver.
61 *
62 * Because various devices have different requirements in terms of
63 * synchronization and migration strategies, implementing that is left up to
64 * the driver, and all that the general API provides should be generic --
65 * allocating objects, reading/writing data with the cpu, freeing objects.
66 * Even there, platform-dependent optimizations for reading/writing data with
67 * the CPU mean we'll likely hook those out to driver-specific calls. However,
68 * the DRI2 implementation wants to have at least allocate/mmap be generic.
69 *
70 * The goal was to have swap-backed object allocation managed through
71 * struct file. However, file descriptors as handles to a struct file have
72 * two major failings:
73 * - Process limits prevent more than 1024 or so being used at a time by
74 * default.
75 * - Inability to allocate high fds will aggravate the X Server's select()
76 * handling, and likely that of many GL client applications as well.
77 *
78 * This led to a plan of using our own integer IDs (called handles, following
79 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
80 * ioctls. The objects themselves will still include the struct file so
81 * that we can transition to fds if the required kernel infrastructure shows
82 * up at a later date, and as our interface with shmfs for memory allocation.
83 */
84
85 /*
86 * We make up offsets for buffer objects so we can recognize them at
87 * mmap time.
88 */
89
90 /* pgoff in mmap is an unsigned long, so we need to make sure that
91 * the faked up offset will fit
92 */
93
94 #if BITS_PER_LONG == 64
95 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
96 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
97 #else
98 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
99 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
100 #endif
101
102 /**
103 * drm_gem_init - Initialize the GEM device fields
104 * @dev: drm_devic structure to initialize
105 */
106 int
107 drm_gem_init(struct drm_device *dev)
108 {
109 struct drm_vma_offset_manager *vma_offset_manager;
110
111 mutex_init(&dev->object_name_lock);
112 idr_init(&dev->object_name_idr);
113
114 vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
115 if (!vma_offset_manager) {
116 DRM_ERROR("out of memory\n");
117 return -ENOMEM;
118 }
119
120 dev->vma_offset_manager = vma_offset_manager;
121 drm_vma_offset_manager_init(vma_offset_manager,
122 DRM_FILE_PAGE_OFFSET_START,
123 DRM_FILE_PAGE_OFFSET_SIZE);
124
125 return 0;
126 }
127
128 void
129 drm_gem_destroy(struct drm_device *dev)
130 {
131
132 drm_vma_offset_manager_destroy(dev->vma_offset_manager);
133 kfree(dev->vma_offset_manager);
134 dev->vma_offset_manager = NULL;
135
136 idr_destroy(&dev->object_name_idr);
137 mutex_destroy(&dev->object_name_lock);
138 }
139
140 /**
141 * drm_gem_object_init - initialize an allocated shmem-backed GEM object
142 * @dev: drm_device the object should be initialized for
143 * @obj: drm_gem_object to initialize
144 * @size: object size
145 *
146 * Initialize an already allocated GEM object of the specified size with
147 * shmfs backing store.
148 */
149 int drm_gem_object_init(struct drm_device *dev,
150 struct drm_gem_object *obj, size_t size)
151 {
152 #ifndef __NetBSD__
153 struct file *filp;
154 #endif
155
156 drm_gem_private_object_init(dev, obj, size);
157
158 #ifdef __NetBSD__
159 /*
160 * A uao may not have size 0, but a gem object may. Allocate a
161 * spurious page so we needn't teach uao how to have size 0.
162 */
163 obj->filp = uao_create(MAX(size, PAGE_SIZE), 0);
164 /*
165 * XXX This is gross. We ought to do it the other way around:
166 * set the uao to have the main uvm object's lock. However,
167 * uvm_obj_setlock is not safe on uvm_aobjs.
168 */
169 mutex_obj_hold(obj->filp->vmobjlock);
170 uvm_obj_setlock(&obj->gemo_uvmobj, obj->filp->vmobjlock);
171 #else
172 filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
173 if (IS_ERR(filp))
174 return PTR_ERR(filp);
175
176 obj->filp = filp;
177 #endif
178
179 return 0;
180 }
181 EXPORT_SYMBOL(drm_gem_object_init);
182
183 /**
184 * drm_gem_private_object_init - initialize an allocated private GEM object
185 * @dev: drm_device the object should be initialized for
186 * @obj: drm_gem_object to initialize
187 * @size: object size
188 *
189 * Initialize an already allocated GEM object of the specified size with
190 * no GEM provided backing store. Instead the caller is responsible for
191 * backing the object and handling it.
192 */
193 void drm_gem_private_object_init(struct drm_device *dev,
194 struct drm_gem_object *obj, size_t size)
195 {
196 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
197
198 obj->dev = dev;
199 #ifdef __NetBSD__
200 obj->filp = NULL;
201 KASSERT(drm_core_check_feature(dev, DRIVER_GEM));
202 KASSERT(dev->driver->gem_uvm_ops != NULL);
203 uvm_obj_init(&obj->gemo_uvmobj, dev->driver->gem_uvm_ops, true, 1);
204 #else
205 obj->filp = NULL;
206 #endif
207
208 kref_init(&obj->refcount);
209 obj->handle_count = 0;
210 obj->size = size;
211 #ifdef __NetBSD__
212 drm_vma_node_init(&obj->vma_node);
213 #else
214 drm_vma_node_reset(&obj->vma_node);
215 #endif
216 }
217 EXPORT_SYMBOL(drm_gem_private_object_init);
218
219 static void
220 drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
221 {
222 /*
223 * Note: obj->dma_buf can't disappear as long as we still hold a
224 * handle reference in obj->handle_count.
225 */
226 mutex_lock(&filp->prime.lock);
227 if (obj->dma_buf) {
228 drm_prime_remove_buf_handle_locked(&filp->prime,
229 obj->dma_buf);
230 }
231 mutex_unlock(&filp->prime.lock);
232 }
233
234 /**
235 * drm_gem_object_handle_free - release resources bound to userspace handles
236 * @obj: GEM object to clean up.
237 *
238 * Called after the last handle to the object has been closed
239 *
240 * Removes any name for the object. Note that this must be
241 * called before drm_gem_object_free or we'll be touching
242 * freed memory
243 */
244 static void drm_gem_object_handle_free(struct drm_gem_object *obj)
245 {
246 struct drm_device *dev = obj->dev;
247
248 /* Remove any name for this object */
249 if (obj->name) {
250 idr_remove(&dev->object_name_idr, obj->name);
251 obj->name = 0;
252 }
253 }
254
255 static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
256 {
257 #ifndef __NetBSD__
258 /* Unbreak the reference cycle if we have an exported dma_buf. */
259 if (obj->dma_buf) {
260 dma_buf_put(obj->dma_buf);
261 obj->dma_buf = NULL;
262 }
263 #endif
264 }
265
266 static void
267 drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
268 {
269 if (WARN_ON(obj->handle_count == 0))
270 return;
271
272 /*
273 * Must bump handle count first as this may be the last
274 * ref, in which case the object would disappear before we
275 * checked for a name
276 */
277
278 mutex_lock(&obj->dev->object_name_lock);
279 if (--obj->handle_count == 0) {
280 drm_gem_object_handle_free(obj);
281 drm_gem_object_exported_dma_buf_free(obj);
282 }
283 mutex_unlock(&obj->dev->object_name_lock);
284
285 drm_gem_object_unreference_unlocked(obj);
286 }
287
288 /**
289 * drm_gem_handle_delete - deletes the given file-private handle
290 * @filp: drm file-private structure to use for the handle look up
291 * @handle: userspace handle to delete
292 *
293 * Removes the GEM handle from the @filp lookup table and if this is the last
294 * handle also cleans up linked resources like GEM names.
295 */
296 int
297 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
298 {
299 struct drm_device *dev;
300 struct drm_gem_object *obj;
301
302 /* This is gross. The idr system doesn't let us try a delete and
303 * return an error code. It just spews if you fail at deleting.
304 * So, we have to grab a lock around finding the object and then
305 * doing the delete on it and dropping the refcount, or the user
306 * could race us to double-decrement the refcount and cause a
307 * use-after-free later. Given the frequency of our handle lookups,
308 * we may want to use ida for number allocation and a hash table
309 * for the pointers, anyway.
310 */
311 spin_lock(&filp->table_lock);
312
313 /* Check if we currently have a reference on the object */
314 obj = idr_find(&filp->object_idr, handle);
315 if (obj == NULL) {
316 spin_unlock(&filp->table_lock);
317 return -EINVAL;
318 }
319 dev = obj->dev;
320
321 /* Release reference and decrement refcount. */
322 idr_remove(&filp->object_idr, handle);
323 spin_unlock(&filp->table_lock);
324
325 if (drm_core_check_feature(dev, DRIVER_PRIME))
326 drm_gem_remove_prime_handles(obj, filp);
327 drm_vma_node_revoke(&obj->vma_node, filp->filp);
328
329 if (dev->driver->gem_close_object)
330 dev->driver->gem_close_object(obj, filp);
331 drm_gem_object_handle_unreference_unlocked(obj);
332
333 return 0;
334 }
335 EXPORT_SYMBOL(drm_gem_handle_delete);
336
337 /**
338 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
339 * @file: drm file-private structure to remove the dumb handle from
340 * @dev: corresponding drm_device
341 * @handle: the dumb handle to remove
342 *
343 * This implements the ->dumb_destroy kms driver callback for drivers which use
344 * gem to manage their backing storage.
345 */
346 int drm_gem_dumb_destroy(struct drm_file *file,
347 struct drm_device *dev,
348 uint32_t handle)
349 {
350 return drm_gem_handle_delete(file, handle);
351 }
352 EXPORT_SYMBOL(drm_gem_dumb_destroy);
353
354 /**
355 * drm_gem_handle_create_tail - internal functions to create a handle
356 * @file_priv: drm file-private structure to register the handle for
357 * @obj: object to register
358 * @handlep: pointer to return the created handle to the caller
359 *
360 * This expects the dev->object_name_lock to be held already and will drop it
361 * before returning. Used to avoid races in establishing new handles when
362 * importing an object from either an flink name or a dma-buf.
363 */
364 int
365 drm_gem_handle_create_tail(struct drm_file *file_priv,
366 struct drm_gem_object *obj,
367 u32 *handlep)
368 {
369 struct drm_device *dev = obj->dev;
370 int ret;
371
372 WARN_ON(!mutex_is_locked(&dev->object_name_lock));
373
374 /*
375 * Get the user-visible handle using idr. Preload and perform
376 * allocation under our spinlock.
377 */
378 idr_preload(GFP_KERNEL);
379 spin_lock(&file_priv->table_lock);
380
381 ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
382 drm_gem_object_reference(obj);
383 obj->handle_count++;
384 spin_unlock(&file_priv->table_lock);
385 idr_preload_end();
386 mutex_unlock(&dev->object_name_lock);
387 if (ret < 0)
388 goto err_unref;
389
390 *handlep = ret;
391
392 ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
393 if (ret)
394 goto err_remove;
395
396 if (dev->driver->gem_open_object) {
397 ret = dev->driver->gem_open_object(obj, file_priv);
398 if (ret)
399 goto err_revoke;
400 }
401
402 return 0;
403
404 err_revoke:
405 drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
406 err_remove:
407 spin_lock(&file_priv->table_lock);
408 idr_remove(&file_priv->object_idr, *handlep);
409 spin_unlock(&file_priv->table_lock);
410 err_unref:
411 drm_gem_object_handle_unreference_unlocked(obj);
412 return ret;
413 }
414
415 /**
416 * drm_gem_handle_create - create a gem handle for an object
417 * @file_priv: drm file-private structure to register the handle for
418 * @obj: object to register
419 * @handlep: pionter to return the created handle to the caller
420 *
421 * Create a handle for this object. This adds a handle reference
422 * to the object, which includes a regular reference count. Callers
423 * will likely want to dereference the object afterwards.
424 */
425 int drm_gem_handle_create(struct drm_file *file_priv,
426 struct drm_gem_object *obj,
427 u32 *handlep)
428 {
429 mutex_lock(&obj->dev->object_name_lock);
430
431 return drm_gem_handle_create_tail(file_priv, obj, handlep);
432 }
433 EXPORT_SYMBOL(drm_gem_handle_create);
434
435
436 /**
437 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
438 * @obj: obj in question
439 *
440 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
441 */
442 void
443 drm_gem_free_mmap_offset(struct drm_gem_object *obj)
444 {
445 struct drm_device *dev = obj->dev;
446
447 drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
448 }
449 EXPORT_SYMBOL(drm_gem_free_mmap_offset);
450
451 /**
452 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
453 * @obj: obj in question
454 * @size: the virtual size
455 *
456 * GEM memory mapping works by handing back to userspace a fake mmap offset
457 * it can use in a subsequent mmap(2) call. The DRM core code then looks
458 * up the object based on the offset and sets up the various memory mapping
459 * structures.
460 *
461 * This routine allocates and attaches a fake offset for @obj, in cases where
462 * the virtual size differs from the physical size (ie. obj->size). Otherwise
463 * just use drm_gem_create_mmap_offset().
464 */
465 int
466 drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
467 {
468 struct drm_device *dev = obj->dev;
469
470 return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
471 size / PAGE_SIZE);
472 }
473 EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
474
475 /**
476 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
477 * @obj: obj in question
478 *
479 * GEM memory mapping works by handing back to userspace a fake mmap offset
480 * it can use in a subsequent mmap(2) call. The DRM core code then looks
481 * up the object based on the offset and sets up the various memory mapping
482 * structures.
483 *
484 * This routine allocates and attaches a fake offset for @obj.
485 */
486 int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
487 {
488 return drm_gem_create_mmap_offset_size(obj, obj->size);
489 }
490 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
491
492 /**
493 * drm_gem_get_pages - helper to allocate backing pages for a GEM object
494 * from shmem
495 * @obj: obj in question
496 *
497 * This reads the page-array of the shmem-backing storage of the given gem
498 * object. An array of pages is returned. If a page is not allocated or
499 * swapped-out, this will allocate/swap-in the required pages. Note that the
500 * whole object is covered by the page-array and pinned in memory.
501 *
502 * Use drm_gem_put_pages() to release the array and unpin all pages.
503 *
504 * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
505 * If you require other GFP-masks, you have to do those allocations yourself.
506 *
507 * Note that you are not allowed to change gfp-zones during runtime. That is,
508 * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
509 * set during initialization. If you have special zone constraints, set them
510 * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care
511 * to keep pages in the required zone during swap-in.
512 */
513 #ifdef __NetBSD__
514 struct page **
515 drm_gem_get_pages(struct drm_gem_object *obj)
516 {
517 struct pglist pglist;
518 struct vm_page *vm_page;
519 struct page **pages;
520 unsigned i;
521 int ret;
522
523 KASSERT((obj->size & (PAGE_SIZE - 1)) != 0);
524
525 pages = drm_malloc_ab(obj->size >> PAGE_SHIFT, sizeof(*pages));
526 if (pages == NULL) {
527 ret = -ENOMEM;
528 goto fail0;
529 }
530
531 TAILQ_INIT(&pglist);
532 /* XXX errno NetBSD->Linux */
533 ret = -uvm_obj_wirepages(obj->filp, 0, obj->size, &pglist);
534 if (ret)
535 goto fail1;
536
537 i = 0;
538 TAILQ_FOREACH(vm_page, &pglist, pageq.queue)
539 pages[i++] = container_of(vm_page, struct page, p_vmp);
540
541 return pages;
542
543 fail1: drm_free_large(pages);
544 fail0: return ERR_PTR(ret);
545 }
546 #else
547 struct page **drm_gem_get_pages(struct drm_gem_object *obj)
548 {
549 struct address_space *mapping;
550 struct page *p, **pages;
551 int i, npages;
552
553 /* This is the shared memory object that backs the GEM resource */
554 mapping = file_inode(obj->filp)->i_mapping;
555
556 /* We already BUG_ON() for non-page-aligned sizes in
557 * drm_gem_object_init(), so we should never hit this unless
558 * driver author is doing something really wrong:
559 */
560 WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
561
562 npages = obj->size >> PAGE_SHIFT;
563
564 pages = drm_malloc_ab(npages, sizeof(struct page *));
565 if (pages == NULL)
566 return ERR_PTR(-ENOMEM);
567
568 for (i = 0; i < npages; i++) {
569 p = shmem_read_mapping_page(mapping, i);
570 if (IS_ERR(p))
571 goto fail;
572 pages[i] = p;
573
574 /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
575 * correct region during swapin. Note that this requires
576 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
577 * so shmem can relocate pages during swapin if required.
578 */
579 BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
580 (page_to_pfn(p) >= 0x00100000UL));
581 }
582
583 return pages;
584
585 fail:
586 while (i--)
587 page_cache_release(pages[i]);
588
589 drm_free_large(pages);
590 return ERR_CAST(p);
591 }
592 #endif
593 EXPORT_SYMBOL(drm_gem_get_pages);
594
595 /**
596 * drm_gem_put_pages - helper to free backing pages for a GEM object
597 * @obj: obj in question
598 * @pages: pages to free
599 * @dirty: if true, pages will be marked as dirty
600 * @accessed: if true, the pages will be marked as accessed
601 */
602 #ifdef __NetBSD__
603 void
604 drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, bool dirty,
605 bool accessed __unused /* XXX */)
606 {
607 unsigned i;
608
609 for (i = 0; i < (obj->size >> PAGE_SHIFT); i++) {
610 if (dirty) {
611 mutex_enter(obj->filp->vmobjlock);
612 uvm_pagemarkdirty(&pages[i]->p_vmp,
613 UVM_PAGE_STATUS_DIRTY);
614 mutex_exit(obj->filp->vmobjlock);
615 }
616 }
617
618 uvm_obj_unwirepages(obj->filp, 0, obj->size);
619 }
620 #else
621 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
622 bool dirty, bool accessed)
623 {
624 int i, npages;
625
626 /* We already BUG_ON() for non-page-aligned sizes in
627 * drm_gem_object_init(), so we should never hit this unless
628 * driver author is doing something really wrong:
629 */
630 WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
631
632 npages = obj->size >> PAGE_SHIFT;
633
634 for (i = 0; i < npages; i++) {
635 if (dirty)
636 set_page_dirty(pages[i]);
637
638 if (accessed)
639 mark_page_accessed(pages[i]);
640
641 /* Undo the reference we took when populating the table */
642 page_cache_release(pages[i]);
643 }
644
645 drm_free_large(pages);
646 }
647 #endif
648 EXPORT_SYMBOL(drm_gem_put_pages);
649
650 /** Returns a reference to the object named by the handle. */
651 struct drm_gem_object *
652 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
653 u32 handle)
654 {
655 struct drm_gem_object *obj;
656
657 spin_lock(&filp->table_lock);
658
659 /* Check if we currently have a reference on the object */
660 obj = idr_find(&filp->object_idr, handle);
661 if (obj == NULL) {
662 spin_unlock(&filp->table_lock);
663 return NULL;
664 }
665
666 drm_gem_object_reference(obj);
667
668 spin_unlock(&filp->table_lock);
669
670 return obj;
671 }
672 EXPORT_SYMBOL(drm_gem_object_lookup);
673
674 /**
675 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
676 * @dev: drm_device
677 * @data: ioctl data
678 * @file_priv: drm file-private structure
679 *
680 * Releases the handle to an mm object.
681 */
682 int
683 drm_gem_close_ioctl(struct drm_device *dev, void *data,
684 struct drm_file *file_priv)
685 {
686 struct drm_gem_close *args = data;
687 int ret;
688
689 if (!drm_core_check_feature(dev, DRIVER_GEM))
690 return -ENODEV;
691
692 ret = drm_gem_handle_delete(file_priv, args->handle);
693
694 return ret;
695 }
696
697 /**
698 * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
699 * @dev: drm_device
700 * @data: ioctl data
701 * @file_priv: drm file-private structure
702 *
703 * Create a global name for an object, returning the name.
704 *
705 * Note that the name does not hold a reference; when the object
706 * is freed, the name goes away.
707 */
708 int
709 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
710 struct drm_file *file_priv)
711 {
712 struct drm_gem_flink *args = data;
713 struct drm_gem_object *obj;
714 int ret;
715
716 if (!drm_core_check_feature(dev, DRIVER_GEM))
717 return -ENODEV;
718
719 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
720 if (obj == NULL)
721 return -ENOENT;
722
723 idr_preload(GFP_KERNEL);
724 mutex_lock(&dev->object_name_lock);
725 /* prevent races with concurrent gem_close. */
726 if (obj->handle_count == 0) {
727 ret = -ENOENT;
728 goto err;
729 }
730
731 if (!obj->name) {
732 ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
733 if (ret < 0)
734 goto err;
735
736 obj->name = ret;
737 }
738
739 args->name = (uint64_t) obj->name;
740 ret = 0;
741
742 err:
743 mutex_unlock(&dev->object_name_lock);
744 idr_preload_end();
745 drm_gem_object_unreference_unlocked(obj);
746 return ret;
747 }
748
749 /**
750 * drm_gem_open - implementation of the GEM_OPEN ioctl
751 * @dev: drm_device
752 * @data: ioctl data
753 * @file_priv: drm file-private structure
754 *
755 * Open an object using the global name, returning a handle and the size.
756 *
757 * This handle (of course) holds a reference to the object, so the object
758 * will not go away until the handle is deleted.
759 */
760 int
761 drm_gem_open_ioctl(struct drm_device *dev, void *data,
762 struct drm_file *file_priv)
763 {
764 struct drm_gem_open *args = data;
765 struct drm_gem_object *obj;
766 int ret;
767 u32 handle;
768
769 if (!drm_core_check_feature(dev, DRIVER_GEM))
770 return -ENODEV;
771
772 mutex_lock(&dev->object_name_lock);
773 obj = idr_find(&dev->object_name_idr, (int) args->name);
774 if (obj) {
775 drm_gem_object_reference(obj);
776 } else {
777 mutex_unlock(&dev->object_name_lock);
778 return -ENOENT;
779 }
780
781 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
782 ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
783 drm_gem_object_unreference_unlocked(obj);
784 if (ret)
785 return ret;
786
787 args->handle = handle;
788 args->size = obj->size;
789
790 return 0;
791 }
792
793 /**
794 * gem_gem_open - initalizes GEM file-private structures at devnode open time
795 * @dev: drm_device which is being opened by userspace
796 * @file_private: drm file-private structure to set up
797 *
798 * Called at device open time, sets up the structure for handling refcounting
799 * of mm objects.
800 */
801 void
802 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
803 {
804 idr_init(&file_private->object_idr);
805 spin_lock_init(&file_private->table_lock);
806 }
807
808 /*
809 * Called at device close to release the file's
810 * handle references on objects.
811 */
812 static int
813 drm_gem_object_release_handle(int id, void *ptr, void *data)
814 {
815 struct drm_file *file_priv = data;
816 struct drm_gem_object *obj = ptr;
817 struct drm_device *dev = obj->dev;
818
819 if (dev->driver->gem_close_object)
820 dev->driver->gem_close_object(obj, file_priv);
821
822 if (drm_core_check_feature(dev, DRIVER_PRIME))
823 drm_gem_remove_prime_handles(obj, file_priv);
824 drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
825
826 drm_gem_object_handle_unreference_unlocked(obj);
827
828 return 0;
829 }
830
831 /**
832 * drm_gem_release - release file-private GEM resources
833 * @dev: drm_device which is being closed by userspace
834 * @file_private: drm file-private structure to clean up
835 *
836 * Called at close time when the filp is going away.
837 *
838 * Releases any remaining references on objects by this filp.
839 */
840 void
841 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
842 {
843 idr_for_each(&file_private->object_idr,
844 &drm_gem_object_release_handle, file_private);
845 idr_destroy(&file_private->object_idr);
846 #ifdef __NetBSD__
847 spin_lock_destroy(&file_private->table_lock);
848 #endif
849 }
850
851 void
852 drm_gem_object_release(struct drm_gem_object *obj)
853 {
854 #ifndef __NetBSD__
855 WARN_ON(obj->dma_buf);
856 #endif
857
858 #ifdef __NetBSD__
859 drm_vma_node_destroy(&obj->vma_node);
860 if (obj->filp)
861 uao_detach(obj->filp);
862 uvm_obj_destroy(&obj->gemo_uvmobj, true);
863 #else
864 if (obj->filp)
865 fput(obj->filp);
866 #endif
867
868 drm_gem_free_mmap_offset(obj);
869 }
870 EXPORT_SYMBOL(drm_gem_object_release);
871
872 /**
873 * drm_gem_object_free - free a GEM object
874 * @kref: kref of the object to free
875 *
876 * Called after the last reference to the object has been lost.
877 * Must be called holding struct_ mutex
878 *
879 * Frees the object
880 */
881 void
882 drm_gem_object_free(struct kref *kref)
883 {
884 struct drm_gem_object *obj =
885 container_of(kref, struct drm_gem_object, refcount);
886 struct drm_device *dev = obj->dev;
887
888 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
889
890 if (dev->driver->gem_free_object != NULL)
891 dev->driver->gem_free_object(obj);
892 }
893 EXPORT_SYMBOL(drm_gem_object_free);
894
895 #ifndef __NetBSD__
896
897 void drm_gem_vm_open(struct vm_area_struct *vma)
898 {
899 struct drm_gem_object *obj = vma->vm_private_data;
900
901 drm_gem_object_reference(obj);
902 }
903 EXPORT_SYMBOL(drm_gem_vm_open);
904
905 void drm_gem_vm_close(struct vm_area_struct *vma)
906 {
907 struct drm_gem_object *obj = vma->vm_private_data;
908
909 drm_gem_object_unreference_unlocked(obj);
910 }
911 EXPORT_SYMBOL(drm_gem_vm_close);
912
913 /**
914 * drm_gem_mmap_obj - memory map a GEM object
915 * @obj: the GEM object to map
916 * @obj_size: the object size to be mapped, in bytes
917 * @vma: VMA for the area to be mapped
918 *
919 * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
920 * provided by the driver. Depending on their requirements, drivers can either
921 * provide a fault handler in their gem_vm_ops (in which case any accesses to
922 * the object will be trapped, to perform migration, GTT binding, surface
923 * register allocation, or performance monitoring), or mmap the buffer memory
924 * synchronously after calling drm_gem_mmap_obj.
925 *
926 * This function is mainly intended to implement the DMABUF mmap operation, when
927 * the GEM object is not looked up based on its fake offset. To implement the
928 * DRM mmap operation, drivers should use the drm_gem_mmap() function.
929 *
930 * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
931 * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
932 * callers must verify access restrictions before calling this helper.
933 *
934 * Return 0 or success or -EINVAL if the object size is smaller than the VMA
935 * size, or if no gem_vm_ops are provided.
936 */
937 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
938 struct vm_area_struct *vma)
939 {
940 struct drm_device *dev = obj->dev;
941
942 /* Check for valid size. */
943 if (obj_size < vma->vm_end - vma->vm_start)
944 return -EINVAL;
945
946 if (!dev->driver->gem_vm_ops)
947 return -EINVAL;
948
949 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
950 vma->vm_ops = dev->driver->gem_vm_ops;
951 vma->vm_private_data = obj;
952 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
953
954 /* Take a ref for this mapping of the object, so that the fault
955 * handler can dereference the mmap offset's pointer to the object.
956 * This reference is cleaned up by the corresponding vm_close
957 * (which should happen whether the vma was created by this call, or
958 * by a vm_open due to mremap or partial unmap or whatever).
959 */
960 drm_gem_object_reference(obj);
961
962 return 0;
963 }
964 EXPORT_SYMBOL(drm_gem_mmap_obj);
965
966 /**
967 * drm_gem_mmap - memory map routine for GEM objects
968 * @filp: DRM file pointer
969 * @vma: VMA for the area to be mapped
970 *
971 * If a driver supports GEM object mapping, mmap calls on the DRM file
972 * descriptor will end up here.
973 *
974 * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
975 * contain the fake offset we created when the GTT map ioctl was called on
976 * the object) and map it with a call to drm_gem_mmap_obj().
977 *
978 * If the caller is not granted access to the buffer object, the mmap will fail
979 * with EACCES. Please see the vma manager for more information.
980 */
981 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
982 {
983 struct drm_file *priv = filp->private_data;
984 struct drm_device *dev = priv->minor->dev;
985 struct drm_gem_object *obj = NULL;
986 struct drm_vma_offset_node *node;
987 int ret;
988
989 if (drm_device_is_unplugged(dev))
990 return -ENODEV;
991
992 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
993 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
994 vma->vm_pgoff,
995 vma_pages(vma));
996 if (likely(node)) {
997 obj = container_of(node, struct drm_gem_object, vma_node);
998 /*
999 * When the object is being freed, after it hits 0-refcnt it
1000 * proceeds to tear down the object. In the process it will
1001 * attempt to remove the VMA offset and so acquire this
1002 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
1003 * that matches our range, we know it is in the process of being
1004 * destroyed and will be freed as soon as we release the lock -
1005 * so we have to check for the 0-refcnted object and treat it as
1006 * invalid.
1007 */
1008 if (!kref_get_unless_zero(&obj->refcount))
1009 obj = NULL;
1010 }
1011 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
1012
1013 if (!obj)
1014 return -EINVAL;
1015
1016 if (!drm_vma_node_is_allowed(node, filp)) {
1017 drm_gem_object_unreference_unlocked(obj);
1018 return -EACCES;
1019 }
1020
1021 ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
1022 vma);
1023
1024 drm_gem_object_unreference_unlocked(obj);
1025
1026 return ret;
1027 }
1028 EXPORT_SYMBOL(drm_gem_mmap);
1029
1030 #endif /* defined(__NetBSD__) */
1031