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