blob: c31ad0f47114144e9562728df319b2c8b534ea8b [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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/api.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010022#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/dlog.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010024
Andrew Scull19503262018-09-20 14:48:39 +010025#include "vmapi/hf/call.h"
26
Andrew Scull23e93a82018-10-26 14:56:04 +010027#define STACK_SIZE PAGE_SIZE
28
Andrew Sculleed724b2019-09-12 09:57:19 +010029/**
30 * The stacks to be used by the CPUs.
31 *
32 * Align to page boundaries to ensure that cache lines are not shared between a
33 * CPU's stack and data that can be accessed from other CPUs. If this did
34 * happen, there may be coherency problems when the stack is being used before
35 * caching is enabled.
36 */
37alignas(PAGE_SIZE) static char callstacks[MAX_CPUS][STACK_SIZE];
38
39/* NOLINTNEXTLINE(misc-redundant-expression) */
40static_assert((STACK_SIZE % PAGE_SIZE) == 0, "Keep each stack page aligned.");
41static_assert((PAGE_SIZE % STACK_ALIGN) == 0,
42 "Page alignment is too weak for the stack.");
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010043
Jose Marinho20713fa2019-08-07 15:42:07 +010044/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045 * Internal buffer used to store FF-A messages from a VM Tx. Its usage prevents
Jose Marinho20713fa2019-08-07 15:42:07 +010046 * TOCTOU issues while Hafnium performs actions on information that would
47 * otherwise be re-writable by the VM.
48 *
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000049 * Each buffer is owned by a single CPU. The buffer can only be used for
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010050 * ffa_msg_send. The information stored in the buffer is only valid during the
51 * ffa_msg_send request is performed.
Jose Marinho20713fa2019-08-07 15:42:07 +010052 */
Andrew Walbran0ec61592019-12-17 13:29:01 +000053alignas(PAGE_SIZE) static uint8_t cpu_message_buffer[MAX_CPUS][PAGE_SIZE];
Jose Marinho20713fa2019-08-07 15:42:07 +010054
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053055uint8_t *cpu_get_buffer(struct cpu *c)
Jose Marinho20713fa2019-08-07 15:42:07 +010056{
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053057 size_t cpu_indx = cpu_index(c);
Jose Marinho20713fa2019-08-07 15:42:07 +010058
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053059 CHECK(cpu_indx < MAX_CPUS);
60
61 return cpu_message_buffer[cpu_indx];
Jose Marinho20713fa2019-08-07 15:42:07 +010062}
63
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053064uint32_t cpu_get_buffer_size(struct cpu *c)
Jose Marinho20713fa2019-08-07 15:42:07 +010065{
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053066 size_t cpu_indx = cpu_index(c);
Jose Marinho20713fa2019-08-07 15:42:07 +010067
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053068 CHECK(cpu_indx < MAX_CPUS);
69
70 return sizeof(cpu_message_buffer[cpu_indx]);
Jose Marinho20713fa2019-08-07 15:42:07 +010071}
72
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010073/* State of all supported CPUs. The stack of the first one is initialized. */
74struct cpu cpus[MAX_CPUS] = {
75 {
76 .is_on = 1,
Andrew Scullf3d45592018-09-20 14:30:22 +010077 .stack_bottom = &callstacks[0][STACK_SIZE],
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010078 },
79};
80
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000081static uint32_t cpu_count = 1;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010082
Andrew Walbran4d3fa282019-06-26 13:31:15 +010083void cpu_module_init(const cpu_id_t *cpu_ids, size_t count)
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000084{
85 uint32_t i;
86 uint32_t j;
Andrew Walbran4d3fa282019-06-26 13:31:15 +010087 cpu_id_t boot_cpu_id = cpus[0].id;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000088 bool found_boot_cpu = false;
89
90 cpu_count = count;
91
92 /*
93 * Initialize CPUs with the IDs from the configuration passed in. The
94 * CPUs after the boot CPU are initialized in reverse order. The boot
95 * CPU is initialized when it is found or in place of the last CPU if it
96 * is not found.
97 */
98 j = cpu_count;
99 for (i = 0; i < cpu_count; ++i) {
100 struct cpu *c;
Andrew Walbran4d3fa282019-06-26 13:31:15 +0100101 cpu_id_t id = cpu_ids[i];
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000102
103 if (found_boot_cpu || id != boot_cpu_id) {
Andrew Scull48973982019-08-16 17:40:28 +0100104 --j;
105 c = &cpus[j];
106 c->stack_bottom = &callstacks[j][STACK_SIZE];
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000107 } else {
108 found_boot_cpu = true;
109 c = &cpus[0];
Andrew Scull48973982019-08-16 17:40:28 +0100110 CHECK(c->stack_bottom == &callstacks[0][STACK_SIZE]);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000111 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000112
Andrew Scull48973982019-08-16 17:40:28 +0100113 sl_init(&c->lock);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000114 c->id = id;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100115 }
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000116
117 if (!found_boot_cpu) {
118 /* Boot CPU was initialized but with wrong ID. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000119 dlog_warning("Boot CPU's ID not found in config.\n");
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000120 cpus[0].id = boot_cpu_id;
121 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100122}
123
124size_t cpu_index(struct cpu *c)
125{
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100126 return c - cpus;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100127}
128
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100129/**
130 * Turns CPU on and returns the previous state.
131 */
Andrew Scull37402872018-10-24 14:23:06 +0100132bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100133{
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100134 bool prev;
135
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100136 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100137 prev = c->is_on;
138 c->is_on = true;
139 sl_unlock(&c->lock);
140
141 if (!prev) {
Andrew Walbran42347a92019-05-09 13:59:03 +0100142 struct vm *vm = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100143 struct vcpu *vcpu = vm_get_vcpu(vm, cpu_index(c));
Andrew Walbranb58f8992019-04-15 12:29:31 +0100144 struct vcpu_locked vcpu_locked;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000145
Andrew Walbranb58f8992019-04-15 12:29:31 +0100146 vcpu_locked = vcpu_lock(vcpu);
147 vcpu_on(vcpu_locked, entry, arg);
148 vcpu_unlock(&vcpu_locked);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100149 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100150
151 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100152}
153
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100154/**
155 * Prepares the CPU for turning itself off.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100156 */
157void cpu_off(struct cpu *c)
158{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100159 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100160 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100161 sl_unlock(&c->lock);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100162}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100163
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100164/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000165 * Searches for a CPU based on its ID.
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100166 */
Andrew Walbran4d3fa282019-06-26 13:31:15 +0100167struct cpu *cpu_find(cpu_id_t id)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100168{
169 size_t i;
170
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000171 for (i = 0; i < cpu_count; i++) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100172 if (cpus[i].id == id) {
Andrew Scullf3d45592018-09-20 14:30:22 +0100173 return &cpus[i];
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100174 }
175 }
176
177 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100178}