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( struct inode *inode, struct file *filp,
86 unsigned int cmd, unsigned long arg )
88 drm_file_t *priv = filp->private_data;
89 drm_device_t *dev = priv->head->dev;
91 drm_map_t __user *argp = (void __user *)arg;
94 if ( !(filp->f_mode & 3) ) return -EACCES; /* Require read/write */
96 map = drm_alloc( sizeof(*map), DRM_MEM_MAPS );
100 if ( copy_from_user( map, argp, sizeof(*map) ) ) {
101 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
105 /* Only allow shared memory to be removable since we only keep enough
106 * book keeping information about shared memory to allow for removal
107 * when processes fork.
109 if ( (map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM ) {
110 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
113 DRM_DEBUG( "offset = 0x%08lx, size = 0x%08lx, type = %d\n",
114 map->offset, map->size, map->type );
115 if ( (map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK)) ) {
116 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
122 switch ( map->type ) {
124 case _DRM_FRAME_BUFFER:
125 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__)
126 if ( map->offset + map->size < map->offset ||
127 map->offset < virt_to_phys(high_memory) ) {
128 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
133 map->offset += dev->hose->mem_space->start;
135 if (drm_core_has_MTRR(dev)) {
136 if ( map->type == _DRM_FRAME_BUFFER ||
137 (map->flags & _DRM_WRITE_COMBINING) ) {
138 map->mtrr = mtrr_add( map->offset, map->size,
139 MTRR_TYPE_WRCOMB, 1 );
142 if (map->type == _DRM_REGISTERS)
143 map->handle = drm_ioremap( map->offset, map->size,
148 map->handle = vmalloc_32(map->size);
149 DRM_DEBUG( "%lu %d %p\n",
150 map->size, drm_order( map->size ), map->handle );
151 if ( !map->handle ) {
152 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
155 map->offset = (unsigned long)map->handle;
156 if ( map->flags & _DRM_CONTAINS_LOCK ) {
157 /* Prevent a 2nd X Server from creating a 2nd lock */
158 if (dev->lock.hw_lock != NULL) {
159 vfree( map->handle );
160 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
164 dev->lock.hw_lock = map->handle; /* Pointer to lock */
168 if (drm_core_has_AGP(dev)) {
170 map->offset += dev->hose->mem_space->start;
172 map->offset += dev->agp->base;
173 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
176 case _DRM_SCATTER_GATHER:
178 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
181 map->offset += dev->sg->handle;
185 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
189 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
191 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
194 memset(list, 0, sizeof(*list));
197 down(&dev->struct_sem);
198 list_add(&list->head, &dev->maplist->head);
200 /* Assign a 32-bit handle for _DRM_SHM mappings */
201 /* We do it here so that dev->struct_sem protects the increment */
202 if (map->type == _DRM_SHM)
203 map->offset = map32_handle += PAGE_SIZE;
205 up(&dev->struct_sem);
207 if ( copy_to_user( argp, map, sizeof(*map) ) )
209 if (copy_to_user(&argp->handle, &map->offset, sizeof(map->offset)))
216 * Remove a map private from list and deallocate resources if the mapping
219 * \param inode device inode.
220 * \param filp file pointer.
221 * \param cmd command.
222 * \param arg pointer to a drm_map_t structure.
223 * \return zero on success or a negative value on error.
225 * Searches the map on drm_device::maplist, removes it from the list, see if
226 * its being used, and free any associate resource (such as MTRR's) if it's not
231 int drm_rmmap(struct inode *inode, struct file *filp,
232 unsigned int cmd, unsigned long arg)
234 drm_file_t *priv = filp->private_data;
235 drm_device_t *dev = priv->head->dev;
236 struct list_head *list;
237 drm_map_list_t *r_list = NULL;
238 drm_vma_entry_t *pt, *prev;
243 if (copy_from_user(&request, (drm_map_t __user *)arg,
248 down(&dev->struct_sem);
249 list = &dev->maplist->head;
250 list_for_each(list, &dev->maplist->head) {
251 r_list = list_entry(list, drm_map_list_t, head);
254 r_list->map->offset == (unsigned long) request.handle &&
255 r_list->map->flags & _DRM_REMOVABLE) break;
258 /* List has wrapped around to the head pointer, or its empty we didn't
261 if(list == (&dev->maplist->head)) {
262 up(&dev->struct_sem);
267 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
269 for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
270 if (pt->vma->vm_private_data == map) found_maps++;
276 case _DRM_FRAME_BUFFER:
277 if (drm_core_has_MTRR(dev)) {
278 if (map->mtrr >= 0) {
280 retcode = mtrr_del(map->mtrr,
283 DRM_DEBUG("mtrr_del = %d\n", retcode);
286 drm_ioremapfree(map->handle, map->size, dev);
292 case _DRM_SCATTER_GATHER:
295 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
297 up(&dev->struct_sem);
302 * Cleanup after an error on one of the addbufs() functions.
304 * \param entry buffer entry where the error occurred.
306 * Frees any pages and buffers associated with the given entry.
308 static void drm_cleanup_buf_error(drm_device_t *dev, drm_buf_entry_t *entry)
312 if (entry->seg_count) {
313 for (i = 0; i < entry->seg_count; i++) {
314 if (entry->seglist[i]) {
315 drm_free_pages(entry->seglist[i],
320 drm_free(entry->seglist,
322 sizeof(*entry->seglist),
325 entry->seg_count = 0;
328 if (entry->buf_count) {
329 for (i = 0; i < entry->buf_count; i++) {
330 if (entry->buflist[i].dev_private) {
331 drm_free(entry->buflist[i].dev_private,
332 entry->buflist[i].dev_priv_size,
336 drm_free(entry->buflist,
338 sizeof(*entry->buflist),
341 entry->buf_count = 0;
347 * Add AGP buffers for DMA transfers (ioctl).
349 * \param inode device inode.
350 * \param filp file pointer.
351 * \param cmd command.
352 * \param arg pointer to a drm_buf_desc_t request.
353 * \return zero on success or a negative number on failure.
355 * After some sanity checks creates a drm_buf structure for each buffer and
356 * reallocates the buffer list of the same size order to accommodate the new
359 static int drm_addbufs_agp( struct inode *inode, struct file *filp,
360 unsigned int cmd, unsigned long arg )
362 drm_file_t *priv = filp->private_data;
363 drm_device_t *dev = priv->head->dev;
364 drm_device_dma_t *dma = dev->dma;
365 drm_buf_desc_t request;
366 drm_buf_entry_t *entry;
368 unsigned long offset;
369 unsigned long agp_offset;
378 drm_buf_t **temp_buflist;
379 drm_buf_desc_t __user *argp = (void __user *)arg;
381 if ( !dma ) return -EINVAL;
383 if ( copy_from_user( &request, argp,
387 count = request.count;
388 order = drm_order( request.size );
391 alignment = (request.flags & _DRM_PAGE_ALIGN)
392 ? PAGE_ALIGN(size) : size;
393 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
394 total = PAGE_SIZE << page_order;
397 agp_offset = dev->agp->base + request.agp_start;
399 DRM_DEBUG( "count: %d\n", count );
400 DRM_DEBUG( "order: %d\n", order );
401 DRM_DEBUG( "size: %d\n", size );
402 DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
403 DRM_DEBUG( "alignment: %d\n", alignment );
404 DRM_DEBUG( "page_order: %d\n", page_order );
405 DRM_DEBUG( "total: %d\n", total );
407 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
408 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
410 spin_lock( &dev->count_lock );
411 if ( dev->buf_use ) {
412 spin_unlock( &dev->count_lock );
415 atomic_inc( &dev->buf_alloc );
416 spin_unlock( &dev->count_lock );
418 down( &dev->struct_sem );
419 entry = &dma->bufs[order];
420 if ( entry->buf_count ) {
421 up( &dev->struct_sem );
422 atomic_dec( &dev->buf_alloc );
423 return -ENOMEM; /* May only call once for each order */
426 if (count < 0 || count > 4096) {
427 up( &dev->struct_sem );
428 atomic_dec( &dev->buf_alloc );
432 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
434 if ( !entry->buflist ) {
435 up( &dev->struct_sem );
436 atomic_dec( &dev->buf_alloc );
439 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
441 entry->buf_size = size;
442 entry->page_order = page_order;
446 while ( entry->buf_count < count ) {
447 buf = &entry->buflist[entry->buf_count];
448 buf->idx = dma->buf_count + entry->buf_count;
449 buf->total = alignment;
453 buf->offset = (dma->byte_count + offset);
454 buf->bus_address = agp_offset + offset;
455 buf->address = (void *)(agp_offset + offset);
459 init_waitqueue_head( &buf->dma_wait );
462 buf->dev_priv_size = dev->driver->dev_priv_size;
463 buf->dev_private = drm_alloc( buf->dev_priv_size,
465 if(!buf->dev_private) {
466 /* Set count correctly so we free the proper amount. */
467 entry->buf_count = count;
468 drm_cleanup_buf_error(dev,entry);
469 up( &dev->struct_sem );
470 atomic_dec( &dev->buf_alloc );
473 memset( buf->dev_private, 0, buf->dev_priv_size );
475 DRM_DEBUG( "buffer %d @ %p\n",
476 entry->buf_count, buf->address );
480 byte_count += PAGE_SIZE << page_order;
483 DRM_DEBUG( "byte_count: %d\n", byte_count );
485 temp_buflist = drm_realloc( dma->buflist,
486 dma->buf_count * sizeof(*dma->buflist),
487 (dma->buf_count + entry->buf_count)
488 * sizeof(*dma->buflist),
491 /* Free the entry because it isn't valid */
492 drm_cleanup_buf_error(dev,entry);
493 up( &dev->struct_sem );
494 atomic_dec( &dev->buf_alloc );
497 dma->buflist = temp_buflist;
499 for ( i = 0 ; i < entry->buf_count ; i++ ) {
500 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
503 dma->buf_count += entry->buf_count;
504 dma->byte_count += byte_count;
506 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
507 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
509 up( &dev->struct_sem );
511 request.count = entry->buf_count;
514 if ( copy_to_user( argp, &request, sizeof(request) ) )
517 dma->flags = _DRM_DMA_USE_AGP;
519 atomic_dec( &dev->buf_alloc );
522 #endif /* __OS_HAS_AGP */
524 static int drm_addbufs_pci( struct inode *inode, struct file *filp,
525 unsigned int cmd, unsigned long arg )
527 drm_file_t *priv = filp->private_data;
528 drm_device_t *dev = priv->head->dev;
529 drm_device_dma_t *dma = dev->dma;
530 drm_buf_desc_t request;
536 drm_buf_entry_t *entry;
540 unsigned long offset;
544 unsigned long *temp_pagelist;
545 drm_buf_t **temp_buflist;
546 drm_buf_desc_t __user *argp = (void __user *)arg;
548 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) return -EINVAL;
549 if ( !dma ) return -EINVAL;
551 if ( copy_from_user( &request, argp, sizeof(request) ) )
554 count = request.count;
555 order = drm_order( request.size );
558 DRM_DEBUG( "count=%d, size=%d (%d), order=%d, queue_count=%d\n",
559 request.count, request.size, size,
560 order, dev->queue_count );
562 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
563 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
565 alignment = (request.flags & _DRM_PAGE_ALIGN)
566 ? PAGE_ALIGN(size) : size;
567 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
568 total = PAGE_SIZE << page_order;
570 spin_lock( &dev->count_lock );
571 if ( dev->buf_use ) {
572 spin_unlock( &dev->count_lock );
575 atomic_inc( &dev->buf_alloc );
576 spin_unlock( &dev->count_lock );
578 down( &dev->struct_sem );
579 entry = &dma->bufs[order];
580 if ( entry->buf_count ) {
581 up( &dev->struct_sem );
582 atomic_dec( &dev->buf_alloc );
583 return -ENOMEM; /* May only call once for each order */
586 if (count < 0 || count > 4096) {
587 up( &dev->struct_sem );
588 atomic_dec( &dev->buf_alloc );
592 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
594 if ( !entry->buflist ) {
595 up( &dev->struct_sem );
596 atomic_dec( &dev->buf_alloc );
599 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
601 entry->seglist = drm_alloc( count * sizeof(*entry->seglist),
603 if ( !entry->seglist ) {
604 drm_free( entry->buflist,
605 count * sizeof(*entry->buflist),
607 up( &dev->struct_sem );
608 atomic_dec( &dev->buf_alloc );
611 memset( entry->seglist, 0, count * sizeof(*entry->seglist) );
613 /* Keep the original pagelist until we know all the allocations
616 temp_pagelist = drm_alloc( (dma->page_count + (count << page_order))
617 * sizeof(*dma->pagelist),
619 if (!temp_pagelist) {
620 drm_free( entry->buflist,
621 count * sizeof(*entry->buflist),
623 drm_free( entry->seglist,
624 count * sizeof(*entry->seglist),
626 up( &dev->struct_sem );
627 atomic_dec( &dev->buf_alloc );
630 memcpy(temp_pagelist,
632 dma->page_count * sizeof(*dma->pagelist));
633 DRM_DEBUG( "pagelist: %d entries\n",
634 dma->page_count + (count << page_order) );
636 entry->buf_size = size;
637 entry->page_order = page_order;
641 while ( entry->buf_count < count ) {
642 page = drm_alloc_pages( page_order, DRM_MEM_DMA );
644 /* Set count correctly so we free the proper amount. */
645 entry->buf_count = count;
646 entry->seg_count = count;
647 drm_cleanup_buf_error(dev, entry);
648 drm_free( temp_pagelist,
649 (dma->page_count + (count << page_order))
650 * sizeof(*dma->pagelist),
652 up( &dev->struct_sem );
653 atomic_dec( &dev->buf_alloc );
656 entry->seglist[entry->seg_count++] = page;
657 for ( i = 0 ; i < (1 << page_order) ; i++ ) {
658 DRM_DEBUG( "page %d @ 0x%08lx\n",
659 dma->page_count + page_count,
660 page + PAGE_SIZE * i );
661 temp_pagelist[dma->page_count + page_count++]
662 = page + PAGE_SIZE * i;
665 offset + size <= total && entry->buf_count < count ;
666 offset += alignment, ++entry->buf_count ) {
667 buf = &entry->buflist[entry->buf_count];
668 buf->idx = dma->buf_count + entry->buf_count;
669 buf->total = alignment;
672 buf->offset = (dma->byte_count + byte_count + offset);
673 buf->address = (void *)(page + offset);
677 init_waitqueue_head( &buf->dma_wait );
680 buf->dev_priv_size = dev->driver->dev_priv_size;
681 buf->dev_private = drm_alloc( buf->dev_priv_size,
683 if(!buf->dev_private) {
684 /* Set count correctly so we free the proper amount. */
685 entry->buf_count = count;
686 entry->seg_count = count;
687 drm_cleanup_buf_error(dev,entry);
688 drm_free( temp_pagelist,
689 (dma->page_count + (count << page_order))
690 * sizeof(*dma->pagelist),
692 up( &dev->struct_sem );
693 atomic_dec( &dev->buf_alloc );
696 memset( buf->dev_private, 0, buf->dev_priv_size );
698 DRM_DEBUG( "buffer %d @ %p\n",
699 entry->buf_count, buf->address );
701 byte_count += PAGE_SIZE << page_order;
704 temp_buflist = drm_realloc( dma->buflist,
705 dma->buf_count * sizeof(*dma->buflist),
706 (dma->buf_count + entry->buf_count)
707 * sizeof(*dma->buflist),
710 /* Free the entry because it isn't valid */
711 drm_cleanup_buf_error(dev,entry);
712 drm_free( temp_pagelist,
713 (dma->page_count + (count << page_order))
714 * sizeof(*dma->pagelist),
716 up( &dev->struct_sem );
717 atomic_dec( &dev->buf_alloc );
720 dma->buflist = temp_buflist;
722 for ( i = 0 ; i < entry->buf_count ; i++ ) {
723 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
726 /* No allocations failed, so now we can replace the orginal pagelist
729 if (dma->page_count) {
730 drm_free(dma->pagelist,
731 dma->page_count * sizeof(*dma->pagelist),
734 dma->pagelist = temp_pagelist;
736 dma->buf_count += entry->buf_count;
737 dma->seg_count += entry->seg_count;
738 dma->page_count += entry->seg_count << page_order;
739 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
741 up( &dev->struct_sem );
743 request.count = entry->buf_count;
746 if ( copy_to_user( argp, &request, sizeof(request) ) )
749 atomic_dec( &dev->buf_alloc );
754 static int drm_addbufs_sg( struct inode *inode, struct file *filp,
755 unsigned int cmd, unsigned long arg )
757 drm_file_t *priv = filp->private_data;
758 drm_device_t *dev = priv->head->dev;
759 drm_device_dma_t *dma = dev->dma;
760 drm_buf_desc_t __user *argp = (void __user *)arg;
761 drm_buf_desc_t request;
762 drm_buf_entry_t *entry;
764 unsigned long offset;
765 unsigned long agp_offset;
774 drm_buf_t **temp_buflist;
776 if (!drm_core_check_feature(dev, DRIVER_SG)) return -EINVAL;
778 if ( !dma ) return -EINVAL;
780 if ( copy_from_user( &request, argp, sizeof(request) ) )
783 count = request.count;
784 order = drm_order( request.size );
787 alignment = (request.flags & _DRM_PAGE_ALIGN)
788 ? PAGE_ALIGN(size) : size;
789 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
790 total = PAGE_SIZE << page_order;
793 agp_offset = request.agp_start;
795 DRM_DEBUG( "count: %d\n", count );
796 DRM_DEBUG( "order: %d\n", order );
797 DRM_DEBUG( "size: %d\n", size );
798 DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
799 DRM_DEBUG( "alignment: %d\n", alignment );
800 DRM_DEBUG( "page_order: %d\n", page_order );
801 DRM_DEBUG( "total: %d\n", total );
803 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
804 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
806 spin_lock( &dev->count_lock );
807 if ( dev->buf_use ) {
808 spin_unlock( &dev->count_lock );
811 atomic_inc( &dev->buf_alloc );
812 spin_unlock( &dev->count_lock );
814 down( &dev->struct_sem );
815 entry = &dma->bufs[order];
816 if ( entry->buf_count ) {
817 up( &dev->struct_sem );
818 atomic_dec( &dev->buf_alloc );
819 return -ENOMEM; /* May only call once for each order */
822 if (count < 0 || count > 4096) {
823 up( &dev->struct_sem );
824 atomic_dec( &dev->buf_alloc );
828 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
830 if ( !entry->buflist ) {
831 up( &dev->struct_sem );
832 atomic_dec( &dev->buf_alloc );
835 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
837 entry->buf_size = size;
838 entry->page_order = page_order;
842 while ( entry->buf_count < count ) {
843 buf = &entry->buflist[entry->buf_count];
844 buf->idx = dma->buf_count + entry->buf_count;
845 buf->total = alignment;
849 buf->offset = (dma->byte_count + offset);
850 buf->bus_address = agp_offset + offset;
851 buf->address = (void *)(agp_offset + offset + dev->sg->handle);
855 init_waitqueue_head( &buf->dma_wait );
858 buf->dev_priv_size = dev->driver->dev_priv_size;
859 buf->dev_private = drm_alloc( buf->dev_priv_size,
861 if(!buf->dev_private) {
862 /* Set count correctly so we free the proper amount. */
863 entry->buf_count = count;
864 drm_cleanup_buf_error(dev,entry);
865 up( &dev->struct_sem );
866 atomic_dec( &dev->buf_alloc );
870 memset( buf->dev_private, 0, buf->dev_priv_size );
872 DRM_DEBUG( "buffer %d @ %p\n",
873 entry->buf_count, buf->address );
877 byte_count += PAGE_SIZE << page_order;
880 DRM_DEBUG( "byte_count: %d\n", byte_count );
882 temp_buflist = drm_realloc( dma->buflist,
883 dma->buf_count * sizeof(*dma->buflist),
884 (dma->buf_count + entry->buf_count)
885 * sizeof(*dma->buflist),
888 /* Free the entry because it isn't valid */
889 drm_cleanup_buf_error(dev,entry);
890 up( &dev->struct_sem );
891 atomic_dec( &dev->buf_alloc );
894 dma->buflist = temp_buflist;
896 for ( i = 0 ; i < entry->buf_count ; i++ ) {
897 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
900 dma->buf_count += entry->buf_count;
901 dma->byte_count += byte_count;
903 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
904 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
906 up( &dev->struct_sem );
908 request.count = entry->buf_count;
911 if ( copy_to_user( argp, &request, sizeof(request) ) )
914 dma->flags = _DRM_DMA_USE_SG;
916 atomic_dec( &dev->buf_alloc );
921 * Add buffers for DMA transfers (ioctl).
923 * \param inode device inode.
924 * \param filp file pointer.
925 * \param cmd command.
926 * \param arg pointer to a drm_buf_desc_t request.
927 * \return zero on success or a negative number on failure.
929 * According with the memory type specified in drm_buf_desc::flags and the
930 * build options, it dispatches the call either to addbufs_agp(),
931 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
932 * PCI memory respectively.
934 int drm_addbufs( struct inode *inode, struct file *filp,
935 unsigned int cmd, unsigned long arg )
937 drm_buf_desc_t request;
938 drm_file_t *priv = filp->private_data;
939 drm_device_t *dev = priv->head->dev;
941 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
944 if ( copy_from_user( &request, (drm_buf_desc_t __user *)arg,
949 if ( request.flags & _DRM_AGP_BUFFER )
950 return drm_addbufs_agp( inode, filp, cmd, arg );
953 if ( request.flags & _DRM_SG_BUFFER )
954 return drm_addbufs_sg( inode, filp, cmd, arg );
956 return drm_addbufs_pci( inode, filp, cmd, arg );
961 * Get information about the buffer mappings.
963 * This was originally mean for debugging purposes, or by a sophisticated
964 * client library to determine how best to use the available buffers (e.g.,
965 * large buffers can be used for image transfer).
967 * \param inode device inode.
968 * \param filp file pointer.
969 * \param cmd command.
970 * \param arg pointer to a drm_buf_info structure.
971 * \return zero on success or a negative number on failure.
973 * Increments drm_device::buf_use while holding the drm_device::count_lock
974 * lock, preventing of allocating more buffers after this call. Information
975 * about each requested buffer is then copied into user space.
977 int drm_infobufs( struct inode *inode, struct file *filp,
978 unsigned int cmd, unsigned long arg )
980 drm_file_t *priv = filp->private_data;
981 drm_device_t *dev = priv->head->dev;
982 drm_device_dma_t *dma = dev->dma;
983 drm_buf_info_t request;
984 drm_buf_info_t __user *argp = (void __user *)arg;
988 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
991 if ( !dma ) return -EINVAL;
993 spin_lock( &dev->count_lock );
994 if ( atomic_read( &dev->buf_alloc ) ) {
995 spin_unlock( &dev->count_lock );
998 ++dev->buf_use; /* Can't allocate more after this call */
999 spin_unlock( &dev->count_lock );
1001 if ( copy_from_user( &request, argp, sizeof(request) ) )
1004 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
1005 if ( dma->bufs[i].buf_count ) ++count;
1008 DRM_DEBUG( "count = %d\n", count );
1010 if ( request.count >= count ) {
1011 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
1012 if ( dma->bufs[i].buf_count ) {
1013 drm_buf_desc_t __user *to = &request.list[count];
1014 drm_buf_entry_t *from = &dma->bufs[i];
1015 drm_freelist_t *list = &dma->bufs[i].freelist;
1016 if ( copy_to_user( &to->count,
1018 sizeof(from->buf_count) ) ||
1019 copy_to_user( &to->size,
1021 sizeof(from->buf_size) ) ||
1022 copy_to_user( &to->low_mark,
1024 sizeof(list->low_mark) ) ||
1025 copy_to_user( &to->high_mark,
1027 sizeof(list->high_mark) ) )
1030 DRM_DEBUG( "%d %d %d %d %d\n",
1032 dma->bufs[i].buf_count,
1033 dma->bufs[i].buf_size,
1034 dma->bufs[i].freelist.low_mark,
1035 dma->bufs[i].freelist.high_mark );
1040 request.count = count;
1042 if ( copy_to_user( argp, &request, sizeof(request) ) )
1049 * Specifies a low and high water mark for buffer allocation
1051 * \param inode device inode.
1052 * \param filp file pointer.
1053 * \param cmd command.
1054 * \param arg a pointer to a drm_buf_desc structure.
1055 * \return zero on success or a negative number on failure.
1057 * Verifies that the size order is bounded between the admissible orders and
1058 * updates the respective drm_device_dma::bufs entry low and high water mark.
1060 * \note This ioctl is deprecated and mostly never used.
1062 int drm_markbufs( struct inode *inode, struct file *filp,
1063 unsigned int cmd, unsigned long arg )
1065 drm_file_t *priv = filp->private_data;
1066 drm_device_t *dev = priv->head->dev;
1067 drm_device_dma_t *dma = dev->dma;
1068 drm_buf_desc_t request;
1070 drm_buf_entry_t *entry;
1072 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1075 if ( !dma ) return -EINVAL;
1077 if ( copy_from_user( &request,
1078 (drm_buf_desc_t __user *)arg,
1082 DRM_DEBUG( "%d, %d, %d\n",
1083 request.size, request.low_mark, request.high_mark );
1084 order = drm_order( request.size );
1085 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
1086 entry = &dma->bufs[order];
1088 if ( request.low_mark < 0 || request.low_mark > entry->buf_count )
1090 if ( request.high_mark < 0 || request.high_mark > entry->buf_count )
1093 entry->freelist.low_mark = request.low_mark;
1094 entry->freelist.high_mark = request.high_mark;
1100 * Unreserve the buffers in list, previously reserved using drmDMA.
1102 * \param inode device inode.
1103 * \param filp file pointer.
1104 * \param cmd command.
1105 * \param arg pointer to a drm_buf_free structure.
1106 * \return zero on success or a negative number on failure.
1108 * Calls free_buffer() for each used buffer.
1109 * This function is primarily used for debugging.
1111 int drm_freebufs( struct inode *inode, struct file *filp,
1112 unsigned int cmd, unsigned long arg )
1114 drm_file_t *priv = filp->private_data;
1115 drm_device_t *dev = priv->head->dev;
1116 drm_device_dma_t *dma = dev->dma;
1117 drm_buf_free_t request;
1122 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1125 if ( !dma ) return -EINVAL;
1127 if ( copy_from_user( &request,
1128 (drm_buf_free_t __user *)arg,
1132 DRM_DEBUG( "%d\n", request.count );
1133 for ( i = 0 ; i < request.count ; i++ ) {
1134 if ( copy_from_user( &idx,
1138 if ( idx < 0 || idx >= dma->buf_count ) {
1139 DRM_ERROR( "Index %d (of %d max)\n",
1140 idx, dma->buf_count - 1 );
1143 buf = dma->buflist[idx];
1144 if ( buf->filp != filp ) {
1145 DRM_ERROR( "Process %d freeing buffer not owned\n",
1149 drm_free_buffer( dev, buf );
1156 * Maps all of the DMA buffers into client-virtual space (ioctl).
1158 * \param inode device inode.
1159 * \param filp file pointer.
1160 * \param cmd command.
1161 * \param arg pointer to a drm_buf_map structure.
1162 * \return zero on success or a negative number on failure.
1164 * Maps the AGP or SG buffer region with do_mmap(), and copies information
1165 * about each buffer into user space. The PCI buffers are already mapped on the
1166 * addbufs_pci() call.
1168 int drm_mapbufs( struct inode *inode, struct file *filp,
1169 unsigned int cmd, unsigned long arg )
1171 drm_file_t *priv = filp->private_data;
1172 drm_device_t *dev = priv->head->dev;
1173 drm_device_dma_t *dma = dev->dma;
1174 drm_buf_map_t __user *argp = (void __user *)arg;
1177 unsigned long virtual;
1178 unsigned long address;
1179 drm_buf_map_t request;
1182 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1185 if ( !dma ) return -EINVAL;
1187 spin_lock( &dev->count_lock );
1188 if ( atomic_read( &dev->buf_alloc ) ) {
1189 spin_unlock( &dev->count_lock );
1192 dev->buf_use++; /* Can't allocate more after this call */
1193 spin_unlock( &dev->count_lock );
1195 if ( copy_from_user( &request, argp, sizeof(request) ) )
1198 if ( request.count >= dma->buf_count ) {
1199 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
1200 (drm_core_check_feature(dev, DRIVER_SG) && (dma->flags & _DRM_DMA_USE_SG)) ) {
1201 drm_map_t *map = dev->agp_buffer_map;
1208 #if LINUX_VERSION_CODE <= 0x020402
1209 down( ¤t->mm->mmap_sem );
1211 down_write( ¤t->mm->mmap_sem );
1213 virtual = do_mmap( filp, 0, map->size,
1214 PROT_READ | PROT_WRITE,
1216 (unsigned long)map->offset );
1217 #if LINUX_VERSION_CODE <= 0x020402
1218 up( ¤t->mm->mmap_sem );
1220 up_write( ¤t->mm->mmap_sem );
1223 #if LINUX_VERSION_CODE <= 0x020402
1224 down( ¤t->mm->mmap_sem );
1226 down_write( ¤t->mm->mmap_sem );
1228 virtual = do_mmap( filp, 0, dma->byte_count,
1229 PROT_READ | PROT_WRITE,
1231 #if LINUX_VERSION_CODE <= 0x020402
1232 up( ¤t->mm->mmap_sem );
1234 up_write( ¤t->mm->mmap_sem );
1237 if ( virtual > -1024UL ) {
1239 retcode = (signed long)virtual;
1242 request.virtual = (void __user *)virtual;
1244 for ( i = 0 ; i < dma->buf_count ; i++ ) {
1245 if ( copy_to_user( &request.list[i].idx,
1246 &dma->buflist[i]->idx,
1247 sizeof(request.list[0].idx) ) ) {
1251 if ( copy_to_user( &request.list[i].total,
1252 &dma->buflist[i]->total,
1253 sizeof(request.list[0].total) ) ) {
1257 if ( copy_to_user( &request.list[i].used,
1263 address = virtual + dma->buflist[i]->offset; /* *** */
1264 if ( copy_to_user( &request.list[i].address,
1266 sizeof(address) ) ) {
1273 request.count = dma->buf_count;
1274 DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
1276 if ( copy_to_user( argp, &request, sizeof(request) ) )