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 inode *inode, struct drm_file *file_priv,
336 unsigned int cmd, unsigned long arg)
338 struct drm_device *dev = file_priv->head->dev;
340 struct drm_map_list *maplist;
341 struct drm_map __user *argp = (void __user *)arg;
344 if (copy_from_user(&map, argp, sizeof(map))) {
348 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
351 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
357 if (copy_to_user(argp, maplist->map, sizeof(struct drm_map)))
360 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
361 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
367 * Remove a map private from list and deallocate resources if the mapping
370 * \param inode device inode.
371 * \param file_priv DRM file private.
372 * \param cmd command.
373 * \param arg pointer to a struct drm_map structure.
374 * \return zero on success or a negative value on error.
376 * Searches the map on drm_device::maplist, removes it from the list, see if
377 * its being used, and free any associate resource (such as MTRR's) if it's not
382 int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
384 struct drm_map_list *r_list = NULL, *list_t;
385 drm_dma_handle_t dmah;
388 /* Find the list entry for the map and remove it */
389 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
390 if (r_list->map == map) {
391 list_del(&r_list->head);
392 drm_ht_remove_key(&dev->map_hash,
393 r_list->user_token >> PAGE_SHIFT);
394 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
405 iounmap(map->handle);
407 case _DRM_FRAME_BUFFER:
408 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
410 retcode = mtrr_del(map->mtrr, map->offset, map->size);
411 DRM_DEBUG("mtrr_del=%d\n", retcode);
418 case _DRM_SCATTER_GATHER:
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);
427 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
432 int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
436 mutex_lock(&dev->struct_mutex);
437 ret = drm_rmmap_locked(dev, map);
438 mutex_unlock(&dev->struct_mutex);
443 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
444 * the last close of the device, and this is necessary for cleanup when things
445 * exit uncleanly. Therefore, having userland manually remove mappings seems
446 * like a pointless exercise since they're going away anyway.
448 * One use case might be after addmap is allowed for normal users for SHM and
449 * gets used by drivers that the server doesn't need to care about. This seems
452 int drm_rmmap_ioctl(struct inode *inode, struct drm_file *file_priv,
453 unsigned int cmd, unsigned long arg)
455 struct drm_device *dev = file_priv->head->dev;
456 struct drm_map request;
457 drm_local_map_t *map = NULL;
458 struct drm_map_list *r_list;
461 if (copy_from_user(&request, (struct drm_map __user *) arg, sizeof(request))) {
465 mutex_lock(&dev->struct_mutex);
466 list_for_each_entry(r_list, &dev->maplist, head) {
468 r_list->user_token == (unsigned long)request.handle &&
469 r_list->map->flags & _DRM_REMOVABLE) {
475 /* List has wrapped around to the head pointer, or its empty we didn't
478 if (list_empty(&dev->maplist) || !map) {
479 mutex_unlock(&dev->struct_mutex);
483 /* Register and framebuffer maps are permanent */
484 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
485 mutex_unlock(&dev->struct_mutex);
489 ret = drm_rmmap_locked(dev, map);
491 mutex_unlock(&dev->struct_mutex);
497 * Cleanup after an error on one of the addbufs() functions.
499 * \param dev DRM device.
500 * \param entry buffer entry where the error occurred.
502 * Frees any pages and buffers associated with the given entry.
504 static void drm_cleanup_buf_error(struct drm_device * dev,
505 struct drm_buf_entry * entry)
509 if (entry->seg_count) {
510 for (i = 0; i < entry->seg_count; i++) {
511 if (entry->seglist[i]) {
512 drm_pci_free(dev, entry->seglist[i]);
515 drm_free(entry->seglist,
517 sizeof(*entry->seglist), DRM_MEM_SEGS);
519 entry->seg_count = 0;
522 if (entry->buf_count) {
523 for (i = 0; i < entry->buf_count; i++) {
524 if (entry->buflist[i].dev_private) {
525 drm_free(entry->buflist[i].dev_private,
526 entry->buflist[i].dev_priv_size,
530 drm_free(entry->buflist,
532 sizeof(*entry->buflist), DRM_MEM_BUFS);
534 entry->buf_count = 0;
540 * Add AGP buffers for DMA transfers.
542 * \param dev struct drm_device to which the buffers are to be added.
543 * \param request pointer to a struct drm_buf_desc describing the request.
544 * \return zero on success or a negative number on failure.
546 * After some sanity checks creates a drm_buf structure for each buffer and
547 * reallocates the buffer list of the same size order to accommodate the new
550 int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
552 struct drm_device_dma *dma = dev->dma;
553 struct drm_buf_entry *entry;
554 struct drm_agp_mem *agp_entry;
556 unsigned long offset;
557 unsigned long agp_offset;
566 struct drm_buf **temp_buflist;
571 count = request->count;
572 order = drm_order(request->size);
575 alignment = (request->flags & _DRM_PAGE_ALIGN)
576 ? PAGE_ALIGN(size) : size;
577 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
578 total = PAGE_SIZE << page_order;
581 agp_offset = dev->agp->base + request->agp_start;
583 DRM_DEBUG("count: %d\n", count);
584 DRM_DEBUG("order: %d\n", order);
585 DRM_DEBUG("size: %d\n", size);
586 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
587 DRM_DEBUG("alignment: %d\n", alignment);
588 DRM_DEBUG("page_order: %d\n", page_order);
589 DRM_DEBUG("total: %d\n", total);
591 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
593 if (dev->queue_count)
594 return -EBUSY; /* Not while in use */
596 /* Make sure buffers are located in AGP memory that we own */
598 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
599 if ((agp_offset >= agp_entry->bound) &&
600 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
605 if (!list_empty(&dev->agp->memory) && !valid) {
606 DRM_DEBUG("zone invalid\n");
609 spin_lock(&dev->count_lock);
611 spin_unlock(&dev->count_lock);
614 atomic_inc(&dev->buf_alloc);
615 spin_unlock(&dev->count_lock);
617 mutex_lock(&dev->struct_mutex);
618 entry = &dma->bufs[order];
619 if (entry->buf_count) {
620 mutex_unlock(&dev->struct_mutex);
621 atomic_dec(&dev->buf_alloc);
622 return -ENOMEM; /* May only call once for each order */
625 if (count < 0 || count > 4096) {
626 mutex_unlock(&dev->struct_mutex);
627 atomic_dec(&dev->buf_alloc);
631 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
633 if (!entry->buflist) {
634 mutex_unlock(&dev->struct_mutex);
635 atomic_dec(&dev->buf_alloc);
638 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
640 entry->buf_size = size;
641 entry->page_order = page_order;
645 while (entry->buf_count < count) {
646 buf = &entry->buflist[entry->buf_count];
647 buf->idx = dma->buf_count + entry->buf_count;
648 buf->total = alignment;
652 buf->offset = (dma->byte_count + offset);
653 buf->bus_address = agp_offset + offset;
654 buf->address = (void *)(agp_offset + offset);
658 init_waitqueue_head(&buf->dma_wait);
659 buf->file_priv = NULL;
661 buf->dev_priv_size = dev->driver->dev_priv_size;
662 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
663 if (!buf->dev_private) {
664 /* Set count correctly so we free the proper amount. */
665 entry->buf_count = count;
666 drm_cleanup_buf_error(dev, entry);
667 mutex_unlock(&dev->struct_mutex);
668 atomic_dec(&dev->buf_alloc);
671 memset(buf->dev_private, 0, buf->dev_priv_size);
673 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
677 byte_count += PAGE_SIZE << page_order;
680 DRM_DEBUG("byte_count: %d\n", byte_count);
682 temp_buflist = drm_realloc(dma->buflist,
683 dma->buf_count * sizeof(*dma->buflist),
684 (dma->buf_count + entry->buf_count)
685 * sizeof(*dma->buflist), DRM_MEM_BUFS);
687 /* Free the entry because it isn't valid */
688 drm_cleanup_buf_error(dev, entry);
689 mutex_unlock(&dev->struct_mutex);
690 atomic_dec(&dev->buf_alloc);
693 dma->buflist = temp_buflist;
695 for (i = 0; i < entry->buf_count; i++) {
696 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
699 dma->buf_count += entry->buf_count;
700 dma->seg_count += entry->seg_count;
701 dma->page_count += byte_count >> PAGE_SHIFT;
702 dma->byte_count += byte_count;
704 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
705 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
707 mutex_unlock(&dev->struct_mutex);
709 request->count = entry->buf_count;
710 request->size = size;
712 dma->flags = _DRM_DMA_USE_AGP;
714 atomic_dec(&dev->buf_alloc);
717 EXPORT_SYMBOL(drm_addbufs_agp);
718 #endif /* __OS_HAS_AGP */
720 int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
722 struct drm_device_dma *dma = dev->dma;
728 struct drm_buf_entry *entry;
729 drm_dma_handle_t *dmah;
732 unsigned long offset;
736 unsigned long *temp_pagelist;
737 struct drm_buf **temp_buflist;
739 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
745 if (!capable(CAP_SYS_ADMIN))
748 count = request->count;
749 order = drm_order(request->size);
752 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
753 request->count, request->size, size, order, dev->queue_count);
755 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
757 if (dev->queue_count)
758 return -EBUSY; /* Not while in use */
760 alignment = (request->flags & _DRM_PAGE_ALIGN)
761 ? PAGE_ALIGN(size) : size;
762 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
763 total = PAGE_SIZE << page_order;
765 spin_lock(&dev->count_lock);
767 spin_unlock(&dev->count_lock);
770 atomic_inc(&dev->buf_alloc);
771 spin_unlock(&dev->count_lock);
773 mutex_lock(&dev->struct_mutex);
774 entry = &dma->bufs[order];
775 if (entry->buf_count) {
776 mutex_unlock(&dev->struct_mutex);
777 atomic_dec(&dev->buf_alloc);
778 return -ENOMEM; /* May only call once for each order */
781 if (count < 0 || count > 4096) {
782 mutex_unlock(&dev->struct_mutex);
783 atomic_dec(&dev->buf_alloc);
787 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
789 if (!entry->buflist) {
790 mutex_unlock(&dev->struct_mutex);
791 atomic_dec(&dev->buf_alloc);
794 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
796 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
798 if (!entry->seglist) {
799 drm_free(entry->buflist,
800 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
801 mutex_unlock(&dev->struct_mutex);
802 atomic_dec(&dev->buf_alloc);
805 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
807 /* Keep the original pagelist until we know all the allocations
810 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
811 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
812 if (!temp_pagelist) {
813 drm_free(entry->buflist,
814 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
815 drm_free(entry->seglist,
816 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
817 mutex_unlock(&dev->struct_mutex);
818 atomic_dec(&dev->buf_alloc);
821 memcpy(temp_pagelist,
822 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
823 DRM_DEBUG("pagelist: %d entries\n",
824 dma->page_count + (count << page_order));
826 entry->buf_size = size;
827 entry->page_order = page_order;
831 while (entry->buf_count < count) {
833 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
836 /* Set count correctly so we free the proper amount. */
837 entry->buf_count = count;
838 entry->seg_count = count;
839 drm_cleanup_buf_error(dev, entry);
840 drm_free(temp_pagelist,
841 (dma->page_count + (count << page_order))
842 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
843 mutex_unlock(&dev->struct_mutex);
844 atomic_dec(&dev->buf_alloc);
847 entry->seglist[entry->seg_count++] = dmah;
848 for (i = 0; i < (1 << page_order); i++) {
849 DRM_DEBUG("page %d @ 0x%08lx\n",
850 dma->page_count + page_count,
851 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
852 temp_pagelist[dma->page_count + page_count++]
853 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
856 offset + size <= total && entry->buf_count < count;
857 offset += alignment, ++entry->buf_count) {
858 buf = &entry->buflist[entry->buf_count];
859 buf->idx = dma->buf_count + entry->buf_count;
860 buf->total = alignment;
863 buf->offset = (dma->byte_count + byte_count + offset);
864 buf->address = (void *)(dmah->vaddr + offset);
865 buf->bus_address = dmah->busaddr + offset;
869 init_waitqueue_head(&buf->dma_wait);
870 buf->file_priv = NULL;
872 buf->dev_priv_size = dev->driver->dev_priv_size;
873 buf->dev_private = drm_alloc(buf->dev_priv_size,
875 if (!buf->dev_private) {
876 /* Set count correctly so we free the proper amount. */
877 entry->buf_count = count;
878 entry->seg_count = count;
879 drm_cleanup_buf_error(dev, entry);
880 drm_free(temp_pagelist,
882 (count << page_order))
883 * sizeof(*dma->pagelist),
885 mutex_unlock(&dev->struct_mutex);
886 atomic_dec(&dev->buf_alloc);
889 memset(buf->dev_private, 0, buf->dev_priv_size);
891 DRM_DEBUG("buffer %d @ %p\n",
892 entry->buf_count, buf->address);
894 byte_count += PAGE_SIZE << page_order;
897 temp_buflist = drm_realloc(dma->buflist,
898 dma->buf_count * sizeof(*dma->buflist),
899 (dma->buf_count + entry->buf_count)
900 * sizeof(*dma->buflist), DRM_MEM_BUFS);
902 /* Free the entry because it isn't valid */
903 drm_cleanup_buf_error(dev, entry);
904 drm_free(temp_pagelist,
905 (dma->page_count + (count << page_order))
906 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
907 mutex_unlock(&dev->struct_mutex);
908 atomic_dec(&dev->buf_alloc);
911 dma->buflist = temp_buflist;
913 for (i = 0; i < entry->buf_count; i++) {
914 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
917 /* No allocations failed, so now we can replace the orginal pagelist
920 if (dma->page_count) {
921 drm_free(dma->pagelist,
922 dma->page_count * sizeof(*dma->pagelist),
925 dma->pagelist = temp_pagelist;
927 dma->buf_count += entry->buf_count;
928 dma->seg_count += entry->seg_count;
929 dma->page_count += entry->seg_count << page_order;
930 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
932 mutex_unlock(&dev->struct_mutex);
934 request->count = entry->buf_count;
935 request->size = size;
937 if (request->flags & _DRM_PCI_BUFFER_RO)
938 dma->flags = _DRM_DMA_USE_PCI_RO;
940 atomic_dec(&dev->buf_alloc);
944 EXPORT_SYMBOL(drm_addbufs_pci);
946 static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
948 struct drm_device_dma *dma = dev->dma;
949 struct drm_buf_entry *entry;
951 unsigned long offset;
952 unsigned long agp_offset;
961 struct drm_buf **temp_buflist;
963 if (!drm_core_check_feature(dev, DRIVER_SG))
969 if (!capable(CAP_SYS_ADMIN))
972 count = request->count;
973 order = drm_order(request->size);
976 alignment = (request->flags & _DRM_PAGE_ALIGN)
977 ? PAGE_ALIGN(size) : size;
978 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
979 total = PAGE_SIZE << page_order;
982 agp_offset = request->agp_start;
984 DRM_DEBUG("count: %d\n", count);
985 DRM_DEBUG("order: %d\n", order);
986 DRM_DEBUG("size: %d\n", size);
987 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
988 DRM_DEBUG("alignment: %d\n", alignment);
989 DRM_DEBUG("page_order: %d\n", page_order);
990 DRM_DEBUG("total: %d\n", total);
992 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
994 if (dev->queue_count)
995 return -EBUSY; /* Not while in use */
997 spin_lock(&dev->count_lock);
999 spin_unlock(&dev->count_lock);
1002 atomic_inc(&dev->buf_alloc);
1003 spin_unlock(&dev->count_lock);
1005 mutex_lock(&dev->struct_mutex);
1006 entry = &dma->bufs[order];
1007 if (entry->buf_count) {
1008 mutex_unlock(&dev->struct_mutex);
1009 atomic_dec(&dev->buf_alloc);
1010 return -ENOMEM; /* May only call once for each order */
1013 if (count < 0 || count > 4096) {
1014 mutex_unlock(&dev->struct_mutex);
1015 atomic_dec(&dev->buf_alloc);
1019 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1021 if (!entry->buflist) {
1022 mutex_unlock(&dev->struct_mutex);
1023 atomic_dec(&dev->buf_alloc);
1026 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1028 entry->buf_size = size;
1029 entry->page_order = page_order;
1033 while (entry->buf_count < count) {
1034 buf = &entry->buflist[entry->buf_count];
1035 buf->idx = dma->buf_count + entry->buf_count;
1036 buf->total = alignment;
1040 buf->offset = (dma->byte_count + offset);
1041 buf->bus_address = agp_offset + offset;
1042 buf->address = (void *)(agp_offset + offset
1043 + (unsigned long)dev->sg->virtual);
1047 init_waitqueue_head(&buf->dma_wait);
1048 buf->file_priv = NULL;
1050 buf->dev_priv_size = dev->driver->dev_priv_size;
1051 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1052 if (!buf->dev_private) {
1053 /* Set count correctly so we free the proper amount. */
1054 entry->buf_count = count;
1055 drm_cleanup_buf_error(dev, entry);
1056 mutex_unlock(&dev->struct_mutex);
1057 atomic_dec(&dev->buf_alloc);
1061 memset(buf->dev_private, 0, buf->dev_priv_size);
1063 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1065 offset += alignment;
1067 byte_count += PAGE_SIZE << page_order;
1070 DRM_DEBUG("byte_count: %d\n", byte_count);
1072 temp_buflist = drm_realloc(dma->buflist,
1073 dma->buf_count * sizeof(*dma->buflist),
1074 (dma->buf_count + entry->buf_count)
1075 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1076 if (!temp_buflist) {
1077 /* Free the entry because it isn't valid */
1078 drm_cleanup_buf_error(dev, entry);
1079 mutex_unlock(&dev->struct_mutex);
1080 atomic_dec(&dev->buf_alloc);
1083 dma->buflist = temp_buflist;
1085 for (i = 0; i < entry->buf_count; i++) {
1086 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1089 dma->buf_count += entry->buf_count;
1090 dma->seg_count += entry->seg_count;
1091 dma->page_count += byte_count >> PAGE_SHIFT;
1092 dma->byte_count += byte_count;
1094 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1095 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1097 mutex_unlock(&dev->struct_mutex);
1099 request->count = entry->buf_count;
1100 request->size = size;
1102 dma->flags = _DRM_DMA_USE_SG;
1104 atomic_dec(&dev->buf_alloc);
1108 static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
1110 struct drm_device_dma *dma = dev->dma;
1111 struct drm_buf_entry *entry;
1112 struct drm_buf *buf;
1113 unsigned long offset;
1114 unsigned long agp_offset;
1123 struct drm_buf **temp_buflist;
1125 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1131 if (!capable(CAP_SYS_ADMIN))
1134 count = request->count;
1135 order = drm_order(request->size);
1138 alignment = (request->flags & _DRM_PAGE_ALIGN)
1139 ? PAGE_ALIGN(size) : size;
1140 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1141 total = PAGE_SIZE << page_order;
1144 agp_offset = request->agp_start;
1146 DRM_DEBUG("count: %d\n", count);
1147 DRM_DEBUG("order: %d\n", order);
1148 DRM_DEBUG("size: %d\n", size);
1149 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1150 DRM_DEBUG("alignment: %d\n", alignment);
1151 DRM_DEBUG("page_order: %d\n", page_order);
1152 DRM_DEBUG("total: %d\n", total);
1154 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1156 if (dev->queue_count)
1157 return -EBUSY; /* Not while in use */
1159 spin_lock(&dev->count_lock);
1161 spin_unlock(&dev->count_lock);
1164 atomic_inc(&dev->buf_alloc);
1165 spin_unlock(&dev->count_lock);
1167 mutex_lock(&dev->struct_mutex);
1168 entry = &dma->bufs[order];
1169 if (entry->buf_count) {
1170 mutex_unlock(&dev->struct_mutex);
1171 atomic_dec(&dev->buf_alloc);
1172 return -ENOMEM; /* May only call once for each order */
1175 if (count < 0 || count > 4096) {
1176 mutex_unlock(&dev->struct_mutex);
1177 atomic_dec(&dev->buf_alloc);
1181 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1183 if (!entry->buflist) {
1184 mutex_unlock(&dev->struct_mutex);
1185 atomic_dec(&dev->buf_alloc);
1188 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1190 entry->buf_size = size;
1191 entry->page_order = page_order;
1195 while (entry->buf_count < count) {
1196 buf = &entry->buflist[entry->buf_count];
1197 buf->idx = dma->buf_count + entry->buf_count;
1198 buf->total = alignment;
1202 buf->offset = (dma->byte_count + offset);
1203 buf->bus_address = agp_offset + offset;
1204 buf->address = (void *)(agp_offset + offset);
1208 init_waitqueue_head(&buf->dma_wait);
1209 buf->file_priv = NULL;
1211 buf->dev_priv_size = dev->driver->dev_priv_size;
1212 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1213 if (!buf->dev_private) {
1214 /* Set count correctly so we free the proper amount. */
1215 entry->buf_count = count;
1216 drm_cleanup_buf_error(dev, entry);
1217 mutex_unlock(&dev->struct_mutex);
1218 atomic_dec(&dev->buf_alloc);
1221 memset(buf->dev_private, 0, buf->dev_priv_size);
1223 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1225 offset += alignment;
1227 byte_count += PAGE_SIZE << page_order;
1230 DRM_DEBUG("byte_count: %d\n", byte_count);
1232 temp_buflist = drm_realloc(dma->buflist,
1233 dma->buf_count * sizeof(*dma->buflist),
1234 (dma->buf_count + entry->buf_count)
1235 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1236 if (!temp_buflist) {
1237 /* Free the entry because it isn't valid */
1238 drm_cleanup_buf_error(dev, entry);
1239 mutex_unlock(&dev->struct_mutex);
1240 atomic_dec(&dev->buf_alloc);
1243 dma->buflist = temp_buflist;
1245 for (i = 0; i < entry->buf_count; i++) {
1246 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1249 dma->buf_count += entry->buf_count;
1250 dma->seg_count += entry->seg_count;
1251 dma->page_count += byte_count >> PAGE_SHIFT;
1252 dma->byte_count += byte_count;
1254 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1255 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1257 mutex_unlock(&dev->struct_mutex);
1259 request->count = entry->buf_count;
1260 request->size = size;
1262 dma->flags = _DRM_DMA_USE_FB;
1264 atomic_dec(&dev->buf_alloc);
1270 * Add buffers for DMA transfers (ioctl).
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.
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.
1283 int drm_addbufs(struct inode *inode, struct drm_file *file_priv,
1284 unsigned int cmd, unsigned long arg)
1286 struct drm_buf_desc request;
1287 struct drm_device *dev = file_priv->head->dev;
1290 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1293 if (copy_from_user(&request, (struct drm_buf_desc __user *) arg,
1298 if (request.flags & _DRM_AGP_BUFFER)
1299 ret = drm_addbufs_agp(dev, &request);
1302 if (request.flags & _DRM_SG_BUFFER)
1303 ret = drm_addbufs_sg(dev, &request);
1304 else if (request.flags & _DRM_FB_BUFFER)
1305 ret = drm_addbufs_fb(dev, &request);
1307 ret = drm_addbufs_pci(dev, &request);
1310 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
1318 * Get information about the buffer mappings.
1320 * This was originally mean for debugging purposes, or by a sophisticated
1321 * client library to determine how best to use the available buffers (e.g.,
1322 * large buffers can be used for image transfer).
1324 * \param inode device inode.
1325 * \param file_priv DRM file private.
1326 * \param cmd command.
1327 * \param arg pointer to a drm_buf_info structure.
1328 * \return zero on success or a negative number on failure.
1330 * Increments drm_device::buf_use while holding the drm_device::count_lock
1331 * lock, preventing of allocating more buffers after this call. Information
1332 * about each requested buffer is then copied into user space.
1334 int drm_infobufs(struct inode *inode, struct drm_file *file_priv,
1335 unsigned int cmd, unsigned long arg)
1337 struct drm_device *dev = file_priv->head->dev;
1338 struct drm_device_dma *dma = dev->dma;
1339 struct drm_buf_info request;
1340 struct drm_buf_info __user *argp = (void __user *)arg;
1344 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1350 spin_lock(&dev->count_lock);
1351 if (atomic_read(&dev->buf_alloc)) {
1352 spin_unlock(&dev->count_lock);
1355 ++dev->buf_use; /* Can't allocate more after this call */
1356 spin_unlock(&dev->count_lock);
1358 if (copy_from_user(&request, argp, sizeof(request)))
1361 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1362 if (dma->bufs[i].buf_count)
1366 DRM_DEBUG("count = %d\n", count);
1368 if (request.count >= count) {
1369 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1370 if (dma->bufs[i].buf_count) {
1371 struct drm_buf_desc __user *to =
1372 &request.list[count];
1373 struct drm_buf_entry *from = &dma->bufs[i];
1374 struct drm_freelist *list = &dma->bufs[i].freelist;
1375 if (copy_to_user(&to->count,
1377 sizeof(from->buf_count)) ||
1378 copy_to_user(&to->size,
1380 sizeof(from->buf_size)) ||
1381 copy_to_user(&to->low_mark,
1383 sizeof(list->low_mark)) ||
1384 copy_to_user(&to->high_mark,
1386 sizeof(list->high_mark)))
1389 DRM_DEBUG("%d %d %d %d %d\n",
1391 dma->bufs[i].buf_count,
1392 dma->bufs[i].buf_size,
1393 dma->bufs[i].freelist.low_mark,
1394 dma->bufs[i].freelist.high_mark);
1399 request.count = count;
1401 if (copy_to_user(argp, &request, sizeof(request)))
1408 * Specifies a low and high water mark for buffer allocation
1410 * \param inode device inode.
1411 * \param file_priv DRM file private.
1412 * \param cmd command.
1413 * \param arg a pointer to a drm_buf_desc structure.
1414 * \return zero on success or a negative number on failure.
1416 * Verifies that the size order is bounded between the admissible orders and
1417 * updates the respective drm_device_dma::bufs entry low and high water mark.
1419 * \note This ioctl is deprecated and mostly never used.
1421 int drm_markbufs(struct inode *inode, struct drm_file *file_priv,
1422 unsigned int cmd, unsigned long arg)
1424 struct drm_device *dev = file_priv->head->dev;
1425 struct drm_device_dma *dma = dev->dma;
1426 struct drm_buf_desc request;
1428 struct drm_buf_entry *entry;
1430 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1436 if (copy_from_user(&request,
1437 (struct drm_buf_desc __user *) arg, sizeof(request)))
1440 DRM_DEBUG("%d, %d, %d\n",
1441 request.size, request.low_mark, request.high_mark);
1442 order = drm_order(request.size);
1443 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1445 entry = &dma->bufs[order];
1447 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
1449 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
1452 entry->freelist.low_mark = request.low_mark;
1453 entry->freelist.high_mark = request.high_mark;
1459 * Unreserve the buffers in list, previously reserved using drmDMA.
1461 * \param inode device inode.
1462 * \param file_priv DRM file private.
1463 * \param cmd command.
1464 * \param arg pointer to a drm_buf_free structure.
1465 * \return zero on success or a negative number on failure.
1467 * Calls free_buffer() for each used buffer.
1468 * This function is primarily used for debugging.
1470 int drm_freebufs(struct inode *inode, struct drm_file *file_priv,
1471 unsigned int cmd, unsigned long arg)
1473 struct drm_device *dev = file_priv->head->dev;
1474 struct drm_device_dma *dma = dev->dma;
1475 struct drm_buf_free request;
1478 struct drm_buf *buf;
1480 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1486 if (copy_from_user(&request,
1487 (struct drm_buf_free __user *) arg, sizeof(request)))
1490 DRM_DEBUG("%d\n", request.count);
1491 for (i = 0; i < request.count; i++) {
1492 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
1494 if (idx < 0 || idx >= dma->buf_count) {
1495 DRM_ERROR("Index %d (of %d max)\n",
1496 idx, dma->buf_count - 1);
1499 buf = dma->buflist[idx];
1500 if (buf->file_priv != file_priv) {
1501 DRM_ERROR("Process %d freeing buffer not owned\n",
1505 drm_free_buffer(dev, buf);
1512 * Maps all of the DMA buffers into client-virtual space (ioctl).
1514 * \param inode device inode.
1515 * \param file_priv DRM file private.
1516 * \param cmd command.
1517 * \param arg pointer to a drm_buf_map structure.
1518 * \return zero on success or a negative number on failure.
1520 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1521 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1522 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1525 int drm_mapbufs(struct inode *inode, struct drm_file *file_priv,
1526 unsigned int cmd, unsigned long arg)
1528 struct drm_device *dev = file_priv->head->dev;
1529 struct drm_device_dma *dma = dev->dma;
1530 struct drm_buf_map __user *argp = (void __user *)arg;
1533 unsigned long virtual;
1534 unsigned long address;
1535 struct drm_buf_map request;
1538 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1544 spin_lock(&dev->count_lock);
1545 if (atomic_read(&dev->buf_alloc)) {
1546 spin_unlock(&dev->count_lock);
1549 dev->buf_use++; /* Can't allocate more after this call */
1550 spin_unlock(&dev->count_lock);
1552 if (copy_from_user(&request, argp, sizeof(request)))
1555 if (request.count >= dma->buf_count) {
1556 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1557 || (drm_core_check_feature(dev, DRIVER_SG)
1558 && (dma->flags & _DRM_DMA_USE_SG))
1559 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1560 && (dma->flags & _DRM_DMA_USE_FB))) {
1561 struct drm_map *map = dev->agp_buffer_map;
1562 unsigned long token = dev->agp_buffer_token;
1569 down_write(¤t->mm->mmap_sem);
1570 virtual = do_mmap(file_priv->filp, 0, map->size,
1571 PROT_READ | PROT_WRITE,
1573 up_write(¤t->mm->mmap_sem);
1575 down_write(¤t->mm->mmap_sem);
1576 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
1577 PROT_READ | PROT_WRITE,
1579 up_write(¤t->mm->mmap_sem);
1581 if (virtual > -1024UL) {
1583 retcode = (signed long)virtual;
1586 request.virtual = (void __user *)virtual;
1588 for (i = 0; i < dma->buf_count; i++) {
1589 if (copy_to_user(&request.list[i].idx,
1590 &dma->buflist[i]->idx,
1591 sizeof(request.list[0].idx))) {
1595 if (copy_to_user(&request.list[i].total,
1596 &dma->buflist[i]->total,
1597 sizeof(request.list[0].total))) {
1601 if (copy_to_user(&request.list[i].used,
1602 &zero, sizeof(zero))) {
1606 address = virtual + dma->buflist[i]->offset; /* *** */
1607 if (copy_to_user(&request.list[i].address,
1608 &address, sizeof(address))) {
1615 request.count = dma->buf_count;
1616 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
1618 if (copy_to_user(argp, &request, sizeof(request)))
1625 * Compute size order. Returns the exponent of the smaller power of two which
1626 * is greater or equal to given number.
1631 * \todo Can be made faster.
1633 int drm_order(unsigned long size)
1638 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1640 if (size & (size - 1))
1645 EXPORT_SYMBOL(drm_order);