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.
23 * $DragonFly: src/sys/dev/drm/drm_pci.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
28 * \brief PCI consistent, DMA-accessible memory allocation.
30 * \author Eric Anholt <anholt@FreeBSD.org>
35 /**********************************************************************/
36 /** \name PCI memory */
39 #if defined(__FreeBSD__) || defined(__DragonFly__)
41 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
43 drm_dma_handle_t *dmah = arg;
48 KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
49 dmah->busaddr = segs[0].ds_addr;
54 * \brief Allocate a physically contiguous DMA-accessible consistent
58 drm_pci_alloc(drm_device_t *dev, size_t size, size_t align, dma_addr_t maxaddr)
60 drm_dma_handle_t *dmah;
63 /* Need power-of-two alignment, so fail the allocation if it isn't. */
64 if ((align & (align - 1)) != 0) {
65 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
70 dmah = malloc(sizeof(drm_dma_handle_t), M_DRM, M_ZERO | M_NOWAIT);
74 #if defined(__FreeBSD__) || defined(__DragonFly__)
75 ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
76 maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
77 NULL, NULL, /* filtfunc, filtfuncargs */
78 size, 1, size, /* maxsize, nsegs, maxsegsize */
79 BUS_DMA_ALLOCNOW, /* flags */
81 NULL, NULL, /* lockfunc, lockfuncargs */
89 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, BUS_DMA_NOWAIT,
92 bus_dma_tag_destroy(dmah->tag);
97 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
98 drm_pci_busdma_callback, dmah, 0);
100 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
101 bus_dma_tag_destroy(dmah->tag);
105 #elif defined(__NetBSD__)
106 ret = bus_dmamem_alloc(dev->dma_tag, size, align, PAGE_SIZE,
107 &dmah->seg, 1, &nsegs, BUS_DMA_NOWAIT);
108 if ((ret != 0) || (nsegs != 1)) {
113 ret = bus_dmamem_map(dev->dma_tag, &dmah->seg, 1, size, &dmah->addr,
116 bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
121 dmah->dmaaddr = h->seg.ds_addr;
128 * \brief Free a DMA-accessible consistent memory block.
131 drm_pci_free(drm_device_t *dev, drm_dma_handle_t *dmah)
136 #if defined(__FreeBSD__) || defined(__DragonFly__)
137 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
138 bus_dma_tag_destroy(dmah->tag);
139 #elif defined(__NetBSD__)
140 bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);