2 * Copyright 2003 Eric Anholt.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
29 * \brief PCI consistent, DMA-accessible memory allocation.
31 * \author Eric Anholt <anholt@FreeBSD.org>
34 #include "dev/drm/drmP.h"
36 /**********************************************************************/
37 /** \name PCI memory */
40 #if defined(__FreeBSD__)
42 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
44 drm_dma_handle_t *dmah = arg;
49 KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
50 dmah->busaddr = segs[0].ds_addr;
55 * \brief Allocate a physically contiguous DMA-accessible consistent
59 drm_pci_alloc(struct drm_device *dev, size_t size,
60 size_t align, dma_addr_t maxaddr)
62 drm_dma_handle_t *dmah;
65 /* Need power-of-two alignment, so fail the allocation if it isn't. */
66 if ((align & (align - 1)) != 0) {
67 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
72 dmah = malloc(sizeof(drm_dma_handle_t), M_DRM, M_ZERO | M_NOWAIT);
78 ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
79 maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
80 NULL, NULL, /* filtfunc, filtfuncargs */
81 size, 1, size, /* maxsize, nsegs, maxsegsize */
82 BUS_DMA_ALLOCNOW, NULL, NULL, /* flags, lockfunc, lockfuncargs */
90 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, BUS_DMA_NOWAIT,
93 bus_dma_tag_destroy(dmah->tag);
99 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
100 drm_pci_busdma_callback, dmah, 0);
102 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
103 bus_dma_tag_destroy(dmah->tag);
107 #elif defined(__NetBSD__)
108 ret = bus_dmamem_alloc(dev->dma_tag, size, align, PAGE_SIZE,
109 &dmah->seg, 1, &nsegs, BUS_DMA_NOWAIT);
110 if ((ret != 0) || (nsegs != 1)) {
115 ret = bus_dmamem_map(dev->dma_tag, &dmah->seg, 1, size, &dmah->addr,
118 bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
123 dmah->dmaaddr = h->seg.ds_addr;
130 * \brief Free a DMA-accessible consistent memory block.
133 drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah)
138 #if defined(__FreeBSD__)
139 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
140 bus_dma_tag_destroy(dmah->tag);
141 #elif defined(__NetBSD__)
142 bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);