Merge from vendor branch GROFF:
[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.4 2005/06/27 22:32:00 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         rsdp = pmap_mapdev(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 RSDT.  For ACPI >= 2.0, use the XSDT.
222          * We map the XSDT and RSDT at page 1 in the crashdump area.
223          * Page 0 is used to map in the headers of candidate ACPI tables.
224          */
225         if (rsdp->Revision >= 2) {
226                 /*
227                  * AcpiOsGetRootPointer only verifies the checksum for
228                  * the version 1.0 portion of the RSDP.  Version 2.0 has
229                  * an additional checksum that we verify first.
230                  */
231                 if (AcpiTbChecksum(rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0) {
232                         if (bootverbose)
233                                 printf("MADT: RSDP failed extended checksum\n");
234                         return (ENXIO);
235                 }
236                 xsdt = madt_map_table(rsdp->XsdtPhysicalAddress, 1, XSDT_SIG);
237                 if (xsdt == NULL) {
238                         if (bootverbose)
239                                 printf("MADT: Failed to map XSDT\n");
240                         return (ENXIO);
241                 }
242                 count = (xsdt->Length - sizeof(ACPI_TABLE_HEADER)) /
243                     sizeof(UINT64);
244                 for (i = 0; i < count; i++)
245                         if (madt_probe_table(xsdt->TableOffsetEntry[i]))
246                                 break;
247                 madt_unmap_table(xsdt);
248         } else {
249                 rsdt = madt_map_table(rsdp->RsdtPhysicalAddress, 1, RSDT_SIG);
250                 if (rsdt == NULL) {
251                         if (bootverbose)
252                                 printf("MADT: Failed to map RSDT\n");
253                         return (ENXIO);
254                 }
255                 count = (rsdt->Length - sizeof(ACPI_TABLE_HEADER)) /
256                     sizeof(UINT32);
257                 for (i = 0; i < count; i++)
258                         if (madt_probe_table(rsdt->TableOffsetEntry[i]))
259                                 break;
260                 madt_unmap_table(rsdt);
261         }
262         pmap_unmapdev((vm_offset_t)rsdp, sizeof(RSDP_DESCRIPTOR));
263         if (madt_physaddr == 0) {
264                 if (bootverbose)
265                         printf("MADT: No MADT table found\n");
266                 return (ENXIO);
267         }
268         if (bootverbose)
269                 printf("MADT: Found table at 0x%jx\n",
270                     (uintmax_t)madt_physaddr);
271
272         /*
273          * Verify that we can map the full table and that its checksum is
274          * correct, etc.
275          */
276         madt = madt_map_table(madt_physaddr, 0, APIC_SIG);
277         if (madt == NULL)
278                 return (ENXIO);
279         madt_unmap_table(madt);
280         madt = NULL;
281
282         return (0);
283 }
284
285 /*
286  * See if a given ACPI table is the MADT.
287  */
288 static int
289 madt_probe_table(vm_paddr_t address)
290 {
291         ACPI_TABLE_HEADER *table;
292
293         table = madt_map(address, 0, sizeof(ACPI_TABLE_HEADER));
294         if (table == NULL) {
295                 if (bootverbose)
296                         printf("MADT: Failed to map table at 0x%jx\n",
297                             (uintmax_t)address);
298                 return (0);
299         }
300         if (bootverbose)
301                 printf("Table '%.4s' at 0x%jx\n", table->Signature,
302                     (uintmax_t)address);
303
304         if (strncmp(table->Signature, APIC_SIG, 4) != 0) {
305                 madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
306                 return (0);
307         }
308         madt_physaddr = address;
309         madt_length = table->Length;
310         madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
311         return (1);
312 }
313
314 /*
315  * Run through the MP table enumerating CPUs.
316  */
317 static int
318 madt_probe_cpus(void)
319 {
320
321         madt = madt_map_table(madt_physaddr, 0, APIC_SIG);
322         KASSERT(madt != NULL, ("Unable to re-map MADT"));
323         madt_walk_table(madt_probe_cpus_handler, NULL);
324         madt_unmap_table(madt);
325         madt = NULL;
326         return (0);
327 }
328
329 /*
330  * Initialize the local APIC on the BSP.
331  */
332 static int
333 madt_setup_local(void)
334 {
335
336         madt = pmap_mapdev(madt_physaddr, madt_length);
337         lapic_init((uintptr_t)madt->LocalApicAddress);
338         printf("ACPI APIC Table: <%.*s %.*s>\n",
339             (int)sizeof(madt->OemId), madt->OemId,
340             (int)sizeof(madt->OemTableId), madt->OemTableId);
341
342         /*
343          * We ignore 64-bit local APIC override entries.  Should we
344          * perhaps emit a warning here if we find one?
345          */
346         return (0);
347 }
348
349 /*
350  * Enumerate I/O APICs and setup interrupt sources.
351  */
352 static int
353 madt_setup_io(void)
354 {
355         void *ioapic;
356         u_int pin;
357         int i;
358
359         /* Try to initialize ACPI so that we can access the FADT. */
360         i = acpi_Startup();
361         if (ACPI_FAILURE(i)) {
362                 printf("MADT: ACPI Startup failed with %s\n",
363                     AcpiFormatException(i));
364                 printf("Try disabling either ACPI or apic support.\n");
365                 panic("Using MADT but ACPI doesn't work");
366         }
367                     
368         /* First, we run through adding I/O APIC's. */
369         if (madt->PCATCompat)
370                 ioapic_enable_mixed_mode();
371         madt_walk_table(madt_parse_apics, NULL);
372
373         /* Second, we run through the table tweaking interrupt sources. */
374         madt_walk_table(madt_parse_ints, NULL);
375
376         /*
377          * If there was not an explicit override entry for the SCI,
378          * force it to use level trigger and active-low polarity.
379          */
380         if (!madt_found_sci_override) {
381                 if (madt_find_interrupt(AcpiGbl_FADT->SciInt, &ioapic, &pin)
382                     != 0)
383                         printf("MADT: Could not find APIC for SCI IRQ %d\n",
384                             AcpiGbl_FADT->SciInt);
385                 else {
386                         printf(
387         "MADT: Forcing active-low polarity and level trigger for SCI\n");
388                         ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
389                         ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
390                 }
391         }
392
393         /* Third, we register all the I/O APIC's. */
394         for (i = 0; i < NIOAPICS; i++)
395                 if (ioapics[i].io_apic != NULL)
396                         ioapic_register(ioapics[i].io_apic);
397
398         /* Finally, we throw the switch to enable the I/O APIC's. */
399         acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
400
401         return (0);
402 }
403
404 static void
405 madt_register(void *dummy __unused)
406 {
407
408         apic_register_enumerator(&madt_enumerator);
409 }
410 SYSINIT(madt_register, SI_SUB_CPU - 1, SI_ORDER_FIRST, madt_register, NULL)
411
412 /*
413  * Call the handler routine for each entry in the MADT table.
414  */
415 static void
416 madt_walk_table(madt_entry_handler *handler, void *arg)
417 {
418         APIC_HEADER *entry;
419         u_char *p, *end;
420
421         end = (u_char *)(madt) + madt->Length;
422         for (p = (u_char *)(madt + 1); p < end; ) {
423                 entry = (APIC_HEADER *)p;
424                 handler(entry, arg);
425                 p += entry->Length;
426         }
427 }
428
429 static void
430 madt_probe_cpus_handler(APIC_HEADER *entry, void *arg)
431 {
432         MADT_PROCESSOR_APIC *proc;
433         struct lapic_info *la;
434
435         switch (entry->Type) {
436         case APIC_PROCESSOR:
437                 /*
438                  * The MADT does not include a BSP flag, so we have to
439                  * let the MP code figure out which CPU is the BSP on
440                  * its own.
441                  */
442                 proc = (MADT_PROCESSOR_APIC *)entry;
443                 if (bootverbose)
444                         printf("MADT: Found CPU APIC ID %d ACPI ID %d: %s\n",
445                             proc->LocalApicId, proc->ProcessorId,
446                             proc->ProcessorEnabled ? "enabled" : "disabled");
447                 if (!proc->ProcessorEnabled)
448                         break;
449                 if (proc->LocalApicId >= NLAPICS)
450                         panic("%s: CPU ID %d too high", __func__,
451                             proc->LocalApicId);
452                 la = &lapics[proc->LocalApicId];
453                 KASSERT(la->la_enabled == 0,
454                     ("Duplicate local APIC ID %d", proc->LocalApicId));
455                 la->la_enabled = 1;
456                 la->la_acpi_id = proc->ProcessorId;
457                 lapic_create(proc->LocalApicId, 0);
458                 break;
459         }
460 }
461
462
463 /*
464  * Add an I/O APIC from an entry in the table.
465  */
466 static void
467 madt_parse_apics(APIC_HEADER *entry, void *arg __unused)
468 {
469         MADT_IO_APIC *apic;
470
471         switch (entry->Type) {
472         case APIC_IO:
473                 apic = (MADT_IO_APIC *)entry;
474                 if (bootverbose)
475                         printf("MADT: Found IO APIC ID %d, Interrupt %d at %p\n",
476                             apic->IoApicId, apic->Interrupt,
477                             (void *)(uintptr_t)apic->Address);
478                 if (apic->IoApicId >= NIOAPICS)
479                         panic("%s: I/O APIC ID %d too high", __func__,
480                             apic->IoApicId);
481                 if (ioapics[apic->IoApicId].io_apic != NULL)
482                         panic("%s: Double APIC ID %d", __func__,
483                             apic->IoApicId);
484                 ioapics[apic->IoApicId].io_apic = ioapic_create(
485                         (uintptr_t)apic->Address, apic->IoApicId,
486                             apic->Interrupt);
487                 ioapics[apic->IoApicId].io_vector = apic->Interrupt;
488                 break;
489         default:
490                 break;
491         }
492 }
493
494 /*
495  * Determine properties of an interrupt source.  Note that for ACPI these
496  * functions are only used for ISA interrupts, so we assume ISA bus values
497  * (Active Hi, Edge Triggered) for conforming values except for the ACPI
498  * SCI for which we use Active Lo, Level Triggered.
499  */
500 static enum intr_polarity
501 interrupt_polarity(UINT16 Polarity, UINT8 Source)
502 {
503
504         switch (Polarity) {
505         case POLARITY_CONFORMS:
506                 if (Source == AcpiGbl_FADT->SciInt)
507                         return (INTR_POLARITY_LOW);
508                 else
509                         return (INTR_POLARITY_HIGH);
510         case POLARITY_ACTIVE_HIGH:
511                 return (INTR_POLARITY_HIGH);
512         case POLARITY_ACTIVE_LOW:
513                 return (INTR_POLARITY_LOW);
514         default:
515                 panic("Bogus Interrupt Polarity");
516         }
517 }
518
519 static enum intr_trigger
520 interrupt_trigger(UINT16 TriggerMode, UINT8 Source)
521 {
522
523         switch (TriggerMode) {
524         case TRIGGER_CONFORMS:
525                 if (Source == AcpiGbl_FADT->SciInt)
526                         return (INTR_TRIGGER_LEVEL);
527                 else
528                         return (INTR_TRIGGER_EDGE);
529         case TRIGGER_EDGE:
530                 return (INTR_TRIGGER_EDGE);
531         case TRIGGER_LEVEL:
532                 return (INTR_TRIGGER_LEVEL);
533         default:
534                 panic("Bogus Interrupt Trigger Mode");
535         }
536 }
537
538 /*
539  * Find the local APIC ID associated with a given ACPI Processor ID.
540  */
541 static int
542 madt_find_cpu(u_int acpi_id, u_int *apic_id)
543 {
544         int i;
545
546         for (i = 0; i < NLAPICS; i++) {
547                 if (!lapics[i].la_enabled)
548                         continue;
549                 if (lapics[i].la_acpi_id != acpi_id)
550                         continue;
551                 *apic_id = i;
552                 return (0);
553         }
554         return (ENOENT);
555 }
556
557 /*
558  * Find the IO APIC and pin on that APIC associated with a given global
559  * interrupt.
560  */
561 static int
562 madt_find_interrupt(int intr, void **apic, u_int *pin)
563 {
564         int i, best;
565
566         best = -1;
567         for (i = 0; i < NIOAPICS; i++) {
568                 if (ioapics[i].io_apic == NULL ||
569                     ioapics[i].io_vector > intr)
570                         continue;
571                 if (best == -1 ||
572                     ioapics[best].io_vector < ioapics[i].io_vector)
573                         best = i;
574         }
575         if (best == -1)
576                 return (ENOENT);
577         *apic = ioapics[best].io_apic;
578         *pin = intr - ioapics[best].io_vector;
579         if (*pin > 32)
580                 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
581                     intr);
582         return (0);
583 }
584
585 /*
586  * Parse an interrupt source override for an ISA interrupt.
587  */
588 static void
589 madt_parse_interrupt_override(MADT_INTERRUPT_OVERRIDE *intr)
590 {
591         void *new_ioapic, *old_ioapic;
592         u_int new_pin, old_pin;
593         enum intr_trigger trig;
594         enum intr_polarity pol;
595         char buf[64];
596
597         if (bootverbose)
598                 printf("MADT: intr override: source %u, irq %u\n",
599                     intr->Source, intr->Interrupt);
600         KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
601         if (madt_find_interrupt(intr->Interrupt, &new_ioapic,
602             &new_pin) != 0) {
603                 printf("MADT: Could not find APIC for vector %d (IRQ %d)\n",
604                     intr->Interrupt, intr->Source);
605                 return;
606         }
607
608         /*
609          * Lookup the appropriate trigger and polarity modes for this
610          * entry.
611          */
612         trig = interrupt_trigger(intr->TriggerMode, intr->Source);
613         pol = interrupt_polarity(intr->Polarity, intr->Source);
614
615         /*
616          * If the SCI is identity mapped but has edge trigger and
617          * active-hi polarity or the force_sci_lo tunable is set,
618          * force it to use level/lo.
619          */
620         if (intr->Source == AcpiGbl_FADT->SciInt) {
621                 madt_found_sci_override = 1;
622                 if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
623                         if (tolower(buf[0]) == 'e')
624                                 trig = INTR_TRIGGER_EDGE;
625                         else if (tolower(buf[0]) == 'l')
626                                 trig = INTR_TRIGGER_LEVEL;
627                         else
628                                 panic(
629                                 "Invalid trigger %s: must be 'edge' or 'level'",
630                                     buf);
631                         printf("MADT: Forcing SCI to %s trigger\n",
632                             trig == INTR_TRIGGER_EDGE ? "edge" : "level");
633                 }
634                 if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
635                         if (tolower(buf[0]) == 'h')
636                                 pol = INTR_POLARITY_HIGH;
637                         else if (tolower(buf[0]) == 'l')
638                                 pol = INTR_POLARITY_LOW;
639                         else
640                                 panic(
641                                 "Invalid polarity %s: must be 'high' or 'low'",
642                                     buf);
643                         printf("MADT: Forcing SCI to active %s polarity\n",
644                             pol == INTR_POLARITY_HIGH ? "high" : "low");
645                 }
646         }
647
648         /* Remap the IRQ if it is mapped to a different interrupt vector. */
649         if (intr->Source != intr->Interrupt) {
650                 /*
651                  * If the SCI is remapped to a non-ISA global interrupt,
652                  * then override the vector we use to setup and allocate
653                  * the interrupt.
654                  */
655                 if (intr->Interrupt > 15 &&
656                     intr->Source == AcpiGbl_FADT->SciInt)
657                         acpi_OverrideInterruptLevel(intr->Interrupt);
658                 else
659                         ioapic_remap_vector(new_ioapic, new_pin, intr->Source);
660                 if (madt_find_interrupt(intr->Source, &old_ioapic,
661                     &old_pin) != 0)
662                         printf("MADT: Could not find APIC for source IRQ %d\n",
663                             intr->Source);
664                 else if (ioapic_get_vector(old_ioapic, old_pin) ==
665                     intr->Source)
666                         ioapic_disable_pin(old_ioapic, old_pin);
667         }
668
669         /* Program the polarity and trigger mode. */
670         ioapic_set_triggermode(new_ioapic, new_pin, trig);
671         ioapic_set_polarity(new_ioapic, new_pin, pol);
672 }
673
674 /*
675  * Parse an entry for an NMI routed to an IO APIC.
676  */
677 static void
678 madt_parse_nmi(MADT_NMI_SOURCE *nmi)
679 {
680         void *ioapic;
681         u_int pin;
682
683         if (madt_find_interrupt(nmi->Interrupt, &ioapic, &pin) != 0) {
684                 printf("MADT: Could not find APIC for vector %d\n",
685                     nmi->Interrupt);
686                 return;
687         }
688
689         ioapic_set_nmi(ioapic, pin);
690         if (nmi->TriggerMode != TRIGGER_CONFORMS)
691                 ioapic_set_triggermode(ioapic, pin,
692                     interrupt_trigger(nmi->TriggerMode, 0));
693         if (nmi->Polarity != TRIGGER_CONFORMS)
694                 ioapic_set_polarity(ioapic, pin,
695                     interrupt_polarity(nmi->Polarity, 0));
696 }
697
698 /*
699  * Parse an entry for an NMI routed to a local APIC LVT pin.
700  */
701 static void
702 madt_parse_local_nmi(MADT_LOCAL_APIC_NMI *nmi)
703 {
704         u_int apic_id, pin;
705
706         if (nmi->ProcessorId == 0xff)
707                 apic_id = APIC_ID_ALL;
708         else if (madt_find_cpu(nmi->ProcessorId, &apic_id) != 0) {
709                 if (bootverbose)
710                         printf("MADT: Ignoring local NMI routed to ACPI CPU %u\n",
711                             nmi->ProcessorId);
712                 return;
713         }
714         if (nmi->Lint == 0)
715                 pin = LVT_LINT0;
716         else
717                 pin = LVT_LINT1;
718         lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
719         if (nmi->TriggerMode != TRIGGER_CONFORMS)
720                 lapic_set_lvt_triggermode(apic_id, pin,
721                     interrupt_trigger(nmi->TriggerMode, 0));
722         if (nmi->Polarity != POLARITY_CONFORMS)
723                 lapic_set_lvt_polarity(apic_id, pin,
724                     interrupt_polarity(nmi->Polarity, 0));
725 }
726
727 /*
728  * Parse interrupt entries.
729  */
730 static void
731 madt_parse_ints(APIC_HEADER *entry, void *arg __unused)
732 {
733
734         switch (entry->Type) {
735         case APIC_XRUPT_OVERRIDE:
736                 madt_parse_interrupt_override(
737                         (MADT_INTERRUPT_OVERRIDE *)entry);
738                 break;
739         case APIC_NMI:
740                 madt_parse_nmi((MADT_NMI_SOURCE *)entry);
741                 break;
742         case APIC_LOCAL_NMI:
743                 madt_parse_local_nmi((MADT_LOCAL_APIC_NMI *)entry);
744                 break;
745         }
746 }
747
748 /*
749  * Setup per-CPU ACPI IDs.
750  */
751 static void
752 madt_set_ids(void *dummy)
753 {
754         struct lapic_info *la;
755         struct mdglobaldata *md;
756         u_int i;
757
758         if (madt == NULL)
759                 return;
760         for (i = 0; i < ncpus; i++) {
761                 if ((smp_active_mask & (1 << i)) == 0)
762                         continue;
763                 md = (struct mdglobaldata *)globaldata_find(i);
764                 KKASSERT(md != NULL);
765                 la = &lapics[md->gd_apic_id];
766                 if (!la->la_enabled)
767                         panic("APIC: CPU with APIC ID %u is not enabled",
768                             md->gd_apic_id);
769                 md->gd_acpi_id = la->la_acpi_id;
770                 if (bootverbose)
771                         printf("APIC: CPU %u has ACPI ID %u\n", i,
772                             la->la_acpi_id);
773         }
774 }
775 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_ANY, madt_set_ids, NULL)