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