xref: /netbsd/sys/external/bsd/drm2/dist/drm/drm_gem.c (revision f411b747)
1*f411b747Sriastradh /*	$NetBSD: drm_gem.c,v 1.23 2021/12/19 11:58:49 riastradh Exp $	*/
24e59feabSriastradh 
356053ce7Sriastradh /*
456053ce7Sriastradh  * Copyright © 2008 Intel Corporation
556053ce7Sriastradh  *
656053ce7Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
756053ce7Sriastradh  * copy of this software and associated documentation files (the "Software"),
856053ce7Sriastradh  * to deal in the Software without restriction, including without limitation
956053ce7Sriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1056053ce7Sriastradh  * and/or sell copies of the Software, and to permit persons to whom the
1156053ce7Sriastradh  * Software is furnished to do so, subject to the following conditions:
1256053ce7Sriastradh  *
1356053ce7Sriastradh  * The above copyright notice and this permission notice (including the next
1456053ce7Sriastradh  * paragraph) shall be included in all copies or substantial portions of the
1556053ce7Sriastradh  * Software.
1656053ce7Sriastradh  *
1756053ce7Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1856053ce7Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1956053ce7Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2056053ce7Sriastradh  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2156053ce7Sriastradh  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2256053ce7Sriastradh  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2356053ce7Sriastradh  * IN THE SOFTWARE.
2456053ce7Sriastradh  *
2556053ce7Sriastradh  * Authors:
2656053ce7Sriastradh  *    Eric Anholt <eric@anholt.net>
2756053ce7Sriastradh  *
2856053ce7Sriastradh  */
2956053ce7Sriastradh 
304e59feabSriastradh #include <sys/cdefs.h>
31*f411b747Sriastradh __KERNEL_RCSID(0, "$NetBSD: drm_gem.c,v 1.23 2021/12/19 11:58:49 riastradh Exp $");
324e59feabSriastradh 
3356053ce7Sriastradh #include <linux/types.h>
3456053ce7Sriastradh #include <linux/slab.h>
3556053ce7Sriastradh #include <linux/mm.h>
3656053ce7Sriastradh #include <linux/uaccess.h>
3756053ce7Sriastradh #include <linux/fs.h>
3856053ce7Sriastradh #include <linux/file.h>
3956053ce7Sriastradh #include <linux/module.h>
4056053ce7Sriastradh #include <linux/mman.h>
4156053ce7Sriastradh #include <linux/pagemap.h>
4256053ce7Sriastradh #include <linux/shmem_fs.h>
4356053ce7Sriastradh #include <linux/dma-buf.h>
44677dec6eSriastradh #include <linux/mem_encrypt.h>
45677dec6eSriastradh #include <linux/pagevec.h>
46677dec6eSriastradh 
47677dec6eSriastradh #include <drm/drm.h>
48677dec6eSriastradh #include <drm/drm_device.h>
49677dec6eSriastradh #include <drm/drm_drv.h>
50677dec6eSriastradh #include <drm/drm_file.h>
514e59feabSriastradh #include <drm/drm_gem.h>
52677dec6eSriastradh #include <drm/drm_print.h>
53677dec6eSriastradh #include <drm/drm_vma_manager.h>
54677dec6eSriastradh 
554e59feabSriastradh #include "drm_internal.h"
5656053ce7Sriastradh 
57fceff80cSriastradh #ifdef __NetBSD__
58fceff80cSriastradh #include <uvm/uvm_extern.h>
591cb26435Sriastradh #include <linux/nbsd-namespace.h>
60fceff80cSriastradh #endif
61fceff80cSriastradh 
6256053ce7Sriastradh /** @file drm_gem.c
6356053ce7Sriastradh  *
6456053ce7Sriastradh  * This file provides some of the base ioctls and library routines for
6556053ce7Sriastradh  * the graphics memory manager implemented by each device driver.
6656053ce7Sriastradh  *
6756053ce7Sriastradh  * Because various devices have different requirements in terms of
6856053ce7Sriastradh  * synchronization and migration strategies, implementing that is left up to
6956053ce7Sriastradh  * the driver, and all that the general API provides should be generic --
7056053ce7Sriastradh  * allocating objects, reading/writing data with the cpu, freeing objects.
7156053ce7Sriastradh  * Even there, platform-dependent optimizations for reading/writing data with
7256053ce7Sriastradh  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
7356053ce7Sriastradh  * the DRI2 implementation wants to have at least allocate/mmap be generic.
7456053ce7Sriastradh  *
7556053ce7Sriastradh  * The goal was to have swap-backed object allocation managed through
7656053ce7Sriastradh  * struct file.  However, file descriptors as handles to a struct file have
7756053ce7Sriastradh  * two major failings:
7856053ce7Sriastradh  * - Process limits prevent more than 1024 or so being used at a time by
7956053ce7Sriastradh  *   default.
8056053ce7Sriastradh  * - Inability to allocate high fds will aggravate the X Server's select()
8156053ce7Sriastradh  *   handling, and likely that of many GL client applications as well.
8256053ce7Sriastradh  *
8356053ce7Sriastradh  * This led to a plan of using our own integer IDs (called handles, following
8456053ce7Sriastradh  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
8556053ce7Sriastradh  * ioctls.  The objects themselves will still include the struct file so
8656053ce7Sriastradh  * that we can transition to fds if the required kernel infrastructure shows
8756053ce7Sriastradh  * up at a later date, and as our interface with shmfs for memory allocation.
8856053ce7Sriastradh  */
8956053ce7Sriastradh 
9056053ce7Sriastradh /**
91a55ed7faSriastradh  * drm_gem_init - Initialize the GEM device fields
92a55ed7faSriastradh  * @dev: drm_devic structure to initialize
9356053ce7Sriastradh  */
9456053ce7Sriastradh int
drm_gem_init(struct drm_device * dev)9556053ce7Sriastradh drm_gem_init(struct drm_device *dev)
9656053ce7Sriastradh {
97a55ed7faSriastradh 	struct drm_vma_offset_manager *vma_offset_manager;
9856053ce7Sriastradh 
99a55ed7faSriastradh 	mutex_init(&dev->object_name_lock);
100677dec6eSriastradh 	idr_init_base(&dev->object_name_idr, 1);
10156053ce7Sriastradh 
102a55ed7faSriastradh 	vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
103a55ed7faSriastradh 	if (!vma_offset_manager) {
10456053ce7Sriastradh 		DRM_ERROR("out of memory\n");
10556053ce7Sriastradh 		return -ENOMEM;
10656053ce7Sriastradh 	}
10756053ce7Sriastradh 
108a55ed7faSriastradh 	dev->vma_offset_manager = vma_offset_manager;
109a55ed7faSriastradh 	drm_vma_offset_manager_init(vma_offset_manager,
110a55ed7faSriastradh 				    DRM_FILE_PAGE_OFFSET_START,
111a55ed7faSriastradh 				    DRM_FILE_PAGE_OFFSET_SIZE);
11256053ce7Sriastradh 
11356053ce7Sriastradh 	return 0;
11456053ce7Sriastradh }
11556053ce7Sriastradh 
11656053ce7Sriastradh void
drm_gem_destroy(struct drm_device * dev)11756053ce7Sriastradh drm_gem_destroy(struct drm_device *dev)
11856053ce7Sriastradh {
11956053ce7Sriastradh 
120a55ed7faSriastradh 	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
121a55ed7faSriastradh 	kfree(dev->vma_offset_manager);
122a55ed7faSriastradh 	dev->vma_offset_manager = NULL;
1232adb3a73Sriastradh 
1242adb3a73Sriastradh 	idr_destroy(&dev->object_name_idr);
1251cb26435Sriastradh 	mutex_destroy(&dev->object_name_lock);
12656053ce7Sriastradh }
12756053ce7Sriastradh 
12856053ce7Sriastradh /**
129a55ed7faSriastradh  * drm_gem_object_init - initialize an allocated shmem-backed GEM object
130a55ed7faSriastradh  * @dev: drm_device the object should be initialized for
131a55ed7faSriastradh  * @obj: drm_gem_object to initialize
132a55ed7faSriastradh  * @size: object size
133a55ed7faSriastradh  *
13456053ce7Sriastradh  * Initialize an already allocated GEM object of the specified size with
13556053ce7Sriastradh  * shmfs backing store.
13656053ce7Sriastradh  */
drm_gem_object_init(struct drm_device * dev,struct drm_gem_object * obj,size_t size)13756053ce7Sriastradh int drm_gem_object_init(struct drm_device *dev,
13856053ce7Sriastradh 			struct drm_gem_object *obj, size_t size)
13956053ce7Sriastradh {
140fceff80cSriastradh #ifndef __NetBSD__
141a55ed7faSriastradh 	struct file *filp;
142fceff80cSriastradh #endif
14356053ce7Sriastradh 
144a55ed7faSriastradh 	drm_gem_private_object_init(dev, obj, size);
145a55ed7faSriastradh 
1462adb3a73Sriastradh #ifdef __NetBSD__
147fd145bfcSriastradh 	/*
148fd145bfcSriastradh 	 * A uao may not have size 0, but a gem object may.  Allocate a
149fd145bfcSriastradh 	 * spurious page so we needn't teach uao how to have size 0.
150fd145bfcSriastradh 	 */
1514f3c69ffSriastradh 	obj->filp = uao_create(MAX(size, PAGE_SIZE), 0);
1520e0fe2b3Sriastradh 	/*
1530e0fe2b3Sriastradh 	 * XXX This is gross.  We ought to do it the other way around:
1540e0fe2b3Sriastradh 	 * set the uao to have the main uvm object's lock.  However,
1550e0fe2b3Sriastradh 	 * uvm_obj_setlock is not safe on uvm_aobjs.
1560e0fe2b3Sriastradh 	 */
157b4dac182Sad 	rw_obj_hold(obj->filp->vmobjlock);
1584f3c69ffSriastradh 	uvm_obj_setlock(&obj->gemo_uvmobj, obj->filp->vmobjlock);
1592adb3a73Sriastradh #else
160a55ed7faSriastradh 	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
161a55ed7faSriastradh 	if (IS_ERR(filp))
162a55ed7faSriastradh 		return PTR_ERR(filp);
16356053ce7Sriastradh 
164a55ed7faSriastradh 	obj->filp = filp;
165a55ed7faSriastradh #endif
16656053ce7Sriastradh 
16756053ce7Sriastradh 	return 0;
16856053ce7Sriastradh }
16956053ce7Sriastradh EXPORT_SYMBOL(drm_gem_object_init);
17056053ce7Sriastradh 
17156053ce7Sriastradh /**
1724e59feabSriastradh  * drm_gem_private_object_init - initialize an allocated private GEM object
173a55ed7faSriastradh  * @dev: drm_device the object should be initialized for
174a55ed7faSriastradh  * @obj: drm_gem_object to initialize
175a55ed7faSriastradh  * @size: object size
176a55ed7faSriastradh  *
17756053ce7Sriastradh  * Initialize an already allocated GEM object of the specified size with
17856053ce7Sriastradh  * no GEM provided backing store. Instead the caller is responsible for
17956053ce7Sriastradh  * backing the object and handling it.
18056053ce7Sriastradh  */
drm_gem_private_object_init(struct drm_device * dev,struct drm_gem_object * obj,size_t size)181a55ed7faSriastradh void drm_gem_private_object_init(struct drm_device *dev,
18256053ce7Sriastradh 				 struct drm_gem_object *obj, size_t size)
18356053ce7Sriastradh {
18456053ce7Sriastradh 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
18556053ce7Sriastradh 
18656053ce7Sriastradh 	obj->dev = dev;
1872adb3a73Sriastradh #ifdef __NetBSD__
1884f3c69ffSriastradh 	obj->filp = NULL;
1892adb3a73Sriastradh 	KASSERT(drm_core_check_feature(dev, DRIVER_GEM));
1902adb3a73Sriastradh 	KASSERT(dev->driver->gem_uvm_ops != NULL);
191491755bdSriastradh 	uvm_obj_init(&obj->gemo_uvmobj, dev->driver->gem_uvm_ops,
192491755bdSriastradh 	    /*allocate lock*/true, /*nrefs*/1);
1932adb3a73Sriastradh #else
19456053ce7Sriastradh 	obj->filp = NULL;
1952adb3a73Sriastradh #endif
19656053ce7Sriastradh 
19756053ce7Sriastradh 	kref_init(&obj->refcount);
198a55ed7faSriastradh 	obj->handle_count = 0;
19956053ce7Sriastradh 	obj->size = size;
200677dec6eSriastradh 	dma_resv_init(&obj->_resv);
201677dec6eSriastradh 	if (!obj->resv)
202677dec6eSriastradh 		obj->resv = &obj->_resv;
203677dec6eSriastradh 
204b9dca190Sriastradh #ifdef __NetBSD__
205b9dca190Sriastradh 	drm_vma_node_init(&obj->vma_node);
206b9dca190Sriastradh #else
207a55ed7faSriastradh 	drm_vma_node_reset(&obj->vma_node);
208fceff80cSriastradh #endif
20956053ce7Sriastradh }
21056053ce7Sriastradh EXPORT_SYMBOL(drm_gem_private_object_init);
21156053ce7Sriastradh 
21256053ce7Sriastradh static void
drm_gem_remove_prime_handles(struct drm_gem_object * obj,struct drm_file * filp)21356053ce7Sriastradh drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
21456053ce7Sriastradh {
215a55ed7faSriastradh 	/*
216a55ed7faSriastradh 	 * Note: obj->dma_buf can't disappear as long as we still hold a
217a55ed7faSriastradh 	 * handle reference in obj->handle_count.
218a55ed7faSriastradh 	 */
219a55ed7faSriastradh 	mutex_lock(&filp->prime.lock);
220a55ed7faSriastradh 	if (obj->dma_buf) {
221a55ed7faSriastradh 		drm_prime_remove_buf_handle_locked(&filp->prime,
222a55ed7faSriastradh 						   obj->dma_buf);
22356053ce7Sriastradh 	}
224a55ed7faSriastradh 	mutex_unlock(&filp->prime.lock);
22556053ce7Sriastradh }
22656053ce7Sriastradh 
22756053ce7Sriastradh /**
2284e59feabSriastradh  * drm_gem_object_handle_free - release resources bound to userspace handles
229a55ed7faSriastradh  * @obj: GEM object to clean up.
230a55ed7faSriastradh  *
231a55ed7faSriastradh  * Called after the last handle to the object has been closed
232a55ed7faSriastradh  *
233a55ed7faSriastradh  * Removes any name for the object. Note that this must be
234a55ed7faSriastradh  * called before drm_gem_object_free or we'll be touching
235a55ed7faSriastradh  * freed memory
236a55ed7faSriastradh  */
drm_gem_object_handle_free(struct drm_gem_object * obj)237a55ed7faSriastradh static void drm_gem_object_handle_free(struct drm_gem_object *obj)
238a55ed7faSriastradh {
239a55ed7faSriastradh 	struct drm_device *dev = obj->dev;
240a55ed7faSriastradh 
241a55ed7faSriastradh 	/* Remove any name for this object */
242a55ed7faSriastradh 	if (obj->name) {
243a55ed7faSriastradh 		idr_remove(&dev->object_name_idr, obj->name);
244a55ed7faSriastradh 		obj->name = 0;
245a55ed7faSriastradh 	}
246a55ed7faSriastradh }
247a55ed7faSriastradh 
drm_gem_object_exported_dma_buf_free(struct drm_gem_object * obj)248a55ed7faSriastradh static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
249a55ed7faSriastradh {
250fceff80cSriastradh #ifndef __NetBSD__
251a55ed7faSriastradh 	/* Unbreak the reference cycle if we have an exported dma_buf. */
252a55ed7faSriastradh 	if (obj->dma_buf) {
253a55ed7faSriastradh 		dma_buf_put(obj->dma_buf);
254a55ed7faSriastradh 		obj->dma_buf = NULL;
255a55ed7faSriastradh 	}
256fceff80cSriastradh #endif
257a55ed7faSriastradh }
258a55ed7faSriastradh 
259a55ed7faSriastradh static void
drm_gem_object_handle_put_unlocked(struct drm_gem_object * obj)260677dec6eSriastradh drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
261a55ed7faSriastradh {
262677dec6eSriastradh 	struct drm_device *dev = obj->dev;
263677dec6eSriastradh 	bool final = false;
264677dec6eSriastradh 
265a55ed7faSriastradh 	if (WARN_ON(obj->handle_count == 0))
266a55ed7faSriastradh 		return;
267a55ed7faSriastradh 
268a55ed7faSriastradh 	/*
269a55ed7faSriastradh 	* Must bump handle count first as this may be the last
270a55ed7faSriastradh 	* ref, in which case the object would disappear before we
271a55ed7faSriastradh 	* checked for a name
272a55ed7faSriastradh 	*/
273a55ed7faSriastradh 
274677dec6eSriastradh 	mutex_lock(&dev->object_name_lock);
275a55ed7faSriastradh 	if (--obj->handle_count == 0) {
276a55ed7faSriastradh 		drm_gem_object_handle_free(obj);
277a55ed7faSriastradh 		drm_gem_object_exported_dma_buf_free(obj);
278677dec6eSriastradh 		final = true;
279a55ed7faSriastradh 	}
280677dec6eSriastradh 	mutex_unlock(&dev->object_name_lock);
281a55ed7faSriastradh 
282677dec6eSriastradh 	if (final)
283677dec6eSriastradh 		drm_gem_object_put_unlocked(obj);
284677dec6eSriastradh }
285677dec6eSriastradh 
286677dec6eSriastradh /*
287677dec6eSriastradh  * Called at device or object close to release the file's
288677dec6eSriastradh  * handle references on objects.
289677dec6eSriastradh  */
290677dec6eSriastradh static int
drm_gem_object_release_handle(int id,void * ptr,void * data)291677dec6eSriastradh drm_gem_object_release_handle(int id, void *ptr, void *data)
292677dec6eSriastradh {
293677dec6eSriastradh 	struct drm_file *file_priv = data;
294677dec6eSriastradh 	struct drm_gem_object *obj = ptr;
295677dec6eSriastradh 	struct drm_device *dev = obj->dev;
296677dec6eSriastradh 
297677dec6eSriastradh 	if (obj->funcs && obj->funcs->close)
298677dec6eSriastradh 		obj->funcs->close(obj, file_priv);
299677dec6eSriastradh 	else if (dev->driver->gem_close_object)
300677dec6eSriastradh 		dev->driver->gem_close_object(obj, file_priv);
301677dec6eSriastradh 
302677dec6eSriastradh 	drm_gem_remove_prime_handles(obj, file_priv);
303677dec6eSriastradh 	drm_vma_node_revoke(&obj->vma_node, file_priv);
304677dec6eSriastradh 
305677dec6eSriastradh 	drm_gem_object_handle_put_unlocked(obj);
306677dec6eSriastradh 
307677dec6eSriastradh 	return 0;
308a55ed7faSriastradh }
309a55ed7faSriastradh 
310a55ed7faSriastradh /**
311a55ed7faSriastradh  * drm_gem_handle_delete - deletes the given file-private handle
312a55ed7faSriastradh  * @filp: drm file-private structure to use for the handle look up
313a55ed7faSriastradh  * @handle: userspace handle to delete
314a55ed7faSriastradh  *
315677dec6eSriastradh  * Removes the GEM handle from the @filp lookup table which has been added with
316677dec6eSriastradh  * drm_gem_handle_create(). If this is the last handle also cleans up linked
317677dec6eSriastradh  * resources like GEM names.
31856053ce7Sriastradh  */
31956053ce7Sriastradh int
drm_gem_handle_delete(struct drm_file * filp,u32 handle)32056053ce7Sriastradh drm_gem_handle_delete(struct drm_file *filp, u32 handle)
32156053ce7Sriastradh {
32256053ce7Sriastradh 	struct drm_gem_object *obj;
32356053ce7Sriastradh 
32456053ce7Sriastradh 	spin_lock(&filp->table_lock);
32556053ce7Sriastradh 
32656053ce7Sriastradh 	/* Check if we currently have a reference on the object */
327677dec6eSriastradh 	obj = idr_replace(&filp->object_idr, NULL, handle);
32856053ce7Sriastradh 	spin_unlock(&filp->table_lock);
329677dec6eSriastradh 	if (IS_ERR_OR_NULL(obj))
33056053ce7Sriastradh 		return -EINVAL;
33156053ce7Sriastradh 
332677dec6eSriastradh 	/* Release driver's reference and decrement refcount. */
333677dec6eSriastradh 	drm_gem_object_release_handle(handle, obj, filp);
334677dec6eSriastradh 
335677dec6eSriastradh 	/* And finally make the handle available for future allocations. */
336677dec6eSriastradh 	spin_lock(&filp->table_lock);
33756053ce7Sriastradh 	idr_remove(&filp->object_idr, handle);
33856053ce7Sriastradh 	spin_unlock(&filp->table_lock);
33956053ce7Sriastradh 
34056053ce7Sriastradh 	return 0;
34156053ce7Sriastradh }
34256053ce7Sriastradh EXPORT_SYMBOL(drm_gem_handle_delete);
34356053ce7Sriastradh 
34456053ce7Sriastradh /**
345677dec6eSriastradh  * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
346677dec6eSriastradh  * @file: drm file-private structure containing the gem object
347677dec6eSriastradh  * @dev: corresponding drm_device
348677dec6eSriastradh  * @handle: gem object handle
349677dec6eSriastradh  * @offset: return location for the fake mmap offset
350677dec6eSriastradh  *
351677dec6eSriastradh  * This implements the &drm_driver.dumb_map_offset kms driver callback for
352677dec6eSriastradh  * drivers which use gem to manage their backing storage.
353677dec6eSriastradh  *
354677dec6eSriastradh  * Returns:
355677dec6eSriastradh  * 0 on success or a negative error code on failure.
356677dec6eSriastradh  */
drm_gem_dumb_map_offset(struct drm_file * file,struct drm_device * dev,u32 handle,u64 * offset)357677dec6eSriastradh int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
358677dec6eSriastradh 			    u32 handle, u64 *offset)
359677dec6eSriastradh {
360677dec6eSriastradh 	struct drm_gem_object *obj;
361677dec6eSriastradh 	int ret;
362677dec6eSriastradh 
363677dec6eSriastradh 	obj = drm_gem_object_lookup(file, handle);
364677dec6eSriastradh 	if (!obj)
365677dec6eSriastradh 		return -ENOENT;
366677dec6eSriastradh 
367677dec6eSriastradh 	/* Don't allow imported objects to be mapped */
368677dec6eSriastradh 	if (obj->import_attach) {
369677dec6eSriastradh 		ret = -EINVAL;
370677dec6eSriastradh 		goto out;
371677dec6eSriastradh 	}
372677dec6eSriastradh 
373677dec6eSriastradh 	ret = drm_gem_create_mmap_offset(obj);
374677dec6eSriastradh 	if (ret)
375677dec6eSriastradh 		goto out;
376677dec6eSriastradh 
377677dec6eSriastradh 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
378677dec6eSriastradh out:
379677dec6eSriastradh 	drm_gem_object_put_unlocked(obj);
380677dec6eSriastradh 
381677dec6eSriastradh 	return ret;
382677dec6eSriastradh }
383677dec6eSriastradh EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
384677dec6eSriastradh 
385677dec6eSriastradh /**
386a55ed7faSriastradh  * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
387a55ed7faSriastradh  * @file: drm file-private structure to remove the dumb handle from
388a55ed7faSriastradh  * @dev: corresponding drm_device
389a55ed7faSriastradh  * @handle: the dumb handle to remove
390a55ed7faSriastradh  *
391677dec6eSriastradh  * This implements the &drm_driver.dumb_destroy kms driver callback for drivers
392677dec6eSriastradh  * which use gem to manage their backing storage.
393a55ed7faSriastradh  */
drm_gem_dumb_destroy(struct drm_file * file,struct drm_device * dev,uint32_t handle)394a55ed7faSriastradh int drm_gem_dumb_destroy(struct drm_file *file,
395a55ed7faSriastradh 			 struct drm_device *dev,
396a55ed7faSriastradh 			 uint32_t handle)
397a55ed7faSriastradh {
398a55ed7faSriastradh 	return drm_gem_handle_delete(file, handle);
399a55ed7faSriastradh }
400a55ed7faSriastradh EXPORT_SYMBOL(drm_gem_dumb_destroy);
401a55ed7faSriastradh 
402a55ed7faSriastradh /**
403a55ed7faSriastradh  * drm_gem_handle_create_tail - internal functions to create a handle
404a55ed7faSriastradh  * @file_priv: drm file-private structure to register the handle for
405a55ed7faSriastradh  * @obj: object to register
4064e59feabSriastradh  * @handlep: pointer to return the created handle to the caller
407a55ed7faSriastradh  *
408677dec6eSriastradh  * This expects the &drm_device.object_name_lock to be held already and will
409677dec6eSriastradh  * drop it before returning. Used to avoid races in establishing new handles
410677dec6eSriastradh  * when importing an object from either an flink name or a dma-buf.
411677dec6eSriastradh  *
412677dec6eSriastradh  * Handles must be release again through drm_gem_handle_delete(). This is done
413677dec6eSriastradh  * when userspace closes @file_priv for all attached handles, or through the
414677dec6eSriastradh  * GEM_CLOSE ioctl for individual handles.
41556053ce7Sriastradh  */
41656053ce7Sriastradh int
drm_gem_handle_create_tail(struct drm_file * file_priv,struct drm_gem_object * obj,u32 * handlep)417a55ed7faSriastradh drm_gem_handle_create_tail(struct drm_file *file_priv,
41856053ce7Sriastradh 			   struct drm_gem_object *obj,
41956053ce7Sriastradh 			   u32 *handlep)
42056053ce7Sriastradh {
42156053ce7Sriastradh 	struct drm_device *dev = obj->dev;
422677dec6eSriastradh 	u32 handle;
42356053ce7Sriastradh 	int ret;
42456053ce7Sriastradh 
425a55ed7faSriastradh 	WARN_ON(!mutex_is_locked(&dev->object_name_lock));
426677dec6eSriastradh 	if (obj->handle_count++ == 0)
427677dec6eSriastradh 		drm_gem_object_get(obj);
428a55ed7faSriastradh 
42956053ce7Sriastradh 	/*
430a55ed7faSriastradh 	 * Get the user-visible handle using idr.  Preload and perform
431a55ed7faSriastradh 	 * allocation under our spinlock.
43256053ce7Sriastradh 	 */
433a55ed7faSriastradh 	idr_preload(GFP_KERNEL);
43456053ce7Sriastradh 	spin_lock(&file_priv->table_lock);
43556053ce7Sriastradh 
436a55ed7faSriastradh 	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
437677dec6eSriastradh 
438a55ed7faSriastradh 	spin_unlock(&file_priv->table_lock);
439a55ed7faSriastradh 	idr_preload_end();
440677dec6eSriastradh 
441a55ed7faSriastradh 	mutex_unlock(&dev->object_name_lock);
4424e59feabSriastradh 	if (ret < 0)
4434e59feabSriastradh 		goto err_unref;
4444e59feabSriastradh 
445677dec6eSriastradh 	handle = ret;
446a55ed7faSriastradh 
447677dec6eSriastradh 	ret = drm_vma_node_allow(&obj->vma_node, file_priv);
4484e59feabSriastradh 	if (ret)
4494e59feabSriastradh 		goto err_remove;
45056053ce7Sriastradh 
451677dec6eSriastradh 	if (obj->funcs && obj->funcs->open) {
452677dec6eSriastradh 		ret = obj->funcs->open(obj, file_priv);
453677dec6eSriastradh 		if (ret)
454677dec6eSriastradh 			goto err_revoke;
455677dec6eSriastradh 	} else if (dev->driver->gem_open_object) {
45656053ce7Sriastradh 		ret = dev->driver->gem_open_object(obj, file_priv);
4574e59feabSriastradh 		if (ret)
4584e59feabSriastradh 			goto err_revoke;
45956053ce7Sriastradh 	}
46056053ce7Sriastradh 
461677dec6eSriastradh 	*handlep = handle;
46256053ce7Sriastradh 	return 0;
4634e59feabSriastradh 
4644e59feabSriastradh err_revoke:
465677dec6eSriastradh 	drm_vma_node_revoke(&obj->vma_node, file_priv);
4664e59feabSriastradh err_remove:
4674e59feabSriastradh 	spin_lock(&file_priv->table_lock);
468677dec6eSriastradh 	idr_remove(&file_priv->object_idr, handle);
4694e59feabSriastradh 	spin_unlock(&file_priv->table_lock);
4704e59feabSriastradh err_unref:
471677dec6eSriastradh 	drm_gem_object_handle_put_unlocked(obj);
4724e59feabSriastradh 	return ret;
47356053ce7Sriastradh }
474a55ed7faSriastradh 
475a55ed7faSriastradh /**
4764e59feabSriastradh  * drm_gem_handle_create - create a gem handle for an object
477a55ed7faSriastradh  * @file_priv: drm file-private structure to register the handle for
478a55ed7faSriastradh  * @obj: object to register
479a55ed7faSriastradh  * @handlep: pionter to return the created handle to the caller
480a55ed7faSriastradh  *
481677dec6eSriastradh  * Create a handle for this object. This adds a handle reference to the object,
482677dec6eSriastradh  * which includes a regular reference count. Callers will likely want to
483677dec6eSriastradh  * dereference the object afterwards.
484677dec6eSriastradh  *
485677dec6eSriastradh  * Since this publishes @obj to userspace it must be fully set up by this point,
486677dec6eSriastradh  * drivers must call this last in their buffer object creation callbacks.
487a55ed7faSriastradh  */
drm_gem_handle_create(struct drm_file * file_priv,struct drm_gem_object * obj,u32 * handlep)4884e59feabSriastradh int drm_gem_handle_create(struct drm_file *file_priv,
489a55ed7faSriastradh 			  struct drm_gem_object *obj,
490a55ed7faSriastradh 			  u32 *handlep)
491a55ed7faSriastradh {
492a55ed7faSriastradh 	mutex_lock(&obj->dev->object_name_lock);
493a55ed7faSriastradh 
494a55ed7faSriastradh 	return drm_gem_handle_create_tail(file_priv, obj, handlep);
495a55ed7faSriastradh }
49656053ce7Sriastradh EXPORT_SYMBOL(drm_gem_handle_create);
49756053ce7Sriastradh 
49856053ce7Sriastradh 
49956053ce7Sriastradh /**
50056053ce7Sriastradh  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
50156053ce7Sriastradh  * @obj: obj in question
50256053ce7Sriastradh  *
50356053ce7Sriastradh  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
504677dec6eSriastradh  *
505677dec6eSriastradh  * Note that drm_gem_object_release() already calls this function, so drivers
506677dec6eSriastradh  * don't have to take care of releasing the mmap offset themselves when freeing
507677dec6eSriastradh  * the GEM object.
50856053ce7Sriastradh  */
50956053ce7Sriastradh void
drm_gem_free_mmap_offset(struct drm_gem_object * obj)51056053ce7Sriastradh drm_gem_free_mmap_offset(struct drm_gem_object *obj)
51156053ce7Sriastradh {
51256053ce7Sriastradh 	struct drm_device *dev = obj->dev;
51356053ce7Sriastradh 
514a55ed7faSriastradh 	drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
51556053ce7Sriastradh }
51656053ce7Sriastradh EXPORT_SYMBOL(drm_gem_free_mmap_offset);
51756053ce7Sriastradh 
51856053ce7Sriastradh /**
519a55ed7faSriastradh  * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
520a55ed7faSriastradh  * @obj: obj in question
521a55ed7faSriastradh  * @size: the virtual size
522a55ed7faSriastradh  *
523a55ed7faSriastradh  * GEM memory mapping works by handing back to userspace a fake mmap offset
524a55ed7faSriastradh  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
525a55ed7faSriastradh  * up the object based on the offset and sets up the various memory mapping
526a55ed7faSriastradh  * structures.
527a55ed7faSriastradh  *
528a55ed7faSriastradh  * This routine allocates and attaches a fake offset for @obj, in cases where
529677dec6eSriastradh  * the virtual size differs from the physical size (ie. &drm_gem_object.size).
530677dec6eSriastradh  * Otherwise just use drm_gem_create_mmap_offset().
531677dec6eSriastradh  *
532677dec6eSriastradh  * This function is idempotent and handles an already allocated mmap offset
533677dec6eSriastradh  * transparently. Drivers do not need to check for this case.
534a55ed7faSriastradh  */
535a55ed7faSriastradh int
drm_gem_create_mmap_offset_size(struct drm_gem_object * obj,size_t size)536a55ed7faSriastradh drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
537a55ed7faSriastradh {
538a55ed7faSriastradh 	struct drm_device *dev = obj->dev;
539a55ed7faSriastradh 
540a55ed7faSriastradh 	return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
541a55ed7faSriastradh 				  size / PAGE_SIZE);
542a55ed7faSriastradh }
543a55ed7faSriastradh EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
544a55ed7faSriastradh 
545a55ed7faSriastradh /**
54656053ce7Sriastradh  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
54756053ce7Sriastradh  * @obj: obj in question
54856053ce7Sriastradh  *
54956053ce7Sriastradh  * GEM memory mapping works by handing back to userspace a fake mmap offset
55056053ce7Sriastradh  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
55156053ce7Sriastradh  * up the object based on the offset and sets up the various memory mapping
55256053ce7Sriastradh  * structures.
55356053ce7Sriastradh  *
55456053ce7Sriastradh  * This routine allocates and attaches a fake offset for @obj.
555677dec6eSriastradh  *
556677dec6eSriastradh  * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
557677dec6eSriastradh  * the fake offset again.
55856053ce7Sriastradh  */
drm_gem_create_mmap_offset(struct drm_gem_object * obj)559a55ed7faSriastradh int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
56056053ce7Sriastradh {
561a55ed7faSriastradh 	return drm_gem_create_mmap_offset_size(obj, obj->size);
56256053ce7Sriastradh }
56356053ce7Sriastradh EXPORT_SYMBOL(drm_gem_create_mmap_offset);
56456053ce7Sriastradh 
565677dec6eSriastradh /*
566677dec6eSriastradh  * Move pages to appropriate lru and release the pagevec, decrementing the
567677dec6eSriastradh  * ref count of those pages.
568677dec6eSriastradh  */
56902e9661bSriastradh #ifndef __NetBSD__
drm_gem_check_release_pagevec(struct pagevec * pvec)570677dec6eSriastradh static void drm_gem_check_release_pagevec(struct pagevec *pvec)
571677dec6eSriastradh {
572677dec6eSriastradh 	check_move_unevictable_pages(pvec);
573677dec6eSriastradh 	__pagevec_release(pvec);
574677dec6eSriastradh 	cond_resched();
575677dec6eSriastradh }
57602e9661bSriastradh #endif
577677dec6eSriastradh 
578a55ed7faSriastradh /**
579a55ed7faSriastradh  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
580a55ed7faSriastradh  * from shmem
581a55ed7faSriastradh  * @obj: obj in question
5824e59feabSriastradh  *
5834e59feabSriastradh  * This reads the page-array of the shmem-backing storage of the given gem
5844e59feabSriastradh  * object. An array of pages is returned. If a page is not allocated or
5854e59feabSriastradh  * swapped-out, this will allocate/swap-in the required pages. Note that the
5864e59feabSriastradh  * whole object is covered by the page-array and pinned in memory.
5874e59feabSriastradh  *
5884e59feabSriastradh  * Use drm_gem_put_pages() to release the array and unpin all pages.
5894e59feabSriastradh  *
5904e59feabSriastradh  * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
5914e59feabSriastradh  * If you require other GFP-masks, you have to do those allocations yourself.
5924e59feabSriastradh  *
5934e59feabSriastradh  * Note that you are not allowed to change gfp-zones during runtime. That is,
5944e59feabSriastradh  * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
5954e59feabSriastradh  * set during initialization. If you have special zone constraints, set them
596677dec6eSriastradh  * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care
5974e59feabSriastradh  * to keep pages in the required zone during swap-in.
598a55ed7faSriastradh  */
599fceff80cSriastradh #ifdef __NetBSD__
600fceff80cSriastradh struct page **
drm_gem_get_pages(struct drm_gem_object * obj)6014e59feabSriastradh drm_gem_get_pages(struct drm_gem_object *obj)
602fceff80cSriastradh {
603fceff80cSriastradh 	struct vm_page *vm_page;
604fceff80cSriastradh 	struct page **pages;
605ad248fcfSriastradh 	unsigned i, npages;
606fceff80cSriastradh 	int ret;
607fceff80cSriastradh 
608fceff80cSriastradh 	KASSERT((obj->size & (PAGE_SIZE - 1)) != 0);
609fceff80cSriastradh 
610ad248fcfSriastradh 	npages = obj->size >> PAGE_SHIFT;
611ad248fcfSriastradh 	pages = kvmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
612fceff80cSriastradh 	if (pages == NULL) {
613fceff80cSriastradh 		ret = -ENOMEM;
614fceff80cSriastradh 		goto fail0;
615fceff80cSriastradh 	}
616fceff80cSriastradh 
617fceff80cSriastradh 	/* XXX errno NetBSD->Linux */
61866bc67baSriastradh 	ret = -uvm_obj_wirepages(obj->filp, 0, obj->size, NULL);
619fceff80cSriastradh 	if (ret)
620fceff80cSriastradh 		goto fail1;
621fceff80cSriastradh 
62266bc67baSriastradh 	rw_enter(obj->filp->vmobjlock, RW_READER);
62366bc67baSriastradh 	for (i = 0; i < npages; i++) {
62466bc67baSriastradh 		vm_page = uvm_pagelookup(obj->filp, ptoa(i));
62566bc67baSriastradh 		pages[i] = container_of(vm_page, struct page, p_vmp);
62666bc67baSriastradh 	}
62766bc67baSriastradh 	rw_exit(obj->filp->vmobjlock);
628fceff80cSriastradh 
629fceff80cSriastradh 	return pages;
630fceff80cSriastradh 
631ad248fcfSriastradh fail1:	kvfree(pages);
632fceff80cSriastradh fail0:	return ERR_PTR(ret);
633fceff80cSriastradh }
634fceff80cSriastradh #else
drm_gem_get_pages(struct drm_gem_object * obj)6354e59feabSriastradh struct page **drm_gem_get_pages(struct drm_gem_object *obj)
636a55ed7faSriastradh {
637a55ed7faSriastradh 	struct address_space *mapping;
638a55ed7faSriastradh 	struct page *p, **pages;
639677dec6eSriastradh 	struct pagevec pvec;
640a55ed7faSriastradh 	int i, npages;
641a55ed7faSriastradh 
642a55ed7faSriastradh 	/* This is the shared memory object that backs the GEM resource */
643677dec6eSriastradh 	mapping = obj->filp->f_mapping;
644a55ed7faSriastradh 
645a55ed7faSriastradh 	/* We already BUG_ON() for non-page-aligned sizes in
646a55ed7faSriastradh 	 * drm_gem_object_init(), so we should never hit this unless
647a55ed7faSriastradh 	 * driver author is doing something really wrong:
648a55ed7faSriastradh 	 */
649a55ed7faSriastradh 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
650a55ed7faSriastradh 
651a55ed7faSriastradh 	npages = obj->size >> PAGE_SHIFT;
652a55ed7faSriastradh 
653677dec6eSriastradh 	pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
654a55ed7faSriastradh 	if (pages == NULL)
655a55ed7faSriastradh 		return ERR_PTR(-ENOMEM);
656a55ed7faSriastradh 
657677dec6eSriastradh 	mapping_set_unevictable(mapping);
658677dec6eSriastradh 
659a55ed7faSriastradh 	for (i = 0; i < npages; i++) {
6604e59feabSriastradh 		p = shmem_read_mapping_page(mapping, i);
661a55ed7faSriastradh 		if (IS_ERR(p))
662a55ed7faSriastradh 			goto fail;
663a55ed7faSriastradh 		pages[i] = p;
664a55ed7faSriastradh 
6654e59feabSriastradh 		/* Make sure shmem keeps __GFP_DMA32 allocated pages in the
6664e59feabSriastradh 		 * correct region during swapin. Note that this requires
6674e59feabSriastradh 		 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
6684e59feabSriastradh 		 * so shmem can relocate pages during swapin if required.
669a55ed7faSriastradh 		 */
6704e59feabSriastradh 		BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
671a55ed7faSriastradh 				(page_to_pfn(p) >= 0x00100000UL));
672a55ed7faSriastradh 	}
673a55ed7faSriastradh 
674a55ed7faSriastradh 	return pages;
675a55ed7faSriastradh 
676a55ed7faSriastradh fail:
677677dec6eSriastradh 	mapping_clear_unevictable(mapping);
678677dec6eSriastradh 	pagevec_init(&pvec);
679677dec6eSriastradh 	while (i--) {
680677dec6eSriastradh 		if (!pagevec_add(&pvec, pages[i]))
681677dec6eSriastradh 			drm_gem_check_release_pagevec(&pvec);
682677dec6eSriastradh 	}
683677dec6eSriastradh 	if (pagevec_count(&pvec))
684677dec6eSriastradh 		drm_gem_check_release_pagevec(&pvec);
685a55ed7faSriastradh 
686677dec6eSriastradh 	kvfree(pages);
687a55ed7faSriastradh 	return ERR_CAST(p);
688a55ed7faSriastradh }
689fceff80cSriastradh #endif
690a55ed7faSriastradh EXPORT_SYMBOL(drm_gem_get_pages);
691a55ed7faSriastradh 
692a55ed7faSriastradh /**
693a55ed7faSriastradh  * drm_gem_put_pages - helper to free backing pages for a GEM object
694a55ed7faSriastradh  * @obj: obj in question
695a55ed7faSriastradh  * @pages: pages to free
696a55ed7faSriastradh  * @dirty: if true, pages will be marked as dirty
697a55ed7faSriastradh  * @accessed: if true, the pages will be marked as accessed
698a55ed7faSriastradh  */
699fceff80cSriastradh #ifdef __NetBSD__
700fceff80cSriastradh void
drm_gem_put_pages(struct drm_gem_object * obj,struct page ** pages,bool dirty,bool accessed __unused)701fceff80cSriastradh drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, bool dirty,
702fceff80cSriastradh     bool accessed __unused /* XXX */)
703fceff80cSriastradh {
704fceff80cSriastradh 	unsigned i;
705fceff80cSriastradh 
706fceff80cSriastradh 	for (i = 0; i < (obj->size >> PAGE_SHIFT); i++) {
707943ff07eSad 		if (dirty) {
708b4dac182Sad 			rw_enter(obj->filp->vmobjlock, RW_WRITER);
709943ff07eSad 			uvm_pagemarkdirty(&pages[i]->p_vmp,
710943ff07eSad 			    UVM_PAGE_STATUS_DIRTY);
711b4dac182Sad 			rw_exit(obj->filp->vmobjlock);
712943ff07eSad 		}
713fceff80cSriastradh 	}
714fceff80cSriastradh 
7154f3c69ffSriastradh 	uvm_obj_unwirepages(obj->filp, 0, obj->size);
716fceff80cSriastradh }
717fceff80cSriastradh #else
drm_gem_put_pages(struct drm_gem_object * obj,struct page ** pages,bool dirty,bool accessed)718a55ed7faSriastradh void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
719a55ed7faSriastradh 		bool dirty, bool accessed)
720a55ed7faSriastradh {
721a55ed7faSriastradh 	int i, npages;
722677dec6eSriastradh 	struct address_space *mapping;
723677dec6eSriastradh 	struct pagevec pvec;
724677dec6eSriastradh 
725677dec6eSriastradh 	mapping = file_inode(obj->filp)->i_mapping;
726677dec6eSriastradh 	mapping_clear_unevictable(mapping);
727a55ed7faSriastradh 
728a55ed7faSriastradh 	/* We already BUG_ON() for non-page-aligned sizes in
729a55ed7faSriastradh 	 * drm_gem_object_init(), so we should never hit this unless
730a55ed7faSriastradh 	 * driver author is doing something really wrong:
731a55ed7faSriastradh 	 */
732a55ed7faSriastradh 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
733a55ed7faSriastradh 
734a55ed7faSriastradh 	npages = obj->size >> PAGE_SHIFT;
735a55ed7faSriastradh 
736677dec6eSriastradh 	pagevec_init(&pvec);
737a55ed7faSriastradh 	for (i = 0; i < npages; i++) {
738677dec6eSriastradh 		if (!pages[i])
739677dec6eSriastradh 			continue;
740677dec6eSriastradh 
741a55ed7faSriastradh 		if (dirty)
742a55ed7faSriastradh 			set_page_dirty(pages[i]);
743a55ed7faSriastradh 
744a55ed7faSriastradh 		if (accessed)
745a55ed7faSriastradh 			mark_page_accessed(pages[i]);
746a55ed7faSriastradh 
747a55ed7faSriastradh 		/* Undo the reference we took when populating the table */
748677dec6eSriastradh 		if (!pagevec_add(&pvec, pages[i]))
749677dec6eSriastradh 			drm_gem_check_release_pagevec(&pvec);
750a55ed7faSriastradh 	}
751677dec6eSriastradh 	if (pagevec_count(&pvec))
752677dec6eSriastradh 		drm_gem_check_release_pagevec(&pvec);
753a55ed7faSriastradh 
754677dec6eSriastradh 	kvfree(pages);
755a55ed7faSriastradh }
756fceff80cSriastradh #endif
757a55ed7faSriastradh EXPORT_SYMBOL(drm_gem_put_pages);
758a55ed7faSriastradh 
objects_lookup(struct drm_file * filp,u32 * handle,int count,struct drm_gem_object ** objs)759677dec6eSriastradh static int objects_lookup(struct drm_file *filp, u32 *handle, int count,
760677dec6eSriastradh 			  struct drm_gem_object **objs)
76156053ce7Sriastradh {
762677dec6eSriastradh 	int i, ret = 0;
76356053ce7Sriastradh 	struct drm_gem_object *obj;
76456053ce7Sriastradh 
76556053ce7Sriastradh 	spin_lock(&filp->table_lock);
76656053ce7Sriastradh 
767677dec6eSriastradh 	for (i = 0; i < count; i++) {
76856053ce7Sriastradh 		/* Check if we currently have a reference on the object */
769677dec6eSriastradh 		obj = idr_find(&filp->object_idr, handle[i]);
770677dec6eSriastradh 		if (!obj) {
771677dec6eSriastradh 			ret = -ENOENT;
772677dec6eSriastradh 			break;
773677dec6eSriastradh 		}
774677dec6eSriastradh 		drm_gem_object_get(obj);
775677dec6eSriastradh 		objs[i] = obj;
776677dec6eSriastradh 	}
77756053ce7Sriastradh 	spin_unlock(&filp->table_lock);
778677dec6eSriastradh 
779677dec6eSriastradh 	return ret;
78056053ce7Sriastradh }
78156053ce7Sriastradh 
782677dec6eSriastradh /**
783677dec6eSriastradh  * drm_gem_objects_lookup - look up GEM objects from an array of handles
784677dec6eSriastradh  * @filp: DRM file private date
785677dec6eSriastradh  * @bo_handles: user pointer to array of userspace handle
786677dec6eSriastradh  * @count: size of handle array
787677dec6eSriastradh  * @objs_out: returned pointer to array of drm_gem_object pointers
788677dec6eSriastradh  *
789677dec6eSriastradh  * Takes an array of userspace handles and returns a newly allocated array of
790677dec6eSriastradh  * GEM objects.
791677dec6eSriastradh  *
792677dec6eSriastradh  * For a single handle lookup, use drm_gem_object_lookup().
793677dec6eSriastradh  *
794677dec6eSriastradh  * Returns:
795677dec6eSriastradh  *
796677dec6eSriastradh  * @objs filled in with GEM object pointers. Returned GEM objects need to be
797677dec6eSriastradh  * released with drm_gem_object_put(). -ENOENT is returned on a lookup
798677dec6eSriastradh  * failure. 0 is returned on success.
799677dec6eSriastradh  *
800677dec6eSriastradh  */
drm_gem_objects_lookup(struct drm_file * filp,void __user * bo_handles,int count,struct drm_gem_object *** objs_out)801677dec6eSriastradh int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
802677dec6eSriastradh 			   int count, struct drm_gem_object ***objs_out)
803677dec6eSriastradh {
804677dec6eSriastradh 	int ret;
805677dec6eSriastradh 	u32 *handles;
806677dec6eSriastradh 	struct drm_gem_object **objs;
80756053ce7Sriastradh 
808677dec6eSriastradh 	if (!count)
809677dec6eSriastradh 		return 0;
81056053ce7Sriastradh 
811677dec6eSriastradh 	objs = kvmalloc_array(count, sizeof(struct drm_gem_object *),
812677dec6eSriastradh 			     GFP_KERNEL | __GFP_ZERO);
813677dec6eSriastradh 	if (!objs)
814677dec6eSriastradh 		return -ENOMEM;
815677dec6eSriastradh 
816677dec6eSriastradh 	handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
817677dec6eSriastradh 	if (!handles) {
818677dec6eSriastradh 		ret = -ENOMEM;
819677dec6eSriastradh 		goto out;
820677dec6eSriastradh 	}
821677dec6eSriastradh 
822677dec6eSriastradh 	if (copy_from_user(handles, bo_handles, count * sizeof(u32))) {
823677dec6eSriastradh 		ret = -EFAULT;
824677dec6eSriastradh 		DRM_DEBUG("Failed to copy in GEM handles\n");
825677dec6eSriastradh 		goto out;
826677dec6eSriastradh 	}
827677dec6eSriastradh 
828677dec6eSriastradh 	ret = objects_lookup(filp, handles, count, objs);
829677dec6eSriastradh 	*objs_out = objs;
830677dec6eSriastradh 
831677dec6eSriastradh out:
832677dec6eSriastradh 	kvfree(handles);
833677dec6eSriastradh 	return ret;
834677dec6eSriastradh 
835677dec6eSriastradh }
836677dec6eSriastradh EXPORT_SYMBOL(drm_gem_objects_lookup);
837677dec6eSriastradh 
838677dec6eSriastradh /**
839677dec6eSriastradh  * drm_gem_object_lookup - look up a GEM object from its handle
840677dec6eSriastradh  * @filp: DRM file private date
841677dec6eSriastradh  * @handle: userspace handle
842677dec6eSriastradh  *
843677dec6eSriastradh  * Returns:
844677dec6eSriastradh  *
845677dec6eSriastradh  * A reference to the object named by the handle if such exists on @filp, NULL
846677dec6eSriastradh  * otherwise.
847677dec6eSriastradh  *
848677dec6eSriastradh  * If looking up an array of handles, use drm_gem_objects_lookup().
849677dec6eSriastradh  */
850677dec6eSriastradh struct drm_gem_object *
drm_gem_object_lookup(struct drm_file * filp,u32 handle)851677dec6eSriastradh drm_gem_object_lookup(struct drm_file *filp, u32 handle)
852677dec6eSriastradh {
853677dec6eSriastradh 	struct drm_gem_object *obj = NULL;
854677dec6eSriastradh 
855677dec6eSriastradh 	objects_lookup(filp, &handle, 1, &obj);
85656053ce7Sriastradh 	return obj;
85756053ce7Sriastradh }
85856053ce7Sriastradh EXPORT_SYMBOL(drm_gem_object_lookup);
85956053ce7Sriastradh 
86056053ce7Sriastradh /**
861677dec6eSriastradh  * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects
862677dec6eSriastradh  * shared and/or exclusive fences.
863677dec6eSriastradh  * @filep: DRM file private date
864677dec6eSriastradh  * @handle: userspace handle
865677dec6eSriastradh  * @wait_all: if true, wait on all fences, else wait on just exclusive fence
866677dec6eSriastradh  * @timeout: timeout value in jiffies or zero to return immediately
867677dec6eSriastradh  *
868677dec6eSriastradh  * Returns:
869677dec6eSriastradh  *
870677dec6eSriastradh  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
871677dec6eSriastradh  * greater than 0 on success.
872677dec6eSriastradh  */
drm_gem_dma_resv_wait(struct drm_file * filep,u32 handle,bool wait_all,unsigned long timeout)873677dec6eSriastradh long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
874677dec6eSriastradh 				    bool wait_all, unsigned long timeout)
875677dec6eSriastradh {
876677dec6eSriastradh 	long ret;
877677dec6eSriastradh 	struct drm_gem_object *obj;
878677dec6eSriastradh 
879677dec6eSriastradh 	obj = drm_gem_object_lookup(filep, handle);
880677dec6eSriastradh 	if (!obj) {
881677dec6eSriastradh 		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
882677dec6eSriastradh 		return -EINVAL;
883677dec6eSriastradh 	}
884677dec6eSriastradh 
885677dec6eSriastradh 	ret = dma_resv_wait_timeout_rcu(obj->resv, wait_all,
886677dec6eSriastradh 						  true, timeout);
887677dec6eSriastradh 	if (ret == 0)
888677dec6eSriastradh 		ret = -ETIME;
889677dec6eSriastradh 	else if (ret > 0)
890677dec6eSriastradh 		ret = 0;
891677dec6eSriastradh 
892677dec6eSriastradh 	drm_gem_object_put_unlocked(obj);
893677dec6eSriastradh 
894677dec6eSriastradh 	return ret;
895677dec6eSriastradh }
896677dec6eSriastradh EXPORT_SYMBOL(drm_gem_dma_resv_wait);
897677dec6eSriastradh 
898677dec6eSriastradh /**
899a55ed7faSriastradh  * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
900a55ed7faSriastradh  * @dev: drm_device
901a55ed7faSriastradh  * @data: ioctl data
902a55ed7faSriastradh  * @file_priv: drm file-private structure
903a55ed7faSriastradh  *
90456053ce7Sriastradh  * Releases the handle to an mm object.
90556053ce7Sriastradh  */
90656053ce7Sriastradh int
drm_gem_close_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)90756053ce7Sriastradh drm_gem_close_ioctl(struct drm_device *dev, void *data,
90856053ce7Sriastradh 		    struct drm_file *file_priv)
90956053ce7Sriastradh {
91056053ce7Sriastradh 	struct drm_gem_close *args = data;
91156053ce7Sriastradh 	int ret;
91256053ce7Sriastradh 
9134e59feabSriastradh 	if (!drm_core_check_feature(dev, DRIVER_GEM))
914677dec6eSriastradh 		return -EOPNOTSUPP;
91556053ce7Sriastradh 
91656053ce7Sriastradh 	ret = drm_gem_handle_delete(file_priv, args->handle);
91756053ce7Sriastradh 
91856053ce7Sriastradh 	return ret;
91956053ce7Sriastradh }
92056053ce7Sriastradh 
92156053ce7Sriastradh /**
922a55ed7faSriastradh  * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
923a55ed7faSriastradh  * @dev: drm_device
924a55ed7faSriastradh  * @data: ioctl data
925a55ed7faSriastradh  * @file_priv: drm file-private structure
926a55ed7faSriastradh  *
92756053ce7Sriastradh  * Create a global name for an object, returning the name.
92856053ce7Sriastradh  *
92956053ce7Sriastradh  * Note that the name does not hold a reference; when the object
93056053ce7Sriastradh  * is freed, the name goes away.
93156053ce7Sriastradh  */
93256053ce7Sriastradh int
drm_gem_flink_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)93356053ce7Sriastradh drm_gem_flink_ioctl(struct drm_device *dev, void *data,
93456053ce7Sriastradh 		    struct drm_file *file_priv)
93556053ce7Sriastradh {
93656053ce7Sriastradh 	struct drm_gem_flink *args = data;
93756053ce7Sriastradh 	struct drm_gem_object *obj;
93856053ce7Sriastradh 	int ret;
93956053ce7Sriastradh 
9404e59feabSriastradh 	if (!drm_core_check_feature(dev, DRIVER_GEM))
941677dec6eSriastradh 		return -EOPNOTSUPP;
94256053ce7Sriastradh 
943677dec6eSriastradh 	obj = drm_gem_object_lookup(file_priv, args->handle);
94456053ce7Sriastradh 	if (obj == NULL)
94556053ce7Sriastradh 		return -ENOENT;
94656053ce7Sriastradh 
947a55ed7faSriastradh 	idr_preload(GFP_KERNEL);
948591ab17bSriastradh 	mutex_lock(&dev->object_name_lock);
949a55ed7faSriastradh 	/* prevent races with concurrent gem_close. */
950a55ed7faSriastradh 	if (obj->handle_count == 0) {
951a55ed7faSriastradh 		ret = -ENOENT;
95256053ce7Sriastradh 		goto err;
95356053ce7Sriastradh 	}
95456053ce7Sriastradh 
95556053ce7Sriastradh 	if (!obj->name) {
956677dec6eSriastradh 		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
957a55ed7faSriastradh 		if (ret < 0)
95856053ce7Sriastradh 			goto err;
95956053ce7Sriastradh 
960a55ed7faSriastradh 		obj->name = ret;
96156053ce7Sriastradh 	}
96256053ce7Sriastradh 
963a55ed7faSriastradh 	args->name = (uint64_t) obj->name;
964a55ed7faSriastradh 	ret = 0;
965a55ed7faSriastradh 
96656053ce7Sriastradh err:
967a55ed7faSriastradh 	mutex_unlock(&dev->object_name_lock);
968591ab17bSriastradh 	idr_preload_end();
969677dec6eSriastradh 	drm_gem_object_put_unlocked(obj);
97056053ce7Sriastradh 	return ret;
97156053ce7Sriastradh }
97256053ce7Sriastradh 
97356053ce7Sriastradh /**
974a55ed7faSriastradh  * drm_gem_open - implementation of the GEM_OPEN ioctl
975a55ed7faSriastradh  * @dev: drm_device
976a55ed7faSriastradh  * @data: ioctl data
977a55ed7faSriastradh  * @file_priv: drm file-private structure
978a55ed7faSriastradh  *
97956053ce7Sriastradh  * Open an object using the global name, returning a handle and the size.
98056053ce7Sriastradh  *
98156053ce7Sriastradh  * This handle (of course) holds a reference to the object, so the object
98256053ce7Sriastradh  * will not go away until the handle is deleted.
98356053ce7Sriastradh  */
98456053ce7Sriastradh int
drm_gem_open_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)98556053ce7Sriastradh drm_gem_open_ioctl(struct drm_device *dev, void *data,
98656053ce7Sriastradh 		   struct drm_file *file_priv)
98756053ce7Sriastradh {
98856053ce7Sriastradh 	struct drm_gem_open *args = data;
98956053ce7Sriastradh 	struct drm_gem_object *obj;
99056053ce7Sriastradh 	int ret;
99156053ce7Sriastradh 	u32 handle;
99256053ce7Sriastradh 
9934e59feabSriastradh 	if (!drm_core_check_feature(dev, DRIVER_GEM))
994677dec6eSriastradh 		return -EOPNOTSUPP;
99556053ce7Sriastradh 
996a55ed7faSriastradh 	mutex_lock(&dev->object_name_lock);
99756053ce7Sriastradh 	obj = idr_find(&dev->object_name_idr, (int) args->name);
998a55ed7faSriastradh 	if (obj) {
999677dec6eSriastradh 		drm_gem_object_get(obj);
1000a55ed7faSriastradh 	} else {
1001a55ed7faSriastradh 		mutex_unlock(&dev->object_name_lock);
100256053ce7Sriastradh 		return -ENOENT;
1003a55ed7faSriastradh 	}
100456053ce7Sriastradh 
1005a55ed7faSriastradh 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
1006a55ed7faSriastradh 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
1007677dec6eSriastradh 	drm_gem_object_put_unlocked(obj);
100856053ce7Sriastradh 	if (ret)
100956053ce7Sriastradh 		return ret;
101056053ce7Sriastradh 
101156053ce7Sriastradh 	args->handle = handle;
101256053ce7Sriastradh 	args->size = obj->size;
101356053ce7Sriastradh 
101456053ce7Sriastradh 	return 0;
101556053ce7Sriastradh }
101656053ce7Sriastradh 
101756053ce7Sriastradh /**
1018a55ed7faSriastradh  * gem_gem_open - initalizes GEM file-private structures at devnode open time
1019a55ed7faSriastradh  * @dev: drm_device which is being opened by userspace
1020a55ed7faSriastradh  * @file_private: drm file-private structure to set up
1021a55ed7faSriastradh  *
102256053ce7Sriastradh  * Called at device open time, sets up the structure for handling refcounting
102356053ce7Sriastradh  * of mm objects.
102456053ce7Sriastradh  */
102556053ce7Sriastradh void
drm_gem_open(struct drm_device * dev,struct drm_file * file_private)102656053ce7Sriastradh drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
102756053ce7Sriastradh {
1028677dec6eSriastradh 	idr_init_base(&file_private->object_idr, 1);
102956053ce7Sriastradh 	spin_lock_init(&file_private->table_lock);
103056053ce7Sriastradh }
103156053ce7Sriastradh 
103256053ce7Sriastradh /**
1033a55ed7faSriastradh  * drm_gem_release - release file-private GEM resources
1034a55ed7faSriastradh  * @dev: drm_device which is being closed by userspace
1035a55ed7faSriastradh  * @file_private: drm file-private structure to clean up
1036a55ed7faSriastradh  *
103756053ce7Sriastradh  * Called at close time when the filp is going away.
103856053ce7Sriastradh  *
103956053ce7Sriastradh  * Releases any remaining references on objects by this filp.
104056053ce7Sriastradh  */
104156053ce7Sriastradh void
drm_gem_release(struct drm_device * dev,struct drm_file * file_private)104256053ce7Sriastradh drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
104356053ce7Sriastradh {
104456053ce7Sriastradh 	idr_for_each(&file_private->object_idr,
104556053ce7Sriastradh 		     &drm_gem_object_release_handle, file_private);
104656053ce7Sriastradh 	idr_destroy(&file_private->object_idr);
10472adb3a73Sriastradh #ifdef __NetBSD__
10482adb3a73Sriastradh 	spin_lock_destroy(&file_private->table_lock);
10492adb3a73Sriastradh #endif
105056053ce7Sriastradh }
105156053ce7Sriastradh 
1052677dec6eSriastradh /**
1053677dec6eSriastradh  * drm_gem_object_release - release GEM buffer object resources
1054677dec6eSriastradh  * @obj: GEM buffer object
1055677dec6eSriastradh  *
1056677dec6eSriastradh  * This releases any structures and resources used by @obj and is the invers of
1057677dec6eSriastradh  * drm_gem_object_init().
1058677dec6eSriastradh  */
105956053ce7Sriastradh void
drm_gem_object_release(struct drm_gem_object * obj)106056053ce7Sriastradh drm_gem_object_release(struct drm_gem_object *obj)
106156053ce7Sriastradh {
1062fceff80cSriastradh #ifndef __NetBSD__
1063a55ed7faSriastradh 	WARN_ON(obj->dma_buf);
1064fceff80cSriastradh #endif
1065a55ed7faSriastradh 
10662adb3a73Sriastradh #ifdef __NetBSD__
10674f3c69ffSriastradh 	if (obj->filp)
10684f3c69ffSriastradh 		uao_detach(obj->filp);
1069491755bdSriastradh 	uvm_obj_destroy(&obj->gemo_uvmobj, /*free lock*/true);
10702adb3a73Sriastradh #else
107156053ce7Sriastradh 	if (obj->filp)
107256053ce7Sriastradh 		fput(obj->filp);
10732adb3a73Sriastradh #endif
1074a55ed7faSriastradh 
1075677dec6eSriastradh 	dma_resv_fini(&obj->_resv);
1076a55ed7faSriastradh 	drm_gem_free_mmap_offset(obj);
1077*f411b747Sriastradh #ifdef __NetBSD__
1078*f411b747Sriastradh 	drm_vma_node_destroy(&obj->vma_node);
1079*f411b747Sriastradh #endif
108056053ce7Sriastradh }
108156053ce7Sriastradh EXPORT_SYMBOL(drm_gem_object_release);
108256053ce7Sriastradh 
108356053ce7Sriastradh /**
1084a55ed7faSriastradh  * drm_gem_object_free - free a GEM object
1085a55ed7faSriastradh  * @kref: kref of the object to free
1086a55ed7faSriastradh  *
108756053ce7Sriastradh  * Called after the last reference to the object has been lost.
1088677dec6eSriastradh  * Must be called holding &drm_device.struct_mutex.
108956053ce7Sriastradh  *
109056053ce7Sriastradh  * Frees the object
109156053ce7Sriastradh  */
109256053ce7Sriastradh void
drm_gem_object_free(struct kref * kref)109356053ce7Sriastradh drm_gem_object_free(struct kref *kref)
109456053ce7Sriastradh {
10954e59feabSriastradh 	struct drm_gem_object *obj =
10964e59feabSriastradh 		container_of(kref, struct drm_gem_object, refcount);
109756053ce7Sriastradh 	struct drm_device *dev = obj->dev;
109856053ce7Sriastradh 
1099677dec6eSriastradh 	if (obj->funcs) {
1100677dec6eSriastradh 		obj->funcs->free(obj);
1101677dec6eSriastradh 	} else if (dev->driver->gem_free_object_unlocked) {
1102677dec6eSriastradh 		dev->driver->gem_free_object_unlocked(obj);
1103677dec6eSriastradh 	} else if (dev->driver->gem_free_object) {
11044e59feabSriastradh 		WARN_ON(!mutex_is_locked(&dev->struct_mutex));
110556053ce7Sriastradh 
110656053ce7Sriastradh 		dev->driver->gem_free_object(obj);
110756053ce7Sriastradh 	}
1108677dec6eSriastradh }
110956053ce7Sriastradh EXPORT_SYMBOL(drm_gem_object_free);
111056053ce7Sriastradh 
1111677dec6eSriastradh /**
1112677dec6eSriastradh  * drm_gem_object_put_unlocked - drop a GEM buffer object reference
1113677dec6eSriastradh  * @obj: GEM buffer object
1114677dec6eSriastradh  *
1115677dec6eSriastradh  * This releases a reference to @obj. Callers must not hold the
1116677dec6eSriastradh  * &drm_device.struct_mutex lock when calling this function.
1117677dec6eSriastradh  *
1118677dec6eSriastradh  * See also __drm_gem_object_put().
1119677dec6eSriastradh  */
1120677dec6eSriastradh void
drm_gem_object_put_unlocked(struct drm_gem_object * obj)1121677dec6eSriastradh drm_gem_object_put_unlocked(struct drm_gem_object *obj)
1122677dec6eSriastradh {
1123677dec6eSriastradh 	struct drm_device *dev;
11242adb3a73Sriastradh 
1125677dec6eSriastradh 	if (!obj)
1126677dec6eSriastradh 		return;
1127677dec6eSriastradh 
1128677dec6eSriastradh 	dev = obj->dev;
1129677dec6eSriastradh 
1130677dec6eSriastradh 	if (dev->driver->gem_free_object) {
1131677dec6eSriastradh 		might_lock(&dev->struct_mutex);
1132677dec6eSriastradh 		if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
1133677dec6eSriastradh 				&dev->struct_mutex))
1134677dec6eSriastradh 			mutex_unlock(&dev->struct_mutex);
1135677dec6eSriastradh 	} else {
1136677dec6eSriastradh 		kref_put(&obj->refcount, drm_gem_object_free);
1137677dec6eSriastradh 	}
1138677dec6eSriastradh }
1139677dec6eSriastradh EXPORT_SYMBOL(drm_gem_object_put_unlocked);
1140677dec6eSriastradh 
1141677dec6eSriastradh /**
1142677dec6eSriastradh  * drm_gem_object_put - release a GEM buffer object reference
1143677dec6eSriastradh  * @obj: GEM buffer object
1144677dec6eSriastradh  *
1145677dec6eSriastradh  * This releases a reference to @obj. Callers must hold the
1146677dec6eSriastradh  * &drm_device.struct_mutex lock when calling this function, even when the
1147677dec6eSriastradh  * driver doesn't use &drm_device.struct_mutex for anything.
1148677dec6eSriastradh  *
1149677dec6eSriastradh  * For drivers not encumbered with legacy locking use
1150677dec6eSriastradh  * drm_gem_object_put_unlocked() instead.
1151677dec6eSriastradh  */
1152677dec6eSriastradh void
drm_gem_object_put(struct drm_gem_object * obj)1153677dec6eSriastradh drm_gem_object_put(struct drm_gem_object *obj)
1154677dec6eSriastradh {
1155677dec6eSriastradh 	if (obj) {
1156677dec6eSriastradh 		WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
1157677dec6eSriastradh 
1158677dec6eSriastradh 		kref_put(&obj->refcount, drm_gem_object_free);
1159677dec6eSriastradh 	}
1160677dec6eSriastradh }
1161677dec6eSriastradh EXPORT_SYMBOL(drm_gem_object_put);
1162677dec6eSriastradh 
1163677dec6eSriastradh #ifndef __NetBSD__
1164677dec6eSriastradh /**
1165677dec6eSriastradh  * drm_gem_vm_open - vma->ops->open implementation for GEM
1166677dec6eSriastradh  * @vma: VM area structure
1167677dec6eSriastradh  *
1168677dec6eSriastradh  * This function implements the #vm_operations_struct open() callback for GEM
1169677dec6eSriastradh  * drivers. This must be used together with drm_gem_vm_close().
1170677dec6eSriastradh  */
drm_gem_vm_open(struct vm_area_struct * vma)117156053ce7Sriastradh void drm_gem_vm_open(struct vm_area_struct *vma)
117256053ce7Sriastradh {
117356053ce7Sriastradh 	struct drm_gem_object *obj = vma->vm_private_data;
117456053ce7Sriastradh 
1175677dec6eSriastradh 	drm_gem_object_get(obj);
117656053ce7Sriastradh }
117756053ce7Sriastradh EXPORT_SYMBOL(drm_gem_vm_open);
117856053ce7Sriastradh 
1179677dec6eSriastradh /**
1180677dec6eSriastradh  * drm_gem_vm_close - vma->ops->close implementation for GEM
1181677dec6eSriastradh  * @vma: VM area structure
1182677dec6eSriastradh  *
1183677dec6eSriastradh  * This function implements the #vm_operations_struct close() callback for GEM
1184677dec6eSriastradh  * drivers. This must be used together with drm_gem_vm_open().
1185677dec6eSriastradh  */
drm_gem_vm_close(struct vm_area_struct * vma)118656053ce7Sriastradh void drm_gem_vm_close(struct vm_area_struct *vma)
118756053ce7Sriastradh {
118856053ce7Sriastradh 	struct drm_gem_object *obj = vma->vm_private_data;
118956053ce7Sriastradh 
1190677dec6eSriastradh 	drm_gem_object_put_unlocked(obj);
119156053ce7Sriastradh }
119256053ce7Sriastradh EXPORT_SYMBOL(drm_gem_vm_close);
119356053ce7Sriastradh 
119456053ce7Sriastradh /**
1195a55ed7faSriastradh  * drm_gem_mmap_obj - memory map a GEM object
1196a55ed7faSriastradh  * @obj: the GEM object to map
1197a55ed7faSriastradh  * @obj_size: the object size to be mapped, in bytes
119856053ce7Sriastradh  * @vma: VMA for the area to be mapped
119956053ce7Sriastradh  *
1200a55ed7faSriastradh  * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
1201a55ed7faSriastradh  * provided by the driver. Depending on their requirements, drivers can either
1202a55ed7faSriastradh  * provide a fault handler in their gem_vm_ops (in which case any accesses to
1203a55ed7faSriastradh  * the object will be trapped, to perform migration, GTT binding, surface
1204a55ed7faSriastradh  * register allocation, or performance monitoring), or mmap the buffer memory
1205a55ed7faSriastradh  * synchronously after calling drm_gem_mmap_obj.
120656053ce7Sriastradh  *
1207a55ed7faSriastradh  * This function is mainly intended to implement the DMABUF mmap operation, when
1208a55ed7faSriastradh  * the GEM object is not looked up based on its fake offset. To implement the
1209a55ed7faSriastradh  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
1210a55ed7faSriastradh  *
1211a55ed7faSriastradh  * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
1212a55ed7faSriastradh  * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
1213a55ed7faSriastradh  * callers must verify access restrictions before calling this helper.
1214a55ed7faSriastradh  *
1215a55ed7faSriastradh  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
1216a55ed7faSriastradh  * size, or if no gem_vm_ops are provided.
121756053ce7Sriastradh  */
drm_gem_mmap_obj(struct drm_gem_object * obj,unsigned long obj_size,struct vm_area_struct * vma)1218a55ed7faSriastradh int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
1219a55ed7faSriastradh 		     struct vm_area_struct *vma)
122056053ce7Sriastradh {
1221a55ed7faSriastradh 	struct drm_device *dev = obj->dev;
1222677dec6eSriastradh 	int ret;
122356053ce7Sriastradh 
122456053ce7Sriastradh 	/* Check for valid size. */
1225a55ed7faSriastradh 	if (obj_size < vma->vm_end - vma->vm_start)
1226a55ed7faSriastradh 		return -EINVAL;
122756053ce7Sriastradh 
122856053ce7Sriastradh 	/* Take a ref for this mapping of the object, so that the fault
122956053ce7Sriastradh 	 * handler can dereference the mmap offset's pointer to the object.
123056053ce7Sriastradh 	 * This reference is cleaned up by the corresponding vm_close
123156053ce7Sriastradh 	 * (which should happen whether the vma was created by this call, or
123256053ce7Sriastradh 	 * by a vm_open due to mremap or partial unmap or whatever).
123356053ce7Sriastradh 	 */
1234677dec6eSriastradh 	drm_gem_object_get(obj);
1235677dec6eSriastradh 
1236677dec6eSriastradh 	if (obj->funcs && obj->funcs->mmap) {
1237677dec6eSriastradh 		ret = obj->funcs->mmap(obj, vma);
1238677dec6eSriastradh 		if (ret) {
1239677dec6eSriastradh 			drm_gem_object_put_unlocked(obj);
1240677dec6eSriastradh 			return ret;
1241677dec6eSriastradh 		}
1242677dec6eSriastradh 		WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
1243677dec6eSriastradh 	} else {
1244677dec6eSriastradh 		if (obj->funcs && obj->funcs->vm_ops)
1245677dec6eSriastradh 			vma->vm_ops = obj->funcs->vm_ops;
1246677dec6eSriastradh 		else if (dev->driver->gem_vm_ops)
1247677dec6eSriastradh 			vma->vm_ops = dev->driver->gem_vm_ops;
1248677dec6eSriastradh 		else {
1249677dec6eSriastradh 			drm_gem_object_put_unlocked(obj);
1250677dec6eSriastradh 			return -EINVAL;
1251677dec6eSriastradh 		}
1252677dec6eSriastradh 
1253677dec6eSriastradh 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1254677dec6eSriastradh 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1255677dec6eSriastradh 		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1256677dec6eSriastradh 	}
1257677dec6eSriastradh 
1258677dec6eSriastradh 	vma->vm_private_data = obj;
125956053ce7Sriastradh 
1260a55ed7faSriastradh 	return 0;
1261a55ed7faSriastradh }
1262a55ed7faSriastradh EXPORT_SYMBOL(drm_gem_mmap_obj);
126356053ce7Sriastradh 
1264a55ed7faSriastradh /**
1265a55ed7faSriastradh  * drm_gem_mmap - memory map routine for GEM objects
1266a55ed7faSriastradh  * @filp: DRM file pointer
1267a55ed7faSriastradh  * @vma: VMA for the area to be mapped
1268a55ed7faSriastradh  *
1269a55ed7faSriastradh  * If a driver supports GEM object mapping, mmap calls on the DRM file
1270a55ed7faSriastradh  * descriptor will end up here.
1271a55ed7faSriastradh  *
1272a55ed7faSriastradh  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
1273a55ed7faSriastradh  * contain the fake offset we created when the GTT map ioctl was called on
1274a55ed7faSriastradh  * the object) and map it with a call to drm_gem_mmap_obj().
1275a55ed7faSriastradh  *
1276a55ed7faSriastradh  * If the caller is not granted access to the buffer object, the mmap will fail
1277a55ed7faSriastradh  * with EACCES. Please see the vma manager for more information.
1278a55ed7faSriastradh  */
drm_gem_mmap(struct file * filp,struct vm_area_struct * vma)1279a55ed7faSriastradh int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
1280a55ed7faSriastradh {
1281a55ed7faSriastradh 	struct drm_file *priv = filp->private_data;
1282a55ed7faSriastradh 	struct drm_device *dev = priv->minor->dev;
12834e59feabSriastradh 	struct drm_gem_object *obj = NULL;
1284a55ed7faSriastradh 	struct drm_vma_offset_node *node;
1285a55ed7faSriastradh 	int ret;
1286a55ed7faSriastradh 
1287677dec6eSriastradh 	if (drm_dev_is_unplugged(dev))
1288a55ed7faSriastradh 		return -ENODEV;
1289a55ed7faSriastradh 
12904e59feabSriastradh 	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
12914e59feabSriastradh 	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1292a55ed7faSriastradh 						  vma->vm_pgoff,
1293a55ed7faSriastradh 						  vma_pages(vma));
12944e59feabSriastradh 	if (likely(node)) {
12954e59feabSriastradh 		obj = container_of(node, struct drm_gem_object, vma_node);
12964e59feabSriastradh 		/*
12974e59feabSriastradh 		 * When the object is being freed, after it hits 0-refcnt it
12984e59feabSriastradh 		 * proceeds to tear down the object. In the process it will
12994e59feabSriastradh 		 * attempt to remove the VMA offset and so acquire this
13004e59feabSriastradh 		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
13014e59feabSriastradh 		 * that matches our range, we know it is in the process of being
13024e59feabSriastradh 		 * destroyed and will be freed as soon as we release the lock -
13034e59feabSriastradh 		 * so we have to check for the 0-refcnted object and treat it as
13044e59feabSriastradh 		 * invalid.
13054e59feabSriastradh 		 */
13064e59feabSriastradh 		if (!kref_get_unless_zero(&obj->refcount))
13074e59feabSriastradh 			obj = NULL;
13084e59feabSriastradh 	}
13094e59feabSriastradh 	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
13104e59feabSriastradh 
13114e59feabSriastradh 	if (!obj)
13124e59feabSriastradh 		return -EINVAL;
13134e59feabSriastradh 
1314677dec6eSriastradh 	if (!drm_vma_node_is_allowed(node, priv)) {
1315677dec6eSriastradh 		drm_gem_object_put_unlocked(obj);
1316a55ed7faSriastradh 		return -EACCES;
1317a55ed7faSriastradh 	}
1318a55ed7faSriastradh 
1319677dec6eSriastradh 	if (node->readonly) {
1320677dec6eSriastradh 		if (vma->vm_flags & VM_WRITE) {
1321677dec6eSriastradh 			drm_gem_object_put_unlocked(obj);
1322677dec6eSriastradh 			return -EINVAL;
1323677dec6eSriastradh 		}
1324677dec6eSriastradh 
1325677dec6eSriastradh 		vma->vm_flags &= ~VM_MAYWRITE;
1326677dec6eSriastradh 	}
1327677dec6eSriastradh 
13284e59feabSriastradh 	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
13294e59feabSriastradh 			       vma);
1330a55ed7faSriastradh 
1331677dec6eSriastradh 	drm_gem_object_put_unlocked(obj);
133256053ce7Sriastradh 
133356053ce7Sriastradh 	return ret;
133456053ce7Sriastradh }
133556053ce7Sriastradh EXPORT_SYMBOL(drm_gem_mmap);
13362adb3a73Sriastradh #endif	/* defined(__NetBSD__) */
1337677dec6eSriastradh 
drm_gem_print_info(struct drm_printer * p,unsigned int indent,const struct drm_gem_object * obj)1338677dec6eSriastradh void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
1339677dec6eSriastradh 			const struct drm_gem_object *obj)
1340677dec6eSriastradh {
1341677dec6eSriastradh 	drm_printf_indent(p, indent, "name=%d\n", obj->name);
1342677dec6eSriastradh 	drm_printf_indent(p, indent, "refcount=%u\n",
1343677dec6eSriastradh 			  kref_read(&obj->refcount));
1344677dec6eSriastradh 	drm_printf_indent(p, indent, "start=%08lx\n",
1345677dec6eSriastradh 			  drm_vma_node_start(&obj->vma_node));
1346677dec6eSriastradh 	drm_printf_indent(p, indent, "size=%zu\n", obj->size);
1347677dec6eSriastradh 	drm_printf_indent(p, indent, "imported=%s\n",
1348677dec6eSriastradh 			  obj->import_attach ? "yes" : "no");
1349677dec6eSriastradh 
1350677dec6eSriastradh 	if (obj->funcs && obj->funcs->print_info)
1351677dec6eSriastradh 		obj->funcs->print_info(p, indent, obj);
1352677dec6eSriastradh 	else if (obj->dev->driver->gem_print_info)
1353677dec6eSriastradh 		obj->dev->driver->gem_print_info(p, indent, obj);
1354677dec6eSriastradh }
1355677dec6eSriastradh 
drm_gem_pin(struct drm_gem_object * obj)1356677dec6eSriastradh int drm_gem_pin(struct drm_gem_object *obj)
1357677dec6eSriastradh {
1358677dec6eSriastradh 	if (obj->funcs && obj->funcs->pin)
1359677dec6eSriastradh 		return obj->funcs->pin(obj);
1360677dec6eSriastradh 	else if (obj->dev->driver->gem_prime_pin)
1361677dec6eSriastradh 		return obj->dev->driver->gem_prime_pin(obj);
1362677dec6eSriastradh 	else
1363677dec6eSriastradh 		return 0;
1364677dec6eSriastradh }
1365677dec6eSriastradh 
drm_gem_unpin(struct drm_gem_object * obj)1366677dec6eSriastradh void drm_gem_unpin(struct drm_gem_object *obj)
1367677dec6eSriastradh {
1368677dec6eSriastradh 	if (obj->funcs && obj->funcs->unpin)
1369677dec6eSriastradh 		obj->funcs->unpin(obj);
1370677dec6eSriastradh 	else if (obj->dev->driver->gem_prime_unpin)
1371677dec6eSriastradh 		obj->dev->driver->gem_prime_unpin(obj);
1372677dec6eSriastradh }
1373677dec6eSriastradh 
drm_gem_vmap(struct drm_gem_object * obj)1374677dec6eSriastradh void *drm_gem_vmap(struct drm_gem_object *obj)
1375677dec6eSriastradh {
1376677dec6eSriastradh 	void *vaddr;
1377677dec6eSriastradh 
1378677dec6eSriastradh 	if (obj->funcs && obj->funcs->vmap)
1379677dec6eSriastradh 		vaddr = obj->funcs->vmap(obj);
1380677dec6eSriastradh 	else if (obj->dev->driver->gem_prime_vmap)
1381677dec6eSriastradh 		vaddr = obj->dev->driver->gem_prime_vmap(obj);
1382677dec6eSriastradh 	else
1383677dec6eSriastradh 		vaddr = ERR_PTR(-EOPNOTSUPP);
1384677dec6eSriastradh 
1385677dec6eSriastradh 	if (!vaddr)
1386677dec6eSriastradh 		vaddr = ERR_PTR(-ENOMEM);
1387677dec6eSriastradh 
1388677dec6eSriastradh 	return vaddr;
1389677dec6eSriastradh }
1390677dec6eSriastradh 
drm_gem_vunmap(struct drm_gem_object * obj,void * vaddr)1391677dec6eSriastradh void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr)
1392677dec6eSriastradh {
1393677dec6eSriastradh 	if (!vaddr)
1394677dec6eSriastradh 		return;
1395677dec6eSriastradh 
1396677dec6eSriastradh 	if (obj->funcs && obj->funcs->vunmap)
1397677dec6eSriastradh 		obj->funcs->vunmap(obj, vaddr);
1398677dec6eSriastradh 	else if (obj->dev->driver->gem_prime_vunmap)
1399677dec6eSriastradh 		obj->dev->driver->gem_prime_vunmap(obj, vaddr);
1400677dec6eSriastradh }
1401677dec6eSriastradh 
1402677dec6eSriastradh /**
1403677dec6eSriastradh  * drm_gem_lock_reservations - Sets up the ww context and acquires
1404677dec6eSriastradh  * the lock on an array of GEM objects.
1405677dec6eSriastradh  *
1406677dec6eSriastradh  * Once you've locked your reservations, you'll want to set up space
1407677dec6eSriastradh  * for your shared fences (if applicable), submit your job, then
1408677dec6eSriastradh  * drm_gem_unlock_reservations().
1409677dec6eSriastradh  *
1410677dec6eSriastradh  * @objs: drm_gem_objects to lock
1411677dec6eSriastradh  * @count: Number of objects in @objs
1412677dec6eSriastradh  * @acquire_ctx: struct ww_acquire_ctx that will be initialized as
1413677dec6eSriastradh  * part of tracking this set of locked reservations.
1414677dec6eSriastradh  */
1415677dec6eSriastradh int
drm_gem_lock_reservations(struct drm_gem_object ** objs,int count,struct ww_acquire_ctx * acquire_ctx)1416677dec6eSriastradh drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
1417677dec6eSriastradh 			  struct ww_acquire_ctx *acquire_ctx)
1418677dec6eSriastradh {
1419677dec6eSriastradh 	int contended = -1;
1420677dec6eSriastradh 	int i, ret;
1421677dec6eSriastradh 
1422677dec6eSriastradh 	ww_acquire_init(acquire_ctx, &reservation_ww_class);
1423677dec6eSriastradh 
1424677dec6eSriastradh retry:
1425677dec6eSriastradh 	if (contended != -1) {
1426677dec6eSriastradh 		struct drm_gem_object *obj = objs[contended];
1427677dec6eSriastradh 
1428677dec6eSriastradh 		ret = dma_resv_lock_slow_interruptible(obj->resv,
1429677dec6eSriastradh 								 acquire_ctx);
1430677dec6eSriastradh 		if (ret) {
1431677dec6eSriastradh 			ww_acquire_done(acquire_ctx);
1432677dec6eSriastradh 			return ret;
1433677dec6eSriastradh 		}
1434677dec6eSriastradh 	}
1435677dec6eSriastradh 
1436677dec6eSriastradh 	for (i = 0; i < count; i++) {
1437677dec6eSriastradh 		if (i == contended)
1438677dec6eSriastradh 			continue;
1439677dec6eSriastradh 
1440677dec6eSriastradh 		ret = dma_resv_lock_interruptible(objs[i]->resv,
1441677dec6eSriastradh 							    acquire_ctx);
1442677dec6eSriastradh 		if (ret) {
1443677dec6eSriastradh 			int j;
1444677dec6eSriastradh 
1445677dec6eSriastradh 			for (j = 0; j < i; j++)
1446677dec6eSriastradh 				dma_resv_unlock(objs[j]->resv);
1447677dec6eSriastradh 
1448677dec6eSriastradh 			if (contended != -1 && contended >= i)
1449677dec6eSriastradh 				dma_resv_unlock(objs[contended]->resv);
1450677dec6eSriastradh 
1451677dec6eSriastradh 			if (ret == -EDEADLK) {
1452677dec6eSriastradh 				contended = i;
1453677dec6eSriastradh 				goto retry;
1454677dec6eSriastradh 			}
1455677dec6eSriastradh 
1456677dec6eSriastradh 			ww_acquire_done(acquire_ctx);
1457677dec6eSriastradh 			return ret;
1458677dec6eSriastradh 		}
1459677dec6eSriastradh 	}
1460677dec6eSriastradh 
1461677dec6eSriastradh 	ww_acquire_done(acquire_ctx);
1462677dec6eSriastradh 
1463677dec6eSriastradh 	return 0;
1464677dec6eSriastradh }
1465677dec6eSriastradh EXPORT_SYMBOL(drm_gem_lock_reservations);
1466677dec6eSriastradh 
1467677dec6eSriastradh void
drm_gem_unlock_reservations(struct drm_gem_object ** objs,int count,struct ww_acquire_ctx * acquire_ctx)1468677dec6eSriastradh drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
1469677dec6eSriastradh 			    struct ww_acquire_ctx *acquire_ctx)
1470677dec6eSriastradh {
1471677dec6eSriastradh 	int i;
1472677dec6eSriastradh 
1473677dec6eSriastradh 	for (i = 0; i < count; i++)
1474677dec6eSriastradh 		dma_resv_unlock(objs[i]->resv);
1475677dec6eSriastradh 
1476677dec6eSriastradh 	ww_acquire_fini(acquire_ctx);
1477677dec6eSriastradh }
1478677dec6eSriastradh EXPORT_SYMBOL(drm_gem_unlock_reservations);
1479677dec6eSriastradh 
148025fcf548Sriastradh #ifndef __NetBSD__		/* XXX xarray */
148125fcf548Sriastradh 
1482677dec6eSriastradh /**
1483677dec6eSriastradh  * drm_gem_fence_array_add - Adds the fence to an array of fences to be
1484677dec6eSriastradh  * waited on, deduplicating fences from the same context.
1485677dec6eSriastradh  *
1486677dec6eSriastradh  * @fence_array: array of dma_fence * for the job to block on.
1487677dec6eSriastradh  * @fence: the dma_fence to add to the list of dependencies.
1488677dec6eSriastradh  *
1489677dec6eSriastradh  * Returns:
1490677dec6eSriastradh  * 0 on success, or an error on failing to expand the array.
1491677dec6eSriastradh  */
drm_gem_fence_array_add(struct xarray * fence_array,struct dma_fence * fence)1492677dec6eSriastradh int drm_gem_fence_array_add(struct xarray *fence_array,
1493677dec6eSriastradh 			    struct dma_fence *fence)
1494677dec6eSriastradh {
1495677dec6eSriastradh 	struct dma_fence *entry;
1496677dec6eSriastradh 	unsigned long index;
1497677dec6eSriastradh 	u32 id = 0;
1498677dec6eSriastradh 	int ret;
1499677dec6eSriastradh 
1500677dec6eSriastradh 	if (!fence)
1501677dec6eSriastradh 		return 0;
1502677dec6eSriastradh 
1503677dec6eSriastradh 	/* Deduplicate if we already depend on a fence from the same context.
1504677dec6eSriastradh 	 * This lets the size of the array of deps scale with the number of
1505677dec6eSriastradh 	 * engines involved, rather than the number of BOs.
1506677dec6eSriastradh 	 */
1507677dec6eSriastradh 	xa_for_each(fence_array, index, entry) {
1508677dec6eSriastradh 		if (entry->context != fence->context)
1509677dec6eSriastradh 			continue;
1510677dec6eSriastradh 
1511677dec6eSriastradh 		if (dma_fence_is_later(fence, entry)) {
1512677dec6eSriastradh 			dma_fence_put(entry);
1513677dec6eSriastradh 			xa_store(fence_array, index, fence, GFP_KERNEL);
1514677dec6eSriastradh 		} else {
1515677dec6eSriastradh 			dma_fence_put(fence);
1516677dec6eSriastradh 		}
1517677dec6eSriastradh 		return 0;
1518677dec6eSriastradh 	}
1519677dec6eSriastradh 
1520677dec6eSriastradh 	ret = xa_alloc(fence_array, &id, fence, xa_limit_32b, GFP_KERNEL);
1521677dec6eSriastradh 	if (ret != 0)
1522677dec6eSriastradh 		dma_fence_put(fence);
1523677dec6eSriastradh 
1524677dec6eSriastradh 	return ret;
1525677dec6eSriastradh }
1526677dec6eSriastradh EXPORT_SYMBOL(drm_gem_fence_array_add);
1527677dec6eSriastradh 
1528677dec6eSriastradh /**
1529677dec6eSriastradh  * drm_gem_fence_array_add_implicit - Adds the implicit dependencies tracked
1530677dec6eSriastradh  * in the GEM object's reservation object to an array of dma_fences for use in
1531677dec6eSriastradh  * scheduling a rendering job.
1532677dec6eSriastradh  *
1533677dec6eSriastradh  * This should be called after drm_gem_lock_reservations() on your array of
1534677dec6eSriastradh  * GEM objects used in the job but before updating the reservations with your
1535677dec6eSriastradh  * own fences.
1536677dec6eSriastradh  *
1537677dec6eSriastradh  * @fence_array: array of dma_fence * for the job to block on.
1538677dec6eSriastradh  * @obj: the gem object to add new dependencies from.
1539677dec6eSriastradh  * @write: whether the job might write the object (so we need to depend on
1540677dec6eSriastradh  * shared fences in the reservation object).
1541677dec6eSriastradh  */
drm_gem_fence_array_add_implicit(struct xarray * fence_array,struct drm_gem_object * obj,bool write)1542677dec6eSriastradh int drm_gem_fence_array_add_implicit(struct xarray *fence_array,
1543677dec6eSriastradh 				     struct drm_gem_object *obj,
1544677dec6eSriastradh 				     bool write)
1545677dec6eSriastradh {
1546677dec6eSriastradh 	int ret;
1547677dec6eSriastradh 	struct dma_fence **fences;
1548677dec6eSriastradh 	unsigned int i, fence_count;
1549677dec6eSriastradh 
1550677dec6eSriastradh 	if (!write) {
1551677dec6eSriastradh 		struct dma_fence *fence =
1552677dec6eSriastradh 			dma_resv_get_excl_rcu(obj->resv);
1553677dec6eSriastradh 
1554677dec6eSriastradh 		return drm_gem_fence_array_add(fence_array, fence);
1555677dec6eSriastradh 	}
1556677dec6eSriastradh 
1557677dec6eSriastradh 	ret = dma_resv_get_fences_rcu(obj->resv, NULL,
1558677dec6eSriastradh 						&fence_count, &fences);
1559677dec6eSriastradh 	if (ret || !fence_count)
1560677dec6eSriastradh 		return ret;
1561677dec6eSriastradh 
1562677dec6eSriastradh 	for (i = 0; i < fence_count; i++) {
1563677dec6eSriastradh 		ret = drm_gem_fence_array_add(fence_array, fences[i]);
1564677dec6eSriastradh 		if (ret)
1565677dec6eSriastradh 			break;
1566677dec6eSriastradh 	}
1567677dec6eSriastradh 
1568677dec6eSriastradh 	for (; i < fence_count; i++)
1569677dec6eSriastradh 		dma_fence_put(fences[i]);
1570677dec6eSriastradh 	kfree(fences);
1571677dec6eSriastradh 	return ret;
1572677dec6eSriastradh }
1573677dec6eSriastradh EXPORT_SYMBOL(drm_gem_fence_array_add_implicit);
157425fcf548Sriastradh 
157525fcf548Sriastradh #endif
1576