drm_gem.c revision 1.1.1.2 1 1.1 riastrad /*
2 1.1 riastrad * Copyright 2008 Intel Corporation
3 1.1 riastrad *
4 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
5 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
6 1.1 riastrad * to deal in the Software without restriction, including without limitation
7 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
9 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
10 1.1 riastrad *
11 1.1 riastrad * The above copyright notice and this permission notice (including the next
12 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
13 1.1 riastrad * Software.
14 1.1 riastrad *
15 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 1.1 riastrad * IN THE SOFTWARE.
22 1.1 riastrad *
23 1.1 riastrad * Authors:
24 1.1 riastrad * Eric Anholt <eric (at) anholt.net>
25 1.1 riastrad *
26 1.1 riastrad */
27 1.1 riastrad
28 1.1 riastrad #include <linux/types.h>
29 1.1 riastrad #include <linux/slab.h>
30 1.1 riastrad #include <linux/mm.h>
31 1.1 riastrad #include <linux/uaccess.h>
32 1.1 riastrad #include <linux/fs.h>
33 1.1 riastrad #include <linux/file.h>
34 1.1 riastrad #include <linux/module.h>
35 1.1 riastrad #include <linux/mman.h>
36 1.1 riastrad #include <linux/pagemap.h>
37 1.1 riastrad #include <linux/shmem_fs.h>
38 1.1 riastrad #include <linux/dma-buf.h>
39 1.1 riastrad #include <drm/drmP.h>
40 1.1.1.2 riastrad #include <drm/drm_vma_manager.h>
41 1.1 riastrad
42 1.1 riastrad /** @file drm_gem.c
43 1.1 riastrad *
44 1.1 riastrad * This file provides some of the base ioctls and library routines for
45 1.1 riastrad * the graphics memory manager implemented by each device driver.
46 1.1 riastrad *
47 1.1 riastrad * Because various devices have different requirements in terms of
48 1.1 riastrad * synchronization and migration strategies, implementing that is left up to
49 1.1 riastrad * the driver, and all that the general API provides should be generic --
50 1.1 riastrad * allocating objects, reading/writing data with the cpu, freeing objects.
51 1.1 riastrad * Even there, platform-dependent optimizations for reading/writing data with
52 1.1 riastrad * the CPU mean we'll likely hook those out to driver-specific calls. However,
53 1.1 riastrad * the DRI2 implementation wants to have at least allocate/mmap be generic.
54 1.1 riastrad *
55 1.1 riastrad * The goal was to have swap-backed object allocation managed through
56 1.1 riastrad * struct file. However, file descriptors as handles to a struct file have
57 1.1 riastrad * two major failings:
58 1.1 riastrad * - Process limits prevent more than 1024 or so being used at a time by
59 1.1 riastrad * default.
60 1.1 riastrad * - Inability to allocate high fds will aggravate the X Server's select()
61 1.1 riastrad * handling, and likely that of many GL client applications as well.
62 1.1 riastrad *
63 1.1 riastrad * This led to a plan of using our own integer IDs (called handles, following
64 1.1 riastrad * DRM terminology) to mimic fds, and implement the fd syscalls we need as
65 1.1 riastrad * ioctls. The objects themselves will still include the struct file so
66 1.1 riastrad * that we can transition to fds if the required kernel infrastructure shows
67 1.1 riastrad * up at a later date, and as our interface with shmfs for memory allocation.
68 1.1 riastrad */
69 1.1 riastrad
70 1.1 riastrad /*
71 1.1 riastrad * We make up offsets for buffer objects so we can recognize them at
72 1.1 riastrad * mmap time.
73 1.1 riastrad */
74 1.1 riastrad
75 1.1 riastrad /* pgoff in mmap is an unsigned long, so we need to make sure that
76 1.1 riastrad * the faked up offset will fit
77 1.1 riastrad */
78 1.1 riastrad
79 1.1 riastrad #if BITS_PER_LONG == 64
80 1.1 riastrad #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
81 1.1 riastrad #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
82 1.1 riastrad #else
83 1.1 riastrad #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
84 1.1 riastrad #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
85 1.1 riastrad #endif
86 1.1 riastrad
87 1.1 riastrad /**
88 1.1.1.2 riastrad * drm_gem_init - Initialize the GEM device fields
89 1.1.1.2 riastrad * @dev: drm_devic structure to initialize
90 1.1 riastrad */
91 1.1 riastrad int
92 1.1 riastrad drm_gem_init(struct drm_device *dev)
93 1.1 riastrad {
94 1.1.1.2 riastrad struct drm_vma_offset_manager *vma_offset_manager;
95 1.1 riastrad
96 1.1.1.2 riastrad mutex_init(&dev->object_name_lock);
97 1.1 riastrad idr_init(&dev->object_name_idr);
98 1.1 riastrad
99 1.1.1.2 riastrad vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
100 1.1.1.2 riastrad if (!vma_offset_manager) {
101 1.1 riastrad DRM_ERROR("out of memory\n");
102 1.1 riastrad return -ENOMEM;
103 1.1 riastrad }
104 1.1 riastrad
105 1.1.1.2 riastrad dev->vma_offset_manager = vma_offset_manager;
106 1.1.1.2 riastrad drm_vma_offset_manager_init(vma_offset_manager,
107 1.1.1.2 riastrad DRM_FILE_PAGE_OFFSET_START,
108 1.1.1.2 riastrad DRM_FILE_PAGE_OFFSET_SIZE);
109 1.1 riastrad
110 1.1 riastrad return 0;
111 1.1 riastrad }
112 1.1 riastrad
113 1.1 riastrad void
114 1.1 riastrad drm_gem_destroy(struct drm_device *dev)
115 1.1 riastrad {
116 1.1 riastrad
117 1.1.1.2 riastrad drm_vma_offset_manager_destroy(dev->vma_offset_manager);
118 1.1.1.2 riastrad kfree(dev->vma_offset_manager);
119 1.1.1.2 riastrad dev->vma_offset_manager = NULL;
120 1.1 riastrad }
121 1.1 riastrad
122 1.1 riastrad /**
123 1.1.1.2 riastrad * drm_gem_object_init - initialize an allocated shmem-backed GEM object
124 1.1.1.2 riastrad * @dev: drm_device the object should be initialized for
125 1.1.1.2 riastrad * @obj: drm_gem_object to initialize
126 1.1.1.2 riastrad * @size: object size
127 1.1.1.2 riastrad *
128 1.1 riastrad * Initialize an already allocated GEM object of the specified size with
129 1.1 riastrad * shmfs backing store.
130 1.1 riastrad */
131 1.1 riastrad int drm_gem_object_init(struct drm_device *dev,
132 1.1 riastrad struct drm_gem_object *obj, size_t size)
133 1.1 riastrad {
134 1.1.1.2 riastrad struct file *filp;
135 1.1 riastrad
136 1.1.1.2 riastrad drm_gem_private_object_init(dev, obj, size);
137 1.1 riastrad
138 1.1.1.2 riastrad filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
139 1.1.1.2 riastrad if (IS_ERR(filp))
140 1.1.1.2 riastrad return PTR_ERR(filp);
141 1.1.1.2 riastrad
142 1.1.1.2 riastrad obj->filp = filp;
143 1.1 riastrad
144 1.1 riastrad return 0;
145 1.1 riastrad }
146 1.1 riastrad EXPORT_SYMBOL(drm_gem_object_init);
147 1.1 riastrad
148 1.1 riastrad /**
149 1.1.1.2 riastrad * drm_gem_object_init - initialize an allocated private GEM object
150 1.1.1.2 riastrad * @dev: drm_device the object should be initialized for
151 1.1.1.2 riastrad * @obj: drm_gem_object to initialize
152 1.1.1.2 riastrad * @size: object size
153 1.1.1.2 riastrad *
154 1.1 riastrad * Initialize an already allocated GEM object of the specified size with
155 1.1 riastrad * no GEM provided backing store. Instead the caller is responsible for
156 1.1 riastrad * backing the object and handling it.
157 1.1 riastrad */
158 1.1.1.2 riastrad void drm_gem_private_object_init(struct drm_device *dev,
159 1.1.1.2 riastrad struct drm_gem_object *obj, size_t size)
160 1.1 riastrad {
161 1.1 riastrad BUG_ON((size & (PAGE_SIZE - 1)) != 0);
162 1.1 riastrad
163 1.1 riastrad obj->dev = dev;
164 1.1 riastrad obj->filp = NULL;
165 1.1 riastrad
166 1.1 riastrad kref_init(&obj->refcount);
167 1.1.1.2 riastrad obj->handle_count = 0;
168 1.1 riastrad obj->size = size;
169 1.1.1.2 riastrad drm_vma_node_reset(&obj->vma_node);
170 1.1 riastrad }
171 1.1 riastrad EXPORT_SYMBOL(drm_gem_private_object_init);
172 1.1 riastrad
173 1.1.1.2 riastrad static void
174 1.1.1.2 riastrad drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
175 1.1.1.2 riastrad {
176 1.1.1.2 riastrad /*
177 1.1.1.2 riastrad * Note: obj->dma_buf can't disappear as long as we still hold a
178 1.1.1.2 riastrad * handle reference in obj->handle_count.
179 1.1.1.2 riastrad */
180 1.1.1.2 riastrad mutex_lock(&filp->prime.lock);
181 1.1.1.2 riastrad if (obj->dma_buf) {
182 1.1.1.2 riastrad drm_prime_remove_buf_handle_locked(&filp->prime,
183 1.1.1.2 riastrad obj->dma_buf);
184 1.1.1.2 riastrad }
185 1.1.1.2 riastrad mutex_unlock(&filp->prime.lock);
186 1.1.1.2 riastrad }
187 1.1.1.2 riastrad
188 1.1 riastrad /**
189 1.1.1.2 riastrad * drm_gem_object_free - release resources bound to userspace handles
190 1.1.1.2 riastrad * @obj: GEM object to clean up.
191 1.1.1.2 riastrad *
192 1.1.1.2 riastrad * Called after the last handle to the object has been closed
193 1.1.1.2 riastrad *
194 1.1.1.2 riastrad * Removes any name for the object. Note that this must be
195 1.1.1.2 riastrad * called before drm_gem_object_free or we'll be touching
196 1.1.1.2 riastrad * freed memory
197 1.1 riastrad */
198 1.1.1.2 riastrad static void drm_gem_object_handle_free(struct drm_gem_object *obj)
199 1.1 riastrad {
200 1.1.1.2 riastrad struct drm_device *dev = obj->dev;
201 1.1 riastrad
202 1.1.1.2 riastrad /* Remove any name for this object */
203 1.1.1.2 riastrad if (obj->name) {
204 1.1.1.2 riastrad idr_remove(&dev->object_name_idr, obj->name);
205 1.1.1.2 riastrad obj->name = 0;
206 1.1 riastrad }
207 1.1 riastrad }
208 1.1 riastrad
209 1.1.1.2 riastrad static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
210 1.1 riastrad {
211 1.1.1.2 riastrad /* Unbreak the reference cycle if we have an exported dma_buf. */
212 1.1.1.2 riastrad if (obj->dma_buf) {
213 1.1.1.2 riastrad dma_buf_put(obj->dma_buf);
214 1.1.1.2 riastrad obj->dma_buf = NULL;
215 1.1 riastrad }
216 1.1.1.2 riastrad }
217 1.1.1.2 riastrad
218 1.1.1.2 riastrad static void
219 1.1.1.2 riastrad drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
220 1.1.1.2 riastrad {
221 1.1.1.2 riastrad if (WARN_ON(obj->handle_count == 0))
222 1.1.1.2 riastrad return;
223 1.1.1.2 riastrad
224 1.1.1.2 riastrad /*
225 1.1.1.2 riastrad * Must bump handle count first as this may be the last
226 1.1.1.2 riastrad * ref, in which case the object would disappear before we
227 1.1.1.2 riastrad * checked for a name
228 1.1.1.2 riastrad */
229 1.1.1.2 riastrad
230 1.1.1.2 riastrad mutex_lock(&obj->dev->object_name_lock);
231 1.1.1.2 riastrad if (--obj->handle_count == 0) {
232 1.1.1.2 riastrad drm_gem_object_handle_free(obj);
233 1.1.1.2 riastrad drm_gem_object_exported_dma_buf_free(obj);
234 1.1 riastrad }
235 1.1.1.2 riastrad mutex_unlock(&obj->dev->object_name_lock);
236 1.1.1.2 riastrad
237 1.1.1.2 riastrad drm_gem_object_unreference_unlocked(obj);
238 1.1 riastrad }
239 1.1 riastrad
240 1.1 riastrad /**
241 1.1.1.2 riastrad * drm_gem_handle_delete - deletes the given file-private handle
242 1.1.1.2 riastrad * @filp: drm file-private structure to use for the handle look up
243 1.1.1.2 riastrad * @handle: userspace handle to delete
244 1.1.1.2 riastrad *
245 1.1.1.2 riastrad * Removes the GEM handle from the @filp lookup table and if this is the last
246 1.1.1.2 riastrad * handle also cleans up linked resources like GEM names.
247 1.1 riastrad */
248 1.1 riastrad int
249 1.1 riastrad drm_gem_handle_delete(struct drm_file *filp, u32 handle)
250 1.1 riastrad {
251 1.1 riastrad struct drm_device *dev;
252 1.1 riastrad struct drm_gem_object *obj;
253 1.1 riastrad
254 1.1 riastrad /* This is gross. The idr system doesn't let us try a delete and
255 1.1 riastrad * return an error code. It just spews if you fail at deleting.
256 1.1 riastrad * So, we have to grab a lock around finding the object and then
257 1.1 riastrad * doing the delete on it and dropping the refcount, or the user
258 1.1 riastrad * could race us to double-decrement the refcount and cause a
259 1.1 riastrad * use-after-free later. Given the frequency of our handle lookups,
260 1.1 riastrad * we may want to use ida for number allocation and a hash table
261 1.1 riastrad * for the pointers, anyway.
262 1.1 riastrad */
263 1.1 riastrad spin_lock(&filp->table_lock);
264 1.1 riastrad
265 1.1 riastrad /* Check if we currently have a reference on the object */
266 1.1 riastrad obj = idr_find(&filp->object_idr, handle);
267 1.1 riastrad if (obj == NULL) {
268 1.1 riastrad spin_unlock(&filp->table_lock);
269 1.1 riastrad return -EINVAL;
270 1.1 riastrad }
271 1.1 riastrad dev = obj->dev;
272 1.1 riastrad
273 1.1 riastrad /* Release reference and decrement refcount. */
274 1.1 riastrad idr_remove(&filp->object_idr, handle);
275 1.1 riastrad spin_unlock(&filp->table_lock);
276 1.1 riastrad
277 1.1.1.2 riastrad if (drm_core_check_feature(dev, DRIVER_PRIME))
278 1.1.1.2 riastrad drm_gem_remove_prime_handles(obj, filp);
279 1.1.1.2 riastrad drm_vma_node_revoke(&obj->vma_node, filp->filp);
280 1.1 riastrad
281 1.1 riastrad if (dev->driver->gem_close_object)
282 1.1 riastrad dev->driver->gem_close_object(obj, filp);
283 1.1 riastrad drm_gem_object_handle_unreference_unlocked(obj);
284 1.1 riastrad
285 1.1 riastrad return 0;
286 1.1 riastrad }
287 1.1 riastrad EXPORT_SYMBOL(drm_gem_handle_delete);
288 1.1 riastrad
289 1.1 riastrad /**
290 1.1.1.2 riastrad * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
291 1.1.1.2 riastrad * @file: drm file-private structure to remove the dumb handle from
292 1.1.1.2 riastrad * @dev: corresponding drm_device
293 1.1.1.2 riastrad * @handle: the dumb handle to remove
294 1.1.1.2 riastrad *
295 1.1.1.2 riastrad * This implements the ->dumb_destroy kms driver callback for drivers which use
296 1.1.1.2 riastrad * gem to manage their backing storage.
297 1.1.1.2 riastrad */
298 1.1.1.2 riastrad int drm_gem_dumb_destroy(struct drm_file *file,
299 1.1.1.2 riastrad struct drm_device *dev,
300 1.1.1.2 riastrad uint32_t handle)
301 1.1.1.2 riastrad {
302 1.1.1.2 riastrad return drm_gem_handle_delete(file, handle);
303 1.1.1.2 riastrad }
304 1.1.1.2 riastrad EXPORT_SYMBOL(drm_gem_dumb_destroy);
305 1.1.1.2 riastrad
306 1.1.1.2 riastrad /**
307 1.1.1.2 riastrad * drm_gem_handle_create_tail - internal functions to create a handle
308 1.1.1.2 riastrad * @file_priv: drm file-private structure to register the handle for
309 1.1.1.2 riastrad * @obj: object to register
310 1.1.1.2 riastrad * @handlep: pionter to return the created handle to the caller
311 1.1.1.2 riastrad *
312 1.1.1.2 riastrad * This expects the dev->object_name_lock to be held already and will drop it
313 1.1.1.2 riastrad * before returning. Used to avoid races in establishing new handles when
314 1.1.1.2 riastrad * importing an object from either an flink name or a dma-buf.
315 1.1 riastrad */
316 1.1 riastrad int
317 1.1.1.2 riastrad drm_gem_handle_create_tail(struct drm_file *file_priv,
318 1.1.1.2 riastrad struct drm_gem_object *obj,
319 1.1.1.2 riastrad u32 *handlep)
320 1.1 riastrad {
321 1.1 riastrad struct drm_device *dev = obj->dev;
322 1.1 riastrad int ret;
323 1.1 riastrad
324 1.1.1.2 riastrad WARN_ON(!mutex_is_locked(&dev->object_name_lock));
325 1.1.1.2 riastrad
326 1.1 riastrad /*
327 1.1.1.2 riastrad * Get the user-visible handle using idr. Preload and perform
328 1.1.1.2 riastrad * allocation under our spinlock.
329 1.1 riastrad */
330 1.1.1.2 riastrad idr_preload(GFP_KERNEL);
331 1.1 riastrad spin_lock(&file_priv->table_lock);
332 1.1.1.2 riastrad
333 1.1.1.2 riastrad ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
334 1.1.1.2 riastrad drm_gem_object_reference(obj);
335 1.1.1.2 riastrad obj->handle_count++;
336 1.1 riastrad spin_unlock(&file_priv->table_lock);
337 1.1.1.2 riastrad idr_preload_end();
338 1.1.1.2 riastrad mutex_unlock(&dev->object_name_lock);
339 1.1.1.2 riastrad if (ret < 0) {
340 1.1.1.2 riastrad drm_gem_object_handle_unreference_unlocked(obj);
341 1.1 riastrad return ret;
342 1.1.1.2 riastrad }
343 1.1.1.2 riastrad *handlep = ret;
344 1.1 riastrad
345 1.1.1.2 riastrad ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
346 1.1.1.2 riastrad if (ret) {
347 1.1.1.2 riastrad drm_gem_handle_delete(file_priv, *handlep);
348 1.1.1.2 riastrad return ret;
349 1.1.1.2 riastrad }
350 1.1 riastrad
351 1.1 riastrad if (dev->driver->gem_open_object) {
352 1.1 riastrad ret = dev->driver->gem_open_object(obj, file_priv);
353 1.1 riastrad if (ret) {
354 1.1 riastrad drm_gem_handle_delete(file_priv, *handlep);
355 1.1 riastrad return ret;
356 1.1 riastrad }
357 1.1 riastrad }
358 1.1 riastrad
359 1.1 riastrad return 0;
360 1.1 riastrad }
361 1.1.1.2 riastrad
362 1.1.1.2 riastrad /**
363 1.1.1.2 riastrad * gem_handle_create - create a gem handle for an object
364 1.1.1.2 riastrad * @file_priv: drm file-private structure to register the handle for
365 1.1.1.2 riastrad * @obj: object to register
366 1.1.1.2 riastrad * @handlep: pionter to return the created handle to the caller
367 1.1.1.2 riastrad *
368 1.1.1.2 riastrad * Create a handle for this object. This adds a handle reference
369 1.1.1.2 riastrad * to the object, which includes a regular reference count. Callers
370 1.1.1.2 riastrad * will likely want to dereference the object afterwards.
371 1.1.1.2 riastrad */
372 1.1.1.2 riastrad int
373 1.1.1.2 riastrad drm_gem_handle_create(struct drm_file *file_priv,
374 1.1.1.2 riastrad struct drm_gem_object *obj,
375 1.1.1.2 riastrad u32 *handlep)
376 1.1.1.2 riastrad {
377 1.1.1.2 riastrad mutex_lock(&obj->dev->object_name_lock);
378 1.1.1.2 riastrad
379 1.1.1.2 riastrad return drm_gem_handle_create_tail(file_priv, obj, handlep);
380 1.1.1.2 riastrad }
381 1.1 riastrad EXPORT_SYMBOL(drm_gem_handle_create);
382 1.1 riastrad
383 1.1 riastrad
384 1.1 riastrad /**
385 1.1 riastrad * drm_gem_free_mmap_offset - release a fake mmap offset for an object
386 1.1 riastrad * @obj: obj in question
387 1.1 riastrad *
388 1.1 riastrad * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
389 1.1 riastrad */
390 1.1 riastrad void
391 1.1 riastrad drm_gem_free_mmap_offset(struct drm_gem_object *obj)
392 1.1 riastrad {
393 1.1 riastrad struct drm_device *dev = obj->dev;
394 1.1 riastrad
395 1.1.1.2 riastrad drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
396 1.1 riastrad }
397 1.1 riastrad EXPORT_SYMBOL(drm_gem_free_mmap_offset);
398 1.1 riastrad
399 1.1 riastrad /**
400 1.1.1.2 riastrad * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
401 1.1 riastrad * @obj: obj in question
402 1.1.1.2 riastrad * @size: the virtual size
403 1.1 riastrad *
404 1.1 riastrad * GEM memory mapping works by handing back to userspace a fake mmap offset
405 1.1 riastrad * it can use in a subsequent mmap(2) call. The DRM core code then looks
406 1.1 riastrad * up the object based on the offset and sets up the various memory mapping
407 1.1 riastrad * structures.
408 1.1 riastrad *
409 1.1.1.2 riastrad * This routine allocates and attaches a fake offset for @obj, in cases where
410 1.1.1.2 riastrad * the virtual size differs from the physical size (ie. obj->size). Otherwise
411 1.1.1.2 riastrad * just use drm_gem_create_mmap_offset().
412 1.1 riastrad */
413 1.1 riastrad int
414 1.1.1.2 riastrad drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
415 1.1 riastrad {
416 1.1 riastrad struct drm_device *dev = obj->dev;
417 1.1 riastrad
418 1.1.1.2 riastrad return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
419 1.1.1.2 riastrad size / PAGE_SIZE);
420 1.1.1.2 riastrad }
421 1.1.1.2 riastrad EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
422 1.1 riastrad
423 1.1.1.2 riastrad /**
424 1.1.1.2 riastrad * drm_gem_create_mmap_offset - create a fake mmap offset for an object
425 1.1.1.2 riastrad * @obj: obj in question
426 1.1.1.2 riastrad *
427 1.1.1.2 riastrad * GEM memory mapping works by handing back to userspace a fake mmap offset
428 1.1.1.2 riastrad * it can use in a subsequent mmap(2) call. The DRM core code then looks
429 1.1.1.2 riastrad * up the object based on the offset and sets up the various memory mapping
430 1.1.1.2 riastrad * structures.
431 1.1.1.2 riastrad *
432 1.1.1.2 riastrad * This routine allocates and attaches a fake offset for @obj.
433 1.1.1.2 riastrad */
434 1.1.1.2 riastrad int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
435 1.1.1.2 riastrad {
436 1.1.1.2 riastrad return drm_gem_create_mmap_offset_size(obj, obj->size);
437 1.1.1.2 riastrad }
438 1.1.1.2 riastrad EXPORT_SYMBOL(drm_gem_create_mmap_offset);
439 1.1 riastrad
440 1.1.1.2 riastrad /**
441 1.1.1.2 riastrad * drm_gem_get_pages - helper to allocate backing pages for a GEM object
442 1.1.1.2 riastrad * from shmem
443 1.1.1.2 riastrad * @obj: obj in question
444 1.1.1.2 riastrad * @gfpmask: gfp mask of requested pages
445 1.1.1.2 riastrad */
446 1.1.1.2 riastrad struct page **drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask)
447 1.1.1.2 riastrad {
448 1.1.1.2 riastrad struct inode *inode;
449 1.1.1.2 riastrad struct address_space *mapping;
450 1.1.1.2 riastrad struct page *p, **pages;
451 1.1.1.2 riastrad int i, npages;
452 1.1.1.2 riastrad
453 1.1.1.2 riastrad /* This is the shared memory object that backs the GEM resource */
454 1.1.1.2 riastrad inode = file_inode(obj->filp);
455 1.1.1.2 riastrad mapping = inode->i_mapping;
456 1.1.1.2 riastrad
457 1.1.1.2 riastrad /* We already BUG_ON() for non-page-aligned sizes in
458 1.1.1.2 riastrad * drm_gem_object_init(), so we should never hit this unless
459 1.1.1.2 riastrad * driver author is doing something really wrong:
460 1.1.1.2 riastrad */
461 1.1.1.2 riastrad WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
462 1.1.1.2 riastrad
463 1.1.1.2 riastrad npages = obj->size >> PAGE_SHIFT;
464 1.1.1.2 riastrad
465 1.1.1.2 riastrad pages = drm_malloc_ab(npages, sizeof(struct page *));
466 1.1.1.2 riastrad if (pages == NULL)
467 1.1.1.2 riastrad return ERR_PTR(-ENOMEM);
468 1.1.1.2 riastrad
469 1.1.1.2 riastrad gfpmask |= mapping_gfp_mask(mapping);
470 1.1.1.2 riastrad
471 1.1.1.2 riastrad for (i = 0; i < npages; i++) {
472 1.1.1.2 riastrad p = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
473 1.1.1.2 riastrad if (IS_ERR(p))
474 1.1.1.2 riastrad goto fail;
475 1.1.1.2 riastrad pages[i] = p;
476 1.1.1.2 riastrad
477 1.1.1.2 riastrad /* There is a hypothetical issue w/ drivers that require
478 1.1.1.2 riastrad * buffer memory in the low 4GB.. if the pages are un-
479 1.1.1.2 riastrad * pinned, and swapped out, they can end up swapped back
480 1.1.1.2 riastrad * in above 4GB. If pages are already in memory, then
481 1.1.1.2 riastrad * shmem_read_mapping_page_gfp will ignore the gfpmask,
482 1.1.1.2 riastrad * even if the already in-memory page disobeys the mask.
483 1.1.1.2 riastrad *
484 1.1.1.2 riastrad * It is only a theoretical issue today, because none of
485 1.1.1.2 riastrad * the devices with this limitation can be populated with
486 1.1.1.2 riastrad * enough memory to trigger the issue. But this BUG_ON()
487 1.1.1.2 riastrad * is here as a reminder in case the problem with
488 1.1.1.2 riastrad * shmem_read_mapping_page_gfp() isn't solved by the time
489 1.1.1.2 riastrad * it does become a real issue.
490 1.1.1.2 riastrad *
491 1.1.1.2 riastrad * See this thread: http://lkml.org/lkml/2011/7/11/238
492 1.1.1.2 riastrad */
493 1.1.1.2 riastrad BUG_ON((gfpmask & __GFP_DMA32) &&
494 1.1.1.2 riastrad (page_to_pfn(p) >= 0x00100000UL));
495 1.1 riastrad }
496 1.1 riastrad
497 1.1.1.2 riastrad return pages;
498 1.1 riastrad
499 1.1.1.2 riastrad fail:
500 1.1.1.2 riastrad while (i--)
501 1.1.1.2 riastrad page_cache_release(pages[i]);
502 1.1 riastrad
503 1.1.1.2 riastrad drm_free_large(pages);
504 1.1.1.2 riastrad return ERR_CAST(p);
505 1.1 riastrad }
506 1.1.1.2 riastrad EXPORT_SYMBOL(drm_gem_get_pages);
507 1.1.1.2 riastrad
508 1.1.1.2 riastrad /**
509 1.1.1.2 riastrad * drm_gem_put_pages - helper to free backing pages for a GEM object
510 1.1.1.2 riastrad * @obj: obj in question
511 1.1.1.2 riastrad * @pages: pages to free
512 1.1.1.2 riastrad * @dirty: if true, pages will be marked as dirty
513 1.1.1.2 riastrad * @accessed: if true, the pages will be marked as accessed
514 1.1.1.2 riastrad */
515 1.1.1.2 riastrad void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
516 1.1.1.2 riastrad bool dirty, bool accessed)
517 1.1.1.2 riastrad {
518 1.1.1.2 riastrad int i, npages;
519 1.1.1.2 riastrad
520 1.1.1.2 riastrad /* We already BUG_ON() for non-page-aligned sizes in
521 1.1.1.2 riastrad * drm_gem_object_init(), so we should never hit this unless
522 1.1.1.2 riastrad * driver author is doing something really wrong:
523 1.1.1.2 riastrad */
524 1.1.1.2 riastrad WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
525 1.1.1.2 riastrad
526 1.1.1.2 riastrad npages = obj->size >> PAGE_SHIFT;
527 1.1.1.2 riastrad
528 1.1.1.2 riastrad for (i = 0; i < npages; i++) {
529 1.1.1.2 riastrad if (dirty)
530 1.1.1.2 riastrad set_page_dirty(pages[i]);
531 1.1.1.2 riastrad
532 1.1.1.2 riastrad if (accessed)
533 1.1.1.2 riastrad mark_page_accessed(pages[i]);
534 1.1.1.2 riastrad
535 1.1.1.2 riastrad /* Undo the reference we took when populating the table */
536 1.1.1.2 riastrad page_cache_release(pages[i]);
537 1.1.1.2 riastrad }
538 1.1.1.2 riastrad
539 1.1.1.2 riastrad drm_free_large(pages);
540 1.1.1.2 riastrad }
541 1.1.1.2 riastrad EXPORT_SYMBOL(drm_gem_put_pages);
542 1.1 riastrad
543 1.1 riastrad /** Returns a reference to the object named by the handle. */
544 1.1 riastrad struct drm_gem_object *
545 1.1 riastrad drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
546 1.1 riastrad u32 handle)
547 1.1 riastrad {
548 1.1 riastrad struct drm_gem_object *obj;
549 1.1 riastrad
550 1.1 riastrad spin_lock(&filp->table_lock);
551 1.1 riastrad
552 1.1 riastrad /* Check if we currently have a reference on the object */
553 1.1 riastrad obj = idr_find(&filp->object_idr, handle);
554 1.1 riastrad if (obj == NULL) {
555 1.1 riastrad spin_unlock(&filp->table_lock);
556 1.1 riastrad return NULL;
557 1.1 riastrad }
558 1.1 riastrad
559 1.1 riastrad drm_gem_object_reference(obj);
560 1.1 riastrad
561 1.1 riastrad spin_unlock(&filp->table_lock);
562 1.1 riastrad
563 1.1 riastrad return obj;
564 1.1 riastrad }
565 1.1 riastrad EXPORT_SYMBOL(drm_gem_object_lookup);
566 1.1 riastrad
567 1.1 riastrad /**
568 1.1.1.2 riastrad * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
569 1.1.1.2 riastrad * @dev: drm_device
570 1.1.1.2 riastrad * @data: ioctl data
571 1.1.1.2 riastrad * @file_priv: drm file-private structure
572 1.1.1.2 riastrad *
573 1.1 riastrad * Releases the handle to an mm object.
574 1.1 riastrad */
575 1.1 riastrad int
576 1.1 riastrad drm_gem_close_ioctl(struct drm_device *dev, void *data,
577 1.1 riastrad struct drm_file *file_priv)
578 1.1 riastrad {
579 1.1 riastrad struct drm_gem_close *args = data;
580 1.1 riastrad int ret;
581 1.1 riastrad
582 1.1 riastrad if (!(dev->driver->driver_features & DRIVER_GEM))
583 1.1 riastrad return -ENODEV;
584 1.1 riastrad
585 1.1 riastrad ret = drm_gem_handle_delete(file_priv, args->handle);
586 1.1 riastrad
587 1.1 riastrad return ret;
588 1.1 riastrad }
589 1.1 riastrad
590 1.1 riastrad /**
591 1.1.1.2 riastrad * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
592 1.1.1.2 riastrad * @dev: drm_device
593 1.1.1.2 riastrad * @data: ioctl data
594 1.1.1.2 riastrad * @file_priv: drm file-private structure
595 1.1.1.2 riastrad *
596 1.1 riastrad * Create a global name for an object, returning the name.
597 1.1 riastrad *
598 1.1 riastrad * Note that the name does not hold a reference; when the object
599 1.1 riastrad * is freed, the name goes away.
600 1.1 riastrad */
601 1.1 riastrad int
602 1.1 riastrad drm_gem_flink_ioctl(struct drm_device *dev, void *data,
603 1.1 riastrad struct drm_file *file_priv)
604 1.1 riastrad {
605 1.1 riastrad struct drm_gem_flink *args = data;
606 1.1 riastrad struct drm_gem_object *obj;
607 1.1 riastrad int ret;
608 1.1 riastrad
609 1.1 riastrad if (!(dev->driver->driver_features & DRIVER_GEM))
610 1.1 riastrad return -ENODEV;
611 1.1 riastrad
612 1.1 riastrad obj = drm_gem_object_lookup(dev, file_priv, args->handle);
613 1.1 riastrad if (obj == NULL)
614 1.1 riastrad return -ENOENT;
615 1.1 riastrad
616 1.1.1.2 riastrad mutex_lock(&dev->object_name_lock);
617 1.1.1.2 riastrad idr_preload(GFP_KERNEL);
618 1.1.1.2 riastrad /* prevent races with concurrent gem_close. */
619 1.1.1.2 riastrad if (obj->handle_count == 0) {
620 1.1.1.2 riastrad ret = -ENOENT;
621 1.1 riastrad goto err;
622 1.1 riastrad }
623 1.1 riastrad
624 1.1 riastrad if (!obj->name) {
625 1.1.1.2 riastrad ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
626 1.1.1.2 riastrad if (ret < 0)
627 1.1 riastrad goto err;
628 1.1 riastrad
629 1.1.1.2 riastrad obj->name = ret;
630 1.1 riastrad }
631 1.1 riastrad
632 1.1.1.2 riastrad args->name = (uint64_t) obj->name;
633 1.1.1.2 riastrad ret = 0;
634 1.1.1.2 riastrad
635 1.1 riastrad err:
636 1.1.1.2 riastrad idr_preload_end();
637 1.1.1.2 riastrad mutex_unlock(&dev->object_name_lock);
638 1.1 riastrad drm_gem_object_unreference_unlocked(obj);
639 1.1 riastrad return ret;
640 1.1 riastrad }
641 1.1 riastrad
642 1.1 riastrad /**
643 1.1.1.2 riastrad * drm_gem_open - implementation of the GEM_OPEN ioctl
644 1.1.1.2 riastrad * @dev: drm_device
645 1.1.1.2 riastrad * @data: ioctl data
646 1.1.1.2 riastrad * @file_priv: drm file-private structure
647 1.1.1.2 riastrad *
648 1.1 riastrad * Open an object using the global name, returning a handle and the size.
649 1.1 riastrad *
650 1.1 riastrad * This handle (of course) holds a reference to the object, so the object
651 1.1 riastrad * will not go away until the handle is deleted.
652 1.1 riastrad */
653 1.1 riastrad int
654 1.1 riastrad drm_gem_open_ioctl(struct drm_device *dev, void *data,
655 1.1 riastrad struct drm_file *file_priv)
656 1.1 riastrad {
657 1.1 riastrad struct drm_gem_open *args = data;
658 1.1 riastrad struct drm_gem_object *obj;
659 1.1 riastrad int ret;
660 1.1 riastrad u32 handle;
661 1.1 riastrad
662 1.1 riastrad if (!(dev->driver->driver_features & DRIVER_GEM))
663 1.1 riastrad return -ENODEV;
664 1.1 riastrad
665 1.1.1.2 riastrad mutex_lock(&dev->object_name_lock);
666 1.1 riastrad obj = idr_find(&dev->object_name_idr, (int) args->name);
667 1.1.1.2 riastrad if (obj) {
668 1.1 riastrad drm_gem_object_reference(obj);
669 1.1.1.2 riastrad } else {
670 1.1.1.2 riastrad mutex_unlock(&dev->object_name_lock);
671 1.1 riastrad return -ENOENT;
672 1.1.1.2 riastrad }
673 1.1 riastrad
674 1.1.1.2 riastrad /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
675 1.1.1.2 riastrad ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
676 1.1 riastrad drm_gem_object_unreference_unlocked(obj);
677 1.1 riastrad if (ret)
678 1.1 riastrad return ret;
679 1.1 riastrad
680 1.1 riastrad args->handle = handle;
681 1.1 riastrad args->size = obj->size;
682 1.1 riastrad
683 1.1 riastrad return 0;
684 1.1 riastrad }
685 1.1 riastrad
686 1.1 riastrad /**
687 1.1.1.2 riastrad * gem_gem_open - initalizes GEM file-private structures at devnode open time
688 1.1.1.2 riastrad * @dev: drm_device which is being opened by userspace
689 1.1.1.2 riastrad * @file_private: drm file-private structure to set up
690 1.1.1.2 riastrad *
691 1.1 riastrad * Called at device open time, sets up the structure for handling refcounting
692 1.1 riastrad * of mm objects.
693 1.1 riastrad */
694 1.1 riastrad void
695 1.1 riastrad drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
696 1.1 riastrad {
697 1.1 riastrad idr_init(&file_private->object_idr);
698 1.1 riastrad spin_lock_init(&file_private->table_lock);
699 1.1 riastrad }
700 1.1 riastrad
701 1.1.1.2 riastrad /*
702 1.1 riastrad * Called at device close to release the file's
703 1.1 riastrad * handle references on objects.
704 1.1 riastrad */
705 1.1 riastrad static int
706 1.1 riastrad drm_gem_object_release_handle(int id, void *ptr, void *data)
707 1.1 riastrad {
708 1.1 riastrad struct drm_file *file_priv = data;
709 1.1 riastrad struct drm_gem_object *obj = ptr;
710 1.1 riastrad struct drm_device *dev = obj->dev;
711 1.1 riastrad
712 1.1.1.2 riastrad if (drm_core_check_feature(dev, DRIVER_PRIME))
713 1.1.1.2 riastrad drm_gem_remove_prime_handles(obj, file_priv);
714 1.1.1.2 riastrad drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
715 1.1 riastrad
716 1.1 riastrad if (dev->driver->gem_close_object)
717 1.1 riastrad dev->driver->gem_close_object(obj, file_priv);
718 1.1 riastrad
719 1.1 riastrad drm_gem_object_handle_unreference_unlocked(obj);
720 1.1 riastrad
721 1.1 riastrad return 0;
722 1.1 riastrad }
723 1.1 riastrad
724 1.1 riastrad /**
725 1.1.1.2 riastrad * drm_gem_release - release file-private GEM resources
726 1.1.1.2 riastrad * @dev: drm_device which is being closed by userspace
727 1.1.1.2 riastrad * @file_private: drm file-private structure to clean up
728 1.1.1.2 riastrad *
729 1.1 riastrad * Called at close time when the filp is going away.
730 1.1 riastrad *
731 1.1 riastrad * Releases any remaining references on objects by this filp.
732 1.1 riastrad */
733 1.1 riastrad void
734 1.1 riastrad drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
735 1.1 riastrad {
736 1.1 riastrad idr_for_each(&file_private->object_idr,
737 1.1 riastrad &drm_gem_object_release_handle, file_private);
738 1.1 riastrad idr_destroy(&file_private->object_idr);
739 1.1 riastrad }
740 1.1 riastrad
741 1.1 riastrad void
742 1.1 riastrad drm_gem_object_release(struct drm_gem_object *obj)
743 1.1 riastrad {
744 1.1.1.2 riastrad WARN_ON(obj->dma_buf);
745 1.1.1.2 riastrad
746 1.1 riastrad if (obj->filp)
747 1.1.1.2 riastrad fput(obj->filp);
748 1.1.1.2 riastrad
749 1.1.1.2 riastrad drm_gem_free_mmap_offset(obj);
750 1.1 riastrad }
751 1.1 riastrad EXPORT_SYMBOL(drm_gem_object_release);
752 1.1 riastrad
753 1.1 riastrad /**
754 1.1.1.2 riastrad * drm_gem_object_free - free a GEM object
755 1.1.1.2 riastrad * @kref: kref of the object to free
756 1.1.1.2 riastrad *
757 1.1 riastrad * Called after the last reference to the object has been lost.
758 1.1 riastrad * Must be called holding struct_ mutex
759 1.1 riastrad *
760 1.1 riastrad * Frees the object
761 1.1 riastrad */
762 1.1 riastrad void
763 1.1 riastrad drm_gem_object_free(struct kref *kref)
764 1.1 riastrad {
765 1.1 riastrad struct drm_gem_object *obj = (struct drm_gem_object *) kref;
766 1.1 riastrad struct drm_device *dev = obj->dev;
767 1.1 riastrad
768 1.1 riastrad BUG_ON(!mutex_is_locked(&dev->struct_mutex));
769 1.1 riastrad
770 1.1 riastrad if (dev->driver->gem_free_object != NULL)
771 1.1 riastrad dev->driver->gem_free_object(obj);
772 1.1 riastrad }
773 1.1 riastrad EXPORT_SYMBOL(drm_gem_object_free);
774 1.1 riastrad
775 1.1 riastrad void drm_gem_vm_open(struct vm_area_struct *vma)
776 1.1 riastrad {
777 1.1 riastrad struct drm_gem_object *obj = vma->vm_private_data;
778 1.1 riastrad
779 1.1 riastrad drm_gem_object_reference(obj);
780 1.1 riastrad
781 1.1 riastrad mutex_lock(&obj->dev->struct_mutex);
782 1.1 riastrad drm_vm_open_locked(obj->dev, vma);
783 1.1 riastrad mutex_unlock(&obj->dev->struct_mutex);
784 1.1 riastrad }
785 1.1 riastrad EXPORT_SYMBOL(drm_gem_vm_open);
786 1.1 riastrad
787 1.1 riastrad void drm_gem_vm_close(struct vm_area_struct *vma)
788 1.1 riastrad {
789 1.1 riastrad struct drm_gem_object *obj = vma->vm_private_data;
790 1.1 riastrad struct drm_device *dev = obj->dev;
791 1.1 riastrad
792 1.1 riastrad mutex_lock(&dev->struct_mutex);
793 1.1 riastrad drm_vm_close_locked(obj->dev, vma);
794 1.1 riastrad drm_gem_object_unreference(obj);
795 1.1 riastrad mutex_unlock(&dev->struct_mutex);
796 1.1 riastrad }
797 1.1 riastrad EXPORT_SYMBOL(drm_gem_vm_close);
798 1.1 riastrad
799 1.1.1.2 riastrad /**
800 1.1.1.2 riastrad * drm_gem_mmap_obj - memory map a GEM object
801 1.1.1.2 riastrad * @obj: the GEM object to map
802 1.1.1.2 riastrad * @obj_size: the object size to be mapped, in bytes
803 1.1.1.2 riastrad * @vma: VMA for the area to be mapped
804 1.1.1.2 riastrad *
805 1.1.1.2 riastrad * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
806 1.1.1.2 riastrad * provided by the driver. Depending on their requirements, drivers can either
807 1.1.1.2 riastrad * provide a fault handler in their gem_vm_ops (in which case any accesses to
808 1.1.1.2 riastrad * the object will be trapped, to perform migration, GTT binding, surface
809 1.1.1.2 riastrad * register allocation, or performance monitoring), or mmap the buffer memory
810 1.1.1.2 riastrad * synchronously after calling drm_gem_mmap_obj.
811 1.1.1.2 riastrad *
812 1.1.1.2 riastrad * This function is mainly intended to implement the DMABUF mmap operation, when
813 1.1.1.2 riastrad * the GEM object is not looked up based on its fake offset. To implement the
814 1.1.1.2 riastrad * DRM mmap operation, drivers should use the drm_gem_mmap() function.
815 1.1.1.2 riastrad *
816 1.1.1.2 riastrad * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
817 1.1.1.2 riastrad * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
818 1.1.1.2 riastrad * callers must verify access restrictions before calling this helper.
819 1.1.1.2 riastrad *
820 1.1.1.2 riastrad * NOTE: This function has to be protected with dev->struct_mutex
821 1.1.1.2 riastrad *
822 1.1.1.2 riastrad * Return 0 or success or -EINVAL if the object size is smaller than the VMA
823 1.1.1.2 riastrad * size, or if no gem_vm_ops are provided.
824 1.1.1.2 riastrad */
825 1.1.1.2 riastrad int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
826 1.1.1.2 riastrad struct vm_area_struct *vma)
827 1.1.1.2 riastrad {
828 1.1.1.2 riastrad struct drm_device *dev = obj->dev;
829 1.1.1.2 riastrad
830 1.1.1.2 riastrad lockdep_assert_held(&dev->struct_mutex);
831 1.1.1.2 riastrad
832 1.1.1.2 riastrad /* Check for valid size. */
833 1.1.1.2 riastrad if (obj_size < vma->vm_end - vma->vm_start)
834 1.1.1.2 riastrad return -EINVAL;
835 1.1.1.2 riastrad
836 1.1.1.2 riastrad if (!dev->driver->gem_vm_ops)
837 1.1.1.2 riastrad return -EINVAL;
838 1.1.1.2 riastrad
839 1.1.1.2 riastrad vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
840 1.1.1.2 riastrad vma->vm_ops = dev->driver->gem_vm_ops;
841 1.1.1.2 riastrad vma->vm_private_data = obj;
842 1.1.1.2 riastrad vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
843 1.1.1.2 riastrad
844 1.1.1.2 riastrad /* Take a ref for this mapping of the object, so that the fault
845 1.1.1.2 riastrad * handler can dereference the mmap offset's pointer to the object.
846 1.1.1.2 riastrad * This reference is cleaned up by the corresponding vm_close
847 1.1.1.2 riastrad * (which should happen whether the vma was created by this call, or
848 1.1.1.2 riastrad * by a vm_open due to mremap or partial unmap or whatever).
849 1.1.1.2 riastrad */
850 1.1.1.2 riastrad drm_gem_object_reference(obj);
851 1.1.1.2 riastrad
852 1.1.1.2 riastrad drm_vm_open_locked(dev, vma);
853 1.1.1.2 riastrad return 0;
854 1.1.1.2 riastrad }
855 1.1.1.2 riastrad EXPORT_SYMBOL(drm_gem_mmap_obj);
856 1.1 riastrad
857 1.1 riastrad /**
858 1.1 riastrad * drm_gem_mmap - memory map routine for GEM objects
859 1.1 riastrad * @filp: DRM file pointer
860 1.1 riastrad * @vma: VMA for the area to be mapped
861 1.1 riastrad *
862 1.1 riastrad * If a driver supports GEM object mapping, mmap calls on the DRM file
863 1.1 riastrad * descriptor will end up here.
864 1.1 riastrad *
865 1.1.1.2 riastrad * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
866 1.1 riastrad * contain the fake offset we created when the GTT map ioctl was called on
867 1.1.1.2 riastrad * the object) and map it with a call to drm_gem_mmap_obj().
868 1.1.1.2 riastrad *
869 1.1.1.2 riastrad * If the caller is not granted access to the buffer object, the mmap will fail
870 1.1.1.2 riastrad * with EACCES. Please see the vma manager for more information.
871 1.1 riastrad */
872 1.1 riastrad int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
873 1.1 riastrad {
874 1.1 riastrad struct drm_file *priv = filp->private_data;
875 1.1 riastrad struct drm_device *dev = priv->minor->dev;
876 1.1 riastrad struct drm_gem_object *obj;
877 1.1.1.2 riastrad struct drm_vma_offset_node *node;
878 1.1.1.2 riastrad int ret;
879 1.1 riastrad
880 1.1 riastrad if (drm_device_is_unplugged(dev))
881 1.1 riastrad return -ENODEV;
882 1.1 riastrad
883 1.1 riastrad mutex_lock(&dev->struct_mutex);
884 1.1 riastrad
885 1.1.1.2 riastrad node = drm_vma_offset_exact_lookup(dev->vma_offset_manager,
886 1.1.1.2 riastrad vma->vm_pgoff,
887 1.1.1.2 riastrad vma_pages(vma));
888 1.1.1.2 riastrad if (!node) {
889 1.1 riastrad mutex_unlock(&dev->struct_mutex);
890 1.1 riastrad return drm_mmap(filp, vma);
891 1.1.1.2 riastrad } else if (!drm_vma_node_is_allowed(node, filp)) {
892 1.1.1.2 riastrad mutex_unlock(&dev->struct_mutex);
893 1.1.1.2 riastrad return -EACCES;
894 1.1 riastrad }
895 1.1 riastrad
896 1.1.1.2 riastrad obj = container_of(node, struct drm_gem_object, vma_node);
897 1.1.1.2 riastrad ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma);
898 1.1 riastrad
899 1.1 riastrad mutex_unlock(&dev->struct_mutex);
900 1.1 riastrad
901 1.1 riastrad return ret;
902 1.1 riastrad }
903 1.1 riastrad EXPORT_SYMBOL(drm_gem_mmap);
904