Home | History | Annotate | Line # | Download | only in pci
pci_intr_fixup.c revision 1.27
      1 /*	$NetBSD: pci_intr_fixup.c,v 1.27 2003/10/27 04:10:43 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1999, by UCHIYAMA Yasushi
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. The name of the developer may NOT be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 /*
     66  * PCI Interrupt Router support.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: pci_intr_fixup.c,v 1.27 2003/10/27 04:10:43 christos Exp $");
     71 
     72 #include "opt_pcibios.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/kernel.h>
     77 #include <sys/malloc.h>
     78 #include <sys/queue.h>
     79 #include <sys/device.h>
     80 
     81 #include <machine/bus.h>
     82 #include <machine/intr.h>
     83 
     84 #include <dev/pci/pcireg.h>
     85 #include <dev/pci/pcivar.h>
     86 #include <dev/pci/pcidevs.h>
     87 
     88 #include <i386/pci/pci_intr_fixup.h>
     89 #include <i386/pci/pcibios.h>
     90 
     91 struct pciintr_link_map {
     92 	int link;
     93 	int clink;
     94 	int irq;
     95 	u_int16_t bitmap;
     96 	int fixup_stage;
     97 	SIMPLEQ_ENTRY(pciintr_link_map) list;
     98 };
     99 
    100 pciintr_icu_tag_t pciintr_icu_tag;
    101 pciintr_icu_handle_t pciintr_icu_handle;
    102 
    103 #ifdef PCIBIOS_IRQS_HINT
    104 int pcibios_irqs_hint = PCIBIOS_IRQS_HINT;
    105 #endif
    106 
    107 struct pciintr_link_map *pciintr_link_lookup __P((int));
    108 struct pciintr_link_map *pciintr_link_alloc __P((struct pcibios_intr_routing *,
    109 	int));
    110 struct pcibios_intr_routing *pciintr_pir_lookup __P((int, int));
    111 static int pciintr_bitmap_count_irq __P((int, int *));
    112 static int pciintr_bitmap_find_lowest_irq __P((int, int *));
    113 int	pciintr_link_init __P((void));
    114 #ifdef PCIBIOS_INTR_GUESS
    115 int	pciintr_guess_irq __P((void));
    116 #endif
    117 int	pciintr_link_fixup __P((void));
    118 int	pciintr_link_route __P((u_int16_t *));
    119 int	pciintr_irq_release __P((u_int16_t *));
    120 int	pciintr_header_fixup __P((pci_chipset_tag_t));
    121 void	pciintr_do_header_fixup __P((pci_chipset_tag_t, pcitag_t, void*));
    122 
    123 SIMPLEQ_HEAD(, pciintr_link_map) pciintr_link_map_list;
    124 
    125 const struct pciintr_icu_table {
    126 	pci_vendor_id_t	piit_vendor;
    127 	pci_product_id_t piit_product;
    128 	int (*piit_init) __P((pci_chipset_tag_t,
    129 		bus_space_tag_t, pcitag_t, pciintr_icu_tag_t *,
    130 		pciintr_icu_handle_t *));
    131 } pciintr_icu_table[] = {
    132 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371MX,
    133 	  piix_init },
    134 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371AB_ISA,
    135 	  piix_init },
    136 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371FB_ISA,
    137 	  piix_init },
    138 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371SB_ISA,
    139 	  piix_init },
    140 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801BA_LPC,
    141 	  piix_init },
    142 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801BAM_LPC,
    143 	  piix_init },
    144 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801DB_LPC,
    145 	  piix_init },
    146 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801EB_LPC,
    147 	  piix_init },
    148 
    149 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C558,
    150 	  opti82c558_init },
    151 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C700,
    152 	  opti82c700_init },
    153 
    154 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C586_ISA,
    155 	  via82c586_init },
    156 	{ PCI_VENDOR_VIATECH,   PCI_PRODUCT_VIATECH_VT82C596A,
    157 	  via82c586_init },
    158 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C686A_ISA,
    159 	  via82c586_init },
    160 
    161 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_85C503,
    162 	  sis85c503_init },
    163 
    164 	{ PCI_VENDOR_AMD,	PCI_PRODUCT_AMD_PBC756_PMC,
    165 	  amd756_init },
    166 
    167 	{ PCI_VENDOR_ALI,	PCI_PRODUCT_ALI_M1543,
    168 	  ali1543_init },
    169 
    170 	{ 0,			0,
    171 	  NULL },
    172 };
    173 
    174 const struct pciintr_icu_table *pciintr_icu_lookup __P((pcireg_t));
    175 
    176 const struct pciintr_icu_table *
    177 pciintr_icu_lookup(id)
    178 	pcireg_t id;
    179 {
    180 	const struct pciintr_icu_table *piit;
    181 
    182 	for (piit = pciintr_icu_table;
    183 	     piit->piit_init != NULL;
    184 	     piit++) {
    185 		if (PCI_VENDOR(id) == piit->piit_vendor &&
    186 		    PCI_PRODUCT(id) == piit->piit_product)
    187 			return (piit);
    188 	}
    189 
    190 	return (NULL);
    191 }
    192 
    193 struct pciintr_link_map *
    194 pciintr_link_lookup(link)
    195 	int link;
    196 {
    197 	struct pciintr_link_map *l;
    198 
    199 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    200 		if (l->link == link)
    201 			return (l);
    202 	}
    203 
    204 	return (NULL);
    205 }
    206 
    207 struct pciintr_link_map *
    208 pciintr_link_alloc(pir, pin)
    209 	struct pcibios_intr_routing *pir;
    210 	int pin;
    211 {
    212 	int link = pir->linkmap[pin].link, clink, irq;
    213 	struct pciintr_link_map *l, *lstart;
    214 
    215 	if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
    216 		/*
    217 		 * Get the canonical link value for this entry.
    218 		 */
    219 		if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
    220 		    link, &clink) != 0) {
    221 			/*
    222 			 * ICU doesn't understand the link value.
    223 			 * Just ignore this PIR entry.
    224 			 */
    225 #ifdef DIAGNOSTIC
    226 			printf("pciintr_link_alloc: bus %d device %d: "
    227 			    "link 0x%02x invalid\n",
    228 			    pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link);
    229 #endif
    230 			return (NULL);
    231 		}
    232 
    233 		/*
    234 		 * Check the link value by asking the ICU for the
    235 		 * canonical link value.
    236 		 * Also, determine if this PIRQ is mapped to an IRQ.
    237 		 */
    238 		if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
    239 		    clink, &irq) != 0) {
    240 			/*
    241 			 * ICU doesn't understand the canonical link value.
    242 			 * Just ignore this PIR entry.
    243 			 */
    244 #ifdef DIAGNOSTIC
    245 			printf("pciintr_link_alloc: "
    246 			    "bus %d device %d link 0x%02x: "
    247 			    "PIRQ 0x%02x invalid\n",
    248 			    pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link,
    249 			    clink);
    250 #endif
    251 			return (NULL);
    252 		}
    253 	}
    254 
    255 	l = malloc(sizeof(*l), M_DEVBUF, M_NOWAIT);
    256 	if (l == NULL)
    257 		panic("pciintr_link_alloc");
    258 
    259 	memset(l, 0, sizeof(*l));
    260 
    261 	l->link = link;
    262 	l->bitmap = pir->linkmap[pin].bitmap;
    263 	if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
    264 		l->clink = clink;
    265 		l->irq = irq; /* maybe X86_PCI_INTERRUPT_LINE_NO_CONNECTION */
    266 	} else {
    267 		l->clink = link; /* only for PCIBIOSVERBOSE diagnostic */
    268 		l->irq = X86_PCI_INTERRUPT_LINE_NO_CONNECTION;
    269 	}
    270 
    271 	lstart = SIMPLEQ_FIRST(&pciintr_link_map_list);
    272 	if (lstart == NULL || lstart->link < l->link)
    273 		SIMPLEQ_INSERT_TAIL(&pciintr_link_map_list, l, list);
    274 	else
    275 		SIMPLEQ_INSERT_HEAD(&pciintr_link_map_list, l, list);
    276 
    277 	return (l);
    278 }
    279 
    280 struct pcibios_intr_routing *
    281 pciintr_pir_lookup(bus, device)
    282 	int bus, device;
    283 {
    284 	struct pcibios_intr_routing *pir;
    285 	int entry;
    286 
    287 	if (pcibios_pir_table == NULL)
    288 		return (NULL);
    289 
    290 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
    291 		pir = &pcibios_pir_table[entry];
    292 		if (pir->bus == bus &&
    293 		    PIR_DEVFUNC_DEVICE(pir->device) == device)
    294 			return (pir);
    295 	}
    296 
    297 	return (NULL);
    298 }
    299 
    300 static int
    301 pciintr_bitmap_count_irq(irq_bitmap, irqp)
    302 	int irq_bitmap, *irqp;
    303 {
    304 	int i, bit, count = 0, irq = X86_PCI_INTERRUPT_LINE_NO_CONNECTION;
    305 
    306 	if (irq_bitmap != 0) {
    307 		for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    308 			if (irq_bitmap & bit) {
    309 				irq = i;
    310 				count++;
    311 			}
    312 		}
    313 	}
    314 	*irqp = irq;
    315 	return (count);
    316 }
    317 
    318 static int
    319 pciintr_bitmap_find_lowest_irq(irq_bitmap, irqp)
    320 	int irq_bitmap, *irqp;
    321 {
    322 	int i, bit;
    323 
    324 	if (irq_bitmap != 0) {
    325 		for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    326 			if (irq_bitmap & bit) {
    327 				*irqp = i;
    328 				return (1); /* found */
    329 			}
    330 		}
    331 	}
    332 	return (0); /* not found */
    333 }
    334 
    335 int
    336 pciintr_link_init()
    337 {
    338 	int entry, pin, link;
    339 	struct pcibios_intr_routing *pir;
    340 	struct pciintr_link_map *l;
    341 
    342 	if (pcibios_pir_table == NULL) {
    343 		/* No PIR table; can't do anything. */
    344 		printf("pciintr_link_init: no PIR table\n");
    345 		return (1);
    346 	}
    347 
    348 	SIMPLEQ_INIT(&pciintr_link_map_list);
    349 
    350 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
    351 		pir = &pcibios_pir_table[entry];
    352 		for (pin = 0; pin < PCI_INTERRUPT_PIN_MAX; pin++) {
    353 			link = pir->linkmap[pin].link;
    354 			if (link == 0) {
    355 				/* No connection for this pin. */
    356 				continue;
    357 			}
    358 			/*
    359 			 * Multiple devices may be wired to the same
    360 			 * interrupt; check to see if we've seen this
    361 			 * one already.  If not, allocate a new link
    362 			 * map entry and stuff it in the map.
    363 			 */
    364 			l = pciintr_link_lookup(link);
    365 			if (l == NULL) {
    366 				(void) pciintr_link_alloc(pir, pin);
    367 			} else if (pir->linkmap[pin].bitmap != l->bitmap) {
    368 				/*
    369 				 * violates PCI IRQ Routing Table Specification
    370 				 */
    371 #ifdef DIAGNOSTIC
    372 				printf("pciintr_link_init: "
    373 				    "bus %d device %d link 0x%02x: "
    374 				    "bad irq bitmap 0x%04x, "
    375 				    "should be 0x%04x\n",
    376 				    pir->bus, PIR_DEVFUNC_DEVICE(pir->device),
    377 				    link, pir->linkmap[pin].bitmap, l->bitmap);
    378 #endif
    379 				/* safer value. */
    380 				l->bitmap &= pir->linkmap[pin].bitmap;
    381 				/* XXX - or, should ignore this entry? */
    382 			}
    383 		}
    384 	}
    385 
    386 	return (0);
    387 }
    388 
    389 #ifdef PCIBIOS_INTR_GUESS
    390 /*
    391  * No compatible PCI ICU found.
    392  * Hopes the BIOS already setup the ICU.
    393  */
    394 int
    395 pciintr_guess_irq()
    396 {
    397 	struct pciintr_link_map *l;
    398 	int irq, guessed = 0;
    399 
    400 	/*
    401 	 * Stage 1: If only one IRQ is available for the link, use it.
    402 	 */
    403 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    404 		if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
    405 			continue;
    406 		if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
    407 			l->irq = irq;
    408 			l->fixup_stage = 1;
    409 #ifdef PCIINTR_DEBUG
    410 			printf("pciintr_guess_irq (stage 1): "
    411 			    "guessing PIRQ 0x%02x to be IRQ %d\n",
    412 			    l->clink, l->irq);
    413 #endif
    414 			guessed = 1;
    415 		}
    416 	}
    417 
    418 	return (guessed ? 0 : -1);
    419 }
    420 #endif /* PCIBIOS_INTR_GUESS */
    421 
    422 int
    423 pciintr_link_fixup()
    424 {
    425 	struct pciintr_link_map *l;
    426 	int irq;
    427 	u_int16_t pciirq = 0;
    428 
    429 	/*
    430 	 * First stage: Attempt to connect PIRQs which aren't
    431 	 * yet connected.
    432 	 */
    433 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    434 		if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    435 			/*
    436 			 * Interrupt is already connected.  Don't do
    437 			 * anything to it.
    438 			 * In this case, l->fixup_stage == 0.
    439 			 */
    440 			pciirq |= 1 << l->irq;
    441 #ifdef PCIINTR_DEBUG
    442 			printf("pciintr_link_fixup: PIRQ 0x%02x already "
    443 			    "connected to IRQ %d\n", l->clink, l->irq);
    444 #endif
    445 			continue;
    446 		}
    447 		/*
    448 		 * Interrupt isn't connected.  Attempt to assign it to an IRQ.
    449 		 */
    450 #ifdef PCIINTR_DEBUG
    451 		printf("pciintr_link_fixup: PIRQ 0x%02x not connected",
    452 		    l->clink);
    453 #endif
    454 		/*
    455 		 * Just do the easy case now; we'll defer the harder ones
    456 		 * to Stage 2.
    457 		 */
    458 		if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
    459 			l->irq = irq;
    460 			l->fixup_stage = 1;
    461 			pciirq |= 1 << irq;
    462 #ifdef PCIINTR_DEBUG
    463 			printf(", assigning IRQ %d", l->irq);
    464 #endif
    465 		}
    466 #ifdef PCIINTR_DEBUG
    467 		printf("\n");
    468 #endif
    469 	}
    470 
    471 	/*
    472 	 * Stage 2: Attempt to connect PIRQs which we didn't
    473 	 * connect in Stage 1.
    474 	 */
    475 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    476 		if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
    477 			continue;
    478 		if (pciintr_bitmap_find_lowest_irq(l->bitmap & pciirq,
    479 		    &l->irq)) {
    480 			/*
    481 			 * This IRQ is a valid PCI IRQ already
    482 			 * connected to another PIRQ, and also an
    483 			 * IRQ our PIRQ can use; connect it up!
    484 			 */
    485 			l->fixup_stage = 2;
    486 #ifdef PCIINTR_DEBUG
    487 			printf("pciintr_link_fixup (stage 2): "
    488 			       "assigning IRQ %d to PIRQ 0x%02x\n",
    489 			       l->irq, l->clink);
    490 #endif
    491 		}
    492 	}
    493 
    494 #ifdef PCIBIOS_IRQS_HINT
    495 	/*
    496 	 * Stage 3: The worst case. I need configuration hint that
    497 	 * user supplied a mask for the PCI irqs
    498 	 */
    499 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    500 		if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
    501 			continue;
    502 		if (pciintr_bitmap_find_lowest_irq(
    503 		    l->bitmap & pcibios_irqs_hint, &l->irq)) {
    504 			l->fixup_stage = 3;
    505 #ifdef PCIINTR_DEBUG
    506 			printf("pciintr_link_fixup (stage 3): "
    507 			       "assigning IRQ %d to PIRQ 0x%02x\n",
    508 			       l->irq, l->clink);
    509 #endif
    510 		}
    511 	}
    512 #endif /* PCIBIOS_IRQS_HINT */
    513 
    514 	return (0);
    515 }
    516 
    517 int
    518 pciintr_link_route(pciirq)
    519 	u_int16_t *pciirq;
    520 {
    521 	struct pciintr_link_map *l;
    522 	int rv = 0;
    523 
    524 	*pciirq = 0;
    525 
    526 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    527 		if (l->fixup_stage == 0) {
    528 			if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    529 				/* Appropriate interrupt was not found. */
    530 #ifdef DIAGNOSTIC
    531 				printf("pciintr_link_route: "
    532 				    "PIRQ 0x%02x: no IRQ, try "
    533 				    "\"options PCIBIOS_IRQS_HINT=0x%04x\"\n",
    534 				    l->clink,
    535 				    /* suggest irq 9/10/11, if possible */
    536 				    (l->bitmap & 0x0e00) ? (l->bitmap & 0x0e00)
    537 				    : l->bitmap);
    538 #endif
    539 			} else {
    540 				/* BIOS setting has no problem */
    541 #ifdef PCIINTR_DEBUG
    542 				printf("pciintr_link_route: "
    543 				    "route of PIRQ 0x%02x -> "
    544 				    "IRQ %d preserved BIOS setting\n",
    545 				    l->clink, l->irq);
    546 #endif
    547 				*pciirq |= (1 << l->irq);
    548 			}
    549 			continue; /* nothing to do. */
    550 		}
    551 
    552 		if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
    553 					 l->clink, l->irq) != 0 ||
    554 		    pciintr_icu_set_trigger(pciintr_icu_tag,
    555 					    pciintr_icu_handle,
    556 					    l->irq, IST_LEVEL) != 0) {
    557 			printf("pciintr_link_route: route of PIRQ 0x%02x -> "
    558 			    "IRQ %d failed\n", l->clink, l->irq);
    559 			rv = 1;
    560 		} else {
    561 			/*
    562 			 * Succssfully routed interrupt.  Mark this as
    563 			 * a PCI interrupt.
    564 			 */
    565 			*pciirq |= (1 << l->irq);
    566 		}
    567 	}
    568 
    569 	return (rv);
    570 }
    571 
    572 int
    573 pciintr_irq_release(pciirq)
    574 	u_int16_t *pciirq;
    575 {
    576 	int i, bit;
    577 
    578 	for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    579 		if ((*pciirq & bit) == 0)
    580 			(void) pciintr_icu_set_trigger(pciintr_icu_tag,
    581 			    pciintr_icu_handle, i, IST_EDGE);
    582 	}
    583 
    584 	return (0);
    585 }
    586 
    587 int
    588 pciintr_header_fixup(pc)
    589 	pci_chipset_tag_t pc;
    590 {
    591 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    592 	PCIBIOS_PRINTV(("  device vendor product pin PIRQ IRQ stage\n"));
    593 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    594 	pci_device_foreach(pc, pcibios_max_bus, pciintr_do_header_fixup, NULL);
    595 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    596 
    597 	return (0);
    598 }
    599 
    600 void
    601 pciintr_do_header_fixup(pc, tag, context)
    602 	pci_chipset_tag_t pc;
    603 	pcitag_t tag;
    604 	void *context;
    605 {
    606 	struct pcibios_intr_routing *pir;
    607 	struct pciintr_link_map *l;
    608 	int pin, irq, link;
    609 	int bus, device, function;
    610 	pcireg_t intr, id;
    611 
    612 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    613 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    614 
    615 	intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    616 	pin = PCI_INTERRUPT_PIN(intr);
    617 	irq = PCI_INTERRUPT_LINE(intr);
    618 
    619 #if 0
    620 	if (pin == 0) {
    621 		/*
    622 		 * No interrupt used.
    623 		 */
    624 		return;
    625 	}
    626 #endif
    627 
    628 	pir = pciintr_pir_lookup(bus, device);
    629 	if (pir == NULL || (link = pir->linkmap[pin - 1].link) == 0) {
    630 		/*
    631 		 * Interrupt not connected; no
    632 		 * need to change.
    633 		 */
    634 		return;
    635 	}
    636 
    637 	l = pciintr_link_lookup(link);
    638 	if (l == NULL) {
    639 #ifdef PCIINTR_DEBUG
    640 		/*
    641 		 * No link map entry.
    642 		 * Probably pciintr_icu_getclink() or pciintr_icu_get_intr()
    643 		 * was failed.
    644 		 */
    645 		printf("pciintr_header_fixup: no entry for link 0x%02x "
    646 		       "(%d:%d:%d:%c)\n", link, bus, device, function,
    647 		       '@' + pin);
    648 #endif
    649 		return;
    650 	}
    651 
    652 #ifdef PCIBIOSVERBOSE
    653 	if (pcibiosverbose) {
    654 		printf("%03d:%02d:%d 0x%04x 0x%04x   %c  0x%02x",
    655 		    bus, device, function, PCI_VENDOR(id), PCI_PRODUCT(id),
    656 		    '@' + pin, l->clink);
    657 		if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
    658 			printf("   -");
    659 		else
    660 			printf(" %3d", l->irq);
    661 		printf("  %d   ", l->fixup_stage);
    662 	}
    663 #endif
    664 
    665 	/*
    666 	 * IRQs 14 and 15 are reserved for PCI IDE interrupts; don't muck
    667 	 * with them.
    668 	 */
    669 	if (irq == 14 || irq == 15) {
    670 		PCIBIOS_PRINTV((" WARNING: ignored\n"));
    671 		return;
    672 	}
    673 
    674 	if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    675 		/* Appropriate interrupt was not found. */
    676 		if (pciintr_icu_tag == NULL &&
    677 		    irq != 0 && irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    678 			/*
    679 			 * Do not print warning,
    680 			 * if no compatible PCI ICU found,
    681 			 * but the irq is already assigned by BIOS.
    682 			 */
    683 			PCIBIOS_PRINTV(("\n"));
    684 		} else {
    685 			PCIBIOS_PRINTV((" WARNING: missing IRQ\n"));
    686 		}
    687 		return;
    688 	}
    689 
    690 	if (l->irq == irq) {
    691 		/* don't have to reconfigure */
    692 		PCIBIOS_PRINTV((" already assigned\n"));
    693 		return;
    694 	}
    695 
    696 	if (irq == 0 || irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    697 		PCIBIOS_PRINTV((" fixed up\n"));
    698 	} else {
    699 		/* routed by BIOS, but inconsistent */
    700 #ifdef PCIBIOS_INTR_FIXUP_FORCE
    701 		/* believe PCI IRQ Routing table */
    702 		PCIBIOS_PRINTV((" WARNING: overriding irq %d\n", irq));
    703 #else
    704 		/* believe PCI Interrupt Configuration Register (default) */
    705 		PCIBIOS_PRINTV((" WARNING: preserving irq %d\n", irq));
    706 		return;
    707 #endif
    708 	}
    709 
    710 	intr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
    711 	intr |= (l->irq << PCI_INTERRUPT_LINE_SHIFT);
    712 	pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
    713 }
    714 
    715 int
    716 pci_intr_fixup(pc, iot, pciirq)
    717 	pci_chipset_tag_t pc;
    718 	bus_space_tag_t iot;
    719 	u_int16_t *pciirq;
    720 {
    721 	const struct pciintr_icu_table *piit = NULL;
    722 	pcitag_t icutag;
    723 	pcireg_t icuid;
    724 
    725 	/*
    726 	 * Attempt to initialize our PCI interrupt router.  If
    727 	 * the PIR Table is present in ROM, use the location
    728 	 * specified by the PIR Table, and use the compat ID,
    729 	 * if present.  Otherwise, we have to look for the router
    730 	 * ourselves (the PCI-ISA bridge).
    731 	 *
    732 	 * A number of buggy BIOS implementations leave the router
    733 	 * entry as 000:00:0, which is typically not the correct
    734 	 * device/function.  If the router device address is set to
    735 	 * this value, and the compatible router entry is undefined
    736 	 * (zero is the correct value to indicate undefined), then we
    737 	 * work on the basis it is most likely an error, and search
    738 	 * the entire device-space of bus 0 (but obviously starting
    739 	 * with 000:00:0, in case that really is the right one).
    740 	 */
    741 	if (pcibios_pir_header.signature != 0 &&
    742 	    (pcibios_pir_header.router_bus != 0 ||
    743 	     PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc) != 0 ||
    744 	     PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc) != 0 ||
    745 	     pcibios_pir_header.compat_router != 0)) {
    746 		icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
    747 		    PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
    748 		    PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
    749 		icuid = pcibios_pir_header.compat_router;
    750 		if (icuid == 0 ||
    751 		    (piit = pciintr_icu_lookup(icuid)) == NULL) {
    752 			/*
    753 			 * No compat ID, or don't know the compat ID?  Read
    754 			 * it from the configuration header.
    755 			 */
    756 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    757 		}
    758 		if (piit == NULL)
    759 			piit = pciintr_icu_lookup(icuid);
    760 	} else {
    761 		int device, maxdevs = pci_bus_maxdevs(pc, 0);
    762 
    763 		/*
    764 		 * Search configuration space for a known interrupt
    765 		 * router.
    766 		 */
    767 		for (device = 0; device < maxdevs; device++) {
    768 			const struct pci_quirkdata *qd;
    769 			int function, nfuncs;
    770 			pcireg_t bhlcr;
    771 
    772 			icutag = pci_make_tag(pc, 0, device, 0);
    773 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    774 
    775 			/* Invalid vendor ID value? */
    776 			if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
    777 				continue;
    778 			/* XXX Not invalid, but we've done this ~forever. */
    779 			if (PCI_VENDOR(icuid) == 0)
    780 				continue;
    781 
    782 			qd = pci_lookup_quirkdata(PCI_VENDOR(icuid),
    783 			    PCI_PRODUCT(icuid));
    784 
    785 			bhlcr = pci_conf_read(pc, icutag, PCI_BHLC_REG);
    786 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    787 			    (qd != NULL &&
    788 			     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    789 				nfuncs = 8;
    790 			else
    791 				nfuncs = 1;
    792 
    793 			for (function = 0; function < nfuncs; function++) {
    794 				icutag = pci_make_tag(pc, 0, device, function);
    795 				icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    796 
    797 				/* Invalid vendor ID value? */
    798 				if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
    799 					continue;
    800 				/* Not invalid, but we've done this ~forever */
    801 				if (PCI_VENDOR(icuid) == 0)
    802 					continue;
    803 
    804 				piit = pciintr_icu_lookup(icuid);
    805 				if (piit != NULL)
    806 					goto found;
    807 			}
    808 		}
    809 
    810 		/*
    811 		 * Invalidate the ICU ID.  If we failed to find the
    812 		 * interrupt router (piit == NULL) we don't want to
    813 		 * display a spurious device address below containing
    814 		 * the product information of the last device we
    815 		 * looked at.
    816 		 */
    817 		icuid = 0;
    818 found:;
    819 	}
    820 
    821 	if (piit == NULL) {
    822 		printf("pci_intr_fixup: no compatible PCI ICU found");
    823 		if (pcibios_pir_header.signature != 0 && icuid != 0)
    824 			printf(": ICU vendor 0x%04x product 0x%04x",
    825 			    PCI_VENDOR(icuid), PCI_PRODUCT(icuid));
    826 		printf("\n");
    827 #ifdef PCIBIOS_INTR_GUESS
    828 		if (pciintr_link_init())
    829 			return (-1);	/* non-fatal */
    830 		if (pciintr_guess_irq())
    831 			return (-1);	/* non-fatal */
    832 		if (pciintr_header_fixup(pc))
    833 			return (1);	/* fatal */
    834 		return (0);		/* success! */
    835 #else
    836 		return (-1);		/* non-fatal */
    837 #endif
    838 	}
    839 
    840 	/*
    841 	 * Initialize the PCI ICU.
    842 	 */
    843 	if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
    844 	    &pciintr_icu_handle) != 0)
    845 		return (-1);		/* non-fatal */
    846 
    847 	/*
    848 	 * Initialize the PCI interrupt link map.
    849 	 */
    850 	if (pciintr_link_init())
    851 		return (-1);		/* non-fatal */
    852 
    853 	/*
    854 	 * Fix up the link->IRQ mappings.
    855 	 */
    856 	if (pciintr_link_fixup() != 0)
    857 		return (-1);		/* non-fatal */
    858 
    859 	/*
    860 	 * Now actually program the PCI ICU with the new
    861 	 * routing information.
    862 	 */
    863 	if (pciintr_link_route(pciirq) != 0)
    864 		return (1);		/* fatal */
    865 
    866 	/*
    867 	 * Now that we've routed all of the PIRQs, rewrite the PCI
    868 	 * configuration headers to reflect the new mapping.
    869 	 */
    870 	if (pciintr_header_fixup(pc) != 0)
    871 		return (1);		/* fatal */
    872 
    873 	/*
    874 	 * Free any unused PCI IRQs for ISA devices.
    875 	 */
    876 	if (pciintr_irq_release(pciirq) != 0)
    877 		return (-1);		/* non-fatal */
    878 
    879 	/*
    880 	 * All done!
    881 	 */
    882 	return (0);			/* success! */
    883 }
    884