1 /* $NetBSD: psycho.c,v 1.123 2015/11/27 00:36:58 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1999, 2000 Matthew R. Green 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * Copyright (c) 2001, 2002 Eduardo E. Horvath 31 * All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. The name of the author may not be used to endorse or promote products 42 * derived from this software without specific prior written permission. 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 47 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 49 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 50 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 51 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54 * SUCH DAMAGE. 55 */ 56 57 #include <sys/cdefs.h> 58 __KERNEL_RCSID(0, "$NetBSD: psycho.c,v 1.123 2015/11/27 00:36:58 mrg Exp $"); 59 60 #include "opt_ddb.h" 61 62 /* 63 * Support for `psycho' and `psycho+' UPA to PCI bridge and 64 * UltraSPARC IIi and IIe `sabre' PCI controllers. 65 */ 66 67 #ifdef DEBUG 68 #define PDB_PROM 0x01 69 #define PDB_BUSMAP 0x02 70 #define PDB_INTR 0x04 71 #define PDB_INTMAP 0x08 72 #define PDB_CONF 0x10 73 #define PDB_STICK 0x20 74 int psycho_debug = 0x0; 75 #define DPRINTF(l, s) do { if (psycho_debug & l) printf s; } while (0) 76 #else 77 #define DPRINTF(l, s) 78 #endif 79 80 #include <sys/param.h> 81 #include <sys/device.h> 82 #include <sys/errno.h> 83 #include <sys/extent.h> 84 #include <sys/malloc.h> 85 #include <sys/systm.h> 86 #include <sys/time.h> 87 #include <sys/reboot.h> 88 89 #include <uvm/uvm.h> 90 91 #define _SPARC_BUS_DMA_PRIVATE 92 #include <sys/bus.h> 93 #include <machine/autoconf.h> 94 #include <machine/psl.h> 95 96 #include <dev/pci/pcivar.h> 97 #include <dev/pci/pcireg.h> 98 #include <dev/sysmon/sysmon_taskq.h> 99 100 #include <sparc64/dev/iommureg.h> 101 #include <sparc64/dev/iommuvar.h> 102 #include <sparc64/dev/psychoreg.h> 103 #include <sparc64/dev/psychovar.h> 104 105 #include "ioconf.h" 106 107 static pci_chipset_tag_t psycho_alloc_chipset(struct psycho_pbm *, int, 108 pci_chipset_tag_t); 109 static struct extent *psycho_alloc_extent(struct psycho_pbm *, int, int, 110 const char *); 111 static void psycho_get_bus_range(int, int *); 112 static void psycho_fixup_bus_range(int, int *); 113 static void psycho_get_ranges(int, struct psycho_ranges **, int *); 114 static void psycho_set_intr(struct psycho_softc *, int, void *, uint64_t *, 115 uint64_t *); 116 117 /* chipset handlers */ 118 static pcireg_t psycho_pci_conf_read(pci_chipset_tag_t, pcitag_t, int); 119 static void psycho_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, 120 pcireg_t); 121 static void *psycho_pci_intr_establish(pci_chipset_tag_t, 122 pci_intr_handle_t, 123 int, int (*)(void *), void *); 124 static int psycho_pci_find_ino(const struct pci_attach_args *, 125 pci_intr_handle_t *); 126 127 /* Interrupt handlers */ 128 static int psycho_ue(void *); 129 static int psycho_ce(void *); 130 static int psycho_bus_a(void *); 131 static int psycho_bus_b(void *); 132 static int psycho_powerfail(void *); 133 static int psycho_wakeup(void *); 134 135 136 /* IOMMU support */ 137 static void psycho_iommu_init(struct psycho_softc *, int); 138 139 /* 140 * bus space and bus DMA support for UltraSPARC `psycho'. note that most 141 * of the bus DMA support is provided by the iommu dvma controller. 142 */ 143 static struct psycho_ranges *get_psychorange(struct psycho_pbm *, int); 144 145 static paddr_t psycho_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int); 146 static int _psycho_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int, 147 vaddr_t, bus_space_handle_t *); 148 static void *psycho_intr_establish(bus_space_tag_t, int, int, int (*)(void *), 149 void *, void(*)(void)); 150 151 static int psycho_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t, 152 bus_size_t, int, bus_dmamap_t *); 153 static void psycho_sabre_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t, 154 bus_size_t, int); 155 156 /* base pci_chipset */ 157 extern struct sparc_pci_chipset _sparc_pci_chipset; 158 159 /* power button handlers */ 160 static void psycho_register_power_button(struct psycho_softc *sc); 161 static void psycho_power_button_pressed(void *arg); 162 163 /* 164 * autoconfiguration 165 */ 166 static int psycho_match(device_t, cfdata_t, void *); 167 static void psycho_attach(device_t, device_t, void *); 168 static int psycho_print(void *aux, const char *p); 169 170 CFATTACH_DECL_NEW(psycho, sizeof(struct psycho_softc), 171 psycho_match, psycho_attach, NULL, NULL); 172 173 /* 174 * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge. It manages a 175 * single PCI bus and does not have a streaming buffer. It often has an APB 176 * (advanced PCI bridge) connected to it, which was designed specifically for 177 * the IIi. The APB let's the IIi handle two independednt PCI buses, and 178 * appears as two "simba"'s underneath the sabre. 179 * 180 * "psycho" and "psycho+" is a dual UPA to PCI bridge. It sits on the UPA bus 181 * and manages two PCI buses. "psycho" has two 64-bit 33 MHz buses, while 182 * "psycho+" controls both a 64-bit 33 MHz and a 64-bit 66 MHz PCI bus. You 183 * will usually find a "psycho+" since I don't think the original "psycho" 184 * ever shipped, and if it did it would be in the U30. 185 * 186 * Each "psycho" PCI bus appears as a separate OFW node, but since they are 187 * both part of the same IC, they only have a single register space. As such, 188 * they need to be configured together, even though the autoconfiguration will 189 * attach them separately. 190 * 191 * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often 192 * as pci1 and pci2, although they have been implemented with other PCI bus 193 * numbers on some machines. 194 * 195 * On UltraII machines, there can be any number of "psycho+" ICs, each 196 * providing two PCI buses. 197 * 198 * 199 * XXXX The psycho/sabre node has an `interrupts' attribute. They contain 200 * the values of the following interrupts in this order: 201 * 202 * PCI Bus Error (30) 203 * DMA UE (2e) 204 * DMA CE (2f) 205 * Power Fail (25) 206 * 207 * We really should attach handlers for each. 208 * 209 */ 210 211 #define ROM_PCI_NAME "pci" 212 213 struct psycho_names { 214 const char *p_name; 215 int p_type; 216 } psycho_names[] = { 217 { "SUNW,psycho", PSYCHO_MODE_PSYCHO }, 218 { "pci108e,8000", PSYCHO_MODE_PSYCHO }, 219 { "SUNW,sabre", PSYCHO_MODE_SABRE }, 220 { "pci108e,a000", PSYCHO_MODE_SABRE }, 221 { "pci108e,a001", PSYCHO_MODE_SABRE }, 222 { NULL, 0 } 223 }; 224 225 struct psycho_softc *psycho0 = NULL; 226 227 static int 228 psycho_match(device_t parent, cfdata_t match, void *aux) 229 { 230 struct mainbus_attach_args *ma = aux; 231 char *model = prom_getpropstring(ma->ma_node, "model"); 232 int i; 233 234 /* match on a name of "pci" and a sabre or a psycho */ 235 if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) { 236 for (i=0; psycho_names[i].p_name; i++) 237 if (strcmp(model, psycho_names[i].p_name) == 0) 238 return (1); 239 240 model = prom_getpropstring(ma->ma_node, "compatible"); 241 for (i=0; psycho_names[i].p_name; i++) 242 if (strcmp(model, psycho_names[i].p_name) == 0) 243 return (1); 244 } 245 return (0); 246 } 247 248 #ifdef DEBUG 249 static void psycho_dump_intmap(struct psycho_softc *sc); 250 static void 251 psycho_dump_intmap(struct psycho_softc *sc) 252 { 253 volatile uint64_t *intrmapptr = NULL; 254 255 printf("psycho_dump_intmap: OBIO\n"); 256 257 for (intrmapptr = &sc->sc_regs->scsi_int_map; 258 intrmapptr < &sc->sc_regs->ue_int_map; 259 intrmapptr++) 260 printf("%p: %llx\n", intrmapptr, 261 (unsigned long long)*intrmapptr); 262 263 printf("\tintmap:pci\n"); 264 for (intrmapptr = &sc->sc_regs->pcia_slot0_int; 265 intrmapptr <= &sc->sc_regs->pcib_slot3_int; 266 intrmapptr++) 267 printf("%p: %llx\n", intrmapptr, 268 (unsigned long long)*intrmapptr); 269 270 printf("\tintmap:ffb\n"); 271 for (intrmapptr = &sc->sc_regs->ffb0_int_map; 272 intrmapptr <= &sc->sc_regs->ffb1_int_map; 273 intrmapptr++) 274 printf("%p: %llx\n", intrmapptr, 275 (unsigned long long)*intrmapptr); 276 } 277 #endif 278 279 /* 280 * SUNW,psycho initialisation .. 281 * - find the per-psycho registers 282 * - figure out the IGN. 283 * - find our partner psycho 284 * - configure ourselves 285 * - bus range, bus, 286 * - get interrupt-map and interrupt-map-mask 287 * - setup the chipsets. 288 * - if we're the first of the pair, initialise the IOMMU, otherwise 289 * just copy its tags and addresses. 290 */ 291 static void 292 psycho_attach(device_t parent, device_t self, void *aux) 293 { 294 struct psycho_softc *sc = device_private(self); 295 struct psycho_softc *osc = NULL; 296 struct psycho_pbm *pp; 297 struct pcibus_attach_args pba; 298 struct mainbus_attach_args *ma = aux; 299 struct psycho_ranges *pr; 300 prop_dictionary_t dict; 301 bus_space_handle_t bh; 302 uint64_t csr, mem_base; 303 int psycho_br[2], n, i; 304 bus_space_handle_t pci_ctl; 305 char *model = prom_getpropstring(ma->ma_node, "model"); 306 307 aprint_normal("\n"); 308 309 sc->sc_dev = self; 310 sc->sc_node = ma->ma_node; 311 sc->sc_bustag = ma->ma_bustag; 312 sc->sc_dmatag = ma->ma_dmatag; 313 sc->sc_last_stick = 0; 314 315 if (psycho0 == NULL) 316 psycho0 = sc; 317 DPRINTF(PDB_STICK, ("init psycho0 %lx\n", (long)sc)); 318 /* 319 * Identify the device. 320 */ 321 for (i=0; psycho_names[i].p_name; i++) 322 if (strcmp(model, psycho_names[i].p_name) == 0) { 323 sc->sc_mode = psycho_names[i].p_type; 324 goto found; 325 } 326 327 model = prom_getpropstring(ma->ma_node, "compatible"); 328 for (i=0; psycho_names[i].p_name; i++) 329 if (strcmp(model, psycho_names[i].p_name) == 0) { 330 sc->sc_mode = psycho_names[i].p_type; 331 goto found; 332 } 333 334 panic("unknown psycho model %s", model); 335 found: 336 337 /* 338 * The psycho gets three register banks: 339 * (0) per-PBM configuration and status registers 340 * (1) per-PBM PCI configuration space, containing only the 341 * PBM 256-byte PCI header 342 * (2) the shared psycho configuration registers (struct psychoreg) 343 */ 344 345 /* Register layouts are different. stuupid. */ 346 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) { 347 sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr; 348 349 if (ma->ma_naddress > 2) { 350 sparc_promaddr_to_handle(sc->sc_bustag, 351 ma->ma_address[2], &sc->sc_bh); 352 sparc_promaddr_to_handle(sc->sc_bustag, 353 ma->ma_address[0], &pci_ctl); 354 355 sc->sc_regs = (struct psychoreg *) 356 bus_space_vaddr(sc->sc_bustag, sc->sc_bh); 357 } else if (ma->ma_nreg > 2) { 358 359 /* We need to map this in ourselves. */ 360 if (bus_space_map(sc->sc_bustag, 361 ma->ma_reg[2].ur_paddr, 362 ma->ma_reg[2].ur_len, BUS_SPACE_MAP_LINEAR, 363 &sc->sc_bh)) 364 panic("psycho_attach: cannot map regs"); 365 sc->sc_regs = (struct psychoreg *) 366 bus_space_vaddr(sc->sc_bustag, sc->sc_bh); 367 368 if (bus_space_map(sc->sc_bustag, 369 ma->ma_reg[0].ur_paddr, 370 ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR, 371 &pci_ctl)) 372 panic("psycho_attach: cannot map ctl"); 373 } else 374 panic("psycho_attach: %d not enough registers", 375 ma->ma_nreg); 376 377 } else { 378 sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr; 379 380 if (ma->ma_naddress) { 381 sparc_promaddr_to_handle(sc->sc_bustag, 382 ma->ma_address[0], &sc->sc_bh); 383 sc->sc_regs = (struct psychoreg *) 384 bus_space_vaddr(sc->sc_bustag, sc->sc_bh); 385 386 bus_space_subregion(sc->sc_bustag, sc->sc_bh, 387 offsetof(struct psychoreg, psy_pcictl), 388 sizeof(struct pci_ctl), &pci_ctl); 389 } else if (ma->ma_nreg) { 390 391 /* We need to map this in ourselves. */ 392 if (bus_space_map(sc->sc_bustag, 393 ma->ma_reg[0].ur_paddr, 394 ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR, 395 &sc->sc_bh)) 396 panic("psycho_attach: cannot map regs"); 397 sc->sc_regs = (struct psychoreg *) 398 bus_space_vaddr(sc->sc_bustag, sc->sc_bh); 399 400 bus_space_subregion(sc->sc_bustag, sc->sc_bh, 401 offsetof(struct psychoreg, psy_pcictl), 402 sizeof(struct pci_ctl), &pci_ctl); 403 } else 404 panic("psycho_attach: %d not enough registers", 405 ma->ma_nreg); 406 } 407 408 csr = bus_space_read_8(sc->sc_bustag, sc->sc_bh, 409 offsetof(struct psychoreg, psy_csr)); 410 sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */ 411 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) 412 sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6; 413 414 aprint_normal_dev(self, "%s: impl %d, version %d: ign %x ", 415 model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr), 416 sc->sc_ign); 417 /* 418 * Match other psycho's that are already configured against 419 * the base physical address. This will be the same for a 420 * pair of devices that share register space. 421 */ 422 for (n = 0; n < psycho_cd.cd_ndevs; n++) { 423 424 struct psycho_softc *asc = device_lookup_private(&psycho_cd, n); 425 426 if (asc == NULL || asc == sc) 427 /* This entry is not there or it is me */ 428 continue; 429 430 if (asc->sc_basepaddr != sc->sc_basepaddr) 431 /* This is an unrelated psycho */ 432 continue; 433 434 /* Found partner */ 435 osc = asc; 436 break; 437 } 438 439 440 /* Oh, dear. OK, lets get started */ 441 442 /* 443 * Setup the PCI control register 444 */ 445 csr = bus_space_read_8(sc->sc_bustag, pci_ctl, 446 offsetof(struct pci_ctl, pci_csr)); 447 csr |= PCICTL_MRLM | 448 PCICTL_ARB_PARK | 449 PCICTL_ERRINTEN | 450 PCICTL_4ENABLE; 451 csr &= ~(PCICTL_SERR | 452 PCICTL_CPU_PRIO | 453 PCICTL_ARB_PRIO | 454 PCICTL_RTRYWAIT); 455 bus_space_write_8(sc->sc_bustag, pci_ctl, 456 offsetof(struct pci_ctl, pci_csr), csr); 457 458 459 /* 460 * Allocate our psycho_pbm 461 */ 462 pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, 463 M_NOWAIT | M_ZERO); 464 if (pp == NULL) 465 panic("could not allocate psycho pbm"); 466 467 pp->pp_sc = sc; 468 469 /* grab the psycho ranges */ 470 psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange); 471 472 /* get the bus-range for the psycho */ 473 psycho_get_bus_range(sc->sc_node, psycho_br); 474 475 pba.pba_bus = psycho_br[0]; 476 pba.pba_bridgetag = NULL; 477 478 /* Fix up invalid 0x00-0xff bus-range, as found on SPARCle */ 479 if (psycho_br[0] == 0 && psycho_br[1] == 0xff) 480 psycho_fixup_bus_range(sc->sc_node, psycho_br); 481 482 aprint_normal("bus range %u to %u", psycho_br[0], psycho_br[1]); 483 aprint_normal("; PCI bus %d", psycho_br[0]); 484 485 pp->pp_pcictl = pci_ctl; 486 487 /* allocate our tags */ 488 pp->pp_memt = psycho_alloc_mem_tag(pp); 489 pp->pp_iot = psycho_alloc_io_tag(pp); 490 pp->pp_dmat = psycho_alloc_dma_tag(pp); 491 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_OKAY : 0) | 492 (pp->pp_iot ? PCI_FLAGS_IO_OKAY : 0); 493 494 /* allocate a chipset for this */ 495 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset); 496 pp->pp_pc->spc_busmax = psycho_br[1]; 497 498 switch((ma->ma_reg[0].ur_paddr) & 0xf000) { 499 case 0x2000: 500 pp->pp_id = PSYCHO_PBM_A; 501 break; 502 case 0x4000: 503 pp->pp_id = PSYCHO_PBM_B; 504 break; 505 } 506 507 aprint_normal("\n"); 508 509 /* allocate extents for free bus space */ 510 pp->pp_exmem = psycho_alloc_extent(pp, sc->sc_node, 0x02, "psycho mem"); 511 pp->pp_exio = psycho_alloc_extent(pp, sc->sc_node, 0x01, "psycho io"); 512 513 #ifdef DEBUG 514 if (psycho_debug & PDB_INTR) 515 psycho_dump_intmap(sc); 516 #endif 517 518 /* 519 * And finally, if we're a sabre or the first of a pair of psycho's to 520 * arrive here, start up the IOMMU and get a config space tag. 521 */ 522 if (osc == NULL) { 523 uint64_t timeo; 524 525 /* 526 * Establish handlers for interesting interrupts.... 527 * 528 * XXX We need to remember these and remove this to support 529 * hotplug on the UPA/FHC bus. 530 * 531 * XXX Not all controllers have these, but installing them 532 * is better than trying to sort through this mess. 533 */ 534 psycho_set_intr(sc, 15, psycho_ue, 535 &sc->sc_regs->ue_int_map, 536 &sc->sc_regs->ue_clr_int); 537 psycho_set_intr(sc, 1, psycho_ce, 538 &sc->sc_regs->ce_int_map, 539 &sc->sc_regs->ce_clr_int); 540 psycho_set_intr(sc, 15, psycho_bus_a, 541 &sc->sc_regs->pciaerr_int_map, 542 &sc->sc_regs->pciaerr_clr_int); 543 /* 544 * Netra X1 may hang when the powerfail interrupt is enabled. 545 */ 546 if (strcmp(machine_model, "SUNW,UltraAX-i2") != 0) { 547 psycho_set_intr(sc, 15, psycho_powerfail, 548 &sc->sc_regs->power_int_map, 549 &sc->sc_regs->power_clr_int); 550 psycho_register_power_button(sc); 551 } 552 if (sc->sc_mode != PSYCHO_MODE_SABRE) { 553 /* sabre doesn't have these interrupts */ 554 psycho_set_intr(sc, 15, psycho_bus_b, 555 &sc->sc_regs->pciberr_int_map, 556 &sc->sc_regs->pciberr_clr_int); 557 psycho_set_intr(sc, 1, psycho_wakeup, 558 &sc->sc_regs->pwrmgt_int_map, 559 &sc->sc_regs->pwrmgt_clr_int); 560 } 561 562 /* 563 * Apparently a number of machines with psycho and psycho+ 564 * controllers have interrupt latency issues. We'll try 565 * setting the interrupt retry timeout to 0xff which gives us 566 * a retry of 3-6 usec (which is what sysio is set to) for the 567 * moment, which seems to help alleviate this problem. 568 */ 569 timeo = sc->sc_regs->intr_retry_timer; 570 if (timeo > 0xfff) { 571 #ifdef DEBUG 572 printf("decreasing interrupt retry timeout " 573 "from %lx to 0xff\n", (long)timeo); 574 #endif 575 sc->sc_regs->intr_retry_timer = 0xff; 576 } 577 578 /* 579 * Allocate bus node, this contains a prom node per bus. 580 */ 581 pp->pp_pc->spc_busnode = 582 malloc(sizeof(*pp->pp_pc->spc_busnode), M_DEVBUF, 583 M_NOWAIT | M_ZERO); 584 if (pp->pp_pc->spc_busnode == NULL) 585 panic("psycho_attach: malloc busnode"); 586 587 /* 588 * Setup IOMMU and PCI configuration if we're the first 589 * of a pair of psycho's to arrive here. 590 * 591 * We should calculate a TSB size based on amount of RAM 592 * and number of bus controllers and number an type of 593 * child devices. 594 * 595 * For the moment, 32KB should be more than enough. 596 */ 597 sc->sc_is = malloc(sizeof(struct iommu_state), 598 M_DEVBUF, M_NOWAIT); 599 if (sc->sc_is == NULL) 600 panic("psycho_attach: malloc iommu_state"); 601 602 /* Point the strbuf_ctl at the iommu_state */ 603 pp->pp_sb.sb_is = sc->sc_is; 604 605 sc->sc_is->is_sb[0] = sc->sc_is->is_sb[1] = NULL; 606 if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) { 607 struct strbuf_ctl *sb = &pp->pp_sb; 608 vaddr_t va = (vaddr_t)&pp->pp_flush[0x40]; 609 610 /* 611 * Initialize the strbuf_ctl. 612 * 613 * The flush sync buffer must be 64-byte aligned. 614 */ 615 sb->sb_flush = (void *)(va & ~0x3f); 616 617 bus_space_subregion(sc->sc_bustag, pci_ctl, 618 offsetof(struct pci_ctl, pci_strbuf), 619 sizeof (struct iommu_strbuf), &sb->sb_sb); 620 621 /* Point our iommu at the strbuf_ctl */ 622 sc->sc_is->is_sb[0] = sb; 623 } 624 625 psycho_iommu_init(sc, 2); 626 627 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this); 628 629 /* 630 * XXX This is a really ugly hack because PCI config space 631 * is explicitly handled with unmapped accesses. 632 */ 633 i = sc->sc_bustag->type; 634 sc->sc_bustag->type = PCI_CONFIG_BUS_SPACE; 635 if (bus_space_map(sc->sc_bustag, sc->sc_basepaddr + 0x01000000, 636 0x01000000, 0, &bh)) 637 panic("could not map psycho PCI configuration space"); 638 sc->sc_bustag->type = i; 639 sc->sc_configaddr = bh; 640 } else { 641 /* Share bus numbers with the pair of mine */ 642 pp->pp_pc->spc_busnode = 643 osc->sc_psycho_this->pp_pc->spc_busnode; 644 645 /* Just copy IOMMU state, config tag and address */ 646 sc->sc_is = osc->sc_is; 647 sc->sc_configtag = osc->sc_configtag; 648 sc->sc_configaddr = osc->sc_configaddr; 649 650 /* Point the strbuf_ctl at the iommu_state */ 651 pp->pp_sb.sb_is = sc->sc_is; 652 653 if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) { 654 struct strbuf_ctl *sb = &pp->pp_sb; 655 vaddr_t va = (vaddr_t)&pp->pp_flush[0x40]; 656 657 /* 658 * Initialize the strbuf_ctl. 659 * 660 * The flush sync buffer must be 64-byte aligned. 661 */ 662 sb->sb_flush = (void *)(va & ~0x3f); 663 664 bus_space_subregion(sc->sc_bustag, pci_ctl, 665 offsetof(struct pci_ctl, pci_strbuf), 666 sizeof (struct iommu_strbuf), &sb->sb_sb); 667 668 /* Point our iommu at the strbuf_ctl */ 669 sc->sc_is->is_sb[1] = sb; 670 } 671 iommu_reset(sc->sc_is); 672 } 673 674 dict = device_properties(self); 675 pr = get_psychorange(pp, 2); /* memory range */ 676 #ifdef DEBUG 677 printf("memory range: %08x %08x\n", pr->phys_hi, pr->phys_lo); 678 #endif 679 mem_base = ((uint64_t)pr->phys_hi) << 32 | pr->phys_lo; 680 prop_dictionary_set_uint64(dict, "mem_base", mem_base); 681 682 /* 683 * attach the pci.. note we pass PCI A tags, etc., for the sabre here. 684 */ 685 pba.pba_flags = sc->sc_psycho_this->pp_flags; 686 pba.pba_dmat = sc->sc_psycho_this->pp_dmat; 687 pba.pba_dmat64 = NULL; 688 pba.pba_iot = sc->sc_psycho_this->pp_iot; 689 pba.pba_memt = sc->sc_psycho_this->pp_memt; 690 pba.pba_pc = pp->pp_pc; 691 692 config_found_ia(self, "pcibus", &pba, psycho_print); 693 } 694 695 static int 696 psycho_print(void *aux, const char *p) 697 { 698 699 if (p == NULL) 700 return (UNCONF); 701 return (QUIET); 702 } 703 704 static void 705 psycho_set_intr(struct psycho_softc *sc, int ipl, void *handler, 706 uint64_t *mapper, uint64_t *clearer) 707 { 708 struct intrhand *ih; 709 710 ih = (struct intrhand *)malloc(sizeof(struct intrhand), 711 M_DEVBUF, M_NOWAIT); 712 ih->ih_arg = sc; 713 ih->ih_map = mapper; 714 ih->ih_clr = clearer; 715 ih->ih_fun = handler; 716 ih->ih_pil = ipl; 717 ih->ih_number = INTVEC(*(ih->ih_map)); 718 ih->ih_pending = 0; 719 intr_establish(ipl, ipl != IPL_VM, ih); 720 *(ih->ih_map) |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT); 721 } 722 723 /* 724 * power button handlers 725 */ 726 static void 727 psycho_register_power_button(struct psycho_softc *sc) 728 { 729 sysmon_task_queue_init(); 730 731 sc->sc_powerpressed = 0; 732 sc->sc_smcontext = malloc(sizeof(struct sysmon_pswitch), M_DEVBUF, 0); 733 if (!sc->sc_smcontext) { 734 aprint_error_dev(sc->sc_dev, "could not allocate power button context\n"); 735 return; 736 } 737 memset(sc->sc_smcontext, 0, sizeof(struct sysmon_pswitch)); 738 sc->sc_smcontext->smpsw_name = device_xname(sc->sc_dev); 739 sc->sc_smcontext->smpsw_type = PSWITCH_TYPE_POWER; 740 if (sysmon_pswitch_register(sc->sc_smcontext) != 0) 741 aprint_error_dev(sc->sc_dev, "unable to register power button with sysmon\n"); 742 } 743 744 static void 745 psycho_power_button_pressed(void *arg) 746 { 747 struct psycho_softc *sc = arg; 748 749 sysmon_pswitch_event(sc->sc_smcontext, PSWITCH_EVENT_PRESSED); 750 sc->sc_powerpressed = 0; 751 } 752 753 /* 754 * PCI bus support 755 */ 756 757 /* 758 * allocate a PCI chipset tag and set its cookie. 759 */ 760 static pci_chipset_tag_t 761 psycho_alloc_chipset(struct psycho_pbm *pp, int node, pci_chipset_tag_t pc) 762 { 763 pci_chipset_tag_t npc; 764 765 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT); 766 if (npc == NULL) 767 panic("could not allocate pci_chipset_tag_t"); 768 memcpy(npc, pc, sizeof *pc); 769 npc->cookie = pp; 770 npc->rootnode = node; 771 npc->spc_conf_read = psycho_pci_conf_read; 772 npc->spc_conf_write = psycho_pci_conf_write; 773 npc->spc_intr_map = NULL; 774 npc->spc_intr_establish = psycho_pci_intr_establish; 775 npc->spc_find_ino = psycho_pci_find_ino; 776 777 return (npc); 778 } 779 780 /* 781 * create extent for free bus space, then allocate assigned regions. 782 */ 783 static struct extent * 784 psycho_alloc_extent(struct psycho_pbm *pp, int node, int ss, const char *name) 785 { 786 struct psycho_registers *pa = NULL; 787 struct psycho_ranges *pr; 788 struct extent *ex; 789 bus_addr_t baddr, addr; 790 bus_size_t bsize, size; 791 int i, num; 792 793 /* get bus space size */ 794 pr = get_psychorange(pp, ss); 795 if (pr == NULL) { 796 printf("psycho_alloc_extent: get_psychorange failed\n"); 797 return NULL; 798 } 799 baddr = 0x00000000; 800 bsize = BUS_ADDR(pr->size_hi, pr->size_lo); 801 802 /* get available lists */ 803 num = 0; 804 if (prom_getprop(node, "available", sizeof(*pa), &num, &pa)) { 805 printf("psycho_alloc_extent: no \"available\" property\n"); 806 return NULL; 807 } 808 809 /* create extent */ 810 ex = extent_create(name, baddr, bsize - baddr - 1, 0, 0, EX_NOWAIT); 811 if (ex == NULL) { 812 printf("psycho_alloc_extent: extent_create failed\n"); 813 goto ret; 814 } 815 816 /* allocate assigned regions */ 817 for (i = 0; i < num; i++) 818 if (((pa[i].phys_hi >> 24) & 0x03) == ss) { 819 /* allocate bus space */ 820 addr = BUS_ADDR(pa[i].phys_mid, pa[i].phys_lo); 821 size = BUS_ADDR(pa[i].size_hi, pa[i].size_lo); 822 if (extent_alloc_region(ex, baddr, addr - baddr, 823 EX_NOWAIT)) { 824 printf("psycho_alloc_extent: " 825 "extent_alloc_region %" PRIx64 "-%" 826 PRIx64 " failed\n", baddr, addr); 827 extent_destroy(ex); 828 ex = NULL; 829 goto ret; 830 } 831 baddr = addr + size; 832 } 833 /* allocate left region if available */ 834 if (baddr < bsize) 835 if (extent_alloc_region(ex, baddr, bsize - baddr, EX_NOWAIT)) { 836 printf("psycho_alloc_extent: extent_alloc_region %" 837 PRIx64 "-%" PRIx64 " failed\n", baddr, bsize); 838 extent_destroy(ex); 839 ex = NULL; 840 goto ret; 841 } 842 843 #ifdef DEBUG 844 /* print extent */ 845 extent_print(ex); 846 #endif 847 848 ret: 849 /* return extent */ 850 free(pa, M_DEVBUF); 851 return ex; 852 } 853 854 /* 855 * grovel the OBP for various psycho properties 856 */ 857 static void 858 psycho_get_bus_range(int node, int *brp) 859 { 860 int n, error; 861 862 n = 2; 863 error = prom_getprop(node, "bus-range", sizeof(*brp), &n, &brp); 864 if (error) 865 panic("could not get psycho bus-range, error %d", error); 866 if (n != 2) 867 panic("broken psycho bus-range"); 868 DPRINTF(PDB_PROM, ("%s: got `bus-range' for node %08x: %u - %u\n", 869 __func__, node, brp[0], brp[1])); 870 } 871 872 static void 873 psycho_fixup_bus_range(int node0, int *brp0) 874 { 875 int node; 876 int len, busrange[2], *brp; 877 878 DPRINTF(PDB_PROM, 879 ("%s: fixing up `bus-range' for node %08x: %u - %u\n", 880 __func__, node0, brp0[0], brp0[1])); 881 882 /* 883 * Check all nodes under this one and increase the bus range to 884 * match. Recurse through PCI-PCI bridges. Cardbus bridges are 885 * fixed up in pccbb_attach_hook(). Assumes that "bus-range" for 886 * PCI-PCI bridges apart from this one is correct. 887 */ 888 brp0[1] = brp0[0]; 889 node = prom_firstchild(node0); 890 for (node = ((node)); node; node = prom_nextsibling(node)) { 891 len = 2; 892 brp = busrange; 893 if (prom_getprop(node, "bus-range", sizeof(*brp), 894 &len, &brp) != 0) 895 break; 896 if (len != 2) 897 break; 898 psycho_fixup_bus_range(node, busrange); 899 if (brp0[0] > busrange[0] && busrange[0] >= 0) 900 brp0[0] = busrange[0]; 901 if (brp0[1] < busrange[1] && busrange[1] < 256) 902 brp0[1] = busrange[1]; 903 } 904 905 DPRINTF(PDB_PROM, 906 ("%s: fixed up `bus-range' for node %08x: %u - %u\n", 907 __func__, node0, brp[0], brp[1])); 908 } 909 910 static void 911 psycho_get_ranges(int node, struct psycho_ranges **rp, int *np) 912 { 913 914 if (prom_getprop(node, "ranges", sizeof(**rp), np, rp)) 915 panic("could not get psycho ranges"); 916 DPRINTF(PDB_PROM, ("%s: got `ranges' for node %08x: %d entries\n", 917 __func__, node, *np)); 918 } 919 920 /* 921 * Interrupt handlers. 922 */ 923 924 static int 925 psycho_ue(void *arg) 926 { 927 struct psycho_softc *sc = (struct psycho_softc *)arg; 928 struct psychoreg *regs = sc->sc_regs; 929 struct iommu_state *is = sc->sc_is; 930 uint64_t afsr = regs->psy_ue_afsr; 931 uint64_t afar = regs->psy_ue_afar; 932 psize_t size = PAGE_SIZE << is->is_tsbsize; 933 char bits[128]; 934 935 /* 936 * It's uncorrectable. Dump the regs and panic. 937 */ 938 snprintb(bits, sizeof(bits), PSYCHO_UE_AFSR_BITS, afsr); 939 aprint_error_dev(sc->sc_dev, 940 "uncorrectable DMA error AFAR %" PRIx64 " AFSR %s\n", afar, bits); 941 942 /* Sometimes the AFAR points to an IOTSB entry */ 943 if (afar >= is->is_ptsb && afar < is->is_ptsb + size) { 944 aprint_error_dev(sc->sc_dev, 945 "IOVA %" PRIx64 " IOTTE %" PRIx64 "\n", 946 (afar - is->is_ptsb) / sizeof(is->is_tsb[0]) * PAGE_SIZE 947 + is->is_dvmabase, ldxa(afar, ASI_PHYS_CACHED)); 948 } 949 #ifdef DDB 950 Debugger(); 951 #endif 952 regs->psy_ue_afar = 0; 953 regs->psy_ue_afsr = 0; 954 return (1); 955 } 956 957 static int 958 psycho_ce(void *arg) 959 { 960 struct psycho_softc *sc = (struct psycho_softc *)arg; 961 struct psychoreg *regs = sc->sc_regs; 962 963 /* 964 * It's correctable. Dump the regs and continue. 965 */ 966 aprint_error_dev(sc->sc_dev, 967 "correctable DMA error AFAR %" PRIx64 " AFSR %" PRIx64 "\n", 968 regs->psy_ce_afar, regs->psy_ce_afsr); 969 return (1); 970 } 971 972 static int 973 psycho_bus_a(void *arg) 974 { 975 struct psycho_softc *sc = (struct psycho_softc *)arg; 976 struct psychoreg *regs = sc->sc_regs; 977 978 /* 979 * It's uncorrectable. Dump the regs and panic. 980 */ 981 982 panic("%s: PCI bus A error AFAR %" PRIx64 " AFSR %" PRIx64, 983 device_xname(sc->sc_dev), 984 regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr); 985 return (1); 986 } 987 988 static int 989 psycho_bus_b(void *arg) 990 { 991 struct psycho_softc *sc = (struct psycho_softc *)arg; 992 struct psychoreg *regs = sc->sc_regs; 993 994 /* 995 * It's uncorrectable. Dump the regs and panic. 996 */ 997 998 panic("%s: PCI bus B error AFAR %" PRIx64 " AFSR %" PRIx64, 999 device_xname(sc->sc_dev), 1000 regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr); 1001 return (1); 1002 } 1003 1004 static int 1005 psycho_powerfail(void *arg) 1006 { 1007 struct psycho_softc *sc = (struct psycho_softc *)arg; 1008 1009 /* 1010 * We lost power. Queue a callback with thread context to 1011 * handle all the real work. 1012 */ 1013 if (sc->sc_powerpressed == 0 && sc->sc_smcontext != NULL) { 1014 sc->sc_powerpressed = 1; 1015 sysmon_task_queue_sched(0, psycho_power_button_pressed, sc); 1016 } 1017 return (1); 1018 } 1019 1020 static 1021 int psycho_wakeup(void *arg) 1022 { 1023 struct psycho_softc *sc = (struct psycho_softc *)arg; 1024 1025 /* 1026 * Gee, we don't really have a framework to deal with this 1027 * properly. 1028 */ 1029 aprint_error_dev(sc->sc_dev, "power management wakeup\n"); 1030 return (1); 1031 } 1032 1033 1034 /* 1035 * initialise the IOMMU.. 1036 */ 1037 void 1038 psycho_iommu_init(struct psycho_softc *sc, int tsbsize) 1039 { 1040 char *name; 1041 struct iommu_state *is = sc->sc_is; 1042 uint32_t iobase = -1; 1043 int *vdma = NULL; 1044 int nitem; 1045 1046 /* punch in our copies */ 1047 is->is_bustag = sc->sc_bustag; 1048 bus_space_subregion(sc->sc_bustag, sc->sc_bh, 1049 offsetof(struct psychoreg, psy_iommu), 1050 sizeof (struct iommureg), 1051 &is->is_iommu); 1052 1053 /* 1054 * Separate the men from the boys. Get the `virtual-dma' 1055 * property for sabre and use that to make sure the damn 1056 * iommu works. 1057 * 1058 * We could query the `#virtual-dma-size-cells' and 1059 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy. 1060 */ 1061 nitem = 0; 1062 if (!prom_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem, 1063 &vdma)) { 1064 /* Damn. Gotta use these values. */ 1065 iobase = vdma[0]; 1066 #define TSBCASE(x) case 1<<((x)+23): tsbsize = (x); break 1067 switch (vdma[1]) { 1068 TSBCASE(1); TSBCASE(2); TSBCASE(3); 1069 TSBCASE(4); TSBCASE(5); TSBCASE(6); 1070 default: 1071 printf("bogus tsb size %x, using 7\n", vdma[1]); 1072 TSBCASE(7); 1073 } 1074 #undef TSBCASE 1075 } 1076 1077 /* give us a nice name.. */ 1078 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT); 1079 if (name == 0) 1080 panic("couldn't malloc iommu name"); 1081 snprintf(name, 32, "%s dvma", device_xname(sc->sc_dev)); 1082 1083 iommu_init(name, is, tsbsize, iobase); 1084 } 1085 1086 /* 1087 * below here is bus space and bus DMA support 1088 */ 1089 bus_space_tag_t 1090 psycho_alloc_bus_tag(struct psycho_pbm *pp, int type) 1091 { 1092 struct psycho_softc *sc = pp->pp_sc; 1093 bus_space_tag_t bt; 1094 1095 bt = (bus_space_tag_t) malloc(sizeof(struct sparc_bus_space_tag), 1096 M_DEVBUF, M_NOWAIT | M_ZERO); 1097 if (bt == NULL) 1098 panic("could not allocate psycho bus tag"); 1099 1100 bt->cookie = pp; 1101 bt->parent = sc->sc_bustag; 1102 bt->type = type; 1103 bt->sparc_bus_map = _psycho_bus_map; 1104 bt->sparc_bus_mmap = psycho_bus_mmap; 1105 bt->sparc_intr_establish = psycho_intr_establish; 1106 return (bt); 1107 } 1108 1109 bus_dma_tag_t 1110 psycho_alloc_dma_tag(struct psycho_pbm *pp) 1111 { 1112 struct psycho_softc *sc = pp->pp_sc; 1113 bus_dma_tag_t dt, pdt = sc->sc_dmatag; 1114 1115 dt = (bus_dma_tag_t) 1116 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT); 1117 if (dt == NULL) 1118 panic("could not allocate psycho DMA tag"); 1119 1120 memset(dt, 0, sizeof *dt); 1121 dt->_cookie = pp; 1122 dt->_parent = pdt; 1123 #define PCOPY(x) dt->x = pdt->x 1124 dt->_dmamap_create = psycho_dmamap_create; 1125 PCOPY(_dmamap_destroy); 1126 dt->_dmamap_load = iommu_dvmamap_load; 1127 PCOPY(_dmamap_load_mbuf); 1128 PCOPY(_dmamap_load_uio); 1129 dt->_dmamap_load_raw = iommu_dvmamap_load_raw; 1130 dt->_dmamap_unload = iommu_dvmamap_unload; 1131 if (sc->sc_mode == PSYCHO_MODE_SABRE) 1132 dt->_dmamap_sync = psycho_sabre_dmamap_sync; 1133 else 1134 dt->_dmamap_sync = iommu_dvmamap_sync; 1135 dt->_dmamem_alloc = iommu_dvmamem_alloc; 1136 dt->_dmamem_free = iommu_dvmamem_free; 1137 dt->_dmamem_map = iommu_dvmamem_map; 1138 dt->_dmamem_unmap = iommu_dvmamem_unmap; 1139 PCOPY(_dmamem_mmap); 1140 #undef PCOPY 1141 return (dt); 1142 } 1143 1144 /* 1145 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about 1146 * PCI physical addresses. 1147 */ 1148 1149 static struct psycho_ranges * 1150 get_psychorange(struct psycho_pbm *pp, int ss) 1151 { 1152 int i; 1153 1154 for (i = 0; i < pp->pp_nrange; i++) { 1155 if (((pp->pp_range[i].cspace >> 24) & 0x03) == ss) 1156 return (&pp->pp_range[i]); 1157 } 1158 /* not found */ 1159 return (NULL); 1160 } 1161 1162 static int 1163 _psycho_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size, 1164 int flags, vaddr_t unused, bus_space_handle_t *hp) 1165 { 1166 struct psycho_pbm *pp = t->cookie; 1167 struct psycho_softc *sc = pp->pp_sc; 1168 struct psycho_ranges *pr; 1169 bus_addr_t paddr; 1170 int ss; 1171 1172 DPRINTF(PDB_BUSMAP, 1173 ("%s: type %d off %qx sz %qx flags %d", 1174 __func__, t->type, (unsigned long long)offset, 1175 (unsigned long long)size, flags)); 1176 1177 ss = sparc_pci_childspace(t->type); 1178 DPRINTF(PDB_BUSMAP, (" cspace %d", ss)); 1179 1180 pr = get_psychorange(pp, ss); 1181 if (pr != NULL) { 1182 paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset); 1183 DPRINTF(PDB_BUSMAP, ("\n%s: mapping paddr " 1184 "space %lx offset %lx paddr %qx\n", 1185 __func__, (long)ss, (long)offset, 1186 (unsigned long long)paddr)); 1187 return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size, 1188 flags, 0, hp)); 1189 } 1190 DPRINTF(PDB_BUSMAP, (" FAILED\n")); 1191 return (EINVAL); 1192 } 1193 1194 static paddr_t 1195 psycho_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot, 1196 int flags) 1197 { 1198 bus_addr_t offset = paddr; 1199 struct psycho_pbm *pp = t->cookie; 1200 struct psycho_softc *sc = pp->pp_sc; 1201 struct psycho_ranges *pr; 1202 int ss; 1203 1204 ss = sparc_pci_childspace(t->type); 1205 1206 DPRINTF(PDB_BUSMAP, ("%s: prot %x flags %d busaddr %qx\n", 1207 __func__, prot, flags, (unsigned long long)paddr)); 1208 1209 pr = get_psychorange(pp, ss); 1210 if (pr != NULL) { 1211 paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset); 1212 DPRINTF(PDB_BUSMAP, ("%s: mapping paddr " 1213 "space %lx offset %lx paddr %qx\n", 1214 __func__, (long)ss, (long)offset, 1215 (unsigned long long)paddr)); 1216 return (bus_space_mmap(sc->sc_bustag, paddr, off, 1217 prot, flags)); 1218 } 1219 1220 return (-1); 1221 } 1222 1223 /* 1224 * Get a PCI offset address from bus_space_handle_t. 1225 */ 1226 bus_addr_t 1227 psycho_bus_offset(bus_space_tag_t t, bus_space_handle_t *hp) 1228 { 1229 struct psycho_pbm *pp = t->cookie; 1230 struct psycho_ranges *pr; 1231 bus_addr_t addr, offset; 1232 vaddr_t va; 1233 int ss; 1234 1235 addr = hp->_ptr; 1236 ss = sparc_pci_childspace(t->type); 1237 DPRINTF(PDB_BUSMAP, ("%s: type %d addr %" PRIx64" cspace %d", 1238 __func__, t->type, addr, ss)); 1239 1240 pr = get_psychorange(pp, ss); 1241 if (pr != NULL) { 1242 if (!PHYS_ASI(hp->_asi)) { 1243 va = trunc_page((vaddr_t)addr); 1244 if (pmap_extract(pmap_kernel(), va, &addr) == FALSE) { 1245 DPRINTF(PDB_BUSMAP, 1246 ("- pmap_extract FAILED\n")); 1247 return (-1); 1248 } 1249 addr += hp->_ptr & PGOFSET; 1250 } 1251 offset = BUS_ADDR_PADDR(addr) - pr->phys_lo; 1252 DPRINTF(PDB_BUSMAP, ("- paddr %" PRIx64" offset %" PRIx64 "\n", 1253 addr, offset)); 1254 return (offset); 1255 } 1256 DPRINTF(PDB_BUSMAP, ("- FAILED\n")); 1257 return (-1); 1258 } 1259 1260 1261 /* 1262 * install an interrupt handler for a PCI device 1263 */ 1264 void * 1265 psycho_intr_establish(bus_space_tag_t t, int ihandle, int level, 1266 int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */) 1267 { 1268 struct psycho_pbm *pp = t->cookie; 1269 struct psycho_softc *sc = pp->pp_sc; 1270 struct intrhand *ih; 1271 volatile uint64_t *intrmapptr = NULL, *intrclrptr = NULL; 1272 int64_t imap = 0; 1273 int ino; 1274 long vec = INTVEC(ihandle); 1275 1276 ih = malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT); 1277 if (ih == NULL) 1278 return (NULL); 1279 1280 ih->ih_ivec = ihandle; 1281 1282 /* 1283 * Hunt through all the interrupt mapping regs to look for our 1284 * interrupt vector. 1285 * 1286 * XXX We only compare INOs rather than IGNs since the firmware may 1287 * not provide the IGN and the IGN is constant for all device on that 1288 * PCI controller. This could cause problems for the FFB/external 1289 * interrupt which has a full vector that can be set arbitrarily. 1290 */ 1291 1292 DPRINTF(PDB_INTR, ("%s: ihandle %x vec %lx", __func__, ihandle, vec)); 1293 ino = INTINO(vec); 1294 DPRINTF(PDB_INTR, (" ino %x\n", ino)); 1295 1296 /* If the device didn't ask for an IPL, use the one encoded. */ 1297 if (level == IPL_NONE) level = INTLEV(vec); 1298 /* If it still has no level, print a warning and assign IPL 2 */ 1299 if (level == IPL_NONE) { 1300 printf("ERROR: no IPL, setting IPL 2.\n"); 1301 level = 2; 1302 } 1303 1304 DPRINTF(PDB_INTR, ("%s: intr %lx: %p\nHunting for IRQ...\n", 1305 __func__, (long)ino, intrlev[ino])); 1306 1307 /* 1308 * First look for PCI interrupts, otherwise the PCI A slot 0 1309 * INTA# interrupt might match an unused non-PCI (obio) 1310 * interrupt. 1311 */ 1312 for (intrmapptr = &sc->sc_regs->pcia_slot0_int, 1313 intrclrptr = &sc->sc_regs->pcia0_clr_int[0]; 1314 intrmapptr <= &sc->sc_regs->pcib_slot3_int; 1315 intrmapptr++, intrclrptr += 4) { 1316 if (sc->sc_mode == PSYCHO_MODE_PSYCHO && 1317 (intrmapptr == &sc->sc_regs->pcia_slot2_int || 1318 intrmapptr == &sc->sc_regs->pcia_slot3_int)) 1319 continue; 1320 if (((*intrmapptr ^ vec) & 0x3c) == 0) { 1321 intrclrptr += vec & 0x3; 1322 goto found; 1323 } 1324 } 1325 1326 /* Now hunt thru obio. */ 1327 for (intrmapptr = &sc->sc_regs->scsi_int_map, 1328 intrclrptr = &sc->sc_regs->scsi_clr_int; 1329 intrmapptr < &sc->sc_regs->ue_int_map; 1330 intrmapptr++, intrclrptr++) { 1331 if (INTINO(*intrmapptr) == ino) 1332 goto found; 1333 } 1334 1335 /* Finally check the two FFB slots */ 1336 intrclrptr = NULL; /* XXX? */ 1337 for (intrmapptr = &sc->sc_regs->ffb0_int_map; 1338 intrmapptr <= &sc->sc_regs->ffb1_int_map; 1339 intrmapptr++) { 1340 if (INTVEC(*intrmapptr) == ino) 1341 goto found; 1342 } 1343 1344 printf("Cannot find interrupt vector %lx\n", vec); 1345 free(ih, M_DEVBUF); 1346 return (NULL); 1347 1348 found: 1349 /* Register the map and clear intr registers */ 1350 ih->ih_map = intrmapptr; 1351 ih->ih_clr = intrclrptr; 1352 1353 ih->ih_fun = handler; 1354 ih->ih_arg = arg; 1355 ih->ih_pil = level; 1356 ih->ih_number = ino | sc->sc_ign; 1357 ih->ih_pending = 0; 1358 1359 DPRINTF(PDB_INTR, ( 1360 "%s: installing handler %p arg %p with ino %u pil %u\n", 1361 __func__, handler, arg, (u_int)ino, (u_int)ih->ih_pil)); 1362 1363 intr_establish(ih->ih_pil, level != IPL_VM, ih); 1364 1365 /* 1366 * Enable the interrupt now we have the handler installed. 1367 * Read the current value as we can't change it besides the 1368 * valid bit so so make sure only this bit is changed. 1369 * 1370 * XXXX --- we really should use bus_space for this. 1371 */ 1372 if (intrmapptr) { 1373 imap = *intrmapptr; 1374 DPRINTF(PDB_INTR, ("%s: read intrmap = %016qx", 1375 __func__, (unsigned long long)imap)); 1376 1377 /* Enable the interrupt */ 1378 imap |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT); 1379 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr)); 1380 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n", 1381 (unsigned long long)imap)); 1382 *intrmapptr = imap; 1383 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx\n", 1384 (unsigned long long)(imap = *intrmapptr))); 1385 } 1386 if (intrclrptr) { 1387 /* set state to IDLE */ 1388 *intrclrptr = 0; 1389 } 1390 return (ih); 1391 } 1392 1393 /* 1394 * per-controller driver calls 1395 */ 1396 1397 /* assume we are mapped little-endian/side-effect */ 1398 static pcireg_t 1399 psycho_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg) 1400 { 1401 struct psycho_pbm *pp = pc->cookie; 1402 struct psycho_softc *sc = pp->pp_sc; 1403 struct cpu_info *ci = curcpu(); 1404 pcireg_t val = (pcireg_t)~0; 1405 int s; 1406 1407 DPRINTF(PDB_CONF, ("%s: tag %lx reg %x ", __func__, 1408 (long)tag, reg)); 1409 if (PCITAG_NODE(tag) != -1 && (unsigned int)reg < PCI_CONF_SIZE) { 1410 1411 DPRINTF(PDB_CONF, ("asi=%x addr=%qx (offset=%x) ...", 1412 sc->sc_configaddr._asi, 1413 (long long)(sc->sc_configaddr._ptr + 1414 PCITAG_OFFSET(tag) + reg), 1415 (int)PCITAG_OFFSET(tag) + reg)); 1416 1417 s = splhigh(); 1418 ci->ci_pci_probe = true; 1419 membar_Sync(); 1420 val = bus_space_read_4(sc->sc_configtag, sc->sc_configaddr, 1421 PCITAG_OFFSET(tag) + reg); 1422 membar_Sync(); 1423 if (ci->ci_pci_fault) 1424 val = (pcireg_t)~0; 1425 ci->ci_pci_probe = ci->ci_pci_fault = false; 1426 splx(s); 1427 } 1428 #ifdef DEBUG 1429 else DPRINTF(PDB_CONF, ("%s: bogus pcitag %x -", __func__, 1430 (int)PCITAG_OFFSET(tag))); 1431 #endif 1432 DPRINTF(PDB_CONF, (" returning %08x\n", (u_int)val)); 1433 1434 return (val); 1435 } 1436 1437 static void 1438 psycho_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data) 1439 { 1440 struct psycho_pbm *pp = pc->cookie; 1441 struct psycho_softc *sc = pp->pp_sc; 1442 1443 DPRINTF(PDB_CONF, ("%s: tag %lx; reg %x; data %x; ", __func__, 1444 (long)PCITAG_OFFSET(tag), reg, (int)data)); 1445 DPRINTF(PDB_CONF, ("asi = %x; readaddr = %qx (offset = %x)\n", 1446 sc->sc_configaddr._asi, 1447 (long long)(sc->sc_configaddr._ptr + PCITAG_OFFSET(tag) + reg), 1448 (int)PCITAG_OFFSET(tag) + reg)); 1449 1450 /* If we don't know it, just punt it. */ 1451 if (PCITAG_NODE(tag) == -1) { 1452 DPRINTF(PDB_CONF, ("%s: bad addr", __func__)); 1453 return; 1454 } 1455 1456 if ((unsigned int)reg >= PCI_CONF_SIZE) 1457 return; 1458 1459 bus_space_write_4(sc->sc_configtag, sc->sc_configaddr, 1460 PCITAG_OFFSET(tag) + reg, data); 1461 } 1462 1463 static void * 1464 psycho_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level, 1465 int (*func)(void *), void *arg) 1466 { 1467 void *cookie; 1468 struct psycho_pbm *pp = (struct psycho_pbm *)pc->cookie; 1469 1470 DPRINTF(PDB_INTR, ("%s: ih %lx; level %d", __func__, (u_long)ih, level)); 1471 cookie = bus_intr_establish(pp->pp_memt, ih, level, func, arg); 1472 1473 DPRINTF(PDB_INTR, ("; returning handle %p\n", cookie)); 1474 return (cookie); 1475 } 1476 1477 static int 1478 psycho_pci_find_ino(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 1479 { 1480 struct psycho_pbm *pp = pa->pa_pc->cookie; 1481 struct psycho_softc *sc = pp->pp_sc; 1482 u_int bus; 1483 u_int dev; 1484 u_int pin; 1485 1486 DPRINTF(PDB_INTMAP, ("%s: pa_tag: node %x, %d:%d:%d\n", __func__, 1487 PCITAG_NODE(pa->pa_tag), (int)PCITAG_BUS(pa->pa_tag), 1488 (int)PCITAG_DEV(pa->pa_tag), 1489 (int)PCITAG_FUN(pa->pa_tag))); 1490 DPRINTF(PDB_INTMAP, 1491 ("%s: intrswiz %d, intrpin %d, intrline %d, rawintrpin %d\n", __func__, 1492 pa->pa_intrswiz, pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin)); 1493 DPRINTF(PDB_INTMAP, ("%s: pa_intrtag: node %x, %d:%d:%d\n", __func__, 1494 PCITAG_NODE(pa->pa_intrtag), 1495 (int)PCITAG_BUS(pa->pa_intrtag), 1496 (int)PCITAG_DEV(pa->pa_intrtag), 1497 (int)PCITAG_FUN(pa->pa_intrtag))); 1498 1499 bus = (pp->pp_id == PSYCHO_PBM_B); 1500 /* 1501 * If we are on a ppb, use the devno on the underlying bus when forming 1502 * the ivec. 1503 */ 1504 if (pa->pa_intrswiz != 0 && PCITAG_NODE(pa->pa_intrtag) != 0) 1505 dev = PCITAG_DEV(pa->pa_intrtag); 1506 else 1507 dev = pa->pa_device; 1508 dev--; 1509 1510 if (sc->sc_mode == PSYCHO_MODE_PSYCHO && 1511 pp->pp_id == PSYCHO_PBM_B) 1512 dev--; 1513 1514 pin = pa->pa_intrpin - 1; 1515 DPRINTF(PDB_INTMAP, ("%s: mode %d, pbm %d, dev %d, pin %d\n", __func__, 1516 sc->sc_mode, pp->pp_id, dev, pin)); 1517 1518 *ihp = sc->sc_ign | ((bus << 4) & INTMAP_PCIBUS) | 1519 ((dev << 2) & INTMAP_PCISLOT) | (pin & INTMAP_PCIINT); 1520 1521 return (0); 1522 } 1523 1524 /* 1525 * hooks into the iommu dvma calls. 1526 */ 1527 static int 1528 psycho_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 1529 bus_size_t maxsegsz, bus_size_t boundary, int flags, 1530 bus_dmamap_t *dmamp) 1531 { 1532 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie; 1533 int error; 1534 1535 error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz, 1536 boundary, flags, dmamp); 1537 if (error == 0) 1538 (*dmamp)->_dm_cookie = &pp->pp_sb; 1539 return error; 1540 } 1541 1542 /* 1543 * UltraSPARC IIi and IIe have no streaming buffers, but have PCI DMA 1544 * Write Synchronization Register (see UltraSPARC-IIi User's Manual 1545 * section 19.3.0.5). So use it to synchronize with the DMA writes. 1546 */ 1547 static void 1548 psycho_sabre_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 1549 bus_size_t len, int ops) 1550 { 1551 struct psycho_pbm *pp; 1552 struct psycho_softc *sc; 1553 1554 /* If len is 0, then there is nothing to do. */ 1555 if (len == 0) 1556 return; 1557 1558 if (ops & BUS_DMASYNC_POSTREAD) { 1559 pp = (struct psycho_pbm *)t->_cookie; 1560 sc = pp->pp_sc; 1561 bus_space_read_8(sc->sc_bustag, sc->sc_bh, 1562 offsetof(struct psychoreg, pci_dma_write_sync)); 1563 } 1564 bus_dmamap_sync(t->_parent, map, offset, len, ops); 1565 } 1566 1567 /* US-IIe STICK support */ 1568 1569 uint64_t 1570 psycho_getstick(void) 1571 { 1572 uint64_t stick; 1573 1574 stick = bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh, 1575 STICK_CNT_LOW) | 1576 (bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh, 1577 STICK_CNT_HIGH) & 0x7fffffff) << 32; 1578 return stick; 1579 } 1580 1581 uint32_t 1582 psycho_getstick32(void) 1583 { 1584 1585 return bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh, 1586 STICK_CNT_LOW); 1587 } 1588 1589 void 1590 psycho_setstick(long cnt) 1591 { 1592 1593 /* 1594 * looks like we can't actually write the STICK counter, so instead we 1595 * prepare sc_last_stick for the coming interrupt setup 1596 */ 1597 #if 0 1598 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1599 STICK_CNT_HIGH, (cnt >> 32)); 1600 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1601 STICK_CNT_LOW, (uint32_t)(cnt & 0xffffffff)); 1602 #endif 1603 1604 if (cnt == 0) { 1605 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1606 STICK_CMP_HIGH, 0); 1607 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1608 STICK_CMP_LOW, 0); 1609 psycho0->sc_last_stick = 0; 1610 } 1611 1612 psycho0->sc_last_stick = psycho_getstick(); 1613 DPRINTF(PDB_STICK, ("%s: %ld\n", __func__, psycho0->sc_last_stick)); 1614 } 1615 1616 void 1617 psycho_nextstick(long diff) 1618 { 1619 uint64_t cmp, now; 1620 1621 /* 1622 * there is no way we'll ever overflow 1623 * the counter is 63 bits wide, at 12MHz that's >24000 years 1624 */ 1625 now = psycho_getstick() + 1000; 1626 cmp = psycho0->sc_last_stick; 1627 1628 while (cmp < now) 1629 cmp += diff; 1630 1631 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1632 STICK_CMP_HIGH, (cmp >> 32) & 0x7fffffff); 1633 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1634 STICK_CMP_LOW, (cmp & 0xffffffff)); 1635 1636 psycho0->sc_last_stick = cmp; 1637 } 1638