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