2 * Copyright © 2008 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
28 * Copyright (c) 2011 The FreeBSD Foundation
29 * All rights reserved.
31 * This software was developed by Konstantin Belousov under sponsorship from
32 * the FreeBSD Foundation.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/limits.h>
62 #include <sys/mutex.h>
66 #include <vm/vm_page.h>
68 #include <linux/module.h>
73 * This file provides some of the base ioctls and library routines for
74 * the graphics memory manager implemented by each device driver.
76 * Because various devices have different requirements in terms of
77 * synchronization and migration strategies, implementing that is left up to
78 * the driver, and all that the general API provides should be generic --
79 * allocating objects, reading/writing data with the cpu, freeing objects.
80 * Even there, platform-dependent optimizations for reading/writing data with
81 * the CPU mean we'll likely hook those out to driver-specific calls. However,
82 * the DRI2 implementation wants to have at least allocate/mmap be generic.
84 * The goal was to have swap-backed object allocation managed through
85 * struct file. However, file descriptors as handles to a struct file have
87 * - Process limits prevent more than 1024 or so being used at a time by
89 * - Inability to allocate high fds will aggravate the X Server's select()
90 * handling, and likely that of many GL client applications as well.
92 * This led to a plan of using our own integer IDs (called handles, following
93 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
94 * ioctls. The objects themselves will still include the struct file so
95 * that we can transition to fds if the required kernel infrastructure shows
96 * up at a later date, and as our interface with shmfs for memory allocation.
100 * We make up offsets for buffer objects so we can recognize them at
104 /* pgoff in mmap is an unsigned long, so we need to make sure that
105 * the faked up offset will fit
108 #if BITS_PER_LONG == 64
109 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
110 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
112 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
113 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
117 * Initialize the GEM device fields
121 drm_gem_init(struct drm_device *dev)
123 struct drm_gem_mm *mm;
125 lockinit(&dev->object_name_lock, "objnam", 0, LK_CANRECURSE);
126 idr_init(&dev->object_name_idr);
128 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
130 DRM_ERROR("out of memory\n");
134 dev->mm_private = mm;
136 if (drm_ht_create(&mm->offset_hash, 12)) {
141 mm->idxunr = new_unrhdr(0, DRM_GEM_MAX_IDX, NULL);
142 drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
143 DRM_FILE_PAGE_OFFSET_SIZE);
148 drm_gem_destroy(struct drm_device *dev)
150 struct drm_gem_mm *mm = dev->mm_private;
152 drm_mm_takedown(&mm->offset_manager);
153 drm_ht_remove(&mm->offset_hash);
154 delete_unrhdr(mm->idxunr);
156 dev->mm_private = NULL;
160 * Initialize an already allocated GEM object of the specified size with
161 * shmfs backing store.
163 int drm_gem_object_init(struct drm_device *dev,
164 struct drm_gem_object *obj, size_t size)
166 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
169 obj->vm_obj = default_pager_alloc(NULL, size,
170 VM_PROT_READ | VM_PROT_WRITE, 0);
172 kref_init(&obj->refcount);
173 atomic_set(&obj->handle_count, 0);
178 EXPORT_SYMBOL(drm_gem_object_init);
181 * Initialize an already allocated GEM object of the specified size with
182 * no GEM provided backing store. Instead the caller is responsible for
183 * backing the object and handling it.
185 int drm_gem_private_object_init(struct drm_device *dev,
186 struct drm_gem_object *obj, size_t size)
188 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
193 kref_init(&obj->refcount);
194 atomic_set(&obj->handle_count, 0);
199 EXPORT_SYMBOL(drm_gem_private_object_init);
202 * Allocate a GEM object of the specified size with shmfs backing store
204 struct drm_gem_object *
205 drm_gem_object_alloc(struct drm_device *dev, size_t size)
207 struct drm_gem_object *obj;
209 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
213 if (drm_gem_object_init(dev, obj, size) != 0)
216 if (dev->driver->gem_init_object != NULL &&
217 dev->driver->gem_init_object(obj) != 0)
221 vm_object_deallocate(obj->vm_obj);
226 EXPORT_SYMBOL(drm_gem_object_alloc);
229 drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
232 if (obj->import_attach) {
233 drm_prime_remove_buf_handle(&filp->prime,
234 obj->import_attach->dmabuf);
236 if (obj->export_dma_buf) {
237 drm_prime_remove_buf_handle(&filp->prime,
238 obj->export_dma_buf);
244 * Removes the mapping from handle to filp for this object.
247 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
249 struct drm_device *dev;
250 struct drm_gem_object *obj;
252 /* This is gross. The idr system doesn't let us try a delete and
253 * return an error code. It just spews if you fail at deleting.
254 * So, we have to grab a lock around finding the object and then
255 * doing the delete on it and dropping the refcount, or the user
256 * could race us to double-decrement the refcount and cause a
257 * use-after-free later. Given the frequency of our handle lookups,
258 * we may want to use ida for number allocation and a hash table
259 * for the pointers, anyway.
261 lockmgr(&filp->table_lock, LK_EXCLUSIVE);
263 /* Check if we currently have a reference on the object */
264 obj = idr_find(&filp->object_idr, handle);
266 lockmgr(&filp->table_lock, LK_RELEASE);
271 /* Release reference and decrement refcount. */
272 idr_remove(&filp->object_idr, handle);
273 lockmgr(&filp->table_lock, LK_RELEASE);
275 drm_gem_remove_prime_handles(obj, filp);
277 if (dev->driver->gem_close_object)
278 dev->driver->gem_close_object(obj, filp);
279 drm_gem_object_handle_unreference_unlocked(obj);
283 EXPORT_SYMBOL(drm_gem_handle_delete);
286 * Create a handle for this object. This adds a handle reference
287 * to the object, which includes a regular reference count. Callers
288 * will likely want to dereference the object afterwards.
291 drm_gem_handle_create(struct drm_file *file_priv,
292 struct drm_gem_object *obj,
295 struct drm_device *dev = obj->dev;
299 * Get the user-visible handle using idr.
302 /* ensure there is space available to allocate a handle */
303 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
306 /* do the allocation under our spinlock */
307 lockmgr(&file_priv->table_lock, LK_EXCLUSIVE);
308 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
309 lockmgr(&file_priv->table_lock, LK_RELEASE);
315 drm_gem_object_handle_reference(obj);
317 if (dev->driver->gem_open_object) {
318 ret = dev->driver->gem_open_object(obj, file_priv);
320 drm_gem_handle_delete(file_priv, *handlep);
327 EXPORT_SYMBOL(drm_gem_handle_create);
331 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
332 * @obj: obj in question
334 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
337 drm_gem_free_mmap_offset(struct drm_gem_object *obj)
339 struct drm_device *dev = obj->dev;
340 struct drm_gem_mm *mm = dev->mm_private;
341 struct drm_hash_item *list;
345 list = &obj->map_list;
347 drm_ht_remove_item(&mm->offset_hash, list);
348 free_unr(mm->idxunr, list->key);
351 EXPORT_SYMBOL(drm_gem_free_mmap_offset);
354 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
355 * @obj: obj in question
357 * GEM memory mapping works by handing back to userspace a fake mmap offset
358 * it can use in a subsequent mmap(2) call. The DRM core code then looks
359 * up the object based on the offset and sets up the various memory mapping
362 * This routine allocates and attaches a fake offset for @obj.
365 drm_gem_create_mmap_offset(struct drm_gem_object *obj)
367 struct drm_device *dev = obj->dev;
368 struct drm_gem_mm *mm = dev->mm_private;
375 obj->map_list.key = alloc_unr(mm->idxunr);
376 ret = drm_ht_insert_item(&mm->offset_hash, &obj->map_list);
378 DRM_ERROR("failed to add to map hash\n");
379 free_unr(mm->idxunr, obj->map_list.key);
385 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
387 /** Returns a reference to the object named by the handle. */
388 struct drm_gem_object *
389 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
392 struct drm_gem_object *obj;
394 lockmgr(&filp->table_lock, LK_EXCLUSIVE);
396 /* Check if we currently have a reference on the object */
397 obj = idr_find(&filp->object_idr, handle);
399 lockmgr(&filp->table_lock, LK_RELEASE);
403 drm_gem_object_reference(obj);
405 lockmgr(&filp->table_lock, LK_RELEASE);
409 EXPORT_SYMBOL(drm_gem_object_lookup);
412 * Releases the handle to an mm object.
415 drm_gem_close_ioctl(struct drm_device *dev, void *data,
416 struct drm_file *file_priv)
418 struct drm_gem_close *args = data;
420 if (!drm_core_check_feature(dev, DRIVER_GEM))
423 return (drm_gem_handle_delete(file_priv, args->handle));
427 * Create a global name for an object, returning the name.
429 * Note that the name does not hold a reference; when the object
430 * is freed, the name goes away.
433 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
434 struct drm_file *file_priv)
436 struct drm_gem_flink *args = data;
437 struct drm_gem_object *obj;
440 if (!drm_core_check_feature(dev, DRIVER_GEM))
443 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
448 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
453 lockmgr(&dev->object_name_lock, LK_EXCLUSIVE);
455 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
457 args->name = (uint64_t) obj->name;
458 lockmgr(&dev->object_name_lock, LK_RELEASE);
465 /* Allocate a reference for the name table. */
466 drm_gem_object_reference(obj);
468 args->name = (uint64_t) obj->name;
469 lockmgr(&dev->object_name_lock, LK_RELEASE);
474 drm_gem_object_unreference_unlocked(obj);
479 * Open an object using the global name, returning a handle and the size.
481 * This handle (of course) holds a reference to the object, so the object
482 * will not go away until the handle is deleted.
485 drm_gem_open_ioctl(struct drm_device *dev, void *data,
486 struct drm_file *file_priv)
488 struct drm_gem_open *args = data;
489 struct drm_gem_object *obj;
493 if (!(dev->driver->driver_features & DRIVER_GEM))
496 lockmgr(&dev->object_name_lock, LK_EXCLUSIVE);
497 obj = idr_find(&dev->object_name_idr, (int) args->name);
499 drm_gem_object_reference(obj);
500 lockmgr(&dev->object_name_lock, LK_RELEASE);
504 ret = drm_gem_handle_create(file_priv, obj, &handle);
505 drm_gem_object_unreference_unlocked(obj);
509 args->handle = handle;
510 args->size = obj->size;
516 * Called at device open time, sets up the structure for handling refcounting
520 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
522 idr_init(&file_private->object_idr);
523 lockinit(&file_private->table_lock, "fptab", 0, LK_CANRECURSE);
527 * Called at device close to release the file's
528 * handle references on objects.
531 drm_gem_object_release_handle(int id, void *ptr, void *data)
533 struct drm_file *file_priv = data;
534 struct drm_gem_object *obj = ptr;
535 struct drm_device *dev = obj->dev;
537 drm_gem_remove_prime_handles(obj, file_priv);
539 if (dev->driver->gem_close_object)
540 dev->driver->gem_close_object(obj, file_priv);
542 drm_gem_object_handle_unreference_unlocked(obj);
548 * Called at close time when the filp is going away.
550 * Releases any remaining references on objects by this filp.
553 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
555 idr_for_each(&file_private->object_idr,
556 &drm_gem_object_release_handle, file_private);
557 idr_destroy(&file_private->object_idr);
561 drm_gem_object_release(struct drm_gem_object *obj)
565 * obj->vm_obj can be NULL for private gem objects.
567 vm_object_deallocate(obj->vm_obj);
569 EXPORT_SYMBOL(drm_gem_object_release);
572 * Called after the last reference to the object has been lost.
573 * Must be called holding struct_ mutex
578 drm_gem_object_free(struct kref *kref)
580 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
581 struct drm_device *dev = obj->dev;
583 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
585 if (dev->driver->gem_free_object != NULL)
586 dev->driver->gem_free_object(obj);
588 EXPORT_SYMBOL(drm_gem_object_free);
590 static void drm_gem_object_ref_bug(struct kref *list_kref)
596 * Called after the last handle to the object has been closed
598 * Removes any name for the object. Note that this must be
599 * called before drm_gem_object_free or we'll be touching
602 void drm_gem_object_handle_free(struct drm_gem_object *obj)
604 struct drm_device *dev = obj->dev;
606 /* Remove any name for this object */
607 lockmgr(&dev->object_name_lock, LK_EXCLUSIVE);
609 idr_remove(&dev->object_name_idr, obj->name);
611 lockmgr(&dev->object_name_lock, LK_RELEASE);
613 * The object name held a reference to this object, drop
616 * This cannot be the last reference, since the handle holds one too.
618 kref_put(&obj->refcount, drm_gem_object_ref_bug);
620 lockmgr(&dev->object_name_lock, LK_RELEASE);
623 EXPORT_SYMBOL(drm_gem_object_handle_free);
625 static struct drm_gem_object *
626 drm_gem_object_from_offset(struct drm_device *dev, vm_ooffset_t offset)
628 struct drm_gem_object *obj;
629 struct drm_gem_mm *mm;
630 struct drm_hash_item *map_list;
632 if ((offset & DRM_GEM_MAPPING_MASK) != DRM_GEM_MAPPING_KEY)
634 offset &= ~DRM_GEM_MAPPING_KEY;
635 mm = dev->mm_private;
636 if (drm_ht_find_item(&mm->offset_hash, DRM_GEM_MAPPING_IDX(offset),
638 DRM_DEBUG("drm_gem_object_from_offset: offset 0x%jx obj not found\n",
642 obj = container_of(map_list, struct drm_gem_object, map_list);
647 drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset, vm_size_t size,
648 struct vm_object **obj_res, int nprot)
650 struct drm_gem_object *gem_obj;
651 struct vm_object *vm_obj;
654 gem_obj = drm_gem_object_from_offset(dev, *offset);
655 if (gem_obj == NULL) {
659 drm_gem_object_reference(gem_obj);
661 vm_obj = cdev_pager_allocate(gem_obj, OBJT_MGTDEVICE,
662 dev->driver->gem_pager_ops, size, nprot,
663 DRM_GEM_MAPPING_MAPOFF(*offset), curthread->td_ucred);
664 if (vm_obj == NULL) {
665 drm_gem_object_unreference_unlocked(gem_obj);
668 *offset = DRM_GEM_MAPPING_MAPOFF(*offset);
674 drm_gem_pager_dtr(void *handle)
676 struct drm_gem_object *obj;
677 struct drm_device *dev;
683 drm_gem_free_mmap_offset(obj);
684 drm_gem_object_unreference(obj);