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