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