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