OSDN Git Service

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