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