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