drm_gem.c revision 1.1.1.1.2.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
44 /** @file drm_gem.c
45 *
46 * This file provides some of the base ioctls and library routines for
47 * the graphics memory manager implemented by each device driver.
48 *
49 * Because various devices have different requirements in terms of
50 * synchronization and migration strategies, implementing that is left up to
51 * the driver, and all that the general API provides should be generic --
52 * allocating objects, reading/writing data with the cpu, freeing objects.
53 * Even there, platform-dependent optimizations for reading/writing data with
54 * the CPU mean we'll likely hook those out to driver-specific calls. However,
55 * the DRI2 implementation wants to have at least allocate/mmap be generic.
56 *
57 * The goal was to have swap-backed object allocation managed through
58 * struct file. However, file descriptors as handles to a struct file have
59 * two major failings:
60 * - Process limits prevent more than 1024 or so being used at a time by
61 * default.
62 * - Inability to allocate high fds will aggravate the X Server's select()
63 * handling, and likely that of many GL client applications as well.
64 *
65 * This led to a plan of using our own integer IDs (called handles, following
66 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
67 * ioctls. The objects themselves will still include the struct file so
68 * that we can transition to fds if the required kernel infrastructure shows
69 * up at a later date, and as our interface with shmfs for memory allocation.
70 */
71
72 /*
73 * We make up offsets for buffer objects so we can recognize them at
74 * mmap time.
75 */
76
77 /* pgoff in mmap is an unsigned long, so we need to make sure that
78 * the faked up offset will fit
79 */
80
81 #if BITS_PER_LONG == 64
82 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
83 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
84 #else
85 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
86 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
87 #endif
88
89 /**
90 * Initialize the GEM device fields
91 */
92
93 int
94 drm_gem_init(struct drm_device *dev)
95 {
96 struct drm_gem_mm *mm;
97
98 spin_lock_init(&dev->object_name_lock);
99 idr_init(&dev->object_name_idr);
100
101 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
102 if (!mm) {
103 DRM_ERROR("out of memory\n");
104 return -ENOMEM;
105 }
106
107 dev->mm_private = mm;
108
109 if (drm_ht_create(&mm->offset_hash, 12)) {
110 kfree(mm);
111 return -ENOMEM;
112 }
113
114 if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
115 DRM_FILE_PAGE_OFFSET_SIZE)) {
116 drm_ht_remove(&mm->offset_hash);
117 kfree(mm);
118 return -ENOMEM;
119 }
120
121 return 0;
122 }
123
124 void
125 drm_gem_destroy(struct drm_device *dev)
126 {
127 struct drm_gem_mm *mm = dev->mm_private;
128
129 drm_mm_takedown(&mm->offset_manager);
130 drm_ht_remove(&mm->offset_hash);
131 kfree(mm);
132 dev->mm_private = NULL;
133 }
134
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 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
143
144 obj->dev = dev;
145 obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
146 if (IS_ERR(obj->filp))
147 return PTR_ERR(obj->filp);
148
149 kref_init(&obj->refcount);
150 atomic_set(&obj->handle_count, 0);
151 obj->size = size;
152
153 return 0;
154 }
155 EXPORT_SYMBOL(drm_gem_object_init);
156
157 /**
158 * Initialize an already allocated GEM object of the specified size with
159 * no GEM provided backing store. Instead the caller is responsible for
160 * backing the object and handling it.
161 */
162 int drm_gem_private_object_init(struct drm_device *dev,
163 struct drm_gem_object *obj, size_t size)
164 {
165 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
166
167 obj->dev = dev;
168 obj->filp = NULL;
169
170 kref_init(&obj->refcount);
171 atomic_set(&obj->handle_count, 0);
172 obj->size = size;
173
174 return 0;
175 }
176 EXPORT_SYMBOL(drm_gem_private_object_init);
177
178 /**
179 * Allocate a GEM object of the specified size with shmfs backing store
180 */
181 struct drm_gem_object *
182 drm_gem_object_alloc(struct drm_device *dev, size_t size)
183 {
184 struct drm_gem_object *obj;
185
186 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
187 if (!obj)
188 goto free;
189
190 if (drm_gem_object_init(dev, obj, size) != 0)
191 goto free;
192
193 if (dev->driver->gem_init_object != NULL &&
194 dev->driver->gem_init_object(obj) != 0) {
195 goto fput;
196 }
197 return obj;
198 fput:
199 /* Object_init mangles the global counters - readjust them. */
200 fput(obj->filp);
201 free:
202 kfree(obj);
203 return NULL;
204 }
205 EXPORT_SYMBOL(drm_gem_object_alloc);
206
207 static void
208 drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
209 {
210 if (obj->import_attach) {
211 drm_prime_remove_imported_buf_handle(&filp->prime,
212 obj->import_attach->dmabuf);
213 }
214 if (obj->export_dma_buf) {
215 drm_prime_remove_imported_buf_handle(&filp->prime,
216 obj->export_dma_buf);
217 }
218 }
219
220 /**
221 * Removes the mapping from handle to filp for this object.
222 */
223 int
224 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
225 {
226 struct drm_device *dev;
227 struct drm_gem_object *obj;
228
229 /* This is gross. The idr system doesn't let us try a delete and
230 * return an error code. It just spews if you fail at deleting.
231 * So, we have to grab a lock around finding the object and then
232 * doing the delete on it and dropping the refcount, or the user
233 * could race us to double-decrement the refcount and cause a
234 * use-after-free later. Given the frequency of our handle lookups,
235 * we may want to use ida for number allocation and a hash table
236 * for the pointers, anyway.
237 */
238 spin_lock(&filp->table_lock);
239
240 /* Check if we currently have a reference on the object */
241 obj = idr_find(&filp->object_idr, handle);
242 if (obj == NULL) {
243 spin_unlock(&filp->table_lock);
244 return -EINVAL;
245 }
246 dev = obj->dev;
247
248 /* Release reference and decrement refcount. */
249 idr_remove(&filp->object_idr, handle);
250 spin_unlock(&filp->table_lock);
251
252 drm_gem_remove_prime_handles(obj, filp);
253
254 if (dev->driver->gem_close_object)
255 dev->driver->gem_close_object(obj, filp);
256 drm_gem_object_handle_unreference_unlocked(obj);
257
258 return 0;
259 }
260 EXPORT_SYMBOL(drm_gem_handle_delete);
261
262 /**
263 * Create a handle for this object. This adds a handle reference
264 * to the object, which includes a regular reference count. Callers
265 * will likely want to dereference the object afterwards.
266 */
267 int
268 drm_gem_handle_create(struct drm_file *file_priv,
269 struct drm_gem_object *obj,
270 u32 *handlep)
271 {
272 struct drm_device *dev = obj->dev;
273 int ret;
274
275 /*
276 * Get the user-visible handle using idr.
277 */
278 again:
279 /* ensure there is space available to allocate a handle */
280 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
281 return -ENOMEM;
282
283 /* do the allocation under our spinlock */
284 spin_lock(&file_priv->table_lock);
285 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
286 spin_unlock(&file_priv->table_lock);
287 if (ret == -EAGAIN)
288 goto again;
289 else if (ret)
290 return ret;
291
292 drm_gem_object_handle_reference(obj);
293
294 if (dev->driver->gem_open_object) {
295 ret = dev->driver->gem_open_object(obj, file_priv);
296 if (ret) {
297 drm_gem_handle_delete(file_priv, *handlep);
298 return ret;
299 }
300 }
301
302 return 0;
303 }
304 EXPORT_SYMBOL(drm_gem_handle_create);
305
306
307 /**
308 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
309 * @obj: obj in question
310 *
311 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
312 */
313 void
314 drm_gem_free_mmap_offset(struct drm_gem_object *obj)
315 {
316 struct drm_device *dev = obj->dev;
317 struct drm_gem_mm *mm = dev->mm_private;
318 struct drm_map_list *list = &obj->map_list;
319
320 drm_ht_remove_item(&mm->offset_hash, &list->hash);
321 drm_mm_put_block(list->file_offset_node);
322 kfree(list->map);
323 list->map = NULL;
324 }
325 EXPORT_SYMBOL(drm_gem_free_mmap_offset);
326
327 /**
328 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
329 * @obj: obj in question
330 *
331 * GEM memory mapping works by handing back to userspace a fake mmap offset
332 * it can use in a subsequent mmap(2) call. The DRM core code then looks
333 * up the object based on the offset and sets up the various memory mapping
334 * structures.
335 *
336 * This routine allocates and attaches a fake offset for @obj.
337 */
338 int
339 drm_gem_create_mmap_offset(struct drm_gem_object *obj)
340 {
341 struct drm_device *dev = obj->dev;
342 struct drm_gem_mm *mm = dev->mm_private;
343 struct drm_map_list *list;
344 struct drm_local_map *map;
345 int ret;
346
347 /* Set the object up for mmap'ing */
348 list = &obj->map_list;
349 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
350 if (!list->map)
351 return -ENOMEM;
352
353 map = list->map;
354 map->type = _DRM_GEM;
355 map->size = obj->size;
356 map->handle = obj;
357
358 /* Get a DRM GEM mmap offset allocated... */
359 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
360 obj->size / PAGE_SIZE, 0, false);
361
362 if (!list->file_offset_node) {
363 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
364 ret = -ENOSPC;
365 goto out_free_list;
366 }
367
368 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
369 obj->size / PAGE_SIZE, 0);
370 if (!list->file_offset_node) {
371 ret = -ENOMEM;
372 goto out_free_list;
373 }
374
375 list->hash.key = list->file_offset_node->start;
376 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
377 if (ret) {
378 DRM_ERROR("failed to add to map hash\n");
379 goto out_free_mm;
380 }
381
382 return 0;
383
384 out_free_mm:
385 drm_mm_put_block(list->file_offset_node);
386 out_free_list:
387 kfree(list->map);
388 list->map = NULL;
389
390 return ret;
391 }
392 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
393
394 /** Returns a reference to the object named by the handle. */
395 struct drm_gem_object *
396 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
397 u32 handle)
398 {
399 struct drm_gem_object *obj;
400
401 spin_lock(&filp->table_lock);
402
403 /* Check if we currently have a reference on the object */
404 obj = idr_find(&filp->object_idr, handle);
405 if (obj == NULL) {
406 spin_unlock(&filp->table_lock);
407 return NULL;
408 }
409
410 drm_gem_object_reference(obj);
411
412 spin_unlock(&filp->table_lock);
413
414 return obj;
415 }
416 EXPORT_SYMBOL(drm_gem_object_lookup);
417
418 /**
419 * Releases the handle to an mm object.
420 */
421 int
422 drm_gem_close_ioctl(struct drm_device *dev, void *data,
423 struct drm_file *file_priv)
424 {
425 struct drm_gem_close *args = data;
426 int ret;
427
428 if (!(dev->driver->driver_features & DRIVER_GEM))
429 return -ENODEV;
430
431 ret = drm_gem_handle_delete(file_priv, args->handle);
432
433 return ret;
434 }
435
436 /**
437 * Create a global name for an object, returning the name.
438 *
439 * Note that the name does not hold a reference; when the object
440 * is freed, the name goes away.
441 */
442 int
443 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
444 struct drm_file *file_priv)
445 {
446 struct drm_gem_flink *args = data;
447 struct drm_gem_object *obj;
448 int ret;
449
450 if (!(dev->driver->driver_features & DRIVER_GEM))
451 return -ENODEV;
452
453 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
454 if (obj == NULL)
455 return -ENOENT;
456
457 again:
458 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
459 ret = -ENOMEM;
460 goto err;
461 }
462
463 spin_lock(&dev->object_name_lock);
464 if (!obj->name) {
465 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
466 &obj->name);
467 args->name = (uint64_t) obj->name;
468 spin_unlock(&dev->object_name_lock);
469
470 if (ret == -EAGAIN)
471 goto again;
472 else if (ret)
473 goto err;
474
475 /* Allocate a reference for the name table. */
476 drm_gem_object_reference(obj);
477 } else {
478 args->name = (uint64_t) obj->name;
479 spin_unlock(&dev->object_name_lock);
480 ret = 0;
481 }
482
483 err:
484 drm_gem_object_unreference_unlocked(obj);
485 return ret;
486 }
487
488 /**
489 * Open an object using the global name, returning a handle and the size.
490 *
491 * This handle (of course) holds a reference to the object, so the object
492 * will not go away until the handle is deleted.
493 */
494 int
495 drm_gem_open_ioctl(struct drm_device *dev, void *data,
496 struct drm_file *file_priv)
497 {
498 struct drm_gem_open *args = data;
499 struct drm_gem_object *obj;
500 int ret;
501 u32 handle;
502
503 if (!(dev->driver->driver_features & DRIVER_GEM))
504 return -ENODEV;
505
506 spin_lock(&dev->object_name_lock);
507 obj = idr_find(&dev->object_name_idr, (int) args->name);
508 if (obj)
509 drm_gem_object_reference(obj);
510 spin_unlock(&dev->object_name_lock);
511 if (!obj)
512 return -ENOENT;
513
514 ret = drm_gem_handle_create(file_priv, obj, &handle);
515 drm_gem_object_unreference_unlocked(obj);
516 if (ret)
517 return ret;
518
519 args->handle = handle;
520 args->size = obj->size;
521
522 return 0;
523 }
524
525 /**
526 * Called at device open time, sets up the structure for handling refcounting
527 * of mm objects.
528 */
529 void
530 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
531 {
532 idr_init(&file_private->object_idr);
533 spin_lock_init(&file_private->table_lock);
534 }
535
536 /**
537 * Called at device close to release the file's
538 * handle references on objects.
539 */
540 static int
541 drm_gem_object_release_handle(int id, void *ptr, void *data)
542 {
543 struct drm_file *file_priv = data;
544 struct drm_gem_object *obj = ptr;
545 struct drm_device *dev = obj->dev;
546
547 drm_gem_remove_prime_handles(obj, file_priv);
548
549 if (dev->driver->gem_close_object)
550 dev->driver->gem_close_object(obj, file_priv);
551
552 drm_gem_object_handle_unreference_unlocked(obj);
553
554 return 0;
555 }
556
557 /**
558 * Called at close time when the filp is going away.
559 *
560 * Releases any remaining references on objects by this filp.
561 */
562 void
563 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
564 {
565 idr_for_each(&file_private->object_idr,
566 &drm_gem_object_release_handle, file_private);
567
568 idr_remove_all(&file_private->object_idr);
569 idr_destroy(&file_private->object_idr);
570 }
571
572 void
573 drm_gem_object_release(struct drm_gem_object *obj)
574 {
575 if (obj->filp)
576 fput(obj->filp);
577 }
578 EXPORT_SYMBOL(drm_gem_object_release);
579
580 /**
581 * Called after the last reference to the object has been lost.
582 * Must be called holding struct_ mutex
583 *
584 * Frees the object
585 */
586 void
587 drm_gem_object_free(struct kref *kref)
588 {
589 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
590 struct drm_device *dev = obj->dev;
591
592 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
593
594 if (dev->driver->gem_free_object != NULL)
595 dev->driver->gem_free_object(obj);
596 }
597 EXPORT_SYMBOL(drm_gem_object_free);
598
599 static void drm_gem_object_ref_bug(struct kref *list_kref)
600 {
601 BUG();
602 }
603
604 /**
605 * Called after the last handle to the object has been closed
606 *
607 * Removes any name for the object. Note that this must be
608 * called before drm_gem_object_free or we'll be touching
609 * freed memory
610 */
611 void drm_gem_object_handle_free(struct drm_gem_object *obj)
612 {
613 struct drm_device *dev = obj->dev;
614
615 /* Remove any name for this object */
616 spin_lock(&dev->object_name_lock);
617 if (obj->name) {
618 idr_remove(&dev->object_name_idr, obj->name);
619 obj->name = 0;
620 spin_unlock(&dev->object_name_lock);
621 /*
622 * The object name held a reference to this object, drop
623 * that now.
624 *
625 * This cannot be the last reference, since the handle holds one too.
626 */
627 kref_put(&obj->refcount, drm_gem_object_ref_bug);
628 } else
629 spin_unlock(&dev->object_name_lock);
630
631 }
632 EXPORT_SYMBOL(drm_gem_object_handle_free);
633
634 void drm_gem_vm_open(struct vm_area_struct *vma)
635 {
636 struct drm_gem_object *obj = vma->vm_private_data;
637
638 drm_gem_object_reference(obj);
639
640 mutex_lock(&obj->dev->struct_mutex);
641 drm_vm_open_locked(obj->dev, vma);
642 mutex_unlock(&obj->dev->struct_mutex);
643 }
644 EXPORT_SYMBOL(drm_gem_vm_open);
645
646 void drm_gem_vm_close(struct vm_area_struct *vma)
647 {
648 struct drm_gem_object *obj = vma->vm_private_data;
649 struct drm_device *dev = obj->dev;
650
651 mutex_lock(&dev->struct_mutex);
652 drm_vm_close_locked(obj->dev, vma);
653 drm_gem_object_unreference(obj);
654 mutex_unlock(&dev->struct_mutex);
655 }
656 EXPORT_SYMBOL(drm_gem_vm_close);
657
658
659 /**
660 * drm_gem_mmap - memory map routine for GEM objects
661 * @filp: DRM file pointer
662 * @vma: VMA for the area to be mapped
663 *
664 * If a driver supports GEM object mapping, mmap calls on the DRM file
665 * descriptor will end up here.
666 *
667 * If we find the object based on the offset passed in (vma->vm_pgoff will
668 * contain the fake offset we created when the GTT map ioctl was called on
669 * the object), we set up the driver fault handler so that any accesses
670 * to the object can be trapped, to perform migration, GTT binding, surface
671 * register allocation, or performance monitoring.
672 */
673 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
674 {
675 struct drm_file *priv = filp->private_data;
676 struct drm_device *dev = priv->minor->dev;
677 struct drm_gem_mm *mm = dev->mm_private;
678 struct drm_local_map *map = NULL;
679 struct drm_gem_object *obj;
680 struct drm_hash_item *hash;
681 int ret = 0;
682
683 if (drm_device_is_unplugged(dev))
684 return -ENODEV;
685
686 mutex_lock(&dev->struct_mutex);
687
688 if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
689 mutex_unlock(&dev->struct_mutex);
690 return drm_mmap(filp, vma);
691 }
692
693 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
694 if (!map ||
695 ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
696 ret = -EPERM;
697 goto out_unlock;
698 }
699
700 /* Check for valid size. */
701 if (map->size < vma->vm_end - vma->vm_start) {
702 ret = -EINVAL;
703 goto out_unlock;
704 }
705
706 obj = map->handle;
707 if (!obj->dev->driver->gem_vm_ops) {
708 ret = -EINVAL;
709 goto out_unlock;
710 }
711
712 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
713 vma->vm_ops = obj->dev->driver->gem_vm_ops;
714 vma->vm_private_data = map->handle;
715 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
716
717 /* Take a ref for this mapping of the object, so that the fault
718 * handler can dereference the mmap offset's pointer to the object.
719 * This reference is cleaned up by the corresponding vm_close
720 * (which should happen whether the vma was created by this call, or
721 * by a vm_open due to mremap or partial unmap or whatever).
722 */
723 drm_gem_object_reference(obj);
724
725 drm_vm_open_locked(dev, vma);
726
727 out_unlock:
728 mutex_unlock(&dev->struct_mutex);
729
730 return ret;
731 }
732 EXPORT_SYMBOL(drm_gem_mmap);
733