blob: bad3359663981ea8e90ccf2c292b37c1172202a5 [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 */
Jordan Crouse05269a32010-05-27 13:40:27 -060071
72/* pgoff in mmap is an unsigned long, so we need to make sure that
73 * the faked up offset will fit
74 */
75
76#if BITS_PER_LONG == 64
Jesse Barnesa2c0a972008-11-05 10:31:53 -080077#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
78#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
Jordan Crouse05269a32010-05-27 13:40:27 -060079#else
80#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
81#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
82#endif
Jesse Barnesa2c0a972008-11-05 10:31:53 -080083
Eric Anholt673a3942008-07-30 12:06:12 -070084/**
85 * Initialize the GEM device fields
86 */
87
88int
89drm_gem_init(struct drm_device *dev)
90{
Jesse Barnesa2c0a972008-11-05 10:31:53 -080091 struct drm_gem_mm *mm;
92
Eric Anholt673a3942008-07-30 12:06:12 -070093 spin_lock_init(&dev->object_name_lock);
94 idr_init(&dev->object_name_idr);
Jesse Barnesa2c0a972008-11-05 10:31:53 -080095
Eric Anholt9a298b22009-03-24 12:23:04 -070096 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
Jesse Barnesa2c0a972008-11-05 10:31:53 -080097 if (!mm) {
98 DRM_ERROR("out of memory\n");
99 return -ENOMEM;
100 }
101
102 dev->mm_private = mm;
103
Chris Wilson4cb81ac2011-01-12 21:11:33 +0000104 if (drm_ht_create(&mm->offset_hash, 12)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700105 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800106 return -ENOMEM;
107 }
108
109 if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
110 DRM_FILE_PAGE_OFFSET_SIZE)) {
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800111 drm_ht_remove(&mm->offset_hash);
Eric Anholt9a298b22009-03-24 12:23:04 -0700112 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800113 return -ENOMEM;
114 }
115
Eric Anholt673a3942008-07-30 12:06:12 -0700116 return 0;
117}
118
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800119void
120drm_gem_destroy(struct drm_device *dev)
121{
122 struct drm_gem_mm *mm = dev->mm_private;
123
124 drm_mm_takedown(&mm->offset_manager);
125 drm_ht_remove(&mm->offset_hash);
Eric Anholt9a298b22009-03-24 12:23:04 -0700126 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800127 dev->mm_private = NULL;
128}
129
Eric Anholt673a3942008-07-30 12:06:12 -0700130/**
Daniel Vetter1d397042010-04-09 19:05:04 +0000131 * Initialize an already allocate GEM object of the specified size with
132 * shmfs backing store.
133 */
134int drm_gem_object_init(struct drm_device *dev,
135 struct drm_gem_object *obj, size_t size)
136{
137 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
138
139 obj->dev = dev;
140 obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
141 if (IS_ERR(obj->filp))
142 return -ENOMEM;
143
144 kref_init(&obj->refcount);
Dave Airlie29d08b32010-09-27 16:17:17 +1000145 atomic_set(&obj->handle_count, 0);
Daniel Vetter1d397042010-04-09 19:05:04 +0000146 obj->size = size;
147
Daniel Vetter1d397042010-04-09 19:05:04 +0000148 return 0;
149}
150EXPORT_SYMBOL(drm_gem_object_init);
151
152/**
Eric Anholt673a3942008-07-30 12:06:12 -0700153 * Allocate a GEM object of the specified size with shmfs backing store
154 */
155struct drm_gem_object *
156drm_gem_object_alloc(struct drm_device *dev, size_t size)
157{
158 struct drm_gem_object *obj;
159
Robert P. J. Dayb798b1f2009-06-10 12:43:49 -0700160 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
Jiri Slaby845792d2009-07-13 23:20:21 +0200161 if (!obj)
162 goto free;
Eric Anholt673a3942008-07-30 12:06:12 -0700163
Daniel Vetter1d397042010-04-09 19:05:04 +0000164 if (drm_gem_object_init(dev, obj, size) != 0)
Jiri Slaby845792d2009-07-13 23:20:21 +0200165 goto free;
Eric Anholt673a3942008-07-30 12:06:12 -0700166
Eric Anholt673a3942008-07-30 12:06:12 -0700167 if (dev->driver->gem_init_object != NULL &&
168 dev->driver->gem_init_object(obj) != 0) {
Jiri Slaby845792d2009-07-13 23:20:21 +0200169 goto fput;
Eric Anholt673a3942008-07-30 12:06:12 -0700170 }
Eric Anholt673a3942008-07-30 12:06:12 -0700171 return obj;
Jiri Slaby845792d2009-07-13 23:20:21 +0200172fput:
Daniel Vetter1d397042010-04-09 19:05:04 +0000173 /* Object_init mangles the global counters - readjust them. */
Jiri Slaby845792d2009-07-13 23:20:21 +0200174 fput(obj->filp);
175free:
176 kfree(obj);
177 return NULL;
Eric Anholt673a3942008-07-30 12:06:12 -0700178}
179EXPORT_SYMBOL(drm_gem_object_alloc);
180
181/**
182 * Removes the mapping from handle to filp for this object.
183 */
Dave Airlieff72145b2011-02-07 12:16:14 +1000184int
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300185drm_gem_handle_delete(struct drm_file *filp, u32 handle)
Eric Anholt673a3942008-07-30 12:06:12 -0700186{
187 struct drm_device *dev;
188 struct drm_gem_object *obj;
189
190 /* This is gross. The idr system doesn't let us try a delete and
191 * return an error code. It just spews if you fail at deleting.
192 * So, we have to grab a lock around finding the object and then
193 * doing the delete on it and dropping the refcount, or the user
194 * could race us to double-decrement the refcount and cause a
195 * use-after-free later. Given the frequency of our handle lookups,
196 * we may want to use ida for number allocation and a hash table
197 * for the pointers, anyway.
198 */
199 spin_lock(&filp->table_lock);
200
201 /* Check if we currently have a reference on the object */
202 obj = idr_find(&filp->object_idr, handle);
203 if (obj == NULL) {
204 spin_unlock(&filp->table_lock);
205 return -EINVAL;
206 }
207 dev = obj->dev;
208
209 /* Release reference and decrement refcount. */
210 idr_remove(&filp->object_idr, handle);
211 spin_unlock(&filp->table_lock);
212
Ben Skeggs304eda32011-06-09 00:24:59 +0000213 if (dev->driver->gem_close_object)
214 dev->driver->gem_close_object(obj, filp);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000215 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700216
217 return 0;
218}
Dave Airlieff72145b2011-02-07 12:16:14 +1000219EXPORT_SYMBOL(drm_gem_handle_delete);
Eric Anholt673a3942008-07-30 12:06:12 -0700220
221/**
222 * Create a handle for this object. This adds a handle reference
223 * to the object, which includes a regular reference count. Callers
224 * will likely want to dereference the object afterwards.
225 */
226int
227drm_gem_handle_create(struct drm_file *file_priv,
228 struct drm_gem_object *obj,
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300229 u32 *handlep)
Eric Anholt673a3942008-07-30 12:06:12 -0700230{
Ben Skeggs304eda32011-06-09 00:24:59 +0000231 struct drm_device *dev = obj->dev;
232 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700233
234 /*
235 * Get the user-visible handle using idr.
236 */
237again:
238 /* ensure there is space available to allocate a handle */
239 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
240 return -ENOMEM;
241
242 /* do the allocation under our spinlock */
243 spin_lock(&file_priv->table_lock);
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300244 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
Eric Anholt673a3942008-07-30 12:06:12 -0700245 spin_unlock(&file_priv->table_lock);
246 if (ret == -EAGAIN)
247 goto again;
248
249 if (ret != 0)
250 return ret;
251
252 drm_gem_object_handle_reference(obj);
Ben Skeggs304eda32011-06-09 00:24:59 +0000253
254 if (dev->driver->gem_open_object) {
255 ret = dev->driver->gem_open_object(obj, file_priv);
256 if (ret) {
257 drm_gem_handle_delete(file_priv, *handlep);
258 return ret;
259 }
260 }
261
Eric Anholt673a3942008-07-30 12:06:12 -0700262 return 0;
263}
264EXPORT_SYMBOL(drm_gem_handle_create);
265
266/** Returns a reference to the object named by the handle. */
267struct drm_gem_object *
268drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300269 u32 handle)
Eric Anholt673a3942008-07-30 12:06:12 -0700270{
271 struct drm_gem_object *obj;
272
273 spin_lock(&filp->table_lock);
274
275 /* Check if we currently have a reference on the object */
276 obj = idr_find(&filp->object_idr, handle);
277 if (obj == NULL) {
278 spin_unlock(&filp->table_lock);
279 return NULL;
280 }
281
282 drm_gem_object_reference(obj);
283
284 spin_unlock(&filp->table_lock);
285
286 return obj;
287}
288EXPORT_SYMBOL(drm_gem_object_lookup);
289
290/**
291 * Releases the handle to an mm object.
292 */
293int
294drm_gem_close_ioctl(struct drm_device *dev, void *data,
295 struct drm_file *file_priv)
296{
297 struct drm_gem_close *args = data;
298 int ret;
299
300 if (!(dev->driver->driver_features & DRIVER_GEM))
301 return -ENODEV;
302
303 ret = drm_gem_handle_delete(file_priv, args->handle);
304
305 return ret;
306}
307
308/**
309 * Create a global name for an object, returning the name.
310 *
311 * Note that the name does not hold a reference; when the object
312 * is freed, the name goes away.
313 */
314int
315drm_gem_flink_ioctl(struct drm_device *dev, void *data,
316 struct drm_file *file_priv)
317{
318 struct drm_gem_flink *args = data;
319 struct drm_gem_object *obj;
320 int ret;
321
322 if (!(dev->driver->driver_features & DRIVER_GEM))
323 return -ENODEV;
324
325 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
326 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100327 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700328
329again:
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000330 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
331 ret = -ENOMEM;
332 goto err;
333 }
Eric Anholt673a3942008-07-30 12:06:12 -0700334
335 spin_lock(&dev->object_name_lock);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000336 if (!obj->name) {
337 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
338 &obj->name);
339 args->name = (uint64_t) obj->name;
Eric Anholt673a3942008-07-30 12:06:12 -0700340 spin_unlock(&dev->object_name_lock);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000341
342 if (ret == -EAGAIN)
343 goto again;
344
345 if (ret != 0)
346 goto err;
347
348 /* Allocate a reference for the name table. */
349 drm_gem_object_reference(obj);
350 } else {
351 args->name = (uint64_t) obj->name;
352 spin_unlock(&dev->object_name_lock);
353 ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700354 }
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000355
356err:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000357 drm_gem_object_unreference_unlocked(obj);
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000358 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700359}
360
361/**
362 * Open an object using the global name, returning a handle and the size.
363 *
364 * This handle (of course) holds a reference to the object, so the object
365 * will not go away until the handle is deleted.
366 */
367int
368drm_gem_open_ioctl(struct drm_device *dev, void *data,
369 struct drm_file *file_priv)
370{
371 struct drm_gem_open *args = data;
372 struct drm_gem_object *obj;
373 int ret;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300374 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700375
376 if (!(dev->driver->driver_features & DRIVER_GEM))
377 return -ENODEV;
378
379 spin_lock(&dev->object_name_lock);
380 obj = idr_find(&dev->object_name_idr, (int) args->name);
381 if (obj)
382 drm_gem_object_reference(obj);
383 spin_unlock(&dev->object_name_lock);
384 if (!obj)
385 return -ENOENT;
386
387 ret = drm_gem_handle_create(file_priv, obj, &handle);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000388 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700389 if (ret)
390 return ret;
391
392 args->handle = handle;
393 args->size = obj->size;
394
395 return 0;
396}
397
398/**
399 * Called at device open time, sets up the structure for handling refcounting
400 * of mm objects.
401 */
402void
403drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
404{
405 idr_init(&file_private->object_idr);
406 spin_lock_init(&file_private->table_lock);
407}
408
409/**
410 * Called at device close to release the file's
411 * handle references on objects.
412 */
413static int
414drm_gem_object_release_handle(int id, void *ptr, void *data)
415{
Ben Skeggs304eda32011-06-09 00:24:59 +0000416 struct drm_file *file_priv = data;
Eric Anholt673a3942008-07-30 12:06:12 -0700417 struct drm_gem_object *obj = ptr;
Ben Skeggs304eda32011-06-09 00:24:59 +0000418 struct drm_device *dev = obj->dev;
419
420 if (dev->driver->gem_close_object)
421 dev->driver->gem_close_object(obj, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700422
Luca Barbieribc9025b2010-02-09 05:49:12 +0000423 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700424
425 return 0;
426}
427
428/**
429 * Called at close time when the filp is going away.
430 *
431 * Releases any remaining references on objects by this filp.
432 */
433void
434drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
435{
Eric Anholt673a3942008-07-30 12:06:12 -0700436 idr_for_each(&file_private->object_idr,
Ben Skeggs304eda32011-06-09 00:24:59 +0000437 &drm_gem_object_release_handle, file_private);
Eric Anholt673a3942008-07-30 12:06:12 -0700438
Chris Wilsonddd3d062010-07-24 22:28:25 +0100439 idr_remove_all(&file_private->object_idr);
Eric Anholt673a3942008-07-30 12:06:12 -0700440 idr_destroy(&file_private->object_idr);
Eric Anholt673a3942008-07-30 12:06:12 -0700441}
442
Daniel Vetterfd632aa2010-04-09 19:05:05 +0000443void
444drm_gem_object_release(struct drm_gem_object *obj)
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000445{
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000446 fput(obj->filp);
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000447}
Daniel Vetterfd632aa2010-04-09 19:05:05 +0000448EXPORT_SYMBOL(drm_gem_object_release);
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000449
Eric Anholt673a3942008-07-30 12:06:12 -0700450/**
451 * Called after the last reference to the object has been lost.
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000452 * Must be called holding struct_ mutex
Eric Anholt673a3942008-07-30 12:06:12 -0700453 *
454 * Frees the object
455 */
456void
457drm_gem_object_free(struct kref *kref)
458{
459 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
460 struct drm_device *dev = obj->dev;
461
462 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
463
464 if (dev->driver->gem_free_object != NULL)
465 dev->driver->gem_free_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700466}
467EXPORT_SYMBOL(drm_gem_object_free);
468
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000469static 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 */
Dave Airlie29d08b32010-09-27 16:17:17 +1000481void drm_gem_object_handle_free(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -0700482{
Eric Anholt673a3942008-07-30 12:06:12 -0700483 struct drm_device *dev = obj->dev;
484
485 /* Remove any name for this object */
486 spin_lock(&dev->object_name_lock);
487 if (obj->name) {
488 idr_remove(&dev->object_name_idr, obj->name);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000489 obj->name = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700490 spin_unlock(&dev->object_name_lock);
491 /*
492 * The object name held a reference to this object, drop
493 * that now.
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000494 *
495 * This cannot be the last reference, since the handle holds one too.
Eric Anholt673a3942008-07-30 12:06:12 -0700496 */
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000497 kref_put(&obj->refcount, drm_gem_object_ref_bug);
Eric Anholt673a3942008-07-30 12:06:12 -0700498 } else
499 spin_unlock(&dev->object_name_lock);
500
501}
502EXPORT_SYMBOL(drm_gem_object_handle_free);
503
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800504void drm_gem_vm_open(struct vm_area_struct *vma)
505{
506 struct drm_gem_object *obj = vma->vm_private_data;
507
508 drm_gem_object_reference(obj);
Chris Wilson31dfbc92010-09-27 21:28:30 +0100509
510 mutex_lock(&obj->dev->struct_mutex);
511 drm_vm_open_locked(vma);
512 mutex_unlock(&obj->dev->struct_mutex);
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800513}
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;
Chris Wilsonb74ad5a2011-03-17 22:33:33 +0000519 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800520
Chris Wilsonb74ad5a2011-03-17 22:33:33 +0000521 mutex_lock(&dev->struct_mutex);
Chris Wilson31dfbc92010-09-27 21:28:30 +0100522 drm_vm_close_locked(vma);
523 drm_gem_object_unreference(obj);
Chris Wilsonb74ad5a2011-03-17 22:33:33 +0000524 mutex_unlock(&dev->struct_mutex);
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800525}
526EXPORT_SYMBOL(drm_gem_vm_close);
527
528
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800529/**
530 * drm_gem_mmap - memory map routine for GEM objects
531 * @filp: DRM file pointer
532 * @vma: VMA for the area to be mapped
533 *
534 * If a driver supports GEM object mapping, mmap calls on the DRM file
535 * descriptor will end up here.
536 *
537 * If we find the object based on the offset passed in (vma->vm_pgoff will
538 * contain the fake offset we created when the GTT map ioctl was called on
539 * the object), we set up the driver fault handler so that any accesses
540 * to the object can be trapped, to perform migration, GTT binding, surface
541 * register allocation, or performance monitoring.
542 */
543int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
544{
545 struct drm_file *priv = filp->private_data;
546 struct drm_device *dev = priv->minor->dev;
547 struct drm_gem_mm *mm = dev->mm_private;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100548 struct drm_local_map *map = NULL;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800549 struct drm_gem_object *obj;
550 struct drm_hash_item *hash;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800551 int ret = 0;
552
553 mutex_lock(&dev->struct_mutex);
554
555 if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
556 mutex_unlock(&dev->struct_mutex);
557 return drm_mmap(filp, vma);
558 }
559
560 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
561 if (!map ||
562 ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
563 ret = -EPERM;
564 goto out_unlock;
565 }
566
567 /* Check for valid size. */
568 if (map->size < vma->vm_end - vma->vm_start) {
569 ret = -EINVAL;
570 goto out_unlock;
571 }
572
573 obj = map->handle;
574 if (!obj->dev->driver->gem_vm_ops) {
575 ret = -EINVAL;
576 goto out_unlock;
577 }
578
579 vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
580 vma->vm_ops = obj->dev->driver->gem_vm_ops;
581 vma->vm_private_data = map->handle;
Jeremy Fitzhardinge79cc3042009-11-17 14:08:54 -0800582 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800583
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800584 /* Take a ref for this mapping of the object, so that the fault
585 * handler can dereference the mmap offset's pointer to the object.
586 * This reference is cleaned up by the corresponding vm_close
587 * (which should happen whether the vma was created by this call, or
588 * by a vm_open due to mremap or partial unmap or whatever).
589 */
590 drm_gem_object_reference(obj);
591
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800592 vma->vm_file = filp; /* Needed for drm_vm_open() */
593 drm_vm_open_locked(vma);
594
595out_unlock:
596 mutex_unlock(&dev->struct_mutex);
597
598 return ret;
599}
600EXPORT_SYMBOL(drm_gem_mmap);