blob: 185d993c95e3a144e3da5d4309b6d25f95bb0b29 [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
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010012/* The stack to be used by the CPUs. */
Andrew Scullf3d45592018-09-20 14:30:22 +010013alignas(2 * sizeof(size_t)) static char callstacks[MAX_CPUS][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010014
15/* State of all supported CPUs. The stack of the first one is initialized. */
16struct cpu cpus[MAX_CPUS] = {
17 {
18 .is_on = 1,
Andrew Scullf3d45592018-09-20 14:30:22 +010019 .stack_bottom = &callstacks[0][STACK_SIZE],
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010020 },
21};
22
23void cpu_module_init(void)
24{
25 size_t i;
26
27 /* Initialize all CPUs. */
28 for (i = 0; i < MAX_CPUS; i++) {
Andrew Scullf3d45592018-09-20 14:30:22 +010029 struct cpu *c = &cpus[i];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010030 cpu_init(c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010031 c->id = i; /* TODO: Initialize ID based on fdt. */
Andrew Scullf3d45592018-09-20 14:30:22 +010032 c->stack_bottom = &callstacks[i][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010033 }
34}
35
36size_t cpu_index(struct cpu *c)
37{
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010038 return c - cpus;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010039}
40
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010041void cpu_init(struct cpu *c)
42{
43 /* TODO: Assumes that c is zeroed out already. */
44 sl_init(&c->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010045 c->irq_disable_count = 1;
46}
47
48void cpu_irq_enable(struct cpu *c)
49{
50 c->irq_disable_count--;
Andrew Scull7364a8e2018-07-19 15:39:29 +010051 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010052 arch_irq_enable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010053 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010054}
55
56void cpu_irq_disable(struct cpu *c)
57{
Andrew Scull7364a8e2018-07-19 15:39:29 +010058 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010059 arch_irq_disable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010060 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010061 c->irq_disable_count++;
62}
63
Wedson Almeida Filho87009642018-07-02 10:20:07 +010064/**
65 * Turns CPU on and returns the previous state.
66 */
Andrew Scull1b8d0442018-08-06 15:47:04 +010067bool cpu_on(struct cpu *c, ipaddr_t entry, size_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010068{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010069 bool prev;
70
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010071 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010072 prev = c->is_on;
73 c->is_on = true;
74 sl_unlock(&c->lock);
75
76 if (!prev) {
Andrew Scullf3d45592018-09-20 14:30:22 +010077 struct vcpu *vcpu = &primary_vm.vcpus[cpu_index(c)];
Andrew Scull89a75242018-08-06 17:04:55 +010078 arch_regs_init(&vcpu->regs, entry, arg);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010079 vcpu_on(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010080 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010081
82 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010083}
84
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010085/**
86 * Prepares the CPU for turning itself off.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010087 */
88void cpu_off(struct cpu *c)
89{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010090 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010091 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010092 sl_unlock(&c->lock);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010093}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010094
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010095/**
96 * Searches for a CPU based on its id.
97 */
98struct cpu *cpu_find(size_t id)
99{
100 size_t i;
101
102 for (i = 0; i < MAX_CPUS; i++) {
103 if (cpus[i].id == id) {
Andrew Scullf3d45592018-09-20 14:30:22 +0100104 return &cpus[i];
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100105 }
106 }
107
108 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100109}
110
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100111void vcpu_init(struct vcpu *vcpu, struct vm *vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100112{
113 memset(vcpu, 0, sizeof(*vcpu));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100114 sl_init(&vcpu->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100115 vcpu->vm = vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100116 vcpu->state = vcpu_state_off;
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100117 /* TODO: This needs to be moved to arch-dependent code. */
118 vcpu->regs.lazy.vmpidr_el2 = vcpu - vm->vcpus;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100119}
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100120
121void vcpu_on(struct vcpu *vcpu)
122{
123 sl_lock(&vcpu->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100124 vcpu->state = vcpu_state_ready;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100125 sl_unlock(&vcpu->lock);
126}
127
128void vcpu_off(struct vcpu *vcpu)
129{
130 sl_lock(&vcpu->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100131 vcpu->state = vcpu_state_off;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100132 sl_unlock(&vcpu->lock);
133}