pci_pir.c revision 128327
155232Speter/* 254816Speter * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 354816Speter * Copyright (c) 2000, Michael Smith <msmith@freebsd.org> 454816Speter * Copyright (c) 2000, BSDi 554816Speter * Copyright (c) 2004, John Baldwin <jhb@FreeBSD.org> 654816Speter * All rights reserved. 754816Speter * 854816Speter * Redistribution and use in source and binary forms, with or without 993858Sgshapiro * modification, are permitted provided that the following conditions 1093858Sgshapiro * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * 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 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/sys/i386/pci/pci_pir.c 128327 2004-04-16 18:54:05Z jhb $"); 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/bus.h> 36#include <sys/kernel.h> 37#include <sys/malloc.h> 38#include <sys/sysctl.h> 39#include <vm/vm.h> 40#include <vm/pmap.h> 41#include <machine/md_var.h> 42#include <dev/pci/pcivar.h> 43#include <dev/pci/pcireg.h> 44#include <machine/pci_cfgreg.h> 45#include <machine/segments.h> 46#include <machine/pc/bios.h> 47 48#define NUM_ISA_INTERRUPTS 16 49 50/* 51 * A link device. Loosely based on the ACPI PCI link device. This doesn't 52 * try to support priorities for different ISA interrupts. 53 */ 54struct pci_link { 55 TAILQ_ENTRY(pci_link) pl_links; 56 uint8_t pl_id; 57 uint8_t pl_irq; 58 uint16_t pl_irqmask; 59 int pl_references; 60 int pl_routed; 61}; 62 63struct pci_link_lookup { 64 struct pci_link **pci_link_ptr; 65 int bus; 66 int device; 67 int pin; 68}; 69 70typedef void pir_entry_handler(struct PIR_entry *entry, 71 struct PIR_intpin* intpin, void *arg); 72 73static void pci_print_irqmask(u_int16_t irqs); 74static int pci_pir_biosroute(int bus, int device, int func, int pin, 75 int irq); 76static int pci_pir_choose_irq(struct pci_link *pci_link, int irqmask); 77static void pci_pir_create_links(struct PIR_entry *entry, 78 struct PIR_intpin *intpin, void *arg); 79static void pci_pir_dump_links(void); 80static struct pci_link *pci_pir_find_link(uint8_t link_id); 81static void pci_pir_find_link_handler(struct PIR_entry *entry, 82 struct PIR_intpin *intpin, void *arg); 83static void pci_pir_initial_irqs(struct PIR_entry *entry, 84 struct PIR_intpin *intpin, void *arg); 85static void pci_pir_print_intpin(struct PIR_entry *entry, 86 struct PIR_intpin *intpin, void *arg); 87static void pci_pir_print_table(void); 88static uint8_t pci_pir_search_irq(int bus, int device, int pin); 89static int pci_pir_valid_irq(struct pci_link *pci_link, int irq); 90static void pci_pir_walk_table(pir_entry_handler *handler, void *arg); 91 92MALLOC_DEFINE(M_PIR, "$PIR", "$PIR structures"); 93 94static struct PIR_table *pci_route_table; 95static int pci_route_count, pir_bios_irqs, pir_parsed; 96static TAILQ_HEAD(, pci_link) pci_links; 97static int pir_interrupt_weight[NUM_ISA_INTERRUPTS]; 98 99/* sysctl vars */ 100SYSCTL_DECL(_hw_pci); 101 102#ifdef PC98 103/* IRQs 3, 5, 7, 9, 10, 11, 12, 13 */ 104#define PCI_IRQ_OVERRIDE_MASK 0x3e68 105#else 106/* IRQs 3, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15 */ 107#define PCI_IRQ_OVERRIDE_MASK 0xdef8 108#endif 109 110static uint32_t pci_irq_override_mask = PCI_IRQ_OVERRIDE_MASK; 111TUNABLE_INT("hw.pci.irq_override_mask", &pci_irq_override_mask); 112SYSCTL_INT(_hw_pci, OID_AUTO, irq_override_mask, CTLFLAG_RDTUN, 113 &pci_irq_override_mask, PCI_IRQ_OVERRIDE_MASK, 114 "Mask of allowed irqs to try to route when it has no good clue about\n" 115 "which irqs it should use."); 116 117/* 118 * Look for the interrupt routing table. 119 * 120 * We use PCI BIOS's PIR table if it's available. $PIR is the standard way 121 * to do this. Sadly, some machines are not standards conforming and have 122 * _PIR instead. We shrug and cope by looking for both. 123 */ 124void 125pci_pir_open(void) 126{ 127 struct PIR_table *pt; 128 uint32_t sigaddr; 129 int i; 130 uint8_t ck, *cv; 131 132 /* Don't try if we've already found a table. */ 133 if (pci_route_table != NULL) 134 return; 135 136 /* Look for $PIR and then _PIR. */ 137 sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0); 138 if (sigaddr == 0) 139 sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0); 140 if (sigaddr == 0) 141 return; 142 143 /* If we found something, check the checksum and length. */ 144 /* XXX - Use pmap_mapdev()? */ 145 pt = (struct PIR_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr); 146 if (pt->pt_header.ph_length <= sizeof(struct PIR_header)) 147 return; 148 for (cv = (u_int8_t *)pt, ck = 0, i = 0; 149 i < (pt->pt_header.ph_length); i++) 150 ck += cv[i]; 151 if (ck != 0) 152 return; 153 154 /* Ok, we've got a valid table. */ 155 pci_route_table = pt; 156 pci_route_count = (pt->pt_header.ph_length - 157 sizeof(struct PIR_header)) / 158 sizeof(struct PIR_entry); 159 printf("Found $PIR table, %d entries at %p\n", 160 pci_route_count, pci_route_table); 161 if (bootverbose) 162 pci_pir_print_table(); 163} 164 165/* 166 * Find the pci_link structure for a given link ID. 167 */ 168static struct pci_link * 169pci_pir_find_link(uint8_t link_id) 170{ 171 struct pci_link *pci_link; 172 173 TAILQ_FOREACH(pci_link, &pci_links, pl_links) { 174 if (pci_link->pl_id == link_id) 175 return (pci_link); 176 } 177 return (NULL); 178} 179 180/* 181 * Find the link device associated with a PCI device in the table. 182 */ 183static void 184pci_pir_find_link_handler(struct PIR_entry *entry, struct PIR_intpin *intpin, 185 void *arg) 186{ 187 struct pci_link_lookup *lookup; 188 189 lookup = (struct pci_link_lookup *)arg; 190 if (entry->pe_bus == lookup->bus && 191 entry->pe_device == lookup->device && 192 intpin - entry->pe_intpin == lookup->pin) 193 *lookup->pci_link_ptr = pci_pir_find_link(intpin->link); 194} 195 196/* 197 * Check to see if a possible IRQ setting is valid. 198 */ 199static int 200pci_pir_valid_irq(struct pci_link *pci_link, int irq) 201{ 202 203 if (!PCI_INTERRUPT_VALID(irq)) 204 return (0); 205 return (pci_link->pl_irqmask & (1 << irq)); 206} 207 208/* 209 * Walk the $PIR executing the worker function for each valid intpin entry 210 * in the table. The handler is passed a pointer to both the entry and 211 * the intpin in the entry. 212 */ 213static void 214pci_pir_walk_table(pir_entry_handler *handler, void *arg) 215{ 216 struct PIR_entry *entry; 217 struct PIR_intpin *intpin; 218 int i, pin; 219 220 entry = &pci_route_table->pt_entry[0]; 221 for (i = 0; i < pci_route_count; i++, entry++) { 222 intpin = &entry->pe_intpin[0]; 223 for (pin = 0; pin < 4; pin++, intpin++) 224 if (intpin->link != 0) 225 handler(entry, intpin, arg); 226 } 227} 228 229static void 230pci_pir_create_links(struct PIR_entry *entry, struct PIR_intpin *intpin, 231 void *arg) 232{ 233 struct pci_link *pci_link; 234 235 pci_link = pci_pir_find_link(intpin->link); 236 if (pci_link != NULL) { 237 pci_link->pl_references++; 238 if (intpin->irqs != pci_link->pl_irqmask) { 239 if (bootverbose) 240 printf( 241 "$PIR: Entry %d.%d.INT%c has different mask for link %#x, merging\n", 242 entry->pe_bus, entry->pe_device, 243 (intpin - entry->pe_intpin) + 'A', 244 pci_link->pl_id); 245 pci_link->pl_irqmask &= intpin->irqs; 246 } 247 } else { 248 pci_link = malloc(sizeof(struct pci_link), M_PIR, M_WAITOK); 249 pci_link->pl_id = intpin->link; 250 pci_link->pl_irqmask = intpin->irqs; 251 pci_link->pl_irq = PCI_INVALID_IRQ; 252 pci_link->pl_references = 1; 253 pci_link->pl_routed = 0; 254 TAILQ_INSERT_TAIL(&pci_links, pci_link, pl_links); 255 } 256} 257 258/* 259 * Look to see if any of the function on the PCI device at bus/device have 260 * an interrupt routed to intpin 'pin' by the BIOS. 261 */ 262static uint8_t 263pci_pir_search_irq(int bus, int device, int pin) 264{ 265 uint32_t value; 266 uint8_t func, maxfunc; 267 268 /* See if we have a valid device at function 0. */ 269 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1); 270 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE) 271 return (PCI_INVALID_IRQ); 272 if (value & PCIM_MFDEV) 273 maxfunc = PCI_FUNCMAX; 274 else 275 maxfunc = 0; 276 277 /* Scan all possible functions at this device. */ 278 for (func = 0; func <= maxfunc; func++) { 279 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4); 280 if (value == 0xffffffff) 281 continue; 282 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1); 283 284 /* 285 * See if it uses the pin in question. Note that the passed 286 * in pin uses 0 for A, .. 3 for D whereas the intpin 287 * register uses 0 for no interrupt, 1 for A, .. 4 for D. 288 */ 289 if (value != pin + 1) 290 continue; 291 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1); 292 if (bootverbose) 293 printf( 294 "$PIR: Found matching pin for %d.%d.INT%c at func %d: %d\n", 295 bus, device, pin + 'A', func, value); 296 if (value != PCI_INVALID_IRQ) 297 return (value); 298 } 299 return (PCI_INVALID_IRQ); 300} 301 302/* 303 * Try to initialize IRQ based on this device's IRQ. 304 */ 305static void 306pci_pir_initial_irqs(struct PIR_entry *entry, struct PIR_intpin *intpin, 307 void *arg) 308{ 309 struct pci_link *pci_link; 310 uint8_t irq, pin; 311 312 pin = intpin - entry->pe_intpin; 313 pci_link = pci_pir_find_link(intpin->link); 314 irq = pci_pir_search_irq(entry->pe_bus, entry->pe_device, pin); 315 if (irq == PCI_INVALID_IRQ) 316 return; 317 if (pci_pir_valid_irq(pci_link, irq)) { 318 if (pci_link->pl_irq == PCI_INVALID_IRQ) { 319 pci_link->pl_irq = irq; 320 pci_link->pl_routed = 1; 321 } else if (pci_link->pl_irq != irq) 322 printf( 323 "$PIR: BIOS IRQ %d for %d.%d.INT%c does not match link %#x irq %d\n", 324 irq, entry->pe_bus, entry->pe_device, pin + 'A', 325 pci_link->pl_id, pci_link->pl_irq); 326 } else 327 printf( 328 "$PIR: BIOS IRQ %d for %d.%d.INT%c is not valid for link %#x\n", 329 irq, entry->pe_bus, entry->pe_device, pin + 'A', 330 pci_link->pl_id); 331} 332 333/* 334 * Parse $PIR to enumerate link devices and attempt to determine their 335 * initial state. This could perhaps be cleaner if we had drivers for the 336 * various interrupt routers as they could read the initial IRQ for each 337 * link. 338 */ 339void 340pci_pir_parse(void) 341{ 342 char tunable_buffer[64]; 343 struct pci_link *pci_link; 344 int i, irq; 345 346 /* Only parse once. */ 347 if (pir_parsed) 348 return; 349 pir_parsed = 1; 350 351 /* Enumerate link devices. */ 352 TAILQ_INIT(&pci_links); 353 pci_pir_walk_table(pci_pir_create_links, NULL); 354 if (bootverbose) { 355 printf("$PIR: Links after initial probe:\n"); 356 pci_pir_dump_links(); 357 } 358 359 /* Check for unique IRQ masks. */ 360 TAILQ_FOREACH(pci_link, &pci_links, pl_links) { 361 if (pci_link->pl_irqmask != 0 && powerof2(pci_link->pl_irqmask)) 362 pci_link->pl_irq = ffs(pci_link->pl_irqmask) - 1; 363 } 364 365 /* 366 * Check to see if the BIOS has already routed any of the links by 367 * checking each device connected to each link to see if it has a 368 * valid IRQ. 369 */ 370 pci_pir_walk_table(pci_pir_initial_irqs, NULL); 371 if (bootverbose) { 372 printf("$PIR: Links after initial IRQ discovery:\n"); 373 pci_pir_dump_links(); 374 } 375 376 /* 377 * Allow the user to override the IRQ for a given link device as 378 * long as the override is valid or is 255 or 0 to clear a preset 379 * IRQ. 380 */ 381 i = 0; 382 TAILQ_FOREACH(pci_link, &pci_links, pl_links) { 383 snprintf(tunable_buffer, sizeof(tunable_buffer), 384 "hw.pci.link.%#x.irq", pci_link->pl_id); 385 if (getenv_int(tunable_buffer, &irq) == 0) 386 continue; 387 if (irq == 0) 388 irq = PCI_INVALID_IRQ; 389 if (irq == PCI_INVALID_IRQ || 390 pci_pir_valid_irq(pci_link, irq)) { 391 pci_link->pl_irq = irq; 392 i = 1; 393 } 394 } 395 if (bootverbose && i) { 396 printf("$PIR: Links after tunable overrides:\n"); 397 pci_pir_dump_links(); 398 } 399 400 /* 401 * Build initial interrupt weights as well as bitmap of "known-good" 402 * IRQs that the BIOS has already used for PCI link devices. 403 */ 404 TAILQ_FOREACH(pci_link, &pci_links, pl_links) { 405 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) 406 continue; 407 pir_bios_irqs |= 1 << pci_link->pl_irq; 408 pir_interrupt_weight[pci_link->pl_irq] += 409 pci_link->pl_references; 410 } 411 if (bootverbose) { 412 printf("$PIR: IRQs used by BIOS: "); 413 pci_print_irqmask(pir_bios_irqs); 414 printf("\n"); 415 printf("$PIR: Interrupt Weights:\n[ "); 416 for (i = 0; i < NUM_ISA_INTERRUPTS; i++) 417 printf(" %3d", i); 418 printf(" ]\n[ "); 419 for (i = 0; i < NUM_ISA_INTERRUPTS; i++) 420 printf(" %3d", pir_interrupt_weight[i]); 421 printf(" ]\n"); 422 } 423} 424 425/* 426 * Use the PCI BIOS to route an interrupt for a given device. 427 * 428 * Input: 429 * AX = PCIBIOS_ROUTE_INTERRUPT 430 * BH = bus 431 * BL = device [7:3] / function [2:0] 432 * CH = IRQ 433 * CL = Interrupt Pin (0x0A = A, ... 0x0D = D) 434 */ 435static int 436pci_pir_biosroute(int bus, int device, int func, int pin, int irq) 437{ 438 struct bios_regs args; 439 440 args.eax = PCIBIOS_ROUTE_INTERRUPT; 441 args.ebx = (bus << 8) | (device << 3) | func; 442 args.ecx = (irq << 8) | (0xa + pin); 443 return (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))); 444} 445 446 447/* 448 * Route a PCI interrupt using a link device from the $PIR. 449 */ 450int 451pci_pir_route_interrupt(int bus, int device, int func, int pin) 452{ 453 struct pci_link_lookup lookup; 454 struct pci_link *pci_link; 455 int error, irq; 456 457 if (pci_route_table == NULL) 458 return (PCI_INVALID_IRQ); 459 460 /* Lookup link device for this PCI device/pin. */ 461 pci_link = NULL; 462 lookup.bus = bus; 463 lookup.device = device; 464 lookup.pin = pin - 1; 465 lookup.pci_link_ptr = &pci_link; 466 pci_pir_walk_table(pci_pir_find_link_handler, &lookup); 467 if (pci_link == NULL) { 468 printf("$PIR: No matching entry for %d.%d.INT%c\n", bus, 469 device, pin - 1 + 'A'); 470 return (PCI_INVALID_IRQ); 471 } 472 473 /* 474 * Pick a new interrupt if we don't have one already. We look for 475 * an interrupt from several different sets. First, we check the 476 * set of PCI only interrupts from the $PIR. Second, we check the 477 * set of known-good interrupts that the BIOS has already used. 478 * Lastly, we check the "all possible valid IRQs" set. 479 */ 480 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) { 481 irq = pci_pir_choose_irq(pci_link, 482 pci_route_table->pt_header.ph_pci_irqs); 483 if (!PCI_INTERRUPT_VALID(irq)) 484 irq = pci_pir_choose_irq(pci_link, pir_bios_irqs); 485 if (!PCI_INTERRUPT_VALID(irq)) 486 irq = pci_pir_choose_irq(pci_link, 487 pci_irq_override_mask); 488 if (!PCI_INTERRUPT_VALID(irq)) { 489 if (bootverbose) 490 printf( 491 "$PIR: Failed to route interrupt for %d:%d INT%c\n", 492 bus, device, pin - 1 + 'A'); 493 return (PCI_INVALID_IRQ); 494 } 495 pci_link->pl_irq = irq; 496 } 497 498 /* Ask the BIOS to route this IRQ if we haven't done so already. */ 499 if (!pci_link->pl_routed) { 500 error = pci_pir_biosroute(bus, device, func, pin - 1, 501 pci_link->pl_irq); 502 503 /* Ignore errors when routing a unique interrupt. */ 504 if (error && !powerof2(pci_link->pl_irqmask)) { 505 printf("$PIR: ROUTE_INTERRUPT failed.\n"); 506 return (PCI_INVALID_IRQ); 507 } 508 pci_link->pl_routed = 1; 509 } 510 printf("$PIR: %d:%d INT%c routed to irq %d\n", bus, device, 511 pin - 1 + 'A', pci_link->pl_irq); 512 return (pci_link->pl_irq); 513} 514 515/* 516 * Try to pick an interrupt for the specified link from the interrupts 517 * set in the mask. 518 */ 519static int 520pci_pir_choose_irq(struct pci_link *pci_link, int irqmask) 521{ 522 int i, irq, realmask; 523 524 /* XXX: Need to have a #define of known bad IRQs to also mask out? */ 525 realmask = pci_link->pl_irqmask & irqmask; 526 if (realmask == 0) 527 return (PCI_INVALID_IRQ); 528 529 /* Find IRQ with lowest weight. */ 530 irq = PCI_INVALID_IRQ; 531 for (i = 0; i < NUM_ISA_INTERRUPTS; i++) { 532 if (!(realmask & 1 << i)) 533 continue; 534 if (irq == PCI_INVALID_IRQ || 535 pir_interrupt_weight[i] < pir_interrupt_weight[irq]) 536 irq = i; 537 } 538 if (bootverbose && PCI_INTERRUPT_VALID(irq)) { 539 printf("$PIR: Found IRQ %d for link %#x from ", irq, 540 pci_link->pl_id); 541 pci_print_irqmask(realmask); 542 printf("\n"); 543 } 544 return (irq); 545} 546 547static void 548pci_print_irqmask(u_int16_t irqs) 549{ 550 int i, first; 551 552 if (irqs == 0) { 553 printf("none"); 554 return; 555 } 556 first = 1; 557 for (i = 0; i < 16; i++, irqs >>= 1) 558 if (irqs & 1) { 559 if (!first) 560 printf(" "); 561 else 562 first = 0; 563 printf("%d", i); 564 } 565} 566 567/* 568 * Dump the contents of a single intpin entry to the console. 569 */ 570static void 571pci_pir_print_intpin(struct PIR_entry *entry, struct PIR_intpin *intpin, 572 void *arg) 573{ 574 575 if (entry->pe_slot == 0) 576 printf("embedded "); 577 else 578 printf("slot %-3d ", entry->pe_slot); 579 printf(" %3d %3d %c 0x%02x ", entry->pe_bus, entry->pe_device, 580 intpin - entry->pe_intpin + 'A', intpin->link); 581 pci_print_irqmask(intpin->irqs); 582 printf("\n"); 583} 584 585/* 586 * Dump the contents of a PCI BIOS Interrupt Routing Table to the console. 587 */ 588static void 589pci_pir_print_table(void) 590{ 591 592 printf("PCI-Only Interrupts: "); 593 pci_print_irqmask(pci_route_table->pt_header.ph_pci_irqs); 594 printf("\nLocation Bus Device Pin Link IRQs\n"); 595 pci_pir_walk_table(pci_pir_print_intpin, NULL); 596} 597 598/* 599 * Display link devices. 600 */ 601static void 602pci_pir_dump_links(void) 603{ 604 struct pci_link *pci_link; 605 606 printf("Link IRQ Ref IRQs\n"); 607 TAILQ_FOREACH(pci_link, &pci_links, pl_links) { 608 printf("%#4x %3d %3d ", pci_link->pl_id, pci_link->pl_irq, 609 pci_link->pl_references); 610 pci_print_irqmask(pci_link->pl_irqmask); 611 printf("\n"); 612 } 613} 614 615/* 616 * See if any interrupts for a given PCI bus are routed in the PIR. Don't 617 * even bother looking if the BIOS doesn't support routing anyways. If we 618 * are probing a PCI-PCI bridge, then require_parse will be true and we should 619 * only succeed if a host-PCI bridge has already attached and parsed the PIR. 620 */ 621int 622pci_pir_probe(int bus, int require_parse) 623{ 624 int i; 625 626 if (pci_route_table == NULL || (require_parse && !pir_parsed)) 627 return (0); 628 for (i = 0; i < pci_route_count; i++) 629 if (pci_route_table->pt_entry[i].pe_bus == bus) 630 return (1); 631 return (0); 632} 633