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->dev, 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 struct drm_local_map *map;
102 struct drm_map_list *entry = NULL;
105 /* Allocate a new map structure, fill it in, and do any type-specific
106 * initialization necessary.
108 map = kmalloc(sizeof(*map), DRM_MEM_MAPS, M_ZERO | M_NOWAIT);
113 map->offset = offset;
117 map->handle = (void *)((unsigned long)alloc_unr(dev->map_unrhdr) <<
118 DRM_MAP_HANDLE_SHIFT);
120 /* Only allow shared memory to be removable since we only keep enough
121 * book keeping information about shared memory to allow for removal
122 * when processes fork.
124 if ((flags & _DRM_REMOVABLE) && type != _DRM_SHM) {
125 DRM_ERROR("Requested removable map for non-DRM_SHM\n");
126 drm_free(map, DRM_MEM_MAPS);
129 if ((offset & PAGE_MASK) || (size & PAGE_MASK)) {
130 DRM_ERROR("offset/size not page aligned: 0x%lx/0x%lx\n",
132 drm_free(map, DRM_MEM_MAPS);
135 if (offset + size < offset) {
136 DRM_ERROR("offset and size wrap around: 0x%lx/0x%lx\n",
138 drm_free(map, DRM_MEM_MAPS);
142 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n", offset,
145 /* Check if this is just another version of a kernel-allocated map, and
146 * just hand that back if so.
148 if (type == _DRM_REGISTERS || type == _DRM_FRAME_BUFFER ||
150 list_for_each_entry(entry, &dev->maplist, head) {
151 if (entry->map->type == type && (entry->map->offset == offset ||
152 (entry->map->type == _DRM_SHM &&
153 entry->map->flags == _DRM_CONTAINS_LOCK))) {
154 entry->map->size = size;
155 DRM_DEBUG("Found kernel map %d\n", type);
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);
179 map->offset = (unsigned long)map->virtual;
180 if (map->flags & _DRM_CONTAINS_LOCK) {
181 /* Prevent a 2nd X Server from creating a 2nd lock */
183 if (dev->lock.hw_lock != NULL) {
185 drm_free(map->virtual, DRM_MEM_MAPS);
186 drm_free(map, DRM_MEM_MAPS);
189 dev->lock.hw_lock = map->virtual; /* Pointer to lock */
195 /* In some cases (i810 driver), user space may have already
196 * added the AGP base itself, because dev->agp->base previously
197 * only got set during AGP enable. So, only add the base
198 * address if the map's offset isn't already within the
201 if (map->offset < dev->agp->base ||
202 map->offset > dev->agp->base +
203 dev->agp->agp_info.ai_aperture_size - 1) {
204 map->offset += dev->agp->base;
206 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
207 /*for (entry = dev->agp->memory; entry; entry = entry->next) {
208 if ((map->offset >= entry->bound) &&
209 (map->offset + map->size <=
210 entry->bound + entry->pages * PAGE_SIZE)) {
216 drm_free(map, DRM_MEM_MAPS);
220 case _DRM_SCATTER_GATHER:
222 drm_free(map, DRM_MEM_MAPS);
225 map->virtual = (void *)(dev->sg->vaddr + offset);
226 map->offset = dev->sg->vaddr + offset;
228 case _DRM_CONSISTENT:
229 /* Unfortunately, we don't get any alignment specification from
230 * the caller, so we have to guess. drm_pci_alloc requires
231 * a power-of-two alignment, so try to align the bus address of
232 * the map to it size if possible, otherwise just assume
233 * PAGE_SIZE alignment.
236 if ((align & (align - 1)) != 0)
238 map->dmah = drm_pci_alloc(dev, map->size, align, 0xfffffffful);
239 if (map->dmah == NULL) {
240 drm_free(map, DRM_MEM_MAPS);
243 map->virtual = map->dmah->vaddr;
244 map->offset = map->dmah->busaddr;
247 DRM_ERROR("Bad map type %d\n", map->type);
248 drm_free(map, DRM_MEM_MAPS);
252 list_add(&entry->head, &dev->maplist);
255 /* Jumped to, with lock held, when a kernel map is found. */
257 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", map->type, map->offset,
265 int drm_addmap_ioctl(struct drm_device *dev, void *data,
266 struct drm_file *file_priv)
268 struct drm_map *request = data;
269 drm_local_map_t *map;
272 if (!(dev->flags & (FREAD|FWRITE)))
273 return EACCES; /* Require read/write */
275 if (!DRM_SUSER(DRM_CURPROC) && request->type != _DRM_AGP)
279 err = drm_addmap(dev, request->offset, request->size, request->type,
280 request->flags, &map);
285 request->offset = map->offset;
286 request->size = map->size;
287 request->type = map->type;
288 request->flags = map->flags;
289 request->mtrr = map->mtrr;
290 request->handle = (void *)map->handle;
295 void drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
297 struct drm_map_list *r_list = NULL, *list_t;
300 DRM_LOCK_ASSERT(dev);
305 /* Find the list entry for the map and remove it */
306 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
307 if (r_list->map == map) {
308 list_del(&r_list->head);
309 drm_free(r_list, DRM_MEM_DRIVER);
320 if (map->bsr == NULL)
321 drm_ioremapfree(map);
323 case _DRM_FRAME_BUFFER:
325 int __unused retcode;
327 retcode = drm_mtrr_del(0, map->offset, map->size,
329 DRM_DEBUG("mtrr_del = %d\n", retcode);
333 drm_free(map->virtual, DRM_MEM_MAPS);
336 case _DRM_SCATTER_GATHER:
338 case _DRM_CONSISTENT:
339 drm_pci_free(dev, map->dmah);
342 DRM_ERROR("Bad map type %d\n", map->type);
346 if (map->bsr != NULL) {
347 bus_release_resource(dev->dev, SYS_RES_MEMORY, map->rid,
353 free_unr(dev->map_unrhdr, (unsigned long)map->handle >>
354 DRM_MAP_HANDLE_SHIFT);
357 drm_free(map, DRM_MEM_MAPS);
360 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
361 * the last close of the device, and this is necessary for cleanup when things
362 * exit uncleanly. Therefore, having userland manually remove mappings seems
363 * like a pointless exercise since they're going away anyway.
365 * One use case might be after addmap is allowed for normal users for SHM and
366 * gets used by drivers that the server doesn't need to care about. This seems
369 * \param inode device inode.
370 * \param file_priv DRM file private.
371 * \param cmd command.
372 * \param arg pointer to a struct drm_map structure.
373 * \return zero on success or a negative value on error.
375 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
376 struct drm_file *file_priv)
378 struct drm_map *request = data;
379 struct drm_local_map *map = NULL;
380 struct drm_map_list *r_list;
383 list_for_each_entry(r_list, &dev->maplist, head) {
385 r_list->user_token == (unsigned long)request->handle &&
386 r_list->map->flags & _DRM_REMOVABLE) {
392 /* List has wrapped around to the head pointer, or its empty we didn't
395 if (list_empty(&dev->maplist) || !map) {
400 /* Register and framebuffer maps are permanent */
401 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
414 static void drm_cleanup_buf_error(struct drm_device *dev,
415 drm_buf_entry_t *entry)
419 if (entry->seg_count) {
420 for (i = 0; i < entry->seg_count; i++) {
421 drm_pci_free(dev, entry->seglist[i]);
423 drm_free(entry->seglist, DRM_MEM_SEGS);
425 entry->seg_count = 0;
428 if (entry->buf_count) {
429 for (i = 0; i < entry->buf_count; i++) {
430 drm_free(entry->buflist[i].dev_private, DRM_MEM_BUFS);
432 drm_free(entry->buflist, DRM_MEM_BUFS);
434 entry->buf_count = 0;
438 static int drm_do_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
440 drm_device_dma_t *dma = dev->dma;
441 drm_buf_entry_t *entry;
442 /*drm_agp_mem_t *agp_entry;
445 unsigned long offset;
446 unsigned long agp_offset;
455 drm_buf_t **temp_buflist;
457 count = request->count;
458 order = drm_order(request->size);
461 alignment = (request->flags & _DRM_PAGE_ALIGN)
462 ? round_page(size) : size;
463 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
464 total = PAGE_SIZE << page_order;
467 agp_offset = dev->agp->base + request->agp_start;
469 DRM_DEBUG("count: %d\n", count);
470 DRM_DEBUG("order: %d\n", order);
471 DRM_DEBUG("size: %d\n", size);
472 DRM_DEBUG("agp_offset: 0x%lx\n", agp_offset);
473 DRM_DEBUG("alignment: %d\n", alignment);
474 DRM_DEBUG("page_order: %d\n", page_order);
475 DRM_DEBUG("total: %d\n", total);
477 /* Make sure buffers are located in AGP memory that we own */
478 /* Breaks MGA due to drm_alloc_agp not setting up entries for the
479 * memory. Safe to ignore for now because these ioctls are still
483 for (agp_entry = dev->agp->memory; agp_entry;
484 agp_entry = agp_entry->next) {
485 if ((agp_offset >= agp_entry->bound) &&
486 (agp_offset + total * count <=
487 agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
493 DRM_DEBUG("zone invalid\n");
497 entry = &dma->bufs[order];
499 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
501 if (!entry->buflist) {
505 entry->buf_size = size;
506 entry->page_order = page_order;
510 while (entry->buf_count < count) {
511 buf = &entry->buflist[entry->buf_count];
512 buf->idx = dma->buf_count + entry->buf_count;
513 buf->total = alignment;
517 buf->offset = (dma->byte_count + offset);
518 buf->bus_address = agp_offset + offset;
519 buf->address = (void *)(agp_offset + offset);
522 buf->file_priv = NULL;
524 buf->dev_priv_size = dev->driver->buf_priv_size;
525 buf->dev_private = kmalloc(buf->dev_priv_size, DRM_MEM_BUFS,
527 if (buf->dev_private == NULL) {
528 /* Set count correctly so we free the proper amount. */
529 entry->buf_count = count;
530 drm_cleanup_buf_error(dev, entry);
536 byte_count += PAGE_SIZE << page_order;
539 DRM_DEBUG("byte_count: %d\n", byte_count);
541 temp_buflist = krealloc(dma->buflist,
542 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
543 DRM_MEM_BUFS, M_NOWAIT);
544 if (temp_buflist == NULL) {
545 /* Free the entry because it isn't valid */
546 drm_cleanup_buf_error(dev, entry);
549 dma->buflist = temp_buflist;
551 for (i = 0; i < entry->buf_count; i++) {
552 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
555 dma->buf_count += entry->buf_count;
556 dma->byte_count += byte_count;
558 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
559 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
561 request->count = entry->buf_count;
562 request->size = size;
564 dma->flags = _DRM_DMA_USE_AGP;
569 static int drm_do_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
571 drm_device_dma_t *dma = dev->dma;
577 drm_buf_entry_t *entry;
580 unsigned long offset;
584 unsigned long *temp_pagelist;
585 drm_buf_t **temp_buflist;
587 count = request->count;
588 order = drm_order(request->size);
591 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
592 request->count, request->size, size, order);
594 alignment = (request->flags & _DRM_PAGE_ALIGN)
595 ? round_page(size) : size;
596 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
597 total = PAGE_SIZE << page_order;
599 entry = &dma->bufs[order];
601 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
603 entry->seglist = kmalloc(count * sizeof(*entry->seglist), DRM_MEM_SEGS,
606 /* Keep the original pagelist until we know all the allocations
609 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
610 sizeof(*dma->pagelist), DRM_MEM_PAGES, M_NOWAIT);
612 if (entry->buflist == NULL || entry->seglist == NULL ||
613 temp_pagelist == NULL) {
614 drm_free(temp_pagelist, DRM_MEM_PAGES);
615 drm_free(entry->seglist, DRM_MEM_SEGS);
616 drm_free(entry->buflist, DRM_MEM_BUFS);
620 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
621 sizeof(*dma->pagelist));
623 DRM_DEBUG("pagelist: %d entries\n",
624 dma->page_count + (count << page_order));
626 entry->buf_size = size;
627 entry->page_order = page_order;
631 while (entry->buf_count < count) {
632 spin_unlock(&dev->dma_lock);
633 drm_dma_handle_t *dmah = drm_pci_alloc(dev, size, alignment,
635 spin_lock(&dev->dma_lock);
637 /* Set count correctly so we free the proper amount. */
638 entry->buf_count = count;
639 entry->seg_count = count;
640 drm_cleanup_buf_error(dev, entry);
641 drm_free(temp_pagelist, DRM_MEM_PAGES);
645 entry->seglist[entry->seg_count++] = dmah;
646 for (i = 0; i < (1 << page_order); i++) {
647 DRM_DEBUG("page %d @ %p\n",
648 dma->page_count + page_count,
649 (char *)dmah->vaddr + PAGE_SIZE * i);
650 temp_pagelist[dma->page_count + page_count++] =
651 (long)dmah->vaddr + PAGE_SIZE * i;
654 offset + size <= total && entry->buf_count < count;
655 offset += alignment, ++entry->buf_count) {
656 buf = &entry->buflist[entry->buf_count];
657 buf->idx = dma->buf_count + entry->buf_count;
658 buf->total = alignment;
661 buf->offset = (dma->byte_count + byte_count + offset);
662 buf->address = ((char *)dmah->vaddr + offset);
663 buf->bus_address = dmah->busaddr + offset;
666 buf->file_priv = NULL;
668 buf->dev_priv_size = dev->driver->buf_priv_size;
669 buf->dev_private = kmalloc(buf->dev_priv_size,
670 DRM_MEM_BUFS, M_NOWAIT | M_ZERO);
671 if (buf->dev_private == NULL) {
672 /* Set count correctly so we free the proper amount. */
673 entry->buf_count = count;
674 entry->seg_count = count;
675 drm_cleanup_buf_error(dev, entry);
676 drm_free(temp_pagelist, DRM_MEM_PAGES);
680 DRM_DEBUG("buffer %d @ %p\n",
681 entry->buf_count, buf->address);
683 byte_count += PAGE_SIZE << page_order;
686 temp_buflist = krealloc(dma->buflist,
687 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
688 DRM_MEM_BUFS, M_NOWAIT);
689 if (temp_buflist == NULL) {
690 /* Free the entry because it isn't valid */
691 drm_cleanup_buf_error(dev, entry);
692 drm_free(temp_pagelist, DRM_MEM_PAGES);
695 dma->buflist = temp_buflist;
697 for (i = 0; i < entry->buf_count; i++) {
698 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
701 /* No allocations failed, so now we can replace the orginal pagelist
704 drm_free(dma->pagelist, DRM_MEM_PAGES);
705 dma->pagelist = temp_pagelist;
707 dma->buf_count += entry->buf_count;
708 dma->seg_count += entry->seg_count;
709 dma->page_count += entry->seg_count << page_order;
710 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
712 request->count = entry->buf_count;
713 request->size = size;
719 static int drm_do_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
721 drm_device_dma_t *dma = dev->dma;
722 drm_buf_entry_t *entry;
724 unsigned long offset;
725 unsigned long agp_offset;
734 drm_buf_t **temp_buflist;
736 count = request->count;
737 order = drm_order(request->size);
740 alignment = (request->flags & _DRM_PAGE_ALIGN)
741 ? round_page(size) : size;
742 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
743 total = PAGE_SIZE << page_order;
746 agp_offset = request->agp_start;
748 DRM_DEBUG("count: %d\n", count);
749 DRM_DEBUG("order: %d\n", order);
750 DRM_DEBUG("size: %d\n", size);
751 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
752 DRM_DEBUG("alignment: %d\n", alignment);
753 DRM_DEBUG("page_order: %d\n", page_order);
754 DRM_DEBUG("total: %d\n", total);
756 entry = &dma->bufs[order];
758 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
760 if (entry->buflist == NULL)
763 entry->buf_size = size;
764 entry->page_order = page_order;
768 while (entry->buf_count < count) {
769 buf = &entry->buflist[entry->buf_count];
770 buf->idx = dma->buf_count + entry->buf_count;
771 buf->total = alignment;
775 buf->offset = (dma->byte_count + offset);
776 buf->bus_address = agp_offset + offset;
777 buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
780 buf->file_priv = NULL;
782 buf->dev_priv_size = dev->driver->buf_priv_size;
783 buf->dev_private = kmalloc(buf->dev_priv_size, DRM_MEM_BUFS,
785 if (buf->dev_private == NULL) {
786 /* Set count correctly so we free the proper amount. */
787 entry->buf_count = count;
788 drm_cleanup_buf_error(dev, entry);
792 DRM_DEBUG("buffer %d @ %p\n",
793 entry->buf_count, buf->address);
797 byte_count += PAGE_SIZE << page_order;
800 DRM_DEBUG("byte_count: %d\n", byte_count);
802 temp_buflist = krealloc(dma->buflist,
803 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
804 DRM_MEM_BUFS, M_NOWAIT);
805 if (temp_buflist == NULL) {
806 /* Free the entry because it isn't valid */
807 drm_cleanup_buf_error(dev, entry);
810 dma->buflist = temp_buflist;
812 for (i = 0; i < entry->buf_count; i++) {
813 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
816 dma->buf_count += entry->buf_count;
817 dma->byte_count += byte_count;
819 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
820 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
822 request->count = entry->buf_count;
823 request->size = size;
825 dma->flags = _DRM_DMA_USE_SG;
830 int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
834 if (request->count < 0 || request->count > 4096)
837 order = drm_order(request->size);
838 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
841 spin_lock(&dev->dma_lock);
843 /* No more allocations after first buffer-using ioctl. */
844 if (dev->buf_use != 0) {
845 spin_unlock(&dev->dma_lock);
848 /* No more than one allocation per order */
849 if (dev->dma->bufs[order].buf_count != 0) {
850 spin_unlock(&dev->dma_lock);
854 ret = drm_do_addbufs_agp(dev, request);
856 spin_unlock(&dev->dma_lock);
861 int drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
865 if (!DRM_SUSER(DRM_CURPROC))
868 if (request->count < 0 || request->count > 4096)
871 order = drm_order(request->size);
872 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
875 spin_lock(&dev->dma_lock);
877 /* No more allocations after first buffer-using ioctl. */
878 if (dev->buf_use != 0) {
879 spin_unlock(&dev->dma_lock);
882 /* No more than one allocation per order */
883 if (dev->dma->bufs[order].buf_count != 0) {
884 spin_unlock(&dev->dma_lock);
888 ret = drm_do_addbufs_sg(dev, request);
890 spin_unlock(&dev->dma_lock);
895 int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
899 if (!DRM_SUSER(DRM_CURPROC))
902 if (request->count < 0 || request->count > 4096)
905 order = drm_order(request->size);
906 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
909 spin_lock(&dev->dma_lock);
911 /* No more allocations after first buffer-using ioctl. */
912 if (dev->buf_use != 0) {
913 spin_unlock(&dev->dma_lock);
916 /* No more than one allocation per order */
917 if (dev->dma->bufs[order].buf_count != 0) {
918 spin_unlock(&dev->dma_lock);
922 ret = drm_do_addbufs_pci(dev, request);
924 spin_unlock(&dev->dma_lock);
929 int drm_addbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
931 struct drm_buf_desc *request = data;
934 if (request->flags & _DRM_AGP_BUFFER)
935 err = drm_addbufs_agp(dev, request);
936 else if (request->flags & _DRM_SG_BUFFER)
937 err = drm_addbufs_sg(dev, request);
939 err = drm_addbufs_pci(dev, request);
944 int drm_infobufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
946 drm_device_dma_t *dma = dev->dma;
947 struct drm_buf_info *request = data;
952 spin_lock(&dev->dma_lock);
953 ++dev->buf_use; /* Can't allocate more after this call */
954 spin_unlock(&dev->dma_lock);
956 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
957 if (dma->bufs[i].buf_count)
961 DRM_DEBUG("count = %d\n", count);
963 if (request->count >= count) {
964 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
965 if (dma->bufs[i].buf_count) {
966 struct drm_buf_desc from;
968 from.count = dma->bufs[i].buf_count;
969 from.size = dma->bufs[i].buf_size;
970 from.low_mark = dma->bufs[i].freelist.low_mark;
971 from.high_mark = dma->bufs[i].freelist.high_mark;
973 if (DRM_COPY_TO_USER(&request->list[count], &from,
974 sizeof(struct drm_buf_desc)) != 0) {
979 DRM_DEBUG("%d %d %d %d %d\n",
980 i, dma->bufs[i].buf_count,
981 dma->bufs[i].buf_size,
982 dma->bufs[i].freelist.low_mark,
983 dma->bufs[i].freelist.high_mark);
988 request->count = count;
993 int drm_markbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
995 drm_device_dma_t *dma = dev->dma;
996 struct drm_buf_desc *request = data;
999 DRM_DEBUG("%d, %d, %d\n",
1000 request->size, request->low_mark, request->high_mark);
1003 order = drm_order(request->size);
1004 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
1005 request->low_mark < 0 || request->high_mark < 0) {
1009 spin_lock(&dev->dma_lock);
1010 if (request->low_mark > dma->bufs[order].buf_count ||
1011 request->high_mark > dma->bufs[order].buf_count) {
1012 spin_unlock(&dev->dma_lock);
1016 dma->bufs[order].freelist.low_mark = request->low_mark;
1017 dma->bufs[order].freelist.high_mark = request->high_mark;
1018 spin_unlock(&dev->dma_lock);
1023 int drm_freebufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
1025 drm_device_dma_t *dma = dev->dma;
1026 struct drm_buf_free *request = data;
1032 DRM_DEBUG("%d\n", request->count);
1034 spin_lock(&dev->dma_lock);
1035 for (i = 0; i < request->count; i++) {
1036 if (DRM_COPY_FROM_USER(&idx, &request->list[i], sizeof(idx))) {
1040 if (idx < 0 || idx >= dma->buf_count) {
1041 DRM_ERROR("Index %d (of %d max)\n",
1042 idx, dma->buf_count - 1);
1046 buf = dma->buflist[idx];
1047 if (buf->file_priv != file_priv) {
1048 DRM_ERROR("Process %d freeing buffer not owned\n",
1053 drm_free_buffer(dev, buf);
1055 spin_unlock(&dev->dma_lock);
1060 int drm_mapbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
1062 drm_device_dma_t *dma = dev->dma;
1065 vm_offset_t address;
1066 struct vmspace *vms;
1070 struct drm_buf_map *request = data;
1073 vms = DRM_CURPROC->td_proc->p_vmspace;
1075 spin_lock(&dev->dma_lock);
1076 dev->buf_use++; /* Can't allocate more after this call */
1077 spin_unlock(&dev->dma_lock);
1079 if (request->count < dma->buf_count)
1082 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
1083 (drm_core_check_feature(dev, DRIVER_SG) &&
1084 (dma->flags & _DRM_DMA_USE_SG))) {
1085 drm_local_map_t *map = dev->agp_buffer_map;
1091 size = round_page(map->size);
1092 foff = (unsigned long)map->handle;
1094 size = round_page(dma->byte_count),
1098 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
1099 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1100 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC,
1101 SLIST_FIRST(&dev->devnode->si_hlist), foff);
1105 request->virtual = (void *)vaddr;
1107 for (i = 0; i < dma->buf_count; i++) {
1108 if (DRM_COPY_TO_USER(&request->list[i].idx,
1109 &dma->buflist[i]->idx, sizeof(request->list[0].idx))) {
1113 if (DRM_COPY_TO_USER(&request->list[i].total,
1114 &dma->buflist[i]->total, sizeof(request->list[0].total))) {
1118 if (DRM_COPY_TO_USER(&request->list[i].used, &zero,
1123 address = vaddr + dma->buflist[i]->offset; /* *** */
1124 if (DRM_COPY_TO_USER(&request->list[i].address, &address,
1132 request->count = dma->buf_count;
1134 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1140 * Compute order. Can be made faster.
1142 int drm_order(unsigned long size)
1149 order = flsl(size) - 1;
1150 if (size & ~(1ul << order))