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