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/acpica/madt.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) 2003 John Baldwin <jhb@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  * 3. Neither the name of the author nor the names of any co-contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * 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/smp.h>
   39 
   40 #include <vm/vm.h>
   41 #include <vm/vm_param.h>
   42 #include <vm/pmap.h>
   43 
   44 #include <machine/apicreg.h>
   45 #include <machine/frame.h>
   46 #include <machine/intr_machdep.h>
   47 #include <machine/apicvar.h>
   48 #include <machine/md_var.h>
   49 #include <machine/specialreg.h>
   50 
   51 #include <contrib/dev/acpica/acpi.h>
   52 #include <contrib/dev/acpica/actables.h>
   53 #include <dev/acpica/acpivar.h>
   54 #include <dev/pci/pcivar.h>
   55 
   56 typedef void madt_entry_handler(APIC_HEADER *entry, void *arg);
   57 
   58 /* These two arrays are indexed by APIC IDs. */
   59 struct ioapic_info {
   60         void *io_apic;
   61         UINT32 io_vector;
   62 } ioapics[MAX_APIC_ID + 1];
   63 
   64 struct lapic_info {
   65         u_int la_enabled:1;
   66         u_int la_acpi_id:8;
   67 } lapics[MAX_APIC_ID + 1];
   68 
   69 static int madt_found_sci_override;
   70 static MULTIPLE_APIC_TABLE *madt;
   71 static vm_paddr_t madt_physaddr;
   72 static vm_offset_t madt_length;
   73 
   74 MALLOC_DEFINE(M_MADT, "MADT Table", "ACPI MADT Table Items");
   75 
   76 static enum intr_polarity interrupt_polarity(UINT16 Polarity, UINT8 Source);
   77 static enum intr_trigger interrupt_trigger(UINT16 TriggerMode, UINT8 Source);
   78 static int      madt_find_cpu(u_int acpi_id, u_int *apic_id);
   79 static int      madt_find_interrupt(int intr, void **apic, u_int *pin);
   80 static void     *madt_map(vm_paddr_t pa, int offset, vm_offset_t length);
   81 static void     *madt_map_table(vm_paddr_t pa, int offset, const char *sig);
   82 static void     madt_parse_apics(APIC_HEADER *entry, void *arg);
   83 static void     madt_parse_interrupt_override(MADT_INTERRUPT_OVERRIDE *intr);
   84 static void     madt_parse_ints(APIC_HEADER *entry, void *arg __unused);
   85 static void     madt_parse_local_nmi(MADT_LOCAL_APIC_NMI *nmi);
   86 static void     madt_parse_nmi(MADT_NMI_SOURCE *nmi);
   87 static int      madt_probe(void);
   88 static int      madt_probe_cpus(void);
   89 static void     madt_probe_cpus_handler(APIC_HEADER *entry, void *arg __unused);
   90 static int      madt_probe_table(vm_paddr_t address);
   91 static void     madt_register(void *dummy);
   92 static int      madt_setup_local(void);
   93 static int      madt_setup_io(void);
   94 static void     madt_unmap(void *data, vm_offset_t length);
   95 static void     madt_unmap_table(void *table);
   96 static void     madt_walk_table(madt_entry_handler *handler, void *arg);
   97 
   98 static struct apic_enumerator madt_enumerator = {
   99         "MADT",
  100         madt_probe,
  101         madt_probe_cpus,
  102         madt_setup_local,
  103         madt_setup_io
  104 };
  105 
  106 /*
  107  * Code to abuse the crashdump map to map in the tables for the early
  108  * probe.  We cheat and make the following assumptions about how we
  109  * use this KVA: pages 0 and 1 are used to map in the header of each
  110  * table found via the RSDT or XSDT and pages 2 to n are used to map
  111  * in the RSDT or XSDT.  We have to use 2 pages for the table headers
  112  * in case a header spans a page boundary.  The offset is in pages;
  113  * the length is in bytes.
  114  */
  115 static void *
  116 madt_map(vm_paddr_t pa, int offset, vm_offset_t length)
  117 {
  118         vm_offset_t va, off;
  119         void *data;
  120 
  121         off = pa & PAGE_MASK;
  122         length = roundup(length + off, PAGE_SIZE);
  123         pa = pa & PG_FRAME;
  124         va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
  125             (offset * PAGE_SIZE);
  126         data = (void *)(va + off);
  127         length -= PAGE_SIZE;
  128         while (length > 0) {
  129                 va += PAGE_SIZE;
  130                 pa += PAGE_SIZE;
  131                 length -= PAGE_SIZE;
  132                 pmap_kenter(va, pa);
  133                 invlpg(va);
  134         }
  135         return (data);
  136 }
  137 
  138 static void
  139 madt_unmap(void *data, vm_offset_t length)
  140 {
  141         vm_offset_t va, off;
  142 
  143         va = (vm_offset_t)data;
  144         off = va & PAGE_MASK;
  145         length = roundup(length + off, PAGE_SIZE);
  146         va &= ~PAGE_MASK;
  147         while (length > 0) {
  148                 pmap_kremove(va);
  149                 invlpg(va);
  150                 va += PAGE_SIZE;
  151                 length -= PAGE_SIZE;
  152         }
  153 }
  154 
  155 static void *
  156 madt_map_table(vm_paddr_t pa, int offset, const char *sig)
  157 {
  158         ACPI_TABLE_HEADER *header;
  159         vm_offset_t length;
  160         void *table;
  161 
  162         header = madt_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
  163         if (strncmp(header->Signature, sig, 4) != 0) {
  164                 madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
  165                 return (NULL);
  166         }
  167         length = header->Length;
  168         madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
  169         table = madt_map(pa, offset, length);
  170         if (ACPI_FAILURE(AcpiTbVerifyTableChecksum(table))) {
  171                 if (bootverbose)
  172                         printf("MADT: Failed checksum for table %s\n", sig);
  173                 madt_unmap(table, length);
  174                 return (NULL);
  175         }
  176         return (table);
  177 }
  178 
  179 static void
  180 madt_unmap_table(void *table)
  181 {
  182         ACPI_TABLE_HEADER *header;
  183 
  184         header = (ACPI_TABLE_HEADER *)table;
  185         madt_unmap(table, header->Length);
  186 }
  187 
  188 /*
  189  * Look for an ACPI Multiple APIC Description Table ("APIC")
  190  */
  191 static int
  192 madt_probe(void)
  193 {
  194         ACPI_POINTER rsdp_ptr;
  195         RSDP_DESCRIPTOR *rsdp;
  196         RSDT_DESCRIPTOR *rsdt;
  197         XSDT_DESCRIPTOR *xsdt;
  198         int i, count;
  199 
  200         if (resource_disabled("acpi", 0))
  201                 return (ENXIO);
  202 
  203         /*
  204          * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
  205          * calls pmap_mapbios() to find the RSDP, we assume that we can use
  206          * pmap_mapbios() to map the RSDP.
  207          */
  208         if (AcpiOsGetRootPointer(ACPI_LOGICAL_ADDRESSING, &rsdp_ptr) != AE_OK)
  209                 return (ENXIO);
  210 #ifdef __i386__
  211         KASSERT(rsdp_ptr.Pointer.Physical < KERNLOAD, ("RSDP too high"));
  212 #endif
  213         rsdp = pmap_mapbios(rsdp_ptr.Pointer.Physical, sizeof(RSDP_DESCRIPTOR));
  214         if (rsdp == NULL) {
  215                 if (bootverbose)
  216                         printf("MADT: Failed to map RSDP\n");
  217                 return (ENXIO);
  218         }
  219 
  220         /*
  221          * For ACPI >= 2.0, use the XSDT if it is available.
  222          * Otherwise, use the RSDT.  We map the XSDT or RSDT at page 1
  223          * in the crashdump area.  Page 0 is used to map in the
  224          * headers of candidate ACPI tables.
  225          */
  226         if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
  227                 /*
  228                  * AcpiOsGetRootPointer only verifies the checksum for
  229                  * the version 1.0 portion of the RSDP.  Version 2.0 has
  230                  * an additional checksum that we verify first.
  231                  */
  232                 if (AcpiTbChecksum(rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0) {
  233                         if (bootverbose)
  234                                 printf("MADT: RSDP failed extended checksum\n");
  235                         return (ENXIO);
  236                 }
  237                 xsdt = madt_map_table(rsdp->XsdtPhysicalAddress, 2, XSDT_SIG);
  238                 if (xsdt == NULL) {
  239                         if (bootverbose)
  240                                 printf("MADT: Failed to map XSDT\n");
  241                         return (ENXIO);
  242                 }
  243                 count = (xsdt->Length - sizeof(ACPI_TABLE_HEADER)) /
  244                     sizeof(UINT64);
  245                 for (i = 0; i < count; i++)
  246                         if (madt_probe_table(xsdt->TableOffsetEntry[i]))
  247                                 break;
  248                 madt_unmap_table(xsdt);
  249         } else {
  250                 rsdt = madt_map_table(rsdp->RsdtPhysicalAddress, 2, RSDT_SIG);
  251                 if (rsdt == NULL) {
  252                         if (bootverbose)
  253                                 printf("MADT: Failed to map RSDT\n");
  254                         return (ENXIO);
  255                 }
  256                 count = (rsdt->Length - sizeof(ACPI_TABLE_HEADER)) /
  257                     sizeof(UINT32);
  258                 for (i = 0; i < count; i++)
  259                         if (madt_probe_table(rsdt->TableOffsetEntry[i]))
  260                                 break;
  261                 madt_unmap_table(rsdt);
  262         }
  263         pmap_unmapbios((vm_offset_t)rsdp, sizeof(RSDP_DESCRIPTOR));
  264         if (madt_physaddr == 0) {
  265                 if (bootverbose)
  266                         printf("MADT: No MADT table found\n");
  267                 return (ENXIO);
  268         }
  269         if (bootverbose)
  270                 printf("MADT: Found table at 0x%jx\n",
  271                     (uintmax_t)madt_physaddr);
  272 
  273         /*
  274          * Verify that we can map the full table and that its checksum is
  275          * correct, etc.
  276          */
  277         madt = madt_map_table(madt_physaddr, 0, APIC_SIG);
  278         if (madt == NULL)
  279                 return (ENXIO);
  280         madt_unmap_table(madt);
  281         madt = NULL;
  282 
  283         return (0);
  284 }
  285 
  286 /*
  287  * See if a given ACPI table is the MADT.
  288  */
  289 static int
  290 madt_probe_table(vm_paddr_t address)
  291 {
  292         ACPI_TABLE_HEADER *table;
  293 
  294         table = madt_map(address, 0, sizeof(ACPI_TABLE_HEADER));
  295         if (table == NULL) {
  296                 if (bootverbose)
  297                         printf("MADT: Failed to map table at 0x%jx\n",
  298                             (uintmax_t)address);
  299                 return (0);
  300         }
  301         if (bootverbose)
  302                 printf("Table '%.4s' at 0x%jx\n", table->Signature,
  303                     (uintmax_t)address);
  304 
  305         if (strncmp(table->Signature, APIC_SIG, 4) != 0) {
  306                 madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
  307                 return (0);
  308         }
  309         madt_physaddr = address;
  310         madt_length = table->Length;
  311         madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
  312         return (1);
  313 }
  314 
  315 /*
  316  * Run through the MP table enumerating CPUs.
  317  */
  318 static int
  319 madt_probe_cpus(void)
  320 {
  321 
  322         madt = madt_map_table(madt_physaddr, 0, APIC_SIG);
  323         KASSERT(madt != NULL, ("Unable to re-map MADT"));
  324         madt_walk_table(madt_probe_cpus_handler, NULL);
  325         madt_unmap_table(madt);
  326         madt = NULL;
  327         return (0);
  328 }
  329 
  330 /*
  331  * Initialize the local APIC on the BSP.
  332  */
  333 static int
  334 madt_setup_local(void)
  335 {
  336 
  337         madt = pmap_mapbios(madt_physaddr, madt_length);
  338         lapic_init((uintptr_t)madt->LocalApicAddress);
  339         printf("ACPI APIC Table: <%.*s %.*s>\n",
  340             (int)sizeof(madt->OemId), madt->OemId,
  341             (int)sizeof(madt->OemTableId), madt->OemTableId);
  342 
  343         /*
  344          * We ignore 64-bit local APIC override entries.  Should we
  345          * perhaps emit a warning here if we find one?
  346          */
  347         return (0);
  348 }
  349 
  350 /*
  351  * Enumerate I/O APICs and setup interrupt sources.
  352  */
  353 static int
  354 madt_setup_io(void)
  355 {
  356         void *ioapic;
  357         u_int pin;
  358         int i;
  359 
  360         /* Try to initialize ACPI so that we can access the FADT. */
  361         i = acpi_Startup();
  362         if (ACPI_FAILURE(i)) {
  363                 printf("MADT: ACPI Startup failed with %s\n",
  364                     AcpiFormatException(i));
  365                 printf("Try disabling either ACPI or apic support.\n");
  366                 panic("Using MADT but ACPI doesn't work");
  367         }
  368                     
  369         /* First, we run through adding I/O APIC's. */
  370         madt_walk_table(madt_parse_apics, NULL);
  371 
  372         /* Second, we run through the table tweaking interrupt sources. */
  373         madt_walk_table(madt_parse_ints, NULL);
  374 
  375         /*
  376          * If there was not an explicit override entry for the SCI,
  377          * force it to use level trigger and active-low polarity.
  378          */
  379         if (!madt_found_sci_override) {
  380                 if (madt_find_interrupt(AcpiGbl_FADT->SciInt, &ioapic, &pin)
  381                     != 0)
  382                         printf("MADT: Could not find APIC for SCI IRQ %d\n",
  383                             AcpiGbl_FADT->SciInt);
  384                 else {
  385                         printf(
  386         "MADT: Forcing active-low polarity and level trigger for SCI\n");
  387                         ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
  388                         ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
  389                 }
  390         }
  391 
  392         /* Third, we register all the I/O APIC's. */
  393         for (i = 0; i <= MAX_APIC_ID; i++)
  394                 if (ioapics[i].io_apic != NULL)
  395                         ioapic_register(ioapics[i].io_apic);
  396 
  397         /* Finally, we throw the switch to enable the I/O APIC's. */
  398         acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
  399 
  400         return (0);
  401 }
  402 
  403 static void
  404 madt_register(void *dummy __unused)
  405 {
  406 
  407         apic_register_enumerator(&madt_enumerator);
  408 }
  409 SYSINIT(madt_register, SI_SUB_CPU - 1, SI_ORDER_FIRST, madt_register, NULL)
  410 
  411 /*
  412  * Call the handler routine for each entry in the MADT table.
  413  */
  414 static void
  415 madt_walk_table(madt_entry_handler *handler, void *arg)
  416 {
  417         APIC_HEADER *entry;
  418         u_char *p, *end;
  419 
  420         end = (u_char *)(madt) + madt->Length;
  421         for (p = (u_char *)(madt + 1); p < end; ) {
  422                 entry = (APIC_HEADER *)p;
  423                 handler(entry, arg);
  424                 p += entry->Length;
  425         }
  426 }
  427 
  428 static void
  429 madt_probe_cpus_handler(APIC_HEADER *entry, void *arg)
  430 {
  431         MADT_PROCESSOR_APIC *proc;
  432         struct lapic_info *la;
  433 
  434         switch (entry->Type) {
  435         case APIC_PROCESSOR:
  436                 /*
  437                  * The MADT does not include a BSP flag, so we have to
  438                  * let the MP code figure out which CPU is the BSP on
  439                  * its own.
  440                  */
  441                 proc = (MADT_PROCESSOR_APIC *)entry;
  442                 if (bootverbose)
  443                         printf("MADT: Found CPU APIC ID %d ACPI ID %d: %s\n",
  444                             proc->LocalApicId, proc->ProcessorId,
  445                             proc->ProcessorEnabled ? "enabled" : "disabled");
  446                 if (!proc->ProcessorEnabled)
  447                         break;
  448                 if (proc->LocalApicId > MAX_APIC_ID)
  449                         panic("%s: CPU ID %d too high", __func__,
  450                             proc->LocalApicId);
  451                 la = &lapics[proc->LocalApicId];
  452                 KASSERT(la->la_enabled == 0,
  453                     ("Duplicate local APIC ID %d", proc->LocalApicId));
  454                 la->la_enabled = 1;
  455                 la->la_acpi_id = proc->ProcessorId;
  456                 lapic_create(proc->LocalApicId, 0);
  457                 break;
  458         }
  459 }
  460 
  461 
  462 /*
  463  * Add an I/O APIC from an entry in the table.
  464  */
  465 static void
  466 madt_parse_apics(APIC_HEADER *entry, void *arg __unused)
  467 {
  468         MADT_IO_APIC *apic;
  469 
  470         switch (entry->Type) {
  471         case APIC_IO:
  472                 apic = (MADT_IO_APIC *)entry;
  473                 if (bootverbose)
  474                         printf("MADT: Found IO APIC ID %d, Interrupt %d at %p\n",
  475                             apic->IoApicId, apic->Interrupt,
  476                             (void *)(uintptr_t)apic->Address);
  477                 if (apic->IoApicId > MAX_APIC_ID)
  478                         panic("%s: I/O APIC ID %d too high", __func__,
  479                             apic->IoApicId);
  480                 if (ioapics[apic->IoApicId].io_apic != NULL)
  481                         panic("%s: Double APIC ID %d", __func__,
  482                             apic->IoApicId);
  483                 ioapics[apic->IoApicId].io_apic = ioapic_create(
  484                         (uintptr_t)apic->Address, apic->IoApicId,
  485                             apic->Interrupt);
  486                 ioapics[apic->IoApicId].io_vector = apic->Interrupt;
  487                 break;
  488         default:
  489                 break;
  490         }
  491 }
  492 
  493 /*
  494  * Determine properties of an interrupt source.  Note that for ACPI these
  495  * functions are only used for ISA interrupts, so we assume ISA bus values
  496  * (Active Hi, Edge Triggered) for conforming values except for the ACPI
  497  * SCI for which we use Active Lo, Level Triggered.
  498  */
  499 static enum intr_polarity
  500 interrupt_polarity(UINT16 Polarity, UINT8 Source)
  501 {
  502 
  503         switch (Polarity) {
  504         case POLARITY_CONFORMS:
  505                 if (Source == AcpiGbl_FADT->SciInt)
  506                         return (INTR_POLARITY_LOW);
  507                 else
  508                         return (INTR_POLARITY_HIGH);
  509         case POLARITY_ACTIVE_HIGH:
  510                 return (INTR_POLARITY_HIGH);
  511         case POLARITY_ACTIVE_LOW:
  512                 return (INTR_POLARITY_LOW);
  513         default:
  514                 panic("Bogus Interrupt Polarity");
  515         }
  516 }
  517 
  518 static enum intr_trigger
  519 interrupt_trigger(UINT16 TriggerMode, UINT8 Source)
  520 {
  521 
  522         switch (TriggerMode) {
  523         case TRIGGER_CONFORMS:
  524                 if (Source == AcpiGbl_FADT->SciInt)
  525                         return (INTR_TRIGGER_LEVEL);
  526                 else
  527                         return (INTR_TRIGGER_EDGE);
  528         case TRIGGER_EDGE:
  529                 return (INTR_TRIGGER_EDGE);
  530         case TRIGGER_LEVEL:
  531                 return (INTR_TRIGGER_LEVEL);
  532         default:
  533                 panic("Bogus Interrupt Trigger Mode");
  534         }
  535 }
  536 
  537 /*
  538  * Find the local APIC ID associated with a given ACPI Processor ID.
  539  */
  540 static int
  541 madt_find_cpu(u_int acpi_id, u_int *apic_id)
  542 {
  543         int i;
  544 
  545         for (i = 0; i <= MAX_APIC_ID; i++) {
  546                 if (!lapics[i].la_enabled)
  547                         continue;
  548                 if (lapics[i].la_acpi_id != acpi_id)
  549                         continue;
  550                 *apic_id = i;
  551                 return (0);
  552         }
  553         return (ENOENT);
  554 }
  555 
  556 /*
  557  * Find the IO APIC and pin on that APIC associated with a given global
  558  * interrupt.
  559  */
  560 static int
  561 madt_find_interrupt(int intr, void **apic, u_int *pin)
  562 {
  563         int i, best;
  564 
  565         best = -1;
  566         for (i = 0; i <= MAX_APIC_ID; i++) {
  567                 if (ioapics[i].io_apic == NULL ||
  568                     ioapics[i].io_vector > intr)
  569                         continue;
  570                 if (best == -1 ||
  571                     ioapics[best].io_vector < ioapics[i].io_vector)
  572                         best = i;
  573         }
  574         if (best == -1)
  575                 return (ENOENT);
  576         *apic = ioapics[best].io_apic;
  577         *pin = intr - ioapics[best].io_vector;
  578         if (*pin > 32)
  579                 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
  580                     intr);
  581         return (0);
  582 }
  583 
  584 /*
  585  * Parse an interrupt source override for an ISA interrupt.
  586  */
  587 static void
  588 madt_parse_interrupt_override(MADT_INTERRUPT_OVERRIDE *intr)
  589 {
  590         void *new_ioapic, *old_ioapic;
  591         u_int new_pin, old_pin;
  592         enum intr_trigger trig;
  593         enum intr_polarity pol;
  594         char buf[64];
  595 
  596         if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->Source == 0 &&
  597             intr->Interrupt == 2) {
  598                 if (bootverbose)
  599                         printf("MADT: Skipping timer override\n");
  600                 return;
  601         }
  602         if (bootverbose)
  603                 printf("MADT: Interrupt override: source %u, irq %u\n",
  604                     intr->Source, intr->Interrupt);
  605         KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
  606         if (madt_find_interrupt(intr->Interrupt, &new_ioapic,
  607             &new_pin) != 0) {
  608                 printf("MADT: Could not find APIC for vector %d (IRQ %d)\n",
  609                     intr->Interrupt, intr->Source);
  610                 return;
  611         }
  612 
  613         /*
  614          * Lookup the appropriate trigger and polarity modes for this
  615          * entry.
  616          */
  617         trig = interrupt_trigger(intr->TriggerMode, intr->Source);
  618         pol = interrupt_polarity(intr->Polarity, intr->Source);
  619         
  620         /*
  621          * If the SCI is identity mapped but has edge trigger and
  622          * active-hi polarity or the force_sci_lo tunable is set,
  623          * force it to use level/lo.
  624          */
  625         if (intr->Source == AcpiGbl_FADT->SciInt) {
  626                 madt_found_sci_override = 1;
  627                 if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
  628                         if (tolower(buf[0]) == 'e')
  629                                 trig = INTR_TRIGGER_EDGE;
  630                         else if (tolower(buf[0]) == 'l')
  631                                 trig = INTR_TRIGGER_LEVEL;
  632                         else
  633                                 panic(
  634                                 "Invalid trigger %s: must be 'edge' or 'level'",
  635                                     buf);
  636                         printf("MADT: Forcing SCI to %s trigger\n",
  637                             trig == INTR_TRIGGER_EDGE ? "edge" : "level");
  638                 }
  639                 if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
  640                         if (tolower(buf[0]) == 'h')
  641                                 pol = INTR_POLARITY_HIGH;
  642                         else if (tolower(buf[0]) == 'l')
  643                                 pol = INTR_POLARITY_LOW;
  644                         else
  645                                 panic(
  646                                 "Invalid polarity %s: must be 'high' or 'low'",
  647                                     buf);
  648                         printf("MADT: Forcing SCI to active %s polarity\n",
  649                             pol == INTR_POLARITY_HIGH ? "high" : "low");
  650                 }
  651         }
  652 
  653         /* Remap the IRQ if it is mapped to a different interrupt vector. */
  654         if (intr->Source != intr->Interrupt) {
  655                 /*
  656                  * If the SCI is remapped to a non-ISA global interrupt,
  657                  * then override the vector we use to setup and allocate
  658                  * the interrupt.
  659                  */
  660                 if (intr->Interrupt > 15 &&
  661                     intr->Source == AcpiGbl_FADT->SciInt)
  662                         acpi_OverrideInterruptLevel(intr->Interrupt);
  663                 else
  664                         ioapic_remap_vector(new_ioapic, new_pin, intr->Source);
  665                 if (madt_find_interrupt(intr->Source, &old_ioapic,
  666                     &old_pin) != 0)
  667                         printf("MADT: Could not find APIC for source IRQ %d\n",
  668                             intr->Source);
  669                 else if (ioapic_get_vector(old_ioapic, old_pin) ==
  670                     intr->Source)
  671                         ioapic_disable_pin(old_ioapic, old_pin);
  672         }
  673 
  674         /* Program the polarity and trigger mode. */
  675         ioapic_set_triggermode(new_ioapic, new_pin, trig);
  676         ioapic_set_polarity(new_ioapic, new_pin, pol);
  677 }
  678 
  679 /*
  680  * Parse an entry for an NMI routed to an IO APIC.
  681  */
  682 static void
  683 madt_parse_nmi(MADT_NMI_SOURCE *nmi)
  684 {
  685         void *ioapic;
  686         u_int pin;
  687 
  688         if (madt_find_interrupt(nmi->Interrupt, &ioapic, &pin) != 0) {
  689                 printf("MADT: Could not find APIC for vector %d\n",
  690                     nmi->Interrupt);
  691                 return;
  692         }
  693 
  694         ioapic_set_nmi(ioapic, pin);
  695         if (nmi->TriggerMode != TRIGGER_CONFORMS)
  696                 ioapic_set_triggermode(ioapic, pin,
  697                     interrupt_trigger(nmi->TriggerMode, 0));
  698         if (nmi->Polarity != TRIGGER_CONFORMS)
  699                 ioapic_set_polarity(ioapic, pin,
  700                     interrupt_polarity(nmi->Polarity, 0));
  701 }
  702 
  703 /*
  704  * Parse an entry for an NMI routed to a local APIC LVT pin.
  705  */
  706 static void
  707 madt_parse_local_nmi(MADT_LOCAL_APIC_NMI *nmi)
  708 {
  709         u_int apic_id, pin;
  710 
  711         if (nmi->ProcessorId == 0xff)
  712                 apic_id = APIC_ID_ALL;
  713         else if (madt_find_cpu(nmi->ProcessorId, &apic_id) != 0) {
  714                 if (bootverbose)
  715                         printf("MADT: Ignoring local NMI routed to ACPI CPU %u\n",
  716                             nmi->ProcessorId);
  717                 return;
  718         }
  719         if (nmi->Lint == 0)
  720                 pin = LVT_LINT0;
  721         else
  722                 pin = LVT_LINT1;
  723         lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
  724         if (nmi->TriggerMode != TRIGGER_CONFORMS)
  725                 lapic_set_lvt_triggermode(apic_id, pin,
  726                     interrupt_trigger(nmi->TriggerMode, 0));
  727         if (nmi->Polarity != POLARITY_CONFORMS)
  728                 lapic_set_lvt_polarity(apic_id, pin,
  729                     interrupt_polarity(nmi->Polarity, 0));
  730 }
  731 
  732 /*
  733  * Parse interrupt entries.
  734  */
  735 static void
  736 madt_parse_ints(APIC_HEADER *entry, void *arg __unused)
  737 {
  738 
  739         switch (entry->Type) {
  740         case APIC_XRUPT_OVERRIDE:
  741                 madt_parse_interrupt_override(
  742                         (MADT_INTERRUPT_OVERRIDE *)entry);
  743                 break;
  744         case APIC_NMI:
  745                 madt_parse_nmi((MADT_NMI_SOURCE *)entry);
  746                 break;
  747         case APIC_LOCAL_NMI:
  748                 madt_parse_local_nmi((MADT_LOCAL_APIC_NMI *)entry);
  749                 break;
  750         }
  751 }
  752 
  753 /*
  754  * Setup per-CPU ACPI IDs.
  755  */
  756 static void
  757 madt_set_ids(void *dummy)
  758 {
  759         struct lapic_info *la;
  760         struct pcpu *pc;
  761         u_int i;
  762 
  763         if (madt == NULL)
  764                 return;
  765         for (i = 0; i <= mp_maxid; i++) {
  766                 if (CPU_ABSENT(i))
  767                         continue;
  768                 pc = pcpu_find(i);
  769                 KASSERT(pc != NULL, ("no pcpu data for CPU %d", i));
  770                 la = &lapics[pc->pc_apic_id];
  771                 if (!la->la_enabled)
  772                         panic("APIC: CPU with APIC ID %u is not enabled",
  773                             pc->pc_apic_id);
  774                 pc->pc_acpi_id = la->la_acpi_id;
  775                 if (bootverbose)
  776                         printf("APIC: CPU %u has ACPI ID %u\n", i,
  777                             la->la_acpi_id);
  778         }
  779 }
  780 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_ANY, madt_set_ids, NULL)

Cache object: e748eba462c8fe447a1defabe828412b


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