blob: 81e7e24ade54d62fbc608bef9f8cc71297a5f844 [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"
Andrew Walbran508e63c2018-12-20 17:02:37 +000016#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000017
Karl Meakin49ec1e42024-05-10 13:08:24 +010018#include "hf/bits.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010019#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000020#include "hf/dlog.h"
Karl Meakin902af082024-11-28 14:58:38 +000021#include "hf/ffa.h"
22#include "hf/ffa/cpu_cycles.h"
23#include "hf/ffa/direct_messaging.h"
24#include "hf/ffa/ffa_memory.h"
25#include "hf/ffa/indirect_messaging.h"
26#include "hf/ffa/interrupts.h"
27#include "hf/ffa/notifications.h"
28#include "hf/ffa/power_management.h"
29#include "hf/ffa/setup_and_discovery.h"
30#include "hf/ffa/vm.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010031#include "hf/ffa_internal.h"
32#include "hf/ffa_memory.h"
J-Alves33c47bf2022-09-29 11:36:20 +010033#include "hf/ffa_v1_0.h"
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +010034#include "hf/hf_ipi.h"
Andrew Scull6386f252018-12-06 13:29:10 +000035#include "hf/mm.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010036#include "hf/plat/interrupts.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010037#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010038#include "hf/std.h"
Madhukar Pappireddy32424db2024-09-25 15:06:19 -050039#include "hf/timer_mgmt.h"
Karl Meakinc5cebbc2024-06-17 11:30:27 +010040#include "hf/vcpu.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010041#include "hf/vm.h"
42
Andrew Scullf35a5c92018-08-07 18:09:46 +010043#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010044#include "vmapi/hf/ffa.h"
J-Alves126ab502022-09-29 11:37:33 +010045#include "vmapi/hf/ffa_v1_0.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010046
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000047static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010048 "Partition information descriptor size doesn't match the one in "
49 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000050static_assert(sizeof(struct ffa_partition_info) == 24,
51 "Partition information descriptor size doesn't match the one in "
52 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -080053static_assert((sizeof(struct ffa_partition_info) & 7) == 0,
54 "Partition information descriptor must be a multiple of 8 bytes"
55 " for ffa_partition_info_get_regs to work correctly. Information"
56 " from this structure are returned in 8 byte registers and the"
57 " count of 8 byte registers is returned by the ABI.");
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000058/*
59 * To eliminate the risk of deadlocks, we define a partial order for the
60 * acquisition of locks held concurrently by the same physical CPU. Our current
61 * ordering requirements are as follows:
62 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010063 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000064 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010065 * Locks of the same kind require the lock of lowest address to be locked first,
66 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000067 */
68
Andrew Scullaa039b32018-10-04 15:02:26 +010069static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010070 "Currently, a page is mapped for the send and receive buffers so "
71 "the maximum request is the size of a page.");
72
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000073static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
74 "The page pool entry size must be at least as big as the mailbox "
75 "size, so that memory region descriptors can be copied from the "
76 "mailbox for memory sharing.");
77
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -080078/*
79 * Maximum ffa_partition_info entries that can be returned by an invocation
80 * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available
81 * registers/args in struct ffa_value divided by size of struct
82 * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e.
83 * 15 uint64_t fields. For FF-A v1.1, this value should be 5.
84 */
85#define MAX_INFO_REGS_ENTRIES_PER_CALL \
86 ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info))
87static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5,
88 "FF-A v1.1 supports no more than 5 entries"
89 " per FFA_PARTITION_INFO_GET_REGS64 calls");
90
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000091static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000092
93/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000094 * Initialises the API page pool by taking ownership of the contents of the
95 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000096 */
97void api_init(struct mpool *ppool)
98{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000099 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000100}
101
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100102/**
J-Alvesad6a0432021-04-09 16:06:21 +0100103 * Get target VM vCPU:
104 * If VM is UP then return first vCPU.
105 * If VM is MP then return vCPU whose index matches current CPU index.
106 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500107struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +0100108{
109 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
110 struct vcpu *vcpu = NULL;
111
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500112 CHECK((vm != NULL) && (current != NULL));
113
Karl Meakin82041822024-05-20 11:14:34 +0100114 if (vm_is_up(vm)) {
J-Alvesad6a0432021-04-09 16:06:21 +0100115 vcpu = vm_get_vcpu(vm, 0);
116 } else if (current_cpu_index < vm->vcpu_count) {
117 vcpu = vm_get_vcpu(vm, current_cpu_index);
118 }
119
120 return vcpu;
121}
122
123/**
J-Alvesfe7f7372020-11-09 11:32:12 +0000124 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
125 * is given as argument of the function.
126 *
127 * Called to change the context between SPs for direct messaging (when Hafnium
128 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
129 *
130 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100131 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000132 * - MP with pinned Execution Contexts.
133 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600134struct vcpu *api_switch_to_vm(struct vcpu_locked current_locked,
135 struct ffa_value to_ret,
J-Alves19e20cf2023-08-02 12:48:55 +0100136 enum vcpu_state vcpu_state, ffa_id_t to_id)
J-Alvesfe7f7372020-11-09 11:32:12 +0000137{
138 struct vm *to_vm = vm_find(to_id);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600139 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current_locked.vcpu);
J-Alvesfe7f7372020-11-09 11:32:12 +0000140
141 CHECK(next != NULL);
142
143 /* Set the return value for the target VM. */
144 arch_regs_set_retval(&next->regs, to_ret);
145
146 /* Set the current vCPU state. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600147 current_locked.vcpu->state = vcpu_state;
J-Alvesfe7f7372020-11-09 11:32:12 +0000148
149 return next;
150}
151
152/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000153 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100154 *
155 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100156 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100157 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600158struct vcpu *api_switch_to_primary(struct vcpu_locked current_locked,
J-Alves27b71962022-12-12 15:29:58 +0000159 struct ffa_value primary_ret,
160 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100161{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600162 return api_switch_to_vm(current_locked, primary_ret, secondary_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000163 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100164}
165
166/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200167 * Choose next vCPU to run to be the counterpart vCPU in the other
168 * world (run the normal world if currently running in the secure
169 * world). Set current vCPU state to the given vcpu_state parameter.
170 * Set FF-A return values to the target vCPU in the other world.
171 *
172 * Called in context of a direct message response from a secure
173 * partition to a VM.
174 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600175struct vcpu *api_switch_to_other_world(struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100176 struct ffa_value other_world_ret,
177 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200178{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600179 return api_switch_to_vm(current_locked, other_world_ret, vcpu_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000180 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200181}
182
183/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000184 * Returns true if the given vCPU is executing in context of an
185 * FFA_MSG_SEND_DIRECT_REQ invocation.
186 */
J-Alves27b71962022-12-12 15:29:58 +0000187bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000188{
Kathleen Capellae468c112023-12-13 17:56:28 -0500189 return locked.vcpu->direct_request_origin.vm_id != HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000190}
191
192/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100193 * Returns true if the VM owning the given vCPU is supporting managed exit and
194 * the vCPU is currently processing a managed exit.
195 */
196static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
197{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100198 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100199 vcpu_locked.vcpu->processing_managed_exit);
200}
201
202/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000203 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000204 */
205struct vcpu *api_preempt(struct vcpu *current)
206{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600207 struct vcpu_locked current_locked;
208 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100209 struct ffa_value ret = {
210 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200211 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000212 };
213
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600214 current_locked = vcpu_lock(current);
215 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_PREEMPTED);
216 vcpu_unlock(&current_locked);
217
218 return next;
Andrew Scull33fecd32019-01-08 14:48:27 +0000219}
220
221/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000222 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000223 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100224 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100225struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100226{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600227 struct vcpu_locked current_locked;
228 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100229 struct ffa_value ret = {
230 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
231 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100232 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000233
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600234 current_locked = vcpu_lock(current);
235 next = api_switch_to_primary(current_locked, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100236 VCPU_STATE_BLOCKED_INTERRUPT);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600237 vcpu_unlock(&current_locked);
238
239 return next;
Andrew Scullaa039b32018-10-04 15:02:26 +0100240}
241
242/**
Andrew Walbran33645652019-04-15 12:29:31 +0100243 * Puts the current vCPU in off mode, and returns to the primary VM.
244 */
245struct vcpu *api_vcpu_off(struct vcpu *current)
246{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600247 struct vcpu_locked current_locked;
248 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100249 struct ffa_value ret = {
250 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
251 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100252 };
253
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600254 current_locked = vcpu_lock(current);
Andrew Walbran33645652019-04-15 12:29:31 +0100255
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600256 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_OFF);
257 vcpu_unlock(&current_locked);
258
259 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100260}
261
262/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500263 * The current vCPU is blocked on some resource and needs to relinquish
264 * control back to the execution context of the endpoint that originally
265 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000266 */
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500267struct ffa_value api_yield(struct vcpu *current, struct vcpu **next,
268 struct ffa_value *args)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000269{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000270 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
271 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500272 bool transition_allowed;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500273 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500274 uint32_t timeout_low = 0;
275 uint32_t timeout_high = 0;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600276 struct vcpu_locked next_locked = (struct vcpu_locked){
277 .vcpu = NULL,
278 };
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500279
280 if (args != NULL) {
281 if (args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
282 args->arg7 != 0U) {
283 dlog_error(
284 "Parameters passed through registers X4-X7 "
285 "must be zero\n");
286 return ffa_error(FFA_INVALID_PARAMETERS);
287 }
288 timeout_low = (uint32_t)args->arg2 & 0xFFFFFFFF;
289 timeout_high = (uint32_t)args->arg3 & 0xFFFFFFFF;
290 }
Andrew Scull66d62bf2019-02-01 13:54:10 +0000291
Karl Meakin5e996992024-05-20 11:27:07 +0100292 if (vm_is_primary(current->vm)) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000293 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000294 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000295 }
296
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000297 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500298 transition_allowed = plat_ffa_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600299 current_locked, current->vm->id, HF_INVALID_VM_ID, next_locked,
300 FFA_YIELD_32, &next_state);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000301
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500302 if (!transition_allowed) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600303 ret = ffa_error(FFA_DENIED);
304 goto out;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000305 }
306
Madhukar Pappireddy65764072023-05-23 17:18:58 -0500307 /*
308 * The current vCPU is expected to move to BLOCKED state. However,
309 * under certain circumstances, it is allowed for the current vCPU
310 * to be resumed immediately without ever moving to BLOCKED state. One
311 * such scenario occurs when an SP's execution context attempts to
312 * yield cycles while handling secure interrupt. Refer to the comments
313 * in the SPMC variant of the plat_ffa_yield_prepare function.
314 */
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500315 assert(!vm_id_is_current_world(current->vm->id) ||
316 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500317
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600318 ret = plat_ffa_yield_prepare(current_locked, next, timeout_low,
319 timeout_high);
320out:
321 vcpu_unlock(&current_locked);
322 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000323}
324
325/**
Andrew Walbran33645652019-04-15 12:29:31 +0100326 * Switches to the primary so that it can switch to the target, or kick it if it
327 * is already running on a different physical CPU.
328 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600329static struct vcpu *api_wake_up_locked(struct vcpu_locked current_locked,
330 struct vcpu *target_vcpu)
Andrew Walbran33645652019-04-15 12:29:31 +0100331{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100333 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100334 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
335 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100336 };
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600337
338 return api_switch_to_primary(current_locked, ret, VCPU_STATE_BLOCKED);
339}
340
341struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
342{
343 struct vcpu_locked current_locked;
344 struct vcpu *next;
345
346 current_locked = vcpu_lock(current);
347 next = api_wake_up_locked(current_locked, target_vcpu);
348 vcpu_unlock(&current_locked);
349
350 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100351}
352
353/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000354 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000355 */
356struct vcpu *api_abort(struct vcpu *current)
357{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100358 struct ffa_value ret = ffa_error(FFA_ABORTED);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600359 struct vcpu_locked current_locked;
360 struct vcpu *next;
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500361 struct vm_locked vm_locked;
Andrew Scull9726c252019-01-23 13:44:19 +0000362
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100363 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000364 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000365
Karl Meakin5e996992024-05-20 11:27:07 +0100366 if (vm_is_primary(current->vm)) {
Andrew Scull9726c252019-01-23 13:44:19 +0000367 /* TODO: what to do when the primary aborts? */
368 for (;;) {
369 /* Do nothing. */
370 }
371 }
372
373 atomic_store_explicit(&current->vm->aborting, true,
374 memory_order_relaxed);
375
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500376 vm_locked = vm_lock(current->vm);
377 plat_ffa_free_vm_resources(vm_locked);
378 vm_unlock(&vm_locked);
Andrew Scull9726c252019-01-23 13:44:19 +0000379
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600380 current_locked = vcpu_lock(current);
381 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_ABORTED);
382 vcpu_unlock(&current_locked);
383
384 return next;
Andrew Scull9726c252019-01-23 13:44:19 +0000385}
386
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000387/*
388 * Format the partition info descriptors according to the version supported
389 * by the endpoint and return the size of the array created.
390 */
391static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000392 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000393 uint32_t vm_count)
394{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000395 struct vm *vm = vm_locked.vm;
Karl Meakin0e617d92024-04-05 12:55:22 +0100396 enum ffa_version version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100397 uint32_t partition_info_size;
398 uint32_t buffer_size;
Federico Recanati644f0462022-03-17 12:04:00 +0100399 struct ffa_value ret;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000400
J-Alvese8c8c2b2022-12-16 15:34:48 +0000401 /* Acquire receiver's RX buffer. */
402 if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) {
403 dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id);
404 return ret;
405 }
406
J-Alves122f1a12022-12-12 15:55:42 +0000407 if (vm_is_mailbox_busy(vm_locked)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000408 /*
409 * Can't retrieve memory information if the mailbox is not
410 * available.
411 */
412 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000413 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000414 }
415
Karl Meakin0e617d92024-04-05 12:55:22 +0100416 if (version == FFA_VERSION_1_0) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000417 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
418
Daniel Boulby835e1592022-05-30 17:38:51 +0100419 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
420 buffer_size = partition_info_size * vm_count;
421 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000422 dlog_error(
423 "Partition information does not fit in the "
J-Alves0a21db12024-03-28 12:43:30 +0000424 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000425 return ffa_error(FFA_NO_MEMORY);
426 }
427
428 for (uint32_t i = 0; i < vm_count; i++) {
429 /*
430 * Populate the VM's RX buffer with the partition
Kathleen Capella402fa852022-11-09 16:16:51 -0500431 * information. Clear properties bits that must be zero
432 * according to DEN0077A FF-A v1.0 REL Table 8.25.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000433 */
434 recv_mailbox[i].vm_id = partitions[i].vm_id;
435 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
Kathleen Capella402fa852022-11-09 16:16:51 -0500436 recv_mailbox[i].properties =
437 partitions[i].properties &
438 ~FFA_PARTITION_v1_0_RES_MASK;
Daniel Boulby191b5f02022-02-17 17:26:14 +0000439 }
440
441 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100442 partition_info_size = sizeof(struct ffa_partition_info);
443 buffer_size = partition_info_size * vm_count;
J-Alves0a21db12024-03-28 12:43:30 +0000444
Daniel Boulby835e1592022-05-30 17:38:51 +0100445 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000446 dlog_error(
447 "Partition information does not fit in the "
J-Alves0a824e92024-04-26 16:20:12 +0100448 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000449 return ffa_error(FFA_NO_MEMORY);
450 }
451
J-Alves0a21db12024-03-28 12:43:30 +0000452 /*
453 * Populate the VM's RX buffer with the partition information.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000454 */
J-Alves0a21db12024-03-28 12:43:30 +0000455 if (!memcpy_trapped(vm->mailbox.recv, HF_MAILBOX_SIZE,
456 partitions, buffer_size)) {
457 dlog_error(
458 "%s: Failed to copy ffa_partition_info "
459 "descriptor\n",
460 __func__);
461 return ffa_error(FFA_ABORTED);
462 }
Daniel Boulby191b5f02022-02-17 17:26:14 +0000463 }
464
Daniel Boulby835e1592022-05-30 17:38:51 +0100465 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000466
467 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000468 vm->mailbox.recv_sender = HF_VM_ID_BASE;
469 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000470 vm->mailbox.state = MAILBOX_STATE_FULL;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000471
Daniel Boulby835e1592022-05-30 17:38:51 +0100472 /*
473 * Return the count of partition information descriptors in w2
474 * and the size of the descriptors in w3.
475 */
476 return (struct ffa_value){.func = FFA_SUCCESS_32,
477 .arg2 = vm_count,
478 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000479}
480
J-Alves7fb0fdb2024-10-09 11:28:07 +0100481/**
482 * Set properties accoridng to the version of the partition, and the FF-A
483 * features it supports.
484 */
485static ffa_partition_properties_t api_ffa_partitions_info_get_properties(
486 ffa_id_t caller_id, struct vm *vm)
487{
488 ffa_partition_properties_t properties;
489
490 properties = plat_ffa_partition_properties(caller_id, vm);
491 properties |= FFA_PARTITION_AARCH64_EXEC;
492
493 if (vm->ffa_version >= FFA_VERSION_1_1) {
494 properties |= vm_are_notifications_enabled(vm)
495 ? FFA_PARTITION_NOTIFICATION
496 : 0;
497 properties |= vm->messaging_method & FFA_PARTITION_INDIRECT_MSG;
498 }
499
Karl Meakina603e082024-08-02 17:57:27 +0100500 /*
501 * Only populate on calls from normal world,
502 * and if SP supports receiving direct message requests.
503 */
504 if (ffa_is_vm_id(caller_id) &&
505 (properties & FFA_PARTITION_DIRECT_REQ_RECV) != 0) {
506 if (vm->vm_availability_messages.vm_created) {
507 properties |= FFA_PARTITION_VM_CREATED;
508 }
509 if (vm->vm_availability_messages.vm_destroyed) {
510 properties |= FFA_PARTITION_VM_DESTROYED;
511 }
512 }
J-Alves7fb0fdb2024-10-09 11:28:07 +0100513 return properties;
514}
515
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100516static void api_ffa_fill_partition_info(
J-Alves7fb0fdb2024-10-09 11:28:07 +0100517 struct ffa_partition_info *out_partition, struct vm *vm,
518 ffa_id_t caller_id)
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800519{
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100520 out_partition->vm_id = vm->id;
521 out_partition->vcpu_count = vm->vcpu_count;
J-Alves7fb0fdb2024-10-09 11:28:07 +0100522 out_partition->properties =
523 api_ffa_partitions_info_get_properties(caller_id, vm);
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100524}
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800525
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100526/**
527 * Find VMs with UUID matching `uuid_to_find` , and fill `out_partitions` with
528 * partition infos. Returns number of VMs that matched.
529 * A null UUID matches against any VM.
530 * If `count_flag` is true, no partition infos are written to `out_partitions`,
531 * only the number of VMs that matched is returned.
532 */
533static ffa_vm_count_t api_ffa_fill_partitions_info_array(
534 struct ffa_partition_info out_partitions[], size_t out_partitions_len,
J-Alves7fb0fdb2024-10-09 11:28:07 +0100535 const struct ffa_uuid *uuid_to_find, bool count_flag,
536 ffa_id_t caller_id)
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100537{
538 ffa_vm_count_t vms_found = 0;
539 bool match_any = ffa_uuid_is_null(uuid_to_find);
540
541 assert(vm_get_count() <= out_partitions_len);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800542
543 /*
544 * Iterate through the VMs to find the ones with a matching
545 * UUID. A Null UUID retrieves information for all VMs.
546 */
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100547 for (ffa_vm_count_t vm_idx = 0; vm_idx < vm_get_count(); vm_idx++) {
548 struct vm *vm = vm_find_index(vm_idx);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800549
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100550 for (size_t uuid_idx = 0; uuid_idx < PARTITION_MAX_UUIDS;
551 uuid_idx++) {
552 struct ffa_uuid uuid = vm->uuids[uuid_idx];
553 struct ffa_partition_info *out_partition =
554 &out_partitions[vms_found];
555
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400556 /*
557 * Null UUID indicates reaching the end of a
558 * partition's array of UUIDs.
559 */
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100560 if (ffa_uuid_is_null(&uuid)) {
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400561 break;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800562 }
563
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100564 if (match_any || ffa_uuid_equal(uuid_to_find, &uuid)) {
565 vms_found++;
566
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400567 if (count_flag) {
568 continue;
569 }
570
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100571 api_ffa_fill_partition_info(out_partition, vm,
J-Alves7fb0fdb2024-10-09 11:28:07 +0100572 caller_id);
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100573 if (match_any) {
574 out_partition->uuid = uuid;
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400575 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800576 }
577 }
578 }
579
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100580 return vms_found;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800581}
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800582
583static inline void api_ffa_pack_vmid_count_props(
J-Alves19e20cf2023-08-02 12:48:55 +0100584 uint64_t *xn, ffa_id_t vm_id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800585 ffa_partition_properties_t properties)
586{
587 *xn = (uint64_t)vm_id;
588 *xn |= (uint64_t)vcpu_count << 16;
589 *xn |= (uint64_t)properties << 32;
590}
591
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700592/**
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700593 * This function forwards the FFA_PARTITION_INFO_GET_REGS ABI to the other world
594 * when hafnium is the hypervisor to determine the secure partitions. When
595 * hafnium is the SPMC, this function forwards the call to the SPMD to discover
596 * SPMD logical partitions. The function returns true when partition information
597 * is filled in the partitions array and false if there are errors. Note that
598 * the SPMD and SPMC may return an FF-A error code of FFA_NOT_SUPPORTED when
599 * there are no SPMD logical partitions or no secure partitions respectively,
600 * and this is not considered a failure of the forwarded call. A caller is
601 * expected to check the return value before consuming the information in the
602 * partitions array passed in and ret_count.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700603 */
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700604static bool api_ffa_partition_info_get_regs_forward(
605 const struct ffa_uuid *uuid, const uint16_t tag,
606 struct ffa_partition_info *partitions, uint16_t partitions_len,
607 ffa_vm_count_t *ret_count)
608{
609 (void)tag;
610 struct ffa_value ret;
611 uint16_t last_index = UINT16_MAX;
612 uint16_t curr_index = 0;
613 uint16_t start_index = 0;
614
615 if (!plat_ffa_partition_info_get_regs_forward_allowed()) {
616 return true;
617 }
618
619 while (start_index <= last_index) {
620 ret = ffa_partition_info_get_regs(uuid, start_index, 0);
621 if (ffa_func_id(ret) != FFA_SUCCESS_64) {
622 /*
623 * If there are no logical partitions, SPMD returns
624 * NOT_SUPPORTED, that is not an error. If there are no
625 * secure partitions the SPMC returns NOT_SUPPORTED.
626 */
627 if ((ffa_func_id(ret) == FFA_ERROR_32) &&
628 (ffa_error_code(ret) == FFA_NOT_SUPPORTED)) {
629 return true;
630 }
631
632 return false;
633 }
634
635 if (!api_ffa_fill_partition_info_from_regs(
636 ret, start_index, partitions, partitions_len,
637 ret_count)) {
638 return false;
639 }
640
641 last_index = ffa_partition_info_regs_get_last_idx(ret);
642 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
643 start_index = curr_index + 1;
644 }
645 return true;
646}
647
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700648bool api_ffa_fill_partition_info_from_regs(
649 struct ffa_value ret, uint16_t start_index,
650 struct ffa_partition_info *partitions, uint16_t partitions_len,
651 ffa_vm_count_t *ret_count)
652{
653 uint16_t vm_count = *ret_count;
654 uint16_t curr_index = 0;
655 uint8_t num_entries = 0;
656 uint8_t idx = 0;
657 /* List of pointers to args in return value. */
658 uint64_t *arg_ptrs[] = {
659 &ret.arg3,
660 &ret.arg4,
661 &ret.arg5,
662 &ret.arg6,
663 &ret.arg7,
664 &ret.extended_val.arg8,
665 &ret.extended_val.arg9,
666 &ret.extended_val.arg10,
667 &ret.extended_val.arg11,
668 &ret.extended_val.arg12,
669 &ret.extended_val.arg13,
670 &ret.extended_val.arg14,
671 &ret.extended_val.arg15,
672 &ret.extended_val.arg16,
673 &ret.extended_val.arg17,
674 };
675
676 if (vm_count > partitions_len) {
677 return false;
678 }
679
680 /*
681 * Tags are currently unused in the implementation. Expect it to be
682 * zero since the implementation does not provide a tag when calling
683 * the FFA_PARTITION_INFO_GET_REGS ABI.
684 */
685 assert(ffa_partition_info_regs_get_tag(ret) == 0);
686
687 /*
688 * Hafnium expects the size of the returned descriptor to be equal to
689 * the size of the structure in the FF-A 1.1 specification. When future
690 * enhancements are made, this assert can be relaxed.
691 */
692 assert(ffa_partition_info_regs_get_desc_size(ret) ==
693 sizeof(struct ffa_partition_info));
694
695 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
696
697 /* FF-A 1.2 ALP0, section 14.9.2 Usage rule 7. */
698 assert(start_index <= curr_index);
699
700 num_entries = curr_index - start_index + 1;
701 if (num_entries > (partitions_len - vm_count) ||
702 num_entries > MAX_INFO_REGS_ENTRIES_PER_CALL) {
703 return false;
704 }
705
706 while (num_entries) {
707 uint64_t info = *(arg_ptrs[(ptrdiff_t)(idx++)]);
708 uint64_t uuid_lo = *(arg_ptrs[(ptrdiff_t)(idx++)]);
709 uint64_t uuid_high = *(arg_ptrs[(ptrdiff_t)(idx++)]);
710
711 partitions[vm_count].vm_id = info & 0xFFFF;
712 partitions[vm_count].vcpu_count = (info >> 16) & 0xFFFF;
713 partitions[vm_count].properties = (info >> 32);
714 partitions[vm_count].uuid.uuid[0] = uuid_lo & 0xFFFFFFFF;
715 partitions[vm_count].uuid.uuid[1] =
716 (uuid_lo >> 32) & 0xFFFFFFFF;
717 partitions[vm_count].uuid.uuid[2] = uuid_high & 0xFFFFFFFF;
718 partitions[vm_count].uuid.uuid[3] =
719 (uuid_high >> 32) & 0xFFFFFFFF;
720 vm_count++;
721 num_entries--;
722 }
723
724 *ret_count = vm_count;
725 return true;
726}
727
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800728struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current,
729 const struct ffa_uuid *uuid,
730 const uint16_t start_index,
731 const uint16_t tag)
732{
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800733 struct vm *current_vm = current->vm;
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800734 static struct ffa_partition_info partitions[2 * MAX_VMS];
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800735 bool uuid_is_null = ffa_uuid_is_null(uuid);
736 ffa_vm_count_t vm_count = 0;
737 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
738 uint16_t max_idx = 0;
739 uint16_t curr_idx = 0;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800740 uint8_t num_entries_to_ret = 0;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800741 uint8_t arg_idx = 3;
742
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800743 /* list of pointers to args in return value */
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800744 uint64_t *arg_ptrs[] = {
745 &(ret).func,
746 &(ret).arg1,
747 &(ret).arg2,
748 &(ret).arg3,
749 &(ret).arg4,
750 &(ret).arg5,
751 &(ret).arg6,
752 &(ret).arg7,
753 &(ret).extended_val.arg8,
754 &(ret).extended_val.arg9,
755 &(ret).extended_val.arg10,
756 &(ret).extended_val.arg11,
757 &(ret).extended_val.arg12,
758 &(ret).extended_val.arg13,
759 &(ret).extended_val.arg14,
760 &(ret).extended_val.arg15,
761 &(ret).extended_val.arg16,
762 &(ret).extended_val.arg17,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800763 };
764
765 /* TODO: Add support for using tags */
766 if (tag != 0) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800767 dlog_error("Tag not 0. Unsupported tag. %d\n", tag);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800768 return ffa_error(FFA_RETRY);
769 }
770
771 memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions));
772
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100773 vm_count = api_ffa_fill_partitions_info_array(
774 partitions, ARRAY_SIZE(partitions), uuid, false,
775 current_vm->id);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800776
777 /* If UUID is Null vm_count must not be zero at this stage. */
778 CHECK(!uuid_is_null || vm_count != 0);
779
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800780 /*
781 * When running the Hypervisor:
782 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
783 * it to fill with secure partitions information.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700784 * When running the SPMC, the SPMC forwards the call to the SPMD to
785 * discover any EL3 SPMD logical partitions, if the call came from an
786 * SP. Otherwise, the call is not forwarded.
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800787 * TODO: Note that for this ABI, forwarding on every invocation when
788 * uuid is Null is inefficient,and if performance becomes a problem,
789 * this would be a good place to optimize using strategies such as
790 * caching info etc. For now, assuming this inefficiency is not a major
791 * issue.
792 * - If UUID is non-Null vm_count may be zero because the UUID matches
793 * a secure partition and the query is forwarded to the SPMC.
794 * When running the SPMC:
795 * - If UUID is non-Null and vm_count is zero it means there is no such
796 * partition identified in the system.
797 */
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700798 if (vm_id_is_current_world(current_vm->id)) {
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700799 if (!api_ffa_partition_info_get_regs_forward(
800 uuid, tag, partitions, ARRAY_SIZE(partitions),
801 &vm_count)) {
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700802 dlog_error(
803 "Failed to forward "
804 "ffa_partition_info_get_regs.\n");
805 return ffa_error(FFA_DENIED);
806 }
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800807 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800808
809 /*
810 * Unrecognized UUID: does not match any of the VMs (or SPs)
811 * and is not Null.
812 */
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800813 if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) {
Olivier Deprez89da0a42023-09-01 17:38:33 +0200814 dlog_verbose(
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800815 "Invalid parameters. vm_count = %d (must not be zero "
Karl Meakine8937d92024-03-19 16:04:25 +0000816 "or > %lu)\n",
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800817 vm_count, ARRAY_SIZE(partitions));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800818 return ffa_error(FFA_INVALID_PARAMETERS);
819 }
820
821 if (start_index >= vm_count) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800822 dlog_error(
823 "start index = %d vm_count = %d (start_index must be "
824 "less than vm_count)\n",
825 start_index, vm_count);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800826 return ffa_error(FFA_INVALID_PARAMETERS);
827 }
828
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800829 max_idx = vm_count - 1;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800830 num_entries_to_ret = (max_idx - start_index) + 1;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800831 num_entries_to_ret =
832 MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800833 curr_idx = start_index + num_entries_to_ret - 1;
834 assert(curr_idx <= max_idx);
835
836 ret.func = FFA_SUCCESS_64;
837 ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48;
838 ret.arg2 |= curr_idx << 16;
839 ret.arg2 |= max_idx;
840
841 if (num_entries_to_ret > 1) {
842 ret.extended_val.valid = 1;
843 }
844
845 for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800846 uint64_t *xn_0 = arg_ptrs[arg_idx++];
847 uint64_t *xn_1 = arg_ptrs[arg_idx++];
848 uint64_t *xn_2 = arg_ptrs[arg_idx++];
849
850 api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id,
851 partitions[idx].vcpu_count,
852 partitions[idx].properties);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800853 if (uuid_is_null) {
Karl Meakin9478e322024-09-23 17:47:09 +0100854 ffa_uuid_to_u64x2(xn_1, xn_2, &partitions[idx].uuid);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800855 }
Raghu Krishnamurthycded31b2023-04-23 15:07:35 -0700856 assert(arg_idx <= ARRAY_SIZE(arg_ptrs));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800857 }
858
859 return ret;
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800860}
861
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100862struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000863 const struct ffa_uuid *uuid,
864 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100865{
866 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100867 ffa_vm_count_t vm_count = 0;
Olivier Deprez013f4d62022-11-21 15:46:20 +0100868 bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) ==
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000869 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100870 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100871 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000872 struct vm_locked vm_locked;
873 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100874
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000875 /* Bits 31:1 Must Be Zero */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100876 if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) {
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000877 return ffa_error(FFA_INVALID_PARAMETERS);
878 }
879
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100880 vm_count = api_ffa_fill_partitions_info_array(
881 partitions, ARRAY_SIZE(partitions), uuid, count_flag,
882 current_vm->id);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100883
Olivier Depreze562e542020-06-11 17:31:54 +0200884 /* If UUID is Null vm_count must not be zero at this stage. */
885 CHECK(!uuid_is_null || vm_count != 0);
886
887 /*
888 * When running the Hypervisor:
889 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
890 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000891 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200892 * a secure partition and the query is forwarded to the SPMC.
893 * When running the SPMC:
894 * - If UUID is non-Null and vm_count is zero it means there is no such
895 * partition identified in the system.
896 */
Karl Meakin86c80272024-07-26 15:13:23 +0100897 vm_count = plat_ffa_partition_info_get_forward(uuid, flags, partitions,
898 vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200899
900 /*
901 * Unrecognized UUID: does not match any of the VMs (or SPs)
902 * and is not Null.
903 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100904 if (vm_count == 0) {
905 return ffa_error(FFA_INVALID_PARAMETERS);
906 }
907
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000908 /*
909 * If the count flag is set we don't need to return the partition info
910 * descriptors.
911 */
912 if (count_flag) {
913 return (struct ffa_value){.func = FFA_SUCCESS_32,
914 .arg2 = vm_count};
915 }
916
Daniel Boulby191b5f02022-02-17 17:26:14 +0000917 vm_locked = vm_lock(current_vm);
918 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
919 vm_count);
920 vm_unlock(&vm_locked);
921 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100922}
923
Andrew Scull9726c252019-01-23 13:44:19 +0000924/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000925 * Returns the ID of the VM.
926 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100927struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000928{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100929 return (struct ffa_value){.func = FFA_SUCCESS_32,
930 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000931}
932
933/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200934 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
935 * FF-A instances.
936 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000937 */
938struct ffa_value api_ffa_spm_id_get(void)
939{
Karl Meakin0e617d92024-04-05 12:55:22 +0100940 if (FFA_VERSION_1_1 <= FFA_VERSION_COMPILED) {
941 /*
942 * Return the SPMC ID that was fetched during FF-A
943 * initialization.
944 */
945 return (struct ffa_value){.func = FFA_SUCCESS_32,
946 .arg2 = arch_ffa_spmc_id_get()};
947 }
948
J-Alves3829fc02021-03-18 12:49:18 +0000949 return ffa_error(FFA_NOT_SUPPORTED);
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000950}
951
952/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000953 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000954 * function to indicate that register state for the given vCPU has been saved
955 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000956 */
957void api_regs_state_saved(struct vcpu *vcpu)
958{
959 sl_lock(&vcpu->lock);
960 vcpu->regs_available = true;
961 sl_unlock(&vcpu->lock);
962}
J-Alves0ffce752024-10-09 14:37:10 +0100963
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000964/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000965 * Assuming that the arguments have already been checked by the caller, injects
966 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
967 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
968 * is next run, which is up to the scheduler.
969 *
970 * Returns:
971 * - 0 on success if no further action is needed.
972 * - 1 if it was called by the primary VM and the primary VM now needs to wake
973 * up or kick the target vCPU.
974 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100975int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600976 uint32_t intid,
977 struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100978 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000979{
Manish Pandey35e452f2021-02-18 21:36:34 +0000980 struct vcpu *target_vcpu = target_locked.vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600981 struct vcpu *current = current_locked.vcpu;
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100982 struct interrupts *interrupts = &target_vcpu->interrupts;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000983 int64_t ret = 0;
984
Andrew Walbran508e63c2018-12-20 17:02:37 +0000985 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000986 * We only need to change state and (maybe) trigger a virtual interrupt
987 * if it is enabled and was not previously pending. Otherwise we can
988 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000989 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100990 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
991 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000992 goto out;
993 }
994
995 /* Increment the count. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100996 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000997
998 /*
999 * Only need to update state if there was not already an
1000 * interrupt enabled and pending.
1001 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001002 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +00001003 goto out;
1004 }
1005
Karl Meakin5e996992024-05-20 11:27:07 +01001006 if (vm_is_primary(current->vm)) {
Andrew Walbran508e63c2018-12-20 17:02:37 +00001007 /*
1008 * If the call came from the primary VM, let it know that it
1009 * should run or kick the target vCPU.
1010 */
1011 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +00001012 } else if (current != target_vcpu && next != NULL) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001013 *next = api_wake_up_locked(current_locked, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001014 }
1015
1016out:
1017 /* Either way, make it pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01001018 vcpu_virt_interrupt_set_pending(interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001019
Olivier Deprezc19a6ea2020-08-06 11:16:07 +02001020 return ret;
1021}
1022
Andrew Walbran508e63c2018-12-20 17:02:37 +00001023/**
J-Alves2ced1672022-12-12 14:35:38 +00001024 * Constructs the return value from a successful FFA_MSG_WAIT call, when used
1025 * with FFA_MSG_SEND_32.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001026 */
J-Alves27b71962022-12-12 15:29:58 +00001027struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001028{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001029 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001030 case FFA_MSG_SEND_32:
1031 return (struct ffa_value){
1032 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001033 .arg1 = (receiver->mailbox.recv_sender << 16) |
1034 receiver->id,
1035 .arg3 = receiver->mailbox.recv_size};
J-Alvesd9e7c8f2024-09-11 13:37:25 +01001036 default:
Federico Recanati25053ee2022-03-14 15:01:53 +01001037 return (struct ffa_value){
1038 .func = FFA_RUN_32,
1039 /*
1040 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
1041 * Retrieving vCPU requires a rework of the function,
1042 * while receiver ID must be set because it's checked by
1043 * other APIs (eg: FFA_NOTIFICATION_GET).
1044 */
1045 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001046 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001047}
1048
J-Alvese8c8c2b2022-12-16 15:34:48 +00001049/**
1050 * Change the state of mailbox to empty, such that the ownership is given to the
1051 * Partition manager.
Karl Meakin1064a9c2024-06-04 17:20:43 +01001052 * Returns FFA_SUCCESS if the mailbox was reset successfully, FFA_ERROR
1053 * otherwise.
J-Alvese8c8c2b2022-12-16 15:34:48 +00001054 */
Karl Meakin1064a9c2024-06-04 17:20:43 +01001055static struct ffa_value api_release_mailbox(struct vm_locked vm_locked)
J-Alvese8c8c2b2022-12-16 15:34:48 +00001056{
Karl Meakin1064a9c2024-06-04 17:20:43 +01001057 struct ffa_value ret = {.func = FFA_SUCCESS_32};
J-Alves19e20cf2023-08-02 12:48:55 +01001058 ffa_id_t vm_id = vm_locked.vm->id;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001059
1060 switch (vm_locked.vm->mailbox.state) {
1061 case MAILBOX_STATE_EMPTY:
1062 dlog_verbose("Mailbox of %x is empty.\n", vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001063 ret = ffa_error(FFA_DENIED);
J-Alves31f8bef2023-07-14 12:46:28 +01001064 break;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001065 case MAILBOX_STATE_FULL:
1066 /* Check it doesn't have pending RX full notifications. */
1067 if (vm_are_fwk_notifications_pending(vm_locked)) {
1068 dlog_verbose(
1069 "Mailbox of endpoint %x has pending "
1070 "messages.\n",
1071 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001072 ret = ffa_error(FFA_DENIED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001073 }
1074 break;
1075 case MAILBOX_STATE_OTHER_WORLD_OWNED:
1076 /*
1077 * The SPMC shouldn't let SP's mailbox get into this state.
1078 * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst
1079 * the mailbox is in this state. In that case, we should report
1080 * error.
1081 */
1082 if (vm_id_is_current_world(vm_id)) {
1083 dlog_verbose(
Karl Meakin1064a9c2024-06-04 17:20:43 +01001084 "Mailbox of endpoint %x is in an incorrect "
1085 "state.\n",
J-Alvese8c8c2b2022-12-16 15:34:48 +00001086 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001087 ret = ffa_error(FFA_ABORTED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001088 }
1089 break;
1090 }
1091
Karl Meakin1064a9c2024-06-04 17:20:43 +01001092 if (ret.func == FFA_SUCCESS_32) {
1093 vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001094 }
1095
Karl Meakin1064a9c2024-06-04 17:20:43 +01001096 return ret;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001097}
1098
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001099/*
1100 * Helper to check if extended arguments (corresponding to regs x8-x17)
1101 * are zeroed out.
1102 */
Kathleen Capella036cc592023-11-30 18:26:15 -05001103bool api_extended_args_are_zero(struct ffa_value *args)
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001104{
1105 if (args->extended_val.arg8 != 0U || args->extended_val.arg9 != 0U ||
1106 args->extended_val.arg10 != 0U || args->extended_val.arg11 != 0U ||
1107 args->extended_val.arg12 != 0U || args->extended_val.arg13 != 0U ||
1108 args->extended_val.arg14 != 0U || args->extended_val.arg15 != 0U ||
1109 args->extended_val.arg16 != 0U || args->extended_val.arg17 != 0U) {
1110 return false;
1111 }
1112 return true;
1113}
1114
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001115static void api_ffa_msg_wait_rx_release(struct vcpu *current)
1116{
1117 struct vm_locked vm_locked;
1118
1119 vm_locked = plat_ffa_vm_find_locked(current->vm->id);
1120 if (vm_locked.vm == NULL) {
1121 return;
1122 }
1123
1124 api_release_mailbox(vm_locked);
1125
1126 if (vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY) {
1127 dlog_warning("Mailbox not released to producer\n");
1128 }
1129
1130 vm_unlock(&vm_locked);
1131}
1132
Kathleen Capella7253bd52024-08-14 17:36:09 -04001133static bool api_retain_rx_buffer_ownership(struct ffa_value args)
1134{
1135 return ((args.arg2 & FFA_MSG_WAIT_FLAG_RETAIN_RX) != 0U);
1136}
1137
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001138struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
1139 struct ffa_value *args)
1140{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001141 struct vcpu_locked current_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001142 enum vcpu_state next_state = VCPU_STATE_RUNNING;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001143 struct ffa_value ret;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001144 struct vcpu_locked next_locked = (struct vcpu_locked){
1145 .vcpu = NULL,
1146 };
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001147
Kathleen Capella7253bd52024-08-14 17:36:09 -04001148 if (args->arg1 != 0U || args->arg3 != 0U || args->arg4 != 0U ||
1149 args->arg5 != 0U || args->arg6 != 0U || args->arg7 != 0U) {
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001150 return ffa_error(FFA_INVALID_PARAMETERS);
1151 }
1152
Kathleen Capella7253bd52024-08-14 17:36:09 -04001153 if (current->vm->ffa_version >= FFA_VERSION_1_2) {
1154 if (!api_extended_args_are_zero(args)) {
1155 return ffa_error(FFA_INVALID_PARAMETERS);
1156 }
1157 } else {
1158 if (args->arg2 != 0U) {
1159 return ffa_error(FFA_INVALID_PARAMETERS);
1160 }
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001161 }
1162
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001163 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001164 if (!plat_ffa_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001165 current_locked, current->vm->id, HF_INVALID_VM_ID,
1166 next_locked, FFA_MSG_WAIT_32, &next_state)) {
1167 ret = ffa_error(FFA_DENIED);
1168 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001169 }
1170
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001171 assert(!vm_id_is_current_world(current->vm->id) ||
1172 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001173
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001174 ret = plat_ffa_msg_wait_prepare(current_locked, next);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001175
1176 /*
1177 * To maintain partial ordering of locks, release vCPU lock before
1178 * releasing the VM's RX buffer, a process which requires locking the
1179 * VM.
1180 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001181out:
1182 vcpu_unlock(&current_locked);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001183
Kathleen Capella7253bd52024-08-14 17:36:09 -04001184 if (ret.func != FFA_ERROR_32 &&
1185 !api_retain_rx_buffer_ownership(*args)) {
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001186 api_ffa_msg_wait_rx_release(current);
1187 }
J-Alvese8c8c2b2022-12-16 15:34:48 +00001188 return ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001189}
1190
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001191/**
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05001192 * Inject virtual timer interrupt to next vCPU if its timer has expired.
1193 */
1194static void api_inject_arch_timer_interrupt(struct vcpu_locked current_locked,
1195 struct vcpu_locked next_locked)
1196{
1197 struct vcpu *next = next_locked.vcpu;
1198
1199 if (arch_timer_expired(&next->regs)) {
1200 /* Make virtual timer interrupt pending. */
1201 api_interrupt_inject_locked(next_locked, HF_VIRTUAL_TIMER_INTID,
1202 current_locked, NULL);
1203 vcpu_interrupt_queue_push(next_locked, HF_VIRTUAL_TIMER_INTID);
1204 }
1205}
1206
1207/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001208 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001209 * value needs to be forced onto the vCPU.
1210 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001211static bool api_vcpu_prepare_run(struct vcpu_locked current_locked,
1212 struct vcpu_locked vcpu_next_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001213 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001214{
Max Shvetsov40108e72020-08-27 12:39:50 +01001215 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001216 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001217 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001218 bool vcpu_was_init_state = false;
J-Alves6e2abc62021-12-02 14:58:56 +00001219 bool need_vm_lock;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001220 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001221
J-Alves478faac2024-10-23 10:35:57 +01001222 const struct ffa_value ffa_run_abi =
1223 (struct ffa_value){.func = FFA_RUN_32};
1224 const struct ffa_value *ffa_run_ret = NULL;
1225
Andrew Scullb06d1752019-02-04 10:15:48 +00001226 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001227 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +00001228 *
Andrew Scull4caadaf2019-07-03 13:13:47 +01001229 * The VM lock is not needed in the common case so it must only be taken
1230 * when it is going to be needed. This ensures there are no inter-vCPU
1231 * dependencies in the common run case meaning the sensitive context
1232 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +00001233 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001234 struct vcpu *vcpu = vcpu_next_locked.vcpu;
1235 struct vcpu *current = current_locked.vcpu;
Max Shvetsov40108e72020-08-27 12:39:50 +01001236
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001237 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +00001238 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
1239 (!vcpu->vm->el0_partition &&
1240 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
1241 vcpu->state == VCPU_STATE_BLOCKED ||
1242 vcpu->state == VCPU_STATE_PREEMPTED));
1243
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001244 if (need_vm_lock) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001245 vcpu_unlock(&vcpu_next_locked);
1246 vcpu_unlock(&current_locked);
Max Shvetsov40108e72020-08-27 12:39:50 +01001247 vm_locked = vm_lock(vcpu->vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001248
1249 /* Lock both vCPUs at once to avoid deadlock. */
1250 vcpus_locked = vcpu_lock_both(current, vcpu);
1251 current_locked = vcpus_locked.vcpu1;
1252 vcpu_next_locked = vcpus_locked.vcpu2;
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001253 }
1254
1255 /*
1256 * If the vCPU is already running somewhere then we can't run it here
1257 * simultaneously. While it is actually running then the state should be
1258 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
1259 * stops running but while Hafnium is in the process of switching back
1260 * to the primary there will be a brief period while the state has been
1261 * updated but `regs_available` is still false (until
1262 * `api_regs_state_saved` is called). We can't start running it again
1263 * until this has finished, so count this state as still running for the
1264 * purposes of this check.
1265 */
1266 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
1267 /*
1268 * vCPU is running on another pCPU.
1269 *
1270 * It's okay not to return the sleep duration here because the
1271 * other physical CPU that is currently running this vCPU will
1272 * return the sleep duration if needed.
1273 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001274 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001275 ret = false;
1276 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001277 }
Andrew Scull9726c252019-01-23 13:44:19 +00001278
1279 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001280 if (vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04001281 dlog_verbose("VM %#x was aborted, cannot run vCPU %u\n",
1282 vcpu->vm->id, vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +01001283 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +00001284 }
Kathleen Capella6ab05132023-05-10 12:27:35 -04001285 *run_ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +00001286 ret = false;
1287 goto out;
1288 }
1289
Andrew Walbran508e63c2018-12-20 17:02:37 +00001290 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001291 case VCPU_STATE_RUNNING:
1292 case VCPU_STATE_OFF:
1293 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001294 ret = false;
1295 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001296
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001297 case VCPU_STATE_WAITING:
1298 /*
Olivier Deprezb2808332023-02-02 15:25:40 +01001299 * An initial FFA_RUN is necessary for SP's secondary vCPUs to
1300 * reach the message wait loop.
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001301 */
Olivier Deprezb2808332023-02-02 15:25:40 +01001302 if (vcpu->rt_model == RTM_SP_INIT) {
1303 /*
1304 * TODO: this should be removed, but omitting it makes
1305 * normal world arch gicv3 tests failing.
1306 */
1307 vcpu->rt_model = RTM_NONE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001308
1309 vcpu_was_init_state = true;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001310 break;
1311 }
1312
J-Alves6e2abc62021-12-02 14:58:56 +00001313 assert(need_vm_lock == true);
J-Alves478faac2024-10-23 10:35:57 +01001314 if (!vm_locked.vm->el0_partition) {
1315 plat_ffa_inject_notification_pending_interrupt(
1316 vcpu_next_locked, current_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001317 }
1318
J-Alves478faac2024-10-23 10:35:57 +01001319 /* Provide reference to the return value. */
1320 ffa_run_ret = &ffa_run_abi;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001321
J-Alves478faac2024-10-23 10:35:57 +01001322 break;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001323 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +00001324 if (need_vm_lock &&
1325 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001326 vcpu_next_locked, current_locked, vm_locked)) {
1327 assert(vcpu_interrupt_count_get(vcpu_next_locked) > 0);
J-Alves6e2abc62021-12-02 14:58:56 +00001328 break;
1329 }
1330
Andrew Scullb06d1752019-02-04 10:15:48 +00001331 /* Allow virtual interrupts to be delivered. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001332 if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +00001333 break;
1334 }
1335
Andrew Walbran508e63c2018-12-20 17:02:37 +00001336 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001337 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001338 arch_timer_remaining_ns(&vcpu->regs);
1339
1340 /*
1341 * The timer expired so allow the interrupt to be
1342 * delivered.
1343 */
1344 if (timer_remaining_ns == 0) {
1345 break;
1346 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001347 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001348
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001349 /*
1350 * The vCPU is not ready to run, return the appropriate code to
1351 * the primary which called vcpu_run.
1352 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001353 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001354 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1355 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +00001356
1357 ret = false;
1358 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001359
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001360 case VCPU_STATE_BLOCKED:
1361 /* A blocked vCPU is run unconditionally. Fall through. */
1362 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +00001363 /* Check NPI is to be injected here. */
1364 if (need_vm_lock) {
1365 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001366 vcpu_next_locked, current_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001367 }
Andrew Walbran508e63c2018-12-20 17:02:37 +00001368 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001369 default:
1370 /*
1371 * Execution not expected to reach here. Deny the request
1372 * gracefully.
1373 */
1374 *run_ret = ffa_error(FFA_DENIED);
1375 ret = false;
1376 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001377 }
1378
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001379 plat_ffa_init_schedule_mode_ffa_run(current_locked, vcpu_next_locked);
Madhukar Pappireddy1480fce2022-06-21 18:09:25 -05001380
Madhukar Pappireddy106bfc32024-09-25 15:47:11 -05001381 timer_migrate_to_other_cpu(current_locked.vcpu->cpu, vcpu_next_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001382 vcpu->cpu = current_locked.vcpu->cpu;
J-Alves478faac2024-10-23 10:35:57 +01001383
1384 vcpu_set_running(vcpu_next_locked, ffa_run_ret);
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001385
Olivier Deprez04168ed2022-09-26 09:20:00 +02001386 if (vcpu_was_init_state) {
1387 vcpu_set_phys_core_idx(vcpu);
1388 vcpu_set_boot_info_gp_reg(vcpu);
J-Alves7ac49052022-02-08 17:20:53 +00001389 }
J-Alves7ac49052022-02-08 17:20:53 +00001390
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001391 ret = true;
1392
1393out:
Andrew Scullb06d1752019-02-04 10:15:48 +00001394 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +01001395 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001396 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001397 return ret;
1398}
1399
J-Alves19e20cf2023-08-02 12:48:55 +01001400struct ffa_value api_ffa_run(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001401 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001402{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001403 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001404 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001405 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001406 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001407 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001408 struct vcpu_locked vcpu_next_locked;
1409 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001410
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001411 current_locked = vcpu_lock(current);
1412 if (!plat_ffa_run_checks(current_locked, vm_id, vcpu_idx, &ret, next)) {
1413 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001414 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001415
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001416 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001417 goto out;
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001418 }
1419
Andrew Scull19503262018-09-20 14:48:39 +01001420 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001421 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +01001422 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001423 goto out;
Andrew Scull19503262018-09-20 14:48:39 +01001424 }
1425
Fuad Tabbaed294af2019-12-20 10:43:01 +00001426 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001427 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001428 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001429 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001430
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001431 /*
1432 * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC
1433 * bypasses the intermediate execution contexts and resumes the
1434 * SP execution context that was originally preempted.
1435 */
1436 if (*next != NULL) {
1437 vcpu = *next;
1438 } else {
1439 vcpu = vm_get_vcpu(vm, vcpu_idx);
1440 }
1441
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001442 /*
1443 * Unlock current vCPU to allow it to be locked together with next
1444 * vcpu.
1445 */
1446 vcpu_unlock(&current_locked);
1447
1448 /* Lock both vCPUs at once to avoid deadlock. */
1449 vcpus_locked = vcpu_lock_both(current, vcpu);
1450 current_locked = vcpus_locked.vcpu1;
1451 vcpu_next_locked = vcpus_locked.vcpu2;
1452
1453 if (!plat_ffa_check_runtime_state_transition(
1454 current_locked, current->vm->id, HF_INVALID_VM_ID,
1455 vcpu_next_locked, FFA_RUN_32, &next_state)) {
1456 ret = ffa_error(FFA_DENIED);
1457 goto out_vcpu;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001458 }
1459
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001460 if (!api_vcpu_prepare_run(current_locked, vcpu_next_locked, &ret)) {
1461 goto out_vcpu;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001462 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001463
Fuad Tabbaed294af2019-12-20 10:43:01 +00001464 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001465 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001466
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001467 assert(!vm_id_is_current_world(current->vm->id) ||
1468 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001469 current->state = VCPU_STATE_BLOCKED;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001470
Andrew Scull33fecd32019-01-08 14:48:27 +00001471 /*
1472 * Set a placeholder return code to the scheduler. This will be
1473 * overwritten when the switch back to the primary occurs.
1474 */
J-Alvesbd32c972024-02-02 16:20:04 +00001475 ret = api_ffa_interrupt_return(0);
Andrew Scull33fecd32019-01-08 14:48:27 +00001476
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001477out_vcpu:
1478 vcpu_unlock(&vcpu_next_locked);
1479
Andrew Scull6d2db332018-10-10 15:28:17 +01001480out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001481 vcpu_unlock(&current_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001482 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001483}
1484
1485/**
Andrew Scull81e85092018-12-12 12:56:20 +00001486 * Check that the mode indicates memory that is valid, owned and exclusive.
1487 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001488static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001489{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001490 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1491 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001492}
1493
1494/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001495 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001496 */
Karl Meakin738bee12024-01-12 15:19:45 +00001497static struct ffa_value api_vm_configure_stage1(
1498 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1499 paddr_t pa_send_begin, paddr_t pa_send_end, paddr_t pa_recv_begin,
1500 paddr_t pa_recv_end, uint32_t extra_attributes,
1501 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001502{
Karl Meakin738bee12024-01-12 15:19:45 +00001503 struct ffa_value ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001504
Karl Meakin738bee12024-01-12 15:19:45 +00001505 /*
1506 * Map the send page as read-only in the SPMC/hypervisor address space.
1507 */
Andrew Sculle1322792019-07-01 17:46:10 +01001508 vm_locked.vm->mailbox.send =
1509 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001510 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001511 if (!vm_locked.vm->mailbox.send) {
Karl Meakin738bee12024-01-12 15:19:45 +00001512 ret = ffa_error(FFA_NO_MEMORY);
1513 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001514 }
1515
1516 /*
Karl Meakin738bee12024-01-12 15:19:45 +00001517 * Map the receive page as writable in the SPMC/hypervisor address
1518 * space. On failure, unmap the send page before returning.
Andrew Sculle1322792019-07-01 17:46:10 +01001519 */
1520 vm_locked.vm->mailbox.recv =
1521 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001522 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001523 if (!vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001524 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001525 goto fail_undo_send;
1526 }
1527
Karl Meakin738bee12024-01-12 15:19:45 +00001528 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001529 goto out;
1530
1531 /*
1532 * The following mappings will not require more memory than is available
1533 * in the local pool.
1534 */
1535fail_undo_send:
1536 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001537 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1538 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001539
Andrew Sculle1322792019-07-01 17:46:10 +01001540out:
Andrew Sculle1322792019-07-01 17:46:10 +01001541 return ret;
1542}
1543
1544/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001545 * Sanity checks and configures the send and receive pages in the VM stage-2
1546 * and hypervisor stage-1 page tables.
1547 *
1548 * Returns:
1549 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001550 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001551 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1552 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001553 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001554 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001555 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001556
1557struct ffa_value api_vm_configure_pages(
1558 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1559 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1560 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001561{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001562 struct ffa_value ret;
1563 paddr_t pa_send_begin;
1564 paddr_t pa_send_end;
1565 paddr_t pa_recv_begin;
1566 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001567 uint32_t orig_send_mode = 0;
1568 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001569 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001570
1571 /* We only allow these to be setup once. */
1572 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001573 dlog_error("%s: Mailboxes have already been setup for VM %#x\n",
1574 __func__, vm_locked.vm->id);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001575 ret = ffa_error(FFA_DENIED);
1576 goto out;
1577 }
1578
1579 /* Hafnium only supports a fixed size of RX/TX buffers. */
1580 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
Karl Meakine8937d92024-03-19 16:04:25 +00001581 dlog_error("%s: Page count must be %zu, it is %d\n", __func__,
Karl Meakin738bee12024-01-12 15:19:45 +00001582 HF_MAILBOX_SIZE / FFA_PAGE_SIZE, page_count);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001583 ret = ffa_error(FFA_INVALID_PARAMETERS);
1584 goto out;
1585 }
1586
1587 /* Fail if addresses are not page-aligned. */
1588 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1589 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001590 dlog_error("%s: Mailbox buffers not page-aligned\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001591 ret = ffa_error(FFA_INVALID_PARAMETERS);
1592 goto out;
1593 }
1594
1595 /* Convert to physical addresses. */
1596 pa_send_begin = pa_from_ipa(send);
1597 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1598 pa_recv_begin = pa_from_ipa(recv);
1599 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1600
1601 /* Fail if the same page is used for the send and receive pages. */
1602 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001603 dlog_error("%s: Mailbox buffers overlap\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001604 ret = ffa_error(FFA_INVALID_PARAMETERS);
1605 goto out;
1606 }
Andrew Sculle1322792019-07-01 17:46:10 +01001607
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001608 /* Set stage 2 translation tables only for virtual FF-A instances. */
1609 if (vm_id_is_current_world(vm_locked.vm->id)) {
1610 /*
1611 * Ensure the pages are valid, owned and exclusive to the VM and
1612 * that the VM has the required access to the memory.
1613 */
1614 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1615 &orig_send_mode) ||
1616 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1617 (orig_send_mode & MM_MODE_R) == 0 ||
1618 (orig_send_mode & MM_MODE_W) == 0) {
1619 dlog_error(
1620 "VM doesn't have required access rights to map "
1621 "TX buffer in stage 2.\n");
1622 ret = ffa_error(FFA_INVALID_PARAMETERS);
1623 goto out;
1624 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001625
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001626 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1627 &orig_recv_mode) ||
1628 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1629 (orig_recv_mode & MM_MODE_R) == 0) {
1630 dlog_error(
1631 "VM doesn't have required access rights to map "
1632 "RX buffer in stage 2.\n");
1633 ret = ffa_error(FFA_INVALID_PARAMETERS);
1634 goto out;
1635 }
Andrew Sculle1322792019-07-01 17:46:10 +01001636
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001637 /* Take memory ownership away from the VM and mark as shared. */
1638 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1639 MM_MODE_W;
1640 if (vm_locked.vm->el0_partition) {
1641 mode |= MM_MODE_USER | MM_MODE_NG;
1642 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001643
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001644 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1645 mode, local_page_pool, NULL)) {
1646 dlog_error(
1647 "Cannot allocate a new entry in stage 2 "
1648 "translation table.\n");
1649 ret = ffa_error(FFA_NO_MEMORY);
1650 goto out;
1651 }
Andrew Sculle1322792019-07-01 17:46:10 +01001652
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001653 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1654 if (vm_locked.vm->el0_partition) {
1655 mode |= MM_MODE_USER | MM_MODE_NG;
1656 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001657
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001658 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1659 mode, local_page_pool, NULL)) {
1660 /* TODO: partial defrag of failed range. */
1661 /* Recover any memory consumed in failed mapping. */
Karl Meakin738bee12024-01-12 15:19:45 +00001662 dlog_error("%s: cannot map recv page\n", __func__);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001663 vm_ptable_defrag(vm_locked, local_page_pool);
Karl Meakin738bee12024-01-12 15:19:45 +00001664 ret = ffa_error(FFA_NO_MEMORY);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001665 goto fail_undo_send;
1666 }
Karl Meakin738bee12024-01-12 15:19:45 +00001667 } else {
1668 ret = arch_other_world_vm_configure_rxtx_map(
1669 vm_locked, local_page_pool, pa_send_begin, pa_send_end,
1670 pa_recv_begin, pa_recv_end);
1671 if (ret.func != FFA_SUCCESS_32) {
1672 goto out;
1673 }
Andrew Sculle1322792019-07-01 17:46:10 +01001674 }
1675
Olivier Deprez96a2a262020-06-11 17:21:38 +02001676 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1677 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1678
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001679 /*
1680 * For EL0 partitions, since both the partition and the hypervisor code
1681 * use the EL2&0 translation regime, it is critical to mark the mappings
1682 * of the send and recv buffers as non-global in the TLB. For one, if we
1683 * dont mark it as non-global, it would cause TLB conflicts since there
1684 * would be an identity mapping with non-global attribute in the
1685 * partitions page tables, but another identity mapping in the
1686 * hypervisor page tables with the global attribute. The other issue is
1687 * one of security, we dont want other partitions to be able to access
1688 * other partitions buffers through cached translations.
1689 */
1690 if (vm_locked.vm->el0_partition) {
1691 extra_attributes |= MM_MODE_NG;
1692 }
1693
Karl Meakin738bee12024-01-12 15:19:45 +00001694 ret = api_vm_configure_stage1(
1695 mm_stage1_locked, vm_locked, pa_send_begin, pa_send_end,
1696 pa_recv_begin, pa_recv_end, extra_attributes, local_page_pool);
1697 if (ret.func != FFA_SUCCESS_32) {
Andrew Sculle1322792019-07-01 17:46:10 +01001698 goto fail_undo_send_and_recv;
1699 }
1700
Manish Pandeyd34f8892020-06-19 17:41:07 +01001701 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001702 goto out;
1703
Andrew Sculle1322792019-07-01 17:46:10 +01001704fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001705 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001706 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001707
1708fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001709 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001710 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001711
1712out:
Andrew Sculle1322792019-07-01 17:46:10 +01001713 return ret;
1714}
1715
Karl Meakinb9705e22024-04-05 13:58:28 +01001716/**
1717 * Read the buffer addresses and VM ID of an FFA_RXTX_MAP request. Handles
1718 * forwarded messages by reading from the hypervisor's TX buffer.
1719 *
1720 * Returns the VM/SP ID on success.
1721 *
1722 * Returns `HF_INVALID_VM_ID` when the arguments provided via the ABI
1723 * FFA_RXTX_MAP indicate the SPMC should retrieve the RXTX description from the
1724 * Hypervisor RXTX buffers, and the hypervisor hasn't given its own RXTX buffers
1725 * for the SPMC to map.
1726 */
1727static ffa_id_t api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send,
1728 ipaddr_t *recv, uint32_t *page_count)
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001729{
Karl Meakinb9705e22024-04-05 13:58:28 +01001730 bool forwarded;
1731 struct vm_locked vm_locked;
1732 ffa_id_t owner_vm_id;
1733 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc;
1734 struct ffa_composite_memory_region *rx_region;
1735 struct ffa_composite_memory_region *tx_region;
1736
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001737 /*
1738 * If the message has been forwarded the effective addresses are in
1739 * hypervisor's TX buffer.
1740 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001741 forwarded = (current_vm->id == HF_OTHER_WORLD_ID) &&
1742 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1743 (*page_count == 0);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001744
1745 if (forwarded) {
Karl Meakinb9705e22024-04-05 13:58:28 +01001746 vm_locked = vm_lock(current_vm);
1747 endpoint_desc = (struct ffa_endpoint_rx_tx_descriptor *)
1748 vm_locked.vm->mailbox.send;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001749
Karl Meakinb9705e22024-04-05 13:58:28 +01001750 if (endpoint_desc == NULL) {
1751 dlog_error(
1752 "Trying to access RXTX description, but "
1753 "hypervisor has not provided RXTX buffers\n");
1754 vm_unlock(&vm_locked);
1755 return HF_INVALID_VM_ID;
1756 }
1757
1758 rx_region = ffa_endpoint_get_rx_memory_region(endpoint_desc);
1759 tx_region = ffa_endpoint_get_tx_memory_region(endpoint_desc);
1760
1761 owner_vm_id = endpoint_desc->endpoint_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001762 *recv = ipa_init(rx_region->constituents[0].address);
1763 *send = ipa_init(tx_region->constituents[0].address);
1764 *page_count = rx_region->constituents[0].page_count;
J-Alves28ad9b42022-12-08 12:19:43 +00001765
1766 vm_unlock(&vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001767 } else {
Karl Meakinb9705e22024-04-05 13:58:28 +01001768 owner_vm_id = current_vm->id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001769 }
Karl Meakinb9705e22024-04-05 13:58:28 +01001770
1771 return owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001772}
Karl Meakinb9705e22024-04-05 13:58:28 +01001773
Andrew Sculle1322792019-07-01 17:46:10 +01001774/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001775 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001776 * must not be shared. Locking of the page tables combined with a local memory
1777 * pool ensures there will always be enough memory to recover from any errors
1778 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1779 * by another core which could result in this transaction being unable to roll
1780 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001781 *
1782 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001783 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001784 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001785 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001786 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001787 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001788 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001789 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001790struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001791 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001792{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001793 struct ffa_value ret;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001794 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001795 struct mm_stage1_locked mm_stage1_locked;
1796 struct mpool local_page_pool;
J-Alves19e20cf2023-08-02 12:48:55 +01001797 ffa_id_t owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001798
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001799 /*
1800 * Get the original buffer addresses and VM ID in case of forwarded
1801 * message.
1802 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001803 owner_vm_id = api_get_rxtx_description(current->vm, &send, &recv,
1804 &page_count);
1805 if (owner_vm_id == HF_INVALID_VM_ID) {
1806 return ffa_error(FFA_INVALID_PARAMETERS);
1807 }
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001808
1809 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1810 if (owner_vm_locked.vm == NULL) {
1811 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1812 owner_vm_id);
1813 return ffa_error(FFA_DENIED);
1814 }
Andrew Scull220e6212018-12-21 18:09:00 +00001815
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001816 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001817 * Create a local pool so any freed memory can't be used by another
1818 * thread. This is to ensure the original mapping can be restored if any
1819 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001820 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001821 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1822
Manish Pandeyd34f8892020-06-19 17:41:07 +01001823 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001824
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001825 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1826 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001827 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001828 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001829 }
1830
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001831 /* Forward buffer mapping to SPMC if coming from a VM. */
1832 plat_ffa_rxtx_map_forward(owner_vm_locked);
1833
Federico Recanati9f1b6532022-04-14 13:15:28 +02001834 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001835
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001836exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001837 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001838 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001839 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001840
1841 return ret;
1842}
1843
1844/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001845 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1846 * translation regime of the caller. Unmap the region for the hypervisor and
1847 * set the memory region to owned and exclusive for the component. Since the
1848 * memory region mapped in the page table, when the buffers were originally
1849 * created we can safely remap it.
1850 *
1851 * Returns:
1852 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1853 * behalf of the caller.
1854 * - FFA_SUCCESS on success if no further action is needed.
1855 */
J-Alves19e20cf2023-08-02 12:48:55 +01001856struct ffa_value api_ffa_rxtx_unmap(ffa_id_t allocator_id, struct vcpu *current)
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001857{
1858 struct vm *vm = current->vm;
1859 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001860 ffa_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001861 struct mm_stage1_locked mm_stage1_locked;
1862 paddr_t send_pa_begin;
1863 paddr_t send_pa_end;
1864 paddr_t recv_pa_begin;
1865 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001866 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001867
J-Alves661e1b72023-08-02 13:39:40 +01001868 if (vm->id == HF_HYPERVISOR_VM_ID && !ffa_is_vm_id(allocator_id)) {
Olivier Deprez106c8ce2024-02-08 11:49:02 +01001869 dlog_verbose(
J-Alves9bd324a2023-01-12 10:52:52 +00001870 "The Hypervisor must specify a valid VM ID in register "
1871 "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n");
1872 return ffa_error(FFA_INVALID_PARAMETERS);
1873 }
1874
Federico Recanati8da9e332022-02-10 11:00:17 +01001875 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1876 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
J-Alves9bd324a2023-01-12 10:52:52 +00001877 dlog_error(
1878 "The register W1 (containing ID) must be 0 at virtual "
1879 "instances.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001880 return ffa_error(FFA_INVALID_PARAMETERS);
1881 }
1882
1883 /* VM ID of which buffers have to be unmapped. */
1884 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1885
1886 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1887 vm = vm_locked.vm;
1888 if (vm == NULL) {
1889 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1890 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001891 return ffa_error(FFA_INVALID_PARAMETERS);
1892 }
1893
1894 /* Get send and receive buffers. */
1895 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001896 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001897 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001898 ret = ffa_error(FFA_INVALID_PARAMETERS);
1899 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001900 }
1901
1902 /* Currently a mailbox size of 1 page is assumed. */
1903 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1904 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1905 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1906 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1907
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001908 mm_stage1_locked = mm_lock_stage1();
1909
Federico Recanati8da9e332022-02-10 11:00:17 +01001910 /* Reset stage 2 mapping only for virtual FF-A instances. */
1911 if (vm_id_is_current_world(owner_vm_id)) {
1912 /*
1913 * Set the memory region of the buffers back to the default mode
1914 * for the VM. Since this memory region was already mapped for
1915 * the RXTX buffers we can safely remap them.
1916 */
1917 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1918 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1919 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001920
Federico Recanati8da9e332022-02-10 11:00:17 +01001921 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1922 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1923 &api_page_pool, NULL));
Karl Meakin738bee12024-01-12 15:19:45 +00001924 } else {
1925 ret = arch_other_world_vm_configure_rxtx_unmap(
1926 vm_locked, &api_page_pool, send_pa_begin, send_pa_end,
1927 recv_pa_begin, recv_pa_end);
1928 if (ret.func != FFA_SUCCESS_32) {
1929 goto out;
1930 }
Federico Recanati8da9e332022-02-10 11:00:17 +01001931 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001932
1933 /* Unmap the buffers in the partition manager. */
1934 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1935 &api_page_pool));
1936 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1937 &api_page_pool));
1938
1939 vm->mailbox.send = NULL;
1940 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001941 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001942
Federico Recanati8da9e332022-02-10 11:00:17 +01001943 /* Forward buffer unmapping to SPMC if coming from a VM. */
J-Alves70079932022-12-07 17:32:20 +00001944 plat_ffa_rxtx_unmap_forward(vm_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001945
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001946 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001947
1948out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001949 vm_unlock(&vm_locked);
1950
Federico Recanati8da9e332022-02-10 11:00:17 +01001951 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001952}
1953
1954/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001955 * Copies data from the sender's send buffer to the recipient's receive buffer
1956 * and notifies the receiver.
1957 */
J-Alves19e20cf2023-08-02 12:48:55 +01001958struct ffa_value api_ffa_msg_send2(ffa_id_t sender_vm_id, uint32_t flags,
Federico Recanati25053ee2022-03-14 15:01:53 +01001959 struct vcpu *current)
1960{
1961 struct vm *from = current->vm;
1962 struct vm *to;
1963 struct vm_locked to_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001964 ffa_id_t msg_sender_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01001965 struct vm_locked sender_locked;
1966 const void *from_msg;
1967 struct ffa_value ret;
J-Alves19e20cf2023-08-02 12:48:55 +01001968 ffa_id_t sender_id;
1969 ffa_id_t receiver_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01001970 uint32_t msg_size;
Federico Recanati25053ee2022-03-14 15:01:53 +01001971
Karl Meakina5ea9092024-05-28 15:40:33 +01001972 alignas(8) struct ffa_partition_rxtx_header header;
1973
Federico Recanati25053ee2022-03-14 15:01:53 +01001974 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
1975 if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) {
1976 dlog_error("Sender VM ID must be zero.\n");
1977 return ffa_error(FFA_INVALID_PARAMETERS);
1978 }
1979
Federico Recanati25053ee2022-03-14 15:01:53 +01001980 /*
1981 * Get message sender's mailbox, which can be different to the `from` vm
1982 * when the message is forwarded.
1983 */
1984 msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id;
1985 sender_locked = plat_ffa_vm_find_locked(msg_sender_id);
1986 if (sender_locked.vm == NULL) {
1987 dlog_error("Cannot send message from VM ID %#x, not found.\n",
1988 msg_sender_id);
1989 return ffa_error(FFA_DENIED);
1990 }
1991
1992 from_msg = sender_locked.vm->mailbox.send;
1993 if (from_msg == NULL) {
1994 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
1995 msg_sender_id);
1996 ret = ffa_error(FFA_DENIED);
1997 goto out_unlock_sender;
1998 }
1999
2000 /*
2001 * Copy message header as safety measure to avoid multiple accesses to
2002 * unsafe memory which could be 'corrupted' between safety checks and
2003 * final buffer copy.
2004 */
J-Alves8a5fd9d2024-04-09 11:22:49 +01002005 if (!memcpy_trapped(&header, FFA_RXTX_HEADER_SIZE, from_msg,
2006 FFA_RXTX_HEADER_SIZE)) {
2007 dlog_error(
2008 "%s: Failed to copy message from sender's(%x) TX "
2009 "buffer.\n",
2010 __func__, sender_locked.vm->id);
2011 ret = ffa_error(FFA_ABORTED);
2012 goto out_unlock_sender;
2013 }
2014
Federico Recanati25053ee2022-03-14 15:01:53 +01002015 sender_id = ffa_rxtx_header_sender(&header);
2016 receiver_id = ffa_rxtx_header_receiver(&header);
2017
2018 /* Ensure Sender IDs from API and from message header match. */
2019 if (msg_sender_id != sender_id) {
2020 dlog_error(
2021 "Message sender VM ID (%#x) doesn't match header's VM "
2022 "ID (%#x).\n",
2023 msg_sender_id, sender_id);
2024 ret = ffa_error(FFA_INVALID_PARAMETERS);
2025 goto out_unlock_sender;
2026 }
2027
2028 /* Disallow reflexive requests as this suggests an error in the VM. */
2029 if (receiver_id == sender_id) {
2030 dlog_error("Sender and receive VM IDs must be different.\n");
2031 ret = ffa_error(FFA_INVALID_PARAMETERS);
2032 goto out_unlock_sender;
2033 }
2034
Federico Recanati7b39ff22022-03-14 15:01:53 +01002035 /* `flags` can be set only at secure virtual FF-A instances. */
J-Alves661e1b72023-08-02 13:39:40 +01002036 if (ffa_is_vm_id(sender_id) && (flags != 0)) {
Federico Recanati7b39ff22022-03-14 15:01:53 +01002037 dlog_error("flags must be zero.\n");
2038 return ffa_error(FFA_INVALID_PARAMETERS);
2039 }
2040
Olivier Deprez37b75112024-03-04 18:27:30 +01002041 if (header.offset != FFA_RXTX_HEADER_SIZE) {
2042 dlog_error("Indirect msg payload must follow the header.\n");
2043 return ffa_error(FFA_INVALID_PARAMETERS);
2044 }
2045
Federico Recanati25053ee2022-03-14 15:01:53 +01002046 /*
2047 * Check if the message has to be forwarded to the SPMC, in
2048 * this case return, the SPMC will handle the buffer copy.
2049 */
2050 if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) {
2051 goto out_unlock_sender;
2052 }
2053
2054 /* Ensure the receiver VM exists. */
2055 to_locked = plat_ffa_vm_find_locked(receiver_id);
2056 to = to_locked.vm;
2057
2058 if (to == NULL) {
2059 dlog_error("Cannot deliver message to VM %#x, not found.\n",
2060 receiver_id);
2061 ret = ffa_error(FFA_INVALID_PARAMETERS);
2062 goto out_unlock_sender;
2063 }
2064
2065 /*
2066 * Check sender and receiver can use indirect messages.
2067 * Sender is the VM/SP who originally sent the message, not the
2068 * hypervisor possibly relaying it.
2069 */
2070 if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) {
2071 dlog_verbose("VM %#x doesn't support indirect message\n",
2072 sender_id);
2073 ret = ffa_error(FFA_DENIED);
2074 goto out;
2075 }
2076
J-Alvese8c8c2b2022-12-16 15:34:48 +00002077 if (vm_is_mailbox_busy(to_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002078 dlog_error(
2079 "Cannot deliver message to VM %#x, RX buffer not "
2080 "ready.\n",
2081 receiver_id);
2082 ret = ffa_error(FFA_BUSY);
2083 goto out;
2084 }
2085
Federico Recanati644f0462022-03-17 12:04:00 +01002086 /* Acquire receiver's RX buffer. */
2087 if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) {
2088 dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id);
2089 goto out;
2090 }
2091
Federico Recanati25053ee2022-03-14 15:01:53 +01002092 /* Check the size of transfer. */
2093 msg_size = FFA_RXTX_HEADER_SIZE + header.size;
Olivier Deprezf1dd1e12024-03-04 18:21:01 +01002094 if ((msg_size > FFA_MSG_PAYLOAD_MAX) ||
Federico Recanati25053ee2022-03-14 15:01:53 +01002095 (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
2096 dlog_error("Message is too big.\n");
2097 ret = ffa_error(FFA_INVALID_PARAMETERS);
2098 goto out;
2099 }
2100
2101 /* Copy data. */
J-Alves8a5fd9d2024-04-09 11:22:49 +01002102 if (!memcpy_trapped(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg,
2103 msg_size)) {
2104 dlog_error(
2105 "%s: Failed to copy message to receiver's(%x) RX "
2106 "buffer.\n",
2107 __func__, to->id);
2108 ret = ffa_error(FFA_ABORTED);
2109 goto out;
2110 }
2111
Federico Recanati25053ee2022-03-14 15:01:53 +01002112 to->mailbox.recv_size = msg_size;
2113 to->mailbox.recv_sender = sender_id;
2114 to->mailbox.recv_func = FFA_MSG_SEND2_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002115 to->mailbox.state = MAILBOX_STATE_FULL;
Federico Recanati25053ee2022-03-14 15:01:53 +01002116
J-Alves3bb82592023-01-23 11:20:54 +00002117 /*
2118 * Set framework notifications, only if the SP has enabled
2119 * receipt of notifications.
2120 * If VMs have provided the RX buffer it is implied they already
2121 * support indirect messaging, and therefore framework notifications.
2122 */
2123 if (ffa_is_vm_id(to_locked.vm->id) ||
2124 vm_are_notifications_enabled(to_locked.vm)) {
2125 ffa_notifications_bitmap_t rx_buffer_full =
2126 ffa_is_vm_id(sender_id)
2127 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
2128 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
Federico Recanati25053ee2022-03-14 15:01:53 +01002129
J-Alves3bb82592023-01-23 11:20:54 +00002130 vm_notifications_framework_set_pending(to_locked,
2131 rx_buffer_full);
2132
2133 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2134 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2135 vcpu_index(current));
2136 plat_ffa_sri_trigger_not_delayed(current->cpu);
2137 } else {
J-Alvesea8ccfe2024-10-09 11:47:24 +01002138 plat_ffa_sri_set_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002139 }
Federico Recanati25053ee2022-03-14 15:01:53 +01002140 }
2141
2142 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2143
2144out:
2145 vm_unlock(&to_locked);
2146
2147out_unlock_sender:
2148 vm_unlock(&sender_locked);
2149
2150 return ret;
2151}
2152
2153/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002154 * Releases the caller's mailbox so that a new message can be received. The
2155 * caller must have copied out all data they wish to preserve as new messages
2156 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002157 *
2158 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002159 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2160 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002161 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2162 * - FFA_SUCCESS on success if no further action is needed.
2163 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002164 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002165 * hf_mailbox_waiter_get.
2166 */
J-Alves19e20cf2023-08-02 12:48:55 +01002167struct ffa_value api_ffa_rx_release(ffa_id_t receiver_id, struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002168{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002169 struct vm *current_vm = current->vm;
2170 struct vm *vm;
2171 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01002172 ffa_id_t current_vm_id = current_vm->id;
2173 ffa_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002174 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002175
Federico Recanati7bef0b92022-03-17 14:56:22 +01002176 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2177 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2178 dlog_error("Invalid `receiver_id`, must be zero.\n");
2179 return ffa_error(FFA_INVALID_PARAMETERS);
2180 }
2181
2182 /*
2183 * VM ID to be released: `receiver_id` if message has been forwarded by
2184 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2185 */
2186 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2187 release_vm_id = current_vm_id;
2188 } else {
2189 release_vm_id = receiver_id;
2190 }
2191
2192 vm_locked = plat_ffa_vm_find_locked(release_vm_id);
2193 vm = vm_locked.vm;
2194 if (vm == NULL) {
2195 dlog_error("No buffer registered for VM ID %#x.\n",
2196 release_vm_id);
2197 return ffa_error(FFA_INVALID_PARAMETERS);
2198 }
2199
J-Alvese8c8c2b2022-12-16 15:34:48 +00002200 if (plat_ffa_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002201 goto out;
2202 }
2203
Karl Meakin1064a9c2024-06-04 17:20:43 +01002204 ret = api_release_mailbox(vm_locked);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002205
2206out:
2207 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002208
2209 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002210}
Andrew Walbran318f5732018-11-20 16:23:42 +00002211
2212/**
Federico Recanati644f0462022-03-17 12:04:00 +01002213 * Acquire ownership of an RX buffer before writing to it. Both
2214 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2215 * could contend for the same buffer. SPMC owns VM's RX buffer after
2216 * it's mapped in its translation regime. This ABI should be
2217 * used by the Hypervisor to get the ownership of a VM's RX buffer
2218 * from the SPMC solving the aforementioned possible contention.
2219 *
2220 * Returns:
2221 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2222 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2223 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2224 */
J-Alves19e20cf2023-08-02 12:48:55 +01002225struct ffa_value api_ffa_rx_acquire(ffa_id_t receiver_id, struct vcpu *current)
Federico Recanati644f0462022-03-17 12:04:00 +01002226{
2227 struct vm_locked receiver_locked;
2228 struct vm *receiver;
2229 struct ffa_value ret;
2230
2231 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
J-Alves661e1b72023-08-02 13:39:40 +01002232 !ffa_is_vm_id(receiver_id)) {
Federico Recanati644f0462022-03-17 12:04:00 +01002233 dlog_error(
2234 "FFA_RX_ACQUIRE not supported at this FF-A "
2235 "instance.\n");
2236 return ffa_error(FFA_NOT_SUPPORTED);
2237 }
2238
2239 receiver_locked = plat_ffa_vm_find_locked(receiver_id);
2240 receiver = receiver_locked.vm;
2241
2242 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2243 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2244 receiver_id);
2245 ret = ffa_error(FFA_INVALID_PARAMETERS);
2246 goto out;
2247 }
2248
2249 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2250 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2251 ret = ffa_error(FFA_DENIED);
2252 goto out;
2253 }
2254
J-Alvese8c8c2b2022-12-16 15:34:48 +00002255 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002256
2257 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2258
2259out:
2260 vm_unlock(&receiver_locked);
2261
2262 return ret;
2263}
2264
J-Alvescc542042024-07-24 19:13:22 +01002265/*
2266 * Returns true if intid relates with either of those:
2267 * - NPI
2268 * - ME
2269 * - Virtual Timer.
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002270 * - IPI
J-Alvescc542042024-07-24 19:13:22 +01002271 *
2272 * These are VIs with no expected interrupt descriptor.
2273 */
2274static bool api_is_maintenance_virtual_interrupt(uint32_t intid)
2275{
2276 return intid == HF_NOTIFICATION_PENDING_INTID ||
2277 intid == HF_MANAGED_EXIT_INTID ||
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002278 intid == HF_VIRTUAL_TIMER_INTID || intid == HF_IPI_INTID;
J-Alvescc542042024-07-24 19:13:22 +01002279}
2280
Federico Recanati644f0462022-03-17 12:04:00 +01002281/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002282 * Enables or disables a given interrupt ID for the calling vCPU.
2283 *
2284 * Returns 0 on success, or -1 if the intid is invalid.
2285 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002286int64_t api_interrupt_enable(uint32_t intid, bool enable,
2287 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002288{
Manish Pandey35e452f2021-02-18 21:36:34 +00002289 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002290 struct interrupts *interrupts = &current->interrupts;
J-Alvescc542042024-07-24 19:13:22 +01002291 struct interrupt_descriptor *int_desc = NULL;
2292 struct vm *vm = current->vm;
2293 struct vm_locked vm_locked;
2294
2295 int64_t ret = -1;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002296
Andrew Walbran318f5732018-11-20 16:23:42 +00002297 if (intid >= HF_NUM_INTIDS) {
2298 return -1;
2299 }
2300
J-Alvescc542042024-07-24 19:13:22 +01002301 vm_locked = vm_lock(vm);
Manish Pandey35e452f2021-02-18 21:36:34 +00002302 current_locked = vcpu_lock(current);
J-Alvescc542042024-07-24 19:13:22 +01002303
2304 int_desc = vm_interrupt_set_enable(vm_locked, intid, enable);
2305
2306 if (!api_is_maintenance_virtual_interrupt(intid)) {
2307 if (int_desc == NULL) {
2308 dlog_error("%s: invalid interrupt ID.\n", __func__);
2309 goto out;
2310 }
2311
2312 plat_interrupts_configure_interrupt(*int_desc);
2313 }
2314
Andrew Walbran318f5732018-11-20 16:23:42 +00002315 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002316 /*
2317 * If it is pending and was not enabled before, increment the
2318 * count.
2319 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002320 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2321 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2322 vcpu_interrupt_count_increment(current_locked,
2323 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002324 }
J-Alvescc542042024-07-24 19:13:22 +01002325
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002326 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2327 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002328 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002329 /*
2330 * If it is pending and was enabled before, decrement the count.
2331 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002332 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2333 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2334 vcpu_interrupt_count_decrement(current_locked,
2335 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002336 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002337 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2338 vcpu_virt_interrupt_set_type(interrupts, intid,
2339 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002340 }
2341
J-Alvescc542042024-07-24 19:13:22 +01002342 ret = 0;
2343
2344out:
2345 vm_unlock(&vm_locked);
Manish Pandey35e452f2021-02-18 21:36:34 +00002346 vcpu_unlock(&current_locked);
J-Alvescc542042024-07-24 19:13:22 +01002347
2348 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002349}
2350
2351/**
2352 * Returns the ID of the next pending interrupt for the calling vCPU, and
2353 * acknowledges it (i.e. marks it as no longer pending). Returns
2354 * HF_INVALID_INTID if there are no pending interrupts.
2355 */
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002356uint32_t api_interrupt_get(struct vcpu_locked current_locked)
Andrew Walbran318f5732018-11-20 16:23:42 +00002357{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002358 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002359 uint32_t first_interrupt = HF_INVALID_INTID;
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002360 struct interrupts *interrupts = &current_locked.vcpu->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002361
2362 /*
2363 * Find the first enabled and pending interrupt ID, return it, and
2364 * deactivate it.
2365 */
Andrew Walbran318f5732018-11-20 16:23:42 +00002366 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2367 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002368 interrupts->interrupt_enabled.bitmap[i] &
2369 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002370
Andrew Walbran318f5732018-11-20 16:23:42 +00002371 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002372 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002373
2374 first_interrupt =
2375 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002376
Andrew Walbran3d84a262018-12-13 14:41:19 +00002377 /*
2378 * Mark it as no longer pending and decrement the count.
2379 */
J-Alvesb8730e92024-08-07 18:28:55 +01002380 vcpu_interrupt_clear_decrement(current_locked,
2381 first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002382 break;
2383 }
2384 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002385
Andrew Walbran318f5732018-11-20 16:23:42 +00002386 return first_interrupt;
2387}
2388
2389/**
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002390 * Negotiate the FF-A version to be used for this FF-A instance.
2391 * See section 13.2 of the FF-A v1.2 ALP1 spec.
2392 *
2393 * Returns Hafnium's version number (`FFA_VERSION_COMPILED`) on success.
2394 * Returns `FFA_NOT_SUPPORTED` on error:
2395 * - The version is invalid (highest bit set).
2396 * - The requested version is incompatible.
2397 * - The version has already been negotiated and cannot be changed.
2398 */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002399struct ffa_value api_ffa_version(struct vcpu *current,
Karl Meakin0e617d92024-04-05 12:55:22 +01002400 enum ffa_version requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002401{
Karl Meakin0e617d92024-04-05 12:55:22 +01002402 static_assert(sizeof(enum ffa_version) == 4,
2403 "enum ffa_version must be 4 bytes wide");
2404
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002405 const struct ffa_value error = {.func = (uint32_t)FFA_NOT_SUPPORTED};
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002406 struct vm_locked current_vm_locked;
2407
Karl Meakin0e617d92024-04-05 12:55:22 +01002408 if (!ffa_version_is_valid(requested_version)) {
2409 dlog_error(
2410 "FFA_VERSION: requested version %#x is invalid "
2411 "(highest bit must be zero)\n",
2412 requested_version);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002413 return error;
Andrew Walbran9fd29072020-04-22 12:12:14 +01002414 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002415
Karl Meakin0e617d92024-04-05 12:55:22 +01002416 if (!ffa_versions_are_compatible(requested_version,
2417 FFA_VERSION_COMPILED)) {
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002418 dlog_error(
2419 "FFA_VERSION: requested version v%u.%u is not "
2420 "compatible with v%u.%u\n",
2421 ffa_version_get_major(requested_version),
2422 ffa_version_get_minor(requested_version),
2423 ffa_version_get_major(FFA_VERSION_COMPILED),
2424 ffa_version_get_minor(FFA_VERSION_COMPILED));
2425 return error;
J-Alves3cc9d6f2023-07-20 16:46:15 +01002426 }
2427
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002428 current_vm_locked = vm_lock(current->vm);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002429
2430 if (current_vm_locked.vm->ffa_version_negotiated &&
2431 requested_version != current_vm_locked.vm->ffa_version) {
2432 vm_unlock(&current_vm_locked);
2433 dlog_error(
2434 "FFA_VERSION: Cannot change FF-A version after other "
2435 "FF-A calls have been made\n");
2436 return error;
2437 }
2438
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002439 current_vm_locked.vm->ffa_version = requested_version;
2440 vm_unlock(&current_vm_locked);
2441
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002442 return (struct ffa_value){.func = (uint32_t)FFA_VERSION_COMPILED};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002443}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002444
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002445/**
Karl Meakin49ec1e42024-05-10 13:08:24 +01002446 * Helper for success return of FFA_FEATURES.
J-Alves6f72ca82021-11-01 12:34:58 +00002447 */
2448struct ffa_value api_ffa_feature_success(uint32_t arg2)
2449{
2450 return (struct ffa_value){
Karl Meakin49ec1e42024-05-10 13:08:24 +01002451 .func = FFA_SUCCESS_32,
2452 .arg1 = 0U,
2453 .arg2 = arg2,
2454 };
J-Alves6f72ca82021-11-01 12:34:58 +00002455}
2456
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002457static struct ffa_value ffa_features_function(uint32_t func,
2458 uint32_t input_property,
2459 struct vcpu *current)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002460{
Karl Meakin0e617d92024-04-05 12:55:22 +01002461 const enum ffa_version ffa_version = current->vm->ffa_version;
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002462 const bool el0_partition = current->vm->el0_partition;
2463
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002464 switch (func) {
J-Alves6f72ca82021-11-01 12:34:58 +00002465 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002466 case FFA_ERROR_32:
2467 case FFA_SUCCESS_32:
2468 case FFA_INTERRUPT_32:
2469 case FFA_VERSION_32:
2470 case FFA_FEATURES_32:
2471 case FFA_RX_RELEASE_32:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002472 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002473 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002474 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002475 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002476 case FFA_RUN_32:
J-Alves95fbb312024-03-20 15:19:16 +00002477 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002478 case FFA_MEM_DONATE_32:
2479 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00002480 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002481 case FFA_MEM_SHARE_32:
J-Alves95fbb312024-03-20 15:19:16 +00002482 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002483 case FFA_MEM_RETRIEVE_RESP_32:
2484 case FFA_MEM_RELINQUISH_32:
2485 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002486 case FFA_MEM_FRAG_RX_32:
2487 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002488 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002489 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002490 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002491 case FFA_MSG_SEND_DIRECT_REQ_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002492
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002493 /* FF-A v1.1 features. */
2494 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002495 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2496 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2497 case FFA_NOTIFICATION_BIND_32:
2498 case FFA_NOTIFICATION_UNBIND_32:
2499 case FFA_NOTIFICATION_SET_32:
2500 case FFA_NOTIFICATION_GET_32:
2501 case FFA_NOTIFICATION_INFO_GET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002502 case FFA_MSG_SEND2_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002503 if (FFA_VERSION_1_1 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002504 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002505 }
2506
Kathleen Capellae4fe2962023-09-01 17:08:47 -04002507 /* FF-A v1.2 features. */
2508 case FFA_CONSOLE_LOG_32:
2509 case FFA_CONSOLE_LOG_64:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002510 case FFA_PARTITION_INFO_GET_REGS_64:
Kathleen Capella41fea932023-06-23 17:39:28 -04002511 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002512 case FFA_MSG_SEND_DIRECT_RESP2_64:
Karl Meakin0e617d92024-04-05 12:55:22 +01002513 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002514 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002515 }
2516
Karl Meakin49ec1e42024-05-10 13:08:24 +01002517 return api_ffa_feature_success(0);
2518
Karl Meakin5a222642024-06-20 10:23:37 +01002519 /* These functions are only supported on S-EL0 partitions. */
2520 case FFA_MEM_PERM_GET_32:
2521 case FFA_MEM_PERM_SET_32:
2522 case FFA_MEM_PERM_GET_64:
2523 case FFA_MEM_PERM_SET_64:
2524 if (!(vm_id_is_current_world(current->vm->id) &&
2525 el0_partition)) {
2526 dlog_verbose(
2527 "FFA_FEATURE: %s is only supported on S-EL0 "
2528 "partitions\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002529 ffa_func_name(func));
Karl Meakin5a222642024-06-20 10:23:37 +01002530 return ffa_error(FFA_NOT_SUPPORTED);
2531 }
2532 return api_ffa_feature_success(0);
2533
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002534 case FFA_SECONDARY_EP_REGISTER_64:
2535 if (FFA_VERSION_COMPILED < FFA_VERSION_1_1) {
2536 return ffa_error(FFA_NOT_SUPPORTED);
2537 }
2538
2539 if (!(vm_id_is_current_world(current->vm->id) &&
2540 current->vm->vcpu_count > 1)) {
2541 dlog_verbose(
2542 "FFA_FEATURE: %s is only supported on SPs with "
2543 "more than 1 vCPU\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002544 ffa_func_name(func));
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002545 return ffa_error(FFA_NOT_SUPPORTED);
2546 }
2547 return api_ffa_feature_success(0);
2548
Karl Meakin650cb142024-06-20 10:31:57 +01002549 /*
2550 * This function is restricted to the secure virtual FF-A instance (i.e.
2551 * only report success to SPs).
2552 */
2553 case FFA_YIELD_32:
2554 if (!vm_id_is_current_world(current->vm->id)) {
2555 dlog_verbose(
2556 "FFA_FEATURES: %s is only supported at secure "
2557 "virtual FF-A instance\n",
2558 ffa_func_name(FFA_YIELD_32));
2559 return ffa_error(FFA_NOT_SUPPORTED);
2560 }
2561 return api_ffa_feature_success(0);
2562
Karl Meakin0fd67292024-02-06 17:33:05 +00002563 case FFA_RXTX_MAP_64: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002564 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002565 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002566 }
2567
Karl Meakin0fd67292024-02-06 17:33:05 +00002568 uint32_t arg2 = 0;
2569 struct ffa_features_rxtx_map_params params = {
2570 .min_buf_size = FFA_RXTX_MAP_MIN_BUF_4K,
2571 .mbz = 0,
2572 .max_buf_size =
Karl Meakin0e617d92024-04-05 12:55:22 +01002573 (ffa_version >= FFA_VERSION_1_2)
Karl Meakin0fd67292024-02-06 17:33:05 +00002574 ? FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT
2575 : 0,
2576 };
2577
2578 memcpy_s(&arg2, sizeof(arg2), &params, sizeof(params));
Karl Meakin49ec1e42024-05-10 13:08:24 +01002579 return api_ffa_feature_success(arg2);
Karl Meakin0fd67292024-02-06 17:33:05 +00002580 }
2581
J-Alves95fbb312024-03-20 15:19:16 +00002582 case FFA_MEM_RETRIEVE_REQ_64:
Karl Meakin49ec1e42024-05-10 13:08:24 +01002583 case FFA_MEM_RETRIEVE_REQ_32: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002584 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002585 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002586 }
2587
Karl Meakin49ec1e42024-05-10 13:08:24 +01002588 if (ANY_BITS_SET(input_property,
2589 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2590 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT) ||
2591 IS_BIT_SET(input_property,
2592 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT)) {
2593 dlog_warning(
2594 "FFA_FEATURES: Bits[%u:%u] and Bit[%u] of "
2595 "input_property should be 0 (input_property = "
2596 "%#x)\n",
2597 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2598 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT,
2599 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT,
Karl Meakin34b8ae92023-01-13 13:33:07 +00002600 input_property);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002601 }
2602
Karl Meakin0e617d92024-04-05 12:55:22 +01002603 if (ffa_version >= FFA_VERSION_1_1 &&
2604 (input_property &
2605 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
Karl Meakin4e8e4792024-07-05 16:28:42 +01002606 dlog_verbose(
2607 "FFA_FEATURES: NS bit support must be 1\n");
Karl Meakin6fd6c1d2024-05-13 12:09:13 +01002608 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002609 }
2610
2611 return api_ffa_feature_success(
2612 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
Karl Meakin49ec1e42024-05-10 13:08:24 +01002613 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT |
Karl Meakin34b8ae92023-01-13 13:33:07 +00002614 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
Karl Meakin49ec1e42024-05-10 13:08:24 +01002615 }
J-Alves6f72ca82021-11-01 12:34:58 +00002616
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002617 default:
2618 return ffa_error(FFA_NOT_SUPPORTED);
2619 }
2620}
2621
2622static struct ffa_value ffa_features_feature(enum ffa_feature_id feature,
2623 uint32_t input_property,
2624 struct vcpu *current)
2625{
2626 const bool el0_partition = current->vm->el0_partition;
2627
2628 if (ANY_BITS_SET(feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2629 FFA_FEATURES_FEATURE_MBZ_LO_BIT)) {
2630 dlog_verbose(
2631 "FFA_FEATURES: feature ID %#x is invalid (bits [%u:%u] "
2632 "must be zero)\n",
2633 feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2634 FFA_FEATURES_FEATURE_MBZ_LO_BIT);
2635 return ffa_error(FFA_NOT_SUPPORTED);
2636 }
2637 if (input_property != 0) {
2638 dlog_verbose(
2639 "FFA_FEATURES: input_property must be 0 "
2640 "(input_property = %#x)\n",
2641 input_property);
2642 return ffa_error(FFA_NOT_SUPPORTED);
2643 }
2644
2645 switch (feature) {
J-Alves6f72ca82021-11-01 12:34:58 +00002646 /* Check support of a feature provided respective feature ID. */
Karl Meakin17c9ad32024-04-12 16:41:00 +01002647
2648 /*
2649 * For NPI and MEI, report the IDs as supported only to partitions at
2650 * the virtual FF-A instances.
2651 */
J-Alves6f72ca82021-11-01 12:34:58 +00002652 case FFA_FEATURE_NPI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002653 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002654 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002655 }
2656
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002657 if (el0_partition) {
2658 return ffa_error(FFA_NOT_SUPPORTED);
2659 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002660 if (!vm_id_is_current_world(current->vm->id)) {
2661 return ffa_error(FFA_NOT_SUPPORTED);
2662 }
J-Alves6f72ca82021-11-01 12:34:58 +00002663 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
Karl Meakin17c9ad32024-04-12 16:41:00 +01002664
2665 case FFA_FEATURE_MEI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002666 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002667 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002668 }
J-Alvesc4d9ae82024-05-14 10:15:03 +01002669 if (el0_partition) {
2670 return ffa_error(FFA_NOT_SUPPORTED);
2671 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002672 if (!vm_id_is_current_world(current->vm->id)) {
2673 return ffa_error(FFA_NOT_SUPPORTED);
2674 }
2675 return api_ffa_feature_success(HF_MANAGED_EXIT_INTID);
2676
J-Alves6f72ca82021-11-01 12:34:58 +00002677 case FFA_FEATURE_SRI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002678 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002679 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002680 }
2681
Karl Meakincb6642c2024-02-27 14:43:45 +00002682 if (!ffa_is_vm_id(current->vm->id)) {
2683 return ffa_error(FFA_NOT_SUPPORTED);
2684 }
J-Alves6f72ca82021-11-01 12:34:58 +00002685 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
Karl Meakin0e617d92024-04-05 12:55:22 +01002686
J-Alves6f72ca82021-11-01 12:34:58 +00002687 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002688 default:
Karl Meakin4271ff92024-05-13 12:33:15 +01002689 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002690 }
2691}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002692
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002693/**
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002694 * Discovery function returning information about the implementation of optional
2695 * FF-A interfaces. See section 13.3 of the FF-A v1.2 ALP1 spec.
2696 *
2697 * `function_or_feature_id` is interpreted as either a function ID or a feature
2698 * ID, depending on the value of bit 31.
2699 * When it is a feature ID, bits [30:8] MBZ and input_property MBZ.
2700 *
2701 * Returns `FFA_SUCCESS` if the interface is supported.
2702 * Returns `FFA_NOT_SUPPORTED` if the interface is not supported or the
2703 * parameters are invalid.
2704 */
2705struct ffa_value api_ffa_features(uint32_t function_or_feature_id,
2706 uint32_t input_property, struct vcpu *current)
2707{
2708 return IS_BIT_UNSET(function_or_feature_id, FFA_FEATURES_FEATURE_BIT)
2709 ? ffa_features_feature(function_or_feature_id,
2710 input_property, current)
2711 : ffa_features_function(function_or_feature_id,
2712 input_property, current);
2713}
2714
2715/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002716 * FF-A specification states that x2/w2 Must Be Zero for FFA_MSG_SEND_DIRECT_REQ
2717 * and FFA_MSG_SEND_DIRECT_RESP interfaces when used for partition messages. See
2718 * FF-A v1.2 Table 16.6: FFA_MSG_SEND_DIRECT_REQ function syntax.
J-Alves645eabe2021-02-22 16:08:27 +00002719 */
2720static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2721{
2722 return args.arg2 == 0U;
2723}
2724
2725/**
J-Alves76d99af2021-03-10 17:42:11 +00002726 * Limits size of arguments in ffa_value structure to 32-bit.
2727 */
2728static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2729{
2730 return (struct ffa_value){
2731 .func = (uint32_t)args.func,
2732 .arg1 = (uint32_t)args.arg1,
Karl Meakin06e8b732024-09-20 18:26:49 +01002733 .arg2 = (uint32_t)args.arg2,
J-Alves76d99af2021-03-10 17:42:11 +00002734 .arg3 = (uint32_t)args.arg3,
2735 .arg4 = (uint32_t)args.arg4,
2736 .arg5 = (uint32_t)args.arg5,
2737 .arg6 = (uint32_t)args.arg6,
2738 .arg7 = (uint32_t)args.arg7,
2739 };
2740}
2741
2742/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002743 * Helper to copy direct message payload, depending on SMC used, direct
Karl Meakin06e8b732024-09-20 18:26:49 +01002744 * messaging interface used, and expected registers size. Can be used for both
2745 * framework messages and standard messages.
J-Alves76d99af2021-03-10 17:42:11 +00002746 */
2747static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2748{
Karl Meakin06e8b732024-09-20 18:26:49 +01002749 switch (args.func) {
2750 case FFA_MSG_SEND_DIRECT_REQ_32:
2751 case FFA_MSG_SEND_DIRECT_RESP_32:
2752 args = api_ffa_value_copy32(args);
2753 if (!ffa_is_framework_msg(args)) {
2754 args.arg2 = 0;
2755 }
2756 break;
J-Alves76d99af2021-03-10 17:42:11 +00002757
Karl Meakin06e8b732024-09-20 18:26:49 +01002758 case FFA_MSG_SEND_DIRECT_REQ_64:
2759 case FFA_MSG_SEND_DIRECT_RESP_64:
2760 if (!ffa_is_framework_msg(args)) {
2761 args.arg2 = 0;
2762 }
2763 break;
Kathleen Capella41fea932023-06-23 17:39:28 -04002764
Karl Meakin06e8b732024-09-20 18:26:49 +01002765 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002766 args.extended_val.valid = true;
Karl Meakin06e8b732024-09-20 18:26:49 +01002767 break;
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002768
Karl Meakin06e8b732024-09-20 18:26:49 +01002769 case FFA_MSG_SEND_DIRECT_RESP2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002770 args.arg2 = 0;
2771 args.arg3 = 0;
Karl Meakin06e8b732024-09-20 18:26:49 +01002772 break;
2773 default:
2774 panic("Invalid direct message function %#x\n", args.func);
2775 break;
Kathleen Capella087e5022023-09-07 18:04:15 -04002776 }
2777
Kathleen Capella41fea932023-06-23 17:39:28 -04002778 return args;
2779}
2780
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002781/**
2782 * Return a pointer to the first UUID in `uuids` that is equal to
2783 * `target_uuid`. If `target_uuid` is 0-0-0-0, it matches any UUID.
2784 */
2785static struct ffa_uuid *ffa_uuid_find(struct ffa_uuid *uuids,
2786 size_t uuids_count,
2787 struct ffa_uuid target_uuid)
2788{
2789 if (ffa_uuid_is_null(&target_uuid)) {
2790 return &uuids[0];
2791 }
2792
2793 for (size_t i = 0; i < uuids_count; i++) {
2794 if (ffa_uuid_is_null(&uuids[i])) {
2795 break;
2796 }
2797 if (ffa_uuid_equal(&target_uuid, &uuids[i])) {
2798 return &uuids[i];
2799 }
2800 }
2801 return NULL;
2802}
2803
Kathleen Capella41fea932023-06-23 17:39:28 -04002804static bool api_ffa_dir_msg_req2_is_uuid_valid(struct vm *receiver_vm,
2805 struct ffa_value args)
2806{
2807 struct ffa_uuid target_uuid;
2808
Karl Meakin9478e322024-09-23 17:47:09 +01002809 ffa_uuid_from_u64x2(args.arg2, args.arg3, &target_uuid);
Kathleen Capella41fea932023-06-23 17:39:28 -04002810
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002811 return ffa_uuid_find(receiver_vm->uuids, PARTITION_MAX_UUIDS,
2812 target_uuid) != NULL;
J-Alves76d99af2021-03-10 17:42:11 +00002813}
2814
2815/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002816 * Send an FF-A direct message request.
Kathleen Capella41fea932023-06-23 17:39:28 -04002817 * This handler covers both FFA_MSG_SEND_DIRECT_REQ_32/64
2818 * and FFA_MSG_SEND_DIRECT_REQ2_64 (introduced in FF-A v1.2) with function-based
2819 * checks to accomodate for the difference between the ABIs.
2820 *
2821 * FFA_MSG_SEND_DIRECT_REQ2_64 works mostly the same as
2822 * FFA_MSG_SEND_DIRECT_REQ_32/64, but adds the ability to send a direct message
2823 * request to a specified UUID within a partition and the usage of an extended
2824 * range of registers (x4-x17, instead of x4-x7) to be used as part of the
2825 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002826 */
Karl Meakin13f09812024-10-28 16:33:23 +00002827struct ffa_value api_ffa_msg_send_direct_req(struct ffa_value args,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002828 struct vcpu *current,
2829 struct vcpu **next)
2830{
Karl Meakin13f09812024-10-28 16:33:23 +00002831 ffa_id_t sender_vm_id = ffa_sender(args);
2832 ffa_id_t receiver_vm_id = ffa_receiver(args);
J-Alves17228f72021-04-20 17:13:19 +01002833 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002834 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002835 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002836 struct vcpu *receiver_vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002837 struct vcpu_locked current_locked;
2838 struct vcpu_locked receiver_vcpu_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002839 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002840 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002841
Kathleen Capella41fea932023-06-23 17:39:28 -04002842 if ((args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2843 args.func == FFA_MSG_SEND_DIRECT_REQ_64) &&
Karl Meakin06e8b732024-09-20 18:26:49 +01002844 !ffa_is_framework_msg(args) &&
Kathleen Capella41fea932023-06-23 17:39:28 -04002845 !api_ffa_dir_msg_is_arg2_zero(args)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002846 dlog_verbose("Direct messaging: w2 must be zero (w2 = %#lx)\n",
2847 args.arg2);
J-Alves645eabe2021-02-22 16:08:27 +00002848 return ffa_error(FFA_INVALID_PARAMETERS);
2849 }
2850
Karl Meakin06e8b732024-09-20 18:26:49 +01002851 if (ffa_is_framework_msg(args) &&
2852 plat_ffa_handle_framework_msg(args, &ret)) {
2853 return ret;
2854 }
2855
Olivier Deprez55a189e2021-06-09 15:45:27 +02002856 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2857 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002858 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002859 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002860 }
2861
Olivier Deprez55a189e2021-06-09 15:45:27 +02002862 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002863 dlog_verbose("Direct message request forwarded\n");
J-Alves17228f72021-04-20 17:13:19 +01002864 return ret;
2865 }
2866
J-Alvesbd32c972024-02-02 16:20:04 +00002867 ret = api_ffa_interrupt_return(0);
J-Alves17228f72021-04-20 17:13:19 +01002868
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002869 receiver_vm = vm_find(receiver_vm_id);
2870 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002871 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002872 return ffa_error(FFA_INVALID_PARAMETERS);
2873 }
2874
Kathleen Capella41fea932023-06-23 17:39:28 -04002875 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64 &&
2876 !api_ffa_dir_msg_req2_is_uuid_valid(receiver_vm, args)) {
2877 dlog_verbose("UUID unrecognized for this VM\n");
2878 return ffa_error(FFA_INVALID_PARAMETERS);
2879 }
2880
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002881 /*
J-Alves439ac972021-11-18 17:32:03 +00002882 * Check if sender supports sending direct message req, and if
2883 * receiver supports receipt of direct message requests.
2884 */
Kathleen Capella41fea932023-06-23 17:39:28 -04002885 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm,
2886 args.func)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002887 dlog_verbose("Direct message request not supported\n");
J-Alves439ac972021-11-18 17:32:03 +00002888 return ffa_error(FFA_DENIED);
2889 }
2890
2891 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002892 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002893 * UP (migratable) or MP partitions with a number of vCPUs matching the
2894 * number of PEs in the system. It further states that MP partitions
2895 * accepting direct request messages cannot migrate.
2896 */
J-Alvesad6a0432021-04-09 16:06:21 +01002897 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002898 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002899 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002900 return ffa_error(FFA_INVALID_PARAMETERS);
2901 }
2902
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002903 /*
2904 * If VM must be locked, it must be done before any of its vCPUs are
2905 * locked.
2906 */
J-Alves6e2abc62021-12-02 14:58:56 +00002907 receiver_locked = vm_lock(receiver_vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002908
2909 /* Lock both vCPUs at once to avoid deadlock. */
2910 vcpus_locked = vcpu_lock_both(current, receiver_vcpu);
2911 current_locked = vcpus_locked.vcpu1;
2912 receiver_vcpu_locked = vcpus_locked.vcpu2;
2913
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002914 /*
2915 * If destination vCPU is executing or already received an
2916 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2917 * busy. There is a brief period of time where the vCPU state has
2918 * changed but regs_available is still false thus consider this case as
2919 * the vCPU not yet ready to receive a direct message request.
2920 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002921 if (is_ffa_direct_msg_request_ongoing(receiver_vcpu_locked) ||
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002922 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2923 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002924 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002925 ret = ffa_error(FFA_BUSY);
2926 goto out;
2927 }
2928
J-Alves7ea3f7e2024-03-27 11:05:11 +00002929 if (!plat_ffa_check_runtime_state_transition(
2930 current_locked, sender_vm_id, HF_INVALID_VM_ID,
2931 receiver_vcpu_locked, args.func, &next_state)) {
2932 ret = ffa_error(FFA_DENIED);
2933 goto out;
2934 }
2935
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002936 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2937 memory_order_relaxed)) {
2938 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04002939 dlog_verbose(
2940 "Receiver VM %#x aborted, cannot run vCPU %u\n",
2941 receiver_vcpu->vm->id,
2942 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002943 receiver_vcpu->state = VCPU_STATE_ABORTED;
2944 }
2945
2946 ret = ffa_error(FFA_ABORTED);
2947 goto out;
2948 }
2949
2950 switch (receiver_vcpu->state) {
2951 case VCPU_STATE_OFF:
2952 case VCPU_STATE_RUNNING:
2953 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002954 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002955 case VCPU_STATE_BLOCKED:
2956 case VCPU_STATE_PREEMPTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002957 ret = ffa_error(FFA_BUSY);
2958 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002959 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002960 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002961 * We expect target vCPU to be in WAITING state after either
2962 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002963 */
2964 break;
2965 }
2966
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05002967 /* Inject timer interrupt if timer has expired. */
2968 api_inject_arch_timer_interrupt(current_locked, receiver_vcpu_locked);
Madhukar Pappireddy106bfc32024-09-25 15:47:11 -05002969 timer_migrate_to_other_cpu(current->cpu, receiver_vcpu_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002970
2971 /* The receiver vCPU runs upon direct message invocation */
2972 receiver_vcpu->cpu = current->cpu;
2973 receiver_vcpu->state = VCPU_STATE_RUNNING;
2974 receiver_vcpu->regs_available = false;
Kathleen Capellae468c112023-12-13 17:56:28 -05002975 receiver_vcpu->direct_request_origin.is_ffa_req2 =
2976 (args.func == FFA_MSG_SEND_DIRECT_REQ2_64);
2977 receiver_vcpu->direct_request_origin.vm_id = sender_vm_id;
Karl Meakin06e8b732024-09-20 18:26:49 +01002978 receiver_vcpu->direct_request_origin.is_framework =
2979 ffa_is_framework_msg(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002980
J-Alves76d99af2021-03-10 17:42:11 +00002981 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002982
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002983 assert(!vm_id_is_current_world(current->vm->id) ||
2984 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002985 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002986
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07002987 plat_ffa_wind_call_chain_ffa_direct_req(
2988 current_locked, receiver_vcpu_locked, sender_vm_id);
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05002989
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002990 /* Switch to receiver vCPU targeted to by direct msg request */
2991 *next = receiver_vcpu;
2992
J-Alves6e2abc62021-12-02 14:58:56 +00002993 if (!receiver_locked.vm->el0_partition) {
2994 /*
2995 * If the scheduler in the system is giving CPU cycles to the
2996 * receiver, due to pending notifications, inject the NPI
2997 * interrupt. Following call assumes that '*next' has been set
2998 * to receiver_vcpu.
2999 */
3000 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003001 receiver_vcpu_locked, current_locked, receiver_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003002 }
3003
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003004 /*
3005 * Since this flow will lead to a VM switch, the return value will not
3006 * be applied to current vCPU.
3007 */
3008
3009out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003010 vcpu_unlock(&receiver_vcpu_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003011 vm_unlock(&receiver_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003012 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003013
3014 return ret;
3015}
3016
3017/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003018 * Resume the target vCPU after the current vCPU sent a direct response.
3019 * Current vCPU moves to waiting state.
3020 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003021void api_ffa_resume_direct_resp_target(struct vcpu_locked current_locked,
3022 struct vcpu **next,
J-Alves19e20cf2023-08-02 12:48:55 +01003023 ffa_id_t receiver_vm_id,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003024 struct ffa_value to_ret,
3025 bool is_nwd_call_chain)
3026{
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07003027 if (plat_ffa_is_spmd_lp_id(receiver_vm_id) ||
3028 !vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003029 *next = api_switch_to_other_world(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003030 VCPU_STATE_WAITING);
3031
3032 /* End of NWd scheduled call chain. */
3033 assert(!is_nwd_call_chain ||
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003034 (current_locked.vcpu->call_chain.prev_node == NULL));
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003035 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003036 *next = api_switch_to_primary(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003037 VCPU_STATE_WAITING);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003038 } else if (vm_id_is_current_world(receiver_vm_id)) {
3039 /*
3040 * It is expected the receiver_vm_id to be from an SP, otherwise
3041 * 'plat_ffa_is_direct_response_valid' should have
3042 * made function return error before getting to this point.
3043 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003044 *next = api_switch_to_vm(current_locked, to_ret,
3045 VCPU_STATE_WAITING, receiver_vm_id);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003046 } else {
3047 panic("Invalid direct message response invocation");
3048 }
3049}
3050
Karl Meakin50ef9112024-10-28 16:41:44 +00003051static bool api_ffa_msg_send_direct_resp_validate_args(struct ffa_value args,
3052 struct vcpu *current)
3053{
3054 ffa_id_t sender_vm_id = ffa_sender(args);
3055 ffa_id_t receiver_vm_id = ffa_receiver(args);
3056
3057 /*
3058 * If using FFA_MSG_SEND_DIRECT_RESP, the caller's
3059 * - x2 MBZ for partition messages
3060 * - x8-x17 SBZ if caller's FF-A version >= FF-A v1.2
3061 */
3062 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) {
3063 if (!ffa_is_framework_msg(args) &&
3064 !api_ffa_dir_msg_is_arg2_zero(args)) {
3065 dlog_verbose("%s: w2 Must Be Zero",
3066 ffa_func_name(args.func));
3067 return false;
3068 }
3069
3070 if (current->vm->ffa_version >= FFA_VERSION_1_2 &&
3071 !api_extended_args_are_zero(&args)) {
3072 return false;
3073 }
3074 }
3075
3076 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
3077 receiver_vm_id)) {
3078 dlog_verbose("Invalid direct response call.\n");
3079 return false;
3080 }
3081
3082 return true;
3083}
3084
3085static bool api_ffa_msg_send_direct_resp_validate_ongoing_request(
3086 struct ffa_value args, struct vcpu_locked current_locked)
3087{
3088 bool req_framework =
3089 current_locked.vcpu->direct_request_origin.is_framework;
3090 bool resp_framework = ffa_is_framework_msg(args);
3091 bool received_req2 =
3092 current_locked.vcpu->direct_request_origin.is_ffa_req2;
3093
3094 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
3095 return false;
3096 }
3097
3098 if (req_framework && !resp_framework) {
3099 dlog_verbose(
3100 "Mismatch in framework message bit: request was a %s "
3101 "message, but response is a %s message\n",
3102 req_framework ? "framework" : "non-framework",
3103 resp_framework ? "framework" : "non-framework");
3104 return false;
3105 }
3106
3107 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64 && received_req2) {
3108 dlog_verbose(
3109 "FFA_MSG_SEND_DIRECT_RESP must be used with "
3110 "FFA_MSG_SEND_DIRECT_REQ\n");
3111 return false;
3112 }
3113
3114 if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64 && !received_req2) {
3115 dlog_verbose(
3116 "FFA_MSG_SEND_DIRECT_RESP2 must be used with "
3117 "FFA_MSG_SEND_DIRECT_REQ2\n");
3118 return false;
3119 }
3120
3121 return true;
3122}
3123
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003124/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003125 * Send an FF-A direct message response.
Kathleen Capella087e5022023-09-07 18:04:15 -04003126 * This handler covers both FFA_MSG_SEND_DIRECT_RESP_32/64
3127 * and FFA_MSG_SEND_DIRECT_RESP2_64 (introduced in FF-A v1.2) with
3128 * function-based checks to accomodate for the difference between the ABIs.
3129 *
3130 * FFA_MSG_SEND_DIRECT_RESP2_64 is used to respond to requests sent via
3131 * FFA_MSG_SEND_DIRECT_REQ2_64 and adds the usage of an extended range
3132 * of registers (x4-x17, instead of x4-x7) to be used as part of the
3133 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003134 */
Karl Meakin13f09812024-10-28 16:33:23 +00003135struct ffa_value api_ffa_msg_send_direct_resp(struct ffa_value args,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003136 struct vcpu *current,
3137 struct vcpu **next)
3138{
Karl Meakin13f09812024-10-28 16:33:23 +00003139 ffa_id_t sender_vm_id = ffa_sender(args);
3140 ffa_id_t receiver_vm_id = ffa_receiver(args);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003141 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003142 struct vcpu_locked next_locked = (struct vcpu_locked){
3143 .vcpu = NULL,
3144 };
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003145 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003146 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003147 struct ffa_value signal_interrupt =
3148 (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003149 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
3150 struct two_vcpu_locked vcpus_locked;
J-Alves645eabe2021-02-22 16:08:27 +00003151
Karl Meakin50ef9112024-10-28 16:41:44 +00003152 if (!api_ffa_msg_send_direct_resp_validate_args(args, current)) {
J-Alvesaa336102021-03-01 13:02:45 +00003153 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003154 }
3155
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003156 current_locked = vcpu_lock(current);
3157
3158 if (!plat_ffa_check_runtime_state_transition(
3159 current_locked, sender_vm_id, receiver_vm_id, next_locked,
3160 args.func, &next_state)) {
3161 ret = ffa_error(FFA_DENIED);
3162 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05003163 }
3164
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003165 assert(!vm_id_is_current_world(current->vm->id) ||
3166 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003167
Karl Meakin50ef9112024-10-28 16:41:44 +00003168 if (!api_ffa_msg_send_direct_resp_validate_ongoing_request(
3169 args, current_locked)) {
Kathleen Capellae468c112023-12-13 17:56:28 -05003170 ret = ffa_error(FFA_DENIED);
3171 goto out;
3172 }
3173
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003174 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003175 struct interrupts *interrupts = &current->interrupts;
3176
Madhukar Pappireddy7945bb52024-08-20 15:22:41 -05003177 CHECK(current->scheduling_mode != SPMC_MODE);
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003178
Madhukar Pappireddy025a4512024-10-14 22:09:19 -05003179 plat_interrupts_set_priority_mask(
3180 current->prev_interrupt_priority);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003181 /*
3182 * A SP may be signaled a managed exit but actually not trap
3183 * the virtual interrupt, probably because it has virtual
3184 * interrupts masked, and emit direct resp. In this case the
3185 * managed exit operation is considered completed and it would
3186 * also need to clear the pending managed exit flag for the SP
3187 * vCPU.
3188 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003189 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003190
3191 if (vcpu_is_virt_interrupt_pending(interrupts,
3192 HF_MANAGED_EXIT_INTID)) {
J-Alvesb8730e92024-08-07 18:28:55 +01003193 vcpu_interrupt_clear_decrement(current_locked,
3194 HF_MANAGED_EXIT_INTID);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003195 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003196 }
3197
Kathleen Capellae468c112023-12-13 17:56:28 -05003198 /* Clear direct request origin vm_id and request type for the caller. */
3199 current->direct_request_origin.is_ffa_req2 = false;
3200 current->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003201
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003202 api_ffa_resume_direct_resp_target(current_locked, next, receiver_vm_id,
3203 to_ret, false);
3204
3205 /*
3206 * Unlock current vCPU to allow it to be locked together with next
3207 * vcpu.
3208 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003209 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003210
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003211 /* Lock both vCPUs at once to avoid deadlock. */
3212 vcpus_locked = vcpu_lock_both(current, *next);
3213 current_locked = vcpus_locked.vcpu1;
3214 next_locked = vcpus_locked.vcpu2;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003215
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05003216 /* Inject timer interrupt if timer has expired. */
3217 api_inject_arch_timer_interrupt(current_locked, next_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003218 plat_ffa_unwind_call_chain_ffa_direct_resp(current_locked, next_locked);
J-Alvesbd32c972024-02-02 16:20:04 +00003219
3220 /*
3221 * Check if there is a pending secure interrupt.
3222 * If there is, return back to the caller with FFA_INTERRUPT,
3223 * and set the `next` vcpu in a preempted state.
3224 */
3225 if (plat_ffa_intercept_call(current_locked, next_locked,
3226 &signal_interrupt)) {
3227 ret = signal_interrupt;
3228 *next = NULL;
3229 }
3230
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003231 vcpu_unlock(&next_locked);
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05003232
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003233out:
3234 vcpu_unlock(&current_locked);
3235 return ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003236}
3237
J-Alves84658fc2021-06-17 14:37:32 +01003238static bool api_memory_region_check_flags(
3239 struct ffa_memory_region *memory_region, uint32_t share_func)
3240{
3241 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00003242 case FFA_MEM_SHARE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003243 case FFA_MEM_SHARE_32:
3244 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3245 0U) {
3246 return false;
3247 }
3248 /* Intentional fall-through */
J-Alves95fbb312024-03-20 15:19:16 +00003249 case FFA_MEM_LEND_64:
J-Alves84658fc2021-06-17 14:37:32 +01003250 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00003251 case FFA_MEM_DONATE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003252 case FFA_MEM_DONATE_32: {
3253 /* Bits 31:2 Must Be Zero. */
3254 ffa_memory_receiver_flags_t to_mask =
3255 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
3256 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
3257
3258 if ((memory_region->flags & to_mask) != 0U) {
3259 return false;
3260 }
3261 break;
3262 }
3263 default:
3264 panic("Check for mem send calls only.\n");
3265 }
3266
3267 /* Last check reserved values are 0 */
3268 return true;
3269}
3270
J-Alves33c47bf2022-09-29 11:36:20 +01003271/*
3272 * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0.
3273 */
3274static void api_ffa_memory_region_v1_1_from_v1_0(
3275 struct ffa_memory_region_v1_0 *memory_region_v1_0,
3276 struct ffa_memory_region *memory_region_v1_1)
3277{
3278 memory_region_v1_1->sender = memory_region_v1_0->sender;
J-Alves00847062022-10-03 17:08:44 +01003279 memory_region_v1_1->handle = memory_region_v1_0->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003280 memory_region_v1_1->attributes =
3281 ffa_memory_attributes_extend(memory_region_v1_0->attributes);
J-Alves33c47bf2022-09-29 11:36:20 +01003282 memory_region_v1_1->flags = memory_region_v1_0->flags;
3283 memory_region_v1_1->tag = memory_region_v1_0->tag;
3284 memory_region_v1_1->memory_access_desc_size =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003285 sizeof(struct ffa_memory_access_v1_0);
J-Alves33c47bf2022-09-29 11:36:20 +01003286 memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003287 memory_region_v1_1->receivers_offset = sizeof(struct ffa_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003288
3289 /* Zero reserved fields. */
3290 for (uint32_t i = 0; i < 3U; i++) {
3291 memory_region_v1_1->reserved[i] = 0U;
3292 }
3293}
3294
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003295/**
3296 * Updates a v1.0 transaction descriptor to v1.1. This gives us the
3297 * memory_access_desc_size field we need for forwards compatability.
3298 * Copy the receivers and composite descriptors to the new struct.
3299 * We also check the fields in the v1.0 transaction descriptor and return:
3300 * - FFA_ERROR FFA_INVALID_PARAMETERS: If any of the fields are not valid
3301 * values, eg the reserved fields are not 0, receiver_count is too large or
3302 * composite offsets are not 0 for retrieve requests or in bounds for send
3303 * requests.
3304 * - FFA ERROR FFA_NOT_SUPPORTED: If an invalid ffa_version is supplied to the
3305 * function. Or the fragment length is more than a single page.
3306 * - FFA_ERROR FFA_NO_MEMORY: If we do not have enough memory for a scratch
3307 * memory transaction descriptor.
3308 * - FFA_SUCCESS: If a successful update has occured.
J-Alves33c47bf2022-09-29 11:36:20 +01003309 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003310static struct ffa_value api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3311 void *allocated, uint32_t *fragment_length, uint32_t *total_length,
Karl Meakin0e617d92024-04-05 12:55:22 +01003312 enum ffa_version ffa_version, bool send_transaction)
J-Alves33c47bf2022-09-29 11:36:20 +01003313{
3314 struct ffa_memory_region_v1_0 *memory_region_v1_0;
3315 struct ffa_memory_region *memory_region_v1_1 = NULL;
3316 struct ffa_composite_memory_region *composite_v1_0;
3317 struct ffa_composite_memory_region *composite_v1_1;
3318 size_t receivers_length;
3319 size_t space_left;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003320 size_t receivers_end;
J-Alves33c47bf2022-09-29 11:36:20 +01003321 size_t composite_offset_v1_1;
3322 size_t composite_offset_v1_0;
3323 size_t fragment_constituents_size;
3324 size_t fragment_length_v1_1;
3325
J-Alves33c47bf2022-09-29 11:36:20 +01003326 assert(fragment_length != NULL);
3327 assert(total_length != NULL);
3328
Karl Meakin0e617d92024-04-05 12:55:22 +01003329 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves33c47bf2022-09-29 11:36:20 +01003330 return (struct ffa_value){.func = FFA_SUCCESS_32};
3331 }
3332
Karl Meakin0e617d92024-04-05 12:55:22 +01003333 if (ffa_version != FFA_VERSION_1_0) {
J-Alves33c47bf2022-09-29 11:36:20 +01003334 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
3335 ffa_version);
3336 return ffa_error(FFA_NOT_SUPPORTED);
3337 }
3338
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003339 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003340 "Updating memory transaction descriptor from v1.0 to v1.1.\n");
J-Alves33c47bf2022-09-29 11:36:20 +01003341
3342 memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated;
3343
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003344 receivers_length = sizeof(struct ffa_memory_access_v1_0) *
J-Alves33c47bf2022-09-29 11:36:20 +01003345 memory_region_v1_0->receiver_count;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003346 receivers_end = sizeof(struct ffa_memory_region) + receivers_length;
J-Alves33c47bf2022-09-29 11:36:20 +01003347
3348 /*
3349 * Check the specified composite offset of v1.0 descriptor, and that all
3350 * receivers were configured with the same offset.
3351 */
3352 composite_offset_v1_0 =
3353 memory_region_v1_0->receivers[0].composite_memory_region_offset;
3354
J-Alves33c47bf2022-09-29 11:36:20 +01003355 /* Determine the composite offset for v1.1 descriptor. */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003356 if (send_transaction) {
3357 fragment_constituents_size =
3358 *fragment_length - composite_offset_v1_0 -
3359 sizeof(struct ffa_composite_memory_region);
3360 fragment_length_v1_1 =
3361 receivers_end +
3362 sizeof(struct ffa_composite_memory_region) +
3363 fragment_constituents_size;
3364 composite_offset_v1_1 = receivers_end;
3365 } else {
3366 fragment_constituents_size = 0;
3367 fragment_length_v1_1 = receivers_end;
3368 composite_offset_v1_1 = 0;
3369 }
J-Alves33c47bf2022-09-29 11:36:20 +01003370
3371 /*
3372 * Currently only support the simpler cases: memory transaction
3373 * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE.
3374 * TODO: allocate the entries needed for this fragment_length_v1_1.
3375 * - Check corner when v1.1 descriptor converted size surpasses
3376 * the size of the entry.
3377 */
3378 if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) {
3379 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003380 "Translation of FF-A v1.0 descriptors for over %lu is "
J-Alves33c47bf2022-09-29 11:36:20 +01003381 "unsupported.",
3382 MM_PPOOL_ENTRY_SIZE);
3383 return ffa_error(FFA_NOT_SUPPORTED);
3384 }
3385
3386 space_left = fragment_length_v1_1;
3387
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003388 /*
3389 * Allocate a page of memory to construct the v1.1 memory descriptor.
3390 * Earlier we checked that the fragment_length_v1_1 would not be larger
3391 * than a page.
3392 */
J-Alves33c47bf2022-09-29 11:36:20 +01003393 memory_region_v1_1 =
3394 (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
3395 if (memory_region_v1_1 == NULL) {
3396 return ffa_error(FFA_NO_MEMORY);
3397 }
3398
3399 /* Translate header from v1.0 to v1.1. */
3400 api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0,
3401 memory_region_v1_1);
3402
3403 space_left -= sizeof(struct ffa_memory_region);
3404
3405 /* Copy memory access information. */
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003406 memcpy_s((uint8_t *)memory_region_v1_1 +
3407 memory_region_v1_1->receivers_offset,
3408 space_left, memory_region_v1_0->receivers, receivers_length);
J-Alves33c47bf2022-09-29 11:36:20 +01003409
3410 /* Initialize the memory access descriptors with composite offset. */
3411 for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) {
3412 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003413 ffa_memory_region_get_receiver(memory_region_v1_1, i);
3414 assert(receiver != NULL);
J-Alves33c47bf2022-09-29 11:36:20 +01003415 receiver->composite_memory_region_offset =
3416 composite_offset_v1_1;
3417 }
3418
3419 space_left -= receivers_length;
3420
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003421 /* Composite memory descriptors to copy. */
3422 if (send_transaction) {
3423 /* Init v1.1 composite. */
3424 composite_v1_1 = (struct ffa_composite_memory_region
3425 *)((uint8_t *)memory_region_v1_1 +
3426 composite_offset_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003427
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003428 composite_v1_0 = ffa_memory_region_get_composite_v1_0(
3429 memory_region_v1_0, 0);
3430 composite_v1_1->constituent_count =
3431 composite_v1_0->constituent_count;
3432 composite_v1_1->page_count = composite_v1_0->page_count;
J-Alves33c47bf2022-09-29 11:36:20 +01003433
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003434 space_left -= sizeof(struct ffa_composite_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003435
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003436 /* Initialize v1.1 constituents. */
3437 memcpy_s(composite_v1_1->constituents, space_left,
3438 composite_v1_0->constituents,
3439 fragment_constituents_size);
J-Alves33c47bf2022-09-29 11:36:20 +01003440
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003441 space_left -= fragment_constituents_size;
3442 }
3443
J-Alves33c47bf2022-09-29 11:36:20 +01003444 assert(space_left == 0U);
3445
J-Alves33c47bf2022-09-29 11:36:20 +01003446 /*
3447 * Remove the v1.0 fragment size, and resultant size of v1.1 fragment.
3448 */
3449 *total_length = *total_length - *fragment_length + fragment_length_v1_1;
3450 *fragment_length = fragment_length_v1_1;
3451
3452 /*
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003453 * After successfully updating to v1.1 copy the descriptor to the
3454 * internal buffer given as a parameter (used to prevent TOCTOU attacks)
3455 * and free the scratch memory used to construct it.
J-Alves33c47bf2022-09-29 11:36:20 +01003456 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003457 memcpy_s(allocated, MM_PPOOL_ENTRY_SIZE, memory_region_v1_1,
3458 *fragment_length);
3459 mpool_free(&api_page_pool, memory_region_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003460
3461 return (struct ffa_value){.func = FFA_SUCCESS_32};
3462}
3463
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003464struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
3465 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003466 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003467{
3468 struct vm *from = current->vm;
3469 struct vm *to;
3470 const void *from_msg;
J-Alves33c47bf2022-09-29 11:36:20 +01003471 void *allocated_entry;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003472 struct ffa_memory_region *memory_region = NULL;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003473 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01003474 bool targets_other_world = false;
Karl Meakin0e617d92024-04-05 12:55:22 +01003475 enum ffa_version ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003476
3477 if (ipa_addr(address) != 0 || page_count != 0) {
3478 /*
3479 * Hafnium only supports passing the descriptor in the TX
3480 * mailbox.
3481 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003482 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003483 }
3484
Andrew Walbranca808b12020-05-15 17:22:28 +01003485 if (fragment_length > length) {
3486 dlog_verbose(
3487 "Fragment length %d greater than total length %d.\n",
3488 fragment_length, length);
3489 return ffa_error(FFA_INVALID_PARAMETERS);
3490 }
J-Alves33c47bf2022-09-29 11:36:20 +01003491
3492 if (fragment_length > HF_MAILBOX_SIZE ||
3493 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003494 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003495 }
3496
3497 /*
3498 * Check that the sender has configured its send buffer. If the TX
3499 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3500 * be safely accessed after releasing the lock since the TX mailbox
3501 * address can only be configured once.
3502 */
3503 sl_lock(&from->lock);
3504 from_msg = from->mailbox.send;
J-Alves33c47bf2022-09-29 11:36:20 +01003505 ffa_version = from->ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003506 sl_unlock(&from->lock);
3507
3508 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003509 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003510 }
3511
3512 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003513 * Copy the memory region descriptor to a fresh page from the memory
3514 * pool. This prevents the sender from changing it underneath us, and
3515 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003516 */
J-Alves33c47bf2022-09-29 11:36:20 +01003517 allocated_entry = mpool_alloc(&api_page_pool);
3518 if (allocated_entry == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003519 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003520 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003521 }
J-Alves33c47bf2022-09-29 11:36:20 +01003522
J-Alves8f2150d2024-03-28 16:15:56 +00003523 if (!memcpy_trapped(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg,
3524 fragment_length)) {
3525 dlog_error(
3526 "%s: Failed to copy FF-A memory region descriptor.\n",
3527 __func__);
3528 return ffa_error(FFA_ABORTED);
3529 }
J-Alves33c47bf2022-09-29 11:36:20 +01003530
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003531 if (!ffa_memory_region_sanity_check(allocated_entry, ffa_version,
3532 fragment_length, true)) {
3533 ret = ffa_error(FFA_INVALID_PARAMETERS);
3534 goto out;
3535 }
3536
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003537 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3538 allocated_entry, &fragment_length, &length, ffa_version, true);
J-Alves33c47bf2022-09-29 11:36:20 +01003539 if (ret.func != FFA_SUCCESS_32) {
3540 goto out;
3541 }
3542
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003543 memory_region = allocated_entry;
3544
J-Alves33c47bf2022-09-29 11:36:20 +01003545 if (fragment_length < sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003546 memory_region->memory_access_desc_size) {
J-Alves33c47bf2022-09-29 11:36:20 +01003547 dlog_verbose(
3548 "Initial fragment length %d smaller than header size "
Karl Meakine8937d92024-03-19 16:04:25 +00003549 "%lu.\n",
J-Alves33c47bf2022-09-29 11:36:20 +01003550 fragment_length,
3551 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003552 memory_region->memory_access_desc_size);
3553 ret = ffa_error(FFA_INVALID_PARAMETERS);
3554 goto out;
J-Alves33c47bf2022-09-29 11:36:20 +01003555 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003556
J-Alves84658fc2021-06-17 14:37:32 +01003557 if (!api_memory_region_check_flags(memory_region, share_func)) {
3558 dlog_verbose(
3559 "Memory region reserved arguments must be zero.\n");
3560 ret = ffa_error(FFA_INVALID_PARAMETERS);
3561 goto out;
3562 }
3563
J-Alves363f5722022-04-25 17:37:37 +01003564 if (memory_region->receiver_count == 0U) {
3565 dlog_verbose("Receiver count can't be 0.\n");
3566 ret = ffa_error(FFA_INVALID_PARAMETERS);
3567 goto out;
3568 }
3569
J-Alves95fbb312024-03-20 15:19:16 +00003570 if ((share_func == FFA_MEM_DONATE_32 ||
3571 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01003572 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003573 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003574 "FFA_MEM_DONATE only supports one recipient. Specified "
3575 "%u\n",
J-Alves363f5722022-04-25 17:37:37 +01003576 memory_region->receiver_count);
3577 ret = ffa_error(FFA_INVALID_PARAMETERS);
3578 goto out;
3579 }
3580
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003581 /*
3582 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01003583 * If there is a receiver from the other world, track it for later
3584 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003585 */
J-Alves363f5722022-04-25 17:37:37 +01003586 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003587 struct ffa_memory_access *receiver =
3588 ffa_memory_region_get_receiver(memory_region, i);
J-Alvesc9227c82024-04-24 21:00:58 +01003589 ffa_id_t receiver_id;
3590
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003591 assert(receiver != NULL);
J-Alvesc9227c82024-04-24 21:00:58 +01003592
3593 receiver_id = receiver->receiver_permissions.receiver;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003594
J-Alves363f5722022-04-25 17:37:37 +01003595 to = vm_find(receiver_id);
3596
J-Alves7de61af2024-03-27 10:29:46 +00003597 if ((vm_id_is_current_world(receiver_id) && to == NULL) ||
3598 to == from) {
J-Alves363f5722022-04-25 17:37:37 +01003599 dlog_verbose("%s: invalid receiver.\n", __func__);
3600 ret = ffa_error(FFA_INVALID_PARAMETERS);
3601 goto out;
3602 }
3603
J-Alvesc9227c82024-04-24 21:00:58 +01003604 if (!plat_ffa_is_memory_send_valid(
3605 receiver_id, from->id, share_func,
3606 memory_region->receiver_count > 1)) {
J-Alves363f5722022-04-25 17:37:37 +01003607 ret = ffa_error(FFA_DENIED);
3608 goto out;
3609 }
3610
3611 /* Capture if any of the receivers is from the other world. */
3612 if (!targets_other_world) {
3613 targets_other_world =
3614 !vm_id_is_current_world(receiver_id);
3615 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003616 }
3617
J-Alves363f5722022-04-25 17:37:37 +01003618 if (targets_other_world) {
J-Alves66652252022-07-06 09:49:51 +01003619 ret = plat_ffa_other_world_mem_send(
3620 from, share_func, &memory_region, length,
3621 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003622 } else {
3623 struct vm_locked from_locked = vm_lock(from);
3624
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003625 ret = ffa_memory_send(from_locked, memory_region, length,
3626 fragment_length, share_func,
3627 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003628 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003629 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003630 * make sure we don't free it.
3631 */
3632 memory_region = NULL;
3633
3634 vm_unlock(&from_locked);
3635 }
3636
3637out:
3638 if (memory_region != NULL) {
3639 mpool_free(&api_page_pool, memory_region);
3640 }
3641
3642 return ret;
3643}
3644
J-Alvesc7270b72024-09-12 10:16:34 +01003645/**
3646 * An FFA_MEM_RETRIEVE_REQ from the hypervisor must specify the handle of the
3647 * memory transaction it is querying and all other fields must be 0.
3648 */
3649static bool api_ffa_memory_hypervisor_retrieve_request_validate(
3650 struct ffa_memory_region *request, enum ffa_version version)
3651{
3652 switch (version) {
3653 case FFA_VERSION_1_0: {
3654 struct ffa_memory_region_v1_0 *request_v1_0 =
3655 (struct ffa_memory_region_v1_0 *)request;
3656
3657 return request_v1_0->sender == 0U &&
3658 request_v1_0->attributes.shareability == 0U &&
3659 request_v1_0->attributes.cacheability == 0U &&
3660 request_v1_0->attributes.type == 0U &&
3661 request_v1_0->attributes.security == 0U &&
3662 request_v1_0->flags == 0U && request_v1_0->tag == 0U &&
3663 request_v1_0->receiver_count == 0U &&
3664 plat_ffa_memory_handle_allocated_by_current_world(
3665 request_v1_0->handle);
3666 }
3667 default:
3668 return request->sender == 0U &&
3669 request->attributes.shareability == 0U &&
3670 request->attributes.cacheability == 0U &&
3671 request->attributes.type == 0U &&
3672 request->attributes.security == 0U &&
3673 request->flags == 0U && request->tag == 0U &&
3674 request->memory_access_desc_size == 0U &&
3675 request->receiver_count == 0U &&
3676 request->receivers_offset == 0U &&
3677 plat_ffa_memory_handle_allocated_by_current_world(
3678 request->handle);
3679 }
3680}
3681
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003682struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3683 uint32_t fragment_length,
3684 ipaddr_t address, uint32_t page_count,
3685 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003686{
3687 struct vm *to = current->vm;
3688 struct vm_locked to_locked;
3689 const void *to_msg;
J-Alves00847062022-10-03 17:08:44 +01003690 void *retrieve_msg;
3691 struct ffa_memory_region *retrieve_request = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003692 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003693 struct ffa_value ret;
Karl Meakin0e617d92024-04-05 12:55:22 +01003694 enum ffa_version ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003695
3696 if (ipa_addr(address) != 0 || page_count != 0) {
3697 /*
3698 * Hafnium only supports passing the descriptor in the TX
3699 * mailbox.
3700 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003701 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003702 }
3703
Andrew Walbrana65a1322020-04-06 19:32:32 +01003704 if (fragment_length != length) {
J-Alves33c47bf2022-09-29 11:36:20 +01003705 dlog_verbose("Fragmentation not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003706 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003707 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003708
J-Alves00847062022-10-03 17:08:44 +01003709 retrieve_msg = cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003710 message_buffer_size = cpu_get_buffer_size(current->cpu);
3711 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3712 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003713 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003714 }
3715
3716 to_locked = vm_lock(to);
3717 to_msg = to->mailbox.send;
J-Alves00847062022-10-03 17:08:44 +01003718 ffa_version = to->ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003719
3720 if (to_msg == NULL) {
3721 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003722 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003723 goto out;
3724 }
3725
3726 /*
3727 * Copy the retrieve request descriptor to an internal buffer, so that
3728 * the caller can't change it underneath us.
3729 */
J-Alves8f2150d2024-03-28 16:15:56 +00003730 if (!memcpy_trapped(retrieve_msg, message_buffer_size, to_msg,
3731 length)) {
3732 dlog_error(
3733 "%s: Failed to copy FF-A retrieve request "
3734 "descriptor.\n",
3735 __func__);
3736 ret = ffa_error(FFA_ABORTED);
3737 goto out;
3738 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003739
J-Alvese8c8c2b2022-12-16 15:34:48 +00003740 if ((vm_is_mailbox_other_world_owned(to_locked) &&
3741 !plat_ffa_acquire_receiver_rx(to_locked, &ret)) ||
3742 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003743 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003744 * Can't retrieve memory information if the mailbox is
3745 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003746 */
J-Alves59ed0042022-07-28 18:26:41 +01003747 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003748 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003749 goto out;
3750 }
3751
Daniel Boulby296ee702023-11-28 13:36:55 +00003752 if (!is_ffa_hypervisor_retrieve_request(retrieve_msg)) {
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003753 if (!ffa_memory_region_sanity_check(retrieve_msg, ffa_version,
3754 fragment_length, false)) {
3755 ret = ffa_error(FFA_INVALID_PARAMETERS);
3756 goto out;
3757 }
3758 /*
3759 * If required, transform the retrieve request to FF-A v1.1.
3760 */
3761 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3762 retrieve_msg, &fragment_length, &length, ffa_version,
3763 false);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003764
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003765 if (ret.func != FFA_SUCCESS_32) {
3766 goto out;
3767 }
J-Alvesc7270b72024-09-12 10:16:34 +01003768 } else {
3769 if (!api_ffa_memory_hypervisor_retrieve_request_validate(
3770 retrieve_msg, ffa_version)) {
3771 dlog_verbose(
3772 "All fields except the handle in the "
3773 "memory access descriptor must be zero for a "
3774 "hypervisor retrieve request.\n");
3775 ret = ffa_error(FFA_INVALID_PARAMETERS);
3776 goto out;
3777 }
J-Alves00847062022-10-03 17:08:44 +01003778 }
3779
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003780 retrieve_request = retrieve_msg;
Kathleen Capella578784f2023-09-01 18:03:27 -04003781
J-Alvesb5084cf2022-07-06 14:20:12 +01003782 if (plat_ffa_memory_handle_allocated_by_current_world(
3783 retrieve_request->handle)) {
3784 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3785 &api_page_pool);
3786 } else {
Daniel Boulbyfdd59f72023-10-19 16:43:19 +01003787 dlog_error("Invalid FF-A memory handle.\n");
3788 ret = ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesb5084cf2022-07-06 14:20:12 +01003789 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003790out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003791 vm_unlock(&to_locked);
3792 return ret;
3793}
3794
J-Alvesa39a8442024-04-26 15:41:47 +01003795/**
3796 * Copies the memory relinquish descriptor from the partition's TX buffer, to
3797 * the buffer of the local CPU. Do it safely, and return error if:
3798 * - FFA_ABORTED: if the `memcpy_trapped` fails.
3799 * - FFA_INVALID_PARAMETERS: if the size of the full memory relinquish
3800 * descriptor doesn't fit the local CPU buffer.
3801 *
3802 * Returns FFA_SUCCESS if copying goes well, and sets 'out_relinquish'
3803 * to the address of the cpu buffer with the relinquish descriptor.
3804 */
3805static struct ffa_value api_get_ffa_mem_relinquish_descriptor(
3806 struct vcpu *current, const void *from_msg,
3807 struct ffa_mem_relinquish **out_relinquish)
3808{
3809 struct ffa_mem_relinquish *relinquish_request;
3810 uint32_t from_msg_size;
3811 uint32_t total_from_msg_size;
3812 uint32_t dst_size;
3813 vaddr_t dst;
3814 vaddr_t src;
3815
3816 assert(from_msg != NULL);
3817 assert(out_relinquish != NULL);
3818
3819 /*
3820 * Copy the relinquish descriptor to an internal buffer, so that the
3821 * caller can't change it underneath us.
3822 */
3823 relinquish_request =
3824 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
3825
3826 /* Set the destination for the copy. */
3827 dst = va_from_ptr(relinquish_request);
3828 src = va_from_ptr(from_msg);
3829
3830 dst_size = cpu_get_buffer_size(current->cpu);
3831
3832 /* Only copy the size to start with. */
3833 from_msg_size = sizeof(struct ffa_mem_relinquish);
3834 total_from_msg_size = from_msg_size;
3835
3836 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3837 from_msg_size)) {
3838 dlog_error(
3839 "%s: Failed to copy FF-A memory relinquish "
3840 "descriptor.\n",
3841 __func__);
3842 return ffa_error(FFA_ABORTED);
3843 }
3844
3845 if (relinquish_request->endpoint_count != 1) {
3846 dlog_error("%s: relinquish descriptor must have 1 endpoint\n",
3847 __func__);
3848 return ffa_error(FFA_INVALID_PARAMETERS);
3849 }
3850
3851 /*
3852 * Increment the `dst` to the position right after the copied header.
3853 * Increment the `src` to point at the list of endpoints.
3854 *
3855 * Calculate the new `dst_size` which is the size of the allocated cpu
3856 * buffer, minus the size of the copied memory relinquish header.
3857 *
3858 * Only after the above: determine the new `from_msg_size` in accordance
3859 * to the endpoint count.
3860 */
3861 dst = va_add(dst, from_msg_size);
3862 src = va_add(src, from_msg_size);
3863
3864 /*
3865 * Check if it is safe to copy the rest of the message.
3866 * This also serves as a santiy check to 'endpoint_count'.
3867 * The size of what is left in the descriptor, based on endpoint_count,
3868 * shall not be bigger than the size of the mailbox minus the size of
3869 * the header which was previously copied in this function.
3870 */
3871 dst_size -= from_msg_size;
3872 from_msg_size = relinquish_request->endpoint_count * sizeof(ffa_id_t);
3873 total_from_msg_size += from_msg_size;
3874
3875 if (total_from_msg_size > HF_MAILBOX_SIZE ||
3876 total_from_msg_size > dst_size) {
3877 dlog_verbose(
3878 "Relinquish message too long. Endpoint count: %u\n",
3879 relinquish_request->endpoint_count);
3880 return ffa_error(FFA_INVALID_PARAMETERS);
3881 }
3882
3883 /* Copy the remaining fragment. */
3884 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3885 from_msg_size)) {
3886 dlog_error("%s: Failed to copy FF-A relinquish request.\n",
3887 __func__);
3888 return ffa_error(FFA_ABORTED);
3889 }
3890
3891 /*
3892 * Set the output address for the relinquish descriptor to the current
3893 * cpu's buffer.
3894 */
3895 *out_relinquish = relinquish_request;
3896
3897 return (struct ffa_value){.func = FFA_SUCCESS_32};
3898}
3899
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003900struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003901{
3902 struct vm *from = current->vm;
3903 struct vm_locked from_locked;
3904 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003905 struct ffa_value ret;
J-Alvesa39a8442024-04-26 15:41:47 +01003906 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003907
3908 from_locked = vm_lock(from);
3909 from_msg = from->mailbox.send;
3910
3911 if (from_msg == NULL) {
3912 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003913 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003914 goto out;
3915 }
3916
J-Alvesa39a8442024-04-26 15:41:47 +01003917 ret = api_get_ffa_mem_relinquish_descriptor(current, from_msg,
3918 &relinquish_request);
3919
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003920 /*
J-Alvesa39a8442024-04-26 15:41:47 +01003921 * If the descriptor was safely copied, continue with the handling of
3922 * the retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003923 */
J-Alvesa39a8442024-04-26 15:41:47 +01003924 if (ret.func == FFA_SUCCESS_32) {
3925 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3926 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003927 }
J-Alves8f2150d2024-03-28 16:15:56 +00003928
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003929out:
3930 vm_unlock(&from_locked);
3931 return ret;
3932}
3933
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003934struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3935 ffa_memory_region_flags_t flags,
3936 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003937{
3938 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003939 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003940
Olivier Deprez55a189e2021-06-09 15:45:27 +02003941 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003942 struct vm_locked to_locked = vm_lock(to);
3943
Andrew Walbranca808b12020-05-15 17:22:28 +01003944 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003945 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003946
Andrew Walbran290b0c92020-02-03 16:37:14 +00003947 vm_unlock(&to_locked);
3948 } else {
J-Alvesfc19b372022-07-06 12:17:35 +01003949 ret = plat_ffa_other_world_mem_reclaim(to, handle, flags,
3950 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003951 }
3952
3953 return ret;
3954}
3955
3956struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
3957 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003958 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003959 struct vcpu *current)
3960{
3961 struct vm *to = current->vm;
3962 struct vm_locked to_locked;
3963 struct ffa_value ret;
3964
3965 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01003966 if (vm_id_is_current_world(to->id)) {
3967 if (sender_vm_id != 0) {
3968 dlog_verbose("%s: Invalid sender.\n", __func__);
3969 return ffa_error(FFA_INVALID_PARAMETERS);
3970 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003971 }
3972
3973 to_locked = vm_lock(to);
3974
J-Alves122f1a12022-12-12 15:55:42 +00003975 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003976 /*
3977 * Can't retrieve memory information if the mailbox is not
3978 * available.
3979 */
J-Alves59ed0042022-07-28 18:26:41 +01003980 dlog_verbose("%s: RX buffer not ready partition %x.\n",
3981 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003982 ret = ffa_error(FFA_BUSY);
3983 goto out;
3984 }
3985
J-Alvesc3fd9752024-04-04 11:45:33 +01003986 /*
3987 * Whilst copying the fragments, initialize the remaining constituents
3988 * in the CPU's internal structure, and later copy from the CPU buffer
3989 * into the partition's RX buffer. In the case SPMC is doing a retrieve
3990 * request for a VM/Hypervisor in an RME enabled system, there is no
3991 * guarantee the RX buffer is in the NS PAS. Accessing the buffer with
3992 * the wrong security state attribute would then result in an GPF.
3993 * The fragment is initialized in an internal buffer, and is later
3994 * copied to the RX buffer using the 'memcpy_trapped' which allows to
3995 * smoothly terminate the operation if the access has been preempted by
3996 * a GPF exception.
3997 */
3998 ret = ffa_memory_retrieve_continue(
3999 to_locked, handle, fragment_offset, sender_vm_id,
4000 cpu_get_buffer(current->cpu), &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01004001out:
4002 vm_unlock(&to_locked);
4003 return ret;
4004}
4005
4006struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
4007 uint32_t fragment_length,
J-Alves19e20cf2023-08-02 12:48:55 +01004008 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01004009 struct vcpu *current)
4010{
4011 struct vm *from = current->vm;
4012 const void *from_msg;
4013 void *fragment_copy;
4014 struct ffa_value ret;
4015
4016 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01004017 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
4018 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01004019 return ffa_error(FFA_INVALID_PARAMETERS);
4020 }
4021
4022 /*
4023 * Check that the sender has configured its send buffer. If the TX
4024 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
4025 * be safely accessed after releasing the lock since the TX mailbox
4026 * address can only be configured once.
4027 */
4028 sl_lock(&from->lock);
4029 from_msg = from->mailbox.send;
4030 sl_unlock(&from->lock);
4031
4032 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01004033 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01004034 return ffa_error(FFA_INVALID_PARAMETERS);
4035 }
4036
4037 /*
4038 * Copy the fragment to a fresh page from the memory pool. This prevents
4039 * the sender from changing it underneath us, and also lets us keep it
4040 * around in the share state table if needed.
4041 */
4042 if (fragment_length > HF_MAILBOX_SIZE ||
4043 fragment_length > MM_PPOOL_ENTRY_SIZE) {
4044 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004045 "Fragment length %d larger than mailbox size %zu.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01004046 fragment_length, HF_MAILBOX_SIZE);
4047 return ffa_error(FFA_INVALID_PARAMETERS);
4048 }
4049 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
4050 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
4051 0) {
4052 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
4053 return ffa_error(FFA_INVALID_PARAMETERS);
4054 }
4055 fragment_copy = mpool_alloc(&api_page_pool);
4056 if (fragment_copy == NULL) {
4057 dlog_verbose("Failed to allocate fragment copy.\n");
4058 return ffa_error(FFA_NO_MEMORY);
4059 }
J-Alves8f2150d2024-03-28 16:15:56 +00004060
4061 if (!memcpy_trapped(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg,
4062 fragment_length)) {
4063 dlog_error("%s: Failed to copy fragment.\n", __func__);
4064 return ffa_error(FFA_ABORTED);
4065 }
Andrew Walbranca808b12020-05-15 17:22:28 +01004066
4067 /*
4068 * Hafnium doesn't support fragmentation of memory retrieve requests
4069 * (because it doesn't support caller-specified mappings, so a request
4070 * will never be larger than a single page), so this must be part of a
4071 * memory send (i.e. donate, lend or share) request.
4072 *
4073 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01004074 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01004075 */
J-Alvesfdd29272022-07-19 13:16:31 +01004076 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004077 struct vm_locked from_locked = vm_lock(from);
4078
4079 ret = ffa_memory_send_continue(from_locked, fragment_copy,
4080 fragment_length, handle,
4081 &api_page_pool);
4082 /*
4083 * `ffa_memory_send_continue` takes ownership of the
4084 * fragment_copy, so we don't need to free it here.
4085 */
4086 vm_unlock(&from_locked);
4087 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01004088 ret = plat_ffa_other_world_mem_send_continue(
4089 from, fragment_copy, fragment_length, handle,
4090 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00004091 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00004092
4093 return ret;
4094}
Max Shvetsov40108e72020-08-27 12:39:50 +01004095
Olivier Deprezd614d322021-06-18 15:21:00 +02004096/**
4097 * Register an entry point for a vCPU in warm boot cases.
4098 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
4099 */
Max Shvetsov40108e72020-08-27 12:39:50 +01004100struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
4101 struct vcpu *current)
4102{
4103 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01004104 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01004105
Olivier Deprezd614d322021-06-18 15:21:00 +02004106 /*
4107 * Reject if interface is not supported at this FF-A instance
4108 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
4109 */
4110 if (!plat_ffa_is_secondary_ep_register_supported() ||
Karl Meakin82041822024-05-20 11:14:34 +01004111 vm_is_up(current->vm)) {
Olivier Deprezd614d322021-06-18 15:21:00 +02004112 return ffa_error(FFA_NOT_SUPPORTED);
4113 }
4114
4115 /*
4116 * No further check is made on the address validity
4117 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
4118 * from the VM or vCPU structure.
4119 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
4120 * For each SP [...] the Framework assumes that the same entry point
4121 * address is used for initializing any execution context during a
4122 * secondary cold boot.
4123 * If this function is invoked multiple times, then the entry point
4124 * address specified in the last valid invocation must be used by the
4125 * callee.
4126 */
Olivier Deprezb2808332023-02-02 15:25:40 +01004127 current_locked = vcpu_lock(current);
4128 if (current->rt_model != RTM_SP_INIT) {
4129 dlog_error(
4130 "FFA_SECONDARY_EP_REGISTER can only be called while "
4131 "vCPU in run-time state for initialization.\n");
4132 vcpu_unlock(&current_locked);
4133 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02004134 }
Olivier Deprezb2808332023-02-02 15:25:40 +01004135 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02004136
Olivier Deprezb2808332023-02-02 15:25:40 +01004137 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01004138 vm_locked.vm->secondary_ep = entry_point;
4139 vm_unlock(&vm_locked);
4140
Olivier Deprezb2808332023-02-02 15:25:40 +01004141 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01004142}
J-Alvesa0f317d2021-06-09 13:31:59 +01004143
J-Alves19e20cf2023-08-02 12:48:55 +01004144struct ffa_value api_ffa_notification_bitmap_create(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004145 ffa_vcpu_count_t vcpu_count,
4146 struct vcpu *current)
4147{
J-Alves7ccaccf2024-02-01 14:25:23 +00004148 const struct ffa_value ret =
4149 plat_ffa_is_notifications_bitmap_access_valid(current, vm_id);
4150
4151 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4152 dlog_verbose(
4153 "FFA_NOTIFICATION_BITMAP_CREATE to be used by "
4154 "hypervisor for valid NWd VM IDs only (%x).\n",
4155 vm_id);
4156 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004157 }
4158
4159 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
4160}
4161
J-Alves19e20cf2023-08-02 12:48:55 +01004162struct ffa_value api_ffa_notification_bitmap_destroy(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004163 struct vcpu *current)
4164{
J-Alves7ccaccf2024-02-01 14:25:23 +00004165 const struct ffa_value ret =
4166 plat_ffa_is_notifications_bitmap_access_valid(current, vm_id);
4167
4168 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4169 dlog_verbose(
4170 "FFA_NOTIFICATION_BITMAP_DESTROY to be used by "
4171 "hypervisor for valid NWd VM IDs only (%x).\n",
4172 vm_id);
4173 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004174 }
4175
4176 return plat_ffa_notifications_bitmap_destroy(vm_id);
4177}
J-Alvesc003a7a2021-03-18 13:06:53 +00004178
4179struct ffa_value api_ffa_notification_update_bindings(
J-Alves19e20cf2023-08-02 12:48:55 +01004180 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesc003a7a2021-03-18 13:06:53 +00004181 ffa_notifications_bitmap_t notifications, bool is_bind,
4182 struct vcpu *current)
4183{
4184 struct ffa_value ret = {.func = FFA_SUCCESS_32};
4185 struct vm_locked receiver_locked;
4186 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
J-Alves19e20cf2023-08-02 12:48:55 +01004187 const ffa_id_t id_to_update = is_bind ? sender_vm_id : HF_INVALID_VM_ID;
4188 const ffa_id_t id_to_validate =
J-Alvesc003a7a2021-03-18 13:06:53 +00004189 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01004190 const uint32_t flags_mbz =
4191 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
4192
4193 if ((flags_mbz & flags) != 0U) {
4194 return ffa_error(FFA_INVALID_PARAMETERS);
4195 }
J-Alvesc003a7a2021-03-18 13:06:53 +00004196
4197 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
4198 receiver_vm_id)) {
4199 dlog_verbose("Invalid use of notifications bind interface.\n");
4200 return ffa_error(FFA_INVALID_PARAMETERS);
4201 }
4202
J-Alvesb15e9402021-09-08 11:44:42 +01004203 if (plat_ffa_notifications_update_bindings_forward(
4204 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
4205 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01004206 return ret;
4207 }
4208
J-Alvesc003a7a2021-03-18 13:06:53 +00004209 if (notifications == 0U) {
Karl Meakine8937d92024-03-19 16:04:25 +00004210 dlog_verbose("No notifications have been specified %lx.\n",
J-Alves1512acc2024-02-01 16:57:10 +00004211 notifications);
J-Alvesc003a7a2021-03-18 13:06:53 +00004212 return ffa_error(FFA_INVALID_PARAMETERS);
4213 }
4214
4215 /**
4216 * This check assumes receiver is the current VM, and has been enforced
4217 * by 'plat_ffa_is_notifications_bind_valid'.
4218 */
4219 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4220
4221 if (receiver_locked.vm == NULL) {
4222 dlog_verbose("Receiver doesn't exist!\n");
J-Alves7333dcf2024-02-01 17:25:10 +00004223 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesc003a7a2021-03-18 13:06:53 +00004224 }
4225
J-Alves3e55c4d2024-10-09 14:24:07 +01004226 if (receiver_locked.vm->ffa_version < FFA_VERSION_1_1) {
4227 dlog_verbose(
4228 "%s: caller (%x) version should be GE to FF-A v1.1.\n",
4229 __func__, receiver_locked.vm->id);
4230 ret = ffa_error(FFA_NOT_SUPPORTED);
4231 goto out;
4232 }
4233
J-Alves09ff9d82021-11-02 11:55:20 +00004234 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004235 dlog_verbose("Notifications are not enabled.\n");
4236 ret = ffa_error(FFA_NOT_SUPPORTED);
4237 goto out;
4238 }
4239
4240 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
4241 vm_find(sender_vm_id) == NULL) {
4242 dlog_verbose("Sender VM does not exist!\n");
4243 ret = ffa_error(FFA_INVALID_PARAMETERS);
4244 goto out;
4245 }
4246
4247 /*
4248 * Can't bind/unbind notifications if at least one is bound to a
4249 * different sender.
4250 */
4251 if (!vm_notifications_validate_bound_sender(
J-Alves661e1b72023-08-02 13:39:40 +01004252 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_validate,
4253 notifications)) {
J-Alvesf0208142024-02-01 17:28:58 +00004254 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004255 "Sender %x not permitted to set notifications %lx to "
J-Alvesf0208142024-02-01 17:28:58 +00004256 "%x.\n",
4257 sender_vm_id, notifications, receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004258 ret = ffa_error(FFA_DENIED);
4259 goto out;
4260 }
4261
4262 /**
4263 * Check if there is a pending notification within those specified in
4264 * the bitmap.
4265 */
4266 if (vm_are_notifications_pending(receiver_locked,
J-Alves661e1b72023-08-02 13:39:40 +01004267 ffa_is_vm_id(sender_vm_id),
J-Alvesc003a7a2021-03-18 13:06:53 +00004268 notifications)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004269 dlog_verbose("Notifications within '%lx' pending.\n",
J-Alvesc003a7a2021-03-18 13:06:53 +00004270 notifications);
4271 ret = ffa_error(FFA_DENIED);
4272 goto out;
4273 }
4274
4275 vm_notifications_update_bindings(
J-Alves661e1b72023-08-02 13:39:40 +01004276 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_update,
J-Alvesc003a7a2021-03-18 13:06:53 +00004277 notifications, is_per_vcpu && is_bind);
4278
4279out:
4280 vm_unlock(&receiver_locked);
4281 return ret;
4282}
J-Alvesaa79c012021-07-09 14:29:45 +01004283
4284struct ffa_value api_ffa_notification_set(
J-Alves19e20cf2023-08-02 12:48:55 +01004285 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesaa79c012021-07-09 14:29:45 +01004286 ffa_notifications_bitmap_t notifications, struct vcpu *current)
4287{
4288 struct ffa_value ret;
4289 struct vm_locked receiver_locked;
J-Alvesaa79c012021-07-09 14:29:45 +01004290 /*
4291 * Check if is per-vCPU or global, and extracting vCPU ID according
4292 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
4293 */
4294 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
4295 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
J-Alves6b754a12024-02-14 14:26:30 +00004296 const uint32_t flags_mbz =
4297 ~(FFA_NOTIFICATIONS_FLAG_PER_VCPU |
4298 FFA_NOTIFICATIONS_FLAG_DELAY_SRI | (0xFFFFU << 16));
J-Alves41c5da32024-06-19 10:05:05 +01004299 const bool delay_sri = (FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) != 0U;
J-Alves6b754a12024-02-14 14:26:30 +00004300
4301 if ((flags_mbz & flags) != 0U) {
4302 dlog_verbose("%s: caller shouldn't set bits that MBZ.\n",
4303 __func__);
4304 return ffa_error(FFA_INVALID_PARAMETERS);
4305 }
J-Alvesaa79c012021-07-09 14:29:45 +01004306
J-Alves30ac91d2024-02-14 15:33:12 +00004307 /* Global notifications must target any vCPU. */
4308 if (!is_per_vcpu && vcpu_id != 0U) {
4309 dlog_verbose(
4310 "For global notifications vCPU ID MBZ in call to set "
4311 "notifications.\n");
4312 return ffa_error(FFA_INVALID_PARAMETERS);
4313 }
4314
J-Alvesaa79c012021-07-09 14:29:45 +01004315 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
4316 receiver_vm_id)) {
4317 dlog_verbose("Invalid use of notifications set interface.\n");
4318 return ffa_error(FFA_INVALID_PARAMETERS);
4319 }
4320
4321 if (notifications == 0U) {
4322 dlog_verbose("No notifications have been specified.\n");
4323 return ffa_error(FFA_INVALID_PARAMETERS);
4324 }
4325
J-Alves41c5da32024-06-19 10:05:05 +01004326 /*
4327 * The 'Delay Schedule Receiver interrupt flag' only applies to the
4328 * secure virtual FF-A instance.
4329 */
4330 if (!vm_id_is_current_world(sender_vm_id) && delay_sri) {
4331 dlog_verbose(
4332 "The delay SRI flag can only be set at the secure "
4333 "virtual FF-A instance.\n");
4334 return ffa_error(FFA_INVALID_PARAMETERS);
4335 }
4336
J-Alvesde7bd2f2021-09-09 19:54:35 +01004337 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
4338 flags, notifications, &ret)) {
4339 return ret;
4340 }
4341
J-Alvesaa79c012021-07-09 14:29:45 +01004342 /*
4343 * This check assumes receiver is the current VM, and has been enforced
4344 * by 'plat_ffa_is_notification_set_valid'.
4345 */
4346 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4347
4348 if (receiver_locked.vm == NULL) {
4349 dlog_verbose("Receiver ID is not valid.\n");
4350 return ffa_error(FFA_INVALID_PARAMETERS);
4351 }
4352
J-Alves09ff9d82021-11-02 11:55:20 +00004353 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004354 dlog_verbose("Receiver's notifications not enabled.\n");
4355 ret = ffa_error(FFA_DENIED);
4356 goto out;
4357 }
4358
4359 /*
J-Alves89c22132024-02-14 15:44:41 +00004360 * Check the if the notifications are bound as global if per-vCPU flag
4361 * is set, or if they are bound as per-vCPU and caller setting as
4362 * global. In either case, return FFA_INVALID_PARAMETERS.
4363 */
4364 if (vm_notifications_validate_binding(
4365 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4366 notifications, !is_per_vcpu)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004367 dlog_verbose("Notifications in %lx are %s\n", notifications,
J-Alves89c22132024-02-14 15:44:41 +00004368 !is_per_vcpu ? "global" : "per-vCPU");
4369 ret = ffa_error(FFA_INVALID_PARAMETERS);
4370 goto out;
4371 }
4372
4373 /*
J-Alvesaa79c012021-07-09 14:29:45 +01004374 * If notifications are not bound to the sender, they wouldn't be
4375 * enabled either for the receiver.
4376 */
4377 if (!vm_notifications_validate_binding(
J-Alves661e1b72023-08-02 13:39:40 +01004378 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4379 notifications, is_per_vcpu)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004380 dlog_verbose("Notifications bindings not valid.\n");
4381 ret = ffa_error(FFA_DENIED);
4382 goto out;
4383 }
4384
4385 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
4386 dlog_verbose("Invalid VCPU ID!\n");
4387 ret = ffa_error(FFA_INVALID_PARAMETERS);
4388 goto out;
4389 }
4390
J-Alves7461ef22021-10-18 17:21:33 +01004391 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00004392 vm_notifications_partition_set_pending(
J-Alves661e1b72023-08-02 13:39:40 +01004393 receiver_locked, ffa_is_vm_id(sender_vm_id), notifications,
J-Alves5a16c962022-03-25 12:32:51 +00004394 vcpu_id, is_per_vcpu);
4395
Karl Meakine8937d92024-03-19 16:04:25 +00004396 dlog_verbose("Set the notifications: %lx.\n", notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004397
J-Alves41c5da32024-06-19 10:05:05 +01004398 if (!delay_sri) {
J-Alves13394022021-06-30 13:48:49 +01004399 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
4400 vcpu_index(current));
4401 plat_ffa_sri_trigger_not_delayed(current->cpu);
4402 } else {
J-Alvesea8ccfe2024-10-09 11:47:24 +01004403 plat_ffa_sri_set_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004404 }
J-Alvesaa79c012021-07-09 14:29:45 +01004405
J-Alves13394022021-06-30 13:48:49 +01004406 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01004407out:
4408 vm_unlock(&receiver_locked);
4409
4410 return ret;
4411}
4412
4413static struct ffa_value api_ffa_notification_get_success_return(
4414 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
4415 ffa_notifications_bitmap_t from_framework)
4416{
4417 return (struct ffa_value){
4418 .func = FFA_SUCCESS_32,
4419 .arg1 = 0U,
4420 .arg2 = (uint32_t)from_sp,
4421 .arg3 = (uint32_t)(from_sp >> 32),
4422 .arg4 = (uint32_t)from_vm,
4423 .arg5 = (uint32_t)(from_vm >> 32),
4424 .arg6 = (uint32_t)from_framework,
4425 .arg7 = (uint32_t)(from_framework >> 32),
4426 };
4427}
4428
J-Alves19e20cf2023-08-02 12:48:55 +01004429struct ffa_value api_ffa_notification_get(ffa_id_t receiver_vm_id,
J-Alvesaa79c012021-07-09 14:29:45 +01004430 ffa_vcpu_index_t vcpu_id,
4431 uint32_t flags, struct vcpu *current)
4432{
J-Alves663682a2022-03-25 13:56:51 +00004433 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01004434 ffa_notifications_bitmap_t sp_notifications = 0;
4435 ffa_notifications_bitmap_t vm_notifications = 0;
4436 struct vm_locked receiver_locked;
4437 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01004438 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
4439 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
4440 FFA_NOTIFICATION_FLAG_BITMAP_SP |
4441 FFA_NOTIFICATION_FLAG_BITMAP_VM);
4442
4443 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
4444 if ((flags & flags_mbz) != 0U) {
4445 dlog_verbose(
4446 "Invalid flags bit(s) set in notifications get. [31:4] "
4447 "MBZ(%x)\n",
4448 flags);
4449 return ffa_error(FFA_INVALID_PARAMETERS);
4450 }
J-Alvesaa79c012021-07-09 14:29:45 +01004451
4452 /*
J-Alvesfc95a302022-04-22 14:18:23 +01004453 * Following check should capture wrong uses of the interface,
4454 * depending on whether Hafnium is SPMC or hypervisor. On the
4455 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01004456 */
J-Alvesfc95a302022-04-22 14:18:23 +01004457 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
4458 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004459 dlog_verbose("Invalid use of notifications get interface.\n");
4460 return ffa_error(FFA_INVALID_PARAMETERS);
4461 }
4462
4463 /*
4464 * This check assumes receiver is the current VM, and has been enforced
4465 * by `plat_ffa_is_notifications_get_valid`.
4466 */
4467 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4468
4469 /*
4470 * `plat_ffa_is_notifications_get_valid` ensures following is never
4471 * true.
4472 */
4473 CHECK(receiver_locked.vm != NULL);
4474
J-Alves60458812024-03-22 11:57:10 +00004475 if (receiver_locked.vm->vcpu_count <= vcpu_id) {
J-Alves1abb3342022-01-05 11:59:10 +00004476 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004477 "Invalid VCPU ID %u. vcpu count %u current core: "
4478 "%zu!\n",
J-Alves1abb3342022-01-05 11:59:10 +00004479 vcpu_id, receiver_locked.vm->vcpu_count,
4480 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01004481 ret = ffa_error(FFA_INVALID_PARAMETERS);
4482 goto out;
4483 }
4484
4485 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
Karl Meakinf9c73ce2024-07-30 17:37:13 +01004486 ret = plat_ffa_notifications_get_from_sp(
4487 receiver_locked, vcpu_id, &sp_notifications);
4488 if (ret.func == FFA_ERROR_32) {
J-Alves98ff9562021-09-09 14:39:41 +01004489 dlog_verbose("Failed to get notifications from sps.");
4490 goto out;
4491 }
J-Alvesaa79c012021-07-09 14:29:45 +01004492 }
4493
4494 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00004495 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01004496 receiver_locked, true, vcpu_id);
4497 }
4498
J-Alvesd605a092022-03-28 14:20:48 +01004499 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
4500 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
Karl Meakinf9c73ce2024-07-30 17:37:13 +01004501 ret = plat_ffa_notifications_get_framework_notifications(
4502 receiver_locked, &framework_notifications, flags,
4503 vcpu_id);
4504 if (ret.func == FFA_ERROR_32) {
J-Alvesd605a092022-03-28 14:20:48 +01004505 dlog_verbose(
4506 "Failed to get notifications from "
4507 "framework.\n");
4508 goto out;
4509 }
4510 }
4511
J-Alves663682a2022-03-25 13:56:51 +00004512 ret = api_ffa_notification_get_success_return(
4513 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004514
J-Alves6e2abc62021-12-02 14:58:56 +00004515 if (!receiver_locked.vm->el0_partition &&
4516 !vm_are_global_notifications_pending(receiver_locked)) {
4517 vm_notifications_set_npi_injected(receiver_locked, false);
4518 }
4519
J-Alvesaa79c012021-07-09 14:29:45 +01004520out:
4521 vm_unlock(&receiver_locked);
4522
4523 return ret;
4524}
J-Alvesc8e8a222021-06-08 17:33:52 +01004525
4526/**
4527 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
4528 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
4529 */
4530static struct ffa_value api_ffa_notification_info_get_success_return(
4531 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01004532 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01004533{
4534 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
4535
4536 /*
4537 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
4538 * hold the list of ids.
4539 */
4540 memcpy_s(&ret.arg3,
4541 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
4542 sizeof(ids[0]) * ids_count);
4543
4544 /*
4545 * According to the spec x2 should have:
4546 * - Bit flagging if there are more notifications pending;
4547 * - The total number of elements (i.e. total list size);
4548 * - The number of VCPU IDs within each VM specific list.
4549 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004550 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
4551 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
4552 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01004553
4554 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
4555 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
4556
4557 for (unsigned int i = 0; i < lists_count; i++) {
4558 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
4559 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
4560 }
4561
4562 return ret;
4563}
4564
4565struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
4566{
4567 /*
4568 * Following set of variables should be populated with the return info.
4569 * At a successfull handling of this interface, they should be used
4570 * to populate the 'ret' structure in accordance to the table 17.29
4571 * of the FF-A v1.1 Beta0 specification.
4572 */
4573 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
4574 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
4575 uint32_t lists_count = 0;
4576 uint32_t ids_count = 0;
4577 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01004578 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004579
4580 /*
4581 * This interface can only be called at NS virtual/physical FF-A
4582 * instance by the endpoint implementing the primary scheduler and the
4583 * Hypervisor/OS kernel.
4584 * In the SPM, following check passes if call has been forwarded from
4585 * the hypervisor.
4586 */
Karl Meakin5e996992024-05-20 11:27:07 +01004587
4588 if (!vm_is_primary(current->vm)) {
J-Alvesc8e8a222021-06-08 17:33:52 +01004589 dlog_verbose(
4590 "Only the receiver's scheduler can use this "
4591 "interface\n");
4592 return ffa_error(FFA_NOT_SUPPORTED);
4593 }
4594
J-Alvesca058c22021-09-10 14:02:07 +01004595 /*
4596 * Forward call to the other world, and fill the arrays used to assemble
4597 * return.
4598 */
4599 plat_ffa_notification_info_get_forward(
4600 ids, &ids_count, lists_sizes, &lists_count,
4601 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4602
4603 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
4604
J-Alvesc8e8a222021-06-08 17:33:52 +01004605 /* Get notifications' info from this world */
4606 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
4607 ++index) {
4608 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
4609
4610 list_is_full = vm_notifications_info_get(
4611 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
4612 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4613
4614 vm_unlock(&vm_locked);
4615 }
4616
4617 if (!list_is_full) {
4618 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004619 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01004620 ids, &ids_count, lists_sizes, &lists_count,
4621 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4622 }
4623
4624 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01004625 dlog_verbose(
4626 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01004627 result = ffa_error(FFA_NO_DATA);
4628 } else {
4629 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01004630 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01004631 }
4632
J-Alves13394022021-06-30 13:48:49 +01004633 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004634}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004635
4636struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
4637{
4638 struct vm_locked vm_locked;
4639 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
4640 bool mode_ret = false;
4641 uint32_t mode = 0;
4642
4643 if (!plat_ffa_is_mem_perm_get_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004644 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004645 }
4646
4647 if (!(current->vm->el0_partition)) {
4648 return ffa_error(FFA_DENIED);
4649 }
4650
4651 vm_locked = vm_lock(current->vm);
4652
4653 /*
4654 * mm_get_mode is used to check if the given base_addr page is already
4655 * mapped. If the page is unmapped, return error. If the page is mapped
4656 * appropriate attributes are returned to the caller. Note that
4657 * mm_get_mode returns true if the address is in the valid VA range as
4658 * supported by the architecture and MMU configurations, as opposed to
4659 * whether a page is mapped or not. For a page to be known as mapped,
4660 * the API must return true AND the returned mode must not have
4661 * MM_MODE_INVALID set.
4662 */
4663 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4664 va_add(base_addr, PAGE_SIZE), &mode);
4665 if (!mode_ret || (mode & MM_MODE_INVALID)) {
4666 ret = ffa_error(FFA_INVALID_PARAMETERS);
4667 goto out;
4668 }
4669
4670 /* No memory should be marked RWX */
4671 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
4672 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
4673
4674 /*
4675 * S-EL0 partitions are expected to have all their pages marked as
4676 * non-global.
4677 */
4678 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
4679 (MM_MODE_NG | MM_MODE_USER));
4680
4681 if (mode & MM_MODE_W) {
4682 /* No memory should be writeable but not readable. */
4683 CHECK(mode & MM_MODE_R);
4684 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4685 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
4686 } else if (mode & MM_MODE_R) {
4687 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4688 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
4689 if (!(mode & MM_MODE_X)) {
4690 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
4691 }
4692 }
4693out:
4694 vm_unlock(&vm_locked);
4695 return ret;
4696}
4697
4698struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
4699 uint32_t mem_perm, struct vcpu *current)
4700{
4701 struct vm_locked vm_locked;
4702 struct ffa_value ret;
4703 bool mode_ret = false;
4704 uint32_t original_mode;
4705 uint32_t new_mode;
4706 struct mpool local_page_pool;
4707
4708 if (!plat_ffa_is_mem_perm_set_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004709 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004710 }
4711
4712 if (!(current->vm->el0_partition)) {
4713 return ffa_error(FFA_DENIED);
4714 }
4715
4716 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
4717 return ffa_error(FFA_INVALID_PARAMETERS);
4718 }
4719
4720 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
4721 (mem_perm != FFA_MEM_PERM_RX)) {
4722 return ffa_error(FFA_INVALID_PARAMETERS);
4723 }
4724
4725 /*
4726 * Create a local pool so any freed memory can't be used by another
4727 * thread. This is to ensure the original mapping can be restored if any
4728 * stage of the process fails.
4729 */
4730 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
4731
4732 vm_locked = vm_lock(current->vm);
4733
4734 /*
4735 * All regions accessible by the partition are mapped during boot. If we
4736 * cannot get a successful translation for the page range, the request
4737 * to change permissions is rejected.
4738 * mm_get_mode is used to check if the given address range is already
4739 * mapped. If the range is unmapped, return error. If the range is
4740 * mapped appropriate attributes are returned to the caller. Note that
4741 * mm_get_mode returns true if the address is in the valid VA range as
4742 * supported by the architecture and MMU configurations, as opposed to
4743 * whether a page is mapped or not. For a page to be known as mapped,
4744 * the API must return true AND the returned mode must not have
4745 * MM_MODE_INVALID set.
4746 */
4747
4748 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4749 va_add(base_addr, page_count * PAGE_SIZE),
4750 &original_mode);
4751 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
4752 ret = ffa_error(FFA_INVALID_PARAMETERS);
4753 goto out;
4754 }
4755
4756 /* Device memory cannot be marked as executable */
4757 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
4758 ret = ffa_error(FFA_INVALID_PARAMETERS);
4759 goto out;
4760 }
4761
4762 new_mode = MM_MODE_USER | MM_MODE_NG;
4763
4764 if (mem_perm == FFA_MEM_PERM_RW) {
4765 new_mode |= MM_MODE_R | MM_MODE_W;
4766 } else if (mem_perm == FFA_MEM_PERM_RX) {
4767 new_mode |= MM_MODE_R | MM_MODE_X;
4768 } else if (mem_perm == FFA_MEM_PERM_RO) {
4769 new_mode |= MM_MODE_R;
4770 }
4771
4772 /*
4773 * Safe to re-map memory, since we know the requested permissions are
4774 * valid, and the memory requested to be re-mapped is also valid.
4775 */
4776 if (!mm_identity_prepare(
4777 &vm_locked.vm->ptable, pa_from_va(base_addr),
4778 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4779 new_mode, &local_page_pool)) {
4780 /*
4781 * Defrag the table into the local page pool.
4782 * mm_identity_prepare could have allocated or freed pages to
4783 * split blocks or tables etc.
4784 */
4785 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
4786
4787 /*
4788 * Guaranteed to succeed mapping with old mode since the mapping
4789 * with old mode already existed and we have a local page pool
4790 * that should have sufficient memory to go back to the original
4791 * state.
4792 */
4793 CHECK(mm_identity_prepare(
4794 &vm_locked.vm->ptable, pa_from_va(base_addr),
4795 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4796 original_mode, &local_page_pool));
4797 mm_identity_commit(
4798 &vm_locked.vm->ptable, pa_from_va(base_addr),
4799 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4800 original_mode, &local_page_pool);
4801
4802 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
4803 ret = ffa_error(FFA_NO_MEMORY);
4804 goto out;
4805 }
4806
4807 mm_identity_commit(
4808 &vm_locked.vm->ptable, pa_from_va(base_addr),
4809 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
4810 &local_page_pool);
4811
4812 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
4813
4814out:
4815 mpool_fini(&local_page_pool);
4816 vm_unlock(&vm_locked);
4817
4818 return ret;
4819}
Maksims Svecovs71b76702022-05-20 15:32:58 +01004820
4821/**
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004822 * Send the contents of the given vCPU's log buffer to the log, preceded
Karl Meakin705b56e2024-05-22 10:04:32 +01004823 * by the VM ID and followed by a newline.
4824 */
Karl Meakin6f1f1212024-07-16 10:18:16 +01004825void api_flush_log_buffer(struct vcpu_locked *vcpu_locked)
Karl Meakin705b56e2024-05-22 10:04:32 +01004826{
Karl Meakin6f1f1212024-07-16 10:18:16 +01004827 /*
4828 * NOTE: This line is parsed by `hftest.py`.
4829 * If you change the format, make sure to update
4830 * `HFTEST_CTRL_JSON_REGEX` as well.
4831 */
4832 struct vcpu *vcpu = vcpu_locked->vcpu;
4833 struct log_buffer *buffer = &vcpu->log_buffer;
4834 ffa_id_t vm_id = vcpu->vm->id;
4835 ffa_id_t vcpu_id = vcpu_index(vcpu);
4836
Karl Meakin7de26952024-06-14 14:50:19 +01004837 buffer->chars[buffer->len] = '\0';
Olivier Deprezbd432092024-07-26 14:01:12 +02004838 dlog("[%x %u] %s\n", vm_id, vcpu_id, buffer->chars);
Karl Meakin7de26952024-06-14 14:50:19 +01004839 buffer->len = 0;
Karl Meakin705b56e2024-05-22 10:04:32 +01004840}
4841
4842/**
Karl Meakin740f74f2024-02-14 18:04:48 +00004843 * Implements FF-A v1.2 FFA_CONSOLE_LOG ABI for buffered logging.
Maksims Svecovs71b76702022-05-20 15:32:58 +01004844 */
4845struct ffa_value api_ffa_console_log(const struct ffa_value args,
4846 struct vcpu *current)
4847{
Karl Meakin740f74f2024-02-14 18:04:48 +00004848 /* Maximum number of characters is 128: 16 registers of 8 bytes each. */
4849 char chars[128] = {0};
Karl Meakin0e617d92024-04-05 12:55:22 +01004850 const bool v1_2 = current->vm->ffa_version >= FFA_VERSION_1_2;
Karl Meakin740f74f2024-02-14 18:04:48 +00004851 const bool log32 = args.func == FFA_CONSOLE_LOG_32;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004852
Karl Meakin740f74f2024-02-14 18:04:48 +00004853 /*
4854 * 32bit: always 6 registers
4855 * 64bit and less than v1.2: 6 registers
4856 * 64bit and v1.2 or greater: 16 registers
4857 */
Karl Meakin66a38bd2024-05-28 16:00:56 +01004858 /* NOLINTNEXTLINE(readability-avoid-nested-conditional-operator) */
Karl Meakin740f74f2024-02-14 18:04:48 +00004859 const size_t registers_max = log32 ? 6 : (v1_2 ? 16 : 6);
4860 const size_t chars_max =
4861 registers_max * (log32 ? sizeof(uint32_t) : sizeof(uint64_t));
4862 const size_t chars_count = args.arg1;
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004863 struct vcpu_locked vcpu_locked;
4864 struct log_buffer *log_buffer;
Karl Meakin740f74f2024-02-14 18:04:48 +00004865
4866 assert(args.func == FFA_CONSOLE_LOG_32 ||
4867 args.func == FFA_CONSOLE_LOG_64);
4868
4869 if (chars_count == 0 || chars_count > chars_max) {
Maksims Svecovs71b76702022-05-20 15:32:58 +01004870 return ffa_error(FFA_INVALID_PARAMETERS);
4871 }
4872
Karl Meakin740f74f2024-02-14 18:04:48 +00004873 if (log32) {
4874 uint32_t *registers = (uint32_t *)chars;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004875
Karl Meakin740f74f2024-02-14 18:04:48 +00004876 registers[0] = args.arg2 & 0xffffffff;
4877 registers[1] = args.arg3 & 0xffffffff;
4878 registers[2] = args.arg4 & 0xffffffff;
4879 registers[3] = args.arg5 & 0xffffffff;
4880 registers[4] = args.arg6 & 0xffffffff;
4881 registers[5] = args.arg7 & 0xffffffff;
4882 } else {
4883 uint64_t *registers = (uint64_t *)chars;
4884
4885 registers[0] = args.arg2;
4886 registers[1] = args.arg3;
4887 registers[2] = args.arg4;
4888 registers[3] = args.arg5;
4889 registers[4] = args.arg6;
4890 registers[5] = args.arg7;
4891 if (v1_2) {
4892 registers[6] = args.extended_val.arg8;
4893 registers[7] = args.extended_val.arg9;
4894 registers[8] = args.extended_val.arg10;
4895 registers[9] = args.extended_val.arg11;
4896 registers[10] = args.extended_val.arg12;
4897 registers[11] = args.extended_val.arg13;
4898 registers[12] = args.extended_val.arg14;
4899 registers[13] = args.extended_val.arg15;
4900 registers[14] = args.extended_val.arg16;
4901 registers[15] = args.extended_val.arg17;
4902 }
4903 }
4904
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004905 vcpu_locked = vcpu_lock(current);
4906 log_buffer = &current->log_buffer;
Karl Meakin7de26952024-06-14 14:50:19 +01004907
Karl Meakin740f74f2024-02-14 18:04:48 +00004908 for (size_t i = 0; i < chars_count; i++) {
4909 bool flush = false;
4910 const char c = chars[i];
4911
4912 if (c == '\n' || c == '\0') {
4913 flush = true;
4914 } else {
Karl Meakin7de26952024-06-14 14:50:19 +01004915 log_buffer->chars[log_buffer->len++] = c;
4916 flush = log_buffer->len == LOG_BUFFER_SIZE;
Karl Meakin740f74f2024-02-14 18:04:48 +00004917 }
4918
4919 if (flush) {
Karl Meakin6f1f1212024-07-16 10:18:16 +01004920 api_flush_log_buffer(&vcpu_locked);
Karl Meakin740f74f2024-02-14 18:04:48 +00004921 }
4922 }
Maksims Svecovs71b76702022-05-20 15:32:58 +01004923
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004924 vcpu_unlock(&vcpu_locked);
Maksims Svecovs71b76702022-05-20 15:32:58 +01004925 return (struct ffa_value){.func = FFA_SUCCESS_32};
4926}
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004927
4928/**
4929 * Send an IPI interrupt to a target vcpu belonging to the
4930 * sender that isn't itself.
4931 */
Daniel Boulby84d49b62024-11-04 18:25:59 +00004932int64_t api_hf_interrupt_send_ipi(uint32_t target_vcpu_id, struct vcpu *current)
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004933{
4934 struct vm *vm = current->vm;
4935 ffa_vcpu_index_t target_vcpu_index = vcpu_id_to_index(target_vcpu_id);
4936
Daniel Boulby84d49b62024-11-04 18:25:59 +00004937 if (target_vcpu_index >= vm->vcpu_count ||
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004938 target_vcpu_index == cpu_index(current->cpu)) {
4939 dlog_verbose("Invalid vCPU %d for IPI.\n", target_vcpu_id);
4940 return -1;
4941 }
4942
4943 dlog_verbose("Injecting IPI to target vCPU%d for %#x\n", target_vcpu_id,
4944 vm->id);
4945
4946 hf_ipi_send_interrupt(vm, target_vcpu_index);
4947
4948 return 0;
4949}