2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
5 * Copyright (c) 2004, John Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
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.
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.
29 * $FreeBSD: src/sys/i386/pci/pci_pir.c,v 1.120.2.1.4.1 2009/04/15 03:14:26 kensmith Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39 #include <sys/machintr.h>
42 #include <vm/vm_param.h>
43 #include <machine/md_var.h>
44 #include <bus/pci/pcivar.h>
45 #include <bus/pci/pcireg.h>
46 #include "pci_cfgreg.h"
47 #include <machine/segments.h>
48 #include <machine/pc/bios.h>
50 #define NUM_ISA_INTERRUPTS 16
53 * A link device. Loosely based on the ACPI PCI link device. This doesn't
54 * try to support priorities for different ISA interrupts.
57 TAILQ_ENTRY(pci_link) pl_links;
65 struct pci_link_lookup {
66 struct pci_link **pci_link_ptr;
72 struct pci_dev_lookup {
79 typedef void pir_entry_handler(struct PIR_entry *entry,
80 struct PIR_intpin* intpin, void *arg);
82 static void pci_print_irqmask(u_int16_t irqs);
83 static int pci_pir_biosroute(int bus, int device, int func, int pin,
85 static int pci_pir_choose_irq(struct pci_link *pci_link, int irqmask);
86 static void pci_pir_create_links(struct PIR_entry *entry,
87 struct PIR_intpin *intpin, void *arg);
88 static void pci_pir_dump_links(void);
89 static struct pci_link *pci_pir_find_link(uint8_t link_id);
90 static void pci_pir_find_link_handler(struct PIR_entry *entry,
91 struct PIR_intpin *intpin, void *arg);
92 static void pci_pir_initial_irqs(struct PIR_entry *entry,
93 struct PIR_intpin *intpin, void *arg);
94 static void pci_pir_parse(void);
95 static uint8_t pci_pir_search_irq(int bus, int device, int pin);
96 static int pci_pir_valid_irq(struct pci_link *pci_link, int irq);
97 static void pci_pir_walk_table(pir_entry_handler *handler, void *arg);
99 static MALLOC_DEFINE(M_PIR, "$PIR", "$PIR structures");
101 static struct PIR_table *pci_route_table;
102 static device_t pir_device;
103 static int pci_route_count, pir_bios_irqs, pir_parsed;
104 static TAILQ_HEAD(, pci_link) pci_links;
105 static int pir_interrupt_weight[NUM_ISA_INTERRUPTS];
108 SYSCTL_DECL(_hw_pci);
110 /* XXX this likely should live in a header file */
111 /* IRQs 3, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15 */
112 #define PCI_IRQ_OVERRIDE_MASK 0xdef8
114 static uint32_t pci_irq_override_mask = PCI_IRQ_OVERRIDE_MASK;
115 TUNABLE_INT("hw.pci.irq_override_mask", &pci_irq_override_mask);
116 SYSCTL_INT(_hw_pci, OID_AUTO, irq_override_mask, CTLFLAG_RD,
117 &pci_irq_override_mask, PCI_IRQ_OVERRIDE_MASK,
118 "Mask of allowed irqs to try to route when it has no good clue about\n"
119 "which irqs it should use.");
122 * Look for the interrupt routing table.
124 * We use PCI BIOS's PIR table if it's available. $PIR is the standard way
125 * to do this. Sadly, some machines are not standards conforming and have
126 * _PIR instead. We shrug and cope by looking for both.
131 struct PIR_table *pt;
136 /* Don't try if we've already found a table. */
137 if (pci_route_table != NULL)
140 /* Look for $PIR and then _PIR. */
141 sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0);
143 sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0);
147 /* If we found something, check the checksum and length. */
148 /* XXX - Use pmap_mapdev()? */
149 pt = (struct PIR_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
150 if (pt->pt_header.ph_length <= sizeof(struct PIR_header))
152 for (cv = (u_int8_t *)pt, ck = 0, i = 0;
153 i < (pt->pt_header.ph_length); i++)
156 kprintf("$PIR: checksum failed!\n");
160 /* Ok, we've got a valid table. */
161 pci_route_table = pt;
162 pci_route_count = (pt->pt_header.ph_length -
163 sizeof(struct PIR_header)) /
164 sizeof(struct PIR_entry);
168 * Find the pci_link structure for a given link ID.
170 static struct pci_link *
171 pci_pir_find_link(uint8_t link_id)
173 struct pci_link *pci_link;
175 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
176 if (pci_link->pl_id == link_id)
183 * Find the link device associated with a PCI device in the table.
186 pci_pir_find_link_handler(struct PIR_entry *entry, struct PIR_intpin *intpin,
189 struct pci_link_lookup *lookup;
191 lookup = (struct pci_link_lookup *)arg;
192 if (entry->pe_bus == lookup->bus &&
193 entry->pe_device == lookup->device &&
194 intpin - entry->pe_intpin == lookup->pin)
195 *lookup->pci_link_ptr = pci_pir_find_link(intpin->link);
199 * Check to see if a possible IRQ setting is valid.
202 pci_pir_valid_irq(struct pci_link *pci_link, int irq)
205 if (!PCI_INTERRUPT_VALID(irq))
207 return (pci_link->pl_irqmask & (1 << irq));
211 * Walk the $PIR executing the worker function for each valid intpin entry
212 * in the table. The handler is passed a pointer to both the entry and
213 * the intpin in the entry.
216 pci_pir_walk_table(pir_entry_handler *handler, void *arg)
218 struct PIR_entry *entry;
219 struct PIR_intpin *intpin;
222 entry = &pci_route_table->pt_entry[0];
223 for (i = 0; i < pci_route_count; i++, entry++) {
224 intpin = &entry->pe_intpin[0];
225 for (pin = 0; pin < 4; pin++, intpin++)
226 if (intpin->link != 0)
227 handler(entry, intpin, arg);
232 pci_pir_create_links(struct PIR_entry *entry, struct PIR_intpin *intpin,
235 struct pci_link *pci_link;
237 pci_link = pci_pir_find_link(intpin->link);
238 if (pci_link != NULL) {
239 pci_link->pl_references++;
240 if (intpin->irqs != pci_link->pl_irqmask) {
243 "$PIR: Entry %d.%d.INT%c has different mask for link %#x, merging\n",
244 entry->pe_bus, entry->pe_device,
245 (intpin - entry->pe_intpin) + 'A',
247 pci_link->pl_irqmask &= intpin->irqs;
250 pci_link = kmalloc(sizeof(struct pci_link), M_PIR, M_WAITOK);
251 pci_link->pl_id = intpin->link;
252 pci_link->pl_irqmask = intpin->irqs;
253 pci_link->pl_irq = PCI_INVALID_IRQ;
254 pci_link->pl_references = 1;
255 pci_link->pl_routed = 0;
256 TAILQ_INSERT_TAIL(&pci_links, pci_link, pl_links);
261 * Look to see if any of the function on the PCI device at bus/device have
262 * an interrupt routed to intpin 'pin' by the BIOS.
265 pci_pir_search_irq(int bus, int device, int pin)
268 uint8_t func, maxfunc;
270 /* See if we have a valid device at function 0. */
271 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
272 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
273 return (PCI_INVALID_IRQ);
274 if (value & PCIM_MFDEV)
275 maxfunc = PCI_FUNCMAX;
279 /* Scan all possible functions at this device. */
280 for (func = 0; func <= maxfunc; func++) {
281 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
282 if (value == 0xffffffff)
284 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
287 * See if it uses the pin in question. Note that the passed
288 * in pin uses 0 for A, .. 3 for D whereas the intpin
289 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
291 if (value != pin + 1)
293 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
296 "$PIR: Found matching pin for %d.%d.INT%c at func %d: %d\n",
297 bus, device, pin + 'A', func, value);
298 if (value != PCI_INVALID_IRQ)
301 return (PCI_INVALID_IRQ);
305 * Try to initialize IRQ based on this device's IRQ.
308 pci_pir_initial_irqs(struct PIR_entry *entry, struct PIR_intpin *intpin,
311 struct pci_link *pci_link;
314 pin = intpin - entry->pe_intpin;
315 pci_link = pci_pir_find_link(intpin->link);
316 irq = pci_pir_search_irq(entry->pe_bus, entry->pe_device, pin);
317 if (irq == PCI_INVALID_IRQ || irq == pci_link->pl_irq)
320 /* Don't trust any BIOS IRQs greater than 15. */
321 if (irq >= NUM_ISA_INTERRUPTS) {
323 "$PIR: Ignoring invalid BIOS IRQ %d from %d.%d.INT%c for link %#x\n",
324 irq, entry->pe_bus, entry->pe_device, pin + 'A',
330 * If we don't have an IRQ for this link yet, then we trust the
331 * BIOS, even if it seems invalid from the $PIR entries.
333 if (pci_link->pl_irq == PCI_INVALID_IRQ) {
334 if (!pci_pir_valid_irq(pci_link, irq))
336 "$PIR: Using invalid BIOS IRQ %d from %d.%d.INT%c for link %#x\n",
337 irq, entry->pe_bus, entry->pe_device, pin + 'A',
339 pci_link->pl_irq = irq;
340 pci_link->pl_routed = 1;
345 * We have an IRQ and it doesn't match the current IRQ for this
346 * link. If the new IRQ is invalid, then warn about it and ignore
347 * it. If the old IRQ is invalid and the new IRQ is valid, then
348 * prefer the new IRQ instead. If both IRQs are valid, then just
349 * use the first one. Note that if we ever get into this situation
350 * we are having to guess which setting the BIOS actually routed.
351 * Perhaps we should just give up instead.
353 if (!pci_pir_valid_irq(pci_link, irq)) {
355 "$PIR: BIOS IRQ %d for %d.%d.INT%c is not valid for link %#x\n",
356 irq, entry->pe_bus, entry->pe_device, pin + 'A',
358 } else if (!pci_pir_valid_irq(pci_link, pci_link->pl_irq)) {
360 "$PIR: Preferring valid BIOS IRQ %d from %d.%d.INT%c for link %#x to IRQ %d\n",
361 irq, entry->pe_bus, entry->pe_device, pin + 'A',
362 pci_link->pl_id, pci_link->pl_irq);
363 pci_link->pl_irq = irq;
364 pci_link->pl_routed = 1;
367 "$PIR: BIOS IRQ %d for %d.%d.INT%c does not match link %#x irq %d\n",
368 irq, entry->pe_bus, entry->pe_device, pin + 'A',
369 pci_link->pl_id, pci_link->pl_irq);
373 * Parse $PIR to enumerate link devices and attempt to determine their
374 * initial state. This could perhaps be cleaner if we had drivers for the
375 * various interrupt routers as they could read the initial IRQ for each
381 char tunable_buffer[64];
382 struct pci_link *pci_link;
385 /* Only parse once. */
390 /* Enumerate link devices. */
391 TAILQ_INIT(&pci_links);
392 pci_pir_walk_table(pci_pir_create_links, NULL);
394 kprintf("$PIR: Links after initial probe:\n");
395 pci_pir_dump_links();
399 * Check to see if the BIOS has already routed any of the links by
400 * checking each device connected to each link to see if it has a
403 pci_pir_walk_table(pci_pir_initial_irqs, NULL);
405 kprintf("$PIR: Links after initial IRQ discovery:\n");
406 pci_pir_dump_links();
410 * Allow the user to override the IRQ for a given link device. We
411 * allow invalid IRQs to be specified but warn about them. An IRQ
412 * of 255 or 0 clears any preset IRQ.
415 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
416 ksnprintf(tunable_buffer, sizeof(tunable_buffer),
417 "hw.pci.link.%#x.irq", pci_link->pl_id);
418 if (kgetenv_int(tunable_buffer, &irq) == 0)
421 irq = PCI_INVALID_IRQ;
422 if (irq != PCI_INVALID_IRQ &&
423 !pci_pir_valid_irq(pci_link, irq) && bootverbose)
425 "$PIR: Warning, IRQ %d for link %#x is not listed as valid\n",
426 irq, pci_link->pl_id);
427 pci_link->pl_routed = 0;
428 pci_link->pl_irq = irq;
431 if (bootverbose && i) {
432 kprintf("$PIR: Links after tunable overrides:\n");
433 pci_pir_dump_links();
437 * Build initial interrupt weights as well as bitmap of "known-good"
438 * IRQs that the BIOS has already used for PCI link devices.
440 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
441 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq))
443 pir_bios_irqs |= 1 << pci_link->pl_irq;
444 pir_interrupt_weight[pci_link->pl_irq] +=
445 pci_link->pl_references;
448 kprintf("$PIR: IRQs used by BIOS: ");
449 pci_print_irqmask(pir_bios_irqs);
451 kprintf("$PIR: Interrupt Weights:\n[ ");
452 for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
455 for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
456 kprintf(" %3d", pir_interrupt_weight[i]);
462 * Use the PCI BIOS to route an interrupt for a given device.
465 * AX = PCIBIOS_ROUTE_INTERRUPT
467 * BL = device [7:3] / function [2:0]
469 * CL = Interrupt Pin (0x0A = A, ... 0x0D = D)
472 pci_pir_biosroute(int bus, int device, int func, int pin, int irq)
474 struct bios_regs args;
476 args.eax = PCIBIOS_ROUTE_INTERRUPT;
477 args.ebx = (bus << 8) | (device << 3) | func;
478 args.ecx = (irq << 8) | (0xa + pin);
479 return (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)));
484 * Route a PCI interrupt using a link device from the $PIR.
487 pci_pir_route_interrupt(int bus, int device, int func, int pin)
489 struct pci_link_lookup lookup;
490 struct pci_link *pci_link;
493 if (pci_route_table == NULL)
494 return (PCI_INVALID_IRQ);
496 /* Lookup link device for this PCI device/pin. */
499 lookup.device = device;
500 lookup.pin = pin - 1;
501 lookup.pci_link_ptr = &pci_link;
502 pci_pir_walk_table(pci_pir_find_link_handler, &lookup);
503 if (pci_link == NULL) {
504 kprintf("$PIR: No matching entry for %d.%d.INT%c\n", bus,
505 device, pin - 1 + 'A');
506 return (PCI_INVALID_IRQ);
510 * Pick a new interrupt if we don't have one already. We look
511 * for an interrupt from several different sets. First, if
512 * this link only has one valid IRQ, use that. Second, we
513 * check the set of PCI only interrupts from the $PIR. Third,
514 * we check the set of known-good interrupts that the BIOS has
515 * already used. Lastly, we check the "all possible valid
518 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
519 if (pci_link->pl_irqmask != 0 && powerof2(pci_link->pl_irqmask))
520 irq = ffs(pci_link->pl_irqmask) - 1;
522 irq = pci_pir_choose_irq(pci_link,
523 pci_route_table->pt_header.ph_pci_irqs);
524 if (!PCI_INTERRUPT_VALID(irq))
525 irq = pci_pir_choose_irq(pci_link, pir_bios_irqs);
526 if (!PCI_INTERRUPT_VALID(irq))
527 irq = pci_pir_choose_irq(pci_link,
528 pci_irq_override_mask);
529 if (!PCI_INTERRUPT_VALID(irq)) {
532 "$PIR: Failed to route interrupt for %d:%d INT%c\n",
533 bus, device, pin - 1 + 'A');
534 return (PCI_INVALID_IRQ);
536 pci_link->pl_irq = irq;
539 /* Ask the BIOS to route this IRQ if we haven't done so already. */
540 if (!pci_link->pl_routed) {
541 if (machintr_legacy_intr_find(pci_link->pl_irq,
542 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW) < 0) {
543 kprintf("$PIR: can't find irq %d\n", pci_link->pl_irq);
544 return PCI_INVALID_IRQ;
547 error = pci_pir_biosroute(bus, device, func, pin - 1,
550 /* Ignore errors when routing a unique interrupt. */
551 if (error && !powerof2(pci_link->pl_irqmask)) {
552 kprintf("$PIR: ROUTE_INTERRUPT failed.\n");
553 return (PCI_INVALID_IRQ);
555 pci_link->pl_routed = 1;
557 /* Ensure the interrupt is set to level/low trigger. */
558 KASSERT(pir_device != NULL, ("missing pir device"));
559 BUS_CONFIG_INTR(pir_device, pir_device, pci_link->pl_irq,
560 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
563 kprintf("$PIR: %d:%d INT%c routed to irq %d\n", bus, device,
564 pin - 1 + 'A', pci_link->pl_irq);
565 return (pci_link->pl_irq);
569 * Try to pick an interrupt for the specified link from the interrupts
573 pci_pir_choose_irq(struct pci_link *pci_link, int irqmask)
575 int i, irq, realmask;
577 /* XXX: Need to have a #define of known bad IRQs to also mask out? */
578 realmask = pci_link->pl_irqmask & irqmask;
580 return (PCI_INVALID_IRQ);
582 /* Find IRQ with lowest weight. */
583 irq = PCI_INVALID_IRQ;
584 for (i = 0; i < NUM_ISA_INTERRUPTS; i++) {
585 if (!(realmask & 1 << i))
587 if (irq == PCI_INVALID_IRQ ||
588 pir_interrupt_weight[i] < pir_interrupt_weight[irq])
591 if (bootverbose && PCI_INTERRUPT_VALID(irq)) {
592 kprintf("$PIR: Found IRQ %d for link %#x from ", irq,
594 pci_print_irqmask(realmask);
601 pci_print_irqmask(u_int16_t irqs)
610 for (i = 0; i < 16; i++, irqs >>= 1)
621 * Display link devices.
624 pci_pir_dump_links(void)
626 struct pci_link *pci_link;
628 kprintf("Link IRQ Rtd Ref IRQs\n");
629 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
630 kprintf("%#4x %3d %c %3d ", pci_link->pl_id,
631 pci_link->pl_irq, pci_link->pl_routed ? 'Y' : 'N',
632 pci_link->pl_references);
633 pci_print_irqmask(pci_link->pl_irqmask);
639 * See if any interrupts for a given PCI bus are routed in the PIR. Don't
640 * even bother looking if the BIOS doesn't support routing anyways. If we
641 * are probing a PCI-PCI bridge, then require_parse will be true and we should
642 * only succeed if a host-PCI bridge has already attached and parsed the PIR.
645 pci_pir_probe(int bus, int require_parse)
648 if (pci_route_table == NULL || (require_parse && !pir_parsed))
650 for (i = 0; i < pci_route_count; i++)
651 if (pci_route_table->pt_entry[i].pe_bus == bus)
657 * The driver for the new-bus psuedo device pir0 for the $PIR table.
661 pir_probe(device_t dev)
665 ksnprintf(buf, sizeof(buf), "PCI Interrupt Routing Table: %d Entries",
667 device_set_desc_copy(dev, buf);
672 pir_attach(device_t dev)
676 KASSERT(pir_device == NULL, ("Multiple pir devices"));
682 pir_resume_find_device(struct PIR_entry *entry, struct PIR_intpin *intpin,
685 struct pci_dev_lookup *pd;
687 pd = (struct pci_dev_lookup *)arg;
688 if (intpin->link != pd->link || pd->bus != -1)
690 pd->bus = entry->pe_bus;
691 pd->device = entry->pe_device;
692 pd->pin = intpin - entry->pe_intpin;
696 pir_resume(device_t dev)
698 struct pci_dev_lookup pd;
699 struct pci_link *pci_link;
702 /* Ask the BIOS to re-route each link that was already routed. */
703 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
704 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
705 KASSERT(!pci_link->pl_routed,
706 ("link %#x is routed but has invalid PCI IRQ",
710 if (pci_link->pl_routed) {
712 pd.link = pci_link->pl_id;
713 pci_pir_walk_table(pir_resume_find_device, &pd);
714 KASSERT(pd.bus != -1,
715 ("did not find matching entry for link %#x in the $PIR table",
719 "Using %d.%d.INT%c to route link %#x to IRQ %d\n",
720 pd.bus, pd.device, pd.pin + 'A',
721 pci_link->pl_id, pci_link->pl_irq);
722 error = pci_pir_biosroute(pd.bus, pd.device, 0, pd.pin,
726 "ROUTE_INTERRUPT on resume for link %#x failed.\n",
733 static device_method_t pir_methods[] = {
734 /* Device interface */
735 DEVMETHOD(device_probe, pir_probe),
736 DEVMETHOD(device_attach, pir_attach),
737 DEVMETHOD(device_resume, pir_resume),
742 static driver_t pir_driver = {
748 static devclass_t pir_devclass;
750 DRIVER_MODULE(pir, legacy, pir_driver, pir_devclass, NULL, NULL);