fa03da7e3735afcdc46c392f445a01e4e46bb342
[dragonfly.git] / sys / platform / pc32 / acpica5 / acpi_madt.c
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>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39
40 #include <machine_base/isa/isa_intr.h>
41 #include <machine_base/apic/lapic.h>
42 #include <machine_base/apic/ioapic.h>
43 #include <machine_base/apic/apicvar.h>
44
45 #include "acpi_sdt.h"
46 #include "acpi_sdt_var.h"
47
48 extern int naps;
49
50 #define MADT_VPRINTF(fmt, arg...) \
51 do { \
52         if (bootverbose) \
53                 kprintf("ACPI MADT: " fmt , ##arg); \
54 } while (0)
55
56 /* Multiple APIC Description Table */
57 struct 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 */
65 struct acpi_madt_ent {
66         uint8_t                 me_type;        /* MADT_ENT_ */
67         uint8_t                 me_len;
68 } __packed;
69
70 #define MADT_ENT_LAPIC          0
71 #define MADT_ENT_IOAPIC         1
72 #define MADT_ENT_INTSRC         2
73 #define MADT_ENT_LAPIC_ADDR     5
74
75 /* MADT Processor Local APIC */
76 struct 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
85 /* MADT I/O APIC */
86 struct 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
94 /* MADT Interrupt Source Override */
95 struct acpi_madt_intsrc {
96         struct acpi_madt_ent    mint_hdr;
97         uint8_t                 mint_bus;       /* MADT_INT_BUS_ */
98         uint8_t                 mint_src;
99         uint32_t                mint_gsi;
100         uint16_t                mint_flags;     /* MADT_INT_ */
101 } __packed;
102
103 #define MADT_INT_BUS_ISA        0
104
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
117
118 /* MADT Local APIC Address Override */
119 struct 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
125 typedef int                     (*madt_iter_t)(void *,
126                                     const struct acpi_madt_ent *);
127
128 static int                      madt_check(vm_paddr_t);
129 static int                      madt_iterate_entries(struct acpi_madt *,
130                                     madt_iter_t, void *);
131
132 static vm_paddr_t               madt_lapic_pass1(void);
133 static int                      madt_lapic_pass2(int);
134
135 static int                      madt_lapic_enumerate(struct lapic_enumerator *);
136 static int                      madt_lapic_probe(struct lapic_enumerator *);
137
138 static void                     madt_ioapic_enumerate(
139                                     struct ioapic_enumerator *);
140 static int                      madt_ioapic_probe(struct ioapic_enumerator *);
141
142 static vm_paddr_t               madt_phyaddr;
143
144 static void
145 madt_probe(void)
146 {
147         vm_paddr_t madt_paddr;
148
149         KKASSERT(madt_phyaddr == 0);
150
151         madt_paddr = sdt_search(ACPI_MADT_SIG);
152         if (madt_paddr == 0) {
153                 kprintf("madt_probe: can't locate MADT\n");
154                 return;
155         }
156
157         /* Preliminary checks */
158         if (madt_check(madt_paddr)) {
159                 kprintf("madt_probe: madt_check failed\n");
160                 return;
161         }
162
163         madt_phyaddr = madt_paddr;
164 }
165 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
166
167 static int
168 madt_check(vm_paddr_t madt_paddr)
169 {
170         struct acpi_madt *madt;
171         int error = 0;
172
173         KKASSERT(madt_paddr != 0);
174
175         madt = sdt_sdth_map(madt_paddr);
176         KKASSERT(madt != NULL);
177
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) {
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         }
195 back:
196         sdt_sdth_unmap(&madt->madt_hdr);
197         return error;
198 }
199
200 static int
201 madt_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
250                 case MADT_ENT_INTSRC:
251                         if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
252                                 kprintf("madt_iterate_entries: invalid MADT "
253                                         "intsrc entry len %d\n",
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
269 static int
270 madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
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)) {
278                 kprintf("madt_lapic_pass1: "
279                         "invalid LAPIC address override length\n");
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
288 static vm_paddr_t
289 madt_lapic_pass1(void)
290 {
291         struct acpi_madt *madt;
292         vm_paddr_t lapic_addr;
293         uint64_t lapic_addr64;
294         int error;
295
296         KKASSERT(madt_phyaddr != 0);
297
298         madt = sdt_sdth_map(madt_phyaddr);
299         KKASSERT(madt != NULL);
300
301         MADT_VPRINTF("LAPIC address 0x%x, flags %#x\n",
302                      madt->madt_lapic_addr, madt->madt_flags);
303         lapic_addr = madt->madt_lapic_addr;
304
305         lapic_addr64 = 0;
306         error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
307                                      &lapic_addr64);
308         if (error)
309                 panic("madt_iterate_entries(pass1) failed");
310
311         if (lapic_addr64 != 0) {
312                 kprintf("ACPI MADT: warning 64bits lapic address 0x%llx\n",
313                         lapic_addr64);
314                 lapic_addr = lapic_addr64;
315         }
316
317         sdt_sdth_unmap(&madt->madt_hdr);
318
319         return lapic_addr;
320 }
321
322 struct madt_lapic_pass2_cbarg {
323         int     cpu;
324         int     bsp_found;
325         int     bsp_apic_id;
326 };
327
328 static int
329 madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
330 {
331         const struct acpi_madt_lapic *lapic_ent;
332         struct madt_lapic_pass2_cbarg *arg = xarg;
333
334         if (ent->me_type != MADT_ENT_LAPIC)
335                 return 0;
336
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                         lapic_set_cpuid(0, lapic_ent->ml_apic_id);
343                         arg->bsp_found = 1;
344                 } else {
345                         lapic_set_cpuid(arg->cpu, lapic_ent->ml_apic_id);
346                         arg->cpu++;
347                 }
348         }
349         return 0;
350 }
351
352 static int
353 madt_lapic_pass2(int bsp_apic_id)
354 {
355         struct acpi_madt *madt;
356         struct madt_lapic_pass2_cbarg arg;
357         int error;
358
359         MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
360
361         KKASSERT(madt_phyaddr != 0);
362
363         madt = sdt_sdth_map(madt_phyaddr);
364         KKASSERT(madt != NULL);
365
366         bzero(&arg, sizeof(arg));
367         arg.cpu = 1;
368         arg.bsp_apic_id = bsp_apic_id;
369
370         error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
371         if (error)
372                 panic("madt_iterate_entries(pass2) failed");
373
374         KKASSERT(arg.bsp_found);
375         naps = arg.cpu - 1; /* exclude BSP */
376
377         sdt_sdth_unmap(&madt->madt_hdr);
378
379         return 0;
380 }
381
382 struct madt_lapic_probe_cbarg {
383         int             cpu_count;
384         vm_paddr_t      lapic_addr;
385 };
386
387 static int
388 madt_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                         if (lapic_ent->ml_apic_id == APICID_MAX) {
399                                 kprintf("madt_lapic_probe: "
400                                     "invalid LAPIC apic id %d\n",
401                                     lapic_ent->ml_apic_id);
402                                 return EINVAL;
403                         }
404                 }
405         } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
406                 const struct acpi_madt_lapic_addr *lapic_addr_ent;
407
408                 if (ent->me_len < sizeof(*lapic_addr_ent)) {
409                         kprintf("madt_lapic_probe: "
410                                 "invalid LAPIC address override length\n");
411                         return 0;
412                 }
413                 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
414
415                 if (lapic_addr_ent->mla_lapic_addr != 0)
416                         arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
417         }
418         return 0;
419 }
420
421 static int
422 madt_lapic_probe(struct lapic_enumerator *e)
423 {
424         struct madt_lapic_probe_cbarg arg;
425         struct acpi_madt *madt;
426         int error;
427
428         if (madt_phyaddr == 0)
429                 return ENXIO;
430
431         madt = sdt_sdth_map(madt_phyaddr);
432         KKASSERT(madt != NULL);
433
434         bzero(&arg, sizeof(arg));
435         arg.lapic_addr = madt->madt_lapic_addr;
436
437         error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
438         if (!error) {
439                 if (arg.cpu_count == 0) {
440                         kprintf("madt_lapic_probe: no CPU is found\n");
441                         error = EOPNOTSUPP;
442                 }
443                 if (arg.lapic_addr == 0) {
444                         kprintf("madt_lapic_probe: zero LAPIC address\n");
445                         error = EOPNOTSUPP;
446                 }
447         }
448
449         sdt_sdth_unmap(&madt->madt_hdr);
450         return error;
451 }
452
453 static int
454 madt_lapic_enumerate(struct lapic_enumerator *e)
455 {
456         vm_paddr_t lapic_addr;
457         int bsp_apic_id;
458
459         KKASSERT(madt_phyaddr != 0);
460
461         lapic_addr = madt_lapic_pass1();
462         if (lapic_addr == 0)
463                 panic("madt_lapic_enumerate: no local apic");
464
465         lapic_map(lapic_addr);
466
467         bsp_apic_id = APIC_ID(lapic->id);
468         if (bsp_apic_id == APICID_MAX) {
469                 /*
470                  * XXX
471                  * Some old brain dead BIOS will set BSP's LAPIC apic id
472                  * to 255, though all LAPIC entries in MADT are valid.
473                  */
474                 kprintf("%s invalid BSP LAPIC apic id %d\n", __func__,
475                     bsp_apic_id);
476                 return EINVAL;
477         }
478
479         if (madt_lapic_pass2(bsp_apic_id))
480                 panic("madt_lapic_enumerate: madt_lapic_pass2 failed");
481
482         return 0;
483 }
484
485 static struct lapic_enumerator  madt_lapic_enumerator = {
486         .lapic_prio = LAPIC_ENUM_PRIO_MADT,
487         .lapic_probe = madt_lapic_probe,
488         .lapic_enumerate = madt_lapic_enumerate
489 };
490
491 static void
492 madt_lapic_enum_register(void)
493 {
494         int prio;
495
496         prio = LAPIC_ENUM_PRIO_MADT;
497         kgetenv_int("hw.madt_lapic_prio", &prio);
498         madt_lapic_enumerator.lapic_prio = prio;
499
500         lapic_enumerator_register(&madt_lapic_enumerator);
501 }
502 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
503
504 struct madt_ioapic_probe_cbarg {
505         int     ioapic_cnt;
506         int     gsi_base0;
507 };
508
509 static int
510 madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
511 {
512         struct madt_ioapic_probe_cbarg *arg = xarg;
513
514         if (ent->me_type == MADT_ENT_INTSRC) {
515                 const struct acpi_madt_intsrc *intsrc_ent;
516                 int trig, pola;
517
518                 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
519
520                 if (intsrc_ent->mint_src >= ISA_IRQ_CNT) {
521                         kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
522                                 intsrc_ent->mint_src);
523                         return EINVAL;
524                 }
525
526                 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
527                         kprintf("ACPI MADT: warning intsrc irq %d "
528                                 "bus is not ISA (%d)\n",
529                                 intsrc_ent->mint_src, intsrc_ent->mint_bus);
530                 }
531
532                 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
533                        MADT_INT_TRIG_SHIFT;
534                 if (trig == MADT_INT_TRIG_RSVD) {
535                         kprintf("ACPI MADT: warning invalid intsrc irq %d "
536                                 "trig, reserved\n", intsrc_ent->mint_src);
537                 } else if (trig == MADT_INT_TRIG_LEVEL) {
538                         MADT_VPRINTF("warning invalid intsrc irq %d "
539                             "trig, level\n", intsrc_ent->mint_src);
540                 }
541
542                 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
543                        MADT_INT_POLA_SHIFT;
544                 if (pola == MADT_INT_POLA_RSVD) {
545                         kprintf("ACPI MADT: warning invalid intsrc irq %d "
546                                 "pola, reserved\n", intsrc_ent->mint_src);
547                 } else if (pola == MADT_INT_POLA_LOW) {
548                         MADT_VPRINTF("warning invalid intsrc irq %d "
549                             "pola, low\n", intsrc_ent->mint_src);
550                 }
551         } else if (ent->me_type == MADT_ENT_IOAPIC) {
552                 const struct acpi_madt_ioapic *ioapic_ent;
553
554                 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
555                 if (ioapic_ent->mio_addr == 0) {
556                         kprintf("madt_ioapic_probe: zero IOAPIC address\n");
557                         return EINVAL;
558                 }
559                 if (ioapic_ent->mio_apic_id == APICID_MAX) {
560                         kprintf("madt_ioapic_probe: "
561                             "invalid IOAPIC apic id %d\n",
562                             ioapic_ent->mio_apic_id);
563                         return EINVAL;
564                 }
565
566                 arg->ioapic_cnt++;
567                 if (ioapic_ent->mio_gsi_base == 0)
568                         arg->gsi_base0 = 1;
569         }
570         return 0;
571 }
572
573 static int
574 madt_ioapic_probe(struct ioapic_enumerator *e)
575 {
576         struct madt_ioapic_probe_cbarg arg;
577         struct acpi_madt *madt;
578         int error;
579
580         if (madt_phyaddr == 0)
581                 return ENXIO;
582
583         madt = sdt_sdth_map(madt_phyaddr);
584         KKASSERT(madt != NULL);
585
586         bzero(&arg, sizeof(arg));
587
588         error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
589         if (!error) {
590                 if (arg.ioapic_cnt == 0) {
591                         kprintf("madt_ioapic_probe: no IOAPIC\n");
592                         error = ENXIO;
593                 }
594                 if (!arg.gsi_base0) {
595                         kprintf("madt_ioapic_probe: no GSI base 0\n");
596                         error = EINVAL;
597                 }
598         }
599
600         sdt_sdth_unmap(&madt->madt_hdr);
601         return error;
602 }
603
604 static int
605 madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
606 {
607         if (ent->me_type == MADT_ENT_INTSRC) {
608                 const struct acpi_madt_intsrc *intsrc_ent;
609                 enum intr_trigger trig;
610                 enum intr_polarity pola;
611 #ifdef foo
612                 int ent_trig, ent_pola;
613 #endif
614
615                 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
616
617                 KKASSERT(intsrc_ent->mint_src < ISA_IRQ_CNT);
618                 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
619                         return 0;
620
621 #ifdef foo
622                 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
623                     MADT_INT_TRIG_SHIFT;
624                 if (ent_trig == MADT_INT_TRIG_RSVD)
625                         return 0;
626                 else if (ent_trig == MADT_INT_TRIG_LEVEL)
627                         trig = INTR_TRIGGER_LEVEL;
628                 else
629                         trig = INTR_TRIGGER_EDGE;
630
631                 ent_pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
632                     MADT_INT_POLA_SHIFT;
633                 if (ent_pola == MADT_INT_POLA_RSVD)
634                         return 0;
635                 else if (ent_pola == MADT_INT_POLA_LOW)
636                         pola = INTR_POLARITY_LOW;
637                 else
638                         pola = INTR_POLARITY_HIGH;
639 #else
640                 /*
641                  * We ignore the polarity and trigger changes, since
642                  * most of them are wrong or useless at best.
643                  */
644                 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi) {
645                         /* Nothing changed */
646                         return 0;
647                 }
648                 trig = INTR_TRIGGER_EDGE;
649                 pola = INTR_POLARITY_HIGH;
650 #endif
651
652                 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
653                              intsrc_ent->mint_src, intsrc_ent->mint_gsi,
654                              intr_str_trigger(trig), intr_str_polarity(pola));
655                 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
656                               trig, pola);
657         } else if (ent->me_type == MADT_ENT_IOAPIC) {
658                 const struct acpi_madt_ioapic *ioapic_ent;
659                 uint32_t ver;
660                 void *addr;
661                 int npin;
662
663                 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
664                 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
665                              ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
666                              ioapic_ent->mio_gsi_base);
667
668                 addr = ioapic_map(ioapic_ent->mio_addr);
669
670                 ver = ioapic_read(addr, IOAPIC_VER);
671                 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
672
673                 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
674         }
675         return 0;
676 }
677
678 static void
679 madt_ioapic_enumerate(struct ioapic_enumerator *e)
680 {
681         struct acpi_madt *madt;
682         int error;
683
684         KKASSERT(madt_phyaddr != 0);
685
686         madt = sdt_sdth_map(madt_phyaddr);
687         KKASSERT(madt != NULL);
688
689         error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
690         if (error)
691                 panic("madt_ioapic_enumerate failed");
692
693         sdt_sdth_unmap(&madt->madt_hdr);
694 }
695
696 static struct ioapic_enumerator madt_ioapic_enumerator = {
697         .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
698         .ioapic_probe = madt_ioapic_probe,
699         .ioapic_enumerate = madt_ioapic_enumerate
700 };
701
702 static void
703 madt_ioapic_enum_register(void)
704 {
705         int prio;
706
707         prio = IOAPIC_ENUM_PRIO_MADT;
708         kgetenv_int("hw.madt_ioapic_prio", &prio);
709         madt_ioapic_enumerator.ioapic_prio = prio;
710
711         ioapic_enumerator_register(&madt_ioapic_enumerator);
712 }
713 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
714         madt_ioapic_enum_register, 0);