1 /* $NetBSD: psycho.c,v 1.124 2016/05/10 19:23:59 palle 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.124 2016/05/10 19:23:59 palle 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 = intrhand_alloc(); 711 ih->ih_arg = sc; 712 ih->ih_map = mapper; 713 ih->ih_clr = clearer; 714 ih->ih_fun = handler; 715 ih->ih_pil = ipl; 716 ih->ih_number = INTVEC(*(ih->ih_map)); 717 ih->ih_pending = 0; 718 intr_establish(ipl, ipl != IPL_VM, ih); 719 *(ih->ih_map) |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT); 720 } 721 722 /* 723 * power button handlers 724 */ 725 static void 726 psycho_register_power_button(struct psycho_softc *sc) 727 { 728 sysmon_task_queue_init(); 729 730 sc->sc_powerpressed = 0; 731 sc->sc_smcontext = malloc(sizeof(struct sysmon_pswitch), M_DEVBUF, 0); 732 if (!sc->sc_smcontext) { 733 aprint_error_dev(sc->sc_dev, "could not allocate power button context\n"); 734 return; 735 } 736 memset(sc->sc_smcontext, 0, sizeof(struct sysmon_pswitch)); 737 sc->sc_smcontext->smpsw_name = device_xname(sc->sc_dev); 738 sc->sc_smcontext->smpsw_type = PSWITCH_TYPE_POWER; 739 if (sysmon_pswitch_register(sc->sc_smcontext) != 0) 740 aprint_error_dev(sc->sc_dev, "unable to register power button with sysmon\n"); 741 } 742 743 static void 744 psycho_power_button_pressed(void *arg) 745 { 746 struct psycho_softc *sc = arg; 747 748 sysmon_pswitch_event(sc->sc_smcontext, PSWITCH_EVENT_PRESSED); 749 sc->sc_powerpressed = 0; 750 } 751 752 /* 753 * PCI bus support 754 */ 755 756 /* 757 * allocate a PCI chipset tag and set its cookie. 758 */ 759 static pci_chipset_tag_t 760 psycho_alloc_chipset(struct psycho_pbm *pp, int node, pci_chipset_tag_t pc) 761 { 762 pci_chipset_tag_t npc; 763 764 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT); 765 if (npc == NULL) 766 panic("could not allocate pci_chipset_tag_t"); 767 memcpy(npc, pc, sizeof *pc); 768 npc->cookie = pp; 769 npc->rootnode = node; 770 npc->spc_conf_read = psycho_pci_conf_read; 771 npc->spc_conf_write = psycho_pci_conf_write; 772 npc->spc_intr_map = NULL; 773 npc->spc_intr_establish = psycho_pci_intr_establish; 774 npc->spc_find_ino = psycho_pci_find_ino; 775 776 return (npc); 777 } 778 779 /* 780 * create extent for free bus space, then allocate assigned regions. 781 */ 782 static struct extent * 783 psycho_alloc_extent(struct psycho_pbm *pp, int node, int ss, const char *name) 784 { 785 struct psycho_registers *pa = NULL; 786 struct psycho_ranges *pr; 787 struct extent *ex; 788 bus_addr_t baddr, addr; 789 bus_size_t bsize, size; 790 int i, num; 791 792 /* get bus space size */ 793 pr = get_psychorange(pp, ss); 794 if (pr == NULL) { 795 printf("psycho_alloc_extent: get_psychorange failed\n"); 796 return NULL; 797 } 798 baddr = 0x00000000; 799 bsize = BUS_ADDR(pr->size_hi, pr->size_lo); 800 801 /* get available lists */ 802 num = 0; 803 if (prom_getprop(node, "available", sizeof(*pa), &num, &pa)) { 804 printf("psycho_alloc_extent: no \"available\" property\n"); 805 return NULL; 806 } 807 808 /* create extent */ 809 ex = extent_create(name, baddr, bsize - baddr - 1, 0, 0, EX_NOWAIT); 810 if (ex == NULL) { 811 printf("psycho_alloc_extent: extent_create failed\n"); 812 goto ret; 813 } 814 815 /* allocate assigned regions */ 816 for (i = 0; i < num; i++) 817 if (((pa[i].phys_hi >> 24) & 0x03) == ss) { 818 /* allocate bus space */ 819 addr = BUS_ADDR(pa[i].phys_mid, pa[i].phys_lo); 820 size = BUS_ADDR(pa[i].size_hi, pa[i].size_lo); 821 if (extent_alloc_region(ex, baddr, addr - baddr, 822 EX_NOWAIT)) { 823 printf("psycho_alloc_extent: " 824 "extent_alloc_region %" PRIx64 "-%" 825 PRIx64 " failed\n", baddr, addr); 826 extent_destroy(ex); 827 ex = NULL; 828 goto ret; 829 } 830 baddr = addr + size; 831 } 832 /* allocate left region if available */ 833 if (baddr < bsize) 834 if (extent_alloc_region(ex, baddr, bsize - baddr, EX_NOWAIT)) { 835 printf("psycho_alloc_extent: extent_alloc_region %" 836 PRIx64 "-%" PRIx64 " failed\n", baddr, bsize); 837 extent_destroy(ex); 838 ex = NULL; 839 goto ret; 840 } 841 842 #ifdef DEBUG 843 /* print extent */ 844 extent_print(ex); 845 #endif 846 847 ret: 848 /* return extent */ 849 free(pa, M_DEVBUF); 850 return ex; 851 } 852 853 /* 854 * grovel the OBP for various psycho properties 855 */ 856 static void 857 psycho_get_bus_range(int node, int *brp) 858 { 859 int n, error; 860 861 n = 2; 862 error = prom_getprop(node, "bus-range", sizeof(*brp), &n, &brp); 863 if (error) 864 panic("could not get psycho bus-range, error %d", error); 865 if (n != 2) 866 panic("broken psycho bus-range"); 867 DPRINTF(PDB_PROM, ("%s: got `bus-range' for node %08x: %u - %u\n", 868 __func__, node, brp[0], brp[1])); 869 } 870 871 static void 872 psycho_fixup_bus_range(int node0, int *brp0) 873 { 874 int node; 875 int len, busrange[2], *brp; 876 877 DPRINTF(PDB_PROM, 878 ("%s: fixing up `bus-range' for node %08x: %u - %u\n", 879 __func__, node0, brp0[0], brp0[1])); 880 881 /* 882 * Check all nodes under this one and increase the bus range to 883 * match. Recurse through PCI-PCI bridges. Cardbus bridges are 884 * fixed up in pccbb_attach_hook(). Assumes that "bus-range" for 885 * PCI-PCI bridges apart from this one is correct. 886 */ 887 brp0[1] = brp0[0]; 888 node = prom_firstchild(node0); 889 for (node = ((node)); node; node = prom_nextsibling(node)) { 890 len = 2; 891 brp = busrange; 892 if (prom_getprop(node, "bus-range", sizeof(*brp), 893 &len, &brp) != 0) 894 break; 895 if (len != 2) 896 break; 897 psycho_fixup_bus_range(node, busrange); 898 if (brp0[0] > busrange[0] && busrange[0] >= 0) 899 brp0[0] = busrange[0]; 900 if (brp0[1] < busrange[1] && busrange[1] < 256) 901 brp0[1] = busrange[1]; 902 } 903 904 DPRINTF(PDB_PROM, 905 ("%s: fixed up `bus-range' for node %08x: %u - %u\n", 906 __func__, node0, brp[0], brp[1])); 907 } 908 909 static void 910 psycho_get_ranges(int node, struct psycho_ranges **rp, int *np) 911 { 912 913 if (prom_getprop(node, "ranges", sizeof(**rp), np, rp)) 914 panic("could not get psycho ranges"); 915 DPRINTF(PDB_PROM, ("%s: got `ranges' for node %08x: %d entries\n", 916 __func__, node, *np)); 917 } 918 919 /* 920 * Interrupt handlers. 921 */ 922 923 static int 924 psycho_ue(void *arg) 925 { 926 struct psycho_softc *sc = (struct psycho_softc *)arg; 927 struct psychoreg *regs = sc->sc_regs; 928 struct iommu_state *is = sc->sc_is; 929 uint64_t afsr = regs->psy_ue_afsr; 930 uint64_t afar = regs->psy_ue_afar; 931 psize_t size = PAGE_SIZE << is->is_tsbsize; 932 char bits[128]; 933 934 /* 935 * It's uncorrectable. Dump the regs and panic. 936 */ 937 snprintb(bits, sizeof(bits), PSYCHO_UE_AFSR_BITS, afsr); 938 aprint_error_dev(sc->sc_dev, 939 "uncorrectable DMA error AFAR %" PRIx64 " AFSR %s\n", afar, bits); 940 941 /* Sometimes the AFAR points to an IOTSB entry */ 942 if (afar >= is->is_ptsb && afar < is->is_ptsb + size) { 943 aprint_error_dev(sc->sc_dev, 944 "IOVA %" PRIx64 " IOTTE %" PRIx64 "\n", 945 (afar - is->is_ptsb) / sizeof(is->is_tsb[0]) * PAGE_SIZE 946 + is->is_dvmabase, ldxa(afar, ASI_PHYS_CACHED)); 947 } 948 #ifdef DDB 949 Debugger(); 950 #endif 951 regs->psy_ue_afar = 0; 952 regs->psy_ue_afsr = 0; 953 return (1); 954 } 955 956 static int 957 psycho_ce(void *arg) 958 { 959 struct psycho_softc *sc = (struct psycho_softc *)arg; 960 struct psychoreg *regs = sc->sc_regs; 961 962 /* 963 * It's correctable. Dump the regs and continue. 964 */ 965 aprint_error_dev(sc->sc_dev, 966 "correctable DMA error AFAR %" PRIx64 " AFSR %" PRIx64 "\n", 967 regs->psy_ce_afar, regs->psy_ce_afsr); 968 return (1); 969 } 970 971 static int 972 psycho_bus_a(void *arg) 973 { 974 struct psycho_softc *sc = (struct psycho_softc *)arg; 975 struct psychoreg *regs = sc->sc_regs; 976 977 /* 978 * It's uncorrectable. Dump the regs and panic. 979 */ 980 981 panic("%s: PCI bus A error AFAR %" PRIx64 " AFSR %" PRIx64, 982 device_xname(sc->sc_dev), 983 regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr); 984 return (1); 985 } 986 987 static int 988 psycho_bus_b(void *arg) 989 { 990 struct psycho_softc *sc = (struct psycho_softc *)arg; 991 struct psychoreg *regs = sc->sc_regs; 992 993 /* 994 * It's uncorrectable. Dump the regs and panic. 995 */ 996 997 panic("%s: PCI bus B error AFAR %" PRIx64 " AFSR %" PRIx64, 998 device_xname(sc->sc_dev), 999 regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr); 1000 return (1); 1001 } 1002 1003 static int 1004 psycho_powerfail(void *arg) 1005 { 1006 struct psycho_softc *sc = (struct psycho_softc *)arg; 1007 1008 /* 1009 * We lost power. Queue a callback with thread context to 1010 * handle all the real work. 1011 */ 1012 if (sc->sc_powerpressed == 0 && sc->sc_smcontext != NULL) { 1013 sc->sc_powerpressed = 1; 1014 sysmon_task_queue_sched(0, psycho_power_button_pressed, sc); 1015 } 1016 return (1); 1017 } 1018 1019 static 1020 int psycho_wakeup(void *arg) 1021 { 1022 struct psycho_softc *sc = (struct psycho_softc *)arg; 1023 1024 /* 1025 * Gee, we don't really have a framework to deal with this 1026 * properly. 1027 */ 1028 aprint_error_dev(sc->sc_dev, "power management wakeup\n"); 1029 return (1); 1030 } 1031 1032 1033 /* 1034 * initialise the IOMMU.. 1035 */ 1036 void 1037 psycho_iommu_init(struct psycho_softc *sc, int tsbsize) 1038 { 1039 char *name; 1040 struct iommu_state *is = sc->sc_is; 1041 uint32_t iobase = -1; 1042 int *vdma = NULL; 1043 int nitem; 1044 1045 /* punch in our copies */ 1046 is->is_bustag = sc->sc_bustag; 1047 bus_space_subregion(sc->sc_bustag, sc->sc_bh, 1048 offsetof(struct psychoreg, psy_iommu), 1049 sizeof (struct iommureg), 1050 &is->is_iommu); 1051 1052 /* 1053 * Separate the men from the boys. Get the `virtual-dma' 1054 * property for sabre and use that to make sure the damn 1055 * iommu works. 1056 * 1057 * We could query the `#virtual-dma-size-cells' and 1058 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy. 1059 */ 1060 nitem = 0; 1061 if (!prom_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem, 1062 &vdma)) { 1063 /* Damn. Gotta use these values. */ 1064 iobase = vdma[0]; 1065 #define TSBCASE(x) case 1<<((x)+23): tsbsize = (x); break 1066 switch (vdma[1]) { 1067 TSBCASE(1); TSBCASE(2); TSBCASE(3); 1068 TSBCASE(4); TSBCASE(5); TSBCASE(6); 1069 default: 1070 printf("bogus tsb size %x, using 7\n", vdma[1]); 1071 TSBCASE(7); 1072 } 1073 #undef TSBCASE 1074 } 1075 1076 /* give us a nice name.. */ 1077 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT); 1078 if (name == 0) 1079 panic("couldn't malloc iommu name"); 1080 snprintf(name, 32, "%s dvma", device_xname(sc->sc_dev)); 1081 1082 iommu_init(name, is, tsbsize, iobase); 1083 } 1084 1085 /* 1086 * below here is bus space and bus DMA support 1087 */ 1088 bus_space_tag_t 1089 psycho_alloc_bus_tag(struct psycho_pbm *pp, int type) 1090 { 1091 struct psycho_softc *sc = pp->pp_sc; 1092 bus_space_tag_t bt; 1093 1094 bt = (bus_space_tag_t) malloc(sizeof(struct sparc_bus_space_tag), 1095 M_DEVBUF, M_NOWAIT | M_ZERO); 1096 if (bt == NULL) 1097 panic("could not allocate psycho bus tag"); 1098 1099 bt->cookie = pp; 1100 bt->parent = sc->sc_bustag; 1101 bt->type = type; 1102 bt->sparc_bus_map = _psycho_bus_map; 1103 bt->sparc_bus_mmap = psycho_bus_mmap; 1104 bt->sparc_intr_establish = psycho_intr_establish; 1105 return (bt); 1106 } 1107 1108 bus_dma_tag_t 1109 psycho_alloc_dma_tag(struct psycho_pbm *pp) 1110 { 1111 struct psycho_softc *sc = pp->pp_sc; 1112 bus_dma_tag_t dt, pdt = sc->sc_dmatag; 1113 1114 dt = (bus_dma_tag_t) 1115 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT); 1116 if (dt == NULL) 1117 panic("could not allocate psycho DMA tag"); 1118 1119 memset(dt, 0, sizeof *dt); 1120 dt->_cookie = pp; 1121 dt->_parent = pdt; 1122 #define PCOPY(x) dt->x = pdt->x 1123 dt->_dmamap_create = psycho_dmamap_create; 1124 PCOPY(_dmamap_destroy); 1125 dt->_dmamap_load = iommu_dvmamap_load; 1126 PCOPY(_dmamap_load_mbuf); 1127 PCOPY(_dmamap_load_uio); 1128 dt->_dmamap_load_raw = iommu_dvmamap_load_raw; 1129 dt->_dmamap_unload = iommu_dvmamap_unload; 1130 if (sc->sc_mode == PSYCHO_MODE_SABRE) 1131 dt->_dmamap_sync = psycho_sabre_dmamap_sync; 1132 else 1133 dt->_dmamap_sync = iommu_dvmamap_sync; 1134 dt->_dmamem_alloc = iommu_dvmamem_alloc; 1135 dt->_dmamem_free = iommu_dvmamem_free; 1136 dt->_dmamem_map = iommu_dvmamem_map; 1137 dt->_dmamem_unmap = iommu_dvmamem_unmap; 1138 PCOPY(_dmamem_mmap); 1139 #undef PCOPY 1140 return (dt); 1141 } 1142 1143 /* 1144 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about 1145 * PCI physical addresses. 1146 */ 1147 1148 static struct psycho_ranges * 1149 get_psychorange(struct psycho_pbm *pp, int ss) 1150 { 1151 int i; 1152 1153 for (i = 0; i < pp->pp_nrange; i++) { 1154 if (((pp->pp_range[i].cspace >> 24) & 0x03) == ss) 1155 return (&pp->pp_range[i]); 1156 } 1157 /* not found */ 1158 return (NULL); 1159 } 1160 1161 static int 1162 _psycho_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size, 1163 int flags, vaddr_t unused, bus_space_handle_t *hp) 1164 { 1165 struct psycho_pbm *pp = t->cookie; 1166 struct psycho_softc *sc = pp->pp_sc; 1167 struct psycho_ranges *pr; 1168 bus_addr_t paddr; 1169 int ss; 1170 1171 DPRINTF(PDB_BUSMAP, 1172 ("%s: type %d off %qx sz %qx flags %d", 1173 __func__, t->type, (unsigned long long)offset, 1174 (unsigned long long)size, flags)); 1175 1176 ss = sparc_pci_childspace(t->type); 1177 DPRINTF(PDB_BUSMAP, (" cspace %d", ss)); 1178 1179 pr = get_psychorange(pp, ss); 1180 if (pr != NULL) { 1181 paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset); 1182 DPRINTF(PDB_BUSMAP, ("\n%s: mapping paddr " 1183 "space %lx offset %lx paddr %qx\n", 1184 __func__, (long)ss, (long)offset, 1185 (unsigned long long)paddr)); 1186 return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size, 1187 flags, 0, hp)); 1188 } 1189 DPRINTF(PDB_BUSMAP, (" FAILED\n")); 1190 return (EINVAL); 1191 } 1192 1193 static paddr_t 1194 psycho_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot, 1195 int flags) 1196 { 1197 bus_addr_t offset = paddr; 1198 struct psycho_pbm *pp = t->cookie; 1199 struct psycho_softc *sc = pp->pp_sc; 1200 struct psycho_ranges *pr; 1201 int ss; 1202 1203 ss = sparc_pci_childspace(t->type); 1204 1205 DPRINTF(PDB_BUSMAP, ("%s: prot %x flags %d busaddr %qx\n", 1206 __func__, prot, flags, (unsigned long long)paddr)); 1207 1208 pr = get_psychorange(pp, ss); 1209 if (pr != NULL) { 1210 paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset); 1211 DPRINTF(PDB_BUSMAP, ("%s: mapping paddr " 1212 "space %lx offset %lx paddr %qx\n", 1213 __func__, (long)ss, (long)offset, 1214 (unsigned long long)paddr)); 1215 return (bus_space_mmap(sc->sc_bustag, paddr, off, 1216 prot, flags)); 1217 } 1218 1219 return (-1); 1220 } 1221 1222 /* 1223 * Get a PCI offset address from bus_space_handle_t. 1224 */ 1225 bus_addr_t 1226 psycho_bus_offset(bus_space_tag_t t, bus_space_handle_t *hp) 1227 { 1228 struct psycho_pbm *pp = t->cookie; 1229 struct psycho_ranges *pr; 1230 bus_addr_t addr, offset; 1231 vaddr_t va; 1232 int ss; 1233 1234 addr = hp->_ptr; 1235 ss = sparc_pci_childspace(t->type); 1236 DPRINTF(PDB_BUSMAP, ("%s: type %d addr %" PRIx64" cspace %d", 1237 __func__, t->type, addr, ss)); 1238 1239 pr = get_psychorange(pp, ss); 1240 if (pr != NULL) { 1241 if (!PHYS_ASI(hp->_asi)) { 1242 va = trunc_page((vaddr_t)addr); 1243 if (pmap_extract(pmap_kernel(), va, &addr) == FALSE) { 1244 DPRINTF(PDB_BUSMAP, 1245 ("- pmap_extract FAILED\n")); 1246 return (-1); 1247 } 1248 addr += hp->_ptr & PGOFSET; 1249 } 1250 offset = BUS_ADDR_PADDR(addr) - pr->phys_lo; 1251 DPRINTF(PDB_BUSMAP, ("- paddr %" PRIx64" offset %" PRIx64 "\n", 1252 addr, offset)); 1253 return (offset); 1254 } 1255 DPRINTF(PDB_BUSMAP, ("- FAILED\n")); 1256 return (-1); 1257 } 1258 1259 1260 /* 1261 * install an interrupt handler for a PCI device 1262 */ 1263 void * 1264 psycho_intr_establish(bus_space_tag_t t, int ihandle, int level, 1265 int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */) 1266 { 1267 struct psycho_pbm *pp = t->cookie; 1268 struct psycho_softc *sc = pp->pp_sc; 1269 struct intrhand *ih; 1270 volatile uint64_t *intrmapptr = NULL, *intrclrptr = NULL; 1271 int64_t imap = 0; 1272 int ino; 1273 long vec = INTVEC(ihandle); 1274 1275 ih = intrhand_alloc(); 1276 1277 ih->ih_ivec = ihandle; 1278 1279 /* 1280 * Hunt through all the interrupt mapping regs to look for our 1281 * interrupt vector. 1282 * 1283 * XXX We only compare INOs rather than IGNs since the firmware may 1284 * not provide the IGN and the IGN is constant for all device on that 1285 * PCI controller. This could cause problems for the FFB/external 1286 * interrupt which has a full vector that can be set arbitrarily. 1287 */ 1288 1289 DPRINTF(PDB_INTR, ("%s: ihandle %x vec %lx", __func__, ihandle, vec)); 1290 ino = INTINO(vec); 1291 DPRINTF(PDB_INTR, (" ino %x\n", ino)); 1292 1293 /* If the device didn't ask for an IPL, use the one encoded. */ 1294 if (level == IPL_NONE) level = INTLEV(vec); 1295 /* If it still has no level, print a warning and assign IPL 2 */ 1296 if (level == IPL_NONE) { 1297 printf("ERROR: no IPL, setting IPL 2.\n"); 1298 level = 2; 1299 } 1300 1301 DPRINTF(PDB_INTR, ("%s: intr %lx: %p\nHunting for IRQ...\n", 1302 __func__, (long)ino, intrlev[ino])); 1303 1304 /* 1305 * First look for PCI interrupts, otherwise the PCI A slot 0 1306 * INTA# interrupt might match an unused non-PCI (obio) 1307 * interrupt. 1308 */ 1309 for (intrmapptr = &sc->sc_regs->pcia_slot0_int, 1310 intrclrptr = &sc->sc_regs->pcia0_clr_int[0]; 1311 intrmapptr <= &sc->sc_regs->pcib_slot3_int; 1312 intrmapptr++, intrclrptr += 4) { 1313 if (sc->sc_mode == PSYCHO_MODE_PSYCHO && 1314 (intrmapptr == &sc->sc_regs->pcia_slot2_int || 1315 intrmapptr == &sc->sc_regs->pcia_slot3_int)) 1316 continue; 1317 if (((*intrmapptr ^ vec) & 0x3c) == 0) { 1318 intrclrptr += vec & 0x3; 1319 goto found; 1320 } 1321 } 1322 1323 /* Now hunt thru obio. */ 1324 for (intrmapptr = &sc->sc_regs->scsi_int_map, 1325 intrclrptr = &sc->sc_regs->scsi_clr_int; 1326 intrmapptr < &sc->sc_regs->ue_int_map; 1327 intrmapptr++, intrclrptr++) { 1328 if (INTINO(*intrmapptr) == ino) 1329 goto found; 1330 } 1331 1332 /* Finally check the two FFB slots */ 1333 intrclrptr = NULL; /* XXX? */ 1334 for (intrmapptr = &sc->sc_regs->ffb0_int_map; 1335 intrmapptr <= &sc->sc_regs->ffb1_int_map; 1336 intrmapptr++) { 1337 if (INTVEC(*intrmapptr) == ino) 1338 goto found; 1339 } 1340 1341 printf("Cannot find interrupt vector %lx\n", vec); 1342 free(ih, M_DEVBUF); 1343 return (NULL); 1344 1345 found: 1346 /* Register the map and clear intr registers */ 1347 ih->ih_map = intrmapptr; 1348 ih->ih_clr = intrclrptr; 1349 1350 ih->ih_fun = handler; 1351 ih->ih_arg = arg; 1352 ih->ih_pil = level; 1353 ih->ih_number = ino | sc->sc_ign; 1354 ih->ih_pending = 0; 1355 1356 DPRINTF(PDB_INTR, ( 1357 "%s: installing handler %p arg %p with ino %u pil %u\n", 1358 __func__, handler, arg, (u_int)ino, (u_int)ih->ih_pil)); 1359 1360 intr_establish(ih->ih_pil, level != IPL_VM, ih); 1361 1362 /* 1363 * Enable the interrupt now we have the handler installed. 1364 * Read the current value as we can't change it besides the 1365 * valid bit so so make sure only this bit is changed. 1366 * 1367 * XXXX --- we really should use bus_space for this. 1368 */ 1369 if (intrmapptr) { 1370 imap = *intrmapptr; 1371 DPRINTF(PDB_INTR, ("%s: read intrmap = %016qx", 1372 __func__, (unsigned long long)imap)); 1373 1374 /* Enable the interrupt */ 1375 imap |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT); 1376 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr)); 1377 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n", 1378 (unsigned long long)imap)); 1379 *intrmapptr = imap; 1380 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx\n", 1381 (unsigned long long)(imap = *intrmapptr))); 1382 } 1383 if (intrclrptr) { 1384 /* set state to IDLE */ 1385 *intrclrptr = 0; 1386 } 1387 return (ih); 1388 } 1389 1390 /* 1391 * per-controller driver calls 1392 */ 1393 1394 /* assume we are mapped little-endian/side-effect */ 1395 static pcireg_t 1396 psycho_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg) 1397 { 1398 struct psycho_pbm *pp = pc->cookie; 1399 struct psycho_softc *sc = pp->pp_sc; 1400 struct cpu_info *ci = curcpu(); 1401 pcireg_t val = (pcireg_t)~0; 1402 int s; 1403 1404 DPRINTF(PDB_CONF, ("%s: tag %lx reg %x ", __func__, 1405 (long)tag, reg)); 1406 if (PCITAG_NODE(tag) != -1 && (unsigned int)reg < PCI_CONF_SIZE) { 1407 1408 DPRINTF(PDB_CONF, ("asi=%x addr=%qx (offset=%x) ...", 1409 sc->sc_configaddr._asi, 1410 (long long)(sc->sc_configaddr._ptr + 1411 PCITAG_OFFSET(tag) + reg), 1412 (int)PCITAG_OFFSET(tag) + reg)); 1413 1414 s = splhigh(); 1415 ci->ci_pci_probe = true; 1416 membar_Sync(); 1417 val = bus_space_read_4(sc->sc_configtag, sc->sc_configaddr, 1418 PCITAG_OFFSET(tag) + reg); 1419 membar_Sync(); 1420 if (ci->ci_pci_fault) 1421 val = (pcireg_t)~0; 1422 ci->ci_pci_probe = ci->ci_pci_fault = false; 1423 splx(s); 1424 } 1425 #ifdef DEBUG 1426 else DPRINTF(PDB_CONF, ("%s: bogus pcitag %x -", __func__, 1427 (int)PCITAG_OFFSET(tag))); 1428 #endif 1429 DPRINTF(PDB_CONF, (" returning %08x\n", (u_int)val)); 1430 1431 return (val); 1432 } 1433 1434 static void 1435 psycho_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data) 1436 { 1437 struct psycho_pbm *pp = pc->cookie; 1438 struct psycho_softc *sc = pp->pp_sc; 1439 1440 DPRINTF(PDB_CONF, ("%s: tag %lx; reg %x; data %x; ", __func__, 1441 (long)PCITAG_OFFSET(tag), reg, (int)data)); 1442 DPRINTF(PDB_CONF, ("asi = %x; readaddr = %qx (offset = %x)\n", 1443 sc->sc_configaddr._asi, 1444 (long long)(sc->sc_configaddr._ptr + PCITAG_OFFSET(tag) + reg), 1445 (int)PCITAG_OFFSET(tag) + reg)); 1446 1447 /* If we don't know it, just punt it. */ 1448 if (PCITAG_NODE(tag) == -1) { 1449 DPRINTF(PDB_CONF, ("%s: bad addr", __func__)); 1450 return; 1451 } 1452 1453 if ((unsigned int)reg >= PCI_CONF_SIZE) 1454 return; 1455 1456 bus_space_write_4(sc->sc_configtag, sc->sc_configaddr, 1457 PCITAG_OFFSET(tag) + reg, data); 1458 } 1459 1460 static void * 1461 psycho_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level, 1462 int (*func)(void *), void *arg) 1463 { 1464 void *cookie; 1465 struct psycho_pbm *pp = (struct psycho_pbm *)pc->cookie; 1466 1467 DPRINTF(PDB_INTR, ("%s: ih %lx; level %d", __func__, (u_long)ih, level)); 1468 cookie = bus_intr_establish(pp->pp_memt, ih, level, func, arg); 1469 1470 DPRINTF(PDB_INTR, ("; returning handle %p\n", cookie)); 1471 return (cookie); 1472 } 1473 1474 static int 1475 psycho_pci_find_ino(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 1476 { 1477 struct psycho_pbm *pp = pa->pa_pc->cookie; 1478 struct psycho_softc *sc = pp->pp_sc; 1479 u_int bus; 1480 u_int dev; 1481 u_int pin; 1482 1483 DPRINTF(PDB_INTMAP, ("%s: pa_tag: node %x, %d:%d:%d\n", __func__, 1484 PCITAG_NODE(pa->pa_tag), (int)PCITAG_BUS(pa->pa_tag), 1485 (int)PCITAG_DEV(pa->pa_tag), 1486 (int)PCITAG_FUN(pa->pa_tag))); 1487 DPRINTF(PDB_INTMAP, 1488 ("%s: intrswiz %d, intrpin %d, intrline %d, rawintrpin %d\n", __func__, 1489 pa->pa_intrswiz, pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin)); 1490 DPRINTF(PDB_INTMAP, ("%s: pa_intrtag: node %x, %d:%d:%d\n", __func__, 1491 PCITAG_NODE(pa->pa_intrtag), 1492 (int)PCITAG_BUS(pa->pa_intrtag), 1493 (int)PCITAG_DEV(pa->pa_intrtag), 1494 (int)PCITAG_FUN(pa->pa_intrtag))); 1495 1496 bus = (pp->pp_id == PSYCHO_PBM_B); 1497 /* 1498 * If we are on a ppb, use the devno on the underlying bus when forming 1499 * the ivec. 1500 */ 1501 if (pa->pa_intrswiz != 0 && PCITAG_NODE(pa->pa_intrtag) != 0) 1502 dev = PCITAG_DEV(pa->pa_intrtag); 1503 else 1504 dev = pa->pa_device; 1505 dev--; 1506 1507 if (sc->sc_mode == PSYCHO_MODE_PSYCHO && 1508 pp->pp_id == PSYCHO_PBM_B) 1509 dev--; 1510 1511 pin = pa->pa_intrpin - 1; 1512 DPRINTF(PDB_INTMAP, ("%s: mode %d, pbm %d, dev %d, pin %d\n", __func__, 1513 sc->sc_mode, pp->pp_id, dev, pin)); 1514 1515 *ihp = sc->sc_ign | ((bus << 4) & INTMAP_PCIBUS) | 1516 ((dev << 2) & INTMAP_PCISLOT) | (pin & INTMAP_PCIINT); 1517 1518 return (0); 1519 } 1520 1521 /* 1522 * hooks into the iommu dvma calls. 1523 */ 1524 static int 1525 psycho_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 1526 bus_size_t maxsegsz, bus_size_t boundary, int flags, 1527 bus_dmamap_t *dmamp) 1528 { 1529 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie; 1530 int error; 1531 1532 error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz, 1533 boundary, flags, dmamp); 1534 if (error == 0) 1535 (*dmamp)->_dm_cookie = &pp->pp_sb; 1536 return error; 1537 } 1538 1539 /* 1540 * UltraSPARC IIi and IIe have no streaming buffers, but have PCI DMA 1541 * Write Synchronization Register (see UltraSPARC-IIi User's Manual 1542 * section 19.3.0.5). So use it to synchronize with the DMA writes. 1543 */ 1544 static void 1545 psycho_sabre_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 1546 bus_size_t len, int ops) 1547 { 1548 struct psycho_pbm *pp; 1549 struct psycho_softc *sc; 1550 1551 /* If len is 0, then there is nothing to do. */ 1552 if (len == 0) 1553 return; 1554 1555 if (ops & BUS_DMASYNC_POSTREAD) { 1556 pp = (struct psycho_pbm *)t->_cookie; 1557 sc = pp->pp_sc; 1558 bus_space_read_8(sc->sc_bustag, sc->sc_bh, 1559 offsetof(struct psychoreg, pci_dma_write_sync)); 1560 } 1561 bus_dmamap_sync(t->_parent, map, offset, len, ops); 1562 } 1563 1564 /* US-IIe STICK support */ 1565 1566 uint64_t 1567 psycho_getstick(void) 1568 { 1569 uint64_t stick; 1570 1571 stick = bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh, 1572 STICK_CNT_LOW) | 1573 (bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh, 1574 STICK_CNT_HIGH) & 0x7fffffff) << 32; 1575 return stick; 1576 } 1577 1578 uint32_t 1579 psycho_getstick32(void) 1580 { 1581 1582 return bus_space_read_8(psycho0->sc_bustag, psycho0->sc_bh, 1583 STICK_CNT_LOW); 1584 } 1585 1586 void 1587 psycho_setstick(long cnt) 1588 { 1589 1590 /* 1591 * looks like we can't actually write the STICK counter, so instead we 1592 * prepare sc_last_stick for the coming interrupt setup 1593 */ 1594 #if 0 1595 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1596 STICK_CNT_HIGH, (cnt >> 32)); 1597 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1598 STICK_CNT_LOW, (uint32_t)(cnt & 0xffffffff)); 1599 #endif 1600 1601 if (cnt == 0) { 1602 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1603 STICK_CMP_HIGH, 0); 1604 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1605 STICK_CMP_LOW, 0); 1606 psycho0->sc_last_stick = 0; 1607 } 1608 1609 psycho0->sc_last_stick = psycho_getstick(); 1610 DPRINTF(PDB_STICK, ("%s: %ld\n", __func__, psycho0->sc_last_stick)); 1611 } 1612 1613 void 1614 psycho_nextstick(long diff) 1615 { 1616 uint64_t cmp, now; 1617 1618 /* 1619 * there is no way we'll ever overflow 1620 * the counter is 63 bits wide, at 12MHz that's >24000 years 1621 */ 1622 now = psycho_getstick() + 1000; 1623 cmp = psycho0->sc_last_stick; 1624 1625 while (cmp < now) 1626 cmp += diff; 1627 1628 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1629 STICK_CMP_HIGH, (cmp >> 32) & 0x7fffffff); 1630 bus_space_write_8(psycho0->sc_bustag, psycho0->sc_bh, 1631 STICK_CMP_LOW, (cmp & 0xffffffff)); 1632 1633 psycho0->sc_last_stick = cmp; 1634 } 1635