Skip to content

Commit

Permalink
cpu: refactor cpu_t structure
Browse files Browse the repository at this point in the history
Add cpu_flags_t structure to handle all CPU specific flags.

Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Nov 13, 2023
1 parent 447dd9c commit c481fb1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
12 changes: 6 additions & 6 deletions common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -602,15 +602,15 @@ 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;
percpu->sapic_eid = slapic->Eid;
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],
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions common/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions common/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
25 changes: 17 additions & 8 deletions include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion smp/mptables.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion smp/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c481fb1

Please sign in to comment.