3 * Generic buffer template
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
35 * $FreeBSD: src/sys/dev/drm2/drm_bufs.c,v 1.1 2012/05/22 11:07:44 kib Exp $
39 #include <bus/pci/pcireg.h>
40 #include <linux/types.h>
41 #include <linux/export.h>
44 /* Allocation of PCI memory resources (framebuffer, registers, etc.) for
45 * drm_get_resource_*. Note that they are not RF_ACTIVE, so there's no virtual
46 * address for accessing them. Cleaned up at unload.
48 static int drm_alloc_resource(struct drm_device *dev, int resource)
55 if (resource >= DRM_MAX_PCI_RESOURCE) {
56 DRM_ERROR("Resource %d too large\n", resource);
60 if (dev->pcir[resource] != NULL) {
65 rid = PCIR_BAR(resource);
66 res = bus_alloc_resource_any(dev->dev, SYS_RES_MEMORY, &rid,
70 DRM_ERROR("Couldn't find resource 0x%x\n", resource);
74 if (dev->pcir[resource] == NULL) {
75 dev->pcirid[resource] = rid;
76 dev->pcir[resource] = res;
82 unsigned long drm_get_resource_start(struct drm_device *dev,
83 unsigned int resource)
85 if (drm_alloc_resource(dev, resource) != 0)
88 return rman_get_start(dev->pcir[resource]);
91 unsigned long drm_get_resource_len(struct drm_device *dev,
92 unsigned int resource)
94 if (drm_alloc_resource(dev, resource) != 0)
97 return rman_get_size(dev->pcir[resource]);
100 int drm_addmap(struct drm_device * dev, resource_size_t offset,
101 unsigned int size, enum drm_map_type type,
102 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
104 struct drm_local_map *map;
105 struct drm_map_list *entry = NULL;
106 drm_dma_handle_t *dmah;
108 /* Allocate a new map structure, fill it in, and do any type-specific
109 * initialization necessary.
111 map = kmalloc(sizeof(*map), M_DRM, M_ZERO | M_WAITOK | M_NULLOK);
116 map->offset = offset;
121 /* Only allow shared memory to be removable since we only keep enough
122 * book keeping information about shared memory to allow for removal
123 * when processes fork.
125 if ((flags & _DRM_REMOVABLE) && type != _DRM_SHM) {
126 DRM_ERROR("Requested removable map for non-DRM_SHM\n");
127 drm_free(map, M_DRM);
130 if ((offset & PAGE_MASK) || (size & PAGE_MASK)) {
131 DRM_ERROR("offset/size not page aligned: 0x%jx/0x%04x\n",
132 (uintmax_t)offset, size);
133 drm_free(map, M_DRM);
136 if (offset + size < offset) {
137 DRM_ERROR("offset and size wrap around: 0x%jx/0x%04x\n",
138 (uintmax_t)offset, size);
139 drm_free(map, M_DRM);
143 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
144 (unsigned long long)map->offset, map->size, map->type);
146 /* Check if this is just another version of a kernel-allocated map, and
147 * just hand that back if so.
149 if (type == _DRM_REGISTERS || type == _DRM_FRAME_BUFFER ||
151 list_for_each_entry(entry, &dev->maplist, head) {
152 if (entry->map->type == type && (entry->map->offset == offset ||
153 (entry->map->type == _DRM_SHM &&
154 entry->map->flags == _DRM_CONTAINS_LOCK))) {
155 entry->map->size = size;
156 DRM_DEBUG("Found kernel map %d\n", type);
164 map->handle = drm_ioremap(dev, map);
165 if (!(map->flags & _DRM_WRITE_COMBINING))
168 case _DRM_FRAME_BUFFER:
169 if (drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC) == 0)
173 map->handle = kmalloc(map->size, M_DRM, M_WAITOK | M_NULLOK);
174 DRM_DEBUG("%lu %d %p\n",
175 map->size, drm_order(map->size), map->handle);
177 drm_free(map, M_DRM);
180 map->offset = (unsigned long)map->handle;
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->handle, M_DRM);
187 drm_free(map, M_DRM);
190 dev->lock.hw_lock = map->handle; /* 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->agp_info.ai_aperture_size - 1) {
205 map->offset += dev->agp->base;
207 map->mtrr = dev->agp->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, M_DRM);
221 case _DRM_SCATTER_GATHER:
223 drm_free(map, M_DRM);
226 map->handle = (void *)(uintptr_t)(dev->sg->vaddr + offset);
227 map->offset = dev->sg->vaddr + offset;
229 case _DRM_CONSISTENT:
230 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
231 * As we're limiting the address to 2^32-1 (or less),
232 * casting it down to 32 bits is no problem, but we
233 * need to point to a 64bit variable first. */
234 dmah = drm_pci_alloc(dev, map->size, map->size);
239 map->handle = dmah->vaddr;
240 map->offset = dmah->busaddr;
243 DRM_ERROR("Bad map type %d\n", map->type);
244 drm_free(map, M_DRM);
248 list_add(&entry->head, &dev->maplist);
251 /* Jumped to, with lock held, when a kernel map is found. */
253 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", map->type, map->offset,
262 * Ioctl to specify a range of memory that is available for mapping by a
265 * \param inode device inode.
266 * \param file_priv DRM file private.
267 * \param cmd command.
268 * \param arg pointer to a drm_map structure.
269 * \return zero on success or a negative value on error.
272 int drm_addmap_ioctl(struct drm_device *dev, void *data,
273 struct drm_file *file_priv)
275 struct drm_map *request = data;
276 drm_local_map_t *map;
279 if (!(dev->flags & (FREAD|FWRITE)))
280 return EACCES; /* Require read/write */
282 if (!DRM_SUSER(DRM_CURPROC) && request->type != _DRM_AGP)
286 err = drm_addmap(dev, request->offset, request->size, request->type,
287 request->flags, &map);
292 request->offset = map->offset;
293 request->size = map->size;
294 request->type = map->type;
295 request->flags = map->flags;
296 request->mtrr = map->mtrr;
297 request->handle = (void *)map->handle;
302 void drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
304 struct drm_map_list *r_list = NULL, *list_t;
305 drm_dma_handle_t dmah;
308 DRM_LOCK_ASSERT(dev);
313 /* Find the list entry for the map and remove it */
314 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
315 if (r_list->map == map) {
316 list_del(&r_list->head);
317 drm_free(r_list, M_DRM);
328 drm_ioremapfree(map);
330 case _DRM_FRAME_BUFFER:
332 int __unused retcode;
334 retcode = drm_mtrr_del(0, map->offset, map->size,
336 DRM_DEBUG("mtrr_del = %d\n", retcode);
340 drm_free(map->handle, M_DRM);
343 case _DRM_SCATTER_GATHER:
345 case _DRM_CONSISTENT:
346 dmah.vaddr = map->handle;
347 dmah.busaddr = map->offset;
348 drm_pci_free(dev, &dmah);
351 DRM_ERROR("Bad map type %d\n", map->type);
355 drm_free(map, M_DRM);
358 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
359 * the last close of the device, and this is necessary for cleanup when things
360 * exit uncleanly. Therefore, having userland manually remove mappings seems
361 * like a pointless exercise since they're going away anyway.
363 * One use case might be after addmap is allowed for normal users for SHM and
364 * gets used by drivers that the server doesn't need to care about. This seems
367 * \param inode device inode.
368 * \param file_priv DRM file private.
369 * \param cmd command.
370 * \param arg pointer to a struct drm_map structure.
371 * \return zero on success or a negative value on error.
373 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
374 struct drm_file *file_priv)
376 struct drm_map *request = data;
377 struct drm_local_map *map = NULL;
378 struct drm_map_list *r_list;
381 list_for_each_entry(r_list, &dev->maplist, head) {
383 r_list->user_token == (unsigned long)request->handle &&
384 r_list->map->flags & _DRM_REMOVABLE) {
390 /* List has wrapped around to the head pointer, or its empty we didn't
393 if (list_empty(&dev->maplist) || !map) {
398 /* Register and framebuffer maps are permanent */
399 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
412 * Cleanup after an error on one of the addbufs() functions.
414 * \param dev DRM device.
415 * \param entry buffer entry where the error occurred.
417 * Frees any pages and buffers associated with the given entry.
419 static void drm_cleanup_buf_error(struct drm_device * dev,
420 struct drm_buf_entry * entry)
424 if (entry->seg_count) {
425 for (i = 0; i < entry->seg_count; i++) {
426 drm_pci_free(dev, entry->seglist[i]);
428 drm_free(entry->seglist, M_DRM);
430 entry->seg_count = 0;
433 if (entry->buf_count) {
434 for (i = 0; i < entry->buf_count; i++) {
435 drm_free(entry->buflist[i].dev_private, M_DRM);
437 drm_free(entry->buflist, M_DRM);
439 entry->buf_count = 0;
443 static int drm_do_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
445 drm_device_dma_t *dma = dev->dma;
446 drm_buf_entry_t *entry;
447 /*drm_agp_mem_t *agp_entry;
450 unsigned long offset;
451 unsigned long agp_offset;
460 drm_buf_t **temp_buflist;
462 count = request->count;
463 order = drm_order(request->size);
466 alignment = (request->flags & _DRM_PAGE_ALIGN)
467 ? round_page(size) : size;
468 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
469 total = PAGE_SIZE << page_order;
472 agp_offset = dev->agp->base + request->agp_start;
474 DRM_DEBUG("count: %d\n", count);
475 DRM_DEBUG("order: %d\n", order);
476 DRM_DEBUG("size: %d\n", size);
477 DRM_DEBUG("agp_offset: 0x%lx\n", agp_offset);
478 DRM_DEBUG("alignment: %d\n", alignment);
479 DRM_DEBUG("page_order: %d\n", page_order);
480 DRM_DEBUG("total: %d\n", total);
482 /* Make sure buffers are located in AGP memory that we own */
483 /* Breaks MGA due to drm_alloc_agp not setting up entries for the
484 * memory. Safe to ignore for now because these ioctls are still
488 for (agp_entry = dev->agp->memory; agp_entry;
489 agp_entry = agp_entry->next) {
490 if ((agp_offset >= agp_entry->bound) &&
491 (agp_offset + total * count <=
492 agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
498 DRM_DEBUG("zone invalid\n");
502 entry = &dma->bufs[order];
504 entry->buflist = kmalloc(count * sizeof(*entry->buflist), M_DRM,
505 M_WAITOK | M_NULLOK | M_ZERO);
506 if (!entry->buflist) {
510 entry->buf_size = size;
511 entry->page_order = page_order;
515 while (entry->buf_count < count) {
516 buf = &entry->buflist[entry->buf_count];
517 buf->idx = dma->buf_count + entry->buf_count;
518 buf->total = alignment;
522 buf->offset = (dma->byte_count + offset);
523 buf->bus_address = agp_offset + offset;
524 buf->address = (void *)(agp_offset + offset);
527 buf->file_priv = NULL;
529 buf->dev_priv_size = dev->driver->buf_priv_size;
530 buf->dev_private = kmalloc(buf->dev_priv_size, M_DRM,
531 M_WAITOK | M_NULLOK | M_ZERO);
532 if (buf->dev_private == NULL) {
533 /* Set count correctly so we free the proper amount. */
534 entry->buf_count = count;
535 drm_cleanup_buf_error(dev, entry);
541 byte_count += PAGE_SIZE << page_order;
544 DRM_DEBUG("byte_count: %d\n", byte_count);
546 temp_buflist = krealloc(dma->buflist,
547 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
548 M_DRM, M_WAITOK | M_NULLOK);
549 if (temp_buflist == NULL) {
550 /* Free the entry because it isn't valid */
551 drm_cleanup_buf_error(dev, entry);
554 dma->buflist = temp_buflist;
556 for (i = 0; i < entry->buf_count; i++) {
557 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
560 dma->buf_count += entry->buf_count;
561 dma->byte_count += byte_count;
563 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
564 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
566 request->count = entry->buf_count;
567 request->size = size;
569 dma->flags = _DRM_DMA_USE_AGP;
574 static int drm_do_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
576 drm_device_dma_t *dma = dev->dma;
582 drm_buf_entry_t *entry;
583 drm_dma_handle_t *dmah;
586 unsigned long offset;
590 unsigned long *temp_pagelist;
591 drm_buf_t **temp_buflist;
593 count = request->count;
594 order = drm_order(request->size);
597 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
598 request->count, request->size, size, order);
600 alignment = (request->flags & _DRM_PAGE_ALIGN)
601 ? round_page(size) : size;
602 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
603 total = PAGE_SIZE << page_order;
605 entry = &dma->bufs[order];
607 entry->buflist = kmalloc(count * sizeof(*entry->buflist), M_DRM,
608 M_WAITOK | M_NULLOK | M_ZERO);
609 entry->seglist = kmalloc(count * sizeof(*entry->seglist), M_DRM,
610 M_WAITOK | M_NULLOK | M_ZERO);
612 /* Keep the original pagelist until we know all the allocations
615 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
616 sizeof(*dma->pagelist),
617 M_DRM, M_WAITOK | M_NULLOK);
619 if (entry->buflist == NULL || entry->seglist == NULL ||
620 temp_pagelist == NULL) {
621 drm_free(temp_pagelist, M_DRM);
622 drm_free(entry->seglist, M_DRM);
623 drm_free(entry->buflist, M_DRM);
627 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
628 sizeof(*dma->pagelist));
630 DRM_DEBUG("pagelist: %d entries\n",
631 dma->page_count + (count << page_order));
633 entry->buf_size = size;
634 entry->page_order = page_order;
638 while (entry->buf_count < count) {
639 spin_unlock(&dev->dma_lock);
640 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
641 spin_lock(&dev->dma_lock);
644 /* Set count correctly so we free the proper amount. */
645 entry->buf_count = count;
646 entry->seg_count = count;
647 drm_cleanup_buf_error(dev, entry);
648 drm_free(temp_pagelist, M_DRM);
652 entry->seglist[entry->seg_count++] = dmah;
653 for (i = 0; i < (1 << page_order); i++) {
654 DRM_DEBUG("page %d @ 0x%08lx\n",
655 dma->page_count + page_count,
656 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
657 temp_pagelist[dma->page_count + page_count++]
658 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
661 offset + size <= total && entry->buf_count < count;
662 offset += alignment, ++entry->buf_count) {
663 buf = &entry->buflist[entry->buf_count];
664 buf->idx = dma->buf_count + entry->buf_count;
665 buf->total = alignment;
668 buf->offset = (dma->byte_count + byte_count + offset);
669 buf->address = ((char *)dmah->vaddr + offset);
670 buf->bus_address = dmah->busaddr + offset;
673 buf->file_priv = NULL;
675 buf->dev_priv_size = dev->driver->buf_priv_size;
676 buf->dev_private = kmalloc(buf->dev_priv_size,
678 M_WAITOK | M_NULLOK |
680 if (buf->dev_private == NULL) {
681 /* Set count correctly so we free the proper amount. */
682 entry->buf_count = count;
683 entry->seg_count = count;
684 drm_cleanup_buf_error(dev, entry);
685 drm_free(temp_pagelist, M_DRM);
689 DRM_DEBUG("buffer %d @ %p\n",
690 entry->buf_count, buf->address);
692 byte_count += PAGE_SIZE << page_order;
695 temp_buflist = krealloc(dma->buflist,
696 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
697 M_DRM, M_WAITOK | M_NULLOK);
698 if (temp_buflist == NULL) {
699 /* Free the entry because it isn't valid */
700 drm_cleanup_buf_error(dev, entry);
701 drm_free(temp_pagelist, M_DRM);
704 dma->buflist = temp_buflist;
706 for (i = 0; i < entry->buf_count; i++) {
707 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
710 /* No allocations failed, so now we can replace the orginal pagelist
713 drm_free(dma->pagelist, M_DRM);
714 dma->pagelist = temp_pagelist;
716 dma->buf_count += entry->buf_count;
717 dma->seg_count += entry->seg_count;
718 dma->page_count += entry->seg_count << page_order;
719 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
721 request->count = entry->buf_count;
722 request->size = size;
728 static int drm_do_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
730 drm_device_dma_t *dma = dev->dma;
731 drm_buf_entry_t *entry;
733 unsigned long offset;
734 unsigned long agp_offset;
743 drm_buf_t **temp_buflist;
745 count = request->count;
746 order = drm_order(request->size);
749 alignment = (request->flags & _DRM_PAGE_ALIGN)
750 ? round_page(size) : size;
751 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
752 total = PAGE_SIZE << page_order;
755 agp_offset = request->agp_start;
757 DRM_DEBUG("count: %d\n", count);
758 DRM_DEBUG("order: %d\n", order);
759 DRM_DEBUG("size: %d\n", size);
760 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
761 DRM_DEBUG("alignment: %d\n", alignment);
762 DRM_DEBUG("page_order: %d\n", page_order);
763 DRM_DEBUG("total: %d\n", total);
765 entry = &dma->bufs[order];
767 entry->buflist = kmalloc(count * sizeof(*entry->buflist), M_DRM,
768 M_WAITOK | M_NULLOK | M_ZERO);
769 if (entry->buflist == NULL)
772 entry->buf_size = size;
773 entry->page_order = page_order;
777 while (entry->buf_count < count) {
778 buf = &entry->buflist[entry->buf_count];
779 buf->idx = dma->buf_count + entry->buf_count;
780 buf->total = alignment;
784 buf->offset = (dma->byte_count + offset);
785 buf->bus_address = agp_offset + offset;
786 buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
789 buf->file_priv = NULL;
791 buf->dev_priv_size = dev->driver->buf_priv_size;
792 buf->dev_private = kmalloc(buf->dev_priv_size, M_DRM,
793 M_WAITOK | M_NULLOK | M_ZERO);
794 if (buf->dev_private == NULL) {
795 /* Set count correctly so we free the proper amount. */
796 entry->buf_count = count;
797 drm_cleanup_buf_error(dev, entry);
801 DRM_DEBUG("buffer %d @ %p\n",
802 entry->buf_count, buf->address);
806 byte_count += PAGE_SIZE << page_order;
809 DRM_DEBUG("byte_count: %d\n", byte_count);
811 temp_buflist = krealloc(dma->buflist,
812 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
813 M_DRM, M_WAITOK | M_NULLOK);
814 if (temp_buflist == NULL) {
815 /* Free the entry because it isn't valid */
816 drm_cleanup_buf_error(dev, entry);
819 dma->buflist = temp_buflist;
821 for (i = 0; i < entry->buf_count; i++) {
822 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
825 dma->buf_count += entry->buf_count;
826 dma->byte_count += byte_count;
828 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
829 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
831 request->count = entry->buf_count;
832 request->size = size;
834 dma->flags = _DRM_DMA_USE_SG;
840 * Add AGP buffers for DMA transfers.
842 * \param dev struct drm_device to which the buffers are to be added.
843 * \param request pointer to a struct drm_buf_desc describing the request.
844 * \return zero on success or a negative number on failure.
846 * After some sanity checks creates a drm_buf structure for each buffer and
847 * reallocates the buffer list of the same size order to accommodate the new
850 int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
854 if (request->count < 0 || request->count > 4096)
857 order = drm_order(request->size);
858 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
861 spin_lock(&dev->dma_lock);
863 /* No more allocations after first buffer-using ioctl. */
864 if (dev->buf_use != 0) {
865 spin_unlock(&dev->dma_lock);
868 /* No more than one allocation per order */
869 if (dev->dma->bufs[order].buf_count != 0) {
870 spin_unlock(&dev->dma_lock);
874 ret = drm_do_addbufs_agp(dev, request);
876 spin_unlock(&dev->dma_lock);
881 static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
885 if (!DRM_SUSER(DRM_CURPROC))
888 if (request->count < 0 || request->count > 4096)
891 order = drm_order(request->size);
892 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
895 spin_lock(&dev->dma_lock);
897 /* No more allocations after first buffer-using ioctl. */
898 if (dev->buf_use != 0) {
899 spin_unlock(&dev->dma_lock);
902 /* No more than one allocation per order */
903 if (dev->dma->bufs[order].buf_count != 0) {
904 spin_unlock(&dev->dma_lock);
908 ret = drm_do_addbufs_sg(dev, request);
910 spin_unlock(&dev->dma_lock);
915 int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
919 if (!DRM_SUSER(DRM_CURPROC))
922 if (request->count < 0 || request->count > 4096)
925 order = drm_order(request->size);
926 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
929 spin_lock(&dev->dma_lock);
931 /* No more allocations after first buffer-using ioctl. */
932 if (dev->buf_use != 0) {
933 spin_unlock(&dev->dma_lock);
936 /* No more than one allocation per order */
937 if (dev->dma->bufs[order].buf_count != 0) {
938 spin_unlock(&dev->dma_lock);
942 ret = drm_do_addbufs_pci(dev, request);
944 spin_unlock(&dev->dma_lock);
950 * Add buffers for DMA transfers (ioctl).
952 * \param inode device inode.
953 * \param file_priv DRM file private.
954 * \param cmd command.
955 * \param arg pointer to a struct drm_buf_desc request.
956 * \return zero on success or a negative number on failure.
958 * According with the memory type specified in drm_buf_desc::flags and the
959 * build options, it dispatches the call either to addbufs_agp(),
960 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
961 * PCI memory respectively.
963 int drm_addbufs(struct drm_device *dev, void *data,
964 struct drm_file *file_priv)
966 struct drm_buf_desc *request = data;
969 if (request->flags & _DRM_AGP_BUFFER)
970 err = drm_addbufs_agp(dev, request);
971 else if (request->flags & _DRM_SG_BUFFER)
972 err = drm_addbufs_sg(dev, request);
974 err = drm_addbufs_pci(dev, request);
980 * Get information about the buffer mappings.
982 * This was originally mean for debugging purposes, or by a sophisticated
983 * client library to determine how best to use the available buffers (e.g.,
984 * large buffers can be used for image transfer).
986 * \param inode device inode.
987 * \param file_priv DRM file private.
988 * \param cmd command.
989 * \param arg pointer to a drm_buf_info structure.
990 * \return zero on success or a negative number on failure.
992 * Increments drm_device::buf_use while holding the drm_device::count_lock
993 * lock, preventing of allocating more buffers after this call. Information
994 * about each requested buffer is then copied into user space.
996 int drm_infobufs(struct drm_device *dev, void *data,
997 struct drm_file *file_priv)
999 drm_device_dma_t *dma = dev->dma;
1000 struct drm_buf_info *request = data;
1005 spin_lock(&dev->dma_lock);
1006 ++dev->buf_use; /* Can't allocate more after this call */
1007 spin_unlock(&dev->dma_lock);
1009 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1010 if (dma->bufs[i].buf_count)
1014 DRM_DEBUG("count = %d\n", count);
1016 if (request->count >= count) {
1017 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1018 if (dma->bufs[i].buf_count) {
1019 struct drm_buf_desc from;
1021 from.count = dma->bufs[i].buf_count;
1022 from.size = dma->bufs[i].buf_size;
1023 from.low_mark = dma->bufs[i].freelist.low_mark;
1024 from.high_mark = dma->bufs[i].freelist.high_mark;
1026 if (DRM_COPY_TO_USER(&request->list[count], &from,
1027 sizeof(struct drm_buf_desc)) != 0) {
1032 DRM_DEBUG("%d %d %d %d %d\n",
1033 i, dma->bufs[i].buf_count,
1034 dma->bufs[i].buf_size,
1035 dma->bufs[i].freelist.low_mark,
1036 dma->bufs[i].freelist.high_mark);
1041 request->count = count;
1047 * Specifies a low and high water mark for buffer allocation
1049 * \param inode device inode.
1050 * \param file_priv DRM file private.
1051 * \param cmd command.
1052 * \param arg a pointer to a drm_buf_desc structure.
1053 * \return zero on success or a negative number on failure.
1055 * Verifies that the size order is bounded between the admissible orders and
1056 * updates the respective drm_device_dma::bufs entry low and high water mark.
1058 * \note This ioctl is deprecated and mostly never used.
1060 int drm_markbufs(struct drm_device *dev, void *data,
1061 struct drm_file *file_priv)
1063 drm_device_dma_t *dma = dev->dma;
1064 struct drm_buf_desc *request = data;
1067 DRM_DEBUG("%d, %d, %d\n",
1068 request->size, request->low_mark, request->high_mark);
1071 order = drm_order(request->size);
1072 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
1073 request->low_mark < 0 || request->high_mark < 0) {
1077 spin_lock(&dev->dma_lock);
1078 if (request->low_mark > dma->bufs[order].buf_count ||
1079 request->high_mark > dma->bufs[order].buf_count) {
1080 spin_unlock(&dev->dma_lock);
1084 dma->bufs[order].freelist.low_mark = request->low_mark;
1085 dma->bufs[order].freelist.high_mark = request->high_mark;
1086 spin_unlock(&dev->dma_lock);
1092 * Unreserve the buffers in list, previously reserved using drmDMA.
1094 * \param inode device inode.
1095 * \param file_priv DRM file private.
1096 * \param cmd command.
1097 * \param arg pointer to a drm_buf_free structure.
1098 * \return zero on success or a negative number on failure.
1100 * Calls free_buffer() for each used buffer.
1101 * This function is primarily used for debugging.
1103 int drm_freebufs(struct drm_device *dev, void *data,
1104 struct drm_file *file_priv)
1106 drm_device_dma_t *dma = dev->dma;
1107 struct drm_buf_free *request = data;
1113 DRM_DEBUG("%d\n", request->count);
1115 spin_lock(&dev->dma_lock);
1116 for (i = 0; i < request->count; i++) {
1117 if (DRM_COPY_FROM_USER(&idx, &request->list[i], sizeof(idx))) {
1121 if (idx < 0 || idx >= dma->buf_count) {
1122 DRM_ERROR("Index %d (of %d max)\n",
1123 idx, dma->buf_count - 1);
1127 buf = dma->buflist[idx];
1128 if (buf->file_priv != file_priv) {
1129 DRM_ERROR("Process %d freeing buffer not owned\n",
1134 drm_free_buffer(dev, buf);
1136 spin_unlock(&dev->dma_lock);
1142 * Maps all of the DMA buffers into client-virtual space (ioctl).
1144 * \param inode device inode.
1145 * \param file_priv DRM file private.
1146 * \param cmd command.
1147 * \param arg pointer to a drm_buf_map structure.
1148 * \return zero on success or a negative number on failure.
1150 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1151 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1152 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1155 int drm_mapbufs(struct drm_device *dev, void *data,
1156 struct drm_file *file_priv)
1158 drm_device_dma_t *dma = dev->dma;
1161 vm_offset_t address;
1162 struct vmspace *vms;
1166 struct drm_buf_map *request = data;
1169 vms = DRM_CURPROC->td_proc->p_vmspace;
1171 spin_lock(&dev->dma_lock);
1172 dev->buf_use++; /* Can't allocate more after this call */
1173 spin_unlock(&dev->dma_lock);
1175 if (request->count < dma->buf_count)
1178 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
1179 (drm_core_check_feature(dev, DRIVER_SG) &&
1180 (dma->flags & _DRM_DMA_USE_SG))) {
1181 drm_local_map_t *map = dev->agp_buffer_map;
1187 size = round_page(map->size);
1188 foff = (unsigned long)map->handle;
1190 size = round_page(dma->byte_count),
1194 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
1195 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1196 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC,
1197 SLIST_FIRST(&dev->devnode->si_hlist), foff);
1201 request->virtual = (void *)vaddr;
1203 for (i = 0; i < dma->buf_count; i++) {
1204 if (DRM_COPY_TO_USER(&request->list[i].idx,
1205 &dma->buflist[i]->idx, sizeof(request->list[0].idx))) {
1209 if (DRM_COPY_TO_USER(&request->list[i].total,
1210 &dma->buflist[i]->total, sizeof(request->list[0].total))) {
1214 if (DRM_COPY_TO_USER(&request->list[i].used, &zero,
1219 address = vaddr + dma->buflist[i]->offset; /* *** */
1220 if (DRM_COPY_TO_USER(&request->list[i].address, &address,
1228 request->count = dma->buf_count;
1230 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1236 * Compute size order. Returns the exponent of the smaller power of two which
1237 * is greater or equal to given number.
1242 * \todo Can be made faster.
1244 int drm_order(unsigned long size)
1249 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1251 if (size & (size - 1))
1256 EXPORT_SYMBOL(drm_order);