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