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