sbus.c revision 1.105 1 /* $NetBSD: sbus.c,v 1.105 2023/12/01 06:47:59 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1999-2002 Eduardo Horvath
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31
32 /*
33 * Sbus stuff.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: sbus.c,v 1.105 2023/12/01 06:47:59 thorpej Exp $");
38
39 #include "opt_ddb.h"
40
41 #include <sys/param.h>
42 #include <sys/malloc.h>
43 #include <sys/kmem.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/reboot.h>
47 #include <sys/vmem.h>
48
49 #include <sys/bus.h>
50 #include <machine/openfirm.h>
51
52 #include <sparc64/dev/iommureg.h>
53 #include <sparc64/dev/iommuvar.h>
54 #include <sparc64/dev/sbusreg.h>
55 #include <dev/sbus/sbusvar.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #include <machine/autoconf.h>
60 #include <machine/cpu.h>
61 #include <machine/sparc64.h>
62
63 #ifdef DEBUG
64 #define SDB_DVMA 0x1
65 #define SDB_INTR 0x2
66 int sbus_debug = 0;
67 #define DPRINTF(l, s) do { if (sbus_debug & l) printf s; } while (0)
68 #else
69 #define DPRINTF(l, s)
70 #endif
71
72 void sbusreset(int);
73
74 static bus_dma_tag_t sbus_alloc_dmatag(struct sbus_softc *);
75 static int sbus_get_intr(struct sbus_softc *, int, struct openprom_intr **,
76 int *, int);
77 static int sbus_overtemp(void *);
78 static int _sbus_bus_map(
79 bus_space_tag_t,
80 bus_addr_t, /*offset*/
81 bus_size_t, /*size*/
82 int, /*flags*/
83 vaddr_t, /* XXX unused -- compat w/sparc */
84 bus_space_handle_t *);
85 static void *sbus_intr_establish(
86 bus_space_tag_t,
87 int, /*`device class' priority*/
88 int, /*Sbus interrupt level*/
89 int (*)(void *), /*handler*/
90 void *, /*handler arg*/
91 void (*)(void)); /*optional fast trap*/
92
93
94 /* autoconfiguration driver */
95 int sbus_match(device_t, cfdata_t, void *);
96 void sbus_attach(device_t, device_t, void *);
97
98
99 CFATTACH_DECL_NEW(sbus, sizeof(struct sbus_softc),
100 sbus_match, sbus_attach, NULL, NULL);
101
102 extern struct cfdriver sbus_cd;
103
104 /*
105 * DVMA routines
106 */
107 static int sbus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
108 bus_size_t, int, bus_dmamap_t *);
109
110 /*
111 * Child devices receive the Sbus interrupt level in their attach
112 * arguments. We translate these to CPU IPLs using the following
113 * tables. Note: obio bus interrupt levels are identical to the
114 * processor IPL.
115 *
116 * The second set of tables is used when the Sbus interrupt level
117 * cannot be had from the PROM as an `interrupt' property. We then
118 * fall back on the `intr' property which contains the CPU IPL.
119 */
120
121 /*
122 * This value is or'ed into the attach args' interrupt level cookie
123 * if the interrupt level comes from an `intr' property, i.e. it is
124 * not an Sbus interrupt level.
125 */
126 #define SBUS_INTR_COMPAT 0x80000000
127
128
129 /*
130 * Print the location of some sbus-attached device (called just
131 * before attaching that device). If `sbus' is not NULL, the
132 * device was found but not configured; print the sbus as well.
133 * Return UNCONF (config_find ignores this if the device was configured).
134 */
135 int
136 sbus_print(void *args, const char *busname)
137 {
138 struct sbus_attach_args *sa = args;
139 int i;
140
141 if (busname)
142 aprint_normal("%s at %s", sa->sa_name, busname);
143 aprint_normal(" slot %ld offset 0x%lx", (long)sa->sa_slot,
144 (u_long)sa->sa_offset);
145 for (i = 0; i < sa->sa_nintr; i++) {
146 struct openprom_intr *sbi = &sa->sa_intr[i];
147
148 aprint_normal(" vector %lx ipl %ld",
149 (u_long)sbi->oi_vec,
150 (long)INTLEV(sbi->oi_pri));
151 }
152 return (UNCONF);
153 }
154
155 int
156 sbus_match(device_t parent, cfdata_t cf, void *aux)
157 {
158 struct mainbus_attach_args *ma = aux;
159
160 return (strcmp(cf->cf_name, ma->ma_name) == 0);
161 }
162
163 /*
164 * Attach an Sbus.
165 */
166 void
167 sbus_attach(device_t parent, device_t self, void *aux)
168 {
169 struct sbus_softc *sc = device_private(self);
170 struct mainbus_attach_args *ma = aux;
171 struct intrhand *ih;
172 int ipl;
173 char *name;
174 int node = ma->ma_node;
175 int node0, error;
176 bus_space_tag_t sbt;
177 struct sbus_attach_args sa;
178
179 sc->sc_dev = self;
180 sc->sc_bustag = ma->ma_bustag;
181 sc->sc_dmatag = ma->ma_dmatag;
182 sc->sc_ign = ma->ma_interrupts[0] & INTMAP_IGN;
183
184 /* XXXX Use sysio PROM mappings for interrupt vector regs. */
185 sparc_promaddr_to_handle(sc->sc_bustag, ma->ma_address[0], &sc->sc_bh);
186 sc->sc_sysio = (struct sysioreg *)bus_space_vaddr(sc->sc_bustag,
187 sc->sc_bh);
188
189 #ifdef _LP64
190 /*
191 * 32-bit kernels use virtual addresses for bus space operations
192 * so we may as well use the prom VA.
193 *
194 * 64-bit kernels use physical addresses for bus space operations
195 * so mapping this in again will reduce TLB thrashing.
196 */
197 if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr,
198 ma->ma_reg[0].ur_len, 0, &sc->sc_bh) != 0) {
199 aprint_error_dev(self, "cannot map registers\n");
200 return;
201 }
202 #endif
203
204 /*
205 * Record clock frequency for synchronous SCSI.
206 * IS THIS THE CORRECT DEFAULT??
207 */
208 sc->sc_clockfreq = prom_getpropint(node, "clock-frequency",
209 25*1000*1000);
210 printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
211
212 sbt = bus_space_tag_alloc(sc->sc_bustag, sc);
213 sbt->type = SBUS_BUS_SPACE;
214 sbt->sparc_bus_map = _sbus_bus_map;
215 sbt->sparc_intr_establish = sbus_intr_establish;
216
217 sc->sc_dmatag = sbus_alloc_dmatag(sc);
218
219 /*
220 * Get the SBus burst transfer size if burst transfers are supported
221 */
222 sc->sc_burst = prom_getpropint(node, "burst-sizes", 0);
223
224 /*
225 * Collect address translations from the OBP.
226 */
227 error = prom_getprop(node, "ranges", sizeof(struct openprom_range),
228 &sbt->nranges, &sbt->ranges);
229 if (error)
230 panic("%s: error getting ranges property", device_xname(self));
231
232 /* initialize the IOMMU */
233
234 /* punch in our copies */
235 sc->sc_is.is_bustag = sc->sc_bustag;
236 bus_space_subregion(sc->sc_bustag, sc->sc_bh,
237 (vaddr_t)&((struct sysioreg *)NULL)->sys_iommu,
238 sizeof (struct iommureg), &sc->sc_is.is_iommu);
239
240 /* initialize our strbuf_ctl */
241 sc->sc_is.is_sb[0] = &sc->sc_sb;
242 sc->sc_sb.sb_is = &sc->sc_is;
243 bus_space_subregion(sc->sc_bustag, sc->sc_bh,
244 (vaddr_t)&((struct sysioreg *)NULL)->sys_strbuf,
245 sizeof (struct iommu_strbuf), &sc->sc_sb.sb_sb);
246 /* Point sb_flush to our flush buffer. */
247 sc->sc_sb.sb_flush = &sc->sc_flush;
248
249 /* give us a nice name.. */
250 name = kmem_asprintf("%s dvma", device_xname(self));
251
252 iommu_init(name, &sc->sc_is, 0, -1);
253
254 /* Enable the over temp intr */
255 ih = intrhand_alloc();
256 ih->ih_map = &sc->sc_sysio->therm_int_map;
257 ih->ih_clr = NULL; /* &sc->sc_sysio->therm_clr_int; */
258 ih->ih_fun = sbus_overtemp;
259 ipl = 1;
260 ih->ih_pil = ipl;
261 ih->ih_number = INTVEC(*(ih->ih_map));
262 ih->ih_pending = 0;
263 intr_establish(ipl, true, ih);
264 *(ih->ih_map) |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT);
265
266 /*
267 * Note: the stupid SBUS IOMMU ignores the high bits of an address, so a
268 * NULL DMA pointer will be translated by the first page of the IOTSB.
269 * To avoid bugs we'll alloc and ignore the first entry in the IOTSB.
270 */
271 {
272 vmem_addr_t dummy;
273
274 if (vmem_xalloc(sc->sc_is.is_dvmamap, PAGE_SIZE,
275 0, /* alignment */
276 0, /* phase */
277 0, /* nocross */
278 sc->sc_is.is_dvmabase,
279 sc->sc_is.is_dvmabase + PAGE_SIZE - 1,
280 VM_BESTFIT | VM_NOSLEEP,
281 &dummy) != 0) {
282 panic("sbus iommu: can't toss first dvma page");
283 }
284 }
285
286 /*
287 * Loop through ROM children, fixing any relative addresses
288 * and then configuring each device.
289 * `specials' is an array of device names that are treated
290 * specially:
291 */
292 devhandle_t selfh = device_handle(self);
293 node0 = OF_child(node);
294 for (node = node0; node; node = OF_peer(node)) {
295 char *name1 = prom_getpropstring(node, "name");
296
297 if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
298 node, &sa) != 0) {
299 printf("sbus_attach: %s: incomplete\n", name1);
300 continue;
301 }
302 (void) config_found(self, &sa, sbus_print,
303 CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
304 sbus_destroy_attach_args(&sa);
305 }
306 }
307
308 int
309 sbus_setup_attach_args(struct sbus_softc *sc, bus_space_tag_t bustag,
310 bus_dma_tag_t dmatag, int node, struct sbus_attach_args *sa)
311 {
312 /*struct openprom_addr sbusreg;*/
313 /*int base;*/
314 int error;
315 int n;
316
317 memset(sa, 0, sizeof(struct sbus_attach_args));
318 n = 0;
319 error = prom_getprop(node, "name", 1, &n, &sa->sa_name);
320 if (error != 0)
321 return (error);
322 KASSERT(sa->sa_name[n-1] == '\0');
323
324 sa->sa_bustag = bustag;
325 sa->sa_dmatag = dmatag;
326 sa->sa_node = node;
327 sa->sa_frequency = sc->sc_clockfreq;
328
329 error = prom_getprop(node, "reg", sizeof(struct openprom_addr),
330 &sa->sa_nreg, &sa->sa_reg);
331 if (error != 0) {
332 char buf[32];
333 if (error != ENOENT ||
334 !node_has_property(node, "device_type") ||
335 strcmp(prom_getpropstringA(node, "device_type", buf, sizeof buf),
336 "hierarchical") != 0)
337 return (error);
338 }
339 for (n = 0; n < sa->sa_nreg; n++) {
340 /* Convert to relative addressing, if necessary */
341 uint32_t base = sa->sa_reg[n].oa_base;
342 if (SBUS_ABS(base)) {
343 sa->sa_reg[n].oa_space = SBUS_ABS_TO_SLOT(base);
344 sa->sa_reg[n].oa_base = SBUS_ABS_TO_OFFSET(base);
345 }
346 }
347
348 if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr,
349 sa->sa_slot)) != 0)
350 return (error);
351
352 error = prom_getprop(node, "address", sizeof(uint32_t),
353 &sa->sa_npromvaddrs, &sa->sa_promvaddrs);
354 if (error != 0 && error != ENOENT)
355 return (error);
356
357 return (0);
358 }
359
360 void
361 sbus_destroy_attach_args(struct sbus_attach_args *sa)
362 {
363 if (sa->sa_name != NULL)
364 free(sa->sa_name, M_DEVBUF);
365
366 if (sa->sa_nreg != 0)
367 free(sa->sa_reg, M_DEVBUF);
368
369 if (sa->sa_intr)
370 free(sa->sa_intr, M_DEVBUF);
371
372 if (sa->sa_promvaddrs)
373 free((void *)sa->sa_promvaddrs, M_DEVBUF);
374
375 memset(sa, 0, sizeof(struct sbus_attach_args)); /*DEBUG*/
376 }
377
378
379 int
380 _sbus_bus_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size, int flags,
381 vaddr_t v, bus_space_handle_t *hp)
382 {
383 int error;
384
385 if (t->ranges != NULL) {
386 if ((error = bus_space_translate_address_generic(
387 t->ranges, t->nranges, &addr)) != 0)
388 return (error);
389 }
390
391 /*
392 * BUS_SPACE_MAP_PREFETCHABLE doesn't work right through sbus, so weed
393 * it out for now until we know better
394 */
395
396 flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
397
398 return (bus_space_map(t->parent, addr, size, flags, hp));
399 }
400
401
402 bus_addr_t
403 sbus_bus_addr(bus_space_tag_t t, u_int btype, u_int offset)
404 {
405 int slot = btype;
406 struct openprom_range *rp;
407 int i;
408
409 for (i = 0; i < t->nranges; i++) {
410 rp = &t->ranges[i];
411 if (rp->or_child_space != slot)
412 continue;
413
414 return BUS_ADDR(rp->or_parent_space,
415 rp->or_parent_base + offset);
416 }
417
418 return (0);
419 }
420
421
422 /*
423 * Handle an overtemp situation.
424 *
425 * SPARCs have temperature sensors which generate interrupts
426 * if the machine's temperature exceeds a certain threshold.
427 * This handles the interrupt and powers off the machine.
428 * The same needs to be done to PCI controller drivers.
429 */
430 int
431 sbus_overtemp(void *arg)
432 {
433 /* Should try a clean shutdown first */
434 printf("DANGER: OVER TEMPERATURE detected\nShutting down...\n");
435 delay(20);
436 kern_reboot(RB_POWERDOWN|RB_HALT, NULL);
437 }
438
439 /*
440 * Get interrupt attributes for an Sbus device.
441 */
442 int
443 sbus_get_intr(struct sbus_softc *sc, int node, struct openprom_intr **ipp,
444 int *np, int slot)
445 {
446 int *ipl;
447 int n, i;
448 char buf[32];
449
450 /*
451 * The `interrupts' property contains the Sbus interrupt level.
452 */
453 ipl = NULL;
454 if (prom_getprop(node, "interrupts", sizeof(int), np, &ipl) == 0) {
455 struct openprom_intr *ip;
456 int pri;
457
458 /* Default to interrupt level 2 -- otherwise unused */
459 pri = INTLEVENCODE(2);
460
461 /* Change format to an `struct sbus_intr' array */
462 ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
463 M_WAITOK);
464
465 /*
466 * Now things get ugly. We need to take this value which is
467 * the interrupt vector number and encode the IPL into it
468 * somehow. Luckily, the interrupt vector has lots of free
469 * space and we can easily stuff the IPL in there for a while.
470 */
471 prom_getpropstringA(node, "device_type", buf, sizeof buf);
472 if (buf[0] == '\0')
473 prom_getpropstringA(node, "name", buf, sizeof buf);
474
475 for (i = 0; intrmap[i].in_class; i++)
476 if (strcmp(intrmap[i].in_class, buf) == 0) {
477 pri = INTLEVENCODE(intrmap[i].in_lev);
478 break;
479 }
480
481 /*
482 * Sbus card devices need the slot number encoded into
483 * the vector as this is generally not done.
484 */
485 if ((ipl[0] & INTMAP_OBIO) == 0)
486 pri |= slot << 3;
487
488 for (n = 0; n < *np; n++) {
489 /*
490 * We encode vector and priority into sbi_pri so we
491 * can pass them as a unit. This will go away if
492 * sbus_establish ever takes an sbus_intr instead
493 * of an integer level.
494 * Stuff the real vector in sbi_vec.
495 */
496
497 ip[n].oi_pri = pri|ipl[n];
498 ip[n].oi_vec = ipl[n];
499 }
500 free(ipl, M_DEVBUF);
501 *ipp = ip;
502 }
503
504 return (0);
505 }
506
507
508 /*
509 * Install an interrupt handler for an Sbus device.
510 */
511 void *
512 sbus_intr_establish(bus_space_tag_t t, int pri, int level,
513 int (*handler)(void *), void *arg, void (*fastvec)(void))
514 {
515 struct sbus_softc *sc = t->cookie;
516 struct intrhand *ih;
517 int ipl;
518 long vec = pri;
519
520 ih = intrhand_alloc();
521
522 if ((vec & SBUS_INTR_COMPAT) != 0)
523 ipl = vec & ~SBUS_INTR_COMPAT;
524 else {
525 /* Decode and remove IPL */
526 ipl = INTLEV(vec);
527 vec = INTVEC(vec);
528 DPRINTF(SDB_INTR,
529 ("\nsbus: intr[%ld]%lx: %lx\nHunting for IRQ...\n",
530 (long)ipl, (long)vec, (u_long)intrlev[vec]));
531 if ((vec & INTMAP_OBIO) == 0) {
532 /* We're in an SBUS slot */
533 /* Register the map and clear intr registers */
534
535 int slot = INTSLOT(pri);
536
537 ih->ih_map = &(&sc->sc_sysio->sbus_slot0_int)[slot];
538 ih->ih_clr = &sc->sc_sysio->sbus0_clr_int[vec];
539 #ifdef DEBUG
540 if (sbus_debug & SDB_INTR) {
541 int64_t imap = *ih->ih_map;
542
543 printf("SBUS %lx IRQ as %llx in slot %d\n",
544 (long)vec, (long long)imap, slot);
545 printf("\tmap addr %p clr addr %p\n",
546 ih->ih_map, ih->ih_clr);
547 }
548 #endif
549 /* Enable the interrupt */
550 vec |= INTMAP_V | sc->sc_ign |
551 (CPU_UPAID << INTMAP_TID_SHIFT);
552 *(ih->ih_map) = vec;
553 } else {
554 int64_t *intrptr = &sc->sc_sysio->scsi_int_map;
555 int64_t imap = 0;
556 int i;
557
558 /* Insert IGN */
559 vec |= sc->sc_ign;
560 for (i = 0; &intrptr[i] <=
561 (int64_t *)&sc->sc_sysio->reserved_int_map &&
562 INTVEC(imap = intrptr[i]) != INTVEC(vec); i++)
563 ;
564 if (INTVEC(imap) == INTVEC(vec)) {
565 DPRINTF(SDB_INTR,
566 ("OBIO %lx IRQ as %lx in slot %d\n",
567 vec, (long)imap, i));
568 /* Register the map and clear intr registers */
569 ih->ih_map = &intrptr[i];
570 intrptr = (int64_t *)&sc->sc_sysio->scsi_clr_int;
571 ih->ih_clr = &intrptr[i];
572 /* Enable the interrupt */
573 imap |= INTMAP_V
574 |(CPU_UPAID << INTMAP_TID_SHIFT);
575 /* XXXX */
576 *(ih->ih_map) = imap;
577 } else
578 panic("IRQ not found!");
579 }
580 }
581 #ifdef DEBUG
582 if (sbus_debug & SDB_INTR) { long i; for (i = 0; i < 400000000; i++); }
583 #endif
584
585 ih->ih_fun = handler;
586 ih->ih_arg = arg;
587 ih->ih_number = vec;
588 ih->ih_ivec = 0;
589 ih->ih_pil = ipl;
590 ih->ih_pending = 0;
591
592 intr_establish(ipl, level != IPL_VM, ih);
593 return (ih);
594 }
595
596 static bus_dma_tag_t
597 sbus_alloc_dmatag(struct sbus_softc *sc)
598 {
599 bus_dma_tag_t sdt, psdt = sc->sc_dmatag;
600
601 sdt = kmem_alloc(sizeof(*sdt), KM_SLEEP);
602 sdt->_cookie = sc;
603 sdt->_parent = psdt;
604 #define PCOPY(x) sdt->x = psdt->x
605 sdt->_dmamap_create = sbus_dmamap_create;
606 PCOPY(_dmamap_destroy);
607 sdt->_dmamap_load = iommu_dvmamap_load;
608 PCOPY(_dmamap_load_mbuf);
609 PCOPY(_dmamap_load_uio);
610 sdt->_dmamap_load_raw = iommu_dvmamap_load_raw;
611 sdt->_dmamap_unload = iommu_dvmamap_unload;
612 sdt->_dmamap_sync = iommu_dvmamap_sync;
613 sdt->_dmamem_alloc = iommu_dvmamem_alloc;
614 sdt->_dmamem_free = iommu_dvmamem_free;
615 sdt->_dmamem_map = iommu_dvmamem_map;
616 sdt->_dmamem_unmap = iommu_dvmamem_unmap;
617 PCOPY(_dmamem_mmap);
618 #undef PCOPY
619 sc->sc_dmatag = sdt;
620 return (sdt);
621 }
622
623 static int
624 sbus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
625 bus_size_t maxsegsz, bus_size_t boundary, int flags,
626 bus_dmamap_t *dmamp)
627 {
628 struct sbus_softc *sc = t->_cookie;
629 int error;
630
631 error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
632 boundary, flags, dmamp);
633 if (error == 0)
634 (*dmamp)->_dm_cookie = &sc->sc_sb;
635 return error;
636 }
637