]>
Commit | Line | Data |
---|---|---|
673a394b EA |
1 | /* |
2 | * Copyright © 2008 Intel Corporation | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice (including the next | |
12 | * paragraph) shall be included in all copies or substantial portions of the | |
13 | * Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | * | |
23 | * Authors: | |
24 | * Eric Anholt <eric@anholt.net> | |
25 | * | |
26 | */ | |
27 | ||
28 | #include <linux/types.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/mm.h> | |
31 | #include <linux/uaccess.h> | |
32 | #include <linux/fs.h> | |
33 | #include <linux/file.h> | |
34 | #include <linux/module.h> | |
35 | #include <linux/mman.h> | |
36 | #include <linux/pagemap.h> | |
37 | #include "drmP.h" | |
38 | ||
39 | /** @file drm_gem.c | |
40 | * | |
41 | * This file provides some of the base ioctls and library routines for | |
42 | * the graphics memory manager implemented by each device driver. | |
43 | * | |
44 | * Because various devices have different requirements in terms of | |
45 | * synchronization and migration strategies, implementing that is left up to | |
46 | * the driver, and all that the general API provides should be generic -- | |
47 | * allocating objects, reading/writing data with the cpu, freeing objects. | |
48 | * Even there, platform-dependent optimizations for reading/writing data with | |
49 | * the CPU mean we'll likely hook those out to driver-specific calls. However, | |
50 | * the DRI2 implementation wants to have at least allocate/mmap be generic. | |
51 | * | |
52 | * The goal was to have swap-backed object allocation managed through | |
53 | * struct file. However, file descriptors as handles to a struct file have | |
54 | * two major failings: | |
55 | * - Process limits prevent more than 1024 or so being used at a time by | |
56 | * default. | |
57 | * - Inability to allocate high fds will aggravate the X Server's select() | |
58 | * handling, and likely that of many GL client applications as well. | |
59 | * | |
60 | * This led to a plan of using our own integer IDs (called handles, following | |
61 | * DRM terminology) to mimic fds, and implement the fd syscalls we need as | |
62 | * ioctls. The objects themselves will still include the struct file so | |
63 | * that we can transition to fds if the required kernel infrastructure shows | |
64 | * up at a later date, and as our interface with shmfs for memory allocation. | |
65 | */ | |
66 | ||
a2c0a97b JB |
67 | /* |
68 | * We make up offsets for buffer objects so we can recognize them at | |
69 | * mmap time. | |
70 | */ | |
71 | #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) | |
72 | #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) | |
73 | ||
673a394b EA |
74 | /** |
75 | * Initialize the GEM device fields | |
76 | */ | |
77 | ||
78 | int | |
79 | drm_gem_init(struct drm_device *dev) | |
80 | { | |
a2c0a97b JB |
81 | struct drm_gem_mm *mm; |
82 | ||
673a394b EA |
83 | spin_lock_init(&dev->object_name_lock); |
84 | idr_init(&dev->object_name_idr); | |
85 | atomic_set(&dev->object_count, 0); | |
86 | atomic_set(&dev->object_memory, 0); | |
87 | atomic_set(&dev->pin_count, 0); | |
88 | atomic_set(&dev->pin_memory, 0); | |
89 | atomic_set(&dev->gtt_count, 0); | |
90 | atomic_set(&dev->gtt_memory, 0); | |
a2c0a97b JB |
91 | |
92 | mm = drm_calloc(1, sizeof(struct drm_gem_mm), DRM_MEM_MM); | |
93 | if (!mm) { | |
94 | DRM_ERROR("out of memory\n"); | |
95 | return -ENOMEM; | |
96 | } | |
97 | ||
98 | dev->mm_private = mm; | |
99 | ||
100 | if (drm_ht_create(&mm->offset_hash, 19)) { | |
101 | drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM); | |
102 | return -ENOMEM; | |
103 | } | |
104 | ||
105 | if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START, | |
106 | DRM_FILE_PAGE_OFFSET_SIZE)) { | |
a2c0a97b | 107 | drm_ht_remove(&mm->offset_hash); |
ad45aa9e | 108 | drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM); |
a2c0a97b JB |
109 | return -ENOMEM; |
110 | } | |
111 | ||
673a394b EA |
112 | return 0; |
113 | } | |
114 | ||
a2c0a97b JB |
115 | void |
116 | drm_gem_destroy(struct drm_device *dev) | |
117 | { | |
118 | struct drm_gem_mm *mm = dev->mm_private; | |
119 | ||
120 | drm_mm_takedown(&mm->offset_manager); | |
121 | drm_ht_remove(&mm->offset_hash); | |
122 | drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM); | |
123 | dev->mm_private = NULL; | |
124 | } | |
125 | ||
673a394b EA |
126 | /** |
127 | * Allocate a GEM object of the specified size with shmfs backing store | |
128 | */ | |
129 | struct drm_gem_object * | |
130 | drm_gem_object_alloc(struct drm_device *dev, size_t size) | |
131 | { | |
132 | struct drm_gem_object *obj; | |
133 | ||
134 | BUG_ON((size & (PAGE_SIZE - 1)) != 0); | |
135 | ||
136 | obj = kcalloc(1, sizeof(*obj), GFP_KERNEL); | |
137 | ||
138 | obj->dev = dev; | |
fc8744ad | 139 | obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); |
673a394b EA |
140 | if (IS_ERR(obj->filp)) { |
141 | kfree(obj); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | kref_init(&obj->refcount); | |
146 | kref_init(&obj->handlecount); | |
147 | obj->size = size; | |
148 | if (dev->driver->gem_init_object != NULL && | |
149 | dev->driver->gem_init_object(obj) != 0) { | |
150 | fput(obj->filp); | |
151 | kfree(obj); | |
152 | return NULL; | |
153 | } | |
154 | atomic_inc(&dev->object_count); | |
155 | atomic_add(obj->size, &dev->object_memory); | |
156 | return obj; | |
157 | } | |
158 | EXPORT_SYMBOL(drm_gem_object_alloc); | |
159 | ||
160 | /** | |
161 | * Removes the mapping from handle to filp for this object. | |
162 | */ | |
163 | static int | |
164 | drm_gem_handle_delete(struct drm_file *filp, int handle) | |
165 | { | |
166 | struct drm_device *dev; | |
167 | struct drm_gem_object *obj; | |
168 | ||
169 | /* This is gross. The idr system doesn't let us try a delete and | |
170 | * return an error code. It just spews if you fail at deleting. | |
171 | * So, we have to grab a lock around finding the object and then | |
172 | * doing the delete on it and dropping the refcount, or the user | |
173 | * could race us to double-decrement the refcount and cause a | |
174 | * use-after-free later. Given the frequency of our handle lookups, | |
175 | * we may want to use ida for number allocation and a hash table | |
176 | * for the pointers, anyway. | |
177 | */ | |
178 | spin_lock(&filp->table_lock); | |
179 | ||
180 | /* Check if we currently have a reference on the object */ | |
181 | obj = idr_find(&filp->object_idr, handle); | |
182 | if (obj == NULL) { | |
183 | spin_unlock(&filp->table_lock); | |
184 | return -EINVAL; | |
185 | } | |
186 | dev = obj->dev; | |
187 | ||
188 | /* Release reference and decrement refcount. */ | |
189 | idr_remove(&filp->object_idr, handle); | |
190 | spin_unlock(&filp->table_lock); | |
191 | ||
192 | mutex_lock(&dev->struct_mutex); | |
193 | drm_gem_object_handle_unreference(obj); | |
194 | mutex_unlock(&dev->struct_mutex); | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
199 | /** | |
200 | * Create a handle for this object. This adds a handle reference | |
201 | * to the object, which includes a regular reference count. Callers | |
202 | * will likely want to dereference the object afterwards. | |
203 | */ | |
204 | int | |
205 | drm_gem_handle_create(struct drm_file *file_priv, | |
206 | struct drm_gem_object *obj, | |
207 | int *handlep) | |
208 | { | |
209 | int ret; | |
210 | ||
211 | /* | |
212 | * Get the user-visible handle using idr. | |
213 | */ | |
214 | again: | |
215 | /* ensure there is space available to allocate a handle */ | |
216 | if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) | |
217 | return -ENOMEM; | |
218 | ||
219 | /* do the allocation under our spinlock */ | |
220 | spin_lock(&file_priv->table_lock); | |
221 | ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep); | |
222 | spin_unlock(&file_priv->table_lock); | |
223 | if (ret == -EAGAIN) | |
224 | goto again; | |
225 | ||
226 | if (ret != 0) | |
227 | return ret; | |
228 | ||
229 | drm_gem_object_handle_reference(obj); | |
230 | return 0; | |
231 | } | |
232 | EXPORT_SYMBOL(drm_gem_handle_create); | |
233 | ||
234 | /** Returns a reference to the object named by the handle. */ | |
235 | struct drm_gem_object * | |
236 | drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, | |
237 | int handle) | |
238 | { | |
239 | struct drm_gem_object *obj; | |
240 | ||
241 | spin_lock(&filp->table_lock); | |
242 | ||
243 | /* Check if we currently have a reference on the object */ | |
244 | obj = idr_find(&filp->object_idr, handle); | |
245 | if (obj == NULL) { | |
246 | spin_unlock(&filp->table_lock); | |
247 | return NULL; | |
248 | } | |
249 | ||
250 | drm_gem_object_reference(obj); | |
251 | ||
252 | spin_unlock(&filp->table_lock); | |
253 | ||
254 | return obj; | |
255 | } | |
256 | EXPORT_SYMBOL(drm_gem_object_lookup); | |
257 | ||
258 | /** | |
259 | * Releases the handle to an mm object. | |
260 | */ | |
261 | int | |
262 | drm_gem_close_ioctl(struct drm_device *dev, void *data, | |
263 | struct drm_file *file_priv) | |
264 | { | |
265 | struct drm_gem_close *args = data; | |
266 | int ret; | |
267 | ||
268 | if (!(dev->driver->driver_features & DRIVER_GEM)) | |
269 | return -ENODEV; | |
270 | ||
271 | ret = drm_gem_handle_delete(file_priv, args->handle); | |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
276 | /** | |
277 | * Create a global name for an object, returning the name. | |
278 | * | |
279 | * Note that the name does not hold a reference; when the object | |
280 | * is freed, the name goes away. | |
281 | */ | |
282 | int | |
283 | drm_gem_flink_ioctl(struct drm_device *dev, void *data, | |
284 | struct drm_file *file_priv) | |
285 | { | |
286 | struct drm_gem_flink *args = data; | |
287 | struct drm_gem_object *obj; | |
288 | int ret; | |
289 | ||
290 | if (!(dev->driver->driver_features & DRIVER_GEM)) | |
291 | return -ENODEV; | |
292 | ||
293 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); | |
294 | if (obj == NULL) | |
d4e7b898 | 295 | return -EBADF; |
673a394b EA |
296 | |
297 | again: | |
3e49c4f4 CW |
298 | if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) { |
299 | ret = -ENOMEM; | |
300 | goto err; | |
301 | } | |
673a394b EA |
302 | |
303 | spin_lock(&dev->object_name_lock); | |
304 | if (obj->name) { | |
d4e7b898 | 305 | args->name = obj->name; |
673a394b | 306 | spin_unlock(&dev->object_name_lock); |
d4e7b898 | 307 | return 0; |
673a394b EA |
308 | } |
309 | ret = idr_get_new_above(&dev->object_name_idr, obj, 1, | |
310 | &obj->name); | |
311 | spin_unlock(&dev->object_name_lock); | |
312 | if (ret == -EAGAIN) | |
313 | goto again; | |
314 | ||
3e49c4f4 CW |
315 | if (ret != 0) |
316 | goto err; | |
673a394b EA |
317 | |
318 | /* | |
319 | * Leave the reference from the lookup around as the | |
320 | * name table now holds one | |
321 | */ | |
322 | args->name = (uint64_t) obj->name; | |
323 | ||
324 | return 0; | |
3e49c4f4 CW |
325 | |
326 | err: | |
327 | mutex_lock(&dev->struct_mutex); | |
328 | drm_gem_object_unreference(obj); | |
329 | mutex_unlock(&dev->struct_mutex); | |
330 | return ret; | |
673a394b EA |
331 | } |
332 | ||
333 | /** | |
334 | * Open an object using the global name, returning a handle and the size. | |
335 | * | |
336 | * This handle (of course) holds a reference to the object, so the object | |
337 | * will not go away until the handle is deleted. | |
338 | */ | |
339 | int | |
340 | drm_gem_open_ioctl(struct drm_device *dev, void *data, | |
341 | struct drm_file *file_priv) | |
342 | { | |
343 | struct drm_gem_open *args = data; | |
344 | struct drm_gem_object *obj; | |
345 | int ret; | |
346 | int handle; | |
347 | ||
348 | if (!(dev->driver->driver_features & DRIVER_GEM)) | |
349 | return -ENODEV; | |
350 | ||
351 | spin_lock(&dev->object_name_lock); | |
352 | obj = idr_find(&dev->object_name_idr, (int) args->name); | |
353 | if (obj) | |
354 | drm_gem_object_reference(obj); | |
355 | spin_unlock(&dev->object_name_lock); | |
356 | if (!obj) | |
357 | return -ENOENT; | |
358 | ||
359 | ret = drm_gem_handle_create(file_priv, obj, &handle); | |
360 | mutex_lock(&dev->struct_mutex); | |
361 | drm_gem_object_unreference(obj); | |
362 | mutex_unlock(&dev->struct_mutex); | |
363 | if (ret) | |
364 | return ret; | |
365 | ||
366 | args->handle = handle; | |
367 | args->size = obj->size; | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
372 | /** | |
373 | * Called at device open time, sets up the structure for handling refcounting | |
374 | * of mm objects. | |
375 | */ | |
376 | void | |
377 | drm_gem_open(struct drm_device *dev, struct drm_file *file_private) | |
378 | { | |
379 | idr_init(&file_private->object_idr); | |
380 | spin_lock_init(&file_private->table_lock); | |
381 | } | |
382 | ||
383 | /** | |
384 | * Called at device close to release the file's | |
385 | * handle references on objects. | |
386 | */ | |
387 | static int | |
388 | drm_gem_object_release_handle(int id, void *ptr, void *data) | |
389 | { | |
390 | struct drm_gem_object *obj = ptr; | |
391 | ||
392 | drm_gem_object_handle_unreference(obj); | |
393 | ||
394 | return 0; | |
395 | } | |
396 | ||
397 | /** | |
398 | * Called at close time when the filp is going away. | |
399 | * | |
400 | * Releases any remaining references on objects by this filp. | |
401 | */ | |
402 | void | |
403 | drm_gem_release(struct drm_device *dev, struct drm_file *file_private) | |
404 | { | |
405 | mutex_lock(&dev->struct_mutex); | |
406 | idr_for_each(&file_private->object_idr, | |
407 | &drm_gem_object_release_handle, NULL); | |
408 | ||
409 | idr_destroy(&file_private->object_idr); | |
410 | mutex_unlock(&dev->struct_mutex); | |
411 | } | |
412 | ||
413 | /** | |
414 | * Called after the last reference to the object has been lost. | |
415 | * | |
416 | * Frees the object | |
417 | */ | |
418 | void | |
419 | drm_gem_object_free(struct kref *kref) | |
420 | { | |
421 | struct drm_gem_object *obj = (struct drm_gem_object *) kref; | |
422 | struct drm_device *dev = obj->dev; | |
423 | ||
424 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); | |
425 | ||
426 | if (dev->driver->gem_free_object != NULL) | |
427 | dev->driver->gem_free_object(obj); | |
428 | ||
429 | fput(obj->filp); | |
430 | atomic_dec(&dev->object_count); | |
431 | atomic_sub(obj->size, &dev->object_memory); | |
432 | kfree(obj); | |
433 | } | |
434 | EXPORT_SYMBOL(drm_gem_object_free); | |
435 | ||
436 | /** | |
437 | * Called after the last handle to the object has been closed | |
438 | * | |
439 | * Removes any name for the object. Note that this must be | |
440 | * called before drm_gem_object_free or we'll be touching | |
441 | * freed memory | |
442 | */ | |
443 | void | |
444 | drm_gem_object_handle_free(struct kref *kref) | |
445 | { | |
446 | struct drm_gem_object *obj = container_of(kref, | |
447 | struct drm_gem_object, | |
448 | handlecount); | |
449 | struct drm_device *dev = obj->dev; | |
450 | ||
451 | /* Remove any name for this object */ | |
452 | spin_lock(&dev->object_name_lock); | |
453 | if (obj->name) { | |
454 | idr_remove(&dev->object_name_idr, obj->name); | |
455 | spin_unlock(&dev->object_name_lock); | |
456 | /* | |
457 | * The object name held a reference to this object, drop | |
458 | * that now. | |
459 | */ | |
460 | drm_gem_object_unreference(obj); | |
461 | } else | |
462 | spin_unlock(&dev->object_name_lock); | |
463 | ||
464 | } | |
465 | EXPORT_SYMBOL(drm_gem_object_handle_free); | |
466 | ||
a2c0a97b JB |
467 | /** |
468 | * drm_gem_mmap - memory map routine for GEM objects | |
469 | * @filp: DRM file pointer | |
470 | * @vma: VMA for the area to be mapped | |
471 | * | |
472 | * If a driver supports GEM object mapping, mmap calls on the DRM file | |
473 | * descriptor will end up here. | |
474 | * | |
475 | * If we find the object based on the offset passed in (vma->vm_pgoff will | |
476 | * contain the fake offset we created when the GTT map ioctl was called on | |
477 | * the object), we set up the driver fault handler so that any accesses | |
478 | * to the object can be trapped, to perform migration, GTT binding, surface | |
479 | * register allocation, or performance monitoring. | |
480 | */ | |
481 | int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) | |
482 | { | |
483 | struct drm_file *priv = filp->private_data; | |
484 | struct drm_device *dev = priv->minor->dev; | |
485 | struct drm_gem_mm *mm = dev->mm_private; | |
486 | struct drm_map *map = NULL; | |
487 | struct drm_gem_object *obj; | |
488 | struct drm_hash_item *hash; | |
489 | unsigned long prot; | |
490 | int ret = 0; | |
491 | ||
492 | mutex_lock(&dev->struct_mutex); | |
493 | ||
494 | if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) { | |
495 | mutex_unlock(&dev->struct_mutex); | |
496 | return drm_mmap(filp, vma); | |
497 | } | |
498 | ||
499 | map = drm_hash_entry(hash, struct drm_map_list, hash)->map; | |
500 | if (!map || | |
501 | ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) { | |
502 | ret = -EPERM; | |
503 | goto out_unlock; | |
504 | } | |
505 | ||
506 | /* Check for valid size. */ | |
507 | if (map->size < vma->vm_end - vma->vm_start) { | |
508 | ret = -EINVAL; | |
509 | goto out_unlock; | |
510 | } | |
511 | ||
512 | obj = map->handle; | |
513 | if (!obj->dev->driver->gem_vm_ops) { | |
514 | ret = -EINVAL; | |
515 | goto out_unlock; | |
516 | } | |
517 | ||
518 | vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND; | |
519 | vma->vm_ops = obj->dev->driver->gem_vm_ops; | |
520 | vma->vm_private_data = map->handle; | |
521 | /* FIXME: use pgprot_writecombine when available */ | |
522 | prot = pgprot_val(vma->vm_page_prot); | |
ae14dc05 | 523 | #ifdef CONFIG_X86 |
a2c0a97b | 524 | prot |= _PAGE_CACHE_WC; |
ae14dc05 | 525 | #endif |
a2c0a97b JB |
526 | vma->vm_page_prot = __pgprot(prot); |
527 | ||
528 | vma->vm_file = filp; /* Needed for drm_vm_open() */ | |
529 | drm_vm_open_locked(vma); | |
530 | ||
531 | out_unlock: | |
532 | mutex_unlock(&dev->struct_mutex); | |
533 | ||
534 | return ret; | |
535 | } | |
536 | EXPORT_SYMBOL(drm_gem_mmap); |