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/dev/acpica/acpi_pci_link.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) 2002 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_acpi.h"
   31 #include <sys/param.h>
   32 #include <sys/bus.h>
   33 #include <sys/kernel.h>
   34 #include <sys/limits.h>
   35 #include <sys/malloc.h>
   36 #include <sys/module.h>
   37 
   38 #include <contrib/dev/acpica/acpi.h>
   39 #include <dev/acpica/acpivar.h>
   40 #include <dev/acpica/acpi_pcibvar.h>
   41 
   42 #include <machine/pci_cfgreg.h>
   43 #include <dev/pci/pcireg.h>
   44 #include <dev/pci/pcivar.h>
   45 #include "pcib_if.h"
   46 
   47 /* Hooks for the ACPI CA debugging infrastructure. */
   48 #define _COMPONENT      ACPI_BUS
   49 ACPI_MODULE_NAME("PCI_LINK")
   50 
   51 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link");
   52 
   53 #define NUM_ISA_INTERRUPTS      16
   54 #define NUM_ACPI_INTERRUPTS     256
   55 
   56 /*
   57  * An ACPI PCI link device may contain multiple links.  Each link has its
   58  * own ACPI resource.  _PRT entries specify which link is being used via
   59  * the Source Index.
   60  *
   61  * XXX: A note about Source Indices and DPFs:  Currently we assume that
   62  * the DPF start and end tags are not counted towards the index that
   63  * Source Index corresponds to.  Also, we assume that when DPFs are in use
   64  * they various sets overlap in terms of Indices.  Here's an example
   65  * resource list indicating these assumptions:
   66  *
   67  * Resource             Index
   68  * --------             -----
   69  * I/O Port             0
   70  * Start DPF            -
   71  * IRQ                  1
   72  * MemIO                2
   73  * Start DPF            -
   74  * IRQ                  1
   75  * MemIO                2
   76  * End DPF              -
   77  * DMA Channel          3
   78  *
   79  * The XXX is because I'm not sure if this is a valid assumption to make.
   80  */
   81 
   82 /* States during DPF processing. */
   83 #define DPF_OUTSIDE     0
   84 #define DPF_FIRST       1
   85 #define DPF_IGNORE      2
   86 
   87 struct link;
   88 
   89 struct acpi_pci_link_softc {
   90         int     pl_num_links;
   91         int     pl_crs_bad;
   92         struct link *pl_links;
   93         device_t pl_dev;
   94 };
   95 
   96 struct link {
   97         struct acpi_pci_link_softc *l_sc;
   98         uint8_t l_bios_irq;
   99         uint8_t l_irq;
  100         uint8_t l_initial_irq;
  101         int     l_res_index;
  102         int     l_num_irqs;
  103         int     *l_irqs;
  104         int     l_references;
  105         int     l_routed:1;
  106         int     l_isa_irq:1;
  107         ACPI_RESOURCE l_prs_template;
  108 };
  109 
  110 struct link_count_request {
  111         int     in_dpf;
  112         int     count;
  113 };
  114 
  115 struct link_res_request {
  116         struct acpi_pci_link_softc *sc;
  117         int     in_dpf;
  118         int     res_index;
  119         int     link_index;
  120 };
  121 
  122 MALLOC_DEFINE(M_PCI_LINK, "PCI Link", "ACPI PCI Link structures");
  123 
  124 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
  125 static int pci_link_bios_isa_irqs;
  126 
  127 static char *pci_link_ids[] = { "PNP0C0F", NULL };
  128 
  129 /*
  130  * Fetch the short name associated with an ACPI handle and save it in the
  131  * passed in buffer.
  132  */
  133 static ACPI_STATUS
  134 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
  135 {
  136         ACPI_BUFFER buf;
  137 
  138         buf.Length = buflen;
  139         buf.Pointer = buffer;
  140         return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
  141 }
  142 
  143 static int
  144 acpi_pci_link_probe(device_t dev)
  145 {
  146         char descr[28], name[12];
  147 
  148         /*
  149          * We explicitly do not check _STA since not all systems set it to
  150          * sensible values.
  151          */
  152         if (acpi_disabled("pci_link") ||
  153             ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
  154                 return (ENXIO);
  155 
  156         if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
  157             sizeof(name)))) {
  158                 snprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
  159                 device_set_desc_copy(dev, descr);
  160         } else
  161                 device_set_desc(dev, "ACPI PCI Link");
  162         device_quiet(dev);
  163         return (0);
  164 }
  165 
  166 static ACPI_STATUS
  167 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
  168 {
  169         struct link_count_request *req;
  170 
  171         req = (struct link_count_request *)context;
  172         switch (res->Id) {
  173         case ACPI_RSTYPE_START_DPF:
  174                 switch (req->in_dpf) {
  175                 case DPF_OUTSIDE:
  176                         /* We've started the first DPF. */
  177                         req->in_dpf = DPF_FIRST;
  178                         break;
  179                 case DPF_FIRST:
  180                         /* We've started the second DPF. */
  181                         req->in_dpf = DPF_IGNORE;
  182                         break;
  183                 }
  184                 break;
  185         case ACPI_RSTYPE_END_DPF:
  186                 /* We are finished with DPF parsing. */
  187                 KASSERT(req->in_dpf != DPF_OUTSIDE,
  188                     ("%s: end dpf when not parsing a dpf", __func__));
  189                 req->in_dpf = DPF_OUTSIDE;
  190                 break;
  191         case ACPI_RSTYPE_IRQ:
  192         case ACPI_RSTYPE_EXT_IRQ:
  193                 /*
  194                  * Don't count resources if we are in a DPF set that we are
  195                  * ignoring.
  196                  */
  197                 if (req->in_dpf != DPF_IGNORE)
  198                         req->count++;
  199         }
  200         return (AE_OK);
  201 }
  202 
  203 static ACPI_STATUS
  204 link_add_crs(ACPI_RESOURCE *res, void *context)
  205 {
  206         struct link_res_request *req;
  207         struct link *link;
  208 
  209         ACPI_SERIAL_ASSERT(pci_link);
  210         req = (struct link_res_request *)context;
  211         switch (res->Id) {
  212         case ACPI_RSTYPE_START_DPF:
  213                 switch (req->in_dpf) {
  214                 case DPF_OUTSIDE:
  215                         /* We've started the first DPF. */
  216                         req->in_dpf = DPF_FIRST;
  217                         break;
  218                 case DPF_FIRST:
  219                         /* We've started the second DPF. */
  220                         panic(
  221                 "%s: Multiple dependent functions within a current resource",
  222                             __func__);
  223                         break;
  224                 }
  225                 break;
  226         case ACPI_RSTYPE_END_DPF:
  227                 /* We are finished with DPF parsing. */
  228                 KASSERT(req->in_dpf != DPF_OUTSIDE,
  229                     ("%s: end dpf when not parsing a dpf", __func__));
  230                 req->in_dpf = DPF_OUTSIDE;
  231                 break;
  232         case ACPI_RSTYPE_IRQ:
  233         case ACPI_RSTYPE_EXT_IRQ:
  234                 KASSERT(req->link_index < req->sc->pl_num_links,
  235                     ("%s: array boundary violation", __func__));
  236                 link = &req->sc->pl_links[req->link_index];
  237                 link->l_res_index = req->res_index;
  238                 req->link_index++;
  239                 req->res_index++;
  240 
  241                 /*
  242                  * Only use the current value if there's one IRQ.  Some
  243                  * systems return multiple IRQs (which is nonsense for _CRS)
  244                  * when the link hasn't been programmed.
  245                  */
  246                 if (res->Id == ACPI_RSTYPE_IRQ) {
  247                         if (res->Data.Irq.NumberOfInterrupts == 1)
  248                                 link->l_irq = res->Data.Irq.Interrupts[0];
  249                 } else if (res->Data.ExtendedIrq.NumberOfInterrupts == 1)
  250                         link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
  251 
  252                 /*
  253                  * An IRQ of zero means that the link isn't routed.
  254                  */
  255                 if (link->l_irq == 0)
  256                         link->l_irq = PCI_INVALID_IRQ;
  257                 break;
  258         default:
  259                 req->res_index++;
  260         }
  261         return (AE_OK);
  262 }
  263 
  264 /*
  265  * Populate the set of possible IRQs for each device.
  266  */
  267 static ACPI_STATUS
  268 link_add_prs(ACPI_RESOURCE *res, void *context)
  269 {
  270         struct link_res_request *req;
  271         struct link *link;
  272         UINT32 *irqs;
  273         int i;
  274 
  275         ACPI_SERIAL_ASSERT(pci_link);
  276         req = (struct link_res_request *)context;
  277         switch (res->Id) {
  278         case ACPI_RSTYPE_START_DPF:
  279                 switch (req->in_dpf) {
  280                 case DPF_OUTSIDE:
  281                         /* We've started the first DPF. */
  282                         req->in_dpf = DPF_FIRST;
  283                         break;
  284                 case DPF_FIRST:
  285                         /* We've started the second DPF. */
  286                         req->in_dpf = DPF_IGNORE;
  287                         break;
  288                 }
  289                 break;
  290         case ACPI_RSTYPE_END_DPF:
  291                 /* We are finished with DPF parsing. */
  292                 KASSERT(req->in_dpf != DPF_OUTSIDE,
  293                     ("%s: end dpf when not parsing a dpf", __func__));
  294                 req->in_dpf = DPF_OUTSIDE;
  295                 break;
  296         case ACPI_RSTYPE_IRQ:
  297         case ACPI_RSTYPE_EXT_IRQ:
  298                 /*
  299                  * Don't parse resources if we are in a DPF set that we are
  300                  * ignoring.
  301                  */
  302                 if (req->in_dpf == DPF_IGNORE)
  303                         break;
  304 
  305                 KASSERT(req->link_index < req->sc->pl_num_links,
  306                     ("%s: array boundary violation", __func__));
  307                 link = &req->sc->pl_links[req->link_index];
  308                 if (link->l_res_index == -1) {
  309                         KASSERT(req->sc->pl_crs_bad,
  310                             ("res_index should be set"));
  311                         link->l_res_index = req->res_index;
  312                 }
  313                 req->link_index++;
  314                 req->res_index++;
  315 
  316                 /*
  317                  * Stash a copy of the resource for later use when doing
  318                  * _SRS.
  319                  */
  320                 bcopy(res, &link->l_prs_template, sizeof(ACPI_RESOURCE));
  321                 if (res->Id == ACPI_RSTYPE_IRQ) {
  322                         link->l_num_irqs = res->Data.Irq.NumberOfInterrupts;
  323                         irqs = res->Data.Irq.Interrupts;
  324                 } else {
  325                         link->l_num_irqs =
  326                             res->Data.ExtendedIrq.NumberOfInterrupts;
  327                         irqs = res->Data.ExtendedIrq.Interrupts;
  328                 }
  329                 if (link->l_num_irqs == 0)
  330                         break;
  331 
  332                 /*
  333                  * Save a list of the valid IRQs.  Also, if all of the
  334                  * valid IRQs are ISA IRQs, then mark this link as
  335                  * routed via an ISA interrupt.
  336                  */
  337                 link->l_isa_irq = TRUE;
  338                 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
  339                     M_PCI_LINK, M_WAITOK | M_ZERO);
  340                 for (i = 0; i < link->l_num_irqs; i++) {
  341                         link->l_irqs[i] = irqs[i];
  342                         if (irqs[i] >= NUM_ISA_INTERRUPTS)
  343                                 link->l_isa_irq = FALSE;
  344                 }
  345                 break;
  346         default:
  347                 if (req->in_dpf == DPF_IGNORE)
  348                         break;
  349                 if (req->sc->pl_crs_bad)
  350                         device_printf(req->sc->pl_dev,
  351                     "Warning: possible resource %d will be lost during _SRS\n",
  352                             req->res_index);
  353                 req->res_index++;
  354         }
  355         return (AE_OK);
  356 }
  357 
  358 static int
  359 link_valid_irq(struct link *link, int irq)
  360 {
  361         int i;
  362 
  363         ACPI_SERIAL_ASSERT(pci_link);
  364 
  365         /* Invalid interrupts are never valid. */
  366         if (!PCI_INTERRUPT_VALID(irq))
  367                 return (FALSE);
  368 
  369         /* Any interrupt in the list of possible interrupts is valid. */
  370         for (i = 0; i < link->l_num_irqs; i++)
  371                 if (link->l_irqs[i] == irq)
  372                          return (TRUE);
  373 
  374         /*
  375          * For links routed via an ISA interrupt, if the SCI is routed via
  376          * an ISA interrupt, the SCI is always treated as a valid IRQ.
  377          */
  378         if (link->l_isa_irq && AcpiGbl_FADT->SciInt == irq &&
  379             irq < NUM_ISA_INTERRUPTS)
  380                 return (TRUE);
  381 
  382         /* If the interrupt wasn't found in the list it is not valid. */
  383         return (FALSE);
  384 }
  385 
  386 static void
  387 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
  388 {
  389         struct link *link;
  390         char buf[16];
  391         int i, j;
  392 
  393         ACPI_SERIAL_ASSERT(pci_link);
  394         if (header) {
  395                 snprintf(buf, sizeof(buf), "%s:",
  396                     device_get_nameunit(sc->pl_dev));
  397                 printf("%-16.16s  Index  IRQ  Rtd  Ref  IRQs\n", buf);
  398         }
  399         for (i = 0; i < sc->pl_num_links; i++) {
  400                 link = &sc->pl_links[i];
  401                 printf("  %-14.14s  %5d  %3d   %c   %3d ", i == 0 ? tag : "", i,
  402                     link->l_irq, link->l_routed ? 'Y' : 'N',
  403                     link->l_references);
  404                 if (link->l_num_irqs == 0)
  405                         printf(" none");
  406                 else for (j = 0; j < link->l_num_irqs; j++)
  407                         printf(" %d", link->l_irqs[j]);
  408                 printf("\n");
  409         }
  410 }
  411 
  412 static int
  413 acpi_pci_link_attach(device_t dev)
  414 {
  415         struct acpi_pci_link_softc *sc;
  416         struct link_count_request creq;
  417         struct link_res_request rreq;
  418         ACPI_STATUS status;
  419         int i;
  420 
  421         sc = device_get_softc(dev);
  422         sc->pl_dev = dev;
  423         ACPI_SERIAL_BEGIN(pci_link);
  424 
  425         /*
  426          * Count the number of current resources so we know how big of
  427          * a link array to allocate.  On some systems, _CRS is broken,
  428          * so for those systems try to derive the count from _PRS instead.
  429          */
  430         creq.in_dpf = DPF_OUTSIDE;
  431         creq.count = 0;
  432         status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
  433             acpi_count_irq_resources, &creq);
  434         sc->pl_crs_bad = ACPI_FAILURE(status);
  435         if (sc->pl_crs_bad) {
  436                 creq.in_dpf = DPF_OUTSIDE;
  437                 creq.count = 0;
  438                 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
  439                     acpi_count_irq_resources, &creq);
  440                 if (ACPI_FAILURE(status)) {
  441                         device_printf(dev,
  442                             "Unable to parse _CRS or _PRS: %s\n",
  443                             AcpiFormatException(status));
  444                         ACPI_SERIAL_END(pci_link);
  445                         return (ENXIO);
  446                 }
  447         }
  448         sc->pl_num_links = creq.count;
  449         if (creq.count == 0) {
  450                 ACPI_SERIAL_END(pci_link);
  451                 return (0);
  452         }
  453         sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
  454             M_PCI_LINK, M_WAITOK | M_ZERO);
  455 
  456         /* Initialize the child links. */
  457         for (i = 0; i < sc->pl_num_links; i++) {
  458                 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
  459                 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
  460                 sc->pl_links[i].l_sc = sc;
  461                 sc->pl_links[i].l_isa_irq = FALSE;
  462                 sc->pl_links[i].l_res_index = -1;
  463         }
  464 
  465         /* Try to read the current settings from _CRS if it is valid. */
  466         if (!sc->pl_crs_bad) {
  467                 rreq.in_dpf = DPF_OUTSIDE;
  468                 rreq.link_index = 0;
  469                 rreq.res_index = 0;
  470                 rreq.sc = sc;
  471                 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
  472                     link_add_crs, &rreq);
  473                 if (ACPI_FAILURE(status)) {
  474                         device_printf(dev, "Unable to parse _CRS: %s\n",
  475                             AcpiFormatException(status));
  476                         goto fail;
  477                 }
  478         }
  479 
  480         /*
  481          * Try to read the possible settings from _PRS.  Note that if the
  482          * _CRS is toast, we depend on having a working _PRS.  However, if
  483          * _CRS works, then it is ok for _PRS to be missing.
  484          */
  485         rreq.in_dpf = DPF_OUTSIDE;
  486         rreq.link_index = 0;
  487         rreq.res_index = 0;
  488         rreq.sc = sc;
  489         status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
  490             link_add_prs, &rreq);
  491         if (ACPI_FAILURE(status) &&
  492             (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
  493                 device_printf(dev, "Unable to parse _PRS: %s\n",
  494                     AcpiFormatException(status));
  495                 goto fail;
  496         }
  497         if (bootverbose)
  498                 acpi_pci_link_dump(sc, 1, "Initial Probe");
  499 
  500         /* Verify initial IRQs if we have _PRS. */
  501         if (status != AE_NOT_FOUND)
  502                 for (i = 0; i < sc->pl_num_links; i++)
  503                         if (!link_valid_irq(&sc->pl_links[i],
  504                             sc->pl_links[i].l_irq))
  505                                 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
  506         if (bootverbose)
  507                 acpi_pci_link_dump(sc, 0, "Validation");
  508 
  509         /* Save initial IRQs. */
  510         for (i = 0; i < sc->pl_num_links; i++)
  511                 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
  512 
  513         /*
  514          * Try to disable this link.  If successful, set the current IRQ to
  515          * zero and flags to indicate this link is not routed.  If we can't
  516          * run _DIS (i.e., the method doesn't exist), assume the initial
  517          * IRQ was routed by the BIOS.
  518          */
  519         if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
  520             NULL)))
  521                 for (i = 0; i < sc->pl_num_links; i++)
  522                         sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
  523         else
  524                 for (i = 0; i < sc->pl_num_links; i++)
  525                         if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
  526                                 sc->pl_links[i].l_routed = TRUE;
  527         if (bootverbose)
  528                 acpi_pci_link_dump(sc, 0, "After Disable");
  529         ACPI_SERIAL_END(pci_link);
  530         return (0);
  531 fail:
  532         ACPI_SERIAL_END(pci_link);
  533         for (i = 0; i < sc->pl_num_links; i++)
  534                 if (sc->pl_links[i].l_irqs != NULL)
  535                         free(sc->pl_links[i].l_irqs, M_PCI_LINK);
  536         free(sc->pl_links, M_PCI_LINK);
  537         return (ENXIO);
  538 }
  539 
  540 /* XXX: Note that this is identical to pci_pir_search_irq(). */
  541 static uint8_t
  542 acpi_pci_link_search_irq(int bus, int device, int pin)
  543 {
  544         uint32_t value;
  545         uint8_t func, maxfunc;
  546 
  547         /* See if we have a valid device at function 0. */
  548         value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
  549         if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
  550                 return (PCI_INVALID_IRQ);
  551         if (value & PCIM_MFDEV)
  552                 maxfunc = PCI_FUNCMAX;
  553         else
  554                 maxfunc = 0;
  555 
  556         /* Scan all possible functions at this device. */
  557         for (func = 0; func <= maxfunc; func++) {
  558                 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
  559                 if (value == 0xffffffff)
  560                         continue;
  561                 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
  562 
  563                 /*
  564                  * See if it uses the pin in question.  Note that the passed
  565                  * in pin uses 0 for A, .. 3 for D whereas the intpin
  566                  * register uses 0 for no interrupt, 1 for A, .. 4 for D.
  567                  */
  568                 if (value != pin + 1)
  569                         continue;
  570                 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
  571                 if (bootverbose)
  572                         printf(
  573                 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
  574                             bus, device, pin + 'A', func, value);
  575                 if (value != PCI_INVALID_IRQ)
  576                         return (value);
  577         }
  578         return (PCI_INVALID_IRQ);
  579 }
  580 
  581 /*
  582  * Find the link structure that corresponds to the resource index passed in
  583  * via 'source_index'.
  584  */
  585 static struct link *
  586 acpi_pci_link_lookup(device_t dev, int source_index)
  587 {
  588         struct acpi_pci_link_softc *sc;
  589         int i;
  590 
  591         ACPI_SERIAL_ASSERT(pci_link);
  592         sc = device_get_softc(dev);
  593         for (i = 0; i < sc->pl_num_links; i++)
  594                 if (sc->pl_links[i].l_res_index == source_index)
  595                         return (&sc->pl_links[i]);
  596         return (NULL);
  597 }
  598 
  599 void
  600 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
  601     int pin)
  602 {
  603         struct link *link;
  604         uint8_t bios_irq;
  605         uintptr_t bus;
  606 
  607         /*
  608          * Look up the PCI bus for the specified PCI bridge device.  Note
  609          * that the PCI bridge device might not have any children yet.
  610          * However, looking up its bus number doesn't require a valid child
  611          * device, so we just pass NULL.
  612          */
  613         if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
  614                 device_printf(pcib, "Unable to read PCI bus number");
  615                 panic("PCI bridge without a bus number");
  616         }
  617                 
  618         /* Bump the reference count. */
  619         ACPI_SERIAL_BEGIN(pci_link);
  620         link = acpi_pci_link_lookup(dev, index);
  621         if (link == NULL) {
  622                 device_printf(dev, "apparently invalid index %d\n", index);
  623                 ACPI_SERIAL_END(pci_link);
  624                 return;
  625         }
  626         link->l_references++;
  627         if (link->l_routed)
  628                 pci_link_interrupt_weights[link->l_irq]++;
  629 
  630         /*
  631          * The BIOS only routes interrupts via ISA IRQs using the ATPICs
  632          * (8259As).  Thus, if this link is routed via an ISA IRQ, go
  633          * look to see if the BIOS routed an IRQ for this link at the
  634          * indicated (bus, slot, pin).  If so, we prefer that IRQ for
  635          * this link and add that IRQ to our list of known-good IRQs.
  636          * This provides a good work-around for link devices whose _CRS
  637          * method is either broken or bogus.  We only use the value
  638          * returned by _CRS if we can't find a valid IRQ via this method
  639          * in fact.
  640          *
  641          * If this link is not routed via an ISA IRQ (because we are using
  642          * APIC for example), then don't bother looking up the BIOS IRQ
  643          * as if we find one it won't be valid anyway.
  644          */
  645         if (!link->l_isa_irq) {
  646                 ACPI_SERIAL_END(pci_link);
  647                 return;
  648         }
  649 
  650         /* Try to find a BIOS IRQ setting from any matching devices. */
  651         bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
  652         if (!PCI_INTERRUPT_VALID(bios_irq)) {
  653                 ACPI_SERIAL_END(pci_link);
  654                 return;
  655         }
  656 
  657         /* Validate the BIOS IRQ. */
  658         if (!link_valid_irq(link, bios_irq)) {
  659                 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
  660                     bios_irq, (int)bus, slot, pin + 'A');
  661         } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
  662                 link->l_bios_irq = bios_irq;
  663                 if (bios_irq < NUM_ISA_INTERRUPTS)
  664                         pci_link_bios_isa_irqs |= (1 << bios_irq);
  665                 if (bios_irq != link->l_initial_irq &&
  666                     PCI_INTERRUPT_VALID(link->l_initial_irq))
  667                         device_printf(dev,
  668                             "BIOS IRQ %u does not match initial IRQ %u\n",
  669                             bios_irq, link->l_initial_irq);
  670         } else if (bios_irq != link->l_bios_irq)
  671                 device_printf(dev,
  672             "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
  673                     bios_irq, (int)bus, slot, pin + 'A',
  674                     link->l_bios_irq);
  675         ACPI_SERIAL_END(pci_link);
  676 }
  677 
  678 static ACPI_STATUS
  679 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
  680 {
  681         ACPI_RESOURCE *resource, *end, newres, *resptr;
  682         ACPI_BUFFER crsbuf;
  683         ACPI_STATUS status;
  684         struct link *link;
  685         int i, in_dpf;
  686 
  687         /* Fetch the _CRS. */
  688         ACPI_SERIAL_ASSERT(pci_link);
  689         crsbuf.Pointer = NULL;
  690         crsbuf.Length = ACPI_ALLOCATE_BUFFER;
  691         status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), &crsbuf);
  692         if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL)
  693                 status = AE_NO_MEMORY;
  694         if (ACPI_FAILURE(status)) {
  695                 if (bootverbose)
  696                         device_printf(sc->pl_dev,
  697                             "Unable to fetch current resources: %s\n",
  698                             AcpiFormatException(status));
  699                 return (status);
  700         }
  701 
  702         /* Fill in IRQ resources via link structures. */
  703         srsbuf->Pointer = NULL;
  704         link = sc->pl_links;
  705         i = 0;
  706         in_dpf = DPF_OUTSIDE;
  707         resource = (ACPI_RESOURCE *)crsbuf.Pointer;
  708         end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
  709         for (;;) {
  710                 switch (resource->Id) {
  711                 case ACPI_RSTYPE_START_DPF:
  712                         switch (in_dpf) {
  713                         case DPF_OUTSIDE:
  714                                 /* We've started the first DPF. */
  715                                 in_dpf = DPF_FIRST;
  716                                 break;
  717                         case DPF_FIRST:
  718                                 /* We've started the second DPF. */
  719                                 panic(
  720                 "%s: Multiple dependent functions within a current resource",
  721                                     __func__);
  722                                 break;
  723                         }
  724                         resptr = NULL;
  725                         break;
  726                 case ACPI_RSTYPE_END_DPF:
  727                         /* We are finished with DPF parsing. */
  728                         KASSERT(in_dpf != DPF_OUTSIDE,
  729                             ("%s: end dpf when not parsing a dpf", __func__));
  730                         in_dpf = DPF_OUTSIDE;
  731                         resptr = NULL;
  732                         break;
  733                 case ACPI_RSTYPE_IRQ:
  734                         MPASS(i < sc->pl_num_links);
  735                         MPASS(link->l_prs_template.Id == ACPI_RSTYPE_IRQ);
  736                         newres = link->l_prs_template;
  737                         resptr = &newres;
  738                         resptr->Data.Irq.NumberOfInterrupts = 1;
  739                         if (PCI_INTERRUPT_VALID(link->l_irq)) {
  740                                 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
  741                 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
  742                                     __func__, link->l_irq));
  743                                 resptr->Data.Irq.Interrupts[0] = link->l_irq;
  744                         } else
  745                                 resptr->Data.Irq.Interrupts[0] = 0;
  746                         link++;
  747                         i++;
  748                         break;
  749                 case ACPI_RSTYPE_EXT_IRQ:
  750                         MPASS(i < sc->pl_num_links);
  751                         MPASS(link->l_prs_template.Id == ACPI_RSTYPE_EXT_IRQ);
  752                         newres = link->l_prs_template;
  753                         resptr = &newres;
  754                         resptr->Data.ExtendedIrq.NumberOfInterrupts = 1;
  755                         if (PCI_INTERRUPT_VALID(link->l_irq))
  756                                 resptr->Data.ExtendedIrq.Interrupts[0] =
  757                                     link->l_irq;
  758                         else
  759                                 resptr->Data.ExtendedIrq.Interrupts[0] = 0;
  760                         link++;
  761                         i++;
  762                         break;
  763                 default:
  764                         resptr = resource;
  765                 }
  766                 if (resptr != NULL) {
  767                         status = acpi_AppendBufferResource(srsbuf, resptr);
  768                         if (ACPI_FAILURE(status)) {
  769                                 device_printf(sc->pl_dev,
  770                                     "Unable to build resources: %s\n",
  771                                     AcpiFormatException(status));
  772                                 if (srsbuf->Pointer != NULL)
  773                                         AcpiOsFree(srsbuf->Pointer);
  774                                 AcpiOsFree(crsbuf.Pointer);
  775                                 return (status);
  776                         }
  777                 }
  778                 if (resource->Id == ACPI_RSTYPE_END_TAG)
  779                         break;
  780                 resource = ACPI_NEXT_RESOURCE(resource);
  781                 if (resource >= end)
  782                         break;
  783         }
  784         AcpiOsFree(crsbuf.Pointer);
  785         return (AE_OK);
  786 }
  787 
  788 static ACPI_STATUS
  789 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
  790     ACPI_BUFFER *srsbuf)
  791 {
  792         ACPI_RESOURCE newres;
  793         ACPI_STATUS status;
  794         struct link *link;
  795         int i;
  796 
  797         /* Start off with an empty buffer. */
  798         srsbuf->Pointer = NULL;
  799         link = sc->pl_links;
  800         for (i = 0; i < sc->pl_num_links; i++) {
  801 
  802                 /* Add a new IRQ resource from each link. */
  803                 link = &sc->pl_links[i];
  804                 newres = link->l_prs_template;
  805                 if (newres.Id == ACPI_RSTYPE_IRQ) {
  806 
  807                         /* Build an IRQ resource. */
  808                         newres.Data.Irq.NumberOfInterrupts = 1;
  809                         if (PCI_INTERRUPT_VALID(link->l_irq)) {
  810                                 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
  811                 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
  812                                     __func__, link->l_irq));
  813                                 newres.Data.Irq.Interrupts[0] = link->l_irq;
  814                         } else
  815                                 newres.Data.Irq.Interrupts[0] = 0;
  816                 } else {
  817 
  818                         /* Build an ExtIRQ resuorce. */
  819                         newres.Data.ExtendedIrq.NumberOfInterrupts = 1;
  820                         if (PCI_INTERRUPT_VALID(link->l_irq))
  821                                 newres.Data.ExtendedIrq.Interrupts[0] =
  822                                     link->l_irq;
  823                         else
  824                                 newres.Data.ExtendedIrq.Interrupts[0] = 0;
  825                 }
  826 
  827                 /* Add the new resource to the end of the _SRS buffer. */
  828                 status = acpi_AppendBufferResource(srsbuf, &newres);
  829                 if (ACPI_FAILURE(status)) {
  830                         device_printf(sc->pl_dev,
  831                             "Unable to build resources: %s\n",
  832                             AcpiFormatException(status));
  833                         if (srsbuf->Pointer != NULL)
  834                                 AcpiOsFree(srsbuf->Pointer);
  835                         return (status);
  836                 }
  837         }
  838         return (AE_OK);
  839 }
  840 
  841 static ACPI_STATUS
  842 acpi_pci_link_route_irqs(device_t dev)
  843 {
  844         struct acpi_pci_link_softc *sc;
  845         ACPI_RESOURCE *resource, *end;
  846         ACPI_BUFFER srsbuf;
  847         ACPI_STATUS status;
  848         struct link *link;
  849         int i;
  850 
  851         ACPI_SERIAL_ASSERT(pci_link);
  852         sc = device_get_softc(dev);
  853         if (sc->pl_crs_bad)
  854                 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
  855         else
  856                 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
  857 
  858         /* Write out new resources via _SRS. */
  859         status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf);
  860         if (ACPI_FAILURE(status)) {
  861                 device_printf(dev, "Unable to route IRQs: %s\n",
  862                     AcpiFormatException(status));
  863                 AcpiOsFree(srsbuf.Pointer);
  864                 return (status);
  865         }
  866 
  867         /*
  868          * Perform acpi_config_intr() on each IRQ resource if it was just
  869          * routed for the first time.
  870          */
  871         link = sc->pl_links;
  872         i = 0;
  873         resource = (ACPI_RESOURCE *)srsbuf.Pointer;
  874         end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
  875         for (;;) {
  876                 if (resource->Id == ACPI_RSTYPE_END_TAG)
  877                         break;
  878                 switch (resource->Id) {
  879                 case ACPI_RSTYPE_IRQ:
  880                 case ACPI_RSTYPE_EXT_IRQ:
  881                         MPASS(i < sc->pl_num_links);
  882 
  883                         /*
  884                          * Only configure the interrupt and update the
  885                          * weights if this link has a valid IRQ and was
  886                          * previously unrouted.
  887                          */
  888                         if (!link->l_routed &&
  889                             PCI_INTERRUPT_VALID(link->l_irq)) {
  890                                 link->l_routed = TRUE;
  891                                 acpi_config_intr(dev, resource);
  892                                 pci_link_interrupt_weights[link->l_irq] +=
  893                                     link->l_references;
  894                         }
  895                         link++;
  896                         i++;
  897                         break;
  898                 }
  899                 resource = ACPI_NEXT_RESOURCE(resource);
  900                 if (resource >= end)
  901                         break;
  902         }
  903         AcpiOsFree(srsbuf.Pointer);
  904         return (AE_OK);
  905 }
  906 
  907 static int
  908 acpi_pci_link_resume(device_t dev)
  909 {
  910         struct acpi_pci_link_softc *sc;
  911         ACPI_STATUS status;
  912         int i, routed;
  913 
  914         /*
  915          * If all of our links are routed, then restore the link via _SRS,
  916          * otherwise, disable the link via _DIS.
  917          */
  918         ACPI_SERIAL_BEGIN(pci_link);
  919         sc = device_get_softc(dev);
  920         routed = 0;
  921         for (i = 0; i < sc->pl_num_links; i++)
  922                 if (sc->pl_links[i].l_routed)
  923                         routed++;
  924         if (routed == sc->pl_num_links)
  925                 status = acpi_pci_link_route_irqs(dev);
  926         else {
  927                 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
  928                 status = AE_OK;
  929         }
  930         ACPI_SERIAL_END(pci_link);
  931         if (ACPI_FAILURE(status))
  932                 return (ENXIO);
  933         else
  934                 return (0);
  935 }
  936 
  937 /*
  938  * Pick an IRQ to use for this unrouted link.
  939  */
  940 static uint8_t
  941 acpi_pci_link_choose_irq(device_t dev, struct link *link)
  942 {
  943         char tunable_buffer[64], link_name[5];
  944         u_int8_t best_irq, pos_irq;
  945         int best_weight, pos_weight, i;
  946 
  947         KASSERT(!link->l_routed, ("%s: link already routed", __func__));
  948         KASSERT(!PCI_INTERRUPT_VALID(link->l_irq),
  949             ("%s: link already has an IRQ", __func__));
  950 
  951         /* Check for a tunable override and use it if it is valid. */
  952         if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name,
  953             sizeof(link_name)))) {
  954                 snprintf(tunable_buffer, sizeof(tunable_buffer),
  955                     "hw.pci.link.%s.%d.irq", link_name, link->l_res_index);
  956                 if (getenv_int(tunable_buffer, &i) &&
  957                     PCI_INTERRUPT_VALID(i) && link_valid_irq(link, i))
  958                         return (i);
  959                 snprintf(tunable_buffer, sizeof(tunable_buffer),
  960                     "hw.pci.link.%s.irq", link_name);
  961                 if (getenv_int(tunable_buffer, &i) &&
  962                     PCI_INTERRUPT_VALID(i) && link_valid_irq(link, i))
  963                         return (i);
  964         }
  965 
  966         /*
  967          * If we have a valid BIOS IRQ, use that.  We trust what the BIOS
  968          * says it routed over what _CRS says the link thinks is routed.
  969          */
  970         if (PCI_INTERRUPT_VALID(link->l_bios_irq))
  971                 return (link->l_bios_irq);
  972 
  973         /*
  974          * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
  975          * then use that.
  976          */
  977         if (PCI_INTERRUPT_VALID(link->l_initial_irq))
  978                 return (link->l_initial_irq);
  979 
  980         /*
  981          * Ok, we have no useful hints, so we have to pick from the
  982          * possible IRQs.  For ISA IRQs we only use interrupts that
  983          * have already been used by the BIOS.
  984          */
  985         best_irq = PCI_INVALID_IRQ;
  986         best_weight = INT_MAX;
  987         for (i = 0; i < link->l_num_irqs; i++) {
  988                 pos_irq = link->l_irqs[i];
  989                 if (pos_irq < NUM_ISA_INTERRUPTS &&
  990                     (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
  991                         continue;
  992                 pos_weight = pci_link_interrupt_weights[pos_irq];
  993                 if (pos_weight < best_weight) {
  994                         best_weight = pos_weight;
  995                         best_irq = pos_irq;
  996                 }
  997         }
  998 
  999         /*
 1000          * If this is an ISA IRQ, try using the SCI if it is also an ISA
 1001          * interrupt as a fallback.
 1002          */
 1003         if (link->l_isa_irq) {
 1004                 pos_irq = AcpiGbl_FADT->SciInt;
 1005                 pos_weight = pci_link_interrupt_weights[pos_irq];
 1006                 if (pos_weight < best_weight) {
 1007                         best_weight = pos_weight;
 1008                         best_irq = pos_irq;
 1009                 }
 1010         }
 1011 
 1012         if (PCI_INTERRUPT_VALID(best_irq)) {
 1013                 if (bootverbose)
 1014                         device_printf(dev, "Picked IRQ %u with weight %d\n",
 1015                             best_irq, best_weight);
 1016         } else
 1017                 device_printf(dev, "Unable to choose an IRQ\n");
 1018         return (best_irq);
 1019 }
 1020 
 1021 int
 1022 acpi_pci_link_route_interrupt(device_t dev, int index)
 1023 {
 1024         struct link *link;
 1025 
 1026         if (acpi_disabled("pci_link"))
 1027                 return (PCI_INVALID_IRQ);
 1028 
 1029         ACPI_SERIAL_BEGIN(pci_link);
 1030         link = acpi_pci_link_lookup(dev, index);
 1031         if (link == NULL)
 1032                 panic("%s: apparently invalid index %d", __func__, index);
 1033 
 1034         /*
 1035          * If this link device is already routed to an interrupt, just return
 1036          * the interrupt it is routed to.
 1037          */
 1038         if (link->l_routed) {
 1039                 KASSERT(PCI_INTERRUPT_VALID(link->l_irq),
 1040                     ("%s: link is routed but has an invalid IRQ", __func__));
 1041                 ACPI_SERIAL_END(pci_link);
 1042                 return (link->l_irq);
 1043         }
 1044 
 1045         /* Choose an IRQ if we need one. */
 1046         if (!PCI_INTERRUPT_VALID(link->l_irq)) {
 1047                 link->l_irq = acpi_pci_link_choose_irq(dev, link);
 1048 
 1049                 /*
 1050                  * Try to route the interrupt we picked.  If it fails, then
 1051                  * assume the interrupt is not routed.
 1052                  */
 1053                 if (PCI_INTERRUPT_VALID(link->l_irq)) {
 1054                         acpi_pci_link_route_irqs(dev);
 1055                         if (!link->l_routed)
 1056                                 link->l_irq = PCI_INVALID_IRQ;
 1057                 }
 1058         }
 1059         ACPI_SERIAL_END(pci_link);
 1060 
 1061         return (link->l_irq);
 1062 }
 1063 
 1064 /*
 1065  * This is gross, but we abuse the identify routine to perform one-time
 1066  * SYSINIT() style initialization for the driver.
 1067  */
 1068 static void
 1069 acpi_pci_link_identify(driver_t *driver, device_t parent)
 1070 {
 1071 
 1072         /*
 1073          * If the SCI is an ISA IRQ, add it to the bitmask of known good
 1074          * ISA IRQs.
 1075          *
 1076          * XXX: If we are using the APIC, the SCI might have been
 1077          * rerouted to an APIC pin in which case this is invalid.  However,
 1078          * if we are using the APIC, we also shouldn't be having any PCI
 1079          * interrupts routed via ISA IRQs, so this is probably ok.
 1080          */
 1081         if (AcpiGbl_FADT->SciInt < NUM_ISA_INTERRUPTS)
 1082                 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT->SciInt);
 1083 }
 1084 
 1085 static device_method_t acpi_pci_link_methods[] = {
 1086         /* Device interface */
 1087         DEVMETHOD(device_identify,      acpi_pci_link_identify),
 1088         DEVMETHOD(device_probe,         acpi_pci_link_probe),
 1089         DEVMETHOD(device_attach,        acpi_pci_link_attach),
 1090         DEVMETHOD(device_resume,        acpi_pci_link_resume),
 1091 
 1092         {0, 0}
 1093 };
 1094 
 1095 static driver_t acpi_pci_link_driver = {
 1096         "pci_link",
 1097         acpi_pci_link_methods,
 1098         sizeof(struct acpi_pci_link_softc),
 1099 };
 1100 
 1101 static devclass_t pci_link_devclass;
 1102 
 1103 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
 1104     0);
 1105 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);

Cache object: 7734835b64d25e7041bacf604cc60805


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