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

Cache object: bb949fb48fddbc8c58bd37475ec510b0


[ 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.