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