staging: ti dspbridge: deh: trivial cleanups
[linux-2.6/x86.git] / drivers / gpu / drm / drm_gem.c
blob33dad3fa60439e0b39daca428f8b062ddfca8276
1 /*
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
13 * Software.
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.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
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 "drmP.h"
39 /** @file drm_gem.c
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
54 * two major failings:
55 * - Process limits prevent more than 1024 or so being used at a time by
56 * default.
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
69 * mmap time.
71 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
72 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
74 /**
75 * Initialize the GEM device fields
78 int
79 drm_gem_init(struct drm_device *dev)
81 struct drm_gem_mm *mm;
83 spin_lock_init(&dev->object_name_lock);
84 idr_init(&dev->object_name_idr);
85 atomic_set(&dev->object_count, 0);
86 atomic_set(&dev->object_memory, 0);
87 atomic_set(&dev->pin_count, 0);
88 atomic_set(&dev->pin_memory, 0);
89 atomic_set(&dev->gtt_count, 0);
90 atomic_set(&dev->gtt_memory, 0);
92 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
93 if (!mm) {
94 DRM_ERROR("out of memory\n");
95 return -ENOMEM;
98 dev->mm_private = mm;
100 if (drm_ht_create(&mm->offset_hash, 19)) {
101 kfree(mm);
102 return -ENOMEM;
105 if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
106 DRM_FILE_PAGE_OFFSET_SIZE)) {
107 drm_ht_remove(&mm->offset_hash);
108 kfree(mm);
109 return -ENOMEM;
112 return 0;
115 void
116 drm_gem_destroy(struct drm_device *dev)
118 struct drm_gem_mm *mm = dev->mm_private;
120 drm_mm_takedown(&mm->offset_manager);
121 drm_ht_remove(&mm->offset_hash);
122 kfree(mm);
123 dev->mm_private = NULL;
127 * Initialize an already allocate GEM object of the specified size with
128 * shmfs backing store.
130 int drm_gem_object_init(struct drm_device *dev,
131 struct drm_gem_object *obj, size_t size)
133 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
135 obj->dev = dev;
136 obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
137 if (IS_ERR(obj->filp))
138 return -ENOMEM;
140 kref_init(&obj->refcount);
141 kref_init(&obj->handlecount);
142 obj->size = size;
144 atomic_inc(&dev->object_count);
145 atomic_add(obj->size, &dev->object_memory);
147 return 0;
149 EXPORT_SYMBOL(drm_gem_object_init);
152 * Allocate a GEM object of the specified size with shmfs backing store
154 struct drm_gem_object *
155 drm_gem_object_alloc(struct drm_device *dev, size_t size)
157 struct drm_gem_object *obj;
159 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
160 if (!obj)
161 goto free;
163 if (drm_gem_object_init(dev, obj, size) != 0)
164 goto free;
166 if (dev->driver->gem_init_object != NULL &&
167 dev->driver->gem_init_object(obj) != 0) {
168 goto fput;
170 return obj;
171 fput:
172 /* Object_init mangles the global counters - readjust them. */
173 atomic_dec(&dev->object_count);
174 atomic_sub(obj->size, &dev->object_memory);
175 fput(obj->filp);
176 free:
177 kfree(obj);
178 return NULL;
180 EXPORT_SYMBOL(drm_gem_object_alloc);
183 * Removes the mapping from handle to filp for this object.
185 static int
186 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
188 struct drm_device *dev;
189 struct drm_gem_object *obj;
191 /* This is gross. The idr system doesn't let us try a delete and
192 * return an error code. It just spews if you fail at deleting.
193 * So, we have to grab a lock around finding the object and then
194 * doing the delete on it and dropping the refcount, or the user
195 * could race us to double-decrement the refcount and cause a
196 * use-after-free later. Given the frequency of our handle lookups,
197 * we may want to use ida for number allocation and a hash table
198 * for the pointers, anyway.
200 spin_lock(&filp->table_lock);
202 /* Check if we currently have a reference on the object */
203 obj = idr_find(&filp->object_idr, handle);
204 if (obj == NULL) {
205 spin_unlock(&filp->table_lock);
206 return -EINVAL;
208 dev = obj->dev;
210 /* Release reference and decrement refcount. */
211 idr_remove(&filp->object_idr, handle);
212 spin_unlock(&filp->table_lock);
214 drm_gem_object_handle_unreference_unlocked(obj);
216 return 0;
220 * Create a handle for this object. This adds a handle reference
221 * to the object, which includes a regular reference count. Callers
222 * will likely want to dereference the object afterwards.
225 drm_gem_handle_create(struct drm_file *file_priv,
226 struct drm_gem_object *obj,
227 u32 *handlep)
229 int ret;
232 * Get the user-visible handle using idr.
234 again:
235 /* ensure there is space available to allocate a handle */
236 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
237 return -ENOMEM;
239 /* do the allocation under our spinlock */
240 spin_lock(&file_priv->table_lock);
241 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
242 spin_unlock(&file_priv->table_lock);
243 if (ret == -EAGAIN)
244 goto again;
246 if (ret != 0)
247 return ret;
249 drm_gem_object_handle_reference(obj);
250 return 0;
252 EXPORT_SYMBOL(drm_gem_handle_create);
254 /** Returns a reference to the object named by the handle. */
255 struct drm_gem_object *
256 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
257 u32 handle)
259 struct drm_gem_object *obj;
261 spin_lock(&filp->table_lock);
263 /* Check if we currently have a reference on the object */
264 obj = idr_find(&filp->object_idr, handle);
265 if (obj == NULL) {
266 spin_unlock(&filp->table_lock);
267 return NULL;
270 drm_gem_object_reference(obj);
272 spin_unlock(&filp->table_lock);
274 return obj;
276 EXPORT_SYMBOL(drm_gem_object_lookup);
279 * Releases the handle to an mm object.
282 drm_gem_close_ioctl(struct drm_device *dev, void *data,
283 struct drm_file *file_priv)
285 struct drm_gem_close *args = data;
286 int ret;
288 if (!(dev->driver->driver_features & DRIVER_GEM))
289 return -ENODEV;
291 ret = drm_gem_handle_delete(file_priv, args->handle);
293 return ret;
297 * Create a global name for an object, returning the name.
299 * Note that the name does not hold a reference; when the object
300 * is freed, the name goes away.
303 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
304 struct drm_file *file_priv)
306 struct drm_gem_flink *args = data;
307 struct drm_gem_object *obj;
308 int ret;
310 if (!(dev->driver->driver_features & DRIVER_GEM))
311 return -ENODEV;
313 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
314 if (obj == NULL)
315 return -EBADF;
317 again:
318 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
319 ret = -ENOMEM;
320 goto err;
323 spin_lock(&dev->object_name_lock);
324 if (!obj->name) {
325 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
326 &obj->name);
327 args->name = (uint64_t) obj->name;
328 spin_unlock(&dev->object_name_lock);
330 if (ret == -EAGAIN)
331 goto again;
333 if (ret != 0)
334 goto err;
336 /* Allocate a reference for the name table. */
337 drm_gem_object_reference(obj);
338 } else {
339 args->name = (uint64_t) obj->name;
340 spin_unlock(&dev->object_name_lock);
341 ret = 0;
344 err:
345 drm_gem_object_unreference_unlocked(obj);
346 return ret;
350 * Open an object using the global name, returning a handle and the size.
352 * This handle (of course) holds a reference to the object, so the object
353 * will not go away until the handle is deleted.
356 drm_gem_open_ioctl(struct drm_device *dev, void *data,
357 struct drm_file *file_priv)
359 struct drm_gem_open *args = data;
360 struct drm_gem_object *obj;
361 int ret;
362 u32 handle;
364 if (!(dev->driver->driver_features & DRIVER_GEM))
365 return -ENODEV;
367 spin_lock(&dev->object_name_lock);
368 obj = idr_find(&dev->object_name_idr, (int) args->name);
369 if (obj)
370 drm_gem_object_reference(obj);
371 spin_unlock(&dev->object_name_lock);
372 if (!obj)
373 return -ENOENT;
375 ret = drm_gem_handle_create(file_priv, obj, &handle);
376 drm_gem_object_unreference_unlocked(obj);
377 if (ret)
378 return ret;
380 args->handle = handle;
381 args->size = obj->size;
383 return 0;
387 * Called at device open time, sets up the structure for handling refcounting
388 * of mm objects.
390 void
391 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
393 idr_init(&file_private->object_idr);
394 spin_lock_init(&file_private->table_lock);
398 * Called at device close to release the file's
399 * handle references on objects.
401 static int
402 drm_gem_object_release_handle(int id, void *ptr, void *data)
404 struct drm_gem_object *obj = ptr;
406 drm_gem_object_handle_unreference_unlocked(obj);
408 return 0;
412 * Called at close time when the filp is going away.
414 * Releases any remaining references on objects by this filp.
416 void
417 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
419 idr_for_each(&file_private->object_idr,
420 &drm_gem_object_release_handle, NULL);
422 idr_destroy(&file_private->object_idr);
425 void
426 drm_gem_object_release(struct drm_gem_object *obj)
428 struct drm_device *dev = obj->dev;
429 fput(obj->filp);
430 atomic_dec(&dev->object_count);
431 atomic_sub(obj->size, &dev->object_memory);
433 EXPORT_SYMBOL(drm_gem_object_release);
436 * Called after the last reference to the object has been lost.
437 * Must be called holding struct_ mutex
439 * Frees the object
441 void
442 drm_gem_object_free(struct kref *kref)
444 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
445 struct drm_device *dev = obj->dev;
447 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
449 if (dev->driver->gem_free_object != NULL)
450 dev->driver->gem_free_object(obj);
452 EXPORT_SYMBOL(drm_gem_object_free);
455 * Called after the last reference to the object has been lost.
456 * Must be called without holding struct_mutex
458 * Frees the object
460 void
461 drm_gem_object_free_unlocked(struct kref *kref)
463 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
464 struct drm_device *dev = obj->dev;
466 if (dev->driver->gem_free_object_unlocked != NULL)
467 dev->driver->gem_free_object_unlocked(obj);
468 else if (dev->driver->gem_free_object != NULL) {
469 mutex_lock(&dev->struct_mutex);
470 dev->driver->gem_free_object(obj);
471 mutex_unlock(&dev->struct_mutex);
474 EXPORT_SYMBOL(drm_gem_object_free_unlocked);
476 static void drm_gem_object_ref_bug(struct kref *list_kref)
478 BUG();
482 * Called after the last handle to the object has been closed
484 * Removes any name for the object. Note that this must be
485 * called before drm_gem_object_free or we'll be touching
486 * freed memory
488 void
489 drm_gem_object_handle_free(struct kref *kref)
491 struct drm_gem_object *obj = container_of(kref,
492 struct drm_gem_object,
493 handlecount);
494 struct drm_device *dev = obj->dev;
496 /* Remove any name for this object */
497 spin_lock(&dev->object_name_lock);
498 if (obj->name) {
499 idr_remove(&dev->object_name_idr, obj->name);
500 obj->name = 0;
501 spin_unlock(&dev->object_name_lock);
503 * The object name held a reference to this object, drop
504 * that now.
506 * This cannot be the last reference, since the handle holds one too.
508 kref_put(&obj->refcount, drm_gem_object_ref_bug);
509 } else
510 spin_unlock(&dev->object_name_lock);
513 EXPORT_SYMBOL(drm_gem_object_handle_free);
515 void drm_gem_vm_open(struct vm_area_struct *vma)
517 struct drm_gem_object *obj = vma->vm_private_data;
519 drm_gem_object_reference(obj);
521 EXPORT_SYMBOL(drm_gem_vm_open);
523 void drm_gem_vm_close(struct vm_area_struct *vma)
525 struct drm_gem_object *obj = vma->vm_private_data;
527 drm_gem_object_unreference_unlocked(obj);
529 EXPORT_SYMBOL(drm_gem_vm_close);
533 * drm_gem_mmap - memory map routine for GEM objects
534 * @filp: DRM file pointer
535 * @vma: VMA for the area to be mapped
537 * If a driver supports GEM object mapping, mmap calls on the DRM file
538 * descriptor will end up here.
540 * If we find the object based on the offset passed in (vma->vm_pgoff will
541 * contain the fake offset we created when the GTT map ioctl was called on
542 * the object), we set up the driver fault handler so that any accesses
543 * to the object can be trapped, to perform migration, GTT binding, surface
544 * register allocation, or performance monitoring.
546 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
548 struct drm_file *priv = filp->private_data;
549 struct drm_device *dev = priv->minor->dev;
550 struct drm_gem_mm *mm = dev->mm_private;
551 struct drm_local_map *map = NULL;
552 struct drm_gem_object *obj;
553 struct drm_hash_item *hash;
554 int ret = 0;
556 mutex_lock(&dev->struct_mutex);
558 if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
559 mutex_unlock(&dev->struct_mutex);
560 return drm_mmap(filp, vma);
563 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
564 if (!map ||
565 ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
566 ret = -EPERM;
567 goto out_unlock;
570 /* Check for valid size. */
571 if (map->size < vma->vm_end - vma->vm_start) {
572 ret = -EINVAL;
573 goto out_unlock;
576 obj = map->handle;
577 if (!obj->dev->driver->gem_vm_ops) {
578 ret = -EINVAL;
579 goto out_unlock;
582 vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
583 vma->vm_ops = obj->dev->driver->gem_vm_ops;
584 vma->vm_private_data = map->handle;
585 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
587 /* Take a ref for this mapping of the object, so that the fault
588 * handler can dereference the mmap offset's pointer to the object.
589 * This reference is cleaned up by the corresponding vm_close
590 * (which should happen whether the vma was created by this call, or
591 * by a vm_open due to mremap or partial unmap or whatever).
593 drm_gem_object_reference(obj);
595 vma->vm_file = filp; /* Needed for drm_vm_open() */
596 drm_vm_open_locked(vma);
598 out_unlock:
599 mutex_unlock(&dev->struct_mutex);
601 return ret;
603 EXPORT_SYMBOL(drm_gem_mmap);