Skip to content

Commit 1afb5d7

Browse files
committed
WIP x86 smp: start the framework for detecting and starting secondary cores
1 parent 181796e commit 1afb5d7

File tree

8 files changed

+132
-35
lines changed

8 files changed

+132
-35
lines changed

arch/x86/mp.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <arch/arch_ops.h>
1616
#include <sys/types.h>
1717

18+
#if WITH_SMP
19+
1820
// the boot cpu's percpu struct
1921
static x86_percpu_t x86_boot_percpu;
2022
// pointer to an array of percpu structs for each of the secondary cpus
@@ -47,7 +49,6 @@ void x86_percpu_init_early(uint cpu_num, uint apic_id) {
4749
x86_set_gdt_descriptor(selector, percpu, sizeof(*percpu), 1, 0, 1, SEG_TYPE_DATA_RW, 0, 1);
4850
x86_set_gs(selector);
4951
#endif
50-
__UNUSED volatile uint foo = x86_get_cpu_num();
5152
}
5253

5354
status_t arch_mp_send_ipi(mp_cpu_mask_t target, mp_ipi_t ipi) {
@@ -56,3 +57,9 @@ status_t arch_mp_send_ipi(mp_cpu_mask_t target, mp_ipi_t ipi) {
5657

5758
void arch_mp_init_percpu(void) {
5859
}
60+
61+
#else
62+
63+
void x86_percpu_init_early(uint cpu_num, uint apic_id) {}
64+
65+
#endif

lib/acpi_lite/acpi_lite.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ void acpi_lite_dump_tables(bool full_dump) {
407407
}
408408
}
409409

410-
status_t acpi_process_madt_entries_etc(const uint8_t search_type, const madt_entry_callback callback) {
410+
status_t acpi_process_madt_entries_etc(const uint8_t search_type, const madt_entry_callback callback, void *cookie) {
411411
const acpi_madt_table* madt =
412412
reinterpret_cast<const acpi_madt_table*>(acpi_get_table_by_sig(ACPI_MADT_SIG));
413413
if (!madt) {
@@ -417,14 +417,17 @@ status_t acpi_process_madt_entries_etc(const uint8_t search_type, const madt_ent
417417
// bytewise array of the same table
418418
const uint8_t* madt_array = reinterpret_cast<const uint8_t*>(madt);
419419

420+
LTRACEF("table at %p\n", madt_array);
421+
420422
// walk the table off the end of the header, looking for the requested type
421423
size_t off = sizeof(*madt);
422424
while (off < madt->header.length) {
423425
uint8_t type = madt_array[off];
424426
uint8_t length = madt_array[off + 1];
425427

428+
LTRACEF("type %u, length %u\n", type, length);
426429
if (type == search_type) {
427-
callback(static_cast<const void*>(&madt_array[off]), length);
430+
callback(static_cast<const void*>(&madt_array[off]), length, cookie);
428431
}
429432

430433
off += length;
@@ -433,4 +436,32 @@ status_t acpi_process_madt_entries_etc(const uint8_t search_type, const madt_ent
433436
return NO_ERROR;
434437
}
435438

439+
void acpi_lite_dump_madt_table() {
440+
auto local_apic_callback = [](const void *_entry, size_t entry_len, void *cookie) {
441+
const auto *entry = reinterpret_cast<const struct acpi_madt_local_apic_entry *>(_entry);
442+
443+
printf("\tLOCAL APIC id %d, processor id %d, flags %#x\n",
444+
entry->apic_id, entry->processor_id, entry->flags);
445+
};
446+
447+
auto io_apic_callback = [](const void *_entry, size_t entry_len, void *cookie) {
448+
const auto *entry = reinterpret_cast<const struct acpi_madt_io_apic_entry *>(_entry);
449+
450+
printf("\tIO APIC id %d, address %#x gsi base %u\n",
451+
entry->io_apic_id, entry->io_apic_address, entry->global_system_interrupt_base);
452+
};
453+
454+
auto int_source_override_callback = [](const void *_entry, size_t entry_len, void *cookie) {
455+
const auto *entry = reinterpret_cast<const struct acpi_madt_int_source_override_entry *>(_entry);
456+
457+
printf("\tINT OVERRIDE bus %u, source %u, gsi %u, flags %#x\n",
458+
entry->bus, entry->source, entry->global_sys_interrupt, entry->flags);
459+
};
460+
printf("MADT/APIC table:\n");
461+
acpi_process_madt_entries_etc(ACPI_MADT_TYPE_LOCAL_APIC, local_apic_callback, nullptr);
462+
acpi_process_madt_entries_etc(ACPI_MADT_TYPE_IO_APIC, io_apic_callback, nullptr);
463+
acpi_process_madt_entries_etc(ACPI_MADT_TYPE_INT_SOURCE_OVERRIDE, int_source_override_callback, nullptr);
464+
}
465+
466+
436467
// vim: set ts=2 sw=2 expandtab:

lib/acpi_lite/include/lib/acpi_lite.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ __BEGIN_CDECLS
1717

1818
status_t acpi_lite_init(paddr_t rsdt);
1919
void acpi_lite_dump_tables(bool full_dump);
20+
void acpi_lite_dump_madt_table(void);
2021

2122
const struct acpi_sdt_header* acpi_get_table_by_sig(const char* sig);
2223

2324
// A routine to iterate over all the MADT entries of a particular type via a callback
2425
//using MadtEntryCallback = fbl::Function<void(const void* entry, size_t entry_len)>;
25-
typedef void (*madt_entry_callback)(const void* entry, size_t entry_len);
26-
status_t acpi_process_madt_entries_etc(uint8_t search_type, const madt_entry_callback);
26+
typedef void (*madt_entry_callback)(const void* entry, size_t entry_len, void *cookie);
27+
status_t acpi_process_madt_entries_etc(uint8_t search_type, const madt_entry_callback, void *cookie);
28+
2729

2830
__END_CDECLS

lib/acpi_lite/include/lib/acpi_lite/structs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ static_assert(sizeof(struct acpi_madt_int_source_override_entry) == 10, "");
230230
#define ACPI_MADT_FLAG_TRIGGER_MASK 0b1100
231231

232232
// DBG2 table
233+
// From https://learn.microsoft.com/en-us/windows-hardware/drivers/bringup/acpi-debug-port-table
233234
#define ACPI_DBG2_SIG "DBG2"
234235
struct acpi_dbg2_table {
235236
struct acpi_sdt_header header;
@@ -263,7 +264,13 @@ static_assert(sizeof(struct acpi_dbg2_device) == 22, "");
263264
// debug port subtypes
264265
#define ACPI_DBG2_SUBTYPE_16550_COMPATIBLE 0x0000
265266
#define ACPI_DBG2_SUBTYPE_16550_SUBSET 0x0001
267+
#define ACPI_DBG2_SUBTYPE_PL011 0x0003
268+
#define ACPI_DBG2_SUBTYPE_ARM_SBSA 0x000e
269+
#define ACPI_DBG2_SUBTYPE_16550_DESCRIBED 0x0012
270+
#define ACPI_DBG2_SUBTYPE_RISCV_SBI 0x0015
271+
266272
#define ACPI_DBG2_SUBTYPE_1394_STANDARD 0x0000
273+
267274
#define ACPI_DBG2_SUBTYPE_USB_XHCI 0x0000
268275
#define ACPI_DBG2_SUBTYPE_USB_EHCI 0x0001
269276

platform/pc/mp.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2024 Travis Geiselbrecht
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT
7+
*/
8+
9+
#include "platform_p.h"
10+
11+
#include <lk/main.h>
12+
#include <lib/acpi_lite.h>
13+
#include <lk/trace.h>
14+
15+
#define LOCAL_TRACE 1
16+
17+
static void start_cpu(uint cpu_num, uint32_t apic_id) {
18+
LTRACEF("cpu_num %u, apic_id %u\n", cpu_num, apic_id);
19+
20+
// XXX do work here
21+
}
22+
23+
struct detected_cpus {
24+
uint32_t num_detected;
25+
uint32_t apic_ids[SMP_MAX_CPUS];
26+
};
27+
28+
static void local_apic_callback(const void *_entry, size_t entry_len, void *cookie) {
29+
const struct acpi_madt_local_apic_entry *entry = _entry;
30+
struct detected_cpus *cpus = cookie;
31+
32+
if (entry->apic_id == 0) {
33+
// skip the boot cpu
34+
return;
35+
}
36+
if (cpus->num_detected < SMP_MAX_CPUS) {
37+
cpus->apic_ids[cpus->num_detected++] = entry->apic_id;
38+
}
39+
}
40+
41+
void platform_start_secondary_cpus(void) {
42+
struct detected_cpus cpus;
43+
cpus.num_detected = 1;
44+
cpus.apic_ids[0] = 0; // the boot cpu
45+
46+
acpi_process_madt_entries_etc(ACPI_MADT_TYPE_LOCAL_APIC, &local_apic_callback, &cpus);
47+
48+
// TODO: fall back to legacy methods if ACPI fails
49+
// TODO: deal with cpu topology
50+
51+
// start up the secondary cpus
52+
if (cpus.num_detected > 1) {
53+
dprintf(INFO, "PC: detected %u cpus\n", cpus.num_detected);
54+
55+
lk_init_secondary_cpus(cpus.num_detected - 1);
56+
57+
for (uint i = 1; i < cpus.num_detected; i++) {
58+
dprintf(INFO, "PC: starting cpu %u\n", cpus.apic_ids[i]);
59+
start_cpu(i, cpus.apic_ids[i]);
60+
}
61+
}
62+
}
63+

platform/pc/platform.c

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <lk/trace.h>
1414
#include <arch/x86/mmu.h>
1515
#include <platform.h>
16-
#include "platform_p.h"
1716
#include <platform/pc.h>
1817
#include <platform/console.h>
1918
#include <platform/keyboard.h>
@@ -22,12 +21,13 @@
2221
#include <arch/x86.h>
2322
#include <arch/mmu.h>
2423
#include <malloc.h>
25-
#include <string.h>
2624
#include <assert.h>
2725
#include <inttypes.h>
2826
#include <kernel/vm.h>
2927
#include <lib/acpi_lite.h>
3028

29+
#include "platform_p.h"
30+
3131
#if WITH_DEV_BUS_PCI
3232
#include <dev/bus/pci.h>
3333
#endif
@@ -218,44 +218,28 @@ void platform_early_init(void) {
218218
dprintf(INFO, "PC: total memory detected %" PRIu64 " bytes\n", total_mem);
219219
}
220220

221-
void local_apic_callback(const void *_entry, size_t entry_len) {
222-
const struct acpi_madt_local_apic_entry *entry = _entry;
223-
224-
printf("\tLOCAL APIC id %d, processor id %d, flags %#x\n",
225-
entry->apic_id, entry->processor_id, entry->flags);
226-
}
227-
228-
void io_apic_callback(const void *_entry, size_t entry_len) {
229-
const struct acpi_madt_io_apic_entry *entry = _entry;
230-
231-
printf("\tIO APIC id %d, address %#x gsi base %u\n",
232-
entry->io_apic_id, entry->io_apic_address, entry->global_system_interrupt_base);
233-
}
234-
235-
void int_source_override_callback(const void *_entry, size_t entry_len) {
236-
const struct acpi_madt_int_source_override_entry *entry = _entry;
237-
238-
printf("\tINT OVERRIDE bus %u, source %u, gsi %u, flags %#x\n",
239-
entry->bus, entry->source, entry->global_sys_interrupt, entry->flags);
240-
}
241-
242221
void platform_init(void) {
243222
platform_init_debug();
244223

245224
platform_init_keyboard(&console_input_buf);
246225

247-
#if WITH_DEV_BUS_PCI
248-
bool pci_initted = false;
226+
// Look for the root ACPI table
227+
bool found_acpi = false;
249228
if (acpi_lite_init(0) == NO_ERROR) {
250229
if (LOCAL_TRACE) {
251230
acpi_lite_dump_tables(false);
252231
}
232+
acpi_lite_dump_madt_table();
233+
found_acpi = true;
234+
}
235+
236+
// Look for secondary cpus
237+
platform_start_secondary_cpus();
253238

254-
// dump the APIC table
255-
printf("MADT/APIC table:\n");
256-
acpi_process_madt_entries_etc(ACPI_MADT_TYPE_LOCAL_APIC, &local_apic_callback);
257-
acpi_process_madt_entries_etc(ACPI_MADT_TYPE_IO_APIC, &io_apic_callback);
258-
acpi_process_madt_entries_etc(ACPI_MADT_TYPE_INT_SOURCE_OVERRIDE, &int_source_override_callback);
239+
#if WITH_DEV_BUS_PCI
240+
bool pci_initted = false;
241+
if (found_acpi) {
242+
// TODO: handle interrupt source overrides from the MADT table
259243

260244
// try to find the mcfg table
261245
const struct acpi_mcfg_table *table = (const struct acpi_mcfg_table *)acpi_get_table_by_sig(ACPI_MCFG_SIG);

platform/pc/platform_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ void pic_mask_interrupts(void);
2626
void lapic_init(void);
2727
void lapic_eoi(unsigned int vector);
2828

29+
// secondary cpus
30+
void platform_start_secondary_cpus(void);

platform/pc/rules.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ MODULE_SRCS += \
2323
$(LOCAL_DIR)/interrupts.c \
2424
$(LOCAL_DIR)/keyboard.c \
2525
$(LOCAL_DIR)/lapic.c \
26+
$(LOCAL_DIR)/mp.c \
2627
$(LOCAL_DIR)/pic.c \
2728
$(LOCAL_DIR)/platform.c \
2829
$(LOCAL_DIR)/timer.c \

0 commit comments

Comments
 (0)