iommu.c revision 1.37 1 /* $NetBSD: iommu.c,v 1.37 2000/01/07 10:54:11 pk Exp $ */
2
3 /*
4 * Copyright (c) 1996
5 * The President and Fellows of Harvard College. All rights reserved.
6 * Copyright (c) 1995 Paul Kranenburg
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Aaron Brown and
19 * Harvard University.
20 * This product includes software developed by Paul Kranenburg.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 */
38
39 #include <sys/param.h>
40 #include <sys/extent.h>
41 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <vm/vm.h>
46 #include <vm/vm_kern.h>
47
48 #include <uvm/uvm_extern.h>
49 #include <uvm/uvm.h>
50
51 #define _SPARC_BUS_DMA_PRIVATE
52 #include <machine/bus.h>
53 #include <machine/autoconf.h>
54 #include <machine/ctlreg.h>
55 #include <sparc/sparc/asm.h>
56 #include <sparc/sparc/vaddrs.h>
57 #include <sparc/sparc/cpuvar.h>
58 #include <sparc/sparc/iommureg.h>
59 #include <sparc/sparc/iommuvar.h>
60
61 struct iommu_softc {
62 struct device sc_dev; /* base device */
63 struct iommureg *sc_reg;
64 u_int sc_pagesize;
65 u_int sc_range;
66 bus_addr_t sc_dvmabase;
67 iopte_t *sc_ptes;
68 int sc_hasiocache;
69 };
70 struct iommu_softc *iommu_sc;/*XXX*/
71 int has_iocache;
72 u_long dvma_cachealign;
73
74 /*
75 * Note: operations on the extent map are being protected with
76 * splhigh(), since we cannot predict at which interrupt priority
77 * our clients will run.
78 */
79 struct extent *iommu_dvmamap;
80
81
82 /* autoconfiguration driver */
83 int iommu_print __P((void *, const char *));
84 void iommu_attach __P((struct device *, struct device *, void *));
85 int iommu_match __P((struct device *, struct cfdata *, void *));
86
87 struct cfattach iommu_ca = {
88 sizeof(struct iommu_softc), iommu_match, iommu_attach
89 };
90
91 /* IOMMU DMA map functions */
92 int iommu_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
93 bus_size_t, struct proc *, int));
94 int iommu_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
95 struct mbuf *, int));
96 int iommu_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
97 struct uio *, int));
98 int iommu_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
99 bus_dma_segment_t *, int, bus_size_t, int));
100 void iommu_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
101 void iommu_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
102 bus_size_t, int));
103
104 int iommu_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
105 bus_size_t alignment, bus_size_t boundary,
106 bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
107 void iommu_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
108 int nsegs));
109 int iommu_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
110 int nsegs, size_t size, caddr_t *kvap, int flags));
111 int iommu_dmamem_mmap __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
112 int nsegs, int off, int prot, int flags));
113
114
115 struct sparc_bus_dma_tag iommu_dma_tag = {
116 NULL,
117 _bus_dmamap_create,
118 _bus_dmamap_destroy,
119 iommu_dmamap_load,
120 iommu_dmamap_load_mbuf,
121 iommu_dmamap_load_uio,
122 iommu_dmamap_load_raw,
123 iommu_dmamap_unload,
124 iommu_dmamap_sync,
125
126 iommu_dmamem_alloc,
127 iommu_dmamem_free,
128 iommu_dmamem_map,
129 _bus_dmamem_unmap,
130 iommu_dmamem_mmap
131 };
132 /*
133 * Print the location of some iommu-attached device (called just
134 * before attaching that device). If `iommu' is not NULL, the
135 * device was found but not configured; print the iommu as well.
136 * Return UNCONF (config_find ignores this if the device was configured).
137 */
138 int
139 iommu_print(args, iommu)
140 void *args;
141 const char *iommu;
142 {
143 struct iommu_attach_args *ia = args;
144
145 if (iommu)
146 printf("%s at %s", ia->iom_name, iommu);
147 return (UNCONF);
148 }
149
150 int
151 iommu_match(parent, cf, aux)
152 struct device *parent;
153 struct cfdata *cf;
154 void *aux;
155 {
156 struct mainbus_attach_args *ma = aux;
157
158 if (CPU_ISSUN4OR4C)
159 return (0);
160 return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
161 }
162
163 /*
164 * Attach the iommu.
165 */
166 void
167 iommu_attach(parent, self, aux)
168 struct device *parent;
169 struct device *self;
170 void *aux;
171 {
172 #if defined(SUN4M)
173 struct iommu_softc *sc = (struct iommu_softc *)self;
174 struct mainbus_attach_args *ma = aux;
175 int node;
176 struct bootpath *bp;
177 bus_space_handle_t bh;
178 u_int pbase, pa;
179 int i, mmupcrsave, s;
180 iopte_t *tpte_p;
181 extern u_int *kernel_iopte_table;
182 extern u_int kernel_iopte_table_pa;
183
184 /*XXX-GCC!*/mmupcrsave=0;
185 iommu_sc = sc;
186 /*
187 * XXX there is only one iommu, for now -- do not know how to
188 * address children on others
189 */
190 if (sc->sc_dev.dv_unit > 0) {
191 printf(" unsupported\n");
192 return;
193 }
194 node = ma->ma_node;
195
196 #if 0
197 if (ra->ra_vaddr)
198 sc->sc_reg = (struct iommureg *)ca->ca_ra.ra_vaddr;
199 #else
200 /*
201 * Map registers into our space. The PROM may have done this
202 * already, but I feel better if we have our own copy. Plus, the
203 * prom doesn't map the entire register set
204 *
205 * XXX struct iommureg is bigger than ra->ra_len; what are the
206 * other fields for?
207 */
208 if (bus_space_map2(
209 ma->ma_bustag,
210 ma->ma_iospace,
211 ma->ma_paddr,
212 sizeof(struct iommureg),
213 0,
214 0,
215 &bh) != 0) {
216 printf("iommu_attach: cannot map registers\n");
217 return;
218 }
219 sc->sc_reg = (struct iommureg *)bh;
220 #endif
221
222 sc->sc_hasiocache = node_has_property(node, "cache-coherence?");
223 if (CACHEINFO.c_enabled == 0) /* XXX - is this correct? */
224 sc->sc_hasiocache = 0;
225 has_iocache = sc->sc_hasiocache; /* Set global flag */
226
227 sc->sc_pagesize = getpropint(node, "page-size", NBPG),
228 sc->sc_range = (1 << 24) <<
229 ((sc->sc_reg->io_cr & IOMMU_CTL_RANGE) >> IOMMU_CTL_RANGESHFT);
230 #if 0
231 sc->sc_dvmabase = (0 - sc->sc_range);
232 #endif
233 pbase = (sc->sc_reg->io_bar & IOMMU_BAR_IBA) <<
234 (14 - IOMMU_BAR_IBASHFT);
235
236 /*
237 * Now we build our own copy of the IOMMU page tables. We need to
238 * do this since we're going to change the range to give us 64M of
239 * mappings, and thus we can move DVMA space down to 0xfd000000 to
240 * give us lots of space and to avoid bumping into the PROM, etc.
241 *
242 * XXX Note that this is rather messy.
243 */
244 sc->sc_ptes = (iopte_t *) kernel_iopte_table;
245
246 /*
247 * Now discache the page tables so that the IOMMU sees our
248 * changes.
249 */
250 kvm_uncache((caddr_t)sc->sc_ptes,
251 (((0 - IOMMU_DVMA_BASE)/sc->sc_pagesize) * sizeof(iopte_t)) / NBPG);
252
253 /*
254 * Ok. We've got to read in the original table using MMU bypass,
255 * and copy all of its entries to the appropriate place in our
256 * new table, even if the sizes are different.
257 * This is pretty easy since we know DVMA ends at 0xffffffff.
258 *
259 * XXX: PGOFSET, NBPG assume same page size as SRMMU
260 */
261 if (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc) {
262 /* set MMU AC bit */
263 sta(SRMMU_PCR, ASI_SRMMU,
264 ((mmupcrsave = lda(SRMMU_PCR, ASI_SRMMU)) | VIKING_PCR_AC));
265 }
266
267 for (tpte_p = &sc->sc_ptes[((0 - IOMMU_DVMA_BASE)/NBPG) - 1],
268 pa = (u_int)pbase - sizeof(iopte_t) +
269 ((u_int)sc->sc_range/NBPG)*sizeof(iopte_t);
270 tpte_p >= &sc->sc_ptes[0] && pa >= (u_int)pbase;
271 tpte_p--, pa -= sizeof(iopte_t)) {
272
273 IOMMU_FLUSHPAGE(sc,
274 (tpte_p - &sc->sc_ptes[0])*NBPG + IOMMU_DVMA_BASE);
275 *tpte_p = lda(pa, ASI_BYPASS);
276 }
277 if (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc) {
278 /* restore mmu after bug-avoidance */
279 sta(SRMMU_PCR, ASI_SRMMU, mmupcrsave);
280 }
281
282 /*
283 * Now we can install our new pagetable into the IOMMU
284 */
285 sc->sc_range = 0 - IOMMU_DVMA_BASE;
286 sc->sc_dvmabase = IOMMU_DVMA_BASE;
287
288 /* calculate log2(sc->sc_range/16MB) */
289 i = ffs(sc->sc_range/(1 << 24)) - 1;
290 if ((1 << i) != (sc->sc_range/(1 << 24)))
291 panic("bad iommu range: %d\n",i);
292
293 s = splhigh();
294 IOMMU_FLUSHALL(sc);
295
296 sc->sc_reg->io_cr = (sc->sc_reg->io_cr & ~IOMMU_CTL_RANGE) |
297 (i << IOMMU_CTL_RANGESHFT) | IOMMU_CTL_ME;
298 sc->sc_reg->io_bar = (kernel_iopte_table_pa >> 4) & IOMMU_BAR_IBA;
299
300 IOMMU_FLUSHALL(sc);
301 splx(s);
302
303 printf(": version 0x%x/0x%x, page-size %d, range %dMB\n",
304 (sc->sc_reg->io_cr & IOMMU_CTL_VER) >> 24,
305 (sc->sc_reg->io_cr & IOMMU_CTL_IMPL) >> 28,
306 sc->sc_pagesize,
307 sc->sc_range >> 20);
308
309 /* Propagate bootpath */
310 if (ma->ma_bp != NULL && strcmp(ma->ma_bp->name, "iommu") == 0)
311 bp = ma->ma_bp + 1;
312 else
313 bp = NULL;
314
315 iommu_dvmamap = extent_create("iommudvma",
316 IOMMU_DVMA_BASE, IOMMU_DVMA_END,
317 M_DEVBUF, 0, 0, EX_NOWAIT);
318 if (iommu_dvmamap == NULL)
319 panic("iommu: unable to allocate DVMA map");
320
321 /*
322 * Loop through ROM children (expect Sbus among them).
323 */
324 for (node = firstchild(node); node; node = nextsibling(node)) {
325 struct iommu_attach_args ia;
326
327 bzero(&ia, sizeof ia);
328 ia.iom_name = getpropstring(node, "name");
329
330 /* Propagate BUS & DMA tags */
331 ia.iom_bustag = ma->ma_bustag;
332 ia.iom_dmatag = &iommu_dma_tag;
333
334 ia.iom_node = node;
335 ia.iom_bp = bp;
336
337 ia.iom_reg = NULL;
338 getprop(node, "reg", sizeof(struct sbus_reg),
339 &ia.iom_nreg, (void **)&ia.iom_reg);
340
341 (void) config_found(&sc->sc_dev, (void *)&ia, iommu_print);
342 if (ia.iom_reg != NULL)
343 free(ia.iom_reg, M_DEVBUF);
344 }
345 #endif
346 }
347
348 void
349 iommu_enter(va, pa)
350 bus_addr_t va;
351 paddr_t pa;
352 {
353 struct iommu_softc *sc = iommu_sc;
354 int pte;
355
356 #ifdef DEBUG
357 if (va < sc->sc_dvmabase)
358 panic("iommu_enter: va 0x%lx not in DVMA space", (long)va);
359 #endif
360
361 pte = atop(pa) << IOPTE_PPNSHFT;
362 pte &= IOPTE_PPN;
363 pte |= IOPTE_V | IOPTE_W | (has_iocache ? IOPTE_C : 0);
364 sc->sc_ptes[atop(va - sc->sc_dvmabase)] = pte;
365 IOMMU_FLUSHPAGE(sc, va);
366 }
367
368 /*
369 * iommu_clear: clears mappings created by iommu_enter
370 */
371 void
372 iommu_remove(va, len)
373 bus_addr_t va;
374 bus_size_t len;
375 {
376 struct iommu_softc *sc = iommu_sc;
377 u_int pagesz = sc->sc_pagesize;
378 bus_addr_t base = sc->sc_dvmabase;
379
380 #ifdef DEBUG
381 if (va < base)
382 panic("iommu_enter: va 0x%lx not in DVMA space", (long)va);
383 #endif
384
385 while ((long)len > 0) {
386 #ifdef notyet
387 #ifdef DEBUG
388 if ((sc->sc_ptes[atop(va - base)] & IOPTE_V) == 0)
389 panic("iommu_clear: clearing invalid pte at va 0x%lx",
390 (long)va);
391 #endif
392 #endif
393 sc->sc_ptes[atop(va - base)] = 0;
394 IOMMU_FLUSHPAGE(sc, va);
395 len -= pagesz;
396 va += pagesz;
397 }
398 }
399
400 #if 0 /* These registers aren't there??? */
401 void
402 iommu_error()
403 {
404 struct iommu_softc *sc = X;
405 struct iommureg *iop = sc->sc_reg;
406
407 printf("iommu: afsr 0x%x, afar 0x%x\n", iop->io_afsr, iop->io_afar);
408 printf("iommu: mfsr 0x%x, mfar 0x%x\n", iop->io_mfsr, iop->io_mfar);
409 }
410 int
411 iommu_alloc(va, len)
412 u_int va, len;
413 {
414 struct iommu_softc *sc = X;
415 int off, tva, iovaddr, pte;
416 paddr_t pa;
417
418 off = (int)va & PGOFSET;
419 len = round_page(len + off);
420 va -= off;
421
422 if ((int)sc->sc_dvmacur + len > 0)
423 sc->sc_dvmacur = sc->sc_dvmabase;
424
425 iovaddr = tva = sc->sc_dvmacur;
426 sc->sc_dvmacur += len;
427 while (len) {
428 (void) pmap_extract(pmap_kernel(), va, &pa);
429
430 #define IOMMU_PPNSHIFT 8
431 #define IOMMU_V 0x00000002
432 #define IOMMU_W 0x00000004
433
434 pte = atop(pa) << IOMMU_PPNSHIFT;
435 pte |= IOMMU_V | IOMMU_W;
436 sta(sc->sc_ptes + atop(tva - sc->sc_dvmabase), ASI_BYPASS, pte);
437 sc->sc_reg->io_flushpage = tva;
438 len -= NBPG;
439 va += NBPG;
440 tva += NBPG;
441 }
442 return iovaddr + off;
443 }
444 #endif
445
446
447 /*
448 * IOMMU DMA map functions.
449 */
450 int
451 iommu_dmamap_load(t, map, buf, buflen, p, flags)
452 bus_dma_tag_t t;
453 bus_dmamap_t map;
454 void *buf;
455 bus_size_t buflen;
456 struct proc *p;
457 int flags;
458 {
459 bus_size_t sgsize;
460 bus_addr_t dva;
461 bus_addr_t boundary;
462 vaddr_t va = (vaddr_t)buf;
463 u_long align, voff;
464 u_long ex_start, ex_end;
465 pmap_t pmap;
466 int s, error;
467
468 /*
469 * Remember page offset, then truncate the buffer address to
470 * a page boundary.
471 */
472 voff = va & PGOFSET;
473 va &= ~PGOFSET;
474
475 /*
476 * Make sure that on error condition we return "no valid mappings".
477 */
478 map->dm_nsegs = 0;
479
480 if (buflen > map->_dm_size)
481 return (EINVAL);
482
483 sgsize = (buflen + voff + PGOFSET) & ~PGOFSET;
484 align = dvma_cachealign ? dvma_cachealign : NBPG;
485 boundary = map->_dm_boundary;
486
487 s = splhigh();
488
489 /* Check `24 address bits' in the map's attributes */
490 if ((map->_dm_flags & BUS_DMA_24BIT) != 0) {
491 ex_start = D24_DVMA_BASE;
492 ex_end = D24_DVMA_END;
493 } else {
494 ex_start = iommu_dvmamap->ex_start;
495 ex_end = iommu_dvmamap->ex_end;
496 }
497 error = extent_alloc_subregion1(iommu_dvmamap,
498 ex_start, ex_end,
499 sgsize, align, va & (align-1), boundary,
500 (flags & BUS_DMA_NOWAIT) == 0
501 ? EX_WAITOK : EX_NOWAIT,
502 (u_long *)&dva);
503 splx(s);
504
505 if (error != 0)
506 return (error);
507
508 cpuinfo.cache_flush(buf, buflen);
509
510 /*
511 * We always use just one segment.
512 */
513 map->dm_mapsize = buflen;
514 map->dm_nsegs = 1;
515 map->dm_segs[0].ds_addr = dva + voff;
516 map->dm_segs[0].ds_len = buflen;
517
518 if (p != NULL)
519 pmap = p->p_vmspace->vm_map.pmap;
520 else
521 pmap = pmap_kernel();
522
523 for (; sgsize != 0; ) {
524 paddr_t pa;
525 /*
526 * Get the physical address for this page.
527 */
528 (void) pmap_extract(pmap, va, &pa);
529
530 iommu_enter(dva, pa);
531
532 dva += NBPG;
533 va += NBPG;
534 sgsize -= NBPG;
535 }
536
537 return (0);
538 }
539
540 /*
541 * Like _bus_dmamap_load(), but for mbufs.
542 */
543 int
544 iommu_dmamap_load_mbuf(t, map, m, flags)
545 bus_dma_tag_t t;
546 bus_dmamap_t map;
547 struct mbuf *m;
548 int flags;
549 {
550
551 panic("_bus_dmamap_load: not implemented");
552 }
553
554 /*
555 * Like _bus_dmamap_load(), but for uios.
556 */
557 int
558 iommu_dmamap_load_uio(t, map, uio, flags)
559 bus_dma_tag_t t;
560 bus_dmamap_t map;
561 struct uio *uio;
562 int flags;
563 {
564
565 panic("_bus_dmamap_load_uio: not implemented");
566 }
567
568 /*
569 * Like _bus_dmamap_load(), but for raw memory allocated with
570 * bus_dmamem_alloc().
571 */
572 int
573 iommu_dmamap_load_raw(t, map, segs, nsegs, size, flags)
574 bus_dma_tag_t t;
575 bus_dmamap_t map;
576 bus_dma_segment_t *segs;
577 int nsegs;
578 bus_size_t size;
579 int flags;
580 {
581
582 panic("_bus_dmamap_load_raw: not implemented");
583 }
584
585 /*
586 * Common function for unloading a DMA map. May be called by
587 * bus-specific DMA map unload functions.
588 */
589 void
590 iommu_dmamap_unload(t, map)
591 bus_dma_tag_t t;
592 bus_dmamap_t map;
593 {
594 bus_addr_t addr;
595 bus_size_t len;
596 int s, error;
597
598 if (map->dm_nsegs != 1)
599 panic("_bus_dmamap_unload: nsegs = %d", map->dm_nsegs);
600
601 addr = map->dm_segs[0].ds_addr;
602 len = map->dm_segs[0].ds_len;
603 len = ((addr & PGOFSET) + len + PGOFSET) & ~PGOFSET;
604 addr &= ~PGOFSET;
605
606 iommu_remove(addr, len);
607 s = splhigh();
608 error = extent_free(iommu_dvmamap, addr, len, EX_NOWAIT);
609 splx(s);
610 if (error != 0)
611 printf("warning: %ld of DVMA space lost\n", (long)len);
612
613 /* Mark the mappings as invalid. */
614 map->dm_mapsize = 0;
615 map->dm_nsegs = 0;
616 }
617
618 /*
619 * Common function for DMA map synchronization. May be called
620 * by bus-specific DMA map synchronization functions.
621 */
622 void
623 iommu_dmamap_sync(t, map, offset, len, ops)
624 bus_dma_tag_t t;
625 bus_dmamap_t map;
626 bus_addr_t offset;
627 bus_size_t len;
628 int ops;
629 {
630
631 /*
632 * XXX Should flush CPU write buffers.
633 */
634 }
635
636 /*
637 * Common function for DMA-safe memory allocation. May be called
638 * by bus-specific DMA memory allocation functions.
639 */
640 int
641 iommu_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
642 bus_dma_tag_t t;
643 bus_size_t size, alignment, boundary;
644 bus_dma_segment_t *segs;
645 int nsegs;
646 int *rsegs;
647 int flags;
648 {
649 paddr_t pa;
650 bus_addr_t dva;
651 vm_page_t m;
652 int s, error;
653 u_long ex_start, ex_end;
654 struct pglist *mlist;
655
656 size = round_page(size);
657 error = _bus_dmamem_alloc_common(t, size, alignment, boundary,
658 segs, nsegs, rsegs, flags);
659 if (error != 0)
660 return (error);
661
662 s = splhigh();
663
664 if ((flags & BUS_DMA_24BIT) != 0) {
665 ex_start = D24_DVMA_BASE;
666 ex_end = D24_DVMA_END;
667 } else {
668 ex_start = iommu_dvmamap->ex_start;
669 ex_end = iommu_dvmamap->ex_end;
670 }
671
672 error = extent_alloc_subregion(iommu_dvmamap,
673 ex_start, ex_end,
674 size, alignment, boundary,
675 (flags & BUS_DMA_NOWAIT) == 0
676 ? EX_WAITOK : EX_NOWAIT,
677 (u_long *)&dva);
678 splx(s);
679 if (error != 0)
680 return (error);
681
682 /*
683 * Compute the location, size, and number of segments actually
684 * returned by the VM code.
685 */
686 segs[0].ds_addr = dva;
687 segs[0].ds_len = size;
688 *rsegs = 1;
689
690 mlist = segs[0]._ds_mlist;
691 /* Map memory into DVMA space */
692 for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
693 pa = VM_PAGE_TO_PHYS(m);
694
695 iommu_enter(dva, pa);
696 dva += PAGE_SIZE;
697 }
698
699 return (0);
700 }
701
702 /*
703 * Common function for freeing DMA-safe memory. May be called by
704 * bus-specific DMA memory free functions.
705 */
706 void
707 iommu_dmamem_free(t, segs, nsegs)
708 bus_dma_tag_t t;
709 bus_dma_segment_t *segs;
710 int nsegs;
711 {
712 bus_addr_t addr;
713 bus_size_t len;
714 int s, error;
715
716 if (nsegs != 1)
717 panic("bus_dmamem_free: nsegs = %d", nsegs);
718
719 addr = segs[0].ds_addr;
720 len = segs[0].ds_len;
721
722 iommu_remove(addr, len);
723 s = splhigh();
724 error = extent_free(iommu_dvmamap, addr, len, EX_NOWAIT);
725 splx(s);
726 if (error != 0)
727 printf("warning: %ld of DVMA space lost\n", (long)len);
728 /*
729 * Return the list of pages back to the VM system.
730 */
731 _bus_dmamem_free_common(t, segs, nsegs);
732 }
733
734 /*
735 * Common function for mapping DMA-safe memory. May be called by
736 * bus-specific DMA memory map functions.
737 */
738 int
739 iommu_dmamem_map(t, segs, nsegs, size, kvap, flags)
740 bus_dma_tag_t t;
741 bus_dma_segment_t *segs;
742 int nsegs;
743 size_t size;
744 caddr_t *kvap;
745 int flags;
746 {
747 vm_page_t m;
748 vaddr_t va, sva;
749 bus_addr_t addr;
750 struct pglist *mlist;
751 int cbit;
752 size_t oversize;
753 u_long align;
754
755 if (nsegs != 1)
756 panic("iommu_dmamem_map: nsegs = %d", nsegs);
757
758 cbit = has_iocache ? 0 : PMAP_NC;
759 align = dvma_cachealign ? dvma_cachealign : PAGE_SIZE;
760
761 size = round_page(size);
762
763 /*
764 * Find a region of kernel virtual addresses that can accomodate
765 * our aligment requirements.
766 */
767 oversize = size + align - PAGE_SIZE;
768 sva = uvm_km_valloc(kernel_map, oversize);
769 if (sva == 0)
770 return (ENOMEM);
771
772 /* Compute start of aligned region */
773 va = sva;
774 va += ((segs[0].ds_addr & (align - 1)) + align - va) & (align - 1);
775
776 /* Return excess virtual addresses */
777 if (va != sva)
778 (void)uvm_unmap(kernel_map, sva, va);
779 if (va + size != sva + oversize)
780 (void)uvm_unmap(kernel_map, va + size, sva + oversize);
781
782
783 *kvap = (caddr_t)va;
784 mlist = segs[0]._ds_mlist;
785
786 for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
787
788 if (size == 0)
789 panic("iommu_dmamem_map: size botch");
790
791 addr = VM_PAGE_TO_PHYS(m);
792 pmap_enter(pmap_kernel(), va, addr | cbit,
793 VM_PROT_READ | VM_PROT_WRITE,
794 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
795 #if 0
796 if (flags & BUS_DMA_COHERENT)
797 /* XXX */;
798 #endif
799 va += PAGE_SIZE;
800 size -= PAGE_SIZE;
801 }
802
803 return (0);
804 }
805
806 /*
807 * Common functin for mmap(2)'ing DMA-safe memory. May be called by
808 * bus-specific DMA mmap(2)'ing functions.
809 */
810 int
811 iommu_dmamem_mmap(t, segs, nsegs, off, prot, flags)
812 bus_dma_tag_t t;
813 bus_dma_segment_t *segs;
814 int nsegs, off, prot, flags;
815 {
816
817 panic("_bus_dmamem_mmap: not implemented");
818 }
819