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/apic/lapic.h>
41 #include <machine_base/apic/ioapic.h>
44 #include "acpi_sdt_var.h"
48 #define MADT_VPRINTF(fmt, arg...) \
51 kprintf("ACPI MADT: " fmt , ##arg); \
54 /* Multiple APIC Description Table */
56 struct acpi_sdth madt_hdr;
57 uint32_t madt_lapic_addr;
62 /* Common parts of MADT APIC structure */
63 struct acpi_madt_ent {
64 uint8_t me_type; /* MADT_ENT_ */
68 #define MADT_ENT_LAPIC 0
69 #define MADT_ENT_IOAPIC 1
70 #define MADT_ENT_INTSRC 2
71 #define MADT_ENT_LAPIC_ADDR 5
73 /* MADT Processor Local APIC */
74 struct acpi_madt_lapic {
75 struct acpi_madt_ent ml_hdr;
78 uint32_t ml_flags; /* MADT_LAPIC_ */
81 #define MADT_LAPIC_ENABLED 0x1
84 struct acpi_madt_ioapic {
85 struct acpi_madt_ent mio_hdr;
89 uint32_t mio_gsi_base;
92 /* MADT Interrupt Source Override */
93 struct acpi_madt_intsrc {
94 struct acpi_madt_ent mint_hdr;
95 uint8_t mint_bus; /* MADT_INT_BUS_ */
98 uint16_t mint_flags; /* MADT_INT_ */
101 #define MADT_INT_BUS_ISA 0
103 #define MADT_INT_POLA_MASK 0x3
104 #define MADT_INT_POLA_SHIFT 0
105 #define MADT_INT_POLA_CONFORM 0
106 #define MADT_INT_POLA_HIGH 1
107 #define MADT_INT_POLA_RSVD 2
108 #define MADT_INT_POLA_LOW 3
109 #define MADT_INT_TRIG_MASK 0xc
110 #define MADT_INT_TRIG_SHIFT 2
111 #define MADT_INT_TRIG_CONFORM 0
112 #define MADT_INT_TRIG_EDGE 1
113 #define MADT_INT_TRIG_RSVD 2
114 #define MADT_INT_TRIG_LEVEL 3
116 /* MADT Local APIC Address Override */
117 struct acpi_madt_lapic_addr {
118 struct acpi_madt_ent mla_hdr;
119 uint16_t mla_reserved;
120 uint64_t mla_lapic_addr;
123 typedef int (*madt_iter_t)(void *,
124 const struct acpi_madt_ent *);
126 static int madt_check(vm_paddr_t);
127 static int madt_iterate_entries(struct acpi_madt *,
128 madt_iter_t, void *);
130 static vm_offset_t madt_lapic_pass1(void);
131 static int madt_lapic_pass2(int);
133 static void madt_lapic_enumerate(struct lapic_enumerator *);
134 static int madt_lapic_probe(struct lapic_enumerator *);
136 static void madt_ioapic_enumerate(
137 struct ioapic_enumerator *);
138 static int madt_ioapic_probe(struct ioapic_enumerator *);
140 static vm_paddr_t madt_phyaddr;
145 vm_paddr_t madt_paddr;
147 KKASSERT(madt_phyaddr == 0);
149 madt_paddr = sdt_search(ACPI_MADT_SIG);
150 if (madt_paddr == 0) {
151 kprintf("madt_probe: can't locate MADT\n");
155 /* Preliminary checks */
156 if (madt_check(madt_paddr)) {
157 kprintf("madt_probe: madt_check failed\n");
161 madt_phyaddr = madt_paddr;
163 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
166 madt_check(vm_paddr_t madt_paddr)
168 struct acpi_madt *madt;
171 KKASSERT(madt_paddr != 0);
173 madt = sdt_sdth_map(madt_paddr);
174 KKASSERT(madt != NULL);
177 * MADT in ACPI specification 1.0 - 4.0
179 if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
180 kprintf("madt_check: unsupported MADT revision %d\n",
181 madt->madt_hdr.sdth_rev);
186 if (madt->madt_hdr.sdth_len <
187 sizeof(*madt) - sizeof(madt->madt_ents)) {
188 kprintf("madt_check: invalid MADT length %u\n",
189 madt->madt_hdr.sdth_len);
194 sdt_sdth_unmap(&madt->madt_hdr);
199 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
201 int size, cur, error;
203 size = madt->madt_hdr.sdth_len -
204 (sizeof(*madt) - sizeof(madt->madt_ents));
208 while (size - cur > sizeof(struct acpi_madt_ent)) {
209 const struct acpi_madt_ent *ent;
211 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
212 if (ent->me_len < sizeof(*ent)) {
213 kprintf("madt_iterate_entries: invalid MADT "
214 "entry len %d\n", ent->me_len);
218 if (ent->me_len > (size - cur)) {
219 kprintf("madt_iterate_entries: invalid MADT "
220 "entry len %d, > table length\n", ent->me_len);
228 * Only Local APIC, I/O APIC and Interrupt Source Override
229 * are defined in ACPI specification 1.0 - 4.0
231 switch (ent->me_type) {
233 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
234 kprintf("madt_iterate_entries: invalid MADT "
235 "lapic entry len %d\n", ent->me_len);
240 case MADT_ENT_IOAPIC:
241 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
242 kprintf("madt_iterate_entries: invalid MADT "
243 "ioapic entry len %d\n", ent->me_len);
248 case MADT_ENT_INTSRC:
249 if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
250 kprintf("madt_iterate_entries: invalid MADT "
251 "intsrc entry len %d\n",
260 error = func(arg, ent);
268 madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
270 const struct acpi_madt_lapic_addr *lapic_addr_ent;
271 uint64_t *addr64 = xarg;
273 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
275 if (ent->me_len < sizeof(*lapic_addr_ent)) {
276 kprintf("madt_lapic_pass1: "
277 "invalid LAPIC address override length\n");
280 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
282 *addr64 = lapic_addr_ent->mla_lapic_addr;
287 madt_lapic_pass1(void)
289 struct acpi_madt *madt;
290 vm_offset_t lapic_addr;
291 uint64_t lapic_addr64;
294 KKASSERT(madt_phyaddr != 0);
296 madt = sdt_sdth_map(madt_phyaddr);
297 KKASSERT(madt != NULL);
299 MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
300 madt->madt_lapic_addr, madt->madt_flags);
301 lapic_addr = madt->madt_lapic_addr;
304 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
307 panic("madt_iterate_entries(pass1) failed\n");
309 if (lapic_addr64 != 0) {
310 kprintf("ACPI MADT: warning 64bits lapic address 0x%llx\n",
312 /* XXX vm_offset_t is 32bits on i386 */
313 lapic_addr = lapic_addr64;
316 sdt_sdth_unmap(&madt->madt_hdr);
321 struct madt_lapic_pass2_cbarg {
328 madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
330 const struct acpi_madt_lapic *lapic_ent;
331 struct madt_lapic_pass2_cbarg *arg = xarg;
333 if (ent->me_type != MADT_ENT_LAPIC)
336 lapic_ent = (const struct acpi_madt_lapic *)ent;
337 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
338 MADT_VPRINTF("cpu id %d, apic id %d\n",
339 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
340 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
341 lapic_set_cpuid(0, lapic_ent->ml_apic_id);
344 lapic_set_cpuid(arg->cpu, lapic_ent->ml_apic_id);
352 madt_lapic_pass2(int bsp_apic_id)
354 struct acpi_madt *madt;
355 struct madt_lapic_pass2_cbarg arg;
358 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
360 KKASSERT(madt_phyaddr != 0);
362 madt = sdt_sdth_map(madt_phyaddr);
363 KKASSERT(madt != NULL);
365 bzero(&arg, sizeof(arg));
367 arg.bsp_apic_id = bsp_apic_id;
369 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
371 panic("madt_iterate_entries(pass2) failed\n");
373 KKASSERT(arg.bsp_found);
374 naps = arg.cpu - 1; /* exclude BSP */
376 sdt_sdth_unmap(&madt->madt_hdr);
381 struct madt_lapic_probe_cbarg {
383 vm_offset_t lapic_addr;
387 madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
389 struct madt_lapic_probe_cbarg *arg = xarg;
391 if (ent->me_type == MADT_ENT_LAPIC) {
392 const struct acpi_madt_lapic *lapic_ent;
394 lapic_ent = (const struct acpi_madt_lapic *)ent;
395 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
397 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
398 const struct acpi_madt_lapic_addr *lapic_addr_ent;
400 if (ent->me_len < sizeof(*lapic_addr_ent)) {
401 kprintf("madt_lapic_probe: "
402 "invalid LAPIC address override length\n");
405 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
407 if (lapic_addr_ent->mla_lapic_addr != 0)
408 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
414 madt_lapic_probe(struct lapic_enumerator *e)
416 struct madt_lapic_probe_cbarg arg;
417 struct acpi_madt *madt;
420 if (madt_phyaddr == 0)
423 madt = sdt_sdth_map(madt_phyaddr);
424 KKASSERT(madt != NULL);
426 bzero(&arg, sizeof(arg));
427 arg.lapic_addr = madt->madt_lapic_addr;
429 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
431 if (arg.cpu_count == 0) {
432 kprintf("madt_lapic_probe: no CPU is found\n");
435 if (arg.lapic_addr == 0) {
436 kprintf("madt_lapic_probe: zero LAPIC address\n");
441 sdt_sdth_unmap(&madt->madt_hdr);
446 madt_lapic_enumerate(struct lapic_enumerator *e)
448 vm_offset_t lapic_addr;
451 KKASSERT(madt_phyaddr != 0);
453 lapic_addr = madt_lapic_pass1();
455 panic("madt_lapic_enumerate: no local apic\n");
457 lapic_map(lapic_addr);
459 bsp_apic_id = APIC_ID(lapic->id);
460 if (madt_lapic_pass2(bsp_apic_id))
461 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
464 static struct lapic_enumerator madt_lapic_enumerator = {
465 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
466 .lapic_probe = madt_lapic_probe,
467 .lapic_enumerate = madt_lapic_enumerate
471 madt_lapic_enum_register(void)
475 prio = LAPIC_ENUM_PRIO_MADT;
476 kgetenv_int("hw.madt_lapic_prio", &prio);
477 madt_lapic_enumerator.lapic_prio = prio;
479 lapic_enumerator_register(&madt_lapic_enumerator);
481 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
483 struct madt_ioapic_probe_cbarg {
489 madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
491 struct madt_ioapic_probe_cbarg *arg = xarg;
493 if (ent->me_type == MADT_ENT_INTSRC) {
494 const struct acpi_madt_intsrc *intsrc_ent;
497 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
499 /* XXX magic number */
500 if (intsrc_ent->mint_src >= 16) {
501 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
502 intsrc_ent->mint_src);
506 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
507 kprintf("ACPI MADT: warning intsrc irq %d "
508 "bus is not ISA (%d)\n",
509 intsrc_ent->mint_src, intsrc_ent->mint_bus);
512 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
514 if (trig == MADT_INT_TRIG_RSVD) {
515 kprintf("ACPI MADT: warning invalid intsrc irq %d "
516 "trig (%d)\n", intsrc_ent->mint_src, trig);
519 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
521 if (pola == MADT_INT_POLA_RSVD) {
522 kprintf("ACPI MADT: warning invalid intsrc irq %d "
523 "pola (%d)\n", intsrc_ent->mint_src, pola);
525 } else if (ent->me_type == MADT_ENT_IOAPIC) {
526 const struct acpi_madt_ioapic *ioapic_ent;
528 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
529 if (ioapic_ent->mio_addr == 0) {
530 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
535 if (ioapic_ent->mio_gsi_base == 0)
542 madt_ioapic_probe(struct ioapic_enumerator *e)
544 struct madt_ioapic_probe_cbarg arg;
545 struct acpi_madt *madt;
548 if (madt_phyaddr == 0)
551 madt = sdt_sdth_map(madt_phyaddr);
552 KKASSERT(madt != NULL);
554 bzero(&arg, sizeof(arg));
556 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
558 if (arg.ioapic_cnt == 0) {
559 kprintf("madt_ioapic_probe: no IOAPIC\n");
562 if (!arg.gsi_base0) {
563 kprintf("madt_ioapic_probe: no GSI base 0\n");
568 sdt_sdth_unmap(&madt->madt_hdr);
573 madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
575 if (ent->me_type == MADT_ENT_INTSRC) {
576 const struct acpi_madt_intsrc *intsrc_ent;
577 enum intr_trigger trig;
578 enum intr_polarity pola;
581 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
583 KKASSERT(intsrc_ent->mint_src < 16);
584 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
587 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
589 if (ent_trig == MADT_INT_TRIG_RSVD) {
592 } else if (ent_trig == MADT_INT_TRIG_LEVEL) {
593 trig = INTR_TRIGGER_LEVEL;
594 pola = INTR_POLARITY_LOW;
597 trig = INTR_TRIGGER_EDGE;
598 pola = INTR_POLARITY_HIGH;
601 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi &&
602 trig == INTR_TRIGGER_EDGE) {
603 /* Nothing changed */
607 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
608 intsrc_ent->mint_src, intsrc_ent->mint_gsi,
609 intr_str_trigger(trig), intr_str_polarity(pola));
610 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
612 } else if (ent->me_type == MADT_ENT_IOAPIC) {
613 const struct acpi_madt_ioapic *ioapic_ent;
618 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
619 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
620 ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
621 ioapic_ent->mio_gsi_base);
623 addr = ioapic_map(ioapic_ent->mio_addr);
625 ver = ioapic_read(addr, IOAPIC_VER);
626 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
628 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
634 madt_ioapic_enumerate(struct ioapic_enumerator *e)
636 struct acpi_madt *madt;
639 KKASSERT(madt_phyaddr != 0);
641 madt = sdt_sdth_map(madt_phyaddr);
642 KKASSERT(madt != NULL);
644 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
646 panic("madt_ioapic_enumerate failed\n");
648 sdt_sdth_unmap(&madt->madt_hdr);
651 static struct ioapic_enumerator madt_ioapic_enumerator = {
652 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
653 .ioapic_probe = madt_ioapic_probe,
654 .ioapic_enumerate = madt_ioapic_enumerate
658 madt_ioapic_enum_register(void)
662 prio = IOAPIC_ENUM_PRIO_MADT;
663 kgetenv_int("hw.madt_ioapic_prio", &prio);
664 madt_ioapic_enumerator.ioapic_prio = prio;
666 ioapic_enumerator_register(&madt_ioapic_enumerator);
668 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
669 madt_ioapic_enum_register, 0);