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;
108 /* Allocate a new map structure, fill it in, and do any type-specific
109 * initialization necessary.
111 map = kmalloc(sizeof(*map), DRM_MEM_MAPS, M_ZERO | M_NOWAIT);
116 map->offset = offset;
120 map->handle = (void *)((unsigned long)alloc_unr(dev->map_unrhdr) <<
121 DRM_MAP_HANDLE_SHIFT);
123 /* Only allow shared memory to be removable since we only keep enough
124 * book keeping information about shared memory to allow for removal
125 * when processes fork.
127 if ((flags & _DRM_REMOVABLE) && type != _DRM_SHM) {
128 DRM_ERROR("Requested removable map for non-DRM_SHM\n");
129 drm_free(map, DRM_MEM_MAPS);
132 if ((offset & PAGE_MASK) || (size & PAGE_MASK)) {
133 DRM_ERROR("offset/size not page aligned: 0x%jx/0x%04x\n",
134 (uintmax_t)offset, size);
135 drm_free(map, DRM_MEM_MAPS);
138 if (offset + size < offset) {
139 DRM_ERROR("offset and size wrap around: 0x%jx/0x%04x\n",
140 (uintmax_t)offset, size);
141 drm_free(map, DRM_MEM_MAPS);
145 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
146 (unsigned long long)map->offset, map->size, map->type);
148 /* Check if this is just another version of a kernel-allocated map, and
149 * just hand that back if so.
151 if (type == _DRM_REGISTERS || type == _DRM_FRAME_BUFFER ||
153 list_for_each_entry(entry, &dev->maplist, head) {
154 if (entry->map->type == type && (entry->map->offset == offset ||
155 (entry->map->type == _DRM_SHM &&
156 entry->map->flags == _DRM_CONTAINS_LOCK))) {
157 entry->map->size = size;
158 DRM_DEBUG("Found kernel map %d\n", type);
166 map->virtual = drm_ioremap(dev, map);
167 if (!(map->flags & _DRM_WRITE_COMBINING))
170 case _DRM_FRAME_BUFFER:
171 if (drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC) == 0)
175 map->virtual = kmalloc(map->size, DRM_MEM_MAPS, M_NOWAIT);
176 DRM_DEBUG("%lu %d %p\n",
177 map->size, drm_order(map->size), map->virtual);
179 drm_free(map, DRM_MEM_MAPS);
182 map->offset = (unsigned long)map->virtual;
183 if (map->flags & _DRM_CONTAINS_LOCK) {
184 /* Prevent a 2nd X Server from creating a 2nd lock */
186 if (dev->lock.hw_lock != NULL) {
188 drm_free(map->virtual, DRM_MEM_MAPS);
189 drm_free(map, DRM_MEM_MAPS);
192 dev->lock.hw_lock = map->virtual; /* Pointer to lock */
198 /* In some cases (i810 driver), user space may have already
199 * added the AGP base itself, because dev->agp->base previously
200 * only got set during AGP enable. So, only add the base
201 * address if the map's offset isn't already within the
204 if (map->offset < dev->agp->base ||
205 map->offset > dev->agp->base +
206 dev->agp->agp_info.ai_aperture_size - 1) {
207 map->offset += dev->agp->base;
209 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
210 /*for (entry = dev->agp->memory; entry; entry = entry->next) {
211 if ((map->offset >= entry->bound) &&
212 (map->offset + map->size <=
213 entry->bound + entry->pages * PAGE_SIZE)) {
219 drm_free(map, DRM_MEM_MAPS);
223 case _DRM_SCATTER_GATHER:
225 drm_free(map, DRM_MEM_MAPS);
228 map->virtual = (void *)(uintptr_t)(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);
246 map->virtual = map->dmah->vaddr;
247 map->offset = map->dmah->busaddr;
250 DRM_ERROR("Bad map type %d\n", map->type);
251 drm_free(map, DRM_MEM_MAPS);
255 list_add(&entry->head, &dev->maplist);
258 /* Jumped to, with lock held, when a kernel map is found. */
260 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", map->type, map->offset,
269 * Ioctl to specify a range of memory that is available for mapping by a
272 * \param inode device inode.
273 * \param file_priv DRM file private.
274 * \param cmd command.
275 * \param arg pointer to a drm_map structure.
276 * \return zero on success or a negative value on error.
279 int drm_addmap_ioctl(struct drm_device *dev, void *data,
280 struct drm_file *file_priv)
282 struct drm_map *request = data;
283 drm_local_map_t *map;
286 if (!(dev->flags & (FREAD|FWRITE)))
287 return EACCES; /* Require read/write */
289 if (!DRM_SUSER(DRM_CURPROC) && request->type != _DRM_AGP)
293 err = drm_addmap(dev, request->offset, request->size, request->type,
294 request->flags, &map);
299 request->offset = map->offset;
300 request->size = map->size;
301 request->type = map->type;
302 request->flags = map->flags;
303 request->mtrr = map->mtrr;
304 request->handle = (void *)map->handle;
309 void drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
311 struct drm_map_list *r_list = NULL, *list_t;
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, DRM_MEM_DRIVER);
334 if (map->bsr == NULL)
335 drm_ioremapfree(map);
337 case _DRM_FRAME_BUFFER:
339 int __unused retcode;
341 retcode = drm_mtrr_del(0, map->offset, map->size,
343 DRM_DEBUG("mtrr_del = %d\n", retcode);
347 drm_free(map->virtual, DRM_MEM_MAPS);
350 case _DRM_SCATTER_GATHER:
352 case _DRM_CONSISTENT:
353 drm_pci_free(dev, map->dmah);
356 DRM_ERROR("Bad map type %d\n", map->type);
360 if (map->bsr != NULL) {
361 bus_release_resource(dev->dev, SYS_RES_MEMORY, map->rid,
367 free_unr(dev->map_unrhdr, (unsigned long)map->handle >>
368 DRM_MAP_HANDLE_SHIFT);
371 drm_free(map, DRM_MEM_MAPS);
374 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
375 * the last close of the device, and this is necessary for cleanup when things
376 * exit uncleanly. Therefore, having userland manually remove mappings seems
377 * like a pointless exercise since they're going away anyway.
379 * One use case might be after addmap is allowed for normal users for SHM and
380 * gets used by drivers that the server doesn't need to care about. This seems
383 * \param inode device inode.
384 * \param file_priv DRM file private.
385 * \param cmd command.
386 * \param arg pointer to a struct drm_map structure.
387 * \return zero on success or a negative value on error.
389 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
390 struct drm_file *file_priv)
392 struct drm_map *request = data;
393 struct drm_local_map *map = NULL;
394 struct drm_map_list *r_list;
397 list_for_each_entry(r_list, &dev->maplist, head) {
399 r_list->user_token == (unsigned long)request->handle &&
400 r_list->map->flags & _DRM_REMOVABLE) {
406 /* List has wrapped around to the head pointer, or its empty we didn't
409 if (list_empty(&dev->maplist) || !map) {
414 /* Register and framebuffer maps are permanent */
415 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
428 * Cleanup after an error on one of the addbufs() functions.
430 * \param dev DRM device.
431 * \param entry buffer entry where the error occurred.
433 * Frees any pages and buffers associated with the given entry.
435 static void drm_cleanup_buf_error(struct drm_device * dev,
436 struct drm_buf_entry * entry)
440 if (entry->seg_count) {
441 for (i = 0; i < entry->seg_count; i++) {
442 drm_pci_free(dev, entry->seglist[i]);
444 drm_free(entry->seglist, DRM_MEM_SEGS);
446 entry->seg_count = 0;
449 if (entry->buf_count) {
450 for (i = 0; i < entry->buf_count; i++) {
451 drm_free(entry->buflist[i].dev_private, DRM_MEM_BUFS);
453 drm_free(entry->buflist, DRM_MEM_BUFS);
455 entry->buf_count = 0;
459 static int drm_do_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
461 drm_device_dma_t *dma = dev->dma;
462 drm_buf_entry_t *entry;
463 /*drm_agp_mem_t *agp_entry;
466 unsigned long offset;
467 unsigned long agp_offset;
476 drm_buf_t **temp_buflist;
478 count = request->count;
479 order = drm_order(request->size);
482 alignment = (request->flags & _DRM_PAGE_ALIGN)
483 ? round_page(size) : size;
484 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
485 total = PAGE_SIZE << page_order;
488 agp_offset = dev->agp->base + request->agp_start;
490 DRM_DEBUG("count: %d\n", count);
491 DRM_DEBUG("order: %d\n", order);
492 DRM_DEBUG("size: %d\n", size);
493 DRM_DEBUG("agp_offset: 0x%lx\n", agp_offset);
494 DRM_DEBUG("alignment: %d\n", alignment);
495 DRM_DEBUG("page_order: %d\n", page_order);
496 DRM_DEBUG("total: %d\n", total);
498 /* Make sure buffers are located in AGP memory that we own */
499 /* Breaks MGA due to drm_alloc_agp not setting up entries for the
500 * memory. Safe to ignore for now because these ioctls are still
504 for (agp_entry = dev->agp->memory; agp_entry;
505 agp_entry = agp_entry->next) {
506 if ((agp_offset >= agp_entry->bound) &&
507 (agp_offset + total * count <=
508 agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
514 DRM_DEBUG("zone invalid\n");
518 entry = &dma->bufs[order];
520 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
522 if (!entry->buflist) {
526 entry->buf_size = size;
527 entry->page_order = page_order;
531 while (entry->buf_count < count) {
532 buf = &entry->buflist[entry->buf_count];
533 buf->idx = dma->buf_count + entry->buf_count;
534 buf->total = alignment;
538 buf->offset = (dma->byte_count + offset);
539 buf->bus_address = agp_offset + offset;
540 buf->address = (void *)(agp_offset + offset);
543 buf->file_priv = NULL;
545 buf->dev_priv_size = dev->driver->buf_priv_size;
546 buf->dev_private = kmalloc(buf->dev_priv_size, DRM_MEM_BUFS,
548 if (buf->dev_private == NULL) {
549 /* Set count correctly so we free the proper amount. */
550 entry->buf_count = count;
551 drm_cleanup_buf_error(dev, entry);
557 byte_count += PAGE_SIZE << page_order;
560 DRM_DEBUG("byte_count: %d\n", byte_count);
562 temp_buflist = krealloc(dma->buflist,
563 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
564 DRM_MEM_BUFS, M_NOWAIT);
565 if (temp_buflist == NULL) {
566 /* Free the entry because it isn't valid */
567 drm_cleanup_buf_error(dev, entry);
570 dma->buflist = temp_buflist;
572 for (i = 0; i < entry->buf_count; i++) {
573 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
576 dma->buf_count += entry->buf_count;
577 dma->byte_count += byte_count;
579 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
580 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
582 request->count = entry->buf_count;
583 request->size = size;
585 dma->flags = _DRM_DMA_USE_AGP;
590 static int drm_do_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
592 drm_device_dma_t *dma = dev->dma;
598 drm_buf_entry_t *entry;
601 unsigned long offset;
605 unsigned long *temp_pagelist;
606 drm_buf_t **temp_buflist;
608 count = request->count;
609 order = drm_order(request->size);
612 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
613 request->count, request->size, size, order);
615 alignment = (request->flags & _DRM_PAGE_ALIGN)
616 ? round_page(size) : size;
617 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
618 total = PAGE_SIZE << page_order;
620 entry = &dma->bufs[order];
622 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
624 entry->seglist = kmalloc(count * sizeof(*entry->seglist), DRM_MEM_SEGS,
627 /* Keep the original pagelist until we know all the allocations
630 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
631 sizeof(*dma->pagelist), DRM_MEM_PAGES, M_NOWAIT);
633 if (entry->buflist == NULL || entry->seglist == NULL ||
634 temp_pagelist == NULL) {
635 drm_free(temp_pagelist, DRM_MEM_PAGES);
636 drm_free(entry->seglist, DRM_MEM_SEGS);
637 drm_free(entry->buflist, DRM_MEM_BUFS);
641 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
642 sizeof(*dma->pagelist));
644 DRM_DEBUG("pagelist: %d entries\n",
645 dma->page_count + (count << page_order));
647 entry->buf_size = size;
648 entry->page_order = page_order;
652 while (entry->buf_count < count) {
653 spin_unlock(&dev->dma_lock);
654 drm_dma_handle_t *dmah = drm_pci_alloc(dev, size, alignment,
656 spin_lock(&dev->dma_lock);
658 /* Set count correctly so we free the proper amount. */
659 entry->buf_count = count;
660 entry->seg_count = count;
661 drm_cleanup_buf_error(dev, entry);
662 drm_free(temp_pagelist, DRM_MEM_PAGES);
666 entry->seglist[entry->seg_count++] = dmah;
667 for (i = 0; i < (1 << page_order); i++) {
668 DRM_DEBUG("page %d @ %p\n",
669 dma->page_count + page_count,
670 (char *)dmah->vaddr + PAGE_SIZE * i);
671 temp_pagelist[dma->page_count + page_count++] =
672 (long)dmah->vaddr + PAGE_SIZE * i;
675 offset + size <= total && entry->buf_count < count;
676 offset += alignment, ++entry->buf_count) {
677 buf = &entry->buflist[entry->buf_count];
678 buf->idx = dma->buf_count + entry->buf_count;
679 buf->total = alignment;
682 buf->offset = (dma->byte_count + byte_count + offset);
683 buf->address = ((char *)dmah->vaddr + offset);
684 buf->bus_address = dmah->busaddr + offset;
687 buf->file_priv = NULL;
689 buf->dev_priv_size = dev->driver->buf_priv_size;
690 buf->dev_private = kmalloc(buf->dev_priv_size,
691 DRM_MEM_BUFS, M_NOWAIT | M_ZERO);
692 if (buf->dev_private == NULL) {
693 /* Set count correctly so we free the proper amount. */
694 entry->buf_count = count;
695 entry->seg_count = count;
696 drm_cleanup_buf_error(dev, entry);
697 drm_free(temp_pagelist, DRM_MEM_PAGES);
701 DRM_DEBUG("buffer %d @ %p\n",
702 entry->buf_count, buf->address);
704 byte_count += PAGE_SIZE << page_order;
707 temp_buflist = krealloc(dma->buflist,
708 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
709 DRM_MEM_BUFS, M_NOWAIT);
710 if (temp_buflist == NULL) {
711 /* Free the entry because it isn't valid */
712 drm_cleanup_buf_error(dev, entry);
713 drm_free(temp_pagelist, DRM_MEM_PAGES);
716 dma->buflist = temp_buflist;
718 for (i = 0; i < entry->buf_count; i++) {
719 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
722 /* No allocations failed, so now we can replace the orginal pagelist
725 drm_free(dma->pagelist, DRM_MEM_PAGES);
726 dma->pagelist = temp_pagelist;
728 dma->buf_count += entry->buf_count;
729 dma->seg_count += entry->seg_count;
730 dma->page_count += entry->seg_count << page_order;
731 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
733 request->count = entry->buf_count;
734 request->size = size;
740 static int drm_do_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
742 drm_device_dma_t *dma = dev->dma;
743 drm_buf_entry_t *entry;
745 unsigned long offset;
746 unsigned long agp_offset;
755 drm_buf_t **temp_buflist;
757 count = request->count;
758 order = drm_order(request->size);
761 alignment = (request->flags & _DRM_PAGE_ALIGN)
762 ? round_page(size) : size;
763 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
764 total = PAGE_SIZE << page_order;
767 agp_offset = request->agp_start;
769 DRM_DEBUG("count: %d\n", count);
770 DRM_DEBUG("order: %d\n", order);
771 DRM_DEBUG("size: %d\n", size);
772 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
773 DRM_DEBUG("alignment: %d\n", alignment);
774 DRM_DEBUG("page_order: %d\n", page_order);
775 DRM_DEBUG("total: %d\n", total);
777 entry = &dma->bufs[order];
779 entry->buflist = kmalloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
781 if (entry->buflist == NULL)
784 entry->buf_size = size;
785 entry->page_order = page_order;
789 while (entry->buf_count < count) {
790 buf = &entry->buflist[entry->buf_count];
791 buf->idx = dma->buf_count + entry->buf_count;
792 buf->total = alignment;
796 buf->offset = (dma->byte_count + offset);
797 buf->bus_address = agp_offset + offset;
798 buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
801 buf->file_priv = NULL;
803 buf->dev_priv_size = dev->driver->buf_priv_size;
804 buf->dev_private = kmalloc(buf->dev_priv_size, DRM_MEM_BUFS,
806 if (buf->dev_private == NULL) {
807 /* Set count correctly so we free the proper amount. */
808 entry->buf_count = count;
809 drm_cleanup_buf_error(dev, entry);
813 DRM_DEBUG("buffer %d @ %p\n",
814 entry->buf_count, buf->address);
818 byte_count += PAGE_SIZE << page_order;
821 DRM_DEBUG("byte_count: %d\n", byte_count);
823 temp_buflist = krealloc(dma->buflist,
824 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
825 DRM_MEM_BUFS, M_NOWAIT);
826 if (temp_buflist == NULL) {
827 /* Free the entry because it isn't valid */
828 drm_cleanup_buf_error(dev, entry);
831 dma->buflist = temp_buflist;
833 for (i = 0; i < entry->buf_count; i++) {
834 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
837 dma->buf_count += entry->buf_count;
838 dma->byte_count += byte_count;
840 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
841 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
843 request->count = entry->buf_count;
844 request->size = size;
846 dma->flags = _DRM_DMA_USE_SG;
852 * Add AGP buffers for DMA transfers.
854 * \param dev struct drm_device to which the buffers are to be added.
855 * \param request pointer to a struct drm_buf_desc describing the request.
856 * \return zero on success or a negative number on failure.
858 * After some sanity checks creates a drm_buf structure for each buffer and
859 * reallocates the buffer list of the same size order to accommodate the new
862 int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
866 if (request->count < 0 || request->count > 4096)
869 order = drm_order(request->size);
870 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
873 spin_lock(&dev->dma_lock);
875 /* No more allocations after first buffer-using ioctl. */
876 if (dev->buf_use != 0) {
877 spin_unlock(&dev->dma_lock);
880 /* No more than one allocation per order */
881 if (dev->dma->bufs[order].buf_count != 0) {
882 spin_unlock(&dev->dma_lock);
886 ret = drm_do_addbufs_agp(dev, request);
888 spin_unlock(&dev->dma_lock);
893 static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
897 if (!DRM_SUSER(DRM_CURPROC))
900 if (request->count < 0 || request->count > 4096)
903 order = drm_order(request->size);
904 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
907 spin_lock(&dev->dma_lock);
909 /* No more allocations after first buffer-using ioctl. */
910 if (dev->buf_use != 0) {
911 spin_unlock(&dev->dma_lock);
914 /* No more than one allocation per order */
915 if (dev->dma->bufs[order].buf_count != 0) {
916 spin_unlock(&dev->dma_lock);
920 ret = drm_do_addbufs_sg(dev, request);
922 spin_unlock(&dev->dma_lock);
927 int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
931 if (!DRM_SUSER(DRM_CURPROC))
934 if (request->count < 0 || request->count > 4096)
937 order = drm_order(request->size);
938 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
941 spin_lock(&dev->dma_lock);
943 /* No more allocations after first buffer-using ioctl. */
944 if (dev->buf_use != 0) {
945 spin_unlock(&dev->dma_lock);
948 /* No more than one allocation per order */
949 if (dev->dma->bufs[order].buf_count != 0) {
950 spin_unlock(&dev->dma_lock);
954 ret = drm_do_addbufs_pci(dev, request);
956 spin_unlock(&dev->dma_lock);
962 * Add buffers for DMA transfers (ioctl).
964 * \param inode device inode.
965 * \param file_priv DRM file private.
966 * \param cmd command.
967 * \param arg pointer to a struct drm_buf_desc request.
968 * \return zero on success or a negative number on failure.
970 * According with the memory type specified in drm_buf_desc::flags and the
971 * build options, it dispatches the call either to addbufs_agp(),
972 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
973 * PCI memory respectively.
975 int drm_addbufs(struct drm_device *dev, void *data,
976 struct drm_file *file_priv)
978 struct drm_buf_desc *request = data;
981 if (request->flags & _DRM_AGP_BUFFER)
982 err = drm_addbufs_agp(dev, request);
983 else if (request->flags & _DRM_SG_BUFFER)
984 err = drm_addbufs_sg(dev, request);
986 err = drm_addbufs_pci(dev, request);
992 * Get information about the buffer mappings.
994 * This was originally mean for debugging purposes, or by a sophisticated
995 * client library to determine how best to use the available buffers (e.g.,
996 * large buffers can be used for image transfer).
998 * \param inode device inode.
999 * \param file_priv DRM file private.
1000 * \param cmd command.
1001 * \param arg pointer to a drm_buf_info structure.
1002 * \return zero on success or a negative number on failure.
1004 * Increments drm_device::buf_use while holding the drm_device::count_lock
1005 * lock, preventing of allocating more buffers after this call. Information
1006 * about each requested buffer is then copied into user space.
1008 int drm_infobufs(struct drm_device *dev, void *data,
1009 struct drm_file *file_priv)
1011 drm_device_dma_t *dma = dev->dma;
1012 struct drm_buf_info *request = data;
1017 spin_lock(&dev->dma_lock);
1018 ++dev->buf_use; /* Can't allocate more after this call */
1019 spin_unlock(&dev->dma_lock);
1021 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1022 if (dma->bufs[i].buf_count)
1026 DRM_DEBUG("count = %d\n", count);
1028 if (request->count >= count) {
1029 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1030 if (dma->bufs[i].buf_count) {
1031 struct drm_buf_desc from;
1033 from.count = dma->bufs[i].buf_count;
1034 from.size = dma->bufs[i].buf_size;
1035 from.low_mark = dma->bufs[i].freelist.low_mark;
1036 from.high_mark = dma->bufs[i].freelist.high_mark;
1038 if (DRM_COPY_TO_USER(&request->list[count], &from,
1039 sizeof(struct drm_buf_desc)) != 0) {
1044 DRM_DEBUG("%d %d %d %d %d\n",
1045 i, dma->bufs[i].buf_count,
1046 dma->bufs[i].buf_size,
1047 dma->bufs[i].freelist.low_mark,
1048 dma->bufs[i].freelist.high_mark);
1053 request->count = count;
1059 * Specifies a low and high water mark for buffer allocation
1061 * \param inode device inode.
1062 * \param file_priv DRM file private.
1063 * \param cmd command.
1064 * \param arg a pointer to a drm_buf_desc structure.
1065 * \return zero on success or a negative number on failure.
1067 * Verifies that the size order is bounded between the admissible orders and
1068 * updates the respective drm_device_dma::bufs entry low and high water mark.
1070 * \note This ioctl is deprecated and mostly never used.
1072 int drm_markbufs(struct drm_device *dev, void *data,
1073 struct drm_file *file_priv)
1075 drm_device_dma_t *dma = dev->dma;
1076 struct drm_buf_desc *request = data;
1079 DRM_DEBUG("%d, %d, %d\n",
1080 request->size, request->low_mark, request->high_mark);
1083 order = drm_order(request->size);
1084 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
1085 request->low_mark < 0 || request->high_mark < 0) {
1089 spin_lock(&dev->dma_lock);
1090 if (request->low_mark > dma->bufs[order].buf_count ||
1091 request->high_mark > dma->bufs[order].buf_count) {
1092 spin_unlock(&dev->dma_lock);
1096 dma->bufs[order].freelist.low_mark = request->low_mark;
1097 dma->bufs[order].freelist.high_mark = request->high_mark;
1098 spin_unlock(&dev->dma_lock);
1104 * Unreserve the buffers in list, previously reserved using drmDMA.
1106 * \param inode device inode.
1107 * \param file_priv DRM file private.
1108 * \param cmd command.
1109 * \param arg pointer to a drm_buf_free structure.
1110 * \return zero on success or a negative number on failure.
1112 * Calls free_buffer() for each used buffer.
1113 * This function is primarily used for debugging.
1115 int drm_freebufs(struct drm_device *dev, void *data,
1116 struct drm_file *file_priv)
1118 drm_device_dma_t *dma = dev->dma;
1119 struct drm_buf_free *request = data;
1125 DRM_DEBUG("%d\n", request->count);
1127 spin_lock(&dev->dma_lock);
1128 for (i = 0; i < request->count; i++) {
1129 if (DRM_COPY_FROM_USER(&idx, &request->list[i], sizeof(idx))) {
1133 if (idx < 0 || idx >= dma->buf_count) {
1134 DRM_ERROR("Index %d (of %d max)\n",
1135 idx, dma->buf_count - 1);
1139 buf = dma->buflist[idx];
1140 if (buf->file_priv != file_priv) {
1141 DRM_ERROR("Process %d freeing buffer not owned\n",
1146 drm_free_buffer(dev, buf);
1148 spin_unlock(&dev->dma_lock);
1154 * Maps all of the DMA buffers into client-virtual space (ioctl).
1156 * \param inode device inode.
1157 * \param file_priv DRM file private.
1158 * \param cmd command.
1159 * \param arg pointer to a drm_buf_map structure.
1160 * \return zero on success or a negative number on failure.
1162 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1163 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1164 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1167 int drm_mapbufs(struct drm_device *dev, void *data,
1168 struct drm_file *file_priv)
1170 drm_device_dma_t *dma = dev->dma;
1173 vm_offset_t address;
1174 struct vmspace *vms;
1178 struct drm_buf_map *request = data;
1181 vms = DRM_CURPROC->td_proc->p_vmspace;
1183 spin_lock(&dev->dma_lock);
1184 dev->buf_use++; /* Can't allocate more after this call */
1185 spin_unlock(&dev->dma_lock);
1187 if (request->count < dma->buf_count)
1190 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
1191 (drm_core_check_feature(dev, DRIVER_SG) &&
1192 (dma->flags & _DRM_DMA_USE_SG))) {
1193 drm_local_map_t *map = dev->agp_buffer_map;
1199 size = round_page(map->size);
1200 foff = (unsigned long)map->handle;
1202 size = round_page(dma->byte_count),
1206 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
1207 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1208 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC,
1209 SLIST_FIRST(&dev->devnode->si_hlist), foff);
1213 request->virtual = (void *)vaddr;
1215 for (i = 0; i < dma->buf_count; i++) {
1216 if (DRM_COPY_TO_USER(&request->list[i].idx,
1217 &dma->buflist[i]->idx, sizeof(request->list[0].idx))) {
1221 if (DRM_COPY_TO_USER(&request->list[i].total,
1222 &dma->buflist[i]->total, sizeof(request->list[0].total))) {
1226 if (DRM_COPY_TO_USER(&request->list[i].used, &zero,
1231 address = vaddr + dma->buflist[i]->offset; /* *** */
1232 if (DRM_COPY_TO_USER(&request->list[i].address, &address,
1240 request->count = dma->buf_count;
1242 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1248 * Compute size order. Returns the exponent of the smaller power of two which
1249 * is greater or equal to given number.
1254 * \todo Can be made faster.
1256 int drm_order(unsigned long size)
1261 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1263 if (size & (size - 1))
1268 EXPORT_SYMBOL(drm_order);