ACPI MADT: Check existance of BSP in madt_check()
[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>
36#include <sys/systm.h>
37
38#include <machine/pmap.h>
39#include <machine/smp.h>
ee5932ac
MN
40#include <machine/md_var.h>
41#include <machine/specialreg.h>
8e4c6923
MN
42
43#define ACPI_RSDP_EBDA_MAPSZ 1024
44#define ACPI_RSDP_BIOS_MAPSZ 0x20000
45#define ACPI_RSDP_BIOS_MAPADDR 0xe0000
46
47#define ACPI_RSDP_ALIGN 16
48
49#define ACPI_RSDP_SIGLEN 8
50#define ACPI_RSDP_SIG "RSD PTR "
51
52#define ACPI_SDTH_SIGLEN 4
53#define ACPI_RSDT_SIG "RSDT"
54#define ACPI_XSDT_SIG "XSDT"
55#define ACPI_MADT_SIG "APIC"
56
57/* Root System Description Pointer */
58struct acpi_rsdp {
59 uint8_t rsdp_sig[ACPI_RSDP_SIGLEN];
60 uint8_t rsdp_cksum;
61 uint8_t rsdp_oem_id[6];
62 uint8_t rsdp_rev;
63 uint32_t rsdp_rsdt;
64 uint32_t rsdp_len;
65 uint64_t rsdp_xsdt;
66 uint8_t rsdp_ext_cksum;
67 uint8_t rsdp_rsvd[3];
68} __packed;
69
70/* System Description Table Header */
71struct acpi_sdth {
72 uint8_t sdth_sig[ACPI_SDTH_SIGLEN];
73 uint32_t sdth_len;
74 uint8_t sdth_rev;
75 uint8_t sdth_cksum;
76 uint8_t sdth_oem_id[6];
77 uint8_t sdth_oem_tbid[8];
78 uint32_t sdth_oem_rev;
79 uint32_t sdth_crt_id;
80 uint32_t sdth_crt_rev;
81} __packed;
82
83/* Extended System Description Table */
84struct acpi_xsdt {
85 struct acpi_sdth xsdt_hdr;
86 uint64_t xsdt_ents[1];
87} __packed;
88
89/* Root System Description Table */
90struct acpi_rsdt {
91 struct acpi_sdth rsdt_hdr;
92 uint32_t rsdt_ents[1];
93} __packed;
94
95/* Multiple APIC Description Table */
96struct acpi_madt {
97 struct acpi_sdth madt_hdr;
98 uint32_t madt_lapic_addr;
99 uint32_t madt_flags;
100 uint8_t madt_ents[1];
101} __packed;
102
103/* Common parts of MADT APIC structure */
104struct acpi_madt_ent {
105 uint8_t me_type; /* MADT_ENT_ */
106 uint8_t me_len;
107} __packed;
108
109#define MADT_ENT_LAPIC 0
0492bd82 110#define MADT_ENT_IOAPIC 1
8e4c6923
MN
111
112/* MADT Processor Local APIC */
113struct acpi_madt_lapic {
114 struct acpi_madt_ent ml_hdr;
115 uint8_t ml_cpu_id;
116 uint8_t ml_apic_id;
117 uint32_t ml_flags; /* MADT_LAPIC_ */
118} __packed;
119
120#define MADT_LAPIC_ENABLED 0x1
121
0492bd82
MN
122/* MADT I/O APIC */
123struct acpi_madt_ioapic {
124 struct acpi_madt_ent mio_hdr;
125 uint8_t mio_apic_id;
126 uint8_t mio_reserved;
127 uint32_t mio_addr;
128 uint32_t mio_gsi_base;
129} __packed;
130
8e4c6923 131typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
0492bd82
MN
132typedef int (*madt_iter_t)(void *,
133 const struct acpi_madt_ent *);
8e4c6923
MN
134
135static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
136static void *madt_sdth_map(vm_paddr_t);
137static void madt_sdth_unmap(struct acpi_sdth *);
138static vm_paddr_t madt_search_xsdt(vm_paddr_t);
139static vm_paddr_t madt_search_rsdt(vm_paddr_t);
4ad5917f 140static int madt_check(vm_paddr_t);
0492bd82
MN
141static int madt_iterate_entries(struct acpi_madt *,
142 madt_iter_t, void *);
8e4c6923
MN
143
144extern u_long ebda_addr;
145
4ad5917f 146vm_paddr_t
8e4c6923
MN
147madt_probe(void)
148{
149 const struct acpi_rsdp *rsdp;
150 madt_search_t search;
151 vm_paddr_t search_paddr, madt_paddr;
152 vm_size_t mapsz;
153 uint8_t *ptr;
154
155 if (ebda_addr != 0) {
156 mapsz = ACPI_RSDP_EBDA_MAPSZ;
157 ptr = pmap_mapdev(ebda_addr, mapsz);
158
159 rsdp = madt_rsdp_search(ptr, mapsz);
160 if (rsdp == NULL) {
161 kprintf("madt: RSDP not in EBDA\n");
162 pmap_unmapdev((vm_offset_t)ptr, mapsz);
163
164 ptr = NULL;
165 mapsz = 0;
166 } else {
167 kprintf("madt: RSDP in EBDA\n");
168 goto found_rsdp;
169 }
170 }
171
172 mapsz = ACPI_RSDP_BIOS_MAPSZ;
173 ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
174
175 rsdp = madt_rsdp_search(ptr, mapsz);
176 if (rsdp == NULL) {
177 kprintf("madt_probe: no RSDP\n");
178 pmap_unmapdev((vm_offset_t)ptr, mapsz);
4ad5917f 179 return 0;
8e4c6923
MN
180 } else {
181 kprintf("madt: RSDP in BIOS mem\n");
182 }
183
184found_rsdp:
185 if (rsdp->rsdp_rev != 2) {
186 search_paddr = rsdp->rsdp_rsdt;
187 search = madt_search_rsdt;
188 } else {
189 search_paddr = rsdp->rsdp_xsdt;
190 search = madt_search_xsdt;
191 }
192 pmap_unmapdev((vm_offset_t)ptr, mapsz);
193
194 madt_paddr = search(search_paddr);
195 if (madt_paddr == 0) {
196 kprintf("madt_probe: can't locate MADT\n");
4ad5917f 197 return 0;
8e4c6923 198 }
4ad5917f
MN
199
200 /* Preliminary checks */
201 if (madt_check(madt_paddr))
202 return 0;
203 return madt_paddr;
8e4c6923
MN
204}
205
206static const struct acpi_rsdp *
207madt_rsdp_search(const uint8_t *target, int size)
208{
209 const struct acpi_rsdp *rsdp;
210 int i;
211
212 KKASSERT(size > sizeof(*rsdp));
213
214 for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
215 rsdp = (const struct acpi_rsdp *)&target[i];
216 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
217 ACPI_RSDP_SIGLEN) == 0)
218 return rsdp;
219 }
220 return NULL;
221}
222
223static void *
224madt_sdth_map(vm_paddr_t paddr)
225{
226 struct acpi_sdth *sdth;
227 vm_size_t mapsz;
228
229 sdth = pmap_mapdev(paddr, sizeof(*sdth));
230 mapsz = sdth->sdth_len;
231 pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
232
233 if (mapsz < sizeof(*sdth))
234 return NULL;
235
236 return pmap_mapdev(paddr, mapsz);
237}
238
239static void
240madt_sdth_unmap(struct acpi_sdth *sdth)
241{
242 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
243}
244
245static vm_paddr_t
246madt_search_xsdt(vm_paddr_t xsdt_paddr)
247{
248 struct acpi_xsdt *xsdt;
249 vm_paddr_t madt_paddr = 0;
250 int i, nent;
251
252 if (xsdt_paddr == 0) {
253 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
254 return 0;
255 }
256
257 xsdt = madt_sdth_map(xsdt_paddr);
258 if (xsdt == NULL) {
259 kprintf("madt_search_xsdt: can't map XSDT\n");
260 return 0;
261 }
262
263 if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
264 ACPI_SDTH_SIGLEN) != 0) {
265 kprintf("madt_search_xsdt: not XSDT\n");
266 goto back;
267 }
268
269 if (xsdt->xsdt_hdr.sdth_rev != 1) {
270 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
271 xsdt->xsdt_hdr.sdth_rev);
272 goto back;
273 }
274
275 nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
276 sizeof(xsdt->xsdt_ents[0]);
277 for (i = 0; i < nent; ++i) {
278 struct acpi_sdth *sdth;
279
280 if (xsdt->xsdt_ents[i] == 0)
281 continue;
282
283 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
284 if (sdth != NULL) {
285 int ret;
286
287 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
288 ACPI_SDTH_SIGLEN);
289 madt_sdth_unmap(sdth);
290
291 if (ret == 0) {
292 kprintf("madt: MADT in XSDT\n");
293 madt_paddr = xsdt->xsdt_ents[i];
294 break;
295 }
296 }
297 }
298back:
299 madt_sdth_unmap(&xsdt->xsdt_hdr);
300 return madt_paddr;
301}
302
303static vm_paddr_t
304madt_search_rsdt(vm_paddr_t rsdt_paddr)
305{
306 struct acpi_rsdt *rsdt;
307 vm_paddr_t madt_paddr = 0;
308 int i, nent;
309
310 if (rsdt_paddr == 0) {
311 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
312 return 0;
313 }
314
315 rsdt = madt_sdth_map(rsdt_paddr);
316 if (rsdt == NULL) {
317 kprintf("madt_search_rsdt: can't map RSDT\n");
318 return 0;
319 }
320
321 if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
322 ACPI_SDTH_SIGLEN) != 0) {
323 kprintf("madt_search_rsdt: not RSDT\n");
324 goto back;
325 }
326
327 if (rsdt->rsdt_hdr.sdth_rev != 1) {
328 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
329 rsdt->rsdt_hdr.sdth_rev);
330 goto back;
331 }
332
333 nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
334 sizeof(rsdt->rsdt_ents[0]);
335 for (i = 0; i < nent; ++i) {
336 struct acpi_sdth *sdth;
337
338 if (rsdt->rsdt_ents[i] == 0)
339 continue;
340
341 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
342 if (sdth != NULL) {
343 int ret;
344
345 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
346 ACPI_SDTH_SIGLEN);
347 madt_sdth_unmap(sdth);
348
349 if (ret == 0) {
350 kprintf("madt: MADT in RSDT\n");
351 madt_paddr = rsdt->rsdt_ents[i];
352 break;
353 }
354 }
355 }
356back:
357 madt_sdth_unmap(&rsdt->rsdt_hdr);
358 return madt_paddr;
359}
360
4ad5917f
MN
361vm_offset_t
362madt_pass1(vm_paddr_t madt_paddr)
8e4c6923
MN
363{
364 struct acpi_madt *madt;
4ad5917f 365 vm_offset_t lapic_addr;
8e4c6923
MN
366
367 KKASSERT(madt_paddr != 0);
368
369 madt = madt_sdth_map(madt_paddr);
370 KKASSERT(madt != NULL);
371
8e4c6923
MN
372 kprintf("madt: LAPIC address 0x%08x, flags %#x\n",
373 madt->madt_lapic_addr, madt->madt_flags);
4ad5917f
MN
374 lapic_addr = madt->madt_lapic_addr;
375
376 madt_sdth_unmap(&madt->madt_hdr);
377
378 return lapic_addr;
379}
380
381int
382madt_pass2(vm_paddr_t madt_paddr, int bsp_apic_id)
383{
384 struct acpi_madt *madt;
385 int size, cur, error, cpu, found_bsp;
386
387 kprintf("madt: BSP apic id %d\n", bsp_apic_id);
388
389 KKASSERT(madt_paddr != 0);
390
391 madt = madt_sdth_map(madt_paddr);
392 KKASSERT(madt != NULL);
8e4c6923
MN
393
394 size = madt->madt_hdr.sdth_len -
395 (sizeof(*madt) - sizeof(madt->madt_ents));
396 cur = 0;
397 error = 0;
4ad5917f
MN
398 found_bsp = 0;
399 cpu = 1;
8e4c6923
MN
400
401 while (size - cur > sizeof(struct acpi_madt_ent)) {
402 const struct acpi_madt_ent *ent;
403
404 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
405 if (ent->me_len < sizeof(*ent)) {
4ad5917f 406 kprintf("madt_pass2: invalid MADT entry len %d\n",
8e4c6923
MN
407 ent->me_len);
408 error = EINVAL;
409 break;
410 }
411 if (ent->me_len > (size - cur)) {
4ad5917f 412 kprintf("madt_pass2: invalid MADT entry len %d, "
8e4c6923
MN
413 "> table length\n", ent->me_len);
414 error = EINVAL;
415 break;
416 }
417
418 cur += ent->me_len;
419
420 if (ent->me_type == MADT_ENT_LAPIC) {
421 const struct acpi_madt_lapic *lapic_ent;
422
423 if (ent->me_len < sizeof(*lapic_ent)) {
4ad5917f 424 kprintf("madt_pass2: invalid MADT lapic entry "
8e4c6923
MN
425 "len %d\n", ent->me_len);
426 error = EINVAL;
427 break;
428 }
429 lapic_ent = (const struct acpi_madt_lapic *)ent;
430 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
431 kprintf("madt: cpu_id %d, apic_id %d\n",
432 lapic_ent->ml_cpu_id,
433 lapic_ent->ml_apic_id);
4ad5917f
MN
434 if (lapic_ent->ml_apic_id == bsp_apic_id) {
435 mp_set_cpuids(0,
436 lapic_ent->ml_apic_id);
437 found_bsp = 1;
438 } else {
439 mp_set_cpuids(cpu,
440 lapic_ent->ml_apic_id);
441 ++cpu;
442 }
8e4c6923
MN
443 }
444 }
445 }
4ad5917f
MN
446 if (!found_bsp) {
447 kprintf("madt_pass2: BSP is not found\n");
448 error = EINVAL;
449 }
450 if (cpu == 1) {
451 kprintf("madt_pass2: no APs\n");
699b0754 452 error = EINVAL;
4ad5917f 453 }
699b0754 454 if (!error)
4ad5917f
MN
455 mp_naps = cpu - 1;
456
457 madt_sdth_unmap(&madt->madt_hdr);
458 return error;
459}
460
0492bd82
MN
461struct madt_check_cbarg {
462 int cpu_count;
ee5932ac
MN
463 int bsp_found;
464 int bsp_apic_id;
0492bd82
MN
465};
466
467static int
468madt_check_callback(void *xarg, const struct acpi_madt_ent *ent)
469{
470 struct madt_check_cbarg *arg = xarg;
471 const struct acpi_madt_lapic *lapic_ent;
472
473 if (ent->me_type != MADT_ENT_LAPIC)
474 return 0;
475 lapic_ent = (const struct acpi_madt_lapic *)ent;
476
ee5932ac 477 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
0492bd82 478 arg->cpu_count++;
ee5932ac
MN
479 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
480 if (arg->bsp_found) {
481 kprintf("madt_check: more than one BSP?\n");
482 return EINVAL;
483 }
484 arg->bsp_found = 1;
485 }
486 }
0492bd82
MN
487 return 0;
488}
489
4ad5917f
MN
490static int
491madt_check(vm_paddr_t madt_paddr)
492{
0492bd82 493 struct madt_check_cbarg arg;
4ad5917f
MN
494 struct acpi_madt *madt;
495 int error = 0;
496
497 KKASSERT(madt_paddr != 0);
498
499 madt = madt_sdth_map(madt_paddr);
500 KKASSERT(madt != NULL);
501
502 if (madt->madt_hdr.sdth_rev != 1 && madt->madt_hdr.sdth_rev != 2) {
503 kprintf("madt_check: unsupported MADT revision %d\n",
504 madt->madt_hdr.sdth_rev);
505 error = EOPNOTSUPP;
506 goto back;
507 }
508
509 if (madt->madt_hdr.sdth_len <
510 sizeof(*madt) - sizeof(madt->madt_ents)) {
511 kprintf("madt_check: invalid MADT length %u\n",
512 madt->madt_hdr.sdth_len);
513 error = EINVAL;
0492bd82
MN
514 goto back;
515 }
516
517 bzero(&arg, sizeof(arg));
ee5932ac
MN
518 arg.bsp_apic_id = (cpu_procinfo & CPUID_LOCAL_APIC_ID) >> 24;
519
0492bd82
MN
520 error = madt_iterate_entries(madt, madt_check_callback, &arg);
521 if (!error) {
ee5932ac
MN
522 if (arg.cpu_count <= 1) {
523 kprintf("madt_check: less than 2 CPUs is found\n");
0492bd82 524 error = EOPNOTSUPP;
ee5932ac
MN
525 } else if (!arg.bsp_found) {
526 kprintf("madt_check: no BSP\n");
527 error = EINVAL;
528 }
4ad5917f 529 }
8e4c6923
MN
530back:
531 madt_sdth_unmap(&madt->madt_hdr);
532 return error;
533}
4ad5917f 534
0492bd82
MN
535static int
536madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
537{
538 int size, cur, error;
539
540 size = madt->madt_hdr.sdth_len -
541 (sizeof(*madt) - sizeof(madt->madt_ents));
542 cur = 0;
543 error = 0;
544
545 while (size - cur > sizeof(struct acpi_madt_ent)) {
546 const struct acpi_madt_ent *ent;
547
548 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
549 if (ent->me_len < sizeof(*ent)) {
550 kprintf("madt_iterate_entries: invalid MADT "
551 "entry len %d\n", ent->me_len);
552 error = EINVAL;
553 break;
554 }
555 if (ent->me_len > (size - cur)) {
556 kprintf("madt_iterate_entries: invalid MADT "
557 "entry len %d, > table length\n", ent->me_len);
558 error = EINVAL;
559 break;
560 }
561
562 cur += ent->me_len;
563
564 /*
565 * Only Local APIC and I/O APIC are defined in
566 * ACPI specification 1.0 - 3.0
567 */
568 switch (ent->me_type) {
569 case MADT_ENT_LAPIC:
570 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
571 kprintf("madt_iterate_entries: invalid MADT "
572 "lapic entry len %d\n", ent->me_len);
573 error = EINVAL;
574 }
575 break;
576
577 case MADT_ENT_IOAPIC:
578 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
579 kprintf("madt_iterate_entries: invalid MADT "
580 "ioapic entry len %d\n", ent->me_len);
581 error = EINVAL;
582 }
583 break;
584 }
585 if (error)
586 break;
587
588 error = func(arg, ent);
589 if (error)
590 break;
591 }
592 return error;
593}
594
4ad5917f 595