bus_dma.c revision 1.70 1 /* $NetBSD: bus_dma.c,v 1.70 2020/10/11 00:33:30 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34
35 __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.70 2020/10/11 00:33:30 thorpej Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/mbuf.h>
44
45 #include <uvm/uvm_extern.h>
46
47 #define _ALPHA_BUS_DMA_PRIVATE
48 #include <sys/bus.h>
49 #include <machine/intr.h>
50
51 #include <dev/bus_dma/bus_dmamem_common.h>
52
53 int _bus_dmamap_load_buffer_direct(bus_dma_tag_t,
54 bus_dmamap_t, void *, bus_size_t, struct vmspace *, int,
55 paddr_t *, int *, int);
56
57 extern paddr_t avail_start, avail_end; /* from pmap.c */
58
59 #define DMA_COUNT_DECL(cnt) _DMA_COUNT_DECL(dma_direct, cnt)
60 #define DMA_COUNT(cnt) _DMA_COUNT(dma_direct, cnt)
61
62 /*
63 * Common function for DMA map creation. May be called by bus-specific
64 * DMA map creation functions.
65 */
66 int
67 _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
68 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
69 {
70 struct alpha_bus_dmamap *map;
71 void *mapstore;
72 size_t mapsize;
73
74 /*
75 * Allocate and initialize the DMA map. The end of the map
76 * is a variable-sized array of segments, so we allocate enough
77 * room for them in one shot.
78 *
79 * Note we don't preserve the WAITOK or NOWAIT flags. Preservation
80 * of ALLOCNOW notifies others that we've reserved these resources,
81 * and they are not to be freed.
82 *
83 * The bus_dmamap_t includes one bus_dma_segment_t, hence
84 * the (nsegments - 1).
85 */
86 mapsize = sizeof(struct alpha_bus_dmamap) +
87 (sizeof(bus_dma_segment_t) * (nsegments - 1));
88 if ((mapstore = malloc(mapsize, M_DMAMAP,
89 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
90 return (ENOMEM);
91
92 memset(mapstore, 0, mapsize);
93 map = (struct alpha_bus_dmamap *)mapstore;
94 map->_dm_size = size;
95 map->_dm_segcnt = nsegments;
96 map->_dm_maxmaxsegsz = maxsegsz;
97 if (t->_boundary != 0 && t->_boundary < boundary)
98 map->_dm_boundary = t->_boundary;
99 else
100 map->_dm_boundary = boundary;
101 map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
102 map->dm_maxsegsz = maxsegsz;
103 map->dm_mapsize = 0; /* no valid mappings */
104 map->dm_nsegs = 0;
105 map->_dm_window = NULL;
106
107 *dmamp = map;
108 return (0);
109 }
110
111 /*
112 * Common function for DMA map destruction. May be called by bus-specific
113 * DMA map destruction functions.
114 */
115 void
116 _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
117 {
118
119 free(map, M_DMAMAP);
120 }
121
122 /*
123 * Utility function to load a linear buffer. lastaddrp holds state
124 * between invocations (for multiple-buffer loads). segp contains
125 * the starting segment on entrance, and the ending segment on exit.
126 * first indicates if this is the first invocation of this function.
127 */
128 int
129 _bus_dmamap_load_buffer_direct(bus_dma_tag_t t, bus_dmamap_t map,
130 void *buf, size_t buflen, struct vmspace *vm, int flags, paddr_t *lastaddrp,
131 int *segp, int first)
132 {
133 bus_size_t sgsize;
134 bus_addr_t curaddr, lastaddr, baddr, bmask;
135 vaddr_t vaddr = (vaddr_t)buf;
136 int seg;
137
138 lastaddr = *lastaddrp;
139 bmask = ~(map->_dm_boundary - 1);
140
141 for (seg = *segp; buflen > 0 ; ) {
142 /*
143 * Get the physical address for this segment.
144 */
145 if (!VMSPACE_IS_KERNEL_P(vm))
146 (void) pmap_extract(vm->vm_map.pmap, vaddr, &curaddr);
147 else
148 curaddr = vtophys(vaddr);
149
150 /*
151 * If we're beyond the current DMA window, indicate
152 * that and try to fall back into SGMAPs.
153 */
154 if (t->_wsize != 0 && curaddr >= t->_wsize)
155 return (EINVAL);
156
157 curaddr |= t->_wbase;
158
159 /*
160 * Compute the segment size, and adjust counts.
161 */
162 sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
163 if (buflen < sgsize)
164 sgsize = buflen;
165 if (map->dm_maxsegsz < sgsize)
166 sgsize = map->dm_maxsegsz;
167
168 /*
169 * Make sure we don't cross any boundaries.
170 */
171 if (map->_dm_boundary > 0) {
172 baddr = (curaddr + map->_dm_boundary) & bmask;
173 if (sgsize > (baddr - curaddr))
174 sgsize = (baddr - curaddr);
175 }
176
177 /*
178 * Insert chunk into a segment, coalescing with
179 * the previous segment if possible.
180 */
181 if (first) {
182 map->dm_segs[seg].ds_addr = curaddr;
183 map->dm_segs[seg].ds_len = sgsize;
184 first = 0;
185 } else {
186 if ((map->_dm_flags & DMAMAP_NO_COALESCE) == 0 &&
187 curaddr == lastaddr &&
188 (map->dm_segs[seg].ds_len + sgsize) <=
189 map->dm_maxsegsz &&
190 (map->_dm_boundary == 0 ||
191 (map->dm_segs[seg].ds_addr & bmask) ==
192 (curaddr & bmask)))
193 map->dm_segs[seg].ds_len += sgsize;
194 else {
195 if (++seg >= map->_dm_segcnt)
196 break;
197 map->dm_segs[seg].ds_addr = curaddr;
198 map->dm_segs[seg].ds_len = sgsize;
199 }
200 }
201
202 lastaddr = curaddr + sgsize;
203 vaddr += sgsize;
204 buflen -= sgsize;
205 }
206
207 *segp = seg;
208 *lastaddrp = lastaddr;
209
210 /*
211 * Did we fit?
212 */
213 if (buflen != 0) {
214 /*
215 * If there is a chained window, we will automatically
216 * fall back to it.
217 */
218 return (EFBIG); /* XXX better return value here? */
219 }
220
221 return (0);
222 }
223
224 DMA_COUNT_DECL(load);
225 DMA_COUNT_DECL(load_next_window);
226
227 /*
228 * Common function for loading a direct-mapped DMA map with a linear
229 * buffer. Called by bus-specific DMA map load functions with the
230 * OR value appropriate for indicating "direct-mapped" for that
231 * chipset.
232 */
233 int
234 _bus_dmamap_load_direct(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
235 bus_size_t buflen, struct proc *p, int flags)
236 {
237 paddr_t lastaddr;
238 int seg, error;
239 struct vmspace *vm;
240
241 /*
242 * Make sure that on error condition we return "no valid mappings".
243 */
244 map->dm_mapsize = 0;
245 map->dm_nsegs = 0;
246 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
247 KASSERT((map->_dm_flags & (BUS_DMA_READ|BUS_DMA_WRITE)) == 0);
248
249 if (buflen > map->_dm_size)
250 return (EINVAL);
251
252 if (p != NULL) {
253 vm = p->p_vmspace;
254 } else {
255 vm = vmspace_kernel();
256 }
257 seg = 0;
258 error = _bus_dmamap_load_buffer_direct(t, map, buf, buflen,
259 vm, flags, &lastaddr, &seg, 1);
260 if (error == 0) {
261 DMA_COUNT(load);
262 map->dm_mapsize = buflen;
263 map->dm_nsegs = seg + 1;
264 map->_dm_window = t;
265 } else if (t->_next_window != NULL) {
266 /*
267 * Give the next window a chance.
268 */
269 DMA_COUNT(load_next_window);
270 error = bus_dmamap_load(t->_next_window, map, buf, buflen,
271 p, flags);
272 }
273 return (error);
274 }
275
276 DMA_COUNT_DECL(load_mbuf);
277 DMA_COUNT_DECL(load_mbuf_next_window);
278
279 /*
280 * Like _bus_dmamap_load_direct(), but for mbufs.
281 */
282 int
283 _bus_dmamap_load_mbuf_direct(bus_dma_tag_t t, bus_dmamap_t map,
284 struct mbuf *m0, int flags)
285 {
286 paddr_t lastaddr;
287 int seg, error, first;
288 struct mbuf *m;
289
290 /*
291 * Make sure that on error condition we return "no valid mappings."
292 */
293 map->dm_mapsize = 0;
294 map->dm_nsegs = 0;
295 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
296 KASSERT((map->_dm_flags & (BUS_DMA_READ|BUS_DMA_WRITE)) == 0);
297
298 #ifdef DIAGNOSTIC
299 if ((m0->m_flags & M_PKTHDR) == 0)
300 panic("_bus_dmamap_load_mbuf_direct: no packet header");
301 #endif
302
303 if (m0->m_pkthdr.len > map->_dm_size)
304 return (EINVAL);
305
306 first = 1;
307 seg = 0;
308 error = 0;
309 for (m = m0; m != NULL && error == 0; m = m->m_next) {
310 if (m->m_len == 0)
311 continue;
312 /* XXX Could be better about coalescing. */
313 /* XXX Doesn't check boundaries. */
314 switch (m->m_flags & (M_EXT|M_EXT_CLUSTER)) {
315 case M_EXT|M_EXT_CLUSTER:
316 /* XXX KDASSERT */
317 KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
318 lastaddr = m->m_ext.ext_paddr +
319 (m->m_data - m->m_ext.ext_buf);
320 have_addr:
321 if (first == 0 &&
322 ++seg >= map->_dm_segcnt) {
323 error = EFBIG;
324 break;
325 }
326
327 /*
328 * If we're beyond the current DMA window, indicate
329 * that and try to fall back into SGMAPs.
330 */
331 if (t->_wsize != 0 && lastaddr >= t->_wsize) {
332 error = EINVAL;
333 break;
334 }
335 lastaddr |= t->_wbase;
336
337 map->dm_segs[seg].ds_addr = lastaddr;
338 map->dm_segs[seg].ds_len = m->m_len;
339 lastaddr += m->m_len;
340 break;
341
342 case 0:
343 lastaddr = m->m_paddr + M_BUFOFFSET(m) +
344 (m->m_data - M_BUFADDR(m));
345 goto have_addr;
346
347 default:
348 error = _bus_dmamap_load_buffer_direct(t, map,
349 m->m_data, m->m_len, vmspace_kernel(), flags,
350 &lastaddr, &seg, first);
351 }
352 first = 0;
353 }
354 if (error == 0) {
355 DMA_COUNT(load_mbuf);
356 map->dm_mapsize = m0->m_pkthdr.len;
357 map->dm_nsegs = seg + 1;
358 map->_dm_window = t;
359 } else if (t->_next_window != NULL) {
360 /*
361 * Give the next window a chance.
362 */
363 DMA_COUNT(load_mbuf_next_window);
364 error = bus_dmamap_load_mbuf(t->_next_window, map, m0, flags);
365 }
366 return (error);
367 }
368
369 DMA_COUNT_DECL(load_uio);
370 DMA_COUNT_DECL(load_uio_next_window);
371
372 /*
373 * Like _bus_dmamap_load_direct(), but for uios.
374 */
375 int
376 _bus_dmamap_load_uio_direct(bus_dma_tag_t t, bus_dmamap_t map,
377 struct uio *uio, int flags)
378 {
379 paddr_t lastaddr;
380 int seg, i, error, first;
381 bus_size_t minlen, resid;
382 struct vmspace *vm;
383 struct iovec *iov;
384 void *addr;
385
386 /*
387 * Make sure that on error condition we return "no valid mappings."
388 */
389 map->dm_mapsize = 0;
390 map->dm_nsegs = 0;
391 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
392 KASSERT((map->_dm_flags & (BUS_DMA_READ|BUS_DMA_WRITE)) == 0);
393
394 resid = uio->uio_resid;
395 iov = uio->uio_iov;
396
397 vm = uio->uio_vmspace;
398
399 first = 1;
400 seg = 0;
401 error = 0;
402 for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
403 /*
404 * Now at the first iovec to load. Load each iovec
405 * until we have exhausted the residual count.
406 */
407 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
408 addr = (void *)iov[i].iov_base;
409
410 error = _bus_dmamap_load_buffer_direct(t, map,
411 addr, minlen, vm, flags, &lastaddr, &seg, first);
412 first = 0;
413
414 resid -= minlen;
415 }
416 if (error == 0) {
417 DMA_COUNT(load_uio);
418 map->dm_mapsize = uio->uio_resid;
419 map->dm_nsegs = seg + 1;
420 map->_dm_window = t;
421 } else if (t->_next_window != NULL) {
422 /*
423 * Give the next window a chance.
424 */
425 DMA_COUNT(load_uio_next_window);
426 error = bus_dmamap_load_uio(t->_next_window, map, uio, flags);
427 }
428 return (error);
429 }
430
431 /*
432 * Like _bus_dmamap_load_direct(), but for raw memory.
433 */
434 int
435 _bus_dmamap_load_raw_direct(bus_dma_tag_t t, bus_dmamap_t map,
436 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
437 {
438
439 panic("_bus_dmamap_load_raw_direct: not implemented");
440 }
441
442 /*
443 * Common function for unloading a DMA map. May be called by
444 * chipset-specific DMA map unload functions.
445 */
446 void
447 _bus_dmamap_unload_common(bus_dma_tag_t t, bus_dmamap_t map)
448 {
449
450 /*
451 * No resources to free; just mark the mappings as
452 * invalid.
453 */
454 map->dm_maxsegsz = map->_dm_maxmaxsegsz;
455 map->dm_mapsize = 0;
456 map->dm_nsegs = 0;
457 map->_dm_window = NULL;
458 map->_dm_flags &= ~(BUS_DMA_READ|BUS_DMA_WRITE);
459 }
460
461 DMA_COUNT_DECL(unload);
462
463 void
464 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
465 {
466 KASSERT(map->_dm_window == t);
467 DMA_COUNT(unload);
468 _bus_dmamap_unload_common(t, map);
469 }
470
471 /*
472 * Common function for DMA map synchronization. May be called
473 * by chipset-specific DMA map synchronization functions.
474 */
475 void
476 _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
477 bus_size_t len, int ops)
478 {
479
480 alpha_mb();
481 }
482
483 /*
484 * Common function for DMA-safe memory allocation. May be called
485 * by bus-specific DMA memory allocation functions.
486 */
487 int
488 _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
489 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
490 int flags)
491 {
492
493 return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
494 segs, nsegs, rsegs, flags, 0, trunc_page(avail_end)));
495 }
496
497 /*
498 * Allocate physical memory from the given physical address range.
499 * Called by DMA-safe memory allocation methods.
500 */
501 int
502 _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
503 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
504 int flags, paddr_t low, paddr_t high)
505 {
506
507 return (_bus_dmamem_alloc_range_common(t, size, alignment, boundary,
508 segs, nsegs, rsegs, flags,
509 low, high));
510 }
511
512 /*
513 * Common function for freeing DMA-safe memory. May be called by
514 * bus-specific DMA memory free functions.
515 */
516 void
517 _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
518 {
519
520 _bus_dmamem_free_common(t, segs, nsegs);
521 }
522
523 /*
524 * Common function for mapping DMA-safe memory. May be called by
525 * bus-specific DMA memory map functions.
526 */
527 int
528 _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
529 size_t size, void **kvap, int flags)
530 {
531
532 /*
533 * If we're only mapping 1 segment, use K0SEG, to avoid
534 * TLB thrashing.
535 */
536 if (nsegs == 1) {
537 *kvap = (void *)ALPHA_PHYS_TO_K0SEG(segs[0].ds_addr);
538 return (0);
539 }
540
541 return (_bus_dmamem_map_common(t, segs, nsegs, size, kvap, flags, 0));
542 }
543
544 /*
545 * Common function for unmapping DMA-safe memory. May be called by
546 * bus-specific DMA memory unmapping functions.
547 */
548 void
549 _bus_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
550 {
551
552 /*
553 * Nothing to do if we mapped it with K0SEG.
554 */
555 if (kva >= (void *)ALPHA_K0SEG_BASE &&
556 kva <= (void *)ALPHA_K0SEG_END)
557 return;
558
559 _bus_dmamem_unmap_common(t, kva, size);
560 }
561
562 /*
563 * Common functin for mmap(2)'ing DMA-safe memory. May be called by
564 * bus-specific DMA mmap(2)'ing functions.
565 */
566 paddr_t
567 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
568 off_t off, int prot, int flags)
569 {
570 bus_addr_t rv;
571
572 rv = _bus_dmamem_mmap_common(t, segs, nsegs, off, prot, flags);
573 if (rv == (bus_addr_t)-1)
574 return (-1);
575
576 return (alpha_btop((char *)rv));
577 }
578