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