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