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/mpapic.h>
47 #include "acpi_sdt_var.h"
49 #define MADT_VPRINTF(fmt, arg...) \
52 kprintf("ACPI MADT: " fmt , ##arg); \
55 /* Multiple APIC Description Table */
57 struct acpi_sdth madt_hdr;
58 uint32_t madt_lapic_addr;
63 /* Common parts of MADT APIC structure */
64 struct acpi_madt_ent {
65 uint8_t me_type; /* MADT_ENT_ */
69 #define MADT_ENT_LAPIC 0
70 #define MADT_ENT_IOAPIC 1
71 #define MADT_ENT_INTSRC 2
72 #define MADT_ENT_LAPIC_ADDR 5
74 /* MADT Processor Local APIC */
75 struct acpi_madt_lapic {
76 struct acpi_madt_ent ml_hdr;
79 uint32_t ml_flags; /* MADT_LAPIC_ */
82 #define MADT_LAPIC_ENABLED 0x1
85 struct acpi_madt_ioapic {
86 struct acpi_madt_ent mio_hdr;
90 uint32_t mio_gsi_base;
93 /* MADT Interrupt Source Override */
94 struct acpi_madt_intsrc {
95 struct acpi_madt_ent mint_hdr;
96 uint8_t mint_bus; /* MADT_INT_BUS_ */
99 uint16_t mint_flags; /* MADT_INT_ */
102 #define MADT_INT_BUS_ISA 0
104 #define MADT_INT_POLA_MASK 0x3
105 #define MADT_INT_POLA_SHIFT 0
106 #define MADT_INT_POLA_CONFORM 0
107 #define MADT_INT_POLA_HIGH 1
108 #define MADT_INT_POLA_RSVD 2
109 #define MADT_INT_POLA_LOW 3
110 #define MADT_INT_TRIG_MASK 0xc
111 #define MADT_INT_TRIG_SHIFT 2
112 #define MADT_INT_TRIG_CONFORM 0
113 #define MADT_INT_TRIG_EDGE 1
114 #define MADT_INT_TRIG_RSVD 2
115 #define MADT_INT_TRIG_LEVEL 3
117 /* MADT Local APIC Address Override */
118 struct acpi_madt_lapic_addr {
119 struct acpi_madt_ent mla_hdr;
120 uint16_t mla_reserved;
121 uint64_t mla_lapic_addr;
124 typedef int (*madt_iter_t)(void *,
125 const struct acpi_madt_ent *);
127 static int madt_check(vm_paddr_t);
128 static int madt_iterate_entries(struct acpi_madt *,
129 madt_iter_t, void *);
131 static vm_offset_t madt_lapic_pass1(void);
132 static int madt_lapic_pass2(int);
134 static void madt_lapic_enumerate(struct lapic_enumerator *);
135 static int madt_lapic_probe(struct lapic_enumerator *);
137 static void madt_ioapic_enumerate(
138 struct ioapic_enumerator *);
139 static int madt_ioapic_probe(struct ioapic_enumerator *);
141 static vm_paddr_t madt_phyaddr;
146 vm_paddr_t madt_paddr;
148 KKASSERT(madt_phyaddr == 0);
150 madt_paddr = sdt_search(ACPI_MADT_SIG);
151 if (madt_paddr == 0) {
152 kprintf("madt_probe: can't locate MADT\n");
156 /* Preliminary checks */
157 if (madt_check(madt_paddr)) {
158 kprintf("madt_probe: madt_check failed\n");
162 madt_phyaddr = madt_paddr;
164 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
167 madt_check(vm_paddr_t madt_paddr)
169 struct acpi_madt *madt;
172 KKASSERT(madt_paddr != 0);
174 madt = sdt_sdth_map(madt_paddr);
175 KKASSERT(madt != NULL);
178 * MADT in ACPI specification 1.0 - 4.0
180 if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
181 kprintf("madt_check: unsupported MADT revision %d\n",
182 madt->madt_hdr.sdth_rev);
187 if (madt->madt_hdr.sdth_len <
188 sizeof(*madt) - sizeof(madt->madt_ents)) {
189 kprintf("madt_check: invalid MADT length %u\n",
190 madt->madt_hdr.sdth_len);
195 sdt_sdth_unmap(&madt->madt_hdr);
200 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
202 int size, cur, error;
204 size = madt->madt_hdr.sdth_len -
205 (sizeof(*madt) - sizeof(madt->madt_ents));
209 while (size - cur > sizeof(struct acpi_madt_ent)) {
210 const struct acpi_madt_ent *ent;
212 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
213 if (ent->me_len < sizeof(*ent)) {
214 kprintf("madt_iterate_entries: invalid MADT "
215 "entry len %d\n", ent->me_len);
219 if (ent->me_len > (size - cur)) {
220 kprintf("madt_iterate_entries: invalid MADT "
221 "entry len %d, > table length\n", ent->me_len);
229 * Only Local APIC, I/O APIC and Interrupt Source Override
230 * are defined in ACPI specification 1.0 - 4.0
232 switch (ent->me_type) {
234 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
235 kprintf("madt_iterate_entries: invalid MADT "
236 "lapic entry len %d\n", ent->me_len);
241 case MADT_ENT_IOAPIC:
242 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
243 kprintf("madt_iterate_entries: invalid MADT "
244 "ioapic entry len %d\n", ent->me_len);
249 case MADT_ENT_INTSRC:
250 if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
251 kprintf("madt_iterate_entries: invalid MADT "
252 "intsrc entry len %d\n",
261 error = func(arg, ent);
269 madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
271 const struct acpi_madt_lapic_addr *lapic_addr_ent;
272 uint64_t *addr64 = xarg;
274 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
276 if (ent->me_len < sizeof(*lapic_addr_ent)) {
277 kprintf("madt_lapic_pass1: "
278 "invalid LAPIC address override length\n");
281 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
283 *addr64 = lapic_addr_ent->mla_lapic_addr;
288 madt_lapic_pass1(void)
290 struct acpi_madt *madt;
291 vm_offset_t lapic_addr;
292 uint64_t lapic_addr64;
295 KKASSERT(madt_phyaddr != 0);
297 madt = sdt_sdth_map(madt_phyaddr);
298 KKASSERT(madt != NULL);
300 MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
301 madt->madt_lapic_addr, madt->madt_flags);
302 lapic_addr = madt->madt_lapic_addr;
305 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
308 panic("madt_iterate_entries(pass1) failed\n");
310 if (lapic_addr64 != 0) {
311 kprintf("ACPI MADT: warning 64bits lapic address 0x%llx\n",
313 /* XXX vm_offset_t is 32bits on i386 */
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 mp_set_cpuids(0, lapic_ent->ml_apic_id);
345 mp_set_cpuids(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 KKASSERT(arg.cpu > 1);
376 mp_naps = arg.cpu - 1; /* exclude BSP */
378 sdt_sdth_unmap(&madt->madt_hdr);
383 struct madt_lapic_probe_cbarg {
385 vm_offset_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 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
400 const struct acpi_madt_lapic_addr *lapic_addr_ent;
402 if (ent->me_len < sizeof(*lapic_addr_ent)) {
403 kprintf("madt_lapic_probe: "
404 "invalid LAPIC address override length\n");
407 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
409 if (lapic_addr_ent->mla_lapic_addr != 0)
410 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
416 madt_lapic_probe(struct lapic_enumerator *e)
418 struct madt_lapic_probe_cbarg arg;
419 struct acpi_madt *madt;
422 if (madt_phyaddr == 0)
425 madt = sdt_sdth_map(madt_phyaddr);
426 KKASSERT(madt != NULL);
428 bzero(&arg, sizeof(arg));
429 arg.lapic_addr = madt->madt_lapic_addr;
431 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
433 if (arg.cpu_count <= 1) {
434 kprintf("madt_lapic_probe: "
435 "less than 2 CPUs is found\n");
438 if (arg.lapic_addr == 0) {
439 kprintf("madt_lapic_probe: zero LAPIC address\n");
444 sdt_sdth_unmap(&madt->madt_hdr);
449 madt_lapic_enumerate(struct lapic_enumerator *e)
451 vm_offset_t lapic_addr;
454 KKASSERT(madt_phyaddr != 0);
456 lapic_addr = madt_lapic_pass1();
458 panic("madt_lapic_enumerate: no local apic\n");
460 lapic_map(lapic_addr);
462 bsp_apic_id = APIC_ID(lapic.id);
463 if (madt_lapic_pass2(bsp_apic_id))
464 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
467 static struct lapic_enumerator madt_lapic_enumerator = {
468 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
469 .lapic_probe = madt_lapic_probe,
470 .lapic_enumerate = madt_lapic_enumerate
474 madt_lapic_enum_register(void)
478 prio = LAPIC_ENUM_PRIO_MADT;
479 kgetenv_int("hw.madt_lapic_prio", &prio);
480 madt_lapic_enumerator.lapic_prio = prio;
482 lapic_enumerator_register(&madt_lapic_enumerator);
484 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
486 struct madt_ioapic_probe_cbarg {
492 madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
494 struct madt_ioapic_probe_cbarg *arg = xarg;
496 if (ent->me_type == MADT_ENT_INTSRC) {
497 const struct acpi_madt_intsrc *intsrc_ent;
500 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
502 /* XXX magic number */
503 if (intsrc_ent->mint_src >= 16) {
504 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
505 intsrc_ent->mint_src);
509 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
510 kprintf("ACPI MADT: warning intsrc irq %d "
511 "bus is not ISA (%d)\n",
512 intsrc_ent->mint_src, intsrc_ent->mint_bus);
515 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
517 if (trig == MADT_INT_TRIG_RSVD) {
518 kprintf("ACPI MADT: warning invalid intsrc irq %d "
519 "trig (%d)\n", intsrc_ent->mint_src, trig);
522 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
524 if (pola == MADT_INT_POLA_RSVD) {
525 kprintf("ACPI MADT: warning invalid intsrc irq %d "
526 "pola (%d)\n", intsrc_ent->mint_src, pola);
528 } else if (ent->me_type == MADT_ENT_IOAPIC) {
529 const struct acpi_madt_ioapic *ioapic_ent;
531 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
532 if (ioapic_ent->mio_addr == 0) {
533 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
538 if (ioapic_ent->mio_gsi_base == 0)
545 madt_ioapic_probe(struct ioapic_enumerator *e)
547 struct madt_ioapic_probe_cbarg arg;
548 struct acpi_madt *madt;
551 if (madt_phyaddr == 0)
554 madt = sdt_sdth_map(madt_phyaddr);
555 KKASSERT(madt != NULL);
557 bzero(&arg, sizeof(arg));
559 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
561 if (arg.ioapic_cnt == 0) {
562 kprintf("madt_ioapic_probe: no IOAPIC\n");
565 if (!arg.gsi_base0) {
566 kprintf("madt_ioapic_probe: no GSI base 0\n");
571 sdt_sdth_unmap(&madt->madt_hdr);
576 madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
578 if (ent->me_type == MADT_ENT_INTSRC) {
579 const struct acpi_madt_intsrc *intsrc_ent;
580 enum intr_trigger trig;
581 enum intr_polarity pola;
584 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
586 KKASSERT(intsrc_ent->mint_src < 16);
587 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
590 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
592 if (ent_trig == MADT_INT_TRIG_RSVD) {
595 } else if (ent_trig == MADT_INT_TRIG_LEVEL) {
596 trig = INTR_TRIGGER_LEVEL;
597 pola = INTR_POLARITY_LOW;
600 trig = INTR_TRIGGER_EDGE;
601 pola = INTR_POLARITY_HIGH;
604 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi &&
605 trig == INTR_TRIGGER_EDGE) {
606 /* Nothing changed */
610 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
611 intsrc_ent->mint_src, intsrc_ent->mint_gsi,
612 intr_str_trigger(trig), intr_str_polarity(pola));
613 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
615 } else if (ent->me_type == MADT_ENT_IOAPIC) {
616 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 if (!ioapic_use_old) {
628 addr = ioapic_map(ioapic_ent->mio_addr);
630 ver = ioapic_read(addr, IOAPIC_VER);
631 npin = ((ver & IOART_VER_MAXREDIR) >>
634 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
641 madt_ioapic_enumerate(struct ioapic_enumerator *e)
643 struct acpi_madt *madt;
646 KKASSERT(madt_phyaddr != 0);
648 madt = sdt_sdth_map(madt_phyaddr);
649 KKASSERT(madt != NULL);
651 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
653 panic("madt_ioapic_enumerate failed\n");
655 sdt_sdth_unmap(&madt->madt_hdr);
658 static struct ioapic_enumerator madt_ioapic_enumerator = {
659 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
660 .ioapic_probe = madt_ioapic_probe,
661 .ioapic_enumerate = madt_ioapic_enumerate
665 madt_ioapic_enum_register(void)
669 prio = IOAPIC_ENUM_PRIO_MADT;
670 kgetenv_int("hw.madt_ioapic_prio", &prio);
671 madt_ioapic_enumerator.ioapic_prio = prio;
673 ioapic_enumerator_register(&madt_ioapic_enumerator);
675 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
676 madt_ioapic_enum_register, 0);