Home | History | Annotate | Line # | Download | only in drm
drm_gem.c revision 1.16
      1  1.16  riastrad /*	$NetBSD: drm_gem.c,v 1.16 2021/12/18 23:44:57 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.16  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_gem.c,v 1.16 2021/12/18 23:44:57 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.16  riastrad #include <linux/mem_encrypt.h>
     45  1.16  riastrad #include <linux/pagevec.h>
     46  1.16  riastrad 
     47  1.16  riastrad #include <drm/drm.h>
     48  1.16  riastrad #include <drm/drm_device.h>
     49  1.16  riastrad #include <drm/drm_drv.h>
     50  1.16  riastrad #include <drm/drm_file.h>
     51  1.16  riastrad #include <drm/drm_gem.h>
     52  1.16  riastrad #include <drm/drm_print.h>
     53   1.3  riastrad #include <drm/drm_vma_manager.h>
     54  1.16  riastrad 
     55   1.7  riastrad #include "drm_internal.h"
     56   1.1  riastrad 
     57   1.4  riastrad #ifdef __NetBSD__
     58   1.4  riastrad #include <uvm/uvm_extern.h>
     59  1.13  riastrad #include <linux/nbsd-namespace.h>
     60   1.4  riastrad #endif
     61   1.4  riastrad 
     62   1.1  riastrad /** @file drm_gem.c
     63   1.1  riastrad  *
     64   1.1  riastrad  * This file provides some of the base ioctls and library routines for
     65   1.1  riastrad  * the graphics memory manager implemented by each device driver.
     66   1.1  riastrad  *
     67   1.1  riastrad  * Because various devices have different requirements in terms of
     68   1.1  riastrad  * synchronization and migration strategies, implementing that is left up to
     69   1.1  riastrad  * the driver, and all that the general API provides should be generic --
     70   1.1  riastrad  * allocating objects, reading/writing data with the cpu, freeing objects.
     71   1.1  riastrad  * Even there, platform-dependent optimizations for reading/writing data with
     72   1.1  riastrad  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
     73   1.1  riastrad  * the DRI2 implementation wants to have at least allocate/mmap be generic.
     74   1.1  riastrad  *
     75   1.1  riastrad  * The goal was to have swap-backed object allocation managed through
     76   1.1  riastrad  * struct file.  However, file descriptors as handles to a struct file have
     77   1.1  riastrad  * two major failings:
     78   1.1  riastrad  * - Process limits prevent more than 1024 or so being used at a time by
     79   1.1  riastrad  *   default.
     80   1.1  riastrad  * - Inability to allocate high fds will aggravate the X Server's select()
     81   1.1  riastrad  *   handling, and likely that of many GL client applications as well.
     82   1.1  riastrad  *
     83   1.1  riastrad  * This led to a plan of using our own integer IDs (called handles, following
     84   1.1  riastrad  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
     85   1.1  riastrad  * ioctls.  The objects themselves will still include the struct file so
     86   1.1  riastrad  * that we can transition to fds if the required kernel infrastructure shows
     87   1.1  riastrad  * up at a later date, and as our interface with shmfs for memory allocation.
     88   1.1  riastrad  */
     89   1.1  riastrad 
     90   1.1  riastrad /**
     91   1.3  riastrad  * drm_gem_init - Initialize the GEM device fields
     92   1.3  riastrad  * @dev: drm_devic structure to initialize
     93   1.1  riastrad  */
     94   1.1  riastrad int
     95   1.1  riastrad drm_gem_init(struct drm_device *dev)
     96   1.1  riastrad {
     97   1.3  riastrad 	struct drm_vma_offset_manager *vma_offset_manager;
     98   1.1  riastrad 
     99   1.3  riastrad 	mutex_init(&dev->object_name_lock);
    100  1.16  riastrad 	idr_init_base(&dev->object_name_idr, 1);
    101   1.1  riastrad 
    102   1.3  riastrad 	vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
    103   1.3  riastrad 	if (!vma_offset_manager) {
    104   1.1  riastrad 		DRM_ERROR("out of memory\n");
    105   1.1  riastrad 		return -ENOMEM;
    106   1.1  riastrad 	}
    107   1.1  riastrad 
    108   1.3  riastrad 	dev->vma_offset_manager = vma_offset_manager;
    109   1.3  riastrad 	drm_vma_offset_manager_init(vma_offset_manager,
    110   1.3  riastrad 				    DRM_FILE_PAGE_OFFSET_START,
    111   1.3  riastrad 				    DRM_FILE_PAGE_OFFSET_SIZE);
    112   1.1  riastrad 
    113   1.1  riastrad 	return 0;
    114   1.1  riastrad }
    115   1.1  riastrad 
    116   1.1  riastrad void
    117   1.1  riastrad drm_gem_destroy(struct drm_device *dev)
    118   1.1  riastrad {
    119   1.1  riastrad 
    120   1.3  riastrad 	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
    121   1.3  riastrad 	kfree(dev->vma_offset_manager);
    122   1.3  riastrad 	dev->vma_offset_manager = NULL;
    123   1.2  riastrad 
    124   1.2  riastrad 	idr_destroy(&dev->object_name_idr);
    125  1.13  riastrad 	mutex_destroy(&dev->object_name_lock);
    126   1.1  riastrad }
    127   1.1  riastrad 
    128   1.1  riastrad /**
    129   1.3  riastrad  * drm_gem_object_init - initialize an allocated shmem-backed GEM object
    130   1.3  riastrad  * @dev: drm_device the object should be initialized for
    131   1.3  riastrad  * @obj: drm_gem_object to initialize
    132   1.3  riastrad  * @size: object size
    133   1.3  riastrad  *
    134   1.1  riastrad  * Initialize an already allocated GEM object of the specified size with
    135   1.1  riastrad  * shmfs backing store.
    136   1.1  riastrad  */
    137   1.1  riastrad int drm_gem_object_init(struct drm_device *dev,
    138   1.1  riastrad 			struct drm_gem_object *obj, size_t size)
    139   1.1  riastrad {
    140   1.4  riastrad #ifndef __NetBSD__
    141   1.3  riastrad 	struct file *filp;
    142   1.4  riastrad #endif
    143   1.3  riastrad 
    144   1.3  riastrad 	drm_gem_private_object_init(dev, obj, size);
    145   1.1  riastrad 
    146   1.2  riastrad #ifdef __NetBSD__
    147   1.6  riastrad 	/*
    148   1.6  riastrad 	 * A uao may not have size 0, but a gem object may.  Allocate a
    149   1.6  riastrad 	 * spurious page so we needn't teach uao how to have size 0.
    150   1.6  riastrad 	 */
    151   1.8  riastrad 	obj->filp = uao_create(MAX(size, PAGE_SIZE), 0);
    152   1.5  riastrad 	/*
    153   1.5  riastrad 	 * XXX This is gross.  We ought to do it the other way around:
    154   1.5  riastrad 	 * set the uao to have the main uvm object's lock.  However,
    155   1.5  riastrad 	 * uvm_obj_setlock is not safe on uvm_aobjs.
    156   1.5  riastrad 	 */
    157  1.15        ad 	rw_obj_hold(obj->filp->vmobjlock);
    158   1.8  riastrad 	uvm_obj_setlock(&obj->gemo_uvmobj, obj->filp->vmobjlock);
    159   1.2  riastrad #else
    160   1.3  riastrad 	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
    161   1.3  riastrad 	if (IS_ERR(filp))
    162   1.3  riastrad 		return PTR_ERR(filp);
    163   1.3  riastrad 
    164   1.3  riastrad 	obj->filp = filp;
    165   1.2  riastrad #endif
    166   1.1  riastrad 
    167   1.1  riastrad 	return 0;
    168   1.1  riastrad }
    169   1.1  riastrad EXPORT_SYMBOL(drm_gem_object_init);
    170   1.1  riastrad 
    171   1.1  riastrad /**
    172   1.7  riastrad  * drm_gem_private_object_init - initialize an allocated private GEM object
    173   1.3  riastrad  * @dev: drm_device the object should be initialized for
    174   1.3  riastrad  * @obj: drm_gem_object to initialize
    175   1.3  riastrad  * @size: object size
    176   1.3  riastrad  *
    177   1.1  riastrad  * Initialize an already allocated GEM object of the specified size with
    178   1.1  riastrad  * no GEM provided backing store. Instead the caller is responsible for
    179   1.1  riastrad  * backing the object and handling it.
    180   1.1  riastrad  */
    181   1.3  riastrad void drm_gem_private_object_init(struct drm_device *dev,
    182   1.3  riastrad 				 struct drm_gem_object *obj, size_t size)
    183   1.1  riastrad {
    184   1.1  riastrad 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
    185   1.1  riastrad 
    186   1.1  riastrad 	obj->dev = dev;
    187   1.2  riastrad #ifdef __NetBSD__
    188   1.8  riastrad 	obj->filp = NULL;
    189   1.2  riastrad 	KASSERT(drm_core_check_feature(dev, DRIVER_GEM));
    190   1.2  riastrad 	KASSERT(dev->driver->gem_uvm_ops != NULL);
    191   1.2  riastrad 	uvm_obj_init(&obj->gemo_uvmobj, dev->driver->gem_uvm_ops, true, 1);
    192   1.2  riastrad #else
    193   1.1  riastrad 	obj->filp = NULL;
    194   1.2  riastrad #endif
    195   1.1  riastrad 
    196   1.1  riastrad 	kref_init(&obj->refcount);
    197   1.3  riastrad 	obj->handle_count = 0;
    198   1.1  riastrad 	obj->size = size;
    199   1.4  riastrad #ifdef __NetBSD__
    200   1.4  riastrad 	drm_vma_node_init(&obj->vma_node);
    201   1.4  riastrad #else
    202  1.16  riastrad 	dma_resv_init(&obj->_resv);
    203  1.16  riastrad 	if (!obj->resv)
    204  1.16  riastrad 		obj->resv = &obj->_resv;
    205  1.16  riastrad 
    206   1.3  riastrad 	drm_vma_node_reset(&obj->vma_node);
    207   1.4  riastrad #endif
    208   1.3  riastrad }
    209   1.3  riastrad EXPORT_SYMBOL(drm_gem_private_object_init);
    210   1.1  riastrad 
    211   1.3  riastrad static void
    212   1.3  riastrad drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
    213   1.3  riastrad {
    214   1.3  riastrad 	/*
    215   1.3  riastrad 	 * Note: obj->dma_buf can't disappear as long as we still hold a
    216   1.3  riastrad 	 * handle reference in obj->handle_count.
    217   1.3  riastrad 	 */
    218   1.3  riastrad 	mutex_lock(&filp->prime.lock);
    219   1.3  riastrad 	if (obj->dma_buf) {
    220   1.3  riastrad 		drm_prime_remove_buf_handle_locked(&filp->prime,
    221   1.3  riastrad 						   obj->dma_buf);
    222   1.3  riastrad 	}
    223   1.3  riastrad 	mutex_unlock(&filp->prime.lock);
    224   1.1  riastrad }
    225   1.1  riastrad 
    226   1.1  riastrad /**
    227   1.7  riastrad  * drm_gem_object_handle_free - release resources bound to userspace handles
    228   1.3  riastrad  * @obj: GEM object to clean up.
    229   1.3  riastrad  *
    230   1.3  riastrad  * Called after the last handle to the object has been closed
    231   1.3  riastrad  *
    232   1.3  riastrad  * Removes any name for the object. Note that this must be
    233   1.3  riastrad  * called before drm_gem_object_free or we'll be touching
    234   1.3  riastrad  * freed memory
    235   1.1  riastrad  */
    236   1.3  riastrad static void drm_gem_object_handle_free(struct drm_gem_object *obj)
    237   1.1  riastrad {
    238   1.3  riastrad 	struct drm_device *dev = obj->dev;
    239   1.3  riastrad 
    240   1.3  riastrad 	/* Remove any name for this object */
    241   1.3  riastrad 	if (obj->name) {
    242   1.3  riastrad 		idr_remove(&dev->object_name_idr, obj->name);
    243   1.3  riastrad 		obj->name = 0;
    244   1.3  riastrad 	}
    245   1.3  riastrad }
    246   1.1  riastrad 
    247   1.3  riastrad static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
    248   1.3  riastrad {
    249   1.4  riastrad #ifndef __NetBSD__
    250   1.3  riastrad 	/* Unbreak the reference cycle if we have an exported dma_buf. */
    251   1.3  riastrad 	if (obj->dma_buf) {
    252   1.3  riastrad 		dma_buf_put(obj->dma_buf);
    253   1.3  riastrad 		obj->dma_buf = NULL;
    254   1.1  riastrad 	}
    255   1.4  riastrad #endif
    256   1.1  riastrad }
    257   1.1  riastrad 
    258   1.1  riastrad static void
    259  1.16  riastrad drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
    260   1.1  riastrad {
    261  1.16  riastrad 	struct drm_device *dev = obj->dev;
    262  1.16  riastrad 	bool final = false;
    263  1.16  riastrad 
    264   1.3  riastrad 	if (WARN_ON(obj->handle_count == 0))
    265   1.3  riastrad 		return;
    266   1.3  riastrad 
    267   1.3  riastrad 	/*
    268   1.3  riastrad 	* Must bump handle count first as this may be the last
    269   1.3  riastrad 	* ref, in which case the object would disappear before we
    270   1.3  riastrad 	* checked for a name
    271   1.3  riastrad 	*/
    272   1.3  riastrad 
    273  1.16  riastrad 	mutex_lock(&dev->object_name_lock);
    274   1.3  riastrad 	if (--obj->handle_count == 0) {
    275   1.3  riastrad 		drm_gem_object_handle_free(obj);
    276   1.3  riastrad 		drm_gem_object_exported_dma_buf_free(obj);
    277  1.16  riastrad 		final = true;
    278   1.1  riastrad 	}
    279  1.16  riastrad 	mutex_unlock(&dev->object_name_lock);
    280   1.3  riastrad 
    281  1.16  riastrad 	if (final)
    282  1.16  riastrad 		drm_gem_object_put_unlocked(obj);
    283  1.16  riastrad }
    284  1.16  riastrad 
    285  1.16  riastrad /*
    286  1.16  riastrad  * Called at device or object close to release the file's
    287  1.16  riastrad  * handle references on objects.
    288  1.16  riastrad  */
    289  1.16  riastrad static int
    290  1.16  riastrad drm_gem_object_release_handle(int id, void *ptr, void *data)
    291  1.16  riastrad {
    292  1.16  riastrad 	struct drm_file *file_priv = data;
    293  1.16  riastrad 	struct drm_gem_object *obj = ptr;
    294  1.16  riastrad 	struct drm_device *dev = obj->dev;
    295  1.16  riastrad 
    296  1.16  riastrad 	if (obj->funcs && obj->funcs->close)
    297  1.16  riastrad 		obj->funcs->close(obj, file_priv);
    298  1.16  riastrad 	else if (dev->driver->gem_close_object)
    299  1.16  riastrad 		dev->driver->gem_close_object(obj, file_priv);
    300  1.16  riastrad 
    301  1.16  riastrad 	drm_gem_remove_prime_handles(obj, file_priv);
    302  1.16  riastrad 	drm_vma_node_revoke(&obj->vma_node, file_priv);
    303  1.16  riastrad 
    304  1.16  riastrad 	drm_gem_object_handle_put_unlocked(obj);
    305  1.16  riastrad 
    306  1.16  riastrad 	return 0;
    307   1.1  riastrad }
    308   1.1  riastrad 
    309   1.1  riastrad /**
    310   1.3  riastrad  * drm_gem_handle_delete - deletes the given file-private handle
    311   1.3  riastrad  * @filp: drm file-private structure to use for the handle look up
    312   1.3  riastrad  * @handle: userspace handle to delete
    313   1.3  riastrad  *
    314  1.16  riastrad  * Removes the GEM handle from the @filp lookup table which has been added with
    315  1.16  riastrad  * drm_gem_handle_create(). If this is the last handle also cleans up linked
    316  1.16  riastrad  * resources like GEM names.
    317   1.1  riastrad  */
    318   1.1  riastrad int
    319   1.1  riastrad drm_gem_handle_delete(struct drm_file *filp, u32 handle)
    320   1.1  riastrad {
    321   1.1  riastrad 	struct drm_gem_object *obj;
    322   1.1  riastrad 
    323   1.1  riastrad 	spin_lock(&filp->table_lock);
    324   1.1  riastrad 
    325   1.1  riastrad 	/* Check if we currently have a reference on the object */
    326  1.16  riastrad 	obj = idr_replace(&filp->object_idr, NULL, handle);
    327  1.16  riastrad 	spin_unlock(&filp->table_lock);
    328  1.16  riastrad 	if (IS_ERR_OR_NULL(obj))
    329   1.1  riastrad 		return -EINVAL;
    330   1.1  riastrad 
    331  1.16  riastrad 	/* Release driver's reference and decrement refcount. */
    332  1.16  riastrad 	drm_gem_object_release_handle(handle, obj, filp);
    333  1.16  riastrad 
    334  1.16  riastrad 	/* And finally make the handle available for future allocations. */
    335  1.16  riastrad 	spin_lock(&filp->table_lock);
    336   1.1  riastrad 	idr_remove(&filp->object_idr, handle);
    337   1.1  riastrad 	spin_unlock(&filp->table_lock);
    338   1.1  riastrad 
    339   1.1  riastrad 	return 0;
    340   1.1  riastrad }
    341   1.1  riastrad EXPORT_SYMBOL(drm_gem_handle_delete);
    342   1.1  riastrad 
    343   1.1  riastrad /**
    344  1.16  riastrad  * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
    345  1.16  riastrad  * @file: drm file-private structure containing the gem object
    346  1.16  riastrad  * @dev: corresponding drm_device
    347  1.16  riastrad  * @handle: gem object handle
    348  1.16  riastrad  * @offset: return location for the fake mmap offset
    349  1.16  riastrad  *
    350  1.16  riastrad  * This implements the &drm_driver.dumb_map_offset kms driver callback for
    351  1.16  riastrad  * drivers which use gem to manage their backing storage.
    352  1.16  riastrad  *
    353  1.16  riastrad  * Returns:
    354  1.16  riastrad  * 0 on success or a negative error code on failure.
    355  1.16  riastrad  */
    356  1.16  riastrad int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
    357  1.16  riastrad 			    u32 handle, u64 *offset)
    358  1.16  riastrad {
    359  1.16  riastrad 	struct drm_gem_object *obj;
    360  1.16  riastrad 	int ret;
    361  1.16  riastrad 
    362  1.16  riastrad 	obj = drm_gem_object_lookup(file, handle);
    363  1.16  riastrad 	if (!obj)
    364  1.16  riastrad 		return -ENOENT;
    365  1.16  riastrad 
    366  1.16  riastrad 	/* Don't allow imported objects to be mapped */
    367  1.16  riastrad 	if (obj->import_attach) {
    368  1.16  riastrad 		ret = -EINVAL;
    369  1.16  riastrad 		goto out;
    370  1.16  riastrad 	}
    371  1.16  riastrad 
    372  1.16  riastrad 	ret = drm_gem_create_mmap_offset(obj);
    373  1.16  riastrad 	if (ret)
    374  1.16  riastrad 		goto out;
    375  1.16  riastrad 
    376  1.16  riastrad 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
    377  1.16  riastrad out:
    378  1.16  riastrad 	drm_gem_object_put_unlocked(obj);
    379  1.16  riastrad 
    380  1.16  riastrad 	return ret;
    381  1.16  riastrad }
    382  1.16  riastrad EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
    383  1.16  riastrad 
    384  1.16  riastrad /**
    385   1.3  riastrad  * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
    386   1.3  riastrad  * @file: drm file-private structure to remove the dumb handle from
    387   1.3  riastrad  * @dev: corresponding drm_device
    388   1.3  riastrad  * @handle: the dumb handle to remove
    389  1.16  riastrad  *
    390  1.16  riastrad  * This implements the &drm_driver.dumb_destroy kms driver callback for drivers
    391  1.16  riastrad  * which use gem to manage their backing storage.
    392   1.3  riastrad  */
    393   1.3  riastrad int drm_gem_dumb_destroy(struct drm_file *file,
    394   1.3  riastrad 			 struct drm_device *dev,
    395   1.3  riastrad 			 uint32_t handle)
    396   1.3  riastrad {
    397   1.3  riastrad 	return drm_gem_handle_delete(file, handle);
    398   1.3  riastrad }
    399   1.3  riastrad EXPORT_SYMBOL(drm_gem_dumb_destroy);
    400   1.3  riastrad 
    401   1.3  riastrad /**
    402   1.3  riastrad  * drm_gem_handle_create_tail - internal functions to create a handle
    403   1.3  riastrad  * @file_priv: drm file-private structure to register the handle for
    404   1.3  riastrad  * @obj: object to register
    405   1.7  riastrad  * @handlep: pointer to return the created handle to the caller
    406  1.16  riastrad  *
    407  1.16  riastrad  * This expects the &drm_device.object_name_lock to be held already and will
    408  1.16  riastrad  * drop it before returning. Used to avoid races in establishing new handles
    409  1.16  riastrad  * when importing an object from either an flink name or a dma-buf.
    410  1.16  riastrad  *
    411  1.16  riastrad  * Handles must be release again through drm_gem_handle_delete(). This is done
    412  1.16  riastrad  * when userspace closes @file_priv for all attached handles, or through the
    413  1.16  riastrad  * GEM_CLOSE ioctl for individual handles.
    414   1.1  riastrad  */
    415   1.1  riastrad int
    416   1.3  riastrad drm_gem_handle_create_tail(struct drm_file *file_priv,
    417   1.3  riastrad 			   struct drm_gem_object *obj,
    418   1.3  riastrad 			   u32 *handlep)
    419   1.1  riastrad {
    420   1.1  riastrad 	struct drm_device *dev = obj->dev;
    421  1.16  riastrad 	u32 handle;
    422   1.1  riastrad 	int ret;
    423   1.1  riastrad 
    424   1.3  riastrad 	WARN_ON(!mutex_is_locked(&dev->object_name_lock));
    425  1.16  riastrad 	if (obj->handle_count++ == 0)
    426  1.16  riastrad 		drm_gem_object_get(obj);
    427   1.3  riastrad 
    428   1.1  riastrad 	/*
    429   1.3  riastrad 	 * Get the user-visible handle using idr.  Preload and perform
    430   1.3  riastrad 	 * allocation under our spinlock.
    431   1.1  riastrad 	 */
    432   1.3  riastrad 	idr_preload(GFP_KERNEL);
    433   1.3  riastrad 	spin_lock(&file_priv->table_lock);
    434   1.1  riastrad 
    435   1.3  riastrad 	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
    436  1.16  riastrad 
    437   1.1  riastrad 	spin_unlock(&file_priv->table_lock);
    438   1.3  riastrad 	idr_preload_end();
    439  1.16  riastrad 
    440   1.3  riastrad 	mutex_unlock(&dev->object_name_lock);
    441   1.7  riastrad 	if (ret < 0)
    442   1.7  riastrad 		goto err_unref;
    443   1.7  riastrad 
    444  1.16  riastrad 	handle = ret;
    445   1.1  riastrad 
    446  1.16  riastrad 	ret = drm_vma_node_allow(&obj->vma_node, file_priv);
    447   1.7  riastrad 	if (ret)
    448   1.7  riastrad 		goto err_remove;
    449   1.1  riastrad 
    450  1.16  riastrad 	if (obj->funcs && obj->funcs->open) {
    451  1.16  riastrad 		ret = obj->funcs->open(obj, file_priv);
    452  1.16  riastrad 		if (ret)
    453  1.16  riastrad 			goto err_revoke;
    454  1.16  riastrad 	} else if (dev->driver->gem_open_object) {
    455   1.1  riastrad 		ret = dev->driver->gem_open_object(obj, file_priv);
    456   1.7  riastrad 		if (ret)
    457   1.7  riastrad 			goto err_revoke;
    458   1.1  riastrad 	}
    459   1.1  riastrad 
    460  1.16  riastrad 	*handlep = handle;
    461   1.1  riastrad 	return 0;
    462   1.7  riastrad 
    463   1.7  riastrad err_revoke:
    464  1.16  riastrad 	drm_vma_node_revoke(&obj->vma_node, file_priv);
    465   1.7  riastrad err_remove:
    466   1.7  riastrad 	spin_lock(&file_priv->table_lock);
    467  1.16  riastrad 	idr_remove(&file_priv->object_idr, handle);
    468   1.7  riastrad 	spin_unlock(&file_priv->table_lock);
    469   1.7  riastrad err_unref:
    470  1.16  riastrad 	drm_gem_object_handle_put_unlocked(obj);
    471   1.7  riastrad 	return ret;
    472   1.1  riastrad }
    473   1.3  riastrad 
    474   1.3  riastrad /**
    475   1.7  riastrad  * drm_gem_handle_create - create a gem handle for an object
    476   1.3  riastrad  * @file_priv: drm file-private structure to register the handle for
    477   1.3  riastrad  * @obj: object to register
    478   1.3  riastrad  * @handlep: pionter to return the created handle to the caller
    479   1.3  riastrad  *
    480  1.16  riastrad  * Create a handle for this object. This adds a handle reference to the object,
    481  1.16  riastrad  * which includes a regular reference count. Callers will likely want to
    482  1.16  riastrad  * dereference the object afterwards.
    483  1.16  riastrad  *
    484  1.16  riastrad  * Since this publishes @obj to userspace it must be fully set up by this point,
    485  1.16  riastrad  * drivers must call this last in their buffer object creation callbacks.
    486   1.3  riastrad  */
    487   1.7  riastrad int drm_gem_handle_create(struct drm_file *file_priv,
    488   1.7  riastrad 			  struct drm_gem_object *obj,
    489   1.7  riastrad 			  u32 *handlep)
    490   1.3  riastrad {
    491   1.3  riastrad 	mutex_lock(&obj->dev->object_name_lock);
    492   1.3  riastrad 
    493   1.3  riastrad 	return drm_gem_handle_create_tail(file_priv, obj, handlep);
    494   1.3  riastrad }
    495   1.1  riastrad EXPORT_SYMBOL(drm_gem_handle_create);
    496   1.1  riastrad 
    497   1.1  riastrad 
    498   1.1  riastrad /**
    499   1.1  riastrad  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
    500   1.1  riastrad  * @obj: obj in question
    501   1.1  riastrad  *
    502   1.1  riastrad  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
    503  1.16  riastrad  *
    504  1.16  riastrad  * Note that drm_gem_object_release() already calls this function, so drivers
    505  1.16  riastrad  * don't have to take care of releasing the mmap offset themselves when freeing
    506  1.16  riastrad  * the GEM object.
    507   1.1  riastrad  */
    508   1.1  riastrad void
    509   1.1  riastrad drm_gem_free_mmap_offset(struct drm_gem_object *obj)
    510   1.1  riastrad {
    511   1.1  riastrad 	struct drm_device *dev = obj->dev;
    512   1.1  riastrad 
    513   1.3  riastrad 	drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
    514   1.1  riastrad }
    515   1.1  riastrad EXPORT_SYMBOL(drm_gem_free_mmap_offset);
    516   1.1  riastrad 
    517   1.1  riastrad /**
    518   1.3  riastrad  * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
    519   1.3  riastrad  * @obj: obj in question
    520   1.3  riastrad  * @size: the virtual size
    521   1.3  riastrad  *
    522   1.3  riastrad  * GEM memory mapping works by handing back to userspace a fake mmap offset
    523   1.3  riastrad  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
    524   1.3  riastrad  * up the object based on the offset and sets up the various memory mapping
    525   1.3  riastrad  * structures.
    526   1.3  riastrad  *
    527   1.3  riastrad  * This routine allocates and attaches a fake offset for @obj, in cases where
    528  1.16  riastrad  * the virtual size differs from the physical size (ie. &drm_gem_object.size).
    529  1.16  riastrad  * Otherwise just use drm_gem_create_mmap_offset().
    530  1.16  riastrad  *
    531  1.16  riastrad  * This function is idempotent and handles an already allocated mmap offset
    532  1.16  riastrad  * transparently. Drivers do not need to check for this case.
    533   1.3  riastrad  */
    534   1.3  riastrad int
    535   1.3  riastrad drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
    536   1.3  riastrad {
    537   1.3  riastrad 	struct drm_device *dev = obj->dev;
    538   1.3  riastrad 
    539   1.3  riastrad 	return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
    540   1.3  riastrad 				  size / PAGE_SIZE);
    541   1.3  riastrad }
    542   1.3  riastrad EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
    543   1.3  riastrad 
    544   1.3  riastrad /**
    545   1.1  riastrad  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
    546   1.1  riastrad  * @obj: obj in question
    547   1.1  riastrad  *
    548   1.1  riastrad  * GEM memory mapping works by handing back to userspace a fake mmap offset
    549   1.1  riastrad  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
    550   1.1  riastrad  * up the object based on the offset and sets up the various memory mapping
    551   1.1  riastrad  * structures.
    552   1.1  riastrad  *
    553   1.1  riastrad  * This routine allocates and attaches a fake offset for @obj.
    554  1.16  riastrad  *
    555  1.16  riastrad  * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
    556  1.16  riastrad  * the fake offset again.
    557   1.1  riastrad  */
    558   1.3  riastrad int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
    559   1.3  riastrad {
    560   1.3  riastrad 	return drm_gem_create_mmap_offset_size(obj, obj->size);
    561   1.3  riastrad }
    562   1.3  riastrad EXPORT_SYMBOL(drm_gem_create_mmap_offset);
    563   1.3  riastrad 
    564  1.16  riastrad /*
    565  1.16  riastrad  * Move pages to appropriate lru and release the pagevec, decrementing the
    566  1.16  riastrad  * ref count of those pages.
    567  1.16  riastrad  */
    568  1.16  riastrad static void drm_gem_check_release_pagevec(struct pagevec *pvec)
    569  1.16  riastrad {
    570  1.16  riastrad 	check_move_unevictable_pages(pvec);
    571  1.16  riastrad 	__pagevec_release(pvec);
    572  1.16  riastrad 	cond_resched();
    573  1.16  riastrad }
    574  1.16  riastrad 
    575   1.3  riastrad /**
    576   1.3  riastrad  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
    577   1.3  riastrad  * from shmem
    578   1.3  riastrad  * @obj: obj in question
    579   1.7  riastrad  *
    580   1.7  riastrad  * This reads the page-array of the shmem-backing storage of the given gem
    581   1.7  riastrad  * object. An array of pages is returned. If a page is not allocated or
    582   1.7  riastrad  * swapped-out, this will allocate/swap-in the required pages. Note that the
    583   1.7  riastrad  * whole object is covered by the page-array and pinned in memory.
    584   1.7  riastrad  *
    585   1.7  riastrad  * Use drm_gem_put_pages() to release the array and unpin all pages.
    586   1.7  riastrad  *
    587   1.7  riastrad  * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
    588   1.7  riastrad  * If you require other GFP-masks, you have to do those allocations yourself.
    589   1.7  riastrad  *
    590   1.7  riastrad  * Note that you are not allowed to change gfp-zones during runtime. That is,
    591   1.7  riastrad  * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
    592   1.7  riastrad  * set during initialization. If you have special zone constraints, set them
    593  1.16  riastrad  * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care
    594   1.7  riastrad  * to keep pages in the required zone during swap-in.
    595   1.3  riastrad  */
    596   1.4  riastrad #ifdef __NetBSD__
    597   1.4  riastrad struct page **
    598   1.7  riastrad drm_gem_get_pages(struct drm_gem_object *obj)
    599   1.4  riastrad {
    600   1.4  riastrad 	struct pglist pglist;
    601   1.4  riastrad 	struct vm_page *vm_page;
    602   1.4  riastrad 	struct page **pages;
    603   1.4  riastrad 	unsigned i;
    604   1.4  riastrad 	int ret;
    605   1.4  riastrad 
    606   1.4  riastrad 	KASSERT((obj->size & (PAGE_SIZE - 1)) != 0);
    607   1.4  riastrad 
    608   1.4  riastrad 	pages = drm_malloc_ab(obj->size >> PAGE_SHIFT, sizeof(*pages));
    609   1.4  riastrad 	if (pages == NULL) {
    610   1.4  riastrad 		ret = -ENOMEM;
    611   1.4  riastrad 		goto fail0;
    612   1.4  riastrad 	}
    613   1.4  riastrad 
    614   1.4  riastrad 	TAILQ_INIT(&pglist);
    615   1.4  riastrad 	/* XXX errno NetBSD->Linux */
    616   1.8  riastrad 	ret = -uvm_obj_wirepages(obj->filp, 0, obj->size, &pglist);
    617   1.4  riastrad 	if (ret)
    618   1.4  riastrad 		goto fail1;
    619   1.4  riastrad 
    620   1.4  riastrad 	i = 0;
    621   1.4  riastrad 	TAILQ_FOREACH(vm_page, &pglist, pageq.queue)
    622   1.4  riastrad 		pages[i++] = container_of(vm_page, struct page, p_vmp);
    623   1.4  riastrad 
    624   1.4  riastrad 	return pages;
    625   1.4  riastrad 
    626   1.4  riastrad fail1:	drm_free_large(pages);
    627   1.4  riastrad fail0:	return ERR_PTR(ret);
    628   1.4  riastrad }
    629   1.4  riastrad #else
    630   1.7  riastrad struct page **drm_gem_get_pages(struct drm_gem_object *obj)
    631   1.1  riastrad {
    632   1.3  riastrad 	struct address_space *mapping;
    633   1.3  riastrad 	struct page *p, **pages;
    634  1.16  riastrad 	struct pagevec pvec;
    635   1.3  riastrad 	int i, npages;
    636   1.3  riastrad 
    637   1.3  riastrad 	/* This is the shared memory object that backs the GEM resource */
    638  1.16  riastrad 	mapping = obj->filp->f_mapping;
    639   1.3  riastrad 
    640   1.3  riastrad 	/* We already BUG_ON() for non-page-aligned sizes in
    641   1.3  riastrad 	 * drm_gem_object_init(), so we should never hit this unless
    642   1.3  riastrad 	 * driver author is doing something really wrong:
    643   1.3  riastrad 	 */
    644   1.3  riastrad 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
    645   1.1  riastrad 
    646   1.3  riastrad 	npages = obj->size >> PAGE_SHIFT;
    647   1.1  riastrad 
    648  1.16  riastrad 	pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
    649   1.3  riastrad 	if (pages == NULL)
    650   1.3  riastrad 		return ERR_PTR(-ENOMEM);
    651   1.3  riastrad 
    652  1.16  riastrad 	mapping_set_unevictable(mapping);
    653  1.16  riastrad 
    654   1.3  riastrad 	for (i = 0; i < npages; i++) {
    655   1.7  riastrad 		p = shmem_read_mapping_page(mapping, i);
    656   1.3  riastrad 		if (IS_ERR(p))
    657   1.3  riastrad 			goto fail;
    658   1.3  riastrad 		pages[i] = p;
    659   1.3  riastrad 
    660   1.7  riastrad 		/* Make sure shmem keeps __GFP_DMA32 allocated pages in the
    661   1.7  riastrad 		 * correct region during swapin. Note that this requires
    662   1.7  riastrad 		 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
    663   1.7  riastrad 		 * so shmem can relocate pages during swapin if required.
    664   1.3  riastrad 		 */
    665   1.7  riastrad 		BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
    666   1.3  riastrad 				(page_to_pfn(p) >= 0x00100000UL));
    667   1.1  riastrad 	}
    668   1.1  riastrad 
    669   1.3  riastrad 	return pages;
    670   1.3  riastrad 
    671   1.3  riastrad fail:
    672  1.16  riastrad 	mapping_clear_unevictable(mapping);
    673  1.16  riastrad 	pagevec_init(&pvec);
    674  1.16  riastrad 	while (i--) {
    675  1.16  riastrad 		if (!pagevec_add(&pvec, pages[i]))
    676  1.16  riastrad 			drm_gem_check_release_pagevec(&pvec);
    677  1.16  riastrad 	}
    678  1.16  riastrad 	if (pagevec_count(&pvec))
    679  1.16  riastrad 		drm_gem_check_release_pagevec(&pvec);
    680   1.3  riastrad 
    681  1.16  riastrad 	kvfree(pages);
    682   1.3  riastrad 	return ERR_CAST(p);
    683   1.3  riastrad }
    684   1.4  riastrad #endif
    685   1.3  riastrad EXPORT_SYMBOL(drm_gem_get_pages);
    686   1.3  riastrad 
    687   1.3  riastrad /**
    688   1.3  riastrad  * drm_gem_put_pages - helper to free backing pages for a GEM object
    689   1.3  riastrad  * @obj: obj in question
    690   1.3  riastrad  * @pages: pages to free
    691   1.3  riastrad  * @dirty: if true, pages will be marked as dirty
    692   1.3  riastrad  * @accessed: if true, the pages will be marked as accessed
    693   1.3  riastrad  */
    694   1.4  riastrad #ifdef __NetBSD__
    695   1.4  riastrad void
    696   1.4  riastrad drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, bool dirty,
    697   1.4  riastrad     bool accessed __unused /* XXX */)
    698   1.4  riastrad {
    699   1.4  riastrad 	unsigned i;
    700   1.4  riastrad 
    701   1.4  riastrad 	for (i = 0; i < (obj->size >> PAGE_SHIFT); i++) {
    702  1.11        ad 		if (dirty) {
    703  1.15        ad 			rw_enter(obj->filp->vmobjlock, RW_WRITER);
    704  1.11        ad 			uvm_pagemarkdirty(&pages[i]->p_vmp,
    705  1.11        ad 			    UVM_PAGE_STATUS_DIRTY);
    706  1.15        ad 			rw_exit(obj->filp->vmobjlock);
    707  1.11        ad 		}
    708   1.4  riastrad 	}
    709   1.4  riastrad 
    710   1.8  riastrad 	uvm_obj_unwirepages(obj->filp, 0, obj->size);
    711   1.4  riastrad }
    712   1.4  riastrad #else
    713   1.3  riastrad void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
    714   1.3  riastrad 		bool dirty, bool accessed)
    715   1.3  riastrad {
    716   1.3  riastrad 	int i, npages;
    717  1.16  riastrad 	struct address_space *mapping;
    718  1.16  riastrad 	struct pagevec pvec;
    719  1.16  riastrad 
    720  1.16  riastrad 	mapping = file_inode(obj->filp)->i_mapping;
    721  1.16  riastrad 	mapping_clear_unevictable(mapping);
    722   1.3  riastrad 
    723   1.3  riastrad 	/* We already BUG_ON() for non-page-aligned sizes in
    724   1.3  riastrad 	 * drm_gem_object_init(), so we should never hit this unless
    725   1.3  riastrad 	 * driver author is doing something really wrong:
    726   1.3  riastrad 	 */
    727   1.3  riastrad 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
    728   1.3  riastrad 
    729   1.3  riastrad 	npages = obj->size >> PAGE_SHIFT;
    730   1.1  riastrad 
    731  1.16  riastrad 	pagevec_init(&pvec);
    732   1.3  riastrad 	for (i = 0; i < npages; i++) {
    733  1.16  riastrad 		if (!pages[i])
    734  1.16  riastrad 			continue;
    735  1.16  riastrad 
    736   1.3  riastrad 		if (dirty)
    737   1.3  riastrad 			set_page_dirty(pages[i]);
    738   1.1  riastrad 
    739   1.3  riastrad 		if (accessed)
    740   1.3  riastrad 			mark_page_accessed(pages[i]);
    741   1.1  riastrad 
    742   1.3  riastrad 		/* Undo the reference we took when populating the table */
    743  1.16  riastrad 		if (!pagevec_add(&pvec, pages[i]))
    744  1.16  riastrad 			drm_gem_check_release_pagevec(&pvec);
    745   1.3  riastrad 	}
    746  1.16  riastrad 	if (pagevec_count(&pvec))
    747  1.16  riastrad 		drm_gem_check_release_pagevec(&pvec);
    748   1.3  riastrad 
    749  1.16  riastrad 	kvfree(pages);
    750   1.1  riastrad }
    751   1.4  riastrad #endif
    752   1.3  riastrad EXPORT_SYMBOL(drm_gem_put_pages);
    753   1.1  riastrad 
    754  1.16  riastrad static int objects_lookup(struct drm_file *filp, u32 *handle, int count,
    755  1.16  riastrad 			  struct drm_gem_object **objs)
    756   1.1  riastrad {
    757  1.16  riastrad 	int i, ret = 0;
    758   1.1  riastrad 	struct drm_gem_object *obj;
    759   1.1  riastrad 
    760   1.1  riastrad 	spin_lock(&filp->table_lock);
    761   1.1  riastrad 
    762  1.16  riastrad 	for (i = 0; i < count; i++) {
    763  1.16  riastrad 		/* Check if we currently have a reference on the object */
    764  1.16  riastrad 		obj = idr_find(&filp->object_idr, handle[i]);
    765  1.16  riastrad 		if (!obj) {
    766  1.16  riastrad 			ret = -ENOENT;
    767  1.16  riastrad 			break;
    768  1.16  riastrad 		}
    769  1.16  riastrad 		drm_gem_object_get(obj);
    770  1.16  riastrad 		objs[i] = obj;
    771  1.16  riastrad 	}
    772  1.16  riastrad 	spin_unlock(&filp->table_lock);
    773  1.16  riastrad 
    774  1.16  riastrad 	return ret;
    775  1.16  riastrad }
    776  1.16  riastrad 
    777  1.16  riastrad /**
    778  1.16  riastrad  * drm_gem_objects_lookup - look up GEM objects from an array of handles
    779  1.16  riastrad  * @filp: DRM file private date
    780  1.16  riastrad  * @bo_handles: user pointer to array of userspace handle
    781  1.16  riastrad  * @count: size of handle array
    782  1.16  riastrad  * @objs_out: returned pointer to array of drm_gem_object pointers
    783  1.16  riastrad  *
    784  1.16  riastrad  * Takes an array of userspace handles and returns a newly allocated array of
    785  1.16  riastrad  * GEM objects.
    786  1.16  riastrad  *
    787  1.16  riastrad  * For a single handle lookup, use drm_gem_object_lookup().
    788  1.16  riastrad  *
    789  1.16  riastrad  * Returns:
    790  1.16  riastrad  *
    791  1.16  riastrad  * @objs filled in with GEM object pointers. Returned GEM objects need to be
    792  1.16  riastrad  * released with drm_gem_object_put(). -ENOENT is returned on a lookup
    793  1.16  riastrad  * failure. 0 is returned on success.
    794  1.16  riastrad  *
    795  1.16  riastrad  */
    796  1.16  riastrad int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
    797  1.16  riastrad 			   int count, struct drm_gem_object ***objs_out)
    798  1.16  riastrad {
    799  1.16  riastrad 	int ret;
    800  1.16  riastrad 	u32 *handles;
    801  1.16  riastrad 	struct drm_gem_object **objs;
    802  1.16  riastrad 
    803  1.16  riastrad 	if (!count)
    804  1.16  riastrad 		return 0;
    805  1.16  riastrad 
    806  1.16  riastrad 	objs = kvmalloc_array(count, sizeof(struct drm_gem_object *),
    807  1.16  riastrad 			     GFP_KERNEL | __GFP_ZERO);
    808  1.16  riastrad 	if (!objs)
    809  1.16  riastrad 		return -ENOMEM;
    810  1.16  riastrad 
    811  1.16  riastrad 	handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
    812  1.16  riastrad 	if (!handles) {
    813  1.16  riastrad 		ret = -ENOMEM;
    814  1.16  riastrad 		goto out;
    815  1.16  riastrad 	}
    816  1.16  riastrad 
    817  1.16  riastrad 	if (copy_from_user(handles, bo_handles, count * sizeof(u32))) {
    818  1.16  riastrad 		ret = -EFAULT;
    819  1.16  riastrad 		DRM_DEBUG("Failed to copy in GEM handles\n");
    820  1.16  riastrad 		goto out;
    821   1.1  riastrad 	}
    822   1.1  riastrad 
    823  1.16  riastrad 	ret = objects_lookup(filp, handles, count, objs);
    824  1.16  riastrad 	*objs_out = objs;
    825   1.1  riastrad 
    826  1.16  riastrad out:
    827  1.16  riastrad 	kvfree(handles);
    828  1.16  riastrad 	return ret;
    829  1.16  riastrad 
    830  1.16  riastrad }
    831  1.16  riastrad EXPORT_SYMBOL(drm_gem_objects_lookup);
    832  1.16  riastrad 
    833  1.16  riastrad /**
    834  1.16  riastrad  * drm_gem_object_lookup - look up a GEM object from its handle
    835  1.16  riastrad  * @filp: DRM file private date
    836  1.16  riastrad  * @handle: userspace handle
    837  1.16  riastrad  *
    838  1.16  riastrad  * Returns:
    839  1.16  riastrad  *
    840  1.16  riastrad  * A reference to the object named by the handle if such exists on @filp, NULL
    841  1.16  riastrad  * otherwise.
    842  1.16  riastrad  *
    843  1.16  riastrad  * If looking up an array of handles, use drm_gem_objects_lookup().
    844  1.16  riastrad  */
    845  1.16  riastrad struct drm_gem_object *
    846  1.16  riastrad drm_gem_object_lookup(struct drm_file *filp, u32 handle)
    847  1.16  riastrad {
    848  1.16  riastrad 	struct drm_gem_object *obj = NULL;
    849   1.1  riastrad 
    850  1.16  riastrad 	objects_lookup(filp, &handle, 1, &obj);
    851   1.1  riastrad 	return obj;
    852   1.1  riastrad }
    853   1.1  riastrad EXPORT_SYMBOL(drm_gem_object_lookup);
    854   1.1  riastrad 
    855   1.1  riastrad /**
    856  1.16  riastrad  * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects
    857  1.16  riastrad  * shared and/or exclusive fences.
    858  1.16  riastrad  * @filep: DRM file private date
    859  1.16  riastrad  * @handle: userspace handle
    860  1.16  riastrad  * @wait_all: if true, wait on all fences, else wait on just exclusive fence
    861  1.16  riastrad  * @timeout: timeout value in jiffies or zero to return immediately
    862  1.16  riastrad  *
    863  1.16  riastrad  * Returns:
    864  1.16  riastrad  *
    865  1.16  riastrad  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
    866  1.16  riastrad  * greater than 0 on success.
    867  1.16  riastrad  */
    868  1.16  riastrad long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
    869  1.16  riastrad 				    bool wait_all, unsigned long timeout)
    870  1.16  riastrad {
    871  1.16  riastrad 	long ret;
    872  1.16  riastrad 	struct drm_gem_object *obj;
    873  1.16  riastrad 
    874  1.16  riastrad 	obj = drm_gem_object_lookup(filep, handle);
    875  1.16  riastrad 	if (!obj) {
    876  1.16  riastrad 		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
    877  1.16  riastrad 		return -EINVAL;
    878  1.16  riastrad 	}
    879  1.16  riastrad 
    880  1.16  riastrad 	ret = dma_resv_wait_timeout_rcu(obj->resv, wait_all,
    881  1.16  riastrad 						  true, timeout);
    882  1.16  riastrad 	if (ret == 0)
    883  1.16  riastrad 		ret = -ETIME;
    884  1.16  riastrad 	else if (ret > 0)
    885  1.16  riastrad 		ret = 0;
    886  1.16  riastrad 
    887  1.16  riastrad 	drm_gem_object_put_unlocked(obj);
    888  1.16  riastrad 
    889  1.16  riastrad 	return ret;
    890  1.16  riastrad }
    891  1.16  riastrad EXPORT_SYMBOL(drm_gem_dma_resv_wait);
    892  1.16  riastrad 
    893  1.16  riastrad /**
    894   1.3  riastrad  * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
    895   1.3  riastrad  * @dev: drm_device
    896   1.3  riastrad  * @data: ioctl data
    897   1.3  riastrad  * @file_priv: drm file-private structure
    898   1.3  riastrad  *
    899   1.1  riastrad  * Releases the handle to an mm object.
    900   1.1  riastrad  */
    901   1.1  riastrad int
    902   1.1  riastrad drm_gem_close_ioctl(struct drm_device *dev, void *data,
    903   1.1  riastrad 		    struct drm_file *file_priv)
    904   1.1  riastrad {
    905   1.1  riastrad 	struct drm_gem_close *args = data;
    906   1.1  riastrad 	int ret;
    907   1.1  riastrad 
    908   1.7  riastrad 	if (!drm_core_check_feature(dev, DRIVER_GEM))
    909  1.16  riastrad 		return -EOPNOTSUPP;
    910   1.1  riastrad 
    911   1.1  riastrad 	ret = drm_gem_handle_delete(file_priv, args->handle);
    912   1.1  riastrad 
    913   1.1  riastrad 	return ret;
    914   1.1  riastrad }
    915   1.1  riastrad 
    916   1.1  riastrad /**
    917   1.3  riastrad  * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
    918   1.3  riastrad  * @dev: drm_device
    919   1.3  riastrad  * @data: ioctl data
    920   1.3  riastrad  * @file_priv: drm file-private structure
    921   1.3  riastrad  *
    922   1.1  riastrad  * Create a global name for an object, returning the name.
    923   1.1  riastrad  *
    924   1.1  riastrad  * Note that the name does not hold a reference; when the object
    925   1.1  riastrad  * is freed, the name goes away.
    926   1.1  riastrad  */
    927   1.1  riastrad int
    928   1.1  riastrad drm_gem_flink_ioctl(struct drm_device *dev, void *data,
    929   1.1  riastrad 		    struct drm_file *file_priv)
    930   1.1  riastrad {
    931   1.1  riastrad 	struct drm_gem_flink *args = data;
    932   1.1  riastrad 	struct drm_gem_object *obj;
    933   1.1  riastrad 	int ret;
    934   1.1  riastrad 
    935   1.7  riastrad 	if (!drm_core_check_feature(dev, DRIVER_GEM))
    936  1.16  riastrad 		return -EOPNOTSUPP;
    937   1.1  riastrad 
    938  1.16  riastrad 	obj = drm_gem_object_lookup(file_priv, args->handle);
    939   1.1  riastrad 	if (obj == NULL)
    940   1.1  riastrad 		return -ENOENT;
    941   1.1  riastrad 
    942   1.9  riastrad 	idr_preload(GFP_KERNEL);
    943   1.3  riastrad 	mutex_lock(&dev->object_name_lock);
    944   1.3  riastrad 	/* prevent races with concurrent gem_close. */
    945   1.3  riastrad 	if (obj->handle_count == 0) {
    946   1.3  riastrad 		ret = -ENOENT;
    947   1.1  riastrad 		goto err;
    948   1.1  riastrad 	}
    949   1.1  riastrad 
    950   1.1  riastrad 	if (!obj->name) {
    951  1.16  riastrad 		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
    952   1.3  riastrad 		if (ret < 0)
    953   1.1  riastrad 			goto err;
    954   1.1  riastrad 
    955   1.3  riastrad 		obj->name = ret;
    956   1.1  riastrad 	}
    957   1.1  riastrad 
    958   1.3  riastrad 	args->name = (uint64_t) obj->name;
    959   1.3  riastrad 	ret = 0;
    960   1.3  riastrad 
    961   1.1  riastrad err:
    962   1.9  riastrad 	mutex_unlock(&dev->object_name_lock);
    963   1.3  riastrad 	idr_preload_end();
    964  1.16  riastrad 	drm_gem_object_put_unlocked(obj);
    965   1.1  riastrad 	return ret;
    966   1.1  riastrad }
    967   1.1  riastrad 
    968   1.1  riastrad /**
    969   1.3  riastrad  * drm_gem_open - implementation of the GEM_OPEN ioctl
    970   1.3  riastrad  * @dev: drm_device
    971   1.3  riastrad  * @data: ioctl data
    972   1.3  riastrad  * @file_priv: drm file-private structure
    973   1.3  riastrad  *
    974   1.1  riastrad  * Open an object using the global name, returning a handle and the size.
    975   1.1  riastrad  *
    976   1.1  riastrad  * This handle (of course) holds a reference to the object, so the object
    977   1.1  riastrad  * will not go away until the handle is deleted.
    978   1.1  riastrad  */
    979   1.1  riastrad int
    980   1.1  riastrad drm_gem_open_ioctl(struct drm_device *dev, void *data,
    981   1.1  riastrad 		   struct drm_file *file_priv)
    982   1.1  riastrad {
    983   1.1  riastrad 	struct drm_gem_open *args = data;
    984   1.1  riastrad 	struct drm_gem_object *obj;
    985   1.1  riastrad 	int ret;
    986   1.1  riastrad 	u32 handle;
    987   1.1  riastrad 
    988   1.7  riastrad 	if (!drm_core_check_feature(dev, DRIVER_GEM))
    989  1.16  riastrad 		return -EOPNOTSUPP;
    990   1.1  riastrad 
    991   1.3  riastrad 	mutex_lock(&dev->object_name_lock);
    992   1.1  riastrad 	obj = idr_find(&dev->object_name_idr, (int) args->name);
    993   1.3  riastrad 	if (obj) {
    994  1.16  riastrad 		drm_gem_object_get(obj);
    995   1.3  riastrad 	} else {
    996   1.3  riastrad 		mutex_unlock(&dev->object_name_lock);
    997   1.1  riastrad 		return -ENOENT;
    998   1.3  riastrad 	}
    999   1.1  riastrad 
   1000   1.3  riastrad 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
   1001   1.3  riastrad 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
   1002  1.16  riastrad 	drm_gem_object_put_unlocked(obj);
   1003   1.1  riastrad 	if (ret)
   1004   1.1  riastrad 		return ret;
   1005   1.1  riastrad 
   1006   1.1  riastrad 	args->handle = handle;
   1007   1.1  riastrad 	args->size = obj->size;
   1008   1.1  riastrad 
   1009   1.1  riastrad 	return 0;
   1010   1.1  riastrad }
   1011   1.1  riastrad 
   1012   1.1  riastrad /**
   1013   1.3  riastrad  * gem_gem_open - initalizes GEM file-private structures at devnode open time
   1014   1.3  riastrad  * @dev: drm_device which is being opened by userspace
   1015   1.3  riastrad  * @file_private: drm file-private structure to set up
   1016   1.3  riastrad  *
   1017   1.1  riastrad  * Called at device open time, sets up the structure for handling refcounting
   1018   1.1  riastrad  * of mm objects.
   1019   1.1  riastrad  */
   1020   1.1  riastrad void
   1021   1.1  riastrad drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
   1022   1.1  riastrad {
   1023  1.16  riastrad 	idr_init_base(&file_private->object_idr, 1);
   1024   1.1  riastrad 	spin_lock_init(&file_private->table_lock);
   1025   1.1  riastrad }
   1026   1.1  riastrad 
   1027   1.1  riastrad /**
   1028   1.3  riastrad  * drm_gem_release - release file-private GEM resources
   1029   1.3  riastrad  * @dev: drm_device which is being closed by userspace
   1030   1.3  riastrad  * @file_private: drm file-private structure to clean up
   1031   1.3  riastrad  *
   1032   1.1  riastrad  * Called at close time when the filp is going away.
   1033   1.1  riastrad  *
   1034   1.1  riastrad  * Releases any remaining references on objects by this filp.
   1035   1.1  riastrad  */
   1036   1.1  riastrad void
   1037   1.1  riastrad drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
   1038   1.1  riastrad {
   1039   1.1  riastrad 	idr_for_each(&file_private->object_idr,
   1040   1.1  riastrad 		     &drm_gem_object_release_handle, file_private);
   1041   1.1  riastrad 	idr_destroy(&file_private->object_idr);
   1042   1.2  riastrad #ifdef __NetBSD__
   1043   1.2  riastrad 	spin_lock_destroy(&file_private->table_lock);
   1044   1.2  riastrad #endif
   1045   1.1  riastrad }
   1046   1.1  riastrad 
   1047  1.16  riastrad /**
   1048  1.16  riastrad  * drm_gem_object_release - release GEM buffer object resources
   1049  1.16  riastrad  * @obj: GEM buffer object
   1050  1.16  riastrad  *
   1051  1.16  riastrad  * This releases any structures and resources used by @obj and is the invers of
   1052  1.16  riastrad  * drm_gem_object_init().
   1053  1.16  riastrad  */
   1054   1.1  riastrad void
   1055   1.1  riastrad drm_gem_object_release(struct drm_gem_object *obj)
   1056   1.1  riastrad {
   1057   1.4  riastrad #ifndef __NetBSD__
   1058   1.3  riastrad 	WARN_ON(obj->dma_buf);
   1059   1.4  riastrad #endif
   1060   1.3  riastrad 
   1061   1.2  riastrad #ifdef __NetBSD__
   1062   1.4  riastrad 	drm_vma_node_destroy(&obj->vma_node);
   1063   1.8  riastrad 	if (obj->filp)
   1064   1.8  riastrad 		uao_detach(obj->filp);
   1065   1.2  riastrad 	uvm_obj_destroy(&obj->gemo_uvmobj, true);
   1066   1.2  riastrad #else
   1067   1.1  riastrad 	if (obj->filp)
   1068   1.3  riastrad 		fput(obj->filp);
   1069   1.2  riastrad #endif
   1070   1.3  riastrad 
   1071  1.16  riastrad 	dma_resv_fini(&obj->_resv);
   1072   1.3  riastrad 	drm_gem_free_mmap_offset(obj);
   1073   1.1  riastrad }
   1074   1.1  riastrad EXPORT_SYMBOL(drm_gem_object_release);
   1075   1.1  riastrad 
   1076   1.1  riastrad /**
   1077   1.3  riastrad  * drm_gem_object_free - free a GEM object
   1078   1.3  riastrad  * @kref: kref of the object to free
   1079   1.3  riastrad  *
   1080   1.1  riastrad  * Called after the last reference to the object has been lost.
   1081  1.16  riastrad  * Must be called holding &drm_device.struct_mutex.
   1082   1.1  riastrad  *
   1083   1.1  riastrad  * Frees the object
   1084   1.1  riastrad  */
   1085   1.1  riastrad void
   1086   1.1  riastrad drm_gem_object_free(struct kref *kref)
   1087   1.1  riastrad {
   1088   1.7  riastrad 	struct drm_gem_object *obj =
   1089   1.7  riastrad 		container_of(kref, struct drm_gem_object, refcount);
   1090   1.1  riastrad 	struct drm_device *dev = obj->dev;
   1091   1.1  riastrad 
   1092  1.16  riastrad 	if (obj->funcs) {
   1093  1.16  riastrad 		obj->funcs->free(obj);
   1094  1.16  riastrad 	} else if (dev->driver->gem_free_object_unlocked) {
   1095  1.16  riastrad 		dev->driver->gem_free_object_unlocked(obj);
   1096  1.16  riastrad 	} else if (dev->driver->gem_free_object) {
   1097  1.16  riastrad 		WARN_ON(!mutex_is_locked(&dev->struct_mutex));
   1098   1.1  riastrad 
   1099   1.1  riastrad 		dev->driver->gem_free_object(obj);
   1100  1.16  riastrad 	}
   1101   1.1  riastrad }
   1102   1.1  riastrad EXPORT_SYMBOL(drm_gem_object_free);
   1103   1.1  riastrad 
   1104  1.16  riastrad /**
   1105  1.16  riastrad  * drm_gem_object_put_unlocked - drop a GEM buffer object reference
   1106  1.16  riastrad  * @obj: GEM buffer object
   1107  1.16  riastrad  *
   1108  1.16  riastrad  * This releases a reference to @obj. Callers must not hold the
   1109  1.16  riastrad  * &drm_device.struct_mutex lock when calling this function.
   1110  1.16  riastrad  *
   1111  1.16  riastrad  * See also __drm_gem_object_put().
   1112  1.16  riastrad  */
   1113  1.16  riastrad void
   1114  1.16  riastrad drm_gem_object_put_unlocked(struct drm_gem_object *obj)
   1115  1.16  riastrad {
   1116  1.16  riastrad 	struct drm_device *dev;
   1117  1.16  riastrad 
   1118  1.16  riastrad 	if (!obj)
   1119  1.16  riastrad 		return;
   1120  1.16  riastrad 
   1121  1.16  riastrad 	dev = obj->dev;
   1122  1.16  riastrad 
   1123  1.16  riastrad 	if (dev->driver->gem_free_object) {
   1124  1.16  riastrad 		might_lock(&dev->struct_mutex);
   1125  1.16  riastrad 		if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
   1126  1.16  riastrad 				&dev->struct_mutex))
   1127  1.16  riastrad 			mutex_unlock(&dev->struct_mutex);
   1128  1.16  riastrad 	} else {
   1129  1.16  riastrad 		kref_put(&obj->refcount, drm_gem_object_free);
   1130  1.16  riastrad 	}
   1131  1.16  riastrad }
   1132  1.16  riastrad EXPORT_SYMBOL(drm_gem_object_put_unlocked);
   1133  1.16  riastrad 
   1134  1.16  riastrad /**
   1135  1.16  riastrad  * drm_gem_object_put - release a GEM buffer object reference
   1136  1.16  riastrad  * @obj: GEM buffer object
   1137  1.16  riastrad  *
   1138  1.16  riastrad  * This releases a reference to @obj. Callers must hold the
   1139  1.16  riastrad  * &drm_device.struct_mutex lock when calling this function, even when the
   1140  1.16  riastrad  * driver doesn't use &drm_device.struct_mutex for anything.
   1141  1.16  riastrad  *
   1142  1.16  riastrad  * For drivers not encumbered with legacy locking use
   1143  1.16  riastrad  * drm_gem_object_put_unlocked() instead.
   1144  1.16  riastrad  */
   1145  1.16  riastrad void
   1146  1.16  riastrad drm_gem_object_put(struct drm_gem_object *obj)
   1147  1.16  riastrad {
   1148  1.16  riastrad 	if (obj) {
   1149  1.16  riastrad 		WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
   1150  1.16  riastrad 
   1151  1.16  riastrad 		kref_put(&obj->refcount, drm_gem_object_free);
   1152  1.16  riastrad 	}
   1153  1.16  riastrad }
   1154  1.16  riastrad EXPORT_SYMBOL(drm_gem_object_put);
   1155  1.16  riastrad 
   1156   1.2  riastrad #ifndef __NetBSD__
   1157  1.16  riastrad /**
   1158  1.16  riastrad  * drm_gem_vm_open - vma->ops->open implementation for GEM
   1159  1.16  riastrad  * @vma: VM area structure
   1160  1.16  riastrad  *
   1161  1.16  riastrad  * This function implements the #vm_operations_struct open() callback for GEM
   1162  1.16  riastrad  * drivers. This must be used together with drm_gem_vm_close().
   1163  1.16  riastrad  */
   1164   1.1  riastrad void drm_gem_vm_open(struct vm_area_struct *vma)
   1165   1.1  riastrad {
   1166   1.1  riastrad 	struct drm_gem_object *obj = vma->vm_private_data;
   1167   1.1  riastrad 
   1168  1.16  riastrad 	drm_gem_object_get(obj);
   1169   1.1  riastrad }
   1170   1.1  riastrad EXPORT_SYMBOL(drm_gem_vm_open);
   1171   1.1  riastrad 
   1172  1.16  riastrad /**
   1173  1.16  riastrad  * drm_gem_vm_close - vma->ops->close implementation for GEM
   1174  1.16  riastrad  * @vma: VM area structure
   1175  1.16  riastrad  *
   1176  1.16  riastrad  * This function implements the #vm_operations_struct close() callback for GEM
   1177  1.16  riastrad  * drivers. This must be used together with drm_gem_vm_open().
   1178  1.16  riastrad  */
   1179   1.1  riastrad void drm_gem_vm_close(struct vm_area_struct *vma)
   1180   1.1  riastrad {
   1181   1.1  riastrad 	struct drm_gem_object *obj = vma->vm_private_data;
   1182   1.1  riastrad 
   1183  1.16  riastrad 	drm_gem_object_put_unlocked(obj);
   1184   1.1  riastrad }
   1185   1.1  riastrad EXPORT_SYMBOL(drm_gem_vm_close);
   1186   1.1  riastrad 
   1187   1.3  riastrad /**
   1188   1.3  riastrad  * drm_gem_mmap_obj - memory map a GEM object
   1189   1.3  riastrad  * @obj: the GEM object to map
   1190   1.3  riastrad  * @obj_size: the object size to be mapped, in bytes
   1191   1.3  riastrad  * @vma: VMA for the area to be mapped
   1192   1.3  riastrad  *
   1193   1.3  riastrad  * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
   1194   1.3  riastrad  * provided by the driver. Depending on their requirements, drivers can either
   1195   1.3  riastrad  * provide a fault handler in their gem_vm_ops (in which case any accesses to
   1196   1.3  riastrad  * the object will be trapped, to perform migration, GTT binding, surface
   1197   1.3  riastrad  * register allocation, or performance monitoring), or mmap the buffer memory
   1198   1.3  riastrad  * synchronously after calling drm_gem_mmap_obj.
   1199   1.3  riastrad  *
   1200   1.3  riastrad  * This function is mainly intended to implement the DMABUF mmap operation, when
   1201   1.3  riastrad  * the GEM object is not looked up based on its fake offset. To implement the
   1202   1.3  riastrad  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
   1203   1.3  riastrad  *
   1204   1.3  riastrad  * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
   1205   1.3  riastrad  * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
   1206   1.3  riastrad  * callers must verify access restrictions before calling this helper.
   1207   1.3  riastrad  *
   1208   1.3  riastrad  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
   1209   1.3  riastrad  * size, or if no gem_vm_ops are provided.
   1210   1.3  riastrad  */
   1211   1.3  riastrad int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
   1212   1.3  riastrad 		     struct vm_area_struct *vma)
   1213   1.3  riastrad {
   1214   1.3  riastrad 	struct drm_device *dev = obj->dev;
   1215  1.16  riastrad 	int ret;
   1216   1.3  riastrad 
   1217   1.3  riastrad 	/* Check for valid size. */
   1218   1.3  riastrad 	if (obj_size < vma->vm_end - vma->vm_start)
   1219   1.3  riastrad 		return -EINVAL;
   1220   1.3  riastrad 
   1221   1.3  riastrad 	/* Take a ref for this mapping of the object, so that the fault
   1222   1.3  riastrad 	 * handler can dereference the mmap offset's pointer to the object.
   1223   1.3  riastrad 	 * This reference is cleaned up by the corresponding vm_close
   1224   1.3  riastrad 	 * (which should happen whether the vma was created by this call, or
   1225   1.3  riastrad 	 * by a vm_open due to mremap or partial unmap or whatever).
   1226   1.3  riastrad 	 */
   1227  1.16  riastrad 	drm_gem_object_get(obj);
   1228  1.16  riastrad 
   1229  1.16  riastrad 	if (obj->funcs && obj->funcs->mmap) {
   1230  1.16  riastrad 		ret = obj->funcs->mmap(obj, vma);
   1231  1.16  riastrad 		if (ret) {
   1232  1.16  riastrad 			drm_gem_object_put_unlocked(obj);
   1233  1.16  riastrad 			return ret;
   1234  1.16  riastrad 		}
   1235  1.16  riastrad 		WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
   1236  1.16  riastrad 	} else {
   1237  1.16  riastrad 		if (obj->funcs && obj->funcs->vm_ops)
   1238  1.16  riastrad 			vma->vm_ops = obj->funcs->vm_ops;
   1239  1.16  riastrad 		else if (dev->driver->gem_vm_ops)
   1240  1.16  riastrad 			vma->vm_ops = dev->driver->gem_vm_ops;
   1241  1.16  riastrad 		else {
   1242  1.16  riastrad 			drm_gem_object_put_unlocked(obj);
   1243  1.16  riastrad 			return -EINVAL;
   1244  1.16  riastrad 		}
   1245  1.16  riastrad 
   1246  1.16  riastrad 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
   1247  1.16  riastrad 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
   1248  1.16  riastrad 		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
   1249  1.16  riastrad 	}
   1250  1.16  riastrad 
   1251  1.16  riastrad 	vma->vm_private_data = obj;
   1252   1.3  riastrad 
   1253   1.3  riastrad 	return 0;
   1254   1.3  riastrad }
   1255   1.3  riastrad EXPORT_SYMBOL(drm_gem_mmap_obj);
   1256   1.1  riastrad 
   1257   1.1  riastrad /**
   1258   1.1  riastrad  * drm_gem_mmap - memory map routine for GEM objects
   1259   1.1  riastrad  * @filp: DRM file pointer
   1260   1.1  riastrad  * @vma: VMA for the area to be mapped
   1261   1.1  riastrad  *
   1262   1.1  riastrad  * If a driver supports GEM object mapping, mmap calls on the DRM file
   1263   1.1  riastrad  * descriptor will end up here.
   1264   1.1  riastrad  *
   1265   1.3  riastrad  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
   1266   1.1  riastrad  * contain the fake offset we created when the GTT map ioctl was called on
   1267   1.3  riastrad  * the object) and map it with a call to drm_gem_mmap_obj().
   1268   1.3  riastrad  *
   1269   1.3  riastrad  * If the caller is not granted access to the buffer object, the mmap will fail
   1270   1.3  riastrad  * with EACCES. Please see the vma manager for more information.
   1271   1.1  riastrad  */
   1272   1.1  riastrad int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
   1273   1.1  riastrad {
   1274   1.1  riastrad 	struct drm_file *priv = filp->private_data;
   1275   1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
   1276   1.7  riastrad 	struct drm_gem_object *obj = NULL;
   1277   1.3  riastrad 	struct drm_vma_offset_node *node;
   1278   1.3  riastrad 	int ret;
   1279   1.1  riastrad 
   1280  1.16  riastrad 	if (drm_dev_is_unplugged(dev))
   1281   1.1  riastrad 		return -ENODEV;
   1282   1.1  riastrad 
   1283   1.7  riastrad 	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
   1284   1.7  riastrad 	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
   1285   1.7  riastrad 						  vma->vm_pgoff,
   1286   1.7  riastrad 						  vma_pages(vma));
   1287   1.7  riastrad 	if (likely(node)) {
   1288   1.7  riastrad 		obj = container_of(node, struct drm_gem_object, vma_node);
   1289   1.7  riastrad 		/*
   1290   1.7  riastrad 		 * When the object is being freed, after it hits 0-refcnt it
   1291   1.7  riastrad 		 * proceeds to tear down the object. In the process it will
   1292   1.7  riastrad 		 * attempt to remove the VMA offset and so acquire this
   1293   1.7  riastrad 		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
   1294   1.7  riastrad 		 * that matches our range, we know it is in the process of being
   1295   1.7  riastrad 		 * destroyed and will be freed as soon as we release the lock -
   1296   1.7  riastrad 		 * so we have to check for the 0-refcnted object and treat it as
   1297   1.7  riastrad 		 * invalid.
   1298   1.7  riastrad 		 */
   1299   1.7  riastrad 		if (!kref_get_unless_zero(&obj->refcount))
   1300   1.7  riastrad 			obj = NULL;
   1301   1.7  riastrad 	}
   1302   1.7  riastrad 	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
   1303   1.7  riastrad 
   1304   1.7  riastrad 	if (!obj)
   1305   1.7  riastrad 		return -EINVAL;
   1306   1.1  riastrad 
   1307  1.16  riastrad 	if (!drm_vma_node_is_allowed(node, priv)) {
   1308  1.16  riastrad 		drm_gem_object_put_unlocked(obj);
   1309   1.3  riastrad 		return -EACCES;
   1310   1.1  riastrad 	}
   1311   1.1  riastrad 
   1312  1.16  riastrad 	if (node->readonly) {
   1313  1.16  riastrad 		if (vma->vm_flags & VM_WRITE) {
   1314  1.16  riastrad 			drm_gem_object_put_unlocked(obj);
   1315  1.16  riastrad 			return -EINVAL;
   1316  1.16  riastrad 		}
   1317  1.16  riastrad 
   1318  1.16  riastrad 		vma->vm_flags &= ~VM_MAYWRITE;
   1319  1.16  riastrad 	}
   1320  1.16  riastrad 
   1321   1.7  riastrad 	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
   1322   1.7  riastrad 			       vma);
   1323   1.1  riastrad 
   1324  1.16  riastrad 	drm_gem_object_put_unlocked(obj);
   1325   1.1  riastrad 
   1326   1.1  riastrad 	return ret;
   1327   1.1  riastrad }
   1328   1.1  riastrad EXPORT_SYMBOL(drm_gem_mmap);
   1329  1.16  riastrad #endif	/* defined(__NetBSD__) */
   1330  1.16  riastrad 
   1331  1.16  riastrad void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
   1332  1.16  riastrad 			const struct drm_gem_object *obj)
   1333  1.16  riastrad {
   1334  1.16  riastrad 	drm_printf_indent(p, indent, "name=%d\n", obj->name);
   1335  1.16  riastrad 	drm_printf_indent(p, indent, "refcount=%u\n",
   1336  1.16  riastrad 			  kref_read(&obj->refcount));
   1337  1.16  riastrad 	drm_printf_indent(p, indent, "start=%08lx\n",
   1338  1.16  riastrad 			  drm_vma_node_start(&obj->vma_node));
   1339  1.16  riastrad 	drm_printf_indent(p, indent, "size=%zu\n", obj->size);
   1340  1.16  riastrad 	drm_printf_indent(p, indent, "imported=%s\n",
   1341  1.16  riastrad 			  obj->import_attach ? "yes" : "no");
   1342  1.16  riastrad 
   1343  1.16  riastrad 	if (obj->funcs && obj->funcs->print_info)
   1344  1.16  riastrad 		obj->funcs->print_info(p, indent, obj);
   1345  1.16  riastrad 	else if (obj->dev->driver->gem_print_info)
   1346  1.16  riastrad 		obj->dev->driver->gem_print_info(p, indent, obj);
   1347  1.16  riastrad }
   1348  1.16  riastrad 
   1349  1.16  riastrad int drm_gem_pin(struct drm_gem_object *obj)
   1350  1.16  riastrad {
   1351  1.16  riastrad 	if (obj->funcs && obj->funcs->pin)
   1352  1.16  riastrad 		return obj->funcs->pin(obj);
   1353  1.16  riastrad 	else if (obj->dev->driver->gem_prime_pin)
   1354  1.16  riastrad 		return obj->dev->driver->gem_prime_pin(obj);
   1355  1.16  riastrad 	else
   1356  1.16  riastrad 		return 0;
   1357  1.16  riastrad }
   1358   1.2  riastrad 
   1359  1.16  riastrad void drm_gem_unpin(struct drm_gem_object *obj)
   1360  1.16  riastrad {
   1361  1.16  riastrad 	if (obj->funcs && obj->funcs->unpin)
   1362  1.16  riastrad 		obj->funcs->unpin(obj);
   1363  1.16  riastrad 	else if (obj->dev->driver->gem_prime_unpin)
   1364  1.16  riastrad 		obj->dev->driver->gem_prime_unpin(obj);
   1365  1.16  riastrad }
   1366  1.16  riastrad 
   1367  1.16  riastrad void *drm_gem_vmap(struct drm_gem_object *obj)
   1368  1.16  riastrad {
   1369  1.16  riastrad 	void *vaddr;
   1370  1.16  riastrad 
   1371  1.16  riastrad 	if (obj->funcs && obj->funcs->vmap)
   1372  1.16  riastrad 		vaddr = obj->funcs->vmap(obj);
   1373  1.16  riastrad 	else if (obj->dev->driver->gem_prime_vmap)
   1374  1.16  riastrad 		vaddr = obj->dev->driver->gem_prime_vmap(obj);
   1375  1.16  riastrad 	else
   1376  1.16  riastrad 		vaddr = ERR_PTR(-EOPNOTSUPP);
   1377  1.16  riastrad 
   1378  1.16  riastrad 	if (!vaddr)
   1379  1.16  riastrad 		vaddr = ERR_PTR(-ENOMEM);
   1380  1.16  riastrad 
   1381  1.16  riastrad 	return vaddr;
   1382  1.16  riastrad }
   1383  1.16  riastrad 
   1384  1.16  riastrad void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr)
   1385  1.16  riastrad {
   1386  1.16  riastrad 	if (!vaddr)
   1387  1.16  riastrad 		return;
   1388  1.16  riastrad 
   1389  1.16  riastrad 	if (obj->funcs && obj->funcs->vunmap)
   1390  1.16  riastrad 		obj->funcs->vunmap(obj, vaddr);
   1391  1.16  riastrad 	else if (obj->dev->driver->gem_prime_vunmap)
   1392  1.16  riastrad 		obj->dev->driver->gem_prime_vunmap(obj, vaddr);
   1393  1.16  riastrad }
   1394  1.16  riastrad 
   1395  1.16  riastrad /**
   1396  1.16  riastrad  * drm_gem_lock_reservations - Sets up the ww context and acquires
   1397  1.16  riastrad  * the lock on an array of GEM objects.
   1398  1.16  riastrad  *
   1399  1.16  riastrad  * Once you've locked your reservations, you'll want to set up space
   1400  1.16  riastrad  * for your shared fences (if applicable), submit your job, then
   1401  1.16  riastrad  * drm_gem_unlock_reservations().
   1402  1.16  riastrad  *
   1403  1.16  riastrad  * @objs: drm_gem_objects to lock
   1404  1.16  riastrad  * @count: Number of objects in @objs
   1405  1.16  riastrad  * @acquire_ctx: struct ww_acquire_ctx that will be initialized as
   1406  1.16  riastrad  * part of tracking this set of locked reservations.
   1407  1.16  riastrad  */
   1408  1.16  riastrad int
   1409  1.16  riastrad drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
   1410  1.16  riastrad 			  struct ww_acquire_ctx *acquire_ctx)
   1411  1.16  riastrad {
   1412  1.16  riastrad 	int contended = -1;
   1413  1.16  riastrad 	int i, ret;
   1414  1.16  riastrad 
   1415  1.16  riastrad 	ww_acquire_init(acquire_ctx, &reservation_ww_class);
   1416  1.16  riastrad 
   1417  1.16  riastrad retry:
   1418  1.16  riastrad 	if (contended != -1) {
   1419  1.16  riastrad 		struct drm_gem_object *obj = objs[contended];
   1420  1.16  riastrad 
   1421  1.16  riastrad 		ret = dma_resv_lock_slow_interruptible(obj->resv,
   1422  1.16  riastrad 								 acquire_ctx);
   1423  1.16  riastrad 		if (ret) {
   1424  1.16  riastrad 			ww_acquire_done(acquire_ctx);
   1425  1.16  riastrad 			return ret;
   1426  1.16  riastrad 		}
   1427  1.16  riastrad 	}
   1428  1.16  riastrad 
   1429  1.16  riastrad 	for (i = 0; i < count; i++) {
   1430  1.16  riastrad 		if (i == contended)
   1431  1.16  riastrad 			continue;
   1432  1.16  riastrad 
   1433  1.16  riastrad 		ret = dma_resv_lock_interruptible(objs[i]->resv,
   1434  1.16  riastrad 							    acquire_ctx);
   1435  1.16  riastrad 		if (ret) {
   1436  1.16  riastrad 			int j;
   1437  1.16  riastrad 
   1438  1.16  riastrad 			for (j = 0; j < i; j++)
   1439  1.16  riastrad 				dma_resv_unlock(objs[j]->resv);
   1440  1.16  riastrad 
   1441  1.16  riastrad 			if (contended != -1 && contended >= i)
   1442  1.16  riastrad 				dma_resv_unlock(objs[contended]->resv);
   1443  1.16  riastrad 
   1444  1.16  riastrad 			if (ret == -EDEADLK) {
   1445  1.16  riastrad 				contended = i;
   1446  1.16  riastrad 				goto retry;
   1447  1.16  riastrad 			}
   1448  1.16  riastrad 
   1449  1.16  riastrad 			ww_acquire_done(acquire_ctx);
   1450  1.16  riastrad 			return ret;
   1451  1.16  riastrad 		}
   1452  1.16  riastrad 	}
   1453  1.16  riastrad 
   1454  1.16  riastrad 	ww_acquire_done(acquire_ctx);
   1455  1.16  riastrad 
   1456  1.16  riastrad 	return 0;
   1457  1.16  riastrad }
   1458  1.16  riastrad EXPORT_SYMBOL(drm_gem_lock_reservations);
   1459  1.16  riastrad 
   1460  1.16  riastrad void
   1461  1.16  riastrad drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
   1462  1.16  riastrad 			    struct ww_acquire_ctx *acquire_ctx)
   1463  1.16  riastrad {
   1464  1.16  riastrad 	int i;
   1465  1.16  riastrad 
   1466  1.16  riastrad 	for (i = 0; i < count; i++)
   1467  1.16  riastrad 		dma_resv_unlock(objs[i]->resv);
   1468  1.16  riastrad 
   1469  1.16  riastrad 	ww_acquire_fini(acquire_ctx);
   1470  1.16  riastrad }
   1471  1.16  riastrad EXPORT_SYMBOL(drm_gem_unlock_reservations);
   1472  1.16  riastrad 
   1473  1.16  riastrad /**
   1474  1.16  riastrad  * drm_gem_fence_array_add - Adds the fence to an array of fences to be
   1475  1.16  riastrad  * waited on, deduplicating fences from the same context.
   1476  1.16  riastrad  *
   1477  1.16  riastrad  * @fence_array: array of dma_fence * for the job to block on.
   1478  1.16  riastrad  * @fence: the dma_fence to add to the list of dependencies.
   1479  1.16  riastrad  *
   1480  1.16  riastrad  * Returns:
   1481  1.16  riastrad  * 0 on success, or an error on failing to expand the array.
   1482  1.16  riastrad  */
   1483  1.16  riastrad int drm_gem_fence_array_add(struct xarray *fence_array,
   1484  1.16  riastrad 			    struct dma_fence *fence)
   1485  1.16  riastrad {
   1486  1.16  riastrad 	struct dma_fence *entry;
   1487  1.16  riastrad 	unsigned long index;
   1488  1.16  riastrad 	u32 id = 0;
   1489  1.16  riastrad 	int ret;
   1490  1.16  riastrad 
   1491  1.16  riastrad 	if (!fence)
   1492  1.16  riastrad 		return 0;
   1493  1.16  riastrad 
   1494  1.16  riastrad 	/* Deduplicate if we already depend on a fence from the same context.
   1495  1.16  riastrad 	 * This lets the size of the array of deps scale with the number of
   1496  1.16  riastrad 	 * engines involved, rather than the number of BOs.
   1497  1.16  riastrad 	 */
   1498  1.16  riastrad 	xa_for_each(fence_array, index, entry) {
   1499  1.16  riastrad 		if (entry->context != fence->context)
   1500  1.16  riastrad 			continue;
   1501  1.16  riastrad 
   1502  1.16  riastrad 		if (dma_fence_is_later(fence, entry)) {
   1503  1.16  riastrad 			dma_fence_put(entry);
   1504  1.16  riastrad 			xa_store(fence_array, index, fence, GFP_KERNEL);
   1505  1.16  riastrad 		} else {
   1506  1.16  riastrad 			dma_fence_put(fence);
   1507  1.16  riastrad 		}
   1508  1.16  riastrad 		return 0;
   1509  1.16  riastrad 	}
   1510  1.16  riastrad 
   1511  1.16  riastrad 	ret = xa_alloc(fence_array, &id, fence, xa_limit_32b, GFP_KERNEL);
   1512  1.16  riastrad 	if (ret != 0)
   1513  1.16  riastrad 		dma_fence_put(fence);
   1514  1.16  riastrad 
   1515  1.16  riastrad 	return ret;
   1516  1.16  riastrad }
   1517  1.16  riastrad EXPORT_SYMBOL(drm_gem_fence_array_add);
   1518  1.16  riastrad 
   1519  1.16  riastrad /**
   1520  1.16  riastrad  * drm_gem_fence_array_add_implicit - Adds the implicit dependencies tracked
   1521  1.16  riastrad  * in the GEM object's reservation object to an array of dma_fences for use in
   1522  1.16  riastrad  * scheduling a rendering job.
   1523  1.16  riastrad  *
   1524  1.16  riastrad  * This should be called after drm_gem_lock_reservations() on your array of
   1525  1.16  riastrad  * GEM objects used in the job but before updating the reservations with your
   1526  1.16  riastrad  * own fences.
   1527  1.16  riastrad  *
   1528  1.16  riastrad  * @fence_array: array of dma_fence * for the job to block on.
   1529  1.16  riastrad  * @obj: the gem object to add new dependencies from.
   1530  1.16  riastrad  * @write: whether the job might write the object (so we need to depend on
   1531  1.16  riastrad  * shared fences in the reservation object).
   1532  1.16  riastrad  */
   1533  1.16  riastrad int drm_gem_fence_array_add_implicit(struct xarray *fence_array,
   1534  1.16  riastrad 				     struct drm_gem_object *obj,
   1535  1.16  riastrad 				     bool write)
   1536  1.16  riastrad {
   1537  1.16  riastrad 	int ret;
   1538  1.16  riastrad 	struct dma_fence **fences;
   1539  1.16  riastrad 	unsigned int i, fence_count;
   1540  1.16  riastrad 
   1541  1.16  riastrad 	if (!write) {
   1542  1.16  riastrad 		struct dma_fence *fence =
   1543  1.16  riastrad 			dma_resv_get_excl_rcu(obj->resv);
   1544  1.16  riastrad 
   1545  1.16  riastrad 		return drm_gem_fence_array_add(fence_array, fence);
   1546  1.16  riastrad 	}
   1547  1.16  riastrad 
   1548  1.16  riastrad 	ret = dma_resv_get_fences_rcu(obj->resv, NULL,
   1549  1.16  riastrad 						&fence_count, &fences);
   1550  1.16  riastrad 	if (ret || !fence_count)
   1551  1.16  riastrad 		return ret;
   1552  1.16  riastrad 
   1553  1.16  riastrad 	for (i = 0; i < fence_count; i++) {
   1554  1.16  riastrad 		ret = drm_gem_fence_array_add(fence_array, fences[i]);
   1555  1.16  riastrad 		if (ret)
   1556  1.16  riastrad 			break;
   1557  1.16  riastrad 	}
   1558  1.16  riastrad 
   1559  1.16  riastrad 	for (; i < fence_count; i++)
   1560  1.16  riastrad 		dma_fence_put(fences[i]);
   1561  1.16  riastrad 	kfree(fences);
   1562  1.16  riastrad 	return ret;
   1563  1.16  riastrad }
   1564  1.16  riastrad EXPORT_SYMBOL(drm_gem_fence_array_add_implicit);
   1565