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