madt: Support MADT rev 3 (used by ACPI 4.0)
[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
2bbbec42 119#define MADT_ENT_INTSRC_OVR 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
SZ
141/* MADT Interrupt Source Override */
142struct acpi_madt_intsrc_ovr {
143 struct acpi_madt_ent miso_hdr;
144 uint8_t miso_bus;
145 uint8_t miso_src;
146 uint32_t miso_gsi;
147 uint16_t miso_flags; /* MADT_ISO_ */
148} __packed;
149
150#define MADT_ISO_POLA_MASK 0x3
151#define MADT_ISO_POLA_SHIFT 0
152#define MADT_ISO_POLA_CONFORM 0
153#define MADT_ISO_POLA_HIGH 1
154#define MADT_ISO_POLA_RSVD 2
155#define MADT_ISO_POLA_LOW 3
156#define MADT_ISO_TRIG_MASK 0xc
157#define MADT_ISO_TRIG_SHIFT 2
158#define MADT_ISO_TRIG_CONFORM 0
159#define MADT_ISO_TRIG_EDGE 1
160#define MADT_ISO_TRIG_RSVD 2
161#define MADT_ISO_TRIG_LEVEL 3
162
ebbde674
MN
163/* MADT Local APIC Address Override */
164struct acpi_madt_lapic_addr {
165 struct acpi_madt_ent mla_hdr;
166 uint16_t mla_reserved;
167 uint64_t mla_lapic_addr;
168} __packed;
169
8e4c6923 170typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
0492bd82
MN
171typedef int (*madt_iter_t)(void *,
172 const struct acpi_madt_ent *);
8e4c6923
MN
173
174static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
175static void *madt_sdth_map(vm_paddr_t);
176static void madt_sdth_unmap(struct acpi_sdth *);
177static vm_paddr_t madt_search_xsdt(vm_paddr_t);
178static vm_paddr_t madt_search_rsdt(vm_paddr_t);
4ad5917f 179static int madt_check(vm_paddr_t);
0492bd82
MN
180static int madt_iterate_entries(struct acpi_madt *,
181 madt_iter_t, void *);
8e4c6923 182
27b59776
SZ
183static vm_offset_t madt_lapic_pass1(void);
184static int madt_lapic_pass2(int);
91903a05
MN
185
186static void madt_lapic_enumerate(struct lapic_enumerator *);
187static int madt_lapic_probe(struct lapic_enumerator *);
188
8e4c6923
MN
189extern u_long ebda_addr;
190
86120698
SZ
191static vm_paddr_t madt_phyaddr;
192
193static void
8e4c6923
MN
194madt_probe(void)
195{
196 const struct acpi_rsdp *rsdp;
197 madt_search_t search;
198 vm_paddr_t search_paddr, madt_paddr;
199 vm_size_t mapsz;
200 uint8_t *ptr;
201
86120698
SZ
202 KKASSERT(madt_phyaddr == 0);
203
8e4c6923
MN
204 if (ebda_addr != 0) {
205 mapsz = ACPI_RSDP_EBDA_MAPSZ;
206 ptr = pmap_mapdev(ebda_addr, mapsz);
207
208 rsdp = madt_rsdp_search(ptr, mapsz);
209 if (rsdp == NULL) {
c87b587d 210 MADT_VPRINTF("RSDP not in EBDA\n");
8e4c6923
MN
211 pmap_unmapdev((vm_offset_t)ptr, mapsz);
212
213 ptr = NULL;
214 mapsz = 0;
215 } else {
c87b587d 216 MADT_VPRINTF("RSDP in EBDA\n");
8e4c6923
MN
217 goto found_rsdp;
218 }
219 }
220
221 mapsz = ACPI_RSDP_BIOS_MAPSZ;
222 ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
223
224 rsdp = madt_rsdp_search(ptr, mapsz);
225 if (rsdp == NULL) {
226 kprintf("madt_probe: no RSDP\n");
227 pmap_unmapdev((vm_offset_t)ptr, mapsz);
86120698 228 return;
8e4c6923 229 } else {
c87b587d 230 MADT_VPRINTF("RSDP in BIOS mem\n");
8e4c6923
MN
231 }
232
233found_rsdp:
234 if (rsdp->rsdp_rev != 2) {
235 search_paddr = rsdp->rsdp_rsdt;
236 search = madt_search_rsdt;
237 } else {
238 search_paddr = rsdp->rsdp_xsdt;
239 search = madt_search_xsdt;
240 }
241 pmap_unmapdev((vm_offset_t)ptr, mapsz);
242
243 madt_paddr = search(search_paddr);
244 if (madt_paddr == 0) {
245 kprintf("madt_probe: can't locate MADT\n");
86120698 246 return;
8e4c6923 247 }
4ad5917f
MN
248
249 /* Preliminary checks */
86120698
SZ
250 if (madt_check(madt_paddr)) {
251 kprintf("madt_probe: madt_check failed\n");
252 return;
253 }
254
255 madt_phyaddr = madt_paddr;
8e4c6923 256}
86120698 257SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, madt_probe, 0);
8e4c6923
MN
258
259static const struct acpi_rsdp *
260madt_rsdp_search(const uint8_t *target, int size)
261{
262 const struct acpi_rsdp *rsdp;
263 int i;
264
265 KKASSERT(size > sizeof(*rsdp));
266
267 for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
268 rsdp = (const struct acpi_rsdp *)&target[i];
269 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
270 ACPI_RSDP_SIGLEN) == 0)
271 return rsdp;
272 }
273 return NULL;
274}
275
276static void *
277madt_sdth_map(vm_paddr_t paddr)
278{
279 struct acpi_sdth *sdth;
280 vm_size_t mapsz;
281
282 sdth = pmap_mapdev(paddr, sizeof(*sdth));
283 mapsz = sdth->sdth_len;
284 pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
285
286 if (mapsz < sizeof(*sdth))
287 return NULL;
288
289 return pmap_mapdev(paddr, mapsz);
290}
291
292static void
293madt_sdth_unmap(struct acpi_sdth *sdth)
294{
295 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
296}
297
298static vm_paddr_t
299madt_search_xsdt(vm_paddr_t xsdt_paddr)
300{
301 struct acpi_xsdt *xsdt;
302 vm_paddr_t madt_paddr = 0;
303 int i, nent;
304
305 if (xsdt_paddr == 0) {
306 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
307 return 0;
308 }
309
310 xsdt = madt_sdth_map(xsdt_paddr);
311 if (xsdt == NULL) {
312 kprintf("madt_search_xsdt: can't map XSDT\n");
313 return 0;
314 }
315
316 if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
317 ACPI_SDTH_SIGLEN) != 0) {
318 kprintf("madt_search_xsdt: not XSDT\n");
319 goto back;
320 }
321
322 if (xsdt->xsdt_hdr.sdth_rev != 1) {
323 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
324 xsdt->xsdt_hdr.sdth_rev);
325 goto back;
326 }
327
328 nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
329 sizeof(xsdt->xsdt_ents[0]);
330 for (i = 0; i < nent; ++i) {
331 struct acpi_sdth *sdth;
332
333 if (xsdt->xsdt_ents[i] == 0)
334 continue;
335
336 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
337 if (sdth != NULL) {
338 int ret;
339
340 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
341 ACPI_SDTH_SIGLEN);
342 madt_sdth_unmap(sdth);
343
344 if (ret == 0) {
c87b587d 345 MADT_VPRINTF("MADT in XSDT\n");
8e4c6923
MN
346 madt_paddr = xsdt->xsdt_ents[i];
347 break;
348 }
349 }
350 }
351back:
352 madt_sdth_unmap(&xsdt->xsdt_hdr);
353 return madt_paddr;
354}
355
356static vm_paddr_t
357madt_search_rsdt(vm_paddr_t rsdt_paddr)
358{
359 struct acpi_rsdt *rsdt;
360 vm_paddr_t madt_paddr = 0;
361 int i, nent;
362
363 if (rsdt_paddr == 0) {
364 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
365 return 0;
366 }
367
368 rsdt = madt_sdth_map(rsdt_paddr);
369 if (rsdt == NULL) {
370 kprintf("madt_search_rsdt: can't map RSDT\n");
371 return 0;
372 }
373
374 if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
375 ACPI_SDTH_SIGLEN) != 0) {
376 kprintf("madt_search_rsdt: not RSDT\n");
377 goto back;
378 }
379
380 if (rsdt->rsdt_hdr.sdth_rev != 1) {
381 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
382 rsdt->rsdt_hdr.sdth_rev);
383 goto back;
384 }
385
386 nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
387 sizeof(rsdt->rsdt_ents[0]);
388 for (i = 0; i < nent; ++i) {
389 struct acpi_sdth *sdth;
390
391 if (rsdt->rsdt_ents[i] == 0)
392 continue;
393
394 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
395 if (sdth != NULL) {
396 int ret;
397
398 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
399 ACPI_SDTH_SIGLEN);
400 madt_sdth_unmap(sdth);
401
402 if (ret == 0) {
c87b587d 403 MADT_VPRINTF("MADT in RSDT\n");
8e4c6923
MN
404 madt_paddr = rsdt->rsdt_ents[i];
405 break;
406 }
407 }
408 }
409back:
410 madt_sdth_unmap(&rsdt->rsdt_hdr);
411 return madt_paddr;
412}
413
ebbde674 414static int
27b59776
SZ
415madt_check(vm_paddr_t madt_paddr)
416{
417 struct acpi_madt *madt;
418 int error = 0;
419
420 KKASSERT(madt_paddr != 0);
421
422 madt = madt_sdth_map(madt_paddr);
423 KKASSERT(madt != NULL);
424
7d460393
SZ
425 /*
426 * MADT in ACPI specification 1.0 - 4.0
427 */
428 if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
27b59776
SZ
429 kprintf("madt_check: unsupported MADT revision %d\n",
430 madt->madt_hdr.sdth_rev);
431 error = EOPNOTSUPP;
432 goto back;
433 }
434
435 if (madt->madt_hdr.sdth_len <
436 sizeof(*madt) - sizeof(madt->madt_ents)) {
437 kprintf("madt_check: invalid MADT length %u\n",
438 madt->madt_hdr.sdth_len);
439 error = EINVAL;
440 goto back;
441 }
442back:
443 madt_sdth_unmap(&madt->madt_hdr);
444 return error;
445}
446
447static int
448madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
449{
450 int size, cur, error;
451
452 size = madt->madt_hdr.sdth_len -
453 (sizeof(*madt) - sizeof(madt->madt_ents));
454 cur = 0;
455 error = 0;
456
457 while (size - cur > sizeof(struct acpi_madt_ent)) {
458 const struct acpi_madt_ent *ent;
459
460 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
461 if (ent->me_len < sizeof(*ent)) {
462 kprintf("madt_iterate_entries: invalid MADT "
463 "entry len %d\n", ent->me_len);
464 error = EINVAL;
465 break;
466 }
467 if (ent->me_len > (size - cur)) {
468 kprintf("madt_iterate_entries: invalid MADT "
469 "entry len %d, > table length\n", ent->me_len);
470 error = EINVAL;
471 break;
472 }
473
474 cur += ent->me_len;
475
476 /*
477 * Only Local APIC, I/O APIC and Interrupt Source Override
478 * are defined in ACPI specification 1.0 - 4.0
479 */
480 switch (ent->me_type) {
481 case MADT_ENT_LAPIC:
482 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
483 kprintf("madt_iterate_entries: invalid MADT "
484 "lapic entry len %d\n", ent->me_len);
485 error = EINVAL;
486 }
487 break;
488
489 case MADT_ENT_IOAPIC:
490 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
491 kprintf("madt_iterate_entries: invalid MADT "
492 "ioapic entry len %d\n", ent->me_len);
493 error = EINVAL;
494 }
495 break;
496
497 case MADT_ENT_INTSRC_OVR:
498 if (ent->me_len < sizeof(struct acpi_madt_intsrc_ovr)) {
499 kprintf("madt_iterate_entries: invalid MADT "
500 "intsrc_ovr entry len %d\n",
501 ent->me_len);
502 error = EINVAL;
503 }
504 break;
505 }
506 if (error)
507 break;
508
509 error = func(arg, ent);
510 if (error)
511 break;
512 }
513 return error;
514}
515
516static int
517madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
ebbde674
MN
518{
519 const struct acpi_madt_lapic_addr *lapic_addr_ent;
520 uint64_t *addr64 = xarg;
521
522 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
523 return 0;
524 if (ent->me_len < sizeof(*lapic_addr_ent)) {
27b59776
SZ
525 kprintf("madt_lapic_pass1: "
526 "invalid LAPIC address override length\n");
ebbde674
MN
527 return 0;
528 }
529 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
530
531 *addr64 = lapic_addr_ent->mla_lapic_addr;
532 return 0;
533}
534
91903a05 535static vm_offset_t
27b59776 536madt_lapic_pass1(void)
8e4c6923
MN
537{
538 struct acpi_madt *madt;
4ad5917f 539 vm_offset_t lapic_addr;
ebbde674
MN
540 uint64_t lapic_addr64;
541 int error;
8e4c6923 542
86120698 543 KKASSERT(madt_phyaddr != 0);
8e4c6923 544
86120698 545 madt = madt_sdth_map(madt_phyaddr);
8e4c6923
MN
546 KKASSERT(madt != NULL);
547
c87b587d
MN
548 MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
549 madt->madt_lapic_addr, madt->madt_flags);
4ad5917f
MN
550 lapic_addr = madt->madt_lapic_addr;
551
ebbde674 552 lapic_addr64 = 0;
27b59776
SZ
553 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
554 &lapic_addr64);
ebbde674
MN
555 if (error)
556 panic("madt_iterate_entries(pass1) failed\n");
557
558 if (lapic_addr64 != 0) {
cb31590d
SZ
559 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
560 lapic_addr64);
ebbde674
MN
561 lapic_addr = lapic_addr64;
562 }
563
4ad5917f
MN
564 madt_sdth_unmap(&madt->madt_hdr);
565
566 return lapic_addr;
567}
568
27b59776 569struct madt_lapic_pass2_cbarg {
2c394345
MN
570 int cpu;
571 int bsp_found;
572 int bsp_apic_id;
573};
574
575static int
27b59776 576madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
2c394345
MN
577{
578 const struct acpi_madt_lapic *lapic_ent;
27b59776 579 struct madt_lapic_pass2_cbarg *arg = xarg;
2c394345
MN
580
581 if (ent->me_type != MADT_ENT_LAPIC)
582 return 0;
583
584 lapic_ent = (const struct acpi_madt_lapic *)ent;
585 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
c87b587d
MN
586 MADT_VPRINTF("cpu_id %d, apic_id %d\n",
587 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
2c394345
MN
588 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
589 mp_set_cpuids(0, lapic_ent->ml_apic_id);
590 arg->bsp_found = 1;
591 } else {
592 mp_set_cpuids(arg->cpu, lapic_ent->ml_apic_id);
593 arg->cpu++;
594 }
595 }
596 return 0;
597}
598
91903a05 599static int
27b59776 600madt_lapic_pass2(int bsp_apic_id)
4ad5917f
MN
601{
602 struct acpi_madt *madt;
27b59776 603 struct madt_lapic_pass2_cbarg arg;
2c394345 604 int error;
4ad5917f 605
c87b587d 606 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
4ad5917f 607
86120698 608 KKASSERT(madt_phyaddr != 0);
4ad5917f 609
86120698 610 madt = madt_sdth_map(madt_phyaddr);
4ad5917f 611 KKASSERT(madt != NULL);
8e4c6923 612
2c394345
MN
613 bzero(&arg, sizeof(arg));
614 arg.cpu = 1;
615 arg.bsp_apic_id = bsp_apic_id;
8e4c6923 616
27b59776 617 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
2c394345
MN
618 if (error)
619 panic("madt_iterate_entries(pass2) failed\n");
8e4c6923 620
2c394345
MN
621 KKASSERT(arg.bsp_found);
622 KKASSERT(arg.cpu > 1);
623 mp_naps = arg.cpu - 1; /* exclude BSP */
4ad5917f
MN
624
625 madt_sdth_unmap(&madt->madt_hdr);
2c394345
MN
626
627 return 0;
4ad5917f
MN
628}
629
86120698
SZ
630struct madt_lapic_probe_cbarg {
631 int cpu_count;
632 vm_offset_t lapic_addr;
633};
634
635static int
636madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
637{
638 struct madt_lapic_probe_cbarg *arg = xarg;
639
640 if (ent->me_type == MADT_ENT_LAPIC) {
641 const struct acpi_madt_lapic *lapic_ent;
642
643 lapic_ent = (const struct acpi_madt_lapic *)ent;
644 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
645 arg->cpu_count++;
646 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
647 const struct acpi_madt_lapic_addr *lapic_addr_ent;
648
649 if (ent->me_len < sizeof(*lapic_addr_ent)) {
650 kprintf("madt_lapic_probe: "
651 "invalid LAPIC address override length\n");
652 return 0;
653 }
654 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
655
656 if (lapic_addr_ent->mla_lapic_addr != 0)
657 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
658 }
659 return 0;
660}
661
91903a05
MN
662static int
663madt_lapic_probe(struct lapic_enumerator *e)
664{
86120698
SZ
665 struct madt_lapic_probe_cbarg arg;
666 struct acpi_madt *madt;
667 int error;
91903a05 668
86120698 669 if (madt_phyaddr == 0)
91903a05
MN
670 return ENXIO;
671
86120698
SZ
672 madt = madt_sdth_map(madt_phyaddr);
673 KKASSERT(madt != NULL);
674
675 bzero(&arg, sizeof(arg));
676 arg.lapic_addr = madt->madt_lapic_addr;
677
678 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
679 if (!error) {
680 if (arg.cpu_count <= 1) {
681 kprintf("madt_lapic_probe: "
682 "less than 2 CPUs is found\n");
683 error = EOPNOTSUPP;
684 }
685 if (arg.lapic_addr == 0) {
686 kprintf("madt_lapic_probe: zero LAPIC address\n");
687 error = EOPNOTSUPP;
688 }
689 }
690
691 madt_sdth_unmap(&madt->madt_hdr);
692 return error;
91903a05
MN
693}
694
695static void
696madt_lapic_enumerate(struct lapic_enumerator *e)
697{
91903a05
MN
698 vm_offset_t lapic_addr;
699 int bsp_apic_id;
700
86120698 701 KKASSERT(madt_phyaddr != 0);
91903a05 702
27b59776 703 lapic_addr = madt_lapic_pass1();
91903a05 704 if (lapic_addr == 0)
86120698 705 panic("madt_lapic_enumerate: no local apic\n");
91903a05 706
b44f1d28 707 lapic_map(lapic_addr);
1c1f402e
MN
708
709 bsp_apic_id = APIC_ID(lapic->id);
27b59776
SZ
710 if (madt_lapic_pass2(bsp_apic_id))
711 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
91903a05
MN
712}
713
86120698
SZ
714static struct lapic_enumerator madt_lapic_enumerator = {
715 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
716 .lapic_probe = madt_lapic_probe,
717 .lapic_enumerate = madt_lapic_enumerate
91903a05
MN
718};
719
720static void
27b59776 721madt_lapic_enum_register(void)
91903a05
MN
722{
723 int prio;
724
725 prio = LAPIC_ENUM_PRIO_MADT;
726 kgetenv_int("hw.madt_lapic_prio", &prio);
86120698 727 madt_lapic_enumerator.lapic_prio = prio;
91903a05 728
86120698 729 lapic_enumerator_register(&madt_lapic_enumerator);
91903a05 730}
27b59776 731SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);