xref: /openbmc/linux/drivers/gpu/drm/drm_gem.c (revision e0b3d2140ec6f04335eb20f4651ca2b705084268)
1673a394bSEric Anholt /*
2673a394bSEric Anholt  * Copyright © 2008 Intel Corporation
3673a394bSEric Anholt  *
4673a394bSEric Anholt  * Permission is hereby granted, free of charge, to any person obtaining a
5673a394bSEric Anholt  * copy of this software and associated documentation files (the "Software"),
6673a394bSEric Anholt  * to deal in the Software without restriction, including without limitation
7673a394bSEric Anholt  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8673a394bSEric Anholt  * and/or sell copies of the Software, and to permit persons to whom the
9673a394bSEric Anholt  * Software is furnished to do so, subject to the following conditions:
10673a394bSEric Anholt  *
11673a394bSEric Anholt  * The above copyright notice and this permission notice (including the next
12673a394bSEric Anholt  * paragraph) shall be included in all copies or substantial portions of the
13673a394bSEric Anholt  * Software.
14673a394bSEric Anholt  *
15673a394bSEric Anholt  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16673a394bSEric Anholt  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17673a394bSEric Anholt  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18673a394bSEric Anholt  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19673a394bSEric Anholt  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20673a394bSEric Anholt  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21673a394bSEric Anholt  * IN THE SOFTWARE.
22673a394bSEric Anholt  *
23673a394bSEric Anholt  * Authors:
24673a394bSEric Anholt  *    Eric Anholt <eric@anholt.net>
25673a394bSEric Anholt  *
26673a394bSEric Anholt  */
27673a394bSEric Anholt 
28673a394bSEric Anholt #include <linux/types.h>
29673a394bSEric Anholt #include <linux/slab.h>
30673a394bSEric Anholt #include <linux/mm.h>
31673a394bSEric Anholt #include <linux/uaccess.h>
32673a394bSEric Anholt #include <linux/fs.h>
33673a394bSEric Anholt #include <linux/file.h>
34673a394bSEric Anholt #include <linux/module.h>
35673a394bSEric Anholt #include <linux/mman.h>
36673a394bSEric Anholt #include <linux/pagemap.h>
375949eac4SHugh Dickins #include <linux/shmem_fs.h>
383248877eSDave Airlie #include <linux/dma-buf.h>
3995cf9264STom Lendacky #include <linux/mem_encrypt.h>
40fb4b4927SKuo-Hsin Yang #include <linux/pagevec.h>
410500c04eSSam Ravnborg 
421c535876SSam Ravnborg #include <drm/drm.h>
430500c04eSSam Ravnborg #include <drm/drm_device.h>
440500c04eSSam Ravnborg #include <drm/drm_drv.h>
450500c04eSSam Ravnborg #include <drm/drm_file.h>
46d9fc9413SDaniel Vetter #include <drm/drm_gem.h>
47641b9103SDaniel Vetter #include <drm/drm_managed.h>
4845d58b40SNoralf Trønnes #include <drm/drm_print.h>
490500c04eSSam Ravnborg #include <drm/drm_vma_manager.h>
500500c04eSSam Ravnborg 
5167d0ec4eSDaniel Vetter #include "drm_internal.h"
52673a394bSEric Anholt 
53673a394bSEric Anholt /** @file drm_gem.c
54673a394bSEric Anholt  *
55673a394bSEric Anholt  * This file provides some of the base ioctls and library routines for
56673a394bSEric Anholt  * the graphics memory manager implemented by each device driver.
57673a394bSEric Anholt  *
58673a394bSEric Anholt  * Because various devices have different requirements in terms of
59673a394bSEric Anholt  * synchronization and migration strategies, implementing that is left up to
60673a394bSEric Anholt  * the driver, and all that the general API provides should be generic --
61673a394bSEric Anholt  * allocating objects, reading/writing data with the cpu, freeing objects.
62673a394bSEric Anholt  * Even there, platform-dependent optimizations for reading/writing data with
63673a394bSEric Anholt  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
64673a394bSEric Anholt  * the DRI2 implementation wants to have at least allocate/mmap be generic.
65673a394bSEric Anholt  *
66673a394bSEric Anholt  * The goal was to have swap-backed object allocation managed through
67673a394bSEric Anholt  * struct file.  However, file descriptors as handles to a struct file have
68673a394bSEric Anholt  * two major failings:
69673a394bSEric Anholt  * - Process limits prevent more than 1024 or so being used at a time by
70673a394bSEric Anholt  *   default.
71673a394bSEric Anholt  * - Inability to allocate high fds will aggravate the X Server's select()
72673a394bSEric Anholt  *   handling, and likely that of many GL client applications as well.
73673a394bSEric Anholt  *
74673a394bSEric Anholt  * This led to a plan of using our own integer IDs (called handles, following
75673a394bSEric Anholt  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
76673a394bSEric Anholt  * ioctls.  The objects themselves will still include the struct file so
77673a394bSEric Anholt  * that we can transition to fds if the required kernel infrastructure shows
78673a394bSEric Anholt  * up at a later date, and as our interface with shmfs for memory allocation.
79673a394bSEric Anholt  */
80673a394bSEric Anholt 
81641b9103SDaniel Vetter static void
82641b9103SDaniel Vetter drm_gem_init_release(struct drm_device *dev, void *ptr)
83641b9103SDaniel Vetter {
84641b9103SDaniel Vetter 	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
85641b9103SDaniel Vetter }
86641b9103SDaniel Vetter 
87673a394bSEric Anholt /**
8889d61fc0SDaniel Vetter  * drm_gem_init - Initialize the GEM device fields
8989d61fc0SDaniel Vetter  * @dev: drm_devic structure to initialize
90673a394bSEric Anholt  */
91673a394bSEric Anholt int
92673a394bSEric Anholt drm_gem_init(struct drm_device *dev)
93673a394bSEric Anholt {
94b04a5906SDaniel Vetter 	struct drm_vma_offset_manager *vma_offset_manager;
95a2c0a97bSJesse Barnes 
96cd4f013fSDaniel Vetter 	mutex_init(&dev->object_name_lock);
97e86584c5SChris Wilson 	idr_init_base(&dev->object_name_idr, 1);
98a2c0a97bSJesse Barnes 
99641b9103SDaniel Vetter 	vma_offset_manager = drmm_kzalloc(dev, sizeof(*vma_offset_manager),
100641b9103SDaniel Vetter 					  GFP_KERNEL);
101b04a5906SDaniel Vetter 	if (!vma_offset_manager) {
102a2c0a97bSJesse Barnes 		DRM_ERROR("out of memory\n");
103a2c0a97bSJesse Barnes 		return -ENOMEM;
104a2c0a97bSJesse Barnes 	}
105a2c0a97bSJesse Barnes 
106b04a5906SDaniel Vetter 	dev->vma_offset_manager = vma_offset_manager;
107b04a5906SDaniel Vetter 	drm_vma_offset_manager_init(vma_offset_manager,
1080de23977SDavid Herrmann 				    DRM_FILE_PAGE_OFFSET_START,
10977ef8bbcSDavid Herrmann 				    DRM_FILE_PAGE_OFFSET_SIZE);
110a2c0a97bSJesse Barnes 
111641b9103SDaniel Vetter 	return drmm_add_action(dev, drm_gem_init_release, NULL);
112a2c0a97bSJesse Barnes }
113a2c0a97bSJesse Barnes 
114673a394bSEric Anholt /**
11589d61fc0SDaniel Vetter  * drm_gem_object_init - initialize an allocated shmem-backed GEM object
11689d61fc0SDaniel Vetter  * @dev: drm_device the object should be initialized for
11789d61fc0SDaniel Vetter  * @obj: drm_gem_object to initialize
11889d61fc0SDaniel Vetter  * @size: object size
11989d61fc0SDaniel Vetter  *
12062cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
1211d397043SDaniel Vetter  * shmfs backing store.
1221d397043SDaniel Vetter  */
1231d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev,
1241d397043SDaniel Vetter 			struct drm_gem_object *obj, size_t size)
1251d397043SDaniel Vetter {
12689c8233fSDavid Herrmann 	struct file *filp;
1271d397043SDaniel Vetter 
1286ab11a26SDaniel Vetter 	drm_gem_private_object_init(dev, obj, size);
1296ab11a26SDaniel Vetter 
13089c8233fSDavid Herrmann 	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
13189c8233fSDavid Herrmann 	if (IS_ERR(filp))
13289c8233fSDavid Herrmann 		return PTR_ERR(filp);
1331d397043SDaniel Vetter 
13489c8233fSDavid Herrmann 	obj->filp = filp;
1351d397043SDaniel Vetter 
1361d397043SDaniel Vetter 	return 0;
1371d397043SDaniel Vetter }
1381d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init);
1391d397043SDaniel Vetter 
1401d397043SDaniel Vetter /**
1412a5706a3SLaurent Pinchart  * drm_gem_private_object_init - initialize an allocated private GEM object
14289d61fc0SDaniel Vetter  * @dev: drm_device the object should be initialized for
14389d61fc0SDaniel Vetter  * @obj: drm_gem_object to initialize
14489d61fc0SDaniel Vetter  * @size: object size
14589d61fc0SDaniel Vetter  *
14662cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
14762cb7011SAlan Cox  * no GEM provided backing store. Instead the caller is responsible for
14862cb7011SAlan Cox  * backing the object and handling it.
14962cb7011SAlan Cox  */
15089c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev,
15162cb7011SAlan Cox 				 struct drm_gem_object *obj, size_t size)
15262cb7011SAlan Cox {
15362cb7011SAlan Cox 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
15462cb7011SAlan Cox 
15562cb7011SAlan Cox 	obj->dev = dev;
15662cb7011SAlan Cox 	obj->filp = NULL;
15762cb7011SAlan Cox 
15862cb7011SAlan Cox 	kref_init(&obj->refcount);
159a8e11d1cSDaniel Vetter 	obj->handle_count = 0;
16062cb7011SAlan Cox 	obj->size = size;
16152791eeeSChristian König 	dma_resv_init(&obj->_resv);
1621ba62714SRob Herring 	if (!obj->resv)
1631ba62714SRob Herring 		obj->resv = &obj->_resv;
1641ba62714SRob Herring 
16588d7ebe5SDavid Herrmann 	drm_vma_node_reset(&obj->vma_node);
16662cb7011SAlan Cox }
16762cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init);
16862cb7011SAlan Cox 
1690ff926c7SDave Airlie static void
1700ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
1710ff926c7SDave Airlie {
172319c933cSDaniel Vetter 	/*
173319c933cSDaniel Vetter 	 * Note: obj->dma_buf can't disappear as long as we still hold a
174319c933cSDaniel Vetter 	 * handle reference in obj->handle_count.
175319c933cSDaniel Vetter 	 */
176d0b2c533SDaniel Vetter 	mutex_lock(&filp->prime.lock);
177319c933cSDaniel Vetter 	if (obj->dma_buf) {
178d0b2c533SDaniel Vetter 		drm_prime_remove_buf_handle_locked(&filp->prime,
179319c933cSDaniel Vetter 						   obj->dma_buf);
1800ff926c7SDave Airlie 	}
181d0b2c533SDaniel Vetter 	mutex_unlock(&filp->prime.lock);
1820ff926c7SDave Airlie }
1830ff926c7SDave Airlie 
18436da5908SDaniel Vetter /**
185c6a84325SThierry Reding  * drm_gem_object_handle_free - release resources bound to userspace handles
18689d61fc0SDaniel Vetter  * @obj: GEM object to clean up.
18789d61fc0SDaniel Vetter  *
18836da5908SDaniel Vetter  * Called after the last handle to the object has been closed
18936da5908SDaniel Vetter  *
19036da5908SDaniel Vetter  * Removes any name for the object. Note that this must be
19136da5908SDaniel Vetter  * called before drm_gem_object_free or we'll be touching
19236da5908SDaniel Vetter  * freed memory
19336da5908SDaniel Vetter  */
19436da5908SDaniel Vetter static void drm_gem_object_handle_free(struct drm_gem_object *obj)
19536da5908SDaniel Vetter {
19636da5908SDaniel Vetter 	struct drm_device *dev = obj->dev;
19736da5908SDaniel Vetter 
19836da5908SDaniel Vetter 	/* Remove any name for this object */
19936da5908SDaniel Vetter 	if (obj->name) {
20036da5908SDaniel Vetter 		idr_remove(&dev->object_name_idr, obj->name);
20136da5908SDaniel Vetter 		obj->name = 0;
202a8e11d1cSDaniel Vetter 	}
20336da5908SDaniel Vetter }
20436da5908SDaniel Vetter 
205319c933cSDaniel Vetter static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
206319c933cSDaniel Vetter {
207319c933cSDaniel Vetter 	/* Unbreak the reference cycle if we have an exported dma_buf. */
208319c933cSDaniel Vetter 	if (obj->dma_buf) {
209319c933cSDaniel Vetter 		dma_buf_put(obj->dma_buf);
210319c933cSDaniel Vetter 		obj->dma_buf = NULL;
211319c933cSDaniel Vetter 	}
212319c933cSDaniel Vetter }
213319c933cSDaniel Vetter 
214becee2a5SDaniel Vetter static void
215e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
21636da5908SDaniel Vetter {
21798a8883aSChris Wilson 	struct drm_device *dev = obj->dev;
21898a8883aSChris Wilson 	bool final = false;
21998a8883aSChris Wilson 
2206afe6929SChris Wilson 	if (WARN_ON(READ_ONCE(obj->handle_count) == 0))
22136da5908SDaniel Vetter 		return;
22236da5908SDaniel Vetter 
22336da5908SDaniel Vetter 	/*
22436da5908SDaniel Vetter 	* Must bump handle count first as this may be the last
22536da5908SDaniel Vetter 	* ref, in which case the object would disappear before we
22636da5908SDaniel Vetter 	* checked for a name
22736da5908SDaniel Vetter 	*/
22836da5908SDaniel Vetter 
22998a8883aSChris Wilson 	mutex_lock(&dev->object_name_lock);
230319c933cSDaniel Vetter 	if (--obj->handle_count == 0) {
23136da5908SDaniel Vetter 		drm_gem_object_handle_free(obj);
232319c933cSDaniel Vetter 		drm_gem_object_exported_dma_buf_free(obj);
23398a8883aSChris Wilson 		final = true;
234319c933cSDaniel Vetter 	}
23598a8883aSChris Wilson 	mutex_unlock(&dev->object_name_lock);
236a8e11d1cSDaniel Vetter 
23798a8883aSChris Wilson 	if (final)
238be6ee102SEmil Velikov 		drm_gem_object_put(obj);
23936da5908SDaniel Vetter }
24036da5908SDaniel Vetter 
2418815b23aSChris Wilson /*
2428815b23aSChris Wilson  * Called at device or object close to release the file's
2438815b23aSChris Wilson  * handle references on objects.
2448815b23aSChris Wilson  */
2458815b23aSChris Wilson static int
2468815b23aSChris Wilson drm_gem_object_release_handle(int id, void *ptr, void *data)
2478815b23aSChris Wilson {
2488815b23aSChris Wilson 	struct drm_file *file_priv = data;
2498815b23aSChris Wilson 	struct drm_gem_object *obj = ptr;
2508815b23aSChris Wilson 	struct drm_device *dev = obj->dev;
2518815b23aSChris Wilson 
252b39b5394SNoralf Trønnes 	if (obj->funcs && obj->funcs->close)
253b39b5394SNoralf Trønnes 		obj->funcs->close(obj, file_priv);
254b39b5394SNoralf Trønnes 	else if (dev->driver->gem_close_object)
255d0a133f7SChris Wilson 		dev->driver->gem_close_object(obj, file_priv);
256d0a133f7SChris Wilson 
2578815b23aSChris Wilson 	drm_gem_remove_prime_handles(obj, file_priv);
258d9a1f0b4SDavid Herrmann 	drm_vma_node_revoke(&obj->vma_node, file_priv);
2598815b23aSChris Wilson 
260e6b62714SThierry Reding 	drm_gem_object_handle_put_unlocked(obj);
2618815b23aSChris Wilson 
2628815b23aSChris Wilson 	return 0;
2638815b23aSChris Wilson }
2648815b23aSChris Wilson 
265673a394bSEric Anholt /**
26689d61fc0SDaniel Vetter  * drm_gem_handle_delete - deletes the given file-private handle
26789d61fc0SDaniel Vetter  * @filp: drm file-private structure to use for the handle look up
26889d61fc0SDaniel Vetter  * @handle: userspace handle to delete
26989d61fc0SDaniel Vetter  *
270df2e0900SDaniel Vetter  * Removes the GEM handle from the @filp lookup table which has been added with
271df2e0900SDaniel Vetter  * drm_gem_handle_create(). If this is the last handle also cleans up linked
272df2e0900SDaniel Vetter  * resources like GEM names.
273673a394bSEric Anholt  */
274ff72145bSDave Airlie int
275a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle)
276673a394bSEric Anholt {
277673a394bSEric Anholt 	struct drm_gem_object *obj;
278673a394bSEric Anholt 
279673a394bSEric Anholt 	spin_lock(&filp->table_lock);
280673a394bSEric Anholt 
281673a394bSEric Anholt 	/* Check if we currently have a reference on the object */
282f6cd7daeSChris Wilson 	obj = idr_replace(&filp->object_idr, NULL, handle);
283673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
284f6cd7daeSChris Wilson 	if (IS_ERR_OR_NULL(obj))
285673a394bSEric Anholt 		return -EINVAL;
286673a394bSEric Anholt 
287f6cd7daeSChris Wilson 	/* Release driver's reference and decrement refcount. */
288f6cd7daeSChris Wilson 	drm_gem_object_release_handle(handle, obj, filp);
289f6cd7daeSChris Wilson 
290f6cd7daeSChris Wilson 	/* And finally make the handle available for future allocations. */
291f6cd7daeSChris Wilson 	spin_lock(&filp->table_lock);
292673a394bSEric Anholt 	idr_remove(&filp->object_idr, handle);
293673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
294673a394bSEric Anholt 
295673a394bSEric Anholt 	return 0;
296673a394bSEric Anholt }
297ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete);
298673a394bSEric Anholt 
299673a394bSEric Anholt /**
300abd4e745SRob Herring  * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
301db611527SNoralf Trønnes  * @file: drm file-private structure containing the gem object
302db611527SNoralf Trønnes  * @dev: corresponding drm_device
303db611527SNoralf Trønnes  * @handle: gem object handle
304db611527SNoralf Trønnes  * @offset: return location for the fake mmap offset
305db611527SNoralf Trønnes  *
306db611527SNoralf Trønnes  * This implements the &drm_driver.dumb_map_offset kms driver callback for
307db611527SNoralf Trønnes  * drivers which use gem to manage their backing storage.
308db611527SNoralf Trønnes  *
309db611527SNoralf Trønnes  * Returns:
310db611527SNoralf Trønnes  * 0 on success or a negative error code on failure.
311db611527SNoralf Trønnes  */
312abd4e745SRob Herring int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
313db611527SNoralf Trønnes 			    u32 handle, u64 *offset)
314db611527SNoralf Trønnes {
315db611527SNoralf Trønnes 	struct drm_gem_object *obj;
316db611527SNoralf Trønnes 	int ret;
317db611527SNoralf Trønnes 
318db611527SNoralf Trønnes 	obj = drm_gem_object_lookup(file, handle);
319db611527SNoralf Trønnes 	if (!obj)
320db611527SNoralf Trønnes 		return -ENOENT;
321db611527SNoralf Trønnes 
32290378e58SNoralf Trønnes 	/* Don't allow imported objects to be mapped */
32390378e58SNoralf Trønnes 	if (obj->import_attach) {
32490378e58SNoralf Trønnes 		ret = -EINVAL;
32590378e58SNoralf Trønnes 		goto out;
32690378e58SNoralf Trønnes 	}
32790378e58SNoralf Trønnes 
328db611527SNoralf Trønnes 	ret = drm_gem_create_mmap_offset(obj);
329db611527SNoralf Trønnes 	if (ret)
330db611527SNoralf Trønnes 		goto out;
331db611527SNoralf Trønnes 
332db611527SNoralf Trønnes 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
333db611527SNoralf Trønnes out:
334be6ee102SEmil Velikov 	drm_gem_object_put(obj);
335db611527SNoralf Trønnes 
336db611527SNoralf Trønnes 	return ret;
337db611527SNoralf Trønnes }
338abd4e745SRob Herring EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
339db611527SNoralf Trønnes 
340db611527SNoralf Trønnes /**
34143387b37SDaniel Vetter  * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
34289d61fc0SDaniel Vetter  * @file: drm file-private structure to remove the dumb handle from
34389d61fc0SDaniel Vetter  * @dev: corresponding drm_device
34489d61fc0SDaniel Vetter  * @handle: the dumb handle to remove
34543387b37SDaniel Vetter  *
346940eba2dSDaniel Vetter  * This implements the &drm_driver.dumb_destroy kms driver callback for drivers
347940eba2dSDaniel Vetter  * which use gem to manage their backing storage.
34843387b37SDaniel Vetter  */
34943387b37SDaniel Vetter int drm_gem_dumb_destroy(struct drm_file *file,
35043387b37SDaniel Vetter 			 struct drm_device *dev,
35143387b37SDaniel Vetter 			 uint32_t handle)
35243387b37SDaniel Vetter {
35343387b37SDaniel Vetter 	return drm_gem_handle_delete(file, handle);
35443387b37SDaniel Vetter }
35543387b37SDaniel Vetter EXPORT_SYMBOL(drm_gem_dumb_destroy);
35643387b37SDaniel Vetter 
35743387b37SDaniel Vetter /**
35820228c44SDaniel Vetter  * drm_gem_handle_create_tail - internal functions to create a handle
35989d61fc0SDaniel Vetter  * @file_priv: drm file-private structure to register the handle for
36089d61fc0SDaniel Vetter  * @obj: object to register
3618bf8180fSThierry Reding  * @handlep: pointer to return the created handle to the caller
36220228c44SDaniel Vetter  *
363940eba2dSDaniel Vetter  * This expects the &drm_device.object_name_lock to be held already and will
364940eba2dSDaniel Vetter  * drop it before returning. Used to avoid races in establishing new handles
365940eba2dSDaniel Vetter  * when importing an object from either an flink name or a dma-buf.
366df2e0900SDaniel Vetter  *
367df2e0900SDaniel Vetter  * Handles must be release again through drm_gem_handle_delete(). This is done
368df2e0900SDaniel Vetter  * when userspace closes @file_priv for all attached handles, or through the
369df2e0900SDaniel Vetter  * GEM_CLOSE ioctl for individual handles.
370673a394bSEric Anholt  */
371673a394bSEric Anholt int
37220228c44SDaniel Vetter drm_gem_handle_create_tail(struct drm_file *file_priv,
373673a394bSEric Anholt 			   struct drm_gem_object *obj,
374a1a2d1d3SPekka Paalanen 			   u32 *handlep)
375673a394bSEric Anholt {
376304eda32SBen Skeggs 	struct drm_device *dev = obj->dev;
3779649399eSChris Wilson 	u32 handle;
378673a394bSEric Anholt 	int ret;
379673a394bSEric Anholt 
38020228c44SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->object_name_lock));
38198a8883aSChris Wilson 	if (obj->handle_count++ == 0)
382e6b62714SThierry Reding 		drm_gem_object_get(obj);
38320228c44SDaniel Vetter 
384673a394bSEric Anholt 	/*
3852e928815STejun Heo 	 * Get the user-visible handle using idr.  Preload and perform
3862e928815STejun Heo 	 * allocation under our spinlock.
387673a394bSEric Anholt 	 */
3882e928815STejun Heo 	idr_preload(GFP_KERNEL);
389673a394bSEric Anholt 	spin_lock(&file_priv->table_lock);
3902e928815STejun Heo 
3912e928815STejun Heo 	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
39298a8883aSChris Wilson 
393673a394bSEric Anholt 	spin_unlock(&file_priv->table_lock);
3942e928815STejun Heo 	idr_preload_end();
39598a8883aSChris Wilson 
396cd4f013fSDaniel Vetter 	mutex_unlock(&dev->object_name_lock);
3976984128dSChris Wilson 	if (ret < 0)
3986984128dSChris Wilson 		goto err_unref;
3996984128dSChris Wilson 
4009649399eSChris Wilson 	handle = ret;
401673a394bSEric Anholt 
402d9a1f0b4SDavid Herrmann 	ret = drm_vma_node_allow(&obj->vma_node, file_priv);
4036984128dSChris Wilson 	if (ret)
4046984128dSChris Wilson 		goto err_remove;
405304eda32SBen Skeggs 
406b39b5394SNoralf Trønnes 	if (obj->funcs && obj->funcs->open) {
407b39b5394SNoralf Trønnes 		ret = obj->funcs->open(obj, file_priv);
408b39b5394SNoralf Trønnes 		if (ret)
409b39b5394SNoralf Trønnes 			goto err_revoke;
410b39b5394SNoralf Trønnes 	} else if (dev->driver->gem_open_object) {
411304eda32SBen Skeggs 		ret = dev->driver->gem_open_object(obj, file_priv);
4126984128dSChris Wilson 		if (ret)
4136984128dSChris Wilson 			goto err_revoke;
414304eda32SBen Skeggs 	}
415304eda32SBen Skeggs 
4169649399eSChris Wilson 	*handlep = handle;
417673a394bSEric Anholt 	return 0;
4186984128dSChris Wilson 
4196984128dSChris Wilson err_revoke:
420d9a1f0b4SDavid Herrmann 	drm_vma_node_revoke(&obj->vma_node, file_priv);
4216984128dSChris Wilson err_remove:
4226984128dSChris Wilson 	spin_lock(&file_priv->table_lock);
4239649399eSChris Wilson 	idr_remove(&file_priv->object_idr, handle);
4246984128dSChris Wilson 	spin_unlock(&file_priv->table_lock);
4256984128dSChris Wilson err_unref:
426e6b62714SThierry Reding 	drm_gem_object_handle_put_unlocked(obj);
4276984128dSChris Wilson 	return ret;
428673a394bSEric Anholt }
42920228c44SDaniel Vetter 
43020228c44SDaniel Vetter /**
4318bf8180fSThierry Reding  * drm_gem_handle_create - create a gem handle for an object
43289d61fc0SDaniel Vetter  * @file_priv: drm file-private structure to register the handle for
43389d61fc0SDaniel Vetter  * @obj: object to register
43482c0ef94SIgor Matheus Andrade Torrente  * @handlep: pointer to return the created handle to the caller
43589d61fc0SDaniel Vetter  *
43639031176SDaniel Vetter  * Create a handle for this object. This adds a handle reference to the object,
43739031176SDaniel Vetter  * which includes a regular reference count. Callers will likely want to
43839031176SDaniel Vetter  * dereference the object afterwards.
43939031176SDaniel Vetter  *
44039031176SDaniel Vetter  * Since this publishes @obj to userspace it must be fully set up by this point,
44139031176SDaniel Vetter  * drivers must call this last in their buffer object creation callbacks.
44220228c44SDaniel Vetter  */
4438bf8180fSThierry Reding int drm_gem_handle_create(struct drm_file *file_priv,
44420228c44SDaniel Vetter 			  struct drm_gem_object *obj,
44520228c44SDaniel Vetter 			  u32 *handlep)
44620228c44SDaniel Vetter {
44720228c44SDaniel Vetter 	mutex_lock(&obj->dev->object_name_lock);
44820228c44SDaniel Vetter 
44920228c44SDaniel Vetter 	return drm_gem_handle_create_tail(file_priv, obj, handlep);
45020228c44SDaniel Vetter }
451673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create);
452673a394bSEric Anholt 
45375ef8b3bSRob Clark 
45475ef8b3bSRob Clark /**
45575ef8b3bSRob Clark  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
45675ef8b3bSRob Clark  * @obj: obj in question
45775ef8b3bSRob Clark  *
45875ef8b3bSRob Clark  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
459f74418a4SDaniel Vetter  *
460f74418a4SDaniel Vetter  * Note that drm_gem_object_release() already calls this function, so drivers
461f74418a4SDaniel Vetter  * don't have to take care of releasing the mmap offset themselves when freeing
462f74418a4SDaniel Vetter  * the GEM object.
46375ef8b3bSRob Clark  */
46475ef8b3bSRob Clark void
46575ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj)
46675ef8b3bSRob Clark {
46775ef8b3bSRob Clark 	struct drm_device *dev = obj->dev;
46875ef8b3bSRob Clark 
469b04a5906SDaniel Vetter 	drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
47075ef8b3bSRob Clark }
47175ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset);
47275ef8b3bSRob Clark 
47375ef8b3bSRob Clark /**
474367bbd49SRob Clark  * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
475367bbd49SRob Clark  * @obj: obj in question
476367bbd49SRob Clark  * @size: the virtual size
477367bbd49SRob Clark  *
478367bbd49SRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
479367bbd49SRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
480367bbd49SRob Clark  * up the object based on the offset and sets up the various memory mapping
481367bbd49SRob Clark  * structures.
482367bbd49SRob Clark  *
483367bbd49SRob Clark  * This routine allocates and attaches a fake offset for @obj, in cases where
484940eba2dSDaniel Vetter  * the virtual size differs from the physical size (ie. &drm_gem_object.size).
485940eba2dSDaniel Vetter  * Otherwise just use drm_gem_create_mmap_offset().
486f74418a4SDaniel Vetter  *
487f74418a4SDaniel Vetter  * This function is idempotent and handles an already allocated mmap offset
488f74418a4SDaniel Vetter  * transparently. Drivers do not need to check for this case.
489367bbd49SRob Clark  */
490367bbd49SRob Clark int
491367bbd49SRob Clark drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
492367bbd49SRob Clark {
493367bbd49SRob Clark 	struct drm_device *dev = obj->dev;
494367bbd49SRob Clark 
495b04a5906SDaniel Vetter 	return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
496367bbd49SRob Clark 				  size / PAGE_SIZE);
497367bbd49SRob Clark }
498367bbd49SRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
499367bbd49SRob Clark 
500367bbd49SRob Clark /**
50175ef8b3bSRob Clark  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
50275ef8b3bSRob Clark  * @obj: obj in question
50375ef8b3bSRob Clark  *
50475ef8b3bSRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
50575ef8b3bSRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
50675ef8b3bSRob Clark  * up the object based on the offset and sets up the various memory mapping
50775ef8b3bSRob Clark  * structures.
50875ef8b3bSRob Clark  *
50975ef8b3bSRob Clark  * This routine allocates and attaches a fake offset for @obj.
510f74418a4SDaniel Vetter  *
511f74418a4SDaniel Vetter  * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
512f74418a4SDaniel Vetter  * the fake offset again.
51375ef8b3bSRob Clark  */
514367bbd49SRob Clark int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
51575ef8b3bSRob Clark {
516367bbd49SRob Clark 	return drm_gem_create_mmap_offset_size(obj, obj->size);
51775ef8b3bSRob Clark }
51875ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset);
51975ef8b3bSRob Clark 
520fb4b4927SKuo-Hsin Yang /*
521fb4b4927SKuo-Hsin Yang  * Move pages to appropriate lru and release the pagevec, decrementing the
522fb4b4927SKuo-Hsin Yang  * ref count of those pages.
523fb4b4927SKuo-Hsin Yang  */
524fb4b4927SKuo-Hsin Yang static void drm_gem_check_release_pagevec(struct pagevec *pvec)
525fb4b4927SKuo-Hsin Yang {
526fb4b4927SKuo-Hsin Yang 	check_move_unevictable_pages(pvec);
527fb4b4927SKuo-Hsin Yang 	__pagevec_release(pvec);
528fb4b4927SKuo-Hsin Yang 	cond_resched();
529fb4b4927SKuo-Hsin Yang }
530fb4b4927SKuo-Hsin Yang 
531bcc5c9d5SRob Clark /**
532bcc5c9d5SRob Clark  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
533bcc5c9d5SRob Clark  * from shmem
534bcc5c9d5SRob Clark  * @obj: obj in question
5350cdbe8acSDavid Herrmann  *
5360cdbe8acSDavid Herrmann  * This reads the page-array of the shmem-backing storage of the given gem
5370cdbe8acSDavid Herrmann  * object. An array of pages is returned. If a page is not allocated or
5380cdbe8acSDavid Herrmann  * swapped-out, this will allocate/swap-in the required pages. Note that the
5390cdbe8acSDavid Herrmann  * whole object is covered by the page-array and pinned in memory.
5400cdbe8acSDavid Herrmann  *
5410cdbe8acSDavid Herrmann  * Use drm_gem_put_pages() to release the array and unpin all pages.
5420cdbe8acSDavid Herrmann  *
5430cdbe8acSDavid Herrmann  * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
5440cdbe8acSDavid Herrmann  * If you require other GFP-masks, you have to do those allocations yourself.
5450cdbe8acSDavid Herrmann  *
5460cdbe8acSDavid Herrmann  * Note that you are not allowed to change gfp-zones during runtime. That is,
5470cdbe8acSDavid Herrmann  * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
5480cdbe8acSDavid Herrmann  * set during initialization. If you have special zone constraints, set them
5495b9fbfffSJordan Crouse  * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care
5500cdbe8acSDavid Herrmann  * to keep pages in the required zone during swap-in.
551*e0b3d214SDaniel Vetter  *
552*e0b3d214SDaniel Vetter  * This function is only valid on objects initialized with
553*e0b3d214SDaniel Vetter  * drm_gem_object_init(), but not for those initialized with
554*e0b3d214SDaniel Vetter  * drm_gem_private_object_init() only.
555bcc5c9d5SRob Clark  */
5560cdbe8acSDavid Herrmann struct page **drm_gem_get_pages(struct drm_gem_object *obj)
557bcc5c9d5SRob Clark {
558bcc5c9d5SRob Clark 	struct address_space *mapping;
559bcc5c9d5SRob Clark 	struct page *p, **pages;
560fb4b4927SKuo-Hsin Yang 	struct pagevec pvec;
561bcc5c9d5SRob Clark 	int i, npages;
562bcc5c9d5SRob Clark 
563*e0b3d214SDaniel Vetter 
564*e0b3d214SDaniel Vetter 	if (WARN_ON(!obj->filp))
565*e0b3d214SDaniel Vetter 		return ERR_PTR(-EINVAL);
566*e0b3d214SDaniel Vetter 
567bcc5c9d5SRob Clark 	/* This is the shared memory object that backs the GEM resource */
56893c76a3dSAl Viro 	mapping = obj->filp->f_mapping;
569bcc5c9d5SRob Clark 
570bcc5c9d5SRob Clark 	/* We already BUG_ON() for non-page-aligned sizes in
571bcc5c9d5SRob Clark 	 * drm_gem_object_init(), so we should never hit this unless
572bcc5c9d5SRob Clark 	 * driver author is doing something really wrong:
573bcc5c9d5SRob Clark 	 */
574bcc5c9d5SRob Clark 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
575bcc5c9d5SRob Clark 
576bcc5c9d5SRob Clark 	npages = obj->size >> PAGE_SHIFT;
577bcc5c9d5SRob Clark 
5782098105eSMichal Hocko 	pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
579bcc5c9d5SRob Clark 	if (pages == NULL)
580bcc5c9d5SRob Clark 		return ERR_PTR(-ENOMEM);
581bcc5c9d5SRob Clark 
582fb4b4927SKuo-Hsin Yang 	mapping_set_unevictable(mapping);
583fb4b4927SKuo-Hsin Yang 
584bcc5c9d5SRob Clark 	for (i = 0; i < npages; i++) {
5850cdbe8acSDavid Herrmann 		p = shmem_read_mapping_page(mapping, i);
586bcc5c9d5SRob Clark 		if (IS_ERR(p))
587bcc5c9d5SRob Clark 			goto fail;
588bcc5c9d5SRob Clark 		pages[i] = p;
589bcc5c9d5SRob Clark 
5902123000bSDavid Herrmann 		/* Make sure shmem keeps __GFP_DMA32 allocated pages in the
5912123000bSDavid Herrmann 		 * correct region during swapin. Note that this requires
5922123000bSDavid Herrmann 		 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
5932123000bSDavid Herrmann 		 * so shmem can relocate pages during swapin if required.
594bcc5c9d5SRob Clark 		 */
595c62d2555SMichal Hocko 		BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
596bcc5c9d5SRob Clark 				(page_to_pfn(p) >= 0x00100000UL));
597bcc5c9d5SRob Clark 	}
598bcc5c9d5SRob Clark 
599bcc5c9d5SRob Clark 	return pages;
600bcc5c9d5SRob Clark 
601bcc5c9d5SRob Clark fail:
602fb4b4927SKuo-Hsin Yang 	mapping_clear_unevictable(mapping);
603fb4b4927SKuo-Hsin Yang 	pagevec_init(&pvec);
604fb4b4927SKuo-Hsin Yang 	while (i--) {
605fb4b4927SKuo-Hsin Yang 		if (!pagevec_add(&pvec, pages[i]))
606fb4b4927SKuo-Hsin Yang 			drm_gem_check_release_pagevec(&pvec);
607fb4b4927SKuo-Hsin Yang 	}
608fb4b4927SKuo-Hsin Yang 	if (pagevec_count(&pvec))
609fb4b4927SKuo-Hsin Yang 		drm_gem_check_release_pagevec(&pvec);
610bcc5c9d5SRob Clark 
6112098105eSMichal Hocko 	kvfree(pages);
612bcc5c9d5SRob Clark 	return ERR_CAST(p);
613bcc5c9d5SRob Clark }
614bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_get_pages);
615bcc5c9d5SRob Clark 
616bcc5c9d5SRob Clark /**
617bcc5c9d5SRob Clark  * drm_gem_put_pages - helper to free backing pages for a GEM object
618bcc5c9d5SRob Clark  * @obj: obj in question
619bcc5c9d5SRob Clark  * @pages: pages to free
620bcc5c9d5SRob Clark  * @dirty: if true, pages will be marked as dirty
621bcc5c9d5SRob Clark  * @accessed: if true, the pages will be marked as accessed
622bcc5c9d5SRob Clark  */
623bcc5c9d5SRob Clark void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
624bcc5c9d5SRob Clark 		bool dirty, bool accessed)
625bcc5c9d5SRob Clark {
626bcc5c9d5SRob Clark 	int i, npages;
627fb4b4927SKuo-Hsin Yang 	struct address_space *mapping;
628fb4b4927SKuo-Hsin Yang 	struct pagevec pvec;
629fb4b4927SKuo-Hsin Yang 
630fb4b4927SKuo-Hsin Yang 	mapping = file_inode(obj->filp)->i_mapping;
631fb4b4927SKuo-Hsin Yang 	mapping_clear_unevictable(mapping);
632bcc5c9d5SRob Clark 
633bcc5c9d5SRob Clark 	/* We already BUG_ON() for non-page-aligned sizes in
634bcc5c9d5SRob Clark 	 * drm_gem_object_init(), so we should never hit this unless
635bcc5c9d5SRob Clark 	 * driver author is doing something really wrong:
636bcc5c9d5SRob Clark 	 */
637bcc5c9d5SRob Clark 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
638bcc5c9d5SRob Clark 
639bcc5c9d5SRob Clark 	npages = obj->size >> PAGE_SHIFT;
640bcc5c9d5SRob Clark 
641fb4b4927SKuo-Hsin Yang 	pagevec_init(&pvec);
642bcc5c9d5SRob Clark 	for (i = 0; i < npages; i++) {
643930a4024SRob Herring 		if (!pages[i])
644930a4024SRob Herring 			continue;
645930a4024SRob Herring 
646bcc5c9d5SRob Clark 		if (dirty)
647bcc5c9d5SRob Clark 			set_page_dirty(pages[i]);
648bcc5c9d5SRob Clark 
649bcc5c9d5SRob Clark 		if (accessed)
650bcc5c9d5SRob Clark 			mark_page_accessed(pages[i]);
651bcc5c9d5SRob Clark 
652bcc5c9d5SRob Clark 		/* Undo the reference we took when populating the table */
653fb4b4927SKuo-Hsin Yang 		if (!pagevec_add(&pvec, pages[i]))
654fb4b4927SKuo-Hsin Yang 			drm_gem_check_release_pagevec(&pvec);
655bcc5c9d5SRob Clark 	}
656fb4b4927SKuo-Hsin Yang 	if (pagevec_count(&pvec))
657fb4b4927SKuo-Hsin Yang 		drm_gem_check_release_pagevec(&pvec);
658bcc5c9d5SRob Clark 
6592098105eSMichal Hocko 	kvfree(pages);
660bcc5c9d5SRob Clark }
661bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_put_pages);
662bcc5c9d5SRob Clark 
663c117aa4dSRob Herring static int objects_lookup(struct drm_file *filp, u32 *handle, int count,
664c117aa4dSRob Herring 			  struct drm_gem_object **objs)
665c117aa4dSRob Herring {
666c117aa4dSRob Herring 	int i, ret = 0;
667c117aa4dSRob Herring 	struct drm_gem_object *obj;
668c117aa4dSRob Herring 
669c117aa4dSRob Herring 	spin_lock(&filp->table_lock);
670c117aa4dSRob Herring 
671c117aa4dSRob Herring 	for (i = 0; i < count; i++) {
672c117aa4dSRob Herring 		/* Check if we currently have a reference on the object */
673c117aa4dSRob Herring 		obj = idr_find(&filp->object_idr, handle[i]);
674c117aa4dSRob Herring 		if (!obj) {
675c117aa4dSRob Herring 			ret = -ENOENT;
676c117aa4dSRob Herring 			break;
677c117aa4dSRob Herring 		}
678c117aa4dSRob Herring 		drm_gem_object_get(obj);
679c117aa4dSRob Herring 		objs[i] = obj;
680c117aa4dSRob Herring 	}
681c117aa4dSRob Herring 	spin_unlock(&filp->table_lock);
682c117aa4dSRob Herring 
683c117aa4dSRob Herring 	return ret;
684c117aa4dSRob Herring }
685c117aa4dSRob Herring 
686c117aa4dSRob Herring /**
687c117aa4dSRob Herring  * drm_gem_objects_lookup - look up GEM objects from an array of handles
688c117aa4dSRob Herring  * @filp: DRM file private date
689c117aa4dSRob Herring  * @bo_handles: user pointer to array of userspace handle
690c117aa4dSRob Herring  * @count: size of handle array
691c117aa4dSRob Herring  * @objs_out: returned pointer to array of drm_gem_object pointers
692c117aa4dSRob Herring  *
693c117aa4dSRob Herring  * Takes an array of userspace handles and returns a newly allocated array of
694c117aa4dSRob Herring  * GEM objects.
695c117aa4dSRob Herring  *
696c117aa4dSRob Herring  * For a single handle lookup, use drm_gem_object_lookup().
697c117aa4dSRob Herring  *
698c117aa4dSRob Herring  * Returns:
699c117aa4dSRob Herring  *
700c117aa4dSRob Herring  * @objs filled in with GEM object pointers. Returned GEM objects need to be
701be6ee102SEmil Velikov  * released with drm_gem_object_put(). -ENOENT is returned on a lookup
702c117aa4dSRob Herring  * failure. 0 is returned on success.
703c117aa4dSRob Herring  *
704c117aa4dSRob Herring  */
705c117aa4dSRob Herring int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
706c117aa4dSRob Herring 			   int count, struct drm_gem_object ***objs_out)
707c117aa4dSRob Herring {
708c117aa4dSRob Herring 	int ret;
709c117aa4dSRob Herring 	u32 *handles;
710c117aa4dSRob Herring 	struct drm_gem_object **objs;
711c117aa4dSRob Herring 
712c117aa4dSRob Herring 	if (!count)
713c117aa4dSRob Herring 		return 0;
714c117aa4dSRob Herring 
715c117aa4dSRob Herring 	objs = kvmalloc_array(count, sizeof(struct drm_gem_object *),
716c117aa4dSRob Herring 			     GFP_KERNEL | __GFP_ZERO);
717c117aa4dSRob Herring 	if (!objs)
718c117aa4dSRob Herring 		return -ENOMEM;
719c117aa4dSRob Herring 
720ec0bb482SDan Carpenter 	*objs_out = objs;
721ec0bb482SDan Carpenter 
722c117aa4dSRob Herring 	handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
723c117aa4dSRob Herring 	if (!handles) {
724c117aa4dSRob Herring 		ret = -ENOMEM;
725c117aa4dSRob Herring 		goto out;
726c117aa4dSRob Herring 	}
727c117aa4dSRob Herring 
728c117aa4dSRob Herring 	if (copy_from_user(handles, bo_handles, count * sizeof(u32))) {
729c117aa4dSRob Herring 		ret = -EFAULT;
730c117aa4dSRob Herring 		DRM_DEBUG("Failed to copy in GEM handles\n");
731c117aa4dSRob Herring 		goto out;
732c117aa4dSRob Herring 	}
733c117aa4dSRob Herring 
734c117aa4dSRob Herring 	ret = objects_lookup(filp, handles, count, objs);
735c117aa4dSRob Herring out:
736c117aa4dSRob Herring 	kvfree(handles);
737c117aa4dSRob Herring 	return ret;
738c117aa4dSRob Herring 
739c117aa4dSRob Herring }
740c117aa4dSRob Herring EXPORT_SYMBOL(drm_gem_objects_lookup);
741c117aa4dSRob Herring 
742df2e0900SDaniel Vetter /**
7431e55a53aSMatt Roper  * drm_gem_object_lookup - look up a GEM object from its handle
744df2e0900SDaniel Vetter  * @filp: DRM file private date
745df2e0900SDaniel Vetter  * @handle: userspace handle
746df2e0900SDaniel Vetter  *
747df2e0900SDaniel Vetter  * Returns:
748df2e0900SDaniel Vetter  *
749df2e0900SDaniel Vetter  * A reference to the object named by the handle if such exists on @filp, NULL
750df2e0900SDaniel Vetter  * otherwise.
751c117aa4dSRob Herring  *
752c117aa4dSRob Herring  * If looking up an array of handles, use drm_gem_objects_lookup().
753df2e0900SDaniel Vetter  */
754673a394bSEric Anholt struct drm_gem_object *
755a8ad0bd8SChris Wilson drm_gem_object_lookup(struct drm_file *filp, u32 handle)
756673a394bSEric Anholt {
757c117aa4dSRob Herring 	struct drm_gem_object *obj = NULL;
758673a394bSEric Anholt 
759c117aa4dSRob Herring 	objects_lookup(filp, &handle, 1, &obj);
760673a394bSEric Anholt 	return obj;
761673a394bSEric Anholt }
762673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup);
763673a394bSEric Anholt 
764673a394bSEric Anholt /**
76552791eeeSChristian König  * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects
7661ba62714SRob Herring  * shared and/or exclusive fences.
7671ba62714SRob Herring  * @filep: DRM file private date
7681ba62714SRob Herring  * @handle: userspace handle
7691ba62714SRob Herring  * @wait_all: if true, wait on all fences, else wait on just exclusive fence
7701ba62714SRob Herring  * @timeout: timeout value in jiffies or zero to return immediately
7711ba62714SRob Herring  *
7721ba62714SRob Herring  * Returns:
7731ba62714SRob Herring  *
7741ba62714SRob Herring  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
7751ba62714SRob Herring  * greater than 0 on success.
7761ba62714SRob Herring  */
77752791eeeSChristian König long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
7781ba62714SRob Herring 				    bool wait_all, unsigned long timeout)
7791ba62714SRob Herring {
7801ba62714SRob Herring 	long ret;
7811ba62714SRob Herring 	struct drm_gem_object *obj;
7821ba62714SRob Herring 
7831ba62714SRob Herring 	obj = drm_gem_object_lookup(filep, handle);
7841ba62714SRob Herring 	if (!obj) {
7851ba62714SRob Herring 		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
7861ba62714SRob Herring 		return -EINVAL;
7871ba62714SRob Herring 	}
7881ba62714SRob Herring 
78952791eeeSChristian König 	ret = dma_resv_wait_timeout_rcu(obj->resv, wait_all,
7901ba62714SRob Herring 						  true, timeout);
7911ba62714SRob Herring 	if (ret == 0)
7921ba62714SRob Herring 		ret = -ETIME;
7931ba62714SRob Herring 	else if (ret > 0)
7941ba62714SRob Herring 		ret = 0;
7951ba62714SRob Herring 
796be6ee102SEmil Velikov 	drm_gem_object_put(obj);
7971ba62714SRob Herring 
7981ba62714SRob Herring 	return ret;
7991ba62714SRob Herring }
80052791eeeSChristian König EXPORT_SYMBOL(drm_gem_dma_resv_wait);
8011ba62714SRob Herring 
8021ba62714SRob Herring /**
80389d61fc0SDaniel Vetter  * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
80489d61fc0SDaniel Vetter  * @dev: drm_device
80589d61fc0SDaniel Vetter  * @data: ioctl data
80689d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
80789d61fc0SDaniel Vetter  *
808673a394bSEric Anholt  * Releases the handle to an mm object.
809673a394bSEric Anholt  */
810673a394bSEric Anholt int
811673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data,
812673a394bSEric Anholt 		    struct drm_file *file_priv)
813673a394bSEric Anholt {
814673a394bSEric Anholt 	struct drm_gem_close *args = data;
815673a394bSEric Anholt 	int ret;
816673a394bSEric Anholt 
8171bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
81869fdf420SChris Wilson 		return -EOPNOTSUPP;
819673a394bSEric Anholt 
820673a394bSEric Anholt 	ret = drm_gem_handle_delete(file_priv, args->handle);
821673a394bSEric Anholt 
822673a394bSEric Anholt 	return ret;
823673a394bSEric Anholt }
824673a394bSEric Anholt 
825673a394bSEric Anholt /**
82689d61fc0SDaniel Vetter  * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
82789d61fc0SDaniel Vetter  * @dev: drm_device
82889d61fc0SDaniel Vetter  * @data: ioctl data
82989d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
83089d61fc0SDaniel Vetter  *
831673a394bSEric Anholt  * Create a global name for an object, returning the name.
832673a394bSEric Anholt  *
833673a394bSEric Anholt  * Note that the name does not hold a reference; when the object
834673a394bSEric Anholt  * is freed, the name goes away.
835673a394bSEric Anholt  */
836673a394bSEric Anholt int
837673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data,
838673a394bSEric Anholt 		    struct drm_file *file_priv)
839673a394bSEric Anholt {
840673a394bSEric Anholt 	struct drm_gem_flink *args = data;
841673a394bSEric Anholt 	struct drm_gem_object *obj;
842673a394bSEric Anholt 	int ret;
843673a394bSEric Anholt 
8441bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
84569fdf420SChris Wilson 		return -EOPNOTSUPP;
846673a394bSEric Anholt 
847a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, args->handle);
848673a394bSEric Anholt 	if (obj == NULL)
849bf79cb91SChris Wilson 		return -ENOENT;
850673a394bSEric Anholt 
851cd4f013fSDaniel Vetter 	mutex_lock(&dev->object_name_lock);
852a8e11d1cSDaniel Vetter 	/* prevent races with concurrent gem_close. */
853a8e11d1cSDaniel Vetter 	if (obj->handle_count == 0) {
854a8e11d1cSDaniel Vetter 		ret = -ENOENT;
855a8e11d1cSDaniel Vetter 		goto err;
856a8e11d1cSDaniel Vetter 	}
857a8e11d1cSDaniel Vetter 
8588d59bae5SChris Wilson 	if (!obj->name) {
8590f646425SChris Wilson 		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
8602e928815STejun Heo 		if (ret < 0)
8613e49c4f4SChris Wilson 			goto err;
8622e07fb22SYoungJun Cho 
8632e07fb22SYoungJun Cho 		obj->name = ret;
8648d59bae5SChris Wilson 	}
8653e49c4f4SChris Wilson 
8662e07fb22SYoungJun Cho 	args->name = (uint64_t) obj->name;
8672e07fb22SYoungJun Cho 	ret = 0;
8682e07fb22SYoungJun Cho 
8693e49c4f4SChris Wilson err:
870cd4f013fSDaniel Vetter 	mutex_unlock(&dev->object_name_lock);
871be6ee102SEmil Velikov 	drm_gem_object_put(obj);
8723e49c4f4SChris Wilson 	return ret;
873673a394bSEric Anholt }
874673a394bSEric Anholt 
875673a394bSEric Anholt /**
87689d61fc0SDaniel Vetter  * drm_gem_open - implementation of the GEM_OPEN ioctl
87789d61fc0SDaniel Vetter  * @dev: drm_device
87889d61fc0SDaniel Vetter  * @data: ioctl data
87989d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
88089d61fc0SDaniel Vetter  *
881673a394bSEric Anholt  * Open an object using the global name, returning a handle and the size.
882673a394bSEric Anholt  *
883673a394bSEric Anholt  * This handle (of course) holds a reference to the object, so the object
884673a394bSEric Anholt  * will not go away until the handle is deleted.
885673a394bSEric Anholt  */
886673a394bSEric Anholt int
887673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data,
888673a394bSEric Anholt 		   struct drm_file *file_priv)
889673a394bSEric Anholt {
890673a394bSEric Anholt 	struct drm_gem_open *args = data;
891673a394bSEric Anholt 	struct drm_gem_object *obj;
892673a394bSEric Anholt 	int ret;
893a1a2d1d3SPekka Paalanen 	u32 handle;
894673a394bSEric Anholt 
8951bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
89669fdf420SChris Wilson 		return -EOPNOTSUPP;
897673a394bSEric Anholt 
898cd4f013fSDaniel Vetter 	mutex_lock(&dev->object_name_lock);
899673a394bSEric Anholt 	obj = idr_find(&dev->object_name_idr, (int) args->name);
90020228c44SDaniel Vetter 	if (obj) {
901e6b62714SThierry Reding 		drm_gem_object_get(obj);
90220228c44SDaniel Vetter 	} else {
903cd4f013fSDaniel Vetter 		mutex_unlock(&dev->object_name_lock);
904673a394bSEric Anholt 		return -ENOENT;
90520228c44SDaniel Vetter 	}
906673a394bSEric Anholt 
90720228c44SDaniel Vetter 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
90820228c44SDaniel Vetter 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
909be6ee102SEmil Velikov 	drm_gem_object_put(obj);
910673a394bSEric Anholt 	if (ret)
911673a394bSEric Anholt 		return ret;
912673a394bSEric Anholt 
913673a394bSEric Anholt 	args->handle = handle;
914673a394bSEric Anholt 	args->size = obj->size;
915673a394bSEric Anholt 
916673a394bSEric Anholt 	return 0;
917673a394bSEric Anholt }
918673a394bSEric Anholt 
919673a394bSEric Anholt /**
92089d61fc0SDaniel Vetter  * gem_gem_open - initalizes GEM file-private structures at devnode open time
92189d61fc0SDaniel Vetter  * @dev: drm_device which is being opened by userspace
92289d61fc0SDaniel Vetter  * @file_private: drm file-private structure to set up
92389d61fc0SDaniel Vetter  *
924673a394bSEric Anholt  * Called at device open time, sets up the structure for handling refcounting
925673a394bSEric Anholt  * of mm objects.
926673a394bSEric Anholt  */
927673a394bSEric Anholt void
928673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
929673a394bSEric Anholt {
930e86584c5SChris Wilson 	idr_init_base(&file_private->object_idr, 1);
931673a394bSEric Anholt 	spin_lock_init(&file_private->table_lock);
932673a394bSEric Anholt }
933673a394bSEric Anholt 
934673a394bSEric Anholt /**
93589d61fc0SDaniel Vetter  * drm_gem_release - release file-private GEM resources
93689d61fc0SDaniel Vetter  * @dev: drm_device which is being closed by userspace
93789d61fc0SDaniel Vetter  * @file_private: drm file-private structure to clean up
93889d61fc0SDaniel Vetter  *
939673a394bSEric Anholt  * Called at close time when the filp is going away.
940673a394bSEric Anholt  *
941673a394bSEric Anholt  * Releases any remaining references on objects by this filp.
942673a394bSEric Anholt  */
943673a394bSEric Anholt void
944673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
945673a394bSEric Anholt {
946673a394bSEric Anholt 	idr_for_each(&file_private->object_idr,
947304eda32SBen Skeggs 		     &drm_gem_object_release_handle, file_private);
948673a394bSEric Anholt 	idr_destroy(&file_private->object_idr);
949673a394bSEric Anholt }
950673a394bSEric Anholt 
951f74418a4SDaniel Vetter /**
952f74418a4SDaniel Vetter  * drm_gem_object_release - release GEM buffer object resources
953f74418a4SDaniel Vetter  * @obj: GEM buffer object
954f74418a4SDaniel Vetter  *
955f74418a4SDaniel Vetter  * This releases any structures and resources used by @obj and is the invers of
956f74418a4SDaniel Vetter  * drm_gem_object_init().
957f74418a4SDaniel Vetter  */
958fd632aa3SDaniel Vetter void
959fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj)
960c3ae90c0SLuca Barbieri {
961319c933cSDaniel Vetter 	WARN_ON(obj->dma_buf);
962319c933cSDaniel Vetter 
96362cb7011SAlan Cox 	if (obj->filp)
964c3ae90c0SLuca Barbieri 		fput(obj->filp);
96577472347SDavid Herrmann 
96652791eeeSChristian König 	dma_resv_fini(&obj->_resv);
96777472347SDavid Herrmann 	drm_gem_free_mmap_offset(obj);
968c3ae90c0SLuca Barbieri }
969fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release);
970c3ae90c0SLuca Barbieri 
971673a394bSEric Anholt /**
97289d61fc0SDaniel Vetter  * drm_gem_object_free - free a GEM object
97389d61fc0SDaniel Vetter  * @kref: kref of the object to free
97489d61fc0SDaniel Vetter  *
975673a394bSEric Anholt  * Called after the last reference to the object has been lost.
976673a394bSEric Anholt  *
977673a394bSEric Anholt  * Frees the object
978673a394bSEric Anholt  */
979673a394bSEric Anholt void
980673a394bSEric Anholt drm_gem_object_free(struct kref *kref)
981673a394bSEric Anholt {
9826ff774bdSDaniel Vetter 	struct drm_gem_object *obj =
9836ff774bdSDaniel Vetter 		container_of(kref, struct drm_gem_object, refcount);
984673a394bSEric Anholt 	struct drm_device *dev = obj->dev;
985673a394bSEric Anholt 
9861a9458aeSEmil Velikov 	if (obj->funcs)
987b39b5394SNoralf Trønnes 		obj->funcs->free(obj);
9881a9458aeSEmil Velikov 	else if (dev->driver->gem_free_object_unlocked)
9896d3e7fddSDaniel Vetter 		dev->driver->gem_free_object_unlocked(obj);
9906d3e7fddSDaniel Vetter }
991673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free);
992673a394bSEric Anholt 
993df2e0900SDaniel Vetter /**
994eecd7fd8SEmil Velikov  * drm_gem_object_put_locked - release a GEM buffer object reference
9959f0ba539SDaniel Vetter  * @obj: GEM buffer object
9969f0ba539SDaniel Vetter  *
997940eba2dSDaniel Vetter  * This releases a reference to @obj. Callers must hold the
998940eba2dSDaniel Vetter  * &drm_device.struct_mutex lock when calling this function, even when the
999940eba2dSDaniel Vetter  * driver doesn't use &drm_device.struct_mutex for anything.
10009f0ba539SDaniel Vetter  *
10019f0ba539SDaniel Vetter  * For drivers not encumbered with legacy locking use
1002be6ee102SEmil Velikov  * drm_gem_object_put() instead.
10039f0ba539SDaniel Vetter  */
10049f0ba539SDaniel Vetter void
1005eecd7fd8SEmil Velikov drm_gem_object_put_locked(struct drm_gem_object *obj)
10069f0ba539SDaniel Vetter {
10079f0ba539SDaniel Vetter 	if (obj) {
10089f0ba539SDaniel Vetter 		WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
10099f0ba539SDaniel Vetter 
10109f0ba539SDaniel Vetter 		kref_put(&obj->refcount, drm_gem_object_free);
10119f0ba539SDaniel Vetter 	}
10129f0ba539SDaniel Vetter }
1013eecd7fd8SEmil Velikov EXPORT_SYMBOL(drm_gem_object_put_locked);
10149f0ba539SDaniel Vetter 
10159f0ba539SDaniel Vetter /**
1016df2e0900SDaniel Vetter  * drm_gem_vm_open - vma->ops->open implementation for GEM
1017df2e0900SDaniel Vetter  * @vma: VM area structure
1018df2e0900SDaniel Vetter  *
1019df2e0900SDaniel Vetter  * This function implements the #vm_operations_struct open() callback for GEM
1020df2e0900SDaniel Vetter  * drivers. This must be used together with drm_gem_vm_close().
1021df2e0900SDaniel Vetter  */
1022ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma)
1023ab00b3e5SJesse Barnes {
1024ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
1025ab00b3e5SJesse Barnes 
1026e6b62714SThierry Reding 	drm_gem_object_get(obj);
1027ab00b3e5SJesse Barnes }
1028ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open);
1029ab00b3e5SJesse Barnes 
1030df2e0900SDaniel Vetter /**
1031df2e0900SDaniel Vetter  * drm_gem_vm_close - vma->ops->close implementation for GEM
1032df2e0900SDaniel Vetter  * @vma: VM area structure
1033df2e0900SDaniel Vetter  *
1034df2e0900SDaniel Vetter  * This function implements the #vm_operations_struct close() callback for GEM
1035df2e0900SDaniel Vetter  * drivers. This must be used together with drm_gem_vm_open().
1036df2e0900SDaniel Vetter  */
1037ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma)
1038ab00b3e5SJesse Barnes {
1039ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
1040ab00b3e5SJesse Barnes 
1041be6ee102SEmil Velikov 	drm_gem_object_put(obj);
1042ab00b3e5SJesse Barnes }
1043ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close);
1044ab00b3e5SJesse Barnes 
10451c5aafa6SLaurent Pinchart /**
10461c5aafa6SLaurent Pinchart  * drm_gem_mmap_obj - memory map a GEM object
10471c5aafa6SLaurent Pinchart  * @obj: the GEM object to map
10481c5aafa6SLaurent Pinchart  * @obj_size: the object size to be mapped, in bytes
10491c5aafa6SLaurent Pinchart  * @vma: VMA for the area to be mapped
10501c5aafa6SLaurent Pinchart  *
10511c5aafa6SLaurent Pinchart  * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
10521c5aafa6SLaurent Pinchart  * provided by the driver. Depending on their requirements, drivers can either
10531c5aafa6SLaurent Pinchart  * provide a fault handler in their gem_vm_ops (in which case any accesses to
10541c5aafa6SLaurent Pinchart  * the object will be trapped, to perform migration, GTT binding, surface
10551c5aafa6SLaurent Pinchart  * register allocation, or performance monitoring), or mmap the buffer memory
10561c5aafa6SLaurent Pinchart  * synchronously after calling drm_gem_mmap_obj.
10571c5aafa6SLaurent Pinchart  *
10581c5aafa6SLaurent Pinchart  * This function is mainly intended to implement the DMABUF mmap operation, when
10591c5aafa6SLaurent Pinchart  * the GEM object is not looked up based on its fake offset. To implement the
10601c5aafa6SLaurent Pinchart  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
10611c5aafa6SLaurent Pinchart  *
1062ca481c9bSDavid Herrmann  * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
1063ca481c9bSDavid Herrmann  * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
1064ca481c9bSDavid Herrmann  * callers must verify access restrictions before calling this helper.
1065ca481c9bSDavid Herrmann  *
10661c5aafa6SLaurent Pinchart  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
10671c5aafa6SLaurent Pinchart  * size, or if no gem_vm_ops are provided.
10681c5aafa6SLaurent Pinchart  */
10691c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
10701c5aafa6SLaurent Pinchart 		     struct vm_area_struct *vma)
10711c5aafa6SLaurent Pinchart {
10721c5aafa6SLaurent Pinchart 	struct drm_device *dev = obj->dev;
1073c40069cbSGerd Hoffmann 	int ret;
10741c5aafa6SLaurent Pinchart 
10751c5aafa6SLaurent Pinchart 	/* Check for valid size. */
10761c5aafa6SLaurent Pinchart 	if (obj_size < vma->vm_end - vma->vm_start)
10771c5aafa6SLaurent Pinchart 		return -EINVAL;
10781c5aafa6SLaurent Pinchart 
10791c5aafa6SLaurent Pinchart 	/* Take a ref for this mapping of the object, so that the fault
10801c5aafa6SLaurent Pinchart 	 * handler can dereference the mmap offset's pointer to the object.
10811c5aafa6SLaurent Pinchart 	 * This reference is cleaned up by the corresponding vm_close
10821c5aafa6SLaurent Pinchart 	 * (which should happen whether the vma was created by this call, or
10831c5aafa6SLaurent Pinchart 	 * by a vm_open due to mremap or partial unmap or whatever).
10841c5aafa6SLaurent Pinchart 	 */
1085e6b62714SThierry Reding 	drm_gem_object_get(obj);
10861c5aafa6SLaurent Pinchart 
10879786b65bSGerd Hoffmann 	if (obj->funcs && obj->funcs->mmap) {
10889786b65bSGerd Hoffmann 		ret = obj->funcs->mmap(obj, vma);
10899786b65bSGerd Hoffmann 		if (ret) {
1090be6ee102SEmil Velikov 			drm_gem_object_put(obj);
10919786b65bSGerd Hoffmann 			return ret;
10929786b65bSGerd Hoffmann 		}
10939786b65bSGerd Hoffmann 		WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
10949786b65bSGerd Hoffmann 	} else {
10959786b65bSGerd Hoffmann 		if (obj->funcs && obj->funcs->vm_ops)
10969786b65bSGerd Hoffmann 			vma->vm_ops = obj->funcs->vm_ops;
10979786b65bSGerd Hoffmann 		else if (dev->driver->gem_vm_ops)
10989786b65bSGerd Hoffmann 			vma->vm_ops = dev->driver->gem_vm_ops;
10999786b65bSGerd Hoffmann 		else {
1100be6ee102SEmil Velikov 			drm_gem_object_put(obj);
11019786b65bSGerd Hoffmann 			return -EINVAL;
11029786b65bSGerd Hoffmann 		}
11039786b65bSGerd Hoffmann 
11049786b65bSGerd Hoffmann 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
11059786b65bSGerd Hoffmann 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
11069786b65bSGerd Hoffmann 		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
11079786b65bSGerd Hoffmann 	}
11089786b65bSGerd Hoffmann 
11099786b65bSGerd Hoffmann 	vma->vm_private_data = obj;
11109786b65bSGerd Hoffmann 
11111c5aafa6SLaurent Pinchart 	return 0;
11121c5aafa6SLaurent Pinchart }
11131c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj);
1114ab00b3e5SJesse Barnes 
1115a2c0a97bSJesse Barnes /**
1116a2c0a97bSJesse Barnes  * drm_gem_mmap - memory map routine for GEM objects
1117a2c0a97bSJesse Barnes  * @filp: DRM file pointer
1118a2c0a97bSJesse Barnes  * @vma: VMA for the area to be mapped
1119a2c0a97bSJesse Barnes  *
1120a2c0a97bSJesse Barnes  * If a driver supports GEM object mapping, mmap calls on the DRM file
1121a2c0a97bSJesse Barnes  * descriptor will end up here.
1122a2c0a97bSJesse Barnes  *
11231c5aafa6SLaurent Pinchart  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
1124a2c0a97bSJesse Barnes  * contain the fake offset we created when the GTT map ioctl was called on
11251c5aafa6SLaurent Pinchart  * the object) and map it with a call to drm_gem_mmap_obj().
1126ca481c9bSDavid Herrmann  *
1127ca481c9bSDavid Herrmann  * If the caller is not granted access to the buffer object, the mmap will fail
1128ca481c9bSDavid Herrmann  * with EACCES. Please see the vma manager for more information.
1129a2c0a97bSJesse Barnes  */
1130a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
1131a2c0a97bSJesse Barnes {
1132a2c0a97bSJesse Barnes 	struct drm_file *priv = filp->private_data;
1133a2c0a97bSJesse Barnes 	struct drm_device *dev = priv->minor->dev;
11342225cfe4SDaniel Vetter 	struct drm_gem_object *obj = NULL;
11350de23977SDavid Herrmann 	struct drm_vma_offset_node *node;
1136a8469aa8SDavid Herrmann 	int ret;
1137a2c0a97bSJesse Barnes 
1138c07dcd61SDaniel Vetter 	if (drm_dev_is_unplugged(dev))
11392c07a21dSDave Airlie 		return -ENODEV;
11402c07a21dSDave Airlie 
11412225cfe4SDaniel Vetter 	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
11422225cfe4SDaniel Vetter 	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1143b04a5906SDaniel Vetter 						  vma->vm_pgoff,
11440de23977SDavid Herrmann 						  vma_pages(vma));
11452225cfe4SDaniel Vetter 	if (likely(node)) {
11462225cfe4SDaniel Vetter 		obj = container_of(node, struct drm_gem_object, vma_node);
11472225cfe4SDaniel Vetter 		/*
11482225cfe4SDaniel Vetter 		 * When the object is being freed, after it hits 0-refcnt it
11492225cfe4SDaniel Vetter 		 * proceeds to tear down the object. In the process it will
11502225cfe4SDaniel Vetter 		 * attempt to remove the VMA offset and so acquire this
11512225cfe4SDaniel Vetter 		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
11522225cfe4SDaniel Vetter 		 * that matches our range, we know it is in the process of being
11532225cfe4SDaniel Vetter 		 * destroyed and will be freed as soon as we release the lock -
11542225cfe4SDaniel Vetter 		 * so we have to check for the 0-refcnted object and treat it as
11552225cfe4SDaniel Vetter 		 * invalid.
11562225cfe4SDaniel Vetter 		 */
11572225cfe4SDaniel Vetter 		if (!kref_get_unless_zero(&obj->refcount))
11582225cfe4SDaniel Vetter 			obj = NULL;
11592225cfe4SDaniel Vetter 	}
11602225cfe4SDaniel Vetter 	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
11612225cfe4SDaniel Vetter 
11622225cfe4SDaniel Vetter 	if (!obj)
1163197633b9SDaniel Vetter 		return -EINVAL;
11642225cfe4SDaniel Vetter 
1165d9a1f0b4SDavid Herrmann 	if (!drm_vma_node_is_allowed(node, priv)) {
1166be6ee102SEmil Velikov 		drm_gem_object_put(obj);
1167ca481c9bSDavid Herrmann 		return -EACCES;
1168a2c0a97bSJesse Barnes 	}
1169a2c0a97bSJesse Barnes 
11703e977ac6SChris Wilson 	if (node->readonly) {
11713e977ac6SChris Wilson 		if (vma->vm_flags & VM_WRITE) {
1172be6ee102SEmil Velikov 			drm_gem_object_put(obj);
11733e977ac6SChris Wilson 			return -EINVAL;
11743e977ac6SChris Wilson 		}
11753e977ac6SChris Wilson 
11763e977ac6SChris Wilson 		vma->vm_flags &= ~VM_MAYWRITE;
11773e977ac6SChris Wilson 	}
11783e977ac6SChris Wilson 
11792225cfe4SDaniel Vetter 	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
11802225cfe4SDaniel Vetter 			       vma);
1181a2c0a97bSJesse Barnes 
1182be6ee102SEmil Velikov 	drm_gem_object_put(obj);
1183a2c0a97bSJesse Barnes 
1184a2c0a97bSJesse Barnes 	return ret;
1185a2c0a97bSJesse Barnes }
1186a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap);
118745d58b40SNoralf Trønnes 
118845d58b40SNoralf Trønnes void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
118945d58b40SNoralf Trønnes 			const struct drm_gem_object *obj)
119045d58b40SNoralf Trønnes {
119145d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "name=%d\n", obj->name);
119245d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "refcount=%u\n",
119345d58b40SNoralf Trønnes 			  kref_read(&obj->refcount));
119445d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "start=%08lx\n",
119545d58b40SNoralf Trønnes 			  drm_vma_node_start(&obj->vma_node));
119645d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "size=%zu\n", obj->size);
119745d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "imported=%s\n",
119845d58b40SNoralf Trønnes 			  obj->import_attach ? "yes" : "no");
119945d58b40SNoralf Trønnes 
1200b39b5394SNoralf Trønnes 	if (obj->funcs && obj->funcs->print_info)
1201b39b5394SNoralf Trønnes 		obj->funcs->print_info(p, indent, obj);
1202b39b5394SNoralf Trønnes 	else if (obj->dev->driver->gem_print_info)
120345d58b40SNoralf Trønnes 		obj->dev->driver->gem_print_info(p, indent, obj);
120445d58b40SNoralf Trønnes }
1205b39b5394SNoralf Trønnes 
1206b39b5394SNoralf Trønnes int drm_gem_pin(struct drm_gem_object *obj)
1207b39b5394SNoralf Trønnes {
1208b39b5394SNoralf Trønnes 	if (obj->funcs && obj->funcs->pin)
1209b39b5394SNoralf Trønnes 		return obj->funcs->pin(obj);
1210b39b5394SNoralf Trønnes 	else if (obj->dev->driver->gem_prime_pin)
1211b39b5394SNoralf Trønnes 		return obj->dev->driver->gem_prime_pin(obj);
1212b39b5394SNoralf Trønnes 	else
1213b39b5394SNoralf Trønnes 		return 0;
1214b39b5394SNoralf Trønnes }
1215b39b5394SNoralf Trønnes 
1216b39b5394SNoralf Trønnes void drm_gem_unpin(struct drm_gem_object *obj)
1217b39b5394SNoralf Trønnes {
1218b39b5394SNoralf Trønnes 	if (obj->funcs && obj->funcs->unpin)
1219b39b5394SNoralf Trønnes 		obj->funcs->unpin(obj);
1220b39b5394SNoralf Trønnes 	else if (obj->dev->driver->gem_prime_unpin)
1221b39b5394SNoralf Trønnes 		obj->dev->driver->gem_prime_unpin(obj);
1222b39b5394SNoralf Trønnes }
1223b39b5394SNoralf Trønnes 
1224b39b5394SNoralf Trønnes void *drm_gem_vmap(struct drm_gem_object *obj)
1225b39b5394SNoralf Trønnes {
1226b39b5394SNoralf Trønnes 	void *vaddr;
1227b39b5394SNoralf Trønnes 
1228b39b5394SNoralf Trønnes 	if (obj->funcs && obj->funcs->vmap)
1229b39b5394SNoralf Trønnes 		vaddr = obj->funcs->vmap(obj);
1230b39b5394SNoralf Trønnes 	else if (obj->dev->driver->gem_prime_vmap)
1231b39b5394SNoralf Trønnes 		vaddr = obj->dev->driver->gem_prime_vmap(obj);
1232b39b5394SNoralf Trønnes 	else
1233b39b5394SNoralf Trønnes 		vaddr = ERR_PTR(-EOPNOTSUPP);
1234b39b5394SNoralf Trønnes 
1235b39b5394SNoralf Trønnes 	if (!vaddr)
1236b39b5394SNoralf Trønnes 		vaddr = ERR_PTR(-ENOMEM);
1237b39b5394SNoralf Trønnes 
1238b39b5394SNoralf Trønnes 	return vaddr;
1239b39b5394SNoralf Trønnes }
1240b39b5394SNoralf Trønnes 
1241b39b5394SNoralf Trønnes void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr)
1242b39b5394SNoralf Trønnes {
1243b39b5394SNoralf Trønnes 	if (!vaddr)
1244b39b5394SNoralf Trønnes 		return;
1245b39b5394SNoralf Trønnes 
1246b39b5394SNoralf Trønnes 	if (obj->funcs && obj->funcs->vunmap)
1247b39b5394SNoralf Trønnes 		obj->funcs->vunmap(obj, vaddr);
1248b39b5394SNoralf Trønnes 	else if (obj->dev->driver->gem_prime_vunmap)
1249b39b5394SNoralf Trønnes 		obj->dev->driver->gem_prime_vunmap(obj, vaddr);
1250b39b5394SNoralf Trønnes }
12517edc3e3bSEric Anholt 
12527edc3e3bSEric Anholt /**
12537edc3e3bSEric Anholt  * drm_gem_lock_reservations - Sets up the ww context and acquires
12547edc3e3bSEric Anholt  * the lock on an array of GEM objects.
12557edc3e3bSEric Anholt  *
12567edc3e3bSEric Anholt  * Once you've locked your reservations, you'll want to set up space
12577edc3e3bSEric Anholt  * for your shared fences (if applicable), submit your job, then
12587edc3e3bSEric Anholt  * drm_gem_unlock_reservations().
12597edc3e3bSEric Anholt  *
12607edc3e3bSEric Anholt  * @objs: drm_gem_objects to lock
12617edc3e3bSEric Anholt  * @count: Number of objects in @objs
12627edc3e3bSEric Anholt  * @acquire_ctx: struct ww_acquire_ctx that will be initialized as
12637edc3e3bSEric Anholt  * part of tracking this set of locked reservations.
12647edc3e3bSEric Anholt  */
12657edc3e3bSEric Anholt int
12667edc3e3bSEric Anholt drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
12677edc3e3bSEric Anholt 			  struct ww_acquire_ctx *acquire_ctx)
12687edc3e3bSEric Anholt {
12697edc3e3bSEric Anholt 	int contended = -1;
12707edc3e3bSEric Anholt 	int i, ret;
12717edc3e3bSEric Anholt 
12727edc3e3bSEric Anholt 	ww_acquire_init(acquire_ctx, &reservation_ww_class);
12737edc3e3bSEric Anholt 
12747edc3e3bSEric Anholt retry:
12757edc3e3bSEric Anholt 	if (contended != -1) {
12767edc3e3bSEric Anholt 		struct drm_gem_object *obj = objs[contended];
12777edc3e3bSEric Anholt 
127852791eeeSChristian König 		ret = dma_resv_lock_slow_interruptible(obj->resv,
12797edc3e3bSEric Anholt 								 acquire_ctx);
12807edc3e3bSEric Anholt 		if (ret) {
12817edc3e3bSEric Anholt 			ww_acquire_done(acquire_ctx);
12827edc3e3bSEric Anholt 			return ret;
12837edc3e3bSEric Anholt 		}
12847edc3e3bSEric Anholt 	}
12857edc3e3bSEric Anholt 
12867edc3e3bSEric Anholt 	for (i = 0; i < count; i++) {
12877edc3e3bSEric Anholt 		if (i == contended)
12887edc3e3bSEric Anholt 			continue;
12897edc3e3bSEric Anholt 
129052791eeeSChristian König 		ret = dma_resv_lock_interruptible(objs[i]->resv,
12917edc3e3bSEric Anholt 							    acquire_ctx);
12927edc3e3bSEric Anholt 		if (ret) {
12937edc3e3bSEric Anholt 			int j;
12947edc3e3bSEric Anholt 
12957edc3e3bSEric Anholt 			for (j = 0; j < i; j++)
129652791eeeSChristian König 				dma_resv_unlock(objs[j]->resv);
12977edc3e3bSEric Anholt 
12987edc3e3bSEric Anholt 			if (contended != -1 && contended >= i)
129952791eeeSChristian König 				dma_resv_unlock(objs[contended]->resv);
13007edc3e3bSEric Anholt 
13017edc3e3bSEric Anholt 			if (ret == -EDEADLK) {
13027edc3e3bSEric Anholt 				contended = i;
13037edc3e3bSEric Anholt 				goto retry;
13047edc3e3bSEric Anholt 			}
13057edc3e3bSEric Anholt 
13067edc3e3bSEric Anholt 			ww_acquire_done(acquire_ctx);
13077edc3e3bSEric Anholt 			return ret;
13087edc3e3bSEric Anholt 		}
13097edc3e3bSEric Anholt 	}
13107edc3e3bSEric Anholt 
13117edc3e3bSEric Anholt 	ww_acquire_done(acquire_ctx);
13127edc3e3bSEric Anholt 
13137edc3e3bSEric Anholt 	return 0;
13147edc3e3bSEric Anholt }
13157edc3e3bSEric Anholt EXPORT_SYMBOL(drm_gem_lock_reservations);
13167edc3e3bSEric Anholt 
13177edc3e3bSEric Anholt void
13187edc3e3bSEric Anholt drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
13197edc3e3bSEric Anholt 			    struct ww_acquire_ctx *acquire_ctx)
13207edc3e3bSEric Anholt {
13217edc3e3bSEric Anholt 	int i;
13227edc3e3bSEric Anholt 
13237edc3e3bSEric Anholt 	for (i = 0; i < count; i++)
132452791eeeSChristian König 		dma_resv_unlock(objs[i]->resv);
13257edc3e3bSEric Anholt 
13267edc3e3bSEric Anholt 	ww_acquire_fini(acquire_ctx);
13277edc3e3bSEric Anholt }
13287edc3e3bSEric Anholt EXPORT_SYMBOL(drm_gem_unlock_reservations);
13295d5a179dSEric Anholt 
13305d5a179dSEric Anholt /**
13315d5a179dSEric Anholt  * drm_gem_fence_array_add - Adds the fence to an array of fences to be
13325d5a179dSEric Anholt  * waited on, deduplicating fences from the same context.
13335d5a179dSEric Anholt  *
1334761e473fSSean Paul  * @fence_array: array of dma_fence * for the job to block on.
1335761e473fSSean Paul  * @fence: the dma_fence to add to the list of dependencies.
13365d5a179dSEric Anholt  *
13375d5a179dSEric Anholt  * Returns:
13385d5a179dSEric Anholt  * 0 on success, or an error on failing to expand the array.
13395d5a179dSEric Anholt  */
13405d5a179dSEric Anholt int drm_gem_fence_array_add(struct xarray *fence_array,
13415d5a179dSEric Anholt 			    struct dma_fence *fence)
13425d5a179dSEric Anholt {
13435d5a179dSEric Anholt 	struct dma_fence *entry;
13445d5a179dSEric Anholt 	unsigned long index;
13455d5a179dSEric Anholt 	u32 id = 0;
13465d5a179dSEric Anholt 	int ret;
13475d5a179dSEric Anholt 
13485d5a179dSEric Anholt 	if (!fence)
13495d5a179dSEric Anholt 		return 0;
13505d5a179dSEric Anholt 
13515d5a179dSEric Anholt 	/* Deduplicate if we already depend on a fence from the same context.
13525d5a179dSEric Anholt 	 * This lets the size of the array of deps scale with the number of
13535d5a179dSEric Anholt 	 * engines involved, rather than the number of BOs.
13545d5a179dSEric Anholt 	 */
13555d5a179dSEric Anholt 	xa_for_each(fence_array, index, entry) {
13565d5a179dSEric Anholt 		if (entry->context != fence->context)
13575d5a179dSEric Anholt 			continue;
13585d5a179dSEric Anholt 
13595d5a179dSEric Anholt 		if (dma_fence_is_later(fence, entry)) {
13605d5a179dSEric Anholt 			dma_fence_put(entry);
13615d5a179dSEric Anholt 			xa_store(fence_array, index, fence, GFP_KERNEL);
13625d5a179dSEric Anholt 		} else {
13635d5a179dSEric Anholt 			dma_fence_put(fence);
13645d5a179dSEric Anholt 		}
13655d5a179dSEric Anholt 		return 0;
13665d5a179dSEric Anholt 	}
13675d5a179dSEric Anholt 
13685d5a179dSEric Anholt 	ret = xa_alloc(fence_array, &id, fence, xa_limit_32b, GFP_KERNEL);
13695d5a179dSEric Anholt 	if (ret != 0)
13705d5a179dSEric Anholt 		dma_fence_put(fence);
13715d5a179dSEric Anholt 
13725d5a179dSEric Anholt 	return ret;
13735d5a179dSEric Anholt }
13745d5a179dSEric Anholt EXPORT_SYMBOL(drm_gem_fence_array_add);
13755d5a179dSEric Anholt 
13765d5a179dSEric Anholt /**
13775d5a179dSEric Anholt  * drm_gem_fence_array_add_implicit - Adds the implicit dependencies tracked
13785d5a179dSEric Anholt  * in the GEM object's reservation object to an array of dma_fences for use in
13795d5a179dSEric Anholt  * scheduling a rendering job.
13805d5a179dSEric Anholt  *
13815d5a179dSEric Anholt  * This should be called after drm_gem_lock_reservations() on your array of
13825d5a179dSEric Anholt  * GEM objects used in the job but before updating the reservations with your
13835d5a179dSEric Anholt  * own fences.
13845d5a179dSEric Anholt  *
1385761e473fSSean Paul  * @fence_array: array of dma_fence * for the job to block on.
1386761e473fSSean Paul  * @obj: the gem object to add new dependencies from.
1387761e473fSSean Paul  * @write: whether the job might write the object (so we need to depend on
13885d5a179dSEric Anholt  * shared fences in the reservation object).
13895d5a179dSEric Anholt  */
13905d5a179dSEric Anholt int drm_gem_fence_array_add_implicit(struct xarray *fence_array,
13915d5a179dSEric Anholt 				     struct drm_gem_object *obj,
13925d5a179dSEric Anholt 				     bool write)
13935d5a179dSEric Anholt {
13945d5a179dSEric Anholt 	int ret;
13955d5a179dSEric Anholt 	struct dma_fence **fences;
13965d5a179dSEric Anholt 	unsigned int i, fence_count;
13975d5a179dSEric Anholt 
13985d5a179dSEric Anholt 	if (!write) {
13995d5a179dSEric Anholt 		struct dma_fence *fence =
140052791eeeSChristian König 			dma_resv_get_excl_rcu(obj->resv);
14015d5a179dSEric Anholt 
14025d5a179dSEric Anholt 		return drm_gem_fence_array_add(fence_array, fence);
14035d5a179dSEric Anholt 	}
14045d5a179dSEric Anholt 
140552791eeeSChristian König 	ret = dma_resv_get_fences_rcu(obj->resv, NULL,
14065d5a179dSEric Anholt 						&fence_count, &fences);
14075d5a179dSEric Anholt 	if (ret || !fence_count)
14085d5a179dSEric Anholt 		return ret;
14095d5a179dSEric Anholt 
14105d5a179dSEric Anholt 	for (i = 0; i < fence_count; i++) {
14115d5a179dSEric Anholt 		ret = drm_gem_fence_array_add(fence_array, fences[i]);
14125d5a179dSEric Anholt 		if (ret)
14135d5a179dSEric Anholt 			break;
14145d5a179dSEric Anholt 	}
14155d5a179dSEric Anholt 
14165d5a179dSEric Anholt 	for (; i < fence_count; i++)
14175d5a179dSEric Anholt 		dma_fence_put(fences[i]);
14185d5a179dSEric Anholt 	kfree(fences);
14195d5a179dSEric Anholt 	return ret;
14205d5a179dSEric Anholt }
14215d5a179dSEric Anholt EXPORT_SYMBOL(drm_gem_fence_array_add_implicit);
1422