drm_gem.c revision 1.1.1.1.2.3 1 1.1.1.1.2.2 riastrad /*
2 1.1.1.1.2.2 riastrad * Copyright 2008 Intel Corporation
3 1.1.1.1.2.2 riastrad *
4 1.1.1.1.2.2 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
5 1.1.1.1.2.2 riastrad * copy of this software and associated documentation files (the "Software"),
6 1.1.1.1.2.2 riastrad * to deal in the Software without restriction, including without limitation
7 1.1.1.1.2.2 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 1.1.1.1.2.2 riastrad * and/or sell copies of the Software, and to permit persons to whom the
9 1.1.1.1.2.2 riastrad * Software is furnished to do so, subject to the following conditions:
10 1.1.1.1.2.2 riastrad *
11 1.1.1.1.2.2 riastrad * The above copyright notice and this permission notice (including the next
12 1.1.1.1.2.2 riastrad * paragraph) shall be included in all copies or substantial portions of the
13 1.1.1.1.2.2 riastrad * Software.
14 1.1.1.1.2.2 riastrad *
15 1.1.1.1.2.2 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 1.1.1.1.2.2 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 1.1.1.1.2.2 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 1.1.1.1.2.2 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 1.1.1.1.2.2 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 1.1.1.1.2.2 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 1.1.1.1.2.2 riastrad * IN THE SOFTWARE.
22 1.1.1.1.2.2 riastrad *
23 1.1.1.1.2.2 riastrad * Authors:
24 1.1.1.1.2.2 riastrad * Eric Anholt <eric (at) anholt.net>
25 1.1.1.1.2.2 riastrad *
26 1.1.1.1.2.2 riastrad */
27 1.1.1.1.2.2 riastrad
28 1.1.1.1.2.2 riastrad #include <linux/types.h>
29 1.1.1.1.2.2 riastrad #include <linux/slab.h>
30 1.1.1.1.2.2 riastrad #include <linux/mm.h>
31 1.1.1.1.2.2 riastrad #include <linux/uaccess.h>
32 1.1.1.1.2.2 riastrad #include <linux/fs.h>
33 1.1.1.1.2.2 riastrad #include <linux/file.h>
34 1.1.1.1.2.2 riastrad #include <linux/module.h>
35 1.1.1.1.2.2 riastrad #include <linux/mman.h>
36 1.1.1.1.2.2 riastrad #include <linux/pagemap.h>
37 1.1.1.1.2.2 riastrad #include <linux/shmem_fs.h>
38 1.1.1.1.2.2 riastrad #include <linux/dma-buf.h>
39 1.1.1.1.2.3 riastrad #include <linux/err.h>
40 1.1.1.1.2.3 riastrad #include <linux/export.h>
41 1.1.1.1.2.3 riastrad #include <asm/bug.h>
42 1.1.1.1.2.2 riastrad #include <drm/drmP.h>
43 1.1.1.1.2.2 riastrad
44 1.1.1.1.2.2 riastrad /** @file drm_gem.c
45 1.1.1.1.2.2 riastrad *
46 1.1.1.1.2.2 riastrad * This file provides some of the base ioctls and library routines for
47 1.1.1.1.2.2 riastrad * the graphics memory manager implemented by each device driver.
48 1.1.1.1.2.2 riastrad *
49 1.1.1.1.2.2 riastrad * Because various devices have different requirements in terms of
50 1.1.1.1.2.2 riastrad * synchronization and migration strategies, implementing that is left up to
51 1.1.1.1.2.2 riastrad * the driver, and all that the general API provides should be generic --
52 1.1.1.1.2.2 riastrad * allocating objects, reading/writing data with the cpu, freeing objects.
53 1.1.1.1.2.2 riastrad * Even there, platform-dependent optimizations for reading/writing data with
54 1.1.1.1.2.2 riastrad * the CPU mean we'll likely hook those out to driver-specific calls. However,
55 1.1.1.1.2.2 riastrad * the DRI2 implementation wants to have at least allocate/mmap be generic.
56 1.1.1.1.2.2 riastrad *
57 1.1.1.1.2.2 riastrad * The goal was to have swap-backed object allocation managed through
58 1.1.1.1.2.2 riastrad * struct file. However, file descriptors as handles to a struct file have
59 1.1.1.1.2.2 riastrad * two major failings:
60 1.1.1.1.2.2 riastrad * - Process limits prevent more than 1024 or so being used at a time by
61 1.1.1.1.2.2 riastrad * default.
62 1.1.1.1.2.2 riastrad * - Inability to allocate high fds will aggravate the X Server's select()
63 1.1.1.1.2.2 riastrad * handling, and likely that of many GL client applications as well.
64 1.1.1.1.2.2 riastrad *
65 1.1.1.1.2.2 riastrad * This led to a plan of using our own integer IDs (called handles, following
66 1.1.1.1.2.2 riastrad * DRM terminology) to mimic fds, and implement the fd syscalls we need as
67 1.1.1.1.2.2 riastrad * ioctls. The objects themselves will still include the struct file so
68 1.1.1.1.2.2 riastrad * that we can transition to fds if the required kernel infrastructure shows
69 1.1.1.1.2.2 riastrad * up at a later date, and as our interface with shmfs for memory allocation.
70 1.1.1.1.2.2 riastrad */
71 1.1.1.1.2.2 riastrad
72 1.1.1.1.2.2 riastrad /*
73 1.1.1.1.2.2 riastrad * We make up offsets for buffer objects so we can recognize them at
74 1.1.1.1.2.2 riastrad * mmap time.
75 1.1.1.1.2.2 riastrad */
76 1.1.1.1.2.2 riastrad
77 1.1.1.1.2.2 riastrad /* pgoff in mmap is an unsigned long, so we need to make sure that
78 1.1.1.1.2.2 riastrad * the faked up offset will fit
79 1.1.1.1.2.2 riastrad */
80 1.1.1.1.2.2 riastrad
81 1.1.1.1.2.2 riastrad #if BITS_PER_LONG == 64
82 1.1.1.1.2.2 riastrad #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
83 1.1.1.1.2.2 riastrad #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
84 1.1.1.1.2.2 riastrad #else
85 1.1.1.1.2.2 riastrad #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
86 1.1.1.1.2.2 riastrad #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
87 1.1.1.1.2.2 riastrad #endif
88 1.1.1.1.2.2 riastrad
89 1.1.1.1.2.2 riastrad /**
90 1.1.1.1.2.2 riastrad * Initialize the GEM device fields
91 1.1.1.1.2.2 riastrad */
92 1.1.1.1.2.2 riastrad
93 1.1.1.1.2.2 riastrad int
94 1.1.1.1.2.2 riastrad drm_gem_init(struct drm_device *dev)
95 1.1.1.1.2.2 riastrad {
96 1.1.1.1.2.2 riastrad struct drm_gem_mm *mm;
97 1.1.1.1.2.2 riastrad
98 1.1.1.1.2.2 riastrad spin_lock_init(&dev->object_name_lock);
99 1.1.1.1.2.2 riastrad idr_init(&dev->object_name_idr);
100 1.1.1.1.2.2 riastrad
101 1.1.1.1.2.2 riastrad mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
102 1.1.1.1.2.2 riastrad if (!mm) {
103 1.1.1.1.2.2 riastrad DRM_ERROR("out of memory\n");
104 1.1.1.1.2.2 riastrad return -ENOMEM;
105 1.1.1.1.2.2 riastrad }
106 1.1.1.1.2.2 riastrad
107 1.1.1.1.2.2 riastrad dev->mm_private = mm;
108 1.1.1.1.2.2 riastrad
109 1.1.1.1.2.2 riastrad if (drm_ht_create(&mm->offset_hash, 12)) {
110 1.1.1.1.2.2 riastrad kfree(mm);
111 1.1.1.1.2.2 riastrad return -ENOMEM;
112 1.1.1.1.2.2 riastrad }
113 1.1.1.1.2.2 riastrad
114 1.1.1.1.2.2 riastrad if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
115 1.1.1.1.2.2 riastrad DRM_FILE_PAGE_OFFSET_SIZE)) {
116 1.1.1.1.2.2 riastrad drm_ht_remove(&mm->offset_hash);
117 1.1.1.1.2.2 riastrad kfree(mm);
118 1.1.1.1.2.2 riastrad return -ENOMEM;
119 1.1.1.1.2.2 riastrad }
120 1.1.1.1.2.2 riastrad
121 1.1.1.1.2.2 riastrad return 0;
122 1.1.1.1.2.2 riastrad }
123 1.1.1.1.2.2 riastrad
124 1.1.1.1.2.2 riastrad void
125 1.1.1.1.2.2 riastrad drm_gem_destroy(struct drm_device *dev)
126 1.1.1.1.2.2 riastrad {
127 1.1.1.1.2.2 riastrad struct drm_gem_mm *mm = dev->mm_private;
128 1.1.1.1.2.2 riastrad
129 1.1.1.1.2.2 riastrad drm_mm_takedown(&mm->offset_manager);
130 1.1.1.1.2.2 riastrad drm_ht_remove(&mm->offset_hash);
131 1.1.1.1.2.2 riastrad kfree(mm);
132 1.1.1.1.2.2 riastrad dev->mm_private = NULL;
133 1.1.1.1.2.2 riastrad }
134 1.1.1.1.2.2 riastrad
135 1.1.1.1.2.2 riastrad /**
136 1.1.1.1.2.2 riastrad * Initialize an already allocated GEM object of the specified size with
137 1.1.1.1.2.2 riastrad * shmfs backing store.
138 1.1.1.1.2.2 riastrad */
139 1.1.1.1.2.2 riastrad int drm_gem_object_init(struct drm_device *dev,
140 1.1.1.1.2.2 riastrad struct drm_gem_object *obj, size_t size)
141 1.1.1.1.2.2 riastrad {
142 1.1.1.1.2.2 riastrad BUG_ON((size & (PAGE_SIZE - 1)) != 0);
143 1.1.1.1.2.2 riastrad
144 1.1.1.1.2.2 riastrad obj->dev = dev;
145 1.1.1.1.2.2 riastrad obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
146 1.1.1.1.2.2 riastrad if (IS_ERR(obj->filp))
147 1.1.1.1.2.2 riastrad return PTR_ERR(obj->filp);
148 1.1.1.1.2.2 riastrad
149 1.1.1.1.2.2 riastrad kref_init(&obj->refcount);
150 1.1.1.1.2.2 riastrad atomic_set(&obj->handle_count, 0);
151 1.1.1.1.2.2 riastrad obj->size = size;
152 1.1.1.1.2.2 riastrad
153 1.1.1.1.2.2 riastrad return 0;
154 1.1.1.1.2.2 riastrad }
155 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_object_init);
156 1.1.1.1.2.2 riastrad
157 1.1.1.1.2.2 riastrad /**
158 1.1.1.1.2.2 riastrad * Initialize an already allocated GEM object of the specified size with
159 1.1.1.1.2.2 riastrad * no GEM provided backing store. Instead the caller is responsible for
160 1.1.1.1.2.2 riastrad * backing the object and handling it.
161 1.1.1.1.2.2 riastrad */
162 1.1.1.1.2.2 riastrad int drm_gem_private_object_init(struct drm_device *dev,
163 1.1.1.1.2.2 riastrad struct drm_gem_object *obj, size_t size)
164 1.1.1.1.2.2 riastrad {
165 1.1.1.1.2.2 riastrad BUG_ON((size & (PAGE_SIZE - 1)) != 0);
166 1.1.1.1.2.2 riastrad
167 1.1.1.1.2.2 riastrad obj->dev = dev;
168 1.1.1.1.2.2 riastrad obj->filp = NULL;
169 1.1.1.1.2.2 riastrad
170 1.1.1.1.2.2 riastrad kref_init(&obj->refcount);
171 1.1.1.1.2.2 riastrad atomic_set(&obj->handle_count, 0);
172 1.1.1.1.2.2 riastrad obj->size = size;
173 1.1.1.1.2.2 riastrad
174 1.1.1.1.2.2 riastrad return 0;
175 1.1.1.1.2.2 riastrad }
176 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_private_object_init);
177 1.1.1.1.2.2 riastrad
178 1.1.1.1.2.2 riastrad /**
179 1.1.1.1.2.2 riastrad * Allocate a GEM object of the specified size with shmfs backing store
180 1.1.1.1.2.2 riastrad */
181 1.1.1.1.2.2 riastrad struct drm_gem_object *
182 1.1.1.1.2.2 riastrad drm_gem_object_alloc(struct drm_device *dev, size_t size)
183 1.1.1.1.2.2 riastrad {
184 1.1.1.1.2.2 riastrad struct drm_gem_object *obj;
185 1.1.1.1.2.2 riastrad
186 1.1.1.1.2.2 riastrad obj = kzalloc(sizeof(*obj), GFP_KERNEL);
187 1.1.1.1.2.2 riastrad if (!obj)
188 1.1.1.1.2.2 riastrad goto free;
189 1.1.1.1.2.2 riastrad
190 1.1.1.1.2.2 riastrad if (drm_gem_object_init(dev, obj, size) != 0)
191 1.1.1.1.2.2 riastrad goto free;
192 1.1.1.1.2.2 riastrad
193 1.1.1.1.2.2 riastrad if (dev->driver->gem_init_object != NULL &&
194 1.1.1.1.2.2 riastrad dev->driver->gem_init_object(obj) != 0) {
195 1.1.1.1.2.2 riastrad goto fput;
196 1.1.1.1.2.2 riastrad }
197 1.1.1.1.2.2 riastrad return obj;
198 1.1.1.1.2.2 riastrad fput:
199 1.1.1.1.2.2 riastrad /* Object_init mangles the global counters - readjust them. */
200 1.1.1.1.2.2 riastrad fput(obj->filp);
201 1.1.1.1.2.2 riastrad free:
202 1.1.1.1.2.2 riastrad kfree(obj);
203 1.1.1.1.2.2 riastrad return NULL;
204 1.1.1.1.2.2 riastrad }
205 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_object_alloc);
206 1.1.1.1.2.2 riastrad
207 1.1.1.1.2.2 riastrad static void
208 1.1.1.1.2.2 riastrad drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
209 1.1.1.1.2.2 riastrad {
210 1.1.1.1.2.2 riastrad if (obj->import_attach) {
211 1.1.1.1.2.2 riastrad drm_prime_remove_imported_buf_handle(&filp->prime,
212 1.1.1.1.2.2 riastrad obj->import_attach->dmabuf);
213 1.1.1.1.2.2 riastrad }
214 1.1.1.1.2.2 riastrad if (obj->export_dma_buf) {
215 1.1.1.1.2.2 riastrad drm_prime_remove_imported_buf_handle(&filp->prime,
216 1.1.1.1.2.2 riastrad obj->export_dma_buf);
217 1.1.1.1.2.2 riastrad }
218 1.1.1.1.2.2 riastrad }
219 1.1.1.1.2.2 riastrad
220 1.1.1.1.2.2 riastrad /**
221 1.1.1.1.2.2 riastrad * Removes the mapping from handle to filp for this object.
222 1.1.1.1.2.2 riastrad */
223 1.1.1.1.2.2 riastrad int
224 1.1.1.1.2.2 riastrad drm_gem_handle_delete(struct drm_file *filp, u32 handle)
225 1.1.1.1.2.2 riastrad {
226 1.1.1.1.2.2 riastrad struct drm_device *dev;
227 1.1.1.1.2.2 riastrad struct drm_gem_object *obj;
228 1.1.1.1.2.2 riastrad
229 1.1.1.1.2.2 riastrad /* This is gross. The idr system doesn't let us try a delete and
230 1.1.1.1.2.2 riastrad * return an error code. It just spews if you fail at deleting.
231 1.1.1.1.2.2 riastrad * So, we have to grab a lock around finding the object and then
232 1.1.1.1.2.2 riastrad * doing the delete on it and dropping the refcount, or the user
233 1.1.1.1.2.2 riastrad * could race us to double-decrement the refcount and cause a
234 1.1.1.1.2.2 riastrad * use-after-free later. Given the frequency of our handle lookups,
235 1.1.1.1.2.2 riastrad * we may want to use ida for number allocation and a hash table
236 1.1.1.1.2.2 riastrad * for the pointers, anyway.
237 1.1.1.1.2.2 riastrad */
238 1.1.1.1.2.2 riastrad spin_lock(&filp->table_lock);
239 1.1.1.1.2.2 riastrad
240 1.1.1.1.2.2 riastrad /* Check if we currently have a reference on the object */
241 1.1.1.1.2.2 riastrad obj = idr_find(&filp->object_idr, handle);
242 1.1.1.1.2.2 riastrad if (obj == NULL) {
243 1.1.1.1.2.2 riastrad spin_unlock(&filp->table_lock);
244 1.1.1.1.2.2 riastrad return -EINVAL;
245 1.1.1.1.2.2 riastrad }
246 1.1.1.1.2.2 riastrad dev = obj->dev;
247 1.1.1.1.2.2 riastrad
248 1.1.1.1.2.2 riastrad /* Release reference and decrement refcount. */
249 1.1.1.1.2.2 riastrad idr_remove(&filp->object_idr, handle);
250 1.1.1.1.2.2 riastrad spin_unlock(&filp->table_lock);
251 1.1.1.1.2.2 riastrad
252 1.1.1.1.2.2 riastrad drm_gem_remove_prime_handles(obj, filp);
253 1.1.1.1.2.2 riastrad
254 1.1.1.1.2.2 riastrad if (dev->driver->gem_close_object)
255 1.1.1.1.2.2 riastrad dev->driver->gem_close_object(obj, filp);
256 1.1.1.1.2.2 riastrad drm_gem_object_handle_unreference_unlocked(obj);
257 1.1.1.1.2.2 riastrad
258 1.1.1.1.2.2 riastrad return 0;
259 1.1.1.1.2.2 riastrad }
260 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_handle_delete);
261 1.1.1.1.2.2 riastrad
262 1.1.1.1.2.2 riastrad /**
263 1.1.1.1.2.2 riastrad * Create a handle for this object. This adds a handle reference
264 1.1.1.1.2.2 riastrad * to the object, which includes a regular reference count. Callers
265 1.1.1.1.2.2 riastrad * will likely want to dereference the object afterwards.
266 1.1.1.1.2.2 riastrad */
267 1.1.1.1.2.2 riastrad int
268 1.1.1.1.2.2 riastrad drm_gem_handle_create(struct drm_file *file_priv,
269 1.1.1.1.2.2 riastrad struct drm_gem_object *obj,
270 1.1.1.1.2.2 riastrad u32 *handlep)
271 1.1.1.1.2.2 riastrad {
272 1.1.1.1.2.2 riastrad struct drm_device *dev = obj->dev;
273 1.1.1.1.2.2 riastrad int ret;
274 1.1.1.1.2.2 riastrad
275 1.1.1.1.2.2 riastrad /*
276 1.1.1.1.2.2 riastrad * Get the user-visible handle using idr.
277 1.1.1.1.2.2 riastrad */
278 1.1.1.1.2.2 riastrad again:
279 1.1.1.1.2.2 riastrad /* ensure there is space available to allocate a handle */
280 1.1.1.1.2.2 riastrad if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
281 1.1.1.1.2.2 riastrad return -ENOMEM;
282 1.1.1.1.2.2 riastrad
283 1.1.1.1.2.2 riastrad /* do the allocation under our spinlock */
284 1.1.1.1.2.2 riastrad spin_lock(&file_priv->table_lock);
285 1.1.1.1.2.2 riastrad ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
286 1.1.1.1.2.2 riastrad spin_unlock(&file_priv->table_lock);
287 1.1.1.1.2.2 riastrad if (ret == -EAGAIN)
288 1.1.1.1.2.2 riastrad goto again;
289 1.1.1.1.2.2 riastrad else if (ret)
290 1.1.1.1.2.2 riastrad return ret;
291 1.1.1.1.2.2 riastrad
292 1.1.1.1.2.2 riastrad drm_gem_object_handle_reference(obj);
293 1.1.1.1.2.2 riastrad
294 1.1.1.1.2.2 riastrad if (dev->driver->gem_open_object) {
295 1.1.1.1.2.2 riastrad ret = dev->driver->gem_open_object(obj, file_priv);
296 1.1.1.1.2.2 riastrad if (ret) {
297 1.1.1.1.2.2 riastrad drm_gem_handle_delete(file_priv, *handlep);
298 1.1.1.1.2.2 riastrad return ret;
299 1.1.1.1.2.2 riastrad }
300 1.1.1.1.2.2 riastrad }
301 1.1.1.1.2.2 riastrad
302 1.1.1.1.2.2 riastrad return 0;
303 1.1.1.1.2.2 riastrad }
304 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_handle_create);
305 1.1.1.1.2.2 riastrad
306 1.1.1.1.2.2 riastrad
307 1.1.1.1.2.2 riastrad /**
308 1.1.1.1.2.2 riastrad * drm_gem_free_mmap_offset - release a fake mmap offset for an object
309 1.1.1.1.2.2 riastrad * @obj: obj in question
310 1.1.1.1.2.2 riastrad *
311 1.1.1.1.2.2 riastrad * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
312 1.1.1.1.2.2 riastrad */
313 1.1.1.1.2.2 riastrad void
314 1.1.1.1.2.2 riastrad drm_gem_free_mmap_offset(struct drm_gem_object *obj)
315 1.1.1.1.2.2 riastrad {
316 1.1.1.1.2.2 riastrad struct drm_device *dev = obj->dev;
317 1.1.1.1.2.2 riastrad struct drm_gem_mm *mm = dev->mm_private;
318 1.1.1.1.2.2 riastrad struct drm_map_list *list = &obj->map_list;
319 1.1.1.1.2.2 riastrad
320 1.1.1.1.2.2 riastrad drm_ht_remove_item(&mm->offset_hash, &list->hash);
321 1.1.1.1.2.2 riastrad drm_mm_put_block(list->file_offset_node);
322 1.1.1.1.2.2 riastrad kfree(list->map);
323 1.1.1.1.2.2 riastrad list->map = NULL;
324 1.1.1.1.2.2 riastrad }
325 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_free_mmap_offset);
326 1.1.1.1.2.2 riastrad
327 1.1.1.1.2.2 riastrad /**
328 1.1.1.1.2.2 riastrad * drm_gem_create_mmap_offset - create a fake mmap offset for an object
329 1.1.1.1.2.2 riastrad * @obj: obj in question
330 1.1.1.1.2.2 riastrad *
331 1.1.1.1.2.2 riastrad * GEM memory mapping works by handing back to userspace a fake mmap offset
332 1.1.1.1.2.2 riastrad * it can use in a subsequent mmap(2) call. The DRM core code then looks
333 1.1.1.1.2.2 riastrad * up the object based on the offset and sets up the various memory mapping
334 1.1.1.1.2.2 riastrad * structures.
335 1.1.1.1.2.2 riastrad *
336 1.1.1.1.2.2 riastrad * This routine allocates and attaches a fake offset for @obj.
337 1.1.1.1.2.2 riastrad */
338 1.1.1.1.2.2 riastrad int
339 1.1.1.1.2.2 riastrad drm_gem_create_mmap_offset(struct drm_gem_object *obj)
340 1.1.1.1.2.2 riastrad {
341 1.1.1.1.2.2 riastrad struct drm_device *dev = obj->dev;
342 1.1.1.1.2.2 riastrad struct drm_gem_mm *mm = dev->mm_private;
343 1.1.1.1.2.2 riastrad struct drm_map_list *list;
344 1.1.1.1.2.2 riastrad struct drm_local_map *map;
345 1.1.1.1.2.2 riastrad int ret;
346 1.1.1.1.2.2 riastrad
347 1.1.1.1.2.2 riastrad /* Set the object up for mmap'ing */
348 1.1.1.1.2.2 riastrad list = &obj->map_list;
349 1.1.1.1.2.2 riastrad list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
350 1.1.1.1.2.2 riastrad if (!list->map)
351 1.1.1.1.2.2 riastrad return -ENOMEM;
352 1.1.1.1.2.2 riastrad
353 1.1.1.1.2.2 riastrad map = list->map;
354 1.1.1.1.2.2 riastrad map->type = _DRM_GEM;
355 1.1.1.1.2.2 riastrad map->size = obj->size;
356 1.1.1.1.2.2 riastrad map->handle = obj;
357 1.1.1.1.2.2 riastrad
358 1.1.1.1.2.2 riastrad /* Get a DRM GEM mmap offset allocated... */
359 1.1.1.1.2.2 riastrad list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
360 1.1.1.1.2.2 riastrad obj->size / PAGE_SIZE, 0, false);
361 1.1.1.1.2.2 riastrad
362 1.1.1.1.2.2 riastrad if (!list->file_offset_node) {
363 1.1.1.1.2.2 riastrad DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
364 1.1.1.1.2.2 riastrad ret = -ENOSPC;
365 1.1.1.1.2.2 riastrad goto out_free_list;
366 1.1.1.1.2.2 riastrad }
367 1.1.1.1.2.2 riastrad
368 1.1.1.1.2.2 riastrad list->file_offset_node = drm_mm_get_block(list->file_offset_node,
369 1.1.1.1.2.2 riastrad obj->size / PAGE_SIZE, 0);
370 1.1.1.1.2.2 riastrad if (!list->file_offset_node) {
371 1.1.1.1.2.2 riastrad ret = -ENOMEM;
372 1.1.1.1.2.2 riastrad goto out_free_list;
373 1.1.1.1.2.2 riastrad }
374 1.1.1.1.2.2 riastrad
375 1.1.1.1.2.2 riastrad list->hash.key = list->file_offset_node->start;
376 1.1.1.1.2.2 riastrad ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
377 1.1.1.1.2.2 riastrad if (ret) {
378 1.1.1.1.2.2 riastrad DRM_ERROR("failed to add to map hash\n");
379 1.1.1.1.2.2 riastrad goto out_free_mm;
380 1.1.1.1.2.2 riastrad }
381 1.1.1.1.2.2 riastrad
382 1.1.1.1.2.2 riastrad return 0;
383 1.1.1.1.2.2 riastrad
384 1.1.1.1.2.2 riastrad out_free_mm:
385 1.1.1.1.2.2 riastrad drm_mm_put_block(list->file_offset_node);
386 1.1.1.1.2.2 riastrad out_free_list:
387 1.1.1.1.2.2 riastrad kfree(list->map);
388 1.1.1.1.2.2 riastrad list->map = NULL;
389 1.1.1.1.2.2 riastrad
390 1.1.1.1.2.2 riastrad return ret;
391 1.1.1.1.2.2 riastrad }
392 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_create_mmap_offset);
393 1.1.1.1.2.2 riastrad
394 1.1.1.1.2.2 riastrad /** Returns a reference to the object named by the handle. */
395 1.1.1.1.2.2 riastrad struct drm_gem_object *
396 1.1.1.1.2.2 riastrad drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
397 1.1.1.1.2.2 riastrad u32 handle)
398 1.1.1.1.2.2 riastrad {
399 1.1.1.1.2.2 riastrad struct drm_gem_object *obj;
400 1.1.1.1.2.2 riastrad
401 1.1.1.1.2.2 riastrad spin_lock(&filp->table_lock);
402 1.1.1.1.2.2 riastrad
403 1.1.1.1.2.2 riastrad /* Check if we currently have a reference on the object */
404 1.1.1.1.2.2 riastrad obj = idr_find(&filp->object_idr, handle);
405 1.1.1.1.2.2 riastrad if (obj == NULL) {
406 1.1.1.1.2.2 riastrad spin_unlock(&filp->table_lock);
407 1.1.1.1.2.2 riastrad return NULL;
408 1.1.1.1.2.2 riastrad }
409 1.1.1.1.2.2 riastrad
410 1.1.1.1.2.2 riastrad drm_gem_object_reference(obj);
411 1.1.1.1.2.2 riastrad
412 1.1.1.1.2.2 riastrad spin_unlock(&filp->table_lock);
413 1.1.1.1.2.2 riastrad
414 1.1.1.1.2.2 riastrad return obj;
415 1.1.1.1.2.2 riastrad }
416 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_object_lookup);
417 1.1.1.1.2.2 riastrad
418 1.1.1.1.2.2 riastrad /**
419 1.1.1.1.2.2 riastrad * Releases the handle to an mm object.
420 1.1.1.1.2.2 riastrad */
421 1.1.1.1.2.2 riastrad int
422 1.1.1.1.2.2 riastrad drm_gem_close_ioctl(struct drm_device *dev, void *data,
423 1.1.1.1.2.2 riastrad struct drm_file *file_priv)
424 1.1.1.1.2.2 riastrad {
425 1.1.1.1.2.2 riastrad struct drm_gem_close *args = data;
426 1.1.1.1.2.2 riastrad int ret;
427 1.1.1.1.2.2 riastrad
428 1.1.1.1.2.2 riastrad if (!(dev->driver->driver_features & DRIVER_GEM))
429 1.1.1.1.2.2 riastrad return -ENODEV;
430 1.1.1.1.2.2 riastrad
431 1.1.1.1.2.2 riastrad ret = drm_gem_handle_delete(file_priv, args->handle);
432 1.1.1.1.2.2 riastrad
433 1.1.1.1.2.2 riastrad return ret;
434 1.1.1.1.2.2 riastrad }
435 1.1.1.1.2.2 riastrad
436 1.1.1.1.2.2 riastrad /**
437 1.1.1.1.2.2 riastrad * Create a global name for an object, returning the name.
438 1.1.1.1.2.2 riastrad *
439 1.1.1.1.2.2 riastrad * Note that the name does not hold a reference; when the object
440 1.1.1.1.2.2 riastrad * is freed, the name goes away.
441 1.1.1.1.2.2 riastrad */
442 1.1.1.1.2.2 riastrad int
443 1.1.1.1.2.2 riastrad drm_gem_flink_ioctl(struct drm_device *dev, void *data,
444 1.1.1.1.2.2 riastrad struct drm_file *file_priv)
445 1.1.1.1.2.2 riastrad {
446 1.1.1.1.2.2 riastrad struct drm_gem_flink *args = data;
447 1.1.1.1.2.2 riastrad struct drm_gem_object *obj;
448 1.1.1.1.2.2 riastrad int ret;
449 1.1.1.1.2.2 riastrad
450 1.1.1.1.2.2 riastrad if (!(dev->driver->driver_features & DRIVER_GEM))
451 1.1.1.1.2.2 riastrad return -ENODEV;
452 1.1.1.1.2.2 riastrad
453 1.1.1.1.2.2 riastrad obj = drm_gem_object_lookup(dev, file_priv, args->handle);
454 1.1.1.1.2.2 riastrad if (obj == NULL)
455 1.1.1.1.2.2 riastrad return -ENOENT;
456 1.1.1.1.2.2 riastrad
457 1.1.1.1.2.2 riastrad again:
458 1.1.1.1.2.2 riastrad if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
459 1.1.1.1.2.2 riastrad ret = -ENOMEM;
460 1.1.1.1.2.2 riastrad goto err;
461 1.1.1.1.2.2 riastrad }
462 1.1.1.1.2.2 riastrad
463 1.1.1.1.2.2 riastrad spin_lock(&dev->object_name_lock);
464 1.1.1.1.2.2 riastrad if (!obj->name) {
465 1.1.1.1.2.2 riastrad ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
466 1.1.1.1.2.2 riastrad &obj->name);
467 1.1.1.1.2.2 riastrad args->name = (uint64_t) obj->name;
468 1.1.1.1.2.2 riastrad spin_unlock(&dev->object_name_lock);
469 1.1.1.1.2.2 riastrad
470 1.1.1.1.2.2 riastrad if (ret == -EAGAIN)
471 1.1.1.1.2.2 riastrad goto again;
472 1.1.1.1.2.2 riastrad else if (ret)
473 1.1.1.1.2.2 riastrad goto err;
474 1.1.1.1.2.2 riastrad
475 1.1.1.1.2.2 riastrad /* Allocate a reference for the name table. */
476 1.1.1.1.2.2 riastrad drm_gem_object_reference(obj);
477 1.1.1.1.2.2 riastrad } else {
478 1.1.1.1.2.2 riastrad args->name = (uint64_t) obj->name;
479 1.1.1.1.2.2 riastrad spin_unlock(&dev->object_name_lock);
480 1.1.1.1.2.2 riastrad ret = 0;
481 1.1.1.1.2.2 riastrad }
482 1.1.1.1.2.2 riastrad
483 1.1.1.1.2.2 riastrad err:
484 1.1.1.1.2.2 riastrad drm_gem_object_unreference_unlocked(obj);
485 1.1.1.1.2.2 riastrad return ret;
486 1.1.1.1.2.2 riastrad }
487 1.1.1.1.2.2 riastrad
488 1.1.1.1.2.2 riastrad /**
489 1.1.1.1.2.2 riastrad * Open an object using the global name, returning a handle and the size.
490 1.1.1.1.2.2 riastrad *
491 1.1.1.1.2.2 riastrad * This handle (of course) holds a reference to the object, so the object
492 1.1.1.1.2.2 riastrad * will not go away until the handle is deleted.
493 1.1.1.1.2.2 riastrad */
494 1.1.1.1.2.2 riastrad int
495 1.1.1.1.2.2 riastrad drm_gem_open_ioctl(struct drm_device *dev, void *data,
496 1.1.1.1.2.2 riastrad struct drm_file *file_priv)
497 1.1.1.1.2.2 riastrad {
498 1.1.1.1.2.2 riastrad struct drm_gem_open *args = data;
499 1.1.1.1.2.2 riastrad struct drm_gem_object *obj;
500 1.1.1.1.2.2 riastrad int ret;
501 1.1.1.1.2.2 riastrad u32 handle;
502 1.1.1.1.2.2 riastrad
503 1.1.1.1.2.2 riastrad if (!(dev->driver->driver_features & DRIVER_GEM))
504 1.1.1.1.2.2 riastrad return -ENODEV;
505 1.1.1.1.2.2 riastrad
506 1.1.1.1.2.2 riastrad spin_lock(&dev->object_name_lock);
507 1.1.1.1.2.2 riastrad obj = idr_find(&dev->object_name_idr, (int) args->name);
508 1.1.1.1.2.2 riastrad if (obj)
509 1.1.1.1.2.2 riastrad drm_gem_object_reference(obj);
510 1.1.1.1.2.2 riastrad spin_unlock(&dev->object_name_lock);
511 1.1.1.1.2.2 riastrad if (!obj)
512 1.1.1.1.2.2 riastrad return -ENOENT;
513 1.1.1.1.2.2 riastrad
514 1.1.1.1.2.2 riastrad ret = drm_gem_handle_create(file_priv, obj, &handle);
515 1.1.1.1.2.2 riastrad drm_gem_object_unreference_unlocked(obj);
516 1.1.1.1.2.2 riastrad if (ret)
517 1.1.1.1.2.2 riastrad return ret;
518 1.1.1.1.2.2 riastrad
519 1.1.1.1.2.2 riastrad args->handle = handle;
520 1.1.1.1.2.2 riastrad args->size = obj->size;
521 1.1.1.1.2.2 riastrad
522 1.1.1.1.2.2 riastrad return 0;
523 1.1.1.1.2.2 riastrad }
524 1.1.1.1.2.2 riastrad
525 1.1.1.1.2.2 riastrad /**
526 1.1.1.1.2.2 riastrad * Called at device open time, sets up the structure for handling refcounting
527 1.1.1.1.2.2 riastrad * of mm objects.
528 1.1.1.1.2.2 riastrad */
529 1.1.1.1.2.2 riastrad void
530 1.1.1.1.2.2 riastrad drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
531 1.1.1.1.2.2 riastrad {
532 1.1.1.1.2.2 riastrad idr_init(&file_private->object_idr);
533 1.1.1.1.2.2 riastrad spin_lock_init(&file_private->table_lock);
534 1.1.1.1.2.2 riastrad }
535 1.1.1.1.2.2 riastrad
536 1.1.1.1.2.2 riastrad /**
537 1.1.1.1.2.2 riastrad * Called at device close to release the file's
538 1.1.1.1.2.2 riastrad * handle references on objects.
539 1.1.1.1.2.2 riastrad */
540 1.1.1.1.2.2 riastrad static int
541 1.1.1.1.2.2 riastrad drm_gem_object_release_handle(int id, void *ptr, void *data)
542 1.1.1.1.2.2 riastrad {
543 1.1.1.1.2.2 riastrad struct drm_file *file_priv = data;
544 1.1.1.1.2.2 riastrad struct drm_gem_object *obj = ptr;
545 1.1.1.1.2.2 riastrad struct drm_device *dev = obj->dev;
546 1.1.1.1.2.2 riastrad
547 1.1.1.1.2.2 riastrad drm_gem_remove_prime_handles(obj, file_priv);
548 1.1.1.1.2.2 riastrad
549 1.1.1.1.2.2 riastrad if (dev->driver->gem_close_object)
550 1.1.1.1.2.2 riastrad dev->driver->gem_close_object(obj, file_priv);
551 1.1.1.1.2.2 riastrad
552 1.1.1.1.2.2 riastrad drm_gem_object_handle_unreference_unlocked(obj);
553 1.1.1.1.2.2 riastrad
554 1.1.1.1.2.2 riastrad return 0;
555 1.1.1.1.2.2 riastrad }
556 1.1.1.1.2.2 riastrad
557 1.1.1.1.2.2 riastrad /**
558 1.1.1.1.2.2 riastrad * Called at close time when the filp is going away.
559 1.1.1.1.2.2 riastrad *
560 1.1.1.1.2.2 riastrad * Releases any remaining references on objects by this filp.
561 1.1.1.1.2.2 riastrad */
562 1.1.1.1.2.2 riastrad void
563 1.1.1.1.2.2 riastrad drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
564 1.1.1.1.2.2 riastrad {
565 1.1.1.1.2.2 riastrad idr_for_each(&file_private->object_idr,
566 1.1.1.1.2.2 riastrad &drm_gem_object_release_handle, file_private);
567 1.1.1.1.2.2 riastrad
568 1.1.1.1.2.2 riastrad idr_remove_all(&file_private->object_idr);
569 1.1.1.1.2.2 riastrad idr_destroy(&file_private->object_idr);
570 1.1.1.1.2.2 riastrad }
571 1.1.1.1.2.2 riastrad
572 1.1.1.1.2.2 riastrad void
573 1.1.1.1.2.2 riastrad drm_gem_object_release(struct drm_gem_object *obj)
574 1.1.1.1.2.2 riastrad {
575 1.1.1.1.2.2 riastrad if (obj->filp)
576 1.1.1.1.2.2 riastrad fput(obj->filp);
577 1.1.1.1.2.2 riastrad }
578 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_object_release);
579 1.1.1.1.2.2 riastrad
580 1.1.1.1.2.2 riastrad /**
581 1.1.1.1.2.2 riastrad * Called after the last reference to the object has been lost.
582 1.1.1.1.2.2 riastrad * Must be called holding struct_ mutex
583 1.1.1.1.2.2 riastrad *
584 1.1.1.1.2.2 riastrad * Frees the object
585 1.1.1.1.2.2 riastrad */
586 1.1.1.1.2.2 riastrad void
587 1.1.1.1.2.2 riastrad drm_gem_object_free(struct kref *kref)
588 1.1.1.1.2.2 riastrad {
589 1.1.1.1.2.2 riastrad struct drm_gem_object *obj = (struct drm_gem_object *) kref;
590 1.1.1.1.2.2 riastrad struct drm_device *dev = obj->dev;
591 1.1.1.1.2.2 riastrad
592 1.1.1.1.2.2 riastrad BUG_ON(!mutex_is_locked(&dev->struct_mutex));
593 1.1.1.1.2.2 riastrad
594 1.1.1.1.2.2 riastrad if (dev->driver->gem_free_object != NULL)
595 1.1.1.1.2.2 riastrad dev->driver->gem_free_object(obj);
596 1.1.1.1.2.2 riastrad }
597 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_object_free);
598 1.1.1.1.2.2 riastrad
599 1.1.1.1.2.2 riastrad static void drm_gem_object_ref_bug(struct kref *list_kref)
600 1.1.1.1.2.2 riastrad {
601 1.1.1.1.2.2 riastrad BUG();
602 1.1.1.1.2.2 riastrad }
603 1.1.1.1.2.2 riastrad
604 1.1.1.1.2.2 riastrad /**
605 1.1.1.1.2.2 riastrad * Called after the last handle to the object has been closed
606 1.1.1.1.2.2 riastrad *
607 1.1.1.1.2.2 riastrad * Removes any name for the object. Note that this must be
608 1.1.1.1.2.2 riastrad * called before drm_gem_object_free or we'll be touching
609 1.1.1.1.2.2 riastrad * freed memory
610 1.1.1.1.2.2 riastrad */
611 1.1.1.1.2.2 riastrad void drm_gem_object_handle_free(struct drm_gem_object *obj)
612 1.1.1.1.2.2 riastrad {
613 1.1.1.1.2.2 riastrad struct drm_device *dev = obj->dev;
614 1.1.1.1.2.2 riastrad
615 1.1.1.1.2.2 riastrad /* Remove any name for this object */
616 1.1.1.1.2.2 riastrad spin_lock(&dev->object_name_lock);
617 1.1.1.1.2.2 riastrad if (obj->name) {
618 1.1.1.1.2.2 riastrad idr_remove(&dev->object_name_idr, obj->name);
619 1.1.1.1.2.2 riastrad obj->name = 0;
620 1.1.1.1.2.2 riastrad spin_unlock(&dev->object_name_lock);
621 1.1.1.1.2.2 riastrad /*
622 1.1.1.1.2.2 riastrad * The object name held a reference to this object, drop
623 1.1.1.1.2.2 riastrad * that now.
624 1.1.1.1.2.2 riastrad *
625 1.1.1.1.2.2 riastrad * This cannot be the last reference, since the handle holds one too.
626 1.1.1.1.2.2 riastrad */
627 1.1.1.1.2.2 riastrad kref_put(&obj->refcount, drm_gem_object_ref_bug);
628 1.1.1.1.2.2 riastrad } else
629 1.1.1.1.2.2 riastrad spin_unlock(&dev->object_name_lock);
630 1.1.1.1.2.2 riastrad
631 1.1.1.1.2.2 riastrad }
632 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_object_handle_free);
633 1.1.1.1.2.2 riastrad
634 1.1.1.1.2.2 riastrad void drm_gem_vm_open(struct vm_area_struct *vma)
635 1.1.1.1.2.2 riastrad {
636 1.1.1.1.2.2 riastrad struct drm_gem_object *obj = vma->vm_private_data;
637 1.1.1.1.2.2 riastrad
638 1.1.1.1.2.2 riastrad drm_gem_object_reference(obj);
639 1.1.1.1.2.2 riastrad
640 1.1.1.1.2.2 riastrad mutex_lock(&obj->dev->struct_mutex);
641 1.1.1.1.2.2 riastrad drm_vm_open_locked(obj->dev, vma);
642 1.1.1.1.2.2 riastrad mutex_unlock(&obj->dev->struct_mutex);
643 1.1.1.1.2.2 riastrad }
644 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_vm_open);
645 1.1.1.1.2.2 riastrad
646 1.1.1.1.2.2 riastrad void drm_gem_vm_close(struct vm_area_struct *vma)
647 1.1.1.1.2.2 riastrad {
648 1.1.1.1.2.2 riastrad struct drm_gem_object *obj = vma->vm_private_data;
649 1.1.1.1.2.2 riastrad struct drm_device *dev = obj->dev;
650 1.1.1.1.2.2 riastrad
651 1.1.1.1.2.2 riastrad mutex_lock(&dev->struct_mutex);
652 1.1.1.1.2.2 riastrad drm_vm_close_locked(obj->dev, vma);
653 1.1.1.1.2.2 riastrad drm_gem_object_unreference(obj);
654 1.1.1.1.2.2 riastrad mutex_unlock(&dev->struct_mutex);
655 1.1.1.1.2.2 riastrad }
656 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_vm_close);
657 1.1.1.1.2.2 riastrad
658 1.1.1.1.2.2 riastrad
659 1.1.1.1.2.2 riastrad /**
660 1.1.1.1.2.2 riastrad * drm_gem_mmap - memory map routine for GEM objects
661 1.1.1.1.2.2 riastrad * @filp: DRM file pointer
662 1.1.1.1.2.2 riastrad * @vma: VMA for the area to be mapped
663 1.1.1.1.2.2 riastrad *
664 1.1.1.1.2.2 riastrad * If a driver supports GEM object mapping, mmap calls on the DRM file
665 1.1.1.1.2.2 riastrad * descriptor will end up here.
666 1.1.1.1.2.2 riastrad *
667 1.1.1.1.2.2 riastrad * If we find the object based on the offset passed in (vma->vm_pgoff will
668 1.1.1.1.2.2 riastrad * contain the fake offset we created when the GTT map ioctl was called on
669 1.1.1.1.2.2 riastrad * the object), we set up the driver fault handler so that any accesses
670 1.1.1.1.2.2 riastrad * to the object can be trapped, to perform migration, GTT binding, surface
671 1.1.1.1.2.2 riastrad * register allocation, or performance monitoring.
672 1.1.1.1.2.2 riastrad */
673 1.1.1.1.2.2 riastrad int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
674 1.1.1.1.2.2 riastrad {
675 1.1.1.1.2.2 riastrad struct drm_file *priv = filp->private_data;
676 1.1.1.1.2.2 riastrad struct drm_device *dev = priv->minor->dev;
677 1.1.1.1.2.2 riastrad struct drm_gem_mm *mm = dev->mm_private;
678 1.1.1.1.2.2 riastrad struct drm_local_map *map = NULL;
679 1.1.1.1.2.2 riastrad struct drm_gem_object *obj;
680 1.1.1.1.2.2 riastrad struct drm_hash_item *hash;
681 1.1.1.1.2.2 riastrad int ret = 0;
682 1.1.1.1.2.2 riastrad
683 1.1.1.1.2.2 riastrad if (drm_device_is_unplugged(dev))
684 1.1.1.1.2.2 riastrad return -ENODEV;
685 1.1.1.1.2.2 riastrad
686 1.1.1.1.2.2 riastrad mutex_lock(&dev->struct_mutex);
687 1.1.1.1.2.2 riastrad
688 1.1.1.1.2.2 riastrad if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
689 1.1.1.1.2.2 riastrad mutex_unlock(&dev->struct_mutex);
690 1.1.1.1.2.2 riastrad return drm_mmap(filp, vma);
691 1.1.1.1.2.2 riastrad }
692 1.1.1.1.2.2 riastrad
693 1.1.1.1.2.2 riastrad map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
694 1.1.1.1.2.2 riastrad if (!map ||
695 1.1.1.1.2.2 riastrad ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
696 1.1.1.1.2.2 riastrad ret = -EPERM;
697 1.1.1.1.2.2 riastrad goto out_unlock;
698 1.1.1.1.2.2 riastrad }
699 1.1.1.1.2.2 riastrad
700 1.1.1.1.2.2 riastrad /* Check for valid size. */
701 1.1.1.1.2.2 riastrad if (map->size < vma->vm_end - vma->vm_start) {
702 1.1.1.1.2.2 riastrad ret = -EINVAL;
703 1.1.1.1.2.2 riastrad goto out_unlock;
704 1.1.1.1.2.2 riastrad }
705 1.1.1.1.2.2 riastrad
706 1.1.1.1.2.2 riastrad obj = map->handle;
707 1.1.1.1.2.2 riastrad if (!obj->dev->driver->gem_vm_ops) {
708 1.1.1.1.2.2 riastrad ret = -EINVAL;
709 1.1.1.1.2.2 riastrad goto out_unlock;
710 1.1.1.1.2.2 riastrad }
711 1.1.1.1.2.2 riastrad
712 1.1.1.1.2.2 riastrad vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
713 1.1.1.1.2.2 riastrad vma->vm_ops = obj->dev->driver->gem_vm_ops;
714 1.1.1.1.2.2 riastrad vma->vm_private_data = map->handle;
715 1.1.1.1.2.2 riastrad vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
716 1.1.1.1.2.2 riastrad
717 1.1.1.1.2.2 riastrad /* Take a ref for this mapping of the object, so that the fault
718 1.1.1.1.2.2 riastrad * handler can dereference the mmap offset's pointer to the object.
719 1.1.1.1.2.2 riastrad * This reference is cleaned up by the corresponding vm_close
720 1.1.1.1.2.2 riastrad * (which should happen whether the vma was created by this call, or
721 1.1.1.1.2.2 riastrad * by a vm_open due to mremap or partial unmap or whatever).
722 1.1.1.1.2.2 riastrad */
723 1.1.1.1.2.2 riastrad drm_gem_object_reference(obj);
724 1.1.1.1.2.2 riastrad
725 1.1.1.1.2.2 riastrad drm_vm_open_locked(dev, vma);
726 1.1.1.1.2.2 riastrad
727 1.1.1.1.2.2 riastrad out_unlock:
728 1.1.1.1.2.2 riastrad mutex_unlock(&dev->struct_mutex);
729 1.1.1.1.2.2 riastrad
730 1.1.1.1.2.2 riastrad return ret;
731 1.1.1.1.2.2 riastrad }
732 1.1.1.1.2.2 riastrad EXPORT_SYMBOL(drm_gem_mmap);
733