]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/amd64/acpica/madt.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / amd64 / acpica / 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
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/include/acpi.h>
52 #include <contrib/dev/acpica/include/accommon.h>
53 #include <contrib/dev/acpica/include/actables.h>
54
55 #include <dev/acpica/acpivar.h>
56 #include <dev/pci/pcivar.h>
57
58 typedef void madt_entry_handler(ACPI_SUBTABLE_HEADER *entry, void *arg);
59
60 /* These two arrays are indexed by APIC IDs. */
61 struct ioapic_info {
62         void *io_apic;
63         UINT32 io_vector;
64 } ioapics[MAX_APIC_ID + 1];
65
66 struct lapic_info {
67         u_int la_enabled:1;
68         u_int la_acpi_id:8;
69 } lapics[MAX_APIC_ID + 1];
70
71 static int madt_found_sci_override;
72 static ACPI_TABLE_MADT *madt;
73 static vm_paddr_t madt_physaddr;
74 static vm_offset_t madt_length;
75
76 MALLOC_DEFINE(M_MADT, "madt_table", "ACPI MADT Table Items");
77
78 static enum intr_polarity interrupt_polarity(UINT16 IntiFlags, UINT8 Source);
79 static enum intr_trigger interrupt_trigger(UINT16 IntiFlags, UINT8 Source);
80 static int      madt_find_cpu(u_int acpi_id, u_int *apic_id);
81 static int      madt_find_interrupt(int intr, void **apic, u_int *pin);
82 static void     *madt_map(vm_paddr_t pa, int offset, vm_offset_t length);
83 static void     *madt_map_table(vm_paddr_t pa, int offset, const char *sig);
84 static void     madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg);
85 static void     madt_parse_interrupt_override(
86                     ACPI_MADT_INTERRUPT_OVERRIDE *intr);
87 static void     madt_parse_ints(ACPI_SUBTABLE_HEADER *entry,
88                     void *arg __unused);
89 static void     madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi);
90 static void     madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi);
91 static int      madt_probe(void);
92 static int      madt_probe_cpus(void);
93 static void     madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry,
94                     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: pages 0 and 1 are used to map in the header of each
115  * table found via the RSDT or XSDT and pages 2 to n are used to map
116  * in the RSDT or XSDT.  We have to use 2 pages for the table headers
117  * in case a header spans a page boundary.  The offset is in pages;
118  * the length is in bytes.
119  */
120 static void *
121 madt_map(vm_paddr_t pa, int offset, vm_offset_t length)
122 {
123         vm_offset_t va, off;
124         void *data;
125
126         off = pa & PAGE_MASK;
127         length = roundup(length + off, PAGE_SIZE);
128         pa = pa & PG_FRAME;
129         va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
130             (offset * PAGE_SIZE);
131         data = (void *)(va + off);
132         length -= PAGE_SIZE;
133         while (length > 0) {
134                 va += PAGE_SIZE;
135                 pa += PAGE_SIZE;
136                 length -= PAGE_SIZE;
137                 pmap_kenter(va, pa);
138                 invlpg(va);
139         }
140         return (data);
141 }
142
143 static void
144 madt_unmap(void *data, vm_offset_t length)
145 {
146         vm_offset_t va, off;
147
148         va = (vm_offset_t)data;
149         off = va & PAGE_MASK;
150         length = roundup(length + off, PAGE_SIZE);
151         va &= ~PAGE_MASK;
152         while (length > 0) {
153                 pmap_kremove(va);
154                 invlpg(va);
155                 va += PAGE_SIZE;
156                 length -= PAGE_SIZE;
157         }
158 }
159
160 static void *
161 madt_map_table(vm_paddr_t pa, int offset, const char *sig)
162 {
163         ACPI_TABLE_HEADER *header;
164         vm_offset_t length;
165         void *table;
166
167         header = madt_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
168         if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
169                 madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
170                 return (NULL);
171         }
172         length = header->Length;
173         madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
174         table = madt_map(pa, offset, length);
175         if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
176                 if (bootverbose)
177                         printf("MADT: Failed checksum for table %s\n", sig);
178                 madt_unmap(table, length);
179                 return (NULL);
180         }
181         return (table);
182 }
183
184 static void
185 madt_unmap_table(void *table)
186 {
187         ACPI_TABLE_HEADER *header;
188
189         header = (ACPI_TABLE_HEADER *)table;
190         madt_unmap(table, header->Length);
191 }
192
193 /*
194  * Look for an ACPI Multiple APIC Description Table ("APIC")
195  */
196 static int
197 madt_probe(void)
198 {
199         ACPI_PHYSICAL_ADDRESS rsdp_ptr;
200         ACPI_TABLE_RSDP *rsdp;
201         ACPI_TABLE_RSDT *rsdt;
202         ACPI_TABLE_XSDT *xsdt;
203         int i, count;
204
205         if (resource_disabled("acpi", 0))
206                 return (ENXIO);
207
208         /*
209          * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
210          * calls pmap_mapbios() to find the RSDP, we assume that we can use
211          * pmap_mapbios() to map the RSDP.
212          */
213         if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
214                 return (ENXIO);
215         rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
216         if (rsdp == NULL) {
217                 if (bootverbose)
218                         printf("MADT: Failed to map RSDP\n");
219                 return (ENXIO);
220         }
221
222         /*
223          * For ACPI >= 2.0, use the XSDT if it is available.
224          * Otherwise, use the RSDT.  We map the XSDT or RSDT at page 1
225          * in the crashdump area.  Page 0 is used to map in the
226          * headers of candidate ACPI tables.
227          */
228         if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
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((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
235                         if (bootverbose)
236                                 printf("MADT: RSDP failed extended checksum\n");
237                         return (ENXIO);
238                 }
239                 xsdt = madt_map_table(rsdp->XsdtPhysicalAddress, 2,
240                     ACPI_SIG_XSDT);
241                 if (xsdt == NULL) {
242                         if (bootverbose)
243                                 printf("MADT: Failed to map XSDT\n");
244                         return (ENXIO);
245                 }
246                 count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
247                     sizeof(UINT64);
248                 for (i = 0; i < count; i++)
249                         if (madt_probe_table(xsdt->TableOffsetEntry[i]))
250                                 break;
251                 madt_unmap_table(xsdt);
252         } else {
253                 rsdt = madt_map_table(rsdp->RsdtPhysicalAddress, 2,
254                     ACPI_SIG_RSDT);
255                 if (rsdt == NULL) {
256                         if (bootverbose)
257                                 printf("MADT: Failed to map RSDT\n");
258                         return (ENXIO);
259                 }
260                 count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
261                     sizeof(UINT32);
262                 for (i = 0; i < count; i++)
263                         if (madt_probe_table(rsdt->TableOffsetEntry[i]))
264                                 break;
265                 madt_unmap_table(rsdt);
266         }
267         pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
268         if (madt_physaddr == 0) {
269                 if (bootverbose)
270                         printf("MADT: No MADT table found\n");
271                 return (ENXIO);
272         }
273         if (bootverbose)
274                 printf("MADT: Found table at 0x%jx\n",
275                     (uintmax_t)madt_physaddr);
276
277         /*
278          * Verify that we can map the full table and that its checksum is
279          * correct, etc.
280          */
281         madt = madt_map_table(madt_physaddr, 0, ACPI_SIG_MADT);
282         if (madt == NULL)
283                 return (ENXIO);
284         madt_unmap_table(madt);
285         madt = NULL;
286
287         return (0);
288 }
289
290 /*
291  * See if a given ACPI table is the MADT.
292  */
293 static int
294 madt_probe_table(vm_paddr_t address)
295 {
296         ACPI_TABLE_HEADER *table;
297
298         table = madt_map(address, 0, sizeof(ACPI_TABLE_HEADER));
299         if (table == NULL) {
300                 if (bootverbose)
301                         printf("MADT: Failed to map table at 0x%jx\n",
302                             (uintmax_t)address);
303                 return (0);
304         }
305         if (bootverbose)
306                 printf("Table '%.4s' at 0x%jx\n", table->Signature,
307                     (uintmax_t)address);
308
309         if (strncmp(table->Signature, ACPI_SIG_MADT, ACPI_NAME_SIZE) != 0) {
310                 madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
311                 return (0);
312         }
313         madt_physaddr = address;
314         madt_length = table->Length;
315         madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
316         return (1);
317 }
318
319 /*
320  * Run through the MP table enumerating CPUs.
321  */
322 static int
323 madt_probe_cpus(void)
324 {
325
326         madt = madt_map_table(madt_physaddr, 0, ACPI_SIG_MADT);
327         KASSERT(madt != NULL, ("Unable to re-map MADT"));
328         madt_walk_table(madt_probe_cpus_handler, NULL);
329         madt_unmap_table(madt);
330         madt = NULL;
331         return (0);
332 }
333
334 /*
335  * Initialize the local APIC on the BSP.
336  */
337 static int
338 madt_setup_local(void)
339 {
340
341         madt = pmap_mapbios(madt_physaddr, madt_length);
342         lapic_init(madt->Address);
343         printf("ACPI APIC Table: <%.*s %.*s>\n",
344             (int)sizeof(madt->Header.OemId), madt->Header.OemId,
345             (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
346
347         /*
348          * We ignore 64-bit local APIC override entries.  Should we
349          * perhaps emit a warning here if we find one?
350          */
351         return (0);
352 }
353
354 /*
355  * Enumerate I/O APICs and setup interrupt sources.
356  */
357 static int
358 madt_setup_io(void)
359 {
360         void *ioapic;
361         u_int pin;
362         int i;
363
364         /* Try to initialize ACPI so that we can access the FADT. */
365         i = acpi_Startup();
366         if (ACPI_FAILURE(i)) {
367                 printf("MADT: ACPI Startup failed with %s\n",
368                     AcpiFormatException(i));
369                 printf("Try disabling either ACPI or apic support.\n");
370                 panic("Using MADT but ACPI doesn't work");
371         }
372                     
373         /* First, we run through adding I/O APIC's. */
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.SciInterrupt, &ioapic,
385                     &pin) != 0)
386                         printf("MADT: Could not find APIC for SCI IRQ %u\n",
387                             AcpiGbl_FADT.SciInterrupt);
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 <= MAX_APIC_ID; 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_TUNABLES - 1, SI_ORDER_FIRST,
414     madt_register, NULL);
415
416 /*
417  * Call the handler routine for each entry in the MADT table.
418  */
419 static void
420 madt_walk_table(madt_entry_handler *handler, void *arg)
421 {
422         ACPI_SUBTABLE_HEADER *entry;
423         u_char *p, *end;
424
425         end = (u_char *)(madt) + madt->Header.Length;
426         for (p = (u_char *)(madt + 1); p < end; ) {
427                 entry = (ACPI_SUBTABLE_HEADER *)p;
428                 handler(entry, arg);
429                 p += entry->Length;
430         }
431 }
432
433 static void
434 madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
435 {
436         ACPI_MADT_LOCAL_APIC *proc;
437         struct lapic_info *la;
438
439         switch (entry->Type) {
440         case ACPI_MADT_TYPE_LOCAL_APIC:
441                 /*
442                  * The MADT does not include a BSP flag, so we have to
443                  * let the MP code figure out which CPU is the BSP on
444                  * its own.
445                  */
446                 proc = (ACPI_MADT_LOCAL_APIC *)entry;
447                 if (bootverbose)
448                         printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
449                             proc->Id, proc->ProcessorId,
450                             (proc->LapicFlags & ACPI_MADT_ENABLED) ?
451                             "enabled" : "disabled");
452                 if (!(proc->LapicFlags & ACPI_MADT_ENABLED))
453                         break;
454                 if (proc->Id > MAX_APIC_ID)
455                         panic("%s: CPU ID %u too high", __func__, proc->Id);
456                 la = &lapics[proc->Id];
457                 KASSERT(la->la_enabled == 0,
458                     ("Duplicate local APIC ID %u", proc->Id));
459                 la->la_enabled = 1;
460                 la->la_acpi_id = proc->ProcessorId;
461                 lapic_create(proc->Id, 0);
462                 break;
463         }
464 }
465
466
467 /*
468  * Add an I/O APIC from an entry in the table.
469  */
470 static void
471 madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
472 {
473         ACPI_MADT_IO_APIC *apic;
474
475         switch (entry->Type) {
476         case ACPI_MADT_TYPE_IO_APIC:
477                 apic = (ACPI_MADT_IO_APIC *)entry;
478                 if (bootverbose)
479                         printf(
480                             "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
481                             apic->Id, apic->GlobalIrqBase,
482                             (void *)(uintptr_t)apic->Address);
483                 if (apic->Id > MAX_APIC_ID)
484                         panic("%s: I/O APIC ID %u too high", __func__,
485                             apic->Id);
486                 if (ioapics[apic->Id].io_apic != NULL)
487                         panic("%s: Double APIC ID %u", __func__, apic->Id);
488                 if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
489                         printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
490                         break;
491                 }
492                 ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
493                     apic->Id, apic->GlobalIrqBase);
494                 ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
495                 break;
496         default:
497                 break;
498         }
499 }
500
501 /*
502  * Determine properties of an interrupt source.  Note that for ACPI these
503  * functions are only used for ISA interrupts, so we assume ISA bus values
504  * (Active Hi, Edge Triggered) for conforming values except for the ACPI
505  * SCI for which we use Active Lo, Level Triggered.
506  */
507 static enum intr_polarity
508 interrupt_polarity(UINT16 IntiFlags, UINT8 Source)
509 {
510
511         switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
512         case ACPI_MADT_POLARITY_CONFORMS:
513                 if (Source == AcpiGbl_FADT.SciInterrupt)
514                         return (INTR_POLARITY_LOW);
515                 else
516                         return (INTR_POLARITY_HIGH);
517         case ACPI_MADT_POLARITY_ACTIVE_HIGH:
518                 return (INTR_POLARITY_HIGH);
519         case ACPI_MADT_POLARITY_ACTIVE_LOW:
520                 return (INTR_POLARITY_LOW);
521         default:
522                 panic("Bogus Interrupt Polarity");
523         }
524 }
525
526 static enum intr_trigger
527 interrupt_trigger(UINT16 IntiFlags, UINT8 Source)
528 {
529
530         switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
531         case ACPI_MADT_TRIGGER_CONFORMS:
532                 if (Source == AcpiGbl_FADT.SciInterrupt)
533                         return (INTR_TRIGGER_LEVEL);
534                 else
535                         return (INTR_TRIGGER_EDGE);
536         case ACPI_MADT_TRIGGER_EDGE:
537                 return (INTR_TRIGGER_EDGE);
538         case ACPI_MADT_TRIGGER_LEVEL:
539                 return (INTR_TRIGGER_LEVEL);
540         default:
541                 panic("Bogus Interrupt Trigger Mode");
542         }
543 }
544
545 /*
546  * Find the local APIC ID associated with a given ACPI Processor ID.
547  */
548 static int
549 madt_find_cpu(u_int acpi_id, u_int *apic_id)
550 {
551         int i;
552
553         for (i = 0; i <= MAX_APIC_ID; i++) {
554                 if (!lapics[i].la_enabled)
555                         continue;
556                 if (lapics[i].la_acpi_id != acpi_id)
557                         continue;
558                 *apic_id = i;
559                 return (0);
560         }
561         return (ENOENT);
562 }
563
564 /*
565  * Find the IO APIC and pin on that APIC associated with a given global
566  * interrupt.
567  */
568 static int
569 madt_find_interrupt(int intr, void **apic, u_int *pin)
570 {
571         int i, best;
572
573         best = -1;
574         for (i = 0; i <= MAX_APIC_ID; i++) {
575                 if (ioapics[i].io_apic == NULL ||
576                     ioapics[i].io_vector > intr)
577                         continue;
578                 if (best == -1 ||
579                     ioapics[best].io_vector < ioapics[i].io_vector)
580                         best = i;
581         }
582         if (best == -1)
583                 return (ENOENT);
584         *apic = ioapics[best].io_apic;
585         *pin = intr - ioapics[best].io_vector;
586         if (*pin > 32)
587                 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
588                     intr);
589         return (0);
590 }
591
592 /*
593  * Parse an interrupt source override for an ISA interrupt.
594  */
595 static void
596 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
597 {
598         void *new_ioapic, *old_ioapic;
599         u_int new_pin, old_pin;
600         enum intr_trigger trig;
601         enum intr_polarity pol;
602         char buf[64];
603
604         if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
605             intr->GlobalIrq == 2) {
606                 if (bootverbose)
607                         printf("MADT: Skipping timer override\n");
608                 return;
609         }
610         if (bootverbose)
611                 printf("MADT: Interrupt override: source %u, irq %u\n",
612                     intr->SourceIrq, intr->GlobalIrq);
613         KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
614         if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
615                 printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
616                     intr->GlobalIrq, intr->SourceIrq);
617                 return;
618         }
619
620         /*
621          * Lookup the appropriate trigger and polarity modes for this
622          * entry.
623          */
624         trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
625         pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
626         
627         /*
628          * If the SCI is identity mapped but has edge trigger and
629          * active-hi polarity or the force_sci_lo tunable is set,
630          * force it to use level/lo.
631          */
632         if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
633                 madt_found_sci_override = 1;
634                 if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
635                         if (tolower(buf[0]) == 'e')
636                                 trig = INTR_TRIGGER_EDGE;
637                         else if (tolower(buf[0]) == 'l')
638                                 trig = INTR_TRIGGER_LEVEL;
639                         else
640                                 panic(
641                                 "Invalid trigger %s: must be 'edge' or 'level'",
642                                     buf);
643                         printf("MADT: Forcing SCI to %s trigger\n",
644                             trig == INTR_TRIGGER_EDGE ? "edge" : "level");
645                 }
646                 if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
647                         if (tolower(buf[0]) == 'h')
648                                 pol = INTR_POLARITY_HIGH;
649                         else if (tolower(buf[0]) == 'l')
650                                 pol = INTR_POLARITY_LOW;
651                         else
652                                 panic(
653                                 "Invalid polarity %s: must be 'high' or 'low'",
654                                     buf);
655                         printf("MADT: Forcing SCI to active %s polarity\n",
656                             pol == INTR_POLARITY_HIGH ? "high" : "low");
657                 }
658         }
659
660         /* Remap the IRQ if it is mapped to a different interrupt vector. */
661         if (intr->SourceIrq != intr->GlobalIrq) {
662                 /*
663                  * If the SCI is remapped to a non-ISA global interrupt,
664                  * then override the vector we use to setup and allocate
665                  * the interrupt.
666                  */
667                 if (intr->GlobalIrq > 15 &&
668                     intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
669                         acpi_OverrideInterruptLevel(intr->GlobalIrq);
670                 else
671                         ioapic_remap_vector(new_ioapic, new_pin,
672                             intr->SourceIrq);
673                 if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
674                     &old_pin) != 0)
675                         printf("MADT: Could not find APIC for source IRQ %u\n",
676                             intr->SourceIrq);
677                 else if (ioapic_get_vector(old_ioapic, old_pin) ==
678                     intr->SourceIrq)
679                         ioapic_disable_pin(old_ioapic, old_pin);
680         }
681
682         /* Program the polarity and trigger mode. */
683         ioapic_set_triggermode(new_ioapic, new_pin, trig);
684         ioapic_set_polarity(new_ioapic, new_pin, pol);
685 }
686
687 /*
688  * Parse an entry for an NMI routed to an IO APIC.
689  */
690 static void
691 madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
692 {
693         void *ioapic;
694         u_int pin;
695
696         if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
697                 printf("MADT: Could not find APIC for vector %u\n",
698                     nmi->GlobalIrq);
699                 return;
700         }
701
702         ioapic_set_nmi(ioapic, pin);
703         if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
704                 ioapic_set_triggermode(ioapic, pin,
705                     interrupt_trigger(nmi->IntiFlags, 0));
706         if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
707                 ioapic_set_polarity(ioapic, pin,
708                     interrupt_polarity(nmi->IntiFlags, 0));
709 }
710
711 /*
712  * Parse an entry for an NMI routed to a local APIC LVT pin.
713  */
714 static void
715 madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
716 {
717         u_int apic_id, pin;
718
719         if (nmi->ProcessorId == 0xff)
720                 apic_id = APIC_ID_ALL;
721         else if (madt_find_cpu(nmi->ProcessorId, &apic_id) != 0) {
722                 if (bootverbose)
723                         printf("MADT: Ignoring local NMI routed to "
724                             "ACPI CPU %u\n", nmi->ProcessorId);
725                 return;
726         }
727         if (nmi->Lint == 0)
728                 pin = LVT_LINT0;
729         else
730                 pin = LVT_LINT1;
731         lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
732         if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
733                 lapic_set_lvt_triggermode(apic_id, pin,
734                     interrupt_trigger(nmi->IntiFlags, 0));
735         if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
736                 lapic_set_lvt_polarity(apic_id, pin,
737                     interrupt_polarity(nmi->IntiFlags, 0));
738 }
739
740 /*
741  * Parse interrupt entries.
742  */
743 static void
744 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
745 {
746
747         switch (entry->Type) {
748         case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
749                 madt_parse_interrupt_override(
750                         (ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
751                 break;
752         case ACPI_MADT_TYPE_NMI_SOURCE:
753                 madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
754                 break;
755         case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
756                 madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
757                 break;
758         }
759 }
760
761 /*
762  * Setup per-CPU ACPI IDs.
763  */
764 static void
765 madt_set_ids(void *dummy)
766 {
767         struct lapic_info *la;
768         struct pcpu *pc;
769         u_int i;
770
771         if (madt == NULL)
772                 return;
773         for (i = 0; i < MAXCPU; i++) {
774                 if (CPU_ABSENT(i))
775                         continue;
776                 pc = pcpu_find(i);
777                 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
778                 la = &lapics[pc->pc_apic_id];
779                 if (!la->la_enabled)
780                         panic("APIC: CPU with APIC ID %u is not enabled",
781                             pc->pc_apic_id);
782                 pc->pc_acpi_id = la->la_acpi_id;
783                 if (bootverbose)
784                         printf("APIC: CPU %u has ACPI ID %u\n", i,
785                             la->la_acpi_id);
786         }
787 }
788 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_ANY, madt_set_ids, NULL);