OSDN Git Service

initial import
[android-x86/device-viliv-s5.git] / psb-kernel-source-4.41.1 / drm_bufs.c
1 /**
2  * \file drm_bufs.c
3  * Generic buffer template
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8
9 /*
10  * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
11  *
12  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14  * All Rights Reserved.
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice (including the next
24  * paragraph) shall be included in all copies or substantial portions of the
25  * Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  */
35
36 #include <linux/vmalloc.h>
37 #include "drmP.h"
38
39 unsigned long drm_get_resource_start(struct drm_device *dev, unsigned int resource)
40 {
41         return pci_resource_start(dev->pdev, resource);
42 }
43 EXPORT_SYMBOL(drm_get_resource_start);
44
45 unsigned long drm_get_resource_len(struct drm_device *dev, unsigned int resource)
46 {
47         return pci_resource_len(dev->pdev, resource);
48 }
49 EXPORT_SYMBOL(drm_get_resource_len);
50
51 struct drm_map_list *drm_find_matching_map(struct drm_device *dev, drm_local_map_t *map)
52 {
53         struct drm_map_list *entry;
54         list_for_each_entry(entry, &dev->maplist, head) {
55                 if (entry->map && map->type == entry->map->type &&
56                     ((entry->map->offset == map->offset) || 
57                      ((map->type == _DRM_SHM) && (map->flags&_DRM_CONTAINS_LOCK)))) {
58                         return entry;
59                 }
60         }
61
62         return NULL;
63 }
64 EXPORT_SYMBOL(drm_find_matching_map);
65
66 static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
67                           unsigned long user_token, int hashed_handle)
68 {
69         int use_hashed_handle;
70
71 #if (BITS_PER_LONG == 64)
72         use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
73 #elif (BITS_PER_LONG == 32)
74         use_hashed_handle = hashed_handle;
75 #else
76 #error Unsupported long size. Neither 64 nor 32 bits.
77 #endif
78
79         if (!use_hashed_handle) {
80                 int ret;
81                 hash->key = user_token >> PAGE_SHIFT;
82                 ret = drm_ht_insert_item(&dev->map_hash, hash);
83                 if (ret != -EINVAL)
84                         return ret;
85         }
86         return drm_ht_just_insert_please(&dev->map_hash, hash,
87                                          user_token, 32 - PAGE_SHIFT - 3,
88                                          0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);
89 }
90
91 /**
92  * Ioctl to specify a range of memory that is available for mapping by a non-root process.
93  *
94  * \param inode device inode.
95  * \param file_priv DRM file private.
96  * \param cmd command.
97  * \param arg pointer to a drm_map structure.
98  * \return zero on success or a negative value on error.
99  *
100  * Adjusts the memory offset to its absolute value according to the mapping
101  * type.  Adds the map to the map list drm_device::maplist. Adds MTRR's where
102  * applicable and if supported by the kernel.
103  */
104 static int drm_addmap_core(struct drm_device *dev, unsigned int offset,
105                            unsigned int size, enum drm_map_type type,
106                            enum drm_map_flags flags,
107                            struct drm_map_list **maplist)
108 {
109         struct drm_map *map;
110         struct drm_map_list *list;
111         drm_dma_handle_t *dmah;
112         unsigned long user_token;
113         int ret;
114
115         map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
116         if (!map)
117                 return -ENOMEM;
118
119         map->offset = offset;
120         map->size = size;
121         map->flags = flags;
122         map->type = type;
123
124         /* Only allow shared memory to be removable since we only keep enough
125          * book keeping information about shared memory to allow for removal
126          * when processes fork.
127          */
128         if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
129                 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
130                 return -EINVAL;
131         }
132         DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
133                   map->offset, map->size, map->type);
134         if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
135                 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
136                 return -EINVAL;
137         }
138         map->mtrr = -1;
139         map->handle = NULL;
140
141         switch (map->type) {
142         case _DRM_REGISTERS:
143         case _DRM_FRAME_BUFFER:
144 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
145                 if (map->offset + (map->size - 1) < map->offset ||
146                     map->offset < virt_to_phys(high_memory)) {
147                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
148                         return -EINVAL;
149                 }
150 #endif
151 #ifdef __alpha__
152                 map->offset += dev->hose->mem_space->start;
153 #endif
154                 /* Some drivers preinitialize some maps, without the X Server
155                  * needing to be aware of it.  Therefore, we just return success
156                  * when the server tries to create a duplicate map.
157                  */
158                 list = drm_find_matching_map(dev, map);
159                 if (list != NULL) {
160                         if (list->map->size != map->size) {
161                                 DRM_DEBUG("Matching maps of type %d with "
162                                           "mismatched sizes, (%ld vs %ld)\n",
163                                           map->type, map->size,
164                                           list->map->size);
165                                 list->map->size = map->size;
166                         }
167
168                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
169                         *maplist = list;
170                         return 0;
171                 }
172
173                 if (drm_core_has_MTRR(dev)) {
174                         if (map->type == _DRM_FRAME_BUFFER ||
175                             (map->flags & _DRM_WRITE_COMBINING)) {
176                                 map->mtrr = mtrr_add(map->offset, map->size,
177                                                      MTRR_TYPE_WRCOMB, 1);
178                         }
179                 }
180                 if (map->type == _DRM_REGISTERS) {
181                         map->handle = ioremap(map->offset, map->size);
182                         if (!map->handle) {
183                                 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
184                                 return -ENOMEM;
185                         }
186                 }
187                 break;
188         case _DRM_SHM:
189                 list = drm_find_matching_map(dev, map);
190                 if (list != NULL) {
191                         if(list->map->size != map->size) {
192                                 DRM_DEBUG("Matching maps of type %d with "
193                                    "mismatched sizes, (%ld vs %ld)\n",
194                                     map->type, map->size, list->map->size);
195                                 list->map->size = map->size;
196                         }
197
198                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
199                         *maplist = list;
200                         return 0;
201                 }
202                 map->handle = vmalloc_user(map->size);
203                 DRM_DEBUG("%lu %d %p\n",
204                           map->size, drm_order(map->size), map->handle);
205                 if (!map->handle) {
206                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
207                         return -ENOMEM;
208                 }
209                 map->offset = (unsigned long)map->handle;
210                 if (map->flags & _DRM_CONTAINS_LOCK) {
211                         /* Prevent a 2nd X Server from creating a 2nd lock */
212                         if (dev->lock.hw_lock != NULL) {
213                                 vfree(map->handle);
214                                 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
215                                 return -EBUSY;
216                         }
217                         dev->sigdata.lock = dev->lock.hw_lock = map->handle;    /* Pointer to lock */
218                 }
219                 break;
220         case _DRM_AGP: {
221                 struct drm_agp_mem *entry;
222                 int valid = 0;
223
224                 if (!drm_core_has_AGP(dev)) {
225                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
226                         return -EINVAL;
227                 }
228 #ifdef __alpha__
229                 map->offset += dev->hose->mem_space->start;
230 #endif
231                 /* In some cases (i810 driver), user space may have already
232                  * added the AGP base itself, because dev->agp->base previously
233                  * only got set during AGP enable.  So, only add the base
234                  * address if the map's offset isn't already within the
235                  * aperture.
236                  */
237                 if (map->offset < dev->agp->base ||
238                     map->offset > dev->agp->base +
239                     dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
240                         map->offset += dev->agp->base;
241                 }
242                 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
243
244                 /* This assumes the DRM is in total control of AGP space.
245                  * It's not always the case as AGP can be in the control
246                  * of user space (i.e. i810 driver). So this loop will get
247                  * skipped and we double check that dev->agp->memory is
248                  * actually set as well as being invalid before EPERM'ing
249                  */
250                 list_for_each_entry(entry, &dev->agp->memory, head) {
251                         if ((map->offset >= entry->bound) &&
252                             (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
253                                 valid = 1;
254                                 break;
255                         }
256                 }
257                 if (!list_empty(&dev->agp->memory) && !valid) {
258                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
259                         return -EPERM;
260                 }
261                 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
262                 break;
263         }
264         case _DRM_SCATTER_GATHER:
265                 if (!dev->sg) {
266                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
267                         return -EINVAL;
268                 }
269                 map->offset += (unsigned long)dev->sg->virtual;
270                 break;
271         case _DRM_CONSISTENT:
272                 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
273                  * As we're limiting the address to 2^32-1 (or less),
274                  * casting it down to 32 bits is no problem, but we
275                  * need to point to a 64bit variable first. */
276                 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
277                 if (!dmah) {
278                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
279                         return -ENOMEM;
280                 }
281                 map->handle = dmah->vaddr;
282                 map->offset = (unsigned long)dmah->busaddr;
283                 kfree(dmah);
284                 break;
285         default:
286                 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
287                 return -EINVAL;
288         }
289
290         list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
291         if (!list) {
292                 if (map->type == _DRM_REGISTERS)
293                         iounmap(map->handle);
294                 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
295                 return -EINVAL;
296         }
297         memset(list, 0, sizeof(*list));
298         list->map = map;
299
300         mutex_lock(&dev->struct_mutex);
301         list_add(&list->head, &dev->maplist);
302
303         /* Assign a 32-bit handle */
304
305         user_token = (map->type == _DRM_SHM) ? (unsigned long) map->handle :
306                 map->offset;
307         ret = drm_map_handle(dev, &list->hash, user_token, 0);
308
309         if (ret) {
310                 if (map->type == _DRM_REGISTERS)
311                         iounmap(map->handle);
312                 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
313                 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
314                 mutex_unlock(&dev->struct_mutex);
315                 return ret;
316         }
317
318         list->user_token = list->hash.key << PAGE_SHIFT;
319         mutex_unlock(&dev->struct_mutex);
320
321         *maplist = list;
322         return 0;
323 }
324
325 int drm_addmap(struct drm_device *dev, unsigned int offset,
326                unsigned int size, enum drm_map_type type,
327                enum drm_map_flags flags, drm_local_map_t ** map_ptr)
328 {
329         struct drm_map_list *list;
330         int rc;
331
332         rc = drm_addmap_core(dev, offset, size, type, flags, &list);
333         if (!rc)
334                 *map_ptr = list->map;
335         return rc;
336 }
337
338 EXPORT_SYMBOL(drm_addmap);
339
340 int drm_addmap_ioctl(struct drm_device *dev, void *data,
341                      struct drm_file *file_priv)
342 {
343         struct drm_map *map = data;
344         struct drm_map_list *maplist;
345         int err;
346
347         if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP))
348                 return -EPERM;
349
350         err = drm_addmap_core(dev, map->offset, map->size, map->type,
351                               map->flags, &maplist);
352
353         if (err)
354                 return err;
355
356         /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
357         map->handle = (void *)(unsigned long)maplist->user_token;
358         return 0;
359 }
360
361 /**
362  * Remove a map private from list and deallocate resources if the mapping
363  * isn't in use.
364  *
365  * \param inode device inode.
366  * \param file_priv DRM file private.
367  * \param cmd command.
368  * \param arg pointer to a struct drm_map structure.
369  * \return zero on success or a negative value on error.
370  *
371  * Searches the map on drm_device::maplist, removes it from the list, see if
372  * its being used, and free any associate resource (such as MTRR's) if it's not
373  * being on use.
374  *
375  * \sa drm_addmap
376  */
377 int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
378 {
379         struct drm_map_list *r_list = NULL, *list_t;
380         drm_dma_handle_t dmah;
381         int found = 0;
382
383         /* Find the list entry for the map and remove it */
384         list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
385                 if (r_list->map == map) {
386                         list_del(&r_list->head);
387                         drm_ht_remove_key(&dev->map_hash,
388                                           r_list->user_token >> PAGE_SHIFT);
389                         drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
390                         found = 1;
391                         break;
392                 }
393         }
394
395         if (!found)
396                 return -EINVAL;
397
398         /* List has wrapped around to the head pointer, or it's empty and we
399          * didn't find anything.
400          */
401
402         switch (map->type) {
403         case _DRM_REGISTERS:
404                 iounmap(map->handle);
405                 /* FALLTHROUGH */
406         case _DRM_FRAME_BUFFER:
407                 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
408                         int retcode;
409                         retcode = mtrr_del(map->mtrr, map->offset, map->size);
410                         DRM_DEBUG("mtrr_del=%d\n", retcode);
411                 }
412                 break;
413         case _DRM_SHM:
414                 vfree(map->handle);
415                 dev->sigdata.lock = dev->lock.hw_lock = NULL;   /* SHM removed */
416                 break;
417         case _DRM_AGP:
418         case _DRM_SCATTER_GATHER:
419                 break;
420         case _DRM_CONSISTENT:
421                 dmah.vaddr = map->handle;
422                 dmah.busaddr = map->offset;
423                 dmah.size = map->size;
424                 __drm_pci_free(dev, &dmah);
425                 break;
426         case _DRM_TTM:
427                 BUG_ON(1);
428         }
429         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
430
431         return 0;
432 }
433 EXPORT_SYMBOL(drm_rmmap_locked);
434
435 int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
436 {
437         int ret;
438
439         mutex_lock(&dev->struct_mutex);
440         ret = drm_rmmap_locked(dev, map);
441         mutex_unlock(&dev->struct_mutex);
442
443         return ret;
444 }
445 EXPORT_SYMBOL(drm_rmmap);
446
447 /* The rmmap ioctl appears to be unnecessary.  All mappings are torn down on
448  * the last close of the device, and this is necessary for cleanup when things
449  * exit uncleanly.  Therefore, having userland manually remove mappings seems
450  * like a pointless exercise since they're going away anyway.
451  *
452  * One use case might be after addmap is allowed for normal users for SHM and
453  * gets used by drivers that the server doesn't need to care about.  This seems
454  * unlikely.
455  */
456 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
457                     struct drm_file *file_priv)
458 {
459         struct drm_map *request = data;
460         drm_local_map_t *map = NULL;
461         struct drm_map_list *r_list;
462         int ret;
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->file_priv = 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->file_priv = 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->file_priv = 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 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->file_priv = 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 EXPORT_SYMBOL(drm_addbufs_fb);
1267
1268
1269 /**
1270  * Add buffers for DMA transfers (ioctl).
1271  *
1272  * \param inode device inode.
1273  * \param file_priv DRM file private.
1274  * \param cmd command.
1275  * \param arg pointer to a struct drm_buf_desc request.
1276  * \return zero on success or a negative number on failure.
1277  *
1278  * According with the memory type specified in drm_buf_desc::flags and the
1279  * build options, it dispatches the call either to addbufs_agp(),
1280  * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1281  * PCI memory respectively.
1282  */
1283 int drm_addbufs(struct drm_device *dev, void *data,
1284                 struct drm_file *file_priv)
1285 {
1286         struct drm_buf_desc *request = data;
1287         int ret;
1288
1289         if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1290                 return -EINVAL;
1291
1292 #if __OS_HAS_AGP
1293         if (request->flags & _DRM_AGP_BUFFER)
1294                 ret = drm_addbufs_agp(dev, request);
1295         else
1296 #endif
1297         if (request->flags & _DRM_SG_BUFFER)
1298                 ret = drm_addbufs_sg(dev, request);
1299         else if (request->flags & _DRM_FB_BUFFER)
1300                 ret = drm_addbufs_fb(dev, request);
1301         else
1302                 ret = drm_addbufs_pci(dev, request);
1303
1304         return ret;
1305 }
1306
1307 /**
1308  * Get information about the buffer mappings.
1309  *
1310  * This was originally mean for debugging purposes, or by a sophisticated
1311  * client library to determine how best to use the available buffers (e.g.,
1312  * large buffers can be used for image transfer).
1313  *
1314  * \param inode device inode.
1315  * \param file_priv DRM file private.
1316  * \param cmd command.
1317  * \param arg pointer to a drm_buf_info structure.
1318  * \return zero on success or a negative number on failure.
1319  *
1320  * Increments drm_device::buf_use while holding the drm_device::count_lock
1321  * lock, preventing of allocating more buffers after this call. Information
1322  * about each requested buffer is then copied into user space.
1323  */
1324 int drm_infobufs(struct drm_device *dev, void *data,
1325                  struct drm_file *file_priv)
1326 {
1327         struct drm_device_dma *dma = dev->dma;
1328         struct drm_buf_info *request = data;
1329         int i;
1330         int count;
1331
1332         if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1333                 return -EINVAL;
1334
1335         if (!dma)
1336                 return -EINVAL;
1337
1338         spin_lock(&dev->count_lock);
1339         if (atomic_read(&dev->buf_alloc)) {
1340                 spin_unlock(&dev->count_lock);
1341                 return -EBUSY;
1342         }
1343         ++dev->buf_use;         /* Can't allocate more after this call */
1344         spin_unlock(&dev->count_lock);
1345
1346         for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1347                 if (dma->bufs[i].buf_count)
1348                         ++count;
1349         }
1350
1351         DRM_DEBUG("count = %d\n", count);
1352
1353         if (request->count >= count) {
1354                 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1355                         if (dma->bufs[i].buf_count) {
1356                                 struct drm_buf_desc __user *to =
1357                                     &request->list[count];
1358                                 struct drm_buf_entry *from = &dma->bufs[i];
1359                                 struct drm_freelist *list = &dma->bufs[i].freelist;
1360                                 if (copy_to_user(&to->count,
1361                                                  &from->buf_count,
1362                                                  sizeof(from->buf_count)) ||
1363                                     copy_to_user(&to->size,
1364                                                  &from->buf_size,
1365                                                  sizeof(from->buf_size)) ||
1366                                     copy_to_user(&to->low_mark,
1367                                                  &list->low_mark,
1368                                                  sizeof(list->low_mark)) ||
1369                                     copy_to_user(&to->high_mark,
1370                                                  &list->high_mark,
1371                                                  sizeof(list->high_mark)))
1372                                         return -EFAULT;
1373
1374                                 DRM_DEBUG("%d %d %d %d %d\n",
1375                                           i,
1376                                           dma->bufs[i].buf_count,
1377                                           dma->bufs[i].buf_size,
1378                                           dma->bufs[i].freelist.low_mark,
1379                                           dma->bufs[i].freelist.high_mark);
1380                                 ++count;
1381                         }
1382                 }
1383         }
1384         request->count = count;
1385
1386         return 0;
1387 }
1388
1389 /**
1390  * Specifies a low and high water mark for buffer allocation
1391  *
1392  * \param inode device inode.
1393  * \param file_priv DRM file private.
1394  * \param cmd command.
1395  * \param arg a pointer to a drm_buf_desc structure.
1396  * \return zero on success or a negative number on failure.
1397  *
1398  * Verifies that the size order is bounded between the admissible orders and
1399  * updates the respective drm_device_dma::bufs entry low and high water mark.
1400  *
1401  * \note This ioctl is deprecated and mostly never used.
1402  */
1403 int drm_markbufs(struct drm_device *dev, void *data,
1404                  struct drm_file *file_priv)
1405 {
1406         struct drm_device_dma *dma = dev->dma;
1407         struct drm_buf_desc *request = data;
1408         int order;
1409         struct drm_buf_entry *entry;
1410
1411         if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1412                 return -EINVAL;
1413
1414         if (!dma)
1415                 return -EINVAL;
1416
1417         DRM_DEBUG("%d, %d, %d\n",
1418                   request->size, request->low_mark, request->high_mark);
1419         order = drm_order(request->size);
1420         if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1421                 return -EINVAL;
1422         entry = &dma->bufs[order];
1423
1424         if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1425                 return -EINVAL;
1426         if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1427                 return -EINVAL;
1428
1429         entry->freelist.low_mark = request->low_mark;
1430         entry->freelist.high_mark = request->high_mark;
1431
1432         return 0;
1433 }
1434
1435 /**
1436  * Unreserve the buffers in list, previously reserved using drmDMA.
1437  *
1438  * \param inode device inode.
1439  * \param file_priv DRM file private.
1440  * \param cmd command.
1441  * \param arg pointer to a drm_buf_free structure.
1442  * \return zero on success or a negative number on failure.
1443  *
1444  * Calls free_buffer() for each used buffer.
1445  * This function is primarily used for debugging.
1446  */
1447 int drm_freebufs(struct drm_device *dev, void *data,
1448                  struct drm_file *file_priv)
1449 {
1450         struct drm_device_dma *dma = dev->dma;
1451         struct drm_buf_free *request = data;
1452         int i;
1453         int idx;
1454         struct drm_buf *buf;
1455
1456         if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1457                 return -EINVAL;
1458
1459         if (!dma)
1460                 return -EINVAL;
1461
1462         DRM_DEBUG("%d\n", request->count);
1463         for (i = 0; i < request->count; i++) {
1464                 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1465                         return -EFAULT;
1466                 if (idx < 0 || idx >= dma->buf_count) {
1467                         DRM_ERROR("Index %d (of %d max)\n",
1468                                   idx, dma->buf_count - 1);
1469                         return -EINVAL;
1470                 }
1471                 buf = dma->buflist[idx];
1472                 if (buf->file_priv != file_priv) {
1473                         DRM_ERROR("Process %d freeing buffer not owned\n",
1474                                   current->pid);
1475                         return -EINVAL;
1476                 }
1477                 drm_free_buffer(dev, buf);
1478         }
1479
1480         return 0;
1481 }
1482
1483 /**
1484  * Maps all of the DMA buffers into client-virtual space (ioctl).
1485  *
1486  * \param inode device inode.
1487  * \param file_priv DRM file private.
1488  * \param cmd command.
1489  * \param arg pointer to a drm_buf_map structure.
1490  * \return zero on success or a negative number on failure.
1491  *
1492  * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1493  * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1494  * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1495  * drm_mmap_dma().
1496  */
1497 int drm_mapbufs(struct drm_device *dev, void *data,
1498                 struct drm_file *file_priv)
1499 {
1500         struct drm_device_dma *dma = dev->dma;
1501         int retcode = 0;
1502         const int zero = 0;
1503         unsigned long virtual;
1504         unsigned long address;
1505         struct drm_buf_map *request = data;
1506         int i;
1507
1508         if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1509                 return -EINVAL;
1510
1511         if (!dma)
1512                 return -EINVAL;
1513
1514         spin_lock(&dev->count_lock);
1515         if (atomic_read(&dev->buf_alloc)) {
1516                 spin_unlock(&dev->count_lock);
1517                 return -EBUSY;
1518         }
1519         dev->buf_use++;         /* Can't allocate more after this call */
1520         spin_unlock(&dev->count_lock);
1521
1522         if (request->count >= dma->buf_count) {
1523                 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1524                     || (drm_core_check_feature(dev, DRIVER_SG)
1525                         && (dma->flags & _DRM_DMA_USE_SG))
1526                     || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1527                         && (dma->flags & _DRM_DMA_USE_FB))) {
1528                         struct drm_map *map = dev->agp_buffer_map;
1529                         unsigned long token = dev->agp_buffer_token;
1530
1531                         if (!map) {
1532                                 retcode = -EINVAL;
1533                                 goto done;
1534                         }
1535                         down_write(&current->mm->mmap_sem);
1536                         virtual = do_mmap(file_priv->filp, 0, map->size,
1537                                           PROT_READ | PROT_WRITE,
1538                                           MAP_SHARED,
1539                                           token);
1540                         up_write(&current->mm->mmap_sem);
1541                 } else {
1542                         down_write(&current->mm->mmap_sem);
1543                         virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
1544                                           PROT_READ | PROT_WRITE,
1545                                           MAP_SHARED, 0);
1546                         up_write(&current->mm->mmap_sem);
1547                 }
1548                 if (virtual > -1024UL) {
1549                         /* Real error */
1550                         retcode = (signed long)virtual;
1551                         goto done;
1552                 }
1553                 request->virtual = (void __user *)virtual;
1554
1555                 for (i = 0; i < dma->buf_count; i++) {
1556                         if (copy_to_user(&request->list[i].idx,
1557                                          &dma->buflist[i]->idx,
1558                                          sizeof(request->list[0].idx))) {
1559                                 retcode = -EFAULT;
1560                                 goto done;
1561                         }
1562                         if (copy_to_user(&request->list[i].total,
1563                                          &dma->buflist[i]->total,
1564                                          sizeof(request->list[0].total))) {
1565                                 retcode = -EFAULT;
1566                                 goto done;
1567                         }
1568                         if (copy_to_user(&request->list[i].used,
1569                                          &zero, sizeof(zero))) {
1570                                 retcode = -EFAULT;
1571                                 goto done;
1572                         }
1573                         address = virtual + dma->buflist[i]->offset;    /* *** */
1574                         if (copy_to_user(&request->list[i].address,
1575                                          &address, sizeof(address))) {
1576                                 retcode = -EFAULT;
1577                                 goto done;
1578                         }
1579                 }
1580         }
1581       done:
1582         request->count = dma->buf_count;
1583         DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1584
1585         return retcode;
1586 }
1587
1588 /**
1589  * Compute size order.  Returns the exponent of the smaller power of two which
1590  * is greater or equal to given number.
1591  *
1592  * \param size size.
1593  * \return order.
1594  *
1595  * \todo Can be made faster.
1596  */
1597 int drm_order(unsigned long size)
1598 {
1599         int order;
1600         unsigned long tmp;
1601
1602         for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1603
1604         if (size & (size - 1))
1605                 ++order;
1606
1607         return order;
1608 }
1609 EXPORT_SYMBOL(drm_order);