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