xref: /openbsd/sys/arch/i386/pci/pci_intr_fixup.c (revision f4e70637)
1*f4e70637Sjsg /*	$OpenBSD: pci_intr_fixup.c,v 1.64 2023/01/30 10:49:05 jsg Exp $	*/
2b25264e3Smickey /*	$NetBSD: pci_intr_fixup.c,v 1.10 2000/08/10 21:18:27 soda Exp $	*/
36661564bSmickey 
464f0e01bSmickey /*
564f0e01bSmickey  * Copyright (c) 2001 Michael Shalayeff
664f0e01bSmickey  * All rights reserved.
764f0e01bSmickey  *
864f0e01bSmickey  * Redistribution and use in source and binary forms, with or without
964f0e01bSmickey  * modification, are permitted provided that the following conditions
1064f0e01bSmickey  * are met:
1164f0e01bSmickey  * 1. Redistributions of source code must retain the above copyright
1264f0e01bSmickey  *    notice, this list of conditions and the following disclaimer.
1364f0e01bSmickey  * 2. Redistributions in binary form must reproduce the above copyright
1464f0e01bSmickey  *    notice, this list of conditions and the following disclaimer in the
1564f0e01bSmickey  *    documentation and/or other materials provided with the distribution.
1664f0e01bSmickey  *
1764f0e01bSmickey  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1864f0e01bSmickey  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1964f0e01bSmickey  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2064f0e01bSmickey  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
2164f0e01bSmickey  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2264f0e01bSmickey  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2364f0e01bSmickey  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2464f0e01bSmickey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2564f0e01bSmickey  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2664f0e01bSmickey  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2764f0e01bSmickey  * THE POSSIBILITY OF SUCH DAMAGE.
2864f0e01bSmickey  */
296661564bSmickey /*-
306661564bSmickey  * Copyright (c) 1999 The NetBSD Foundation, Inc.
316661564bSmickey  * All rights reserved.
326661564bSmickey  *
336661564bSmickey  * This code is derived from software contributed to The NetBSD Foundation
346661564bSmickey  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
356661564bSmickey  * NASA Ames Research Center.
366661564bSmickey  *
376661564bSmickey  * Redistribution and use in source and binary forms, with or without
386661564bSmickey  * modification, are permitted provided that the following conditions
396661564bSmickey  * are met:
406661564bSmickey  * 1. Redistributions of source code must retain the above copyright
416661564bSmickey  *    notice, this list of conditions and the following disclaimer.
426661564bSmickey  * 2. Redistributions in binary form must reproduce the above copyright
436661564bSmickey  *    notice, this list of conditions and the following disclaimer in the
446661564bSmickey  *    documentation and/or other materials provided with the distribution.
456661564bSmickey  *
466661564bSmickey  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
476661564bSmickey  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
486661564bSmickey  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
496661564bSmickey  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
506661564bSmickey  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
516661564bSmickey  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
526661564bSmickey  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
536661564bSmickey  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
546661564bSmickey  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
556661564bSmickey  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
566661564bSmickey  * POSSIBILITY OF SUCH DAMAGE.
576661564bSmickey  */
586661564bSmickey /*
596661564bSmickey  * Copyright (c) 1999, by UCHIYAMA Yasushi
606661564bSmickey  * All rights reserved.
616661564bSmickey  *
626661564bSmickey  * Redistribution and use in source and binary forms, with or without
636661564bSmickey  * modification, are permitted provided that the following conditions
646661564bSmickey  * are met:
656661564bSmickey  * 1. Redistributions of source code must retain the above copyright
666661564bSmickey  *    notice, this list of conditions and the following disclaimer.
676661564bSmickey  * 2. The name of the developer may NOT be used to endorse or promote products
686661564bSmickey  *    derived from this software without specific prior written permission.
696661564bSmickey  *
706661564bSmickey  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
716661564bSmickey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
726661564bSmickey  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
736661564bSmickey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
746661564bSmickey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
756661564bSmickey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
766661564bSmickey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
776661564bSmickey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
786661564bSmickey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
796661564bSmickey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
806661564bSmickey  * SUCH DAMAGE.
816661564bSmickey  */
826661564bSmickey 
836661564bSmickey /*
846661564bSmickey  * PCI Interrupt Router support.
856661564bSmickey  */
866661564bSmickey 
876661564bSmickey #include <sys/param.h>
886661564bSmickey #include <sys/systm.h>
896661564bSmickey #include <sys/malloc.h>
906661564bSmickey #include <sys/queue.h>
916661564bSmickey #include <sys/device.h>
926661564bSmickey 
936661564bSmickey #include <machine/bus.h>
946661564bSmickey #include <machine/intr.h>
95a25c7806Smickey #include <machine/i82093var.h>
966661564bSmickey 
976661564bSmickey #include <dev/pci/pcireg.h>
986661564bSmickey #include <dev/pci/pcivar.h>
996661564bSmickey #include <dev/pci/pcidevs.h>
1006661564bSmickey 
101e2331c6cSderaadt #include <i386/pci/pcibiosvar.h>
1026661564bSmickey 
1036661564bSmickey struct pciintr_link_map {
10464f0e01bSmickey 	int link, clink, irq, fixup_stage;
1056661564bSmickey 	u_int16_t bitmap;
1066661564bSmickey 	SIMPLEQ_ENTRY(pciintr_link_map) list;
1076661564bSmickey };
1086661564bSmickey 
109b25264e3Smickey pciintr_icu_tag_t pciintr_icu_tag = NULL;
1106661564bSmickey pciintr_icu_handle_t pciintr_icu_handle;
1116661564bSmickey 
1120edf2c54Smickey #ifdef PCIBIOS_IRQS_HINT
1130edf2c54Smickey int pcibios_irqs_hint = PCIBIOS_IRQS_HINT;
1140edf2c54Smickey #endif
1150edf2c54Smickey 
116c4071fd1Smillert struct pciintr_link_map *pciintr_link_lookup(int);
117c4071fd1Smillert struct pcibios_intr_routing *pciintr_pir_lookup(int, int);
118c4071fd1Smillert int	pciintr_bitmap_count_irq(int, int *);
1196661564bSmickey 
1206661564bSmickey SIMPLEQ_HEAD(, pciintr_link_map) pciintr_link_map_list;
1216661564bSmickey 
1226661564bSmickey const struct pciintr_icu_table {
1236661564bSmickey 	pci_vendor_id_t	piit_vendor;
1246661564bSmickey 	pci_product_id_t piit_product;
125c4071fd1Smillert 	int (*piit_init)(pci_chipset_tag_t,
1266661564bSmickey 		bus_space_tag_t, pcitag_t, pciintr_icu_tag_t *,
127c4071fd1Smillert 		pciintr_icu_handle_t *);
1286661564bSmickey } pciintr_icu_table[] = {
12923f11131Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_6300ESB_LPC,
13023f11131Sbrad 	  piix_init },
131b566768eSbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_6321ESB_LPC,
132b566768eSbrad 	  piix_init },
1336661564bSmickey 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371MX,
1346661564bSmickey 	  piix_init },
1356661564bSmickey 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371AB_ISA,
1366661564bSmickey 	  piix_init },
1376661564bSmickey 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371FB_ISA,
1386661564bSmickey 	  piix_init },
1396661564bSmickey 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371SB_ISA,
1406661564bSmickey 	  piix_init },
14123f11131Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82440MX_ISA,
14223f11131Sbrad 	  piix_init },
143ea8e013cSho 	{ PCI_VENDOR_INTEL,     PCI_PRODUCT_INTEL_82801AA_LPC,
144ea8e013cSho 	  piix_init },
14523f11131Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801AB_LPC,
14623f11131Sbrad 	  piix_init },
14753003c45Smickey 	{ PCI_VENDOR_INTEL,     PCI_PRODUCT_INTEL_82801BA_LPC,
14853003c45Smickey 	  piix_init },
14953003c45Smickey 	{ PCI_VENDOR_INTEL,     PCI_PRODUCT_INTEL_82801BAM_LPC,
15053003c45Smickey 	  piix_init },
151b9a626b7Smickey 	{ PCI_VENDOR_INTEL,     PCI_PRODUCT_INTEL_82801CA_LPC,
152b9a626b7Smickey 	  piix_init },
1536d805c04Smickey 	{ PCI_VENDOR_INTEL,     PCI_PRODUCT_INTEL_82801CAM_LPC,
1546d805c04Smickey 	  piix_init },
1556d805c04Smickey 	{ PCI_VENDOR_INTEL,     PCI_PRODUCT_INTEL_82801DB_LPC,
1566d805c04Smickey 	  piix_init },
157aa7d4239Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801DBM_LPC,
158aa7d4239Sbrad 	  piix_init },
15923f11131Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801E_LPC,
16023f11131Sbrad 	  piix_init },
1615b6ecbecSmickey 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801EB_LPC,
1625b6ecbecSmickey 	  piix_init },
1636eced139Sbeck 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801FB_LPC,
1646eced139Sbeck 	  piix_init },
16523f11131Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801FBM_LPC,
16623f11131Sbrad 	  piix_init },
167d984c6bdSmickey 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801GB_LPC,
168d984c6bdSmickey 	  piix_init },
16923f11131Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801GBM_LPC,
17023f11131Sbrad 	  piix_init },
17123f11131Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801GH_LPC,
172aa5f4fe6Sbrad 	  piix_init },
173b566768eSbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801GHM_LPC,
174b566768eSbrad 	  piix_init },
17509e7fbc8Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801IB_LPC,
17609e7fbc8Sbrad 	  piix_init },
17709e7fbc8Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801IH_LPC,
17809e7fbc8Sbrad 	  piix_init },
17909e7fbc8Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801IO_LPC,
18009e7fbc8Sbrad 	  piix_init },
18109e7fbc8Sbrad 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801IR_LPC,
18209e7fbc8Sbrad 	  piix_init },
1836661564bSmickey 
1846661564bSmickey 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C558,
1856661564bSmickey 	  opti82c558_init },
1866661564bSmickey 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C700,
1876661564bSmickey 	  opti82c700_init },
1886661564bSmickey 
1898a1eb7fcSbrad 	{ PCI_VENDOR_RCC,	PCI_PRODUCT_RCC_OSB4,
190bd25a23dSmickey 	  osb4_init },
191bd25a23dSmickey 	{ PCI_VENDOR_RCC,	PCI_PRODUCT_RCC_CSB5,
192bd25a23dSmickey 	  osb4_init },
193bd25a23dSmickey 
1946661564bSmickey 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C586_ISA,
1956661564bSmickey 	  via82c586_init, },
196139f38fdSbrad 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C596A,
197139f38fdSbrad 	  via82c586_init, },
19864f0e01bSmickey 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C686A_ISA,
19992598216Smickey 	  via82c586_init },
20092598216Smickey 
20192598216Smickey 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8231_ISA,
20292598216Smickey 	  via8231_init },
203139f38fdSbrad 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8233_ISA,
204139f38fdSbrad 	  via8231_init },
205275f08b3Skettenis 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8233A_ISA,
20624996536Smickey 	  via8231_init },
20724996536Smickey 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8235_ISA,
20824996536Smickey 	  via8231_init },
209bc465bf0Shenning 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8237_ISA,
210bc465bf0Shenning 	  via8231_init },
2113bd326a1Sgrange 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8237A_ISA,
2123bd326a1Sgrange 	  via8231_init },
21314516421Sjcs 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8237S_ISA,
21414516421Sjcs 	  via8231_init },
2156661564bSmickey 
2166661564bSmickey 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_85C503,
2176661564bSmickey 	  sis85c503_init },
218139f38fdSbrad 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_962,
219139f38fdSbrad 	  sis85c503_init },
2200d58a2caSjsg 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_963,
2210d58a2caSjsg 	  sis85c503_init },
2226661564bSmickey 
223cc48d299Smickey 	{ PCI_VENDOR_AMD,	PCI_PRODUCT_AMD_PBC756_PMC,
22467218ad0Smickey 	  amd756_init },
2251b2c3d0dSbrad 	{ PCI_VENDOR_AMD,	PCI_PRODUCT_AMD_766_PMC,
2261b2c3d0dSbrad 	  amd756_init },
2271b2c3d0dSbrad 	{ PCI_VENDOR_AMD,	PCI_PRODUCT_AMD_PBC768_PMC,
22864c49bbaSmickey 	  amd756_init },
22967218ad0Smickey 
2302766fe41Smillert 	{ PCI_VENDOR_ALI,	PCI_PRODUCT_ALI_M1533,
2312766fe41Smillert 	  ali1543_init },
2322766fe41Smillert 
2337853e030Smickey 	{ PCI_VENDOR_ALI,	PCI_PRODUCT_ALI_M1543,
2347853e030Smickey 	  ali1543_init },
2357853e030Smickey 
2366661564bSmickey 	{ 0,			0,
2376661564bSmickey 	  NULL },
2386661564bSmickey };
2396661564bSmickey 
240c4071fd1Smillert const struct pciintr_icu_table *pciintr_icu_lookup(pcireg_t);
2416661564bSmickey 
2426661564bSmickey const struct pciintr_icu_table *
pciintr_icu_lookup(pcireg_t id)2430bca52fcSjsg pciintr_icu_lookup(pcireg_t id)
2446661564bSmickey {
2456661564bSmickey 	const struct pciintr_icu_table *piit;
2466661564bSmickey 
24764f0e01bSmickey 	for (piit = pciintr_icu_table; piit->piit_init != NULL; piit++)
2486661564bSmickey 		if (PCI_VENDOR(id) == piit->piit_vendor &&
2496661564bSmickey 		    PCI_PRODUCT(id) == piit->piit_product)
2506661564bSmickey 			return (piit);
2516661564bSmickey 
2526661564bSmickey 	return (NULL);
2536661564bSmickey }
2546661564bSmickey 
2556661564bSmickey struct pciintr_link_map *
pciintr_link_lookup(int link)25608f75f72Sjsg pciintr_link_lookup(int link)
2576661564bSmickey {
2586661564bSmickey 	struct pciintr_link_map *l;
2596661564bSmickey 
2606661564bSmickey 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
26164f0e01bSmickey 	     l = SIMPLEQ_NEXT(l, list))
2626661564bSmickey 		if (l->link == link)
2636661564bSmickey 			return (l);
2646661564bSmickey 
2656661564bSmickey 	return (NULL);
2666661564bSmickey }
2676661564bSmickey 
26864f0e01bSmickey static __inline struct pciintr_link_map *
pciintr_link_alloc(pci_chipset_tag_t pc,struct pcibios_intr_routing * pir,int pin)26964f0e01bSmickey pciintr_link_alloc(pci_chipset_tag_t pc, struct pcibios_intr_routing *pir, int pin)
2706661564bSmickey {
2710edf2c54Smickey 	int link = pir->linkmap[pin].link, clink, irq;
2726661564bSmickey 	struct pciintr_link_map *l, *lstart;
2736661564bSmickey 
274b25264e3Smickey 	if (pciintr_icu_tag != NULL) {
2750edf2c54Smickey 		/*
2760edf2c54Smickey 		 * Get the canonical link value for this entry.
2770edf2c54Smickey 		 */
278b25264e3Smickey 		if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
279b25264e3Smickey 		    link, &clink) != 0) {
2800edf2c54Smickey 			/*
2810edf2c54Smickey 			 * ICU doesn't understand the link value.
2820edf2c54Smickey 			 * Just ignore this PIR entry.
2830edf2c54Smickey 			 */
28464f0e01bSmickey 			PCIBIOS_PRINTV(("pciintr_link_alloc: bus %d device %d: "
28564f0e01bSmickey 			    "ignoring link 0x%02x\n", pir->bus,
28664f0e01bSmickey 			    PIR_DEVFUNC_DEVICE(pir->device), link));
2870edf2c54Smickey 			return (NULL);
2880edf2c54Smickey 		}
2890edf2c54Smickey 
2900edf2c54Smickey 		/*
291b25264e3Smickey 		 * Check the link value by asking the ICU for the
292b25264e3Smickey 		 * canonical link value.
2930edf2c54Smickey 		 * Also, determine if this PIRQ is mapped to an IRQ.
2940edf2c54Smickey 		 */
295b25264e3Smickey 		if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
296b25264e3Smickey 		    clink, &irq) != 0) {
2970edf2c54Smickey 			/*
2980edf2c54Smickey 			 * ICU doesn't understand the canonical link value.
2990edf2c54Smickey 			 * Just ignore this PIR entry.
3000edf2c54Smickey 			 */
30164f0e01bSmickey 			PCIBIOS_PRINTV(("pciintr_link_alloc: "
302b25264e3Smickey 			    "bus %d device %d link 0x%02x: "
303b25264e3Smickey 			    "ignoring PIRQ 0x%02x\n", pir->bus,
30464f0e01bSmickey 			    PIR_DEVFUNC_DEVICE(pir->device), link, clink));
3050edf2c54Smickey 			return (NULL);
3060edf2c54Smickey 		}
307b25264e3Smickey 	}
3080edf2c54Smickey 
30928a8f404Sart 	if ((l = malloc(sizeof(*l), M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
3100edf2c54Smickey 		return (NULL);
3116661564bSmickey 
3120edf2c54Smickey 	l->link = link;
3136661564bSmickey 	l->bitmap = pir->linkmap[pin].bitmap;
314b25264e3Smickey 	if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
3150edf2c54Smickey 		l->clink = clink;
3160edf2c54Smickey 		l->irq = irq; /* maybe I386_PCI_INTERRUPT_LINE_NO_CONNECTION */
317b25264e3Smickey 	} else {
31864f0e01bSmickey 		l->clink = link;
319b25264e3Smickey 		l->irq = I386_PCI_INTERRUPT_LINE_NO_CONNECTION;
320b25264e3Smickey 	}
3216661564bSmickey 
3226661564bSmickey 	lstart = SIMPLEQ_FIRST(&pciintr_link_map_list);
3236661564bSmickey 	if (lstart == NULL || lstart->link < l->link)
3246661564bSmickey 		SIMPLEQ_INSERT_TAIL(&pciintr_link_map_list, l, list);
3256661564bSmickey 	else
3266661564bSmickey 		SIMPLEQ_INSERT_HEAD(&pciintr_link_map_list, l, list);
3276661564bSmickey 
3286661564bSmickey 	return (l);
3296661564bSmickey }
3306661564bSmickey 
3316661564bSmickey struct pcibios_intr_routing *
pciintr_pir_lookup(int bus,int device)33208f75f72Sjsg pciintr_pir_lookup(int bus, int device)
3336661564bSmickey {
3346661564bSmickey 	struct pcibios_intr_routing *pir;
3356661564bSmickey 	int entry;
3366661564bSmickey 
3376661564bSmickey 	if (pcibios_pir_table == NULL)
3386661564bSmickey 		return (NULL);
3396661564bSmickey 
3406661564bSmickey 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
3416661564bSmickey 		pir = &pcibios_pir_table[entry];
3420edf2c54Smickey 		if (pir->bus == bus &&
3430edf2c54Smickey 		    PIR_DEVFUNC_DEVICE(pir->device) == device)
3446661564bSmickey 			return (pir);
3456661564bSmickey 	}
3466661564bSmickey 
3476661564bSmickey 	return (NULL);
3486661564bSmickey }
3496661564bSmickey 
35064f0e01bSmickey int
pciintr_bitmap_count_irq(int irq_bitmap,int * irqp)35108f75f72Sjsg pciintr_bitmap_count_irq(int irq_bitmap, int *irqp)
3520edf2c54Smickey {
3530edf2c54Smickey 	int i, bit, count = 0, irq = I386_PCI_INTERRUPT_LINE_NO_CONNECTION;
3540edf2c54Smickey 
35564f0e01bSmickey 	if (irq_bitmap != 0)
35664f0e01bSmickey 		for (i = 0, bit = 1; i < 16; i++, bit <<= 1)
3570edf2c54Smickey 			if (irq_bitmap & bit) {
3580edf2c54Smickey 				irq = i;
3590edf2c54Smickey 				count++;
3600edf2c54Smickey 			}
36164f0e01bSmickey 
3620edf2c54Smickey 	*irqp = irq;
3630edf2c54Smickey 	return (count);
3640edf2c54Smickey }
3650edf2c54Smickey 
36664f0e01bSmickey static __inline int
pciintr_link_init(pci_chipset_tag_t pc)36764f0e01bSmickey pciintr_link_init(pci_chipset_tag_t pc)
3686661564bSmickey {
369b25264e3Smickey 	int entry, pin, link;
3706661564bSmickey 	struct pcibios_intr_routing *pir;
3716661564bSmickey 	struct pciintr_link_map *l;
3726661564bSmickey 
3736661564bSmickey 	if (pcibios_pir_table == NULL) {
3746661564bSmickey 		/* No PIR table; can't do anything. */
3756661564bSmickey 		printf("pciintr_link_init: no PIR table\n");
3766661564bSmickey 		return (1);
3776661564bSmickey 	}
3786661564bSmickey 
3796661564bSmickey 	SIMPLEQ_INIT(&pciintr_link_map_list);
3806661564bSmickey 
3816661564bSmickey 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
3826661564bSmickey 		pir = &pcibios_pir_table[entry];
3830edf2c54Smickey 		for (pin = 0; pin < PCI_INTERRUPT_PIN_MAX; pin++) {
38464f0e01bSmickey 			if ((link = pir->linkmap[pin].link) == 0)
3856661564bSmickey 				/* No connection for this pin. */
3866661564bSmickey 				continue;
38764f0e01bSmickey 
3886661564bSmickey 			/*
3896661564bSmickey 			 * Multiple devices may be wired to the same
3906661564bSmickey 			 * interrupt; check to see if we've seen this
3916661564bSmickey 			 * one already.  If not, allocate a new link
3926661564bSmickey 			 * map entry and stuff it in the map.
3936661564bSmickey 			 */
39464f0e01bSmickey 			if ((l = pciintr_link_lookup(link)) == NULL)
39564f0e01bSmickey 				pciintr_link_alloc(pc, pir, pin);
39664f0e01bSmickey 			else if (pir->linkmap[pin].bitmap != l->bitmap) {
3970edf2c54Smickey 				/*
3980edf2c54Smickey 				 * violates PCI IRQ Routing Table Specification
3990edf2c54Smickey 				 */
40064f0e01bSmickey 				PCIBIOS_PRINTV(("pciintr_link_init: "
4010edf2c54Smickey 				    "bus %d device %d link 0x%02x: "
4020edf2c54Smickey 				    "bad irq bitmap 0x%04x, "
40364f0e01bSmickey 				    "should be 0x%04x\n", pir->bus,
40464f0e01bSmickey 				    PIR_DEVFUNC_DEVICE(pir->device), link,
40564f0e01bSmickey 				    pir->linkmap[pin].bitmap, l->bitmap));
4060edf2c54Smickey 				/* safer value. */
4070edf2c54Smickey 				l->bitmap &= pir->linkmap[pin].bitmap;
4080edf2c54Smickey 				/* XXX - or, should ignore this entry? */
4090edf2c54Smickey 			}
4106661564bSmickey 		}
4116661564bSmickey 	}
4126661564bSmickey 
413b25264e3Smickey 	return (0);
414b25264e3Smickey }
415b25264e3Smickey 
416b25264e3Smickey /*
417b25264e3Smickey  * No compatible PCI ICU found.
418b25264e3Smickey  * Hopes the BIOS already setup the ICU.
419b25264e3Smickey  */
42064f0e01bSmickey static __inline int
pciintr_guess_irq(void)42164f0e01bSmickey pciintr_guess_irq(void)
422b25264e3Smickey {
423b25264e3Smickey 	struct pciintr_link_map *l;
424b25264e3Smickey 	int irq, guessed = 0;
425b25264e3Smickey 
426b25264e3Smickey 	/*
427b25264e3Smickey 	 * Stage 1: If only one IRQ is available for the link, use it.
428b25264e3Smickey 	 */
429b25264e3Smickey 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
430b25264e3Smickey 	     l = SIMPLEQ_NEXT(l, list)) {
431b25264e3Smickey 		if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
432b25264e3Smickey 			continue;
433b25264e3Smickey 		if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
434b25264e3Smickey 			l->irq = irq;
435b25264e3Smickey 			l->fixup_stage = 1;
43664f0e01bSmickey 			if (pcibios_flags & PCIBIOS_INTRDEBUG)
437b25264e3Smickey 				printf("pciintr_guess_irq (stage 1): "
438b25264e3Smickey 				    "guessing PIRQ 0x%02x to be IRQ %d\n",
439b25264e3Smickey 				    l->clink, l->irq);
440b25264e3Smickey 			guessed = 1;
441b25264e3Smickey 		}
442b25264e3Smickey 	}
443b25264e3Smickey 
444b25264e3Smickey 	return (guessed ? 0 : -1);
4456661564bSmickey }
4466661564bSmickey 
44764f0e01bSmickey static __inline int
pciintr_link_fixup(void)44864f0e01bSmickey pciintr_link_fixup(void)
4496661564bSmickey {
4506661564bSmickey 	struct pciintr_link_map *l;
4510edf2c54Smickey 	u_int16_t pciirq = 0;
45264f0e01bSmickey 	int irq;
4536661564bSmickey 
4546661564bSmickey 	/*
4556661564bSmickey 	 * First stage: Attempt to connect PIRQs which aren't
4566661564bSmickey 	 * yet connected.
4576661564bSmickey 	 */
4586661564bSmickey 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
4596661564bSmickey 	     l = SIMPLEQ_NEXT(l, list)) {
4600edf2c54Smickey 		if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
4616661564bSmickey 			/*
4620edf2c54Smickey 			 * Interrupt is already connected.  Don't do
4630edf2c54Smickey 			 * anything to it.
4640edf2c54Smickey 			 * In this case, l->fixup_stage == 0.
4656661564bSmickey 			 */
4660edf2c54Smickey 			pciirq |= 1 << l->irq;
46764f0e01bSmickey 			if (pcibios_flags & PCIBIOS_INTRDEBUG)
46864f0e01bSmickey 				printf("pciintr_link_fixup: PIRQ 0x%02x is "
46964f0e01bSmickey 				    "already connected to IRQ %d\n",
47064f0e01bSmickey 				    l->clink, l->irq);
4716661564bSmickey 			continue;
4726661564bSmickey 		}
4736661564bSmickey 		/*
4740edf2c54Smickey 		 * Interrupt isn't connected.  Attempt to assign it to an IRQ.
4756661564bSmickey 		 */
47664f0e01bSmickey 		if (pcibios_flags & PCIBIOS_INTRDEBUG)
4770edf2c54Smickey 			printf("pciintr_link_fixup: PIRQ 0x%02x not connected",
4786661564bSmickey 			    l->clink);
47964f0e01bSmickey 
4806661564bSmickey 		/*
4810edf2c54Smickey 		 * Just do the easy case now; we'll defer the harder ones
4820edf2c54Smickey 		 * to Stage 2.
4836661564bSmickey 		 */
4840edf2c54Smickey 		if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
4850edf2c54Smickey 			l->irq = irq;
4866661564bSmickey 			l->fixup_stage = 1;
4870edf2c54Smickey 			pciirq |= 1 << irq;
48864f0e01bSmickey 			if (pcibios_flags & PCIBIOS_INTRDEBUG)
4896661564bSmickey 				printf(", assigning IRQ %d", l->irq);
4906661564bSmickey 		}
49164f0e01bSmickey 		if (pcibios_flags & PCIBIOS_INTRDEBUG)
4926661564bSmickey 			printf("\n");
4936661564bSmickey 	}
4946661564bSmickey 
4956661564bSmickey 	/*
4966661564bSmickey 	 * Stage 2: Attempt to connect PIRQs which we didn't
4976661564bSmickey 	 * connect in Stage 1.
4986661564bSmickey 	 */
4996661564bSmickey 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
50064f0e01bSmickey 	     l = SIMPLEQ_NEXT(l, list))
50164f0e01bSmickey 		if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION &&
50264f0e01bSmickey 		    (irq = ffs(l->bitmap & pciirq)) > 0) {
5036661564bSmickey 			/*
5040edf2c54Smickey 			 * This IRQ is a valid PCI IRQ already
5050edf2c54Smickey 			 * connected to another PIRQ, and also an
5060edf2c54Smickey 			 * IRQ our PIRQ can use; connect it up!
5076661564bSmickey 			 */
5086661564bSmickey 			l->fixup_stage = 2;
50964f0e01bSmickey 			l->irq = irq - 1;
51064f0e01bSmickey 			if (pcibios_flags & PCIBIOS_INTRDEBUG)
5110edf2c54Smickey 				printf("pciintr_link_fixup (stage 2): "
5120edf2c54Smickey 				       "assigning IRQ %d to PIRQ 0x%02x\n",
5130edf2c54Smickey 				       l->irq, l->clink);
5146661564bSmickey 		}
5156661564bSmickey 
5160edf2c54Smickey #ifdef PCIBIOS_IRQS_HINT
5176661564bSmickey 	/*
5180edf2c54Smickey 	 * Stage 3: The worst case. I need configuration hint that
5190edf2c54Smickey 	 * user supplied a mask for the PCI irqs
5206661564bSmickey 	 */
5210edf2c54Smickey 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
5220edf2c54Smickey 	     l = SIMPLEQ_NEXT(l, list)) {
52364f0e01bSmickey 		if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION &&
52464f0e01bSmickey 		    (irq = ffs(l->bitmap & pcibios_irqs_hint)) > 0) {
5250edf2c54Smickey 			l->fixup_stage = 3;
52664f0e01bSmickey 			l->irq = irq - 1;
52764f0e01bSmickey 			if (pcibios_flags & PCIBIOS_INTRDEBUG)
5280edf2c54Smickey 				printf("pciintr_link_fixup (stage 3): "
5290edf2c54Smickey 				    "assigning IRQ %d to PIRQ 0x%02x\n",
5300edf2c54Smickey 				    l->irq, l->clink);
5310edf2c54Smickey 		}
5320edf2c54Smickey 	}
5330edf2c54Smickey #endif /* PCIBIOS_IRQS_HINT */
5346661564bSmickey 
53564f0e01bSmickey 	if (pcibios_flags & PCIBIOS_INTRDEBUG)
53664f0e01bSmickey 		printf("pciintr_link_fixup: piirq 0x%04x\n", pciirq);
53764f0e01bSmickey 
5386661564bSmickey 	return (0);
5396661564bSmickey }
5406661564bSmickey 
5416661564bSmickey int
pci_intr_route_link(pci_chipset_tag_t pc,pci_intr_handle_t * ihp)54208f75f72Sjsg pci_intr_route_link(pci_chipset_tag_t pc, pci_intr_handle_t *ihp)
5436661564bSmickey {
5446661564bSmickey 	struct pciintr_link_map *l;
54564f0e01bSmickey 	pcireg_t intr;
546a25c7806Smickey 	int irq, rv = 1;
54764f0e01bSmickey 	char *p = NULL;
5486661564bSmickey 
5498b0f2e63Smickey 	if (pcibios_flags & PCIBIOS_INTR_FIXUP)
5508b0f2e63Smickey 		return 1;
5518b0f2e63Smickey 
552a25c7806Smickey 	irq = ihp->line & APIC_INT_LINE_MASK;
553a25c7806Smickey 	if (irq != 0 && irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
554a25c7806Smickey 		pcibios_pir_header.exclusive_irq |= 1 << irq;
555ff51c22aSmickey 
55664f0e01bSmickey 	l = ihp->link;
55764f0e01bSmickey 	if (!l || pciintr_icu_tag == NULL)
55864f0e01bSmickey 		return (1);
5596661564bSmickey 
5600edf2c54Smickey 	if (l->fixup_stage == 0) {
5610edf2c54Smickey 		if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
5620edf2c54Smickey 			/* Appropriate interrupt was not found. */
56364f0e01bSmickey 			if (pcibios_flags & PCIBIOS_INTRDEBUG)
56464f0e01bSmickey 				printf("pci_intr_route_link: PIRQ 0x%02x: "
56564f0e01bSmickey 				    "no IRQ, try "
56664f0e01bSmickey 				    "\"option PCIBIOS_IRQS_HINT=0x%04x\"\n",
5670edf2c54Smickey 				    l->clink,
5680edf2c54Smickey 				    /* suggest irq 9/10/11, if possible */
5690edf2c54Smickey 				    (l->bitmap & 0x0e00) ? (l->bitmap & 0x0e00)
5700edf2c54Smickey 				    : l->bitmap);
57164f0e01bSmickey 		} else
57264f0e01bSmickey 			p = " preserved BIOS setting";
5730edf2c54Smickey 	} else {
5740edf2c54Smickey 
5756661564bSmickey 		if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
5766661564bSmickey 		    l->clink, l->irq) != 0 ||
57764f0e01bSmickey 		    pciintr_icu_set_trigger(pciintr_icu_tag, pciintr_icu_handle,
5786661564bSmickey 		    l->irq, IST_LEVEL) != 0) {
57964f0e01bSmickey 			p = " failed";
58064f0e01bSmickey 			rv = 0;
58164f0e01bSmickey 		} else
58264f0e01bSmickey 			p = "";
5836661564bSmickey 	}
58464f0e01bSmickey 	if (p && pcibios_flags & PCIBIOS_INTRDEBUG)
58564f0e01bSmickey 		printf("pci_intr_route_link: route PIRQ 0x%02x -> IRQ %d%s\n",
58664f0e01bSmickey 		    l->clink, l->irq, p);
5876661564bSmickey 
58864f0e01bSmickey 	if (!rv)
5896661564bSmickey 		return (0);
5900edf2c54Smickey 
5916661564bSmickey 	/*
5920edf2c54Smickey 	 * IRQs 14 and 15 are reserved for PCI IDE interrupts; don't muck
5936661564bSmickey 	 * with them.
5946661564bSmickey 	 */
595a25c7806Smickey 	if (irq == 14 || irq == 15)
59664f0e01bSmickey 		return (1);
59764f0e01bSmickey 
59864f0e01bSmickey 	intr = pci_conf_read(pc, ihp->tag, PCI_INTERRUPT_REG);
599a25c7806Smickey 	if (irq != PCI_INTERRUPT_LINE(intr)) {
60064f0e01bSmickey 		intr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
601a25c7806Smickey 		intr |= irq << PCI_INTERRUPT_LINE_SHIFT;
60264f0e01bSmickey 		pci_conf_write(pc, ihp->tag, PCI_INTERRUPT_REG, intr);
6030edf2c54Smickey 	}
6046661564bSmickey 
60564f0e01bSmickey 	return (1);
60664f0e01bSmickey }
60764f0e01bSmickey 
60864f0e01bSmickey int
pci_intr_post_fixup(void)60908f75f72Sjsg pci_intr_post_fixup(void)
61064f0e01bSmickey {
61164f0e01bSmickey 	struct pciintr_link_map *l;
61264f0e01bSmickey 	int i, pciirq;
61364f0e01bSmickey 
6148b0f2e63Smickey 	if (pcibios_flags & PCIBIOS_INTR_FIXUP)
6158b0f2e63Smickey 		return 1;
6168b0f2e63Smickey 
61764f0e01bSmickey 	if (!pciintr_icu_handle)
61864f0e01bSmickey 		return 0;
61964f0e01bSmickey 
62064f0e01bSmickey 	pciirq = pcibios_pir_header.exclusive_irq;
62164f0e01bSmickey 	if (pcibios_flags & PCIBIOS_INTRDEBUG)
62264f0e01bSmickey 		printf("pci_intr_post_fixup: PCI IRQs:");
62364f0e01bSmickey 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list);
62464f0e01bSmickey 	    l != NULL; l = SIMPLEQ_NEXT(l, list))
625ff51c22aSmickey 		if (l->fixup_stage == 0 && l->irq != 0 &&
62664f0e01bSmickey 		    l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
62764f0e01bSmickey 			if (pcibios_flags & PCIBIOS_INTRDEBUG)
62864f0e01bSmickey 				printf(" %d", l->irq);
62964f0e01bSmickey 			pciirq |= (1 << l->irq);
63064f0e01bSmickey 		}
63164f0e01bSmickey 
63264f0e01bSmickey 	if (pcibios_flags & PCIBIOS_INTRDEBUG)
63364f0e01bSmickey 		printf("; ISA IRQs:");
63464f0e01bSmickey 	for (i = 0; i < 16; i++)
63564f0e01bSmickey 		if (!(pciirq & (1 << i))) {
63664f0e01bSmickey 			if (pcibios_flags & PCIBIOS_INTRDEBUG)
63764f0e01bSmickey 				printf(" %d", i);
63864f0e01bSmickey 			pciintr_icu_set_trigger(pciintr_icu_tag,
63964f0e01bSmickey 			    pciintr_icu_handle, i, IST_EDGE);
64064f0e01bSmickey 		}
64164f0e01bSmickey 
64264f0e01bSmickey 	if (pcibios_flags & PCIBIOS_INTRDEBUG)
64364f0e01bSmickey 		printf("\n");
64464f0e01bSmickey 
64564f0e01bSmickey 	return (0);
64664f0e01bSmickey }
64764f0e01bSmickey 
64864f0e01bSmickey int
pci_intr_header_fixup(pci_chipset_tag_t pc,pcitag_t tag,pci_intr_handle_t * ihp)64908f75f72Sjsg pci_intr_header_fixup(pci_chipset_tag_t pc, pcitag_t tag,
65008f75f72Sjsg     pci_intr_handle_t *ihp)
65164f0e01bSmickey {
65264f0e01bSmickey 	struct pcibios_intr_routing *pir;
65364f0e01bSmickey 	struct pciintr_link_map *l;
65464f0e01bSmickey 	int irq, link, bus, device, function;
65564f0e01bSmickey 	char *p = NULL;
65664f0e01bSmickey 
6578b0f2e63Smickey 	if (pcibios_flags & PCIBIOS_INTR_FIXUP)
6588b0f2e63Smickey 		return 1;
6598b0f2e63Smickey 
660a25c7806Smickey 	irq = ihp->line & APIC_INT_LINE_MASK;
66164f0e01bSmickey 	ihp->link = NULL;
66264f0e01bSmickey 	pci_decompose_tag(pc, tag, &bus, &device, &function);
66364f0e01bSmickey 
66464f0e01bSmickey 	if ((pir = pciintr_pir_lookup(bus, device)) == NULL ||
66564f0e01bSmickey 	    (link = pir->linkmap[ihp->pin - 1].link) == 0) {
66664f0e01bSmickey 		PCIBIOS_PRINTV(("Interrupt not connected; no need to change."));
66764f0e01bSmickey 		return 1;
66864f0e01bSmickey 	}
66964f0e01bSmickey 
67064f0e01bSmickey 	if ((l = pciintr_link_lookup(link)) == NULL) {
67164f0e01bSmickey 		/*
67264f0e01bSmickey 		 * No link map entry.
67364f0e01bSmickey 		 * Probably pciintr_icu_getclink() or pciintr_icu_get_intr()
674a25c7806Smickey 		 * has failed.
67564f0e01bSmickey 		 */
67664f0e01bSmickey 		if (pcibios_flags & PCIBIOS_INTRDEBUG)
67764f0e01bSmickey 			printf("pci_intr_header_fixup: no entry for link "
67864f0e01bSmickey 			    "0x%02x (%d:%d:%d:%c)\n",
67964f0e01bSmickey 			    link, bus, device, function, '@' + ihp->pin);
68064f0e01bSmickey 		return 1;
68164f0e01bSmickey 	}
68264f0e01bSmickey 
6831120a2ccSmarkus 	ihp->link = l;
6841120a2ccSmarkus 	if (irq == 14 || irq == 15) {
68564f0e01bSmickey 		p = " WARNING: ignored";
6861120a2ccSmarkus 		ihp->link = NULL;
6871120a2ccSmarkus 	} else if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
68864f0e01bSmickey 
6890edf2c54Smickey 		/* Appropriate interrupt was not found. */
690a25c7806Smickey 		if (pciintr_icu_tag == NULL && irq != 0 &&
691a25c7806Smickey 		    irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
692b25264e3Smickey 			/*
693b25264e3Smickey 			 * Do not print warning,
694b25264e3Smickey 			 * if no compatible PCI ICU found,
695b25264e3Smickey 			 * but the irq is already assigned by BIOS.
696b25264e3Smickey 			 */
69764f0e01bSmickey 			p = "";
69864f0e01bSmickey 		else
69964f0e01bSmickey 			p = " WARNING: missing";
70064f0e01bSmickey 	} else if (irq == 0 || irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
7010edf2c54Smickey 
70264f0e01bSmickey 		p = " fixed up";
703a25c7806Smickey 		ihp->line = irq = l->irq;
7040edf2c54Smickey 
705a003464cSmickey 	} else if (pcibios_flags & PCIBIOS_FIXUP_FORCE) {
7060edf2c54Smickey 		/* routed by BIOS, but inconsistent */
7070edf2c54Smickey 		/* believe PCI IRQ Routing table */
70864f0e01bSmickey 		p = " WARNING: overriding";
709a25c7806Smickey 		ihp->line = irq = l->irq;
71078b5d43cSmickey 	} else {
7110edf2c54Smickey 		/* believe PCI Interrupt Configuration Register (default) */
71264f0e01bSmickey 		p = " WARNING: preserving";
713a25c7806Smickey 		ihp->line = (l->irq = irq) | (l->clink & PCI_INT_VIA_ISA);
71478b5d43cSmickey 	}
7156661564bSmickey 
71664f0e01bSmickey 	if (pcibios_flags & PCIBIOS_INTRDEBUG) {
71708f75f72Sjsg 		pcireg_t id = pci_conf_read(pc, tag, PCI_ID_REG);
71864f0e01bSmickey 
71978b5d43cSmickey 		printf("\n%d:%d:%d %04x:%04x pin %c clink 0x%02x irq %d "
72078b5d43cSmickey 		    "stage %d %s irq %d\n", bus, device, function,
72164f0e01bSmickey 		    PCI_VENDOR(id), PCI_PRODUCT(id), '@' + ihp->pin, l->clink,
72264f0e01bSmickey 		    ((l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION)?
72364f0e01bSmickey 		    -1 : l->irq), l->fixup_stage, p, irq);
72464f0e01bSmickey 	}
72564f0e01bSmickey 
72664f0e01bSmickey 	return (1);
7276661564bSmickey }
7286661564bSmickey 
7296661564bSmickey int
pci_intr_fixup(struct pcibios_softc * sc,pci_chipset_tag_t pc,bus_space_tag_t iot)73008f75f72Sjsg pci_intr_fixup(struct pcibios_softc *sc, pci_chipset_tag_t pc,
73108f75f72Sjsg     bus_space_tag_t iot)
7326661564bSmickey {
733df73a7d0Smickey 	struct pcibios_pir_header *pirh = &pcibios_pir_header;
7346661564bSmickey 	const struct pciintr_icu_table *piit = NULL;
7356661564bSmickey 	pcitag_t icutag;
7366661564bSmickey 
7376661564bSmickey 	/*
7386661564bSmickey 	 * Attempt to initialize our PCI interrupt router.  If
7396661564bSmickey 	 * the PIR Table is present in ROM, use the location
7406661564bSmickey 	 * specified by the PIR Table, and use the compat ID,
7416661564bSmickey 	 * if present.  Otherwise, we have to look for the router
7426661564bSmickey 	 * ourselves (the PCI-ISA bridge).
743911088d6Smickey 	 *
744911088d6Smickey 	 * A number of buggy BIOS implementations leave the router
745911088d6Smickey 	 * entry as 000:00:0, which is typically not the correct
746911088d6Smickey 	 * device/function.  If the router device address is set to
747911088d6Smickey 	 * this value, and the compatible router entry is undefined
748911088d6Smickey 	 * (zero is the correct value to indicate undefined), then we
749911088d6Smickey 	 * work on the basis it is most likely an error, and search
750911088d6Smickey 	 * the entire device-space of bus 0 (but obviously starting
751911088d6Smickey 	 * with 000:00:0, in case that really is the right one).
7526661564bSmickey 	 */
753911088d6Smickey 	if (pirh->signature != 0 && (pirh->router_bus != 0 ||
754911088d6Smickey 	    pirh->router_devfunc != 0 || pirh->compat_router != 0)) {
755911088d6Smickey 
7566c0a4d12Smickey 		icutag = pci_make_tag(pc, pirh->router_bus,
7576c0a4d12Smickey 		    PIR_DEVFUNC_DEVICE(pirh->router_devfunc),
7586c0a4d12Smickey 		    PIR_DEVFUNC_FUNCTION(pirh->router_devfunc));
759911088d6Smickey 		if (pirh->compat_router == 0 ||
760911088d6Smickey 		    (piit = pciintr_icu_lookup(pirh->compat_router)) == NULL) {
7616661564bSmickey 			/*
7626661564bSmickey 			 * No compat ID, or don't know the compat ID?  Read
7636661564bSmickey 			 * it from the configuration header.
7646661564bSmickey 			 */
7656c0a4d12Smickey 			pirh->compat_router = pci_conf_read(pc, icutag,
766911088d6Smickey 			    PCI_ID_REG);
7676661564bSmickey 		}
7686661564bSmickey 		if (piit == NULL)
769911088d6Smickey 			piit = pciintr_icu_lookup(pirh->compat_router);
7706661564bSmickey 	} else {
7716661564bSmickey 		int device, maxdevs = pci_bus_maxdevs(pc, 0);
7726661564bSmickey 
7736661564bSmickey 		/*
7746661564bSmickey 		 * Search configuration space for a known interrupt
7756661564bSmickey 		 * router.
7766661564bSmickey 		 */
7776661564bSmickey 		for (device = 0; device < maxdevs; device++) {
778911088d6Smickey 			const struct pci_quirkdata *qd;
779911088d6Smickey 			int function, nfuncs;
780911088d6Smickey 			pcireg_t icuid;
781911088d6Smickey 			pcireg_t bhlcr;
782911088d6Smickey 
7836661564bSmickey 			icutag = pci_make_tag(pc, 0, device, 0);
7846661564bSmickey 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
7856661564bSmickey 
7866661564bSmickey 			/* Invalid vendor ID value? */
7876661564bSmickey 			if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
7886661564bSmickey 				continue;
7896661564bSmickey 			/* XXX Not invalid, but we've done this ~forever. */
7906661564bSmickey 			if (PCI_VENDOR(icuid) == 0)
7916661564bSmickey 				continue;
7926661564bSmickey 
793911088d6Smickey 			qd = pci_lookup_quirkdata(PCI_VENDOR(icuid),
794911088d6Smickey 			    PCI_PRODUCT(icuid));
795911088d6Smickey 
796911088d6Smickey 			bhlcr = pci_conf_read(pc, icutag, PCI_BHLC_REG);
797911088d6Smickey 			if (PCI_HDRTYPE_MULTIFN(bhlcr) || (qd != NULL &&
798911088d6Smickey 			    (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
799911088d6Smickey 				nfuncs = 8;
800911088d6Smickey 			else
801911088d6Smickey 				nfuncs = 1;
802911088d6Smickey 
803911088d6Smickey 			for (function = 0; function < nfuncs; function++) {
804911088d6Smickey 				icutag = pci_make_tag(pc, 0, device, function);
805911088d6Smickey 				icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
806911088d6Smickey 
807911088d6Smickey 				/* Invalid vendor ID value? */
808911088d6Smickey 				if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
809911088d6Smickey 					continue;
810911088d6Smickey 				/* Not invalid, but we've done this ~forever. */
811911088d6Smickey 				if (PCI_VENDOR(icuid) == 0)
812911088d6Smickey 					continue;
813911088d6Smickey 
814df73a7d0Smickey 				if ((piit = pciintr_icu_lookup(icuid))) {
815df73a7d0Smickey 					pirh->compat_router = icuid;
816df73a7d0Smickey 					pirh->router_bus = 0;
817df73a7d0Smickey 					pirh->router_devfunc =
818df73a7d0Smickey 					    PIR_DEVFUNC_COMPOSE(device, 0);
8196661564bSmickey 					break;
8206661564bSmickey 				}
8216661564bSmickey 			}
822911088d6Smickey 
823911088d6Smickey 			if (piit != NULL)
824911088d6Smickey 				break;
825911088d6Smickey 		}
826df73a7d0Smickey 	}
8276661564bSmickey 
8286661564bSmickey 	if (piit == NULL) {
829df73a7d0Smickey 		printf("%s: no compatible PCI ICU found", sc->sc_dev.dv_xname);
830911088d6Smickey 		if (pirh->signature != 0 && pirh->compat_router != 0)
831b25264e3Smickey 			printf(": ICU vendor 0x%04x product 0x%04x",
832911088d6Smickey 			    PCI_VENDOR(pirh->compat_router),
833911088d6Smickey 			    PCI_PRODUCT(pirh->compat_router));
834b25264e3Smickey 		printf("\n");
835b25264e3Smickey 		if (!(pcibios_flags & PCIBIOS_INTR_GUESS)) {
83664f0e01bSmickey 			if (pciintr_link_init(pc))
837b25264e3Smickey 				return (-1);	/* non-fatal */
838b25264e3Smickey 			if (pciintr_guess_irq())
839b25264e3Smickey 				return (-1);	/* non-fatal */
840bb701a32Smickey 		}
841bb701a32Smickey 		return (0);
842df73a7d0Smickey 	} else {
843df73a7d0Smickey 		char devinfo[256];
844df73a7d0Smickey 
845df73a7d0Smickey 		printf("%s: PCI Interrupt Router at %03d:%02d:%01d",
846df73a7d0Smickey 		    sc->sc_dev.dv_xname, pirh->router_bus,
847df73a7d0Smickey 		    PIR_DEVFUNC_DEVICE(pirh->router_devfunc),
848df73a7d0Smickey 		    PIR_DEVFUNC_FUNCTION(pirh->router_devfunc));
849df73a7d0Smickey 		if (pirh->compat_router != 0) {
8502627362dSho 			pci_devinfo(pirh->compat_router, 0, 0, devinfo,
8512627362dSho 			    sizeof devinfo);
852df73a7d0Smickey 			printf(" (%s)", devinfo);
853df73a7d0Smickey 		}
854df73a7d0Smickey 		printf("\n");
8556661564bSmickey 	}
8566661564bSmickey 
8576661564bSmickey 	/*
8586661564bSmickey 	 * Initialize the PCI ICU.
8596661564bSmickey 	 */
8606661564bSmickey 	if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
8616661564bSmickey 	    &pciintr_icu_handle) != 0)
8626661564bSmickey 		return (-1);		/* non-fatal */
8636661564bSmickey 
8646661564bSmickey 	/*
8656661564bSmickey 	 * Initialize the PCI interrupt link map.
8666661564bSmickey 	 */
86764f0e01bSmickey 	if (pciintr_link_init(pc))
8686661564bSmickey 		return (-1);		/* non-fatal */
8696661564bSmickey 
8706661564bSmickey 	/*
8716661564bSmickey 	 * Fix up the link->IRQ mappings.
8726661564bSmickey 	 */
8736661564bSmickey 	if (pciintr_link_fixup() != 0)
8746661564bSmickey 		return (-1);		/* non-fatal */
8756661564bSmickey 
87664f0e01bSmickey 	return (0);
8776661564bSmickey }
878