apic/i386: Wait APs's LAPIC to be initialized before programming I/O APIC
[dragonfly.git] / sys / platform / pc64 / x86_64 / mp_madt.c
CommitLineData
8e4c6923
MN
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>
91903a05 36#include <sys/kernel.h>
8e4c6923
MN
37#include <sys/systm.h>
38
39#include <machine/pmap.h>
40#include <machine/smp.h>
ee5932ac
MN
41#include <machine/md_var.h>
42#include <machine/specialreg.h>
91903a05 43#include <machine_base/apic/mpapic.h>
8e4c6923 44
c87b587d
MN
45#define MADT_VPRINTF(fmt, arg...) \
46do { \
47 if (bootverbose) \
48 kprintf("ACPI MADT: " fmt , ##arg); \
49} while (0)
50
8e4c6923
MN
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 */
66struct 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 */
79struct 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 */
92struct acpi_xsdt {
93 struct acpi_sdth xsdt_hdr;
94 uint64_t xsdt_ents[1];
95} __packed;
96
97/* Root System Description Table */
98struct acpi_rsdt {
99 struct acpi_sdth rsdt_hdr;
100 uint32_t rsdt_ents[1];
101} __packed;
102
103/* Multiple APIC Description Table */
104struct 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 */
112struct acpi_madt_ent {
113 uint8_t me_type; /* MADT_ENT_ */
114 uint8_t me_len;
115} __packed;
116
117#define MADT_ENT_LAPIC 0
0492bd82 118#define MADT_ENT_IOAPIC 1
65b2387f 119#define MADT_ENT_INTSRC 2
ebbde674 120#define MADT_ENT_LAPIC_ADDR 5
8e4c6923
MN
121
122/* MADT Processor Local APIC */
123struct 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
0492bd82
MN
132/* MADT I/O APIC */
133struct 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
2bbbec42 141/* MADT Interrupt Source Override */
922e4bcb
SZ
142struct acpi_madt_intsrc {
143 struct acpi_madt_ent mint_hdr;
65b2387f 144 uint8_t mint_bus; /* MADT_INT_BUS_ */
922e4bcb
SZ
145 uint8_t mint_src;
146 uint32_t mint_gsi;
147 uint16_t mint_flags; /* MADT_INT_ */
2bbbec42
SZ
148} __packed;
149
65b2387f
SZ
150#define MADT_INT_BUS_ISA 0
151
922e4bcb
SZ
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
2bbbec42 164
ebbde674
MN
165/* MADT Local APIC Address Override */
166struct 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
8e4c6923 172typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
0492bd82
MN
173typedef int (*madt_iter_t)(void *,
174 const struct acpi_madt_ent *);
8e4c6923
MN
175
176static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
177static void *madt_sdth_map(vm_paddr_t);
178static void madt_sdth_unmap(struct acpi_sdth *);
179static vm_paddr_t madt_search_xsdt(vm_paddr_t);
180static vm_paddr_t madt_search_rsdt(vm_paddr_t);
4ad5917f 181static int madt_check(vm_paddr_t);
0492bd82
MN
182static int madt_iterate_entries(struct acpi_madt *,
183 madt_iter_t, void *);
8e4c6923 184
27b59776
SZ
185static vm_offset_t madt_lapic_pass1(void);
186static int madt_lapic_pass2(int);
91903a05
MN
187
188static void madt_lapic_enumerate(struct lapic_enumerator *);
189static int madt_lapic_probe(struct lapic_enumerator *);
190
65b2387f
SZ
191static void madt_ioapic_enumerate(
192 struct ioapic_enumerator *);
193static int madt_ioapic_probe(struct ioapic_enumerator *);
194
195extern u_long ebda_addr;
8e4c6923 196
86120698
SZ
197static vm_paddr_t madt_phyaddr;
198
199static void
8e4c6923
MN
200madt_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
86120698
SZ
208 KKASSERT(madt_phyaddr == 0);
209
8e4c6923
MN
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) {
c87b587d 216 MADT_VPRINTF("RSDP not in EBDA\n");
8e4c6923
MN
217 pmap_unmapdev((vm_offset_t)ptr, mapsz);
218
219 ptr = NULL;
220 mapsz = 0;
221 } else {
c87b587d 222 MADT_VPRINTF("RSDP in EBDA\n");
8e4c6923
MN
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);
86120698 234 return;
8e4c6923 235 } else {
c87b587d 236 MADT_VPRINTF("RSDP in BIOS mem\n");
8e4c6923
MN
237 }
238
239found_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");
86120698 252 return;
8e4c6923 253 }
4ad5917f
MN
254
255 /* Preliminary checks */
86120698
SZ
256 if (madt_check(madt_paddr)) {
257 kprintf("madt_probe: madt_check failed\n");
258 return;
259 }
260
261 madt_phyaddr = madt_paddr;
8e4c6923 262}
86120698 263SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, madt_probe, 0);
8e4c6923
MN
264
265static const struct acpi_rsdp *
266madt_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
282static void *
283madt_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
298static void
299madt_sdth_unmap(struct acpi_sdth *sdth)
300{
301 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
302}
303
304static vm_paddr_t
305madt_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) {
c87b587d 351 MADT_VPRINTF("MADT in XSDT\n");
8e4c6923
MN
352 madt_paddr = xsdt->xsdt_ents[i];
353 break;
354 }
355 }
356 }
357back:
358 madt_sdth_unmap(&xsdt->xsdt_hdr);
359 return madt_paddr;
360}
361
362static vm_paddr_t
363madt_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) {
c87b587d 409 MADT_VPRINTF("MADT in RSDT\n");
8e4c6923
MN
410 madt_paddr = rsdt->rsdt_ents[i];
411 break;
412 }
413 }
414 }
415back:
416 madt_sdth_unmap(&rsdt->rsdt_hdr);
417 return madt_paddr;
418}
419
ebbde674 420static int
27b59776
SZ
421madt_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
7d460393
SZ
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) {
27b59776
SZ
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 }
448back:
449 madt_sdth_unmap(&madt->madt_hdr);
450 return error;
451}
452
453static int
454madt_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
65b2387f 503 case MADT_ENT_INTSRC:
922e4bcb 504 if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
27b59776 505 kprintf("madt_iterate_entries: invalid MADT "
922e4bcb 506 "intsrc entry len %d\n",
27b59776
SZ
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
522static int
523madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
ebbde674
MN
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)) {
27b59776
SZ
531 kprintf("madt_lapic_pass1: "
532 "invalid LAPIC address override length\n");
ebbde674
MN
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
91903a05 541static vm_offset_t
27b59776 542madt_lapic_pass1(void)
8e4c6923
MN
543{
544 struct acpi_madt *madt;
4ad5917f 545 vm_offset_t lapic_addr;
ebbde674
MN
546 uint64_t lapic_addr64;
547 int error;
8e4c6923 548
86120698 549 KKASSERT(madt_phyaddr != 0);
8e4c6923 550
86120698 551 madt = madt_sdth_map(madt_phyaddr);
8e4c6923
MN
552 KKASSERT(madt != NULL);
553
c87b587d
MN
554 MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
555 madt->madt_lapic_addr, madt->madt_flags);
4ad5917f
MN
556 lapic_addr = madt->madt_lapic_addr;
557
ebbde674 558 lapic_addr64 = 0;
27b59776
SZ
559 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
560 &lapic_addr64);
ebbde674
MN
561 if (error)
562 panic("madt_iterate_entries(pass1) failed\n");
563
564 if (lapic_addr64 != 0) {
cb31590d
SZ
565 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
566 lapic_addr64);
ebbde674
MN
567 lapic_addr = lapic_addr64;
568 }
569
4ad5917f
MN
570 madt_sdth_unmap(&madt->madt_hdr);
571
572 return lapic_addr;
573}
574
27b59776 575struct madt_lapic_pass2_cbarg {
2c394345
MN
576 int cpu;
577 int bsp_found;
578 int bsp_apic_id;
579};
580
581static int
27b59776 582madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
2c394345
MN
583{
584 const struct acpi_madt_lapic *lapic_ent;
27b59776 585 struct madt_lapic_pass2_cbarg *arg = xarg;
2c394345
MN
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) {
65b2387f 592 MADT_VPRINTF("cpu id %d, apic id %d\n",
c87b587d 593 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
2c394345
MN
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
91903a05 605static int
27b59776 606madt_lapic_pass2(int bsp_apic_id)
4ad5917f
MN
607{
608 struct acpi_madt *madt;
27b59776 609 struct madt_lapic_pass2_cbarg arg;
2c394345 610 int error;
4ad5917f 611
c87b587d 612 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
4ad5917f 613
86120698 614 KKASSERT(madt_phyaddr != 0);
4ad5917f 615
86120698 616 madt = madt_sdth_map(madt_phyaddr);
4ad5917f 617 KKASSERT(madt != NULL);
8e4c6923 618
2c394345
MN
619 bzero(&arg, sizeof(arg));
620 arg.cpu = 1;
621 arg.bsp_apic_id = bsp_apic_id;
8e4c6923 622
27b59776 623 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
2c394345
MN
624 if (error)
625 panic("madt_iterate_entries(pass2) failed\n");
8e4c6923 626
2c394345
MN
627 KKASSERT(arg.bsp_found);
628 KKASSERT(arg.cpu > 1);
629 mp_naps = arg.cpu - 1; /* exclude BSP */
4ad5917f
MN
630
631 madt_sdth_unmap(&madt->madt_hdr);
2c394345
MN
632
633 return 0;
4ad5917f
MN
634}
635
86120698
SZ
636struct madt_lapic_probe_cbarg {
637 int cpu_count;
638 vm_offset_t lapic_addr;
639};
640
641static int
642madt_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
91903a05
MN
668static int
669madt_lapic_probe(struct lapic_enumerator *e)
670{
86120698
SZ
671 struct madt_lapic_probe_cbarg arg;
672 struct acpi_madt *madt;
673 int error;
91903a05 674
86120698 675 if (madt_phyaddr == 0)
91903a05
MN
676 return ENXIO;
677
86120698
SZ
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;
91903a05
MN
699}
700
701static void
702madt_lapic_enumerate(struct lapic_enumerator *e)
703{
91903a05
MN
704 vm_offset_t lapic_addr;
705 int bsp_apic_id;
706
86120698 707 KKASSERT(madt_phyaddr != 0);
91903a05 708
27b59776 709 lapic_addr = madt_lapic_pass1();
91903a05 710 if (lapic_addr == 0)
86120698 711 panic("madt_lapic_enumerate: no local apic\n");
91903a05 712
b44f1d28 713 lapic_map(lapic_addr);
1c1f402e
MN
714
715 bsp_apic_id = APIC_ID(lapic->id);
27b59776
SZ
716 if (madt_lapic_pass2(bsp_apic_id))
717 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
91903a05
MN
718}
719
86120698
SZ
720static 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
91903a05
MN
724};
725
726static void
27b59776 727madt_lapic_enum_register(void)
91903a05
MN
728{
729 int prio;
730
731 prio = LAPIC_ENUM_PRIO_MADT;
732 kgetenv_int("hw.madt_lapic_prio", &prio);
86120698 733 madt_lapic_enumerator.lapic_prio = prio;
91903a05 734
86120698 735 lapic_enumerator_register(&madt_lapic_enumerator);
91903a05 736}
27b59776 737SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
65b2387f
SZ
738
739struct madt_ioapic_probe_cbarg {
740 int ioapic_cnt;
741 int gsi_base0;
742};
743
744static int
745madt_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
1365d5cd
SZ
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
65b2387f 767 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
1365d5cd
SZ
768 kprintf("ACPI MADT: warning intsrc irq %d "
769 "bus is not ISA (%d)\n",
770 intsrc_ent->mint_src, intsrc_ent->mint_bus);
65b2387f
SZ
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) {
1365d5cd
SZ
777 kprintf("ACPI MADT: warning invalid intsrc irq %d "
778 "trig (%d)\n", intsrc_ent->mint_src, trig);
65b2387f
SZ
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) {
1365d5cd
SZ
785 kprintf("ACPI MADT: warning invalid intsrc irq %d "
786 "pola (%d)\n", intsrc_ent->mint_src, pola);
65b2387f
SZ
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
804static int
805madt_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
835static int
836madt_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;
512fb675
SZ
842 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi ||
843 intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
1365d5cd
SZ
844 return 0;
845
65b2387f
SZ
846 MADT_VPRINTF("INTSRC irq %d -> gsi %u\n",
847 intsrc_ent->mint_src, intsrc_ent->mint_gsi);
512fb675 848 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi);
65b2387f
SZ
849 } else if (ent->me_type == MADT_ENT_IOAPIC) {
850 const struct acpi_madt_ioapic *ioapic_ent;
851
852 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
853 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
854 ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
855 ioapic_ent->mio_gsi_base);
ebf4f417
SZ
856
857 if (!ioapic_use_old) {
858 uint32_t ver;
859 void *addr;
860 int npin;
861
862 addr = ioapic_map(ioapic_ent->mio_addr);
863
864 ver = ioapic_read(addr, IOAPIC_VER);
865 npin = ((ver & IOART_VER_MAXREDIR) >>
866 MAXREDIRSHIFT) + 1;
867
868 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
869 }
65b2387f
SZ
870 }
871 return 0;
872}
873
874static void
875madt_ioapic_enumerate(struct ioapic_enumerator *e)
876{
877 struct acpi_madt *madt;
878 int error;
879
880 KKASSERT(madt_phyaddr != 0);
881
882 madt = madt_sdth_map(madt_phyaddr);
883 KKASSERT(madt != NULL);
884
885 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
886 if (error)
887 panic("madt_ioapic_enumerate failed\n");
888
889 madt_sdth_unmap(&madt->madt_hdr);
890}
891
892static struct ioapic_enumerator madt_ioapic_enumerator = {
893 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
894 .ioapic_probe = madt_ioapic_probe,
895 .ioapic_enumerate = madt_ioapic_enumerate
896};
897
898static void
899madt_ioapic_enum_register(void)
900{
901 int prio;
902
903 prio = IOAPIC_ENUM_PRIO_MADT;
904 kgetenv_int("hw.madt_ioapic_prio", &prio);
905 madt_ioapic_enumerator.ioapic_prio = prio;
906
907 ioapic_enumerator_register(&madt_ioapic_enumerator);
908}
909SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
910 madt_ioapic_enum_register, 0);