1432c32b2bbef6e4280b1d57bcba1e1f43d25edf
[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/apic/lapic.h>
41 #include <machine_base/apic/ioapic.h>
42
43 #include "acpi_sdt.h"
44 #include "acpi_sdt_var.h"
45
46 extern int naps;
47
48 #define MADT_VPRINTF(fmt, arg...) \
49 do { \
50         if (bootverbose) \
51                 kprintf("ACPI MADT: " fmt , ##arg); \
52 } while (0)
53
54 /* Multiple APIC Description Table */
55 struct acpi_madt {
56         struct acpi_sdth        madt_hdr;
57         uint32_t                madt_lapic_addr;
58         uint32_t                madt_flags;
59         uint8_t                 madt_ents[1];
60 } __packed;
61
62 /* Common parts of MADT APIC structure */
63 struct acpi_madt_ent {
64         uint8_t                 me_type;        /* MADT_ENT_ */
65         uint8_t                 me_len;
66 } __packed;
67
68 #define MADT_ENT_LAPIC          0
69 #define MADT_ENT_IOAPIC         1
70 #define MADT_ENT_INTSRC         2
71 #define MADT_ENT_LAPIC_ADDR     5
72
73 /* MADT Processor Local APIC */
74 struct acpi_madt_lapic {
75         struct acpi_madt_ent    ml_hdr;
76         uint8_t                 ml_cpu_id;
77         uint8_t                 ml_apic_id;
78         uint32_t                ml_flags;       /* MADT_LAPIC_ */
79 } __packed;
80
81 #define MADT_LAPIC_ENABLED      0x1
82
83 /* MADT I/O APIC */
84 struct acpi_madt_ioapic {
85         struct acpi_madt_ent    mio_hdr;
86         uint8_t                 mio_apic_id;
87         uint8_t                 mio_reserved;
88         uint32_t                mio_addr;
89         uint32_t                mio_gsi_base;
90 } __packed;
91
92 /* MADT Interrupt Source Override */
93 struct acpi_madt_intsrc {
94         struct acpi_madt_ent    mint_hdr;
95         uint8_t                 mint_bus;       /* MADT_INT_BUS_ */
96         uint8_t                 mint_src;
97         uint32_t                mint_gsi;
98         uint16_t                mint_flags;     /* MADT_INT_ */
99 } __packed;
100
101 #define MADT_INT_BUS_ISA        0
102
103 #define MADT_INT_POLA_MASK      0x3
104 #define MADT_INT_POLA_SHIFT     0
105 #define MADT_INT_POLA_CONFORM   0
106 #define MADT_INT_POLA_HIGH      1
107 #define MADT_INT_POLA_RSVD      2
108 #define MADT_INT_POLA_LOW       3
109 #define MADT_INT_TRIG_MASK      0xc
110 #define MADT_INT_TRIG_SHIFT     2
111 #define MADT_INT_TRIG_CONFORM   0
112 #define MADT_INT_TRIG_EDGE      1
113 #define MADT_INT_TRIG_RSVD      2
114 #define MADT_INT_TRIG_LEVEL     3
115
116 /* MADT Local APIC Address Override */
117 struct acpi_madt_lapic_addr {
118         struct acpi_madt_ent    mla_hdr;
119         uint16_t                mla_reserved;
120         uint64_t                mla_lapic_addr;
121 } __packed;
122
123 typedef int                     (*madt_iter_t)(void *,
124                                     const struct acpi_madt_ent *);
125
126 static int                      madt_check(vm_paddr_t);
127 static int                      madt_iterate_entries(struct acpi_madt *,
128                                     madt_iter_t, void *);
129
130 static vm_offset_t              madt_lapic_pass1(void);
131 static int                      madt_lapic_pass2(int);
132
133 static void                     madt_lapic_enumerate(struct lapic_enumerator *);
134 static int                      madt_lapic_probe(struct lapic_enumerator *);
135
136 static void                     madt_ioapic_enumerate(
137                                     struct ioapic_enumerator *);
138 static int                      madt_ioapic_probe(struct ioapic_enumerator *);
139
140 static vm_paddr_t               madt_phyaddr;
141
142 static void
143 madt_probe(void)
144 {
145         vm_paddr_t madt_paddr;
146
147         KKASSERT(madt_phyaddr == 0);
148
149         madt_paddr = sdt_search(ACPI_MADT_SIG);
150         if (madt_paddr == 0) {
151                 kprintf("madt_probe: can't locate MADT\n");
152                 return;
153         }
154
155         /* Preliminary checks */
156         if (madt_check(madt_paddr)) {
157                 kprintf("madt_probe: madt_check failed\n");
158                 return;
159         }
160
161         madt_phyaddr = madt_paddr;
162 }
163 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);
164
165 static int
166 madt_check(vm_paddr_t madt_paddr)
167 {
168         struct acpi_madt *madt;
169         int error = 0;
170
171         KKASSERT(madt_paddr != 0);
172
173         madt = sdt_sdth_map(madt_paddr);
174         KKASSERT(madt != NULL);
175
176         /*
177          * MADT in ACPI specification 1.0 - 4.0
178          */
179         if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
180                 kprintf("madt_check: unsupported MADT revision %d\n",
181                         madt->madt_hdr.sdth_rev);
182                 error = EOPNOTSUPP;
183                 goto back;
184         }
185
186         if (madt->madt_hdr.sdth_len <
187             sizeof(*madt) - sizeof(madt->madt_ents)) {
188                 kprintf("madt_check: invalid MADT length %u\n",
189                         madt->madt_hdr.sdth_len);
190                 error = EINVAL;
191                 goto back;
192         }
193 back:
194         sdt_sdth_unmap(&madt->madt_hdr);
195         return error;
196 }
197
198 static int
199 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
200 {
201         int size, cur, error;
202
203         size = madt->madt_hdr.sdth_len -
204                (sizeof(*madt) - sizeof(madt->madt_ents));
205         cur = 0;
206         error = 0;
207
208         while (size - cur > sizeof(struct acpi_madt_ent)) {
209                 const struct acpi_madt_ent *ent;
210
211                 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
212                 if (ent->me_len < sizeof(*ent)) {
213                         kprintf("madt_iterate_entries: invalid MADT "
214                                 "entry len %d\n", ent->me_len);
215                         error = EINVAL;
216                         break;
217                 }
218                 if (ent->me_len > (size - cur)) {
219                         kprintf("madt_iterate_entries: invalid MADT "
220                                 "entry len %d, > table length\n", ent->me_len);
221                         error = EINVAL;
222                         break;
223                 }
224
225                 cur += ent->me_len;
226
227                 /*
228                  * Only Local APIC, I/O APIC and Interrupt Source Override
229                  * are defined in ACPI specification 1.0 - 4.0
230                  */
231                 switch (ent->me_type) {
232                 case MADT_ENT_LAPIC:
233                         if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
234                                 kprintf("madt_iterate_entries: invalid MADT "
235                                         "lapic entry len %d\n", ent->me_len);
236                                 error = EINVAL;
237                         }
238                         break;
239
240                 case MADT_ENT_IOAPIC:
241                         if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
242                                 kprintf("madt_iterate_entries: invalid MADT "
243                                         "ioapic entry len %d\n", ent->me_len);
244                                 error = EINVAL;
245                         }
246                         break;
247
248                 case MADT_ENT_INTSRC:
249                         if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
250                                 kprintf("madt_iterate_entries: invalid MADT "
251                                         "intsrc entry len %d\n",
252                                         ent->me_len);
253                                 error = EINVAL;
254                         }
255                         break;
256                 }
257                 if (error)
258                         break;
259
260                 error = func(arg, ent);
261                 if (error)
262                         break;
263         }
264         return error;
265 }
266
267 static int
268 madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
269 {
270         const struct acpi_madt_lapic_addr *lapic_addr_ent;
271         uint64_t *addr64 = xarg;
272
273         if (ent->me_type != MADT_ENT_LAPIC_ADDR)
274                 return 0;
275         if (ent->me_len < sizeof(*lapic_addr_ent)) {
276                 kprintf("madt_lapic_pass1: "
277                         "invalid LAPIC address override length\n");
278                 return 0;
279         }
280         lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
281
282         *addr64 = lapic_addr_ent->mla_lapic_addr;
283         return 0;
284 }
285
286 static vm_offset_t
287 madt_lapic_pass1(void)
288 {
289         struct acpi_madt *madt;
290         vm_offset_t lapic_addr;
291         uint64_t lapic_addr64;
292         int error;
293
294         KKASSERT(madt_phyaddr != 0);
295
296         madt = sdt_sdth_map(madt_phyaddr);
297         KKASSERT(madt != NULL);
298
299         MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
300                      madt->madt_lapic_addr, madt->madt_flags);
301         lapic_addr = madt->madt_lapic_addr;
302
303         lapic_addr64 = 0;
304         error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
305                                      &lapic_addr64);
306         if (error)
307                 panic("madt_iterate_entries(pass1) failed\n");
308
309         if (lapic_addr64 != 0) {
310                 kprintf("ACPI MADT: warning 64bits lapic address 0x%llx\n",
311                         lapic_addr64);
312                 /* XXX vm_offset_t is 32bits on i386 */
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_offset_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_offset_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                 /* XXX magic number */
500                 if (intsrc_ent->mint_src >= 16) {
501                         kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
502                                 intsrc_ent->mint_src);
503                         return EINVAL;
504                 }
505
506                 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
507                         kprintf("ACPI MADT: warning intsrc irq %d "
508                                 "bus is not ISA (%d)\n",
509                                 intsrc_ent->mint_src, intsrc_ent->mint_bus);
510                 }
511
512                 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
513                        MADT_INT_TRIG_SHIFT;
514                 if (trig == MADT_INT_TRIG_RSVD) {
515                         kprintf("ACPI MADT: warning invalid intsrc irq %d "
516                                 "trig, reserved\n", intsrc_ent->mint_src);
517                 } else if (trig == MADT_INT_TRIG_LEVEL) {
518                         MADT_VPRINTF("warning invalid intsrc irq %d "
519                             "trig, level\n", intsrc_ent->mint_src);
520                 }
521
522                 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
523                        MADT_INT_POLA_SHIFT;
524                 if (pola == MADT_INT_POLA_RSVD) {
525                         kprintf("ACPI MADT: warning invalid intsrc irq %d "
526                                 "pola, reserved\n", intsrc_ent->mint_src);
527                 } else if (pola == MADT_INT_POLA_LOW) {
528                         MADT_VPRINTF("warning invalid intsrc irq %d "
529                             "pola, low\n", intsrc_ent->mint_src);
530                 }
531         } else if (ent->me_type == MADT_ENT_IOAPIC) {
532                 const struct acpi_madt_ioapic *ioapic_ent;
533
534                 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
535                 if (ioapic_ent->mio_addr == 0) {
536                         kprintf("madt_ioapic_probe: zero IOAPIC address\n");
537                         return EINVAL;
538                 }
539
540                 arg->ioapic_cnt++;
541                 if (ioapic_ent->mio_gsi_base == 0)
542                         arg->gsi_base0 = 1;
543         }
544         return 0;
545 }
546
547 static int
548 madt_ioapic_probe(struct ioapic_enumerator *e)
549 {
550         struct madt_ioapic_probe_cbarg arg;
551         struct acpi_madt *madt;
552         int error;
553
554         if (madt_phyaddr == 0)
555                 return ENXIO;
556
557         madt = sdt_sdth_map(madt_phyaddr);
558         KKASSERT(madt != NULL);
559
560         bzero(&arg, sizeof(arg));
561
562         error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
563         if (!error) {
564                 if (arg.ioapic_cnt == 0) {
565                         kprintf("madt_ioapic_probe: no IOAPIC\n");
566                         error = ENXIO;
567                 }
568                 if (!arg.gsi_base0) {
569                         kprintf("madt_ioapic_probe: no GSI base 0\n");
570                         error = EINVAL;
571                 }
572         }
573
574         sdt_sdth_unmap(&madt->madt_hdr);
575         return error;
576 }
577
578 static int
579 madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
580 {
581         if (ent->me_type == MADT_ENT_INTSRC) {
582                 const struct acpi_madt_intsrc *intsrc_ent;
583                 enum intr_trigger trig;
584                 enum intr_polarity pola;
585 #ifdef foo
586                 int ent_trig, ent_pola;
587 #endif
588
589                 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
590
591                 KKASSERT(intsrc_ent->mint_src < 16);
592                 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
593                         return 0;
594
595 #ifdef foo
596                 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
597                     MADT_INT_TRIG_SHIFT;
598                 if (ent_trig == MADT_INT_TRIG_RSVD)
599                         return 0;
600                 else if (ent_trig == MADT_INT_TRIG_LEVEL)
601                         trig = INTR_TRIGGER_LEVEL;
602                 else
603                         trig = INTR_TRIGGER_EDGE;
604
605                 ent_pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
606                     MADT_INT_POLA_SHIFT;
607                 if (ent_pola == MADT_INT_POLA_RSVD)
608                         return 0;
609                 else if (ent_pola == MADT_INT_POLA_LOW)
610                         pola = INTR_POLARITY_LOW;
611                 else
612                         pola = INTR_POLARITY_HIGH;
613 #else
614                 /*
615                  * We ignore the polarity and trigger changes, since
616                  * most of them are wrong or useless at best.
617                  */
618                 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi) {
619                         /* Nothing changed */
620                         return 0;
621                 }
622                 trig = INTR_TRIGGER_EDGE;
623                 pola = INTR_POLARITY_HIGH;
624 #endif
625
626                 MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
627                              intsrc_ent->mint_src, intsrc_ent->mint_gsi,
628                              intr_str_trigger(trig), intr_str_polarity(pola));
629                 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
630                               trig, pola);
631         } else if (ent->me_type == MADT_ENT_IOAPIC) {
632                 const struct acpi_madt_ioapic *ioapic_ent;
633                 uint32_t ver;
634                 void *addr;
635                 int npin;
636
637                 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
638                 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
639                              ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
640                              ioapic_ent->mio_gsi_base);
641
642                 addr = ioapic_map(ioapic_ent->mio_addr);
643
644                 ver = ioapic_read(addr, IOAPIC_VER);
645                 npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
646
647                 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
648         }
649         return 0;
650 }
651
652 static void
653 madt_ioapic_enumerate(struct ioapic_enumerator *e)
654 {
655         struct acpi_madt *madt;
656         int error;
657
658         KKASSERT(madt_phyaddr != 0);
659
660         madt = sdt_sdth_map(madt_phyaddr);
661         KKASSERT(madt != NULL);
662
663         error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
664         if (error)
665                 panic("madt_ioapic_enumerate failed\n");
666
667         sdt_sdth_unmap(&madt->madt_hdr);
668 }
669
670 static struct ioapic_enumerator madt_ioapic_enumerator = {
671         .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
672         .ioapic_probe = madt_ioapic_probe,
673         .ioapic_enumerate = madt_ioapic_enumerate
674 };
675
676 static void
677 madt_ioapic_enum_register(void)
678 {
679         int prio;
680
681         prio = IOAPIC_ENUM_PRIO_MADT;
682         kgetenv_int("hw.madt_ioapic_prio", &prio);
683         madt_ioapic_enumerator.ioapic_prio = prio;
684
685         ioapic_enumerator_register(&madt_ioapic_enumerator);
686 }
687 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
688         madt_ioapic_enum_register, 0);