xref: /openbmc/linux/drivers/gpu/drm/drm_gem.c (revision d0a133f7f5bc3583e460ba6bb54474a50ada5201)
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>
39760285e7SDavid Howells #include <drm/drmP.h>
400de23977SDavid Herrmann #include <drm/drm_vma_manager.h>
41d9fc9413SDaniel Vetter #include <drm/drm_gem.h>
4267d0ec4eSDaniel Vetter #include "drm_internal.h"
43673a394bSEric Anholt 
44673a394bSEric Anholt /** @file drm_gem.c
45673a394bSEric Anholt  *
46673a394bSEric Anholt  * This file provides some of the base ioctls and library routines for
47673a394bSEric Anholt  * the graphics memory manager implemented by each device driver.
48673a394bSEric Anholt  *
49673a394bSEric Anholt  * Because various devices have different requirements in terms of
50673a394bSEric Anholt  * synchronization and migration strategies, implementing that is left up to
51673a394bSEric Anholt  * the driver, and all that the general API provides should be generic --
52673a394bSEric Anholt  * allocating objects, reading/writing data with the cpu, freeing objects.
53673a394bSEric Anholt  * Even there, platform-dependent optimizations for reading/writing data with
54673a394bSEric Anholt  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
55673a394bSEric Anholt  * the DRI2 implementation wants to have at least allocate/mmap be generic.
56673a394bSEric Anholt  *
57673a394bSEric Anholt  * The goal was to have swap-backed object allocation managed through
58673a394bSEric Anholt  * struct file.  However, file descriptors as handles to a struct file have
59673a394bSEric Anholt  * two major failings:
60673a394bSEric Anholt  * - Process limits prevent more than 1024 or so being used at a time by
61673a394bSEric Anholt  *   default.
62673a394bSEric Anholt  * - Inability to allocate high fds will aggravate the X Server's select()
63673a394bSEric Anholt  *   handling, and likely that of many GL client applications as well.
64673a394bSEric Anholt  *
65673a394bSEric Anholt  * This led to a plan of using our own integer IDs (called handles, following
66673a394bSEric Anholt  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
67673a394bSEric Anholt  * ioctls.  The objects themselves will still include the struct file so
68673a394bSEric Anholt  * that we can transition to fds if the required kernel infrastructure shows
69673a394bSEric Anholt  * up at a later date, and as our interface with shmfs for memory allocation.
70673a394bSEric Anholt  */
71673a394bSEric Anholt 
72a2c0a97bSJesse Barnes /*
73a2c0a97bSJesse Barnes  * We make up offsets for buffer objects so we can recognize them at
74a2c0a97bSJesse Barnes  * mmap time.
75a2c0a97bSJesse Barnes  */
7605269a3aSJordan Crouse 
7705269a3aSJordan Crouse /* pgoff in mmap is an unsigned long, so we need to make sure that
7805269a3aSJordan Crouse  * the faked up offset will fit
7905269a3aSJordan Crouse  */
8005269a3aSJordan Crouse 
8105269a3aSJordan Crouse #if BITS_PER_LONG == 64
82a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
83a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
8405269a3aSJordan Crouse #else
8505269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
8605269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
8705269a3aSJordan Crouse #endif
88a2c0a97bSJesse Barnes 
89673a394bSEric Anholt /**
9089d61fc0SDaniel Vetter  * drm_gem_init - Initialize the GEM device fields
9189d61fc0SDaniel Vetter  * @dev: drm_devic structure to initialize
92673a394bSEric Anholt  */
93673a394bSEric Anholt int
94673a394bSEric Anholt drm_gem_init(struct drm_device *dev)
95673a394bSEric Anholt {
96b04a5906SDaniel Vetter 	struct drm_vma_offset_manager *vma_offset_manager;
97a2c0a97bSJesse Barnes 
98cd4f013fSDaniel Vetter 	mutex_init(&dev->object_name_lock);
99673a394bSEric Anholt 	idr_init(&dev->object_name_idr);
100a2c0a97bSJesse Barnes 
101b04a5906SDaniel Vetter 	vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), 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 
112673a394bSEric Anholt 	return 0;
113673a394bSEric Anholt }
114673a394bSEric Anholt 
115a2c0a97bSJesse Barnes void
116a2c0a97bSJesse Barnes drm_gem_destroy(struct drm_device *dev)
117a2c0a97bSJesse Barnes {
118a2c0a97bSJesse Barnes 
119b04a5906SDaniel Vetter 	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
120b04a5906SDaniel Vetter 	kfree(dev->vma_offset_manager);
121b04a5906SDaniel Vetter 	dev->vma_offset_manager = NULL;
122a2c0a97bSJesse Barnes }
123a2c0a97bSJesse Barnes 
124673a394bSEric Anholt /**
12589d61fc0SDaniel Vetter  * drm_gem_object_init - initialize an allocated shmem-backed GEM object
12689d61fc0SDaniel Vetter  * @dev: drm_device the object should be initialized for
12789d61fc0SDaniel Vetter  * @obj: drm_gem_object to initialize
12889d61fc0SDaniel Vetter  * @size: object size
12989d61fc0SDaniel Vetter  *
13062cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
1311d397043SDaniel Vetter  * shmfs backing store.
1321d397043SDaniel Vetter  */
1331d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev,
1341d397043SDaniel Vetter 			struct drm_gem_object *obj, size_t size)
1351d397043SDaniel Vetter {
13689c8233fSDavid Herrmann 	struct file *filp;
1371d397043SDaniel Vetter 
1386ab11a26SDaniel Vetter 	drm_gem_private_object_init(dev, obj, size);
1396ab11a26SDaniel Vetter 
14089c8233fSDavid Herrmann 	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
14189c8233fSDavid Herrmann 	if (IS_ERR(filp))
14289c8233fSDavid Herrmann 		return PTR_ERR(filp);
1431d397043SDaniel Vetter 
14489c8233fSDavid Herrmann 	obj->filp = filp;
1451d397043SDaniel Vetter 
1461d397043SDaniel Vetter 	return 0;
1471d397043SDaniel Vetter }
1481d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init);
1491d397043SDaniel Vetter 
1501d397043SDaniel Vetter /**
1512a5706a3SLaurent Pinchart  * drm_gem_private_object_init - initialize an allocated private GEM object
15289d61fc0SDaniel Vetter  * @dev: drm_device the object should be initialized for
15389d61fc0SDaniel Vetter  * @obj: drm_gem_object to initialize
15489d61fc0SDaniel Vetter  * @size: object size
15589d61fc0SDaniel Vetter  *
15662cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
15762cb7011SAlan Cox  * no GEM provided backing store. Instead the caller is responsible for
15862cb7011SAlan Cox  * backing the object and handling it.
15962cb7011SAlan Cox  */
16089c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev,
16162cb7011SAlan Cox 				 struct drm_gem_object *obj, size_t size)
16262cb7011SAlan Cox {
16362cb7011SAlan Cox 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
16462cb7011SAlan Cox 
16562cb7011SAlan Cox 	obj->dev = dev;
16662cb7011SAlan Cox 	obj->filp = NULL;
16762cb7011SAlan Cox 
16862cb7011SAlan Cox 	kref_init(&obj->refcount);
169a8e11d1cSDaniel Vetter 	obj->handle_count = 0;
17062cb7011SAlan Cox 	obj->size = size;
17188d7ebe5SDavid Herrmann 	drm_vma_node_reset(&obj->vma_node);
17262cb7011SAlan Cox }
17362cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init);
17462cb7011SAlan Cox 
1750ff926c7SDave Airlie static void
1760ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
1770ff926c7SDave Airlie {
178319c933cSDaniel Vetter 	/*
179319c933cSDaniel Vetter 	 * Note: obj->dma_buf can't disappear as long as we still hold a
180319c933cSDaniel Vetter 	 * handle reference in obj->handle_count.
181319c933cSDaniel Vetter 	 */
182d0b2c533SDaniel Vetter 	mutex_lock(&filp->prime.lock);
183319c933cSDaniel Vetter 	if (obj->dma_buf) {
184d0b2c533SDaniel Vetter 		drm_prime_remove_buf_handle_locked(&filp->prime,
185319c933cSDaniel Vetter 						   obj->dma_buf);
1860ff926c7SDave Airlie 	}
187d0b2c533SDaniel Vetter 	mutex_unlock(&filp->prime.lock);
1880ff926c7SDave Airlie }
1890ff926c7SDave Airlie 
19036da5908SDaniel Vetter /**
191c6a84325SThierry Reding  * drm_gem_object_handle_free - release resources bound to userspace handles
19289d61fc0SDaniel Vetter  * @obj: GEM object to clean up.
19389d61fc0SDaniel Vetter  *
19436da5908SDaniel Vetter  * Called after the last handle to the object has been closed
19536da5908SDaniel Vetter  *
19636da5908SDaniel Vetter  * Removes any name for the object. Note that this must be
19736da5908SDaniel Vetter  * called before drm_gem_object_free or we'll be touching
19836da5908SDaniel Vetter  * freed memory
19936da5908SDaniel Vetter  */
20036da5908SDaniel Vetter static void drm_gem_object_handle_free(struct drm_gem_object *obj)
20136da5908SDaniel Vetter {
20236da5908SDaniel Vetter 	struct drm_device *dev = obj->dev;
20336da5908SDaniel Vetter 
20436da5908SDaniel Vetter 	/* Remove any name for this object */
20536da5908SDaniel Vetter 	if (obj->name) {
20636da5908SDaniel Vetter 		idr_remove(&dev->object_name_idr, obj->name);
20736da5908SDaniel Vetter 		obj->name = 0;
208a8e11d1cSDaniel Vetter 	}
20936da5908SDaniel Vetter }
21036da5908SDaniel Vetter 
211319c933cSDaniel Vetter static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
212319c933cSDaniel Vetter {
213319c933cSDaniel Vetter 	/* Unbreak the reference cycle if we have an exported dma_buf. */
214319c933cSDaniel Vetter 	if (obj->dma_buf) {
215319c933cSDaniel Vetter 		dma_buf_put(obj->dma_buf);
216319c933cSDaniel Vetter 		obj->dma_buf = NULL;
217319c933cSDaniel Vetter 	}
218319c933cSDaniel Vetter }
219319c933cSDaniel Vetter 
220becee2a5SDaniel Vetter static void
221e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
22236da5908SDaniel Vetter {
22398a8883aSChris Wilson 	struct drm_device *dev = obj->dev;
22498a8883aSChris Wilson 	bool final = false;
22598a8883aSChris Wilson 
226a8e11d1cSDaniel Vetter 	if (WARN_ON(obj->handle_count == 0))
22736da5908SDaniel Vetter 		return;
22836da5908SDaniel Vetter 
22936da5908SDaniel Vetter 	/*
23036da5908SDaniel Vetter 	* Must bump handle count first as this may be the last
23136da5908SDaniel Vetter 	* ref, in which case the object would disappear before we
23236da5908SDaniel Vetter 	* checked for a name
23336da5908SDaniel Vetter 	*/
23436da5908SDaniel Vetter 
23598a8883aSChris Wilson 	mutex_lock(&dev->object_name_lock);
236319c933cSDaniel Vetter 	if (--obj->handle_count == 0) {
23736da5908SDaniel Vetter 		drm_gem_object_handle_free(obj);
238319c933cSDaniel Vetter 		drm_gem_object_exported_dma_buf_free(obj);
23998a8883aSChris Wilson 		final = true;
240319c933cSDaniel Vetter 	}
24198a8883aSChris Wilson 	mutex_unlock(&dev->object_name_lock);
242a8e11d1cSDaniel Vetter 
24398a8883aSChris Wilson 	if (final)
244e6b62714SThierry Reding 		drm_gem_object_put_unlocked(obj);
24536da5908SDaniel Vetter }
24636da5908SDaniel Vetter 
2478815b23aSChris Wilson /*
2488815b23aSChris Wilson  * Called at device or object close to release the file's
2498815b23aSChris Wilson  * handle references on objects.
2508815b23aSChris Wilson  */
2518815b23aSChris Wilson static int
2528815b23aSChris Wilson drm_gem_object_release_handle(int id, void *ptr, void *data)
2538815b23aSChris Wilson {
2548815b23aSChris Wilson 	struct drm_file *file_priv = data;
2558815b23aSChris Wilson 	struct drm_gem_object *obj = ptr;
2568815b23aSChris Wilson 	struct drm_device *dev = obj->dev;
2578815b23aSChris Wilson 
258*d0a133f7SChris Wilson 	if (dev->driver->gem_close_object)
259*d0a133f7SChris Wilson 		dev->driver->gem_close_object(obj, file_priv);
260*d0a133f7SChris Wilson 
2618815b23aSChris Wilson 	if (drm_core_check_feature(dev, DRIVER_PRIME))
2628815b23aSChris Wilson 		drm_gem_remove_prime_handles(obj, file_priv);
263d9a1f0b4SDavid Herrmann 	drm_vma_node_revoke(&obj->vma_node, file_priv);
2648815b23aSChris Wilson 
265e6b62714SThierry Reding 	drm_gem_object_handle_put_unlocked(obj);
2668815b23aSChris Wilson 
2678815b23aSChris Wilson 	return 0;
2688815b23aSChris Wilson }
2698815b23aSChris Wilson 
270673a394bSEric Anholt /**
27189d61fc0SDaniel Vetter  * drm_gem_handle_delete - deletes the given file-private handle
27289d61fc0SDaniel Vetter  * @filp: drm file-private structure to use for the handle look up
27389d61fc0SDaniel Vetter  * @handle: userspace handle to delete
27489d61fc0SDaniel Vetter  *
275df2e0900SDaniel Vetter  * Removes the GEM handle from the @filp lookup table which has been added with
276df2e0900SDaniel Vetter  * drm_gem_handle_create(). If this is the last handle also cleans up linked
277df2e0900SDaniel Vetter  * resources like GEM names.
278673a394bSEric Anholt  */
279ff72145bSDave Airlie int
280a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle)
281673a394bSEric Anholt {
282673a394bSEric Anholt 	struct drm_gem_object *obj;
283673a394bSEric Anholt 
284673a394bSEric Anholt 	/* This is gross. The idr system doesn't let us try a delete and
285673a394bSEric Anholt 	 * return an error code.  It just spews if you fail at deleting.
286673a394bSEric Anholt 	 * So, we have to grab a lock around finding the object and then
287673a394bSEric Anholt 	 * doing the delete on it and dropping the refcount, or the user
288673a394bSEric Anholt 	 * could race us to double-decrement the refcount and cause a
289673a394bSEric Anholt 	 * use-after-free later.  Given the frequency of our handle lookups,
290673a394bSEric Anholt 	 * we may want to use ida for number allocation and a hash table
291673a394bSEric Anholt 	 * for the pointers, anyway.
292673a394bSEric Anholt 	 */
293673a394bSEric Anholt 	spin_lock(&filp->table_lock);
294673a394bSEric Anholt 
295673a394bSEric Anholt 	/* Check if we currently have a reference on the object */
296f6cd7daeSChris Wilson 	obj = idr_replace(&filp->object_idr, NULL, handle);
297673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
298f6cd7daeSChris Wilson 	if (IS_ERR_OR_NULL(obj))
299673a394bSEric Anholt 		return -EINVAL;
300673a394bSEric Anholt 
301f6cd7daeSChris Wilson 	/* Release driver's reference and decrement refcount. */
302f6cd7daeSChris Wilson 	drm_gem_object_release_handle(handle, obj, filp);
303f6cd7daeSChris Wilson 
304f6cd7daeSChris Wilson 	/* And finally make the handle available for future allocations. */
305f6cd7daeSChris Wilson 	spin_lock(&filp->table_lock);
306673a394bSEric Anholt 	idr_remove(&filp->object_idr, handle);
307673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
308673a394bSEric Anholt 
309673a394bSEric Anholt 	return 0;
310673a394bSEric Anholt }
311ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete);
312673a394bSEric Anholt 
313673a394bSEric Anholt /**
314db611527SNoralf Trønnes  * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
315db611527SNoralf Trønnes  * @file: drm file-private structure containing the gem object
316db611527SNoralf Trønnes  * @dev: corresponding drm_device
317db611527SNoralf Trønnes  * @handle: gem object handle
318db611527SNoralf Trønnes  * @offset: return location for the fake mmap offset
319db611527SNoralf Trønnes  *
320db611527SNoralf Trønnes  * This implements the &drm_driver.dumb_map_offset kms driver callback for
321db611527SNoralf Trønnes  * drivers which use gem to manage their backing storage.
322db611527SNoralf Trønnes  *
323db611527SNoralf Trønnes  * Returns:
324db611527SNoralf Trønnes  * 0 on success or a negative error code on failure.
325db611527SNoralf Trønnes  */
326db611527SNoralf Trønnes int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
327db611527SNoralf Trønnes 			    u32 handle, u64 *offset)
328db611527SNoralf Trønnes {
329db611527SNoralf Trønnes 	struct drm_gem_object *obj;
330db611527SNoralf Trønnes 	int ret;
331db611527SNoralf Trønnes 
332db611527SNoralf Trønnes 	obj = drm_gem_object_lookup(file, handle);
333db611527SNoralf Trønnes 	if (!obj)
334db611527SNoralf Trønnes 		return -ENOENT;
335db611527SNoralf Trønnes 
336db611527SNoralf Trønnes 	ret = drm_gem_create_mmap_offset(obj);
337db611527SNoralf Trønnes 	if (ret)
338db611527SNoralf Trønnes 		goto out;
339db611527SNoralf Trønnes 
340db611527SNoralf Trønnes 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
341db611527SNoralf Trønnes out:
342db611527SNoralf Trønnes 	drm_gem_object_put_unlocked(obj);
343db611527SNoralf Trønnes 
344db611527SNoralf Trønnes 	return ret;
345db611527SNoralf Trønnes }
346db611527SNoralf Trønnes EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
347db611527SNoralf Trønnes 
348db611527SNoralf Trønnes /**
34943387b37SDaniel Vetter  * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
35089d61fc0SDaniel Vetter  * @file: drm file-private structure to remove the dumb handle from
35189d61fc0SDaniel Vetter  * @dev: corresponding drm_device
35289d61fc0SDaniel Vetter  * @handle: the dumb handle to remove
35343387b37SDaniel Vetter  *
354940eba2dSDaniel Vetter  * This implements the &drm_driver.dumb_destroy kms driver callback for drivers
355940eba2dSDaniel Vetter  * which use gem to manage their backing storage.
35643387b37SDaniel Vetter  */
35743387b37SDaniel Vetter int drm_gem_dumb_destroy(struct drm_file *file,
35843387b37SDaniel Vetter 			 struct drm_device *dev,
35943387b37SDaniel Vetter 			 uint32_t handle)
36043387b37SDaniel Vetter {
36143387b37SDaniel Vetter 	return drm_gem_handle_delete(file, handle);
36243387b37SDaniel Vetter }
36343387b37SDaniel Vetter EXPORT_SYMBOL(drm_gem_dumb_destroy);
36443387b37SDaniel Vetter 
36543387b37SDaniel Vetter /**
36620228c44SDaniel Vetter  * drm_gem_handle_create_tail - internal functions to create a handle
36789d61fc0SDaniel Vetter  * @file_priv: drm file-private structure to register the handle for
36889d61fc0SDaniel Vetter  * @obj: object to register
3698bf8180fSThierry Reding  * @handlep: pointer to return the created handle to the caller
37020228c44SDaniel Vetter  *
371940eba2dSDaniel Vetter  * This expects the &drm_device.object_name_lock to be held already and will
372940eba2dSDaniel Vetter  * drop it before returning. Used to avoid races in establishing new handles
373940eba2dSDaniel Vetter  * when importing an object from either an flink name or a dma-buf.
374df2e0900SDaniel Vetter  *
375df2e0900SDaniel Vetter  * Handles must be release again through drm_gem_handle_delete(). This is done
376df2e0900SDaniel Vetter  * when userspace closes @file_priv for all attached handles, or through the
377df2e0900SDaniel Vetter  * GEM_CLOSE ioctl for individual handles.
378673a394bSEric Anholt  */
379673a394bSEric Anholt int
38020228c44SDaniel Vetter drm_gem_handle_create_tail(struct drm_file *file_priv,
381673a394bSEric Anholt 			   struct drm_gem_object *obj,
382a1a2d1d3SPekka Paalanen 			   u32 *handlep)
383673a394bSEric Anholt {
384304eda32SBen Skeggs 	struct drm_device *dev = obj->dev;
3859649399eSChris Wilson 	u32 handle;
386673a394bSEric Anholt 	int ret;
387673a394bSEric Anholt 
38820228c44SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->object_name_lock));
38998a8883aSChris Wilson 	if (obj->handle_count++ == 0)
390e6b62714SThierry Reding 		drm_gem_object_get(obj);
39120228c44SDaniel Vetter 
392673a394bSEric Anholt 	/*
3932e928815STejun Heo 	 * Get the user-visible handle using idr.  Preload and perform
3942e928815STejun Heo 	 * allocation under our spinlock.
395673a394bSEric Anholt 	 */
3962e928815STejun Heo 	idr_preload(GFP_KERNEL);
397673a394bSEric Anholt 	spin_lock(&file_priv->table_lock);
3982e928815STejun Heo 
3992e928815STejun Heo 	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
40098a8883aSChris Wilson 
401673a394bSEric Anholt 	spin_unlock(&file_priv->table_lock);
4022e928815STejun Heo 	idr_preload_end();
40398a8883aSChris Wilson 
404cd4f013fSDaniel Vetter 	mutex_unlock(&dev->object_name_lock);
4056984128dSChris Wilson 	if (ret < 0)
4066984128dSChris Wilson 		goto err_unref;
4076984128dSChris Wilson 
4089649399eSChris Wilson 	handle = ret;
409673a394bSEric Anholt 
410d9a1f0b4SDavid Herrmann 	ret = drm_vma_node_allow(&obj->vma_node, file_priv);
4116984128dSChris Wilson 	if (ret)
4126984128dSChris Wilson 		goto err_remove;
413304eda32SBen Skeggs 
414304eda32SBen Skeggs 	if (dev->driver->gem_open_object) {
415304eda32SBen Skeggs 		ret = dev->driver->gem_open_object(obj, file_priv);
4166984128dSChris Wilson 		if (ret)
4176984128dSChris Wilson 			goto err_revoke;
418304eda32SBen Skeggs 	}
419304eda32SBen Skeggs 
4209649399eSChris Wilson 	*handlep = handle;
421673a394bSEric Anholt 	return 0;
4226984128dSChris Wilson 
4236984128dSChris Wilson err_revoke:
424d9a1f0b4SDavid Herrmann 	drm_vma_node_revoke(&obj->vma_node, file_priv);
4256984128dSChris Wilson err_remove:
4266984128dSChris Wilson 	spin_lock(&file_priv->table_lock);
4279649399eSChris Wilson 	idr_remove(&file_priv->object_idr, handle);
4286984128dSChris Wilson 	spin_unlock(&file_priv->table_lock);
4296984128dSChris Wilson err_unref:
430e6b62714SThierry Reding 	drm_gem_object_handle_put_unlocked(obj);
4316984128dSChris Wilson 	return ret;
432673a394bSEric Anholt }
43320228c44SDaniel Vetter 
43420228c44SDaniel Vetter /**
4358bf8180fSThierry Reding  * drm_gem_handle_create - create a gem handle for an object
43689d61fc0SDaniel Vetter  * @file_priv: drm file-private structure to register the handle for
43789d61fc0SDaniel Vetter  * @obj: object to register
43889d61fc0SDaniel Vetter  * @handlep: pionter to return the created handle to the caller
43989d61fc0SDaniel Vetter  *
44020228c44SDaniel Vetter  * Create a handle for this object. This adds a handle reference
44120228c44SDaniel Vetter  * to the object, which includes a regular reference count. Callers
44220228c44SDaniel Vetter  * will likely want to dereference the object afterwards.
44320228c44SDaniel Vetter  */
4448bf8180fSThierry Reding int drm_gem_handle_create(struct drm_file *file_priv,
44520228c44SDaniel Vetter 			  struct drm_gem_object *obj,
44620228c44SDaniel Vetter 			  u32 *handlep)
44720228c44SDaniel Vetter {
44820228c44SDaniel Vetter 	mutex_lock(&obj->dev->object_name_lock);
44920228c44SDaniel Vetter 
45020228c44SDaniel Vetter 	return drm_gem_handle_create_tail(file_priv, obj, handlep);
45120228c44SDaniel Vetter }
452673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create);
453673a394bSEric Anholt 
45475ef8b3bSRob Clark 
45575ef8b3bSRob Clark /**
45675ef8b3bSRob Clark  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
45775ef8b3bSRob Clark  * @obj: obj in question
45875ef8b3bSRob Clark  *
45975ef8b3bSRob Clark  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
460f74418a4SDaniel Vetter  *
461f74418a4SDaniel Vetter  * Note that drm_gem_object_release() already calls this function, so drivers
462f74418a4SDaniel Vetter  * don't have to take care of releasing the mmap offset themselves when freeing
463f74418a4SDaniel Vetter  * the GEM object.
46475ef8b3bSRob Clark  */
46575ef8b3bSRob Clark void
46675ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj)
46775ef8b3bSRob Clark {
46875ef8b3bSRob Clark 	struct drm_device *dev = obj->dev;
46975ef8b3bSRob Clark 
470b04a5906SDaniel Vetter 	drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
47175ef8b3bSRob Clark }
47275ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset);
47375ef8b3bSRob Clark 
47475ef8b3bSRob Clark /**
475367bbd49SRob Clark  * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
476367bbd49SRob Clark  * @obj: obj in question
477367bbd49SRob Clark  * @size: the virtual size
478367bbd49SRob Clark  *
479367bbd49SRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
480367bbd49SRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
481367bbd49SRob Clark  * up the object based on the offset and sets up the various memory mapping
482367bbd49SRob Clark  * structures.
483367bbd49SRob Clark  *
484367bbd49SRob Clark  * This routine allocates and attaches a fake offset for @obj, in cases where
485940eba2dSDaniel Vetter  * the virtual size differs from the physical size (ie. &drm_gem_object.size).
486940eba2dSDaniel Vetter  * Otherwise just use drm_gem_create_mmap_offset().
487f74418a4SDaniel Vetter  *
488f74418a4SDaniel Vetter  * This function is idempotent and handles an already allocated mmap offset
489f74418a4SDaniel Vetter  * transparently. Drivers do not need to check for this case.
490367bbd49SRob Clark  */
491367bbd49SRob Clark int
492367bbd49SRob Clark drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
493367bbd49SRob Clark {
494367bbd49SRob Clark 	struct drm_device *dev = obj->dev;
495367bbd49SRob Clark 
496b04a5906SDaniel Vetter 	return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
497367bbd49SRob Clark 				  size / PAGE_SIZE);
498367bbd49SRob Clark }
499367bbd49SRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
500367bbd49SRob Clark 
501367bbd49SRob Clark /**
50275ef8b3bSRob Clark  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
50375ef8b3bSRob Clark  * @obj: obj in question
50475ef8b3bSRob Clark  *
50575ef8b3bSRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
50675ef8b3bSRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
50775ef8b3bSRob Clark  * up the object based on the offset and sets up the various memory mapping
50875ef8b3bSRob Clark  * structures.
50975ef8b3bSRob Clark  *
51075ef8b3bSRob Clark  * This routine allocates and attaches a fake offset for @obj.
511f74418a4SDaniel Vetter  *
512f74418a4SDaniel Vetter  * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
513f74418a4SDaniel Vetter  * the fake offset again.
51475ef8b3bSRob Clark  */
515367bbd49SRob Clark int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
51675ef8b3bSRob Clark {
517367bbd49SRob Clark 	return drm_gem_create_mmap_offset_size(obj, obj->size);
51875ef8b3bSRob Clark }
51975ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset);
52075ef8b3bSRob Clark 
521bcc5c9d5SRob Clark /**
522bcc5c9d5SRob Clark  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
523bcc5c9d5SRob Clark  * from shmem
524bcc5c9d5SRob Clark  * @obj: obj in question
5250cdbe8acSDavid Herrmann  *
5260cdbe8acSDavid Herrmann  * This reads the page-array of the shmem-backing storage of the given gem
5270cdbe8acSDavid Herrmann  * object. An array of pages is returned. If a page is not allocated or
5280cdbe8acSDavid Herrmann  * swapped-out, this will allocate/swap-in the required pages. Note that the
5290cdbe8acSDavid Herrmann  * whole object is covered by the page-array and pinned in memory.
5300cdbe8acSDavid Herrmann  *
5310cdbe8acSDavid Herrmann  * Use drm_gem_put_pages() to release the array and unpin all pages.
5320cdbe8acSDavid Herrmann  *
5330cdbe8acSDavid Herrmann  * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
5340cdbe8acSDavid Herrmann  * If you require other GFP-masks, you have to do those allocations yourself.
5350cdbe8acSDavid Herrmann  *
5360cdbe8acSDavid Herrmann  * Note that you are not allowed to change gfp-zones during runtime. That is,
5370cdbe8acSDavid Herrmann  * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
5380cdbe8acSDavid Herrmann  * set during initialization. If you have special zone constraints, set them
5390cdbe8acSDavid Herrmann  * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care
5400cdbe8acSDavid Herrmann  * to keep pages in the required zone during swap-in.
541bcc5c9d5SRob Clark  */
5420cdbe8acSDavid Herrmann struct page **drm_gem_get_pages(struct drm_gem_object *obj)
543bcc5c9d5SRob Clark {
544bcc5c9d5SRob Clark 	struct address_space *mapping;
545bcc5c9d5SRob Clark 	struct page *p, **pages;
546bcc5c9d5SRob Clark 	int i, npages;
547bcc5c9d5SRob Clark 
548bcc5c9d5SRob Clark 	/* This is the shared memory object that backs the GEM resource */
54993c76a3dSAl Viro 	mapping = obj->filp->f_mapping;
550bcc5c9d5SRob Clark 
551bcc5c9d5SRob Clark 	/* We already BUG_ON() for non-page-aligned sizes in
552bcc5c9d5SRob Clark 	 * drm_gem_object_init(), so we should never hit this unless
553bcc5c9d5SRob Clark 	 * driver author is doing something really wrong:
554bcc5c9d5SRob Clark 	 */
555bcc5c9d5SRob Clark 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
556bcc5c9d5SRob Clark 
557bcc5c9d5SRob Clark 	npages = obj->size >> PAGE_SHIFT;
558bcc5c9d5SRob Clark 
5592098105eSMichal Hocko 	pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
560bcc5c9d5SRob Clark 	if (pages == NULL)
561bcc5c9d5SRob Clark 		return ERR_PTR(-ENOMEM);
562bcc5c9d5SRob Clark 
563bcc5c9d5SRob Clark 	for (i = 0; i < npages; i++) {
5640cdbe8acSDavid Herrmann 		p = shmem_read_mapping_page(mapping, i);
565bcc5c9d5SRob Clark 		if (IS_ERR(p))
566bcc5c9d5SRob Clark 			goto fail;
567bcc5c9d5SRob Clark 		pages[i] = p;
568bcc5c9d5SRob Clark 
5692123000bSDavid Herrmann 		/* Make sure shmem keeps __GFP_DMA32 allocated pages in the
5702123000bSDavid Herrmann 		 * correct region during swapin. Note that this requires
5712123000bSDavid Herrmann 		 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
5722123000bSDavid Herrmann 		 * so shmem can relocate pages during swapin if required.
573bcc5c9d5SRob Clark 		 */
574c62d2555SMichal Hocko 		BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
575bcc5c9d5SRob Clark 				(page_to_pfn(p) >= 0x00100000UL));
576bcc5c9d5SRob Clark 	}
577bcc5c9d5SRob Clark 
578bcc5c9d5SRob Clark 	return pages;
579bcc5c9d5SRob Clark 
580bcc5c9d5SRob Clark fail:
581bcc5c9d5SRob Clark 	while (i--)
58209cbfeafSKirill A. Shutemov 		put_page(pages[i]);
583bcc5c9d5SRob Clark 
5842098105eSMichal Hocko 	kvfree(pages);
585bcc5c9d5SRob Clark 	return ERR_CAST(p);
586bcc5c9d5SRob Clark }
587bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_get_pages);
588bcc5c9d5SRob Clark 
589bcc5c9d5SRob Clark /**
590bcc5c9d5SRob Clark  * drm_gem_put_pages - helper to free backing pages for a GEM object
591bcc5c9d5SRob Clark  * @obj: obj in question
592bcc5c9d5SRob Clark  * @pages: pages to free
593bcc5c9d5SRob Clark  * @dirty: if true, pages will be marked as dirty
594bcc5c9d5SRob Clark  * @accessed: if true, the pages will be marked as accessed
595bcc5c9d5SRob Clark  */
596bcc5c9d5SRob Clark void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
597bcc5c9d5SRob Clark 		bool dirty, bool accessed)
598bcc5c9d5SRob Clark {
599bcc5c9d5SRob Clark 	int i, npages;
600bcc5c9d5SRob Clark 
601bcc5c9d5SRob Clark 	/* We already BUG_ON() for non-page-aligned sizes in
602bcc5c9d5SRob Clark 	 * drm_gem_object_init(), so we should never hit this unless
603bcc5c9d5SRob Clark 	 * driver author is doing something really wrong:
604bcc5c9d5SRob Clark 	 */
605bcc5c9d5SRob Clark 	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
606bcc5c9d5SRob Clark 
607bcc5c9d5SRob Clark 	npages = obj->size >> PAGE_SHIFT;
608bcc5c9d5SRob Clark 
609bcc5c9d5SRob Clark 	for (i = 0; i < npages; i++) {
610bcc5c9d5SRob Clark 		if (dirty)
611bcc5c9d5SRob Clark 			set_page_dirty(pages[i]);
612bcc5c9d5SRob Clark 
613bcc5c9d5SRob Clark 		if (accessed)
614bcc5c9d5SRob Clark 			mark_page_accessed(pages[i]);
615bcc5c9d5SRob Clark 
616bcc5c9d5SRob Clark 		/* Undo the reference we took when populating the table */
61709cbfeafSKirill A. Shutemov 		put_page(pages[i]);
618bcc5c9d5SRob Clark 	}
619bcc5c9d5SRob Clark 
6202098105eSMichal Hocko 	kvfree(pages);
621bcc5c9d5SRob Clark }
622bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_put_pages);
623bcc5c9d5SRob Clark 
624df2e0900SDaniel Vetter /**
625df2e0900SDaniel Vetter  * drm_gem_object_lookup - look up a GEM object from it's handle
626df2e0900SDaniel Vetter  * @filp: DRM file private date
627df2e0900SDaniel Vetter  * @handle: userspace handle
628df2e0900SDaniel Vetter  *
629df2e0900SDaniel Vetter  * Returns:
630df2e0900SDaniel Vetter  *
631df2e0900SDaniel Vetter  * A reference to the object named by the handle if such exists on @filp, NULL
632df2e0900SDaniel Vetter  * otherwise.
633df2e0900SDaniel Vetter  */
634673a394bSEric Anholt struct drm_gem_object *
635a8ad0bd8SChris Wilson drm_gem_object_lookup(struct drm_file *filp, u32 handle)
636673a394bSEric Anholt {
637673a394bSEric Anholt 	struct drm_gem_object *obj;
638673a394bSEric Anholt 
639673a394bSEric Anholt 	spin_lock(&filp->table_lock);
640673a394bSEric Anholt 
641673a394bSEric Anholt 	/* Check if we currently have a reference on the object */
642673a394bSEric Anholt 	obj = idr_find(&filp->object_idr, handle);
643a8ad0bd8SChris Wilson 	if (obj)
644e6b62714SThierry Reding 		drm_gem_object_get(obj);
645673a394bSEric Anholt 
646673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
647673a394bSEric Anholt 
648673a394bSEric Anholt 	return obj;
649673a394bSEric Anholt }
650673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup);
651673a394bSEric Anholt 
652673a394bSEric Anholt /**
65389d61fc0SDaniel Vetter  * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
65489d61fc0SDaniel Vetter  * @dev: drm_device
65589d61fc0SDaniel Vetter  * @data: ioctl data
65689d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
65789d61fc0SDaniel Vetter  *
658673a394bSEric Anholt  * Releases the handle to an mm object.
659673a394bSEric Anholt  */
660673a394bSEric Anholt int
661673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data,
662673a394bSEric Anholt 		    struct drm_file *file_priv)
663673a394bSEric Anholt {
664673a394bSEric Anholt 	struct drm_gem_close *args = data;
665673a394bSEric Anholt 	int ret;
666673a394bSEric Anholt 
6671bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
668673a394bSEric Anholt 		return -ENODEV;
669673a394bSEric Anholt 
670673a394bSEric Anholt 	ret = drm_gem_handle_delete(file_priv, args->handle);
671673a394bSEric Anholt 
672673a394bSEric Anholt 	return ret;
673673a394bSEric Anholt }
674673a394bSEric Anholt 
675673a394bSEric Anholt /**
67689d61fc0SDaniel Vetter  * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
67789d61fc0SDaniel Vetter  * @dev: drm_device
67889d61fc0SDaniel Vetter  * @data: ioctl data
67989d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
68089d61fc0SDaniel Vetter  *
681673a394bSEric Anholt  * Create a global name for an object, returning the name.
682673a394bSEric Anholt  *
683673a394bSEric Anholt  * Note that the name does not hold a reference; when the object
684673a394bSEric Anholt  * is freed, the name goes away.
685673a394bSEric Anholt  */
686673a394bSEric Anholt int
687673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data,
688673a394bSEric Anholt 		    struct drm_file *file_priv)
689673a394bSEric Anholt {
690673a394bSEric Anholt 	struct drm_gem_flink *args = data;
691673a394bSEric Anholt 	struct drm_gem_object *obj;
692673a394bSEric Anholt 	int ret;
693673a394bSEric Anholt 
6941bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
695673a394bSEric Anholt 		return -ENODEV;
696673a394bSEric Anholt 
697a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, args->handle);
698673a394bSEric Anholt 	if (obj == NULL)
699bf79cb91SChris Wilson 		return -ENOENT;
700673a394bSEric Anholt 
701cd4f013fSDaniel Vetter 	mutex_lock(&dev->object_name_lock);
702a8e11d1cSDaniel Vetter 	/* prevent races with concurrent gem_close. */
703a8e11d1cSDaniel Vetter 	if (obj->handle_count == 0) {
704a8e11d1cSDaniel Vetter 		ret = -ENOENT;
705a8e11d1cSDaniel Vetter 		goto err;
706a8e11d1cSDaniel Vetter 	}
707a8e11d1cSDaniel Vetter 
7088d59bae5SChris Wilson 	if (!obj->name) {
7090f646425SChris Wilson 		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
7102e928815STejun Heo 		if (ret < 0)
7113e49c4f4SChris Wilson 			goto err;
7122e07fb22SYoungJun Cho 
7132e07fb22SYoungJun Cho 		obj->name = ret;
7148d59bae5SChris Wilson 	}
7153e49c4f4SChris Wilson 
7162e07fb22SYoungJun Cho 	args->name = (uint64_t) obj->name;
7172e07fb22SYoungJun Cho 	ret = 0;
7182e07fb22SYoungJun Cho 
7193e49c4f4SChris Wilson err:
720cd4f013fSDaniel Vetter 	mutex_unlock(&dev->object_name_lock);
721e6b62714SThierry Reding 	drm_gem_object_put_unlocked(obj);
7223e49c4f4SChris Wilson 	return ret;
723673a394bSEric Anholt }
724673a394bSEric Anholt 
725673a394bSEric Anholt /**
72689d61fc0SDaniel Vetter  * drm_gem_open - implementation of the GEM_OPEN ioctl
72789d61fc0SDaniel Vetter  * @dev: drm_device
72889d61fc0SDaniel Vetter  * @data: ioctl data
72989d61fc0SDaniel Vetter  * @file_priv: drm file-private structure
73089d61fc0SDaniel Vetter  *
731673a394bSEric Anholt  * Open an object using the global name, returning a handle and the size.
732673a394bSEric Anholt  *
733673a394bSEric Anholt  * This handle (of course) holds a reference to the object, so the object
734673a394bSEric Anholt  * will not go away until the handle is deleted.
735673a394bSEric Anholt  */
736673a394bSEric Anholt int
737673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data,
738673a394bSEric Anholt 		   struct drm_file *file_priv)
739673a394bSEric Anholt {
740673a394bSEric Anholt 	struct drm_gem_open *args = data;
741673a394bSEric Anholt 	struct drm_gem_object *obj;
742673a394bSEric Anholt 	int ret;
743a1a2d1d3SPekka Paalanen 	u32 handle;
744673a394bSEric Anholt 
7451bcecfacSAndrzej Hajda 	if (!drm_core_check_feature(dev, DRIVER_GEM))
746673a394bSEric Anholt 		return -ENODEV;
747673a394bSEric Anholt 
748cd4f013fSDaniel Vetter 	mutex_lock(&dev->object_name_lock);
749673a394bSEric Anholt 	obj = idr_find(&dev->object_name_idr, (int) args->name);
75020228c44SDaniel Vetter 	if (obj) {
751e6b62714SThierry Reding 		drm_gem_object_get(obj);
75220228c44SDaniel Vetter 	} else {
753cd4f013fSDaniel Vetter 		mutex_unlock(&dev->object_name_lock);
754673a394bSEric Anholt 		return -ENOENT;
75520228c44SDaniel Vetter 	}
756673a394bSEric Anholt 
75720228c44SDaniel Vetter 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
75820228c44SDaniel Vetter 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
759e6b62714SThierry Reding 	drm_gem_object_put_unlocked(obj);
760673a394bSEric Anholt 	if (ret)
761673a394bSEric Anholt 		return ret;
762673a394bSEric Anholt 
763673a394bSEric Anholt 	args->handle = handle;
764673a394bSEric Anholt 	args->size = obj->size;
765673a394bSEric Anholt 
766673a394bSEric Anholt 	return 0;
767673a394bSEric Anholt }
768673a394bSEric Anholt 
769673a394bSEric Anholt /**
77089d61fc0SDaniel Vetter  * gem_gem_open - initalizes GEM file-private structures at devnode open time
77189d61fc0SDaniel Vetter  * @dev: drm_device which is being opened by userspace
77289d61fc0SDaniel Vetter  * @file_private: drm file-private structure to set up
77389d61fc0SDaniel Vetter  *
774673a394bSEric Anholt  * Called at device open time, sets up the structure for handling refcounting
775673a394bSEric Anholt  * of mm objects.
776673a394bSEric Anholt  */
777673a394bSEric Anholt void
778673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
779673a394bSEric Anholt {
780673a394bSEric Anholt 	idr_init(&file_private->object_idr);
781673a394bSEric Anholt 	spin_lock_init(&file_private->table_lock);
782673a394bSEric Anholt }
783673a394bSEric Anholt 
784673a394bSEric Anholt /**
78589d61fc0SDaniel Vetter  * drm_gem_release - release file-private GEM resources
78689d61fc0SDaniel Vetter  * @dev: drm_device which is being closed by userspace
78789d61fc0SDaniel Vetter  * @file_private: drm file-private structure to clean up
78889d61fc0SDaniel Vetter  *
789673a394bSEric Anholt  * Called at close time when the filp is going away.
790673a394bSEric Anholt  *
791673a394bSEric Anholt  * Releases any remaining references on objects by this filp.
792673a394bSEric Anholt  */
793673a394bSEric Anholt void
794673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
795673a394bSEric Anholt {
796673a394bSEric Anholt 	idr_for_each(&file_private->object_idr,
797304eda32SBen Skeggs 		     &drm_gem_object_release_handle, file_private);
798673a394bSEric Anholt 	idr_destroy(&file_private->object_idr);
799673a394bSEric Anholt }
800673a394bSEric Anholt 
801f74418a4SDaniel Vetter /**
802f74418a4SDaniel Vetter  * drm_gem_object_release - release GEM buffer object resources
803f74418a4SDaniel Vetter  * @obj: GEM buffer object
804f74418a4SDaniel Vetter  *
805f74418a4SDaniel Vetter  * This releases any structures and resources used by @obj and is the invers of
806f74418a4SDaniel Vetter  * drm_gem_object_init().
807f74418a4SDaniel Vetter  */
808fd632aa3SDaniel Vetter void
809fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj)
810c3ae90c0SLuca Barbieri {
811319c933cSDaniel Vetter 	WARN_ON(obj->dma_buf);
812319c933cSDaniel Vetter 
81362cb7011SAlan Cox 	if (obj->filp)
814c3ae90c0SLuca Barbieri 		fput(obj->filp);
81577472347SDavid Herrmann 
81677472347SDavid Herrmann 	drm_gem_free_mmap_offset(obj);
817c3ae90c0SLuca Barbieri }
818fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release);
819c3ae90c0SLuca Barbieri 
820673a394bSEric Anholt /**
82189d61fc0SDaniel Vetter  * drm_gem_object_free - free a GEM object
82289d61fc0SDaniel Vetter  * @kref: kref of the object to free
82389d61fc0SDaniel Vetter  *
824673a394bSEric Anholt  * Called after the last reference to the object has been lost.
825940eba2dSDaniel Vetter  * Must be called holding &drm_device.struct_mutex.
826673a394bSEric Anholt  *
827673a394bSEric Anholt  * Frees the object
828673a394bSEric Anholt  */
829673a394bSEric Anholt void
830673a394bSEric Anholt drm_gem_object_free(struct kref *kref)
831673a394bSEric Anholt {
8326ff774bdSDaniel Vetter 	struct drm_gem_object *obj =
8336ff774bdSDaniel Vetter 		container_of(kref, struct drm_gem_object, refcount);
834673a394bSEric Anholt 	struct drm_device *dev = obj->dev;
835673a394bSEric Anholt 
8366d3e7fddSDaniel Vetter 	if (dev->driver->gem_free_object_unlocked) {
8376d3e7fddSDaniel Vetter 		dev->driver->gem_free_object_unlocked(obj);
8386d3e7fddSDaniel Vetter 	} else if (dev->driver->gem_free_object) {
8398d77a940SDaniel Vetter 		WARN_ON(!mutex_is_locked(&dev->struct_mutex));
840673a394bSEric Anholt 
841673a394bSEric Anholt 		dev->driver->gem_free_object(obj);
842673a394bSEric Anholt 	}
8436d3e7fddSDaniel Vetter }
844673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free);
845673a394bSEric Anholt 
846df2e0900SDaniel Vetter /**
847e6b62714SThierry Reding  * drm_gem_object_put_unlocked - drop a GEM buffer object reference
8489f0ba539SDaniel Vetter  * @obj: GEM buffer object
8499f0ba539SDaniel Vetter  *
8509f0ba539SDaniel Vetter  * This releases a reference to @obj. Callers must not hold the
851940eba2dSDaniel Vetter  * &drm_device.struct_mutex lock when calling this function.
8529f0ba539SDaniel Vetter  *
853e6b62714SThierry Reding  * See also __drm_gem_object_put().
8549f0ba539SDaniel Vetter  */
8559f0ba539SDaniel Vetter void
856e6b62714SThierry Reding drm_gem_object_put_unlocked(struct drm_gem_object *obj)
8579f0ba539SDaniel Vetter {
8589f0ba539SDaniel Vetter 	struct drm_device *dev;
8599f0ba539SDaniel Vetter 
8609f0ba539SDaniel Vetter 	if (!obj)
8619f0ba539SDaniel Vetter 		return;
8629f0ba539SDaniel Vetter 
8639f0ba539SDaniel Vetter 	dev = obj->dev;
8649f0ba539SDaniel Vetter 
8653379c04cSDaniel Vetter 	if (dev->driver->gem_free_object_unlocked) {
8669f0ba539SDaniel Vetter 		kref_put(&obj->refcount, drm_gem_object_free);
8673379c04cSDaniel Vetter 	} else {
8683379c04cSDaniel Vetter 		might_lock(&dev->struct_mutex);
8693379c04cSDaniel Vetter 		if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
8709f0ba539SDaniel Vetter 				&dev->struct_mutex))
8719f0ba539SDaniel Vetter 			mutex_unlock(&dev->struct_mutex);
8729f0ba539SDaniel Vetter 	}
8733379c04cSDaniel Vetter }
874e6b62714SThierry Reding EXPORT_SYMBOL(drm_gem_object_put_unlocked);
8759f0ba539SDaniel Vetter 
8769f0ba539SDaniel Vetter /**
877e6b62714SThierry Reding  * drm_gem_object_put - release a GEM buffer object reference
8789f0ba539SDaniel Vetter  * @obj: GEM buffer object
8799f0ba539SDaniel Vetter  *
880940eba2dSDaniel Vetter  * This releases a reference to @obj. Callers must hold the
881940eba2dSDaniel Vetter  * &drm_device.struct_mutex lock when calling this function, even when the
882940eba2dSDaniel Vetter  * driver doesn't use &drm_device.struct_mutex for anything.
8839f0ba539SDaniel Vetter  *
8849f0ba539SDaniel Vetter  * For drivers not encumbered with legacy locking use
885e6b62714SThierry Reding  * drm_gem_object_put_unlocked() instead.
8869f0ba539SDaniel Vetter  */
8879f0ba539SDaniel Vetter void
888e6b62714SThierry Reding drm_gem_object_put(struct drm_gem_object *obj)
8899f0ba539SDaniel Vetter {
8909f0ba539SDaniel Vetter 	if (obj) {
8919f0ba539SDaniel Vetter 		WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
8929f0ba539SDaniel Vetter 
8939f0ba539SDaniel Vetter 		kref_put(&obj->refcount, drm_gem_object_free);
8949f0ba539SDaniel Vetter 	}
8959f0ba539SDaniel Vetter }
896e6b62714SThierry Reding EXPORT_SYMBOL(drm_gem_object_put);
8979f0ba539SDaniel Vetter 
8989f0ba539SDaniel Vetter /**
899df2e0900SDaniel Vetter  * drm_gem_vm_open - vma->ops->open implementation for GEM
900df2e0900SDaniel Vetter  * @vma: VM area structure
901df2e0900SDaniel Vetter  *
902df2e0900SDaniel Vetter  * This function implements the #vm_operations_struct open() callback for GEM
903df2e0900SDaniel Vetter  * drivers. This must be used together with drm_gem_vm_close().
904df2e0900SDaniel Vetter  */
905ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma)
906ab00b3e5SJesse Barnes {
907ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
908ab00b3e5SJesse Barnes 
909e6b62714SThierry Reding 	drm_gem_object_get(obj);
910ab00b3e5SJesse Barnes }
911ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open);
912ab00b3e5SJesse Barnes 
913df2e0900SDaniel Vetter /**
914df2e0900SDaniel Vetter  * drm_gem_vm_close - vma->ops->close implementation for GEM
915df2e0900SDaniel Vetter  * @vma: VM area structure
916df2e0900SDaniel Vetter  *
917df2e0900SDaniel Vetter  * This function implements the #vm_operations_struct close() callback for GEM
918df2e0900SDaniel Vetter  * drivers. This must be used together with drm_gem_vm_open().
919df2e0900SDaniel Vetter  */
920ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma)
921ab00b3e5SJesse Barnes {
922ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
923ab00b3e5SJesse Barnes 
924e6b62714SThierry Reding 	drm_gem_object_put_unlocked(obj);
925ab00b3e5SJesse Barnes }
926ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close);
927ab00b3e5SJesse Barnes 
9281c5aafa6SLaurent Pinchart /**
9291c5aafa6SLaurent Pinchart  * drm_gem_mmap_obj - memory map a GEM object
9301c5aafa6SLaurent Pinchart  * @obj: the GEM object to map
9311c5aafa6SLaurent Pinchart  * @obj_size: the object size to be mapped, in bytes
9321c5aafa6SLaurent Pinchart  * @vma: VMA for the area to be mapped
9331c5aafa6SLaurent Pinchart  *
9341c5aafa6SLaurent Pinchart  * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
9351c5aafa6SLaurent Pinchart  * provided by the driver. Depending on their requirements, drivers can either
9361c5aafa6SLaurent Pinchart  * provide a fault handler in their gem_vm_ops (in which case any accesses to
9371c5aafa6SLaurent Pinchart  * the object will be trapped, to perform migration, GTT binding, surface
9381c5aafa6SLaurent Pinchart  * register allocation, or performance monitoring), or mmap the buffer memory
9391c5aafa6SLaurent Pinchart  * synchronously after calling drm_gem_mmap_obj.
9401c5aafa6SLaurent Pinchart  *
9411c5aafa6SLaurent Pinchart  * This function is mainly intended to implement the DMABUF mmap operation, when
9421c5aafa6SLaurent Pinchart  * the GEM object is not looked up based on its fake offset. To implement the
9431c5aafa6SLaurent Pinchart  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
9441c5aafa6SLaurent Pinchart  *
945ca481c9bSDavid Herrmann  * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
946ca481c9bSDavid Herrmann  * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
947ca481c9bSDavid Herrmann  * callers must verify access restrictions before calling this helper.
948ca481c9bSDavid Herrmann  *
9491c5aafa6SLaurent Pinchart  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
9501c5aafa6SLaurent Pinchart  * size, or if no gem_vm_ops are provided.
9511c5aafa6SLaurent Pinchart  */
9521c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
9531c5aafa6SLaurent Pinchart 		     struct vm_area_struct *vma)
9541c5aafa6SLaurent Pinchart {
9551c5aafa6SLaurent Pinchart 	struct drm_device *dev = obj->dev;
9561c5aafa6SLaurent Pinchart 
9571c5aafa6SLaurent Pinchart 	/* Check for valid size. */
9581c5aafa6SLaurent Pinchart 	if (obj_size < vma->vm_end - vma->vm_start)
9591c5aafa6SLaurent Pinchart 		return -EINVAL;
9601c5aafa6SLaurent Pinchart 
9611c5aafa6SLaurent Pinchart 	if (!dev->driver->gem_vm_ops)
9621c5aafa6SLaurent Pinchart 		return -EINVAL;
9631c5aafa6SLaurent Pinchart 
9641c5aafa6SLaurent Pinchart 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
9651c5aafa6SLaurent Pinchart 	vma->vm_ops = dev->driver->gem_vm_ops;
9661c5aafa6SLaurent Pinchart 	vma->vm_private_data = obj;
9671c5aafa6SLaurent Pinchart 	vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
9681c5aafa6SLaurent Pinchart 
9691c5aafa6SLaurent Pinchart 	/* Take a ref for this mapping of the object, so that the fault
9701c5aafa6SLaurent Pinchart 	 * handler can dereference the mmap offset's pointer to the object.
9711c5aafa6SLaurent Pinchart 	 * This reference is cleaned up by the corresponding vm_close
9721c5aafa6SLaurent Pinchart 	 * (which should happen whether the vma was created by this call, or
9731c5aafa6SLaurent Pinchart 	 * by a vm_open due to mremap or partial unmap or whatever).
9741c5aafa6SLaurent Pinchart 	 */
975e6b62714SThierry Reding 	drm_gem_object_get(obj);
9761c5aafa6SLaurent Pinchart 
9771c5aafa6SLaurent Pinchart 	return 0;
9781c5aafa6SLaurent Pinchart }
9791c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj);
980ab00b3e5SJesse Barnes 
981a2c0a97bSJesse Barnes /**
982a2c0a97bSJesse Barnes  * drm_gem_mmap - memory map routine for GEM objects
983a2c0a97bSJesse Barnes  * @filp: DRM file pointer
984a2c0a97bSJesse Barnes  * @vma: VMA for the area to be mapped
985a2c0a97bSJesse Barnes  *
986a2c0a97bSJesse Barnes  * If a driver supports GEM object mapping, mmap calls on the DRM file
987a2c0a97bSJesse Barnes  * descriptor will end up here.
988a2c0a97bSJesse Barnes  *
9891c5aafa6SLaurent Pinchart  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
990a2c0a97bSJesse Barnes  * contain the fake offset we created when the GTT map ioctl was called on
9911c5aafa6SLaurent Pinchart  * the object) and map it with a call to drm_gem_mmap_obj().
992ca481c9bSDavid Herrmann  *
993ca481c9bSDavid Herrmann  * If the caller is not granted access to the buffer object, the mmap will fail
994ca481c9bSDavid Herrmann  * with EACCES. Please see the vma manager for more information.
995a2c0a97bSJesse Barnes  */
996a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
997a2c0a97bSJesse Barnes {
998a2c0a97bSJesse Barnes 	struct drm_file *priv = filp->private_data;
999a2c0a97bSJesse Barnes 	struct drm_device *dev = priv->minor->dev;
10002225cfe4SDaniel Vetter 	struct drm_gem_object *obj = NULL;
10010de23977SDavid Herrmann 	struct drm_vma_offset_node *node;
1002a8469aa8SDavid Herrmann 	int ret;
1003a2c0a97bSJesse Barnes 
1004c07dcd61SDaniel Vetter 	if (drm_dev_is_unplugged(dev))
10052c07a21dSDave Airlie 		return -ENODEV;
10062c07a21dSDave Airlie 
10072225cfe4SDaniel Vetter 	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
10082225cfe4SDaniel Vetter 	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1009b04a5906SDaniel Vetter 						  vma->vm_pgoff,
10100de23977SDavid Herrmann 						  vma_pages(vma));
10112225cfe4SDaniel Vetter 	if (likely(node)) {
10122225cfe4SDaniel Vetter 		obj = container_of(node, struct drm_gem_object, vma_node);
10132225cfe4SDaniel Vetter 		/*
10142225cfe4SDaniel Vetter 		 * When the object is being freed, after it hits 0-refcnt it
10152225cfe4SDaniel Vetter 		 * proceeds to tear down the object. In the process it will
10162225cfe4SDaniel Vetter 		 * attempt to remove the VMA offset and so acquire this
10172225cfe4SDaniel Vetter 		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
10182225cfe4SDaniel Vetter 		 * that matches our range, we know it is in the process of being
10192225cfe4SDaniel Vetter 		 * destroyed and will be freed as soon as we release the lock -
10202225cfe4SDaniel Vetter 		 * so we have to check for the 0-refcnted object and treat it as
10212225cfe4SDaniel Vetter 		 * invalid.
10222225cfe4SDaniel Vetter 		 */
10232225cfe4SDaniel Vetter 		if (!kref_get_unless_zero(&obj->refcount))
10242225cfe4SDaniel Vetter 			obj = NULL;
10252225cfe4SDaniel Vetter 	}
10262225cfe4SDaniel Vetter 	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
10272225cfe4SDaniel Vetter 
10282225cfe4SDaniel Vetter 	if (!obj)
1029197633b9SDaniel Vetter 		return -EINVAL;
10302225cfe4SDaniel Vetter 
1031d9a1f0b4SDavid Herrmann 	if (!drm_vma_node_is_allowed(node, priv)) {
1032e6b62714SThierry Reding 		drm_gem_object_put_unlocked(obj);
1033ca481c9bSDavid Herrmann 		return -EACCES;
1034a2c0a97bSJesse Barnes 	}
1035a2c0a97bSJesse Barnes 
10362225cfe4SDaniel Vetter 	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
10372225cfe4SDaniel Vetter 			       vma);
1038a2c0a97bSJesse Barnes 
1039e6b62714SThierry Reding 	drm_gem_object_put_unlocked(obj);
1040a2c0a97bSJesse Barnes 
1041a2c0a97bSJesse Barnes 	return ret;
1042a2c0a97bSJesse Barnes }
1043a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap);
1044