Skip to content

Commit

Permalink
smp,drivers,arch: use new CPU API for using detected CPUs
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Jul 8, 2022
1 parent a080eca commit c2e042e
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 122 deletions.
16 changes: 10 additions & 6 deletions arch/x86/apic.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
#include <apic.h>
#include <console.h>
#include <cpu.h>
#include <drivers/pit.h>
#include <ktf.h>
#include <lib.h>
Expand Down Expand Up @@ -107,10 +108,10 @@ void apic_icr_write(const apic_icr_t *icr) {

apic_mode_t apic_get_mode(void) { return apic_mode; }

void init_apic(unsigned int cpu, apic_mode_t mode) {
percpu_t *percpu = get_percpu_page(cpu);
void init_apic(unsigned int cpu_id, apic_mode_t mode) {
apic_base_t apic_base;
apic_spiv_t spiv;
cpu_t *cpu;

BUG_ON(mode < APIC_MODE_DISABLED);

Expand All @@ -128,8 +129,8 @@ void init_apic(unsigned int cpu, apic_mode_t mode) {
BUG();
}

printk("CPU%u: Initializing APIC mode: %s -> %s\n", cpu, apic_mode_names[apic_mode],
apic_mode_names[mode]);
printk("CPU%u: Initializing APIC mode: %s -> %s\n", cpu_id,
apic_mode_names[apic_mode], apic_mode_names[mode]);

/* Disable APIC */
apic_base.en = apic_base.extd = 0;
Expand All @@ -146,8 +147,11 @@ void init_apic(unsigned int cpu, apic_mode_t mode) {
}

apic_mode = mode;
percpu->apic_base = apic_base;
PERCPU_SET(bsp, apic_base.bsp);

cpu = !!apic_base.bsp ? get_bsp_cpu() : get_cpu(cpu_id);
BUG_ON(!cpu || cpu->id != cpu_id);

cpu->percpu->apic_base = apic_base;

/* XAPIC requires MMIO accesses, thus the APIC_BASE page needs to be mapped.
* X2APIC uses MSRs for accesses, so no mapping needed.
Expand Down
6 changes: 3 additions & 3 deletions arch/x86/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ static void init_gdt(percpu_t *percpu) {
init_tss(percpu);
}

void init_traps(unsigned int cpu) {
percpu_t *percpu = get_percpu_page(cpu);
void init_traps(const cpu_t *cpu) {
percpu_t *percpu = cpu->percpu;

BUG_ON(!percpu);

Expand Down Expand Up @@ -175,7 +175,7 @@ void init_traps(unsigned int cpu) {

init_gdt(percpu);

wrmsr(MSR_TSC_AUX, cpu);
wrmsr(MSR_TSC_AUX, cpu->id);
}

static void dump_general_regs(const struct cpu_regs *regs) {
Expand Down
83 changes: 37 additions & 46 deletions common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <acpi_ktf.h>
#include <cpu.h>
#include <ioapic.h>
#include <percpu.h>
#include <setup.h>
#include <string.h>

Expand All @@ -46,10 +46,6 @@ static const char *madt_int_trigger_names[] = {
[ACPI_MADT_INT_TRIGGER_LT] = "Level",
};

static unsigned nr_cpus;

unsigned acpi_get_nr_cpus(void) { return nr_cpus; }

#ifndef KTF_ACPICA
#include <errno.h>
#include <page.h>
Expand Down Expand Up @@ -232,7 +228,7 @@ static int process_fadt(void) {
return 0;
}

static int process_madt_entries(unsigned bsp_cpu_id) {
static int process_madt_entries(void) {
acpi_madt_t *madt = (acpi_madt_t *) acpi_find_table(MADT_SIGNATURE);
acpi_madt_entry_t *entry;
bus_t *isa_bus = NULL;
Expand All @@ -257,23 +253,23 @@ static int process_madt_entries(unsigned bsp_cpu_id) {
switch (entry->type) {
case ACPI_MADT_TYPE_LAPIC: {
acpi_madt_processor_t *madt_cpu = (acpi_madt_processor_t *) entry->data;
percpu_t *percpu;
bool enabled;

/* Some systems report all CPUs, marked as disabled */
enabled = !!(madt_cpu->flags & 0x1);
if (!enabled)
break;

percpu = get_percpu_page(madt_cpu->apic_proc_id);
cpu_t *cpu = get_cpu(madt_cpu->apic_proc_id)
?: add_cpu(madt_cpu->apic_proc_id, false, enabled);
cpu->enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->cpu_id = madt_cpu->apic_proc_id;
percpu->apic_id = madt_cpu->apic_id;
percpu->bsp = !!(madt_cpu->apic_proc_id == bsp_cpu_id);
percpu->enabled = enabled;

nr_cpus++;
printk("ACPI: [MADT] APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n",
madt_cpu->apic_proc_id, madt_cpu->apic_id, madt_cpu->flags);
cpu->id, percpu->apic_id, madt_cpu->flags);
break;
}
case ACPI_MADT_TYPE_IOAPIC: {
Expand Down Expand Up @@ -383,7 +379,7 @@ acpi_table_t *acpi_find_table(uint32_t signature) {
return NULL;
}

int init_acpi(unsigned bsp_cpu_id) {
int init_acpi(void) {
unsigned acpi_nr_tables;
rsdt_t *rsdt = NULL;
xsdt_t *xsdt = NULL;
Expand Down Expand Up @@ -437,7 +433,7 @@ int init_acpi(unsigned bsp_cpu_id) {
if (rc < 0)
return rc;

return process_madt_entries(bsp_cpu_id);
return process_madt_entries();
}
#else /* KTF_ACPICA */
#include "acpi.h"
Expand Down Expand Up @@ -486,24 +482,22 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {
switch (entry->Type) {
case ACPI_MADT_TYPE_LOCAL_APIC: {
ACPI_MADT_LOCAL_APIC *lapic = (ACPI_MADT_LOCAL_APIC *) entry;
uint32_t bsp_cpu_id = (uint32_t) _ul(arg);
percpu_t *percpu;
bool enabled;

/* Some systems report all CPUs, marked as disabled */
enabled = !!(lapic->LapicFlags & 0x1);
if (!enabled)
break;

percpu = get_percpu_page(lapic->ProcessorId);
percpu->cpu_id = lapic->ProcessorId;
cpu_t *cpu =
get_cpu(lapic->ProcessorId) ?: add_cpu(lapic->ProcessorId, false, enabled);
cpu->enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->apic_id = lapic->Id;
percpu->bsp = !!(lapic->ProcessorId == bsp_cpu_id);
percpu->enabled = enabled;

nr_cpus++;
printk("ACPI: [MADT] APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n",
percpu->cpu_id, percpu->apic_id, lapic->LapicFlags);
printk("ACPI: [MADT] APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n", cpu->id,
percpu->apic_id, lapic->LapicFlags);
break;
}
case ACPI_MADT_TYPE_IO_APIC: {
Expand Down Expand Up @@ -597,25 +591,23 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {
}
case ACPI_MADT_TYPE_LOCAL_SAPIC: {
ACPI_MADT_LOCAL_SAPIC *slapic = (ACPI_MADT_LOCAL_SAPIC *) entry;
percpu_t *percpu = get_percpu_page(slapic->ProcessorId);
uint32_t bsp_cpu_id = (uint32_t) _ul(arg);
bool enabled = !!(slapic->LapicFlags & 0x1);
cpu_t *cpu =
get_cpu(slapic->ProcessorId) ?: add_cpu(slapic->ProcessorId, false, enabled);

percpu->cpu_id = slapic->ProcessorId;
cpu->enabled = enabled;

percpu_t *percpu = cpu->percpu;
percpu->sapic_id = slapic->Id;
percpu->sapic_eid = slapic->Eid;

percpu->sapic_uid = slapic->Uid;
percpu->sapic_uid_str[0] = slapic->UidString[0];

percpu->bsp = !!(slapic->ProcessorId == bsp_cpu_id);
percpu->enabled = !!(slapic->LapicFlags & 0x1);

if (percpu->enabled) {
nr_cpus++;
if (cpu->enabled) {
printk("ACPI: [MADT] SAPIC Processor ID: %u, SAPIC ID: %u, SAPIC EID: %u, "
"SAPIC UID: %u, SAPIC UID Str: %c Flags: %08x\n",
percpu->cpu_id, slapic->Id, slapic->Eid, slapic->Uid,
slapic->UidString[0], slapic->LapicFlags);
cpu->id, slapic->Id, slapic->Eid, slapic->Uid, slapic->UidString[0],
slapic->LapicFlags);
}
break;
}
Expand All @@ -625,18 +617,17 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) {
}
case ACPI_MADT_TYPE_LOCAL_X2APIC: {
ACPI_MADT_LOCAL_X2APIC *x2lapic = (ACPI_MADT_LOCAL_X2APIC *) entry;
percpu_t *percpu = get_percpu_page(x2lapic->Uid);
uint32_t bsp_cpu_id = (uint32_t) _ul(arg);
bool enabled = !!(x2lapic->LapicFlags & 0x1);
cpu_t *cpu = get_cpu(x2lapic->Uid) ?: add_cpu(x2lapic->Uid, false, enabled);

cpu->enabled = enabled;

percpu->cpu_id = x2lapic->Uid;
percpu_t *percpu = cpu->percpu;
percpu->apic_id = x2lapic->LocalApicId;
percpu->bsp = !!(x2lapic->Uid == bsp_cpu_id);
percpu->enabled = !!(x2lapic->LapicFlags & 0x1);

if (percpu->enabled) {
nr_cpus++;
if (cpu->enabled) {
printk("ACPI: [MADT] X2APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n",
percpu->cpu_id, percpu->apic_id, x2lapic->LapicFlags);
cpu->id, percpu->apic_id, x2lapic->LapicFlags);
}
break;
}
Expand Down Expand Up @@ -703,7 +694,7 @@ static ACPI_STATUS init_fadt(void) {
return AE_OK;
}

static ACPI_STATUS init_madt(unsigned bsp_cpu_id) {
static ACPI_STATUS init_madt(void) {
ACPI_TABLE_MADT *madt = acpi_find_table(ACPI_SIG_MADT);
ACPI_SUBTABLE_HEADER *subtbl = (void *) madt + sizeof(*madt);

Expand All @@ -712,7 +703,7 @@ static ACPI_STATUS init_madt(unsigned bsp_cpu_id) {

uint32_t length = madt->Header.Length - sizeof(*madt);

acpi_walk_subtables(subtbl, length, madt_parser, (void *) _ul(bsp_cpu_id));
acpi_walk_subtables(subtbl, length, madt_parser, NULL);
return AE_OK;
}

Expand All @@ -733,7 +724,7 @@ void acpi_walk_subtables(ACPI_SUBTABLE_HEADER *entry, uint32_t length,
}
}

ACPI_STATUS init_acpi(unsigned bsp_cpu_id) {
ACPI_STATUS init_acpi(void) {
ACPI_STATUS status;

printk("Initializing ACPI support\n");
Expand All @@ -746,7 +737,7 @@ ACPI_STATUS init_acpi(unsigned bsp_cpu_id) {
if (status != AE_OK)
return status;

status = init_madt(bsp_cpu_id);
status = init_madt();
return status;
}

Expand Down
24 changes: 11 additions & 13 deletions common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <apic.h>
#include <cmdline.h>
#include <console.h>
#include <cpu.h>
#include <cpuid.h>
#include <drivers/keyboard.h>
#include <ioapic.h>
Expand Down Expand Up @@ -63,11 +64,6 @@

boot_flags_t boot_flags;

static unsigned bsp_cpu_id = 0;

unsigned get_bsp_cpu_id(void) { return bsp_cpu_id; }
void set_bsp_cpu_id(unsigned cpu_id) { bsp_cpu_id = cpu_id; }

#define QEMU_CONSOLE_PORT 0x0e9

static void __text_init init_console(void) {
Expand Down Expand Up @@ -204,24 +200,26 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic,

init_percpu();

init_traps(get_bsp_cpu_id());
cpu_t *bsp = init_cpus();

init_traps(bsp);

init_extables();

init_slab();

/* Try to initialize ACPI (and MADT) */
#ifndef KTF_ACPICA
if (init_acpi(get_bsp_cpu_id()) < 0) {
if (init_acpi() < 0) {
#else
if (ACPI_FAILURE(init_acpi(get_bsp_cpu_id()))) {
if (ACPI_FAILURE(init_acpi())) {
#endif
/* Fallback to MP tables when no ACPI */
if (init_mptables() < 0)
BUG();
}

init_apic(get_bsp_cpu_id(), APIC_MODE_XAPIC);
init_apic(bsp->id, APIC_MODE_XAPIC);

init_tasks();

Expand All @@ -232,20 +230,20 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic,
init_pci();

/* Initialize console input */
init_uart_input(get_bsp_cpu_id());
init_uart_input(bsp);

/* Initialize timers */
bool hpet_initialized = false;
if (opt_hpet)
hpet_initialized = init_hpet(get_bsp_cpu_id());
hpet_initialized = init_hpet(bsp);
if (!hpet_initialized && opt_pit)
init_pit(get_bsp_cpu_id());
init_pit(bsp);
if (opt_apic_timer)
init_apic_timer();

/* Initialize keyboard */
if (opt_keyboard)
init_keyboard(get_bsp_cpu_id());
init_keyboard(bsp);

if (opt_fpu) {
printk("Enabling FPU instructions support\n");
Expand Down
Loading

0 comments on commit c2e042e

Please sign in to comment.