Home | History | Annotate | Line # | Download | only in pci
pci_intr_fixup.c revision 1.1
      1 /*	$NetBSD: pci_intr_fixup.c,v 1.1 1999/11/17 01:20:38 thorpej 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 	int old_irq;
     96 	SIMPLEQ_ENTRY(pciintr_link_map) list;
     97 };
     98 
     99 pciintr_icu_tag_t pciintr_icu_tag;
    100 pciintr_icu_handle_t pciintr_icu_handle;
    101 
    102 struct pciintr_link_map *pciintr_link_lookup_pin
    103 	__P((struct pcibios_intr_routing *, int));
    104 struct pciintr_link_map *pciintr_link_lookup_link __P((int));
    105 struct pciintr_link_map *pciintr_link_alloc __P((struct pcibios_intr_routing *,
    106 	int));
    107 struct pcibios_intr_routing *pciintr_pir_lookup __P((int, int));
    108 int	pciintr_link_init __P((void));
    109 int	pciintr_link_fixup __P((void));
    110 int	pciintr_link_route __P((u_int16_t *));
    111 int	pciintr_irq_release __P((u_int16_t *));
    112 int	pciintr_header_fixup __P((pci_chipset_tag_t));
    113 
    114 SIMPLEQ_HEAD(, pciintr_link_map) pciintr_link_map_list;
    115 
    116 const struct pciintr_icu_table {
    117 	pci_vendor_id_t	piit_vendor;
    118 	pci_product_id_t piit_product;
    119 	int (*piit_init) __P((pci_chipset_tag_t,
    120 		bus_space_tag_t, pcitag_t, pciintr_icu_tag_t *,
    121 		pciintr_icu_handle_t *));
    122 } pciintr_icu_table[] = {
    123 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371MX,
    124 	  piix_init },
    125 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371AB_ISA,
    126 	  piix_init },
    127 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371FB_ISA,
    128 	  piix_init },
    129 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371SB_ISA,
    130 	  piix_init },
    131 
    132 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C558,
    133 	  opti82c558_init },
    134 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C700,
    135 	  opti82c700_init },
    136 
    137 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C586_ISA,
    138 	  via82c586_init, },
    139 
    140 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_85C503,
    141 	  sis85c503_init },
    142 
    143 	{ 0,			0,
    144 	  NULL },
    145 };
    146 
    147 const struct pciintr_icu_table *pciintr_icu_lookup __P((pcireg_t));
    148 
    149 const struct pciintr_icu_table *
    150 pciintr_icu_lookup(id)
    151 	pcireg_t id;
    152 {
    153 	const struct pciintr_icu_table *piit;
    154 
    155 	for (piit = pciintr_icu_table;
    156 	     piit->piit_init != NULL;
    157 	     piit++) {
    158 		if (PCI_VENDOR(id) == piit->piit_vendor &&
    159 		    PCI_PRODUCT(id) == piit->piit_product)
    160 			return (piit);
    161 	}
    162 
    163 	return (NULL);
    164 }
    165 
    166 struct pciintr_link_map *
    167 pciintr_link_lookup_pin(pir, pin)
    168 	struct pcibios_intr_routing *pir;
    169 	int pin;
    170 {
    171 
    172 	return (pciintr_link_lookup_link(pir->linkmap[pin].link));
    173 }
    174 
    175 struct pciintr_link_map *
    176 pciintr_link_lookup_link(link)
    177 	int link;
    178 {
    179 	struct pciintr_link_map *l;
    180 
    181 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    182 	     l = SIMPLEQ_NEXT(l, list)) {
    183 		if (l->link == link)
    184 			return (l);
    185 	}
    186 
    187 	return (NULL);
    188 }
    189 
    190 struct pciintr_link_map *
    191 pciintr_link_alloc(pir, pin)
    192 	struct pcibios_intr_routing *pir;
    193 	int pin;
    194 {
    195 	struct pciintr_link_map *l, *lstart;
    196 
    197 	l = malloc(sizeof(*l), M_DEVBUF, M_NOWAIT);
    198 	if (l == NULL)
    199 		panic("pciintr_link_alloc");
    200 
    201 	memset(l, 0, sizeof(*l));
    202 
    203 	l->link = pir->linkmap[pin].link;
    204 	l->bitmap = pir->linkmap[pin].bitmap;
    205 
    206 	lstart = SIMPLEQ_FIRST(&pciintr_link_map_list);
    207 	if (lstart == NULL || lstart->link < l->link)
    208 		SIMPLEQ_INSERT_TAIL(&pciintr_link_map_list, l, list);
    209 	else
    210 		SIMPLEQ_INSERT_HEAD(&pciintr_link_map_list, l, list);
    211 
    212 	return (l);
    213 }
    214 
    215 struct pcibios_intr_routing *
    216 pciintr_pir_lookup(bus, device)
    217 	int bus, device;
    218 {
    219 	struct pcibios_intr_routing *pir;
    220 	int entry;
    221 
    222 	if (pcibios_pir_table == NULL)
    223 		return (NULL);
    224 
    225 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
    226 		pir = &pcibios_pir_table[entry];
    227 		if (pir->bus == bus && ((pir->device >> 3) & 0x1f) == device)
    228 			return (pir);
    229 	}
    230 
    231 	return (NULL);
    232 }
    233 
    234 int
    235 pciintr_link_init()
    236 {
    237 	int entry, pin, error, link, clink;
    238 	struct pcibios_intr_routing *pir;
    239 	struct pciintr_link_map *l;
    240 
    241 	if (pcibios_pir_table == NULL) {
    242 		/* No PIR table; can't do anything. */
    243 		printf("pciintr_link_init: no PIR table\n");
    244 		return (1);
    245 	}
    246 
    247 	error = 0;
    248 	SIMPLEQ_INIT(&pciintr_link_map_list);
    249 
    250 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
    251 		pir = &pcibios_pir_table[entry];
    252 		for (pin = 0; pin < 4; pin++) {
    253 			link = pir->linkmap[pin].link;
    254 			if (link == 0) {
    255 				/* No connection for this pin. */
    256 				continue;
    257 			}
    258 
    259 			/*
    260 			 * Check the link value by asking the ICU for
    261 			 * the canonical link value.
    262 			 */
    263 			if (pciintr_icu_getclink(pciintr_icu_tag,
    264 			    pciintr_icu_handle, link, &clink) != 0) {
    265 				/*
    266 				 * Table entry is bogus.
    267 				 */
    268 				printf("pciintr_link_init: bad table entry: "
    269 				    "bus %d device %d link 0x%02x\n",
    270 				    pir->bus, (pir->device >> 3 & 0x1f), link);
    271 				error = 1;
    272 				continue;
    273 			}
    274 
    275 			/*
    276 			 * Multiple devices may be wired to the same
    277 			 * interrupt; check to see if we've seen this
    278 			 * one already.  If not, allocate a new link
    279 			 * map entry and stuff it in the map.
    280 			 */
    281 			l = pciintr_link_lookup_pin(pir, pin);
    282 			if (l == NULL)
    283 				(void) pciintr_link_alloc(pir, pin);
    284 		}
    285 	}
    286 
    287 	return (error);
    288 }
    289 
    290 int
    291 pciintr_link_fixup()
    292 {
    293 	struct pciintr_link_map *l;
    294 	u_int16_t pciirq, bitmap;
    295 	int i, j, cnt, irq;
    296 
    297 	/*
    298 	 * First stage: Attempt to connect PIRQs which aren't
    299 	 * yet connected.
    300 	 */
    301 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    302 	     l = SIMPLEQ_NEXT(l, list)) {
    303 		/*
    304 		 * Get the canonical link value for this entry.
    305 		 */
    306 		if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
    307 		    l->link, &l->clink) != 0) {
    308 			/*
    309 			 * ICU doesn't understand this link value.
    310 			 */
    311 #ifdef PCIINTR_DEBUG
    312 			printf("pciintr_link_fixup: link 0x%02x invalid\n",
    313 			    l->link);
    314 #endif
    315 			l->clink = -1;
    316 			continue;
    317 		}
    318 
    319 		/*
    320 		 * Determine if this PIRQ is mapped to an IRQ.
    321 		 */
    322 		if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
    323 		    l->clink, &irq) != 0) {
    324 			/*
    325 			 * ICU doesn't understand this PIRQ value.
    326 			 */
    327 			l->clink = -1;
    328 #ifdef PCIINTR_DEBUG
    329 			printf("pciintr_link_fixup: PIRQ %d invalid\n",
    330 			    l->clink);
    331 #endif
    332 			continue;
    333 		}
    334 
    335 		if (irq == 0xff) {
    336 			/*
    337 			 * Interrupt isn't connected.  Attempt to assign
    338 			 * it to an IRQ.
    339 			 */
    340 #ifdef PCIINTR_DEBUG
    341 			printf("pciintr_link_fixup: PIRQ %d not connected",
    342 			    l->clink);
    343 #endif
    344 			bitmap = l->bitmap;
    345 			for (i = 0, j = 0xff, cnt = 0; i < 16; i++)
    346 				if (bitmap & (1 << i))
    347 					j = i, cnt++;
    348 			/*
    349 			 * Just do the easy case now; we'll defer the
    350 			 * harder ones to Stage 2.
    351 			 */
    352 			if (cnt == 1) {
    353 				l->irq = j;
    354 				l->old_irq = irq;
    355 				l->fixup_stage = 1;
    356 				pciirq |= 1 << j;
    357 #ifdef PCIINTR_DEBUG
    358 				printf(", assigning IRQ %d", l->irq);
    359 #endif
    360 			}
    361 #ifdef PCIINTR_DEBUG
    362 			printf("\n");
    363 #endif
    364 		} else {
    365 			/*
    366 			 * Interrupt is already connected.  Don't do
    367 			 * anything to it.
    368 			 */
    369 			l->irq = irq;
    370 			pciirq |= 1 << irq;
    371 #ifdef PCIINTR_DEBUG
    372 			printf("pciintr_link_fixup: PIRQ %d already connected "
    373 			    "to IRQ %d\n", l->clink, l->irq);
    374 #endif
    375 		}
    376 	}
    377 
    378 	/*
    379 	 * Stage 2: Attempt to connect PIRQs which we didn't
    380 	 * connect in Stage 1.
    381 	 */
    382 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    383 	     l = SIMPLEQ_NEXT(l, list)) {
    384 		if (l->irq == 0) {
    385 			bitmap = l->bitmap;
    386 			for (i = 0; i < 16; i++) {
    387 				if ((pciirq & (1 << i)) != 0 &&
    388 				    (bitmap & (1 << i)) != 0) {
    389 					/*
    390 					 * This IRQ is a valid PCI
    391 					 * IRQ already connected to
    392 					 * another PIRQ, and also an
    393 					 * IRQ our PIRQ can use; connect
    394 					 * it up!
    395 					 */
    396 					l->irq = i;
    397 					l->old_irq = 0xff;
    398 					l->fixup_stage = 2;
    399 #ifdef PCIINTR_DEBUG
    400 					printf("pciintr_link_fixup: assigning "
    401 					    "IRQ %d to PIRQ %d\n", l->irq,
    402 					    l->clink);
    403 #endif
    404 					break;
    405 				}
    406 			}
    407 		}
    408 	}
    409 
    410 	/*
    411 	 * Stage 3: Allow the user to specify interrupt routing
    412 	 * information, overriding what we've done above.
    413 	 */
    414 	/* XXX Not implemented. */
    415 
    416 	return (0);
    417 }
    418 
    419 int
    420 pciintr_link_route(pciirq)
    421 	u_int16_t *pciirq;
    422 {
    423 	struct pciintr_link_map *l;
    424 	int rv = 0;
    425 
    426 	*pciirq = 0;
    427 
    428 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    429 	     l = SIMPLEQ_NEXT(l, list)) {
    430 		if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
    431 					 l->clink, l->irq) != 0 ||
    432 		    pciintr_icu_set_trigger(pciintr_icu_tag, pciintr_icu_handle,
    433 					    l->irq, IST_LEVEL) != 0) {
    434 			printf("pciintr_link_route: route of PIRQ %d -> IRQ %d"
    435 			    " failed\n", l->clink, l->irq);
    436 			rv = 1;
    437 		} else {
    438 			/*
    439 			 * Succssfully routed interrupt.  Mark this as
    440 			 * a PCI interrupt.
    441 			 */
    442 			*pciirq |= (1 << l->irq);
    443 		}
    444 	}
    445 
    446 	return (rv);
    447 }
    448 
    449 int
    450 pciintr_irq_release(pciirq)
    451 	u_int16_t *pciirq;
    452 {
    453 	int i;
    454 
    455 	for (i = 0; i < 16; i++) {
    456 		if ((*pciirq & (1 << i)) == 0)
    457 			(void) pciintr_icu_set_trigger(pciintr_icu_tag,
    458 			    pciintr_icu_handle, i, IST_EDGE);
    459 	}
    460 
    461 	return (0);
    462 }
    463 
    464 int
    465 pciintr_header_fixup(pc)
    466 	pci_chipset_tag_t pc;
    467 {
    468 	const struct pci_quirkdata *qd;
    469 	struct pcibios_intr_routing *pir;
    470 	struct pciintr_link_map *l;
    471 	int pin, bus, device, function, maxdevs, nfuncs, irq, link;
    472 	pcireg_t id, bhlcr, intr;
    473 	pcitag_t tag;
    474 
    475 #ifdef PCIBIOSVERBOSE
    476 	printf("--------------------------------------------\n");
    477 	printf("  device vendor product pin PIRQ   IRQ stage\n");
    478 	printf("--------------------------------------------\n");
    479 #endif
    480 
    481 	for (bus = 0; bus <= pcibios_max_bus; bus++) {
    482 		maxdevs = pci_bus_maxdevs(pc, bus);
    483 		for (device = 0; device < maxdevs; device++) {
    484 			tag = pci_make_tag(pc, bus, device, 0);
    485 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    486 
    487 			/* Invalid vendor ID value? */
    488 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    489 				continue;
    490 			/* XXX Not invalid, but we've done this ~forever. */
    491 			if (PCI_VENDOR(id) == 0)
    492 				continue;
    493 
    494 			qd = pci_lookup_quirkdata(PCI_VENDOR(id),
    495 			    PCI_PRODUCT(id));
    496 
    497 			bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    498 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    499 			    (qd != NULL &&
    500 			     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    501 				nfuncs = 8;
    502 			else
    503 				nfuncs = 1;
    504 
    505 			for (function = 0; function < nfuncs; function++) {
    506 				tag = pci_make_tag(pc, bus, device, function);
    507 				id = pci_conf_read(pc, tag, PCI_ID_REG);
    508 				intr = pci_conf_read(pc, tag,
    509 				    PCI_INTERRUPT_REG);
    510 
    511 				/* Invalid vendor ID value? */
    512 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    513 					continue;
    514 				/*
    515 				 * XXX Not invalid, but we've done this
    516 				 * ~forever.
    517 				 */
    518 				if (PCI_VENDOR(id) == 0)
    519 					continue;
    520 
    521 				pin = PCI_INTERRUPT_PIN(intr);
    522 				irq = PCI_INTERRUPT_LINE(intr);
    523 
    524 				if (pin == 0) {
    525 					/*
    526 					 * No interrupt used.
    527 					 */
    528 					continue;
    529 				}
    530 
    531 				pir = pciintr_pir_lookup(bus, device);
    532 				if (pir == NULL ||
    533 				    (link = pir->linkmap[pin - 1].link) == 0) {
    534 					/*
    535 					 * Interrupt not connected; no
    536 					 * need to change.
    537 					 */
    538 					continue;
    539 				}
    540 
    541 				l = pciintr_link_lookup_link(link);
    542 				if (l == NULL) {
    543 					/*
    544 					 * No link map entry?!
    545 					 */
    546 					printf("pciintr_header_fixup: no entry "
    547 					    "for link 0x%02x (%d:%d:%d:%c)\n",
    548 					    link, bus, device, function,
    549 					    '@' + pin);
    550 					continue;
    551 				}
    552 
    553 				/*
    554 				 * IRQs 14 and 15 are reserved for
    555 				 * PCI IDE interrupts; don't muck
    556 				 * with them.
    557 				 */
    558 				if (irq == 14 || irq == 15)
    559 					continue;
    560 
    561 #ifdef PCIBIOSVERBOSE
    562 				printf("%03d:%02d:%d 0x%04x 0x%04x  %c   "
    563 				    "0x%02x   %02d  %d\n",
    564 				    bus, device, function,
    565 				    PCI_VENDOR(id), PCI_PRODUCT(id),
    566 				    '@' + pin, l->clink, l->irq,
    567 				    l->fixup_stage);
    568 #endif
    569 
    570 				intr &= ~(PCI_INTERRUPT_LINE_MASK <<
    571 				    PCI_INTERRUPT_LINE_SHIFT);
    572 				intr |= (l->irq << PCI_INTERRUPT_LINE_SHIFT);
    573 				pci_conf_write(pc, tag, PCI_INTERRUPT_REG,
    574 				    intr);
    575 			}
    576 		}
    577 	}
    578 
    579 #ifdef PCIBIOSVERBOSE
    580 	printf("--------------------------------------------\n");
    581 #endif
    582 
    583 	return (0);
    584 }
    585 
    586 int
    587 pci_intr_fixup(pc, iot, pciirq)
    588 	pci_chipset_tag_t pc;
    589 	bus_space_tag_t iot;
    590 	u_int16_t *pciirq;
    591 {
    592 	const struct pciintr_icu_table *piit = NULL;
    593 	pcitag_t icutag;
    594 	pcireg_t icuid;
    595 
    596 	/*
    597 	 * Attempt to initialize our PCI interrupt router.  If
    598 	 * the PIR Table is present in ROM, use the location
    599 	 * specified by the PIR Table, and use the compat ID,
    600 	 * if present.  Otherwise, we have to look for the router
    601 	 * ourselves (the PCI-ISA bridge).
    602 	 */
    603 	if (pcibios_pir_header.signature != 0) {
    604 		icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
    605 		    (pcibios_pir_header.router_devfunc >> 3) & 0x1f,
    606 		    pcibios_pir_header.router_devfunc & 7);
    607 		icuid = pcibios_pir_header.compat_router;
    608 		if (icuid == 0 ||
    609 		    (piit = pciintr_icu_lookup(icuid)) == NULL) {
    610 			/*
    611 			 * No compat ID, or don't know the compat ID?  Read
    612 			 * it from the configuration header.
    613 			 */
    614 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    615 		}
    616 		if (piit == NULL)
    617 			piit = pciintr_icu_lookup(icuid);
    618 	} else {
    619 		int device, maxdevs = pci_bus_maxdevs(pc, 0);
    620 
    621 		/*
    622 		 * Search configuration space for a known interrupt
    623 		 * router.
    624 		 */
    625 		for (device = 0; device < maxdevs; device++) {
    626 			icutag = pci_make_tag(pc, 0, device, 0);
    627 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    628 
    629 			/* Invalid vendor ID value? */
    630 			if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
    631 				continue;
    632 			/* XXX Not invalid, but we've done this ~forever. */
    633 			if (PCI_VENDOR(icuid) == 0)
    634 				continue;
    635 
    636 			piit = pciintr_icu_lookup(icuid);
    637 			if (piit != NULL)
    638 				break;
    639 		}
    640 	}
    641 
    642 	if (piit == NULL) {
    643 		printf("pci_intr_fixup: no compatible PCI ICU found\n");
    644 		return (-1);		/* non-fatal */
    645 	}
    646 
    647 	/*
    648 	 * Initialize the PCI ICU.
    649 	 */
    650 	if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
    651 	    &pciintr_icu_handle) != 0)
    652 		return (-1);		/* non-fatal */
    653 
    654 	/*
    655 	 * Initialize the PCI interrupt link map.
    656 	 */
    657 	if (pciintr_link_init())
    658 		return (-1);		/* non-fatal */
    659 
    660 	/*
    661 	 * Fix up the link->IRQ mappings.
    662 	 */
    663 	if (pciintr_link_fixup() != 0)
    664 		return (-1);		/* non-fatal */
    665 
    666 	/*
    667 	 * Now actually program the PCI ICU with the new
    668 	 * routing information.
    669 	 */
    670 	if (pciintr_link_route(pciirq) != 0)
    671 		return (1);		/* fatal */
    672 
    673 	/*
    674 	 * Now that we've routed all of the PIRQs, rewrite the PCI
    675 	 * configuration headers to reflect the new mapping.
    676 	 */
    677 	if (pciintr_header_fixup(pc) != 0)
    678 		return (1);		/* fatal */
    679 
    680 	/*
    681 	 * Free any unused PCI IRQs for ISA devices.
    682 	 */
    683 	if (pciintr_irq_release(pciirq) != 0)
    684 		return (-1);		/* non-fatal */
    685 
    686 	/*
    687 	 * All done!
    688 	 */
    689 	return (0);			/* success! */
    690 }
    691