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