1 /* $NetBSD: psycho.c,v 1.98 2009/12/06 01:35:40 nakayama 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.98 2009/12/06 01:35:40 nakayama 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 int psycho_debug = 0x0; 74 #define DPRINTF(l, s) do { if (psycho_debug & l) printf s; } while (0) 75 #else 76 #define DPRINTF(l, s) 77 #endif 78 79 #include <sys/param.h> 80 #include <sys/device.h> 81 #include <sys/errno.h> 82 #include <sys/extent.h> 83 #include <sys/malloc.h> 84 #include <sys/systm.h> 85 #include <sys/time.h> 86 #include <sys/reboot.h> 87 88 #include <uvm/uvm.h> 89 90 #define _SPARC_BUS_DMA_PRIVATE 91 #include <machine/bus.h> 92 #include <machine/autoconf.h> 93 #include <machine/psl.h> 94 95 #include <dev/pci/pcivar.h> 96 #include <dev/pci/pcireg.h> 97 #include <dev/sysmon/sysmon_taskq.h> 98 99 #include <sparc64/dev/iommureg.h> 100 #include <sparc64/dev/iommuvar.h> 101 #include <sparc64/dev/psychoreg.h> 102 #include <sparc64/dev/psychovar.h> 103 104 #include "ioconf.h" 105 106 static pci_chipset_tag_t psycho_alloc_chipset(struct psycho_pbm *, int, 107 pci_chipset_tag_t); 108 static struct extent *psycho_alloc_extent(struct psycho_pbm *, int, int, 109 const char *); 110 static void psycho_get_bus_range(int, int *); 111 static void psycho_get_ranges(int, struct psycho_ranges **, int *); 112 static void psycho_set_intr(struct psycho_softc *, int, void *, uint64_t *, 113 uint64_t *); 114 115 /* chipset handlers */ 116 static pcireg_t psycho_pci_conf_read(pci_chipset_tag_t, pcitag_t, int); 117 static void psycho_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, 118 pcireg_t); 119 static int psycho_pci_intr_map(struct pci_attach_args *, 120 pci_intr_handle_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(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(struct device *, struct cfdata *, void *); 167 static void psycho_attach(struct device *, struct device *, void *); 168 static int psycho_print(void *aux, const char *p); 169 170 CFATTACH_DECL(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 static int 226 psycho_match(struct device *parent, struct cfdata *match, void *aux) 227 { 228 struct mainbus_attach_args *ma = aux; 229 char *model = prom_getpropstring(ma->ma_node, "model"); 230 int i; 231 232 /* match on a name of "pci" and a sabre or a psycho */ 233 if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) { 234 for (i=0; psycho_names[i].p_name; i++) 235 if (strcmp(model, psycho_names[i].p_name) == 0) 236 return (1); 237 238 model = prom_getpropstring(ma->ma_node, "compatible"); 239 for (i=0; psycho_names[i].p_name; i++) 240 if (strcmp(model, psycho_names[i].p_name) == 0) 241 return (1); 242 } 243 return (0); 244 } 245 246 #ifdef DEBUG 247 static void psycho_dump_intmap(struct psycho_softc *sc); 248 static void 249 psycho_dump_intmap(struct psycho_softc *sc) 250 { 251 volatile uint64_t *intrmapptr = NULL; 252 253 printf("psycho_dump_intmap: OBIO\n"); 254 255 for (intrmapptr = &sc->sc_regs->scsi_int_map; 256 intrmapptr < &sc->sc_regs->ue_int_map; 257 intrmapptr++) 258 printf("%p: %llx\n", intrmapptr, 259 (unsigned long long)*intrmapptr); 260 261 printf("\tintmap:pci\n"); 262 for (intrmapptr = &sc->sc_regs->pcia_slot0_int; 263 intrmapptr <= &sc->sc_regs->pcib_slot3_int; 264 intrmapptr++) 265 printf("%p: %llx\n", intrmapptr, 266 (unsigned long long)*intrmapptr); 267 268 printf("\tintmap:ffb\n"); 269 for (intrmapptr = &sc->sc_regs->ffb0_int_map; 270 intrmapptr <= &sc->sc_regs->ffb1_int_map; 271 intrmapptr++) 272 printf("%p: %llx\n", intrmapptr, 273 (unsigned long long)*intrmapptr); 274 } 275 #endif 276 277 /* 278 * SUNW,psycho initialisation .. 279 * - find the per-psycho registers 280 * - figure out the IGN. 281 * - find our partner psycho 282 * - configure ourselves 283 * - bus range, bus, 284 * - get interrupt-map and interrupt-map-mask 285 * - setup the chipsets. 286 * - if we're the first of the pair, initialise the IOMMU, otherwise 287 * just copy it's tags and addresses. 288 */ 289 static void 290 psycho_attach(struct device *parent, struct device *self, void *aux) 291 { 292 struct psycho_softc *sc = (struct psycho_softc *)self; 293 struct psycho_softc *osc = NULL; 294 struct psycho_pbm *pp; 295 struct pcibus_attach_args pba; 296 struct mainbus_attach_args *ma = aux; 297 struct psycho_ranges *pr; 298 prop_dictionary_t dict; 299 bus_space_handle_t bh; 300 uint64_t csr, mem_base; 301 int psycho_br[2], n, i; 302 bus_space_handle_t pci_ctl; 303 char *model = prom_getpropstring(ma->ma_node, "model"); 304 305 aprint_normal("\n"); 306 307 sc->sc_node = ma->ma_node; 308 sc->sc_bustag = ma->ma_bustag; 309 sc->sc_dmatag = ma->ma_dmatag; 310 311 /* 312 * Identify the device. 313 */ 314 for (i=0; psycho_names[i].p_name; i++) 315 if (strcmp(model, psycho_names[i].p_name) == 0) { 316 sc->sc_mode = psycho_names[i].p_type; 317 goto found; 318 } 319 320 model = prom_getpropstring(ma->ma_node, "compatible"); 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 panic("unknown psycho model %s", model); 328 found: 329 330 /* 331 * The psycho gets three register banks: 332 * (0) per-PBM configuration and status registers 333 * (1) per-PBM PCI configuration space, containing only the 334 * PBM 256-byte PCI header 335 * (2) the shared psycho configuration registers (struct psychoreg) 336 */ 337 338 /* Register layouts are different. stuupid. */ 339 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) { 340 sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr; 341 342 if (ma->ma_naddress > 2) { 343 sparc_promaddr_to_handle(sc->sc_bustag, 344 ma->ma_address[2], &sc->sc_bh); 345 sparc_promaddr_to_handle(sc->sc_bustag, 346 ma->ma_address[0], &pci_ctl); 347 348 sc->sc_regs = (struct psychoreg *) 349 bus_space_vaddr(sc->sc_bustag, sc->sc_bh); 350 } else if (ma->ma_nreg > 2) { 351 352 /* We need to map this in ourselves. */ 353 if (bus_space_map(sc->sc_bustag, 354 ma->ma_reg[2].ur_paddr, 355 ma->ma_reg[2].ur_len, BUS_SPACE_MAP_LINEAR, 356 &sc->sc_bh)) 357 panic("psycho_attach: cannot map regs"); 358 sc->sc_regs = (struct psychoreg *) 359 bus_space_vaddr(sc->sc_bustag, sc->sc_bh); 360 361 if (bus_space_map(sc->sc_bustag, 362 ma->ma_reg[0].ur_paddr, 363 ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR, 364 &pci_ctl)) 365 panic("psycho_attach: cannot map ctl"); 366 } else 367 panic("psycho_attach: %d not enough registers", 368 ma->ma_nreg); 369 370 } else { 371 sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr; 372 373 if (ma->ma_naddress) { 374 sparc_promaddr_to_handle(sc->sc_bustag, 375 ma->ma_address[0], &sc->sc_bh); 376 sc->sc_regs = (struct psychoreg *) 377 bus_space_vaddr(sc->sc_bustag, sc->sc_bh); 378 bus_space_subregion(sc->sc_bustag, sc->sc_bh, 379 offsetof(struct psychoreg, psy_pcictl), 380 sizeof(struct pci_ctl), &pci_ctl); 381 } else if (ma->ma_nreg) { 382 383 /* We need to map this in ourselves. */ 384 if (bus_space_map(sc->sc_bustag, 385 ma->ma_reg[0].ur_paddr, 386 ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR, 387 &sc->sc_bh)) 388 panic("psycho_attach: cannot map regs"); 389 sc->sc_regs = (struct psychoreg *) 390 bus_space_vaddr(sc->sc_bustag, sc->sc_bh); 391 392 bus_space_subregion(sc->sc_bustag, sc->sc_bh, 393 offsetof(struct psychoreg, psy_pcictl), 394 sizeof(struct pci_ctl), &pci_ctl); 395 } else 396 panic("psycho_attach: %d not enough registers", 397 ma->ma_nreg); 398 } 399 400 401 csr = bus_space_read_8(sc->sc_bustag, sc->sc_bh, 402 offsetof(struct psychoreg, psy_csr)); 403 sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */ 404 if (sc->sc_mode == PSYCHO_MODE_PSYCHO) 405 sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6; 406 407 aprint_normal_dev(self, "%s: impl %d, version %d: ign %x ", 408 model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr), 409 sc->sc_ign); 410 /* 411 * Match other psycho's that are already configured against 412 * the base physical address. This will be the same for a 413 * pair of devices that share register space. 414 */ 415 for (n = 0; n < psycho_cd.cd_ndevs; n++) { 416 417 struct psycho_softc *asc = device_lookup_private(&psycho_cd, n); 418 419 if (asc == NULL || asc == sc) 420 /* This entry is not there or it is me */ 421 continue; 422 423 if (asc->sc_basepaddr != sc->sc_basepaddr) 424 /* This is an unrelated psycho */ 425 continue; 426 427 /* Found partner */ 428 osc = asc; 429 break; 430 } 431 432 433 /* Oh, dear. OK, lets get started */ 434 435 /* 436 * Setup the PCI control register 437 */ 438 csr = bus_space_read_8(sc->sc_bustag, pci_ctl, 439 offsetof(struct pci_ctl, pci_csr)); 440 csr |= PCICTL_MRLM | 441 PCICTL_ARB_PARK | 442 PCICTL_ERRINTEN | 443 PCICTL_4ENABLE; 444 csr &= ~(PCICTL_SERR | 445 PCICTL_CPU_PRIO | 446 PCICTL_ARB_PRIO | 447 PCICTL_RTRYWAIT); 448 bus_space_write_8(sc->sc_bustag, pci_ctl, 449 offsetof(struct pci_ctl, pci_csr), csr); 450 451 452 /* 453 * Allocate our psycho_pbm 454 */ 455 pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, 456 M_NOWAIT | M_ZERO); 457 if (pp == NULL) 458 panic("could not allocate psycho pbm"); 459 460 pp->pp_sc = sc; 461 462 /* grab the psycho ranges */ 463 psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange); 464 465 /* get the bus-range for the psycho */ 466 psycho_get_bus_range(sc->sc_node, psycho_br); 467 468 pba.pba_bus = psycho_br[0]; 469 pba.pba_bridgetag = NULL; 470 471 aprint_normal("bus range %u to %u", psycho_br[0], psycho_br[1]); 472 aprint_normal("; PCI bus %d", psycho_br[0]); 473 474 pp->pp_pcictl = pci_ctl; 475 476 /* allocate our tags */ 477 pp->pp_memt = psycho_alloc_mem_tag(pp); 478 pp->pp_iot = psycho_alloc_io_tag(pp); 479 pp->pp_dmat = psycho_alloc_dma_tag(pp); 480 pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) | 481 (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0); 482 483 /* allocate a chipset for this */ 484 pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset); 485 pp->pp_pc->spc_busmax = psycho_br[1]; 486 487 switch((ma->ma_reg[0].ur_paddr) & 0xf000) { 488 case 0x2000: 489 pp->pp_id = PSYCHO_PBM_A; 490 break; 491 case 0x4000: 492 pp->pp_id = PSYCHO_PBM_B; 493 break; 494 } 495 496 aprint_normal("\n"); 497 498 /* allocate extents for free bus space */ 499 pp->pp_exmem = psycho_alloc_extent(pp, sc->sc_node, 0x02, "psycho mem"); 500 pp->pp_exio = psycho_alloc_extent(pp, sc->sc_node, 0x01, "psycho io"); 501 502 #ifdef DEBUG 503 if (psycho_debug & PDB_INTR) 504 psycho_dump_intmap(sc); 505 #endif 506 507 /* 508 * And finally, if we're a sabre or the first of a pair of psycho's to 509 * arrive here, start up the IOMMU and get a config space tag. 510 */ 511 if (osc == NULL) { 512 uint64_t timeo; 513 514 /* 515 * Establish handlers for interesting interrupts.... 516 * 517 * XXX We need to remember these and remove this to support 518 * hotplug on the UPA/FHC bus. 519 * 520 * XXX Not all controllers have these, but installing them 521 * is better than trying to sort through this mess. 522 */ 523 psycho_set_intr(sc, 15, psycho_ue, 524 &sc->sc_regs->ue_int_map, 525 &sc->sc_regs->ue_clr_int); 526 psycho_set_intr(sc, 1, psycho_ce, 527 &sc->sc_regs->ce_int_map, 528 &sc->sc_regs->ce_clr_int); 529 psycho_set_intr(sc, 15, psycho_bus_a, 530 &sc->sc_regs->pciaerr_int_map, 531 &sc->sc_regs->pciaerr_clr_int); 532 psycho_set_intr(sc, 15, psycho_powerfail, 533 &sc->sc_regs->power_int_map, 534 &sc->sc_regs->power_clr_int); 535 psycho_register_power_button(sc); 536 if (sc->sc_mode != PSYCHO_MODE_SABRE) { 537 /* sabre doesn't have these interrupts */ 538 psycho_set_intr(sc, 15, psycho_bus_b, 539 &sc->sc_regs->pciberr_int_map, 540 &sc->sc_regs->pciberr_clr_int); 541 psycho_set_intr(sc, 1, psycho_wakeup, 542 &sc->sc_regs->pwrmgt_int_map, 543 &sc->sc_regs->pwrmgt_clr_int); 544 } 545 546 /* 547 * Apparently a number of machines with psycho and psycho+ 548 * controllers have interrupt latency issues. We'll try 549 * setting the interrupt retry timeout to 0xff which gives us 550 * a retry of 3-6 usec (which is what sysio is set to) for the 551 * moment, which seems to help alleviate this problem. 552 */ 553 timeo = sc->sc_regs->intr_retry_timer; 554 if (timeo > 0xfff) { 555 #ifdef DEBUG 556 printf("decreasing interrupt retry timeout " 557 "from %lx to 0xff\n", (long)timeo); 558 #endif 559 sc->sc_regs->intr_retry_timer = 0xff; 560 } 561 562 /* 563 * Allocate bus node, this contains a prom node per bus. 564 */ 565 pp->pp_pc->spc_busnode = 566 malloc(sizeof(*pp->pp_pc->spc_busnode), M_DEVBUF, 567 M_NOWAIT | M_ZERO); 568 if (pp->pp_pc->spc_busnode == NULL) 569 panic("psycho_attach: malloc busnode"); 570 571 /* 572 * Setup IOMMU and PCI configuration if we're the first 573 * of a pair of psycho's to arrive here. 574 * 575 * We should calculate a TSB size based on amount of RAM 576 * and number of bus controllers and number an type of 577 * child devices. 578 * 579 * For the moment, 32KB should be more than enough. 580 */ 581 sc->sc_is = malloc(sizeof(struct iommu_state), 582 M_DEVBUF, M_NOWAIT); 583 if (sc->sc_is == NULL) 584 panic("psycho_attach: malloc iommu_state"); 585 586 /* Point the strbuf_ctl at the iommu_state */ 587 pp->pp_sb.sb_is = sc->sc_is; 588 589 sc->sc_is->is_sb[0] = sc->sc_is->is_sb[1] = NULL; 590 if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) { 591 struct strbuf_ctl *sb = &pp->pp_sb; 592 vaddr_t va = (vaddr_t)&pp->pp_flush[0x40]; 593 594 /* 595 * Initialize the strbuf_ctl. 596 * 597 * The flush sync buffer must be 64-byte aligned. 598 */ 599 sb->sb_flush = (void *)(va & ~0x3f); 600 601 bus_space_subregion(sc->sc_bustag, pci_ctl, 602 offsetof(struct pci_ctl, pci_strbuf), 603 sizeof (struct iommu_strbuf), &sb->sb_sb); 604 605 /* Point our iommu at the strbuf_ctl */ 606 sc->sc_is->is_sb[0] = sb; 607 } 608 609 psycho_iommu_init(sc, 2); 610 611 sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this); 612 613 /* 614 * XXX This is a really ugly hack because PCI config space 615 * is explicitly handled with unmapped accesses. 616 */ 617 i = sc->sc_bustag->type; 618 sc->sc_bustag->type = PCI_CONFIG_BUS_SPACE; 619 if (bus_space_map(sc->sc_bustag, sc->sc_basepaddr + 0x01000000, 620 0x01000000, 0, &bh)) 621 panic("could not map psycho PCI configuration space"); 622 sc->sc_bustag->type = i; 623 sc->sc_configaddr = bh; 624 } else { 625 /* Share bus numbers with the pair of mine */ 626 pp->pp_pc->spc_busnode = 627 osc->sc_psycho_this->pp_pc->spc_busnode; 628 629 /* Just copy IOMMU state, config tag and address */ 630 sc->sc_is = osc->sc_is; 631 sc->sc_configtag = osc->sc_configtag; 632 sc->sc_configaddr = osc->sc_configaddr; 633 634 /* Point the strbuf_ctl at the iommu_state */ 635 pp->pp_sb.sb_is = sc->sc_is; 636 637 if (prom_getproplen(sc->sc_node, "no-streaming-cache") < 0) { 638 struct strbuf_ctl *sb = &pp->pp_sb; 639 vaddr_t va = (vaddr_t)&pp->pp_flush[0x40]; 640 641 /* 642 * Initialize the strbuf_ctl. 643 * 644 * The flush sync buffer must be 64-byte aligned. 645 */ 646 sb->sb_flush = (void *)(va & ~0x3f); 647 648 bus_space_subregion(sc->sc_bustag, pci_ctl, 649 offsetof(struct pci_ctl, pci_strbuf), 650 sizeof (struct iommu_strbuf), &sb->sb_sb); 651 652 /* Point our iommu at the strbuf_ctl */ 653 sc->sc_is->is_sb[1] = sb; 654 } 655 iommu_reset(sc->sc_is); 656 } 657 658 dict = device_properties(self); 659 pr = get_psychorange(pp, 2); /* memory range */ 660 #ifdef DEBUG 661 printf("memory range: %08x %08x\n", pr->phys_hi, pr->phys_lo); 662 #endif 663 mem_base = ((uint64_t)pr->phys_hi) << 32 | pr->phys_lo; 664 prop_dictionary_set_uint64(dict, "mem_base", mem_base); 665 666 /* 667 * attach the pci.. note we pass PCI A tags, etc., for the sabre here. 668 */ 669 pba.pba_flags = sc->sc_psycho_this->pp_flags; 670 pba.pba_dmat = sc->sc_psycho_this->pp_dmat; 671 pba.pba_dmat64 = NULL; 672 pba.pba_iot = sc->sc_psycho_this->pp_iot; 673 pba.pba_memt = sc->sc_psycho_this->pp_memt; 674 pba.pba_pc = pp->pp_pc; 675 676 config_found_ia(self, "pcibus", &pba, psycho_print); 677 } 678 679 static int 680 psycho_print(void *aux, const char *p) 681 { 682 683 if (p == NULL) 684 return (UNCONF); 685 return (QUIET); 686 } 687 688 static void 689 psycho_set_intr(struct psycho_softc *sc, int ipl, void *handler, 690 uint64_t *mapper, uint64_t *clearer) 691 { 692 struct intrhand *ih; 693 694 ih = (struct intrhand *)malloc(sizeof(struct intrhand), 695 M_DEVBUF, M_NOWAIT); 696 ih->ih_arg = sc; 697 ih->ih_map = mapper; 698 ih->ih_clr = clearer; 699 ih->ih_fun = handler; 700 ih->ih_pil = (1<<ipl); 701 ih->ih_number = INTVEC(*(ih->ih_map)); 702 intr_establish(ipl, ipl != IPL_VM, ih); 703 *(ih->ih_map) |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT); 704 } 705 706 /* 707 * power button handlers 708 */ 709 static void 710 psycho_register_power_button(struct psycho_softc *sc) 711 { 712 sysmon_task_queue_init(); 713 714 sc->sc_powerpressed = 0; 715 sc->sc_smcontext = malloc(sizeof(struct sysmon_pswitch), M_DEVBUF, 0); 716 if (!sc->sc_smcontext) { 717 aprint_error_dev(&sc->sc_dev, "could not allocate power button context\n"); 718 return; 719 } 720 memset(sc->sc_smcontext, 0, sizeof(struct sysmon_pswitch)); 721 sc->sc_smcontext->smpsw_name = device_xname(&sc->sc_dev); 722 sc->sc_smcontext->smpsw_type = PSWITCH_TYPE_POWER; 723 if (sysmon_pswitch_register(sc->sc_smcontext) != 0) 724 aprint_error_dev(&sc->sc_dev, "unable to register power button with sysmon\n"); 725 } 726 727 static void 728 psycho_power_button_pressed(void *arg) 729 { 730 struct psycho_softc *sc = arg; 731 732 sysmon_pswitch_event(sc->sc_smcontext, PSWITCH_EVENT_PRESSED); 733 sc->sc_powerpressed = 0; 734 } 735 736 /* 737 * PCI bus support 738 */ 739 740 /* 741 * allocate a PCI chipset tag and set it's cookie. 742 */ 743 static pci_chipset_tag_t 744 psycho_alloc_chipset(struct psycho_pbm *pp, int node, pci_chipset_tag_t pc) 745 { 746 pci_chipset_tag_t npc; 747 748 npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT); 749 if (npc == NULL) 750 panic("could not allocate pci_chipset_tag_t"); 751 memcpy(npc, pc, sizeof *pc); 752 npc->cookie = pp; 753 npc->rootnode = node; 754 npc->spc_conf_read = psycho_pci_conf_read; 755 npc->spc_conf_write = psycho_pci_conf_write; 756 npc->spc_intr_map = psycho_pci_intr_map; 757 npc->spc_intr_establish = psycho_pci_intr_establish; 758 npc->spc_find_ino = psycho_pci_find_ino; 759 760 return (npc); 761 } 762 763 /* 764 * create extent for free bus space, then allocate assigned regions. 765 */ 766 static struct extent * 767 psycho_alloc_extent(struct psycho_pbm *pp, int node, int ss, const char *name) 768 { 769 struct psycho_registers *pa = NULL; 770 struct psycho_ranges *pr; 771 struct extent *ex; 772 bus_addr_t baddr, addr; 773 bus_size_t bsize, size; 774 int i, num; 775 776 /* get bus space size */ 777 pr = get_psychorange(pp, ss); 778 if (pr == NULL) { 779 printf("psycho_alloc_extent: get_psychorange failed\n"); 780 return NULL; 781 } 782 baddr = 0x00000000; 783 bsize = BUS_ADDR(pr->size_hi, pr->size_lo); 784 785 /* get available lists */ 786 num = 0; 787 if (prom_getprop(node, "available", sizeof(*pa), &num, &pa)) { 788 printf("psycho_alloc_extent: no \"available\" property\n"); 789 return NULL; 790 } 791 792 /* create extent */ 793 ex = extent_create(name, baddr, bsize - baddr - 1, M_DEVBUF, 0, 0, 794 EX_NOWAIT); 795 if (ex == NULL) { 796 printf("psycho_alloc_extent: extent_create failed\n"); 797 goto ret; 798 } 799 800 /* allocate assigned regions */ 801 for (i = 0; i < num; i++) 802 if (((pa[i].phys_hi >> 24) & 0x03) == ss) { 803 /* allocate bus space */ 804 addr = BUS_ADDR(pa[i].phys_mid, pa[i].phys_lo); 805 size = BUS_ADDR(pa[i].size_hi, pa[i].size_lo); 806 if (extent_alloc_region(ex, baddr, addr - baddr, 807 EX_NOWAIT)) { 808 printf("psycho_alloc_extent: " 809 "extent_alloc_region %" PRIx64 "-%" 810 PRIx64 " failed\n", baddr, addr); 811 extent_destroy(ex); 812 ex = NULL; 813 goto ret; 814 } 815 baddr = addr + size; 816 } 817 /* allocate left region if available */ 818 if (baddr < bsize) 819 if (extent_alloc_region(ex, baddr, bsize - baddr, EX_NOWAIT)) { 820 printf("psycho_alloc_extent: extent_alloc_region %" 821 PRIx64 "-%" PRIx64 " failed\n", baddr, bsize); 822 extent_destroy(ex); 823 ex = NULL; 824 goto ret; 825 } 826 827 #ifdef DEBUG 828 /* print extent */ 829 extent_print(ex); 830 #endif 831 832 ret: 833 /* return extent */ 834 free(pa, M_DEVBUF); 835 return ex; 836 } 837 838 /* 839 * grovel the OBP for various psycho properties 840 */ 841 static void 842 psycho_get_bus_range(int node, int *brp) 843 { 844 int n, error; 845 846 n = 2; 847 error = prom_getprop(node, "bus-range", sizeof(*brp), &n, &brp); 848 if (error) 849 panic("could not get psycho bus-range, error %d", error); 850 if (n != 2) 851 panic("broken psycho bus-range"); 852 DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", 853 node, brp[0], brp[1])); 854 } 855 856 static void 857 psycho_get_ranges(int node, struct psycho_ranges **rp, int *np) 858 { 859 860 if (prom_getprop(node, "ranges", sizeof(**rp), np, rp)) 861 panic("could not get psycho ranges"); 862 DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np)); 863 } 864 865 /* 866 * Interrupt handlers. 867 */ 868 869 static int 870 psycho_ue(void *arg) 871 { 872 struct psycho_softc *sc = (struct psycho_softc *)arg; 873 struct psychoreg *regs = sc->sc_regs; 874 struct iommu_state *is = sc->sc_is; 875 uint64_t afsr = regs->psy_ue_afsr; 876 uint64_t afar = regs->psy_ue_afar; 877 psize_t size = PAGE_SIZE << is->is_tsbsize; 878 char bits[128]; 879 880 /* 881 * It's uncorrectable. Dump the regs and panic. 882 */ 883 snprintb(bits, sizeof(bits), PSYCHO_UE_AFSR_BITS, afsr); 884 aprint_error_dev(&sc->sc_dev, 885 "uncorrectable DMA error AFAR %" PRIx64 " AFSR %s\n", afar, bits); 886 887 /* Sometimes the AFAR points to an IOTSB entry */ 888 if (afar >= is->is_ptsb && afar < is->is_ptsb + size) { 889 aprint_error_dev(&sc->sc_dev, 890 "IOVA %" PRIx64 " IOTTE %" PRIx64 "\n", 891 (afar - is->is_ptsb) / sizeof(is->is_tsb[0]) * PAGE_SIZE 892 + is->is_dvmabase, ldxa(afar, ASI_PHYS_CACHED)); 893 } 894 #ifdef DDB 895 Debugger(); 896 #endif 897 regs->psy_ue_afar = 0; 898 regs->psy_ue_afsr = 0; 899 return (1); 900 } 901 902 static int 903 psycho_ce(void *arg) 904 { 905 struct psycho_softc *sc = (struct psycho_softc *)arg; 906 struct psychoreg *regs = sc->sc_regs; 907 908 /* 909 * It's correctable. Dump the regs and continue. 910 */ 911 aprint_error_dev(&sc->sc_dev, 912 "correctable DMA error AFAR %" PRIx64 " AFSR %" PRIx64 "\n", 913 regs->psy_ce_afar, regs->psy_ce_afsr); 914 return (1); 915 } 916 917 static int 918 psycho_bus_a(void *arg) 919 { 920 struct psycho_softc *sc = (struct psycho_softc *)arg; 921 struct psychoreg *regs = sc->sc_regs; 922 923 /* 924 * It's uncorrectable. Dump the regs and panic. 925 */ 926 927 panic("%s: PCI bus A error AFAR %" PRIx64 " AFSR %" PRIx64, 928 device_xname(&sc->sc_dev), 929 regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr); 930 return (1); 931 } 932 933 static int 934 psycho_bus_b(void *arg) 935 { 936 struct psycho_softc *sc = (struct psycho_softc *)arg; 937 struct psychoreg *regs = sc->sc_regs; 938 939 /* 940 * It's uncorrectable. Dump the regs and panic. 941 */ 942 943 panic("%s: PCI bus B error AFAR %" PRIx64 " AFSR %" PRIx64, 944 device_xname(&sc->sc_dev), 945 regs->psy_pcictl[0].pci_afar, regs->psy_pcictl[0].pci_afsr); 946 return (1); 947 } 948 949 static int 950 psycho_powerfail(void *arg) 951 { 952 struct psycho_softc *sc = (struct psycho_softc *)arg; 953 954 /* 955 * We lost power. Queue a callback with thread context to 956 * handle all the real work. 957 */ 958 if (sc->sc_powerpressed == 0 && sc->sc_smcontext != NULL) { 959 sc->sc_powerpressed = 1; 960 sysmon_task_queue_sched(0, psycho_power_button_pressed, sc); 961 } 962 return (1); 963 } 964 965 static 966 int psycho_wakeup(void *arg) 967 { 968 struct psycho_softc *sc = (struct psycho_softc *)arg; 969 970 /* 971 * Gee, we don't really have a framework to deal with this 972 * properly. 973 */ 974 aprint_error_dev(&sc->sc_dev, "power management wakeup\n"); 975 return (1); 976 } 977 978 979 /* 980 * initialise the IOMMU.. 981 */ 982 void 983 psycho_iommu_init(struct psycho_softc *sc, int tsbsize) 984 { 985 char *name; 986 struct iommu_state *is = sc->sc_is; 987 uint32_t iobase = -1; 988 int *vdma = NULL; 989 int nitem; 990 991 /* punch in our copies */ 992 is->is_bustag = sc->sc_bustag; 993 bus_space_subregion(sc->sc_bustag, sc->sc_bh, 994 offsetof(struct psychoreg, psy_iommu), 995 sizeof (struct iommureg), 996 &is->is_iommu); 997 998 /* 999 * Separate the men from the boys. Get the `virtual-dma' 1000 * property for sabre and use that to make sure the damn 1001 * iommu works. 1002 * 1003 * We could query the `#virtual-dma-size-cells' and 1004 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy. 1005 */ 1006 nitem = 0; 1007 if (!prom_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem, 1008 &vdma)) { 1009 /* Damn. Gotta use these values. */ 1010 iobase = vdma[0]; 1011 #define TSBCASE(x) case 1<<((x)+23): tsbsize = (x); break 1012 switch (vdma[1]) { 1013 TSBCASE(1); TSBCASE(2); TSBCASE(3); 1014 TSBCASE(4); TSBCASE(5); TSBCASE(6); 1015 default: 1016 printf("bogus tsb size %x, using 7\n", vdma[1]); 1017 TSBCASE(7); 1018 } 1019 #undef TSBCASE 1020 } 1021 1022 /* give us a nice name.. */ 1023 name = (char *)malloc(32, M_DEVBUF, M_NOWAIT); 1024 if (name == 0) 1025 panic("couldn't malloc iommu name"); 1026 snprintf(name, 32, "%s dvma", device_xname(&sc->sc_dev)); 1027 1028 iommu_init(name, is, tsbsize, iobase); 1029 } 1030 1031 /* 1032 * below here is bus space and bus DMA support 1033 */ 1034 bus_space_tag_t 1035 psycho_alloc_bus_tag(struct psycho_pbm *pp, int type) 1036 { 1037 struct psycho_softc *sc = pp->pp_sc; 1038 bus_space_tag_t bt; 1039 1040 bt = (bus_space_tag_t) malloc(sizeof(struct sparc_bus_space_tag), 1041 M_DEVBUF, M_NOWAIT | M_ZERO); 1042 if (bt == NULL) 1043 panic("could not allocate psycho bus tag"); 1044 1045 bt->cookie = pp; 1046 bt->parent = sc->sc_bustag; 1047 bt->type = type; 1048 bt->sparc_bus_map = _psycho_bus_map; 1049 bt->sparc_bus_mmap = psycho_bus_mmap; 1050 bt->sparc_intr_establish = psycho_intr_establish; 1051 return (bt); 1052 } 1053 1054 bus_dma_tag_t 1055 psycho_alloc_dma_tag(struct psycho_pbm *pp) 1056 { 1057 struct psycho_softc *sc = pp->pp_sc; 1058 bus_dma_tag_t dt, pdt = sc->sc_dmatag; 1059 1060 dt = (bus_dma_tag_t) 1061 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT); 1062 if (dt == NULL) 1063 panic("could not allocate psycho DMA tag"); 1064 1065 memset(dt, 0, sizeof *dt); 1066 dt->_cookie = pp; 1067 dt->_parent = pdt; 1068 #define PCOPY(x) dt->x = pdt->x 1069 dt->_dmamap_create = psycho_dmamap_create; 1070 PCOPY(_dmamap_destroy); 1071 dt->_dmamap_load = iommu_dvmamap_load; 1072 PCOPY(_dmamap_load_mbuf); 1073 PCOPY(_dmamap_load_uio); 1074 dt->_dmamap_load_raw = iommu_dvmamap_load_raw; 1075 dt->_dmamap_unload = iommu_dvmamap_unload; 1076 if (sc->sc_mode == PSYCHO_MODE_SABRE) 1077 dt->_dmamap_sync = psycho_sabre_dmamap_sync; 1078 else 1079 dt->_dmamap_sync = iommu_dvmamap_sync; 1080 dt->_dmamem_alloc = iommu_dvmamem_alloc; 1081 dt->_dmamem_free = iommu_dvmamem_free; 1082 dt->_dmamem_map = iommu_dvmamem_map; 1083 dt->_dmamem_unmap = iommu_dvmamem_unmap; 1084 PCOPY(_dmamem_mmap); 1085 #undef PCOPY 1086 return (dt); 1087 } 1088 1089 /* 1090 * bus space support. <sparc64/dev/psychoreg.h> has a discussion about 1091 * PCI physical addresses. 1092 */ 1093 1094 static struct psycho_ranges * 1095 get_psychorange(struct psycho_pbm *pp, int ss) 1096 { 1097 int i; 1098 1099 for (i = 0; i < pp->pp_nrange; i++) { 1100 if (((pp->pp_range[i].cspace >> 24) & 0x03) == ss) 1101 return (&pp->pp_range[i]); 1102 } 1103 /* not found */ 1104 return (NULL); 1105 } 1106 1107 static int 1108 _psycho_bus_map(bus_space_tag_t t, bus_addr_t offset, bus_size_t size, 1109 int flags, vaddr_t unused, bus_space_handle_t *hp) 1110 { 1111 struct psycho_pbm *pp = t->cookie; 1112 struct psycho_softc *sc = pp->pp_sc; 1113 struct psycho_ranges *pr; 1114 bus_addr_t paddr; 1115 int ss; 1116 1117 DPRINTF(PDB_BUSMAP, 1118 ("_psycho_bus_map: type %d off %qx sz %qx flags %d", 1119 t->type, (unsigned long long)offset, 1120 (unsigned long long)size, flags)); 1121 1122 ss = sparc_pci_childspace(t->type); 1123 DPRINTF(PDB_BUSMAP, (" cspace %d", ss)); 1124 1125 pr = get_psychorange(pp, ss); 1126 if (pr != NULL) { 1127 paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset); 1128 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr " 1129 "space %lx offset %lx paddr %qx\n", 1130 (long)ss, (long)offset, 1131 (unsigned long long)paddr)); 1132 return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size, 1133 flags, 0, hp)); 1134 } 1135 DPRINTF(PDB_BUSMAP, (" FAILED\n")); 1136 return (EINVAL); 1137 } 1138 1139 static paddr_t 1140 psycho_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot, 1141 int flags) 1142 { 1143 bus_addr_t offset = paddr; 1144 struct psycho_pbm *pp = t->cookie; 1145 struct psycho_softc *sc = pp->pp_sc; 1146 struct psycho_ranges *pr; 1147 int ss; 1148 1149 ss = sparc_pci_childspace(t->type); 1150 1151 DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n", 1152 prot, flags, (unsigned long long)paddr)); 1153 1154 pr = get_psychorange(pp, ss); 1155 if (pr != NULL) { 1156 paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset); 1157 DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr " 1158 "space %lx offset %lx paddr %qx\n", 1159 (long)ss, (long)offset, 1160 (unsigned long long)paddr)); 1161 return (bus_space_mmap(sc->sc_bustag, paddr, off, 1162 prot, flags)); 1163 } 1164 1165 return (-1); 1166 } 1167 1168 /* 1169 * Get a PCI offset address from bus_space_handle_t. 1170 */ 1171 bus_addr_t 1172 psycho_bus_offset(bus_space_tag_t t, bus_space_handle_t *hp) 1173 { 1174 struct psycho_pbm *pp = t->cookie; 1175 struct psycho_ranges *pr; 1176 bus_addr_t addr, offset; 1177 vaddr_t va; 1178 int ss; 1179 1180 addr = hp->_ptr; 1181 ss = sparc_pci_childspace(t->type); 1182 DPRINTF(PDB_BUSMAP, ("psycho_bus_offset: type %d addr %" PRIx64 1183 " cspace %d", t->type, addr, ss)); 1184 1185 pr = get_psychorange(pp, ss); 1186 if (pr != NULL) { 1187 if (!PHYS_ASI(hp->_asi)) { 1188 va = trunc_page((vaddr_t)addr); 1189 if (pmap_extract(pmap_kernel(), va, &addr) == FALSE) { 1190 DPRINTF(PDB_BUSMAP, 1191 ("\n pmap_extract FAILED\n")); 1192 return (-1); 1193 } 1194 addr += hp->_ptr & PGOFSET; 1195 } 1196 offset = BUS_ADDR_PADDR(addr) - pr->phys_lo; 1197 DPRINTF(PDB_BUSMAP, ("\npsycho_bus_offset: paddr %" PRIx64 1198 " offset %" PRIx64 "\n", addr, offset)); 1199 return (offset); 1200 } 1201 DPRINTF(PDB_BUSMAP, ("\n FAILED\n")); 1202 return (-1); 1203 } 1204 1205 1206 /* 1207 * install an interrupt handler for a PCI device 1208 */ 1209 void * 1210 psycho_intr_establish(bus_space_tag_t t, int ihandle, int level, 1211 int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */) 1212 { 1213 struct psycho_pbm *pp = t->cookie; 1214 struct psycho_softc *sc = pp->pp_sc; 1215 struct intrhand *ih; 1216 volatile uint64_t *intrmapptr = NULL, *intrclrptr = NULL; 1217 int64_t imap = 0; 1218 int ino; 1219 long vec = INTVEC(ihandle); 1220 1221 ih = (struct intrhand *) 1222 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT); 1223 if (ih == NULL) 1224 return (NULL); 1225 1226 /* 1227 * Hunt through all the interrupt mapping regs to look for our 1228 * interrupt vector. 1229 * 1230 * XXX We only compare INOs rather than IGNs since the firmware may 1231 * not provide the IGN and the IGN is constant for all device on that 1232 * PCI controller. This could cause problems for the FFB/external 1233 * interrupt which has a full vector that can be set arbitrarily. 1234 */ 1235 1236 DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec)); 1237 ino = INTINO(vec); 1238 DPRINTF(PDB_INTR, (" ino %x", ino)); 1239 1240 /* If the device didn't ask for an IPL, use the one encoded. */ 1241 if (level == IPL_NONE) level = INTLEV(vec); 1242 /* If it still has no level, print a warning and assign IPL 2 */ 1243 if (level == IPL_NONE) { 1244 printf("ERROR: no IPL, setting IPL 2.\n"); 1245 level = 2; 1246 } 1247 1248 DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n", 1249 (long)ino, intrlev[ino])); 1250 1251 /* 1252 * First look for PCI interrupts, otherwise the PCI A slot 0 1253 * INTA# interrupt might match an unused non-PCI (obio) 1254 * interrupt. 1255 */ 1256 for (intrmapptr = &sc->sc_regs->pcia_slot0_int, 1257 intrclrptr = &sc->sc_regs->pcia0_clr_int[0]; 1258 intrmapptr <= &sc->sc_regs->pcib_slot3_int; 1259 intrmapptr++, intrclrptr += 4) { 1260 if (sc->sc_mode == PSYCHO_MODE_PSYCHO && 1261 (intrmapptr == &sc->sc_regs->pcia_slot2_int || 1262 intrmapptr == &sc->sc_regs->pcia_slot3_int)) 1263 continue; 1264 if (((*intrmapptr ^ vec) & 0x3c) == 0) { 1265 intrclrptr += vec & 0x3; 1266 goto found; 1267 } 1268 } 1269 1270 /* Now hunt thru obio. */ 1271 for (intrmapptr = &sc->sc_regs->scsi_int_map, 1272 intrclrptr = &sc->sc_regs->scsi_clr_int; 1273 intrmapptr < &sc->sc_regs->ue_int_map; 1274 intrmapptr++, intrclrptr++) { 1275 if (INTINO(*intrmapptr) == ino) 1276 goto found; 1277 } 1278 1279 /* Finally check the two FFB slots */ 1280 intrclrptr = NULL; /* XXX? */ 1281 for (intrmapptr = &sc->sc_regs->ffb0_int_map; 1282 intrmapptr <= &sc->sc_regs->ffb1_int_map; 1283 intrmapptr++) { 1284 if (INTVEC(*intrmapptr) == ino) 1285 goto found; 1286 } 1287 1288 printf("Cannot find interrupt vector %lx\n", vec); 1289 return (NULL); 1290 1291 found: 1292 /* Register the map and clear intr registers */ 1293 ih->ih_map = intrmapptr; 1294 ih->ih_clr = intrclrptr; 1295 1296 ih->ih_fun = handler; 1297 ih->ih_arg = arg; 1298 ih->ih_pil = level; 1299 ih->ih_number = ino | sc->sc_ign; 1300 1301 DPRINTF(PDB_INTR, ( 1302 "; installing handler %p arg %p with ino %u pil %u\n", 1303 handler, arg, (u_int)ino, (u_int)ih->ih_pil)); 1304 1305 intr_establish(ih->ih_pil, level != IPL_VM, ih); 1306 1307 /* 1308 * Enable the interrupt now we have the handler installed. 1309 * Read the current value as we can't change it besides the 1310 * valid bit so so make sure only this bit is changed. 1311 * 1312 * XXXX --- we really should use bus_space for this. 1313 */ 1314 if (intrmapptr) { 1315 imap = *intrmapptr; 1316 DPRINTF(PDB_INTR, ("; read intrmap = %016qx", 1317 (unsigned long long)imap)); 1318 1319 /* Enable the interrupt */ 1320 imap |= INTMAP_V|(CPU_UPAID << INTMAP_TID_SHIFT); 1321 DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr)); 1322 DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n", 1323 (unsigned long long)imap)); 1324 *intrmapptr = imap; 1325 DPRINTF(PDB_INTR, ("; reread intrmap = %016qx", 1326 (unsigned long long)(imap = *intrmapptr))); 1327 } 1328 if (intrclrptr) { 1329 /* set state to IDLE */ 1330 *intrclrptr = 0; 1331 } 1332 return (ih); 1333 } 1334 1335 /* 1336 * per-controller driver calls 1337 */ 1338 1339 /* assume we are mapped little-endian/side-effect */ 1340 static pcireg_t 1341 psycho_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg) 1342 { 1343 struct psycho_pbm *pp = pc->cookie; 1344 struct psycho_softc *sc = pp->pp_sc; 1345 pcireg_t val = (pcireg_t)~0; 1346 1347 DPRINTF(PDB_CONF, ("pci_conf_read: tag %lx reg %x ", 1348 (long)tag, reg)); 1349 if (PCITAG_NODE(tag) != -1) { 1350 1351 DPRINTF(PDB_CONF, ("asi=%x addr=%qx (offset=%x) ...", 1352 sc->sc_configaddr._asi, 1353 (long long)(sc->sc_configaddr._ptr + 1354 PCITAG_OFFSET(tag) + reg), 1355 (int)PCITAG_OFFSET(tag) + reg)); 1356 1357 val = bus_space_read_4(sc->sc_configtag, sc->sc_configaddr, 1358 PCITAG_OFFSET(tag) + reg); 1359 } 1360 #ifdef DEBUG 1361 else DPRINTF(PDB_CONF, ("pci_conf_read: bogus pcitag %x\n", 1362 (int)PCITAG_OFFSET(tag))); 1363 #endif 1364 DPRINTF(PDB_CONF, (" returning %08x\n", (u_int)val)); 1365 1366 return (val); 1367 } 1368 1369 static void 1370 psycho_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data) 1371 { 1372 struct psycho_pbm *pp = pc->cookie; 1373 struct psycho_softc *sc = pp->pp_sc; 1374 1375 DPRINTF(PDB_CONF, ("pci_conf_write: tag %lx; reg %x; data %x; ", 1376 (long)PCITAG_OFFSET(tag), reg, (int)data)); 1377 DPRINTF(PDB_CONF, ("asi = %x; readaddr = %qx (offset = %x)\n", 1378 sc->sc_configaddr._asi, 1379 (long long)(sc->sc_configaddr._ptr + PCITAG_OFFSET(tag) + reg), 1380 (int)PCITAG_OFFSET(tag) + reg)); 1381 1382 /* If we don't know it, just punt it. */ 1383 if (PCITAG_NODE(tag) == -1) { 1384 DPRINTF(PDB_CONF, ("pci_conf_write: bad addr")); 1385 return; 1386 } 1387 1388 bus_space_write_4(sc->sc_configtag, sc->sc_configaddr, 1389 PCITAG_OFFSET(tag) + reg, data); 1390 } 1391 1392 /* 1393 * interrupt mapping foo. 1394 * XXX: how does this deal with multiple interrupts for a device? 1395 */ 1396 int 1397 psycho_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp) 1398 { 1399 pcitag_t tag = pa->pa_tag; 1400 int interrupts, *intp; 1401 int len, node = PCITAG_NODE(tag); 1402 char devtype[30]; 1403 1404 intp = &interrupts; 1405 len = 1; 1406 if (prom_getprop(node, "interrupts", sizeof(interrupts), 1407 &len, &intp) != 0 || len != 1) { 1408 DPRINTF(PDB_INTMAP, 1409 ("pci_intr_map: could not read interrupts\n")); 1410 return (ENODEV); 1411 } 1412 1413 if (OF_mapintr(node, &interrupts, sizeof(interrupts), 1414 sizeof(interrupts)) < 0) { 1415 printf("OF_mapintr failed\n"); 1416 KASSERT(pa->pa_pc->spc_find_ino); 1417 pa->pa_pc->spc_find_ino(pa, &interrupts); 1418 } 1419 DPRINTF(PDB_INTMAP, ("OF_mapintr() gave %x\n", interrupts)); 1420 1421 /* Try to find an IPL for this type of device. */ 1422 prom_getpropstringA(node, "device_type", devtype, sizeof(devtype)); 1423 for (len = 0; intrmap[len].in_class != NULL; len++) 1424 if (strcmp(intrmap[len].in_class, devtype) == 0) { 1425 interrupts |= INTLEVENCODE(intrmap[len].in_lev); 1426 DPRINTF(PDB_INTMAP, ("reset to %x\n", interrupts)); 1427 break; 1428 } 1429 1430 /* XXXX -- we use the ino. What if there is a valid IGN? */ 1431 *ihp = interrupts; 1432 return (0); 1433 } 1434 1435 static void * 1436 psycho_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level, 1437 int (*func)(void *), void *arg) 1438 { 1439 void *cookie; 1440 struct psycho_pbm *pp = (struct psycho_pbm *)pc->cookie; 1441 1442 DPRINTF(PDB_INTR, ("%s: ih %lx; level %d", __func__, (u_long)ih, level)); 1443 cookie = bus_intr_establish(pp->pp_memt, ih, level, func, arg); 1444 1445 DPRINTF(PDB_INTR, ("; returning handle %p\n", cookie)); 1446 return (cookie); 1447 } 1448 1449 static int 1450 psycho_pci_find_ino(struct pci_attach_args *pa, pci_intr_handle_t *ihp) 1451 { 1452 struct psycho_pbm *pp = pa->pa_pc->cookie; 1453 struct psycho_softc *sc = pp->pp_sc; 1454 u_int bus; 1455 u_int dev; 1456 u_int pin; 1457 1458 DPRINTF(PDB_INTMAP, ("pci_find_ino: pa_tag: node %x, %d:%d:%d\n", 1459 PCITAG_NODE(pa->pa_tag), (int)PCITAG_BUS(pa->pa_tag), 1460 (int)PCITAG_DEV(pa->pa_tag), 1461 (int)PCITAG_FUN(pa->pa_tag))); 1462 DPRINTF(PDB_INTMAP, 1463 ("pci_find_ino: intrswiz %d, intrpin %d, intrline %d, rawintrpin %d\n", 1464 pa->pa_intrswiz, pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin)); 1465 DPRINTF(PDB_INTMAP, ("pci_find_ino: pa_intrtag: node %x, %d:%d:%d\n", 1466 PCITAG_NODE(pa->pa_intrtag), 1467 (int)PCITAG_BUS(pa->pa_intrtag), 1468 (int)PCITAG_DEV(pa->pa_intrtag), 1469 (int)PCITAG_FUN(pa->pa_intrtag))); 1470 1471 bus = (pp->pp_id == PSYCHO_PBM_B); 1472 /* 1473 * If we are on a ppb, use the devno on the underlying bus when forming 1474 * the ivec. 1475 */ 1476 if (pa->pa_intrswiz != 0 && PCITAG_NODE(pa->pa_intrtag) != 0) 1477 dev = PCITAG_DEV(pa->pa_intrtag); 1478 else 1479 dev = pa->pa_device; 1480 dev--; 1481 1482 if (sc->sc_mode == PSYCHO_MODE_PSYCHO && 1483 pp->pp_id == PSYCHO_PBM_B) 1484 dev--; 1485 1486 pin = pa->pa_intrpin - 1; 1487 DPRINTF(PDB_INTMAP, ("pci_find_ino: mode %d, pbm %d, dev %d, pin %d\n", 1488 sc->sc_mode, pp->pp_id, dev, pin)); 1489 1490 *ihp = sc->sc_ign | ((bus << 4) & INTMAP_PCIBUS) | 1491 ((dev << 2) & INTMAP_PCISLOT) | (pin & INTMAP_PCIINT); 1492 1493 return (0); 1494 } 1495 1496 /* 1497 * hooks into the iommu dvma calls. 1498 */ 1499 static int 1500 psycho_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 1501 bus_size_t maxsegsz, bus_size_t boundary, int flags, 1502 bus_dmamap_t *dmamp) 1503 { 1504 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie; 1505 int error; 1506 1507 error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz, 1508 boundary, flags, dmamp); 1509 if (error == 0) 1510 (*dmamp)->_dm_cookie = &pp->pp_sb; 1511 return error; 1512 } 1513 1514 /* 1515 * UltraSPARC IIi and IIe have no streaming buffers, but have PCI DMA 1516 * Write Synchronization Register (see UltraSPARC-IIi User's Manual 1517 * section 19.3.0.5). So use it to synchronize with the DMA writes. 1518 */ 1519 static void 1520 psycho_sabre_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 1521 bus_size_t len, int ops) 1522 { 1523 struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie; 1524 struct psycho_softc *sc = pp->pp_sc; 1525 1526 if (ops & BUS_DMASYNC_POSTREAD) 1527 bus_space_read_8(sc->sc_bustag, sc->sc_bh, 1528 offsetof(struct psychoreg, pci_dma_write_sync)); 1529 bus_dmamap_sync(t->_parent, map, offset, len, ops); 1530 } 1531