int_bus_dma.c revision 1.3.2.4 1 1.3.2.4 jdolecek /* $NetBSD: int_bus_dma.c,v 1.3.2.4 2002/06/23 17:35:41 jdolecek 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.3 jdolecek map->_dm_proc = p;
113 1.3.2.2 thorpej }
114 1.3.2.2 thorpej #ifdef DEBUG_DMA
115 1.3.2.2 thorpej printf("dmamap_load: error=%d\n", error);
116 1.3.2.2 thorpej #endif /* DEBUG_DMA */
117 1.3.2.2 thorpej return (error);
118 1.3.2.2 thorpej }
119 1.3.2.2 thorpej
120 1.3.2.2 thorpej /*
121 1.3.2.2 thorpej * Like _bus_dmamap_load(), but for mbufs.
122 1.3.2.2 thorpej */
123 1.3.2.2 thorpej int
124 1.3.2.2 thorpej integrator_bus_dmamap_load_mbuf(t, map, m0, flags)
125 1.3.2.2 thorpej bus_dma_tag_t t;
126 1.3.2.2 thorpej bus_dmamap_t map;
127 1.3.2.2 thorpej struct mbuf *m0;
128 1.3.2.2 thorpej int flags;
129 1.3.2.2 thorpej {
130 1.3.2.2 thorpej vm_offset_t lastaddr;
131 1.3.2.2 thorpej int seg, error, first;
132 1.3.2.2 thorpej struct mbuf *m;
133 1.3.2.2 thorpej
134 1.3.2.2 thorpej #ifdef DEBUG_DMA
135 1.3.2.2 thorpej printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
136 1.3.2.2 thorpej t, map, m0, flags);
137 1.3.2.2 thorpej #endif /* DEBUG_DMA */
138 1.3.2.2 thorpej
139 1.3.2.2 thorpej /*
140 1.3.2.2 thorpej * Make sure that on error condition we return "no valid mappings."
141 1.3.2.2 thorpej */
142 1.3.2.2 thorpej map->dm_mapsize = 0;
143 1.3.2.2 thorpej map->dm_nsegs = 0;
144 1.3.2.2 thorpej
145 1.3.2.2 thorpej #ifdef DIAGNOSTIC
146 1.3.2.2 thorpej if ((m0->m_flags & M_PKTHDR) == 0)
147 1.3.2.2 thorpej panic("integrator_bus_dmamap_load_mbuf: no packet header");
148 1.3.2.2 thorpej #endif /* DIAGNOSTIC */
149 1.3.2.2 thorpej
150 1.3.2.2 thorpej if (m0->m_pkthdr.len > map->_dm_size)
151 1.3.2.2 thorpej return (EINVAL);
152 1.3.2.2 thorpej
153 1.3.2.2 thorpej first = 1;
154 1.3.2.2 thorpej seg = 0;
155 1.3.2.2 thorpej error = 0;
156 1.3.2.2 thorpej for (m = m0; m != NULL && error == 0; m = m->m_next) {
157 1.3.2.2 thorpej error = integrator_bus_dmamap_load_buffer(t, map, m->m_data,
158 1.3.2.2 thorpej m->m_len, NULL, flags, &lastaddr, &seg, first);
159 1.3.2.2 thorpej first = 0;
160 1.3.2.2 thorpej }
161 1.3.2.2 thorpej if (error == 0) {
162 1.3.2.2 thorpej map->dm_mapsize = m0->m_pkthdr.len;
163 1.3.2.2 thorpej map->dm_nsegs = seg + 1;
164 1.3.2.3 jdolecek map->_dm_proc = NULL; /* always kernel */
165 1.3.2.2 thorpej }
166 1.3.2.2 thorpej #ifdef DEBUG_DMA
167 1.3.2.2 thorpej printf("dmamap_load_mbuf: error=%d\n", error);
168 1.3.2.2 thorpej #endif /* DEBUG_DMA */
169 1.3.2.2 thorpej return (error);
170 1.3.2.2 thorpej }
171 1.3.2.2 thorpej
172 1.3.2.2 thorpej /*
173 1.3.2.2 thorpej * Like _bus_dmamap_load(), but for uios.
174 1.3.2.2 thorpej */
175 1.3.2.2 thorpej int
176 1.3.2.2 thorpej integrator_bus_dmamap_load_uio(t, map, uio, flags)
177 1.3.2.2 thorpej bus_dma_tag_t t;
178 1.3.2.2 thorpej bus_dmamap_t map;
179 1.3.2.2 thorpej struct uio *uio;
180 1.3.2.2 thorpej int flags;
181 1.3.2.2 thorpej {
182 1.3.2.2 thorpej vm_offset_t lastaddr;
183 1.3.2.2 thorpej int seg, i, error, first;
184 1.3.2.2 thorpej bus_size_t minlen, resid;
185 1.3.2.2 thorpej struct proc *p = NULL;
186 1.3.2.2 thorpej struct iovec *iov;
187 1.3.2.2 thorpej caddr_t addr;
188 1.3.2.2 thorpej
189 1.3.2.2 thorpej /*
190 1.3.2.2 thorpej * Make sure that on error condition we return "no valid mappings."
191 1.3.2.2 thorpej */
192 1.3.2.2 thorpej map->dm_mapsize = 0;
193 1.3.2.2 thorpej map->dm_nsegs = 0;
194 1.3.2.2 thorpej
195 1.3.2.2 thorpej resid = uio->uio_resid;
196 1.3.2.2 thorpej iov = uio->uio_iov;
197 1.3.2.2 thorpej
198 1.3.2.2 thorpej if (uio->uio_segflg == UIO_USERSPACE) {
199 1.3.2.2 thorpej p = uio->uio_procp;
200 1.3.2.2 thorpej #ifdef DIAGNOSTIC
201 1.3.2.2 thorpej if (p == NULL)
202 1.3.2.2 thorpej panic("integrator_bus_dmamap_load_uio: USERSPACE but no proc");
203 1.3.2.2 thorpej #endif
204 1.3.2.2 thorpej }
205 1.3.2.2 thorpej
206 1.3.2.2 thorpej first = 1;
207 1.3.2.2 thorpej seg = 0;
208 1.3.2.2 thorpej error = 0;
209 1.3.2.2 thorpej for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
210 1.3.2.2 thorpej /*
211 1.3.2.2 thorpej * Now at the first iovec to load. Load each iovec
212 1.3.2.2 thorpej * until we have exhausted the residual count.
213 1.3.2.2 thorpej */
214 1.3.2.2 thorpej minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
215 1.3.2.2 thorpej addr = (caddr_t)iov[i].iov_base;
216 1.3.2.2 thorpej
217 1.3.2.2 thorpej error = integrator_bus_dmamap_load_buffer(t, map, addr, minlen,
218 1.3.2.2 thorpej p, flags, &lastaddr, &seg, first);
219 1.3.2.2 thorpej first = 0;
220 1.3.2.2 thorpej
221 1.3.2.2 thorpej resid -= minlen;
222 1.3.2.2 thorpej }
223 1.3.2.2 thorpej if (error == 0) {
224 1.3.2.2 thorpej map->dm_mapsize = uio->uio_resid;
225 1.3.2.2 thorpej map->dm_nsegs = seg + 1;
226 1.3.2.3 jdolecek map->_dm_proc = p;
227 1.3.2.2 thorpej }
228 1.3.2.2 thorpej return (error);
229 1.3.2.2 thorpej }
230 1.3.2.2 thorpej
231 1.3.2.2 thorpej /*
232 1.3.2.2 thorpej * Common function for DMA-safe memory allocation. May be called
233 1.3.2.2 thorpej * by bus-specific DMA memory allocation functions.
234 1.3.2.2 thorpej */
235 1.3.2.2 thorpej
236 1.3.2.2 thorpej extern vm_offset_t physical_start;
237 1.3.2.2 thorpej extern vm_offset_t physical_freestart;
238 1.3.2.2 thorpej extern vm_offset_t physical_freeend;
239 1.3.2.2 thorpej extern vm_offset_t physical_end;
240 1.3.2.2 thorpej
241 1.3.2.2 thorpej int
242 1.3.2.2 thorpej integrator_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
243 1.3.2.2 thorpej bus_dma_tag_t t;
244 1.3.2.2 thorpej bus_size_t size, alignment, boundary;
245 1.3.2.2 thorpej bus_dma_segment_t *segs;
246 1.3.2.2 thorpej int nsegs;
247 1.3.2.2 thorpej int *rsegs;
248 1.3.2.2 thorpej int flags;
249 1.3.2.2 thorpej {
250 1.3.2.2 thorpej int error;
251 1.3.2.2 thorpej #ifdef DEBUG_DMA
252 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",
253 1.3.2.2 thorpej t, size, alignment, boundary, segs, nsegs, rsegs, flags);
254 1.3.2.2 thorpej #endif /* DEBUG_DMA */
255 1.3.2.2 thorpej error = (integrator_bus_dmamem_alloc_range(t, size, alignment, boundary,
256 1.3.2.2 thorpej segs, nsegs, rsegs, flags, trunc_page(physical_start), trunc_page(physical_end)));
257 1.3.2.2 thorpej #ifdef DEBUG_DMA
258 1.3.2.2 thorpej printf("dmamem_alloc: =%d\n", error);
259 1.3.2.2 thorpej #endif /* DEBUG_DMA */
260 1.3.2.2 thorpej return(error);
261 1.3.2.2 thorpej }
262 1.3.2.2 thorpej
263 1.3.2.2 thorpej /*
264 1.3.2.2 thorpej * Common function for freeing DMA-safe memory. May be called by
265 1.3.2.2 thorpej * bus-specific DMA memory free functions.
266 1.3.2.2 thorpej */
267 1.3.2.2 thorpej void
268 1.3.2.2 thorpej integrator_bus_dmamem_free(t, segs, nsegs)
269 1.3.2.2 thorpej bus_dma_tag_t t;
270 1.3.2.2 thorpej bus_dma_segment_t *segs;
271 1.3.2.2 thorpej int nsegs;
272 1.3.2.2 thorpej {
273 1.3.2.2 thorpej struct vm_page *m;
274 1.3.2.2 thorpej bus_addr_t addr;
275 1.3.2.2 thorpej struct pglist mlist;
276 1.3.2.2 thorpej int curseg;
277 1.3.2.2 thorpej
278 1.3.2.2 thorpej #ifdef DEBUG_DMA
279 1.3.2.2 thorpej printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
280 1.3.2.2 thorpej #endif /* DEBUG_DMA */
281 1.3.2.2 thorpej
282 1.3.2.2 thorpej /*
283 1.3.2.2 thorpej * Build a list of pages to free back to the VM system.
284 1.3.2.2 thorpej */
285 1.3.2.2 thorpej TAILQ_INIT(&mlist);
286 1.3.2.2 thorpej for (curseg = 0; curseg < nsegs; curseg++) {
287 1.3.2.2 thorpej for (addr = segs[curseg].ds_addr;
288 1.3.2.2 thorpej addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
289 1.3.2.2 thorpej addr += PAGE_SIZE) {
290 1.3.2.2 thorpej m = PHYS_TO_VM_PAGE(CM_ALIAS_TO_LOCAL(addr));
291 1.3.2.2 thorpej TAILQ_INSERT_TAIL(&mlist, m, pageq);
292 1.3.2.2 thorpej }
293 1.3.2.2 thorpej }
294 1.3.2.2 thorpej uvm_pglistfree(&mlist);
295 1.3.2.2 thorpej }
296 1.3.2.2 thorpej
297 1.3.2.2 thorpej /*
298 1.3.2.2 thorpej * Common function for mapping DMA-safe memory. May be called by
299 1.3.2.2 thorpej * bus-specific DMA memory map functions.
300 1.3.2.2 thorpej */
301 1.3.2.2 thorpej int
302 1.3.2.2 thorpej integrator_bus_dmamem_map(t, segs, nsegs, size, kvap, flags)
303 1.3.2.2 thorpej bus_dma_tag_t t;
304 1.3.2.2 thorpej bus_dma_segment_t *segs;
305 1.3.2.2 thorpej int nsegs;
306 1.3.2.2 thorpej size_t size;
307 1.3.2.2 thorpej caddr_t *kvap;
308 1.3.2.2 thorpej int flags;
309 1.3.2.2 thorpej {
310 1.3.2.2 thorpej vm_offset_t va;
311 1.3.2.2 thorpej bus_addr_t addr;
312 1.3.2.2 thorpej int curseg;
313 1.3.2.2 thorpej pt_entry_t *ptep/*, pte*/;
314 1.3.2.2 thorpej
315 1.3.2.2 thorpej #ifdef DEBUG_DMA
316 1.3.2.2 thorpej printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
317 1.3.2.2 thorpej segs, nsegs, (unsigned long)size, flags);
318 1.3.2.2 thorpej #endif /* DEBUG_DMA */
319 1.3.2.2 thorpej
320 1.3.2.2 thorpej size = round_page(size);
321 1.3.2.2 thorpej va = uvm_km_valloc(kernel_map, size);
322 1.3.2.2 thorpej
323 1.3.2.2 thorpej if (va == 0)
324 1.3.2.2 thorpej return (ENOMEM);
325 1.3.2.2 thorpej
326 1.3.2.2 thorpej *kvap = (caddr_t)va;
327 1.3.2.2 thorpej
328 1.3.2.2 thorpej for (curseg = 0; curseg < nsegs; curseg++) {
329 1.3.2.2 thorpej for (addr = segs[curseg].ds_addr;
330 1.3.2.2 thorpej addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
331 1.3.2.2 thorpej addr += NBPG, va += NBPG, size -= NBPG) {
332 1.3.2.2 thorpej #ifdef DEBUG_DMA
333 1.3.2.2 thorpej printf("wiring p%lx to v%lx", CM_ALIAS_TO_LOCAL(addr),
334 1.3.2.2 thorpej va);
335 1.3.2.2 thorpej #endif /* DEBUG_DMA */
336 1.3.2.2 thorpej if (size == 0)
337 1.3.2.2 thorpej panic("integrator_bus_dmamem_map: size botch");
338 1.3.2.2 thorpej pmap_enter(pmap_kernel(), va, CM_ALIAS_TO_LOCAL(addr),
339 1.3.2.2 thorpej VM_PROT_READ | VM_PROT_WRITE,
340 1.3.2.2 thorpej VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
341 1.3.2.2 thorpej /*
342 1.3.2.2 thorpej * If the memory must remain coherent with the
343 1.3.2.2 thorpej * cache then we must make the memory uncacheable
344 1.3.2.2 thorpej * in order to maintain virtual cache coherency.
345 1.3.2.2 thorpej * We must also guarentee the cache does not already
346 1.3.2.2 thorpej * contain the virtal addresses we are making
347 1.3.2.2 thorpej * uncacheable.
348 1.3.2.2 thorpej */
349 1.3.2.2 thorpej if (flags & BUS_DMA_COHERENT) {
350 1.3.2.3 jdolecek cpu_dcache_wbinv_range(va, NBPG);
351 1.3.2.2 thorpej cpu_drain_writebuf();
352 1.3.2.2 thorpej ptep = vtopte(va);
353 1.3.2.4 jdolecek *ptep &= ~(L2_C | L2_B);
354 1.3.2.2 thorpej tlb_flush();
355 1.3.2.2 thorpej }
356 1.3.2.2 thorpej #ifdef DEBUG_DMA
357 1.3.2.2 thorpej ptep = vtopte(va);
358 1.3.2.2 thorpej printf(" pte=v%p *pte=%x\n", ptep, *ptep);
359 1.3.2.2 thorpej #endif /* DEBUG_DMA */
360 1.3.2.2 thorpej }
361 1.3.2.2 thorpej }
362 1.3.2.2 thorpej pmap_update(pmap_kernel());
363 1.3.2.2 thorpej #ifdef DEBUG_DMA
364 1.3.2.2 thorpej printf("dmamem_map: =%p\n", *kvap);
365 1.3.2.2 thorpej #endif /* DEBUG_DMA */
366 1.3.2.2 thorpej return (0);
367 1.3.2.2 thorpej }
368 1.3.2.2 thorpej
369 1.3.2.2 thorpej /*
370 1.3.2.2 thorpej * Common functin for mmap(2)'ing DMA-safe memory. May be called by
371 1.3.2.2 thorpej * bus-specific DMA mmap(2)'ing functions.
372 1.3.2.2 thorpej */
373 1.3.2.2 thorpej paddr_t
374 1.3.2.2 thorpej integrator_bus_dmamem_mmap(t, segs, nsegs, off, prot, flags)
375 1.3.2.2 thorpej bus_dma_tag_t t;
376 1.3.2.2 thorpej bus_dma_segment_t *segs;
377 1.3.2.2 thorpej int nsegs;
378 1.3.2.2 thorpej off_t off;
379 1.3.2.2 thorpej int prot, flags;
380 1.3.2.2 thorpej {
381 1.3.2.2 thorpej int i;
382 1.3.2.2 thorpej
383 1.3.2.2 thorpej for (i = 0; i < nsegs; i++) {
384 1.3.2.2 thorpej #ifdef DIAGNOSTIC
385 1.3.2.2 thorpej if (off & PGOFSET)
386 1.3.2.2 thorpej panic("integrator_bus_dmamem_mmap: offset unaligned");
387 1.3.2.2 thorpej if (segs[i].ds_addr & PGOFSET)
388 1.3.2.2 thorpej panic("integrator_bus_dmamem_mmap: segment unaligned");
389 1.3.2.2 thorpej if (segs[i].ds_len & PGOFSET)
390 1.3.2.2 thorpej panic("integrator_bus_dmamem_mmap: segment size not multiple"
391 1.3.2.2 thorpej " of page size");
392 1.3.2.2 thorpej #endif /* DIAGNOSTIC */
393 1.3.2.2 thorpej if (off >= segs[i].ds_len) {
394 1.3.2.2 thorpej off -= segs[i].ds_len;
395 1.3.2.2 thorpej continue;
396 1.3.2.2 thorpej }
397 1.3.2.2 thorpej
398 1.3.2.4 jdolecek return arm_btop((u_long)CM_ALIAS_TO_LOCAL(segs[i].ds_addr) + off);
399 1.3.2.2 thorpej }
400 1.3.2.2 thorpej
401 1.3.2.2 thorpej /* Page not found. */
402 1.3.2.2 thorpej return -1;
403 1.3.2.2 thorpej }
404 1.3.2.2 thorpej
405 1.3.2.2 thorpej /**********************************************************************
406 1.3.2.2 thorpej * DMA utility functions
407 1.3.2.2 thorpej **********************************************************************/
408 1.3.2.2 thorpej
409 1.3.2.2 thorpej /*
410 1.3.2.2 thorpej * Utility function to load a linear buffer. lastaddrp holds state
411 1.3.2.2 thorpej * between invocations (for multiple-buffer loads). segp contains
412 1.3.2.2 thorpej * the starting segment on entrace, and the ending segment on exit.
413 1.3.2.2 thorpej * first indicates if this is the first invocation of this function.
414 1.3.2.2 thorpej */
415 1.3.2.2 thorpej static int
416 1.3.2.2 thorpej integrator_bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp,
417 1.3.2.2 thorpej segp, first)
418 1.3.2.2 thorpej bus_dma_tag_t t;
419 1.3.2.2 thorpej bus_dmamap_t map;
420 1.3.2.2 thorpej void *buf;
421 1.3.2.2 thorpej bus_size_t buflen;
422 1.3.2.2 thorpej struct proc *p;
423 1.3.2.2 thorpej int flags;
424 1.3.2.2 thorpej vm_offset_t *lastaddrp;
425 1.3.2.2 thorpej int *segp;
426 1.3.2.2 thorpej int first;
427 1.3.2.2 thorpej {
428 1.3.2.2 thorpej bus_size_t sgsize;
429 1.3.2.2 thorpej bus_addr_t curaddr, lastaddr, baddr, bmask;
430 1.3.2.2 thorpej vm_offset_t vaddr = (vm_offset_t)buf;
431 1.3.2.2 thorpej int seg;
432 1.3.2.2 thorpej pmap_t pmap;
433 1.3.2.2 thorpej
434 1.3.2.2 thorpej #ifdef DEBUG_DMA
435 1.3.2.2 thorpej printf("integrator_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d, 1st=%d)\n",
436 1.3.2.2 thorpej buf, buflen, flags, first);
437 1.3.2.2 thorpej #endif /* DEBUG_DMA */
438 1.3.2.2 thorpej
439 1.3.2.2 thorpej if (p != NULL)
440 1.3.2.2 thorpej pmap = p->p_vmspace->vm_map.pmap;
441 1.3.2.2 thorpej else
442 1.3.2.2 thorpej pmap = pmap_kernel();
443 1.3.2.2 thorpej
444 1.3.2.2 thorpej lastaddr = *lastaddrp;
445 1.3.2.2 thorpej bmask = ~(map->_dm_boundary - 1);
446 1.3.2.2 thorpej
447 1.3.2.2 thorpej for (seg = *segp; buflen > 0; ) {
448 1.3.2.2 thorpej /*
449 1.3.2.2 thorpej * Get the physical address for this segment.
450 1.3.2.2 thorpej */
451 1.3.2.2 thorpej (void) pmap_extract(pmap, (vaddr_t)vaddr, &curaddr);
452 1.3.2.2 thorpej
453 1.3.2.2 thorpej /*
454 1.3.2.2 thorpej * Make sure we're in an allowed DMA range.
455 1.3.2.2 thorpej */
456 1.3.2.2 thorpej if (t->_ranges != NULL &&
457 1.3.2.2 thorpej integrator_bus_dma_inrange(t->_ranges, t->_nranges, curaddr) == 0)
458 1.3.2.2 thorpej return (EINVAL);
459 1.3.2.2 thorpej
460 1.3.2.2 thorpej /*
461 1.3.2.2 thorpej * Compute the segment size, and adjust counts.
462 1.3.2.2 thorpej */
463 1.3.2.2 thorpej sgsize = NBPG - ((u_long)vaddr & PGOFSET);
464 1.3.2.2 thorpej if (buflen < sgsize)
465 1.3.2.2 thorpej sgsize = buflen;
466 1.3.2.2 thorpej
467 1.3.2.2 thorpej /*
468 1.3.2.2 thorpej * Make sure we don't cross any boundaries.
469 1.3.2.2 thorpej */
470 1.3.2.2 thorpej if (map->_dm_boundary > 0) {
471 1.3.2.2 thorpej baddr = (curaddr + map->_dm_boundary) & bmask;
472 1.3.2.2 thorpej if (sgsize > (baddr - curaddr))
473 1.3.2.2 thorpej sgsize = (baddr - curaddr);
474 1.3.2.2 thorpej }
475 1.3.2.2 thorpej
476 1.3.2.2 thorpej /*
477 1.3.2.2 thorpej * Insert chunk into a segment, coalescing with
478 1.3.2.2 thorpej * previous segment if possible.
479 1.3.2.2 thorpej */
480 1.3.2.2 thorpej if (first) {
481 1.3.2.2 thorpej map->dm_segs[seg].ds_addr = LOCAL_TO_CM_ALIAS(curaddr);
482 1.3.2.2 thorpej map->dm_segs[seg].ds_len = sgsize;
483 1.3.2.2 thorpej map->dm_segs[seg]._ds_vaddr = vaddr;
484 1.3.2.2 thorpej first = 0;
485 1.3.2.2 thorpej } else {
486 1.3.2.2 thorpej if (curaddr == lastaddr &&
487 1.3.2.2 thorpej (map->dm_segs[seg].ds_len + sgsize) <=
488 1.3.2.2 thorpej map->_dm_maxsegsz &&
489 1.3.2.2 thorpej (map->_dm_boundary == 0 ||
490 1.3.2.2 thorpej (map->dm_segs[seg].ds_addr & bmask) ==
491 1.3.2.2 thorpej (LOCAL_TO_CM_ALIAS(curaddr) & bmask)))
492 1.3.2.2 thorpej map->dm_segs[seg].ds_len += sgsize;
493 1.3.2.2 thorpej else {
494 1.3.2.2 thorpej if (++seg >= map->_dm_segcnt)
495 1.3.2.2 thorpej break;
496 1.3.2.2 thorpej map->dm_segs[seg].ds_addr = LOCAL_TO_CM_ALIAS(curaddr);
497 1.3.2.2 thorpej map->dm_segs[seg].ds_len = sgsize;
498 1.3.2.2 thorpej map->dm_segs[seg]._ds_vaddr = vaddr;
499 1.3.2.2 thorpej }
500 1.3.2.2 thorpej }
501 1.3.2.2 thorpej
502 1.3.2.2 thorpej lastaddr = curaddr + sgsize;
503 1.3.2.2 thorpej vaddr += sgsize;
504 1.3.2.2 thorpej buflen -= sgsize;
505 1.3.2.2 thorpej }
506 1.3.2.2 thorpej
507 1.3.2.2 thorpej *segp = seg;
508 1.3.2.2 thorpej *lastaddrp = lastaddr;
509 1.3.2.2 thorpej
510 1.3.2.2 thorpej /*
511 1.3.2.2 thorpej * Did we fit?
512 1.3.2.2 thorpej */
513 1.3.2.2 thorpej if (buflen != 0)
514 1.3.2.2 thorpej return (EFBIG); /* XXX better return value here? */
515 1.3.2.2 thorpej return (0);
516 1.3.2.2 thorpej }
517 1.3.2.2 thorpej
518 1.3.2.2 thorpej /*
519 1.3.2.2 thorpej * Check to see if the specified page is in an allowed DMA range.
520 1.3.2.2 thorpej */
521 1.3.2.2 thorpej static int
522 1.3.2.2 thorpej integrator_bus_dma_inrange(ranges, nranges, curaddr)
523 1.3.2.2 thorpej bus_dma_segment_t *ranges;
524 1.3.2.2 thorpej int nranges;
525 1.3.2.2 thorpej bus_addr_t curaddr;
526 1.3.2.2 thorpej {
527 1.3.2.2 thorpej bus_dma_segment_t *ds;
528 1.3.2.2 thorpej int i;
529 1.3.2.2 thorpej
530 1.3.2.2 thorpej for (i = 0, ds = ranges; i < nranges; i++, ds++) {
531 1.3.2.2 thorpej if (curaddr >= CM_ALIAS_TO_LOCAL(ds->ds_addr) &&
532 1.3.2.2 thorpej round_page(curaddr) <= (CM_ALIAS_TO_LOCAL(ds->ds_addr) + ds->ds_len))
533 1.3.2.2 thorpej return (1);
534 1.3.2.2 thorpej }
535 1.3.2.2 thorpej
536 1.3.2.2 thorpej return (0);
537 1.3.2.2 thorpej }
538 1.3.2.2 thorpej
539 1.3.2.2 thorpej /*
540 1.3.2.2 thorpej * Allocate physical memory from the given physical address range.
541 1.3.2.2 thorpej * Called by DMA-safe memory allocation methods.
542 1.3.2.2 thorpej */
543 1.3.2.2 thorpej int
544 1.3.2.2 thorpej integrator_bus_dmamem_alloc_range(t, size, alignment, boundary, segs, nsegs, rsegs,
545 1.3.2.2 thorpej flags, low, high)
546 1.3.2.2 thorpej bus_dma_tag_t t;
547 1.3.2.2 thorpej bus_size_t size, alignment, boundary;
548 1.3.2.2 thorpej bus_dma_segment_t *segs;
549 1.3.2.2 thorpej int nsegs;
550 1.3.2.2 thorpej int *rsegs;
551 1.3.2.2 thorpej int flags;
552 1.3.2.2 thorpej vm_offset_t low;
553 1.3.2.2 thorpej vm_offset_t high;
554 1.3.2.2 thorpej {
555 1.3.2.2 thorpej vm_offset_t curaddr, lastaddr;
556 1.3.2.2 thorpej struct vm_page *m;
557 1.3.2.2 thorpej struct pglist mlist;
558 1.3.2.2 thorpej int curseg, error;
559 1.3.2.2 thorpej
560 1.3.2.2 thorpej #ifdef DEBUG_DMA
561 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",
562 1.3.2.2 thorpej t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
563 1.3.2.2 thorpej #endif /* DEBUG_DMA */
564 1.3.2.2 thorpej
565 1.3.2.2 thorpej /* Always round the size. */
566 1.3.2.2 thorpej size = round_page(size);
567 1.3.2.2 thorpej
568 1.3.2.2 thorpej /*
569 1.3.2.2 thorpej * Allocate pages from the VM system.
570 1.3.2.2 thorpej */
571 1.3.2.2 thorpej error = uvm_pglistalloc(size, low, high, alignment, boundary,
572 1.3.2.2 thorpej &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
573 1.3.2.2 thorpej if (error)
574 1.3.2.2 thorpej return (error);
575 1.3.2.2 thorpej
576 1.3.2.2 thorpej /*
577 1.3.2.2 thorpej * Compute the location, size, and number of segments actually
578 1.3.2.2 thorpej * returned by the VM code.
579 1.3.2.2 thorpej */
580 1.3.2.2 thorpej m = mlist.tqh_first;
581 1.3.2.2 thorpej curseg = 0;
582 1.3.2.2 thorpej lastaddr = VM_PAGE_TO_PHYS(m);
583 1.3.2.2 thorpej segs[curseg].ds_addr = LOCAL_TO_CM_ALIAS(lastaddr);
584 1.3.2.2 thorpej segs[curseg].ds_len = PAGE_SIZE;
585 1.3.2.2 thorpej #ifdef DEBUG_DMA
586 1.3.2.2 thorpej printf("alloc: page %lx\n", lastaddr);
587 1.3.2.2 thorpej #endif /* DEBUG_DMA */
588 1.3.2.2 thorpej m = m->pageq.tqe_next;
589 1.3.2.2 thorpej
590 1.3.2.2 thorpej for (; m != NULL; m = m->pageq.tqe_next) {
591 1.3.2.2 thorpej curaddr = VM_PAGE_TO_PHYS(m);
592 1.3.2.2 thorpej #ifdef DIAGNOSTIC
593 1.3.2.2 thorpej if (curaddr < low || curaddr >= high) {
594 1.3.2.2 thorpej printf("uvm_pglistalloc returned non-sensical"
595 1.3.2.2 thorpej " address 0x%lx\n", curaddr);
596 1.3.2.2 thorpej panic("integrator_bus_dmamem_alloc_range");
597 1.3.2.2 thorpej }
598 1.3.2.2 thorpej #endif /* DIAGNOSTIC */
599 1.3.2.2 thorpej #ifdef DEBUG_DMA
600 1.3.2.2 thorpej printf("alloc: page %lx\n", curaddr);
601 1.3.2.2 thorpej #endif /* DEBUG_DMA */
602 1.3.2.2 thorpej if (curaddr == (lastaddr + PAGE_SIZE))
603 1.3.2.2 thorpej segs[curseg].ds_len += PAGE_SIZE;
604 1.3.2.2 thorpej else {
605 1.3.2.2 thorpej curseg++;
606 1.3.2.2 thorpej segs[curseg].ds_addr = LOCAL_TO_CM_ALIAS(curaddr);
607 1.3.2.2 thorpej segs[curseg].ds_len = PAGE_SIZE;
608 1.3.2.2 thorpej }
609 1.3.2.2 thorpej lastaddr = curaddr;
610 1.3.2.2 thorpej }
611 1.3.2.2 thorpej
612 1.3.2.2 thorpej *rsegs = curseg + 1;
613 1.3.2.2 thorpej
614 1.3.2.2 thorpej return (0);
615 1.3.2.2 thorpej }
616