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(drm_device_t *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(drm_device_t *dev, unsigned int resource)
47 return pci_resource_len(dev->pdev, resource);
50 EXPORT_SYMBOL(drm_get_resource_len);
52 static drm_map_list_t *drm_find_matching_map(drm_device_t *dev,
55 struct list_head *list;
57 list_for_each(list, &dev->maplist->head) {
58 drm_map_list_t *entry = list_entry(list, drm_map_list_t, head);
59 if (entry->map && map->type == entry->map->type &&
60 entry->map->offset == map->offset) {
68 static int drm_map_handle(drm_device_t *dev, drm_hash_item_t *hash,
69 unsigned long user_token, int hashed_handle)
71 int use_hashed_handle;
72 #if (BITS_PER_LONG == 64)
73 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
74 #elif (BITS_PER_LONG == 32)
75 use_hashed_handle = hashed_handle;
77 #error Unsupported long size. Neither 64 nor 32 bits.
80 if (!use_hashed_handle) {
82 hash->key = user_token;
83 ret = drm_ht_insert_item(&dev->map_hash, hash);
87 return drm_ht_just_insert_please(&dev->map_hash, hash,
88 user_token, 32 - PAGE_SHIFT - 3,
89 PAGE_SHIFT, DRM_MAP_HASH_OFFSET);
93 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
95 * \param inode device inode.
96 * \param filp file pointer.
98 * \param arg pointer to a drm_map structure.
99 * \return zero on success or a negative value on error.
101 * Adjusts the memory offset to its absolute value according to the mapping
102 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
103 * applicable and if supported by the kernel.
105 static int drm_addmap_core(drm_device_t * dev, unsigned int offset,
106 unsigned int size, drm_map_type_t type,
107 drm_map_flags_t flags, drm_map_list_t ** maplist)
110 drm_map_list_t *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 = drm_ioremap(map->offset, map->size, dev);
185 map->handle = vmalloc_32(map->size);
186 DRM_DEBUG("%lu %d %p\n",
187 map->size, drm_order(map->size), map->handle);
189 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
192 map->offset = (unsigned long)map->handle;
193 if (map->flags & _DRM_CONTAINS_LOCK) {
194 /* Prevent a 2nd X Server from creating a 2nd lock */
195 if (dev->lock.hw_lock != NULL) {
197 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
200 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
204 if (drm_core_has_AGP(dev)) {
206 map->offset += dev->hose->mem_space->start;
208 map->offset += dev->agp->base;
209 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
212 case _DRM_SCATTER_GATHER:
214 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
217 map->offset += (unsigned long)dev->sg->virtual;
219 case _DRM_CONSISTENT:
220 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
221 * As we're limiting the address to 2^32-1 (or less),
222 * casting it down to 32 bits is no problem, but we
223 * need to point to a 64bit variable first. */
224 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
226 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
229 map->handle = dmah->vaddr;
230 map->offset = (unsigned long)dmah->busaddr;
234 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
238 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
240 if (map->type == _DRM_REGISTERS)
241 drm_ioremapfree(map->handle, map->size, dev);
242 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
245 memset(list, 0, sizeof(*list));
248 mutex_lock(&dev->struct_mutex);
249 list_add(&list->head, &dev->maplist->head);
251 /* Assign a 32-bit handle */
252 /* We do it here so that dev->struct_mutex protects the increment */
253 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
255 ret = drm_map_handle(dev, &list->hash, user_token, 0);
257 if (map->type == _DRM_REGISTERS)
258 drm_ioremapfree(map->handle, map->size, dev);
259 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
260 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
261 mutex_unlock(&dev->struct_mutex);
265 list->user_token = list->hash.key;
266 mutex_unlock(&dev->struct_mutex);
272 int drm_addmap(drm_device_t * dev, unsigned int offset,
273 unsigned int size, drm_map_type_t type,
274 drm_map_flags_t flags, drm_local_map_t ** map_ptr)
276 drm_map_list_t *list;
279 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
281 *map_ptr = list->map;
285 EXPORT_SYMBOL(drm_addmap);
287 int drm_addmap_ioctl(struct inode *inode, struct file *filp,
288 unsigned int cmd, unsigned long arg)
290 drm_file_t *priv = filp->private_data;
291 drm_device_t *dev = priv->head->dev;
293 drm_map_list_t *maplist;
294 drm_map_t __user *argp = (void __user *)arg;
297 if (!(filp->f_mode & 3))
298 return -EACCES; /* Require read/write */
300 if (copy_from_user(&map, argp, sizeof(map))) {
304 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
307 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
313 if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))
316 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
317 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
323 * Remove a map private from list and deallocate resources if the mapping
326 * \param inode device inode.
327 * \param filp file pointer.
328 * \param cmd command.
329 * \param arg pointer to a drm_map_t structure.
330 * \return zero on success or a negative value on error.
332 * Searches the map on drm_device::maplist, removes it from the list, see if
333 * its being used, and free any associate resource (such as MTRR's) if it's not
338 int drm_rmmap_locked(drm_device_t *dev, drm_local_map_t *map)
340 struct list_head *list;
341 drm_map_list_t *r_list = NULL;
342 drm_dma_handle_t dmah;
344 /* Find the list entry for the map and remove it */
345 list_for_each(list, &dev->maplist->head) {
346 r_list = list_entry(list, drm_map_list_t, head);
348 if (r_list->map == map) {
350 drm_ht_remove_key(&dev->map_hash, r_list->user_token);
351 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
356 /* List has wrapped around to the head pointer, or it's empty and we
357 * didn't find anything.
359 if (list == (&dev->maplist->head)) {
365 drm_ioremapfree(map->handle, map->size, dev);
367 case _DRM_FRAME_BUFFER:
368 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
370 retcode = mtrr_del(map->mtrr, map->offset, map->size);
371 DRM_DEBUG("mtrr_del=%d\n", retcode);
378 case _DRM_SCATTER_GATHER:
380 case _DRM_CONSISTENT:
381 dmah.vaddr = map->handle;
382 dmah.busaddr = map->offset;
383 dmah.size = map->size;
384 __drm_pci_free(dev, &dmah);
387 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
392 int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
396 mutex_lock(&dev->struct_mutex);
397 ret = drm_rmmap_locked(dev, map);
398 mutex_unlock(&dev->struct_mutex);
403 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
404 * the last close of the device, and this is necessary for cleanup when things
405 * exit uncleanly. Therefore, having userland manually remove mappings seems
406 * like a pointless exercise since they're going away anyway.
408 * One use case might be after addmap is allowed for normal users for SHM and
409 * gets used by drivers that the server doesn't need to care about. This seems
412 int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
413 unsigned int cmd, unsigned long arg)
415 drm_file_t *priv = filp->private_data;
416 drm_device_t *dev = priv->head->dev;
418 drm_local_map_t *map = NULL;
419 struct list_head *list;
422 if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
426 mutex_lock(&dev->struct_mutex);
427 list_for_each(list, &dev->maplist->head) {
428 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
431 r_list->user_token == (unsigned long)request.handle &&
432 r_list->map->flags & _DRM_REMOVABLE) {
438 /* List has wrapped around to the head pointer, or its empty we didn't
441 if (list == (&dev->maplist->head)) {
442 mutex_unlock(&dev->struct_mutex);
447 mutex_unlock(&dev->struct_mutex);
451 /* Register and framebuffer maps are permanent */
452 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
453 mutex_unlock(&dev->struct_mutex);
457 ret = drm_rmmap_locked(dev, map);
459 mutex_unlock(&dev->struct_mutex);
465 * Cleanup after an error on one of the addbufs() functions.
467 * \param dev DRM device.
468 * \param entry buffer entry where the error occurred.
470 * Frees any pages and buffers associated with the given entry.
472 static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
476 if (entry->seg_count) {
477 for (i = 0; i < entry->seg_count; i++) {
478 if (entry->seglist[i]) {
479 drm_pci_free(dev, entry->seglist[i]);
482 drm_free(entry->seglist,
484 sizeof(*entry->seglist), DRM_MEM_SEGS);
486 entry->seg_count = 0;
489 if (entry->buf_count) {
490 for (i = 0; i < entry->buf_count; i++) {
491 if (entry->buflist[i].dev_private) {
492 drm_free(entry->buflist[i].dev_private,
493 entry->buflist[i].dev_priv_size,
497 drm_free(entry->buflist,
499 sizeof(*entry->buflist), DRM_MEM_BUFS);
501 entry->buf_count = 0;
507 * Add AGP buffers for DMA transfers.
509 * \param dev drm_device_t to which the buffers are to be added.
510 * \param request pointer to a drm_buf_desc_t describing the request.
511 * \return zero on success or a negative number on failure.
513 * After some sanity checks creates a drm_buf structure for each buffer and
514 * reallocates the buffer list of the same size order to accommodate the new
517 int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request)
519 drm_device_dma_t *dma = dev->dma;
520 drm_buf_entry_t *entry;
522 unsigned long offset;
523 unsigned long agp_offset;
532 drm_buf_t **temp_buflist;
537 count = request->count;
538 order = drm_order(request->size);
541 alignment = (request->flags & _DRM_PAGE_ALIGN)
542 ? PAGE_ALIGN(size) : size;
543 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
544 total = PAGE_SIZE << page_order;
547 agp_offset = dev->agp->base + request->agp_start;
549 DRM_DEBUG("count: %d\n", count);
550 DRM_DEBUG("order: %d\n", order);
551 DRM_DEBUG("size: %d\n", size);
552 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
553 DRM_DEBUG("alignment: %d\n", alignment);
554 DRM_DEBUG("page_order: %d\n", page_order);
555 DRM_DEBUG("total: %d\n", total);
557 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
559 if (dev->queue_count)
560 return -EBUSY; /* Not while in use */
562 spin_lock(&dev->count_lock);
564 spin_unlock(&dev->count_lock);
567 atomic_inc(&dev->buf_alloc);
568 spin_unlock(&dev->count_lock);
570 mutex_lock(&dev->struct_mutex);
571 entry = &dma->bufs[order];
572 if (entry->buf_count) {
573 mutex_unlock(&dev->struct_mutex);
574 atomic_dec(&dev->buf_alloc);
575 return -ENOMEM; /* May only call once for each order */
578 if (count < 0 || count > 4096) {
579 mutex_unlock(&dev->struct_mutex);
580 atomic_dec(&dev->buf_alloc);
584 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
586 if (!entry->buflist) {
587 mutex_unlock(&dev->struct_mutex);
588 atomic_dec(&dev->buf_alloc);
591 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
593 entry->buf_size = size;
594 entry->page_order = page_order;
598 while (entry->buf_count < count) {
599 buf = &entry->buflist[entry->buf_count];
600 buf->idx = dma->buf_count + entry->buf_count;
601 buf->total = alignment;
605 buf->offset = (dma->byte_count + offset);
606 buf->bus_address = agp_offset + offset;
607 buf->address = (void *)(agp_offset + offset);
611 init_waitqueue_head(&buf->dma_wait);
614 buf->dev_priv_size = dev->driver->dev_priv_size;
615 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
616 if (!buf->dev_private) {
617 /* Set count correctly so we free the proper amount. */
618 entry->buf_count = count;
619 drm_cleanup_buf_error(dev, entry);
620 mutex_unlock(&dev->struct_mutex);
621 atomic_dec(&dev->buf_alloc);
624 memset(buf->dev_private, 0, buf->dev_priv_size);
626 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
630 byte_count += PAGE_SIZE << page_order;
633 DRM_DEBUG("byte_count: %d\n", byte_count);
635 temp_buflist = drm_realloc(dma->buflist,
636 dma->buf_count * sizeof(*dma->buflist),
637 (dma->buf_count + entry->buf_count)
638 * sizeof(*dma->buflist), DRM_MEM_BUFS);
640 /* Free the entry because it isn't valid */
641 drm_cleanup_buf_error(dev, entry);
642 mutex_unlock(&dev->struct_mutex);
643 atomic_dec(&dev->buf_alloc);
646 dma->buflist = temp_buflist;
648 for (i = 0; i < entry->buf_count; i++) {
649 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
652 dma->buf_count += entry->buf_count;
653 dma->seg_count += entry->seg_count;
654 dma->page_count += byte_count >> PAGE_SHIFT;
655 dma->byte_count += byte_count;
657 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
658 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
660 mutex_unlock(&dev->struct_mutex);
662 request->count = entry->buf_count;
663 request->size = size;
665 dma->flags = _DRM_DMA_USE_AGP;
667 atomic_dec(&dev->buf_alloc);
670 EXPORT_SYMBOL(drm_addbufs_agp);
671 #endif /* __OS_HAS_AGP */
673 int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
675 drm_device_dma_t *dma = dev->dma;
681 drm_buf_entry_t *entry;
682 drm_dma_handle_t *dmah;
685 unsigned long offset;
689 unsigned long *temp_pagelist;
690 drm_buf_t **temp_buflist;
692 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
698 if (!capable(CAP_SYS_ADMIN))
701 count = request->count;
702 order = drm_order(request->size);
705 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
706 request->count, request->size, size, order, dev->queue_count);
708 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
710 if (dev->queue_count)
711 return -EBUSY; /* Not while in use */
713 alignment = (request->flags & _DRM_PAGE_ALIGN)
714 ? PAGE_ALIGN(size) : size;
715 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
716 total = PAGE_SIZE << page_order;
718 spin_lock(&dev->count_lock);
720 spin_unlock(&dev->count_lock);
723 atomic_inc(&dev->buf_alloc);
724 spin_unlock(&dev->count_lock);
726 mutex_lock(&dev->struct_mutex);
727 entry = &dma->bufs[order];
728 if (entry->buf_count) {
729 mutex_unlock(&dev->struct_mutex);
730 atomic_dec(&dev->buf_alloc);
731 return -ENOMEM; /* May only call once for each order */
734 if (count < 0 || count > 4096) {
735 mutex_unlock(&dev->struct_mutex);
736 atomic_dec(&dev->buf_alloc);
740 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
742 if (!entry->buflist) {
743 mutex_unlock(&dev->struct_mutex);
744 atomic_dec(&dev->buf_alloc);
747 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
749 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
751 if (!entry->seglist) {
752 drm_free(entry->buflist,
753 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
754 mutex_unlock(&dev->struct_mutex);
755 atomic_dec(&dev->buf_alloc);
758 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
760 /* Keep the original pagelist until we know all the allocations
763 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
764 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
765 if (!temp_pagelist) {
766 drm_free(entry->buflist,
767 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
768 drm_free(entry->seglist,
769 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
770 mutex_unlock(&dev->struct_mutex);
771 atomic_dec(&dev->buf_alloc);
774 memcpy(temp_pagelist,
775 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
776 DRM_DEBUG("pagelist: %d entries\n",
777 dma->page_count + (count << page_order));
779 entry->buf_size = size;
780 entry->page_order = page_order;
784 while (entry->buf_count < count) {
786 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
789 /* Set count correctly so we free the proper amount. */
790 entry->buf_count = count;
791 entry->seg_count = count;
792 drm_cleanup_buf_error(dev, entry);
793 drm_free(temp_pagelist,
794 (dma->page_count + (count << page_order))
795 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
796 mutex_unlock(&dev->struct_mutex);
797 atomic_dec(&dev->buf_alloc);
800 entry->seglist[entry->seg_count++] = dmah;
801 for (i = 0; i < (1 << page_order); i++) {
802 DRM_DEBUG("page %d @ 0x%08lx\n",
803 dma->page_count + page_count,
804 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
805 temp_pagelist[dma->page_count + page_count++]
806 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
809 offset + size <= total && entry->buf_count < count;
810 offset += alignment, ++entry->buf_count) {
811 buf = &entry->buflist[entry->buf_count];
812 buf->idx = dma->buf_count + entry->buf_count;
813 buf->total = alignment;
816 buf->offset = (dma->byte_count + byte_count + offset);
817 buf->address = (void *)(dmah->vaddr + offset);
818 buf->bus_address = dmah->busaddr + offset;
822 init_waitqueue_head(&buf->dma_wait);
825 buf->dev_priv_size = dev->driver->dev_priv_size;
826 buf->dev_private = drm_alloc(buf->dev_priv_size,
828 if (!buf->dev_private) {
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,
835 (count << page_order))
836 * sizeof(*dma->pagelist),
838 mutex_unlock(&dev->struct_mutex);
839 atomic_dec(&dev->buf_alloc);
842 memset(buf->dev_private, 0, buf->dev_priv_size);
844 DRM_DEBUG("buffer %d @ %p\n",
845 entry->buf_count, buf->address);
847 byte_count += PAGE_SIZE << page_order;
850 temp_buflist = drm_realloc(dma->buflist,
851 dma->buf_count * sizeof(*dma->buflist),
852 (dma->buf_count + entry->buf_count)
853 * sizeof(*dma->buflist), DRM_MEM_BUFS);
855 /* Free the entry because it isn't valid */
856 drm_cleanup_buf_error(dev, entry);
857 drm_free(temp_pagelist,
858 (dma->page_count + (count << page_order))
859 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
860 mutex_unlock(&dev->struct_mutex);
861 atomic_dec(&dev->buf_alloc);
864 dma->buflist = temp_buflist;
866 for (i = 0; i < entry->buf_count; i++) {
867 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
870 /* No allocations failed, so now we can replace the orginal pagelist
873 if (dma->page_count) {
874 drm_free(dma->pagelist,
875 dma->page_count * sizeof(*dma->pagelist),
878 dma->pagelist = temp_pagelist;
880 dma->buf_count += entry->buf_count;
881 dma->seg_count += entry->seg_count;
882 dma->page_count += entry->seg_count << page_order;
883 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
885 mutex_unlock(&dev->struct_mutex);
887 request->count = entry->buf_count;
888 request->size = size;
890 if (request->flags & _DRM_PCI_BUFFER_RO)
891 dma->flags = _DRM_DMA_USE_PCI_RO;
893 atomic_dec(&dev->buf_alloc);
897 EXPORT_SYMBOL(drm_addbufs_pci);
899 static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
901 drm_device_dma_t *dma = dev->dma;
902 drm_buf_entry_t *entry;
904 unsigned long offset;
905 unsigned long agp_offset;
914 drm_buf_t **temp_buflist;
916 if (!drm_core_check_feature(dev, DRIVER_SG))
922 if (!capable(CAP_SYS_ADMIN))
925 count = request->count;
926 order = drm_order(request->size);
929 alignment = (request->flags & _DRM_PAGE_ALIGN)
930 ? PAGE_ALIGN(size) : size;
931 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
932 total = PAGE_SIZE << page_order;
935 agp_offset = request->agp_start;
937 DRM_DEBUG("count: %d\n", count);
938 DRM_DEBUG("order: %d\n", order);
939 DRM_DEBUG("size: %d\n", size);
940 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
941 DRM_DEBUG("alignment: %d\n", alignment);
942 DRM_DEBUG("page_order: %d\n", page_order);
943 DRM_DEBUG("total: %d\n", total);
945 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
947 if (dev->queue_count)
948 return -EBUSY; /* Not while in use */
950 spin_lock(&dev->count_lock);
952 spin_unlock(&dev->count_lock);
955 atomic_inc(&dev->buf_alloc);
956 spin_unlock(&dev->count_lock);
958 mutex_lock(&dev->struct_mutex);
959 entry = &dma->bufs[order];
960 if (entry->buf_count) {
961 mutex_unlock(&dev->struct_mutex);
962 atomic_dec(&dev->buf_alloc);
963 return -ENOMEM; /* May only call once for each order */
966 if (count < 0 || count > 4096) {
967 mutex_unlock(&dev->struct_mutex);
968 atomic_dec(&dev->buf_alloc);
972 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
974 if (!entry->buflist) {
975 mutex_unlock(&dev->struct_mutex);
976 atomic_dec(&dev->buf_alloc);
979 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
981 entry->buf_size = size;
982 entry->page_order = page_order;
986 while (entry->buf_count < count) {
987 buf = &entry->buflist[entry->buf_count];
988 buf->idx = dma->buf_count + entry->buf_count;
989 buf->total = alignment;
993 buf->offset = (dma->byte_count + offset);
994 buf->bus_address = agp_offset + offset;
995 buf->address = (void *)(agp_offset + offset
996 + (unsigned long)dev->sg->virtual);
1000 init_waitqueue_head(&buf->dma_wait);
1003 buf->dev_priv_size = dev->driver->dev_priv_size;
1004 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1005 if (!buf->dev_private) {
1006 /* Set count correctly so we free the proper amount. */
1007 entry->buf_count = count;
1008 drm_cleanup_buf_error(dev, entry);
1009 mutex_unlock(&dev->struct_mutex);
1010 atomic_dec(&dev->buf_alloc);
1014 memset(buf->dev_private, 0, buf->dev_priv_size);
1016 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1018 offset += alignment;
1020 byte_count += PAGE_SIZE << page_order;
1023 DRM_DEBUG("byte_count: %d\n", byte_count);
1025 temp_buflist = drm_realloc(dma->buflist,
1026 dma->buf_count * sizeof(*dma->buflist),
1027 (dma->buf_count + entry->buf_count)
1028 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1029 if (!temp_buflist) {
1030 /* Free the entry because it isn't valid */
1031 drm_cleanup_buf_error(dev, entry);
1032 mutex_unlock(&dev->struct_mutex);
1033 atomic_dec(&dev->buf_alloc);
1036 dma->buflist = temp_buflist;
1038 for (i = 0; i < entry->buf_count; i++) {
1039 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1042 dma->buf_count += entry->buf_count;
1043 dma->seg_count += entry->seg_count;
1044 dma->page_count += byte_count >> PAGE_SHIFT;
1045 dma->byte_count += byte_count;
1047 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1048 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1050 mutex_unlock(&dev->struct_mutex);
1052 request->count = entry->buf_count;
1053 request->size = size;
1055 dma->flags = _DRM_DMA_USE_SG;
1057 atomic_dec(&dev->buf_alloc);
1061 static int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
1063 drm_device_dma_t *dma = dev->dma;
1064 drm_buf_entry_t *entry;
1066 unsigned long offset;
1067 unsigned long agp_offset;
1076 drm_buf_t **temp_buflist;
1078 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1084 if (!capable(CAP_SYS_ADMIN))
1087 count = request->count;
1088 order = drm_order(request->size);
1091 alignment = (request->flags & _DRM_PAGE_ALIGN)
1092 ? PAGE_ALIGN(size) : size;
1093 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1094 total = PAGE_SIZE << page_order;
1097 agp_offset = request->agp_start;
1099 DRM_DEBUG("count: %d\n", count);
1100 DRM_DEBUG("order: %d\n", order);
1101 DRM_DEBUG("size: %d\n", size);
1102 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1103 DRM_DEBUG("alignment: %d\n", alignment);
1104 DRM_DEBUG("page_order: %d\n", page_order);
1105 DRM_DEBUG("total: %d\n", total);
1107 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1109 if (dev->queue_count)
1110 return -EBUSY; /* Not while in use */
1112 spin_lock(&dev->count_lock);
1114 spin_unlock(&dev->count_lock);
1117 atomic_inc(&dev->buf_alloc);
1118 spin_unlock(&dev->count_lock);
1120 mutex_lock(&dev->struct_mutex);
1121 entry = &dma->bufs[order];
1122 if (entry->buf_count) {
1123 mutex_unlock(&dev->struct_mutex);
1124 atomic_dec(&dev->buf_alloc);
1125 return -ENOMEM; /* May only call once for each order */
1128 if (count < 0 || count > 4096) {
1129 mutex_unlock(&dev->struct_mutex);
1130 atomic_dec(&dev->buf_alloc);
1134 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1136 if (!entry->buflist) {
1137 mutex_unlock(&dev->struct_mutex);
1138 atomic_dec(&dev->buf_alloc);
1141 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1143 entry->buf_size = size;
1144 entry->page_order = page_order;
1148 while (entry->buf_count < count) {
1149 buf = &entry->buflist[entry->buf_count];
1150 buf->idx = dma->buf_count + entry->buf_count;
1151 buf->total = alignment;
1155 buf->offset = (dma->byte_count + offset);
1156 buf->bus_address = agp_offset + offset;
1157 buf->address = (void *)(agp_offset + offset);
1161 init_waitqueue_head(&buf->dma_wait);
1164 buf->dev_priv_size = dev->driver->dev_priv_size;
1165 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1166 if (!buf->dev_private) {
1167 /* Set count correctly so we free the proper amount. */
1168 entry->buf_count = count;
1169 drm_cleanup_buf_error(dev, entry);
1170 mutex_unlock(&dev->struct_mutex);
1171 atomic_dec(&dev->buf_alloc);
1174 memset(buf->dev_private, 0, buf->dev_priv_size);
1176 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1178 offset += alignment;
1180 byte_count += PAGE_SIZE << page_order;
1183 DRM_DEBUG("byte_count: %d\n", byte_count);
1185 temp_buflist = drm_realloc(dma->buflist,
1186 dma->buf_count * sizeof(*dma->buflist),
1187 (dma->buf_count + entry->buf_count)
1188 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1189 if (!temp_buflist) {
1190 /* Free the entry because it isn't valid */
1191 drm_cleanup_buf_error(dev, entry);
1192 mutex_unlock(&dev->struct_mutex);
1193 atomic_dec(&dev->buf_alloc);
1196 dma->buflist = temp_buflist;
1198 for (i = 0; i < entry->buf_count; i++) {
1199 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1202 dma->buf_count += entry->buf_count;
1203 dma->seg_count += entry->seg_count;
1204 dma->page_count += byte_count >> PAGE_SHIFT;
1205 dma->byte_count += byte_count;
1207 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1208 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1210 mutex_unlock(&dev->struct_mutex);
1212 request->count = entry->buf_count;
1213 request->size = size;
1215 dma->flags = _DRM_DMA_USE_FB;
1217 atomic_dec(&dev->buf_alloc);
1223 * Add buffers for DMA transfers (ioctl).
1225 * \param inode device inode.
1226 * \param filp file pointer.
1227 * \param cmd command.
1228 * \param arg pointer to a drm_buf_desc_t request.
1229 * \return zero on success or a negative number on failure.
1231 * According with the memory type specified in drm_buf_desc::flags and the
1232 * build options, it dispatches the call either to addbufs_agp(),
1233 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1234 * PCI memory respectively.
1236 int drm_addbufs(struct inode *inode, struct file *filp,
1237 unsigned int cmd, unsigned long arg)
1239 drm_buf_desc_t request;
1240 drm_file_t *priv = filp->private_data;
1241 drm_device_t *dev = priv->head->dev;
1244 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1247 if (copy_from_user(&request, (drm_buf_desc_t __user *) arg,
1252 if (request.flags & _DRM_AGP_BUFFER)
1253 ret = drm_addbufs_agp(dev, &request);
1256 if (request.flags & _DRM_SG_BUFFER)
1257 ret = drm_addbufs_sg(dev, &request);
1258 else if (request.flags & _DRM_FB_BUFFER)
1259 ret = drm_addbufs_fb(dev, &request);
1261 ret = drm_addbufs_pci(dev, &request);
1264 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
1272 * Get information about the buffer mappings.
1274 * This was originally mean for debugging purposes, or by a sophisticated
1275 * client library to determine how best to use the available buffers (e.g.,
1276 * large buffers can be used for image transfer).
1278 * \param inode device inode.
1279 * \param filp file pointer.
1280 * \param cmd command.
1281 * \param arg pointer to a drm_buf_info structure.
1282 * \return zero on success or a negative number on failure.
1284 * Increments drm_device::buf_use while holding the drm_device::count_lock
1285 * lock, preventing of allocating more buffers after this call. Information
1286 * about each requested buffer is then copied into user space.
1288 int drm_infobufs(struct inode *inode, struct file *filp,
1289 unsigned int cmd, unsigned long arg)
1291 drm_file_t *priv = filp->private_data;
1292 drm_device_t *dev = priv->head->dev;
1293 drm_device_dma_t *dma = dev->dma;
1294 drm_buf_info_t request;
1295 drm_buf_info_t __user *argp = (void __user *)arg;
1299 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1305 spin_lock(&dev->count_lock);
1306 if (atomic_read(&dev->buf_alloc)) {
1307 spin_unlock(&dev->count_lock);
1310 ++dev->buf_use; /* Can't allocate more after this call */
1311 spin_unlock(&dev->count_lock);
1313 if (copy_from_user(&request, argp, sizeof(request)))
1316 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1317 if (dma->bufs[i].buf_count)
1321 DRM_DEBUG("count = %d\n", count);
1323 if (request.count >= count) {
1324 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1325 if (dma->bufs[i].buf_count) {
1326 drm_buf_desc_t __user *to =
1327 &request.list[count];
1328 drm_buf_entry_t *from = &dma->bufs[i];
1329 drm_freelist_t *list = &dma->bufs[i].freelist;
1330 if (copy_to_user(&to->count,
1332 sizeof(from->buf_count)) ||
1333 copy_to_user(&to->size,
1335 sizeof(from->buf_size)) ||
1336 copy_to_user(&to->low_mark,
1338 sizeof(list->low_mark)) ||
1339 copy_to_user(&to->high_mark,
1341 sizeof(list->high_mark)))
1344 DRM_DEBUG("%d %d %d %d %d\n",
1346 dma->bufs[i].buf_count,
1347 dma->bufs[i].buf_size,
1348 dma->bufs[i].freelist.low_mark,
1349 dma->bufs[i].freelist.high_mark);
1354 request.count = count;
1356 if (copy_to_user(argp, &request, sizeof(request)))
1363 * Specifies a low and high water mark for buffer allocation
1365 * \param inode device inode.
1366 * \param filp file pointer.
1367 * \param cmd command.
1368 * \param arg a pointer to a drm_buf_desc structure.
1369 * \return zero on success or a negative number on failure.
1371 * Verifies that the size order is bounded between the admissible orders and
1372 * updates the respective drm_device_dma::bufs entry low and high water mark.
1374 * \note This ioctl is deprecated and mostly never used.
1376 int drm_markbufs(struct inode *inode, struct file *filp,
1377 unsigned int cmd, unsigned long arg)
1379 drm_file_t *priv = filp->private_data;
1380 drm_device_t *dev = priv->head->dev;
1381 drm_device_dma_t *dma = dev->dma;
1382 drm_buf_desc_t request;
1384 drm_buf_entry_t *entry;
1386 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1392 if (copy_from_user(&request,
1393 (drm_buf_desc_t __user *) arg, sizeof(request)))
1396 DRM_DEBUG("%d, %d, %d\n",
1397 request.size, request.low_mark, request.high_mark);
1398 order = drm_order(request.size);
1399 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1401 entry = &dma->bufs[order];
1403 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
1405 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
1408 entry->freelist.low_mark = request.low_mark;
1409 entry->freelist.high_mark = request.high_mark;
1415 * Unreserve the buffers in list, previously reserved using drmDMA.
1417 * \param inode device inode.
1418 * \param filp file pointer.
1419 * \param cmd command.
1420 * \param arg pointer to a drm_buf_free structure.
1421 * \return zero on success or a negative number on failure.
1423 * Calls free_buffer() for each used buffer.
1424 * This function is primarily used for debugging.
1426 int drm_freebufs(struct inode *inode, struct file *filp,
1427 unsigned int cmd, unsigned long arg)
1429 drm_file_t *priv = filp->private_data;
1430 drm_device_t *dev = priv->head->dev;
1431 drm_device_dma_t *dma = dev->dma;
1432 drm_buf_free_t request;
1437 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1443 if (copy_from_user(&request,
1444 (drm_buf_free_t __user *) arg, sizeof(request)))
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->filp != filp) {
1458 DRM_ERROR("Process %d freeing buffer not owned\n",
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 filp file pointer.
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 inode *inode, struct file *filp,
1483 unsigned int cmd, unsigned long arg)
1485 drm_file_t *priv = filp->private_data;
1486 drm_device_t *dev = priv->head->dev;
1487 drm_device_dma_t *dma = dev->dma;
1488 drm_buf_map_t __user *argp = (void __user *)arg;
1491 unsigned long virtual;
1492 unsigned long address;
1493 drm_buf_map_t request;
1496 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1502 spin_lock(&dev->count_lock);
1503 if (atomic_read(&dev->buf_alloc)) {
1504 spin_unlock(&dev->count_lock);
1507 dev->buf_use++; /* Can't allocate more after this call */
1508 spin_unlock(&dev->count_lock);
1510 if (copy_from_user(&request, argp, sizeof(request)))
1513 if (request.count >= dma->buf_count) {
1514 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1515 || (drm_core_check_feature(dev, DRIVER_SG)
1516 && (dma->flags & _DRM_DMA_USE_SG))
1517 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1518 && (dma->flags & _DRM_DMA_USE_FB))) {
1519 drm_map_t *map = dev->agp_buffer_map;
1520 unsigned long token = dev->agp_buffer_token;
1527 down_write(¤t->mm->mmap_sem);
1528 virtual = do_mmap(filp, 0, map->size,
1529 PROT_READ | PROT_WRITE,
1531 up_write(¤t->mm->mmap_sem);
1533 down_write(¤t->mm->mmap_sem);
1534 virtual = do_mmap(filp, 0, dma->byte_count,
1535 PROT_READ | PROT_WRITE,
1537 up_write(¤t->mm->mmap_sem);
1539 if (virtual > -1024UL) {
1541 retcode = (signed long)virtual;
1544 request.virtual = (void __user *)virtual;
1546 for (i = 0; i < dma->buf_count; i++) {
1547 if (copy_to_user(&request.list[i].idx,
1548 &dma->buflist[i]->idx,
1549 sizeof(request.list[0].idx))) {
1553 if (copy_to_user(&request.list[i].total,
1554 &dma->buflist[i]->total,
1555 sizeof(request.list[0].total))) {
1559 if (copy_to_user(&request.list[i].used,
1560 &zero, sizeof(zero))) {
1564 address = virtual + dma->buflist[i]->offset; /* *** */
1565 if (copy_to_user(&request.list[i].address,
1566 &address, sizeof(address))) {
1573 request.count = dma->buf_count;
1574 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
1576 if (copy_to_user(argp, &request, sizeof(request)))
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);