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