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