1 /* drm_bufs.h -- Generic buffer template -*- linux-c -*-
2 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
5 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
6 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
29 * Rickard E. (Rik) Faith <faith@valinux.com>
30 * Gareth Hughes <gareth@valinux.com>
37 * Compute order. Can be made faster.
39 int drm_order(unsigned long size)
44 for ( order = 0, tmp = size ; tmp >>= 1 ; ++order );
46 if ( size & ~(1 << order) )
52 unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource)
57 resource = resource * 4 + 0x10;
59 bsr = bus_alloc_resource_any(dev->device, SYS_RES_MEMORY, &resource,
60 RF_ACTIVE | RF_SHAREABLE);
62 DRM_ERROR("Couldn't find resource 0x%x\n", resource);
66 offset = rman_get_start(bsr);
68 bus_release_resource(dev->device, SYS_RES_MEMORY, resource, bsr);
73 unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource)
78 resource = resource * 4 + 0x10;
80 bsr = bus_alloc_resource_any(dev->device, SYS_RES_MEMORY, &resource,
81 RF_ACTIVE | RF_SHAREABLE);
83 DRM_ERROR("Couldn't find resource 0x%x\n", resource);
87 len = rman_get_size(bsr);
89 bus_release_resource(dev->device, SYS_RES_MEMORY, resource, bsr);
94 int drm_initmap(drm_device_t *dev, unsigned long start, unsigned long len,
95 unsigned int resource, int type, int flags)
100 if (type != _DRM_REGISTERS && type != _DRM_FRAME_BUFFER)
105 map = malloc(sizeof(*map), M_DRM, M_ZERO | M_NOWAIT);
109 map->rid = resource * 4 + 0x10;
110 bsr = bus_alloc_resource_any(dev->device, SYS_RES_MEMORY, &map->rid,
111 RF_ACTIVE | RF_SHAREABLE);
113 DRM_ERROR("Couldn't allocate %s resource\n",
114 ((type == _DRM_REGISTERS) ? "mmio" : "framebuffer"));
119 map->kernel_owned = 1;
123 map->bst = rman_get_bustag(bsr);
124 map->bsh = rman_get_bushandle(bsr);
128 if (type == _DRM_REGISTERS)
129 map->handle = rman_get_virtual(bsr);
131 DRM_DEBUG("initmap %d,0x%x@0x%lx/0x%lx\n", map->type, map->flags,
132 map->offset, map->size);
134 if (map->flags & _DRM_WRITE_COMBINING) {
137 err = drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC);
143 TAILQ_INSERT_TAIL(&dev->maplist, map, link);
149 int drm_addmap(DRM_IOCTL_ARGS)
153 drm_local_map_t *map;
156 if (!(dev->flags & (FREAD|FWRITE)))
157 return DRM_ERR(EACCES); /* Require read/write */
159 DRM_COPY_FROM_USER_IOCTL( request, (drm_map_t *)data, sizeof(drm_map_t) );
161 /* Only allow shared memory to be removable since we only keep enough
162 * book keeping information about shared memory to allow for removal
163 * when processes fork.
165 if ((request.flags & _DRM_REMOVABLE) && request.type != _DRM_SHM)
167 if ((request.offset & PAGE_MASK) || (request.size & PAGE_MASK))
169 if (request.offset + request.size < request.offset)
172 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
173 request.offset, request.size, request.type);
175 /* Check if this is just another version of a kernel-allocated map, and
176 * just hand that back if so.
178 if (request.type == _DRM_REGISTERS || request.type == _DRM_FRAME_BUFFER)
181 TAILQ_FOREACH(map, &dev->maplist, link) {
182 if (map->kernel_owned && map->type == request.type &&
183 map->offset == request.offset) {
184 /* XXX: this size setting is questionable. */
185 map->size = request.size;
186 DRM_DEBUG("Found kernel map %d\n", request.type);
193 /* Allocate a new map structure, fill it in, and do any type-specific
194 * initialization necessary.
196 map = malloc(sizeof(*map), M_DRM, M_ZERO | M_NOWAIT);
198 return DRM_ERR(ENOMEM);
200 map->offset = request.offset;
201 map->size = request.size;
202 map->type = request.type;
203 map->flags = request.flags;
205 switch ( map->type ) {
207 map->handle = drm_ioremap(dev, map);
208 if (!(map->flags & _DRM_WRITE_COMBINING))
211 case _DRM_FRAME_BUFFER:
212 if (drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC) == 0)
216 map->handle = malloc(map->size, M_DRM, M_NOWAIT);
217 DRM_DEBUG( "%lu %d %p\n",
218 map->size, drm_order(map->size), map->handle );
219 if ( !map->handle ) {
221 return DRM_ERR(ENOMEM);
223 map->offset = (unsigned long)map->handle;
224 if ( map->flags & _DRM_CONTAINS_LOCK ) {
225 /* Prevent a 2nd X Server from creating a 2nd lock */
227 if (dev->lock.hw_lock != NULL) {
229 free(map->handle, M_DRM);
231 return DRM_ERR(EBUSY);
233 dev->lock.hw_lock = map->handle; /* Pointer to lock */
238 map->offset += dev->agp->base;
239 map->mtrr = dev->agp->mtrr; /* for getmap */
241 case _DRM_SCATTER_GATHER:
244 return DRM_ERR(EINVAL);
246 map->offset = map->offset + dev->sg->handle;
248 case _DRM_CONSISTENT:
249 map->handle = drm_pci_alloc(dev, map->size, map->size,
250 0xfffffffful, &bus_addr);
251 if (map->handle == NULL) {
255 map->offset = (unsigned long)bus_addr;
259 return DRM_ERR(EINVAL);
263 TAILQ_INSERT_TAIL(&dev->maplist, map, link);
266 /* Jumped to, with lock held, when a kernel map is found. */
267 request.offset = map->offset;
268 request.size = map->size;
269 request.type = map->type;
270 request.flags = map->flags;
271 request.mtrr = map->mtrr;
272 request.handle = map->handle;
275 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", request.type, request.offset, request.size);
277 if ( request.type != _DRM_SHM ) {
278 request.handle = (void *)request.offset;
281 DRM_COPY_TO_USER_IOCTL( (drm_map_t *)data, request, sizeof(drm_map_t) );
286 void drm_remove_map(drm_device_t *dev, drm_local_map_t *map)
288 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
290 TAILQ_REMOVE(&dev->maplist, map, link);
294 if (map->bsr == NULL)
295 drm_ioremapfree(map);
297 case _DRM_FRAME_BUFFER:
299 int __unused retcode;
301 retcode = drm_mtrr_del(map->offset, map->size,
303 DRM_DEBUG("mtrr_del = %d\n", retcode);
307 free(map->handle, M_DRM);
310 case _DRM_SCATTER_GATHER:
312 case _DRM_CONSISTENT:
313 drm_pci_free(dev, map->size, map->handle, map->offset);
317 if (map->bsr != NULL) {
318 bus_release_resource(dev->device, SYS_RES_MEMORY, map->rid,
325 /* Remove a map private from list and deallocate resources if the mapping
329 int drm_rmmap(DRM_IOCTL_ARGS)
332 drm_local_map_t *map;
335 DRM_COPY_FROM_USER_IOCTL( request, (drm_map_t *)data, sizeof(request) );
338 TAILQ_FOREACH(map, &dev->maplist, link) {
339 if (map->handle == request.handle &&
340 map->flags & _DRM_REMOVABLE)
344 /* No match found. */
347 return DRM_ERR(EINVAL);
350 drm_remove_map(dev, map);
358 static void drm_cleanup_buf_error(drm_device_t *dev, drm_buf_entry_t *entry)
362 if (entry->seg_count) {
363 for (i = 0; i < entry->seg_count; i++) {
364 drm_pci_free(dev, entry->buf_size,
365 (void *)entry->seglist[i],
366 entry->seglist_bus[i]);
368 free(entry->seglist, M_DRM);
369 free(entry->seglist_bus, M_DRM);
371 entry->seg_count = 0;
374 if (entry->buf_count) {
375 for (i = 0; i < entry->buf_count; i++) {
376 free(entry->buflist[i].dev_private, M_DRM);
378 free(entry->buflist, M_DRM);
380 entry->buf_count = 0;
384 static int drm_addbufs_agp(drm_device_t *dev, drm_buf_desc_t *request)
386 drm_device_dma_t *dma = dev->dma;
387 drm_buf_entry_t *entry;
389 unsigned long offset;
390 unsigned long agp_offset;
399 drm_buf_t **temp_buflist;
401 count = request->count;
402 order = drm_order(request->size);
405 alignment = (request->flags & _DRM_PAGE_ALIGN)
406 ? round_page(size) : size;
407 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
408 total = PAGE_SIZE << page_order;
411 agp_offset = dev->agp->base + request->agp_start;
413 DRM_DEBUG( "count: %d\n", count );
414 DRM_DEBUG( "order: %d\n", order );
415 DRM_DEBUG( "size: %d\n", size );
416 DRM_DEBUG( "agp_offset: 0x%lx\n", agp_offset );
417 DRM_DEBUG( "alignment: %d\n", alignment );
418 DRM_DEBUG( "page_order: %d\n", page_order );
419 DRM_DEBUG( "total: %d\n", total );
421 entry = &dma->bufs[order];
423 entry->buflist = malloc(count * sizeof(*entry->buflist), M_DRM,
425 if ( !entry->buflist ) {
426 return DRM_ERR(ENOMEM);
429 entry->buf_size = size;
430 entry->page_order = page_order;
434 while ( entry->buf_count < count ) {
435 buf = &entry->buflist[entry->buf_count];
436 buf->idx = dma->buf_count + entry->buf_count;
437 buf->total = alignment;
441 buf->offset = (dma->byte_count + offset);
442 buf->bus_address = agp_offset + offset;
443 buf->address = (void *)(agp_offset + offset);
448 buf->dev_priv_size = dev->dev_priv_size;
449 buf->dev_private = malloc(buf->dev_priv_size, M_DRM,
451 if (buf->dev_private == NULL) {
452 /* Set count correctly so we free the proper amount. */
453 entry->buf_count = count;
454 drm_cleanup_buf_error(dev, entry);
455 return DRM_ERR(ENOMEM);
460 byte_count += PAGE_SIZE << page_order;
463 DRM_DEBUG( "byte_count: %d\n", byte_count );
465 temp_buflist = realloc(dma->buflist,
466 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), M_DRM,
468 if (temp_buflist == NULL) {
469 /* Free the entry because it isn't valid */
470 drm_cleanup_buf_error(dev, entry);
471 return DRM_ERR(ENOMEM);
473 dma->buflist = temp_buflist;
475 for ( i = 0 ; i < entry->buf_count ; i++ ) {
476 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
479 dma->buf_count += entry->buf_count;
480 dma->byte_count += byte_count;
482 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
483 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
485 request->count = entry->buf_count;
486 request->size = size;
488 dma->flags = _DRM_DMA_USE_AGP;
493 static int drm_addbufs_pci(drm_device_t *dev, drm_buf_desc_t *request)
495 drm_device_dma_t *dma = dev->dma;
501 drm_buf_entry_t *entry;
505 unsigned long offset;
509 unsigned long *temp_pagelist;
510 drm_buf_t **temp_buflist;
513 count = request->count;
514 order = drm_order(request->size);
517 DRM_DEBUG( "count=%d, size=%d (%d), order=%d\n",
518 request->count, request->size, size, order );
520 alignment = (request->flags & _DRM_PAGE_ALIGN)
521 ? round_page(size) : size;
522 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
523 total = PAGE_SIZE << page_order;
525 entry = &dma->bufs[order];
527 entry->buflist = malloc(count * sizeof(*entry->buflist), M_DRM,
529 entry->seglist = malloc(count * sizeof(*entry->seglist), M_DRM,
531 entry->seglist_bus = malloc(count * sizeof(*entry->seglist_bus), M_DRM,
534 /* Keep the original pagelist until we know all the allocations
537 temp_pagelist = malloc((dma->page_count + (count << page_order)) *
538 sizeof(*dma->pagelist), M_DRM, M_NOWAIT);
540 if (entry->buflist == NULL || entry->seglist == NULL ||
541 entry->seglist_bus == NULL || temp_pagelist == NULL) {
542 free(entry->buflist, M_DRM);
543 free(entry->seglist, M_DRM);
544 free(entry->seglist_bus, M_DRM);
545 return DRM_ERR(ENOMEM);
548 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
549 sizeof(*dma->pagelist));
551 DRM_DEBUG( "pagelist: %d entries\n",
552 dma->page_count + (count << page_order) );
554 entry->buf_size = size;
555 entry->page_order = page_order;
559 while ( entry->buf_count < count ) {
560 vaddr = (vm_offset_t)drm_pci_alloc(dev, size, alignment,
561 0xfffffffful, &bus_addr);
563 /* Set count correctly so we free the proper amount. */
564 entry->buf_count = count;
565 entry->seg_count = count;
566 drm_cleanup_buf_error(dev, entry);
567 free(temp_pagelist, M_DRM);
568 return DRM_ERR(ENOMEM);
571 entry->seglist_bus[entry->seg_count] = bus_addr;
572 entry->seglist[entry->seg_count++] = vaddr;
573 for ( i = 0 ; i < (1 << page_order) ; i++ ) {
574 DRM_DEBUG( "page %d @ 0x%08lx\n",
575 dma->page_count + page_count,
576 (long)vaddr + PAGE_SIZE * i );
577 temp_pagelist[dma->page_count + page_count++] =
578 vaddr + PAGE_SIZE * i;
581 offset + size <= total && entry->buf_count < count ;
582 offset += alignment, ++entry->buf_count ) {
583 buf = &entry->buflist[entry->buf_count];
584 buf->idx = dma->buf_count + entry->buf_count;
585 buf->total = alignment;
588 buf->offset = (dma->byte_count + byte_count + offset);
589 buf->address = (void *)(vaddr + offset);
590 buf->bus_address = bus_addr + offset;
595 buf->dev_priv_size = dev->dev_priv_size;
596 buf->dev_private = malloc(buf->dev_priv_size, M_DRM,
598 if (buf->dev_private == NULL) {
599 /* Set count correctly so we free the proper amount. */
600 entry->buf_count = count;
601 entry->seg_count = count;
602 drm_cleanup_buf_error(dev, entry);
603 free(temp_pagelist, M_DRM);
604 return DRM_ERR(ENOMEM);
607 DRM_DEBUG( "buffer %d @ %p\n",
608 entry->buf_count, buf->address );
610 byte_count += PAGE_SIZE << page_order;
613 temp_buflist = realloc(dma->buflist,
614 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), M_DRM,
616 if (temp_buflist == NULL) {
617 /* Free the entry because it isn't valid */
618 drm_cleanup_buf_error(dev, entry);
619 free(temp_pagelist, M_DRM);
620 return DRM_ERR(ENOMEM);
622 dma->buflist = temp_buflist;
624 for ( i = 0 ; i < entry->buf_count ; i++ ) {
625 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
628 /* No allocations failed, so now we can replace the orginal pagelist
631 free(dma->pagelist, M_DRM);
632 dma->pagelist = temp_pagelist;
634 dma->buf_count += entry->buf_count;
635 dma->seg_count += entry->seg_count;
636 dma->page_count += entry->seg_count << page_order;
637 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
639 request->count = entry->buf_count;
640 request->size = size;
646 static int drm_addbufs_sg(drm_device_t *dev, drm_buf_desc_t *request)
648 drm_device_dma_t *dma = dev->dma;
649 drm_buf_entry_t *entry;
651 unsigned long offset;
652 unsigned long agp_offset;
661 drm_buf_t **temp_buflist;
663 count = request->count;
664 order = drm_order(request->size);
667 alignment = (request->flags & _DRM_PAGE_ALIGN)
668 ? round_page(size) : size;
669 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
670 total = PAGE_SIZE << page_order;
673 agp_offset = request->agp_start;
675 DRM_DEBUG( "count: %d\n", count );
676 DRM_DEBUG( "order: %d\n", order );
677 DRM_DEBUG( "size: %d\n", size );
678 DRM_DEBUG( "agp_offset: %ld\n", agp_offset );
679 DRM_DEBUG( "alignment: %d\n", alignment );
680 DRM_DEBUG( "page_order: %d\n", page_order );
681 DRM_DEBUG( "total: %d\n", total );
683 entry = &dma->bufs[order];
685 entry->buflist = malloc(count * sizeof(*entry->buflist), M_DRM,
687 if (entry->buflist == NULL)
688 return DRM_ERR(ENOMEM);
690 entry->buf_size = size;
691 entry->page_order = page_order;
695 while ( entry->buf_count < count ) {
696 buf = &entry->buflist[entry->buf_count];
697 buf->idx = dma->buf_count + entry->buf_count;
698 buf->total = alignment;
702 buf->offset = (dma->byte_count + offset);
703 buf->bus_address = agp_offset + offset;
704 buf->address = (void *)(agp_offset + offset + dev->sg->handle);
709 buf->dev_priv_size = dev->dev_priv_size;
710 buf->dev_private = malloc(buf->dev_priv_size, M_DRM,
712 if (buf->dev_private == NULL) {
713 /* Set count correctly so we free the proper amount. */
714 entry->buf_count = count;
715 drm_cleanup_buf_error(dev, entry);
716 return DRM_ERR(ENOMEM);
719 DRM_DEBUG( "buffer %d @ %p\n",
720 entry->buf_count, buf->address );
724 byte_count += PAGE_SIZE << page_order;
727 DRM_DEBUG( "byte_count: %d\n", byte_count );
729 temp_buflist = realloc(dma->buflist,
730 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), M_DRM,
732 if (temp_buflist == NULL) {
733 /* Free the entry because it isn't valid */
734 drm_cleanup_buf_error(dev, entry);
735 return DRM_ERR(ENOMEM);
737 dma->buflist = temp_buflist;
739 for ( i = 0 ; i < entry->buf_count ; i++ ) {
740 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
743 dma->buf_count += entry->buf_count;
744 dma->byte_count += byte_count;
746 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
747 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
749 request->count = entry->buf_count;
750 request->size = size;
752 dma->flags = _DRM_DMA_USE_SG;
757 int drm_addbufs(DRM_IOCTL_ARGS)
760 drm_buf_desc_t request;
764 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_desc_t *)data, sizeof(request) );
766 if (request.count < 0 || request.count > 4096)
767 return DRM_ERR(EINVAL);
769 order = drm_order(request.size);
770 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
771 return DRM_ERR(EINVAL);
773 DRM_SPINLOCK(&dev->dma_lock);
774 /* No more allocations after first buffer-using ioctl. */
775 if (dev->buf_use != 0) {
776 DRM_SPINUNLOCK(&dev->dma_lock);
777 return DRM_ERR(EBUSY);
779 /* No more than one allocation per order */
780 if (dev->dma->bufs[order].buf_count != 0) {
781 DRM_SPINUNLOCK(&dev->dma_lock);
782 return DRM_ERR(ENOMEM);
785 if ( request.flags & _DRM_AGP_BUFFER )
786 err = drm_addbufs_agp(dev, &request);
788 if ( request.flags & _DRM_SG_BUFFER )
789 err = drm_addbufs_sg(dev, &request);
791 err = drm_addbufs_pci(dev, &request);
792 DRM_SPINUNLOCK(&dev->dma_lock);
794 DRM_COPY_TO_USER_IOCTL((drm_buf_desc_t *)data, request, sizeof(request));
799 int drm_infobufs(DRM_IOCTL_ARGS)
802 drm_device_dma_t *dma = dev->dma;
803 drm_buf_info_t request;
808 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_info_t *)data, sizeof(request) );
810 DRM_SPINLOCK(&dev->dma_lock);
811 ++dev->buf_use; /* Can't allocate more after this call */
812 DRM_SPINUNLOCK(&dev->dma_lock);
814 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
815 if ( dma->bufs[i].buf_count ) ++count;
818 DRM_DEBUG( "count = %d\n", count );
820 if ( request.count >= count ) {
821 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
822 if ( dma->bufs[i].buf_count ) {
825 from.count = dma->bufs[i].buf_count;
826 from.size = dma->bufs[i].buf_size;
827 from.low_mark = dma->bufs[i].freelist.low_mark;
828 from.high_mark = dma->bufs[i].freelist.high_mark;
830 if (DRM_COPY_TO_USER(&request.list[count], &from,
831 sizeof(drm_buf_desc_t)) != 0) {
832 retcode = DRM_ERR(EFAULT);
836 DRM_DEBUG( "%d %d %d %d %d\n",
838 dma->bufs[i].buf_count,
839 dma->bufs[i].buf_size,
840 dma->bufs[i].freelist.low_mark,
841 dma->bufs[i].freelist.high_mark );
846 request.count = count;
848 DRM_COPY_TO_USER_IOCTL( (drm_buf_info_t *)data, request, sizeof(request) );
853 int drm_markbufs(DRM_IOCTL_ARGS)
856 drm_device_dma_t *dma = dev->dma;
857 drm_buf_desc_t request;
860 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_desc_t *)data, sizeof(request) );
862 DRM_DEBUG( "%d, %d, %d\n",
863 request.size, request.low_mark, request.high_mark );
866 order = drm_order(request.size);
867 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
868 request.low_mark < 0 || request.high_mark < 0) {
869 return DRM_ERR(EINVAL);
872 DRM_SPINLOCK(&dev->dma_lock);
873 if (request.low_mark > dma->bufs[order].buf_count ||
874 request.high_mark > dma->bufs[order].buf_count) {
875 return DRM_ERR(EINVAL);
878 dma->bufs[order].freelist.low_mark = request.low_mark;
879 dma->bufs[order].freelist.high_mark = request.high_mark;
880 DRM_SPINUNLOCK(&dev->dma_lock);
885 int drm_freebufs(DRM_IOCTL_ARGS)
888 drm_device_dma_t *dma = dev->dma;
889 drm_buf_free_t request;
895 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_free_t *)data, sizeof(request) );
897 DRM_DEBUG( "%d\n", request.count );
899 DRM_SPINLOCK(&dev->dma_lock);
900 for ( i = 0 ; i < request.count ; i++ ) {
901 if (DRM_COPY_FROM_USER(&idx, &request.list[i], sizeof(idx))) {
902 retcode = DRM_ERR(EFAULT);
905 if ( idx < 0 || idx >= dma->buf_count ) {
906 DRM_ERROR( "Index %d (of %d max)\n",
907 idx, dma->buf_count - 1 );
908 retcode = DRM_ERR(EINVAL);
911 buf = dma->buflist[idx];
912 if ( buf->filp != filp ) {
913 DRM_ERROR("Process %d freeing buffer not owned\n",
915 retcode = DRM_ERR(EINVAL);
918 drm_free_buffer(dev, buf);
920 DRM_SPINUNLOCK(&dev->dma_lock);
925 int drm_mapbufs(DRM_IOCTL_ARGS)
928 drm_device_dma_t *dma = dev->dma;
937 #elif defined(__NetBSD__) || defined(__OpenBSD__)
941 #endif /* __NetBSD__ || __OpenBSD__ */
943 drm_buf_map_t request;
946 DRM_COPY_FROM_USER_IOCTL( request, (drm_buf_map_t *)data, sizeof(request) );
948 #if defined(__NetBSD__) || defined(__OpenBSD__)
949 if (!vfinddev(kdev, VCHR, &vn))
950 return 0; /* FIXME: Shouldn't this be EINVAL or something? */
951 #endif /* __NetBSD__ || __OpenBSD */
953 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
954 vms = p->td_proc->p_vmspace;
959 DRM_SPINLOCK(&dev->dma_lock);
960 dev->buf_use++; /* Can't allocate more after this call */
961 DRM_SPINUNLOCK(&dev->dma_lock);
963 if (request.count < dma->buf_count)
966 if ((dev->use_agp && (dma->flags & _DRM_DMA_USE_AGP)) ||
967 (dev->use_sg && (dma->flags & _DRM_DMA_USE_SG))) {
968 drm_local_map_t *map = dev->agp_buffer_map;
974 size = round_page(map->size);
977 size = round_page(dma->byte_count),
982 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
983 #if __FreeBSD_version >= 600023
984 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
985 VM_PROT_ALL, MAP_SHARED, OBJT_DEVICE, kdev, foff );
987 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
988 VM_PROT_ALL, MAP_SHARED, SLIST_FIRST(&kdev->si_hlist), foff );
990 #elif defined(__NetBSD__) || defined(__OpenBSD__)
991 vaddr = round_page((vaddr_t)vms->vm_daddr + MAXDSIZ);
992 retcode = uvm_mmap(&vms->vm_map, &vaddr, size,
993 UVM_PROT_READ | UVM_PROT_WRITE, UVM_PROT_ALL, MAP_SHARED,
994 &vn->v_uobj, foff, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
995 #endif /* __NetBSD__ || __OpenBSD */
999 request.virtual = (void *)vaddr;
1001 for ( i = 0 ; i < dma->buf_count ; i++ ) {
1002 if (DRM_COPY_TO_USER(&request.list[i].idx,
1003 &dma->buflist[i]->idx, sizeof(request.list[0].idx))) {
1007 if (DRM_COPY_TO_USER(&request.list[i].total,
1008 &dma->buflist[i]->total, sizeof(request.list[0].total))) {
1012 if (DRM_COPY_TO_USER(&request.list[i].used, &zero,
1017 address = vaddr + dma->buflist[i]->offset; /* *** */
1018 if (DRM_COPY_TO_USER(&request.list[i].address, &address,
1026 request.count = dma->buf_count;
1028 DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
1030 DRM_COPY_TO_USER_IOCTL((drm_buf_map_t *)data, request, sizeof(request));
1032 return DRM_ERR(retcode);