xref: /openbmc/linux/drivers/gpu/drm/drm_gem.c (revision 0ff926c7d4f06f9703226dc09acad17e86f169d6)
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>
39673a394bSEric Anholt #include "drmP.h"
40673a394bSEric Anholt 
41673a394bSEric Anholt /** @file drm_gem.c
42673a394bSEric Anholt  *
43673a394bSEric Anholt  * This file provides some of the base ioctls and library routines for
44673a394bSEric Anholt  * the graphics memory manager implemented by each device driver.
45673a394bSEric Anholt  *
46673a394bSEric Anholt  * Because various devices have different requirements in terms of
47673a394bSEric Anholt  * synchronization and migration strategies, implementing that is left up to
48673a394bSEric Anholt  * the driver, and all that the general API provides should be generic --
49673a394bSEric Anholt  * allocating objects, reading/writing data with the cpu, freeing objects.
50673a394bSEric Anholt  * Even there, platform-dependent optimizations for reading/writing data with
51673a394bSEric Anholt  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
52673a394bSEric Anholt  * the DRI2 implementation wants to have at least allocate/mmap be generic.
53673a394bSEric Anholt  *
54673a394bSEric Anholt  * The goal was to have swap-backed object allocation managed through
55673a394bSEric Anholt  * struct file.  However, file descriptors as handles to a struct file have
56673a394bSEric Anholt  * two major failings:
57673a394bSEric Anholt  * - Process limits prevent more than 1024 or so being used at a time by
58673a394bSEric Anholt  *   default.
59673a394bSEric Anholt  * - Inability to allocate high fds will aggravate the X Server's select()
60673a394bSEric Anholt  *   handling, and likely that of many GL client applications as well.
61673a394bSEric Anholt  *
62673a394bSEric Anholt  * This led to a plan of using our own integer IDs (called handles, following
63673a394bSEric Anholt  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
64673a394bSEric Anholt  * ioctls.  The objects themselves will still include the struct file so
65673a394bSEric Anholt  * that we can transition to fds if the required kernel infrastructure shows
66673a394bSEric Anholt  * up at a later date, and as our interface with shmfs for memory allocation.
67673a394bSEric Anholt  */
68673a394bSEric Anholt 
69a2c0a97bSJesse Barnes /*
70a2c0a97bSJesse Barnes  * We make up offsets for buffer objects so we can recognize them at
71a2c0a97bSJesse Barnes  * mmap time.
72a2c0a97bSJesse Barnes  */
7305269a3aSJordan Crouse 
7405269a3aSJordan Crouse /* pgoff in mmap is an unsigned long, so we need to make sure that
7505269a3aSJordan Crouse  * the faked up offset will fit
7605269a3aSJordan Crouse  */
7705269a3aSJordan Crouse 
7805269a3aSJordan Crouse #if BITS_PER_LONG == 64
79a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
80a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
8105269a3aSJordan Crouse #else
8205269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
8305269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
8405269a3aSJordan Crouse #endif
85a2c0a97bSJesse Barnes 
86673a394bSEric Anholt /**
87673a394bSEric Anholt  * Initialize the GEM device fields
88673a394bSEric Anholt  */
89673a394bSEric Anholt 
90673a394bSEric Anholt int
91673a394bSEric Anholt drm_gem_init(struct drm_device *dev)
92673a394bSEric Anholt {
93a2c0a97bSJesse Barnes 	struct drm_gem_mm *mm;
94a2c0a97bSJesse Barnes 
95673a394bSEric Anholt 	spin_lock_init(&dev->object_name_lock);
96673a394bSEric Anholt 	idr_init(&dev->object_name_idr);
97a2c0a97bSJesse Barnes 
989a298b2aSEric Anholt 	mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
99a2c0a97bSJesse Barnes 	if (!mm) {
100a2c0a97bSJesse Barnes 		DRM_ERROR("out of memory\n");
101a2c0a97bSJesse Barnes 		return -ENOMEM;
102a2c0a97bSJesse Barnes 	}
103a2c0a97bSJesse Barnes 
104a2c0a97bSJesse Barnes 	dev->mm_private = mm;
105a2c0a97bSJesse Barnes 
1064cb81ac2SChris Wilson 	if (drm_ht_create(&mm->offset_hash, 12)) {
1079a298b2aSEric Anholt 		kfree(mm);
108a2c0a97bSJesse Barnes 		return -ENOMEM;
109a2c0a97bSJesse Barnes 	}
110a2c0a97bSJesse Barnes 
111a2c0a97bSJesse Barnes 	if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
112a2c0a97bSJesse Barnes 			DRM_FILE_PAGE_OFFSET_SIZE)) {
113a2c0a97bSJesse Barnes 		drm_ht_remove(&mm->offset_hash);
1149a298b2aSEric Anholt 		kfree(mm);
115a2c0a97bSJesse Barnes 		return -ENOMEM;
116a2c0a97bSJesse Barnes 	}
117a2c0a97bSJesse Barnes 
118673a394bSEric Anholt 	return 0;
119673a394bSEric Anholt }
120673a394bSEric Anholt 
121a2c0a97bSJesse Barnes void
122a2c0a97bSJesse Barnes drm_gem_destroy(struct drm_device *dev)
123a2c0a97bSJesse Barnes {
124a2c0a97bSJesse Barnes 	struct drm_gem_mm *mm = dev->mm_private;
125a2c0a97bSJesse Barnes 
126a2c0a97bSJesse Barnes 	drm_mm_takedown(&mm->offset_manager);
127a2c0a97bSJesse Barnes 	drm_ht_remove(&mm->offset_hash);
1289a298b2aSEric Anholt 	kfree(mm);
129a2c0a97bSJesse Barnes 	dev->mm_private = NULL;
130a2c0a97bSJesse Barnes }
131a2c0a97bSJesse Barnes 
132673a394bSEric Anholt /**
13362cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
1341d397043SDaniel Vetter  * shmfs backing store.
1351d397043SDaniel Vetter  */
1361d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev,
1371d397043SDaniel Vetter 			struct drm_gem_object *obj, size_t size)
1381d397043SDaniel Vetter {
1391d397043SDaniel Vetter 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
1401d397043SDaniel Vetter 
1411d397043SDaniel Vetter 	obj->dev = dev;
1421d397043SDaniel Vetter 	obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
1431d397043SDaniel Vetter 	if (IS_ERR(obj->filp))
144dd8bc93dSChris Wilson 		return PTR_ERR(obj->filp);
1451d397043SDaniel Vetter 
1461d397043SDaniel Vetter 	kref_init(&obj->refcount);
14729d08b3eSDave Airlie 	atomic_set(&obj->handle_count, 0);
1481d397043SDaniel Vetter 	obj->size = size;
1491d397043SDaniel Vetter 
1501d397043SDaniel Vetter 	return 0;
1511d397043SDaniel Vetter }
1521d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init);
1531d397043SDaniel Vetter 
1541d397043SDaniel Vetter /**
15562cb7011SAlan Cox  * Initialize an already allocated GEM object of the specified size with
15662cb7011SAlan Cox  * no GEM provided backing store. Instead the caller is responsible for
15762cb7011SAlan Cox  * backing the object and handling it.
15862cb7011SAlan Cox  */
15962cb7011SAlan Cox int drm_gem_private_object_init(struct drm_device *dev,
16062cb7011SAlan Cox 			struct drm_gem_object *obj, size_t size)
16162cb7011SAlan Cox {
16262cb7011SAlan Cox 	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
16362cb7011SAlan Cox 
16462cb7011SAlan Cox 	obj->dev = dev;
16562cb7011SAlan Cox 	obj->filp = NULL;
16662cb7011SAlan Cox 
16762cb7011SAlan Cox 	kref_init(&obj->refcount);
16862cb7011SAlan Cox 	atomic_set(&obj->handle_count, 0);
16962cb7011SAlan Cox 	obj->size = size;
17062cb7011SAlan Cox 
17162cb7011SAlan Cox 	return 0;
17262cb7011SAlan Cox }
17362cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init);
17462cb7011SAlan Cox 
17562cb7011SAlan Cox /**
176673a394bSEric Anholt  * Allocate a GEM object of the specified size with shmfs backing store
177673a394bSEric Anholt  */
178673a394bSEric Anholt struct drm_gem_object *
179673a394bSEric Anholt drm_gem_object_alloc(struct drm_device *dev, size_t size)
180673a394bSEric Anholt {
181673a394bSEric Anholt 	struct drm_gem_object *obj;
182673a394bSEric Anholt 
183b798b1feSRobert P. J. Day 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
184845792d9SJiri Slaby 	if (!obj)
185845792d9SJiri Slaby 		goto free;
186673a394bSEric Anholt 
1871d397043SDaniel Vetter 	if (drm_gem_object_init(dev, obj, size) != 0)
188845792d9SJiri Slaby 		goto free;
189673a394bSEric Anholt 
190673a394bSEric Anholt 	if (dev->driver->gem_init_object != NULL &&
191673a394bSEric Anholt 	    dev->driver->gem_init_object(obj) != 0) {
192845792d9SJiri Slaby 		goto fput;
193673a394bSEric Anholt 	}
194673a394bSEric Anholt 	return obj;
195845792d9SJiri Slaby fput:
1961d397043SDaniel Vetter 	/* Object_init mangles the global counters - readjust them. */
197845792d9SJiri Slaby 	fput(obj->filp);
198845792d9SJiri Slaby free:
199845792d9SJiri Slaby 	kfree(obj);
200845792d9SJiri Slaby 	return NULL;
201673a394bSEric Anholt }
202673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_alloc);
203673a394bSEric Anholt 
204*0ff926c7SDave Airlie static void
205*0ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
206*0ff926c7SDave Airlie {
207*0ff926c7SDave Airlie 	if (obj->import_attach) {
208*0ff926c7SDave Airlie 		drm_prime_remove_imported_buf_handle(&filp->prime,
209*0ff926c7SDave Airlie 				obj->import_attach->dmabuf);
210*0ff926c7SDave Airlie 	}
211*0ff926c7SDave Airlie 	if (obj->export_dma_buf) {
212*0ff926c7SDave Airlie 		drm_prime_remove_imported_buf_handle(&filp->prime,
213*0ff926c7SDave Airlie 				obj->export_dma_buf);
214*0ff926c7SDave Airlie 	}
215*0ff926c7SDave Airlie }
216*0ff926c7SDave Airlie 
217673a394bSEric Anholt /**
218673a394bSEric Anholt  * Removes the mapping from handle to filp for this object.
219673a394bSEric Anholt  */
220ff72145bSDave Airlie int
221a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle)
222673a394bSEric Anholt {
223673a394bSEric Anholt 	struct drm_device *dev;
224673a394bSEric Anholt 	struct drm_gem_object *obj;
225673a394bSEric Anholt 
226673a394bSEric Anholt 	/* This is gross. The idr system doesn't let us try a delete and
227673a394bSEric Anholt 	 * return an error code.  It just spews if you fail at deleting.
228673a394bSEric Anholt 	 * So, we have to grab a lock around finding the object and then
229673a394bSEric Anholt 	 * doing the delete on it and dropping the refcount, or the user
230673a394bSEric Anholt 	 * could race us to double-decrement the refcount and cause a
231673a394bSEric Anholt 	 * use-after-free later.  Given the frequency of our handle lookups,
232673a394bSEric Anholt 	 * we may want to use ida for number allocation and a hash table
233673a394bSEric Anholt 	 * for the pointers, anyway.
234673a394bSEric Anholt 	 */
235673a394bSEric Anholt 	spin_lock(&filp->table_lock);
236673a394bSEric Anholt 
237673a394bSEric Anholt 	/* Check if we currently have a reference on the object */
238673a394bSEric Anholt 	obj = idr_find(&filp->object_idr, handle);
239673a394bSEric Anholt 	if (obj == NULL) {
240673a394bSEric Anholt 		spin_unlock(&filp->table_lock);
241673a394bSEric Anholt 		return -EINVAL;
242673a394bSEric Anholt 	}
243673a394bSEric Anholt 	dev = obj->dev;
244673a394bSEric Anholt 
245673a394bSEric Anholt 	/* Release reference and decrement refcount. */
246673a394bSEric Anholt 	idr_remove(&filp->object_idr, handle);
247673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
248673a394bSEric Anholt 
249*0ff926c7SDave Airlie 	drm_gem_remove_prime_handles(obj, filp);
2503248877eSDave Airlie 
251304eda32SBen Skeggs 	if (dev->driver->gem_close_object)
252304eda32SBen Skeggs 		dev->driver->gem_close_object(obj, filp);
253bc9025bdSLuca Barbieri 	drm_gem_object_handle_unreference_unlocked(obj);
254673a394bSEric Anholt 
255673a394bSEric Anholt 	return 0;
256673a394bSEric Anholt }
257ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete);
258673a394bSEric Anholt 
259673a394bSEric Anholt /**
260673a394bSEric Anholt  * Create a handle for this object. This adds a handle reference
261673a394bSEric Anholt  * to the object, which includes a regular reference count. Callers
262673a394bSEric Anholt  * will likely want to dereference the object afterwards.
263673a394bSEric Anholt  */
264673a394bSEric Anholt int
265673a394bSEric Anholt drm_gem_handle_create(struct drm_file *file_priv,
266673a394bSEric Anholt 		       struct drm_gem_object *obj,
267a1a2d1d3SPekka Paalanen 		       u32 *handlep)
268673a394bSEric Anholt {
269304eda32SBen Skeggs 	struct drm_device *dev = obj->dev;
270673a394bSEric Anholt 	int ret;
271673a394bSEric Anholt 
272673a394bSEric Anholt 	/*
273673a394bSEric Anholt 	 * Get the user-visible handle using idr.
274673a394bSEric Anholt 	 */
275673a394bSEric Anholt again:
276673a394bSEric Anholt 	/* ensure there is space available to allocate a handle */
277673a394bSEric Anholt 	if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
278673a394bSEric Anholt 		return -ENOMEM;
279673a394bSEric Anholt 
280673a394bSEric Anholt 	/* do the allocation under our spinlock */
281673a394bSEric Anholt 	spin_lock(&file_priv->table_lock);
282a1a2d1d3SPekka Paalanen 	ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
283673a394bSEric Anholt 	spin_unlock(&file_priv->table_lock);
284673a394bSEric Anholt 	if (ret == -EAGAIN)
285673a394bSEric Anholt 		goto again;
286f1ae126cSVille Syrjälä 	else if (ret)
287673a394bSEric Anholt 		return ret;
288673a394bSEric Anholt 
289673a394bSEric Anholt 	drm_gem_object_handle_reference(obj);
290304eda32SBen Skeggs 
291304eda32SBen Skeggs 	if (dev->driver->gem_open_object) {
292304eda32SBen Skeggs 		ret = dev->driver->gem_open_object(obj, file_priv);
293304eda32SBen Skeggs 		if (ret) {
294304eda32SBen Skeggs 			drm_gem_handle_delete(file_priv, *handlep);
295304eda32SBen Skeggs 			return ret;
296304eda32SBen Skeggs 		}
297304eda32SBen Skeggs 	}
298304eda32SBen Skeggs 
299673a394bSEric Anholt 	return 0;
300673a394bSEric Anholt }
301673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create);
302673a394bSEric Anholt 
30375ef8b3bSRob Clark 
30475ef8b3bSRob Clark /**
30575ef8b3bSRob Clark  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
30675ef8b3bSRob Clark  * @obj: obj in question
30775ef8b3bSRob Clark  *
30875ef8b3bSRob Clark  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
30975ef8b3bSRob Clark  */
31075ef8b3bSRob Clark void
31175ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj)
31275ef8b3bSRob Clark {
31375ef8b3bSRob Clark 	struct drm_device *dev = obj->dev;
31475ef8b3bSRob Clark 	struct drm_gem_mm *mm = dev->mm_private;
31575ef8b3bSRob Clark 	struct drm_map_list *list = &obj->map_list;
31675ef8b3bSRob Clark 
31775ef8b3bSRob Clark 	drm_ht_remove_item(&mm->offset_hash, &list->hash);
31875ef8b3bSRob Clark 	drm_mm_put_block(list->file_offset_node);
31975ef8b3bSRob Clark 	kfree(list->map);
32075ef8b3bSRob Clark 	list->map = NULL;
32175ef8b3bSRob Clark }
32275ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset);
32375ef8b3bSRob Clark 
32475ef8b3bSRob Clark /**
32575ef8b3bSRob Clark  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
32675ef8b3bSRob Clark  * @obj: obj in question
32775ef8b3bSRob Clark  *
32875ef8b3bSRob Clark  * GEM memory mapping works by handing back to userspace a fake mmap offset
32975ef8b3bSRob Clark  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
33075ef8b3bSRob Clark  * up the object based on the offset and sets up the various memory mapping
33175ef8b3bSRob Clark  * structures.
33275ef8b3bSRob Clark  *
33375ef8b3bSRob Clark  * This routine allocates and attaches a fake offset for @obj.
33475ef8b3bSRob Clark  */
33575ef8b3bSRob Clark int
33675ef8b3bSRob Clark drm_gem_create_mmap_offset(struct drm_gem_object *obj)
33775ef8b3bSRob Clark {
33875ef8b3bSRob Clark 	struct drm_device *dev = obj->dev;
33975ef8b3bSRob Clark 	struct drm_gem_mm *mm = dev->mm_private;
34075ef8b3bSRob Clark 	struct drm_map_list *list;
34175ef8b3bSRob Clark 	struct drm_local_map *map;
3424a1b0714SLaurent Pinchart 	int ret;
34375ef8b3bSRob Clark 
34475ef8b3bSRob Clark 	/* Set the object up for mmap'ing */
34575ef8b3bSRob Clark 	list = &obj->map_list;
34675ef8b3bSRob Clark 	list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
34775ef8b3bSRob Clark 	if (!list->map)
34875ef8b3bSRob Clark 		return -ENOMEM;
34975ef8b3bSRob Clark 
35075ef8b3bSRob Clark 	map = list->map;
35175ef8b3bSRob Clark 	map->type = _DRM_GEM;
35275ef8b3bSRob Clark 	map->size = obj->size;
35375ef8b3bSRob Clark 	map->handle = obj;
35475ef8b3bSRob Clark 
35575ef8b3bSRob Clark 	/* Get a DRM GEM mmap offset allocated... */
35675ef8b3bSRob Clark 	list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
35775ef8b3bSRob Clark 			obj->size / PAGE_SIZE, 0, 0);
35875ef8b3bSRob Clark 
35975ef8b3bSRob Clark 	if (!list->file_offset_node) {
36075ef8b3bSRob Clark 		DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
36175ef8b3bSRob Clark 		ret = -ENOSPC;
36275ef8b3bSRob Clark 		goto out_free_list;
36375ef8b3bSRob Clark 	}
36475ef8b3bSRob Clark 
36575ef8b3bSRob Clark 	list->file_offset_node = drm_mm_get_block(list->file_offset_node,
36675ef8b3bSRob Clark 			obj->size / PAGE_SIZE, 0);
36775ef8b3bSRob Clark 	if (!list->file_offset_node) {
36875ef8b3bSRob Clark 		ret = -ENOMEM;
36975ef8b3bSRob Clark 		goto out_free_list;
37075ef8b3bSRob Clark 	}
37175ef8b3bSRob Clark 
37275ef8b3bSRob Clark 	list->hash.key = list->file_offset_node->start;
37375ef8b3bSRob Clark 	ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
37475ef8b3bSRob Clark 	if (ret) {
37575ef8b3bSRob Clark 		DRM_ERROR("failed to add to map hash\n");
37675ef8b3bSRob Clark 		goto out_free_mm;
37775ef8b3bSRob Clark 	}
37875ef8b3bSRob Clark 
37975ef8b3bSRob Clark 	return 0;
38075ef8b3bSRob Clark 
38175ef8b3bSRob Clark out_free_mm:
38275ef8b3bSRob Clark 	drm_mm_put_block(list->file_offset_node);
38375ef8b3bSRob Clark out_free_list:
38475ef8b3bSRob Clark 	kfree(list->map);
38575ef8b3bSRob Clark 	list->map = NULL;
38675ef8b3bSRob Clark 
38775ef8b3bSRob Clark 	return ret;
38875ef8b3bSRob Clark }
38975ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset);
39075ef8b3bSRob Clark 
391673a394bSEric Anholt /** Returns a reference to the object named by the handle. */
392673a394bSEric Anholt struct drm_gem_object *
393673a394bSEric Anholt drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
394a1a2d1d3SPekka Paalanen 		      u32 handle)
395673a394bSEric Anholt {
396673a394bSEric Anholt 	struct drm_gem_object *obj;
397673a394bSEric Anholt 
398673a394bSEric Anholt 	spin_lock(&filp->table_lock);
399673a394bSEric Anholt 
400673a394bSEric Anholt 	/* Check if we currently have a reference on the object */
401673a394bSEric Anholt 	obj = idr_find(&filp->object_idr, handle);
402673a394bSEric Anholt 	if (obj == NULL) {
403673a394bSEric Anholt 		spin_unlock(&filp->table_lock);
404673a394bSEric Anholt 		return NULL;
405673a394bSEric Anholt 	}
406673a394bSEric Anholt 
407673a394bSEric Anholt 	drm_gem_object_reference(obj);
408673a394bSEric Anholt 
409673a394bSEric Anholt 	spin_unlock(&filp->table_lock);
410673a394bSEric Anholt 
411673a394bSEric Anholt 	return obj;
412673a394bSEric Anholt }
413673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup);
414673a394bSEric Anholt 
415673a394bSEric Anholt /**
416673a394bSEric Anholt  * Releases the handle to an mm object.
417673a394bSEric Anholt  */
418673a394bSEric Anholt int
419673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data,
420673a394bSEric Anholt 		    struct drm_file *file_priv)
421673a394bSEric Anholt {
422673a394bSEric Anholt 	struct drm_gem_close *args = data;
423673a394bSEric Anholt 	int ret;
424673a394bSEric Anholt 
425673a394bSEric Anholt 	if (!(dev->driver->driver_features & DRIVER_GEM))
426673a394bSEric Anholt 		return -ENODEV;
427673a394bSEric Anholt 
428673a394bSEric Anholt 	ret = drm_gem_handle_delete(file_priv, args->handle);
429673a394bSEric Anholt 
430673a394bSEric Anholt 	return ret;
431673a394bSEric Anholt }
432673a394bSEric Anholt 
433673a394bSEric Anholt /**
434673a394bSEric Anholt  * Create a global name for an object, returning the name.
435673a394bSEric Anholt  *
436673a394bSEric Anholt  * Note that the name does not hold a reference; when the object
437673a394bSEric Anholt  * is freed, the name goes away.
438673a394bSEric Anholt  */
439673a394bSEric Anholt int
440673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data,
441673a394bSEric Anholt 		    struct drm_file *file_priv)
442673a394bSEric Anholt {
443673a394bSEric Anholt 	struct drm_gem_flink *args = data;
444673a394bSEric Anholt 	struct drm_gem_object *obj;
445673a394bSEric Anholt 	int ret;
446673a394bSEric Anholt 
447673a394bSEric Anholt 	if (!(dev->driver->driver_features & DRIVER_GEM))
448673a394bSEric Anholt 		return -ENODEV;
449673a394bSEric Anholt 
450673a394bSEric Anholt 	obj = drm_gem_object_lookup(dev, file_priv, args->handle);
451673a394bSEric Anholt 	if (obj == NULL)
452bf79cb91SChris Wilson 		return -ENOENT;
453673a394bSEric Anholt 
454673a394bSEric Anholt again:
4553e49c4f4SChris Wilson 	if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
4563e49c4f4SChris Wilson 		ret = -ENOMEM;
4573e49c4f4SChris Wilson 		goto err;
4583e49c4f4SChris Wilson 	}
459673a394bSEric Anholt 
460673a394bSEric Anholt 	spin_lock(&dev->object_name_lock);
4618d59bae5SChris Wilson 	if (!obj->name) {
462673a394bSEric Anholt 		ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
463673a394bSEric Anholt 					&obj->name);
4648d59bae5SChris Wilson 		args->name = (uint64_t) obj->name;
465673a394bSEric Anholt 		spin_unlock(&dev->object_name_lock);
4668d59bae5SChris Wilson 
467673a394bSEric Anholt 		if (ret == -EAGAIN)
468673a394bSEric Anholt 			goto again;
469f1ae126cSVille Syrjälä 		else if (ret)
4703e49c4f4SChris Wilson 			goto err;
471673a394bSEric Anholt 
4728d59bae5SChris Wilson 		/* Allocate a reference for the name table.  */
4738d59bae5SChris Wilson 		drm_gem_object_reference(obj);
4748d59bae5SChris Wilson 	} else {
475673a394bSEric Anholt 		args->name = (uint64_t) obj->name;
4768d59bae5SChris Wilson 		spin_unlock(&dev->object_name_lock);
4778d59bae5SChris Wilson 		ret = 0;
4788d59bae5SChris Wilson 	}
4793e49c4f4SChris Wilson 
4803e49c4f4SChris Wilson err:
481bc9025bdSLuca Barbieri 	drm_gem_object_unreference_unlocked(obj);
4823e49c4f4SChris Wilson 	return ret;
483673a394bSEric Anholt }
484673a394bSEric Anholt 
485673a394bSEric Anholt /**
486673a394bSEric Anholt  * Open an object using the global name, returning a handle and the size.
487673a394bSEric Anholt  *
488673a394bSEric Anholt  * This handle (of course) holds a reference to the object, so the object
489673a394bSEric Anholt  * will not go away until the handle is deleted.
490673a394bSEric Anholt  */
491673a394bSEric Anholt int
492673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data,
493673a394bSEric Anholt 		   struct drm_file *file_priv)
494673a394bSEric Anholt {
495673a394bSEric Anholt 	struct drm_gem_open *args = data;
496673a394bSEric Anholt 	struct drm_gem_object *obj;
497673a394bSEric Anholt 	int ret;
498a1a2d1d3SPekka Paalanen 	u32 handle;
499673a394bSEric Anholt 
500673a394bSEric Anholt 	if (!(dev->driver->driver_features & DRIVER_GEM))
501673a394bSEric Anholt 		return -ENODEV;
502673a394bSEric Anholt 
503673a394bSEric Anholt 	spin_lock(&dev->object_name_lock);
504673a394bSEric Anholt 	obj = idr_find(&dev->object_name_idr, (int) args->name);
505673a394bSEric Anholt 	if (obj)
506673a394bSEric Anholt 		drm_gem_object_reference(obj);
507673a394bSEric Anholt 	spin_unlock(&dev->object_name_lock);
508673a394bSEric Anholt 	if (!obj)
509673a394bSEric Anholt 		return -ENOENT;
510673a394bSEric Anholt 
511673a394bSEric Anholt 	ret = drm_gem_handle_create(file_priv, obj, &handle);
512bc9025bdSLuca Barbieri 	drm_gem_object_unreference_unlocked(obj);
513673a394bSEric Anholt 	if (ret)
514673a394bSEric Anholt 		return ret;
515673a394bSEric Anholt 
516673a394bSEric Anholt 	args->handle = handle;
517673a394bSEric Anholt 	args->size = obj->size;
518673a394bSEric Anholt 
519673a394bSEric Anholt 	return 0;
520673a394bSEric Anholt }
521673a394bSEric Anholt 
522673a394bSEric Anholt /**
523673a394bSEric Anholt  * Called at device open time, sets up the structure for handling refcounting
524673a394bSEric Anholt  * of mm objects.
525673a394bSEric Anholt  */
526673a394bSEric Anholt void
527673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
528673a394bSEric Anholt {
529673a394bSEric Anholt 	idr_init(&file_private->object_idr);
530673a394bSEric Anholt 	spin_lock_init(&file_private->table_lock);
531673a394bSEric Anholt }
532673a394bSEric Anholt 
533673a394bSEric Anholt /**
534673a394bSEric Anholt  * Called at device close to release the file's
535673a394bSEric Anholt  * handle references on objects.
536673a394bSEric Anholt  */
537673a394bSEric Anholt static int
538673a394bSEric Anholt drm_gem_object_release_handle(int id, void *ptr, void *data)
539673a394bSEric Anholt {
540304eda32SBen Skeggs 	struct drm_file *file_priv = data;
541673a394bSEric Anholt 	struct drm_gem_object *obj = ptr;
542304eda32SBen Skeggs 	struct drm_device *dev = obj->dev;
543304eda32SBen Skeggs 
544*0ff926c7SDave Airlie 	drm_gem_remove_prime_handles(obj, file_priv);
5453248877eSDave Airlie 
546304eda32SBen Skeggs 	if (dev->driver->gem_close_object)
547304eda32SBen Skeggs 		dev->driver->gem_close_object(obj, file_priv);
548673a394bSEric Anholt 
549bc9025bdSLuca Barbieri 	drm_gem_object_handle_unreference_unlocked(obj);
550673a394bSEric Anholt 
551673a394bSEric Anholt 	return 0;
552673a394bSEric Anholt }
553673a394bSEric Anholt 
554673a394bSEric Anholt /**
555673a394bSEric Anholt  * Called at close time when the filp is going away.
556673a394bSEric Anholt  *
557673a394bSEric Anholt  * Releases any remaining references on objects by this filp.
558673a394bSEric Anholt  */
559673a394bSEric Anholt void
560673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
561673a394bSEric Anholt {
562673a394bSEric Anholt 	idr_for_each(&file_private->object_idr,
563304eda32SBen Skeggs 		     &drm_gem_object_release_handle, file_private);
564673a394bSEric Anholt 
565ddd3d069SChris Wilson 	idr_remove_all(&file_private->object_idr);
566673a394bSEric Anholt 	idr_destroy(&file_private->object_idr);
567673a394bSEric Anholt }
568673a394bSEric Anholt 
569fd632aa3SDaniel Vetter void
570fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj)
571c3ae90c0SLuca Barbieri {
57262cb7011SAlan Cox 	if (obj->filp)
573c3ae90c0SLuca Barbieri 	    fput(obj->filp);
574c3ae90c0SLuca Barbieri }
575fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release);
576c3ae90c0SLuca Barbieri 
577673a394bSEric Anholt /**
578673a394bSEric Anholt  * Called after the last reference to the object has been lost.
579c3ae90c0SLuca Barbieri  * Must be called holding struct_ mutex
580673a394bSEric Anholt  *
581673a394bSEric Anholt  * Frees the object
582673a394bSEric Anholt  */
583673a394bSEric Anholt void
584673a394bSEric Anholt drm_gem_object_free(struct kref *kref)
585673a394bSEric Anholt {
586673a394bSEric Anholt 	struct drm_gem_object *obj = (struct drm_gem_object *) kref;
587673a394bSEric Anholt 	struct drm_device *dev = obj->dev;
588673a394bSEric Anholt 
589673a394bSEric Anholt 	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
590673a394bSEric Anholt 
591673a394bSEric Anholt 	if (dev->driver->gem_free_object != NULL)
592673a394bSEric Anholt 		dev->driver->gem_free_object(obj);
593673a394bSEric Anholt }
594673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free);
595673a394bSEric Anholt 
596c3ae90c0SLuca Barbieri static void drm_gem_object_ref_bug(struct kref *list_kref)
597c3ae90c0SLuca Barbieri {
598c3ae90c0SLuca Barbieri 	BUG();
599c3ae90c0SLuca Barbieri }
600c3ae90c0SLuca Barbieri 
601c3ae90c0SLuca Barbieri /**
602673a394bSEric Anholt  * Called after the last handle to the object has been closed
603673a394bSEric Anholt  *
604673a394bSEric Anholt  * Removes any name for the object. Note that this must be
605673a394bSEric Anholt  * called before drm_gem_object_free or we'll be touching
606673a394bSEric Anholt  * freed memory
607673a394bSEric Anholt  */
60829d08b3eSDave Airlie void drm_gem_object_handle_free(struct drm_gem_object *obj)
609673a394bSEric Anholt {
610673a394bSEric Anholt 	struct drm_device *dev = obj->dev;
611673a394bSEric Anholt 
612673a394bSEric Anholt 	/* Remove any name for this object */
613673a394bSEric Anholt 	spin_lock(&dev->object_name_lock);
614673a394bSEric Anholt 	if (obj->name) {
615673a394bSEric Anholt 		idr_remove(&dev->object_name_idr, obj->name);
6168d59bae5SChris Wilson 		obj->name = 0;
617673a394bSEric Anholt 		spin_unlock(&dev->object_name_lock);
618673a394bSEric Anholt 		/*
619673a394bSEric Anholt 		 * The object name held a reference to this object, drop
620673a394bSEric Anholt 		 * that now.
621c3ae90c0SLuca Barbieri 		*
622c3ae90c0SLuca Barbieri 		* This cannot be the last reference, since the handle holds one too.
623673a394bSEric Anholt 		 */
624c3ae90c0SLuca Barbieri 		kref_put(&obj->refcount, drm_gem_object_ref_bug);
625673a394bSEric Anholt 	} else
626673a394bSEric Anholt 		spin_unlock(&dev->object_name_lock);
627673a394bSEric Anholt 
628673a394bSEric Anholt }
629673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_handle_free);
630673a394bSEric Anholt 
631ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma)
632ab00b3e5SJesse Barnes {
633ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
634ab00b3e5SJesse Barnes 
635ab00b3e5SJesse Barnes 	drm_gem_object_reference(obj);
63631dfbc93SChris Wilson 
63731dfbc93SChris Wilson 	mutex_lock(&obj->dev->struct_mutex);
638b06d66beSRob Clark 	drm_vm_open_locked(obj->dev, vma);
63931dfbc93SChris Wilson 	mutex_unlock(&obj->dev->struct_mutex);
640ab00b3e5SJesse Barnes }
641ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open);
642ab00b3e5SJesse Barnes 
643ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma)
644ab00b3e5SJesse Barnes {
645ab00b3e5SJesse Barnes 	struct drm_gem_object *obj = vma->vm_private_data;
646b74ad5aeSChris Wilson 	struct drm_device *dev = obj->dev;
647ab00b3e5SJesse Barnes 
648b74ad5aeSChris Wilson 	mutex_lock(&dev->struct_mutex);
649b06d66beSRob Clark 	drm_vm_close_locked(obj->dev, vma);
65031dfbc93SChris Wilson 	drm_gem_object_unreference(obj);
651b74ad5aeSChris Wilson 	mutex_unlock(&dev->struct_mutex);
652ab00b3e5SJesse Barnes }
653ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close);
654ab00b3e5SJesse Barnes 
655ab00b3e5SJesse Barnes 
656a2c0a97bSJesse Barnes /**
657a2c0a97bSJesse Barnes  * drm_gem_mmap - memory map routine for GEM objects
658a2c0a97bSJesse Barnes  * @filp: DRM file pointer
659a2c0a97bSJesse Barnes  * @vma: VMA for the area to be mapped
660a2c0a97bSJesse Barnes  *
661a2c0a97bSJesse Barnes  * If a driver supports GEM object mapping, mmap calls on the DRM file
662a2c0a97bSJesse Barnes  * descriptor will end up here.
663a2c0a97bSJesse Barnes  *
664a2c0a97bSJesse Barnes  * If we find the object based on the offset passed in (vma->vm_pgoff will
665a2c0a97bSJesse Barnes  * contain the fake offset we created when the GTT map ioctl was called on
666a2c0a97bSJesse Barnes  * the object), we set up the driver fault handler so that any accesses
667a2c0a97bSJesse Barnes  * to the object can be trapped, to perform migration, GTT binding, surface
668a2c0a97bSJesse Barnes  * register allocation, or performance monitoring.
669a2c0a97bSJesse Barnes  */
670a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
671a2c0a97bSJesse Barnes {
672a2c0a97bSJesse Barnes 	struct drm_file *priv = filp->private_data;
673a2c0a97bSJesse Barnes 	struct drm_device *dev = priv->minor->dev;
674a2c0a97bSJesse Barnes 	struct drm_gem_mm *mm = dev->mm_private;
675f77d390cSBenjamin Herrenschmidt 	struct drm_local_map *map = NULL;
676a2c0a97bSJesse Barnes 	struct drm_gem_object *obj;
677a2c0a97bSJesse Barnes 	struct drm_hash_item *hash;
678a2c0a97bSJesse Barnes 	int ret = 0;
679a2c0a97bSJesse Barnes 
6802c07a21dSDave Airlie 	if (drm_device_is_unplugged(dev))
6812c07a21dSDave Airlie 		return -ENODEV;
6822c07a21dSDave Airlie 
683a2c0a97bSJesse Barnes 	mutex_lock(&dev->struct_mutex);
684a2c0a97bSJesse Barnes 
685a2c0a97bSJesse Barnes 	if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
686a2c0a97bSJesse Barnes 		mutex_unlock(&dev->struct_mutex);
687a2c0a97bSJesse Barnes 		return drm_mmap(filp, vma);
688a2c0a97bSJesse Barnes 	}
689a2c0a97bSJesse Barnes 
690a2c0a97bSJesse Barnes 	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
691a2c0a97bSJesse Barnes 	if (!map ||
692a2c0a97bSJesse Barnes 	    ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
693a2c0a97bSJesse Barnes 		ret =  -EPERM;
694a2c0a97bSJesse Barnes 		goto out_unlock;
695a2c0a97bSJesse Barnes 	}
696a2c0a97bSJesse Barnes 
697a2c0a97bSJesse Barnes 	/* Check for valid size. */
698a2c0a97bSJesse Barnes 	if (map->size < vma->vm_end - vma->vm_start) {
699a2c0a97bSJesse Barnes 		ret = -EINVAL;
700a2c0a97bSJesse Barnes 		goto out_unlock;
701a2c0a97bSJesse Barnes 	}
702a2c0a97bSJesse Barnes 
703a2c0a97bSJesse Barnes 	obj = map->handle;
704a2c0a97bSJesse Barnes 	if (!obj->dev->driver->gem_vm_ops) {
705a2c0a97bSJesse Barnes 		ret = -EINVAL;
706a2c0a97bSJesse Barnes 		goto out_unlock;
707a2c0a97bSJesse Barnes 	}
708a2c0a97bSJesse Barnes 
709a2c0a97bSJesse Barnes 	vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
710a2c0a97bSJesse Barnes 	vma->vm_ops = obj->dev->driver->gem_vm_ops;
711a2c0a97bSJesse Barnes 	vma->vm_private_data = map->handle;
71279cc304fSJeremy Fitzhardinge 	vma->vm_page_prot =  pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
713a2c0a97bSJesse Barnes 
714ab00b3e5SJesse Barnes 	/* Take a ref for this mapping of the object, so that the fault
715ab00b3e5SJesse Barnes 	 * handler can dereference the mmap offset's pointer to the object.
716ab00b3e5SJesse Barnes 	 * This reference is cleaned up by the corresponding vm_close
717ab00b3e5SJesse Barnes 	 * (which should happen whether the vma was created by this call, or
718ab00b3e5SJesse Barnes 	 * by a vm_open due to mremap or partial unmap or whatever).
719ab00b3e5SJesse Barnes 	 */
720ab00b3e5SJesse Barnes 	drm_gem_object_reference(obj);
721ab00b3e5SJesse Barnes 
722b06d66beSRob Clark 	drm_vm_open_locked(dev, vma);
723a2c0a97bSJesse Barnes 
724a2c0a97bSJesse Barnes out_unlock:
725a2c0a97bSJesse Barnes 	mutex_unlock(&dev->struct_mutex);
726a2c0a97bSJesse Barnes 
727a2c0a97bSJesse Barnes 	return ret;
728a2c0a97bSJesse Barnes }
729a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap);
730