Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
9fc5cde7 | 2 | * Legacy: Generic DRM Buffer Management |
1da177e4 LT |
3 | * |
4 | * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. | |
5 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. | |
6 | * All Rights Reserved. | |
7 | * | |
9fc5cde7 DH |
8 | * Author: Rickard E. (Rik) Faith <faith@valinux.com> |
9 | * Author: Gareth Hughes <gareth@valinux.com> | |
10 | * | |
1da177e4 LT |
11 | * Permission is hereby granted, free of charge, to any person obtaining a |
12 | * copy of this software and associated documentation files (the "Software"), | |
13 | * to deal in the Software without restriction, including without limitation | |
14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
15 | * and/or sell copies of the Software, and to permit persons to whom the | |
16 | * Software is furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice (including the next | |
19 | * paragraph) shall be included in all copies or substantial portions of the | |
20 | * Software. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
25 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
26 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
27 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
28 | * OTHER DEALINGS IN THE SOFTWARE. | |
29 | */ | |
30 | ||
31 | #include <linux/vmalloc.h> | |
5a0e3ad6 | 32 | #include <linux/slab.h> |
f1a2a9b6 | 33 | #include <linux/log2.h> |
2d1a8a48 | 34 | #include <linux/export.h> |
f1a2a9b6 | 35 | #include <asm/shmparam.h> |
760285e7 | 36 | #include <drm/drmP.h> |
9fc5cde7 | 37 | #include "drm_legacy.h" |
1da177e4 | 38 | |
55910517 | 39 | static struct drm_map_list *drm_find_matching_map(struct drm_device *dev, |
f77d390c | 40 | struct drm_local_map *map) |
836cf046 | 41 | { |
55910517 | 42 | struct drm_map_list *entry; |
bd1b331f | 43 | list_for_each_entry(entry, &dev->maplist, head) { |
41c2e75e BH |
44 | /* |
45 | * Because the kernel-userspace ABI is fixed at a 32-bit offset | |
66aa6962 TV |
46 | * while PCI resources may live above that, we only compare the |
47 | * lower 32 bits of the map offset for maps of type | |
48 | * _DRM_FRAMEBUFFER or _DRM_REGISTERS. | |
49 | * It is assumed that if a driver have more than one resource | |
50 | * of each type, the lower 32 bits are different. | |
41c2e75e BH |
51 | */ |
52 | if (!entry->map || | |
53 | map->type != entry->map->type || | |
54 | entry->master != dev->primary->master) | |
55 | continue; | |
56 | switch (map->type) { | |
57 | case _DRM_SHM: | |
58 | if (map->flags != _DRM_CONTAINS_LOCK) | |
59 | break; | |
66aa6962 | 60 | return entry; |
41c2e75e BH |
61 | case _DRM_REGISTERS: |
62 | case _DRM_FRAME_BUFFER: | |
66aa6962 TV |
63 | if ((entry->map->offset & 0xffffffff) == |
64 | (map->offset & 0xffffffff)) | |
65 | return entry; | |
41c2e75e BH |
66 | default: /* Make gcc happy */ |
67 | ; | |
836cf046 | 68 | } |
41c2e75e BH |
69 | if (entry->map->offset == map->offset) |
70 | return entry; | |
836cf046 DA |
71 | } |
72 | ||
73 | return NULL; | |
1da177e4 | 74 | } |
1da177e4 | 75 | |
e0be428e | 76 | static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash, |
f1a2a9b6 | 77 | unsigned long user_token, int hashed_handle, int shm) |
d1f2b55a | 78 | { |
f1a2a9b6 DM |
79 | int use_hashed_handle, shift; |
80 | unsigned long add; | |
81 | ||
c2604ce0 | 82 | #if (BITS_PER_LONG == 64) |
8d153f71 TH |
83 | use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle); |
84 | #elif (BITS_PER_LONG == 32) | |
85 | use_hashed_handle = hashed_handle; | |
86 | #else | |
87 | #error Unsupported long size. Neither 64 nor 32 bits. | |
88 | #endif | |
d1f2b55a | 89 | |
e08870c8 TH |
90 | if (!use_hashed_handle) { |
91 | int ret; | |
1545085a | 92 | hash->key = user_token >> PAGE_SHIFT; |
e08870c8 TH |
93 | ret = drm_ht_insert_item(&dev->map_hash, hash); |
94 | if (ret != -EINVAL) | |
95 | return ret; | |
d1f2b55a | 96 | } |
f1a2a9b6 DM |
97 | |
98 | shift = 0; | |
99 | add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT; | |
100 | if (shm && (SHMLBA > PAGE_SIZE)) { | |
101 | int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1; | |
102 | ||
103 | /* For shared memory, we have to preserve the SHMLBA | |
104 | * bits of the eventual vma->vm_pgoff value during | |
105 | * mmap(). Otherwise we run into cache aliasing problems | |
106 | * on some platforms. On these platforms, the pgoff of | |
107 | * a mmap() request is used to pick a suitable virtual | |
108 | * address for the mmap() region such that it will not | |
109 | * cause cache aliasing problems. | |
110 | * | |
111 | * Therefore, make sure the SHMLBA relevant bits of the | |
112 | * hash value we use are equal to those in the original | |
113 | * kernel virtual address. | |
114 | */ | |
115 | shift = bits; | |
116 | add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL)); | |
117 | } | |
118 | ||
e08870c8 TH |
119 | return drm_ht_just_insert_please(&dev->map_hash, hash, |
120 | user_token, 32 - PAGE_SHIFT - 3, | |
f1a2a9b6 | 121 | shift, add); |
d1f2b55a | 122 | } |
9a186645 | 123 | |
1da177e4 | 124 | /** |
f77d390c BH |
125 | * Core function to create a range of memory available for mapping by a |
126 | * non-root process. | |
1da177e4 LT |
127 | * |
128 | * Adjusts the memory offset to its absolute value according to the mapping | |
129 | * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where | |
130 | * applicable and if supported by the kernel. | |
131 | */ | |
41c2e75e | 132 | static int drm_addmap_core(struct drm_device * dev, resource_size_t offset, |
c60ce623 | 133 | unsigned int size, enum drm_map_type type, |
55910517 DA |
134 | enum drm_map_flags flags, |
135 | struct drm_map_list ** maplist) | |
1da177e4 | 136 | { |
f77d390c | 137 | struct drm_local_map *map; |
55910517 | 138 | struct drm_map_list *list; |
9c8da5eb | 139 | drm_dma_handle_t *dmah; |
8d153f71 TH |
140 | unsigned long user_token; |
141 | int ret; | |
1da177e4 | 142 | |
9a298b2a | 143 | map = kmalloc(sizeof(*map), GFP_KERNEL); |
b5e89ed5 | 144 | if (!map) |
1da177e4 LT |
145 | return -ENOMEM; |
146 | ||
7ab98401 DA |
147 | map->offset = offset; |
148 | map->size = size; | |
149 | map->flags = flags; | |
150 | map->type = type; | |
1da177e4 LT |
151 | |
152 | /* Only allow shared memory to be removable since we only keep enough | |
153 | * book keeping information about shared memory to allow for removal | |
154 | * when processes fork. | |
155 | */ | |
b5e89ed5 | 156 | if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) { |
9a298b2a | 157 | kfree(map); |
1da177e4 LT |
158 | return -EINVAL; |
159 | } | |
41c2e75e BH |
160 | DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n", |
161 | (unsigned long long)map->offset, map->size, map->type); | |
b6741377 BH |
162 | |
163 | /* page-align _DRM_SHM maps. They are allocated here so there is no security | |
164 | * hole created by that and it works around various broken drivers that use | |
165 | * a non-aligned quantity to map the SAREA. --BenH | |
166 | */ | |
167 | if (map->type == _DRM_SHM) | |
168 | map->size = PAGE_ALIGN(map->size); | |
169 | ||
41c2e75e | 170 | if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) { |
9a298b2a | 171 | kfree(map); |
1da177e4 LT |
172 | return -EINVAL; |
173 | } | |
b5e89ed5 | 174 | map->mtrr = -1; |
1da177e4 LT |
175 | map->handle = NULL; |
176 | ||
b5e89ed5 | 177 | switch (map->type) { |
1da177e4 LT |
178 | case _DRM_REGISTERS: |
179 | case _DRM_FRAME_BUFFER: | |
4b7fb9b5 | 180 | #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__) |
8d2ea625 | 181 | if (map->offset + (map->size-1) < map->offset || |
b5e89ed5 | 182 | map->offset < virt_to_phys(high_memory)) { |
9a298b2a | 183 | kfree(map); |
1da177e4 LT |
184 | return -EINVAL; |
185 | } | |
1da177e4 | 186 | #endif |
836cf046 DA |
187 | /* Some drivers preinitialize some maps, without the X Server |
188 | * needing to be aware of it. Therefore, we just return success | |
189 | * when the server tries to create a duplicate map. | |
190 | */ | |
89625eb1 DA |
191 | list = drm_find_matching_map(dev, map); |
192 | if (list != NULL) { | |
193 | if (list->map->size != map->size) { | |
836cf046 | 194 | DRM_DEBUG("Matching maps of type %d with " |
b5e89ed5 DA |
195 | "mismatched sizes, (%ld vs %ld)\n", |
196 | map->type, map->size, | |
197 | list->map->size); | |
89625eb1 | 198 | list->map->size = map->size; |
836cf046 DA |
199 | } |
200 | ||
9a298b2a | 201 | kfree(map); |
89625eb1 | 202 | *maplist = list; |
836cf046 DA |
203 | return 0; |
204 | } | |
205 | ||
28185647 DV |
206 | if (map->type == _DRM_FRAME_BUFFER || |
207 | (map->flags & _DRM_WRITE_COMBINING)) { | |
208 | map->mtrr = | |
209 | arch_phys_wc_add(map->offset, map->size); | |
1da177e4 | 210 | } |
0769d39c | 211 | if (map->type == _DRM_REGISTERS) { |
ff47eaf2 AL |
212 | if (map->flags & _DRM_WRITE_COMBINING) |
213 | map->handle = ioremap_wc(map->offset, | |
214 | map->size); | |
215 | else | |
216 | map->handle = ioremap(map->offset, map->size); | |
0769d39c | 217 | if (!map->handle) { |
9a298b2a | 218 | kfree(map); |
0769d39c ST |
219 | return -ENOMEM; |
220 | } | |
221 | } | |
bc5f4523 | 222 | |
1da177e4 | 223 | break; |
1da177e4 | 224 | case _DRM_SHM: |
54ba2f76 DA |
225 | list = drm_find_matching_map(dev, map); |
226 | if (list != NULL) { | |
227 | if(list->map->size != map->size) { | |
228 | DRM_DEBUG("Matching maps of type %d with " | |
229 | "mismatched sizes, (%ld vs %ld)\n", | |
230 | map->type, map->size, list->map->size); | |
231 | list->map->size = map->size; | |
232 | } | |
233 | ||
9a298b2a | 234 | kfree(map); |
54ba2f76 DA |
235 | *maplist = list; |
236 | return 0; | |
237 | } | |
f239b7b0 | 238 | map->handle = vmalloc_user(map->size); |
b5e89ed5 | 239 | DRM_DEBUG("%lu %d %p\n", |
04420c9c | 240 | map->size, order_base_2(map->size), map->handle); |
b5e89ed5 | 241 | if (!map->handle) { |
9a298b2a | 242 | kfree(map); |
1da177e4 LT |
243 | return -ENOMEM; |
244 | } | |
245 | map->offset = (unsigned long)map->handle; | |
b5e89ed5 | 246 | if (map->flags & _DRM_CONTAINS_LOCK) { |
1da177e4 | 247 | /* Prevent a 2nd X Server from creating a 2nd lock */ |
7c1c2871 | 248 | if (dev->primary->master->lock.hw_lock != NULL) { |
b5e89ed5 | 249 | vfree(map->handle); |
9a298b2a | 250 | kfree(map); |
1da177e4 LT |
251 | return -EBUSY; |
252 | } | |
7c1c2871 | 253 | dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */ |
1da177e4 LT |
254 | } |
255 | break; | |
54ba2f76 | 256 | case _DRM_AGP: { |
55910517 | 257 | struct drm_agp_mem *entry; |
54ba2f76 DA |
258 | int valid = 0; |
259 | ||
d9906753 | 260 | if (!dev->agp) { |
9a298b2a | 261 | kfree(map); |
54ba2f76 DA |
262 | return -EINVAL; |
263 | } | |
1da177e4 | 264 | #ifdef __alpha__ |
54ba2f76 | 265 | map->offset += dev->hose->mem_space->start; |
1da177e4 | 266 | #endif |
47a184a8 EA |
267 | /* In some cases (i810 driver), user space may have already |
268 | * added the AGP base itself, because dev->agp->base previously | |
269 | * only got set during AGP enable. So, only add the base | |
270 | * address if the map's offset isn't already within the | |
271 | * aperture. | |
54ba2f76 | 272 | */ |
47a184a8 EA |
273 | if (map->offset < dev->agp->base || |
274 | map->offset > dev->agp->base + | |
275 | dev->agp->agp_info.aper_size * 1024 * 1024 - 1) { | |
276 | map->offset += dev->agp->base; | |
277 | } | |
54ba2f76 DA |
278 | map->mtrr = dev->agp->agp_mtrr; /* for getmap */ |
279 | ||
280 | /* This assumes the DRM is in total control of AGP space. | |
281 | * It's not always the case as AGP can be in the control | |
282 | * of user space (i.e. i810 driver). So this loop will get | |
283 | * skipped and we double check that dev->agp->memory is | |
284 | * actually set as well as being invalid before EPERM'ing | |
285 | */ | |
bd1b331f | 286 | list_for_each_entry(entry, &dev->agp->memory, head) { |
54ba2f76 DA |
287 | if ((map->offset >= entry->bound) && |
288 | (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) { | |
289 | valid = 1; | |
290 | break; | |
291 | } | |
1da177e4 | 292 | } |
bd1b331f | 293 | if (!list_empty(&dev->agp->memory) && !valid) { |
9a298b2a | 294 | kfree(map); |
54ba2f76 DA |
295 | return -EPERM; |
296 | } | |
41c2e75e BH |
297 | DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n", |
298 | (unsigned long long)map->offset, map->size); | |
54ba2f76 | 299 | |
a2c0a97b | 300 | break; |
812c369d | 301 | } |
1da177e4 LT |
302 | case _DRM_SCATTER_GATHER: |
303 | if (!dev->sg) { | |
9a298b2a | 304 | kfree(map); |
1da177e4 LT |
305 | return -EINVAL; |
306 | } | |
d1f2b55a | 307 | map->offset += (unsigned long)dev->sg->virtual; |
1da177e4 | 308 | break; |
b5e89ed5 | 309 | case _DRM_CONSISTENT: |
2d0f9eaf | 310 | /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G, |
9c8da5eb | 311 | * As we're limiting the address to 2^32-1 (or less), |
2d0f9eaf DA |
312 | * casting it down to 32 bits is no problem, but we |
313 | * need to point to a 64bit variable first. */ | |
e6be8d9d | 314 | dmah = drm_pci_alloc(dev, map->size, map->size); |
9c8da5eb | 315 | if (!dmah) { |
9a298b2a | 316 | kfree(map); |
2d0f9eaf DA |
317 | return -ENOMEM; |
318 | } | |
9c8da5eb DA |
319 | map->handle = dmah->vaddr; |
320 | map->offset = (unsigned long)dmah->busaddr; | |
321 | kfree(dmah); | |
2d0f9eaf | 322 | break; |
1da177e4 | 323 | default: |
9a298b2a | 324 | kfree(map); |
1da177e4 LT |
325 | return -EINVAL; |
326 | } | |
327 | ||
94e3370e | 328 | list = kzalloc(sizeof(*list), GFP_KERNEL); |
b5e89ed5 | 329 | if (!list) { |
85abb3f9 | 330 | if (map->type == _DRM_REGISTERS) |
004a7727 | 331 | iounmap(map->handle); |
9a298b2a | 332 | kfree(map); |
1da177e4 LT |
333 | return -EINVAL; |
334 | } | |
1da177e4 LT |
335 | list->map = map; |
336 | ||
30e2fb18 | 337 | mutex_lock(&dev->struct_mutex); |
bd1b331f | 338 | list_add(&list->head, &dev->maplist); |
8d153f71 | 339 | |
d1f2b55a | 340 | /* Assign a 32-bit handle */ |
30e2fb18 | 341 | /* We do it here so that dev->struct_mutex protects the increment */ |
8d153f71 TH |
342 | user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle : |
343 | map->offset; | |
f1a2a9b6 DM |
344 | ret = drm_map_handle(dev, &list->hash, user_token, 0, |
345 | (map->type == _DRM_SHM)); | |
8d153f71 | 346 | if (ret) { |
85abb3f9 | 347 | if (map->type == _DRM_REGISTERS) |
004a7727 | 348 | iounmap(map->handle); |
9a298b2a EA |
349 | kfree(map); |
350 | kfree(list); | |
8d153f71 TH |
351 | mutex_unlock(&dev->struct_mutex); |
352 | return ret; | |
353 | } | |
354 | ||
1545085a | 355 | list->user_token = list->hash.key << PAGE_SHIFT; |
30e2fb18 | 356 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 357 | |
2ff2e8a3 BS |
358 | if (!(map->flags & _DRM_DRIVER)) |
359 | list->master = dev->primary->master; | |
89625eb1 | 360 | *maplist = list; |
7ab98401 | 361 | return 0; |
afe0f696 | 362 | } |
89625eb1 | 363 | |
9fc5cde7 DH |
364 | int drm_legacy_addmap(struct drm_device * dev, resource_size_t offset, |
365 | unsigned int size, enum drm_map_type type, | |
366 | enum drm_map_flags flags, struct drm_local_map **map_ptr) | |
89625eb1 | 367 | { |
55910517 | 368 | struct drm_map_list *list; |
89625eb1 DA |
369 | int rc; |
370 | ||
371 | rc = drm_addmap_core(dev, offset, size, type, flags, &list); | |
372 | if (!rc) | |
373 | *map_ptr = list->map; | |
374 | return rc; | |
375 | } | |
9fc5cde7 | 376 | EXPORT_SYMBOL(drm_legacy_addmap); |
7ab98401 | 377 | |
f77d390c BH |
378 | /** |
379 | * Ioctl to specify a range of memory that is available for mapping by a | |
380 | * non-root process. | |
381 | * | |
382 | * \param inode device inode. | |
383 | * \param file_priv DRM file private. | |
384 | * \param cmd command. | |
385 | * \param arg pointer to a drm_map structure. | |
386 | * \return zero on success or a negative value on error. | |
387 | * | |
388 | */ | |
9fc5cde7 DH |
389 | int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data, |
390 | struct drm_file *file_priv) | |
7ab98401 | 391 | { |
c153f45f | 392 | struct drm_map *map = data; |
55910517 | 393 | struct drm_map_list *maplist; |
7ab98401 DA |
394 | int err; |
395 | ||
7c1c2871 | 396 | if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM)) |
d985c108 DA |
397 | return -EPERM; |
398 | ||
c153f45f EA |
399 | err = drm_addmap_core(dev, map->offset, map->size, map->type, |
400 | map->flags, &maplist); | |
7ab98401 | 401 | |
b5e89ed5 | 402 | if (err) |
7ab98401 | 403 | return err; |
d1f2b55a | 404 | |
67e1a014 | 405 | /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */ |
c153f45f | 406 | map->handle = (void *)(unsigned long)maplist->user_token; |
0dd99f1b AL |
407 | |
408 | /* | |
409 | * It appears that there are no users of this value whatsoever -- | |
410 | * drmAddMap just discards it. Let's not encourage its use. | |
411 | * (Keeping drm_addmap_core's returned mtrr value would be wrong -- | |
412 | * it's not a real mtrr index anymore.) | |
413 | */ | |
414 | map->mtrr = -1; | |
415 | ||
1da177e4 | 416 | return 0; |
88f399cd | 417 | } |
1da177e4 | 418 | |
1da177e4 LT |
419 | /** |
420 | * Remove a map private from list and deallocate resources if the mapping | |
421 | * isn't in use. | |
422 | * | |
1da177e4 LT |
423 | * Searches the map on drm_device::maplist, removes it from the list, see if |
424 | * its being used, and free any associate resource (such as MTRR's) if it's not | |
425 | * being on use. | |
426 | * | |
9fc5cde7 | 427 | * \sa drm_legacy_addmap |
1da177e4 | 428 | */ |
9fc5cde7 | 429 | int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map) |
1da177e4 | 430 | { |
55910517 | 431 | struct drm_map_list *r_list = NULL, *list_t; |
836cf046 | 432 | drm_dma_handle_t dmah; |
bd1b331f | 433 | int found = 0; |
7c1c2871 | 434 | struct drm_master *master; |
1da177e4 | 435 | |
836cf046 | 436 | /* Find the list entry for the map and remove it */ |
bd1b331f | 437 | list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) { |
836cf046 | 438 | if (r_list->map == map) { |
7c1c2871 | 439 | master = r_list->master; |
bd1b331f | 440 | list_del(&r_list->head); |
1545085a TH |
441 | drm_ht_remove_key(&dev->map_hash, |
442 | r_list->user_token >> PAGE_SHIFT); | |
9a298b2a | 443 | kfree(r_list); |
bd1b331f | 444 | found = 1; |
836cf046 DA |
445 | break; |
446 | } | |
1da177e4 LT |
447 | } |
448 | ||
bd1b331f | 449 | if (!found) |
1da177e4 | 450 | return -EINVAL; |
1da177e4 | 451 | |
836cf046 DA |
452 | switch (map->type) { |
453 | case _DRM_REGISTERS: | |
004a7727 | 454 | iounmap(map->handle); |
836cf046 DA |
455 | /* FALLTHROUGH */ |
456 | case _DRM_FRAME_BUFFER: | |
28185647 | 457 | arch_phys_wc_del(map->mtrr); |
836cf046 DA |
458 | break; |
459 | case _DRM_SHM: | |
460 | vfree(map->handle); | |
7c1c2871 DA |
461 | if (master) { |
462 | if (dev->sigdata.lock == master->lock.hw_lock) | |
463 | dev->sigdata.lock = NULL; | |
464 | master->lock.hw_lock = NULL; /* SHM removed */ | |
465 | master->lock.file_priv = NULL; | |
171901d1 | 466 | wake_up_interruptible_all(&master->lock.lock_queue); |
7c1c2871 | 467 | } |
836cf046 DA |
468 | break; |
469 | case _DRM_AGP: | |
470 | case _DRM_SCATTER_GATHER: | |
471 | break; | |
472 | case _DRM_CONSISTENT: | |
473 | dmah.vaddr = map->handle; | |
474 | dmah.busaddr = map->offset; | |
475 | dmah.size = map->size; | |
1c96e84e | 476 | __drm_legacy_pci_free(dev, &dmah); |
836cf046 | 477 | break; |
1da177e4 | 478 | } |
9a298b2a | 479 | kfree(map); |
836cf046 | 480 | |
1da177e4 LT |
481 | return 0; |
482 | } | |
9fc5cde7 | 483 | EXPORT_SYMBOL(drm_legacy_rmmap_locked); |
836cf046 | 484 | |
9fc5cde7 | 485 | int drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map) |
836cf046 DA |
486 | { |
487 | int ret; | |
488 | ||
30e2fb18 | 489 | mutex_lock(&dev->struct_mutex); |
9fc5cde7 | 490 | ret = drm_legacy_rmmap_locked(dev, map); |
30e2fb18 | 491 | mutex_unlock(&dev->struct_mutex); |
836cf046 DA |
492 | |
493 | return ret; | |
494 | } | |
9fc5cde7 | 495 | EXPORT_SYMBOL(drm_legacy_rmmap); |
7ab98401 | 496 | |
836cf046 DA |
497 | /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on |
498 | * the last close of the device, and this is necessary for cleanup when things | |
499 | * exit uncleanly. Therefore, having userland manually remove mappings seems | |
500 | * like a pointless exercise since they're going away anyway. | |
501 | * | |
502 | * One use case might be after addmap is allowed for normal users for SHM and | |
503 | * gets used by drivers that the server doesn't need to care about. This seems | |
504 | * unlikely. | |
f77d390c BH |
505 | * |
506 | * \param inode device inode. | |
507 | * \param file_priv DRM file private. | |
508 | * \param cmd command. | |
509 | * \param arg pointer to a struct drm_map structure. | |
510 | * \return zero on success or a negative value on error. | |
836cf046 | 511 | */ |
9fc5cde7 DH |
512 | int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data, |
513 | struct drm_file *file_priv) | |
7ab98401 | 514 | { |
c153f45f | 515 | struct drm_map *request = data; |
f77d390c | 516 | struct drm_local_map *map = NULL; |
55910517 | 517 | struct drm_map_list *r_list; |
836cf046 | 518 | int ret; |
7ab98401 | 519 | |
30e2fb18 | 520 | mutex_lock(&dev->struct_mutex); |
bd1b331f | 521 | list_for_each_entry(r_list, &dev->maplist, head) { |
836cf046 | 522 | if (r_list->map && |
c153f45f | 523 | r_list->user_token == (unsigned long)request->handle && |
836cf046 DA |
524 | r_list->map->flags & _DRM_REMOVABLE) { |
525 | map = r_list->map; | |
526 | break; | |
527 | } | |
528 | } | |
529 | ||
530 | /* List has wrapped around to the head pointer, or its empty we didn't | |
531 | * find anything. | |
532 | */ | |
bd1b331f | 533 | if (list_empty(&dev->maplist) || !map) { |
30e2fb18 | 534 | mutex_unlock(&dev->struct_mutex); |
836cf046 DA |
535 | return -EINVAL; |
536 | } | |
537 | ||
836cf046 DA |
538 | /* Register and framebuffer maps are permanent */ |
539 | if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) { | |
30e2fb18 | 540 | mutex_unlock(&dev->struct_mutex); |
836cf046 DA |
541 | return 0; |
542 | } | |
543 | ||
9fc5cde7 | 544 | ret = drm_legacy_rmmap_locked(dev, map); |
836cf046 | 545 | |
30e2fb18 | 546 | mutex_unlock(&dev->struct_mutex); |
836cf046 DA |
547 | |
548 | return ret; | |
7ab98401 | 549 | } |
1da177e4 LT |
550 | |
551 | /** | |
552 | * Cleanup after an error on one of the addbufs() functions. | |
553 | * | |
836cf046 | 554 | * \param dev DRM device. |
1da177e4 LT |
555 | * \param entry buffer entry where the error occurred. |
556 | * | |
557 | * Frees any pages and buffers associated with the given entry. | |
558 | */ | |
cdd55a29 DA |
559 | static void drm_cleanup_buf_error(struct drm_device * dev, |
560 | struct drm_buf_entry * entry) | |
1da177e4 LT |
561 | { |
562 | int i; | |
563 | ||
564 | if (entry->seg_count) { | |
565 | for (i = 0; i < entry->seg_count; i++) { | |
566 | if (entry->seglist[i]) { | |
ddf19b97 | 567 | drm_pci_free(dev, entry->seglist[i]); |
1da177e4 LT |
568 | } |
569 | } | |
9a298b2a | 570 | kfree(entry->seglist); |
1da177e4 LT |
571 | |
572 | entry->seg_count = 0; | |
573 | } | |
574 | ||
b5e89ed5 DA |
575 | if (entry->buf_count) { |
576 | for (i = 0; i < entry->buf_count; i++) { | |
9a298b2a | 577 | kfree(entry->buflist[i].dev_private); |
1da177e4 | 578 | } |
9a298b2a | 579 | kfree(entry->buflist); |
1da177e4 LT |
580 | |
581 | entry->buf_count = 0; | |
582 | } | |
583 | } | |
584 | ||
a7fb8a23 | 585 | #if IS_ENABLED(CONFIG_AGP) |
1da177e4 | 586 | /** |
d59431bf | 587 | * Add AGP buffers for DMA transfers. |
1da177e4 | 588 | * |
84b1fd10 | 589 | * \param dev struct drm_device to which the buffers are to be added. |
c60ce623 | 590 | * \param request pointer to a struct drm_buf_desc describing the request. |
1da177e4 | 591 | * \return zero on success or a negative number on failure. |
b5e89ed5 | 592 | * |
1da177e4 LT |
593 | * After some sanity checks creates a drm_buf structure for each buffer and |
594 | * reallocates the buffer list of the same size order to accommodate the new | |
595 | * buffers. | |
596 | */ | |
9fc5cde7 DH |
597 | int drm_legacy_addbufs_agp(struct drm_device *dev, |
598 | struct drm_buf_desc *request) | |
1da177e4 | 599 | { |
cdd55a29 DA |
600 | struct drm_device_dma *dma = dev->dma; |
601 | struct drm_buf_entry *entry; | |
55910517 | 602 | struct drm_agp_mem *agp_entry; |
056219e2 | 603 | struct drm_buf *buf; |
1da177e4 LT |
604 | unsigned long offset; |
605 | unsigned long agp_offset; | |
606 | int count; | |
607 | int order; | |
608 | int size; | |
609 | int alignment; | |
610 | int page_order; | |
611 | int total; | |
612 | int byte_count; | |
54ba2f76 | 613 | int i, valid; |
056219e2 | 614 | struct drm_buf **temp_buflist; |
1da177e4 | 615 | |
b5e89ed5 DA |
616 | if (!dma) |
617 | return -EINVAL; | |
1da177e4 | 618 | |
d59431bf | 619 | count = request->count; |
04420c9c | 620 | order = order_base_2(request->size); |
1da177e4 LT |
621 | size = 1 << order; |
622 | ||
b5e89ed5 DA |
623 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
624 | ? PAGE_ALIGN(size) : size; | |
1da177e4 LT |
625 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
626 | total = PAGE_SIZE << page_order; | |
627 | ||
628 | byte_count = 0; | |
d59431bf | 629 | agp_offset = dev->agp->base + request->agp_start; |
1da177e4 | 630 | |
b5e89ed5 DA |
631 | DRM_DEBUG("count: %d\n", count); |
632 | DRM_DEBUG("order: %d\n", order); | |
633 | DRM_DEBUG("size: %d\n", size); | |
d985c108 | 634 | DRM_DEBUG("agp_offset: %lx\n", agp_offset); |
b5e89ed5 DA |
635 | DRM_DEBUG("alignment: %d\n", alignment); |
636 | DRM_DEBUG("page_order: %d\n", page_order); | |
637 | DRM_DEBUG("total: %d\n", total); | |
1da177e4 | 638 | |
b5e89ed5 DA |
639 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
640 | return -EINVAL; | |
1da177e4 | 641 | |
54ba2f76 DA |
642 | /* Make sure buffers are located in AGP memory that we own */ |
643 | valid = 0; | |
bd1b331f | 644 | list_for_each_entry(agp_entry, &dev->agp->memory, head) { |
54ba2f76 DA |
645 | if ((agp_offset >= agp_entry->bound) && |
646 | (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) { | |
647 | valid = 1; | |
648 | break; | |
649 | } | |
650 | } | |
bd1b331f | 651 | if (!list_empty(&dev->agp->memory) && !valid) { |
54ba2f76 DA |
652 | DRM_DEBUG("zone invalid\n"); |
653 | return -EINVAL; | |
654 | } | |
2177a218 | 655 | spin_lock(&dev->buf_lock); |
b5e89ed5 | 656 | if (dev->buf_use) { |
2177a218 | 657 | spin_unlock(&dev->buf_lock); |
1da177e4 LT |
658 | return -EBUSY; |
659 | } | |
b5e89ed5 | 660 | atomic_inc(&dev->buf_alloc); |
2177a218 | 661 | spin_unlock(&dev->buf_lock); |
1da177e4 | 662 | |
30e2fb18 | 663 | mutex_lock(&dev->struct_mutex); |
1da177e4 | 664 | entry = &dma->bufs[order]; |
b5e89ed5 | 665 | if (entry->buf_count) { |
30e2fb18 | 666 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 DA |
667 | atomic_dec(&dev->buf_alloc); |
668 | return -ENOMEM; /* May only call once for each order */ | |
1da177e4 LT |
669 | } |
670 | ||
671 | if (count < 0 || count > 4096) { | |
30e2fb18 | 672 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 673 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
674 | return -EINVAL; |
675 | } | |
676 | ||
94e3370e | 677 | entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL); |
b5e89ed5 | 678 | if (!entry->buflist) { |
30e2fb18 | 679 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 680 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
681 | return -ENOMEM; |
682 | } | |
1da177e4 LT |
683 | |
684 | entry->buf_size = size; | |
685 | entry->page_order = page_order; | |
686 | ||
687 | offset = 0; | |
688 | ||
b5e89ed5 DA |
689 | while (entry->buf_count < count) { |
690 | buf = &entry->buflist[entry->buf_count]; | |
691 | buf->idx = dma->buf_count + entry->buf_count; | |
692 | buf->total = alignment; | |
693 | buf->order = order; | |
694 | buf->used = 0; | |
1da177e4 | 695 | |
b5e89ed5 | 696 | buf->offset = (dma->byte_count + offset); |
1da177e4 LT |
697 | buf->bus_address = agp_offset + offset; |
698 | buf->address = (void *)(agp_offset + offset); | |
b5e89ed5 | 699 | buf->next = NULL; |
1da177e4 LT |
700 | buf->waiting = 0; |
701 | buf->pending = 0; | |
6c340eac | 702 | buf->file_priv = NULL; |
1da177e4 LT |
703 | |
704 | buf->dev_priv_size = dev->driver->dev_priv_size; | |
94e3370e | 705 | buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL); |
b5e89ed5 | 706 | if (!buf->dev_private) { |
1da177e4 LT |
707 | /* Set count correctly so we free the proper amount. */ |
708 | entry->buf_count = count; | |
b5e89ed5 | 709 | drm_cleanup_buf_error(dev, entry); |
30e2fb18 | 710 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 711 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
712 | return -ENOMEM; |
713 | } | |
1da177e4 | 714 | |
b5e89ed5 | 715 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
1da177e4 LT |
716 | |
717 | offset += alignment; | |
718 | entry->buf_count++; | |
719 | byte_count += PAGE_SIZE << page_order; | |
720 | } | |
721 | ||
b5e89ed5 | 722 | DRM_DEBUG("byte_count: %d\n", byte_count); |
1da177e4 | 723 | |
9a298b2a EA |
724 | temp_buflist = krealloc(dma->buflist, |
725 | (dma->buf_count + entry->buf_count) * | |
726 | sizeof(*dma->buflist), GFP_KERNEL); | |
b5e89ed5 | 727 | if (!temp_buflist) { |
1da177e4 | 728 | /* Free the entry because it isn't valid */ |
b5e89ed5 | 729 | drm_cleanup_buf_error(dev, entry); |
30e2fb18 | 730 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 731 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
732 | return -ENOMEM; |
733 | } | |
734 | dma->buflist = temp_buflist; | |
735 | ||
b5e89ed5 | 736 | for (i = 0; i < entry->buf_count; i++) { |
1da177e4 LT |
737 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
738 | } | |
739 | ||
740 | dma->buf_count += entry->buf_count; | |
d985c108 DA |
741 | dma->seg_count += entry->seg_count; |
742 | dma->page_count += byte_count >> PAGE_SHIFT; | |
1da177e4 LT |
743 | dma->byte_count += byte_count; |
744 | ||
b5e89ed5 DA |
745 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
746 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); | |
1da177e4 | 747 | |
30e2fb18 | 748 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 749 | |
d59431bf DA |
750 | request->count = entry->buf_count; |
751 | request->size = size; | |
1da177e4 LT |
752 | |
753 | dma->flags = _DRM_DMA_USE_AGP; | |
754 | ||
b5e89ed5 | 755 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
756 | return 0; |
757 | } | |
9fc5cde7 | 758 | EXPORT_SYMBOL(drm_legacy_addbufs_agp); |
a7fb8a23 | 759 | #endif /* CONFIG_AGP */ |
1da177e4 | 760 | |
9fc5cde7 DH |
761 | int drm_legacy_addbufs_pci(struct drm_device *dev, |
762 | struct drm_buf_desc *request) | |
1da177e4 | 763 | { |
cdd55a29 | 764 | struct drm_device_dma *dma = dev->dma; |
1da177e4 LT |
765 | int count; |
766 | int order; | |
767 | int size; | |
768 | int total; | |
769 | int page_order; | |
cdd55a29 | 770 | struct drm_buf_entry *entry; |
ddf19b97 | 771 | drm_dma_handle_t *dmah; |
056219e2 | 772 | struct drm_buf *buf; |
1da177e4 LT |
773 | int alignment; |
774 | unsigned long offset; | |
775 | int i; | |
776 | int byte_count; | |
777 | int page_count; | |
778 | unsigned long *temp_pagelist; | |
056219e2 | 779 | struct drm_buf **temp_buflist; |
1da177e4 | 780 | |
b5e89ed5 DA |
781 | if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) |
782 | return -EINVAL; | |
d985c108 | 783 | |
b5e89ed5 DA |
784 | if (!dma) |
785 | return -EINVAL; | |
1da177e4 | 786 | |
d985c108 DA |
787 | if (!capable(CAP_SYS_ADMIN)) |
788 | return -EPERM; | |
789 | ||
d59431bf | 790 | count = request->count; |
04420c9c | 791 | order = order_base_2(request->size); |
1da177e4 LT |
792 | size = 1 << order; |
793 | ||
a344a7e7 DV |
794 | DRM_DEBUG("count=%d, size=%d (%d), order=%d\n", |
795 | request->count, request->size, size, order); | |
1da177e4 | 796 | |
b5e89ed5 DA |
797 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
798 | return -EINVAL; | |
1da177e4 | 799 | |
d59431bf | 800 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
b5e89ed5 | 801 | ? PAGE_ALIGN(size) : size; |
1da177e4 LT |
802 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
803 | total = PAGE_SIZE << page_order; | |
804 | ||
2177a218 | 805 | spin_lock(&dev->buf_lock); |
b5e89ed5 | 806 | if (dev->buf_use) { |
2177a218 | 807 | spin_unlock(&dev->buf_lock); |
1da177e4 LT |
808 | return -EBUSY; |
809 | } | |
b5e89ed5 | 810 | atomic_inc(&dev->buf_alloc); |
2177a218 | 811 | spin_unlock(&dev->buf_lock); |
1da177e4 | 812 | |
30e2fb18 | 813 | mutex_lock(&dev->struct_mutex); |
1da177e4 | 814 | entry = &dma->bufs[order]; |
b5e89ed5 | 815 | if (entry->buf_count) { |
30e2fb18 | 816 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 817 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
818 | return -ENOMEM; /* May only call once for each order */ |
819 | } | |
820 | ||
821 | if (count < 0 || count > 4096) { | |
30e2fb18 | 822 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 823 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
824 | return -EINVAL; |
825 | } | |
826 | ||
94e3370e | 827 | entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL); |
b5e89ed5 | 828 | if (!entry->buflist) { |
30e2fb18 | 829 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 830 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
831 | return -ENOMEM; |
832 | } | |
b5e89ed5 | 833 | |
94e3370e | 834 | entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL); |
b5e89ed5 | 835 | if (!entry->seglist) { |
9a298b2a | 836 | kfree(entry->buflist); |
30e2fb18 | 837 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 838 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
839 | return -ENOMEM; |
840 | } | |
1da177e4 LT |
841 | |
842 | /* Keep the original pagelist until we know all the allocations | |
843 | * have succeeded | |
844 | */ | |
9a298b2a EA |
845 | temp_pagelist = kmalloc((dma->page_count + (count << page_order)) * |
846 | sizeof(*dma->pagelist), GFP_KERNEL); | |
1da177e4 | 847 | if (!temp_pagelist) { |
9a298b2a EA |
848 | kfree(entry->buflist); |
849 | kfree(entry->seglist); | |
30e2fb18 | 850 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 851 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
852 | return -ENOMEM; |
853 | } | |
854 | memcpy(temp_pagelist, | |
b5e89ed5 DA |
855 | dma->pagelist, dma->page_count * sizeof(*dma->pagelist)); |
856 | DRM_DEBUG("pagelist: %d entries\n", | |
857 | dma->page_count + (count << page_order)); | |
1da177e4 | 858 | |
b5e89ed5 | 859 | entry->buf_size = size; |
1da177e4 LT |
860 | entry->page_order = page_order; |
861 | byte_count = 0; | |
862 | page_count = 0; | |
863 | ||
b5e89ed5 | 864 | while (entry->buf_count < count) { |
bc5f4523 | 865 | |
e6be8d9d | 866 | dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000); |
bc5f4523 | 867 | |
ddf19b97 | 868 | if (!dmah) { |
1da177e4 LT |
869 | /* Set count correctly so we free the proper amount. */ |
870 | entry->buf_count = count; | |
871 | entry->seg_count = count; | |
872 | drm_cleanup_buf_error(dev, entry); | |
9a298b2a | 873 | kfree(temp_pagelist); |
30e2fb18 | 874 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 875 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
876 | return -ENOMEM; |
877 | } | |
ddf19b97 | 878 | entry->seglist[entry->seg_count++] = dmah; |
b5e89ed5 DA |
879 | for (i = 0; i < (1 << page_order); i++) { |
880 | DRM_DEBUG("page %d @ 0x%08lx\n", | |
881 | dma->page_count + page_count, | |
ddf19b97 | 882 | (unsigned long)dmah->vaddr + PAGE_SIZE * i); |
1da177e4 | 883 | temp_pagelist[dma->page_count + page_count++] |
ddf19b97 | 884 | = (unsigned long)dmah->vaddr + PAGE_SIZE * i; |
1da177e4 | 885 | } |
b5e89ed5 DA |
886 | for (offset = 0; |
887 | offset + size <= total && entry->buf_count < count; | |
888 | offset += alignment, ++entry->buf_count) { | |
889 | buf = &entry->buflist[entry->buf_count]; | |
890 | buf->idx = dma->buf_count + entry->buf_count; | |
891 | buf->total = alignment; | |
892 | buf->order = order; | |
893 | buf->used = 0; | |
894 | buf->offset = (dma->byte_count + byte_count + offset); | |
ddf19b97 DA |
895 | buf->address = (void *)(dmah->vaddr + offset); |
896 | buf->bus_address = dmah->busaddr + offset; | |
b5e89ed5 | 897 | buf->next = NULL; |
1da177e4 LT |
898 | buf->waiting = 0; |
899 | buf->pending = 0; | |
6c340eac | 900 | buf->file_priv = NULL; |
1da177e4 LT |
901 | |
902 | buf->dev_priv_size = dev->driver->dev_priv_size; | |
94e3370e DB |
903 | buf->dev_private = kzalloc(buf->dev_priv_size, |
904 | GFP_KERNEL); | |
b5e89ed5 | 905 | if (!buf->dev_private) { |
1da177e4 LT |
906 | /* Set count correctly so we free the proper amount. */ |
907 | entry->buf_count = count; | |
908 | entry->seg_count = count; | |
b5e89ed5 | 909 | drm_cleanup_buf_error(dev, entry); |
9a298b2a | 910 | kfree(temp_pagelist); |
30e2fb18 | 911 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 912 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
913 | return -ENOMEM; |
914 | } | |
1da177e4 | 915 | |
b5e89ed5 DA |
916 | DRM_DEBUG("buffer %d @ %p\n", |
917 | entry->buf_count, buf->address); | |
1da177e4 LT |
918 | } |
919 | byte_count += PAGE_SIZE << page_order; | |
920 | } | |
921 | ||
9a298b2a EA |
922 | temp_buflist = krealloc(dma->buflist, |
923 | (dma->buf_count + entry->buf_count) * | |
924 | sizeof(*dma->buflist), GFP_KERNEL); | |
1da177e4 LT |
925 | if (!temp_buflist) { |
926 | /* Free the entry because it isn't valid */ | |
b5e89ed5 | 927 | drm_cleanup_buf_error(dev, entry); |
9a298b2a | 928 | kfree(temp_pagelist); |
30e2fb18 | 929 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 930 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
931 | return -ENOMEM; |
932 | } | |
933 | dma->buflist = temp_buflist; | |
934 | ||
b5e89ed5 | 935 | for (i = 0; i < entry->buf_count; i++) { |
1da177e4 LT |
936 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
937 | } | |
938 | ||
88393161 | 939 | /* No allocations failed, so now we can replace the original pagelist |
1da177e4 LT |
940 | * with the new one. |
941 | */ | |
942 | if (dma->page_count) { | |
9a298b2a | 943 | kfree(dma->pagelist); |
1da177e4 LT |
944 | } |
945 | dma->pagelist = temp_pagelist; | |
946 | ||
947 | dma->buf_count += entry->buf_count; | |
948 | dma->seg_count += entry->seg_count; | |
949 | dma->page_count += entry->seg_count << page_order; | |
950 | dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order); | |
951 | ||
30e2fb18 | 952 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 953 | |
d59431bf DA |
954 | request->count = entry->buf_count; |
955 | request->size = size; | |
1da177e4 | 956 | |
3417f33e GS |
957 | if (request->flags & _DRM_PCI_BUFFER_RO) |
958 | dma->flags = _DRM_DMA_USE_PCI_RO; | |
959 | ||
b5e89ed5 | 960 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
961 | return 0; |
962 | ||
963 | } | |
9fc5cde7 | 964 | EXPORT_SYMBOL(drm_legacy_addbufs_pci); |
1da177e4 | 965 | |
9fc5cde7 DH |
966 | static int drm_legacy_addbufs_sg(struct drm_device *dev, |
967 | struct drm_buf_desc *request) | |
1da177e4 | 968 | { |
cdd55a29 DA |
969 | struct drm_device_dma *dma = dev->dma; |
970 | struct drm_buf_entry *entry; | |
056219e2 | 971 | struct drm_buf *buf; |
1da177e4 LT |
972 | unsigned long offset; |
973 | unsigned long agp_offset; | |
974 | int count; | |
975 | int order; | |
976 | int size; | |
977 | int alignment; | |
978 | int page_order; | |
979 | int total; | |
980 | int byte_count; | |
981 | int i; | |
056219e2 | 982 | struct drm_buf **temp_buflist; |
1da177e4 | 983 | |
b5e89ed5 DA |
984 | if (!drm_core_check_feature(dev, DRIVER_SG)) |
985 | return -EINVAL; | |
986 | ||
987 | if (!dma) | |
988 | return -EINVAL; | |
1da177e4 | 989 | |
d985c108 DA |
990 | if (!capable(CAP_SYS_ADMIN)) |
991 | return -EPERM; | |
992 | ||
d59431bf | 993 | count = request->count; |
04420c9c | 994 | order = order_base_2(request->size); |
1da177e4 LT |
995 | size = 1 << order; |
996 | ||
b5e89ed5 DA |
997 | alignment = (request->flags & _DRM_PAGE_ALIGN) |
998 | ? PAGE_ALIGN(size) : size; | |
1da177e4 LT |
999 | page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; |
1000 | total = PAGE_SIZE << page_order; | |
1001 | ||
1002 | byte_count = 0; | |
d59431bf | 1003 | agp_offset = request->agp_start; |
1da177e4 | 1004 | |
b5e89ed5 DA |
1005 | DRM_DEBUG("count: %d\n", count); |
1006 | DRM_DEBUG("order: %d\n", order); | |
1007 | DRM_DEBUG("size: %d\n", size); | |
1008 | DRM_DEBUG("agp_offset: %lu\n", agp_offset); | |
1009 | DRM_DEBUG("alignment: %d\n", alignment); | |
1010 | DRM_DEBUG("page_order: %d\n", page_order); | |
1011 | DRM_DEBUG("total: %d\n", total); | |
1da177e4 | 1012 | |
b5e89ed5 DA |
1013 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
1014 | return -EINVAL; | |
1da177e4 | 1015 | |
2177a218 | 1016 | spin_lock(&dev->buf_lock); |
b5e89ed5 | 1017 | if (dev->buf_use) { |
2177a218 | 1018 | spin_unlock(&dev->buf_lock); |
1da177e4 LT |
1019 | return -EBUSY; |
1020 | } | |
b5e89ed5 | 1021 | atomic_inc(&dev->buf_alloc); |
2177a218 | 1022 | spin_unlock(&dev->buf_lock); |
1da177e4 | 1023 | |
30e2fb18 | 1024 | mutex_lock(&dev->struct_mutex); |
1da177e4 | 1025 | entry = &dma->bufs[order]; |
b5e89ed5 | 1026 | if (entry->buf_count) { |
30e2fb18 | 1027 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 DA |
1028 | atomic_dec(&dev->buf_alloc); |
1029 | return -ENOMEM; /* May only call once for each order */ | |
1da177e4 LT |
1030 | } |
1031 | ||
1032 | if (count < 0 || count > 4096) { | |
30e2fb18 | 1033 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 1034 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
1035 | return -EINVAL; |
1036 | } | |
1037 | ||
94e3370e | 1038 | entry->buflist = kzalloc(count * sizeof(*entry->buflist), |
9a298b2a | 1039 | GFP_KERNEL); |
b5e89ed5 | 1040 | if (!entry->buflist) { |
30e2fb18 | 1041 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 1042 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
1043 | return -ENOMEM; |
1044 | } | |
1da177e4 LT |
1045 | |
1046 | entry->buf_size = size; | |
1047 | entry->page_order = page_order; | |
1048 | ||
1049 | offset = 0; | |
1050 | ||
b5e89ed5 DA |
1051 | while (entry->buf_count < count) { |
1052 | buf = &entry->buflist[entry->buf_count]; | |
1053 | buf->idx = dma->buf_count + entry->buf_count; | |
1054 | buf->total = alignment; | |
1055 | buf->order = order; | |
1056 | buf->used = 0; | |
1da177e4 | 1057 | |
b5e89ed5 | 1058 | buf->offset = (dma->byte_count + offset); |
1da177e4 | 1059 | buf->bus_address = agp_offset + offset; |
b5e89ed5 | 1060 | buf->address = (void *)(agp_offset + offset |
d1f2b55a | 1061 | + (unsigned long)dev->sg->virtual); |
b5e89ed5 | 1062 | buf->next = NULL; |
1da177e4 LT |
1063 | buf->waiting = 0; |
1064 | buf->pending = 0; | |
6c340eac | 1065 | buf->file_priv = NULL; |
1da177e4 LT |
1066 | |
1067 | buf->dev_priv_size = dev->driver->dev_priv_size; | |
94e3370e | 1068 | buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL); |
b5e89ed5 | 1069 | if (!buf->dev_private) { |
1da177e4 LT |
1070 | /* Set count correctly so we free the proper amount. */ |
1071 | entry->buf_count = count; | |
b5e89ed5 | 1072 | drm_cleanup_buf_error(dev, entry); |
30e2fb18 | 1073 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 1074 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
1075 | return -ENOMEM; |
1076 | } | |
1077 | ||
b5e89ed5 | 1078 | DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); |
1da177e4 LT |
1079 | |
1080 | offset += alignment; | |
1081 | entry->buf_count++; | |
1082 | byte_count += PAGE_SIZE << page_order; | |
1083 | } | |
1084 | ||
b5e89ed5 | 1085 | DRM_DEBUG("byte_count: %d\n", byte_count); |
1da177e4 | 1086 | |
9a298b2a EA |
1087 | temp_buflist = krealloc(dma->buflist, |
1088 | (dma->buf_count + entry->buf_count) * | |
1089 | sizeof(*dma->buflist), GFP_KERNEL); | |
b5e89ed5 | 1090 | if (!temp_buflist) { |
1da177e4 | 1091 | /* Free the entry because it isn't valid */ |
b5e89ed5 | 1092 | drm_cleanup_buf_error(dev, entry); |
30e2fb18 | 1093 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 1094 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
1095 | return -ENOMEM; |
1096 | } | |
1097 | dma->buflist = temp_buflist; | |
1098 | ||
b5e89ed5 | 1099 | for (i = 0; i < entry->buf_count; i++) { |
1da177e4 LT |
1100 | dma->buflist[i + dma->buf_count] = &entry->buflist[i]; |
1101 | } | |
1102 | ||
1103 | dma->buf_count += entry->buf_count; | |
d985c108 DA |
1104 | dma->seg_count += entry->seg_count; |
1105 | dma->page_count += byte_count >> PAGE_SHIFT; | |
1da177e4 LT |
1106 | dma->byte_count += byte_count; |
1107 | ||
b5e89ed5 DA |
1108 | DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); |
1109 | DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); | |
1da177e4 | 1110 | |
30e2fb18 | 1111 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 1112 | |
d59431bf DA |
1113 | request->count = entry->buf_count; |
1114 | request->size = size; | |
1da177e4 LT |
1115 | |
1116 | dma->flags = _DRM_DMA_USE_SG; | |
1117 | ||
b5e89ed5 | 1118 | atomic_dec(&dev->buf_alloc); |
1da177e4 LT |
1119 | return 0; |
1120 | } | |
1121 | ||
1122 | /** | |
1123 | * Add buffers for DMA transfers (ioctl). | |
1124 | * | |
1125 | * \param inode device inode. | |
6c340eac | 1126 | * \param file_priv DRM file private. |
1da177e4 | 1127 | * \param cmd command. |
c60ce623 | 1128 | * \param arg pointer to a struct drm_buf_desc request. |
1da177e4 LT |
1129 | * \return zero on success or a negative number on failure. |
1130 | * | |
1131 | * According with the memory type specified in drm_buf_desc::flags and the | |
1132 | * build options, it dispatches the call either to addbufs_agp(), | |
1133 | * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent | |
1134 | * PCI memory respectively. | |
1135 | */ | |
9fc5cde7 DH |
1136 | int drm_legacy_addbufs(struct drm_device *dev, void *data, |
1137 | struct drm_file *file_priv) | |
1da177e4 | 1138 | { |
c153f45f | 1139 | struct drm_buf_desc *request = data; |
d59431bf | 1140 | int ret; |
b5e89ed5 | 1141 | |
8d38c4b4 DV |
1142 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
1143 | return -EINVAL; | |
1144 | ||
1da177e4 LT |
1145 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
1146 | return -EINVAL; | |
1147 | ||
a7fb8a23 | 1148 | #if IS_ENABLED(CONFIG_AGP) |
c153f45f | 1149 | if (request->flags & _DRM_AGP_BUFFER) |
9fc5cde7 | 1150 | ret = drm_legacy_addbufs_agp(dev, request); |
1da177e4 LT |
1151 | else |
1152 | #endif | |
c153f45f | 1153 | if (request->flags & _DRM_SG_BUFFER) |
9fc5cde7 | 1154 | ret = drm_legacy_addbufs_sg(dev, request); |
c153f45f | 1155 | else if (request->flags & _DRM_FB_BUFFER) |
687fbb2e | 1156 | ret = -EINVAL; |
1da177e4 | 1157 | else |
9fc5cde7 | 1158 | ret = drm_legacy_addbufs_pci(dev, request); |
d59431bf | 1159 | |
d59431bf | 1160 | return ret; |
1da177e4 LT |
1161 | } |
1162 | ||
1da177e4 LT |
1163 | /** |
1164 | * Get information about the buffer mappings. | |
1165 | * | |
1166 | * This was originally mean for debugging purposes, or by a sophisticated | |
1167 | * client library to determine how best to use the available buffers (e.g., | |
1168 | * large buffers can be used for image transfer). | |
1169 | * | |
1170 | * \param inode device inode. | |
6c340eac | 1171 | * \param file_priv DRM file private. |
1da177e4 LT |
1172 | * \param cmd command. |
1173 | * \param arg pointer to a drm_buf_info structure. | |
1174 | * \return zero on success or a negative number on failure. | |
1175 | * | |
2177a218 | 1176 | * Increments drm_device::buf_use while holding the drm_device::buf_lock |
1da177e4 LT |
1177 | * lock, preventing of allocating more buffers after this call. Information |
1178 | * about each requested buffer is then copied into user space. | |
1179 | */ | |
9fc5cde7 DH |
1180 | int drm_legacy_infobufs(struct drm_device *dev, void *data, |
1181 | struct drm_file *file_priv) | |
1da177e4 | 1182 | { |
cdd55a29 | 1183 | struct drm_device_dma *dma = dev->dma; |
c153f45f | 1184 | struct drm_buf_info *request = data; |
1da177e4 LT |
1185 | int i; |
1186 | int count; | |
1187 | ||
8d38c4b4 DV |
1188 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
1189 | return -EINVAL; | |
1190 | ||
1da177e4 LT |
1191 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
1192 | return -EINVAL; | |
1193 | ||
b5e89ed5 DA |
1194 | if (!dma) |
1195 | return -EINVAL; | |
1da177e4 | 1196 | |
2177a218 | 1197 | spin_lock(&dev->buf_lock); |
b5e89ed5 | 1198 | if (atomic_read(&dev->buf_alloc)) { |
2177a218 | 1199 | spin_unlock(&dev->buf_lock); |
1da177e4 LT |
1200 | return -EBUSY; |
1201 | } | |
1202 | ++dev->buf_use; /* Can't allocate more after this call */ | |
2177a218 | 1203 | spin_unlock(&dev->buf_lock); |
1da177e4 | 1204 | |
b5e89ed5 DA |
1205 | for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { |
1206 | if (dma->bufs[i].buf_count) | |
1207 | ++count; | |
1da177e4 LT |
1208 | } |
1209 | ||
b5e89ed5 | 1210 | DRM_DEBUG("count = %d\n", count); |
1da177e4 | 1211 | |
c153f45f | 1212 | if (request->count >= count) { |
b5e89ed5 DA |
1213 | for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { |
1214 | if (dma->bufs[i].buf_count) { | |
c60ce623 | 1215 | struct drm_buf_desc __user *to = |
c153f45f | 1216 | &request->list[count]; |
cdd55a29 | 1217 | struct drm_buf_entry *from = &dma->bufs[i]; |
b5e89ed5 DA |
1218 | if (copy_to_user(&to->count, |
1219 | &from->buf_count, | |
1220 | sizeof(from->buf_count)) || | |
1221 | copy_to_user(&to->size, | |
1222 | &from->buf_size, | |
1223 | sizeof(from->buf_size)) || | |
1224 | copy_to_user(&to->low_mark, | |
b008c0fc DH |
1225 | &from->low_mark, |
1226 | sizeof(from->low_mark)) || | |
b5e89ed5 | 1227 | copy_to_user(&to->high_mark, |
b008c0fc DH |
1228 | &from->high_mark, |
1229 | sizeof(from->high_mark))) | |
1da177e4 LT |
1230 | return -EFAULT; |
1231 | ||
b5e89ed5 DA |
1232 | DRM_DEBUG("%d %d %d %d %d\n", |
1233 | i, | |
1234 | dma->bufs[i].buf_count, | |
1235 | dma->bufs[i].buf_size, | |
b008c0fc DH |
1236 | dma->bufs[i].low_mark, |
1237 | dma->bufs[i].high_mark); | |
1da177e4 LT |
1238 | ++count; |
1239 | } | |
1240 | } | |
1241 | } | |
c153f45f | 1242 | request->count = count; |
1da177e4 LT |
1243 | |
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | /** | |
1248 | * Specifies a low and high water mark for buffer allocation | |
1249 | * | |
1250 | * \param inode device inode. | |
6c340eac | 1251 | * \param file_priv DRM file private. |
1da177e4 LT |
1252 | * \param cmd command. |
1253 | * \param arg a pointer to a drm_buf_desc structure. | |
1254 | * \return zero on success or a negative number on failure. | |
1255 | * | |
1256 | * Verifies that the size order is bounded between the admissible orders and | |
1257 | * updates the respective drm_device_dma::bufs entry low and high water mark. | |
1258 | * | |
1259 | * \note This ioctl is deprecated and mostly never used. | |
1260 | */ | |
9fc5cde7 DH |
1261 | int drm_legacy_markbufs(struct drm_device *dev, void *data, |
1262 | struct drm_file *file_priv) | |
1da177e4 | 1263 | { |
cdd55a29 | 1264 | struct drm_device_dma *dma = dev->dma; |
c153f45f | 1265 | struct drm_buf_desc *request = data; |
1da177e4 | 1266 | int order; |
cdd55a29 | 1267 | struct drm_buf_entry *entry; |
1da177e4 | 1268 | |
8d38c4b4 DV |
1269 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
1270 | return -EINVAL; | |
1271 | ||
1da177e4 LT |
1272 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
1273 | return -EINVAL; | |
1274 | ||
b5e89ed5 DA |
1275 | if (!dma) |
1276 | return -EINVAL; | |
1da177e4 | 1277 | |
b5e89ed5 | 1278 | DRM_DEBUG("%d, %d, %d\n", |
c153f45f | 1279 | request->size, request->low_mark, request->high_mark); |
04420c9c | 1280 | order = order_base_2(request->size); |
b5e89ed5 DA |
1281 | if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) |
1282 | return -EINVAL; | |
1da177e4 LT |
1283 | entry = &dma->bufs[order]; |
1284 | ||
c153f45f | 1285 | if (request->low_mark < 0 || request->low_mark > entry->buf_count) |
1da177e4 | 1286 | return -EINVAL; |
c153f45f | 1287 | if (request->high_mark < 0 || request->high_mark > entry->buf_count) |
1da177e4 LT |
1288 | return -EINVAL; |
1289 | ||
b008c0fc DH |
1290 | entry->low_mark = request->low_mark; |
1291 | entry->high_mark = request->high_mark; | |
1da177e4 LT |
1292 | |
1293 | return 0; | |
1294 | } | |
1295 | ||
1296 | /** | |
b5e89ed5 | 1297 | * Unreserve the buffers in list, previously reserved using drmDMA. |
1da177e4 LT |
1298 | * |
1299 | * \param inode device inode. | |
6c340eac | 1300 | * \param file_priv DRM file private. |
1da177e4 LT |
1301 | * \param cmd command. |
1302 | * \param arg pointer to a drm_buf_free structure. | |
1303 | * \return zero on success or a negative number on failure. | |
b5e89ed5 | 1304 | * |
1da177e4 LT |
1305 | * Calls free_buffer() for each used buffer. |
1306 | * This function is primarily used for debugging. | |
1307 | */ | |
9fc5cde7 DH |
1308 | int drm_legacy_freebufs(struct drm_device *dev, void *data, |
1309 | struct drm_file *file_priv) | |
1da177e4 | 1310 | { |
cdd55a29 | 1311 | struct drm_device_dma *dma = dev->dma; |
c153f45f | 1312 | struct drm_buf_free *request = data; |
1da177e4 LT |
1313 | int i; |
1314 | int idx; | |
056219e2 | 1315 | struct drm_buf *buf; |
1da177e4 | 1316 | |
8d38c4b4 DV |
1317 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
1318 | return -EINVAL; | |
1319 | ||
1da177e4 LT |
1320 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
1321 | return -EINVAL; | |
1322 | ||
b5e89ed5 DA |
1323 | if (!dma) |
1324 | return -EINVAL; | |
1da177e4 | 1325 | |
c153f45f EA |
1326 | DRM_DEBUG("%d\n", request->count); |
1327 | for (i = 0; i < request->count; i++) { | |
1328 | if (copy_from_user(&idx, &request->list[i], sizeof(idx))) | |
1da177e4 | 1329 | return -EFAULT; |
b5e89ed5 DA |
1330 | if (idx < 0 || idx >= dma->buf_count) { |
1331 | DRM_ERROR("Index %d (of %d max)\n", | |
1332 | idx, dma->buf_count - 1); | |
1da177e4 LT |
1333 | return -EINVAL; |
1334 | } | |
1335 | buf = dma->buflist[idx]; | |
6c340eac | 1336 | if (buf->file_priv != file_priv) { |
b5e89ed5 | 1337 | DRM_ERROR("Process %d freeing buffer not owned\n", |
ba25f9dc | 1338 | task_pid_nr(current)); |
1da177e4 LT |
1339 | return -EINVAL; |
1340 | } | |
a266162a | 1341 | drm_legacy_free_buffer(dev, buf); |
1da177e4 LT |
1342 | } |
1343 | ||
1344 | return 0; | |
1345 | } | |
1346 | ||
1347 | /** | |
1348 | * Maps all of the DMA buffers into client-virtual space (ioctl). | |
1349 | * | |
1350 | * \param inode device inode. | |
6c340eac | 1351 | * \param file_priv DRM file private. |
1da177e4 LT |
1352 | * \param cmd command. |
1353 | * \param arg pointer to a drm_buf_map structure. | |
1354 | * \return zero on success or a negative number on failure. | |
1355 | * | |
6be5ceb0 LT |
1356 | * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information |
1357 | * about each buffer into user space. For PCI buffers, it calls vm_mmap() with | |
3417f33e GS |
1358 | * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls |
1359 | * drm_mmap_dma(). | |
1da177e4 | 1360 | */ |
9fc5cde7 DH |
1361 | int drm_legacy_mapbufs(struct drm_device *dev, void *data, |
1362 | struct drm_file *file_priv) | |
1da177e4 | 1363 | { |
cdd55a29 | 1364 | struct drm_device_dma *dma = dev->dma; |
1da177e4 LT |
1365 | int retcode = 0; |
1366 | const int zero = 0; | |
1367 | unsigned long virtual; | |
1368 | unsigned long address; | |
c153f45f | 1369 | struct drm_buf_map *request = data; |
1da177e4 LT |
1370 | int i; |
1371 | ||
8d38c4b4 DV |
1372 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
1373 | return -EINVAL; | |
1374 | ||
1da177e4 LT |
1375 | if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
1376 | return -EINVAL; | |
1377 | ||
b5e89ed5 DA |
1378 | if (!dma) |
1379 | return -EINVAL; | |
1da177e4 | 1380 | |
2177a218 | 1381 | spin_lock(&dev->buf_lock); |
b5e89ed5 | 1382 | if (atomic_read(&dev->buf_alloc)) { |
2177a218 | 1383 | spin_unlock(&dev->buf_lock); |
1da177e4 LT |
1384 | return -EBUSY; |
1385 | } | |
1386 | dev->buf_use++; /* Can't allocate more after this call */ | |
2177a218 | 1387 | spin_unlock(&dev->buf_lock); |
1da177e4 | 1388 | |
c153f45f | 1389 | if (request->count >= dma->buf_count) { |
d9906753 | 1390 | if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP)) |
b5e89ed5 | 1391 | || (drm_core_check_feature(dev, DRIVER_SG) |
687fbb2e | 1392 | && (dma->flags & _DRM_DMA_USE_SG))) { |
f77d390c | 1393 | struct drm_local_map *map = dev->agp_buffer_map; |
d1f2b55a | 1394 | unsigned long token = dev->agp_buffer_token; |
1da177e4 | 1395 | |
b5e89ed5 | 1396 | if (!map) { |
1da177e4 LT |
1397 | retcode = -EINVAL; |
1398 | goto done; | |
1399 | } | |
6be5ceb0 | 1400 | virtual = vm_mmap(file_priv->filp, 0, map->size, |
b5e89ed5 | 1401 | PROT_READ | PROT_WRITE, |
c153f45f EA |
1402 | MAP_SHARED, |
1403 | token); | |
1da177e4 | 1404 | } else { |
6be5ceb0 | 1405 | virtual = vm_mmap(file_priv->filp, 0, dma->byte_count, |
b5e89ed5 DA |
1406 | PROT_READ | PROT_WRITE, |
1407 | MAP_SHARED, 0); | |
1da177e4 | 1408 | } |
b5e89ed5 | 1409 | if (virtual > -1024UL) { |
1da177e4 LT |
1410 | /* Real error */ |
1411 | retcode = (signed long)virtual; | |
1412 | goto done; | |
1413 | } | |
c153f45f | 1414 | request->virtual = (void __user *)virtual; |
1da177e4 | 1415 | |
b5e89ed5 | 1416 | for (i = 0; i < dma->buf_count; i++) { |
c153f45f | 1417 | if (copy_to_user(&request->list[i].idx, |
b5e89ed5 | 1418 | &dma->buflist[i]->idx, |
c153f45f | 1419 | sizeof(request->list[0].idx))) { |
1da177e4 LT |
1420 | retcode = -EFAULT; |
1421 | goto done; | |
1422 | } | |
c153f45f | 1423 | if (copy_to_user(&request->list[i].total, |
b5e89ed5 | 1424 | &dma->buflist[i]->total, |
c153f45f | 1425 | sizeof(request->list[0].total))) { |
1da177e4 LT |
1426 | retcode = -EFAULT; |
1427 | goto done; | |
1428 | } | |
c153f45f | 1429 | if (copy_to_user(&request->list[i].used, |
b5e89ed5 | 1430 | &zero, sizeof(zero))) { |
1da177e4 LT |
1431 | retcode = -EFAULT; |
1432 | goto done; | |
1433 | } | |
b5e89ed5 | 1434 | address = virtual + dma->buflist[i]->offset; /* *** */ |
c153f45f | 1435 | if (copy_to_user(&request->list[i].address, |
b5e89ed5 | 1436 | &address, sizeof(address))) { |
1da177e4 LT |
1437 | retcode = -EFAULT; |
1438 | goto done; | |
1439 | } | |
1440 | } | |
1441 | } | |
b5e89ed5 | 1442 | done: |
c153f45f EA |
1443 | request->count = dma->buf_count; |
1444 | DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode); | |
1da177e4 LT |
1445 | |
1446 | return retcode; | |
1447 | } | |
1448 | ||
9fc5cde7 | 1449 | int drm_legacy_dma_ioctl(struct drm_device *dev, void *data, |
6eb9278a DV |
1450 | struct drm_file *file_priv) |
1451 | { | |
1452 | if (drm_core_check_feature(dev, DRIVER_MODESET)) | |
1453 | return -EINVAL; | |
1454 | ||
1455 | if (dev->driver->dma_ioctl) | |
1456 | return dev->driver->dma_ioctl(dev, data, file_priv); | |
1457 | else | |
1458 | return -EINVAL; | |
1459 | } | |
1460 | ||
9fc5cde7 | 1461 | struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev) |
bd0c0cee DV |
1462 | { |
1463 | struct drm_map_list *entry; | |
1464 | ||
1465 | list_for_each_entry(entry, &dev->maplist, head) { | |
1466 | if (entry->map && entry->map->type == _DRM_SHM && | |
1467 | (entry->map->flags & _DRM_CONTAINS_LOCK)) { | |
1468 | return entry->map; | |
1469 | } | |
1470 | } | |
1471 | return NULL; | |
1472 | } | |
9fc5cde7 | 1473 | EXPORT_SYMBOL(drm_legacy_getsarea); |