blob: b9ff6ce0a5d4a101410c2f40fe52c4599c66c705 [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 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/cpu.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010010
Andrew Scull04502e42018-09-03 14:54:52 +010011#include <stdalign.h>
12
Max Shvetsov9c0ebe42020-08-27 12:37:57 +010013#include "hf/arch/cache.h"
14
Andrew Scull18c78fc2018-08-20 12:57:41 +010015#include "hf/api.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010016#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/dlog.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
Andrew Scull19503262018-09-20 14:48:39 +010019#include "vmapi/hf/call.h"
20
Maksims Svecovs134b8f92022-03-04 15:14:09 +000021#include "system/sys/cdefs.h"
22
Andrew Sculleed724b2019-09-12 09:57:19 +010023/**
24 * The stacks to be used by the CPUs.
25 *
Kathleen Capella4df55202022-12-01 15:39:26 -050026 * Defined in assembly for aarch64 in "src/arch/aarch64/stacks.S."
27 * Defined for host-based unit tests in "src/cpu_test.cc".
Andrew Sculleed724b2019-09-12 09:57:19 +010028 */
Kathleen Capella4df55202022-12-01 15:39:26 -050029
30extern char callstacks[MAX_CPUS][STACK_SIZE];
Andrew Sculleed724b2019-09-12 09:57:19 +010031
32/* NOLINTNEXTLINE(misc-redundant-expression) */
33static_assert((STACK_SIZE % PAGE_SIZE) == 0, "Keep each stack page aligned.");
34static_assert((PAGE_SIZE % STACK_ALIGN) == 0,
35 "Page alignment is too weak for the stack.");
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010036
Jose Marinho20713fa2019-08-07 15:42:07 +010037/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010038 * Internal buffer used to store FF-A messages from a VM Tx. Its usage prevents
Jose Marinho20713fa2019-08-07 15:42:07 +010039 * TOCTOU issues while Hafnium performs actions on information that would
40 * otherwise be re-writable by the VM.
41 *
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000042 * Each buffer is owned by a single CPU. The buffer can only be used for
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043 * ffa_msg_send. The information stored in the buffer is only valid during the
44 * ffa_msg_send request is performed.
Jose Marinho20713fa2019-08-07 15:42:07 +010045 */
Andrew Walbran0ec61592019-12-17 13:29:01 +000046alignas(PAGE_SIZE) static uint8_t cpu_message_buffer[MAX_CPUS][PAGE_SIZE];
Jose Marinho20713fa2019-08-07 15:42:07 +010047
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053048uint8_t *cpu_get_buffer(struct cpu *c)
Jose Marinho20713fa2019-08-07 15:42:07 +010049{
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053050 size_t cpu_indx = cpu_index(c);
Jose Marinho20713fa2019-08-07 15:42:07 +010051
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053052 CHECK(cpu_indx < MAX_CPUS);
53
54 return cpu_message_buffer[cpu_indx];
Jose Marinho20713fa2019-08-07 15:42:07 +010055}
56
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053057uint32_t cpu_get_buffer_size(struct cpu *c)
Jose Marinho20713fa2019-08-07 15:42:07 +010058{
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053059 size_t cpu_indx = cpu_index(c);
Jose Marinho20713fa2019-08-07 15:42:07 +010060
Mahesh Bireddy8ca57862020-01-07 13:43:21 +053061 CHECK(cpu_indx < MAX_CPUS);
62
63 return sizeof(cpu_message_buffer[cpu_indx]);
Jose Marinho20713fa2019-08-07 15:42:07 +010064}
65
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010066/* State of all supported CPUs. The stack of the first one is initialized. */
67struct cpu cpus[MAX_CPUS] = {
68 {
69 .is_on = 1,
Andrew Scullf3d45592018-09-20 14:30:22 +010070 .stack_bottom = &callstacks[0][STACK_SIZE],
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010071 },
72};
73
Max Shvetsov9c0ebe42020-08-27 12:37:57 +010074uint32_t cpu_count = 1;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010075
Andrew Walbran4d3fa282019-06-26 13:31:15 +010076void cpu_module_init(const cpu_id_t *cpu_ids, size_t count)
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000077{
78 uint32_t i;
79 uint32_t j;
Andrew Walbran4d3fa282019-06-26 13:31:15 +010080 cpu_id_t boot_cpu_id = cpus[0].id;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000081 bool found_boot_cpu = false;
82
83 cpu_count = count;
84
85 /*
86 * Initialize CPUs with the IDs from the configuration passed in. The
87 * CPUs after the boot CPU are initialized in reverse order. The boot
88 * CPU is initialized when it is found or in place of the last CPU if it
89 * is not found.
90 */
91 j = cpu_count;
92 for (i = 0; i < cpu_count; ++i) {
93 struct cpu *c;
Andrew Walbran4d3fa282019-06-26 13:31:15 +010094 cpu_id_t id = cpu_ids[i];
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000095
96 if (found_boot_cpu || id != boot_cpu_id) {
Andrew Scull48973982019-08-16 17:40:28 +010097 --j;
98 c = &cpus[j];
99 c->stack_bottom = &callstacks[j][STACK_SIZE];
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000100 } else {
101 found_boot_cpu = true;
102 c = &cpus[0];
Andrew Scull48973982019-08-16 17:40:28 +0100103 CHECK(c->stack_bottom == &callstacks[0][STACK_SIZE]);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000104 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000105
Andrew Scull48973982019-08-16 17:40:28 +0100106 sl_init(&c->lock);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000107 c->id = id;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100108 }
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000109
110 if (!found_boot_cpu) {
111 /* Boot CPU was initialized but with wrong ID. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000112 dlog_warning("Boot CPU's ID not found in config.\n");
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000113 cpus[0].id = boot_cpu_id;
114 }
Max Shvetsov9c0ebe42020-08-27 12:37:57 +0100115
116 /*
117 * Clean the cache for the cpus array such that secondary cores
118 * hitting the entry point can read the cpus array consistently
119 * with MMU off (hence data cache off).
120 */
Olivier Depreza2846172021-03-23 18:45:41 +0100121 arch_cache_data_clean_range(va_from_ptr(cpus), sizeof(cpus));
Max Shvetsov9c0ebe42020-08-27 12:37:57 +0100122
Olivier Depreza2846172021-03-23 18:45:41 +0100123 arch_cache_data_clean_range(va_from_ptr(&cpu_count), sizeof(cpu_count));
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100124}
125
126size_t cpu_index(struct cpu *c)
127{
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100128 return c - cpus;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100129}
130
Olivier Deprez7d5e5532020-09-22 15:06:58 +0200131/*
132 * Return cpu with the given index.
133 */
134struct cpu *cpu_find_index(size_t index)
135{
136 return (index < MAX_CPUS) ? &cpus[index] : NULL;
137}
138
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100139/**
140 * Turns CPU on and returns the previous state.
141 */
Olivier Deprez70f8a4a2022-09-26 09:17:56 +0200142bool cpu_on(struct cpu *c)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100143{
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100144 bool prev;
145
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100146 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100147 prev = c->is_on;
148 c->is_on = true;
149 sl_unlock(&c->lock);
150
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100151 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}