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 u_int cpu_id_to_acpi_id[NAPICID];
85 vm_paddr_t madt_paddr;
88 for (i = 0; i < NAPICID; ++i)
89 CPUID_TO_ACPIID(i) = (u_int)-1;
91 KKASSERT(madt_phyaddr == 0);
93 madt_paddr = sdt_search(ACPI_SIG_MADT);
94 if (madt_paddr == 0) {
95 kprintf("madt_probe: can't locate MADT\n");
99 /* Preliminary checks */
100 if (madt_check(madt_paddr)) {
101 kprintf("madt_probe: madt_check failed\n");
105 madt_phyaddr = madt_paddr;
107 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
110 madt_check(vm_paddr_t madt_paddr)
112 ACPI_TABLE_MADT *madt;
115 KKASSERT(madt_paddr != 0);
117 madt = sdt_sdth_map(madt_paddr);
118 KKASSERT(madt != NULL);
121 * MADT in ACPI specification 1.0 - 5.0
123 if (madt->Header.Revision < 1 || madt->Header.Revision > 3) {
124 kprintf("madt_check: unknown MADT revision %d\n",
125 madt->Header.Revision);
128 if (madt->Header.Length < sizeof(*madt)) {
129 kprintf("madt_check: invalid MADT length %u\n",
130 madt->Header.Length);
135 sdt_sdth_unmap(&madt->Header);
140 madt_iterate_entries(ACPI_TABLE_MADT *madt, madt_iter_t func, void *arg)
142 int size, cur, error;
144 size = madt->Header.Length - sizeof(*madt);
148 while (size - cur > sizeof(ACPI_SUBTABLE_HEADER)) {
149 const ACPI_SUBTABLE_HEADER *ent;
151 ent = (const ACPI_SUBTABLE_HEADER *)
152 ((char *)madt + sizeof(*madt) + cur);
153 if (ent->Length < sizeof(*ent)) {
154 kprintf("madt_iterate_entries: invalid MADT "
155 "entry len %d\n", ent->Length);
159 if (ent->Length > (size - cur)) {
160 kprintf("madt_iterate_entries: invalid MADT "
161 "entry len %d, > table length\n", ent->Length);
169 * Only Local APIC, I/O APIC and Interrupt Source Override
170 * are defined in ACPI specification 1.0 - 5.0
173 case ACPI_MADT_TYPE_LOCAL_APIC:
174 if (ent->Length < sizeof(ACPI_MADT_LOCAL_APIC)) {
175 kprintf("madt_iterate_entries: invalid MADT "
176 "lapic entry len %d\n", ent->Length);
181 case ACPI_MADT_TYPE_IO_APIC:
182 if (ent->Length < sizeof(ACPI_MADT_IO_APIC)) {
183 kprintf("madt_iterate_entries: invalid MADT "
184 "ioapic entry len %d\n", ent->Length);
189 case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
190 if (ent->Length < sizeof(ACPI_MADT_INTERRUPT_OVERRIDE)) {
191 kprintf("madt_iterate_entries: invalid MADT "
192 "intsrc entry len %d\n",
201 error = func(arg, ent);
205 ent = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, ent, ent->Length);
211 madt_lapic_pass1_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
213 const ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_addr_ent;
214 uint64_t *addr64 = xarg;
216 if (ent->Type != ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE)
218 if (ent->Length < sizeof(*lapic_addr_ent)) {
219 kprintf("madt_lapic_pass1: "
220 "invalid LAPIC address override length\n");
223 lapic_addr_ent = (const ACPI_MADT_LOCAL_APIC_OVERRIDE *)ent;
225 *addr64 = lapic_addr_ent->Address;
230 madt_lapic_pass1(void)
232 ACPI_TABLE_MADT *madt;
233 vm_paddr_t lapic_addr;
234 uint64_t lapic_addr64;
237 KKASSERT(madt_phyaddr != 0);
239 madt = sdt_sdth_map(madt_phyaddr);
240 KKASSERT(madt != NULL);
242 MADT_VPRINTF("LAPIC address 0x%x, flags %#x\n",
243 madt->Address, madt->Flags);
244 lapic_addr = madt->Address;
247 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
250 panic("madt_iterate_entries(pass1) failed");
252 if (lapic_addr64 != 0) {
253 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
255 lapic_addr = lapic_addr64;
258 sdt_sdth_unmap(&madt->Header);
263 struct madt_lapic_pass2_cbarg {
270 madt_lapic_pass2_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
272 const ACPI_MADT_LOCAL_APIC *lapic_ent;
273 struct madt_lapic_pass2_cbarg *arg = xarg;
275 if (ent->Type != ACPI_MADT_TYPE_LOCAL_APIC)
278 lapic_ent = (const ACPI_MADT_LOCAL_APIC *)ent;
279 if (lapic_ent->LapicFlags & ACPI_MADT_ENABLED) {
282 if (lapic_ent->Id == arg->bsp_apic_id) {
289 MADT_VPRINTF("cpu id %d, acpi id %d, apic id %d\n",
290 cpu, lapic_ent->ProcessorId, lapic_ent->Id);
291 lapic_set_cpuid(cpu, lapic_ent->Id);
292 CPUID_TO_ACPIID(cpu) = lapic_ent->ProcessorId;
298 madt_lapic_pass2(int bsp_apic_id)
300 ACPI_TABLE_MADT *madt;
301 struct madt_lapic_pass2_cbarg arg;
304 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
306 KKASSERT(madt_phyaddr != 0);
308 madt = sdt_sdth_map(madt_phyaddr);
309 KKASSERT(madt != NULL);
311 bzero(&arg, sizeof(arg));
313 arg.bsp_apic_id = bsp_apic_id;
315 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
317 panic("madt_iterate_entries(pass2) failed");
319 KKASSERT(arg.bsp_found);
320 naps = arg.cpu - 1; /* exclude BSP */
322 sdt_sdth_unmap(&madt->Header);
327 struct madt_lapic_probe_cbarg {
329 vm_paddr_t lapic_addr;
333 madt_lapic_probe_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
335 struct madt_lapic_probe_cbarg *arg = xarg;
337 if (ent->Type == ACPI_MADT_TYPE_LOCAL_APIC) {
338 const ACPI_MADT_LOCAL_APIC *lapic_ent;
340 lapic_ent = (const ACPI_MADT_LOCAL_APIC *)ent;
341 if (lapic_ent->LapicFlags & ACPI_MADT_ENABLED) {
343 if (lapic_ent->Id == APICID_MAX) {
344 kprintf("madt_lapic_probe: "
345 "invalid LAPIC apic id %d\n",
350 } else if (ent->Type == ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE) {
351 const ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_addr_ent;
353 if (ent->Length < sizeof(*lapic_addr_ent)) {
354 kprintf("madt_lapic_probe: "
355 "invalid LAPIC address override length\n");
358 lapic_addr_ent = (const ACPI_MADT_LOCAL_APIC_OVERRIDE *)ent;
360 if (lapic_addr_ent->Address != 0)
361 arg->lapic_addr = lapic_addr_ent->Address;
367 madt_lapic_probe(struct lapic_enumerator *e)
369 struct madt_lapic_probe_cbarg arg;
370 ACPI_TABLE_MADT *madt;
373 if (madt_phyaddr == 0)
376 madt = sdt_sdth_map(madt_phyaddr);
377 KKASSERT(madt != NULL);
379 bzero(&arg, sizeof(arg));
380 arg.lapic_addr = madt->Address;
382 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
384 if (arg.cpu_count == 0) {
385 kprintf("madt_lapic_probe: no CPU is found\n");
388 if (arg.lapic_addr == 0) {
389 kprintf("madt_lapic_probe: zero LAPIC address\n");
394 sdt_sdth_unmap(&madt->Header);
399 madt_lapic_enumerate(struct lapic_enumerator *e)
401 vm_paddr_t lapic_addr;
404 KKASSERT(madt_phyaddr != 0);
406 lapic_addr = madt_lapic_pass1();
408 panic("madt_lapic_enumerate: no local apic");
410 lapic_map(lapic_addr);
412 bsp_apic_id = APIC_ID(lapic->id);
413 if (bsp_apic_id == APICID_MAX) {
416 * Some old brain dead BIOS will set BSP's LAPIC apic id
417 * to 255, though all LAPIC entries in MADT are valid.
419 kprintf("%s invalid BSP LAPIC apic id %d\n", __func__,
424 if (madt_lapic_pass2(bsp_apic_id))
425 panic("madt_lapic_enumerate: madt_lapic_pass2 failed");
430 static struct lapic_enumerator madt_lapic_enumerator = {
431 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
432 .lapic_probe = madt_lapic_probe,
433 .lapic_enumerate = madt_lapic_enumerate
437 madt_lapic_enum_register(void)
441 prio = LAPIC_ENUM_PRIO_MADT;
442 kgetenv_int("hw.madt_lapic_prio", &prio);
443 madt_lapic_enumerator.lapic_prio = prio;
445 lapic_enumerator_register(&madt_lapic_enumerator);
447 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
449 struct madt_ioapic_probe_cbarg {
455 madt_ioapic_probe_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
457 struct madt_ioapic_probe_cbarg *arg = xarg;
459 if (ent->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
460 const ACPI_MADT_INTERRUPT_OVERRIDE *intsrc_ent;
463 intsrc_ent = (const ACPI_MADT_INTERRUPT_OVERRIDE *)ent;
465 if (intsrc_ent->SourceIrq >= ISA_IRQ_CNT) {
466 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
467 intsrc_ent->SourceIrq);
471 if (intsrc_ent->Bus != MADT_INT_BUS_ISA) {
472 kprintf("ACPI MADT: warning intsrc irq %d "
473 "bus is not ISA (%d)\n",
474 intsrc_ent->SourceIrq, intsrc_ent->Bus);
477 trig = intsrc_ent->IntiFlags & ACPI_MADT_TRIGGER_MASK;
478 if (trig == ACPI_MADT_TRIGGER_RESERVED) {
479 kprintf("ACPI MADT: warning invalid intsrc irq %d "
480 "trig, reserved\n", intsrc_ent->SourceIrq);
481 } else if (trig == ACPI_MADT_TRIGGER_LEVEL) {
482 MADT_VPRINTF("warning invalid intsrc irq %d "
483 "trig, level\n", intsrc_ent->SourceIrq);
486 pola = intsrc_ent->IntiFlags & ACPI_MADT_POLARITY_MASK;
487 if (pola == ACPI_MADT_POLARITY_RESERVED) {
488 kprintf("ACPI MADT: warning invalid intsrc irq %d "
489 "pola, reserved\n", intsrc_ent->SourceIrq);
490 } else if (pola == ACPI_MADT_POLARITY_ACTIVE_LOW) {
491 MADT_VPRINTF("warning invalid intsrc irq %d "
492 "pola, low\n", intsrc_ent->SourceIrq);
494 } else if (ent->Type == ACPI_MADT_TYPE_IO_APIC) {
495 const ACPI_MADT_IO_APIC *ioapic_ent;
497 ioapic_ent = (const ACPI_MADT_IO_APIC *)ent;
498 if (ioapic_ent->Address == 0) {
499 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
502 if (ioapic_ent->Id == APICID_MAX) {
503 kprintf("madt_ioapic_probe: "
504 "invalid IOAPIC apic id %d\n",
510 if (ioapic_ent->GlobalIrqBase == 0)
517 madt_ioapic_probe(struct ioapic_enumerator *e)
519 struct madt_ioapic_probe_cbarg arg;
520 ACPI_TABLE_MADT *madt;
523 if (madt_phyaddr == 0)
526 madt = sdt_sdth_map(madt_phyaddr);
527 KKASSERT(madt != NULL);
529 bzero(&arg, sizeof(arg));
531 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
533 if (arg.ioapic_cnt == 0) {
534 kprintf("madt_ioapic_probe: no IOAPIC\n");
537 if (!arg.gsi_base0) {
538 kprintf("madt_ioapic_probe: no GSI base 0\n");
543 sdt_sdth_unmap(&madt->Header);
548 madt_ioapic_enum_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
550 if (ent->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
551 const ACPI_MADT_INTERRUPT_OVERRIDE *intsrc_ent;
552 enum intr_trigger trig;
553 enum intr_polarity pola;
554 int ent_trig, ent_pola;
556 intsrc_ent = (const ACPI_MADT_INTERRUPT_OVERRIDE *)ent;
558 KKASSERT(intsrc_ent->SourceIrq < ISA_IRQ_CNT);
559 if (intsrc_ent->Bus != MADT_INT_BUS_ISA)
562 ent_trig = intsrc_ent->IntiFlags & ACPI_MADT_TRIGGER_MASK;
563 if (ent_trig == ACPI_MADT_TRIGGER_RESERVED)
565 else if (ent_trig == ACPI_MADT_TRIGGER_LEVEL)
566 trig = INTR_TRIGGER_LEVEL;
568 trig = INTR_TRIGGER_EDGE;
570 ent_pola = intsrc_ent->IntiFlags & ACPI_MADT_POLARITY_MASK;
571 if (ent_pola == ACPI_MADT_POLARITY_RESERVED)
573 else if (ent_pola == ACPI_MADT_POLARITY_ACTIVE_LOW)
574 pola = INTR_POLARITY_LOW;
576 pola = INTR_POLARITY_HIGH;
578 if (intsrc_ent->SourceIrq == acpi_sci_irqno()) {
579 acpi_sci_setmode1(trig, pola);
580 MADT_VPRINTF("SCI irq %d, first test %s/%s\n",
581 intsrc_ent->SourceIrq,
582 intr_str_trigger(trig), intr_str_polarity(pola));
586 * We ignore the polarity and trigger changes, since
587 * most of them are wrong or useless at best.
589 if (intsrc_ent->SourceIrq == intsrc_ent->GlobalIrq) {
590 /* Nothing changed */
593 trig = INTR_TRIGGER_EDGE;
594 pola = INTR_POLARITY_HIGH;
596 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
597 intsrc_ent->SourceIrq, intsrc_ent->GlobalIrq,
598 intr_str_trigger(trig), intr_str_polarity(pola));
599 ioapic_intsrc(intsrc_ent->SourceIrq, intsrc_ent->GlobalIrq,
601 } else if (ent->Type == ACPI_MADT_TYPE_IO_APIC) {
602 const ACPI_MADT_IO_APIC *ioapic_ent;
607 ioapic_ent = (const ACPI_MADT_IO_APIC *)ent;
608 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
609 ioapic_ent->Address, ioapic_ent->Id,
610 ioapic_ent->GlobalIrqBase);
612 addr = ioapic_map(ioapic_ent->Address);
614 ver = ioapic_read(addr, IOAPIC_VER);
615 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
617 ioapic_add(addr, ioapic_ent->GlobalIrqBase, npin);
623 madt_ioapic_enumerate(struct ioapic_enumerator *e)
625 ACPI_TABLE_MADT *madt;
628 KKASSERT(madt_phyaddr != 0);
630 madt = sdt_sdth_map(madt_phyaddr);
631 KKASSERT(madt != NULL);
633 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
635 panic("madt_ioapic_enumerate failed");
637 sdt_sdth_unmap(&madt->Header);
640 static struct ioapic_enumerator madt_ioapic_enumerator = {
641 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
642 .ioapic_probe = madt_ioapic_probe,
643 .ioapic_enumerate = madt_ioapic_enumerate
647 madt_ioapic_enum_register(void)
651 prio = IOAPIC_ENUM_PRIO_MADT;
652 kgetenv_int("hw.madt_ioapic_prio", &prio);
653 madt_ioapic_enumerator.ioapic_prio = prio;
655 ioapic_enumerator_register(&madt_ioapic_enumerator);
657 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
658 madt_ioapic_enum_register, 0);