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.
26 * \brief PCI consistent, DMA-accessible memory allocation.
28 * \author Eric Anholt <anholt@FreeBSD.org>
33 /**********************************************************************/
34 /** \name PCI memory */
37 #if defined(__FreeBSD__)
39 drm_pci_busdma_callback(void *arg
, bus_dma_segment_t
*segs
, int nsegs
, int error
)
41 drm_dma_handle_t
*dmah
= arg
;
46 KASSERT(nsegs
== 1, ("drm_pci_busdma_callback: bad dma segment count"));
47 dmah
->busaddr
= segs
[0].ds_addr
;
52 * \brief Allocate a physically contiguous DMA-accessible consistent
56 drm_pci_alloc(struct drm_device
*dev
, size_t size
,
57 size_t align
, dma_addr_t maxaddr
)
59 drm_dma_handle_t
*dmah
;
61 #if defined (__NetBSD__)
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
), DRM_MEM_DMA
, M_ZERO
| M_NOWAIT
);
76 #if defined__FreeBSD__
77 /* Make sure we aren't holding locks here */
78 mtx_assert(&dev
->dev_lock
, MA_NOTOWNED
);
79 if (mtx_owned(&dev
->dev_lock
))
80 DRM_ERROR("called while holding dev_lock\n");
81 mtx_assert(&dev
->dma_lock
, MA_NOTOWNED
);
82 if (mtx_owned(&dev
->dma_lock
))
83 DRM_ERROR("called while holding dma_lock\n");
85 ret
= bus_dma_tag_create(NULL
, align
, 0, /* tag, align, boundary */
86 maxaddr
, BUS_SPACE_MAXADDR
, /* lowaddr, highaddr */
87 NULL
, NULL
, /* filtfunc, filtfuncargs */
88 size
, 1, size
, /* maxsize, nsegs, maxsegsize */
89 0, NULL
, NULL
, /* flags, lockfunc, lockfuncargs */
92 free(dmah
, DRM_MEM_DMA
);
96 ret
= bus_dmamem_alloc(dmah
->tag
, &dmah
->vaddr
,
97 BUS_DMA_WAITOK
| BUS_DMA_ZERO
, &dmah
->map
);
99 bus_dma_tag_destroy(dmah
->tag
);
100 free(dmah
, DRM_MEM_DMA
);
104 ret
= bus_dmamap_load(dmah
->tag
, dmah
->map
, dmah
->vaddr
, size
,
105 drm_pci_busdma_callback
, dmah
, BUS_DMA_NOWAIT
| BUS_DMA_NOCACHE
);
107 bus_dmamem_free(dmah
->tag
, dmah
->vaddr
, dmah
->map
);
108 bus_dma_tag_destroy(dmah
->tag
);
109 free(dmah
, DRM_MEM_DMA
);
112 #elif defined(__NetBSD__)
113 KASSERT(maxaddr
>= 0xffffffffUL
); /* no way to tell bus_dma_alloc */
114 dmah
->tag
= dev
->pa
.pa_dmat
; /* use 32-bit DMA tag */
116 if ((ret
= bus_dmamem_alloc(dmah
->tag
, size
, align
, 0,
117 dmah
->segs
, 1, &nsegs
, BUS_DMA_WAITOK
)) != 0) {
118 printf("drm: Unable to allocate %zu bytes of DMA, error %d\n",
121 free(dmah
, DRM_MEM_DMA
);
124 /* XXX is there a better way to deal with this? */
126 printf("drm: bad segment count from bus_dmamem_alloc\n");
127 bus_dmamem_free(dmah
->tag
, dmah
->segs
, 1);
129 free(dmah
, DRM_MEM_DMA
);
132 if ((ret
= bus_dmamem_map(dmah
->tag
, dmah
->segs
, nsegs
, size
,
133 &dmah
->vaddr
, BUS_DMA_NOWAIT
| BUS_DMA_COHERENT
| BUS_DMA_NOCACHE
)) != 0) {
134 printf("drm: Unable to map DMA, error %d\n", ret
);
135 bus_dmamem_free(dmah
->tag
, dmah
->segs
, 1);
137 free(dmah
, DRM_MEM_DMA
);
140 if ((ret
= bus_dmamap_create(dmah
->tag
, size
, 1, size
, maxaddr
,
141 BUS_DMA_NOWAIT
, &dmah
->map
)) != 0) {
142 printf("drm: Unable to create DMA map, error %d\n", ret
);
143 bus_dmamem_unmap(dmah
->tag
, dmah
->vaddr
, size
);
144 bus_dmamem_free(dmah
->tag
, dmah
->segs
, 1);
146 free(dmah
, DRM_MEM_DMA
);
149 if ((ret
= bus_dmamap_load(dmah
->tag
, dmah
->map
, dmah
->vaddr
,
150 size
, NULL
, BUS_DMA_NOWAIT
| BUS_DMA_NOCACHE
)) != 0) {
151 printf("drm: Unable to load DMA map, error %d\n", ret
);
152 bus_dmamap_destroy(dmah
->tag
, dmah
->map
);
153 bus_dmamem_unmap(dmah
->tag
, dmah
->vaddr
, size
);
154 bus_dmamem_free(dmah
->tag
, dmah
->segs
, 1);
156 free(dmah
, DRM_MEM_DMA
);
159 dmah
->busaddr
= dmah
->map
->dm_segs
[0].ds_addr
;
162 memset(dmah
->vaddr
, 0, size
);
169 * \brief Free a DMA-accessible consistent memory block.
172 drm_pci_free(struct drm_device
*dev
, drm_dma_handle_t
*dmah
)
177 #if defined(__FreeBSD__)
178 bus_dmamem_free(dmah
->tag
, dmah
->vaddr
, dmah
->map
);
179 bus_dma_tag_destroy(dmah
->tag
);
180 #elif defined(__NetBSD__)
181 bus_dmamap_unload(dmah
->tag
, dmah
->map
);
182 bus_dmamap_destroy(dmah
->tag
, dmah
->map
);
183 bus_dmamem_unmap(dmah
->tag
, dmah
->vaddr
, dmah
->size
);
184 bus_dmamem_free(dmah
->tag
, dmah
->segs
, 1);
188 free(dmah
, DRM_MEM_DMA
);