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