x86_64: Remove old IOAPIC code
[dragonfly.git] / sys / platform / pc64 / acpica5 / acpi_madt.c
CommitLineData
8e4c6923
MN
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>
91903a05 37#include <sys/kernel.h>
8e4c6923
MN
38#include <sys/systm.h>
39
40#include <machine/pmap.h>
41#include <machine/smp.h>
ee5932ac
MN
42#include <machine/md_var.h>
43#include <machine/specialreg.h>
91903a05 44#include <machine_base/apic/mpapic.h>
8e4c6923 45
d6c62a5d
SZ
46#include "acpi_sdt.h"
47#include "acpi_sdt_var.h"
48
c87b587d
MN
49#define MADT_VPRINTF(fmt, arg...) \
50do { \
51 if (bootverbose) \
52 kprintf("ACPI MADT: " fmt , ##arg); \
53} while (0)
54
8e4c6923
MN
55/* Multiple APIC Description Table */
56struct acpi_madt {
57 struct acpi_sdth madt_hdr;
58 uint32_t madt_lapic_addr;
59 uint32_t madt_flags;
60 uint8_t madt_ents[1];
61} __packed;
62
63/* Common parts of MADT APIC structure */
64struct acpi_madt_ent {
65 uint8_t me_type; /* MADT_ENT_ */
66 uint8_t me_len;
67} __packed;
68
69#define MADT_ENT_LAPIC 0
0492bd82 70#define MADT_ENT_IOAPIC 1
65b2387f 71#define MADT_ENT_INTSRC 2
ebbde674 72#define MADT_ENT_LAPIC_ADDR 5
8e4c6923
MN
73
74/* MADT Processor Local APIC */
75struct acpi_madt_lapic {
76 struct acpi_madt_ent ml_hdr;
77 uint8_t ml_cpu_id;
78 uint8_t ml_apic_id;
79 uint32_t ml_flags; /* MADT_LAPIC_ */
80} __packed;
81
82#define MADT_LAPIC_ENABLED 0x1
83
0492bd82
MN
84/* MADT I/O APIC */
85struct acpi_madt_ioapic {
86 struct acpi_madt_ent mio_hdr;
87 uint8_t mio_apic_id;
88 uint8_t mio_reserved;
89 uint32_t mio_addr;
90 uint32_t mio_gsi_base;
91} __packed;
92
2bbbec42 93/* MADT Interrupt Source Override */
922e4bcb
SZ
94struct acpi_madt_intsrc {
95 struct acpi_madt_ent mint_hdr;
65b2387f 96 uint8_t mint_bus; /* MADT_INT_BUS_ */
922e4bcb
SZ
97 uint8_t mint_src;
98 uint32_t mint_gsi;
99 uint16_t mint_flags; /* MADT_INT_ */
2bbbec42
SZ
100} __packed;
101
65b2387f
SZ
102#define MADT_INT_BUS_ISA 0
103
922e4bcb
SZ
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
2bbbec42 116
ebbde674
MN
117/* MADT Local APIC Address Override */
118struct acpi_madt_lapic_addr {
119 struct acpi_madt_ent mla_hdr;
120 uint16_t mla_reserved;
121 uint64_t mla_lapic_addr;
122} __packed;
123
0492bd82
MN
124typedef int (*madt_iter_t)(void *,
125 const struct acpi_madt_ent *);
8e4c6923 126
4ad5917f 127static int madt_check(vm_paddr_t);
0492bd82
MN
128static int madt_iterate_entries(struct acpi_madt *,
129 madt_iter_t, void *);
8e4c6923 130
27b59776
SZ
131static vm_offset_t madt_lapic_pass1(void);
132static int madt_lapic_pass2(int);
91903a05
MN
133
134static void madt_lapic_enumerate(struct lapic_enumerator *);
135static int madt_lapic_probe(struct lapic_enumerator *);
136
65b2387f
SZ
137static void madt_ioapic_enumerate(
138 struct ioapic_enumerator *);
139static int madt_ioapic_probe(struct ioapic_enumerator *);
140
86120698
SZ
141static vm_paddr_t madt_phyaddr;
142
143static void
8e4c6923
MN
144madt_probe(void)
145{
d6c62a5d 146 vm_paddr_t madt_paddr;
8e4c6923 147
86120698
SZ
148 KKASSERT(madt_phyaddr == 0);
149
d6c62a5d 150 madt_paddr = sdt_search(ACPI_MADT_SIG);
8e4c6923
MN
151 if (madt_paddr == 0) {
152 kprintf("madt_probe: can't locate MADT\n");
86120698 153 return;
8e4c6923 154 }
4ad5917f
MN
155
156 /* Preliminary checks */
86120698
SZ
157 if (madt_check(madt_paddr)) {
158 kprintf("madt_probe: madt_check failed\n");
159 return;
160 }
161
162 madt_phyaddr = madt_paddr;
8e4c6923 163}
d6c62a5d 164SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
8e4c6923 165
ebbde674 166static int
27b59776
SZ
167madt_check(vm_paddr_t madt_paddr)
168{
169 struct acpi_madt *madt;
170 int error = 0;
171
172 KKASSERT(madt_paddr != 0);
173
d6c62a5d 174 madt = sdt_sdth_map(madt_paddr);
27b59776
SZ
175 KKASSERT(madt != NULL);
176
7d460393
SZ
177 /*
178 * MADT in ACPI specification 1.0 - 4.0
179 */
180 if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
27b59776
SZ
181 kprintf("madt_check: unsupported MADT revision %d\n",
182 madt->madt_hdr.sdth_rev);
183 error = EOPNOTSUPP;
184 goto back;
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)
ebbde674
MN
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");
ebbde674
MN
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
91903a05 287static vm_offset_t
27b59776 288madt_lapic_pass1(void)
8e4c6923
MN
289{
290 struct acpi_madt *madt;
4ad5917f 291 vm_offset_t lapic_addr;
ebbde674
MN
292 uint64_t lapic_addr64;
293 int error;
8e4c6923 294
86120698 295 KKASSERT(madt_phyaddr != 0);
8e4c6923 296
d6c62a5d 297 madt = sdt_sdth_map(madt_phyaddr);
8e4c6923
MN
298 KKASSERT(madt != NULL);
299
c87b587d
MN
300 MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
301 madt->madt_lapic_addr, madt->madt_flags);
4ad5917f
MN
302 lapic_addr = madt->madt_lapic_addr;
303
ebbde674 304 lapic_addr64 = 0;
27b59776
SZ
305 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
306 &lapic_addr64);
ebbde674
MN
307 if (error)
308 panic("madt_iterate_entries(pass1) failed\n");
309
310 if (lapic_addr64 != 0) {
cb31590d
SZ
311 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
312 lapic_addr64);
ebbde674
MN
313 lapic_addr = lapic_addr64;
314 }
315
d6c62a5d 316 sdt_sdth_unmap(&madt->madt_hdr);
4ad5917f
MN
317
318 return lapic_addr;
319}
320
27b59776 321struct madt_lapic_pass2_cbarg {
2c394345
MN
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)
2c394345
MN
329{
330 const struct acpi_madt_lapic *lapic_ent;
27b59776 331 struct madt_lapic_pass2_cbarg *arg = xarg;
2c394345
MN
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",
c87b587d 339 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
2c394345
MN
340 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
341 mp_set_cpuids(0, lapic_ent->ml_apic_id);
342 arg->bsp_found = 1;
343 } else {
344 mp_set_cpuids(arg->cpu, lapic_ent->ml_apic_id);
345 arg->cpu++;
346 }
347 }
348 return 0;
349}
350
91903a05 351static int
27b59776 352madt_lapic_pass2(int bsp_apic_id)
4ad5917f
MN
353{
354 struct acpi_madt *madt;
27b59776 355 struct madt_lapic_pass2_cbarg arg;
2c394345 356 int error;
4ad5917f 357
c87b587d 358 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
4ad5917f 359
86120698 360 KKASSERT(madt_phyaddr != 0);
4ad5917f 361
d6c62a5d 362 madt = sdt_sdth_map(madt_phyaddr);
4ad5917f 363 KKASSERT(madt != NULL);
8e4c6923 364
2c394345
MN
365 bzero(&arg, sizeof(arg));
366 arg.cpu = 1;
367 arg.bsp_apic_id = bsp_apic_id;
8e4c6923 368
27b59776 369 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
2c394345
MN
370 if (error)
371 panic("madt_iterate_entries(pass2) failed\n");
8e4c6923 372
2c394345
MN
373 KKASSERT(arg.bsp_found);
374 KKASSERT(arg.cpu > 1);
375 mp_naps = arg.cpu - 1; /* exclude BSP */
4ad5917f 376
d6c62a5d 377 sdt_sdth_unmap(&madt->madt_hdr);
2c394345
MN
378
379 return 0;
4ad5917f
MN
380}
381
86120698
SZ
382struct madt_lapic_probe_cbarg {
383 int cpu_count;
384 vm_offset_t lapic_addr;
385};
386
387static int
388madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
389{
390 struct madt_lapic_probe_cbarg *arg = xarg;
391
392 if (ent->me_type == MADT_ENT_LAPIC) {
393 const struct acpi_madt_lapic *lapic_ent;
394
395 lapic_ent = (const struct acpi_madt_lapic *)ent;
396 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
397 arg->cpu_count++;
398 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
399 const struct acpi_madt_lapic_addr *lapic_addr_ent;
400
401 if (ent->me_len < sizeof(*lapic_addr_ent)) {
402 kprintf("madt_lapic_probe: "
403 "invalid LAPIC address override length\n");
404 return 0;
405 }
406 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
407
408 if (lapic_addr_ent->mla_lapic_addr != 0)
409 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
410 }
411 return 0;
412}
413
91903a05
MN
414static int
415madt_lapic_probe(struct lapic_enumerator *e)
416{
86120698
SZ
417 struct madt_lapic_probe_cbarg arg;
418 struct acpi_madt *madt;
419 int error;
91903a05 420
86120698 421 if (madt_phyaddr == 0)
91903a05
MN
422 return ENXIO;
423
d6c62a5d 424 madt = sdt_sdth_map(madt_phyaddr);
86120698
SZ
425 KKASSERT(madt != NULL);
426
427 bzero(&arg, sizeof(arg));
428 arg.lapic_addr = madt->madt_lapic_addr;
429
430 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
431 if (!error) {
432 if (arg.cpu_count <= 1) {
433 kprintf("madt_lapic_probe: "
434 "less than 2 CPUs is found\n");
435 error = EOPNOTSUPP;
436 }
437 if (arg.lapic_addr == 0) {
438 kprintf("madt_lapic_probe: zero LAPIC address\n");
439 error = EOPNOTSUPP;
440 }
441 }
442
d6c62a5d 443 sdt_sdth_unmap(&madt->madt_hdr);
86120698 444 return error;
91903a05
MN
445}
446
447static void
448madt_lapic_enumerate(struct lapic_enumerator *e)
449{
91903a05
MN
450 vm_offset_t lapic_addr;
451 int bsp_apic_id;
452
86120698 453 KKASSERT(madt_phyaddr != 0);
91903a05 454
27b59776 455 lapic_addr = madt_lapic_pass1();
91903a05 456 if (lapic_addr == 0)
86120698 457 panic("madt_lapic_enumerate: no local apic\n");
91903a05 458
b44f1d28 459 lapic_map(lapic_addr);
1c1f402e
MN
460
461 bsp_apic_id = APIC_ID(lapic->id);
27b59776
SZ
462 if (madt_lapic_pass2(bsp_apic_id))
463 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
91903a05
MN
464}
465
86120698
SZ
466static struct lapic_enumerator madt_lapic_enumerator = {
467 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
468 .lapic_probe = madt_lapic_probe,
469 .lapic_enumerate = madt_lapic_enumerate
91903a05
MN
470};
471
472static void
27b59776 473madt_lapic_enum_register(void)
91903a05
MN
474{
475 int prio;
476
477 prio = LAPIC_ENUM_PRIO_MADT;
478 kgetenv_int("hw.madt_lapic_prio", &prio);
86120698 479 madt_lapic_enumerator.lapic_prio = prio;
91903a05 480
86120698 481 lapic_enumerator_register(&madt_lapic_enumerator);
91903a05 482}
27b59776 483SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
65b2387f
SZ
484
485struct madt_ioapic_probe_cbarg {
486 int ioapic_cnt;
487 int gsi_base0;
488};
489
490static int
491madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
492{
493 struct madt_ioapic_probe_cbarg *arg = xarg;
494
495 if (ent->me_type == MADT_ENT_INTSRC) {
496 const struct acpi_madt_intsrc *intsrc_ent;
497 int trig, pola;
498
499 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
500
1365d5cd
SZ
501 /* XXX magic number */
502 if (intsrc_ent->mint_src >= 16) {
503 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
504 intsrc_ent->mint_src);
505 return EINVAL;
506 }
507
65b2387f 508 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
1365d5cd
SZ
509 kprintf("ACPI MADT: warning intsrc irq %d "
510 "bus is not ISA (%d)\n",
511 intsrc_ent->mint_src, intsrc_ent->mint_bus);
65b2387f
SZ
512 }
513
514 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
515 MADT_INT_TRIG_SHIFT;
ae80be10 516 if (trig == MADT_INT_TRIG_RSVD) {
1365d5cd
SZ
517 kprintf("ACPI MADT: warning invalid intsrc irq %d "
518 "trig (%d)\n", intsrc_ent->mint_src, trig);
65b2387f
SZ
519 }
520
521 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
522 MADT_INT_POLA_SHIFT;
ae80be10 523 if (pola == MADT_INT_POLA_RSVD) {
1365d5cd
SZ
524 kprintf("ACPI MADT: warning invalid intsrc irq %d "
525 "pola (%d)\n", intsrc_ent->mint_src, pola);
65b2387f
SZ
526 }
527 } else if (ent->me_type == MADT_ENT_IOAPIC) {
528 const struct acpi_madt_ioapic *ioapic_ent;
529
530 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
531 if (ioapic_ent->mio_addr == 0) {
532 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
533 return EINVAL;
534 }
535
536 arg->ioapic_cnt++;
537 if (ioapic_ent->mio_gsi_base == 0)
538 arg->gsi_base0 = 1;
539 }
540 return 0;
541}
542
543static int
544madt_ioapic_probe(struct ioapic_enumerator *e)
545{
546 struct madt_ioapic_probe_cbarg arg;
547 struct acpi_madt *madt;
548 int error;
549
550 if (madt_phyaddr == 0)
551 return ENXIO;
552
d6c62a5d 553 madt = sdt_sdth_map(madt_phyaddr);
65b2387f
SZ
554 KKASSERT(madt != NULL);
555
556 bzero(&arg, sizeof(arg));
557
558 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
559 if (!error) {
560 if (arg.ioapic_cnt == 0) {
561 kprintf("madt_ioapic_probe: no IOAPIC\n");
562 error = ENXIO;
563 }
564 if (!arg.gsi_base0) {
565 kprintf("madt_ioapic_probe: no GSI base 0\n");
566 error = EINVAL;
567 }
568 }
569
d6c62a5d 570 sdt_sdth_unmap(&madt->madt_hdr);
65b2387f
SZ
571 return error;
572}
573
574static int
575madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
576{
577 if (ent->me_type == MADT_ENT_INTSRC) {
578 const struct acpi_madt_intsrc *intsrc_ent;
ae80be10
SZ
579 enum intr_trigger trig;
580 enum intr_polarity pola;
581 int ent_trig;
65b2387f
SZ
582
583 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
ae80be10
SZ
584
585 KKASSERT(intsrc_ent->mint_src < 16);
586 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
587 return 0;
588
589 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
590 MADT_INT_TRIG_SHIFT;
591 if (ent_trig == MADT_INT_TRIG_RSVD) {
1365d5cd 592 return 0;
ca4177e4 593#ifdef notyet
ae80be10
SZ
594 } else if (ent_trig == MADT_INT_TRIG_LEVEL) {
595 trig = INTR_TRIGGER_LEVEL;
596 pola = INTR_POLARITY_LOW;
ca4177e4 597#endif
ae80be10
SZ
598 } else {
599 trig = INTR_TRIGGER_EDGE;
600 pola = INTR_POLARITY_HIGH;
601 }
602
603 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi &&
604 trig == INTR_TRIGGER_EDGE) {
605 /* Nothing changed */
606 return 0;
607 }
1365d5cd 608
4ecd5d4d 609 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
ae80be10 610 intsrc_ent->mint_src, intsrc_ent->mint_gsi,
4ecd5d4d 611 intr_str_trigger(trig), intr_str_polarity(pola));
ae80be10
SZ
612 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
613 trig, pola);
65b2387f
SZ
614 } else if (ent->me_type == MADT_ENT_IOAPIC) {
615 const struct acpi_madt_ioapic *ioapic_ent;
7a603b36
SZ
616 uint32_t ver;
617 void *addr;
618 int npin;
65b2387f
SZ
619
620 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
621 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
622 ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
623 ioapic_ent->mio_gsi_base);
ebf4f417 624
7a603b36 625 addr = ioapic_map(ioapic_ent->mio_addr);
ebf4f417 626
7a603b36
SZ
627 ver = ioapic_read(addr, IOAPIC_VER);
628 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
ebf4f417 629
7a603b36 630 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
65b2387f
SZ
631 }
632 return 0;
633}
634
635static void
636madt_ioapic_enumerate(struct ioapic_enumerator *e)
637{
638 struct acpi_madt *madt;
639 int error;
640
641 KKASSERT(madt_phyaddr != 0);
642
d6c62a5d 643 madt = sdt_sdth_map(madt_phyaddr);
65b2387f
SZ
644 KKASSERT(madt != NULL);
645
646 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
647 if (error)
648 panic("madt_ioapic_enumerate failed\n");
649
d6c62a5d 650 sdt_sdth_unmap(&madt->madt_hdr);
65b2387f
SZ
651}
652
653static struct ioapic_enumerator madt_ioapic_enumerator = {
654 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
655 .ioapic_probe = madt_ioapic_probe,
656 .ioapic_enumerate = madt_ioapic_enumerate
657};
658
659static void
660madt_ioapic_enum_register(void)
661{
662 int prio;
663
664 prio = IOAPIC_ENUM_PRIO_MADT;
665 kgetenv_int("hw.madt_ioapic_prio", &prio);
666 madt_ioapic_enumerator.ioapic_prio = prio;
667
668 ioapic_enumerator_register(&madt_ioapic_enumerator);
669}
670SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
671 madt_ioapic_enum_register, 0);