iommu.c revision 1.50 1 /* $NetBSD: iommu.c,v 1.50 2000/07/22 21:23:05 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
46 #include <uvm/uvm.h>
47
48 #define _SPARC_BUS_DMA_PRIVATE
49 #include <machine/bus.h>
50 #include <machine/autoconf.h>
51 #include <machine/ctlreg.h>
52 #include <sparc/sparc/asm.h>
53 #include <sparc/sparc/vaddrs.h>
54 #include <sparc/sparc/cpuvar.h>
55 #include <sparc/sparc/iommureg.h>
56 #include <sparc/sparc/iommuvar.h>
57
58 struct iommu_softc {
59 struct device sc_dev; /* base device */
60 struct iommureg *sc_reg;
61 u_int sc_pagesize;
62 u_int sc_range;
63 bus_addr_t sc_dvmabase;
64 iopte_t *sc_ptes;
65 int sc_hasiocache;
66 };
67 struct iommu_softc *iommu_sc;/*XXX*/
68 int has_iocache;
69 u_long dvma_cachealign;
70
71 /*
72 * Note: operations on the extent map are being protected with
73 * splhigh(), since we cannot predict at which interrupt priority
74 * our clients will run.
75 */
76 struct extent *iommu_dvmamap;
77
78
79 /* autoconfiguration driver */
80 int iommu_print __P((void *, const char *));
81 void iommu_attach __P((struct device *, struct device *, void *));
82 int iommu_match __P((struct device *, struct cfdata *, void *));
83
84 static void iommu_copy_prom_entries __P((struct iommu_softc *));
85
86 struct cfattach iommu_ca = {
87 sizeof(struct iommu_softc), iommu_match, iommu_attach
88 };
89
90 /* IOMMU DMA map functions */
91 int iommu_dmamap_create __P((bus_dma_tag_t, bus_size_t, int, bus_size_t,
92 bus_size_t, int, bus_dmamap_t *));
93 int iommu_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
94 bus_size_t, struct proc *, int));
95 int iommu_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
96 struct mbuf *, int));
97 int iommu_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
98 struct uio *, int));
99 int iommu_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
100 bus_dma_segment_t *, int, bus_size_t, int));
101 void iommu_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
102 void iommu_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
103 bus_size_t, int));
104
105 int iommu_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
106 int nsegs, size_t size, caddr_t *kvap, int flags));
107 paddr_t iommu_dmamem_mmap __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
108 int nsegs, off_t off, int prot, int flags));
109 int iommu_dvma_alloc(bus_dmamap_t, vaddr_t, bus_size_t, int,
110 bus_addr_t *, bus_size_t *);
111
112
113 struct sparc_bus_dma_tag iommu_dma_tag = {
114 NULL,
115 iommu_dmamap_create,
116 _bus_dmamap_destroy,
117 iommu_dmamap_load,
118 iommu_dmamap_load_mbuf,
119 iommu_dmamap_load_uio,
120 iommu_dmamap_load_raw,
121 iommu_dmamap_unload,
122 iommu_dmamap_sync,
123
124 _bus_dmamem_alloc,
125 _bus_dmamem_free,
126 iommu_dmamem_map,
127 _bus_dmamem_unmap,
128 iommu_dmamem_mmap
129 };
130 /*
131 * Print the location of some iommu-attached device (called just
132 * before attaching that device). If `iommu' is not NULL, the
133 * device was found but not configured; print the iommu as well.
134 * Return UNCONF (config_find ignores this if the device was configured).
135 */
136 int
137 iommu_print(args, iommu)
138 void *args;
139 const char *iommu;
140 {
141 struct iommu_attach_args *ia = args;
142
143 if (iommu)
144 printf("%s at %s", ia->iom_name, iommu);
145 return (UNCONF);
146 }
147
148 int
149 iommu_match(parent, cf, aux)
150 struct device *parent;
151 struct cfdata *cf;
152 void *aux;
153 {
154 struct mainbus_attach_args *ma = aux;
155
156 if (CPU_ISSUN4OR4C)
157 return (0);
158 return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
159 }
160
161 /*
162 * Attach the iommu.
163 */
164 void
165 iommu_attach(parent, self, aux)
166 struct device *parent;
167 struct device *self;
168 void *aux;
169 {
170 #if defined(SUN4M)
171 struct iommu_softc *sc = (struct iommu_softc *)self;
172 struct mainbus_attach_args *ma = aux;
173 bus_space_handle_t bh;
174 int node;
175 int i, s;
176 u_int iopte_table_pa;
177 struct pglist mlist;
178 u_int size;
179 vm_page_t m;
180 vaddr_t va;
181
182 iommu_sc = sc;
183 /*
184 * XXX there is only one iommu, for now -- do not know how to
185 * address children on others
186 */
187 if (sc->sc_dev.dv_unit > 0) {
188 printf(" unsupported\n");
189 return;
190 }
191 node = ma->ma_node;
192
193 /*
194 * Map registers into our space. The PROM may have done this
195 * already, but I feel better if we have our own copy. Plus, the
196 * prom doesn't map the entire register set.
197 *
198 * XXX struct iommureg is bigger than ra->ra_len; what are the
199 * other fields for?
200 */
201 if (bus_space_map2(
202 ma->ma_bustag,
203 ma->ma_iospace,
204 ma->ma_paddr,
205 sizeof(struct iommureg),
206 0,
207 0,
208 &bh) != 0) {
209 printf("iommu_attach: cannot map registers\n");
210 return;
211 }
212 sc->sc_reg = (struct iommureg *)bh;
213
214 sc->sc_hasiocache = node_has_property(node, "cache-coherence?");
215 if (CACHEINFO.c_enabled == 0) /* XXX - is this correct? */
216 sc->sc_hasiocache = 0;
217 has_iocache = sc->sc_hasiocache; /* Set global flag */
218
219 sc->sc_pagesize = getpropint(node, "page-size", NBPG),
220
221 /*
222 * Allocate memory for I/O pagetables.
223 * This takes 64K of contiguous physical memory to map 64M of
224 * DVMA space (starting at IOMMU_DVMA_BASE).
225 * The table must be aligned on a (-IOMMU_DVMA_BASE/pagesize)
226 * boundary (i.e. 64K for 64M of DVMA space).
227 */
228
229 size = ((0 - IOMMU_DVMA_BASE) / sc->sc_pagesize) * sizeof(iopte_t);
230 TAILQ_INIT(&mlist);
231 if (uvm_pglistalloc(size, vm_first_phys, vm_first_phys+vm_num_phys,
232 size, 0, &mlist, 1, 0) != 0)
233 panic("iommu_attach: no memory");
234
235 va = uvm_km_valloc(kernel_map, size);
236 if (va == 0)
237 panic("iommu_attach: no memory");
238
239 sc->sc_ptes = (iopte_t *)va;
240
241 m = TAILQ_FIRST(&mlist);
242 iopte_table_pa = VM_PAGE_TO_PHYS(m);
243
244 /* Map the pages */
245 for (; m != NULL; m = TAILQ_NEXT(m,pageq)) {
246 paddr_t pa = VM_PAGE_TO_PHYS(m);
247 pmap_enter(pmap_kernel(), va, pa | PMAP_NC,
248 VM_PROT_READ|VM_PROT_WRITE,
249 VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
250 va += NBPG;
251 }
252
253 /*
254 * Copy entries from current IOMMU table.
255 * XXX - Why do we need to do this?
256 */
257 iommu_copy_prom_entries(sc);
258
259 /*
260 * Now we can install our new pagetable into the IOMMU
261 */
262 sc->sc_range = 0 - IOMMU_DVMA_BASE;
263 sc->sc_dvmabase = IOMMU_DVMA_BASE;
264
265 /* calculate log2(sc->sc_range/16MB) */
266 i = ffs(sc->sc_range/(1 << 24)) - 1;
267 if ((1 << i) != (sc->sc_range/(1 << 24)))
268 panic("iommu: bad range: %d\n", i);
269
270 s = splhigh();
271 IOMMU_FLUSHALL(sc);
272
273 /* Load range and physical address of PTEs */
274 sc->sc_reg->io_cr = (sc->sc_reg->io_cr & ~IOMMU_CTL_RANGE) |
275 (i << IOMMU_CTL_RANGESHFT) | IOMMU_CTL_ME;
276 sc->sc_reg->io_bar = (iopte_table_pa >> 4) & IOMMU_BAR_IBA;
277
278 IOMMU_FLUSHALL(sc);
279 splx(s);
280
281 printf(": version 0x%x/0x%x, page-size %d, range %dMB\n",
282 (sc->sc_reg->io_cr & IOMMU_CTL_VER) >> 24,
283 (sc->sc_reg->io_cr & IOMMU_CTL_IMPL) >> 28,
284 sc->sc_pagesize,
285 sc->sc_range >> 20);
286
287 iommu_dvmamap = extent_create("iommudvma",
288 IOMMU_DVMA_BASE, IOMMU_DVMA_END,
289 M_DEVBUF, 0, 0, EX_NOWAIT);
290 if (iommu_dvmamap == NULL)
291 panic("iommu: unable to allocate DVMA map");
292
293 /*
294 * Loop through ROM children (expect Sbus among them).
295 */
296 for (node = firstchild(node); node; node = nextsibling(node)) {
297 struct iommu_attach_args ia;
298
299 bzero(&ia, sizeof ia);
300 ia.iom_name = getpropstring(node, "name");
301
302 /* Propagate BUS & DMA tags */
303 ia.iom_bustag = ma->ma_bustag;
304 ia.iom_dmatag = &iommu_dma_tag;
305
306 ia.iom_node = node;
307
308 ia.iom_reg = NULL;
309 getprop(node, "reg", sizeof(struct sbus_reg),
310 &ia.iom_nreg, (void **)&ia.iom_reg);
311
312 (void) config_found(&sc->sc_dev, (void *)&ia, iommu_print);
313 if (ia.iom_reg != NULL)
314 free(ia.iom_reg, M_DEVBUF);
315 }
316 #endif
317 }
318
319 static void
320 iommu_copy_prom_entries(sc)
321 struct iommu_softc *sc;
322 {
323 u_int pbase, pa;
324 u_int range;
325 iopte_t *tpte_p;
326 u_int pagesz = sc->sc_pagesize;
327 int use_ac = (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc);
328 u_int mmupcr_save;
329
330 /*
331 * We read in the original table using MMU bypass and copy all
332 * of its entries to the appropriate place in our new table,
333 * even if the sizes are different.
334 * This is pretty easy since we know DVMA ends at 0xffffffff.
335 */
336
337 range = (1 << 24) <<
338 ((sc->sc_reg->io_cr & IOMMU_CTL_RANGE) >> IOMMU_CTL_RANGESHFT);
339
340 pbase = (sc->sc_reg->io_bar & IOMMU_BAR_IBA) <<
341 (14 - IOMMU_BAR_IBASHFT);
342
343 if (use_ac) {
344 /*
345 * Set MMU AC bit so we'll still read from the cache
346 * in by-pass mode.
347 */
348 mmupcr_save = lda(SRMMU_PCR, ASI_SRMMU);
349 sta(SRMMU_PCR, ASI_SRMMU, mmupcr_save | VIKING_PCR_AC);
350 } else
351 mmupcr_save = 0; /* XXX - avoid GCC `unintialized' warning */
352
353 /* Flush entire IOMMU TLB before messing with the in-memory tables */
354 IOMMU_FLUSHALL(sc);
355
356 /*
357 * tpte_p = top of our PTE table
358 * pa = top of current PTE table
359 * Then work downwards and copy entries until we hit the bottom
360 * of either table.
361 */
362 for (tpte_p = &sc->sc_ptes[((0 - IOMMU_DVMA_BASE)/pagesz) - 1],
363 pa = (u_int)pbase + (range/pagesz - 1)*sizeof(iopte_t);
364 tpte_p >= &sc->sc_ptes[0] && pa >= (u_int)pbase;
365 tpte_p--, pa -= sizeof(iopte_t)) {
366
367 *tpte_p = lda(pa, ASI_BYPASS);
368 }
369
370 if (use_ac) {
371 /* restore mmu after bug-avoidance */
372 sta(SRMMU_PCR, ASI_SRMMU, mmupcr_save);
373 }
374 }
375
376 void
377 iommu_enter(dva, pa)
378 bus_addr_t dva;
379 paddr_t pa;
380 {
381 struct iommu_softc *sc = iommu_sc;
382 int pte;
383
384 /* This routine relies on the fact that sc->sc_pagesize == PAGE_SIZE */
385
386 #ifdef DIAGNOSTIC
387 if (dva < sc->sc_dvmabase)
388 panic("iommu_enter: dva 0x%lx not in DVMA space", (long)dva);
389 #endif
390
391 pte = atop(pa) << IOPTE_PPNSHFT;
392 pte &= IOPTE_PPN;
393 pte |= IOPTE_V | IOPTE_W | (has_iocache ? IOPTE_C : 0);
394 sc->sc_ptes[atop(dva - sc->sc_dvmabase)] = pte;
395 IOMMU_FLUSHPAGE(sc, dva);
396 }
397
398 /*
399 * iommu_clear: clears mappings created by iommu_enter
400 */
401 void
402 iommu_remove(dva, len)
403 bus_addr_t dva;
404 bus_size_t len;
405 {
406 struct iommu_softc *sc = iommu_sc;
407 u_int pagesz = sc->sc_pagesize;
408 bus_addr_t base = sc->sc_dvmabase;
409
410 #ifdef DEBUG
411 if (dva < base)
412 panic("iommu_remove: va 0x%lx not in DVMA space", (long)dva);
413 #endif
414
415 while ((long)len > 0) {
416 #ifdef notyet
417 #ifdef DEBUG
418 if ((sc->sc_ptes[atop(dva - base)] & IOPTE_V) == 0)
419 panic("iommu_remove: clearing invalid pte at dva 0x%lx",
420 (long)dva);
421 #endif
422 #endif
423 sc->sc_ptes[atop(dva - base)] = 0;
424 IOMMU_FLUSHPAGE(sc, dva);
425 len -= pagesz;
426 dva += pagesz;
427 }
428 }
429
430 #if 0 /* These registers aren't there??? */
431 void
432 iommu_error()
433 {
434 struct iommu_softc *sc = X;
435 struct iommureg *iop = sc->sc_reg;
436
437 printf("iommu: afsr 0x%x, afar 0x%x\n", iop->io_afsr, iop->io_afar);
438 printf("iommu: mfsr 0x%x, mfar 0x%x\n", iop->io_mfsr, iop->io_mfar);
439 }
440 int
441 iommu_alloc(va, len)
442 u_int va, len;
443 {
444 struct iommu_softc *sc = X;
445 int off, tva, iovaddr, pte;
446 paddr_t pa;
447
448 off = (int)va & PGOFSET;
449 len = round_page(len + off);
450 va -= off;
451
452 if ((int)sc->sc_dvmacur + len > 0)
453 sc->sc_dvmacur = sc->sc_dvmabase;
454
455 iovaddr = tva = sc->sc_dvmacur;
456 sc->sc_dvmacur += len;
457 while (len) {
458 (void) pmap_extract(pmap_kernel(), va, &pa);
459
460 #define IOMMU_PPNSHIFT 8
461 #define IOMMU_V 0x00000002
462 #define IOMMU_W 0x00000004
463
464 pte = atop(pa) << IOMMU_PPNSHIFT;
465 pte |= IOMMU_V | IOMMU_W;
466 sta(sc->sc_ptes + atop(tva - sc->sc_dvmabase), ASI_BYPASS, pte);
467 sc->sc_reg->io_flushpage = tva;
468 len -= NBPG;
469 va += NBPG;
470 tva += NBPG;
471 }
472 return iovaddr + off;
473 }
474 #endif
475
476
477 /*
478 * IOMMU DMA map functions.
479 */
480 int
481 iommu_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
482 bus_dma_tag_t t;
483 bus_size_t size;
484 int nsegments;
485 bus_size_t maxsegsz;
486 bus_size_t boundary;
487 int flags;
488 bus_dmamap_t *dmamp;
489 {
490 bus_dmamap_t map;
491 int error;
492
493 if ((error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
494 boundary, flags, &map)) != 0)
495 return (error);
496
497 if ((flags & BUS_DMA_24BIT) != 0) {
498 /* Limit this map to the range usable by `24-bit' devices */
499 map->_dm_ex_start = D24_DVMA_BASE;
500 map->_dm_ex_end = D24_DVMA_END;
501 } else {
502 /* Enable allocations from the entire map */
503 map->_dm_ex_start = iommu_dvmamap->ex_start;
504 map->_dm_ex_end = iommu_dvmamap->ex_end;
505 }
506
507 *dmamp = map;
508 return (0);
509 }
510
511 /*
512 * Internal routine to allocate space in the IOMMU map.
513 */
514 int
515 iommu_dvma_alloc(map, va, len, flags, dvap, sgsizep)
516 bus_dmamap_t map;
517 vaddr_t va;
518 bus_size_t len;
519 int flags;
520 bus_addr_t *dvap;
521 bus_size_t *sgsizep;
522 {
523 bus_size_t sgsize;
524 u_long align, voff;
525 int s, error;
526 int pagesz = PAGE_SIZE;
527
528 /*
529 * Remember page offset, then truncate the buffer address to
530 * a page boundary.
531 */
532 voff = va & (pagesz - 1);
533 va &= -pagesz;
534
535 if (len > map->_dm_size)
536 return (EINVAL);
537
538 sgsize = (len + voff + pagesz - 1) & -pagesz;
539 align = dvma_cachealign ? dvma_cachealign : map->_dm_align;
540
541 s = splhigh();
542 error = extent_alloc_subregion1(iommu_dvmamap,
543 map->_dm_ex_start, map->_dm_ex_end,
544 sgsize, align, va & (align-1),
545 map->_dm_boundary,
546 (flags & BUS_DMA_NOWAIT) == 0
547 ? EX_WAITOK : EX_NOWAIT,
548 (u_long *)dvap);
549 splx(s);
550
551 *sgsizep = sgsize;
552 return (error);
553 }
554
555 /*
556 * Prepare buffer for DMA transfer.
557 */
558 int
559 iommu_dmamap_load(t, map, buf, buflen, p, flags)
560 bus_dma_tag_t t;
561 bus_dmamap_t map;
562 void *buf;
563 bus_size_t buflen;
564 struct proc *p;
565 int flags;
566 {
567 bus_size_t sgsize;
568 bus_addr_t dva;
569 vaddr_t va = (vaddr_t)buf;
570 int pagesz = PAGE_SIZE;
571 pmap_t pmap;
572 int error;
573
574 /*
575 * Make sure that on error condition we return "no valid mappings".
576 */
577 map->dm_nsegs = 0;
578
579 /* Allocate IOMMU resources */
580 if ((error = iommu_dvma_alloc(map, va, buflen, flags,
581 &dva, &sgsize)) != 0)
582 return (error);
583
584 cpuinfo.cache_flush(buf, buflen); /* XXX - move to bus_dma_sync? */
585
586 /*
587 * We always use just one segment.
588 */
589 map->dm_mapsize = buflen;
590 map->dm_nsegs = 1;
591 map->dm_segs[0].ds_addr = dva + (va & (pagesz - 1));
592 map->dm_segs[0].ds_len = buflen;
593 map->dm_segs[0]._ds_sgsize = sgsize;
594
595 if (p != NULL)
596 pmap = p->p_vmspace->vm_map.pmap;
597 else
598 pmap = pmap_kernel();
599
600 for (; sgsize != 0; ) {
601 paddr_t pa;
602 /*
603 * Get the physical address for this page.
604 */
605 (void) pmap_extract(pmap, va, &pa);
606
607 iommu_enter(dva, pa);
608
609 dva += pagesz;
610 va += pagesz;
611 sgsize -= pagesz;
612 }
613
614 return (0);
615 }
616
617 /*
618 * Like _bus_dmamap_load(), but for mbufs.
619 */
620 int
621 iommu_dmamap_load_mbuf(t, map, m, flags)
622 bus_dma_tag_t t;
623 bus_dmamap_t map;
624 struct mbuf *m;
625 int flags;
626 {
627
628 panic("_bus_dmamap_load_mbuf: not implemented");
629 }
630
631 /*
632 * Like _bus_dmamap_load(), but for uios.
633 */
634 int
635 iommu_dmamap_load_uio(t, map, uio, flags)
636 bus_dma_tag_t t;
637 bus_dmamap_t map;
638 struct uio *uio;
639 int flags;
640 {
641
642 panic("_bus_dmamap_load_uio: not implemented");
643 }
644
645 /*
646 * Like _bus_dmamap_load(), but for raw memory allocated with
647 * bus_dmamem_alloc().
648 */
649 int
650 iommu_dmamap_load_raw(t, map, segs, nsegs, size, flags)
651 bus_dma_tag_t t;
652 bus_dmamap_t map;
653 bus_dma_segment_t *segs;
654 int nsegs;
655 bus_size_t size;
656 int flags;
657 {
658 vm_page_t m;
659 paddr_t pa;
660 bus_addr_t dva;
661 bus_size_t sgsize;
662 struct pglist *mlist;
663 int pagesz = PAGE_SIZE;
664 int error;
665
666 map->dm_nsegs = 0;
667
668 /* Allocate IOMMU resources */
669 if ((error = iommu_dvma_alloc(map, segs[0]._ds_va, size,
670 flags, &dva, &sgsize)) != 0)
671 return (error);
672
673 /*
674 * Note DVMA address in case bus_dmamem_map() is called later.
675 * It can then insure cache coherency by choosing a KVA that
676 * is aligned to `ds_addr'.
677 */
678 segs[0].ds_addr = dva;
679 segs[0].ds_len = size;
680
681 map->dm_segs[0].ds_addr = dva;
682 map->dm_segs[0].ds_len = size;
683 map->dm_segs[0]._ds_sgsize = sgsize;
684
685 /* Map physical pages into IOMMU */
686 mlist = segs[0]._ds_mlist;
687 for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
688 if (sgsize == 0)
689 panic("iommu_dmamap_load_raw: size botch");
690 pa = VM_PAGE_TO_PHYS(m);
691 iommu_enter(dva, pa);
692 dva += pagesz;
693 sgsize -= pagesz;
694 }
695
696 map->dm_nsegs = 1;
697 map->dm_mapsize = size;
698
699 return (0);
700 }
701
702 /*
703 * Unload an IOMMU DMA map.
704 */
705 void
706 iommu_dmamap_unload(t, map)
707 bus_dma_tag_t t;
708 bus_dmamap_t map;
709 {
710 bus_dma_segment_t *segs = map->dm_segs;
711 int nsegs = map->dm_nsegs;
712 bus_addr_t dva;
713 bus_size_t len;
714 int i, s, error;
715
716 for (i = 0; i < nsegs; i++) {
717 dva = segs[i].ds_addr & -PAGE_SIZE;
718 len = segs[i]._ds_sgsize;
719
720 iommu_remove(dva, len);
721 s = splhigh();
722 error = extent_free(iommu_dvmamap, dva, len, EX_NOWAIT);
723 splx(s);
724 if (error != 0)
725 printf("warning: %ld of DVMA space lost\n", (long)len);
726 }
727
728 /* Mark the mappings as invalid. */
729 map->dm_mapsize = 0;
730 map->dm_nsegs = 0;
731 }
732
733 /*
734 * DMA map synchronization.
735 */
736 void
737 iommu_dmamap_sync(t, map, offset, len, ops)
738 bus_dma_tag_t t;
739 bus_dmamap_t map;
740 bus_addr_t offset;
741 bus_size_t len;
742 int ops;
743 {
744
745 /*
746 * XXX Should flush CPU write buffers.
747 */
748 }
749
750 /*
751 * Map DMA-safe memory.
752 */
753 int
754 iommu_dmamem_map(t, segs, nsegs, size, kvap, flags)
755 bus_dma_tag_t t;
756 bus_dma_segment_t *segs;
757 int nsegs;
758 size_t size;
759 caddr_t *kvap;
760 int flags;
761 {
762 vm_page_t m;
763 vaddr_t va;
764 bus_addr_t addr;
765 struct pglist *mlist;
766 int cbit;
767 u_long align;
768 int pagesz = PAGE_SIZE;
769
770 if (nsegs != 1)
771 panic("iommu_dmamem_map: nsegs = %d", nsegs);
772
773 cbit = has_iocache ? 0 : PMAP_NC;
774 align = dvma_cachealign ? dvma_cachealign : pagesz;
775
776 size = round_page(size);
777
778 /*
779 * In case the segment has already been loaded by
780 * iommu_dmamap_load_raw(), find a region of kernel virtual
781 * addresses that can accomodate our aligment requirements.
782 */
783 va = _bus_dma_valloc_skewed(size, 0, align,
784 segs[0].ds_addr & (align - 1));
785 if (va == 0)
786 return (ENOMEM);
787
788 segs[0]._ds_va = va;
789 *kvap = (caddr_t)va;
790
791 /*
792 * Map the pages allocated in _bus_dmamem_alloc() to the
793 * kernel virtual address space.
794 */
795 mlist = segs[0]._ds_mlist;
796 for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
797
798 if (size == 0)
799 panic("iommu_dmamem_map: size botch");
800
801 addr = VM_PAGE_TO_PHYS(m);
802 pmap_enter(pmap_kernel(), va, addr | cbit,
803 VM_PROT_READ | VM_PROT_WRITE,
804 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
805 #if 0
806 if (flags & BUS_DMA_COHERENT)
807 /* XXX */;
808 #endif
809 va += pagesz;
810 size -= pagesz;
811 }
812
813 return (0);
814 }
815
816 /*
817 * mmap(2)'ing DMA-safe memory.
818 */
819 paddr_t
820 iommu_dmamem_mmap(t, segs, nsegs, off, prot, flags)
821 bus_dma_tag_t t;
822 bus_dma_segment_t *segs;
823 int nsegs;
824 off_t off;
825 int prot, flags;
826 {
827
828 panic("_bus_dmamem_mmap: not implemented");
829 }
830