1 /* $OpenBSD: psycho.c,v 1.37 2003/06/24 21:54:39 henric Exp $ */ 2 /* $NetBSD: psycho.c,v 1.39 2001/10/07 20:30:41 eeh Exp $ */ 3 4 /* 5 * Copyright (c) 1999, 2000 Matthew R. Green 6 * Copyright (c) 2003 Henric Jungheim 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Support for `psycho' and `psycho+' UPA to PCI bridge and 35 * UltraSPARC IIi and IIe `sabre' PCI controllers. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/device.h> 40 #include <sys/errno.h> 41 #include <sys/extent.h> 42 #include <sys/malloc.h> 43 #include <sys/systm.h> 44 #include <sys/time.h> 45 #include <sys/reboot.h> 46 47 #include <uvm/uvm_extern.h> 48 49 #define _SPARC_BUS_DMA_PRIVATE 50 #include <machine/bus.h> 51 #include <machine/autoconf.h> 52 #include <machine/psl.h> 53 54 #include <dev/pci/pcivar.h> 55 #include <dev/pci/pcireg.h> 56 57 #include <sparc64/dev/iommureg.h> 58 #include <sparc64/dev/iommuvar.h> 59 #include <sparc64/dev/psychoreg.h> 60 #include <sparc64/dev/psychovar.h> 61 #include <sparc64/sparc64/cache.h> 62 63 #ifdef DEBUG 64 #define PDB_PROM 0x01 65 #define PDB_BUSMAP 0x02 66 #define PDB_INTR 0x04 67 #define PDB_CONF 0x08 68 int psycho_debug = ~0; 69 #define DPRINTF(l, s) do { if (psycho_debug & l) printf s; } while (0) 70 #else 71 #define DPRINTF(l, s) 72 #endif 73 74 pci_chipset_tag_t psycho_alloc_chipset(struct psycho_pbm *, int, 75 pci_chipset_tag_t); 76 void psycho_get_bus_range(int, int *); 77 void psycho_get_ranges(int, struct psycho_ranges **, int *); 78 void psycho_set_intr(struct psycho_softc *, int, void *, 79 u_int64_t *, u_int64_t *); 80 bus_space_tag_t _psycho_alloc_bus_tag(struct psycho_pbm *, 81 const char *, int, int, int); 82 83 /* Interrupt handlers */ 84 int psycho_ue(void *); 85 int psycho_ce(void *); 86 int psycho_bus_a(void *); 87 int psycho_bus_b(void *); 88 int psycho_bus_error(struct psycho_softc *, int); 89 int psycho_powerfail(void *); 90 int psycho_wakeup(void *); 91 92 /* IOMMU support */ 93 void psycho_iommu_init(struct psycho_softc *, int); 94 95 /* 96 * bus space and bus dma support for UltraSPARC `psycho'. note that most 97 * of the bus dma support is provided by the iommu dvma controller. 98 */ 99 paddr_t psycho_bus_mmap(bus_space_tag_t, bus_space_tag_t, bus_addr_t, off_t, 100 int, int); 101 int _psycho_bus_map(bus_space_tag_t, bus_space_tag_t, bus_addr_t, 102 bus_size_t, int, bus_space_handle_t *); 103 void *psycho_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int, 104 int (*)(void *), void *, const char *); 105 106 int psycho_dmamap_create(bus_dma_tag_t, bus_dma_tag_t, bus_size_t, int, 107 bus_size_t, bus_size_t, int, bus_dmamap_t *); 108 void psycho_sabre_dvmamap_sync(bus_dma_tag_t, bus_dma_tag_t, bus_dmamap_t, 109 bus_size_t, bus_size_t, int); 110 void psycho_map_psycho(struct psycho_softc *, int, bus_addr_t, bus_size_t, 111 bus_addr_t, bus_size_t); 112 int psycho_intr_map(struct pci_attach_args *, pci_intr_handle_t *); 113 void psycho_identify_pbm(struct psycho_softc *sc, struct psycho_pbm *pp, 114 struct pcibus_attach_args *pa); 115 116 /* base pci_chipset */ 117 extern struct sparc_pci_chipset _sparc_pci_chipset; 118 119 /* 120 * autoconfiguration 121 */ 122 int psycho_match(struct device *, void *, void *); 123 void psycho_attach(struct device *, struct device *, void *); 124 int psycho_print(void *aux, const char *p); 125 126 127 struct cfattach psycho_ca = { 128 sizeof(struct psycho_softc), psycho_match, psycho_attach 129 }; 130 131 struct cfdriver psycho_cd = { 132 NULL, "psycho", DV_DULL 133 }; 134 135 /* 136 * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge. It manages a 137 * single PCI bus and does not have a streaming buffer. It often has an APB 138 * (advanced PCI bridge) connected to it, which was designed specifically for 139 * the IIi. The APB let's the IIi handle two independednt PCI buses, and 140 * appears as two "simba"'s underneath the sabre. 141 * 142 * "psycho" and "psycho+" is a dual UPA to PCI bridge. It sits on the UPA bus 143 * and manages two PCI buses. "psycho" has two 64-bit 33MHz buses, while 144 * "psycho+" controls both a 64-bit 33MHz and a 64-bit 66MHz PCI bus. You 145 * will usually find a "psycho+" since I don't think the original "psycho" 146 * ever shipped, and if it did it would be in the U30. 147 * 148 * Each "psycho" PCI bus appears as a separate OFW node, but since they are 149 * both part of the same IC, they only have a single register space. As such, 150 * they need to be configured together, even though the autoconfiguration will 151 * attach them separately. 152 * 153 * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often 154 * as pci1 and pci2, although they have been implemented with other PCI bus 155 * numbers on some machines. 156 * 157 * On UltraII machines, there can be any number of "psycho+" ICs, each 158 * providing two PCI buses. 159 * 160 * 161 * XXXX The psycho/sabre node has an `interrupts' attribute. They contain 162 * the values of the following interrupts in this order: 163 * 164 * PCI Bus Error (30) 165 * DMA UE (2e) 166 * DMA CE (2f) 167 * Power Fail (25) 168 * 169 * We really should attach handlers for each. 170 * 171 */ 172 #define ROM_PCI_NAME "pci" 173 174 struct psycho_type { 175 char *p_name; 176 int p_type; 177 } psycho_types[] = { 178 { "SUNW,psycho", PSYCHO_MODE_PSYCHO }, 179 { "pci108e,8000", PSYCHO_MODE_PSYCHO }, 180 { "SUNW,sabre", PSYCHO_MODE_SABRE }, 181 { "pci108e,a000", PSYCHO_MODE_SABRE }, 182 { "pci108e,a001", PSYCHO_MODE_SABRE }, 183 { NULL, 0 } 184 }; 185 186 int 187 psycho_match(struct device *parent, void *match, void *aux) 188 { 189 struct mainbus_attach_args *ma = aux; 190 struct psycho_type *ptype; 191 char *str; 192 193 /* match on a name of "pci" and a sabre or a psycho */ 194 if (strcmp(ma->ma_name, ROM_PCI_NAME) != 0) 195 return (0); 196 197 for (ptype = psycho_types; ptype->p_name != NULL; ptype++) { 198 str = getpropstring(ma->ma_node, "model"); 199 if (strcmp(str, ptype->p_name) == 0) 200 return (1); 201 str = getpropstring(ma->ma_node, "compatible"); 202 if (strcmp(str, ptype->p_name) == 0) 203 return (1); 204 } 205 return (0); 206 } 207 208 /* 209 * SUNW,psycho initialization ... 210 * - find the per-psycho registers 211 * - figure out the IGN. 212 * - find our partner psycho 213 * - configure ourselves 214 * - bus range, bus, 215 * - get interrupt-map and interrupt-map-mask 216 * - setup the chipsets. 217 * - if we're the first of the pair, initialise the IOMMU, otherwise 218 * just copy it's tags and addresses. 219 */ 220 void 221 psycho_attach(struct device *parent, struct device *self, void *aux) 222 { 223 struct psycho_softc *sc = (struct psycho_softc *)self; 224 struct psycho_softc *osc = NULL; 225 struct psycho_pbm *pp; 226 struct pcibus_attach_args pba; 227 struct mainbus_attach_args *ma = aux; 228 u_int64_t csr; 229 int psycho_br[2], n; 230 struct psycho_type *ptype; 231 232 printf("\n"); 233 234 sc->sc_node = ma->ma_node; 235 sc->sc_bustag = ma->ma_bustag; 236 sc->sc_dmatag = ma->ma_dmatag; 237 238 /* 239 * call the model-specific initialization routine. 240 */ 241 242 for (ptype = psycho_types; ptype->p_name != NULL; ptype++) { 243 char *str; 244 245 str = getpropstring(ma->ma_node, "model"); 246 if (strcmp(str, ptype->p_name) == 0) 247 break; 248 str = getpropstring(ma->ma_node, "compatible"); 249 if (strcmp(str, ptype->p_name) == 0) 250 break; 251 } 252 if (ptype->p_name == NULL) 253 panic("psycho_attach: unknown model?"); 254 sc->sc_mode = ptype->p_type; 255 256 /* 257 * The psycho gets three register banks: 258 * (0) per-PBM configuration and status registers 259 * (1) per-PBM PCI configuration space, containing only the 260 * PBM 256-byte PCI header 261 * (2) the shared psycho configuration registers (struct psychoreg) 262 * 263 * XXX use the prom address for the psycho registers? we do so far. 264 */ 265 266 /* Register layouts are different. stuupid. */ 267 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) { 268 sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr; 269 270 if (ma->ma_naddress > 2) { 271 psycho_map_psycho(sc, 0, 272 ma->ma_address[2], sizeof(struct psychoreg), 273 ma->ma_address[0], sizeof(struct pci_ctl)); 274 } else if (ma->ma_nreg > 2) { 275 psycho_map_psycho(sc, 1, 276 ma->ma_reg[2].ur_paddr, ma->ma_reg[2].ur_len, 277 ma->ma_reg[0].ur_paddr, ma->ma_reg[0].ur_len); 278 } else 279 panic("psycho_attach: %d not enough registers", 280 ma->ma_nreg); 281 } else { 282 sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr; 283 284 if (ma->ma_naddress) { 285 psycho_map_psycho(sc, 0, 286 ma->ma_address[0], sizeof(struct psychoreg), 287 ma->ma_address[0] + 288 offsetof(struct psychoreg, psy_pcictl[0]), 289 sizeof(struct pci_ctl)); 290 } else if (ma->ma_nreg) { 291 psycho_map_psycho(sc, 1, 292 ma->ma_reg[0].ur_paddr, ma->ma_reg[0].ur_len, 293 ma->ma_reg[0].ur_paddr + 294 offsetof(struct psychoreg, psy_pcictl[0]), 295 sizeof(struct pci_ctl)); 296 } else 297 panic("psycho_attach: %d not enough registers", 298 ma->ma_nreg); 299 } 300 301 csr = psycho_psychoreg_read(sc, psy_csr); 302 sc->sc_ign = INTMAP_IGN; /* APB IGN is always 0x1f << 6 = 0x7c */ 303 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) 304 sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6; 305 306 printf("%s: impl %d, version %d: ign %x ", ptype->p_name, 307 PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr), sc->sc_ign); 308 309 /* 310 * Match other psycho's that are already configured against 311 * the base physical address. This will be the same for a 312 * pair of devices that share register space. 313 */ 314 for (n = 0; n < psycho_cd.cd_ndevs; n++) { 315 struct psycho_softc *asc = 316 (struct psycho_softc *)psycho_cd.cd_devs[n]; 317 318 if (asc == NULL || asc == sc) 319 /* This entry is not there or it is me */ 320 continue; 321 322 if (asc->sc_basepaddr != sc->sc_basepaddr) 323 /* This is an unrelated psycho */ 324 continue; 325 326 /* Found partner */ 327 osc = asc; 328 break; 329 } 330 331 /* Oh, dear. OK, lets get started */ 332 333 /* 334 * Setup the PCI control register 335 */ 336 csr = psycho_pcictl_read(sc, pci_csr); 337 csr |= PCICTL_MRLM | PCICTL_ARB_PARK | PCICTL_ERRINTEN | 338 PCICTL_4ENABLE; 339 csr &= ~(PCICTL_SERR | PCICTL_CPU_PRIO | PCICTL_ARB_PRIO | 340 PCICTL_RTRYWAIT); 341 psycho_pcictl_write(sc, pci_csr, csr); 342 343 /* 344 * Allocate our psycho_pbm 345 */ 346 pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT); 347 if (pp == NULL) 348 panic("could not allocate psycho pbm"); 349 350 memset(pp, 0, sizeof *pp); 351 352 pp->pp_sc = sc; 353 354 /* grab the psycho ranges */ 355 psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange); 356 357 /* get the bus-range for the psycho */ 358 psycho_get_bus_range(sc->sc_node, psycho_br); 359 360 pba.pba_bus = psycho_br[0]; 361 362 printf("bus range %u to %u", psycho_br[0], psycho_br[1]); 363 printf("; PCI bus %d", psycho_br[0]); 364 365 pp->pp_pcictl = sc->sc_pcictl; 366 367 /* allocate our tags */ 368 pp->pp_memt = psycho_alloc_mem_tag(pp); 369 pp->pp_iot = psycho_alloc_io_tag(pp); 370 pp->pp_dmat = psycho_alloc_dma_tag(pp); 371 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) | 372 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0); 373 374 /* allocate a chipset for this */ 375 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset); 376 377 /* setup the rest of the psycho pbm */ 378 pba.pba_pc = pp->pp_pc; 379 380 printf("\n"); 381 382 /* 383 * And finally, if we're a sabre or the first of a pair of psycho's to 384 * arrive here, start up the IOMMU and get a config space tag. 385 */ 386 387 if (osc == NULL) { 388 uint64_t timeo; 389 390 /* 391 * Establish handlers for interesting interrupts.... 392 * 393 * XXX We need to remember these and remove this to support 394 * hotplug on the UPA/FHC bus. 395 * 396 * XXX Not all controllers have these, but installing them 397 * is better than trying to sort through this mess. 398 */ 399 psycho_set_intr(sc, 15, psycho_ue, 400 psycho_psychoreg_vaddr(sc, ue_int_map), 401 psycho_psychoreg_vaddr(sc, ue_clr_int)); 402 psycho_set_intr(sc, 1, psycho_ce, 403 psycho_psychoreg_vaddr(sc, ce_int_map), 404 psycho_psychoreg_vaddr(sc, ce_clr_int)); 405 psycho_set_intr(sc, 15, psycho_bus_a, 406 psycho_psychoreg_vaddr(sc, pciaerr_int_map), 407 psycho_psychoreg_vaddr(sc, pciaerr_clr_int)); 408 #if 0 409 psycho_set_intr(sc, 15, psycho_powerfail, 410 psycho_psychoreg_vaddr(sc, power_int_map), 411 psycho_psychoreg_vaddr(sc, power_clr_int)); 412 #endif 413 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) { 414 psycho_set_intr(sc, 15, psycho_bus_b, 415 psycho_psychoreg_vaddr(sc, pciberr_int_map), 416 psycho_psychoreg_vaddr(sc, pciberr_clr_int)); 417 psycho_set_intr(sc, 1, psycho_wakeup, 418 psycho_psychoreg_vaddr(sc, pwrmgt_int_map), 419 psycho_psychoreg_vaddr(sc, pwrmgt_clr_int)); 420 } 421 422 /* 423 * Apparently a number of machines with psycho and psycho+ 424 * controllers have interrupt latency issues. We'll try 425 * setting the interrupt retry timeout to 0xff which gives us 426 * a retry of 3-6 usec (which is what sysio is set to) for the 427 * moment, which seems to help alleviate this problem. 428 */ 429 timeo = psycho_psychoreg_read(sc, intr_retry_timer); 430 if (timeo > 0xfff) { 431 #ifdef DEBUG 432 printf("decreasing interrupt retry timeout " 433 "from %lx to 0xff\n", (long)timeo); 434 #endif 435 psycho_psychoreg_write(sc, intr_retry_timer, 0xff); 436 } 437 438 /* 439 * Setup IOMMU and PCI configuration if we're the first 440 * of a pair of psycho's to arrive here. 441 * 442 * We should calculate a TSB size based on the amount of RAM, 443 * number of bus controllers, and number and type of child 444 * devices. 445 * 446 * For the moment, 32KB should be more than enough. 447 */ 448 sc->sc_is = malloc(sizeof(struct iommu_state), 449 M_DEVBUF, M_NOWAIT); 450 if (sc->sc_is == NULL) 451 panic("psycho_attach: malloc iommu_state"); 452 453 memset(sc->sc_is, 0, sizeof *sc->sc_is); 454 455 if (getproplen(sc->sc_node, "no-streaming-cache") < 0) { 456 struct strbuf_ctl *sb = &pp->pp_sb; 457 vaddr_t va = (vaddr_t)&pp->pp_flush[0x40]; 458 459 /* 460 * Initialize the strbuf_ctl. 461 * 462 * The flush sync buffer must be 64-byte aligned. 463 */ 464 465 sb->sb_flush = (void *)(va & ~0x3f); 466 467 sb->sb_bustag = sc->sc_bustag; 468 if (bus_space_subregion(sc->sc_bustag, sc->sc_pcictl, 469 offsetof(struct pci_ctl, pci_strbuf), 470 sizeof(struct iommu_strbuf), 471 &sb->sb_sb)) { 472 printf("STC0 subregion failed\n"); 473 sb->sb_flush = 0; 474 } 475 } 476 477 /* Point out iommu at the strbuf_ctl. */ 478 sc->sc_is->is_sb[0] = &pp->pp_sb; 479 480 psycho_iommu_init(sc, 2); 481 482 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this); 483 if (bus_space_map(sc->sc_configtag, 484 sc->sc_basepaddr, 0x01000000, 0, &sc->sc_configaddr)) 485 panic("could not map psycho PCI configuration space"); 486 } else { 487 /* Just copy IOMMU state, config tag and address */ 488 sc->sc_is = osc->sc_is; 489 sc->sc_configtag = osc->sc_configtag; 490 sc->sc_configaddr = osc->sc_configaddr; 491 492 if (getproplen(sc->sc_node, "no-streaming-cache") < 0) { 493 struct strbuf_ctl *sb = &pp->pp_sb; 494 vaddr_t va = (vaddr_t)&pp->pp_flush[0x40]; 495 496 /* 497 * Initialize the strbuf_ctl. 498 * 499 * The flush sync buffer must be 64-byte aligned. 500 */ 501 502 sb->sb_flush = (void *)(va & ~0x3f); 503 504 sb->sb_bustag = sc->sc_bustag; 505 if (bus_space_subregion(sc->sc_bustag, sc->sc_pcictl, 506 offsetof(struct pci_ctl, pci_strbuf), 507 sizeof(struct iommu_strbuf), 508 &sb->sb_sb)) { 509 printf("STC1 subregion failed\n"); 510 sb->sb_flush = 0; 511 } 512 513 /* Point out iommu at the strbuf_ctl. */ 514 sc->sc_is->is_sb[1] = sb; 515 } 516 517 /* Point out iommu at the strbuf_ctl. */ 518 sc->sc_is->is_sb[1] = &pp->pp_sb; 519 520 iommu_reset(sc->sc_is); 521 } 522 523 /* 524 * attach the pci.. note we pass PCI A tags, etc., for the sabre here. 525 */ 526 pba.pba_busname = "pci"; 527 #if 0 528 pba.pba_flags = sc->sc_psycho_this->pp_flags; 529 #endif 530 pba.pba_dmat = sc->sc_psycho_this->pp_dmat; 531 pba.pba_iot = sc->sc_psycho_this->pp_iot; 532 pba.pba_memt = sc->sc_psycho_this->pp_memt; 533 pba.pba_pc->bustag = sc->sc_configtag; 534 pba.pba_pc->bushandle = sc->sc_configaddr; 535 pba.pba_pc->intr_map = psycho_intr_map; 536 537 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) 538 psycho_identify_pbm(sc, pp, &pba); 539 else 540 pp->pp_id = PSYCHO_PBM_UNKNOWN; 541 542 config_found(self, &pba, psycho_print); 543 } 544 545 void 546 psycho_identify_pbm(struct psycho_softc *sc, struct psycho_pbm *pp, 547 struct pcibus_attach_args *pa) 548 { 549 vaddr_t pci_va = (vaddr_t)bus_space_vaddr(sc->sc_bustag, sc->sc_pcictl); 550 paddr_t pci_pa; 551 552 if (pmap_extract(pmap_kernel(), pci_va, &pci_pa) == 0) 553 pp->pp_id = PSYCHO_PBM_UNKNOWN; 554 else switch(pci_pa & 0xffff) { 555 case 0x2000: 556 pp->pp_id = PSYCHO_PBM_A; 557 break; 558 case 0x4000: 559 pp->pp_id = PSYCHO_PBM_B; 560 break; 561 default: 562 pp->pp_id = PSYCHO_PBM_UNKNOWN; 563 break; 564 } 565 } 566 567 void 568 psycho_map_psycho(struct psycho_softc* sc, int do_map, bus_addr_t reg_addr, 569 bus_size_t reg_size, bus_addr_t pci_addr, bus_size_t pci_size) 570 { 571 if (do_map) { 572 if (bus_space_map(sc->sc_bustag, 573 reg_addr, reg_size, 0, &sc->sc_regsh)) 574 panic("psycho_attach: cannot map regs"); 575 576 if (pci_addr >= reg_addr && 577 pci_addr + pci_size <= reg_addr + reg_size) { 578 if (bus_space_subregion(sc->sc_bustag, sc->sc_regsh, 579 pci_addr - reg_addr, pci_size, &sc->sc_pcictl)) 580 panic("psycho_map_psycho: map ctl"); 581 } 582 else if (bus_space_map(sc->sc_bustag, pci_addr, pci_size, 583 0, &sc->sc_pcictl)) 584 panic("psycho_map_psycho: cannot map pci"); 585 } else { 586 if (bus_space_map(sc->sc_bustag, reg_addr, reg_size, 587 BUS_SPACE_MAP_PROMADDRESS, &sc->sc_regsh)) 588 panic("psycho_map_psycho: cannot map ctl"); 589 if (bus_space_map(sc->sc_bustag, pci_addr, pci_size, 590 BUS_SPACE_MAP_PROMADDRESS, &sc->sc_pcictl)) 591 panic("psycho_map_psycho: cannot map pci"); 592 } 593 } 594 595 int 596 psycho_print(void *aux, const char *p) 597 { 598 if (p == NULL) 599 return (UNCONF); 600 return (QUIET); 601 } 602 603 void 604 psycho_set_intr(struct psycho_softc *sc, int ipl, void *handler, 605 u_int64_t *mapper, u_int64_t *clearer) 606 { 607 struct intrhand *ih; 608 609 ih = (struct intrhand *)malloc(sizeof(struct intrhand), 610 M_DEVBUF, M_NOWAIT); 611 if (ih == NULL) 612 panic("couldn't malloc intrhand"); 613 ih->ih_arg = sc; 614 ih->ih_map = mapper; 615 ih->ih_clr = clearer; 616 ih->ih_fun = handler; 617 ih->ih_pil = (1 << ipl); 618 ih->ih_number = INTVEC(*(ih->ih_map)); 619 620 DPRINTF(PDB_INTR, ( 621 "\ninstalling handler %p arg %p for %s with number %x pil %u", 622 ih->ih_fun, ih->ih_arg, sc->sc_dev.dv_xname, ih->ih_number, 623 ih->ih_pil)); 624 625 intr_establish(ipl, ih); 626 *(ih->ih_map) |= INTMAP_V; 627 } 628 629 /* 630 * PCI bus support 631 */ 632 633 /* 634 * allocate a PCI chipset tag and set it's cookie. 635 */ 636 pci_chipset_tag_t 637 psycho_alloc_chipset(struct psycho_pbm *pp, int node, pci_chipset_tag_t pc) 638 { 639 pci_chipset_tag_t npc; 640 641 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT); 642 if (npc == NULL) 643 panic("could not allocate pci_chipset_tag_t"); 644 memcpy(npc, pc, sizeof *pc); 645 npc->cookie = pp; 646 npc->rootnode = node; 647 npc->curnode = node; 648 649 return (npc); 650 } 651 652 /* 653 * grovel the OBP for various psycho properties 654 */ 655 void 656 psycho_get_bus_range(node, brp) 657 int node; 658 int *brp; 659 { 660 int n; 661 662 if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp)) 663 panic("could not get psycho bus-range"); 664 if (n != 2) 665 panic("broken psycho bus-range"); 666 DPRINTF(PDB_PROM, 667 ("psycho debug: got `bus-range' for node %08x: %u - %u\n", 668 node, brp[0], brp[1])); 669 } 670 671 void 672 psycho_get_ranges(int node, struct psycho_ranges **rp, int *np) 673 { 674 675 if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp)) 676 panic("could not get psycho ranges"); 677 DPRINTF(PDB_PROM, 678 ("psycho debug: got `ranges' for node %08x: %d entries\n", 679 node, *np)); 680 } 681 682 /* 683 * Interrupt handlers. 684 */ 685 686 int 687 psycho_ue(void *arg) 688 { 689 struct psycho_softc *sc = arg; 690 unsigned long long afsr = psycho_psychoreg_read(sc, psy_ue_afsr); 691 unsigned long long afar = psycho_psychoreg_read(sc, psy_ue_afar); 692 693 /* 694 * It's uncorrectable. Dump the regs and panic. 695 */ 696 panic("%s: uncorrectable DMA error AFAR %llx (pa=%llx tte=%llx/%llx) " 697 "AFSR %llx", sc->sc_dev.dv_xname, afar, 698 iommu_extract(sc->sc_is, (vaddr_t)afar), 699 iommu_lookup_tte(sc->sc_is, (vaddr_t)afar), 700 iommu_fetch_tte(sc->sc_is, (paddr_t)afar), 701 afsr); 702 return (1); 703 } 704 705 int 706 psycho_ce(void *arg) 707 { 708 struct psycho_softc *sc = arg; 709 710 /* 711 * It's correctable. Dump the regs and continue. 712 */ 713 714 printf("%s: correctable DMA error AFAR %llx AFSR %llx\n", 715 sc->sc_dev.dv_xname, 716 (long long)psycho_psychoreg_read(sc, psy_ce_afar), 717 (long long)psycho_psychoreg_read(sc, psy_ce_afsr)); 718 return (1); 719 } 720 721 int 722 psycho_bus_error(struct psycho_softc *sc, int bus) 723 { 724 u_int64_t afsr, afar, bits; 725 726 afar = psycho_psychoreg_read(sc, psy_pcictl[bus].pci_afar); 727 afsr = psycho_psychoreg_read(sc, psy_pcictl[bus].pci_afsr); 728 729 bits = afsr & (PSY_PCIAFSR_PMA | PSY_PCIAFSR_PTA | PSY_PCIAFSR_PTRY | 730 PSY_PCIAFSR_PPERR | PSY_PCIAFSR_SMA | PSY_PCIAFSR_STA | 731 PSY_PCIAFSR_STRY | PSY_PCIAFSR_SPERR); 732 733 if (bits == 0) 734 return (0); 735 736 /* 737 * It's uncorrectable. Dump the regs and panic. 738 */ 739 printf("%s: PCI bus %c error AFAR %llx (pa=%llx) AFSR %llx\n", 740 sc->sc_dev.dv_xname, 'A' + bus, (long long)afar, 741 (long long)iommu_extract(sc->sc_is, (vaddr_t)afar), 742 (long long)afsr); 743 744 psycho_psychoreg_write(sc, psy_pcictl[bus].pci_afsr, bits); 745 return (1); 746 } 747 748 int 749 psycho_bus_a(void *arg) 750 { 751 struct psycho_softc *sc = arg; 752 753 return (psycho_bus_error(sc, 0)); 754 } 755 756 int 757 psycho_bus_b(void *arg) 758 { 759 struct psycho_softc *sc = arg; 760 761 return (psycho_bus_error(sc, 1)); 762 } 763 764 int 765 psycho_powerfail(void *arg) 766 { 767 /* 768 * We lost power. Try to shut down NOW. 769 */ 770 printf("Power Failure Detected: Shutting down NOW.\n"); 771 boot(RB_POWERDOWN|RB_HALT); 772 return (1); 773 } 774 775 int 776 psycho_wakeup(void *arg) 777 { 778 struct psycho_softc *sc = arg; 779 780 /* 781 * Gee, we don't really have a framework to deal with this 782 * properly. 783 */ 784 printf("%s: power management wakeup\n", sc->sc_dev.dv_xname); 785 return (1); 786 } 787 788 /* 789 * initialise the IOMMU.. 790 */ 791 void 792 psycho_iommu_init(struct psycho_softc *sc, int tsbsize) 793 { 794 char *name; 795 struct iommu_state *is = sc->sc_is; 796 u_int32_t iobase = -1; 797 int *vdma = NULL; 798 int nitem; 799 800 /* punch in our copies */ 801 is->is_bustag = sc->sc_bustag; 802 bus_space_subregion(sc->sc_bustag, sc->sc_regsh, 803 offsetof(struct psychoreg, psy_iommu), sizeof(struct iommureg), 804 &is->is_iommu); 805 806 /* 807 * Separate the men from the boys. Get the `virtual-dma' 808 * property for sabre and use that to make sure the damn 809 * iommu works. 810 * 811 * We could query the `#virtual-dma-size-cells' and 812 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy. 813 */ 814 if (!getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem, 815 (void **)&vdma)) { 816 /* Damn. Gotta use these values. */ 817 iobase = vdma[0]; 818 #define TSBCASE(x) case 1 << ((x) + 23): tsbsize = (x); break 819 switch (vdma[1]) { 820 TSBCASE(1); TSBCASE(2); TSBCASE(3); 821 TSBCASE(4); TSBCASE(5); TSBCASE(6); 822 default: 823 printf("bogus tsb size %x, using 7\n", vdma[1]); 824 TSBCASE(7); 825 } 826 #undef TSBCASE 827 DPRINTF(PDB_CONF, ("psycho_iommu_init: iobase=0x%x\n", iobase)); 828 } 829 else { 830 DPRINTF(PDB_CONF, ("psycho_iommu_init: getprop failed, " 831 "iobase=0x%x\n", iobase)); 832 } 833 834 /* give us a nice name.. */ 835 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT); 836 if (name == NULL) 837 panic("couldn't malloc iommu name"); 838 snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname); 839 840 iommu_init(name, is, tsbsize, iobase); 841 } 842 843 /* 844 * below here is bus space and bus dma support 845 */ 846 847 bus_space_tag_t 848 psycho_alloc_mem_tag(struct psycho_pbm *pp) 849 { 850 return (_psycho_alloc_bus_tag(pp, "mem", 851 0x02, /* 32-bit mem space (where's the #define???) */ 852 ASI_PRIMARY, ASI_PRIMARY_LITTLE)); 853 } 854 855 bus_space_tag_t 856 psycho_alloc_io_tag(struct psycho_pbm *pp) 857 { 858 return (_psycho_alloc_bus_tag(pp, "io", 859 0x01, /* IO space (where's the #define???) */ 860 ASI_PHYS_NON_CACHED_LITTLE, ASI_PHYS_NON_CACHED)); 861 } 862 863 bus_space_tag_t 864 psycho_alloc_config_tag(struct psycho_pbm *pp) 865 { 866 return (_psycho_alloc_bus_tag(pp, "cfg", 867 0x00, /* Config space (where's the #define???) */ 868 ASI_PHYS_NON_CACHED_LITTLE, ASI_PHYS_NON_CACHED)); 869 } 870 871 bus_space_tag_t 872 _psycho_alloc_bus_tag(struct psycho_pbm *pp, 873 const char *name, int ss, int asi, int sasi) 874 { 875 struct psycho_softc *sc = pp->pp_sc; 876 struct sparc_bus_space_tag *bt; 877 878 bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT); 879 if (bt == NULL) 880 panic("could not allocate psycho bus tag"); 881 882 bzero(bt, sizeof *bt); 883 884 snprintf(bt->name, sizeof(bt->name), "%s-pbm_%s(%d-%2.2x)", 885 sc->sc_dev.dv_xname, name, ss, asi); 886 887 bt->cookie = pp; 888 bt->parent = sc->sc_bustag; 889 bt->default_type = ss; 890 bt->asi = asi; 891 bt->sasi = sasi; 892 bt->sparc_bus_map = _psycho_bus_map; 893 bt->sparc_bus_mmap = psycho_bus_mmap; 894 bt->sparc_intr_establish = psycho_intr_establish; 895 896 return (bt); 897 } 898 899 bus_dma_tag_t 900 psycho_alloc_dma_tag(struct psycho_pbm *pp) 901 { 902 struct psycho_softc *sc = pp->pp_sc; 903 bus_dma_tag_t dt, pdt = sc->sc_dmatag; 904 905 dt = (bus_dma_tag_t) 906 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT); 907 if (dt == NULL) 908 panic("could not allocate psycho dma tag"); 909 910 bzero(dt, sizeof *dt); 911 dt->_cookie = pp; 912 dt->_parent = pdt; 913 dt->_dmamap_create = psycho_dmamap_create; 914 dt->_dmamap_destroy = iommu_dvmamap_destroy; 915 dt->_dmamap_load = iommu_dvmamap_load; 916 dt->_dmamap_load_raw = iommu_dvmamap_load_raw; 917 dt->_dmamap_unload = iommu_dvmamap_unload; 918 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) 919 dt->_dmamap_sync = iommu_dvmamap_sync; 920 else 921 dt->_dmamap_sync = psycho_sabre_dvmamap_sync; 922 dt->_dmamem_alloc = iommu_dvmamem_alloc; 923 dt->_dmamem_free = iommu_dvmamem_free; 924 dt->_dmamem_map = iommu_dvmamem_map; 925 dt->_dmamem_unmap = iommu_dvmamem_unmap; 926 927 return (dt); 928 } 929 930 /* 931 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about 932 * PCI physical addresses. 933 */ 934 935 int 936 _psycho_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t offset, 937 bus_size_t size, int flags, bus_space_handle_t *hp) 938 { 939 struct psycho_pbm *pp = t->cookie; 940 int i, ss; 941 942 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: type %d off %qx sz %qx " 943 "flags %d", t->default_type, (unsigned long long)offset, 944 (unsigned long long)size, flags)); 945 946 ss = t->default_type; 947 DPRINTF(PDB_BUSMAP, (" cspace %d", ss)); 948 949 if (t->parent == 0 || t->parent->sparc_bus_map == 0) { 950 printf("\n_psycho_bus_map: invalid parent"); 951 return (EINVAL); 952 } 953 954 t = t->parent; 955 956 if (flags & BUS_SPACE_MAP_PROMADDRESS) { 957 return ((*t->sparc_bus_map) 958 (t, t0, offset, size, flags, hp)); 959 } 960 961 for (i = 0; i < pp->pp_nrange; i++) { 962 bus_addr_t paddr; 963 964 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss) 965 continue; 966 967 paddr = pp->pp_range[i].phys_lo + offset; 968 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi << 32); 969 DPRINTF(PDB_BUSMAP, 970 ("\n_psycho_bus_map: mapping paddr space %lx offset %lx " 971 "paddr %qx", 972 (long)ss, (long)offset, 973 (unsigned long long)paddr)); 974 return ((*t->sparc_bus_map)(t, t0, paddr, size, flags, hp)); 975 } 976 DPRINTF(PDB_BUSMAP, (" FAILED\n")); 977 return (EINVAL); 978 } 979 980 paddr_t 981 psycho_bus_mmap(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t paddr, 982 off_t off, int prot, int flags) 983 { 984 bus_addr_t offset = paddr; 985 struct psycho_pbm *pp = t->cookie; 986 int i, ss; 987 988 ss = t->default_type; 989 990 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: prot %d flags %d pa %qx", 991 prot, flags, (unsigned long long)paddr)); 992 993 if (t->parent == 0 || t->parent->sparc_bus_mmap == 0) { 994 printf("\n_psycho_bus_mmap: invalid parent"); 995 return (-1); 996 } 997 998 t = t->parent; 999 1000 for (i = 0; i < pp->pp_nrange; i++) { 1001 bus_addr_t paddr; 1002 1003 if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss) 1004 continue; 1005 1006 paddr = pp->pp_range[i].phys_lo + offset; 1007 paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi << 32); 1008 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr " 1009 "space %lx offset %lx paddr %qx", 1010 (long)ss, (long)offset, 1011 (unsigned long long)paddr)); 1012 return ((*t->sparc_bus_mmap)(t, t0, paddr, off, prot, flags)); 1013 } 1014 1015 return (-1); 1016 } 1017 1018 /* 1019 * Bus-specific interrupt mapping 1020 */ 1021 int 1022 psycho_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp) 1023 { 1024 struct psycho_pbm *pp = pa->pa_pc->cookie; 1025 struct psycho_softc *sc = pp->pp_sc; 1026 u_int dev; 1027 u_int ino; 1028 1029 ino = *ihp; 1030 1031 if ((ino & ~INTMAP_PCIINT) == 0) { 1032 /* 1033 * This deserves some documentation. Should anyone 1034 * have anything official looking, please speak up. 1035 */ 1036 if (sc->sc_mode == PSYCHO_MODE_PSYCHO && 1037 pp->pp_id == PSYCHO_PBM_B) 1038 dev = pa->pa_device - 2; 1039 else 1040 dev = pa->pa_device - 1; 1041 1042 if (ino == 0 || ino > 4) { 1043 u_int32_t intreg; 1044 1045 intreg = pci_conf_read(pa->pa_pc, pa->pa_tag, 1046 PCI_INTERRUPT_REG); 1047 1048 ino = PCI_INTERRUPT_PIN(intreg) - 1; 1049 } else 1050 ino -= 1; 1051 1052 ino &= INTMAP_PCIINT; 1053 1054 ino |= sc->sc_ign; 1055 ino |= ((pp->pp_id == PSYCHO_PBM_B) ? INTMAP_PCIBUS : 0); 1056 ino |= (dev << 2) & INTMAP_PCISLOT; 1057 1058 *ihp = ino; 1059 } 1060 1061 return (0); 1062 } 1063 1064 /* 1065 * install an interrupt handler for a PCI device 1066 */ 1067 void * 1068 psycho_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle, 1069 int level, int flags, int (*handler)(void *), void *arg, const char *what) 1070 { 1071 struct psycho_pbm *pp = t->cookie; 1072 struct psycho_softc *sc = pp->pp_sc; 1073 struct intrhand *ih; 1074 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL; 1075 int64_t intrmap = 0; 1076 int ino; 1077 long vec = INTVEC(ihandle); 1078 1079 /* 1080 * Hunt through all the interrupt mapping regs to look for our 1081 * interrupt vector. 1082 * 1083 * XXX We only compare INOs rather than IGNs since the firmware may 1084 * not provide the IGN and the IGN is constant for all device on that 1085 * PCI controller. This could cause problems for the FFB/external 1086 * interrupt which has a full vector that can be set arbitrarily. 1087 */ 1088 1089 1090 DPRINTF(PDB_INTR, 1091 ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec)); 1092 ino = INTINO(vec); 1093 DPRINTF(PDB_INTR, (" ino %x", ino)); 1094 1095 /* If the device didn't ask for an IPL, use the one encoded. */ 1096 if (level == IPL_NONE) 1097 level = INTLEV(vec); 1098 /* If it still has no level, print a warning and assign IPL 2 */ 1099 if (level == IPL_NONE) { 1100 printf("ERROR: no IPL, setting IPL 2.\n"); 1101 level = 2; 1102 } 1103 1104 if (flags & BUS_INTR_ESTABLISH_SOFTINTR) 1105 goto found; 1106 1107 DPRINTF(PDB_INTR, 1108 ("\npsycho: intr %lx: %p\nHunting for IRQ...\n", 1109 (long)ino, intrlev[ino])); 1110 1111 /* Hunt thru obio first */ 1112 for (intrmapptr = psycho_psychoreg_vaddr(sc, scsi_int_map), 1113 intrclrptr = psycho_psychoreg_vaddr(sc, scsi_clr_int); 1114 intrmapptr < (volatile u_int64_t *) 1115 psycho_psychoreg_vaddr(sc, ffb0_int_map); 1116 intrmapptr++, intrclrptr++) { 1117 if (INTINO(*intrmapptr) == ino) 1118 goto found; 1119 } 1120 1121 /* Now do PCI interrupts */ 1122 for (intrmapptr = psycho_psychoreg_vaddr(sc, pcia_slot0_int), 1123 intrclrptr = psycho_psychoreg_vaddr(sc, pcia0_clr_int[0]); 1124 intrmapptr <= (volatile u_int64_t *) 1125 psycho_psychoreg_vaddr(sc, pcib_slot3_int); 1126 intrmapptr++, intrclrptr += 4) { 1127 /* Skip PCI-A Slot 2 and PCI-A Slot 3 on psycho's */ 1128 if (sc->sc_mode == PSYCHO_MODE_PSYCHO && 1129 (intrmapptr == 1130 psycho_psychoreg_vaddr(sc, pcia_slot2_int) || 1131 intrmapptr == 1132 psycho_psychoreg_vaddr(sc, pcia_slot3_int))) 1133 continue; 1134 1135 if (((*intrmapptr ^ vec) & 0x3c) == 0) { 1136 intrclrptr += vec & 0x3; 1137 goto found; 1138 } 1139 } 1140 printf("Cannot find interrupt vector %lx\n", vec); 1141 return (NULL); 1142 1143 found: 1144 ih = bus_intr_allocate(t0, handler, arg, ino | sc->sc_ign, level, 1145 intrmapptr, intrclrptr, what); 1146 if (ih == NULL) { 1147 printf("Cannot allocate interrupt vector %lx\n", vec); 1148 return (NULL); 1149 } 1150 1151 DPRINTF(PDB_INTR, ( 1152 "\ninstalling handler %p arg %p with number %x pil %u", 1153 ih->ih_fun, ih->ih_arg, ih->ih_number, ih->ih_pil)); 1154 1155 intr_establish(ih->ih_pil, ih); 1156 1157 /* 1158 * Enable the interrupt now we have the handler installed. 1159 * Read the current value as we can't change it besides the 1160 * valid bit so so make sure only this bit is changed. 1161 * 1162 * XXXX --- we really should use bus_space for this. 1163 */ 1164 if (intrmapptr) { 1165 intrmap = *intrmapptr; 1166 DPRINTF(PDB_INTR, ("; read intrmap = %016qx", 1167 (unsigned long long)intrmap)); 1168 1169 /* Enable the interrupt */ 1170 intrmap |= INTMAP_V; 1171 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr)); 1172 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx", 1173 (unsigned long long)intrmap)); 1174 *intrmapptr = intrmap; 1175 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx", 1176 (unsigned long long)(intrmap = *intrmapptr))); 1177 } 1178 return (ih); 1179 } 1180 1181 /* 1182 * hooks into the iommu dvma calls. 1183 */ 1184 int 1185 psycho_dmamap_create(bus_dma_tag_t t, bus_dma_tag_t t0, bus_size_t size, 1186 int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags, 1187 bus_dmamap_t *dmamp) 1188 { 1189 struct psycho_pbm *pp = t->_cookie; 1190 1191 return (iommu_dvmamap_create(t, t0, &pp->pp_sb, size, nsegments, 1192 maxsegsz, boundary, flags, dmamp)); 1193 } 1194 1195 void 1196 psycho_sabre_dvmamap_sync(bus_dma_tag_t t, bus_dma_tag_t t0, bus_dmamap_t map, 1197 bus_size_t offset, bus_size_t len, int ops) 1198 { 1199 struct psycho_pbm *pp = t->_cookie; 1200 struct psycho_softc *sc = pp->pp_sc; 1201 1202 if (ops & BUS_DMASYNC_POSTREAD) 1203 psycho_psychoreg_read(sc, pci_dma_write_sync); 1204 1205 if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_PREWRITE)) 1206 membar(MemIssue); 1207 } 1208 1209