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