madt.c revision 278749
1121992Sjhb/*-
2121992Sjhb * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3121992Sjhb * All rights reserved.
4121992Sjhb *
5121992Sjhb * Redistribution and use in source and binary forms, with or without
6121992Sjhb * modification, are permitted provided that the following conditions
7121992Sjhb * are met:
8121992Sjhb * 1. Redistributions of source code must retain the above copyright
9121992Sjhb *    notice, this list of conditions and the following disclaimer.
10121992Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11121992Sjhb *    notice, this list of conditions and the following disclaimer in the
12121992Sjhb *    documentation and/or other materials provided with the distribution.
13121992Sjhb *
14121992Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15121992Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16121992Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17121992Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18121992Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19121992Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20121992Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21121992Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22121992Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23121992Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24121992Sjhb * SUCH DAMAGE.
25121992Sjhb */
26121992Sjhb
27121992Sjhb#include <sys/cdefs.h>
28121992Sjhb__FBSDID("$FreeBSD: head/sys/x86/acpica/madt.c 278749 2015-02-14 09:00:12Z kib $");
29121992Sjhb
30121992Sjhb#include <sys/param.h>
31121992Sjhb#include <sys/systm.h>
32121992Sjhb#include <sys/bus.h>
33121992Sjhb#include <sys/kernel.h>
34278749Skib#include <sys/limits.h>
35121992Sjhb#include <sys/malloc.h>
36121992Sjhb#include <sys/smp.h>
37121992Sjhb#include <vm/vm.h>
38121992Sjhb#include <vm/pmap.h>
39121992Sjhb
40214631Sjhb#include <x86/apicreg.h>
41121992Sjhb#include <machine/intr_machdep.h>
42261087Sjhb#include <x86/apicvar.h>
43278473Skib#include <machine/md_var.h>
44278749Skib#include <x86/vmware.h>
45121992Sjhb
46193530Sjkim#include <contrib/dev/acpica/include/acpi.h>
47193530Sjkim#include <contrib/dev/acpica/include/actables.h>
48193530Sjkim
49121992Sjhb#include <dev/acpica/acpivar.h>
50121992Sjhb#include <dev/pci/pcivar.h>
51121992Sjhb
52121992Sjhb/* These two arrays are indexed by APIC IDs. */
53233623Sjhbstatic struct {
54121992Sjhb	void *io_apic;
55121992Sjhb	UINT32 io_vector;
56233623Sjhb} *ioapics;
57121992Sjhb
58233623Sjhbstatic struct lapic_info {
59121992Sjhb	u_int la_enabled:1;
60129960Sjhb	u_int la_acpi_id:8;
61233623Sjhb} lapics[MAX_APIC_ID + 1];
62121992Sjhb
63269512Sroygerint madt_found_sci_override;
64167814Sjkimstatic ACPI_TABLE_MADT *madt;
65121992Sjhbstatic vm_paddr_t madt_physaddr;
66121992Sjhbstatic vm_offset_t madt_length;
67121992Sjhb
68227293Sedstatic MALLOC_DEFINE(M_MADT, "madt_table", "ACPI MADT Table Items");
69121992Sjhb
70167814Sjkimstatic enum intr_polarity interrupt_polarity(UINT16 IntiFlags, UINT8 Source);
71167814Sjkimstatic enum intr_trigger interrupt_trigger(UINT16 IntiFlags, UINT8 Source);
72121992Sjhbstatic int	madt_find_cpu(u_int acpi_id, u_int *apic_id);
73121992Sjhbstatic int	madt_find_interrupt(int intr, void **apic, u_int *pin);
74167814Sjkimstatic void	madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg);
75167814Sjkimstatic void	madt_parse_interrupt_override(
76167814Sjkim		    ACPI_MADT_INTERRUPT_OVERRIDE *intr);
77167814Sjkimstatic void	madt_parse_ints(ACPI_SUBTABLE_HEADER *entry,
78167814Sjkim		    void *arg __unused);
79167814Sjkimstatic void	madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi);
80167814Sjkimstatic void	madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi);
81121992Sjhbstatic int	madt_probe(void);
82121992Sjhbstatic int	madt_probe_cpus(void);
83167814Sjkimstatic void	madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry,
84167814Sjkim		    void *arg __unused);
85121992Sjhbstatic void	madt_register(void *dummy);
86121992Sjhbstatic int	madt_setup_local(void);
87121992Sjhbstatic int	madt_setup_io(void);
88197439Sjhbstatic void	madt_walk_table(acpi_subtable_handler *handler, void *arg);
89121992Sjhb
90121992Sjhbstatic struct apic_enumerator madt_enumerator = {
91121992Sjhb	"MADT",
92121992Sjhb	madt_probe,
93121992Sjhb	madt_probe_cpus,
94121992Sjhb	madt_setup_local,
95121992Sjhb	madt_setup_io
96121992Sjhb};
97121992Sjhb
98121992Sjhb/*
99121992Sjhb * Look for an ACPI Multiple APIC Description Table ("APIC")
100121992Sjhb */
101121992Sjhbstatic int
102121992Sjhbmadt_probe(void)
103121992Sjhb{
104121992Sjhb
105197439Sjhb	madt_physaddr = acpi_find_table(ACPI_SIG_MADT);
106197439Sjhb	if (madt_physaddr == 0)
107121992Sjhb		return (ENXIO);
108269511Sroyger	return (-50);
109121992Sjhb}
110121992Sjhb
111121992Sjhb/*
112121992Sjhb * Run through the MP table enumerating CPUs.
113121992Sjhb */
114121992Sjhbstatic int
115121992Sjhbmadt_probe_cpus(void)
116121992Sjhb{
117121992Sjhb
118197439Sjhb	madt = acpi_map_table(madt_physaddr, ACPI_SIG_MADT);
119197439Sjhb	madt_length = madt->Header.Length;
120121992Sjhb	KASSERT(madt != NULL, ("Unable to re-map MADT"));
121121992Sjhb	madt_walk_table(madt_probe_cpus_handler, NULL);
122197439Sjhb	acpi_unmap_table(madt);
123121992Sjhb	madt = NULL;
124121992Sjhb	return (0);
125121992Sjhb}
126121992Sjhb
127121992Sjhb/*
128121992Sjhb * Initialize the local APIC on the BSP.
129121992Sjhb */
130121992Sjhbstatic int
131121992Sjhbmadt_setup_local(void)
132121992Sjhb{
133278473Skib	ACPI_TABLE_DMAR *dmartbl;
134278473Skib	vm_paddr_t dmartbl_physaddr;
135278749Skib	u_int p[4];
136121992Sjhb
137161223Sjhb	madt = pmap_mapbios(madt_physaddr, madt_length);
138278473Skib	if ((cpu_feature2 & CPUID2_X2APIC) != 0) {
139278473Skib		x2apic_mode = 1;
140278473Skib		dmartbl_physaddr = acpi_find_table(ACPI_SIG_DMAR);
141278473Skib		if (dmartbl_physaddr != 0) {
142278473Skib			dmartbl = acpi_map_table(dmartbl_physaddr,
143278473Skib			    ACPI_SIG_DMAR);
144278473Skib			if ((dmartbl->Flags & ACPI_DMAR_X2APIC_OPT_OUT) != 0) {
145278473Skib				x2apic_mode = 0;
146278473Skib				if (bootverbose)
147278473Skib					printf(
148278473Skib		"x2APIC available but disabled by DMAR table\n");
149278473Skib			}
150278473Skib			acpi_unmap_table(dmartbl);
151278473Skib		}
152278749Skib		if (vm_guest == VM_GUEST_VMWARE) {
153278749Skib			vmware_hvcall(VMW_HVCMD_GETVCPU_INFO, p);
154278749Skib			if ((p[0] & VMW_VCPUINFO_VCPU_RESERVED) != 0 ||
155278749Skib			    (p[0] & VMW_VCPUINFO_LEGACY_X2APIC) == 0) {
156278749Skib				x2apic_mode = 0;
157278749Skib				if (bootverbose)
158278749Skib					printf(
159278749Skib       "x2APIC available but disabled inside VMWare without intr redirection\n");
160278749Skib			}
161278749Skib		}
162278473Skib		TUNABLE_INT_FETCH("hw.x2apic_enable", &x2apic_mode);
163278473Skib	}
164278473Skib
165167814Sjkim	lapic_init(madt->Address);
166121992Sjhb	printf("ACPI APIC Table: <%.*s %.*s>\n",
167167814Sjkim	    (int)sizeof(madt->Header.OemId), madt->Header.OemId,
168167814Sjkim	    (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
169121992Sjhb
170121992Sjhb	/*
171121992Sjhb	 * We ignore 64-bit local APIC override entries.  Should we
172121992Sjhb	 * perhaps emit a warning here if we find one?
173121992Sjhb	 */
174121992Sjhb	return (0);
175121992Sjhb}
176121992Sjhb
177121992Sjhb/*
178125048Sjhb * Enumerate I/O APICs and setup interrupt sources.
179121992Sjhb */
180121992Sjhbstatic int
181121992Sjhbmadt_setup_io(void)
182121992Sjhb{
183128930Sjhb	void *ioapic;
184128930Sjhb	u_int pin;
185121992Sjhb	int i;
186121992Sjhb
187125048Sjhb	/* Try to initialize ACPI so that we can access the FADT. */
188125048Sjhb	i = acpi_Startup();
189125048Sjhb	if (ACPI_FAILURE(i)) {
190125048Sjhb		printf("MADT: ACPI Startup failed with %s\n",
191125048Sjhb		    AcpiFormatException(i));
192125048Sjhb		printf("Try disabling either ACPI or apic support.\n");
193125048Sjhb		panic("Using MADT but ACPI doesn't work");
194125048Sjhb	}
195233623Sjhb
196233623Sjhb	ioapics = malloc(sizeof(*ioapics) * (MAX_APIC_ID + 1), M_MADT,
197233623Sjhb	    M_WAITOK | M_ZERO);
198233623Sjhb
199121992Sjhb	/* First, we run through adding I/O APIC's. */
200121992Sjhb	madt_walk_table(madt_parse_apics, NULL);
201121992Sjhb
202121992Sjhb	/* Second, we run through the table tweaking interrupt sources. */
203121992Sjhb	madt_walk_table(madt_parse_ints, NULL);
204121992Sjhb
205128930Sjhb	/*
206128930Sjhb	 * If there was not an explicit override entry for the SCI,
207128930Sjhb	 * force it to use level trigger and active-low polarity.
208128930Sjhb	 */
209128930Sjhb	if (!madt_found_sci_override) {
210167814Sjkim		if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic,
211167814Sjkim		    &pin) != 0)
212167814Sjkim			printf("MADT: Could not find APIC for SCI IRQ %u\n",
213167814Sjkim			    AcpiGbl_FADT.SciInterrupt);
214128930Sjhb		else {
215128930Sjhb			printf(
216128930Sjhb	"MADT: Forcing active-low polarity and level trigger for SCI\n");
217128930Sjhb			ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
218128930Sjhb			ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
219128930Sjhb		}
220128930Sjhb	}
221128930Sjhb
222121992Sjhb	/* Third, we register all the I/O APIC's. */
223169395Sjhb	for (i = 0; i <= MAX_APIC_ID; i++)
224121992Sjhb		if (ioapics[i].io_apic != NULL)
225121992Sjhb			ioapic_register(ioapics[i].io_apic);
226121992Sjhb
227121992Sjhb	/* Finally, we throw the switch to enable the I/O APIC's. */
228121992Sjhb	acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
229121992Sjhb
230233623Sjhb	free(ioapics, M_MADT);
231233623Sjhb	ioapics = NULL;
232233623Sjhb
233121992Sjhb	return (0);
234121992Sjhb}
235121992Sjhb
236121992Sjhbstatic void
237121992Sjhbmadt_register(void *dummy __unused)
238121992Sjhb{
239121992Sjhb
240121992Sjhb	apic_register_enumerator(&madt_enumerator);
241121992Sjhb}
242215009SjhbSYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, madt_register, NULL);
243121992Sjhb
244121992Sjhb/*
245121992Sjhb * Call the handler routine for each entry in the MADT table.
246121992Sjhb */
247121992Sjhbstatic void
248197439Sjhbmadt_walk_table(acpi_subtable_handler *handler, void *arg)
249121992Sjhb{
250121992Sjhb
251197439Sjhb	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
252197439Sjhb	    handler, arg);
253121992Sjhb}
254121992Sjhb
255121992Sjhbstatic void
256167814Sjkimmadt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
257121992Sjhb{
258167814Sjkim	ACPI_MADT_LOCAL_APIC *proc;
259121992Sjhb	struct lapic_info *la;
260121992Sjhb
261121992Sjhb	switch (entry->Type) {
262167814Sjkim	case ACPI_MADT_TYPE_LOCAL_APIC:
263121992Sjhb		/*
264121992Sjhb		 * The MADT does not include a BSP flag, so we have to
265121992Sjhb		 * let the MP code figure out which CPU is the BSP on
266121992Sjhb		 * its own.
267121992Sjhb		 */
268167814Sjkim		proc = (ACPI_MADT_LOCAL_APIC *)entry;
269121992Sjhb		if (bootverbose)
270167814Sjkim			printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
271167814Sjkim			    proc->Id, proc->ProcessorId,
272167814Sjkim			    (proc->LapicFlags & ACPI_MADT_ENABLED) ?
273167814Sjkim			    "enabled" : "disabled");
274167814Sjkim		if (!(proc->LapicFlags & ACPI_MADT_ENABLED))
275130310Sjhb			break;
276169395Sjhb		if (proc->Id > MAX_APIC_ID)
277167814Sjkim			panic("%s: CPU ID %u too high", __func__, proc->Id);
278167814Sjkim		la = &lapics[proc->Id];
279130310Sjhb		KASSERT(la->la_enabled == 0,
280167814Sjkim		    ("Duplicate local APIC ID %u", proc->Id));
281130310Sjhb		la->la_enabled = 1;
282129960Sjhb		la->la_acpi_id = proc->ProcessorId;
283167814Sjkim		lapic_create(proc->Id, 0);
284121992Sjhb		break;
285121992Sjhb	}
286121992Sjhb}
287121992Sjhb
288121992Sjhb
289121992Sjhb/*
290121992Sjhb * Add an I/O APIC from an entry in the table.
291121992Sjhb */
292121992Sjhbstatic void
293167814Sjkimmadt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
294121992Sjhb{
295167814Sjkim	ACPI_MADT_IO_APIC *apic;
296121992Sjhb
297121992Sjhb	switch (entry->Type) {
298167814Sjkim	case ACPI_MADT_TYPE_IO_APIC:
299167814Sjkim		apic = (ACPI_MADT_IO_APIC *)entry;
300121992Sjhb		if (bootverbose)
301167814Sjkim			printf(
302167814Sjkim			    "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
303167814Sjkim			    apic->Id, apic->GlobalIrqBase,
304123326Snjl			    (void *)(uintptr_t)apic->Address);
305169395Sjhb		if (apic->Id > MAX_APIC_ID)
306167814Sjkim			panic("%s: I/O APIC ID %u too high", __func__,
307167814Sjkim			    apic->Id);
308167814Sjkim		if (ioapics[apic->Id].io_apic != NULL)
309167814Sjkim			panic("%s: Double APIC ID %u", __func__, apic->Id);
310189404Sjhb		if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
311189404Sjhb			printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
312189404Sjhb			break;
313189404Sjhb		}
314167814Sjkim		ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
315167814Sjkim		    apic->Id, apic->GlobalIrqBase);
316167814Sjkim		ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
317121992Sjhb		break;
318121992Sjhb	default:
319121992Sjhb		break;
320121992Sjhb	}
321121992Sjhb}
322121992Sjhb
323121992Sjhb/*
324129128Sjhb * Determine properties of an interrupt source.  Note that for ACPI these
325129128Sjhb * functions are only used for ISA interrupts, so we assume ISA bus values
326128930Sjhb * (Active Hi, Edge Triggered) for conforming values except for the ACPI
327129128Sjhb * SCI for which we use Active Lo, Level Triggered.
328121992Sjhb */
329128930Sjhbstatic enum intr_polarity
330167814Sjkiminterrupt_polarity(UINT16 IntiFlags, UINT8 Source)
331121992Sjhb{
332121992Sjhb
333167814Sjkim	switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
334263859Stakawata	default:
335269184Sakiyama		printf("WARNING: Bogus Interrupt Polarity. Assume CONFORMS\n");
336263859Stakawata		/* FALLTHROUGH*/
337167814Sjkim	case ACPI_MADT_POLARITY_CONFORMS:
338167814Sjkim		if (Source == AcpiGbl_FADT.SciInterrupt)
339128930Sjhb			return (INTR_POLARITY_LOW);
340128930Sjhb		else
341128930Sjhb			return (INTR_POLARITY_HIGH);
342167814Sjkim	case ACPI_MADT_POLARITY_ACTIVE_HIGH:
343128930Sjhb		return (INTR_POLARITY_HIGH);
344167814Sjkim	case ACPI_MADT_POLARITY_ACTIVE_LOW:
345263859Stakawata		return (INTR_POLARITY_LOW);
346121992Sjhb	}
347121992Sjhb}
348121992Sjhb
349128930Sjhbstatic enum intr_trigger
350167814Sjkiminterrupt_trigger(UINT16 IntiFlags, UINT8 Source)
351121992Sjhb{
352121992Sjhb
353167814Sjkim	switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
354263859Stakawata	default:
355269184Sakiyama		printf("WARNING: Bogus Interrupt Trigger Mode. Assume CONFORMS.\n");
356263859Stakawata		/*FALLTHROUGH*/
357167814Sjkim	case ACPI_MADT_TRIGGER_CONFORMS:
358167814Sjkim		if (Source == AcpiGbl_FADT.SciInterrupt)
359128930Sjhb			return (INTR_TRIGGER_LEVEL);
360128930Sjhb		else
361128930Sjhb			return (INTR_TRIGGER_EDGE);
362167814Sjkim	case ACPI_MADT_TRIGGER_EDGE:
363128930Sjhb		return (INTR_TRIGGER_EDGE);
364167814Sjkim	case ACPI_MADT_TRIGGER_LEVEL:
365263859Stakawata		return (INTR_TRIGGER_LEVEL);
366121992Sjhb	}
367121992Sjhb}
368121992Sjhb
369121992Sjhb/*
370121992Sjhb * Find the local APIC ID associated with a given ACPI Processor ID.
371121992Sjhb */
372121992Sjhbstatic int
373121992Sjhbmadt_find_cpu(u_int acpi_id, u_int *apic_id)
374121992Sjhb{
375129960Sjhb	int i;
376121992Sjhb
377169395Sjhb	for (i = 0; i <= MAX_APIC_ID; i++) {
378130310Sjhb		if (!lapics[i].la_enabled)
379129960Sjhb			continue;
380129960Sjhb		if (lapics[i].la_acpi_id != acpi_id)
381129960Sjhb			continue;
382129960Sjhb		*apic_id = i;
383130310Sjhb		return (0);
384129960Sjhb	}
385129960Sjhb	return (ENOENT);
386121992Sjhb}
387121992Sjhb
388121992Sjhb/*
389121992Sjhb * Find the IO APIC and pin on that APIC associated with a given global
390121992Sjhb * interrupt.
391121992Sjhb */
392121992Sjhbstatic int
393121992Sjhbmadt_find_interrupt(int intr, void **apic, u_int *pin)
394121992Sjhb{
395121992Sjhb	int i, best;
396121992Sjhb
397121992Sjhb	best = -1;
398169395Sjhb	for (i = 0; i <= MAX_APIC_ID; i++) {
399121992Sjhb		if (ioapics[i].io_apic == NULL ||
400121992Sjhb		    ioapics[i].io_vector > intr)
401121992Sjhb			continue;
402121992Sjhb		if (best == -1 ||
403121992Sjhb		    ioapics[best].io_vector < ioapics[i].io_vector)
404121992Sjhb			best = i;
405121992Sjhb	}
406121992Sjhb	if (best == -1)
407121992Sjhb		return (ENOENT);
408121992Sjhb	*apic = ioapics[best].io_apic;
409121992Sjhb	*pin = intr - ioapics[best].io_vector;
410121992Sjhb	if (*pin > 32)
411121992Sjhb		printf("WARNING: Found intpin of %u for vector %d\n", *pin,
412121992Sjhb		    intr);
413121992Sjhb	return (0);
414121992Sjhb}
415121992Sjhb
416269512Sroygervoid
417269512Sroygermadt_parse_interrupt_values(void *entry,
418269512Sroyger    enum intr_trigger *trig, enum intr_polarity *pol)
419121992Sjhb{
420269512Sroyger	ACPI_MADT_INTERRUPT_OVERRIDE *intr;
421128930Sjhb	char buf[64];
422121992Sjhb
423269512Sroyger	intr = entry;
424269512Sroyger
425121992Sjhb	if (bootverbose)
426142257Sjhb		printf("MADT: Interrupt override: source %u, irq %u\n",
427167814Sjkim		    intr->SourceIrq, intr->GlobalIrq);
428121992Sjhb	KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
429121992Sjhb
430125048Sjhb	/*
431128930Sjhb	 * Lookup the appropriate trigger and polarity modes for this
432128930Sjhb	 * entry.
433128930Sjhb	 */
434269512Sroyger	*trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
435269512Sroyger	*pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
436269512Sroyger
437128930Sjhb	/*
438125048Sjhb	 * If the SCI is identity mapped but has edge trigger and
439128329Sjhb	 * active-hi polarity or the force_sci_lo tunable is set,
440128329Sjhb	 * force it to use level/lo.
441125048Sjhb	 */
442167814Sjkim	if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
443128930Sjhb		madt_found_sci_override = 1;
444128930Sjhb		if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
445128930Sjhb			if (tolower(buf[0]) == 'e')
446269512Sroyger				*trig = INTR_TRIGGER_EDGE;
447128930Sjhb			else if (tolower(buf[0]) == 'l')
448269512Sroyger				*trig = INTR_TRIGGER_LEVEL;
449128930Sjhb			else
450128930Sjhb				panic(
451128930Sjhb				"Invalid trigger %s: must be 'edge' or 'level'",
452128930Sjhb				    buf);
453128930Sjhb			printf("MADT: Forcing SCI to %s trigger\n",
454269512Sroyger			    *trig == INTR_TRIGGER_EDGE ? "edge" : "level");
455128930Sjhb		}
456128930Sjhb		if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
457128930Sjhb			if (tolower(buf[0]) == 'h')
458269512Sroyger				*pol = INTR_POLARITY_HIGH;
459128930Sjhb			else if (tolower(buf[0]) == 'l')
460269512Sroyger				*pol = INTR_POLARITY_LOW;
461128930Sjhb			else
462128930Sjhb				panic(
463128930Sjhb				"Invalid polarity %s: must be 'high' or 'low'",
464128930Sjhb				    buf);
465128930Sjhb			printf("MADT: Forcing SCI to active %s polarity\n",
466269512Sroyger			    *pol == INTR_POLARITY_HIGH ? "high" : "low");
467128930Sjhb		}
468128930Sjhb	}
469269512Sroyger}
470125048Sjhb
471269512Sroyger/*
472269512Sroyger * Parse an interrupt source override for an ISA interrupt.
473269512Sroyger */
474269512Sroygerstatic void
475269512Sroygermadt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
476269512Sroyger{
477269512Sroyger	void *new_ioapic, *old_ioapic;
478269512Sroyger	u_int new_pin, old_pin;
479269512Sroyger	enum intr_trigger trig;
480269512Sroyger	enum intr_polarity pol;
481269512Sroyger
482269512Sroyger	if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
483269512Sroyger	    intr->GlobalIrq == 2) {
484269512Sroyger		if (bootverbose)
485269512Sroyger			printf("MADT: Skipping timer override\n");
486269512Sroyger		return;
487269512Sroyger	}
488269512Sroyger
489269512Sroyger	if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
490269512Sroyger		printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
491269512Sroyger		    intr->GlobalIrq, intr->SourceIrq);
492269512Sroyger		return;
493269512Sroyger	}
494269512Sroyger
495269512Sroyger	madt_parse_interrupt_values(intr, &trig, &pol);
496269512Sroyger
497128930Sjhb	/* Remap the IRQ if it is mapped to a different interrupt vector. */
498167814Sjkim	if (intr->SourceIrq != intr->GlobalIrq) {
499125048Sjhb		/*
500125048Sjhb		 * If the SCI is remapped to a non-ISA global interrupt,
501125048Sjhb		 * then override the vector we use to setup and allocate
502125048Sjhb		 * the interrupt.
503125048Sjhb		 */
504167814Sjkim		if (intr->GlobalIrq > 15 &&
505167814Sjkim		    intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
506167814Sjkim			acpi_OverrideInterruptLevel(intr->GlobalIrq);
507122502Sjhb		else
508167814Sjkim			ioapic_remap_vector(new_ioapic, new_pin,
509167814Sjkim			    intr->SourceIrq);
510167814Sjkim		if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
511122149Sjhb		    &old_pin) != 0)
512167814Sjkim			printf("MADT: Could not find APIC for source IRQ %u\n",
513167814Sjkim			    intr->SourceIrq);
514122172Sjhb		else if (ioapic_get_vector(old_ioapic, old_pin) ==
515167814Sjkim		    intr->SourceIrq)
516122149Sjhb			ioapic_disable_pin(old_ioapic, old_pin);
517122149Sjhb	}
518128930Sjhb
519128930Sjhb	/* Program the polarity and trigger mode. */
520128930Sjhb	ioapic_set_triggermode(new_ioapic, new_pin, trig);
521128930Sjhb	ioapic_set_polarity(new_ioapic, new_pin, pol);
522121992Sjhb}
523121992Sjhb
524121992Sjhb/*
525121992Sjhb * Parse an entry for an NMI routed to an IO APIC.
526121992Sjhb */
527121992Sjhbstatic void
528167814Sjkimmadt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
529121992Sjhb{
530121992Sjhb	void *ioapic;
531121992Sjhb	u_int pin;
532121992Sjhb
533167814Sjkim	if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
534167814Sjkim		printf("MADT: Could not find APIC for vector %u\n",
535167814Sjkim		    nmi->GlobalIrq);
536121992Sjhb		return;
537121992Sjhb	}
538121992Sjhb
539121992Sjhb	ioapic_set_nmi(ioapic, pin);
540167814Sjkim	if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
541121992Sjhb		ioapic_set_triggermode(ioapic, pin,
542167814Sjkim		    interrupt_trigger(nmi->IntiFlags, 0));
543263794Stakawata	if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
544121992Sjhb		ioapic_set_polarity(ioapic, pin,
545167814Sjkim		    interrupt_polarity(nmi->IntiFlags, 0));
546121992Sjhb}
547121992Sjhb
548121992Sjhb/*
549121992Sjhb * Parse an entry for an NMI routed to a local APIC LVT pin.
550121992Sjhb */
551121992Sjhbstatic void
552167814Sjkimmadt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
553121992Sjhb{
554121992Sjhb	u_int apic_id, pin;
555121992Sjhb
556123326Snjl	if (nmi->ProcessorId == 0xff)
557121992Sjhb		apic_id = APIC_ID_ALL;
558123326Snjl	else if (madt_find_cpu(nmi->ProcessorId, &apic_id) != 0) {
559121992Sjhb		if (bootverbose)
560167814Sjkim			printf("MADT: Ignoring local NMI routed to "
561167814Sjkim			    "ACPI CPU %u\n", nmi->ProcessorId);
562121992Sjhb		return;
563121992Sjhb	}
564123326Snjl	if (nmi->Lint == 0)
565259140Sjhb		pin = APIC_LVT_LINT0;
566121992Sjhb	else
567259140Sjhb		pin = APIC_LVT_LINT1;
568121992Sjhb	lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
569167814Sjkim	if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
570121992Sjhb		lapic_set_lvt_triggermode(apic_id, pin,
571167814Sjkim		    interrupt_trigger(nmi->IntiFlags, 0));
572167814Sjkim	if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
573121992Sjhb		lapic_set_lvt_polarity(apic_id, pin,
574167814Sjkim		    interrupt_polarity(nmi->IntiFlags, 0));
575121992Sjhb}
576121992Sjhb
577121992Sjhb/*
578121992Sjhb * Parse interrupt entries.
579121992Sjhb */
580121992Sjhbstatic void
581167814Sjkimmadt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
582121992Sjhb{
583121992Sjhb
584121992Sjhb	switch (entry->Type) {
585167814Sjkim	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
586121992Sjhb		madt_parse_interrupt_override(
587167814Sjkim			(ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
588121992Sjhb		break;
589167814Sjkim	case ACPI_MADT_TYPE_NMI_SOURCE:
590167814Sjkim		madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
591121992Sjhb		break;
592167814Sjkim	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
593167814Sjkim		madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
594121992Sjhb		break;
595121992Sjhb	}
596121992Sjhb}
597121992Sjhb
598121992Sjhb/*
599121992Sjhb * Setup per-CPU ACPI IDs.
600121992Sjhb */
601121992Sjhbstatic void
602121992Sjhbmadt_set_ids(void *dummy)
603121992Sjhb{
604129960Sjhb	struct lapic_info *la;
605121992Sjhb	struct pcpu *pc;
606129960Sjhb	u_int i;
607121992Sjhb
608121992Sjhb	if (madt == NULL)
609121992Sjhb		return;
610209059Sjhb	CPU_FOREACH(i) {
611121992Sjhb		pc = pcpu_find(i);
612167814Sjkim		KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
613129960Sjhb		la = &lapics[pc->pc_apic_id];
614130310Sjhb		if (!la->la_enabled)
615129960Sjhb			panic("APIC: CPU with APIC ID %u is not enabled",
616129960Sjhb			    pc->pc_apic_id);
617129960Sjhb		pc->pc_acpi_id = la->la_acpi_id;
618129960Sjhb		if (bootverbose)
619129960Sjhb			printf("APIC: CPU %u has ACPI ID %u\n", i,
620129960Sjhb			    la->la_acpi_id);
621121992Sjhb	}
622121992Sjhb}
623256073SgibbsSYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_MIDDLE, madt_set_ids, NULL);
624