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