sbus.c revision 1.9 1 /* $NetBSD: sbus.c,v 1.9 1999/01/10 23:32:57 eeh Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1992, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This software was developed by the Computer Systems Engineering group
44 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45 * contributed to Berkeley.
46 *
47 * All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Lawrence Berkeley Laboratory.
51 *
52 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
54 * are met:
55 * 1. Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
60 * 3. All advertising materials mentioning features or use of this software
61 * must display the following acknowledgement:
62 * This product includes software developed by the University of
63 * California, Berkeley and its contributors.
64 * 4. Neither the name of the University nor the names of its contributors
65 * may be used to endorse or promote products derived from this software
66 * without specific prior written permission.
67 *
68 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 * SUCH DAMAGE.
79 *
80 * @(#)sbus.c 8.1 (Berkeley) 6/11/93
81 */
82
83 /*
84 * Sbus stuff.
85 */
86 #include "opt_ddb.h"
87
88 #include <sys/param.h>
89 #include <sys/malloc.h>
90 #include <sys/systm.h>
91 #include <sys/device.h>
92 #include <vm/vm.h>
93
94 #include <machine/bus.h>
95 #include <sparc64/sparc64/vaddrs.h>
96 #include <sparc64/dev/sbusreg.h>
97 #include <dev/sbus/sbusvar.h>
98
99 #include <machine/autoconf.h>
100 #include <machine/ctlreg.h>
101 #include <machine/cpu.h>
102 #include <machine/sparc64.h>
103
104 #ifdef DEBUG
105 #define SDB_DVMA 0x1
106 #define SDB_INTR 0x2
107 int sbusdebug = 0;
108 #endif
109
110 void sbusreset __P((int));
111 int sbus_flush __P((struct sbus_softc *));
112
113 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
114 static bus_dma_tag_t sbus_alloc_dmatag __P((struct sbus_softc *));
115 static int sbus_get_intr __P((struct sbus_softc *, int,
116 struct sbus_intr **, int *));
117 static int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
118 int, bus_space_handle_t *));
119 static int _sbus_bus_map __P((
120 bus_space_tag_t,
121 bus_type_t,
122 bus_addr_t, /*offset*/
123 bus_size_t, /*size*/
124 int, /*flags*/
125 vaddr_t, /*preferred virtual address */
126 bus_space_handle_t *));
127 static void *sbus_intr_establish __P((
128 bus_space_tag_t,
129 int, /*level*/
130 int, /*flags*/
131 int (*) __P((void *)), /*handler*/
132 void *)); /*handler arg*/
133
134
135 /* autoconfiguration driver */
136 int sbus_match __P((struct device *, struct cfdata *, void *));
137 void sbus_attach __P((struct device *, struct device *, void *));
138
139
140 struct cfattach sbus_ca = {
141 sizeof(struct sbus_softc), sbus_match, sbus_attach
142 };
143
144 extern struct cfdriver sbus_cd;
145
146 /*
147 * DVMA routines
148 */
149 void sbus_enter __P((struct sbus_softc *, vaddr_t, int64_t, int));
150 void sbus_remove __P((struct sbus_softc *, vaddr_t, size_t));
151 int sbus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
152 bus_size_t, struct proc *, int));
153 void sbus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
154 void sbus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
155 bus_size_t, int));
156 int sbus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
157 bus_size_t alignment, bus_size_t boundary,
158 bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
159 void sbus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
160 int nsegs));
161 int sbus_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
162 int nsegs, size_t size, caddr_t *kvap, int flags));
163 void sbus_dmamem_unmap __P((bus_dma_tag_t tag, caddr_t kva,
164 size_t size));
165
166
167 /*
168 * Child devices receive the Sbus interrupt level in their attach
169 * arguments. We translate these to CPU IPLs using the following
170 * tables. Note: obio bus interrupt levels are identical to the
171 * processor IPL.
172 *
173 * The second set of tables is used when the Sbus interrupt level
174 * cannot be had from the PROM as an `interrupt' property. We then
175 * fall back on the `intr' property which contains the CPU IPL.
176 */
177
178 /* Translate Sbus interrupt level to processor IPL */
179 static int intr_sbus2ipl_4c[] = {
180 0, 1, 2, 3, 5, 7, 8, 9
181 };
182 static int intr_sbus2ipl_4m[] = {
183 0, 2, 3, 5, 7, 9, 11, 13
184 };
185
186 /*
187 * This value is or'ed into the attach args' interrupt level cookie
188 * if the interrupt level comes from an `intr' property, i.e. it is
189 * not an Sbus interrupt level.
190 */
191 #define SBUS_INTR_COMPAT 0x80000000
192
193
194 /*
195 * Print the location of some sbus-attached device (called just
196 * before attaching that device). If `sbus' is not NULL, the
197 * device was found but not configured; print the sbus as well.
198 * Return UNCONF (config_find ignores this if the device was configured).
199 */
200 int
201 sbus_print(args, busname)
202 void *args;
203 const char *busname;
204 {
205 struct sbus_attach_args *sa = args;
206 int i;
207
208 if (busname)
209 printf("%s at %s", sa->sa_name, busname);
210 printf(" slot %ld offset 0x%lx", (long)sa->sa_slot,
211 (u_long)sa->sa_offset);
212 for (i=0; i<sa->sa_nintr; i++) {
213 struct sbus_intr *sbi = &sa->sa_intr[i];
214
215 printf(" vector %lx ipl %ld",
216 (u_long)sbi->sbi_vec,
217 (long)INTLEV(sbi->sbi_pri));
218 }
219 return (UNCONF);
220 }
221
222 int
223 sbus_match(parent, cf, aux)
224 struct device *parent;
225 struct cfdata *cf;
226 void *aux;
227 {
228 struct mainbus_attach_args *ma = aux;
229
230 return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
231 }
232
233 /*
234 * Attach an Sbus.
235 */
236 void
237 sbus_attach(parent, self, aux)
238 struct device *parent;
239 struct device *self;
240 void *aux;
241 {
242 struct sbus_softc *sc = (struct sbus_softc *)self;
243 struct mainbus_attach_args *ma = aux;
244 int node = ma->ma_node;
245
246 int node0, error;
247 bus_space_tag_t sbt;
248 struct sbus_attach_args sa;
249 char *busname = "sbus";
250 struct bootpath *bp = ma->ma_bp;
251
252
253 sc->sc_bustag = ma->ma_bustag;
254 sc->sc_dmatag = ma->ma_dmatag;
255 sc->sc_sysio = (struct sysioreg*)(u_long)ma->ma_address[0]; /* Use prom mapping for sysio. */
256 sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN; /* Find interrupt group no */
257
258 /* Setup interrupt translation tables */
259 sc->sc_intr2ipl = CPU_ISSUN4C
260 ? intr_sbus2ipl_4c
261 : intr_sbus2ipl_4m;
262
263 /*
264 * Record clock frequency for synchronous SCSI.
265 * IS THIS THE CORRECT DEFAULT??
266 */
267 sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
268 printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
269
270 sbt = sbus_alloc_bustag(sc);
271 sc->sc_dmatag = sbus_alloc_dmatag(sc);
272
273 /*
274 * Get the SBus burst transfer size if burst transfers are supported
275 */
276 sc->sc_burst = getpropint(node, "burst-sizes", 0);
277
278 /* Propagate bootpath */
279 if (bp != NULL && strcmp(bp->name, busname) == 0)
280 bp++;
281 else
282 bp = NULL;
283
284 /*
285 * Collect address translations from the OBP.
286 */
287 error = getprop(node, "ranges", sizeof(struct sbus_range),
288 &sc->sc_nrange, (void **)&sc->sc_range);
289 switch (error) {
290 case 0:
291 break;
292 #if 0
293 case ENOENT:
294 /* Fall back to our own `range' construction */
295 sc->sc_range = sbus_translations;
296 sc->sc_nrange =
297 sizeof(sbus_translations)/sizeof(sbus_translations[0]);
298 break;
299 #endif
300 default:
301 panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
302 }
303
304
305 /*
306 * Setup the iommu.
307 *
308 * The sun4u iommu is part of the SBUS controller so we will
309 * deal with it here. We could try to fake a device node so
310 * we can eventually share it with the PCI bus run by psyco,
311 * but I don't want to get into that sort of cruft.
312 */
313
314 /*
315 * All IOMMUs will share the same TSB which is allocated in pmap_bootstrap.
316 *
317 * This makes device management easier.
318 */
319 {
320 extern int64_t *iotsb;
321 extern paddr_t iotsbp;
322 extern int iotsbsize;
323
324 sc->sc_tsbsize = iotsbsize;
325 sc->sc_tsb = iotsb;
326 sc->sc_ptsb = iotsbp;
327 }
328 #if 1
329 /* Need to do 64-bit stores */
330 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_iommu.iommu_cr,
331 0, (IOMMUCR_TSB1K|IOMMUCR_8KPG|IOMMUCR_EN));
332 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_iommu.iommu_tsb,
333 0, sc->sc_ptsb);
334 #else
335 stxa(&sc->sc_sysio->sys_iommu.iommu_cr,ASI_NUCLEUS,(IOMMUCR_TSB1K|IOMMUCR_8KPG|IOMMUCR_EN));
336 stxa(&sc->sc_sysio->sys_iommu.iommu_tsb,ASI_NUCLEUS,sc->sc_ptsb);
337 #endif
338 #ifdef DEBUG
339 if (sbusdebug & SDB_DVMA)
340 {
341 /* Probe the iommu */
342 int64_t cr, tsb;
343
344 printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n", &sc->sc_sysio->sys_iommu.iommu_cr,
345 &sc->sc_sysio->sys_iommu.iommu_tsb, &sc->sc_sysio->sys_iommu.iommu_flush);
346 cr = sc->sc_sysio->sys_iommu.iommu_cr;
347 tsb = sc->sc_sysio->sys_iommu.iommu_tsb;
348 printf("iommu cr=%lx tsb=%lx\n", (long)cr, (long)tsb);
349 printf("sysio base %p phys %p TSB base %p phys %p",
350 (long)sc->sc_sysio, (long)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_sysio),
351 (long)sc->sc_tsb, (long)sc->sc_ptsb);
352 delay(1000000); /* 1 s */
353 }
354 #endif
355
356 /*
357 * Initialize streaming buffer.
358 */
359 sc->sc_flushpa = pmap_extract(pmap_kernel(), (vaddr_t)&sc->sc_flush);
360 #if 1
361 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_strbuf.strbuf_ctl,
362 0, STRBUF_EN); /* Enable diagnostics mode? */
363 #else
364 stxa(&sc->sc_sysio->sys_strbuf.strbuf_ctl,ASI_NUCLEUS,STRBUF_EN);
365 #endif
366
367 /*
368 * Loop through ROM children, fixing any relative addresses
369 * and then configuring each device.
370 * `specials' is an array of device names that are treated
371 * specially:
372 */
373 node0 = firstchild(node);
374 for (node = node0; node; node = nextsibling(node)) {
375 char *name = getpropstring(node, "name");
376
377 if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
378 node, bp, &sa) != 0) {
379 printf("sbus_attach: %s: incomplete\n", name);
380 continue;
381 }
382 (void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
383 sbus_destroy_attach_args(&sa);
384 }
385 }
386
387 int
388 sbus_setup_attach_args(sc, bustag, dmatag, node, bp, sa)
389 struct sbus_softc *sc;
390 bus_space_tag_t bustag;
391 bus_dma_tag_t dmatag;
392 int node;
393 struct bootpath *bp;
394 struct sbus_attach_args *sa;
395 {
396 /*struct sbus_reg sbusreg;*/
397 /*int base;*/
398 int error;
399 int n;
400
401 bzero(sa, sizeof(struct sbus_attach_args));
402 error = getprop(node, "name", 1, &n, (void **)&sa->sa_name);
403 if (error != 0)
404 return (error);
405 sa->sa_name[n] = '\0';
406
407 sa->sa_bustag = bustag;
408 sa->sa_dmatag = dmatag;
409 sa->sa_node = node;
410 sa->sa_bp = bp;
411
412 error = getprop(node, "reg", sizeof(struct sbus_reg),
413 &sa->sa_nreg, (void **)&sa->sa_reg);
414 if (error != 0) {
415 char buf[32];
416 if (error != ENOENT ||
417 !node_has_property(node, "device_type") ||
418 strcmp(getpropstringA(node, "device_type", buf),
419 "hierarchical") != 0)
420 return (error);
421 }
422 for (n = 0; n < sa->sa_nreg; n++) {
423 /* Convert to relative addressing, if necessary */
424 u_int32_t base = sa->sa_reg[n].sbr_offset;
425 if (SBUS_ABS(base)) {
426 sa->sa_reg[n].sbr_slot = SBUS_ABS_TO_SLOT(base);
427 sa->sa_reg[n].sbr_offset = SBUS_ABS_TO_OFFSET(base);
428 }
429 }
430
431 if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0)
432 return (error);
433
434 error = getprop(node, "address", sizeof(u_int32_t),
435 &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs);
436 if (error != 0 && error != ENOENT)
437 return (error);
438
439 return (0);
440 }
441
442 void
443 sbus_destroy_attach_args(sa)
444 struct sbus_attach_args *sa;
445 {
446 if (sa->sa_name != NULL)
447 free(sa->sa_name, M_DEVBUF);
448
449 if (sa->sa_nreg != 0)
450 free(sa->sa_reg, M_DEVBUF);
451
452 if (sa->sa_intr)
453 free(sa->sa_intr, M_DEVBUF);
454
455 if (sa->sa_promvaddrs)
456 free((void *)sa->sa_promvaddrs, M_DEVBUF);
457
458 bzero(sa, sizeof(struct sbus_attach_args));/*DEBUG*/
459 }
460
461
462 int
463 _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp)
464 bus_space_tag_t t;
465 bus_type_t btype;
466 bus_addr_t offset;
467 bus_size_t size;
468 int flags;
469 vaddr_t vaddr;
470 bus_space_handle_t *hp;
471 {
472 struct sbus_softc *sc = t->cookie;
473 int64_t slot = btype;
474 int i;
475
476 for (i = 0; i < sc->sc_nrange; i++) {
477 bus_addr_t paddr;
478
479 if (sc->sc_range[i].cspace != slot)
480 continue;
481
482 /* We've found the connection to the parent bus */
483 paddr = sc->sc_range[i].poffset + offset;
484 paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
485 #ifdef DEBUG
486 if (sbusdebug & SDB_DVMA)
487 printf("\n_sbus_bus_map: mapping paddr slot %lx offset %lx poffset %lx paddr %lx\n",
488 (long)slot, (long)offset, (long)sc->sc_range[i].poffset, (long)paddr);
489 #endif
490 return (bus_space_map2(sc->sc_bustag, 0, paddr,
491 size, flags, vaddr, hp));
492 }
493
494 return (EINVAL);
495 }
496
497 int
498 sbus_bus_mmap(t, btype, paddr, flags, hp)
499 bus_space_tag_t t;
500 bus_type_t btype;
501 bus_addr_t paddr;
502 int flags;
503 bus_space_handle_t *hp;
504 {
505 bus_addr_t offset = paddr;
506 int slot = (paddr>>32);
507 struct sbus_softc *sc = t->cookie;
508 int i;
509
510 for (i = 0; i < sc->sc_nrange; i++) {
511 bus_addr_t paddr;
512
513 if (sc->sc_range[i].cspace != slot)
514 continue;
515
516 paddr = sc->sc_range[i].poffset + offset;
517 paddr |= ((bus_addr_t)sc->sc_range[i].pspace<<32);
518 return (bus_space_mmap(sc->sc_bustag, 0, paddr,
519 flags, hp));
520 }
521
522 return (-1);
523 }
524
525
526 /*
527 * Each attached device calls sbus_establish after it initializes
528 * its sbusdev portion.
529 */
530 void
531 sbus_establish(sd, dev)
532 register struct sbusdev *sd;
533 register struct device *dev;
534 {
535 register struct sbus_softc *sc;
536 register struct device *curdev;
537
538 /*
539 * We have to look for the sbus by name, since it is not necessarily
540 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
541 * We don't just use the device structure of the above-attached
542 * sbus, since we might (in the future) support multiple sbus's.
543 */
544 for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
545 if (!curdev || !curdev->dv_xname)
546 panic("sbus_establish: can't find sbus parent for %s",
547 sd->sd_dev->dv_xname
548 ? sd->sd_dev->dv_xname
549 : "<unknown>" );
550
551 if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
552 break;
553 }
554 sc = (struct sbus_softc *) curdev;
555
556 sd->sd_dev = dev;
557 sd->sd_bchain = sc->sc_sbdev;
558 sc->sc_sbdev = sd;
559 }
560
561 /*
562 * Reset the given sbus. (???)
563 */
564 void
565 sbusreset(sbus)
566 int sbus;
567 {
568 register struct sbusdev *sd;
569 struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
570 struct device *dev;
571
572 printf("reset %s:", sc->sc_dev.dv_xname);
573 for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
574 if (sd->sd_reset) {
575 dev = sd->sd_dev;
576 (*sd->sd_reset)(dev);
577 printf(" %s", dev->dv_xname);
578 }
579 }
580 #if 1
581 /* Reload iommu regs */
582 bus_space_write_8(sc->ma_bustag, &sc->sc_sysio->sys_iommu.iommu_cr,
583 0, (IOMMUCR_TSB1K|IOMMUCR_8KPG|IOMMUCR_EN));
584 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_iommu.iommu_tsb,
585 0, sc->sc_ptsb);
586 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_strbuf.strbuf_ctl,
587 0, STRBUF_EN); /* Enable diagnostics mode? */
588 #else
589 /* Reload iommu regs */
590 stxa(&sc->sc_sysio->sys_iommu.iommu_cr,ASI_NUCLEUS,(IOMMUCR_TSB1K|IOMMUCR_8KPG|IOMMUCR_EN));
591 stxa(&sc->sc_sysio->sys_iommu.iommu_tsb,ASI_NUCLEUS,sc->sc_ptsb);
592 stxa(&sc->sc_sysio->sys_strbuf.strbuf_ctl,ASI_NUCLEUS,STRBUF_EN);
593 #endif
594 }
595
596 /*
597 * Here are the iommu control routines.
598 */
599 void
600 sbus_enter(sc, va, pa, flags)
601 struct sbus_softc *sc;
602 vaddr_t va;
603 int64_t pa;
604 int flags;
605 {
606 int64_t tte;
607
608 #ifdef DIAGNOSTIC
609 if (va < sc->sc_dvmabase)
610 panic("sbus_enter: va 0x%lx not in DVMA space",va);
611 #endif
612
613 tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
614 !(flags&BUS_DMA_COHERENT));
615
616 /* Is the streamcache flush really needed? */
617 #if 1
618 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_strbuf.strbuf_pgflush,
619 0, va);
620 #else
621 stxa(&(sc->sc_sysio->sys_strbuf.strbuf_pgflush), ASI_NUCLEUS, va);
622 #endif
623 sbus_flush(sc);
624 sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)] = tte;
625 #if 1
626 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_iommu.iommu_flush,
627 0, va);
628 #else
629 stxa(&sc->sc_sysio->sys_iommu.iommu_flush,ASI_NUCLEUS,va);
630 #endif
631 #ifdef DEBUG
632 if (sbusdebug & SDB_DVMA)
633 printf("sbus_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
634 va, (long)pa, IOTSBSLOT(va,sc->sc_tsbsize),
635 &sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)],
636 (long)tte);
637 #endif
638 }
639
640 /*
641 * sbus_clear: clears mappings created by sbus_enter
642 *
643 * Only demap from IOMMU if flag is set.
644 */
645 void
646 sbus_remove(sc, va, len)
647 struct sbus_softc *sc;
648 vaddr_t va;
649 size_t len;
650 {
651
652 #ifdef DIAGNOSTIC
653 if (va < sc->sc_dvmabase)
654 panic("sbus_remove: va 0x%lx not in DVMA space", (long)va);
655 if ((long)(va + len) < (long)va)
656 panic("sbus_remove: va 0x%lx + len 0x%lx wraps",
657 (long) va, (long) len);
658 if (len & ~0xfffffff)
659 panic("sbus_remove: rediculous len 0x%lx", (long)len);
660 #endif
661
662 va = trunc_page(va);
663 while (len > 0) {
664
665 /*
666 * Streaming buffer flushes:
667 *
668 * 1 Tell strbuf to flush by storing va to strbuf_pgflush
669 * If we're not on a cache line boundary (64-bits):
670 * 2 Store 0 in flag
671 * 3 Store pointer to flag in flushsync
672 * 4 wait till flushsync becomes 0x1
673 *
674 * If it takes more than .5 sec, something went wrong.
675 */
676 #ifdef DEBUG
677 if (sbusdebug & SDB_DVMA)
678 printf("sbus_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
679 (long)va, (long)IOTSBSLOT(va,sc->sc_tsbsize),
680 (long)&sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)],
681 (long)(sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)]),
682 (u_long)len);
683 #endif
684 #if 1
685 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_strbuf.strbuf_pgflush, 0, va);
686 #else
687 stxa(&(sc->sc_sysio->sys_strbuf.strbuf_pgflush), ASI_NUCLEUS, va);
688 #endif
689 if (len <= NBPG) {
690 sbus_flush(sc);
691 len = 0;
692 } else len -= NBPG;
693 #ifdef DEBUG
694 if (sbusdebug & SDB_DVMA)
695 printf("sbus_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
696 (long)va, (long)IOTSBSLOT(va,sc->sc_tsbsize),
697 (long)&sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)],
698 (long)(sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)]),
699 (u_long)len);
700 #endif
701 sc->sc_tsb[IOTSBSLOT(va,sc->sc_tsbsize)] = 0;
702 #if 1
703 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_iommu.iommu_flush, 0, va);
704 #else
705 stxa(&sc->sc_sysio->sys_iommu.iommu_flush, ASI_NUCLEUS, va);
706 #endif
707 va += NBPG;
708 }
709 }
710
711 int
712 sbus_flush(sc)
713 struct sbus_softc *sc;
714 {
715 extern u_int64_t cpu_clockrate;
716 u_int64_t flushtimeout;
717
718 sc->sc_flush = 0;
719 membar_sync();
720 #if 1
721 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_strbuf.strbuf_flushsync, 0, sc->sc_flushpa);
722 #else
723 stxa(&sc->sc_sysio->sys_strbuf.strbuf_flushsync, ASI_NUCLEUS, sc->sc_flushpa);
724 #endif
725 membar_sync();
726 flushtimeout = tick() + cpu_clockrate/2; /* .5 sec after *now* */
727 #ifdef DEBUG
728 if (sbusdebug & SDB_DVMA)
729 printf("sbus_flush: flush = %lx at va = %lx pa = %lx now=%lx until = %lx\n",
730 (long)sc->sc_flush, (long)&sc->sc_flush,
731 (long)sc->sc_flushpa, (long)tick(), flushtimeout);
732 #endif
733 /* Bypass non-coherent D$ */
734 #if 0
735 while( !ldxa(sc->sc_flushpa, ASI_PHYS_CACHED) && flushtimeout > tick()) membar_sync();
736 #else
737 { int i; for(i=140000000/2; !ldxa(sc->sc_flushpa, ASI_PHYS_CACHED) && i; i--) membar_sync(); }
738 #endif
739 #ifdef DIAGNOSTIC
740 if( !sc->sc_flush ) {
741 printf("sbus_flush: flush timeout %p at %p\n", (long)sc->sc_flush,
742 (long)sc->sc_flushpa); /* panic? */
743 #ifdef DDB
744 Debugger();
745 #endif
746 }
747 #endif
748 #ifdef DEBUG
749 if (sbusdebug & SDB_DVMA)
750 printf("sbus_flush: flushed\n");
751 #endif
752 return (sc->sc_flush);
753 }
754 /*
755 * Get interrupt attributes for an Sbus device.
756 */
757 int
758 sbus_get_intr(sc, node, ipp, np)
759 struct sbus_softc *sc;
760 int node;
761 struct sbus_intr **ipp;
762 int *np;
763 {
764 int *ipl;
765 int i, n, error;
766 char buf[32];
767
768 /*
769 * The `interrupts' property contains the Sbus interrupt level.
770 */
771 ipl = NULL;
772 if (getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) {
773 /* Change format to an `struct sbus_intr' array */
774 struct sbus_intr *ip;
775 int pri = 0;
776 ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT);
777 if (ip == NULL)
778 return (ENOMEM);
779 /* Now things get ugly. We need to take this value which is
780 * the interrupt vector number and encode the IPL into it
781 * somehow. Luckily, the interrupt vector has lots of free
782 * space and we can easily stuff the IPL in there for a while.
783 */
784 getpropstringA(node, "device_type", buf);
785 for (i=0; intrmap[i].in_class; i++) {
786 if (strcmp(intrmap[i].in_class, buf) == 0) {
787 pri = INTLEVENCODE(intrmap[i].in_lev);
788 break;
789 }
790 }
791 for (n = 0; n < *np; n++) {
792 /*
793 * We encode vector and priority into sbi_pri so we
794 * can pass them as a unit. This will go away if
795 * sbus_establish ever takes an sbus_intr instead
796 * of an integer level.
797 * Stuff the real vector in sbi_vec.
798 */
799 ip[n].sbi_pri = pri|ipl[n];
800 ip[n].sbi_vec = ipl[n];
801 }
802 free(ipl, M_DEVBUF);
803 *ipp = ip;
804 return (0);
805 }
806
807 /* We really don't support the following */
808 /* printf("\nWARNING: sbus_get_intr() \"interrupts\" not found -- using \"intr\"\n"); */
809 /* And some devices don't even have interrupts */
810 /*
811 * Fall back on `intr' property.
812 */
813 *ipp = NULL;
814 error = getprop(node, "intr", sizeof(struct sbus_intr),
815 np, (void **)ipp);
816 switch (error) {
817 case 0:
818 for (n = *np; n-- > 0;) {
819 /*
820 * Move the interrupt vector into place.
821 * We could remap the level, but the SBUS priorities
822 * are probably good enough.
823 */
824 (*ipp)[n].sbi_vec = (*ipp)[n].sbi_pri;
825 (*ipp)[n].sbi_pri |= INTLEVENCODE((*ipp)[n].sbi_pri);
826 }
827 break;
828 case ENOENT:
829 error = 0;
830 break;
831 }
832
833 return (error);
834 }
835
836
837 /*
838 * Install an interrupt handler for an Sbus device.
839 */
840 void *
841 sbus_intr_establish(t, level, flags, handler, arg)
842 bus_space_tag_t t;
843 int level;
844 int flags;
845 int (*handler) __P((void *));
846 void *arg;
847 {
848 struct sbus_softc *sc = t->cookie;
849 struct intrhand *ih;
850 int ipl;
851 long vec = level;
852
853 ih = (struct intrhand *)
854 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
855 if (ih == NULL)
856 return (NULL);
857
858 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
859 ipl = vec;
860 else if ((vec & SBUS_INTR_COMPAT) != 0)
861 ipl = vec & ~SBUS_INTR_COMPAT;
862 else {
863 /* Decode and remove IPL */
864 ipl = INTLEV(vec);
865 vec = INTVEC(vec);
866 #ifdef DEBUG
867 if (sbusdebug & SDB_INTR) {
868 printf("\nsbus: intr[%ld]%lx: %lx\n", (long)ipl, (long)vec,
869 intrlev[vec]);
870 printf("Hunting for IRQ...\n");
871 }
872 #endif
873 if ((vec & INTMAP_OBIO) == 0) {
874 /* We're in an SBUS slot */
875 /* Register the map and clear intr registers */
876 #ifdef DEBUG
877 if (sbusdebug & SDB_INTR) {
878 int64_t *intrptr = &(&sc->sc_sysio->sbus_slot0_int)[INTSLOT(vec)];
879 int64_t intrmap = *intrptr;
880
881 printf("Found SBUS %lx IRQ as %llx in slot %ld\n",
882 (long)vec, (long)intrmap,
883 (long)INTSLOT(vec));
884 }
885 #endif
886 ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[INTSLOT(vec)];
887 ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[INTVEC(vec)];
888 /* Enable the interrupt */
889 vec |= INTMAP_V;
890 /* Insert IGN */
891 vec |= sc->sc_ign;
892 bus_space_write_8(sc->sc_bustag, ih->ih_map, 0, vec);
893 } else {
894 int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
895 int64_t intrmap = 0;
896 int i;
897
898 /* Insert IGN */
899 vec |= sc->sc_ign;
900 for (i=0;
901 &intrptr[i] <= (int64_t *)&sc->sc_sysio->reserved_int_map &&
902 INTVEC(intrmap=intrptr[i]) != INTVEC(vec);
903 i++);
904 if (INTVEC(intrmap) == INTVEC(vec)) {
905 #ifdef DEBUG
906 if (sbusdebug & SDB_INTR)
907 printf("Found OBIO %lx IRQ as %lx in slot %d\n",
908 vec, (long)intrmap, i);
909 #endif
910 /* Register the map and clear intr registers */
911 ih->ih_map = &intrptr[i];
912 intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
913 ih->ih_clr = &intrptr[i];
914 /* Enable the interrupt */
915 intrmap |= INTMAP_V;
916 bus_space_write_8(sc->sc_bustag, ih->ih_map, 0, (u_long)intrmap);
917 } else panic("IRQ not found!");
918 }
919 }
920 #ifdef DEBUG
921 if (sbusdebug & SDB_INTR) { long i; for (i=0; i<1400000000; i++); }
922 #endif
923
924 ih->ih_fun = handler;
925 ih->ih_arg = arg;
926 ih->ih_number = vec;
927 ih->ih_pil = (1<<ipl);
928 if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
929 intr_fasttrap(ipl, (void (*)__P((void)))handler);
930 else
931 intr_establish(ipl, ih);
932 return (ih);
933 }
934
935 static bus_space_tag_t
936 sbus_alloc_bustag(sc)
937 struct sbus_softc *sc;
938 {
939 bus_space_tag_t sbt;
940
941 sbt = (bus_space_tag_t)
942 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
943 if (sbt == NULL)
944 return (NULL);
945
946 bzero(sbt, sizeof *sbt);
947 sbt->cookie = sc;
948 sbt->parent = sc->sc_bustag;
949 sbt->type = ASI_PRIMARY;
950 sbt->sparc_bus_map = _sbus_bus_map;
951 sbt->sparc_bus_mmap = sbus_bus_mmap;
952 sbt->sparc_intr_establish = sbus_intr_establish;
953 return (sbt);
954 }
955
956
957 static bus_dma_tag_t
958 sbus_alloc_dmatag(sc)
959 struct sbus_softc *sc;
960 {
961 bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
962
963 sdt = (bus_dma_tag_t)
964 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
965 if (sdt == NULL)
966 /* Panic? */
967 return (psdt);
968
969 sdt->_cookie = sc;
970 sdt->_parent = psdt;
971 #define PCOPY(x) sdt->x = psdt->x
972 PCOPY(_dmamap_create);
973 PCOPY(_dmamap_destroy);
974 sdt->_dmamap_load = sbus_dmamap_load;
975 PCOPY(_dmamap_load_mbuf);
976 PCOPY(_dmamap_load_uio);
977 PCOPY(_dmamap_load_raw);
978 sdt->_dmamap_unload = sbus_dmamap_unload;
979 sdt->_dmamap_sync = sbus_dmamap_sync;
980 sdt->_dmamem_alloc = sbus_dmamem_alloc;
981 sdt->_dmamem_free = sbus_dmamem_free;
982 sdt->_dmamem_map = sbus_dmamem_map;
983 sdt->_dmamem_unmap = sbus_dmamem_unmap;
984 PCOPY(_dmamem_mmap);
985 #undef PCOPY
986 sc->sc_dmatag = sdt;
987 return (sdt);
988 }
989
990 int
991 sbus_dmamap_load(t, map, buf, buflen, p, flags)
992 bus_dma_tag_t t;
993 bus_dmamap_t map;
994 void *buf;
995 bus_size_t buflen;
996 struct proc *p;
997 int flags;
998 {
999 int err;
1000 bus_size_t sgsize;
1001 paddr_t curaddr;
1002 vaddr_t dvmaddr, vaddr = (vaddr_t)buf;
1003 pmap_t pmap;
1004 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1005
1006 if (map->dm_nsegs) {
1007 /* Already in use?? */
1008 #ifdef DIAGNOSTIC
1009 printf("sbus_dmamap_load: map still in use\n");
1010 #endif
1011 bus_dmamap_unload(t, map);
1012 }
1013 if ((err = bus_dmamap_load(t->_parent, map, buf, buflen, p, flags)))
1014 return (err);
1015
1016 if (p != NULL)
1017 pmap = p->p_vmspace->vm_map.pmap;
1018 else
1019 pmap = pmap_kernel();
1020
1021 dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
1022 sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
1023 for (; buflen > 0; ) {
1024 /*
1025 * Get the physical address for this page.
1026 */
1027 if ((curaddr = (bus_addr_t)pmap_extract(pmap, (vaddr_t)vaddr)) == NULL) {
1028 bus_dmamap_unload(t, map);
1029 return (-1);
1030 }
1031
1032 /*
1033 * Compute the segment size, and adjust counts.
1034 */
1035 sgsize = NBPG - ((u_long)vaddr & PGOFSET);
1036 if (buflen < sgsize)
1037 sgsize = buflen;
1038
1039 #ifdef DEBUG
1040 if (sbusdebug & SDB_DVMA)
1041 printf("sbus_dmamap_load: map %p loading va %lx at pa %lx\n",
1042 map, (long)dvmaddr, (long)(curaddr & ~(NBPG-1)));
1043 #endif
1044 sbus_enter(sc, trunc_page(dvmaddr), trunc_page(curaddr), flags);
1045
1046 dvmaddr += PAGE_SIZE;
1047 vaddr += sgsize;
1048 buflen -= sgsize;
1049 }
1050 return (0);
1051 }
1052
1053 void
1054 sbus_dmamap_unload(t, map)
1055 bus_dma_tag_t t;
1056 bus_dmamap_t map;
1057 {
1058 vaddr_t addr;
1059 int len;
1060 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1061
1062 if (map->dm_nsegs != 1)
1063 panic("_sbus_dmamap_unload: nsegs = %d", map->dm_nsegs);
1064
1065 addr = trunc_page(map->dm_segs[0].ds_addr);
1066 len = map->dm_segs[0].ds_len;
1067
1068 #ifdef DEBUG
1069 if (sbusdebug & SDB_DVMA)
1070 printf("sbus_dmamap_unload: map %p removing va %lx size %lx\n",
1071 map, (long)addr, (long)len);
1072 #endif
1073 sbus_remove(sc, addr, len);
1074 bus_dmamap_unload(t->_parent, map);
1075 }
1076
1077
1078 void
1079 sbus_dmamap_sync(t, map, offset, len, ops)
1080 bus_dma_tag_t t;
1081 bus_dmamap_t map;
1082 bus_addr_t offset;
1083 bus_size_t len;
1084 int ops;
1085 {
1086 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1087 vaddr_t va = map->dm_segs[0].ds_addr + offset;
1088
1089 /*
1090 * We only support one DMA segment; supporting more makes this code
1091 * too unweildy.
1092 */
1093
1094 if (ops&BUS_DMASYNC_PREREAD) {
1095 #ifdef DEBUG
1096 if (sbusdebug & SDB_DVMA)
1097 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREREAD\n",
1098 (long)va, (u_long)len);
1099 #endif
1100
1101 /* Nothing to do */;
1102 }
1103 if (ops&BUS_DMASYNC_POSTREAD) {
1104 /*
1105 * We should sync the IOMMU streaming caches here first.
1106 */
1107 #ifdef DEBUG
1108 if (sbusdebug & SDB_DVMA)
1109 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTREAD\n",
1110 (long)va, (u_long)len);
1111 #endif
1112 while (len > 0) {
1113
1114 /*
1115 * Streaming buffer flushes:
1116 *
1117 * 1 Tell strbuf to flush by storing va to strbuf_pgflush
1118 * If we're not on a cache line boundary (64-bits):
1119 * 2 Store 0 in flag
1120 * 3 Store pointer to flag in flushsync
1121 * 4 wait till flushsync becomes 0x1
1122 *
1123 * If it takes more than .5 sec, something went wrong.
1124 */
1125 #ifdef DEBUG
1126 if (sbusdebug & SDB_DVMA)
1127 printf("sbus_dmamap_sync: flushing va %p, %lu bytes left\n",
1128 (long)va, (u_long)len);
1129 #endif
1130 #if 1
1131 bus_space_write_8(sc->sc_bustag, &sc->sc_sysio->sys_strbuf.strbuf_pgflush, 0, va);
1132 #else
1133 stxa(&(sc->sc_sysio->sys_strbuf.strbuf_pgflush), ASI_NUCLEUS, va);
1134 #endif
1135 if (len <= NBPG) {
1136 sbus_flush(sc);
1137 len = 0;
1138 } else
1139 len -= NBPG;
1140 va += NBPG;
1141 }
1142 }
1143 if (ops&BUS_DMASYNC_PREWRITE) {
1144 #ifdef DEBUG
1145 if (sbusdebug & SDB_DVMA)
1146 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_PREWRITE\n",
1147 (long)va, (u_long)len);
1148 #endif
1149 /* Nothing to do */;
1150 }
1151 if (ops&BUS_DMASYNC_POSTWRITE) {
1152 #ifdef DEBUG
1153 if (sbusdebug & SDB_DVMA)
1154 printf("sbus_dmamap_sync: syncing va %p len %lu BUS_DMASYNC_POSTWRITE\n",
1155 (long)va, (u_long)len);
1156 #endif
1157 /* Nothing to do */;
1158 }
1159 bus_dmamap_sync(t->_parent, map, offset, len, ops);
1160 }
1161
1162
1163 /*
1164 * Take memory allocated by our parent bus and generate DVMA mappings for it.
1165 */
1166 int
1167 sbus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1168 bus_dma_tag_t t;
1169 bus_size_t size, alignment, boundary;
1170 bus_dma_segment_t *segs;
1171 int nsegs;
1172 int *rsegs;
1173 int flags;
1174 {
1175 paddr_t curaddr;
1176 bus_addr_t dvmaddr;
1177 vm_page_t m;
1178 struct pglist *mlist;
1179 int error;
1180 int n;
1181 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1182
1183 if ((error = bus_dmamem_alloc(t->_parent, size, alignment,
1184 boundary, segs, nsegs, rsegs, flags)))
1185 return (error);
1186
1187 /*
1188 * Allocate a DVMA mapping for our new memory.
1189 */
1190 for (n=0; n<*rsegs; n++) {
1191 dvmaddr = dvmamap_alloc(segs[0].ds_len, flags);
1192 if (dvmaddr == (bus_addr_t)-1) {
1193 /* Free what we got and exit */
1194 bus_dmamem_free(t->_parent, segs, nsegs);
1195 return (ENOMEM);
1196 }
1197 segs[n].ds_addr = dvmaddr;
1198 size = segs[n].ds_len;
1199 mlist = segs[n]._ds_mlist;
1200
1201 /* Map memory into DVMA space */
1202 for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1203 curaddr = VM_PAGE_TO_PHYS(m);
1204 sbus_enter(sc, dvmaddr, curaddr, flags);
1205 dvmaddr += PAGE_SIZE;
1206 }
1207 }
1208 return (0);
1209 }
1210
1211 void
1212 sbus_dmamem_free(t, segs, nsegs)
1213 bus_dma_tag_t t;
1214 bus_dma_segment_t *segs;
1215 int nsegs;
1216 {
1217 vaddr_t addr;
1218 int len;
1219 int n;
1220 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1221
1222
1223 for (n=0; n<nsegs; n++) {
1224 addr = segs[n].ds_addr;
1225 len = segs[n].ds_len;
1226 sbus_remove(sc, addr, len);
1227 dvmamap_free(addr, len);
1228 }
1229 bus_dmamem_free(t->_parent, segs, nsegs);
1230 }
1231
1232 /*
1233 * Map the DVMA mappings into the kernel pmap.
1234 * Check the flags to see whether we're streaming or coherent.
1235 */
1236 int
1237 sbus_dmamem_map(t, segs, nsegs, size, kvap, flags)
1238 bus_dma_tag_t t;
1239 bus_dma_segment_t *segs;
1240 int nsegs;
1241 size_t size;
1242 caddr_t *kvap;
1243 int flags;
1244 {
1245 vm_page_t m;
1246 vaddr_t va;
1247 bus_addr_t addr;
1248 struct pglist *mlist;
1249 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1250 int cbit;
1251
1252 /*
1253 * digest flags:
1254 */
1255 cbit = 0;
1256 if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1257 cbit |= PMAP_NVC;
1258 if (flags & BUS_DMA_NOCACHE) /* sideffects */
1259 cbit |= PMAP_NC;
1260 /*
1261 * Now take this and map it into the CPU since it should already
1262 * be in the the IOMMU.
1263 */
1264 *kvap = (caddr_t)va = segs[0].ds_addr;
1265 mlist = segs[0]._ds_mlist;
1266 for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1267
1268 if (size == 0)
1269 panic("_bus_dmamem_map: size botch");
1270
1271 addr = VM_PAGE_TO_PHYS(m);
1272 pmap_enter(pmap_kernel(), va, addr | cbit,
1273 VM_PROT_READ | VM_PROT_WRITE, TRUE);
1274 va += PAGE_SIZE;
1275 size -= PAGE_SIZE;
1276 }
1277
1278 return (0);
1279 }
1280
1281 /*
1282 * Unmap DVMA mappings from kernel
1283 */
1284 void
1285 sbus_dmamem_unmap(t, kva, size)
1286 bus_dma_tag_t t;
1287 caddr_t kva;
1288 size_t size;
1289 {
1290 struct sbus_softc *sc = (struct sbus_softc *)t->_cookie;
1291
1292 #ifdef DIAGNOSTIC
1293 if ((u_long)kva & PGOFSET)
1294 panic("_bus_dmamem_unmap");
1295 #endif
1296
1297 size = round_page(size);
1298 pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
1299 }
1300