Home | History | Annotate | Line # | Download | only in drm
drm_gem.c revision 1.1.1.1.2.4
      1 /*
      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 (at) 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 <linux/shmem_fs.h>
     38 #include <linux/dma-buf.h>
     39 #include <linux/err.h>
     40 #include <linux/export.h>
     41 #include <asm/bug.h>
     42 #include <drm/drmP.h>
     43 
     44 /** @file drm_gem.c
     45  *
     46  * This file provides some of the base ioctls and library routines for
     47  * the graphics memory manager implemented by each device driver.
     48  *
     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.
     56  *
     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
     59  * two major failings:
     60  * - Process limits prevent more than 1024 or so being used at a time by
     61  *   default.
     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.
     64  *
     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.
     70  */
     71 
     72 /*
     73  * We make up offsets for buffer objects so we can recognize them at
     74  * mmap time.
     75  */
     76 
     77 /* pgoff in mmap is an unsigned long, so we need to make sure that
     78  * the faked up offset will fit
     79  */
     80 
     81 #if BITS_PER_LONG == 64
     82 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
     83 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
     84 #else
     85 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
     86 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
     87 #endif
     88 
     89 /**
     90  * Initialize the GEM device fields
     91  */
     92 
     93 int
     94 drm_gem_init(struct drm_device *dev)
     95 {
     96 	struct drm_gem_mm *mm;
     97 
     98 	spin_lock_init(&dev->object_name_lock);
     99 	idr_init(&dev->object_name_idr);
    100 
    101 	mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
    102 	if (!mm) {
    103 		DRM_ERROR("out of memory\n");
    104 		return -ENOMEM;
    105 	}
    106 
    107 	dev->mm_private = mm;
    108 
    109 	if (drm_ht_create(&mm->offset_hash, 12)) {
    110 		kfree(mm);
    111 		return -ENOMEM;
    112 	}
    113 
    114 	if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
    115 			DRM_FILE_PAGE_OFFSET_SIZE)) {
    116 		drm_ht_remove(&mm->offset_hash);
    117 		kfree(mm);
    118 		return -ENOMEM;
    119 	}
    120 
    121 	return 0;
    122 }
    123 
    124 void
    125 drm_gem_destroy(struct drm_device *dev)
    126 {
    127 	struct drm_gem_mm *mm = dev->mm_private;
    128 
    129 	drm_mm_takedown(&mm->offset_manager);
    130 	drm_ht_remove(&mm->offset_hash);
    131 	kfree(mm);
    132 	dev->mm_private = NULL;
    133 }
    134 
    135 /**
    136  * Initialize an already allocated GEM object of the specified size with
    137  * shmfs backing store.
    138  */
    139 int drm_gem_object_init(struct drm_device *dev,
    140 			struct drm_gem_object *obj, size_t size)
    141 {
    142 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
    143 
    144 	obj->dev = dev;
    145 	obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
    146 	if (IS_ERR(obj->filp))
    147 		return PTR_ERR(obj->filp);
    148 
    149 	kref_init(&obj->refcount);
    150 	atomic_set(&obj->handle_count, 0);
    151 	obj->size = size;
    152 
    153 	return 0;
    154 }
    155 EXPORT_SYMBOL(drm_gem_object_init);
    156 
    157 /**
    158  * Initialize an already allocated GEM object of the specified size with
    159  * no GEM provided backing store. Instead the caller is responsible for
    160  * backing the object and handling it.
    161  */
    162 int drm_gem_private_object_init(struct drm_device *dev,
    163 			struct drm_gem_object *obj, size_t size)
    164 {
    165 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
    166 
    167 	obj->dev = dev;
    168 	obj->filp = NULL;
    169 
    170 	kref_init(&obj->refcount);
    171 	atomic_set(&obj->handle_count, 0);
    172 	obj->size = size;
    173 
    174 	return 0;
    175 }
    176 EXPORT_SYMBOL(drm_gem_private_object_init);
    177 
    178 /**
    179  * Allocate a GEM object of the specified size with shmfs backing store
    180  */
    181 struct drm_gem_object *
    182 drm_gem_object_alloc(struct drm_device *dev, size_t size)
    183 {
    184 	struct drm_gem_object *obj;
    185 
    186 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
    187 	if (!obj)
    188 		goto free;
    189 
    190 	if (drm_gem_object_init(dev, obj, size) != 0)
    191 		goto free;
    192 
    193 	if (dev->driver->gem_init_object != NULL &&
    194 	    dev->driver->gem_init_object(obj) != 0) {
    195 		goto fput;
    196 	}
    197 	return obj;
    198 fput:
    199 	/* Object_init mangles the global counters - readjust them. */
    200 	fput(obj->filp);
    201 free:
    202 	kfree(obj);
    203 	return NULL;
    204 }
    205 EXPORT_SYMBOL(drm_gem_object_alloc);
    206 
    207 #ifndef __NetBSD__			/* XXX drm prime */
    208 static void
    209 drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
    210 {
    211 	if (obj->import_attach) {
    212 		drm_prime_remove_imported_buf_handle(&filp->prime,
    213 				obj->import_attach->dmabuf);
    214 	}
    215 	if (obj->export_dma_buf) {
    216 		drm_prime_remove_imported_buf_handle(&filp->prime,
    217 				obj->export_dma_buf);
    218 	}
    219 }
    220 #endif
    221 
    222 /**
    223  * Removes the mapping from handle to filp for this object.
    224  */
    225 int
    226 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
    227 {
    228 	struct drm_device *dev;
    229 	struct drm_gem_object *obj;
    230 
    231 	/* This is gross. The idr system doesn't let us try a delete and
    232 	 * return an error code.  It just spews if you fail at deleting.
    233 	 * So, we have to grab a lock around finding the object and then
    234 	 * doing the delete on it and dropping the refcount, or the user
    235 	 * could race us to double-decrement the refcount and cause a
    236 	 * use-after-free later.  Given the frequency of our handle lookups,
    237 	 * we may want to use ida for number allocation and a hash table
    238 	 * for the pointers, anyway.
    239 	 */
    240 	spin_lock(&filp->table_lock);
    241 
    242 	/* Check if we currently have a reference on the object */
    243 	obj = idr_find(&filp->object_idr, handle);
    244 	if (obj == NULL) {
    245 		spin_unlock(&filp->table_lock);
    246 		return -EINVAL;
    247 	}
    248 	dev = obj->dev;
    249 
    250 	/* Release reference and decrement refcount. */
    251 	idr_remove(&filp->object_idr, handle);
    252 	spin_unlock(&filp->table_lock);
    253 
    254 #ifndef __NetBSD__
    255 	drm_gem_remove_prime_handles(obj, filp);
    256 #endif
    257 
    258 	if (dev->driver->gem_close_object)
    259 		dev->driver->gem_close_object(obj, filp);
    260 	drm_gem_object_handle_unreference_unlocked(obj);
    261 
    262 	return 0;
    263 }
    264 EXPORT_SYMBOL(drm_gem_handle_delete);
    265 
    266 /**
    267  * Create a handle for this object. This adds a handle reference
    268  * to the object, which includes a regular reference count. Callers
    269  * will likely want to dereference the object afterwards.
    270  */
    271 int
    272 drm_gem_handle_create(struct drm_file *file_priv,
    273 		       struct drm_gem_object *obj,
    274 		       u32 *handlep)
    275 {
    276 	struct drm_device *dev = obj->dev;
    277 	int ret;
    278 
    279 	/*
    280 	 * Get the user-visible handle using idr.
    281 	 */
    282 again:
    283 	/* ensure there is space available to allocate a handle */
    284 	if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
    285 		return -ENOMEM;
    286 
    287 	/* do the allocation under our spinlock */
    288 	spin_lock(&file_priv->table_lock);
    289 	ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
    290 	spin_unlock(&file_priv->table_lock);
    291 	if (ret == -EAGAIN)
    292 		goto again;
    293 	else if (ret)
    294 		return ret;
    295 
    296 	drm_gem_object_handle_reference(obj);
    297 
    298 	if (dev->driver->gem_open_object) {
    299 		ret = dev->driver->gem_open_object(obj, file_priv);
    300 		if (ret) {
    301 			drm_gem_handle_delete(file_priv, *handlep);
    302 			return ret;
    303 		}
    304 	}
    305 
    306 	return 0;
    307 }
    308 EXPORT_SYMBOL(drm_gem_handle_create);
    309 
    310 
    311 /**
    312  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
    313  * @obj: obj in question
    314  *
    315  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
    316  */
    317 void
    318 drm_gem_free_mmap_offset(struct drm_gem_object *obj)
    319 {
    320 	struct drm_device *dev = obj->dev;
    321 	struct drm_gem_mm *mm = dev->mm_private;
    322 	struct drm_map_list *list = &obj->map_list;
    323 
    324 	drm_ht_remove_item(&mm->offset_hash, &list->hash);
    325 	drm_mm_put_block(list->file_offset_node);
    326 	kfree(list->map);
    327 	list->map = NULL;
    328 }
    329 EXPORT_SYMBOL(drm_gem_free_mmap_offset);
    330 
    331 /**
    332  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
    333  * @obj: obj in question
    334  *
    335  * GEM memory mapping works by handing back to userspace a fake mmap offset
    336  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
    337  * up the object based on the offset and sets up the various memory mapping
    338  * structures.
    339  *
    340  * This routine allocates and attaches a fake offset for @obj.
    341  */
    342 int
    343 drm_gem_create_mmap_offset(struct drm_gem_object *obj)
    344 {
    345 	struct drm_device *dev = obj->dev;
    346 	struct drm_gem_mm *mm = dev->mm_private;
    347 	struct drm_map_list *list;
    348 	struct drm_local_map *map;
    349 	int ret;
    350 
    351 	/* Set the object up for mmap'ing */
    352 	list = &obj->map_list;
    353 	list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
    354 	if (!list->map)
    355 		return -ENOMEM;
    356 
    357 	map = list->map;
    358 	map->type = _DRM_GEM;
    359 	map->size = obj->size;
    360 	map->handle = obj;
    361 
    362 	/* Get a DRM GEM mmap offset allocated... */
    363 	list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
    364 			obj->size / PAGE_SIZE, 0, false);
    365 
    366 	if (!list->file_offset_node) {
    367 		DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
    368 		ret = -ENOSPC;
    369 		goto out_free_list;
    370 	}
    371 
    372 	list->file_offset_node = drm_mm_get_block(list->file_offset_node,
    373 			obj->size / PAGE_SIZE, 0);
    374 	if (!list->file_offset_node) {
    375 		ret = -ENOMEM;
    376 		goto out_free_list;
    377 	}
    378 
    379 	list->hash.key = list->file_offset_node->start;
    380 	ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
    381 	if (ret) {
    382 		DRM_ERROR("failed to add to map hash\n");
    383 		goto out_free_mm;
    384 	}
    385 
    386 	return 0;
    387 
    388 out_free_mm:
    389 	drm_mm_put_block(list->file_offset_node);
    390 out_free_list:
    391 	kfree(list->map);
    392 	list->map = NULL;
    393 
    394 	return ret;
    395 }
    396 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
    397 
    398 /** Returns a reference to the object named by the handle. */
    399 struct drm_gem_object *
    400 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
    401 		      u32 handle)
    402 {
    403 	struct drm_gem_object *obj;
    404 
    405 	spin_lock(&filp->table_lock);
    406 
    407 	/* Check if we currently have a reference on the object */
    408 	obj = idr_find(&filp->object_idr, handle);
    409 	if (obj == NULL) {
    410 		spin_unlock(&filp->table_lock);
    411 		return NULL;
    412 	}
    413 
    414 	drm_gem_object_reference(obj);
    415 
    416 	spin_unlock(&filp->table_lock);
    417 
    418 	return obj;
    419 }
    420 EXPORT_SYMBOL(drm_gem_object_lookup);
    421 
    422 /**
    423  * Releases the handle to an mm object.
    424  */
    425 int
    426 drm_gem_close_ioctl(struct drm_device *dev, void *data,
    427 		    struct drm_file *file_priv)
    428 {
    429 	struct drm_gem_close *args = data;
    430 	int ret;
    431 
    432 	if (!(dev->driver->driver_features & DRIVER_GEM))
    433 		return -ENODEV;
    434 
    435 	ret = drm_gem_handle_delete(file_priv, args->handle);
    436 
    437 	return ret;
    438 }
    439 
    440 /**
    441  * Create a global name for an object, returning the name.
    442  *
    443  * Note that the name does not hold a reference; when the object
    444  * is freed, the name goes away.
    445  */
    446 int
    447 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
    448 		    struct drm_file *file_priv)
    449 {
    450 	struct drm_gem_flink *args = data;
    451 	struct drm_gem_object *obj;
    452 	int ret;
    453 
    454 	if (!(dev->driver->driver_features & DRIVER_GEM))
    455 		return -ENODEV;
    456 
    457 	obj = drm_gem_object_lookup(dev, file_priv, args->handle);
    458 	if (obj == NULL)
    459 		return -ENOENT;
    460 
    461 again:
    462 	if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
    463 		ret = -ENOMEM;
    464 		goto err;
    465 	}
    466 
    467 	spin_lock(&dev->object_name_lock);
    468 	if (!obj->name) {
    469 		ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
    470 					&obj->name);
    471 		args->name = (uint64_t) obj->name;
    472 		spin_unlock(&dev->object_name_lock);
    473 
    474 		if (ret == -EAGAIN)
    475 			goto again;
    476 		else if (ret)
    477 			goto err;
    478 
    479 		/* Allocate a reference for the name table.  */
    480 		drm_gem_object_reference(obj);
    481 	} else {
    482 		args->name = (uint64_t) obj->name;
    483 		spin_unlock(&dev->object_name_lock);
    484 		ret = 0;
    485 	}
    486 
    487 err:
    488 	drm_gem_object_unreference_unlocked(obj);
    489 	return ret;
    490 }
    491 
    492 /**
    493  * Open an object using the global name, returning a handle and the size.
    494  *
    495  * This handle (of course) holds a reference to the object, so the object
    496  * will not go away until the handle is deleted.
    497  */
    498 int
    499 drm_gem_open_ioctl(struct drm_device *dev, void *data,
    500 		   struct drm_file *file_priv)
    501 {
    502 	struct drm_gem_open *args = data;
    503 	struct drm_gem_object *obj;
    504 	int ret;
    505 	u32 handle;
    506 
    507 	if (!(dev->driver->driver_features & DRIVER_GEM))
    508 		return -ENODEV;
    509 
    510 	spin_lock(&dev->object_name_lock);
    511 	obj = idr_find(&dev->object_name_idr, (int) args->name);
    512 	if (obj)
    513 		drm_gem_object_reference(obj);
    514 	spin_unlock(&dev->object_name_lock);
    515 	if (!obj)
    516 		return -ENOENT;
    517 
    518 	ret = drm_gem_handle_create(file_priv, obj, &handle);
    519 	drm_gem_object_unreference_unlocked(obj);
    520 	if (ret)
    521 		return ret;
    522 
    523 	args->handle = handle;
    524 	args->size = obj->size;
    525 
    526 	return 0;
    527 }
    528 
    529 /**
    530  * Called at device open time, sets up the structure for handling refcounting
    531  * of mm objects.
    532  */
    533 void
    534 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
    535 {
    536 	idr_init(&file_private->object_idr);
    537 	spin_lock_init(&file_private->table_lock);
    538 }
    539 
    540 /**
    541  * Called at device close to release the file's
    542  * handle references on objects.
    543  */
    544 static int
    545 drm_gem_object_release_handle(int id, void *ptr, void *data)
    546 {
    547 	struct drm_file *file_priv = data;
    548 	struct drm_gem_object *obj = ptr;
    549 	struct drm_device *dev = obj->dev;
    550 
    551 #ifndef __NetBSD__			/* XXX drm prime */
    552 	drm_gem_remove_prime_handles(obj, file_priv);
    553 #endif
    554 
    555 	if (dev->driver->gem_close_object)
    556 		dev->driver->gem_close_object(obj, file_priv);
    557 
    558 	drm_gem_object_handle_unreference_unlocked(obj);
    559 
    560 	return 0;
    561 }
    562 
    563 /**
    564  * Called at close time when the filp is going away.
    565  *
    566  * Releases any remaining references on objects by this filp.
    567  */
    568 void
    569 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
    570 {
    571 	idr_for_each(&file_private->object_idr,
    572 		     &drm_gem_object_release_handle, file_private);
    573 
    574 	idr_remove_all(&file_private->object_idr);
    575 	idr_destroy(&file_private->object_idr);
    576 }
    577 
    578 void
    579 drm_gem_object_release(struct drm_gem_object *obj)
    580 {
    581 	if (obj->filp)
    582 	    fput(obj->filp);
    583 }
    584 EXPORT_SYMBOL(drm_gem_object_release);
    585 
    586 /**
    587  * Called after the last reference to the object has been lost.
    588  * Must be called holding struct_ mutex
    589  *
    590  * Frees the object
    591  */
    592 void
    593 drm_gem_object_free(struct kref *kref)
    594 {
    595 	struct drm_gem_object *obj = (struct drm_gem_object *) kref;
    596 	struct drm_device *dev = obj->dev;
    597 
    598 	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
    599 
    600 	if (dev->driver->gem_free_object != NULL)
    601 		dev->driver->gem_free_object(obj);
    602 }
    603 EXPORT_SYMBOL(drm_gem_object_free);
    604 
    605 static void drm_gem_object_ref_bug(struct kref *list_kref)
    606 {
    607 	BUG();
    608 }
    609 
    610 /**
    611  * Called after the last handle to the object has been closed
    612  *
    613  * Removes any name for the object. Note that this must be
    614  * called before drm_gem_object_free or we'll be touching
    615  * freed memory
    616  */
    617 void drm_gem_object_handle_free(struct drm_gem_object *obj)
    618 {
    619 	struct drm_device *dev = obj->dev;
    620 
    621 	/* Remove any name for this object */
    622 	spin_lock(&dev->object_name_lock);
    623 	if (obj->name) {
    624 		idr_remove(&dev->object_name_idr, obj->name);
    625 		obj->name = 0;
    626 		spin_unlock(&dev->object_name_lock);
    627 		/*
    628 		 * The object name held a reference to this object, drop
    629 		 * that now.
    630 		*
    631 		* This cannot be the last reference, since the handle holds one too.
    632 		 */
    633 		kref_put(&obj->refcount, drm_gem_object_ref_bug);
    634 	} else
    635 		spin_unlock(&dev->object_name_lock);
    636 
    637 }
    638 EXPORT_SYMBOL(drm_gem_object_handle_free);
    639 
    640 void drm_gem_vm_open(struct vm_area_struct *vma)
    641 {
    642 	struct drm_gem_object *obj = vma->vm_private_data;
    643 
    644 	drm_gem_object_reference(obj);
    645 
    646 	mutex_lock(&obj->dev->struct_mutex);
    647 	drm_vm_open_locked(obj->dev, vma);
    648 	mutex_unlock(&obj->dev->struct_mutex);
    649 }
    650 EXPORT_SYMBOL(drm_gem_vm_open);
    651 
    652 void drm_gem_vm_close(struct vm_area_struct *vma)
    653 {
    654 	struct drm_gem_object *obj = vma->vm_private_data;
    655 	struct drm_device *dev = obj->dev;
    656 
    657 	mutex_lock(&dev->struct_mutex);
    658 	drm_vm_close_locked(obj->dev, vma);
    659 	drm_gem_object_unreference(obj);
    660 	mutex_unlock(&dev->struct_mutex);
    661 }
    662 EXPORT_SYMBOL(drm_gem_vm_close);
    663 
    664 
    665 /**
    666  * drm_gem_mmap - memory map routine for GEM objects
    667  * @filp: DRM file pointer
    668  * @vma: VMA for the area to be mapped
    669  *
    670  * If a driver supports GEM object mapping, mmap calls on the DRM file
    671  * descriptor will end up here.
    672  *
    673  * If we find the object based on the offset passed in (vma->vm_pgoff will
    674  * contain the fake offset we created when the GTT map ioctl was called on
    675  * the object), we set up the driver fault handler so that any accesses
    676  * to the object can be trapped, to perform migration, GTT binding, surface
    677  * register allocation, or performance monitoring.
    678  */
    679 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
    680 {
    681 	struct drm_file *priv = filp->private_data;
    682 	struct drm_device *dev = priv->minor->dev;
    683 	struct drm_gem_mm *mm = dev->mm_private;
    684 	struct drm_local_map *map = NULL;
    685 	struct drm_gem_object *obj;
    686 	struct drm_hash_item *hash;
    687 	int ret = 0;
    688 
    689 	if (drm_device_is_unplugged(dev))
    690 		return -ENODEV;
    691 
    692 	mutex_lock(&dev->struct_mutex);
    693 
    694 	if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
    695 		mutex_unlock(&dev->struct_mutex);
    696 		return drm_mmap(filp, vma);
    697 	}
    698 
    699 	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
    700 	if (!map ||
    701 	    ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
    702 		ret =  -EPERM;
    703 		goto out_unlock;
    704 	}
    705 
    706 	/* Check for valid size. */
    707 	if (map->size < vma->vm_end - vma->vm_start) {
    708 		ret = -EINVAL;
    709 		goto out_unlock;
    710 	}
    711 
    712 	obj = map->handle;
    713 	if (!obj->dev->driver->gem_vm_ops) {
    714 		ret = -EINVAL;
    715 		goto out_unlock;
    716 	}
    717 
    718 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
    719 	vma->vm_ops = obj->dev->driver->gem_vm_ops;
    720 	vma->vm_private_data = map->handle;
    721 	vma->vm_page_prot =  pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
    722 
    723 	/* Take a ref for this mapping of the object, so that the fault
    724 	 * handler can dereference the mmap offset's pointer to the object.
    725 	 * This reference is cleaned up by the corresponding vm_close
    726 	 * (which should happen whether the vma was created by this call, or
    727 	 * by a vm_open due to mremap or partial unmap or whatever).
    728 	 */
    729 	drm_gem_object_reference(obj);
    730 
    731 	drm_vm_open_locked(dev, vma);
    732 
    733 out_unlock:
    734 	mutex_unlock(&dev->struct_mutex);
    735 
    736 	return ret;
    737 }
    738 EXPORT_SYMBOL(drm_gem_mmap);
    739