2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
40 #include <machine_base/isa/isa_intr.h>
41 #include <machine_base/apic/lapic.h>
42 #include <machine_base/apic/ioapic.h>
43 #include <machine_base/apic/apicvar.h>
44 #include <machine_base/acpica/acpi_md_cpu.h>
45 #include <machine/specialreg.h>
47 #include <contrib/dev/acpica/source/include/acpi.h>
49 #include "acpi_sdt_var.h"
50 #include "acpi_sci_var.h"
54 #define MADT_VPRINTF(fmt, arg...) \
57 kprintf("ACPI MADT: " fmt , ##arg); \
60 #define MADT_INT_BUS_ISA 0
62 typedef int (*madt_iter_t)(void *,
63 const ACPI_SUBTABLE_HEADER *);
65 static int madt_check(vm_paddr_t);
66 static int madt_iterate_entries(ACPI_TABLE_MADT *,
69 static vm_paddr_t madt_lapic_pass1(void);
70 static int madt_lapic_pass2(int);
72 static int madt_lapic_enumerate(struct lapic_enumerator *);
73 static int madt_lapic_probe(struct lapic_enumerator *);
75 static void madt_ioapic_enumerate(
76 struct ioapic_enumerator *);
77 static int madt_ioapic_probe(struct ioapic_enumerator *);
79 static vm_paddr_t madt_phyaddr;
81 static boolean_t madt_use_x2apic = FALSE;
83 u_int cpu_id_to_acpi_id[NAPICID];
88 vm_paddr_t madt_paddr;
91 for (i = 0; i < NAPICID; ++i)
92 CPUID_TO_ACPIID(i) = (u_int)-1;
94 KKASSERT(madt_phyaddr == 0);
96 madt_paddr = sdt_search(ACPI_SIG_MADT);
97 if (madt_paddr == 0) {
98 kprintf("madt_probe: can't locate MADT\n");
102 /* Preliminary checks */
103 if (madt_check(madt_paddr)) {
104 kprintf("madt_probe: madt_check failed\n");
108 madt_phyaddr = madt_paddr;
110 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
113 madt_check(vm_paddr_t madt_paddr)
115 ACPI_TABLE_MADT *madt;
118 KKASSERT(madt_paddr != 0);
120 madt = sdt_sdth_map(madt_paddr);
121 KKASSERT(madt != NULL);
124 * MADT in ACPI specification 1.0 - 5.0
126 if (madt->Header.Revision < 1 || madt->Header.Revision > 3) {
127 kprintf("madt_check: unknown MADT revision %d\n",
128 madt->Header.Revision);
131 if (madt->Header.Length < sizeof(*madt)) {
132 kprintf("madt_check: invalid MADT length %u\n",
133 madt->Header.Length);
138 sdt_sdth_unmap(&madt->Header);
143 madt_iterate_entries(ACPI_TABLE_MADT *madt, madt_iter_t func, void *arg)
145 int size, cur, error;
147 size = madt->Header.Length - sizeof(*madt);
151 while (size - cur > sizeof(ACPI_SUBTABLE_HEADER)) {
152 const ACPI_SUBTABLE_HEADER *ent;
154 ent = (const ACPI_SUBTABLE_HEADER *)
155 ((char *)madt + sizeof(*madt) + cur);
156 if (ent->Length < sizeof(*ent)) {
157 kprintf("madt_iterate_entries: invalid MADT "
158 "entry len %d\n", ent->Length);
162 if (ent->Length > (size - cur)) {
163 kprintf("madt_iterate_entries: invalid MADT "
164 "entry len %d, > table length\n", ent->Length);
172 * Only Local APIC, I/O APIC and Interrupt Source Override
173 * are defined in ACPI specification 1.0 - 5.0
176 case ACPI_MADT_TYPE_LOCAL_APIC:
177 if (ent->Length < sizeof(ACPI_MADT_LOCAL_APIC)) {
178 kprintf("madt_iterate_entries: invalid MADT "
179 "lapic entry len %d\n", ent->Length);
184 case ACPI_MADT_TYPE_LOCAL_X2APIC:
185 if (ent->Length < sizeof(ACPI_MADT_LOCAL_X2APIC)) {
186 kprintf("madt_iterate_entries: invalid MADT "
187 "x2apic entry len %d\n", ent->Length);
192 case ACPI_MADT_TYPE_IO_APIC:
193 if (ent->Length < sizeof(ACPI_MADT_IO_APIC)) {
194 kprintf("madt_iterate_entries: invalid MADT "
195 "ioapic entry len %d\n", ent->Length);
200 case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
201 if (ent->Length < sizeof(ACPI_MADT_INTERRUPT_OVERRIDE)) {
202 kprintf("madt_iterate_entries: invalid MADT "
203 "intsrc entry len %d\n",
212 error = func(arg, ent);
216 ent = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, ent, ent->Length);
222 madt_lapic_pass1_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
224 const ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_addr_ent;
225 uint64_t *addr64 = xarg;
227 if (ent->Type != ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE)
229 if (ent->Length < sizeof(*lapic_addr_ent)) {
230 kprintf("madt_lapic_pass1: "
231 "invalid LAPIC address override length\n");
234 lapic_addr_ent = (const ACPI_MADT_LOCAL_APIC_OVERRIDE *)ent;
236 *addr64 = lapic_addr_ent->Address;
241 madt_lapic_pass1(void)
243 ACPI_TABLE_MADT *madt;
244 vm_paddr_t lapic_addr;
245 uint64_t lapic_addr64;
248 KKASSERT(madt_phyaddr != 0);
250 madt = sdt_sdth_map(madt_phyaddr);
251 KKASSERT(madt != NULL);
253 MADT_VPRINTF("LAPIC address 0x%x, flags %#x\n",
254 madt->Address, madt->Flags);
255 lapic_addr = madt->Address;
258 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
261 panic("madt_iterate_entries(pass1) failed");
263 if (lapic_addr64 != 0) {
264 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
266 lapic_addr = lapic_addr64;
269 sdt_sdth_unmap(&madt->Header);
274 struct madt_lapic_pass2_cbarg {
281 madt_lapic_pass2_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
283 const ACPI_MADT_LOCAL_APIC *lapic_ent;
284 struct madt_lapic_pass2_cbarg *arg = xarg;
286 if (ent->Type != ACPI_MADT_TYPE_LOCAL_APIC)
289 lapic_ent = (const ACPI_MADT_LOCAL_APIC *)ent;
290 if (lapic_ent->LapicFlags & ACPI_MADT_ENABLED) {
293 if (lapic_ent->Id == arg->bsp_apic_id) {
300 MADT_VPRINTF("cpu id %d, acpi id %d, apic id %d\n",
301 cpu, lapic_ent->ProcessorId, lapic_ent->Id);
302 lapic_set_cpuid(cpu, lapic_ent->Id);
303 CPUID_TO_ACPIID(cpu) = lapic_ent->ProcessorId;
309 madt_x2apic_pass2_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
311 const ACPI_MADT_LOCAL_X2APIC *x2apic_ent;
312 struct madt_lapic_pass2_cbarg *arg = xarg;
314 if (ent->Type != ACPI_MADT_TYPE_LOCAL_X2APIC)
317 x2apic_ent = (const ACPI_MADT_LOCAL_X2APIC *)ent;
318 if (x2apic_ent->LapicFlags & ACPI_MADT_ENABLED) {
321 if (x2apic_ent->LocalApicId == arg->bsp_apic_id) {
328 MADT_VPRINTF("cpu id %d, acpi uid %u, apic id %d\n",
329 cpu, x2apic_ent->Uid, x2apic_ent->LocalApicId);
330 lapic_set_cpuid(cpu, x2apic_ent->LocalApicId);
331 CPUID_TO_ACPIID(cpu) = x2apic_ent->Uid;
337 madt_lapic_pass2(int bsp_apic_id)
339 ACPI_TABLE_MADT *madt;
340 struct madt_lapic_pass2_cbarg arg;
344 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
346 KKASSERT(madt_phyaddr != 0);
348 madt = sdt_sdth_map(madt_phyaddr);
349 KKASSERT(madt != NULL);
351 bzero(&arg, sizeof(arg));
353 arg.bsp_apic_id = bsp_apic_id;
356 func = madt_x2apic_pass2_callback;
358 func = madt_lapic_pass2_callback;
359 error = madt_iterate_entries(madt, func, &arg);
361 panic("madt_iterate_entries(pass2) failed");
363 KKASSERT(arg.bsp_found);
364 naps = arg.cpu - 1; /* exclude BSP */
366 sdt_sdth_unmap(&madt->Header);
371 struct madt_lapic_probe_cbarg {
374 vm_paddr_t lapic_addr;
378 madt_lapic_probe_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
380 struct madt_lapic_probe_cbarg *arg = xarg;
382 if (ent->Type == ACPI_MADT_TYPE_LOCAL_APIC) {
383 const ACPI_MADT_LOCAL_APIC *lapic_ent;
385 lapic_ent = (const ACPI_MADT_LOCAL_APIC *)ent;
386 if (lapic_ent->LapicFlags & ACPI_MADT_ENABLED) {
388 if (lapic_ent->Id == APICID_MAX) {
389 kprintf("madt_lapic_probe: "
390 "invalid LAPIC apic id %d\n",
395 } else if (ent->Type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
396 const ACPI_MADT_LOCAL_X2APIC *x2apic_ent;
398 x2apic_ent = (const ACPI_MADT_LOCAL_X2APIC *)ent;
399 if (x2apic_ent->LapicFlags & ACPI_MADT_ENABLED) {
400 if (x2apic_ent->LocalApicId < APICID_MAX) {
402 * XXX we only support APIC ID 0~254 at
408 } else if (ent->Type == ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE) {
409 const ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_addr_ent;
411 if (ent->Length < sizeof(*lapic_addr_ent)) {
412 kprintf("madt_lapic_probe: "
413 "invalid LAPIC address override length\n");
416 lapic_addr_ent = (const ACPI_MADT_LOCAL_APIC_OVERRIDE *)ent;
418 if (lapic_addr_ent->Address != 0)
419 arg->lapic_addr = lapic_addr_ent->Address;
425 madt_lapic_probe(struct lapic_enumerator *e)
427 struct madt_lapic_probe_cbarg arg;
428 ACPI_TABLE_MADT *madt;
431 if (madt_phyaddr == 0)
434 madt = sdt_sdth_map(madt_phyaddr);
435 KKASSERT(madt != NULL);
437 bzero(&arg, sizeof(arg));
438 arg.lapic_addr = madt->Address;
440 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
442 if (arg.lapic_count == 0 && arg.x2apic_count == 0) {
443 kprintf("madt_lapic_probe: no CPU is found\n");
445 } else if (arg.lapic_count == 0) {
447 * ACPI 5.1 says that LOCAL_X2APIC entry should
448 * be used only if APIC ID > 255. While ACPI 6.2
449 * removes that constraint, which means that
450 * LOCAL_X2APIC entry could be used for any APIC
454 * In DragonFlyBSD, we don't support APIC ID >=
455 * 255, so LOCAL_X2APIC entries should be ignored,
456 * if LOCAL_X2APIC entries are mixed with
457 * LOCAL_APIC entries. LOCAL_X2APIC entries are
458 * used, iff only LOCAL_X2APIC entries exist.
460 madt_use_x2apic = TRUE;
461 kprintf("MADT: use X2APIC entries\n");
464 if (arg.lapic_addr == 0) {
466 * The LAPIC address does not matter in X2APIC mode.
468 if ((cpu_feature2 & CPUID2_X2APIC) == 0) {
469 kprintf("madt_lapic_probe: zero LAPIC address\n");
475 sdt_sdth_unmap(&madt->Header);
480 madt_lapic_enumerate(struct lapic_enumerator *e)
482 vm_paddr_t lapic_addr = 0;
485 KKASSERT(madt_phyaddr != 0);
487 if (!x2apic_enable) {
488 lapic_addr = madt_lapic_pass1();
489 if (lapic_addr == 0) {
493 if (cpu_feature2 & CPUID2_X2APIC) {
495 * X2APIC mode is not enabled, but the CPU supports
496 * it. Forcefully enable X2APIC mode, which nullifies
497 * the requirement of the LAPIC address.
499 kprintf("MADT: no LAPIC address, force X2APIC mode\n");
500 KKASSERT(!x2apic_enable);
502 lapic_x2apic_enter(TRUE);
505 * We should not reach here, madt_lapic_probe() must
508 panic("madt_lapic_enumerate: no local apic");
513 if (!x2apic_enable) {
514 KASSERT(lapic_addr != 0, ("madt_lapic_enumerate: zero LAPIC address"));
515 lapic_map(lapic_addr);
518 bsp_apic_id = LAPIC_READID;
519 if (bsp_apic_id == APICID_MAX) {
522 * Some old brain dead BIOS will set BSP's LAPIC apic id
523 * to 255, though all LAPIC entries in MADT are valid.
525 kprintf("%s invalid BSP LAPIC apic id %d\n", __func__,
530 if (madt_lapic_pass2(bsp_apic_id))
531 panic("madt_lapic_enumerate: madt_lapic_pass2 failed");
536 static struct lapic_enumerator madt_lapic_enumerator = {
537 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
538 .lapic_probe = madt_lapic_probe,
539 .lapic_enumerate = madt_lapic_enumerate
543 madt_lapic_enum_register(void)
547 prio = LAPIC_ENUM_PRIO_MADT;
548 kgetenv_int("hw.madt_lapic_prio", &prio);
549 madt_lapic_enumerator.lapic_prio = prio;
551 lapic_enumerator_register(&madt_lapic_enumerator);
553 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
555 struct madt_ioapic_probe_cbarg {
561 madt_ioapic_probe_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
563 struct madt_ioapic_probe_cbarg *arg = xarg;
565 if (ent->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
566 const ACPI_MADT_INTERRUPT_OVERRIDE *intsrc_ent;
569 intsrc_ent = (const ACPI_MADT_INTERRUPT_OVERRIDE *)ent;
571 if (intsrc_ent->SourceIrq >= ISA_IRQ_CNT) {
572 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
573 intsrc_ent->SourceIrq);
577 if (intsrc_ent->Bus != MADT_INT_BUS_ISA) {
578 kprintf("ACPI MADT: warning intsrc irq %d "
579 "bus is not ISA (%d)\n",
580 intsrc_ent->SourceIrq, intsrc_ent->Bus);
583 trig = intsrc_ent->IntiFlags & ACPI_MADT_TRIGGER_MASK;
584 if (trig == ACPI_MADT_TRIGGER_RESERVED) {
585 kprintf("ACPI MADT: warning invalid intsrc irq %d "
586 "trig, reserved\n", intsrc_ent->SourceIrq);
587 } else if (trig == ACPI_MADT_TRIGGER_LEVEL) {
588 MADT_VPRINTF("warning invalid intsrc irq %d "
589 "trig, level\n", intsrc_ent->SourceIrq);
592 pola = intsrc_ent->IntiFlags & ACPI_MADT_POLARITY_MASK;
593 if (pola == ACPI_MADT_POLARITY_RESERVED) {
594 kprintf("ACPI MADT: warning invalid intsrc irq %d "
595 "pola, reserved\n", intsrc_ent->SourceIrq);
596 } else if (pola == ACPI_MADT_POLARITY_ACTIVE_LOW) {
597 MADT_VPRINTF("warning invalid intsrc irq %d "
598 "pola, low\n", intsrc_ent->SourceIrq);
600 } else if (ent->Type == ACPI_MADT_TYPE_IO_APIC) {
601 const ACPI_MADT_IO_APIC *ioapic_ent;
603 ioapic_ent = (const ACPI_MADT_IO_APIC *)ent;
604 if (ioapic_ent->Address == 0) {
605 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
608 if (ioapic_ent->Id == APICID_MAX) {
609 kprintf("madt_ioapic_probe: "
610 "invalid IOAPIC apic id %d\n",
616 if (ioapic_ent->GlobalIrqBase == 0)
623 madt_ioapic_probe(struct ioapic_enumerator *e)
625 struct madt_ioapic_probe_cbarg arg;
626 ACPI_TABLE_MADT *madt;
629 if (madt_phyaddr == 0)
632 madt = sdt_sdth_map(madt_phyaddr);
633 KKASSERT(madt != NULL);
635 bzero(&arg, sizeof(arg));
637 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
639 if (arg.ioapic_cnt == 0) {
640 kprintf("madt_ioapic_probe: no IOAPIC\n");
643 if (!arg.gsi_base0) {
644 kprintf("madt_ioapic_probe: no GSI base 0\n");
649 sdt_sdth_unmap(&madt->Header);
654 madt_ioapic_enum_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
656 if (ent->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
657 const ACPI_MADT_INTERRUPT_OVERRIDE *intsrc_ent;
658 enum intr_trigger trig;
659 enum intr_polarity pola;
660 int ent_trig, ent_pola;
662 intsrc_ent = (const ACPI_MADT_INTERRUPT_OVERRIDE *)ent;
664 KKASSERT(intsrc_ent->SourceIrq < ISA_IRQ_CNT);
665 if (intsrc_ent->Bus != MADT_INT_BUS_ISA)
668 ent_trig = intsrc_ent->IntiFlags & ACPI_MADT_TRIGGER_MASK;
669 if (ent_trig == ACPI_MADT_TRIGGER_RESERVED)
671 else if (ent_trig == ACPI_MADT_TRIGGER_LEVEL)
672 trig = INTR_TRIGGER_LEVEL;
674 trig = INTR_TRIGGER_EDGE;
676 ent_pola = intsrc_ent->IntiFlags & ACPI_MADT_POLARITY_MASK;
677 if (ent_pola == ACPI_MADT_POLARITY_RESERVED)
679 else if (ent_pola == ACPI_MADT_POLARITY_ACTIVE_LOW)
680 pola = INTR_POLARITY_LOW;
682 pola = INTR_POLARITY_HIGH;
684 if (intsrc_ent->SourceIrq == acpi_sci_irqno()) {
685 acpi_sci_setmode1(trig, pola);
686 MADT_VPRINTF("SCI irq %d, first test %s/%s\n",
687 intsrc_ent->SourceIrq,
688 intr_str_trigger(trig), intr_str_polarity(pola));
692 * We ignore the polarity and trigger changes, since
693 * most of them are wrong or useless at best.
695 if (intsrc_ent->SourceIrq == intsrc_ent->GlobalIrq) {
696 /* Nothing changed */
699 trig = INTR_TRIGGER_EDGE;
700 pola = INTR_POLARITY_HIGH;
702 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
703 intsrc_ent->SourceIrq, intsrc_ent->GlobalIrq,
704 intr_str_trigger(trig), intr_str_polarity(pola));
705 ioapic_intsrc(intsrc_ent->SourceIrq, intsrc_ent->GlobalIrq,
707 } else if (ent->Type == ACPI_MADT_TYPE_IO_APIC) {
708 const ACPI_MADT_IO_APIC *ioapic_ent;
713 ioapic_ent = (const ACPI_MADT_IO_APIC *)ent;
714 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
715 ioapic_ent->Address, ioapic_ent->Id,
716 ioapic_ent->GlobalIrqBase);
718 addr = ioapic_map(ioapic_ent->Address);
720 ver = ioapic_read(addr, IOAPIC_VER);
721 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
723 ioapic_add(addr, ioapic_ent->GlobalIrqBase, npin);
729 madt_ioapic_enumerate(struct ioapic_enumerator *e)
731 ACPI_TABLE_MADT *madt;
734 KKASSERT(madt_phyaddr != 0);
736 madt = sdt_sdth_map(madt_phyaddr);
737 KKASSERT(madt != NULL);
739 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
741 panic("madt_ioapic_enumerate failed");
743 sdt_sdth_unmap(&madt->Header);
746 static struct ioapic_enumerator madt_ioapic_enumerator = {
747 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
748 .ioapic_probe = madt_ioapic_probe,
749 .ioapic_enumerate = madt_ioapic_enumerate
753 madt_ioapic_enum_register(void)
757 prio = IOAPIC_ENUM_PRIO_MADT;
758 kgetenv_int("hw.madt_ioapic_prio", &prio);
759 madt_ioapic_enumerator.ioapic_prio = prio;
761 ioapic_enumerator_register(&madt_ioapic_enumerator);
763 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
764 madt_ioapic_enum_register, 0);