The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/pci/pci_pir.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: releng/6.3/sys/i386/pci/pci_pir.c 164454 2006-11-20 22:47:00Z jhb $");
   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 #ifdef PC98
  111 /* IRQs 3, 5, 7, 9, 10, 11, 12, 13 */
  112 #define PCI_IRQ_OVERRIDE_MASK 0x3e68
  113 #else
  114 /* IRQs 3, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15 */
  115 #define PCI_IRQ_OVERRIDE_MASK 0xdef8
  116 #endif
  117 
  118 static uint32_t pci_irq_override_mask = PCI_IRQ_OVERRIDE_MASK;
  119 TUNABLE_INT("hw.pci.irq_override_mask", &pci_irq_override_mask);
  120 SYSCTL_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  */
  132 void
  133 pci_pir_open(void)
  134 {
  135         struct PIR_table *pt;
  136         uint32_t sigaddr;
  137         int i;
  138         uint8_t ck, *cv;
  139 
  140         /* Don't try if we've already found a table. */
  141         if (pci_route_table != NULL)
  142                 return;
  143 
  144         /* Look for $PIR and then _PIR. */
  145         sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0);
  146         if (sigaddr == 0)
  147                 sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0);
  148         if (sigaddr == 0)
  149                 return;
  150 
  151         /* If we found something, check the checksum and length. */
  152         /* XXX - Use pmap_mapdev()? */
  153         pt = (struct PIR_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
  154         if (pt->pt_header.ph_length <= sizeof(struct PIR_header))
  155                 return;
  156         for (cv = (u_int8_t *)pt, ck = 0, i = 0;
  157              i < (pt->pt_header.ph_length); i++)
  158                 ck += cv[i];
  159         if (ck != 0)
  160                 return;
  161 
  162         /* Ok, we've got a valid table. */
  163         pci_route_table = pt;
  164         pci_route_count = (pt->pt_header.ph_length -
  165             sizeof(struct PIR_header)) / 
  166             sizeof(struct PIR_entry);
  167 }
  168 
  169 /*
  170  * Find the pci_link structure for a given link ID.
  171  */
  172 static struct pci_link *
  173 pci_pir_find_link(uint8_t link_id)
  174 {
  175         struct pci_link *pci_link;
  176 
  177         TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
  178                 if (pci_link->pl_id == link_id)
  179                         return (pci_link);
  180         }
  181         return (NULL);
  182 }
  183 
  184 /*
  185  * Find the link device associated with a PCI device in the table.
  186  */
  187 static void
  188 pci_pir_find_link_handler(struct PIR_entry *entry, struct PIR_intpin *intpin,
  189     void *arg)
  190 {
  191         struct pci_link_lookup *lookup;
  192 
  193         lookup = (struct pci_link_lookup *)arg;
  194         if (entry->pe_bus == lookup->bus &&
  195             entry->pe_device == lookup->device &&
  196             intpin - entry->pe_intpin == lookup->pin)
  197                 *lookup->pci_link_ptr = pci_pir_find_link(intpin->link);
  198 }
  199 
  200 /*
  201  * Check to see if a possible IRQ setting is valid.
  202  */
  203 static int
  204 pci_pir_valid_irq(struct pci_link *pci_link, int irq)
  205 {
  206 
  207         if (!PCI_INTERRUPT_VALID(irq))
  208                 return (0);
  209         return (pci_link->pl_irqmask & (1 << irq));
  210 }
  211 
  212 /*
  213  * Walk the $PIR executing the worker function for each valid intpin entry
  214  * in the table.  The handler is passed a pointer to both the entry and
  215  * the intpin in the entry.
  216  */
  217 static void
  218 pci_pir_walk_table(pir_entry_handler *handler, void *arg)
  219 {
  220         struct PIR_entry *entry;
  221         struct PIR_intpin *intpin;
  222         int i, pin;
  223 
  224         entry = &pci_route_table->pt_entry[0];
  225         for (i = 0; i < pci_route_count; i++, entry++) {
  226                 intpin = &entry->pe_intpin[0];
  227                 for (pin = 0; pin < 4; pin++, intpin++)
  228                         if (intpin->link != 0)
  229                                 handler(entry, intpin, arg);
  230         }
  231 }
  232 
  233 static void
  234 pci_pir_create_links(struct PIR_entry *entry, struct PIR_intpin *intpin,
  235     void *arg)
  236 {
  237         struct pci_link *pci_link;
  238 
  239         pci_link = pci_pir_find_link(intpin->link);
  240         if (pci_link != NULL) {
  241                 pci_link->pl_references++;
  242                 if (intpin->irqs != pci_link->pl_irqmask) {
  243                         if (bootverbose)
  244                                 printf(
  245         "$PIR: Entry %d.%d.INT%c has different mask for link %#x, merging\n",
  246                                     entry->pe_bus, entry->pe_device,
  247                                     (intpin - entry->pe_intpin) + 'A',
  248                                     pci_link->pl_id);
  249                         pci_link->pl_irqmask &= intpin->irqs;
  250                 }
  251         } else {
  252                 pci_link = malloc(sizeof(struct pci_link), M_PIR, M_WAITOK);
  253                 pci_link->pl_id = intpin->link;
  254                 pci_link->pl_irqmask = intpin->irqs;
  255                 pci_link->pl_irq = PCI_INVALID_IRQ;
  256                 pci_link->pl_references = 1;
  257                 pci_link->pl_routed = 0;
  258                 TAILQ_INSERT_TAIL(&pci_links, pci_link, pl_links);
  259         }
  260 }
  261 
  262 /*
  263  * Look to see if any of the function on the PCI device at bus/device have
  264  * an interrupt routed to intpin 'pin' by the BIOS.
  265  */
  266 static uint8_t
  267 pci_pir_search_irq(int bus, int device, int pin)
  268 {
  269         uint32_t value;
  270         uint8_t func, maxfunc;
  271 
  272         /* See if we have a valid device at function 0. */
  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_DEVVENDOR, 4);
  284                 if (value == 0xffffffff)
  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         /* Check for unique IRQ masks. */
  401         TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
  402                 if (pci_link->pl_irqmask != 0 && powerof2(pci_link->pl_irqmask))
  403                         pci_link->pl_irq = ffs(pci_link->pl_irqmask) - 1;
  404         }
  405 
  406         /*
  407          * Check to see if the BIOS has already routed any of the links by
  408          * checking each device connected to each link to see if it has a
  409          * valid IRQ.
  410          */
  411         pci_pir_walk_table(pci_pir_initial_irqs, NULL);
  412         if (bootverbose) {
  413                 printf("$PIR: Links after initial IRQ discovery:\n");
  414                 pci_pir_dump_links();
  415         }
  416 
  417         /*
  418          * Allow the user to override the IRQ for a given link device.  We
  419          * allow invalid IRQs to be specified but warn about them.  An IRQ
  420          * of 255 or 0 clears any preset IRQ.
  421          */
  422         i = 0;
  423         TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
  424                 snprintf(tunable_buffer, sizeof(tunable_buffer),
  425                     "hw.pci.link.%#x.irq", pci_link->pl_id);
  426                 if (getenv_int(tunable_buffer, &irq) == 0)
  427                         continue;
  428                 if (irq == 0)
  429                         irq = PCI_INVALID_IRQ;
  430                 if (irq != PCI_INVALID_IRQ &&
  431                     !pci_pir_valid_irq(pci_link, irq) && bootverbose)
  432                         printf(
  433                 "$PIR: Warning, IRQ %d for link %#x is not listed as valid\n",
  434                             irq, pci_link->pl_id);
  435                 pci_link->pl_routed = 0;
  436                 pci_link->pl_irq = irq;
  437                 i = 1;
  438         }
  439         if (bootverbose && i) {
  440                 printf("$PIR: Links after tunable overrides:\n");
  441                 pci_pir_dump_links();
  442         }
  443 
  444         /*
  445          * Build initial interrupt weights as well as bitmap of "known-good"
  446          * IRQs that the BIOS has already used for PCI link devices.
  447          */
  448         TAILQ_FOREACH(pci_link, &pci_links, pl_links) {
  449                 if (!PCI_INTERRUPT_VALID(pci_link->pl_irq))
  450                         continue;
  451                 pir_bios_irqs |= 1 << pci_link->pl_irq;
  452                 pir_interrupt_weight[pci_link->pl_irq] +=
  453                     pci_link->pl_references;
  454         }
  455         if (bootverbose) {
  456                 printf("$PIR: IRQs used by BIOS: ");
  457                 pci_print_irqmask(pir_bios_irqs);
  458                 printf("\n");
  459                 printf("$PIR: Interrupt Weights:\n[ ");
  460                 for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
  461                         printf(" %3d", i);
  462                 printf(" ]\n[ ");
  463                 for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
  464                         printf(" %3d", pir_interrupt_weight[i]);
  465                 printf(" ]\n");
  466         }
  467 }
  468 
  469 /*
  470  * Use the PCI BIOS to route an interrupt for a given device.
  471  *
  472  * Input:
  473  * AX = PCIBIOS_ROUTE_INTERRUPT
  474  * BH = bus
  475  * BL = device [7:3] / function [2:0]
  476  * CH = IRQ
  477  * CL = Interrupt Pin (0x0A = A, ... 0x0D = D)
  478  */
  479 static int
  480 pci_pir_biosroute(int bus, int device, int func, int pin, int irq)
  481 {
  482         struct bios_regs args;
  483 
  484         args.eax = PCIBIOS_ROUTE_INTERRUPT;
  485         args.ebx = (bus << 8) | (device << 3) | func;
  486         args.ecx = (irq << 8) | (0xa + pin);
  487         return (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)));
  488 }
  489 
  490 
  491 /*
  492  * Route a PCI interrupt using a link device from the $PIR.
  493  */
  494 int
  495 pci_pir_route_interrupt(int bus, int device, int func, int pin)
  496 {
  497         struct pci_link_lookup lookup;
  498         struct pci_link *pci_link;
  499         int error, irq;
  500 
  501         if (pci_route_table == NULL)
  502                 return (PCI_INVALID_IRQ);
  503 
  504         /* Lookup link device for this PCI device/pin. */
  505         pci_link = NULL;
  506         lookup.bus = bus;
  507         lookup.device = device;
  508         lookup.pin = pin - 1;
  509         lookup.pci_link_ptr = &pci_link;
  510         pci_pir_walk_table(pci_pir_find_link_handler, &lookup);
  511         if (pci_link == NULL) {
  512                 printf("$PIR: No matching entry for %d.%d.INT%c\n", bus,
  513                     device, pin - 1 + 'A');
  514                 return (PCI_INVALID_IRQ);
  515         }
  516 
  517         /*
  518          * Pick a new interrupt if we don't have one already.  We look for
  519          * an interrupt from several different sets.  First, we check the
  520          * set of PCI only interrupts from the $PIR.  Second, we check the
  521          * set of known-good interrupts that the BIOS has already used.
  522          * Lastly, we check the "all possible valid IRQs" set.
  523          */
  524         if (!PCI_INTERRUPT_VALID(pci_link->pl_irq)) {
  525                 irq = pci_pir_choose_irq(pci_link,
  526                     pci_route_table->pt_header.ph_pci_irqs);
  527                 if (!PCI_INTERRUPT_VALID(irq))
  528                         irq = pci_pir_choose_irq(pci_link, pir_bios_irqs);
  529                 if (!PCI_INTERRUPT_VALID(irq))
  530                         irq = pci_pir_choose_irq(pci_link,
  531                             pci_irq_override_mask);
  532                 if (!PCI_INTERRUPT_VALID(irq)) {
  533                         if (bootverbose)
  534                                 printf(
  535                         "$PIR: Failed to route interrupt for %d:%d INT%c\n",
  536                                     bus, device, pin - 1 + 'A');
  537                         return (PCI_INVALID_IRQ);
  538                 }
  539                 pci_link->pl_irq = irq;
  540         }
  541 
  542         /* Ask the BIOS to route this IRQ if we haven't done so already. */
  543         if (!pci_link->pl_routed) {
  544                 error = pci_pir_biosroute(bus, device, func, pin - 1,
  545                     pci_link->pl_irq);
  546 
  547                 /* Ignore errors when routing a unique interrupt. */
  548                 if (error && !powerof2(pci_link->pl_irqmask)) {
  549                         printf("$PIR: ROUTE_INTERRUPT failed.\n");
  550                         return (PCI_INVALID_IRQ);
  551                 }
  552                 pci_link->pl_routed = 1;
  553 
  554                 /* Ensure the interrupt is set to level/low trigger. */
  555                 KASSERT(pir_device != NULL, ("missing pir device"));
  556                 BUS_CONFIG_INTR(pir_device, pci_link->pl_irq,
  557                     INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
  558         }
  559         if (bootverbose)
  560                 printf("$PIR: %d:%d INT%c routed to irq %d\n", bus, device,
  561                     pin - 1 + 'A', pci_link->pl_irq);
  562         return (pci_link->pl_irq);
  563 }
  564 
  565 /*
  566  * Try to pick an interrupt for the specified link from the interrupts
  567  * set in the mask.
  568  */
  569 static int
  570 pci_pir_choose_irq(struct pci_link *pci_link, int irqmask)
  571 {
  572         int i, irq, realmask;
  573 
  574         /* XXX: Need to have a #define of known bad IRQs to also mask out? */
  575         realmask = pci_link->pl_irqmask & irqmask;
  576         if (realmask == 0)
  577                 return (PCI_INVALID_IRQ);
  578 
  579         /* Find IRQ with lowest weight. */
  580         irq = PCI_INVALID_IRQ;
  581         for (i = 0; i < NUM_ISA_INTERRUPTS; i++) {
  582                 if (!(realmask & 1 << i))
  583                         continue;
  584                 if (irq == PCI_INVALID_IRQ ||
  585                     pir_interrupt_weight[i] < pir_interrupt_weight[irq])
  586                         irq = i;
  587         }
  588         if (bootverbose && PCI_INTERRUPT_VALID(irq)) {
  589                 printf("$PIR: Found IRQ %d for link %#x from ", irq,
  590                     pci_link->pl_id);
  591                 pci_print_irqmask(realmask);
  592                 printf("\n");
  593         }
  594         return (irq);
  595 }
  596 
  597 static void
  598 pci_print_irqmask(u_int16_t irqs)
  599 {
  600         int i, first;
  601 
  602         if (irqs == 0) {
  603                 printf("none");
  604                 return;
  605         }
  606         first = 1;
  607         for (i = 0; i < 16; i++, irqs >>= 1)
  608                 if (irqs & 1) {
  609                         if (!first)
  610                                 printf(" ");
  611                         else
  612                                 first = 0;
  613                         printf("%d", i);
  614                 }
  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);

Cache object: d0c9aad484132a3a43c50de79ef834be


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.