acpi: Be lenient about unknown revision of FADT, MADT, XSDT and RSDT
[dragonfly.git] / sys / platform / pc32 / acpica / acpi_madt.c
CommitLineData
85fd9f87
SZ
1/*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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
32 * SUCH DAMAGE.
33 */
34
35#include <sys/param.h>
ae80be10 36#include <sys/bus.h>
281d9482 37#include <sys/kernel.h>
85fd9f87
SZ
38#include <sys/systm.h>
39
58587c23 40#include <machine_base/isa/isa_intr.h>
3340ac41 41#include <machine_base/apic/lapic.h>
cb7d6921 42#include <machine_base/apic/ioapic.h>
740024d3 43#include <machine_base/apic/apicvar.h>
85fd9f87 44
d6c62a5d
SZ
45#include "acpi_sdt.h"
46#include "acpi_sdt_var.h"
c098b394 47#include "acpi_sci_var.h"
d6c62a5d 48
2abaa030
SZ
49extern int naps;
50
e907d51d
SZ
51#define MADT_VPRINTF(fmt, arg...) \
52do { \
53 if (bootverbose) \
54 kprintf("ACPI MADT: " fmt , ##arg); \
55} while (0)
56
85fd9f87
SZ
57/* Multiple APIC Description Table */
58struct acpi_madt {
59 struct acpi_sdth madt_hdr;
60 uint32_t madt_lapic_addr;
61 uint32_t madt_flags;
62 uint8_t madt_ents[1];
63} __packed;
64
65/* Common parts of MADT APIC structure */
66struct acpi_madt_ent {
67 uint8_t me_type; /* MADT_ENT_ */
68 uint8_t me_len;
69} __packed;
70
71#define MADT_ENT_LAPIC 0
030832e4 72#define MADT_ENT_IOAPIC 1
65b2387f 73#define MADT_ENT_INTSRC 2
e25b100a 74#define MADT_ENT_LAPIC_ADDR 5
85fd9f87
SZ
75
76/* MADT Processor Local APIC */
77struct acpi_madt_lapic {
78 struct acpi_madt_ent ml_hdr;
79 uint8_t ml_cpu_id;
80 uint8_t ml_apic_id;
81 uint32_t ml_flags; /* MADT_LAPIC_ */
82} __packed;
83
84#define MADT_LAPIC_ENABLED 0x1
85
030832e4
SZ
86/* MADT I/O APIC */
87struct acpi_madt_ioapic {
88 struct acpi_madt_ent mio_hdr;
89 uint8_t mio_apic_id;
90 uint8_t mio_reserved;
91 uint32_t mio_addr;
92 uint32_t mio_gsi_base;
93} __packed;
94
2bbbec42 95/* MADT Interrupt Source Override */
922e4bcb
SZ
96struct acpi_madt_intsrc {
97 struct acpi_madt_ent mint_hdr;
65b2387f 98 uint8_t mint_bus; /* MADT_INT_BUS_ */
922e4bcb
SZ
99 uint8_t mint_src;
100 uint32_t mint_gsi;
101 uint16_t mint_flags; /* MADT_INT_ */
2bbbec42
SZ
102} __packed;
103
65b2387f
SZ
104#define MADT_INT_BUS_ISA 0
105
922e4bcb
SZ
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
2bbbec42 118
e25b100a
SZ
119/* MADT Local APIC Address Override */
120struct acpi_madt_lapic_addr {
121 struct acpi_madt_ent mla_hdr;
122 uint16_t mla_reserved;
123 uint64_t mla_lapic_addr;
124} __packed;
125
030832e4
SZ
126typedef int (*madt_iter_t)(void *,
127 const struct acpi_madt_ent *);
85fd9f87 128
223bf214 129static int madt_check(vm_paddr_t);
030832e4
SZ
130static int madt_iterate_entries(struct acpi_madt *,
131 madt_iter_t, void *);
85fd9f87 132
1ebd15a6 133static vm_paddr_t madt_lapic_pass1(void);
27b59776 134static int madt_lapic_pass2(int);
281d9482 135
f8ae0475 136static int madt_lapic_enumerate(struct lapic_enumerator *);
281d9482
SZ
137static int madt_lapic_probe(struct lapic_enumerator *);
138
65b2387f
SZ
139static void madt_ioapic_enumerate(
140 struct ioapic_enumerator *);
141static int madt_ioapic_probe(struct ioapic_enumerator *);
142
86120698
SZ
143static vm_paddr_t madt_phyaddr;
144
145static void
85fd9f87
SZ
146madt_probe(void)
147{
d6c62a5d 148 vm_paddr_t madt_paddr;
85fd9f87 149
86120698
SZ
150 KKASSERT(madt_phyaddr == 0);
151
d6c62a5d 152 madt_paddr = sdt_search(ACPI_MADT_SIG);
85fd9f87
SZ
153 if (madt_paddr == 0) {
154 kprintf("madt_probe: can't locate MADT\n");
86120698 155 return;
85fd9f87 156 }
223bf214
SZ
157
158 /* Preliminary checks */
86120698
SZ
159 if (madt_check(madt_paddr)) {
160 kprintf("madt_probe: madt_check failed\n");
161 return;
162 }
163
164 madt_phyaddr = madt_paddr;
85fd9f87 165}
d6c62a5d 166SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
85fd9f87 167
e25b100a 168static int
27b59776
SZ
169madt_check(vm_paddr_t madt_paddr)
170{
171 struct acpi_madt *madt;
172 int error = 0;
173
174 KKASSERT(madt_paddr != 0);
175
d6c62a5d 176 madt = sdt_sdth_map(madt_paddr);
27b59776
SZ
177 KKASSERT(madt != NULL);
178
7d460393
SZ
179 /*
180 * MADT in ACPI specification 1.0 - 4.0
181 */
182 if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
fa90647a 183 kprintf("madt_check: unknown MADT revision %d\n",
27b59776 184 madt->madt_hdr.sdth_rev);
27b59776
SZ
185 }
186
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);
191 error = EINVAL;
192 goto back;
193 }
194back:
d6c62a5d 195 sdt_sdth_unmap(&madt->madt_hdr);
27b59776
SZ
196 return error;
197}
198
199static int
200madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
201{
202 int size, cur, error;
203
204 size = madt->madt_hdr.sdth_len -
205 (sizeof(*madt) - sizeof(madt->madt_ents));
206 cur = 0;
207 error = 0;
208
209 while (size - cur > sizeof(struct acpi_madt_ent)) {
210 const struct acpi_madt_ent *ent;
211
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);
216 error = EINVAL;
217 break;
218 }
219 if (ent->me_len > (size - cur)) {
220 kprintf("madt_iterate_entries: invalid MADT "
221 "entry len %d, > table length\n", ent->me_len);
222 error = EINVAL;
223 break;
224 }
225
226 cur += ent->me_len;
227
228 /*
229 * Only Local APIC, I/O APIC and Interrupt Source Override
230 * are defined in ACPI specification 1.0 - 4.0
231 */
232 switch (ent->me_type) {
233 case MADT_ENT_LAPIC:
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);
237 error = EINVAL;
238 }
239 break;
240
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);
245 error = EINVAL;
246 }
247 break;
248
65b2387f 249 case MADT_ENT_INTSRC:
922e4bcb 250 if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
27b59776 251 kprintf("madt_iterate_entries: invalid MADT "
922e4bcb 252 "intsrc entry len %d\n",
27b59776
SZ
253 ent->me_len);
254 error = EINVAL;
255 }
256 break;
257 }
258 if (error)
259 break;
260
261 error = func(arg, ent);
262 if (error)
263 break;
264 }
265 return error;
266}
267
268static int
269madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
e25b100a
SZ
270{
271 const struct acpi_madt_lapic_addr *lapic_addr_ent;
272 uint64_t *addr64 = xarg;
273
274 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
275 return 0;
276 if (ent->me_len < sizeof(*lapic_addr_ent)) {
27b59776
SZ
277 kprintf("madt_lapic_pass1: "
278 "invalid LAPIC address override length\n");
e25b100a
SZ
279 return 0;
280 }
281 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
282
283 *addr64 = lapic_addr_ent->mla_lapic_addr;
284 return 0;
285}
286
1ebd15a6 287static vm_paddr_t
27b59776 288madt_lapic_pass1(void)
85fd9f87
SZ
289{
290 struct acpi_madt *madt;
1ebd15a6 291 vm_paddr_t lapic_addr;
e25b100a
SZ
292 uint64_t lapic_addr64;
293 int error;
85fd9f87 294
86120698 295 KKASSERT(madt_phyaddr != 0);
85fd9f87 296
d6c62a5d 297 madt = sdt_sdth_map(madt_phyaddr);
85fd9f87
SZ
298 KKASSERT(madt != NULL);
299
1ebd15a6 300 MADT_VPRINTF("LAPIC address 0x%x, flags %#x\n",
e907d51d 301 madt->madt_lapic_addr, madt->madt_flags);
223bf214
SZ
302 lapic_addr = madt->madt_lapic_addr;
303
e25b100a 304 lapic_addr64 = 0;
27b59776
SZ
305 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
306 &lapic_addr64);
e25b100a 307 if (error)
ed20d0e3 308 panic("madt_iterate_entries(pass1) failed");
e25b100a
SZ
309
310 if (lapic_addr64 != 0) {
65b2387f
SZ
311 kprintf("ACPI MADT: warning 64bits lapic address 0x%llx\n",
312 lapic_addr64);
e25b100a
SZ
313 lapic_addr = lapic_addr64;
314 }
315
d6c62a5d 316 sdt_sdth_unmap(&madt->madt_hdr);
223bf214
SZ
317
318 return lapic_addr;
319}
320
27b59776 321struct madt_lapic_pass2_cbarg {
a11898be
SZ
322 int cpu;
323 int bsp_found;
324 int bsp_apic_id;
325};
326
327static int
27b59776 328madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
a11898be
SZ
329{
330 const struct acpi_madt_lapic *lapic_ent;
27b59776 331 struct madt_lapic_pass2_cbarg *arg = xarg;
a11898be
SZ
332
333 if (ent->me_type != MADT_ENT_LAPIC)
334 return 0;
335
336 lapic_ent = (const struct acpi_madt_lapic *)ent;
337 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
65b2387f 338 MADT_VPRINTF("cpu id %d, apic id %d\n",
e907d51d 339 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
a11898be 340 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
41e2c7e0 341 lapic_set_cpuid(0, lapic_ent->ml_apic_id);
a11898be
SZ
342 arg->bsp_found = 1;
343 } else {
41e2c7e0 344 lapic_set_cpuid(arg->cpu, lapic_ent->ml_apic_id);
a11898be
SZ
345 arg->cpu++;
346 }
347 }
348 return 0;
349}
350
281d9482 351static int
27b59776 352madt_lapic_pass2(int bsp_apic_id)
223bf214
SZ
353{
354 struct acpi_madt *madt;
27b59776 355 struct madt_lapic_pass2_cbarg arg;
a11898be 356 int error;
223bf214 357
e907d51d 358 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
223bf214 359
86120698 360 KKASSERT(madt_phyaddr != 0);
223bf214 361
d6c62a5d 362 madt = sdt_sdth_map(madt_phyaddr);
223bf214 363 KKASSERT(madt != NULL);
85fd9f87 364
a11898be
SZ
365 bzero(&arg, sizeof(arg));
366 arg.cpu = 1;
367 arg.bsp_apic_id = bsp_apic_id;
85fd9f87 368
27b59776 369 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
a11898be 370 if (error)
ed20d0e3 371 panic("madt_iterate_entries(pass2) failed");
85fd9f87 372
a11898be 373 KKASSERT(arg.bsp_found);
2abaa030 374 naps = arg.cpu - 1; /* exclude BSP */
223bf214 375
d6c62a5d 376 sdt_sdth_unmap(&madt->madt_hdr);
a11898be
SZ
377
378 return 0;
223bf214
SZ
379}
380
86120698
SZ
381struct madt_lapic_probe_cbarg {
382 int cpu_count;
1ebd15a6 383 vm_paddr_t lapic_addr;
86120698
SZ
384};
385
386static int
387madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
388{
389 struct madt_lapic_probe_cbarg *arg = xarg;
390
391 if (ent->me_type == MADT_ENT_LAPIC) {
392 const struct acpi_madt_lapic *lapic_ent;
393
394 lapic_ent = (const struct acpi_madt_lapic *)ent;
b7fe87d9 395 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
86120698 396 arg->cpu_count++;
b7fe87d9
AH
397 if (lapic_ent->ml_apic_id == APICID_MAX) {
398 kprintf("madt_lapic_probe: "
399 "invalid LAPIC apic id %d\n",
400 lapic_ent->ml_apic_id);
401 return EINVAL;
402 }
740024d3 403 }
86120698
SZ
404 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
405 const struct acpi_madt_lapic_addr *lapic_addr_ent;
406
407 if (ent->me_len < sizeof(*lapic_addr_ent)) {
408 kprintf("madt_lapic_probe: "
409 "invalid LAPIC address override length\n");
410 return 0;
411 }
412 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
413
414 if (lapic_addr_ent->mla_lapic_addr != 0)
415 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
416 }
417 return 0;
418}
419
281d9482
SZ
420static int
421madt_lapic_probe(struct lapic_enumerator *e)
422{
86120698
SZ
423 struct madt_lapic_probe_cbarg arg;
424 struct acpi_madt *madt;
425 int error;
281d9482 426
86120698 427 if (madt_phyaddr == 0)
281d9482
SZ
428 return ENXIO;
429
d6c62a5d 430 madt = sdt_sdth_map(madt_phyaddr);
86120698
SZ
431 KKASSERT(madt != NULL);
432
433 bzero(&arg, sizeof(arg));
434 arg.lapic_addr = madt->madt_lapic_addr;
435
436 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
437 if (!error) {
f3d10f56
SZ
438 if (arg.cpu_count == 0) {
439 kprintf("madt_lapic_probe: no CPU is found\n");
86120698
SZ
440 error = EOPNOTSUPP;
441 }
442 if (arg.lapic_addr == 0) {
443 kprintf("madt_lapic_probe: zero LAPIC address\n");
444 error = EOPNOTSUPP;
445 }
446 }
447
d6c62a5d 448 sdt_sdth_unmap(&madt->madt_hdr);
86120698 449 return error;
281d9482
SZ
450}
451
f8ae0475 452static int
281d9482
SZ
453madt_lapic_enumerate(struct lapic_enumerator *e)
454{
1ebd15a6 455 vm_paddr_t lapic_addr;
281d9482
SZ
456 int bsp_apic_id;
457
86120698 458 KKASSERT(madt_phyaddr != 0);
281d9482 459
27b59776 460 lapic_addr = madt_lapic_pass1();
281d9482 461 if (lapic_addr == 0)
ed20d0e3 462 panic("madt_lapic_enumerate: no local apic");
281d9482 463
84cc808b 464 lapic_map(lapic_addr);
733c107f 465
cb7d6921 466 bsp_apic_id = APIC_ID(lapic->id);
f8ae0475
SZ
467 if (bsp_apic_id == APICID_MAX) {
468 /*
469 * XXX
470 * Some old brain dead BIOS will set BSP's LAPIC apic id
471 * to 255, though all LAPIC entries in MADT are valid.
472 */
473 kprintf("%s invalid BSP LAPIC apic id %d\n", __func__,
474 bsp_apic_id);
475 return EINVAL;
476 }
477
27b59776 478 if (madt_lapic_pass2(bsp_apic_id))
ed20d0e3 479 panic("madt_lapic_enumerate: madt_lapic_pass2 failed");
f8ae0475
SZ
480
481 return 0;
281d9482
SZ
482}
483
86120698
SZ
484static struct lapic_enumerator madt_lapic_enumerator = {
485 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
486 .lapic_probe = madt_lapic_probe,
487 .lapic_enumerate = madt_lapic_enumerate
281d9482
SZ
488};
489
490static void
27b59776 491madt_lapic_enum_register(void)
281d9482
SZ
492{
493 int prio;
494
495 prio = LAPIC_ENUM_PRIO_MADT;
496 kgetenv_int("hw.madt_lapic_prio", &prio);
86120698 497 madt_lapic_enumerator.lapic_prio = prio;
281d9482 498
86120698 499 lapic_enumerator_register(&madt_lapic_enumerator);
281d9482 500}
27b59776 501SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
65b2387f
SZ
502
503struct madt_ioapic_probe_cbarg {
504 int ioapic_cnt;
505 int gsi_base0;
506};
507
508static int
509madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
510{
511 struct madt_ioapic_probe_cbarg *arg = xarg;
512
513 if (ent->me_type == MADT_ENT_INTSRC) {
514 const struct acpi_madt_intsrc *intsrc_ent;
515 int trig, pola;
516
517 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
518
58587c23 519 if (intsrc_ent->mint_src >= ISA_IRQ_CNT) {
1365d5cd
SZ
520 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
521 intsrc_ent->mint_src);
522 return EINVAL;
523 }
524
65b2387f 525 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
1365d5cd
SZ
526 kprintf("ACPI MADT: warning intsrc irq %d "
527 "bus is not ISA (%d)\n",
528 intsrc_ent->mint_src, intsrc_ent->mint_bus);
65b2387f
SZ
529 }
530
531 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
532 MADT_INT_TRIG_SHIFT;
ae80be10 533 if (trig == MADT_INT_TRIG_RSVD) {
1365d5cd 534 kprintf("ACPI MADT: warning invalid intsrc irq %d "
b1fdcbc3
SZ
535 "trig, reserved\n", intsrc_ent->mint_src);
536 } else if (trig == MADT_INT_TRIG_LEVEL) {
537 MADT_VPRINTF("warning invalid intsrc irq %d "
538 "trig, level\n", intsrc_ent->mint_src);
65b2387f
SZ
539 }
540
541 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
542 MADT_INT_POLA_SHIFT;
ae80be10 543 if (pola == MADT_INT_POLA_RSVD) {
1365d5cd 544 kprintf("ACPI MADT: warning invalid intsrc irq %d "
b1fdcbc3
SZ
545 "pola, reserved\n", intsrc_ent->mint_src);
546 } else if (pola == MADT_INT_POLA_LOW) {
547 MADT_VPRINTF("warning invalid intsrc irq %d "
548 "pola, low\n", intsrc_ent->mint_src);
65b2387f
SZ
549 }
550 } else if (ent->me_type == MADT_ENT_IOAPIC) {
551 const struct acpi_madt_ioapic *ioapic_ent;
552
553 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
554 if (ioapic_ent->mio_addr == 0) {
555 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
556 return EINVAL;
557 }
740024d3
SZ
558 if (ioapic_ent->mio_apic_id == APICID_MAX) {
559 kprintf("madt_ioapic_probe: "
560 "invalid IOAPIC apic id %d\n",
561 ioapic_ent->mio_apic_id);
562 return EINVAL;
563 }
65b2387f
SZ
564
565 arg->ioapic_cnt++;
566 if (ioapic_ent->mio_gsi_base == 0)
567 arg->gsi_base0 = 1;
568 }
569 return 0;
570}
571
572static int
573madt_ioapic_probe(struct ioapic_enumerator *e)
574{
575 struct madt_ioapic_probe_cbarg arg;
576 struct acpi_madt *madt;
577 int error;
578
579 if (madt_phyaddr == 0)
580 return ENXIO;
581
d6c62a5d 582 madt = sdt_sdth_map(madt_phyaddr);
65b2387f
SZ
583 KKASSERT(madt != NULL);
584
585 bzero(&arg, sizeof(arg));
586
587 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
588 if (!error) {
589 if (arg.ioapic_cnt == 0) {
590 kprintf("madt_ioapic_probe: no IOAPIC\n");
591 error = ENXIO;
592 }
593 if (!arg.gsi_base0) {
594 kprintf("madt_ioapic_probe: no GSI base 0\n");
595 error = EINVAL;
596 }
597 }
598
d6c62a5d 599 sdt_sdth_unmap(&madt->madt_hdr);
65b2387f
SZ
600 return error;
601}
602
603static int
604madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
605{
606 if (ent->me_type == MADT_ENT_INTSRC) {
607 const struct acpi_madt_intsrc *intsrc_ent;
ae80be10
SZ
608 enum intr_trigger trig;
609 enum intr_polarity pola;
b1fdcbc3 610 int ent_trig, ent_pola;
65b2387f
SZ
611
612 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
ae80be10 613
58587c23 614 KKASSERT(intsrc_ent->mint_src < ISA_IRQ_CNT);
ae80be10
SZ
615 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
616 return 0;
617
618 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
b1fdcbc3
SZ
619 MADT_INT_TRIG_SHIFT;
620 if (ent_trig == MADT_INT_TRIG_RSVD)
1365d5cd 621 return 0;
b1fdcbc3 622 else if (ent_trig == MADT_INT_TRIG_LEVEL)
ae80be10 623 trig = INTR_TRIGGER_LEVEL;
b1fdcbc3 624 else
ae80be10 625 trig = INTR_TRIGGER_EDGE;
ae80be10 626
b1fdcbc3
SZ
627 ent_pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
628 MADT_INT_POLA_SHIFT;
629 if (ent_pola == MADT_INT_POLA_RSVD)
630 return 0;
631 else if (ent_pola == MADT_INT_POLA_LOW)
632 pola = INTR_POLARITY_LOW;
633 else
634 pola = INTR_POLARITY_HIGH;
c098b394
SZ
635
636 if (intsrc_ent->mint_src == acpi_sci_irqno()) {
637 acpi_sci_setmode1(trig, pola);
638 MADT_VPRINTF("SCI irq %d, first test %s/%s\n",
639 intsrc_ent->mint_src,
640 intr_str_trigger(trig), intr_str_polarity(pola));
641 }
642
b1fdcbc3
SZ
643 /*
644 * We ignore the polarity and trigger changes, since
645 * most of them are wrong or useless at best.
646 */
647 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi) {
ae80be10
SZ
648 /* Nothing changed */
649 return 0;
650 }
b1fdcbc3
SZ
651 trig = INTR_TRIGGER_EDGE;
652 pola = INTR_POLARITY_HIGH;
1365d5cd 653
4ecd5d4d 654 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
ae80be10 655 intsrc_ent->mint_src, intsrc_ent->mint_gsi,
4ecd5d4d 656 intr_str_trigger(trig), intr_str_polarity(pola));
ae80be10
SZ
657 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
658 trig, pola);
65b2387f
SZ
659 } else if (ent->me_type == MADT_ENT_IOAPIC) {
660 const struct acpi_madt_ioapic *ioapic_ent;
6ac31e9d
SZ
661 uint32_t ver;
662 void *addr;
663 int npin;
65b2387f
SZ
664
665 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
666 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
667 ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
668 ioapic_ent->mio_gsi_base);
ebf4f417 669
6ac31e9d 670 addr = ioapic_map(ioapic_ent->mio_addr);
ebf4f417 671
6ac31e9d
SZ
672 ver = ioapic_read(addr, IOAPIC_VER);
673 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
ebf4f417 674
6ac31e9d 675 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
65b2387f
SZ
676 }
677 return 0;
678}
679
680static void
681madt_ioapic_enumerate(struct ioapic_enumerator *e)
682{
683 struct acpi_madt *madt;
684 int error;
685
686 KKASSERT(madt_phyaddr != 0);
687
d6c62a5d 688 madt = sdt_sdth_map(madt_phyaddr);
65b2387f
SZ
689 KKASSERT(madt != NULL);
690
691 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
692 if (error)
ed20d0e3 693 panic("madt_ioapic_enumerate failed");
65b2387f 694
d6c62a5d 695 sdt_sdth_unmap(&madt->madt_hdr);
65b2387f
SZ
696}
697
698static struct ioapic_enumerator madt_ioapic_enumerator = {
699 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
700 .ioapic_probe = madt_ioapic_probe,
701 .ioapic_enumerate = madt_ioapic_enumerate
702};
703
704static void
705madt_ioapic_enum_register(void)
706{
707 int prio;
708
709 prio = IOAPIC_ENUM_PRIO_MADT;
710 kgetenv_int("hw.madt_ioapic_prio", &prio);
711 madt_ioapic_enumerator.ioapic_prio = prio;
712
713 ioapic_enumerator_register(&madt_ioapic_enumerator);
714}
715SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
716 madt_ioapic_enum_register, 0);