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>
29 * $FreeBSD: src/sys/dev/drm2/drm_bufs.c,v 1.1 2012/05/22 11:07:44 kib Exp $
33 * Implementation of the ioctls for setup of DRM mappings and DMA buffers.
37 #include <bus/pci/pcireg.h>
41 /* Allocation of PCI memory resources (framebuffer, registers, etc.) for
42 * drm_get_resource_*. Note that they are not RF_ACTIVE, so there's no virtual
43 * address for accessing them. Cleaned up at unload.
45 static int drm_alloc_resource(struct drm_device *dev, int resource)
52 if (resource >= DRM_MAX_PCI_RESOURCE) {
53 DRM_ERROR("Resource %d too large\n", resource);
57 if (dev->pcir[resource] != NULL) {
62 rid = PCIR_BAR(resource);
63 res = bus_alloc_resource_any(dev->device, SYS_RES_MEMORY, &rid,
67 DRM_ERROR("Couldn't find resource 0x%x\n", resource);
71 if (dev->pcir[resource] == NULL) {
72 dev->pcirid[resource] = rid;
73 dev->pcir[resource] = res;
79 unsigned long drm_get_resource_start(struct drm_device *dev,
80 unsigned int resource)
82 if (drm_alloc_resource(dev, resource) != 0)
85 return rman_get_start(dev->pcir[resource]);
88 unsigned long drm_get_resource_len(struct drm_device *dev,
89 unsigned int resource)
91 if (drm_alloc_resource(dev, resource) != 0)
94 return rman_get_size(dev->pcir[resource]);
97 int drm_addmap(struct drm_device * dev, unsigned long offset,
99 enum drm_map_type type, enum drm_map_flags flags, drm_local_map_t **map_ptr)
101 drm_local_map_t *map;
103 /*drm_agp_mem_t *entry;
106 /* Only allow shared memory to be removable since we only keep enough
107 * book keeping information about shared memory to allow for removal
108 * when processes fork.
110 if ((flags & _DRM_REMOVABLE) && type != _DRM_SHM) {
111 DRM_ERROR("Requested removable map for non-DRM_SHM\n");
114 if ((offset & PAGE_MASK) || (size & PAGE_MASK)) {
115 DRM_ERROR("offset/size not page aligned: 0x%lx/0x%lx\n",
119 if (offset + size < offset) {
120 DRM_ERROR("offset and size wrap around: 0x%lx/0x%lx\n",
125 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n", offset,
128 /* Check if this is just another version of a kernel-allocated map, and
129 * just hand that back if so.
131 if (type == _DRM_REGISTERS || type == _DRM_FRAME_BUFFER ||
133 TAILQ_FOREACH(map, &dev->maplist, link) {
134 if (map->type == type && (map->offset == offset ||
135 (map->type == _DRM_SHM &&
136 map->flags == _DRM_CONTAINS_LOCK))) {
138 DRM_DEBUG("Found kernel map %d\n", type);
145 /* Allocate a new map structure, fill it in, and do any type-specific
146 * initialization necessary.
148 map = kmalloc(sizeof(*map), DRM_MEM_MAPS, M_ZERO | M_NOWAIT);
154 map->offset = offset;
158 map->handle = (void *)((unsigned long)alloc_unr(dev->map_unrhdr) <<
159 DRM_MAP_HANDLE_SHIFT);
163 map->virtual = drm_ioremap(dev, map);
164 if (!(map->flags & _DRM_WRITE_COMBINING))
167 case _DRM_FRAME_BUFFER:
168 if (drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC) == 0)
172 map->virtual = kmalloc(map->size, DRM_MEM_MAPS, M_NOWAIT);
173 DRM_DEBUG("%lu %d %p\n",
174 map->size, drm_order(map->size), map->virtual);
176 drm_free(map, DRM_MEM_MAPS);
180 map->offset = (unsigned long)map->virtual;
181 if (map->flags & _DRM_CONTAINS_LOCK) {
182 /* Prevent a 2nd X Server from creating a 2nd lock */
184 if (dev->lock.hw_lock != NULL) {
186 drm_free(map->virtual, DRM_MEM_MAPS);
187 drm_free(map, DRM_MEM_MAPS);
190 dev->lock.hw_lock = map->virtual; /* Pointer to lock */
196 /* In some cases (i810 driver), user space may have already
197 * added the AGP base itself, because dev->agp->base previously
198 * only got set during AGP enable. So, only add the base
199 * address if the map's offset isn't already within the
202 if (map->offset < dev->agp->base ||
203 map->offset > dev->agp->base +
204 dev->agp->info.ai_aperture_size - 1) {
205 map->offset += dev->agp->base;
207 map->mtrr = dev->agp->mtrr; /* for getmap */
208 /*for (entry = dev->agp->memory; entry; entry = entry->next) {
209 if ((map->offset >= entry->bound) &&
210 (map->offset + map->size <=
211 entry->bound + entry->pages * PAGE_SIZE)) {
217 drm_free(map, DRM_MEM_MAPS);
222 case _DRM_SCATTER_GATHER:
224 drm_free(map, DRM_MEM_MAPS);
228 map->virtual = (void *)(dev->sg->vaddr + offset);
229 map->offset = dev->sg->vaddr + offset;
231 case _DRM_CONSISTENT:
232 /* Unfortunately, we don't get any alignment specification from
233 * the caller, so we have to guess. drm_pci_alloc requires
234 * a power-of-two alignment, so try to align the bus address of
235 * the map to it size if possible, otherwise just assume
236 * PAGE_SIZE alignment.
239 if ((align & (align - 1)) != 0)
241 map->dmah = drm_pci_alloc(dev, map->size, align, 0xfffffffful);
242 if (map->dmah == NULL) {
243 drm_free(map, DRM_MEM_MAPS);
247 map->virtual = map->dmah->vaddr;
248 map->offset = map->dmah->busaddr;
251 DRM_ERROR("Bad map type %d\n", map->type);
252 drm_free(map, DRM_MEM_MAPS);
258 TAILQ_INSERT_TAIL(&dev->maplist, map, link);
261 /* Jumped to, with lock held, when a kernel map is found. */
263 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", map->type, map->offset,
271 int drm_addmap_ioctl(struct drm_device *dev, void *data,
272 struct drm_file *file_priv)
274 struct drm_map *request = data;
275 drm_local_map_t *map;
278 if (!(dev->flags & (FREAD|FWRITE)))
279 return EACCES; /* Require read/write */
281 if (!DRM_SUSER(DRM_CURPROC) && request->type != _DRM_AGP)
285 err = drm_addmap(dev, request->offset, request->size, request->type,
286 request->flags, &map);
291 request->offset = map->offset;
292 request->size = map->size;
293 request->type = map->type;
294 request->flags = map->flags;
295 request->mtrr = map->mtrr;
296 request->handle = (void *)map->handle;
301 void drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
303 DRM_LOCK_ASSERT(dev);
308 TAILQ_REMOVE(&dev->maplist, map, link);
312 if (map->bsr == NULL)
313 drm_ioremapfree(map);
315 case _DRM_FRAME_BUFFER:
317 int __unused retcode;
319 retcode = drm_mtrr_del(0, map->offset, map->size,
321 DRM_DEBUG("mtrr_del = %d\n", retcode);
325 drm_free(map->virtual, DRM_MEM_MAPS);
328 case _DRM_SCATTER_GATHER:
330 case _DRM_CONSISTENT:
331 drm_pci_free(dev, map->dmah);
334 DRM_ERROR("Bad map type %d\n", map->type);
338 if (map->bsr != NULL) {
339 bus_release_resource(dev->device, SYS_RES_MEMORY, map->rid,
345 free_unr(dev->map_unrhdr, (unsigned long)map->handle >>
346 DRM_MAP_HANDLE_SHIFT);
349 drm_free(map, DRM_MEM_MAPS);
352 /* Remove a map private from list and deallocate resources if the mapping
356 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
357 struct drm_file *file_priv)
359 drm_local_map_t *map;
360 struct drm_map *request = data;
363 TAILQ_FOREACH(map, &dev->maplist, link) {
364 if (map->handle == request->handle &&
365 map->flags & _DRM_REMOVABLE)
369 /* No match found. */
383 static void drm_cleanup_buf_error(struct drm_device *dev,
384 drm_buf_entry_t *entry)
388 if (entry->seg_count) {
389 for (i = 0; i < entry->seg_count; i++) {
390 drm_pci_free(dev, entry->seglist[i]);
392 drm_free(entry->seglist, DRM_MEM_SEGS);
394 entry->seg_count = 0;
397 if (entry->buf_count) {
398 for (i = 0; i < entry->buf_count; i++) {
399 drm_free(entry->buflist[i].dev_private, DRM_MEM_BUFS);
401 drm_free(entry->buflist, DRM_MEM_BUFS);
403 entry->buf_count = 0;
407 static int drm_do_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
409 drm_device_dma_t *dma = dev->dma;
410 drm_buf_entry_t *entry;
411 /*drm_agp_mem_t *agp_entry;
414 unsigned long offset;
415 unsigned long agp_offset;
424 drm_buf_t **temp_buflist;
426 count = request->count;
427 order = drm_order(request->size);
430 alignment = (request->flags & _DRM_PAGE_ALIGN)
431 ? round_page(size) : size;
432 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
433 total = PAGE_SIZE << page_order;
436 agp_offset = dev->agp->base + request->agp_start;
438 DRM_DEBUG("count: %d\n", count);
439 DRM_DEBUG("order: %d\n", order);
440 DRM_DEBUG("size: %d\n", size);
441 DRM_DEBUG("agp_offset: 0x%lx\n", agp_offset);
442 DRM_DEBUG("alignment: %d\n", alignment);
443 DRM_DEBUG("page_order: %d\n", page_order);
444 DRM_DEBUG("total: %d\n", total);
446 /* Make sure buffers are located in AGP memory that we own */
447 /* Breaks MGA due to drm_alloc_agp not setting up entries for the
448 * memory. Safe to ignore for now because these ioctls are still
452 for (agp_entry = dev->agp->memory; agp_entry;
453 agp_entry = agp_entry->next) {
454 if ((agp_offset >= agp_entry->bound) &&
455 (agp_offset + total * count <=
456 agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
462 DRM_DEBUG("zone invalid\n");
466 entry = &dma->bufs[order];
468 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
470 if (!entry->buflist) {
474 entry->buf_size = size;
475 entry->page_order = page_order;
479 while (entry->buf_count < count) {
480 buf = &entry->buflist[entry->buf_count];
481 buf->idx = dma->buf_count + entry->buf_count;
482 buf->total = alignment;
486 buf->offset = (dma->byte_count + offset);
487 buf->bus_address = agp_offset + offset;
488 buf->address = (void *)(agp_offset + offset);
491 buf->file_priv = NULL;
493 buf->dev_priv_size = dev->driver->buf_priv_size;
494 buf->dev_private = kmalloc(buf->dev_priv_size, DRM_MEM_BUFS,
496 if (buf->dev_private == NULL) {
497 /* Set count correctly so we free the proper amount. */
498 entry->buf_count = count;
499 drm_cleanup_buf_error(dev, entry);
505 byte_count += PAGE_SIZE << page_order;
508 DRM_DEBUG("byte_count: %d\n", byte_count);
510 temp_buflist = krealloc(dma->buflist,
511 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
512 DRM_MEM_BUFS, M_NOWAIT);
513 if (temp_buflist == NULL) {
514 /* Free the entry because it isn't valid */
515 drm_cleanup_buf_error(dev, entry);
518 dma->buflist = temp_buflist;
520 for (i = 0; i < entry->buf_count; i++) {
521 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
524 dma->buf_count += entry->buf_count;
525 dma->byte_count += byte_count;
527 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
528 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
530 request->count = entry->buf_count;
531 request->size = size;
533 dma->flags = _DRM_DMA_USE_AGP;
538 static int drm_do_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
540 drm_device_dma_t *dma = dev->dma;
546 drm_buf_entry_t *entry;
549 unsigned long offset;
553 unsigned long *temp_pagelist;
554 drm_buf_t **temp_buflist;
556 count = request->count;
557 order = drm_order(request->size);
560 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
561 request->count, request->size, size, order);
563 alignment = (request->flags & _DRM_PAGE_ALIGN)
564 ? round_page(size) : size;
565 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
566 total = PAGE_SIZE << page_order;
568 entry = &dma->bufs[order];
570 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
572 entry->seglist = kmalloc(count * sizeof(*entry->seglist), DRM_MEM_SEGS,
575 /* Keep the original pagelist until we know all the allocations
578 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
579 sizeof(*dma->pagelist), DRM_MEM_PAGES, M_NOWAIT);
581 if (entry->buflist == NULL || entry->seglist == NULL ||
582 temp_pagelist == NULL) {
583 drm_free(temp_pagelist, DRM_MEM_PAGES);
584 drm_free(entry->seglist, DRM_MEM_SEGS);
585 drm_free(entry->buflist, DRM_MEM_BUFS);
589 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
590 sizeof(*dma->pagelist));
592 DRM_DEBUG("pagelist: %d entries\n",
593 dma->page_count + (count << page_order));
595 entry->buf_size = size;
596 entry->page_order = page_order;
600 while (entry->buf_count < count) {
601 spin_unlock(&dev->dma_lock);
602 drm_dma_handle_t *dmah = drm_pci_alloc(dev, size, alignment,
604 spin_lock(&dev->dma_lock);
606 /* Set count correctly so we free the proper amount. */
607 entry->buf_count = count;
608 entry->seg_count = count;
609 drm_cleanup_buf_error(dev, entry);
610 drm_free(temp_pagelist, DRM_MEM_PAGES);
614 entry->seglist[entry->seg_count++] = dmah;
615 for (i = 0; i < (1 << page_order); i++) {
616 DRM_DEBUG("page %d @ %p\n",
617 dma->page_count + page_count,
618 (char *)dmah->vaddr + PAGE_SIZE * i);
619 temp_pagelist[dma->page_count + page_count++] =
620 (long)dmah->vaddr + PAGE_SIZE * i;
623 offset + size <= total && entry->buf_count < count;
624 offset += alignment, ++entry->buf_count) {
625 buf = &entry->buflist[entry->buf_count];
626 buf->idx = dma->buf_count + entry->buf_count;
627 buf->total = alignment;
630 buf->offset = (dma->byte_count + byte_count + offset);
631 buf->address = ((char *)dmah->vaddr + offset);
632 buf->bus_address = dmah->busaddr + offset;
635 buf->file_priv = NULL;
637 buf->dev_priv_size = dev->driver->buf_priv_size;
638 buf->dev_private = kmalloc(buf->dev_priv_size,
639 DRM_MEM_BUFS, M_NOWAIT | M_ZERO);
640 if (buf->dev_private == NULL) {
641 /* Set count correctly so we free the proper amount. */
642 entry->buf_count = count;
643 entry->seg_count = count;
644 drm_cleanup_buf_error(dev, entry);
645 drm_free(temp_pagelist, DRM_MEM_PAGES);
649 DRM_DEBUG("buffer %d @ %p\n",
650 entry->buf_count, buf->address);
652 byte_count += PAGE_SIZE << page_order;
655 temp_buflist = krealloc(dma->buflist,
656 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
657 DRM_MEM_BUFS, M_NOWAIT);
658 if (temp_buflist == NULL) {
659 /* Free the entry because it isn't valid */
660 drm_cleanup_buf_error(dev, entry);
661 drm_free(temp_pagelist, DRM_MEM_PAGES);
664 dma->buflist = temp_buflist;
666 for (i = 0; i < entry->buf_count; i++) {
667 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
670 /* No allocations failed, so now we can replace the orginal pagelist
673 drm_free(dma->pagelist, DRM_MEM_PAGES);
674 dma->pagelist = temp_pagelist;
676 dma->buf_count += entry->buf_count;
677 dma->seg_count += entry->seg_count;
678 dma->page_count += entry->seg_count << page_order;
679 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
681 request->count = entry->buf_count;
682 request->size = size;
688 static int drm_do_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
690 drm_device_dma_t *dma = dev->dma;
691 drm_buf_entry_t *entry;
693 unsigned long offset;
694 unsigned long agp_offset;
703 drm_buf_t **temp_buflist;
705 count = request->count;
706 order = drm_order(request->size);
709 alignment = (request->flags & _DRM_PAGE_ALIGN)
710 ? round_page(size) : size;
711 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
712 total = PAGE_SIZE << page_order;
715 agp_offset = request->agp_start;
717 DRM_DEBUG("count: %d\n", count);
718 DRM_DEBUG("order: %d\n", order);
719 DRM_DEBUG("size: %d\n", size);
720 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
721 DRM_DEBUG("alignment: %d\n", alignment);
722 DRM_DEBUG("page_order: %d\n", page_order);
723 DRM_DEBUG("total: %d\n", total);
725 entry = &dma->bufs[order];
727 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
729 if (entry->buflist == NULL)
732 entry->buf_size = size;
733 entry->page_order = page_order;
737 while (entry->buf_count < count) {
738 buf = &entry->buflist[entry->buf_count];
739 buf->idx = dma->buf_count + entry->buf_count;
740 buf->total = alignment;
744 buf->offset = (dma->byte_count + offset);
745 buf->bus_address = agp_offset + offset;
746 buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
749 buf->file_priv = NULL;
751 buf->dev_priv_size = dev->driver->buf_priv_size;
752 buf->dev_private = kmalloc(buf->dev_priv_size, DRM_MEM_BUFS,
754 if (buf->dev_private == NULL) {
755 /* Set count correctly so we free the proper amount. */
756 entry->buf_count = count;
757 drm_cleanup_buf_error(dev, entry);
761 DRM_DEBUG("buffer %d @ %p\n",
762 entry->buf_count, buf->address);
766 byte_count += PAGE_SIZE << page_order;
769 DRM_DEBUG("byte_count: %d\n", byte_count);
771 temp_buflist = krealloc(dma->buflist,
772 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
773 DRM_MEM_BUFS, M_NOWAIT);
774 if (temp_buflist == NULL) {
775 /* Free the entry because it isn't valid */
776 drm_cleanup_buf_error(dev, entry);
779 dma->buflist = temp_buflist;
781 for (i = 0; i < entry->buf_count; i++) {
782 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
785 dma->buf_count += entry->buf_count;
786 dma->byte_count += byte_count;
788 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
789 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
791 request->count = entry->buf_count;
792 request->size = size;
794 dma->flags = _DRM_DMA_USE_SG;
799 int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
803 if (request->count < 0 || request->count > 4096)
806 order = drm_order(request->size);
807 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
810 spin_lock(&dev->dma_lock);
812 /* No more allocations after first buffer-using ioctl. */
813 if (dev->buf_use != 0) {
814 spin_unlock(&dev->dma_lock);
817 /* No more than one allocation per order */
818 if (dev->dma->bufs[order].buf_count != 0) {
819 spin_unlock(&dev->dma_lock);
823 ret = drm_do_addbufs_agp(dev, request);
825 spin_unlock(&dev->dma_lock);
830 int drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
834 if (!DRM_SUSER(DRM_CURPROC))
837 if (request->count < 0 || request->count > 4096)
840 order = drm_order(request->size);
841 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
844 spin_lock(&dev->dma_lock);
846 /* No more allocations after first buffer-using ioctl. */
847 if (dev->buf_use != 0) {
848 spin_unlock(&dev->dma_lock);
851 /* No more than one allocation per order */
852 if (dev->dma->bufs[order].buf_count != 0) {
853 spin_unlock(&dev->dma_lock);
857 ret = drm_do_addbufs_sg(dev, request);
859 spin_unlock(&dev->dma_lock);
864 int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
868 if (!DRM_SUSER(DRM_CURPROC))
871 if (request->count < 0 || request->count > 4096)
874 order = drm_order(request->size);
875 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
878 spin_lock(&dev->dma_lock);
880 /* No more allocations after first buffer-using ioctl. */
881 if (dev->buf_use != 0) {
882 spin_unlock(&dev->dma_lock);
885 /* No more than one allocation per order */
886 if (dev->dma->bufs[order].buf_count != 0) {
887 spin_unlock(&dev->dma_lock);
891 ret = drm_do_addbufs_pci(dev, request);
893 spin_unlock(&dev->dma_lock);
898 int drm_addbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
900 struct drm_buf_desc *request = data;
903 if (request->flags & _DRM_AGP_BUFFER)
904 err = drm_addbufs_agp(dev, request);
905 else if (request->flags & _DRM_SG_BUFFER)
906 err = drm_addbufs_sg(dev, request);
908 err = drm_addbufs_pci(dev, request);
913 int drm_infobufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
915 drm_device_dma_t *dma = dev->dma;
916 struct drm_buf_info *request = data;
921 spin_lock(&dev->dma_lock);
922 ++dev->buf_use; /* Can't allocate more after this call */
923 spin_unlock(&dev->dma_lock);
925 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
926 if (dma->bufs[i].buf_count)
930 DRM_DEBUG("count = %d\n", count);
932 if (request->count >= count) {
933 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
934 if (dma->bufs[i].buf_count) {
935 struct drm_buf_desc from;
937 from.count = dma->bufs[i].buf_count;
938 from.size = dma->bufs[i].buf_size;
939 from.low_mark = dma->bufs[i].freelist.low_mark;
940 from.high_mark = dma->bufs[i].freelist.high_mark;
942 if (DRM_COPY_TO_USER(&request->list[count], &from,
943 sizeof(struct drm_buf_desc)) != 0) {
948 DRM_DEBUG("%d %d %d %d %d\n",
949 i, dma->bufs[i].buf_count,
950 dma->bufs[i].buf_size,
951 dma->bufs[i].freelist.low_mark,
952 dma->bufs[i].freelist.high_mark);
957 request->count = count;
962 int drm_markbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
964 drm_device_dma_t *dma = dev->dma;
965 struct drm_buf_desc *request = data;
968 DRM_DEBUG("%d, %d, %d\n",
969 request->size, request->low_mark, request->high_mark);
972 order = drm_order(request->size);
973 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
974 request->low_mark < 0 || request->high_mark < 0) {
978 spin_lock(&dev->dma_lock);
979 if (request->low_mark > dma->bufs[order].buf_count ||
980 request->high_mark > dma->bufs[order].buf_count) {
981 spin_unlock(&dev->dma_lock);
985 dma->bufs[order].freelist.low_mark = request->low_mark;
986 dma->bufs[order].freelist.high_mark = request->high_mark;
987 spin_unlock(&dev->dma_lock);
992 int drm_freebufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
994 drm_device_dma_t *dma = dev->dma;
995 struct drm_buf_free *request = data;
1001 DRM_DEBUG("%d\n", request->count);
1003 spin_lock(&dev->dma_lock);
1004 for (i = 0; i < request->count; i++) {
1005 if (DRM_COPY_FROM_USER(&idx, &request->list[i], sizeof(idx))) {
1009 if (idx < 0 || idx >= dma->buf_count) {
1010 DRM_ERROR("Index %d (of %d max)\n",
1011 idx, dma->buf_count - 1);
1015 buf = dma->buflist[idx];
1016 if (buf->file_priv != file_priv) {
1017 DRM_ERROR("Process %d freeing buffer not owned\n",
1022 drm_free_buffer(dev, buf);
1024 spin_unlock(&dev->dma_lock);
1029 int drm_mapbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
1031 drm_device_dma_t *dma = dev->dma;
1034 vm_offset_t address;
1035 struct vmspace *vms;
1039 struct drm_buf_map *request = data;
1042 vms = DRM_CURPROC->td_proc->p_vmspace;
1044 spin_lock(&dev->dma_lock);
1045 dev->buf_use++; /* Can't allocate more after this call */
1046 spin_unlock(&dev->dma_lock);
1048 if (request->count < dma->buf_count)
1051 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
1052 (drm_core_check_feature(dev, DRIVER_SG) &&
1053 (dma->flags & _DRM_DMA_USE_SG))) {
1054 drm_local_map_t *map = dev->agp_buffer_map;
1060 size = round_page(map->size);
1061 foff = (unsigned long)map->handle;
1063 size = round_page(dma->byte_count),
1067 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
1068 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1069 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC,
1070 SLIST_FIRST(&dev->devnode->si_hlist), foff);
1074 request->virtual = (void *)vaddr;
1076 for (i = 0; i < dma->buf_count; i++) {
1077 if (DRM_COPY_TO_USER(&request->list[i].idx,
1078 &dma->buflist[i]->idx, sizeof(request->list[0].idx))) {
1082 if (DRM_COPY_TO_USER(&request->list[i].total,
1083 &dma->buflist[i]->total, sizeof(request->list[0].total))) {
1087 if (DRM_COPY_TO_USER(&request->list[i].used, &zero,
1092 address = vaddr + dma->buflist[i]->offset; /* *** */
1093 if (DRM_COPY_TO_USER(&request->list[i].address, &address,
1101 request->count = dma->buf_count;
1103 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1109 * Compute order. Can be made faster.
1111 int drm_order(unsigned long size)
1118 order = flsl(size) - 1;
1119 if (size & ~(1ul << order))