3 * Generic buffer template
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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
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.
36 #include <linux/vmalloc.h>
39 unsigned long drm_get_resource_start(struct drm_device *dev, unsigned int resource)
41 return pci_resource_start(dev->pdev, resource);
43 EXPORT_SYMBOL(drm_get_resource_start);
45 unsigned long drm_get_resource_len(struct drm_device *dev, unsigned int resource)
47 return pci_resource_len(dev->pdev, resource);
50 EXPORT_SYMBOL(drm_get_resource_len);
52 static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
55 struct drm_map_list *entry;
56 list_for_each_entry(entry, &dev->maplist, head) {
57 if (entry->map && map->type == entry->map->type &&
58 ((entry->map->offset == map->offset) ||
59 (map->type == _DRM_SHM && map->flags==_DRM_CONTAINS_LOCK))) {
67 static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
68 unsigned long user_token, int hashed_handle)
70 int use_hashed_handle;
71 #if (BITS_PER_LONG == 64)
72 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
73 #elif (BITS_PER_LONG == 32)
74 use_hashed_handle = hashed_handle;
76 #error Unsupported long size. Neither 64 nor 32 bits.
79 if (!use_hashed_handle) {
81 hash->key = user_token >> PAGE_SHIFT;
82 ret = drm_ht_insert_item(&dev->map_hash, hash);
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);
92 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
94 * \param inode device inode.
95 * \param file_priv DRM file private.
97 * \param arg pointer to a drm_map structure.
98 * \return zero on success or a negative value on error.
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.
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)
110 struct drm_map_list *list;
111 drm_dma_handle_t *dmah;
112 unsigned long user_token;
115 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
119 map->offset = offset;
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.
128 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
129 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
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);
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);
152 map->offset += dev->hose->mem_space->start;
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.
158 list = drm_find_matching_map(dev, map);
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,
165 list->map->size = map->size;
168 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
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);
180 if (map->type == _DRM_REGISTERS) {
181 map->handle = ioremap(map->offset, map->size);
183 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
190 list = drm_find_matching_map(dev, map);
192 if(list->map->size != map->size) {
193 DRM_DEBUG("Matching maps of type %d with "
194 "mismatched sizes, (%ld vs %ld)\n",
195 map->type, map->size, list->map->size);
196 list->map->size = map->size;
199 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
203 map->handle = vmalloc_user(map->size);
204 DRM_DEBUG("%lu %d %p\n",
205 map->size, drm_order(map->size), map->handle);
207 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
210 map->offset = (unsigned long)map->handle;
211 if (map->flags & _DRM_CONTAINS_LOCK) {
212 /* Prevent a 2nd X Server from creating a 2nd lock */
213 if (dev->lock.hw_lock != NULL) {
215 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
218 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
222 struct drm_agp_mem *entry;
225 if (!drm_core_has_AGP(dev)) {
226 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
230 map->offset += dev->hose->mem_space->start;
232 /* Note: dev->agp->base may actually be 0 when the DRM
233 * is not in control of AGP space. But if user space is
234 * it should already have added the AGP base itself.
236 map->offset += dev->agp->base;
237 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
239 /* This assumes the DRM is in total control of AGP space.
240 * It's not always the case as AGP can be in the control
241 * of user space (i.e. i810 driver). So this loop will get
242 * skipped and we double check that dev->agp->memory is
243 * actually set as well as being invalid before EPERM'ing
245 list_for_each_entry(entry, &dev->agp->memory, head) {
246 if ((map->offset >= entry->bound) &&
247 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
252 if (!list_empty(&dev->agp->memory) && !valid) {
253 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
256 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
260 case _DRM_SCATTER_GATHER:
262 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
265 map->offset += (unsigned long)dev->sg->virtual;
267 case _DRM_CONSISTENT:
268 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
269 * As we're limiting the address to 2^32-1 (or less),
270 * casting it down to 32 bits is no problem, but we
271 * need to point to a 64bit variable first. */
272 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
274 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
277 map->handle = dmah->vaddr;
278 map->offset = (unsigned long)dmah->busaddr;
282 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
286 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
288 if (map->type == _DRM_REGISTERS)
289 iounmap(map->handle);
290 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
293 memset(list, 0, sizeof(*list));
296 mutex_lock(&dev->struct_mutex);
297 list_add(&list->head, &dev->maplist);
299 /* Assign a 32-bit handle */
300 /* We do it here so that dev->struct_mutex protects the increment */
301 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
303 ret = drm_map_handle(dev, &list->hash, user_token, 0);
305 if (map->type == _DRM_REGISTERS)
306 iounmap(map->handle);
307 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
308 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
309 mutex_unlock(&dev->struct_mutex);
313 list->user_token = list->hash.key << PAGE_SHIFT;
314 mutex_unlock(&dev->struct_mutex);
320 int drm_addmap(struct drm_device * dev, unsigned int offset,
321 unsigned int size, enum drm_map_type type,
322 enum drm_map_flags flags, drm_local_map_t ** map_ptr)
324 struct drm_map_list *list;
327 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
329 *map_ptr = list->map;
333 EXPORT_SYMBOL(drm_addmap);
335 int drm_addmap_ioctl(struct drm_device *dev, void *data,
336 struct drm_file *file_priv)
338 struct drm_map *map = data;
339 struct drm_map_list *maplist;
342 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP))
345 err = drm_addmap_core(dev, map->offset, map->size, map->type,
346 map->flags, &maplist);
351 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
352 map->handle = (void *)(unsigned long)maplist->user_token;
357 * Remove a map private from list and deallocate resources if the mapping
360 * \param inode device inode.
361 * \param file_priv DRM file private.
362 * \param cmd command.
363 * \param arg pointer to a struct drm_map structure.
364 * \return zero on success or a negative value on error.
366 * Searches the map on drm_device::maplist, removes it from the list, see if
367 * its being used, and free any associate resource (such as MTRR's) if it's not
372 int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
374 struct drm_map_list *r_list = NULL, *list_t;
375 drm_dma_handle_t dmah;
378 /* Find the list entry for the map and remove it */
379 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
380 if (r_list->map == map) {
381 list_del(&r_list->head);
382 drm_ht_remove_key(&dev->map_hash,
383 r_list->user_token >> PAGE_SHIFT);
384 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
395 iounmap(map->handle);
397 case _DRM_FRAME_BUFFER:
398 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
400 retcode = mtrr_del(map->mtrr, map->offset, map->size);
401 DRM_DEBUG("mtrr_del=%d\n", retcode);
408 case _DRM_SCATTER_GATHER:
410 case _DRM_CONSISTENT:
411 dmah.vaddr = map->handle;
412 dmah.busaddr = map->offset;
413 dmah.size = map->size;
414 __drm_pci_free(dev, &dmah);
417 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
422 int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
426 mutex_lock(&dev->struct_mutex);
427 ret = drm_rmmap_locked(dev, map);
428 mutex_unlock(&dev->struct_mutex);
433 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
434 * the last close of the device, and this is necessary for cleanup when things
435 * exit uncleanly. Therefore, having userland manually remove mappings seems
436 * like a pointless exercise since they're going away anyway.
438 * One use case might be after addmap is allowed for normal users for SHM and
439 * gets used by drivers that the server doesn't need to care about. This seems
442 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
443 struct drm_file *file_priv)
445 struct drm_map *request = data;
446 drm_local_map_t *map = NULL;
447 struct drm_map_list *r_list;
450 mutex_lock(&dev->struct_mutex);
451 list_for_each_entry(r_list, &dev->maplist, head) {
453 r_list->user_token == (unsigned long)request->handle &&
454 r_list->map->flags & _DRM_REMOVABLE) {
460 /* List has wrapped around to the head pointer, or its empty we didn't
463 if (list_empty(&dev->maplist) || !map) {
464 mutex_unlock(&dev->struct_mutex);
468 /* Register and framebuffer maps are permanent */
469 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
470 mutex_unlock(&dev->struct_mutex);
474 ret = drm_rmmap_locked(dev, map);
476 mutex_unlock(&dev->struct_mutex);
482 * Cleanup after an error on one of the addbufs() functions.
484 * \param dev DRM device.
485 * \param entry buffer entry where the error occurred.
487 * Frees any pages and buffers associated with the given entry.
489 static void drm_cleanup_buf_error(struct drm_device * dev,
490 struct drm_buf_entry * entry)
494 if (entry->seg_count) {
495 for (i = 0; i < entry->seg_count; i++) {
496 if (entry->seglist[i]) {
497 drm_pci_free(dev, entry->seglist[i]);
500 drm_free(entry->seglist,
502 sizeof(*entry->seglist), DRM_MEM_SEGS);
504 entry->seg_count = 0;
507 if (entry->buf_count) {
508 for (i = 0; i < entry->buf_count; i++) {
509 if (entry->buflist[i].dev_private) {
510 drm_free(entry->buflist[i].dev_private,
511 entry->buflist[i].dev_priv_size,
515 drm_free(entry->buflist,
517 sizeof(*entry->buflist), DRM_MEM_BUFS);
519 entry->buf_count = 0;
525 * Add AGP buffers for DMA transfers.
527 * \param dev struct drm_device to which the buffers are to be added.
528 * \param request pointer to a struct drm_buf_desc describing the request.
529 * \return zero on success or a negative number on failure.
531 * After some sanity checks creates a drm_buf structure for each buffer and
532 * reallocates the buffer list of the same size order to accommodate the new
535 int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
537 struct drm_device_dma *dma = dev->dma;
538 struct drm_buf_entry *entry;
539 struct drm_agp_mem *agp_entry;
541 unsigned long offset;
542 unsigned long agp_offset;
551 struct drm_buf **temp_buflist;
556 count = request->count;
557 order = drm_order(request->size);
560 alignment = (request->flags & _DRM_PAGE_ALIGN)
561 ? PAGE_ALIGN(size) : size;
562 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
563 total = PAGE_SIZE << page_order;
566 agp_offset = dev->agp->base + request->agp_start;
568 DRM_DEBUG("count: %d\n", count);
569 DRM_DEBUG("order: %d\n", order);
570 DRM_DEBUG("size: %d\n", size);
571 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
572 DRM_DEBUG("alignment: %d\n", alignment);
573 DRM_DEBUG("page_order: %d\n", page_order);
574 DRM_DEBUG("total: %d\n", total);
576 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
578 if (dev->queue_count)
579 return -EBUSY; /* Not while in use */
581 /* Make sure buffers are located in AGP memory that we own */
583 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
584 if ((agp_offset >= agp_entry->bound) &&
585 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
590 if (!list_empty(&dev->agp->memory) && !valid) {
591 DRM_DEBUG("zone invalid\n");
594 spin_lock(&dev->count_lock);
596 spin_unlock(&dev->count_lock);
599 atomic_inc(&dev->buf_alloc);
600 spin_unlock(&dev->count_lock);
602 mutex_lock(&dev->struct_mutex);
603 entry = &dma->bufs[order];
604 if (entry->buf_count) {
605 mutex_unlock(&dev->struct_mutex);
606 atomic_dec(&dev->buf_alloc);
607 return -ENOMEM; /* May only call once for each order */
610 if (count < 0 || count > 4096) {
611 mutex_unlock(&dev->struct_mutex);
612 atomic_dec(&dev->buf_alloc);
616 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
618 if (!entry->buflist) {
619 mutex_unlock(&dev->struct_mutex);
620 atomic_dec(&dev->buf_alloc);
623 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
625 entry->buf_size = size;
626 entry->page_order = page_order;
630 while (entry->buf_count < count) {
631 buf = &entry->buflist[entry->buf_count];
632 buf->idx = dma->buf_count + entry->buf_count;
633 buf->total = alignment;
637 buf->offset = (dma->byte_count + offset);
638 buf->bus_address = agp_offset + offset;
639 buf->address = (void *)(agp_offset + offset);
643 init_waitqueue_head(&buf->dma_wait);
644 buf->file_priv = NULL;
646 buf->dev_priv_size = dev->driver->dev_priv_size;
647 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
648 if (!buf->dev_private) {
649 /* Set count correctly so we free the proper amount. */
650 entry->buf_count = count;
651 drm_cleanup_buf_error(dev, entry);
652 mutex_unlock(&dev->struct_mutex);
653 atomic_dec(&dev->buf_alloc);
656 memset(buf->dev_private, 0, buf->dev_priv_size);
658 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
662 byte_count += PAGE_SIZE << page_order;
665 DRM_DEBUG("byte_count: %d\n", byte_count);
667 temp_buflist = drm_realloc(dma->buflist,
668 dma->buf_count * sizeof(*dma->buflist),
669 (dma->buf_count + entry->buf_count)
670 * sizeof(*dma->buflist), DRM_MEM_BUFS);
672 /* Free the entry because it isn't valid */
673 drm_cleanup_buf_error(dev, entry);
674 mutex_unlock(&dev->struct_mutex);
675 atomic_dec(&dev->buf_alloc);
678 dma->buflist = temp_buflist;
680 for (i = 0; i < entry->buf_count; i++) {
681 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
684 dma->buf_count += entry->buf_count;
685 dma->seg_count += entry->seg_count;
686 dma->page_count += byte_count >> PAGE_SHIFT;
687 dma->byte_count += byte_count;
689 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
690 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
692 mutex_unlock(&dev->struct_mutex);
694 request->count = entry->buf_count;
695 request->size = size;
697 dma->flags = _DRM_DMA_USE_AGP;
699 atomic_dec(&dev->buf_alloc);
702 EXPORT_SYMBOL(drm_addbufs_agp);
703 #endif /* __OS_HAS_AGP */
705 int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
707 struct drm_device_dma *dma = dev->dma;
713 struct drm_buf_entry *entry;
714 drm_dma_handle_t *dmah;
717 unsigned long offset;
721 unsigned long *temp_pagelist;
722 struct drm_buf **temp_buflist;
724 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
730 if (!capable(CAP_SYS_ADMIN))
733 count = request->count;
734 order = drm_order(request->size);
737 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
738 request->count, request->size, size, order, dev->queue_count);
740 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
742 if (dev->queue_count)
743 return -EBUSY; /* Not while in use */
745 alignment = (request->flags & _DRM_PAGE_ALIGN)
746 ? PAGE_ALIGN(size) : size;
747 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
748 total = PAGE_SIZE << page_order;
750 spin_lock(&dev->count_lock);
752 spin_unlock(&dev->count_lock);
755 atomic_inc(&dev->buf_alloc);
756 spin_unlock(&dev->count_lock);
758 mutex_lock(&dev->struct_mutex);
759 entry = &dma->bufs[order];
760 if (entry->buf_count) {
761 mutex_unlock(&dev->struct_mutex);
762 atomic_dec(&dev->buf_alloc);
763 return -ENOMEM; /* May only call once for each order */
766 if (count < 0 || count > 4096) {
767 mutex_unlock(&dev->struct_mutex);
768 atomic_dec(&dev->buf_alloc);
772 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
774 if (!entry->buflist) {
775 mutex_unlock(&dev->struct_mutex);
776 atomic_dec(&dev->buf_alloc);
779 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
781 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
783 if (!entry->seglist) {
784 drm_free(entry->buflist,
785 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
786 mutex_unlock(&dev->struct_mutex);
787 atomic_dec(&dev->buf_alloc);
790 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
792 /* Keep the original pagelist until we know all the allocations
795 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
796 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
797 if (!temp_pagelist) {
798 drm_free(entry->buflist,
799 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
800 drm_free(entry->seglist,
801 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
802 mutex_unlock(&dev->struct_mutex);
803 atomic_dec(&dev->buf_alloc);
806 memcpy(temp_pagelist,
807 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
808 DRM_DEBUG("pagelist: %d entries\n",
809 dma->page_count + (count << page_order));
811 entry->buf_size = size;
812 entry->page_order = page_order;
816 while (entry->buf_count < count) {
818 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
821 /* Set count correctly so we free the proper amount. */
822 entry->buf_count = count;
823 entry->seg_count = count;
824 drm_cleanup_buf_error(dev, entry);
825 drm_free(temp_pagelist,
826 (dma->page_count + (count << page_order))
827 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
828 mutex_unlock(&dev->struct_mutex);
829 atomic_dec(&dev->buf_alloc);
832 entry->seglist[entry->seg_count++] = dmah;
833 for (i = 0; i < (1 << page_order); i++) {
834 DRM_DEBUG("page %d @ 0x%08lx\n",
835 dma->page_count + page_count,
836 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
837 temp_pagelist[dma->page_count + page_count++]
838 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
841 offset + size <= total && entry->buf_count < count;
842 offset += alignment, ++entry->buf_count) {
843 buf = &entry->buflist[entry->buf_count];
844 buf->idx = dma->buf_count + entry->buf_count;
845 buf->total = alignment;
848 buf->offset = (dma->byte_count + byte_count + offset);
849 buf->address = (void *)(dmah->vaddr + offset);
850 buf->bus_address = dmah->busaddr + offset;
854 init_waitqueue_head(&buf->dma_wait);
855 buf->file_priv = NULL;
857 buf->dev_priv_size = dev->driver->dev_priv_size;
858 buf->dev_private = drm_alloc(buf->dev_priv_size,
860 if (!buf->dev_private) {
861 /* Set count correctly so we free the proper amount. */
862 entry->buf_count = count;
863 entry->seg_count = count;
864 drm_cleanup_buf_error(dev, entry);
865 drm_free(temp_pagelist,
867 (count << page_order))
868 * sizeof(*dma->pagelist),
870 mutex_unlock(&dev->struct_mutex);
871 atomic_dec(&dev->buf_alloc);
874 memset(buf->dev_private, 0, buf->dev_priv_size);
876 DRM_DEBUG("buffer %d @ %p\n",
877 entry->buf_count, buf->address);
879 byte_count += PAGE_SIZE << page_order;
882 temp_buflist = drm_realloc(dma->buflist,
883 dma->buf_count * sizeof(*dma->buflist),
884 (dma->buf_count + entry->buf_count)
885 * sizeof(*dma->buflist), DRM_MEM_BUFS);
887 /* Free the entry because it isn't valid */
888 drm_cleanup_buf_error(dev, entry);
889 drm_free(temp_pagelist,
890 (dma->page_count + (count << page_order))
891 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
892 mutex_unlock(&dev->struct_mutex);
893 atomic_dec(&dev->buf_alloc);
896 dma->buflist = temp_buflist;
898 for (i = 0; i < entry->buf_count; i++) {
899 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
902 /* No allocations failed, so now we can replace the orginal pagelist
905 if (dma->page_count) {
906 drm_free(dma->pagelist,
907 dma->page_count * sizeof(*dma->pagelist),
910 dma->pagelist = temp_pagelist;
912 dma->buf_count += entry->buf_count;
913 dma->seg_count += entry->seg_count;
914 dma->page_count += entry->seg_count << page_order;
915 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
917 mutex_unlock(&dev->struct_mutex);
919 request->count = entry->buf_count;
920 request->size = size;
922 if (request->flags & _DRM_PCI_BUFFER_RO)
923 dma->flags = _DRM_DMA_USE_PCI_RO;
925 atomic_dec(&dev->buf_alloc);
929 EXPORT_SYMBOL(drm_addbufs_pci);
931 static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
933 struct drm_device_dma *dma = dev->dma;
934 struct drm_buf_entry *entry;
936 unsigned long offset;
937 unsigned long agp_offset;
946 struct drm_buf **temp_buflist;
948 if (!drm_core_check_feature(dev, DRIVER_SG))
954 if (!capable(CAP_SYS_ADMIN))
957 count = request->count;
958 order = drm_order(request->size);
961 alignment = (request->flags & _DRM_PAGE_ALIGN)
962 ? PAGE_ALIGN(size) : size;
963 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
964 total = PAGE_SIZE << page_order;
967 agp_offset = request->agp_start;
969 DRM_DEBUG("count: %d\n", count);
970 DRM_DEBUG("order: %d\n", order);
971 DRM_DEBUG("size: %d\n", size);
972 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
973 DRM_DEBUG("alignment: %d\n", alignment);
974 DRM_DEBUG("page_order: %d\n", page_order);
975 DRM_DEBUG("total: %d\n", total);
977 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
979 if (dev->queue_count)
980 return -EBUSY; /* Not while in use */
982 spin_lock(&dev->count_lock);
984 spin_unlock(&dev->count_lock);
987 atomic_inc(&dev->buf_alloc);
988 spin_unlock(&dev->count_lock);
990 mutex_lock(&dev->struct_mutex);
991 entry = &dma->bufs[order];
992 if (entry->buf_count) {
993 mutex_unlock(&dev->struct_mutex);
994 atomic_dec(&dev->buf_alloc);
995 return -ENOMEM; /* May only call once for each order */
998 if (count < 0 || count > 4096) {
999 mutex_unlock(&dev->struct_mutex);
1000 atomic_dec(&dev->buf_alloc);
1004 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1006 if (!entry->buflist) {
1007 mutex_unlock(&dev->struct_mutex);
1008 atomic_dec(&dev->buf_alloc);
1011 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1013 entry->buf_size = size;
1014 entry->page_order = page_order;
1018 while (entry->buf_count < count) {
1019 buf = &entry->buflist[entry->buf_count];
1020 buf->idx = dma->buf_count + entry->buf_count;
1021 buf->total = alignment;
1025 buf->offset = (dma->byte_count + offset);
1026 buf->bus_address = agp_offset + offset;
1027 buf->address = (void *)(agp_offset + offset
1028 + (unsigned long)dev->sg->virtual);
1032 init_waitqueue_head(&buf->dma_wait);
1033 buf->file_priv = NULL;
1035 buf->dev_priv_size = dev->driver->dev_priv_size;
1036 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1037 if (!buf->dev_private) {
1038 /* Set count correctly so we free the proper amount. */
1039 entry->buf_count = count;
1040 drm_cleanup_buf_error(dev, entry);
1041 mutex_unlock(&dev->struct_mutex);
1042 atomic_dec(&dev->buf_alloc);
1046 memset(buf->dev_private, 0, buf->dev_priv_size);
1048 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1050 offset += alignment;
1052 byte_count += PAGE_SIZE << page_order;
1055 DRM_DEBUG("byte_count: %d\n", byte_count);
1057 temp_buflist = drm_realloc(dma->buflist,
1058 dma->buf_count * sizeof(*dma->buflist),
1059 (dma->buf_count + entry->buf_count)
1060 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1061 if (!temp_buflist) {
1062 /* Free the entry because it isn't valid */
1063 drm_cleanup_buf_error(dev, entry);
1064 mutex_unlock(&dev->struct_mutex);
1065 atomic_dec(&dev->buf_alloc);
1068 dma->buflist = temp_buflist;
1070 for (i = 0; i < entry->buf_count; i++) {
1071 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1074 dma->buf_count += entry->buf_count;
1075 dma->seg_count += entry->seg_count;
1076 dma->page_count += byte_count >> PAGE_SHIFT;
1077 dma->byte_count += byte_count;
1079 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1080 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1082 mutex_unlock(&dev->struct_mutex);
1084 request->count = entry->buf_count;
1085 request->size = size;
1087 dma->flags = _DRM_DMA_USE_SG;
1089 atomic_dec(&dev->buf_alloc);
1093 static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
1095 struct drm_device_dma *dma = dev->dma;
1096 struct drm_buf_entry *entry;
1097 struct drm_buf *buf;
1098 unsigned long offset;
1099 unsigned long agp_offset;
1108 struct drm_buf **temp_buflist;
1110 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1116 if (!capable(CAP_SYS_ADMIN))
1119 count = request->count;
1120 order = drm_order(request->size);
1123 alignment = (request->flags & _DRM_PAGE_ALIGN)
1124 ? PAGE_ALIGN(size) : size;
1125 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1126 total = PAGE_SIZE << page_order;
1129 agp_offset = request->agp_start;
1131 DRM_DEBUG("count: %d\n", count);
1132 DRM_DEBUG("order: %d\n", order);
1133 DRM_DEBUG("size: %d\n", size);
1134 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1135 DRM_DEBUG("alignment: %d\n", alignment);
1136 DRM_DEBUG("page_order: %d\n", page_order);
1137 DRM_DEBUG("total: %d\n", total);
1139 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1141 if (dev->queue_count)
1142 return -EBUSY; /* Not while in use */
1144 spin_lock(&dev->count_lock);
1146 spin_unlock(&dev->count_lock);
1149 atomic_inc(&dev->buf_alloc);
1150 spin_unlock(&dev->count_lock);
1152 mutex_lock(&dev->struct_mutex);
1153 entry = &dma->bufs[order];
1154 if (entry->buf_count) {
1155 mutex_unlock(&dev->struct_mutex);
1156 atomic_dec(&dev->buf_alloc);
1157 return -ENOMEM; /* May only call once for each order */
1160 if (count < 0 || count > 4096) {
1161 mutex_unlock(&dev->struct_mutex);
1162 atomic_dec(&dev->buf_alloc);
1166 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1168 if (!entry->buflist) {
1169 mutex_unlock(&dev->struct_mutex);
1170 atomic_dec(&dev->buf_alloc);
1173 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1175 entry->buf_size = size;
1176 entry->page_order = page_order;
1180 while (entry->buf_count < count) {
1181 buf = &entry->buflist[entry->buf_count];
1182 buf->idx = dma->buf_count + entry->buf_count;
1183 buf->total = alignment;
1187 buf->offset = (dma->byte_count + offset);
1188 buf->bus_address = agp_offset + offset;
1189 buf->address = (void *)(agp_offset + offset);
1193 init_waitqueue_head(&buf->dma_wait);
1194 buf->file_priv = NULL;
1196 buf->dev_priv_size = dev->driver->dev_priv_size;
1197 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1198 if (!buf->dev_private) {
1199 /* Set count correctly so we free the proper amount. */
1200 entry->buf_count = count;
1201 drm_cleanup_buf_error(dev, entry);
1202 mutex_unlock(&dev->struct_mutex);
1203 atomic_dec(&dev->buf_alloc);
1206 memset(buf->dev_private, 0, buf->dev_priv_size);
1208 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1210 offset += alignment;
1212 byte_count += PAGE_SIZE << page_order;
1215 DRM_DEBUG("byte_count: %d\n", byte_count);
1217 temp_buflist = drm_realloc(dma->buflist,
1218 dma->buf_count * sizeof(*dma->buflist),
1219 (dma->buf_count + entry->buf_count)
1220 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1221 if (!temp_buflist) {
1222 /* Free the entry because it isn't valid */
1223 drm_cleanup_buf_error(dev, entry);
1224 mutex_unlock(&dev->struct_mutex);
1225 atomic_dec(&dev->buf_alloc);
1228 dma->buflist = temp_buflist;
1230 for (i = 0; i < entry->buf_count; i++) {
1231 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1234 dma->buf_count += entry->buf_count;
1235 dma->seg_count += entry->seg_count;
1236 dma->page_count += byte_count >> PAGE_SHIFT;
1237 dma->byte_count += byte_count;
1239 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1240 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1242 mutex_unlock(&dev->struct_mutex);
1244 request->count = entry->buf_count;
1245 request->size = size;
1247 dma->flags = _DRM_DMA_USE_FB;
1249 atomic_dec(&dev->buf_alloc);
1255 * Add buffers for DMA transfers (ioctl).
1257 * \param inode device inode.
1258 * \param file_priv DRM file private.
1259 * \param cmd command.
1260 * \param arg pointer to a struct drm_buf_desc request.
1261 * \return zero on success or a negative number on failure.
1263 * According with the memory type specified in drm_buf_desc::flags and the
1264 * build options, it dispatches the call either to addbufs_agp(),
1265 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1266 * PCI memory respectively.
1268 int drm_addbufs(struct drm_device *dev, void *data,
1269 struct drm_file *file_priv)
1271 struct drm_buf_desc *request = data;
1274 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1278 if (request->flags & _DRM_AGP_BUFFER)
1279 ret = drm_addbufs_agp(dev, request);
1282 if (request->flags & _DRM_SG_BUFFER)
1283 ret = drm_addbufs_sg(dev, request);
1284 else if (request->flags & _DRM_FB_BUFFER)
1285 ret = drm_addbufs_fb(dev, request);
1287 ret = drm_addbufs_pci(dev, request);
1293 * Get information about the buffer mappings.
1295 * This was originally mean for debugging purposes, or by a sophisticated
1296 * client library to determine how best to use the available buffers (e.g.,
1297 * large buffers can be used for image transfer).
1299 * \param inode device inode.
1300 * \param file_priv DRM file private.
1301 * \param cmd command.
1302 * \param arg pointer to a drm_buf_info structure.
1303 * \return zero on success or a negative number on failure.
1305 * Increments drm_device::buf_use while holding the drm_device::count_lock
1306 * lock, preventing of allocating more buffers after this call. Information
1307 * about each requested buffer is then copied into user space.
1309 int drm_infobufs(struct drm_device *dev, void *data,
1310 struct drm_file *file_priv)
1312 struct drm_device_dma *dma = dev->dma;
1313 struct drm_buf_info *request = data;
1317 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1323 spin_lock(&dev->count_lock);
1324 if (atomic_read(&dev->buf_alloc)) {
1325 spin_unlock(&dev->count_lock);
1328 ++dev->buf_use; /* Can't allocate more after this call */
1329 spin_unlock(&dev->count_lock);
1331 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1332 if (dma->bufs[i].buf_count)
1336 DRM_DEBUG("count = %d\n", count);
1338 if (request->count >= count) {
1339 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1340 if (dma->bufs[i].buf_count) {
1341 struct drm_buf_desc __user *to =
1342 &request->list[count];
1343 struct drm_buf_entry *from = &dma->bufs[i];
1344 struct drm_freelist *list = &dma->bufs[i].freelist;
1345 if (copy_to_user(&to->count,
1347 sizeof(from->buf_count)) ||
1348 copy_to_user(&to->size,
1350 sizeof(from->buf_size)) ||
1351 copy_to_user(&to->low_mark,
1353 sizeof(list->low_mark)) ||
1354 copy_to_user(&to->high_mark,
1356 sizeof(list->high_mark)))
1359 DRM_DEBUG("%d %d %d %d %d\n",
1361 dma->bufs[i].buf_count,
1362 dma->bufs[i].buf_size,
1363 dma->bufs[i].freelist.low_mark,
1364 dma->bufs[i].freelist.high_mark);
1369 request->count = count;
1375 * Specifies a low and high water mark for buffer allocation
1377 * \param inode device inode.
1378 * \param file_priv DRM file private.
1379 * \param cmd command.
1380 * \param arg a pointer to a drm_buf_desc structure.
1381 * \return zero on success or a negative number on failure.
1383 * Verifies that the size order is bounded between the admissible orders and
1384 * updates the respective drm_device_dma::bufs entry low and high water mark.
1386 * \note This ioctl is deprecated and mostly never used.
1388 int drm_markbufs(struct drm_device *dev, void *data,
1389 struct drm_file *file_priv)
1391 struct drm_device_dma *dma = dev->dma;
1392 struct drm_buf_desc *request = data;
1394 struct drm_buf_entry *entry;
1396 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1402 DRM_DEBUG("%d, %d, %d\n",
1403 request->size, request->low_mark, request->high_mark);
1404 order = drm_order(request->size);
1405 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1407 entry = &dma->bufs[order];
1409 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1411 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1414 entry->freelist.low_mark = request->low_mark;
1415 entry->freelist.high_mark = request->high_mark;
1421 * Unreserve the buffers in list, previously reserved using drmDMA.
1423 * \param inode device inode.
1424 * \param file_priv DRM file private.
1425 * \param cmd command.
1426 * \param arg pointer to a drm_buf_free structure.
1427 * \return zero on success or a negative number on failure.
1429 * Calls free_buffer() for each used buffer.
1430 * This function is primarily used for debugging.
1432 int drm_freebufs(struct drm_device *dev, void *data,
1433 struct drm_file *file_priv)
1435 struct drm_device_dma *dma = dev->dma;
1436 struct drm_buf_free *request = data;
1439 struct drm_buf *buf;
1441 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1447 DRM_DEBUG("%d\n", request->count);
1448 for (i = 0; i < request->count; i++) {
1449 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1451 if (idx < 0 || idx >= dma->buf_count) {
1452 DRM_ERROR("Index %d (of %d max)\n",
1453 idx, dma->buf_count - 1);
1456 buf = dma->buflist[idx];
1457 if (buf->file_priv != file_priv) {
1458 DRM_ERROR("Process %d freeing buffer not owned\n",
1459 task_pid_nr(current));
1462 drm_free_buffer(dev, buf);
1469 * Maps all of the DMA buffers into client-virtual space (ioctl).
1471 * \param inode device inode.
1472 * \param file_priv DRM file private.
1473 * \param cmd command.
1474 * \param arg pointer to a drm_buf_map structure.
1475 * \return zero on success or a negative number on failure.
1477 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1478 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1479 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1482 int drm_mapbufs(struct drm_device *dev, void *data,
1483 struct drm_file *file_priv)
1485 struct drm_device_dma *dma = dev->dma;
1488 unsigned long virtual;
1489 unsigned long address;
1490 struct drm_buf_map *request = data;
1493 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1499 spin_lock(&dev->count_lock);
1500 if (atomic_read(&dev->buf_alloc)) {
1501 spin_unlock(&dev->count_lock);
1504 dev->buf_use++; /* Can't allocate more after this call */
1505 spin_unlock(&dev->count_lock);
1507 if (request->count >= dma->buf_count) {
1508 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1509 || (drm_core_check_feature(dev, DRIVER_SG)
1510 && (dma->flags & _DRM_DMA_USE_SG))
1511 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1512 && (dma->flags & _DRM_DMA_USE_FB))) {
1513 struct drm_map *map = dev->agp_buffer_map;
1514 unsigned long token = dev->agp_buffer_token;
1520 down_write(¤t->mm->mmap_sem);
1521 virtual = do_mmap(file_priv->filp, 0, map->size,
1522 PROT_READ | PROT_WRITE,
1525 up_write(¤t->mm->mmap_sem);
1527 down_write(¤t->mm->mmap_sem);
1528 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
1529 PROT_READ | PROT_WRITE,
1531 up_write(¤t->mm->mmap_sem);
1533 if (virtual > -1024UL) {
1535 retcode = (signed long)virtual;
1538 request->virtual = (void __user *)virtual;
1540 for (i = 0; i < dma->buf_count; i++) {
1541 if (copy_to_user(&request->list[i].idx,
1542 &dma->buflist[i]->idx,
1543 sizeof(request->list[0].idx))) {
1547 if (copy_to_user(&request->list[i].total,
1548 &dma->buflist[i]->total,
1549 sizeof(request->list[0].total))) {
1553 if (copy_to_user(&request->list[i].used,
1554 &zero, sizeof(zero))) {
1558 address = virtual + dma->buflist[i]->offset; /* *** */
1559 if (copy_to_user(&request->list[i].address,
1560 &address, sizeof(address))) {
1567 request->count = dma->buf_count;
1568 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1574 * Compute size order. Returns the exponent of the smaller power of two which
1575 * is greater or equal to given number.
1580 * \todo Can be made faster.
1582 int drm_order(unsigned long size)
1587 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1589 if (size & (size - 1))
1594 EXPORT_SYMBOL(drm_order);