madt.c revision 129094
1/*-
2 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/i386/acpica/madt.c 129094 2004-05-10 18:33:08Z jhb $");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/bus.h>
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/smp.h>
39
40#include <vm/vm.h>
41#include <vm/vm_param.h>
42#include <vm/pmap.h>
43
44#include <machine/apicreg.h>
45#include <machine/frame.h>
46#include <machine/intr_machdep.h>
47#include <machine/apicvar.h>
48#include <machine/md_var.h>
49#include <machine/specialreg.h>
50
51#include "acpi.h"
52#include <contrib/dev/acpica/actables.h>
53#include <dev/acpica/acpivar.h>
54#include <dev/pci/pcivar.h>
55
56#define	NIOAPICS		32	/* Max number of I/O APICs */
57#define	NLAPICS			32	/* Max number of local APICs */
58
59typedef	void madt_entry_handler(APIC_HEADER *entry, void *arg);
60
61/* These two arrays are indexed by APIC IDs. */
62struct ioapic_info {
63	void *io_apic;
64	UINT32 io_vector;
65} ioapics[NIOAPICS];
66
67struct lapic_info {
68	u_int la_present:1;
69	u_int la_enabled:1;
70	u_int la_apic_id:8;
71} lapics[NLAPICS + 1];
72
73static int madt_found_sci_override;
74static MULTIPLE_APIC_TABLE *madt;
75static vm_paddr_t madt_physaddr;
76static vm_offset_t madt_length;
77
78MALLOC_DEFINE(M_MADT, "MADT Table", "ACPI MADT Table Items");
79
80static enum intr_polarity interrupt_polarity(UINT16 Polarity, UINT8 Source);
81static enum intr_trigger interrupt_trigger(UINT16 TriggerMode, UINT8 Source);
82static int	madt_find_cpu(u_int acpi_id, u_int *apic_id);
83static int	madt_find_interrupt(int intr, void **apic, u_int *pin);
84static void	*madt_map(vm_paddr_t pa, int offset, vm_offset_t length);
85static void	*madt_map_table(vm_paddr_t pa, int offset, const char *sig);
86static void	madt_parse_apics(APIC_HEADER *entry, void *arg);
87static void	madt_parse_interrupt_override(MADT_INTERRUPT_OVERRIDE *intr);
88static void	madt_parse_ints(APIC_HEADER *entry, void *arg __unused);
89static void	madt_parse_local_nmi(MADT_LOCAL_APIC_NMI *nmi);
90static void	madt_parse_nmi(MADT_NMI_SOURCE *nmi);
91static int	madt_probe(void);
92static int	madt_probe_cpus(void);
93static void	madt_probe_cpus_handler(APIC_HEADER *entry, void *arg __unused);
94static int	madt_probe_table(vm_paddr_t address);
95static void	madt_register(void *dummy);
96static int	madt_setup_local(void);
97static int	madt_setup_io(void);
98static void	madt_unmap(void *data, vm_offset_t length);
99static void	madt_unmap_table(void *table);
100static void	madt_walk_table(madt_entry_handler *handler, void *arg);
101
102static struct apic_enumerator madt_enumerator = {
103	"MADT",
104	madt_probe,
105	madt_probe_cpus,
106	madt_setup_local,
107	madt_setup_io
108};
109
110/*
111 * Code to abuse the crashdump map to map in the tables for the early
112 * probe.  We cheat and make the following assumptions about how we
113 * use this KVA: page 0 is used to map in the first page of each table
114 * found via the RSDT or XSDT and pages 1 to n are used to map in the
115 * RSDT or XSDT.  The offset is in pages; the length is in bytes.
116 */
117static void *
118madt_map(vm_paddr_t pa, int offset, vm_offset_t length)
119{
120	vm_offset_t va, off;
121	void *data;
122
123	off = pa & PAGE_MASK;
124	length = roundup(length + off, PAGE_SIZE);
125	pa = pa & PG_FRAME;
126	va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
127	    (offset * PAGE_SIZE);
128	data = (void *)(va + off);
129	length -= PAGE_SIZE;
130	while (length > 0) {
131		va += PAGE_SIZE;
132		pa += PAGE_SIZE;
133		length -= PAGE_SIZE;
134		pmap_kenter(va, pa);
135		invlpg(va);
136	}
137	return (data);
138}
139
140static void
141madt_unmap(void *data, vm_offset_t length)
142{
143	vm_offset_t va, off;
144
145	va = (vm_offset_t)data;
146	off = va & PAGE_MASK;
147	length = roundup(length + off, PAGE_SIZE);
148	va &= ~PAGE_MASK;
149	while (length > 0) {
150		pmap_kremove(va);
151		invlpg(va);
152		va += PAGE_SIZE;
153		length -= PAGE_SIZE;
154	}
155}
156
157static void *
158madt_map_table(vm_paddr_t pa, int offset, const char *sig)
159{
160	ACPI_TABLE_HEADER *header;
161	vm_offset_t length;
162	void *table;
163
164	header = madt_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
165	if (strncmp(header->Signature, sig, 4) != 0) {
166		madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
167		return (NULL);
168	}
169	length = header->Length;
170	madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
171	table = madt_map(pa, offset, length);
172	if (ACPI_FAILURE(AcpiTbVerifyTableChecksum(table))) {
173		if (bootverbose)
174			printf("MADT: Failed checksum for table %s\n", sig);
175		madt_unmap(table, length);
176		return (NULL);
177	}
178	return (table);
179}
180
181static void
182madt_unmap_table(void *table)
183{
184	ACPI_TABLE_HEADER *header;
185
186	header = (ACPI_TABLE_HEADER *)table;
187	madt_unmap(table, header->Length);
188}
189
190/*
191 * Look for an ACPI Multiple APIC Description Table ("APIC")
192 */
193static int
194madt_probe(void)
195{
196	ACPI_POINTER rsdp_ptr;
197	RSDP_DESCRIPTOR *rsdp;
198	RSDT_DESCRIPTOR *rsdt;
199	XSDT_DESCRIPTOR *xsdt;
200	int i, count;
201
202	if (resource_disabled("acpi", 0))
203		return (ENXIO);
204
205	/*
206	 * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
207	 * calls pmap_mapdev() to find the RSDP, we assume that we can use
208	 * pmap_mapdev() to map the RSDP.
209	 */
210	if (AcpiOsGetRootPointer(ACPI_LOGICAL_ADDRESSING, &rsdp_ptr) != AE_OK)
211		return (ENXIO);
212#ifdef __i386__
213	KASSERT(rsdp_ptr.Pointer.Physical < KERNLOAD, ("RSDP too high"));
214#endif
215	rsdp = pmap_mapdev(rsdp_ptr.Pointer.Physical, sizeof(RSDP_DESCRIPTOR));
216	if (rsdp == NULL) {
217		if (bootverbose)
218			printf("MADT: Failed to map RSDP\n");
219		return (ENXIO);
220	}
221
222	/*
223	 * For ACPI < 2.0, use the RSDT.  For ACPI >= 2.0, use the XSDT.
224	 * We map the XSDT and RSDT at page 1 in the crashdump area.
225	 * Page 0 is used to map in the headers of candidate ACPI tables.
226	 */
227	if (rsdp->Revision >= 2) {
228		/*
229		 * AcpiOsGetRootPointer only verifies the checksum for
230		 * the version 1.0 portion of the RSDP.  Version 2.0 has
231		 * an additional checksum that we verify first.
232		 */
233		if (AcpiTbChecksum(rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0) {
234			if (bootverbose)
235				printf("MADT: RSDP failed extended checksum\n");
236			return (ENXIO);
237		}
238		xsdt = madt_map_table(rsdp->XsdtPhysicalAddress, 1, XSDT_SIG);
239		if (xsdt == NULL) {
240			if (bootverbose)
241				printf("MADT: Failed to map XSDT\n");
242			return (ENXIO);
243		}
244		count = (xsdt->Length - sizeof(ACPI_TABLE_HEADER)) /
245		    sizeof(UINT64);
246		for (i = 0; i < count; i++)
247			if (madt_probe_table(xsdt->TableOffsetEntry[i]))
248				break;
249		madt_unmap_table(xsdt);
250	} else {
251		rsdt = madt_map_table(rsdp->RsdtPhysicalAddress, 1, RSDT_SIG);
252		if (rsdt == NULL) {
253			if (bootverbose)
254				printf("MADT: Failed to map RSDT\n");
255			return (ENXIO);
256		}
257		count = (rsdt->Length - sizeof(ACPI_TABLE_HEADER)) /
258		    sizeof(UINT32);
259		for (i = 0; i < count; i++)
260			if (madt_probe_table(rsdt->TableOffsetEntry[i]))
261				break;
262		madt_unmap_table(rsdt);
263	}
264	pmap_unmapdev((vm_offset_t)rsdp, sizeof(RSDP_DESCRIPTOR));
265	if (madt_physaddr == 0) {
266		if (bootverbose)
267			printf("MADT: No MADT table found\n");
268		return (ENXIO);
269	}
270	if (bootverbose)
271		printf("MADT: Found table at 0x%jx\n",
272		    (uintmax_t)madt_physaddr);
273
274	/*
275	 * Verify that we can map the full table and that its checksum is
276	 * correct, etc.
277	 */
278	madt = madt_map_table(madt_physaddr, 0, APIC_SIG);
279	if (madt == NULL)
280		return (ENXIO);
281	madt_unmap_table(madt);
282	madt = NULL;
283
284	return (0);
285}
286
287/*
288 * See if a given ACPI table is the MADT.
289 */
290static int
291madt_probe_table(vm_paddr_t address)
292{
293	ACPI_TABLE_HEADER *table;
294
295	table = madt_map(address, 0, sizeof(ACPI_TABLE_HEADER));
296	if (table == NULL) {
297		if (bootverbose)
298			printf("MADT: Failed to map table at 0x%jx\n",
299			    (uintmax_t)address);
300		return (0);
301	}
302	if (bootverbose)
303		printf("Table '%.4s' at 0x%jx\n", table->Signature,
304		    (uintmax_t)address);
305
306	if (strncmp(table->Signature, APIC_SIG, 4) != 0) {
307		madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
308		return (0);
309	}
310	madt_physaddr = address;
311	madt_length = table->Length;
312	madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
313	return (1);
314}
315
316/*
317 * Run through the MP table enumerating CPUs.
318 */
319static int
320madt_probe_cpus(void)
321{
322
323	madt = madt_map_table(madt_physaddr, 0, APIC_SIG);
324	KASSERT(madt != NULL, ("Unable to re-map MADT"));
325	madt_walk_table(madt_probe_cpus_handler, NULL);
326	madt_unmap_table(madt);
327	madt = NULL;
328	return (0);
329}
330
331/*
332 * Initialize the local APIC on the BSP.
333 */
334static int
335madt_setup_local(void)
336{
337
338	madt = pmap_mapdev(madt_physaddr, madt_length);
339	lapic_init((uintptr_t)madt->LocalApicAddress);
340	printf("ACPI APIC Table: <%.*s %.*s>\n",
341	    (int)sizeof(madt->OemId), madt->OemId,
342	    (int)sizeof(madt->OemTableId), madt->OemTableId);
343
344	/*
345	 * We ignore 64-bit local APIC override entries.  Should we
346	 * perhaps emit a warning here if we find one?
347	 */
348	return (0);
349}
350
351/*
352 * Enumerate I/O APICs and setup interrupt sources.
353 */
354static int
355madt_setup_io(void)
356{
357	void *ioapic;
358	u_int pin;
359	int i;
360
361	/* Try to initialize ACPI so that we can access the FADT. */
362	i = acpi_Startup();
363	if (ACPI_FAILURE(i)) {
364		printf("MADT: ACPI Startup failed with %s\n",
365		    AcpiFormatException(i));
366		printf("Try disabling either ACPI or apic support.\n");
367		panic("Using MADT but ACPI doesn't work");
368	}
369
370	/* First, we run through adding I/O APIC's. */
371	madt_walk_table(madt_parse_apics, NULL);
372
373	/* Second, we run through the table tweaking interrupt sources. */
374	madt_walk_table(madt_parse_ints, NULL);
375
376	/*
377	 * If there was not an explicit override entry for the SCI,
378	 * force it to use level trigger and active-low polarity.
379	 */
380	if (!madt_found_sci_override) {
381		if (madt_find_interrupt(AcpiGbl_FADT->SciInt, &ioapic, &pin)
382		    != 0)
383			printf("MADT: Could not find APIC for SCI IRQ %d\n",
384			    AcpiGbl_FADT->SciInt);
385		else {
386			printf(
387	"MADT: Forcing active-low polarity and level trigger for SCI\n");
388			ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
389			ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
390		}
391	}
392
393	/* Third, we register all the I/O APIC's. */
394	for (i = 0; i < NIOAPICS; i++)
395		if (ioapics[i].io_apic != NULL)
396			ioapic_register(ioapics[i].io_apic);
397
398	/* Finally, we throw the switch to enable the I/O APIC's. */
399	acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
400
401	return (0);
402}
403
404static void
405madt_register(void *dummy __unused)
406{
407
408	apic_register_enumerator(&madt_enumerator);
409}
410SYSINIT(madt_register, SI_SUB_CPU - 1, SI_ORDER_FIRST, madt_register, NULL)
411
412/*
413 * Call the handler routine for each entry in the MADT table.
414 */
415static void
416madt_walk_table(madt_entry_handler *handler, void *arg)
417{
418	APIC_HEADER *entry;
419	u_char *p, *end;
420
421	end = (u_char *)(madt) + madt->Length;
422	for (p = (u_char *)(madt + 1); p < end; ) {
423		entry = (APIC_HEADER *)p;
424		handler(entry, arg);
425		p += entry->Length;
426	}
427}
428
429static void
430madt_probe_cpus_handler(APIC_HEADER *entry, void *arg)
431{
432	MADT_PROCESSOR_APIC *proc;
433	struct lapic_info *la;
434
435	switch (entry->Type) {
436	case APIC_PROCESSOR:
437		/*
438		 * The MADT does not include a BSP flag, so we have to
439		 * let the MP code figure out which CPU is the BSP on
440		 * its own.
441		 */
442		proc = (MADT_PROCESSOR_APIC *)entry;
443		if (bootverbose)
444			printf("MADT: Found CPU APIC ID %d ACPI ID %d: %s\n",
445			    proc->LocalApicId, proc->ProcessorId,
446			    proc->ProcessorEnabled ? "enabled" : "disabled");
447		if (proc->ProcessorId > NLAPICS)
448			panic("%s: CPU ID %d too high", __func__,
449			    proc->ProcessorId);
450		la = &lapics[proc->ProcessorId];
451		KASSERT(la->la_present == 0,
452		    ("Duplicate local ACPI ID %d", proc->ProcessorId));
453		la->la_present = 1;
454		la->la_apic_id = proc->LocalApicId;
455		if (proc->ProcessorEnabled) {
456			la->la_enabled = 1;
457			lapic_create(proc->LocalApicId, 0);
458		}
459		break;
460	}
461}
462
463
464/*
465 * Add an I/O APIC from an entry in the table.
466 */
467static void
468madt_parse_apics(APIC_HEADER *entry, void *arg __unused)
469{
470	MADT_IO_APIC *apic;
471
472	switch (entry->Type) {
473	case APIC_IO:
474		apic = (MADT_IO_APIC *)entry;
475		if (bootverbose)
476			printf("MADT: Found IO APIC ID %d, Interrupt %d at %p\n",
477			    apic->IoApicId, apic->Interrupt,
478			    (void *)(uintptr_t)apic->Address);
479		if (apic->IoApicId >= NIOAPICS)
480			panic("%s: I/O APIC ID %d too high", __func__,
481			    apic->IoApicId);
482		if (ioapics[apic->IoApicId].io_apic != NULL)
483			panic("%s: Double APIC ID %d", __func__,
484			    apic->IoApicId);
485		ioapics[apic->IoApicId].io_apic = ioapic_create(
486			(uintptr_t)apic->Address, apic->IoApicId,
487			    apic->Interrupt);
488		ioapics[apic->IoApicId].io_vector = apic->Interrupt;
489		break;
490	default:
491		break;
492	}
493}
494
495/*
496 * Determine properties of an interrupt source.  Note that for ACPI,
497 * these are only used for ISA interrupts, so we assume ISA bus values
498 * (Active Hi, Edge Triggered) for conforming values except for the ACPI
499 * SCI for which we use Active Lo, Level Triggered..
500 */
501static enum intr_polarity
502interrupt_polarity(UINT16 Polarity, UINT8 Source)
503{
504
505	switch (Polarity) {
506	case POLARITY_CONFORMS:
507		if (Source == AcpiGbl_FADT->SciInt)
508			return (INTR_POLARITY_LOW);
509		else
510			return (INTR_POLARITY_HIGH);
511	case POLARITY_ACTIVE_HIGH:
512		return (INTR_POLARITY_HIGH);
513	case POLARITY_ACTIVE_LOW:
514		return (INTR_POLARITY_LOW);
515	default:
516		panic("Bogus Interrupt Polarity");
517	}
518}
519
520static enum intr_trigger
521interrupt_trigger(UINT16 TriggerMode, UINT8 Source)
522{
523
524	switch (TriggerMode) {
525	case TRIGGER_CONFORMS:
526		if (Source == AcpiGbl_FADT->SciInt)
527			return (INTR_TRIGGER_LEVEL);
528		else
529			return (INTR_TRIGGER_EDGE);
530	case TRIGGER_EDGE:
531		return (INTR_TRIGGER_EDGE);
532	case TRIGGER_LEVEL:
533		return (INTR_TRIGGER_LEVEL);
534	default:
535		panic("Bogus Interrupt Trigger Mode");
536	}
537}
538
539/*
540 * Find the local APIC ID associated with a given ACPI Processor ID.
541 */
542static int
543madt_find_cpu(u_int acpi_id, u_int *apic_id)
544{
545
546	if (!lapics[acpi_id].la_present)
547		return (ENOENT);
548	*apic_id = lapics[acpi_id].la_apic_id;
549	if (lapics[acpi_id].la_enabled)
550		return (0);
551	else
552		return (ENXIO);
553}
554
555/*
556 * Find the IO APIC and pin on that APIC associated with a given global
557 * interrupt.
558 */
559static int
560madt_find_interrupt(int intr, void **apic, u_int *pin)
561{
562	int i, best;
563
564	best = -1;
565	for (i = 0; i < NIOAPICS; i++) {
566		if (ioapics[i].io_apic == NULL ||
567		    ioapics[i].io_vector > intr)
568			continue;
569		if (best == -1 ||
570		    ioapics[best].io_vector < ioapics[i].io_vector)
571			best = i;
572	}
573	if (best == -1)
574		return (ENOENT);
575	*apic = ioapics[best].io_apic;
576	*pin = intr - ioapics[best].io_vector;
577	if (*pin > 32)
578		printf("WARNING: Found intpin of %u for vector %d\n", *pin,
579		    intr);
580	return (0);
581}
582
583/*
584 * Parse an interrupt source override for an ISA interrupt.
585 */
586static void
587madt_parse_interrupt_override(MADT_INTERRUPT_OVERRIDE *intr)
588{
589	void *new_ioapic, *old_ioapic;
590	u_int new_pin, old_pin;
591	enum intr_trigger trig;
592	enum intr_polarity pol;
593	char buf[64];
594
595	if (bootverbose)
596		printf("MADT: intr override: source %u, irq %u\n",
597		    intr->Source, intr->Interrupt);
598	KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
599	if (madt_find_interrupt(intr->Interrupt, &new_ioapic,
600	    &new_pin) != 0) {
601		printf("MADT: Could not find APIC for vector %d (IRQ %d)\n",
602		    intr->Interrupt, intr->Source);
603		return;
604	}
605
606	/*
607	 * Lookup the appropriate trigger and polarity modes for this
608	 * entry.
609	 */
610	trig = interrupt_trigger(intr->TriggerMode, intr->Source);
611	pol = interrupt_polarity(intr->Polarity, intr->Source);
612
613	/*
614	 * If the SCI is identity mapped but has edge trigger and
615	 * active-hi polarity or the force_sci_lo tunable is set,
616	 * force it to use level/lo.
617	 */
618	if (intr->Source == AcpiGbl_FADT->SciInt) {
619		madt_found_sci_override = 1;
620		if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
621			if (tolower(buf[0]) == 'e')
622				trig = INTR_TRIGGER_EDGE;
623			else if (tolower(buf[0]) == 'l')
624				trig = INTR_TRIGGER_LEVEL;
625			else
626				panic(
627				"Invalid trigger %s: must be 'edge' or 'level'",
628				    buf);
629			printf("MADT: Forcing SCI to %s trigger\n",
630			    trig == INTR_TRIGGER_EDGE ? "edge" : "level");
631		}
632		if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
633			if (tolower(buf[0]) == 'h')
634				pol = INTR_POLARITY_HIGH;
635			else if (tolower(buf[0]) == 'l')
636				pol = INTR_POLARITY_LOW;
637			else
638				panic(
639				"Invalid polarity %s: must be 'high' or 'low'",
640				    buf);
641			printf("MADT: Forcing SCI to active %s polarity\n",
642			    pol == INTR_POLARITY_HIGH ? "high" : "low");
643		}
644	}
645
646	/* Remap the IRQ if it is mapped to a different interrupt vector. */
647	if (intr->Source != intr->Interrupt) {
648		/*
649		 * If the SCI is remapped to a non-ISA global interrupt,
650		 * then override the vector we use to setup and allocate
651		 * the interrupt.
652		 */
653		if (intr->Interrupt > 15 &&
654		    intr->Source == AcpiGbl_FADT->SciInt)
655			acpi_OverrideInterruptLevel(intr->Interrupt);
656		else
657			ioapic_remap_vector(new_ioapic, new_pin, intr->Source);
658		if (madt_find_interrupt(intr->Source, &old_ioapic,
659		    &old_pin) != 0)
660			printf("MADT: Could not find APIC for source IRQ %d\n",
661			    intr->Source);
662		else if (ioapic_get_vector(old_ioapic, old_pin) ==
663		    intr->Source)
664			ioapic_disable_pin(old_ioapic, old_pin);
665	}
666
667	/* Program the polarity and trigger mode. */
668	ioapic_set_triggermode(new_ioapic, new_pin, trig);
669	ioapic_set_polarity(new_ioapic, new_pin, pol);
670}
671
672/*
673 * Parse an entry for an NMI routed to an IO APIC.
674 */
675static void
676madt_parse_nmi(MADT_NMI_SOURCE *nmi)
677{
678	void *ioapic;
679	u_int pin;
680
681	if (madt_find_interrupt(nmi->Interrupt, &ioapic, &pin) != 0) {
682		printf("MADT: Could not find APIC for vector %d\n",
683		    nmi->Interrupt);
684		return;
685	}
686
687	ioapic_set_nmi(ioapic, pin);
688	if (nmi->TriggerMode != TRIGGER_CONFORMS)
689		ioapic_set_triggermode(ioapic, pin,
690		    interrupt_trigger(nmi->TriggerMode, 0));
691	if (nmi->Polarity != TRIGGER_CONFORMS)
692		ioapic_set_polarity(ioapic, pin,
693		    interrupt_polarity(nmi->Polarity, 0));
694}
695
696/*
697 * Parse an entry for an NMI routed to a local APIC LVT pin.
698 */
699static void
700madt_parse_local_nmi(MADT_LOCAL_APIC_NMI *nmi)
701{
702	u_int apic_id, pin;
703
704	if (nmi->ProcessorId == 0xff)
705		apic_id = APIC_ID_ALL;
706	else if (madt_find_cpu(nmi->ProcessorId, &apic_id) != 0) {
707		if (bootverbose)
708			printf("MADT: Ignoring local NMI routed to ACPI CPU %u\n",
709			    nmi->ProcessorId);
710		return;
711	}
712	if (nmi->Lint == 0)
713		pin = LVT_LINT0;
714	else
715		pin = LVT_LINT1;
716	lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
717	if (nmi->TriggerMode != TRIGGER_CONFORMS)
718		lapic_set_lvt_triggermode(apic_id, pin,
719		    interrupt_trigger(nmi->TriggerMode, 0));
720	if (nmi->Polarity != POLARITY_CONFORMS)
721		lapic_set_lvt_polarity(apic_id, pin,
722		    interrupt_polarity(nmi->Polarity, 0));
723}
724
725/*
726 * Parse interrupt entries.
727 */
728static void
729madt_parse_ints(APIC_HEADER *entry, void *arg __unused)
730{
731
732	switch (entry->Type) {
733	case APIC_XRUPT_OVERRIDE:
734		madt_parse_interrupt_override(
735			(MADT_INTERRUPT_OVERRIDE *)entry);
736		break;
737	case APIC_NMI:
738		madt_parse_nmi((MADT_NMI_SOURCE *)entry);
739		break;
740	case APIC_LOCAL_NMI:
741		madt_parse_local_nmi((MADT_LOCAL_APIC_NMI *)entry);
742		break;
743	}
744}
745
746/*
747 * Setup per-CPU ACPI IDs.
748 */
749static void
750madt_set_ids(void *dummy)
751{
752	struct pcpu *pc;
753	u_int i, j;
754
755	if (madt == NULL)
756		return;
757	for (i = 0; i <= mp_maxid; i++) {
758		if (CPU_ABSENT(i))
759			continue;
760		pc = pcpu_find(i);
761		KASSERT(pc != NULL, ("no pcpu data for CPU %d", i));
762		for (j = 0; j < NLAPICS + 1; j++) {
763			if (!lapics[j].la_present || !lapics[j].la_enabled)
764				continue;
765			if (lapics[j].la_apic_id == pc->pc_apic_id) {
766				pc->pc_acpi_id = j;
767				if (bootverbose)
768					printf("APIC: CPU %u has ACPI ID %u\n",
769					    i, j);
770				break;
771			}
772		}
773		if (j == NLAPICS + 1)
774			panic("Unable to find ACPI ID for CPU %d", i);
775	}
776}
777SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_ANY, madt_set_ids, NULL)
778