xref: /freebsd/sys/i386/pci/pci_pir.c (revision c711aea6)
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$");
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  */
56 struct 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 
65 struct pci_link_lookup {
66 	struct pci_link	**pci_link_ptr;
67 	int		bus;
68 	int		device;
69 	int		pin;
70 };
71 
72 struct pci_dev_lookup {
73 	uint8_t		link;
74 	int		bus;
75 	int		device;
76 	int		pin;
77 };
78 
79 typedef void pir_entry_handler(struct PIR_entry *entry,
80     struct PIR_intpin* intpin, void *arg);
81 
82 static void	pci_print_irqmask(u_int16_t irqs);
83 static int	pci_pir_biosroute(int bus, int device, int func, int pin,
84 		    int irq);
85 static int	pci_pir_choose_irq(struct pci_link *pci_link, int irqmask);
86 static void	pci_pir_create_links(struct PIR_entry *entry,
87 		    struct PIR_intpin *intpin, void *arg);
88 static void	pci_pir_dump_links(void);
89 static struct pci_link *pci_pir_find_link(uint8_t link_id);
90 static void	pci_pir_find_link_handler(struct PIR_entry *entry,
91 		    struct PIR_intpin *intpin, void *arg);
92 static void	pci_pir_initial_irqs(struct PIR_entry *entry,
93 		    struct PIR_intpin *intpin, void *arg);
94 static void	pci_pir_parse(void);
95 static void	pci_pir_print_intpin(struct PIR_entry *entry,
96 		    struct PIR_intpin *intpin, void *arg);
97 static void	pci_pir_print_table(void);
98 static uint8_t	pci_pir_search_irq(int bus, int device, int pin);
99 static int	pci_pir_valid_irq(struct pci_link *pci_link, int irq);
100 static void	pci_pir_walk_table(pir_entry_handler *handler, void *arg);
101 
102 static MALLOC_DEFINE(M_PIR, "$PIR", "$PIR structures");
103 
104 static struct PIR_table *pci_route_table;
105 static device_t pir_device;
106 static int pci_route_count, pir_bios_irqs, pir_parsed;
107 static TAILQ_HEAD(, pci_link) pci_links;
108 static int pir_interrupt_weight[NUM_ISA_INTERRUPTS];
109 
110 /* sysctl vars */
111 SYSCTL_DECL(_hw_pci);
112 
113 #ifdef PC98
114 /* IRQs 3, 5, 7, 9, 10, 11, 12, 13 */
115 #define PCI_IRQ_OVERRIDE_MASK 0x3e68
116 #else
117 /* IRQs 3, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15 */
118 #define PCI_IRQ_OVERRIDE_MASK 0xdef8
119 #endif
120 
121 static uint32_t pci_irq_override_mask = PCI_IRQ_OVERRIDE_MASK;
122 TUNABLE_INT("hw.pci.irq_override_mask", &pci_irq_override_mask);
123 SYSCTL_INT(_hw_pci, OID_AUTO, irq_override_mask, CTLFLAG_RDTUN,
124     &pci_irq_override_mask, PCI_IRQ_OVERRIDE_MASK,
125     "Mask of allowed irqs to try to route when it has no good clue about\n"
126     "which irqs it should use.");
127 
128 /*
129  * Look for the interrupt routing table.
130  *
131  * We use PCI BIOS's PIR table if it's available. $PIR is the standard way
132  * to do this.  Sadly, some machines are not standards conforming and have
133  * _PIR instead.  We shrug and cope by looking for both.
134  */
135 void
136 pci_pir_open(void)
137 {
138 	struct PIR_table *pt;
139 	uint32_t sigaddr;
140 	int i;
141 	uint8_t ck, *cv;
142 
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 
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 	if (bootverbose) {
171 		printf("Found $PIR table, %d entries at %p\n",
172 		    pci_route_count, pci_route_table);
173 		pci_pir_print_table();
174 	}
175 }
176 
177 /*
178  * Find the pci_link structure for a given link ID.
179  */
180 static struct pci_link *
181 pci_pir_find_link(uint8_t link_id)
182 {
183 	struct pci_link *pci_link;
184 
185 	TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
186 		if (pci_link->pl_id == link_id)
187 			return (pci_link);
188 	}
189 	return (NULL);
190 }
191 
192 /*
193  * Find the link device associated with a PCI device in the table.
194  */
195 static void
196 pci_pir_find_link_handler(struct PIR_entry *entry, struct PIR_intpin *intpin,
197     void *arg)
198 {
199 	struct pci_link_lookup *lookup;
200 
201 	lookup = (struct pci_link_lookup *)arg;
202 	if (entry->pe_bus == lookup->bus &&
203 	    entry->pe_device == lookup->device &&
204 	    intpin - entry->pe_intpin == lookup->pin)
205 		*lookup->pci_link_ptr = pci_pir_find_link(intpin->link);
206 }
207 
208 /*
209  * Check to see if a possible IRQ setting is valid.
210  */
211 static int
212 pci_pir_valid_irq(struct pci_link *pci_link, int irq)
213 {
214 
215 	if (!PCI_INTERRUPT_VALID(irq))
216 		return (0);
217 	return (pci_link->pl_irqmask & (1 << irq));
218 }
219 
220 /*
221  * Walk the $PIR executing the worker function for each valid intpin entry
222  * in the table.  The handler is passed a pointer to both the entry and
223  * the intpin in the entry.
224  */
225 static void
226 pci_pir_walk_table(pir_entry_handler *handler, void *arg)
227 {
228 	struct PIR_entry *entry;
229 	struct PIR_intpin *intpin;
230 	int i, pin;
231 
232 	entry = &pci_route_table->pt_entry[0];
233 	for (i = 0; i < pci_route_count; i++, entry++) {
234 		intpin = &entry->pe_intpin[0];
235 		for (pin = 0; pin < 4; pin++, intpin++)
236 			if (intpin->link != 0)
237 				handler(entry, intpin, arg);
238 	}
239 }
240 
241 static void
242 pci_pir_create_links(struct PIR_entry *entry, struct PIR_intpin *intpin,
243     void *arg)
244 {
245 	struct pci_link *pci_link;
246 
247 	pci_link = pci_pir_find_link(intpin->link);
248 	if (pci_link != NULL) {
249 		pci_link->pl_references++;
250 		if (intpin->irqs != pci_link->pl_irqmask) {
251 			if (bootverbose)
252 				printf(
253 	"$PIR: Entry %d.%d.INT%c has different mask for link %#x, merging\n",
254 				    entry->pe_bus, entry->pe_device,
255 				    (intpin - entry->pe_intpin) + 'A',
256 				    pci_link->pl_id);
257 			pci_link->pl_irqmask &= intpin->irqs;
258 		}
259 	} else {
260 		pci_link = malloc(sizeof(struct pci_link), M_PIR, M_WAITOK);
261 		pci_link->pl_id = intpin->link;
262 		pci_link->pl_irqmask = intpin->irqs;
263 		pci_link->pl_irq = PCI_INVALID_IRQ;
264 		pci_link->pl_references = 1;
265 		pci_link->pl_routed = 0;
266 		TAILQ_INSERT_TAIL(&pci_links, pci_link, pl_links);
267 	}
268 }
269 
270 /*
271  * Look to see if any of the function on the PCI device at bus/device have
272  * an interrupt routed to intpin 'pin' by the BIOS.
273  */
274 static uint8_t
275 pci_pir_search_irq(int bus, int device, int pin)
276 {
277 	uint32_t value;
278 	uint8_t func, maxfunc;
279 
280 	/* See if we have a valid device at function 0. */
281 	value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
282 	if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
283 		return (PCI_INVALID_IRQ);
284 	if (value & PCIM_MFDEV)
285 		maxfunc = PCI_FUNCMAX;
286 	else
287 		maxfunc = 0;
288 
289 	/* Scan all possible functions at this device. */
290 	for (func = 0; func <= maxfunc; func++) {
291 		value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
292 		if (value == 0xffffffff)
293 			continue;
294 		value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
295 
296 		/*
297 		 * See if it uses the pin in question.  Note that the passed
298 		 * in pin uses 0 for A, .. 3 for D whereas the intpin
299 		 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
300 		 */
301 		if (value != pin + 1)
302 			continue;
303 		value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
304 		if (bootverbose)
305 			printf(
306 		"$PIR: Found matching pin for %d.%d.INT%c at func %d: %d\n",
307 			    bus, device, pin + 'A', func, value);
308 		if (value != PCI_INVALID_IRQ)
309 			return (value);
310 	}
311 	return (PCI_INVALID_IRQ);
312 }
313 
314 /*
315  * Try to initialize IRQ based on this device's IRQ.
316  */
317 static void
318 pci_pir_initial_irqs(struct PIR_entry *entry, struct PIR_intpin *intpin,
319     void *arg)
320 {
321 	struct pci_link *pci_link;
322 	uint8_t irq, pin;
323 
324 	pin = intpin - entry->pe_intpin;
325 	pci_link = pci_pir_find_link(intpin->link);
326 	irq = pci_pir_search_irq(entry->pe_bus, entry->pe_device, pin);
327 	if (irq == PCI_INVALID_IRQ)
328 		return;
329 	if (pci_pir_valid_irq(pci_link, irq)) {
330 		if (pci_link->pl_irq == PCI_INVALID_IRQ) {
331 			pci_link->pl_irq = irq;
332 			pci_link->pl_routed = 1;
333 		} else if (pci_link->pl_irq != irq)
334 			printf(
335 	"$PIR: BIOS IRQ %d for %d.%d.INT%c does not match link %#x irq %d\n",
336 			    irq, entry->pe_bus, entry->pe_device, pin + 'A',
337 			    pci_link->pl_id, pci_link->pl_irq);
338 	} else
339 		printf(
340 		"$PIR: BIOS IRQ %d for %d.%d.INT%c is not valid for link %#x\n",
341 		    irq, entry->pe_bus, entry->pe_device, pin + 'A',
342 		    pci_link->pl_id);
343 }
344 
345 /*
346  * Parse $PIR to enumerate link devices and attempt to determine their
347  * initial state.  This could perhaps be cleaner if we had drivers for the
348  * various interrupt routers as they could read the initial IRQ for each
349  * link.
350  */
351 static void
352 pci_pir_parse(void)
353 {
354 	char tunable_buffer[64];
355 	struct pci_link *pci_link;
356 	int i, irq;
357 
358 	/* Only parse once. */
359 	if (pir_parsed)
360 		return;
361 	pir_parsed = 1;
362 
363 	/* Enumerate link devices. */
364 	TAILQ_INIT(&pci_links);
365 	pci_pir_walk_table(pci_pir_create_links, NULL);
366 	if (bootverbose) {
367 		printf("$PIR: Links after initial probe:\n");
368 		pci_pir_dump_links();
369 	}
370 
371 	/* Check for unique IRQ masks. */
372 	TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
373 		if (pci_link->pl_irqmask != 0 && powerof2(pci_link->pl_irqmask))
374 			pci_link->pl_irq = ffs(pci_link->pl_irqmask) - 1;
375 	}
376 
377 	/*
378 	 * Check to see if the BIOS has already routed any of the links by
379 	 * checking each device connected to each link to see if it has a
380 	 * valid IRQ.
381 	 */
382 	pci_pir_walk_table(pci_pir_initial_irqs, NULL);
383 	if (bootverbose) {
384 		printf("$PIR: Links after initial IRQ discovery:\n");
385 		pci_pir_dump_links();
386 	}
387 
388 	/*
389 	 * Allow the user to override the IRQ for a given link device as
390 	 * long as the override is valid or is 255 or 0 to clear a preset
391 	 * IRQ.
392 	 */
393 	i = 0;
394 	TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
395 		snprintf(tunable_buffer, sizeof(tunable_buffer),
396 		    "hw.pci.link.%#x.irq", pci_link->pl_id);
397 		if (getenv_int(tunable_buffer, &irq) == 0)
398 			continue;
399 		if (irq == 0)
400 			irq = PCI_INVALID_IRQ;
401 		if (irq == PCI_INVALID_IRQ ||
402 		    pci_pir_valid_irq(pci_link, irq)) {
403 			pci_link->pl_routed = 0;
404 			pci_link->pl_irq = irq;
405 			i = 1;
406 		}
407 	}
408 	if (bootverbose && i) {
409 		printf("$PIR: Links after tunable overrides:\n");
410 		pci_pir_dump_links();
411 	}
412 
413 	/*
414 	 * Build initial interrupt weights as well as bitmap of "known-good"
415 	 * IRQs that the BIOS has already used for PCI link devices.
416 	 */
417 	TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
418 		if (!PCI_INTERRUPT_VALID(pci_link->pl_irq))
419 			continue;
420 		pir_bios_irqs |= 1 << pci_link->pl_irq;
421 		pir_interrupt_weight[pci_link->pl_irq] +=
422 		    pci_link->pl_references;
423 	}
424 	if (bootverbose) {
425 		printf("$PIR: IRQs used by BIOS: ");
426 		pci_print_irqmask(pir_bios_irqs);
427 		printf("\n");
428 		printf("$PIR: Interrupt Weights:\n[ ");
429 		for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
430 			printf(" %3d", i);
431 		printf(" ]\n[ ");
432 		for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
433 			printf(" %3d", pir_interrupt_weight[i]);
434 		printf(" ]\n");
435 	}
436 }
437 
438 /*
439  * Use the PCI BIOS to route an interrupt for a given device.
440  *
441  * Input:
442  * AX = PCIBIOS_ROUTE_INTERRUPT
443  * BH = bus
444  * BL = device [7:3] / function [2:0]
445  * CH = IRQ
446  * CL = Interrupt Pin (0x0A = A, ... 0x0D = D)
447  */
448 static int
449 pci_pir_biosroute(int bus, int device, int func, int pin, int irq)
450 {
451 	struct bios_regs args;
452 
453 	args.eax = PCIBIOS_ROUTE_INTERRUPT;
454 	args.ebx = (bus << 8) | (device << 3) | func;
455 	args.ecx = (irq << 8) | (0xa + pin);
456 	return (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)));
457 }
458 
459 
460 /*
461  * Route a PCI interrupt using a link device from the $PIR.
462  */
463 int
464 pci_pir_route_interrupt(int bus, int device, int func, int pin)
465 {
466 	struct pci_link_lookup lookup;
467 	struct pci_link *pci_link;
468 	int error, irq;
469 
470 	if (pci_route_table == NULL)
471 		return (PCI_INVALID_IRQ);
472 
473 	/* Lookup link device for this PCI device/pin. */
474 	pci_link = NULL;
475 	lookup.bus = bus;
476 	lookup.device = device;
477 	lookup.pin = pin - 1;
478 	lookup.pci_link_ptr = &pci_link;
479 	pci_pir_walk_table(pci_pir_find_link_handler, &lookup);
480 	if (pci_link == NULL) {
481 		printf("$PIR: No matching entry for %d.%d.INT%c\n", bus,
482 		    device, pin - 1 + 'A');
483 		return (PCI_INVALID_IRQ);
484 	}
485 
486 	/*
487 	 * Pick a new interrupt if we don't have one already.  We look for
488 	 * an interrupt from several different sets.  First, we check the
489 	 * set of PCI only interrupts from the $PIR.  Second, we check the
490 	 * set of known-good interrupts that the BIOS has already used.
491 	 * Lastly, we check the "all possible valid IRQs" set.
492 	 */
493 	if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
494 		irq = pci_pir_choose_irq(pci_link,
495 		    pci_route_table->pt_header.ph_pci_irqs);
496 		if (!PCI_INTERRUPT_VALID(irq))
497 			irq = pci_pir_choose_irq(pci_link, pir_bios_irqs);
498 		if (!PCI_INTERRUPT_VALID(irq))
499 			irq = pci_pir_choose_irq(pci_link,
500 			    pci_irq_override_mask);
501 		if (!PCI_INTERRUPT_VALID(irq)) {
502 			if (bootverbose)
503 				printf(
504 			"$PIR: Failed to route interrupt for %d:%d INT%c\n",
505 				    bus, device, pin - 1 + 'A');
506 			return (PCI_INVALID_IRQ);
507 		}
508 		pci_link->pl_irq = irq;
509 	}
510 
511 	/* Ask the BIOS to route this IRQ if we haven't done so already. */
512 	if (!pci_link->pl_routed) {
513 		error = pci_pir_biosroute(bus, device, func, pin - 1,
514 		    pci_link->pl_irq);
515 
516 		/* Ignore errors when routing a unique interrupt. */
517 		if (error && !powerof2(pci_link->pl_irqmask)) {
518 			printf("$PIR: ROUTE_INTERRUPT failed.\n");
519 			return (PCI_INVALID_IRQ);
520 		}
521 		pci_link->pl_routed = 1;
522 
523 		/* Ensure the interrupt is set to level/low trigger. */
524 		KASSERT(pir_device != NULL, ("missing pir device"));
525 		BUS_CONFIG_INTR(pir_device, pci_link->pl_irq,
526 		    INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
527 	}
528 	if (bootverbose)
529 		printf("$PIR: %d:%d INT%c routed to irq %d\n", bus, device,
530 		    pin - 1 + 'A', pci_link->pl_irq);
531 	return (pci_link->pl_irq);
532 }
533 
534 /*
535  * Try to pick an interrupt for the specified link from the interrupts
536  * set in the mask.
537  */
538 static int
539 pci_pir_choose_irq(struct pci_link *pci_link, int irqmask)
540 {
541 	int i, irq, realmask;
542 
543 	/* XXX: Need to have a #define of known bad IRQs to also mask out? */
544 	realmask = pci_link->pl_irqmask & irqmask;
545 	if (realmask == 0)
546 		return (PCI_INVALID_IRQ);
547 
548 	/* Find IRQ with lowest weight. */
549 	irq = PCI_INVALID_IRQ;
550 	for (i = 0; i < NUM_ISA_INTERRUPTS; i++) {
551 		if (!(realmask & 1 << i))
552 			continue;
553 		if (irq == PCI_INVALID_IRQ ||
554 		    pir_interrupt_weight[i] < pir_interrupt_weight[irq])
555 			irq = i;
556 	}
557 	if (bootverbose && PCI_INTERRUPT_VALID(irq)) {
558 		printf("$PIR: Found IRQ %d for link %#x from ", irq,
559 		    pci_link->pl_id);
560 		pci_print_irqmask(realmask);
561 		printf("\n");
562 	}
563 	return (irq);
564 }
565 
566 static void
567 pci_print_irqmask(u_int16_t irqs)
568 {
569 	int i, first;
570 
571 	if (irqs == 0) {
572 		printf("none");
573 		return;
574 	}
575 	first = 1;
576 	for (i = 0; i < 16; i++, irqs >>= 1)
577 		if (irqs & 1) {
578 			if (!first)
579 				printf(" ");
580 			else
581 				first = 0;
582 			printf("%d", i);
583 		}
584 }
585 
586 /*
587  * Dump the contents of a single intpin entry to the console.
588  */
589 static void
590 pci_pir_print_intpin(struct PIR_entry *entry, struct PIR_intpin *intpin,
591     void *arg)
592 {
593 
594 	if (entry->pe_slot == 0)
595 		printf("embedded ");
596 	else
597 		printf("slot %-3d ", entry->pe_slot);
598 	printf(" %3d  %3d    %c   0x%02x  ", entry->pe_bus, entry->pe_device,
599 	    intpin - entry->pe_intpin + 'A', intpin->link);
600 	pci_print_irqmask(intpin->irqs);
601 	printf("\n");
602 }
603 
604 /*
605  * Dump the contents of a PCI BIOS Interrupt Routing Table to the console.
606  */
607 static void
608 pci_pir_print_table(void)
609 {
610 
611 	printf("PCI-Only Interrupts: ");
612 	pci_print_irqmask(pci_route_table->pt_header.ph_pci_irqs);
613 	printf("\nLocation  Bus Device Pin  Link  IRQs\n");
614 	pci_pir_walk_table(pci_pir_print_intpin, NULL);
615 }
616 
617 /*
618  * Display link devices.
619  */
620 static void
621 pci_pir_dump_links(void)
622 {
623 	struct pci_link *pci_link;
624 
625 	printf("Link  IRQ  Rtd  Ref  IRQs\n");
626 	TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
627 		printf("%#4x  %3d   %c   %3d  ", pci_link->pl_id,
628 		    pci_link->pl_irq, pci_link->pl_routed ? 'Y' : 'N',
629 		    pci_link->pl_references);
630 		pci_print_irqmask(pci_link->pl_irqmask);
631 		printf("\n");
632 	}
633 }
634 
635 /*
636  * See if any interrupts for a given PCI bus are routed in the PIR.  Don't
637  * even bother looking if the BIOS doesn't support routing anyways.  If we
638  * are probing a PCI-PCI bridge, then require_parse will be true and we should
639  * only succeed if a host-PCI bridge has already attached and parsed the PIR.
640  */
641 int
642 pci_pir_probe(int bus, int require_parse)
643 {
644 	int i;
645 
646 	if (pci_route_table == NULL || (require_parse && !pir_parsed))
647 		return (0);
648 	for (i = 0; i < pci_route_count; i++)
649 		if (pci_route_table->pt_entry[i].pe_bus == bus)
650 			return (1);
651 	return (0);
652 }
653 
654 /*
655  * The driver for the new-bus psuedo device pir0 for the $PIR table.
656  */
657 
658 static int
659 pir_probe(device_t dev)
660 {
661 	char buf[64];
662 
663 	snprintf(buf, sizeof(buf), "PCI Interrupt Routing Table: %d Entries",
664 	    pci_route_count);
665 	device_set_desc_copy(dev, buf);
666 	return (0);
667 }
668 
669 static int
670 pir_attach(device_t dev)
671 {
672 
673 	pci_pir_parse();
674 	KASSERT(pir_device == NULL, ("Multiple pir devices"));
675 	pir_device = dev;
676 	return (0);
677 }
678 
679 static void
680 pir_resume_find_device(struct PIR_entry *entry, struct PIR_intpin *intpin,
681     void *arg)
682 {
683 	struct pci_dev_lookup *pd;
684 
685 	pd = (struct pci_dev_lookup *)arg;
686 	if (intpin->link != pd->link || pd->bus != -1)
687 		return;
688 	pd->bus = entry->pe_bus;
689 	pd->device = entry->pe_device;
690 	pd->pin = intpin - entry->pe_intpin;
691 }
692 
693 static int
694 pir_resume(device_t dev)
695 {
696 	struct pci_dev_lookup pd;
697 	struct pci_link *pci_link;
698 	int error;
699 
700 	/* Ask the BIOS to re-route each link that was already routed. */
701 	TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
702 		if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
703 			KASSERT(!pci_link->pl_routed,
704 			    ("link %#x is routed but has invalid PCI IRQ",
705 			    pci_link->pl_id));
706 			continue;
707 		}
708 		if (pci_link->pl_routed) {
709 			pd.bus = -1;
710 			pd.link = pci_link->pl_id;
711 			pci_pir_walk_table(pir_resume_find_device, &pd);
712 			KASSERT(pd.bus != -1,
713 		("did not find matching entry for link %#x in the $PIR table",
714 			    pci_link->pl_id));
715 			if (bootverbose)
716 				device_printf(dev,
717 			    "Using %d.%d.INT%c to route link %#x to IRQ %d\n",
718 				    pd.bus, pd.device, pd.pin + 'A',
719 				    pci_link->pl_id, pci_link->pl_irq);
720 			error = pci_pir_biosroute(pd.bus, pd.device, 0, pd.pin,
721 			    pci_link->pl_irq);
722 			if (error)
723 				device_printf(dev,
724 			    "ROUTE_INTERRUPT on resume for link %#x failed.\n",
725 				    pci_link->pl_id);
726 		}
727 	}
728 	return (0);
729 }
730 
731 static device_method_t pir_methods[] = {
732 	/* Device interface */
733 	DEVMETHOD(device_probe,		pir_probe),
734 	DEVMETHOD(device_attach,	pir_attach),
735 	DEVMETHOD(device_resume,	pir_resume),
736 
737 	{ 0, 0 }
738 };
739 
740 static driver_t pir_driver = {
741 	"pir",
742 	pir_methods,
743 	1,
744 };
745 
746 static devclass_t pir_devclass;
747 
748 DRIVER_MODULE(pir, legacy, pir_driver, pir_devclass, 0, 0);
749