Home | History | Annotate | Line # | Download | only in pci
pci_intr_fixup.c revision 1.5.6.2
      1 /*	$NetBSD: pci_intr_fixup.c,v 1.5.6.2 2000/08/10 22:53:44 soda 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 "opt_pcibios.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/kernel.h>
     74 #include <sys/malloc.h>
     75 #include <sys/queue.h>
     76 #include <sys/device.h>
     77 
     78 #include <machine/bus.h>
     79 #include <machine/intr.h>
     80 
     81 #include <dev/pci/pcireg.h>
     82 #include <dev/pci/pcivar.h>
     83 #include <dev/pci/pcidevs.h>
     84 
     85 #include <i386/isa/icu.h>
     86 #include <i386/pci/pci_intr_fixup.h>
     87 #include <i386/pci/pcibios.h>
     88 
     89 struct pciintr_link_map {
     90 	int link;
     91 	int clink;
     92 	int irq;
     93 	u_int16_t bitmap;
     94 	int fixup_stage;
     95 	SIMPLEQ_ENTRY(pciintr_link_map) list;
     96 };
     97 
     98 pciintr_icu_tag_t pciintr_icu_tag = NULL;
     99 pciintr_icu_handle_t pciintr_icu_handle;
    100 
    101 #ifdef PCIBIOS_IRQS_HINT
    102 int pcibios_irqs_hint = PCIBIOS_IRQS_HINT;
    103 #endif
    104 
    105 struct pciintr_link_map *pciintr_link_lookup __P((int));
    106 struct pciintr_link_map *pciintr_link_alloc __P((struct pcibios_intr_routing *,
    107 	int));
    108 struct pcibios_intr_routing *pciintr_pir_lookup __P((int, int));
    109 static int pciintr_bitmap_count_irq __P((int, int *));
    110 static int pciintr_bitmap_find_lowest_irq __P((int, int *));
    111 int	pciintr_link_init __P((void));
    112 #ifdef PCIBIOS_INTR_GUESS
    113 int	pciintr_guess_irq __P((void));
    114 #endif
    115 int	pciintr_link_fixup __P((void));
    116 int	pciintr_link_route __P((u_int16_t *));
    117 int	pciintr_irq_release __P((u_int16_t *));
    118 int	pciintr_header_fixup __P((pci_chipset_tag_t));
    119 void	pciintr_do_header_fixup __P((pci_chipset_tag_t, pcitag_t));
    120 
    121 SIMPLEQ_HEAD(, pciintr_link_map) pciintr_link_map_list;
    122 
    123 const struct pciintr_icu_table {
    124 	pci_vendor_id_t	piit_vendor;
    125 	pci_product_id_t piit_product;
    126 	int (*piit_init) __P((pci_chipset_tag_t,
    127 		bus_space_tag_t, pcitag_t, pciintr_icu_tag_t *,
    128 		pciintr_icu_handle_t *));
    129 } pciintr_icu_table[] = {
    130 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371MX,
    131 	  piix_init },
    132 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371AB_ISA,
    133 	  piix_init },
    134 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371FB_ISA,
    135 	  piix_init },
    136 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371SB_ISA,
    137 	  piix_init },
    138 
    139 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C558,
    140 	  opti82c558_init },
    141 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C700,
    142 	  opti82c700_init },
    143 
    144 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C586_ISA,
    145 	  via82c586_init, },
    146 
    147 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_85C503,
    148 	  sis85c503_init },
    149 
    150 	{ 0,			0,
    151 	  NULL },
    152 };
    153 
    154 const struct pciintr_icu_table *pciintr_icu_lookup __P((pcireg_t));
    155 
    156 const struct pciintr_icu_table *
    157 pciintr_icu_lookup(id)
    158 	pcireg_t id;
    159 {
    160 	const struct pciintr_icu_table *piit;
    161 
    162 	for (piit = pciintr_icu_table;
    163 	     piit->piit_init != NULL;
    164 	     piit++) {
    165 		if (PCI_VENDOR(id) == piit->piit_vendor &&
    166 		    PCI_PRODUCT(id) == piit->piit_product)
    167 			return (piit);
    168 	}
    169 
    170 	return (NULL);
    171 }
    172 
    173 struct pciintr_link_map *
    174 pciintr_link_lookup(link)
    175 	int link;
    176 {
    177 	struct pciintr_link_map *l;
    178 
    179 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    180 	     l = SIMPLEQ_NEXT(l, list)) {
    181 		if (l->link == link)
    182 			return (l);
    183 	}
    184 
    185 	return (NULL);
    186 }
    187 
    188 struct pciintr_link_map *
    189 pciintr_link_alloc(pir, pin)
    190 	struct pcibios_intr_routing *pir;
    191 	int pin;
    192 {
    193 	int link = pir->linkmap[pin].link, clink, irq;
    194 	struct pciintr_link_map *l, *lstart;
    195 
    196 	if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
    197 		/*
    198 		 * Get the canonical link value for this entry.
    199 		 */
    200 		if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
    201 		    link, &clink) != 0) {
    202 			/*
    203 			 * ICU doesn't understand the link value.
    204 			 * Just ignore this PIR entry.
    205 			 */
    206 #ifdef DIAGNOSTIC
    207 			printf("pciintr_link_alloc: bus %d device %d: "
    208 			    "link 0x%02x invalid\n",
    209 			    pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link);
    210 #endif
    211 			return (NULL);
    212 		}
    213 
    214 		/*
    215 		 * Check the link value by asking the ICU for the
    216 		 * canonical link value.
    217 		 * Also, determine if this PIRQ is mapped to an IRQ.
    218 		 */
    219 		if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
    220 		    clink, &irq) != 0) {
    221 			/*
    222 			 * ICU doesn't understand the canonical link value.
    223 			 * Just ignore this PIR entry.
    224 			 */
    225 #ifdef DIAGNOSTIC
    226 			printf("pciintr_link_alloc: "
    227 			    "bus %d device %d link 0x%02x: "
    228 			    "PIRQ 0x%02x invalid\n",
    229 			    pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link,
    230 			    clink);
    231 #endif
    232 			return (NULL);
    233 		}
    234 	}
    235 
    236 	l = malloc(sizeof(*l), M_DEVBUF, M_NOWAIT);
    237 	if (l == NULL)
    238 		panic("pciintr_link_alloc");
    239 
    240 	memset(l, 0, sizeof(*l));
    241 
    242 	l->link = link;
    243 	l->bitmap = pir->linkmap[pin].bitmap;
    244 	if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
    245 		l->clink = clink;
    246 		l->irq = irq; /* maybe I386_PCI_INTERRUPT_LINE_NO_CONNECTION */
    247 	} else {
    248 		l->clink = link; /* only for PCIBIOSVERBOSE diagnostic */
    249 		l->irq = I386_PCI_INTERRUPT_LINE_NO_CONNECTION;
    250 	}
    251 
    252 	lstart = SIMPLEQ_FIRST(&pciintr_link_map_list);
    253 	if (lstart == NULL || lstart->link < l->link)
    254 		SIMPLEQ_INSERT_TAIL(&pciintr_link_map_list, l, list);
    255 	else
    256 		SIMPLEQ_INSERT_HEAD(&pciintr_link_map_list, l, list);
    257 
    258 	return (l);
    259 }
    260 
    261 struct pcibios_intr_routing *
    262 pciintr_pir_lookup(bus, device)
    263 	int bus, device;
    264 {
    265 	struct pcibios_intr_routing *pir;
    266 	int entry;
    267 
    268 	if (pcibios_pir_table == NULL)
    269 		return (NULL);
    270 
    271 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
    272 		pir = &pcibios_pir_table[entry];
    273 		if (pir->bus == bus &&
    274 		    PIR_DEVFUNC_DEVICE(pir->device) == device)
    275 			return (pir);
    276 	}
    277 
    278 	return (NULL);
    279 }
    280 
    281 static int
    282 pciintr_bitmap_count_irq(irq_bitmap, irqp)
    283 	int irq_bitmap, *irqp;
    284 {
    285 	int i, bit, count = 0, irq = I386_PCI_INTERRUPT_LINE_NO_CONNECTION;
    286 
    287 	if (irq_bitmap != 0) {
    288 		for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    289 			if (irq_bitmap & bit) {
    290 				irq = i;
    291 				count++;
    292 			}
    293 		}
    294 	}
    295 	*irqp = irq;
    296 	return (count);
    297 }
    298 
    299 static int
    300 pciintr_bitmap_find_lowest_irq(irq_bitmap, irqp)
    301 	int irq_bitmap, *irqp;
    302 {
    303 	int i, bit;
    304 
    305 	if (irq_bitmap != 0) {
    306 		for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    307 			if (irq_bitmap & bit) {
    308 				*irqp = i;
    309 				return (1); /* found */
    310 			}
    311 		}
    312 	}
    313 	return (0); /* not found */
    314 }
    315 
    316 int
    317 pciintr_link_init()
    318 {
    319 	int entry, pin, link;
    320 	struct pcibios_intr_routing *pir;
    321 	struct pciintr_link_map *l;
    322 
    323 	if (pcibios_pir_table == NULL) {
    324 		/* No PIR table; can't do anything. */
    325 		printf("pciintr_link_init: no PIR table\n");
    326 		return (1);
    327 	}
    328 
    329 	SIMPLEQ_INIT(&pciintr_link_map_list);
    330 
    331 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
    332 		pir = &pcibios_pir_table[entry];
    333 		for (pin = 0; pin < PCI_INTERRUPT_PIN_MAX; pin++) {
    334 			link = pir->linkmap[pin].link;
    335 			if (link == 0) {
    336 				/* No connection for this pin. */
    337 				continue;
    338 			}
    339 			/*
    340 			 * Multiple devices may be wired to the same
    341 			 * interrupt; check to see if we've seen this
    342 			 * one already.  If not, allocate a new link
    343 			 * map entry and stuff it in the map.
    344 			 */
    345 			l = pciintr_link_lookup(link);
    346 			if (l == NULL) {
    347 				(void) pciintr_link_alloc(pir, pin);
    348 			} else if (pir->linkmap[pin].bitmap != l->bitmap) {
    349 				/*
    350 				 * violates PCI IRQ Routing Table Specification
    351 				 */
    352 #ifdef DIAGNOSTIC
    353 				printf("pciintr_link_init: "
    354 				    "bus %d device %d link 0x%02x: "
    355 				    "bad irq bitmap 0x%04x, "
    356 				    "should be 0x%04x\n",
    357 				    pir->bus, PIR_DEVFUNC_DEVICE(pir->device),
    358 				    link, pir->linkmap[pin].bitmap, l->bitmap);
    359 #endif
    360 				/* safer value. */
    361 				l->bitmap &= pir->linkmap[pin].bitmap;
    362 				/* XXX - or, should ignore this entry? */
    363 			}
    364 		}
    365 	}
    366 
    367 	return (0);
    368 }
    369 
    370 #ifdef PCIBIOS_INTR_GUESS
    371 /*
    372  * No compatible PCI ICU found.
    373  * Hopes the BIOS already setup the ICU.
    374  */
    375 int
    376 pciintr_guess_irq()
    377 {
    378 	struct pciintr_link_map *l;
    379 	int irq, guessed = 0;
    380 
    381 	/*
    382 	 * Stage 1: If only one IRQ is available for the link, use it.
    383 	 */
    384 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    385 	     l = SIMPLEQ_NEXT(l, list)) {
    386 		if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
    387 			continue;
    388 		if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
    389 			l->irq = irq;
    390 			l->fixup_stage = 1;
    391 #ifdef PCIINTR_DEBUG
    392 			printf("pciintr_guess_irq (stage 1): "
    393 			    "guessing PIRQ 0x%02x to be IRQ %d\n",
    394 			    l->clink, l->irq);
    395 #endif
    396 			guessed = 1;
    397 		}
    398 	}
    399 
    400 	return (guessed ? 0 : -1);
    401 }
    402 #endif /* PCIBIOS_INTR_GUESS */
    403 
    404 int
    405 pciintr_link_fixup()
    406 {
    407 	struct pciintr_link_map *l;
    408 	int irq;
    409 	u_int16_t pciirq = 0;
    410 
    411 	/*
    412 	 * First stage: Attempt to connect PIRQs which aren't
    413 	 * yet connected.
    414 	 */
    415 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    416 	     l = SIMPLEQ_NEXT(l, list)) {
    417 		if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    418 			/*
    419 			 * Interrupt is already connected.  Don't do
    420 			 * anything to it.
    421 			 * In this case, l->fixup_stage == 0.
    422 			 */
    423 			pciirq |= 1 << l->irq;
    424 #ifdef PCIINTR_DEBUG
    425 			printf("pciintr_link_fixup: PIRQ 0x%02x already "
    426 			    "connected to IRQ %d\n", l->clink, l->irq);
    427 #endif
    428 			continue;
    429 		}
    430 		/*
    431 		 * Interrupt isn't connected.  Attempt to assign it to an IRQ.
    432 		 */
    433 #ifdef PCIINTR_DEBUG
    434 		printf("pciintr_link_fixup: PIRQ 0x%02x not connected",
    435 		    l->clink);
    436 #endif
    437 		/*
    438 		 * Just do the easy case now; we'll defer the harder ones
    439 		 * to Stage 2.
    440 		 */
    441 		if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
    442 			l->irq = irq;
    443 			l->fixup_stage = 1;
    444 			pciirq |= 1 << irq;
    445 #ifdef PCIINTR_DEBUG
    446 			printf(", assigning IRQ %d", l->irq);
    447 #endif
    448 		}
    449 #ifdef PCIINTR_DEBUG
    450 		printf("\n");
    451 #endif
    452 	}
    453 
    454 	/*
    455 	 * Stage 2: Attempt to connect PIRQs which we didn't
    456 	 * connect in Stage 1.
    457 	 */
    458 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    459 	     l = SIMPLEQ_NEXT(l, list)) {
    460 		if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
    461 			continue;
    462 		if (pciintr_bitmap_find_lowest_irq(l->bitmap & pciirq,
    463 		    &l->irq)) {
    464 			/*
    465 			 * This IRQ is a valid PCI IRQ already
    466 			 * connected to another PIRQ, and also an
    467 			 * IRQ our PIRQ can use; connect it up!
    468 			 */
    469 			l->fixup_stage = 2;
    470 #ifdef PCIINTR_DEBUG
    471 			printf("pciintr_link_fixup (stage 2): "
    472 			       "assigning IRQ %d to PIRQ 0x%02x\n",
    473 			       l->irq, l->clink);
    474 #endif
    475 		}
    476 	}
    477 
    478 #ifdef PCIBIOS_IRQS_HINT
    479 	/*
    480 	 * Stage 3: The worst case. I need configuration hint that
    481 	 * user supplied a mask for the PCI irqs
    482 	 */
    483 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    484 	     l = SIMPLEQ_NEXT(l, list)) {
    485 		if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
    486 			continue;
    487 		if (pciintr_bitmap_find_lowest_irq(
    488 		    l->bitmap & pcibios_irqs_hint, &l->irq)) {
    489 			l->fixup_stage = 3;
    490 #ifdef PCIINTR_DEBUG
    491 			printf("pciintr_link_fixup (stage 3): "
    492 			       "assigning IRQ %d to PIRQ 0x%02x\n",
    493 			       l->irq, l->clink);
    494 #endif
    495 		}
    496 	}
    497 #endif /* PCIBIOS_IRQS_HINT */
    498 
    499 	return (0);
    500 }
    501 
    502 int
    503 pciintr_link_route(pciirq)
    504 	u_int16_t *pciirq;
    505 {
    506 	struct pciintr_link_map *l;
    507 	int rv = 0;
    508 
    509 	*pciirq = 0;
    510 
    511 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    512 	     l = SIMPLEQ_NEXT(l, list)) {
    513 		if (l->fixup_stage == 0) {
    514 			if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    515 				/* Appropriate interrupt was not found. */
    516 #ifdef DIAGNOSTIC
    517 				printf("pciintr_link_route: "
    518 				    "PIRQ 0x%02x: no IRQ, try "
    519 				    "\"options PCIBIOS_IRQS_HINT=0x%04x\"\n",
    520 				    l->clink,
    521 				    /* suggest irq 9/10/11, if possible */
    522 				    (l->bitmap & 0x0e00) ? (l->bitmap & 0x0e00)
    523 				    : l->bitmap);
    524 #endif
    525 			} else {
    526 				/* BIOS setting has no problem */
    527 #ifdef PCIINTR_DEBUG
    528 				printf("pciintr_link_route: "
    529 				    "route of PIRQ 0x%02x -> "
    530 				    "IRQ %d preserved BIOS setting\n",
    531 				    l->clink, l->irq);
    532 #endif
    533 				*pciirq |= (1 << l->irq);
    534 			}
    535 			continue; /* nothing to do. */
    536 		}
    537 
    538 		if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
    539 					 l->clink, l->irq) != 0 ||
    540 		    pciintr_icu_set_trigger(pciintr_icu_tag,
    541 					    pciintr_icu_handle,
    542 					    l->irq, IST_LEVEL) != 0) {
    543 			printf("pciintr_link_route: route of PIRQ 0x%02x -> "
    544 			    "IRQ %d failed\n", l->clink, l->irq);
    545 			rv = 1;
    546 		} else {
    547 			/*
    548 			 * Succssfully routed interrupt.  Mark this as
    549 			 * a PCI interrupt.
    550 			 */
    551 			*pciirq |= (1 << l->irq);
    552 		}
    553 	}
    554 
    555 	return (rv);
    556 }
    557 
    558 int
    559 pciintr_irq_release(pciirq)
    560 	u_int16_t *pciirq;
    561 {
    562 	int i, bit;
    563 
    564 	for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    565 		if ((*pciirq & bit) == 0)
    566 			(void) pciintr_icu_set_trigger(pciintr_icu_tag,
    567 			    pciintr_icu_handle, i, IST_EDGE);
    568 	}
    569 
    570 	return (0);
    571 }
    572 
    573 int
    574 pciintr_header_fixup(pc)
    575 	pci_chipset_tag_t pc;
    576 {
    577 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    578 	PCIBIOS_PRINTV(("  device vendor product pin PIRQ IRQ stage\n"));
    579 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    580 	pci_device_foreach(pc, pcibios_max_bus, pciintr_do_header_fixup);
    581 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    582 
    583 	return (0);
    584 }
    585 
    586 void
    587 pciintr_do_header_fixup(pc, tag)
    588 	pci_chipset_tag_t pc;
    589 	pcitag_t tag;
    590 {
    591 	struct pcibios_intr_routing *pir;
    592 	struct pciintr_link_map *l;
    593 	int pin, irq, link;
    594 	int bus, device, function;
    595 	pcireg_t intr, id;
    596 
    597 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    598 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    599 
    600 	intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    601 	pin = PCI_INTERRUPT_PIN(intr);
    602 	irq = PCI_INTERRUPT_LINE(intr);
    603 
    604 	if (pin == 0) {
    605 		/*
    606 		 * No interrupt used.
    607 		 */
    608 		return;
    609 	}
    610 
    611 	pir = pciintr_pir_lookup(bus, device);
    612 	if (pir == NULL || (link = pir->linkmap[pin - 1].link) == 0) {
    613 		/*
    614 		 * Interrupt not connected; no
    615 		 * need to change.
    616 		 */
    617 		return;
    618 	}
    619 
    620 	l = pciintr_link_lookup(link);
    621 	if (l == NULL) {
    622 #ifdef PCIINTR_DEBUG
    623 		/*
    624 		 * No link map entry.
    625 		 * Probably pciintr_icu_getclink() or pciintr_icu_get_intr()
    626 		 * was failed.
    627 		 */
    628 		printf("pciintr_header_fixup: no entry for link 0x%02x "
    629 		       "(%d:%d:%d:%c)\n", link, bus, device, function,
    630 		       '@' + pin);
    631 #endif
    632 		return;
    633 	}
    634 
    635 #ifdef PCIBIOSVERBOSE
    636 	if (pcibiosverbose) {
    637 		printf("%03d:%02d:%d 0x%04x 0x%04x   %c  0x%02x",
    638 		    bus, device, function, PCI_VENDOR(id), PCI_PRODUCT(id),
    639 		    '@' + pin, l->clink);
    640 		if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
    641 			printf("   -");
    642 		else
    643 			printf(" %3d", l->irq);
    644 		printf("  %d   ", l->fixup_stage);
    645 	}
    646 #endif
    647 
    648 	/*
    649 	 * IRQs 14 and 15 are reserved for PCI IDE interrupts; don't muck
    650 	 * with them.
    651 	 */
    652 	if (irq == 14 || irq == 15) {
    653 		PCIBIOS_PRINTV((" WARNING: ignored\n"));
    654 		return;
    655 	}
    656 
    657 	if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    658 		/* Appropriate interrupt was not found. */
    659 		if (pciintr_icu_tag == NULL &&
    660 		    irq != 0 && irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    661 			/*
    662 			 * Do not print warning,
    663 			 * if no compatible PCI ICU found,
    664 			 * but the irq is already assigned by BIOS.
    665 			 */
    666 			PCIBIOS_PRINTV(("\n"));
    667 		} else {
    668 			PCIBIOS_PRINTV((" WARNING: missing IRQ\n"));
    669 		}
    670 		return;
    671 	}
    672 
    673 	if (l->irq == irq) {
    674 		/* don't have to reconfigure */
    675 		PCIBIOS_PRINTV((" already assigned\n"));
    676 		return;
    677 	}
    678 
    679 	if (irq == 0 || irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    680 		PCIBIOS_PRINTV((" fixed up\n"));
    681 	} else {
    682 		/* routed by BIOS, but inconsistent */
    683 #ifdef PCIBIOS_INTR_FIXUP_FORCE
    684 		/* believe PCI IRQ Routing table */
    685 		PCIBIOS_PRINTV((" WARNING: overriding irq %d\n", irq));
    686 #else
    687 		/* believe PCI Interrupt Configuration Register (default) */
    688 		PCIBIOS_PRINTV((" WARNING: preserving irq %d\n", irq));
    689 		return;
    690 #endif
    691 	}
    692 
    693 	intr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
    694 	intr |= (l->irq << PCI_INTERRUPT_LINE_SHIFT);
    695 	pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
    696 }
    697 
    698 int
    699 pci_intr_fixup(pc, iot, pciirq)
    700 	pci_chipset_tag_t pc;
    701 	bus_space_tag_t iot;
    702 	u_int16_t *pciirq;
    703 {
    704 	const struct pciintr_icu_table *piit = NULL;
    705 	pcitag_t icutag;
    706 	pcireg_t icuid;
    707 
    708 	/*
    709 	 * Attempt to initialize our PCI interrupt router.  If
    710 	 * the PIR Table is present in ROM, use the location
    711 	 * specified by the PIR Table, and use the compat ID,
    712 	 * if present.  Otherwise, we have to look for the router
    713 	 * ourselves (the PCI-ISA bridge).
    714 	 */
    715 	if (pcibios_pir_header.signature != 0) {
    716 		icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
    717 		    PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
    718 		    PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
    719 		icuid = pcibios_pir_header.compat_router;
    720 		if (icuid == 0 ||
    721 		    (piit = pciintr_icu_lookup(icuid)) == NULL) {
    722 			/*
    723 			 * No compat ID, or don't know the compat ID?  Read
    724 			 * it from the configuration header.
    725 			 */
    726 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    727 		}
    728 		if (piit == NULL)
    729 			piit = pciintr_icu_lookup(icuid);
    730 	} else {
    731 		int device, maxdevs = pci_bus_maxdevs(pc, 0);
    732 
    733 		/*
    734 		 * Search configuration space for a known interrupt
    735 		 * router.
    736 		 */
    737 		for (device = 0; device < maxdevs; device++) {
    738 			icutag = pci_make_tag(pc, 0, device, 0);
    739 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    740 
    741 			/* Invalid vendor ID value? */
    742 			if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
    743 				continue;
    744 			/* XXX Not invalid, but we've done this ~forever. */
    745 			if (PCI_VENDOR(icuid) == 0)
    746 				continue;
    747 
    748 			piit = pciintr_icu_lookup(icuid);
    749 			if (piit != NULL)
    750 				break;
    751 		}
    752 	}
    753 
    754 	if (piit == NULL) {
    755 		printf("pci_intr_fixup: no compatible PCI ICU found");
    756 		if (pcibios_pir_header.signature != 0 && icuid != 0)
    757 			printf(": ICU vendor 0x%04x product 0x%04x",
    758 			    PCI_VENDOR(icuid), PCI_PRODUCT(icuid));
    759 		printf("\n");
    760 #ifdef PCIBIOS_INTR_GUESS
    761 		if (pciintr_link_init())
    762 			return (-1);	/* non-fatal */
    763 		if (pciintr_guess_irq())
    764 			return (-1);	/* non-fatal */
    765 		if (pciintr_header_fixup(pc))
    766 			return (1);	/* fatal */
    767 		return (0);		/* success! */
    768 #else
    769 		return (-1);		/* non-fatal */
    770 #endif
    771 	}
    772 
    773 	/*
    774 	 * Initialize the PCI ICU.
    775 	 */
    776 	if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
    777 	    &pciintr_icu_handle) != 0)
    778 		return (-1);		/* non-fatal */
    779 
    780 	/*
    781 	 * Initialize the PCI interrupt link map.
    782 	 */
    783 	if (pciintr_link_init())
    784 		return (-1);		/* non-fatal */
    785 
    786 	/*
    787 	 * Fix up the link->IRQ mappings.
    788 	 */
    789 	if (pciintr_link_fixup() != 0)
    790 		return (-1);		/* non-fatal */
    791 
    792 	/*
    793 	 * Now actually program the PCI ICU with the new
    794 	 * routing information.
    795 	 */
    796 	if (pciintr_link_route(pciirq) != 0)
    797 		return (1);		/* fatal */
    798 
    799 	/*
    800 	 * Now that we've routed all of the PIRQs, rewrite the PCI
    801 	 * configuration headers to reflect the new mapping.
    802 	 */
    803 	if (pciintr_header_fixup(pc) != 0)
    804 		return (1);		/* fatal */
    805 
    806 	/*
    807 	 * Free any unused PCI IRQs for ISA devices.
    808 	 */
    809 	if (pciintr_irq_release(pciirq) != 0)
    810 		return (-1);		/* non-fatal */
    811 
    812 	/*
    813 	 * All done!
    814 	 */
    815 	return (0);			/* success! */
    816 }
    817