Deleted Added
sdiff udiff text old ( 267992 ) new ( 282274 )
full compact
1/*-
2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
5 * Copyright (c) 2004, John Baldwin <jhb@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
13 * disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/i386/pci/pci_pir.c 267992 2014-06-28 03:56:17Z hselasky $");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/bus.h>
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
39#include <sys/sysctl.h>
40#include <vm/vm.h>
41#include <vm/pmap.h>
42#include <vm/vm_param.h>
43#include <machine/md_var.h>
44#include <dev/pci/pcivar.h>
45#include <dev/pci/pcireg.h>
46#include <machine/pci_cfgreg.h>
47#include <machine/segments.h>
48#include <machine/pc/bios.h>
49
50#define NUM_ISA_INTERRUPTS 16
51
52/*
53 * A link device. Loosely based on the ACPI PCI link device. This doesn't
54 * try to support priorities for different ISA interrupts.
55 */
56struct pci_link {
57 TAILQ_ENTRY(pci_link) pl_links;
58 uint8_t pl_id;
59 uint8_t pl_irq;
60 uint16_t pl_irqmask;
61 int pl_references;
62 int pl_routed;
63};
64
65struct pci_link_lookup {
66 struct pci_link **pci_link_ptr;
67 int bus;
68 int device;
69 int pin;
70};
71
72struct pci_dev_lookup {
73 uint8_t link;
74 int bus;
75 int device;
76 int pin;
77};
78
79typedef void pir_entry_handler(struct PIR_entry *entry,
80 struct PIR_intpin* intpin, void *arg);
81
82static void pci_print_irqmask(u_int16_t irqs);
83static int pci_pir_biosroute(int bus, int device, int func, int pin,
84 int irq);
85static int pci_pir_choose_irq(struct pci_link *pci_link, int irqmask);
86static void pci_pir_create_links(struct PIR_entry *entry,
87 struct PIR_intpin *intpin, void *arg);
88static void pci_pir_dump_links(void);
89static struct pci_link *pci_pir_find_link(uint8_t link_id);
90static void pci_pir_find_link_handler(struct PIR_entry *entry,
91 struct PIR_intpin *intpin, void *arg);
92static void pci_pir_initial_irqs(struct PIR_entry *entry,
93 struct PIR_intpin *intpin, void *arg);
94static void pci_pir_parse(void);
95static uint8_t pci_pir_search_irq(int bus, int device, int pin);
96static int pci_pir_valid_irq(struct pci_link *pci_link, int irq);
97static void pci_pir_walk_table(pir_entry_handler *handler, void *arg);
98
99static MALLOC_DEFINE(M_PIR, "$PIR", "$PIR structures");
100
101static struct PIR_table *pci_route_table;
102static device_t pir_device;
103static int pci_route_count, pir_bios_irqs, pir_parsed;
104static TAILQ_HEAD(, pci_link) pci_links;
105static int pir_interrupt_weight[NUM_ISA_INTERRUPTS];
106
107/* sysctl vars */
108SYSCTL_DECL(_hw_pci);
109
110/* XXX this likely should live in a header file */
111#ifdef PC98
112/* IRQs 3, 5, 7, 9, 10, 11, 12, 13 */
113#define PCI_IRQ_OVERRIDE_MASK 0x3e68
114#else
115/* IRQs 3, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15 */
116#define PCI_IRQ_OVERRIDE_MASK 0xdef8
117#endif
118
119static uint32_t pci_irq_override_mask = PCI_IRQ_OVERRIDE_MASK;
120SYSCTL_INT(_hw_pci, OID_AUTO, irq_override_mask, CTLFLAG_RDTUN,
121 &pci_irq_override_mask, PCI_IRQ_OVERRIDE_MASK,
122 "Mask of allowed irqs to try to route when it has no good clue about\n"
123 "which irqs it should use.");
124
125/*
126 * Look for the interrupt routing table.
127 *
128 * We use PCI BIOS's PIR table if it's available. $PIR is the standard way
129 * to do this. Sadly, some machines are not standards conforming and have
130 * _PIR instead. We shrug and cope by looking for both.
131 */
132void
133pci_pir_open(void)
134{
135 struct PIR_table *pt;
136 uint32_t sigaddr;
137 int i;
138 uint8_t ck, *cv;
139
140#ifdef XEN
141 return;
142#else
143 /* Don't try if we've already found a table. */
144 if (pci_route_table != NULL)
145 return;
146
147 /* Look for $PIR and then _PIR. */
148 sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0);
149 if (sigaddr == 0)
150 sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0);
151 if (sigaddr == 0)
152 return;
153#endif
154 /* If we found something, check the checksum and length. */
155 /* XXX - Use pmap_mapdev()? */
156 pt = (struct PIR_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
157 if (pt->pt_header.ph_length <= sizeof(struct PIR_header))
158 return;
159 for (cv = (u_int8_t *)pt, ck = 0, i = 0;
160 i < (pt->pt_header.ph_length); i++)
161 ck += cv[i];
162 if (ck != 0)
163 return;
164
165 /* Ok, we've got a valid table. */
166 pci_route_table = pt;
167 pci_route_count = (pt->pt_header.ph_length -
168 sizeof(struct PIR_header)) /
169 sizeof(struct PIR_entry);
170}
171
172/*
173 * Find the pci_link structure for a given link ID.
174 */
175static struct pci_link *
176pci_pir_find_link(uint8_t link_id)
177{
178 struct pci_link *pci_link;
179
180 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
181 if (pci_link->pl_id == link_id)
182 return (pci_link);
183 }
184 return (NULL);
185}
186
187/*
188 * Find the link device associated with a PCI device in the table.
189 */
190static void
191pci_pir_find_link_handler(struct PIR_entry *entry, struct PIR_intpin *intpin,
192 void *arg)
193{
194 struct pci_link_lookup *lookup;
195
196 lookup = (struct pci_link_lookup *)arg;
197 if (entry->pe_bus == lookup->bus &&
198 entry->pe_device == lookup->device &&
199 intpin - entry->pe_intpin == lookup->pin)
200 *lookup->pci_link_ptr = pci_pir_find_link(intpin->link);
201}
202
203/*
204 * Check to see if a possible IRQ setting is valid.
205 */
206static int
207pci_pir_valid_irq(struct pci_link *pci_link, int irq)
208{
209
210 if (!PCI_INTERRUPT_VALID(irq))
211 return (0);
212 return (pci_link->pl_irqmask & (1 << irq));
213}
214
215/*
216 * Walk the $PIR executing the worker function for each valid intpin entry
217 * in the table. The handler is passed a pointer to both the entry and
218 * the intpin in the entry.
219 */
220static void
221pci_pir_walk_table(pir_entry_handler *handler, void *arg)
222{
223 struct PIR_entry *entry;
224 struct PIR_intpin *intpin;
225 int i, pin;
226
227 entry = &pci_route_table->pt_entry[0];
228 for (i = 0; i < pci_route_count; i++, entry++) {
229 intpin = &entry->pe_intpin[0];
230 for (pin = 0; pin < 4; pin++, intpin++)
231 if (intpin->link != 0)
232 handler(entry, intpin, arg);
233 }
234}
235
236static void
237pci_pir_create_links(struct PIR_entry *entry, struct PIR_intpin *intpin,
238 void *arg)
239{
240 struct pci_link *pci_link;
241
242 pci_link = pci_pir_find_link(intpin->link);
243 if (pci_link != NULL) {
244 pci_link->pl_references++;
245 if (intpin->irqs != pci_link->pl_irqmask) {
246 if (bootverbose)
247 printf(
248 "$PIR: Entry %d.%d.INT%c has different mask for link %#x, merging\n",
249 entry->pe_bus, entry->pe_device,
250 (intpin - entry->pe_intpin) + 'A',
251 pci_link->pl_id);
252 pci_link->pl_irqmask &= intpin->irqs;
253 }
254 } else {
255 pci_link = malloc(sizeof(struct pci_link), M_PIR, M_WAITOK);
256 pci_link->pl_id = intpin->link;
257 pci_link->pl_irqmask = intpin->irqs;
258 pci_link->pl_irq = PCI_INVALID_IRQ;
259 pci_link->pl_references = 1;
260 pci_link->pl_routed = 0;
261 TAILQ_INSERT_TAIL(&pci_links, pci_link, pl_links);
262 }
263}
264
265/*
266 * Look to see if any of the function on the PCI device at bus/device have
267 * an interrupt routed to intpin 'pin' by the BIOS.
268 */
269static uint8_t
270pci_pir_search_irq(int bus, int device, int pin)
271{
272 uint32_t value;
273 uint8_t func, maxfunc;
274
275 /* See if we have a valid device at function 0. */
276 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
277 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
278 return (PCI_INVALID_IRQ);
279 if (value & PCIM_MFDEV)
280 maxfunc = PCI_FUNCMAX;
281 else
282 maxfunc = 0;
283
284 /* Scan all possible functions at this device. */
285 for (func = 0; func <= maxfunc; func++) {
286 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
287 if (value == 0xffffffff)
288 continue;
289 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
290
291 /*
292 * See if it uses the pin in question. Note that the passed
293 * in pin uses 0 for A, .. 3 for D whereas the intpin
294 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
295 */
296 if (value != pin + 1)
297 continue;
298 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
299 if (bootverbose)
300 printf(
301 "$PIR: Found matching pin for %d.%d.INT%c at func %d: %d\n",
302 bus, device, pin + 'A', func, value);
303 if (value != PCI_INVALID_IRQ)
304 return (value);
305 }
306 return (PCI_INVALID_IRQ);
307}
308
309/*
310 * Try to initialize IRQ based on this device's IRQ.
311 */
312static void
313pci_pir_initial_irqs(struct PIR_entry *entry, struct PIR_intpin *intpin,
314 void *arg)
315{
316 struct pci_link *pci_link;
317 uint8_t irq, pin;
318
319 pin = intpin - entry->pe_intpin;
320 pci_link = pci_pir_find_link(intpin->link);
321 irq = pci_pir_search_irq(entry->pe_bus, entry->pe_device, pin);
322 if (irq == PCI_INVALID_IRQ || irq == pci_link->pl_irq)
323 return;
324
325 /* Don't trust any BIOS IRQs greater than 15. */
326 if (irq >= NUM_ISA_INTERRUPTS) {
327 printf(
328 "$PIR: Ignoring invalid BIOS IRQ %d from %d.%d.INT%c for link %#x\n",
329 irq, entry->pe_bus, entry->pe_device, pin + 'A',
330 pci_link->pl_id);
331 return;
332 }
333
334 /*
335 * If we don't have an IRQ for this link yet, then we trust the
336 * BIOS, even if it seems invalid from the $PIR entries.
337 */
338 if (pci_link->pl_irq == PCI_INVALID_IRQ) {
339 if (!pci_pir_valid_irq(pci_link, irq))
340 printf(
341 "$PIR: Using invalid BIOS IRQ %d from %d.%d.INT%c for link %#x\n",
342 irq, entry->pe_bus, entry->pe_device, pin + 'A',
343 pci_link->pl_id);
344 pci_link->pl_irq = irq;
345 pci_link->pl_routed = 1;
346 return;
347 }
348
349 /*
350 * We have an IRQ and it doesn't match the current IRQ for this
351 * link. If the new IRQ is invalid, then warn about it and ignore
352 * it. If the old IRQ is invalid and the new IRQ is valid, then
353 * prefer the new IRQ instead. If both IRQs are valid, then just
354 * use the first one. Note that if we ever get into this situation
355 * we are having to guess which setting the BIOS actually routed.
356 * Perhaps we should just give up instead.
357 */
358 if (!pci_pir_valid_irq(pci_link, irq)) {
359 printf(
360 "$PIR: BIOS IRQ %d for %d.%d.INT%c is not valid for link %#x\n",
361 irq, entry->pe_bus, entry->pe_device, pin + 'A',
362 pci_link->pl_id);
363 } else if (!pci_pir_valid_irq(pci_link, pci_link->pl_irq)) {
364 printf(
365"$PIR: Preferring valid BIOS IRQ %d from %d.%d.INT%c for link %#x to IRQ %d\n",
366 irq, entry->pe_bus, entry->pe_device, pin + 'A',
367 pci_link->pl_id, pci_link->pl_irq);
368 pci_link->pl_irq = irq;
369 pci_link->pl_routed = 1;
370 } else
371 printf(
372 "$PIR: BIOS IRQ %d for %d.%d.INT%c does not match link %#x irq %d\n",
373 irq, entry->pe_bus, entry->pe_device, pin + 'A',
374 pci_link->pl_id, pci_link->pl_irq);
375}
376
377/*
378 * Parse $PIR to enumerate link devices and attempt to determine their
379 * initial state. This could perhaps be cleaner if we had drivers for the
380 * various interrupt routers as they could read the initial IRQ for each
381 * link.
382 */
383static void
384pci_pir_parse(void)
385{
386 char tunable_buffer[64];
387 struct pci_link *pci_link;
388 int i, irq;
389
390 /* Only parse once. */
391 if (pir_parsed)
392 return;
393 pir_parsed = 1;
394
395 /* Enumerate link devices. */
396 TAILQ_INIT(&pci_links);
397 pci_pir_walk_table(pci_pir_create_links, NULL);
398 if (bootverbose) {
399 printf("$PIR: Links after initial probe:\n");
400 pci_pir_dump_links();
401 }
402
403 /*
404 * Check to see if the BIOS has already routed any of the links by
405 * checking each device connected to each link to see if it has a
406 * valid IRQ.
407 */
408 pci_pir_walk_table(pci_pir_initial_irqs, NULL);
409 if (bootverbose) {
410 printf("$PIR: Links after initial IRQ discovery:\n");
411 pci_pir_dump_links();
412 }
413
414 /*
415 * Allow the user to override the IRQ for a given link device. We
416 * allow invalid IRQs to be specified but warn about them. An IRQ
417 * of 255 or 0 clears any preset IRQ.
418 */
419 i = 0;
420 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
421 snprintf(tunable_buffer, sizeof(tunable_buffer),
422 "hw.pci.link.%#x.irq", pci_link->pl_id);
423 if (getenv_int(tunable_buffer, &irq) == 0)
424 continue;
425 if (irq == 0)
426 irq = PCI_INVALID_IRQ;
427 if (irq != PCI_INVALID_IRQ &&
428 !pci_pir_valid_irq(pci_link, irq) && bootverbose)
429 printf(
430 "$PIR: Warning, IRQ %d for link %#x is not listed as valid\n",
431 irq, pci_link->pl_id);
432 pci_link->pl_routed = 0;
433 pci_link->pl_irq = irq;
434 i = 1;
435 }
436 if (bootverbose && i) {
437 printf("$PIR: Links after tunable overrides:\n");
438 pci_pir_dump_links();
439 }
440
441 /*
442 * Build initial interrupt weights as well as bitmap of "known-good"
443 * IRQs that the BIOS has already used for PCI link devices.
444 */
445 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
446 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq))
447 continue;
448 pir_bios_irqs |= 1 << pci_link->pl_irq;
449 pir_interrupt_weight[pci_link->pl_irq] +=
450 pci_link->pl_references;
451 }
452 if (bootverbose) {
453 printf("$PIR: IRQs used by BIOS: ");
454 pci_print_irqmask(pir_bios_irqs);
455 printf("\n");
456 printf("$PIR: Interrupt Weights:\n[ ");
457 for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
458 printf(" %3d", i);
459 printf(" ]\n[ ");
460 for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
461 printf(" %3d", pir_interrupt_weight[i]);
462 printf(" ]\n");
463 }
464}
465
466/*
467 * Use the PCI BIOS to route an interrupt for a given device.
468 *
469 * Input:
470 * AX = PCIBIOS_ROUTE_INTERRUPT
471 * BH = bus
472 * BL = device [7:3] / function [2:0]
473 * CH = IRQ
474 * CL = Interrupt Pin (0x0A = A, ... 0x0D = D)
475 */
476static int
477pci_pir_biosroute(int bus, int device, int func, int pin, int irq)
478{
479 struct bios_regs args;
480
481 args.eax = PCIBIOS_ROUTE_INTERRUPT;
482 args.ebx = (bus << 8) | (device << 3) | func;
483 args.ecx = (irq << 8) | (0xa + pin);
484#ifdef XEN
485 return (0);
486#else
487 return (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)));
488#endif
489}
490
491
492/*
493 * Route a PCI interrupt using a link device from the $PIR.
494 */
495int
496pci_pir_route_interrupt(int bus, int device, int func, int pin)
497{
498 struct pci_link_lookup lookup;
499 struct pci_link *pci_link;
500 int error, irq;
501
502 if (pci_route_table == NULL)
503 return (PCI_INVALID_IRQ);
504
505 /* Lookup link device for this PCI device/pin. */
506 pci_link = NULL;
507 lookup.bus = bus;
508 lookup.device = device;
509 lookup.pin = pin - 1;
510 lookup.pci_link_ptr = &pci_link;
511 pci_pir_walk_table(pci_pir_find_link_handler, &lookup);
512 if (pci_link == NULL) {
513 printf("$PIR: No matching entry for %d.%d.INT%c\n", bus,
514 device, pin - 1 + 'A');
515 return (PCI_INVALID_IRQ);
516 }
517
518 /*
519 * Pick a new interrupt if we don't have one already. We look
520 * for an interrupt from several different sets. First, if
521 * this link only has one valid IRQ, use that. Second, we
522 * check the set of PCI only interrupts from the $PIR. Third,
523 * we check the set of known-good interrupts that the BIOS has
524 * already used. Lastly, we check the "all possible valid
525 * IRQs" set.
526 */
527 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
528 if (pci_link->pl_irqmask != 0 && powerof2(pci_link->pl_irqmask))
529 irq = ffs(pci_link->pl_irqmask) - 1;
530 else
531 irq = pci_pir_choose_irq(pci_link,
532 pci_route_table->pt_header.ph_pci_irqs);
533 if (!PCI_INTERRUPT_VALID(irq))
534 irq = pci_pir_choose_irq(pci_link, pir_bios_irqs);
535 if (!PCI_INTERRUPT_VALID(irq))
536 irq = pci_pir_choose_irq(pci_link,
537 pci_irq_override_mask);
538 if (!PCI_INTERRUPT_VALID(irq)) {
539 if (bootverbose)
540 printf(
541 "$PIR: Failed to route interrupt for %d:%d INT%c\n",
542 bus, device, pin - 1 + 'A');
543 return (PCI_INVALID_IRQ);
544 }
545 pci_link->pl_irq = irq;
546 }
547
548 /* Ask the BIOS to route this IRQ if we haven't done so already. */
549 if (!pci_link->pl_routed) {
550 error = pci_pir_biosroute(bus, device, func, pin - 1,
551 pci_link->pl_irq);
552
553 /* Ignore errors when routing a unique interrupt. */
554 if (error && !powerof2(pci_link->pl_irqmask)) {
555 printf("$PIR: ROUTE_INTERRUPT failed.\n");
556 return (PCI_INVALID_IRQ);
557 }
558 pci_link->pl_routed = 1;
559
560 /* Ensure the interrupt is set to level/low trigger. */
561 KASSERT(pir_device != NULL, ("missing pir device"));
562 BUS_CONFIG_INTR(pir_device, pci_link->pl_irq,
563 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
564 }
565 if (bootverbose)
566 printf("$PIR: %d:%d INT%c routed to irq %d\n", bus, device,
567 pin - 1 + 'A', pci_link->pl_irq);
568 return (pci_link->pl_irq);
569}
570
571/*
572 * Try to pick an interrupt for the specified link from the interrupts
573 * set in the mask.
574 */
575static int
576pci_pir_choose_irq(struct pci_link *pci_link, int irqmask)
577{
578 int i, irq, realmask;
579
580 /* XXX: Need to have a #define of known bad IRQs to also mask out? */
581 realmask = pci_link->pl_irqmask & irqmask;
582 if (realmask == 0)
583 return (PCI_INVALID_IRQ);
584
585 /* Find IRQ with lowest weight. */
586 irq = PCI_INVALID_IRQ;
587 for (i = 0; i < NUM_ISA_INTERRUPTS; i++) {
588 if (!(realmask & 1 << i))
589 continue;
590 if (irq == PCI_INVALID_IRQ ||
591 pir_interrupt_weight[i] < pir_interrupt_weight[irq])
592 irq = i;
593 }
594 if (bootverbose && PCI_INTERRUPT_VALID(irq)) {
595 printf("$PIR: Found IRQ %d for link %#x from ", irq,
596 pci_link->pl_id);
597 pci_print_irqmask(realmask);
598 printf("\n");
599 }
600 return (irq);
601}
602
603static void
604pci_print_irqmask(u_int16_t irqs)
605{
606 int i, first;
607
608 if (irqs == 0) {
609 printf("none");
610 return;
611 }
612 first = 1;
613 for (i = 0; i < 16; i++, irqs >>= 1)
614 if (irqs & 1) {
615 if (!first)
616 printf(" ");
617 else
618 first = 0;
619 printf("%d", i);
620 }
621}
622
623/*
624 * Display link devices.
625 */
626static void
627pci_pir_dump_links(void)
628{
629 struct pci_link *pci_link;
630
631 printf("Link IRQ Rtd Ref IRQs\n");
632 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
633 printf("%#4x %3d %c %3d ", pci_link->pl_id,
634 pci_link->pl_irq, pci_link->pl_routed ? 'Y' : 'N',
635 pci_link->pl_references);
636 pci_print_irqmask(pci_link->pl_irqmask);
637 printf("\n");
638 }
639}
640
641/*
642 * See if any interrupts for a given PCI bus are routed in the PIR. Don't
643 * even bother looking if the BIOS doesn't support routing anyways. If we
644 * are probing a PCI-PCI bridge, then require_parse will be true and we should
645 * only succeed if a host-PCI bridge has already attached and parsed the PIR.
646 */
647int
648pci_pir_probe(int bus, int require_parse)
649{
650 int i;
651
652 if (pci_route_table == NULL || (require_parse && !pir_parsed))
653 return (0);
654 for (i = 0; i < pci_route_count; i++)
655 if (pci_route_table->pt_entry[i].pe_bus == bus)
656 return (1);
657 return (0);
658}
659
660/*
661 * The driver for the new-bus psuedo device pir0 for the $PIR table.
662 */
663
664static int
665pir_probe(device_t dev)
666{
667 char buf[64];
668
669 snprintf(buf, sizeof(buf), "PCI Interrupt Routing Table: %d Entries",
670 pci_route_count);
671 device_set_desc_copy(dev, buf);
672 return (0);
673}
674
675static int
676pir_attach(device_t dev)
677{
678
679 pci_pir_parse();
680 KASSERT(pir_device == NULL, ("Multiple pir devices"));
681 pir_device = dev;
682 return (0);
683}
684
685static void
686pir_resume_find_device(struct PIR_entry *entry, struct PIR_intpin *intpin,
687 void *arg)
688{
689 struct pci_dev_lookup *pd;
690
691 pd = (struct pci_dev_lookup *)arg;
692 if (intpin->link != pd->link || pd->bus != -1)
693 return;
694 pd->bus = entry->pe_bus;
695 pd->device = entry->pe_device;
696 pd->pin = intpin - entry->pe_intpin;
697}
698
699static int
700pir_resume(device_t dev)
701{
702 struct pci_dev_lookup pd;
703 struct pci_link *pci_link;
704 int error;
705
706 /* Ask the BIOS to re-route each link that was already routed. */
707 TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
708 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
709 KASSERT(!pci_link->pl_routed,
710 ("link %#x is routed but has invalid PCI IRQ",
711 pci_link->pl_id));
712 continue;
713 }
714 if (pci_link->pl_routed) {
715 pd.bus = -1;
716 pd.link = pci_link->pl_id;
717 pci_pir_walk_table(pir_resume_find_device, &pd);
718 KASSERT(pd.bus != -1,
719 ("did not find matching entry for link %#x in the $PIR table",
720 pci_link->pl_id));
721 if (bootverbose)
722 device_printf(dev,
723 "Using %d.%d.INT%c to route link %#x to IRQ %d\n",
724 pd.bus, pd.device, pd.pin + 'A',
725 pci_link->pl_id, pci_link->pl_irq);
726 error = pci_pir_biosroute(pd.bus, pd.device, 0, pd.pin,
727 pci_link->pl_irq);
728 if (error)
729 device_printf(dev,
730 "ROUTE_INTERRUPT on resume for link %#x failed.\n",
731 pci_link->pl_id);
732 }
733 }
734 return (0);
735}
736
737static device_method_t pir_methods[] = {
738 /* Device interface */
739 DEVMETHOD(device_probe, pir_probe),
740 DEVMETHOD(device_attach, pir_attach),
741 DEVMETHOD(device_resume, pir_resume),
742
743 { 0, 0 }
744};
745
746static driver_t pir_driver = {
747 "pir",
748 pir_methods,
749 1,
750};
751
752static devclass_t pir_devclass;
753
754DRIVER_MODULE(pir, legacy, pir_driver, pir_devclass, 0, 0);