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