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>
46 #include <contrib/dev/acpica/source/include/acpi.h>
48 #include "acpi_sdt_var.h"
49 #include "acpi_sci_var.h"
53 #define MADT_VPRINTF(fmt, arg...) \
56 kprintf("ACPI MADT: " fmt , ##arg); \
59 #define MADT_INT_BUS_ISA 0
61 typedef int (*madt_iter_t)(void *,
62 const ACPI_SUBTABLE_HEADER *);
64 static int madt_check(vm_paddr_t);
65 static int madt_iterate_entries(ACPI_TABLE_MADT *,
68 static vm_paddr_t madt_lapic_pass1(void);
69 static int madt_lapic_pass2(int);
71 static int madt_lapic_enumerate(struct lapic_enumerator *);
72 static int madt_lapic_probe(struct lapic_enumerator *);
74 static void madt_ioapic_enumerate(
75 struct ioapic_enumerator *);
76 static int madt_ioapic_probe(struct ioapic_enumerator *);
78 static vm_paddr_t madt_phyaddr;
80 static boolean_t madt_use_x2apic = FALSE;
82 u_int cpu_id_to_acpi_id[NAPICID];
87 vm_paddr_t madt_paddr;
90 for (i = 0; i < NAPICID; ++i)
91 CPUID_TO_ACPIID(i) = (u_int)-1;
93 KKASSERT(madt_phyaddr == 0);
95 madt_paddr = sdt_search(ACPI_SIG_MADT);
96 if (madt_paddr == 0) {
97 kprintf("madt_probe: can't locate MADT\n");
101 /* Preliminary checks */
102 if (madt_check(madt_paddr)) {
103 kprintf("madt_probe: madt_check failed\n");
107 madt_phyaddr = madt_paddr;
109 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
112 madt_check(vm_paddr_t madt_paddr)
114 ACPI_TABLE_MADT *madt;
117 KKASSERT(madt_paddr != 0);
119 madt = sdt_sdth_map(madt_paddr);
120 KKASSERT(madt != NULL);
123 * MADT in ACPI specification 1.0 - 5.0
125 if (madt->Header.Revision < 1 || madt->Header.Revision > 3) {
126 kprintf("madt_check: unknown MADT revision %d\n",
127 madt->Header.Revision);
130 if (madt->Header.Length < sizeof(*madt)) {
131 kprintf("madt_check: invalid MADT length %u\n",
132 madt->Header.Length);
137 sdt_sdth_unmap(&madt->Header);
142 madt_iterate_entries(ACPI_TABLE_MADT *madt, madt_iter_t func, void *arg)
144 int size, cur, error;
146 size = madt->Header.Length - sizeof(*madt);
150 while (size - cur > sizeof(ACPI_SUBTABLE_HEADER)) {
151 const ACPI_SUBTABLE_HEADER *ent;
153 ent = (const ACPI_SUBTABLE_HEADER *)
154 ((char *)madt + sizeof(*madt) + cur);
155 if (ent->Length < sizeof(*ent)) {
156 kprintf("madt_iterate_entries: invalid MADT "
157 "entry len %d\n", ent->Length);
161 if (ent->Length > (size - cur)) {
162 kprintf("madt_iterate_entries: invalid MADT "
163 "entry len %d, > table length\n", ent->Length);
171 * Only Local APIC, I/O APIC and Interrupt Source Override
172 * are defined in ACPI specification 1.0 - 5.0
175 case ACPI_MADT_TYPE_LOCAL_APIC:
176 if (ent->Length < sizeof(ACPI_MADT_LOCAL_APIC)) {
177 kprintf("madt_iterate_entries: invalid MADT "
178 "lapic entry len %d\n", ent->Length);
183 case ACPI_MADT_TYPE_LOCAL_X2APIC:
184 if (ent->Length < sizeof(ACPI_MADT_LOCAL_X2APIC)) {
185 kprintf("madt_iterate_entries: invalid MADT "
186 "x2apic entry len %d\n", ent->Length);
191 case ACPI_MADT_TYPE_IO_APIC:
192 if (ent->Length < sizeof(ACPI_MADT_IO_APIC)) {
193 kprintf("madt_iterate_entries: invalid MADT "
194 "ioapic entry len %d\n", ent->Length);
199 case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
200 if (ent->Length < sizeof(ACPI_MADT_INTERRUPT_OVERRIDE)) {
201 kprintf("madt_iterate_entries: invalid MADT "
202 "intsrc entry len %d\n",
211 error = func(arg, ent);
215 ent = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, ent, ent->Length);
221 madt_lapic_pass1_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
223 const ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_addr_ent;
224 uint64_t *addr64 = xarg;
226 if (ent->Type != ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE)
228 if (ent->Length < sizeof(*lapic_addr_ent)) {
229 kprintf("madt_lapic_pass1: "
230 "invalid LAPIC address override length\n");
233 lapic_addr_ent = (const ACPI_MADT_LOCAL_APIC_OVERRIDE *)ent;
235 *addr64 = lapic_addr_ent->Address;
240 madt_lapic_pass1(void)
242 ACPI_TABLE_MADT *madt;
243 vm_paddr_t lapic_addr;
244 uint64_t lapic_addr64;
247 KKASSERT(madt_phyaddr != 0);
249 madt = sdt_sdth_map(madt_phyaddr);
250 KKASSERT(madt != NULL);
252 MADT_VPRINTF("LAPIC address 0x%x, flags %#x\n",
253 madt->Address, madt->Flags);
254 lapic_addr = madt->Address;
257 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
260 panic("madt_iterate_entries(pass1) failed");
262 if (lapic_addr64 != 0) {
263 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
265 lapic_addr = lapic_addr64;
268 sdt_sdth_unmap(&madt->Header);
273 struct madt_lapic_pass2_cbarg {
280 madt_lapic_pass2_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
282 const ACPI_MADT_LOCAL_APIC *lapic_ent;
283 struct madt_lapic_pass2_cbarg *arg = xarg;
285 if (ent->Type != ACPI_MADT_TYPE_LOCAL_APIC)
288 lapic_ent = (const ACPI_MADT_LOCAL_APIC *)ent;
289 if (lapic_ent->LapicFlags & ACPI_MADT_ENABLED) {
292 if (lapic_ent->Id == arg->bsp_apic_id) {
299 MADT_VPRINTF("cpu id %d, acpi id %d, apic id %d\n",
300 cpu, lapic_ent->ProcessorId, lapic_ent->Id);
301 lapic_set_cpuid(cpu, lapic_ent->Id);
302 CPUID_TO_ACPIID(cpu) = lapic_ent->ProcessorId;
308 madt_x2apic_pass2_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
310 const ACPI_MADT_LOCAL_X2APIC *x2apic_ent;
311 struct madt_lapic_pass2_cbarg *arg = xarg;
313 if (ent->Type != ACPI_MADT_TYPE_LOCAL_X2APIC)
316 x2apic_ent = (const ACPI_MADT_LOCAL_X2APIC *)ent;
317 if (x2apic_ent->LapicFlags & ACPI_MADT_ENABLED) {
320 if (x2apic_ent->LocalApicId == arg->bsp_apic_id) {
327 MADT_VPRINTF("cpu id %d, acpi uid %u, apic id %d\n",
328 cpu, x2apic_ent->Uid, x2apic_ent->LocalApicId);
329 lapic_set_cpuid(cpu, x2apic_ent->LocalApicId);
330 CPUID_TO_ACPIID(cpu) = x2apic_ent->Uid;
336 madt_lapic_pass2(int bsp_apic_id)
338 ACPI_TABLE_MADT *madt;
339 struct madt_lapic_pass2_cbarg arg;
343 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
345 KKASSERT(madt_phyaddr != 0);
347 madt = sdt_sdth_map(madt_phyaddr);
348 KKASSERT(madt != NULL);
350 bzero(&arg, sizeof(arg));
352 arg.bsp_apic_id = bsp_apic_id;
355 func = madt_x2apic_pass2_callback;
357 func = madt_lapic_pass2_callback;
358 error = madt_iterate_entries(madt, func, &arg);
360 panic("madt_iterate_entries(pass2) failed");
362 KKASSERT(arg.bsp_found);
363 naps = arg.cpu - 1; /* exclude BSP */
365 sdt_sdth_unmap(&madt->Header);
370 struct madt_lapic_probe_cbarg {
373 vm_paddr_t lapic_addr;
377 madt_lapic_probe_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
379 struct madt_lapic_probe_cbarg *arg = xarg;
381 if (ent->Type == ACPI_MADT_TYPE_LOCAL_APIC) {
382 const ACPI_MADT_LOCAL_APIC *lapic_ent;
384 lapic_ent = (const ACPI_MADT_LOCAL_APIC *)ent;
385 if (lapic_ent->LapicFlags & ACPI_MADT_ENABLED) {
387 if (lapic_ent->Id == APICID_MAX) {
388 kprintf("madt_lapic_probe: "
389 "invalid LAPIC apic id %d\n",
394 } else if (ent->Type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
395 const ACPI_MADT_LOCAL_X2APIC *x2apic_ent;
397 x2apic_ent = (const ACPI_MADT_LOCAL_X2APIC *)ent;
398 if (x2apic_ent->LapicFlags & ACPI_MADT_ENABLED) {
399 if (x2apic_ent->LocalApicId < APICID_MAX) {
401 * XXX we only support APIC ID 0~254 at
407 } else if (ent->Type == ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE) {
408 const ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_addr_ent;
410 if (ent->Length < sizeof(*lapic_addr_ent)) {
411 kprintf("madt_lapic_probe: "
412 "invalid LAPIC address override length\n");
415 lapic_addr_ent = (const ACPI_MADT_LOCAL_APIC_OVERRIDE *)ent;
417 if (lapic_addr_ent->Address != 0)
418 arg->lapic_addr = lapic_addr_ent->Address;
424 madt_lapic_probe(struct lapic_enumerator *e)
426 struct madt_lapic_probe_cbarg arg;
427 ACPI_TABLE_MADT *madt;
430 if (madt_phyaddr == 0)
433 madt = sdt_sdth_map(madt_phyaddr);
434 KKASSERT(madt != NULL);
436 bzero(&arg, sizeof(arg));
437 arg.lapic_addr = madt->Address;
439 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
441 if (arg.lapic_count == 0 && arg.x2apic_count == 0) {
442 kprintf("madt_lapic_probe: no CPU is found\n");
444 } else if (arg.lapic_count == 0) {
446 * ACPI 5.1 says that LOCAL_X2APIC entry should
447 * be used only if APIC ID > 255. While ACPI 6.2
448 * removes that constraint, which means that
449 * LOCAL_X2APIC entry could be used for any APIC
453 * In DragonFlyBSD, we don't support APIC ID >=
454 * 255, so LOCAL_X2APIC entries should be ignored,
455 * if LOCAL_X2APIC entries are mixed with
456 * LOCAL_APIC entries. LOCAL_X2APIC entries are
457 * used, iff only LOCAL_X2APIC entries exist.
459 madt_use_x2apic = TRUE;
460 kprintf("MADT: use X2APIC entries\n");
463 if (arg.lapic_addr == 0) {
467 kprintf("madt_lapic_probe: zero LAPIC address\n");
472 sdt_sdth_unmap(&madt->Header);
477 madt_lapic_enumerate(struct lapic_enumerator *e)
479 vm_paddr_t lapic_addr;
482 KKASSERT(madt_phyaddr != 0);
484 lapic_addr = madt_lapic_pass1();
486 panic("madt_lapic_enumerate: no local apic");
488 lapic_map(lapic_addr);
490 bsp_apic_id = APIC_ID(lapic->id);
491 if (bsp_apic_id == APICID_MAX) {
494 * Some old brain dead BIOS will set BSP's LAPIC apic id
495 * to 255, though all LAPIC entries in MADT are valid.
497 kprintf("%s invalid BSP LAPIC apic id %d\n", __func__,
502 if (madt_lapic_pass2(bsp_apic_id))
503 panic("madt_lapic_enumerate: madt_lapic_pass2 failed");
508 static struct lapic_enumerator madt_lapic_enumerator = {
509 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
510 .lapic_probe = madt_lapic_probe,
511 .lapic_enumerate = madt_lapic_enumerate
515 madt_lapic_enum_register(void)
519 prio = LAPIC_ENUM_PRIO_MADT;
520 kgetenv_int("hw.madt_lapic_prio", &prio);
521 madt_lapic_enumerator.lapic_prio = prio;
523 lapic_enumerator_register(&madt_lapic_enumerator);
525 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
527 struct madt_ioapic_probe_cbarg {
533 madt_ioapic_probe_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
535 struct madt_ioapic_probe_cbarg *arg = xarg;
537 if (ent->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
538 const ACPI_MADT_INTERRUPT_OVERRIDE *intsrc_ent;
541 intsrc_ent = (const ACPI_MADT_INTERRUPT_OVERRIDE *)ent;
543 if (intsrc_ent->SourceIrq >= ISA_IRQ_CNT) {
544 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
545 intsrc_ent->SourceIrq);
549 if (intsrc_ent->Bus != MADT_INT_BUS_ISA) {
550 kprintf("ACPI MADT: warning intsrc irq %d "
551 "bus is not ISA (%d)\n",
552 intsrc_ent->SourceIrq, intsrc_ent->Bus);
555 trig = intsrc_ent->IntiFlags & ACPI_MADT_TRIGGER_MASK;
556 if (trig == ACPI_MADT_TRIGGER_RESERVED) {
557 kprintf("ACPI MADT: warning invalid intsrc irq %d "
558 "trig, reserved\n", intsrc_ent->SourceIrq);
559 } else if (trig == ACPI_MADT_TRIGGER_LEVEL) {
560 MADT_VPRINTF("warning invalid intsrc irq %d "
561 "trig, level\n", intsrc_ent->SourceIrq);
564 pola = intsrc_ent->IntiFlags & ACPI_MADT_POLARITY_MASK;
565 if (pola == ACPI_MADT_POLARITY_RESERVED) {
566 kprintf("ACPI MADT: warning invalid intsrc irq %d "
567 "pola, reserved\n", intsrc_ent->SourceIrq);
568 } else if (pola == ACPI_MADT_POLARITY_ACTIVE_LOW) {
569 MADT_VPRINTF("warning invalid intsrc irq %d "
570 "pola, low\n", intsrc_ent->SourceIrq);
572 } else if (ent->Type == ACPI_MADT_TYPE_IO_APIC) {
573 const ACPI_MADT_IO_APIC *ioapic_ent;
575 ioapic_ent = (const ACPI_MADT_IO_APIC *)ent;
576 if (ioapic_ent->Address == 0) {
577 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
580 if (ioapic_ent->Id == APICID_MAX) {
581 kprintf("madt_ioapic_probe: "
582 "invalid IOAPIC apic id %d\n",
588 if (ioapic_ent->GlobalIrqBase == 0)
595 madt_ioapic_probe(struct ioapic_enumerator *e)
597 struct madt_ioapic_probe_cbarg arg;
598 ACPI_TABLE_MADT *madt;
601 if (madt_phyaddr == 0)
604 madt = sdt_sdth_map(madt_phyaddr);
605 KKASSERT(madt != NULL);
607 bzero(&arg, sizeof(arg));
609 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
611 if (arg.ioapic_cnt == 0) {
612 kprintf("madt_ioapic_probe: no IOAPIC\n");
615 if (!arg.gsi_base0) {
616 kprintf("madt_ioapic_probe: no GSI base 0\n");
621 sdt_sdth_unmap(&madt->Header);
626 madt_ioapic_enum_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
628 if (ent->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
629 const ACPI_MADT_INTERRUPT_OVERRIDE *intsrc_ent;
630 enum intr_trigger trig;
631 enum intr_polarity pola;
632 int ent_trig, ent_pola;
634 intsrc_ent = (const ACPI_MADT_INTERRUPT_OVERRIDE *)ent;
636 KKASSERT(intsrc_ent->SourceIrq < ISA_IRQ_CNT);
637 if (intsrc_ent->Bus != MADT_INT_BUS_ISA)
640 ent_trig = intsrc_ent->IntiFlags & ACPI_MADT_TRIGGER_MASK;
641 if (ent_trig == ACPI_MADT_TRIGGER_RESERVED)
643 else if (ent_trig == ACPI_MADT_TRIGGER_LEVEL)
644 trig = INTR_TRIGGER_LEVEL;
646 trig = INTR_TRIGGER_EDGE;
648 ent_pola = intsrc_ent->IntiFlags & ACPI_MADT_POLARITY_MASK;
649 if (ent_pola == ACPI_MADT_POLARITY_RESERVED)
651 else if (ent_pola == ACPI_MADT_POLARITY_ACTIVE_LOW)
652 pola = INTR_POLARITY_LOW;
654 pola = INTR_POLARITY_HIGH;
656 if (intsrc_ent->SourceIrq == acpi_sci_irqno()) {
657 acpi_sci_setmode1(trig, pola);
658 MADT_VPRINTF("SCI irq %d, first test %s/%s\n",
659 intsrc_ent->SourceIrq,
660 intr_str_trigger(trig), intr_str_polarity(pola));
664 * We ignore the polarity and trigger changes, since
665 * most of them are wrong or useless at best.
667 if (intsrc_ent->SourceIrq == intsrc_ent->GlobalIrq) {
668 /* Nothing changed */
671 trig = INTR_TRIGGER_EDGE;
672 pola = INTR_POLARITY_HIGH;
674 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
675 intsrc_ent->SourceIrq, intsrc_ent->GlobalIrq,
676 intr_str_trigger(trig), intr_str_polarity(pola));
677 ioapic_intsrc(intsrc_ent->SourceIrq, intsrc_ent->GlobalIrq,
679 } else if (ent->Type == ACPI_MADT_TYPE_IO_APIC) {
680 const ACPI_MADT_IO_APIC *ioapic_ent;
685 ioapic_ent = (const ACPI_MADT_IO_APIC *)ent;
686 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
687 ioapic_ent->Address, ioapic_ent->Id,
688 ioapic_ent->GlobalIrqBase);
690 addr = ioapic_map(ioapic_ent->Address);
692 ver = ioapic_read(addr, IOAPIC_VER);
693 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
695 ioapic_add(addr, ioapic_ent->GlobalIrqBase, npin);
701 madt_ioapic_enumerate(struct ioapic_enumerator *e)
703 ACPI_TABLE_MADT *madt;
706 KKASSERT(madt_phyaddr != 0);
708 madt = sdt_sdth_map(madt_phyaddr);
709 KKASSERT(madt != NULL);
711 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
713 panic("madt_ioapic_enumerate failed");
715 sdt_sdth_unmap(&madt->Header);
718 static struct ioapic_enumerator madt_ioapic_enumerator = {
719 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
720 .ioapic_probe = madt_ioapic_probe,
721 .ioapic_enumerate = madt_ioapic_enumerate
725 madt_ioapic_enum_register(void)
729 prio = IOAPIC_ENUM_PRIO_MADT;
730 kgetenv_int("hw.madt_ioapic_prio", &prio);
731 madt_ioapic_enumerator.ioapic_prio = prio;
733 ioapic_enumerator_register(&madt_ioapic_enumerator);
735 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
736 madt_ioapic_enum_register, 0);