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