int_bus_dma.c revision 1.3.2.2 1 1.3.2.2 thorpej /* $NetBSD: int_bus_dma.c,v 1.3.2.2 2002/01/10 19:42:32 thorpej Exp $ */
2 1.3.2.2 thorpej
3 1.3.2.2 thorpej /*-
4 1.3.2.2 thorpej * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 1.3.2.2 thorpej * All rights reserved.
6 1.3.2.2 thorpej *
7 1.3.2.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.3.2.2 thorpej * NASA Ames Research Center.
10 1.3.2.2 thorpej *
11 1.3.2.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.3.2.2 thorpej * modification, are permitted provided that the following conditions
13 1.3.2.2 thorpej * are met:
14 1.3.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.3.2.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.3.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.3.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.3.2.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.3.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.3.2.2 thorpej * must display the following acknowledgement:
21 1.3.2.2 thorpej * This product includes software developed by the NetBSD
22 1.3.2.2 thorpej * Foundation, Inc. and its contributors.
23 1.3.2.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.3.2.2 thorpej * contributors may be used to endorse or promote products derived
25 1.3.2.2 thorpej * from this software without specific prior written permission.
26 1.3.2.2 thorpej *
27 1.3.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.3.2.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.3.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.3.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.3.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.3.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.3.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.3.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.3.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.3.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.3.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.3.2.2 thorpej */
39 1.3.2.2 thorpej /*
40 1.3.2.2 thorpej * The integrator board has memory steering hardware that means that
41 1.3.2.2 thorpej * the normal physical addresses used by the processor cannot be used
42 1.3.2.2 thorpej * for DMA. Instead we have to use the "core module alias mapping
43 1.3.2.2 thorpej * addresses". We don't use these for normal processor accesses since
44 1.3.2.2 thorpej * they are much slower than the direct addresses when accessing
45 1.3.2.2 thorpej * memory on the local board.
46 1.3.2.2 thorpej */
47 1.3.2.2 thorpej
48 1.3.2.2 thorpej #include <sys/param.h>
49 1.3.2.2 thorpej #include <sys/systm.h>
50 1.3.2.2 thorpej #include <sys/kernel.h>
51 1.3.2.2 thorpej #include <sys/map.h>
52 1.3.2.2 thorpej #include <sys/proc.h>
53 1.3.2.2 thorpej #include <sys/buf.h>
54 1.3.2.2 thorpej #include <sys/reboot.h>
55 1.3.2.2 thorpej #include <sys/conf.h>
56 1.3.2.2 thorpej #include <sys/file.h>
57 1.3.2.2 thorpej #include <sys/malloc.h>
58 1.3.2.2 thorpej #include <sys/mbuf.h>
59 1.3.2.2 thorpej #include <sys/vnode.h>
60 1.3.2.2 thorpej #include <sys/device.h>
61 1.3.2.2 thorpej
62 1.3.2.2 thorpej #include <uvm/uvm_extern.h>
63 1.3.2.2 thorpej
64 1.3.2.2 thorpej #define _ARM32_BUS_DMA_PRIVATE
65 1.3.2.2 thorpej #include <evbarm/integrator/int_bus_dma.h>
66 1.3.2.2 thorpej
67 1.3.2.2 thorpej #include <machine/cpu.h>
68 1.3.2.2 thorpej #include <arm/cpufunc.h>
69 1.3.2.2 thorpej
70 1.3.2.2 thorpej static int integrator_bus_dmamap_load_buffer __P((bus_dma_tag_t,
71 1.3.2.2 thorpej bus_dmamap_t, void *, bus_size_t, struct proc *, int,
72 1.3.2.2 thorpej vm_offset_t *, int *, int));
73 1.3.2.2 thorpej static int integrator_bus_dma_inrange __P((bus_dma_segment_t *, int,
74 1.3.2.2 thorpej bus_addr_t));
75 1.3.2.2 thorpej
76 1.3.2.2 thorpej /*
77 1.3.2.2 thorpej * Common function for loading a DMA map with a linear buffer. May
78 1.3.2.2 thorpej * be called by bus-specific DMA map load functions.
79 1.3.2.2 thorpej */
80 1.3.2.2 thorpej int
81 1.3.2.2 thorpej integrator_bus_dmamap_load(t, map, buf, buflen, p, flags)
82 1.3.2.2 thorpej bus_dma_tag_t t;
83 1.3.2.2 thorpej bus_dmamap_t map;
84 1.3.2.2 thorpej void *buf;
85 1.3.2.2 thorpej bus_size_t buflen;
86 1.3.2.2 thorpej struct proc *p;
87 1.3.2.2 thorpej int flags;
88 1.3.2.2 thorpej {
89 1.3.2.2 thorpej vm_offset_t lastaddr;
90 1.3.2.2 thorpej int seg, error;
91 1.3.2.2 thorpej
92 1.3.2.2 thorpej #ifdef DEBUG_DMA
93 1.3.2.2 thorpej printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
94 1.3.2.2 thorpej t, map, buf, buflen, p, flags);
95 1.3.2.2 thorpej #endif /* DEBUG_DMA */
96 1.3.2.2 thorpej
97 1.3.2.2 thorpej /*
98 1.3.2.2 thorpej * Make sure that on error condition we return "no valid mappings".
99 1.3.2.2 thorpej */
100 1.3.2.2 thorpej map->dm_mapsize = 0;
101 1.3.2.2 thorpej map->dm_nsegs = 0;
102 1.3.2.2 thorpej
103 1.3.2.2 thorpej if (buflen > map->_dm_size)
104 1.3.2.2 thorpej return (EINVAL);
105 1.3.2.2 thorpej
106 1.3.2.2 thorpej seg = 0;
107 1.3.2.2 thorpej error = integrator_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
108 1.3.2.2 thorpej &lastaddr, &seg, 1);
109 1.3.2.2 thorpej if (error == 0) {
110 1.3.2.2 thorpej map->dm_mapsize = buflen;
111 1.3.2.2 thorpej map->dm_nsegs = seg + 1;
112 1.3.2.2 thorpej }
113 1.3.2.2 thorpej #ifdef DEBUG_DMA
114 1.3.2.2 thorpej printf("dmamap_load: error=%d\n", error);
115 1.3.2.2 thorpej #endif /* DEBUG_DMA */
116 1.3.2.2 thorpej return (error);
117 1.3.2.2 thorpej }
118 1.3.2.2 thorpej
119 1.3.2.2 thorpej /*
120 1.3.2.2 thorpej * Like _bus_dmamap_load(), but for mbufs.
121 1.3.2.2 thorpej */
122 1.3.2.2 thorpej int
123 1.3.2.2 thorpej integrator_bus_dmamap_load_mbuf(t, map, m0, flags)
124 1.3.2.2 thorpej bus_dma_tag_t t;
125 1.3.2.2 thorpej bus_dmamap_t map;
126 1.3.2.2 thorpej struct mbuf *m0;
127 1.3.2.2 thorpej int flags;
128 1.3.2.2 thorpej {
129 1.3.2.2 thorpej vm_offset_t lastaddr;
130 1.3.2.2 thorpej int seg, error, first;
131 1.3.2.2 thorpej struct mbuf *m;
132 1.3.2.2 thorpej
133 1.3.2.2 thorpej #ifdef DEBUG_DMA
134 1.3.2.2 thorpej printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
135 1.3.2.2 thorpej t, map, m0, flags);
136 1.3.2.2 thorpej #endif /* DEBUG_DMA */
137 1.3.2.2 thorpej
138 1.3.2.2 thorpej /*
139 1.3.2.2 thorpej * Make sure that on error condition we return "no valid mappings."
140 1.3.2.2 thorpej */
141 1.3.2.2 thorpej map->dm_mapsize = 0;
142 1.3.2.2 thorpej map->dm_nsegs = 0;
143 1.3.2.2 thorpej
144 1.3.2.2 thorpej #ifdef DIAGNOSTIC
145 1.3.2.2 thorpej if ((m0->m_flags & M_PKTHDR) == 0)
146 1.3.2.2 thorpej panic("integrator_bus_dmamap_load_mbuf: no packet header");
147 1.3.2.2 thorpej #endif /* DIAGNOSTIC */
148 1.3.2.2 thorpej
149 1.3.2.2 thorpej if (m0->m_pkthdr.len > map->_dm_size)
150 1.3.2.2 thorpej return (EINVAL);
151 1.3.2.2 thorpej
152 1.3.2.2 thorpej first = 1;
153 1.3.2.2 thorpej seg = 0;
154 1.3.2.2 thorpej error = 0;
155 1.3.2.2 thorpej for (m = m0; m != NULL && error == 0; m = m->m_next) {
156 1.3.2.2 thorpej error = integrator_bus_dmamap_load_buffer(t, map, m->m_data,
157 1.3.2.2 thorpej m->m_len, NULL, flags, &lastaddr, &seg, first);
158 1.3.2.2 thorpej first = 0;
159 1.3.2.2 thorpej }
160 1.3.2.2 thorpej if (error == 0) {
161 1.3.2.2 thorpej map->dm_mapsize = m0->m_pkthdr.len;
162 1.3.2.2 thorpej map->dm_nsegs = seg + 1;
163 1.3.2.2 thorpej }
164 1.3.2.2 thorpej #ifdef DEBUG_DMA
165 1.3.2.2 thorpej printf("dmamap_load_mbuf: error=%d\n", error);
166 1.3.2.2 thorpej #endif /* DEBUG_DMA */
167 1.3.2.2 thorpej return (error);
168 1.3.2.2 thorpej }
169 1.3.2.2 thorpej
170 1.3.2.2 thorpej /*
171 1.3.2.2 thorpej * Like _bus_dmamap_load(), but for uios.
172 1.3.2.2 thorpej */
173 1.3.2.2 thorpej int
174 1.3.2.2 thorpej integrator_bus_dmamap_load_uio(t, map, uio, flags)
175 1.3.2.2 thorpej bus_dma_tag_t t;
176 1.3.2.2 thorpej bus_dmamap_t map;
177 1.3.2.2 thorpej struct uio *uio;
178 1.3.2.2 thorpej int flags;
179 1.3.2.2 thorpej {
180 1.3.2.2 thorpej vm_offset_t lastaddr;
181 1.3.2.2 thorpej int seg, i, error, first;
182 1.3.2.2 thorpej bus_size_t minlen, resid;
183 1.3.2.2 thorpej struct proc *p = NULL;
184 1.3.2.2 thorpej struct iovec *iov;
185 1.3.2.2 thorpej caddr_t addr;
186 1.3.2.2 thorpej
187 1.3.2.2 thorpej /*
188 1.3.2.2 thorpej * Make sure that on error condition we return "no valid mappings."
189 1.3.2.2 thorpej */
190 1.3.2.2 thorpej map->dm_mapsize = 0;
191 1.3.2.2 thorpej map->dm_nsegs = 0;
192 1.3.2.2 thorpej
193 1.3.2.2 thorpej resid = uio->uio_resid;
194 1.3.2.2 thorpej iov = uio->uio_iov;
195 1.3.2.2 thorpej
196 1.3.2.2 thorpej if (uio->uio_segflg == UIO_USERSPACE) {
197 1.3.2.2 thorpej p = uio->uio_procp;
198 1.3.2.2 thorpej #ifdef DIAGNOSTIC
199 1.3.2.2 thorpej if (p == NULL)
200 1.3.2.2 thorpej panic("integrator_bus_dmamap_load_uio: USERSPACE but no proc");
201 1.3.2.2 thorpej #endif
202 1.3.2.2 thorpej }
203 1.3.2.2 thorpej
204 1.3.2.2 thorpej first = 1;
205 1.3.2.2 thorpej seg = 0;
206 1.3.2.2 thorpej error = 0;
207 1.3.2.2 thorpej for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
208 1.3.2.2 thorpej /*
209 1.3.2.2 thorpej * Now at the first iovec to load. Load each iovec
210 1.3.2.2 thorpej * until we have exhausted the residual count.
211 1.3.2.2 thorpej */
212 1.3.2.2 thorpej minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
213 1.3.2.2 thorpej addr = (caddr_t)iov[i].iov_base;
214 1.3.2.2 thorpej
215 1.3.2.2 thorpej error = integrator_bus_dmamap_load_buffer(t, map, addr, minlen,
216 1.3.2.2 thorpej p, flags, &lastaddr, &seg, first);
217 1.3.2.2 thorpej first = 0;
218 1.3.2.2 thorpej
219 1.3.2.2 thorpej resid -= minlen;
220 1.3.2.2 thorpej }
221 1.3.2.2 thorpej if (error == 0) {
222 1.3.2.2 thorpej map->dm_mapsize = uio->uio_resid;
223 1.3.2.2 thorpej map->dm_nsegs = seg + 1;
224 1.3.2.2 thorpej }
225 1.3.2.2 thorpej return (error);
226 1.3.2.2 thorpej }
227 1.3.2.2 thorpej
228 1.3.2.2 thorpej /*
229 1.3.2.2 thorpej * Common function for DMA-safe memory allocation. May be called
230 1.3.2.2 thorpej * by bus-specific DMA memory allocation functions.
231 1.3.2.2 thorpej */
232 1.3.2.2 thorpej
233 1.3.2.2 thorpej extern vm_offset_t physical_start;
234 1.3.2.2 thorpej extern vm_offset_t physical_freestart;
235 1.3.2.2 thorpej extern vm_offset_t physical_freeend;
236 1.3.2.2 thorpej extern vm_offset_t physical_end;
237 1.3.2.2 thorpej
238 1.3.2.2 thorpej int
239 1.3.2.2 thorpej integrator_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
240 1.3.2.2 thorpej bus_dma_tag_t t;
241 1.3.2.2 thorpej bus_size_t size, alignment, boundary;
242 1.3.2.2 thorpej bus_dma_segment_t *segs;
243 1.3.2.2 thorpej int nsegs;
244 1.3.2.2 thorpej int *rsegs;
245 1.3.2.2 thorpej int flags;
246 1.3.2.2 thorpej {
247 1.3.2.2 thorpej int error;
248 1.3.2.2 thorpej #ifdef DEBUG_DMA
249 1.3.2.2 thorpej printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x\n",
250 1.3.2.2 thorpej t, size, alignment, boundary, segs, nsegs, rsegs, flags);
251 1.3.2.2 thorpej #endif /* DEBUG_DMA */
252 1.3.2.2 thorpej error = (integrator_bus_dmamem_alloc_range(t, size, alignment, boundary,
253 1.3.2.2 thorpej segs, nsegs, rsegs, flags, trunc_page(physical_start), trunc_page(physical_end)));
254 1.3.2.2 thorpej #ifdef DEBUG_DMA
255 1.3.2.2 thorpej printf("dmamem_alloc: =%d\n", error);
256 1.3.2.2 thorpej #endif /* DEBUG_DMA */
257 1.3.2.2 thorpej return(error);
258 1.3.2.2 thorpej }
259 1.3.2.2 thorpej
260 1.3.2.2 thorpej /*
261 1.3.2.2 thorpej * Common function for freeing DMA-safe memory. May be called by
262 1.3.2.2 thorpej * bus-specific DMA memory free functions.
263 1.3.2.2 thorpej */
264 1.3.2.2 thorpej void
265 1.3.2.2 thorpej integrator_bus_dmamem_free(t, segs, nsegs)
266 1.3.2.2 thorpej bus_dma_tag_t t;
267 1.3.2.2 thorpej bus_dma_segment_t *segs;
268 1.3.2.2 thorpej int nsegs;
269 1.3.2.2 thorpej {
270 1.3.2.2 thorpej struct vm_page *m;
271 1.3.2.2 thorpej bus_addr_t addr;
272 1.3.2.2 thorpej struct pglist mlist;
273 1.3.2.2 thorpej int curseg;
274 1.3.2.2 thorpej
275 1.3.2.2 thorpej #ifdef DEBUG_DMA
276 1.3.2.2 thorpej printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
277 1.3.2.2 thorpej #endif /* DEBUG_DMA */
278 1.3.2.2 thorpej
279 1.3.2.2 thorpej /*
280 1.3.2.2 thorpej * Build a list of pages to free back to the VM system.
281 1.3.2.2 thorpej */
282 1.3.2.2 thorpej TAILQ_INIT(&mlist);
283 1.3.2.2 thorpej for (curseg = 0; curseg < nsegs; curseg++) {
284 1.3.2.2 thorpej for (addr = segs[curseg].ds_addr;
285 1.3.2.2 thorpej addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
286 1.3.2.2 thorpej addr += PAGE_SIZE) {
287 1.3.2.2 thorpej m = PHYS_TO_VM_PAGE(CM_ALIAS_TO_LOCAL(addr));
288 1.3.2.2 thorpej TAILQ_INSERT_TAIL(&mlist, m, pageq);
289 1.3.2.2 thorpej }
290 1.3.2.2 thorpej }
291 1.3.2.2 thorpej uvm_pglistfree(&mlist);
292 1.3.2.2 thorpej }
293 1.3.2.2 thorpej
294 1.3.2.2 thorpej /*
295 1.3.2.2 thorpej * Common function for mapping DMA-safe memory. May be called by
296 1.3.2.2 thorpej * bus-specific DMA memory map functions.
297 1.3.2.2 thorpej */
298 1.3.2.2 thorpej int
299 1.3.2.2 thorpej integrator_bus_dmamem_map(t, segs, nsegs, size, kvap, flags)
300 1.3.2.2 thorpej bus_dma_tag_t t;
301 1.3.2.2 thorpej bus_dma_segment_t *segs;
302 1.3.2.2 thorpej int nsegs;
303 1.3.2.2 thorpej size_t size;
304 1.3.2.2 thorpej caddr_t *kvap;
305 1.3.2.2 thorpej int flags;
306 1.3.2.2 thorpej {
307 1.3.2.2 thorpej vm_offset_t va;
308 1.3.2.2 thorpej bus_addr_t addr;
309 1.3.2.2 thorpej int curseg;
310 1.3.2.2 thorpej pt_entry_t *ptep/*, pte*/;
311 1.3.2.2 thorpej
312 1.3.2.2 thorpej #ifdef DEBUG_DMA
313 1.3.2.2 thorpej printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
314 1.3.2.2 thorpej segs, nsegs, (unsigned long)size, flags);
315 1.3.2.2 thorpej #endif /* DEBUG_DMA */
316 1.3.2.2 thorpej
317 1.3.2.2 thorpej size = round_page(size);
318 1.3.2.2 thorpej va = uvm_km_valloc(kernel_map, size);
319 1.3.2.2 thorpej
320 1.3.2.2 thorpej if (va == 0)
321 1.3.2.2 thorpej return (ENOMEM);
322 1.3.2.2 thorpej
323 1.3.2.2 thorpej *kvap = (caddr_t)va;
324 1.3.2.2 thorpej
325 1.3.2.2 thorpej for (curseg = 0; curseg < nsegs; curseg++) {
326 1.3.2.2 thorpej for (addr = segs[curseg].ds_addr;
327 1.3.2.2 thorpej addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
328 1.3.2.2 thorpej addr += NBPG, va += NBPG, size -= NBPG) {
329 1.3.2.2 thorpej #ifdef DEBUG_DMA
330 1.3.2.2 thorpej printf("wiring p%lx to v%lx", CM_ALIAS_TO_LOCAL(addr),
331 1.3.2.2 thorpej va);
332 1.3.2.2 thorpej #endif /* DEBUG_DMA */
333 1.3.2.2 thorpej if (size == 0)
334 1.3.2.2 thorpej panic("integrator_bus_dmamem_map: size botch");
335 1.3.2.2 thorpej pmap_enter(pmap_kernel(), va, CM_ALIAS_TO_LOCAL(addr),
336 1.3.2.2 thorpej VM_PROT_READ | VM_PROT_WRITE,
337 1.3.2.2 thorpej VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
338 1.3.2.2 thorpej /*
339 1.3.2.2 thorpej * If the memory must remain coherent with the
340 1.3.2.2 thorpej * cache then we must make the memory uncacheable
341 1.3.2.2 thorpej * in order to maintain virtual cache coherency.
342 1.3.2.2 thorpej * We must also guarentee the cache does not already
343 1.3.2.2 thorpej * contain the virtal addresses we are making
344 1.3.2.2 thorpej * uncacheable.
345 1.3.2.2 thorpej */
346 1.3.2.2 thorpej if (flags & BUS_DMA_COHERENT) {
347 1.3.2.2 thorpej cpu_cache_purgeD_rng(va, NBPG);
348 1.3.2.2 thorpej cpu_drain_writebuf();
349 1.3.2.2 thorpej ptep = vtopte(va);
350 1.3.2.2 thorpej *ptep = ((*ptep) & (~PT_C | PT_B));
351 1.3.2.2 thorpej tlb_flush();
352 1.3.2.2 thorpej }
353 1.3.2.2 thorpej #ifdef DEBUG_DMA
354 1.3.2.2 thorpej ptep = vtopte(va);
355 1.3.2.2 thorpej printf(" pte=v%p *pte=%x\n", ptep, *ptep);
356 1.3.2.2 thorpej #endif /* DEBUG_DMA */
357 1.3.2.2 thorpej }
358 1.3.2.2 thorpej }
359 1.3.2.2 thorpej pmap_update(pmap_kernel());
360 1.3.2.2 thorpej #ifdef DEBUG_DMA
361 1.3.2.2 thorpej printf("dmamem_map: =%p\n", *kvap);
362 1.3.2.2 thorpej #endif /* DEBUG_DMA */
363 1.3.2.2 thorpej return (0);
364 1.3.2.2 thorpej }
365 1.3.2.2 thorpej
366 1.3.2.2 thorpej /*
367 1.3.2.2 thorpej * Common functin for mmap(2)'ing DMA-safe memory. May be called by
368 1.3.2.2 thorpej * bus-specific DMA mmap(2)'ing functions.
369 1.3.2.2 thorpej */
370 1.3.2.2 thorpej paddr_t
371 1.3.2.2 thorpej integrator_bus_dmamem_mmap(t, segs, nsegs, off, prot, flags)
372 1.3.2.2 thorpej bus_dma_tag_t t;
373 1.3.2.2 thorpej bus_dma_segment_t *segs;
374 1.3.2.2 thorpej int nsegs;
375 1.3.2.2 thorpej off_t off;
376 1.3.2.2 thorpej int prot, flags;
377 1.3.2.2 thorpej {
378 1.3.2.2 thorpej int i;
379 1.3.2.2 thorpej
380 1.3.2.2 thorpej for (i = 0; i < nsegs; i++) {
381 1.3.2.2 thorpej #ifdef DIAGNOSTIC
382 1.3.2.2 thorpej if (off & PGOFSET)
383 1.3.2.2 thorpej panic("integrator_bus_dmamem_mmap: offset unaligned");
384 1.3.2.2 thorpej if (segs[i].ds_addr & PGOFSET)
385 1.3.2.2 thorpej panic("integrator_bus_dmamem_mmap: segment unaligned");
386 1.3.2.2 thorpej if (segs[i].ds_len & PGOFSET)
387 1.3.2.2 thorpej panic("integrator_bus_dmamem_mmap: segment size not multiple"
388 1.3.2.2 thorpej " of page size");
389 1.3.2.2 thorpej #endif /* DIAGNOSTIC */
390 1.3.2.2 thorpej if (off >= segs[i].ds_len) {
391 1.3.2.2 thorpej off -= segs[i].ds_len;
392 1.3.2.2 thorpej continue;
393 1.3.2.2 thorpej }
394 1.3.2.2 thorpej
395 1.3.2.2 thorpej return arm_byte_to_page((u_long)CM_ALIAS_TO_LOCAL(segs[i].ds_addr) + off);
396 1.3.2.2 thorpej }
397 1.3.2.2 thorpej
398 1.3.2.2 thorpej /* Page not found. */
399 1.3.2.2 thorpej return -1;
400 1.3.2.2 thorpej }
401 1.3.2.2 thorpej
402 1.3.2.2 thorpej /**********************************************************************
403 1.3.2.2 thorpej * DMA utility functions
404 1.3.2.2 thorpej **********************************************************************/
405 1.3.2.2 thorpej
406 1.3.2.2 thorpej /*
407 1.3.2.2 thorpej * Utility function to load a linear buffer. lastaddrp holds state
408 1.3.2.2 thorpej * between invocations (for multiple-buffer loads). segp contains
409 1.3.2.2 thorpej * the starting segment on entrace, and the ending segment on exit.
410 1.3.2.2 thorpej * first indicates if this is the first invocation of this function.
411 1.3.2.2 thorpej */
412 1.3.2.2 thorpej static int
413 1.3.2.2 thorpej integrator_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp,
414 1.3.2.2 thorpej segp, first)
415 1.3.2.2 thorpej bus_dma_tag_t t;
416 1.3.2.2 thorpej bus_dmamap_t map;
417 1.3.2.2 thorpej void *buf;
418 1.3.2.2 thorpej bus_size_t buflen;
419 1.3.2.2 thorpej struct proc *p;
420 1.3.2.2 thorpej int flags;
421 1.3.2.2 thorpej vm_offset_t *lastaddrp;
422 1.3.2.2 thorpej int *segp;
423 1.3.2.2 thorpej int first;
424 1.3.2.2 thorpej {
425 1.3.2.2 thorpej bus_size_t sgsize;
426 1.3.2.2 thorpej bus_addr_t curaddr, lastaddr, baddr, bmask;
427 1.3.2.2 thorpej vm_offset_t vaddr = (vm_offset_t)buf;
428 1.3.2.2 thorpej int seg;
429 1.3.2.2 thorpej pmap_t pmap;
430 1.3.2.2 thorpej
431 1.3.2.2 thorpej #ifdef DEBUG_DMA
432 1.3.2.2 thorpej printf("integrator_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d, 1st=%d)\n",
433 1.3.2.2 thorpej buf, buflen, flags, first);
434 1.3.2.2 thorpej #endif /* DEBUG_DMA */
435 1.3.2.2 thorpej
436 1.3.2.2 thorpej if (p != NULL)
437 1.3.2.2 thorpej pmap = p->p_vmspace->vm_map.pmap;
438 1.3.2.2 thorpej else
439 1.3.2.2 thorpej pmap = pmap_kernel();
440 1.3.2.2 thorpej
441 1.3.2.2 thorpej lastaddr = *lastaddrp;
442 1.3.2.2 thorpej bmask = ~(map->_dm_boundary - 1);
443 1.3.2.2 thorpej
444 1.3.2.2 thorpej for (seg = *segp; buflen > 0; ) {
445 1.3.2.2 thorpej /*
446 1.3.2.2 thorpej * Get the physical address for this segment.
447 1.3.2.2 thorpej */
448 1.3.2.2 thorpej (void) pmap_extract(pmap, (vaddr_t)vaddr, &curaddr);
449 1.3.2.2 thorpej
450 1.3.2.2 thorpej /*
451 1.3.2.2 thorpej * Make sure we're in an allowed DMA range.
452 1.3.2.2 thorpej */
453 1.3.2.2 thorpej if (t->_ranges != NULL &&
454 1.3.2.2 thorpej integrator_bus_dma_inrange(t->_ranges, t->_nranges, curaddr) == 0)
455 1.3.2.2 thorpej return (EINVAL);
456 1.3.2.2 thorpej
457 1.3.2.2 thorpej /*
458 1.3.2.2 thorpej * Compute the segment size, and adjust counts.
459 1.3.2.2 thorpej */
460 1.3.2.2 thorpej sgsize = NBPG - ((u_long)vaddr & PGOFSET);
461 1.3.2.2 thorpej if (buflen < sgsize)
462 1.3.2.2 thorpej sgsize = buflen;
463 1.3.2.2 thorpej
464 1.3.2.2 thorpej /*
465 1.3.2.2 thorpej * Make sure we don't cross any boundaries.
466 1.3.2.2 thorpej */
467 1.3.2.2 thorpej if (map->_dm_boundary > 0) {
468 1.3.2.2 thorpej baddr = (curaddr + map->_dm_boundary) & bmask;
469 1.3.2.2 thorpej if (sgsize > (baddr - curaddr))
470 1.3.2.2 thorpej sgsize = (baddr - curaddr);
471 1.3.2.2 thorpej }
472 1.3.2.2 thorpej
473 1.3.2.2 thorpej /*
474 1.3.2.2 thorpej * Insert chunk into a segment, coalescing with
475 1.3.2.2 thorpej * previous segment if possible.
476 1.3.2.2 thorpej */
477 1.3.2.2 thorpej if (first) {
478 1.3.2.2 thorpej map->dm_segs[seg].ds_addr = LOCAL_TO_CM_ALIAS(curaddr);
479 1.3.2.2 thorpej map->dm_segs[seg].ds_len = sgsize;
480 1.3.2.2 thorpej map->dm_segs[seg]._ds_vaddr = vaddr;
481 1.3.2.2 thorpej first = 0;
482 1.3.2.2 thorpej } else {
483 1.3.2.2 thorpej if (curaddr == lastaddr &&
484 1.3.2.2 thorpej (map->dm_segs[seg].ds_len + sgsize) <=
485 1.3.2.2 thorpej map->_dm_maxsegsz &&
486 1.3.2.2 thorpej (map->_dm_boundary == 0 ||
487 1.3.2.2 thorpej (map->dm_segs[seg].ds_addr & bmask) ==
488 1.3.2.2 thorpej (LOCAL_TO_CM_ALIAS(curaddr) & bmask)))
489 1.3.2.2 thorpej map->dm_segs[seg].ds_len += sgsize;
490 1.3.2.2 thorpej else {
491 1.3.2.2 thorpej if (++seg >= map->_dm_segcnt)
492 1.3.2.2 thorpej break;
493 1.3.2.2 thorpej map->dm_segs[seg].ds_addr = LOCAL_TO_CM_ALIAS(curaddr);
494 1.3.2.2 thorpej map->dm_segs[seg].ds_len = sgsize;
495 1.3.2.2 thorpej map->dm_segs[seg]._ds_vaddr = vaddr;
496 1.3.2.2 thorpej }
497 1.3.2.2 thorpej }
498 1.3.2.2 thorpej
499 1.3.2.2 thorpej lastaddr = curaddr + sgsize;
500 1.3.2.2 thorpej vaddr += sgsize;
501 1.3.2.2 thorpej buflen -= sgsize;
502 1.3.2.2 thorpej }
503 1.3.2.2 thorpej
504 1.3.2.2 thorpej *segp = seg;
505 1.3.2.2 thorpej *lastaddrp = lastaddr;
506 1.3.2.2 thorpej
507 1.3.2.2 thorpej /*
508 1.3.2.2 thorpej * Did we fit?
509 1.3.2.2 thorpej */
510 1.3.2.2 thorpej if (buflen != 0)
511 1.3.2.2 thorpej return (EFBIG); /* XXX better return value here? */
512 1.3.2.2 thorpej return (0);
513 1.3.2.2 thorpej }
514 1.3.2.2 thorpej
515 1.3.2.2 thorpej /*
516 1.3.2.2 thorpej * Check to see if the specified page is in an allowed DMA range.
517 1.3.2.2 thorpej */
518 1.3.2.2 thorpej static int
519 1.3.2.2 thorpej integrator_bus_dma_inrange(ranges, nranges, curaddr)
520 1.3.2.2 thorpej bus_dma_segment_t *ranges;
521 1.3.2.2 thorpej int nranges;
522 1.3.2.2 thorpej bus_addr_t curaddr;
523 1.3.2.2 thorpej {
524 1.3.2.2 thorpej bus_dma_segment_t *ds;
525 1.3.2.2 thorpej int i;
526 1.3.2.2 thorpej
527 1.3.2.2 thorpej for (i = 0, ds = ranges; i < nranges; i++, ds++) {
528 1.3.2.2 thorpej if (curaddr >= CM_ALIAS_TO_LOCAL(ds->ds_addr) &&
529 1.3.2.2 thorpej round_page(curaddr) <= (CM_ALIAS_TO_LOCAL(ds->ds_addr) + ds->ds_len))
530 1.3.2.2 thorpej return (1);
531 1.3.2.2 thorpej }
532 1.3.2.2 thorpej
533 1.3.2.2 thorpej return (0);
534 1.3.2.2 thorpej }
535 1.3.2.2 thorpej
536 1.3.2.2 thorpej /*
537 1.3.2.2 thorpej * Allocate physical memory from the given physical address range.
538 1.3.2.2 thorpej * Called by DMA-safe memory allocation methods.
539 1.3.2.2 thorpej */
540 1.3.2.2 thorpej int
541 1.3.2.2 thorpej integrator_bus_dmamem_alloc_range(t, size, alignment, boundary, segs, nsegs, rsegs,
542 1.3.2.2 thorpej flags, low, high)
543 1.3.2.2 thorpej bus_dma_tag_t t;
544 1.3.2.2 thorpej bus_size_t size, alignment, boundary;
545 1.3.2.2 thorpej bus_dma_segment_t *segs;
546 1.3.2.2 thorpej int nsegs;
547 1.3.2.2 thorpej int *rsegs;
548 1.3.2.2 thorpej int flags;
549 1.3.2.2 thorpej vm_offset_t low;
550 1.3.2.2 thorpej vm_offset_t high;
551 1.3.2.2 thorpej {
552 1.3.2.2 thorpej vm_offset_t curaddr, lastaddr;
553 1.3.2.2 thorpej struct vm_page *m;
554 1.3.2.2 thorpej struct pglist mlist;
555 1.3.2.2 thorpej int curseg, error;
556 1.3.2.2 thorpej
557 1.3.2.2 thorpej #ifdef DEBUG_DMA
558 1.3.2.2 thorpej printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
559 1.3.2.2 thorpej t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
560 1.3.2.2 thorpej #endif /* DEBUG_DMA */
561 1.3.2.2 thorpej
562 1.3.2.2 thorpej /* Always round the size. */
563 1.3.2.2 thorpej size = round_page(size);
564 1.3.2.2 thorpej
565 1.3.2.2 thorpej /*
566 1.3.2.2 thorpej * Allocate pages from the VM system.
567 1.3.2.2 thorpej */
568 1.3.2.2 thorpej TAILQ_INIT(&mlist);
569 1.3.2.2 thorpej error = uvm_pglistalloc(size, low, high, alignment, boundary,
570 1.3.2.2 thorpej &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
571 1.3.2.2 thorpej if (error)
572 1.3.2.2 thorpej return (error);
573 1.3.2.2 thorpej
574 1.3.2.2 thorpej /*
575 1.3.2.2 thorpej * Compute the location, size, and number of segments actually
576 1.3.2.2 thorpej * returned by the VM code.
577 1.3.2.2 thorpej */
578 1.3.2.2 thorpej m = mlist.tqh_first;
579 1.3.2.2 thorpej curseg = 0;
580 1.3.2.2 thorpej lastaddr = VM_PAGE_TO_PHYS(m);
581 1.3.2.2 thorpej segs[curseg].ds_addr = LOCAL_TO_CM_ALIAS(lastaddr);
582 1.3.2.2 thorpej segs[curseg].ds_len = PAGE_SIZE;
583 1.3.2.2 thorpej #ifdef DEBUG_DMA
584 1.3.2.2 thorpej printf("alloc: page %lx\n", lastaddr);
585 1.3.2.2 thorpej #endif /* DEBUG_DMA */
586 1.3.2.2 thorpej m = m->pageq.tqe_next;
587 1.3.2.2 thorpej
588 1.3.2.2 thorpej for (; m != NULL; m = m->pageq.tqe_next) {
589 1.3.2.2 thorpej curaddr = VM_PAGE_TO_PHYS(m);
590 1.3.2.2 thorpej #ifdef DIAGNOSTIC
591 1.3.2.2 thorpej if (curaddr < low || curaddr >= high) {
592 1.3.2.2 thorpej printf("uvm_pglistalloc returned non-sensical"
593 1.3.2.2 thorpej " address 0x%lx\n", curaddr);
594 1.3.2.2 thorpej panic("integrator_bus_dmamem_alloc_range");
595 1.3.2.2 thorpej }
596 1.3.2.2 thorpej #endif /* DIAGNOSTIC */
597 1.3.2.2 thorpej #ifdef DEBUG_DMA
598 1.3.2.2 thorpej printf("alloc: page %lx\n", curaddr);
599 1.3.2.2 thorpej #endif /* DEBUG_DMA */
600 1.3.2.2 thorpej if (curaddr == (lastaddr + PAGE_SIZE))
601 1.3.2.2 thorpej segs[curseg].ds_len += PAGE_SIZE;
602 1.3.2.2 thorpej else {
603 1.3.2.2 thorpej curseg++;
604 1.3.2.2 thorpej segs[curseg].ds_addr = LOCAL_TO_CM_ALIAS(curaddr);
605 1.3.2.2 thorpej segs[curseg].ds_len = PAGE_SIZE;
606 1.3.2.2 thorpej }
607 1.3.2.2 thorpej lastaddr = curaddr;
608 1.3.2.2 thorpej }
609 1.3.2.2 thorpej
610 1.3.2.2 thorpej *rsegs = curseg + 1;
611 1.3.2.2 thorpej
612 1.3.2.2 thorpej return (0);
613 1.3.2.2 thorpej }
614