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;
109 /* Allocate a new map structure, fill it in, and do any type-specific
110 * initialization necessary.
112 map = kmalloc(sizeof(*map), M_DRM, M_ZERO | M_WAITOK | M_NULLOK);
117 map->offset = offset;
122 /* Only allow shared memory to be removable since we only keep enough
123 * book keeping information about shared memory to allow for removal
124 * when processes fork.
126 if ((flags & _DRM_REMOVABLE) && type != _DRM_SHM) {
127 DRM_ERROR("Requested removable map for non-DRM_SHM\n");
128 drm_free(map, M_DRM);
131 if ((offset & PAGE_MASK) || (size & PAGE_MASK)) {
132 DRM_ERROR("offset/size not page aligned: 0x%jx/0x%04x\n",
133 (uintmax_t)offset, size);
134 drm_free(map, M_DRM);
137 if (offset + size < offset) {
138 DRM_ERROR("offset and size wrap around: 0x%jx/0x%04x\n",
139 (uintmax_t)offset, size);
140 drm_free(map, M_DRM);
144 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
145 (unsigned long long)map->offset, map->size, map->type);
147 /* Check if this is just another version of a kernel-allocated map, and
148 * just hand that back if so.
150 if (type == _DRM_REGISTERS || type == _DRM_FRAME_BUFFER ||
152 list_for_each_entry(entry, &dev->maplist, head) {
153 if (entry->map->type == type && (entry->map->offset == offset ||
154 (entry->map->type == _DRM_SHM &&
155 entry->map->flags == _DRM_CONTAINS_LOCK))) {
156 entry->map->size = size;
157 DRM_DEBUG("Found kernel map %d\n", type);
165 map->handle = drm_ioremap(dev, map);
166 if (!(map->flags & _DRM_WRITE_COMBINING))
169 case _DRM_FRAME_BUFFER:
170 if (drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC) == 0)
174 map->handle = kmalloc(map->size, M_DRM, M_WAITOK | M_NULLOK);
175 DRM_DEBUG("%lu %d %p\n",
176 map->size, drm_order(map->size), map->handle);
178 drm_free(map, M_DRM);
181 map->offset = (unsigned long)map->handle;
182 if (map->flags & _DRM_CONTAINS_LOCK) {
183 /* Prevent a 2nd X Server from creating a 2nd lock */
185 if (dev->lock.hw_lock != NULL) {
187 drm_free(map->handle, M_DRM);
188 drm_free(map, M_DRM);
191 dev->lock.hw_lock = map->handle; /* Pointer to lock */
197 /* In some cases (i810 driver), user space may have already
198 * added the AGP base itself, because dev->agp->base previously
199 * only got set during AGP enable. So, only add the base
200 * address if the map's offset isn't already within the
203 if (map->offset < dev->agp->base ||
204 map->offset > dev->agp->base +
205 dev->agp->agp_info.ai_aperture_size - 1) {
206 map->offset += dev->agp->base;
208 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
209 /*for (entry = dev->agp->memory; entry; entry = entry->next) {
210 if ((map->offset >= entry->bound) &&
211 (map->offset + map->size <=
212 entry->bound + entry->pages * PAGE_SIZE)) {
218 drm_free(map, M_DRM);
222 case _DRM_SCATTER_GATHER:
224 drm_free(map, M_DRM);
227 map->handle = (void *)(uintptr_t)(dev->sg->vaddr + offset);
228 map->offset = dev->sg->vaddr + offset;
230 case _DRM_CONSISTENT:
231 /* Unfortunately, we don't get any alignment specification from
232 * the caller, so we have to guess. drm_pci_alloc requires
233 * a power-of-two alignment, so try to align the bus address of
234 * the map to it size if possible, otherwise just assume
235 * PAGE_SIZE alignment.
238 if ((align & (align - 1)) != 0)
240 dmah = drm_pci_alloc(dev, map->size, align, 0xfffffffful);
242 drm_free(map, M_DRM);
245 map->handle = dmah->vaddr;
246 map->offset = (unsigned long)dmah->busaddr;
249 DRM_ERROR("Bad map type %d\n", map->type);
250 drm_free(map, M_DRM);
254 list_add(&entry->head, &dev->maplist);
257 /* Jumped to, with lock held, when a kernel map is found. */
259 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", map->type, map->offset,
268 * Ioctl to specify a range of memory that is available for mapping by a
271 * \param inode device inode.
272 * \param file_priv DRM file private.
273 * \param cmd command.
274 * \param arg pointer to a drm_map structure.
275 * \return zero on success or a negative value on error.
278 int drm_addmap_ioctl(struct drm_device *dev, void *data,
279 struct drm_file *file_priv)
281 struct drm_map *request = data;
282 drm_local_map_t *map;
285 if (!(dev->flags & (FREAD|FWRITE)))
286 return EACCES; /* Require read/write */
288 if (!DRM_SUSER(DRM_CURPROC) && request->type != _DRM_AGP)
292 err = drm_addmap(dev, request->offset, request->size, request->type,
293 request->flags, &map);
298 request->offset = map->offset;
299 request->size = map->size;
300 request->type = map->type;
301 request->flags = map->flags;
302 request->mtrr = map->mtrr;
303 request->handle = (void *)map->handle;
308 void drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
310 struct drm_map_list *r_list = NULL, *list_t;
311 drm_dma_handle_t dmah;
314 DRM_LOCK_ASSERT(dev);
319 /* Find the list entry for the map and remove it */
320 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
321 if (r_list->map == map) {
322 list_del(&r_list->head);
323 drm_free(r_list, M_DRM);
334 drm_ioremapfree(map);
336 case _DRM_FRAME_BUFFER:
338 int __unused retcode;
340 retcode = drm_mtrr_del(0, map->offset, map->size,
342 DRM_DEBUG("mtrr_del = %d\n", retcode);
346 drm_free(map->handle, M_DRM);
349 case _DRM_SCATTER_GATHER:
351 case _DRM_CONSISTENT:
352 dmah.vaddr = map->handle;
353 dmah.busaddr = map->offset;
354 drm_pci_free(dev, &dmah);
357 DRM_ERROR("Bad map type %d\n", map->type);
361 drm_free(map, M_DRM);
364 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
365 * the last close of the device, and this is necessary for cleanup when things
366 * exit uncleanly. Therefore, having userland manually remove mappings seems
367 * like a pointless exercise since they're going away anyway.
369 * One use case might be after addmap is allowed for normal users for SHM and
370 * gets used by drivers that the server doesn't need to care about. This seems
373 * \param inode device inode.
374 * \param file_priv DRM file private.
375 * \param cmd command.
376 * \param arg pointer to a struct drm_map structure.
377 * \return zero on success or a negative value on error.
379 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
380 struct drm_file *file_priv)
382 struct drm_map *request = data;
383 struct drm_local_map *map = NULL;
384 struct drm_map_list *r_list;
387 list_for_each_entry(r_list, &dev->maplist, head) {
389 r_list->user_token == (unsigned long)request->handle &&
390 r_list->map->flags & _DRM_REMOVABLE) {
396 /* List has wrapped around to the head pointer, or its empty we didn't
399 if (list_empty(&dev->maplist) || !map) {
404 /* Register and framebuffer maps are permanent */
405 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
418 * Cleanup after an error on one of the addbufs() functions.
420 * \param dev DRM device.
421 * \param entry buffer entry where the error occurred.
423 * Frees any pages and buffers associated with the given entry.
425 static void drm_cleanup_buf_error(struct drm_device * dev,
426 struct drm_buf_entry * entry)
430 if (entry->seg_count) {
431 for (i = 0; i < entry->seg_count; i++) {
432 drm_pci_free(dev, entry->seglist[i]);
434 drm_free(entry->seglist, M_DRM);
436 entry->seg_count = 0;
439 if (entry->buf_count) {
440 for (i = 0; i < entry->buf_count; i++) {
441 drm_free(entry->buflist[i].dev_private, M_DRM);
443 drm_free(entry->buflist, M_DRM);
445 entry->buf_count = 0;
449 static int drm_do_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
451 drm_device_dma_t *dma = dev->dma;
452 drm_buf_entry_t *entry;
453 /*drm_agp_mem_t *agp_entry;
456 unsigned long offset;
457 unsigned long agp_offset;
466 drm_buf_t **temp_buflist;
468 count = request->count;
469 order = drm_order(request->size);
472 alignment = (request->flags & _DRM_PAGE_ALIGN)
473 ? round_page(size) : size;
474 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
475 total = PAGE_SIZE << page_order;
478 agp_offset = dev->agp->base + request->agp_start;
480 DRM_DEBUG("count: %d\n", count);
481 DRM_DEBUG("order: %d\n", order);
482 DRM_DEBUG("size: %d\n", size);
483 DRM_DEBUG("agp_offset: 0x%lx\n", agp_offset);
484 DRM_DEBUG("alignment: %d\n", alignment);
485 DRM_DEBUG("page_order: %d\n", page_order);
486 DRM_DEBUG("total: %d\n", total);
488 /* Make sure buffers are located in AGP memory that we own */
489 /* Breaks MGA due to drm_alloc_agp not setting up entries for the
490 * memory. Safe to ignore for now because these ioctls are still
494 for (agp_entry = dev->agp->memory; agp_entry;
495 agp_entry = agp_entry->next) {
496 if ((agp_offset >= agp_entry->bound) &&
497 (agp_offset + total * count <=
498 agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
504 DRM_DEBUG("zone invalid\n");
508 entry = &dma->bufs[order];
510 entry->buflist = kmalloc(count * sizeof(*entry->buflist), M_DRM,
511 M_WAITOK | M_NULLOK | M_ZERO);
512 if (!entry->buflist) {
516 entry->buf_size = size;
517 entry->page_order = page_order;
521 while (entry->buf_count < count) {
522 buf = &entry->buflist[entry->buf_count];
523 buf->idx = dma->buf_count + entry->buf_count;
524 buf->total = alignment;
528 buf->offset = (dma->byte_count + offset);
529 buf->bus_address = agp_offset + offset;
530 buf->address = (void *)(agp_offset + offset);
533 buf->file_priv = NULL;
535 buf->dev_priv_size = dev->driver->buf_priv_size;
536 buf->dev_private = kmalloc(buf->dev_priv_size, M_DRM,
537 M_WAITOK | M_NULLOK | M_ZERO);
538 if (buf->dev_private == NULL) {
539 /* Set count correctly so we free the proper amount. */
540 entry->buf_count = count;
541 drm_cleanup_buf_error(dev, entry);
547 byte_count += PAGE_SIZE << page_order;
550 DRM_DEBUG("byte_count: %d\n", byte_count);
552 temp_buflist = krealloc(dma->buflist,
553 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
554 M_DRM, M_WAITOK | M_NULLOK);
555 if (temp_buflist == NULL) {
556 /* Free the entry because it isn't valid */
557 drm_cleanup_buf_error(dev, entry);
560 dma->buflist = temp_buflist;
562 for (i = 0; i < entry->buf_count; i++) {
563 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
566 dma->buf_count += entry->buf_count;
567 dma->byte_count += byte_count;
569 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
570 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
572 request->count = entry->buf_count;
573 request->size = size;
575 dma->flags = _DRM_DMA_USE_AGP;
580 static int drm_do_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
582 drm_device_dma_t *dma = dev->dma;
588 drm_buf_entry_t *entry;
591 unsigned long offset;
595 unsigned long *temp_pagelist;
596 drm_buf_t **temp_buflist;
598 count = request->count;
599 order = drm_order(request->size);
602 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
603 request->count, request->size, size, order);
605 alignment = (request->flags & _DRM_PAGE_ALIGN)
606 ? round_page(size) : size;
607 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
608 total = PAGE_SIZE << page_order;
610 entry = &dma->bufs[order];
612 entry->buflist = kmalloc(count * sizeof(*entry->buflist), M_DRM,
613 M_WAITOK | M_NULLOK | M_ZERO);
614 entry->seglist = kmalloc(count * sizeof(*entry->seglist), M_DRM,
615 M_WAITOK | M_NULLOK | M_ZERO);
617 /* Keep the original pagelist until we know all the allocations
620 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
621 sizeof(*dma->pagelist),
622 M_DRM, M_WAITOK | M_NULLOK);
624 if (entry->buflist == NULL || entry->seglist == NULL ||
625 temp_pagelist == NULL) {
626 drm_free(temp_pagelist, M_DRM);
627 drm_free(entry->seglist, M_DRM);
628 drm_free(entry->buflist, M_DRM);
632 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
633 sizeof(*dma->pagelist));
635 DRM_DEBUG("pagelist: %d entries\n",
636 dma->page_count + (count << page_order));
638 entry->buf_size = size;
639 entry->page_order = page_order;
643 while (entry->buf_count < count) {
644 spin_unlock(&dev->dma_lock);
645 drm_dma_handle_t *dmah = drm_pci_alloc(dev, size, alignment,
647 spin_lock(&dev->dma_lock);
649 /* Set count correctly so we free the proper amount. */
650 entry->buf_count = count;
651 entry->seg_count = count;
652 drm_cleanup_buf_error(dev, entry);
653 drm_free(temp_pagelist, M_DRM);
657 entry->seglist[entry->seg_count++] = dmah;
658 for (i = 0; i < (1 << page_order); i++) {
659 DRM_DEBUG("page %d @ %p\n",
660 dma->page_count + page_count,
661 (char *)dmah->vaddr + PAGE_SIZE * i);
662 temp_pagelist[dma->page_count + page_count++] =
663 (long)dmah->vaddr + PAGE_SIZE * i;
666 offset + size <= total && entry->buf_count < count;
667 offset += alignment, ++entry->buf_count) {
668 buf = &entry->buflist[entry->buf_count];
669 buf->idx = dma->buf_count + entry->buf_count;
670 buf->total = alignment;
673 buf->offset = (dma->byte_count + byte_count + offset);
674 buf->address = ((char *)dmah->vaddr + offset);
675 buf->bus_address = dmah->busaddr + offset;
678 buf->file_priv = NULL;
680 buf->dev_priv_size = dev->driver->buf_priv_size;
681 buf->dev_private = kmalloc(buf->dev_priv_size,
683 M_WAITOK | M_NULLOK |
685 if (buf->dev_private == NULL) {
686 /* Set count correctly so we free the proper amount. */
687 entry->buf_count = count;
688 entry->seg_count = count;
689 drm_cleanup_buf_error(dev, entry);
690 drm_free(temp_pagelist, M_DRM);
694 DRM_DEBUG("buffer %d @ %p\n",
695 entry->buf_count, buf->address);
697 byte_count += PAGE_SIZE << page_order;
700 temp_buflist = krealloc(dma->buflist,
701 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
702 M_DRM, M_WAITOK | M_NULLOK);
703 if (temp_buflist == NULL) {
704 /* Free the entry because it isn't valid */
705 drm_cleanup_buf_error(dev, entry);
706 drm_free(temp_pagelist, M_DRM);
709 dma->buflist = temp_buflist;
711 for (i = 0; i < entry->buf_count; i++) {
712 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
715 /* No allocations failed, so now we can replace the orginal pagelist
718 drm_free(dma->pagelist, M_DRM);
719 dma->pagelist = temp_pagelist;
721 dma->buf_count += entry->buf_count;
722 dma->seg_count += entry->seg_count;
723 dma->page_count += entry->seg_count << page_order;
724 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
726 request->count = entry->buf_count;
727 request->size = size;
733 static int drm_do_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
735 drm_device_dma_t *dma = dev->dma;
736 drm_buf_entry_t *entry;
738 unsigned long offset;
739 unsigned long agp_offset;
748 drm_buf_t **temp_buflist;
750 count = request->count;
751 order = drm_order(request->size);
754 alignment = (request->flags & _DRM_PAGE_ALIGN)
755 ? round_page(size) : size;
756 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
757 total = PAGE_SIZE << page_order;
760 agp_offset = request->agp_start;
762 DRM_DEBUG("count: %d\n", count);
763 DRM_DEBUG("order: %d\n", order);
764 DRM_DEBUG("size: %d\n", size);
765 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
766 DRM_DEBUG("alignment: %d\n", alignment);
767 DRM_DEBUG("page_order: %d\n", page_order);
768 DRM_DEBUG("total: %d\n", total);
770 entry = &dma->bufs[order];
772 entry->buflist = kmalloc(count * sizeof(*entry->buflist), M_DRM,
773 M_WAITOK | M_NULLOK | M_ZERO);
774 if (entry->buflist == NULL)
777 entry->buf_size = size;
778 entry->page_order = page_order;
782 while (entry->buf_count < count) {
783 buf = &entry->buflist[entry->buf_count];
784 buf->idx = dma->buf_count + entry->buf_count;
785 buf->total = alignment;
789 buf->offset = (dma->byte_count + offset);
790 buf->bus_address = agp_offset + offset;
791 buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
794 buf->file_priv = NULL;
796 buf->dev_priv_size = dev->driver->buf_priv_size;
797 buf->dev_private = kmalloc(buf->dev_priv_size, M_DRM,
798 M_WAITOK | M_NULLOK | M_ZERO);
799 if (buf->dev_private == NULL) {
800 /* Set count correctly so we free the proper amount. */
801 entry->buf_count = count;
802 drm_cleanup_buf_error(dev, entry);
806 DRM_DEBUG("buffer %d @ %p\n",
807 entry->buf_count, buf->address);
811 byte_count += PAGE_SIZE << page_order;
814 DRM_DEBUG("byte_count: %d\n", byte_count);
816 temp_buflist = krealloc(dma->buflist,
817 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
818 M_DRM, M_WAITOK | M_NULLOK);
819 if (temp_buflist == NULL) {
820 /* Free the entry because it isn't valid */
821 drm_cleanup_buf_error(dev, entry);
824 dma->buflist = temp_buflist;
826 for (i = 0; i < entry->buf_count; i++) {
827 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
830 dma->buf_count += entry->buf_count;
831 dma->byte_count += byte_count;
833 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
834 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
836 request->count = entry->buf_count;
837 request->size = size;
839 dma->flags = _DRM_DMA_USE_SG;
845 * Add AGP buffers for DMA transfers.
847 * \param dev struct drm_device to which the buffers are to be added.
848 * \param request pointer to a struct drm_buf_desc describing the request.
849 * \return zero on success or a negative number on failure.
851 * After some sanity checks creates a drm_buf structure for each buffer and
852 * reallocates the buffer list of the same size order to accommodate the new
855 int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
859 if (request->count < 0 || request->count > 4096)
862 order = drm_order(request->size);
863 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
866 spin_lock(&dev->dma_lock);
868 /* No more allocations after first buffer-using ioctl. */
869 if (dev->buf_use != 0) {
870 spin_unlock(&dev->dma_lock);
873 /* No more than one allocation per order */
874 if (dev->dma->bufs[order].buf_count != 0) {
875 spin_unlock(&dev->dma_lock);
879 ret = drm_do_addbufs_agp(dev, request);
881 spin_unlock(&dev->dma_lock);
886 static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
890 if (!DRM_SUSER(DRM_CURPROC))
893 if (request->count < 0 || request->count > 4096)
896 order = drm_order(request->size);
897 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
900 spin_lock(&dev->dma_lock);
902 /* No more allocations after first buffer-using ioctl. */
903 if (dev->buf_use != 0) {
904 spin_unlock(&dev->dma_lock);
907 /* No more than one allocation per order */
908 if (dev->dma->bufs[order].buf_count != 0) {
909 spin_unlock(&dev->dma_lock);
913 ret = drm_do_addbufs_sg(dev, request);
915 spin_unlock(&dev->dma_lock);
920 int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
924 if (!DRM_SUSER(DRM_CURPROC))
927 if (request->count < 0 || request->count > 4096)
930 order = drm_order(request->size);
931 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
934 spin_lock(&dev->dma_lock);
936 /* No more allocations after first buffer-using ioctl. */
937 if (dev->buf_use != 0) {
938 spin_unlock(&dev->dma_lock);
941 /* No more than one allocation per order */
942 if (dev->dma->bufs[order].buf_count != 0) {
943 spin_unlock(&dev->dma_lock);
947 ret = drm_do_addbufs_pci(dev, request);
949 spin_unlock(&dev->dma_lock);
955 * Add buffers for DMA transfers (ioctl).
957 * \param inode device inode.
958 * \param file_priv DRM file private.
959 * \param cmd command.
960 * \param arg pointer to a struct drm_buf_desc request.
961 * \return zero on success or a negative number on failure.
963 * According with the memory type specified in drm_buf_desc::flags and the
964 * build options, it dispatches the call either to addbufs_agp(),
965 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
966 * PCI memory respectively.
968 int drm_addbufs(struct drm_device *dev, void *data,
969 struct drm_file *file_priv)
971 struct drm_buf_desc *request = data;
974 if (request->flags & _DRM_AGP_BUFFER)
975 err = drm_addbufs_agp(dev, request);
976 else if (request->flags & _DRM_SG_BUFFER)
977 err = drm_addbufs_sg(dev, request);
979 err = drm_addbufs_pci(dev, request);
985 * Get information about the buffer mappings.
987 * This was originally mean for debugging purposes, or by a sophisticated
988 * client library to determine how best to use the available buffers (e.g.,
989 * large buffers can be used for image transfer).
991 * \param inode device inode.
992 * \param file_priv DRM file private.
993 * \param cmd command.
994 * \param arg pointer to a drm_buf_info structure.
995 * \return zero on success or a negative number on failure.
997 * Increments drm_device::buf_use while holding the drm_device::count_lock
998 * lock, preventing of allocating more buffers after this call. Information
999 * about each requested buffer is then copied into user space.
1001 int drm_infobufs(struct drm_device *dev, void *data,
1002 struct drm_file *file_priv)
1004 drm_device_dma_t *dma = dev->dma;
1005 struct drm_buf_info *request = data;
1010 spin_lock(&dev->dma_lock);
1011 ++dev->buf_use; /* Can't allocate more after this call */
1012 spin_unlock(&dev->dma_lock);
1014 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1015 if (dma->bufs[i].buf_count)
1019 DRM_DEBUG("count = %d\n", count);
1021 if (request->count >= count) {
1022 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1023 if (dma->bufs[i].buf_count) {
1024 struct drm_buf_desc from;
1026 from.count = dma->bufs[i].buf_count;
1027 from.size = dma->bufs[i].buf_size;
1028 from.low_mark = dma->bufs[i].freelist.low_mark;
1029 from.high_mark = dma->bufs[i].freelist.high_mark;
1031 if (DRM_COPY_TO_USER(&request->list[count], &from,
1032 sizeof(struct drm_buf_desc)) != 0) {
1037 DRM_DEBUG("%d %d %d %d %d\n",
1038 i, dma->bufs[i].buf_count,
1039 dma->bufs[i].buf_size,
1040 dma->bufs[i].freelist.low_mark,
1041 dma->bufs[i].freelist.high_mark);
1046 request->count = count;
1052 * Specifies a low and high water mark for buffer allocation
1054 * \param inode device inode.
1055 * \param file_priv DRM file private.
1056 * \param cmd command.
1057 * \param arg a pointer to a drm_buf_desc structure.
1058 * \return zero on success or a negative number on failure.
1060 * Verifies that the size order is bounded between the admissible orders and
1061 * updates the respective drm_device_dma::bufs entry low and high water mark.
1063 * \note This ioctl is deprecated and mostly never used.
1065 int drm_markbufs(struct drm_device *dev, void *data,
1066 struct drm_file *file_priv)
1068 drm_device_dma_t *dma = dev->dma;
1069 struct drm_buf_desc *request = data;
1072 DRM_DEBUG("%d, %d, %d\n",
1073 request->size, request->low_mark, request->high_mark);
1076 order = drm_order(request->size);
1077 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
1078 request->low_mark < 0 || request->high_mark < 0) {
1082 spin_lock(&dev->dma_lock);
1083 if (request->low_mark > dma->bufs[order].buf_count ||
1084 request->high_mark > dma->bufs[order].buf_count) {
1085 spin_unlock(&dev->dma_lock);
1089 dma->bufs[order].freelist.low_mark = request->low_mark;
1090 dma->bufs[order].freelist.high_mark = request->high_mark;
1091 spin_unlock(&dev->dma_lock);
1097 * Unreserve the buffers in list, previously reserved using drmDMA.
1099 * \param inode device inode.
1100 * \param file_priv DRM file private.
1101 * \param cmd command.
1102 * \param arg pointer to a drm_buf_free structure.
1103 * \return zero on success or a negative number on failure.
1105 * Calls free_buffer() for each used buffer.
1106 * This function is primarily used for debugging.
1108 int drm_freebufs(struct drm_device *dev, void *data,
1109 struct drm_file *file_priv)
1111 drm_device_dma_t *dma = dev->dma;
1112 struct drm_buf_free *request = data;
1118 DRM_DEBUG("%d\n", request->count);
1120 spin_lock(&dev->dma_lock);
1121 for (i = 0; i < request->count; i++) {
1122 if (DRM_COPY_FROM_USER(&idx, &request->list[i], sizeof(idx))) {
1126 if (idx < 0 || idx >= dma->buf_count) {
1127 DRM_ERROR("Index %d (of %d max)\n",
1128 idx, dma->buf_count - 1);
1132 buf = dma->buflist[idx];
1133 if (buf->file_priv != file_priv) {
1134 DRM_ERROR("Process %d freeing buffer not owned\n",
1139 drm_free_buffer(dev, buf);
1141 spin_unlock(&dev->dma_lock);
1147 * Maps all of the DMA buffers into client-virtual space (ioctl).
1149 * \param inode device inode.
1150 * \param file_priv DRM file private.
1151 * \param cmd command.
1152 * \param arg pointer to a drm_buf_map structure.
1153 * \return zero on success or a negative number on failure.
1155 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1156 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1157 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1160 int drm_mapbufs(struct drm_device *dev, void *data,
1161 struct drm_file *file_priv)
1163 drm_device_dma_t *dma = dev->dma;
1166 vm_offset_t address;
1167 struct vmspace *vms;
1171 struct drm_buf_map *request = data;
1174 vms = DRM_CURPROC->td_proc->p_vmspace;
1176 spin_lock(&dev->dma_lock);
1177 dev->buf_use++; /* Can't allocate more after this call */
1178 spin_unlock(&dev->dma_lock);
1180 if (request->count < dma->buf_count)
1183 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
1184 (drm_core_check_feature(dev, DRIVER_SG) &&
1185 (dma->flags & _DRM_DMA_USE_SG))) {
1186 drm_local_map_t *map = dev->agp_buffer_map;
1192 size = round_page(map->size);
1193 foff = (unsigned long)map->handle;
1195 size = round_page(dma->byte_count),
1199 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
1200 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1201 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC,
1202 SLIST_FIRST(&dev->devnode->si_hlist), foff);
1206 request->virtual = (void *)vaddr;
1208 for (i = 0; i < dma->buf_count; i++) {
1209 if (DRM_COPY_TO_USER(&request->list[i].idx,
1210 &dma->buflist[i]->idx, sizeof(request->list[0].idx))) {
1214 if (DRM_COPY_TO_USER(&request->list[i].total,
1215 &dma->buflist[i]->total, sizeof(request->list[0].total))) {
1219 if (DRM_COPY_TO_USER(&request->list[i].used, &zero,
1224 address = vaddr + dma->buflist[i]->offset; /* *** */
1225 if (DRM_COPY_TO_USER(&request->list[i].address, &address,
1233 request->count = dma->buf_count;
1235 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1241 * Compute size order. Returns the exponent of the smaller power of two which
1242 * is greater or equal to given number.
1247 * \todo Can be made faster.
1249 int drm_order(unsigned long size)
1254 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1256 if (size & (size - 1))
1261 EXPORT_SYMBOL(drm_order);