drm: Push struct_mutex into ->master_destroy
[deliverable/linux.git] / drivers / gpu / drm / drm_bufs.c
CommitLineData
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 39static 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 76static 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 132static 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
364int 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 376EXPORT_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
389int 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
ec1f52ef
DV
419/*
420 * Get a mapping information.
421 *
422 * \param inode device inode.
423 * \param file_priv DRM file private.
424 * \param cmd command.
425 * \param arg user argument, pointing to a drm_map structure.
426 *
427 * \return zero on success or a negative number on failure.
428 *
429 * Searches for the mapping with the specified offset and copies its information
430 * into userspace
431 */
432int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
433 struct drm_file *file_priv)
434{
435 struct drm_map *map = data;
436 struct drm_map_list *r_list = NULL;
437 struct list_head *list;
438 int idx;
439 int i;
440
441 idx = map->offset;
442 if (idx < 0)
443 return -EINVAL;
444
445 i = 0;
446 mutex_lock(&dev->struct_mutex);
447 list_for_each(list, &dev->maplist) {
448 if (i == idx) {
449 r_list = list_entry(list, struct drm_map_list, head);
450 break;
451 }
452 i++;
453 }
454 if (!r_list || !r_list->map) {
455 mutex_unlock(&dev->struct_mutex);
456 return -EINVAL;
457 }
458
459 map->offset = r_list->map->offset;
460 map->size = r_list->map->size;
461 map->type = r_list->map->type;
462 map->flags = r_list->map->flags;
463 map->handle = (void *)(unsigned long) r_list->user_token;
464 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
465
466 mutex_unlock(&dev->struct_mutex);
467
468 return 0;
469}
470
1da177e4
LT
471/**
472 * Remove a map private from list and deallocate resources if the mapping
473 * isn't in use.
474 *
1da177e4
LT
475 * Searches the map on drm_device::maplist, removes it from the list, see if
476 * its being used, and free any associate resource (such as MTRR's) if it's not
477 * being on use.
478 *
9fc5cde7 479 * \sa drm_legacy_addmap
1da177e4 480 */
9fc5cde7 481int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
1da177e4 482{
55910517 483 struct drm_map_list *r_list = NULL, *list_t;
836cf046 484 drm_dma_handle_t dmah;
bd1b331f 485 int found = 0;
7c1c2871 486 struct drm_master *master;
1da177e4 487
836cf046 488 /* Find the list entry for the map and remove it */
bd1b331f 489 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
836cf046 490 if (r_list->map == map) {
7c1c2871 491 master = r_list->master;
bd1b331f 492 list_del(&r_list->head);
1545085a
TH
493 drm_ht_remove_key(&dev->map_hash,
494 r_list->user_token >> PAGE_SHIFT);
9a298b2a 495 kfree(r_list);
bd1b331f 496 found = 1;
836cf046
DA
497 break;
498 }
1da177e4
LT
499 }
500
bd1b331f 501 if (!found)
1da177e4 502 return -EINVAL;
1da177e4 503
836cf046
DA
504 switch (map->type) {
505 case _DRM_REGISTERS:
004a7727 506 iounmap(map->handle);
836cf046
DA
507 /* FALLTHROUGH */
508 case _DRM_FRAME_BUFFER:
28185647 509 arch_phys_wc_del(map->mtrr);
836cf046
DA
510 break;
511 case _DRM_SHM:
512 vfree(map->handle);
7c1c2871
DA
513 if (master) {
514 if (dev->sigdata.lock == master->lock.hw_lock)
515 dev->sigdata.lock = NULL;
516 master->lock.hw_lock = NULL; /* SHM removed */
517 master->lock.file_priv = NULL;
171901d1 518 wake_up_interruptible_all(&master->lock.lock_queue);
7c1c2871 519 }
836cf046
DA
520 break;
521 case _DRM_AGP:
522 case _DRM_SCATTER_GATHER:
523 break;
524 case _DRM_CONSISTENT:
525 dmah.vaddr = map->handle;
526 dmah.busaddr = map->offset;
527 dmah.size = map->size;
1c96e84e 528 __drm_legacy_pci_free(dev, &dmah);
836cf046 529 break;
1da177e4 530 }
9a298b2a 531 kfree(map);
836cf046 532
1da177e4
LT
533 return 0;
534}
9fc5cde7 535EXPORT_SYMBOL(drm_legacy_rmmap_locked);
836cf046 536
9fc5cde7 537int drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
836cf046
DA
538{
539 int ret;
540
30e2fb18 541 mutex_lock(&dev->struct_mutex);
9fc5cde7 542 ret = drm_legacy_rmmap_locked(dev, map);
30e2fb18 543 mutex_unlock(&dev->struct_mutex);
836cf046
DA
544
545 return ret;
546}
9fc5cde7 547EXPORT_SYMBOL(drm_legacy_rmmap);
7ab98401 548
836cf046
DA
549/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
550 * the last close of the device, and this is necessary for cleanup when things
551 * exit uncleanly. Therefore, having userland manually remove mappings seems
552 * like a pointless exercise since they're going away anyway.
553 *
554 * One use case might be after addmap is allowed for normal users for SHM and
555 * gets used by drivers that the server doesn't need to care about. This seems
556 * unlikely.
f77d390c
BH
557 *
558 * \param inode device inode.
559 * \param file_priv DRM file private.
560 * \param cmd command.
561 * \param arg pointer to a struct drm_map structure.
562 * \return zero on success or a negative value on error.
836cf046 563 */
9fc5cde7
DH
564int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
565 struct drm_file *file_priv)
7ab98401 566{
c153f45f 567 struct drm_map *request = data;
f77d390c 568 struct drm_local_map *map = NULL;
55910517 569 struct drm_map_list *r_list;
836cf046 570 int ret;
7ab98401 571
30e2fb18 572 mutex_lock(&dev->struct_mutex);
bd1b331f 573 list_for_each_entry(r_list, &dev->maplist, head) {
836cf046 574 if (r_list->map &&
c153f45f 575 r_list->user_token == (unsigned long)request->handle &&
836cf046
DA
576 r_list->map->flags & _DRM_REMOVABLE) {
577 map = r_list->map;
578 break;
579 }
580 }
581
582 /* List has wrapped around to the head pointer, or its empty we didn't
583 * find anything.
584 */
bd1b331f 585 if (list_empty(&dev->maplist) || !map) {
30e2fb18 586 mutex_unlock(&dev->struct_mutex);
836cf046
DA
587 return -EINVAL;
588 }
589
836cf046
DA
590 /* Register and framebuffer maps are permanent */
591 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
30e2fb18 592 mutex_unlock(&dev->struct_mutex);
836cf046
DA
593 return 0;
594 }
595
9fc5cde7 596 ret = drm_legacy_rmmap_locked(dev, map);
836cf046 597
30e2fb18 598 mutex_unlock(&dev->struct_mutex);
836cf046
DA
599
600 return ret;
7ab98401 601}
1da177e4
LT
602
603/**
604 * Cleanup after an error on one of the addbufs() functions.
605 *
836cf046 606 * \param dev DRM device.
1da177e4
LT
607 * \param entry buffer entry where the error occurred.
608 *
609 * Frees any pages and buffers associated with the given entry.
610 */
cdd55a29
DA
611static void drm_cleanup_buf_error(struct drm_device * dev,
612 struct drm_buf_entry * entry)
1da177e4
LT
613{
614 int i;
615
616 if (entry->seg_count) {
617 for (i = 0; i < entry->seg_count; i++) {
618 if (entry->seglist[i]) {
ddf19b97 619 drm_pci_free(dev, entry->seglist[i]);
1da177e4
LT
620 }
621 }
9a298b2a 622 kfree(entry->seglist);
1da177e4
LT
623
624 entry->seg_count = 0;
625 }
626
b5e89ed5
DA
627 if (entry->buf_count) {
628 for (i = 0; i < entry->buf_count; i++) {
9a298b2a 629 kfree(entry->buflist[i].dev_private);
1da177e4 630 }
9a298b2a 631 kfree(entry->buflist);
1da177e4
LT
632
633 entry->buf_count = 0;
634 }
635}
636
a7fb8a23 637#if IS_ENABLED(CONFIG_AGP)
1da177e4 638/**
d59431bf 639 * Add AGP buffers for DMA transfers.
1da177e4 640 *
84b1fd10 641 * \param dev struct drm_device to which the buffers are to be added.
c60ce623 642 * \param request pointer to a struct drm_buf_desc describing the request.
1da177e4 643 * \return zero on success or a negative number on failure.
b5e89ed5 644 *
1da177e4
LT
645 * After some sanity checks creates a drm_buf structure for each buffer and
646 * reallocates the buffer list of the same size order to accommodate the new
647 * buffers.
648 */
9fc5cde7
DH
649int drm_legacy_addbufs_agp(struct drm_device *dev,
650 struct drm_buf_desc *request)
1da177e4 651{
cdd55a29
DA
652 struct drm_device_dma *dma = dev->dma;
653 struct drm_buf_entry *entry;
55910517 654 struct drm_agp_mem *agp_entry;
056219e2 655 struct drm_buf *buf;
1da177e4
LT
656 unsigned long offset;
657 unsigned long agp_offset;
658 int count;
659 int order;
660 int size;
661 int alignment;
662 int page_order;
663 int total;
664 int byte_count;
54ba2f76 665 int i, valid;
056219e2 666 struct drm_buf **temp_buflist;
1da177e4 667
b5e89ed5
DA
668 if (!dma)
669 return -EINVAL;
1da177e4 670
d59431bf 671 count = request->count;
04420c9c 672 order = order_base_2(request->size);
1da177e4
LT
673 size = 1 << order;
674
b5e89ed5
DA
675 alignment = (request->flags & _DRM_PAGE_ALIGN)
676 ? PAGE_ALIGN(size) : size;
1da177e4
LT
677 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
678 total = PAGE_SIZE << page_order;
679
680 byte_count = 0;
d59431bf 681 agp_offset = dev->agp->base + request->agp_start;
1da177e4 682
b5e89ed5
DA
683 DRM_DEBUG("count: %d\n", count);
684 DRM_DEBUG("order: %d\n", order);
685 DRM_DEBUG("size: %d\n", size);
d985c108 686 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
b5e89ed5
DA
687 DRM_DEBUG("alignment: %d\n", alignment);
688 DRM_DEBUG("page_order: %d\n", page_order);
689 DRM_DEBUG("total: %d\n", total);
1da177e4 690
b5e89ed5
DA
691 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
692 return -EINVAL;
1da177e4 693
54ba2f76
DA
694 /* Make sure buffers are located in AGP memory that we own */
695 valid = 0;
bd1b331f 696 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
54ba2f76
DA
697 if ((agp_offset >= agp_entry->bound) &&
698 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
699 valid = 1;
700 break;
701 }
702 }
bd1b331f 703 if (!list_empty(&dev->agp->memory) && !valid) {
54ba2f76
DA
704 DRM_DEBUG("zone invalid\n");
705 return -EINVAL;
706 }
2177a218 707 spin_lock(&dev->buf_lock);
b5e89ed5 708 if (dev->buf_use) {
2177a218 709 spin_unlock(&dev->buf_lock);
1da177e4
LT
710 return -EBUSY;
711 }
b5e89ed5 712 atomic_inc(&dev->buf_alloc);
2177a218 713 spin_unlock(&dev->buf_lock);
1da177e4 714
30e2fb18 715 mutex_lock(&dev->struct_mutex);
1da177e4 716 entry = &dma->bufs[order];
b5e89ed5 717 if (entry->buf_count) {
30e2fb18 718 mutex_unlock(&dev->struct_mutex);
b5e89ed5
DA
719 atomic_dec(&dev->buf_alloc);
720 return -ENOMEM; /* May only call once for each order */
1da177e4
LT
721 }
722
723 if (count < 0 || count > 4096) {
30e2fb18 724 mutex_unlock(&dev->struct_mutex);
b5e89ed5 725 atomic_dec(&dev->buf_alloc);
1da177e4
LT
726 return -EINVAL;
727 }
728
94e3370e 729 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
b5e89ed5 730 if (!entry->buflist) {
30e2fb18 731 mutex_unlock(&dev->struct_mutex);
b5e89ed5 732 atomic_dec(&dev->buf_alloc);
1da177e4
LT
733 return -ENOMEM;
734 }
1da177e4
LT
735
736 entry->buf_size = size;
737 entry->page_order = page_order;
738
739 offset = 0;
740
b5e89ed5
DA
741 while (entry->buf_count < count) {
742 buf = &entry->buflist[entry->buf_count];
743 buf->idx = dma->buf_count + entry->buf_count;
744 buf->total = alignment;
745 buf->order = order;
746 buf->used = 0;
1da177e4 747
b5e89ed5 748 buf->offset = (dma->byte_count + offset);
1da177e4
LT
749 buf->bus_address = agp_offset + offset;
750 buf->address = (void *)(agp_offset + offset);
b5e89ed5 751 buf->next = NULL;
1da177e4
LT
752 buf->waiting = 0;
753 buf->pending = 0;
6c340eac 754 buf->file_priv = NULL;
1da177e4
LT
755
756 buf->dev_priv_size = dev->driver->dev_priv_size;
94e3370e 757 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
b5e89ed5 758 if (!buf->dev_private) {
1da177e4
LT
759 /* Set count correctly so we free the proper amount. */
760 entry->buf_count = count;
b5e89ed5 761 drm_cleanup_buf_error(dev, entry);
30e2fb18 762 mutex_unlock(&dev->struct_mutex);
b5e89ed5 763 atomic_dec(&dev->buf_alloc);
1da177e4
LT
764 return -ENOMEM;
765 }
1da177e4 766
b5e89ed5 767 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1da177e4
LT
768
769 offset += alignment;
770 entry->buf_count++;
771 byte_count += PAGE_SIZE << page_order;
772 }
773
b5e89ed5 774 DRM_DEBUG("byte_count: %d\n", byte_count);
1da177e4 775
9a298b2a
EA
776 temp_buflist = krealloc(dma->buflist,
777 (dma->buf_count + entry->buf_count) *
778 sizeof(*dma->buflist), GFP_KERNEL);
b5e89ed5 779 if (!temp_buflist) {
1da177e4 780 /* Free the entry because it isn't valid */
b5e89ed5 781 drm_cleanup_buf_error(dev, entry);
30e2fb18 782 mutex_unlock(&dev->struct_mutex);
b5e89ed5 783 atomic_dec(&dev->buf_alloc);
1da177e4
LT
784 return -ENOMEM;
785 }
786 dma->buflist = temp_buflist;
787
b5e89ed5 788 for (i = 0; i < entry->buf_count; i++) {
1da177e4
LT
789 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
790 }
791
792 dma->buf_count += entry->buf_count;
d985c108
DA
793 dma->seg_count += entry->seg_count;
794 dma->page_count += byte_count >> PAGE_SHIFT;
1da177e4
LT
795 dma->byte_count += byte_count;
796
b5e89ed5
DA
797 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
798 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1da177e4 799
30e2fb18 800 mutex_unlock(&dev->struct_mutex);
1da177e4 801
d59431bf
DA
802 request->count = entry->buf_count;
803 request->size = size;
1da177e4
LT
804
805 dma->flags = _DRM_DMA_USE_AGP;
806
b5e89ed5 807 atomic_dec(&dev->buf_alloc);
1da177e4
LT
808 return 0;
809}
9fc5cde7 810EXPORT_SYMBOL(drm_legacy_addbufs_agp);
a7fb8a23 811#endif /* CONFIG_AGP */
1da177e4 812
9fc5cde7
DH
813int drm_legacy_addbufs_pci(struct drm_device *dev,
814 struct drm_buf_desc *request)
1da177e4 815{
cdd55a29 816 struct drm_device_dma *dma = dev->dma;
1da177e4
LT
817 int count;
818 int order;
819 int size;
820 int total;
821 int page_order;
cdd55a29 822 struct drm_buf_entry *entry;
ddf19b97 823 drm_dma_handle_t *dmah;
056219e2 824 struct drm_buf *buf;
1da177e4
LT
825 int alignment;
826 unsigned long offset;
827 int i;
828 int byte_count;
829 int page_count;
830 unsigned long *temp_pagelist;
056219e2 831 struct drm_buf **temp_buflist;
1da177e4 832
b5e89ed5
DA
833 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
834 return -EINVAL;
d985c108 835
b5e89ed5
DA
836 if (!dma)
837 return -EINVAL;
1da177e4 838
d985c108
DA
839 if (!capable(CAP_SYS_ADMIN))
840 return -EPERM;
841
d59431bf 842 count = request->count;
04420c9c 843 order = order_base_2(request->size);
1da177e4
LT
844 size = 1 << order;
845
a344a7e7
DV
846 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
847 request->count, request->size, size, order);
1da177e4 848
b5e89ed5
DA
849 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
850 return -EINVAL;
1da177e4 851
d59431bf 852 alignment = (request->flags & _DRM_PAGE_ALIGN)
b5e89ed5 853 ? PAGE_ALIGN(size) : size;
1da177e4
LT
854 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
855 total = PAGE_SIZE << page_order;
856
2177a218 857 spin_lock(&dev->buf_lock);
b5e89ed5 858 if (dev->buf_use) {
2177a218 859 spin_unlock(&dev->buf_lock);
1da177e4
LT
860 return -EBUSY;
861 }
b5e89ed5 862 atomic_inc(&dev->buf_alloc);
2177a218 863 spin_unlock(&dev->buf_lock);
1da177e4 864
30e2fb18 865 mutex_lock(&dev->struct_mutex);
1da177e4 866 entry = &dma->bufs[order];
b5e89ed5 867 if (entry->buf_count) {
30e2fb18 868 mutex_unlock(&dev->struct_mutex);
b5e89ed5 869 atomic_dec(&dev->buf_alloc);
1da177e4
LT
870 return -ENOMEM; /* May only call once for each order */
871 }
872
873 if (count < 0 || count > 4096) {
30e2fb18 874 mutex_unlock(&dev->struct_mutex);
b5e89ed5 875 atomic_dec(&dev->buf_alloc);
1da177e4
LT
876 return -EINVAL;
877 }
878
94e3370e 879 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
b5e89ed5 880 if (!entry->buflist) {
30e2fb18 881 mutex_unlock(&dev->struct_mutex);
b5e89ed5 882 atomic_dec(&dev->buf_alloc);
1da177e4
LT
883 return -ENOMEM;
884 }
b5e89ed5 885
94e3370e 886 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
b5e89ed5 887 if (!entry->seglist) {
9a298b2a 888 kfree(entry->buflist);
30e2fb18 889 mutex_unlock(&dev->struct_mutex);
b5e89ed5 890 atomic_dec(&dev->buf_alloc);
1da177e4
LT
891 return -ENOMEM;
892 }
1da177e4
LT
893
894 /* Keep the original pagelist until we know all the allocations
895 * have succeeded
896 */
9a298b2a
EA
897 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
898 sizeof(*dma->pagelist), GFP_KERNEL);
1da177e4 899 if (!temp_pagelist) {
9a298b2a
EA
900 kfree(entry->buflist);
901 kfree(entry->seglist);
30e2fb18 902 mutex_unlock(&dev->struct_mutex);
b5e89ed5 903 atomic_dec(&dev->buf_alloc);
1da177e4
LT
904 return -ENOMEM;
905 }
906 memcpy(temp_pagelist,
b5e89ed5
DA
907 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
908 DRM_DEBUG("pagelist: %d entries\n",
909 dma->page_count + (count << page_order));
1da177e4 910
b5e89ed5 911 entry->buf_size = size;
1da177e4
LT
912 entry->page_order = page_order;
913 byte_count = 0;
914 page_count = 0;
915
b5e89ed5 916 while (entry->buf_count < count) {
bc5f4523 917
e6be8d9d 918 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
bc5f4523 919
ddf19b97 920 if (!dmah) {
1da177e4
LT
921 /* Set count correctly so we free the proper amount. */
922 entry->buf_count = count;
923 entry->seg_count = count;
924 drm_cleanup_buf_error(dev, entry);
9a298b2a 925 kfree(temp_pagelist);
30e2fb18 926 mutex_unlock(&dev->struct_mutex);
b5e89ed5 927 atomic_dec(&dev->buf_alloc);
1da177e4
LT
928 return -ENOMEM;
929 }
ddf19b97 930 entry->seglist[entry->seg_count++] = dmah;
b5e89ed5
DA
931 for (i = 0; i < (1 << page_order); i++) {
932 DRM_DEBUG("page %d @ 0x%08lx\n",
933 dma->page_count + page_count,
ddf19b97 934 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
1da177e4 935 temp_pagelist[dma->page_count + page_count++]
ddf19b97 936 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
1da177e4 937 }
b5e89ed5
DA
938 for (offset = 0;
939 offset + size <= total && entry->buf_count < count;
940 offset += alignment, ++entry->buf_count) {
941 buf = &entry->buflist[entry->buf_count];
942 buf->idx = dma->buf_count + entry->buf_count;
943 buf->total = alignment;
944 buf->order = order;
945 buf->used = 0;
946 buf->offset = (dma->byte_count + byte_count + offset);
ddf19b97
DA
947 buf->address = (void *)(dmah->vaddr + offset);
948 buf->bus_address = dmah->busaddr + offset;
b5e89ed5 949 buf->next = NULL;
1da177e4
LT
950 buf->waiting = 0;
951 buf->pending = 0;
6c340eac 952 buf->file_priv = NULL;
1da177e4
LT
953
954 buf->dev_priv_size = dev->driver->dev_priv_size;
94e3370e
DB
955 buf->dev_private = kzalloc(buf->dev_priv_size,
956 GFP_KERNEL);
b5e89ed5 957 if (!buf->dev_private) {
1da177e4
LT
958 /* Set count correctly so we free the proper amount. */
959 entry->buf_count = count;
960 entry->seg_count = count;
b5e89ed5 961 drm_cleanup_buf_error(dev, entry);
9a298b2a 962 kfree(temp_pagelist);
30e2fb18 963 mutex_unlock(&dev->struct_mutex);
b5e89ed5 964 atomic_dec(&dev->buf_alloc);
1da177e4
LT
965 return -ENOMEM;
966 }
1da177e4 967
b5e89ed5
DA
968 DRM_DEBUG("buffer %d @ %p\n",
969 entry->buf_count, buf->address);
1da177e4
LT
970 }
971 byte_count += PAGE_SIZE << page_order;
972 }
973
9a298b2a
EA
974 temp_buflist = krealloc(dma->buflist,
975 (dma->buf_count + entry->buf_count) *
976 sizeof(*dma->buflist), GFP_KERNEL);
1da177e4
LT
977 if (!temp_buflist) {
978 /* Free the entry because it isn't valid */
b5e89ed5 979 drm_cleanup_buf_error(dev, entry);
9a298b2a 980 kfree(temp_pagelist);
30e2fb18 981 mutex_unlock(&dev->struct_mutex);
b5e89ed5 982 atomic_dec(&dev->buf_alloc);
1da177e4
LT
983 return -ENOMEM;
984 }
985 dma->buflist = temp_buflist;
986
b5e89ed5 987 for (i = 0; i < entry->buf_count; i++) {
1da177e4
LT
988 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
989 }
990
88393161 991 /* No allocations failed, so now we can replace the original pagelist
1da177e4
LT
992 * with the new one.
993 */
994 if (dma->page_count) {
9a298b2a 995 kfree(dma->pagelist);
1da177e4
LT
996 }
997 dma->pagelist = temp_pagelist;
998
999 dma->buf_count += entry->buf_count;
1000 dma->seg_count += entry->seg_count;
1001 dma->page_count += entry->seg_count << page_order;
1002 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1003
30e2fb18 1004 mutex_unlock(&dev->struct_mutex);
1da177e4 1005
d59431bf
DA
1006 request->count = entry->buf_count;
1007 request->size = size;
1da177e4 1008
3417f33e
GS
1009 if (request->flags & _DRM_PCI_BUFFER_RO)
1010 dma->flags = _DRM_DMA_USE_PCI_RO;
1011
b5e89ed5 1012 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1013 return 0;
1014
1015}
9fc5cde7 1016EXPORT_SYMBOL(drm_legacy_addbufs_pci);
1da177e4 1017
9fc5cde7
DH
1018static int drm_legacy_addbufs_sg(struct drm_device *dev,
1019 struct drm_buf_desc *request)
1da177e4 1020{
cdd55a29
DA
1021 struct drm_device_dma *dma = dev->dma;
1022 struct drm_buf_entry *entry;
056219e2 1023 struct drm_buf *buf;
1da177e4
LT
1024 unsigned long offset;
1025 unsigned long agp_offset;
1026 int count;
1027 int order;
1028 int size;
1029 int alignment;
1030 int page_order;
1031 int total;
1032 int byte_count;
1033 int i;
056219e2 1034 struct drm_buf **temp_buflist;
1da177e4 1035
b5e89ed5
DA
1036 if (!drm_core_check_feature(dev, DRIVER_SG))
1037 return -EINVAL;
1038
1039 if (!dma)
1040 return -EINVAL;
1da177e4 1041
d985c108
DA
1042 if (!capable(CAP_SYS_ADMIN))
1043 return -EPERM;
1044
d59431bf 1045 count = request->count;
04420c9c 1046 order = order_base_2(request->size);
1da177e4
LT
1047 size = 1 << order;
1048
b5e89ed5
DA
1049 alignment = (request->flags & _DRM_PAGE_ALIGN)
1050 ? PAGE_ALIGN(size) : size;
1da177e4
LT
1051 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1052 total = PAGE_SIZE << page_order;
1053
1054 byte_count = 0;
d59431bf 1055 agp_offset = request->agp_start;
1da177e4 1056
b5e89ed5
DA
1057 DRM_DEBUG("count: %d\n", count);
1058 DRM_DEBUG("order: %d\n", order);
1059 DRM_DEBUG("size: %d\n", size);
1060 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1061 DRM_DEBUG("alignment: %d\n", alignment);
1062 DRM_DEBUG("page_order: %d\n", page_order);
1063 DRM_DEBUG("total: %d\n", total);
1da177e4 1064
b5e89ed5
DA
1065 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1066 return -EINVAL;
1da177e4 1067
2177a218 1068 spin_lock(&dev->buf_lock);
b5e89ed5 1069 if (dev->buf_use) {
2177a218 1070 spin_unlock(&dev->buf_lock);
1da177e4
LT
1071 return -EBUSY;
1072 }
b5e89ed5 1073 atomic_inc(&dev->buf_alloc);
2177a218 1074 spin_unlock(&dev->buf_lock);
1da177e4 1075
30e2fb18 1076 mutex_lock(&dev->struct_mutex);
1da177e4 1077 entry = &dma->bufs[order];
b5e89ed5 1078 if (entry->buf_count) {
30e2fb18 1079 mutex_unlock(&dev->struct_mutex);
b5e89ed5
DA
1080 atomic_dec(&dev->buf_alloc);
1081 return -ENOMEM; /* May only call once for each order */
1da177e4
LT
1082 }
1083
1084 if (count < 0 || count > 4096) {
30e2fb18 1085 mutex_unlock(&dev->struct_mutex);
b5e89ed5 1086 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1087 return -EINVAL;
1088 }
1089
94e3370e 1090 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
9a298b2a 1091 GFP_KERNEL);
b5e89ed5 1092 if (!entry->buflist) {
30e2fb18 1093 mutex_unlock(&dev->struct_mutex);
b5e89ed5 1094 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1095 return -ENOMEM;
1096 }
1da177e4
LT
1097
1098 entry->buf_size = size;
1099 entry->page_order = page_order;
1100
1101 offset = 0;
1102
b5e89ed5
DA
1103 while (entry->buf_count < count) {
1104 buf = &entry->buflist[entry->buf_count];
1105 buf->idx = dma->buf_count + entry->buf_count;
1106 buf->total = alignment;
1107 buf->order = order;
1108 buf->used = 0;
1da177e4 1109
b5e89ed5 1110 buf->offset = (dma->byte_count + offset);
1da177e4 1111 buf->bus_address = agp_offset + offset;
b5e89ed5 1112 buf->address = (void *)(agp_offset + offset
d1f2b55a 1113 + (unsigned long)dev->sg->virtual);
b5e89ed5 1114 buf->next = NULL;
1da177e4
LT
1115 buf->waiting = 0;
1116 buf->pending = 0;
6c340eac 1117 buf->file_priv = NULL;
1da177e4
LT
1118
1119 buf->dev_priv_size = dev->driver->dev_priv_size;
94e3370e 1120 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
b5e89ed5 1121 if (!buf->dev_private) {
1da177e4
LT
1122 /* Set count correctly so we free the proper amount. */
1123 entry->buf_count = count;
b5e89ed5 1124 drm_cleanup_buf_error(dev, entry);
30e2fb18 1125 mutex_unlock(&dev->struct_mutex);
b5e89ed5 1126 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1127 return -ENOMEM;
1128 }
1129
b5e89ed5 1130 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1da177e4
LT
1131
1132 offset += alignment;
1133 entry->buf_count++;
1134 byte_count += PAGE_SIZE << page_order;
1135 }
1136
b5e89ed5 1137 DRM_DEBUG("byte_count: %d\n", byte_count);
1da177e4 1138
9a298b2a
EA
1139 temp_buflist = krealloc(dma->buflist,
1140 (dma->buf_count + entry->buf_count) *
1141 sizeof(*dma->buflist), GFP_KERNEL);
b5e89ed5 1142 if (!temp_buflist) {
1da177e4 1143 /* Free the entry because it isn't valid */
b5e89ed5 1144 drm_cleanup_buf_error(dev, entry);
30e2fb18 1145 mutex_unlock(&dev->struct_mutex);
b5e89ed5 1146 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1147 return -ENOMEM;
1148 }
1149 dma->buflist = temp_buflist;
1150
b5e89ed5 1151 for (i = 0; i < entry->buf_count; i++) {
1da177e4
LT
1152 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1153 }
1154
1155 dma->buf_count += entry->buf_count;
d985c108
DA
1156 dma->seg_count += entry->seg_count;
1157 dma->page_count += byte_count >> PAGE_SHIFT;
1da177e4
LT
1158 dma->byte_count += byte_count;
1159
b5e89ed5
DA
1160 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1161 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1da177e4 1162
30e2fb18 1163 mutex_unlock(&dev->struct_mutex);
1da177e4 1164
d59431bf
DA
1165 request->count = entry->buf_count;
1166 request->size = size;
1da177e4
LT
1167
1168 dma->flags = _DRM_DMA_USE_SG;
1169
b5e89ed5 1170 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1171 return 0;
1172}
1173
1174/**
1175 * Add buffers for DMA transfers (ioctl).
1176 *
1177 * \param inode device inode.
6c340eac 1178 * \param file_priv DRM file private.
1da177e4 1179 * \param cmd command.
c60ce623 1180 * \param arg pointer to a struct drm_buf_desc request.
1da177e4
LT
1181 * \return zero on success or a negative number on failure.
1182 *
1183 * According with the memory type specified in drm_buf_desc::flags and the
1184 * build options, it dispatches the call either to addbufs_agp(),
1185 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1186 * PCI memory respectively.
1187 */
9fc5cde7
DH
1188int drm_legacy_addbufs(struct drm_device *dev, void *data,
1189 struct drm_file *file_priv)
1da177e4 1190{
c153f45f 1191 struct drm_buf_desc *request = data;
d59431bf 1192 int ret;
b5e89ed5 1193
8d38c4b4
DV
1194 if (drm_core_check_feature(dev, DRIVER_MODESET))
1195 return -EINVAL;
1196
1da177e4
LT
1197 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1198 return -EINVAL;
1199
a7fb8a23 1200#if IS_ENABLED(CONFIG_AGP)
c153f45f 1201 if (request->flags & _DRM_AGP_BUFFER)
9fc5cde7 1202 ret = drm_legacy_addbufs_agp(dev, request);
1da177e4
LT
1203 else
1204#endif
c153f45f 1205 if (request->flags & _DRM_SG_BUFFER)
9fc5cde7 1206 ret = drm_legacy_addbufs_sg(dev, request);
c153f45f 1207 else if (request->flags & _DRM_FB_BUFFER)
687fbb2e 1208 ret = -EINVAL;
1da177e4 1209 else
9fc5cde7 1210 ret = drm_legacy_addbufs_pci(dev, request);
d59431bf 1211
d59431bf 1212 return ret;
1da177e4
LT
1213}
1214
1da177e4
LT
1215/**
1216 * Get information about the buffer mappings.
1217 *
1218 * This was originally mean for debugging purposes, or by a sophisticated
1219 * client library to determine how best to use the available buffers (e.g.,
1220 * large buffers can be used for image transfer).
1221 *
1222 * \param inode device inode.
6c340eac 1223 * \param file_priv DRM file private.
1da177e4
LT
1224 * \param cmd command.
1225 * \param arg pointer to a drm_buf_info structure.
1226 * \return zero on success or a negative number on failure.
1227 *
2177a218 1228 * Increments drm_device::buf_use while holding the drm_device::buf_lock
1da177e4
LT
1229 * lock, preventing of allocating more buffers after this call. Information
1230 * about each requested buffer is then copied into user space.
1231 */
9fc5cde7
DH
1232int drm_legacy_infobufs(struct drm_device *dev, void *data,
1233 struct drm_file *file_priv)
1da177e4 1234{
cdd55a29 1235 struct drm_device_dma *dma = dev->dma;
c153f45f 1236 struct drm_buf_info *request = data;
1da177e4
LT
1237 int i;
1238 int count;
1239
8d38c4b4
DV
1240 if (drm_core_check_feature(dev, DRIVER_MODESET))
1241 return -EINVAL;
1242
1da177e4
LT
1243 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1244 return -EINVAL;
1245
b5e89ed5
DA
1246 if (!dma)
1247 return -EINVAL;
1da177e4 1248
2177a218 1249 spin_lock(&dev->buf_lock);
b5e89ed5 1250 if (atomic_read(&dev->buf_alloc)) {
2177a218 1251 spin_unlock(&dev->buf_lock);
1da177e4
LT
1252 return -EBUSY;
1253 }
1254 ++dev->buf_use; /* Can't allocate more after this call */
2177a218 1255 spin_unlock(&dev->buf_lock);
1da177e4 1256
b5e89ed5
DA
1257 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1258 if (dma->bufs[i].buf_count)
1259 ++count;
1da177e4
LT
1260 }
1261
b5e89ed5 1262 DRM_DEBUG("count = %d\n", count);
1da177e4 1263
c153f45f 1264 if (request->count >= count) {
b5e89ed5
DA
1265 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1266 if (dma->bufs[i].buf_count) {
c60ce623 1267 struct drm_buf_desc __user *to =
c153f45f 1268 &request->list[count];
cdd55a29 1269 struct drm_buf_entry *from = &dma->bufs[i];
b5e89ed5
DA
1270 if (copy_to_user(&to->count,
1271 &from->buf_count,
1272 sizeof(from->buf_count)) ||
1273 copy_to_user(&to->size,
1274 &from->buf_size,
1275 sizeof(from->buf_size)) ||
1276 copy_to_user(&to->low_mark,
b008c0fc
DH
1277 &from->low_mark,
1278 sizeof(from->low_mark)) ||
b5e89ed5 1279 copy_to_user(&to->high_mark,
b008c0fc
DH
1280 &from->high_mark,
1281 sizeof(from->high_mark)))
1da177e4
LT
1282 return -EFAULT;
1283
b5e89ed5
DA
1284 DRM_DEBUG("%d %d %d %d %d\n",
1285 i,
1286 dma->bufs[i].buf_count,
1287 dma->bufs[i].buf_size,
b008c0fc
DH
1288 dma->bufs[i].low_mark,
1289 dma->bufs[i].high_mark);
1da177e4
LT
1290 ++count;
1291 }
1292 }
1293 }
c153f45f 1294 request->count = count;
1da177e4
LT
1295
1296 return 0;
1297}
1298
1299/**
1300 * Specifies a low and high water mark for buffer allocation
1301 *
1302 * \param inode device inode.
6c340eac 1303 * \param file_priv DRM file private.
1da177e4
LT
1304 * \param cmd command.
1305 * \param arg a pointer to a drm_buf_desc structure.
1306 * \return zero on success or a negative number on failure.
1307 *
1308 * Verifies that the size order is bounded between the admissible orders and
1309 * updates the respective drm_device_dma::bufs entry low and high water mark.
1310 *
1311 * \note This ioctl is deprecated and mostly never used.
1312 */
9fc5cde7
DH
1313int drm_legacy_markbufs(struct drm_device *dev, void *data,
1314 struct drm_file *file_priv)
1da177e4 1315{
cdd55a29 1316 struct drm_device_dma *dma = dev->dma;
c153f45f 1317 struct drm_buf_desc *request = data;
1da177e4 1318 int order;
cdd55a29 1319 struct drm_buf_entry *entry;
1da177e4 1320
8d38c4b4
DV
1321 if (drm_core_check_feature(dev, DRIVER_MODESET))
1322 return -EINVAL;
1323
1da177e4
LT
1324 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1325 return -EINVAL;
1326
b5e89ed5
DA
1327 if (!dma)
1328 return -EINVAL;
1da177e4 1329
b5e89ed5 1330 DRM_DEBUG("%d, %d, %d\n",
c153f45f 1331 request->size, request->low_mark, request->high_mark);
04420c9c 1332 order = order_base_2(request->size);
b5e89ed5
DA
1333 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1334 return -EINVAL;
1da177e4
LT
1335 entry = &dma->bufs[order];
1336
c153f45f 1337 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1da177e4 1338 return -EINVAL;
c153f45f 1339 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1da177e4
LT
1340 return -EINVAL;
1341
b008c0fc
DH
1342 entry->low_mark = request->low_mark;
1343 entry->high_mark = request->high_mark;
1da177e4
LT
1344
1345 return 0;
1346}
1347
1348/**
b5e89ed5 1349 * Unreserve the buffers in list, previously reserved using drmDMA.
1da177e4
LT
1350 *
1351 * \param inode device inode.
6c340eac 1352 * \param file_priv DRM file private.
1da177e4
LT
1353 * \param cmd command.
1354 * \param arg pointer to a drm_buf_free structure.
1355 * \return zero on success or a negative number on failure.
b5e89ed5 1356 *
1da177e4
LT
1357 * Calls free_buffer() for each used buffer.
1358 * This function is primarily used for debugging.
1359 */
9fc5cde7
DH
1360int drm_legacy_freebufs(struct drm_device *dev, void *data,
1361 struct drm_file *file_priv)
1da177e4 1362{
cdd55a29 1363 struct drm_device_dma *dma = dev->dma;
c153f45f 1364 struct drm_buf_free *request = data;
1da177e4
LT
1365 int i;
1366 int idx;
056219e2 1367 struct drm_buf *buf;
1da177e4 1368
8d38c4b4
DV
1369 if (drm_core_check_feature(dev, DRIVER_MODESET))
1370 return -EINVAL;
1371
1da177e4
LT
1372 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1373 return -EINVAL;
1374
b5e89ed5
DA
1375 if (!dma)
1376 return -EINVAL;
1da177e4 1377
c153f45f
EA
1378 DRM_DEBUG("%d\n", request->count);
1379 for (i = 0; i < request->count; i++) {
1380 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1da177e4 1381 return -EFAULT;
b5e89ed5
DA
1382 if (idx < 0 || idx >= dma->buf_count) {
1383 DRM_ERROR("Index %d (of %d max)\n",
1384 idx, dma->buf_count - 1);
1da177e4
LT
1385 return -EINVAL;
1386 }
1387 buf = dma->buflist[idx];
6c340eac 1388 if (buf->file_priv != file_priv) {
b5e89ed5 1389 DRM_ERROR("Process %d freeing buffer not owned\n",
ba25f9dc 1390 task_pid_nr(current));
1da177e4
LT
1391 return -EINVAL;
1392 }
a266162a 1393 drm_legacy_free_buffer(dev, buf);
1da177e4
LT
1394 }
1395
1396 return 0;
1397}
1398
1399/**
1400 * Maps all of the DMA buffers into client-virtual space (ioctl).
1401 *
1402 * \param inode device inode.
6c340eac 1403 * \param file_priv DRM file private.
1da177e4
LT
1404 * \param cmd command.
1405 * \param arg pointer to a drm_buf_map structure.
1406 * \return zero on success or a negative number on failure.
1407 *
6be5ceb0
LT
1408 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1409 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
3417f33e
GS
1410 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1411 * drm_mmap_dma().
1da177e4 1412 */
9fc5cde7
DH
1413int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1414 struct drm_file *file_priv)
1da177e4 1415{
cdd55a29 1416 struct drm_device_dma *dma = dev->dma;
1da177e4
LT
1417 int retcode = 0;
1418 const int zero = 0;
1419 unsigned long virtual;
1420 unsigned long address;
c153f45f 1421 struct drm_buf_map *request = data;
1da177e4
LT
1422 int i;
1423
8d38c4b4
DV
1424 if (drm_core_check_feature(dev, DRIVER_MODESET))
1425 return -EINVAL;
1426
1da177e4
LT
1427 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1428 return -EINVAL;
1429
b5e89ed5
DA
1430 if (!dma)
1431 return -EINVAL;
1da177e4 1432
2177a218 1433 spin_lock(&dev->buf_lock);
b5e89ed5 1434 if (atomic_read(&dev->buf_alloc)) {
2177a218 1435 spin_unlock(&dev->buf_lock);
1da177e4
LT
1436 return -EBUSY;
1437 }
1438 dev->buf_use++; /* Can't allocate more after this call */
2177a218 1439 spin_unlock(&dev->buf_lock);
1da177e4 1440
c153f45f 1441 if (request->count >= dma->buf_count) {
d9906753 1442 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
b5e89ed5 1443 || (drm_core_check_feature(dev, DRIVER_SG)
687fbb2e 1444 && (dma->flags & _DRM_DMA_USE_SG))) {
f77d390c 1445 struct drm_local_map *map = dev->agp_buffer_map;
d1f2b55a 1446 unsigned long token = dev->agp_buffer_token;
1da177e4 1447
b5e89ed5 1448 if (!map) {
1da177e4
LT
1449 retcode = -EINVAL;
1450 goto done;
1451 }
6be5ceb0 1452 virtual = vm_mmap(file_priv->filp, 0, map->size,
b5e89ed5 1453 PROT_READ | PROT_WRITE,
c153f45f
EA
1454 MAP_SHARED,
1455 token);
1da177e4 1456 } else {
6be5ceb0 1457 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
b5e89ed5
DA
1458 PROT_READ | PROT_WRITE,
1459 MAP_SHARED, 0);
1da177e4 1460 }
b5e89ed5 1461 if (virtual > -1024UL) {
1da177e4
LT
1462 /* Real error */
1463 retcode = (signed long)virtual;
1464 goto done;
1465 }
c153f45f 1466 request->virtual = (void __user *)virtual;
1da177e4 1467
b5e89ed5 1468 for (i = 0; i < dma->buf_count; i++) {
c153f45f 1469 if (copy_to_user(&request->list[i].idx,
b5e89ed5 1470 &dma->buflist[i]->idx,
c153f45f 1471 sizeof(request->list[0].idx))) {
1da177e4
LT
1472 retcode = -EFAULT;
1473 goto done;
1474 }
c153f45f 1475 if (copy_to_user(&request->list[i].total,
b5e89ed5 1476 &dma->buflist[i]->total,
c153f45f 1477 sizeof(request->list[0].total))) {
1da177e4
LT
1478 retcode = -EFAULT;
1479 goto done;
1480 }
c153f45f 1481 if (copy_to_user(&request->list[i].used,
b5e89ed5 1482 &zero, sizeof(zero))) {
1da177e4
LT
1483 retcode = -EFAULT;
1484 goto done;
1485 }
b5e89ed5 1486 address = virtual + dma->buflist[i]->offset; /* *** */
c153f45f 1487 if (copy_to_user(&request->list[i].address,
b5e89ed5 1488 &address, sizeof(address))) {
1da177e4
LT
1489 retcode = -EFAULT;
1490 goto done;
1491 }
1492 }
1493 }
b5e89ed5 1494 done:
c153f45f
EA
1495 request->count = dma->buf_count;
1496 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1da177e4
LT
1497
1498 return retcode;
1499}
1500
9fc5cde7 1501int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
6eb9278a
DV
1502 struct drm_file *file_priv)
1503{
1504 if (drm_core_check_feature(dev, DRIVER_MODESET))
1505 return -EINVAL;
1506
1507 if (dev->driver->dma_ioctl)
1508 return dev->driver->dma_ioctl(dev, data, file_priv);
1509 else
1510 return -EINVAL;
1511}
1512
9fc5cde7 1513struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
bd0c0cee
DV
1514{
1515 struct drm_map_list *entry;
1516
1517 list_for_each_entry(entry, &dev->maplist, head) {
1518 if (entry->map && entry->map->type == _DRM_SHM &&
1519 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1520 return entry->map;
1521 }
1522 }
1523 return NULL;
1524}
9fc5cde7 1525EXPORT_SYMBOL(drm_legacy_getsarea);
This page took 0.802239 seconds and 5 git commands to generate.