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