Home | History | Annotate | Line # | Download | only in drm
drm_gem.c revision 1.2.2.1
      1      1.1  riastrad /*
      2      1.1  riastrad  * Copyright  2008 Intel Corporation
      3      1.1  riastrad  *
      4      1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      5      1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      6      1.1  riastrad  * to deal in the Software without restriction, including without limitation
      7      1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8      1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
      9      1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     10      1.1  riastrad  *
     11      1.1  riastrad  * The above copyright notice and this permission notice (including the next
     12      1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     13      1.1  riastrad  * Software.
     14      1.1  riastrad  *
     15      1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16      1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17      1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18      1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19      1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20      1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21      1.1  riastrad  * IN THE SOFTWARE.
     22      1.1  riastrad  *
     23      1.1  riastrad  * Authors:
     24      1.1  riastrad  *    Eric Anholt <eric (at) anholt.net>
     25      1.1  riastrad  *
     26      1.1  riastrad  */
     27      1.1  riastrad 
     28      1.1  riastrad #include <linux/types.h>
     29      1.1  riastrad #include <linux/slab.h>
     30      1.1  riastrad #include <linux/mm.h>
     31      1.1  riastrad #include <linux/uaccess.h>
     32      1.1  riastrad #include <linux/fs.h>
     33      1.1  riastrad #include <linux/file.h>
     34      1.1  riastrad #include <linux/module.h>
     35      1.1  riastrad #include <linux/mman.h>
     36      1.1  riastrad #include <linux/pagemap.h>
     37      1.1  riastrad #include <linux/shmem_fs.h>
     38      1.1  riastrad #include <linux/dma-buf.h>
     39      1.2  riastrad #include <linux/err.h>
     40      1.2  riastrad #include <linux/export.h>
     41      1.2  riastrad #include <asm/bug.h>
     42      1.1  riastrad #include <drm/drmP.h>
     43  1.2.2.1       tls #include <drm/drm_vma_manager.h>
     44  1.2.2.1       tls 
     45  1.2.2.1       tls #ifdef __NetBSD__
     46  1.2.2.1       tls #include <uvm/uvm_extern.h>
     47  1.2.2.1       tls #endif
     48      1.1  riastrad 
     49      1.1  riastrad /** @file drm_gem.c
     50      1.1  riastrad  *
     51      1.1  riastrad  * This file provides some of the base ioctls and library routines for
     52      1.1  riastrad  * the graphics memory manager implemented by each device driver.
     53      1.1  riastrad  *
     54      1.1  riastrad  * Because various devices have different requirements in terms of
     55      1.1  riastrad  * synchronization and migration strategies, implementing that is left up to
     56      1.1  riastrad  * the driver, and all that the general API provides should be generic --
     57      1.1  riastrad  * allocating objects, reading/writing data with the cpu, freeing objects.
     58      1.1  riastrad  * Even there, platform-dependent optimizations for reading/writing data with
     59      1.1  riastrad  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
     60      1.1  riastrad  * the DRI2 implementation wants to have at least allocate/mmap be generic.
     61      1.1  riastrad  *
     62      1.1  riastrad  * The goal was to have swap-backed object allocation managed through
     63      1.1  riastrad  * struct file.  However, file descriptors as handles to a struct file have
     64      1.1  riastrad  * two major failings:
     65      1.1  riastrad  * - Process limits prevent more than 1024 or so being used at a time by
     66      1.1  riastrad  *   default.
     67      1.1  riastrad  * - Inability to allocate high fds will aggravate the X Server's select()
     68      1.1  riastrad  *   handling, and likely that of many GL client applications as well.
     69      1.1  riastrad  *
     70      1.1  riastrad  * This led to a plan of using our own integer IDs (called handles, following
     71      1.1  riastrad  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
     72      1.1  riastrad  * ioctls.  The objects themselves will still include the struct file so
     73      1.1  riastrad  * that we can transition to fds if the required kernel infrastructure shows
     74      1.1  riastrad  * up at a later date, and as our interface with shmfs for memory allocation.
     75      1.1  riastrad  */
     76      1.1  riastrad 
     77      1.1  riastrad /*
     78      1.1  riastrad  * We make up offsets for buffer objects so we can recognize them at
     79      1.1  riastrad  * mmap time.
     80      1.1  riastrad  */
     81      1.1  riastrad 
     82      1.1  riastrad /* pgoff in mmap is an unsigned long, so we need to make sure that
     83      1.1  riastrad  * the faked up offset will fit
     84      1.1  riastrad  */
     85      1.1  riastrad 
     86      1.1  riastrad #if BITS_PER_LONG == 64
     87      1.1  riastrad #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
     88      1.1  riastrad #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
     89      1.1  riastrad #else
     90      1.1  riastrad #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
     91      1.1  riastrad #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
     92      1.1  riastrad #endif
     93      1.1  riastrad 
     94      1.1  riastrad /**
     95  1.2.2.1       tls  * drm_gem_init - Initialize the GEM device fields
     96  1.2.2.1       tls  * @dev: drm_devic structure to initialize
     97      1.1  riastrad  */
     98      1.1  riastrad int
     99      1.1  riastrad drm_gem_init(struct drm_device *dev)
    100      1.1  riastrad {
    101  1.2.2.1       tls 	struct drm_vma_offset_manager *vma_offset_manager;
    102      1.1  riastrad 
    103  1.2.2.1       tls #ifdef __NetBSD__
    104  1.2.2.1       tls 	linux_mutex_init(&dev->object_name_lock);
    105  1.2.2.1       tls #else
    106  1.2.2.1       tls 	mutex_init(&dev->object_name_lock);
    107  1.2.2.1       tls #endif
    108      1.1  riastrad 	idr_init(&dev->object_name_idr);
    109      1.1  riastrad 
    110  1.2.2.1       tls 	vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
    111  1.2.2.1       tls 	if (!vma_offset_manager) {
    112      1.1  riastrad 		DRM_ERROR("out of memory\n");
    113      1.1  riastrad 		return -ENOMEM;
    114      1.1  riastrad 	}
    115      1.1  riastrad 
    116  1.2.2.1       tls 	dev->vma_offset_manager = vma_offset_manager;
    117  1.2.2.1       tls 	drm_vma_offset_manager_init(vma_offset_manager,
    118  1.2.2.1       tls 				    DRM_FILE_PAGE_OFFSET_START,
    119  1.2.2.1       tls 				    DRM_FILE_PAGE_OFFSET_SIZE);
    120      1.1  riastrad 
    121      1.1  riastrad 	return 0;
    122      1.1  riastrad }
    123      1.1  riastrad 
    124      1.1  riastrad void
    125      1.1  riastrad drm_gem_destroy(struct drm_device *dev)
    126      1.1  riastrad {
    127      1.1  riastrad 
    128  1.2.2.1       tls 	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
    129  1.2.2.1       tls 	kfree(dev->vma_offset_manager);
    130  1.2.2.1       tls 	dev->vma_offset_manager = NULL;
    131      1.2  riastrad 
    132      1.2  riastrad 	idr_destroy(&dev->object_name_idr);
    133      1.2  riastrad #ifdef __NetBSD__
    134  1.2.2.1       tls 	linux_mutex_destroy(&dev->object_name_lock);
    135      1.2  riastrad #endif
    136      1.1  riastrad }
    137      1.1  riastrad 
    138      1.1  riastrad /**
    139  1.2.2.1       tls  * drm_gem_object_init - initialize an allocated shmem-backed GEM object
    140  1.2.2.1       tls  * @dev: drm_device the object should be initialized for
    141  1.2.2.1       tls  * @obj: drm_gem_object to initialize
    142  1.2.2.1       tls  * @size: object size
    143  1.2.2.1       tls  *
    144      1.1  riastrad  * Initialize an already allocated GEM object of the specified size with
    145      1.1  riastrad  * shmfs backing store.
    146      1.1  riastrad  */
    147      1.1  riastrad int drm_gem_object_init(struct drm_device *dev,
    148      1.1  riastrad 			struct drm_gem_object *obj, size_t size)
    149      1.1  riastrad {
    150  1.2.2.1       tls #ifndef __NetBSD__
    151  1.2.2.1       tls 	struct file *filp;
    152  1.2.2.1       tls #endif
    153  1.2.2.1       tls 
    154  1.2.2.1       tls 	drm_gem_private_object_init(dev, obj, size);
    155      1.1  riastrad 
    156      1.2  riastrad #ifdef __NetBSD__
    157      1.2  riastrad 	obj->gemo_shm_uao = uao_create(size, 0);
    158      1.2  riastrad #else
    159  1.2.2.1       tls 	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
    160  1.2.2.1       tls 	if (IS_ERR(filp))
    161  1.2.2.1       tls 		return PTR_ERR(filp);
    162      1.1  riastrad 
    163  1.2.2.1       tls 	obj->filp = filp;
    164  1.2.2.1       tls #endif
    165      1.1  riastrad 
    166      1.1  riastrad 	return 0;
    167      1.1  riastrad }
    168      1.1  riastrad EXPORT_SYMBOL(drm_gem_object_init);
    169      1.1  riastrad 
    170      1.1  riastrad /**
    171  1.2.2.1       tls  * drm_gem_object_init - initialize an allocated private GEM object
    172  1.2.2.1       tls  * @dev: drm_device the object should be initialized for
    173  1.2.2.1       tls  * @obj: drm_gem_object to initialize
    174  1.2.2.1       tls  * @size: object size
    175  1.2.2.1       tls  *
    176      1.1  riastrad  * Initialize an already allocated GEM object of the specified size with
    177      1.1  riastrad  * no GEM provided backing store. Instead the caller is responsible for
    178      1.1  riastrad  * backing the object and handling it.
    179      1.1  riastrad  */
    180  1.2.2.1       tls void drm_gem_private_object_init(struct drm_device *dev,
    181  1.2.2.1       tls 				 struct drm_gem_object *obj, size_t size)
    182      1.1  riastrad {
    183      1.1  riastrad 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
    184      1.1  riastrad 
    185      1.1  riastrad 	obj->dev = dev;
    186      1.2  riastrad #ifdef __NetBSD__
    187      1.2  riastrad 	obj->gemo_shm_uao = NULL;
    188      1.2  riastrad 	KASSERT(drm_core_check_feature(dev, DRIVER_GEM));
    189      1.2  riastrad 	KASSERT(dev->driver->gem_uvm_ops != NULL);
    190      1.2  riastrad 	uvm_obj_init(&obj->gemo_uvmobj, dev->driver->gem_uvm_ops, true, 1);
    191      1.2  riastrad #else
    192      1.1  riastrad 	obj->filp = NULL;
    193      1.2  riastrad #endif
    194      1.1  riastrad 
    195      1.1  riastrad 	kref_init(&obj->refcount);
    196  1.2.2.1       tls 	obj->handle_count = 0;
    197      1.1  riastrad 	obj->size = size;
    198  1.2.2.1       tls #ifdef __NetBSD__
    199  1.2.2.1       tls 	drm_vma_node_init(&obj->vma_node);
    200  1.2.2.1       tls #else
    201  1.2.2.1       tls 	drm_vma_node_reset(&obj->vma_node);
    202  1.2.2.1       tls #endif
    203      1.1  riastrad }
    204      1.1  riastrad EXPORT_SYMBOL(drm_gem_private_object_init);
    205      1.1  riastrad 
    206  1.2.2.1       tls static void
    207  1.2.2.1       tls drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
    208  1.2.2.1       tls {
    209  1.2.2.1       tls #ifndef __NetBSD__		/* XXX drm prime */
    210  1.2.2.1       tls 	/*
    211  1.2.2.1       tls 	 * Note: obj->dma_buf can't disappear as long as we still hold a
    212  1.2.2.1       tls 	 * handle reference in obj->handle_count.
    213  1.2.2.1       tls 	 */
    214  1.2.2.1       tls 	mutex_lock(&filp->prime.lock);
    215  1.2.2.1       tls 	if (obj->dma_buf) {
    216  1.2.2.1       tls 		drm_prime_remove_buf_handle_locked(&filp->prime,
    217  1.2.2.1       tls 						   obj->dma_buf);
    218  1.2.2.1       tls 	}
    219  1.2.2.1       tls 	mutex_unlock(&filp->prime.lock);
    220  1.2.2.1       tls #endif
    221  1.2.2.1       tls }
    222  1.2.2.1       tls 
    223      1.1  riastrad /**
    224  1.2.2.1       tls  * drm_gem_object_free - release resources bound to userspace handles
    225  1.2.2.1       tls  * @obj: GEM object to clean up.
    226  1.2.2.1       tls  *
    227  1.2.2.1       tls  * Called after the last handle to the object has been closed
    228  1.2.2.1       tls  *
    229  1.2.2.1       tls  * Removes any name for the object. Note that this must be
    230  1.2.2.1       tls  * called before drm_gem_object_free or we'll be touching
    231  1.2.2.1       tls  * freed memory
    232      1.1  riastrad  */
    233  1.2.2.1       tls static void drm_gem_object_handle_free(struct drm_gem_object *obj)
    234      1.1  riastrad {
    235  1.2.2.1       tls 	struct drm_device *dev = obj->dev;
    236      1.1  riastrad 
    237  1.2.2.1       tls 	/* Remove any name for this object */
    238  1.2.2.1       tls 	if (obj->name) {
    239  1.2.2.1       tls 		idr_remove(&dev->object_name_idr, obj->name);
    240  1.2.2.1       tls 		obj->name = 0;
    241      1.1  riastrad 	}
    242      1.1  riastrad }
    243      1.1  riastrad 
    244  1.2.2.1       tls static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
    245      1.1  riastrad {
    246  1.2.2.1       tls #ifndef __NetBSD__
    247  1.2.2.1       tls 	/* Unbreak the reference cycle if we have an exported dma_buf. */
    248  1.2.2.1       tls 	if (obj->dma_buf) {
    249  1.2.2.1       tls 		dma_buf_put(obj->dma_buf);
    250  1.2.2.1       tls 		obj->dma_buf = NULL;
    251      1.1  riastrad 	}
    252  1.2.2.1       tls #endif
    253  1.2.2.1       tls }
    254  1.2.2.1       tls 
    255  1.2.2.1       tls static void
    256  1.2.2.1       tls drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
    257  1.2.2.1       tls {
    258  1.2.2.1       tls 	if (WARN_ON(obj->handle_count == 0))
    259  1.2.2.1       tls 		return;
    260  1.2.2.1       tls 
    261  1.2.2.1       tls 	/*
    262  1.2.2.1       tls 	* Must bump handle count first as this may be the last
    263  1.2.2.1       tls 	* ref, in which case the object would disappear before we
    264  1.2.2.1       tls 	* checked for a name
    265  1.2.2.1       tls 	*/
    266  1.2.2.1       tls 
    267  1.2.2.1       tls 	mutex_lock(&obj->dev->object_name_lock);
    268  1.2.2.1       tls 	if (--obj->handle_count == 0) {
    269  1.2.2.1       tls 		drm_gem_object_handle_free(obj);
    270  1.2.2.1       tls 		drm_gem_object_exported_dma_buf_free(obj);
    271      1.1  riastrad 	}
    272  1.2.2.1       tls 	mutex_unlock(&obj->dev->object_name_lock);
    273  1.2.2.1       tls 
    274  1.2.2.1       tls 	drm_gem_object_unreference_unlocked(obj);
    275      1.1  riastrad }
    276      1.1  riastrad 
    277      1.1  riastrad /**
    278  1.2.2.1       tls  * drm_gem_handle_delete - deletes the given file-private handle
    279  1.2.2.1       tls  * @filp: drm file-private structure to use for the handle look up
    280  1.2.2.1       tls  * @handle: userspace handle to delete
    281  1.2.2.1       tls  *
    282  1.2.2.1       tls  * Removes the GEM handle from the @filp lookup table and if this is the last
    283  1.2.2.1       tls  * handle also cleans up linked resources like GEM names.
    284      1.1  riastrad  */
    285      1.1  riastrad int
    286      1.1  riastrad drm_gem_handle_delete(struct drm_file *filp, u32 handle)
    287      1.1  riastrad {
    288      1.1  riastrad 	struct drm_device *dev;
    289      1.1  riastrad 	struct drm_gem_object *obj;
    290      1.1  riastrad 
    291      1.1  riastrad 	/* This is gross. The idr system doesn't let us try a delete and
    292      1.1  riastrad 	 * return an error code.  It just spews if you fail at deleting.
    293      1.1  riastrad 	 * So, we have to grab a lock around finding the object and then
    294      1.1  riastrad 	 * doing the delete on it and dropping the refcount, or the user
    295      1.1  riastrad 	 * could race us to double-decrement the refcount and cause a
    296      1.1  riastrad 	 * use-after-free later.  Given the frequency of our handle lookups,
    297      1.1  riastrad 	 * we may want to use ida for number allocation and a hash table
    298      1.1  riastrad 	 * for the pointers, anyway.
    299      1.1  riastrad 	 */
    300      1.1  riastrad 	spin_lock(&filp->table_lock);
    301      1.1  riastrad 
    302      1.1  riastrad 	/* Check if we currently have a reference on the object */
    303      1.1  riastrad 	obj = idr_find(&filp->object_idr, handle);
    304      1.1  riastrad 	if (obj == NULL) {
    305      1.1  riastrad 		spin_unlock(&filp->table_lock);
    306      1.1  riastrad 		return -EINVAL;
    307      1.1  riastrad 	}
    308      1.1  riastrad 	dev = obj->dev;
    309      1.1  riastrad 
    310      1.1  riastrad 	/* Release reference and decrement refcount. */
    311      1.1  riastrad 	idr_remove(&filp->object_idr, handle);
    312      1.1  riastrad 	spin_unlock(&filp->table_lock);
    313      1.1  riastrad 
    314  1.2.2.1       tls 	if (drm_core_check_feature(dev, DRIVER_PRIME))
    315  1.2.2.1       tls 		drm_gem_remove_prime_handles(obj, filp);
    316  1.2.2.1       tls 	drm_vma_node_revoke(&obj->vma_node, filp->filp);
    317      1.1  riastrad 
    318      1.1  riastrad 	if (dev->driver->gem_close_object)
    319      1.1  riastrad 		dev->driver->gem_close_object(obj, filp);
    320      1.1  riastrad 	drm_gem_object_handle_unreference_unlocked(obj);
    321      1.1  riastrad 
    322      1.1  riastrad 	return 0;
    323      1.1  riastrad }
    324      1.1  riastrad EXPORT_SYMBOL(drm_gem_handle_delete);
    325      1.1  riastrad 
    326      1.1  riastrad /**
    327  1.2.2.1       tls  * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
    328  1.2.2.1       tls  * @file: drm file-private structure to remove the dumb handle from
    329  1.2.2.1       tls  * @dev: corresponding drm_device
    330  1.2.2.1       tls  * @handle: the dumb handle to remove
    331  1.2.2.1       tls  *
    332  1.2.2.1       tls  * This implements the ->dumb_destroy kms driver callback for drivers which use
    333  1.2.2.1       tls  * gem to manage their backing storage.
    334  1.2.2.1       tls  */
    335  1.2.2.1       tls int drm_gem_dumb_destroy(struct drm_file *file,
    336  1.2.2.1       tls 			 struct drm_device *dev,
    337  1.2.2.1       tls 			 uint32_t handle)
    338  1.2.2.1       tls {
    339  1.2.2.1       tls 	return drm_gem_handle_delete(file, handle);
    340  1.2.2.1       tls }
    341  1.2.2.1       tls EXPORT_SYMBOL(drm_gem_dumb_destroy);
    342  1.2.2.1       tls 
    343  1.2.2.1       tls /**
    344  1.2.2.1       tls  * drm_gem_handle_create_tail - internal functions to create a handle
    345  1.2.2.1       tls  * @file_priv: drm file-private structure to register the handle for
    346  1.2.2.1       tls  * @obj: object to register
    347  1.2.2.1       tls  * @handlep: pionter to return the created handle to the caller
    348  1.2.2.1       tls  *
    349  1.2.2.1       tls  * This expects the dev->object_name_lock to be held already and will drop it
    350  1.2.2.1       tls  * before returning. Used to avoid races in establishing new handles when
    351  1.2.2.1       tls  * importing an object from either an flink name or a dma-buf.
    352      1.1  riastrad  */
    353      1.1  riastrad int
    354  1.2.2.1       tls drm_gem_handle_create_tail(struct drm_file *file_priv,
    355  1.2.2.1       tls 			   struct drm_gem_object *obj,
    356  1.2.2.1       tls 			   u32 *handlep)
    357      1.1  riastrad {
    358      1.1  riastrad 	struct drm_device *dev = obj->dev;
    359      1.1  riastrad 	int ret;
    360      1.1  riastrad 
    361  1.2.2.1       tls 	WARN_ON(!mutex_is_locked(&dev->object_name_lock));
    362  1.2.2.1       tls 
    363      1.1  riastrad 	/*
    364  1.2.2.1       tls 	 * Get the user-visible handle using idr.  Preload and perform
    365  1.2.2.1       tls 	 * allocation under our spinlock.
    366      1.1  riastrad 	 */
    367  1.2.2.1       tls 	idr_preload(GFP_KERNEL);
    368      1.1  riastrad 	spin_lock(&file_priv->table_lock);
    369  1.2.2.1       tls 
    370  1.2.2.1       tls 	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
    371  1.2.2.1       tls 	drm_gem_object_reference(obj);
    372  1.2.2.1       tls 	obj->handle_count++;
    373      1.1  riastrad 	spin_unlock(&file_priv->table_lock);
    374  1.2.2.1       tls 	idr_preload_end();
    375  1.2.2.1       tls 	mutex_unlock(&dev->object_name_lock);
    376  1.2.2.1       tls 	if (ret < 0) {
    377  1.2.2.1       tls 		drm_gem_object_handle_unreference_unlocked(obj);
    378      1.1  riastrad 		return ret;
    379  1.2.2.1       tls 	}
    380  1.2.2.1       tls 	*handlep = ret;
    381      1.1  riastrad 
    382  1.2.2.1       tls 	ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
    383  1.2.2.1       tls 	if (ret) {
    384  1.2.2.1       tls 		drm_gem_handle_delete(file_priv, *handlep);
    385  1.2.2.1       tls 		return ret;
    386  1.2.2.1       tls 	}
    387      1.1  riastrad 
    388      1.1  riastrad 	if (dev->driver->gem_open_object) {
    389      1.1  riastrad 		ret = dev->driver->gem_open_object(obj, file_priv);
    390      1.1  riastrad 		if (ret) {
    391      1.1  riastrad 			drm_gem_handle_delete(file_priv, *handlep);
    392      1.1  riastrad 			return ret;
    393      1.1  riastrad 		}
    394      1.1  riastrad 	}
    395      1.1  riastrad 
    396      1.1  riastrad 	return 0;
    397      1.1  riastrad }
    398  1.2.2.1       tls 
    399  1.2.2.1       tls /**
    400  1.2.2.1       tls  * gem_handle_create - create a gem handle for an object
    401  1.2.2.1       tls  * @file_priv: drm file-private structure to register the handle for
    402  1.2.2.1       tls  * @obj: object to register
    403  1.2.2.1       tls  * @handlep: pionter to return the created handle to the caller
    404  1.2.2.1       tls  *
    405  1.2.2.1       tls  * Create a handle for this object. This adds a handle reference
    406  1.2.2.1       tls  * to the object, which includes a regular reference count. Callers
    407  1.2.2.1       tls  * will likely want to dereference the object afterwards.
    408  1.2.2.1       tls  */
    409  1.2.2.1       tls int
    410  1.2.2.1       tls drm_gem_handle_create(struct drm_file *file_priv,
    411  1.2.2.1       tls 		       struct drm_gem_object *obj,
    412  1.2.2.1       tls 		       u32 *handlep)
    413  1.2.2.1       tls {
    414  1.2.2.1       tls 	mutex_lock(&obj->dev->object_name_lock);
    415  1.2.2.1       tls 
    416  1.2.2.1       tls 	return drm_gem_handle_create_tail(file_priv, obj, handlep);
    417  1.2.2.1       tls }
    418      1.1  riastrad EXPORT_SYMBOL(drm_gem_handle_create);
    419      1.1  riastrad 
    420      1.1  riastrad 
    421      1.1  riastrad /**
    422      1.1  riastrad  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
    423      1.1  riastrad  * @obj: obj in question
    424      1.1  riastrad  *
    425      1.1  riastrad  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
    426      1.1  riastrad  */
    427      1.1  riastrad void
    428      1.1  riastrad drm_gem_free_mmap_offset(struct drm_gem_object *obj)
    429      1.1  riastrad {
    430      1.1  riastrad 	struct drm_device *dev = obj->dev;
    431      1.1  riastrad 
    432  1.2.2.1       tls 	drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
    433      1.1  riastrad }
    434      1.1  riastrad EXPORT_SYMBOL(drm_gem_free_mmap_offset);
    435      1.1  riastrad 
    436      1.1  riastrad /**
    437  1.2.2.1       tls  * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
    438      1.1  riastrad  * @obj: obj in question
    439  1.2.2.1       tls  * @size: the virtual size
    440      1.1  riastrad  *
    441      1.1  riastrad  * GEM memory mapping works by handing back to userspace a fake mmap offset
    442      1.1  riastrad  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
    443      1.1  riastrad  * up the object based on the offset and sets up the various memory mapping
    444      1.1  riastrad  * structures.
    445      1.1  riastrad  *
    446  1.2.2.1       tls  * This routine allocates and attaches a fake offset for @obj, in cases where
    447  1.2.2.1       tls  * the virtual size differs from the physical size (ie. obj->size).  Otherwise
    448  1.2.2.1       tls  * just use drm_gem_create_mmap_offset().
    449      1.1  riastrad  */
    450      1.1  riastrad int
    451  1.2.2.1       tls drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
    452      1.1  riastrad {
    453      1.1  riastrad 	struct drm_device *dev = obj->dev;
    454  1.2.2.1       tls 
    455  1.2.2.1       tls 	return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
    456  1.2.2.1       tls 				  size / PAGE_SIZE);
    457  1.2.2.1       tls }
    458  1.2.2.1       tls EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
    459  1.2.2.1       tls 
    460  1.2.2.1       tls /**
    461  1.2.2.1       tls  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
    462  1.2.2.1       tls  * @obj: obj in question
    463  1.2.2.1       tls  *
    464  1.2.2.1       tls  * GEM memory mapping works by handing back to userspace a fake mmap offset
    465  1.2.2.1       tls  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
    466  1.2.2.1       tls  * up the object based on the offset and sets up the various memory mapping
    467  1.2.2.1       tls  * structures.
    468  1.2.2.1       tls  *
    469  1.2.2.1       tls  * This routine allocates and attaches a fake offset for @obj.
    470  1.2.2.1       tls  */
    471  1.2.2.1       tls int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
    472  1.2.2.1       tls {
    473  1.2.2.1       tls 	return drm_gem_create_mmap_offset_size(obj, obj->size);
    474  1.2.2.1       tls }
    475  1.2.2.1       tls EXPORT_SYMBOL(drm_gem_create_mmap_offset);
    476  1.2.2.1       tls 
    477  1.2.2.1       tls /**
    478  1.2.2.1       tls  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
    479  1.2.2.1       tls  * from shmem
    480  1.2.2.1       tls  * @obj: obj in question
    481  1.2.2.1       tls  * @gfpmask: gfp mask of requested pages
    482  1.2.2.1       tls  */
    483  1.2.2.1       tls #ifdef __NetBSD__
    484  1.2.2.1       tls struct page **
    485  1.2.2.1       tls drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask)
    486  1.2.2.1       tls {
    487  1.2.2.1       tls 	struct pglist pglist;
    488  1.2.2.1       tls 	struct vm_page *vm_page;
    489  1.2.2.1       tls 	struct page **pages;
    490  1.2.2.1       tls 	unsigned i;
    491      1.1  riastrad 	int ret;
    492      1.1  riastrad 
    493  1.2.2.1       tls 	KASSERT((obj->size & (PAGE_SIZE - 1)) != 0);
    494      1.1  riastrad 
    495  1.2.2.1       tls 	pages = drm_malloc_ab(obj->size >> PAGE_SHIFT, sizeof(*pages));
    496  1.2.2.1       tls 	if (pages == NULL) {
    497      1.1  riastrad 		ret = -ENOMEM;
    498  1.2.2.1       tls 		goto fail0;
    499      1.1  riastrad 	}
    500      1.1  riastrad 
    501  1.2.2.1       tls 	TAILQ_INIT(&pglist);
    502  1.2.2.1       tls 	/* XXX errno NetBSD->Linux */
    503  1.2.2.1       tls 	ret = -uvm_obj_wirepages(obj->gemo_shm_uao, 0, obj->size, &pglist);
    504  1.2.2.1       tls 	if (ret)
    505  1.2.2.1       tls 		goto fail1;
    506  1.2.2.1       tls 
    507  1.2.2.1       tls 	i = 0;
    508  1.2.2.1       tls 	TAILQ_FOREACH(vm_page, &pglist, pageq.queue)
    509  1.2.2.1       tls 		pages[i++] = container_of(vm_page, struct page, p_vmp);
    510  1.2.2.1       tls 
    511  1.2.2.1       tls 	return pages;
    512  1.2.2.1       tls 
    513  1.2.2.1       tls fail1:	drm_free_large(pages);
    514  1.2.2.1       tls fail0:	return ERR_PTR(ret);
    515  1.2.2.1       tls }
    516  1.2.2.1       tls #else
    517  1.2.2.1       tls struct page **drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask)
    518  1.2.2.1       tls {
    519  1.2.2.1       tls 	struct inode *inode;
    520  1.2.2.1       tls 	struct address_space *mapping;
    521  1.2.2.1       tls 	struct page *p, **pages;
    522  1.2.2.1       tls 	int i, npages;
    523  1.2.2.1       tls 
    524  1.2.2.1       tls 	/* This is the shared memory object that backs the GEM resource */
    525  1.2.2.1       tls 	inode = file_inode(obj->filp);
    526  1.2.2.1       tls 	mapping = inode->i_mapping;
    527  1.2.2.1       tls 
    528  1.2.2.1       tls 	/* We already BUG_ON() for non-page-aligned sizes in
    529  1.2.2.1       tls 	 * drm_gem_object_init(), so we should never hit this unless
    530  1.2.2.1       tls 	 * driver author is doing something really wrong:
    531  1.2.2.1       tls 	 */
    532  1.2.2.1       tls 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
    533  1.2.2.1       tls 
    534  1.2.2.1       tls 	npages = obj->size >> PAGE_SHIFT;
    535  1.2.2.1       tls 
    536  1.2.2.1       tls 	pages = drm_malloc_ab(npages, sizeof(struct page *));
    537  1.2.2.1       tls 	if (pages == NULL)
    538  1.2.2.1       tls 		return ERR_PTR(-ENOMEM);
    539  1.2.2.1       tls 
    540  1.2.2.1       tls 	gfpmask |= mapping_gfp_mask(mapping);
    541  1.2.2.1       tls 
    542  1.2.2.1       tls 	for (i = 0; i < npages; i++) {
    543  1.2.2.1       tls 		p = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
    544  1.2.2.1       tls 		if (IS_ERR(p))
    545  1.2.2.1       tls 			goto fail;
    546  1.2.2.1       tls 		pages[i] = p;
    547  1.2.2.1       tls 
    548  1.2.2.1       tls 		/* There is a hypothetical issue w/ drivers that require
    549  1.2.2.1       tls 		 * buffer memory in the low 4GB.. if the pages are un-
    550  1.2.2.1       tls 		 * pinned, and swapped out, they can end up swapped back
    551  1.2.2.1       tls 		 * in above 4GB.  If pages are already in memory, then
    552  1.2.2.1       tls 		 * shmem_read_mapping_page_gfp will ignore the gfpmask,
    553  1.2.2.1       tls 		 * even if the already in-memory page disobeys the mask.
    554  1.2.2.1       tls 		 *
    555  1.2.2.1       tls 		 * It is only a theoretical issue today, because none of
    556  1.2.2.1       tls 		 * the devices with this limitation can be populated with
    557  1.2.2.1       tls 		 * enough memory to trigger the issue.  But this BUG_ON()
    558  1.2.2.1       tls 		 * is here as a reminder in case the problem with
    559  1.2.2.1       tls 		 * shmem_read_mapping_page_gfp() isn't solved by the time
    560  1.2.2.1       tls 		 * it does become a real issue.
    561  1.2.2.1       tls 		 *
    562  1.2.2.1       tls 		 * See this thread: http://lkml.org/lkml/2011/7/11/238
    563  1.2.2.1       tls 		 */
    564  1.2.2.1       tls 		BUG_ON((gfpmask & __GFP_DMA32) &&
    565  1.2.2.1       tls 				(page_to_pfn(p) >= 0x00100000UL));
    566      1.1  riastrad 	}
    567      1.1  riastrad 
    568  1.2.2.1       tls 	return pages;
    569      1.1  riastrad 
    570  1.2.2.1       tls fail:
    571  1.2.2.1       tls 	while (i--)
    572  1.2.2.1       tls 		page_cache_release(pages[i]);
    573      1.1  riastrad 
    574  1.2.2.1       tls 	drm_free_large(pages);
    575  1.2.2.1       tls 	return ERR_CAST(p);
    576      1.1  riastrad }
    577  1.2.2.1       tls #endif
    578  1.2.2.1       tls EXPORT_SYMBOL(drm_gem_get_pages);
    579  1.2.2.1       tls 
    580  1.2.2.1       tls /**
    581  1.2.2.1       tls  * drm_gem_put_pages - helper to free backing pages for a GEM object
    582  1.2.2.1       tls  * @obj: obj in question
    583  1.2.2.1       tls  * @pages: pages to free
    584  1.2.2.1       tls  * @dirty: if true, pages will be marked as dirty
    585  1.2.2.1       tls  * @accessed: if true, the pages will be marked as accessed
    586  1.2.2.1       tls  */
    587  1.2.2.1       tls #ifdef __NetBSD__
    588  1.2.2.1       tls void
    589  1.2.2.1       tls drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, bool dirty,
    590  1.2.2.1       tls     bool accessed __unused /* XXX */)
    591  1.2.2.1       tls {
    592  1.2.2.1       tls 	unsigned i;
    593  1.2.2.1       tls 
    594  1.2.2.1       tls 	for (i = 0; i < (obj->size >> PAGE_SHIFT); i++) {
    595  1.2.2.1       tls 		if (dirty)
    596  1.2.2.1       tls 			pages[i]->p_vmp.flags &= ~PG_CLEAN;
    597  1.2.2.1       tls 	}
    598  1.2.2.1       tls 
    599  1.2.2.1       tls 	uvm_obj_unwirepages(obj->gemo_shm_uao, 0, obj->size);
    600  1.2.2.1       tls }
    601  1.2.2.1       tls #else
    602  1.2.2.1       tls void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
    603  1.2.2.1       tls 		bool dirty, bool accessed)
    604  1.2.2.1       tls {
    605  1.2.2.1       tls 	int i, npages;
    606  1.2.2.1       tls 
    607  1.2.2.1       tls 	/* We already BUG_ON() for non-page-aligned sizes in
    608  1.2.2.1       tls 	 * drm_gem_object_init(), so we should never hit this unless
    609  1.2.2.1       tls 	 * driver author is doing something really wrong:
    610  1.2.2.1       tls 	 */
    611  1.2.2.1       tls 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
    612  1.2.2.1       tls 
    613  1.2.2.1       tls 	npages = obj->size >> PAGE_SHIFT;
    614  1.2.2.1       tls 
    615  1.2.2.1       tls 	for (i = 0; i < npages; i++) {
    616  1.2.2.1       tls 		if (dirty)
    617  1.2.2.1       tls 			set_page_dirty(pages[i]);
    618  1.2.2.1       tls 
    619  1.2.2.1       tls 		if (accessed)
    620  1.2.2.1       tls 			mark_page_accessed(pages[i]);
    621  1.2.2.1       tls 
    622  1.2.2.1       tls 		/* Undo the reference we took when populating the table */
    623  1.2.2.1       tls 		page_cache_release(pages[i]);
    624  1.2.2.1       tls 	}
    625  1.2.2.1       tls 
    626  1.2.2.1       tls 	drm_free_large(pages);
    627  1.2.2.1       tls }
    628  1.2.2.1       tls #endif
    629  1.2.2.1       tls EXPORT_SYMBOL(drm_gem_put_pages);
    630      1.1  riastrad 
    631      1.1  riastrad /** Returns a reference to the object named by the handle. */
    632      1.1  riastrad struct drm_gem_object *
    633      1.1  riastrad drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
    634      1.1  riastrad 		      u32 handle)
    635      1.1  riastrad {
    636      1.1  riastrad 	struct drm_gem_object *obj;
    637      1.1  riastrad 
    638      1.1  riastrad 	spin_lock(&filp->table_lock);
    639      1.1  riastrad 
    640      1.1  riastrad 	/* Check if we currently have a reference on the object */
    641      1.1  riastrad 	obj = idr_find(&filp->object_idr, handle);
    642      1.1  riastrad 	if (obj == NULL) {
    643      1.1  riastrad 		spin_unlock(&filp->table_lock);
    644      1.1  riastrad 		return NULL;
    645      1.1  riastrad 	}
    646      1.1  riastrad 
    647      1.1  riastrad 	drm_gem_object_reference(obj);
    648      1.1  riastrad 
    649      1.1  riastrad 	spin_unlock(&filp->table_lock);
    650      1.1  riastrad 
    651      1.1  riastrad 	return obj;
    652      1.1  riastrad }
    653      1.1  riastrad EXPORT_SYMBOL(drm_gem_object_lookup);
    654      1.1  riastrad 
    655      1.1  riastrad /**
    656  1.2.2.1       tls  * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
    657  1.2.2.1       tls  * @dev: drm_device
    658  1.2.2.1       tls  * @data: ioctl data
    659  1.2.2.1       tls  * @file_priv: drm file-private structure
    660  1.2.2.1       tls  *
    661      1.1  riastrad  * Releases the handle to an mm object.
    662      1.1  riastrad  */
    663      1.1  riastrad int
    664      1.1  riastrad drm_gem_close_ioctl(struct drm_device *dev, void *data,
    665      1.1  riastrad 		    struct drm_file *file_priv)
    666      1.1  riastrad {
    667      1.1  riastrad 	struct drm_gem_close *args = data;
    668      1.1  riastrad 	int ret;
    669      1.1  riastrad 
    670      1.1  riastrad 	if (!(dev->driver->driver_features & DRIVER_GEM))
    671      1.1  riastrad 		return -ENODEV;
    672      1.1  riastrad 
    673      1.1  riastrad 	ret = drm_gem_handle_delete(file_priv, args->handle);
    674      1.1  riastrad 
    675      1.1  riastrad 	return ret;
    676      1.1  riastrad }
    677      1.1  riastrad 
    678      1.1  riastrad /**
    679  1.2.2.1       tls  * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
    680  1.2.2.1       tls  * @dev: drm_device
    681  1.2.2.1       tls  * @data: ioctl data
    682  1.2.2.1       tls  * @file_priv: drm file-private structure
    683  1.2.2.1       tls  *
    684      1.1  riastrad  * Create a global name for an object, returning the name.
    685      1.1  riastrad  *
    686      1.1  riastrad  * Note that the name does not hold a reference; when the object
    687      1.1  riastrad  * is freed, the name goes away.
    688      1.1  riastrad  */
    689      1.1  riastrad int
    690      1.1  riastrad drm_gem_flink_ioctl(struct drm_device *dev, void *data,
    691      1.1  riastrad 		    struct drm_file *file_priv)
    692      1.1  riastrad {
    693      1.1  riastrad 	struct drm_gem_flink *args = data;
    694      1.1  riastrad 	struct drm_gem_object *obj;
    695      1.1  riastrad 	int ret;
    696      1.1  riastrad 
    697      1.1  riastrad 	if (!(dev->driver->driver_features & DRIVER_GEM))
    698      1.1  riastrad 		return -ENODEV;
    699      1.1  riastrad 
    700      1.1  riastrad 	obj = drm_gem_object_lookup(dev, file_priv, args->handle);
    701      1.1  riastrad 	if (obj == NULL)
    702      1.1  riastrad 		return -ENOENT;
    703      1.1  riastrad 
    704  1.2.2.1       tls 	mutex_lock(&dev->object_name_lock);
    705  1.2.2.1       tls 	idr_preload(GFP_KERNEL);
    706  1.2.2.1       tls 	/* prevent races with concurrent gem_close. */
    707  1.2.2.1       tls 	if (obj->handle_count == 0) {
    708  1.2.2.1       tls 		ret = -ENOENT;
    709      1.1  riastrad 		goto err;
    710      1.1  riastrad 	}
    711      1.1  riastrad 
    712      1.1  riastrad 	if (!obj->name) {
    713  1.2.2.1       tls 		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
    714  1.2.2.1       tls 		if (ret < 0)
    715      1.1  riastrad 			goto err;
    716      1.1  riastrad 
    717  1.2.2.1       tls 		obj->name = ret;
    718      1.1  riastrad 	}
    719      1.1  riastrad 
    720  1.2.2.1       tls 	args->name = (uint64_t) obj->name;
    721  1.2.2.1       tls 	ret = 0;
    722  1.2.2.1       tls 
    723      1.1  riastrad err:
    724  1.2.2.1       tls 	idr_preload_end();
    725  1.2.2.1       tls 	mutex_unlock(&dev->object_name_lock);
    726      1.1  riastrad 	drm_gem_object_unreference_unlocked(obj);
    727      1.1  riastrad 	return ret;
    728      1.1  riastrad }
    729      1.1  riastrad 
    730      1.1  riastrad /**
    731  1.2.2.1       tls  * drm_gem_open - implementation of the GEM_OPEN ioctl
    732  1.2.2.1       tls  * @dev: drm_device
    733  1.2.2.1       tls  * @data: ioctl data
    734  1.2.2.1       tls  * @file_priv: drm file-private structure
    735  1.2.2.1       tls  *
    736      1.1  riastrad  * Open an object using the global name, returning a handle and the size.
    737      1.1  riastrad  *
    738      1.1  riastrad  * This handle (of course) holds a reference to the object, so the object
    739      1.1  riastrad  * will not go away until the handle is deleted.
    740      1.1  riastrad  */
    741      1.1  riastrad int
    742      1.1  riastrad drm_gem_open_ioctl(struct drm_device *dev, void *data,
    743      1.1  riastrad 		   struct drm_file *file_priv)
    744      1.1  riastrad {
    745      1.1  riastrad 	struct drm_gem_open *args = data;
    746      1.1  riastrad 	struct drm_gem_object *obj;
    747      1.1  riastrad 	int ret;
    748      1.1  riastrad 	u32 handle;
    749      1.1  riastrad 
    750      1.1  riastrad 	if (!(dev->driver->driver_features & DRIVER_GEM))
    751      1.1  riastrad 		return -ENODEV;
    752      1.1  riastrad 
    753  1.2.2.1       tls 	mutex_lock(&dev->object_name_lock);
    754      1.1  riastrad 	obj = idr_find(&dev->object_name_idr, (int) args->name);
    755  1.2.2.1       tls 	if (obj) {
    756      1.1  riastrad 		drm_gem_object_reference(obj);
    757  1.2.2.1       tls 	} else {
    758  1.2.2.1       tls 		mutex_unlock(&dev->object_name_lock);
    759      1.1  riastrad 		return -ENOENT;
    760  1.2.2.1       tls 	}
    761      1.1  riastrad 
    762  1.2.2.1       tls 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
    763  1.2.2.1       tls 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
    764      1.1  riastrad 	drm_gem_object_unreference_unlocked(obj);
    765      1.1  riastrad 	if (ret)
    766      1.1  riastrad 		return ret;
    767      1.1  riastrad 
    768      1.1  riastrad 	args->handle = handle;
    769      1.1  riastrad 	args->size = obj->size;
    770      1.1  riastrad 
    771      1.1  riastrad 	return 0;
    772      1.1  riastrad }
    773      1.1  riastrad 
    774      1.1  riastrad /**
    775  1.2.2.1       tls  * gem_gem_open - initalizes GEM file-private structures at devnode open time
    776  1.2.2.1       tls  * @dev: drm_device which is being opened by userspace
    777  1.2.2.1       tls  * @file_private: drm file-private structure to set up
    778  1.2.2.1       tls  *
    779      1.1  riastrad  * Called at device open time, sets up the structure for handling refcounting
    780      1.1  riastrad  * of mm objects.
    781      1.1  riastrad  */
    782      1.1  riastrad void
    783      1.1  riastrad drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
    784      1.1  riastrad {
    785      1.1  riastrad 	idr_init(&file_private->object_idr);
    786      1.1  riastrad 	spin_lock_init(&file_private->table_lock);
    787      1.1  riastrad }
    788      1.1  riastrad 
    789  1.2.2.1       tls /*
    790      1.1  riastrad  * Called at device close to release the file's
    791      1.1  riastrad  * handle references on objects.
    792      1.1  riastrad  */
    793      1.1  riastrad static int
    794      1.1  riastrad drm_gem_object_release_handle(int id, void *ptr, void *data)
    795      1.1  riastrad {
    796      1.1  riastrad 	struct drm_file *file_priv = data;
    797      1.1  riastrad 	struct drm_gem_object *obj = ptr;
    798      1.1  riastrad 	struct drm_device *dev = obj->dev;
    799      1.1  riastrad 
    800      1.2  riastrad #ifndef __NetBSD__			/* XXX drm prime */
    801  1.2.2.1       tls 	if (drm_core_check_feature(dev, DRIVER_PRIME))
    802  1.2.2.1       tls 		drm_gem_remove_prime_handles(obj, file_priv);
    803      1.2  riastrad #endif
    804  1.2.2.1       tls 	drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
    805      1.1  riastrad 
    806      1.1  riastrad 	if (dev->driver->gem_close_object)
    807      1.1  riastrad 		dev->driver->gem_close_object(obj, file_priv);
    808      1.1  riastrad 
    809      1.1  riastrad 	drm_gem_object_handle_unreference_unlocked(obj);
    810      1.1  riastrad 
    811      1.1  riastrad 	return 0;
    812      1.1  riastrad }
    813      1.1  riastrad 
    814      1.1  riastrad /**
    815  1.2.2.1       tls  * drm_gem_release - release file-private GEM resources
    816  1.2.2.1       tls  * @dev: drm_device which is being closed by userspace
    817  1.2.2.1       tls  * @file_private: drm file-private structure to clean up
    818  1.2.2.1       tls  *
    819      1.1  riastrad  * Called at close time when the filp is going away.
    820      1.1  riastrad  *
    821      1.1  riastrad  * Releases any remaining references on objects by this filp.
    822      1.1  riastrad  */
    823      1.1  riastrad void
    824      1.1  riastrad drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
    825      1.1  riastrad {
    826      1.1  riastrad 	idr_for_each(&file_private->object_idr,
    827      1.1  riastrad 		     &drm_gem_object_release_handle, file_private);
    828      1.1  riastrad 	idr_destroy(&file_private->object_idr);
    829      1.2  riastrad #ifdef __NetBSD__
    830      1.2  riastrad 	spin_lock_destroy(&file_private->table_lock);
    831      1.2  riastrad #endif
    832      1.1  riastrad }
    833      1.1  riastrad 
    834      1.1  riastrad void
    835      1.1  riastrad drm_gem_object_release(struct drm_gem_object *obj)
    836      1.1  riastrad {
    837  1.2.2.1       tls #ifndef __NetBSD__
    838  1.2.2.1       tls 	WARN_ON(obj->dma_buf);
    839  1.2.2.1       tls #endif
    840  1.2.2.1       tls 
    841      1.2  riastrad #ifdef __NetBSD__
    842  1.2.2.1       tls 	drm_vma_node_destroy(&obj->vma_node);
    843      1.2  riastrad 	if (obj->gemo_shm_uao)
    844      1.2  riastrad 		uao_detach(obj->gemo_shm_uao);
    845      1.2  riastrad 	uvm_obj_destroy(&obj->gemo_uvmobj, true);
    846      1.2  riastrad #else
    847      1.1  riastrad 	if (obj->filp)
    848  1.2.2.1       tls 		fput(obj->filp);
    849      1.2  riastrad #endif
    850  1.2.2.1       tls 
    851  1.2.2.1       tls 	drm_gem_free_mmap_offset(obj);
    852      1.1  riastrad }
    853      1.1  riastrad EXPORT_SYMBOL(drm_gem_object_release);
    854      1.1  riastrad 
    855      1.1  riastrad /**
    856  1.2.2.1       tls  * drm_gem_object_free - free a GEM object
    857  1.2.2.1       tls  * @kref: kref of the object to free
    858  1.2.2.1       tls  *
    859      1.1  riastrad  * Called after the last reference to the object has been lost.
    860      1.1  riastrad  * Must be called holding struct_ mutex
    861      1.1  riastrad  *
    862      1.1  riastrad  * Frees the object
    863      1.1  riastrad  */
    864      1.1  riastrad void
    865      1.1  riastrad drm_gem_object_free(struct kref *kref)
    866      1.1  riastrad {
    867      1.1  riastrad 	struct drm_gem_object *obj = (struct drm_gem_object *) kref;
    868      1.1  riastrad 	struct drm_device *dev = obj->dev;
    869      1.1  riastrad 
    870      1.1  riastrad 	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
    871      1.1  riastrad 
    872      1.1  riastrad 	if (dev->driver->gem_free_object != NULL)
    873      1.1  riastrad 		dev->driver->gem_free_object(obj);
    874      1.1  riastrad }
    875      1.1  riastrad EXPORT_SYMBOL(drm_gem_object_free);
    876      1.1  riastrad 
    877      1.2  riastrad #ifndef __NetBSD__
    878      1.2  riastrad 
    879      1.1  riastrad void drm_gem_vm_open(struct vm_area_struct *vma)
    880      1.1  riastrad {
    881      1.1  riastrad 	struct drm_gem_object *obj = vma->vm_private_data;
    882      1.1  riastrad 
    883      1.1  riastrad 	drm_gem_object_reference(obj);
    884      1.1  riastrad 
    885      1.1  riastrad 	mutex_lock(&obj->dev->struct_mutex);
    886      1.1  riastrad 	drm_vm_open_locked(obj->dev, vma);
    887      1.1  riastrad 	mutex_unlock(&obj->dev->struct_mutex);
    888      1.1  riastrad }
    889      1.1  riastrad EXPORT_SYMBOL(drm_gem_vm_open);
    890      1.1  riastrad 
    891      1.1  riastrad void drm_gem_vm_close(struct vm_area_struct *vma)
    892      1.1  riastrad {
    893      1.1  riastrad 	struct drm_gem_object *obj = vma->vm_private_data;
    894      1.1  riastrad 	struct drm_device *dev = obj->dev;
    895      1.1  riastrad 
    896      1.1  riastrad 	mutex_lock(&dev->struct_mutex);
    897      1.1  riastrad 	drm_vm_close_locked(obj->dev, vma);
    898      1.1  riastrad 	drm_gem_object_unreference(obj);
    899      1.1  riastrad 	mutex_unlock(&dev->struct_mutex);
    900      1.1  riastrad }
    901      1.1  riastrad EXPORT_SYMBOL(drm_gem_vm_close);
    902      1.1  riastrad 
    903  1.2.2.1       tls /**
    904  1.2.2.1       tls  * drm_gem_mmap_obj - memory map a GEM object
    905  1.2.2.1       tls  * @obj: the GEM object to map
    906  1.2.2.1       tls  * @obj_size: the object size to be mapped, in bytes
    907  1.2.2.1       tls  * @vma: VMA for the area to be mapped
    908  1.2.2.1       tls  *
    909  1.2.2.1       tls  * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
    910  1.2.2.1       tls  * provided by the driver. Depending on their requirements, drivers can either
    911  1.2.2.1       tls  * provide a fault handler in their gem_vm_ops (in which case any accesses to
    912  1.2.2.1       tls  * the object will be trapped, to perform migration, GTT binding, surface
    913  1.2.2.1       tls  * register allocation, or performance monitoring), or mmap the buffer memory
    914  1.2.2.1       tls  * synchronously after calling drm_gem_mmap_obj.
    915  1.2.2.1       tls  *
    916  1.2.2.1       tls  * This function is mainly intended to implement the DMABUF mmap operation, when
    917  1.2.2.1       tls  * the GEM object is not looked up based on its fake offset. To implement the
    918  1.2.2.1       tls  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
    919  1.2.2.1       tls  *
    920  1.2.2.1       tls  * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
    921  1.2.2.1       tls  * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
    922  1.2.2.1       tls  * callers must verify access restrictions before calling this helper.
    923  1.2.2.1       tls  *
    924  1.2.2.1       tls  * NOTE: This function has to be protected with dev->struct_mutex
    925  1.2.2.1       tls  *
    926  1.2.2.1       tls  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
    927  1.2.2.1       tls  * size, or if no gem_vm_ops are provided.
    928  1.2.2.1       tls  */
    929  1.2.2.1       tls int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
    930  1.2.2.1       tls 		     struct vm_area_struct *vma)
    931  1.2.2.1       tls {
    932  1.2.2.1       tls 	struct drm_device *dev = obj->dev;
    933  1.2.2.1       tls 
    934  1.2.2.1       tls 	lockdep_assert_held(&dev->struct_mutex);
    935  1.2.2.1       tls 
    936  1.2.2.1       tls 	/* Check for valid size. */
    937  1.2.2.1       tls 	if (obj_size < vma->vm_end - vma->vm_start)
    938  1.2.2.1       tls 		return -EINVAL;
    939  1.2.2.1       tls 
    940  1.2.2.1       tls 	if (!dev->driver->gem_vm_ops)
    941  1.2.2.1       tls 		return -EINVAL;
    942  1.2.2.1       tls 
    943  1.2.2.1       tls 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
    944  1.2.2.1       tls 	vma->vm_ops = dev->driver->gem_vm_ops;
    945  1.2.2.1       tls 	vma->vm_private_data = obj;
    946  1.2.2.1       tls 	vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
    947  1.2.2.1       tls 
    948  1.2.2.1       tls 	/* Take a ref for this mapping of the object, so that the fault
    949  1.2.2.1       tls 	 * handler can dereference the mmap offset's pointer to the object.
    950  1.2.2.1       tls 	 * This reference is cleaned up by the corresponding vm_close
    951  1.2.2.1       tls 	 * (which should happen whether the vma was created by this call, or
    952  1.2.2.1       tls 	 * by a vm_open due to mremap or partial unmap or whatever).
    953  1.2.2.1       tls 	 */
    954  1.2.2.1       tls 	drm_gem_object_reference(obj);
    955  1.2.2.1       tls 
    956  1.2.2.1       tls 	drm_vm_open_locked(dev, vma);
    957  1.2.2.1       tls 	return 0;
    958  1.2.2.1       tls }
    959  1.2.2.1       tls EXPORT_SYMBOL(drm_gem_mmap_obj);
    960      1.1  riastrad 
    961      1.1  riastrad /**
    962      1.1  riastrad  * drm_gem_mmap - memory map routine for GEM objects
    963      1.1  riastrad  * @filp: DRM file pointer
    964      1.1  riastrad  * @vma: VMA for the area to be mapped
    965      1.1  riastrad  *
    966      1.1  riastrad  * If a driver supports GEM object mapping, mmap calls on the DRM file
    967      1.1  riastrad  * descriptor will end up here.
    968      1.1  riastrad  *
    969  1.2.2.1       tls  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
    970      1.1  riastrad  * contain the fake offset we created when the GTT map ioctl was called on
    971  1.2.2.1       tls  * the object) and map it with a call to drm_gem_mmap_obj().
    972  1.2.2.1       tls  *
    973  1.2.2.1       tls  * If the caller is not granted access to the buffer object, the mmap will fail
    974  1.2.2.1       tls  * with EACCES. Please see the vma manager for more information.
    975      1.1  riastrad  */
    976      1.1  riastrad int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
    977      1.1  riastrad {
    978      1.1  riastrad 	struct drm_file *priv = filp->private_data;
    979      1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
    980      1.1  riastrad 	struct drm_gem_object *obj;
    981  1.2.2.1       tls 	struct drm_vma_offset_node *node;
    982  1.2.2.1       tls 	int ret;
    983      1.1  riastrad 
    984      1.1  riastrad 	if (drm_device_is_unplugged(dev))
    985      1.1  riastrad 		return -ENODEV;
    986      1.1  riastrad 
    987      1.1  riastrad 	mutex_lock(&dev->struct_mutex);
    988      1.1  riastrad 
    989  1.2.2.1       tls 	node = drm_vma_offset_exact_lookup(dev->vma_offset_manager,
    990  1.2.2.1       tls 					   vma->vm_pgoff,
    991  1.2.2.1       tls 					   vma_pages(vma));
    992  1.2.2.1       tls 	if (!node) {
    993      1.1  riastrad 		mutex_unlock(&dev->struct_mutex);
    994      1.1  riastrad 		return drm_mmap(filp, vma);
    995  1.2.2.1       tls 	} else if (!drm_vma_node_is_allowed(node, filp)) {
    996  1.2.2.1       tls 		mutex_unlock(&dev->struct_mutex);
    997  1.2.2.1       tls 		return -EACCES;
    998      1.1  riastrad 	}
    999      1.1  riastrad 
   1000  1.2.2.1       tls 	obj = container_of(node, struct drm_gem_object, vma_node);
   1001  1.2.2.1       tls 	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma);
   1002      1.1  riastrad 
   1003      1.1  riastrad 	mutex_unlock(&dev->struct_mutex);
   1004      1.1  riastrad 
   1005      1.1  riastrad 	return ret;
   1006      1.1  riastrad }
   1007      1.1  riastrad EXPORT_SYMBOL(drm_gem_mmap);
   1008      1.2  riastrad 
   1009      1.2  riastrad #endif	/* defined(__NetBSD__) */
   1010