blob: 2c09d8128b31da5474864db8aa0edd90ba1c282a [file] [log] [blame]
Andrew Scull18c78fc2018-08-20 12:57:41 +01001#include "hf/cpu.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01002
Andrew Scull04502e42018-09-03 14:54:52 +01003#include <stdalign.h>
4
Andrew Scull18c78fc2018-08-20 12:57:41 +01005#include "hf/arch/cpu.h"
6
7#include "hf/api.h"
8#include "hf/dlog.h"
9#include "hf/std.h"
10#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010011
Andrew Scull19503262018-09-20 14:48:39 +010012#include "vmapi/hf/call.h"
13
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010014/* The stack to be used by the CPUs. */
Andrew Scullf3d45592018-09-20 14:30:22 +010015alignas(2 * sizeof(size_t)) static char callstacks[MAX_CPUS][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010016
17/* State of all supported CPUs. The stack of the first one is initialized. */
18struct cpu cpus[MAX_CPUS] = {
19 {
20 .is_on = 1,
Andrew Scullf3d45592018-09-20 14:30:22 +010021 .stack_bottom = &callstacks[0][STACK_SIZE],
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010022 },
23};
24
25void cpu_module_init(void)
26{
27 size_t i;
28
29 /* Initialize all CPUs. */
30 for (i = 0; i < MAX_CPUS; i++) {
Andrew Scullf3d45592018-09-20 14:30:22 +010031 struct cpu *c = &cpus[i];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010032 cpu_init(c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010033 c->id = i; /* TODO: Initialize ID based on fdt. */
Andrew Scullf3d45592018-09-20 14:30:22 +010034 c->stack_bottom = &callstacks[i][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010035 }
36}
37
38size_t cpu_index(struct cpu *c)
39{
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010040 return c - cpus;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010041}
42
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010043void cpu_init(struct cpu *c)
44{
45 /* TODO: Assumes that c is zeroed out already. */
46 sl_init(&c->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010047 c->irq_disable_count = 1;
48}
49
50void cpu_irq_enable(struct cpu *c)
51{
52 c->irq_disable_count--;
Andrew Scull7364a8e2018-07-19 15:39:29 +010053 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010054 arch_irq_enable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010055 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010056}
57
58void cpu_irq_disable(struct cpu *c)
59{
Andrew Scull7364a8e2018-07-19 15:39:29 +010060 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010061 arch_irq_disable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010062 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010063 c->irq_disable_count++;
64}
65
Wedson Almeida Filho87009642018-07-02 10:20:07 +010066/**
67 * Turns CPU on and returns the previous state.
68 */
Andrew Scull1b8d0442018-08-06 15:47:04 +010069bool cpu_on(struct cpu *c, ipaddr_t entry, size_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010070{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010071 bool prev;
72
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010073 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010074 prev = c->is_on;
75 c->is_on = true;
76 sl_unlock(&c->lock);
77
78 if (!prev) {
Andrew Scull19503262018-09-20 14:48:39 +010079 struct vcpu *vcpu =
80 &vm_get(HF_PRIMARY_VM_ID)->vcpus[cpu_index(c)];
Andrew Scull89a75242018-08-06 17:04:55 +010081 arch_regs_init(&vcpu->regs, entry, arg);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010082 vcpu_on(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010083 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010084
85 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010086}
87
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010088/**
89 * Prepares the CPU for turning itself off.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010090 */
91void cpu_off(struct cpu *c)
92{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010093 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010094 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010095 sl_unlock(&c->lock);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010096}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010097
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010098/**
99 * Searches for a CPU based on its id.
100 */
101struct cpu *cpu_find(size_t id)
102{
103 size_t i;
104
105 for (i = 0; i < MAX_CPUS; i++) {
106 if (cpus[i].id == id) {
Andrew Scullf3d45592018-09-20 14:30:22 +0100107 return &cpus[i];
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100108 }
109 }
110
111 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100112}
113
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100114void vcpu_init(struct vcpu *vcpu, struct vm *vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100115{
116 memset(vcpu, 0, sizeof(*vcpu));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100117 sl_init(&vcpu->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100118 vcpu->vm = vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100119 vcpu->state = vcpu_state_off;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100120 /* TODO: This needs to be moved to arch-dependent code. */
121 vcpu->regs.lazy.vmpidr_el2 = vcpu - vm->vcpus;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100122}
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100123
124void vcpu_on(struct vcpu *vcpu)
125{
126 sl_lock(&vcpu->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100127 vcpu->state = vcpu_state_ready;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100128 sl_unlock(&vcpu->lock);
129}
130
131void vcpu_off(struct vcpu *vcpu)
132{
133 sl_lock(&vcpu->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100134 vcpu->state = vcpu_state_off;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100135 sl_unlock(&vcpu->lock);
136}