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