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>
46 #include "acpi_sdt_var.h"
47 #include "acpi_sci_var.h"
51 #define MADT_VPRINTF(fmt, arg...) \
54 kprintf("ACPI MADT: " fmt , ##arg); \
57 /* Multiple APIC Description Table */
59 struct acpi_sdth madt_hdr;
60 uint32_t madt_lapic_addr;
65 /* Common parts of MADT APIC structure */
66 struct acpi_madt_ent {
67 uint8_t me_type; /* MADT_ENT_ */
71 #define MADT_ENT_LAPIC 0
72 #define MADT_ENT_IOAPIC 1
73 #define MADT_ENT_INTSRC 2
74 #define MADT_ENT_LAPIC_ADDR 5
76 /* MADT Processor Local APIC */
77 struct acpi_madt_lapic {
78 struct acpi_madt_ent ml_hdr;
81 uint32_t ml_flags; /* MADT_LAPIC_ */
84 #define MADT_LAPIC_ENABLED 0x1
87 struct acpi_madt_ioapic {
88 struct acpi_madt_ent mio_hdr;
92 uint32_t mio_gsi_base;
95 /* MADT Interrupt Source Override */
96 struct acpi_madt_intsrc {
97 struct acpi_madt_ent mint_hdr;
98 uint8_t mint_bus; /* MADT_INT_BUS_ */
101 uint16_t mint_flags; /* MADT_INT_ */
104 #define MADT_INT_BUS_ISA 0
106 #define MADT_INT_POLA_MASK 0x3
107 #define MADT_INT_POLA_SHIFT 0
108 #define MADT_INT_POLA_CONFORM 0
109 #define MADT_INT_POLA_HIGH 1
110 #define MADT_INT_POLA_RSVD 2
111 #define MADT_INT_POLA_LOW 3
112 #define MADT_INT_TRIG_MASK 0xc
113 #define MADT_INT_TRIG_SHIFT 2
114 #define MADT_INT_TRIG_CONFORM 0
115 #define MADT_INT_TRIG_EDGE 1
116 #define MADT_INT_TRIG_RSVD 2
117 #define MADT_INT_TRIG_LEVEL 3
119 /* MADT Local APIC Address Override */
120 struct acpi_madt_lapic_addr {
121 struct acpi_madt_ent mla_hdr;
122 uint16_t mla_reserved;
123 uint64_t mla_lapic_addr;
126 typedef int (*madt_iter_t)(void *,
127 const struct acpi_madt_ent *);
129 static int madt_check(vm_paddr_t);
130 static int madt_iterate_entries(struct acpi_madt *,
131 madt_iter_t, void *);
133 static vm_paddr_t madt_lapic_pass1(void);
134 static int madt_lapic_pass2(int);
136 static int madt_lapic_enumerate(struct lapic_enumerator *);
137 static int madt_lapic_probe(struct lapic_enumerator *);
139 static void madt_ioapic_enumerate(
140 struct ioapic_enumerator *);
141 static int madt_ioapic_probe(struct ioapic_enumerator *);
143 static vm_paddr_t madt_phyaddr;
148 vm_paddr_t madt_paddr;
150 KKASSERT(madt_phyaddr == 0);
152 madt_paddr = sdt_search(ACPI_MADT_SIG);
153 if (madt_paddr == 0) {
154 kprintf("madt_probe: can't locate MADT\n");
158 /* Preliminary checks */
159 if (madt_check(madt_paddr)) {
160 kprintf("madt_probe: madt_check failed\n");
164 madt_phyaddr = madt_paddr;
166 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
169 madt_check(vm_paddr_t madt_paddr)
171 struct acpi_madt *madt;
174 KKASSERT(madt_paddr != 0);
176 madt = sdt_sdth_map(madt_paddr);
177 KKASSERT(madt != NULL);
180 * MADT in ACPI specification 1.0 - 4.0
182 if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
183 kprintf("madt_check: unsupported MADT revision %d\n",
184 madt->madt_hdr.sdth_rev);
189 if (madt->madt_hdr.sdth_len <
190 sizeof(*madt) - sizeof(madt->madt_ents)) {
191 kprintf("madt_check: invalid MADT length %u\n",
192 madt->madt_hdr.sdth_len);
197 sdt_sdth_unmap(&madt->madt_hdr);
202 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
204 int size, cur, error;
206 size = madt->madt_hdr.sdth_len -
207 (sizeof(*madt) - sizeof(madt->madt_ents));
211 while (size - cur > sizeof(struct acpi_madt_ent)) {
212 const struct acpi_madt_ent *ent;
214 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
215 if (ent->me_len < sizeof(*ent)) {
216 kprintf("madt_iterate_entries: invalid MADT "
217 "entry len %d\n", ent->me_len);
221 if (ent->me_len > (size - cur)) {
222 kprintf("madt_iterate_entries: invalid MADT "
223 "entry len %d, > table length\n", ent->me_len);
231 * Only Local APIC, I/O APIC and Interrupt Source Override
232 * are defined in ACPI specification 1.0 - 4.0
234 switch (ent->me_type) {
236 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
237 kprintf("madt_iterate_entries: invalid MADT "
238 "lapic entry len %d\n", ent->me_len);
243 case MADT_ENT_IOAPIC:
244 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
245 kprintf("madt_iterate_entries: invalid MADT "
246 "ioapic entry len %d\n", ent->me_len);
251 case MADT_ENT_INTSRC:
252 if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
253 kprintf("madt_iterate_entries: invalid MADT "
254 "intsrc entry len %d\n",
263 error = func(arg, ent);
271 madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
273 const struct acpi_madt_lapic_addr *lapic_addr_ent;
274 uint64_t *addr64 = xarg;
276 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
278 if (ent->me_len < sizeof(*lapic_addr_ent)) {
279 kprintf("madt_lapic_pass1: "
280 "invalid LAPIC address override length\n");
283 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
285 *addr64 = lapic_addr_ent->mla_lapic_addr;
290 madt_lapic_pass1(void)
292 struct acpi_madt *madt;
293 vm_paddr_t lapic_addr;
294 uint64_t lapic_addr64;
297 KKASSERT(madt_phyaddr != 0);
299 madt = sdt_sdth_map(madt_phyaddr);
300 KKASSERT(madt != NULL);
302 MADT_VPRINTF("LAPIC address 0x%x, flags %#x\n",
303 madt->madt_lapic_addr, madt->madt_flags);
304 lapic_addr = madt->madt_lapic_addr;
307 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
310 panic("madt_iterate_entries(pass1) failed");
312 if (lapic_addr64 != 0) {
313 kprintf("ACPI MADT: warning 64bits lapic address 0x%llx\n",
315 lapic_addr = lapic_addr64;
318 sdt_sdth_unmap(&madt->madt_hdr);
323 struct madt_lapic_pass2_cbarg {
330 madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
332 const struct acpi_madt_lapic *lapic_ent;
333 struct madt_lapic_pass2_cbarg *arg = xarg;
335 if (ent->me_type != MADT_ENT_LAPIC)
338 lapic_ent = (const struct acpi_madt_lapic *)ent;
339 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
340 MADT_VPRINTF("cpu id %d, apic id %d\n",
341 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
342 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
343 lapic_set_cpuid(0, lapic_ent->ml_apic_id);
346 lapic_set_cpuid(arg->cpu, lapic_ent->ml_apic_id);
354 madt_lapic_pass2(int bsp_apic_id)
356 struct acpi_madt *madt;
357 struct madt_lapic_pass2_cbarg arg;
360 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
362 KKASSERT(madt_phyaddr != 0);
364 madt = sdt_sdth_map(madt_phyaddr);
365 KKASSERT(madt != NULL);
367 bzero(&arg, sizeof(arg));
369 arg.bsp_apic_id = bsp_apic_id;
371 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
373 panic("madt_iterate_entries(pass2) failed");
375 KKASSERT(arg.bsp_found);
376 naps = arg.cpu - 1; /* exclude BSP */
378 sdt_sdth_unmap(&madt->madt_hdr);
383 struct madt_lapic_probe_cbarg {
385 vm_paddr_t lapic_addr;
389 madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
391 struct madt_lapic_probe_cbarg *arg = xarg;
393 if (ent->me_type == MADT_ENT_LAPIC) {
394 const struct acpi_madt_lapic *lapic_ent;
396 lapic_ent = (const struct acpi_madt_lapic *)ent;
397 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
399 if (lapic_ent->ml_apic_id == APICID_MAX) {
400 kprintf("madt_lapic_probe: "
401 "invalid LAPIC apic id %d\n",
402 lapic_ent->ml_apic_id);
406 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
407 const struct acpi_madt_lapic_addr *lapic_addr_ent;
409 if (ent->me_len < sizeof(*lapic_addr_ent)) {
410 kprintf("madt_lapic_probe: "
411 "invalid LAPIC address override length\n");
414 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
416 if (lapic_addr_ent->mla_lapic_addr != 0)
417 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
423 madt_lapic_probe(struct lapic_enumerator *e)
425 struct madt_lapic_probe_cbarg arg;
426 struct acpi_madt *madt;
429 if (madt_phyaddr == 0)
432 madt = sdt_sdth_map(madt_phyaddr);
433 KKASSERT(madt != NULL);
435 bzero(&arg, sizeof(arg));
436 arg.lapic_addr = madt->madt_lapic_addr;
438 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
440 if (arg.cpu_count == 0) {
441 kprintf("madt_lapic_probe: no CPU is found\n");
444 if (arg.lapic_addr == 0) {
445 kprintf("madt_lapic_probe: zero LAPIC address\n");
450 sdt_sdth_unmap(&madt->madt_hdr);
455 madt_lapic_enumerate(struct lapic_enumerator *e)
457 vm_paddr_t lapic_addr;
460 KKASSERT(madt_phyaddr != 0);
462 lapic_addr = madt_lapic_pass1();
464 panic("madt_lapic_enumerate: no local apic");
466 lapic_map(lapic_addr);
468 bsp_apic_id = APIC_ID(lapic->id);
469 if (bsp_apic_id == APICID_MAX) {
472 * Some old brain dead BIOS will set BSP's LAPIC apic id
473 * to 255, though all LAPIC entries in MADT are valid.
475 kprintf("%s invalid BSP LAPIC apic id %d\n", __func__,
480 if (madt_lapic_pass2(bsp_apic_id))
481 panic("madt_lapic_enumerate: madt_lapic_pass2 failed");
486 static struct lapic_enumerator madt_lapic_enumerator = {
487 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
488 .lapic_probe = madt_lapic_probe,
489 .lapic_enumerate = madt_lapic_enumerate
493 madt_lapic_enum_register(void)
497 prio = LAPIC_ENUM_PRIO_MADT;
498 kgetenv_int("hw.madt_lapic_prio", &prio);
499 madt_lapic_enumerator.lapic_prio = prio;
501 lapic_enumerator_register(&madt_lapic_enumerator);
503 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
505 struct madt_ioapic_probe_cbarg {
511 madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
513 struct madt_ioapic_probe_cbarg *arg = xarg;
515 if (ent->me_type == MADT_ENT_INTSRC) {
516 const struct acpi_madt_intsrc *intsrc_ent;
519 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
521 if (intsrc_ent->mint_src >= ISA_IRQ_CNT) {
522 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
523 intsrc_ent->mint_src);
527 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
528 kprintf("ACPI MADT: warning intsrc irq %d "
529 "bus is not ISA (%d)\n",
530 intsrc_ent->mint_src, intsrc_ent->mint_bus);
533 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
535 if (trig == MADT_INT_TRIG_RSVD) {
536 kprintf("ACPI MADT: warning invalid intsrc irq %d "
537 "trig, reserved\n", intsrc_ent->mint_src);
538 } else if (trig == MADT_INT_TRIG_LEVEL) {
539 MADT_VPRINTF("warning invalid intsrc irq %d "
540 "trig, level\n", intsrc_ent->mint_src);
543 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
545 if (pola == MADT_INT_POLA_RSVD) {
546 kprintf("ACPI MADT: warning invalid intsrc irq %d "
547 "pola, reserved\n", intsrc_ent->mint_src);
548 } else if (pola == MADT_INT_POLA_LOW) {
549 MADT_VPRINTF("warning invalid intsrc irq %d "
550 "pola, low\n", intsrc_ent->mint_src);
552 } else if (ent->me_type == MADT_ENT_IOAPIC) {
553 const struct acpi_madt_ioapic *ioapic_ent;
555 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
556 if (ioapic_ent->mio_addr == 0) {
557 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
560 if (ioapic_ent->mio_apic_id == APICID_MAX) {
561 kprintf("madt_ioapic_probe: "
562 "invalid IOAPIC apic id %d\n",
563 ioapic_ent->mio_apic_id);
568 if (ioapic_ent->mio_gsi_base == 0)
575 madt_ioapic_probe(struct ioapic_enumerator *e)
577 struct madt_ioapic_probe_cbarg arg;
578 struct acpi_madt *madt;
581 if (madt_phyaddr == 0)
584 madt = sdt_sdth_map(madt_phyaddr);
585 KKASSERT(madt != NULL);
587 bzero(&arg, sizeof(arg));
589 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
591 if (arg.ioapic_cnt == 0) {
592 kprintf("madt_ioapic_probe: no IOAPIC\n");
595 if (!arg.gsi_base0) {
596 kprintf("madt_ioapic_probe: no GSI base 0\n");
601 sdt_sdth_unmap(&madt->madt_hdr);
606 madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
608 if (ent->me_type == MADT_ENT_INTSRC) {
609 const struct acpi_madt_intsrc *intsrc_ent;
610 enum intr_trigger trig;
611 enum intr_polarity pola;
612 int ent_trig, ent_pola;
614 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
616 KKASSERT(intsrc_ent->mint_src < ISA_IRQ_CNT);
617 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
620 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
622 if (ent_trig == MADT_INT_TRIG_RSVD)
624 else if (ent_trig == MADT_INT_TRIG_LEVEL)
625 trig = INTR_TRIGGER_LEVEL;
627 trig = INTR_TRIGGER_EDGE;
629 ent_pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
631 if (ent_pola == MADT_INT_POLA_RSVD)
633 else if (ent_pola == MADT_INT_POLA_LOW)
634 pola = INTR_POLARITY_LOW;
636 pola = INTR_POLARITY_HIGH;
638 if (intsrc_ent->mint_src == acpi_sci_irqno()) {
639 acpi_sci_setmode1(trig, pola);
640 MADT_VPRINTF("SCI irq %d, first test %s/%s\n",
641 intsrc_ent->mint_src,
642 intr_str_trigger(trig), intr_str_polarity(pola));
646 * We ignore the polarity and trigger changes, since
647 * most of them are wrong or useless at best.
649 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi) {
650 /* Nothing changed */
653 trig = INTR_TRIGGER_EDGE;
654 pola = INTR_POLARITY_HIGH;
656 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
657 intsrc_ent->mint_src, intsrc_ent->mint_gsi,
658 intr_str_trigger(trig), intr_str_polarity(pola));
659 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
661 } else if (ent->me_type == MADT_ENT_IOAPIC) {
662 const struct acpi_madt_ioapic *ioapic_ent;
667 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
668 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
669 ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
670 ioapic_ent->mio_gsi_base);
672 addr = ioapic_map(ioapic_ent->mio_addr);
674 ver = ioapic_read(addr, IOAPIC_VER);
675 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
677 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
683 madt_ioapic_enumerate(struct ioapic_enumerator *e)
685 struct acpi_madt *madt;
688 KKASSERT(madt_phyaddr != 0);
690 madt = sdt_sdth_map(madt_phyaddr);
691 KKASSERT(madt != NULL);
693 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
695 panic("madt_ioapic_enumerate failed");
697 sdt_sdth_unmap(&madt->madt_hdr);
700 static struct ioapic_enumerator madt_ioapic_enumerator = {
701 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
702 .ioapic_probe = madt_ioapic_probe,
703 .ioapic_enumerate = madt_ioapic_enumerate
707 madt_ioapic_enum_register(void)
711 prio = IOAPIC_ENUM_PRIO_MADT;
712 kgetenv_int("hw.madt_ioapic_prio", &prio);
713 madt_ioapic_enumerator.ioapic_prio = prio;
715 ioapic_enumerator_register(&madt_ioapic_enumerator);
717 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
718 madt_ioapic_enum_register, 0);