bus_dma.c revision 1.7 1 1.7 thorpej /* $NetBSD: bus_dma.c,v 1.7 2002/01/25 19:37:49 thorpej 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.1 chris
40 1.1 chris #include <sys/param.h>
41 1.1 chris #include <sys/systm.h>
42 1.1 chris #include <sys/kernel.h>
43 1.1 chris #include <sys/map.h>
44 1.1 chris #include <sys/proc.h>
45 1.1 chris #include <sys/buf.h>
46 1.1 chris #include <sys/reboot.h>
47 1.1 chris #include <sys/conf.h>
48 1.1 chris #include <sys/file.h>
49 1.1 chris #include <sys/malloc.h>
50 1.1 chris #include <sys/mbuf.h>
51 1.1 chris #include <sys/vnode.h>
52 1.1 chris #include <sys/device.h>
53 1.1 chris
54 1.1 chris #include <uvm/uvm_extern.h>
55 1.1 chris
56 1.1 chris #define _ARM32_BUS_DMA_PRIVATE
57 1.1 chris #include <machine/bus.h>
58 1.1 chris
59 1.1 chris #include <machine/cpu.h>
60 1.4 thorpej
61 1.4 thorpej #include <arm/cpufunc.h>
62 1.1 chris
63 1.7 thorpej int _bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
64 1.7 thorpej bus_size_t, struct proc *, int, vm_offset_t *, int *, int);
65 1.7 thorpej int _bus_dma_inrange(bus_dma_segment_t *, int, bus_addr_t);
66 1.1 chris
67 1.1 chris /*
68 1.1 chris * Common function for DMA map creation. May be called by bus-specific
69 1.1 chris * DMA map creation functions.
70 1.1 chris */
71 1.1 chris int
72 1.7 thorpej _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
73 1.7 thorpej bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
74 1.1 chris {
75 1.1 chris struct arm32_bus_dmamap *map;
76 1.1 chris void *mapstore;
77 1.1 chris size_t mapsize;
78 1.1 chris
79 1.1 chris #ifdef DEBUG_DMA
80 1.1 chris printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
81 1.1 chris t, size, nsegments, maxsegsz, boundary, flags);
82 1.1 chris #endif /* DEBUG_DMA */
83 1.1 chris
84 1.1 chris /*
85 1.1 chris * Allocate and initialize the DMA map. The end of the map
86 1.1 chris * is a variable-sized array of segments, so we allocate enough
87 1.1 chris * room for them in one shot.
88 1.1 chris *
89 1.1 chris * Note we don't preserve the WAITOK or NOWAIT flags. Preservation
90 1.1 chris * of ALLOCNOW notifies others that we've reserved these resources,
91 1.1 chris * and they are not to be freed.
92 1.1 chris *
93 1.1 chris * The bus_dmamap_t includes one bus_dma_segment_t, hence
94 1.1 chris * the (nsegments - 1).
95 1.1 chris */
96 1.1 chris mapsize = sizeof(struct arm32_bus_dmamap) +
97 1.1 chris (sizeof(bus_dma_segment_t) * (nsegments - 1));
98 1.1 chris if ((mapstore = malloc(mapsize, M_DMAMAP,
99 1.1 chris (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
100 1.1 chris return (ENOMEM);
101 1.1 chris
102 1.1 chris memset(mapstore, 0, mapsize);
103 1.1 chris map = (struct arm32_bus_dmamap *)mapstore;
104 1.1 chris map->_dm_size = size;
105 1.1 chris map->_dm_segcnt = nsegments;
106 1.1 chris map->_dm_maxsegsz = maxsegsz;
107 1.1 chris map->_dm_boundary = boundary;
108 1.1 chris map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
109 1.1 chris map->dm_mapsize = 0; /* no valid mappings */
110 1.1 chris map->dm_nsegs = 0;
111 1.1 chris
112 1.1 chris *dmamp = map;
113 1.1 chris #ifdef DEBUG_DMA
114 1.1 chris printf("dmamap_create:map=%p\n", map);
115 1.1 chris #endif /* DEBUG_DMA */
116 1.1 chris return (0);
117 1.1 chris }
118 1.1 chris
119 1.1 chris /*
120 1.1 chris * Common function for DMA map destruction. May be called by bus-specific
121 1.1 chris * DMA map destruction functions.
122 1.1 chris */
123 1.1 chris void
124 1.7 thorpej _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
125 1.1 chris {
126 1.1 chris
127 1.1 chris #ifdef DEBUG_DMA
128 1.1 chris printf("dmamap_destroy: t=%p map=%p\n", t, map);
129 1.1 chris #endif /* DEBUG_DMA */
130 1.1 chris #ifdef DIAGNOSTIC
131 1.1 chris if (map->dm_nsegs > 0)
132 1.1 chris printf("bus_dmamap_destroy() called for map with valid mappings\n");
133 1.1 chris #endif /* DIAGNOSTIC */
134 1.1 chris free(map, M_DEVBUF);
135 1.1 chris }
136 1.1 chris
137 1.1 chris /*
138 1.1 chris * Common function for loading a DMA map with a linear buffer. May
139 1.1 chris * be called by bus-specific DMA map load functions.
140 1.1 chris */
141 1.1 chris int
142 1.7 thorpej _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
143 1.7 thorpej bus_size_t buflen, struct proc *p, int flags)
144 1.1 chris {
145 1.1 chris vm_offset_t lastaddr;
146 1.1 chris int seg, error;
147 1.1 chris
148 1.1 chris #ifdef DEBUG_DMA
149 1.1 chris printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
150 1.1 chris t, map, buf, buflen, p, flags);
151 1.1 chris #endif /* DEBUG_DMA */
152 1.1 chris
153 1.1 chris /*
154 1.1 chris * Make sure that on error condition we return "no valid mappings".
155 1.1 chris */
156 1.1 chris map->dm_mapsize = 0;
157 1.1 chris map->dm_nsegs = 0;
158 1.1 chris
159 1.1 chris if (buflen > map->_dm_size)
160 1.1 chris return (EINVAL);
161 1.1 chris
162 1.1 chris seg = 0;
163 1.1 chris error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
164 1.1 chris &lastaddr, &seg, 1);
165 1.1 chris if (error == 0) {
166 1.1 chris map->dm_mapsize = buflen;
167 1.1 chris map->dm_nsegs = seg + 1;
168 1.1 chris }
169 1.1 chris #ifdef DEBUG_DMA
170 1.1 chris printf("dmamap_load: error=%d\n", error);
171 1.1 chris #endif /* DEBUG_DMA */
172 1.1 chris return (error);
173 1.1 chris }
174 1.1 chris
175 1.1 chris /*
176 1.1 chris * Like _bus_dmamap_load(), but for mbufs.
177 1.1 chris */
178 1.1 chris int
179 1.7 thorpej _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
180 1.7 thorpej int flags)
181 1.1 chris {
182 1.1 chris vm_offset_t lastaddr;
183 1.1 chris int seg, error, first;
184 1.1 chris struct mbuf *m;
185 1.1 chris
186 1.1 chris #ifdef DEBUG_DMA
187 1.1 chris printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
188 1.1 chris t, map, m0, flags);
189 1.1 chris #endif /* DEBUG_DMA */
190 1.1 chris
191 1.1 chris /*
192 1.1 chris * Make sure that on error condition we return "no valid mappings."
193 1.1 chris */
194 1.1 chris map->dm_mapsize = 0;
195 1.1 chris map->dm_nsegs = 0;
196 1.1 chris
197 1.1 chris #ifdef DIAGNOSTIC
198 1.1 chris if ((m0->m_flags & M_PKTHDR) == 0)
199 1.1 chris panic("_bus_dmamap_load_mbuf: no packet header");
200 1.1 chris #endif /* DIAGNOSTIC */
201 1.1 chris
202 1.1 chris if (m0->m_pkthdr.len > map->_dm_size)
203 1.1 chris return (EINVAL);
204 1.1 chris
205 1.1 chris first = 1;
206 1.1 chris seg = 0;
207 1.1 chris error = 0;
208 1.1 chris for (m = m0; m != NULL && error == 0; m = m->m_next) {
209 1.1 chris error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
210 1.1 chris NULL, flags, &lastaddr, &seg, first);
211 1.1 chris first = 0;
212 1.1 chris }
213 1.1 chris if (error == 0) {
214 1.1 chris map->dm_mapsize = m0->m_pkthdr.len;
215 1.1 chris map->dm_nsegs = seg + 1;
216 1.1 chris }
217 1.1 chris #ifdef DEBUG_DMA
218 1.1 chris printf("dmamap_load_mbuf: error=%d\n", error);
219 1.1 chris #endif /* DEBUG_DMA */
220 1.1 chris return (error);
221 1.1 chris }
222 1.1 chris
223 1.1 chris /*
224 1.1 chris * Like _bus_dmamap_load(), but for uios.
225 1.1 chris */
226 1.1 chris int
227 1.7 thorpej _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
228 1.7 thorpej int flags)
229 1.1 chris {
230 1.1 chris vm_offset_t lastaddr;
231 1.1 chris int seg, i, error, first;
232 1.1 chris bus_size_t minlen, resid;
233 1.1 chris struct proc *p = NULL;
234 1.1 chris struct iovec *iov;
235 1.1 chris caddr_t addr;
236 1.1 chris
237 1.1 chris /*
238 1.1 chris * Make sure that on error condition we return "no valid mappings."
239 1.1 chris */
240 1.1 chris map->dm_mapsize = 0;
241 1.1 chris map->dm_nsegs = 0;
242 1.1 chris
243 1.1 chris resid = uio->uio_resid;
244 1.1 chris iov = uio->uio_iov;
245 1.1 chris
246 1.1 chris if (uio->uio_segflg == UIO_USERSPACE) {
247 1.1 chris p = uio->uio_procp;
248 1.1 chris #ifdef DIAGNOSTIC
249 1.1 chris if (p == NULL)
250 1.1 chris panic("_bus_dmamap_load_uio: USERSPACE but no proc");
251 1.1 chris #endif
252 1.1 chris }
253 1.1 chris
254 1.1 chris first = 1;
255 1.1 chris seg = 0;
256 1.1 chris error = 0;
257 1.1 chris for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
258 1.1 chris /*
259 1.1 chris * Now at the first iovec to load. Load each iovec
260 1.1 chris * until we have exhausted the residual count.
261 1.1 chris */
262 1.1 chris minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
263 1.1 chris addr = (caddr_t)iov[i].iov_base;
264 1.1 chris
265 1.1 chris error = _bus_dmamap_load_buffer(t, map, addr, minlen,
266 1.1 chris p, flags, &lastaddr, &seg, first);
267 1.1 chris first = 0;
268 1.1 chris
269 1.1 chris resid -= minlen;
270 1.1 chris }
271 1.1 chris if (error == 0) {
272 1.1 chris map->dm_mapsize = uio->uio_resid;
273 1.1 chris map->dm_nsegs = seg + 1;
274 1.1 chris }
275 1.1 chris return (error);
276 1.1 chris }
277 1.1 chris
278 1.1 chris /*
279 1.1 chris * Like _bus_dmamap_load(), but for raw memory allocated with
280 1.1 chris * bus_dmamem_alloc().
281 1.1 chris */
282 1.1 chris int
283 1.7 thorpej _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
284 1.7 thorpej bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
285 1.1 chris {
286 1.1 chris
287 1.1 chris panic("_bus_dmamap_load_raw: not implemented");
288 1.1 chris }
289 1.1 chris
290 1.1 chris /*
291 1.1 chris * Common function for unloading a DMA map. May be called by
292 1.1 chris * bus-specific DMA map unload functions.
293 1.1 chris */
294 1.1 chris void
295 1.7 thorpej _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
296 1.1 chris {
297 1.1 chris
298 1.1 chris #ifdef DEBUG_DMA
299 1.1 chris printf("dmamap_unload: t=%p map=%p\n", t, map);
300 1.1 chris #endif /* DEBUG_DMA */
301 1.1 chris
302 1.1 chris /*
303 1.1 chris * No resources to free; just mark the mappings as
304 1.1 chris * invalid.
305 1.1 chris */
306 1.1 chris map->dm_mapsize = 0;
307 1.1 chris map->dm_nsegs = 0;
308 1.1 chris }
309 1.1 chris
310 1.1 chris /*
311 1.1 chris * Common function for DMA map synchronization. May be called
312 1.1 chris * by bus-specific DMA map synchronization functions.
313 1.1 chris */
314 1.1 chris void
315 1.7 thorpej _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
316 1.7 thorpej bus_size_t len, int ops)
317 1.1 chris {
318 1.1 chris int loop;
319 1.1 chris bus_addr_t vaddr;
320 1.1 chris bus_size_t length;
321 1.1 chris bus_dma_segment_t *seg;
322 1.1 chris
323 1.1 chris #ifdef DEBUG_DMA
324 1.1 chris printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
325 1.1 chris t, map, offset, len, ops);
326 1.1 chris #endif /* DEBUG_DMA */
327 1.1 chris
328 1.1 chris if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) {
329 1.1 chris /* Quick exit if length is zero */
330 1.1 chris if (len == 0)
331 1.1 chris return;
332 1.1 chris
333 1.1 chris /* Find the segment pointed to by offset */
334 1.1 chris loop = map->dm_nsegs;
335 1.1 chris seg = &map->dm_segs[0];
336 1.1 chris while (offset >= seg->ds_len) {
337 1.1 chris offset -= seg->ds_len;
338 1.1 chris ++seg;
339 1.1 chris /* Got any more segments ? */
340 1.1 chris --loop;
341 1.1 chris if (loop == 0)
342 1.1 chris return;
343 1.1 chris }
344 1.1 chris
345 1.1 chris /* Set the starting address and maximum length */
346 1.1 chris vaddr = seg->_ds_vaddr + offset;
347 1.1 chris length = seg->ds_len - offset;
348 1.1 chris do {
349 1.1 chris /* Limit the length if not the whole segment */
350 1.1 chris if (len < length)
351 1.1 chris length = len;
352 1.1 chris #ifdef DEBUG_DMA
353 1.1 chris printf("syncing: %lx,%lx\n", vaddr, length);
354 1.1 chris #endif /* DEBUG_DMA */
355 1.1 chris /* Actually sync the cache */
356 1.6 thorpej cpu_dcache_wbinv_range(vaddr, length);
357 1.1 chris
358 1.1 chris /* Adjust the length */
359 1.1 chris len -= length;
360 1.1 chris
361 1.1 chris /* sync complete ? */
362 1.1 chris if (len > 0) {
363 1.1 chris /* Got any more segments ? */
364 1.1 chris --loop;
365 1.1 chris if (loop == 0)
366 1.1 chris return;
367 1.1 chris ++seg;
368 1.1 chris vaddr = seg->_ds_vaddr;
369 1.1 chris length = seg->ds_len;
370 1.1 chris }
371 1.1 chris } while (len > 0);
372 1.1 chris
373 1.1 chris cpu_drain_writebuf();
374 1.1 chris }
375 1.1 chris }
376 1.1 chris
377 1.1 chris /*
378 1.1 chris * Common function for DMA-safe memory allocation. May be called
379 1.1 chris * by bus-specific DMA memory allocation functions.
380 1.1 chris */
381 1.1 chris
382 1.1 chris extern vm_offset_t physical_start;
383 1.1 chris extern vm_offset_t physical_freestart;
384 1.1 chris extern vm_offset_t physical_freeend;
385 1.1 chris extern vm_offset_t physical_end;
386 1.1 chris
387 1.1 chris int
388 1.7 thorpej _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
389 1.7 thorpej bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
390 1.7 thorpej int flags)
391 1.1 chris {
392 1.1 chris int error;
393 1.1 chris #ifdef DEBUG_DMA
394 1.1 chris printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x\n",
395 1.1 chris t, size, alignment, boundary, segs, nsegs, rsegs, flags);
396 1.1 chris #endif /* DEBUG_DMA */
397 1.1 chris error = (_bus_dmamem_alloc_range(t, size, alignment, boundary,
398 1.1 chris segs, nsegs, rsegs, flags, trunc_page(physical_start), trunc_page(physical_end)));
399 1.1 chris #ifdef DEBUG_DMA
400 1.1 chris printf("dmamem_alloc: =%d\n", error);
401 1.1 chris #endif /* DEBUG_DMA */
402 1.1 chris return(error);
403 1.1 chris }
404 1.1 chris
405 1.1 chris /*
406 1.1 chris * Common function for freeing DMA-safe memory. May be called by
407 1.1 chris * bus-specific DMA memory free functions.
408 1.1 chris */
409 1.1 chris void
410 1.7 thorpej _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
411 1.1 chris {
412 1.1 chris struct vm_page *m;
413 1.1 chris bus_addr_t addr;
414 1.1 chris struct pglist mlist;
415 1.1 chris int curseg;
416 1.1 chris
417 1.1 chris #ifdef DEBUG_DMA
418 1.1 chris printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
419 1.1 chris #endif /* DEBUG_DMA */
420 1.1 chris
421 1.1 chris /*
422 1.1 chris * Build a list of pages to free back to the VM system.
423 1.1 chris */
424 1.1 chris TAILQ_INIT(&mlist);
425 1.1 chris for (curseg = 0; curseg < nsegs; curseg++) {
426 1.1 chris for (addr = segs[curseg].ds_addr;
427 1.1 chris addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
428 1.1 chris addr += PAGE_SIZE) {
429 1.1 chris m = PHYS_TO_VM_PAGE(addr);
430 1.1 chris TAILQ_INSERT_TAIL(&mlist, m, pageq);
431 1.1 chris }
432 1.1 chris }
433 1.1 chris uvm_pglistfree(&mlist);
434 1.1 chris }
435 1.1 chris
436 1.1 chris /*
437 1.1 chris * Common function for mapping DMA-safe memory. May be called by
438 1.1 chris * bus-specific DMA memory map functions.
439 1.1 chris */
440 1.1 chris int
441 1.7 thorpej _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
442 1.7 thorpej size_t size, caddr_t *kvap, int flags)
443 1.1 chris {
444 1.1 chris vm_offset_t va;
445 1.1 chris bus_addr_t addr;
446 1.1 chris int curseg;
447 1.1 chris pt_entry_t *ptep/*, pte*/;
448 1.1 chris
449 1.1 chris #ifdef DEBUG_DMA
450 1.3 rearnsha printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
451 1.3 rearnsha segs, nsegs, (unsigned long)size, flags);
452 1.1 chris #endif /* DEBUG_DMA */
453 1.1 chris
454 1.1 chris size = round_page(size);
455 1.1 chris va = uvm_km_valloc(kernel_map, size);
456 1.1 chris
457 1.1 chris if (va == 0)
458 1.1 chris return (ENOMEM);
459 1.1 chris
460 1.1 chris *kvap = (caddr_t)va;
461 1.1 chris
462 1.1 chris for (curseg = 0; curseg < nsegs; curseg++) {
463 1.1 chris for (addr = segs[curseg].ds_addr;
464 1.1 chris addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
465 1.1 chris addr += NBPG, va += NBPG, size -= NBPG) {
466 1.1 chris #ifdef DEBUG_DMA
467 1.1 chris printf("wiring p%lx to v%lx", addr, va);
468 1.1 chris #endif /* DEBUG_DMA */
469 1.1 chris if (size == 0)
470 1.1 chris panic("_bus_dmamem_map: size botch");
471 1.1 chris pmap_enter(pmap_kernel(), va, addr,
472 1.1 chris VM_PROT_READ | VM_PROT_WRITE,
473 1.1 chris VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
474 1.1 chris /*
475 1.1 chris * If the memory must remain coherent with the
476 1.1 chris * cache then we must make the memory uncacheable
477 1.1 chris * in order to maintain virtual cache coherency.
478 1.1 chris * We must also guarentee the cache does not already
479 1.1 chris * contain the virtal addresses we are making
480 1.1 chris * uncacheable.
481 1.1 chris */
482 1.1 chris if (flags & BUS_DMA_COHERENT) {
483 1.6 thorpej cpu_dcache_wbinv_range(va, NBPG);
484 1.1 chris cpu_drain_writebuf();
485 1.1 chris ptep = vtopte(va);
486 1.1 chris *ptep = ((*ptep) & (~PT_C | PT_B));
487 1.1 chris tlb_flush();
488 1.1 chris }
489 1.1 chris #ifdef DEBUG_DMA
490 1.1 chris ptep = vtopte(va);
491 1.1 chris printf(" pte=v%p *pte=%x\n", ptep, *ptep);
492 1.1 chris #endif /* DEBUG_DMA */
493 1.1 chris }
494 1.1 chris }
495 1.2 chris pmap_update(pmap_kernel());
496 1.1 chris #ifdef DEBUG_DMA
497 1.1 chris printf("dmamem_map: =%p\n", *kvap);
498 1.1 chris #endif /* DEBUG_DMA */
499 1.1 chris return (0);
500 1.1 chris }
501 1.1 chris
502 1.1 chris /*
503 1.1 chris * Common function for unmapping DMA-safe memory. May be called by
504 1.1 chris * bus-specific DMA memory unmapping functions.
505 1.1 chris */
506 1.1 chris void
507 1.7 thorpej _bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
508 1.1 chris {
509 1.1 chris
510 1.1 chris #ifdef DEBUG_DMA
511 1.3 rearnsha printf("dmamem_unmap: t=%p kva=%p size=%lx\n", t, kva,
512 1.3 rearnsha (unsigned long)size);
513 1.1 chris #endif /* DEBUG_DMA */
514 1.1 chris #ifdef DIAGNOSTIC
515 1.1 chris if ((u_long)kva & PGOFSET)
516 1.1 chris panic("_bus_dmamem_unmap");
517 1.1 chris #endif /* DIAGNOSTIC */
518 1.1 chris
519 1.1 chris size = round_page(size);
520 1.1 chris uvm_km_free(kernel_map, (vm_offset_t)kva, size);
521 1.1 chris }
522 1.1 chris
523 1.1 chris /*
524 1.1 chris * Common functin for mmap(2)'ing DMA-safe memory. May be called by
525 1.1 chris * bus-specific DMA mmap(2)'ing functions.
526 1.1 chris */
527 1.1 chris paddr_t
528 1.7 thorpej _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
529 1.7 thorpej off_t off, int prot, int flags)
530 1.1 chris {
531 1.1 chris int i;
532 1.1 chris
533 1.1 chris for (i = 0; i < nsegs; i++) {
534 1.1 chris #ifdef DIAGNOSTIC
535 1.1 chris if (off & PGOFSET)
536 1.1 chris panic("_bus_dmamem_mmap: offset unaligned");
537 1.1 chris if (segs[i].ds_addr & PGOFSET)
538 1.1 chris panic("_bus_dmamem_mmap: segment unaligned");
539 1.1 chris if (segs[i].ds_len & PGOFSET)
540 1.1 chris panic("_bus_dmamem_mmap: segment size not multiple"
541 1.1 chris " of page size");
542 1.1 chris #endif /* DIAGNOSTIC */
543 1.1 chris if (off >= segs[i].ds_len) {
544 1.1 chris off -= segs[i].ds_len;
545 1.1 chris continue;
546 1.1 chris }
547 1.1 chris
548 1.1 chris return (arm_byte_to_page((u_long)segs[i].ds_addr + off));
549 1.1 chris }
550 1.1 chris
551 1.1 chris /* Page not found. */
552 1.1 chris return (-1);
553 1.1 chris }
554 1.1 chris
555 1.1 chris /**********************************************************************
556 1.1 chris * DMA utility functions
557 1.1 chris **********************************************************************/
558 1.1 chris
559 1.1 chris /*
560 1.1 chris * Utility function to load a linear buffer. lastaddrp holds state
561 1.1 chris * between invocations (for multiple-buffer loads). segp contains
562 1.1 chris * the starting segment on entrace, and the ending segment on exit.
563 1.1 chris * first indicates if this is the first invocation of this function.
564 1.1 chris */
565 1.1 chris int
566 1.7 thorpej _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
567 1.7 thorpej bus_size_t buflen, struct proc *p, int flags, vm_offset_t *lastaddrp,
568 1.7 thorpej int *segp, int first)
569 1.1 chris {
570 1.1 chris bus_size_t sgsize;
571 1.1 chris bus_addr_t curaddr, lastaddr, baddr, bmask;
572 1.1 chris vm_offset_t vaddr = (vm_offset_t)buf;
573 1.1 chris int seg;
574 1.1 chris pmap_t pmap;
575 1.1 chris
576 1.1 chris #ifdef DEBUG_DMA
577 1.1 chris printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d, 1st=%d)\n",
578 1.1 chris buf, buflen, flags, first);
579 1.1 chris #endif /* DEBUG_DMA */
580 1.1 chris
581 1.1 chris if (p != NULL)
582 1.1 chris pmap = p->p_vmspace->vm_map.pmap;
583 1.1 chris else
584 1.1 chris pmap = pmap_kernel();
585 1.1 chris
586 1.1 chris lastaddr = *lastaddrp;
587 1.1 chris bmask = ~(map->_dm_boundary - 1);
588 1.1 chris
589 1.1 chris for (seg = *segp; buflen > 0; ) {
590 1.1 chris /*
591 1.1 chris * Get the physical address for this segment.
592 1.1 chris */
593 1.1 chris (void) pmap_extract(pmap, (vaddr_t)vaddr, &curaddr);
594 1.1 chris
595 1.1 chris /*
596 1.1 chris * Make sure we're in an allowed DMA range.
597 1.1 chris */
598 1.1 chris if (t->_ranges != NULL &&
599 1.1 chris _bus_dma_inrange(t->_ranges, t->_nranges, curaddr) == 0)
600 1.1 chris return (EINVAL);
601 1.1 chris
602 1.1 chris /*
603 1.1 chris * Compute the segment size, and adjust counts.
604 1.1 chris */
605 1.1 chris sgsize = NBPG - ((u_long)vaddr & PGOFSET);
606 1.1 chris if (buflen < sgsize)
607 1.1 chris sgsize = buflen;
608 1.1 chris
609 1.1 chris /*
610 1.1 chris * Make sure we don't cross any boundaries.
611 1.1 chris */
612 1.1 chris if (map->_dm_boundary > 0) {
613 1.1 chris baddr = (curaddr + map->_dm_boundary) & bmask;
614 1.1 chris if (sgsize > (baddr - curaddr))
615 1.1 chris sgsize = (baddr - curaddr);
616 1.1 chris }
617 1.1 chris
618 1.1 chris /*
619 1.1 chris * Insert chunk into a segment, coalescing with
620 1.1 chris * previous segment if possible.
621 1.1 chris */
622 1.1 chris if (first) {
623 1.1 chris map->dm_segs[seg].ds_addr = curaddr;
624 1.1 chris map->dm_segs[seg].ds_len = sgsize;
625 1.1 chris map->dm_segs[seg]._ds_vaddr = vaddr;
626 1.1 chris first = 0;
627 1.1 chris } else {
628 1.1 chris if (curaddr == lastaddr &&
629 1.1 chris (map->dm_segs[seg].ds_len + sgsize) <=
630 1.1 chris map->_dm_maxsegsz &&
631 1.1 chris (map->_dm_boundary == 0 ||
632 1.1 chris (map->dm_segs[seg].ds_addr & bmask) ==
633 1.1 chris (curaddr & bmask)))
634 1.1 chris map->dm_segs[seg].ds_len += sgsize;
635 1.1 chris else {
636 1.1 chris if (++seg >= map->_dm_segcnt)
637 1.1 chris break;
638 1.1 chris map->dm_segs[seg].ds_addr = curaddr;
639 1.1 chris map->dm_segs[seg].ds_len = sgsize;
640 1.1 chris map->dm_segs[seg]._ds_vaddr = vaddr;
641 1.1 chris }
642 1.1 chris }
643 1.1 chris
644 1.1 chris lastaddr = curaddr + sgsize;
645 1.1 chris vaddr += sgsize;
646 1.1 chris buflen -= sgsize;
647 1.1 chris }
648 1.1 chris
649 1.1 chris *segp = seg;
650 1.1 chris *lastaddrp = lastaddr;
651 1.1 chris
652 1.1 chris /*
653 1.1 chris * Did we fit?
654 1.1 chris */
655 1.1 chris if (buflen != 0)
656 1.1 chris return (EFBIG); /* XXX better return value here? */
657 1.1 chris return (0);
658 1.1 chris }
659 1.1 chris
660 1.1 chris /*
661 1.1 chris * Check to see if the specified page is in an allowed DMA range.
662 1.1 chris */
663 1.1 chris int
664 1.7 thorpej _bus_dma_inrange(bus_dma_segment_t *ranges, int nranges, bus_addr_t curaddr)
665 1.1 chris {
666 1.1 chris bus_dma_segment_t *ds;
667 1.1 chris int i;
668 1.1 chris
669 1.1 chris for (i = 0, ds = ranges; i < nranges; i++, ds++) {
670 1.1 chris if (curaddr >= ds->ds_addr &&
671 1.1 chris round_page(curaddr) <= (ds->ds_addr + ds->ds_len))
672 1.1 chris return (1);
673 1.1 chris }
674 1.1 chris
675 1.1 chris return (0);
676 1.1 chris }
677 1.1 chris
678 1.1 chris /*
679 1.1 chris * Allocate physical memory from the given physical address range.
680 1.1 chris * Called by DMA-safe memory allocation methods.
681 1.1 chris */
682 1.1 chris int
683 1.7 thorpej _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
684 1.7 thorpej bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
685 1.7 thorpej int flags, vm_offset_t low, vm_offset_t high)
686 1.1 chris {
687 1.1 chris vm_offset_t curaddr, lastaddr;
688 1.1 chris struct vm_page *m;
689 1.1 chris struct pglist mlist;
690 1.1 chris int curseg, error;
691 1.1 chris
692 1.1 chris #ifdef DEBUG_DMA
693 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",
694 1.1 chris t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
695 1.1 chris #endif /* DEBUG_DMA */
696 1.1 chris
697 1.1 chris /* Always round the size. */
698 1.1 chris size = round_page(size);
699 1.1 chris
700 1.1 chris /*
701 1.1 chris * Allocate pages from the VM system.
702 1.1 chris */
703 1.1 chris TAILQ_INIT(&mlist);
704 1.1 chris error = uvm_pglistalloc(size, low, high, alignment, boundary,
705 1.1 chris &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
706 1.1 chris if (error)
707 1.1 chris return (error);
708 1.1 chris
709 1.1 chris /*
710 1.1 chris * Compute the location, size, and number of segments actually
711 1.1 chris * returned by the VM code.
712 1.1 chris */
713 1.1 chris m = mlist.tqh_first;
714 1.1 chris curseg = 0;
715 1.1 chris lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
716 1.1 chris segs[curseg].ds_len = PAGE_SIZE;
717 1.1 chris #ifdef DEBUG_DMA
718 1.1 chris printf("alloc: page %lx\n", lastaddr);
719 1.1 chris #endif /* DEBUG_DMA */
720 1.1 chris m = m->pageq.tqe_next;
721 1.1 chris
722 1.1 chris for (; m != NULL; m = m->pageq.tqe_next) {
723 1.1 chris curaddr = VM_PAGE_TO_PHYS(m);
724 1.1 chris #ifdef DIAGNOSTIC
725 1.1 chris if (curaddr < low || curaddr >= high) {
726 1.1 chris printf("uvm_pglistalloc returned non-sensical"
727 1.1 chris " address 0x%lx\n", curaddr);
728 1.1 chris panic("_bus_dmamem_alloc_range");
729 1.1 chris }
730 1.1 chris #endif /* DIAGNOSTIC */
731 1.1 chris #ifdef DEBUG_DMA
732 1.1 chris printf("alloc: page %lx\n", curaddr);
733 1.1 chris #endif /* DEBUG_DMA */
734 1.1 chris if (curaddr == (lastaddr + PAGE_SIZE))
735 1.1 chris segs[curseg].ds_len += PAGE_SIZE;
736 1.1 chris else {
737 1.1 chris curseg++;
738 1.1 chris segs[curseg].ds_addr = curaddr;
739 1.1 chris segs[curseg].ds_len = PAGE_SIZE;
740 1.1 chris }
741 1.1 chris lastaddr = curaddr;
742 1.1 chris }
743 1.1 chris
744 1.1 chris *rsegs = curseg + 1;
745 1.1 chris
746 1.1 chris return (0);
747 1.1 chris }
748