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