186cb007fSWarner Losh /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
383ef78beSPedro F. Giffuni *
45bec6157SStefan Eßer * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
512a02d6eSMike Smith * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
612a02d6eSMike Smith * Copyright (c) 2000, BSDi
75bec6157SStefan Eßer * All rights reserved.
82e43efd0SJohn Baldwin * Copyright (c) 2004, John Baldwin <jhb@FreeBSD.org>
95bec6157SStefan Eßer *
105bec6157SStefan Eßer * Redistribution and use in source and binary forms, with or without
115bec6157SStefan Eßer * modification, are permitted provided that the following conditions
125bec6157SStefan Eßer * are met:
135bec6157SStefan Eßer * 1. Redistributions of source code must retain the above copyright
145bec6157SStefan Eßer * notice unmodified, this list of conditions, and the following
155bec6157SStefan Eßer * disclaimer.
165bec6157SStefan Eßer * 2. Redistributions in binary form must reproduce the above copyright
175bec6157SStefan Eßer * notice, this list of conditions and the following disclaimer in the
185bec6157SStefan Eßer * documentation and/or other materials provided with the distribution.
195bec6157SStefan Eßer *
205bec6157SStefan Eßer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
215bec6157SStefan Eßer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
225bec6157SStefan Eßer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
235bec6157SStefan Eßer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
245bec6157SStefan Eßer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
255bec6157SStefan Eßer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
265bec6157SStefan Eßer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275bec6157SStefan Eßer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285bec6157SStefan Eßer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
295bec6157SStefan Eßer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ac19f918SStefan Eßer */
31ac19f918SStefan Eßer
322e41ba54SJohn Baldwin #include <sys/param.h>
335bec6157SStefan Eßer #include <sys/systm.h>
348dc26439SPeter Wemm #include <sys/bus.h>
358dc26439SPeter Wemm #include <sys/kernel.h>
36280b4748SPeter Wemm #include <sys/malloc.h>
3741ee9f1cSPoul-Henning Kamp #include <sys/module.h>
38e86bd39aSWarner Losh #include <sys/sysctl.h>
3954c9005fSWarner Losh #include <vm/vm.h>
4054c9005fSWarner Losh #include <vm/pmap.h>
41092a5c45SJohn Baldwin #include <vm/vm_param.h>
4254c9005fSWarner Losh #include <machine/md_var.h>
43e300f53cSWarner Losh #include <dev/pci/pcivar.h>
44e300f53cSWarner Losh #include <dev/pci/pcireg.h>
4512a02d6eSMike Smith #include <machine/pci_cfgreg.h>
46300451c4SMike Smith #include <machine/segments.h>
47300451c4SMike Smith #include <machine/pc/bios.h>
48300451c4SMike Smith
492e41ba54SJohn Baldwin #define NUM_ISA_INTERRUPTS 16
5021c3015aSDoug Rabson
512e41ba54SJohn Baldwin /*
522e41ba54SJohn Baldwin * A link device. Loosely based on the ACPI PCI link device. This doesn't
532e41ba54SJohn Baldwin * try to support priorities for different ISA interrupts.
542e41ba54SJohn Baldwin */
552e41ba54SJohn Baldwin struct pci_link {
562e41ba54SJohn Baldwin TAILQ_ENTRY(pci_link) pl_links;
572e41ba54SJohn Baldwin uint8_t pl_id;
582e41ba54SJohn Baldwin uint8_t pl_irq;
592e41ba54SJohn Baldwin uint16_t pl_irqmask;
602e41ba54SJohn Baldwin int pl_references;
612e41ba54SJohn Baldwin int pl_routed;
622e41ba54SJohn Baldwin };
63d626906bSWarner Losh
642e41ba54SJohn Baldwin struct pci_link_lookup {
652e41ba54SJohn Baldwin struct pci_link **pci_link_ptr;
662e41ba54SJohn Baldwin int bus;
672e41ba54SJohn Baldwin int device;
682e41ba54SJohn Baldwin int pin;
692e41ba54SJohn Baldwin };
70300451c4SMike Smith
717a64d8d7SJohn Baldwin struct pci_dev_lookup {
727a64d8d7SJohn Baldwin uint8_t link;
737a64d8d7SJohn Baldwin int bus;
747a64d8d7SJohn Baldwin int device;
757a64d8d7SJohn Baldwin int pin;
767a64d8d7SJohn Baldwin };
777a64d8d7SJohn Baldwin
782e41ba54SJohn Baldwin typedef void pir_entry_handler(struct PIR_entry *entry,
792e41ba54SJohn Baldwin struct PIR_intpin* intpin, void *arg);
80099d058bSMike Smith
81fbabd7beSJohn Baldwin static void pci_print_irqmask(u_int16_t irqs);
822e41ba54SJohn Baldwin static int pci_pir_biosroute(int bus, int device, int func, int pin,
832e41ba54SJohn Baldwin int irq);
842e41ba54SJohn Baldwin static int pci_pir_choose_irq(struct pci_link *pci_link, int irqmask);
852e41ba54SJohn Baldwin static void pci_pir_create_links(struct PIR_entry *entry,
862e41ba54SJohn Baldwin struct PIR_intpin *intpin, void *arg);
872e41ba54SJohn Baldwin static void pci_pir_dump_links(void);
882e41ba54SJohn Baldwin static struct pci_link *pci_pir_find_link(uint8_t link_id);
892e41ba54SJohn Baldwin static void pci_pir_find_link_handler(struct PIR_entry *entry,
902e41ba54SJohn Baldwin struct PIR_intpin *intpin, void *arg);
912e41ba54SJohn Baldwin static void pci_pir_initial_irqs(struct PIR_entry *entry,
922e41ba54SJohn Baldwin struct PIR_intpin *intpin, void *arg);
937a64d8d7SJohn Baldwin static void pci_pir_parse(void);
942e41ba54SJohn Baldwin static uint8_t pci_pir_search_irq(int bus, int device, int pin);
952e41ba54SJohn Baldwin static int pci_pir_valid_irq(struct pci_link *pci_link, int irq);
962e41ba54SJohn Baldwin static void pci_pir_walk_table(pir_entry_handler *handler, void *arg);
972e41ba54SJohn Baldwin
98c711aea6SPoul-Henning Kamp static MALLOC_DEFINE(M_PIR, "$PIR", "$PIR structures");
99300451c4SMike Smith
100099d058bSMike Smith static struct PIR_table *pci_route_table;
1017a64d8d7SJohn Baldwin static device_t pir_device;
1022e41ba54SJohn Baldwin static int pci_route_count, pir_bios_irqs, pir_parsed;
1032e41ba54SJohn Baldwin static TAILQ_HEAD(, pci_link) pci_links;
1042e41ba54SJohn Baldwin static int pir_interrupt_weight[NUM_ISA_INTERRUPTS];
105af3d516fSPeter Wemm
106e86bd39aSWarner Losh /* sysctl vars */
107e86bd39aSWarner Losh SYSCTL_DECL(_hw_pci);
108e86bd39aSWarner Losh
109b3ffa2aeSWarner Losh /* XXX this likely should live in a header file */
1102e41ba54SJohn Baldwin /* IRQs 3, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15 */
1112e41ba54SJohn Baldwin #define PCI_IRQ_OVERRIDE_MASK 0xdef8
112126ef7fcSYoshihiro Takahashi
113126ef7fcSYoshihiro Takahashi static uint32_t pci_irq_override_mask = PCI_IRQ_OVERRIDE_MASK;
114184dcdc7SMike Silbersack SYSCTL_INT(_hw_pci, OID_AUTO, irq_override_mask, CTLFLAG_RDTUN,
115126ef7fcSYoshihiro Takahashi &pci_irq_override_mask, PCI_IRQ_OVERRIDE_MASK,
116e86bd39aSWarner Losh "Mask of allowed irqs to try to route when it has no good clue about\n"
117e86bd39aSWarner Losh "which irqs it should use.");
118e86bd39aSWarner Losh
11954c9005fSWarner Losh /*
12054c9005fSWarner Losh * Look for the interrupt routing table.
121a8c18609SWarner Losh *
1222e41ba54SJohn Baldwin * We use PCI BIOS's PIR table if it's available. $PIR is the standard way
1232e41ba54SJohn Baldwin * to do this. Sadly, some machines are not standards conforming and have
1242e41ba54SJohn Baldwin * _PIR instead. We shrug and cope by looking for both.
12554c9005fSWarner Losh */
1262e41ba54SJohn Baldwin void
pci_pir_open(void)1272e41ba54SJohn Baldwin pci_pir_open(void)
1282e41ba54SJohn Baldwin {
1292e41ba54SJohn Baldwin struct PIR_table *pt;
1302e41ba54SJohn Baldwin uint32_t sigaddr;
1312e41ba54SJohn Baldwin int i;
1322e41ba54SJohn Baldwin uint8_t ck, *cv;
1332e41ba54SJohn Baldwin
1342e41ba54SJohn Baldwin /* Don't try if we've already found a table. */
1352e41ba54SJohn Baldwin if (pci_route_table != NULL)
1362e41ba54SJohn Baldwin return;
1372e41ba54SJohn Baldwin
1382e41ba54SJohn Baldwin /* Look for $PIR and then _PIR. */
139a8c18609SWarner Losh sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0);
140a8c18609SWarner Losh if (sigaddr == 0)
141a8c18609SWarner Losh sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0);
1422e41ba54SJohn Baldwin if (sigaddr == 0)
1432e41ba54SJohn Baldwin return;
144ed95805eSJohn Baldwin
1452e41ba54SJohn Baldwin /* If we found something, check the checksum and length. */
1462e41ba54SJohn Baldwin /* XXX - Use pmap_mapdev()? */
1472e41ba54SJohn Baldwin pt = (struct PIR_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
1482e41ba54SJohn Baldwin if (pt->pt_header.ph_length <= sizeof(struct PIR_header))
1492e41ba54SJohn Baldwin return;
150a8c18609SWarner Losh for (cv = (u_int8_t *)pt, ck = 0, i = 0;
1512e41ba54SJohn Baldwin i < (pt->pt_header.ph_length); i++)
15254c9005fSWarner Losh ck += cv[i];
1532e41ba54SJohn Baldwin if (ck != 0)
1542e41ba54SJohn Baldwin return;
1552e41ba54SJohn Baldwin
1562e41ba54SJohn Baldwin /* Ok, we've got a valid table. */
157099d058bSMike Smith pci_route_table = pt;
158a8c18609SWarner Losh pci_route_count = (pt->pt_header.ph_length -
159e300f53cSWarner Losh sizeof(struct PIR_header)) /
160e300f53cSWarner Losh sizeof(struct PIR_entry);
1617a64d8d7SJohn Baldwin }
162300451c4SMike Smith
16312a02d6eSMike Smith /*
1642e41ba54SJohn Baldwin * Find the pci_link structure for a given link ID.
16512a02d6eSMike Smith */
1662e41ba54SJohn Baldwin static struct pci_link *
pci_pir_find_link(uint8_t link_id)1672e41ba54SJohn Baldwin pci_pir_find_link(uint8_t link_id)
168bb0d0a8eSMike Smith {
1692e41ba54SJohn Baldwin struct pci_link *pci_link;
170e300f53cSWarner Losh
1712e41ba54SJohn Baldwin TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
1722e41ba54SJohn Baldwin if (pci_link->pl_id == link_id)
1732e41ba54SJohn Baldwin return (pci_link);
174d5ccecfaSWarner Losh }
1752e41ba54SJohn Baldwin return (NULL);
176bb0d0a8eSMike Smith }
177bb0d0a8eSMike Smith
17812a02d6eSMike Smith /*
1792e41ba54SJohn Baldwin * Find the link device associated with a PCI device in the table.
18012a02d6eSMike Smith */
1812e41ba54SJohn Baldwin static void
pci_pir_find_link_handler(struct PIR_entry * entry,struct PIR_intpin * intpin,void * arg)1822e41ba54SJohn Baldwin pci_pir_find_link_handler(struct PIR_entry *entry, struct PIR_intpin *intpin,
1832e41ba54SJohn Baldwin void *arg)
18412a02d6eSMike Smith {
1852e41ba54SJohn Baldwin struct pci_link_lookup *lookup;
186af3d516fSPeter Wemm
1872e41ba54SJohn Baldwin lookup = (struct pci_link_lookup *)arg;
1882e41ba54SJohn Baldwin if (entry->pe_bus == lookup->bus &&
1892e41ba54SJohn Baldwin entry->pe_device == lookup->device &&
1902e41ba54SJohn Baldwin intpin - entry->pe_intpin == lookup->pin)
1912e41ba54SJohn Baldwin *lookup->pci_link_ptr = pci_pir_find_link(intpin->link);
19212a02d6eSMike Smith }
19312a02d6eSMike Smith
19412a02d6eSMike Smith /*
1952e41ba54SJohn Baldwin * Check to see if a possible IRQ setting is valid.
1968ab96fd8SJohn Baldwin */
1978ab96fd8SJohn Baldwin static int
pci_pir_valid_irq(struct pci_link * pci_link,int irq)1982e41ba54SJohn Baldwin pci_pir_valid_irq(struct pci_link *pci_link, int irq)
1998ab96fd8SJohn Baldwin {
2008ab96fd8SJohn Baldwin
2018ab96fd8SJohn Baldwin if (!PCI_INTERRUPT_VALID(irq))
2028ab96fd8SJohn Baldwin return (0);
2032e41ba54SJohn Baldwin return (pci_link->pl_irqmask & (1 << irq));
2048ab96fd8SJohn Baldwin }
2058ab96fd8SJohn Baldwin
2068ab96fd8SJohn Baldwin /*
2072e41ba54SJohn Baldwin * Walk the $PIR executing the worker function for each valid intpin entry
2082e41ba54SJohn Baldwin * in the table. The handler is passed a pointer to both the entry and
2092e41ba54SJohn Baldwin * the intpin in the entry.
210099d058bSMike Smith */
2112e41ba54SJohn Baldwin static void
pci_pir_walk_table(pir_entry_handler * handler,void * arg)2122e41ba54SJohn Baldwin pci_pir_walk_table(pir_entry_handler *handler, void *arg)
213099d058bSMike Smith {
2142e41ba54SJohn Baldwin struct PIR_entry *entry;
2152e41ba54SJohn Baldwin struct PIR_intpin *intpin;
2162e41ba54SJohn Baldwin int i, pin;
217099d058bSMike Smith
2182e41ba54SJohn Baldwin entry = &pci_route_table->pt_entry[0];
2192e41ba54SJohn Baldwin for (i = 0; i < pci_route_count; i++, entry++) {
2202e41ba54SJohn Baldwin intpin = &entry->pe_intpin[0];
2212e41ba54SJohn Baldwin for (pin = 0; pin < 4; pin++, intpin++)
2222e41ba54SJohn Baldwin if (intpin->link != 0)
2232e41ba54SJohn Baldwin handler(entry, intpin, arg);
2242e41ba54SJohn Baldwin }
2252e41ba54SJohn Baldwin }
2262e41ba54SJohn Baldwin
2272e41ba54SJohn Baldwin static void
pci_pir_create_links(struct PIR_entry * entry,struct PIR_intpin * intpin,void * arg)2282e41ba54SJohn Baldwin pci_pir_create_links(struct PIR_entry *entry, struct PIR_intpin *intpin,
2292e41ba54SJohn Baldwin void *arg)
2302e41ba54SJohn Baldwin {
2312e41ba54SJohn Baldwin struct pci_link *pci_link;
2322e41ba54SJohn Baldwin
2332e41ba54SJohn Baldwin pci_link = pci_pir_find_link(intpin->link);
2342e41ba54SJohn Baldwin if (pci_link != NULL) {
2352e41ba54SJohn Baldwin pci_link->pl_references++;
2362e41ba54SJohn Baldwin if (intpin->irqs != pci_link->pl_irqmask) {
2372e41ba54SJohn Baldwin if (bootverbose)
2382e41ba54SJohn Baldwin printf(
2392e41ba54SJohn Baldwin "$PIR: Entry %d.%d.INT%c has different mask for link %#x, merging\n",
2402e41ba54SJohn Baldwin entry->pe_bus, entry->pe_device,
2412e41ba54SJohn Baldwin (intpin - entry->pe_intpin) + 'A',
2422e41ba54SJohn Baldwin pci_link->pl_id);
2432e41ba54SJohn Baldwin pci_link->pl_irqmask &= intpin->irqs;
2442e41ba54SJohn Baldwin }
2452e41ba54SJohn Baldwin } else {
2462e41ba54SJohn Baldwin pci_link = malloc(sizeof(struct pci_link), M_PIR, M_WAITOK);
2472e41ba54SJohn Baldwin pci_link->pl_id = intpin->link;
2482e41ba54SJohn Baldwin pci_link->pl_irqmask = intpin->irqs;
2492e41ba54SJohn Baldwin pci_link->pl_irq = PCI_INVALID_IRQ;
2502e41ba54SJohn Baldwin pci_link->pl_references = 1;
2512e41ba54SJohn Baldwin pci_link->pl_routed = 0;
2522e41ba54SJohn Baldwin TAILQ_INSERT_TAIL(&pci_links, pci_link, pl_links);
2532e41ba54SJohn Baldwin }
2542e41ba54SJohn Baldwin }
2552e41ba54SJohn Baldwin
2562e41ba54SJohn Baldwin /*
257a04725cdSJohn Baldwin * Look to see if any of the functions on the PCI device at bus/device
258a04725cdSJohn Baldwin * have an interrupt routed to intpin 'pin' by the BIOS.
2592e41ba54SJohn Baldwin */
2602e41ba54SJohn Baldwin static uint8_t
pci_pir_search_irq(int bus,int device,int pin)2612e41ba54SJohn Baldwin pci_pir_search_irq(int bus, int device, int pin)
2622e41ba54SJohn Baldwin {
2632e41ba54SJohn Baldwin uint32_t value;
2642e41ba54SJohn Baldwin uint8_t func, maxfunc;
2652e41ba54SJohn Baldwin
2662e41ba54SJohn Baldwin /* See if we have a valid device at function 0. */
2671587a9dbSJohn Baldwin value = pci_cfgregread(0, bus, device, 0, PCIR_VENDOR, 2);
268a04725cdSJohn Baldwin if (value == PCIV_INVALID)
269a04725cdSJohn Baldwin return (PCI_INVALID_IRQ);
2701587a9dbSJohn Baldwin value = pci_cfgregread(0, bus, device, 0, PCIR_HDRTYPE, 1);
2712e41ba54SJohn Baldwin if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
2722e41ba54SJohn Baldwin return (PCI_INVALID_IRQ);
2732e41ba54SJohn Baldwin if (value & PCIM_MFDEV)
2742e41ba54SJohn Baldwin maxfunc = PCI_FUNCMAX;
2752e41ba54SJohn Baldwin else
2762e41ba54SJohn Baldwin maxfunc = 0;
2772e41ba54SJohn Baldwin
2782e41ba54SJohn Baldwin /* Scan all possible functions at this device. */
2792e41ba54SJohn Baldwin for (func = 0; func <= maxfunc; func++) {
2801587a9dbSJohn Baldwin value = pci_cfgregread(0, bus, device, func, PCIR_VENDOR, 2);
281a04725cdSJohn Baldwin if (value == PCIV_INVALID)
2822e41ba54SJohn Baldwin continue;
2831587a9dbSJohn Baldwin value = pci_cfgregread(0, bus, device, func, PCIR_INTPIN, 1);
2842e41ba54SJohn Baldwin
2852e41ba54SJohn Baldwin /*
2862e41ba54SJohn Baldwin * See if it uses the pin in question. Note that the passed
2872e41ba54SJohn Baldwin * in pin uses 0 for A, .. 3 for D whereas the intpin
2882e41ba54SJohn Baldwin * register uses 0 for no interrupt, 1 for A, .. 4 for D.
2892e41ba54SJohn Baldwin */
2902e41ba54SJohn Baldwin if (value != pin + 1)
2912e41ba54SJohn Baldwin continue;
2921587a9dbSJohn Baldwin value = pci_cfgregread(0, bus, device, func, PCIR_INTLINE, 1);
2932e41ba54SJohn Baldwin if (bootverbose)
2942e41ba54SJohn Baldwin printf(
2952e41ba54SJohn Baldwin "$PIR: Found matching pin for %d.%d.INT%c at func %d: %d\n",
2962e41ba54SJohn Baldwin bus, device, pin + 'A', func, value);
2972e41ba54SJohn Baldwin if (value != PCI_INVALID_IRQ)
2982e41ba54SJohn Baldwin return (value);
299099d058bSMike Smith }
300e300f53cSWarner Losh return (PCI_INVALID_IRQ);
301099d058bSMike Smith }
302099d058bSMike Smith
303099d058bSMike Smith /*
3042e41ba54SJohn Baldwin * Try to initialize IRQ based on this device's IRQ.
305099d058bSMike Smith */
3062e41ba54SJohn Baldwin static void
pci_pir_initial_irqs(struct PIR_entry * entry,struct PIR_intpin * intpin,void * arg)3072e41ba54SJohn Baldwin pci_pir_initial_irqs(struct PIR_entry *entry, struct PIR_intpin *intpin,
3082e41ba54SJohn Baldwin void *arg)
309099d058bSMike Smith {
3102e41ba54SJohn Baldwin struct pci_link *pci_link;
3112e41ba54SJohn Baldwin uint8_t irq, pin;
312099d058bSMike Smith
3132e41ba54SJohn Baldwin pin = intpin - entry->pe_intpin;
3142e41ba54SJohn Baldwin pci_link = pci_pir_find_link(intpin->link);
3152e41ba54SJohn Baldwin irq = pci_pir_search_irq(entry->pe_bus, entry->pe_device, pin);
31684c7fde7SJohn Baldwin if (irq == PCI_INVALID_IRQ || irq == pci_link->pl_irq)
3172e41ba54SJohn Baldwin return;
31884c7fde7SJohn Baldwin
31911f3a4f0SJohn Baldwin /* Don't trust any BIOS IRQs greater than 15. */
32011f3a4f0SJohn Baldwin if (irq >= NUM_ISA_INTERRUPTS) {
32111f3a4f0SJohn Baldwin printf(
32211f3a4f0SJohn Baldwin "$PIR: Ignoring invalid BIOS IRQ %d from %d.%d.INT%c for link %#x\n",
32311f3a4f0SJohn Baldwin irq, entry->pe_bus, entry->pe_device, pin + 'A',
32411f3a4f0SJohn Baldwin pci_link->pl_id);
32511f3a4f0SJohn Baldwin return;
32611f3a4f0SJohn Baldwin }
32711f3a4f0SJohn Baldwin
32884c7fde7SJohn Baldwin /*
32984c7fde7SJohn Baldwin * If we don't have an IRQ for this link yet, then we trust the
33084c7fde7SJohn Baldwin * BIOS, even if it seems invalid from the $PIR entries.
33184c7fde7SJohn Baldwin */
33286f4fd6fSJohn Baldwin if (pci_link->pl_irq == PCI_INVALID_IRQ) {
33384c7fde7SJohn Baldwin if (!pci_pir_valid_irq(pci_link, irq))
33484c7fde7SJohn Baldwin printf(
33511f3a4f0SJohn Baldwin "$PIR: Using invalid BIOS IRQ %d from %d.%d.INT%c for link %#x\n",
33684c7fde7SJohn Baldwin irq, entry->pe_bus, entry->pe_device, pin + 'A',
33784c7fde7SJohn Baldwin pci_link->pl_id);
3382e41ba54SJohn Baldwin pci_link->pl_irq = irq;
33986f4fd6fSJohn Baldwin pci_link->pl_routed = 1;
34084c7fde7SJohn Baldwin return;
34184c7fde7SJohn Baldwin }
34284c7fde7SJohn Baldwin
34384c7fde7SJohn Baldwin /*
34484c7fde7SJohn Baldwin * We have an IRQ and it doesn't match the current IRQ for this
34584c7fde7SJohn Baldwin * link. If the new IRQ is invalid, then warn about it and ignore
34684c7fde7SJohn Baldwin * it. If the old IRQ is invalid and the new IRQ is valid, then
34784c7fde7SJohn Baldwin * prefer the new IRQ instead. If both IRQs are valid, then just
34884c7fde7SJohn Baldwin * use the first one. Note that if we ever get into this situation
34984c7fde7SJohn Baldwin * we are having to guess which setting the BIOS actually routed.
35084c7fde7SJohn Baldwin * Perhaps we should just give up instead.
35184c7fde7SJohn Baldwin */
35284c7fde7SJohn Baldwin if (!pci_pir_valid_irq(pci_link, irq)) {
3532e41ba54SJohn Baldwin printf(
3542e41ba54SJohn Baldwin "$PIR: BIOS IRQ %d for %d.%d.INT%c is not valid for link %#x\n",
3552e41ba54SJohn Baldwin irq, entry->pe_bus, entry->pe_device, pin + 'A',
3562e41ba54SJohn Baldwin pci_link->pl_id);
35784c7fde7SJohn Baldwin } else if (!pci_pir_valid_irq(pci_link, pci_link->pl_irq)) {
35884c7fde7SJohn Baldwin printf(
35984c7fde7SJohn Baldwin "$PIR: Preferring valid BIOS IRQ %d from %d.%d.INT%c for link %#x to IRQ %d\n",
36084c7fde7SJohn Baldwin irq, entry->pe_bus, entry->pe_device, pin + 'A',
36184c7fde7SJohn Baldwin pci_link->pl_id, pci_link->pl_irq);
36284c7fde7SJohn Baldwin pci_link->pl_irq = irq;
36384c7fde7SJohn Baldwin pci_link->pl_routed = 1;
36484c7fde7SJohn Baldwin } else
36584c7fde7SJohn Baldwin printf(
36684c7fde7SJohn Baldwin "$PIR: BIOS IRQ %d for %d.%d.INT%c does not match link %#x irq %d\n",
36784c7fde7SJohn Baldwin irq, entry->pe_bus, entry->pe_device, pin + 'A',
36884c7fde7SJohn Baldwin pci_link->pl_id, pci_link->pl_irq);
369099d058bSMike Smith }
370099d058bSMike Smith
371e300f53cSWarner Losh /*
3722e41ba54SJohn Baldwin * Parse $PIR to enumerate link devices and attempt to determine their
3732e41ba54SJohn Baldwin * initial state. This could perhaps be cleaner if we had drivers for the
3742e41ba54SJohn Baldwin * various interrupt routers as they could read the initial IRQ for each
3752e41ba54SJohn Baldwin * link.
376e300f53cSWarner Losh */
3777a64d8d7SJohn Baldwin static void
pci_pir_parse(void)3782e41ba54SJohn Baldwin pci_pir_parse(void)
379099d058bSMike Smith {
3802e41ba54SJohn Baldwin char tunable_buffer[64];
3812e41ba54SJohn Baldwin struct pci_link *pci_link;
3822e41ba54SJohn Baldwin int i, irq;
3832e41ba54SJohn Baldwin
3842e41ba54SJohn Baldwin /* Only parse once. */
3852e41ba54SJohn Baldwin if (pir_parsed)
3862e41ba54SJohn Baldwin return;
3872e41ba54SJohn Baldwin pir_parsed = 1;
3882e41ba54SJohn Baldwin
3892e41ba54SJohn Baldwin /* Enumerate link devices. */
3902e41ba54SJohn Baldwin TAILQ_INIT(&pci_links);
3912e41ba54SJohn Baldwin pci_pir_walk_table(pci_pir_create_links, NULL);
3922e41ba54SJohn Baldwin if (bootverbose) {
3932e41ba54SJohn Baldwin printf("$PIR: Links after initial probe:\n");
3942e41ba54SJohn Baldwin pci_pir_dump_links();
3952e41ba54SJohn Baldwin }
3962e41ba54SJohn Baldwin
397099d058bSMike Smith /*
3982e41ba54SJohn Baldwin * Check to see if the BIOS has already routed any of the links by
3992e41ba54SJohn Baldwin * checking each device connected to each link to see if it has a
4002e41ba54SJohn Baldwin * valid IRQ.
401099d058bSMike Smith */
4022e41ba54SJohn Baldwin pci_pir_walk_table(pci_pir_initial_irqs, NULL);
4032e41ba54SJohn Baldwin if (bootverbose) {
4042e41ba54SJohn Baldwin printf("$PIR: Links after initial IRQ discovery:\n");
4052e41ba54SJohn Baldwin pci_pir_dump_links();
4062e41ba54SJohn Baldwin }
407099d058bSMike Smith
408099d058bSMike Smith /*
40984c7fde7SJohn Baldwin * Allow the user to override the IRQ for a given link device. We
41084c7fde7SJohn Baldwin * allow invalid IRQs to be specified but warn about them. An IRQ
41184c7fde7SJohn Baldwin * of 255 or 0 clears any preset IRQ.
412099d058bSMike Smith */
4132e41ba54SJohn Baldwin i = 0;
4142e41ba54SJohn Baldwin TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
4152e41ba54SJohn Baldwin snprintf(tunable_buffer, sizeof(tunable_buffer),
4162e41ba54SJohn Baldwin "hw.pci.link.%#x.irq", pci_link->pl_id);
4172e41ba54SJohn Baldwin if (getenv_int(tunable_buffer, &irq) == 0)
4182e41ba54SJohn Baldwin continue;
4192e41ba54SJohn Baldwin if (irq == 0)
420e300f53cSWarner Losh irq = PCI_INVALID_IRQ;
42184c7fde7SJohn Baldwin if (irq != PCI_INVALID_IRQ &&
42284c7fde7SJohn Baldwin !pci_pir_valid_irq(pci_link, irq) && bootverbose)
42384c7fde7SJohn Baldwin printf(
42484c7fde7SJohn Baldwin "$PIR: Warning, IRQ %d for link %#x is not listed as valid\n",
42584c7fde7SJohn Baldwin irq, pci_link->pl_id);
4267a64d8d7SJohn Baldwin pci_link->pl_routed = 0;
4272e41ba54SJohn Baldwin pci_link->pl_irq = irq;
4282e41ba54SJohn Baldwin i = 1;
4296c9eb5f3SMike Smith }
4302e41ba54SJohn Baldwin if (bootverbose && i) {
4312e41ba54SJohn Baldwin printf("$PIR: Links after tunable overrides:\n");
4322e41ba54SJohn Baldwin pci_pir_dump_links();
433099d058bSMike Smith }
434099d058bSMike Smith
435099d058bSMike Smith /*
4362e41ba54SJohn Baldwin * Build initial interrupt weights as well as bitmap of "known-good"
4372e41ba54SJohn Baldwin * IRQs that the BIOS has already used for PCI link devices.
4382e41ba54SJohn Baldwin */
4392e41ba54SJohn Baldwin TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
4402e41ba54SJohn Baldwin if (!PCI_INTERRUPT_VALID(pci_link->pl_irq))
4412e41ba54SJohn Baldwin continue;
4422e41ba54SJohn Baldwin pir_bios_irqs |= 1 << pci_link->pl_irq;
4432e41ba54SJohn Baldwin pir_interrupt_weight[pci_link->pl_irq] +=
4442e41ba54SJohn Baldwin pci_link->pl_references;
4452e41ba54SJohn Baldwin }
4462e41ba54SJohn Baldwin if (bootverbose) {
4472e41ba54SJohn Baldwin printf("$PIR: IRQs used by BIOS: ");
4482e41ba54SJohn Baldwin pci_print_irqmask(pir_bios_irqs);
4492e41ba54SJohn Baldwin printf("\n");
4502e41ba54SJohn Baldwin printf("$PIR: Interrupt Weights:\n[ ");
4512e41ba54SJohn Baldwin for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
4522e41ba54SJohn Baldwin printf(" %3d", i);
4532e41ba54SJohn Baldwin printf(" ]\n[ ");
4542e41ba54SJohn Baldwin for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
4552e41ba54SJohn Baldwin printf(" %3d", pir_interrupt_weight[i]);
4562e41ba54SJohn Baldwin printf(" ]\n");
4572e41ba54SJohn Baldwin }
4582e41ba54SJohn Baldwin }
4592e41ba54SJohn Baldwin
4602e41ba54SJohn Baldwin /*
4612e41ba54SJohn Baldwin * Use the PCI BIOS to route an interrupt for a given device.
4622e41ba54SJohn Baldwin *
4632e41ba54SJohn Baldwin * Input:
4642e41ba54SJohn Baldwin * AX = PCIBIOS_ROUTE_INTERRUPT
4652e41ba54SJohn Baldwin * BH = bus
4662e41ba54SJohn Baldwin * BL = device [7:3] / function [2:0]
4672e41ba54SJohn Baldwin * CH = IRQ
4682e41ba54SJohn Baldwin * CL = Interrupt Pin (0x0A = A, ... 0x0D = D)
469099d058bSMike Smith */
470099d058bSMike Smith static int
pci_pir_biosroute(int bus,int device,int func,int pin,int irq)4712e41ba54SJohn Baldwin pci_pir_biosroute(int bus, int device, int func, int pin, int irq)
472099d058bSMike Smith {
4732e41ba54SJohn Baldwin struct bios_regs args;
4742e41ba54SJohn Baldwin
4752e41ba54SJohn Baldwin args.eax = PCIBIOS_ROUTE_INTERRUPT;
4762e41ba54SJohn Baldwin args.ebx = (bus << 8) | (device << 3) | func;
4772e41ba54SJohn Baldwin args.ecx = (irq << 8) | (0xa + pin);
4782e41ba54SJohn Baldwin return (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)));
4792e41ba54SJohn Baldwin }
4802e41ba54SJohn Baldwin
481e300f53cSWarner Losh /*
4822e41ba54SJohn Baldwin * Route a PCI interrupt using a link device from the $PIR.
483e300f53cSWarner Losh */
4842e41ba54SJohn Baldwin int
pci_pir_route_interrupt(int bus,int device,int func,int pin)4852e41ba54SJohn Baldwin pci_pir_route_interrupt(int bus, int device, int func, int pin)
4862e41ba54SJohn Baldwin {
4872e41ba54SJohn Baldwin struct pci_link_lookup lookup;
4882e41ba54SJohn Baldwin struct pci_link *pci_link;
4892e41ba54SJohn Baldwin int error, irq;
490099d058bSMike Smith
4912e41ba54SJohn Baldwin if (pci_route_table == NULL)
492e300f53cSWarner Losh return (PCI_INVALID_IRQ);
4932e41ba54SJohn Baldwin
4942e41ba54SJohn Baldwin /* Lookup link device for this PCI device/pin. */
4952e41ba54SJohn Baldwin pci_link = NULL;
4962e41ba54SJohn Baldwin lookup.bus = bus;
4972e41ba54SJohn Baldwin lookup.device = device;
4982e41ba54SJohn Baldwin lookup.pin = pin - 1;
4992e41ba54SJohn Baldwin lookup.pci_link_ptr = &pci_link;
5002e41ba54SJohn Baldwin pci_pir_walk_table(pci_pir_find_link_handler, &lookup);
5012e41ba54SJohn Baldwin if (pci_link == NULL) {
5022e41ba54SJohn Baldwin printf("$PIR: No matching entry for %d.%d.INT%c\n", bus,
5032e41ba54SJohn Baldwin device, pin - 1 + 'A');
5042e41ba54SJohn Baldwin return (PCI_INVALID_IRQ);
5052e41ba54SJohn Baldwin }
5062e41ba54SJohn Baldwin
5072e41ba54SJohn Baldwin /*
50869296991SJohn Baldwin * Pick a new interrupt if we don't have one already. We look
50969296991SJohn Baldwin * for an interrupt from several different sets. First, if
51069296991SJohn Baldwin * this link only has one valid IRQ, use that. Second, we
51169296991SJohn Baldwin * check the set of PCI only interrupts from the $PIR. Third,
51269296991SJohn Baldwin * we check the set of known-good interrupts that the BIOS has
51369296991SJohn Baldwin * already used. Lastly, we check the "all possible valid
51469296991SJohn Baldwin * IRQs" set.
5152e41ba54SJohn Baldwin */
5162e41ba54SJohn Baldwin if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
51769296991SJohn Baldwin if (pci_link->pl_irqmask != 0 && powerof2(pci_link->pl_irqmask))
51869296991SJohn Baldwin irq = ffs(pci_link->pl_irqmask) - 1;
51969296991SJohn Baldwin else
5202e41ba54SJohn Baldwin irq = pci_pir_choose_irq(pci_link,
5212e41ba54SJohn Baldwin pci_route_table->pt_header.ph_pci_irqs);
5222e41ba54SJohn Baldwin if (!PCI_INTERRUPT_VALID(irq))
5232e41ba54SJohn Baldwin irq = pci_pir_choose_irq(pci_link, pir_bios_irqs);
5242e41ba54SJohn Baldwin if (!PCI_INTERRUPT_VALID(irq))
5252e41ba54SJohn Baldwin irq = pci_pir_choose_irq(pci_link,
5262e41ba54SJohn Baldwin pci_irq_override_mask);
5272e41ba54SJohn Baldwin if (!PCI_INTERRUPT_VALID(irq)) {
5282e41ba54SJohn Baldwin if (bootverbose)
5292e41ba54SJohn Baldwin printf(
5302e41ba54SJohn Baldwin "$PIR: Failed to route interrupt for %d:%d INT%c\n",
5312e41ba54SJohn Baldwin bus, device, pin - 1 + 'A');
5322e41ba54SJohn Baldwin return (PCI_INVALID_IRQ);
5332e41ba54SJohn Baldwin }
5342e41ba54SJohn Baldwin pci_link->pl_irq = irq;
5352e41ba54SJohn Baldwin }
5362e41ba54SJohn Baldwin
5372e41ba54SJohn Baldwin /* Ask the BIOS to route this IRQ if we haven't done so already. */
5382e41ba54SJohn Baldwin if (!pci_link->pl_routed) {
5392e41ba54SJohn Baldwin error = pci_pir_biosroute(bus, device, func, pin - 1,
5402e41ba54SJohn Baldwin pci_link->pl_irq);
5412e41ba54SJohn Baldwin
5422e41ba54SJohn Baldwin /* Ignore errors when routing a unique interrupt. */
5432e41ba54SJohn Baldwin if (error && !powerof2(pci_link->pl_irqmask)) {
5442e41ba54SJohn Baldwin printf("$PIR: ROUTE_INTERRUPT failed.\n");
5452e41ba54SJohn Baldwin return (PCI_INVALID_IRQ);
5462e41ba54SJohn Baldwin }
5472e41ba54SJohn Baldwin pci_link->pl_routed = 1;
5487a64d8d7SJohn Baldwin
5497a64d8d7SJohn Baldwin /* Ensure the interrupt is set to level/low trigger. */
5507a64d8d7SJohn Baldwin KASSERT(pir_device != NULL, ("missing pir device"));
5517a64d8d7SJohn Baldwin BUS_CONFIG_INTR(pir_device, pci_link->pl_irq,
5527a64d8d7SJohn Baldwin INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
5532e41ba54SJohn Baldwin }
55439981fedSJohn Baldwin if (bootverbose)
5552e41ba54SJohn Baldwin printf("$PIR: %d:%d INT%c routed to irq %d\n", bus, device,
5562e41ba54SJohn Baldwin pin - 1 + 'A', pci_link->pl_irq);
5572e41ba54SJohn Baldwin return (pci_link->pl_irq);
5582e41ba54SJohn Baldwin }
5592e41ba54SJohn Baldwin
5602e41ba54SJohn Baldwin /*
5612e41ba54SJohn Baldwin * Try to pick an interrupt for the specified link from the interrupts
5622e41ba54SJohn Baldwin * set in the mask.
5632e41ba54SJohn Baldwin */
5642e41ba54SJohn Baldwin static int
pci_pir_choose_irq(struct pci_link * pci_link,int irqmask)5652e41ba54SJohn Baldwin pci_pir_choose_irq(struct pci_link *pci_link, int irqmask)
5662e41ba54SJohn Baldwin {
5672e41ba54SJohn Baldwin int i, irq, realmask;
5682e41ba54SJohn Baldwin
5692e41ba54SJohn Baldwin /* XXX: Need to have a #define of known bad IRQs to also mask out? */
5702e41ba54SJohn Baldwin realmask = pci_link->pl_irqmask & irqmask;
5712e41ba54SJohn Baldwin if (realmask == 0)
5722e41ba54SJohn Baldwin return (PCI_INVALID_IRQ);
5732e41ba54SJohn Baldwin
5742e41ba54SJohn Baldwin /* Find IRQ with lowest weight. */
5752e41ba54SJohn Baldwin irq = PCI_INVALID_IRQ;
5762e41ba54SJohn Baldwin for (i = 0; i < NUM_ISA_INTERRUPTS; i++) {
5772e41ba54SJohn Baldwin if (!(realmask & 1 << i))
5782e41ba54SJohn Baldwin continue;
5792e41ba54SJohn Baldwin if (irq == PCI_INVALID_IRQ ||
5802e41ba54SJohn Baldwin pir_interrupt_weight[i] < pir_interrupt_weight[irq])
5812e41ba54SJohn Baldwin irq = i;
5822e41ba54SJohn Baldwin }
5832e41ba54SJohn Baldwin if (bootverbose && PCI_INTERRUPT_VALID(irq)) {
5842e41ba54SJohn Baldwin printf("$PIR: Found IRQ %d for link %#x from ", irq,
5852e41ba54SJohn Baldwin pci_link->pl_id);
5862e41ba54SJohn Baldwin pci_print_irqmask(realmask);
5872e41ba54SJohn Baldwin printf("\n");
5882e41ba54SJohn Baldwin }
5892e41ba54SJohn Baldwin return (irq);
59054c9005fSWarner Losh }
59154c9005fSWarner Losh
592fbabd7beSJohn Baldwin static void
pci_print_irqmask(u_int16_t irqs)593fbabd7beSJohn Baldwin pci_print_irqmask(u_int16_t irqs)
594fbabd7beSJohn Baldwin {
595fbabd7beSJohn Baldwin int i, first;
596fbabd7beSJohn Baldwin
597fbabd7beSJohn Baldwin if (irqs == 0) {
598fbabd7beSJohn Baldwin printf("none");
599fbabd7beSJohn Baldwin return;
600fbabd7beSJohn Baldwin }
601fbabd7beSJohn Baldwin first = 1;
602fbabd7beSJohn Baldwin for (i = 0; i < 16; i++, irqs >>= 1)
603fbabd7beSJohn Baldwin if (irqs & 1) {
604fbabd7beSJohn Baldwin if (!first)
605fbabd7beSJohn Baldwin printf(" ");
606fbabd7beSJohn Baldwin else
607fbabd7beSJohn Baldwin first = 0;
608fbabd7beSJohn Baldwin printf("%d", i);
609fbabd7beSJohn Baldwin }
610fbabd7beSJohn Baldwin }
611fbabd7beSJohn Baldwin
612fbabd7beSJohn Baldwin /*
6132e41ba54SJohn Baldwin * Display link devices.
6142e41ba54SJohn Baldwin */
6152e41ba54SJohn Baldwin static void
pci_pir_dump_links(void)6162e41ba54SJohn Baldwin pci_pir_dump_links(void)
6172e41ba54SJohn Baldwin {
6182e41ba54SJohn Baldwin struct pci_link *pci_link;
6192e41ba54SJohn Baldwin
6207a64d8d7SJohn Baldwin printf("Link IRQ Rtd Ref IRQs\n");
6212e41ba54SJohn Baldwin TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
6227a64d8d7SJohn Baldwin printf("%#4x %3d %c %3d ", pci_link->pl_id,
6237a64d8d7SJohn Baldwin pci_link->pl_irq, pci_link->pl_routed ? 'Y' : 'N',
6242e41ba54SJohn Baldwin pci_link->pl_references);
6252e41ba54SJohn Baldwin pci_print_irqmask(pci_link->pl_irqmask);
6262e41ba54SJohn Baldwin printf("\n");
627fbabd7beSJohn Baldwin }
628fbabd7beSJohn Baldwin }
62954c9005fSWarner Losh
63054c9005fSWarner Losh /*
631c3ba1376SJohn Baldwin * See if any interrupts for a given PCI bus are routed in the PIR. Don't
6322e41ba54SJohn Baldwin * even bother looking if the BIOS doesn't support routing anyways. If we
6332e41ba54SJohn Baldwin * are probing a PCI-PCI bridge, then require_parse will be true and we should
6342e41ba54SJohn Baldwin * only succeed if a host-PCI bridge has already attached and parsed the PIR.
635c3ba1376SJohn Baldwin */
636c3ba1376SJohn Baldwin int
pci_pir_probe(int bus,int require_parse)6372e41ba54SJohn Baldwin pci_pir_probe(int bus, int require_parse)
638c3ba1376SJohn Baldwin {
639c3ba1376SJohn Baldwin int i;
640c3ba1376SJohn Baldwin
6412e41ba54SJohn Baldwin if (pci_route_table == NULL || (require_parse && !pir_parsed))
642c3ba1376SJohn Baldwin return (0);
643c3ba1376SJohn Baldwin for (i = 0; i < pci_route_count; i++)
644c3ba1376SJohn Baldwin if (pci_route_table->pt_entry[i].pe_bus == bus)
645c3ba1376SJohn Baldwin return (1);
646c3ba1376SJohn Baldwin return (0);
647c3ba1376SJohn Baldwin }
6487a64d8d7SJohn Baldwin
6497a64d8d7SJohn Baldwin /*
650315fbaecSEd Maste * The driver for the new-bus pseudo device pir0 for the $PIR table.
6517a64d8d7SJohn Baldwin */
6527a64d8d7SJohn Baldwin
6537a64d8d7SJohn Baldwin static int
pir_probe(device_t dev)6547a64d8d7SJohn Baldwin pir_probe(device_t dev)
6557a64d8d7SJohn Baldwin {
656*a96a9577SMark Johnston device_set_descf(dev, "PCI Interrupt Routing Table: %d Entries",
6577a64d8d7SJohn Baldwin pci_route_count);
6587a64d8d7SJohn Baldwin return (0);
6597a64d8d7SJohn Baldwin }
6607a64d8d7SJohn Baldwin
6617a64d8d7SJohn Baldwin static int
pir_attach(device_t dev)6627a64d8d7SJohn Baldwin pir_attach(device_t dev)
6637a64d8d7SJohn Baldwin {
6647a64d8d7SJohn Baldwin
6657a64d8d7SJohn Baldwin pci_pir_parse();
6667a64d8d7SJohn Baldwin KASSERT(pir_device == NULL, ("Multiple pir devices"));
6677a64d8d7SJohn Baldwin pir_device = dev;
6687a64d8d7SJohn Baldwin return (0);
6697a64d8d7SJohn Baldwin }
6707a64d8d7SJohn Baldwin
6717a64d8d7SJohn Baldwin static void
pir_resume_find_device(struct PIR_entry * entry,struct PIR_intpin * intpin,void * arg)6727a64d8d7SJohn Baldwin pir_resume_find_device(struct PIR_entry *entry, struct PIR_intpin *intpin,
6737a64d8d7SJohn Baldwin void *arg)
6747a64d8d7SJohn Baldwin {
6757a64d8d7SJohn Baldwin struct pci_dev_lookup *pd;
6767a64d8d7SJohn Baldwin
6777a64d8d7SJohn Baldwin pd = (struct pci_dev_lookup *)arg;
6787a64d8d7SJohn Baldwin if (intpin->link != pd->link || pd->bus != -1)
6797a64d8d7SJohn Baldwin return;
6807a64d8d7SJohn Baldwin pd->bus = entry->pe_bus;
6817a64d8d7SJohn Baldwin pd->device = entry->pe_device;
6827a64d8d7SJohn Baldwin pd->pin = intpin - entry->pe_intpin;
6837a64d8d7SJohn Baldwin }
6847a64d8d7SJohn Baldwin
6857a64d8d7SJohn Baldwin static int
pir_resume(device_t dev)6867a64d8d7SJohn Baldwin pir_resume(device_t dev)
6877a64d8d7SJohn Baldwin {
6887a64d8d7SJohn Baldwin struct pci_dev_lookup pd;
6897a64d8d7SJohn Baldwin struct pci_link *pci_link;
6907a64d8d7SJohn Baldwin int error;
6917a64d8d7SJohn Baldwin
6927a64d8d7SJohn Baldwin /* Ask the BIOS to re-route each link that was already routed. */
6937a64d8d7SJohn Baldwin TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
6947a64d8d7SJohn Baldwin if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
6957a64d8d7SJohn Baldwin KASSERT(!pci_link->pl_routed,
6967a64d8d7SJohn Baldwin ("link %#x is routed but has invalid PCI IRQ",
6977a64d8d7SJohn Baldwin pci_link->pl_id));
6987a64d8d7SJohn Baldwin continue;
6997a64d8d7SJohn Baldwin }
7007a64d8d7SJohn Baldwin if (pci_link->pl_routed) {
7017a64d8d7SJohn Baldwin pd.bus = -1;
7027a64d8d7SJohn Baldwin pd.link = pci_link->pl_id;
7037a64d8d7SJohn Baldwin pci_pir_walk_table(pir_resume_find_device, &pd);
7047a64d8d7SJohn Baldwin KASSERT(pd.bus != -1,
7057a64d8d7SJohn Baldwin ("did not find matching entry for link %#x in the $PIR table",
7067a64d8d7SJohn Baldwin pci_link->pl_id));
7077a64d8d7SJohn Baldwin if (bootverbose)
7087a64d8d7SJohn Baldwin device_printf(dev,
7097a64d8d7SJohn Baldwin "Using %d.%d.INT%c to route link %#x to IRQ %d\n",
7107a64d8d7SJohn Baldwin pd.bus, pd.device, pd.pin + 'A',
7117a64d8d7SJohn Baldwin pci_link->pl_id, pci_link->pl_irq);
7127a64d8d7SJohn Baldwin error = pci_pir_biosroute(pd.bus, pd.device, 0, pd.pin,
7137a64d8d7SJohn Baldwin pci_link->pl_irq);
7147a64d8d7SJohn Baldwin if (error)
7157a64d8d7SJohn Baldwin device_printf(dev,
7167a64d8d7SJohn Baldwin "ROUTE_INTERRUPT on resume for link %#x failed.\n",
7177a64d8d7SJohn Baldwin pci_link->pl_id);
7187a64d8d7SJohn Baldwin }
7197a64d8d7SJohn Baldwin }
7207a64d8d7SJohn Baldwin return (0);
7217a64d8d7SJohn Baldwin }
7227a64d8d7SJohn Baldwin
7237a64d8d7SJohn Baldwin static device_method_t pir_methods[] = {
7247a64d8d7SJohn Baldwin /* Device interface */
7257a64d8d7SJohn Baldwin DEVMETHOD(device_probe, pir_probe),
7267a64d8d7SJohn Baldwin DEVMETHOD(device_attach, pir_attach),
7277a64d8d7SJohn Baldwin DEVMETHOD(device_resume, pir_resume),
7287a64d8d7SJohn Baldwin { 0, 0 }
7297a64d8d7SJohn Baldwin };
7307a64d8d7SJohn Baldwin
7317a64d8d7SJohn Baldwin static driver_t pir_driver = {
7327a64d8d7SJohn Baldwin "pir",
7337a64d8d7SJohn Baldwin pir_methods,
7347a64d8d7SJohn Baldwin 1,
7357a64d8d7SJohn Baldwin };
7367a64d8d7SJohn Baldwin
7370f3a50d5SJohn Baldwin DRIVER_MODULE(pir, legacy, pir_driver, 0, 0);
738