bus_dma.c revision 1.37 1 1.37 mycroft /* $NetBSD: bus_dma.c,v 1.37 2003/10/29 05:03:41 mycroft Exp $ */
2 1.1 chris
3 1.1 chris /*-
4 1.1 chris * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 1.1 chris * All rights reserved.
6 1.1 chris *
7 1.1 chris * This code is derived from software contributed to The NetBSD Foundation
8 1.1 chris * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 chris * NASA Ames Research Center.
10 1.1 chris *
11 1.1 chris * Redistribution and use in source and binary forms, with or without
12 1.1 chris * modification, are permitted provided that the following conditions
13 1.1 chris * are met:
14 1.1 chris * 1. Redistributions of source code must retain the above copyright
15 1.1 chris * notice, this list of conditions and the following disclaimer.
16 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 chris * notice, this list of conditions and the following disclaimer in the
18 1.1 chris * documentation and/or other materials provided with the distribution.
19 1.1 chris * 3. All advertising materials mentioning features or use of this software
20 1.1 chris * must display the following acknowledgement:
21 1.1 chris * This product includes software developed by the NetBSD
22 1.1 chris * Foundation, Inc. and its contributors.
23 1.1 chris * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 chris * contributors may be used to endorse or promote products derived
25 1.1 chris * from this software without specific prior written permission.
26 1.1 chris *
27 1.1 chris * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 chris * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 chris * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 chris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 chris * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 chris * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 chris * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 chris * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 chris * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 chris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 chris * POSSIBILITY OF SUCH DAMAGE.
38 1.1 chris */
39 1.33 lukem
40 1.35 rearnsha #define _ARM32_BUS_DMA_PRIVATE
41 1.35 rearnsha
42 1.33 lukem #include <sys/cdefs.h>
43 1.37 mycroft __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.37 2003/10/29 05:03:41 mycroft Exp $");
44 1.1 chris
45 1.1 chris #include <sys/param.h>
46 1.1 chris #include <sys/systm.h>
47 1.1 chris #include <sys/kernel.h>
48 1.1 chris #include <sys/proc.h>
49 1.1 chris #include <sys/buf.h>
50 1.1 chris #include <sys/reboot.h>
51 1.1 chris #include <sys/conf.h>
52 1.1 chris #include <sys/file.h>
53 1.1 chris #include <sys/malloc.h>
54 1.1 chris #include <sys/mbuf.h>
55 1.1 chris #include <sys/vnode.h>
56 1.1 chris #include <sys/device.h>
57 1.1 chris
58 1.1 chris #include <uvm/uvm_extern.h>
59 1.1 chris
60 1.1 chris #include <machine/bus.h>
61 1.1 chris #include <machine/cpu.h>
62 1.4 thorpej
63 1.4 thorpej #include <arm/cpufunc.h>
64 1.1 chris
65 1.7 thorpej int _bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
66 1.11 thorpej bus_size_t, struct proc *, int, paddr_t *, int *, int);
67 1.15 thorpej struct arm32_dma_range *_bus_dma_inrange(struct arm32_dma_range *,
68 1.15 thorpej int, bus_addr_t);
69 1.1 chris
70 1.1 chris /*
71 1.19 briggs * Check to see if the specified page is in an allowed DMA range.
72 1.19 briggs */
73 1.19 briggs __inline struct arm32_dma_range *
74 1.19 briggs _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
75 1.19 briggs bus_addr_t curaddr)
76 1.19 briggs {
77 1.19 briggs struct arm32_dma_range *dr;
78 1.19 briggs int i;
79 1.19 briggs
80 1.19 briggs for (i = 0, dr = ranges; i < nranges; i++, dr++) {
81 1.19 briggs if (curaddr >= dr->dr_sysbase &&
82 1.19 briggs round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
83 1.19 briggs return (dr);
84 1.19 briggs }
85 1.19 briggs
86 1.19 briggs return (NULL);
87 1.19 briggs }
88 1.19 briggs
89 1.19 briggs /*
90 1.1 chris * Common function for DMA map creation. May be called by bus-specific
91 1.1 chris * DMA map creation functions.
92 1.1 chris */
93 1.1 chris int
94 1.7 thorpej _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
95 1.7 thorpej bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
96 1.1 chris {
97 1.1 chris struct arm32_bus_dmamap *map;
98 1.1 chris void *mapstore;
99 1.1 chris size_t mapsize;
100 1.1 chris
101 1.1 chris #ifdef DEBUG_DMA
102 1.1 chris printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
103 1.1 chris t, size, nsegments, maxsegsz, boundary, flags);
104 1.1 chris #endif /* DEBUG_DMA */
105 1.1 chris
106 1.1 chris /*
107 1.1 chris * Allocate and initialize the DMA map. The end of the map
108 1.1 chris * is a variable-sized array of segments, so we allocate enough
109 1.1 chris * room for them in one shot.
110 1.1 chris *
111 1.1 chris * Note we don't preserve the WAITOK or NOWAIT flags. Preservation
112 1.1 chris * of ALLOCNOW notifies others that we've reserved these resources,
113 1.1 chris * and they are not to be freed.
114 1.1 chris *
115 1.1 chris * The bus_dmamap_t includes one bus_dma_segment_t, hence
116 1.1 chris * the (nsegments - 1).
117 1.1 chris */
118 1.1 chris mapsize = sizeof(struct arm32_bus_dmamap) +
119 1.1 chris (sizeof(bus_dma_segment_t) * (nsegments - 1));
120 1.1 chris if ((mapstore = malloc(mapsize, M_DMAMAP,
121 1.1 chris (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
122 1.1 chris return (ENOMEM);
123 1.1 chris
124 1.1 chris memset(mapstore, 0, mapsize);
125 1.1 chris map = (struct arm32_bus_dmamap *)mapstore;
126 1.1 chris map->_dm_size = size;
127 1.1 chris map->_dm_segcnt = nsegments;
128 1.1 chris map->_dm_maxsegsz = maxsegsz;
129 1.1 chris map->_dm_boundary = boundary;
130 1.1 chris map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
131 1.14 thorpej map->_dm_origbuf = NULL;
132 1.14 thorpej map->_dm_buftype = ARM32_BUFTYPE_INVALID;
133 1.8 thorpej map->_dm_proc = NULL;
134 1.1 chris map->dm_mapsize = 0; /* no valid mappings */
135 1.1 chris map->dm_nsegs = 0;
136 1.1 chris
137 1.1 chris *dmamp = map;
138 1.1 chris #ifdef DEBUG_DMA
139 1.1 chris printf("dmamap_create:map=%p\n", map);
140 1.1 chris #endif /* DEBUG_DMA */
141 1.1 chris return (0);
142 1.1 chris }
143 1.1 chris
144 1.1 chris /*
145 1.1 chris * Common function for DMA map destruction. May be called by bus-specific
146 1.1 chris * DMA map destruction functions.
147 1.1 chris */
148 1.1 chris void
149 1.7 thorpej _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
150 1.1 chris {
151 1.1 chris
152 1.1 chris #ifdef DEBUG_DMA
153 1.1 chris printf("dmamap_destroy: t=%p map=%p\n", t, map);
154 1.1 chris #endif /* DEBUG_DMA */
155 1.13 briggs
156 1.13 briggs /*
157 1.13 briggs * Explicit unload.
158 1.13 briggs */
159 1.13 briggs map->dm_mapsize = 0;
160 1.13 briggs map->dm_nsegs = 0;
161 1.14 thorpej map->_dm_origbuf = NULL;
162 1.14 thorpej map->_dm_buftype = ARM32_BUFTYPE_INVALID;
163 1.13 briggs map->_dm_proc = NULL;
164 1.13 briggs
165 1.25 chris free(map, M_DMAMAP);
166 1.1 chris }
167 1.1 chris
168 1.1 chris /*
169 1.1 chris * Common function for loading a DMA map with a linear buffer. May
170 1.1 chris * be called by bus-specific DMA map load functions.
171 1.1 chris */
172 1.1 chris int
173 1.7 thorpej _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
174 1.7 thorpej bus_size_t buflen, struct proc *p, int flags)
175 1.1 chris {
176 1.11 thorpej paddr_t lastaddr;
177 1.1 chris int seg, error;
178 1.1 chris
179 1.1 chris #ifdef DEBUG_DMA
180 1.1 chris printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
181 1.1 chris t, map, buf, buflen, p, flags);
182 1.1 chris #endif /* DEBUG_DMA */
183 1.1 chris
184 1.1 chris /*
185 1.1 chris * Make sure that on error condition we return "no valid mappings".
186 1.1 chris */
187 1.1 chris map->dm_mapsize = 0;
188 1.1 chris map->dm_nsegs = 0;
189 1.1 chris
190 1.1 chris if (buflen > map->_dm_size)
191 1.1 chris return (EINVAL);
192 1.1 chris
193 1.17 thorpej /* _bus_dmamap_load_buffer() clears this if we're not... */
194 1.17 thorpej map->_dm_flags |= ARM32_DMAMAP_COHERENT;
195 1.17 thorpej
196 1.1 chris seg = 0;
197 1.1 chris error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
198 1.1 chris &lastaddr, &seg, 1);
199 1.1 chris if (error == 0) {
200 1.1 chris map->dm_mapsize = buflen;
201 1.1 chris map->dm_nsegs = seg + 1;
202 1.14 thorpej map->_dm_origbuf = buf;
203 1.14 thorpej map->_dm_buftype = ARM32_BUFTYPE_LINEAR;
204 1.8 thorpej map->_dm_proc = p;
205 1.1 chris }
206 1.1 chris #ifdef DEBUG_DMA
207 1.1 chris printf("dmamap_load: error=%d\n", error);
208 1.1 chris #endif /* DEBUG_DMA */
209 1.1 chris return (error);
210 1.1 chris }
211 1.1 chris
212 1.1 chris /*
213 1.1 chris * Like _bus_dmamap_load(), but for mbufs.
214 1.1 chris */
215 1.1 chris int
216 1.7 thorpej _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
217 1.7 thorpej int flags)
218 1.1 chris {
219 1.28 thorpej struct arm32_dma_range *dr;
220 1.11 thorpej paddr_t lastaddr;
221 1.1 chris int seg, error, first;
222 1.1 chris struct mbuf *m;
223 1.1 chris
224 1.1 chris #ifdef DEBUG_DMA
225 1.1 chris printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
226 1.1 chris t, map, m0, flags);
227 1.1 chris #endif /* DEBUG_DMA */
228 1.1 chris
229 1.1 chris /*
230 1.1 chris * Make sure that on error condition we return "no valid mappings."
231 1.1 chris */
232 1.1 chris map->dm_mapsize = 0;
233 1.1 chris map->dm_nsegs = 0;
234 1.1 chris
235 1.1 chris #ifdef DIAGNOSTIC
236 1.1 chris if ((m0->m_flags & M_PKTHDR) == 0)
237 1.1 chris panic("_bus_dmamap_load_mbuf: no packet header");
238 1.1 chris #endif /* DIAGNOSTIC */
239 1.1 chris
240 1.1 chris if (m0->m_pkthdr.len > map->_dm_size)
241 1.1 chris return (EINVAL);
242 1.1 chris
243 1.28 thorpej /*
244 1.28 thorpej * Mbuf chains should almost never have coherent (i.e.
245 1.28 thorpej * un-cached) mappings, so clear that flag now.
246 1.28 thorpej */
247 1.28 thorpej map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
248 1.17 thorpej
249 1.1 chris first = 1;
250 1.1 chris seg = 0;
251 1.1 chris error = 0;
252 1.1 chris for (m = m0; m != NULL && error == 0; m = m->m_next) {
253 1.28 thorpej if (m->m_len == 0)
254 1.28 thorpej continue;
255 1.28 thorpej /* XXX Could be better about coalescing. */
256 1.28 thorpej /* XXX Doesn't check boundaries. */
257 1.28 thorpej switch (m->m_flags & (M_EXT|M_CLUSTER)) {
258 1.28 thorpej case M_EXT|M_CLUSTER:
259 1.28 thorpej /* XXX KDASSERT */
260 1.28 thorpej KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
261 1.28 thorpej lastaddr = m->m_ext.ext_paddr +
262 1.28 thorpej (m->m_data - m->m_ext.ext_buf);
263 1.28 thorpej have_addr:
264 1.28 thorpej if (first == 0 &&
265 1.28 thorpej ++seg >= map->_dm_segcnt) {
266 1.28 thorpej error = EFBIG;
267 1.28 thorpej break;
268 1.28 thorpej }
269 1.28 thorpej /*
270 1.28 thorpej * Make sure we're in an allowed DMA range.
271 1.28 thorpej */
272 1.28 thorpej if (t->_ranges != NULL) {
273 1.28 thorpej /* XXX cache last result? */
274 1.28 thorpej dr = _bus_dma_inrange(t->_ranges, t->_nranges,
275 1.28 thorpej lastaddr);
276 1.28 thorpej if (dr == NULL) {
277 1.28 thorpej error = EINVAL;
278 1.28 thorpej break;
279 1.28 thorpej }
280 1.28 thorpej
281 1.28 thorpej /*
282 1.28 thorpej * In a valid DMA range. Translate the
283 1.28 thorpej * physical memory address to an address
284 1.28 thorpej * in the DMA window.
285 1.28 thorpej */
286 1.28 thorpej lastaddr = (lastaddr - dr->dr_sysbase) +
287 1.28 thorpej dr->dr_busbase;
288 1.28 thorpej }
289 1.28 thorpej map->dm_segs[seg].ds_addr = lastaddr;
290 1.28 thorpej map->dm_segs[seg].ds_len = m->m_len;
291 1.28 thorpej lastaddr += m->m_len;
292 1.28 thorpej break;
293 1.28 thorpej
294 1.28 thorpej case 0:
295 1.28 thorpej lastaddr = m->m_paddr + M_BUFOFFSET(m) +
296 1.28 thorpej (m->m_data - M_BUFADDR(m));
297 1.28 thorpej goto have_addr;
298 1.28 thorpej
299 1.28 thorpej default:
300 1.28 thorpej error = _bus_dmamap_load_buffer(t, map, m->m_data,
301 1.28 thorpej m->m_len, NULL, flags, &lastaddr, &seg, first);
302 1.28 thorpej }
303 1.1 chris first = 0;
304 1.1 chris }
305 1.1 chris if (error == 0) {
306 1.1 chris map->dm_mapsize = m0->m_pkthdr.len;
307 1.1 chris map->dm_nsegs = seg + 1;
308 1.14 thorpej map->_dm_origbuf = m0;
309 1.14 thorpej map->_dm_buftype = ARM32_BUFTYPE_MBUF;
310 1.8 thorpej map->_dm_proc = NULL; /* always kernel */
311 1.1 chris }
312 1.1 chris #ifdef DEBUG_DMA
313 1.1 chris printf("dmamap_load_mbuf: error=%d\n", error);
314 1.1 chris #endif /* DEBUG_DMA */
315 1.1 chris return (error);
316 1.1 chris }
317 1.1 chris
318 1.1 chris /*
319 1.1 chris * Like _bus_dmamap_load(), but for uios.
320 1.1 chris */
321 1.1 chris int
322 1.7 thorpej _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
323 1.7 thorpej int flags)
324 1.1 chris {
325 1.11 thorpej paddr_t lastaddr;
326 1.1 chris int seg, i, error, first;
327 1.1 chris bus_size_t minlen, resid;
328 1.1 chris struct proc *p = NULL;
329 1.1 chris struct iovec *iov;
330 1.1 chris caddr_t addr;
331 1.1 chris
332 1.1 chris /*
333 1.1 chris * Make sure that on error condition we return "no valid mappings."
334 1.1 chris */
335 1.1 chris map->dm_mapsize = 0;
336 1.1 chris map->dm_nsegs = 0;
337 1.1 chris
338 1.1 chris resid = uio->uio_resid;
339 1.1 chris iov = uio->uio_iov;
340 1.1 chris
341 1.1 chris if (uio->uio_segflg == UIO_USERSPACE) {
342 1.32 fvdl p = uio->uio_procp;
343 1.1 chris #ifdef DIAGNOSTIC
344 1.1 chris if (p == NULL)
345 1.1 chris panic("_bus_dmamap_load_uio: USERSPACE but no proc");
346 1.1 chris #endif
347 1.1 chris }
348 1.1 chris
349 1.17 thorpej /* _bus_dmamap_load_buffer() clears this if we're not... */
350 1.17 thorpej map->_dm_flags |= ARM32_DMAMAP_COHERENT;
351 1.17 thorpej
352 1.1 chris first = 1;
353 1.1 chris seg = 0;
354 1.1 chris error = 0;
355 1.1 chris for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
356 1.1 chris /*
357 1.1 chris * Now at the first iovec to load. Load each iovec
358 1.1 chris * until we have exhausted the residual count.
359 1.1 chris */
360 1.1 chris minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
361 1.1 chris addr = (caddr_t)iov[i].iov_base;
362 1.1 chris
363 1.1 chris error = _bus_dmamap_load_buffer(t, map, addr, minlen,
364 1.1 chris p, flags, &lastaddr, &seg, first);
365 1.1 chris first = 0;
366 1.1 chris
367 1.1 chris resid -= minlen;
368 1.1 chris }
369 1.1 chris if (error == 0) {
370 1.1 chris map->dm_mapsize = uio->uio_resid;
371 1.1 chris map->dm_nsegs = seg + 1;
372 1.14 thorpej map->_dm_origbuf = uio;
373 1.14 thorpej map->_dm_buftype = ARM32_BUFTYPE_UIO;
374 1.8 thorpej map->_dm_proc = p;
375 1.1 chris }
376 1.1 chris return (error);
377 1.1 chris }
378 1.1 chris
379 1.1 chris /*
380 1.1 chris * Like _bus_dmamap_load(), but for raw memory allocated with
381 1.1 chris * bus_dmamem_alloc().
382 1.1 chris */
383 1.1 chris int
384 1.7 thorpej _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
385 1.7 thorpej bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
386 1.1 chris {
387 1.1 chris
388 1.1 chris panic("_bus_dmamap_load_raw: not implemented");
389 1.1 chris }
390 1.1 chris
391 1.1 chris /*
392 1.1 chris * Common function for unloading a DMA map. May be called by
393 1.1 chris * bus-specific DMA map unload functions.
394 1.1 chris */
395 1.1 chris void
396 1.7 thorpej _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
397 1.1 chris {
398 1.1 chris
399 1.1 chris #ifdef DEBUG_DMA
400 1.1 chris printf("dmamap_unload: t=%p map=%p\n", t, map);
401 1.1 chris #endif /* DEBUG_DMA */
402 1.1 chris
403 1.1 chris /*
404 1.1 chris * No resources to free; just mark the mappings as
405 1.1 chris * invalid.
406 1.1 chris */
407 1.1 chris map->dm_mapsize = 0;
408 1.1 chris map->dm_nsegs = 0;
409 1.14 thorpej map->_dm_origbuf = NULL;
410 1.14 thorpej map->_dm_buftype = ARM32_BUFTYPE_INVALID;
411 1.8 thorpej map->_dm_proc = NULL;
412 1.1 chris }
413 1.1 chris
414 1.19 briggs static __inline void
415 1.14 thorpej _bus_dmamap_sync_linear(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
416 1.14 thorpej bus_size_t len, int ops)
417 1.14 thorpej {
418 1.14 thorpej vaddr_t addr = (vaddr_t) map->_dm_origbuf;
419 1.14 thorpej
420 1.14 thorpej addr += offset;
421 1.14 thorpej
422 1.14 thorpej switch (ops) {
423 1.14 thorpej case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
424 1.14 thorpej cpu_dcache_wbinv_range(addr, len);
425 1.14 thorpej break;
426 1.14 thorpej
427 1.14 thorpej case BUS_DMASYNC_PREREAD:
428 1.18 thorpej if (((addr | len) & arm_dcache_align_mask) == 0)
429 1.18 thorpej cpu_dcache_inv_range(addr, len);
430 1.18 thorpej else
431 1.18 thorpej cpu_dcache_wbinv_range(addr, len);
432 1.14 thorpej break;
433 1.14 thorpej
434 1.14 thorpej case BUS_DMASYNC_PREWRITE:
435 1.14 thorpej cpu_dcache_wb_range(addr, len);
436 1.14 thorpej break;
437 1.14 thorpej }
438 1.14 thorpej }
439 1.14 thorpej
440 1.19 briggs static __inline void
441 1.14 thorpej _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
442 1.14 thorpej bus_size_t len, int ops)
443 1.14 thorpej {
444 1.14 thorpej struct mbuf *m, *m0 = map->_dm_origbuf;
445 1.14 thorpej bus_size_t minlen, moff;
446 1.14 thorpej vaddr_t maddr;
447 1.14 thorpej
448 1.14 thorpej for (moff = offset, m = m0; m != NULL && len != 0;
449 1.14 thorpej m = m->m_next) {
450 1.14 thorpej /* Find the beginning mbuf. */
451 1.14 thorpej if (moff >= m->m_len) {
452 1.14 thorpej moff -= m->m_len;
453 1.14 thorpej continue;
454 1.14 thorpej }
455 1.14 thorpej
456 1.14 thorpej /*
457 1.14 thorpej * Now at the first mbuf to sync; nail each one until
458 1.14 thorpej * we have exhausted the length.
459 1.14 thorpej */
460 1.14 thorpej minlen = m->m_len - moff;
461 1.14 thorpej if (len < minlen)
462 1.14 thorpej minlen = len;
463 1.14 thorpej
464 1.14 thorpej maddr = mtod(m, vaddr_t);
465 1.14 thorpej maddr += moff;
466 1.14 thorpej
467 1.28 thorpej /*
468 1.28 thorpej * We can save a lot of work here if we know the mapping
469 1.28 thorpej * is read-only at the MMU:
470 1.28 thorpej *
471 1.28 thorpej * If a mapping is read-only, no dirty cache blocks will
472 1.28 thorpej * exist for it. If a writable mapping was made read-only,
473 1.28 thorpej * we know any dirty cache lines for the range will have
474 1.28 thorpej * been cleaned for us already. Therefore, if the upper
475 1.28 thorpej * layer can tell us we have a read-only mapping, we can
476 1.28 thorpej * skip all cache cleaning.
477 1.28 thorpej *
478 1.28 thorpej * NOTE: This only works if we know the pmap cleans pages
479 1.28 thorpej * before making a read-write -> read-only transition. If
480 1.28 thorpej * this ever becomes non-true (e.g. Physically Indexed
481 1.28 thorpej * cache), this will have to be revisited.
482 1.28 thorpej */
483 1.14 thorpej switch (ops) {
484 1.14 thorpej case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
485 1.28 thorpej if (! M_ROMAP(m)) {
486 1.28 thorpej cpu_dcache_wbinv_range(maddr, minlen);
487 1.28 thorpej break;
488 1.28 thorpej }
489 1.28 thorpej /* else FALLTHROUGH */
490 1.14 thorpej
491 1.14 thorpej case BUS_DMASYNC_PREREAD:
492 1.18 thorpej if (((maddr | minlen) & arm_dcache_align_mask) == 0)
493 1.18 thorpej cpu_dcache_inv_range(maddr, minlen);
494 1.18 thorpej else
495 1.18 thorpej cpu_dcache_wbinv_range(maddr, minlen);
496 1.14 thorpej break;
497 1.14 thorpej
498 1.14 thorpej case BUS_DMASYNC_PREWRITE:
499 1.28 thorpej if (! M_ROMAP(m))
500 1.28 thorpej cpu_dcache_wb_range(maddr, minlen);
501 1.14 thorpej break;
502 1.14 thorpej }
503 1.14 thorpej moff = 0;
504 1.14 thorpej len -= minlen;
505 1.14 thorpej }
506 1.14 thorpej }
507 1.14 thorpej
508 1.19 briggs static __inline void
509 1.14 thorpej _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
510 1.14 thorpej bus_size_t len, int ops)
511 1.14 thorpej {
512 1.14 thorpej struct uio *uio = map->_dm_origbuf;
513 1.14 thorpej struct iovec *iov;
514 1.14 thorpej bus_size_t minlen, ioff;
515 1.14 thorpej vaddr_t addr;
516 1.14 thorpej
517 1.14 thorpej for (iov = uio->uio_iov, ioff = offset; len != 0; iov++) {
518 1.14 thorpej /* Find the beginning iovec. */
519 1.14 thorpej if (ioff >= iov->iov_len) {
520 1.14 thorpej ioff -= iov->iov_len;
521 1.14 thorpej continue;
522 1.14 thorpej }
523 1.14 thorpej
524 1.14 thorpej /*
525 1.14 thorpej * Now at the first iovec to sync; nail each one until
526 1.14 thorpej * we have exhausted the length.
527 1.14 thorpej */
528 1.14 thorpej minlen = iov->iov_len - ioff;
529 1.14 thorpej if (len < minlen)
530 1.14 thorpej minlen = len;
531 1.14 thorpej
532 1.14 thorpej addr = (vaddr_t) iov->iov_base;
533 1.14 thorpej addr += ioff;
534 1.14 thorpej
535 1.14 thorpej switch (ops) {
536 1.14 thorpej case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
537 1.14 thorpej cpu_dcache_wbinv_range(addr, minlen);
538 1.14 thorpej break;
539 1.14 thorpej
540 1.14 thorpej case BUS_DMASYNC_PREREAD:
541 1.18 thorpej if (((addr | minlen) & arm_dcache_align_mask) == 0)
542 1.18 thorpej cpu_dcache_inv_range(addr, minlen);
543 1.18 thorpej else
544 1.18 thorpej cpu_dcache_wbinv_range(addr, minlen);
545 1.14 thorpej break;
546 1.14 thorpej
547 1.14 thorpej case BUS_DMASYNC_PREWRITE:
548 1.14 thorpej cpu_dcache_wb_range(addr, minlen);
549 1.14 thorpej break;
550 1.14 thorpej }
551 1.14 thorpej ioff = 0;
552 1.14 thorpej len -= minlen;
553 1.14 thorpej }
554 1.14 thorpej }
555 1.14 thorpej
556 1.1 chris /*
557 1.1 chris * Common function for DMA map synchronization. May be called
558 1.1 chris * by bus-specific DMA map synchronization functions.
559 1.8 thorpej *
560 1.8 thorpej * This version works for the Virtually Indexed Virtually Tagged
561 1.8 thorpej * cache found on 32-bit ARM processors.
562 1.8 thorpej *
563 1.8 thorpej * XXX Should have separate versions for write-through vs.
564 1.8 thorpej * XXX write-back caches. We currently assume write-back
565 1.8 thorpej * XXX here, which is not as efficient as it could be for
566 1.8 thorpej * XXX the write-through case.
567 1.1 chris */
568 1.1 chris void
569 1.7 thorpej _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
570 1.7 thorpej bus_size_t len, int ops)
571 1.1 chris {
572 1.1 chris
573 1.1 chris #ifdef DEBUG_DMA
574 1.1 chris printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
575 1.1 chris t, map, offset, len, ops);
576 1.1 chris #endif /* DEBUG_DMA */
577 1.1 chris
578 1.8 thorpej /*
579 1.8 thorpej * Mixing of PRE and POST operations is not allowed.
580 1.8 thorpej */
581 1.8 thorpej if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
582 1.8 thorpej (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
583 1.8 thorpej panic("_bus_dmamap_sync: mix PRE and POST");
584 1.8 thorpej
585 1.8 thorpej #ifdef DIAGNOSTIC
586 1.8 thorpej if (offset >= map->dm_mapsize)
587 1.8 thorpej panic("_bus_dmamap_sync: bad offset %lu (map size is %lu)",
588 1.8 thorpej offset, map->dm_mapsize);
589 1.8 thorpej if (len == 0 || (offset + len) > map->dm_mapsize)
590 1.8 thorpej panic("_bus_dmamap_sync: bad length");
591 1.8 thorpej #endif
592 1.8 thorpej
593 1.8 thorpej /*
594 1.8 thorpej * For a virtually-indexed write-back cache, we need
595 1.8 thorpej * to do the following things:
596 1.8 thorpej *
597 1.8 thorpej * PREREAD -- Invalidate the D-cache. We do this
598 1.8 thorpej * here in case a write-back is required by the back-end.
599 1.8 thorpej *
600 1.8 thorpej * PREWRITE -- Write-back the D-cache. Note that if
601 1.8 thorpej * we are doing a PREREAD|PREWRITE, we can collapse
602 1.8 thorpej * the whole thing into a single Wb-Inv.
603 1.8 thorpej *
604 1.8 thorpej * POSTREAD -- Nothing.
605 1.8 thorpej *
606 1.8 thorpej * POSTWRITE -- Nothing.
607 1.8 thorpej */
608 1.8 thorpej
609 1.8 thorpej ops &= (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
610 1.8 thorpej if (ops == 0)
611 1.8 thorpej return;
612 1.8 thorpej
613 1.17 thorpej /* Skip cache frobbing if mapping was COHERENT. */
614 1.17 thorpej if (map->_dm_flags & ARM32_DMAMAP_COHERENT) {
615 1.17 thorpej /* Drain the write buffer. */
616 1.17 thorpej cpu_drain_writebuf();
617 1.17 thorpej return;
618 1.17 thorpej }
619 1.8 thorpej
620 1.8 thorpej /*
621 1.8 thorpej * If the mapping is not the kernel's and also not the
622 1.8 thorpej * current process's (XXX actually, vmspace), then we
623 1.8 thorpej * don't have anything to do, since the cache is Wb-Inv'd
624 1.8 thorpej * on context switch.
625 1.8 thorpej *
626 1.8 thorpej * XXX REVISIT WHEN WE DO FCSE!
627 1.8 thorpej */
628 1.23 thorpej if (__predict_false(map->_dm_proc != NULL &&
629 1.23 thorpej curlwp != NULL && map->_dm_proc != curproc))
630 1.8 thorpej return;
631 1.8 thorpej
632 1.14 thorpej switch (map->_dm_buftype) {
633 1.14 thorpej case ARM32_BUFTYPE_LINEAR:
634 1.14 thorpej _bus_dmamap_sync_linear(t, map, offset, len, ops);
635 1.14 thorpej break;
636 1.14 thorpej
637 1.14 thorpej case ARM32_BUFTYPE_MBUF:
638 1.14 thorpej _bus_dmamap_sync_mbuf(t, map, offset, len, ops);
639 1.14 thorpej break;
640 1.14 thorpej
641 1.14 thorpej case ARM32_BUFTYPE_UIO:
642 1.14 thorpej _bus_dmamap_sync_uio(t, map, offset, len, ops);
643 1.14 thorpej break;
644 1.14 thorpej
645 1.14 thorpej case ARM32_BUFTYPE_RAW:
646 1.14 thorpej panic("_bus_dmamap_sync: ARM32_BUFTYPE_RAW");
647 1.14 thorpej break;
648 1.14 thorpej
649 1.14 thorpej case ARM32_BUFTYPE_INVALID:
650 1.14 thorpej panic("_bus_dmamap_sync: ARM32_BUFTYPE_INVALID");
651 1.14 thorpej break;
652 1.14 thorpej
653 1.14 thorpej default:
654 1.14 thorpej printf("unknown buffer type %d\n", map->_dm_buftype);
655 1.14 thorpej panic("_bus_dmamap_sync");
656 1.8 thorpej }
657 1.1 chris
658 1.8 thorpej /* Drain the write buffer. */
659 1.8 thorpej cpu_drain_writebuf();
660 1.1 chris }
661 1.1 chris
662 1.1 chris /*
663 1.1 chris * Common function for DMA-safe memory allocation. May be called
664 1.1 chris * by bus-specific DMA memory allocation functions.
665 1.1 chris */
666 1.1 chris
667 1.11 thorpej extern paddr_t physical_start;
668 1.11 thorpej extern paddr_t physical_end;
669 1.1 chris
670 1.1 chris int
671 1.7 thorpej _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
672 1.7 thorpej bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
673 1.7 thorpej int flags)
674 1.1 chris {
675 1.15 thorpej struct arm32_dma_range *dr;
676 1.37 mycroft int error, i;
677 1.15 thorpej
678 1.1 chris #ifdef DEBUG_DMA
679 1.15 thorpej printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx "
680 1.15 thorpej "segs=%p nsegs=%x rsegs=%p flags=%x\n", t, size, alignment,
681 1.15 thorpej boundary, segs, nsegs, rsegs, flags);
682 1.15 thorpej #endif
683 1.15 thorpej
684 1.15 thorpej if ((dr = t->_ranges) != NULL) {
685 1.37 mycroft error = ENOMEM;
686 1.15 thorpej for (i = 0; i < t->_nranges; i++, dr++) {
687 1.37 mycroft if (dr->dr_len == 0)
688 1.15 thorpej continue;
689 1.15 thorpej error = _bus_dmamem_alloc_range(t, size, alignment,
690 1.15 thorpej boundary, segs, nsegs, rsegs, flags,
691 1.15 thorpej trunc_page(dr->dr_sysbase),
692 1.15 thorpej trunc_page(dr->dr_sysbase + dr->dr_len));
693 1.15 thorpej if (error == 0)
694 1.15 thorpej break;
695 1.15 thorpej }
696 1.15 thorpej } else {
697 1.15 thorpej error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
698 1.15 thorpej segs, nsegs, rsegs, flags, trunc_page(physical_start),
699 1.15 thorpej trunc_page(physical_end));
700 1.15 thorpej }
701 1.15 thorpej
702 1.1 chris #ifdef DEBUG_DMA
703 1.1 chris printf("dmamem_alloc: =%d\n", error);
704 1.15 thorpej #endif
705 1.15 thorpej
706 1.1 chris return(error);
707 1.1 chris }
708 1.1 chris
709 1.1 chris /*
710 1.1 chris * Common function for freeing DMA-safe memory. May be called by
711 1.1 chris * bus-specific DMA memory free functions.
712 1.1 chris */
713 1.1 chris void
714 1.7 thorpej _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
715 1.1 chris {
716 1.1 chris struct vm_page *m;
717 1.1 chris bus_addr_t addr;
718 1.1 chris struct pglist mlist;
719 1.1 chris int curseg;
720 1.1 chris
721 1.1 chris #ifdef DEBUG_DMA
722 1.1 chris printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
723 1.1 chris #endif /* DEBUG_DMA */
724 1.1 chris
725 1.1 chris /*
726 1.1 chris * Build a list of pages to free back to the VM system.
727 1.1 chris */
728 1.1 chris TAILQ_INIT(&mlist);
729 1.1 chris for (curseg = 0; curseg < nsegs; curseg++) {
730 1.1 chris for (addr = segs[curseg].ds_addr;
731 1.1 chris addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
732 1.1 chris addr += PAGE_SIZE) {
733 1.1 chris m = PHYS_TO_VM_PAGE(addr);
734 1.1 chris TAILQ_INSERT_TAIL(&mlist, m, pageq);
735 1.1 chris }
736 1.1 chris }
737 1.1 chris uvm_pglistfree(&mlist);
738 1.1 chris }
739 1.1 chris
740 1.1 chris /*
741 1.1 chris * Common function for mapping DMA-safe memory. May be called by
742 1.1 chris * bus-specific DMA memory map functions.
743 1.1 chris */
744 1.1 chris int
745 1.7 thorpej _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
746 1.7 thorpej size_t size, caddr_t *kvap, int flags)
747 1.1 chris {
748 1.11 thorpej vaddr_t va;
749 1.1 chris bus_addr_t addr;
750 1.1 chris int curseg;
751 1.1 chris pt_entry_t *ptep/*, pte*/;
752 1.1 chris
753 1.1 chris #ifdef DEBUG_DMA
754 1.3 rearnsha printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
755 1.3 rearnsha segs, nsegs, (unsigned long)size, flags);
756 1.1 chris #endif /* DEBUG_DMA */
757 1.1 chris
758 1.1 chris size = round_page(size);
759 1.1 chris va = uvm_km_valloc(kernel_map, size);
760 1.1 chris
761 1.1 chris if (va == 0)
762 1.1 chris return (ENOMEM);
763 1.1 chris
764 1.1 chris *kvap = (caddr_t)va;
765 1.1 chris
766 1.1 chris for (curseg = 0; curseg < nsegs; curseg++) {
767 1.1 chris for (addr = segs[curseg].ds_addr;
768 1.1 chris addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
769 1.27 thorpej addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
770 1.1 chris #ifdef DEBUG_DMA
771 1.1 chris printf("wiring p%lx to v%lx", addr, va);
772 1.1 chris #endif /* DEBUG_DMA */
773 1.1 chris if (size == 0)
774 1.1 chris panic("_bus_dmamem_map: size botch");
775 1.1 chris pmap_enter(pmap_kernel(), va, addr,
776 1.1 chris VM_PROT_READ | VM_PROT_WRITE,
777 1.1 chris VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
778 1.1 chris /*
779 1.1 chris * If the memory must remain coherent with the
780 1.1 chris * cache then we must make the memory uncacheable
781 1.1 chris * in order to maintain virtual cache coherency.
782 1.24 wiz * We must also guarantee the cache does not already
783 1.1 chris * contain the virtal addresses we are making
784 1.1 chris * uncacheable.
785 1.1 chris */
786 1.1 chris if (flags & BUS_DMA_COHERENT) {
787 1.27 thorpej cpu_dcache_wbinv_range(va, PAGE_SIZE);
788 1.1 chris cpu_drain_writebuf();
789 1.1 chris ptep = vtopte(va);
790 1.17 thorpej *ptep &= ~L2_S_CACHE_MASK;
791 1.21 thorpej PTE_SYNC(ptep);
792 1.1 chris tlb_flush();
793 1.1 chris }
794 1.1 chris #ifdef DEBUG_DMA
795 1.1 chris ptep = vtopte(va);
796 1.1 chris printf(" pte=v%p *pte=%x\n", ptep, *ptep);
797 1.1 chris #endif /* DEBUG_DMA */
798 1.1 chris }
799 1.1 chris }
800 1.2 chris pmap_update(pmap_kernel());
801 1.1 chris #ifdef DEBUG_DMA
802 1.1 chris printf("dmamem_map: =%p\n", *kvap);
803 1.1 chris #endif /* DEBUG_DMA */
804 1.1 chris return (0);
805 1.1 chris }
806 1.1 chris
807 1.1 chris /*
808 1.1 chris * Common function for unmapping DMA-safe memory. May be called by
809 1.1 chris * bus-specific DMA memory unmapping functions.
810 1.1 chris */
811 1.1 chris void
812 1.7 thorpej _bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
813 1.1 chris {
814 1.1 chris
815 1.1 chris #ifdef DEBUG_DMA
816 1.3 rearnsha printf("dmamem_unmap: t=%p kva=%p size=%lx\n", t, kva,
817 1.3 rearnsha (unsigned long)size);
818 1.1 chris #endif /* DEBUG_DMA */
819 1.1 chris #ifdef DIAGNOSTIC
820 1.1 chris if ((u_long)kva & PGOFSET)
821 1.1 chris panic("_bus_dmamem_unmap");
822 1.1 chris #endif /* DIAGNOSTIC */
823 1.1 chris
824 1.1 chris size = round_page(size);
825 1.11 thorpej uvm_km_free(kernel_map, (vaddr_t)kva, size);
826 1.1 chris }
827 1.1 chris
828 1.1 chris /*
829 1.1 chris * Common functin for mmap(2)'ing DMA-safe memory. May be called by
830 1.1 chris * bus-specific DMA mmap(2)'ing functions.
831 1.1 chris */
832 1.1 chris paddr_t
833 1.7 thorpej _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
834 1.7 thorpej off_t off, int prot, int flags)
835 1.1 chris {
836 1.1 chris int i;
837 1.1 chris
838 1.1 chris for (i = 0; i < nsegs; i++) {
839 1.1 chris #ifdef DIAGNOSTIC
840 1.1 chris if (off & PGOFSET)
841 1.1 chris panic("_bus_dmamem_mmap: offset unaligned");
842 1.1 chris if (segs[i].ds_addr & PGOFSET)
843 1.1 chris panic("_bus_dmamem_mmap: segment unaligned");
844 1.1 chris if (segs[i].ds_len & PGOFSET)
845 1.1 chris panic("_bus_dmamem_mmap: segment size not multiple"
846 1.1 chris " of page size");
847 1.1 chris #endif /* DIAGNOSTIC */
848 1.1 chris if (off >= segs[i].ds_len) {
849 1.1 chris off -= segs[i].ds_len;
850 1.1 chris continue;
851 1.1 chris }
852 1.1 chris
853 1.9 thorpej return (arm_btop((u_long)segs[i].ds_addr + off));
854 1.1 chris }
855 1.1 chris
856 1.1 chris /* Page not found. */
857 1.1 chris return (-1);
858 1.1 chris }
859 1.1 chris
860 1.1 chris /**********************************************************************
861 1.1 chris * DMA utility functions
862 1.1 chris **********************************************************************/
863 1.1 chris
864 1.1 chris /*
865 1.1 chris * Utility function to load a linear buffer. lastaddrp holds state
866 1.1 chris * between invocations (for multiple-buffer loads). segp contains
867 1.1 chris * the starting segment on entrace, and the ending segment on exit.
868 1.1 chris * first indicates if this is the first invocation of this function.
869 1.1 chris */
870 1.1 chris int
871 1.7 thorpej _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
872 1.11 thorpej bus_size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
873 1.7 thorpej int *segp, int first)
874 1.1 chris {
875 1.15 thorpej struct arm32_dma_range *dr;
876 1.1 chris bus_size_t sgsize;
877 1.1 chris bus_addr_t curaddr, lastaddr, baddr, bmask;
878 1.11 thorpej vaddr_t vaddr = (vaddr_t)buf;
879 1.17 thorpej pd_entry_t *pde;
880 1.17 thorpej pt_entry_t pte;
881 1.1 chris int seg;
882 1.1 chris pmap_t pmap;
883 1.29 scw pt_entry_t *ptep;
884 1.1 chris
885 1.1 chris #ifdef DEBUG_DMA
886 1.1 chris printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d, 1st=%d)\n",
887 1.1 chris buf, buflen, flags, first);
888 1.1 chris #endif /* DEBUG_DMA */
889 1.1 chris
890 1.1 chris if (p != NULL)
891 1.1 chris pmap = p->p_vmspace->vm_map.pmap;
892 1.1 chris else
893 1.1 chris pmap = pmap_kernel();
894 1.1 chris
895 1.1 chris lastaddr = *lastaddrp;
896 1.1 chris bmask = ~(map->_dm_boundary - 1);
897 1.1 chris
898 1.1 chris for (seg = *segp; buflen > 0; ) {
899 1.1 chris /*
900 1.1 chris * Get the physical address for this segment.
901 1.17 thorpej *
902 1.17 thorpej * XXX Don't support checking for coherent mappings
903 1.17 thorpej * XXX in user address space.
904 1.1 chris */
905 1.17 thorpej if (__predict_true(pmap == pmap_kernel())) {
906 1.29 scw (void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
907 1.17 thorpej if (__predict_false(pmap_pde_section(pde))) {
908 1.17 thorpej curaddr = (*pde & L1_S_FRAME) |
909 1.17 thorpej (vaddr & L1_S_OFFSET);
910 1.17 thorpej if (*pde & L1_S_CACHE_MASK) {
911 1.17 thorpej map->_dm_flags &=
912 1.17 thorpej ~ARM32_DMAMAP_COHERENT;
913 1.17 thorpej }
914 1.17 thorpej } else {
915 1.29 scw pte = *ptep;
916 1.17 thorpej KDASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV);
917 1.17 thorpej if (__predict_false((pte & L2_TYPE_MASK)
918 1.17 thorpej == L2_TYPE_L)) {
919 1.17 thorpej curaddr = (pte & L2_L_FRAME) |
920 1.17 thorpej (vaddr & L2_L_OFFSET);
921 1.17 thorpej if (pte & L2_L_CACHE_MASK) {
922 1.17 thorpej map->_dm_flags &=
923 1.17 thorpej ~ARM32_DMAMAP_COHERENT;
924 1.17 thorpej }
925 1.17 thorpej } else {
926 1.17 thorpej curaddr = (pte & L2_S_FRAME) |
927 1.17 thorpej (vaddr & L2_S_OFFSET);
928 1.17 thorpej if (pte & L2_S_CACHE_MASK) {
929 1.17 thorpej map->_dm_flags &=
930 1.17 thorpej ~ARM32_DMAMAP_COHERENT;
931 1.17 thorpej }
932 1.17 thorpej }
933 1.17 thorpej }
934 1.34 briggs } else {
935 1.17 thorpej (void) pmap_extract(pmap, vaddr, &curaddr);
936 1.34 briggs map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
937 1.34 briggs }
938 1.1 chris
939 1.1 chris /*
940 1.1 chris * Make sure we're in an allowed DMA range.
941 1.1 chris */
942 1.15 thorpej if (t->_ranges != NULL) {
943 1.15 thorpej /* XXX cache last result? */
944 1.15 thorpej dr = _bus_dma_inrange(t->_ranges, t->_nranges,
945 1.15 thorpej curaddr);
946 1.15 thorpej if (dr == NULL)
947 1.15 thorpej return (EINVAL);
948 1.15 thorpej
949 1.15 thorpej /*
950 1.15 thorpej * In a valid DMA range. Translate the physical
951 1.15 thorpej * memory address to an address in the DMA window.
952 1.15 thorpej */
953 1.15 thorpej curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase;
954 1.15 thorpej }
955 1.1 chris
956 1.1 chris /*
957 1.1 chris * Compute the segment size, and adjust counts.
958 1.1 chris */
959 1.27 thorpej sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
960 1.1 chris if (buflen < sgsize)
961 1.1 chris sgsize = buflen;
962 1.1 chris
963 1.1 chris /*
964 1.1 chris * Make sure we don't cross any boundaries.
965 1.1 chris */
966 1.1 chris if (map->_dm_boundary > 0) {
967 1.1 chris baddr = (curaddr + map->_dm_boundary) & bmask;
968 1.1 chris if (sgsize > (baddr - curaddr))
969 1.1 chris sgsize = (baddr - curaddr);
970 1.1 chris }
971 1.1 chris
972 1.1 chris /*
973 1.1 chris * Insert chunk into a segment, coalescing with
974 1.1 chris * previous segment if possible.
975 1.1 chris */
976 1.1 chris if (first) {
977 1.1 chris map->dm_segs[seg].ds_addr = curaddr;
978 1.1 chris map->dm_segs[seg].ds_len = sgsize;
979 1.1 chris first = 0;
980 1.1 chris } else {
981 1.1 chris if (curaddr == lastaddr &&
982 1.1 chris (map->dm_segs[seg].ds_len + sgsize) <=
983 1.1 chris map->_dm_maxsegsz &&
984 1.1 chris (map->_dm_boundary == 0 ||
985 1.1 chris (map->dm_segs[seg].ds_addr & bmask) ==
986 1.1 chris (curaddr & bmask)))
987 1.1 chris map->dm_segs[seg].ds_len += sgsize;
988 1.1 chris else {
989 1.1 chris if (++seg >= map->_dm_segcnt)
990 1.1 chris break;
991 1.1 chris map->dm_segs[seg].ds_addr = curaddr;
992 1.1 chris map->dm_segs[seg].ds_len = sgsize;
993 1.1 chris }
994 1.1 chris }
995 1.1 chris
996 1.1 chris lastaddr = curaddr + sgsize;
997 1.1 chris vaddr += sgsize;
998 1.1 chris buflen -= sgsize;
999 1.1 chris }
1000 1.1 chris
1001 1.1 chris *segp = seg;
1002 1.1 chris *lastaddrp = lastaddr;
1003 1.1 chris
1004 1.1 chris /*
1005 1.1 chris * Did we fit?
1006 1.1 chris */
1007 1.1 chris if (buflen != 0)
1008 1.1 chris return (EFBIG); /* XXX better return value here? */
1009 1.1 chris return (0);
1010 1.1 chris }
1011 1.1 chris
1012 1.1 chris /*
1013 1.1 chris * Allocate physical memory from the given physical address range.
1014 1.1 chris * Called by DMA-safe memory allocation methods.
1015 1.1 chris */
1016 1.1 chris int
1017 1.7 thorpej _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1018 1.7 thorpej bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1019 1.11 thorpej int flags, paddr_t low, paddr_t high)
1020 1.1 chris {
1021 1.11 thorpej paddr_t curaddr, lastaddr;
1022 1.1 chris struct vm_page *m;
1023 1.1 chris struct pglist mlist;
1024 1.1 chris int curseg, error;
1025 1.1 chris
1026 1.1 chris #ifdef DEBUG_DMA
1027 1.1 chris printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
1028 1.1 chris t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
1029 1.1 chris #endif /* DEBUG_DMA */
1030 1.1 chris
1031 1.1 chris /* Always round the size. */
1032 1.1 chris size = round_page(size);
1033 1.1 chris
1034 1.1 chris /*
1035 1.1 chris * Allocate pages from the VM system.
1036 1.1 chris */
1037 1.1 chris error = uvm_pglistalloc(size, low, high, alignment, boundary,
1038 1.1 chris &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
1039 1.1 chris if (error)
1040 1.1 chris return (error);
1041 1.1 chris
1042 1.1 chris /*
1043 1.1 chris * Compute the location, size, and number of segments actually
1044 1.1 chris * returned by the VM code.
1045 1.1 chris */
1046 1.1 chris m = mlist.tqh_first;
1047 1.1 chris curseg = 0;
1048 1.1 chris lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
1049 1.1 chris segs[curseg].ds_len = PAGE_SIZE;
1050 1.1 chris #ifdef DEBUG_DMA
1051 1.1 chris printf("alloc: page %lx\n", lastaddr);
1052 1.1 chris #endif /* DEBUG_DMA */
1053 1.1 chris m = m->pageq.tqe_next;
1054 1.1 chris
1055 1.1 chris for (; m != NULL; m = m->pageq.tqe_next) {
1056 1.1 chris curaddr = VM_PAGE_TO_PHYS(m);
1057 1.1 chris #ifdef DIAGNOSTIC
1058 1.1 chris if (curaddr < low || curaddr >= high) {
1059 1.1 chris printf("uvm_pglistalloc returned non-sensical"
1060 1.1 chris " address 0x%lx\n", curaddr);
1061 1.1 chris panic("_bus_dmamem_alloc_range");
1062 1.1 chris }
1063 1.1 chris #endif /* DIAGNOSTIC */
1064 1.1 chris #ifdef DEBUG_DMA
1065 1.1 chris printf("alloc: page %lx\n", curaddr);
1066 1.1 chris #endif /* DEBUG_DMA */
1067 1.1 chris if (curaddr == (lastaddr + PAGE_SIZE))
1068 1.1 chris segs[curseg].ds_len += PAGE_SIZE;
1069 1.1 chris else {
1070 1.1 chris curseg++;
1071 1.1 chris segs[curseg].ds_addr = curaddr;
1072 1.1 chris segs[curseg].ds_len = PAGE_SIZE;
1073 1.1 chris }
1074 1.1 chris lastaddr = curaddr;
1075 1.1 chris }
1076 1.1 chris
1077 1.1 chris *rsegs = curseg + 1;
1078 1.1 chris
1079 1.15 thorpej return (0);
1080 1.15 thorpej }
1081 1.15 thorpej
1082 1.15 thorpej /*
1083 1.15 thorpej * Check if a memory region intersects with a DMA range, and return the
1084 1.15 thorpej * page-rounded intersection if it does.
1085 1.15 thorpej */
1086 1.15 thorpej int
1087 1.15 thorpej arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
1088 1.15 thorpej paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
1089 1.15 thorpej {
1090 1.15 thorpej struct arm32_dma_range *dr;
1091 1.15 thorpej int i;
1092 1.15 thorpej
1093 1.15 thorpej if (ranges == NULL)
1094 1.15 thorpej return (0);
1095 1.15 thorpej
1096 1.15 thorpej for (i = 0, dr = ranges; i < nranges; i++, dr++) {
1097 1.15 thorpej if (dr->dr_sysbase <= pa &&
1098 1.15 thorpej pa < (dr->dr_sysbase + dr->dr_len)) {
1099 1.15 thorpej /*
1100 1.15 thorpej * Beginning of region intersects with this range.
1101 1.15 thorpej */
1102 1.15 thorpej *pap = trunc_page(pa);
1103 1.15 thorpej *sizep = round_page(min(pa + size,
1104 1.15 thorpej dr->dr_sysbase + dr->dr_len) - pa);
1105 1.15 thorpej return (1);
1106 1.15 thorpej }
1107 1.15 thorpej if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
1108 1.15 thorpej /*
1109 1.15 thorpej * End of region intersects with this range.
1110 1.15 thorpej */
1111 1.15 thorpej *pap = trunc_page(dr->dr_sysbase);
1112 1.15 thorpej *sizep = round_page(min((pa + size) - dr->dr_sysbase,
1113 1.15 thorpej dr->dr_len));
1114 1.15 thorpej return (1);
1115 1.15 thorpej }
1116 1.15 thorpej }
1117 1.15 thorpej
1118 1.15 thorpej /* No intersection found. */
1119 1.1 chris return (0);
1120 1.1 chris }
1121