blob: a6f03a005e0a1b88bfe56a60b1efd441ee8eee4f [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Federico Recanati25053ee2022-03-14 15:01:53 +01002 * Copyright 2022 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/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +000012#include "hf/arch/ffa.h"
J-Alves0a21db12024-03-28 12:43:30 +000013#include "hf/arch/memcpy_trapped.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020014#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020015#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020016#include "hf/arch/plat/ffa.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000017#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020018#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000019
Karl Meakin49ec1e42024-05-10 13:08:24 +010020#include "hf/bits.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010021#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000022#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010023#include "hf/ffa_internal.h"
24#include "hf/ffa_memory.h"
J-Alves33c47bf2022-09-29 11:36:20 +010025#include "hf/ffa_v1_0.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010027#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010028#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000029#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010030#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010031#include "hf/std.h"
Karl Meakinc5cebbc2024-06-17 11:30:27 +010032#include "hf/vcpu.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010033#include "hf/vm.h"
34
Andrew Scullf35a5c92018-08-07 18:09:46 +010035#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010036#include "vmapi/hf/ffa.h"
J-Alves126ab502022-09-29 11:37:33 +010037#include "vmapi/hf/ffa_v1_0.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010038
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000039static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010040 "Partition information descriptor size doesn't match the one in "
41 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000042static_assert(sizeof(struct ffa_partition_info) == 24,
43 "Partition information descriptor size doesn't match the one in "
44 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -080045static_assert((sizeof(struct ffa_partition_info) & 7) == 0,
46 "Partition information descriptor must be a multiple of 8 bytes"
47 " for ffa_partition_info_get_regs to work correctly. Information"
48 " from this structure are returned in 8 byte registers and the"
49 " count of 8 byte registers is returned by the ABI.");
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000050/*
51 * To eliminate the risk of deadlocks, we define a partial order for the
52 * acquisition of locks held concurrently by the same physical CPU. Our current
53 * ordering requirements are as follows:
54 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010055 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000056 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010057 * Locks of the same kind require the lock of lowest address to be locked first,
58 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000059 */
60
Andrew Scullaa039b32018-10-04 15:02:26 +010061static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010062 "Currently, a page is mapped for the send and receive buffers so "
63 "the maximum request is the size of a page.");
64
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000065static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
66 "The page pool entry size must be at least as big as the mailbox "
67 "size, so that memory region descriptors can be copied from the "
68 "mailbox for memory sharing.");
69
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -080070/*
71 * Maximum ffa_partition_info entries that can be returned by an invocation
72 * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available
73 * registers/args in struct ffa_value divided by size of struct
74 * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e.
75 * 15 uint64_t fields. For FF-A v1.1, this value should be 5.
76 */
77#define MAX_INFO_REGS_ENTRIES_PER_CALL \
78 ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info))
79static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5,
80 "FF-A v1.1 supports no more than 5 entries"
81 " per FFA_PARTITION_INFO_GET_REGS64 calls");
82
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000083static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000084
85/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000086 * Initialises the API page pool by taking ownership of the contents of the
87 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000088 */
89void api_init(struct mpool *ppool)
90{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000091 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000092}
93
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010094/**
J-Alvesad6a0432021-04-09 16:06:21 +010095 * Get target VM vCPU:
96 * If VM is UP then return first vCPU.
97 * If VM is MP then return vCPU whose index matches current CPU index.
98 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050099struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +0100100{
101 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
102 struct vcpu *vcpu = NULL;
103
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500104 CHECK((vm != NULL) && (current != NULL));
105
Karl Meakin82041822024-05-20 11:14:34 +0100106 if (vm_is_up(vm)) {
J-Alvesad6a0432021-04-09 16:06:21 +0100107 vcpu = vm_get_vcpu(vm, 0);
108 } else if (current_cpu_index < vm->vcpu_count) {
109 vcpu = vm_get_vcpu(vm, current_cpu_index);
110 }
111
112 return vcpu;
113}
114
115/**
J-Alvesfe7f7372020-11-09 11:32:12 +0000116 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
117 * is given as argument of the function.
118 *
119 * Called to change the context between SPs for direct messaging (when Hafnium
120 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
121 *
122 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100123 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000124 * - MP with pinned Execution Contexts.
125 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600126struct vcpu *api_switch_to_vm(struct vcpu_locked current_locked,
127 struct ffa_value to_ret,
J-Alves19e20cf2023-08-02 12:48:55 +0100128 enum vcpu_state vcpu_state, ffa_id_t to_id)
J-Alvesfe7f7372020-11-09 11:32:12 +0000129{
130 struct vm *to_vm = vm_find(to_id);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600131 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current_locked.vcpu);
J-Alvesfe7f7372020-11-09 11:32:12 +0000132
133 CHECK(next != NULL);
134
135 /* Set the return value for the target VM. */
136 arch_regs_set_retval(&next->regs, to_ret);
137
138 /* Set the current vCPU state. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600139 current_locked.vcpu->state = vcpu_state;
J-Alvesfe7f7372020-11-09 11:32:12 +0000140
141 return next;
142}
143
144/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000145 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100146 *
147 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100148 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100149 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600150struct vcpu *api_switch_to_primary(struct vcpu_locked current_locked,
J-Alves27b71962022-12-12 15:29:58 +0000151 struct ffa_value primary_ret,
152 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100153{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600154 return api_switch_to_vm(current_locked, primary_ret, secondary_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000155 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100156}
157
158/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200159 * Choose next vCPU to run to be the counterpart vCPU in the other
160 * world (run the normal world if currently running in the secure
161 * world). Set current vCPU state to the given vcpu_state parameter.
162 * Set FF-A return values to the target vCPU in the other world.
163 *
164 * Called in context of a direct message response from a secure
165 * partition to a VM.
166 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600167struct vcpu *api_switch_to_other_world(struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100168 struct ffa_value other_world_ret,
169 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200170{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600171 return api_switch_to_vm(current_locked, other_world_ret, vcpu_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000172 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200173}
174
175/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000176 * Returns true if the given vCPU is executing in context of an
177 * FFA_MSG_SEND_DIRECT_REQ invocation.
178 */
J-Alves27b71962022-12-12 15:29:58 +0000179bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000180{
Kathleen Capellae468c112023-12-13 17:56:28 -0500181 return locked.vcpu->direct_request_origin.vm_id != HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000182}
183
184/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100185 * Returns true if the VM owning the given vCPU is supporting managed exit and
186 * the vCPU is currently processing a managed exit.
187 */
188static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
189{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100190 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100191 vcpu_locked.vcpu->processing_managed_exit);
192}
193
194/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000195 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000196 */
197struct vcpu *api_preempt(struct vcpu *current)
198{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600199 struct vcpu_locked current_locked;
200 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100201 struct ffa_value ret = {
202 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200203 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000204 };
205
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600206 current_locked = vcpu_lock(current);
207 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_PREEMPTED);
208 vcpu_unlock(&current_locked);
209
210 return next;
Andrew Scull33fecd32019-01-08 14:48:27 +0000211}
212
213/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000214 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000215 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100216 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100217struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100218{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600219 struct vcpu_locked current_locked;
220 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100221 struct ffa_value ret = {
222 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
223 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100224 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000225
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600226 current_locked = vcpu_lock(current);
227 next = api_switch_to_primary(current_locked, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100228 VCPU_STATE_BLOCKED_INTERRUPT);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600229 vcpu_unlock(&current_locked);
230
231 return next;
Andrew Scullaa039b32018-10-04 15:02:26 +0100232}
233
234/**
Andrew Walbran33645652019-04-15 12:29:31 +0100235 * Puts the current vCPU in off mode, and returns to the primary VM.
236 */
237struct vcpu *api_vcpu_off(struct vcpu *current)
238{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600239 struct vcpu_locked current_locked;
240 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100241 struct ffa_value ret = {
242 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
243 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100244 };
245
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600246 current_locked = vcpu_lock(current);
Andrew Walbran33645652019-04-15 12:29:31 +0100247
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600248 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_OFF);
249 vcpu_unlock(&current_locked);
250
251 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100252}
253
254/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500255 * The current vCPU is blocked on some resource and needs to relinquish
256 * control back to the execution context of the endpoint that originally
257 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000258 */
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500259struct ffa_value api_yield(struct vcpu *current, struct vcpu **next,
260 struct ffa_value *args)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000261{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000262 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
263 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500264 bool transition_allowed;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500265 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500266 uint32_t timeout_low = 0;
267 uint32_t timeout_high = 0;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600268 struct vcpu_locked next_locked = (struct vcpu_locked){
269 .vcpu = NULL,
270 };
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500271
272 if (args != NULL) {
273 if (args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
274 args->arg7 != 0U) {
275 dlog_error(
276 "Parameters passed through registers X4-X7 "
277 "must be zero\n");
278 return ffa_error(FFA_INVALID_PARAMETERS);
279 }
280 timeout_low = (uint32_t)args->arg2 & 0xFFFFFFFF;
281 timeout_high = (uint32_t)args->arg3 & 0xFFFFFFFF;
282 }
Andrew Scull66d62bf2019-02-01 13:54:10 +0000283
Karl Meakin5e996992024-05-20 11:27:07 +0100284 if (vm_is_primary(current->vm)) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000285 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000286 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000287 }
288
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000289 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500290 transition_allowed = plat_ffa_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600291 current_locked, current->vm->id, HF_INVALID_VM_ID, next_locked,
292 FFA_YIELD_32, &next_state);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000293
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500294 if (!transition_allowed) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600295 ret = ffa_error(FFA_DENIED);
296 goto out;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000297 }
298
Madhukar Pappireddy65764072023-05-23 17:18:58 -0500299 /*
300 * The current vCPU is expected to move to BLOCKED state. However,
301 * under certain circumstances, it is allowed for the current vCPU
302 * to be resumed immediately without ever moving to BLOCKED state. One
303 * such scenario occurs when an SP's execution context attempts to
304 * yield cycles while handling secure interrupt. Refer to the comments
305 * in the SPMC variant of the plat_ffa_yield_prepare function.
306 */
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500307 assert(!vm_id_is_current_world(current->vm->id) ||
308 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500309
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600310 ret = plat_ffa_yield_prepare(current_locked, next, timeout_low,
311 timeout_high);
312out:
313 vcpu_unlock(&current_locked);
314 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000315}
316
317/**
Andrew Walbran33645652019-04-15 12:29:31 +0100318 * Switches to the primary so that it can switch to the target, or kick it if it
319 * is already running on a different physical CPU.
320 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600321static struct vcpu *api_wake_up_locked(struct vcpu_locked current_locked,
322 struct vcpu *target_vcpu)
Andrew Walbran33645652019-04-15 12:29:31 +0100323{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100324 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100325 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100326 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
327 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100328 };
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600329
330 return api_switch_to_primary(current_locked, ret, VCPU_STATE_BLOCKED);
331}
332
333struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
334{
335 struct vcpu_locked current_locked;
336 struct vcpu *next;
337
338 current_locked = vcpu_lock(current);
339 next = api_wake_up_locked(current_locked, target_vcpu);
340 vcpu_unlock(&current_locked);
341
342 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100343}
344
345/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000346 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000347 */
348struct vcpu *api_abort(struct vcpu *current)
349{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100350 struct ffa_value ret = ffa_error(FFA_ABORTED);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600351 struct vcpu_locked current_locked;
352 struct vcpu *next;
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500353 struct vm_locked vm_locked;
Andrew Scull9726c252019-01-23 13:44:19 +0000354
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100355 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000356 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000357
Karl Meakin5e996992024-05-20 11:27:07 +0100358 if (vm_is_primary(current->vm)) {
Andrew Scull9726c252019-01-23 13:44:19 +0000359 /* TODO: what to do when the primary aborts? */
360 for (;;) {
361 /* Do nothing. */
362 }
363 }
364
365 atomic_store_explicit(&current->vm->aborting, true,
366 memory_order_relaxed);
367
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500368 vm_locked = vm_lock(current->vm);
369 plat_ffa_free_vm_resources(vm_locked);
370 vm_unlock(&vm_locked);
Andrew Scull9726c252019-01-23 13:44:19 +0000371
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600372 current_locked = vcpu_lock(current);
373 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_ABORTED);
374 vcpu_unlock(&current_locked);
375
376 return next;
Andrew Scull9726c252019-01-23 13:44:19 +0000377}
378
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000379/*
380 * Format the partition info descriptors according to the version supported
381 * by the endpoint and return the size of the array created.
382 */
383static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000384 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000385 uint32_t vm_count)
386{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000387 struct vm *vm = vm_locked.vm;
Karl Meakin0e617d92024-04-05 12:55:22 +0100388 enum ffa_version version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100389 uint32_t partition_info_size;
390 uint32_t buffer_size;
Federico Recanati644f0462022-03-17 12:04:00 +0100391 struct ffa_value ret;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000392
J-Alvese8c8c2b2022-12-16 15:34:48 +0000393 /* Acquire receiver's RX buffer. */
394 if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) {
395 dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id);
396 return ret;
397 }
398
J-Alves122f1a12022-12-12 15:55:42 +0000399 if (vm_is_mailbox_busy(vm_locked)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000400 /*
401 * Can't retrieve memory information if the mailbox is not
402 * available.
403 */
404 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000405 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000406 }
407
Karl Meakin0e617d92024-04-05 12:55:22 +0100408 if (version == FFA_VERSION_1_0) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000409 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
410
Daniel Boulby835e1592022-05-30 17:38:51 +0100411 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
412 buffer_size = partition_info_size * vm_count;
413 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000414 dlog_error(
415 "Partition information does not fit in the "
J-Alves0a21db12024-03-28 12:43:30 +0000416 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000417 return ffa_error(FFA_NO_MEMORY);
418 }
419
420 for (uint32_t i = 0; i < vm_count; i++) {
421 /*
422 * Populate the VM's RX buffer with the partition
Kathleen Capella402fa852022-11-09 16:16:51 -0500423 * information. Clear properties bits that must be zero
424 * according to DEN0077A FF-A v1.0 REL Table 8.25.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000425 */
426 recv_mailbox[i].vm_id = partitions[i].vm_id;
427 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
Kathleen Capella402fa852022-11-09 16:16:51 -0500428 recv_mailbox[i].properties =
429 partitions[i].properties &
430 ~FFA_PARTITION_v1_0_RES_MASK;
Daniel Boulby191b5f02022-02-17 17:26:14 +0000431 }
432
433 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100434 partition_info_size = sizeof(struct ffa_partition_info);
435 buffer_size = partition_info_size * vm_count;
J-Alves0a21db12024-03-28 12:43:30 +0000436
Daniel Boulby835e1592022-05-30 17:38:51 +0100437 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000438 dlog_error(
439 "Partition information does not fit in the "
J-Alves0a824e92024-04-26 16:20:12 +0100440 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000441 return ffa_error(FFA_NO_MEMORY);
442 }
443
J-Alves0a21db12024-03-28 12:43:30 +0000444 /*
445 * Populate the VM's RX buffer with the partition information.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000446 */
J-Alves0a21db12024-03-28 12:43:30 +0000447 if (!memcpy_trapped(vm->mailbox.recv, HF_MAILBOX_SIZE,
448 partitions, buffer_size)) {
449 dlog_error(
450 "%s: Failed to copy ffa_partition_info "
451 "descriptor\n",
452 __func__);
453 return ffa_error(FFA_ABORTED);
454 }
Daniel Boulby191b5f02022-02-17 17:26:14 +0000455 }
456
Daniel Boulby835e1592022-05-30 17:38:51 +0100457 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000458
459 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000460 vm->mailbox.recv_sender = HF_VM_ID_BASE;
461 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000462 vm->mailbox.state = MAILBOX_STATE_FULL;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000463
Daniel Boulby835e1592022-05-30 17:38:51 +0100464 /*
465 * Return the count of partition information descriptors in w2
466 * and the size of the descriptors in w3.
467 */
468 return (struct ffa_value){.func = FFA_SUCCESS_32,
469 .arg2 = vm_count,
470 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000471}
472
J-Alves7fb0fdb2024-10-09 11:28:07 +0100473/**
474 * Set properties accoridng to the version of the partition, and the FF-A
475 * features it supports.
476 */
477static ffa_partition_properties_t api_ffa_partitions_info_get_properties(
478 ffa_id_t caller_id, struct vm *vm)
479{
480 ffa_partition_properties_t properties;
481
482 properties = plat_ffa_partition_properties(caller_id, vm);
483 properties |= FFA_PARTITION_AARCH64_EXEC;
484
485 if (vm->ffa_version >= FFA_VERSION_1_1) {
486 properties |= vm_are_notifications_enabled(vm)
487 ? FFA_PARTITION_NOTIFICATION
488 : 0;
489 properties |= vm->messaging_method & FFA_PARTITION_INDIRECT_MSG;
490 }
491
492 return properties;
493}
494
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100495static void api_ffa_fill_partition_info(
J-Alves7fb0fdb2024-10-09 11:28:07 +0100496 struct ffa_partition_info *out_partition, struct vm *vm,
497 ffa_id_t caller_id)
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800498{
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100499 out_partition->vm_id = vm->id;
500 out_partition->vcpu_count = vm->vcpu_count;
J-Alves7fb0fdb2024-10-09 11:28:07 +0100501 out_partition->properties =
502 api_ffa_partitions_info_get_properties(caller_id, vm);
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100503}
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800504
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100505/**
506 * Find VMs with UUID matching `uuid_to_find` , and fill `out_partitions` with
507 * partition infos. Returns number of VMs that matched.
508 * A null UUID matches against any VM.
509 * If `count_flag` is true, no partition infos are written to `out_partitions`,
510 * only the number of VMs that matched is returned.
511 */
512static ffa_vm_count_t api_ffa_fill_partitions_info_array(
513 struct ffa_partition_info out_partitions[], size_t out_partitions_len,
J-Alves7fb0fdb2024-10-09 11:28:07 +0100514 const struct ffa_uuid *uuid_to_find, bool count_flag,
515 ffa_id_t caller_id)
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100516{
517 ffa_vm_count_t vms_found = 0;
518 bool match_any = ffa_uuid_is_null(uuid_to_find);
519
520 assert(vm_get_count() <= out_partitions_len);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800521
522 /*
523 * Iterate through the VMs to find the ones with a matching
524 * UUID. A Null UUID retrieves information for all VMs.
525 */
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100526 for (ffa_vm_count_t vm_idx = 0; vm_idx < vm_get_count(); vm_idx++) {
527 struct vm *vm = vm_find_index(vm_idx);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800528
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100529 for (size_t uuid_idx = 0; uuid_idx < PARTITION_MAX_UUIDS;
530 uuid_idx++) {
531 struct ffa_uuid uuid = vm->uuids[uuid_idx];
532 struct ffa_partition_info *out_partition =
533 &out_partitions[vms_found];
534
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400535 /*
536 * Null UUID indicates reaching the end of a
537 * partition's array of UUIDs.
538 */
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100539 if (ffa_uuid_is_null(&uuid)) {
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400540 break;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800541 }
542
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100543 if (match_any || ffa_uuid_equal(uuid_to_find, &uuid)) {
544 vms_found++;
545
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400546 if (count_flag) {
547 continue;
548 }
549
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100550 api_ffa_fill_partition_info(out_partition, vm,
J-Alves7fb0fdb2024-10-09 11:28:07 +0100551 caller_id);
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100552 if (match_any) {
553 out_partition->uuid = uuid;
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400554 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800555 }
556 }
557 }
558
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100559 return vms_found;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800560}
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800561
562static inline void api_ffa_pack_vmid_count_props(
J-Alves19e20cf2023-08-02 12:48:55 +0100563 uint64_t *xn, ffa_id_t vm_id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800564 ffa_partition_properties_t properties)
565{
566 *xn = (uint64_t)vm_id;
567 *xn |= (uint64_t)vcpu_count << 16;
568 *xn |= (uint64_t)properties << 32;
569}
570
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700571/**
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700572 * This function forwards the FFA_PARTITION_INFO_GET_REGS ABI to the other world
573 * when hafnium is the hypervisor to determine the secure partitions. When
574 * hafnium is the SPMC, this function forwards the call to the SPMD to discover
575 * SPMD logical partitions. The function returns true when partition information
576 * is filled in the partitions array and false if there are errors. Note that
577 * the SPMD and SPMC may return an FF-A error code of FFA_NOT_SUPPORTED when
578 * there are no SPMD logical partitions or no secure partitions respectively,
579 * and this is not considered a failure of the forwarded call. A caller is
580 * expected to check the return value before consuming the information in the
581 * partitions array passed in and ret_count.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700582 */
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700583static bool api_ffa_partition_info_get_regs_forward(
584 const struct ffa_uuid *uuid, const uint16_t tag,
585 struct ffa_partition_info *partitions, uint16_t partitions_len,
586 ffa_vm_count_t *ret_count)
587{
588 (void)tag;
589 struct ffa_value ret;
590 uint16_t last_index = UINT16_MAX;
591 uint16_t curr_index = 0;
592 uint16_t start_index = 0;
593
594 if (!plat_ffa_partition_info_get_regs_forward_allowed()) {
595 return true;
596 }
597
598 while (start_index <= last_index) {
599 ret = ffa_partition_info_get_regs(uuid, start_index, 0);
600 if (ffa_func_id(ret) != FFA_SUCCESS_64) {
601 /*
602 * If there are no logical partitions, SPMD returns
603 * NOT_SUPPORTED, that is not an error. If there are no
604 * secure partitions the SPMC returns NOT_SUPPORTED.
605 */
606 if ((ffa_func_id(ret) == FFA_ERROR_32) &&
607 (ffa_error_code(ret) == FFA_NOT_SUPPORTED)) {
608 return true;
609 }
610
611 return false;
612 }
613
614 if (!api_ffa_fill_partition_info_from_regs(
615 ret, start_index, partitions, partitions_len,
616 ret_count)) {
617 return false;
618 }
619
620 last_index = ffa_partition_info_regs_get_last_idx(ret);
621 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
622 start_index = curr_index + 1;
623 }
624 return true;
625}
626
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700627bool api_ffa_fill_partition_info_from_regs(
628 struct ffa_value ret, uint16_t start_index,
629 struct ffa_partition_info *partitions, uint16_t partitions_len,
630 ffa_vm_count_t *ret_count)
631{
632 uint16_t vm_count = *ret_count;
633 uint16_t curr_index = 0;
634 uint8_t num_entries = 0;
635 uint8_t idx = 0;
636 /* List of pointers to args in return value. */
637 uint64_t *arg_ptrs[] = {
638 &ret.arg3,
639 &ret.arg4,
640 &ret.arg5,
641 &ret.arg6,
642 &ret.arg7,
643 &ret.extended_val.arg8,
644 &ret.extended_val.arg9,
645 &ret.extended_val.arg10,
646 &ret.extended_val.arg11,
647 &ret.extended_val.arg12,
648 &ret.extended_val.arg13,
649 &ret.extended_val.arg14,
650 &ret.extended_val.arg15,
651 &ret.extended_val.arg16,
652 &ret.extended_val.arg17,
653 };
654
655 if (vm_count > partitions_len) {
656 return false;
657 }
658
659 /*
660 * Tags are currently unused in the implementation. Expect it to be
661 * zero since the implementation does not provide a tag when calling
662 * the FFA_PARTITION_INFO_GET_REGS ABI.
663 */
664 assert(ffa_partition_info_regs_get_tag(ret) == 0);
665
666 /*
667 * Hafnium expects the size of the returned descriptor to be equal to
668 * the size of the structure in the FF-A 1.1 specification. When future
669 * enhancements are made, this assert can be relaxed.
670 */
671 assert(ffa_partition_info_regs_get_desc_size(ret) ==
672 sizeof(struct ffa_partition_info));
673
674 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
675
676 /* FF-A 1.2 ALP0, section 14.9.2 Usage rule 7. */
677 assert(start_index <= curr_index);
678
679 num_entries = curr_index - start_index + 1;
680 if (num_entries > (partitions_len - vm_count) ||
681 num_entries > MAX_INFO_REGS_ENTRIES_PER_CALL) {
682 return false;
683 }
684
685 while (num_entries) {
686 uint64_t info = *(arg_ptrs[(ptrdiff_t)(idx++)]);
687 uint64_t uuid_lo = *(arg_ptrs[(ptrdiff_t)(idx++)]);
688 uint64_t uuid_high = *(arg_ptrs[(ptrdiff_t)(idx++)]);
689
690 partitions[vm_count].vm_id = info & 0xFFFF;
691 partitions[vm_count].vcpu_count = (info >> 16) & 0xFFFF;
692 partitions[vm_count].properties = (info >> 32);
693 partitions[vm_count].uuid.uuid[0] = uuid_lo & 0xFFFFFFFF;
694 partitions[vm_count].uuid.uuid[1] =
695 (uuid_lo >> 32) & 0xFFFFFFFF;
696 partitions[vm_count].uuid.uuid[2] = uuid_high & 0xFFFFFFFF;
697 partitions[vm_count].uuid.uuid[3] =
698 (uuid_high >> 32) & 0xFFFFFFFF;
699 vm_count++;
700 num_entries--;
701 }
702
703 *ret_count = vm_count;
704 return true;
705}
706
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800707struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current,
708 const struct ffa_uuid *uuid,
709 const uint16_t start_index,
710 const uint16_t tag)
711{
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800712 struct vm *current_vm = current->vm;
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800713 static struct ffa_partition_info partitions[2 * MAX_VMS];
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800714 bool uuid_is_null = ffa_uuid_is_null(uuid);
715 ffa_vm_count_t vm_count = 0;
716 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
717 uint16_t max_idx = 0;
718 uint16_t curr_idx = 0;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800719 uint8_t num_entries_to_ret = 0;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800720 uint8_t arg_idx = 3;
721
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800722 /* list of pointers to args in return value */
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800723 uint64_t *arg_ptrs[] = {
724 &(ret).func,
725 &(ret).arg1,
726 &(ret).arg2,
727 &(ret).arg3,
728 &(ret).arg4,
729 &(ret).arg5,
730 &(ret).arg6,
731 &(ret).arg7,
732 &(ret).extended_val.arg8,
733 &(ret).extended_val.arg9,
734 &(ret).extended_val.arg10,
735 &(ret).extended_val.arg11,
736 &(ret).extended_val.arg12,
737 &(ret).extended_val.arg13,
738 &(ret).extended_val.arg14,
739 &(ret).extended_val.arg15,
740 &(ret).extended_val.arg16,
741 &(ret).extended_val.arg17,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800742 };
743
744 /* TODO: Add support for using tags */
745 if (tag != 0) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800746 dlog_error("Tag not 0. Unsupported tag. %d\n", tag);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800747 return ffa_error(FFA_RETRY);
748 }
749
750 memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions));
751
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100752 vm_count = api_ffa_fill_partitions_info_array(
753 partitions, ARRAY_SIZE(partitions), uuid, false,
754 current_vm->id);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800755
756 /* If UUID is Null vm_count must not be zero at this stage. */
757 CHECK(!uuid_is_null || vm_count != 0);
758
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800759 /*
760 * When running the Hypervisor:
761 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
762 * it to fill with secure partitions information.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700763 * When running the SPMC, the SPMC forwards the call to the SPMD to
764 * discover any EL3 SPMD logical partitions, if the call came from an
765 * SP. Otherwise, the call is not forwarded.
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800766 * TODO: Note that for this ABI, forwarding on every invocation when
767 * uuid is Null is inefficient,and if performance becomes a problem,
768 * this would be a good place to optimize using strategies such as
769 * caching info etc. For now, assuming this inefficiency is not a major
770 * issue.
771 * - If UUID is non-Null vm_count may be zero because the UUID matches
772 * a secure partition and the query is forwarded to the SPMC.
773 * When running the SPMC:
774 * - If UUID is non-Null and vm_count is zero it means there is no such
775 * partition identified in the system.
776 */
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700777 if (vm_id_is_current_world(current_vm->id)) {
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700778 if (!api_ffa_partition_info_get_regs_forward(
779 uuid, tag, partitions, ARRAY_SIZE(partitions),
780 &vm_count)) {
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700781 dlog_error(
782 "Failed to forward "
783 "ffa_partition_info_get_regs.\n");
784 return ffa_error(FFA_DENIED);
785 }
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800786 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800787
788 /*
789 * Unrecognized UUID: does not match any of the VMs (or SPs)
790 * and is not Null.
791 */
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800792 if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) {
Olivier Deprez89da0a42023-09-01 17:38:33 +0200793 dlog_verbose(
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800794 "Invalid parameters. vm_count = %d (must not be zero "
Karl Meakine8937d92024-03-19 16:04:25 +0000795 "or > %lu)\n",
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800796 vm_count, ARRAY_SIZE(partitions));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800797 return ffa_error(FFA_INVALID_PARAMETERS);
798 }
799
800 if (start_index >= vm_count) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800801 dlog_error(
802 "start index = %d vm_count = %d (start_index must be "
803 "less than vm_count)\n",
804 start_index, vm_count);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800805 return ffa_error(FFA_INVALID_PARAMETERS);
806 }
807
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800808 max_idx = vm_count - 1;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800809 num_entries_to_ret = (max_idx - start_index) + 1;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800810 num_entries_to_ret =
811 MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800812 curr_idx = start_index + num_entries_to_ret - 1;
813 assert(curr_idx <= max_idx);
814
815 ret.func = FFA_SUCCESS_64;
816 ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48;
817 ret.arg2 |= curr_idx << 16;
818 ret.arg2 |= max_idx;
819
820 if (num_entries_to_ret > 1) {
821 ret.extended_val.valid = 1;
822 }
823
824 for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800825 uint64_t *xn_0 = arg_ptrs[arg_idx++];
826 uint64_t *xn_1 = arg_ptrs[arg_idx++];
827 uint64_t *xn_2 = arg_ptrs[arg_idx++];
828
829 api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id,
830 partitions[idx].vcpu_count,
831 partitions[idx].properties);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800832 if (uuid_is_null) {
Karl Meakin9478e322024-09-23 17:47:09 +0100833 ffa_uuid_to_u64x2(xn_1, xn_2, &partitions[idx].uuid);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800834 }
Raghu Krishnamurthycded31b2023-04-23 15:07:35 -0700835 assert(arg_idx <= ARRAY_SIZE(arg_ptrs));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800836 }
837
838 return ret;
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800839}
840
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100841struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000842 const struct ffa_uuid *uuid,
843 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100844{
845 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100846 ffa_vm_count_t vm_count = 0;
Olivier Deprez013f4d62022-11-21 15:46:20 +0100847 bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) ==
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000848 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100849 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100850 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000851 struct vm_locked vm_locked;
852 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100853
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000854 /* Bits 31:1 Must Be Zero */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100855 if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) {
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000856 return ffa_error(FFA_INVALID_PARAMETERS);
857 }
858
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100859 vm_count = api_ffa_fill_partitions_info_array(
860 partitions, ARRAY_SIZE(partitions), uuid, count_flag,
861 current_vm->id);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100862
Olivier Depreze562e542020-06-11 17:31:54 +0200863 /* If UUID is Null vm_count must not be zero at this stage. */
864 CHECK(!uuid_is_null || vm_count != 0);
865
866 /*
867 * When running the Hypervisor:
868 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
869 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000870 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200871 * a secure partition and the query is forwarded to the SPMC.
872 * When running the SPMC:
873 * - If UUID is non-Null and vm_count is zero it means there is no such
874 * partition identified in the system.
875 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000876 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200877
878 /*
879 * Unrecognized UUID: does not match any of the VMs (or SPs)
880 * and is not Null.
881 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100882 if (vm_count == 0) {
883 return ffa_error(FFA_INVALID_PARAMETERS);
884 }
885
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000886 /*
887 * If the count flag is set we don't need to return the partition info
888 * descriptors.
889 */
890 if (count_flag) {
891 return (struct ffa_value){.func = FFA_SUCCESS_32,
892 .arg2 = vm_count};
893 }
894
Daniel Boulby191b5f02022-02-17 17:26:14 +0000895 vm_locked = vm_lock(current_vm);
896 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
897 vm_count);
898 vm_unlock(&vm_locked);
899 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100900}
901
Andrew Scull9726c252019-01-23 13:44:19 +0000902/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000903 * Returns the ID of the VM.
904 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100905struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000906{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100907 return (struct ffa_value){.func = FFA_SUCCESS_32,
908 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000909}
910
911/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200912 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
913 * FF-A instances.
914 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000915 */
916struct ffa_value api_ffa_spm_id_get(void)
917{
Karl Meakin0e617d92024-04-05 12:55:22 +0100918 if (FFA_VERSION_1_1 <= FFA_VERSION_COMPILED) {
919 /*
920 * Return the SPMC ID that was fetched during FF-A
921 * initialization.
922 */
923 return (struct ffa_value){.func = FFA_SUCCESS_32,
924 .arg2 = arch_ffa_spmc_id_get()};
925 }
926
J-Alves3829fc02021-03-18 12:49:18 +0000927 return ffa_error(FFA_NOT_SUPPORTED);
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000928}
929
930/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000931 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000932 * function to indicate that register state for the given vCPU has been saved
933 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000934 */
935void api_regs_state_saved(struct vcpu *vcpu)
936{
937 sl_lock(&vcpu->lock);
938 vcpu->regs_available = true;
939 sl_unlock(&vcpu->lock);
940}
J-Alves0ffce752024-10-09 14:37:10 +0100941
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000942/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000943 * Assuming that the arguments have already been checked by the caller, injects
944 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
945 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
946 * is next run, which is up to the scheduler.
947 *
948 * Returns:
949 * - 0 on success if no further action is needed.
950 * - 1 if it was called by the primary VM and the primary VM now needs to wake
951 * up or kick the target vCPU.
952 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100953int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600954 uint32_t intid,
955 struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100956 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000957{
Manish Pandey35e452f2021-02-18 21:36:34 +0000958 struct vcpu *target_vcpu = target_locked.vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600959 struct vcpu *current = current_locked.vcpu;
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100960 struct interrupts *interrupts = &target_vcpu->interrupts;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000961 int64_t ret = 0;
962
Andrew Walbran508e63c2018-12-20 17:02:37 +0000963 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000964 * We only need to change state and (maybe) trigger a virtual interrupt
965 * if it is enabled and was not previously pending. Otherwise we can
966 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000967 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100968 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
969 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000970 goto out;
971 }
972
973 /* Increment the count. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100974 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000975
976 /*
977 * Only need to update state if there was not already an
978 * interrupt enabled and pending.
979 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000980 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000981 goto out;
982 }
983
Karl Meakin5e996992024-05-20 11:27:07 +0100984 if (vm_is_primary(current->vm)) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000985 /*
986 * If the call came from the primary VM, let it know that it
987 * should run or kick the target vCPU.
988 */
989 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000990 } else if (current != target_vcpu && next != NULL) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600991 *next = api_wake_up_locked(current_locked, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000992 }
993
994out:
995 /* Either way, make it pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100996 vcpu_virt_interrupt_set_pending(interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000997
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200998 return ret;
999}
1000
Andrew Walbran508e63c2018-12-20 17:02:37 +00001001/**
J-Alves2ced1672022-12-12 14:35:38 +00001002 * Constructs the return value from a successful FFA_MSG_WAIT call, when used
1003 * with FFA_MSG_SEND_32.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001004 */
J-Alves27b71962022-12-12 15:29:58 +00001005struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001006{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001007 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001008 case FFA_MSG_SEND_32:
1009 return (struct ffa_value){
1010 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001011 .arg1 = (receiver->mailbox.recv_sender << 16) |
1012 receiver->id,
1013 .arg3 = receiver->mailbox.recv_size};
J-Alvesd9e7c8f2024-09-11 13:37:25 +01001014 default:
Federico Recanati25053ee2022-03-14 15:01:53 +01001015 return (struct ffa_value){
1016 .func = FFA_RUN_32,
1017 /*
1018 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
1019 * Retrieving vCPU requires a rework of the function,
1020 * while receiver ID must be set because it's checked by
1021 * other APIs (eg: FFA_NOTIFICATION_GET).
1022 */
1023 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001024 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001025}
1026
J-Alvese8c8c2b2022-12-16 15:34:48 +00001027/**
1028 * Change the state of mailbox to empty, such that the ownership is given to the
1029 * Partition manager.
Karl Meakin1064a9c2024-06-04 17:20:43 +01001030 * Returns FFA_SUCCESS if the mailbox was reset successfully, FFA_ERROR
1031 * otherwise.
J-Alvese8c8c2b2022-12-16 15:34:48 +00001032 */
Karl Meakin1064a9c2024-06-04 17:20:43 +01001033static struct ffa_value api_release_mailbox(struct vm_locked vm_locked)
J-Alvese8c8c2b2022-12-16 15:34:48 +00001034{
Karl Meakin1064a9c2024-06-04 17:20:43 +01001035 struct ffa_value ret = {.func = FFA_SUCCESS_32};
J-Alves19e20cf2023-08-02 12:48:55 +01001036 ffa_id_t vm_id = vm_locked.vm->id;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001037
1038 switch (vm_locked.vm->mailbox.state) {
1039 case MAILBOX_STATE_EMPTY:
1040 dlog_verbose("Mailbox of %x is empty.\n", vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001041 ret = ffa_error(FFA_DENIED);
J-Alves31f8bef2023-07-14 12:46:28 +01001042 break;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001043 case MAILBOX_STATE_FULL:
1044 /* Check it doesn't have pending RX full notifications. */
1045 if (vm_are_fwk_notifications_pending(vm_locked)) {
1046 dlog_verbose(
1047 "Mailbox of endpoint %x has pending "
1048 "messages.\n",
1049 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001050 ret = ffa_error(FFA_DENIED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001051 }
1052 break;
1053 case MAILBOX_STATE_OTHER_WORLD_OWNED:
1054 /*
1055 * The SPMC shouldn't let SP's mailbox get into this state.
1056 * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst
1057 * the mailbox is in this state. In that case, we should report
1058 * error.
1059 */
1060 if (vm_id_is_current_world(vm_id)) {
1061 dlog_verbose(
Karl Meakin1064a9c2024-06-04 17:20:43 +01001062 "Mailbox of endpoint %x is in an incorrect "
1063 "state.\n",
J-Alvese8c8c2b2022-12-16 15:34:48 +00001064 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001065 ret = ffa_error(FFA_ABORTED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001066 }
1067 break;
1068 }
1069
Karl Meakin1064a9c2024-06-04 17:20:43 +01001070 if (ret.func == FFA_SUCCESS_32) {
1071 vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001072 }
1073
Karl Meakin1064a9c2024-06-04 17:20:43 +01001074 return ret;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001075}
1076
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001077/*
1078 * Helper to check if extended arguments (corresponding to regs x8-x17)
1079 * are zeroed out.
1080 */
Kathleen Capella036cc592023-11-30 18:26:15 -05001081bool api_extended_args_are_zero(struct ffa_value *args)
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001082{
1083 if (args->extended_val.arg8 != 0U || args->extended_val.arg9 != 0U ||
1084 args->extended_val.arg10 != 0U || args->extended_val.arg11 != 0U ||
1085 args->extended_val.arg12 != 0U || args->extended_val.arg13 != 0U ||
1086 args->extended_val.arg14 != 0U || args->extended_val.arg15 != 0U ||
1087 args->extended_val.arg16 != 0U || args->extended_val.arg17 != 0U) {
1088 return false;
1089 }
1090 return true;
1091}
1092
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001093static void api_ffa_msg_wait_rx_release(struct vcpu *current)
1094{
1095 struct vm_locked vm_locked;
1096
1097 vm_locked = plat_ffa_vm_find_locked(current->vm->id);
1098 if (vm_locked.vm == NULL) {
1099 return;
1100 }
1101
1102 api_release_mailbox(vm_locked);
1103
1104 if (vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY) {
1105 dlog_warning("Mailbox not released to producer\n");
1106 }
1107
1108 vm_unlock(&vm_locked);
1109}
1110
Kathleen Capella7253bd52024-08-14 17:36:09 -04001111static bool api_retain_rx_buffer_ownership(struct ffa_value args)
1112{
1113 return ((args.arg2 & FFA_MSG_WAIT_FLAG_RETAIN_RX) != 0U);
1114}
1115
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001116struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
1117 struct ffa_value *args)
1118{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001119 struct vcpu_locked current_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001120 enum vcpu_state next_state = VCPU_STATE_RUNNING;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001121 struct ffa_value ret;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001122 struct vcpu_locked next_locked = (struct vcpu_locked){
1123 .vcpu = NULL,
1124 };
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001125
Kathleen Capella7253bd52024-08-14 17:36:09 -04001126 if (args->arg1 != 0U || args->arg3 != 0U || args->arg4 != 0U ||
1127 args->arg5 != 0U || args->arg6 != 0U || args->arg7 != 0U) {
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001128 return ffa_error(FFA_INVALID_PARAMETERS);
1129 }
1130
Kathleen Capella7253bd52024-08-14 17:36:09 -04001131 if (current->vm->ffa_version >= FFA_VERSION_1_2) {
1132 if (!api_extended_args_are_zero(args)) {
1133 return ffa_error(FFA_INVALID_PARAMETERS);
1134 }
1135 } else {
1136 if (args->arg2 != 0U) {
1137 return ffa_error(FFA_INVALID_PARAMETERS);
1138 }
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001139 }
1140
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001141 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001142 if (!plat_ffa_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001143 current_locked, current->vm->id, HF_INVALID_VM_ID,
1144 next_locked, FFA_MSG_WAIT_32, &next_state)) {
1145 ret = ffa_error(FFA_DENIED);
1146 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001147 }
1148
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001149 assert(!vm_id_is_current_world(current->vm->id) ||
1150 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001151
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001152 ret = plat_ffa_msg_wait_prepare(current_locked, next);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001153
1154 /*
1155 * To maintain partial ordering of locks, release vCPU lock before
1156 * releasing the VM's RX buffer, a process which requires locking the
1157 * VM.
1158 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001159out:
1160 vcpu_unlock(&current_locked);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001161
Kathleen Capella7253bd52024-08-14 17:36:09 -04001162 if (ret.func != FFA_ERROR_32 &&
1163 !api_retain_rx_buffer_ownership(*args)) {
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001164 api_ffa_msg_wait_rx_release(current);
1165 }
J-Alvese8c8c2b2022-12-16 15:34:48 +00001166 return ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001167}
1168
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001169/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001170 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001171 * value needs to be forced onto the vCPU.
1172 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001173static bool api_vcpu_prepare_run(struct vcpu_locked current_locked,
1174 struct vcpu_locked vcpu_next_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001175 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001176{
Max Shvetsov40108e72020-08-27 12:39:50 +01001177 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001178 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001179 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001180 bool vcpu_was_init_state = false;
J-Alves6e2abc62021-12-02 14:58:56 +00001181 bool need_vm_lock;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001182 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001183
Andrew Scullb06d1752019-02-04 10:15:48 +00001184 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001185 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +00001186 *
Andrew Scull4caadaf2019-07-03 13:13:47 +01001187 * The VM lock is not needed in the common case so it must only be taken
1188 * when it is going to be needed. This ensures there are no inter-vCPU
1189 * dependencies in the common run case meaning the sensitive context
1190 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +00001191 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001192 struct vcpu *vcpu = vcpu_next_locked.vcpu;
1193 struct vcpu *current = current_locked.vcpu;
Max Shvetsov40108e72020-08-27 12:39:50 +01001194
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001195 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +00001196 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
1197 (!vcpu->vm->el0_partition &&
1198 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
1199 vcpu->state == VCPU_STATE_BLOCKED ||
1200 vcpu->state == VCPU_STATE_PREEMPTED));
1201
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001202 if (need_vm_lock) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001203 vcpu_unlock(&vcpu_next_locked);
1204 vcpu_unlock(&current_locked);
Max Shvetsov40108e72020-08-27 12:39:50 +01001205 vm_locked = vm_lock(vcpu->vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001206
1207 /* Lock both vCPUs at once to avoid deadlock. */
1208 vcpus_locked = vcpu_lock_both(current, vcpu);
1209 current_locked = vcpus_locked.vcpu1;
1210 vcpu_next_locked = vcpus_locked.vcpu2;
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001211 }
1212
1213 /*
1214 * If the vCPU is already running somewhere then we can't run it here
1215 * simultaneously. While it is actually running then the state should be
1216 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
1217 * stops running but while Hafnium is in the process of switching back
1218 * to the primary there will be a brief period while the state has been
1219 * updated but `regs_available` is still false (until
1220 * `api_regs_state_saved` is called). We can't start running it again
1221 * until this has finished, so count this state as still running for the
1222 * purposes of this check.
1223 */
1224 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
1225 /*
1226 * vCPU is running on another pCPU.
1227 *
1228 * It's okay not to return the sleep duration here because the
1229 * other physical CPU that is currently running this vCPU will
1230 * return the sleep duration if needed.
1231 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001232 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001233 ret = false;
1234 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001235 }
Andrew Scull9726c252019-01-23 13:44:19 +00001236
1237 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001238 if (vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04001239 dlog_verbose("VM %#x was aborted, cannot run vCPU %u\n",
1240 vcpu->vm->id, vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +01001241 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +00001242 }
Kathleen Capella6ab05132023-05-10 12:27:35 -04001243 *run_ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +00001244 ret = false;
1245 goto out;
1246 }
1247
Andrew Walbran508e63c2018-12-20 17:02:37 +00001248 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001249 case VCPU_STATE_RUNNING:
1250 case VCPU_STATE_OFF:
1251 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001252 ret = false;
1253 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001254
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001255 case VCPU_STATE_WAITING:
1256 /*
Olivier Deprezb2808332023-02-02 15:25:40 +01001257 * An initial FFA_RUN is necessary for SP's secondary vCPUs to
1258 * reach the message wait loop.
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001259 */
Olivier Deprezb2808332023-02-02 15:25:40 +01001260 if (vcpu->rt_model == RTM_SP_INIT) {
1261 /*
1262 * TODO: this should be removed, but omitting it makes
1263 * normal world arch gicv3 tests failing.
1264 */
1265 vcpu->rt_model = RTM_NONE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001266
1267 vcpu_was_init_state = true;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001268 break;
1269 }
1270
J-Alves6e2abc62021-12-02 14:58:56 +00001271 assert(need_vm_lock == true);
1272 if (!vm_locked.vm->el0_partition &&
1273 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001274 vcpu_next_locked, current_locked, vm_locked)) {
Federico Recanati9dccc4b2022-04-27 12:52:36 +02001275 /* TODO: setting a return value to override
1276 * the placeholder (FFA_ERROR(INTERRUPTED))
1277 * set by FFA_MSG_WAIT. FF-A v1.1 allows
1278 * FFA_MSG_WAIT to successfully return even if
1279 * it didn't receive a message. TFTF tests are
1280 * still expecting an FFA_ERROR instead,
1281 * should be fixed?
1282 */
1283 arch_regs_set_retval(
1284 &vcpu->regs,
1285 (struct ffa_value){.func = FFA_RUN_32,
1286 // TODO: does it make sense
1287 // to set vCPU/receiver?
1288 .arg1 = 0});
J-Alves6e2abc62021-12-02 14:58:56 +00001289 break;
1290 }
1291
Andrew Scullb06d1752019-02-04 10:15:48 +00001292 /*
1293 * A pending message allows the vCPU to run so the message can
1294 * be delivered directly.
1295 */
J-Alvese8c8c2b2022-12-16 15:34:48 +00001296 if (vcpu->vm->mailbox.state == MAILBOX_STATE_FULL) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001297 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001298 ffa_msg_recv_return(vcpu->vm));
Andrew Scullb06d1752019-02-04 10:15:48 +00001299 break;
1300 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001301
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001302 if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001303 break;
1304 }
1305
1306 if (arch_timer_enabled(&vcpu->regs)) {
1307 timer_remaining_ns =
1308 arch_timer_remaining_ns(&vcpu->regs);
1309 if (timer_remaining_ns == 0) {
1310 break;
1311 }
1312 } else {
1313 dlog_verbose("Timer disabled\n");
1314 }
1315 run_ret->func = FFA_MSG_WAIT_32;
1316 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1317 run_ret->arg2 = timer_remaining_ns;
1318 ret = false;
1319 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001320 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +00001321 if (need_vm_lock &&
1322 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001323 vcpu_next_locked, current_locked, vm_locked)) {
1324 assert(vcpu_interrupt_count_get(vcpu_next_locked) > 0);
J-Alves6e2abc62021-12-02 14:58:56 +00001325 break;
1326 }
1327
Andrew Scullb06d1752019-02-04 10:15:48 +00001328 /* Allow virtual interrupts to be delivered. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001329 if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +00001330 break;
1331 }
1332
Andrew Walbran508e63c2018-12-20 17:02:37 +00001333 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001334 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001335 arch_timer_remaining_ns(&vcpu->regs);
1336
1337 /*
1338 * The timer expired so allow the interrupt to be
1339 * delivered.
1340 */
1341 if (timer_remaining_ns == 0) {
1342 break;
1343 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001344 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001345
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001346 /*
1347 * The vCPU is not ready to run, return the appropriate code to
1348 * the primary which called vcpu_run.
1349 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001350 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001351 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1352 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +00001353
1354 ret = false;
1355 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001356
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001357 case VCPU_STATE_BLOCKED:
1358 /* A blocked vCPU is run unconditionally. Fall through. */
1359 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +00001360 /* Check NPI is to be injected here. */
1361 if (need_vm_lock) {
1362 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001363 vcpu_next_locked, current_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001364 }
Andrew Walbran508e63c2018-12-20 17:02:37 +00001365 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001366 default:
1367 /*
1368 * Execution not expected to reach here. Deny the request
1369 * gracefully.
1370 */
1371 *run_ret = ffa_error(FFA_DENIED);
1372 ret = false;
1373 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001374 }
1375
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001376 plat_ffa_init_schedule_mode_ffa_run(current_locked, vcpu_next_locked);
Madhukar Pappireddy1480fce2022-06-21 18:09:25 -05001377
Andrew Scullb06d1752019-02-04 10:15:48 +00001378 /* It has been decided that the vCPU should be run. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001379 vcpu->cpu = current_locked.vcpu->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001380 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001381
Olivier Deprez04168ed2022-09-26 09:20:00 +02001382 if (vcpu_was_init_state) {
1383 vcpu_set_phys_core_idx(vcpu);
1384 vcpu_set_boot_info_gp_reg(vcpu);
J-Alves7ac49052022-02-08 17:20:53 +00001385 }
J-Alves7ac49052022-02-08 17:20:53 +00001386
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001387 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001388 * Mark the registers as unavailable now that we're about to reflect
1389 * them onto the real registers. This will also prevent another physical
1390 * CPU from trying to read these registers.
1391 */
1392 vcpu->regs_available = false;
1393
1394 ret = true;
1395
1396out:
Andrew Scullb06d1752019-02-04 10:15:48 +00001397 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +01001398 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001399 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001400 return ret;
1401}
1402
J-Alves19e20cf2023-08-02 12:48:55 +01001403struct ffa_value api_ffa_run(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001404 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001405{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001406 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001407 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001409 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001410 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001411 struct vcpu_locked vcpu_next_locked;
1412 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001413
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001414 current_locked = vcpu_lock(current);
1415 if (!plat_ffa_run_checks(current_locked, vm_id, vcpu_idx, &ret, next)) {
1416 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001417 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001418
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001419 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001420 goto out;
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001421 }
1422
Andrew Scull19503262018-09-20 14:48:39 +01001423 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001424 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +01001425 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001426 goto out;
Andrew Scull19503262018-09-20 14:48:39 +01001427 }
1428
Fuad Tabbaed294af2019-12-20 10:43:01 +00001429 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001430 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001431 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001432 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001433
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001434 /*
1435 * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC
1436 * bypasses the intermediate execution contexts and resumes the
1437 * SP execution context that was originally preempted.
1438 */
1439 if (*next != NULL) {
1440 vcpu = *next;
1441 } else {
1442 vcpu = vm_get_vcpu(vm, vcpu_idx);
1443 }
1444
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001445 /*
1446 * Unlock current vCPU to allow it to be locked together with next
1447 * vcpu.
1448 */
1449 vcpu_unlock(&current_locked);
1450
1451 /* Lock both vCPUs at once to avoid deadlock. */
1452 vcpus_locked = vcpu_lock_both(current, vcpu);
1453 current_locked = vcpus_locked.vcpu1;
1454 vcpu_next_locked = vcpus_locked.vcpu2;
1455
1456 if (!plat_ffa_check_runtime_state_transition(
1457 current_locked, current->vm->id, HF_INVALID_VM_ID,
1458 vcpu_next_locked, FFA_RUN_32, &next_state)) {
1459 ret = ffa_error(FFA_DENIED);
1460 goto out_vcpu;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001461 }
1462
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001463 if (!api_vcpu_prepare_run(current_locked, vcpu_next_locked, &ret)) {
1464 goto out_vcpu;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001465 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001466
Andrew Walbran508e63c2018-12-20 17:02:37 +00001467 /*
1468 * Inject timer interrupt if timer has expired. It's safe to access
1469 * vcpu->regs here because api_vcpu_prepare_run already made sure that
1470 * regs_available was true (and then set it to false) before returning
1471 * true.
1472 */
1473 if (arch_timer_pending(&vcpu->regs)) {
1474 /* Make virtual timer interrupt pending. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001475 api_interrupt_inject_locked(vcpu_next_locked,
1476 HF_VIRTUAL_TIMER_INTID,
1477 vcpu_next_locked, NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001478
1479 /*
1480 * Set the mask bit so the hardware interrupt doesn't fire
1481 * again. Ideally we wouldn't do this because it affects what
1482 * the secondary vCPU sees, but if we don't then we end up with
1483 * a loop of the interrupt firing each time we try to return to
1484 * the secondary vCPU.
1485 */
1486 arch_timer_mask(&vcpu->regs);
1487 }
1488
Fuad Tabbaed294af2019-12-20 10:43:01 +00001489 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001490 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001491
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001492 assert(!vm_id_is_current_world(current->vm->id) ||
1493 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001494 current->state = VCPU_STATE_BLOCKED;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001495
Andrew Scull33fecd32019-01-08 14:48:27 +00001496 /*
1497 * Set a placeholder return code to the scheduler. This will be
1498 * overwritten when the switch back to the primary occurs.
1499 */
J-Alvesbd32c972024-02-02 16:20:04 +00001500 ret = api_ffa_interrupt_return(0);
Andrew Scull33fecd32019-01-08 14:48:27 +00001501
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001502out_vcpu:
1503 vcpu_unlock(&vcpu_next_locked);
1504
Andrew Scull6d2db332018-10-10 15:28:17 +01001505out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001506 vcpu_unlock(&current_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001507 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001508}
1509
1510/**
Andrew Scull81e85092018-12-12 12:56:20 +00001511 * Check that the mode indicates memory that is valid, owned and exclusive.
1512 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001513static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001514{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001515 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1516 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001517}
1518
1519/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001520 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001521 */
Karl Meakin738bee12024-01-12 15:19:45 +00001522static struct ffa_value api_vm_configure_stage1(
1523 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1524 paddr_t pa_send_begin, paddr_t pa_send_end, paddr_t pa_recv_begin,
1525 paddr_t pa_recv_end, uint32_t extra_attributes,
1526 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001527{
Karl Meakin738bee12024-01-12 15:19:45 +00001528 struct ffa_value ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001529
Karl Meakin738bee12024-01-12 15:19:45 +00001530 /*
1531 * Map the send page as read-only in the SPMC/hypervisor address space.
1532 */
Andrew Sculle1322792019-07-01 17:46:10 +01001533 vm_locked.vm->mailbox.send =
1534 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001535 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001536 if (!vm_locked.vm->mailbox.send) {
Karl Meakin738bee12024-01-12 15:19:45 +00001537 ret = ffa_error(FFA_NO_MEMORY);
1538 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001539 }
1540
1541 /*
Karl Meakin738bee12024-01-12 15:19:45 +00001542 * Map the receive page as writable in the SPMC/hypervisor address
1543 * space. On failure, unmap the send page before returning.
Andrew Sculle1322792019-07-01 17:46:10 +01001544 */
1545 vm_locked.vm->mailbox.recv =
1546 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001547 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001548 if (!vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001549 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001550 goto fail_undo_send;
1551 }
1552
Karl Meakin738bee12024-01-12 15:19:45 +00001553 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001554 goto out;
1555
1556 /*
1557 * The following mappings will not require more memory than is available
1558 * in the local pool.
1559 */
1560fail_undo_send:
1561 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001562 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1563 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001564
Andrew Sculle1322792019-07-01 17:46:10 +01001565out:
Andrew Sculle1322792019-07-01 17:46:10 +01001566 return ret;
1567}
1568
1569/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001570 * Sanity checks and configures the send and receive pages in the VM stage-2
1571 * and hypervisor stage-1 page tables.
1572 *
1573 * Returns:
1574 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001575 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001576 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1577 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001578 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001579 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001580 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001581
1582struct ffa_value api_vm_configure_pages(
1583 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1584 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1585 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001586{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001587 struct ffa_value ret;
1588 paddr_t pa_send_begin;
1589 paddr_t pa_send_end;
1590 paddr_t pa_recv_begin;
1591 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001592 uint32_t orig_send_mode = 0;
1593 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001594 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001595
1596 /* We only allow these to be setup once. */
1597 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001598 dlog_error("%s: Mailboxes have already been setup for VM %#x\n",
1599 __func__, vm_locked.vm->id);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001600 ret = ffa_error(FFA_DENIED);
1601 goto out;
1602 }
1603
1604 /* Hafnium only supports a fixed size of RX/TX buffers. */
1605 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
Karl Meakine8937d92024-03-19 16:04:25 +00001606 dlog_error("%s: Page count must be %zu, it is %d\n", __func__,
Karl Meakin738bee12024-01-12 15:19:45 +00001607 HF_MAILBOX_SIZE / FFA_PAGE_SIZE, page_count);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001608 ret = ffa_error(FFA_INVALID_PARAMETERS);
1609 goto out;
1610 }
1611
1612 /* Fail if addresses are not page-aligned. */
1613 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1614 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001615 dlog_error("%s: Mailbox buffers not page-aligned\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001616 ret = ffa_error(FFA_INVALID_PARAMETERS);
1617 goto out;
1618 }
1619
1620 /* Convert to physical addresses. */
1621 pa_send_begin = pa_from_ipa(send);
1622 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1623 pa_recv_begin = pa_from_ipa(recv);
1624 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1625
1626 /* Fail if the same page is used for the send and receive pages. */
1627 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001628 dlog_error("%s: Mailbox buffers overlap\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001629 ret = ffa_error(FFA_INVALID_PARAMETERS);
1630 goto out;
1631 }
Andrew Sculle1322792019-07-01 17:46:10 +01001632
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001633 /* Set stage 2 translation tables only for virtual FF-A instances. */
1634 if (vm_id_is_current_world(vm_locked.vm->id)) {
1635 /*
1636 * Ensure the pages are valid, owned and exclusive to the VM and
1637 * that the VM has the required access to the memory.
1638 */
1639 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1640 &orig_send_mode) ||
1641 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1642 (orig_send_mode & MM_MODE_R) == 0 ||
1643 (orig_send_mode & MM_MODE_W) == 0) {
1644 dlog_error(
1645 "VM doesn't have required access rights to map "
1646 "TX buffer in stage 2.\n");
1647 ret = ffa_error(FFA_INVALID_PARAMETERS);
1648 goto out;
1649 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001650
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001651 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1652 &orig_recv_mode) ||
1653 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1654 (orig_recv_mode & MM_MODE_R) == 0) {
1655 dlog_error(
1656 "VM doesn't have required access rights to map "
1657 "RX buffer in stage 2.\n");
1658 ret = ffa_error(FFA_INVALID_PARAMETERS);
1659 goto out;
1660 }
Andrew Sculle1322792019-07-01 17:46:10 +01001661
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001662 /* Take memory ownership away from the VM and mark as shared. */
1663 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1664 MM_MODE_W;
1665 if (vm_locked.vm->el0_partition) {
1666 mode |= MM_MODE_USER | MM_MODE_NG;
1667 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001668
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001669 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1670 mode, local_page_pool, NULL)) {
1671 dlog_error(
1672 "Cannot allocate a new entry in stage 2 "
1673 "translation table.\n");
1674 ret = ffa_error(FFA_NO_MEMORY);
1675 goto out;
1676 }
Andrew Sculle1322792019-07-01 17:46:10 +01001677
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001678 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1679 if (vm_locked.vm->el0_partition) {
1680 mode |= MM_MODE_USER | MM_MODE_NG;
1681 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001682
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001683 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1684 mode, local_page_pool, NULL)) {
1685 /* TODO: partial defrag of failed range. */
1686 /* Recover any memory consumed in failed mapping. */
Karl Meakin738bee12024-01-12 15:19:45 +00001687 dlog_error("%s: cannot map recv page\n", __func__);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001688 vm_ptable_defrag(vm_locked, local_page_pool);
Karl Meakin738bee12024-01-12 15:19:45 +00001689 ret = ffa_error(FFA_NO_MEMORY);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001690 goto fail_undo_send;
1691 }
Karl Meakin738bee12024-01-12 15:19:45 +00001692 } else {
1693 ret = arch_other_world_vm_configure_rxtx_map(
1694 vm_locked, local_page_pool, pa_send_begin, pa_send_end,
1695 pa_recv_begin, pa_recv_end);
1696 if (ret.func != FFA_SUCCESS_32) {
1697 goto out;
1698 }
Andrew Sculle1322792019-07-01 17:46:10 +01001699 }
1700
Olivier Deprez96a2a262020-06-11 17:21:38 +02001701 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1702 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1703
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001704 /*
1705 * For EL0 partitions, since both the partition and the hypervisor code
1706 * use the EL2&0 translation regime, it is critical to mark the mappings
1707 * of the send and recv buffers as non-global in the TLB. For one, if we
1708 * dont mark it as non-global, it would cause TLB conflicts since there
1709 * would be an identity mapping with non-global attribute in the
1710 * partitions page tables, but another identity mapping in the
1711 * hypervisor page tables with the global attribute. The other issue is
1712 * one of security, we dont want other partitions to be able to access
1713 * other partitions buffers through cached translations.
1714 */
1715 if (vm_locked.vm->el0_partition) {
1716 extra_attributes |= MM_MODE_NG;
1717 }
1718
Karl Meakin738bee12024-01-12 15:19:45 +00001719 ret = api_vm_configure_stage1(
1720 mm_stage1_locked, vm_locked, pa_send_begin, pa_send_end,
1721 pa_recv_begin, pa_recv_end, extra_attributes, local_page_pool);
1722 if (ret.func != FFA_SUCCESS_32) {
Andrew Sculle1322792019-07-01 17:46:10 +01001723 goto fail_undo_send_and_recv;
1724 }
1725
Manish Pandeyd34f8892020-06-19 17:41:07 +01001726 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001727 goto out;
1728
Andrew Sculle1322792019-07-01 17:46:10 +01001729fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001730 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001731 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001732
1733fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001734 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001735 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001736
1737out:
Andrew Sculle1322792019-07-01 17:46:10 +01001738 return ret;
1739}
1740
Karl Meakinb9705e22024-04-05 13:58:28 +01001741/**
1742 * Read the buffer addresses and VM ID of an FFA_RXTX_MAP request. Handles
1743 * forwarded messages by reading from the hypervisor's TX buffer.
1744 *
1745 * Returns the VM/SP ID on success.
1746 *
1747 * Returns `HF_INVALID_VM_ID` when the arguments provided via the ABI
1748 * FFA_RXTX_MAP indicate the SPMC should retrieve the RXTX description from the
1749 * Hypervisor RXTX buffers, and the hypervisor hasn't given its own RXTX buffers
1750 * for the SPMC to map.
1751 */
1752static ffa_id_t api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send,
1753 ipaddr_t *recv, uint32_t *page_count)
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001754{
Karl Meakinb9705e22024-04-05 13:58:28 +01001755 bool forwarded;
1756 struct vm_locked vm_locked;
1757 ffa_id_t owner_vm_id;
1758 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc;
1759 struct ffa_composite_memory_region *rx_region;
1760 struct ffa_composite_memory_region *tx_region;
1761
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001762 /*
1763 * If the message has been forwarded the effective addresses are in
1764 * hypervisor's TX buffer.
1765 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001766 forwarded = (current_vm->id == HF_OTHER_WORLD_ID) &&
1767 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1768 (*page_count == 0);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001769
1770 if (forwarded) {
Karl Meakinb9705e22024-04-05 13:58:28 +01001771 vm_locked = vm_lock(current_vm);
1772 endpoint_desc = (struct ffa_endpoint_rx_tx_descriptor *)
1773 vm_locked.vm->mailbox.send;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001774
Karl Meakinb9705e22024-04-05 13:58:28 +01001775 if (endpoint_desc == NULL) {
1776 dlog_error(
1777 "Trying to access RXTX description, but "
1778 "hypervisor has not provided RXTX buffers\n");
1779 vm_unlock(&vm_locked);
1780 return HF_INVALID_VM_ID;
1781 }
1782
1783 rx_region = ffa_endpoint_get_rx_memory_region(endpoint_desc);
1784 tx_region = ffa_endpoint_get_tx_memory_region(endpoint_desc);
1785
1786 owner_vm_id = endpoint_desc->endpoint_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001787 *recv = ipa_init(rx_region->constituents[0].address);
1788 *send = ipa_init(tx_region->constituents[0].address);
1789 *page_count = rx_region->constituents[0].page_count;
J-Alves28ad9b42022-12-08 12:19:43 +00001790
1791 vm_unlock(&vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001792 } else {
Karl Meakinb9705e22024-04-05 13:58:28 +01001793 owner_vm_id = current_vm->id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001794 }
Karl Meakinb9705e22024-04-05 13:58:28 +01001795
1796 return owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001797}
Karl Meakinb9705e22024-04-05 13:58:28 +01001798
Andrew Sculle1322792019-07-01 17:46:10 +01001799/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001800 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001801 * must not be shared. Locking of the page tables combined with a local memory
1802 * pool ensures there will always be enough memory to recover from any errors
1803 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1804 * by another core which could result in this transaction being unable to roll
1805 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001806 *
1807 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001808 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001809 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001810 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001811 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001812 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001813 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001814 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001815struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001816 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001817{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001818 struct ffa_value ret;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001819 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001820 struct mm_stage1_locked mm_stage1_locked;
1821 struct mpool local_page_pool;
J-Alves19e20cf2023-08-02 12:48:55 +01001822 ffa_id_t owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001823
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001824 /*
1825 * Get the original buffer addresses and VM ID in case of forwarded
1826 * message.
1827 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001828 owner_vm_id = api_get_rxtx_description(current->vm, &send, &recv,
1829 &page_count);
1830 if (owner_vm_id == HF_INVALID_VM_ID) {
1831 return ffa_error(FFA_INVALID_PARAMETERS);
1832 }
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001833
1834 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1835 if (owner_vm_locked.vm == NULL) {
1836 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1837 owner_vm_id);
1838 return ffa_error(FFA_DENIED);
1839 }
Andrew Scull220e6212018-12-21 18:09:00 +00001840
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001841 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001842 * Create a local pool so any freed memory can't be used by another
1843 * thread. This is to ensure the original mapping can be restored if any
1844 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001845 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001846 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1847
Manish Pandeyd34f8892020-06-19 17:41:07 +01001848 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001849
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001850 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1851 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001852 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001853 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001854 }
1855
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001856 /* Forward buffer mapping to SPMC if coming from a VM. */
1857 plat_ffa_rxtx_map_forward(owner_vm_locked);
1858
Federico Recanati9f1b6532022-04-14 13:15:28 +02001859 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001860
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001861exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001862 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001863 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001864 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001865
1866 return ret;
1867}
1868
1869/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001870 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1871 * translation regime of the caller. Unmap the region for the hypervisor and
1872 * set the memory region to owned and exclusive for the component. Since the
1873 * memory region mapped in the page table, when the buffers were originally
1874 * created we can safely remap it.
1875 *
1876 * Returns:
1877 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1878 * behalf of the caller.
1879 * - FFA_SUCCESS on success if no further action is needed.
1880 */
J-Alves19e20cf2023-08-02 12:48:55 +01001881struct ffa_value api_ffa_rxtx_unmap(ffa_id_t allocator_id, struct vcpu *current)
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001882{
1883 struct vm *vm = current->vm;
1884 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001885 ffa_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001886 struct mm_stage1_locked mm_stage1_locked;
1887 paddr_t send_pa_begin;
1888 paddr_t send_pa_end;
1889 paddr_t recv_pa_begin;
1890 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001891 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001892
J-Alves661e1b72023-08-02 13:39:40 +01001893 if (vm->id == HF_HYPERVISOR_VM_ID && !ffa_is_vm_id(allocator_id)) {
Olivier Deprez106c8ce2024-02-08 11:49:02 +01001894 dlog_verbose(
J-Alves9bd324a2023-01-12 10:52:52 +00001895 "The Hypervisor must specify a valid VM ID in register "
1896 "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n");
1897 return ffa_error(FFA_INVALID_PARAMETERS);
1898 }
1899
Federico Recanati8da9e332022-02-10 11:00:17 +01001900 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1901 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
J-Alves9bd324a2023-01-12 10:52:52 +00001902 dlog_error(
1903 "The register W1 (containing ID) must be 0 at virtual "
1904 "instances.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001905 return ffa_error(FFA_INVALID_PARAMETERS);
1906 }
1907
1908 /* VM ID of which buffers have to be unmapped. */
1909 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1910
1911 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1912 vm = vm_locked.vm;
1913 if (vm == NULL) {
1914 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1915 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001916 return ffa_error(FFA_INVALID_PARAMETERS);
1917 }
1918
1919 /* Get send and receive buffers. */
1920 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001921 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001922 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001923 ret = ffa_error(FFA_INVALID_PARAMETERS);
1924 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001925 }
1926
1927 /* Currently a mailbox size of 1 page is assumed. */
1928 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1929 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1930 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1931 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1932
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001933 mm_stage1_locked = mm_lock_stage1();
1934
Federico Recanati8da9e332022-02-10 11:00:17 +01001935 /* Reset stage 2 mapping only for virtual FF-A instances. */
1936 if (vm_id_is_current_world(owner_vm_id)) {
1937 /*
1938 * Set the memory region of the buffers back to the default mode
1939 * for the VM. Since this memory region was already mapped for
1940 * the RXTX buffers we can safely remap them.
1941 */
1942 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1943 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1944 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001945
Federico Recanati8da9e332022-02-10 11:00:17 +01001946 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1947 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1948 &api_page_pool, NULL));
Karl Meakin738bee12024-01-12 15:19:45 +00001949 } else {
1950 ret = arch_other_world_vm_configure_rxtx_unmap(
1951 vm_locked, &api_page_pool, send_pa_begin, send_pa_end,
1952 recv_pa_begin, recv_pa_end);
1953 if (ret.func != FFA_SUCCESS_32) {
1954 goto out;
1955 }
Federico Recanati8da9e332022-02-10 11:00:17 +01001956 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001957
1958 /* Unmap the buffers in the partition manager. */
1959 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1960 &api_page_pool));
1961 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1962 &api_page_pool));
1963
1964 vm->mailbox.send = NULL;
1965 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001966 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001967
Federico Recanati8da9e332022-02-10 11:00:17 +01001968 /* Forward buffer unmapping to SPMC if coming from a VM. */
J-Alves70079932022-12-07 17:32:20 +00001969 plat_ffa_rxtx_unmap_forward(vm_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001970
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001971 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001972
1973out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001974 vm_unlock(&vm_locked);
1975
Federico Recanati8da9e332022-02-10 11:00:17 +01001976 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001977}
1978
1979/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001980 * Copies data from the sender's send buffer to the recipient's receive buffer
1981 * and notifies the receiver.
1982 */
J-Alves19e20cf2023-08-02 12:48:55 +01001983struct ffa_value api_ffa_msg_send2(ffa_id_t sender_vm_id, uint32_t flags,
Federico Recanati25053ee2022-03-14 15:01:53 +01001984 struct vcpu *current)
1985{
1986 struct vm *from = current->vm;
1987 struct vm *to;
1988 struct vm_locked to_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001989 ffa_id_t msg_sender_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01001990 struct vm_locked sender_locked;
1991 const void *from_msg;
1992 struct ffa_value ret;
J-Alves19e20cf2023-08-02 12:48:55 +01001993 ffa_id_t sender_id;
1994 ffa_id_t receiver_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01001995 uint32_t msg_size;
Federico Recanati25053ee2022-03-14 15:01:53 +01001996
Karl Meakina5ea9092024-05-28 15:40:33 +01001997 alignas(8) struct ffa_partition_rxtx_header header;
1998
Federico Recanati25053ee2022-03-14 15:01:53 +01001999 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
2000 if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) {
2001 dlog_error("Sender VM ID must be zero.\n");
2002 return ffa_error(FFA_INVALID_PARAMETERS);
2003 }
2004
Federico Recanati25053ee2022-03-14 15:01:53 +01002005 /*
2006 * Get message sender's mailbox, which can be different to the `from` vm
2007 * when the message is forwarded.
2008 */
2009 msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id;
2010 sender_locked = plat_ffa_vm_find_locked(msg_sender_id);
2011 if (sender_locked.vm == NULL) {
2012 dlog_error("Cannot send message from VM ID %#x, not found.\n",
2013 msg_sender_id);
2014 return ffa_error(FFA_DENIED);
2015 }
2016
2017 from_msg = sender_locked.vm->mailbox.send;
2018 if (from_msg == NULL) {
2019 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
2020 msg_sender_id);
2021 ret = ffa_error(FFA_DENIED);
2022 goto out_unlock_sender;
2023 }
2024
2025 /*
2026 * Copy message header as safety measure to avoid multiple accesses to
2027 * unsafe memory which could be 'corrupted' between safety checks and
2028 * final buffer copy.
2029 */
J-Alves8a5fd9d2024-04-09 11:22:49 +01002030 if (!memcpy_trapped(&header, FFA_RXTX_HEADER_SIZE, from_msg,
2031 FFA_RXTX_HEADER_SIZE)) {
2032 dlog_error(
2033 "%s: Failed to copy message from sender's(%x) TX "
2034 "buffer.\n",
2035 __func__, sender_locked.vm->id);
2036 ret = ffa_error(FFA_ABORTED);
2037 goto out_unlock_sender;
2038 }
2039
Federico Recanati25053ee2022-03-14 15:01:53 +01002040 sender_id = ffa_rxtx_header_sender(&header);
2041 receiver_id = ffa_rxtx_header_receiver(&header);
2042
2043 /* Ensure Sender IDs from API and from message header match. */
2044 if (msg_sender_id != sender_id) {
2045 dlog_error(
2046 "Message sender VM ID (%#x) doesn't match header's VM "
2047 "ID (%#x).\n",
2048 msg_sender_id, sender_id);
2049 ret = ffa_error(FFA_INVALID_PARAMETERS);
2050 goto out_unlock_sender;
2051 }
2052
2053 /* Disallow reflexive requests as this suggests an error in the VM. */
2054 if (receiver_id == sender_id) {
2055 dlog_error("Sender and receive VM IDs must be different.\n");
2056 ret = ffa_error(FFA_INVALID_PARAMETERS);
2057 goto out_unlock_sender;
2058 }
2059
Federico Recanati7b39ff22022-03-14 15:01:53 +01002060 /* `flags` can be set only at secure virtual FF-A instances. */
J-Alves661e1b72023-08-02 13:39:40 +01002061 if (ffa_is_vm_id(sender_id) && (flags != 0)) {
Federico Recanati7b39ff22022-03-14 15:01:53 +01002062 dlog_error("flags must be zero.\n");
2063 return ffa_error(FFA_INVALID_PARAMETERS);
2064 }
2065
Olivier Deprez37b75112024-03-04 18:27:30 +01002066 if (header.offset != FFA_RXTX_HEADER_SIZE) {
2067 dlog_error("Indirect msg payload must follow the header.\n");
2068 return ffa_error(FFA_INVALID_PARAMETERS);
2069 }
2070
Federico Recanati25053ee2022-03-14 15:01:53 +01002071 /*
2072 * Check if the message has to be forwarded to the SPMC, in
2073 * this case return, the SPMC will handle the buffer copy.
2074 */
2075 if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) {
2076 goto out_unlock_sender;
2077 }
2078
2079 /* Ensure the receiver VM exists. */
2080 to_locked = plat_ffa_vm_find_locked(receiver_id);
2081 to = to_locked.vm;
2082
2083 if (to == NULL) {
2084 dlog_error("Cannot deliver message to VM %#x, not found.\n",
2085 receiver_id);
2086 ret = ffa_error(FFA_INVALID_PARAMETERS);
2087 goto out_unlock_sender;
2088 }
2089
2090 /*
2091 * Check sender and receiver can use indirect messages.
2092 * Sender is the VM/SP who originally sent the message, not the
2093 * hypervisor possibly relaying it.
2094 */
2095 if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) {
2096 dlog_verbose("VM %#x doesn't support indirect message\n",
2097 sender_id);
2098 ret = ffa_error(FFA_DENIED);
2099 goto out;
2100 }
2101
J-Alvese8c8c2b2022-12-16 15:34:48 +00002102 if (vm_is_mailbox_busy(to_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002103 dlog_error(
2104 "Cannot deliver message to VM %#x, RX buffer not "
2105 "ready.\n",
2106 receiver_id);
2107 ret = ffa_error(FFA_BUSY);
2108 goto out;
2109 }
2110
Federico Recanati644f0462022-03-17 12:04:00 +01002111 /* Acquire receiver's RX buffer. */
2112 if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) {
2113 dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id);
2114 goto out;
2115 }
2116
Federico Recanati25053ee2022-03-14 15:01:53 +01002117 /* Check the size of transfer. */
2118 msg_size = FFA_RXTX_HEADER_SIZE + header.size;
Olivier Deprezf1dd1e12024-03-04 18:21:01 +01002119 if ((msg_size > FFA_MSG_PAYLOAD_MAX) ||
Federico Recanati25053ee2022-03-14 15:01:53 +01002120 (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
2121 dlog_error("Message is too big.\n");
2122 ret = ffa_error(FFA_INVALID_PARAMETERS);
2123 goto out;
2124 }
2125
2126 /* Copy data. */
J-Alves8a5fd9d2024-04-09 11:22:49 +01002127 if (!memcpy_trapped(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg,
2128 msg_size)) {
2129 dlog_error(
2130 "%s: Failed to copy message to receiver's(%x) RX "
2131 "buffer.\n",
2132 __func__, to->id);
2133 ret = ffa_error(FFA_ABORTED);
2134 goto out;
2135 }
2136
Federico Recanati25053ee2022-03-14 15:01:53 +01002137 to->mailbox.recv_size = msg_size;
2138 to->mailbox.recv_sender = sender_id;
2139 to->mailbox.recv_func = FFA_MSG_SEND2_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002140 to->mailbox.state = MAILBOX_STATE_FULL;
Federico Recanati25053ee2022-03-14 15:01:53 +01002141
J-Alves3bb82592023-01-23 11:20:54 +00002142 /*
2143 * Set framework notifications, only if the SP has enabled
2144 * receipt of notifications.
2145 * If VMs have provided the RX buffer it is implied they already
2146 * support indirect messaging, and therefore framework notifications.
2147 */
2148 if (ffa_is_vm_id(to_locked.vm->id) ||
2149 vm_are_notifications_enabled(to_locked.vm)) {
2150 ffa_notifications_bitmap_t rx_buffer_full =
2151 ffa_is_vm_id(sender_id)
2152 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
2153 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
Federico Recanati25053ee2022-03-14 15:01:53 +01002154
J-Alves3bb82592023-01-23 11:20:54 +00002155 vm_notifications_framework_set_pending(to_locked,
2156 rx_buffer_full);
2157
2158 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2159 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2160 vcpu_index(current));
2161 plat_ffa_sri_trigger_not_delayed(current->cpu);
2162 } else {
J-Alvesea8ccfe2024-10-09 11:47:24 +01002163 plat_ffa_sri_set_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002164 }
Federico Recanati25053ee2022-03-14 15:01:53 +01002165 }
2166
2167 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2168
2169out:
2170 vm_unlock(&to_locked);
2171
2172out_unlock_sender:
2173 vm_unlock(&sender_locked);
2174
2175 return ret;
2176}
2177
2178/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002179 * Releases the caller's mailbox so that a new message can be received. The
2180 * caller must have copied out all data they wish to preserve as new messages
2181 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002182 *
2183 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002184 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2185 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002186 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2187 * - FFA_SUCCESS on success if no further action is needed.
2188 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002189 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002190 * hf_mailbox_waiter_get.
2191 */
J-Alves19e20cf2023-08-02 12:48:55 +01002192struct ffa_value api_ffa_rx_release(ffa_id_t receiver_id, struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002193{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002194 struct vm *current_vm = current->vm;
2195 struct vm *vm;
2196 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01002197 ffa_id_t current_vm_id = current_vm->id;
2198 ffa_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002199 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002200
Federico Recanati7bef0b92022-03-17 14:56:22 +01002201 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2202 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2203 dlog_error("Invalid `receiver_id`, must be zero.\n");
2204 return ffa_error(FFA_INVALID_PARAMETERS);
2205 }
2206
2207 /*
2208 * VM ID to be released: `receiver_id` if message has been forwarded by
2209 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2210 */
2211 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2212 release_vm_id = current_vm_id;
2213 } else {
2214 release_vm_id = receiver_id;
2215 }
2216
2217 vm_locked = plat_ffa_vm_find_locked(release_vm_id);
2218 vm = vm_locked.vm;
2219 if (vm == NULL) {
2220 dlog_error("No buffer registered for VM ID %#x.\n",
2221 release_vm_id);
2222 return ffa_error(FFA_INVALID_PARAMETERS);
2223 }
2224
J-Alvese8c8c2b2022-12-16 15:34:48 +00002225 if (plat_ffa_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002226 goto out;
2227 }
2228
Karl Meakin1064a9c2024-06-04 17:20:43 +01002229 ret = api_release_mailbox(vm_locked);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002230
2231out:
2232 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002233
2234 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002235}
Andrew Walbran318f5732018-11-20 16:23:42 +00002236
2237/**
Federico Recanati644f0462022-03-17 12:04:00 +01002238 * Acquire ownership of an RX buffer before writing to it. Both
2239 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2240 * could contend for the same buffer. SPMC owns VM's RX buffer after
2241 * it's mapped in its translation regime. This ABI should be
2242 * used by the Hypervisor to get the ownership of a VM's RX buffer
2243 * from the SPMC solving the aforementioned possible contention.
2244 *
2245 * Returns:
2246 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2247 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2248 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2249 */
J-Alves19e20cf2023-08-02 12:48:55 +01002250struct ffa_value api_ffa_rx_acquire(ffa_id_t receiver_id, struct vcpu *current)
Federico Recanati644f0462022-03-17 12:04:00 +01002251{
2252 struct vm_locked receiver_locked;
2253 struct vm *receiver;
2254 struct ffa_value ret;
2255
2256 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
J-Alves661e1b72023-08-02 13:39:40 +01002257 !ffa_is_vm_id(receiver_id)) {
Federico Recanati644f0462022-03-17 12:04:00 +01002258 dlog_error(
2259 "FFA_RX_ACQUIRE not supported at this FF-A "
2260 "instance.\n");
2261 return ffa_error(FFA_NOT_SUPPORTED);
2262 }
2263
2264 receiver_locked = plat_ffa_vm_find_locked(receiver_id);
2265 receiver = receiver_locked.vm;
2266
2267 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2268 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2269 receiver_id);
2270 ret = ffa_error(FFA_INVALID_PARAMETERS);
2271 goto out;
2272 }
2273
2274 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2275 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2276 ret = ffa_error(FFA_DENIED);
2277 goto out;
2278 }
2279
J-Alvese8c8c2b2022-12-16 15:34:48 +00002280 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002281
2282 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2283
2284out:
2285 vm_unlock(&receiver_locked);
2286
2287 return ret;
2288}
2289
J-Alvescc542042024-07-24 19:13:22 +01002290/*
2291 * Returns true if intid relates with either of those:
2292 * - NPI
2293 * - ME
2294 * - Virtual Timer.
2295 *
2296 * These are VIs with no expected interrupt descriptor.
2297 */
2298static bool api_is_maintenance_virtual_interrupt(uint32_t intid)
2299{
2300 return intid == HF_NOTIFICATION_PENDING_INTID ||
2301 intid == HF_MANAGED_EXIT_INTID ||
2302 intid == HF_VIRTUAL_TIMER_INTID;
2303}
2304
Federico Recanati644f0462022-03-17 12:04:00 +01002305/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002306 * Enables or disables a given interrupt ID for the calling vCPU.
2307 *
2308 * Returns 0 on success, or -1 if the intid is invalid.
2309 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002310int64_t api_interrupt_enable(uint32_t intid, bool enable,
2311 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002312{
Manish Pandey35e452f2021-02-18 21:36:34 +00002313 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002314 struct interrupts *interrupts = &current->interrupts;
J-Alvescc542042024-07-24 19:13:22 +01002315 struct interrupt_descriptor *int_desc = NULL;
2316 struct vm *vm = current->vm;
2317 struct vm_locked vm_locked;
2318
2319 int64_t ret = -1;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002320
Andrew Walbran318f5732018-11-20 16:23:42 +00002321 if (intid >= HF_NUM_INTIDS) {
2322 return -1;
2323 }
2324
J-Alvescc542042024-07-24 19:13:22 +01002325 vm_locked = vm_lock(vm);
Manish Pandey35e452f2021-02-18 21:36:34 +00002326 current_locked = vcpu_lock(current);
J-Alvescc542042024-07-24 19:13:22 +01002327
2328 int_desc = vm_interrupt_set_enable(vm_locked, intid, enable);
2329
2330 if (!api_is_maintenance_virtual_interrupt(intid)) {
2331 if (int_desc == NULL) {
2332 dlog_error("%s: invalid interrupt ID.\n", __func__);
2333 goto out;
2334 }
2335
2336 plat_interrupts_configure_interrupt(*int_desc);
2337 }
2338
Andrew Walbran318f5732018-11-20 16:23:42 +00002339 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002340 /*
2341 * If it is pending and was not enabled before, increment the
2342 * count.
2343 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002344 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2345 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2346 vcpu_interrupt_count_increment(current_locked,
2347 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002348 }
J-Alvescc542042024-07-24 19:13:22 +01002349
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002350 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2351 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002352 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002353 /*
2354 * If it is pending and was enabled before, decrement the count.
2355 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002356 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2357 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2358 vcpu_interrupt_count_decrement(current_locked,
2359 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002360 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002361 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2362 vcpu_virt_interrupt_set_type(interrupts, intid,
2363 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002364 }
2365
J-Alvescc542042024-07-24 19:13:22 +01002366 ret = 0;
2367
2368out:
2369 vm_unlock(&vm_locked);
Manish Pandey35e452f2021-02-18 21:36:34 +00002370 vcpu_unlock(&current_locked);
J-Alvescc542042024-07-24 19:13:22 +01002371
2372 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002373}
2374
2375/**
2376 * Returns the ID of the next pending interrupt for the calling vCPU, and
2377 * acknowledges it (i.e. marks it as no longer pending). Returns
2378 * HF_INVALID_INTID if there are no pending interrupts.
2379 */
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002380uint32_t api_interrupt_get(struct vcpu_locked current_locked)
Andrew Walbran318f5732018-11-20 16:23:42 +00002381{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002382 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002383 uint32_t first_interrupt = HF_INVALID_INTID;
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002384 struct interrupts *interrupts = &current_locked.vcpu->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002385
2386 /*
2387 * Find the first enabled and pending interrupt ID, return it, and
2388 * deactivate it.
2389 */
Andrew Walbran318f5732018-11-20 16:23:42 +00002390 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2391 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002392 interrupts->interrupt_enabled.bitmap[i] &
2393 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002394
Andrew Walbran318f5732018-11-20 16:23:42 +00002395 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002396 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002397
2398 first_interrupt =
2399 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002400
Andrew Walbran3d84a262018-12-13 14:41:19 +00002401 /*
2402 * Mark it as no longer pending and decrement the count.
2403 */
J-Alvesb8730e92024-08-07 18:28:55 +01002404 vcpu_interrupt_clear_decrement(current_locked,
2405 first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002406 break;
2407 }
2408 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002409
Andrew Walbran318f5732018-11-20 16:23:42 +00002410 return first_interrupt;
2411}
2412
2413/**
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002414 * Negotiate the FF-A version to be used for this FF-A instance.
2415 * See section 13.2 of the FF-A v1.2 ALP1 spec.
2416 *
2417 * Returns Hafnium's version number (`FFA_VERSION_COMPILED`) on success.
2418 * Returns `FFA_NOT_SUPPORTED` on error:
2419 * - The version is invalid (highest bit set).
2420 * - The requested version is incompatible.
2421 * - The version has already been negotiated and cannot be changed.
2422 */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002423struct ffa_value api_ffa_version(struct vcpu *current,
Karl Meakin0e617d92024-04-05 12:55:22 +01002424 enum ffa_version requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002425{
Karl Meakin0e617d92024-04-05 12:55:22 +01002426 static_assert(sizeof(enum ffa_version) == 4,
2427 "enum ffa_version must be 4 bytes wide");
2428
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002429 const struct ffa_value error = {.func = (uint32_t)FFA_NOT_SUPPORTED};
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002430 struct vm_locked current_vm_locked;
2431
Karl Meakin0e617d92024-04-05 12:55:22 +01002432 if (!ffa_version_is_valid(requested_version)) {
2433 dlog_error(
2434 "FFA_VERSION: requested version %#x is invalid "
2435 "(highest bit must be zero)\n",
2436 requested_version);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002437 return error;
Andrew Walbran9fd29072020-04-22 12:12:14 +01002438 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002439
Karl Meakin0e617d92024-04-05 12:55:22 +01002440 if (!ffa_versions_are_compatible(requested_version,
2441 FFA_VERSION_COMPILED)) {
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002442 dlog_error(
2443 "FFA_VERSION: requested version v%u.%u is not "
2444 "compatible with v%u.%u\n",
2445 ffa_version_get_major(requested_version),
2446 ffa_version_get_minor(requested_version),
2447 ffa_version_get_major(FFA_VERSION_COMPILED),
2448 ffa_version_get_minor(FFA_VERSION_COMPILED));
2449 return error;
J-Alves3cc9d6f2023-07-20 16:46:15 +01002450 }
2451
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002452 current_vm_locked = vm_lock(current->vm);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002453
2454 if (current_vm_locked.vm->ffa_version_negotiated &&
2455 requested_version != current_vm_locked.vm->ffa_version) {
2456 vm_unlock(&current_vm_locked);
2457 dlog_error(
2458 "FFA_VERSION: Cannot change FF-A version after other "
2459 "FF-A calls have been made\n");
2460 return error;
2461 }
2462
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002463 current_vm_locked.vm->ffa_version = requested_version;
2464 vm_unlock(&current_vm_locked);
2465
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002466 return (struct ffa_value){.func = (uint32_t)FFA_VERSION_COMPILED};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002467}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002468
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002469/**
Karl Meakin49ec1e42024-05-10 13:08:24 +01002470 * Helper for success return of FFA_FEATURES.
J-Alves6f72ca82021-11-01 12:34:58 +00002471 */
2472struct ffa_value api_ffa_feature_success(uint32_t arg2)
2473{
2474 return (struct ffa_value){
Karl Meakin49ec1e42024-05-10 13:08:24 +01002475 .func = FFA_SUCCESS_32,
2476 .arg1 = 0U,
2477 .arg2 = arg2,
2478 };
J-Alves6f72ca82021-11-01 12:34:58 +00002479}
2480
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002481static struct ffa_value ffa_features_function(uint32_t func,
2482 uint32_t input_property,
2483 struct vcpu *current)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002484{
Karl Meakin0e617d92024-04-05 12:55:22 +01002485 const enum ffa_version ffa_version = current->vm->ffa_version;
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002486 const bool el0_partition = current->vm->el0_partition;
2487
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002488 switch (func) {
J-Alves6f72ca82021-11-01 12:34:58 +00002489 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002490 case FFA_ERROR_32:
2491 case FFA_SUCCESS_32:
2492 case FFA_INTERRUPT_32:
2493 case FFA_VERSION_32:
2494 case FFA_FEATURES_32:
2495 case FFA_RX_RELEASE_32:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002496 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002497 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002498 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002499 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002500 case FFA_RUN_32:
J-Alves95fbb312024-03-20 15:19:16 +00002501 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002502 case FFA_MEM_DONATE_32:
2503 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00002504 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002505 case FFA_MEM_SHARE_32:
J-Alves95fbb312024-03-20 15:19:16 +00002506 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002507 case FFA_MEM_RETRIEVE_RESP_32:
2508 case FFA_MEM_RELINQUISH_32:
2509 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002510 case FFA_MEM_FRAG_RX_32:
2511 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002512 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002513 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002514 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002515 case FFA_MSG_SEND_DIRECT_REQ_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002516
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002517 /* FF-A v1.1 features. */
2518 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002519 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2520 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2521 case FFA_NOTIFICATION_BIND_32:
2522 case FFA_NOTIFICATION_UNBIND_32:
2523 case FFA_NOTIFICATION_SET_32:
2524 case FFA_NOTIFICATION_GET_32:
2525 case FFA_NOTIFICATION_INFO_GET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002526 case FFA_MSG_SEND2_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002527 if (FFA_VERSION_1_1 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002528 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002529 }
2530
Kathleen Capellae4fe2962023-09-01 17:08:47 -04002531 /* FF-A v1.2 features. */
2532 case FFA_CONSOLE_LOG_32:
2533 case FFA_CONSOLE_LOG_64:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002534 case FFA_PARTITION_INFO_GET_REGS_64:
Kathleen Capella41fea932023-06-23 17:39:28 -04002535 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002536 case FFA_MSG_SEND_DIRECT_RESP2_64:
Karl Meakin0e617d92024-04-05 12:55:22 +01002537 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002538 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002539 }
2540
Karl Meakin49ec1e42024-05-10 13:08:24 +01002541 return api_ffa_feature_success(0);
2542
Karl Meakin5a222642024-06-20 10:23:37 +01002543 /* These functions are only supported on S-EL0 partitions. */
2544 case FFA_MEM_PERM_GET_32:
2545 case FFA_MEM_PERM_SET_32:
2546 case FFA_MEM_PERM_GET_64:
2547 case FFA_MEM_PERM_SET_64:
2548 if (!(vm_id_is_current_world(current->vm->id) &&
2549 el0_partition)) {
2550 dlog_verbose(
2551 "FFA_FEATURE: %s is only supported on S-EL0 "
2552 "partitions\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002553 ffa_func_name(func));
Karl Meakin5a222642024-06-20 10:23:37 +01002554 return ffa_error(FFA_NOT_SUPPORTED);
2555 }
2556 return api_ffa_feature_success(0);
2557
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002558 case FFA_SECONDARY_EP_REGISTER_64:
2559 if (FFA_VERSION_COMPILED < FFA_VERSION_1_1) {
2560 return ffa_error(FFA_NOT_SUPPORTED);
2561 }
2562
2563 if (!(vm_id_is_current_world(current->vm->id) &&
2564 current->vm->vcpu_count > 1)) {
2565 dlog_verbose(
2566 "FFA_FEATURE: %s is only supported on SPs with "
2567 "more than 1 vCPU\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002568 ffa_func_name(func));
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002569 return ffa_error(FFA_NOT_SUPPORTED);
2570 }
2571 return api_ffa_feature_success(0);
2572
Karl Meakin650cb142024-06-20 10:31:57 +01002573 /*
2574 * This function is restricted to the secure virtual FF-A instance (i.e.
2575 * only report success to SPs).
2576 */
2577 case FFA_YIELD_32:
2578 if (!vm_id_is_current_world(current->vm->id)) {
2579 dlog_verbose(
2580 "FFA_FEATURES: %s is only supported at secure "
2581 "virtual FF-A instance\n",
2582 ffa_func_name(FFA_YIELD_32));
2583 return ffa_error(FFA_NOT_SUPPORTED);
2584 }
2585 return api_ffa_feature_success(0);
2586
Karl Meakin0fd67292024-02-06 17:33:05 +00002587 case FFA_RXTX_MAP_64: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002588 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002589 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002590 }
2591
Karl Meakin0fd67292024-02-06 17:33:05 +00002592 uint32_t arg2 = 0;
2593 struct ffa_features_rxtx_map_params params = {
2594 .min_buf_size = FFA_RXTX_MAP_MIN_BUF_4K,
2595 .mbz = 0,
2596 .max_buf_size =
Karl Meakin0e617d92024-04-05 12:55:22 +01002597 (ffa_version >= FFA_VERSION_1_2)
Karl Meakin0fd67292024-02-06 17:33:05 +00002598 ? FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT
2599 : 0,
2600 };
2601
2602 memcpy_s(&arg2, sizeof(arg2), &params, sizeof(params));
Karl Meakin49ec1e42024-05-10 13:08:24 +01002603 return api_ffa_feature_success(arg2);
Karl Meakin0fd67292024-02-06 17:33:05 +00002604 }
2605
J-Alves95fbb312024-03-20 15:19:16 +00002606 case FFA_MEM_RETRIEVE_REQ_64:
Karl Meakin49ec1e42024-05-10 13:08:24 +01002607 case FFA_MEM_RETRIEVE_REQ_32: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002608 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002609 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002610 }
2611
Karl Meakin49ec1e42024-05-10 13:08:24 +01002612 if (ANY_BITS_SET(input_property,
2613 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2614 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT) ||
2615 IS_BIT_SET(input_property,
2616 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT)) {
2617 dlog_warning(
2618 "FFA_FEATURES: Bits[%u:%u] and Bit[%u] of "
2619 "input_property should be 0 (input_property = "
2620 "%#x)\n",
2621 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2622 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT,
2623 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT,
Karl Meakin34b8ae92023-01-13 13:33:07 +00002624 input_property);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002625 }
2626
Karl Meakin0e617d92024-04-05 12:55:22 +01002627 if (ffa_version >= FFA_VERSION_1_1 &&
2628 (input_property &
2629 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
Karl Meakin4e8e4792024-07-05 16:28:42 +01002630 dlog_verbose(
2631 "FFA_FEATURES: NS bit support must be 1\n");
Karl Meakin6fd6c1d2024-05-13 12:09:13 +01002632 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002633 }
2634
2635 return api_ffa_feature_success(
2636 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
Karl Meakin49ec1e42024-05-10 13:08:24 +01002637 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT |
Karl Meakin34b8ae92023-01-13 13:33:07 +00002638 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
Karl Meakin49ec1e42024-05-10 13:08:24 +01002639 }
J-Alves6f72ca82021-11-01 12:34:58 +00002640
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002641 default:
2642 return ffa_error(FFA_NOT_SUPPORTED);
2643 }
2644}
2645
2646static struct ffa_value ffa_features_feature(enum ffa_feature_id feature,
2647 uint32_t input_property,
2648 struct vcpu *current)
2649{
2650 const bool el0_partition = current->vm->el0_partition;
2651
2652 if (ANY_BITS_SET(feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2653 FFA_FEATURES_FEATURE_MBZ_LO_BIT)) {
2654 dlog_verbose(
2655 "FFA_FEATURES: feature ID %#x is invalid (bits [%u:%u] "
2656 "must be zero)\n",
2657 feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2658 FFA_FEATURES_FEATURE_MBZ_LO_BIT);
2659 return ffa_error(FFA_NOT_SUPPORTED);
2660 }
2661 if (input_property != 0) {
2662 dlog_verbose(
2663 "FFA_FEATURES: input_property must be 0 "
2664 "(input_property = %#x)\n",
2665 input_property);
2666 return ffa_error(FFA_NOT_SUPPORTED);
2667 }
2668
2669 switch (feature) {
J-Alves6f72ca82021-11-01 12:34:58 +00002670 /* Check support of a feature provided respective feature ID. */
Karl Meakin17c9ad32024-04-12 16:41:00 +01002671
2672 /*
2673 * For NPI and MEI, report the IDs as supported only to partitions at
2674 * the virtual FF-A instances.
2675 */
J-Alves6f72ca82021-11-01 12:34:58 +00002676 case FFA_FEATURE_NPI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002677 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002678 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002679 }
2680
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002681 if (el0_partition) {
2682 return ffa_error(FFA_NOT_SUPPORTED);
2683 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002684 if (!vm_id_is_current_world(current->vm->id)) {
2685 return ffa_error(FFA_NOT_SUPPORTED);
2686 }
J-Alves6f72ca82021-11-01 12:34:58 +00002687 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
Karl Meakin17c9ad32024-04-12 16:41:00 +01002688
2689 case FFA_FEATURE_MEI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002690 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002691 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002692 }
J-Alvesc4d9ae82024-05-14 10:15:03 +01002693 if (el0_partition) {
2694 return ffa_error(FFA_NOT_SUPPORTED);
2695 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002696 if (!vm_id_is_current_world(current->vm->id)) {
2697 return ffa_error(FFA_NOT_SUPPORTED);
2698 }
2699 return api_ffa_feature_success(HF_MANAGED_EXIT_INTID);
2700
J-Alves6f72ca82021-11-01 12:34:58 +00002701 case FFA_FEATURE_SRI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002702 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002703 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002704 }
2705
Karl Meakincb6642c2024-02-27 14:43:45 +00002706 if (!ffa_is_vm_id(current->vm->id)) {
2707 return ffa_error(FFA_NOT_SUPPORTED);
2708 }
J-Alves6f72ca82021-11-01 12:34:58 +00002709 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
Karl Meakin0e617d92024-04-05 12:55:22 +01002710
J-Alves6f72ca82021-11-01 12:34:58 +00002711 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002712 default:
Karl Meakin4271ff92024-05-13 12:33:15 +01002713 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002714 }
2715}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002716
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002717/**
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002718 * Discovery function returning information about the implementation of optional
2719 * FF-A interfaces. See section 13.3 of the FF-A v1.2 ALP1 spec.
2720 *
2721 * `function_or_feature_id` is interpreted as either a function ID or a feature
2722 * ID, depending on the value of bit 31.
2723 * When it is a feature ID, bits [30:8] MBZ and input_property MBZ.
2724 *
2725 * Returns `FFA_SUCCESS` if the interface is supported.
2726 * Returns `FFA_NOT_SUPPORTED` if the interface is not supported or the
2727 * parameters are invalid.
2728 */
2729struct ffa_value api_ffa_features(uint32_t function_or_feature_id,
2730 uint32_t input_property, struct vcpu *current)
2731{
2732 return IS_BIT_UNSET(function_or_feature_id, FFA_FEATURES_FEATURE_BIT)
2733 ? ffa_features_feature(function_or_feature_id,
2734 input_property, current)
2735 : ffa_features_function(function_or_feature_id,
2736 input_property, current);
2737}
2738
2739/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002740 * FF-A specification states that x2/w2 Must Be Zero for FFA_MSG_SEND_DIRECT_REQ
2741 * and FFA_MSG_SEND_DIRECT_RESP interfaces when used for partition messages. See
2742 * FF-A v1.2 Table 16.6: FFA_MSG_SEND_DIRECT_REQ function syntax.
J-Alves645eabe2021-02-22 16:08:27 +00002743 */
2744static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2745{
2746 return args.arg2 == 0U;
2747}
2748
2749/**
J-Alves76d99af2021-03-10 17:42:11 +00002750 * Limits size of arguments in ffa_value structure to 32-bit.
2751 */
2752static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2753{
2754 return (struct ffa_value){
2755 .func = (uint32_t)args.func,
2756 .arg1 = (uint32_t)args.arg1,
2757 .arg2 = (uint32_t)0,
2758 .arg3 = (uint32_t)args.arg3,
2759 .arg4 = (uint32_t)args.arg4,
2760 .arg5 = (uint32_t)args.arg5,
2761 .arg6 = (uint32_t)args.arg6,
2762 .arg7 = (uint32_t)args.arg7,
2763 };
2764}
2765
2766/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002767 * Helper to copy direct message payload, depending on SMC used, direct
2768 * messaging interface used, and expected registers size.
J-Alves76d99af2021-03-10 17:42:11 +00002769 */
2770static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2771{
2772 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2773 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2774 return api_ffa_value_copy32(args);
2775 }
2776
Kathleen Capella41fea932023-06-23 17:39:28 -04002777 if (args.func == FFA_MSG_SEND_DIRECT_REQ_64 ||
2778 args.func == FFA_MSG_SEND_DIRECT_RESP_64) {
2779 args.arg2 = 0;
2780 }
2781
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002782 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64) {
2783 args.extended_val.valid = true;
2784 }
2785
Kathleen Capella087e5022023-09-07 18:04:15 -04002786 if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64) {
2787 args.arg2 = 0;
2788 args.arg3 = 0;
2789 }
2790
Kathleen Capella41fea932023-06-23 17:39:28 -04002791 return args;
2792}
2793
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002794/**
2795 * Return a pointer to the first UUID in `uuids` that is equal to
2796 * `target_uuid`. If `target_uuid` is 0-0-0-0, it matches any UUID.
2797 */
2798static struct ffa_uuid *ffa_uuid_find(struct ffa_uuid *uuids,
2799 size_t uuids_count,
2800 struct ffa_uuid target_uuid)
2801{
2802 if (ffa_uuid_is_null(&target_uuid)) {
2803 return &uuids[0];
2804 }
2805
2806 for (size_t i = 0; i < uuids_count; i++) {
2807 if (ffa_uuid_is_null(&uuids[i])) {
2808 break;
2809 }
2810 if (ffa_uuid_equal(&target_uuid, &uuids[i])) {
2811 return &uuids[i];
2812 }
2813 }
2814 return NULL;
2815}
2816
Kathleen Capella41fea932023-06-23 17:39:28 -04002817static bool api_ffa_dir_msg_req2_is_uuid_valid(struct vm *receiver_vm,
2818 struct ffa_value args)
2819{
2820 struct ffa_uuid target_uuid;
2821
Karl Meakin9478e322024-09-23 17:47:09 +01002822 ffa_uuid_from_u64x2(args.arg2, args.arg3, &target_uuid);
Kathleen Capella41fea932023-06-23 17:39:28 -04002823
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002824 return ffa_uuid_find(receiver_vm->uuids, PARTITION_MAX_UUIDS,
2825 target_uuid) != NULL;
J-Alves76d99af2021-03-10 17:42:11 +00002826}
2827
2828/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002829 * Send an FF-A direct message request.
Kathleen Capella41fea932023-06-23 17:39:28 -04002830 * This handler covers both FFA_MSG_SEND_DIRECT_REQ_32/64
2831 * and FFA_MSG_SEND_DIRECT_REQ2_64 (introduced in FF-A v1.2) with function-based
2832 * checks to accomodate for the difference between the ABIs.
2833 *
2834 * FFA_MSG_SEND_DIRECT_REQ2_64 works mostly the same as
2835 * FFA_MSG_SEND_DIRECT_REQ_32/64, but adds the ability to send a direct message
2836 * request to a specified UUID within a partition and the usage of an extended
2837 * range of registers (x4-x17, instead of x4-x7) to be used as part of the
2838 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002839 */
J-Alves19e20cf2023-08-02 12:48:55 +01002840struct ffa_value api_ffa_msg_send_direct_req(ffa_id_t sender_vm_id,
2841 ffa_id_t receiver_vm_id,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002842 struct ffa_value args,
2843 struct vcpu *current,
2844 struct vcpu **next)
2845{
J-Alves17228f72021-04-20 17:13:19 +01002846 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002847 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002848 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002849 struct vcpu *receiver_vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002850 struct vcpu_locked current_locked;
2851 struct vcpu_locked receiver_vcpu_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002852 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002853 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002854
Kathleen Capella41fea932023-06-23 17:39:28 -04002855 if ((args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2856 args.func == FFA_MSG_SEND_DIRECT_REQ_64) &&
2857 !api_ffa_dir_msg_is_arg2_zero(args)) {
J-Alves645eabe2021-02-22 16:08:27 +00002858 return ffa_error(FFA_INVALID_PARAMETERS);
2859 }
2860
Olivier Deprez55a189e2021-06-09 15:45:27 +02002861 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2862 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002863 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002864 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002865 }
2866
Olivier Deprez55a189e2021-06-09 15:45:27 +02002867 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002868 return ret;
2869 }
2870
J-Alvesbd32c972024-02-02 16:20:04 +00002871 ret = api_ffa_interrupt_return(0);
J-Alves17228f72021-04-20 17:13:19 +01002872
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002873 receiver_vm = vm_find(receiver_vm_id);
2874 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002875 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002876 return ffa_error(FFA_INVALID_PARAMETERS);
2877 }
2878
Kathleen Capella41fea932023-06-23 17:39:28 -04002879 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64 &&
2880 !api_ffa_dir_msg_req2_is_uuid_valid(receiver_vm, args)) {
2881 dlog_verbose("UUID unrecognized for this VM\n");
2882 return ffa_error(FFA_INVALID_PARAMETERS);
2883 }
2884
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002885 /*
J-Alves439ac972021-11-18 17:32:03 +00002886 * Check if sender supports sending direct message req, and if
2887 * receiver supports receipt of direct message requests.
2888 */
Kathleen Capella41fea932023-06-23 17:39:28 -04002889 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm,
2890 args.func)) {
J-Alves439ac972021-11-18 17:32:03 +00002891 return ffa_error(FFA_DENIED);
2892 }
2893
2894 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002895 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002896 * UP (migratable) or MP partitions with a number of vCPUs matching the
2897 * number of PEs in the system. It further states that MP partitions
2898 * accepting direct request messages cannot migrate.
2899 */
J-Alvesad6a0432021-04-09 16:06:21 +01002900 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002901 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002902 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002903 return ffa_error(FFA_INVALID_PARAMETERS);
2904 }
2905
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002906 /*
2907 * If VM must be locked, it must be done before any of its vCPUs are
2908 * locked.
2909 */
J-Alves6e2abc62021-12-02 14:58:56 +00002910 receiver_locked = vm_lock(receiver_vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002911
2912 /* Lock both vCPUs at once to avoid deadlock. */
2913 vcpus_locked = vcpu_lock_both(current, receiver_vcpu);
2914 current_locked = vcpus_locked.vcpu1;
2915 receiver_vcpu_locked = vcpus_locked.vcpu2;
2916
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002917 /*
2918 * If destination vCPU is executing or already received an
2919 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2920 * busy. There is a brief period of time where the vCPU state has
2921 * changed but regs_available is still false thus consider this case as
2922 * the vCPU not yet ready to receive a direct message request.
2923 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002924 if (is_ffa_direct_msg_request_ongoing(receiver_vcpu_locked) ||
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002925 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2926 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002927 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002928 ret = ffa_error(FFA_BUSY);
2929 goto out;
2930 }
2931
J-Alves7ea3f7e2024-03-27 11:05:11 +00002932 if (!plat_ffa_check_runtime_state_transition(
2933 current_locked, sender_vm_id, HF_INVALID_VM_ID,
2934 receiver_vcpu_locked, args.func, &next_state)) {
2935 ret = ffa_error(FFA_DENIED);
2936 goto out;
2937 }
2938
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002939 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2940 memory_order_relaxed)) {
2941 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04002942 dlog_verbose(
2943 "Receiver VM %#x aborted, cannot run vCPU %u\n",
2944 receiver_vcpu->vm->id,
2945 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002946 receiver_vcpu->state = VCPU_STATE_ABORTED;
2947 }
2948
2949 ret = ffa_error(FFA_ABORTED);
2950 goto out;
2951 }
2952
2953 switch (receiver_vcpu->state) {
2954 case VCPU_STATE_OFF:
2955 case VCPU_STATE_RUNNING:
2956 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002957 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002958 case VCPU_STATE_BLOCKED:
2959 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002960 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2961 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002962 ret = ffa_error(FFA_BUSY);
2963 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002964 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002965 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002966 * We expect target vCPU to be in WAITING state after either
2967 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002968 */
2969 break;
2970 }
2971
2972 /* Inject timer interrupt if any pending */
2973 if (arch_timer_pending(&receiver_vcpu->regs)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002974 api_interrupt_inject_locked(receiver_vcpu_locked,
2975 HF_VIRTUAL_TIMER_INTID,
2976 current_locked, NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002977
2978 arch_timer_mask(&receiver_vcpu->regs);
2979 }
2980
2981 /* The receiver vCPU runs upon direct message invocation */
2982 receiver_vcpu->cpu = current->cpu;
2983 receiver_vcpu->state = VCPU_STATE_RUNNING;
2984 receiver_vcpu->regs_available = false;
Kathleen Capellae468c112023-12-13 17:56:28 -05002985 receiver_vcpu->direct_request_origin.is_ffa_req2 =
2986 (args.func == FFA_MSG_SEND_DIRECT_REQ2_64);
2987 receiver_vcpu->direct_request_origin.vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002988
J-Alves76d99af2021-03-10 17:42:11 +00002989 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002990
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002991 assert(!vm_id_is_current_world(current->vm->id) ||
2992 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002993 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002994
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07002995 plat_ffa_wind_call_chain_ffa_direct_req(
2996 current_locked, receiver_vcpu_locked, sender_vm_id);
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05002997
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002998 /* Switch to receiver vCPU targeted to by direct msg request */
2999 *next = receiver_vcpu;
3000
J-Alves6e2abc62021-12-02 14:58:56 +00003001 if (!receiver_locked.vm->el0_partition) {
3002 /*
3003 * If the scheduler in the system is giving CPU cycles to the
3004 * receiver, due to pending notifications, inject the NPI
3005 * interrupt. Following call assumes that '*next' has been set
3006 * to receiver_vcpu.
3007 */
3008 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003009 receiver_vcpu_locked, current_locked, receiver_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003010 }
3011
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003012 /*
3013 * Since this flow will lead to a VM switch, the return value will not
3014 * be applied to current vCPU.
3015 */
3016
3017out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003018 vcpu_unlock(&receiver_vcpu_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003019 vm_unlock(&receiver_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003020 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003021
3022 return ret;
3023}
3024
3025/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003026 * Resume the target vCPU after the current vCPU sent a direct response.
3027 * Current vCPU moves to waiting state.
3028 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003029void api_ffa_resume_direct_resp_target(struct vcpu_locked current_locked,
3030 struct vcpu **next,
J-Alves19e20cf2023-08-02 12:48:55 +01003031 ffa_id_t receiver_vm_id,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003032 struct ffa_value to_ret,
3033 bool is_nwd_call_chain)
3034{
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07003035 if (plat_ffa_is_spmd_lp_id(receiver_vm_id) ||
3036 !vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003037 *next = api_switch_to_other_world(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003038 VCPU_STATE_WAITING);
3039
3040 /* End of NWd scheduled call chain. */
3041 assert(!is_nwd_call_chain ||
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003042 (current_locked.vcpu->call_chain.prev_node == NULL));
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003043 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003044 *next = api_switch_to_primary(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003045 VCPU_STATE_WAITING);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003046 } else if (vm_id_is_current_world(receiver_vm_id)) {
3047 /*
3048 * It is expected the receiver_vm_id to be from an SP, otherwise
3049 * 'plat_ffa_is_direct_response_valid' should have
3050 * made function return error before getting to this point.
3051 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003052 *next = api_switch_to_vm(current_locked, to_ret,
3053 VCPU_STATE_WAITING, receiver_vm_id);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003054 } else {
3055 panic("Invalid direct message response invocation");
3056 }
3057}
3058
3059/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003060 * Send an FF-A direct message response.
Kathleen Capella087e5022023-09-07 18:04:15 -04003061 * This handler covers both FFA_MSG_SEND_DIRECT_RESP_32/64
3062 * and FFA_MSG_SEND_DIRECT_RESP2_64 (introduced in FF-A v1.2) with
3063 * function-based checks to accomodate for the difference between the ABIs.
3064 *
3065 * FFA_MSG_SEND_DIRECT_RESP2_64 is used to respond to requests sent via
3066 * FFA_MSG_SEND_DIRECT_REQ2_64 and adds the usage of an extended range
3067 * of registers (x4-x17, instead of x4-x7) to be used as part of the
3068 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003069 */
J-Alves19e20cf2023-08-02 12:48:55 +01003070struct ffa_value api_ffa_msg_send_direct_resp(ffa_id_t sender_vm_id,
3071 ffa_id_t receiver_vm_id,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003072 struct ffa_value args,
3073 struct vcpu *current,
3074 struct vcpu **next)
3075{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003076 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003077 struct vcpu_locked next_locked = (struct vcpu_locked){
3078 .vcpu = NULL,
3079 };
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003080 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003081 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003082 struct ffa_value signal_interrupt =
3083 (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003084 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
3085 struct two_vcpu_locked vcpus_locked;
Kathleen Capellae468c112023-12-13 17:56:28 -05003086 bool received_req2;
J-Alves645eabe2021-02-22 16:08:27 +00003087
Kathleen Capella35238872024-01-12 15:05:52 -05003088 /*
3089 * If using FFA_MSG_SEND_DIRECT_RESP, the caller's
3090 * - x2 MBZ for partition messages
3091 * - x8-x17 SBZ if caller's FF-A version >= FF-A v1.2
3092 */
3093 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) {
3094 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
3095 return ffa_error(FFA_INVALID_PARAMETERS);
3096 }
3097
Karl Meakin0e617d92024-04-05 12:55:22 +01003098 if (current->vm->ffa_version >= FFA_VERSION_1_2 &&
Kathleen Capella35238872024-01-12 15:05:52 -05003099 !api_extended_args_are_zero(&args)) {
3100 return ffa_error(FFA_INVALID_PARAMETERS);
3101 }
J-Alves645eabe2021-02-22 16:08:27 +00003102 }
3103
Olivier Deprez55a189e2021-06-09 15:45:27 +02003104 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
3105 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00003106 dlog_verbose("Invalid direct response call.\n");
J-Alvesaa336102021-03-01 13:02:45 +00003107 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003108 }
3109
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003110 current_locked = vcpu_lock(current);
3111
3112 if (!plat_ffa_check_runtime_state_transition(
3113 current_locked, sender_vm_id, receiver_vm_id, next_locked,
3114 args.func, &next_state)) {
3115 ret = ffa_error(FFA_DENIED);
3116 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05003117 }
3118
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003119 assert(!vm_id_is_current_world(current->vm->id) ||
3120 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003121
3122 /*
3123 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
3124 * defined originator.
3125 */
3126 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
3127 /*
3128 * Sending direct response but direct request origin
3129 * vCPU is not set.
3130 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003131 ret = ffa_error(FFA_DENIED);
3132 goto out;
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003133 }
3134
Kathleen Capellae468c112023-12-13 17:56:28 -05003135 received_req2 = current->direct_request_origin.is_ffa_req2;
3136
3137 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64 && received_req2) {
3138 dlog_verbose(
3139 "%s: FFA_MSG_SEND_DIRECT_RESP must be used with "
3140 "FFA_MSG_SEND_DIRECT_REQ.\n",
3141 __func__);
3142 ret = ffa_error(FFA_DENIED);
3143 goto out;
3144 } else if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64 &&
3145 !received_req2) {
3146 dlog_verbose(
3147 "%s: FFA_MSG_SEND_DIRECT_RESP2 must be used with "
3148 "FFA_MSG_SEND_DIRECT_REQ2.\n",
3149 __func__);
3150 ret = ffa_error(FFA_DENIED);
3151 goto out;
3152 }
3153
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003154 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003155 struct interrupts *interrupts = &current->interrupts;
3156
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003157 /*
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003158 * Per FF-A v1.1 EAC0 section 8.3.1.2.1 rule 6, SPMC can signal
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003159 * a secure interrupt to a SP that is performing managed exit.
3160 * We have taken a implementation defined choice to not allow
3161 * Managed exit while a SP is processing a secure interrupt.
3162 */
Madhukar Pappireddy7945bb52024-08-20 15:22:41 -05003163 CHECK(current->scheduling_mode != SPMC_MODE);
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003164
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06003165 plat_interrupts_set_priority_mask(current->priority_mask);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003166 /*
3167 * A SP may be signaled a managed exit but actually not trap
3168 * the virtual interrupt, probably because it has virtual
3169 * interrupts masked, and emit direct resp. In this case the
3170 * managed exit operation is considered completed and it would
3171 * also need to clear the pending managed exit flag for the SP
3172 * vCPU.
3173 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003174 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003175
3176 if (vcpu_is_virt_interrupt_pending(interrupts,
3177 HF_MANAGED_EXIT_INTID)) {
J-Alvesb8730e92024-08-07 18:28:55 +01003178 vcpu_interrupt_clear_decrement(current_locked,
3179 HF_MANAGED_EXIT_INTID);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003180 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003181 }
3182
Kathleen Capellae468c112023-12-13 17:56:28 -05003183 /* Clear direct request origin vm_id and request type for the caller. */
3184 current->direct_request_origin.is_ffa_req2 = false;
3185 current->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003186
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003187 api_ffa_resume_direct_resp_target(current_locked, next, receiver_vm_id,
3188 to_ret, false);
3189
3190 /*
3191 * Unlock current vCPU to allow it to be locked together with next
3192 * vcpu.
3193 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003194 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003195
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003196 /* Lock both vCPUs at once to avoid deadlock. */
3197 vcpus_locked = vcpu_lock_both(current, *next);
3198 current_locked = vcpus_locked.vcpu1;
3199 next_locked = vcpus_locked.vcpu2;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003200
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003201 plat_ffa_unwind_call_chain_ffa_direct_resp(current_locked, next_locked);
J-Alvesbd32c972024-02-02 16:20:04 +00003202
3203 /*
3204 * Check if there is a pending secure interrupt.
3205 * If there is, return back to the caller with FFA_INTERRUPT,
3206 * and set the `next` vcpu in a preempted state.
3207 */
3208 if (plat_ffa_intercept_call(current_locked, next_locked,
3209 &signal_interrupt)) {
3210 ret = signal_interrupt;
3211 *next = NULL;
3212 }
3213
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003214 vcpu_unlock(&next_locked);
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05003215
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003216out:
3217 vcpu_unlock(&current_locked);
3218 return ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003219}
3220
J-Alves84658fc2021-06-17 14:37:32 +01003221static bool api_memory_region_check_flags(
3222 struct ffa_memory_region *memory_region, uint32_t share_func)
3223{
3224 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00003225 case FFA_MEM_SHARE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003226 case FFA_MEM_SHARE_32:
3227 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3228 0U) {
3229 return false;
3230 }
3231 /* Intentional fall-through */
J-Alves95fbb312024-03-20 15:19:16 +00003232 case FFA_MEM_LEND_64:
J-Alves84658fc2021-06-17 14:37:32 +01003233 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00003234 case FFA_MEM_DONATE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003235 case FFA_MEM_DONATE_32: {
3236 /* Bits 31:2 Must Be Zero. */
3237 ffa_memory_receiver_flags_t to_mask =
3238 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
3239 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
3240
3241 if ((memory_region->flags & to_mask) != 0U) {
3242 return false;
3243 }
3244 break;
3245 }
3246 default:
3247 panic("Check for mem send calls only.\n");
3248 }
3249
3250 /* Last check reserved values are 0 */
3251 return true;
3252}
3253
J-Alves33c47bf2022-09-29 11:36:20 +01003254/*
3255 * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0.
3256 */
3257static void api_ffa_memory_region_v1_1_from_v1_0(
3258 struct ffa_memory_region_v1_0 *memory_region_v1_0,
3259 struct ffa_memory_region *memory_region_v1_1)
3260{
3261 memory_region_v1_1->sender = memory_region_v1_0->sender;
J-Alves00847062022-10-03 17:08:44 +01003262 memory_region_v1_1->handle = memory_region_v1_0->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003263 memory_region_v1_1->attributes =
3264 ffa_memory_attributes_extend(memory_region_v1_0->attributes);
J-Alves33c47bf2022-09-29 11:36:20 +01003265 memory_region_v1_1->flags = memory_region_v1_0->flags;
3266 memory_region_v1_1->tag = memory_region_v1_0->tag;
3267 memory_region_v1_1->memory_access_desc_size =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003268 sizeof(struct ffa_memory_access_v1_0);
J-Alves33c47bf2022-09-29 11:36:20 +01003269 memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003270 memory_region_v1_1->receivers_offset = sizeof(struct ffa_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003271
3272 /* Zero reserved fields. */
3273 for (uint32_t i = 0; i < 3U; i++) {
3274 memory_region_v1_1->reserved[i] = 0U;
3275 }
3276}
3277
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003278/**
3279 * Updates a v1.0 transaction descriptor to v1.1. This gives us the
3280 * memory_access_desc_size field we need for forwards compatability.
3281 * Copy the receivers and composite descriptors to the new struct.
3282 * We also check the fields in the v1.0 transaction descriptor and return:
3283 * - FFA_ERROR FFA_INVALID_PARAMETERS: If any of the fields are not valid
3284 * values, eg the reserved fields are not 0, receiver_count is too large or
3285 * composite offsets are not 0 for retrieve requests or in bounds for send
3286 * requests.
3287 * - FFA ERROR FFA_NOT_SUPPORTED: If an invalid ffa_version is supplied to the
3288 * function. Or the fragment length is more than a single page.
3289 * - FFA_ERROR FFA_NO_MEMORY: If we do not have enough memory for a scratch
3290 * memory transaction descriptor.
3291 * - FFA_SUCCESS: If a successful update has occured.
J-Alves33c47bf2022-09-29 11:36:20 +01003292 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003293static struct ffa_value api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3294 void *allocated, uint32_t *fragment_length, uint32_t *total_length,
Karl Meakin0e617d92024-04-05 12:55:22 +01003295 enum ffa_version ffa_version, bool send_transaction)
J-Alves33c47bf2022-09-29 11:36:20 +01003296{
3297 struct ffa_memory_region_v1_0 *memory_region_v1_0;
3298 struct ffa_memory_region *memory_region_v1_1 = NULL;
3299 struct ffa_composite_memory_region *composite_v1_0;
3300 struct ffa_composite_memory_region *composite_v1_1;
3301 size_t receivers_length;
3302 size_t space_left;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003303 size_t receivers_end;
J-Alves33c47bf2022-09-29 11:36:20 +01003304 size_t composite_offset_v1_1;
3305 size_t composite_offset_v1_0;
3306 size_t fragment_constituents_size;
3307 size_t fragment_length_v1_1;
3308
J-Alves33c47bf2022-09-29 11:36:20 +01003309 assert(fragment_length != NULL);
3310 assert(total_length != NULL);
3311
Karl Meakin0e617d92024-04-05 12:55:22 +01003312 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves33c47bf2022-09-29 11:36:20 +01003313 return (struct ffa_value){.func = FFA_SUCCESS_32};
3314 }
3315
Karl Meakin0e617d92024-04-05 12:55:22 +01003316 if (ffa_version != FFA_VERSION_1_0) {
J-Alves33c47bf2022-09-29 11:36:20 +01003317 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
3318 ffa_version);
3319 return ffa_error(FFA_NOT_SUPPORTED);
3320 }
3321
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003322 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003323 "Updating memory transaction descriptor from v1.0 to v1.1.\n");
J-Alves33c47bf2022-09-29 11:36:20 +01003324
3325 memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated;
3326
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003327 receivers_length = sizeof(struct ffa_memory_access_v1_0) *
J-Alves33c47bf2022-09-29 11:36:20 +01003328 memory_region_v1_0->receiver_count;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003329 receivers_end = sizeof(struct ffa_memory_region) + receivers_length;
J-Alves33c47bf2022-09-29 11:36:20 +01003330
3331 /*
3332 * Check the specified composite offset of v1.0 descriptor, and that all
3333 * receivers were configured with the same offset.
3334 */
3335 composite_offset_v1_0 =
3336 memory_region_v1_0->receivers[0].composite_memory_region_offset;
3337
J-Alves33c47bf2022-09-29 11:36:20 +01003338 /* Determine the composite offset for v1.1 descriptor. */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003339 if (send_transaction) {
3340 fragment_constituents_size =
3341 *fragment_length - composite_offset_v1_0 -
3342 sizeof(struct ffa_composite_memory_region);
3343 fragment_length_v1_1 =
3344 receivers_end +
3345 sizeof(struct ffa_composite_memory_region) +
3346 fragment_constituents_size;
3347 composite_offset_v1_1 = receivers_end;
3348 } else {
3349 fragment_constituents_size = 0;
3350 fragment_length_v1_1 = receivers_end;
3351 composite_offset_v1_1 = 0;
3352 }
J-Alves33c47bf2022-09-29 11:36:20 +01003353
3354 /*
3355 * Currently only support the simpler cases: memory transaction
3356 * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE.
3357 * TODO: allocate the entries needed for this fragment_length_v1_1.
3358 * - Check corner when v1.1 descriptor converted size surpasses
3359 * the size of the entry.
3360 */
3361 if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) {
3362 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003363 "Translation of FF-A v1.0 descriptors for over %lu is "
J-Alves33c47bf2022-09-29 11:36:20 +01003364 "unsupported.",
3365 MM_PPOOL_ENTRY_SIZE);
3366 return ffa_error(FFA_NOT_SUPPORTED);
3367 }
3368
3369 space_left = fragment_length_v1_1;
3370
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003371 /*
3372 * Allocate a page of memory to construct the v1.1 memory descriptor.
3373 * Earlier we checked that the fragment_length_v1_1 would not be larger
3374 * than a page.
3375 */
J-Alves33c47bf2022-09-29 11:36:20 +01003376 memory_region_v1_1 =
3377 (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
3378 if (memory_region_v1_1 == NULL) {
3379 return ffa_error(FFA_NO_MEMORY);
3380 }
3381
3382 /* Translate header from v1.0 to v1.1. */
3383 api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0,
3384 memory_region_v1_1);
3385
3386 space_left -= sizeof(struct ffa_memory_region);
3387
3388 /* Copy memory access information. */
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003389 memcpy_s((uint8_t *)memory_region_v1_1 +
3390 memory_region_v1_1->receivers_offset,
3391 space_left, memory_region_v1_0->receivers, receivers_length);
J-Alves33c47bf2022-09-29 11:36:20 +01003392
3393 /* Initialize the memory access descriptors with composite offset. */
3394 for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) {
3395 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003396 ffa_memory_region_get_receiver(memory_region_v1_1, i);
3397 assert(receiver != NULL);
J-Alves33c47bf2022-09-29 11:36:20 +01003398 receiver->composite_memory_region_offset =
3399 composite_offset_v1_1;
3400 }
3401
3402 space_left -= receivers_length;
3403
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003404 /* Composite memory descriptors to copy. */
3405 if (send_transaction) {
3406 /* Init v1.1 composite. */
3407 composite_v1_1 = (struct ffa_composite_memory_region
3408 *)((uint8_t *)memory_region_v1_1 +
3409 composite_offset_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003410
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003411 composite_v1_0 = ffa_memory_region_get_composite_v1_0(
3412 memory_region_v1_0, 0);
3413 composite_v1_1->constituent_count =
3414 composite_v1_0->constituent_count;
3415 composite_v1_1->page_count = composite_v1_0->page_count;
J-Alves33c47bf2022-09-29 11:36:20 +01003416
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003417 space_left -= sizeof(struct ffa_composite_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003418
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003419 /* Initialize v1.1 constituents. */
3420 memcpy_s(composite_v1_1->constituents, space_left,
3421 composite_v1_0->constituents,
3422 fragment_constituents_size);
J-Alves33c47bf2022-09-29 11:36:20 +01003423
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003424 space_left -= fragment_constituents_size;
3425 }
3426
J-Alves33c47bf2022-09-29 11:36:20 +01003427 assert(space_left == 0U);
3428
J-Alves33c47bf2022-09-29 11:36:20 +01003429 /*
3430 * Remove the v1.0 fragment size, and resultant size of v1.1 fragment.
3431 */
3432 *total_length = *total_length - *fragment_length + fragment_length_v1_1;
3433 *fragment_length = fragment_length_v1_1;
3434
3435 /*
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003436 * After successfully updating to v1.1 copy the descriptor to the
3437 * internal buffer given as a parameter (used to prevent TOCTOU attacks)
3438 * and free the scratch memory used to construct it.
J-Alves33c47bf2022-09-29 11:36:20 +01003439 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003440 memcpy_s(allocated, MM_PPOOL_ENTRY_SIZE, memory_region_v1_1,
3441 *fragment_length);
3442 mpool_free(&api_page_pool, memory_region_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003443
3444 return (struct ffa_value){.func = FFA_SUCCESS_32};
3445}
3446
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003447struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
3448 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003449 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003450{
3451 struct vm *from = current->vm;
3452 struct vm *to;
3453 const void *from_msg;
J-Alves33c47bf2022-09-29 11:36:20 +01003454 void *allocated_entry;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003455 struct ffa_memory_region *memory_region = NULL;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003456 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01003457 bool targets_other_world = false;
Karl Meakin0e617d92024-04-05 12:55:22 +01003458 enum ffa_version ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003459
3460 if (ipa_addr(address) != 0 || page_count != 0) {
3461 /*
3462 * Hafnium only supports passing the descriptor in the TX
3463 * mailbox.
3464 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003465 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003466 }
3467
Andrew Walbranca808b12020-05-15 17:22:28 +01003468 if (fragment_length > length) {
3469 dlog_verbose(
3470 "Fragment length %d greater than total length %d.\n",
3471 fragment_length, length);
3472 return ffa_error(FFA_INVALID_PARAMETERS);
3473 }
J-Alves33c47bf2022-09-29 11:36:20 +01003474
3475 if (fragment_length > HF_MAILBOX_SIZE ||
3476 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003477 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003478 }
3479
3480 /*
3481 * Check that the sender has configured its send buffer. If the TX
3482 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3483 * be safely accessed after releasing the lock since the TX mailbox
3484 * address can only be configured once.
3485 */
3486 sl_lock(&from->lock);
3487 from_msg = from->mailbox.send;
J-Alves33c47bf2022-09-29 11:36:20 +01003488 ffa_version = from->ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003489 sl_unlock(&from->lock);
3490
3491 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003492 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003493 }
3494
3495 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003496 * Copy the memory region descriptor to a fresh page from the memory
3497 * pool. This prevents the sender from changing it underneath us, and
3498 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003499 */
J-Alves33c47bf2022-09-29 11:36:20 +01003500 allocated_entry = mpool_alloc(&api_page_pool);
3501 if (allocated_entry == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003502 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003503 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003504 }
J-Alves33c47bf2022-09-29 11:36:20 +01003505
J-Alves8f2150d2024-03-28 16:15:56 +00003506 if (!memcpy_trapped(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg,
3507 fragment_length)) {
3508 dlog_error(
3509 "%s: Failed to copy FF-A memory region descriptor.\n",
3510 __func__);
3511 return ffa_error(FFA_ABORTED);
3512 }
J-Alves33c47bf2022-09-29 11:36:20 +01003513
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003514 if (!ffa_memory_region_sanity_check(allocated_entry, ffa_version,
3515 fragment_length, true)) {
3516 ret = ffa_error(FFA_INVALID_PARAMETERS);
3517 goto out;
3518 }
3519
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003520 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3521 allocated_entry, &fragment_length, &length, ffa_version, true);
J-Alves33c47bf2022-09-29 11:36:20 +01003522 if (ret.func != FFA_SUCCESS_32) {
3523 goto out;
3524 }
3525
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003526 memory_region = allocated_entry;
3527
J-Alves33c47bf2022-09-29 11:36:20 +01003528 if (fragment_length < sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003529 memory_region->memory_access_desc_size) {
J-Alves33c47bf2022-09-29 11:36:20 +01003530 dlog_verbose(
3531 "Initial fragment length %d smaller than header size "
Karl Meakine8937d92024-03-19 16:04:25 +00003532 "%lu.\n",
J-Alves33c47bf2022-09-29 11:36:20 +01003533 fragment_length,
3534 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003535 memory_region->memory_access_desc_size);
3536 ret = ffa_error(FFA_INVALID_PARAMETERS);
3537 goto out;
J-Alves33c47bf2022-09-29 11:36:20 +01003538 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003539
J-Alves84658fc2021-06-17 14:37:32 +01003540 if (!api_memory_region_check_flags(memory_region, share_func)) {
3541 dlog_verbose(
3542 "Memory region reserved arguments must be zero.\n");
3543 ret = ffa_error(FFA_INVALID_PARAMETERS);
3544 goto out;
3545 }
3546
J-Alves363f5722022-04-25 17:37:37 +01003547 if (memory_region->receiver_count == 0U) {
3548 dlog_verbose("Receiver count can't be 0.\n");
3549 ret = ffa_error(FFA_INVALID_PARAMETERS);
3550 goto out;
3551 }
3552
J-Alves95fbb312024-03-20 15:19:16 +00003553 if ((share_func == FFA_MEM_DONATE_32 ||
3554 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01003555 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003556 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003557 "FFA_MEM_DONATE only supports one recipient. Specified "
3558 "%u\n",
J-Alves363f5722022-04-25 17:37:37 +01003559 memory_region->receiver_count);
3560 ret = ffa_error(FFA_INVALID_PARAMETERS);
3561 goto out;
3562 }
3563
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003564 /*
3565 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01003566 * If there is a receiver from the other world, track it for later
3567 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003568 */
J-Alves363f5722022-04-25 17:37:37 +01003569 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003570 struct ffa_memory_access *receiver =
3571 ffa_memory_region_get_receiver(memory_region, i);
J-Alvesc9227c82024-04-24 21:00:58 +01003572 ffa_id_t receiver_id;
3573
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003574 assert(receiver != NULL);
J-Alvesc9227c82024-04-24 21:00:58 +01003575
3576 receiver_id = receiver->receiver_permissions.receiver;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003577
J-Alves363f5722022-04-25 17:37:37 +01003578 to = vm_find(receiver_id);
3579
J-Alves7de61af2024-03-27 10:29:46 +00003580 if ((vm_id_is_current_world(receiver_id) && to == NULL) ||
3581 to == from) {
J-Alves363f5722022-04-25 17:37:37 +01003582 dlog_verbose("%s: invalid receiver.\n", __func__);
3583 ret = ffa_error(FFA_INVALID_PARAMETERS);
3584 goto out;
3585 }
3586
J-Alvesc9227c82024-04-24 21:00:58 +01003587 if (!plat_ffa_is_memory_send_valid(
3588 receiver_id, from->id, share_func,
3589 memory_region->receiver_count > 1)) {
J-Alves363f5722022-04-25 17:37:37 +01003590 ret = ffa_error(FFA_DENIED);
3591 goto out;
3592 }
3593
3594 /* Capture if any of the receivers is from the other world. */
3595 if (!targets_other_world) {
3596 targets_other_world =
3597 !vm_id_is_current_world(receiver_id);
3598 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003599 }
3600
J-Alves363f5722022-04-25 17:37:37 +01003601 if (targets_other_world) {
J-Alves66652252022-07-06 09:49:51 +01003602 ret = plat_ffa_other_world_mem_send(
3603 from, share_func, &memory_region, length,
3604 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003605 } else {
3606 struct vm_locked from_locked = vm_lock(from);
3607
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003608 ret = ffa_memory_send(from_locked, memory_region, length,
3609 fragment_length, share_func,
3610 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003611 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003612 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003613 * make sure we don't free it.
3614 */
3615 memory_region = NULL;
3616
3617 vm_unlock(&from_locked);
3618 }
3619
3620out:
3621 if (memory_region != NULL) {
3622 mpool_free(&api_page_pool, memory_region);
3623 }
3624
3625 return ret;
3626}
3627
J-Alvesc7270b72024-09-12 10:16:34 +01003628/**
3629 * An FFA_MEM_RETRIEVE_REQ from the hypervisor must specify the handle of the
3630 * memory transaction it is querying and all other fields must be 0.
3631 */
3632static bool api_ffa_memory_hypervisor_retrieve_request_validate(
3633 struct ffa_memory_region *request, enum ffa_version version)
3634{
3635 switch (version) {
3636 case FFA_VERSION_1_0: {
3637 struct ffa_memory_region_v1_0 *request_v1_0 =
3638 (struct ffa_memory_region_v1_0 *)request;
3639
3640 return request_v1_0->sender == 0U &&
3641 request_v1_0->attributes.shareability == 0U &&
3642 request_v1_0->attributes.cacheability == 0U &&
3643 request_v1_0->attributes.type == 0U &&
3644 request_v1_0->attributes.security == 0U &&
3645 request_v1_0->flags == 0U && request_v1_0->tag == 0U &&
3646 request_v1_0->receiver_count == 0U &&
3647 plat_ffa_memory_handle_allocated_by_current_world(
3648 request_v1_0->handle);
3649 }
3650 default:
3651 return request->sender == 0U &&
3652 request->attributes.shareability == 0U &&
3653 request->attributes.cacheability == 0U &&
3654 request->attributes.type == 0U &&
3655 request->attributes.security == 0U &&
3656 request->flags == 0U && request->tag == 0U &&
3657 request->memory_access_desc_size == 0U &&
3658 request->receiver_count == 0U &&
3659 request->receivers_offset == 0U &&
3660 plat_ffa_memory_handle_allocated_by_current_world(
3661 request->handle);
3662 }
3663}
3664
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003665struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3666 uint32_t fragment_length,
3667 ipaddr_t address, uint32_t page_count,
3668 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003669{
3670 struct vm *to = current->vm;
3671 struct vm_locked to_locked;
3672 const void *to_msg;
J-Alves00847062022-10-03 17:08:44 +01003673 void *retrieve_msg;
3674 struct ffa_memory_region *retrieve_request = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003675 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003676 struct ffa_value ret;
Karl Meakin0e617d92024-04-05 12:55:22 +01003677 enum ffa_version ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003678
3679 if (ipa_addr(address) != 0 || page_count != 0) {
3680 /*
3681 * Hafnium only supports passing the descriptor in the TX
3682 * mailbox.
3683 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003684 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003685 }
3686
Andrew Walbrana65a1322020-04-06 19:32:32 +01003687 if (fragment_length != length) {
J-Alves33c47bf2022-09-29 11:36:20 +01003688 dlog_verbose("Fragmentation not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003689 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003690 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003691
J-Alves00847062022-10-03 17:08:44 +01003692 retrieve_msg = cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003693 message_buffer_size = cpu_get_buffer_size(current->cpu);
3694 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3695 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003696 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003697 }
3698
3699 to_locked = vm_lock(to);
3700 to_msg = to->mailbox.send;
J-Alves00847062022-10-03 17:08:44 +01003701 ffa_version = to->ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003702
3703 if (to_msg == NULL) {
3704 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003705 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003706 goto out;
3707 }
3708
3709 /*
3710 * Copy the retrieve request descriptor to an internal buffer, so that
3711 * the caller can't change it underneath us.
3712 */
J-Alves8f2150d2024-03-28 16:15:56 +00003713 if (!memcpy_trapped(retrieve_msg, message_buffer_size, to_msg,
3714 length)) {
3715 dlog_error(
3716 "%s: Failed to copy FF-A retrieve request "
3717 "descriptor.\n",
3718 __func__);
3719 ret = ffa_error(FFA_ABORTED);
3720 goto out;
3721 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003722
J-Alvese8c8c2b2022-12-16 15:34:48 +00003723 if ((vm_is_mailbox_other_world_owned(to_locked) &&
3724 !plat_ffa_acquire_receiver_rx(to_locked, &ret)) ||
3725 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003726 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003727 * Can't retrieve memory information if the mailbox is
3728 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003729 */
J-Alves59ed0042022-07-28 18:26:41 +01003730 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003731 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003732 goto out;
3733 }
3734
Daniel Boulby296ee702023-11-28 13:36:55 +00003735 if (!is_ffa_hypervisor_retrieve_request(retrieve_msg)) {
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003736 if (!ffa_memory_region_sanity_check(retrieve_msg, ffa_version,
3737 fragment_length, false)) {
3738 ret = ffa_error(FFA_INVALID_PARAMETERS);
3739 goto out;
3740 }
3741 /*
3742 * If required, transform the retrieve request to FF-A v1.1.
3743 */
3744 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3745 retrieve_msg, &fragment_length, &length, ffa_version,
3746 false);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003747
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003748 if (ret.func != FFA_SUCCESS_32) {
3749 goto out;
3750 }
J-Alvesc7270b72024-09-12 10:16:34 +01003751 } else {
3752 if (!api_ffa_memory_hypervisor_retrieve_request_validate(
3753 retrieve_msg, ffa_version)) {
3754 dlog_verbose(
3755 "All fields except the handle in the "
3756 "memory access descriptor must be zero for a "
3757 "hypervisor retrieve request.\n");
3758 ret = ffa_error(FFA_INVALID_PARAMETERS);
3759 goto out;
3760 }
J-Alves00847062022-10-03 17:08:44 +01003761 }
3762
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003763 retrieve_request = retrieve_msg;
Kathleen Capella578784f2023-09-01 18:03:27 -04003764
J-Alvesb5084cf2022-07-06 14:20:12 +01003765 if (plat_ffa_memory_handle_allocated_by_current_world(
3766 retrieve_request->handle)) {
3767 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3768 &api_page_pool);
3769 } else {
Daniel Boulbyfdd59f72023-10-19 16:43:19 +01003770 dlog_error("Invalid FF-A memory handle.\n");
3771 ret = ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesb5084cf2022-07-06 14:20:12 +01003772 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003773out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003774 vm_unlock(&to_locked);
3775 return ret;
3776}
3777
J-Alvesa39a8442024-04-26 15:41:47 +01003778/**
3779 * Copies the memory relinquish descriptor from the partition's TX buffer, to
3780 * the buffer of the local CPU. Do it safely, and return error if:
3781 * - FFA_ABORTED: if the `memcpy_trapped` fails.
3782 * - FFA_INVALID_PARAMETERS: if the size of the full memory relinquish
3783 * descriptor doesn't fit the local CPU buffer.
3784 *
3785 * Returns FFA_SUCCESS if copying goes well, and sets 'out_relinquish'
3786 * to the address of the cpu buffer with the relinquish descriptor.
3787 */
3788static struct ffa_value api_get_ffa_mem_relinquish_descriptor(
3789 struct vcpu *current, const void *from_msg,
3790 struct ffa_mem_relinquish **out_relinquish)
3791{
3792 struct ffa_mem_relinquish *relinquish_request;
3793 uint32_t from_msg_size;
3794 uint32_t total_from_msg_size;
3795 uint32_t dst_size;
3796 vaddr_t dst;
3797 vaddr_t src;
3798
3799 assert(from_msg != NULL);
3800 assert(out_relinquish != NULL);
3801
3802 /*
3803 * Copy the relinquish descriptor to an internal buffer, so that the
3804 * caller can't change it underneath us.
3805 */
3806 relinquish_request =
3807 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
3808
3809 /* Set the destination for the copy. */
3810 dst = va_from_ptr(relinquish_request);
3811 src = va_from_ptr(from_msg);
3812
3813 dst_size = cpu_get_buffer_size(current->cpu);
3814
3815 /* Only copy the size to start with. */
3816 from_msg_size = sizeof(struct ffa_mem_relinquish);
3817 total_from_msg_size = from_msg_size;
3818
3819 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3820 from_msg_size)) {
3821 dlog_error(
3822 "%s: Failed to copy FF-A memory relinquish "
3823 "descriptor.\n",
3824 __func__);
3825 return ffa_error(FFA_ABORTED);
3826 }
3827
3828 if (relinquish_request->endpoint_count != 1) {
3829 dlog_error("%s: relinquish descriptor must have 1 endpoint\n",
3830 __func__);
3831 return ffa_error(FFA_INVALID_PARAMETERS);
3832 }
3833
3834 /*
3835 * Increment the `dst` to the position right after the copied header.
3836 * Increment the `src` to point at the list of endpoints.
3837 *
3838 * Calculate the new `dst_size` which is the size of the allocated cpu
3839 * buffer, minus the size of the copied memory relinquish header.
3840 *
3841 * Only after the above: determine the new `from_msg_size` in accordance
3842 * to the endpoint count.
3843 */
3844 dst = va_add(dst, from_msg_size);
3845 src = va_add(src, from_msg_size);
3846
3847 /*
3848 * Check if it is safe to copy the rest of the message.
3849 * This also serves as a santiy check to 'endpoint_count'.
3850 * The size of what is left in the descriptor, based on endpoint_count,
3851 * shall not be bigger than the size of the mailbox minus the size of
3852 * the header which was previously copied in this function.
3853 */
3854 dst_size -= from_msg_size;
3855 from_msg_size = relinquish_request->endpoint_count * sizeof(ffa_id_t);
3856 total_from_msg_size += from_msg_size;
3857
3858 if (total_from_msg_size > HF_MAILBOX_SIZE ||
3859 total_from_msg_size > dst_size) {
3860 dlog_verbose(
3861 "Relinquish message too long. Endpoint count: %u\n",
3862 relinquish_request->endpoint_count);
3863 return ffa_error(FFA_INVALID_PARAMETERS);
3864 }
3865
3866 /* Copy the remaining fragment. */
3867 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3868 from_msg_size)) {
3869 dlog_error("%s: Failed to copy FF-A relinquish request.\n",
3870 __func__);
3871 return ffa_error(FFA_ABORTED);
3872 }
3873
3874 /*
3875 * Set the output address for the relinquish descriptor to the current
3876 * cpu's buffer.
3877 */
3878 *out_relinquish = relinquish_request;
3879
3880 return (struct ffa_value){.func = FFA_SUCCESS_32};
3881}
3882
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003883struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003884{
3885 struct vm *from = current->vm;
3886 struct vm_locked from_locked;
3887 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003888 struct ffa_value ret;
J-Alvesa39a8442024-04-26 15:41:47 +01003889 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003890
3891 from_locked = vm_lock(from);
3892 from_msg = from->mailbox.send;
3893
3894 if (from_msg == NULL) {
3895 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003896 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003897 goto out;
3898 }
3899
J-Alvesa39a8442024-04-26 15:41:47 +01003900 ret = api_get_ffa_mem_relinquish_descriptor(current, from_msg,
3901 &relinquish_request);
3902
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003903 /*
J-Alvesa39a8442024-04-26 15:41:47 +01003904 * If the descriptor was safely copied, continue with the handling of
3905 * the retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003906 */
J-Alvesa39a8442024-04-26 15:41:47 +01003907 if (ret.func == FFA_SUCCESS_32) {
3908 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3909 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003910 }
J-Alves8f2150d2024-03-28 16:15:56 +00003911
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003912out:
3913 vm_unlock(&from_locked);
3914 return ret;
3915}
3916
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003917struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3918 ffa_memory_region_flags_t flags,
3919 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003920{
3921 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003922 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003923
Olivier Deprez55a189e2021-06-09 15:45:27 +02003924 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003925 struct vm_locked to_locked = vm_lock(to);
3926
Andrew Walbranca808b12020-05-15 17:22:28 +01003927 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003928 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003929
Andrew Walbran290b0c92020-02-03 16:37:14 +00003930 vm_unlock(&to_locked);
3931 } else {
J-Alvesfc19b372022-07-06 12:17:35 +01003932 ret = plat_ffa_other_world_mem_reclaim(to, handle, flags,
3933 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003934 }
3935
3936 return ret;
3937}
3938
3939struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
3940 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003941 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003942 struct vcpu *current)
3943{
3944 struct vm *to = current->vm;
3945 struct vm_locked to_locked;
3946 struct ffa_value ret;
3947
3948 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01003949 if (vm_id_is_current_world(to->id)) {
3950 if (sender_vm_id != 0) {
3951 dlog_verbose("%s: Invalid sender.\n", __func__);
3952 return ffa_error(FFA_INVALID_PARAMETERS);
3953 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003954 }
3955
3956 to_locked = vm_lock(to);
3957
J-Alves122f1a12022-12-12 15:55:42 +00003958 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003959 /*
3960 * Can't retrieve memory information if the mailbox is not
3961 * available.
3962 */
J-Alves59ed0042022-07-28 18:26:41 +01003963 dlog_verbose("%s: RX buffer not ready partition %x.\n",
3964 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003965 ret = ffa_error(FFA_BUSY);
3966 goto out;
3967 }
3968
3969 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01003970 sender_vm_id, &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003971out:
3972 vm_unlock(&to_locked);
3973 return ret;
3974}
3975
3976struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
3977 uint32_t fragment_length,
J-Alves19e20cf2023-08-02 12:48:55 +01003978 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003979 struct vcpu *current)
3980{
3981 struct vm *from = current->vm;
3982 const void *from_msg;
3983 void *fragment_copy;
3984 struct ffa_value ret;
3985
3986 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01003987 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
3988 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01003989 return ffa_error(FFA_INVALID_PARAMETERS);
3990 }
3991
3992 /*
3993 * Check that the sender has configured its send buffer. If the TX
3994 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3995 * be safely accessed after releasing the lock since the TX mailbox
3996 * address can only be configured once.
3997 */
3998 sl_lock(&from->lock);
3999 from_msg = from->mailbox.send;
4000 sl_unlock(&from->lock);
4001
4002 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01004003 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01004004 return ffa_error(FFA_INVALID_PARAMETERS);
4005 }
4006
4007 /*
4008 * Copy the fragment to a fresh page from the memory pool. This prevents
4009 * the sender from changing it underneath us, and also lets us keep it
4010 * around in the share state table if needed.
4011 */
4012 if (fragment_length > HF_MAILBOX_SIZE ||
4013 fragment_length > MM_PPOOL_ENTRY_SIZE) {
4014 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004015 "Fragment length %d larger than mailbox size %zu.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01004016 fragment_length, HF_MAILBOX_SIZE);
4017 return ffa_error(FFA_INVALID_PARAMETERS);
4018 }
4019 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
4020 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
4021 0) {
4022 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
4023 return ffa_error(FFA_INVALID_PARAMETERS);
4024 }
4025 fragment_copy = mpool_alloc(&api_page_pool);
4026 if (fragment_copy == NULL) {
4027 dlog_verbose("Failed to allocate fragment copy.\n");
4028 return ffa_error(FFA_NO_MEMORY);
4029 }
J-Alves8f2150d2024-03-28 16:15:56 +00004030
4031 if (!memcpy_trapped(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg,
4032 fragment_length)) {
4033 dlog_error("%s: Failed to copy fragment.\n", __func__);
4034 return ffa_error(FFA_ABORTED);
4035 }
Andrew Walbranca808b12020-05-15 17:22:28 +01004036
4037 /*
4038 * Hafnium doesn't support fragmentation of memory retrieve requests
4039 * (because it doesn't support caller-specified mappings, so a request
4040 * will never be larger than a single page), so this must be part of a
4041 * memory send (i.e. donate, lend or share) request.
4042 *
4043 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01004044 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01004045 */
J-Alvesfdd29272022-07-19 13:16:31 +01004046 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004047 struct vm_locked from_locked = vm_lock(from);
4048
4049 ret = ffa_memory_send_continue(from_locked, fragment_copy,
4050 fragment_length, handle,
4051 &api_page_pool);
4052 /*
4053 * `ffa_memory_send_continue` takes ownership of the
4054 * fragment_copy, so we don't need to free it here.
4055 */
4056 vm_unlock(&from_locked);
4057 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01004058 ret = plat_ffa_other_world_mem_send_continue(
4059 from, fragment_copy, fragment_length, handle,
4060 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00004061 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00004062
4063 return ret;
4064}
Max Shvetsov40108e72020-08-27 12:39:50 +01004065
Olivier Deprezd614d322021-06-18 15:21:00 +02004066/**
4067 * Register an entry point for a vCPU in warm boot cases.
4068 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
4069 */
Max Shvetsov40108e72020-08-27 12:39:50 +01004070struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
4071 struct vcpu *current)
4072{
4073 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01004074 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01004075
Olivier Deprezd614d322021-06-18 15:21:00 +02004076 /*
4077 * Reject if interface is not supported at this FF-A instance
4078 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
4079 */
4080 if (!plat_ffa_is_secondary_ep_register_supported() ||
Karl Meakin82041822024-05-20 11:14:34 +01004081 vm_is_up(current->vm)) {
Olivier Deprezd614d322021-06-18 15:21:00 +02004082 return ffa_error(FFA_NOT_SUPPORTED);
4083 }
4084
4085 /*
4086 * No further check is made on the address validity
4087 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
4088 * from the VM or vCPU structure.
4089 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
4090 * For each SP [...] the Framework assumes that the same entry point
4091 * address is used for initializing any execution context during a
4092 * secondary cold boot.
4093 * If this function is invoked multiple times, then the entry point
4094 * address specified in the last valid invocation must be used by the
4095 * callee.
4096 */
Olivier Deprezb2808332023-02-02 15:25:40 +01004097 current_locked = vcpu_lock(current);
4098 if (current->rt_model != RTM_SP_INIT) {
4099 dlog_error(
4100 "FFA_SECONDARY_EP_REGISTER can only be called while "
4101 "vCPU in run-time state for initialization.\n");
4102 vcpu_unlock(&current_locked);
4103 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02004104 }
Olivier Deprezb2808332023-02-02 15:25:40 +01004105 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02004106
Olivier Deprezb2808332023-02-02 15:25:40 +01004107 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01004108 vm_locked.vm->secondary_ep = entry_point;
4109 vm_unlock(&vm_locked);
4110
Olivier Deprezb2808332023-02-02 15:25:40 +01004111 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01004112}
J-Alvesa0f317d2021-06-09 13:31:59 +01004113
J-Alves19e20cf2023-08-02 12:48:55 +01004114struct ffa_value api_ffa_notification_bitmap_create(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004115 ffa_vcpu_count_t vcpu_count,
4116 struct vcpu *current)
4117{
J-Alves7ccaccf2024-02-01 14:25:23 +00004118 const struct ffa_value ret =
4119 plat_ffa_is_notifications_bitmap_access_valid(current, vm_id);
4120
4121 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4122 dlog_verbose(
4123 "FFA_NOTIFICATION_BITMAP_CREATE to be used by "
4124 "hypervisor for valid NWd VM IDs only (%x).\n",
4125 vm_id);
4126 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004127 }
4128
4129 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
4130}
4131
J-Alves19e20cf2023-08-02 12:48:55 +01004132struct ffa_value api_ffa_notification_bitmap_destroy(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004133 struct vcpu *current)
4134{
J-Alves7ccaccf2024-02-01 14:25:23 +00004135 const struct ffa_value ret =
4136 plat_ffa_is_notifications_bitmap_access_valid(current, vm_id);
4137
4138 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4139 dlog_verbose(
4140 "FFA_NOTIFICATION_BITMAP_DESTROY to be used by "
4141 "hypervisor for valid NWd VM IDs only (%x).\n",
4142 vm_id);
4143 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004144 }
4145
4146 return plat_ffa_notifications_bitmap_destroy(vm_id);
4147}
J-Alvesc003a7a2021-03-18 13:06:53 +00004148
4149struct ffa_value api_ffa_notification_update_bindings(
J-Alves19e20cf2023-08-02 12:48:55 +01004150 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesc003a7a2021-03-18 13:06:53 +00004151 ffa_notifications_bitmap_t notifications, bool is_bind,
4152 struct vcpu *current)
4153{
4154 struct ffa_value ret = {.func = FFA_SUCCESS_32};
4155 struct vm_locked receiver_locked;
4156 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
J-Alves19e20cf2023-08-02 12:48:55 +01004157 const ffa_id_t id_to_update = is_bind ? sender_vm_id : HF_INVALID_VM_ID;
4158 const ffa_id_t id_to_validate =
J-Alvesc003a7a2021-03-18 13:06:53 +00004159 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01004160 const uint32_t flags_mbz =
4161 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
4162
4163 if ((flags_mbz & flags) != 0U) {
4164 return ffa_error(FFA_INVALID_PARAMETERS);
4165 }
J-Alvesc003a7a2021-03-18 13:06:53 +00004166
4167 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
4168 receiver_vm_id)) {
4169 dlog_verbose("Invalid use of notifications bind interface.\n");
4170 return ffa_error(FFA_INVALID_PARAMETERS);
4171 }
4172
J-Alvesb15e9402021-09-08 11:44:42 +01004173 if (plat_ffa_notifications_update_bindings_forward(
4174 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
4175 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01004176 return ret;
4177 }
4178
J-Alvesc003a7a2021-03-18 13:06:53 +00004179 if (notifications == 0U) {
Karl Meakine8937d92024-03-19 16:04:25 +00004180 dlog_verbose("No notifications have been specified %lx.\n",
J-Alves1512acc2024-02-01 16:57:10 +00004181 notifications);
J-Alvesc003a7a2021-03-18 13:06:53 +00004182 return ffa_error(FFA_INVALID_PARAMETERS);
4183 }
4184
4185 /**
4186 * This check assumes receiver is the current VM, and has been enforced
4187 * by 'plat_ffa_is_notifications_bind_valid'.
4188 */
4189 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4190
4191 if (receiver_locked.vm == NULL) {
4192 dlog_verbose("Receiver doesn't exist!\n");
J-Alves7333dcf2024-02-01 17:25:10 +00004193 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesc003a7a2021-03-18 13:06:53 +00004194 }
4195
J-Alves3e55c4d2024-10-09 14:24:07 +01004196 if (receiver_locked.vm->ffa_version < FFA_VERSION_1_1) {
4197 dlog_verbose(
4198 "%s: caller (%x) version should be GE to FF-A v1.1.\n",
4199 __func__, receiver_locked.vm->id);
4200 ret = ffa_error(FFA_NOT_SUPPORTED);
4201 goto out;
4202 }
4203
J-Alves09ff9d82021-11-02 11:55:20 +00004204 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004205 dlog_verbose("Notifications are not enabled.\n");
4206 ret = ffa_error(FFA_NOT_SUPPORTED);
4207 goto out;
4208 }
4209
4210 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
4211 vm_find(sender_vm_id) == NULL) {
4212 dlog_verbose("Sender VM does not exist!\n");
4213 ret = ffa_error(FFA_INVALID_PARAMETERS);
4214 goto out;
4215 }
4216
4217 /*
4218 * Can't bind/unbind notifications if at least one is bound to a
4219 * different sender.
4220 */
4221 if (!vm_notifications_validate_bound_sender(
J-Alves661e1b72023-08-02 13:39:40 +01004222 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_validate,
4223 notifications)) {
J-Alvesf0208142024-02-01 17:28:58 +00004224 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004225 "Sender %x not permitted to set notifications %lx to "
J-Alvesf0208142024-02-01 17:28:58 +00004226 "%x.\n",
4227 sender_vm_id, notifications, receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004228 ret = ffa_error(FFA_DENIED);
4229 goto out;
4230 }
4231
4232 /**
4233 * Check if there is a pending notification within those specified in
4234 * the bitmap.
4235 */
4236 if (vm_are_notifications_pending(receiver_locked,
J-Alves661e1b72023-08-02 13:39:40 +01004237 ffa_is_vm_id(sender_vm_id),
J-Alvesc003a7a2021-03-18 13:06:53 +00004238 notifications)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004239 dlog_verbose("Notifications within '%lx' pending.\n",
J-Alvesc003a7a2021-03-18 13:06:53 +00004240 notifications);
4241 ret = ffa_error(FFA_DENIED);
4242 goto out;
4243 }
4244
4245 vm_notifications_update_bindings(
J-Alves661e1b72023-08-02 13:39:40 +01004246 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_update,
J-Alvesc003a7a2021-03-18 13:06:53 +00004247 notifications, is_per_vcpu && is_bind);
4248
4249out:
4250 vm_unlock(&receiver_locked);
4251 return ret;
4252}
J-Alvesaa79c012021-07-09 14:29:45 +01004253
4254struct ffa_value api_ffa_notification_set(
J-Alves19e20cf2023-08-02 12:48:55 +01004255 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesaa79c012021-07-09 14:29:45 +01004256 ffa_notifications_bitmap_t notifications, struct vcpu *current)
4257{
4258 struct ffa_value ret;
4259 struct vm_locked receiver_locked;
J-Alvesaa79c012021-07-09 14:29:45 +01004260 /*
4261 * Check if is per-vCPU or global, and extracting vCPU ID according
4262 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
4263 */
4264 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
4265 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
J-Alves6b754a12024-02-14 14:26:30 +00004266 const uint32_t flags_mbz =
4267 ~(FFA_NOTIFICATIONS_FLAG_PER_VCPU |
4268 FFA_NOTIFICATIONS_FLAG_DELAY_SRI | (0xFFFFU << 16));
J-Alves41c5da32024-06-19 10:05:05 +01004269 const bool delay_sri = (FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) != 0U;
J-Alves6b754a12024-02-14 14:26:30 +00004270
4271 if ((flags_mbz & flags) != 0U) {
4272 dlog_verbose("%s: caller shouldn't set bits that MBZ.\n",
4273 __func__);
4274 return ffa_error(FFA_INVALID_PARAMETERS);
4275 }
J-Alvesaa79c012021-07-09 14:29:45 +01004276
J-Alves30ac91d2024-02-14 15:33:12 +00004277 /* Global notifications must target any vCPU. */
4278 if (!is_per_vcpu && vcpu_id != 0U) {
4279 dlog_verbose(
4280 "For global notifications vCPU ID MBZ in call to set "
4281 "notifications.\n");
4282 return ffa_error(FFA_INVALID_PARAMETERS);
4283 }
4284
J-Alvesaa79c012021-07-09 14:29:45 +01004285 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
4286 receiver_vm_id)) {
4287 dlog_verbose("Invalid use of notifications set interface.\n");
4288 return ffa_error(FFA_INVALID_PARAMETERS);
4289 }
4290
4291 if (notifications == 0U) {
4292 dlog_verbose("No notifications have been specified.\n");
4293 return ffa_error(FFA_INVALID_PARAMETERS);
4294 }
4295
J-Alves41c5da32024-06-19 10:05:05 +01004296 /*
4297 * The 'Delay Schedule Receiver interrupt flag' only applies to the
4298 * secure virtual FF-A instance.
4299 */
4300 if (!vm_id_is_current_world(sender_vm_id) && delay_sri) {
4301 dlog_verbose(
4302 "The delay SRI flag can only be set at the secure "
4303 "virtual FF-A instance.\n");
4304 return ffa_error(FFA_INVALID_PARAMETERS);
4305 }
4306
J-Alvesde7bd2f2021-09-09 19:54:35 +01004307 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
4308 flags, notifications, &ret)) {
4309 return ret;
4310 }
4311
J-Alvesaa79c012021-07-09 14:29:45 +01004312 /*
4313 * This check assumes receiver is the current VM, and has been enforced
4314 * by 'plat_ffa_is_notification_set_valid'.
4315 */
4316 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4317
4318 if (receiver_locked.vm == NULL) {
4319 dlog_verbose("Receiver ID is not valid.\n");
4320 return ffa_error(FFA_INVALID_PARAMETERS);
4321 }
4322
J-Alves09ff9d82021-11-02 11:55:20 +00004323 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004324 dlog_verbose("Receiver's notifications not enabled.\n");
4325 ret = ffa_error(FFA_DENIED);
4326 goto out;
4327 }
4328
4329 /*
J-Alves89c22132024-02-14 15:44:41 +00004330 * Check the if the notifications are bound as global if per-vCPU flag
4331 * is set, or if they are bound as per-vCPU and caller setting as
4332 * global. In either case, return FFA_INVALID_PARAMETERS.
4333 */
4334 if (vm_notifications_validate_binding(
4335 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4336 notifications, !is_per_vcpu)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004337 dlog_verbose("Notifications in %lx are %s\n", notifications,
J-Alves89c22132024-02-14 15:44:41 +00004338 !is_per_vcpu ? "global" : "per-vCPU");
4339 ret = ffa_error(FFA_INVALID_PARAMETERS);
4340 goto out;
4341 }
4342
4343 /*
J-Alvesaa79c012021-07-09 14:29:45 +01004344 * If notifications are not bound to the sender, they wouldn't be
4345 * enabled either for the receiver.
4346 */
4347 if (!vm_notifications_validate_binding(
J-Alves661e1b72023-08-02 13:39:40 +01004348 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4349 notifications, is_per_vcpu)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004350 dlog_verbose("Notifications bindings not valid.\n");
4351 ret = ffa_error(FFA_DENIED);
4352 goto out;
4353 }
4354
4355 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
4356 dlog_verbose("Invalid VCPU ID!\n");
4357 ret = ffa_error(FFA_INVALID_PARAMETERS);
4358 goto out;
4359 }
4360
J-Alves7461ef22021-10-18 17:21:33 +01004361 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00004362 vm_notifications_partition_set_pending(
J-Alves661e1b72023-08-02 13:39:40 +01004363 receiver_locked, ffa_is_vm_id(sender_vm_id), notifications,
J-Alves5a16c962022-03-25 12:32:51 +00004364 vcpu_id, is_per_vcpu);
4365
Karl Meakine8937d92024-03-19 16:04:25 +00004366 dlog_verbose("Set the notifications: %lx.\n", notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004367
J-Alves41c5da32024-06-19 10:05:05 +01004368 if (!delay_sri) {
J-Alves13394022021-06-30 13:48:49 +01004369 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
4370 vcpu_index(current));
4371 plat_ffa_sri_trigger_not_delayed(current->cpu);
4372 } else {
J-Alvesea8ccfe2024-10-09 11:47:24 +01004373 plat_ffa_sri_set_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004374 }
J-Alvesaa79c012021-07-09 14:29:45 +01004375
J-Alves13394022021-06-30 13:48:49 +01004376 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01004377out:
4378 vm_unlock(&receiver_locked);
4379
4380 return ret;
4381}
4382
4383static struct ffa_value api_ffa_notification_get_success_return(
4384 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
4385 ffa_notifications_bitmap_t from_framework)
4386{
4387 return (struct ffa_value){
4388 .func = FFA_SUCCESS_32,
4389 .arg1 = 0U,
4390 .arg2 = (uint32_t)from_sp,
4391 .arg3 = (uint32_t)(from_sp >> 32),
4392 .arg4 = (uint32_t)from_vm,
4393 .arg5 = (uint32_t)(from_vm >> 32),
4394 .arg6 = (uint32_t)from_framework,
4395 .arg7 = (uint32_t)(from_framework >> 32),
4396 };
4397}
4398
J-Alves19e20cf2023-08-02 12:48:55 +01004399struct ffa_value api_ffa_notification_get(ffa_id_t receiver_vm_id,
J-Alvesaa79c012021-07-09 14:29:45 +01004400 ffa_vcpu_index_t vcpu_id,
4401 uint32_t flags, struct vcpu *current)
4402{
J-Alves663682a2022-03-25 13:56:51 +00004403 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01004404 ffa_notifications_bitmap_t sp_notifications = 0;
4405 ffa_notifications_bitmap_t vm_notifications = 0;
4406 struct vm_locked receiver_locked;
4407 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01004408 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
4409 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
4410 FFA_NOTIFICATION_FLAG_BITMAP_SP |
4411 FFA_NOTIFICATION_FLAG_BITMAP_VM);
4412
4413 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
4414 if ((flags & flags_mbz) != 0U) {
4415 dlog_verbose(
4416 "Invalid flags bit(s) set in notifications get. [31:4] "
4417 "MBZ(%x)\n",
4418 flags);
4419 return ffa_error(FFA_INVALID_PARAMETERS);
4420 }
J-Alvesaa79c012021-07-09 14:29:45 +01004421
4422 /*
J-Alvesfc95a302022-04-22 14:18:23 +01004423 * Following check should capture wrong uses of the interface,
4424 * depending on whether Hafnium is SPMC or hypervisor. On the
4425 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01004426 */
J-Alvesfc95a302022-04-22 14:18:23 +01004427 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
4428 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004429 dlog_verbose("Invalid use of notifications get interface.\n");
4430 return ffa_error(FFA_INVALID_PARAMETERS);
4431 }
4432
4433 /*
4434 * This check assumes receiver is the current VM, and has been enforced
4435 * by `plat_ffa_is_notifications_get_valid`.
4436 */
4437 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4438
4439 /*
4440 * `plat_ffa_is_notifications_get_valid` ensures following is never
4441 * true.
4442 */
4443 CHECK(receiver_locked.vm != NULL);
4444
J-Alves60458812024-03-22 11:57:10 +00004445 if (receiver_locked.vm->vcpu_count <= vcpu_id) {
J-Alves1abb3342022-01-05 11:59:10 +00004446 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004447 "Invalid VCPU ID %u. vcpu count %u current core: "
4448 "%zu!\n",
J-Alves1abb3342022-01-05 11:59:10 +00004449 vcpu_id, receiver_locked.vm->vcpu_count,
4450 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01004451 ret = ffa_error(FFA_INVALID_PARAMETERS);
4452 goto out;
4453 }
4454
4455 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01004456 if (!plat_ffa_notifications_get_from_sp(
4457 receiver_locked, vcpu_id, &sp_notifications,
4458 &ret)) {
4459 dlog_verbose("Failed to get notifications from sps.");
4460 goto out;
4461 }
J-Alvesaa79c012021-07-09 14:29:45 +01004462 }
4463
4464 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00004465 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01004466 receiver_locked, true, vcpu_id);
4467 }
4468
J-Alvesd605a092022-03-28 14:20:48 +01004469 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
4470 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
4471 if (!plat_ffa_notifications_get_framework_notifications(
4472 receiver_locked, &framework_notifications, flags,
4473 vcpu_id, &ret)) {
4474 dlog_verbose(
4475 "Failed to get notifications from "
4476 "framework.\n");
4477 goto out;
4478 }
4479 }
4480
J-Alves663682a2022-03-25 13:56:51 +00004481 ret = api_ffa_notification_get_success_return(
4482 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004483
J-Alves6e2abc62021-12-02 14:58:56 +00004484 if (!receiver_locked.vm->el0_partition &&
4485 !vm_are_global_notifications_pending(receiver_locked)) {
4486 vm_notifications_set_npi_injected(receiver_locked, false);
4487 }
4488
J-Alvesaa79c012021-07-09 14:29:45 +01004489out:
4490 vm_unlock(&receiver_locked);
4491
4492 return ret;
4493}
J-Alvesc8e8a222021-06-08 17:33:52 +01004494
4495/**
4496 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
4497 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
4498 */
4499static struct ffa_value api_ffa_notification_info_get_success_return(
4500 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01004501 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01004502{
4503 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
4504
4505 /*
4506 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
4507 * hold the list of ids.
4508 */
4509 memcpy_s(&ret.arg3,
4510 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
4511 sizeof(ids[0]) * ids_count);
4512
4513 /*
4514 * According to the spec x2 should have:
4515 * - Bit flagging if there are more notifications pending;
4516 * - The total number of elements (i.e. total list size);
4517 * - The number of VCPU IDs within each VM specific list.
4518 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004519 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
4520 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
4521 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01004522
4523 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
4524 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
4525
4526 for (unsigned int i = 0; i < lists_count; i++) {
4527 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
4528 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
4529 }
4530
4531 return ret;
4532}
4533
4534struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
4535{
4536 /*
4537 * Following set of variables should be populated with the return info.
4538 * At a successfull handling of this interface, they should be used
4539 * to populate the 'ret' structure in accordance to the table 17.29
4540 * of the FF-A v1.1 Beta0 specification.
4541 */
4542 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
4543 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
4544 uint32_t lists_count = 0;
4545 uint32_t ids_count = 0;
4546 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01004547 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004548
4549 /*
4550 * This interface can only be called at NS virtual/physical FF-A
4551 * instance by the endpoint implementing the primary scheduler and the
4552 * Hypervisor/OS kernel.
4553 * In the SPM, following check passes if call has been forwarded from
4554 * the hypervisor.
4555 */
Karl Meakin5e996992024-05-20 11:27:07 +01004556
4557 if (!vm_is_primary(current->vm)) {
J-Alvesc8e8a222021-06-08 17:33:52 +01004558 dlog_verbose(
4559 "Only the receiver's scheduler can use this "
4560 "interface\n");
4561 return ffa_error(FFA_NOT_SUPPORTED);
4562 }
4563
J-Alvesca058c22021-09-10 14:02:07 +01004564 /*
4565 * Forward call to the other world, and fill the arrays used to assemble
4566 * return.
4567 */
4568 plat_ffa_notification_info_get_forward(
4569 ids, &ids_count, lists_sizes, &lists_count,
4570 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4571
4572 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
4573
J-Alvesc8e8a222021-06-08 17:33:52 +01004574 /* Get notifications' info from this world */
4575 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
4576 ++index) {
4577 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
4578
4579 list_is_full = vm_notifications_info_get(
4580 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
4581 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4582
4583 vm_unlock(&vm_locked);
4584 }
4585
4586 if (!list_is_full) {
4587 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004588 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01004589 ids, &ids_count, lists_sizes, &lists_count,
4590 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4591 }
4592
4593 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01004594 dlog_verbose(
4595 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01004596 result = ffa_error(FFA_NO_DATA);
4597 } else {
4598 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01004599 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01004600 }
4601
J-Alves13394022021-06-30 13:48:49 +01004602 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004603}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004604
4605struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
4606{
4607 struct vm_locked vm_locked;
4608 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
4609 bool mode_ret = false;
4610 uint32_t mode = 0;
4611
4612 if (!plat_ffa_is_mem_perm_get_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004613 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004614 }
4615
4616 if (!(current->vm->el0_partition)) {
4617 return ffa_error(FFA_DENIED);
4618 }
4619
4620 vm_locked = vm_lock(current->vm);
4621
4622 /*
4623 * mm_get_mode is used to check if the given base_addr page is already
4624 * mapped. If the page is unmapped, return error. If the page is mapped
4625 * appropriate attributes are returned to the caller. Note that
4626 * mm_get_mode returns true if the address is in the valid VA range as
4627 * supported by the architecture and MMU configurations, as opposed to
4628 * whether a page is mapped or not. For a page to be known as mapped,
4629 * the API must return true AND the returned mode must not have
4630 * MM_MODE_INVALID set.
4631 */
4632 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4633 va_add(base_addr, PAGE_SIZE), &mode);
4634 if (!mode_ret || (mode & MM_MODE_INVALID)) {
4635 ret = ffa_error(FFA_INVALID_PARAMETERS);
4636 goto out;
4637 }
4638
4639 /* No memory should be marked RWX */
4640 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
4641 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
4642
4643 /*
4644 * S-EL0 partitions are expected to have all their pages marked as
4645 * non-global.
4646 */
4647 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
4648 (MM_MODE_NG | MM_MODE_USER));
4649
4650 if (mode & MM_MODE_W) {
4651 /* No memory should be writeable but not readable. */
4652 CHECK(mode & MM_MODE_R);
4653 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4654 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
4655 } else if (mode & MM_MODE_R) {
4656 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4657 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
4658 if (!(mode & MM_MODE_X)) {
4659 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
4660 }
4661 }
4662out:
4663 vm_unlock(&vm_locked);
4664 return ret;
4665}
4666
4667struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
4668 uint32_t mem_perm, struct vcpu *current)
4669{
4670 struct vm_locked vm_locked;
4671 struct ffa_value ret;
4672 bool mode_ret = false;
4673 uint32_t original_mode;
4674 uint32_t new_mode;
4675 struct mpool local_page_pool;
4676
4677 if (!plat_ffa_is_mem_perm_set_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004678 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004679 }
4680
4681 if (!(current->vm->el0_partition)) {
4682 return ffa_error(FFA_DENIED);
4683 }
4684
4685 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
4686 return ffa_error(FFA_INVALID_PARAMETERS);
4687 }
4688
4689 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
4690 (mem_perm != FFA_MEM_PERM_RX)) {
4691 return ffa_error(FFA_INVALID_PARAMETERS);
4692 }
4693
4694 /*
4695 * Create a local pool so any freed memory can't be used by another
4696 * thread. This is to ensure the original mapping can be restored if any
4697 * stage of the process fails.
4698 */
4699 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
4700
4701 vm_locked = vm_lock(current->vm);
4702
4703 /*
4704 * All regions accessible by the partition are mapped during boot. If we
4705 * cannot get a successful translation for the page range, the request
4706 * to change permissions is rejected.
4707 * mm_get_mode is used to check if the given address range is already
4708 * mapped. If the range is unmapped, return error. If the range is
4709 * mapped appropriate attributes are returned to the caller. Note that
4710 * mm_get_mode returns true if the address is in the valid VA range as
4711 * supported by the architecture and MMU configurations, as opposed to
4712 * whether a page is mapped or not. For a page to be known as mapped,
4713 * the API must return true AND the returned mode must not have
4714 * MM_MODE_INVALID set.
4715 */
4716
4717 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4718 va_add(base_addr, page_count * PAGE_SIZE),
4719 &original_mode);
4720 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
4721 ret = ffa_error(FFA_INVALID_PARAMETERS);
4722 goto out;
4723 }
4724
4725 /* Device memory cannot be marked as executable */
4726 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
4727 ret = ffa_error(FFA_INVALID_PARAMETERS);
4728 goto out;
4729 }
4730
4731 new_mode = MM_MODE_USER | MM_MODE_NG;
4732
4733 if (mem_perm == FFA_MEM_PERM_RW) {
4734 new_mode |= MM_MODE_R | MM_MODE_W;
4735 } else if (mem_perm == FFA_MEM_PERM_RX) {
4736 new_mode |= MM_MODE_R | MM_MODE_X;
4737 } else if (mem_perm == FFA_MEM_PERM_RO) {
4738 new_mode |= MM_MODE_R;
4739 }
4740
4741 /*
4742 * Safe to re-map memory, since we know the requested permissions are
4743 * valid, and the memory requested to be re-mapped is also valid.
4744 */
4745 if (!mm_identity_prepare(
4746 &vm_locked.vm->ptable, pa_from_va(base_addr),
4747 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4748 new_mode, &local_page_pool)) {
4749 /*
4750 * Defrag the table into the local page pool.
4751 * mm_identity_prepare could have allocated or freed pages to
4752 * split blocks or tables etc.
4753 */
4754 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
4755
4756 /*
4757 * Guaranteed to succeed mapping with old mode since the mapping
4758 * with old mode already existed and we have a local page pool
4759 * that should have sufficient memory to go back to the original
4760 * state.
4761 */
4762 CHECK(mm_identity_prepare(
4763 &vm_locked.vm->ptable, pa_from_va(base_addr),
4764 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4765 original_mode, &local_page_pool));
4766 mm_identity_commit(
4767 &vm_locked.vm->ptable, pa_from_va(base_addr),
4768 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4769 original_mode, &local_page_pool);
4770
4771 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
4772 ret = ffa_error(FFA_NO_MEMORY);
4773 goto out;
4774 }
4775
4776 mm_identity_commit(
4777 &vm_locked.vm->ptable, pa_from_va(base_addr),
4778 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
4779 &local_page_pool);
4780
4781 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
4782
4783out:
4784 mpool_fini(&local_page_pool);
4785 vm_unlock(&vm_locked);
4786
4787 return ret;
4788}
Maksims Svecovs71b76702022-05-20 15:32:58 +01004789
4790/**
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004791 * Send the contents of the given vCPU's log buffer to the log, preceded
Karl Meakin705b56e2024-05-22 10:04:32 +01004792 * by the VM ID and followed by a newline.
4793 */
Karl Meakin6f1f1212024-07-16 10:18:16 +01004794void api_flush_log_buffer(struct vcpu_locked *vcpu_locked)
Karl Meakin705b56e2024-05-22 10:04:32 +01004795{
Karl Meakin6f1f1212024-07-16 10:18:16 +01004796 /*
4797 * NOTE: This line is parsed by `hftest.py`.
4798 * If you change the format, make sure to update
4799 * `HFTEST_CTRL_JSON_REGEX` as well.
4800 */
4801 struct vcpu *vcpu = vcpu_locked->vcpu;
4802 struct log_buffer *buffer = &vcpu->log_buffer;
4803 ffa_id_t vm_id = vcpu->vm->id;
4804 ffa_id_t vcpu_id = vcpu_index(vcpu);
4805
Karl Meakin7de26952024-06-14 14:50:19 +01004806 buffer->chars[buffer->len] = '\0';
Olivier Deprezbd432092024-07-26 14:01:12 +02004807 dlog("[%x %u] %s\n", vm_id, vcpu_id, buffer->chars);
Karl Meakin7de26952024-06-14 14:50:19 +01004808 buffer->len = 0;
Karl Meakin705b56e2024-05-22 10:04:32 +01004809}
4810
4811/**
Karl Meakin740f74f2024-02-14 18:04:48 +00004812 * Implements FF-A v1.2 FFA_CONSOLE_LOG ABI for buffered logging.
Maksims Svecovs71b76702022-05-20 15:32:58 +01004813 */
4814struct ffa_value api_ffa_console_log(const struct ffa_value args,
4815 struct vcpu *current)
4816{
Karl Meakin740f74f2024-02-14 18:04:48 +00004817 /* Maximum number of characters is 128: 16 registers of 8 bytes each. */
4818 char chars[128] = {0};
Karl Meakin0e617d92024-04-05 12:55:22 +01004819 const bool v1_2 = current->vm->ffa_version >= FFA_VERSION_1_2;
Karl Meakin740f74f2024-02-14 18:04:48 +00004820 const bool log32 = args.func == FFA_CONSOLE_LOG_32;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004821
Karl Meakin740f74f2024-02-14 18:04:48 +00004822 /*
4823 * 32bit: always 6 registers
4824 * 64bit and less than v1.2: 6 registers
4825 * 64bit and v1.2 or greater: 16 registers
4826 */
Karl Meakin66a38bd2024-05-28 16:00:56 +01004827 /* NOLINTNEXTLINE(readability-avoid-nested-conditional-operator) */
Karl Meakin740f74f2024-02-14 18:04:48 +00004828 const size_t registers_max = log32 ? 6 : (v1_2 ? 16 : 6);
4829 const size_t chars_max =
4830 registers_max * (log32 ? sizeof(uint32_t) : sizeof(uint64_t));
4831 const size_t chars_count = args.arg1;
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004832 struct vcpu_locked vcpu_locked;
4833 struct log_buffer *log_buffer;
Karl Meakin740f74f2024-02-14 18:04:48 +00004834
4835 assert(args.func == FFA_CONSOLE_LOG_32 ||
4836 args.func == FFA_CONSOLE_LOG_64);
4837
4838 if (chars_count == 0 || chars_count > chars_max) {
Maksims Svecovs71b76702022-05-20 15:32:58 +01004839 return ffa_error(FFA_INVALID_PARAMETERS);
4840 }
4841
Karl Meakin740f74f2024-02-14 18:04:48 +00004842 if (log32) {
4843 uint32_t *registers = (uint32_t *)chars;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004844
Karl Meakin740f74f2024-02-14 18:04:48 +00004845 registers[0] = args.arg2 & 0xffffffff;
4846 registers[1] = args.arg3 & 0xffffffff;
4847 registers[2] = args.arg4 & 0xffffffff;
4848 registers[3] = args.arg5 & 0xffffffff;
4849 registers[4] = args.arg6 & 0xffffffff;
4850 registers[5] = args.arg7 & 0xffffffff;
4851 } else {
4852 uint64_t *registers = (uint64_t *)chars;
4853
4854 registers[0] = args.arg2;
4855 registers[1] = args.arg3;
4856 registers[2] = args.arg4;
4857 registers[3] = args.arg5;
4858 registers[4] = args.arg6;
4859 registers[5] = args.arg7;
4860 if (v1_2) {
4861 registers[6] = args.extended_val.arg8;
4862 registers[7] = args.extended_val.arg9;
4863 registers[8] = args.extended_val.arg10;
4864 registers[9] = args.extended_val.arg11;
4865 registers[10] = args.extended_val.arg12;
4866 registers[11] = args.extended_val.arg13;
4867 registers[12] = args.extended_val.arg14;
4868 registers[13] = args.extended_val.arg15;
4869 registers[14] = args.extended_val.arg16;
4870 registers[15] = args.extended_val.arg17;
4871 }
4872 }
4873
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004874 vcpu_locked = vcpu_lock(current);
4875 log_buffer = &current->log_buffer;
Karl Meakin7de26952024-06-14 14:50:19 +01004876
Karl Meakin740f74f2024-02-14 18:04:48 +00004877 for (size_t i = 0; i < chars_count; i++) {
4878 bool flush = false;
4879 const char c = chars[i];
4880
4881 if (c == '\n' || c == '\0') {
4882 flush = true;
4883 } else {
Karl Meakin7de26952024-06-14 14:50:19 +01004884 log_buffer->chars[log_buffer->len++] = c;
4885 flush = log_buffer->len == LOG_BUFFER_SIZE;
Karl Meakin740f74f2024-02-14 18:04:48 +00004886 }
4887
4888 if (flush) {
Karl Meakin6f1f1212024-07-16 10:18:16 +01004889 api_flush_log_buffer(&vcpu_locked);
Karl Meakin740f74f2024-02-14 18:04:48 +00004890 }
4891 }
Maksims Svecovs71b76702022-05-20 15:32:58 +01004892
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004893 vcpu_unlock(&vcpu_locked);
Maksims Svecovs71b76702022-05-20 15:32:58 +01004894 return (struct ffa_value){.func = FFA_SUCCESS_32};
4895}