xref: /openbmc/linux/drivers/gpu/drm/drm_gem.c (revision 7938f4218168ae9fc4bdddb15976f9ebbae41999)
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>
39*7938f421SLucas De Marchi #include <linux/iosys-map.h>
4095cf9264STom Lendacky #include <linux/mem_encrypt.h>
41fb4b4927SKuo-Hsin Yang #include <linux/pagevec.h>
420500c04eSSam Ravnborg 
431c535876SSam Ravnborg #include <drm/drm.h>
440500c04eSSam Ravnborg #include <drm/drm_device.h>
450500c04eSSam Ravnborg #include <drm/drm_drv.h>
460500c04eSSam Ravnborg #include <drm/drm_file.h>
47d9fc9413SDaniel Vetter #include <drm/drm_gem.h>
48641b9103SDaniel Vetter #include <drm/drm_managed.h>
4945d58b40SNoralf Trønnes #include <drm/drm_print.h>
500500c04eSSam Ravnborg #include <drm/drm_vma_manager.h>
510500c04eSSam Ravnborg 
5267d0ec4eSDaniel Vetter #include "drm_internal.h"
53673a394bSEric Anholt 
54673a394bSEric Anholt /** @file drm_gem.c
55673a394bSEric Anholt  *
56673a394bSEric Anholt  * This file provides some of the base ioctls and library routines for
57673a394bSEric Anholt  * the graphics memory manager implemented by each device driver.
58673a394bSEric Anholt  *
59673a394bSEric Anholt  * Because various devices have different requirements in terms of
60673a394bSEric Anholt  * synchronization and migration strategies, implementing that is left up to
61673a394bSEric Anholt  * the driver, and all that the general API provides should be generic --
62673a394bSEric Anholt  * allocating objects, reading/writing data with the cpu, freeing objects.
63673a394bSEric Anholt  * Even there, platform-dependent optimizations for reading/writing data with
64673a394bSEric Anholt  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
65673a394bSEric Anholt  * the DRI2 implementation wants to have at least allocate/mmap be generic.
66673a394bSEric Anholt  *
67673a394bSEric Anholt  * The goal was to have swap-backed object allocation managed through
68673a394bSEric Anholt  * struct file.  However, file descriptors as handles to a struct file have
69673a394bSEric Anholt  * two major failings:
70673a394bSEric Anholt  * - Process limits prevent more than 1024 or so being used at a time by
71673a394bSEric Anholt  *   default.
72673a394bSEric Anholt  * - Inability to allocate high fds will aggravate the X Server's select()
73673a394bSEric Anholt  *   handling, and likely that of many GL client applications as well.
74673a394bSEric Anholt  *
75673a394bSEric Anholt  * This led to a plan of using our own integer IDs (called handles, following
76673a394bSEric Anholt  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
77673a394bSEric Anholt  * ioctls.  The objects themselves will still include the struct file so
78673a394bSEric Anholt  * that we can transition to fds if the required kernel infrastructure shows
79673a394bSEric Anholt  * up at a later date, and as our interface with shmfs for memory allocation.
80673a394bSEric Anholt  */
81673a394bSEric Anholt 
82641b9103SDaniel Vetter static void
83641b9103SDaniel Vetter drm_gem_init_release(struct drm_device *dev, void *ptr)
84641b9103SDaniel Vetter {
85641b9103SDaniel Vetter 	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
86641b9103SDaniel Vetter }
87641b9103SDaniel Vetter 
88673a394bSEric Anholt /**
8989d61fc0SDaniel Vetter  * drm_gem_init - Initialize the GEM device fields
9089d61fc0SDaniel Vetter  * @dev: drm_devic structure to initialize
91673a394bSEric Anholt  */
92673a394bSEric Anholt int
93673a394bSEric Anholt drm_gem_init(struct drm_device *dev)
94673a394bSEric Anholt {
95b04a5906SDaniel Vetter 	struct drm_vma_offset_manager *vma_offset_manager;
96a2c0a97bSJesse Barnes 
97cd4f013fSDaniel Vetter 	mutex_init(&dev->object_name_lock);
98e86584c5SChris Wilson 	idr_init_base(&dev->object_name_idr, 1);
99a2c0a97bSJesse Barnes 
100641b9103SDaniel Vetter 	vma_offset_manager = drmm_kzalloc(dev, sizeof(*vma_offset_manager),
101641b9103SDaniel Vetter 					  GFP_KERNEL);
102b04a5906SDaniel Vetter 	if (!vma_offset_manager) {
103a2c0a97bSJesse Barnes 		DRM_ERROR("out of memory\n");
104a2c0a97bSJesse Barnes 		return -ENOMEM;
105a2c0a97bSJesse Barnes 	}
106a2c0a97bSJesse Barnes 
107b04a5906SDaniel Vetter 	dev->vma_offset_manager = vma_offset_manager;
108b04a5906SDaniel Vetter 	drm_vma_offset_manager_init(vma_offset_manager,
1090de23977SDavid Herrmann 				    DRM_FILE_PAGE_OFFSET_START,
11077ef8bbcSDavid Herrmann 				    DRM_FILE_PAGE_OFFSET_SIZE);
111a2c0a97bSJesse Barnes 
112641b9103SDaniel Vetter 	return drmm_add_action(dev, drm_gem_init_release, NULL);
113a2c0a97bSJesse Barnes }
114a2c0a97bSJesse Barnes 
115673a394bSEric Anholt /**
11689d61fc0SDaniel Vetter  * drm_gem_object_init - initialize an allocated shmem-backed GEM object
11789d61fc0SDaniel Vetter  * @dev: drm_device the object should be initialized for
11889d61fc0SDaniel Vetter  * @obj: drm_gem_object to initialize
11989d61fc0SDaniel Vetter  * @size: object size
12089d61fc0SDaniel Vetter  *
12162cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
1221d397043SDaniel Vetter  * shmfs backing store.
1231d397043SDaniel Vetter  */
1241d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev,
1251d397043SDaniel Vetter 			struct drm_gem_object *obj, size_t size)
1261d397043SDaniel Vetter {
12789c8233fSDavid Herrmann 	struct file *filp;
1281d397043SDaniel Vetter 
1296ab11a26SDaniel Vetter 	drm_gem_private_object_init(dev, obj, size);
1306ab11a26SDaniel Vetter 
13189c8233fSDavid Herrmann 	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
13289c8233fSDavid Herrmann 	if (IS_ERR(filp))
13389c8233fSDavid Herrmann 		return PTR_ERR(filp);
1341d397043SDaniel Vetter 
13589c8233fSDavid Herrmann 	obj->filp = filp;
1361d397043SDaniel Vetter 
1371d397043SDaniel Vetter 	return 0;
1381d397043SDaniel Vetter }
1391d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init);
1401d397043SDaniel Vetter 
1411d397043SDaniel Vetter /**
1422a5706a3SLaurent Pinchart  * drm_gem_private_object_init - initialize an allocated private GEM object
14389d61fc0SDaniel Vetter  * @dev: drm_device the object should be initialized for
14489d61fc0SDaniel Vetter  * @obj: drm_gem_object to initialize
14589d61fc0SDaniel Vetter  * @size: object size
14689d61fc0SDaniel Vetter  *
14762cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
14862cb7011SAlan Cox  * no GEM provided backing store. Instead the caller is responsible for
14962cb7011SAlan Cox  * backing the object and handling it.
15062cb7011SAlan Cox  */
15189c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev,
15262cb7011SAlan Cox 				 struct drm_gem_object *obj, size_t size)
15362cb7011SAlan Cox {
15462cb7011SAlan Cox 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
15562cb7011SAlan Cox 
15662cb7011SAlan Cox 	obj->dev = dev;
15762cb7011SAlan Cox 	obj->filp = NULL;
15862cb7011SAlan Cox 
15962cb7011SAlan Cox 	kref_init(&obj->refcount);
160a8e11d1cSDaniel Vetter 	obj->handle_count = 0;
16162cb7011SAlan Cox 	obj->size = size;
16252791eeeSChristian König 	dma_resv_init(&obj->_resv);
1631ba62714SRob Herring 	if (!obj->resv)
1641ba62714SRob Herring 		obj->resv = &obj->_resv;
1651ba62714SRob Herring 
16688d7ebe5SDavid Herrmann 	drm_vma_node_reset(&obj->vma_node);
16762cb7011SAlan Cox }
16862cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init);
16962cb7011SAlan Cox 
1700ff926c7SDave Airlie static void
1710ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
1720ff926c7SDave Airlie {
173319c933cSDaniel Vetter 	/*
174319c933cSDaniel Vetter 	 * Note: obj->dma_buf can't disappear as long as we still hold a
175319c933cSDaniel Vetter 	 * handle reference in obj->handle_count.
176319c933cSDaniel Vetter 	 */
177d0b2c533SDaniel Vetter 	mutex_lock(&filp->prime.lock);
178319c933cSDaniel Vetter 	if (obj->dma_buf) {
179d0b2c533SDaniel Vetter 		drm_prime_remove_buf_handle_locked(&filp->prime,
180319c933cSDaniel Vetter 						   obj->dma_buf);
1810ff926c7SDave Airlie 	}
182d0b2c533SDaniel Vetter 	mutex_unlock(&filp->prime.lock);
1830ff926c7SDave Airlie }
1840ff926c7SDave Airlie 
18536da5908SDaniel Vetter /**
186c6a84325SThierry Reding  * drm_gem_object_handle_free - release resources bound to userspace handles
18789d61fc0SDaniel Vetter  * @obj: GEM object to clean up.
18889d61fc0SDaniel Vetter  *
18936da5908SDaniel Vetter  * Called after the last handle to the object has been closed
19036da5908SDaniel Vetter  *
19136da5908SDaniel Vetter  * Removes any name for the object. Note that this must be
19236da5908SDaniel Vetter  * called before drm_gem_object_free or we'll be touching
19336da5908SDaniel Vetter  * freed memory
19436da5908SDaniel Vetter  */
19536da5908SDaniel Vetter static void drm_gem_object_handle_free(struct drm_gem_object *obj)
19636da5908SDaniel Vetter {
19736da5908SDaniel Vetter 	struct drm_device *dev = obj->dev;
19836da5908SDaniel Vetter 
19936da5908SDaniel Vetter 	/* Remove any name for this object */
20036da5908SDaniel Vetter 	if (obj->name) {
20136da5908SDaniel Vetter 		idr_remove(&dev->object_name_idr, obj->name);
20236da5908SDaniel Vetter 		obj->name = 0;
203a8e11d1cSDaniel Vetter 	}
20436da5908SDaniel Vetter }
20536da5908SDaniel Vetter 
206319c933cSDaniel Vetter static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
207319c933cSDaniel Vetter {
208319c933cSDaniel Vetter 	/* Unbreak the reference cycle if we have an exported dma_buf. */
209319c933cSDaniel Vetter 	if (obj->dma_buf) {
210319c933cSDaniel Vetter 		dma_buf_put(obj->dma_buf);
211319c933cSDaniel Vetter 		obj->dma_buf = NULL;
212319c933cSDaniel Vetter 	}
213319c933cSDaniel Vetter }
214319c933cSDaniel Vetter 
215becee2a5SDaniel Vetter static void
216e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
21736da5908SDaniel Vetter {
21898a8883aSChris Wilson 	struct drm_device *dev = obj->dev;
21998a8883aSChris Wilson 	bool final = false;
22098a8883aSChris Wilson 
2216afe6929SChris Wilson 	if (WARN_ON(READ_ONCE(obj->handle_count) == 0))
22236da5908SDaniel Vetter 		return;
22336da5908SDaniel Vetter 
22436da5908SDaniel Vetter 	/*
22536da5908SDaniel Vetter 	* Must bump handle count first as this may be the last
22636da5908SDaniel Vetter 	* ref, in which case the object would disappear before we
22736da5908SDaniel Vetter 	* checked for a name
22836da5908SDaniel Vetter 	*/
22936da5908SDaniel Vetter 
23098a8883aSChris Wilson 	mutex_lock(&dev->object_name_lock);
231319c933cSDaniel Vetter 	if (--obj->handle_count == 0) {
23236da5908SDaniel Vetter 		drm_gem_object_handle_free(obj);
233319c933cSDaniel Vetter 		drm_gem_object_exported_dma_buf_free(obj);
23498a8883aSChris Wilson 		final = true;
235319c933cSDaniel Vetter 	}
23698a8883aSChris Wilson 	mutex_unlock(&dev->object_name_lock);
237a8e11d1cSDaniel Vetter 
23898a8883aSChris Wilson 	if (final)
239be6ee102SEmil Velikov 		drm_gem_object_put(obj);
24036da5908SDaniel Vetter }
24136da5908SDaniel Vetter 
2428815b23aSChris Wilson /*
2438815b23aSChris Wilson  * Called at device or object close to release the file's
2448815b23aSChris Wilson  * handle references on objects.
2458815b23aSChris Wilson  */
2468815b23aSChris Wilson static int
2478815b23aSChris Wilson drm_gem_object_release_handle(int id, void *ptr, void *data)
2488815b23aSChris Wilson {
2498815b23aSChris Wilson 	struct drm_file *file_priv = data;
2508815b23aSChris Wilson 	struct drm_gem_object *obj = ptr;
2518815b23aSChris Wilson 
252d693def4SThomas Zimmermann 	if (obj->funcs->close)
253b39b5394SNoralf Trønnes 		obj->funcs->close(obj, file_priv);
254d0a133f7SChris Wilson 
2558815b23aSChris Wilson 	drm_gem_remove_prime_handles(obj, file_priv);
256d9a1f0b4SDavid Herrmann 	drm_vma_node_revoke(&obj->vma_node, file_priv);
2578815b23aSChris Wilson 
258e6b62714SThierry Reding 	drm_gem_object_handle_put_unlocked(obj);
2598815b23aSChris Wilson 
2608815b23aSChris Wilson 	return 0;
2618815b23aSChris Wilson }
2628815b23aSChris Wilson 
263673a394bSEric Anholt /**
26489d61fc0SDaniel Vetter  * drm_gem_handle_delete - deletes the given file-private handle
26589d61fc0SDaniel Vetter  * @filp: drm file-private structure to use for the handle look up
26689d61fc0SDaniel Vetter  * @handle: userspace handle to delete
26789d61fc0SDaniel Vetter  *
268df2e0900SDaniel Vetter  * Removes the GEM handle from the @filp lookup table which has been added with
269df2e0900SDaniel Vetter  * drm_gem_handle_create(). If this is the last handle also cleans up linked
270df2e0900SDaniel Vetter  * resources like GEM names.
271673a394bSEric Anholt  */
272ff72145bSDave Airlie int
273a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle)
274673a394bSEric Anholt {
275673a394bSEric Anholt 	struct drm_gem_object *obj;
276673a394bSEric Anholt 
277673a394bSEric Anholt 	spin_lock(&filp->table_lock);
278673a394bSEric Anholt 
279673a394bSEric Anholt 	/* Check if we currently have a reference on the object */
280f6cd7daeSChris Wilson 	obj = idr_replace(&filp->object_idr, NULL, handle);
281673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
282f6cd7daeSChris Wilson 	if (IS_ERR_OR_NULL(obj))
283673a394bSEric Anholt 		return -EINVAL;
284673a394bSEric Anholt 
285f6cd7daeSChris Wilson 	/* Release driver's reference and decrement refcount. */
286f6cd7daeSChris Wilson 	drm_gem_object_release_handle(handle, obj, filp);
287f6cd7daeSChris Wilson 
288f6cd7daeSChris Wilson 	/* And finally make the handle available for future allocations. */
289f6cd7daeSChris Wilson 	spin_lock(&filp->table_lock);
290673a394bSEric Anholt 	idr_remove(&filp->object_idr, handle);
291673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
292673a394bSEric Anholt 
293673a394bSEric Anholt 	return 0;
294673a394bSEric Anholt }
295ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete);
296673a394bSEric Anholt 
297673a394bSEric Anholt /**
298abd4e745SRob Herring  * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
299db611527SNoralf Trønnes  * @file: drm file-private structure containing the gem object
300db611527SNoralf Trønnes  * @dev: corresponding drm_device
301db611527SNoralf Trønnes  * @handle: gem object handle
302db611527SNoralf Trønnes  * @offset: return location for the fake mmap offset
303db611527SNoralf Trønnes  *
304db611527SNoralf Trønnes  * This implements the &drm_driver.dumb_map_offset kms driver callback for
305db611527SNoralf Trønnes  * drivers which use gem to manage their backing storage.
306db611527SNoralf Trønnes  *
307db611527SNoralf Trønnes  * Returns:
308db611527SNoralf Trønnes  * 0 on success or a negative error code on failure.
309db611527SNoralf Trønnes  */
310abd4e745SRob Herring int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
311db611527SNoralf Trønnes 			    u32 handle, u64 *offset)
312db611527SNoralf Trønnes {
313db611527SNoralf Trønnes 	struct drm_gem_object *obj;
314db611527SNoralf Trønnes 	int ret;
315db611527SNoralf Trønnes 
316db611527SNoralf Trønnes 	obj = drm_gem_object_lookup(file, handle);
317db611527SNoralf Trønnes 	if (!obj)
318db611527SNoralf Trønnes 		return -ENOENT;
319db611527SNoralf Trønnes 
32090378e58SNoralf Trønnes 	/* Don't allow imported objects to be mapped */
32190378e58SNoralf Trønnes 	if (obj->import_attach) {
32290378e58SNoralf Trønnes 		ret = -EINVAL;
32390378e58SNoralf Trønnes 		goto out;
32490378e58SNoralf Trønnes 	}
32590378e58SNoralf Trønnes 
326db611527SNoralf Trønnes 	ret = drm_gem_create_mmap_offset(obj);
327db611527SNoralf Trønnes 	if (ret)
328db611527SNoralf Trønnes 		goto out;
329db611527SNoralf Trønnes 
330db611527SNoralf Trønnes 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
331db611527SNoralf Trønnes out:
332be6ee102SEmil Velikov 	drm_gem_object_put(obj);
333db611527SNoralf Trønnes 
334db611527SNoralf Trønnes 	return ret;
335db611527SNoralf Trønnes }
336abd4e745SRob Herring EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
337db611527SNoralf Trønnes 
33843387b37SDaniel Vetter int drm_gem_dumb_destroy(struct drm_file *file,
33943387b37SDaniel Vetter 			 struct drm_device *dev,
34047f10854SLaurent Pinchart 			 u32 handle)
34143387b37SDaniel Vetter {
34243387b37SDaniel Vetter 	return drm_gem_handle_delete(file, handle);
34343387b37SDaniel Vetter }
34443387b37SDaniel Vetter 
34543387b37SDaniel Vetter /**
34620228c44SDaniel Vetter  * drm_gem_handle_create_tail - internal functions to create a handle
34789d61fc0SDaniel Vetter  * @file_priv: drm file-private structure to register the handle for
34889d61fc0SDaniel Vetter  * @obj: object to register
3498bf8180fSThierry Reding  * @handlep: pointer to return the created handle to the caller
35020228c44SDaniel Vetter  *
351940eba2dSDaniel Vetter  * This expects the &drm_device.object_name_lock to be held already and will
352940eba2dSDaniel Vetter  * drop it before returning. Used to avoid races in establishing new handles
353940eba2dSDaniel Vetter  * when importing an object from either an flink name or a dma-buf.
354df2e0900SDaniel Vetter  *
355df2e0900SDaniel Vetter  * Handles must be release again through drm_gem_handle_delete(). This is done
356df2e0900SDaniel Vetter  * when userspace closes @file_priv for all attached handles, or through the
357df2e0900SDaniel Vetter  * GEM_CLOSE ioctl for individual handles.
358673a394bSEric Anholt  */
359673a394bSEric Anholt int
36020228c44SDaniel Vetter drm_gem_handle_create_tail(struct drm_file *file_priv,
361673a394bSEric Anholt 			   struct drm_gem_object *obj,
362a1a2d1d3SPekka Paalanen 			   u32 *handlep)
363673a394bSEric Anholt {
364304eda32SBen Skeggs 	struct drm_device *dev = obj->dev;
3659649399eSChris Wilson 	u32 handle;
366673a394bSEric Anholt 	int ret;
367673a394bSEric Anholt 
36820228c44SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->object_name_lock));
36998a8883aSChris Wilson 	if (obj->handle_count++ == 0)
370e6b62714SThierry Reding 		drm_gem_object_get(obj);
37120228c44SDaniel Vetter 
372673a394bSEric Anholt 	/*
3732e928815STejun Heo 	 * Get the user-visible handle using idr.  Preload and perform
3742e928815STejun Heo 	 * allocation under our spinlock.
375673a394bSEric Anholt 	 */
3762e928815STejun Heo 	idr_preload(GFP_KERNEL);
377673a394bSEric Anholt 	spin_lock(&file_priv->table_lock);
3782e928815STejun Heo 
3792e928815STejun Heo 	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
38098a8883aSChris Wilson 
381673a394bSEric Anholt 	spin_unlock(&file_priv->table_lock);
3822e928815STejun Heo 	idr_preload_end();
38398a8883aSChris Wilson 
384cd4f013fSDaniel Vetter 	mutex_unlock(&dev->object_name_lock);
3856984128dSChris Wilson 	if (ret < 0)
3866984128dSChris Wilson 		goto err_unref;
3876984128dSChris Wilson 
3889649399eSChris Wilson 	handle = ret;
389673a394bSEric Anholt 
390d9a1f0b4SDavid Herrmann 	ret = drm_vma_node_allow(&obj->vma_node, file_priv);
3916984128dSChris Wilson 	if (ret)
3926984128dSChris Wilson 		goto err_remove;
393304eda32SBen Skeggs 
394d693def4SThomas Zimmermann 	if (obj->funcs->open) {
395b39b5394SNoralf Trønnes 		ret = obj->funcs->open(obj, file_priv);
396b39b5394SNoralf Trønnes 		if (ret)
397b39b5394SNoralf Trønnes 			goto err_revoke;
398304eda32SBen Skeggs 	}
399304eda32SBen Skeggs 
4009649399eSChris Wilson 	*handlep = handle;
401673a394bSEric Anholt 	return 0;
4026984128dSChris Wilson 
4036984128dSChris Wilson err_revoke:
404d9a1f0b4SDavid Herrmann 	drm_vma_node_revoke(&obj->vma_node, file_priv);
4056984128dSChris Wilson err_remove:
4066984128dSChris Wilson 	spin_lock(&file_priv->table_lock);
4079649399eSChris Wilson 	idr_remove(&file_priv->object_idr, handle);
4086984128dSChris Wilson 	spin_unlock(&file_priv->table_lock);
4096984128dSChris Wilson err_unref:
410e6b62714SThierry Reding 	drm_gem_object_handle_put_unlocked(obj);
4116984128dSChris Wilson 	return ret;
412673a394bSEric Anholt }
41320228c44SDaniel Vetter 
41420228c44SDaniel Vetter /**
4158bf8180fSThierry Reding  * drm_gem_handle_create - create a gem handle for an object
41689d61fc0SDaniel Vetter  * @file_priv: drm file-private structure to register the handle for
41789d61fc0SDaniel Vetter  * @obj: object to register
41882c0ef94SIgor Matheus Andrade Torrente  * @handlep: pointer to return the created handle to the caller
41989d61fc0SDaniel Vetter  *
42039031176SDaniel Vetter  * Create a handle for this object. This adds a handle reference to the object,
42139031176SDaniel Vetter  * which includes a regular reference count. Callers will likely want to
42239031176SDaniel Vetter  * dereference the object afterwards.
42339031176SDaniel Vetter  *
42439031176SDaniel Vetter  * Since this publishes @obj to userspace it must be fully set up by this point,
42539031176SDaniel Vetter  * drivers must call this last in their buffer object creation callbacks.
42620228c44SDaniel Vetter  */
4278bf8180fSThierry Reding int drm_gem_handle_create(struct drm_file *file_priv,
42820228c44SDaniel Vetter 			  struct drm_gem_object *obj,
42920228c44SDaniel Vetter 			  u32 *handlep)
43020228c44SDaniel Vetter {
43120228c44SDaniel Vetter 	mutex_lock(&obj->dev->object_name_lock);
43220228c44SDaniel Vetter 
43320228c44SDaniel Vetter 	return drm_gem_handle_create_tail(file_priv, obj, handlep);
43420228c44SDaniel Vetter }
435673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create);
436673a394bSEric Anholt 
43775ef8b3bSRob Clark 
43875ef8b3bSRob Clark /**
43975ef8b3bSRob Clark  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
44075ef8b3bSRob Clark  * @obj: obj in question
44175ef8b3bSRob Clark  *
44275ef8b3bSRob Clark  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
443f74418a4SDaniel Vetter  *
444f74418a4SDaniel Vetter  * Note that drm_gem_object_release() already calls this function, so drivers
445f74418a4SDaniel Vetter  * don't have to take care of releasing the mmap offset themselves when freeing
446f74418a4SDaniel Vetter  * the GEM object.
44775ef8b3bSRob Clark  */
44875ef8b3bSRob Clark void
44975ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj)
45075ef8b3bSRob Clark {
45175ef8b3bSRob Clark 	struct drm_device *dev = obj->dev;
45275ef8b3bSRob Clark 
453b04a5906SDaniel Vetter 	drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
45475ef8b3bSRob Clark }
45575ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset);
45675ef8b3bSRob Clark 
45775ef8b3bSRob Clark /**
458367bbd49SRob Clark  * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
459367bbd49SRob Clark  * @obj: obj in question
460367bbd49SRob Clark  * @size: the virtual size
461367bbd49SRob Clark  *
462367bbd49SRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
463367bbd49SRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
464367bbd49SRob Clark  * up the object based on the offset and sets up the various memory mapping
465367bbd49SRob Clark  * structures.
466367bbd49SRob Clark  *
467367bbd49SRob Clark  * This routine allocates and attaches a fake offset for @obj, in cases where
468940eba2dSDaniel Vetter  * the virtual size differs from the physical size (ie. &drm_gem_object.size).
469940eba2dSDaniel Vetter  * Otherwise just use drm_gem_create_mmap_offset().
470f74418a4SDaniel Vetter  *
471f74418a4SDaniel Vetter  * This function is idempotent and handles an already allocated mmap offset
472f74418a4SDaniel Vetter  * transparently. Drivers do not need to check for this case.
473367bbd49SRob Clark  */
474367bbd49SRob Clark int
475367bbd49SRob Clark drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
476367bbd49SRob Clark {
477367bbd49SRob Clark 	struct drm_device *dev = obj->dev;
478367bbd49SRob Clark 
479b04a5906SDaniel Vetter 	return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
480367bbd49SRob Clark 				  size / PAGE_SIZE);
481367bbd49SRob Clark }
482367bbd49SRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
483367bbd49SRob Clark 
484367bbd49SRob Clark /**
48575ef8b3bSRob Clark  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
48675ef8b3bSRob Clark  * @obj: obj in question
48775ef8b3bSRob Clark  *
48875ef8b3bSRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
48975ef8b3bSRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
49075ef8b3bSRob Clark  * up the object based on the offset and sets up the various memory mapping
49175ef8b3bSRob Clark  * structures.
49275ef8b3bSRob Clark  *
49375ef8b3bSRob Clark  * This routine allocates and attaches a fake offset for @obj.
494f74418a4SDaniel Vetter  *
495f74418a4SDaniel Vetter  * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
496f74418a4SDaniel Vetter  * the fake offset again.
49775ef8b3bSRob Clark  */
498367bbd49SRob Clark int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
49975ef8b3bSRob Clark {
500367bbd49SRob Clark 	return drm_gem_create_mmap_offset_size(obj, obj->size);
50175ef8b3bSRob Clark }
50275ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset);
50375ef8b3bSRob Clark 
504fb4b4927SKuo-Hsin Yang /*
505fb4b4927SKuo-Hsin Yang  * Move pages to appropriate lru and release the pagevec, decrementing the
506fb4b4927SKuo-Hsin Yang  * ref count of those pages.
507fb4b4927SKuo-Hsin Yang  */
508fb4b4927SKuo-Hsin Yang static void drm_gem_check_release_pagevec(struct pagevec *pvec)
509fb4b4927SKuo-Hsin Yang {
510fb4b4927SKuo-Hsin Yang 	check_move_unevictable_pages(pvec);
511fb4b4927SKuo-Hsin Yang 	__pagevec_release(pvec);
512fb4b4927SKuo-Hsin Yang 	cond_resched();
513fb4b4927SKuo-Hsin Yang }
514fb4b4927SKuo-Hsin Yang 
515bcc5c9d5SRob Clark /**
516bcc5c9d5SRob Clark  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
517bcc5c9d5SRob Clark  * from shmem
518bcc5c9d5SRob Clark  * @obj: obj in question
5190cdbe8acSDavid Herrmann  *
5200cdbe8acSDavid Herrmann  * This reads the page-array of the shmem-backing storage of the given gem
5210cdbe8acSDavid Herrmann  * object. An array of pages is returned. If a page is not allocated or
5220cdbe8acSDavid Herrmann  * swapped-out, this will allocate/swap-in the required pages. Note that the
5230cdbe8acSDavid Herrmann  * whole object is covered by the page-array and pinned in memory.
5240cdbe8acSDavid Herrmann  *
5250cdbe8acSDavid Herrmann  * Use drm_gem_put_pages() to release the array and unpin all pages.
5260cdbe8acSDavid Herrmann  *
5270cdbe8acSDavid Herrmann  * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
5280cdbe8acSDavid Herrmann  * If you require other GFP-masks, you have to do those allocations yourself.
5290cdbe8acSDavid Herrmann  *
5300cdbe8acSDavid Herrmann  * Note that you are not allowed to change gfp-zones during runtime. That is,
5310cdbe8acSDavid Herrmann  * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
5320cdbe8acSDavid Herrmann  * set during initialization. If you have special zone constraints, set them
5335b9fbfffSJordan Crouse  * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care
5340cdbe8acSDavid Herrmann  * to keep pages in the required zone during swap-in.
535e0b3d214SDaniel Vetter  *
536e0b3d214SDaniel Vetter  * This function is only valid on objects initialized with
537e0b3d214SDaniel Vetter  * drm_gem_object_init(), but not for those initialized with
538e0b3d214SDaniel Vetter  * drm_gem_private_object_init() only.
539bcc5c9d5SRob Clark  */
5400cdbe8acSDavid Herrmann struct page **drm_gem_get_pages(struct drm_gem_object *obj)
541bcc5c9d5SRob Clark {
542bcc5c9d5SRob Clark 	struct address_space *mapping;
543bcc5c9d5SRob Clark 	struct page *p, **pages;
544fb4b4927SKuo-Hsin Yang 	struct pagevec pvec;
545bcc5c9d5SRob Clark 	int i, npages;
546bcc5c9d5SRob Clark 
547e0b3d214SDaniel Vetter 
548e0b3d214SDaniel Vetter 	if (WARN_ON(!obj->filp))
549e0b3d214SDaniel Vetter 		return ERR_PTR(-EINVAL);
550e0b3d214SDaniel Vetter 
551bcc5c9d5SRob Clark 	/* This is the shared memory object that backs the GEM resource */
55293c76a3dSAl Viro 	mapping = obj->filp->f_mapping;
553bcc5c9d5SRob Clark 
554bcc5c9d5SRob Clark 	/* We already BUG_ON() for non-page-aligned sizes in
555bcc5c9d5SRob Clark 	 * drm_gem_object_init(), so we should never hit this unless
556bcc5c9d5SRob Clark 	 * driver author is doing something really wrong:
557bcc5c9d5SRob Clark 	 */
558bcc5c9d5SRob Clark 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
559bcc5c9d5SRob Clark 
560bcc5c9d5SRob Clark 	npages = obj->size >> PAGE_SHIFT;
561bcc5c9d5SRob Clark 
5622098105eSMichal Hocko 	pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
563bcc5c9d5SRob Clark 	if (pages == NULL)
564bcc5c9d5SRob Clark 		return ERR_PTR(-ENOMEM);
565bcc5c9d5SRob Clark 
566fb4b4927SKuo-Hsin Yang 	mapping_set_unevictable(mapping);
567fb4b4927SKuo-Hsin Yang 
568bcc5c9d5SRob Clark 	for (i = 0; i < npages; i++) {
5690cdbe8acSDavid Herrmann 		p = shmem_read_mapping_page(mapping, i);
570bcc5c9d5SRob Clark 		if (IS_ERR(p))
571bcc5c9d5SRob Clark 			goto fail;
572bcc5c9d5SRob Clark 		pages[i] = p;
573bcc5c9d5SRob Clark 
5742123000bSDavid Herrmann 		/* Make sure shmem keeps __GFP_DMA32 allocated pages in the
5752123000bSDavid Herrmann 		 * correct region during swapin. Note that this requires
5762123000bSDavid Herrmann 		 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
5772123000bSDavid Herrmann 		 * so shmem can relocate pages during swapin if required.
578bcc5c9d5SRob Clark 		 */
579c62d2555SMichal Hocko 		BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
580bcc5c9d5SRob Clark 				(page_to_pfn(p) >= 0x00100000UL));
581bcc5c9d5SRob Clark 	}
582bcc5c9d5SRob Clark 
583bcc5c9d5SRob Clark 	return pages;
584bcc5c9d5SRob Clark 
585bcc5c9d5SRob Clark fail:
586fb4b4927SKuo-Hsin Yang 	mapping_clear_unevictable(mapping);
587fb4b4927SKuo-Hsin Yang 	pagevec_init(&pvec);
588fb4b4927SKuo-Hsin Yang 	while (i--) {
589fb4b4927SKuo-Hsin Yang 		if (!pagevec_add(&pvec, pages[i]))
590fb4b4927SKuo-Hsin Yang 			drm_gem_check_release_pagevec(&pvec);
591fb4b4927SKuo-Hsin Yang 	}
592fb4b4927SKuo-Hsin Yang 	if (pagevec_count(&pvec))
593fb4b4927SKuo-Hsin Yang 		drm_gem_check_release_pagevec(&pvec);
594bcc5c9d5SRob Clark 
5952098105eSMichal Hocko 	kvfree(pages);
596bcc5c9d5SRob Clark 	return ERR_CAST(p);
597bcc5c9d5SRob Clark }
598bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_get_pages);
599bcc5c9d5SRob Clark 
600bcc5c9d5SRob Clark /**
601bcc5c9d5SRob Clark  * drm_gem_put_pages - helper to free backing pages for a GEM object
602bcc5c9d5SRob Clark  * @obj: obj in question
603bcc5c9d5SRob Clark  * @pages: pages to free
604bcc5c9d5SRob Clark  * @dirty: if true, pages will be marked as dirty
605bcc5c9d5SRob Clark  * @accessed: if true, the pages will be marked as accessed
606bcc5c9d5SRob Clark  */
607bcc5c9d5SRob Clark void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
608bcc5c9d5SRob Clark 		bool dirty, bool accessed)
609bcc5c9d5SRob Clark {
610bcc5c9d5SRob Clark 	int i, npages;
611fb4b4927SKuo-Hsin Yang 	struct address_space *mapping;
612fb4b4927SKuo-Hsin Yang 	struct pagevec pvec;
613fb4b4927SKuo-Hsin Yang 
614fb4b4927SKuo-Hsin Yang 	mapping = file_inode(obj->filp)->i_mapping;
615fb4b4927SKuo-Hsin Yang 	mapping_clear_unevictable(mapping);
616bcc5c9d5SRob Clark 
617bcc5c9d5SRob Clark 	/* We already BUG_ON() for non-page-aligned sizes in
618bcc5c9d5SRob Clark 	 * drm_gem_object_init(), so we should never hit this unless
619bcc5c9d5SRob Clark 	 * driver author is doing something really wrong:
620bcc5c9d5SRob Clark 	 */
621bcc5c9d5SRob Clark 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
622bcc5c9d5SRob Clark 
623bcc5c9d5SRob Clark 	npages = obj->size >> PAGE_SHIFT;
624bcc5c9d5SRob Clark 
625fb4b4927SKuo-Hsin Yang 	pagevec_init(&pvec);
626bcc5c9d5SRob Clark 	for (i = 0; i < npages; i++) {
627930a4024SRob Herring 		if (!pages[i])
628930a4024SRob Herring 			continue;
629930a4024SRob Herring 
630bcc5c9d5SRob Clark 		if (dirty)
631bcc5c9d5SRob Clark 			set_page_dirty(pages[i]);
632bcc5c9d5SRob Clark 
633bcc5c9d5SRob Clark 		if (accessed)
634bcc5c9d5SRob Clark 			mark_page_accessed(pages[i]);
635bcc5c9d5SRob Clark 
636bcc5c9d5SRob Clark 		/* Undo the reference we took when populating the table */
637fb4b4927SKuo-Hsin Yang 		if (!pagevec_add(&pvec, pages[i]))
638fb4b4927SKuo-Hsin Yang 			drm_gem_check_release_pagevec(&pvec);
639bcc5c9d5SRob Clark 	}
640fb4b4927SKuo-Hsin Yang 	if (pagevec_count(&pvec))
641fb4b4927SKuo-Hsin Yang 		drm_gem_check_release_pagevec(&pvec);
642bcc5c9d5SRob Clark 
6432098105eSMichal Hocko 	kvfree(pages);
644bcc5c9d5SRob Clark }
645bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_put_pages);
646bcc5c9d5SRob Clark 
647c117aa4dSRob Herring static int objects_lookup(struct drm_file *filp, u32 *handle, int count,
648c117aa4dSRob Herring 			  struct drm_gem_object **objs)
649c117aa4dSRob Herring {
650c117aa4dSRob Herring 	int i, ret = 0;
651c117aa4dSRob Herring 	struct drm_gem_object *obj;
652c117aa4dSRob Herring 
653c117aa4dSRob Herring 	spin_lock(&filp->table_lock);
654c117aa4dSRob Herring 
655c117aa4dSRob Herring 	for (i = 0; i < count; i++) {
656c117aa4dSRob Herring 		/* Check if we currently have a reference on the object */
657c117aa4dSRob Herring 		obj = idr_find(&filp->object_idr, handle[i]);
658c117aa4dSRob Herring 		if (!obj) {
659c117aa4dSRob Herring 			ret = -ENOENT;
660c117aa4dSRob Herring 			break;
661c117aa4dSRob Herring 		}
662c117aa4dSRob Herring 		drm_gem_object_get(obj);
663c117aa4dSRob Herring 		objs[i] = obj;
664c117aa4dSRob Herring 	}
665c117aa4dSRob Herring 	spin_unlock(&filp->table_lock);
666c117aa4dSRob Herring 
667c117aa4dSRob Herring 	return ret;
668c117aa4dSRob Herring }
669c117aa4dSRob Herring 
670c117aa4dSRob Herring /**
671c117aa4dSRob Herring  * drm_gem_objects_lookup - look up GEM objects from an array of handles
672c117aa4dSRob Herring  * @filp: DRM file private date
673c117aa4dSRob Herring  * @bo_handles: user pointer to array of userspace handle
674c117aa4dSRob Herring  * @count: size of handle array
675c117aa4dSRob Herring  * @objs_out: returned pointer to array of drm_gem_object pointers
676c117aa4dSRob Herring  *
677c117aa4dSRob Herring  * Takes an array of userspace handles and returns a newly allocated array of
678c117aa4dSRob Herring  * GEM objects.
679c117aa4dSRob Herring  *
680c117aa4dSRob Herring  * For a single handle lookup, use drm_gem_object_lookup().
681c117aa4dSRob Herring  *
682c117aa4dSRob Herring  * Returns:
683c117aa4dSRob Herring  *
684c117aa4dSRob Herring  * @objs filled in with GEM object pointers. Returned GEM objects need to be
685be6ee102SEmil Velikov  * released with drm_gem_object_put(). -ENOENT is returned on a lookup
686c117aa4dSRob Herring  * failure. 0 is returned on success.
687c117aa4dSRob Herring  *
688c117aa4dSRob Herring  */
689c117aa4dSRob Herring int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
690c117aa4dSRob Herring 			   int count, struct drm_gem_object ***objs_out)
691c117aa4dSRob Herring {
692c117aa4dSRob Herring 	int ret;
693c117aa4dSRob Herring 	u32 *handles;
694c117aa4dSRob Herring 	struct drm_gem_object **objs;
695c117aa4dSRob Herring 
696c117aa4dSRob Herring 	if (!count)
697c117aa4dSRob Herring 		return 0;
698c117aa4dSRob Herring 
699c117aa4dSRob Herring 	objs = kvmalloc_array(count, sizeof(struct drm_gem_object *),
700c117aa4dSRob Herring 			     GFP_KERNEL | __GFP_ZERO);
701c117aa4dSRob Herring 	if (!objs)
702c117aa4dSRob Herring 		return -ENOMEM;
703c117aa4dSRob Herring 
704ec0bb482SDan Carpenter 	*objs_out = objs;
705ec0bb482SDan Carpenter 
706c117aa4dSRob Herring 	handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
707c117aa4dSRob Herring 	if (!handles) {
708c117aa4dSRob Herring 		ret = -ENOMEM;
709c117aa4dSRob Herring 		goto out;
710c117aa4dSRob Herring 	}
711c117aa4dSRob Herring 
712c117aa4dSRob Herring 	if (copy_from_user(handles, bo_handles, count * sizeof(u32))) {
713c117aa4dSRob Herring 		ret = -EFAULT;
714c117aa4dSRob Herring 		DRM_DEBUG("Failed to copy in GEM handles\n");
715c117aa4dSRob Herring 		goto out;
716c117aa4dSRob Herring 	}
717c117aa4dSRob Herring 
718c117aa4dSRob Herring 	ret = objects_lookup(filp, handles, count, objs);
719c117aa4dSRob Herring out:
720c117aa4dSRob Herring 	kvfree(handles);
721c117aa4dSRob Herring 	return ret;
722c117aa4dSRob Herring 
723c117aa4dSRob Herring }
724c117aa4dSRob Herring EXPORT_SYMBOL(drm_gem_objects_lookup);
725c117aa4dSRob Herring 
726df2e0900SDaniel Vetter /**
7271e55a53aSMatt Roper  * drm_gem_object_lookup - look up a GEM object from its handle
728df2e0900SDaniel Vetter  * @filp: DRM file private date
729df2e0900SDaniel Vetter  * @handle: userspace handle
730df2e0900SDaniel Vetter  *
731df2e0900SDaniel Vetter  * Returns:
732df2e0900SDaniel Vetter  *
733df2e0900SDaniel Vetter  * A reference to the object named by the handle if such exists on @filp, NULL
734df2e0900SDaniel Vetter  * otherwise.
735c117aa4dSRob Herring  *
736c117aa4dSRob Herring  * If looking up an array of handles, use drm_gem_objects_lookup().
737df2e0900SDaniel Vetter  */
738673a394bSEric Anholt struct drm_gem_object *
739a8ad0bd8SChris Wilson drm_gem_object_lookup(struct drm_file *filp, u32 handle)
740673a394bSEric Anholt {
741c117aa4dSRob Herring 	struct drm_gem_object *obj = NULL;
742673a394bSEric Anholt 
743c117aa4dSRob Herring 	objects_lookup(filp, &handle, 1, &obj);
744673a394bSEric Anholt 	return obj;
745673a394bSEric Anholt }
746673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup);
747673a394bSEric Anholt 
748673a394bSEric Anholt /**
74952791eeeSChristian König  * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects
7501ba62714SRob Herring  * shared and/or exclusive fences.
7511ba62714SRob Herring  * @filep: DRM file private date
7521ba62714SRob Herring  * @handle: userspace handle
7531ba62714SRob Herring  * @wait_all: if true, wait on all fences, else wait on just exclusive fence
7541ba62714SRob Herring  * @timeout: timeout value in jiffies or zero to return immediately
7551ba62714SRob Herring  *
7561ba62714SRob Herring  * Returns:
7571ba62714SRob Herring  *
7581ba62714SRob Herring  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
7591ba62714SRob Herring  * greater than 0 on success.
7601ba62714SRob Herring  */
76152791eeeSChristian König long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
7621ba62714SRob Herring 				    bool wait_all, unsigned long timeout)
7631ba62714SRob Herring {
7641ba62714SRob Herring 	long ret;
7651ba62714SRob Herring 	struct drm_gem_object *obj;
7661ba62714SRob Herring 
7671ba62714SRob Herring 	obj = drm_gem_object_lookup(filep, handle);
7681ba62714SRob Herring 	if (!obj) {
7691ba62714SRob Herring 		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
7701ba62714SRob Herring 		return -EINVAL;
7711ba62714SRob Herring 	}
7721ba62714SRob Herring 
773d3fae3b3SChristian König 	ret = dma_resv_wait_timeout(obj->resv, wait_all, true, timeout);
7741ba62714SRob Herring 	if (ret == 0)
7751ba62714SRob Herring 		ret = -ETIME;
7761ba62714SRob Herring 	else if (ret > 0)
7771ba62714SRob Herring 		ret = 0;
7781ba62714SRob Herring 
779be6ee102SEmil Velikov 	drm_gem_object_put(obj);
7801ba62714SRob Herring 
7811ba62714SRob Herring 	return ret;
7821ba62714SRob Herring }
78352791eeeSChristian König EXPORT_SYMBOL(drm_gem_dma_resv_wait);
7841ba62714SRob Herring 
7851ba62714SRob Herring /**
78689d61fc0SDaniel Vetter  * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
78789d61fc0SDaniel Vetter  * @dev: drm_device
78889d61fc0SDaniel Vetter  * @data: ioctl data
78989d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
79089d61fc0SDaniel Vetter  *
791673a394bSEric Anholt  * Releases the handle to an mm object.
792673a394bSEric Anholt  */
793673a394bSEric Anholt int
794673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data,
795673a394bSEric Anholt 		    struct drm_file *file_priv)
796673a394bSEric Anholt {
797673a394bSEric Anholt 	struct drm_gem_close *args = data;
798673a394bSEric Anholt 	int ret;
799673a394bSEric Anholt 
8001bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
80169fdf420SChris Wilson 		return -EOPNOTSUPP;
802673a394bSEric Anholt 
803673a394bSEric Anholt 	ret = drm_gem_handle_delete(file_priv, args->handle);
804673a394bSEric Anholt 
805673a394bSEric Anholt 	return ret;
806673a394bSEric Anholt }
807673a394bSEric Anholt 
808673a394bSEric Anholt /**
80989d61fc0SDaniel Vetter  * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
81089d61fc0SDaniel Vetter  * @dev: drm_device
81189d61fc0SDaniel Vetter  * @data: ioctl data
81289d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
81389d61fc0SDaniel Vetter  *
814673a394bSEric Anholt  * Create a global name for an object, returning the name.
815673a394bSEric Anholt  *
816673a394bSEric Anholt  * Note that the name does not hold a reference; when the object
817673a394bSEric Anholt  * is freed, the name goes away.
818673a394bSEric Anholt  */
819673a394bSEric Anholt int
820673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data,
821673a394bSEric Anholt 		    struct drm_file *file_priv)
822673a394bSEric Anholt {
823673a394bSEric Anholt 	struct drm_gem_flink *args = data;
824673a394bSEric Anholt 	struct drm_gem_object *obj;
825673a394bSEric Anholt 	int ret;
826673a394bSEric Anholt 
8271bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
82869fdf420SChris Wilson 		return -EOPNOTSUPP;
829673a394bSEric Anholt 
830a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, args->handle);
831673a394bSEric Anholt 	if (obj == NULL)
832bf79cb91SChris Wilson 		return -ENOENT;
833673a394bSEric Anholt 
834cd4f013fSDaniel Vetter 	mutex_lock(&dev->object_name_lock);
835a8e11d1cSDaniel Vetter 	/* prevent races with concurrent gem_close. */
836a8e11d1cSDaniel Vetter 	if (obj->handle_count == 0) {
837a8e11d1cSDaniel Vetter 		ret = -ENOENT;
838a8e11d1cSDaniel Vetter 		goto err;
839a8e11d1cSDaniel Vetter 	}
840a8e11d1cSDaniel Vetter 
8418d59bae5SChris Wilson 	if (!obj->name) {
8420f646425SChris Wilson 		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
8432e928815STejun Heo 		if (ret < 0)
8443e49c4f4SChris Wilson 			goto err;
8452e07fb22SYoungJun Cho 
8462e07fb22SYoungJun Cho 		obj->name = ret;
8478d59bae5SChris Wilson 	}
8483e49c4f4SChris Wilson 
8492e07fb22SYoungJun Cho 	args->name = (uint64_t) obj->name;
8502e07fb22SYoungJun Cho 	ret = 0;
8512e07fb22SYoungJun Cho 
8523e49c4f4SChris Wilson err:
853cd4f013fSDaniel Vetter 	mutex_unlock(&dev->object_name_lock);
854be6ee102SEmil Velikov 	drm_gem_object_put(obj);
8553e49c4f4SChris Wilson 	return ret;
856673a394bSEric Anholt }
857673a394bSEric Anholt 
858673a394bSEric Anholt /**
859e9d2871fSMauro Carvalho Chehab  * drm_gem_open_ioctl - implementation of the GEM_OPEN ioctl
86089d61fc0SDaniel Vetter  * @dev: drm_device
86189d61fc0SDaniel Vetter  * @data: ioctl data
86289d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
86389d61fc0SDaniel Vetter  *
864673a394bSEric Anholt  * Open an object using the global name, returning a handle and the size.
865a9e10b16SSteve Cohen  *
866a9e10b16SSteve Cohen  * This handle (of course) holds a reference to the object, so the object
867a9e10b16SSteve Cohen  * will not go away until the handle is deleted.
868673a394bSEric Anholt  */
869673a394bSEric Anholt int
870673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data,
871673a394bSEric Anholt 		   struct drm_file *file_priv)
872673a394bSEric Anholt {
873673a394bSEric Anholt 	struct drm_gem_open *args = data;
874673a394bSEric Anholt 	struct drm_gem_object *obj;
875673a394bSEric Anholt 	int ret;
876a1a2d1d3SPekka Paalanen 	u32 handle;
877673a394bSEric Anholt 
8781bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
87969fdf420SChris Wilson 		return -EOPNOTSUPP;
880673a394bSEric Anholt 
881cd4f013fSDaniel Vetter 	mutex_lock(&dev->object_name_lock);
882673a394bSEric Anholt 	obj = idr_find(&dev->object_name_idr, (int) args->name);
88320228c44SDaniel Vetter 	if (obj) {
884e6b62714SThierry Reding 		drm_gem_object_get(obj);
88520228c44SDaniel Vetter 	} else {
886cd4f013fSDaniel Vetter 		mutex_unlock(&dev->object_name_lock);
887673a394bSEric Anholt 		return -ENOENT;
88820228c44SDaniel Vetter 	}
889673a394bSEric Anholt 
89020228c44SDaniel Vetter 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
89120228c44SDaniel Vetter 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
892673a394bSEric Anholt 	if (ret)
8938490d6a7SSteve Cohen 		goto err;
894673a394bSEric Anholt 
895673a394bSEric Anholt 	args->handle = handle;
896673a394bSEric Anholt 	args->size = obj->size;
897673a394bSEric Anholt 
8988490d6a7SSteve Cohen err:
899c44264f9SDave Airlie 	drm_gem_object_put(obj);
9008490d6a7SSteve Cohen 	return ret;
901673a394bSEric Anholt }
902673a394bSEric Anholt 
903673a394bSEric Anholt /**
9040ae865efSCai Huoqing  * drm_gem_open - initializes GEM file-private structures at devnode open time
90589d61fc0SDaniel Vetter  * @dev: drm_device which is being opened by userspace
90689d61fc0SDaniel Vetter  * @file_private: drm file-private structure to set up
90789d61fc0SDaniel Vetter  *
908673a394bSEric Anholt  * Called at device open time, sets up the structure for handling refcounting
909673a394bSEric Anholt  * of mm objects.
910673a394bSEric Anholt  */
911673a394bSEric Anholt void
912673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
913673a394bSEric Anholt {
914e86584c5SChris Wilson 	idr_init_base(&file_private->object_idr, 1);
915673a394bSEric Anholt 	spin_lock_init(&file_private->table_lock);
916673a394bSEric Anholt }
917673a394bSEric Anholt 
918673a394bSEric Anholt /**
91989d61fc0SDaniel Vetter  * drm_gem_release - release file-private GEM resources
92089d61fc0SDaniel Vetter  * @dev: drm_device which is being closed by userspace
92189d61fc0SDaniel Vetter  * @file_private: drm file-private structure to clean up
92289d61fc0SDaniel Vetter  *
923673a394bSEric Anholt  * Called at close time when the filp is going away.
924673a394bSEric Anholt  *
925673a394bSEric Anholt  * Releases any remaining references on objects by this filp.
926673a394bSEric Anholt  */
927673a394bSEric Anholt void
928673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
929673a394bSEric Anholt {
930673a394bSEric Anholt 	idr_for_each(&file_private->object_idr,
931304eda32SBen Skeggs 		     &drm_gem_object_release_handle, file_private);
932673a394bSEric Anholt 	idr_destroy(&file_private->object_idr);
933673a394bSEric Anholt }
934673a394bSEric Anholt 
935f74418a4SDaniel Vetter /**
936f74418a4SDaniel Vetter  * drm_gem_object_release - release GEM buffer object resources
937f74418a4SDaniel Vetter  * @obj: GEM buffer object
938f74418a4SDaniel Vetter  *
9390ae865efSCai Huoqing  * This releases any structures and resources used by @obj and is the inverse of
940f74418a4SDaniel Vetter  * drm_gem_object_init().
941f74418a4SDaniel Vetter  */
942fd632aa3SDaniel Vetter void
943fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj)
944c3ae90c0SLuca Barbieri {
945319c933cSDaniel Vetter 	WARN_ON(obj->dma_buf);
946319c933cSDaniel Vetter 
94762cb7011SAlan Cox 	if (obj->filp)
948c3ae90c0SLuca Barbieri 		fput(obj->filp);
94977472347SDavid Herrmann 
95052791eeeSChristian König 	dma_resv_fini(&obj->_resv);
95177472347SDavid Herrmann 	drm_gem_free_mmap_offset(obj);
952c3ae90c0SLuca Barbieri }
953fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release);
954c3ae90c0SLuca Barbieri 
955673a394bSEric Anholt /**
95689d61fc0SDaniel Vetter  * drm_gem_object_free - free a GEM object
95789d61fc0SDaniel Vetter  * @kref: kref of the object to free
95889d61fc0SDaniel Vetter  *
959673a394bSEric Anholt  * Called after the last reference to the object has been lost.
960673a394bSEric Anholt  *
961673a394bSEric Anholt  * Frees the object
962673a394bSEric Anholt  */
963673a394bSEric Anholt void
964673a394bSEric Anholt drm_gem_object_free(struct kref *kref)
965673a394bSEric Anholt {
9666ff774bdSDaniel Vetter 	struct drm_gem_object *obj =
9676ff774bdSDaniel Vetter 		container_of(kref, struct drm_gem_object, refcount);
968673a394bSEric Anholt 
969d693def4SThomas Zimmermann 	if (WARN_ON(!obj->funcs->free))
970d693def4SThomas Zimmermann 		return;
971d693def4SThomas Zimmermann 
972b39b5394SNoralf Trønnes 	obj->funcs->free(obj);
9736d3e7fddSDaniel Vetter }
974673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free);
975673a394bSEric Anholt 
976df2e0900SDaniel Vetter /**
977df2e0900SDaniel Vetter  * drm_gem_vm_open - vma->ops->open implementation for GEM
978df2e0900SDaniel Vetter  * @vma: VM area structure
979df2e0900SDaniel Vetter  *
980df2e0900SDaniel Vetter  * This function implements the #vm_operations_struct open() callback for GEM
981df2e0900SDaniel Vetter  * drivers. This must be used together with drm_gem_vm_close().
982df2e0900SDaniel Vetter  */
983ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma)
984ab00b3e5SJesse Barnes {
985ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
986ab00b3e5SJesse Barnes 
987e6b62714SThierry Reding 	drm_gem_object_get(obj);
988ab00b3e5SJesse Barnes }
989ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open);
990ab00b3e5SJesse Barnes 
991df2e0900SDaniel Vetter /**
992df2e0900SDaniel Vetter  * drm_gem_vm_close - vma->ops->close implementation for GEM
993df2e0900SDaniel Vetter  * @vma: VM area structure
994df2e0900SDaniel Vetter  *
995df2e0900SDaniel Vetter  * This function implements the #vm_operations_struct close() callback for GEM
996df2e0900SDaniel Vetter  * drivers. This must be used together with drm_gem_vm_open().
997df2e0900SDaniel Vetter  */
998ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma)
999ab00b3e5SJesse Barnes {
1000ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
1001ab00b3e5SJesse Barnes 
1002be6ee102SEmil Velikov 	drm_gem_object_put(obj);
1003ab00b3e5SJesse Barnes }
1004ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close);
1005ab00b3e5SJesse Barnes 
10061c5aafa6SLaurent Pinchart /**
10071c5aafa6SLaurent Pinchart  * drm_gem_mmap_obj - memory map a GEM object
10081c5aafa6SLaurent Pinchart  * @obj: the GEM object to map
10091c5aafa6SLaurent Pinchart  * @obj_size: the object size to be mapped, in bytes
10101c5aafa6SLaurent Pinchart  * @vma: VMA for the area to be mapped
10111c5aafa6SLaurent Pinchart  *
1012d693def4SThomas Zimmermann  * Set up the VMA to prepare mapping of the GEM object using the GEM object's
1013d693def4SThomas Zimmermann  * vm_ops. Depending on their requirements, GEM objects can either
1014d693def4SThomas Zimmermann  * provide a fault handler in their vm_ops (in which case any accesses to
10151c5aafa6SLaurent Pinchart  * the object will be trapped, to perform migration, GTT binding, surface
10161c5aafa6SLaurent Pinchart  * register allocation, or performance monitoring), or mmap the buffer memory
10171c5aafa6SLaurent Pinchart  * synchronously after calling drm_gem_mmap_obj.
10181c5aafa6SLaurent Pinchart  *
10191c5aafa6SLaurent Pinchart  * This function is mainly intended to implement the DMABUF mmap operation, when
10201c5aafa6SLaurent Pinchart  * the GEM object is not looked up based on its fake offset. To implement the
10211c5aafa6SLaurent Pinchart  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
10221c5aafa6SLaurent Pinchart  *
1023ca481c9bSDavid Herrmann  * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
1024ca481c9bSDavid Herrmann  * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
1025ca481c9bSDavid Herrmann  * callers must verify access restrictions before calling this helper.
1026ca481c9bSDavid Herrmann  *
10271c5aafa6SLaurent Pinchart  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
1028d693def4SThomas Zimmermann  * size, or if no vm_ops are provided.
10291c5aafa6SLaurent Pinchart  */
10301c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
10311c5aafa6SLaurent Pinchart 		     struct vm_area_struct *vma)
10321c5aafa6SLaurent Pinchart {
1033c40069cbSGerd Hoffmann 	int ret;
10341c5aafa6SLaurent Pinchart 
10351c5aafa6SLaurent Pinchart 	/* Check for valid size. */
10361c5aafa6SLaurent Pinchart 	if (obj_size < vma->vm_end - vma->vm_start)
10371c5aafa6SLaurent Pinchart 		return -EINVAL;
10381c5aafa6SLaurent Pinchart 
10391c5aafa6SLaurent Pinchart 	/* Take a ref for this mapping of the object, so that the fault
10401c5aafa6SLaurent Pinchart 	 * handler can dereference the mmap offset's pointer to the object.
10411c5aafa6SLaurent Pinchart 	 * This reference is cleaned up by the corresponding vm_close
10421c5aafa6SLaurent Pinchart 	 * (which should happen whether the vma was created by this call, or
10431c5aafa6SLaurent Pinchart 	 * by a vm_open due to mremap or partial unmap or whatever).
10441c5aafa6SLaurent Pinchart 	 */
1045e6b62714SThierry Reding 	drm_gem_object_get(obj);
10461c5aafa6SLaurent Pinchart 
1047f49a51bfSDaniel Vetter 	vma->vm_private_data = obj;
104847d35c1cSThomas Zimmermann 	vma->vm_ops = obj->funcs->vm_ops;
1049f49a51bfSDaniel Vetter 
1050d693def4SThomas Zimmermann 	if (obj->funcs->mmap) {
10519786b65bSGerd Hoffmann 		ret = obj->funcs->mmap(obj, vma);
105247d35c1cSThomas Zimmermann 		if (ret)
105347d35c1cSThomas Zimmermann 			goto err_drm_gem_object_put;
10549786b65bSGerd Hoffmann 		WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
10559786b65bSGerd Hoffmann 	} else {
105647d35c1cSThomas Zimmermann 		if (!vma->vm_ops) {
105747d35c1cSThomas Zimmermann 			ret = -EINVAL;
105847d35c1cSThomas Zimmermann 			goto err_drm_gem_object_put;
10599786b65bSGerd Hoffmann 		}
10609786b65bSGerd Hoffmann 
10619786b65bSGerd Hoffmann 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
10629786b65bSGerd Hoffmann 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
10639786b65bSGerd Hoffmann 		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
10649786b65bSGerd Hoffmann 	}
10659786b65bSGerd Hoffmann 
10661c5aafa6SLaurent Pinchart 	return 0;
106747d35c1cSThomas Zimmermann 
106847d35c1cSThomas Zimmermann err_drm_gem_object_put:
106947d35c1cSThomas Zimmermann 	drm_gem_object_put(obj);
107047d35c1cSThomas Zimmermann 	return ret;
10711c5aafa6SLaurent Pinchart }
10721c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj);
1073ab00b3e5SJesse Barnes 
1074a2c0a97bSJesse Barnes /**
1075a2c0a97bSJesse Barnes  * drm_gem_mmap - memory map routine for GEM objects
1076a2c0a97bSJesse Barnes  * @filp: DRM file pointer
1077a2c0a97bSJesse Barnes  * @vma: VMA for the area to be mapped
1078a2c0a97bSJesse Barnes  *
1079a2c0a97bSJesse Barnes  * If a driver supports GEM object mapping, mmap calls on the DRM file
1080a2c0a97bSJesse Barnes  * descriptor will end up here.
1081a2c0a97bSJesse Barnes  *
10821c5aafa6SLaurent Pinchart  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
1083a2c0a97bSJesse Barnes  * contain the fake offset we created when the GTT map ioctl was called on
10841c5aafa6SLaurent Pinchart  * the object) and map it with a call to drm_gem_mmap_obj().
1085ca481c9bSDavid Herrmann  *
1086ca481c9bSDavid Herrmann  * If the caller is not granted access to the buffer object, the mmap will fail
1087ca481c9bSDavid Herrmann  * with EACCES. Please see the vma manager for more information.
1088a2c0a97bSJesse Barnes  */
1089a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
1090a2c0a97bSJesse Barnes {
1091a2c0a97bSJesse Barnes 	struct drm_file *priv = filp->private_data;
1092a2c0a97bSJesse Barnes 	struct drm_device *dev = priv->minor->dev;
10932225cfe4SDaniel Vetter 	struct drm_gem_object *obj = NULL;
10940de23977SDavid Herrmann 	struct drm_vma_offset_node *node;
1095a8469aa8SDavid Herrmann 	int ret;
1096a2c0a97bSJesse Barnes 
1097c07dcd61SDaniel Vetter 	if (drm_dev_is_unplugged(dev))
10982c07a21dSDave Airlie 		return -ENODEV;
10992c07a21dSDave Airlie 
11002225cfe4SDaniel Vetter 	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
11012225cfe4SDaniel Vetter 	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1102b04a5906SDaniel Vetter 						  vma->vm_pgoff,
11030de23977SDavid Herrmann 						  vma_pages(vma));
11042225cfe4SDaniel Vetter 	if (likely(node)) {
11052225cfe4SDaniel Vetter 		obj = container_of(node, struct drm_gem_object, vma_node);
11062225cfe4SDaniel Vetter 		/*
11072225cfe4SDaniel Vetter 		 * When the object is being freed, after it hits 0-refcnt it
11082225cfe4SDaniel Vetter 		 * proceeds to tear down the object. In the process it will
11092225cfe4SDaniel Vetter 		 * attempt to remove the VMA offset and so acquire this
11102225cfe4SDaniel Vetter 		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
11112225cfe4SDaniel Vetter 		 * that matches our range, we know it is in the process of being
11122225cfe4SDaniel Vetter 		 * destroyed and will be freed as soon as we release the lock -
11132225cfe4SDaniel Vetter 		 * so we have to check for the 0-refcnted object and treat it as
11142225cfe4SDaniel Vetter 		 * invalid.
11152225cfe4SDaniel Vetter 		 */
11162225cfe4SDaniel Vetter 		if (!kref_get_unless_zero(&obj->refcount))
11172225cfe4SDaniel Vetter 			obj = NULL;
11182225cfe4SDaniel Vetter 	}
11192225cfe4SDaniel Vetter 	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
11202225cfe4SDaniel Vetter 
11212225cfe4SDaniel Vetter 	if (!obj)
1122197633b9SDaniel Vetter 		return -EINVAL;
11232225cfe4SDaniel Vetter 
1124d9a1f0b4SDavid Herrmann 	if (!drm_vma_node_is_allowed(node, priv)) {
1125be6ee102SEmil Velikov 		drm_gem_object_put(obj);
1126ca481c9bSDavid Herrmann 		return -EACCES;
1127a2c0a97bSJesse Barnes 	}
1128a2c0a97bSJesse Barnes 
11292225cfe4SDaniel Vetter 	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
11302225cfe4SDaniel Vetter 			       vma);
1131a2c0a97bSJesse Barnes 
1132be6ee102SEmil Velikov 	drm_gem_object_put(obj);
1133a2c0a97bSJesse Barnes 
1134a2c0a97bSJesse Barnes 	return ret;
1135a2c0a97bSJesse Barnes }
1136a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap);
113745d58b40SNoralf Trønnes 
113845d58b40SNoralf Trønnes void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
113945d58b40SNoralf Trønnes 			const struct drm_gem_object *obj)
114045d58b40SNoralf Trønnes {
114145d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "name=%d\n", obj->name);
114245d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "refcount=%u\n",
114345d58b40SNoralf Trønnes 			  kref_read(&obj->refcount));
114445d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "start=%08lx\n",
114545d58b40SNoralf Trønnes 			  drm_vma_node_start(&obj->vma_node));
114645d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "size=%zu\n", obj->size);
114745d58b40SNoralf Trønnes 	drm_printf_indent(p, indent, "imported=%s\n",
114845d58b40SNoralf Trønnes 			  obj->import_attach ? "yes" : "no");
114945d58b40SNoralf Trønnes 
1150d693def4SThomas Zimmermann 	if (obj->funcs->print_info)
1151b39b5394SNoralf Trønnes 		obj->funcs->print_info(p, indent, obj);
115245d58b40SNoralf Trønnes }
1153b39b5394SNoralf Trønnes 
1154b39b5394SNoralf Trønnes int drm_gem_pin(struct drm_gem_object *obj)
1155b39b5394SNoralf Trønnes {
1156d693def4SThomas Zimmermann 	if (obj->funcs->pin)
1157b39b5394SNoralf Trønnes 		return obj->funcs->pin(obj);
1158b39b5394SNoralf Trønnes 	else
1159b39b5394SNoralf Trønnes 		return 0;
1160b39b5394SNoralf Trønnes }
1161b39b5394SNoralf Trønnes 
1162b39b5394SNoralf Trønnes void drm_gem_unpin(struct drm_gem_object *obj)
1163b39b5394SNoralf Trønnes {
1164d693def4SThomas Zimmermann 	if (obj->funcs->unpin)
1165b39b5394SNoralf Trønnes 		obj->funcs->unpin(obj);
1166b39b5394SNoralf Trønnes }
1167b39b5394SNoralf Trønnes 
1168*7938f421SLucas De Marchi int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map)
1169b39b5394SNoralf Trønnes {
117049a3f51dSThomas Zimmermann 	int ret;
1171b39b5394SNoralf Trønnes 
117249a3f51dSThomas Zimmermann 	if (!obj->funcs->vmap)
1173a745fb1cSThomas Zimmermann 		return -EOPNOTSUPP;
1174b39b5394SNoralf Trønnes 
1175a745fb1cSThomas Zimmermann 	ret = obj->funcs->vmap(obj, map);
117649a3f51dSThomas Zimmermann 	if (ret)
1177a745fb1cSThomas Zimmermann 		return ret;
1178*7938f421SLucas De Marchi 	else if (iosys_map_is_null(map))
1179a745fb1cSThomas Zimmermann 		return -ENOMEM;
1180b39b5394SNoralf Trønnes 
1181a745fb1cSThomas Zimmermann 	return 0;
1182b39b5394SNoralf Trønnes }
1183db0c6bd2SThomas Zimmermann EXPORT_SYMBOL(drm_gem_vmap);
1184b39b5394SNoralf Trønnes 
1185*7938f421SLucas De Marchi void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
1186b39b5394SNoralf Trønnes {
1187*7938f421SLucas De Marchi 	if (iosys_map_is_null(map))
1188b39b5394SNoralf Trønnes 		return;
1189b39b5394SNoralf Trønnes 
1190d693def4SThomas Zimmermann 	if (obj->funcs->vunmap)
1191a745fb1cSThomas Zimmermann 		obj->funcs->vunmap(obj, map);
1192a745fb1cSThomas Zimmermann 
1193a745fb1cSThomas Zimmermann 	/* Always set the mapping to NULL. Callers may rely on this. */
1194*7938f421SLucas De Marchi 	iosys_map_clear(map);
1195b39b5394SNoralf Trønnes }
1196db0c6bd2SThomas Zimmermann EXPORT_SYMBOL(drm_gem_vunmap);
11977edc3e3bSEric Anholt 
11987edc3e3bSEric Anholt /**
11997edc3e3bSEric Anholt  * drm_gem_lock_reservations - Sets up the ww context and acquires
12007edc3e3bSEric Anholt  * the lock on an array of GEM objects.
12017edc3e3bSEric Anholt  *
12027edc3e3bSEric Anholt  * Once you've locked your reservations, you'll want to set up space
12037edc3e3bSEric Anholt  * for your shared fences (if applicable), submit your job, then
12047edc3e3bSEric Anholt  * drm_gem_unlock_reservations().
12057edc3e3bSEric Anholt  *
12067edc3e3bSEric Anholt  * @objs: drm_gem_objects to lock
12077edc3e3bSEric Anholt  * @count: Number of objects in @objs
12087edc3e3bSEric Anholt  * @acquire_ctx: struct ww_acquire_ctx that will be initialized as
12097edc3e3bSEric Anholt  * part of tracking this set of locked reservations.
12107edc3e3bSEric Anholt  */
12117edc3e3bSEric Anholt int
12127edc3e3bSEric Anholt drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
12137edc3e3bSEric Anholt 			  struct ww_acquire_ctx *acquire_ctx)
12147edc3e3bSEric Anholt {
12157edc3e3bSEric Anholt 	int contended = -1;
12167edc3e3bSEric Anholt 	int i, ret;
12177edc3e3bSEric Anholt 
12187edc3e3bSEric Anholt 	ww_acquire_init(acquire_ctx, &reservation_ww_class);
12197edc3e3bSEric Anholt 
12207edc3e3bSEric Anholt retry:
12217edc3e3bSEric Anholt 	if (contended != -1) {
12227edc3e3bSEric Anholt 		struct drm_gem_object *obj = objs[contended];
12237edc3e3bSEric Anholt 
122452791eeeSChristian König 		ret = dma_resv_lock_slow_interruptible(obj->resv,
12257edc3e3bSEric Anholt 								 acquire_ctx);
12267edc3e3bSEric Anholt 		if (ret) {
12277edc3e3bSEric Anholt 			ww_acquire_done(acquire_ctx);
12287edc3e3bSEric Anholt 			return ret;
12297edc3e3bSEric Anholt 		}
12307edc3e3bSEric Anholt 	}
12317edc3e3bSEric Anholt 
12327edc3e3bSEric Anholt 	for (i = 0; i < count; i++) {
12337edc3e3bSEric Anholt 		if (i == contended)
12347edc3e3bSEric Anholt 			continue;
12357edc3e3bSEric Anholt 
123652791eeeSChristian König 		ret = dma_resv_lock_interruptible(objs[i]->resv,
12377edc3e3bSEric Anholt 							    acquire_ctx);
12387edc3e3bSEric Anholt 		if (ret) {
12397edc3e3bSEric Anholt 			int j;
12407edc3e3bSEric Anholt 
12417edc3e3bSEric Anholt 			for (j = 0; j < i; j++)
124252791eeeSChristian König 				dma_resv_unlock(objs[j]->resv);
12437edc3e3bSEric Anholt 
12447edc3e3bSEric Anholt 			if (contended != -1 && contended >= i)
124552791eeeSChristian König 				dma_resv_unlock(objs[contended]->resv);
12467edc3e3bSEric Anholt 
12477edc3e3bSEric Anholt 			if (ret == -EDEADLK) {
12487edc3e3bSEric Anholt 				contended = i;
12497edc3e3bSEric Anholt 				goto retry;
12507edc3e3bSEric Anholt 			}
12517edc3e3bSEric Anholt 
12527edc3e3bSEric Anholt 			ww_acquire_done(acquire_ctx);
12537edc3e3bSEric Anholt 			return ret;
12547edc3e3bSEric Anholt 		}
12557edc3e3bSEric Anholt 	}
12567edc3e3bSEric Anholt 
12577edc3e3bSEric Anholt 	ww_acquire_done(acquire_ctx);
12587edc3e3bSEric Anholt 
12597edc3e3bSEric Anholt 	return 0;
12607edc3e3bSEric Anholt }
12617edc3e3bSEric Anholt EXPORT_SYMBOL(drm_gem_lock_reservations);
12627edc3e3bSEric Anholt 
12637edc3e3bSEric Anholt void
12647edc3e3bSEric Anholt drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
12657edc3e3bSEric Anholt 			    struct ww_acquire_ctx *acquire_ctx)
12667edc3e3bSEric Anholt {
12677edc3e3bSEric Anholt 	int i;
12687edc3e3bSEric Anholt 
12697edc3e3bSEric Anholt 	for (i = 0; i < count; i++)
127052791eeeSChristian König 		dma_resv_unlock(objs[i]->resv);
12717edc3e3bSEric Anholt 
12727edc3e3bSEric Anholt 	ww_acquire_fini(acquire_ctx);
12737edc3e3bSEric Anholt }
12747edc3e3bSEric Anholt EXPORT_SYMBOL(drm_gem_unlock_reservations);
12755d5a179dSEric Anholt 
12765d5a179dSEric Anholt /**
12775d5a179dSEric Anholt  * drm_gem_fence_array_add - Adds the fence to an array of fences to be
12785d5a179dSEric Anholt  * waited on, deduplicating fences from the same context.
12795d5a179dSEric Anholt  *
1280761e473fSSean Paul  * @fence_array: array of dma_fence * for the job to block on.
1281761e473fSSean Paul  * @fence: the dma_fence to add to the list of dependencies.
12825d5a179dSEric Anholt  *
1283d7fdae59SDaniel Vetter  * This functions consumes the reference for @fence both on success and error
1284d7fdae59SDaniel Vetter  * cases.
1285d7fdae59SDaniel Vetter  *
12865d5a179dSEric Anholt  * Returns:
12875d5a179dSEric Anholt  * 0 on success, or an error on failing to expand the array.
12885d5a179dSEric Anholt  */
12895d5a179dSEric Anholt int drm_gem_fence_array_add(struct xarray *fence_array,
12905d5a179dSEric Anholt 			    struct dma_fence *fence)
12915d5a179dSEric Anholt {
12925d5a179dSEric Anholt 	struct dma_fence *entry;
12935d5a179dSEric Anholt 	unsigned long index;
12945d5a179dSEric Anholt 	u32 id = 0;
12955d5a179dSEric Anholt 	int ret;
12965d5a179dSEric Anholt 
12975d5a179dSEric Anholt 	if (!fence)
12985d5a179dSEric Anholt 		return 0;
12995d5a179dSEric Anholt 
13005d5a179dSEric Anholt 	/* Deduplicate if we already depend on a fence from the same context.
13015d5a179dSEric Anholt 	 * This lets the size of the array of deps scale with the number of
13025d5a179dSEric Anholt 	 * engines involved, rather than the number of BOs.
13035d5a179dSEric Anholt 	 */
13045d5a179dSEric Anholt 	xa_for_each(fence_array, index, entry) {
13055d5a179dSEric Anholt 		if (entry->context != fence->context)
13065d5a179dSEric Anholt 			continue;
13075d5a179dSEric Anholt 
13085d5a179dSEric Anholt 		if (dma_fence_is_later(fence, entry)) {
13095d5a179dSEric Anholt 			dma_fence_put(entry);
13105d5a179dSEric Anholt 			xa_store(fence_array, index, fence, GFP_KERNEL);
13115d5a179dSEric Anholt 		} else {
13125d5a179dSEric Anholt 			dma_fence_put(fence);
13135d5a179dSEric Anholt 		}
13145d5a179dSEric Anholt 		return 0;
13155d5a179dSEric Anholt 	}
13165d5a179dSEric Anholt 
13175d5a179dSEric Anholt 	ret = xa_alloc(fence_array, &id, fence, xa_limit_32b, GFP_KERNEL);
13185d5a179dSEric Anholt 	if (ret != 0)
13195d5a179dSEric Anholt 		dma_fence_put(fence);
13205d5a179dSEric Anholt 
13215d5a179dSEric Anholt 	return ret;
13225d5a179dSEric Anholt }
13235d5a179dSEric Anholt EXPORT_SYMBOL(drm_gem_fence_array_add);
13245d5a179dSEric Anholt 
13255d5a179dSEric Anholt /**
13265d5a179dSEric Anholt  * drm_gem_fence_array_add_implicit - Adds the implicit dependencies tracked
13275d5a179dSEric Anholt  * in the GEM object's reservation object to an array of dma_fences for use in
13285d5a179dSEric Anholt  * scheduling a rendering job.
13295d5a179dSEric Anholt  *
13305d5a179dSEric Anholt  * This should be called after drm_gem_lock_reservations() on your array of
13315d5a179dSEric Anholt  * GEM objects used in the job but before updating the reservations with your
13325d5a179dSEric Anholt  * own fences.
13335d5a179dSEric Anholt  *
1334761e473fSSean Paul  * @fence_array: array of dma_fence * for the job to block on.
1335761e473fSSean Paul  * @obj: the gem object to add new dependencies from.
1336761e473fSSean Paul  * @write: whether the job might write the object (so we need to depend on
13375d5a179dSEric Anholt  * shared fences in the reservation object).
13385d5a179dSEric Anholt  */
13395d5a179dSEric Anholt int drm_gem_fence_array_add_implicit(struct xarray *fence_array,
13405d5a179dSEric Anholt 				     struct drm_gem_object *obj,
13415d5a179dSEric Anholt 				     bool write)
13425d5a179dSEric Anholt {
13437cd80132SChristian König 	struct dma_resv_iter cursor;
13447cd80132SChristian König 	struct dma_fence *fence;
13457cd80132SChristian König 	int ret = 0;
13465d5a179dSEric Anholt 
13477cd80132SChristian König 	dma_resv_for_each_fence(&cursor, obj->resv, write, fence) {
13487cd80132SChristian König 		ret = drm_gem_fence_array_add(fence_array, fence);
13495d5a179dSEric Anholt 		if (ret)
13505d5a179dSEric Anholt 			break;
13515d5a179dSEric Anholt 	}
13525d5a179dSEric Anholt 	return ret;
13535d5a179dSEric Anholt }
13545d5a179dSEric Anholt EXPORT_SYMBOL(drm_gem_fence_array_add_implicit);
1355