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/version.h>
34 #include <linux/types.h>
35 #include <linux/slab.h>
37 #include <linux/uaccess.h>
39 #include <linux/file.h>
40 #include <linux/module.h>
41 #include <linux/mman.h>
42 #include <linux/pagemap.h>
46 * This file provides some of the base ioctls and library routines for
47 * the graphics memory manager implemented by each device driver.
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.
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
60 * - Process limits prevent more than 1024 or so being used at a time by
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.
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.
73 * Initialize the GEM device fields
77 drm_gem_init(struct drm_device *dev)
79 spin_lock_init(&dev->object_name_lock);
80 idr_init(&dev->object_name_idr);
81 atomic_set(&dev->object_count, 0);
82 atomic_set(&dev->object_memory, 0);
83 atomic_set(&dev->pin_count, 0);
84 atomic_set(&dev->pin_memory, 0);
85 atomic_set(&dev->gtt_count, 0);
86 atomic_set(&dev->gtt_memory, 0);
91 * Allocate a GEM object of the specified size with shmfs backing store
93 struct drm_gem_object *
94 drm_gem_object_alloc(struct drm_device *dev, size_t size)
96 struct drm_gem_object *obj;
98 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
100 obj = kcalloc(1, sizeof(*obj), GFP_KERNEL);
103 obj->filp = shmem_file_setup("drm mm object", size, 0);
104 if (IS_ERR(obj->filp)) {
109 kref_init(&obj->refcount);
110 kref_init(&obj->handlecount);
112 if (dev->driver->gem_init_object != NULL &&
113 dev->driver->gem_init_object(obj) != 0) {
118 atomic_inc(&dev->object_count);
119 atomic_add(obj->size, &dev->object_memory);
122 EXPORT_SYMBOL(drm_gem_object_alloc);
125 * Removes the mapping from handle to filp for this object.
128 drm_gem_handle_delete(struct drm_file *filp, int handle)
130 struct drm_device *dev;
131 struct drm_gem_object *obj;
133 /* This is gross. The idr system doesn't let us try a delete and
134 * return an error code. It just spews if you fail at deleting.
135 * So, we have to grab a lock around finding the object and then
136 * doing the delete on it and dropping the refcount, or the user
137 * could race us to double-decrement the refcount and cause a
138 * use-after-free later. Given the frequency of our handle lookups,
139 * we may want to use ida for number allocation and a hash table
140 * for the pointers, anyway.
142 spin_lock(&filp->table_lock);
144 /* Check if we currently have a reference on the object */
145 obj = idr_find(&filp->object_idr, handle);
147 spin_unlock(&filp->table_lock);
152 /* Release reference and decrement refcount. */
153 idr_remove(&filp->object_idr, handle);
154 spin_unlock(&filp->table_lock);
156 mutex_lock(&dev->struct_mutex);
157 drm_gem_object_handle_unreference(obj);
158 mutex_unlock(&dev->struct_mutex);
164 * Create a handle for this object. This adds a handle reference
165 * to the object, which includes a regular reference count. Callers
166 * will likely want to dereference the object afterwards.
169 drm_gem_handle_create(struct drm_file *file_priv,
170 struct drm_gem_object *obj,
176 * Get the user-visible handle using idr.
179 /* ensure there is space available to allocate a handle */
180 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
183 /* do the allocation under our spinlock */
184 spin_lock(&file_priv->table_lock);
185 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep);
186 spin_unlock(&file_priv->table_lock);
193 drm_gem_object_handle_reference(obj);
196 EXPORT_SYMBOL(drm_gem_handle_create);
198 /** Returns a reference to the object named by the handle. */
199 struct drm_gem_object *
200 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
203 struct drm_gem_object *obj;
205 spin_lock(&filp->table_lock);
207 /* Check if we currently have a reference on the object */
208 obj = idr_find(&filp->object_idr, handle);
210 spin_unlock(&filp->table_lock);
214 drm_gem_object_reference(obj);
216 spin_unlock(&filp->table_lock);
220 EXPORT_SYMBOL(drm_gem_object_lookup);
223 * Releases the handle to an mm object.
226 drm_gem_close_ioctl(struct drm_device *dev, void *data,
227 struct drm_file *file_priv)
229 struct drm_gem_close *args = data;
232 if (!(dev->driver->driver_features & DRIVER_GEM))
235 ret = drm_gem_handle_delete(file_priv, args->handle);
241 * Create a global name for an object, returning the name.
243 * Note that the name does not hold a reference; when the object
244 * is freed, the name goes away.
247 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
248 struct drm_file *file_priv)
250 struct drm_gem_flink *args = data;
251 struct drm_gem_object *obj;
254 if (!(dev->driver->driver_features & DRIVER_GEM))
257 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
262 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0)
265 spin_lock(&dev->object_name_lock);
267 spin_unlock(&dev->object_name_lock);
270 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
272 spin_unlock(&dev->object_name_lock);
277 mutex_lock(&dev->struct_mutex);
278 drm_gem_object_unreference(obj);
279 mutex_unlock(&dev->struct_mutex);
284 * Leave the reference from the lookup around as the
285 * name table now holds one
287 args->name = (uint64_t) obj->name;
293 * Open an object using the global name, returning a handle and the size.
295 * This handle (of course) holds a reference to the object, so the object
296 * will not go away until the handle is deleted.
299 drm_gem_open_ioctl(struct drm_device *dev, void *data,
300 struct drm_file *file_priv)
302 struct drm_gem_open *args = data;
303 struct drm_gem_object *obj;
307 if (!(dev->driver->driver_features & DRIVER_GEM))
310 spin_lock(&dev->object_name_lock);
311 obj = idr_find(&dev->object_name_idr, (int) args->name);
313 drm_gem_object_reference(obj);
314 spin_unlock(&dev->object_name_lock);
318 ret = drm_gem_handle_create(file_priv, obj, &handle);
319 mutex_lock(&dev->struct_mutex);
320 drm_gem_object_unreference(obj);
321 mutex_unlock(&dev->struct_mutex);
325 args->handle = handle;
326 args->size = obj->size;
332 * Called at device open time, sets up the structure for handling refcounting
336 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
338 idr_init(&file_private->object_idr);
339 spin_lock_init(&file_private->table_lock);
343 * Called at device close to release the file's
344 * handle references on objects.
347 drm_gem_object_release_handle(int id, void *ptr, void *data)
349 struct drm_gem_object *obj = ptr;
351 drm_gem_object_handle_unreference(obj);
357 * Called at close time when the filp is going away.
359 * Releases any remaining references on objects by this filp.
362 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
364 mutex_lock(&dev->struct_mutex);
365 idr_for_each(&file_private->object_idr,
366 &drm_gem_object_release_handle, NULL);
368 idr_destroy(&file_private->object_idr);
369 mutex_unlock(&dev->struct_mutex);
373 * Called after the last reference to the object has been lost.
378 drm_gem_object_free(struct kref *kref)
380 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
381 struct drm_device *dev = obj->dev;
383 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
385 if (dev->driver->gem_free_object != NULL)
386 dev->driver->gem_free_object(obj);
389 atomic_dec(&dev->object_count);
390 atomic_sub(obj->size, &dev->object_memory);
393 EXPORT_SYMBOL(drm_gem_object_free);
396 * Called after the last handle to the object has been closed
398 * Removes any name for the object. Note that this must be
399 * called before drm_gem_object_free or we'll be touching
403 drm_gem_object_handle_free(struct kref *kref)
405 struct drm_gem_object *obj = container_of(kref,
406 struct drm_gem_object,
408 struct drm_device *dev = obj->dev;
410 /* Remove any name for this object */
411 spin_lock(&dev->object_name_lock);
413 idr_remove(&dev->object_name_idr, obj->name);
414 spin_unlock(&dev->object_name_lock);
416 * The object name held a reference to this object, drop
419 drm_gem_object_unreference(obj);
421 spin_unlock(&dev->object_name_lock);
424 EXPORT_SYMBOL(drm_gem_object_handle_free);
428 int drm_gem_init(struct drm_device *dev)
433 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
439 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)