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>
36 * Compute order. Can be made faster.
38 int drm_order(unsigned long size)
43 for ( order = 0, tmp = size ; tmp >>= 1 ; ++order );
45 if ( size & ~(1 << order) )
51 unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource)
56 resource = resource * 4 + 0x10;
58 bsr = bus_alloc_resource_any(dev->device, SYS_RES_MEMORY, &resource,
59 RF_ACTIVE | RF_SHAREABLE);
61 DRM_ERROR("Couldn't find resource 0x%x\n", resource);
65 offset = rman_get_start(bsr);
67 bus_release_resource(dev->device, SYS_RES_MEMORY, resource, bsr);
72 unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource)
77 resource = resource * 4 + 0x10;
79 bsr = bus_alloc_resource_any(dev->device, SYS_RES_MEMORY, &resource,
80 RF_ACTIVE | RF_SHAREABLE);
82 DRM_ERROR("Couldn't find resource 0x%x\n", resource);
86 len = rman_get_size(bsr);
88 bus_release_resource(dev->device, SYS_RES_MEMORY, resource, bsr);
93 int drm_initmap(drm_device_t *dev, unsigned long start, unsigned long len,
94 unsigned int resource, int type, int flags)
99 if (type != _DRM_REGISTERS && type != _DRM_FRAME_BUFFER)
104 map = malloc(sizeof(*map), M_DRM, M_ZERO | M_NOWAIT);
108 map->rid = resource * 4 + 0x10;
109 bsr = bus_alloc_resource_any(dev->device, SYS_RES_MEMORY, &map->rid,
110 RF_ACTIVE | RF_SHAREABLE);
112 DRM_ERROR("Couldn't allocate %s resource\n",
113 ((type == _DRM_REGISTERS) ? "mmio" : "framebuffer"));
118 map->kernel_owned = 1;
122 map->bst = rman_get_bustag(bsr);
123 map->bsh = rman_get_bushandle(bsr);
127 if (type == _DRM_REGISTERS)
128 map->handle = rman_get_virtual(bsr);
130 DRM_DEBUG("initmap %d,0x%x@0x%lx/0x%lx\n", map->type, map->flags,
131 map->offset, map->size);
133 if (map->flags & _DRM_WRITE_COMBINING) {
136 err = drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC);
142 TAILQ_INSERT_TAIL(&dev->maplist, map, link);
148 int drm_addmap(DRM_IOCTL_ARGS)
152 drm_local_map_t *map;
155 if (!(dev->flags & (FREAD|FWRITE)))
156 return DRM_ERR(EACCES); /* Require read/write */
158 DRM_COPY_FROM_USER_IOCTL( request, (drm_map_t *)data, sizeof(drm_map_t) );
160 /* Only allow shared memory to be removable since we only keep enough
161 * book keeping information about shared memory to allow for removal
162 * when processes fork.
164 if ((request.flags & _DRM_REMOVABLE) && request.type != _DRM_SHM)
166 if ((request.offset & PAGE_MASK) || (request.size & PAGE_MASK))
168 if (request.offset + request.size < request.offset)
171 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
172 request.offset, request.size, request.type);
174 /* Check if this is just another version of a kernel-allocated map, and
175 * just hand that back if so.
177 if (request.type == _DRM_REGISTERS || request.type == _DRM_FRAME_BUFFER)
180 TAILQ_FOREACH(map, &dev->maplist, link) {
181 if (map->kernel_owned && map->type == request.type &&
182 map->offset == request.offset) {
183 /* XXX: this size setting is questionable. */
184 map->size = request.size;
185 DRM_DEBUG("Found kernel map %d\n", request.type);
192 /* Allocate a new map structure, fill it in, and do any type-specific
193 * initialization necessary.
195 map = malloc(sizeof(*map), M_DRM, M_ZERO | M_NOWAIT);
197 return DRM_ERR(ENOMEM);
199 map->offset = request.offset;
200 map->size = request.size;
201 map->type = request.type;
202 map->flags = request.flags;
204 switch ( map->type ) {
206 drm_ioremap(dev, map);
207 if (!(map->flags & _DRM_WRITE_COMBINING))
210 case _DRM_FRAME_BUFFER:
211 if (drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC) == 0)
215 map->handle = malloc(map->size, M_DRM, M_NOWAIT);
216 DRM_DEBUG( "%lu %d %p\n",
217 map->size, drm_order(map->size), map->handle );
218 if ( !map->handle ) {
220 return DRM_ERR(ENOMEM);
222 map->offset = (unsigned long)map->handle;
223 if ( map->flags & _DRM_CONTAINS_LOCK ) {
224 /* Prevent a 2nd X Server from creating a 2nd lock */
226 if (dev->lock.hw_lock != NULL) {
228 free(map->handle, M_DRM);
230 return DRM_ERR(EBUSY);
232 dev->lock.hw_lock = map->handle; /* Pointer to lock */
237 map->offset += dev->agp->base;
238 map->mtrr = dev->agp->mtrr; /* for getmap */
240 case _DRM_SCATTER_GATHER:
243 return DRM_ERR(EINVAL);
245 map->offset = map->offset + dev->sg->handle;
247 case _DRM_CONSISTENT:
248 map->handle = drm_pci_alloc(dev, map->size, map->size,
249 0xfffffffful, &bus_addr);
250 if (map->handle == NULL) {
254 map->offset = (unsigned long)bus_addr;
258 return DRM_ERR(EINVAL);
262 TAILQ_INSERT_TAIL(&dev->maplist, map, link);
265 /* Jumped to, with lock held, when a kernel map is found. */
266 request.offset = map->offset;
267 request.size = map->size;
268 request.type = map->type;
269 request.flags = map->flags;
270 request.mtrr = map->mtrr;
271 request.handle = map->handle;
274 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", request.type, request.offset, request.size);
276 if ( request.type != _DRM_SHM ) {
277 request.handle = (void *)request.offset;
280 DRM_COPY_TO_USER_IOCTL( (drm_map_t *)data, request, sizeof(drm_map_t) );
285 void drm_remove_map(drm_device_t *dev, drm_local_map_t *map)
287 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
289 TAILQ_REMOVE(&dev->maplist, map, link);
293 if (map->bsr == NULL)
294 drm_ioremapfree(map);
296 case _DRM_FRAME_BUFFER:
298 int __unused retcode;
300 retcode = drm_mtrr_del(map->offset, map->size,
302 DRM_DEBUG("mtrr_del = %d\n", retcode);
306 free(map->handle, M_DRM);
309 case _DRM_SCATTER_GATHER:
311 case _DRM_CONSISTENT:
312 drm_pci_free(dev, map->size, map->handle, map->offset);
316 if (map->bsr != NULL) {
317 bus_release_resource(dev->device, SYS_RES_MEMORY, map->rid,
324 /* Remove a map private from list and deallocate resources if the mapping
328 int drm_rmmap(DRM_IOCTL_ARGS)
331 drm_local_map_t *map;
334 DRM_COPY_FROM_USER_IOCTL( request, (drm_map_t *)data, sizeof(request) );
337 TAILQ_FOREACH(map, &dev->maplist, link) {
338 if (map->handle == request.handle &&
339 map->flags & _DRM_REMOVABLE)
343 /* No match found. */
346 return DRM_ERR(EINVAL);
349 drm_remove_map(dev, map);
357 static void drm_cleanup_buf_error(drm_device_t *dev, drm_buf_entry_t *entry)
361 if (entry->seg_count) {
362 for (i = 0; i < entry->seg_count; i++) {
363 drm_pci_free(dev, entry->buf_size,
364 (void *)entry->seglist[i],
365 entry->seglist_bus[i]);
367 free(entry->seglist, M_DRM);
368 free(entry->seglist_bus, M_DRM);
370 entry->seg_count = 0;
373 if (entry->buf_count) {
374 for (i = 0; i < entry->buf_count; i++) {
375 free(entry->buflist[i].dev_private, M_DRM);
377 free(entry->buflist, M_DRM);
379 entry->buf_count = 0;
383 static int drm_addbufs_agp(drm_device_t *dev, drm_buf_desc_t *request)
385 drm_device_dma_t *dma = dev->dma;
386 drm_buf_entry_t *entry;
388 unsigned long offset;
389 unsigned long agp_offset;
398 drm_buf_t **temp_buflist;
400 count = request->count;
401 order = drm_order(request->size);
404 alignment = (request->flags & _DRM_PAGE_ALIGN)
405 ? round_page(size) : size;
406 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
407 total = PAGE_SIZE << page_order;
410 agp_offset = dev->agp->base + request->agp_start;
412 DRM_DEBUG( "count: %d\n", count );
413 DRM_DEBUG( "order: %d\n", order );
414 DRM_DEBUG( "size: %d\n", size );
415 DRM_DEBUG( "agp_offset: 0x%lx\n", agp_offset );
416 DRM_DEBUG( "alignment: %d\n", alignment );
417 DRM_DEBUG( "page_order: %d\n", page_order );
418 DRM_DEBUG( "total: %d\n", total );
420 entry = &dma->bufs[order];
422 entry->buflist = malloc(count * sizeof(*entry->buflist), M_DRM,
424 if ( !entry->buflist ) {
425 return DRM_ERR(ENOMEM);
428 entry->buf_size = size;
429 entry->page_order = page_order;
433 while ( entry->buf_count < count ) {
434 buf = &entry->buflist[entry->buf_count];
435 buf->idx = dma->buf_count + entry->buf_count;
436 buf->total = alignment;
440 buf->offset = (dma->byte_count + offset);
441 buf->bus_address = agp_offset + offset;
442 buf->address = (void *)(agp_offset + offset);
447 buf->dev_priv_size = dev->dev_priv_size;
448 buf->dev_private = malloc(buf->dev_priv_size, M_DRM,
450 if (buf->dev_private == NULL) {
451 /* Set count correctly so we free the proper amount. */
452 entry->buf_count = count;
453 drm_cleanup_buf_error(dev, entry);
454 return DRM_ERR(ENOMEM);
459 byte_count += PAGE_SIZE << page_order;
462 DRM_DEBUG( "byte_count: %d\n", byte_count );
464 temp_buflist = realloc(dma->buflist,
465 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), M_DRM,
467 if (temp_buflist == NULL) {
468 /* Free the entry because it isn't valid */
469 drm_cleanup_buf_error(dev, entry);
470 return DRM_ERR(ENOMEM);
472 dma->buflist = temp_buflist;
474 for ( i = 0 ; i < entry->buf_count ; i++ ) {
475 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
478 dma->buf_count += entry->buf_count;
479 dma->byte_count += byte_count;
481 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
482 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
484 request->count = entry->buf_count;
485 request->size = size;
487 dma->flags = _DRM_DMA_USE_AGP;
492 static int drm_addbufs_pci(drm_device_t *dev, drm_buf_desc_t *request)
494 drm_device_dma_t *dma = dev->dma;
500 drm_buf_entry_t *entry;
504 unsigned long offset;
508 unsigned long *temp_pagelist;
509 drm_buf_t **temp_buflist;
512 count = request->count;
513 order = drm_order(request->size);
516 DRM_DEBUG( "count=%d, size=%d (%d), order=%d\n",
517 request->count, request->size, size, order );
519 alignment = (request->flags & _DRM_PAGE_ALIGN)
520 ? round_page(size) : size;
521 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
522 total = PAGE_SIZE << page_order;
524 entry = &dma->bufs[order];
526 entry->buflist = malloc(count * sizeof(*entry->buflist), M_DRM,
528 entry->seglist = malloc(count * sizeof(*entry->seglist), M_DRM,
530 entry->seglist_bus = malloc(count * sizeof(*entry->seglist_bus), M_DRM,
533 /* Keep the original pagelist until we know all the allocations
536 temp_pagelist = malloc((dma->page_count + (count << page_order)) *
537 sizeof(*dma->pagelist), M_DRM, M_NOWAIT);
539 if (entry->buflist == NULL || entry->seglist == NULL ||
540 entry->seglist_bus == NULL || temp_pagelist == NULL) {
541 free(entry->buflist, M_DRM);
542 free(entry->seglist, M_DRM);
543 free(entry->seglist_bus, M_DRM);
544 return DRM_ERR(ENOMEM);
547 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
548 sizeof(*dma->pagelist));
550 DRM_DEBUG( "pagelist: %d entries\n",
551 dma->page_count + (count << page_order) );
553 entry->buf_size = size;
554 entry->page_order = page_order;
558 while ( entry->buf_count < count ) {
559 vaddr = (vm_offset_t)drm_pci_alloc(dev, size, alignment,
560 0xfffffffful, &bus_addr);
562 /* Set count correctly so we free the proper amount. */
563 entry->buf_count = count;
564 entry->seg_count = count;
565 drm_cleanup_buf_error(dev, entry);
566 free(temp_pagelist, M_DRM);
567 return DRM_ERR(ENOMEM);
570 entry->seglist_bus[entry->seg_count] = bus_addr;
571 entry->seglist[entry->seg_count++] = vaddr;
572 for ( i = 0 ; i < (1 << page_order) ; i++ ) {
573 DRM_DEBUG( "page %d @ 0x%08lx\n",
574 dma->page_count + page_count,
575 (long)vaddr + PAGE_SIZE * i );
576 temp_pagelist[dma->page_count + page_count++] =
577 vaddr + PAGE_SIZE * i;
580 offset + size <= total && entry->buf_count < count ;
581 offset += alignment, ++entry->buf_count ) {
582 buf = &entry->buflist[entry->buf_count];
583 buf->idx = dma->buf_count + entry->buf_count;
584 buf->total = alignment;
587 buf->offset = (dma->byte_count + byte_count + offset);
588 buf->address = (void *)(vaddr + offset);
589 buf->bus_address = bus_addr + offset;
594 buf->dev_priv_size = dev->dev_priv_size;
595 buf->dev_private = malloc(buf->dev_priv_size, M_DRM,
597 if (buf->dev_private == NULL) {
598 /* Set count correctly so we free the proper amount. */
599 entry->buf_count = count;
600 entry->seg_count = count;
601 drm_cleanup_buf_error(dev, entry);
602 free(temp_pagelist, M_DRM);
603 return DRM_ERR(ENOMEM);
606 DRM_DEBUG( "buffer %d @ %p\n",
607 entry->buf_count, buf->address );
609 byte_count += PAGE_SIZE << page_order;
612 temp_buflist = realloc(dma->buflist,
613 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), M_DRM,
615 if (temp_buflist == NULL) {
616 /* Free the entry because it isn't valid */
617 drm_cleanup_buf_error(dev, entry);
618 free(temp_pagelist, M_DRM);
619 return DRM_ERR(ENOMEM);
621 dma->buflist = temp_buflist;
623 for ( i = 0 ; i < entry->buf_count ; i++ ) {
624 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
627 /* No allocations failed, so now we can replace the orginal pagelist
630 free(dma->pagelist, M_DRM);
631 dma->pagelist = temp_pagelist;
633 dma->buf_count += entry->buf_count;
634 dma->seg_count += entry->seg_count;
635 dma->page_count += entry->seg_count << page_order;
636 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
638 request->count = entry->buf_count;
639 request->size = size;
645 static int drm_addbufs_sg(drm_device_t *dev, drm_buf_desc_t *request)
647 drm_device_dma_t *dma = dev->dma;
648 drm_buf_entry_t *entry;
650 unsigned long offset;
651 unsigned long agp_offset;
660 drm_buf_t **temp_buflist;
662 count = request->count;
663 order = drm_order(request->size);
666 alignment = (request->flags & _DRM_PAGE_ALIGN)
667 ? round_page(size) : size;
668 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
669 total = PAGE_SIZE << page_order;
672 agp_offset = request->agp_start;
674 DRM_DEBUG( "count: %d\n", count );
675 DRM_DEBUG( "order: %d\n", order );
676 DRM_DEBUG( "size: %d\n", size );
677 DRM_DEBUG( "agp_offset: %ld\n", agp_offset );
678 DRM_DEBUG( "alignment: %d\n", alignment );
679 DRM_DEBUG( "page_order: %d\n", page_order );
680 DRM_DEBUG( "total: %d\n", total );
682 entry = &dma->bufs[order];
684 entry->buflist = malloc(count * sizeof(*entry->buflist), M_DRM,
686 if (entry->buflist == NULL)
687 return DRM_ERR(ENOMEM);
689 entry->buf_size = size;
690 entry->page_order = page_order;
694 while ( entry->buf_count < count ) {
695 buf = &entry->buflist[entry->buf_count];
696 buf->idx = dma->buf_count + entry->buf_count;
697 buf->total = alignment;
701 buf->offset = (dma->byte_count + offset);
702 buf->bus_address = agp_offset + offset;
703 buf->address = (void *)(agp_offset + offset + dev->sg->handle);
708 buf->dev_priv_size = dev->dev_priv_size;
709 buf->dev_private = malloc(buf->dev_priv_size, M_DRM,
711 if (buf->dev_private == NULL) {
712 /* Set count correctly so we free the proper amount. */
713 entry->buf_count = count;
714 drm_cleanup_buf_error(dev, entry);
715 return DRM_ERR(ENOMEM);
718 DRM_DEBUG( "buffer %d @ %p\n",
719 entry->buf_count, buf->address );
723 byte_count += PAGE_SIZE << page_order;
726 DRM_DEBUG( "byte_count: %d\n", byte_count );
728 temp_buflist = realloc(dma->buflist,
729 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), M_DRM,
731 if (temp_buflist == NULL) {
732 /* Free the entry because it isn't valid */
733 drm_cleanup_buf_error(dev, entry);
734 return DRM_ERR(ENOMEM);
736 dma->buflist = temp_buflist;
738 for ( i = 0 ; i < entry->buf_count ; i++ ) {
739 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
742 dma->buf_count += entry->buf_count;
743 dma->byte_count += byte_count;
745 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
746 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
748 request->count = entry->buf_count;
749 request->size = size;
751 dma->flags = _DRM_DMA_USE_SG;
756 int drm_addbufs(DRM_IOCTL_ARGS)
759 drm_buf_desc_t request;
763 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_desc_t *)data, sizeof(request) );
765 if (request.count < 0 || request.count > 4096)
766 return DRM_ERR(EINVAL);
768 order = drm_order(request.size);
769 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
770 return DRM_ERR(EINVAL);
772 DRM_SPINLOCK(&dev->dma_lock);
773 /* No more allocations after first buffer-using ioctl. */
774 if (dev->buf_use != 0) {
775 DRM_SPINUNLOCK(&dev->dma_lock);
776 return DRM_ERR(EBUSY);
778 /* No more than one allocation per order */
779 if (dev->dma->bufs[order].buf_count != 0) {
780 DRM_SPINUNLOCK(&dev->dma_lock);
781 return DRM_ERR(ENOMEM);
784 if ( request.flags & _DRM_AGP_BUFFER )
785 err = drm_addbufs_agp(dev, &request);
787 if ( request.flags & _DRM_SG_BUFFER )
788 err = drm_addbufs_sg(dev, &request);
790 err = drm_addbufs_pci(dev, &request);
791 DRM_SPINUNLOCK(&dev->dma_lock);
793 DRM_COPY_TO_USER_IOCTL((drm_buf_desc_t *)data, request, sizeof(request));
798 int drm_infobufs(DRM_IOCTL_ARGS)
801 drm_device_dma_t *dma = dev->dma;
802 drm_buf_info_t request;
807 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_info_t *)data, sizeof(request) );
809 DRM_SPINLOCK(&dev->dma_lock);
810 ++dev->buf_use; /* Can't allocate more after this call */
811 DRM_SPINUNLOCK(&dev->dma_lock);
813 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
814 if ( dma->bufs[i].buf_count ) ++count;
817 DRM_DEBUG( "count = %d\n", count );
819 if ( request.count >= count ) {
820 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
821 if ( dma->bufs[i].buf_count ) {
824 from.count = dma->bufs[i].buf_count;
825 from.size = dma->bufs[i].buf_size;
826 from.low_mark = dma->bufs[i].freelist.low_mark;
827 from.high_mark = dma->bufs[i].freelist.high_mark;
829 if (DRM_COPY_TO_USER(&request.list[count], &from,
830 sizeof(drm_buf_desc_t)) != 0) {
831 retcode = DRM_ERR(EFAULT);
835 DRM_DEBUG( "%d %d %d %d %d\n",
837 dma->bufs[i].buf_count,
838 dma->bufs[i].buf_size,
839 dma->bufs[i].freelist.low_mark,
840 dma->bufs[i].freelist.high_mark );
845 request.count = count;
847 DRM_COPY_TO_USER_IOCTL( (drm_buf_info_t *)data, request, sizeof(request) );
852 int drm_markbufs(DRM_IOCTL_ARGS)
855 drm_device_dma_t *dma = dev->dma;
856 drm_buf_desc_t request;
859 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_desc_t *)data, sizeof(request) );
861 DRM_DEBUG( "%d, %d, %d\n",
862 request.size, request.low_mark, request.high_mark );
865 order = drm_order(request.size);
866 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
867 request.low_mark < 0 || request.high_mark < 0) {
868 return DRM_ERR(EINVAL);
871 DRM_SPINLOCK(&dev->dma_lock);
872 if (request.low_mark > dma->bufs[order].buf_count ||
873 request.high_mark > dma->bufs[order].buf_count) {
874 return DRM_ERR(EINVAL);
877 dma->bufs[order].freelist.low_mark = request.low_mark;
878 dma->bufs[order].freelist.high_mark = request.high_mark;
879 DRM_SPINUNLOCK(&dev->dma_lock);
884 int drm_freebufs(DRM_IOCTL_ARGS)
887 drm_device_dma_t *dma = dev->dma;
888 drm_buf_free_t request;
894 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_free_t *)data, sizeof(request) );
896 DRM_DEBUG( "%d\n", request.count );
898 DRM_SPINLOCK(&dev->dma_lock);
899 for ( i = 0 ; i < request.count ; i++ ) {
900 if (DRM_COPY_FROM_USER(&idx, &request.list[i], sizeof(idx))) {
901 retcode = DRM_ERR(EFAULT);
904 if ( idx < 0 || idx >= dma->buf_count ) {
905 DRM_ERROR( "Index %d (of %d max)\n",
906 idx, dma->buf_count - 1 );
907 retcode = DRM_ERR(EINVAL);
910 buf = dma->buflist[idx];
911 if ( buf->filp != filp ) {
912 DRM_ERROR("Process %d freeing buffer not owned\n",
914 retcode = DRM_ERR(EINVAL);
917 drm_free_buffer(dev, buf);
919 DRM_SPINUNLOCK(&dev->dma_lock);
924 int drm_mapbufs(DRM_IOCTL_ARGS)
927 drm_device_dma_t *dma = dev->dma;
936 #elif defined(__NetBSD__) || defined(__OpenBSD__)
940 #endif /* __NetBSD__ || __OpenBSD__ */
942 drm_buf_map_t request;
945 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_map_t *)data, sizeof(request) );
947 #if defined(__NetBSD__) || defined(__OpenBSD__)
948 if (!vfinddev(kdev, VCHR, &vn))
949 return 0; /* FIXME: Shouldn't this be EINVAL or something? */
950 #endif /* __NetBSD__ || __OpenBSD */
952 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
953 vms = p->td_proc->p_vmspace;
958 DRM_SPINLOCK(&dev->dma_lock);
959 dev->buf_use++; /* Can't allocate more after this call */
960 DRM_SPINUNLOCK(&dev->dma_lock);
962 if (request.count < dma->buf_count)
965 if ((dev->use_agp && (dma->flags & _DRM_DMA_USE_AGP)) ||
966 (dev->use_sg && (dma->flags & _DRM_DMA_USE_SG))) {
967 drm_local_map_t *map = dev->agp_buffer_map;
973 size = round_page(map->size);
976 size = round_page(dma->byte_count),
981 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
982 #if __FreeBSD_version >= 600023
983 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
984 VM_PROT_ALL, MAP_SHARED, OBJT_DEVICE, kdev, foff );
986 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
987 VM_PROT_ALL, MAP_SHARED, SLIST_FIRST(&kdev->si_hlist), foff );
989 #elif defined(__NetBSD__) || defined(__OpenBSD__)
990 vaddr = round_page((vaddr_t)vms->vm_daddr + MAXDSIZ);
991 retcode = uvm_mmap(&vms->vm_map, &vaddr, size,
992 UVM_PROT_READ | UVM_PROT_WRITE, UVM_PROT_ALL, MAP_SHARED,
993 &vn->v_uobj, foff, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
994 #endif /* __NetBSD__ || __OpenBSD */
998 request.virtual = (void *)vaddr;
1000 for ( i = 0 ; i < dma->buf_count ; i++ ) {
1001 if (DRM_COPY_TO_USER(&request.list[i].idx,
1002 &dma->buflist[i]->idx, sizeof(request.list[0].idx))) {
1006 if (DRM_COPY_TO_USER(&request.list[i].total,
1007 &dma->buflist[i]->total, sizeof(request.list[0].total))) {
1011 if (DRM_COPY_TO_USER(&request.list[i].used, &zero,
1016 address = vaddr + dma->buflist[i]->offset; /* *** */
1017 if (DRM_COPY_TO_USER(&request.list[i].address, &address,
1025 request.count = dma->buf_count;
1027 DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
1029 DRM_COPY_TO_USER_IOCTL((drm_buf_map_t *)data, request, sizeof(request));
1031 return DRM_ERR(retcode);