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);
49 EXPORT_SYMBOL(drm_get_resource_len);
51 struct drm_map_list *drm_find_matching_map(struct drm_device *dev, drm_local_map_t *map)
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)))) {
64 EXPORT_SYMBOL(drm_find_matching_map);
66 static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
67 unsigned long user_token, int hashed_handle)
69 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);
184 list = drm_find_matching_map(dev, map);
186 if(list->map->size != map->size) {
187 DRM_DEBUG("Matching maps of type %d with "
188 "mismatched sizes, (%ld vs %ld)\n",
189 map->type, map->size, list->map->size);
190 list->map->size = map->size;
193 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
197 map->handle = vmalloc_user(map->size);
198 DRM_DEBUG("%lu %d %p\n",
199 map->size, drm_order(map->size), map->handle);
201 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
204 map->offset = (unsigned long)map->handle;
205 if (map->flags & _DRM_CONTAINS_LOCK) {
206 /* Prevent a 2nd X Server from creating a 2nd lock */
207 if (dev->lock.hw_lock != NULL) {
209 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
212 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
216 struct drm_agp_mem *entry;
219 if (!drm_core_has_AGP(dev)) {
220 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
224 map->offset += dev->hose->mem_space->start;
226 /* In some cases (i810 driver), user space may have already
227 * added the AGP base itself, because dev->agp->base previously
228 * only got set during AGP enable. So, only add the base
229 * address if the map's offset isn't already within the
232 if (map->offset < dev->agp->base ||
233 map->offset > dev->agp->base +
234 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
235 map->offset += dev->agp->base;
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);
259 case _DRM_SCATTER_GATHER:
261 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
264 map->offset += (unsigned long)dev->sg->virtual;
266 case _DRM_CONSISTENT:
267 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
268 * As we're limiting the address to 2^32-1 (or less),
269 * casting it down to 32 bits is no problem, but we
270 * need to point to a 64bit variable first. */
271 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
273 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
276 map->handle = dmah->vaddr;
277 map->offset = (unsigned long)dmah->busaddr;
281 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
285 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
287 if (map->type == _DRM_REGISTERS)
288 iounmap(map->handle);
289 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
292 memset(list, 0, sizeof(*list));
295 mutex_lock(&dev->struct_mutex);
296 list_add(&list->head, &dev->maplist);
298 /* Assign a 32-bit handle */
300 user_token = (map->type == _DRM_SHM) ? (unsigned long) map->handle :
302 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);
393 /* List has wrapped around to the head pointer, or it's empty and we
394 * didn't find anything.
399 iounmap(map->handle);
401 case _DRM_FRAME_BUFFER:
402 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
404 retcode = mtrr_del(map->mtrr, map->offset, map->size);
405 DRM_DEBUG("mtrr_del=%d\n", retcode);
410 dev->sigdata.lock = dev->lock.hw_lock = NULL; /* SHM removed */
413 case _DRM_SCATTER_GATHER:
415 case _DRM_CONSISTENT:
416 dmah.vaddr = map->handle;
417 dmah.busaddr = map->offset;
418 dmah.size = map->size;
419 __drm_pci_free(dev, &dmah);
424 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
428 EXPORT_SYMBOL(drm_rmmap_locked);
430 int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
434 mutex_lock(&dev->struct_mutex);
435 ret = drm_rmmap_locked(dev, map);
436 mutex_unlock(&dev->struct_mutex);
440 EXPORT_SYMBOL(drm_rmmap);
442 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
443 * the last close of the device, and this is necessary for cleanup when things
444 * exit uncleanly. Therefore, having userland manually remove mappings seems
445 * like a pointless exercise since they're going away anyway.
447 * One use case might be after addmap is allowed for normal users for SHM and
448 * gets used by drivers that the server doesn't need to care about. This seems
451 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
452 struct drm_file *file_priv)
454 struct drm_map *request = data;
455 drm_local_map_t *map = NULL;
456 struct drm_map_list *r_list;
459 mutex_lock(&dev->struct_mutex);
460 list_for_each_entry(r_list, &dev->maplist, head) {
462 r_list->user_token == (unsigned long)request->handle &&
463 r_list->map->flags & _DRM_REMOVABLE) {
469 /* List has wrapped around to the head pointer, or its empty we didn't
472 if (list_empty(&dev->maplist) || !map) {
473 mutex_unlock(&dev->struct_mutex);
477 /* Register and framebuffer maps are permanent */
478 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
479 mutex_unlock(&dev->struct_mutex);
483 ret = drm_rmmap_locked(dev, map);
485 mutex_unlock(&dev->struct_mutex);
491 * Cleanup after an error on one of the addbufs() functions.
493 * \param dev DRM device.
494 * \param entry buffer entry where the error occurred.
496 * Frees any pages and buffers associated with the given entry.
498 static void drm_cleanup_buf_error(struct drm_device *dev, struct drm_buf_entry * entry)
502 if (entry->seg_count) {
503 for (i = 0; i < entry->seg_count; i++) {
504 if (entry->seglist[i]) {
505 drm_pci_free(dev, entry->seglist[i]);
508 drm_free(entry->seglist,
510 sizeof(*entry->seglist), DRM_MEM_SEGS);
512 entry->seg_count = 0;
515 if (entry->buf_count) {
516 for (i = 0; i < entry->buf_count; i++) {
517 if (entry->buflist[i].dev_private) {
518 drm_free(entry->buflist[i].dev_private,
519 entry->buflist[i].dev_priv_size,
523 drm_free(entry->buflist,
525 sizeof(*entry->buflist), DRM_MEM_BUFS);
527 entry->buf_count = 0;
533 * Add AGP buffers for DMA transfers
535 * \param dev struct drm_device to which the buffers are to be added.
536 * \param request pointer to a struct drm_buf_desc describing the request.
537 * \return zero on success or a negative number on failure.
539 * After some sanity checks creates a drm_buf structure for each buffer and
540 * reallocates the buffer list of the same size order to accommodate the new
543 int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc * request)
545 struct drm_device_dma *dma = dev->dma;
546 struct drm_buf_entry *entry;
547 struct drm_agp_mem *agp_entry;
549 unsigned long offset;
550 unsigned long agp_offset;
559 struct drm_buf **temp_buflist;
564 count = request->count;
565 order = drm_order(request->size);
568 alignment = (request->flags & _DRM_PAGE_ALIGN)
569 ? PAGE_ALIGN(size) : size;
570 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
571 total = PAGE_SIZE << page_order;
574 agp_offset = dev->agp->base + request->agp_start;
576 DRM_DEBUG("count: %d\n", count);
577 DRM_DEBUG("order: %d\n", order);
578 DRM_DEBUG("size: %d\n", size);
579 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
580 DRM_DEBUG("alignment: %d\n", alignment);
581 DRM_DEBUG("page_order: %d\n", page_order);
582 DRM_DEBUG("total: %d\n", total);
584 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
586 if (dev->queue_count)
587 return -EBUSY; /* Not while in use */
589 /* Make sure buffers are located in AGP memory that we own */
591 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
592 if ((agp_offset >= agp_entry->bound) &&
593 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
598 if (!list_empty(&dev->agp->memory) && !valid) {
599 DRM_DEBUG("zone invalid\n");
602 spin_lock(&dev->count_lock);
604 spin_unlock(&dev->count_lock);
607 atomic_inc(&dev->buf_alloc);
608 spin_unlock(&dev->count_lock);
610 mutex_lock(&dev->struct_mutex);
611 entry = &dma->bufs[order];
612 if (entry->buf_count) {
613 mutex_unlock(&dev->struct_mutex);
614 atomic_dec(&dev->buf_alloc);
615 return -ENOMEM; /* May only call once for each order */
618 if (count < 0 || count > 4096) {
619 mutex_unlock(&dev->struct_mutex);
620 atomic_dec(&dev->buf_alloc);
624 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
626 if (!entry->buflist) {
627 mutex_unlock(&dev->struct_mutex);
628 atomic_dec(&dev->buf_alloc);
631 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
633 entry->buf_size = size;
634 entry->page_order = page_order;
638 while (entry->buf_count < count) {
639 buf = &entry->buflist[entry->buf_count];
640 buf->idx = dma->buf_count + entry->buf_count;
641 buf->total = alignment;
645 buf->offset = (dma->byte_count + offset);
646 buf->bus_address = agp_offset + offset;
647 buf->address = (void *)(agp_offset + offset);
651 init_waitqueue_head(&buf->dma_wait);
652 buf->file_priv = NULL;
654 buf->dev_priv_size = dev->driver->dev_priv_size;
655 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
656 if (!buf->dev_private) {
657 /* Set count correctly so we free the proper amount. */
658 entry->buf_count = count;
659 drm_cleanup_buf_error(dev, entry);
660 mutex_unlock(&dev->struct_mutex);
661 atomic_dec(&dev->buf_alloc);
664 memset(buf->dev_private, 0, buf->dev_priv_size);
666 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
670 byte_count += PAGE_SIZE << page_order;
673 DRM_DEBUG("byte_count: %d\n", byte_count);
675 temp_buflist = drm_realloc(dma->buflist,
676 dma->buf_count * sizeof(*dma->buflist),
677 (dma->buf_count + entry->buf_count)
678 * sizeof(*dma->buflist), DRM_MEM_BUFS);
680 /* Free the entry because it isn't valid */
681 drm_cleanup_buf_error(dev, entry);
682 mutex_unlock(&dev->struct_mutex);
683 atomic_dec(&dev->buf_alloc);
686 dma->buflist = temp_buflist;
688 for (i = 0; i < entry->buf_count; i++) {
689 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
692 dma->buf_count += entry->buf_count;
693 dma->seg_count += entry->seg_count;
694 dma->page_count += byte_count >> PAGE_SHIFT;
695 dma->byte_count += byte_count;
697 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
698 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
700 mutex_unlock(&dev->struct_mutex);
702 request->count = entry->buf_count;
703 request->size = size;
705 dma->flags = _DRM_DMA_USE_AGP;
707 atomic_dec(&dev->buf_alloc);
710 EXPORT_SYMBOL(drm_addbufs_agp);
711 #endif /* __OS_HAS_AGP */
713 int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc * request)
715 struct drm_device_dma *dma = dev->dma;
721 struct drm_buf_entry *entry;
722 drm_dma_handle_t *dmah;
725 unsigned long offset;
729 unsigned long *temp_pagelist;
730 struct drm_buf **temp_buflist;
732 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
738 if (!capable(CAP_SYS_ADMIN))
741 count = request->count;
742 order = drm_order(request->size);
745 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
746 request->count, request->size, size, order, dev->queue_count);
748 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
750 if (dev->queue_count)
751 return -EBUSY; /* Not while in use */
753 alignment = (request->flags & _DRM_PAGE_ALIGN)
754 ? PAGE_ALIGN(size) : size;
755 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
756 total = PAGE_SIZE << page_order;
758 spin_lock(&dev->count_lock);
760 spin_unlock(&dev->count_lock);
763 atomic_inc(&dev->buf_alloc);
764 spin_unlock(&dev->count_lock);
766 mutex_lock(&dev->struct_mutex);
767 entry = &dma->bufs[order];
768 if (entry->buf_count) {
769 mutex_unlock(&dev->struct_mutex);
770 atomic_dec(&dev->buf_alloc);
771 return -ENOMEM; /* May only call once for each order */
774 if (count < 0 || count > 4096) {
775 mutex_unlock(&dev->struct_mutex);
776 atomic_dec(&dev->buf_alloc);
780 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
782 if (!entry->buflist) {
783 mutex_unlock(&dev->struct_mutex);
784 atomic_dec(&dev->buf_alloc);
787 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
789 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
791 if (!entry->seglist) {
792 drm_free(entry->buflist,
793 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
794 mutex_unlock(&dev->struct_mutex);
795 atomic_dec(&dev->buf_alloc);
798 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
800 /* Keep the original pagelist until we know all the allocations
803 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
804 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
805 if (!temp_pagelist) {
806 drm_free(entry->buflist,
807 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
808 drm_free(entry->seglist,
809 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
810 mutex_unlock(&dev->struct_mutex);
811 atomic_dec(&dev->buf_alloc);
814 memcpy(temp_pagelist,
815 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
816 DRM_DEBUG("pagelist: %d entries\n",
817 dma->page_count + (count << page_order));
819 entry->buf_size = size;
820 entry->page_order = page_order;
824 while (entry->buf_count < count) {
826 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
829 /* Set count correctly so we free the proper amount. */
830 entry->buf_count = count;
831 entry->seg_count = count;
832 drm_cleanup_buf_error(dev, entry);
833 drm_free(temp_pagelist,
834 (dma->page_count + (count << page_order))
835 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
836 mutex_unlock(&dev->struct_mutex);
837 atomic_dec(&dev->buf_alloc);
840 entry->seglist[entry->seg_count++] = dmah;
841 for (i = 0; i < (1 << page_order); i++) {
842 DRM_DEBUG("page %d @ 0x%08lx\n",
843 dma->page_count + page_count,
844 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
845 temp_pagelist[dma->page_count + page_count++]
846 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
849 offset + size <= total && entry->buf_count < count;
850 offset += alignment, ++entry->buf_count) {
851 buf = &entry->buflist[entry->buf_count];
852 buf->idx = dma->buf_count + entry->buf_count;
853 buf->total = alignment;
856 buf->offset = (dma->byte_count + byte_count + offset);
857 buf->address = (void *)(dmah->vaddr + offset);
858 buf->bus_address = dmah->busaddr + offset;
862 init_waitqueue_head(&buf->dma_wait);
863 buf->file_priv = NULL;
865 buf->dev_priv_size = dev->driver->dev_priv_size;
866 buf->dev_private = drm_alloc(buf->dev_priv_size,
868 if (!buf->dev_private) {
869 /* Set count correctly so we free the proper amount. */
870 entry->buf_count = count;
871 entry->seg_count = count;
872 drm_cleanup_buf_error(dev, entry);
873 drm_free(temp_pagelist,
875 (count << page_order))
876 * sizeof(*dma->pagelist),
878 mutex_unlock(&dev->struct_mutex);
879 atomic_dec(&dev->buf_alloc);
882 memset(buf->dev_private, 0, buf->dev_priv_size);
884 DRM_DEBUG("buffer %d @ %p\n",
885 entry->buf_count, buf->address);
887 byte_count += PAGE_SIZE << page_order;
890 temp_buflist = drm_realloc(dma->buflist,
891 dma->buf_count * sizeof(*dma->buflist),
892 (dma->buf_count + entry->buf_count)
893 * sizeof(*dma->buflist), DRM_MEM_BUFS);
895 /* Free the entry because it isn't valid */
896 drm_cleanup_buf_error(dev, entry);
897 drm_free(temp_pagelist,
898 (dma->page_count + (count << page_order))
899 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
900 mutex_unlock(&dev->struct_mutex);
901 atomic_dec(&dev->buf_alloc);
904 dma->buflist = temp_buflist;
906 for (i = 0; i < entry->buf_count; i++) {
907 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
910 /* No allocations failed, so now we can replace the orginal pagelist
913 if (dma->page_count) {
914 drm_free(dma->pagelist,
915 dma->page_count * sizeof(*dma->pagelist),
918 dma->pagelist = temp_pagelist;
920 dma->buf_count += entry->buf_count;
921 dma->seg_count += entry->seg_count;
922 dma->page_count += entry->seg_count << page_order;
923 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
925 mutex_unlock(&dev->struct_mutex);
927 request->count = entry->buf_count;
928 request->size = size;
930 if (request->flags & _DRM_PCI_BUFFER_RO)
931 dma->flags = _DRM_DMA_USE_PCI_RO;
933 atomic_dec(&dev->buf_alloc);
937 EXPORT_SYMBOL(drm_addbufs_pci);
939 static int drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc * request)
941 struct drm_device_dma *dma = dev->dma;
942 struct drm_buf_entry *entry;
944 unsigned long offset;
945 unsigned long agp_offset;
954 struct drm_buf **temp_buflist;
956 if (!drm_core_check_feature(dev, DRIVER_SG))
962 if (!capable(CAP_SYS_ADMIN))
965 count = request->count;
966 order = drm_order(request->size);
969 alignment = (request->flags & _DRM_PAGE_ALIGN)
970 ? PAGE_ALIGN(size) : size;
971 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
972 total = PAGE_SIZE << page_order;
975 agp_offset = request->agp_start;
977 DRM_DEBUG("count: %d\n", count);
978 DRM_DEBUG("order: %d\n", order);
979 DRM_DEBUG("size: %d\n", size);
980 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
981 DRM_DEBUG("alignment: %d\n", alignment);
982 DRM_DEBUG("page_order: %d\n", page_order);
983 DRM_DEBUG("total: %d\n", total);
985 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
987 if (dev->queue_count)
988 return -EBUSY; /* Not while in use */
990 spin_lock(&dev->count_lock);
992 spin_unlock(&dev->count_lock);
995 atomic_inc(&dev->buf_alloc);
996 spin_unlock(&dev->count_lock);
998 mutex_lock(&dev->struct_mutex);
999 entry = &dma->bufs[order];
1000 if (entry->buf_count) {
1001 mutex_unlock(&dev->struct_mutex);
1002 atomic_dec(&dev->buf_alloc);
1003 return -ENOMEM; /* May only call once for each order */
1006 if (count < 0 || count > 4096) {
1007 mutex_unlock(&dev->struct_mutex);
1008 atomic_dec(&dev->buf_alloc);
1012 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1014 if (!entry->buflist) {
1015 mutex_unlock(&dev->struct_mutex);
1016 atomic_dec(&dev->buf_alloc);
1019 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1021 entry->buf_size = size;
1022 entry->page_order = page_order;
1026 while (entry->buf_count < count) {
1027 buf = &entry->buflist[entry->buf_count];
1028 buf->idx = dma->buf_count + entry->buf_count;
1029 buf->total = alignment;
1033 buf->offset = (dma->byte_count + offset);
1034 buf->bus_address = agp_offset + offset;
1035 buf->address = (void *)(agp_offset + offset
1036 + (unsigned long)dev->sg->virtual);
1040 init_waitqueue_head(&buf->dma_wait);
1041 buf->file_priv = NULL;
1043 buf->dev_priv_size = dev->driver->dev_priv_size;
1044 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1045 if (!buf->dev_private) {
1046 /* Set count correctly so we free the proper amount. */
1047 entry->buf_count = count;
1048 drm_cleanup_buf_error(dev, entry);
1049 mutex_unlock(&dev->struct_mutex);
1050 atomic_dec(&dev->buf_alloc);
1054 memset(buf->dev_private, 0, buf->dev_priv_size);
1056 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1058 offset += alignment;
1060 byte_count += PAGE_SIZE << page_order;
1063 DRM_DEBUG("byte_count: %d\n", byte_count);
1065 temp_buflist = drm_realloc(dma->buflist,
1066 dma->buf_count * sizeof(*dma->buflist),
1067 (dma->buf_count + entry->buf_count)
1068 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1069 if (!temp_buflist) {
1070 /* Free the entry because it isn't valid */
1071 drm_cleanup_buf_error(dev, entry);
1072 mutex_unlock(&dev->struct_mutex);
1073 atomic_dec(&dev->buf_alloc);
1076 dma->buflist = temp_buflist;
1078 for (i = 0; i < entry->buf_count; i++) {
1079 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1082 dma->buf_count += entry->buf_count;
1083 dma->seg_count += entry->seg_count;
1084 dma->page_count += byte_count >> PAGE_SHIFT;
1085 dma->byte_count += byte_count;
1087 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1088 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1090 mutex_unlock(&dev->struct_mutex);
1092 request->count = entry->buf_count;
1093 request->size = size;
1095 dma->flags = _DRM_DMA_USE_SG;
1097 atomic_dec(&dev->buf_alloc);
1101 int drm_addbufs_fb(struct drm_device *dev, struct drm_buf_desc *request)
1103 struct drm_device_dma *dma = dev->dma;
1104 struct drm_buf_entry *entry;
1105 struct drm_buf *buf;
1106 unsigned long offset;
1107 unsigned long agp_offset;
1116 struct drm_buf **temp_buflist;
1118 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1124 if (!capable(CAP_SYS_ADMIN))
1127 count = request->count;
1128 order = drm_order(request->size);
1131 alignment = (request->flags & _DRM_PAGE_ALIGN)
1132 ? PAGE_ALIGN(size) : size;
1133 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1134 total = PAGE_SIZE << page_order;
1137 agp_offset = request->agp_start;
1139 DRM_DEBUG("count: %d\n", count);
1140 DRM_DEBUG("order: %d\n", order);
1141 DRM_DEBUG("size: %d\n", size);
1142 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1143 DRM_DEBUG("alignment: %d\n", alignment);
1144 DRM_DEBUG("page_order: %d\n", page_order);
1145 DRM_DEBUG("total: %d\n", total);
1147 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1149 if (dev->queue_count)
1150 return -EBUSY; /* Not while in use */
1152 spin_lock(&dev->count_lock);
1154 spin_unlock(&dev->count_lock);
1157 atomic_inc(&dev->buf_alloc);
1158 spin_unlock(&dev->count_lock);
1160 mutex_lock(&dev->struct_mutex);
1161 entry = &dma->bufs[order];
1162 if (entry->buf_count) {
1163 mutex_unlock(&dev->struct_mutex);
1164 atomic_dec(&dev->buf_alloc);
1165 return -ENOMEM; /* May only call once for each order */
1168 if (count < 0 || count > 4096) {
1169 mutex_unlock(&dev->struct_mutex);
1170 atomic_dec(&dev->buf_alloc);
1174 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1176 if (!entry->buflist) {
1177 mutex_unlock(&dev->struct_mutex);
1178 atomic_dec(&dev->buf_alloc);
1181 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1183 entry->buf_size = size;
1184 entry->page_order = page_order;
1188 while (entry->buf_count < count) {
1189 buf = &entry->buflist[entry->buf_count];
1190 buf->idx = dma->buf_count + entry->buf_count;
1191 buf->total = alignment;
1195 buf->offset = (dma->byte_count + offset);
1196 buf->bus_address = agp_offset + offset;
1197 buf->address = (void *)(agp_offset + offset);
1201 init_waitqueue_head(&buf->dma_wait);
1202 buf->file_priv = NULL;
1204 buf->dev_priv_size = dev->driver->dev_priv_size;
1205 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1206 if (!buf->dev_private) {
1207 /* Set count correctly so we free the proper amount. */
1208 entry->buf_count = count;
1209 drm_cleanup_buf_error(dev, entry);
1210 mutex_unlock(&dev->struct_mutex);
1211 atomic_dec(&dev->buf_alloc);
1214 memset(buf->dev_private, 0, buf->dev_priv_size);
1216 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1218 offset += alignment;
1220 byte_count += PAGE_SIZE << page_order;
1223 DRM_DEBUG("byte_count: %d\n", byte_count);
1225 temp_buflist = drm_realloc(dma->buflist,
1226 dma->buf_count * sizeof(*dma->buflist),
1227 (dma->buf_count + entry->buf_count)
1228 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1229 if (!temp_buflist) {
1230 /* Free the entry because it isn't valid */
1231 drm_cleanup_buf_error(dev, entry);
1232 mutex_unlock(&dev->struct_mutex);
1233 atomic_dec(&dev->buf_alloc);
1236 dma->buflist = temp_buflist;
1238 for (i = 0; i < entry->buf_count; i++) {
1239 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1242 dma->buf_count += entry->buf_count;
1243 dma->seg_count += entry->seg_count;
1244 dma->page_count += byte_count >> PAGE_SHIFT;
1245 dma->byte_count += byte_count;
1247 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1248 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1250 mutex_unlock(&dev->struct_mutex);
1252 request->count = entry->buf_count;
1253 request->size = size;
1255 dma->flags = _DRM_DMA_USE_FB;
1257 atomic_dec(&dev->buf_alloc);
1260 EXPORT_SYMBOL(drm_addbufs_fb);
1264 * Add buffers for DMA transfers (ioctl).
1266 * \param inode device inode.
1267 * \param file_priv DRM file private.
1268 * \param cmd command.
1269 * \param arg pointer to a struct drm_buf_desc request.
1270 * \return zero on success or a negative number on failure.
1272 * According with the memory type specified in drm_buf_desc::flags and the
1273 * build options, it dispatches the call either to addbufs_agp(),
1274 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1275 * PCI memory respectively.
1277 int drm_addbufs(struct drm_device *dev, void *data,
1278 struct drm_file *file_priv)
1280 struct drm_buf_desc *request = data;
1283 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1287 if (request->flags & _DRM_AGP_BUFFER)
1288 ret = drm_addbufs_agp(dev, request);
1291 if (request->flags & _DRM_SG_BUFFER)
1292 ret = drm_addbufs_sg(dev, request);
1293 else if (request->flags & _DRM_FB_BUFFER)
1294 ret = drm_addbufs_fb(dev, request);
1296 ret = drm_addbufs_pci(dev, request);
1302 * Get information about the buffer mappings.
1304 * This was originally mean for debugging purposes, or by a sophisticated
1305 * client library to determine how best to use the available buffers (e.g.,
1306 * large buffers can be used for image transfer).
1308 * \param inode device inode.
1309 * \param file_priv DRM file private.
1310 * \param cmd command.
1311 * \param arg pointer to a drm_buf_info structure.
1312 * \return zero on success or a negative number on failure.
1314 * Increments drm_device::buf_use while holding the drm_device::count_lock
1315 * lock, preventing of allocating more buffers after this call. Information
1316 * about each requested buffer is then copied into user space.
1318 int drm_infobufs(struct drm_device *dev, void *data,
1319 struct drm_file *file_priv)
1321 struct drm_device_dma *dma = dev->dma;
1322 struct drm_buf_info *request = data;
1326 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1332 spin_lock(&dev->count_lock);
1333 if (atomic_read(&dev->buf_alloc)) {
1334 spin_unlock(&dev->count_lock);
1337 ++dev->buf_use; /* Can't allocate more after this call */
1338 spin_unlock(&dev->count_lock);
1340 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1341 if (dma->bufs[i].buf_count)
1345 DRM_DEBUG("count = %d\n", count);
1347 if (request->count >= count) {
1348 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1349 if (dma->bufs[i].buf_count) {
1350 struct drm_buf_desc __user *to =
1351 &request->list[count];
1352 struct drm_buf_entry *from = &dma->bufs[i];
1353 struct drm_freelist *list = &dma->bufs[i].freelist;
1354 if (copy_to_user(&to->count,
1356 sizeof(from->buf_count)) ||
1357 copy_to_user(&to->size,
1359 sizeof(from->buf_size)) ||
1360 copy_to_user(&to->low_mark,
1362 sizeof(list->low_mark)) ||
1363 copy_to_user(&to->high_mark,
1365 sizeof(list->high_mark)))
1368 DRM_DEBUG("%d %d %d %d %d\n",
1370 dma->bufs[i].buf_count,
1371 dma->bufs[i].buf_size,
1372 dma->bufs[i].freelist.low_mark,
1373 dma->bufs[i].freelist.high_mark);
1378 request->count = count;
1384 * Specifies a low and high water mark for buffer allocation
1386 * \param inode device inode.
1387 * \param file_priv DRM file private.
1388 * \param cmd command.
1389 * \param arg a pointer to a drm_buf_desc structure.
1390 * \return zero on success or a negative number on failure.
1392 * Verifies that the size order is bounded between the admissible orders and
1393 * updates the respective drm_device_dma::bufs entry low and high water mark.
1395 * \note This ioctl is deprecated and mostly never used.
1397 int drm_markbufs(struct drm_device *dev, void *data,
1398 struct drm_file *file_priv)
1400 struct drm_device_dma *dma = dev->dma;
1401 struct drm_buf_desc *request = data;
1403 struct drm_buf_entry *entry;
1405 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1411 DRM_DEBUG("%d, %d, %d\n",
1412 request->size, request->low_mark, request->high_mark);
1413 order = drm_order(request->size);
1414 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1416 entry = &dma->bufs[order];
1418 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1420 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1423 entry->freelist.low_mark = request->low_mark;
1424 entry->freelist.high_mark = request->high_mark;
1430 * Unreserve the buffers in list, previously reserved using drmDMA.
1432 * \param inode device inode.
1433 * \param file_priv DRM file private.
1434 * \param cmd command.
1435 * \param arg pointer to a drm_buf_free structure.
1436 * \return zero on success or a negative number on failure.
1438 * Calls free_buffer() for each used buffer.
1439 * This function is primarily used for debugging.
1441 int drm_freebufs(struct drm_device *dev, void *data,
1442 struct drm_file *file_priv)
1444 struct drm_device_dma *dma = dev->dma;
1445 struct drm_buf_free *request = data;
1448 struct drm_buf *buf;
1450 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1456 DRM_DEBUG("%d\n", request->count);
1457 for (i = 0; i < request->count; i++) {
1458 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1460 if (idx < 0 || idx >= dma->buf_count) {
1461 DRM_ERROR("Index %d (of %d max)\n",
1462 idx, dma->buf_count - 1);
1465 buf = dma->buflist[idx];
1466 if (buf->file_priv != file_priv) {
1467 DRM_ERROR("Process %d freeing buffer not owned\n",
1471 drm_free_buffer(dev, buf);
1478 * Maps all of the DMA buffers into client-virtual space (ioctl).
1480 * \param inode device inode.
1481 * \param file_priv DRM file private.
1482 * \param cmd command.
1483 * \param arg pointer to a drm_buf_map structure.
1484 * \return zero on success or a negative number on failure.
1486 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1487 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1488 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1491 int drm_mapbufs(struct drm_device *dev, void *data,
1492 struct drm_file *file_priv)
1494 struct drm_device_dma *dma = dev->dma;
1497 unsigned long virtual;
1498 unsigned long address;
1499 struct drm_buf_map *request = data;
1502 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1508 spin_lock(&dev->count_lock);
1509 if (atomic_read(&dev->buf_alloc)) {
1510 spin_unlock(&dev->count_lock);
1513 dev->buf_use++; /* Can't allocate more after this call */
1514 spin_unlock(&dev->count_lock);
1516 if (request->count >= dma->buf_count) {
1517 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1518 || (drm_core_check_feature(dev, DRIVER_SG)
1519 && (dma->flags & _DRM_DMA_USE_SG))
1520 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1521 && (dma->flags & _DRM_DMA_USE_FB))) {
1522 struct drm_map *map = dev->agp_buffer_map;
1523 unsigned long token = dev->agp_buffer_token;
1529 down_write(¤t->mm->mmap_sem);
1530 virtual = do_mmap(file_priv->filp, 0, map->size,
1531 PROT_READ | PROT_WRITE,
1534 up_write(¤t->mm->mmap_sem);
1536 down_write(¤t->mm->mmap_sem);
1537 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
1538 PROT_READ | PROT_WRITE,
1540 up_write(¤t->mm->mmap_sem);
1542 if (virtual > -1024UL) {
1544 retcode = (signed long)virtual;
1547 request->virtual = (void __user *)virtual;
1549 for (i = 0; i < dma->buf_count; i++) {
1550 if (copy_to_user(&request->list[i].idx,
1551 &dma->buflist[i]->idx,
1552 sizeof(request->list[0].idx))) {
1556 if (copy_to_user(&request->list[i].total,
1557 &dma->buflist[i]->total,
1558 sizeof(request->list[0].total))) {
1562 if (copy_to_user(&request->list[i].used,
1563 &zero, sizeof(zero))) {
1567 address = virtual + dma->buflist[i]->offset; /* *** */
1568 if (copy_to_user(&request->list[i].address,
1569 &address, sizeof(address))) {
1576 request->count = dma->buf_count;
1577 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1583 * Compute size order. Returns the exponent of the smaller power of two which
1584 * is greater or equal to given number.
1589 * \todo Can be made faster.
1591 int drm_order(unsigned long size)
1596 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1598 if (size & (size - 1))
1603 EXPORT_SYMBOL(drm_order);