blob: 1ae6cd1bb7f7ee0de5c3d52cb18374d097144925 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/cpu.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
Andrew Scull04502e42018-09-03 14:54:52 +010019#include <stdalign.h>
20
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/arch/cpu.h"
22
23#include "hf/api.h"
24#include "hf/dlog.h"
25#include "hf/std.h"
26#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027
Andrew Scull19503262018-09-20 14:48:39 +010028#include "vmapi/hf/call.h"
29
Andrew Scull23e93a82018-10-26 14:56:04 +010030#define STACK_SIZE PAGE_SIZE
31
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010032/* The stack to be used by the CPUs. */
Andrew Scull37402872018-10-24 14:23:06 +010033alignas(2 * sizeof(uintreg_t)) static char callstacks[MAX_CPUS][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010034
35/* State of all supported CPUs. The stack of the first one is initialized. */
36struct cpu cpus[MAX_CPUS] = {
37 {
38 .is_on = 1,
Andrew Scullf3d45592018-09-20 14:30:22 +010039 .stack_bottom = &callstacks[0][STACK_SIZE],
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010040 },
41};
42
43void cpu_module_init(void)
44{
45 size_t i;
46
47 /* Initialize all CPUs. */
48 for (i = 0; i < MAX_CPUS; i++) {
Andrew Scullf3d45592018-09-20 14:30:22 +010049 struct cpu *c = &cpus[i];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010050 cpu_init(c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010051 c->id = i; /* TODO: Initialize ID based on fdt. */
Andrew Scullf3d45592018-09-20 14:30:22 +010052 c->stack_bottom = &callstacks[i][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010053 }
54}
55
56size_t cpu_index(struct cpu *c)
57{
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010058 return c - cpus;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010059}
60
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010061void cpu_init(struct cpu *c)
62{
63 /* TODO: Assumes that c is zeroed out already. */
64 sl_init(&c->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010065 c->irq_disable_count = 1;
66}
67
68void cpu_irq_enable(struct cpu *c)
69{
70 c->irq_disable_count--;
Andrew Scull7364a8e2018-07-19 15:39:29 +010071 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010072 arch_irq_enable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010073 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010074}
75
76void cpu_irq_disable(struct cpu *c)
77{
Andrew Scull7364a8e2018-07-19 15:39:29 +010078 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010079 arch_irq_disable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010080 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010081 c->irq_disable_count++;
82}
83
Wedson Almeida Filho87009642018-07-02 10:20:07 +010084/**
85 * Turns CPU on and returns the previous state.
86 */
Andrew Scull37402872018-10-24 14:23:06 +010087bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010088{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010089 bool prev;
90
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010091 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010092 prev = c->is_on;
93 c->is_on = true;
94 sl_unlock(&c->lock);
95
96 if (!prev) {
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +010097 struct vm *vm = vm_get(HF_PRIMARY_VM_ID);
98 struct vcpu *vcpu = &vm->vcpus[cpu_index(c)];
Wedson Almeida Filho03306112018-11-26 00:08:03 +000099 arch_regs_set_pc_arg(&vcpu->regs, entry, arg);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100100 vcpu_on(vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100101 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100102
103 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100104}
105
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100106/**
107 * Prepares the CPU for turning itself off.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100108 */
109void cpu_off(struct cpu *c)
110{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100111 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100112 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100113 sl_unlock(&c->lock);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100114}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100115
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100116/**
117 * Searches for a CPU based on its id.
118 */
119struct cpu *cpu_find(size_t id)
120{
121 size_t i;
122
123 for (i = 0; i < MAX_CPUS; i++) {
124 if (cpus[i].id == id) {
Andrew Scullf3d45592018-09-20 14:30:22 +0100125 return &cpus[i];
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100126 }
127 }
128
129 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100130}
131
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100132void vcpu_init(struct vcpu *vcpu, struct vm *vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100133{
134 memset(vcpu, 0, sizeof(*vcpu));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100135 sl_init(&vcpu->lock);
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000136 vcpu->regs_available = true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100137 vcpu->vm = vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100138 vcpu->state = vcpu_state_off;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000139 arch_regs_init(&vcpu->regs, vm->id == HF_PRIMARY_VM_ID, vm->id,
140 vm->ptable.root, vcpu_index(vcpu));
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100141}
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100142
143void vcpu_on(struct vcpu *vcpu)
144{
145 sl_lock(&vcpu->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100146 vcpu->state = vcpu_state_ready;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100147 sl_unlock(&vcpu->lock);
148}
149
150void vcpu_off(struct vcpu *vcpu)
151{
152 sl_lock(&vcpu->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100153 vcpu->state = vcpu_state_off;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100154 sl_unlock(&vcpu->lock);
155}
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000156
157size_t vcpu_index(struct vcpu *vcpu)
158{
159 return vcpu - vcpu->vm->vcpus;
160}