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