2 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
32 * Implementation of the ioctls for setup of DRM mappings and DMA buffers.
35 #include "bus/pci/pcireg.h"
37 #include "dev/drm/drmP.h"
39 /* Allocation of PCI memory resources (framebuffer, registers, etc.) for
40 * drm_get_resource_*. Note that they are not RF_ACTIVE, so there's no virtual
41 * address for accessing them. Cleaned up at unload.
43 static int drm_alloc_resource(struct drm_device *dev, int resource)
45 if (resource >= DRM_MAX_PCI_RESOURCE) {
46 DRM_ERROR("Resource %d too large\n", resource);
51 if (dev->pcir[resource] != NULL) {
56 dev->pcirid[resource] = PCIR_BAR(resource);
57 dev->pcir[resource] = bus_alloc_resource_any(dev->device,
58 SYS_RES_MEMORY, &dev->pcirid[resource], RF_SHAREABLE);
61 if (dev->pcir[resource] == NULL) {
62 DRM_ERROR("Couldn't find resource 0x%x\n", resource);
69 unsigned long drm_get_resource_start(struct drm_device *dev,
70 unsigned int resource)
72 if (drm_alloc_resource(dev, resource) != 0)
75 return rman_get_start(dev->pcir[resource]);
78 unsigned long drm_get_resource_len(struct drm_device *dev,
79 unsigned int resource)
81 if (drm_alloc_resource(dev, resource) != 0)
84 return rman_get_size(dev->pcir[resource]);
87 int drm_addmap(struct drm_device * dev, unsigned long offset,
89 enum drm_map_type type, enum drm_map_flags flags, drm_local_map_t **map_ptr)
93 /*drm_agp_mem_t *entry;
96 /* Only allow shared memory to be removable since we only keep enough
97 * book keeping information about shared memory to allow for removal
98 * when processes fork.
100 if ((flags & _DRM_REMOVABLE) && type != _DRM_SHM) {
101 DRM_ERROR("Requested removable map for non-DRM_SHM\n");
104 if ((offset & PAGE_MASK) || (size & PAGE_MASK)) {
105 DRM_ERROR("offset/size not page aligned: 0x%lx/0x%lx\n",
109 if (offset + size < offset) {
110 DRM_ERROR("offset and size wrap around: 0x%lx/0x%lx\n",
115 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n", offset,
118 /* Check if this is just another version of a kernel-allocated map, and
119 * just hand that back if so.
121 if (type == _DRM_REGISTERS || type == _DRM_FRAME_BUFFER ||
123 TAILQ_FOREACH(map, &dev->maplist, link) {
124 if (map->type == type && (map->offset == offset ||
125 (map->type == _DRM_SHM &&
126 map->flags == _DRM_CONTAINS_LOCK))) {
128 DRM_DEBUG("Found kernel map %d\n", type);
135 /* Allocate a new map structure, fill it in, and do any type-specific
136 * initialization necessary.
138 map = malloc(sizeof(*map), DRM_MEM_MAPS, M_ZERO | M_NOWAIT);
144 map->offset = offset;
151 map->handle = drm_ioremap(dev, map);
152 if (!(map->flags & _DRM_WRITE_COMBINING))
155 case _DRM_FRAME_BUFFER:
156 if (drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC) == 0)
160 map->handle = malloc(map->size, DRM_MEM_MAPS, M_NOWAIT);
161 DRM_DEBUG("%lu %d %p\n",
162 map->size, drm_order(map->size), map->handle);
164 free(map, DRM_MEM_MAPS);
168 map->offset = (unsigned long)map->handle;
169 if (map->flags & _DRM_CONTAINS_LOCK) {
170 /* Prevent a 2nd X Server from creating a 2nd lock */
172 if (dev->lock.hw_lock != NULL) {
174 free(map->handle, DRM_MEM_MAPS);
175 free(map, DRM_MEM_MAPS);
178 dev->lock.hw_lock = map->handle; /* Pointer to lock */
184 /* In some cases (i810 driver), user space may have already
185 * added the AGP base itself, because dev->agp->base previously
186 * only got set during AGP enable. So, only add the base
187 * address if the map's offset isn't already within the
190 if (map->offset < dev->agp->base ||
191 map->offset > dev->agp->base +
192 dev->agp->info.ai_aperture_size - 1) {
193 map->offset += dev->agp->base;
195 map->mtrr = dev->agp->mtrr; /* for getmap */
196 /*for (entry = dev->agp->memory; entry; entry = entry->next) {
197 if ((map->offset >= entry->bound) &&
198 (map->offset + map->size <=
199 entry->bound + entry->pages * PAGE_SIZE)) {
205 free(map, DRM_MEM_MAPS);
210 case _DRM_SCATTER_GATHER:
212 free(map, DRM_MEM_MAPS);
216 map->offset += dev->sg->handle;
218 case _DRM_CONSISTENT:
219 /* Unfortunately, we don't get any alignment specification from
220 * the caller, so we have to guess. drm_pci_alloc requires
221 * a power-of-two alignment, so try to align the bus address of
222 * the map to it size if possible, otherwise just assume
223 * PAGE_SIZE alignment.
226 if ((align & (align - 1)) != 0)
228 map->dmah = drm_pci_alloc(dev, map->size, align, 0xfffffffful);
229 if (map->dmah == NULL) {
230 free(map, DRM_MEM_MAPS);
234 map->handle = map->dmah->vaddr;
235 map->offset = map->dmah->busaddr;
238 DRM_ERROR("Bad map type %d\n", map->type);
239 free(map, DRM_MEM_MAPS);
245 TAILQ_INSERT_TAIL(&dev->maplist, map, link);
248 /* Jumped to, with lock held, when a kernel map is found. */
250 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", map->type, map->offset,
258 int drm_addmap_ioctl(struct drm_device *dev, void *data,
259 struct drm_file *file_priv)
261 struct drm_map *request = data;
262 drm_local_map_t *map;
265 if (!(dev->flags & (FREAD|FWRITE)))
266 return EACCES; /* Require read/write */
268 if (!DRM_SUSER(DRM_CURPROC) && request->type != _DRM_AGP)
272 err = drm_addmap(dev, request->offset, request->size, request->type,
273 request->flags, &map);
278 request->offset = map->offset;
279 request->size = map->size;
280 request->type = map->type;
281 request->flags = map->flags;
282 request->mtrr = map->mtrr;
283 request->handle = map->handle;
285 if (request->type != _DRM_SHM) {
286 request->handle = (void *)request->offset;
292 void drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
294 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
299 TAILQ_REMOVE(&dev->maplist, map, link);
303 if (map->bsr == NULL)
304 drm_ioremapfree(map);
306 case _DRM_FRAME_BUFFER:
308 int __unused retcode;
310 retcode = drm_mtrr_del(0, map->offset, map->size,
312 DRM_DEBUG("mtrr_del = %d\n", retcode);
316 free(map->handle, DRM_MEM_MAPS);
319 case _DRM_SCATTER_GATHER:
321 case _DRM_CONSISTENT:
322 drm_pci_free(dev, map->dmah);
325 DRM_ERROR("Bad map type %d\n", map->type);
329 if (map->bsr != NULL) {
330 bus_release_resource(dev->device, SYS_RES_MEMORY, map->rid,
334 free(map, DRM_MEM_MAPS);
337 /* Remove a map private from list and deallocate resources if the mapping
341 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
342 struct drm_file *file_priv)
344 drm_local_map_t *map;
345 struct drm_map *request = data;
348 TAILQ_FOREACH(map, &dev->maplist, link) {
349 if (map->handle == request->handle &&
350 map->flags & _DRM_REMOVABLE)
354 /* No match found. */
368 static void drm_cleanup_buf_error(struct drm_device *dev,
369 drm_buf_entry_t *entry)
373 if (entry->seg_count) {
374 for (i = 0; i < entry->seg_count; i++) {
375 drm_pci_free(dev, entry->seglist[i]);
377 free(entry->seglist, DRM_MEM_SEGS);
379 entry->seg_count = 0;
382 if (entry->buf_count) {
383 for (i = 0; i < entry->buf_count; i++) {
384 free(entry->buflist[i].dev_private, DRM_MEM_BUFS);
386 free(entry->buflist, DRM_MEM_BUFS);
388 entry->buf_count = 0;
392 static int drm_do_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
394 drm_device_dma_t *dma = dev->dma;
395 drm_buf_entry_t *entry;
396 /*drm_agp_mem_t *agp_entry;
399 unsigned long offset;
400 unsigned long agp_offset;
409 drm_buf_t **temp_buflist;
411 count = request->count;
412 order = drm_order(request->size);
415 alignment = (request->flags & _DRM_PAGE_ALIGN)
416 ? round_page(size) : size;
417 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
418 total = PAGE_SIZE << page_order;
421 agp_offset = dev->agp->base + request->agp_start;
423 DRM_DEBUG("count: %d\n", count);
424 DRM_DEBUG("order: %d\n", order);
425 DRM_DEBUG("size: %d\n", size);
426 DRM_DEBUG("agp_offset: 0x%lx\n", agp_offset);
427 DRM_DEBUG("alignment: %d\n", alignment);
428 DRM_DEBUG("page_order: %d\n", page_order);
429 DRM_DEBUG("total: %d\n", total);
431 /* Make sure buffers are located in AGP memory that we own */
432 /* Breaks MGA due to drm_alloc_agp not setting up entries for the
433 * memory. Safe to ignore for now because these ioctls are still
437 for (agp_entry = dev->agp->memory; agp_entry;
438 agp_entry = agp_entry->next) {
439 if ((agp_offset >= agp_entry->bound) &&
440 (agp_offset + total * count <=
441 agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
447 DRM_DEBUG("zone invalid\n");
451 entry = &dma->bufs[order];
453 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
455 if (!entry->buflist) {
459 entry->buf_size = size;
460 entry->page_order = page_order;
464 while (entry->buf_count < count) {
465 buf = &entry->buflist[entry->buf_count];
466 buf->idx = dma->buf_count + entry->buf_count;
467 buf->total = alignment;
471 buf->offset = (dma->byte_count + offset);
472 buf->bus_address = agp_offset + offset;
473 buf->address = (void *)(agp_offset + offset);
476 buf->file_priv = NULL;
478 buf->dev_priv_size = dev->driver->buf_priv_size;
479 buf->dev_private = malloc(buf->dev_priv_size, DRM_MEM_BUFS,
481 if (buf->dev_private == NULL) {
482 /* Set count correctly so we free the proper amount. */
483 entry->buf_count = count;
484 drm_cleanup_buf_error(dev, entry);
490 byte_count += PAGE_SIZE << page_order;
493 DRM_DEBUG("byte_count: %d\n", byte_count);
495 temp_buflist = realloc(dma->buflist,
496 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
497 DRM_MEM_BUFS, M_NOWAIT);
498 if (temp_buflist == NULL) {
499 /* Free the entry because it isn't valid */
500 drm_cleanup_buf_error(dev, entry);
503 dma->buflist = temp_buflist;
505 for (i = 0; i < entry->buf_count; i++) {
506 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
509 dma->buf_count += entry->buf_count;
510 dma->byte_count += byte_count;
512 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
513 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
515 request->count = entry->buf_count;
516 request->size = size;
518 dma->flags = _DRM_DMA_USE_AGP;
523 static int drm_do_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
525 drm_device_dma_t *dma = dev->dma;
531 drm_buf_entry_t *entry;
534 unsigned long offset;
538 unsigned long *temp_pagelist;
539 drm_buf_t **temp_buflist;
541 count = request->count;
542 order = drm_order(request->size);
545 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
546 request->count, request->size, size, order);
548 alignment = (request->flags & _DRM_PAGE_ALIGN)
549 ? round_page(size) : size;
550 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
551 total = PAGE_SIZE << page_order;
553 entry = &dma->bufs[order];
555 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
557 entry->seglist = malloc(count * sizeof(*entry->seglist), DRM_MEM_SEGS,
560 /* Keep the original pagelist until we know all the allocations
563 temp_pagelist = malloc((dma->page_count + (count << page_order)) *
564 sizeof(*dma->pagelist), DRM_MEM_PAGES, M_NOWAIT);
566 if (entry->buflist == NULL || entry->seglist == NULL ||
567 temp_pagelist == NULL) {
568 free(temp_pagelist, DRM_MEM_PAGES);
569 free(entry->seglist, DRM_MEM_SEGS);
570 free(entry->buflist, DRM_MEM_BUFS);
574 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
575 sizeof(*dma->pagelist));
577 DRM_DEBUG("pagelist: %d entries\n",
578 dma->page_count + (count << page_order));
580 entry->buf_size = size;
581 entry->page_order = page_order;
585 while (entry->buf_count < count) {
586 DRM_SPINUNLOCK(&dev->dma_lock);
587 drm_dma_handle_t *dmah = drm_pci_alloc(dev, size, alignment,
589 DRM_SPINLOCK(&dev->dma_lock);
591 /* Set count correctly so we free the proper amount. */
592 entry->buf_count = count;
593 entry->seg_count = count;
594 drm_cleanup_buf_error(dev, entry);
595 free(temp_pagelist, DRM_MEM_PAGES);
599 entry->seglist[entry->seg_count++] = dmah;
600 for (i = 0; i < (1 << page_order); i++) {
601 DRM_DEBUG("page %d @ %p\n",
602 dma->page_count + page_count,
603 (char *)dmah->vaddr + PAGE_SIZE * i);
604 temp_pagelist[dma->page_count + page_count++] =
605 (long)dmah->vaddr + PAGE_SIZE * i;
608 offset + size <= total && entry->buf_count < count;
609 offset += alignment, ++entry->buf_count) {
610 buf = &entry->buflist[entry->buf_count];
611 buf->idx = dma->buf_count + entry->buf_count;
612 buf->total = alignment;
615 buf->offset = (dma->byte_count + byte_count + offset);
616 buf->address = ((char *)dmah->vaddr + offset);
617 buf->bus_address = dmah->busaddr + offset;
620 buf->file_priv = NULL;
622 buf->dev_priv_size = dev->driver->buf_priv_size;
623 buf->dev_private = malloc(buf->dev_priv_size,
624 DRM_MEM_BUFS, M_NOWAIT | M_ZERO);
625 if (buf->dev_private == NULL) {
626 /* Set count correctly so we free the proper amount. */
627 entry->buf_count = count;
628 entry->seg_count = count;
629 drm_cleanup_buf_error(dev, entry);
630 free(temp_pagelist, DRM_MEM_PAGES);
634 DRM_DEBUG("buffer %d @ %p\n",
635 entry->buf_count, buf->address);
637 byte_count += PAGE_SIZE << page_order;
640 temp_buflist = realloc(dma->buflist,
641 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
642 DRM_MEM_BUFS, M_NOWAIT);
643 if (temp_buflist == NULL) {
644 /* Free the entry because it isn't valid */
645 drm_cleanup_buf_error(dev, entry);
646 free(temp_pagelist, DRM_MEM_PAGES);
649 dma->buflist = temp_buflist;
651 for (i = 0; i < entry->buf_count; i++) {
652 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
655 /* No allocations failed, so now we can replace the orginal pagelist
658 free(dma->pagelist, DRM_MEM_PAGES);
659 dma->pagelist = temp_pagelist;
661 dma->buf_count += entry->buf_count;
662 dma->seg_count += entry->seg_count;
663 dma->page_count += entry->seg_count << page_order;
664 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
666 request->count = entry->buf_count;
667 request->size = size;
673 static int drm_do_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
675 drm_device_dma_t *dma = dev->dma;
676 drm_buf_entry_t *entry;
678 unsigned long offset;
679 unsigned long agp_offset;
688 drm_buf_t **temp_buflist;
690 count = request->count;
691 order = drm_order(request->size);
694 alignment = (request->flags & _DRM_PAGE_ALIGN)
695 ? round_page(size) : size;
696 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
697 total = PAGE_SIZE << page_order;
700 agp_offset = request->agp_start;
702 DRM_DEBUG("count: %d\n", count);
703 DRM_DEBUG("order: %d\n", order);
704 DRM_DEBUG("size: %d\n", size);
705 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
706 DRM_DEBUG("alignment: %d\n", alignment);
707 DRM_DEBUG("page_order: %d\n", page_order);
708 DRM_DEBUG("total: %d\n", total);
710 entry = &dma->bufs[order];
712 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
714 if (entry->buflist == NULL)
717 entry->buf_size = size;
718 entry->page_order = page_order;
722 while (entry->buf_count < count) {
723 buf = &entry->buflist[entry->buf_count];
724 buf->idx = dma->buf_count + entry->buf_count;
725 buf->total = alignment;
729 buf->offset = (dma->byte_count + offset);
730 buf->bus_address = agp_offset + offset;
731 buf->address = (void *)(agp_offset + offset + dev->sg->handle);
734 buf->file_priv = NULL;
736 buf->dev_priv_size = dev->driver->buf_priv_size;
737 buf->dev_private = malloc(buf->dev_priv_size, DRM_MEM_BUFS,
739 if (buf->dev_private == NULL) {
740 /* Set count correctly so we free the proper amount. */
741 entry->buf_count = count;
742 drm_cleanup_buf_error(dev, entry);
746 DRM_DEBUG("buffer %d @ %p\n",
747 entry->buf_count, buf->address);
751 byte_count += PAGE_SIZE << page_order;
754 DRM_DEBUG("byte_count: %d\n", byte_count);
756 temp_buflist = realloc(dma->buflist,
757 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
758 DRM_MEM_BUFS, M_NOWAIT);
759 if (temp_buflist == NULL) {
760 /* Free the entry because it isn't valid */
761 drm_cleanup_buf_error(dev, entry);
764 dma->buflist = temp_buflist;
766 for (i = 0; i < entry->buf_count; i++) {
767 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
770 dma->buf_count += entry->buf_count;
771 dma->byte_count += byte_count;
773 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
774 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
776 request->count = entry->buf_count;
777 request->size = size;
779 dma->flags = _DRM_DMA_USE_SG;
784 int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
788 if (request->count < 0 || request->count > 4096)
791 order = drm_order(request->size);
792 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
795 DRM_SPINLOCK(&dev->dma_lock);
797 /* No more allocations after first buffer-using ioctl. */
798 if (dev->buf_use != 0) {
799 DRM_SPINUNLOCK(&dev->dma_lock);
802 /* No more than one allocation per order */
803 if (dev->dma->bufs[order].buf_count != 0) {
804 DRM_SPINUNLOCK(&dev->dma_lock);
808 ret = drm_do_addbufs_agp(dev, request);
810 DRM_SPINUNLOCK(&dev->dma_lock);
815 int drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
819 if (!DRM_SUSER(DRM_CURPROC))
822 if (request->count < 0 || request->count > 4096)
825 order = drm_order(request->size);
826 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
829 DRM_SPINLOCK(&dev->dma_lock);
831 /* No more allocations after first buffer-using ioctl. */
832 if (dev->buf_use != 0) {
833 DRM_SPINUNLOCK(&dev->dma_lock);
836 /* No more than one allocation per order */
837 if (dev->dma->bufs[order].buf_count != 0) {
838 DRM_SPINUNLOCK(&dev->dma_lock);
842 ret = drm_do_addbufs_sg(dev, request);
844 DRM_SPINUNLOCK(&dev->dma_lock);
849 int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
853 if (!DRM_SUSER(DRM_CURPROC))
856 if (request->count < 0 || request->count > 4096)
859 order = drm_order(request->size);
860 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
863 DRM_SPINLOCK(&dev->dma_lock);
865 /* No more allocations after first buffer-using ioctl. */
866 if (dev->buf_use != 0) {
867 DRM_SPINUNLOCK(&dev->dma_lock);
870 /* No more than one allocation per order */
871 if (dev->dma->bufs[order].buf_count != 0) {
872 DRM_SPINUNLOCK(&dev->dma_lock);
876 ret = drm_do_addbufs_pci(dev, request);
878 DRM_SPINUNLOCK(&dev->dma_lock);
883 int drm_addbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
885 struct drm_buf_desc *request = data;
888 if (request->flags & _DRM_AGP_BUFFER)
889 err = drm_addbufs_agp(dev, request);
890 else if (request->flags & _DRM_SG_BUFFER)
891 err = drm_addbufs_sg(dev, request);
893 err = drm_addbufs_pci(dev, request);
898 int drm_infobufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
900 drm_device_dma_t *dma = dev->dma;
901 struct drm_buf_info *request = data;
906 DRM_SPINLOCK(&dev->dma_lock);
907 ++dev->buf_use; /* Can't allocate more after this call */
908 DRM_SPINUNLOCK(&dev->dma_lock);
910 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
911 if (dma->bufs[i].buf_count)
915 DRM_DEBUG("count = %d\n", count);
917 if (request->count >= count) {
918 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
919 if (dma->bufs[i].buf_count) {
920 struct drm_buf_desc from;
922 from.count = dma->bufs[i].buf_count;
923 from.size = dma->bufs[i].buf_size;
924 from.low_mark = dma->bufs[i].freelist.low_mark;
925 from.high_mark = dma->bufs[i].freelist.high_mark;
927 if (DRM_COPY_TO_USER(&request->list[count], &from,
928 sizeof(struct drm_buf_desc)) != 0) {
933 DRM_DEBUG("%d %d %d %d %d\n",
934 i, dma->bufs[i].buf_count,
935 dma->bufs[i].buf_size,
936 dma->bufs[i].freelist.low_mark,
937 dma->bufs[i].freelist.high_mark);
942 request->count = count;
947 int drm_markbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
949 drm_device_dma_t *dma = dev->dma;
950 struct drm_buf_desc *request = data;
953 DRM_DEBUG("%d, %d, %d\n",
954 request->size, request->low_mark, request->high_mark);
957 order = drm_order(request->size);
958 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
959 request->low_mark < 0 || request->high_mark < 0) {
963 DRM_SPINLOCK(&dev->dma_lock);
964 if (request->low_mark > dma->bufs[order].buf_count ||
965 request->high_mark > dma->bufs[order].buf_count) {
966 DRM_SPINUNLOCK(&dev->dma_lock);
970 dma->bufs[order].freelist.low_mark = request->low_mark;
971 dma->bufs[order].freelist.high_mark = request->high_mark;
972 DRM_SPINUNLOCK(&dev->dma_lock);
977 int drm_freebufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
979 drm_device_dma_t *dma = dev->dma;
980 struct drm_buf_free *request = data;
986 DRM_DEBUG("%d\n", request->count);
988 DRM_SPINLOCK(&dev->dma_lock);
989 for (i = 0; i < request->count; i++) {
990 if (DRM_COPY_FROM_USER(&idx, &request->list[i], sizeof(idx))) {
994 if (idx < 0 || idx >= dma->buf_count) {
995 DRM_ERROR("Index %d (of %d max)\n",
996 idx, dma->buf_count - 1);
1000 buf = dma->buflist[idx];
1001 if (buf->file_priv != file_priv) {
1002 DRM_ERROR("Process %d freeing buffer not owned\n",
1007 drm_free_buffer(dev, buf);
1009 DRM_SPINUNLOCK(&dev->dma_lock);
1014 int drm_mapbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
1016 drm_device_dma_t *dma = dev->dma;
1019 vm_offset_t address;
1020 struct vmspace *vms;
1024 struct drm_buf_map *request = data;
1027 vms = DRM_CURPROC->td_proc->p_vmspace;
1029 DRM_SPINLOCK(&dev->dma_lock);
1030 dev->buf_use++; /* Can't allocate more after this call */
1031 DRM_SPINUNLOCK(&dev->dma_lock);
1033 if (request->count < dma->buf_count)
1036 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
1037 (drm_core_check_feature(dev, DRIVER_SG) &&
1038 (dma->flags & _DRM_DMA_USE_SG))) {
1039 drm_local_map_t *map = dev->agp_buffer_map;
1045 size = round_page(map->size);
1048 size = round_page(dma->byte_count),
1052 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
1053 #if __FreeBSD_version >= 600023
1054 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1055 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC, OBJT_DEVICE,
1056 dev->devnode, foff);
1058 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1059 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC,
1060 SLIST_FIRST(&dev->devnode->si_hlist), foff);
1065 request->virtual = (void *)vaddr;
1067 for (i = 0; i < dma->buf_count; i++) {
1068 if (DRM_COPY_TO_USER(&request->list[i].idx,
1069 &dma->buflist[i]->idx, sizeof(request->list[0].idx))) {
1073 if (DRM_COPY_TO_USER(&request->list[i].total,
1074 &dma->buflist[i]->total, sizeof(request->list[0].total))) {
1078 if (DRM_COPY_TO_USER(&request->list[i].used, &zero,
1083 address = vaddr + dma->buflist[i]->offset; /* *** */
1084 if (DRM_COPY_TO_USER(&request->list[i].address, &address,
1092 request->count = dma->buf_count;
1094 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1100 * Compute order. Can be made faster.
1102 int drm_order(unsigned long size)
1109 order = flsl(size) - 1;
1110 if (size & ~(1ul << order))