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 #include <linux/types.h>
29 #include <linux/slab.h>
31 #include <linux/uaccess.h>
33 #include <linux/file.h>
34 #include <linux/module.h>
35 #include <linux/mman.h>
36 #include <linux/pagemap.h>
41 * This file provides some of the base ioctls and library routines for
42 * the graphics memory manager implemented by each device driver.
44 * Because various devices have different requirements in terms of
45 * synchronization and migration strategies, implementing that is left up to
46 * the driver, and all that the general API provides should be generic --
47 * allocating objects, reading/writing data with the cpu, freeing objects.
48 * Even there, platform-dependent optimizations for reading/writing data with
49 * the CPU mean we'll likely hook those out to driver-specific calls. However,
50 * the DRI2 implementation wants to have at least allocate/mmap be generic.
52 * The goal was to have swap-backed object allocation managed through
53 * struct file. However, file descriptors as handles to a struct file have
55 * - Process limits prevent more than 1024 or so being used at a time by
57 * - Inability to allocate high fds will aggravate the X Server's select()
58 * handling, and likely that of many GL client applications as well.
60 * This led to a plan of using our own integer IDs (called handles, following
61 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
62 * ioctls. The objects themselves will still include the struct file so
63 * that we can transition to fds if the required kernel infrastructure shows
64 * up at a later date, and as our interface with shmfs for memory allocation.
68 * We make up offsets for buffer objects so we can recognize them at
72 /* pgoff in mmap is an unsigned long, so we need to make sure that
73 * the faked up offset will fit
76 #if BITS_PER_LONG == 64
77 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
78 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
80 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
81 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
85 * Initialize the GEM device fields
89 drm_gem_init(struct drm_device *dev)
91 struct drm_gem_mm *mm;
93 spin_lock_init(&dev->object_name_lock);
94 idr_init(&dev->object_name_idr);
95 atomic_set(&dev->object_count, 0);
96 atomic_set(&dev->object_memory, 0);
97 atomic_set(&dev->pin_count, 0);
98 atomic_set(&dev->pin_memory, 0);
99 atomic_set(&dev->gtt_count, 0);
100 atomic_set(&dev->gtt_memory, 0);
102 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
104 DRM_ERROR("out of memory\n");
108 dev->mm_private = mm;
110 if (drm_ht_create(&mm->offset_hash, 19)) {
115 if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
116 DRM_FILE_PAGE_OFFSET_SIZE)) {
117 drm_ht_remove(&mm->offset_hash);
126 drm_gem_destroy(struct drm_device *dev)
128 struct drm_gem_mm *mm = dev->mm_private;
130 drm_mm_takedown(&mm->offset_manager);
131 drm_ht_remove(&mm->offset_hash);
133 dev->mm_private = NULL;
137 * Initialize an already allocate GEM object of the specified size with
138 * shmfs backing store.
140 int drm_gem_object_init(struct drm_device *dev,
141 struct drm_gem_object *obj, size_t size)
143 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
146 obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
147 if (IS_ERR(obj->filp))
150 kref_init(&obj->refcount);
151 atomic_set(&obj->handle_count, 0);
154 atomic_inc(&dev->object_count);
155 atomic_add(obj->size, &dev->object_memory);
159 EXPORT_SYMBOL(drm_gem_object_init);
162 * Allocate a GEM object of the specified size with shmfs backing store
164 struct drm_gem_object *
165 drm_gem_object_alloc(struct drm_device *dev, size_t size)
167 struct drm_gem_object *obj;
169 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
173 if (drm_gem_object_init(dev, obj, size) != 0)
176 if (dev->driver->gem_init_object != NULL &&
177 dev->driver->gem_init_object(obj) != 0) {
182 /* Object_init mangles the global counters - readjust them. */
183 atomic_dec(&dev->object_count);
184 atomic_sub(obj->size, &dev->object_memory);
190 EXPORT_SYMBOL(drm_gem_object_alloc);
193 * Removes the mapping from handle to filp for this object.
196 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
198 struct drm_device *dev;
199 struct drm_gem_object *obj;
201 /* This is gross. The idr system doesn't let us try a delete and
202 * return an error code. It just spews if you fail at deleting.
203 * So, we have to grab a lock around finding the object and then
204 * doing the delete on it and dropping the refcount, or the user
205 * could race us to double-decrement the refcount and cause a
206 * use-after-free later. Given the frequency of our handle lookups,
207 * we may want to use ida for number allocation and a hash table
208 * for the pointers, anyway.
210 spin_lock(&filp->table_lock);
212 /* Check if we currently have a reference on the object */
213 obj = idr_find(&filp->object_idr, handle);
215 spin_unlock(&filp->table_lock);
220 /* Release reference and decrement refcount. */
221 idr_remove(&filp->object_idr, handle);
222 spin_unlock(&filp->table_lock);
224 drm_gem_object_handle_unreference_unlocked(obj);
230 * Create a handle for this object. This adds a handle reference
231 * to the object, which includes a regular reference count. Callers
232 * will likely want to dereference the object afterwards.
235 drm_gem_handle_create(struct drm_file *file_priv,
236 struct drm_gem_object *obj,
242 * Get the user-visible handle using idr.
245 /* ensure there is space available to allocate a handle */
246 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
249 /* do the allocation under our spinlock */
250 spin_lock(&file_priv->table_lock);
251 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
252 spin_unlock(&file_priv->table_lock);
259 drm_gem_object_handle_reference(obj);
262 EXPORT_SYMBOL(drm_gem_handle_create);
264 /** Returns a reference to the object named by the handle. */
265 struct drm_gem_object *
266 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
269 struct drm_gem_object *obj;
271 spin_lock(&filp->table_lock);
273 /* Check if we currently have a reference on the object */
274 obj = idr_find(&filp->object_idr, handle);
276 spin_unlock(&filp->table_lock);
280 drm_gem_object_reference(obj);
282 spin_unlock(&filp->table_lock);
286 EXPORT_SYMBOL(drm_gem_object_lookup);
289 * Releases the handle to an mm object.
292 drm_gem_close_ioctl(struct drm_device *dev, void *data,
293 struct drm_file *file_priv)
295 struct drm_gem_close *args = data;
298 if (!(dev->driver->driver_features & DRIVER_GEM))
301 ret = drm_gem_handle_delete(file_priv, args->handle);
307 * Create a global name for an object, returning the name.
309 * Note that the name does not hold a reference; when the object
310 * is freed, the name goes away.
313 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
314 struct drm_file *file_priv)
316 struct drm_gem_flink *args = data;
317 struct drm_gem_object *obj;
320 if (!(dev->driver->driver_features & DRIVER_GEM))
323 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
328 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
333 spin_lock(&dev->object_name_lock);
335 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
337 args->name = (uint64_t) obj->name;
338 spin_unlock(&dev->object_name_lock);
346 /* Allocate a reference for the name table. */
347 drm_gem_object_reference(obj);
349 args->name = (uint64_t) obj->name;
350 spin_unlock(&dev->object_name_lock);
355 drm_gem_object_unreference_unlocked(obj);
360 * Open an object using the global name, returning a handle and the size.
362 * This handle (of course) holds a reference to the object, so the object
363 * will not go away until the handle is deleted.
366 drm_gem_open_ioctl(struct drm_device *dev, void *data,
367 struct drm_file *file_priv)
369 struct drm_gem_open *args = data;
370 struct drm_gem_object *obj;
374 if (!(dev->driver->driver_features & DRIVER_GEM))
377 spin_lock(&dev->object_name_lock);
378 obj = idr_find(&dev->object_name_idr, (int) args->name);
380 drm_gem_object_reference(obj);
381 spin_unlock(&dev->object_name_lock);
385 ret = drm_gem_handle_create(file_priv, obj, &handle);
386 drm_gem_object_unreference_unlocked(obj);
390 args->handle = handle;
391 args->size = obj->size;
397 * Called at device open time, sets up the structure for handling refcounting
401 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
403 idr_init(&file_private->object_idr);
404 spin_lock_init(&file_private->table_lock);
408 * Called at device close to release the file's
409 * handle references on objects.
412 drm_gem_object_release_handle(int id, void *ptr, void *data)
414 struct drm_gem_object *obj = ptr;
416 drm_gem_object_handle_unreference_unlocked(obj);
422 * Called at close time when the filp is going away.
424 * Releases any remaining references on objects by this filp.
427 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
429 idr_for_each(&file_private->object_idr,
430 &drm_gem_object_release_handle, NULL);
432 idr_remove_all(&file_private->object_idr);
433 idr_destroy(&file_private->object_idr);
437 drm_gem_object_release(struct drm_gem_object *obj)
439 struct drm_device *dev = obj->dev;
441 atomic_dec(&dev->object_count);
442 atomic_sub(obj->size, &dev->object_memory);
444 EXPORT_SYMBOL(drm_gem_object_release);
447 * Called after the last reference to the object has been lost.
448 * Must be called holding struct_ mutex
453 drm_gem_object_free(struct kref *kref)
455 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
456 struct drm_device *dev = obj->dev;
458 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
460 if (dev->driver->gem_free_object != NULL)
461 dev->driver->gem_free_object(obj);
463 EXPORT_SYMBOL(drm_gem_object_free);
465 static void drm_gem_object_ref_bug(struct kref *list_kref)
471 * Called after the last handle to the object has been closed
473 * Removes any name for the object. Note that this must be
474 * called before drm_gem_object_free or we'll be touching
477 void drm_gem_object_handle_free(struct drm_gem_object *obj)
479 struct drm_device *dev = obj->dev;
481 /* Remove any name for this object */
482 spin_lock(&dev->object_name_lock);
484 idr_remove(&dev->object_name_idr, obj->name);
486 spin_unlock(&dev->object_name_lock);
488 * The object name held a reference to this object, drop
491 * This cannot be the last reference, since the handle holds one too.
493 kref_put(&obj->refcount, drm_gem_object_ref_bug);
495 spin_unlock(&dev->object_name_lock);
498 EXPORT_SYMBOL(drm_gem_object_handle_free);
500 void drm_gem_vm_open(struct vm_area_struct *vma)
502 struct drm_gem_object *obj = vma->vm_private_data;
504 drm_gem_object_reference(obj);
506 mutex_lock(&obj->dev->struct_mutex);
507 drm_vm_open_locked(vma);
508 mutex_unlock(&obj->dev->struct_mutex);
510 EXPORT_SYMBOL(drm_gem_vm_open);
512 void drm_gem_vm_close(struct vm_area_struct *vma)
514 struct drm_gem_object *obj = vma->vm_private_data;
516 mutex_lock(&obj->dev->struct_mutex);
517 drm_vm_close_locked(vma);
518 drm_gem_object_unreference(obj);
519 mutex_unlock(&obj->dev->struct_mutex);
521 EXPORT_SYMBOL(drm_gem_vm_close);
525 * drm_gem_mmap - memory map routine for GEM objects
526 * @filp: DRM file pointer
527 * @vma: VMA for the area to be mapped
529 * If a driver supports GEM object mapping, mmap calls on the DRM file
530 * descriptor will end up here.
532 * If we find the object based on the offset passed in (vma->vm_pgoff will
533 * contain the fake offset we created when the GTT map ioctl was called on
534 * the object), we set up the driver fault handler so that any accesses
535 * to the object can be trapped, to perform migration, GTT binding, surface
536 * register allocation, or performance monitoring.
538 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
540 struct drm_file *priv = filp->private_data;
541 struct drm_device *dev = priv->minor->dev;
542 struct drm_gem_mm *mm = dev->mm_private;
543 struct drm_local_map *map = NULL;
544 struct drm_gem_object *obj;
545 struct drm_hash_item *hash;
548 mutex_lock(&dev->struct_mutex);
550 if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
551 mutex_unlock(&dev->struct_mutex);
552 return drm_mmap(filp, vma);
555 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
557 ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
562 /* Check for valid size. */
563 if (map->size < vma->vm_end - vma->vm_start) {
569 if (!obj->dev->driver->gem_vm_ops) {
574 vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
575 vma->vm_ops = obj->dev->driver->gem_vm_ops;
576 vma->vm_private_data = map->handle;
577 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
579 /* Take a ref for this mapping of the object, so that the fault
580 * handler can dereference the mmap offset's pointer to the object.
581 * This reference is cleaned up by the corresponding vm_close
582 * (which should happen whether the vma was created by this call, or
583 * by a vm_open due to mremap or partial unmap or whatever).
585 drm_gem_object_reference(obj);
587 vma->vm_file = filp; /* Needed for drm_vm_open() */
588 drm_vm_open_locked(vma);
591 mutex_unlock(&dev->struct_mutex);
595 EXPORT_SYMBOL(drm_gem_mmap);