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