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