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>
40 * Compute size order. Returns the exponent of the smaller power of two which
41 * is greater or equal to given number.
46 * \todo Can be made faster.
48 int drm_order( unsigned long size )
53 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++)
56 if (size & (size - 1))
61 EXPORT_SYMBOL(drm_order);
65 * Used to allocate 32-bit handles for _DRM_SHM regions
66 * The 0x10000000 value is chosen to be out of the way of
67 * FB/register and GART physical addresses.
69 static unsigned int map32_handle = 0x10000000;
73 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
75 * \param inode device inode.
76 * \param filp file pointer.
78 * \param arg pointer to a drm_map structure.
79 * \return zero on success or a negative value on error.
81 * Adjusts the memory offset to its absolute value according to the mapping
82 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
83 * applicable and if supported by the kernel.
85 int drm_addmap(drm_device_t * dev, unsigned int offset,
86 unsigned int size, drm_map_type_t type,
87 drm_map_flags_t flags, drm_local_map_t ** map_ptr)
91 drm_dma_handle_t *dmah;
93 map = drm_alloc( sizeof(*map), DRM_MEM_MAPS );
102 /* Only allow shared memory to be removable since we only keep enough
103 * book keeping information about shared memory to allow for removal
104 * when processes fork.
106 if ( (map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM ) {
107 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
110 DRM_DEBUG( "offset = 0x%08lx, size = 0x%08lx, type = %d\n",
111 map->offset, map->size, map->type );
112 if ( (map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK)) ) {
113 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
119 switch ( map->type ) {
121 case _DRM_FRAME_BUFFER:
122 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__)
123 if ( map->offset + map->size < map->offset ||
124 map->offset < virt_to_phys(high_memory) ) {
125 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
130 map->offset += dev->hose->mem_space->start;
132 if (drm_core_has_MTRR(dev)) {
133 if ( map->type == _DRM_FRAME_BUFFER ||
134 (map->flags & _DRM_WRITE_COMBINING) ) {
135 map->mtrr = mtrr_add( map->offset, map->size,
136 MTRR_TYPE_WRCOMB, 1 );
139 if (map->type == _DRM_REGISTERS)
140 map->handle = drm_ioremap( map->offset, map->size,
145 map->handle = vmalloc_32(map->size);
146 DRM_DEBUG( "%lu %d %p\n",
147 map->size, drm_order( map->size ), map->handle );
148 if ( !map->handle ) {
149 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
152 map->offset = (unsigned long)map->handle;
153 if ( map->flags & _DRM_CONTAINS_LOCK ) {
154 /* Prevent a 2nd X Server from creating a 2nd lock */
155 if (dev->lock.hw_lock != NULL) {
156 vfree( map->handle );
157 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
161 dev->lock.hw_lock = map->handle; /* Pointer to lock */
165 if (drm_core_has_AGP(dev)) {
167 map->offset += dev->hose->mem_space->start;
169 map->offset += dev->agp->base;
170 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
173 case _DRM_SCATTER_GATHER:
175 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
178 map->offset += dev->sg->handle;
180 case _DRM_CONSISTENT:
181 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
182 * As we're limiting the address to 2^32-1 (or less),
183 * casting it down to 32 bits is no problem, but we
184 * need to point to a 64bit variable first. */
185 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
187 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
190 map->handle = dmah->vaddr;
191 map->offset = (unsigned long)dmah->busaddr;
195 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
199 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
201 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
204 memset(list, 0, sizeof(*list));
207 down(&dev->struct_sem);
208 list_add(&list->head, &dev->maplist->head);
210 /* Assign a 32-bit handle for _DRM_SHM mappings */
211 /* We do it here so that dev->struct_sem protects the increment */
212 if (map->type == _DRM_SHM)
213 map->offset = map32_handle += PAGE_SIZE;
215 up(&dev->struct_sem);
220 EXPORT_SYMBOL(drm_addmap);
222 int drm_addmap_ioctl(struct inode *inode, struct file *filp,
223 unsigned int cmd, unsigned long arg)
225 drm_file_t *priv = filp->private_data;
226 drm_device_t *dev = priv->head->dev;
229 drm_map_t __user *argp = (void __user *)arg;
232 if (!(filp->f_mode & 3))
233 return -EACCES; /* Require read/write */
235 if (copy_from_user(& map, argp, sizeof(map))) {
239 err = drm_addmap( dev, map.offset, map.size, map.type, map.flags,
246 if (copy_to_user(argp, map_ptr, sizeof(*map_ptr)))
248 if (map_ptr->type != _DRM_SHM) {
249 if (copy_to_user(&argp->handle, &map_ptr->offset,
250 sizeof(map_ptr->offset)))
258 * Remove a map private from list and deallocate resources if the mapping
261 * \param inode device inode.
262 * \param filp file pointer.
263 * \param cmd command.
264 * \param arg pointer to a drm_map_t structure.
265 * \return zero on success or a negative value on error.
267 * Searches the map on drm_device::maplist, removes it from the list, see if
268 * its being used, and free any associate resource (such as MTRR's) if it's not
273 int drm_rmmap(drm_device_t *dev, void *handle)
275 struct list_head *list;
276 drm_map_list_t *r_list = NULL;
277 drm_vma_entry_t *pt, *prev;
281 down(&dev->struct_sem);
282 list = &dev->maplist->head;
283 list_for_each(list, &dev->maplist->head) {
284 r_list = list_entry(list, drm_map_list_t, head);
287 r_list->map->handle == handle &&
288 r_list->map->flags & _DRM_REMOVABLE) break;
291 /* List has wrapped around to the head pointer, or its empty we didn't
294 if(list == (&dev->maplist->head)) {
295 up(&dev->struct_sem);
300 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
302 for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
303 if (pt->vma->vm_private_data == map) found_maps++;
307 drm_dma_handle_t dmah;
311 case _DRM_FRAME_BUFFER:
312 if (drm_core_has_MTRR(dev)) {
313 if (map->mtrr >= 0) {
315 retcode = mtrr_del(map->mtrr,
318 DRM_DEBUG("mtrr_del = %d\n", retcode);
321 drm_ioremapfree(map->handle, map->size, dev);
327 case _DRM_SCATTER_GATHER:
329 case _DRM_CONSISTENT:
330 dmah.vaddr = map->handle;
331 dmah.busaddr = map->offset;
332 dmah.size = map->size;
333 __drm_pci_free(dev, &dmah);
336 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
338 up(&dev->struct_sem);
341 EXPORT_SYMBOL(drm_rmmap);
343 int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
344 unsigned int cmd, unsigned long arg)
346 drm_file_t *priv = filp->private_data;
347 drm_device_t *dev = priv->head->dev;
350 if (copy_from_user(&request, (drm_map_t __user *)arg, sizeof(request))) {
354 return drm_rmmap(dev, request.handle);
358 * Cleanup after an error on one of the addbufs() functions.
360 * \param entry buffer entry where the error occurred.
362 * Frees any pages and buffers associated with the given entry.
364 static void drm_cleanup_buf_error(drm_device_t *dev, drm_buf_entry_t *entry)
368 if (entry->seg_count) {
369 for (i = 0; i < entry->seg_count; i++) {
370 if (entry->seglist[i]) {
371 drm_free_pages(entry->seglist[i],
376 drm_free(entry->seglist,
378 sizeof(*entry->seglist),
381 entry->seg_count = 0;
384 if (entry->buf_count) {
385 for (i = 0; i < entry->buf_count; i++) {
386 if (entry->buflist[i].dev_private) {
387 drm_free(entry->buflist[i].dev_private,
388 entry->buflist[i].dev_priv_size,
392 drm_free(entry->buflist,
394 sizeof(*entry->buflist),
397 entry->buf_count = 0;
403 * Add AGP buffers for DMA transfers.
405 * \param dev drm_device_t to which the buffers are to be added.
406 * \param request pointer to a drm_buf_desc_t describing the request.
407 * \return zero on success or a negative number on failure.
409 * After some sanity checks creates a drm_buf structure for each buffer and
410 * reallocates the buffer list of the same size order to accommodate the new
413 int drm_addbufs_agp(drm_device_t *dev, drm_buf_desc_t *request)
415 drm_device_dma_t *dma = dev->dma;
416 drm_buf_entry_t *entry;
418 unsigned long offset;
419 unsigned long agp_offset;
428 drm_buf_t **temp_buflist;
430 if ( !dma ) return -EINVAL;
432 count = request->count;
433 order = drm_order(request->size);
436 alignment = (request->flags & _DRM_PAGE_ALIGN)
437 ? PAGE_ALIGN(size) : size;
438 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
439 total = PAGE_SIZE << page_order;
442 agp_offset = dev->agp->base + request->agp_start;
444 DRM_DEBUG( "count: %d\n", count );
445 DRM_DEBUG( "order: %d\n", order );
446 DRM_DEBUG( "size: %d\n", size );
447 DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
448 DRM_DEBUG( "alignment: %d\n", alignment );
449 DRM_DEBUG( "page_order: %d\n", page_order );
450 DRM_DEBUG( "total: %d\n", total );
452 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
453 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
455 spin_lock( &dev->count_lock );
456 if ( dev->buf_use ) {
457 spin_unlock( &dev->count_lock );
460 atomic_inc( &dev->buf_alloc );
461 spin_unlock( &dev->count_lock );
463 down( &dev->struct_sem );
464 entry = &dma->bufs[order];
465 if ( entry->buf_count ) {
466 up( &dev->struct_sem );
467 atomic_dec( &dev->buf_alloc );
468 return -ENOMEM; /* May only call once for each order */
471 if (count < 0 || count > 4096) {
472 up( &dev->struct_sem );
473 atomic_dec( &dev->buf_alloc );
477 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
479 if ( !entry->buflist ) {
480 up( &dev->struct_sem );
481 atomic_dec( &dev->buf_alloc );
484 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
486 entry->buf_size = size;
487 entry->page_order = page_order;
491 while ( entry->buf_count < count ) {
492 buf = &entry->buflist[entry->buf_count];
493 buf->idx = dma->buf_count + entry->buf_count;
494 buf->total = alignment;
498 buf->offset = (dma->byte_count + offset);
499 buf->bus_address = agp_offset + offset;
500 buf->address = (void *)(agp_offset + offset);
504 init_waitqueue_head( &buf->dma_wait );
507 buf->dev_priv_size = dev->driver->dev_priv_size;
508 buf->dev_private = drm_alloc( buf->dev_priv_size,
510 if(!buf->dev_private) {
511 /* Set count correctly so we free the proper amount. */
512 entry->buf_count = count;
513 drm_cleanup_buf_error(dev,entry);
514 up( &dev->struct_sem );
515 atomic_dec( &dev->buf_alloc );
518 memset( buf->dev_private, 0, buf->dev_priv_size );
520 DRM_DEBUG( "buffer %d @ %p\n",
521 entry->buf_count, buf->address );
525 byte_count += PAGE_SIZE << page_order;
528 DRM_DEBUG( "byte_count: %d\n", byte_count );
530 temp_buflist = drm_realloc( dma->buflist,
531 dma->buf_count * sizeof(*dma->buflist),
532 (dma->buf_count + entry->buf_count)
533 * sizeof(*dma->buflist),
536 /* Free the entry because it isn't valid */
537 drm_cleanup_buf_error(dev,entry);
538 up( &dev->struct_sem );
539 atomic_dec( &dev->buf_alloc );
542 dma->buflist = temp_buflist;
544 for ( i = 0 ; i < entry->buf_count ; i++ ) {
545 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
548 dma->buf_count += entry->buf_count;
549 dma->byte_count += byte_count;
551 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
552 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
554 up( &dev->struct_sem );
556 request->count = entry->buf_count;
557 request->size = size;
559 dma->flags = _DRM_DMA_USE_AGP;
561 atomic_dec( &dev->buf_alloc );
564 EXPORT_SYMBOL(drm_addbufs_agp);
565 #endif /* __OS_HAS_AGP */
567 int drm_addbufs_pci(drm_device_t *dev, drm_buf_desc_t *request)
569 drm_device_dma_t *dma = dev->dma;
575 drm_buf_entry_t *entry;
579 unsigned long offset;
583 unsigned long *temp_pagelist;
584 drm_buf_t **temp_buflist;
586 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) return -EINVAL;
587 if ( !dma ) return -EINVAL;
589 count = request->count;
590 order = drm_order(request->size);
593 DRM_DEBUG( "count=%d, size=%d (%d), order=%d, queue_count=%d\n",
594 request->count, request->size, size,
595 order, dev->queue_count );
597 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
598 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
600 alignment = (request->flags & _DRM_PAGE_ALIGN)
601 ? PAGE_ALIGN(size) : size;
602 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
603 total = PAGE_SIZE << page_order;
605 spin_lock( &dev->count_lock );
606 if ( dev->buf_use ) {
607 spin_unlock( &dev->count_lock );
610 atomic_inc( &dev->buf_alloc );
611 spin_unlock( &dev->count_lock );
613 down( &dev->struct_sem );
614 entry = &dma->bufs[order];
615 if ( entry->buf_count ) {
616 up( &dev->struct_sem );
617 atomic_dec( &dev->buf_alloc );
618 return -ENOMEM; /* May only call once for each order */
621 if (count < 0 || count > 4096) {
622 up( &dev->struct_sem );
623 atomic_dec( &dev->buf_alloc );
627 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
629 if ( !entry->buflist ) {
630 up( &dev->struct_sem );
631 atomic_dec( &dev->buf_alloc );
634 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
636 entry->seglist = drm_alloc( count * sizeof(*entry->seglist),
638 if ( !entry->seglist ) {
639 drm_free( entry->buflist,
640 count * sizeof(*entry->buflist),
642 up( &dev->struct_sem );
643 atomic_dec( &dev->buf_alloc );
646 memset( entry->seglist, 0, count * sizeof(*entry->seglist) );
648 /* Keep the original pagelist until we know all the allocations
651 temp_pagelist = drm_alloc( (dma->page_count + (count << page_order))
652 * sizeof(*dma->pagelist),
654 if (!temp_pagelist) {
655 drm_free( entry->buflist,
656 count * sizeof(*entry->buflist),
658 drm_free( entry->seglist,
659 count * sizeof(*entry->seglist),
661 up( &dev->struct_sem );
662 atomic_dec( &dev->buf_alloc );
665 memcpy(temp_pagelist,
667 dma->page_count * sizeof(*dma->pagelist));
668 DRM_DEBUG( "pagelist: %d entries\n",
669 dma->page_count + (count << page_order) );
671 entry->buf_size = size;
672 entry->page_order = page_order;
676 while ( entry->buf_count < count ) {
677 page = drm_alloc_pages( page_order, DRM_MEM_DMA );
679 /* Set count correctly so we free the proper amount. */
680 entry->buf_count = count;
681 entry->seg_count = count;
682 drm_cleanup_buf_error(dev, entry);
683 drm_free( temp_pagelist,
684 (dma->page_count + (count << page_order))
685 * sizeof(*dma->pagelist),
687 up( &dev->struct_sem );
688 atomic_dec( &dev->buf_alloc );
691 entry->seglist[entry->seg_count++] = page;
692 for ( i = 0 ; i < (1 << page_order) ; i++ ) {
693 DRM_DEBUG( "page %d @ 0x%08lx\n",
694 dma->page_count + page_count,
695 page + PAGE_SIZE * i );
696 temp_pagelist[dma->page_count + page_count++]
697 = page + PAGE_SIZE * i;
700 offset + size <= total && entry->buf_count < count ;
701 offset += alignment, ++entry->buf_count ) {
702 buf = &entry->buflist[entry->buf_count];
703 buf->idx = dma->buf_count + entry->buf_count;
704 buf->total = alignment;
707 buf->offset = (dma->byte_count + byte_count + offset);
708 buf->address = (void *)(page + offset);
712 init_waitqueue_head( &buf->dma_wait );
715 buf->dev_priv_size = dev->driver->dev_priv_size;
716 buf->dev_private = drm_alloc( buf->dev_priv_size,
718 if(!buf->dev_private) {
719 /* Set count correctly so we free the proper amount. */
720 entry->buf_count = count;
721 entry->seg_count = count;
722 drm_cleanup_buf_error(dev,entry);
723 drm_free( temp_pagelist,
724 (dma->page_count + (count << page_order))
725 * sizeof(*dma->pagelist),
727 up( &dev->struct_sem );
728 atomic_dec( &dev->buf_alloc );
731 memset( buf->dev_private, 0, buf->dev_priv_size );
733 DRM_DEBUG( "buffer %d @ %p\n",
734 entry->buf_count, buf->address );
736 byte_count += PAGE_SIZE << page_order;
739 temp_buflist = drm_realloc( dma->buflist,
740 dma->buf_count * sizeof(*dma->buflist),
741 (dma->buf_count + entry->buf_count)
742 * sizeof(*dma->buflist),
745 /* Free the entry because it isn't valid */
746 drm_cleanup_buf_error(dev,entry);
747 drm_free( temp_pagelist,
748 (dma->page_count + (count << page_order))
749 * sizeof(*dma->pagelist),
751 up( &dev->struct_sem );
752 atomic_dec( &dev->buf_alloc );
755 dma->buflist = temp_buflist;
757 for ( i = 0 ; i < entry->buf_count ; i++ ) {
758 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
761 /* No allocations failed, so now we can replace the orginal pagelist
764 if (dma->page_count) {
765 drm_free(dma->pagelist,
766 dma->page_count * sizeof(*dma->pagelist),
769 dma->pagelist = temp_pagelist;
771 dma->buf_count += entry->buf_count;
772 dma->seg_count += entry->seg_count;
773 dma->page_count += entry->seg_count << page_order;
774 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
776 up( &dev->struct_sem );
778 request->count = entry->buf_count;
779 request->size = size;
781 atomic_dec( &dev->buf_alloc );
785 EXPORT_SYMBOL(drm_addbufs_pci);
787 static int drm_addbufs_sg(drm_device_t *dev, drm_buf_desc_t *request)
789 drm_device_dma_t *dma = dev->dma;
790 drm_buf_entry_t *entry;
792 unsigned long offset;
793 unsigned long agp_offset;
802 drm_buf_t **temp_buflist;
804 if (!drm_core_check_feature(dev, DRIVER_SG)) return -EINVAL;
806 if ( !dma ) return -EINVAL;
808 count = request->count;
809 order = drm_order(request->size);
812 alignment = (request->flags & _DRM_PAGE_ALIGN)
813 ? PAGE_ALIGN(size) : size;
814 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
815 total = PAGE_SIZE << page_order;
818 agp_offset = request->agp_start;
820 DRM_DEBUG( "count: %d\n", count );
821 DRM_DEBUG( "order: %d\n", order );
822 DRM_DEBUG( "size: %d\n", size );
823 DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
824 DRM_DEBUG( "alignment: %d\n", alignment );
825 DRM_DEBUG( "page_order: %d\n", page_order );
826 DRM_DEBUG( "total: %d\n", total );
828 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
829 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
831 spin_lock( &dev->count_lock );
832 if ( dev->buf_use ) {
833 spin_unlock( &dev->count_lock );
836 atomic_inc( &dev->buf_alloc );
837 spin_unlock( &dev->count_lock );
839 down( &dev->struct_sem );
840 entry = &dma->bufs[order];
841 if ( entry->buf_count ) {
842 up( &dev->struct_sem );
843 atomic_dec( &dev->buf_alloc );
844 return -ENOMEM; /* May only call once for each order */
847 if (count < 0 || count > 4096) {
848 up( &dev->struct_sem );
849 atomic_dec( &dev->buf_alloc );
853 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
855 if ( !entry->buflist ) {
856 up( &dev->struct_sem );
857 atomic_dec( &dev->buf_alloc );
860 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
862 entry->buf_size = size;
863 entry->page_order = page_order;
867 while ( entry->buf_count < count ) {
868 buf = &entry->buflist[entry->buf_count];
869 buf->idx = dma->buf_count + entry->buf_count;
870 buf->total = alignment;
874 buf->offset = (dma->byte_count + offset);
875 buf->bus_address = agp_offset + offset;
876 buf->address = (void *)(agp_offset + offset + dev->sg->handle);
880 init_waitqueue_head( &buf->dma_wait );
883 buf->dev_priv_size = dev->driver->dev_priv_size;
884 buf->dev_private = drm_alloc( buf->dev_priv_size,
886 if(!buf->dev_private) {
887 /* Set count correctly so we free the proper amount. */
888 entry->buf_count = count;
889 drm_cleanup_buf_error(dev,entry);
890 up( &dev->struct_sem );
891 atomic_dec( &dev->buf_alloc );
895 memset( buf->dev_private, 0, buf->dev_priv_size );
897 DRM_DEBUG( "buffer %d @ %p\n",
898 entry->buf_count, buf->address );
902 byte_count += PAGE_SIZE << page_order;
905 DRM_DEBUG( "byte_count: %d\n", byte_count );
907 temp_buflist = drm_realloc( dma->buflist,
908 dma->buf_count * sizeof(*dma->buflist),
909 (dma->buf_count + entry->buf_count)
910 * sizeof(*dma->buflist),
913 /* Free the entry because it isn't valid */
914 drm_cleanup_buf_error(dev,entry);
915 up( &dev->struct_sem );
916 atomic_dec( &dev->buf_alloc );
919 dma->buflist = temp_buflist;
921 for ( i = 0 ; i < entry->buf_count ; i++ ) {
922 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
925 dma->buf_count += entry->buf_count;
926 dma->byte_count += byte_count;
928 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
929 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
931 up( &dev->struct_sem );
933 request->count = entry->buf_count;
934 request->size = size;
936 dma->flags = _DRM_DMA_USE_SG;
938 atomic_dec( &dev->buf_alloc );
942 int drm_addbufs_fb(drm_device_t *dev, drm_buf_desc_t *request)
944 drm_device_dma_t *dma = dev->dma;
945 drm_buf_entry_t *entry;
947 unsigned long offset;
948 unsigned long agp_offset;
957 drm_buf_t **temp_buflist;
959 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
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 down(&dev->struct_sem);
999 entry = &dma->bufs[order];
1000 if (entry->buf_count) {
1001 up(&dev->struct_sem);
1002 atomic_dec(&dev->buf_alloc);
1003 return -ENOMEM; /* May only call once for each order */
1006 if (count < 0 || count > 4096) {
1007 up(&dev->struct_sem);
1008 atomic_dec(&dev->buf_alloc);
1012 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1014 if (!entry->buflist) {
1015 up(&dev->struct_sem);
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);
1039 init_waitqueue_head(&buf->dma_wait);
1042 buf->dev_priv_size = dev->driver->dev_priv_size;
1043 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1044 if (!buf->dev_private) {
1045 /* Set count correctly so we free the proper amount. */
1046 entry->buf_count = count;
1047 drm_cleanup_buf_error(dev, entry);
1048 up(&dev->struct_sem);
1049 atomic_dec(&dev->buf_alloc);
1052 memset(buf->dev_private, 0, buf->dev_priv_size);
1054 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1056 offset += alignment;
1058 byte_count += PAGE_SIZE << page_order;
1061 DRM_DEBUG("byte_count: %d\n", byte_count);
1063 temp_buflist = drm_realloc(dma->buflist,
1064 dma->buf_count * sizeof(*dma->buflist),
1065 (dma->buf_count + entry->buf_count)
1066 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1067 if (!temp_buflist) {
1068 /* Free the entry because it isn't valid */
1069 drm_cleanup_buf_error(dev, entry);
1070 up(&dev->struct_sem);
1071 atomic_dec(&dev->buf_alloc);
1074 dma->buflist = temp_buflist;
1076 for (i = 0; i < entry->buf_count; i++) {
1077 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1080 dma->buf_count += entry->buf_count;
1081 dma->byte_count += byte_count;
1083 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1084 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1086 up(&dev->struct_sem);
1088 request->count = entry->buf_count;
1089 request->size = size;
1091 dma->flags = _DRM_DMA_USE_FB;
1093 atomic_dec(&dev->buf_alloc);
1098 * Add buffers for DMA transfers (ioctl).
1100 * \param inode device inode.
1101 * \param filp file pointer.
1102 * \param cmd command.
1103 * \param arg pointer to a drm_buf_desc_t request.
1104 * \return zero on success or a negative number on failure.
1106 * According with the memory type specified in drm_buf_desc::flags and the
1107 * build options, it dispatches the call either to addbufs_agp(),
1108 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1109 * PCI memory respectively.
1111 int drm_addbufs( struct inode *inode, struct file *filp,
1112 unsigned int cmd, unsigned long arg )
1114 drm_buf_desc_t request;
1115 drm_file_t *priv = filp->private_data;
1116 drm_device_t *dev = priv->head->dev;
1119 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1122 if ( copy_from_user( &request, (drm_buf_desc_t __user *)arg,
1127 if ( request.flags & _DRM_AGP_BUFFER )
1128 ret=drm_addbufs_agp(dev, &request);
1131 if ( request.flags & _DRM_SG_BUFFER )
1132 ret=drm_addbufs_sg(dev, &request);
1133 else if ( request.flags & _DRM_FB_BUFFER)
1134 ret=drm_addbufs_fb(dev, &request);
1136 ret=drm_addbufs_pci(dev, &request);
1139 if (copy_to_user((void __user *)arg, &request,
1149 * Get information about the buffer mappings.
1151 * This was originally mean for debugging purposes, or by a sophisticated
1152 * client library to determine how best to use the available buffers (e.g.,
1153 * large buffers can be used for image transfer).
1155 * \param inode device inode.
1156 * \param filp file pointer.
1157 * \param cmd command.
1158 * \param arg pointer to a drm_buf_info structure.
1159 * \return zero on success or a negative number on failure.
1161 * Increments drm_device::buf_use while holding the drm_device::count_lock
1162 * lock, preventing of allocating more buffers after this call. Information
1163 * about each requested buffer is then copied into user space.
1165 int drm_infobufs( struct inode *inode, struct file *filp,
1166 unsigned int cmd, unsigned long arg )
1168 drm_file_t *priv = filp->private_data;
1169 drm_device_t *dev = priv->head->dev;
1170 drm_device_dma_t *dma = dev->dma;
1171 drm_buf_info_t request;
1172 drm_buf_info_t __user *argp = (void __user *)arg;
1176 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1179 if ( !dma ) return -EINVAL;
1181 spin_lock( &dev->count_lock );
1182 if ( atomic_read( &dev->buf_alloc ) ) {
1183 spin_unlock( &dev->count_lock );
1186 ++dev->buf_use; /* Can't allocate more after this call */
1187 spin_unlock( &dev->count_lock );
1189 if ( copy_from_user( &request, argp, sizeof(request) ) )
1192 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
1193 if ( dma->bufs[i].buf_count ) ++count;
1196 DRM_DEBUG( "count = %d\n", count );
1198 if ( request.count >= count ) {
1199 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
1200 if ( dma->bufs[i].buf_count ) {
1201 drm_buf_desc_t __user *to = &request.list[count];
1202 drm_buf_entry_t *from = &dma->bufs[i];
1203 drm_freelist_t *list = &dma->bufs[i].freelist;
1204 if ( copy_to_user( &to->count,
1206 sizeof(from->buf_count) ) ||
1207 copy_to_user( &to->size,
1209 sizeof(from->buf_size) ) ||
1210 copy_to_user( &to->low_mark,
1212 sizeof(list->low_mark) ) ||
1213 copy_to_user( &to->high_mark,
1215 sizeof(list->high_mark) ) )
1218 DRM_DEBUG( "%d %d %d %d %d\n",
1220 dma->bufs[i].buf_count,
1221 dma->bufs[i].buf_size,
1222 dma->bufs[i].freelist.low_mark,
1223 dma->bufs[i].freelist.high_mark );
1228 request.count = count;
1230 if ( copy_to_user( argp, &request, sizeof(request) ) )
1237 * Specifies a low and high water mark for buffer allocation
1239 * \param inode device inode.
1240 * \param filp file pointer.
1241 * \param cmd command.
1242 * \param arg a pointer to a drm_buf_desc structure.
1243 * \return zero on success or a negative number on failure.
1245 * Verifies that the size order is bounded between the admissible orders and
1246 * updates the respective drm_device_dma::bufs entry low and high water mark.
1248 * \note This ioctl is deprecated and mostly never used.
1250 int drm_markbufs( struct inode *inode, struct file *filp,
1251 unsigned int cmd, unsigned long arg )
1253 drm_file_t *priv = filp->private_data;
1254 drm_device_t *dev = priv->head->dev;
1255 drm_device_dma_t *dma = dev->dma;
1256 drm_buf_desc_t request;
1258 drm_buf_entry_t *entry;
1260 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1263 if ( !dma ) return -EINVAL;
1265 if ( copy_from_user( &request,
1266 (drm_buf_desc_t __user *)arg,
1270 DRM_DEBUG( "%d, %d, %d\n",
1271 request.size, request.low_mark, request.high_mark );
1272 order = drm_order( request.size );
1273 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
1274 entry = &dma->bufs[order];
1276 if ( request.low_mark < 0 || request.low_mark > entry->buf_count )
1278 if ( request.high_mark < 0 || request.high_mark > entry->buf_count )
1281 entry->freelist.low_mark = request.low_mark;
1282 entry->freelist.high_mark = request.high_mark;
1288 * Unreserve the buffers in list, previously reserved using drmDMA.
1290 * \param inode device inode.
1291 * \param filp file pointer.
1292 * \param cmd command.
1293 * \param arg pointer to a drm_buf_free structure.
1294 * \return zero on success or a negative number on failure.
1296 * Calls free_buffer() for each used buffer.
1297 * This function is primarily used for debugging.
1299 int drm_freebufs( struct inode *inode, struct file *filp,
1300 unsigned int cmd, unsigned long arg )
1302 drm_file_t *priv = filp->private_data;
1303 drm_device_t *dev = priv->head->dev;
1304 drm_device_dma_t *dma = dev->dma;
1305 drm_buf_free_t request;
1310 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1313 if ( !dma ) return -EINVAL;
1315 if ( copy_from_user( &request,
1316 (drm_buf_free_t __user *)arg,
1320 DRM_DEBUG( "%d\n", request.count );
1321 for ( i = 0 ; i < request.count ; i++ ) {
1322 if ( copy_from_user( &idx,
1326 if ( idx < 0 || idx >= dma->buf_count ) {
1327 DRM_ERROR( "Index %d (of %d max)\n",
1328 idx, dma->buf_count - 1 );
1331 buf = dma->buflist[idx];
1332 if ( buf->filp != filp ) {
1333 DRM_ERROR( "Process %d freeing buffer not owned\n",
1337 drm_free_buffer( dev, buf );
1344 * Maps all of the DMA buffers into client-virtual space (ioctl).
1346 * \param inode device inode.
1347 * \param filp file pointer.
1348 * \param cmd command.
1349 * \param arg pointer to a drm_buf_map structure.
1350 * \return zero on success or a negative number on failure.
1352 * Maps the AGP or SG buffer region with do_mmap(), and copies information
1353 * about each buffer into user space. The PCI buffers are already mapped on the
1354 * addbufs_pci() call.
1356 int drm_mapbufs( struct inode *inode, struct file *filp,
1357 unsigned int cmd, unsigned long arg )
1359 drm_file_t *priv = filp->private_data;
1360 drm_device_t *dev = priv->head->dev;
1361 drm_device_dma_t *dma = dev->dma;
1362 drm_buf_map_t __user *argp = (void __user *)arg;
1365 unsigned long virtual;
1366 unsigned long address;
1367 drm_buf_map_t request;
1370 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1373 if ( !dma ) return -EINVAL;
1375 spin_lock( &dev->count_lock );
1376 if ( atomic_read( &dev->buf_alloc ) ) {
1377 spin_unlock( &dev->count_lock );
1380 dev->buf_use++; /* Can't allocate more after this call */
1381 spin_unlock( &dev->count_lock );
1383 if ( copy_from_user( &request, argp, sizeof(request) ) )
1386 if ( request.count >= dma->buf_count ) {
1387 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1388 || (drm_core_check_feature(dev, DRIVER_SG)
1389 && (dma->flags & _DRM_DMA_USE_SG))
1390 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1391 && (dma->flags & _DRM_DMA_USE_FB))) {
1392 drm_map_t *map = dev->agp_buffer_map;
1399 #if LINUX_VERSION_CODE <= 0x020402
1400 down( ¤t->mm->mmap_sem );
1402 down_write( ¤t->mm->mmap_sem );
1404 virtual = do_mmap( filp, 0, map->size,
1405 PROT_READ | PROT_WRITE,
1407 (unsigned long)map->offset );
1408 #if LINUX_VERSION_CODE <= 0x020402
1409 up( ¤t->mm->mmap_sem );
1411 up_write( ¤t->mm->mmap_sem );
1414 #if LINUX_VERSION_CODE <= 0x020402
1415 down( ¤t->mm->mmap_sem );
1417 down_write( ¤t->mm->mmap_sem );
1419 virtual = do_mmap( filp, 0, dma->byte_count,
1420 PROT_READ | PROT_WRITE,
1422 #if LINUX_VERSION_CODE <= 0x020402
1423 up( ¤t->mm->mmap_sem );
1425 up_write( ¤t->mm->mmap_sem );
1428 if ( virtual > -1024UL ) {
1430 retcode = (signed long)virtual;
1433 request.virtual = (void __user *)virtual;
1435 for ( i = 0 ; i < dma->buf_count ; i++ ) {
1436 if ( copy_to_user( &request.list[i].idx,
1437 &dma->buflist[i]->idx,
1438 sizeof(request.list[0].idx) ) ) {
1442 if ( copy_to_user( &request.list[i].total,
1443 &dma->buflist[i]->total,
1444 sizeof(request.list[0].total) ) ) {
1448 if ( copy_to_user( &request.list[i].used,
1454 address = virtual + dma->buflist[i]->offset; /* *** */
1455 if ( copy_to_user( &request.list[i].address,
1457 sizeof(address) ) ) {
1464 request.count = dma->buf_count;
1465 DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
1467 if ( copy_to_user( argp, &request, sizeof(request) ) )