ioapic: Save I/O APIC information for later configuration
[dragonfly.git] / sys / platform / pc64 / x86_64 / mp_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/kernel.h>
37 #include <sys/systm.h>
38
39 #include <machine/pmap.h>
40 #include <machine/smp.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
43 #include <machine_base/apic/mpapic.h>
44
45 #define MADT_VPRINTF(fmt, arg...) \
46 do { \
47         if (bootverbose) \
48                 kprintf("ACPI MADT: " fmt , ##arg); \
49 } while (0)
50
51 #define ACPI_RSDP_EBDA_MAPSZ    1024
52 #define ACPI_RSDP_BIOS_MAPSZ    0x20000
53 #define ACPI_RSDP_BIOS_MAPADDR  0xe0000
54
55 #define ACPI_RSDP_ALIGN         16
56
57 #define ACPI_RSDP_SIGLEN        8
58 #define ACPI_RSDP_SIG           "RSD PTR "
59
60 #define ACPI_SDTH_SIGLEN        4
61 #define ACPI_RSDT_SIG           "RSDT"
62 #define ACPI_XSDT_SIG           "XSDT"
63 #define ACPI_MADT_SIG           "APIC"
64
65 /* Root System Description Pointer */
66 struct acpi_rsdp {
67         uint8_t                 rsdp_sig[ACPI_RSDP_SIGLEN];
68         uint8_t                 rsdp_cksum;
69         uint8_t                 rsdp_oem_id[6];
70         uint8_t                 rsdp_rev;
71         uint32_t                rsdp_rsdt;
72         uint32_t                rsdp_len;
73         uint64_t                rsdp_xsdt;
74         uint8_t                 rsdp_ext_cksum;
75         uint8_t                 rsdp_rsvd[3];
76 } __packed;
77
78 /* System Description Table Header */
79 struct acpi_sdth {
80         uint8_t                 sdth_sig[ACPI_SDTH_SIGLEN];
81         uint32_t                sdth_len;
82         uint8_t                 sdth_rev;
83         uint8_t                 sdth_cksum;
84         uint8_t                 sdth_oem_id[6];
85         uint8_t                 sdth_oem_tbid[8];
86         uint32_t                sdth_oem_rev;
87         uint32_t                sdth_crt_id;
88         uint32_t                sdth_crt_rev;
89 } __packed;
90
91 /* Extended System Description Table */
92 struct acpi_xsdt {
93         struct acpi_sdth        xsdt_hdr;
94         uint64_t                xsdt_ents[1];
95 } __packed;
96
97 /* Root System Description Table */
98 struct acpi_rsdt {
99         struct acpi_sdth        rsdt_hdr;
100         uint32_t                rsdt_ents[1];
101 } __packed;
102
103 /* Multiple APIC Description Table */
104 struct acpi_madt {
105         struct acpi_sdth        madt_hdr;
106         uint32_t                madt_lapic_addr;
107         uint32_t                madt_flags;
108         uint8_t                 madt_ents[1];
109 } __packed;
110
111 /* Common parts of MADT APIC structure */
112 struct acpi_madt_ent {
113         uint8_t                 me_type;        /* MADT_ENT_ */
114         uint8_t                 me_len;
115 } __packed;
116
117 #define MADT_ENT_LAPIC          0
118 #define MADT_ENT_IOAPIC         1
119 #define MADT_ENT_INTSRC         2
120 #define MADT_ENT_LAPIC_ADDR     5
121
122 /* MADT Processor Local APIC */
123 struct acpi_madt_lapic {
124         struct acpi_madt_ent    ml_hdr;
125         uint8_t                 ml_cpu_id;
126         uint8_t                 ml_apic_id;
127         uint32_t                ml_flags;       /* MADT_LAPIC_ */
128 } __packed;
129
130 #define MADT_LAPIC_ENABLED      0x1
131
132 /* MADT I/O APIC */
133 struct acpi_madt_ioapic {
134         struct acpi_madt_ent    mio_hdr;
135         uint8_t                 mio_apic_id;
136         uint8_t                 mio_reserved;
137         uint32_t                mio_addr;
138         uint32_t                mio_gsi_base;
139 } __packed;
140
141 /* MADT Interrupt Source Override */
142 struct acpi_madt_intsrc {
143         struct acpi_madt_ent    mint_hdr;
144         uint8_t                 mint_bus;       /* MADT_INT_BUS_ */
145         uint8_t                 mint_src;
146         uint32_t                mint_gsi;
147         uint16_t                mint_flags;     /* MADT_INT_ */
148 } __packed;
149
150 #define MADT_INT_BUS_ISA        0
151
152 #define MADT_INT_POLA_MASK      0x3
153 #define MADT_INT_POLA_SHIFT     0
154 #define MADT_INT_POLA_CONFORM   0
155 #define MADT_INT_POLA_HIGH      1
156 #define MADT_INT_POLA_RSVD      2
157 #define MADT_INT_POLA_LOW       3
158 #define MADT_INT_TRIG_MASK      0xc
159 #define MADT_INT_TRIG_SHIFT     2
160 #define MADT_INT_TRIG_CONFORM   0
161 #define MADT_INT_TRIG_EDGE      1
162 #define MADT_INT_TRIG_RSVD      2
163 #define MADT_INT_TRIG_LEVEL     3
164
165 /* MADT Local APIC Address Override */
166 struct acpi_madt_lapic_addr {
167         struct acpi_madt_ent    mla_hdr;
168         uint16_t                mla_reserved;
169         uint64_t                mla_lapic_addr;
170 } __packed;
171
172 typedef vm_paddr_t              (*madt_search_t)(vm_paddr_t);
173 typedef int                     (*madt_iter_t)(void *,
174                                     const struct acpi_madt_ent *);
175
176 static const struct acpi_rsdp   *madt_rsdp_search(const uint8_t *, int);
177 static void                     *madt_sdth_map(vm_paddr_t);
178 static void                     madt_sdth_unmap(struct acpi_sdth *);
179 static vm_paddr_t               madt_search_xsdt(vm_paddr_t);
180 static vm_paddr_t               madt_search_rsdt(vm_paddr_t);
181 static int                      madt_check(vm_paddr_t);
182 static int                      madt_iterate_entries(struct acpi_madt *,
183                                     madt_iter_t, void *);
184
185 static vm_offset_t              madt_lapic_pass1(void);
186 static int                      madt_lapic_pass2(int);
187
188 static void                     madt_lapic_enumerate(struct lapic_enumerator *);
189 static int                      madt_lapic_probe(struct lapic_enumerator *);
190
191 static void                     madt_ioapic_enumerate(
192                                     struct ioapic_enumerator *);
193 static int                      madt_ioapic_probe(struct ioapic_enumerator *);
194
195 extern u_long                   ebda_addr;
196
197 static vm_paddr_t               madt_phyaddr;
198
199 static void
200 madt_probe(void)
201 {
202         const struct acpi_rsdp *rsdp;
203         madt_search_t search;
204         vm_paddr_t search_paddr, madt_paddr;
205         vm_size_t mapsz;
206         uint8_t *ptr;
207
208         KKASSERT(madt_phyaddr == 0);
209
210         if (ebda_addr != 0) {
211                 mapsz = ACPI_RSDP_EBDA_MAPSZ;
212                 ptr = pmap_mapdev(ebda_addr, mapsz);
213
214                 rsdp = madt_rsdp_search(ptr, mapsz);
215                 if (rsdp == NULL) {
216                         MADT_VPRINTF("RSDP not in EBDA\n");
217                         pmap_unmapdev((vm_offset_t)ptr, mapsz);
218
219                         ptr = NULL;
220                         mapsz = 0;
221                 } else {
222                         MADT_VPRINTF("RSDP in EBDA\n");
223                         goto found_rsdp;
224                 }
225         }
226
227         mapsz = ACPI_RSDP_BIOS_MAPSZ;
228         ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
229
230         rsdp = madt_rsdp_search(ptr, mapsz);
231         if (rsdp == NULL) {
232                 kprintf("madt_probe: no RSDP\n");
233                 pmap_unmapdev((vm_offset_t)ptr, mapsz);
234                 return;
235         } else {
236                 MADT_VPRINTF("RSDP in BIOS mem\n");
237         }
238
239 found_rsdp:
240         if (rsdp->rsdp_rev != 2) {
241                 search_paddr = rsdp->rsdp_rsdt;
242                 search = madt_search_rsdt;
243         } else {
244                 search_paddr = rsdp->rsdp_xsdt;
245                 search = madt_search_xsdt;
246         }
247         pmap_unmapdev((vm_offset_t)ptr, mapsz);
248
249         madt_paddr = search(search_paddr);
250         if (madt_paddr == 0) {
251                 kprintf("madt_probe: can't locate MADT\n");
252                 return;
253         }
254
255         /* Preliminary checks */
256         if (madt_check(madt_paddr)) {
257                 kprintf("madt_probe: madt_check failed\n");
258                 return;
259         }
260
261         madt_phyaddr = madt_paddr;
262 }
263 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, madt_probe, 0);
264
265 static const struct acpi_rsdp *
266 madt_rsdp_search(const uint8_t *target, int size)
267 {
268         const struct acpi_rsdp *rsdp;
269         int i;
270
271         KKASSERT(size > sizeof(*rsdp));
272
273         for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
274                 rsdp = (const struct acpi_rsdp *)&target[i];
275                 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
276                            ACPI_RSDP_SIGLEN) == 0)
277                         return rsdp;
278         }
279         return NULL;
280 }
281
282 static void *
283 madt_sdth_map(vm_paddr_t paddr)
284 {
285         struct acpi_sdth *sdth;
286         vm_size_t mapsz;
287
288         sdth = pmap_mapdev(paddr, sizeof(*sdth));
289         mapsz = sdth->sdth_len;
290         pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
291
292         if (mapsz < sizeof(*sdth))
293                 return NULL;
294
295         return pmap_mapdev(paddr, mapsz);
296 }
297
298 static void
299 madt_sdth_unmap(struct acpi_sdth *sdth)
300 {
301         pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
302 }
303
304 static vm_paddr_t
305 madt_search_xsdt(vm_paddr_t xsdt_paddr)
306 {
307         struct acpi_xsdt *xsdt;
308         vm_paddr_t madt_paddr = 0;
309         int i, nent;
310
311         if (xsdt_paddr == 0) {
312                 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
313                 return 0;
314         }
315
316         xsdt = madt_sdth_map(xsdt_paddr);
317         if (xsdt == NULL) {
318                 kprintf("madt_search_xsdt: can't map XSDT\n");
319                 return 0;
320         }
321
322         if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
323                    ACPI_SDTH_SIGLEN) != 0) {
324                 kprintf("madt_search_xsdt: not XSDT\n");
325                 goto back;
326         }
327
328         if (xsdt->xsdt_hdr.sdth_rev != 1) {
329                 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
330                         xsdt->xsdt_hdr.sdth_rev);
331                 goto back;
332         }
333
334         nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
335                sizeof(xsdt->xsdt_ents[0]);
336         for (i = 0; i < nent; ++i) {
337                 struct acpi_sdth *sdth;
338
339                 if (xsdt->xsdt_ents[i] == 0)
340                         continue;
341
342                 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
343                 if (sdth != NULL) {
344                         int ret;
345
346                         ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
347                                      ACPI_SDTH_SIGLEN);
348                         madt_sdth_unmap(sdth);
349
350                         if (ret == 0) {
351                                 MADT_VPRINTF("MADT in XSDT\n");
352                                 madt_paddr = xsdt->xsdt_ents[i];
353                                 break;
354                         }
355                 }
356         }
357 back:
358         madt_sdth_unmap(&xsdt->xsdt_hdr);
359         return madt_paddr;
360 }
361
362 static vm_paddr_t
363 madt_search_rsdt(vm_paddr_t rsdt_paddr)
364 {
365         struct acpi_rsdt *rsdt;
366         vm_paddr_t madt_paddr = 0;
367         int i, nent;
368
369         if (rsdt_paddr == 0) {
370                 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
371                 return 0;
372         }
373
374         rsdt = madt_sdth_map(rsdt_paddr);
375         if (rsdt == NULL) {
376                 kprintf("madt_search_rsdt: can't map RSDT\n");
377                 return 0;
378         }
379
380         if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
381                    ACPI_SDTH_SIGLEN) != 0) {
382                 kprintf("madt_search_rsdt: not RSDT\n");
383                 goto back;
384         }
385
386         if (rsdt->rsdt_hdr.sdth_rev != 1) {
387                 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
388                         rsdt->rsdt_hdr.sdth_rev);
389                 goto back;
390         }
391
392         nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
393                sizeof(rsdt->rsdt_ents[0]);
394         for (i = 0; i < nent; ++i) {
395                 struct acpi_sdth *sdth;
396
397                 if (rsdt->rsdt_ents[i] == 0)
398                         continue;
399
400                 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
401                 if (sdth != NULL) {
402                         int ret;
403
404                         ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
405                                      ACPI_SDTH_SIGLEN);
406                         madt_sdth_unmap(sdth);
407
408                         if (ret == 0) {
409                                 MADT_VPRINTF("MADT in RSDT\n");
410                                 madt_paddr = rsdt->rsdt_ents[i];
411                                 break;
412                         }
413                 }
414         }
415 back:
416         madt_sdth_unmap(&rsdt->rsdt_hdr);
417         return madt_paddr;
418 }
419
420 static int
421 madt_check(vm_paddr_t madt_paddr)
422 {
423         struct acpi_madt *madt;
424         int error = 0;
425
426         KKASSERT(madt_paddr != 0);
427
428         madt = madt_sdth_map(madt_paddr);
429         KKASSERT(madt != NULL);
430
431         /*
432          * MADT in ACPI specification 1.0 - 4.0
433          */
434         if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
435                 kprintf("madt_check: unsupported MADT revision %d\n",
436                         madt->madt_hdr.sdth_rev);
437                 error = EOPNOTSUPP;
438                 goto back;
439         }
440
441         if (madt->madt_hdr.sdth_len <
442             sizeof(*madt) - sizeof(madt->madt_ents)) {
443                 kprintf("madt_check: invalid MADT length %u\n",
444                         madt->madt_hdr.sdth_len);
445                 error = EINVAL;
446                 goto back;
447         }
448 back:
449         madt_sdth_unmap(&madt->madt_hdr);
450         return error;
451 }
452
453 static int
454 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
455 {
456         int size, cur, error;
457
458         size = madt->madt_hdr.sdth_len -
459                (sizeof(*madt) - sizeof(madt->madt_ents));
460         cur = 0;
461         error = 0;
462
463         while (size - cur > sizeof(struct acpi_madt_ent)) {
464                 const struct acpi_madt_ent *ent;
465
466                 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
467                 if (ent->me_len < sizeof(*ent)) {
468                         kprintf("madt_iterate_entries: invalid MADT "
469                                 "entry len %d\n", ent->me_len);
470                         error = EINVAL;
471                         break;
472                 }
473                 if (ent->me_len > (size - cur)) {
474                         kprintf("madt_iterate_entries: invalid MADT "
475                                 "entry len %d, > table length\n", ent->me_len);
476                         error = EINVAL;
477                         break;
478                 }
479
480                 cur += ent->me_len;
481
482                 /*
483                  * Only Local APIC, I/O APIC and Interrupt Source Override
484                  * are defined in ACPI specification 1.0 - 4.0
485                  */
486                 switch (ent->me_type) {
487                 case MADT_ENT_LAPIC:
488                         if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
489                                 kprintf("madt_iterate_entries: invalid MADT "
490                                         "lapic entry len %d\n", ent->me_len);
491                                 error = EINVAL;
492                         }
493                         break;
494
495                 case MADT_ENT_IOAPIC:
496                         if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
497                                 kprintf("madt_iterate_entries: invalid MADT "
498                                         "ioapic entry len %d\n", ent->me_len);
499                                 error = EINVAL;
500                         }
501                         break;
502
503                 case MADT_ENT_INTSRC:
504                         if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
505                                 kprintf("madt_iterate_entries: invalid MADT "
506                                         "intsrc entry len %d\n",
507                                         ent->me_len);
508                                 error = EINVAL;
509                         }
510                         break;
511                 }
512                 if (error)
513                         break;
514
515                 error = func(arg, ent);
516                 if (error)
517                         break;
518         }
519         return error;
520 }
521
522 static int
523 madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
524 {
525         const struct acpi_madt_lapic_addr *lapic_addr_ent;
526         uint64_t *addr64 = xarg;
527
528         if (ent->me_type != MADT_ENT_LAPIC_ADDR)
529                 return 0;
530         if (ent->me_len < sizeof(*lapic_addr_ent)) {
531                 kprintf("madt_lapic_pass1: "
532                         "invalid LAPIC address override length\n");
533                 return 0;
534         }
535         lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
536
537         *addr64 = lapic_addr_ent->mla_lapic_addr;
538         return 0;
539 }
540
541 static vm_offset_t
542 madt_lapic_pass1(void)
543 {
544         struct acpi_madt *madt;
545         vm_offset_t lapic_addr;
546         uint64_t lapic_addr64;
547         int error;
548
549         KKASSERT(madt_phyaddr != 0);
550
551         madt = madt_sdth_map(madt_phyaddr);
552         KKASSERT(madt != NULL);
553
554         MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
555                      madt->madt_lapic_addr, madt->madt_flags);
556         lapic_addr = madt->madt_lapic_addr;
557
558         lapic_addr64 = 0;
559         error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
560                                      &lapic_addr64);
561         if (error)
562                 panic("madt_iterate_entries(pass1) failed\n");
563
564         if (lapic_addr64 != 0) {
565                 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
566                         lapic_addr64);
567                 lapic_addr = lapic_addr64;
568         }
569
570         madt_sdth_unmap(&madt->madt_hdr);
571
572         return lapic_addr;
573 }
574
575 struct madt_lapic_pass2_cbarg {
576         int     cpu;
577         int     bsp_found;
578         int     bsp_apic_id;
579 };
580
581 static int
582 madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
583 {
584         const struct acpi_madt_lapic *lapic_ent;
585         struct madt_lapic_pass2_cbarg *arg = xarg;
586
587         if (ent->me_type != MADT_ENT_LAPIC)
588                 return 0;
589
590         lapic_ent = (const struct acpi_madt_lapic *)ent;
591         if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
592                 MADT_VPRINTF("cpu id %d, apic id %d\n",
593                              lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
594                 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
595                         mp_set_cpuids(0, lapic_ent->ml_apic_id);
596                         arg->bsp_found = 1;
597                 } else {
598                         mp_set_cpuids(arg->cpu, lapic_ent->ml_apic_id);
599                         arg->cpu++;
600                 }
601         }
602         return 0;
603 }
604
605 static int
606 madt_lapic_pass2(int bsp_apic_id)
607 {
608         struct acpi_madt *madt;
609         struct madt_lapic_pass2_cbarg arg;
610         int error;
611
612         MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
613
614         KKASSERT(madt_phyaddr != 0);
615
616         madt = madt_sdth_map(madt_phyaddr);
617         KKASSERT(madt != NULL);
618
619         bzero(&arg, sizeof(arg));
620         arg.cpu = 1;
621         arg.bsp_apic_id = bsp_apic_id;
622
623         error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
624         if (error)
625                 panic("madt_iterate_entries(pass2) failed\n");
626
627         KKASSERT(arg.bsp_found);
628         KKASSERT(arg.cpu > 1);
629         mp_naps = arg.cpu - 1; /* exclude BSP */
630
631         madt_sdth_unmap(&madt->madt_hdr);
632
633         return 0;
634 }
635
636 struct madt_lapic_probe_cbarg {
637         int             cpu_count;
638         vm_offset_t     lapic_addr;
639 };
640
641 static int
642 madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
643 {
644         struct madt_lapic_probe_cbarg *arg = xarg;
645
646         if (ent->me_type == MADT_ENT_LAPIC) {
647                 const struct acpi_madt_lapic *lapic_ent;
648
649                 lapic_ent = (const struct acpi_madt_lapic *)ent;
650                 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
651                         arg->cpu_count++;
652         } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
653                 const struct acpi_madt_lapic_addr *lapic_addr_ent;
654
655                 if (ent->me_len < sizeof(*lapic_addr_ent)) {
656                         kprintf("madt_lapic_probe: "
657                                 "invalid LAPIC address override length\n");
658                         return 0;
659                 }
660                 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
661
662                 if (lapic_addr_ent->mla_lapic_addr != 0)
663                         arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
664         }
665         return 0;
666 }
667
668 static int
669 madt_lapic_probe(struct lapic_enumerator *e)
670 {
671         struct madt_lapic_probe_cbarg arg;
672         struct acpi_madt *madt;
673         int error;
674
675         if (madt_phyaddr == 0)
676                 return ENXIO;
677
678         madt = madt_sdth_map(madt_phyaddr);
679         KKASSERT(madt != NULL);
680
681         bzero(&arg, sizeof(arg));
682         arg.lapic_addr = madt->madt_lapic_addr;
683
684         error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
685         if (!error) {
686                 if (arg.cpu_count <= 1) {
687                         kprintf("madt_lapic_probe: "
688                                 "less than 2 CPUs is found\n");
689                         error = EOPNOTSUPP;
690                 }
691                 if (arg.lapic_addr == 0) {
692                         kprintf("madt_lapic_probe: zero LAPIC address\n");
693                         error = EOPNOTSUPP;
694                 }
695         }
696
697         madt_sdth_unmap(&madt->madt_hdr);
698         return error;
699 }
700
701 static void
702 madt_lapic_enumerate(struct lapic_enumerator *e)
703 {
704         vm_offset_t lapic_addr;
705         int bsp_apic_id;
706
707         KKASSERT(madt_phyaddr != 0);
708
709         lapic_addr = madt_lapic_pass1();
710         if (lapic_addr == 0)
711                 panic("madt_lapic_enumerate: no local apic\n");
712
713         lapic_map(lapic_addr);
714
715         bsp_apic_id = APIC_ID(lapic->id);
716         if (madt_lapic_pass2(bsp_apic_id))
717                 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
718 }
719
720 static struct lapic_enumerator  madt_lapic_enumerator = {
721         .lapic_prio = LAPIC_ENUM_PRIO_MADT,
722         .lapic_probe = madt_lapic_probe,
723         .lapic_enumerate = madt_lapic_enumerate
724 };
725
726 static void
727 madt_lapic_enum_register(void)
728 {
729         int prio;
730
731         prio = LAPIC_ENUM_PRIO_MADT;
732         kgetenv_int("hw.madt_lapic_prio", &prio);
733         madt_lapic_enumerator.lapic_prio = prio;
734
735         lapic_enumerator_register(&madt_lapic_enumerator);
736 }
737 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
738
739 struct madt_ioapic_probe_cbarg {
740         int     ioapic_cnt;
741         int     gsi_base0;
742 };
743
744 static int
745 madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
746 {
747         struct madt_ioapic_probe_cbarg *arg = xarg;
748
749         if (ent->me_type == MADT_ENT_INTSRC) {
750                 const struct acpi_madt_intsrc *intsrc_ent;
751                 int trig, pola;
752
753                 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
754
755                 /* XXX magic number */
756                 if (intsrc_ent->mint_src >= 16) {
757                         kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
758                                 intsrc_ent->mint_src);
759                         return EINVAL;
760                 }
761
762                 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi) {
763                         kprintf("ACPI MADT: warning intsrc irq %d "
764                                 "no gsi change\n", intsrc_ent->mint_src);
765                 }
766
767                 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
768                         kprintf("ACPI MADT: warning intsrc irq %d "
769                                 "bus is not ISA (%d)\n",
770                                 intsrc_ent->mint_src, intsrc_ent->mint_bus);
771                 }
772
773                 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
774                        MADT_INT_TRIG_SHIFT;
775                 if (trig != MADT_INT_TRIG_EDGE &&
776                     trig != MADT_INT_TRIG_CONFORM) {
777                         kprintf("ACPI MADT: warning invalid intsrc irq %d "
778                                 "trig (%d)\n", intsrc_ent->mint_src, trig);
779                 }
780
781                 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
782                        MADT_INT_POLA_SHIFT;
783                 if (pola != MADT_INT_POLA_HIGH &&
784                     pola != MADT_INT_POLA_CONFORM) {
785                         kprintf("ACPI MADT: warning invalid intsrc irq %d "
786                                 "pola (%d)\n", intsrc_ent->mint_src, pola);
787                 }
788         } else if (ent->me_type == MADT_ENT_IOAPIC) {
789                 const struct acpi_madt_ioapic *ioapic_ent;
790
791                 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
792                 if (ioapic_ent->mio_addr == 0) {
793                         kprintf("madt_ioapic_probe: zero IOAPIC address\n");
794                         return EINVAL;
795                 }
796
797                 arg->ioapic_cnt++;
798                 if (ioapic_ent->mio_gsi_base == 0)
799                         arg->gsi_base0 = 1;
800         }
801         return 0;
802 }
803
804 static int
805 madt_ioapic_probe(struct ioapic_enumerator *e)
806 {
807         struct madt_ioapic_probe_cbarg arg;
808         struct acpi_madt *madt;
809         int error;
810
811         if (madt_phyaddr == 0)
812                 return ENXIO;
813
814         madt = madt_sdth_map(madt_phyaddr);
815         KKASSERT(madt != NULL);
816
817         bzero(&arg, sizeof(arg));
818
819         error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
820         if (!error) {
821                 if (arg.ioapic_cnt == 0) {
822                         kprintf("madt_ioapic_probe: no IOAPIC\n");
823                         error = ENXIO;
824                 }
825                 if (!arg.gsi_base0) {
826                         kprintf("madt_ioapic_probe: no GSI base 0\n");
827                         error = EINVAL;
828                 }
829         }
830
831         madt_sdth_unmap(&madt->madt_hdr);
832         return error;
833 }
834
835 static int
836 madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
837 {
838         if (ent->me_type == MADT_ENT_INTSRC) {
839                 const struct acpi_madt_intsrc *intsrc_ent;
840
841                 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
842                 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi)
843                         return 0;
844
845                 MADT_VPRINTF("INTSRC irq %d -> gsi %u\n",
846                              intsrc_ent->mint_src, intsrc_ent->mint_gsi);
847         } else if (ent->me_type == MADT_ENT_IOAPIC) {
848                 const struct acpi_madt_ioapic *ioapic_ent;
849
850                 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
851                 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
852                              ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
853                              ioapic_ent->mio_gsi_base);
854
855                 if (!ioapic_use_old) {
856                         uint32_t ver;
857                         void *addr;
858                         int npin;
859
860                         addr = ioapic_map(ioapic_ent->mio_addr);
861
862                         ver = ioapic_read(addr, IOAPIC_VER);
863                         npin = ((ver & IOART_VER_MAXREDIR) >>
864                                 MAXREDIRSHIFT) + 1;
865
866                         ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
867                 }
868         }
869         return 0;
870 }
871
872 static void
873 madt_ioapic_enumerate(struct ioapic_enumerator *e)
874 {
875         struct acpi_madt *madt;
876         int error;
877
878         KKASSERT(madt_phyaddr != 0);
879
880         madt = madt_sdth_map(madt_phyaddr);
881         KKASSERT(madt != NULL);
882
883         error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
884         if (error)
885                 panic("madt_ioapic_enumerate failed\n");
886
887         madt_sdth_unmap(&madt->madt_hdr);
888 }
889
890 static struct ioapic_enumerator madt_ioapic_enumerator = {
891         .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
892         .ioapic_probe = madt_ioapic_probe,
893         .ioapic_enumerate = madt_ioapic_enumerate
894 };
895
896 static void
897 madt_ioapic_enum_register(void)
898 {
899         int prio;
900
901         prio = IOAPIC_ENUM_PRIO_MADT;
902         kgetenv_int("hw.madt_ioapic_prio", &prio);
903         madt_ioapic_enumerator.ioapic_prio = prio;
904
905         ioapic_enumerator_register(&madt_ioapic_enumerator);
906 }
907 SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
908         madt_ioapic_enum_register, 0);