xref: /freebsd/sys/x86/acpica/madt.c (revision 43faedc1339a9624c7acedb7f3e5624e64da5b99)
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
36 #include <sys/smp.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 
40 #include <x86/apicreg.h>
41 #include <machine/intr_machdep.h>
42 #include <x86/apicvar.h>
43 #include <machine/md_var.h>
44 #include <x86/vmware.h>
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/actables.h>
48 
49 #include <dev/acpica/acpivar.h>
50 #include <dev/pci/pcivar.h>
51 
52 /* These two arrays are indexed by APIC IDs. */
53 static struct {
54 	void *io_apic;
55 	UINT32 io_vector;
56 } *ioapics;
57 
58 static struct lapic_info {
59 	u_int la_enabled;
60 	u_int la_acpi_id;
61 } lapics[MAX_APIC_ID + 1];
62 
63 int madt_found_sci_override;
64 static ACPI_TABLE_MADT *madt;
65 static vm_paddr_t madt_physaddr;
66 static vm_offset_t madt_length;
67 
68 static MALLOC_DEFINE(M_MADT, "madt_table", "ACPI MADT Table Items");
69 
70 static enum intr_polarity interrupt_polarity(UINT16 IntiFlags, UINT8 Source);
71 static enum intr_trigger interrupt_trigger(UINT16 IntiFlags, UINT8 Source);
72 static int	madt_find_cpu(u_int acpi_id, u_int *apic_id);
73 static int	madt_find_interrupt(int intr, void **apic, u_int *pin);
74 static void	madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg);
75 static void	madt_parse_interrupt_override(
76 		    ACPI_MADT_INTERRUPT_OVERRIDE *intr);
77 static void	madt_parse_ints(ACPI_SUBTABLE_HEADER *entry,
78 		    void *arg __unused);
79 static void	madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi);
80 static void	madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi);
81 static int	madt_probe(void);
82 static int	madt_probe_cpus(void);
83 static void	madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry,
84 		    void *arg __unused);
85 static void	madt_register(void *dummy);
86 static int	madt_setup_local(void);
87 static int	madt_setup_io(void);
88 static void	madt_walk_table(acpi_subtable_handler *handler, void *arg);
89 
90 static struct apic_enumerator madt_enumerator = {
91 	"MADT",
92 	madt_probe,
93 	madt_probe_cpus,
94 	madt_setup_local,
95 	madt_setup_io
96 };
97 
98 /*
99  * Look for an ACPI Multiple APIC Description Table ("APIC")
100  */
101 static int
102 madt_probe(void)
103 {
104 
105 	madt_physaddr = acpi_find_table(ACPI_SIG_MADT);
106 	if (madt_physaddr == 0)
107 		return (ENXIO);
108 	return (-50);
109 }
110 
111 /*
112  * Run through the MP table enumerating CPUs.
113  */
114 static int
115 madt_probe_cpus(void)
116 {
117 
118 	madt = acpi_map_table(madt_physaddr, ACPI_SIG_MADT);
119 	madt_length = madt->Header.Length;
120 	KASSERT(madt != NULL, ("Unable to re-map MADT"));
121 	madt_walk_table(madt_probe_cpus_handler, NULL);
122 	acpi_unmap_table(madt);
123 	madt = NULL;
124 	return (0);
125 }
126 
127 /*
128  * Initialize the local APIC on the BSP.
129  */
130 static int
131 madt_setup_local(void)
132 {
133 	ACPI_TABLE_DMAR *dmartbl;
134 	vm_paddr_t dmartbl_physaddr;
135 	const char *reason;
136 	char *hw_vendor;
137 	u_int p[4];
138 
139 	madt = pmap_mapbios(madt_physaddr, madt_length);
140 	if ((cpu_feature2 & CPUID2_X2APIC) != 0) {
141 		x2apic_mode = 1;
142 		reason = NULL;
143 
144 		/*
145 		 * Automatically detect several configurations where
146 		 * x2APIC mode is known to cause troubles.  User can
147 		 * override the setting with hw.x2apic_enable tunable.
148 		 */
149 		dmartbl_physaddr = acpi_find_table(ACPI_SIG_DMAR);
150 		if (dmartbl_physaddr != 0) {
151 			dmartbl = acpi_map_table(dmartbl_physaddr,
152 			    ACPI_SIG_DMAR);
153 			if ((dmartbl->Flags & ACPI_DMAR_X2APIC_OPT_OUT) != 0) {
154 				x2apic_mode = 0;
155 				reason = "by DMAR table";
156 			}
157 			acpi_unmap_table(dmartbl);
158 		}
159 		if (vm_guest == VM_GUEST_VMWARE) {
160 			vmware_hvcall(VMW_HVCMD_GETVCPU_INFO, p);
161 			if ((p[0] & VMW_VCPUINFO_VCPU_RESERVED) != 0 ||
162 			    (p[0] & VMW_VCPUINFO_LEGACY_X2APIC) == 0) {
163 				x2apic_mode = 0;
164 		reason = "inside VMWare without intr redirection";
165 			}
166 		} else if (vm_guest == VM_GUEST_XEN) {
167 			x2apic_mode = 0;
168 			reason = "due to running under XEN";
169 		} else if (vm_guest == VM_GUEST_NO &&
170 		    CPUID_TO_FAMILY(cpu_id) == 0x6 &&
171 		    CPUID_TO_MODEL(cpu_id) == 0x2a) {
172 			hw_vendor = kern_getenv("smbios.planar.maker");
173 			/*
174 			 * It seems that some Lenovo and ASUS
175 			 * SandyBridge-based notebook BIOSes have a
176 			 * bug which prevents booting AP in x2APIC
177 			 * mode.  Since the only way to detect mobile
178 			 * CPU is to check northbridge pci id, which
179 			 * cannot be done that early, disable x2APIC
180 			 * for all Lenovo and ASUS SandyBridge
181 			 * machines.
182 			 */
183 			if (hw_vendor != NULL) {
184 				if (!strcmp(hw_vendor, "LENOVO") ||
185 				    !strcmp(hw_vendor,
186 				    "ASUSTeK Computer Inc.")) {
187 					x2apic_mode = 0;
188 					reason =
189 				    "for a suspected SandyBridge BIOS bug";
190 				}
191 				freeenv(hw_vendor);
192 			}
193 		}
194 		TUNABLE_INT_FETCH("hw.x2apic_enable", &x2apic_mode);
195 		if (!x2apic_mode && reason != NULL && bootverbose)
196 			printf("x2APIC available but disabled %s\n", reason);
197 	}
198 
199 	lapic_init(madt->Address);
200 	printf("ACPI APIC Table: <%.*s %.*s>\n",
201 	    (int)sizeof(madt->Header.OemId), madt->Header.OemId,
202 	    (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
203 
204 	/*
205 	 * We ignore 64-bit local APIC override entries.  Should we
206 	 * perhaps emit a warning here if we find one?
207 	 */
208 	return (0);
209 }
210 
211 /*
212  * Enumerate I/O APICs and setup interrupt sources.
213  */
214 static int
215 madt_setup_io(void)
216 {
217 	void *ioapic;
218 	u_int pin;
219 	int i;
220 
221 	/* Try to initialize ACPI so that we can access the FADT. */
222 	i = acpi_Startup();
223 	if (ACPI_FAILURE(i)) {
224 		printf("MADT: ACPI Startup failed with %s\n",
225 		    AcpiFormatException(i));
226 		printf("Try disabling either ACPI or apic support.\n");
227 		panic("Using MADT but ACPI doesn't work");
228 	}
229 
230 	ioapics = malloc(sizeof(*ioapics) * (MAX_APIC_ID + 1), M_MADT,
231 	    M_WAITOK | M_ZERO);
232 
233 	/* First, we run through adding I/O APIC's. */
234 	madt_walk_table(madt_parse_apics, NULL);
235 
236 	/* Second, we run through the table tweaking interrupt sources. */
237 	madt_walk_table(madt_parse_ints, NULL);
238 
239 	/*
240 	 * If there was not an explicit override entry for the SCI,
241 	 * force it to use level trigger and active-low polarity.
242 	 */
243 	if (!madt_found_sci_override) {
244 		if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic,
245 		    &pin) != 0)
246 			printf("MADT: Could not find APIC for SCI IRQ %u\n",
247 			    AcpiGbl_FADT.SciInterrupt);
248 		else {
249 			printf(
250 	"MADT: Forcing active-low polarity and level trigger for SCI\n");
251 			ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
252 			ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
253 		}
254 	}
255 
256 	/* Third, we register all the I/O APIC's. */
257 	for (i = 0; i <= MAX_APIC_ID; i++)
258 		if (ioapics[i].io_apic != NULL)
259 			ioapic_register(ioapics[i].io_apic);
260 
261 	/* Finally, we throw the switch to enable the I/O APIC's. */
262 	acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
263 
264 	free(ioapics, M_MADT);
265 	ioapics = NULL;
266 
267 	return (0);
268 }
269 
270 static void
271 madt_register(void *dummy __unused)
272 {
273 
274 	apic_register_enumerator(&madt_enumerator);
275 }
276 SYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, madt_register, NULL);
277 
278 /*
279  * Call the handler routine for each entry in the MADT table.
280  */
281 static void
282 madt_walk_table(acpi_subtable_handler *handler, void *arg)
283 {
284 
285 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
286 	    handler, arg);
287 }
288 
289 static void
290 madt_add_cpu(u_int acpi_id, u_int apic_id, u_int flags)
291 {
292 	struct lapic_info *la;
293 
294 	/*
295 	 * The MADT does not include a BSP flag, so we have to let the
296 	 * MP code figure out which CPU is the BSP on its own.
297 	 */
298 	if (bootverbose)
299 		printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
300 		    apic_id, acpi_id, flags & ACPI_MADT_ENABLED ?
301 		    "enabled" : "disabled");
302 	if (!(flags & ACPI_MADT_ENABLED))
303 		return;
304 	if (apic_id > MAX_APIC_ID) {
305 		printf("MADT: Ignoring local APIC ID %u (too high)\n",
306 		    apic_id);
307 		return;
308 	}
309 
310 	la = &lapics[apic_id];
311 	KASSERT(la->la_enabled == 0, ("Duplicate local APIC ID %u", apic_id));
312 	la->la_enabled = 1;
313 	la->la_acpi_id = acpi_id;
314 	lapic_create(apic_id, 0);
315 }
316 
317 static void
318 madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
319 {
320 	ACPI_MADT_LOCAL_APIC *proc;
321 	ACPI_MADT_LOCAL_X2APIC *x2apic;
322 
323 	switch (entry->Type) {
324 	case ACPI_MADT_TYPE_LOCAL_APIC:
325 		proc = (ACPI_MADT_LOCAL_APIC *)entry;
326 		madt_add_cpu(proc->ProcessorId, proc->Id, proc->LapicFlags);
327 		break;
328 	case ACPI_MADT_TYPE_LOCAL_X2APIC:
329 		x2apic = (ACPI_MADT_LOCAL_X2APIC *)entry;
330 		madt_add_cpu(x2apic->Uid, x2apic->LocalApicId,
331 		    x2apic->LapicFlags);
332 		break;
333 	}
334 }
335 
336 
337 /*
338  * Add an I/O APIC from an entry in the table.
339  */
340 static void
341 madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
342 {
343 	ACPI_MADT_IO_APIC *apic;
344 
345 	switch (entry->Type) {
346 	case ACPI_MADT_TYPE_IO_APIC:
347 		apic = (ACPI_MADT_IO_APIC *)entry;
348 		if (bootverbose)
349 			printf(
350 			    "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
351 			    apic->Id, apic->GlobalIrqBase,
352 			    (void *)(uintptr_t)apic->Address);
353 		if (apic->Id > MAX_APIC_ID)
354 			panic("%s: I/O APIC ID %u too high", __func__,
355 			    apic->Id);
356 		if (ioapics[apic->Id].io_apic != NULL)
357 			panic("%s: Double APIC ID %u", __func__, apic->Id);
358 		if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
359 			printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
360 			break;
361 		}
362 		ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
363 		    apic->Id, apic->GlobalIrqBase);
364 		ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
365 		break;
366 	default:
367 		break;
368 	}
369 }
370 
371 /*
372  * Determine properties of an interrupt source.  Note that for ACPI these
373  * functions are only used for ISA interrupts, so we assume ISA bus values
374  * (Active Hi, Edge Triggered) for conforming values except for the ACPI
375  * SCI for which we use Active Lo, Level Triggered.
376  */
377 static enum intr_polarity
378 interrupt_polarity(UINT16 IntiFlags, UINT8 Source)
379 {
380 
381 	switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
382 	default:
383 		printf("WARNING: Bogus Interrupt Polarity. Assume CONFORMS\n");
384 		/* FALLTHROUGH*/
385 	case ACPI_MADT_POLARITY_CONFORMS:
386 		if (Source == AcpiGbl_FADT.SciInterrupt)
387 			return (INTR_POLARITY_LOW);
388 		else
389 			return (INTR_POLARITY_HIGH);
390 	case ACPI_MADT_POLARITY_ACTIVE_HIGH:
391 		return (INTR_POLARITY_HIGH);
392 	case ACPI_MADT_POLARITY_ACTIVE_LOW:
393 		return (INTR_POLARITY_LOW);
394 	}
395 }
396 
397 static enum intr_trigger
398 interrupt_trigger(UINT16 IntiFlags, UINT8 Source)
399 {
400 
401 	switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
402 	default:
403 		printf("WARNING: Bogus Interrupt Trigger Mode. Assume CONFORMS.\n");
404 		/*FALLTHROUGH*/
405 	case ACPI_MADT_TRIGGER_CONFORMS:
406 		if (Source == AcpiGbl_FADT.SciInterrupt)
407 			return (INTR_TRIGGER_LEVEL);
408 		else
409 			return (INTR_TRIGGER_EDGE);
410 	case ACPI_MADT_TRIGGER_EDGE:
411 		return (INTR_TRIGGER_EDGE);
412 	case ACPI_MADT_TRIGGER_LEVEL:
413 		return (INTR_TRIGGER_LEVEL);
414 	}
415 }
416 
417 /*
418  * Find the local APIC ID associated with a given ACPI Processor ID.
419  */
420 static int
421 madt_find_cpu(u_int acpi_id, u_int *apic_id)
422 {
423 	int i;
424 
425 	for (i = 0; i <= MAX_APIC_ID; i++) {
426 		if (!lapics[i].la_enabled)
427 			continue;
428 		if (lapics[i].la_acpi_id != acpi_id)
429 			continue;
430 		*apic_id = i;
431 		return (0);
432 	}
433 	return (ENOENT);
434 }
435 
436 /*
437  * Find the IO APIC and pin on that APIC associated with a given global
438  * interrupt.
439  */
440 static int
441 madt_find_interrupt(int intr, void **apic, u_int *pin)
442 {
443 	int i, best;
444 
445 	best = -1;
446 	for (i = 0; i <= MAX_APIC_ID; i++) {
447 		if (ioapics[i].io_apic == NULL ||
448 		    ioapics[i].io_vector > intr)
449 			continue;
450 		if (best == -1 ||
451 		    ioapics[best].io_vector < ioapics[i].io_vector)
452 			best = i;
453 	}
454 	if (best == -1)
455 		return (ENOENT);
456 	*apic = ioapics[best].io_apic;
457 	*pin = intr - ioapics[best].io_vector;
458 	if (*pin > 32)
459 		printf("WARNING: Found intpin of %u for vector %d\n", *pin,
460 		    intr);
461 	return (0);
462 }
463 
464 void
465 madt_parse_interrupt_values(void *entry,
466     enum intr_trigger *trig, enum intr_polarity *pol)
467 {
468 	ACPI_MADT_INTERRUPT_OVERRIDE *intr;
469 	char buf[64];
470 
471 	intr = entry;
472 
473 	if (bootverbose)
474 		printf("MADT: Interrupt override: source %u, irq %u\n",
475 		    intr->SourceIrq, intr->GlobalIrq);
476 	KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
477 
478 	/*
479 	 * Lookup the appropriate trigger and polarity modes for this
480 	 * entry.
481 	 */
482 	*trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
483 	*pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
484 
485 	/*
486 	 * If the SCI is identity mapped but has edge trigger and
487 	 * active-hi polarity or the force_sci_lo tunable is set,
488 	 * force it to use level/lo.
489 	 */
490 	if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
491 		madt_found_sci_override = 1;
492 		if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
493 			if (tolower(buf[0]) == 'e')
494 				*trig = INTR_TRIGGER_EDGE;
495 			else if (tolower(buf[0]) == 'l')
496 				*trig = INTR_TRIGGER_LEVEL;
497 			else
498 				panic(
499 				"Invalid trigger %s: must be 'edge' or 'level'",
500 				    buf);
501 			printf("MADT: Forcing SCI to %s trigger\n",
502 			    *trig == INTR_TRIGGER_EDGE ? "edge" : "level");
503 		}
504 		if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
505 			if (tolower(buf[0]) == 'h')
506 				*pol = INTR_POLARITY_HIGH;
507 			else if (tolower(buf[0]) == 'l')
508 				*pol = INTR_POLARITY_LOW;
509 			else
510 				panic(
511 				"Invalid polarity %s: must be 'high' or 'low'",
512 				    buf);
513 			printf("MADT: Forcing SCI to active %s polarity\n",
514 			    *pol == INTR_POLARITY_HIGH ? "high" : "low");
515 		}
516 	}
517 }
518 
519 /*
520  * Parse an interrupt source override for an ISA interrupt.
521  */
522 static void
523 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
524 {
525 	void *new_ioapic, *old_ioapic;
526 	u_int new_pin, old_pin;
527 	enum intr_trigger trig;
528 	enum intr_polarity pol;
529 
530 	if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
531 	    intr->GlobalIrq == 2) {
532 		if (bootverbose)
533 			printf("MADT: Skipping timer override\n");
534 		return;
535 	}
536 
537 	if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
538 		printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
539 		    intr->GlobalIrq, intr->SourceIrq);
540 		return;
541 	}
542 
543 	madt_parse_interrupt_values(intr, &trig, &pol);
544 
545 	/* Remap the IRQ if it is mapped to a different interrupt vector. */
546 	if (intr->SourceIrq != intr->GlobalIrq) {
547 		/*
548 		 * If the SCI is remapped to a non-ISA global interrupt,
549 		 * then override the vector we use to setup and allocate
550 		 * the interrupt.
551 		 */
552 		if (intr->GlobalIrq > 15 &&
553 		    intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
554 			acpi_OverrideInterruptLevel(intr->GlobalIrq);
555 		else
556 			ioapic_remap_vector(new_ioapic, new_pin,
557 			    intr->SourceIrq);
558 		if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
559 		    &old_pin) != 0)
560 			printf("MADT: Could not find APIC for source IRQ %u\n",
561 			    intr->SourceIrq);
562 		else if (ioapic_get_vector(old_ioapic, old_pin) ==
563 		    intr->SourceIrq)
564 			ioapic_disable_pin(old_ioapic, old_pin);
565 	}
566 
567 	/* Program the polarity and trigger mode. */
568 	ioapic_set_triggermode(new_ioapic, new_pin, trig);
569 	ioapic_set_polarity(new_ioapic, new_pin, pol);
570 }
571 
572 /*
573  * Parse an entry for an NMI routed to an IO APIC.
574  */
575 static void
576 madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
577 {
578 	void *ioapic;
579 	u_int pin;
580 
581 	if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
582 		printf("MADT: Could not find APIC for vector %u\n",
583 		    nmi->GlobalIrq);
584 		return;
585 	}
586 
587 	ioapic_set_nmi(ioapic, pin);
588 	if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
589 		ioapic_set_triggermode(ioapic, pin,
590 		    interrupt_trigger(nmi->IntiFlags, 0));
591 	if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
592 		ioapic_set_polarity(ioapic, pin,
593 		    interrupt_polarity(nmi->IntiFlags, 0));
594 }
595 
596 /*
597  * Parse an entry for an NMI routed to a local APIC LVT pin.
598  */
599 static void
600 madt_handle_local_nmi(u_int acpi_id, UINT8 Lint, UINT16 IntiFlags)
601 {
602 	u_int apic_id, pin;
603 
604 	if (acpi_id == 0xffffffff)
605 		apic_id = APIC_ID_ALL;
606 	else if (madt_find_cpu(acpi_id, &apic_id) != 0) {
607 		if (bootverbose)
608 			printf("MADT: Ignoring local NMI routed to "
609 			    "ACPI CPU %u\n", acpi_id);
610 		return;
611 	}
612 	if (Lint == 0)
613 		pin = APIC_LVT_LINT0;
614 	else
615 		pin = APIC_LVT_LINT1;
616 	lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
617 	if (!(IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
618 		lapic_set_lvt_triggermode(apic_id, pin,
619 		    interrupt_trigger(IntiFlags, 0));
620 	if (!(IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
621 		lapic_set_lvt_polarity(apic_id, pin,
622 		    interrupt_polarity(IntiFlags, 0));
623 }
624 
625 static void
626 madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
627 {
628 
629 	madt_handle_local_nmi(nmi->ProcessorId == 0xff ? 0xffffffff :
630 	    nmi->ProcessorId, nmi->Lint, nmi->IntiFlags);
631 }
632 
633 static void
634 madt_parse_local_x2apic_nmi(ACPI_MADT_LOCAL_X2APIC_NMI *nmi)
635 {
636 
637 	madt_handle_local_nmi(nmi->Uid, nmi->Lint, nmi->IntiFlags);
638 }
639 
640 /*
641  * Parse interrupt entries.
642  */
643 static void
644 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
645 {
646 
647 	switch (entry->Type) {
648 	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
649 		madt_parse_interrupt_override(
650 			(ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
651 		break;
652 	case ACPI_MADT_TYPE_NMI_SOURCE:
653 		madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
654 		break;
655 	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
656 		madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
657 		break;
658 	case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
659 		madt_parse_local_x2apic_nmi(
660 		    (ACPI_MADT_LOCAL_X2APIC_NMI *)entry);
661 		break;
662 	}
663 }
664 
665 /*
666  * Setup per-CPU ACPI IDs.
667  */
668 static void
669 madt_set_ids(void *dummy)
670 {
671 	struct lapic_info *la;
672 	struct pcpu *pc;
673 	u_int i;
674 
675 	if (madt == NULL)
676 		return;
677 	CPU_FOREACH(i) {
678 		pc = pcpu_find(i);
679 		KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
680 		la = &lapics[pc->pc_apic_id];
681 		if (!la->la_enabled)
682 			panic("APIC: CPU with APIC ID %u is not enabled",
683 			    pc->pc_apic_id);
684 		pc->pc_acpi_id = la->la_acpi_id;
685 		if (bootverbose)
686 			printf("APIC: CPU %u has ACPI ID %u\n", i,
687 			    la->la_acpi_id);
688 	}
689 }
690 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_MIDDLE, madt_set_ids, NULL);
691