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