1 /* drm_bufs.h -- Generic buffer template -*- linux-c -*-
2 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
35 #ifndef __HAVE_PCI_DMA
36 #define __HAVE_PCI_DMA 0
43 #ifndef DRIVER_BUF_PRIV_T
44 #define DRIVER_BUF_PRIV_T u32
46 #ifndef DRIVER_AGP_BUFFERS_MAP
47 #if __HAVE_AGP && __HAVE_DMA
48 #error "You must define DRIVER_AGP_BUFFERS_MAP()"
50 #define DRIVER_AGP_BUFFERS_MAP( dev ) NULL
55 * Compute order. Can be made faster.
57 int DRM(order)( unsigned long size )
62 for ( order = 0, tmp = size ; tmp >>= 1 ; ++order );
64 if ( size & ~(1 << order) )
70 int DRM(addmap)( DRM_IOCTL_ARGS )
75 drm_map_list_entry_t *list;
77 if (!(dev->flags & (FREAD|FWRITE)))
78 return DRM_ERR(EACCES); /* Require read/write */
80 DRM_COPY_FROM_USER_IOCTL( request, (drm_map_t *)data, sizeof(drm_map_t) );
82 map = (drm_local_map_t *) DRM(alloc)( sizeof(*map), DRM_MEM_MAPS );
84 return DRM_ERR(ENOMEM);
86 map->offset = request.offset;
87 map->size = request.size;
88 map->type = request.type;
89 map->flags = request.flags;
93 /* Only allow shared memory to be removable since we only keep enough
94 * book keeping information about shared memory to allow for removal
95 * when processes fork.
97 if ( (map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM ) {
98 DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
99 return DRM_ERR(EINVAL);
101 DRM_DEBUG( "offset = 0x%08lx, size = 0x%08lx, type = %d\n",
102 map->offset, map->size, map->type );
103 if ( (map->offset & PAGE_MASK) || (map->size & PAGE_MASK) ) {
104 DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
105 return DRM_ERR(EINVAL);
108 switch ( map->type ) {
110 case _DRM_FRAME_BUFFER:
111 if ( map->offset + map->size < map->offset ) {
112 DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
113 return DRM_ERR(EINVAL);
115 #if __REALLY_HAVE_MTRR
116 if ( map->type == _DRM_FRAME_BUFFER ||
117 (map->flags & _DRM_WRITE_COMBINING) ) {
119 int retcode = 0, act;
120 struct mem_range_desc mrdesc;
121 mrdesc.mr_base = map->offset;
122 mrdesc.mr_len = map->size;
123 mrdesc.mr_flags = MDF_WRITECOMBINE;
124 act = MEMRANGE_SET_UPDATE;
125 bcopy(DRIVER_NAME, &mrdesc.mr_owner, strlen(DRIVER_NAME));
126 retcode = mem_range_attr_set(&mrdesc, &act);
128 #elif defined __NetBSD__
131 mtrrmap.base = map->offset;
132 mtrrmap.len = map->size;
133 mtrrmap.type = MTRR_TYPE_WC;
134 mtrrmap.flags = MTRR_VALID;
135 map->mtrr = mtrr_set( &mtrrmap, &one, p, MTRR_GETSET_KERNEL );
138 #endif /* __REALLY_HAVE_MTRR */
139 DRM_IOREMAP(map, dev);
143 map->handle = (void *)DRM(alloc)(map->size, DRM_MEM_SAREA);
144 DRM_DEBUG( "%lu %d %p\n",
145 map->size, DRM(order)( map->size ), map->handle );
146 if ( !map->handle ) {
147 DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
148 return DRM_ERR(ENOMEM);
150 map->offset = (unsigned long)map->handle;
151 if ( map->flags & _DRM_CONTAINS_LOCK ) {
152 dev->lock.hw_lock = map->handle; /* Pointer to lock */
155 #if __REALLY_HAVE_AGP
157 map->offset += dev->agp->base;
158 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
161 case _DRM_SCATTER_GATHER:
163 DRM(free)(map, sizeof(*map), DRM_MEM_MAPS);
164 return DRM_ERR(EINVAL);
166 map->offset = map->offset + dev->sg->handle;
170 DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
171 return DRM_ERR(EINVAL);
174 list = DRM(calloc)(1, sizeof(*list), DRM_MEM_MAPS);
176 DRM(free)(map, sizeof(*map), DRM_MEM_MAPS);
177 return DRM_ERR(EINVAL);
182 TAILQ_INSERT_TAIL(dev->maplist, list, link);
185 request.offset = map->offset;
186 request.size = map->size;
187 request.type = map->type;
188 request.flags = map->flags;
189 request.mtrr = map->mtrr;
190 request.handle = map->handle;
192 if ( request.type != _DRM_SHM ) {
193 request.handle = (void *)request.offset;
196 DRM_COPY_TO_USER_IOCTL( (drm_map_t *)data, request, sizeof(drm_map_t) );
202 /* Remove a map private from list and deallocate resources if the mapping
206 int DRM(rmmap)( DRM_IOCTL_ARGS )
209 drm_map_list_entry_t *list;
210 drm_local_map_t *map;
213 DRM_COPY_FROM_USER_IOCTL( request, (drm_map_t *)data, sizeof(request) );
216 TAILQ_FOREACH(list, dev->maplist, link) {
218 if (map->handle == request.handle &&
219 map->flags & _DRM_REMOVABLE)
223 /* No match found. */
226 return DRM_ERR(EINVAL);
228 TAILQ_REMOVE(dev->maplist, list, link);
231 DRM(free)(list, sizeof(*list), DRM_MEM_MAPS);
235 case _DRM_FRAME_BUFFER:
236 #if __REALLY_HAVE_MTRR
237 if (map->mtrr >= 0) {
241 struct mem_range_desc mrdesc;
242 mrdesc.mr_base = map->offset;
243 mrdesc.mr_len = map->size;
244 mrdesc.mr_flags = MDF_WRITECOMBINE;
245 act = MEMRANGE_SET_REMOVE;
246 bcopy(DRIVER_NAME, &mrdesc.mr_owner,
247 strlen(DRIVER_NAME));
248 retcode = mem_range_attr_set(&mrdesc, &act);
249 #elif defined __NetBSD__
252 mtrrmap.base = map->offset;
253 mtrrmap.len = map->size;
256 mtrrmap.owner = p->p_pid;
257 retcode = mtrr_set(&mtrrmap, &one, p,
259 DRM_DEBUG("mtrr_del = %d\n", retcode);
263 DRM(ioremapfree)(map);
266 DRM(free)(map->handle, map->size, DRM_MEM_SAREA);
269 case _DRM_SCATTER_GATHER:
272 DRM(free)(map, sizeof(*map), DRM_MEM_MAPS);
279 static void DRM(cleanup_buf_error)(drm_device_t *dev, drm_buf_entry_t *entry)
284 if (entry->seg_count) {
285 for (i = 0; i < entry->seg_count; i++) {
286 if (entry->seglist[i] != NULL)
287 DRM(pci_free)(dev, entry->buf_size,
288 (void *)entry->seglist[i],
289 entry->seglist_bus[i]);
291 DRM(free)(entry->seglist,
293 sizeof(*entry->seglist),
295 DRM(free)(entry->seglist_bus, entry->seg_count *
296 sizeof(*entry->seglist_bus), DRM_MEM_SEGS);
298 entry->seg_count = 0;
300 #endif /* __HAVE_PCI_DMA */
302 if (entry->buf_count) {
303 for (i = 0; i < entry->buf_count; i++) {
304 DRM(free)(entry->buflist[i].dev_private,
305 entry->buflist[i].dev_priv_size, DRM_MEM_BUFS);
307 DRM(free)(entry->buflist,
309 sizeof(*entry->buflist),
312 entry->buf_count = 0;
316 #if __REALLY_HAVE_AGP
317 static int DRM(addbufs_agp)(drm_device_t *dev, drm_buf_desc_t *request)
319 drm_device_dma_t *dma = dev->dma;
320 drm_buf_entry_t *entry;
322 unsigned long offset;
323 unsigned long agp_offset;
332 drm_buf_t **temp_buflist;
334 count = request->count;
335 order = DRM(order)(request->size);
338 alignment = (request->flags & _DRM_PAGE_ALIGN)
339 ? round_page(size) : size;
340 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
341 total = PAGE_SIZE << page_order;
344 agp_offset = dev->agp->base + request->agp_start;
346 DRM_DEBUG( "count: %d\n", count );
347 DRM_DEBUG( "order: %d\n", order );
348 DRM_DEBUG( "size: %d\n", size );
349 DRM_DEBUG( "agp_offset: 0x%lx\n", agp_offset );
350 DRM_DEBUG( "alignment: %d\n", alignment );
351 DRM_DEBUG( "page_order: %d\n", page_order );
352 DRM_DEBUG( "total: %d\n", total );
354 entry = &dma->bufs[order];
356 entry->buflist = DRM(alloc)( count * sizeof(*entry->buflist),
358 if ( !entry->buflist ) {
359 return DRM_ERR(ENOMEM);
361 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
363 entry->buf_size = size;
364 entry->page_order = page_order;
368 while ( entry->buf_count < count ) {
369 buf = &entry->buflist[entry->buf_count];
370 buf->idx = dma->buf_count + entry->buf_count;
371 buf->total = alignment;
375 buf->offset = (dma->byte_count + offset);
376 buf->bus_address = agp_offset + offset;
377 buf->address = (void *)(agp_offset + offset);
382 buf->dev_priv_size = sizeof(DRIVER_BUF_PRIV_T);
383 buf->dev_private = DRM(calloc)(1, buf->dev_priv_size,
385 if (buf->dev_private == NULL) {
386 /* Set count correctly so we free the proper amount. */
387 entry->buf_count = count;
388 DRM(cleanup_buf_error)(dev, entry);
389 return DRM_ERR(ENOMEM);
394 byte_count += PAGE_SIZE << page_order;
397 DRM_DEBUG( "byte_count: %d\n", byte_count );
399 temp_buflist = DRM(realloc)( dma->buflist,
400 dma->buf_count * sizeof(*dma->buflist),
401 (dma->buf_count + entry->buf_count)
402 * sizeof(*dma->buflist),
404 if (temp_buflist == NULL) {
405 /* Free the entry because it isn't valid */
406 DRM(cleanup_buf_error)(dev, entry);
407 return DRM_ERR(ENOMEM);
409 dma->buflist = temp_buflist;
411 for ( i = 0 ; i < entry->buf_count ; i++ ) {
412 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
415 dma->buf_count += entry->buf_count;
416 dma->byte_count += byte_count;
418 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
419 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
421 request->count = entry->buf_count;
422 request->size = size;
424 dma->flags = _DRM_DMA_USE_AGP;
428 #endif /* __REALLY_HAVE_AGP */
431 static int DRM(addbufs_pci)(drm_device_t *dev, drm_buf_desc_t *request)
433 drm_device_dma_t *dma = dev->dma;
439 drm_buf_entry_t *entry;
443 unsigned long offset;
447 unsigned long *temp_pagelist;
448 drm_buf_t **temp_buflist;
451 count = request->count;
452 order = DRM(order)(request->size);
455 DRM_DEBUG( "count=%d, size=%d (%d), order=%d\n",
456 request->count, request->size, size, order );
458 alignment = (request->flags & _DRM_PAGE_ALIGN)
459 ? round_page(size) : size;
460 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
461 total = PAGE_SIZE << page_order;
463 entry = &dma->bufs[order];
465 entry->buflist = DRM(alloc)(count * sizeof(*entry->buflist),
467 entry->seglist = DRM(alloc)(count * sizeof(*entry->seglist),
469 entry->seglist_bus = DRM(alloc)(count * sizeof(*entry->seglist_bus),
472 /* Keep the original pagelist until we know all the allocations
475 temp_pagelist = DRM(alloc)((dma->page_count + (count << page_order)) *
476 sizeof(*dma->pagelist), DRM_MEM_PAGES);
478 if (entry->buflist == NULL || entry->seglist == NULL ||
479 temp_pagelist == NULL) {
480 DRM(free)(entry->buflist, count * sizeof(*entry->buflist),
482 DRM(free)(entry->seglist, count * sizeof(*entry->seglist),
484 DRM(free)(entry->seglist_bus, count *
485 sizeof(*entry->seglist_bus), DRM_MEM_SEGS);
486 return DRM_ERR(ENOMEM);
489 bzero(entry->buflist, count * sizeof(*entry->buflist));
490 bzero(entry->seglist, count * sizeof(*entry->seglist));
492 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
493 sizeof(*dma->pagelist));
495 DRM_DEBUG( "pagelist: %d entries\n",
496 dma->page_count + (count << page_order) );
498 entry->buf_size = size;
499 entry->page_order = page_order;
503 while ( entry->buf_count < count ) {
504 vaddr = (vm_offset_t) DRM(pci_alloc)(dev, size, alignment,
505 0xfffffffful, &bus_addr);
507 /* Set count correctly so we free the proper amount. */
508 entry->buf_count = count;
509 entry->seg_count = count;
510 DRM(cleanup_buf_error)(dev, entry);
511 DRM(free)(temp_pagelist, (dma->page_count +
512 (count << page_order)) * sizeof(*dma->pagelist),
514 return DRM_ERR(ENOMEM);
517 entry->seglist_bus[entry->seg_count] = bus_addr;
518 entry->seglist[entry->seg_count++] = vaddr;
519 for ( i = 0 ; i < (1 << page_order) ; i++ ) {
520 DRM_DEBUG( "page %d @ 0x%08lx\n",
521 dma->page_count + page_count,
522 (long)vaddr + PAGE_SIZE * i );
523 temp_pagelist[dma->page_count + page_count++] =
524 vaddr + PAGE_SIZE * i;
527 offset + size <= total && entry->buf_count < count ;
528 offset += alignment, ++entry->buf_count ) {
529 buf = &entry->buflist[entry->buf_count];
530 buf->idx = dma->buf_count + entry->buf_count;
531 buf->total = alignment;
534 buf->offset = (dma->byte_count + byte_count + offset);
535 buf->address = (void *)(vaddr + offset);
536 buf->bus_address = bus_addr + offset;
541 buf->dev_priv_size = sizeof(DRIVER_BUF_PRIV_T);
542 buf->dev_private = DRM(alloc)(sizeof(DRIVER_BUF_PRIV_T),
544 if (buf->dev_private == NULL) {
545 /* Set count correctly so we free the proper amount. */
546 entry->buf_count = count;
547 entry->seg_count = count;
548 DRM(cleanup_buf_error)(dev, entry);
549 DRM(free)(temp_pagelist, (dma->page_count +
550 (count << page_order)) *
551 sizeof(*dma->pagelist), DRM_MEM_PAGES );
552 return DRM_ERR(ENOMEM);
554 bzero(buf->dev_private, buf->dev_priv_size);
556 DRM_DEBUG( "buffer %d @ %p\n",
557 entry->buf_count, buf->address );
559 byte_count += PAGE_SIZE << page_order;
562 temp_buflist = DRM(realloc)( dma->buflist,
563 dma->buf_count * sizeof(*dma->buflist),
564 (dma->buf_count + entry->buf_count)
565 * sizeof(*dma->buflist),
567 if (temp_buflist == NULL) {
568 /* Free the entry because it isn't valid */
569 DRM(cleanup_buf_error)(dev, entry);
570 DRM(free)(temp_pagelist, (dma->page_count +
571 (count << page_order)) * sizeof(*dma->pagelist),
573 return DRM_ERR(ENOMEM);
575 dma->buflist = temp_buflist;
577 for ( i = 0 ; i < entry->buf_count ; i++ ) {
578 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
581 /* No allocations failed, so now we can replace the orginal pagelist
584 DRM(free)(dma->pagelist, dma->page_count * sizeof(*dma->pagelist),
586 dma->pagelist = temp_pagelist;
588 dma->buf_count += entry->buf_count;
589 dma->seg_count += entry->seg_count;
590 dma->page_count += entry->seg_count << page_order;
591 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
593 request->count = entry->buf_count;
594 request->size = size;
599 #endif /* __HAVE_PCI_DMA */
602 static int DRM(addbufs_sg)(drm_device_t *dev, drm_buf_desc_t *request)
604 drm_device_dma_t *dma = dev->dma;
605 drm_buf_entry_t *entry;
607 unsigned long offset;
608 unsigned long agp_offset;
617 drm_buf_t **temp_buflist;
619 count = request->count;
620 order = DRM(order)(request->size);
623 alignment = (request->flags & _DRM_PAGE_ALIGN)
624 ? round_page(size) : size;
625 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
626 total = PAGE_SIZE << page_order;
629 agp_offset = request->agp_start;
631 DRM_DEBUG( "count: %d\n", count );
632 DRM_DEBUG( "order: %d\n", order );
633 DRM_DEBUG( "size: %d\n", size );
634 DRM_DEBUG( "agp_offset: %ld\n", agp_offset );
635 DRM_DEBUG( "alignment: %d\n", alignment );
636 DRM_DEBUG( "page_order: %d\n", page_order );
637 DRM_DEBUG( "total: %d\n", total );
639 entry = &dma->bufs[order];
641 entry->buflist = DRM(calloc)(1, count * sizeof(*entry->buflist),
643 if (entry->buflist == NULL)
644 return DRM_ERR(ENOMEM);
646 entry->buf_size = size;
647 entry->page_order = page_order;
651 while ( entry->buf_count < count ) {
652 buf = &entry->buflist[entry->buf_count];
653 buf->idx = dma->buf_count + entry->buf_count;
654 buf->total = alignment;
658 buf->offset = (dma->byte_count + offset);
659 buf->bus_address = agp_offset + offset;
660 buf->address = (void *)(agp_offset + offset + dev->sg->handle);
665 buf->dev_priv_size = sizeof(DRIVER_BUF_PRIV_T);
666 buf->dev_private = DRM(calloc)(1, buf->dev_priv_size,
668 if (buf->dev_private == NULL) {
669 /* Set count correctly so we free the proper amount. */
670 entry->buf_count = count;
671 DRM(cleanup_buf_error)(dev, entry);
672 return DRM_ERR(ENOMEM);
675 DRM_DEBUG( "buffer %d @ %p\n",
676 entry->buf_count, buf->address );
680 byte_count += PAGE_SIZE << page_order;
683 DRM_DEBUG( "byte_count: %d\n", byte_count );
685 temp_buflist = DRM(realloc)( dma->buflist,
686 dma->buf_count * sizeof(*dma->buflist),
687 (dma->buf_count + entry->buf_count)
688 * sizeof(*dma->buflist),
690 if (temp_buflist == NULL) {
691 /* Free the entry because it isn't valid */
692 DRM(cleanup_buf_error)(dev, entry);
693 return DRM_ERR(ENOMEM);
695 dma->buflist = temp_buflist;
697 for ( i = 0 ; i < entry->buf_count ; i++ ) {
698 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
701 dma->buf_count += entry->buf_count;
702 dma->byte_count += byte_count;
704 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
705 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
707 request->count = entry->buf_count;
708 request->size = size;
710 dma->flags = _DRM_DMA_USE_SG;
714 #endif /* __REALLY_HAVE_SG */
716 int DRM(addbufs)( DRM_IOCTL_ARGS )
719 drm_buf_desc_t request;
723 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_desc_t *)data, sizeof(request) );
725 if (request.count < 0 || request.count > 4096)
726 return DRM_ERR(EINVAL);
728 order = DRM(order)(request.size);
729 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
730 return DRM_ERR(EINVAL);
732 DRM_SPINLOCK(&dev->dma_lock);
733 /* No more allocations after first buffer-using ioctl. */
734 if (dev->buf_use != 0) {
735 DRM_SPINUNLOCK(&dev->dma_lock);
736 return DRM_ERR(EBUSY);
738 /* No more than one allocation per order */
739 if (dev->dma->bufs[order].buf_count != 0) {
740 DRM_SPINUNLOCK(&dev->dma_lock);
741 return DRM_ERR(ENOMEM);
744 #if __REALLY_HAVE_AGP
745 if ( request.flags & _DRM_AGP_BUFFER )
746 err = DRM(addbufs_agp)(dev, &request);
750 if ( request.flags & _DRM_SG_BUFFER )
751 err = DRM(addbufs_sg)(dev, &request);
755 err = DRM(addbufs_pci)(dev, &request);
757 err = DRM_ERR(EINVAL);
759 DRM_SPINUNLOCK(&dev->dma_lock);
761 DRM_COPY_TO_USER_IOCTL((drm_buf_desc_t *)data, request, sizeof(request));
766 int DRM(infobufs)( DRM_IOCTL_ARGS )
769 drm_device_dma_t *dma = dev->dma;
770 drm_buf_info_t request;
775 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_info_t *)data, sizeof(request) );
777 DRM_SPINLOCK(&dev->dma_lock);
778 ++dev->buf_use; /* Can't allocate more after this call */
779 DRM_SPINUNLOCK(&dev->dma_lock);
781 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
782 if ( dma->bufs[i].buf_count ) ++count;
785 DRM_DEBUG( "count = %d\n", count );
787 if ( request.count >= count ) {
788 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
789 if ( dma->bufs[i].buf_count ) {
792 from.count = dma->bufs[i].buf_count;
793 from.size = dma->bufs[i].buf_size;
794 from.low_mark = dma->bufs[i].freelist.low_mark;
795 from.high_mark = dma->bufs[i].freelist.high_mark;
797 if (DRM_COPY_TO_USER(&request.list[count], &from,
798 sizeof(drm_buf_desc_t)) != 0) {
799 retcode = DRM_ERR(EFAULT);
803 DRM_DEBUG( "%d %d %d %d %d\n",
805 dma->bufs[i].buf_count,
806 dma->bufs[i].buf_size,
807 dma->bufs[i].freelist.low_mark,
808 dma->bufs[i].freelist.high_mark );
813 request.count = count;
815 DRM_COPY_TO_USER_IOCTL( (drm_buf_info_t *)data, request, sizeof(request) );
820 int DRM(markbufs)( DRM_IOCTL_ARGS )
823 drm_device_dma_t *dma = dev->dma;
824 drm_buf_desc_t request;
827 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_desc_t *)data, sizeof(request) );
829 DRM_DEBUG( "%d, %d, %d\n",
830 request.size, request.low_mark, request.high_mark );
833 order = DRM(order)(request.size);
834 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
835 request.low_mark < 0 || request.high_mark < 0) {
836 return DRM_ERR(EINVAL);
839 DRM_SPINLOCK(&dev->dma_lock);
840 if (request.low_mark > dma->bufs[order].buf_count ||
841 request.high_mark > dma->bufs[order].buf_count) {
842 return DRM_ERR(EINVAL);
845 dma->bufs[order].freelist.low_mark = request.low_mark;
846 dma->bufs[order].freelist.high_mark = request.high_mark;
847 DRM_SPINUNLOCK(&dev->dma_lock);
852 int DRM(freebufs)( DRM_IOCTL_ARGS )
855 drm_device_dma_t *dma = dev->dma;
856 drm_buf_free_t request;
862 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_free_t *)data, sizeof(request) );
864 DRM_DEBUG( "%d\n", request.count );
866 DRM_SPINLOCK(&dev->dma_lock);
867 for ( i = 0 ; i < request.count ; i++ ) {
868 if (DRM_COPY_FROM_USER(&idx, &request.list[i], sizeof(idx))) {
869 retcode = DRM_ERR(EFAULT);
872 if ( idx < 0 || idx >= dma->buf_count ) {
873 DRM_ERROR( "Index %d (of %d max)\n",
874 idx, dma->buf_count - 1 );
875 retcode = DRM_ERR(EINVAL);
878 buf = dma->buflist[idx];
879 if ( buf->filp != filp ) {
880 DRM_ERROR("Process %d freeing buffer not owned\n",
882 retcode = DRM_ERR(EINVAL);
885 DRM(free_buffer)( dev, buf );
887 DRM_SPINUNLOCK(&dev->dma_lock);
892 int DRM(mapbufs)( DRM_IOCTL_ARGS )
895 drm_device_dma_t *dma = dev->dma;
904 #endif /* __FreeBSD__ */
909 #endif /* __NetBSD__ */
911 drm_buf_map_t request;
914 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_map_t *)data, sizeof(request) );
917 if (!vfinddev(kdev, VCHR, &vn))
918 return 0; /* FIXME: Shouldn't this be EINVAL or something? */
919 #endif /* __NetBSD__ */
921 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
922 vms = p->td_proc->p_vmspace;
927 DRM_SPINLOCK(&dev->dma_lock);
928 dev->buf_use++; /* Can't allocate more after this call */
929 DRM_SPINUNLOCK(&dev->dma_lock);
931 if (request.count < dma->buf_count)
934 if ((__HAVE_AGP && (dma->flags & _DRM_DMA_USE_AGP)) ||
935 (__HAVE_SG && (dma->flags & _DRM_DMA_USE_SG))) {
936 drm_local_map_t *map = DRIVER_AGP_BUFFERS_MAP(dev);
942 size = round_page(map->size);
945 size = round_page(dma->byte_count),
950 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
951 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
952 VM_PROT_ALL, MAP_SHARED, SLIST_FIRST(&kdev->si_hlist), foff );
953 #elif defined(__NetBSD__)
954 vaddr = round_page((vaddr_t)vms->vm_daddr + MAXDSIZ);
955 retcode = uvm_mmap(&vms->vm_map, &vaddr, size,
956 UVM_PROT_READ | UVM_PROT_WRITE, UVM_PROT_ALL, MAP_SHARED,
957 &vn->v_uobj, foff, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
958 #endif /* __NetBSD__ */
962 request.virtual = (void *)vaddr;
964 for ( i = 0 ; i < dma->buf_count ; i++ ) {
965 if (DRM_COPY_TO_USER(&request.list[i].idx,
966 &dma->buflist[i]->idx, sizeof(request.list[0].idx))) {
970 if (DRM_COPY_TO_USER(&request.list[i].total,
971 &dma->buflist[i]->total, sizeof(request.list[0].total))) {
975 if (DRM_COPY_TO_USER(&request.list[i].used, &zero,
980 address = vaddr + dma->buflist[i]->offset; /* *** */
981 if (DRM_COPY_TO_USER(&request.list[i].address, &address,
989 request.count = dma->buf_count;
991 DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
993 DRM_COPY_TO_USER_IOCTL((drm_buf_map_t *)data, request, sizeof(request));
995 return DRM_ERR(retcode);
998 #endif /* __HAVE_DMA */