1*72e44f84Sandvar /* $NetBSD: pci_intr_fixup.c,v 1.52 2021/09/16 21:29:41 andvar Exp $ */
22026e5f7Sthorpej
32026e5f7Sthorpej /*-
42026e5f7Sthorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
52026e5f7Sthorpej * All rights reserved.
62026e5f7Sthorpej *
72026e5f7Sthorpej * This code is derived from software contributed to The NetBSD Foundation
82026e5f7Sthorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
92026e5f7Sthorpej * NASA Ames Research Center.
102026e5f7Sthorpej *
112026e5f7Sthorpej * Redistribution and use in source and binary forms, with or without
122026e5f7Sthorpej * modification, are permitted provided that the following conditions
132026e5f7Sthorpej * are met:
142026e5f7Sthorpej * 1. Redistributions of source code must retain the above copyright
152026e5f7Sthorpej * notice, this list of conditions and the following disclaimer.
162026e5f7Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
172026e5f7Sthorpej * notice, this list of conditions and the following disclaimer in the
182026e5f7Sthorpej * documentation and/or other materials provided with the distribution.
192026e5f7Sthorpej *
202026e5f7Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
212026e5f7Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
222026e5f7Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
232026e5f7Sthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
242026e5f7Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252026e5f7Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262026e5f7Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272026e5f7Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282026e5f7Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292026e5f7Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302026e5f7Sthorpej * POSSIBILITY OF SUCH DAMAGE.
312026e5f7Sthorpej */
322026e5f7Sthorpej
332026e5f7Sthorpej /*
342026e5f7Sthorpej * Copyright (c) 1999, by UCHIYAMA Yasushi
352026e5f7Sthorpej * All rights reserved.
362026e5f7Sthorpej *
372026e5f7Sthorpej * Redistribution and use in source and binary forms, with or without
382026e5f7Sthorpej * modification, are permitted provided that the following conditions
392026e5f7Sthorpej * are met:
402026e5f7Sthorpej * 1. Redistributions of source code must retain the above copyright
412026e5f7Sthorpej * notice, this list of conditions and the following disclaimer.
422026e5f7Sthorpej * 2. The name of the developer may NOT be used to endorse or promote products
432026e5f7Sthorpej * derived from this software without specific prior written permission.
442026e5f7Sthorpej *
452026e5f7Sthorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
462026e5f7Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
472026e5f7Sthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
482026e5f7Sthorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
492026e5f7Sthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
502026e5f7Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
512026e5f7Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
522026e5f7Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
532026e5f7Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
542026e5f7Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
552026e5f7Sthorpej * SUCH DAMAGE.
562026e5f7Sthorpej */
572026e5f7Sthorpej
582026e5f7Sthorpej /*
592026e5f7Sthorpej * PCI Interrupt Router support.
602026e5f7Sthorpej */
612026e5f7Sthorpej
6295c969f2Slukem #include <sys/cdefs.h>
63*72e44f84Sandvar __KERNEL_RCSID(0, "$NetBSD: pci_intr_fixup.c,v 1.52 2021/09/16 21:29:41 andvar Exp $");
6495c969f2Slukem
652026e5f7Sthorpej #include "opt_pcibios.h"
660919f4d2Ssekiya #include "opt_pcifixup.h"
672026e5f7Sthorpej
682026e5f7Sthorpej #include <sys/param.h>
692026e5f7Sthorpej #include <sys/systm.h>
702026e5f7Sthorpej #include <sys/kernel.h>
712026e5f7Sthorpej #include <sys/malloc.h>
722026e5f7Sthorpej #include <sys/queue.h>
732026e5f7Sthorpej #include <sys/device.h>
742026e5f7Sthorpej
75f5b064eeSdyoung #include <sys/bus.h>
762026e5f7Sthorpej #include <machine/intr.h>
772026e5f7Sthorpej
782026e5f7Sthorpej #include <dev/pci/pcireg.h>
792026e5f7Sthorpej #include <dev/pci/pcivar.h>
802026e5f7Sthorpej #include <dev/pci/pcidevs.h>
812026e5f7Sthorpej
822026e5f7Sthorpej #include <i386/pci/pci_intr_fixup.h>
832026e5f7Sthorpej #include <i386/pci/pcibios.h>
842026e5f7Sthorpej
852026e5f7Sthorpej struct pciintr_link_map {
862026e5f7Sthorpej int link;
872026e5f7Sthorpej int clink;
882026e5f7Sthorpej int irq;
8968da4482Sperry uint16_t bitmap;
902026e5f7Sthorpej int fixup_stage;
912026e5f7Sthorpej SIMPLEQ_ENTRY(pciintr_link_map) list;
922026e5f7Sthorpej };
932026e5f7Sthorpej
94d5d707cfSonoe pciintr_icu_tag_t pciintr_icu_tag;
952026e5f7Sthorpej pciintr_icu_handle_t pciintr_icu_handle;
962026e5f7Sthorpej
9787b0dd6eSsoda #ifdef PCIBIOS_IRQS_HINT
9887b0dd6eSsoda int pcibios_irqs_hint = PCIBIOS_IRQS_HINT;
9987b0dd6eSsoda #endif
10087b0dd6eSsoda
101e8085086Skochi struct pciintr_link_map *pciintr_link_lookup(int);
102e8085086Skochi struct pciintr_link_map *pciintr_link_alloc(struct pcibios_intr_routing *,
103e8085086Skochi int);
104e8085086Skochi struct pcibios_intr_routing *pciintr_pir_lookup(int, int);
105e8085086Skochi static int pciintr_bitmap_count_irq(int, int *);
106e8085086Skochi static int pciintr_bitmap_find_lowest_irq(int, int *);
107e8085086Skochi int pciintr_link_init (void);
108b63f6b50Ssoda #ifdef PCIBIOS_INTR_GUESS
109e8085086Skochi int pciintr_guess_irq(void);
110b63f6b50Ssoda #endif
111e8085086Skochi int pciintr_link_fixup(void);
11268da4482Sperry int pciintr_link_route(uint16_t *);
11368da4482Sperry int pciintr_irq_release(uint16_t *);
114e8085086Skochi int pciintr_header_fixup(pci_chipset_tag_t);
115e8085086Skochi void pciintr_do_header_fixup(pci_chipset_tag_t, pcitag_t, void*);
1162026e5f7Sthorpej
1172026e5f7Sthorpej SIMPLEQ_HEAD(, pciintr_link_map) pciintr_link_map_list;
1182026e5f7Sthorpej
1192026e5f7Sthorpej const struct pciintr_icu_table {
1202026e5f7Sthorpej pci_vendor_id_t piit_vendor;
1212026e5f7Sthorpej pci_product_id_t piit_product;
122e8085086Skochi int (*piit_init)(pci_chipset_tag_t,
1232026e5f7Sthorpej bus_space_tag_t, pcitag_t, pciintr_icu_tag_t *,
124e8085086Skochi pciintr_icu_handle_t *);
125f86d875aSjmcneill void (*piit_uninit)(pciintr_icu_handle_t);
1262026e5f7Sthorpej } pciintr_icu_table[] = {
1272026e5f7Sthorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371MX,
128f86d875aSjmcneill piix_init, piix_uninit },
1292026e5f7Sthorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371AB_ISA,
130f86d875aSjmcneill piix_init, piix_uninit },
1312026e5f7Sthorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371FB_ISA,
132f86d875aSjmcneill piix_init, piix_uninit },
1332026e5f7Sthorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371SB_ISA,
134f86d875aSjmcneill piix_init, piix_uninit },
135dd74d927Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82440MX_ISA,
136f86d875aSjmcneill piix_init, piix_uninit },
137ee2683e7Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801AA_LPC,
138f86d875aSjmcneill piix_init, piix_uninit }, /* ICH */
139ee2683e7Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801AB_LPC,
140f86d875aSjmcneill piix_init, piix_uninit }, /* ICH0 */
1413d57cec0Shaya { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801BA_LPC,
14240be650cSchristos ich_init, NULL }, /* ICH2 */
143d5d707cfSonoe { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801BAM_LPC,
14440be650cSchristos ich_init, NULL }, /* ICH2M */
145ee2683e7Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801CA_LPC,
14640be650cSchristos ich_init, NULL }, /* ICH3S */
147ee2683e7Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801CAM_LPC,
14840be650cSchristos ich_init, NULL }, /* ICH3M */
149c9aa0785Skanaoka { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801DB_LPC,
15040be650cSchristos ich_init, NULL }, /* ICH4 */
1514ad86a18Smsaitoh { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801DBM_LPC,
15240be650cSchristos ich_init, NULL }, /* ICH4M */
153b491c05aSdyoung { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801EB_LPC,
15440be650cSchristos ich_init, NULL }, /* ICH5 */
155dd74d927Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801FB_LPC,
15640be650cSchristos ich_init, NULL }, /* ICH6/ICH6R */
15730a362abSrpaulo { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801FBM_LPC,
15840be650cSchristos ich_init, NULL }, /* ICH6M */
159dd74d927Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801G_LPC,
16040be650cSchristos ich_init, NULL }, /* ICH7/ICH7R */
161dd74d927Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801GBM_LPC,
16240be650cSchristos ich_init, NULL }, /* ICH7-M */
163dd74d927Skochi { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801GHM_LPC,
16440be650cSchristos ich_init, NULL }, /* ICH7DH/ICH7-M DH */
1652026e5f7Sthorpej
1662026e5f7Sthorpej { PCI_VENDOR_OPTI, PCI_PRODUCT_OPTI_82C558,
16740be650cSchristos opti82c558_init, NULL },
1682026e5f7Sthorpej { PCI_VENDOR_OPTI, PCI_PRODUCT_OPTI_82C700,
16940be650cSchristos opti82c700_init, NULL },
1702026e5f7Sthorpej
1712026e5f7Sthorpej { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT82C586_ISA,
17240be650cSchristos via82c586_init, NULL },
173773de18aSperry { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT82C596A,
17440be650cSchristos via82c586_init, NULL },
1756a808ce8Saymeric { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT82C686A_ISA,
17640be650cSchristos via82c586_init, NULL },
1772026e5f7Sthorpej
17837a21a63Sxtraeme { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8231,
17940be650cSchristos via8231_init, NULL },
180d05d5214Schristos { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8233,
18140be650cSchristos via82c586_init, NULL },
18237a21a63Sxtraeme { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8233A,
18340be650cSchristos via8231_init, NULL },
18437a21a63Sxtraeme { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8235,
18540be650cSchristos via8231_init, NULL },
18637a21a63Sxtraeme { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8237,
18740be650cSchristos via8231_init, NULL },
18837a21a63Sxtraeme
189d05d5214Schristos
1902026e5f7Sthorpej { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_85C503,
19140be650cSchristos sis85c503_init, NULL },
1929552b18eSxtraeme { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_962,
19340be650cSchristos sis85c503_init, NULL },
1949552b18eSxtraeme { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_963,
19540be650cSchristos sis85c503_init, NULL },
1962026e5f7Sthorpej
1979b730bc3Such { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_PBC756_PMC,
19840be650cSchristos amd756_init, NULL },
1990b27695aSxtraeme { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_PBC766_PMC,
20040be650cSchristos amd756_init, NULL },
2019552b18eSxtraeme { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_PBC768_PMC,
20240be650cSchristos amd756_init, NULL },
2039b730bc3Such
2049552b18eSxtraeme { PCI_VENDOR_ALI, PCI_PRODUCT_ALI_M1533,
20540be650cSchristos ali1543_init, NULL },
20631d98218Shaya { PCI_VENDOR_ALI, PCI_PRODUCT_ALI_M1543,
20740be650cSchristos ali1543_init, NULL },
20831d98218Shaya
2092026e5f7Sthorpej { 0, 0,
21040be650cSchristos NULL, NULL },
2112026e5f7Sthorpej };
2122026e5f7Sthorpej
213e8085086Skochi const struct pciintr_icu_table *pciintr_icu_lookup(pcireg_t);
2142026e5f7Sthorpej
2152026e5f7Sthorpej const struct pciintr_icu_table *
pciintr_icu_lookup(pcireg_t id)216e8085086Skochi pciintr_icu_lookup(pcireg_t id)
2172026e5f7Sthorpej {
2182026e5f7Sthorpej const struct pciintr_icu_table *piit;
2192026e5f7Sthorpej
2202026e5f7Sthorpej for (piit = pciintr_icu_table;
2212026e5f7Sthorpej piit->piit_init != NULL;
2222026e5f7Sthorpej piit++) {
2232026e5f7Sthorpej if (PCI_VENDOR(id) == piit->piit_vendor &&
2242026e5f7Sthorpej PCI_PRODUCT(id) == piit->piit_product)
2252026e5f7Sthorpej return (piit);
2262026e5f7Sthorpej }
2272026e5f7Sthorpej
2282026e5f7Sthorpej return (NULL);
2292026e5f7Sthorpej }
2302026e5f7Sthorpej
2312026e5f7Sthorpej struct pciintr_link_map *
pciintr_link_lookup(int link)232e8085086Skochi pciintr_link_lookup(int link)
2332026e5f7Sthorpej {
2342026e5f7Sthorpej struct pciintr_link_map *l;
2352026e5f7Sthorpej
23606de4264Slukem SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
2372026e5f7Sthorpej if (l->link == link)
2382026e5f7Sthorpej return (l);
2392026e5f7Sthorpej }
2402026e5f7Sthorpej
2412026e5f7Sthorpej return (NULL);
2422026e5f7Sthorpej }
2432026e5f7Sthorpej
2442026e5f7Sthorpej struct pciintr_link_map *
pciintr_link_alloc(struct pcibios_intr_routing * pir,int pin)245e8085086Skochi pciintr_link_alloc(struct pcibios_intr_routing *pir, int pin)
2462026e5f7Sthorpej {
24781858d7cSsoda int link = pir->linkmap[pin].link, clink, irq;
2482026e5f7Sthorpej struct pciintr_link_map *l, *lstart;
2492026e5f7Sthorpej
250b63f6b50Ssoda if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
25181858d7cSsoda /*
25281858d7cSsoda * Get the canonical link value for this entry.
25381858d7cSsoda */
254b63f6b50Ssoda if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
255b63f6b50Ssoda link, &clink) != 0) {
25681858d7cSsoda /*
25781858d7cSsoda * ICU doesn't understand the link value.
25881858d7cSsoda * Just ignore this PIR entry.
25981858d7cSsoda */
26081858d7cSsoda #ifdef DIAGNOSTIC
26181858d7cSsoda printf("pciintr_link_alloc: bus %d device %d: "
26281858d7cSsoda "link 0x%02x invalid\n",
26381858d7cSsoda pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link);
26481858d7cSsoda #endif
26581858d7cSsoda return (NULL);
26681858d7cSsoda }
26781858d7cSsoda
26881858d7cSsoda /*
269b63f6b50Ssoda * Check the link value by asking the ICU for the
270b63f6b50Ssoda * canonical link value.
27181858d7cSsoda * Also, determine if this PIRQ is mapped to an IRQ.
27281858d7cSsoda */
273b63f6b50Ssoda if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
274b63f6b50Ssoda clink, &irq) != 0) {
27581858d7cSsoda /*
27681858d7cSsoda * ICU doesn't understand the canonical link value.
27781858d7cSsoda * Just ignore this PIR entry.
27881858d7cSsoda */
27981858d7cSsoda #ifdef DIAGNOSTIC
280b63f6b50Ssoda printf("pciintr_link_alloc: "
281b63f6b50Ssoda "bus %d device %d link 0x%02x: "
28281858d7cSsoda "PIRQ 0x%02x invalid\n",
283b63f6b50Ssoda pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link,
284b63f6b50Ssoda clink);
28581858d7cSsoda #endif
28681858d7cSsoda return (NULL);
28781858d7cSsoda }
288b63f6b50Ssoda }
28981858d7cSsoda
290d47bcd29Schs l = malloc(sizeof(*l), M_DEVBUF, M_WAITOK | M_ZERO);
29181858d7cSsoda l->link = link;
2922026e5f7Sthorpej l->bitmap = pir->linkmap[pin].bitmap;
293b63f6b50Ssoda if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
29481858d7cSsoda l->clink = clink;
295ab4edb55Sfvdl l->irq = irq; /* maybe X86_PCI_INTERRUPT_LINE_NO_CONNECTION */
296b63f6b50Ssoda } else {
297b63f6b50Ssoda l->clink = link; /* only for PCIBIOSVERBOSE diagnostic */
298ab4edb55Sfvdl l->irq = X86_PCI_INTERRUPT_LINE_NO_CONNECTION;
299b63f6b50Ssoda }
3002026e5f7Sthorpej
3012026e5f7Sthorpej lstart = SIMPLEQ_FIRST(&pciintr_link_map_list);
3022026e5f7Sthorpej if (lstart == NULL || lstart->link < l->link)
3032026e5f7Sthorpej SIMPLEQ_INSERT_TAIL(&pciintr_link_map_list, l, list);
3042026e5f7Sthorpej else
3052026e5f7Sthorpej SIMPLEQ_INSERT_HEAD(&pciintr_link_map_list, l, list);
3062026e5f7Sthorpej
3072026e5f7Sthorpej return (l);
3082026e5f7Sthorpej }
3092026e5f7Sthorpej
3102026e5f7Sthorpej struct pcibios_intr_routing *
pciintr_pir_lookup(int bus,int device)311e8085086Skochi pciintr_pir_lookup(int bus, int device)
3122026e5f7Sthorpej {
3132026e5f7Sthorpej struct pcibios_intr_routing *pir;
3142026e5f7Sthorpej int entry;
3152026e5f7Sthorpej
3162026e5f7Sthorpej if (pcibios_pir_table == NULL)
3172026e5f7Sthorpej return (NULL);
3182026e5f7Sthorpej
3192026e5f7Sthorpej for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
3202026e5f7Sthorpej pir = &pcibios_pir_table[entry];
32181858d7cSsoda if (pir->bus == bus &&
32281858d7cSsoda PIR_DEVFUNC_DEVICE(pir->device) == device)
3232026e5f7Sthorpej return (pir);
3242026e5f7Sthorpej }
3252026e5f7Sthorpej
3262026e5f7Sthorpej return (NULL);
3272026e5f7Sthorpej }
3282026e5f7Sthorpej
32981858d7cSsoda static int
pciintr_bitmap_count_irq(int irq_bitmap,int * irqp)330e8085086Skochi pciintr_bitmap_count_irq(int irq_bitmap, int *irqp)
33181858d7cSsoda {
332ab4edb55Sfvdl int i, bit, count = 0, irq = X86_PCI_INTERRUPT_LINE_NO_CONNECTION;
33381858d7cSsoda
33481858d7cSsoda if (irq_bitmap != 0) {
33581858d7cSsoda for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
33681858d7cSsoda if (irq_bitmap & bit) {
33781858d7cSsoda irq = i;
33881858d7cSsoda count++;
33981858d7cSsoda }
34081858d7cSsoda }
34181858d7cSsoda }
34281858d7cSsoda *irqp = irq;
34381858d7cSsoda return (count);
34481858d7cSsoda }
34581858d7cSsoda
34681858d7cSsoda static int
pciintr_bitmap_find_lowest_irq(int irq_bitmap,int * irqp)347e8085086Skochi pciintr_bitmap_find_lowest_irq(int irq_bitmap, int *irqp)
34881858d7cSsoda {
34981858d7cSsoda int i, bit;
35081858d7cSsoda
35181858d7cSsoda if (irq_bitmap != 0) {
35281858d7cSsoda for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
35381858d7cSsoda if (irq_bitmap & bit) {
35481858d7cSsoda *irqp = i;
35581858d7cSsoda return (1); /* found */
35681858d7cSsoda }
35781858d7cSsoda }
35881858d7cSsoda }
35981858d7cSsoda return (0); /* not found */
36081858d7cSsoda }
36181858d7cSsoda
3622026e5f7Sthorpej int
pciintr_link_init(void)36335adf403Sperry pciintr_link_init(void)
3642026e5f7Sthorpej {
365b63f6b50Ssoda int entry, pin, link;
3662026e5f7Sthorpej struct pcibios_intr_routing *pir;
3672026e5f7Sthorpej struct pciintr_link_map *l;
3682026e5f7Sthorpej
3692026e5f7Sthorpej if (pcibios_pir_table == NULL) {
3702026e5f7Sthorpej /* No PIR table; can't do anything. */
3712026e5f7Sthorpej printf("pciintr_link_init: no PIR table\n");
3722026e5f7Sthorpej return (1);
3732026e5f7Sthorpej }
3742026e5f7Sthorpej
3752026e5f7Sthorpej SIMPLEQ_INIT(&pciintr_link_map_list);
3762026e5f7Sthorpej
3772026e5f7Sthorpej for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
3782026e5f7Sthorpej pir = &pcibios_pir_table[entry];
37981858d7cSsoda for (pin = 0; pin < PCI_INTERRUPT_PIN_MAX; pin++) {
3802026e5f7Sthorpej link = pir->linkmap[pin].link;
3812026e5f7Sthorpej if (link == 0) {
3822026e5f7Sthorpej /* No connection for this pin. */
3832026e5f7Sthorpej continue;
3842026e5f7Sthorpej }
3852026e5f7Sthorpej /*
3862026e5f7Sthorpej * Multiple devices may be wired to the same
3872026e5f7Sthorpej * interrupt; check to see if we've seen this
3882026e5f7Sthorpej * one already. If not, allocate a new link
3892026e5f7Sthorpej * map entry and stuff it in the map.
3902026e5f7Sthorpej */
39181858d7cSsoda l = pciintr_link_lookup(link);
39281858d7cSsoda if (l == NULL) {
3932026e5f7Sthorpej (void) pciintr_link_alloc(pir, pin);
39481858d7cSsoda } else if (pir->linkmap[pin].bitmap != l->bitmap) {
39581858d7cSsoda /*
39681858d7cSsoda * violates PCI IRQ Routing Table Specification
39781858d7cSsoda */
39881858d7cSsoda #ifdef DIAGNOSTIC
39981858d7cSsoda printf("pciintr_link_init: "
40081858d7cSsoda "bus %d device %d link 0x%02x: "
40181858d7cSsoda "bad irq bitmap 0x%04x, "
40281858d7cSsoda "should be 0x%04x\n",
40381858d7cSsoda pir->bus, PIR_DEVFUNC_DEVICE(pir->device),
40481858d7cSsoda link, pir->linkmap[pin].bitmap, l->bitmap);
40581858d7cSsoda #endif
40681858d7cSsoda /* safer value. */
40781858d7cSsoda l->bitmap &= pir->linkmap[pin].bitmap;
40881858d7cSsoda /* XXX - or, should ignore this entry? */
40981858d7cSsoda }
4102026e5f7Sthorpej }
4112026e5f7Sthorpej }
4122026e5f7Sthorpej
413b63f6b50Ssoda return (0);
4142026e5f7Sthorpej }
4152026e5f7Sthorpej
416b63f6b50Ssoda #ifdef PCIBIOS_INTR_GUESS
417b63f6b50Ssoda /*
418b63f6b50Ssoda * No compatible PCI ICU found.
419b63f6b50Ssoda * Hopes the BIOS already setup the ICU.
420b63f6b50Ssoda */
421b63f6b50Ssoda int
pciintr_guess_irq(void)42235adf403Sperry pciintr_guess_irq(void)
423b63f6b50Ssoda {
424b63f6b50Ssoda struct pciintr_link_map *l;
425b63f6b50Ssoda int irq, guessed = 0;
426b63f6b50Ssoda
427b63f6b50Ssoda /*
428b63f6b50Ssoda * Stage 1: If only one IRQ is available for the link, use it.
429b63f6b50Ssoda */
43006de4264Slukem SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
431ab4edb55Sfvdl if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
432b63f6b50Ssoda continue;
433b63f6b50Ssoda if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
434b63f6b50Ssoda l->irq = irq;
435b63f6b50Ssoda l->fixup_stage = 1;
436b63f6b50Ssoda #ifdef PCIINTR_DEBUG
437b63f6b50Ssoda printf("pciintr_guess_irq (stage 1): "
438b63f6b50Ssoda "guessing PIRQ 0x%02x to be IRQ %d\n",
439b63f6b50Ssoda l->clink, l->irq);
440b63f6b50Ssoda #endif
441b63f6b50Ssoda guessed = 1;
442b63f6b50Ssoda }
443b63f6b50Ssoda }
444b63f6b50Ssoda
445b63f6b50Ssoda return (guessed ? 0 : -1);
446b63f6b50Ssoda }
447b63f6b50Ssoda #endif /* PCIBIOS_INTR_GUESS */
448b63f6b50Ssoda
4492026e5f7Sthorpej int
pciintr_link_fixup(void)45035adf403Sperry pciintr_link_fixup(void)
4512026e5f7Sthorpej {
4522026e5f7Sthorpej struct pciintr_link_map *l;
45381858d7cSsoda int irq;
45468da4482Sperry uint16_t pciirq = 0;
4552026e5f7Sthorpej
4562026e5f7Sthorpej /*
4572026e5f7Sthorpej * First stage: Attempt to connect PIRQs which aren't
4582026e5f7Sthorpej * yet connected.
4592026e5f7Sthorpej */
46006de4264Slukem SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
461ab4edb55Sfvdl if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
4622026e5f7Sthorpej /*
46381858d7cSsoda * Interrupt is already connected. Don't do
46481858d7cSsoda * anything to it.
46581858d7cSsoda * In this case, l->fixup_stage == 0.
4662026e5f7Sthorpej */
46781858d7cSsoda pciirq |= 1 << l->irq;
4682026e5f7Sthorpej #ifdef PCIINTR_DEBUG
46981858d7cSsoda printf("pciintr_link_fixup: PIRQ 0x%02x already "
47081858d7cSsoda "connected to IRQ %d\n", l->clink, l->irq);
4712026e5f7Sthorpej #endif
4722026e5f7Sthorpej continue;
4732026e5f7Sthorpej }
4742026e5f7Sthorpej /*
47581858d7cSsoda * Interrupt isn't connected. Attempt to assign it to an IRQ.
4762026e5f7Sthorpej */
4772026e5f7Sthorpej #ifdef PCIINTR_DEBUG
47881858d7cSsoda printf("pciintr_link_fixup: PIRQ 0x%02x not connected",
4792026e5f7Sthorpej l->clink);
4802026e5f7Sthorpej #endif
4812026e5f7Sthorpej /*
48281858d7cSsoda * Just do the easy case now; we'll defer the harder ones
48381858d7cSsoda * to Stage 2.
4842026e5f7Sthorpej */
48581858d7cSsoda if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
48681858d7cSsoda l->irq = irq;
4872026e5f7Sthorpej l->fixup_stage = 1;
48881858d7cSsoda pciirq |= 1 << irq;
4892026e5f7Sthorpej #ifdef PCIINTR_DEBUG
4902026e5f7Sthorpej printf(", assigning IRQ %d", l->irq);
4912026e5f7Sthorpej #endif
4922026e5f7Sthorpej }
4932026e5f7Sthorpej #ifdef PCIINTR_DEBUG
4942026e5f7Sthorpej printf("\n");
4952026e5f7Sthorpej #endif
4962026e5f7Sthorpej }
4972026e5f7Sthorpej
4982026e5f7Sthorpej /*
4992026e5f7Sthorpej * Stage 2: Attempt to connect PIRQs which we didn't
5002026e5f7Sthorpej * connect in Stage 1.
5012026e5f7Sthorpej */
50206de4264Slukem SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
503ab4edb55Sfvdl if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
504e3c38828Such continue;
50581858d7cSsoda if (pciintr_bitmap_find_lowest_irq(l->bitmap & pciirq,
50681858d7cSsoda &l->irq)) {
5072026e5f7Sthorpej /*
508e3c38828Such * This IRQ is a valid PCI IRQ already
509e3c38828Such * connected to another PIRQ, and also an
510e3c38828Such * IRQ our PIRQ can use; connect it up!
5112026e5f7Sthorpej */
5122026e5f7Sthorpej l->fixup_stage = 2;
5132026e5f7Sthorpej #ifdef PCIINTR_DEBUG
51481858d7cSsoda printf("pciintr_link_fixup (stage 2): "
51581858d7cSsoda "assigning IRQ %d to PIRQ 0x%02x\n",
51681858d7cSsoda l->irq, l->clink);
5172026e5f7Sthorpej #endif
5182026e5f7Sthorpej }
5192026e5f7Sthorpej }
5202026e5f7Sthorpej
521e3c38828Such #ifdef PCIBIOS_IRQS_HINT
5222026e5f7Sthorpej /*
523e3c38828Such * Stage 3: The worst case. I need configuration hint that
524e3c38828Such * user supplied a mask for the PCI irqs
5252026e5f7Sthorpej */
52606de4264Slukem SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
527ab4edb55Sfvdl if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
528e3c38828Such continue;
52981858d7cSsoda if (pciintr_bitmap_find_lowest_irq(
53087b0dd6eSsoda l->bitmap & pcibios_irqs_hint, &l->irq)) {
531e3c38828Such l->fixup_stage = 3;
532e3c38828Such #ifdef PCIINTR_DEBUG
533e3c38828Such printf("pciintr_link_fixup (stage 3): "
53481858d7cSsoda "assigning IRQ %d to PIRQ 0x%02x\n",
535e3c38828Such l->irq, l->clink);
536e3c38828Such #endif
537e3c38828Such }
538e3c38828Such }
539e3c38828Such #endif /* PCIBIOS_IRQS_HINT */
5402026e5f7Sthorpej
5412026e5f7Sthorpej return (0);
5422026e5f7Sthorpej }
5432026e5f7Sthorpej
5442026e5f7Sthorpej int
pciintr_link_route(uint16_t * pciirq)54568da4482Sperry pciintr_link_route(uint16_t *pciirq)
5462026e5f7Sthorpej {
5472026e5f7Sthorpej struct pciintr_link_map *l;
5482026e5f7Sthorpej int rv = 0;
5492026e5f7Sthorpej
5502026e5f7Sthorpej *pciirq = 0;
5512026e5f7Sthorpej
55206de4264Slukem SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
55381858d7cSsoda if (l->fixup_stage == 0) {
554ab4edb55Sfvdl if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
55581858d7cSsoda /* Appropriate interrupt was not found. */
55681858d7cSsoda #ifdef DIAGNOSTIC
55781858d7cSsoda printf("pciintr_link_route: "
55881858d7cSsoda "PIRQ 0x%02x: no IRQ, try "
55981858d7cSsoda "\"options PCIBIOS_IRQS_HINT=0x%04x\"\n",
56081858d7cSsoda l->clink,
56181858d7cSsoda /* suggest irq 9/10/11, if possible */
56281858d7cSsoda (l->bitmap & 0x0e00) ? (l->bitmap & 0x0e00)
56381858d7cSsoda : l->bitmap);
56481858d7cSsoda #endif
56581858d7cSsoda } else {
56681858d7cSsoda /* BIOS setting has no problem */
56781858d7cSsoda #ifdef PCIINTR_DEBUG
56881858d7cSsoda printf("pciintr_link_route: "
56981858d7cSsoda "route of PIRQ 0x%02x -> "
57081858d7cSsoda "IRQ %d preserved BIOS setting\n",
57181858d7cSsoda l->clink, l->irq);
57281858d7cSsoda #endif
57381858d7cSsoda *pciirq |= (1 << l->irq);
57481858d7cSsoda }
57581858d7cSsoda continue; /* nothing to do. */
57681858d7cSsoda }
57781858d7cSsoda
5782026e5f7Sthorpej if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
5792026e5f7Sthorpej l->clink, l->irq) != 0 ||
58081858d7cSsoda pciintr_icu_set_trigger(pciintr_icu_tag,
58181858d7cSsoda pciintr_icu_handle,
5822026e5f7Sthorpej l->irq, IST_LEVEL) != 0) {
58381858d7cSsoda printf("pciintr_link_route: route of PIRQ 0x%02x -> "
58481858d7cSsoda "IRQ %d failed\n", l->clink, l->irq);
5852026e5f7Sthorpej rv = 1;
5862026e5f7Sthorpej } else {
5872026e5f7Sthorpej /*
588*72e44f84Sandvar * Successfully routed interrupt. Mark this as
5892026e5f7Sthorpej * a PCI interrupt.
5902026e5f7Sthorpej */
5912026e5f7Sthorpej *pciirq |= (1 << l->irq);
5922026e5f7Sthorpej }
5932026e5f7Sthorpej }
5942026e5f7Sthorpej
5952026e5f7Sthorpej return (rv);
5962026e5f7Sthorpej }
5972026e5f7Sthorpej
5982026e5f7Sthorpej int
pciintr_irq_release(uint16_t * pciirq)59968da4482Sperry pciintr_irq_release(uint16_t *pciirq)
6002026e5f7Sthorpej {
60181858d7cSsoda int i, bit;
60268da4482Sperry uint16_t bios_pciirq;
603f117f546Schristos int reg;
6042026e5f7Sthorpej
605f117f546Schristos #ifdef PCIINTR_DEBUG
606f117f546Schristos printf("pciintr_irq_release: fixup pciirq level/edge map 0x%04x\n",
607f117f546Schristos *pciirq);
608f117f546Schristos #endif
609f117f546Schristos
610f117f546Schristos /* Get bios level/edge setting. */
611f117f546Schristos bios_pciirq = 0;
612f117f546Schristos for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
613f117f546Schristos (void)pciintr_icu_get_trigger(pciintr_icu_tag,
614f117f546Schristos pciintr_icu_handle, i, ®);
615f117f546Schristos if (reg == IST_LEVEL)
616f117f546Schristos bios_pciirq |= bit;
617f117f546Schristos }
618f117f546Schristos
619f117f546Schristos #ifdef PCIINTR_DEBUG
620f117f546Schristos printf("pciintr_irq_release: bios pciirq level/edge map 0x%04x\n",
621f117f546Schristos bios_pciirq);
622f117f546Schristos #endif /* PCIINTR_DEBUG */
623f117f546Schristos
624f117f546Schristos /* fixup final level/edge setting. */
625f117f546Schristos *pciirq |= bios_pciirq;
62681858d7cSsoda for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
62781858d7cSsoda if ((*pciirq & bit) == 0)
628f117f546Schristos reg = IST_EDGE;
629f117f546Schristos else
630f117f546Schristos reg = IST_LEVEL;
6312026e5f7Sthorpej (void) pciintr_icu_set_trigger(pciintr_icu_tag,
632f117f546Schristos pciintr_icu_handle, i, reg);
633f117f546Schristos
6342026e5f7Sthorpej }
6352026e5f7Sthorpej
636f117f546Schristos #ifdef PCIINTR_DEBUG
637f117f546Schristos printf("pciintr_irq_release: final pciirq level/edge map 0x%04x\n",
638f117f546Schristos *pciirq);
639f117f546Schristos #endif /* PCIINTR_DEBUG */
640f117f546Schristos
6412026e5f7Sthorpej return (0);
6422026e5f7Sthorpej }
6432026e5f7Sthorpej
6442026e5f7Sthorpej int
pciintr_header_fixup(pci_chipset_tag_t pc)645e8085086Skochi pciintr_header_fixup(pci_chipset_tag_t pc)
6462026e5f7Sthorpej {
64781858d7cSsoda PCIBIOS_PRINTV(("------------------------------------------\n"));
64881858d7cSsoda PCIBIOS_PRINTV((" device vendor product pin PIRQ IRQ stage\n"));
64981858d7cSsoda PCIBIOS_PRINTV(("------------------------------------------\n"));
65007ba5352Smcr pci_device_foreach(pc, pcibios_max_bus, pciintr_do_header_fixup, NULL);
65181858d7cSsoda PCIBIOS_PRINTV(("------------------------------------------\n"));
6522026e5f7Sthorpej
653e3c38828Such return (0);
654e3c38828Such }
655e3c38828Such
656e3c38828Such void
pciintr_do_header_fixup(pci_chipset_tag_t pc,pcitag_t tag,void * context)6574d595fd7Schristos pciintr_do_header_fixup(pci_chipset_tag_t pc, pcitag_t tag,
658168cd830Schristos void *context)
659e3c38828Such {
660e3c38828Such struct pcibios_intr_routing *pir;
661e3c38828Such struct pciintr_link_map *l;
662e3c38828Such int pin, irq, link;
663e3c38828Such int bus, device, function;
664e3c38828Such pcireg_t intr, id;
665e3c38828Such
666e3c38828Such pci_decompose_tag(pc, tag, &bus, &device, &function);
6672026e5f7Sthorpej id = pci_conf_read(pc, tag, PCI_ID_REG);
6682026e5f7Sthorpej
669e3c38828Such intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
6702026e5f7Sthorpej pin = PCI_INTERRUPT_PIN(intr);
6712026e5f7Sthorpej irq = PCI_INTERRUPT_LINE(intr);
6722026e5f7Sthorpej
67307ba5352Smcr #if 0
6742026e5f7Sthorpej if (pin == 0) {
6752026e5f7Sthorpej /*
6762026e5f7Sthorpej * No interrupt used.
6772026e5f7Sthorpej */
678e3c38828Such return;
6792026e5f7Sthorpej }
68007ba5352Smcr #endif
6812026e5f7Sthorpej
6822026e5f7Sthorpej pir = pciintr_pir_lookup(bus, device);
683e3c38828Such if (pir == NULL || (link = pir->linkmap[pin - 1].link) == 0) {
6842026e5f7Sthorpej /*
6852026e5f7Sthorpej * Interrupt not connected; no
6862026e5f7Sthorpej * need to change.
6872026e5f7Sthorpej */
688e3c38828Such return;
6892026e5f7Sthorpej }
6902026e5f7Sthorpej
69181858d7cSsoda l = pciintr_link_lookup(link);
6922026e5f7Sthorpej if (l == NULL) {
69381858d7cSsoda #ifdef PCIINTR_DEBUG
6942026e5f7Sthorpej /*
69581858d7cSsoda * No link map entry.
69681858d7cSsoda * Probably pciintr_icu_getclink() or pciintr_icu_get_intr()
69781858d7cSsoda * was failed.
6982026e5f7Sthorpej */
699e3c38828Such printf("pciintr_header_fixup: no entry for link 0x%02x "
700e3c38828Such "(%d:%d:%d:%c)\n", link, bus, device, function,
7012026e5f7Sthorpej '@' + pin);
70281858d7cSsoda #endif
703e3c38828Such return;
7042026e5f7Sthorpej }
7052026e5f7Sthorpej
70681858d7cSsoda #ifdef PCIBIOSVERBOSE
70781858d7cSsoda if (pcibiosverbose) {
708b5831015Suwe PCIBIOS_PRINTV(("%03d:%02d:%d 0x%04x 0x%04x %c 0x%02x",
70981858d7cSsoda bus, device, function, PCI_VENDOR(id), PCI_PRODUCT(id),
710b5831015Suwe '@' + pin, l->clink));
711ab4edb55Sfvdl if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
712b5831015Suwe PCIBIOS_PRINTV((" -"));
71381858d7cSsoda else
714b5831015Suwe PCIBIOS_PRINTV((" %3d", l->irq));
715b5831015Suwe PCIBIOS_PRINTV((" %d ", l->fixup_stage));
71681858d7cSsoda }
717db44b201Sapb #else
718db44b201Sapb __USE(id);
71981858d7cSsoda #endif
72081858d7cSsoda
7212026e5f7Sthorpej /*
722e3c38828Such * IRQs 14 and 15 are reserved for PCI IDE interrupts; don't muck
7232026e5f7Sthorpej * with them.
7242026e5f7Sthorpej */
72581858d7cSsoda if (irq == 14 || irq == 15) {
72681858d7cSsoda PCIBIOS_PRINTV((" WARNING: ignored\n"));
727e3c38828Such return;
72881858d7cSsoda }
7292026e5f7Sthorpej
730ab4edb55Sfvdl if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
73181858d7cSsoda /* Appropriate interrupt was not found. */
732b63f6b50Ssoda if (pciintr_icu_tag == NULL &&
733ab4edb55Sfvdl irq != 0 && irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
734b63f6b50Ssoda /*
735b63f6b50Ssoda * Do not print warning,
736b63f6b50Ssoda * if no compatible PCI ICU found,
737b63f6b50Ssoda * but the irq is already assigned by BIOS.
738b63f6b50Ssoda */
739b63f6b50Ssoda PCIBIOS_PRINTV(("\n"));
740b63f6b50Ssoda } else {
74181858d7cSsoda PCIBIOS_PRINTV((" WARNING: missing IRQ\n"));
742b63f6b50Ssoda }
74381858d7cSsoda return;
74481858d7cSsoda }
74581858d7cSsoda
74681858d7cSsoda if (l->irq == irq) {
74781858d7cSsoda /* don't have to reconfigure */
74881858d7cSsoda PCIBIOS_PRINTV((" already assigned\n"));
74981858d7cSsoda return;
75081858d7cSsoda }
75181858d7cSsoda
752ab4edb55Sfvdl if (irq == 0 || irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
75381858d7cSsoda PCIBIOS_PRINTV((" fixed up\n"));
75481858d7cSsoda } else {
75581858d7cSsoda /* routed by BIOS, but inconsistent */
7560919f4d2Ssekiya #ifdef PCI_INTR_FIXUP_FORCE
75781858d7cSsoda /* believe PCI IRQ Routing table */
758a86a9c61Ssoda PCIBIOS_PRINTV((" WARNING: overriding irq %d\n", irq));
75981858d7cSsoda #else
76081858d7cSsoda /* believe PCI Interrupt Configuration Register (default) */
761a86a9c61Ssoda PCIBIOS_PRINTV((" WARNING: preserving irq %d\n", irq));
76281858d7cSsoda return;
7632026e5f7Sthorpej #endif
76481858d7cSsoda }
7652026e5f7Sthorpej
766e3c38828Such intr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
7672026e5f7Sthorpej intr |= (l->irq << PCI_INTERRUPT_LINE_SHIFT);
768e3c38828Such pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
7692026e5f7Sthorpej }
7702026e5f7Sthorpej
7712026e5f7Sthorpej int
pci_intr_fixup(pci_chipset_tag_t pc,bus_space_tag_t iot,uint16_t * pciirq)77268da4482Sperry pci_intr_fixup(pci_chipset_tag_t pc, bus_space_tag_t iot, uint16_t *pciirq)
7732026e5f7Sthorpej {
7742026e5f7Sthorpej const struct pciintr_icu_table *piit = NULL;
7752026e5f7Sthorpej pcitag_t icutag;
7762026e5f7Sthorpej pcireg_t icuid;
777f86d875aSjmcneill int error = 0;
7782026e5f7Sthorpej
7792026e5f7Sthorpej /*
7802026e5f7Sthorpej * Attempt to initialize our PCI interrupt router. If
7812026e5f7Sthorpej * the PIR Table is present in ROM, use the location
7822026e5f7Sthorpej * specified by the PIR Table, and use the compat ID,
7832026e5f7Sthorpej * if present. Otherwise, we have to look for the router
7842026e5f7Sthorpej * ourselves (the PCI-ISA bridge).
785aa09aa91Skanaoka *
786aa09aa91Skanaoka * A number of buggy BIOS implementations leave the router
787aa09aa91Skanaoka * entry as 000:00:0, which is typically not the correct
788aa09aa91Skanaoka * device/function. If the router device address is set to
789aa09aa91Skanaoka * this value, and the compatible router entry is undefined
790aa09aa91Skanaoka * (zero is the correct value to indicate undefined), then we
791aa09aa91Skanaoka * work on the basis it is most likely an error, and search
792aa09aa91Skanaoka * the entire device-space of bus 0 (but obviously starting
793aa09aa91Skanaoka * with 000:00:0, in case that really is the right one).
7942026e5f7Sthorpej */
795aa09aa91Skanaoka if (pcibios_pir_header.signature != 0 &&
796aa09aa91Skanaoka (pcibios_pir_header.router_bus != 0 ||
797aa09aa91Skanaoka PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc) != 0 ||
798aa09aa91Skanaoka PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc) != 0 ||
799aa09aa91Skanaoka pcibios_pir_header.compat_router != 0)) {
8002026e5f7Sthorpej icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
80181858d7cSsoda PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
80281858d7cSsoda PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
8032026e5f7Sthorpej icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
804ee2683e7Skochi if ((piit = pciintr_icu_lookup(icuid)) == NULL) {
805ee2683e7Skochi /*
806ee2683e7Skochi * if we fail to look up an ICU at given
807ee2683e7Skochi * PCI address, try compat ID next.
808ee2683e7Skochi */
809ee2683e7Skochi icuid = pcibios_pir_header.compat_router;
8102026e5f7Sthorpej piit = pciintr_icu_lookup(icuid);
811ee2683e7Skochi }
8122026e5f7Sthorpej } else {
8132026e5f7Sthorpej int device, maxdevs = pci_bus_maxdevs(pc, 0);
8142026e5f7Sthorpej
8152026e5f7Sthorpej /*
8162026e5f7Sthorpej * Search configuration space for a known interrupt
8172026e5f7Sthorpej * router.
8182026e5f7Sthorpej */
8192026e5f7Sthorpej for (device = 0; device < maxdevs; device++) {
820aa09aa91Skanaoka const struct pci_quirkdata *qd;
821aa09aa91Skanaoka int function, nfuncs;
822aa09aa91Skanaoka pcireg_t bhlcr;
823aa09aa91Skanaoka
8242026e5f7Sthorpej icutag = pci_make_tag(pc, 0, device, 0);
8252026e5f7Sthorpej icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
8262026e5f7Sthorpej
8272026e5f7Sthorpej /* Invalid vendor ID value? */
8282026e5f7Sthorpej if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
8292026e5f7Sthorpej continue;
8302026e5f7Sthorpej /* XXX Not invalid, but we've done this ~forever. */
8312026e5f7Sthorpej if (PCI_VENDOR(icuid) == 0)
8322026e5f7Sthorpej continue;
8332026e5f7Sthorpej
834aa09aa91Skanaoka qd = pci_lookup_quirkdata(PCI_VENDOR(icuid),
835aa09aa91Skanaoka PCI_PRODUCT(icuid));
836aa09aa91Skanaoka
837aa09aa91Skanaoka bhlcr = pci_conf_read(pc, icutag, PCI_BHLC_REG);
838aa09aa91Skanaoka if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
839aa09aa91Skanaoka (qd != NULL &&
840aa09aa91Skanaoka (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
841aa09aa91Skanaoka nfuncs = 8;
842aa09aa91Skanaoka else
843aa09aa91Skanaoka nfuncs = 1;
844aa09aa91Skanaoka
845aa09aa91Skanaoka for (function = 0; function < nfuncs; function++) {
846aa09aa91Skanaoka icutag = pci_make_tag(pc, 0, device, function);
847aa09aa91Skanaoka icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
848aa09aa91Skanaoka
849aa09aa91Skanaoka /* Invalid vendor ID value? */
850aa09aa91Skanaoka if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
851aa09aa91Skanaoka continue;
852aa09aa91Skanaoka /* Not invalid, but we've done this ~forever */
853aa09aa91Skanaoka if (PCI_VENDOR(icuid) == 0)
854aa09aa91Skanaoka continue;
855aa09aa91Skanaoka
8562026e5f7Sthorpej piit = pciintr_icu_lookup(icuid);
8572026e5f7Sthorpej if (piit != NULL)
858aa09aa91Skanaoka goto found;
8592026e5f7Sthorpej }
8602026e5f7Sthorpej }
8612026e5f7Sthorpej
862aa09aa91Skanaoka /*
863aa09aa91Skanaoka * Invalidate the ICU ID. If we failed to find the
864aa09aa91Skanaoka * interrupt router (piit == NULL) we don't want to
865aa09aa91Skanaoka * display a spurious device address below containing
866aa09aa91Skanaoka * the product information of the last device we
867aa09aa91Skanaoka * looked at.
868aa09aa91Skanaoka */
869aa09aa91Skanaoka icuid = 0;
870ef38b7e8Smrg found:;
871aa09aa91Skanaoka }
872aa09aa91Skanaoka
8732026e5f7Sthorpej if (piit == NULL) {
874b63f6b50Ssoda printf("pci_intr_fixup: no compatible PCI ICU found");
875b63f6b50Ssoda if (pcibios_pir_header.signature != 0 && icuid != 0)
876b63f6b50Ssoda printf(": ICU vendor 0x%04x product 0x%04x",
877b63f6b50Ssoda PCI_VENDOR(icuid), PCI_PRODUCT(icuid));
878b63f6b50Ssoda printf("\n");
879b63f6b50Ssoda #ifdef PCIBIOS_INTR_GUESS
880b63f6b50Ssoda if (pciintr_link_init())
8812026e5f7Sthorpej return (-1); /* non-fatal */
882b63f6b50Ssoda if (pciintr_guess_irq())
883b63f6b50Ssoda return (-1); /* non-fatal */
884b63f6b50Ssoda if (pciintr_header_fixup(pc))
885b63f6b50Ssoda return (1); /* fatal */
886b63f6b50Ssoda return (0); /* success! */
887b63f6b50Ssoda #else
888b63f6b50Ssoda return (-1); /* non-fatal */
889b63f6b50Ssoda #endif
8902026e5f7Sthorpej }
8912026e5f7Sthorpej
8922026e5f7Sthorpej /*
8932026e5f7Sthorpej * Initialize the PCI ICU.
8942026e5f7Sthorpej */
895f86d875aSjmcneill if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
896f86d875aSjmcneill &pciintr_icu_handle) != 0)
8972026e5f7Sthorpej return (-1); /* non-fatal */
8982026e5f7Sthorpej
8992026e5f7Sthorpej /*
9002026e5f7Sthorpej * Initialize the PCI interrupt link map.
9012026e5f7Sthorpej */
902f86d875aSjmcneill if (pciintr_link_init()) {
903f86d875aSjmcneill error = -1; /* non-fatal */
904f86d875aSjmcneill goto cleanup;
905f86d875aSjmcneill }
9062026e5f7Sthorpej
9072026e5f7Sthorpej /*
9082026e5f7Sthorpej * Fix up the link->IRQ mappings.
9092026e5f7Sthorpej */
910f86d875aSjmcneill if (pciintr_link_fixup() != 0) {
911f86d875aSjmcneill error = -1; /* non-fatal */
912f86d875aSjmcneill goto cleanup;
913f86d875aSjmcneill }
9142026e5f7Sthorpej
9152026e5f7Sthorpej /*
9162026e5f7Sthorpej * Now actually program the PCI ICU with the new
9172026e5f7Sthorpej * routing information.
9182026e5f7Sthorpej */
919f86d875aSjmcneill if (pciintr_link_route(pciirq) != 0) {
920f86d875aSjmcneill error = 1; /* fatal */
921f86d875aSjmcneill goto cleanup;
922f86d875aSjmcneill }
9232026e5f7Sthorpej
9242026e5f7Sthorpej /*
9252026e5f7Sthorpej * Now that we've routed all of the PIRQs, rewrite the PCI
9262026e5f7Sthorpej * configuration headers to reflect the new mapping.
9272026e5f7Sthorpej */
928f86d875aSjmcneill if (pciintr_header_fixup(pc) != 0) {
929f86d875aSjmcneill error = 1; /* fatal */
930f86d875aSjmcneill goto cleanup;
931f86d875aSjmcneill }
9322026e5f7Sthorpej
9332026e5f7Sthorpej /*
9342026e5f7Sthorpej * Free any unused PCI IRQs for ISA devices.
9352026e5f7Sthorpej */
936f86d875aSjmcneill if (pciintr_irq_release(pciirq) != 0) {
937f86d875aSjmcneill error = -1; /* non-fatal */
938f86d875aSjmcneill goto cleanup;
939f86d875aSjmcneill }
9402026e5f7Sthorpej
9412026e5f7Sthorpej /*
9422026e5f7Sthorpej * All done!
9432026e5f7Sthorpej */
944f86d875aSjmcneill cleanup:
945f86d875aSjmcneill if (piit->piit_uninit != NULL)
946f86d875aSjmcneill (*piit->piit_uninit)(pciintr_icu_handle);
947f86d875aSjmcneill return (error);
9482026e5f7Sthorpej }
949