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