From c481fb1ee777cc581d2b764311a551e4cf0d4052 Mon Sep 17 00:00:00 2001 From: Pawel Wieczorkiewicz Date: Tue, 7 Nov 2023 11:48:10 +0100 Subject: [PATCH] cpu: refactor cpu_t structure Add cpu_flags_t structure to handle all CPU specific flags. Signed-off-by: Pawel Wieczorkiewicz --- common/acpi.c | 12 ++++++------ common/cpu.c | 6 +++--- common/sched.c | 4 ++-- common/setup.c | 2 +- include/cpu.h | 25 +++++++++++++++++-------- smp/mptables.c | 2 +- smp/smp.c | 2 +- 7 files changed, 31 insertions(+), 22 deletions(-) diff --git a/common/acpi.c b/common/acpi.c index 6f487655..f036fcd1 100644 --- a/common/acpi.c +++ b/common/acpi.c @@ -268,7 +268,7 @@ static int process_madt_entries(void) { cpu_t *cpu = get_cpu(madt_cpu->apic_proc_id) ?: add_cpu(madt_cpu->apic_proc_id, false, enabled); - cpu->enabled = enabled; + cpu->flags.enabled = enabled; percpu_t *percpu = cpu->percpu; percpu->cpu_id = madt_cpu->apic_proc_id; @@ -498,7 +498,7 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) { cpu_t *cpu = get_cpu(lapic->ProcessorId) ?: add_cpu(lapic->ProcessorId, false, enabled); - cpu->enabled = enabled; + cpu->flags.enabled = enabled; percpu_t *percpu = cpu->percpu; percpu->apic_id = lapic->Id; @@ -602,7 +602,7 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) { cpu_t *cpu = get_cpu(slapic->ProcessorId) ?: add_cpu(slapic->ProcessorId, false, enabled); - cpu->enabled = enabled; + cpu->flags.enabled = enabled; percpu_t *percpu = cpu->percpu; percpu->sapic_id = slapic->Id; @@ -610,7 +610,7 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) { percpu->sapic_uid = slapic->Uid; percpu->sapic_uid_str[0] = slapic->UidString[0]; - if (cpu->enabled) { + if (is_cpu_enabled(cpu)) { printk("ACPI: [MADT] SAPIC Processor ID: %u, SAPIC ID: %u, SAPIC EID: %u, " "SAPIC UID: %u, SAPIC UID Str: %c Flags: %08x\n", cpu->id, slapic->Id, slapic->Eid, slapic->Uid, slapic->UidString[0], @@ -627,12 +627,12 @@ static void madt_parser(ACPI_SUBTABLE_HEADER *entry, void *arg) { bool enabled = !!(x2lapic->LapicFlags & 0x1); cpu_t *cpu = get_cpu(x2lapic->Uid) ?: add_cpu(x2lapic->Uid, false, enabled); - cpu->enabled = enabled; + cpu->flags.enabled = enabled; percpu_t *percpu = cpu->percpu; percpu->apic_id = x2lapic->LocalApicId; - if (cpu->enabled) { + if (is_cpu_enabled(cpu)) { printk("ACPI: [MADT] X2APIC Processor ID: %u, APIC ID: %u, Flags: %08x\n", cpu->id, percpu->apic_id, x2lapic->LapicFlags); } diff --git a/common/cpu.c b/common/cpu.c index 7f01cca4..1bd0dc85 100644 --- a/common/cpu.c +++ b/common/cpu.c @@ -40,8 +40,8 @@ static cpu_t bsp = {0}; static void init_cpu(cpu_t *cpu, unsigned int id, bool is_bsp, bool enabled) { memset(cpu, 0, sizeof(*cpu)); cpu->id = id; - cpu->bsp = is_bsp; - cpu->enabled = enabled; + cpu->flags.bsp = is_bsp; + cpu->flags.enabled = enabled; init_cpu_runstate(cpu); if (is_bsp) @@ -131,7 +131,7 @@ void wait_for_all_cpus(void) { cpu_t *cpu; list_for_each_entry (cpu, &cpus, list) { - if (cpu->bsp) + if (is_cpu_bsp(cpu)) continue; while (!is_cpu_finished(cpu) || !list_is_empty(&cpu->task_queue)) diff --git a/common/sched.c b/common/sched.c index 485ba0c3..34e9e94d 100644 --- a/common/sched.c +++ b/common/sched.c @@ -252,7 +252,7 @@ void process_task_repeat(task_t *task) { void run_tasks(cpu_t *cpu) { task_t *task, *safe; - if (!cpu->bsp) + if (!is_cpu_bsp(cpu)) wait_cpu_unblocked(cpu); set_cpu_unfinished(cpu); @@ -272,7 +272,7 @@ void run_tasks(cpu_t *cpu) { } } while (!list_is_empty(&cpu->task_queue)); - if (!cpu->bsp) + if (!is_cpu_bsp(cpu)) set_cpu_blocked(cpu); set_cpu_finished(cpu); } diff --git a/common/setup.c b/common/setup.c index 1cd2d060..0976a787 100644 --- a/common/setup.c +++ b/common/setup.c @@ -167,7 +167,7 @@ static void __text_init init_vga_console(void) { void __text_init init_timers(cpu_t *cpu) { static bool __data_init hpet_initialized = false; - if (cpu->bsp) { + if (is_cpu_bsp(cpu)) { if (opt_hpet) hpet_initialized = init_hpet(cpu); diff --git a/include/cpu.h b/include/cpu.h index ed979bfb..31b39d2f 100644 --- a/include/cpu.h +++ b/include/cpu.h @@ -35,19 +35,20 @@ #define CPU_UNBLOCKED (1 << 0) #define CPU_FINISHED (1 << 1) +struct cpu_flags { + uint64_t bsp : 1, enabled : 1, rsvd : 62; +}; +typedef struct cpu_flags cpu_flags_t; + struct cpu { list_head_t list; - - unsigned int id; - unsigned int bsp : 1, enabled : 1; - - atomic_t run_state; - percpu_t *percpu; - spinlock_t lock; - list_head_t task_queue; + atomic_t run_state; + + unsigned int id; + cpu_flags_t flags; }; typedef struct cpu cpu_t; @@ -66,6 +67,14 @@ extern void wait_for_all_cpus(void); /* Static declarations */ +static inline bool is_cpu_bsp(cpu_t *cpu) { + return cpu->flags.bsp; +} + +static inline bool is_cpu_enabled(cpu_t *cpu) { + return cpu->flags.enabled; +} + static inline void init_cpu_runstate(cpu_t *cpu) { atomic_set(&cpu->run_state, 0); } diff --git a/smp/mptables.c b/smp/mptables.c index 70eafb0e..5b9f3316 100644 --- a/smp/mptables.c +++ b/smp/mptables.c @@ -223,7 +223,7 @@ static void process_mpc_entries(mpc_hdr_t *mpc_ptr) { cpu = get_cpu(mpc_cpu->lapic_id) ?: add_cpu(mpc_cpu->lapic_id, false, enabled); - cpu->enabled = enabled; + cpu->flags.enabled = enabled; percpu_t *percpu = cpu->percpu; percpu->apic_id = mpc_cpu->lapic_id; diff --git a/smp/smp.c b/smp/smp.c index fc0ef959..4aa5147d 100644 --- a/smp/smp.c +++ b/smp/smp.c @@ -75,7 +75,7 @@ static __text_init void boot_cpu(cpu_t *cpu) { percpu_t *percpu = cpu->percpu; apic_icr_t icr; - if (cpu->bsp) + if (is_cpu_bsp(cpu)) return; ap_new_sp = get_free_pages_top(PAGE_ORDER_2M, GFP_KERNEL_MAP);