xref: /freebsd/sys/x86/acpica/madt.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
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 	int user_x2apic;
139 	bool bios_x2apic;
140 
141 	madt = pmap_mapbios(madt_physaddr, madt_length);
142 	if ((cpu_feature2 & CPUID2_X2APIC) != 0) {
143 		reason = NULL;
144 
145 		/*
146 		 * Automatically detect several configurations where
147 		 * x2APIC mode is known to cause troubles.  User can
148 		 * override the setting with hw.x2apic_enable tunable.
149 		 */
150 		dmartbl_physaddr = acpi_find_table(ACPI_SIG_DMAR);
151 		if (dmartbl_physaddr != 0) {
152 			dmartbl = acpi_map_table(dmartbl_physaddr,
153 			    ACPI_SIG_DMAR);
154 			if ((dmartbl->Flags & ACPI_DMAR_X2APIC_OPT_OUT) != 0)
155 				reason = "by DMAR table";
156 			acpi_unmap_table(dmartbl);
157 		}
158 		if (vm_guest == VM_GUEST_VMWARE) {
159 			vmware_hvcall(VMW_HVCMD_GETVCPU_INFO, p);
160 			if ((p[0] & VMW_VCPUINFO_VCPU_RESERVED) != 0 ||
161 			    (p[0] & VMW_VCPUINFO_LEGACY_X2APIC) == 0)
162 				reason =
163 				    "inside VMWare without intr redirection";
164 		} else if (vm_guest == VM_GUEST_XEN) {
165 			reason = "due to running under XEN";
166 		} else if (vm_guest == VM_GUEST_NO &&
167 		    CPUID_TO_FAMILY(cpu_id) == 0x6 &&
168 		    CPUID_TO_MODEL(cpu_id) == 0x2a) {
169 			hw_vendor = kern_getenv("smbios.planar.maker");
170 			/*
171 			 * It seems that some Lenovo and ASUS
172 			 * SandyBridge-based notebook BIOSes have a
173 			 * bug which prevents booting AP in x2APIC
174 			 * mode.  Since the only way to detect mobile
175 			 * CPU is to check northbridge pci id, which
176 			 * cannot be done that early, disable x2APIC
177 			 * for all Lenovo and ASUS SandyBridge
178 			 * machines.
179 			 */
180 			if (hw_vendor != NULL) {
181 				if (!strcmp(hw_vendor, "LENOVO") ||
182 				    !strcmp(hw_vendor,
183 				    "ASUSTeK Computer Inc.")) {
184 					reason =
185 				    "for a suspected SandyBridge BIOS bug";
186 				}
187 				freeenv(hw_vendor);
188 			}
189 		}
190 		bios_x2apic = lapic_is_x2apic();
191 		if (reason != NULL && bios_x2apic) {
192 			if (bootverbose)
193 				printf("x2APIC should be disabled %s but "
194 				    "already enabled by BIOS; enabling.\n",
195 				     reason);
196 			reason = NULL;
197 		}
198 		if (reason == NULL)
199 			x2apic_mode = 1;
200 		else if (bootverbose)
201 			printf("x2APIC available but disabled %s\n", reason);
202 		user_x2apic = x2apic_mode;
203 		TUNABLE_INT_FETCH("hw.x2apic_enable", &user_x2apic);
204 		if (user_x2apic != x2apic_mode) {
205 			if (bios_x2apic && !user_x2apic)
206 				printf("x2APIC disabled by tunable and "
207 				    "enabled by BIOS; ignoring tunable.");
208 			else
209 				x2apic_mode = user_x2apic;
210 		}
211 	}
212 
213 	lapic_init(madt->Address);
214 	printf("ACPI APIC Table: <%.*s %.*s>\n",
215 	    (int)sizeof(madt->Header.OemId), madt->Header.OemId,
216 	    (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
217 
218 	/*
219 	 * We ignore 64-bit local APIC override entries.  Should we
220 	 * perhaps emit a warning here if we find one?
221 	 */
222 	return (0);
223 }
224 
225 /*
226  * Enumerate I/O APICs and setup interrupt sources.
227  */
228 static int
229 madt_setup_io(void)
230 {
231 	void *ioapic;
232 	u_int pin;
233 	int i;
234 
235 	/* Try to initialize ACPI so that we can access the FADT. */
236 	i = acpi_Startup();
237 	if (ACPI_FAILURE(i)) {
238 		printf("MADT: ACPI Startup failed with %s\n",
239 		    AcpiFormatException(i));
240 		printf("Try disabling either ACPI or apic support.\n");
241 		panic("Using MADT but ACPI doesn't work");
242 	}
243 
244 	ioapics = malloc(sizeof(*ioapics) * (MAX_APIC_ID + 1), M_MADT,
245 	    M_WAITOK | M_ZERO);
246 
247 	/* First, we run through adding I/O APIC's. */
248 	madt_walk_table(madt_parse_apics, NULL);
249 
250 	/* Second, we run through the table tweaking interrupt sources. */
251 	madt_walk_table(madt_parse_ints, NULL);
252 
253 	/*
254 	 * If there was not an explicit override entry for the SCI,
255 	 * force it to use level trigger and active-low polarity.
256 	 */
257 	if (!madt_found_sci_override) {
258 		if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic,
259 		    &pin) != 0)
260 			printf("MADT: Could not find APIC for SCI IRQ %u\n",
261 			    AcpiGbl_FADT.SciInterrupt);
262 		else {
263 			printf(
264 	"MADT: Forcing active-low polarity and level trigger for SCI\n");
265 			ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
266 			ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
267 		}
268 	}
269 
270 	/* Third, we register all the I/O APIC's. */
271 	for (i = 0; i <= MAX_APIC_ID; i++)
272 		if (ioapics[i].io_apic != NULL)
273 			ioapic_register(ioapics[i].io_apic);
274 
275 	/* Finally, we throw the switch to enable the I/O APIC's. */
276 	acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
277 
278 	free(ioapics, M_MADT);
279 	ioapics = NULL;
280 
281 	return (0);
282 }
283 
284 static void
285 madt_register(void *dummy __unused)
286 {
287 
288 	apic_register_enumerator(&madt_enumerator);
289 }
290 SYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, madt_register, NULL);
291 
292 /*
293  * Call the handler routine for each entry in the MADT table.
294  */
295 static void
296 madt_walk_table(acpi_subtable_handler *handler, void *arg)
297 {
298 
299 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
300 	    handler, arg);
301 }
302 
303 static void
304 madt_add_cpu(u_int acpi_id, u_int apic_id, u_int flags)
305 {
306 	struct lapic_info *la;
307 
308 	/*
309 	 * The MADT does not include a BSP flag, so we have to let the
310 	 * MP code figure out which CPU is the BSP on its own.
311 	 */
312 	if (bootverbose)
313 		printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
314 		    apic_id, acpi_id, flags & ACPI_MADT_ENABLED ?
315 		    "enabled" : "disabled");
316 	if (!(flags & ACPI_MADT_ENABLED))
317 		return;
318 	if (apic_id > MAX_APIC_ID) {
319 		printf("MADT: Ignoring local APIC ID %u (too high)\n",
320 		    apic_id);
321 		return;
322 	}
323 
324 	la = &lapics[apic_id];
325 	KASSERT(la->la_enabled == 0, ("Duplicate local APIC ID %u", apic_id));
326 	la->la_enabled = 1;
327 	la->la_acpi_id = acpi_id;
328 	lapic_create(apic_id, 0);
329 }
330 
331 static void
332 madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
333 {
334 	ACPI_MADT_LOCAL_APIC *proc;
335 	ACPI_MADT_LOCAL_X2APIC *x2apic;
336 
337 	switch (entry->Type) {
338 	case ACPI_MADT_TYPE_LOCAL_APIC:
339 		proc = (ACPI_MADT_LOCAL_APIC *)entry;
340 		madt_add_cpu(proc->ProcessorId, proc->Id, proc->LapicFlags);
341 		break;
342 	case ACPI_MADT_TYPE_LOCAL_X2APIC:
343 		x2apic = (ACPI_MADT_LOCAL_X2APIC *)entry;
344 		madt_add_cpu(x2apic->Uid, x2apic->LocalApicId,
345 		    x2apic->LapicFlags);
346 		break;
347 	}
348 }
349 
350 
351 /*
352  * Add an I/O APIC from an entry in the table.
353  */
354 static void
355 madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
356 {
357 	ACPI_MADT_IO_APIC *apic;
358 
359 	switch (entry->Type) {
360 	case ACPI_MADT_TYPE_IO_APIC:
361 		apic = (ACPI_MADT_IO_APIC *)entry;
362 		if (bootverbose)
363 			printf(
364 			    "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
365 			    apic->Id, apic->GlobalIrqBase,
366 			    (void *)(uintptr_t)apic->Address);
367 		if (apic->Id > MAX_APIC_ID)
368 			panic("%s: I/O APIC ID %u too high", __func__,
369 			    apic->Id);
370 		if (ioapics[apic->Id].io_apic != NULL)
371 			panic("%s: Double APIC ID %u", __func__, apic->Id);
372 		if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
373 			printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
374 			break;
375 		}
376 		ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
377 		    apic->Id, apic->GlobalIrqBase);
378 		ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
379 		break;
380 	default:
381 		break;
382 	}
383 }
384 
385 /*
386  * Determine properties of an interrupt source.  Note that for ACPI these
387  * functions are only used for ISA interrupts, so we assume ISA bus values
388  * (Active Hi, Edge Triggered) for conforming values except for the ACPI
389  * SCI for which we use Active Lo, Level Triggered.
390  */
391 static enum intr_polarity
392 interrupt_polarity(UINT16 IntiFlags, UINT8 Source)
393 {
394 
395 	switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
396 	default:
397 		printf("WARNING: Bogus Interrupt Polarity. Assume CONFORMS\n");
398 		/* FALLTHROUGH*/
399 	case ACPI_MADT_POLARITY_CONFORMS:
400 		if (Source == AcpiGbl_FADT.SciInterrupt)
401 			return (INTR_POLARITY_LOW);
402 		else
403 			return (INTR_POLARITY_HIGH);
404 	case ACPI_MADT_POLARITY_ACTIVE_HIGH:
405 		return (INTR_POLARITY_HIGH);
406 	case ACPI_MADT_POLARITY_ACTIVE_LOW:
407 		return (INTR_POLARITY_LOW);
408 	}
409 }
410 
411 static enum intr_trigger
412 interrupt_trigger(UINT16 IntiFlags, UINT8 Source)
413 {
414 
415 	switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
416 	default:
417 		printf("WARNING: Bogus Interrupt Trigger Mode. Assume CONFORMS.\n");
418 		/*FALLTHROUGH*/
419 	case ACPI_MADT_TRIGGER_CONFORMS:
420 		if (Source == AcpiGbl_FADT.SciInterrupt)
421 			return (INTR_TRIGGER_LEVEL);
422 		else
423 			return (INTR_TRIGGER_EDGE);
424 	case ACPI_MADT_TRIGGER_EDGE:
425 		return (INTR_TRIGGER_EDGE);
426 	case ACPI_MADT_TRIGGER_LEVEL:
427 		return (INTR_TRIGGER_LEVEL);
428 	}
429 }
430 
431 /*
432  * Find the local APIC ID associated with a given ACPI Processor ID.
433  */
434 static int
435 madt_find_cpu(u_int acpi_id, u_int *apic_id)
436 {
437 	int i;
438 
439 	for (i = 0; i <= MAX_APIC_ID; i++) {
440 		if (!lapics[i].la_enabled)
441 			continue;
442 		if (lapics[i].la_acpi_id != acpi_id)
443 			continue;
444 		*apic_id = i;
445 		return (0);
446 	}
447 	return (ENOENT);
448 }
449 
450 /*
451  * Find the IO APIC and pin on that APIC associated with a given global
452  * interrupt.
453  */
454 static int
455 madt_find_interrupt(int intr, void **apic, u_int *pin)
456 {
457 	int i, best;
458 
459 	best = -1;
460 	for (i = 0; i <= MAX_APIC_ID; i++) {
461 		if (ioapics[i].io_apic == NULL ||
462 		    ioapics[i].io_vector > intr)
463 			continue;
464 		if (best == -1 ||
465 		    ioapics[best].io_vector < ioapics[i].io_vector)
466 			best = i;
467 	}
468 	if (best == -1)
469 		return (ENOENT);
470 	*apic = ioapics[best].io_apic;
471 	*pin = intr - ioapics[best].io_vector;
472 	if (*pin > 32)
473 		printf("WARNING: Found intpin of %u for vector %d\n", *pin,
474 		    intr);
475 	return (0);
476 }
477 
478 void
479 madt_parse_interrupt_values(void *entry,
480     enum intr_trigger *trig, enum intr_polarity *pol)
481 {
482 	ACPI_MADT_INTERRUPT_OVERRIDE *intr;
483 	char buf[64];
484 
485 	intr = entry;
486 
487 	if (bootverbose)
488 		printf("MADT: Interrupt override: source %u, irq %u\n",
489 		    intr->SourceIrq, intr->GlobalIrq);
490 	KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
491 
492 	/*
493 	 * Lookup the appropriate trigger and polarity modes for this
494 	 * entry.
495 	 */
496 	*trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
497 	*pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
498 
499 	/*
500 	 * If the SCI is identity mapped but has edge trigger and
501 	 * active-hi polarity or the force_sci_lo tunable is set,
502 	 * force it to use level/lo.
503 	 */
504 	if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
505 		madt_found_sci_override = 1;
506 		if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
507 			if (tolower(buf[0]) == 'e')
508 				*trig = INTR_TRIGGER_EDGE;
509 			else if (tolower(buf[0]) == 'l')
510 				*trig = INTR_TRIGGER_LEVEL;
511 			else
512 				panic(
513 				"Invalid trigger %s: must be 'edge' or 'level'",
514 				    buf);
515 			printf("MADT: Forcing SCI to %s trigger\n",
516 			    *trig == INTR_TRIGGER_EDGE ? "edge" : "level");
517 		}
518 		if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
519 			if (tolower(buf[0]) == 'h')
520 				*pol = INTR_POLARITY_HIGH;
521 			else if (tolower(buf[0]) == 'l')
522 				*pol = INTR_POLARITY_LOW;
523 			else
524 				panic(
525 				"Invalid polarity %s: must be 'high' or 'low'",
526 				    buf);
527 			printf("MADT: Forcing SCI to active %s polarity\n",
528 			    *pol == INTR_POLARITY_HIGH ? "high" : "low");
529 		}
530 	}
531 }
532 
533 /*
534  * Parse an interrupt source override for an ISA interrupt.
535  */
536 static void
537 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
538 {
539 	void *new_ioapic, *old_ioapic;
540 	u_int new_pin, old_pin;
541 	enum intr_trigger trig;
542 	enum intr_polarity pol;
543 
544 	if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
545 	    intr->GlobalIrq == 2) {
546 		if (bootverbose)
547 			printf("MADT: Skipping timer override\n");
548 		return;
549 	}
550 
551 	if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
552 		printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
553 		    intr->GlobalIrq, intr->SourceIrq);
554 		return;
555 	}
556 
557 	madt_parse_interrupt_values(intr, &trig, &pol);
558 
559 	/* Remap the IRQ if it is mapped to a different interrupt vector. */
560 	if (intr->SourceIrq != intr->GlobalIrq) {
561 		/*
562 		 * If the SCI is remapped to a non-ISA global interrupt,
563 		 * then override the vector we use to setup and allocate
564 		 * the interrupt.
565 		 */
566 		if (intr->GlobalIrq > 15 &&
567 		    intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
568 			acpi_OverrideInterruptLevel(intr->GlobalIrq);
569 		else
570 			ioapic_remap_vector(new_ioapic, new_pin,
571 			    intr->SourceIrq);
572 		if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
573 		    &old_pin) != 0)
574 			printf("MADT: Could not find APIC for source IRQ %u\n",
575 			    intr->SourceIrq);
576 		else if (ioapic_get_vector(old_ioapic, old_pin) ==
577 		    intr->SourceIrq)
578 			ioapic_disable_pin(old_ioapic, old_pin);
579 	}
580 
581 	/* Program the polarity and trigger mode. */
582 	ioapic_set_triggermode(new_ioapic, new_pin, trig);
583 	ioapic_set_polarity(new_ioapic, new_pin, pol);
584 }
585 
586 /*
587  * Parse an entry for an NMI routed to an IO APIC.
588  */
589 static void
590 madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
591 {
592 	void *ioapic;
593 	u_int pin;
594 
595 	if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
596 		printf("MADT: Could not find APIC for vector %u\n",
597 		    nmi->GlobalIrq);
598 		return;
599 	}
600 
601 	ioapic_set_nmi(ioapic, pin);
602 	if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
603 		ioapic_set_triggermode(ioapic, pin,
604 		    interrupt_trigger(nmi->IntiFlags, 0));
605 	if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
606 		ioapic_set_polarity(ioapic, pin,
607 		    interrupt_polarity(nmi->IntiFlags, 0));
608 }
609 
610 /*
611  * Parse an entry for an NMI routed to a local APIC LVT pin.
612  */
613 static void
614 madt_handle_local_nmi(u_int acpi_id, UINT8 Lint, UINT16 IntiFlags)
615 {
616 	u_int apic_id, pin;
617 
618 	if (acpi_id == 0xffffffff)
619 		apic_id = APIC_ID_ALL;
620 	else if (madt_find_cpu(acpi_id, &apic_id) != 0) {
621 		if (bootverbose)
622 			printf("MADT: Ignoring local NMI routed to "
623 			    "ACPI CPU %u\n", acpi_id);
624 		return;
625 	}
626 	if (Lint == 0)
627 		pin = APIC_LVT_LINT0;
628 	else
629 		pin = APIC_LVT_LINT1;
630 	lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
631 	if (!(IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
632 		lapic_set_lvt_triggermode(apic_id, pin,
633 		    interrupt_trigger(IntiFlags, 0));
634 	if (!(IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
635 		lapic_set_lvt_polarity(apic_id, pin,
636 		    interrupt_polarity(IntiFlags, 0));
637 }
638 
639 static void
640 madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
641 {
642 
643 	madt_handle_local_nmi(nmi->ProcessorId == 0xff ? 0xffffffff :
644 	    nmi->ProcessorId, nmi->Lint, nmi->IntiFlags);
645 }
646 
647 static void
648 madt_parse_local_x2apic_nmi(ACPI_MADT_LOCAL_X2APIC_NMI *nmi)
649 {
650 
651 	madt_handle_local_nmi(nmi->Uid, nmi->Lint, nmi->IntiFlags);
652 }
653 
654 /*
655  * Parse interrupt entries.
656  */
657 static void
658 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
659 {
660 
661 	switch (entry->Type) {
662 	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
663 		madt_parse_interrupt_override(
664 			(ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
665 		break;
666 	case ACPI_MADT_TYPE_NMI_SOURCE:
667 		madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
668 		break;
669 	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
670 		madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
671 		break;
672 	case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
673 		madt_parse_local_x2apic_nmi(
674 		    (ACPI_MADT_LOCAL_X2APIC_NMI *)entry);
675 		break;
676 	}
677 }
678 
679 /*
680  * Setup per-CPU ACPI IDs.
681  */
682 static void
683 madt_set_ids(void *dummy)
684 {
685 	struct lapic_info *la;
686 	struct pcpu *pc;
687 	u_int i;
688 
689 	if (madt == NULL)
690 		return;
691 	CPU_FOREACH(i) {
692 		pc = pcpu_find(i);
693 		KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
694 		la = &lapics[pc->pc_apic_id];
695 		if (!la->la_enabled)
696 			panic("APIC: CPU with APIC ID %u is not enabled",
697 			    pc->pc_apic_id);
698 		pc->pc_acpi_id = la->la_acpi_id;
699 		if (bootverbose)
700 			printf("APIC: CPU %u has ACPI ID %u\n", i,
701 			    la->la_acpi_id);
702 	}
703 }
704 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_MIDDLE, madt_set_ids, NULL);
705