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