Home | History | Annotate | Line # | Download | only in pci
pci_intr_fixup.c revision 1.3.2.1
      1 /*	$NetBSD: pci_intr_fixup.c,v 1.3.2.1 1999/12/27 18:32:26 wrstuden 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.  Just ignore it.
    267 				 */
    268 #ifdef PCIINTR_DEBUG
    269 				printf("pciintr_link_init: bad table entry: "
    270 				    "bus %d device %d link 0x%02x\n",
    271 				    pir->bus, (pir->device >> 3 & 0x1f), link);
    272 #endif
    273 				continue;
    274 			}
    275 
    276 			/*
    277 			 * Multiple devices may be wired to the same
    278 			 * interrupt; check to see if we've seen this
    279 			 * one already.  If not, allocate a new link
    280 			 * map entry and stuff it in the map.
    281 			 */
    282 			l = pciintr_link_lookup_pin(pir, pin);
    283 			if (l == NULL)
    284 				(void) pciintr_link_alloc(pir, pin);
    285 		}
    286 	}
    287 
    288 	return (error);
    289 }
    290 
    291 int
    292 pciintr_link_fixup()
    293 {
    294 	struct pciintr_link_map *l;
    295 	u_int16_t pciirq, bitmap;
    296 	int i, j, cnt, irq;
    297 
    298 	/*
    299 	 * First stage: Attempt to connect PIRQs which aren't
    300 	 * yet connected.
    301 	 */
    302 	pciirq = 0;
    303 
    304 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    305 	     l = SIMPLEQ_NEXT(l, list)) {
    306 		/*
    307 		 * Get the canonical link value for this entry.
    308 		 */
    309 		if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
    310 		    l->link, &l->clink) != 0) {
    311 			/*
    312 			 * ICU doesn't understand this link value.
    313 			 */
    314 #ifdef PCIINTR_DEBUG
    315 			printf("pciintr_link_fixup: link 0x%02x invalid\n",
    316 			    l->link);
    317 #endif
    318 			l->clink = -1;
    319 			continue;
    320 		}
    321 
    322 		/*
    323 		 * Determine if this PIRQ is mapped to an IRQ.
    324 		 */
    325 		if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
    326 		    l->clink, &irq) != 0) {
    327 			/*
    328 			 * ICU doesn't understand this PIRQ value.
    329 			 */
    330 			l->clink = -1;
    331 #ifdef PCIINTR_DEBUG
    332 			printf("pciintr_link_fixup: PIRQ %d invalid\n",
    333 			    l->clink);
    334 #endif
    335 			continue;
    336 		}
    337 
    338 		if (irq == 0xff) {
    339 			/*
    340 			 * Interrupt isn't connected.  Attempt to assign
    341 			 * it to an IRQ.
    342 			 */
    343 #ifdef PCIINTR_DEBUG
    344 			printf("pciintr_link_fixup: PIRQ %d not connected",
    345 			    l->clink);
    346 #endif
    347 			bitmap = l->bitmap;
    348 			for (i = 0, j = 0xff, cnt = 0; i < 16; i++)
    349 				if (bitmap & (1 << i))
    350 					j = i, cnt++;
    351 			/*
    352 			 * Just do the easy case now; we'll defer the
    353 			 * harder ones to Stage 2.
    354 			 */
    355 			if (cnt == 1) {
    356 				l->irq = j;
    357 				l->old_irq = irq;
    358 				l->fixup_stage = 1;
    359 				pciirq |= 1 << j;
    360 #ifdef PCIINTR_DEBUG
    361 				printf(", assigning IRQ %d", l->irq);
    362 #endif
    363 			}
    364 #ifdef PCIINTR_DEBUG
    365 			printf("\n");
    366 #endif
    367 		} else {
    368 			/*
    369 			 * Interrupt is already connected.  Don't do
    370 			 * anything to it.
    371 			 */
    372 			l->irq = irq;
    373 			pciirq |= 1 << irq;
    374 #ifdef PCIINTR_DEBUG
    375 			printf("pciintr_link_fixup: PIRQ %d already connected "
    376 			    "to IRQ %d\n", l->clink, l->irq);
    377 #endif
    378 		}
    379 	}
    380 
    381 	/*
    382 	 * Stage 2: Attempt to connect PIRQs which we didn't
    383 	 * connect in Stage 1.
    384 	 */
    385 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    386 	     l = SIMPLEQ_NEXT(l, list)) {
    387 		if (l->irq == 0) {
    388 			bitmap = l->bitmap;
    389 			for (i = 0; i < 16; i++) {
    390 				if ((pciirq & (1 << i)) != 0 &&
    391 				    (bitmap & (1 << i)) != 0) {
    392 					/*
    393 					 * This IRQ is a valid PCI
    394 					 * IRQ already connected to
    395 					 * another PIRQ, and also an
    396 					 * IRQ our PIRQ can use; connect
    397 					 * it up!
    398 					 */
    399 					l->irq = i;
    400 					l->old_irq = 0xff;
    401 					l->fixup_stage = 2;
    402 #ifdef PCIINTR_DEBUG
    403 					printf("pciintr_link_fixup: assigning "
    404 					    "IRQ %d to PIRQ %d\n", l->irq,
    405 					    l->clink);
    406 #endif
    407 					break;
    408 				}
    409 			}
    410 		}
    411 	}
    412 
    413 	/*
    414 	 * Stage 3: Allow the user to specify interrupt routing
    415 	 * information, overriding what we've done above.
    416 	 */
    417 	/* XXX Not implemented. */
    418 
    419 	return (0);
    420 }
    421 
    422 int
    423 pciintr_link_route(pciirq)
    424 	u_int16_t *pciirq;
    425 {
    426 	struct pciintr_link_map *l;
    427 	int rv = 0;
    428 
    429 	*pciirq = 0;
    430 
    431 	for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
    432 	     l = SIMPLEQ_NEXT(l, list)) {
    433 		if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
    434 					 l->clink, l->irq) != 0 ||
    435 		    pciintr_icu_set_trigger(pciintr_icu_tag, pciintr_icu_handle,
    436 					    l->irq, IST_LEVEL) != 0) {
    437 			printf("pciintr_link_route: route of PIRQ %d -> IRQ %d"
    438 			    " failed\n", l->clink, l->irq);
    439 			rv = 1;
    440 		} else {
    441 			/*
    442 			 * Succssfully routed interrupt.  Mark this as
    443 			 * a PCI interrupt.
    444 			 */
    445 			*pciirq |= (1 << l->irq);
    446 		}
    447 	}
    448 
    449 	return (rv);
    450 }
    451 
    452 int
    453 pciintr_irq_release(pciirq)
    454 	u_int16_t *pciirq;
    455 {
    456 	int i;
    457 
    458 	for (i = 0; i < 16; i++) {
    459 		if ((*pciirq & (1 << i)) == 0)
    460 			(void) pciintr_icu_set_trigger(pciintr_icu_tag,
    461 			    pciintr_icu_handle, i, IST_EDGE);
    462 	}
    463 
    464 	return (0);
    465 }
    466 
    467 int
    468 pciintr_header_fixup(pc)
    469 	pci_chipset_tag_t pc;
    470 {
    471 	const struct pci_quirkdata *qd;
    472 	struct pcibios_intr_routing *pir;
    473 	struct pciintr_link_map *l;
    474 	int pin, bus, device, function, maxdevs, nfuncs, irq, link;
    475 	pcireg_t id, bhlcr, intr;
    476 	pcitag_t tag;
    477 
    478 #ifdef PCIBIOSVERBOSE
    479 	printf("--------------------------------------------\n");
    480 	printf("  device vendor product pin PIRQ   IRQ stage\n");
    481 	printf("--------------------------------------------\n");
    482 #endif
    483 
    484 	for (bus = 0; bus <= pcibios_max_bus; bus++) {
    485 		maxdevs = pci_bus_maxdevs(pc, bus);
    486 		for (device = 0; device < maxdevs; device++) {
    487 			tag = pci_make_tag(pc, bus, device, 0);
    488 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    489 
    490 			/* Invalid vendor ID value? */
    491 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    492 				continue;
    493 			/* XXX Not invalid, but we've done this ~forever. */
    494 			if (PCI_VENDOR(id) == 0)
    495 				continue;
    496 
    497 			qd = pci_lookup_quirkdata(PCI_VENDOR(id),
    498 			    PCI_PRODUCT(id));
    499 
    500 			bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    501 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    502 			    (qd != NULL &&
    503 			     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    504 				nfuncs = 8;
    505 			else
    506 				nfuncs = 1;
    507 
    508 			for (function = 0; function < nfuncs; function++) {
    509 				tag = pci_make_tag(pc, bus, device, function);
    510 				id = pci_conf_read(pc, tag, PCI_ID_REG);
    511 				intr = pci_conf_read(pc, tag,
    512 				    PCI_INTERRUPT_REG);
    513 
    514 				/* Invalid vendor ID value? */
    515 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    516 					continue;
    517 				/*
    518 				 * XXX Not invalid, but we've done this
    519 				 * ~forever.
    520 				 */
    521 				if (PCI_VENDOR(id) == 0)
    522 					continue;
    523 
    524 				pin = PCI_INTERRUPT_PIN(intr);
    525 				irq = PCI_INTERRUPT_LINE(intr);
    526 
    527 				if (pin == 0) {
    528 					/*
    529 					 * No interrupt used.
    530 					 */
    531 					continue;
    532 				}
    533 
    534 				pir = pciintr_pir_lookup(bus, device);
    535 				if (pir == NULL ||
    536 				    (link = pir->linkmap[pin - 1].link) == 0) {
    537 					/*
    538 					 * Interrupt not connected; no
    539 					 * need to change.
    540 					 */
    541 					continue;
    542 				}
    543 
    544 				l = pciintr_link_lookup_link(link);
    545 				if (l == NULL) {
    546 					/*
    547 					 * No link map entry?!
    548 					 */
    549 					printf("pciintr_header_fixup: no entry "
    550 					    "for link 0x%02x (%d:%d:%d:%c)\n",
    551 					    link, bus, device, function,
    552 					    '@' + pin);
    553 					continue;
    554 				}
    555 
    556 				/*
    557 				 * IRQs 14 and 15 are reserved for
    558 				 * PCI IDE interrupts; don't muck
    559 				 * with them.
    560 				 */
    561 				if (irq == 14 || irq == 15)
    562 					continue;
    563 
    564 #ifdef PCIBIOSVERBOSE
    565 				printf("%03d:%02d:%d 0x%04x 0x%04x  %c   "
    566 				    "0x%02x   %02d  %d\n",
    567 				    bus, device, function,
    568 				    PCI_VENDOR(id), PCI_PRODUCT(id),
    569 				    '@' + pin, l->clink, l->irq,
    570 				    l->fixup_stage);
    571 #endif
    572 
    573 				intr &= ~(PCI_INTERRUPT_LINE_MASK <<
    574 				    PCI_INTERRUPT_LINE_SHIFT);
    575 				intr |= (l->irq << PCI_INTERRUPT_LINE_SHIFT);
    576 				pci_conf_write(pc, tag, PCI_INTERRUPT_REG,
    577 				    intr);
    578 			}
    579 		}
    580 	}
    581 
    582 #ifdef PCIBIOSVERBOSE
    583 	printf("--------------------------------------------\n");
    584 #endif
    585 
    586 	return (0);
    587 }
    588 
    589 int
    590 pci_intr_fixup(pc, iot, pciirq)
    591 	pci_chipset_tag_t pc;
    592 	bus_space_tag_t iot;
    593 	u_int16_t *pciirq;
    594 {
    595 	const struct pciintr_icu_table *piit = NULL;
    596 	pcitag_t icutag;
    597 	pcireg_t icuid;
    598 
    599 	/*
    600 	 * Attempt to initialize our PCI interrupt router.  If
    601 	 * the PIR Table is present in ROM, use the location
    602 	 * specified by the PIR Table, and use the compat ID,
    603 	 * if present.  Otherwise, we have to look for the router
    604 	 * ourselves (the PCI-ISA bridge).
    605 	 */
    606 	if (pcibios_pir_header.signature != 0) {
    607 		icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
    608 		    (pcibios_pir_header.router_devfunc >> 3) & 0x1f,
    609 		    pcibios_pir_header.router_devfunc & 7);
    610 		icuid = pcibios_pir_header.compat_router;
    611 		if (icuid == 0 ||
    612 		    (piit = pciintr_icu_lookup(icuid)) == NULL) {
    613 			/*
    614 			 * No compat ID, or don't know the compat ID?  Read
    615 			 * it from the configuration header.
    616 			 */
    617 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    618 		}
    619 		if (piit == NULL)
    620 			piit = pciintr_icu_lookup(icuid);
    621 	} else {
    622 		int device, maxdevs = pci_bus_maxdevs(pc, 0);
    623 
    624 		/*
    625 		 * Search configuration space for a known interrupt
    626 		 * router.
    627 		 */
    628 		for (device = 0; device < maxdevs; device++) {
    629 			icutag = pci_make_tag(pc, 0, device, 0);
    630 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    631 
    632 			/* Invalid vendor ID value? */
    633 			if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
    634 				continue;
    635 			/* XXX Not invalid, but we've done this ~forever. */
    636 			if (PCI_VENDOR(icuid) == 0)
    637 				continue;
    638 
    639 			piit = pciintr_icu_lookup(icuid);
    640 			if (piit != NULL)
    641 				break;
    642 		}
    643 	}
    644 
    645 	if (piit == NULL) {
    646 		printf("pci_intr_fixup: no compatible PCI ICU found\n");
    647 		return (-1);		/* non-fatal */
    648 	}
    649 
    650 	/*
    651 	 * Initialize the PCI ICU.
    652 	 */
    653 	if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
    654 	    &pciintr_icu_handle) != 0)
    655 		return (-1);		/* non-fatal */
    656 
    657 	/*
    658 	 * Initialize the PCI interrupt link map.
    659 	 */
    660 	if (pciintr_link_init())
    661 		return (-1);		/* non-fatal */
    662 
    663 	/*
    664 	 * Fix up the link->IRQ mappings.
    665 	 */
    666 	if (pciintr_link_fixup() != 0)
    667 		return (-1);		/* non-fatal */
    668 
    669 	/*
    670 	 * Now actually program the PCI ICU with the new
    671 	 * routing information.
    672 	 */
    673 	if (pciintr_link_route(pciirq) != 0)
    674 		return (1);		/* fatal */
    675 
    676 	/*
    677 	 * Now that we've routed all of the PIRQs, rewrite the PCI
    678 	 * configuration headers to reflect the new mapping.
    679 	 */
    680 	if (pciintr_header_fixup(pc) != 0)
    681 		return (1);		/* fatal */
    682 
    683 	/*
    684 	 * Free any unused PCI IRQs for ISA devices.
    685 	 */
    686 	if (pciintr_irq_release(pciirq) != 0)
    687 		return (-1);		/* non-fatal */
    688 
    689 	/*
    690 	 * All done!
    691 	 */
    692 	return (0);			/* success! */
    693 }
    694