blob: 186d62eb063b6860647ca49a4ae3a46100ca9c42 [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>
Hugh Dickins5949eac2011-06-27 16:18:18 -070037#include <linux/shmem_fs.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038#include "drmP.h"
39
40/** @file drm_gem.c
41 *
42 * This file provides some of the base ioctls and library routines for
43 * the graphics memory manager implemented by each device driver.
44 *
45 * Because various devices have different requirements in terms of
46 * synchronization and migration strategies, implementing that is left up to
47 * the driver, and all that the general API provides should be generic --
48 * allocating objects, reading/writing data with the cpu, freeing objects.
49 * Even there, platform-dependent optimizations for reading/writing data with
50 * the CPU mean we'll likely hook those out to driver-specific calls. However,
51 * the DRI2 implementation wants to have at least allocate/mmap be generic.
52 *
53 * The goal was to have swap-backed object allocation managed through
54 * struct file. However, file descriptors as handles to a struct file have
55 * two major failings:
56 * - Process limits prevent more than 1024 or so being used at a time by
57 * default.
58 * - Inability to allocate high fds will aggravate the X Server's select()
59 * handling, and likely that of many GL client applications as well.
60 *
61 * This led to a plan of using our own integer IDs (called handles, following
62 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
63 * ioctls. The objects themselves will still include the struct file so
64 * that we can transition to fds if the required kernel infrastructure shows
65 * up at a later date, and as our interface with shmfs for memory allocation.
66 */
67
Jesse Barnesa2c0a972008-11-05 10:31:53 -080068/*
69 * We make up offsets for buffer objects so we can recognize them at
70 * mmap time.
71 */
Jordan Crouse05269a32010-05-27 13:40:27 -060072
73/* pgoff in mmap is an unsigned long, so we need to make sure that
74 * the faked up offset will fit
75 */
76
77#if BITS_PER_LONG == 64
Jesse Barnesa2c0a972008-11-05 10:31:53 -080078#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
79#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
Jordan Crouse05269a32010-05-27 13:40:27 -060080#else
81#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
82#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
83#endif
Jesse Barnesa2c0a972008-11-05 10:31:53 -080084
Eric Anholt673a3942008-07-30 12:06:12 -070085/**
86 * Initialize the GEM device fields
87 */
88
89int
90drm_gem_init(struct drm_device *dev)
91{
Jesse Barnesa2c0a972008-11-05 10:31:53 -080092 struct drm_gem_mm *mm;
93
Eric Anholt673a3942008-07-30 12:06:12 -070094 spin_lock_init(&dev->object_name_lock);
95 idr_init(&dev->object_name_idr);
Jesse Barnesa2c0a972008-11-05 10:31:53 -080096
Eric Anholt9a298b22009-03-24 12:23:04 -070097 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
Jesse Barnesa2c0a972008-11-05 10:31:53 -080098 if (!mm) {
99 DRM_ERROR("out of memory\n");
100 return -ENOMEM;
101 }
102
103 dev->mm_private = mm;
104
Chris Wilson4cb81ac2011-01-12 21:11:33 +0000105 if (drm_ht_create(&mm->offset_hash, 12)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700106 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800107 return -ENOMEM;
108 }
109
110 if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
111 DRM_FILE_PAGE_OFFSET_SIZE)) {
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800112 drm_ht_remove(&mm->offset_hash);
Eric Anholt9a298b22009-03-24 12:23:04 -0700113 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800114 return -ENOMEM;
115 }
116
Eric Anholt673a3942008-07-30 12:06:12 -0700117 return 0;
118}
119
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800120void
121drm_gem_destroy(struct drm_device *dev)
122{
123 struct drm_gem_mm *mm = dev->mm_private;
124
125 drm_mm_takedown(&mm->offset_manager);
126 drm_ht_remove(&mm->offset_hash);
Eric Anholt9a298b22009-03-24 12:23:04 -0700127 kfree(mm);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800128 dev->mm_private = NULL;
129}
130
Eric Anholt673a3942008-07-30 12:06:12 -0700131/**
Alan Cox62cb70112011-06-07 14:17:51 +0100132 * Initialize an already allocated GEM object of the specified size with
Daniel Vetter1d397042010-04-09 19:05:04 +0000133 * shmfs backing store.
134 */
135int drm_gem_object_init(struct drm_device *dev,
136 struct drm_gem_object *obj, size_t size)
137{
138 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
139
140 obj->dev = dev;
141 obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
142 if (IS_ERR(obj->filp))
143 return -ENOMEM;
144
145 kref_init(&obj->refcount);
Dave Airlie29d08b32010-09-27 16:17:17 +1000146 atomic_set(&obj->handle_count, 0);
Daniel Vetter1d397042010-04-09 19:05:04 +0000147 obj->size = size;
148
Daniel Vetter1d397042010-04-09 19:05:04 +0000149 return 0;
150}
151EXPORT_SYMBOL(drm_gem_object_init);
152
153/**
Alan Cox62cb70112011-06-07 14:17:51 +0100154 * Initialize an already allocated GEM object of the specified size with
155 * no GEM provided backing store. Instead the caller is responsible for
156 * backing the object and handling it.
157 */
158int drm_gem_private_object_init(struct drm_device *dev,
159 struct drm_gem_object *obj, size_t size)
160{
161 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
162
163 obj->dev = dev;
164 obj->filp = NULL;
165
166 kref_init(&obj->refcount);
167 atomic_set(&obj->handle_count, 0);
168 obj->size = size;
169
170 return 0;
171}
172EXPORT_SYMBOL(drm_gem_private_object_init);
173
174/**
Eric Anholt673a3942008-07-30 12:06:12 -0700175 * Allocate a GEM object of the specified size with shmfs backing store
176 */
177struct drm_gem_object *
178drm_gem_object_alloc(struct drm_device *dev, size_t size)
179{
180 struct drm_gem_object *obj;
181
Robert P. J. Dayb798b1f2009-06-10 12:43:49 -0700182 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
Jiri Slaby845792d2009-07-13 23:20:21 +0200183 if (!obj)
184 goto free;
Eric Anholt673a3942008-07-30 12:06:12 -0700185
Daniel Vetter1d397042010-04-09 19:05:04 +0000186 if (drm_gem_object_init(dev, obj, size) != 0)
Jiri Slaby845792d2009-07-13 23:20:21 +0200187 goto free;
Eric Anholt673a3942008-07-30 12:06:12 -0700188
Eric Anholt673a3942008-07-30 12:06:12 -0700189 if (dev->driver->gem_init_object != NULL &&
190 dev->driver->gem_init_object(obj) != 0) {
Jiri Slaby845792d2009-07-13 23:20:21 +0200191 goto fput;
Eric Anholt673a3942008-07-30 12:06:12 -0700192 }
Eric Anholt673a3942008-07-30 12:06:12 -0700193 return obj;
Jiri Slaby845792d2009-07-13 23:20:21 +0200194fput:
Daniel Vetter1d397042010-04-09 19:05:04 +0000195 /* Object_init mangles the global counters - readjust them. */
Jiri Slaby845792d2009-07-13 23:20:21 +0200196 fput(obj->filp);
197free:
198 kfree(obj);
199 return NULL;
Eric Anholt673a3942008-07-30 12:06:12 -0700200}
201EXPORT_SYMBOL(drm_gem_object_alloc);
202
203/**
204 * Removes the mapping from handle to filp for this object.
205 */
Dave Airlieff72145b2011-02-07 12:16:14 +1000206int
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300207drm_gem_handle_delete(struct drm_file *filp, u32 handle)
Eric Anholt673a3942008-07-30 12:06:12 -0700208{
209 struct drm_device *dev;
210 struct drm_gem_object *obj;
211
212 /* This is gross. The idr system doesn't let us try a delete and
213 * return an error code. It just spews if you fail at deleting.
214 * So, we have to grab a lock around finding the object and then
215 * doing the delete on it and dropping the refcount, or the user
216 * could race us to double-decrement the refcount and cause a
217 * use-after-free later. Given the frequency of our handle lookups,
218 * we may want to use ida for number allocation and a hash table
219 * for the pointers, anyway.
220 */
221 spin_lock(&filp->table_lock);
222
223 /* Check if we currently have a reference on the object */
224 obj = idr_find(&filp->object_idr, handle);
225 if (obj == NULL) {
226 spin_unlock(&filp->table_lock);
227 return -EINVAL;
228 }
229 dev = obj->dev;
230
231 /* Release reference and decrement refcount. */
232 idr_remove(&filp->object_idr, handle);
233 spin_unlock(&filp->table_lock);
234
Ben Skeggs304eda32011-06-09 00:24:59 +0000235 if (dev->driver->gem_close_object)
236 dev->driver->gem_close_object(obj, filp);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000237 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700238
239 return 0;
240}
Dave Airlieff72145b2011-02-07 12:16:14 +1000241EXPORT_SYMBOL(drm_gem_handle_delete);
Eric Anholt673a3942008-07-30 12:06:12 -0700242
243/**
244 * Create a handle for this object. This adds a handle reference
245 * to the object, which includes a regular reference count. Callers
246 * will likely want to dereference the object afterwards.
247 */
248int
249drm_gem_handle_create(struct drm_file *file_priv,
250 struct drm_gem_object *obj,
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300251 u32 *handlep)
Eric Anholt673a3942008-07-30 12:06:12 -0700252{
Ben Skeggs304eda32011-06-09 00:24:59 +0000253 struct drm_device *dev = obj->dev;
254 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700255
256 /*
257 * Get the user-visible handle using idr.
258 */
259again:
260 /* ensure there is space available to allocate a handle */
261 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
262 return -ENOMEM;
263
264 /* do the allocation under our spinlock */
265 spin_lock(&file_priv->table_lock);
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300266 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
Eric Anholt673a3942008-07-30 12:06:12 -0700267 spin_unlock(&file_priv->table_lock);
268 if (ret == -EAGAIN)
269 goto again;
270
271 if (ret != 0)
272 return ret;
273
274 drm_gem_object_handle_reference(obj);
Ben Skeggs304eda32011-06-09 00:24:59 +0000275
276 if (dev->driver->gem_open_object) {
277 ret = dev->driver->gem_open_object(obj, file_priv);
278 if (ret) {
279 drm_gem_handle_delete(file_priv, *handlep);
280 return ret;
281 }
282 }
283
Eric Anholt673a3942008-07-30 12:06:12 -0700284 return 0;
285}
286EXPORT_SYMBOL(drm_gem_handle_create);
287
288/** Returns a reference to the object named by the handle. */
289struct drm_gem_object *
290drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300291 u32 handle)
Eric Anholt673a3942008-07-30 12:06:12 -0700292{
293 struct drm_gem_object *obj;
294
295 spin_lock(&filp->table_lock);
296
297 /* Check if we currently have a reference on the object */
298 obj = idr_find(&filp->object_idr, handle);
299 if (obj == NULL) {
300 spin_unlock(&filp->table_lock);
301 return NULL;
302 }
303
304 drm_gem_object_reference(obj);
305
306 spin_unlock(&filp->table_lock);
307
308 return obj;
309}
310EXPORT_SYMBOL(drm_gem_object_lookup);
311
312/**
313 * Releases the handle to an mm object.
314 */
315int
316drm_gem_close_ioctl(struct drm_device *dev, void *data,
317 struct drm_file *file_priv)
318{
319 struct drm_gem_close *args = data;
320 int ret;
321
322 if (!(dev->driver->driver_features & DRIVER_GEM))
323 return -ENODEV;
324
325 ret = drm_gem_handle_delete(file_priv, args->handle);
326
327 return ret;
328}
329
330/**
331 * Create a global name for an object, returning the name.
332 *
333 * Note that the name does not hold a reference; when the object
334 * is freed, the name goes away.
335 */
336int
337drm_gem_flink_ioctl(struct drm_device *dev, void *data,
338 struct drm_file *file_priv)
339{
340 struct drm_gem_flink *args = data;
341 struct drm_gem_object *obj;
342 int ret;
343
344 if (!(dev->driver->driver_features & DRIVER_GEM))
345 return -ENODEV;
346
347 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
348 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100349 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700350
351again:
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000352 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
353 ret = -ENOMEM;
354 goto err;
355 }
Eric Anholt673a3942008-07-30 12:06:12 -0700356
357 spin_lock(&dev->object_name_lock);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000358 if (!obj->name) {
359 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
360 &obj->name);
361 args->name = (uint64_t) obj->name;
Eric Anholt673a3942008-07-30 12:06:12 -0700362 spin_unlock(&dev->object_name_lock);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000363
364 if (ret == -EAGAIN)
365 goto again;
366
367 if (ret != 0)
368 goto err;
369
370 /* Allocate a reference for the name table. */
371 drm_gem_object_reference(obj);
372 } else {
373 args->name = (uint64_t) obj->name;
374 spin_unlock(&dev->object_name_lock);
375 ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700376 }
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000377
378err:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000379 drm_gem_object_unreference_unlocked(obj);
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000380 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700381}
382
383/**
384 * Open an object using the global name, returning a handle and the size.
385 *
386 * This handle (of course) holds a reference to the object, so the object
387 * will not go away until the handle is deleted.
388 */
389int
390drm_gem_open_ioctl(struct drm_device *dev, void *data,
391 struct drm_file *file_priv)
392{
393 struct drm_gem_open *args = data;
394 struct drm_gem_object *obj;
395 int ret;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300396 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700397
398 if (!(dev->driver->driver_features & DRIVER_GEM))
399 return -ENODEV;
400
401 spin_lock(&dev->object_name_lock);
402 obj = idr_find(&dev->object_name_idr, (int) args->name);
403 if (obj)
404 drm_gem_object_reference(obj);
405 spin_unlock(&dev->object_name_lock);
406 if (!obj)
407 return -ENOENT;
408
409 ret = drm_gem_handle_create(file_priv, obj, &handle);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000410 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700411 if (ret)
412 return ret;
413
414 args->handle = handle;
415 args->size = obj->size;
416
417 return 0;
418}
419
420/**
421 * Called at device open time, sets up the structure for handling refcounting
422 * of mm objects.
423 */
424void
425drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
426{
427 idr_init(&file_private->object_idr);
428 spin_lock_init(&file_private->table_lock);
429}
430
431/**
432 * Called at device close to release the file's
433 * handle references on objects.
434 */
435static int
436drm_gem_object_release_handle(int id, void *ptr, void *data)
437{
Ben Skeggs304eda32011-06-09 00:24:59 +0000438 struct drm_file *file_priv = data;
Eric Anholt673a3942008-07-30 12:06:12 -0700439 struct drm_gem_object *obj = ptr;
Ben Skeggs304eda32011-06-09 00:24:59 +0000440 struct drm_device *dev = obj->dev;
441
442 if (dev->driver->gem_close_object)
443 dev->driver->gem_close_object(obj, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700444
Luca Barbieribc9025b2010-02-09 05:49:12 +0000445 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700446
447 return 0;
448}
449
450/**
451 * Called at close time when the filp is going away.
452 *
453 * Releases any remaining references on objects by this filp.
454 */
455void
456drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
457{
Eric Anholt673a3942008-07-30 12:06:12 -0700458 idr_for_each(&file_private->object_idr,
Ben Skeggs304eda32011-06-09 00:24:59 +0000459 &drm_gem_object_release_handle, file_private);
Eric Anholt673a3942008-07-30 12:06:12 -0700460
Chris Wilsonddd3d062010-07-24 22:28:25 +0100461 idr_remove_all(&file_private->object_idr);
Eric Anholt673a3942008-07-30 12:06:12 -0700462 idr_destroy(&file_private->object_idr);
Eric Anholt673a3942008-07-30 12:06:12 -0700463}
464
Daniel Vetterfd632aa2010-04-09 19:05:05 +0000465void
466drm_gem_object_release(struct drm_gem_object *obj)
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000467{
Alan Cox62cb70112011-06-07 14:17:51 +0100468 if (obj->filp)
469 fput(obj->filp);
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000470}
Daniel Vetterfd632aa2010-04-09 19:05:05 +0000471EXPORT_SYMBOL(drm_gem_object_release);
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000472
Eric Anholt673a3942008-07-30 12:06:12 -0700473/**
474 * Called after the last reference to the object has been lost.
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000475 * Must be called holding struct_ mutex
Eric Anholt673a3942008-07-30 12:06:12 -0700476 *
477 * Frees the object
478 */
479void
480drm_gem_object_free(struct kref *kref)
481{
482 struct drm_gem_object *obj = (struct drm_gem_object *) kref;
483 struct drm_device *dev = obj->dev;
484
485 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
486
487 if (dev->driver->gem_free_object != NULL)
488 dev->driver->gem_free_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700489}
490EXPORT_SYMBOL(drm_gem_object_free);
491
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000492static void drm_gem_object_ref_bug(struct kref *list_kref)
493{
494 BUG();
495}
496
497/**
Eric Anholt673a3942008-07-30 12:06:12 -0700498 * Called after the last handle to the object has been closed
499 *
500 * Removes any name for the object. Note that this must be
501 * called before drm_gem_object_free or we'll be touching
502 * freed memory
503 */
Dave Airlie29d08b32010-09-27 16:17:17 +1000504void drm_gem_object_handle_free(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -0700505{
Eric Anholt673a3942008-07-30 12:06:12 -0700506 struct drm_device *dev = obj->dev;
507
508 /* Remove any name for this object */
509 spin_lock(&dev->object_name_lock);
510 if (obj->name) {
511 idr_remove(&dev->object_name_idr, obj->name);
Chris Wilson8d59bae2009-02-11 14:26:28 +0000512 obj->name = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700513 spin_unlock(&dev->object_name_lock);
514 /*
515 * The object name held a reference to this object, drop
516 * that now.
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000517 *
518 * This cannot be the last reference, since the handle holds one too.
Eric Anholt673a3942008-07-30 12:06:12 -0700519 */
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000520 kref_put(&obj->refcount, drm_gem_object_ref_bug);
Eric Anholt673a3942008-07-30 12:06:12 -0700521 } else
522 spin_unlock(&dev->object_name_lock);
523
524}
525EXPORT_SYMBOL(drm_gem_object_handle_free);
526
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800527void drm_gem_vm_open(struct vm_area_struct *vma)
528{
529 struct drm_gem_object *obj = vma->vm_private_data;
530
531 drm_gem_object_reference(obj);
Chris Wilson31dfbc92010-09-27 21:28:30 +0100532
533 mutex_lock(&obj->dev->struct_mutex);
534 drm_vm_open_locked(vma);
535 mutex_unlock(&obj->dev->struct_mutex);
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800536}
537EXPORT_SYMBOL(drm_gem_vm_open);
538
539void drm_gem_vm_close(struct vm_area_struct *vma)
540{
541 struct drm_gem_object *obj = vma->vm_private_data;
Chris Wilsonb74ad5a2011-03-17 22:33:33 +0000542 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800543
Chris Wilsonb74ad5a2011-03-17 22:33:33 +0000544 mutex_lock(&dev->struct_mutex);
Chris Wilson31dfbc92010-09-27 21:28:30 +0100545 drm_vm_close_locked(vma);
546 drm_gem_object_unreference(obj);
Chris Wilsonb74ad5a2011-03-17 22:33:33 +0000547 mutex_unlock(&dev->struct_mutex);
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800548}
549EXPORT_SYMBOL(drm_gem_vm_close);
550
551
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800552/**
553 * drm_gem_mmap - memory map routine for GEM objects
554 * @filp: DRM file pointer
555 * @vma: VMA for the area to be mapped
556 *
557 * If a driver supports GEM object mapping, mmap calls on the DRM file
558 * descriptor will end up here.
559 *
560 * If we find the object based on the offset passed in (vma->vm_pgoff will
561 * contain the fake offset we created when the GTT map ioctl was called on
562 * the object), we set up the driver fault handler so that any accesses
563 * to the object can be trapped, to perform migration, GTT binding, surface
564 * register allocation, or performance monitoring.
565 */
566int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
567{
568 struct drm_file *priv = filp->private_data;
569 struct drm_device *dev = priv->minor->dev;
570 struct drm_gem_mm *mm = dev->mm_private;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100571 struct drm_local_map *map = NULL;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800572 struct drm_gem_object *obj;
573 struct drm_hash_item *hash;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800574 int ret = 0;
575
576 mutex_lock(&dev->struct_mutex);
577
578 if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
579 mutex_unlock(&dev->struct_mutex);
580 return drm_mmap(filp, vma);
581 }
582
583 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
584 if (!map ||
585 ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
586 ret = -EPERM;
587 goto out_unlock;
588 }
589
590 /* Check for valid size. */
591 if (map->size < vma->vm_end - vma->vm_start) {
592 ret = -EINVAL;
593 goto out_unlock;
594 }
595
596 obj = map->handle;
597 if (!obj->dev->driver->gem_vm_ops) {
598 ret = -EINVAL;
599 goto out_unlock;
600 }
601
602 vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
603 vma->vm_ops = obj->dev->driver->gem_vm_ops;
604 vma->vm_private_data = map->handle;
Jeremy Fitzhardinge79cc3042009-11-17 14:08:54 -0800605 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800606
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800607 /* Take a ref for this mapping of the object, so that the fault
608 * handler can dereference the mmap offset's pointer to the object.
609 * This reference is cleaned up by the corresponding vm_close
610 * (which should happen whether the vma was created by this call, or
611 * by a vm_open due to mremap or partial unmap or whatever).
612 */
613 drm_gem_object_reference(obj);
614
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800615 vma->vm_file = filp; /* Needed for drm_vm_open() */
616 drm_vm_open_locked(vma);
617
618out_unlock:
619 mutex_unlock(&dev->struct_mutex);
620
621 return ret;
622}
623EXPORT_SYMBOL(drm_gem_mmap);