blob: ed7f772a1ec273a8fe66a840a828f304afe7283b [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"
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +010026#include "hf/hf_ipi.h"
Andrew Scull6386f252018-12-06 13:29:10 +000027#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010028#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010029#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000030#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010031#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010032#include "hf/std.h"
Karl Meakinc5cebbc2024-06-17 11:30:27 +010033#include "hf/vcpu.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010034#include "hf/vm.h"
35
Andrew Scullf35a5c92018-08-07 18:09:46 +010036#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010037#include "vmapi/hf/ffa.h"
J-Alves126ab502022-09-29 11:37:33 +010038#include "vmapi/hf/ffa_v1_0.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010039
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000040static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010041 "Partition information descriptor size doesn't match the one in "
42 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000043static_assert(sizeof(struct ffa_partition_info) == 24,
44 "Partition information descriptor size doesn't match the one in "
45 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -080046static_assert((sizeof(struct ffa_partition_info) & 7) == 0,
47 "Partition information descriptor must be a multiple of 8 bytes"
48 " for ffa_partition_info_get_regs to work correctly. Information"
49 " from this structure are returned in 8 byte registers and the"
50 " count of 8 byte registers is returned by the ABI.");
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000051/*
52 * To eliminate the risk of deadlocks, we define a partial order for the
53 * acquisition of locks held concurrently by the same physical CPU. Our current
54 * ordering requirements are as follows:
55 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010056 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000057 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010058 * Locks of the same kind require the lock of lowest address to be locked first,
59 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000060 */
61
Andrew Scullaa039b32018-10-04 15:02:26 +010062static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010063 "Currently, a page is mapped for the send and receive buffers so "
64 "the maximum request is the size of a page.");
65
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000066static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
67 "The page pool entry size must be at least as big as the mailbox "
68 "size, so that memory region descriptors can be copied from the "
69 "mailbox for memory sharing.");
70
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -080071/*
72 * Maximum ffa_partition_info entries that can be returned by an invocation
73 * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available
74 * registers/args in struct ffa_value divided by size of struct
75 * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e.
76 * 15 uint64_t fields. For FF-A v1.1, this value should be 5.
77 */
78#define MAX_INFO_REGS_ENTRIES_PER_CALL \
79 ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info))
80static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5,
81 "FF-A v1.1 supports no more than 5 entries"
82 " per FFA_PARTITION_INFO_GET_REGS64 calls");
83
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000084static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000085
86/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000087 * Initialises the API page pool by taking ownership of the contents of the
88 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000089 */
90void api_init(struct mpool *ppool)
91{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000092 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000093}
94
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010095/**
J-Alvesad6a0432021-04-09 16:06:21 +010096 * Get target VM vCPU:
97 * If VM is UP then return first vCPU.
98 * If VM is MP then return vCPU whose index matches current CPU index.
99 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500100struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +0100101{
102 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
103 struct vcpu *vcpu = NULL;
104
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500105 CHECK((vm != NULL) && (current != NULL));
106
Karl Meakin82041822024-05-20 11:14:34 +0100107 if (vm_is_up(vm)) {
J-Alvesad6a0432021-04-09 16:06:21 +0100108 vcpu = vm_get_vcpu(vm, 0);
109 } else if (current_cpu_index < vm->vcpu_count) {
110 vcpu = vm_get_vcpu(vm, current_cpu_index);
111 }
112
113 return vcpu;
114}
115
116/**
J-Alvesfe7f7372020-11-09 11:32:12 +0000117 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
118 * is given as argument of the function.
119 *
120 * Called to change the context between SPs for direct messaging (when Hafnium
121 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
122 *
123 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100124 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000125 * - MP with pinned Execution Contexts.
126 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600127struct vcpu *api_switch_to_vm(struct vcpu_locked current_locked,
128 struct ffa_value to_ret,
J-Alves19e20cf2023-08-02 12:48:55 +0100129 enum vcpu_state vcpu_state, ffa_id_t to_id)
J-Alvesfe7f7372020-11-09 11:32:12 +0000130{
131 struct vm *to_vm = vm_find(to_id);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600132 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current_locked.vcpu);
J-Alvesfe7f7372020-11-09 11:32:12 +0000133
134 CHECK(next != NULL);
135
136 /* Set the return value for the target VM. */
137 arch_regs_set_retval(&next->regs, to_ret);
138
139 /* Set the current vCPU state. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600140 current_locked.vcpu->state = vcpu_state;
J-Alvesfe7f7372020-11-09 11:32:12 +0000141
142 return next;
143}
144
145/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000146 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100147 *
148 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100149 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100150 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600151struct vcpu *api_switch_to_primary(struct vcpu_locked current_locked,
J-Alves27b71962022-12-12 15:29:58 +0000152 struct ffa_value primary_ret,
153 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100154{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600155 return api_switch_to_vm(current_locked, primary_ret, secondary_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000156 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100157}
158
159/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200160 * Choose next vCPU to run to be the counterpart vCPU in the other
161 * world (run the normal world if currently running in the secure
162 * world). Set current vCPU state to the given vcpu_state parameter.
163 * Set FF-A return values to the target vCPU in the other world.
164 *
165 * Called in context of a direct message response from a secure
166 * partition to a VM.
167 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600168struct vcpu *api_switch_to_other_world(struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100169 struct ffa_value other_world_ret,
170 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200171{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600172 return api_switch_to_vm(current_locked, other_world_ret, vcpu_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000173 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200174}
175
176/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000177 * Returns true if the given vCPU is executing in context of an
178 * FFA_MSG_SEND_DIRECT_REQ invocation.
179 */
J-Alves27b71962022-12-12 15:29:58 +0000180bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000181{
Kathleen Capellae468c112023-12-13 17:56:28 -0500182 return locked.vcpu->direct_request_origin.vm_id != HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000183}
184
185/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100186 * Returns true if the VM owning the given vCPU is supporting managed exit and
187 * the vCPU is currently processing a managed exit.
188 */
189static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
190{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100191 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100192 vcpu_locked.vcpu->processing_managed_exit);
193}
194
195/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000196 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000197 */
198struct vcpu *api_preempt(struct vcpu *current)
199{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600200 struct vcpu_locked current_locked;
201 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100202 struct ffa_value ret = {
203 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200204 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000205 };
206
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600207 current_locked = vcpu_lock(current);
208 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_PREEMPTED);
209 vcpu_unlock(&current_locked);
210
211 return next;
Andrew Scull33fecd32019-01-08 14:48:27 +0000212}
213
214/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000215 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000216 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100217 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100218struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100219{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600220 struct vcpu_locked current_locked;
221 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100222 struct ffa_value ret = {
223 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
224 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100225 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000226
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600227 current_locked = vcpu_lock(current);
228 next = api_switch_to_primary(current_locked, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100229 VCPU_STATE_BLOCKED_INTERRUPT);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600230 vcpu_unlock(&current_locked);
231
232 return next;
Andrew Scullaa039b32018-10-04 15:02:26 +0100233}
234
235/**
Andrew Walbran33645652019-04-15 12:29:31 +0100236 * Puts the current vCPU in off mode, and returns to the primary VM.
237 */
238struct vcpu *api_vcpu_off(struct vcpu *current)
239{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600240 struct vcpu_locked current_locked;
241 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100242 struct ffa_value ret = {
243 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
244 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100245 };
246
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600247 current_locked = vcpu_lock(current);
Andrew Walbran33645652019-04-15 12:29:31 +0100248
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600249 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_OFF);
250 vcpu_unlock(&current_locked);
251
252 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100253}
254
255/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500256 * The current vCPU is blocked on some resource and needs to relinquish
257 * control back to the execution context of the endpoint that originally
258 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000259 */
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500260struct ffa_value api_yield(struct vcpu *current, struct vcpu **next,
261 struct ffa_value *args)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000262{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000263 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
264 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500265 bool transition_allowed;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500266 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500267 uint32_t timeout_low = 0;
268 uint32_t timeout_high = 0;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600269 struct vcpu_locked next_locked = (struct vcpu_locked){
270 .vcpu = NULL,
271 };
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500272
273 if (args != NULL) {
274 if (args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
275 args->arg7 != 0U) {
276 dlog_error(
277 "Parameters passed through registers X4-X7 "
278 "must be zero\n");
279 return ffa_error(FFA_INVALID_PARAMETERS);
280 }
281 timeout_low = (uint32_t)args->arg2 & 0xFFFFFFFF;
282 timeout_high = (uint32_t)args->arg3 & 0xFFFFFFFF;
283 }
Andrew Scull66d62bf2019-02-01 13:54:10 +0000284
Karl Meakin5e996992024-05-20 11:27:07 +0100285 if (vm_is_primary(current->vm)) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000286 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000287 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000288 }
289
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000290 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500291 transition_allowed = plat_ffa_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600292 current_locked, current->vm->id, HF_INVALID_VM_ID, next_locked,
293 FFA_YIELD_32, &next_state);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000294
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500295 if (!transition_allowed) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600296 ret = ffa_error(FFA_DENIED);
297 goto out;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000298 }
299
Madhukar Pappireddy65764072023-05-23 17:18:58 -0500300 /*
301 * The current vCPU is expected to move to BLOCKED state. However,
302 * under certain circumstances, it is allowed for the current vCPU
303 * to be resumed immediately without ever moving to BLOCKED state. One
304 * such scenario occurs when an SP's execution context attempts to
305 * yield cycles while handling secure interrupt. Refer to the comments
306 * in the SPMC variant of the plat_ffa_yield_prepare function.
307 */
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500308 assert(!vm_id_is_current_world(current->vm->id) ||
309 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500310
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600311 ret = plat_ffa_yield_prepare(current_locked, next, timeout_low,
312 timeout_high);
313out:
314 vcpu_unlock(&current_locked);
315 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000316}
317
318/**
Andrew Walbran33645652019-04-15 12:29:31 +0100319 * Switches to the primary so that it can switch to the target, or kick it if it
320 * is already running on a different physical CPU.
321 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600322static struct vcpu *api_wake_up_locked(struct vcpu_locked current_locked,
323 struct vcpu *target_vcpu)
Andrew Walbran33645652019-04-15 12:29:31 +0100324{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100325 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100326 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
328 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100329 };
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600330
331 return api_switch_to_primary(current_locked, ret, VCPU_STATE_BLOCKED);
332}
333
334struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
335{
336 struct vcpu_locked current_locked;
337 struct vcpu *next;
338
339 current_locked = vcpu_lock(current);
340 next = api_wake_up_locked(current_locked, target_vcpu);
341 vcpu_unlock(&current_locked);
342
343 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100344}
345
346/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000347 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000348 */
349struct vcpu *api_abort(struct vcpu *current)
350{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100351 struct ffa_value ret = ffa_error(FFA_ABORTED);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600352 struct vcpu_locked current_locked;
353 struct vcpu *next;
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500354 struct vm_locked vm_locked;
Andrew Scull9726c252019-01-23 13:44:19 +0000355
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100356 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000357 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000358
Karl Meakin5e996992024-05-20 11:27:07 +0100359 if (vm_is_primary(current->vm)) {
Andrew Scull9726c252019-01-23 13:44:19 +0000360 /* TODO: what to do when the primary aborts? */
361 for (;;) {
362 /* Do nothing. */
363 }
364 }
365
366 atomic_store_explicit(&current->vm->aborting, true,
367 memory_order_relaxed);
368
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500369 vm_locked = vm_lock(current->vm);
370 plat_ffa_free_vm_resources(vm_locked);
371 vm_unlock(&vm_locked);
Andrew Scull9726c252019-01-23 13:44:19 +0000372
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600373 current_locked = vcpu_lock(current);
374 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_ABORTED);
375 vcpu_unlock(&current_locked);
376
377 return next;
Andrew Scull9726c252019-01-23 13:44:19 +0000378}
379
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000380/*
381 * Format the partition info descriptors according to the version supported
382 * by the endpoint and return the size of the array created.
383 */
384static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000385 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000386 uint32_t vm_count)
387{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000388 struct vm *vm = vm_locked.vm;
Karl Meakin0e617d92024-04-05 12:55:22 +0100389 enum ffa_version version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100390 uint32_t partition_info_size;
391 uint32_t buffer_size;
Federico Recanati644f0462022-03-17 12:04:00 +0100392 struct ffa_value ret;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000393
J-Alvese8c8c2b2022-12-16 15:34:48 +0000394 /* Acquire receiver's RX buffer. */
395 if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) {
396 dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id);
397 return ret;
398 }
399
J-Alves122f1a12022-12-12 15:55:42 +0000400 if (vm_is_mailbox_busy(vm_locked)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000401 /*
402 * Can't retrieve memory information if the mailbox is not
403 * available.
404 */
405 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000406 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000407 }
408
Karl Meakin0e617d92024-04-05 12:55:22 +0100409 if (version == FFA_VERSION_1_0) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000410 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
411
Daniel Boulby835e1592022-05-30 17:38:51 +0100412 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
413 buffer_size = partition_info_size * vm_count;
414 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000415 dlog_error(
416 "Partition information does not fit in the "
J-Alves0a21db12024-03-28 12:43:30 +0000417 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000418 return ffa_error(FFA_NO_MEMORY);
419 }
420
421 for (uint32_t i = 0; i < vm_count; i++) {
422 /*
423 * Populate the VM's RX buffer with the partition
Kathleen Capella402fa852022-11-09 16:16:51 -0500424 * information. Clear properties bits that must be zero
425 * according to DEN0077A FF-A v1.0 REL Table 8.25.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000426 */
427 recv_mailbox[i].vm_id = partitions[i].vm_id;
428 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
Kathleen Capella402fa852022-11-09 16:16:51 -0500429 recv_mailbox[i].properties =
430 partitions[i].properties &
431 ~FFA_PARTITION_v1_0_RES_MASK;
Daniel Boulby191b5f02022-02-17 17:26:14 +0000432 }
433
434 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100435 partition_info_size = sizeof(struct ffa_partition_info);
436 buffer_size = partition_info_size * vm_count;
J-Alves0a21db12024-03-28 12:43:30 +0000437
Daniel Boulby835e1592022-05-30 17:38:51 +0100438 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000439 dlog_error(
440 "Partition information does not fit in the "
J-Alves0a824e92024-04-26 16:20:12 +0100441 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000442 return ffa_error(FFA_NO_MEMORY);
443 }
444
J-Alves0a21db12024-03-28 12:43:30 +0000445 /*
446 * Populate the VM's RX buffer with the partition information.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000447 */
J-Alves0a21db12024-03-28 12:43:30 +0000448 if (!memcpy_trapped(vm->mailbox.recv, HF_MAILBOX_SIZE,
449 partitions, buffer_size)) {
450 dlog_error(
451 "%s: Failed to copy ffa_partition_info "
452 "descriptor\n",
453 __func__);
454 return ffa_error(FFA_ABORTED);
455 }
Daniel Boulby191b5f02022-02-17 17:26:14 +0000456 }
457
Daniel Boulby835e1592022-05-30 17:38:51 +0100458 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000459
460 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000461 vm->mailbox.recv_sender = HF_VM_ID_BASE;
462 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000463 vm->mailbox.state = MAILBOX_STATE_FULL;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000464
Daniel Boulby835e1592022-05-30 17:38:51 +0100465 /*
466 * Return the count of partition information descriptors in w2
467 * and the size of the descriptors in w3.
468 */
469 return (struct ffa_value){.func = FFA_SUCCESS_32,
470 .arg2 = vm_count,
471 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000472}
473
J-Alves7fb0fdb2024-10-09 11:28:07 +0100474/**
475 * Set properties accoridng to the version of the partition, and the FF-A
476 * features it supports.
477 */
478static ffa_partition_properties_t api_ffa_partitions_info_get_properties(
479 ffa_id_t caller_id, struct vm *vm)
480{
481 ffa_partition_properties_t properties;
482
483 properties = plat_ffa_partition_properties(caller_id, vm);
484 properties |= FFA_PARTITION_AARCH64_EXEC;
485
486 if (vm->ffa_version >= FFA_VERSION_1_1) {
487 properties |= vm_are_notifications_enabled(vm)
488 ? FFA_PARTITION_NOTIFICATION
489 : 0;
490 properties |= vm->messaging_method & FFA_PARTITION_INDIRECT_MSG;
491 }
492
493 return properties;
494}
495
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100496static void api_ffa_fill_partition_info(
J-Alves7fb0fdb2024-10-09 11:28:07 +0100497 struct ffa_partition_info *out_partition, struct vm *vm,
498 ffa_id_t caller_id)
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800499{
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100500 out_partition->vm_id = vm->id;
501 out_partition->vcpu_count = vm->vcpu_count;
J-Alves7fb0fdb2024-10-09 11:28:07 +0100502 out_partition->properties =
503 api_ffa_partitions_info_get_properties(caller_id, vm);
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100504}
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800505
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100506/**
507 * Find VMs with UUID matching `uuid_to_find` , and fill `out_partitions` with
508 * partition infos. Returns number of VMs that matched.
509 * A null UUID matches against any VM.
510 * If `count_flag` is true, no partition infos are written to `out_partitions`,
511 * only the number of VMs that matched is returned.
512 */
513static ffa_vm_count_t api_ffa_fill_partitions_info_array(
514 struct ffa_partition_info out_partitions[], size_t out_partitions_len,
J-Alves7fb0fdb2024-10-09 11:28:07 +0100515 const struct ffa_uuid *uuid_to_find, bool count_flag,
516 ffa_id_t caller_id)
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100517{
518 ffa_vm_count_t vms_found = 0;
519 bool match_any = ffa_uuid_is_null(uuid_to_find);
520
521 assert(vm_get_count() <= out_partitions_len);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800522
523 /*
524 * Iterate through the VMs to find the ones with a matching
525 * UUID. A Null UUID retrieves information for all VMs.
526 */
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100527 for (ffa_vm_count_t vm_idx = 0; vm_idx < vm_get_count(); vm_idx++) {
528 struct vm *vm = vm_find_index(vm_idx);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800529
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100530 for (size_t uuid_idx = 0; uuid_idx < PARTITION_MAX_UUIDS;
531 uuid_idx++) {
532 struct ffa_uuid uuid = vm->uuids[uuid_idx];
533 struct ffa_partition_info *out_partition =
534 &out_partitions[vms_found];
535
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400536 /*
537 * Null UUID indicates reaching the end of a
538 * partition's array of UUIDs.
539 */
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100540 if (ffa_uuid_is_null(&uuid)) {
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400541 break;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800542 }
543
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100544 if (match_any || ffa_uuid_equal(uuid_to_find, &uuid)) {
545 vms_found++;
546
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400547 if (count_flag) {
548 continue;
549 }
550
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100551 api_ffa_fill_partition_info(out_partition, vm,
J-Alves7fb0fdb2024-10-09 11:28:07 +0100552 caller_id);
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100553 if (match_any) {
554 out_partition->uuid = uuid;
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400555 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800556 }
557 }
558 }
559
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100560 return vms_found;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800561}
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800562
563static inline void api_ffa_pack_vmid_count_props(
J-Alves19e20cf2023-08-02 12:48:55 +0100564 uint64_t *xn, ffa_id_t vm_id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800565 ffa_partition_properties_t properties)
566{
567 *xn = (uint64_t)vm_id;
568 *xn |= (uint64_t)vcpu_count << 16;
569 *xn |= (uint64_t)properties << 32;
570}
571
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700572/**
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700573 * This function forwards the FFA_PARTITION_INFO_GET_REGS ABI to the other world
574 * when hafnium is the hypervisor to determine the secure partitions. When
575 * hafnium is the SPMC, this function forwards the call to the SPMD to discover
576 * SPMD logical partitions. The function returns true when partition information
577 * is filled in the partitions array and false if there are errors. Note that
578 * the SPMD and SPMC may return an FF-A error code of FFA_NOT_SUPPORTED when
579 * there are no SPMD logical partitions or no secure partitions respectively,
580 * and this is not considered a failure of the forwarded call. A caller is
581 * expected to check the return value before consuming the information in the
582 * partitions array passed in and ret_count.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700583 */
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700584static bool api_ffa_partition_info_get_regs_forward(
585 const struct ffa_uuid *uuid, const uint16_t tag,
586 struct ffa_partition_info *partitions, uint16_t partitions_len,
587 ffa_vm_count_t *ret_count)
588{
589 (void)tag;
590 struct ffa_value ret;
591 uint16_t last_index = UINT16_MAX;
592 uint16_t curr_index = 0;
593 uint16_t start_index = 0;
594
595 if (!plat_ffa_partition_info_get_regs_forward_allowed()) {
596 return true;
597 }
598
599 while (start_index <= last_index) {
600 ret = ffa_partition_info_get_regs(uuid, start_index, 0);
601 if (ffa_func_id(ret) != FFA_SUCCESS_64) {
602 /*
603 * If there are no logical partitions, SPMD returns
604 * NOT_SUPPORTED, that is not an error. If there are no
605 * secure partitions the SPMC returns NOT_SUPPORTED.
606 */
607 if ((ffa_func_id(ret) == FFA_ERROR_32) &&
608 (ffa_error_code(ret) == FFA_NOT_SUPPORTED)) {
609 return true;
610 }
611
612 return false;
613 }
614
615 if (!api_ffa_fill_partition_info_from_regs(
616 ret, start_index, partitions, partitions_len,
617 ret_count)) {
618 return false;
619 }
620
621 last_index = ffa_partition_info_regs_get_last_idx(ret);
622 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
623 start_index = curr_index + 1;
624 }
625 return true;
626}
627
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700628bool api_ffa_fill_partition_info_from_regs(
629 struct ffa_value ret, uint16_t start_index,
630 struct ffa_partition_info *partitions, uint16_t partitions_len,
631 ffa_vm_count_t *ret_count)
632{
633 uint16_t vm_count = *ret_count;
634 uint16_t curr_index = 0;
635 uint8_t num_entries = 0;
636 uint8_t idx = 0;
637 /* List of pointers to args in return value. */
638 uint64_t *arg_ptrs[] = {
639 &ret.arg3,
640 &ret.arg4,
641 &ret.arg5,
642 &ret.arg6,
643 &ret.arg7,
644 &ret.extended_val.arg8,
645 &ret.extended_val.arg9,
646 &ret.extended_val.arg10,
647 &ret.extended_val.arg11,
648 &ret.extended_val.arg12,
649 &ret.extended_val.arg13,
650 &ret.extended_val.arg14,
651 &ret.extended_val.arg15,
652 &ret.extended_val.arg16,
653 &ret.extended_val.arg17,
654 };
655
656 if (vm_count > partitions_len) {
657 return false;
658 }
659
660 /*
661 * Tags are currently unused in the implementation. Expect it to be
662 * zero since the implementation does not provide a tag when calling
663 * the FFA_PARTITION_INFO_GET_REGS ABI.
664 */
665 assert(ffa_partition_info_regs_get_tag(ret) == 0);
666
667 /*
668 * Hafnium expects the size of the returned descriptor to be equal to
669 * the size of the structure in the FF-A 1.1 specification. When future
670 * enhancements are made, this assert can be relaxed.
671 */
672 assert(ffa_partition_info_regs_get_desc_size(ret) ==
673 sizeof(struct ffa_partition_info));
674
675 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
676
677 /* FF-A 1.2 ALP0, section 14.9.2 Usage rule 7. */
678 assert(start_index <= curr_index);
679
680 num_entries = curr_index - start_index + 1;
681 if (num_entries > (partitions_len - vm_count) ||
682 num_entries > MAX_INFO_REGS_ENTRIES_PER_CALL) {
683 return false;
684 }
685
686 while (num_entries) {
687 uint64_t info = *(arg_ptrs[(ptrdiff_t)(idx++)]);
688 uint64_t uuid_lo = *(arg_ptrs[(ptrdiff_t)(idx++)]);
689 uint64_t uuid_high = *(arg_ptrs[(ptrdiff_t)(idx++)]);
690
691 partitions[vm_count].vm_id = info & 0xFFFF;
692 partitions[vm_count].vcpu_count = (info >> 16) & 0xFFFF;
693 partitions[vm_count].properties = (info >> 32);
694 partitions[vm_count].uuid.uuid[0] = uuid_lo & 0xFFFFFFFF;
695 partitions[vm_count].uuid.uuid[1] =
696 (uuid_lo >> 32) & 0xFFFFFFFF;
697 partitions[vm_count].uuid.uuid[2] = uuid_high & 0xFFFFFFFF;
698 partitions[vm_count].uuid.uuid[3] =
699 (uuid_high >> 32) & 0xFFFFFFFF;
700 vm_count++;
701 num_entries--;
702 }
703
704 *ret_count = vm_count;
705 return true;
706}
707
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800708struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current,
709 const struct ffa_uuid *uuid,
710 const uint16_t start_index,
711 const uint16_t tag)
712{
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800713 struct vm *current_vm = current->vm;
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800714 static struct ffa_partition_info partitions[2 * MAX_VMS];
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800715 bool uuid_is_null = ffa_uuid_is_null(uuid);
716 ffa_vm_count_t vm_count = 0;
717 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
718 uint16_t max_idx = 0;
719 uint16_t curr_idx = 0;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800720 uint8_t num_entries_to_ret = 0;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800721 uint8_t arg_idx = 3;
722
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800723 /* list of pointers to args in return value */
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800724 uint64_t *arg_ptrs[] = {
725 &(ret).func,
726 &(ret).arg1,
727 &(ret).arg2,
728 &(ret).arg3,
729 &(ret).arg4,
730 &(ret).arg5,
731 &(ret).arg6,
732 &(ret).arg7,
733 &(ret).extended_val.arg8,
734 &(ret).extended_val.arg9,
735 &(ret).extended_val.arg10,
736 &(ret).extended_val.arg11,
737 &(ret).extended_val.arg12,
738 &(ret).extended_val.arg13,
739 &(ret).extended_val.arg14,
740 &(ret).extended_val.arg15,
741 &(ret).extended_val.arg16,
742 &(ret).extended_val.arg17,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800743 };
744
745 /* TODO: Add support for using tags */
746 if (tag != 0) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800747 dlog_error("Tag not 0. Unsupported tag. %d\n", tag);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800748 return ffa_error(FFA_RETRY);
749 }
750
751 memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions));
752
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100753 vm_count = api_ffa_fill_partitions_info_array(
754 partitions, ARRAY_SIZE(partitions), uuid, false,
755 current_vm->id);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800756
757 /* If UUID is Null vm_count must not be zero at this stage. */
758 CHECK(!uuid_is_null || vm_count != 0);
759
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800760 /*
761 * When running the Hypervisor:
762 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
763 * it to fill with secure partitions information.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700764 * When running the SPMC, the SPMC forwards the call to the SPMD to
765 * discover any EL3 SPMD logical partitions, if the call came from an
766 * SP. Otherwise, the call is not forwarded.
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800767 * TODO: Note that for this ABI, forwarding on every invocation when
768 * uuid is Null is inefficient,and if performance becomes a problem,
769 * this would be a good place to optimize using strategies such as
770 * caching info etc. For now, assuming this inefficiency is not a major
771 * issue.
772 * - If UUID is non-Null vm_count may be zero because the UUID matches
773 * a secure partition and the query is forwarded to the SPMC.
774 * When running the SPMC:
775 * - If UUID is non-Null and vm_count is zero it means there is no such
776 * partition identified in the system.
777 */
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700778 if (vm_id_is_current_world(current_vm->id)) {
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700779 if (!api_ffa_partition_info_get_regs_forward(
780 uuid, tag, partitions, ARRAY_SIZE(partitions),
781 &vm_count)) {
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700782 dlog_error(
783 "Failed to forward "
784 "ffa_partition_info_get_regs.\n");
785 return ffa_error(FFA_DENIED);
786 }
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800787 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800788
789 /*
790 * Unrecognized UUID: does not match any of the VMs (or SPs)
791 * and is not Null.
792 */
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800793 if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) {
Olivier Deprez89da0a42023-09-01 17:38:33 +0200794 dlog_verbose(
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800795 "Invalid parameters. vm_count = %d (must not be zero "
Karl Meakine8937d92024-03-19 16:04:25 +0000796 "or > %lu)\n",
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800797 vm_count, ARRAY_SIZE(partitions));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800798 return ffa_error(FFA_INVALID_PARAMETERS);
799 }
800
801 if (start_index >= vm_count) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800802 dlog_error(
803 "start index = %d vm_count = %d (start_index must be "
804 "less than vm_count)\n",
805 start_index, vm_count);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800806 return ffa_error(FFA_INVALID_PARAMETERS);
807 }
808
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800809 max_idx = vm_count - 1;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800810 num_entries_to_ret = (max_idx - start_index) + 1;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800811 num_entries_to_ret =
812 MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800813 curr_idx = start_index + num_entries_to_ret - 1;
814 assert(curr_idx <= max_idx);
815
816 ret.func = FFA_SUCCESS_64;
817 ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48;
818 ret.arg2 |= curr_idx << 16;
819 ret.arg2 |= max_idx;
820
821 if (num_entries_to_ret > 1) {
822 ret.extended_val.valid = 1;
823 }
824
825 for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800826 uint64_t *xn_0 = arg_ptrs[arg_idx++];
827 uint64_t *xn_1 = arg_ptrs[arg_idx++];
828 uint64_t *xn_2 = arg_ptrs[arg_idx++];
829
830 api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id,
831 partitions[idx].vcpu_count,
832 partitions[idx].properties);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800833 if (uuid_is_null) {
Karl Meakin9478e322024-09-23 17:47:09 +0100834 ffa_uuid_to_u64x2(xn_1, xn_2, &partitions[idx].uuid);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800835 }
Raghu Krishnamurthycded31b2023-04-23 15:07:35 -0700836 assert(arg_idx <= ARRAY_SIZE(arg_ptrs));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800837 }
838
839 return ret;
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800840}
841
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100842struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000843 const struct ffa_uuid *uuid,
844 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100845{
846 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100847 ffa_vm_count_t vm_count = 0;
Olivier Deprez013f4d62022-11-21 15:46:20 +0100848 bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) ==
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000849 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100850 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100851 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000852 struct vm_locked vm_locked;
853 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100854
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000855 /* Bits 31:1 Must Be Zero */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100856 if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) {
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000857 return ffa_error(FFA_INVALID_PARAMETERS);
858 }
859
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100860 vm_count = api_ffa_fill_partitions_info_array(
861 partitions, ARRAY_SIZE(partitions), uuid, count_flag,
862 current_vm->id);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100863
Olivier Depreze562e542020-06-11 17:31:54 +0200864 /* If UUID is Null vm_count must not be zero at this stage. */
865 CHECK(!uuid_is_null || vm_count != 0);
866
867 /*
868 * When running the Hypervisor:
869 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
870 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000871 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200872 * a secure partition and the query is forwarded to the SPMC.
873 * When running the SPMC:
874 * - If UUID is non-Null and vm_count is zero it means there is no such
875 * partition identified in the system.
876 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000877 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200878
879 /*
880 * Unrecognized UUID: does not match any of the VMs (or SPs)
881 * and is not Null.
882 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100883 if (vm_count == 0) {
884 return ffa_error(FFA_INVALID_PARAMETERS);
885 }
886
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000887 /*
888 * If the count flag is set we don't need to return the partition info
889 * descriptors.
890 */
891 if (count_flag) {
892 return (struct ffa_value){.func = FFA_SUCCESS_32,
893 .arg2 = vm_count};
894 }
895
Daniel Boulby191b5f02022-02-17 17:26:14 +0000896 vm_locked = vm_lock(current_vm);
897 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
898 vm_count);
899 vm_unlock(&vm_locked);
900 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100901}
902
Andrew Scull9726c252019-01-23 13:44:19 +0000903/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000904 * Returns the ID of the VM.
905 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100906struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000907{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100908 return (struct ffa_value){.func = FFA_SUCCESS_32,
909 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000910}
911
912/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200913 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
914 * FF-A instances.
915 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000916 */
917struct ffa_value api_ffa_spm_id_get(void)
918{
Karl Meakin0e617d92024-04-05 12:55:22 +0100919 if (FFA_VERSION_1_1 <= FFA_VERSION_COMPILED) {
920 /*
921 * Return the SPMC ID that was fetched during FF-A
922 * initialization.
923 */
924 return (struct ffa_value){.func = FFA_SUCCESS_32,
925 .arg2 = arch_ffa_spmc_id_get()};
926 }
927
J-Alves3829fc02021-03-18 12:49:18 +0000928 return ffa_error(FFA_NOT_SUPPORTED);
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000929}
930
931/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000932 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000933 * function to indicate that register state for the given vCPU has been saved
934 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000935 */
936void api_regs_state_saved(struct vcpu *vcpu)
937{
938 sl_lock(&vcpu->lock);
939 vcpu->regs_available = true;
940 sl_unlock(&vcpu->lock);
941}
J-Alves0ffce752024-10-09 14:37:10 +0100942
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000943/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000944 * Assuming that the arguments have already been checked by the caller, injects
945 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
946 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
947 * is next run, which is up to the scheduler.
948 *
949 * Returns:
950 * - 0 on success if no further action is needed.
951 * - 1 if it was called by the primary VM and the primary VM now needs to wake
952 * up or kick the target vCPU.
953 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100954int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600955 uint32_t intid,
956 struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100957 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000958{
Manish Pandey35e452f2021-02-18 21:36:34 +0000959 struct vcpu *target_vcpu = target_locked.vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600960 struct vcpu *current = current_locked.vcpu;
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100961 struct interrupts *interrupts = &target_vcpu->interrupts;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000962 int64_t ret = 0;
963
Andrew Walbran508e63c2018-12-20 17:02:37 +0000964 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000965 * We only need to change state and (maybe) trigger a virtual interrupt
966 * if it is enabled and was not previously pending. Otherwise we can
967 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000968 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100969 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
970 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000971 goto out;
972 }
973
974 /* Increment the count. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100975 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000976
977 /*
978 * Only need to update state if there was not already an
979 * interrupt enabled and pending.
980 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000981 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000982 goto out;
983 }
984
Karl Meakin5e996992024-05-20 11:27:07 +0100985 if (vm_is_primary(current->vm)) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000986 /*
987 * If the call came from the primary VM, let it know that it
988 * should run or kick the target vCPU.
989 */
990 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000991 } else if (current != target_vcpu && next != NULL) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600992 *next = api_wake_up_locked(current_locked, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000993 }
994
995out:
996 /* Either way, make it pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100997 vcpu_virt_interrupt_set_pending(interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000998
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200999 return ret;
1000}
1001
Andrew Walbran508e63c2018-12-20 17:02:37 +00001002/**
J-Alves2ced1672022-12-12 14:35:38 +00001003 * Constructs the return value from a successful FFA_MSG_WAIT call, when used
1004 * with FFA_MSG_SEND_32.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001005 */
J-Alves27b71962022-12-12 15:29:58 +00001006struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001007{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001008 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001009 case FFA_MSG_SEND_32:
1010 return (struct ffa_value){
1011 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001012 .arg1 = (receiver->mailbox.recv_sender << 16) |
1013 receiver->id,
1014 .arg3 = receiver->mailbox.recv_size};
J-Alvesd9e7c8f2024-09-11 13:37:25 +01001015 default:
Federico Recanati25053ee2022-03-14 15:01:53 +01001016 return (struct ffa_value){
1017 .func = FFA_RUN_32,
1018 /*
1019 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
1020 * Retrieving vCPU requires a rework of the function,
1021 * while receiver ID must be set because it's checked by
1022 * other APIs (eg: FFA_NOTIFICATION_GET).
1023 */
1024 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001025 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001026}
1027
J-Alvese8c8c2b2022-12-16 15:34:48 +00001028/**
1029 * Change the state of mailbox to empty, such that the ownership is given to the
1030 * Partition manager.
Karl Meakin1064a9c2024-06-04 17:20:43 +01001031 * Returns FFA_SUCCESS if the mailbox was reset successfully, FFA_ERROR
1032 * otherwise.
J-Alvese8c8c2b2022-12-16 15:34:48 +00001033 */
Karl Meakin1064a9c2024-06-04 17:20:43 +01001034static struct ffa_value api_release_mailbox(struct vm_locked vm_locked)
J-Alvese8c8c2b2022-12-16 15:34:48 +00001035{
Karl Meakin1064a9c2024-06-04 17:20:43 +01001036 struct ffa_value ret = {.func = FFA_SUCCESS_32};
J-Alves19e20cf2023-08-02 12:48:55 +01001037 ffa_id_t vm_id = vm_locked.vm->id;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001038
1039 switch (vm_locked.vm->mailbox.state) {
1040 case MAILBOX_STATE_EMPTY:
1041 dlog_verbose("Mailbox of %x is empty.\n", vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001042 ret = ffa_error(FFA_DENIED);
J-Alves31f8bef2023-07-14 12:46:28 +01001043 break;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001044 case MAILBOX_STATE_FULL:
1045 /* Check it doesn't have pending RX full notifications. */
1046 if (vm_are_fwk_notifications_pending(vm_locked)) {
1047 dlog_verbose(
1048 "Mailbox of endpoint %x has pending "
1049 "messages.\n",
1050 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001051 ret = ffa_error(FFA_DENIED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001052 }
1053 break;
1054 case MAILBOX_STATE_OTHER_WORLD_OWNED:
1055 /*
1056 * The SPMC shouldn't let SP's mailbox get into this state.
1057 * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst
1058 * the mailbox is in this state. In that case, we should report
1059 * error.
1060 */
1061 if (vm_id_is_current_world(vm_id)) {
1062 dlog_verbose(
Karl Meakin1064a9c2024-06-04 17:20:43 +01001063 "Mailbox of endpoint %x is in an incorrect "
1064 "state.\n",
J-Alvese8c8c2b2022-12-16 15:34:48 +00001065 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001066 ret = ffa_error(FFA_ABORTED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001067 }
1068 break;
1069 }
1070
Karl Meakin1064a9c2024-06-04 17:20:43 +01001071 if (ret.func == FFA_SUCCESS_32) {
1072 vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001073 }
1074
Karl Meakin1064a9c2024-06-04 17:20:43 +01001075 return ret;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001076}
1077
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001078/*
1079 * Helper to check if extended arguments (corresponding to regs x8-x17)
1080 * are zeroed out.
1081 */
Kathleen Capella036cc592023-11-30 18:26:15 -05001082bool api_extended_args_are_zero(struct ffa_value *args)
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001083{
1084 if (args->extended_val.arg8 != 0U || args->extended_val.arg9 != 0U ||
1085 args->extended_val.arg10 != 0U || args->extended_val.arg11 != 0U ||
1086 args->extended_val.arg12 != 0U || args->extended_val.arg13 != 0U ||
1087 args->extended_val.arg14 != 0U || args->extended_val.arg15 != 0U ||
1088 args->extended_val.arg16 != 0U || args->extended_val.arg17 != 0U) {
1089 return false;
1090 }
1091 return true;
1092}
1093
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001094static void api_ffa_msg_wait_rx_release(struct vcpu *current)
1095{
1096 struct vm_locked vm_locked;
1097
1098 vm_locked = plat_ffa_vm_find_locked(current->vm->id);
1099 if (vm_locked.vm == NULL) {
1100 return;
1101 }
1102
1103 api_release_mailbox(vm_locked);
1104
1105 if (vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY) {
1106 dlog_warning("Mailbox not released to producer\n");
1107 }
1108
1109 vm_unlock(&vm_locked);
1110}
1111
Kathleen Capella7253bd52024-08-14 17:36:09 -04001112static bool api_retain_rx_buffer_ownership(struct ffa_value args)
1113{
1114 return ((args.arg2 & FFA_MSG_WAIT_FLAG_RETAIN_RX) != 0U);
1115}
1116
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001117struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
1118 struct ffa_value *args)
1119{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001120 struct vcpu_locked current_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001121 enum vcpu_state next_state = VCPU_STATE_RUNNING;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001122 struct ffa_value ret;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001123 struct vcpu_locked next_locked = (struct vcpu_locked){
1124 .vcpu = NULL,
1125 };
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001126
Kathleen Capella7253bd52024-08-14 17:36:09 -04001127 if (args->arg1 != 0U || args->arg3 != 0U || args->arg4 != 0U ||
1128 args->arg5 != 0U || args->arg6 != 0U || args->arg7 != 0U) {
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001129 return ffa_error(FFA_INVALID_PARAMETERS);
1130 }
1131
Kathleen Capella7253bd52024-08-14 17:36:09 -04001132 if (current->vm->ffa_version >= FFA_VERSION_1_2) {
1133 if (!api_extended_args_are_zero(args)) {
1134 return ffa_error(FFA_INVALID_PARAMETERS);
1135 }
1136 } else {
1137 if (args->arg2 != 0U) {
1138 return ffa_error(FFA_INVALID_PARAMETERS);
1139 }
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001140 }
1141
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001142 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001143 if (!plat_ffa_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001144 current_locked, current->vm->id, HF_INVALID_VM_ID,
1145 next_locked, FFA_MSG_WAIT_32, &next_state)) {
1146 ret = ffa_error(FFA_DENIED);
1147 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001148 }
1149
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001150 assert(!vm_id_is_current_world(current->vm->id) ||
1151 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001152
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001153 ret = plat_ffa_msg_wait_prepare(current_locked, next);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001154
1155 /*
1156 * To maintain partial ordering of locks, release vCPU lock before
1157 * releasing the VM's RX buffer, a process which requires locking the
1158 * VM.
1159 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001160out:
1161 vcpu_unlock(&current_locked);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001162
Kathleen Capella7253bd52024-08-14 17:36:09 -04001163 if (ret.func != FFA_ERROR_32 &&
1164 !api_retain_rx_buffer_ownership(*args)) {
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001165 api_ffa_msg_wait_rx_release(current);
1166 }
J-Alvese8c8c2b2022-12-16 15:34:48 +00001167 return ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001168}
1169
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001170/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001171 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001172 * value needs to be forced onto the vCPU.
1173 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001174static bool api_vcpu_prepare_run(struct vcpu_locked current_locked,
1175 struct vcpu_locked vcpu_next_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001176 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001177{
Max Shvetsov40108e72020-08-27 12:39:50 +01001178 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001179 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001180 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001181 bool vcpu_was_init_state = false;
J-Alves6e2abc62021-12-02 14:58:56 +00001182 bool need_vm_lock;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001183 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001184
J-Alves478faac2024-10-23 10:35:57 +01001185 const struct ffa_value ffa_run_abi =
1186 (struct ffa_value){.func = FFA_RUN_32};
1187 const struct ffa_value *ffa_run_ret = NULL;
1188
Andrew Scullb06d1752019-02-04 10:15:48 +00001189 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001190 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +00001191 *
Andrew Scull4caadaf2019-07-03 13:13:47 +01001192 * The VM lock is not needed in the common case so it must only be taken
1193 * when it is going to be needed. This ensures there are no inter-vCPU
1194 * dependencies in the common run case meaning the sensitive context
1195 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +00001196 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001197 struct vcpu *vcpu = vcpu_next_locked.vcpu;
1198 struct vcpu *current = current_locked.vcpu;
Max Shvetsov40108e72020-08-27 12:39:50 +01001199
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001200 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +00001201 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
1202 (!vcpu->vm->el0_partition &&
1203 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
1204 vcpu->state == VCPU_STATE_BLOCKED ||
1205 vcpu->state == VCPU_STATE_PREEMPTED));
1206
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001207 if (need_vm_lock) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001208 vcpu_unlock(&vcpu_next_locked);
1209 vcpu_unlock(&current_locked);
Max Shvetsov40108e72020-08-27 12:39:50 +01001210 vm_locked = vm_lock(vcpu->vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001211
1212 /* Lock both vCPUs at once to avoid deadlock. */
1213 vcpus_locked = vcpu_lock_both(current, vcpu);
1214 current_locked = vcpus_locked.vcpu1;
1215 vcpu_next_locked = vcpus_locked.vcpu2;
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001216 }
1217
1218 /*
1219 * If the vCPU is already running somewhere then we can't run it here
1220 * simultaneously. While it is actually running then the state should be
1221 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
1222 * stops running but while Hafnium is in the process of switching back
1223 * to the primary there will be a brief period while the state has been
1224 * updated but `regs_available` is still false (until
1225 * `api_regs_state_saved` is called). We can't start running it again
1226 * until this has finished, so count this state as still running for the
1227 * purposes of this check.
1228 */
1229 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
1230 /*
1231 * vCPU is running on another pCPU.
1232 *
1233 * It's okay not to return the sleep duration here because the
1234 * other physical CPU that is currently running this vCPU will
1235 * return the sleep duration if needed.
1236 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001237 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001238 ret = false;
1239 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001240 }
Andrew Scull9726c252019-01-23 13:44:19 +00001241
1242 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001243 if (vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04001244 dlog_verbose("VM %#x was aborted, cannot run vCPU %u\n",
1245 vcpu->vm->id, vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +01001246 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +00001247 }
Kathleen Capella6ab05132023-05-10 12:27:35 -04001248 *run_ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +00001249 ret = false;
1250 goto out;
1251 }
1252
Andrew Walbran508e63c2018-12-20 17:02:37 +00001253 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001254 case VCPU_STATE_RUNNING:
1255 case VCPU_STATE_OFF:
1256 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001257 ret = false;
1258 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001259
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001260 case VCPU_STATE_WAITING:
1261 /*
Olivier Deprezb2808332023-02-02 15:25:40 +01001262 * An initial FFA_RUN is necessary for SP's secondary vCPUs to
1263 * reach the message wait loop.
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001264 */
Olivier Deprezb2808332023-02-02 15:25:40 +01001265 if (vcpu->rt_model == RTM_SP_INIT) {
1266 /*
1267 * TODO: this should be removed, but omitting it makes
1268 * normal world arch gicv3 tests failing.
1269 */
1270 vcpu->rt_model = RTM_NONE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001271
1272 vcpu_was_init_state = true;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001273 break;
1274 }
1275
J-Alves6e2abc62021-12-02 14:58:56 +00001276 assert(need_vm_lock == true);
J-Alves478faac2024-10-23 10:35:57 +01001277 if (!vm_locked.vm->el0_partition) {
1278 plat_ffa_inject_notification_pending_interrupt(
1279 vcpu_next_locked, current_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001280 }
1281
J-Alves478faac2024-10-23 10:35:57 +01001282 /* Provide reference to the return value. */
1283 ffa_run_ret = &ffa_run_abi;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001284
J-Alves478faac2024-10-23 10:35:57 +01001285 break;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001286 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +00001287 if (need_vm_lock &&
1288 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001289 vcpu_next_locked, current_locked, vm_locked)) {
1290 assert(vcpu_interrupt_count_get(vcpu_next_locked) > 0);
J-Alves6e2abc62021-12-02 14:58:56 +00001291 break;
1292 }
1293
Andrew Scullb06d1752019-02-04 10:15:48 +00001294 /* Allow virtual interrupts to be delivered. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001295 if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +00001296 break;
1297 }
1298
Andrew Walbran508e63c2018-12-20 17:02:37 +00001299 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001300 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001301 arch_timer_remaining_ns(&vcpu->regs);
1302
1303 /*
1304 * The timer expired so allow the interrupt to be
1305 * delivered.
1306 */
1307 if (timer_remaining_ns == 0) {
1308 break;
1309 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001310 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001311
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001312 /*
1313 * The vCPU is not ready to run, return the appropriate code to
1314 * the primary which called vcpu_run.
1315 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001316 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001317 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1318 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +00001319
1320 ret = false;
1321 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001322
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001323 case VCPU_STATE_BLOCKED:
1324 /* A blocked vCPU is run unconditionally. Fall through. */
1325 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +00001326 /* Check NPI is to be injected here. */
1327 if (need_vm_lock) {
1328 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001329 vcpu_next_locked, current_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001330 }
Andrew Walbran508e63c2018-12-20 17:02:37 +00001331 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001332 default:
1333 /*
1334 * Execution not expected to reach here. Deny the request
1335 * gracefully.
1336 */
1337 *run_ret = ffa_error(FFA_DENIED);
1338 ret = false;
1339 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001340 }
1341
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001342 plat_ffa_init_schedule_mode_ffa_run(current_locked, vcpu_next_locked);
Madhukar Pappireddy1480fce2022-06-21 18:09:25 -05001343
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001344 vcpu->cpu = current_locked.vcpu->cpu;
J-Alves478faac2024-10-23 10:35:57 +01001345
1346 vcpu_set_running(vcpu_next_locked, ffa_run_ret);
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001347
Olivier Deprez04168ed2022-09-26 09:20:00 +02001348 if (vcpu_was_init_state) {
1349 vcpu_set_phys_core_idx(vcpu);
1350 vcpu_set_boot_info_gp_reg(vcpu);
J-Alves7ac49052022-02-08 17:20:53 +00001351 }
J-Alves7ac49052022-02-08 17:20:53 +00001352
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001353 ret = true;
1354
1355out:
Andrew Scullb06d1752019-02-04 10:15:48 +00001356 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +01001357 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001358 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001359 return ret;
1360}
1361
J-Alves19e20cf2023-08-02 12:48:55 +01001362struct ffa_value api_ffa_run(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001363 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001364{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001365 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001366 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001367 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001368 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001369 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001370 struct vcpu_locked vcpu_next_locked;
1371 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001372
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001373 current_locked = vcpu_lock(current);
1374 if (!plat_ffa_run_checks(current_locked, vm_id, vcpu_idx, &ret, next)) {
1375 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001376 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001377
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001378 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001379 goto out;
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001380 }
1381
Andrew Scull19503262018-09-20 14:48:39 +01001382 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001383 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +01001384 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001385 goto out;
Andrew Scull19503262018-09-20 14:48:39 +01001386 }
1387
Fuad Tabbaed294af2019-12-20 10:43:01 +00001388 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001389 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001390 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001391 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001392
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001393 /*
1394 * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC
1395 * bypasses the intermediate execution contexts and resumes the
1396 * SP execution context that was originally preempted.
1397 */
1398 if (*next != NULL) {
1399 vcpu = *next;
1400 } else {
1401 vcpu = vm_get_vcpu(vm, vcpu_idx);
1402 }
1403
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001404 /*
1405 * Unlock current vCPU to allow it to be locked together with next
1406 * vcpu.
1407 */
1408 vcpu_unlock(&current_locked);
1409
1410 /* Lock both vCPUs at once to avoid deadlock. */
1411 vcpus_locked = vcpu_lock_both(current, vcpu);
1412 current_locked = vcpus_locked.vcpu1;
1413 vcpu_next_locked = vcpus_locked.vcpu2;
1414
1415 if (!plat_ffa_check_runtime_state_transition(
1416 current_locked, current->vm->id, HF_INVALID_VM_ID,
1417 vcpu_next_locked, FFA_RUN_32, &next_state)) {
1418 ret = ffa_error(FFA_DENIED);
1419 goto out_vcpu;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001420 }
1421
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001422 if (!api_vcpu_prepare_run(current_locked, vcpu_next_locked, &ret)) {
1423 goto out_vcpu;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001424 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001425
Andrew Walbran508e63c2018-12-20 17:02:37 +00001426 /*
1427 * Inject timer interrupt if timer has expired. It's safe to access
1428 * vcpu->regs here because api_vcpu_prepare_run already made sure that
1429 * regs_available was true (and then set it to false) before returning
1430 * true.
1431 */
1432 if (arch_timer_pending(&vcpu->regs)) {
1433 /* Make virtual timer interrupt pending. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001434 api_interrupt_inject_locked(vcpu_next_locked,
1435 HF_VIRTUAL_TIMER_INTID,
1436 vcpu_next_locked, NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001437
1438 /*
1439 * Set the mask bit so the hardware interrupt doesn't fire
1440 * again. Ideally we wouldn't do this because it affects what
1441 * the secondary vCPU sees, but if we don't then we end up with
1442 * a loop of the interrupt firing each time we try to return to
1443 * the secondary vCPU.
1444 */
1445 arch_timer_mask(&vcpu->regs);
1446 }
1447
Fuad Tabbaed294af2019-12-20 10:43:01 +00001448 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001449 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001450
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001451 assert(!vm_id_is_current_world(current->vm->id) ||
1452 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001453 current->state = VCPU_STATE_BLOCKED;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001454
Andrew Scull33fecd32019-01-08 14:48:27 +00001455 /*
1456 * Set a placeholder return code to the scheduler. This will be
1457 * overwritten when the switch back to the primary occurs.
1458 */
J-Alvesbd32c972024-02-02 16:20:04 +00001459 ret = api_ffa_interrupt_return(0);
Andrew Scull33fecd32019-01-08 14:48:27 +00001460
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001461out_vcpu:
1462 vcpu_unlock(&vcpu_next_locked);
1463
Andrew Scull6d2db332018-10-10 15:28:17 +01001464out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001465 vcpu_unlock(&current_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001466 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001467}
1468
1469/**
Andrew Scull81e85092018-12-12 12:56:20 +00001470 * Check that the mode indicates memory that is valid, owned and exclusive.
1471 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001472static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001473{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001474 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1475 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001476}
1477
1478/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001479 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001480 */
Karl Meakin738bee12024-01-12 15:19:45 +00001481static struct ffa_value api_vm_configure_stage1(
1482 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1483 paddr_t pa_send_begin, paddr_t pa_send_end, paddr_t pa_recv_begin,
1484 paddr_t pa_recv_end, uint32_t extra_attributes,
1485 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001486{
Karl Meakin738bee12024-01-12 15:19:45 +00001487 struct ffa_value ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001488
Karl Meakin738bee12024-01-12 15:19:45 +00001489 /*
1490 * Map the send page as read-only in the SPMC/hypervisor address space.
1491 */
Andrew Sculle1322792019-07-01 17:46:10 +01001492 vm_locked.vm->mailbox.send =
1493 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001494 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001495 if (!vm_locked.vm->mailbox.send) {
Karl Meakin738bee12024-01-12 15:19:45 +00001496 ret = ffa_error(FFA_NO_MEMORY);
1497 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001498 }
1499
1500 /*
Karl Meakin738bee12024-01-12 15:19:45 +00001501 * Map the receive page as writable in the SPMC/hypervisor address
1502 * space. On failure, unmap the send page before returning.
Andrew Sculle1322792019-07-01 17:46:10 +01001503 */
1504 vm_locked.vm->mailbox.recv =
1505 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001506 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001507 if (!vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001508 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001509 goto fail_undo_send;
1510 }
1511
Karl Meakin738bee12024-01-12 15:19:45 +00001512 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001513 goto out;
1514
1515 /*
1516 * The following mappings will not require more memory than is available
1517 * in the local pool.
1518 */
1519fail_undo_send:
1520 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001521 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1522 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001523
Andrew Sculle1322792019-07-01 17:46:10 +01001524out:
Andrew Sculle1322792019-07-01 17:46:10 +01001525 return ret;
1526}
1527
1528/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001529 * Sanity checks and configures the send and receive pages in the VM stage-2
1530 * and hypervisor stage-1 page tables.
1531 *
1532 * Returns:
1533 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001534 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001535 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1536 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001537 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001538 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001539 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001540
1541struct ffa_value api_vm_configure_pages(
1542 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1543 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1544 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001545{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001546 struct ffa_value ret;
1547 paddr_t pa_send_begin;
1548 paddr_t pa_send_end;
1549 paddr_t pa_recv_begin;
1550 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001551 uint32_t orig_send_mode = 0;
1552 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001553 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001554
1555 /* We only allow these to be setup once. */
1556 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001557 dlog_error("%s: Mailboxes have already been setup for VM %#x\n",
1558 __func__, vm_locked.vm->id);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001559 ret = ffa_error(FFA_DENIED);
1560 goto out;
1561 }
1562
1563 /* Hafnium only supports a fixed size of RX/TX buffers. */
1564 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
Karl Meakine8937d92024-03-19 16:04:25 +00001565 dlog_error("%s: Page count must be %zu, it is %d\n", __func__,
Karl Meakin738bee12024-01-12 15:19:45 +00001566 HF_MAILBOX_SIZE / FFA_PAGE_SIZE, page_count);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001567 ret = ffa_error(FFA_INVALID_PARAMETERS);
1568 goto out;
1569 }
1570
1571 /* Fail if addresses are not page-aligned. */
1572 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1573 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001574 dlog_error("%s: Mailbox buffers not page-aligned\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001575 ret = ffa_error(FFA_INVALID_PARAMETERS);
1576 goto out;
1577 }
1578
1579 /* Convert to physical addresses. */
1580 pa_send_begin = pa_from_ipa(send);
1581 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1582 pa_recv_begin = pa_from_ipa(recv);
1583 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1584
1585 /* Fail if the same page is used for the send and receive pages. */
1586 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001587 dlog_error("%s: Mailbox buffers overlap\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001588 ret = ffa_error(FFA_INVALID_PARAMETERS);
1589 goto out;
1590 }
Andrew Sculle1322792019-07-01 17:46:10 +01001591
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001592 /* Set stage 2 translation tables only for virtual FF-A instances. */
1593 if (vm_id_is_current_world(vm_locked.vm->id)) {
1594 /*
1595 * Ensure the pages are valid, owned and exclusive to the VM and
1596 * that the VM has the required access to the memory.
1597 */
1598 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1599 &orig_send_mode) ||
1600 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1601 (orig_send_mode & MM_MODE_R) == 0 ||
1602 (orig_send_mode & MM_MODE_W) == 0) {
1603 dlog_error(
1604 "VM doesn't have required access rights to map "
1605 "TX buffer in stage 2.\n");
1606 ret = ffa_error(FFA_INVALID_PARAMETERS);
1607 goto out;
1608 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001609
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001610 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1611 &orig_recv_mode) ||
1612 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1613 (orig_recv_mode & MM_MODE_R) == 0) {
1614 dlog_error(
1615 "VM doesn't have required access rights to map "
1616 "RX buffer in stage 2.\n");
1617 ret = ffa_error(FFA_INVALID_PARAMETERS);
1618 goto out;
1619 }
Andrew Sculle1322792019-07-01 17:46:10 +01001620
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001621 /* Take memory ownership away from the VM and mark as shared. */
1622 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1623 MM_MODE_W;
1624 if (vm_locked.vm->el0_partition) {
1625 mode |= MM_MODE_USER | MM_MODE_NG;
1626 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001627
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001628 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1629 mode, local_page_pool, NULL)) {
1630 dlog_error(
1631 "Cannot allocate a new entry in stage 2 "
1632 "translation table.\n");
1633 ret = ffa_error(FFA_NO_MEMORY);
1634 goto out;
1635 }
Andrew Sculle1322792019-07-01 17:46:10 +01001636
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001637 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1638 if (vm_locked.vm->el0_partition) {
1639 mode |= MM_MODE_USER | MM_MODE_NG;
1640 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001641
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001642 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1643 mode, local_page_pool, NULL)) {
1644 /* TODO: partial defrag of failed range. */
1645 /* Recover any memory consumed in failed mapping. */
Karl Meakin738bee12024-01-12 15:19:45 +00001646 dlog_error("%s: cannot map recv page\n", __func__);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001647 vm_ptable_defrag(vm_locked, local_page_pool);
Karl Meakin738bee12024-01-12 15:19:45 +00001648 ret = ffa_error(FFA_NO_MEMORY);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001649 goto fail_undo_send;
1650 }
Karl Meakin738bee12024-01-12 15:19:45 +00001651 } else {
1652 ret = arch_other_world_vm_configure_rxtx_map(
1653 vm_locked, local_page_pool, pa_send_begin, pa_send_end,
1654 pa_recv_begin, pa_recv_end);
1655 if (ret.func != FFA_SUCCESS_32) {
1656 goto out;
1657 }
Andrew Sculle1322792019-07-01 17:46:10 +01001658 }
1659
Olivier Deprez96a2a262020-06-11 17:21:38 +02001660 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1661 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1662
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001663 /*
1664 * For EL0 partitions, since both the partition and the hypervisor code
1665 * use the EL2&0 translation regime, it is critical to mark the mappings
1666 * of the send and recv buffers as non-global in the TLB. For one, if we
1667 * dont mark it as non-global, it would cause TLB conflicts since there
1668 * would be an identity mapping with non-global attribute in the
1669 * partitions page tables, but another identity mapping in the
1670 * hypervisor page tables with the global attribute. The other issue is
1671 * one of security, we dont want other partitions to be able to access
1672 * other partitions buffers through cached translations.
1673 */
1674 if (vm_locked.vm->el0_partition) {
1675 extra_attributes |= MM_MODE_NG;
1676 }
1677
Karl Meakin738bee12024-01-12 15:19:45 +00001678 ret = api_vm_configure_stage1(
1679 mm_stage1_locked, vm_locked, pa_send_begin, pa_send_end,
1680 pa_recv_begin, pa_recv_end, extra_attributes, local_page_pool);
1681 if (ret.func != FFA_SUCCESS_32) {
Andrew Sculle1322792019-07-01 17:46:10 +01001682 goto fail_undo_send_and_recv;
1683 }
1684
Manish Pandeyd34f8892020-06-19 17:41:07 +01001685 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001686 goto out;
1687
Andrew Sculle1322792019-07-01 17:46:10 +01001688fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001689 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001690 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001691
1692fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001693 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001694 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001695
1696out:
Andrew Sculle1322792019-07-01 17:46:10 +01001697 return ret;
1698}
1699
Karl Meakinb9705e22024-04-05 13:58:28 +01001700/**
1701 * Read the buffer addresses and VM ID of an FFA_RXTX_MAP request. Handles
1702 * forwarded messages by reading from the hypervisor's TX buffer.
1703 *
1704 * Returns the VM/SP ID on success.
1705 *
1706 * Returns `HF_INVALID_VM_ID` when the arguments provided via the ABI
1707 * FFA_RXTX_MAP indicate the SPMC should retrieve the RXTX description from the
1708 * Hypervisor RXTX buffers, and the hypervisor hasn't given its own RXTX buffers
1709 * for the SPMC to map.
1710 */
1711static ffa_id_t api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send,
1712 ipaddr_t *recv, uint32_t *page_count)
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001713{
Karl Meakinb9705e22024-04-05 13:58:28 +01001714 bool forwarded;
1715 struct vm_locked vm_locked;
1716 ffa_id_t owner_vm_id;
1717 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc;
1718 struct ffa_composite_memory_region *rx_region;
1719 struct ffa_composite_memory_region *tx_region;
1720
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001721 /*
1722 * If the message has been forwarded the effective addresses are in
1723 * hypervisor's TX buffer.
1724 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001725 forwarded = (current_vm->id == HF_OTHER_WORLD_ID) &&
1726 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1727 (*page_count == 0);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001728
1729 if (forwarded) {
Karl Meakinb9705e22024-04-05 13:58:28 +01001730 vm_locked = vm_lock(current_vm);
1731 endpoint_desc = (struct ffa_endpoint_rx_tx_descriptor *)
1732 vm_locked.vm->mailbox.send;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001733
Karl Meakinb9705e22024-04-05 13:58:28 +01001734 if (endpoint_desc == NULL) {
1735 dlog_error(
1736 "Trying to access RXTX description, but "
1737 "hypervisor has not provided RXTX buffers\n");
1738 vm_unlock(&vm_locked);
1739 return HF_INVALID_VM_ID;
1740 }
1741
1742 rx_region = ffa_endpoint_get_rx_memory_region(endpoint_desc);
1743 tx_region = ffa_endpoint_get_tx_memory_region(endpoint_desc);
1744
1745 owner_vm_id = endpoint_desc->endpoint_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001746 *recv = ipa_init(rx_region->constituents[0].address);
1747 *send = ipa_init(tx_region->constituents[0].address);
1748 *page_count = rx_region->constituents[0].page_count;
J-Alves28ad9b42022-12-08 12:19:43 +00001749
1750 vm_unlock(&vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001751 } else {
Karl Meakinb9705e22024-04-05 13:58:28 +01001752 owner_vm_id = current_vm->id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001753 }
Karl Meakinb9705e22024-04-05 13:58:28 +01001754
1755 return owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001756}
Karl Meakinb9705e22024-04-05 13:58:28 +01001757
Andrew Sculle1322792019-07-01 17:46:10 +01001758/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001759 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001760 * must not be shared. Locking of the page tables combined with a local memory
1761 * pool ensures there will always be enough memory to recover from any errors
1762 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1763 * by another core which could result in this transaction being unable to roll
1764 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001765 *
1766 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001767 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001768 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001769 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001770 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001771 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001772 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001773 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001774struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001775 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001776{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001777 struct ffa_value ret;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001778 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001779 struct mm_stage1_locked mm_stage1_locked;
1780 struct mpool local_page_pool;
J-Alves19e20cf2023-08-02 12:48:55 +01001781 ffa_id_t owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001782
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001783 /*
1784 * Get the original buffer addresses and VM ID in case of forwarded
1785 * message.
1786 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001787 owner_vm_id = api_get_rxtx_description(current->vm, &send, &recv,
1788 &page_count);
1789 if (owner_vm_id == HF_INVALID_VM_ID) {
1790 return ffa_error(FFA_INVALID_PARAMETERS);
1791 }
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001792
1793 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1794 if (owner_vm_locked.vm == NULL) {
1795 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1796 owner_vm_id);
1797 return ffa_error(FFA_DENIED);
1798 }
Andrew Scull220e6212018-12-21 18:09:00 +00001799
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001800 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001801 * Create a local pool so any freed memory can't be used by another
1802 * thread. This is to ensure the original mapping can be restored if any
1803 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001804 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001805 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1806
Manish Pandeyd34f8892020-06-19 17:41:07 +01001807 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001808
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001809 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1810 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001811 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001812 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001813 }
1814
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001815 /* Forward buffer mapping to SPMC if coming from a VM. */
1816 plat_ffa_rxtx_map_forward(owner_vm_locked);
1817
Federico Recanati9f1b6532022-04-14 13:15:28 +02001818 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001819
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001820exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001821 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001822 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001823 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001824
1825 return ret;
1826}
1827
1828/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001829 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1830 * translation regime of the caller. Unmap the region for the hypervisor and
1831 * set the memory region to owned and exclusive for the component. Since the
1832 * memory region mapped in the page table, when the buffers were originally
1833 * created we can safely remap it.
1834 *
1835 * Returns:
1836 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1837 * behalf of the caller.
1838 * - FFA_SUCCESS on success if no further action is needed.
1839 */
J-Alves19e20cf2023-08-02 12:48:55 +01001840struct ffa_value api_ffa_rxtx_unmap(ffa_id_t allocator_id, struct vcpu *current)
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001841{
1842 struct vm *vm = current->vm;
1843 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001844 ffa_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001845 struct mm_stage1_locked mm_stage1_locked;
1846 paddr_t send_pa_begin;
1847 paddr_t send_pa_end;
1848 paddr_t recv_pa_begin;
1849 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001850 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001851
J-Alves661e1b72023-08-02 13:39:40 +01001852 if (vm->id == HF_HYPERVISOR_VM_ID && !ffa_is_vm_id(allocator_id)) {
Olivier Deprez106c8ce2024-02-08 11:49:02 +01001853 dlog_verbose(
J-Alves9bd324a2023-01-12 10:52:52 +00001854 "The Hypervisor must specify a valid VM ID in register "
1855 "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n");
1856 return ffa_error(FFA_INVALID_PARAMETERS);
1857 }
1858
Federico Recanati8da9e332022-02-10 11:00:17 +01001859 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1860 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
J-Alves9bd324a2023-01-12 10:52:52 +00001861 dlog_error(
1862 "The register W1 (containing ID) must be 0 at virtual "
1863 "instances.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001864 return ffa_error(FFA_INVALID_PARAMETERS);
1865 }
1866
1867 /* VM ID of which buffers have to be unmapped. */
1868 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1869
1870 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1871 vm = vm_locked.vm;
1872 if (vm == NULL) {
1873 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1874 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001875 return ffa_error(FFA_INVALID_PARAMETERS);
1876 }
1877
1878 /* Get send and receive buffers. */
1879 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001880 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001881 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001882 ret = ffa_error(FFA_INVALID_PARAMETERS);
1883 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001884 }
1885
1886 /* Currently a mailbox size of 1 page is assumed. */
1887 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1888 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1889 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1890 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1891
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001892 mm_stage1_locked = mm_lock_stage1();
1893
Federico Recanati8da9e332022-02-10 11:00:17 +01001894 /* Reset stage 2 mapping only for virtual FF-A instances. */
1895 if (vm_id_is_current_world(owner_vm_id)) {
1896 /*
1897 * Set the memory region of the buffers back to the default mode
1898 * for the VM. Since this memory region was already mapped for
1899 * the RXTX buffers we can safely remap them.
1900 */
1901 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1902 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1903 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001904
Federico Recanati8da9e332022-02-10 11:00:17 +01001905 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1906 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1907 &api_page_pool, NULL));
Karl Meakin738bee12024-01-12 15:19:45 +00001908 } else {
1909 ret = arch_other_world_vm_configure_rxtx_unmap(
1910 vm_locked, &api_page_pool, send_pa_begin, send_pa_end,
1911 recv_pa_begin, recv_pa_end);
1912 if (ret.func != FFA_SUCCESS_32) {
1913 goto out;
1914 }
Federico Recanati8da9e332022-02-10 11:00:17 +01001915 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001916
1917 /* Unmap the buffers in the partition manager. */
1918 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1919 &api_page_pool));
1920 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1921 &api_page_pool));
1922
1923 vm->mailbox.send = NULL;
1924 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001925 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001926
Federico Recanati8da9e332022-02-10 11:00:17 +01001927 /* Forward buffer unmapping to SPMC if coming from a VM. */
J-Alves70079932022-12-07 17:32:20 +00001928 plat_ffa_rxtx_unmap_forward(vm_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001929
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001930 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001931
1932out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001933 vm_unlock(&vm_locked);
1934
Federico Recanati8da9e332022-02-10 11:00:17 +01001935 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001936}
1937
1938/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001939 * Copies data from the sender's send buffer to the recipient's receive buffer
1940 * and notifies the receiver.
1941 */
J-Alves19e20cf2023-08-02 12:48:55 +01001942struct ffa_value api_ffa_msg_send2(ffa_id_t sender_vm_id, uint32_t flags,
Federico Recanati25053ee2022-03-14 15:01:53 +01001943 struct vcpu *current)
1944{
1945 struct vm *from = current->vm;
1946 struct vm *to;
1947 struct vm_locked to_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001948 ffa_id_t msg_sender_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01001949 struct vm_locked sender_locked;
1950 const void *from_msg;
1951 struct ffa_value ret;
J-Alves19e20cf2023-08-02 12:48:55 +01001952 ffa_id_t sender_id;
1953 ffa_id_t receiver_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01001954 uint32_t msg_size;
Federico Recanati25053ee2022-03-14 15:01:53 +01001955
Karl Meakina5ea9092024-05-28 15:40:33 +01001956 alignas(8) struct ffa_partition_rxtx_header header;
1957
Federico Recanati25053ee2022-03-14 15:01:53 +01001958 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
1959 if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) {
1960 dlog_error("Sender VM ID must be zero.\n");
1961 return ffa_error(FFA_INVALID_PARAMETERS);
1962 }
1963
Federico Recanati25053ee2022-03-14 15:01:53 +01001964 /*
1965 * Get message sender's mailbox, which can be different to the `from` vm
1966 * when the message is forwarded.
1967 */
1968 msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id;
1969 sender_locked = plat_ffa_vm_find_locked(msg_sender_id);
1970 if (sender_locked.vm == NULL) {
1971 dlog_error("Cannot send message from VM ID %#x, not found.\n",
1972 msg_sender_id);
1973 return ffa_error(FFA_DENIED);
1974 }
1975
1976 from_msg = sender_locked.vm->mailbox.send;
1977 if (from_msg == NULL) {
1978 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
1979 msg_sender_id);
1980 ret = ffa_error(FFA_DENIED);
1981 goto out_unlock_sender;
1982 }
1983
1984 /*
1985 * Copy message header as safety measure to avoid multiple accesses to
1986 * unsafe memory which could be 'corrupted' between safety checks and
1987 * final buffer copy.
1988 */
J-Alves8a5fd9d2024-04-09 11:22:49 +01001989 if (!memcpy_trapped(&header, FFA_RXTX_HEADER_SIZE, from_msg,
1990 FFA_RXTX_HEADER_SIZE)) {
1991 dlog_error(
1992 "%s: Failed to copy message from sender's(%x) TX "
1993 "buffer.\n",
1994 __func__, sender_locked.vm->id);
1995 ret = ffa_error(FFA_ABORTED);
1996 goto out_unlock_sender;
1997 }
1998
Federico Recanati25053ee2022-03-14 15:01:53 +01001999 sender_id = ffa_rxtx_header_sender(&header);
2000 receiver_id = ffa_rxtx_header_receiver(&header);
2001
2002 /* Ensure Sender IDs from API and from message header match. */
2003 if (msg_sender_id != sender_id) {
2004 dlog_error(
2005 "Message sender VM ID (%#x) doesn't match header's VM "
2006 "ID (%#x).\n",
2007 msg_sender_id, sender_id);
2008 ret = ffa_error(FFA_INVALID_PARAMETERS);
2009 goto out_unlock_sender;
2010 }
2011
2012 /* Disallow reflexive requests as this suggests an error in the VM. */
2013 if (receiver_id == sender_id) {
2014 dlog_error("Sender and receive VM IDs must be different.\n");
2015 ret = ffa_error(FFA_INVALID_PARAMETERS);
2016 goto out_unlock_sender;
2017 }
2018
Federico Recanati7b39ff22022-03-14 15:01:53 +01002019 /* `flags` can be set only at secure virtual FF-A instances. */
J-Alves661e1b72023-08-02 13:39:40 +01002020 if (ffa_is_vm_id(sender_id) && (flags != 0)) {
Federico Recanati7b39ff22022-03-14 15:01:53 +01002021 dlog_error("flags must be zero.\n");
2022 return ffa_error(FFA_INVALID_PARAMETERS);
2023 }
2024
Olivier Deprez37b75112024-03-04 18:27:30 +01002025 if (header.offset != FFA_RXTX_HEADER_SIZE) {
2026 dlog_error("Indirect msg payload must follow the header.\n");
2027 return ffa_error(FFA_INVALID_PARAMETERS);
2028 }
2029
Federico Recanati25053ee2022-03-14 15:01:53 +01002030 /*
2031 * Check if the message has to be forwarded to the SPMC, in
2032 * this case return, the SPMC will handle the buffer copy.
2033 */
2034 if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) {
2035 goto out_unlock_sender;
2036 }
2037
2038 /* Ensure the receiver VM exists. */
2039 to_locked = plat_ffa_vm_find_locked(receiver_id);
2040 to = to_locked.vm;
2041
2042 if (to == NULL) {
2043 dlog_error("Cannot deliver message to VM %#x, not found.\n",
2044 receiver_id);
2045 ret = ffa_error(FFA_INVALID_PARAMETERS);
2046 goto out_unlock_sender;
2047 }
2048
2049 /*
2050 * Check sender and receiver can use indirect messages.
2051 * Sender is the VM/SP who originally sent the message, not the
2052 * hypervisor possibly relaying it.
2053 */
2054 if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) {
2055 dlog_verbose("VM %#x doesn't support indirect message\n",
2056 sender_id);
2057 ret = ffa_error(FFA_DENIED);
2058 goto out;
2059 }
2060
J-Alvese8c8c2b2022-12-16 15:34:48 +00002061 if (vm_is_mailbox_busy(to_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002062 dlog_error(
2063 "Cannot deliver message to VM %#x, RX buffer not "
2064 "ready.\n",
2065 receiver_id);
2066 ret = ffa_error(FFA_BUSY);
2067 goto out;
2068 }
2069
Federico Recanati644f0462022-03-17 12:04:00 +01002070 /* Acquire receiver's RX buffer. */
2071 if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) {
2072 dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id);
2073 goto out;
2074 }
2075
Federico Recanati25053ee2022-03-14 15:01:53 +01002076 /* Check the size of transfer. */
2077 msg_size = FFA_RXTX_HEADER_SIZE + header.size;
Olivier Deprezf1dd1e12024-03-04 18:21:01 +01002078 if ((msg_size > FFA_MSG_PAYLOAD_MAX) ||
Federico Recanati25053ee2022-03-14 15:01:53 +01002079 (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
2080 dlog_error("Message is too big.\n");
2081 ret = ffa_error(FFA_INVALID_PARAMETERS);
2082 goto out;
2083 }
2084
2085 /* Copy data. */
J-Alves8a5fd9d2024-04-09 11:22:49 +01002086 if (!memcpy_trapped(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg,
2087 msg_size)) {
2088 dlog_error(
2089 "%s: Failed to copy message to receiver's(%x) RX "
2090 "buffer.\n",
2091 __func__, to->id);
2092 ret = ffa_error(FFA_ABORTED);
2093 goto out;
2094 }
2095
Federico Recanati25053ee2022-03-14 15:01:53 +01002096 to->mailbox.recv_size = msg_size;
2097 to->mailbox.recv_sender = sender_id;
2098 to->mailbox.recv_func = FFA_MSG_SEND2_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002099 to->mailbox.state = MAILBOX_STATE_FULL;
Federico Recanati25053ee2022-03-14 15:01:53 +01002100
J-Alves3bb82592023-01-23 11:20:54 +00002101 /*
2102 * Set framework notifications, only if the SP has enabled
2103 * receipt of notifications.
2104 * If VMs have provided the RX buffer it is implied they already
2105 * support indirect messaging, and therefore framework notifications.
2106 */
2107 if (ffa_is_vm_id(to_locked.vm->id) ||
2108 vm_are_notifications_enabled(to_locked.vm)) {
2109 ffa_notifications_bitmap_t rx_buffer_full =
2110 ffa_is_vm_id(sender_id)
2111 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
2112 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
Federico Recanati25053ee2022-03-14 15:01:53 +01002113
J-Alves3bb82592023-01-23 11:20:54 +00002114 vm_notifications_framework_set_pending(to_locked,
2115 rx_buffer_full);
2116
2117 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2118 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2119 vcpu_index(current));
2120 plat_ffa_sri_trigger_not_delayed(current->cpu);
2121 } else {
J-Alvesea8ccfe2024-10-09 11:47:24 +01002122 plat_ffa_sri_set_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002123 }
Federico Recanati25053ee2022-03-14 15:01:53 +01002124 }
2125
2126 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2127
2128out:
2129 vm_unlock(&to_locked);
2130
2131out_unlock_sender:
2132 vm_unlock(&sender_locked);
2133
2134 return ret;
2135}
2136
2137/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002138 * Releases the caller's mailbox so that a new message can be received. The
2139 * caller must have copied out all data they wish to preserve as new messages
2140 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002141 *
2142 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002143 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2144 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002145 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2146 * - FFA_SUCCESS on success if no further action is needed.
2147 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002148 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002149 * hf_mailbox_waiter_get.
2150 */
J-Alves19e20cf2023-08-02 12:48:55 +01002151struct ffa_value api_ffa_rx_release(ffa_id_t receiver_id, struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002152{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002153 struct vm *current_vm = current->vm;
2154 struct vm *vm;
2155 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01002156 ffa_id_t current_vm_id = current_vm->id;
2157 ffa_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002158 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002159
Federico Recanati7bef0b92022-03-17 14:56:22 +01002160 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2161 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2162 dlog_error("Invalid `receiver_id`, must be zero.\n");
2163 return ffa_error(FFA_INVALID_PARAMETERS);
2164 }
2165
2166 /*
2167 * VM ID to be released: `receiver_id` if message has been forwarded by
2168 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2169 */
2170 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2171 release_vm_id = current_vm_id;
2172 } else {
2173 release_vm_id = receiver_id;
2174 }
2175
2176 vm_locked = plat_ffa_vm_find_locked(release_vm_id);
2177 vm = vm_locked.vm;
2178 if (vm == NULL) {
2179 dlog_error("No buffer registered for VM ID %#x.\n",
2180 release_vm_id);
2181 return ffa_error(FFA_INVALID_PARAMETERS);
2182 }
2183
J-Alvese8c8c2b2022-12-16 15:34:48 +00002184 if (plat_ffa_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002185 goto out;
2186 }
2187
Karl Meakin1064a9c2024-06-04 17:20:43 +01002188 ret = api_release_mailbox(vm_locked);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002189
2190out:
2191 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002192
2193 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002194}
Andrew Walbran318f5732018-11-20 16:23:42 +00002195
2196/**
Federico Recanati644f0462022-03-17 12:04:00 +01002197 * Acquire ownership of an RX buffer before writing to it. Both
2198 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2199 * could contend for the same buffer. SPMC owns VM's RX buffer after
2200 * it's mapped in its translation regime. This ABI should be
2201 * used by the Hypervisor to get the ownership of a VM's RX buffer
2202 * from the SPMC solving the aforementioned possible contention.
2203 *
2204 * Returns:
2205 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2206 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2207 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2208 */
J-Alves19e20cf2023-08-02 12:48:55 +01002209struct ffa_value api_ffa_rx_acquire(ffa_id_t receiver_id, struct vcpu *current)
Federico Recanati644f0462022-03-17 12:04:00 +01002210{
2211 struct vm_locked receiver_locked;
2212 struct vm *receiver;
2213 struct ffa_value ret;
2214
2215 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
J-Alves661e1b72023-08-02 13:39:40 +01002216 !ffa_is_vm_id(receiver_id)) {
Federico Recanati644f0462022-03-17 12:04:00 +01002217 dlog_error(
2218 "FFA_RX_ACQUIRE not supported at this FF-A "
2219 "instance.\n");
2220 return ffa_error(FFA_NOT_SUPPORTED);
2221 }
2222
2223 receiver_locked = plat_ffa_vm_find_locked(receiver_id);
2224 receiver = receiver_locked.vm;
2225
2226 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2227 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2228 receiver_id);
2229 ret = ffa_error(FFA_INVALID_PARAMETERS);
2230 goto out;
2231 }
2232
2233 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2234 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2235 ret = ffa_error(FFA_DENIED);
2236 goto out;
2237 }
2238
J-Alvese8c8c2b2022-12-16 15:34:48 +00002239 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002240
2241 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2242
2243out:
2244 vm_unlock(&receiver_locked);
2245
2246 return ret;
2247}
2248
J-Alvescc542042024-07-24 19:13:22 +01002249/*
2250 * Returns true if intid relates with either of those:
2251 * - NPI
2252 * - ME
2253 * - Virtual Timer.
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002254 * - IPI
J-Alvescc542042024-07-24 19:13:22 +01002255 *
2256 * These are VIs with no expected interrupt descriptor.
2257 */
2258static bool api_is_maintenance_virtual_interrupt(uint32_t intid)
2259{
2260 return intid == HF_NOTIFICATION_PENDING_INTID ||
2261 intid == HF_MANAGED_EXIT_INTID ||
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002262 intid == HF_VIRTUAL_TIMER_INTID || intid == HF_IPI_INTID;
J-Alvescc542042024-07-24 19:13:22 +01002263}
2264
Federico Recanati644f0462022-03-17 12:04:00 +01002265/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002266 * Enables or disables a given interrupt ID for the calling vCPU.
2267 *
2268 * Returns 0 on success, or -1 if the intid is invalid.
2269 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002270int64_t api_interrupt_enable(uint32_t intid, bool enable,
2271 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002272{
Manish Pandey35e452f2021-02-18 21:36:34 +00002273 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002274 struct interrupts *interrupts = &current->interrupts;
J-Alvescc542042024-07-24 19:13:22 +01002275 struct interrupt_descriptor *int_desc = NULL;
2276 struct vm *vm = current->vm;
2277 struct vm_locked vm_locked;
2278
2279 int64_t ret = -1;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002280
Andrew Walbran318f5732018-11-20 16:23:42 +00002281 if (intid >= HF_NUM_INTIDS) {
2282 return -1;
2283 }
2284
J-Alvescc542042024-07-24 19:13:22 +01002285 vm_locked = vm_lock(vm);
Manish Pandey35e452f2021-02-18 21:36:34 +00002286 current_locked = vcpu_lock(current);
J-Alvescc542042024-07-24 19:13:22 +01002287
2288 int_desc = vm_interrupt_set_enable(vm_locked, intid, enable);
2289
2290 if (!api_is_maintenance_virtual_interrupt(intid)) {
2291 if (int_desc == NULL) {
2292 dlog_error("%s: invalid interrupt ID.\n", __func__);
2293 goto out;
2294 }
2295
2296 plat_interrupts_configure_interrupt(*int_desc);
2297 }
2298
Andrew Walbran318f5732018-11-20 16:23:42 +00002299 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002300 /*
2301 * If it is pending and was not enabled before, increment the
2302 * count.
2303 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002304 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2305 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2306 vcpu_interrupt_count_increment(current_locked,
2307 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002308 }
J-Alvescc542042024-07-24 19:13:22 +01002309
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002310 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2311 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002312 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002313 /*
2314 * If it is pending and was enabled before, decrement the count.
2315 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002316 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2317 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2318 vcpu_interrupt_count_decrement(current_locked,
2319 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002320 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002321 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2322 vcpu_virt_interrupt_set_type(interrupts, intid,
2323 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002324 }
2325
J-Alvescc542042024-07-24 19:13:22 +01002326 ret = 0;
2327
2328out:
2329 vm_unlock(&vm_locked);
Manish Pandey35e452f2021-02-18 21:36:34 +00002330 vcpu_unlock(&current_locked);
J-Alvescc542042024-07-24 19:13:22 +01002331
2332 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002333}
2334
2335/**
2336 * Returns the ID of the next pending interrupt for the calling vCPU, and
2337 * acknowledges it (i.e. marks it as no longer pending). Returns
2338 * HF_INVALID_INTID if there are no pending interrupts.
2339 */
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002340uint32_t api_interrupt_get(struct vcpu_locked current_locked)
Andrew Walbran318f5732018-11-20 16:23:42 +00002341{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002342 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002343 uint32_t first_interrupt = HF_INVALID_INTID;
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002344 struct interrupts *interrupts = &current_locked.vcpu->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002345
2346 /*
2347 * Find the first enabled and pending interrupt ID, return it, and
2348 * deactivate it.
2349 */
Andrew Walbran318f5732018-11-20 16:23:42 +00002350 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2351 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002352 interrupts->interrupt_enabled.bitmap[i] &
2353 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002354
Andrew Walbran318f5732018-11-20 16:23:42 +00002355 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002356 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002357
2358 first_interrupt =
2359 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002360
Andrew Walbran3d84a262018-12-13 14:41:19 +00002361 /*
2362 * Mark it as no longer pending and decrement the count.
2363 */
J-Alvesb8730e92024-08-07 18:28:55 +01002364 vcpu_interrupt_clear_decrement(current_locked,
2365 first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002366 break;
2367 }
2368 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002369
Andrew Walbran318f5732018-11-20 16:23:42 +00002370 return first_interrupt;
2371}
2372
2373/**
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002374 * Negotiate the FF-A version to be used for this FF-A instance.
2375 * See section 13.2 of the FF-A v1.2 ALP1 spec.
2376 *
2377 * Returns Hafnium's version number (`FFA_VERSION_COMPILED`) on success.
2378 * Returns `FFA_NOT_SUPPORTED` on error:
2379 * - The version is invalid (highest bit set).
2380 * - The requested version is incompatible.
2381 * - The version has already been negotiated and cannot be changed.
2382 */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002383struct ffa_value api_ffa_version(struct vcpu *current,
Karl Meakin0e617d92024-04-05 12:55:22 +01002384 enum ffa_version requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002385{
Karl Meakin0e617d92024-04-05 12:55:22 +01002386 static_assert(sizeof(enum ffa_version) == 4,
2387 "enum ffa_version must be 4 bytes wide");
2388
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002389 const struct ffa_value error = {.func = (uint32_t)FFA_NOT_SUPPORTED};
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002390 struct vm_locked current_vm_locked;
2391
Karl Meakin0e617d92024-04-05 12:55:22 +01002392 if (!ffa_version_is_valid(requested_version)) {
2393 dlog_error(
2394 "FFA_VERSION: requested version %#x is invalid "
2395 "(highest bit must be zero)\n",
2396 requested_version);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002397 return error;
Andrew Walbran9fd29072020-04-22 12:12:14 +01002398 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002399
Karl Meakin0e617d92024-04-05 12:55:22 +01002400 if (!ffa_versions_are_compatible(requested_version,
2401 FFA_VERSION_COMPILED)) {
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002402 dlog_error(
2403 "FFA_VERSION: requested version v%u.%u is not "
2404 "compatible with v%u.%u\n",
2405 ffa_version_get_major(requested_version),
2406 ffa_version_get_minor(requested_version),
2407 ffa_version_get_major(FFA_VERSION_COMPILED),
2408 ffa_version_get_minor(FFA_VERSION_COMPILED));
2409 return error;
J-Alves3cc9d6f2023-07-20 16:46:15 +01002410 }
2411
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002412 current_vm_locked = vm_lock(current->vm);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002413
2414 if (current_vm_locked.vm->ffa_version_negotiated &&
2415 requested_version != current_vm_locked.vm->ffa_version) {
2416 vm_unlock(&current_vm_locked);
2417 dlog_error(
2418 "FFA_VERSION: Cannot change FF-A version after other "
2419 "FF-A calls have been made\n");
2420 return error;
2421 }
2422
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002423 current_vm_locked.vm->ffa_version = requested_version;
2424 vm_unlock(&current_vm_locked);
2425
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002426 return (struct ffa_value){.func = (uint32_t)FFA_VERSION_COMPILED};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002427}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002428
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002429/**
Karl Meakin49ec1e42024-05-10 13:08:24 +01002430 * Helper for success return of FFA_FEATURES.
J-Alves6f72ca82021-11-01 12:34:58 +00002431 */
2432struct ffa_value api_ffa_feature_success(uint32_t arg2)
2433{
2434 return (struct ffa_value){
Karl Meakin49ec1e42024-05-10 13:08:24 +01002435 .func = FFA_SUCCESS_32,
2436 .arg1 = 0U,
2437 .arg2 = arg2,
2438 };
J-Alves6f72ca82021-11-01 12:34:58 +00002439}
2440
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002441static struct ffa_value ffa_features_function(uint32_t func,
2442 uint32_t input_property,
2443 struct vcpu *current)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002444{
Karl Meakin0e617d92024-04-05 12:55:22 +01002445 const enum ffa_version ffa_version = current->vm->ffa_version;
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002446 const bool el0_partition = current->vm->el0_partition;
2447
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002448 switch (func) {
J-Alves6f72ca82021-11-01 12:34:58 +00002449 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002450 case FFA_ERROR_32:
2451 case FFA_SUCCESS_32:
2452 case FFA_INTERRUPT_32:
2453 case FFA_VERSION_32:
2454 case FFA_FEATURES_32:
2455 case FFA_RX_RELEASE_32:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002456 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002457 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002458 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002459 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002460 case FFA_RUN_32:
J-Alves95fbb312024-03-20 15:19:16 +00002461 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002462 case FFA_MEM_DONATE_32:
2463 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00002464 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002465 case FFA_MEM_SHARE_32:
J-Alves95fbb312024-03-20 15:19:16 +00002466 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002467 case FFA_MEM_RETRIEVE_RESP_32:
2468 case FFA_MEM_RELINQUISH_32:
2469 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002470 case FFA_MEM_FRAG_RX_32:
2471 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002472 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002473 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002474 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002475 case FFA_MSG_SEND_DIRECT_REQ_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002476
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002477 /* FF-A v1.1 features. */
2478 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002479 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2480 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2481 case FFA_NOTIFICATION_BIND_32:
2482 case FFA_NOTIFICATION_UNBIND_32:
2483 case FFA_NOTIFICATION_SET_32:
2484 case FFA_NOTIFICATION_GET_32:
2485 case FFA_NOTIFICATION_INFO_GET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002486 case FFA_MSG_SEND2_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002487 if (FFA_VERSION_1_1 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002488 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002489 }
2490
Kathleen Capellae4fe2962023-09-01 17:08:47 -04002491 /* FF-A v1.2 features. */
2492 case FFA_CONSOLE_LOG_32:
2493 case FFA_CONSOLE_LOG_64:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002494 case FFA_PARTITION_INFO_GET_REGS_64:
Kathleen Capella41fea932023-06-23 17:39:28 -04002495 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002496 case FFA_MSG_SEND_DIRECT_RESP2_64:
Karl Meakin0e617d92024-04-05 12:55:22 +01002497 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002498 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002499 }
2500
Karl Meakin49ec1e42024-05-10 13:08:24 +01002501 return api_ffa_feature_success(0);
2502
Karl Meakin5a222642024-06-20 10:23:37 +01002503 /* These functions are only supported on S-EL0 partitions. */
2504 case FFA_MEM_PERM_GET_32:
2505 case FFA_MEM_PERM_SET_32:
2506 case FFA_MEM_PERM_GET_64:
2507 case FFA_MEM_PERM_SET_64:
2508 if (!(vm_id_is_current_world(current->vm->id) &&
2509 el0_partition)) {
2510 dlog_verbose(
2511 "FFA_FEATURE: %s is only supported on S-EL0 "
2512 "partitions\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002513 ffa_func_name(func));
Karl Meakin5a222642024-06-20 10:23:37 +01002514 return ffa_error(FFA_NOT_SUPPORTED);
2515 }
2516 return api_ffa_feature_success(0);
2517
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002518 case FFA_SECONDARY_EP_REGISTER_64:
2519 if (FFA_VERSION_COMPILED < FFA_VERSION_1_1) {
2520 return ffa_error(FFA_NOT_SUPPORTED);
2521 }
2522
2523 if (!(vm_id_is_current_world(current->vm->id) &&
2524 current->vm->vcpu_count > 1)) {
2525 dlog_verbose(
2526 "FFA_FEATURE: %s is only supported on SPs with "
2527 "more than 1 vCPU\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002528 ffa_func_name(func));
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002529 return ffa_error(FFA_NOT_SUPPORTED);
2530 }
2531 return api_ffa_feature_success(0);
2532
Karl Meakin650cb142024-06-20 10:31:57 +01002533 /*
2534 * This function is restricted to the secure virtual FF-A instance (i.e.
2535 * only report success to SPs).
2536 */
2537 case FFA_YIELD_32:
2538 if (!vm_id_is_current_world(current->vm->id)) {
2539 dlog_verbose(
2540 "FFA_FEATURES: %s is only supported at secure "
2541 "virtual FF-A instance\n",
2542 ffa_func_name(FFA_YIELD_32));
2543 return ffa_error(FFA_NOT_SUPPORTED);
2544 }
2545 return api_ffa_feature_success(0);
2546
Karl Meakin0fd67292024-02-06 17:33:05 +00002547 case FFA_RXTX_MAP_64: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002548 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002549 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002550 }
2551
Karl Meakin0fd67292024-02-06 17:33:05 +00002552 uint32_t arg2 = 0;
2553 struct ffa_features_rxtx_map_params params = {
2554 .min_buf_size = FFA_RXTX_MAP_MIN_BUF_4K,
2555 .mbz = 0,
2556 .max_buf_size =
Karl Meakin0e617d92024-04-05 12:55:22 +01002557 (ffa_version >= FFA_VERSION_1_2)
Karl Meakin0fd67292024-02-06 17:33:05 +00002558 ? FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT
2559 : 0,
2560 };
2561
2562 memcpy_s(&arg2, sizeof(arg2), &params, sizeof(params));
Karl Meakin49ec1e42024-05-10 13:08:24 +01002563 return api_ffa_feature_success(arg2);
Karl Meakin0fd67292024-02-06 17:33:05 +00002564 }
2565
J-Alves95fbb312024-03-20 15:19:16 +00002566 case FFA_MEM_RETRIEVE_REQ_64:
Karl Meakin49ec1e42024-05-10 13:08:24 +01002567 case FFA_MEM_RETRIEVE_REQ_32: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002568 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002569 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002570 }
2571
Karl Meakin49ec1e42024-05-10 13:08:24 +01002572 if (ANY_BITS_SET(input_property,
2573 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2574 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT) ||
2575 IS_BIT_SET(input_property,
2576 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT)) {
2577 dlog_warning(
2578 "FFA_FEATURES: Bits[%u:%u] and Bit[%u] of "
2579 "input_property should be 0 (input_property = "
2580 "%#x)\n",
2581 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2582 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT,
2583 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT,
Karl Meakin34b8ae92023-01-13 13:33:07 +00002584 input_property);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002585 }
2586
Karl Meakin0e617d92024-04-05 12:55:22 +01002587 if (ffa_version >= FFA_VERSION_1_1 &&
2588 (input_property &
2589 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
Karl Meakin4e8e4792024-07-05 16:28:42 +01002590 dlog_verbose(
2591 "FFA_FEATURES: NS bit support must be 1\n");
Karl Meakin6fd6c1d2024-05-13 12:09:13 +01002592 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002593 }
2594
2595 return api_ffa_feature_success(
2596 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
Karl Meakin49ec1e42024-05-10 13:08:24 +01002597 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT |
Karl Meakin34b8ae92023-01-13 13:33:07 +00002598 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
Karl Meakin49ec1e42024-05-10 13:08:24 +01002599 }
J-Alves6f72ca82021-11-01 12:34:58 +00002600
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002601 default:
2602 return ffa_error(FFA_NOT_SUPPORTED);
2603 }
2604}
2605
2606static struct ffa_value ffa_features_feature(enum ffa_feature_id feature,
2607 uint32_t input_property,
2608 struct vcpu *current)
2609{
2610 const bool el0_partition = current->vm->el0_partition;
2611
2612 if (ANY_BITS_SET(feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2613 FFA_FEATURES_FEATURE_MBZ_LO_BIT)) {
2614 dlog_verbose(
2615 "FFA_FEATURES: feature ID %#x is invalid (bits [%u:%u] "
2616 "must be zero)\n",
2617 feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2618 FFA_FEATURES_FEATURE_MBZ_LO_BIT);
2619 return ffa_error(FFA_NOT_SUPPORTED);
2620 }
2621 if (input_property != 0) {
2622 dlog_verbose(
2623 "FFA_FEATURES: input_property must be 0 "
2624 "(input_property = %#x)\n",
2625 input_property);
2626 return ffa_error(FFA_NOT_SUPPORTED);
2627 }
2628
2629 switch (feature) {
J-Alves6f72ca82021-11-01 12:34:58 +00002630 /* Check support of a feature provided respective feature ID. */
Karl Meakin17c9ad32024-04-12 16:41:00 +01002631
2632 /*
2633 * For NPI and MEI, report the IDs as supported only to partitions at
2634 * the virtual FF-A instances.
2635 */
J-Alves6f72ca82021-11-01 12:34:58 +00002636 case FFA_FEATURE_NPI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002637 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002638 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002639 }
2640
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002641 if (el0_partition) {
2642 return ffa_error(FFA_NOT_SUPPORTED);
2643 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002644 if (!vm_id_is_current_world(current->vm->id)) {
2645 return ffa_error(FFA_NOT_SUPPORTED);
2646 }
J-Alves6f72ca82021-11-01 12:34:58 +00002647 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
Karl Meakin17c9ad32024-04-12 16:41:00 +01002648
2649 case FFA_FEATURE_MEI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002650 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002651 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002652 }
J-Alvesc4d9ae82024-05-14 10:15:03 +01002653 if (el0_partition) {
2654 return ffa_error(FFA_NOT_SUPPORTED);
2655 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002656 if (!vm_id_is_current_world(current->vm->id)) {
2657 return ffa_error(FFA_NOT_SUPPORTED);
2658 }
2659 return api_ffa_feature_success(HF_MANAGED_EXIT_INTID);
2660
J-Alves6f72ca82021-11-01 12:34:58 +00002661 case FFA_FEATURE_SRI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002662 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002663 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002664 }
2665
Karl Meakincb6642c2024-02-27 14:43:45 +00002666 if (!ffa_is_vm_id(current->vm->id)) {
2667 return ffa_error(FFA_NOT_SUPPORTED);
2668 }
J-Alves6f72ca82021-11-01 12:34:58 +00002669 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
Karl Meakin0e617d92024-04-05 12:55:22 +01002670
J-Alves6f72ca82021-11-01 12:34:58 +00002671 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002672 default:
Karl Meakin4271ff92024-05-13 12:33:15 +01002673 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002674 }
2675}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002676
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002677/**
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002678 * Discovery function returning information about the implementation of optional
2679 * FF-A interfaces. See section 13.3 of the FF-A v1.2 ALP1 spec.
2680 *
2681 * `function_or_feature_id` is interpreted as either a function ID or a feature
2682 * ID, depending on the value of bit 31.
2683 * When it is a feature ID, bits [30:8] MBZ and input_property MBZ.
2684 *
2685 * Returns `FFA_SUCCESS` if the interface is supported.
2686 * Returns `FFA_NOT_SUPPORTED` if the interface is not supported or the
2687 * parameters are invalid.
2688 */
2689struct ffa_value api_ffa_features(uint32_t function_or_feature_id,
2690 uint32_t input_property, struct vcpu *current)
2691{
2692 return IS_BIT_UNSET(function_or_feature_id, FFA_FEATURES_FEATURE_BIT)
2693 ? ffa_features_feature(function_or_feature_id,
2694 input_property, current)
2695 : ffa_features_function(function_or_feature_id,
2696 input_property, current);
2697}
2698
2699/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002700 * FF-A specification states that x2/w2 Must Be Zero for FFA_MSG_SEND_DIRECT_REQ
2701 * and FFA_MSG_SEND_DIRECT_RESP interfaces when used for partition messages. See
2702 * FF-A v1.2 Table 16.6: FFA_MSG_SEND_DIRECT_REQ function syntax.
J-Alves645eabe2021-02-22 16:08:27 +00002703 */
2704static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2705{
2706 return args.arg2 == 0U;
2707}
2708
2709/**
J-Alves76d99af2021-03-10 17:42:11 +00002710 * Limits size of arguments in ffa_value structure to 32-bit.
2711 */
2712static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2713{
2714 return (struct ffa_value){
2715 .func = (uint32_t)args.func,
2716 .arg1 = (uint32_t)args.arg1,
2717 .arg2 = (uint32_t)0,
2718 .arg3 = (uint32_t)args.arg3,
2719 .arg4 = (uint32_t)args.arg4,
2720 .arg5 = (uint32_t)args.arg5,
2721 .arg6 = (uint32_t)args.arg6,
2722 .arg7 = (uint32_t)args.arg7,
2723 };
2724}
2725
2726/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002727 * Helper to copy direct message payload, depending on SMC used, direct
2728 * messaging interface used, and expected registers size.
J-Alves76d99af2021-03-10 17:42:11 +00002729 */
2730static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2731{
2732 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2733 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2734 return api_ffa_value_copy32(args);
2735 }
2736
Kathleen Capella41fea932023-06-23 17:39:28 -04002737 if (args.func == FFA_MSG_SEND_DIRECT_REQ_64 ||
2738 args.func == FFA_MSG_SEND_DIRECT_RESP_64) {
2739 args.arg2 = 0;
2740 }
2741
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002742 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64) {
2743 args.extended_val.valid = true;
2744 }
2745
Kathleen Capella087e5022023-09-07 18:04:15 -04002746 if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64) {
2747 args.arg2 = 0;
2748 args.arg3 = 0;
2749 }
2750
Kathleen Capella41fea932023-06-23 17:39:28 -04002751 return args;
2752}
2753
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002754/**
2755 * Return a pointer to the first UUID in `uuids` that is equal to
2756 * `target_uuid`. If `target_uuid` is 0-0-0-0, it matches any UUID.
2757 */
2758static struct ffa_uuid *ffa_uuid_find(struct ffa_uuid *uuids,
2759 size_t uuids_count,
2760 struct ffa_uuid target_uuid)
2761{
2762 if (ffa_uuid_is_null(&target_uuid)) {
2763 return &uuids[0];
2764 }
2765
2766 for (size_t i = 0; i < uuids_count; i++) {
2767 if (ffa_uuid_is_null(&uuids[i])) {
2768 break;
2769 }
2770 if (ffa_uuid_equal(&target_uuid, &uuids[i])) {
2771 return &uuids[i];
2772 }
2773 }
2774 return NULL;
2775}
2776
Kathleen Capella41fea932023-06-23 17:39:28 -04002777static bool api_ffa_dir_msg_req2_is_uuid_valid(struct vm *receiver_vm,
2778 struct ffa_value args)
2779{
2780 struct ffa_uuid target_uuid;
2781
Karl Meakin9478e322024-09-23 17:47:09 +01002782 ffa_uuid_from_u64x2(args.arg2, args.arg3, &target_uuid);
Kathleen Capella41fea932023-06-23 17:39:28 -04002783
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002784 return ffa_uuid_find(receiver_vm->uuids, PARTITION_MAX_UUIDS,
2785 target_uuid) != NULL;
J-Alves76d99af2021-03-10 17:42:11 +00002786}
2787
2788/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002789 * Send an FF-A direct message request.
Kathleen Capella41fea932023-06-23 17:39:28 -04002790 * This handler covers both FFA_MSG_SEND_DIRECT_REQ_32/64
2791 * and FFA_MSG_SEND_DIRECT_REQ2_64 (introduced in FF-A v1.2) with function-based
2792 * checks to accomodate for the difference between the ABIs.
2793 *
2794 * FFA_MSG_SEND_DIRECT_REQ2_64 works mostly the same as
2795 * FFA_MSG_SEND_DIRECT_REQ_32/64, but adds the ability to send a direct message
2796 * request to a specified UUID within a partition and the usage of an extended
2797 * range of registers (x4-x17, instead of x4-x7) to be used as part of the
2798 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002799 */
J-Alves19e20cf2023-08-02 12:48:55 +01002800struct ffa_value api_ffa_msg_send_direct_req(ffa_id_t sender_vm_id,
2801 ffa_id_t receiver_vm_id,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002802 struct ffa_value args,
2803 struct vcpu *current,
2804 struct vcpu **next)
2805{
J-Alves17228f72021-04-20 17:13:19 +01002806 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002807 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002808 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002809 struct vcpu *receiver_vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002810 struct vcpu_locked current_locked;
2811 struct vcpu_locked receiver_vcpu_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002812 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002813 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002814
Kathleen Capella41fea932023-06-23 17:39:28 -04002815 if ((args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2816 args.func == FFA_MSG_SEND_DIRECT_REQ_64) &&
2817 !api_ffa_dir_msg_is_arg2_zero(args)) {
J-Alves645eabe2021-02-22 16:08:27 +00002818 return ffa_error(FFA_INVALID_PARAMETERS);
2819 }
2820
Olivier Deprez55a189e2021-06-09 15:45:27 +02002821 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2822 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002823 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002824 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002825 }
2826
Olivier Deprez55a189e2021-06-09 15:45:27 +02002827 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002828 return ret;
2829 }
2830
J-Alvesbd32c972024-02-02 16:20:04 +00002831 ret = api_ffa_interrupt_return(0);
J-Alves17228f72021-04-20 17:13:19 +01002832
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002833 receiver_vm = vm_find(receiver_vm_id);
2834 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002835 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002836 return ffa_error(FFA_INVALID_PARAMETERS);
2837 }
2838
Kathleen Capella41fea932023-06-23 17:39:28 -04002839 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64 &&
2840 !api_ffa_dir_msg_req2_is_uuid_valid(receiver_vm, args)) {
2841 dlog_verbose("UUID unrecognized for this VM\n");
2842 return ffa_error(FFA_INVALID_PARAMETERS);
2843 }
2844
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002845 /*
J-Alves439ac972021-11-18 17:32:03 +00002846 * Check if sender supports sending direct message req, and if
2847 * receiver supports receipt of direct message requests.
2848 */
Kathleen Capella41fea932023-06-23 17:39:28 -04002849 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm,
2850 args.func)) {
J-Alves439ac972021-11-18 17:32:03 +00002851 return ffa_error(FFA_DENIED);
2852 }
2853
2854 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002855 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002856 * UP (migratable) or MP partitions with a number of vCPUs matching the
2857 * number of PEs in the system. It further states that MP partitions
2858 * accepting direct request messages cannot migrate.
2859 */
J-Alvesad6a0432021-04-09 16:06:21 +01002860 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002861 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002862 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002863 return ffa_error(FFA_INVALID_PARAMETERS);
2864 }
2865
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002866 /*
2867 * If VM must be locked, it must be done before any of its vCPUs are
2868 * locked.
2869 */
J-Alves6e2abc62021-12-02 14:58:56 +00002870 receiver_locked = vm_lock(receiver_vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002871
2872 /* Lock both vCPUs at once to avoid deadlock. */
2873 vcpus_locked = vcpu_lock_both(current, receiver_vcpu);
2874 current_locked = vcpus_locked.vcpu1;
2875 receiver_vcpu_locked = vcpus_locked.vcpu2;
2876
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002877 /*
2878 * If destination vCPU is executing or already received an
2879 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2880 * busy. There is a brief period of time where the vCPU state has
2881 * changed but regs_available is still false thus consider this case as
2882 * the vCPU not yet ready to receive a direct message request.
2883 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002884 if (is_ffa_direct_msg_request_ongoing(receiver_vcpu_locked) ||
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002885 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2886 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002887 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002888 ret = ffa_error(FFA_BUSY);
2889 goto out;
2890 }
2891
J-Alves7ea3f7e2024-03-27 11:05:11 +00002892 if (!plat_ffa_check_runtime_state_transition(
2893 current_locked, sender_vm_id, HF_INVALID_VM_ID,
2894 receiver_vcpu_locked, args.func, &next_state)) {
2895 ret = ffa_error(FFA_DENIED);
2896 goto out;
2897 }
2898
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002899 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2900 memory_order_relaxed)) {
2901 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04002902 dlog_verbose(
2903 "Receiver VM %#x aborted, cannot run vCPU %u\n",
2904 receiver_vcpu->vm->id,
2905 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002906 receiver_vcpu->state = VCPU_STATE_ABORTED;
2907 }
2908
2909 ret = ffa_error(FFA_ABORTED);
2910 goto out;
2911 }
2912
2913 switch (receiver_vcpu->state) {
2914 case VCPU_STATE_OFF:
2915 case VCPU_STATE_RUNNING:
2916 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002917 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002918 case VCPU_STATE_BLOCKED:
2919 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002920 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2921 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002922 ret = ffa_error(FFA_BUSY);
2923 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002924 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002925 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002926 * We expect target vCPU to be in WAITING state after either
2927 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002928 */
2929 break;
2930 }
2931
2932 /* Inject timer interrupt if any pending */
2933 if (arch_timer_pending(&receiver_vcpu->regs)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002934 api_interrupt_inject_locked(receiver_vcpu_locked,
2935 HF_VIRTUAL_TIMER_INTID,
2936 current_locked, NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002937
2938 arch_timer_mask(&receiver_vcpu->regs);
2939 }
2940
2941 /* The receiver vCPU runs upon direct message invocation */
2942 receiver_vcpu->cpu = current->cpu;
2943 receiver_vcpu->state = VCPU_STATE_RUNNING;
2944 receiver_vcpu->regs_available = false;
Kathleen Capellae468c112023-12-13 17:56:28 -05002945 receiver_vcpu->direct_request_origin.is_ffa_req2 =
2946 (args.func == FFA_MSG_SEND_DIRECT_REQ2_64);
2947 receiver_vcpu->direct_request_origin.vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002948
J-Alves76d99af2021-03-10 17:42:11 +00002949 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002950
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002951 assert(!vm_id_is_current_world(current->vm->id) ||
2952 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002953 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002954
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07002955 plat_ffa_wind_call_chain_ffa_direct_req(
2956 current_locked, receiver_vcpu_locked, sender_vm_id);
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05002957
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002958 /* Switch to receiver vCPU targeted to by direct msg request */
2959 *next = receiver_vcpu;
2960
J-Alves6e2abc62021-12-02 14:58:56 +00002961 if (!receiver_locked.vm->el0_partition) {
2962 /*
2963 * If the scheduler in the system is giving CPU cycles to the
2964 * receiver, due to pending notifications, inject the NPI
2965 * interrupt. Following call assumes that '*next' has been set
2966 * to receiver_vcpu.
2967 */
2968 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002969 receiver_vcpu_locked, current_locked, receiver_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00002970 }
2971
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002972 /*
2973 * Since this flow will lead to a VM switch, the return value will not
2974 * be applied to current vCPU.
2975 */
2976
2977out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002978 vcpu_unlock(&receiver_vcpu_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00002979 vm_unlock(&receiver_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002980 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002981
2982 return ret;
2983}
2984
2985/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002986 * Resume the target vCPU after the current vCPU sent a direct response.
2987 * Current vCPU moves to waiting state.
2988 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002989void api_ffa_resume_direct_resp_target(struct vcpu_locked current_locked,
2990 struct vcpu **next,
J-Alves19e20cf2023-08-02 12:48:55 +01002991 ffa_id_t receiver_vm_id,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002992 struct ffa_value to_ret,
2993 bool is_nwd_call_chain)
2994{
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07002995 if (plat_ffa_is_spmd_lp_id(receiver_vm_id) ||
2996 !vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002997 *next = api_switch_to_other_world(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002998 VCPU_STATE_WAITING);
2999
3000 /* End of NWd scheduled call chain. */
3001 assert(!is_nwd_call_chain ||
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003002 (current_locked.vcpu->call_chain.prev_node == NULL));
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003003 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003004 *next = api_switch_to_primary(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003005 VCPU_STATE_WAITING);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003006 } else if (vm_id_is_current_world(receiver_vm_id)) {
3007 /*
3008 * It is expected the receiver_vm_id to be from an SP, otherwise
3009 * 'plat_ffa_is_direct_response_valid' should have
3010 * made function return error before getting to this point.
3011 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003012 *next = api_switch_to_vm(current_locked, to_ret,
3013 VCPU_STATE_WAITING, receiver_vm_id);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003014 } else {
3015 panic("Invalid direct message response invocation");
3016 }
3017}
3018
3019/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003020 * Send an FF-A direct message response.
Kathleen Capella087e5022023-09-07 18:04:15 -04003021 * This handler covers both FFA_MSG_SEND_DIRECT_RESP_32/64
3022 * and FFA_MSG_SEND_DIRECT_RESP2_64 (introduced in FF-A v1.2) with
3023 * function-based checks to accomodate for the difference between the ABIs.
3024 *
3025 * FFA_MSG_SEND_DIRECT_RESP2_64 is used to respond to requests sent via
3026 * FFA_MSG_SEND_DIRECT_REQ2_64 and adds the usage of an extended range
3027 * of registers (x4-x17, instead of x4-x7) to be used as part of the
3028 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003029 */
J-Alves19e20cf2023-08-02 12:48:55 +01003030struct ffa_value api_ffa_msg_send_direct_resp(ffa_id_t sender_vm_id,
3031 ffa_id_t receiver_vm_id,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003032 struct ffa_value args,
3033 struct vcpu *current,
3034 struct vcpu **next)
3035{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003036 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003037 struct vcpu_locked next_locked = (struct vcpu_locked){
3038 .vcpu = NULL,
3039 };
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003040 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003041 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003042 struct ffa_value signal_interrupt =
3043 (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003044 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
3045 struct two_vcpu_locked vcpus_locked;
Kathleen Capellae468c112023-12-13 17:56:28 -05003046 bool received_req2;
J-Alves645eabe2021-02-22 16:08:27 +00003047
Kathleen Capella35238872024-01-12 15:05:52 -05003048 /*
3049 * If using FFA_MSG_SEND_DIRECT_RESP, the caller's
3050 * - x2 MBZ for partition messages
3051 * - x8-x17 SBZ if caller's FF-A version >= FF-A v1.2
3052 */
3053 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) {
3054 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
3055 return ffa_error(FFA_INVALID_PARAMETERS);
3056 }
3057
Karl Meakin0e617d92024-04-05 12:55:22 +01003058 if (current->vm->ffa_version >= FFA_VERSION_1_2 &&
Kathleen Capella35238872024-01-12 15:05:52 -05003059 !api_extended_args_are_zero(&args)) {
3060 return ffa_error(FFA_INVALID_PARAMETERS);
3061 }
J-Alves645eabe2021-02-22 16:08:27 +00003062 }
3063
Olivier Deprez55a189e2021-06-09 15:45:27 +02003064 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
3065 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00003066 dlog_verbose("Invalid direct response call.\n");
J-Alvesaa336102021-03-01 13:02:45 +00003067 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003068 }
3069
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003070 current_locked = vcpu_lock(current);
3071
3072 if (!plat_ffa_check_runtime_state_transition(
3073 current_locked, sender_vm_id, receiver_vm_id, next_locked,
3074 args.func, &next_state)) {
3075 ret = ffa_error(FFA_DENIED);
3076 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05003077 }
3078
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003079 assert(!vm_id_is_current_world(current->vm->id) ||
3080 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003081
3082 /*
3083 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
3084 * defined originator.
3085 */
3086 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
3087 /*
3088 * Sending direct response but direct request origin
3089 * vCPU is not set.
3090 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003091 ret = ffa_error(FFA_DENIED);
3092 goto out;
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003093 }
3094
Kathleen Capellae468c112023-12-13 17:56:28 -05003095 received_req2 = current->direct_request_origin.is_ffa_req2;
3096
3097 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64 && received_req2) {
3098 dlog_verbose(
3099 "%s: FFA_MSG_SEND_DIRECT_RESP must be used with "
3100 "FFA_MSG_SEND_DIRECT_REQ.\n",
3101 __func__);
3102 ret = ffa_error(FFA_DENIED);
3103 goto out;
3104 } else if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64 &&
3105 !received_req2) {
3106 dlog_verbose(
3107 "%s: FFA_MSG_SEND_DIRECT_RESP2 must be used with "
3108 "FFA_MSG_SEND_DIRECT_REQ2.\n",
3109 __func__);
3110 ret = ffa_error(FFA_DENIED);
3111 goto out;
3112 }
3113
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003114 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003115 struct interrupts *interrupts = &current->interrupts;
3116
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003117 /*
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003118 * Per FF-A v1.1 EAC0 section 8.3.1.2.1 rule 6, SPMC can signal
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003119 * a secure interrupt to a SP that is performing managed exit.
3120 * We have taken a implementation defined choice to not allow
3121 * Managed exit while a SP is processing a secure interrupt.
3122 */
Madhukar Pappireddy7945bb52024-08-20 15:22:41 -05003123 CHECK(current->scheduling_mode != SPMC_MODE);
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003124
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06003125 plat_interrupts_set_priority_mask(current->priority_mask);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003126 /*
3127 * A SP may be signaled a managed exit but actually not trap
3128 * the virtual interrupt, probably because it has virtual
3129 * interrupts masked, and emit direct resp. In this case the
3130 * managed exit operation is considered completed and it would
3131 * also need to clear the pending managed exit flag for the SP
3132 * vCPU.
3133 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003134 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003135
3136 if (vcpu_is_virt_interrupt_pending(interrupts,
3137 HF_MANAGED_EXIT_INTID)) {
J-Alvesb8730e92024-08-07 18:28:55 +01003138 vcpu_interrupt_clear_decrement(current_locked,
3139 HF_MANAGED_EXIT_INTID);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003140 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003141 }
3142
Kathleen Capellae468c112023-12-13 17:56:28 -05003143 /* Clear direct request origin vm_id and request type for the caller. */
3144 current->direct_request_origin.is_ffa_req2 = false;
3145 current->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003146
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003147 api_ffa_resume_direct_resp_target(current_locked, next, receiver_vm_id,
3148 to_ret, false);
3149
3150 /*
3151 * Unlock current vCPU to allow it to be locked together with next
3152 * vcpu.
3153 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003154 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003155
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003156 /* Lock both vCPUs at once to avoid deadlock. */
3157 vcpus_locked = vcpu_lock_both(current, *next);
3158 current_locked = vcpus_locked.vcpu1;
3159 next_locked = vcpus_locked.vcpu2;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003160
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003161 plat_ffa_unwind_call_chain_ffa_direct_resp(current_locked, next_locked);
J-Alvesbd32c972024-02-02 16:20:04 +00003162
3163 /*
3164 * Check if there is a pending secure interrupt.
3165 * If there is, return back to the caller with FFA_INTERRUPT,
3166 * and set the `next` vcpu in a preempted state.
3167 */
3168 if (plat_ffa_intercept_call(current_locked, next_locked,
3169 &signal_interrupt)) {
3170 ret = signal_interrupt;
3171 *next = NULL;
3172 }
3173
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003174 vcpu_unlock(&next_locked);
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05003175
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003176out:
3177 vcpu_unlock(&current_locked);
3178 return ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003179}
3180
J-Alves84658fc2021-06-17 14:37:32 +01003181static bool api_memory_region_check_flags(
3182 struct ffa_memory_region *memory_region, uint32_t share_func)
3183{
3184 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00003185 case FFA_MEM_SHARE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003186 case FFA_MEM_SHARE_32:
3187 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3188 0U) {
3189 return false;
3190 }
3191 /* Intentional fall-through */
J-Alves95fbb312024-03-20 15:19:16 +00003192 case FFA_MEM_LEND_64:
J-Alves84658fc2021-06-17 14:37:32 +01003193 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00003194 case FFA_MEM_DONATE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003195 case FFA_MEM_DONATE_32: {
3196 /* Bits 31:2 Must Be Zero. */
3197 ffa_memory_receiver_flags_t to_mask =
3198 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
3199 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
3200
3201 if ((memory_region->flags & to_mask) != 0U) {
3202 return false;
3203 }
3204 break;
3205 }
3206 default:
3207 panic("Check for mem send calls only.\n");
3208 }
3209
3210 /* Last check reserved values are 0 */
3211 return true;
3212}
3213
J-Alves33c47bf2022-09-29 11:36:20 +01003214/*
3215 * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0.
3216 */
3217static void api_ffa_memory_region_v1_1_from_v1_0(
3218 struct ffa_memory_region_v1_0 *memory_region_v1_0,
3219 struct ffa_memory_region *memory_region_v1_1)
3220{
3221 memory_region_v1_1->sender = memory_region_v1_0->sender;
J-Alves00847062022-10-03 17:08:44 +01003222 memory_region_v1_1->handle = memory_region_v1_0->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003223 memory_region_v1_1->attributes =
3224 ffa_memory_attributes_extend(memory_region_v1_0->attributes);
J-Alves33c47bf2022-09-29 11:36:20 +01003225 memory_region_v1_1->flags = memory_region_v1_0->flags;
3226 memory_region_v1_1->tag = memory_region_v1_0->tag;
3227 memory_region_v1_1->memory_access_desc_size =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003228 sizeof(struct ffa_memory_access_v1_0);
J-Alves33c47bf2022-09-29 11:36:20 +01003229 memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003230 memory_region_v1_1->receivers_offset = sizeof(struct ffa_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003231
3232 /* Zero reserved fields. */
3233 for (uint32_t i = 0; i < 3U; i++) {
3234 memory_region_v1_1->reserved[i] = 0U;
3235 }
3236}
3237
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003238/**
3239 * Updates a v1.0 transaction descriptor to v1.1. This gives us the
3240 * memory_access_desc_size field we need for forwards compatability.
3241 * Copy the receivers and composite descriptors to the new struct.
3242 * We also check the fields in the v1.0 transaction descriptor and return:
3243 * - FFA_ERROR FFA_INVALID_PARAMETERS: If any of the fields are not valid
3244 * values, eg the reserved fields are not 0, receiver_count is too large or
3245 * composite offsets are not 0 for retrieve requests or in bounds for send
3246 * requests.
3247 * - FFA ERROR FFA_NOT_SUPPORTED: If an invalid ffa_version is supplied to the
3248 * function. Or the fragment length is more than a single page.
3249 * - FFA_ERROR FFA_NO_MEMORY: If we do not have enough memory for a scratch
3250 * memory transaction descriptor.
3251 * - FFA_SUCCESS: If a successful update has occured.
J-Alves33c47bf2022-09-29 11:36:20 +01003252 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003253static struct ffa_value api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3254 void *allocated, uint32_t *fragment_length, uint32_t *total_length,
Karl Meakin0e617d92024-04-05 12:55:22 +01003255 enum ffa_version ffa_version, bool send_transaction)
J-Alves33c47bf2022-09-29 11:36:20 +01003256{
3257 struct ffa_memory_region_v1_0 *memory_region_v1_0;
3258 struct ffa_memory_region *memory_region_v1_1 = NULL;
3259 struct ffa_composite_memory_region *composite_v1_0;
3260 struct ffa_composite_memory_region *composite_v1_1;
3261 size_t receivers_length;
3262 size_t space_left;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003263 size_t receivers_end;
J-Alves33c47bf2022-09-29 11:36:20 +01003264 size_t composite_offset_v1_1;
3265 size_t composite_offset_v1_0;
3266 size_t fragment_constituents_size;
3267 size_t fragment_length_v1_1;
3268
J-Alves33c47bf2022-09-29 11:36:20 +01003269 assert(fragment_length != NULL);
3270 assert(total_length != NULL);
3271
Karl Meakin0e617d92024-04-05 12:55:22 +01003272 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves33c47bf2022-09-29 11:36:20 +01003273 return (struct ffa_value){.func = FFA_SUCCESS_32};
3274 }
3275
Karl Meakin0e617d92024-04-05 12:55:22 +01003276 if (ffa_version != FFA_VERSION_1_0) {
J-Alves33c47bf2022-09-29 11:36:20 +01003277 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
3278 ffa_version);
3279 return ffa_error(FFA_NOT_SUPPORTED);
3280 }
3281
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003282 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003283 "Updating memory transaction descriptor from v1.0 to v1.1.\n");
J-Alves33c47bf2022-09-29 11:36:20 +01003284
3285 memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated;
3286
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003287 receivers_length = sizeof(struct ffa_memory_access_v1_0) *
J-Alves33c47bf2022-09-29 11:36:20 +01003288 memory_region_v1_0->receiver_count;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003289 receivers_end = sizeof(struct ffa_memory_region) + receivers_length;
J-Alves33c47bf2022-09-29 11:36:20 +01003290
3291 /*
3292 * Check the specified composite offset of v1.0 descriptor, and that all
3293 * receivers were configured with the same offset.
3294 */
3295 composite_offset_v1_0 =
3296 memory_region_v1_0->receivers[0].composite_memory_region_offset;
3297
J-Alves33c47bf2022-09-29 11:36:20 +01003298 /* Determine the composite offset for v1.1 descriptor. */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003299 if (send_transaction) {
3300 fragment_constituents_size =
3301 *fragment_length - composite_offset_v1_0 -
3302 sizeof(struct ffa_composite_memory_region);
3303 fragment_length_v1_1 =
3304 receivers_end +
3305 sizeof(struct ffa_composite_memory_region) +
3306 fragment_constituents_size;
3307 composite_offset_v1_1 = receivers_end;
3308 } else {
3309 fragment_constituents_size = 0;
3310 fragment_length_v1_1 = receivers_end;
3311 composite_offset_v1_1 = 0;
3312 }
J-Alves33c47bf2022-09-29 11:36:20 +01003313
3314 /*
3315 * Currently only support the simpler cases: memory transaction
3316 * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE.
3317 * TODO: allocate the entries needed for this fragment_length_v1_1.
3318 * - Check corner when v1.1 descriptor converted size surpasses
3319 * the size of the entry.
3320 */
3321 if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) {
3322 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003323 "Translation of FF-A v1.0 descriptors for over %lu is "
J-Alves33c47bf2022-09-29 11:36:20 +01003324 "unsupported.",
3325 MM_PPOOL_ENTRY_SIZE);
3326 return ffa_error(FFA_NOT_SUPPORTED);
3327 }
3328
3329 space_left = fragment_length_v1_1;
3330
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003331 /*
3332 * Allocate a page of memory to construct the v1.1 memory descriptor.
3333 * Earlier we checked that the fragment_length_v1_1 would not be larger
3334 * than a page.
3335 */
J-Alves33c47bf2022-09-29 11:36:20 +01003336 memory_region_v1_1 =
3337 (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
3338 if (memory_region_v1_1 == NULL) {
3339 return ffa_error(FFA_NO_MEMORY);
3340 }
3341
3342 /* Translate header from v1.0 to v1.1. */
3343 api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0,
3344 memory_region_v1_1);
3345
3346 space_left -= sizeof(struct ffa_memory_region);
3347
3348 /* Copy memory access information. */
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003349 memcpy_s((uint8_t *)memory_region_v1_1 +
3350 memory_region_v1_1->receivers_offset,
3351 space_left, memory_region_v1_0->receivers, receivers_length);
J-Alves33c47bf2022-09-29 11:36:20 +01003352
3353 /* Initialize the memory access descriptors with composite offset. */
3354 for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) {
3355 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003356 ffa_memory_region_get_receiver(memory_region_v1_1, i);
3357 assert(receiver != NULL);
J-Alves33c47bf2022-09-29 11:36:20 +01003358 receiver->composite_memory_region_offset =
3359 composite_offset_v1_1;
3360 }
3361
3362 space_left -= receivers_length;
3363
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003364 /* Composite memory descriptors to copy. */
3365 if (send_transaction) {
3366 /* Init v1.1 composite. */
3367 composite_v1_1 = (struct ffa_composite_memory_region
3368 *)((uint8_t *)memory_region_v1_1 +
3369 composite_offset_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003370
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003371 composite_v1_0 = ffa_memory_region_get_composite_v1_0(
3372 memory_region_v1_0, 0);
3373 composite_v1_1->constituent_count =
3374 composite_v1_0->constituent_count;
3375 composite_v1_1->page_count = composite_v1_0->page_count;
J-Alves33c47bf2022-09-29 11:36:20 +01003376
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003377 space_left -= sizeof(struct ffa_composite_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003378
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003379 /* Initialize v1.1 constituents. */
3380 memcpy_s(composite_v1_1->constituents, space_left,
3381 composite_v1_0->constituents,
3382 fragment_constituents_size);
J-Alves33c47bf2022-09-29 11:36:20 +01003383
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003384 space_left -= fragment_constituents_size;
3385 }
3386
J-Alves33c47bf2022-09-29 11:36:20 +01003387 assert(space_left == 0U);
3388
J-Alves33c47bf2022-09-29 11:36:20 +01003389 /*
3390 * Remove the v1.0 fragment size, and resultant size of v1.1 fragment.
3391 */
3392 *total_length = *total_length - *fragment_length + fragment_length_v1_1;
3393 *fragment_length = fragment_length_v1_1;
3394
3395 /*
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003396 * After successfully updating to v1.1 copy the descriptor to the
3397 * internal buffer given as a parameter (used to prevent TOCTOU attacks)
3398 * and free the scratch memory used to construct it.
J-Alves33c47bf2022-09-29 11:36:20 +01003399 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003400 memcpy_s(allocated, MM_PPOOL_ENTRY_SIZE, memory_region_v1_1,
3401 *fragment_length);
3402 mpool_free(&api_page_pool, memory_region_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003403
3404 return (struct ffa_value){.func = FFA_SUCCESS_32};
3405}
3406
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003407struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
3408 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003409 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003410{
3411 struct vm *from = current->vm;
3412 struct vm *to;
3413 const void *from_msg;
J-Alves33c47bf2022-09-29 11:36:20 +01003414 void *allocated_entry;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003415 struct ffa_memory_region *memory_region = NULL;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003416 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01003417 bool targets_other_world = false;
Karl Meakin0e617d92024-04-05 12:55:22 +01003418 enum ffa_version ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003419
3420 if (ipa_addr(address) != 0 || page_count != 0) {
3421 /*
3422 * Hafnium only supports passing the descriptor in the TX
3423 * mailbox.
3424 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003425 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003426 }
3427
Andrew Walbranca808b12020-05-15 17:22:28 +01003428 if (fragment_length > length) {
3429 dlog_verbose(
3430 "Fragment length %d greater than total length %d.\n",
3431 fragment_length, length);
3432 return ffa_error(FFA_INVALID_PARAMETERS);
3433 }
J-Alves33c47bf2022-09-29 11:36:20 +01003434
3435 if (fragment_length > HF_MAILBOX_SIZE ||
3436 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003437 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003438 }
3439
3440 /*
3441 * Check that the sender has configured its send buffer. If the TX
3442 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3443 * be safely accessed after releasing the lock since the TX mailbox
3444 * address can only be configured once.
3445 */
3446 sl_lock(&from->lock);
3447 from_msg = from->mailbox.send;
J-Alves33c47bf2022-09-29 11:36:20 +01003448 ffa_version = from->ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003449 sl_unlock(&from->lock);
3450
3451 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003452 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003453 }
3454
3455 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003456 * Copy the memory region descriptor to a fresh page from the memory
3457 * pool. This prevents the sender from changing it underneath us, and
3458 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003459 */
J-Alves33c47bf2022-09-29 11:36:20 +01003460 allocated_entry = mpool_alloc(&api_page_pool);
3461 if (allocated_entry == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003462 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003463 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003464 }
J-Alves33c47bf2022-09-29 11:36:20 +01003465
J-Alves8f2150d2024-03-28 16:15:56 +00003466 if (!memcpy_trapped(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg,
3467 fragment_length)) {
3468 dlog_error(
3469 "%s: Failed to copy FF-A memory region descriptor.\n",
3470 __func__);
3471 return ffa_error(FFA_ABORTED);
3472 }
J-Alves33c47bf2022-09-29 11:36:20 +01003473
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003474 if (!ffa_memory_region_sanity_check(allocated_entry, ffa_version,
3475 fragment_length, true)) {
3476 ret = ffa_error(FFA_INVALID_PARAMETERS);
3477 goto out;
3478 }
3479
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003480 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3481 allocated_entry, &fragment_length, &length, ffa_version, true);
J-Alves33c47bf2022-09-29 11:36:20 +01003482 if (ret.func != FFA_SUCCESS_32) {
3483 goto out;
3484 }
3485
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003486 memory_region = allocated_entry;
3487
J-Alves33c47bf2022-09-29 11:36:20 +01003488 if (fragment_length < sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003489 memory_region->memory_access_desc_size) {
J-Alves33c47bf2022-09-29 11:36:20 +01003490 dlog_verbose(
3491 "Initial fragment length %d smaller than header size "
Karl Meakine8937d92024-03-19 16:04:25 +00003492 "%lu.\n",
J-Alves33c47bf2022-09-29 11:36:20 +01003493 fragment_length,
3494 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003495 memory_region->memory_access_desc_size);
3496 ret = ffa_error(FFA_INVALID_PARAMETERS);
3497 goto out;
J-Alves33c47bf2022-09-29 11:36:20 +01003498 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003499
J-Alves84658fc2021-06-17 14:37:32 +01003500 if (!api_memory_region_check_flags(memory_region, share_func)) {
3501 dlog_verbose(
3502 "Memory region reserved arguments must be zero.\n");
3503 ret = ffa_error(FFA_INVALID_PARAMETERS);
3504 goto out;
3505 }
3506
J-Alves363f5722022-04-25 17:37:37 +01003507 if (memory_region->receiver_count == 0U) {
3508 dlog_verbose("Receiver count can't be 0.\n");
3509 ret = ffa_error(FFA_INVALID_PARAMETERS);
3510 goto out;
3511 }
3512
J-Alves95fbb312024-03-20 15:19:16 +00003513 if ((share_func == FFA_MEM_DONATE_32 ||
3514 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01003515 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003516 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003517 "FFA_MEM_DONATE only supports one recipient. Specified "
3518 "%u\n",
J-Alves363f5722022-04-25 17:37:37 +01003519 memory_region->receiver_count);
3520 ret = ffa_error(FFA_INVALID_PARAMETERS);
3521 goto out;
3522 }
3523
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003524 /*
3525 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01003526 * If there is a receiver from the other world, track it for later
3527 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003528 */
J-Alves363f5722022-04-25 17:37:37 +01003529 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003530 struct ffa_memory_access *receiver =
3531 ffa_memory_region_get_receiver(memory_region, i);
J-Alvesc9227c82024-04-24 21:00:58 +01003532 ffa_id_t receiver_id;
3533
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003534 assert(receiver != NULL);
J-Alvesc9227c82024-04-24 21:00:58 +01003535
3536 receiver_id = receiver->receiver_permissions.receiver;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003537
J-Alves363f5722022-04-25 17:37:37 +01003538 to = vm_find(receiver_id);
3539
J-Alves7de61af2024-03-27 10:29:46 +00003540 if ((vm_id_is_current_world(receiver_id) && to == NULL) ||
3541 to == from) {
J-Alves363f5722022-04-25 17:37:37 +01003542 dlog_verbose("%s: invalid receiver.\n", __func__);
3543 ret = ffa_error(FFA_INVALID_PARAMETERS);
3544 goto out;
3545 }
3546
J-Alvesc9227c82024-04-24 21:00:58 +01003547 if (!plat_ffa_is_memory_send_valid(
3548 receiver_id, from->id, share_func,
3549 memory_region->receiver_count > 1)) {
J-Alves363f5722022-04-25 17:37:37 +01003550 ret = ffa_error(FFA_DENIED);
3551 goto out;
3552 }
3553
3554 /* Capture if any of the receivers is from the other world. */
3555 if (!targets_other_world) {
3556 targets_other_world =
3557 !vm_id_is_current_world(receiver_id);
3558 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003559 }
3560
J-Alves363f5722022-04-25 17:37:37 +01003561 if (targets_other_world) {
J-Alves66652252022-07-06 09:49:51 +01003562 ret = plat_ffa_other_world_mem_send(
3563 from, share_func, &memory_region, length,
3564 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003565 } else {
3566 struct vm_locked from_locked = vm_lock(from);
3567
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003568 ret = ffa_memory_send(from_locked, memory_region, length,
3569 fragment_length, share_func,
3570 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003571 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003572 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003573 * make sure we don't free it.
3574 */
3575 memory_region = NULL;
3576
3577 vm_unlock(&from_locked);
3578 }
3579
3580out:
3581 if (memory_region != NULL) {
3582 mpool_free(&api_page_pool, memory_region);
3583 }
3584
3585 return ret;
3586}
3587
J-Alvesc7270b72024-09-12 10:16:34 +01003588/**
3589 * An FFA_MEM_RETRIEVE_REQ from the hypervisor must specify the handle of the
3590 * memory transaction it is querying and all other fields must be 0.
3591 */
3592static bool api_ffa_memory_hypervisor_retrieve_request_validate(
3593 struct ffa_memory_region *request, enum ffa_version version)
3594{
3595 switch (version) {
3596 case FFA_VERSION_1_0: {
3597 struct ffa_memory_region_v1_0 *request_v1_0 =
3598 (struct ffa_memory_region_v1_0 *)request;
3599
3600 return request_v1_0->sender == 0U &&
3601 request_v1_0->attributes.shareability == 0U &&
3602 request_v1_0->attributes.cacheability == 0U &&
3603 request_v1_0->attributes.type == 0U &&
3604 request_v1_0->attributes.security == 0U &&
3605 request_v1_0->flags == 0U && request_v1_0->tag == 0U &&
3606 request_v1_0->receiver_count == 0U &&
3607 plat_ffa_memory_handle_allocated_by_current_world(
3608 request_v1_0->handle);
3609 }
3610 default:
3611 return request->sender == 0U &&
3612 request->attributes.shareability == 0U &&
3613 request->attributes.cacheability == 0U &&
3614 request->attributes.type == 0U &&
3615 request->attributes.security == 0U &&
3616 request->flags == 0U && request->tag == 0U &&
3617 request->memory_access_desc_size == 0U &&
3618 request->receiver_count == 0U &&
3619 request->receivers_offset == 0U &&
3620 plat_ffa_memory_handle_allocated_by_current_world(
3621 request->handle);
3622 }
3623}
3624
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003625struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3626 uint32_t fragment_length,
3627 ipaddr_t address, uint32_t page_count,
3628 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003629{
3630 struct vm *to = current->vm;
3631 struct vm_locked to_locked;
3632 const void *to_msg;
J-Alves00847062022-10-03 17:08:44 +01003633 void *retrieve_msg;
3634 struct ffa_memory_region *retrieve_request = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003635 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003636 struct ffa_value ret;
Karl Meakin0e617d92024-04-05 12:55:22 +01003637 enum ffa_version ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003638
3639 if (ipa_addr(address) != 0 || page_count != 0) {
3640 /*
3641 * Hafnium only supports passing the descriptor in the TX
3642 * mailbox.
3643 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003644 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003645 }
3646
Andrew Walbrana65a1322020-04-06 19:32:32 +01003647 if (fragment_length != length) {
J-Alves33c47bf2022-09-29 11:36:20 +01003648 dlog_verbose("Fragmentation not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003649 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003650 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003651
J-Alves00847062022-10-03 17:08:44 +01003652 retrieve_msg = cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003653 message_buffer_size = cpu_get_buffer_size(current->cpu);
3654 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3655 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003656 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003657 }
3658
3659 to_locked = vm_lock(to);
3660 to_msg = to->mailbox.send;
J-Alves00847062022-10-03 17:08:44 +01003661 ffa_version = to->ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003662
3663 if (to_msg == NULL) {
3664 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003665 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003666 goto out;
3667 }
3668
3669 /*
3670 * Copy the retrieve request descriptor to an internal buffer, so that
3671 * the caller can't change it underneath us.
3672 */
J-Alves8f2150d2024-03-28 16:15:56 +00003673 if (!memcpy_trapped(retrieve_msg, message_buffer_size, to_msg,
3674 length)) {
3675 dlog_error(
3676 "%s: Failed to copy FF-A retrieve request "
3677 "descriptor.\n",
3678 __func__);
3679 ret = ffa_error(FFA_ABORTED);
3680 goto out;
3681 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003682
J-Alvese8c8c2b2022-12-16 15:34:48 +00003683 if ((vm_is_mailbox_other_world_owned(to_locked) &&
3684 !plat_ffa_acquire_receiver_rx(to_locked, &ret)) ||
3685 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003686 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003687 * Can't retrieve memory information if the mailbox is
3688 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003689 */
J-Alves59ed0042022-07-28 18:26:41 +01003690 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003691 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003692 goto out;
3693 }
3694
Daniel Boulby296ee702023-11-28 13:36:55 +00003695 if (!is_ffa_hypervisor_retrieve_request(retrieve_msg)) {
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003696 if (!ffa_memory_region_sanity_check(retrieve_msg, ffa_version,
3697 fragment_length, false)) {
3698 ret = ffa_error(FFA_INVALID_PARAMETERS);
3699 goto out;
3700 }
3701 /*
3702 * If required, transform the retrieve request to FF-A v1.1.
3703 */
3704 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3705 retrieve_msg, &fragment_length, &length, ffa_version,
3706 false);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003707
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003708 if (ret.func != FFA_SUCCESS_32) {
3709 goto out;
3710 }
J-Alvesc7270b72024-09-12 10:16:34 +01003711 } else {
3712 if (!api_ffa_memory_hypervisor_retrieve_request_validate(
3713 retrieve_msg, ffa_version)) {
3714 dlog_verbose(
3715 "All fields except the handle in the "
3716 "memory access descriptor must be zero for a "
3717 "hypervisor retrieve request.\n");
3718 ret = ffa_error(FFA_INVALID_PARAMETERS);
3719 goto out;
3720 }
J-Alves00847062022-10-03 17:08:44 +01003721 }
3722
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003723 retrieve_request = retrieve_msg;
Kathleen Capella578784f2023-09-01 18:03:27 -04003724
J-Alvesb5084cf2022-07-06 14:20:12 +01003725 if (plat_ffa_memory_handle_allocated_by_current_world(
3726 retrieve_request->handle)) {
3727 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3728 &api_page_pool);
3729 } else {
Daniel Boulbyfdd59f72023-10-19 16:43:19 +01003730 dlog_error("Invalid FF-A memory handle.\n");
3731 ret = ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesb5084cf2022-07-06 14:20:12 +01003732 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003733out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003734 vm_unlock(&to_locked);
3735 return ret;
3736}
3737
J-Alvesa39a8442024-04-26 15:41:47 +01003738/**
3739 * Copies the memory relinquish descriptor from the partition's TX buffer, to
3740 * the buffer of the local CPU. Do it safely, and return error if:
3741 * - FFA_ABORTED: if the `memcpy_trapped` fails.
3742 * - FFA_INVALID_PARAMETERS: if the size of the full memory relinquish
3743 * descriptor doesn't fit the local CPU buffer.
3744 *
3745 * Returns FFA_SUCCESS if copying goes well, and sets 'out_relinquish'
3746 * to the address of the cpu buffer with the relinquish descriptor.
3747 */
3748static struct ffa_value api_get_ffa_mem_relinquish_descriptor(
3749 struct vcpu *current, const void *from_msg,
3750 struct ffa_mem_relinquish **out_relinquish)
3751{
3752 struct ffa_mem_relinquish *relinquish_request;
3753 uint32_t from_msg_size;
3754 uint32_t total_from_msg_size;
3755 uint32_t dst_size;
3756 vaddr_t dst;
3757 vaddr_t src;
3758
3759 assert(from_msg != NULL);
3760 assert(out_relinquish != NULL);
3761
3762 /*
3763 * Copy the relinquish descriptor to an internal buffer, so that the
3764 * caller can't change it underneath us.
3765 */
3766 relinquish_request =
3767 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
3768
3769 /* Set the destination for the copy. */
3770 dst = va_from_ptr(relinquish_request);
3771 src = va_from_ptr(from_msg);
3772
3773 dst_size = cpu_get_buffer_size(current->cpu);
3774
3775 /* Only copy the size to start with. */
3776 from_msg_size = sizeof(struct ffa_mem_relinquish);
3777 total_from_msg_size = from_msg_size;
3778
3779 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3780 from_msg_size)) {
3781 dlog_error(
3782 "%s: Failed to copy FF-A memory relinquish "
3783 "descriptor.\n",
3784 __func__);
3785 return ffa_error(FFA_ABORTED);
3786 }
3787
3788 if (relinquish_request->endpoint_count != 1) {
3789 dlog_error("%s: relinquish descriptor must have 1 endpoint\n",
3790 __func__);
3791 return ffa_error(FFA_INVALID_PARAMETERS);
3792 }
3793
3794 /*
3795 * Increment the `dst` to the position right after the copied header.
3796 * Increment the `src` to point at the list of endpoints.
3797 *
3798 * Calculate the new `dst_size` which is the size of the allocated cpu
3799 * buffer, minus the size of the copied memory relinquish header.
3800 *
3801 * Only after the above: determine the new `from_msg_size` in accordance
3802 * to the endpoint count.
3803 */
3804 dst = va_add(dst, from_msg_size);
3805 src = va_add(src, from_msg_size);
3806
3807 /*
3808 * Check if it is safe to copy the rest of the message.
3809 * This also serves as a santiy check to 'endpoint_count'.
3810 * The size of what is left in the descriptor, based on endpoint_count,
3811 * shall not be bigger than the size of the mailbox minus the size of
3812 * the header which was previously copied in this function.
3813 */
3814 dst_size -= from_msg_size;
3815 from_msg_size = relinquish_request->endpoint_count * sizeof(ffa_id_t);
3816 total_from_msg_size += from_msg_size;
3817
3818 if (total_from_msg_size > HF_MAILBOX_SIZE ||
3819 total_from_msg_size > dst_size) {
3820 dlog_verbose(
3821 "Relinquish message too long. Endpoint count: %u\n",
3822 relinquish_request->endpoint_count);
3823 return ffa_error(FFA_INVALID_PARAMETERS);
3824 }
3825
3826 /* Copy the remaining fragment. */
3827 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3828 from_msg_size)) {
3829 dlog_error("%s: Failed to copy FF-A relinquish request.\n",
3830 __func__);
3831 return ffa_error(FFA_ABORTED);
3832 }
3833
3834 /*
3835 * Set the output address for the relinquish descriptor to the current
3836 * cpu's buffer.
3837 */
3838 *out_relinquish = relinquish_request;
3839
3840 return (struct ffa_value){.func = FFA_SUCCESS_32};
3841}
3842
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003843struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003844{
3845 struct vm *from = current->vm;
3846 struct vm_locked from_locked;
3847 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003848 struct ffa_value ret;
J-Alvesa39a8442024-04-26 15:41:47 +01003849 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003850
3851 from_locked = vm_lock(from);
3852 from_msg = from->mailbox.send;
3853
3854 if (from_msg == NULL) {
3855 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003856 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003857 goto out;
3858 }
3859
J-Alvesa39a8442024-04-26 15:41:47 +01003860 ret = api_get_ffa_mem_relinquish_descriptor(current, from_msg,
3861 &relinquish_request);
3862
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003863 /*
J-Alvesa39a8442024-04-26 15:41:47 +01003864 * If the descriptor was safely copied, continue with the handling of
3865 * the retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003866 */
J-Alvesa39a8442024-04-26 15:41:47 +01003867 if (ret.func == FFA_SUCCESS_32) {
3868 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3869 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003870 }
J-Alves8f2150d2024-03-28 16:15:56 +00003871
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003872out:
3873 vm_unlock(&from_locked);
3874 return ret;
3875}
3876
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003877struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3878 ffa_memory_region_flags_t flags,
3879 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003880{
3881 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003882 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003883
Olivier Deprez55a189e2021-06-09 15:45:27 +02003884 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003885 struct vm_locked to_locked = vm_lock(to);
3886
Andrew Walbranca808b12020-05-15 17:22:28 +01003887 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003888 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003889
Andrew Walbran290b0c92020-02-03 16:37:14 +00003890 vm_unlock(&to_locked);
3891 } else {
J-Alvesfc19b372022-07-06 12:17:35 +01003892 ret = plat_ffa_other_world_mem_reclaim(to, handle, flags,
3893 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003894 }
3895
3896 return ret;
3897}
3898
3899struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
3900 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003901 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003902 struct vcpu *current)
3903{
3904 struct vm *to = current->vm;
3905 struct vm_locked to_locked;
3906 struct ffa_value ret;
3907
3908 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01003909 if (vm_id_is_current_world(to->id)) {
3910 if (sender_vm_id != 0) {
3911 dlog_verbose("%s: Invalid sender.\n", __func__);
3912 return ffa_error(FFA_INVALID_PARAMETERS);
3913 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003914 }
3915
3916 to_locked = vm_lock(to);
3917
J-Alves122f1a12022-12-12 15:55:42 +00003918 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003919 /*
3920 * Can't retrieve memory information if the mailbox is not
3921 * available.
3922 */
J-Alves59ed0042022-07-28 18:26:41 +01003923 dlog_verbose("%s: RX buffer not ready partition %x.\n",
3924 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003925 ret = ffa_error(FFA_BUSY);
3926 goto out;
3927 }
3928
3929 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01003930 sender_vm_id, &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003931out:
3932 vm_unlock(&to_locked);
3933 return ret;
3934}
3935
3936struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
3937 uint32_t fragment_length,
J-Alves19e20cf2023-08-02 12:48:55 +01003938 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003939 struct vcpu *current)
3940{
3941 struct vm *from = current->vm;
3942 const void *from_msg;
3943 void *fragment_copy;
3944 struct ffa_value ret;
3945
3946 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01003947 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
3948 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01003949 return ffa_error(FFA_INVALID_PARAMETERS);
3950 }
3951
3952 /*
3953 * Check that the sender has configured its send buffer. If the TX
3954 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3955 * be safely accessed after releasing the lock since the TX mailbox
3956 * address can only be configured once.
3957 */
3958 sl_lock(&from->lock);
3959 from_msg = from->mailbox.send;
3960 sl_unlock(&from->lock);
3961
3962 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01003963 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003964 return ffa_error(FFA_INVALID_PARAMETERS);
3965 }
3966
3967 /*
3968 * Copy the fragment to a fresh page from the memory pool. This prevents
3969 * the sender from changing it underneath us, and also lets us keep it
3970 * around in the share state table if needed.
3971 */
3972 if (fragment_length > HF_MAILBOX_SIZE ||
3973 fragment_length > MM_PPOOL_ENTRY_SIZE) {
3974 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003975 "Fragment length %d larger than mailbox size %zu.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003976 fragment_length, HF_MAILBOX_SIZE);
3977 return ffa_error(FFA_INVALID_PARAMETERS);
3978 }
3979 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
3980 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
3981 0) {
3982 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
3983 return ffa_error(FFA_INVALID_PARAMETERS);
3984 }
3985 fragment_copy = mpool_alloc(&api_page_pool);
3986 if (fragment_copy == NULL) {
3987 dlog_verbose("Failed to allocate fragment copy.\n");
3988 return ffa_error(FFA_NO_MEMORY);
3989 }
J-Alves8f2150d2024-03-28 16:15:56 +00003990
3991 if (!memcpy_trapped(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg,
3992 fragment_length)) {
3993 dlog_error("%s: Failed to copy fragment.\n", __func__);
3994 return ffa_error(FFA_ABORTED);
3995 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003996
3997 /*
3998 * Hafnium doesn't support fragmentation of memory retrieve requests
3999 * (because it doesn't support caller-specified mappings, so a request
4000 * will never be larger than a single page), so this must be part of a
4001 * memory send (i.e. donate, lend or share) request.
4002 *
4003 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01004004 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01004005 */
J-Alvesfdd29272022-07-19 13:16:31 +01004006 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004007 struct vm_locked from_locked = vm_lock(from);
4008
4009 ret = ffa_memory_send_continue(from_locked, fragment_copy,
4010 fragment_length, handle,
4011 &api_page_pool);
4012 /*
4013 * `ffa_memory_send_continue` takes ownership of the
4014 * fragment_copy, so we don't need to free it here.
4015 */
4016 vm_unlock(&from_locked);
4017 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01004018 ret = plat_ffa_other_world_mem_send_continue(
4019 from, fragment_copy, fragment_length, handle,
4020 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00004021 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00004022
4023 return ret;
4024}
Max Shvetsov40108e72020-08-27 12:39:50 +01004025
Olivier Deprezd614d322021-06-18 15:21:00 +02004026/**
4027 * Register an entry point for a vCPU in warm boot cases.
4028 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
4029 */
Max Shvetsov40108e72020-08-27 12:39:50 +01004030struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
4031 struct vcpu *current)
4032{
4033 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01004034 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01004035
Olivier Deprezd614d322021-06-18 15:21:00 +02004036 /*
4037 * Reject if interface is not supported at this FF-A instance
4038 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
4039 */
4040 if (!plat_ffa_is_secondary_ep_register_supported() ||
Karl Meakin82041822024-05-20 11:14:34 +01004041 vm_is_up(current->vm)) {
Olivier Deprezd614d322021-06-18 15:21:00 +02004042 return ffa_error(FFA_NOT_SUPPORTED);
4043 }
4044
4045 /*
4046 * No further check is made on the address validity
4047 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
4048 * from the VM or vCPU structure.
4049 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
4050 * For each SP [...] the Framework assumes that the same entry point
4051 * address is used for initializing any execution context during a
4052 * secondary cold boot.
4053 * If this function is invoked multiple times, then the entry point
4054 * address specified in the last valid invocation must be used by the
4055 * callee.
4056 */
Olivier Deprezb2808332023-02-02 15:25:40 +01004057 current_locked = vcpu_lock(current);
4058 if (current->rt_model != RTM_SP_INIT) {
4059 dlog_error(
4060 "FFA_SECONDARY_EP_REGISTER can only be called while "
4061 "vCPU in run-time state for initialization.\n");
4062 vcpu_unlock(&current_locked);
4063 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02004064 }
Olivier Deprezb2808332023-02-02 15:25:40 +01004065 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02004066
Olivier Deprezb2808332023-02-02 15:25:40 +01004067 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01004068 vm_locked.vm->secondary_ep = entry_point;
4069 vm_unlock(&vm_locked);
4070
Olivier Deprezb2808332023-02-02 15:25:40 +01004071 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01004072}
J-Alvesa0f317d2021-06-09 13:31:59 +01004073
J-Alves19e20cf2023-08-02 12:48:55 +01004074struct ffa_value api_ffa_notification_bitmap_create(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004075 ffa_vcpu_count_t vcpu_count,
4076 struct vcpu *current)
4077{
J-Alves7ccaccf2024-02-01 14:25:23 +00004078 const struct ffa_value ret =
4079 plat_ffa_is_notifications_bitmap_access_valid(current, vm_id);
4080
4081 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4082 dlog_verbose(
4083 "FFA_NOTIFICATION_BITMAP_CREATE to be used by "
4084 "hypervisor for valid NWd VM IDs only (%x).\n",
4085 vm_id);
4086 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004087 }
4088
4089 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
4090}
4091
J-Alves19e20cf2023-08-02 12:48:55 +01004092struct ffa_value api_ffa_notification_bitmap_destroy(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004093 struct vcpu *current)
4094{
J-Alves7ccaccf2024-02-01 14:25:23 +00004095 const struct ffa_value ret =
4096 plat_ffa_is_notifications_bitmap_access_valid(current, vm_id);
4097
4098 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4099 dlog_verbose(
4100 "FFA_NOTIFICATION_BITMAP_DESTROY to be used by "
4101 "hypervisor for valid NWd VM IDs only (%x).\n",
4102 vm_id);
4103 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004104 }
4105
4106 return plat_ffa_notifications_bitmap_destroy(vm_id);
4107}
J-Alvesc003a7a2021-03-18 13:06:53 +00004108
4109struct ffa_value api_ffa_notification_update_bindings(
J-Alves19e20cf2023-08-02 12:48:55 +01004110 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesc003a7a2021-03-18 13:06:53 +00004111 ffa_notifications_bitmap_t notifications, bool is_bind,
4112 struct vcpu *current)
4113{
4114 struct ffa_value ret = {.func = FFA_SUCCESS_32};
4115 struct vm_locked receiver_locked;
4116 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
J-Alves19e20cf2023-08-02 12:48:55 +01004117 const ffa_id_t id_to_update = is_bind ? sender_vm_id : HF_INVALID_VM_ID;
4118 const ffa_id_t id_to_validate =
J-Alvesc003a7a2021-03-18 13:06:53 +00004119 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01004120 const uint32_t flags_mbz =
4121 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
4122
4123 if ((flags_mbz & flags) != 0U) {
4124 return ffa_error(FFA_INVALID_PARAMETERS);
4125 }
J-Alvesc003a7a2021-03-18 13:06:53 +00004126
4127 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
4128 receiver_vm_id)) {
4129 dlog_verbose("Invalid use of notifications bind interface.\n");
4130 return ffa_error(FFA_INVALID_PARAMETERS);
4131 }
4132
J-Alvesb15e9402021-09-08 11:44:42 +01004133 if (plat_ffa_notifications_update_bindings_forward(
4134 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
4135 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01004136 return ret;
4137 }
4138
J-Alvesc003a7a2021-03-18 13:06:53 +00004139 if (notifications == 0U) {
Karl Meakine8937d92024-03-19 16:04:25 +00004140 dlog_verbose("No notifications have been specified %lx.\n",
J-Alves1512acc2024-02-01 16:57:10 +00004141 notifications);
J-Alvesc003a7a2021-03-18 13:06:53 +00004142 return ffa_error(FFA_INVALID_PARAMETERS);
4143 }
4144
4145 /**
4146 * This check assumes receiver is the current VM, and has been enforced
4147 * by 'plat_ffa_is_notifications_bind_valid'.
4148 */
4149 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4150
4151 if (receiver_locked.vm == NULL) {
4152 dlog_verbose("Receiver doesn't exist!\n");
J-Alves7333dcf2024-02-01 17:25:10 +00004153 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesc003a7a2021-03-18 13:06:53 +00004154 }
4155
J-Alves3e55c4d2024-10-09 14:24:07 +01004156 if (receiver_locked.vm->ffa_version < FFA_VERSION_1_1) {
4157 dlog_verbose(
4158 "%s: caller (%x) version should be GE to FF-A v1.1.\n",
4159 __func__, receiver_locked.vm->id);
4160 ret = ffa_error(FFA_NOT_SUPPORTED);
4161 goto out;
4162 }
4163
J-Alves09ff9d82021-11-02 11:55:20 +00004164 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004165 dlog_verbose("Notifications are not enabled.\n");
4166 ret = ffa_error(FFA_NOT_SUPPORTED);
4167 goto out;
4168 }
4169
4170 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
4171 vm_find(sender_vm_id) == NULL) {
4172 dlog_verbose("Sender VM does not exist!\n");
4173 ret = ffa_error(FFA_INVALID_PARAMETERS);
4174 goto out;
4175 }
4176
4177 /*
4178 * Can't bind/unbind notifications if at least one is bound to a
4179 * different sender.
4180 */
4181 if (!vm_notifications_validate_bound_sender(
J-Alves661e1b72023-08-02 13:39:40 +01004182 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_validate,
4183 notifications)) {
J-Alvesf0208142024-02-01 17:28:58 +00004184 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004185 "Sender %x not permitted to set notifications %lx to "
J-Alvesf0208142024-02-01 17:28:58 +00004186 "%x.\n",
4187 sender_vm_id, notifications, receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004188 ret = ffa_error(FFA_DENIED);
4189 goto out;
4190 }
4191
4192 /**
4193 * Check if there is a pending notification within those specified in
4194 * the bitmap.
4195 */
4196 if (vm_are_notifications_pending(receiver_locked,
J-Alves661e1b72023-08-02 13:39:40 +01004197 ffa_is_vm_id(sender_vm_id),
J-Alvesc003a7a2021-03-18 13:06:53 +00004198 notifications)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004199 dlog_verbose("Notifications within '%lx' pending.\n",
J-Alvesc003a7a2021-03-18 13:06:53 +00004200 notifications);
4201 ret = ffa_error(FFA_DENIED);
4202 goto out;
4203 }
4204
4205 vm_notifications_update_bindings(
J-Alves661e1b72023-08-02 13:39:40 +01004206 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_update,
J-Alvesc003a7a2021-03-18 13:06:53 +00004207 notifications, is_per_vcpu && is_bind);
4208
4209out:
4210 vm_unlock(&receiver_locked);
4211 return ret;
4212}
J-Alvesaa79c012021-07-09 14:29:45 +01004213
4214struct ffa_value api_ffa_notification_set(
J-Alves19e20cf2023-08-02 12:48:55 +01004215 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesaa79c012021-07-09 14:29:45 +01004216 ffa_notifications_bitmap_t notifications, struct vcpu *current)
4217{
4218 struct ffa_value ret;
4219 struct vm_locked receiver_locked;
J-Alvesaa79c012021-07-09 14:29:45 +01004220 /*
4221 * Check if is per-vCPU or global, and extracting vCPU ID according
4222 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
4223 */
4224 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
4225 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
J-Alves6b754a12024-02-14 14:26:30 +00004226 const uint32_t flags_mbz =
4227 ~(FFA_NOTIFICATIONS_FLAG_PER_VCPU |
4228 FFA_NOTIFICATIONS_FLAG_DELAY_SRI | (0xFFFFU << 16));
J-Alves41c5da32024-06-19 10:05:05 +01004229 const bool delay_sri = (FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) != 0U;
J-Alves6b754a12024-02-14 14:26:30 +00004230
4231 if ((flags_mbz & flags) != 0U) {
4232 dlog_verbose("%s: caller shouldn't set bits that MBZ.\n",
4233 __func__);
4234 return ffa_error(FFA_INVALID_PARAMETERS);
4235 }
J-Alvesaa79c012021-07-09 14:29:45 +01004236
J-Alves30ac91d2024-02-14 15:33:12 +00004237 /* Global notifications must target any vCPU. */
4238 if (!is_per_vcpu && vcpu_id != 0U) {
4239 dlog_verbose(
4240 "For global notifications vCPU ID MBZ in call to set "
4241 "notifications.\n");
4242 return ffa_error(FFA_INVALID_PARAMETERS);
4243 }
4244
J-Alvesaa79c012021-07-09 14:29:45 +01004245 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
4246 receiver_vm_id)) {
4247 dlog_verbose("Invalid use of notifications set interface.\n");
4248 return ffa_error(FFA_INVALID_PARAMETERS);
4249 }
4250
4251 if (notifications == 0U) {
4252 dlog_verbose("No notifications have been specified.\n");
4253 return ffa_error(FFA_INVALID_PARAMETERS);
4254 }
4255
J-Alves41c5da32024-06-19 10:05:05 +01004256 /*
4257 * The 'Delay Schedule Receiver interrupt flag' only applies to the
4258 * secure virtual FF-A instance.
4259 */
4260 if (!vm_id_is_current_world(sender_vm_id) && delay_sri) {
4261 dlog_verbose(
4262 "The delay SRI flag can only be set at the secure "
4263 "virtual FF-A instance.\n");
4264 return ffa_error(FFA_INVALID_PARAMETERS);
4265 }
4266
J-Alvesde7bd2f2021-09-09 19:54:35 +01004267 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
4268 flags, notifications, &ret)) {
4269 return ret;
4270 }
4271
J-Alvesaa79c012021-07-09 14:29:45 +01004272 /*
4273 * This check assumes receiver is the current VM, and has been enforced
4274 * by 'plat_ffa_is_notification_set_valid'.
4275 */
4276 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4277
4278 if (receiver_locked.vm == NULL) {
4279 dlog_verbose("Receiver ID is not valid.\n");
4280 return ffa_error(FFA_INVALID_PARAMETERS);
4281 }
4282
J-Alves09ff9d82021-11-02 11:55:20 +00004283 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004284 dlog_verbose("Receiver's notifications not enabled.\n");
4285 ret = ffa_error(FFA_DENIED);
4286 goto out;
4287 }
4288
4289 /*
J-Alves89c22132024-02-14 15:44:41 +00004290 * Check the if the notifications are bound as global if per-vCPU flag
4291 * is set, or if they are bound as per-vCPU and caller setting as
4292 * global. In either case, return FFA_INVALID_PARAMETERS.
4293 */
4294 if (vm_notifications_validate_binding(
4295 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4296 notifications, !is_per_vcpu)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004297 dlog_verbose("Notifications in %lx are %s\n", notifications,
J-Alves89c22132024-02-14 15:44:41 +00004298 !is_per_vcpu ? "global" : "per-vCPU");
4299 ret = ffa_error(FFA_INVALID_PARAMETERS);
4300 goto out;
4301 }
4302
4303 /*
J-Alvesaa79c012021-07-09 14:29:45 +01004304 * If notifications are not bound to the sender, they wouldn't be
4305 * enabled either for the receiver.
4306 */
4307 if (!vm_notifications_validate_binding(
J-Alves661e1b72023-08-02 13:39:40 +01004308 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4309 notifications, is_per_vcpu)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004310 dlog_verbose("Notifications bindings not valid.\n");
4311 ret = ffa_error(FFA_DENIED);
4312 goto out;
4313 }
4314
4315 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
4316 dlog_verbose("Invalid VCPU ID!\n");
4317 ret = ffa_error(FFA_INVALID_PARAMETERS);
4318 goto out;
4319 }
4320
J-Alves7461ef22021-10-18 17:21:33 +01004321 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00004322 vm_notifications_partition_set_pending(
J-Alves661e1b72023-08-02 13:39:40 +01004323 receiver_locked, ffa_is_vm_id(sender_vm_id), notifications,
J-Alves5a16c962022-03-25 12:32:51 +00004324 vcpu_id, is_per_vcpu);
4325
Karl Meakine8937d92024-03-19 16:04:25 +00004326 dlog_verbose("Set the notifications: %lx.\n", notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004327
J-Alves41c5da32024-06-19 10:05:05 +01004328 if (!delay_sri) {
J-Alves13394022021-06-30 13:48:49 +01004329 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
4330 vcpu_index(current));
4331 plat_ffa_sri_trigger_not_delayed(current->cpu);
4332 } else {
J-Alvesea8ccfe2024-10-09 11:47:24 +01004333 plat_ffa_sri_set_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004334 }
J-Alvesaa79c012021-07-09 14:29:45 +01004335
J-Alves13394022021-06-30 13:48:49 +01004336 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01004337out:
4338 vm_unlock(&receiver_locked);
4339
4340 return ret;
4341}
4342
4343static struct ffa_value api_ffa_notification_get_success_return(
4344 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
4345 ffa_notifications_bitmap_t from_framework)
4346{
4347 return (struct ffa_value){
4348 .func = FFA_SUCCESS_32,
4349 .arg1 = 0U,
4350 .arg2 = (uint32_t)from_sp,
4351 .arg3 = (uint32_t)(from_sp >> 32),
4352 .arg4 = (uint32_t)from_vm,
4353 .arg5 = (uint32_t)(from_vm >> 32),
4354 .arg6 = (uint32_t)from_framework,
4355 .arg7 = (uint32_t)(from_framework >> 32),
4356 };
4357}
4358
J-Alves19e20cf2023-08-02 12:48:55 +01004359struct ffa_value api_ffa_notification_get(ffa_id_t receiver_vm_id,
J-Alvesaa79c012021-07-09 14:29:45 +01004360 ffa_vcpu_index_t vcpu_id,
4361 uint32_t flags, struct vcpu *current)
4362{
J-Alves663682a2022-03-25 13:56:51 +00004363 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01004364 ffa_notifications_bitmap_t sp_notifications = 0;
4365 ffa_notifications_bitmap_t vm_notifications = 0;
4366 struct vm_locked receiver_locked;
4367 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01004368 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
4369 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
4370 FFA_NOTIFICATION_FLAG_BITMAP_SP |
4371 FFA_NOTIFICATION_FLAG_BITMAP_VM);
4372
4373 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
4374 if ((flags & flags_mbz) != 0U) {
4375 dlog_verbose(
4376 "Invalid flags bit(s) set in notifications get. [31:4] "
4377 "MBZ(%x)\n",
4378 flags);
4379 return ffa_error(FFA_INVALID_PARAMETERS);
4380 }
J-Alvesaa79c012021-07-09 14:29:45 +01004381
4382 /*
J-Alvesfc95a302022-04-22 14:18:23 +01004383 * Following check should capture wrong uses of the interface,
4384 * depending on whether Hafnium is SPMC or hypervisor. On the
4385 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01004386 */
J-Alvesfc95a302022-04-22 14:18:23 +01004387 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
4388 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004389 dlog_verbose("Invalid use of notifications get interface.\n");
4390 return ffa_error(FFA_INVALID_PARAMETERS);
4391 }
4392
4393 /*
4394 * This check assumes receiver is the current VM, and has been enforced
4395 * by `plat_ffa_is_notifications_get_valid`.
4396 */
4397 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4398
4399 /*
4400 * `plat_ffa_is_notifications_get_valid` ensures following is never
4401 * true.
4402 */
4403 CHECK(receiver_locked.vm != NULL);
4404
J-Alves60458812024-03-22 11:57:10 +00004405 if (receiver_locked.vm->vcpu_count <= vcpu_id) {
J-Alves1abb3342022-01-05 11:59:10 +00004406 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004407 "Invalid VCPU ID %u. vcpu count %u current core: "
4408 "%zu!\n",
J-Alves1abb3342022-01-05 11:59:10 +00004409 vcpu_id, receiver_locked.vm->vcpu_count,
4410 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01004411 ret = ffa_error(FFA_INVALID_PARAMETERS);
4412 goto out;
4413 }
4414
4415 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01004416 if (!plat_ffa_notifications_get_from_sp(
4417 receiver_locked, vcpu_id, &sp_notifications,
4418 &ret)) {
4419 dlog_verbose("Failed to get notifications from sps.");
4420 goto out;
4421 }
J-Alvesaa79c012021-07-09 14:29:45 +01004422 }
4423
4424 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00004425 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01004426 receiver_locked, true, vcpu_id);
4427 }
4428
J-Alvesd605a092022-03-28 14:20:48 +01004429 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
4430 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
4431 if (!plat_ffa_notifications_get_framework_notifications(
4432 receiver_locked, &framework_notifications, flags,
4433 vcpu_id, &ret)) {
4434 dlog_verbose(
4435 "Failed to get notifications from "
4436 "framework.\n");
4437 goto out;
4438 }
4439 }
4440
J-Alves663682a2022-03-25 13:56:51 +00004441 ret = api_ffa_notification_get_success_return(
4442 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004443
J-Alves6e2abc62021-12-02 14:58:56 +00004444 if (!receiver_locked.vm->el0_partition &&
4445 !vm_are_global_notifications_pending(receiver_locked)) {
4446 vm_notifications_set_npi_injected(receiver_locked, false);
4447 }
4448
J-Alvesaa79c012021-07-09 14:29:45 +01004449out:
4450 vm_unlock(&receiver_locked);
4451
4452 return ret;
4453}
J-Alvesc8e8a222021-06-08 17:33:52 +01004454
4455/**
4456 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
4457 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
4458 */
4459static struct ffa_value api_ffa_notification_info_get_success_return(
4460 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01004461 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01004462{
4463 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
4464
4465 /*
4466 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
4467 * hold the list of ids.
4468 */
4469 memcpy_s(&ret.arg3,
4470 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
4471 sizeof(ids[0]) * ids_count);
4472
4473 /*
4474 * According to the spec x2 should have:
4475 * - Bit flagging if there are more notifications pending;
4476 * - The total number of elements (i.e. total list size);
4477 * - The number of VCPU IDs within each VM specific list.
4478 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004479 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
4480 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
4481 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01004482
4483 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
4484 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
4485
4486 for (unsigned int i = 0; i < lists_count; i++) {
4487 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
4488 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
4489 }
4490
4491 return ret;
4492}
4493
4494struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
4495{
4496 /*
4497 * Following set of variables should be populated with the return info.
4498 * At a successfull handling of this interface, they should be used
4499 * to populate the 'ret' structure in accordance to the table 17.29
4500 * of the FF-A v1.1 Beta0 specification.
4501 */
4502 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
4503 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
4504 uint32_t lists_count = 0;
4505 uint32_t ids_count = 0;
4506 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01004507 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004508
4509 /*
4510 * This interface can only be called at NS virtual/physical FF-A
4511 * instance by the endpoint implementing the primary scheduler and the
4512 * Hypervisor/OS kernel.
4513 * In the SPM, following check passes if call has been forwarded from
4514 * the hypervisor.
4515 */
Karl Meakin5e996992024-05-20 11:27:07 +01004516
4517 if (!vm_is_primary(current->vm)) {
J-Alvesc8e8a222021-06-08 17:33:52 +01004518 dlog_verbose(
4519 "Only the receiver's scheduler can use this "
4520 "interface\n");
4521 return ffa_error(FFA_NOT_SUPPORTED);
4522 }
4523
J-Alvesca058c22021-09-10 14:02:07 +01004524 /*
4525 * Forward call to the other world, and fill the arrays used to assemble
4526 * return.
4527 */
4528 plat_ffa_notification_info_get_forward(
4529 ids, &ids_count, lists_sizes, &lists_count,
4530 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4531
4532 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
4533
J-Alvesc8e8a222021-06-08 17:33:52 +01004534 /* Get notifications' info from this world */
4535 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
4536 ++index) {
4537 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
4538
4539 list_is_full = vm_notifications_info_get(
4540 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
4541 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4542
4543 vm_unlock(&vm_locked);
4544 }
4545
4546 if (!list_is_full) {
4547 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004548 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01004549 ids, &ids_count, lists_sizes, &lists_count,
4550 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4551 }
4552
4553 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01004554 dlog_verbose(
4555 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01004556 result = ffa_error(FFA_NO_DATA);
4557 } else {
4558 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01004559 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01004560 }
4561
J-Alves13394022021-06-30 13:48:49 +01004562 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004563}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004564
4565struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
4566{
4567 struct vm_locked vm_locked;
4568 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
4569 bool mode_ret = false;
4570 uint32_t mode = 0;
4571
4572 if (!plat_ffa_is_mem_perm_get_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004573 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004574 }
4575
4576 if (!(current->vm->el0_partition)) {
4577 return ffa_error(FFA_DENIED);
4578 }
4579
4580 vm_locked = vm_lock(current->vm);
4581
4582 /*
4583 * mm_get_mode is used to check if the given base_addr page is already
4584 * mapped. If the page is unmapped, return error. If the page is mapped
4585 * appropriate attributes are returned to the caller. Note that
4586 * mm_get_mode returns true if the address is in the valid VA range as
4587 * supported by the architecture and MMU configurations, as opposed to
4588 * whether a page is mapped or not. For a page to be known as mapped,
4589 * the API must return true AND the returned mode must not have
4590 * MM_MODE_INVALID set.
4591 */
4592 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4593 va_add(base_addr, PAGE_SIZE), &mode);
4594 if (!mode_ret || (mode & MM_MODE_INVALID)) {
4595 ret = ffa_error(FFA_INVALID_PARAMETERS);
4596 goto out;
4597 }
4598
4599 /* No memory should be marked RWX */
4600 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
4601 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
4602
4603 /*
4604 * S-EL0 partitions are expected to have all their pages marked as
4605 * non-global.
4606 */
4607 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
4608 (MM_MODE_NG | MM_MODE_USER));
4609
4610 if (mode & MM_MODE_W) {
4611 /* No memory should be writeable but not readable. */
4612 CHECK(mode & MM_MODE_R);
4613 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4614 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
4615 } else if (mode & MM_MODE_R) {
4616 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4617 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
4618 if (!(mode & MM_MODE_X)) {
4619 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
4620 }
4621 }
4622out:
4623 vm_unlock(&vm_locked);
4624 return ret;
4625}
4626
4627struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
4628 uint32_t mem_perm, struct vcpu *current)
4629{
4630 struct vm_locked vm_locked;
4631 struct ffa_value ret;
4632 bool mode_ret = false;
4633 uint32_t original_mode;
4634 uint32_t new_mode;
4635 struct mpool local_page_pool;
4636
4637 if (!plat_ffa_is_mem_perm_set_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004638 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004639 }
4640
4641 if (!(current->vm->el0_partition)) {
4642 return ffa_error(FFA_DENIED);
4643 }
4644
4645 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
4646 return ffa_error(FFA_INVALID_PARAMETERS);
4647 }
4648
4649 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
4650 (mem_perm != FFA_MEM_PERM_RX)) {
4651 return ffa_error(FFA_INVALID_PARAMETERS);
4652 }
4653
4654 /*
4655 * Create a local pool so any freed memory can't be used by another
4656 * thread. This is to ensure the original mapping can be restored if any
4657 * stage of the process fails.
4658 */
4659 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
4660
4661 vm_locked = vm_lock(current->vm);
4662
4663 /*
4664 * All regions accessible by the partition are mapped during boot. If we
4665 * cannot get a successful translation for the page range, the request
4666 * to change permissions is rejected.
4667 * mm_get_mode is used to check if the given address range is already
4668 * mapped. If the range is unmapped, return error. If the range is
4669 * mapped appropriate attributes are returned to the caller. Note that
4670 * mm_get_mode returns true if the address is in the valid VA range as
4671 * supported by the architecture and MMU configurations, as opposed to
4672 * whether a page is mapped or not. For a page to be known as mapped,
4673 * the API must return true AND the returned mode must not have
4674 * MM_MODE_INVALID set.
4675 */
4676
4677 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4678 va_add(base_addr, page_count * PAGE_SIZE),
4679 &original_mode);
4680 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
4681 ret = ffa_error(FFA_INVALID_PARAMETERS);
4682 goto out;
4683 }
4684
4685 /* Device memory cannot be marked as executable */
4686 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
4687 ret = ffa_error(FFA_INVALID_PARAMETERS);
4688 goto out;
4689 }
4690
4691 new_mode = MM_MODE_USER | MM_MODE_NG;
4692
4693 if (mem_perm == FFA_MEM_PERM_RW) {
4694 new_mode |= MM_MODE_R | MM_MODE_W;
4695 } else if (mem_perm == FFA_MEM_PERM_RX) {
4696 new_mode |= MM_MODE_R | MM_MODE_X;
4697 } else if (mem_perm == FFA_MEM_PERM_RO) {
4698 new_mode |= MM_MODE_R;
4699 }
4700
4701 /*
4702 * Safe to re-map memory, since we know the requested permissions are
4703 * valid, and the memory requested to be re-mapped is also valid.
4704 */
4705 if (!mm_identity_prepare(
4706 &vm_locked.vm->ptable, pa_from_va(base_addr),
4707 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4708 new_mode, &local_page_pool)) {
4709 /*
4710 * Defrag the table into the local page pool.
4711 * mm_identity_prepare could have allocated or freed pages to
4712 * split blocks or tables etc.
4713 */
4714 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
4715
4716 /*
4717 * Guaranteed to succeed mapping with old mode since the mapping
4718 * with old mode already existed and we have a local page pool
4719 * that should have sufficient memory to go back to the original
4720 * state.
4721 */
4722 CHECK(mm_identity_prepare(
4723 &vm_locked.vm->ptable, pa_from_va(base_addr),
4724 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4725 original_mode, &local_page_pool));
4726 mm_identity_commit(
4727 &vm_locked.vm->ptable, pa_from_va(base_addr),
4728 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4729 original_mode, &local_page_pool);
4730
4731 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
4732 ret = ffa_error(FFA_NO_MEMORY);
4733 goto out;
4734 }
4735
4736 mm_identity_commit(
4737 &vm_locked.vm->ptable, pa_from_va(base_addr),
4738 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
4739 &local_page_pool);
4740
4741 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
4742
4743out:
4744 mpool_fini(&local_page_pool);
4745 vm_unlock(&vm_locked);
4746
4747 return ret;
4748}
Maksims Svecovs71b76702022-05-20 15:32:58 +01004749
4750/**
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004751 * Send the contents of the given vCPU's log buffer to the log, preceded
Karl Meakin705b56e2024-05-22 10:04:32 +01004752 * by the VM ID and followed by a newline.
4753 */
Karl Meakin6f1f1212024-07-16 10:18:16 +01004754void api_flush_log_buffer(struct vcpu_locked *vcpu_locked)
Karl Meakin705b56e2024-05-22 10:04:32 +01004755{
Karl Meakin6f1f1212024-07-16 10:18:16 +01004756 /*
4757 * NOTE: This line is parsed by `hftest.py`.
4758 * If you change the format, make sure to update
4759 * `HFTEST_CTRL_JSON_REGEX` as well.
4760 */
4761 struct vcpu *vcpu = vcpu_locked->vcpu;
4762 struct log_buffer *buffer = &vcpu->log_buffer;
4763 ffa_id_t vm_id = vcpu->vm->id;
4764 ffa_id_t vcpu_id = vcpu_index(vcpu);
4765
Karl Meakin7de26952024-06-14 14:50:19 +01004766 buffer->chars[buffer->len] = '\0';
Olivier Deprezbd432092024-07-26 14:01:12 +02004767 dlog("[%x %u] %s\n", vm_id, vcpu_id, buffer->chars);
Karl Meakin7de26952024-06-14 14:50:19 +01004768 buffer->len = 0;
Karl Meakin705b56e2024-05-22 10:04:32 +01004769}
4770
4771/**
Karl Meakin740f74f2024-02-14 18:04:48 +00004772 * Implements FF-A v1.2 FFA_CONSOLE_LOG ABI for buffered logging.
Maksims Svecovs71b76702022-05-20 15:32:58 +01004773 */
4774struct ffa_value api_ffa_console_log(const struct ffa_value args,
4775 struct vcpu *current)
4776{
Karl Meakin740f74f2024-02-14 18:04:48 +00004777 /* Maximum number of characters is 128: 16 registers of 8 bytes each. */
4778 char chars[128] = {0};
Karl Meakin0e617d92024-04-05 12:55:22 +01004779 const bool v1_2 = current->vm->ffa_version >= FFA_VERSION_1_2;
Karl Meakin740f74f2024-02-14 18:04:48 +00004780 const bool log32 = args.func == FFA_CONSOLE_LOG_32;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004781
Karl Meakin740f74f2024-02-14 18:04:48 +00004782 /*
4783 * 32bit: always 6 registers
4784 * 64bit and less than v1.2: 6 registers
4785 * 64bit and v1.2 or greater: 16 registers
4786 */
Karl Meakin66a38bd2024-05-28 16:00:56 +01004787 /* NOLINTNEXTLINE(readability-avoid-nested-conditional-operator) */
Karl Meakin740f74f2024-02-14 18:04:48 +00004788 const size_t registers_max = log32 ? 6 : (v1_2 ? 16 : 6);
4789 const size_t chars_max =
4790 registers_max * (log32 ? sizeof(uint32_t) : sizeof(uint64_t));
4791 const size_t chars_count = args.arg1;
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004792 struct vcpu_locked vcpu_locked;
4793 struct log_buffer *log_buffer;
Karl Meakin740f74f2024-02-14 18:04:48 +00004794
4795 assert(args.func == FFA_CONSOLE_LOG_32 ||
4796 args.func == FFA_CONSOLE_LOG_64);
4797
4798 if (chars_count == 0 || chars_count > chars_max) {
Maksims Svecovs71b76702022-05-20 15:32:58 +01004799 return ffa_error(FFA_INVALID_PARAMETERS);
4800 }
4801
Karl Meakin740f74f2024-02-14 18:04:48 +00004802 if (log32) {
4803 uint32_t *registers = (uint32_t *)chars;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004804
Karl Meakin740f74f2024-02-14 18:04:48 +00004805 registers[0] = args.arg2 & 0xffffffff;
4806 registers[1] = args.arg3 & 0xffffffff;
4807 registers[2] = args.arg4 & 0xffffffff;
4808 registers[3] = args.arg5 & 0xffffffff;
4809 registers[4] = args.arg6 & 0xffffffff;
4810 registers[5] = args.arg7 & 0xffffffff;
4811 } else {
4812 uint64_t *registers = (uint64_t *)chars;
4813
4814 registers[0] = args.arg2;
4815 registers[1] = args.arg3;
4816 registers[2] = args.arg4;
4817 registers[3] = args.arg5;
4818 registers[4] = args.arg6;
4819 registers[5] = args.arg7;
4820 if (v1_2) {
4821 registers[6] = args.extended_val.arg8;
4822 registers[7] = args.extended_val.arg9;
4823 registers[8] = args.extended_val.arg10;
4824 registers[9] = args.extended_val.arg11;
4825 registers[10] = args.extended_val.arg12;
4826 registers[11] = args.extended_val.arg13;
4827 registers[12] = args.extended_val.arg14;
4828 registers[13] = args.extended_val.arg15;
4829 registers[14] = args.extended_val.arg16;
4830 registers[15] = args.extended_val.arg17;
4831 }
4832 }
4833
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004834 vcpu_locked = vcpu_lock(current);
4835 log_buffer = &current->log_buffer;
Karl Meakin7de26952024-06-14 14:50:19 +01004836
Karl Meakin740f74f2024-02-14 18:04:48 +00004837 for (size_t i = 0; i < chars_count; i++) {
4838 bool flush = false;
4839 const char c = chars[i];
4840
4841 if (c == '\n' || c == '\0') {
4842 flush = true;
4843 } else {
Karl Meakin7de26952024-06-14 14:50:19 +01004844 log_buffer->chars[log_buffer->len++] = c;
4845 flush = log_buffer->len == LOG_BUFFER_SIZE;
Karl Meakin740f74f2024-02-14 18:04:48 +00004846 }
4847
4848 if (flush) {
Karl Meakin6f1f1212024-07-16 10:18:16 +01004849 api_flush_log_buffer(&vcpu_locked);
Karl Meakin740f74f2024-02-14 18:04:48 +00004850 }
4851 }
Maksims Svecovs71b76702022-05-20 15:32:58 +01004852
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004853 vcpu_unlock(&vcpu_locked);
Maksims Svecovs71b76702022-05-20 15:32:58 +01004854 return (struct ffa_value){.func = FFA_SUCCESS_32};
4855}
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004856
4857/**
4858 * Send an IPI interrupt to a target vcpu belonging to the
4859 * sender that isn't itself.
4860 */
4861uint64_t api_hf_interrupt_send_ipi(uint32_t target_vcpu_id,
4862 struct vcpu *current)
4863{
4864 struct vm *vm = current->vm;
4865 ffa_vcpu_index_t target_vcpu_index = vcpu_id_to_index(target_vcpu_id);
4866
4867 if (target_vcpu_index >= vm->vcpu_count &&
4868 target_vcpu_index == cpu_index(current->cpu)) {
4869 dlog_verbose("Invalid vCPU %d for IPI.\n", target_vcpu_id);
4870 return -1;
4871 }
4872
4873 dlog_verbose("Injecting IPI to target vCPU%d for %#x\n", target_vcpu_id,
4874 vm->id);
4875
4876 hf_ipi_send_interrupt(vm, target_vcpu_index);
4877
4878 return 0;
4879}