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