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