xref: /openbmc/linux/drivers/gpu/drm/drm_gem.c (revision 367bbd49202dd256dce1217c2f7cd0d5d1916f7b)
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>
41673a394bSEric Anholt 
42673a394bSEric Anholt /** @file drm_gem.c
43673a394bSEric Anholt  *
44673a394bSEric Anholt  * This file provides some of the base ioctls and library routines for
45673a394bSEric Anholt  * the graphics memory manager implemented by each device driver.
46673a394bSEric Anholt  *
47673a394bSEric Anholt  * Because various devices have different requirements in terms of
48673a394bSEric Anholt  * synchronization and migration strategies, implementing that is left up to
49673a394bSEric Anholt  * the driver, and all that the general API provides should be generic --
50673a394bSEric Anholt  * allocating objects, reading/writing data with the cpu, freeing objects.
51673a394bSEric Anholt  * Even there, platform-dependent optimizations for reading/writing data with
52673a394bSEric Anholt  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
53673a394bSEric Anholt  * the DRI2 implementation wants to have at least allocate/mmap be generic.
54673a394bSEric Anholt  *
55673a394bSEric Anholt  * The goal was to have swap-backed object allocation managed through
56673a394bSEric Anholt  * struct file.  However, file descriptors as handles to a struct file have
57673a394bSEric Anholt  * two major failings:
58673a394bSEric Anholt  * - Process limits prevent more than 1024 or so being used at a time by
59673a394bSEric Anholt  *   default.
60673a394bSEric Anholt  * - Inability to allocate high fds will aggravate the X Server's select()
61673a394bSEric Anholt  *   handling, and likely that of many GL client applications as well.
62673a394bSEric Anholt  *
63673a394bSEric Anholt  * This led to a plan of using our own integer IDs (called handles, following
64673a394bSEric Anholt  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
65673a394bSEric Anholt  * ioctls.  The objects themselves will still include the struct file so
66673a394bSEric Anholt  * that we can transition to fds if the required kernel infrastructure shows
67673a394bSEric Anholt  * up at a later date, and as our interface with shmfs for memory allocation.
68673a394bSEric Anholt  */
69673a394bSEric Anholt 
70a2c0a97bSJesse Barnes /*
71a2c0a97bSJesse Barnes  * We make up offsets for buffer objects so we can recognize them at
72a2c0a97bSJesse Barnes  * mmap time.
73a2c0a97bSJesse Barnes  */
7405269a3aSJordan Crouse 
7505269a3aSJordan Crouse /* pgoff in mmap is an unsigned long, so we need to make sure that
7605269a3aSJordan Crouse  * the faked up offset will fit
7705269a3aSJordan Crouse  */
7805269a3aSJordan Crouse 
7905269a3aSJordan Crouse #if BITS_PER_LONG == 64
80a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
81a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
8205269a3aSJordan Crouse #else
8305269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
8405269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
8505269a3aSJordan Crouse #endif
86a2c0a97bSJesse Barnes 
87673a394bSEric Anholt /**
88673a394bSEric Anholt  * Initialize the GEM device fields
89673a394bSEric Anholt  */
90673a394bSEric Anholt 
91673a394bSEric Anholt int
92673a394bSEric Anholt drm_gem_init(struct drm_device *dev)
93673a394bSEric Anholt {
94a2c0a97bSJesse Barnes 	struct drm_gem_mm *mm;
95a2c0a97bSJesse Barnes 
96673a394bSEric Anholt 	spin_lock_init(&dev->object_name_lock);
97673a394bSEric Anholt 	idr_init(&dev->object_name_idr);
98a2c0a97bSJesse Barnes 
999a298b2aSEric Anholt 	mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
100a2c0a97bSJesse Barnes 	if (!mm) {
101a2c0a97bSJesse Barnes 		DRM_ERROR("out of memory\n");
102a2c0a97bSJesse Barnes 		return -ENOMEM;
103a2c0a97bSJesse Barnes 	}
104a2c0a97bSJesse Barnes 
105a2c0a97bSJesse Barnes 	dev->mm_private = mm;
1060de23977SDavid Herrmann 	drm_vma_offset_manager_init(&mm->vma_manager,
1070de23977SDavid Herrmann 				    DRM_FILE_PAGE_OFFSET_START,
10877ef8bbcSDavid Herrmann 				    DRM_FILE_PAGE_OFFSET_SIZE);
109a2c0a97bSJesse Barnes 
110673a394bSEric Anholt 	return 0;
111673a394bSEric Anholt }
112673a394bSEric Anholt 
113a2c0a97bSJesse Barnes void
114a2c0a97bSJesse Barnes drm_gem_destroy(struct drm_device *dev)
115a2c0a97bSJesse Barnes {
116a2c0a97bSJesse Barnes 	struct drm_gem_mm *mm = dev->mm_private;
117a2c0a97bSJesse Barnes 
1180de23977SDavid Herrmann 	drm_vma_offset_manager_destroy(&mm->vma_manager);
1199a298b2aSEric Anholt 	kfree(mm);
120a2c0a97bSJesse Barnes 	dev->mm_private = NULL;
121a2c0a97bSJesse Barnes }
122a2c0a97bSJesse Barnes 
123673a394bSEric Anholt /**
12462cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
1251d397043SDaniel Vetter  * shmfs backing store.
1261d397043SDaniel Vetter  */
1271d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev,
1281d397043SDaniel Vetter 			struct drm_gem_object *obj, size_t size)
1291d397043SDaniel Vetter {
13089c8233fSDavid Herrmann 	struct file *filp;
1311d397043SDaniel Vetter 
13289c8233fSDavid Herrmann 	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
13389c8233fSDavid Herrmann 	if (IS_ERR(filp))
13489c8233fSDavid Herrmann 		return PTR_ERR(filp);
1351d397043SDaniel Vetter 
13689c8233fSDavid Herrmann 	drm_gem_private_object_init(dev, obj, size);
13789c8233fSDavid Herrmann 	obj->filp = filp;
1381d397043SDaniel Vetter 
1391d397043SDaniel Vetter 	return 0;
1401d397043SDaniel Vetter }
1411d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init);
1421d397043SDaniel Vetter 
1431d397043SDaniel Vetter /**
14462cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
14562cb7011SAlan Cox  * no GEM provided backing store. Instead the caller is responsible for
14662cb7011SAlan Cox  * backing the object and handling it.
14762cb7011SAlan Cox  */
14889c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev,
14962cb7011SAlan Cox 				 struct drm_gem_object *obj, size_t size)
15062cb7011SAlan Cox {
15162cb7011SAlan Cox 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
15262cb7011SAlan Cox 
15362cb7011SAlan Cox 	obj->dev = dev;
15462cb7011SAlan Cox 	obj->filp = NULL;
15562cb7011SAlan Cox 
15662cb7011SAlan Cox 	kref_init(&obj->refcount);
15762cb7011SAlan Cox 	atomic_set(&obj->handle_count, 0);
15862cb7011SAlan Cox 	obj->size = size;
15962cb7011SAlan Cox }
16062cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init);
16162cb7011SAlan Cox 
16262cb7011SAlan Cox /**
163673a394bSEric Anholt  * Allocate a GEM object of the specified size with shmfs backing store
164673a394bSEric Anholt  */
165673a394bSEric Anholt struct drm_gem_object *
166673a394bSEric Anholt drm_gem_object_alloc(struct drm_device *dev, size_t size)
167673a394bSEric Anholt {
168673a394bSEric Anholt 	struct drm_gem_object *obj;
169673a394bSEric Anholt 
170b798b1feSRobert P. J. Day 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
171845792d9SJiri Slaby 	if (!obj)
172845792d9SJiri Slaby 		goto free;
173673a394bSEric Anholt 
1741d397043SDaniel Vetter 	if (drm_gem_object_init(dev, obj, size) != 0)
175845792d9SJiri Slaby 		goto free;
176673a394bSEric Anholt 
177673a394bSEric Anholt 	if (dev->driver->gem_init_object != NULL &&
178673a394bSEric Anholt 	    dev->driver->gem_init_object(obj) != 0) {
179845792d9SJiri Slaby 		goto fput;
180673a394bSEric Anholt 	}
181673a394bSEric Anholt 	return obj;
182845792d9SJiri Slaby fput:
1831d397043SDaniel Vetter 	/* Object_init mangles the global counters - readjust them. */
184845792d9SJiri Slaby 	fput(obj->filp);
185845792d9SJiri Slaby free:
186845792d9SJiri Slaby 	kfree(obj);
187845792d9SJiri Slaby 	return NULL;
188673a394bSEric Anholt }
189673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_alloc);
190673a394bSEric Anholt 
1910ff926c7SDave Airlie static void
1920ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
1930ff926c7SDave Airlie {
1940ff926c7SDave Airlie 	if (obj->import_attach) {
195219b4733SDave Airlie 		drm_prime_remove_buf_handle(&filp->prime,
1960ff926c7SDave Airlie 				obj->import_attach->dmabuf);
1970ff926c7SDave Airlie 	}
1980ff926c7SDave Airlie 	if (obj->export_dma_buf) {
199219b4733SDave Airlie 		drm_prime_remove_buf_handle(&filp->prime,
2000ff926c7SDave Airlie 				obj->export_dma_buf);
2010ff926c7SDave Airlie 	}
2020ff926c7SDave Airlie }
2030ff926c7SDave Airlie 
204673a394bSEric Anholt /**
205673a394bSEric Anholt  * Removes the mapping from handle to filp for this object.
206673a394bSEric Anholt  */
207ff72145bSDave Airlie int
208a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle)
209673a394bSEric Anholt {
210673a394bSEric Anholt 	struct drm_device *dev;
211673a394bSEric Anholt 	struct drm_gem_object *obj;
212673a394bSEric Anholt 
213673a394bSEric Anholt 	/* This is gross. The idr system doesn't let us try a delete and
214673a394bSEric Anholt 	 * return an error code.  It just spews if you fail at deleting.
215673a394bSEric Anholt 	 * So, we have to grab a lock around finding the object and then
216673a394bSEric Anholt 	 * doing the delete on it and dropping the refcount, or the user
217673a394bSEric Anholt 	 * could race us to double-decrement the refcount and cause a
218673a394bSEric Anholt 	 * use-after-free later.  Given the frequency of our handle lookups,
219673a394bSEric Anholt 	 * we may want to use ida for number allocation and a hash table
220673a394bSEric Anholt 	 * for the pointers, anyway.
221673a394bSEric Anholt 	 */
222673a394bSEric Anholt 	spin_lock(&filp->table_lock);
223673a394bSEric Anholt 
224673a394bSEric Anholt 	/* Check if we currently have a reference on the object */
225673a394bSEric Anholt 	obj = idr_find(&filp->object_idr, handle);
226673a394bSEric Anholt 	if (obj == NULL) {
227673a394bSEric Anholt 		spin_unlock(&filp->table_lock);
228673a394bSEric Anholt 		return -EINVAL;
229673a394bSEric Anholt 	}
230673a394bSEric Anholt 	dev = obj->dev;
231673a394bSEric Anholt 
232673a394bSEric Anholt 	/* Release reference and decrement refcount. */
233673a394bSEric Anholt 	idr_remove(&filp->object_idr, handle);
234673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
235673a394bSEric Anholt 
2360ff926c7SDave Airlie 	drm_gem_remove_prime_handles(obj, filp);
2373248877eSDave Airlie 
238304eda32SBen Skeggs 	if (dev->driver->gem_close_object)
239304eda32SBen Skeggs 		dev->driver->gem_close_object(obj, filp);
240bc9025bdSLuca Barbieri 	drm_gem_object_handle_unreference_unlocked(obj);
241673a394bSEric Anholt 
242673a394bSEric Anholt 	return 0;
243673a394bSEric Anholt }
244ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete);
245673a394bSEric Anholt 
246673a394bSEric Anholt /**
24743387b37SDaniel Vetter  * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
24843387b37SDaniel Vetter  *
24943387b37SDaniel Vetter  * This implements the ->dumb_destroy kms driver callback for drivers which use
25043387b37SDaniel Vetter  * gem to manage their backing storage.
25143387b37SDaniel Vetter  */
25243387b37SDaniel Vetter int drm_gem_dumb_destroy(struct drm_file *file,
25343387b37SDaniel Vetter 			 struct drm_device *dev,
25443387b37SDaniel Vetter 			 uint32_t handle)
25543387b37SDaniel Vetter {
25643387b37SDaniel Vetter 	return drm_gem_handle_delete(file, handle);
25743387b37SDaniel Vetter }
25843387b37SDaniel Vetter EXPORT_SYMBOL(drm_gem_dumb_destroy);
25943387b37SDaniel Vetter 
26043387b37SDaniel Vetter /**
261673a394bSEric Anholt  * Create a handle for this object. This adds a handle reference
262673a394bSEric Anholt  * to the object, which includes a regular reference count. Callers
263673a394bSEric Anholt  * will likely want to dereference the object afterwards.
264673a394bSEric Anholt  */
265673a394bSEric Anholt int
266673a394bSEric Anholt drm_gem_handle_create(struct drm_file *file_priv,
267673a394bSEric Anholt 		       struct drm_gem_object *obj,
268a1a2d1d3SPekka Paalanen 		       u32 *handlep)
269673a394bSEric Anholt {
270304eda32SBen Skeggs 	struct drm_device *dev = obj->dev;
271673a394bSEric Anholt 	int ret;
272673a394bSEric Anholt 
273673a394bSEric Anholt 	/*
2742e928815STejun Heo 	 * Get the user-visible handle using idr.  Preload and perform
2752e928815STejun Heo 	 * allocation under our spinlock.
276673a394bSEric Anholt 	 */
2772e928815STejun Heo 	idr_preload(GFP_KERNEL);
278673a394bSEric Anholt 	spin_lock(&file_priv->table_lock);
2792e928815STejun Heo 
2802e928815STejun Heo 	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
2812e928815STejun Heo 
282673a394bSEric Anholt 	spin_unlock(&file_priv->table_lock);
2832e928815STejun Heo 	idr_preload_end();
2842e928815STejun Heo 	if (ret < 0)
285673a394bSEric Anholt 		return ret;
2862e928815STejun Heo 	*handlep = ret;
287673a394bSEric Anholt 
288673a394bSEric Anholt 	drm_gem_object_handle_reference(obj);
289304eda32SBen Skeggs 
290304eda32SBen Skeggs 	if (dev->driver->gem_open_object) {
291304eda32SBen Skeggs 		ret = dev->driver->gem_open_object(obj, file_priv);
292304eda32SBen Skeggs 		if (ret) {
293304eda32SBen Skeggs 			drm_gem_handle_delete(file_priv, *handlep);
294304eda32SBen Skeggs 			return ret;
295304eda32SBen Skeggs 		}
296304eda32SBen Skeggs 	}
297304eda32SBen Skeggs 
298673a394bSEric Anholt 	return 0;
299673a394bSEric Anholt }
300673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create);
301673a394bSEric Anholt 
30275ef8b3bSRob Clark 
30375ef8b3bSRob Clark /**
30475ef8b3bSRob Clark  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
30575ef8b3bSRob Clark  * @obj: obj in question
30675ef8b3bSRob Clark  *
30775ef8b3bSRob Clark  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
30875ef8b3bSRob Clark  */
30975ef8b3bSRob Clark void
31075ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj)
31175ef8b3bSRob Clark {
31275ef8b3bSRob Clark 	struct drm_device *dev = obj->dev;
31375ef8b3bSRob Clark 	struct drm_gem_mm *mm = dev->mm_private;
31475ef8b3bSRob Clark 
3150de23977SDavid Herrmann 	drm_vma_offset_remove(&mm->vma_manager, &obj->vma_node);
31675ef8b3bSRob Clark }
31775ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset);
31875ef8b3bSRob Clark 
31975ef8b3bSRob Clark /**
320*367bbd49SRob Clark  * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
321*367bbd49SRob Clark  * @obj: obj in question
322*367bbd49SRob Clark  * @size: the virtual size
323*367bbd49SRob Clark  *
324*367bbd49SRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
325*367bbd49SRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
326*367bbd49SRob Clark  * up the object based on the offset and sets up the various memory mapping
327*367bbd49SRob Clark  * structures.
328*367bbd49SRob Clark  *
329*367bbd49SRob Clark  * This routine allocates and attaches a fake offset for @obj, in cases where
330*367bbd49SRob Clark  * the virtual size differs from the physical size (ie. obj->size).  Otherwise
331*367bbd49SRob Clark  * just use drm_gem_create_mmap_offset().
332*367bbd49SRob Clark  */
333*367bbd49SRob Clark int
334*367bbd49SRob Clark drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
335*367bbd49SRob Clark {
336*367bbd49SRob Clark 	struct drm_device *dev = obj->dev;
337*367bbd49SRob Clark 	struct drm_gem_mm *mm = dev->mm_private;
338*367bbd49SRob Clark 
339*367bbd49SRob Clark 	return drm_vma_offset_add(&mm->vma_manager, &obj->vma_node,
340*367bbd49SRob Clark 				  size / PAGE_SIZE);
341*367bbd49SRob Clark }
342*367bbd49SRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
343*367bbd49SRob Clark 
344*367bbd49SRob Clark /**
34575ef8b3bSRob Clark  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
34675ef8b3bSRob Clark  * @obj: obj in question
34775ef8b3bSRob Clark  *
34875ef8b3bSRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
34975ef8b3bSRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
35075ef8b3bSRob Clark  * up the object based on the offset and sets up the various memory mapping
35175ef8b3bSRob Clark  * structures.
35275ef8b3bSRob Clark  *
35375ef8b3bSRob Clark  * This routine allocates and attaches a fake offset for @obj.
35475ef8b3bSRob Clark  */
355*367bbd49SRob Clark int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
35675ef8b3bSRob Clark {
357*367bbd49SRob Clark 	return drm_gem_create_mmap_offset_size(obj, obj->size);
35875ef8b3bSRob Clark }
35975ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset);
36075ef8b3bSRob Clark 
361673a394bSEric Anholt /** Returns a reference to the object named by the handle. */
362673a394bSEric Anholt struct drm_gem_object *
363673a394bSEric Anholt drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
364a1a2d1d3SPekka Paalanen 		      u32 handle)
365673a394bSEric Anholt {
366673a394bSEric Anholt 	struct drm_gem_object *obj;
367673a394bSEric Anholt 
368673a394bSEric Anholt 	spin_lock(&filp->table_lock);
369673a394bSEric Anholt 
370673a394bSEric Anholt 	/* Check if we currently have a reference on the object */
371673a394bSEric Anholt 	obj = idr_find(&filp->object_idr, handle);
372673a394bSEric Anholt 	if (obj == NULL) {
373673a394bSEric Anholt 		spin_unlock(&filp->table_lock);
374673a394bSEric Anholt 		return NULL;
375673a394bSEric Anholt 	}
376673a394bSEric Anholt 
377673a394bSEric Anholt 	drm_gem_object_reference(obj);
378673a394bSEric Anholt 
379673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
380673a394bSEric Anholt 
381673a394bSEric Anholt 	return obj;
382673a394bSEric Anholt }
383673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup);
384673a394bSEric Anholt 
385673a394bSEric Anholt /**
386673a394bSEric Anholt  * Releases the handle to an mm object.
387673a394bSEric Anholt  */
388673a394bSEric Anholt int
389673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data,
390673a394bSEric Anholt 		    struct drm_file *file_priv)
391673a394bSEric Anholt {
392673a394bSEric Anholt 	struct drm_gem_close *args = data;
393673a394bSEric Anholt 	int ret;
394673a394bSEric Anholt 
395673a394bSEric Anholt 	if (!(dev->driver->driver_features & DRIVER_GEM))
396673a394bSEric Anholt 		return -ENODEV;
397673a394bSEric Anholt 
398673a394bSEric Anholt 	ret = drm_gem_handle_delete(file_priv, args->handle);
399673a394bSEric Anholt 
400673a394bSEric Anholt 	return ret;
401673a394bSEric Anholt }
402673a394bSEric Anholt 
403673a394bSEric Anholt /**
404673a394bSEric Anholt  * Create a global name for an object, returning the name.
405673a394bSEric Anholt  *
406673a394bSEric Anholt  * Note that the name does not hold a reference; when the object
407673a394bSEric Anholt  * is freed, the name goes away.
408673a394bSEric Anholt  */
409673a394bSEric Anholt int
410673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data,
411673a394bSEric Anholt 		    struct drm_file *file_priv)
412673a394bSEric Anholt {
413673a394bSEric Anholt 	struct drm_gem_flink *args = data;
414673a394bSEric Anholt 	struct drm_gem_object *obj;
415673a394bSEric Anholt 	int ret;
416673a394bSEric Anholt 
417673a394bSEric Anholt 	if (!(dev->driver->driver_features & DRIVER_GEM))
418673a394bSEric Anholt 		return -ENODEV;
419673a394bSEric Anholt 
420673a394bSEric Anholt 	obj = drm_gem_object_lookup(dev, file_priv, args->handle);
421673a394bSEric Anholt 	if (obj == NULL)
422bf79cb91SChris Wilson 		return -ENOENT;
423673a394bSEric Anholt 
4242e928815STejun Heo 	idr_preload(GFP_KERNEL);
425673a394bSEric Anholt 	spin_lock(&dev->object_name_lock);
4268d59bae5SChris Wilson 	if (!obj->name) {
4272e928815STejun Heo 		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
4282e928815STejun Heo 		if (ret < 0)
4293e49c4f4SChris Wilson 			goto err;
4302e07fb22SYoungJun Cho 
4312e07fb22SYoungJun Cho 		obj->name = ret;
432673a394bSEric Anholt 
4338d59bae5SChris Wilson 		/* Allocate a reference for the name table.  */
4348d59bae5SChris Wilson 		drm_gem_object_reference(obj);
4358d59bae5SChris Wilson 	}
4363e49c4f4SChris Wilson 
4372e07fb22SYoungJun Cho 	args->name = (uint64_t) obj->name;
4382e07fb22SYoungJun Cho 	ret = 0;
4392e07fb22SYoungJun Cho 
4403e49c4f4SChris Wilson err:
4412e07fb22SYoungJun Cho 	spin_unlock(&dev->object_name_lock);
4422e07fb22SYoungJun Cho 	idr_preload_end();
443bc9025bdSLuca Barbieri 	drm_gem_object_unreference_unlocked(obj);
4443e49c4f4SChris Wilson 	return ret;
445673a394bSEric Anholt }
446673a394bSEric Anholt 
447673a394bSEric Anholt /**
448673a394bSEric Anholt  * Open an object using the global name, returning a handle and the size.
449673a394bSEric Anholt  *
450673a394bSEric Anholt  * This handle (of course) holds a reference to the object, so the object
451673a394bSEric Anholt  * will not go away until the handle is deleted.
452673a394bSEric Anholt  */
453673a394bSEric Anholt int
454673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data,
455673a394bSEric Anholt 		   struct drm_file *file_priv)
456673a394bSEric Anholt {
457673a394bSEric Anholt 	struct drm_gem_open *args = data;
458673a394bSEric Anholt 	struct drm_gem_object *obj;
459673a394bSEric Anholt 	int ret;
460a1a2d1d3SPekka Paalanen 	u32 handle;
461673a394bSEric Anholt 
462673a394bSEric Anholt 	if (!(dev->driver->driver_features & DRIVER_GEM))
463673a394bSEric Anholt 		return -ENODEV;
464673a394bSEric Anholt 
465673a394bSEric Anholt 	spin_lock(&dev->object_name_lock);
466673a394bSEric Anholt 	obj = idr_find(&dev->object_name_idr, (int) args->name);
467673a394bSEric Anholt 	if (obj)
468673a394bSEric Anholt 		drm_gem_object_reference(obj);
469673a394bSEric Anholt 	spin_unlock(&dev->object_name_lock);
470673a394bSEric Anholt 	if (!obj)
471673a394bSEric Anholt 		return -ENOENT;
472673a394bSEric Anholt 
473673a394bSEric Anholt 	ret = drm_gem_handle_create(file_priv, obj, &handle);
474bc9025bdSLuca Barbieri 	drm_gem_object_unreference_unlocked(obj);
475673a394bSEric Anholt 	if (ret)
476673a394bSEric Anholt 		return ret;
477673a394bSEric Anholt 
478673a394bSEric Anholt 	args->handle = handle;
479673a394bSEric Anholt 	args->size = obj->size;
480673a394bSEric Anholt 
481673a394bSEric Anholt 	return 0;
482673a394bSEric Anholt }
483673a394bSEric Anholt 
484673a394bSEric Anholt /**
485673a394bSEric Anholt  * Called at device open time, sets up the structure for handling refcounting
486673a394bSEric Anholt  * of mm objects.
487673a394bSEric Anholt  */
488673a394bSEric Anholt void
489673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
490673a394bSEric Anholt {
491673a394bSEric Anholt 	idr_init(&file_private->object_idr);
492673a394bSEric Anholt 	spin_lock_init(&file_private->table_lock);
493673a394bSEric Anholt }
494673a394bSEric Anholt 
495673a394bSEric Anholt /**
496673a394bSEric Anholt  * Called at device close to release the file's
497673a394bSEric Anholt  * handle references on objects.
498673a394bSEric Anholt  */
499673a394bSEric Anholt static int
500673a394bSEric Anholt drm_gem_object_release_handle(int id, void *ptr, void *data)
501673a394bSEric Anholt {
502304eda32SBen Skeggs 	struct drm_file *file_priv = data;
503673a394bSEric Anholt 	struct drm_gem_object *obj = ptr;
504304eda32SBen Skeggs 	struct drm_device *dev = obj->dev;
505304eda32SBen Skeggs 
5060ff926c7SDave Airlie 	drm_gem_remove_prime_handles(obj, file_priv);
5073248877eSDave Airlie 
508304eda32SBen Skeggs 	if (dev->driver->gem_close_object)
509304eda32SBen Skeggs 		dev->driver->gem_close_object(obj, file_priv);
510673a394bSEric Anholt 
511bc9025bdSLuca Barbieri 	drm_gem_object_handle_unreference_unlocked(obj);
512673a394bSEric Anholt 
513673a394bSEric Anholt 	return 0;
514673a394bSEric Anholt }
515673a394bSEric Anholt 
516673a394bSEric Anholt /**
517673a394bSEric Anholt  * Called at close time when the filp is going away.
518673a394bSEric Anholt  *
519673a394bSEric Anholt  * Releases any remaining references on objects by this filp.
520673a394bSEric Anholt  */
521673a394bSEric Anholt void
522673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
523673a394bSEric Anholt {
524673a394bSEric Anholt 	idr_for_each(&file_private->object_idr,
525304eda32SBen Skeggs 		     &drm_gem_object_release_handle, file_private);
526673a394bSEric Anholt 	idr_destroy(&file_private->object_idr);
527673a394bSEric Anholt }
528673a394bSEric Anholt 
529fd632aa3SDaniel Vetter void
530fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj)
531c3ae90c0SLuca Barbieri {
53262cb7011SAlan Cox 	if (obj->filp)
533c3ae90c0SLuca Barbieri 	    fput(obj->filp);
534c3ae90c0SLuca Barbieri }
535fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release);
536c3ae90c0SLuca Barbieri 
537673a394bSEric Anholt /**
538673a394bSEric Anholt  * Called after the last reference to the object has been lost.
539c3ae90c0SLuca Barbieri  * Must be called holding struct_ mutex
540673a394bSEric Anholt  *
541673a394bSEric Anholt  * Frees the object
542673a394bSEric Anholt  */
543673a394bSEric Anholt void
544673a394bSEric Anholt drm_gem_object_free(struct kref *kref)
545673a394bSEric Anholt {
546673a394bSEric Anholt 	struct drm_gem_object *obj = (struct drm_gem_object *) kref;
547673a394bSEric Anholt 	struct drm_device *dev = obj->dev;
548673a394bSEric Anholt 
549673a394bSEric Anholt 	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
550673a394bSEric Anholt 
551673a394bSEric Anholt 	if (dev->driver->gem_free_object != NULL)
552673a394bSEric Anholt 		dev->driver->gem_free_object(obj);
553673a394bSEric Anholt }
554673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free);
555673a394bSEric Anholt 
556c3ae90c0SLuca Barbieri static void drm_gem_object_ref_bug(struct kref *list_kref)
557c3ae90c0SLuca Barbieri {
558c3ae90c0SLuca Barbieri 	BUG();
559c3ae90c0SLuca Barbieri }
560c3ae90c0SLuca Barbieri 
561c3ae90c0SLuca Barbieri /**
562673a394bSEric Anholt  * Called after the last handle to the object has been closed
563673a394bSEric Anholt  *
564673a394bSEric Anholt  * Removes any name for the object. Note that this must be
565673a394bSEric Anholt  * called before drm_gem_object_free or we'll be touching
566673a394bSEric Anholt  * freed memory
567673a394bSEric Anholt  */
56829d08b3eSDave Airlie void drm_gem_object_handle_free(struct drm_gem_object *obj)
569673a394bSEric Anholt {
570673a394bSEric Anholt 	struct drm_device *dev = obj->dev;
571673a394bSEric Anholt 
572673a394bSEric Anholt 	/* Remove any name for this object */
573673a394bSEric Anholt 	spin_lock(&dev->object_name_lock);
574673a394bSEric Anholt 	if (obj->name) {
575673a394bSEric Anholt 		idr_remove(&dev->object_name_idr, obj->name);
5768d59bae5SChris Wilson 		obj->name = 0;
577673a394bSEric Anholt 		spin_unlock(&dev->object_name_lock);
578673a394bSEric Anholt 		/*
579673a394bSEric Anholt 		 * The object name held a reference to this object, drop
580673a394bSEric Anholt 		 * that now.
581c3ae90c0SLuca Barbieri 		*
582c3ae90c0SLuca Barbieri 		* This cannot be the last reference, since the handle holds one too.
583673a394bSEric Anholt 		 */
584c3ae90c0SLuca Barbieri 		kref_put(&obj->refcount, drm_gem_object_ref_bug);
585673a394bSEric Anholt 	} else
586673a394bSEric Anholt 		spin_unlock(&dev->object_name_lock);
587673a394bSEric Anholt 
588673a394bSEric Anholt }
589673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_handle_free);
590673a394bSEric Anholt 
591ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma)
592ab00b3e5SJesse Barnes {
593ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
594ab00b3e5SJesse Barnes 
595ab00b3e5SJesse Barnes 	drm_gem_object_reference(obj);
59631dfbc93SChris Wilson 
59731dfbc93SChris Wilson 	mutex_lock(&obj->dev->struct_mutex);
598b06d66beSRob Clark 	drm_vm_open_locked(obj->dev, vma);
59931dfbc93SChris Wilson 	mutex_unlock(&obj->dev->struct_mutex);
600ab00b3e5SJesse Barnes }
601ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open);
602ab00b3e5SJesse Barnes 
603ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma)
604ab00b3e5SJesse Barnes {
605ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
606b74ad5aeSChris Wilson 	struct drm_device *dev = obj->dev;
607ab00b3e5SJesse Barnes 
608b74ad5aeSChris Wilson 	mutex_lock(&dev->struct_mutex);
609b06d66beSRob Clark 	drm_vm_close_locked(obj->dev, vma);
61031dfbc93SChris Wilson 	drm_gem_object_unreference(obj);
611b74ad5aeSChris Wilson 	mutex_unlock(&dev->struct_mutex);
612ab00b3e5SJesse Barnes }
613ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close);
614ab00b3e5SJesse Barnes 
6151c5aafa6SLaurent Pinchart /**
6161c5aafa6SLaurent Pinchart  * drm_gem_mmap_obj - memory map a GEM object
6171c5aafa6SLaurent Pinchart  * @obj: the GEM object to map
6181c5aafa6SLaurent Pinchart  * @obj_size: the object size to be mapped, in bytes
6191c5aafa6SLaurent Pinchart  * @vma: VMA for the area to be mapped
6201c5aafa6SLaurent Pinchart  *
6211c5aafa6SLaurent Pinchart  * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
6221c5aafa6SLaurent Pinchart  * provided by the driver. Depending on their requirements, drivers can either
6231c5aafa6SLaurent Pinchart  * provide a fault handler in their gem_vm_ops (in which case any accesses to
6241c5aafa6SLaurent Pinchart  * the object will be trapped, to perform migration, GTT binding, surface
6251c5aafa6SLaurent Pinchart  * register allocation, or performance monitoring), or mmap the buffer memory
6261c5aafa6SLaurent Pinchart  * synchronously after calling drm_gem_mmap_obj.
6271c5aafa6SLaurent Pinchart  *
6281c5aafa6SLaurent Pinchart  * This function is mainly intended to implement the DMABUF mmap operation, when
6291c5aafa6SLaurent Pinchart  * the GEM object is not looked up based on its fake offset. To implement the
6301c5aafa6SLaurent Pinchart  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
6311c5aafa6SLaurent Pinchart  *
6324368dd84SYoungJun Cho  * NOTE: This function has to be protected with dev->struct_mutex
6334368dd84SYoungJun Cho  *
6341c5aafa6SLaurent Pinchart  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
6351c5aafa6SLaurent Pinchart  * size, or if no gem_vm_ops are provided.
6361c5aafa6SLaurent Pinchart  */
6371c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
6381c5aafa6SLaurent Pinchart 		     struct vm_area_struct *vma)
6391c5aafa6SLaurent Pinchart {
6401c5aafa6SLaurent Pinchart 	struct drm_device *dev = obj->dev;
6411c5aafa6SLaurent Pinchart 
6424368dd84SYoungJun Cho 	lockdep_assert_held(&dev->struct_mutex);
6434368dd84SYoungJun Cho 
6441c5aafa6SLaurent Pinchart 	/* Check for valid size. */
6451c5aafa6SLaurent Pinchart 	if (obj_size < vma->vm_end - vma->vm_start)
6461c5aafa6SLaurent Pinchart 		return -EINVAL;
6471c5aafa6SLaurent Pinchart 
6481c5aafa6SLaurent Pinchart 	if (!dev->driver->gem_vm_ops)
6491c5aafa6SLaurent Pinchart 		return -EINVAL;
6501c5aafa6SLaurent Pinchart 
6511c5aafa6SLaurent Pinchart 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
6521c5aafa6SLaurent Pinchart 	vma->vm_ops = dev->driver->gem_vm_ops;
6531c5aafa6SLaurent Pinchart 	vma->vm_private_data = obj;
6541c5aafa6SLaurent Pinchart 	vma->vm_page_prot =  pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
6551c5aafa6SLaurent Pinchart 
6561c5aafa6SLaurent Pinchart 	/* Take a ref for this mapping of the object, so that the fault
6571c5aafa6SLaurent Pinchart 	 * handler can dereference the mmap offset's pointer to the object.
6581c5aafa6SLaurent Pinchart 	 * This reference is cleaned up by the corresponding vm_close
6591c5aafa6SLaurent Pinchart 	 * (which should happen whether the vma was created by this call, or
6601c5aafa6SLaurent Pinchart 	 * by a vm_open due to mremap or partial unmap or whatever).
6611c5aafa6SLaurent Pinchart 	 */
6621c5aafa6SLaurent Pinchart 	drm_gem_object_reference(obj);
6631c5aafa6SLaurent Pinchart 
6641c5aafa6SLaurent Pinchart 	drm_vm_open_locked(dev, vma);
6651c5aafa6SLaurent Pinchart 	return 0;
6661c5aafa6SLaurent Pinchart }
6671c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj);
668ab00b3e5SJesse Barnes 
669a2c0a97bSJesse Barnes /**
670a2c0a97bSJesse Barnes  * drm_gem_mmap - memory map routine for GEM objects
671a2c0a97bSJesse Barnes  * @filp: DRM file pointer
672a2c0a97bSJesse Barnes  * @vma: VMA for the area to be mapped
673a2c0a97bSJesse Barnes  *
674a2c0a97bSJesse Barnes  * If a driver supports GEM object mapping, mmap calls on the DRM file
675a2c0a97bSJesse Barnes  * descriptor will end up here.
676a2c0a97bSJesse Barnes  *
6771c5aafa6SLaurent Pinchart  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
678a2c0a97bSJesse Barnes  * contain the fake offset we created when the GTT map ioctl was called on
6791c5aafa6SLaurent Pinchart  * the object) and map it with a call to drm_gem_mmap_obj().
680a2c0a97bSJesse Barnes  */
681a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
682a2c0a97bSJesse Barnes {
683a2c0a97bSJesse Barnes 	struct drm_file *priv = filp->private_data;
684a2c0a97bSJesse Barnes 	struct drm_device *dev = priv->minor->dev;
685a2c0a97bSJesse Barnes 	struct drm_gem_mm *mm = dev->mm_private;
6860de23977SDavid Herrmann 	struct drm_gem_object *obj;
6870de23977SDavid Herrmann 	struct drm_vma_offset_node *node;
688a2c0a97bSJesse Barnes 	int ret = 0;
689a2c0a97bSJesse Barnes 
6902c07a21dSDave Airlie 	if (drm_device_is_unplugged(dev))
6912c07a21dSDave Airlie 		return -ENODEV;
6922c07a21dSDave Airlie 
693a2c0a97bSJesse Barnes 	mutex_lock(&dev->struct_mutex);
694a2c0a97bSJesse Barnes 
6950de23977SDavid Herrmann 	node = drm_vma_offset_exact_lookup(&mm->vma_manager, vma->vm_pgoff,
6960de23977SDavid Herrmann 					   vma_pages(vma));
6970de23977SDavid Herrmann 	if (!node) {
698a2c0a97bSJesse Barnes 		mutex_unlock(&dev->struct_mutex);
699a2c0a97bSJesse Barnes 		return drm_mmap(filp, vma);
700a2c0a97bSJesse Barnes 	}
701a2c0a97bSJesse Barnes 
7020de23977SDavid Herrmann 	obj = container_of(node, struct drm_gem_object, vma_node);
703aed2c03cSDavid Herrmann 	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma);
704a2c0a97bSJesse Barnes 
705a2c0a97bSJesse Barnes 	mutex_unlock(&dev->struct_mutex);
706a2c0a97bSJesse Barnes 
707a2c0a97bSJesse Barnes 	return ret;
708a2c0a97bSJesse Barnes }
709a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap);
710