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