blob: 4018b3bfc72e49e507b282a218602ef4048a1714 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
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:
10 *
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.
14 *
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.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
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"
38
39/** @file drm_gem.c
40 *
41 * This file provides some of the base ioctls and library routines for
42 * the graphics memory manager implemented by each device driver.
43 *
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.
51 *
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.
59 *
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.
65 */
66
Jesse Barnesa2c0a972008-11-05 10:31:53 -080067/*
68 * We make up offsets for buffer objects so we can recognize them at
69 * mmap time.
70 */
71#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
72#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
73
Eric Anholt673a3942008-07-30 12:06:12 -070074/**
75 * Initialize the GEM device fields
76 */
77
78int
79drm_gem_init(struct drm_device *dev)
80{
Jesse Barnesa2c0a972008-11-05 10:31:53 -080081 struct drm_gem_mm *mm;
82
Eric Anholt673a3942008-07-30 12:06:12 -070083 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);
Jesse Barnesa2c0a972008-11-05 10:31:53 -080091
Eric Anholt9a298b22009-03-24 12:23:04 -070092 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
Jesse Barnesa2c0a972008-11-05 10:31:53 -080093 if (!mm) {
94 DRM_ERROR("out of memory\n");
95 return -ENOMEM;
96 }
97
98 dev->mm_private = mm;
99
100 if (drm_ht_create(&mm->offset_hash, 19)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700101 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800102 return -ENOMEM;
103 }
104
105 if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
106 DRM_FILE_PAGE_OFFSET_SIZE)) {
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800107 drm_ht_remove(&mm->offset_hash);
Eric Anholt9a298b22009-03-24 12:23:04 -0700108 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800109 return -ENOMEM;
110 }
111
Eric Anholt673a3942008-07-30 12:06:12 -0700112 return 0;
113}
114
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800115void
116drm_gem_destroy(struct drm_device *dev)
117{
118 struct drm_gem_mm *mm = dev->mm_private;
119
120 drm_mm_takedown(&mm->offset_manager);
121 drm_ht_remove(&mm->offset_hash);
Eric Anholt9a298b22009-03-24 12:23:04 -0700122 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800123 dev->mm_private = NULL;
124}
125
Eric Anholt673a3942008-07-30 12:06:12 -0700126/**
127 * Allocate a GEM object of the specified size with shmfs backing store
128 */
129struct drm_gem_object *
130drm_gem_object_alloc(struct drm_device *dev, size_t size)
131{
132 struct drm_gem_object *obj;
133
134 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
135
Robert P. J. Dayb798b1f2009-06-10 12:43:49 -0700136 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
Jiri Slaby845792d2009-07-13 23:20:21 +0200137 if (!obj)
138 goto free;
Eric Anholt673a3942008-07-30 12:06:12 -0700139
140 obj->dev = dev;
Linus Torvaldsfc8744a2009-01-31 15:08:56 -0800141 obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
Jiri Slaby845792d2009-07-13 23:20:21 +0200142 if (IS_ERR(obj->filp))
143 goto free;
Eric Anholt673a3942008-07-30 12:06:12 -0700144
145 kref_init(&obj->refcount);
146 kref_init(&obj->handlecount);
147 obj->size = size;
148 if (dev->driver->gem_init_object != NULL &&
149 dev->driver->gem_init_object(obj) != 0) {
Jiri Slaby845792d2009-07-13 23:20:21 +0200150 goto fput;
Eric Anholt673a3942008-07-30 12:06:12 -0700151 }
152 atomic_inc(&dev->object_count);
153 atomic_add(obj->size, &dev->object_memory);
154 return obj;
Jiri Slaby845792d2009-07-13 23:20:21 +0200155fput:
156 fput(obj->filp);
157free:
158 kfree(obj);
159 return NULL;
Eric Anholt673a3942008-07-30 12:06:12 -0700160}
161EXPORT_SYMBOL(drm_gem_object_alloc);
162
163/**
164 * Removes the mapping from handle to filp for this object.
165 */
166static int
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300167drm_gem_handle_delete(struct drm_file *filp, u32 handle)
Eric Anholt673a3942008-07-30 12:06:12 -0700168{
169 struct drm_device *dev;
170 struct drm_gem_object *obj;
171
172 /* This is gross. The idr system doesn't let us try a delete and
173 * return an error code. It just spews if you fail at deleting.
174 * So, we have to grab a lock around finding the object and then
175 * doing the delete on it and dropping the refcount, or the user
176 * could race us to double-decrement the refcount and cause a
177 * use-after-free later. Given the frequency of our handle lookups,
178 * we may want to use ida for number allocation and a hash table
179 * for the pointers, anyway.
180 */
181 spin_lock(&filp->table_lock);
182
183 /* Check if we currently have a reference on the object */
184 obj = idr_find(&filp->object_idr, handle);
185 if (obj == NULL) {
186 spin_unlock(&filp->table_lock);
187 return -EINVAL;
188 }
189 dev = obj->dev;
190
191 /* Release reference and decrement refcount. */
192 idr_remove(&filp->object_idr, handle);
193 spin_unlock(&filp->table_lock);
194
195 mutex_lock(&dev->struct_mutex);
196 drm_gem_object_handle_unreference(obj);
197 mutex_unlock(&dev->struct_mutex);
198
199 return 0;
200}
201
202/**
203 * Create a handle for this object. This adds a handle reference
204 * to the object, which includes a regular reference count. Callers
205 * will likely want to dereference the object afterwards.
206 */
207int
208drm_gem_handle_create(struct drm_file *file_priv,
209 struct drm_gem_object *obj,
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300210 u32 *handlep)
Eric Anholt673a3942008-07-30 12:06:12 -0700211{
212 int ret;
213
214 /*
215 * Get the user-visible handle using idr.
216 */
217again:
218 /* ensure there is space available to allocate a handle */
219 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
220 return -ENOMEM;
221
222 /* do the allocation under our spinlock */
223 spin_lock(&file_priv->table_lock);
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300224 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
Eric Anholt673a3942008-07-30 12:06:12 -0700225 spin_unlock(&file_priv->table_lock);
226 if (ret == -EAGAIN)
227 goto again;
228
229 if (ret != 0)
230 return ret;
231
232 drm_gem_object_handle_reference(obj);
233 return 0;
234}
235EXPORT_SYMBOL(drm_gem_handle_create);
236
237/** Returns a reference to the object named by the handle. */
238struct drm_gem_object *
239drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300240 u32 handle)
Eric Anholt673a3942008-07-30 12:06:12 -0700241{
242 struct drm_gem_object *obj;
243
244 spin_lock(&filp->table_lock);
245
246 /* Check if we currently have a reference on the object */
247 obj = idr_find(&filp->object_idr, handle);
248 if (obj == NULL) {
249 spin_unlock(&filp->table_lock);
250 return NULL;
251 }
252
253 drm_gem_object_reference(obj);
254
255 spin_unlock(&filp->table_lock);
256
257 return obj;
258}
259EXPORT_SYMBOL(drm_gem_object_lookup);
260
261/**
262 * Releases the handle to an mm object.
263 */
264int
265drm_gem_close_ioctl(struct drm_device *dev, void *data,
266 struct drm_file *file_priv)
267{
268 struct drm_gem_close *args = data;
269 int ret;
270
271 if (!(dev->driver->driver_features & DRIVER_GEM))
272 return -ENODEV;
273
274 ret = drm_gem_handle_delete(file_priv, args->handle);
275
276 return ret;
277}
278
279/**
280 * Create a global name for an object, returning the name.
281 *
282 * Note that the name does not hold a reference; when the object
283 * is freed, the name goes away.
284 */
285int
286drm_gem_flink_ioctl(struct drm_device *dev, void *data,
287 struct drm_file *file_priv)
288{
289 struct drm_gem_flink *args = data;
290 struct drm_gem_object *obj;
291 int ret;
292
293 if (!(dev->driver->driver_features & DRIVER_GEM))
294 return -ENODEV;
295
296 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
297 if (obj == NULL)
Eric Anholtd4e7b892008-09-09 11:40:34 -0700298 return -EBADF;
Eric Anholt673a3942008-07-30 12:06:12 -0700299
300again:
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000301 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
302 ret = -ENOMEM;
303 goto err;
304 }
Eric Anholt673a3942008-07-30 12:06:12 -0700305
306 spin_lock(&dev->object_name_lock);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000307 if (!obj->name) {
308 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
309 &obj->name);
310 args->name = (uint64_t) obj->name;
Eric Anholt673a3942008-07-30 12:06:12 -0700311 spin_unlock(&dev->object_name_lock);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000312
313 if (ret == -EAGAIN)
314 goto again;
315
316 if (ret != 0)
317 goto err;
318
319 /* Allocate a reference for the name table. */
320 drm_gem_object_reference(obj);
321 } else {
322 args->name = (uint64_t) obj->name;
323 spin_unlock(&dev->object_name_lock);
324 ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700325 }
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000326
327err:
328 mutex_lock(&dev->struct_mutex);
329 drm_gem_object_unreference(obj);
330 mutex_unlock(&dev->struct_mutex);
331 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700332}
333
334/**
335 * Open an object using the global name, returning a handle and the size.
336 *
337 * This handle (of course) holds a reference to the object, so the object
338 * will not go away until the handle is deleted.
339 */
340int
341drm_gem_open_ioctl(struct drm_device *dev, void *data,
342 struct drm_file *file_priv)
343{
344 struct drm_gem_open *args = data;
345 struct drm_gem_object *obj;
346 int ret;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300347 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700348
349 if (!(dev->driver->driver_features & DRIVER_GEM))
350 return -ENODEV;
351
352 spin_lock(&dev->object_name_lock);
353 obj = idr_find(&dev->object_name_idr, (int) args->name);
354 if (obj)
355 drm_gem_object_reference(obj);
356 spin_unlock(&dev->object_name_lock);
357 if (!obj)
358 return -ENOENT;
359
360 ret = drm_gem_handle_create(file_priv, obj, &handle);
361 mutex_lock(&dev->struct_mutex);
362 drm_gem_object_unreference(obj);
363 mutex_unlock(&dev->struct_mutex);
364 if (ret)
365 return ret;
366
367 args->handle = handle;
368 args->size = obj->size;
369
370 return 0;
371}
372
373/**
374 * Called at device open time, sets up the structure for handling refcounting
375 * of mm objects.
376 */
377void
378drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
379{
380 idr_init(&file_private->object_idr);
381 spin_lock_init(&file_private->table_lock);
382}
383
384/**
385 * Called at device close to release the file's
386 * handle references on objects.
387 */
388static int
389drm_gem_object_release_handle(int id, void *ptr, void *data)
390{
391 struct drm_gem_object *obj = ptr;
392
393 drm_gem_object_handle_unreference(obj);
394
395 return 0;
396}
397
398/**
399 * Called at close time when the filp is going away.
400 *
401 * Releases any remaining references on objects by this filp.
402 */
403void
404drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
405{
406 mutex_lock(&dev->struct_mutex);
407 idr_for_each(&file_private->object_idr,
408 &drm_gem_object_release_handle, NULL);
409
410 idr_destroy(&file_private->object_idr);
411 mutex_unlock(&dev->struct_mutex);
412}
413
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000414static void
415drm_gem_object_free_common(struct drm_gem_object *obj)
416{
417 struct drm_device *dev = obj->dev;
418 fput(obj->filp);
419 atomic_dec(&dev->object_count);
420 atomic_sub(obj->size, &dev->object_memory);
421 kfree(obj);
422}
423
Eric Anholt673a3942008-07-30 12:06:12 -0700424/**
425 * Called after the last reference to the object has been lost.
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000426 * Must be called holding struct_ mutex
Eric Anholt673a3942008-07-30 12:06:12 -0700427 *
428 * Frees the object
429 */
430void
431drm_gem_object_free(struct kref *kref)
432{
433 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
434 struct drm_device *dev = obj->dev;
435
436 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
437
438 if (dev->driver->gem_free_object != NULL)
439 dev->driver->gem_free_object(obj);
440
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000441 drm_gem_object_free_common(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700442}
443EXPORT_SYMBOL(drm_gem_object_free);
444
445/**
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000446 * Called after the last reference to the object has been lost.
447 * Must be called without holding struct_mutex
448 *
449 * Frees the object
450 */
451void
452drm_gem_object_free_unlocked(struct kref *kref)
453{
454 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
455 struct drm_device *dev = obj->dev;
456
457 if (dev->driver->gem_free_object_unlocked != NULL)
458 dev->driver->gem_free_object_unlocked(obj);
459 else if (dev->driver->gem_free_object != NULL) {
460 mutex_lock(&dev->struct_mutex);
461 dev->driver->gem_free_object(obj);
462 mutex_unlock(&dev->struct_mutex);
463 }
464
465 drm_gem_object_free_common(obj);
466}
467EXPORT_SYMBOL(drm_gem_object_free_unlocked);
468
469static void drm_gem_object_ref_bug(struct kref *list_kref)
470{
471 BUG();
472}
473
474/**
Eric Anholt673a3942008-07-30 12:06:12 -0700475 * Called after the last handle to the object has been closed
476 *
477 * Removes any name for the object. Note that this must be
478 * called before drm_gem_object_free or we'll be touching
479 * freed memory
480 */
481void
482drm_gem_object_handle_free(struct kref *kref)
483{
484 struct drm_gem_object *obj = container_of(kref,
485 struct drm_gem_object,
486 handlecount);
487 struct drm_device *dev = obj->dev;
488
489 /* Remove any name for this object */
490 spin_lock(&dev->object_name_lock);
491 if (obj->name) {
492 idr_remove(&dev->object_name_idr, obj->name);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000493 obj->name = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700494 spin_unlock(&dev->object_name_lock);
495 /*
496 * The object name held a reference to this object, drop
497 * that now.
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000498 *
499 * This cannot be the last reference, since the handle holds one too.
Eric Anholt673a3942008-07-30 12:06:12 -0700500 */
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000501 kref_put(&obj->refcount, drm_gem_object_ref_bug);
Eric Anholt673a3942008-07-30 12:06:12 -0700502 } else
503 spin_unlock(&dev->object_name_lock);
504
505}
506EXPORT_SYMBOL(drm_gem_object_handle_free);
507
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800508void drm_gem_vm_open(struct vm_area_struct *vma)
509{
510 struct drm_gem_object *obj = vma->vm_private_data;
511
512 drm_gem_object_reference(obj);
513}
514EXPORT_SYMBOL(drm_gem_vm_open);
515
516void drm_gem_vm_close(struct vm_area_struct *vma)
517{
518 struct drm_gem_object *obj = vma->vm_private_data;
519 struct drm_device *dev = obj->dev;
520
521 mutex_lock(&dev->struct_mutex);
522 drm_gem_object_unreference(obj);
523 mutex_unlock(&dev->struct_mutex);
524}
525EXPORT_SYMBOL(drm_gem_vm_close);
526
527
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800528/**
529 * drm_gem_mmap - memory map routine for GEM objects
530 * @filp: DRM file pointer
531 * @vma: VMA for the area to be mapped
532 *
533 * If a driver supports GEM object mapping, mmap calls on the DRM file
534 * descriptor will end up here.
535 *
536 * If we find the object based on the offset passed in (vma->vm_pgoff will
537 * contain the fake offset we created when the GTT map ioctl was called on
538 * the object), we set up the driver fault handler so that any accesses
539 * to the object can be trapped, to perform migration, GTT binding, surface
540 * register allocation, or performance monitoring.
541 */
542int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
543{
544 struct drm_file *priv = filp->private_data;
545 struct drm_device *dev = priv->minor->dev;
546 struct drm_gem_mm *mm = dev->mm_private;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100547 struct drm_local_map *map = NULL;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800548 struct drm_gem_object *obj;
549 struct drm_hash_item *hash;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800550 int ret = 0;
551
552 mutex_lock(&dev->struct_mutex);
553
554 if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
555 mutex_unlock(&dev->struct_mutex);
556 return drm_mmap(filp, vma);
557 }
558
559 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
560 if (!map ||
561 ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
562 ret = -EPERM;
563 goto out_unlock;
564 }
565
566 /* Check for valid size. */
567 if (map->size < vma->vm_end - vma->vm_start) {
568 ret = -EINVAL;
569 goto out_unlock;
570 }
571
572 obj = map->handle;
573 if (!obj->dev->driver->gem_vm_ops) {
574 ret = -EINVAL;
575 goto out_unlock;
576 }
577
578 vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
579 vma->vm_ops = obj->dev->driver->gem_vm_ops;
580 vma->vm_private_data = map->handle;
Jeremy Fitzhardinge79cc3042009-11-17 14:08:54 -0800581 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800582
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800583 /* Take a ref for this mapping of the object, so that the fault
584 * handler can dereference the mmap offset's pointer to the object.
585 * This reference is cleaned up by the corresponding vm_close
586 * (which should happen whether the vma was created by this call, or
587 * by a vm_open due to mremap or partial unmap or whatever).
588 */
589 drm_gem_object_reference(obj);
590
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800591 vma->vm_file = filp; /* Needed for drm_vm_open() */
592 drm_vm_open_locked(vma);
593
594out_unlock:
595 mutex_unlock(&dev->struct_mutex);
596
597 return ret;
598}
599EXPORT_SYMBOL(drm_gem_mmap);