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)) {
108 kref_init(&obj->refcount);
109 kref_init(&obj->handlecount);
111 if (dev->driver->gem_init_object != NULL &&
112 dev->driver->gem_init_object(obj) != 0) {
117 atomic_inc(&dev->object_count);
118 atomic_add(obj->size, &dev->object_memory);
121 EXPORT_SYMBOL(drm_gem_object_alloc);
124 * Removes the mapping from handle to filp for this object.
127 drm_gem_handle_delete(struct drm_file *filp, int handle)
129 struct drm_device *dev;
130 struct drm_gem_object *obj;
132 /* This is gross. The idr system doesn't let us try a delete and
133 * return an error code. It just spews if you fail at deleting.
134 * So, we have to grab a lock around finding the object and then
135 * doing the delete on it and dropping the refcount, or the user
136 * could race us to double-decrement the refcount and cause a
137 * use-after-free later. Given the frequency of our handle lookups,
138 * we may want to use ida for number allocation and a hash table
139 * for the pointers, anyway.
141 spin_lock(&filp->table_lock);
143 /* Check if we currently have a reference on the object */
144 obj = idr_find(&filp->object_idr, handle);
146 spin_unlock(&filp->table_lock);
151 /* Release reference and decrement refcount. */
152 idr_remove(&filp->object_idr, handle);
153 spin_unlock(&filp->table_lock);
155 mutex_lock(&dev->struct_mutex);
156 drm_gem_object_handle_unreference(obj);
157 mutex_unlock(&dev->struct_mutex);
163 * Create a handle for this object. This adds a handle reference
164 * to the object, which includes a regular reference count. Callers
165 * will likely want to dereference the object afterwards.
168 drm_gem_handle_create(struct drm_file *file_priv,
169 struct drm_gem_object *obj,
175 * Get the user-visible handle using idr.
178 /* ensure there is space available to allocate a handle */
179 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
182 /* do the allocation under our spinlock */
183 spin_lock(&file_priv->table_lock);
184 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep);
185 spin_unlock(&file_priv->table_lock);
192 drm_gem_object_handle_reference(obj);
195 EXPORT_SYMBOL(drm_gem_handle_create);
197 /** Returns a reference to the object named by the handle. */
198 struct drm_gem_object *
199 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
202 struct drm_gem_object *obj;
204 spin_lock(&filp->table_lock);
206 /* Check if we currently have a reference on the object */
207 obj = idr_find(&filp->object_idr, handle);
209 spin_unlock(&filp->table_lock);
213 drm_gem_object_reference(obj);
215 spin_unlock(&filp->table_lock);
219 EXPORT_SYMBOL(drm_gem_object_lookup);
222 * Releases the handle to an mm object.
225 drm_gem_close_ioctl(struct drm_device *dev, void *data,
226 struct drm_file *file_priv)
228 struct drm_gem_close *args = data;
231 if (!(dev->driver->driver_features & DRIVER_GEM))
234 ret = drm_gem_handle_delete(file_priv, args->handle);
240 * Create a global name for an object, returning the name.
242 * Note that the name does not hold a reference; when the object
243 * is freed, the name goes away.
246 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
247 struct drm_file *file_priv)
249 struct drm_gem_flink *args = data;
250 struct drm_gem_object *obj;
253 if (!(dev->driver->driver_features & DRIVER_GEM))
256 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
261 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0)
264 spin_lock(&dev->object_name_lock);
266 spin_unlock(&dev->object_name_lock);
269 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
271 spin_unlock(&dev->object_name_lock);
276 mutex_lock(&dev->struct_mutex);
277 drm_gem_object_unreference(obj);
278 mutex_unlock(&dev->struct_mutex);
283 * Leave the reference from the lookup around as the
284 * name table now holds one
286 args->name = (uint64_t) obj->name;
292 * Open an object using the global name, returning a handle and the size.
294 * This handle (of course) holds a reference to the object, so the object
295 * will not go away until the handle is deleted.
298 drm_gem_open_ioctl(struct drm_device *dev, void *data,
299 struct drm_file *file_priv)
301 struct drm_gem_open *args = data;
302 struct drm_gem_object *obj;
306 if (!(dev->driver->driver_features & DRIVER_GEM))
309 spin_lock(&dev->object_name_lock);
310 obj = idr_find(&dev->object_name_idr, (int) args->name);
312 drm_gem_object_reference(obj);
313 spin_unlock(&dev->object_name_lock);
317 ret = drm_gem_handle_create(file_priv, obj, &handle);
318 mutex_lock(&dev->struct_mutex);
319 drm_gem_object_unreference(obj);
320 mutex_unlock(&dev->struct_mutex);
324 args->handle = handle;
325 args->size = obj->size;
331 * Called at device open time, sets up the structure for handling refcounting
335 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
337 idr_init(&file_private->object_idr);
338 spin_lock_init(&file_private->table_lock);
342 * Called at device close to release the file's
343 * handle references on objects.
346 drm_gem_object_release_handle(int id, void *ptr, void *data)
348 struct drm_gem_object *obj = ptr;
350 drm_gem_object_handle_unreference(obj);
356 * Called at close time when the filp is going away.
358 * Releases any remaining references on objects by this filp.
361 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
363 mutex_lock(&dev->struct_mutex);
364 idr_for_each(&file_private->object_idr,
365 &drm_gem_object_release_handle, NULL);
367 idr_destroy(&file_private->object_idr);
368 mutex_unlock(&dev->struct_mutex);
372 * Called after the last reference to the object has been lost.
377 drm_gem_object_free(struct kref *kref)
379 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
380 struct drm_device *dev = obj->dev;
382 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
384 if (dev->driver->gem_free_object != NULL)
385 dev->driver->gem_free_object(obj);
388 atomic_dec(&dev->object_count);
389 atomic_sub(obj->size, &dev->object_memory);
392 EXPORT_SYMBOL(drm_gem_object_free);
395 * Called after the last handle to the object has been closed
397 * Removes any name for the object. Note that this must be
398 * called before drm_gem_object_free or we'll be touching
402 drm_gem_object_handle_free(struct kref *kref)
404 struct drm_gem_object *obj = container_of(kref,
405 struct drm_gem_object,
407 struct drm_device *dev = obj->dev;
409 /* Remove any name for this object */
410 spin_lock(&dev->object_name_lock);
412 idr_remove(&dev->object_name_idr, obj->name);
413 spin_unlock(&dev->object_name_lock);
415 * The object name held a reference to this object, drop
418 drm_gem_object_unreference(obj);
420 spin_unlock(&dev->object_name_lock);
423 EXPORT_SYMBOL(drm_gem_object_handle_free);
427 int drm_gem_init(struct drm_device *dev)
432 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
438 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)