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/pmap.h>
41 #include <machine/smp.h>
42 #include <machine/md_var.h>
43 #include <machine/specialreg.h>
44 #include <machine_base/apic/lapic.h>
45 #include <machine_base/apic/ioapic.h>
48 #include "acpi_sdt_var.h"
50 #define MADT_VPRINTF(fmt, arg...) \
53 kprintf("ACPI MADT: " fmt , ##arg); \
56 /* Multiple APIC Description Table */
58 struct acpi_sdth madt_hdr;
59 uint32_t madt_lapic_addr;
64 /* Common parts of MADT APIC structure */
65 struct acpi_madt_ent {
66 uint8_t me_type; /* MADT_ENT_ */
70 #define MADT_ENT_LAPIC 0
71 #define MADT_ENT_IOAPIC 1
72 #define MADT_ENT_INTSRC 2
73 #define MADT_ENT_LAPIC_ADDR 5
75 /* MADT Processor Local APIC */
76 struct acpi_madt_lapic {
77 struct acpi_madt_ent ml_hdr;
80 uint32_t ml_flags; /* MADT_LAPIC_ */
83 #define MADT_LAPIC_ENABLED 0x1
86 struct acpi_madt_ioapic {
87 struct acpi_madt_ent mio_hdr;
91 uint32_t mio_gsi_base;
94 /* MADT Interrupt Source Override */
95 struct acpi_madt_intsrc {
96 struct acpi_madt_ent mint_hdr;
97 uint8_t mint_bus; /* MADT_INT_BUS_ */
100 uint16_t mint_flags; /* MADT_INT_ */
103 #define MADT_INT_BUS_ISA 0
105 #define MADT_INT_POLA_MASK 0x3
106 #define MADT_INT_POLA_SHIFT 0
107 #define MADT_INT_POLA_CONFORM 0
108 #define MADT_INT_POLA_HIGH 1
109 #define MADT_INT_POLA_RSVD 2
110 #define MADT_INT_POLA_LOW 3
111 #define MADT_INT_TRIG_MASK 0xc
112 #define MADT_INT_TRIG_SHIFT 2
113 #define MADT_INT_TRIG_CONFORM 0
114 #define MADT_INT_TRIG_EDGE 1
115 #define MADT_INT_TRIG_RSVD 2
116 #define MADT_INT_TRIG_LEVEL 3
118 /* MADT Local APIC Address Override */
119 struct acpi_madt_lapic_addr {
120 struct acpi_madt_ent mla_hdr;
121 uint16_t mla_reserved;
122 uint64_t mla_lapic_addr;
125 typedef int (*madt_iter_t)(void *,
126 const struct acpi_madt_ent *);
128 static int madt_check(vm_paddr_t);
129 static int madt_iterate_entries(struct acpi_madt *,
130 madt_iter_t, void *);
132 static vm_offset_t madt_lapic_pass1(void);
133 static int madt_lapic_pass2(int);
135 static void madt_lapic_enumerate(struct lapic_enumerator *);
136 static int madt_lapic_probe(struct lapic_enumerator *);
138 static void madt_ioapic_enumerate(
139 struct ioapic_enumerator *);
140 static int madt_ioapic_probe(struct ioapic_enumerator *);
142 static vm_paddr_t madt_phyaddr;
147 vm_paddr_t madt_paddr;
149 KKASSERT(madt_phyaddr == 0);
151 madt_paddr = sdt_search(ACPI_MADT_SIG);
152 if (madt_paddr == 0) {
153 kprintf("madt_probe: can't locate MADT\n");
157 /* Preliminary checks */
158 if (madt_check(madt_paddr)) {
159 kprintf("madt_probe: madt_check failed\n");
163 madt_phyaddr = madt_paddr;
165 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
168 madt_check(vm_paddr_t madt_paddr)
170 struct acpi_madt *madt;
173 KKASSERT(madt_paddr != 0);
175 madt = sdt_sdth_map(madt_paddr);
176 KKASSERT(madt != NULL);
179 * MADT in ACPI specification 1.0 - 4.0
181 if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
182 kprintf("madt_check: unsupported MADT revision %d\n",
183 madt->madt_hdr.sdth_rev);
188 if (madt->madt_hdr.sdth_len <
189 sizeof(*madt) - sizeof(madt->madt_ents)) {
190 kprintf("madt_check: invalid MADT length %u\n",
191 madt->madt_hdr.sdth_len);
196 sdt_sdth_unmap(&madt->madt_hdr);
201 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
203 int size, cur, error;
205 size = madt->madt_hdr.sdth_len -
206 (sizeof(*madt) - sizeof(madt->madt_ents));
210 while (size - cur > sizeof(struct acpi_madt_ent)) {
211 const struct acpi_madt_ent *ent;
213 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
214 if (ent->me_len < sizeof(*ent)) {
215 kprintf("madt_iterate_entries: invalid MADT "
216 "entry len %d\n", ent->me_len);
220 if (ent->me_len > (size - cur)) {
221 kprintf("madt_iterate_entries: invalid MADT "
222 "entry len %d, > table length\n", ent->me_len);
230 * Only Local APIC, I/O APIC and Interrupt Source Override
231 * are defined in ACPI specification 1.0 - 4.0
233 switch (ent->me_type) {
235 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
236 kprintf("madt_iterate_entries: invalid MADT "
237 "lapic entry len %d\n", ent->me_len);
242 case MADT_ENT_IOAPIC:
243 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
244 kprintf("madt_iterate_entries: invalid MADT "
245 "ioapic entry len %d\n", ent->me_len);
250 case MADT_ENT_INTSRC:
251 if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
252 kprintf("madt_iterate_entries: invalid MADT "
253 "intsrc entry len %d\n",
262 error = func(arg, ent);
270 madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
272 const struct acpi_madt_lapic_addr *lapic_addr_ent;
273 uint64_t *addr64 = xarg;
275 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
277 if (ent->me_len < sizeof(*lapic_addr_ent)) {
278 kprintf("madt_lapic_pass1: "
279 "invalid LAPIC address override length\n");
282 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
284 *addr64 = lapic_addr_ent->mla_lapic_addr;
289 madt_lapic_pass1(void)
291 struct acpi_madt *madt;
292 vm_offset_t lapic_addr;
293 uint64_t lapic_addr64;
296 KKASSERT(madt_phyaddr != 0);
298 madt = sdt_sdth_map(madt_phyaddr);
299 KKASSERT(madt != NULL);
301 MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
302 madt->madt_lapic_addr, madt->madt_flags);
303 lapic_addr = madt->madt_lapic_addr;
306 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
309 panic("madt_iterate_entries(pass1) failed\n");
311 if (lapic_addr64 != 0) {
312 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
314 lapic_addr = lapic_addr64;
317 sdt_sdth_unmap(&madt->madt_hdr);
322 struct madt_lapic_pass2_cbarg {
329 madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
331 const struct acpi_madt_lapic *lapic_ent;
332 struct madt_lapic_pass2_cbarg *arg = xarg;
334 if (ent->me_type != MADT_ENT_LAPIC)
337 lapic_ent = (const struct acpi_madt_lapic *)ent;
338 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
339 MADT_VPRINTF("cpu id %d, apic id %d\n",
340 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
341 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
342 lapic_set_cpuid(0, lapic_ent->ml_apic_id);
345 lapic_set_cpuid(arg->cpu, lapic_ent->ml_apic_id);
353 madt_lapic_pass2(int bsp_apic_id)
355 struct acpi_madt *madt;
356 struct madt_lapic_pass2_cbarg arg;
359 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
361 KKASSERT(madt_phyaddr != 0);
363 madt = sdt_sdth_map(madt_phyaddr);
364 KKASSERT(madt != NULL);
366 bzero(&arg, sizeof(arg));
368 arg.bsp_apic_id = bsp_apic_id;
370 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
372 panic("madt_iterate_entries(pass2) failed\n");
374 KKASSERT(arg.bsp_found);
375 mp_naps = arg.cpu - 1; /* exclude BSP */
377 sdt_sdth_unmap(&madt->madt_hdr);
382 struct madt_lapic_probe_cbarg {
384 vm_offset_t lapic_addr;
388 madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
390 struct madt_lapic_probe_cbarg *arg = xarg;
392 if (ent->me_type == MADT_ENT_LAPIC) {
393 const struct acpi_madt_lapic *lapic_ent;
395 lapic_ent = (const struct acpi_madt_lapic *)ent;
396 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
398 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
399 const struct acpi_madt_lapic_addr *lapic_addr_ent;
401 if (ent->me_len < sizeof(*lapic_addr_ent)) {
402 kprintf("madt_lapic_probe: "
403 "invalid LAPIC address override length\n");
406 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
408 if (lapic_addr_ent->mla_lapic_addr != 0)
409 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
415 madt_lapic_probe(struct lapic_enumerator *e)
417 struct madt_lapic_probe_cbarg arg;
418 struct acpi_madt *madt;
421 if (madt_phyaddr == 0)
424 madt = sdt_sdth_map(madt_phyaddr);
425 KKASSERT(madt != NULL);
427 bzero(&arg, sizeof(arg));
428 arg.lapic_addr = madt->madt_lapic_addr;
430 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
432 if (arg.cpu_count == 0) {
433 kprintf("madt_lapic_probe: no CPU is found\n");
436 if (arg.lapic_addr == 0) {
437 kprintf("madt_lapic_probe: zero LAPIC address\n");
442 sdt_sdth_unmap(&madt->madt_hdr);
447 madt_lapic_enumerate(struct lapic_enumerator *e)
449 vm_offset_t lapic_addr;
452 KKASSERT(madt_phyaddr != 0);
454 lapic_addr = madt_lapic_pass1();
456 panic("madt_lapic_enumerate: no local apic\n");
458 lapic_map(lapic_addr);
460 bsp_apic_id = APIC_ID(lapic->id);
461 if (madt_lapic_pass2(bsp_apic_id))
462 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
465 static struct lapic_enumerator madt_lapic_enumerator = {
466 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
467 .lapic_probe = madt_lapic_probe,
468 .lapic_enumerate = madt_lapic_enumerate
472 madt_lapic_enum_register(void)
476 prio = LAPIC_ENUM_PRIO_MADT;
477 kgetenv_int("hw.madt_lapic_prio", &prio);
478 madt_lapic_enumerator.lapic_prio = prio;
480 lapic_enumerator_register(&madt_lapic_enumerator);
482 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
484 struct madt_ioapic_probe_cbarg {
490 madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
492 struct madt_ioapic_probe_cbarg *arg = xarg;
494 if (ent->me_type == MADT_ENT_INTSRC) {
495 const struct acpi_madt_intsrc *intsrc_ent;
498 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
500 /* XXX magic number */
501 if (intsrc_ent->mint_src >= 16) {
502 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
503 intsrc_ent->mint_src);
507 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
508 kprintf("ACPI MADT: warning intsrc irq %d "
509 "bus is not ISA (%d)\n",
510 intsrc_ent->mint_src, intsrc_ent->mint_bus);
513 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
515 if (trig == MADT_INT_TRIG_RSVD) {
516 kprintf("ACPI MADT: warning invalid intsrc irq %d "
517 "trig (%d)\n", intsrc_ent->mint_src, trig);
520 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
522 if (pola == MADT_INT_POLA_RSVD) {
523 kprintf("ACPI MADT: warning invalid intsrc irq %d "
524 "pola (%d)\n", intsrc_ent->mint_src, pola);
526 } else if (ent->me_type == MADT_ENT_IOAPIC) {
527 const struct acpi_madt_ioapic *ioapic_ent;
529 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
530 if (ioapic_ent->mio_addr == 0) {
531 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
536 if (ioapic_ent->mio_gsi_base == 0)
543 madt_ioapic_probe(struct ioapic_enumerator *e)
545 struct madt_ioapic_probe_cbarg arg;
546 struct acpi_madt *madt;
549 if (madt_phyaddr == 0)
552 madt = sdt_sdth_map(madt_phyaddr);
553 KKASSERT(madt != NULL);
555 bzero(&arg, sizeof(arg));
557 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
559 if (arg.ioapic_cnt == 0) {
560 kprintf("madt_ioapic_probe: no IOAPIC\n");
563 if (!arg.gsi_base0) {
564 kprintf("madt_ioapic_probe: no GSI base 0\n");
569 sdt_sdth_unmap(&madt->madt_hdr);
574 madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
576 if (ent->me_type == MADT_ENT_INTSRC) {
577 const struct acpi_madt_intsrc *intsrc_ent;
578 enum intr_trigger trig;
579 enum intr_polarity pola;
582 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
584 KKASSERT(intsrc_ent->mint_src < 16);
585 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
588 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
590 if (ent_trig == MADT_INT_TRIG_RSVD) {
593 } else if (ent_trig == MADT_INT_TRIG_LEVEL) {
594 trig = INTR_TRIGGER_LEVEL;
595 pola = INTR_POLARITY_LOW;
598 trig = INTR_TRIGGER_EDGE;
599 pola = INTR_POLARITY_HIGH;
602 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi &&
603 trig == INTR_TRIGGER_EDGE) {
604 /* Nothing changed */
608 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
609 intsrc_ent->mint_src, intsrc_ent->mint_gsi,
610 intr_str_trigger(trig), intr_str_polarity(pola));
611 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
613 } else if (ent->me_type == MADT_ENT_IOAPIC) {
614 const struct acpi_madt_ioapic *ioapic_ent;
619 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
620 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
621 ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
622 ioapic_ent->mio_gsi_base);
624 addr = ioapic_map(ioapic_ent->mio_addr);
626 ver = ioapic_read(addr, IOAPIC_VER);
627 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
629 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
635 madt_ioapic_enumerate(struct ioapic_enumerator *e)
637 struct acpi_madt *madt;
640 KKASSERT(madt_phyaddr != 0);
642 madt = sdt_sdth_map(madt_phyaddr);
643 KKASSERT(madt != NULL);
645 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
647 panic("madt_ioapic_enumerate failed\n");
649 sdt_sdth_unmap(&madt->madt_hdr);
652 static struct ioapic_enumerator madt_ioapic_enumerator = {
653 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
654 .ioapic_probe = madt_ioapic_probe,
655 .ioapic_enumerate = madt_ioapic_enumerate
659 madt_ioapic_enum_register(void)
663 prio = IOAPIC_ENUM_PRIO_MADT;
664 kgetenv_int("hw.madt_ioapic_prio", &prio);
665 madt_ioapic_enumerator.ioapic_prio = prio;
667 ioapic_enumerator_register(&madt_ioapic_enumerator);
669 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
670 madt_ioapic_enum_register, 0);