blob: 14c78870feb918472f97aaeb8cba7e40b84402d6 [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 Meakined91e992025-02-01 16:16:27 +000018#include "hf/addr.h"
Karl Meakin49ec1e42024-05-10 13:08:24 +010019#include "hf/bits.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010020#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000021#include "hf/dlog.h"
Karl Meakin902af082024-11-28 14:58:38 +000022#include "hf/ffa.h"
23#include "hf/ffa/cpu_cycles.h"
24#include "hf/ffa/direct_messaging.h"
25#include "hf/ffa/ffa_memory.h"
26#include "hf/ffa/indirect_messaging.h"
27#include "hf/ffa/interrupts.h"
28#include "hf/ffa/notifications.h"
Karl Meakin902af082024-11-28 14:58:38 +000029#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{
Karl Meakin117c8082024-12-04 16:03:28 +0000198 return (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);
Karl Meakin117c8082024-12-04 16:03:28 +0000298 transition_allowed = ffa_cpu_cycles_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
Karl Meakin117c8082024-12-04 16:03:28 +0000313 * in the SPMC variant of the ffa_cpu_cycles_yield_prepare function.
Madhukar Pappireddy65764072023-05-23 17:18:58 -0500314 */
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
Karl Meakin117c8082024-12-04 16:03:28 +0000318 ret = ffa_cpu_cycles_yield_prepare(current_locked, next, timeout_low,
319 timeout_high);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600320out:
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);
Karl Meakin117c8082024-12-04 16:03:28 +0000377 ffa_vm_free_resources(vm_locked);
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500378 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. */
Karl Meakin117c8082024-12-04 16:03:28 +0000402 if (!ffa_setup_acquire_receiver_rx(vm_locked, &ret)) {
J-Alvese8c8c2b2022-12-16 15:34:48 +0000403 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
Karl Meakin117c8082024-12-04 16:03:28 +0000490 properties = ffa_setup_partition_properties(caller_id, vm);
J-Alves7fb0fdb2024-10-09 11:28:07 +0100491 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,
Daniel Boulby48546b12025-01-08 18:22:13 +0000536 ffa_id_t caller_id, enum ffa_version caller_version)
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 }
Daniel Boulby48546b12025-01-08 18:22:13 +0000576
577 /*
578 * Multiple UUIDs for a partition was only
579 * introduced in FF-A v1.2, so for any version
580 * less than v1.2 return only one UUID per
581 * partition.
582 */
583 if (caller_version < FFA_VERSION_1_2) {
584 break;
585 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800586 }
587 }
588 }
589
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100590 return vms_found;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800591}
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800592
593static inline void api_ffa_pack_vmid_count_props(
J-Alves19e20cf2023-08-02 12:48:55 +0100594 uint64_t *xn, ffa_id_t vm_id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800595 ffa_partition_properties_t properties)
596{
597 *xn = (uint64_t)vm_id;
598 *xn |= (uint64_t)vcpu_count << 16;
599 *xn |= (uint64_t)properties << 32;
600}
601
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700602/**
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700603 * This function forwards the FFA_PARTITION_INFO_GET_REGS ABI to the other world
604 * when hafnium is the hypervisor to determine the secure partitions. When
605 * hafnium is the SPMC, this function forwards the call to the SPMD to discover
606 * SPMD logical partitions. The function returns true when partition information
607 * is filled in the partitions array and false if there are errors. Note that
608 * the SPMD and SPMC may return an FF-A error code of FFA_NOT_SUPPORTED when
609 * there are no SPMD logical partitions or no secure partitions respectively,
610 * and this is not considered a failure of the forwarded call. A caller is
611 * expected to check the return value before consuming the information in the
612 * partitions array passed in and ret_count.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700613 */
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700614static bool api_ffa_partition_info_get_regs_forward(
615 const struct ffa_uuid *uuid, const uint16_t tag,
616 struct ffa_partition_info *partitions, uint16_t partitions_len,
617 ffa_vm_count_t *ret_count)
618{
619 (void)tag;
620 struct ffa_value ret;
621 uint16_t last_index = UINT16_MAX;
622 uint16_t curr_index = 0;
623 uint16_t start_index = 0;
624
Karl Meakin117c8082024-12-04 16:03:28 +0000625 if (!ffa_setup_partition_info_get_regs_forward_allowed()) {
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700626 return true;
627 }
628
629 while (start_index <= last_index) {
630 ret = ffa_partition_info_get_regs(uuid, start_index, 0);
631 if (ffa_func_id(ret) != FFA_SUCCESS_64) {
632 /*
633 * If there are no logical partitions, SPMD returns
634 * NOT_SUPPORTED, that is not an error. If there are no
635 * secure partitions the SPMC returns NOT_SUPPORTED.
636 */
637 if ((ffa_func_id(ret) == FFA_ERROR_32) &&
638 (ffa_error_code(ret) == FFA_NOT_SUPPORTED)) {
639 return true;
640 }
641
642 return false;
643 }
644
645 if (!api_ffa_fill_partition_info_from_regs(
646 ret, start_index, partitions, partitions_len,
647 ret_count)) {
648 return false;
649 }
650
651 last_index = ffa_partition_info_regs_get_last_idx(ret);
652 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
653 start_index = curr_index + 1;
654 }
655 return true;
656}
657
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700658bool api_ffa_fill_partition_info_from_regs(
659 struct ffa_value ret, uint16_t start_index,
660 struct ffa_partition_info *partitions, uint16_t partitions_len,
661 ffa_vm_count_t *ret_count)
662{
663 uint16_t vm_count = *ret_count;
664 uint16_t curr_index = 0;
665 uint8_t num_entries = 0;
666 uint8_t idx = 0;
667 /* List of pointers to args in return value. */
668 uint64_t *arg_ptrs[] = {
669 &ret.arg3,
670 &ret.arg4,
671 &ret.arg5,
672 &ret.arg6,
673 &ret.arg7,
674 &ret.extended_val.arg8,
675 &ret.extended_val.arg9,
676 &ret.extended_val.arg10,
677 &ret.extended_val.arg11,
678 &ret.extended_val.arg12,
679 &ret.extended_val.arg13,
680 &ret.extended_val.arg14,
681 &ret.extended_val.arg15,
682 &ret.extended_val.arg16,
683 &ret.extended_val.arg17,
684 };
685
686 if (vm_count > partitions_len) {
687 return false;
688 }
689
690 /*
691 * Tags are currently unused in the implementation. Expect it to be
692 * zero since the implementation does not provide a tag when calling
693 * the FFA_PARTITION_INFO_GET_REGS ABI.
694 */
695 assert(ffa_partition_info_regs_get_tag(ret) == 0);
696
697 /*
698 * Hafnium expects the size of the returned descriptor to be equal to
699 * the size of the structure in the FF-A 1.1 specification. When future
700 * enhancements are made, this assert can be relaxed.
701 */
702 assert(ffa_partition_info_regs_get_desc_size(ret) ==
703 sizeof(struct ffa_partition_info));
704
705 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
706
707 /* FF-A 1.2 ALP0, section 14.9.2 Usage rule 7. */
708 assert(start_index <= curr_index);
709
710 num_entries = curr_index - start_index + 1;
711 if (num_entries > (partitions_len - vm_count) ||
712 num_entries > MAX_INFO_REGS_ENTRIES_PER_CALL) {
713 return false;
714 }
715
716 while (num_entries) {
717 uint64_t info = *(arg_ptrs[(ptrdiff_t)(idx++)]);
718 uint64_t uuid_lo = *(arg_ptrs[(ptrdiff_t)(idx++)]);
719 uint64_t uuid_high = *(arg_ptrs[(ptrdiff_t)(idx++)]);
720
721 partitions[vm_count].vm_id = info & 0xFFFF;
722 partitions[vm_count].vcpu_count = (info >> 16) & 0xFFFF;
723 partitions[vm_count].properties = (info >> 32);
724 partitions[vm_count].uuid.uuid[0] = uuid_lo & 0xFFFFFFFF;
725 partitions[vm_count].uuid.uuid[1] =
726 (uuid_lo >> 32) & 0xFFFFFFFF;
727 partitions[vm_count].uuid.uuid[2] = uuid_high & 0xFFFFFFFF;
728 partitions[vm_count].uuid.uuid[3] =
729 (uuid_high >> 32) & 0xFFFFFFFF;
730 vm_count++;
731 num_entries--;
732 }
733
734 *ret_count = vm_count;
735 return true;
736}
737
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800738struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current,
739 const struct ffa_uuid *uuid,
740 const uint16_t start_index,
741 const uint16_t tag)
742{
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800743 struct vm *current_vm = current->vm;
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800744 static struct ffa_partition_info partitions[2 * MAX_VMS];
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800745 bool uuid_is_null = ffa_uuid_is_null(uuid);
746 ffa_vm_count_t vm_count = 0;
747 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
748 uint16_t max_idx = 0;
749 uint16_t curr_idx = 0;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800750 uint8_t num_entries_to_ret = 0;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800751 uint8_t arg_idx = 3;
752
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800753 /* list of pointers to args in return value */
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800754 uint64_t *arg_ptrs[] = {
755 &(ret).func,
756 &(ret).arg1,
757 &(ret).arg2,
758 &(ret).arg3,
759 &(ret).arg4,
760 &(ret).arg5,
761 &(ret).arg6,
762 &(ret).arg7,
763 &(ret).extended_val.arg8,
764 &(ret).extended_val.arg9,
765 &(ret).extended_val.arg10,
766 &(ret).extended_val.arg11,
767 &(ret).extended_val.arg12,
768 &(ret).extended_val.arg13,
769 &(ret).extended_val.arg14,
770 &(ret).extended_val.arg15,
771 &(ret).extended_val.arg16,
772 &(ret).extended_val.arg17,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800773 };
774
775 /* TODO: Add support for using tags */
776 if (tag != 0) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800777 dlog_error("Tag not 0. Unsupported tag. %d\n", tag);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800778 return ffa_error(FFA_RETRY);
779 }
780
781 memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions));
782
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100783 vm_count = api_ffa_fill_partitions_info_array(
Daniel Boulby48546b12025-01-08 18:22:13 +0000784 partitions, ARRAY_SIZE(partitions), uuid, false, current_vm->id,
785 current_vm->ffa_version);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800786
787 /* If UUID is Null vm_count must not be zero at this stage. */
788 CHECK(!uuid_is_null || vm_count != 0);
789
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800790 /*
791 * When running the Hypervisor:
792 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
793 * it to fill with secure partitions information.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700794 * When running the SPMC, the SPMC forwards the call to the SPMD to
795 * discover any EL3 SPMD logical partitions, if the call came from an
796 * SP. Otherwise, the call is not forwarded.
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800797 * TODO: Note that for this ABI, forwarding on every invocation when
798 * uuid is Null is inefficient,and if performance becomes a problem,
799 * this would be a good place to optimize using strategies such as
800 * caching info etc. For now, assuming this inefficiency is not a major
801 * issue.
802 * - If UUID is non-Null vm_count may be zero because the UUID matches
803 * a secure partition and the query is forwarded to the SPMC.
804 * When running the SPMC:
805 * - If UUID is non-Null and vm_count is zero it means there is no such
806 * partition identified in the system.
807 */
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700808 if (vm_id_is_current_world(current_vm->id)) {
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700809 if (!api_ffa_partition_info_get_regs_forward(
810 uuid, tag, partitions, ARRAY_SIZE(partitions),
811 &vm_count)) {
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700812 dlog_error(
813 "Failed to forward "
814 "ffa_partition_info_get_regs.\n");
815 return ffa_error(FFA_DENIED);
816 }
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800817 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800818
819 /*
820 * Unrecognized UUID: does not match any of the VMs (or SPs)
821 * and is not Null.
822 */
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800823 if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) {
Olivier Deprez89da0a42023-09-01 17:38:33 +0200824 dlog_verbose(
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800825 "Invalid parameters. vm_count = %d (must not be zero "
Karl Meakine8937d92024-03-19 16:04:25 +0000826 "or > %lu)\n",
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800827 vm_count, ARRAY_SIZE(partitions));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800828 return ffa_error(FFA_INVALID_PARAMETERS);
829 }
830
831 if (start_index >= vm_count) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800832 dlog_error(
833 "start index = %d vm_count = %d (start_index must be "
834 "less than vm_count)\n",
835 start_index, vm_count);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800836 return ffa_error(FFA_INVALID_PARAMETERS);
837 }
838
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800839 max_idx = vm_count - 1;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800840 num_entries_to_ret = (max_idx - start_index) + 1;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800841 num_entries_to_ret =
842 MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800843 curr_idx = start_index + num_entries_to_ret - 1;
844 assert(curr_idx <= max_idx);
845
846 ret.func = FFA_SUCCESS_64;
847 ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48;
J-Alves296c9012025-03-20 19:59:36 +0000848 ret.arg2 |= ((uint64_t)curr_idx) << 16;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800849 ret.arg2 |= max_idx;
850
851 if (num_entries_to_ret > 1) {
852 ret.extended_val.valid = 1;
853 }
854
855 for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800856 uint64_t *xn_0 = arg_ptrs[arg_idx++];
857 uint64_t *xn_1 = arg_ptrs[arg_idx++];
858 uint64_t *xn_2 = arg_ptrs[arg_idx++];
859
860 api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id,
861 partitions[idx].vcpu_count,
862 partitions[idx].properties);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800863 if (uuid_is_null) {
Karl Meakin9478e322024-09-23 17:47:09 +0100864 ffa_uuid_to_u64x2(xn_1, xn_2, &partitions[idx].uuid);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800865 }
Raghu Krishnamurthycded31b2023-04-23 15:07:35 -0700866 assert(arg_idx <= ARRAY_SIZE(arg_ptrs));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800867 }
868
869 return ret;
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800870}
871
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100872struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000873 const struct ffa_uuid *uuid,
874 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100875{
876 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100877 ffa_vm_count_t vm_count = 0;
Olivier Deprez013f4d62022-11-21 15:46:20 +0100878 bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) ==
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000879 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100880 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100881 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000882 struct vm_locked vm_locked;
883 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100884
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000885 /* Bits 31:1 Must Be Zero */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100886 if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) {
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000887 return ffa_error(FFA_INVALID_PARAMETERS);
888 }
889
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100890 vm_count = api_ffa_fill_partitions_info_array(
891 partitions, ARRAY_SIZE(partitions), uuid, count_flag,
Daniel Boulby48546b12025-01-08 18:22:13 +0000892 current_vm->id, current_vm->ffa_version);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100893
Olivier Depreze562e542020-06-11 17:31:54 +0200894 /* If UUID is Null vm_count must not be zero at this stage. */
895 CHECK(!uuid_is_null || vm_count != 0);
896
897 /*
898 * When running the Hypervisor:
899 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
900 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000901 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200902 * a secure partition and the query is forwarded to the SPMC.
903 * When running the SPMC:
904 * - If UUID is non-Null and vm_count is zero it means there is no such
905 * partition identified in the system.
906 */
Karl Meakin117c8082024-12-04 16:03:28 +0000907 vm_count = ffa_setup_partition_info_get_forward(uuid, flags, partitions,
908 vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200909
910 /*
911 * Unrecognized UUID: does not match any of the VMs (or SPs)
912 * and is not Null.
913 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100914 if (vm_count == 0) {
915 return ffa_error(FFA_INVALID_PARAMETERS);
916 }
917
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000918 /*
919 * If the count flag is set we don't need to return the partition info
920 * descriptors.
921 */
922 if (count_flag) {
923 return (struct ffa_value){.func = FFA_SUCCESS_32,
924 .arg2 = vm_count};
925 }
926
Daniel Boulby191b5f02022-02-17 17:26:14 +0000927 vm_locked = vm_lock(current_vm);
928 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
929 vm_count);
930 vm_unlock(&vm_locked);
931 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100932}
933
Andrew Scull9726c252019-01-23 13:44:19 +0000934/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000935 * Returns the ID of the VM.
936 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100937struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000938{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100939 return (struct ffa_value){.func = FFA_SUCCESS_32,
940 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000941}
942
943/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200944 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
945 * FF-A instances.
946 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000947 */
948struct ffa_value api_ffa_spm_id_get(void)
949{
Karl Meakin0e617d92024-04-05 12:55:22 +0100950 if (FFA_VERSION_1_1 <= FFA_VERSION_COMPILED) {
951 /*
952 * Return the SPMC ID that was fetched during FF-A
953 * initialization.
954 */
955 return (struct ffa_value){.func = FFA_SUCCESS_32,
956 .arg2 = arch_ffa_spmc_id_get()};
957 }
958
J-Alves3829fc02021-03-18 12:49:18 +0000959 return ffa_error(FFA_NOT_SUPPORTED);
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000960}
961
962/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000963 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000964 * function to indicate that register state for the given vCPU has been saved
965 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000966 */
967void api_regs_state_saved(struct vcpu *vcpu)
968{
969 sl_lock(&vcpu->lock);
970 vcpu->regs_available = true;
971 sl_unlock(&vcpu->lock);
972}
J-Alves0ffce752024-10-09 14:37:10 +0100973
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000974/**
J-Alves2ced1672022-12-12 14:35:38 +0000975 * Constructs the return value from a successful FFA_MSG_WAIT call, when used
976 * with FFA_MSG_SEND_32.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100977 */
J-Alves27b71962022-12-12 15:29:58 +0000978struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100979{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000980 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100981 case FFA_MSG_SEND_32:
982 return (struct ffa_value){
983 .func = FFA_MSG_SEND_32,
J-Alves296c9012025-03-20 19:59:36 +0000984 .arg1 = ((uint64_t)(receiver->mailbox.recv_sender)
985 << 16) |
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000986 receiver->id,
987 .arg3 = receiver->mailbox.recv_size};
J-Alvesd9e7c8f2024-09-11 13:37:25 +0100988 default:
Federico Recanati25053ee2022-03-14 15:01:53 +0100989 return (struct ffa_value){
990 .func = FFA_RUN_32,
991 /*
992 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
993 * Retrieving vCPU requires a rework of the function,
994 * while receiver ID must be set because it's checked by
995 * other APIs (eg: FFA_NOTIFICATION_GET).
996 */
997 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000998 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100999}
1000
J-Alvese8c8c2b2022-12-16 15:34:48 +00001001/**
1002 * Change the state of mailbox to empty, such that the ownership is given to the
1003 * Partition manager.
Karl Meakin1064a9c2024-06-04 17:20:43 +01001004 * Returns FFA_SUCCESS if the mailbox was reset successfully, FFA_ERROR
1005 * otherwise.
J-Alvese8c8c2b2022-12-16 15:34:48 +00001006 */
Karl Meakin1064a9c2024-06-04 17:20:43 +01001007static struct ffa_value api_release_mailbox(struct vm_locked vm_locked)
J-Alvese8c8c2b2022-12-16 15:34:48 +00001008{
Karl Meakin1064a9c2024-06-04 17:20:43 +01001009 struct ffa_value ret = {.func = FFA_SUCCESS_32};
J-Alves19e20cf2023-08-02 12:48:55 +01001010 ffa_id_t vm_id = vm_locked.vm->id;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001011
1012 switch (vm_locked.vm->mailbox.state) {
1013 case MAILBOX_STATE_EMPTY:
1014 dlog_verbose("Mailbox of %x is empty.\n", vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001015 ret = ffa_error(FFA_DENIED);
J-Alves31f8bef2023-07-14 12:46:28 +01001016 break;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001017 case MAILBOX_STATE_FULL:
1018 /* Check it doesn't have pending RX full notifications. */
1019 if (vm_are_fwk_notifications_pending(vm_locked)) {
1020 dlog_verbose(
1021 "Mailbox of endpoint %x has pending "
1022 "messages.\n",
1023 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001024 ret = ffa_error(FFA_DENIED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001025 }
1026 break;
1027 case MAILBOX_STATE_OTHER_WORLD_OWNED:
1028 /*
1029 * The SPMC shouldn't let SP's mailbox get into this state.
1030 * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst
1031 * the mailbox is in this state. In that case, we should report
1032 * error.
1033 */
1034 if (vm_id_is_current_world(vm_id)) {
1035 dlog_verbose(
Karl Meakin1064a9c2024-06-04 17:20:43 +01001036 "Mailbox of endpoint %x is in an incorrect "
1037 "state.\n",
J-Alvese8c8c2b2022-12-16 15:34:48 +00001038 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001039 ret = ffa_error(FFA_ABORTED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001040 }
1041 break;
1042 }
1043
Karl Meakin1064a9c2024-06-04 17:20:43 +01001044 if (ret.func == FFA_SUCCESS_32) {
1045 vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001046 }
1047
Karl Meakin1064a9c2024-06-04 17:20:43 +01001048 return ret;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001049}
1050
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001051/*
1052 * Helper to check if extended arguments (corresponding to regs x8-x17)
1053 * are zeroed out.
1054 */
Kathleen Capella036cc592023-11-30 18:26:15 -05001055bool api_extended_args_are_zero(struct ffa_value *args)
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001056{
1057 if (args->extended_val.arg8 != 0U || args->extended_val.arg9 != 0U ||
1058 args->extended_val.arg10 != 0U || args->extended_val.arg11 != 0U ||
1059 args->extended_val.arg12 != 0U || args->extended_val.arg13 != 0U ||
1060 args->extended_val.arg14 != 0U || args->extended_val.arg15 != 0U ||
1061 args->extended_val.arg16 != 0U || args->extended_val.arg17 != 0U) {
1062 return false;
1063 }
1064 return true;
1065}
1066
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001067static void api_ffa_msg_wait_rx_release(struct vcpu *current)
1068{
1069 struct vm_locked vm_locked;
1070
Karl Meakin117c8082024-12-04 16:03:28 +00001071 vm_locked = ffa_vm_find_locked(current->vm->id);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001072 if (vm_locked.vm == NULL) {
1073 return;
1074 }
1075
1076 api_release_mailbox(vm_locked);
1077
1078 if (vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY) {
1079 dlog_warning("Mailbox not released to producer\n");
1080 }
1081
1082 vm_unlock(&vm_locked);
1083}
1084
Kathleen Capella7253bd52024-08-14 17:36:09 -04001085static bool api_retain_rx_buffer_ownership(struct ffa_value args)
1086{
1087 return ((args.arg2 & FFA_MSG_WAIT_FLAG_RETAIN_RX) != 0U);
1088}
1089
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001090struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
1091 struct ffa_value *args)
1092{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001093 struct vcpu_locked current_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001094 enum vcpu_state next_state = VCPU_STATE_RUNNING;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001095 struct ffa_value ret;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001096 struct vcpu_locked next_locked = (struct vcpu_locked){
1097 .vcpu = NULL,
1098 };
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001099
Kathleen Capella7253bd52024-08-14 17:36:09 -04001100 if (args->arg1 != 0U || args->arg3 != 0U || args->arg4 != 0U ||
1101 args->arg5 != 0U || args->arg6 != 0U || args->arg7 != 0U) {
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001102 return ffa_error(FFA_INVALID_PARAMETERS);
1103 }
1104
Kathleen Capella7253bd52024-08-14 17:36:09 -04001105 if (current->vm->ffa_version >= FFA_VERSION_1_2) {
1106 if (!api_extended_args_are_zero(args)) {
1107 return ffa_error(FFA_INVALID_PARAMETERS);
1108 }
1109 } else {
1110 if (args->arg2 != 0U) {
1111 return ffa_error(FFA_INVALID_PARAMETERS);
1112 }
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001113 }
1114
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001115 current_locked = vcpu_lock(current);
Karl Meakin117c8082024-12-04 16:03:28 +00001116 if (!ffa_cpu_cycles_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001117 current_locked, current->vm->id, HF_INVALID_VM_ID,
1118 next_locked, FFA_MSG_WAIT_32, &next_state)) {
1119 ret = ffa_error(FFA_DENIED);
1120 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001121 }
1122
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001123 assert(!vm_id_is_current_world(current->vm->id) ||
1124 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001125
Karl Meakin117c8082024-12-04 16:03:28 +00001126 ret = ffa_cpu_cycles_msg_wait_prepare(current_locked, next);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001127
1128 /*
1129 * To maintain partial ordering of locks, release vCPU lock before
1130 * releasing the VM's RX buffer, a process which requires locking the
1131 * VM.
1132 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001133out:
1134 vcpu_unlock(&current_locked);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001135
Kathleen Capella7253bd52024-08-14 17:36:09 -04001136 if (ret.func != FFA_ERROR_32 &&
1137 !api_retain_rx_buffer_ownership(*args)) {
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001138 api_ffa_msg_wait_rx_release(current);
1139 }
J-Alvese8c8c2b2022-12-16 15:34:48 +00001140 return ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001141}
1142
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001143/**
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05001144 * Inject virtual timer interrupt to next vCPU if its timer has expired.
1145 */
Daniel Boulbyd49d0772025-01-15 11:19:36 +00001146static void api_inject_arch_timer_interrupt(struct vcpu_locked next_locked)
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05001147{
1148 struct vcpu *next = next_locked.vcpu;
1149
1150 if (arch_timer_expired(&next->regs)) {
1151 /* Make virtual timer interrupt pending. */
Daniel Boulby3c1506b2025-02-25 10:49:51 +00001152 vcpu_virt_interrupt_inject(next_locked, HF_VIRTUAL_TIMER_INTID);
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05001153 }
1154}
1155
1156/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001157 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001158 * value needs to be forced onto the vCPU.
1159 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001160static bool api_vcpu_prepare_run(struct vcpu_locked current_locked,
1161 struct vcpu_locked vcpu_next_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001162 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001163{
Max Shvetsov40108e72020-08-27 12:39:50 +01001164 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001165 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001166 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001167 bool vcpu_was_init_state = false;
J-Alves6e2abc62021-12-02 14:58:56 +00001168 bool need_vm_lock;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001169 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001170
J-Alves478faac2024-10-23 10:35:57 +01001171 const struct ffa_value ffa_run_abi =
1172 (struct ffa_value){.func = FFA_RUN_32};
1173 const struct ffa_value *ffa_run_ret = NULL;
1174
Andrew Scullb06d1752019-02-04 10:15:48 +00001175 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001176 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +00001177 *
Andrew Scull4caadaf2019-07-03 13:13:47 +01001178 * The VM lock is not needed in the common case so it must only be taken
1179 * when it is going to be needed. This ensures there are no inter-vCPU
1180 * dependencies in the common run case meaning the sensitive context
1181 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +00001182 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001183 struct vcpu *vcpu = vcpu_next_locked.vcpu;
1184 struct vcpu *current = current_locked.vcpu;
Max Shvetsov40108e72020-08-27 12:39:50 +01001185
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001186 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +00001187 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
1188 (!vcpu->vm->el0_partition &&
1189 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
1190 vcpu->state == VCPU_STATE_BLOCKED ||
1191 vcpu->state == VCPU_STATE_PREEMPTED));
1192
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001193 if (need_vm_lock) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001194 vcpu_unlock(&vcpu_next_locked);
1195 vcpu_unlock(&current_locked);
Max Shvetsov40108e72020-08-27 12:39:50 +01001196 vm_locked = vm_lock(vcpu->vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001197
1198 /* Lock both vCPUs at once to avoid deadlock. */
1199 vcpus_locked = vcpu_lock_both(current, vcpu);
1200 current_locked = vcpus_locked.vcpu1;
1201 vcpu_next_locked = vcpus_locked.vcpu2;
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001202 }
1203
1204 /*
1205 * If the vCPU is already running somewhere then we can't run it here
1206 * simultaneously. While it is actually running then the state should be
1207 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
1208 * stops running but while Hafnium is in the process of switching back
1209 * to the primary there will be a brief period while the state has been
1210 * updated but `regs_available` is still false (until
1211 * `api_regs_state_saved` is called). We can't start running it again
1212 * until this has finished, so count this state as still running for the
1213 * purposes of this check.
1214 */
1215 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
1216 /*
1217 * vCPU is running on another pCPU.
1218 *
1219 * It's okay not to return the sleep duration here because the
1220 * other physical CPU that is currently running this vCPU will
1221 * return the sleep duration if needed.
1222 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001223 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001224 ret = false;
1225 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001226 }
Andrew Scull9726c252019-01-23 13:44:19 +00001227
1228 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001229 if (vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04001230 dlog_verbose("VM %#x was aborted, cannot run vCPU %u\n",
1231 vcpu->vm->id, vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +01001232 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +00001233 }
Kathleen Capella6ab05132023-05-10 12:27:35 -04001234 *run_ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +00001235 ret = false;
1236 goto out;
1237 }
1238
Andrew Walbran508e63c2018-12-20 17:02:37 +00001239 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001240 case VCPU_STATE_RUNNING:
1241 case VCPU_STATE_OFF:
1242 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001243 ret = false;
1244 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001245
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001246 case VCPU_STATE_WAITING:
1247 /*
Olivier Deprezb2808332023-02-02 15:25:40 +01001248 * An initial FFA_RUN is necessary for SP's secondary vCPUs to
1249 * reach the message wait loop.
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001250 */
Olivier Deprezb2808332023-02-02 15:25:40 +01001251 if (vcpu->rt_model == RTM_SP_INIT) {
1252 /*
1253 * TODO: this should be removed, but omitting it makes
1254 * normal world arch gicv3 tests failing.
1255 */
1256 vcpu->rt_model = RTM_NONE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001257
1258 vcpu_was_init_state = true;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001259 break;
1260 }
1261
J-Alves6e2abc62021-12-02 14:58:56 +00001262 assert(need_vm_lock == true);
J-Alves478faac2024-10-23 10:35:57 +01001263 if (!vm_locked.vm->el0_partition) {
Karl Meakin117c8082024-12-04 16:03:28 +00001264 ffa_interrupts_inject_notification_pending_interrupt(
Daniel Boulbyd49d0772025-01-15 11:19:36 +00001265 vcpu_next_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001266 }
1267
J-Alves478faac2024-10-23 10:35:57 +01001268 /* Provide reference to the return value. */
1269 ffa_run_ret = &ffa_run_abi;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001270
J-Alves478faac2024-10-23 10:35:57 +01001271 break;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001272 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +00001273 if (need_vm_lock &&
Karl Meakin117c8082024-12-04 16:03:28 +00001274 ffa_interrupts_inject_notification_pending_interrupt(
Daniel Boulbyd49d0772025-01-15 11:19:36 +00001275 vcpu_next_locked, vm_locked)) {
Daniel Boulbyd21e9b32025-02-13 15:53:21 +00001276 assert(vcpu_virt_interrupt_count_get(vcpu_next_locked) >
1277 0);
J-Alves6e2abc62021-12-02 14:58:56 +00001278 break;
1279 }
1280
Andrew Scullb06d1752019-02-04 10:15:48 +00001281 /* Allow virtual interrupts to be delivered. */
Daniel Boulbyd21e9b32025-02-13 15:53:21 +00001282 if (vcpu_virt_interrupt_count_get(vcpu_next_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +00001283 break;
1284 }
1285
Andrew Walbran508e63c2018-12-20 17:02:37 +00001286 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001287 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001288 arch_timer_remaining_ns(&vcpu->regs);
1289
1290 /*
1291 * The timer expired so allow the interrupt to be
1292 * delivered.
1293 */
1294 if (timer_remaining_ns == 0) {
1295 break;
1296 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001297 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001298
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001299 /*
1300 * The vCPU is not ready to run, return the appropriate code to
1301 * the primary which called vcpu_run.
1302 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001303 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001304 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1305 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +00001306
1307 ret = false;
1308 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001309
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001310 case VCPU_STATE_BLOCKED:
1311 /* A blocked vCPU is run unconditionally. Fall through. */
1312 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +00001313 /* Check NPI is to be injected here. */
1314 if (need_vm_lock) {
Karl Meakin117c8082024-12-04 16:03:28 +00001315 ffa_interrupts_inject_notification_pending_interrupt(
Daniel Boulbyd49d0772025-01-15 11:19:36 +00001316 vcpu_next_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001317 }
Andrew Walbran508e63c2018-12-20 17:02:37 +00001318 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001319 default:
1320 /*
1321 * Execution not expected to reach here. Deny the request
1322 * gracefully.
1323 */
1324 *run_ret = ffa_error(FFA_DENIED);
1325 ret = false;
1326 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001327 }
1328
J-Alves5d65fe72025-02-18 16:02:32 +00001329 ffa_cpu_cycles_init_schedule_mode_ffa_run(current_locked,
1330 vcpu_next_locked);
Madhukar Pappireddy1480fce2022-06-21 18:09:25 -05001331
Madhukar Pappireddy106bfc32024-09-25 15:47:11 -05001332 timer_migrate_to_other_cpu(current_locked.vcpu->cpu, vcpu_next_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001333 vcpu->cpu = current_locked.vcpu->cpu;
J-Alves478faac2024-10-23 10:35:57 +01001334
1335 vcpu_set_running(vcpu_next_locked, ffa_run_ret);
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001336
Olivier Deprez04168ed2022-09-26 09:20:00 +02001337 if (vcpu_was_init_state) {
1338 vcpu_set_phys_core_idx(vcpu);
1339 vcpu_set_boot_info_gp_reg(vcpu);
J-Alves7ac49052022-02-08 17:20:53 +00001340 }
J-Alves7ac49052022-02-08 17:20:53 +00001341
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001342 ret = true;
1343
1344out:
Andrew Scullb06d1752019-02-04 10:15:48 +00001345 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +01001346 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001347 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001348 return ret;
1349}
1350
J-Alves19e20cf2023-08-02 12:48:55 +01001351struct ffa_value api_ffa_run(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001352 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001353{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001354 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001355 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001356 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001357 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001358 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001359 struct vcpu_locked vcpu_next_locked;
1360 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001361
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001362 current_locked = vcpu_lock(current);
Karl Meakin117c8082024-12-04 16:03:28 +00001363 if (!ffa_cpu_cycles_run_checks(current_locked, vm_id, vcpu_idx, &ret,
1364 next)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001365 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001366 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001367
Karl Meakin117c8082024-12-04 16:03:28 +00001368 if (ffa_cpu_cycles_run_forward(vm_id, vcpu_idx, &ret)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001369 goto out;
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001370 }
1371
Andrew Scull19503262018-09-20 14:48:39 +01001372 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001373 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +01001374 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001375 goto out;
Andrew Scull19503262018-09-20 14:48:39 +01001376 }
1377
Fuad Tabbaed294af2019-12-20 10:43:01 +00001378 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001379 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001380 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001381 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001382
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001383 /*
1384 * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC
1385 * bypasses the intermediate execution contexts and resumes the
1386 * SP execution context that was originally preempted.
1387 */
1388 if (*next != NULL) {
1389 vcpu = *next;
1390 } else {
1391 vcpu = vm_get_vcpu(vm, vcpu_idx);
1392 }
1393
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001394 /*
1395 * Unlock current vCPU to allow it to be locked together with next
1396 * vcpu.
1397 */
1398 vcpu_unlock(&current_locked);
1399
1400 /* Lock both vCPUs at once to avoid deadlock. */
1401 vcpus_locked = vcpu_lock_both(current, vcpu);
1402 current_locked = vcpus_locked.vcpu1;
1403 vcpu_next_locked = vcpus_locked.vcpu2;
1404
Karl Meakin117c8082024-12-04 16:03:28 +00001405 if (!ffa_cpu_cycles_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001406 current_locked, current->vm->id, HF_INVALID_VM_ID,
1407 vcpu_next_locked, FFA_RUN_32, &next_state)) {
1408 ret = ffa_error(FFA_DENIED);
1409 goto out_vcpu;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001410 }
1411
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001412 if (!api_vcpu_prepare_run(current_locked, vcpu_next_locked, &ret)) {
1413 goto out_vcpu;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001414 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001415
Fuad Tabbaed294af2019-12-20 10:43:01 +00001416 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001417 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001418
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001419 assert(!vm_id_is_current_world(current->vm->id) ||
1420 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001421 current->state = VCPU_STATE_BLOCKED;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001422
Andrew Scull33fecd32019-01-08 14:48:27 +00001423 /*
1424 * Set a placeholder return code to the scheduler. This will be
1425 * overwritten when the switch back to the primary occurs.
1426 */
J-Alvesbd32c972024-02-02 16:20:04 +00001427 ret = api_ffa_interrupt_return(0);
Andrew Scull33fecd32019-01-08 14:48:27 +00001428
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001429out_vcpu:
1430 vcpu_unlock(&vcpu_next_locked);
1431
Andrew Scull6d2db332018-10-10 15:28:17 +01001432out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001433 vcpu_unlock(&current_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001434 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001435}
1436
1437/**
Andrew Scull81e85092018-12-12 12:56:20 +00001438 * Check that the mode indicates memory that is valid, owned and exclusive.
1439 */
Karl Meakin07a69ab2025-02-07 14:53:19 +00001440static bool api_mode_valid_owned_and_exclusive(mm_mode_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001441{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001442 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1443 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001444}
1445
1446/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001447 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001448 */
Karl Meakin738bee12024-01-12 15:19:45 +00001449static struct ffa_value api_vm_configure_stage1(
1450 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1451 paddr_t pa_send_begin, paddr_t pa_send_end, paddr_t pa_recv_begin,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001452 paddr_t pa_recv_end, mm_mode_t extra_mode,
Karl Meakin738bee12024-01-12 15:19:45 +00001453 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001454{
Karl Meakin738bee12024-01-12 15:19:45 +00001455 struct ffa_value ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001456
Karl Meakin738bee12024-01-12 15:19:45 +00001457 /*
1458 * Map the send page as read-only in the SPMC/hypervisor address space.
1459 */
Andrew Sculle1322792019-07-01 17:46:10 +01001460 vm_locked.vm->mailbox.send =
1461 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001462 MM_MODE_R | extra_mode, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001463 if (!vm_locked.vm->mailbox.send) {
Karl Meakin738bee12024-01-12 15:19:45 +00001464 ret = ffa_error(FFA_NO_MEMORY);
1465 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001466 }
1467
1468 /*
Karl Meakin738bee12024-01-12 15:19:45 +00001469 * Map the receive page as writable in the SPMC/hypervisor address
1470 * space. On failure, unmap the send page before returning.
Andrew Sculle1322792019-07-01 17:46:10 +01001471 */
1472 vm_locked.vm->mailbox.recv =
1473 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001474 MM_MODE_W | extra_mode, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001475 if (!vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001476 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001477 goto fail_undo_send;
1478 }
1479
Karl Meakin738bee12024-01-12 15:19:45 +00001480 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001481 goto out;
1482
1483 /*
1484 * The following mappings will not require more memory than is available
1485 * in the local pool.
1486 */
1487fail_undo_send:
1488 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001489 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1490 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001491
Andrew Sculle1322792019-07-01 17:46:10 +01001492out:
Andrew Sculle1322792019-07-01 17:46:10 +01001493 return ret;
1494}
1495
1496/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001497 * Sanity checks and configures the send and receive pages in the VM stage-2
1498 * and hypervisor stage-1 page tables.
1499 *
1500 * Returns:
1501 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001502 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001503 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1504 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001505 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001506 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001507 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001508
1509struct ffa_value api_vm_configure_pages(
1510 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1511 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1512 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001513{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001514 struct ffa_value ret;
1515 paddr_t pa_send_begin;
1516 paddr_t pa_send_end;
1517 paddr_t pa_recv_begin;
1518 paddr_t pa_recv_end;
Karl Meakin07a69ab2025-02-07 14:53:19 +00001519 mm_mode_t orig_send_mode = 0;
1520 mm_mode_t orig_recv_mode = 0;
1521 mm_mode_t extra_mode;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001522
1523 /* We only allow these to be setup once. */
1524 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001525 dlog_error("%s: Mailboxes have already been setup for VM %#x\n",
1526 __func__, vm_locked.vm->id);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001527 ret = ffa_error(FFA_DENIED);
1528 goto out;
1529 }
1530
1531 /* Hafnium only supports a fixed size of RX/TX buffers. */
1532 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
Karl Meakine8937d92024-03-19 16:04:25 +00001533 dlog_error("%s: Page count must be %zu, it is %d\n", __func__,
Karl Meakin738bee12024-01-12 15:19:45 +00001534 HF_MAILBOX_SIZE / FFA_PAGE_SIZE, page_count);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001535 ret = ffa_error(FFA_INVALID_PARAMETERS);
1536 goto out;
1537 }
1538
1539 /* Fail if addresses are not page-aligned. */
1540 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1541 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001542 dlog_error("%s: Mailbox buffers not page-aligned\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001543 ret = ffa_error(FFA_INVALID_PARAMETERS);
1544 goto out;
1545 }
1546
1547 /* Convert to physical addresses. */
1548 pa_send_begin = pa_from_ipa(send);
1549 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1550 pa_recv_begin = pa_from_ipa(recv);
1551 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1552
1553 /* Fail if the same page is used for the send and receive pages. */
1554 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001555 dlog_error("%s: Mailbox buffers overlap\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001556 ret = ffa_error(FFA_INVALID_PARAMETERS);
1557 goto out;
1558 }
Andrew Sculle1322792019-07-01 17:46:10 +01001559
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001560 /* Set stage 2 translation tables only for virtual FF-A instances. */
1561 if (vm_id_is_current_world(vm_locked.vm->id)) {
1562 /*
1563 * Ensure the pages are valid, owned and exclusive to the VM and
1564 * that the VM has the required access to the memory.
1565 */
1566 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1567 &orig_send_mode) ||
1568 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1569 (orig_send_mode & MM_MODE_R) == 0 ||
1570 (orig_send_mode & MM_MODE_W) == 0) {
1571 dlog_error(
1572 "VM doesn't have required access rights to map "
1573 "TX buffer in stage 2.\n");
1574 ret = ffa_error(FFA_INVALID_PARAMETERS);
1575 goto out;
1576 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001577
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001578 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1579 &orig_recv_mode) ||
1580 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1581 (orig_recv_mode & MM_MODE_R) == 0) {
1582 dlog_error(
1583 "VM doesn't have required access rights to map "
1584 "RX buffer in stage 2.\n");
1585 ret = ffa_error(FFA_INVALID_PARAMETERS);
1586 goto out;
1587 }
Andrew Sculle1322792019-07-01 17:46:10 +01001588
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001589 /* Take memory ownership away from the VM and mark as shared. */
Karl Meakin07a69ab2025-02-07 14:53:19 +00001590 mm_mode_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1591 MM_MODE_W;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001592 if (vm_locked.vm->el0_partition) {
1593 mode |= MM_MODE_USER | MM_MODE_NG;
1594 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001595
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001596 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1597 mode, local_page_pool, NULL)) {
1598 dlog_error(
1599 "Cannot allocate a new entry in stage 2 "
1600 "translation table.\n");
1601 ret = ffa_error(FFA_NO_MEMORY);
1602 goto out;
1603 }
Andrew Sculle1322792019-07-01 17:46:10 +01001604
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001605 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1606 if (vm_locked.vm->el0_partition) {
1607 mode |= MM_MODE_USER | MM_MODE_NG;
1608 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001609
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001610 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1611 mode, local_page_pool, NULL)) {
1612 /* TODO: partial defrag of failed range. */
1613 /* Recover any memory consumed in failed mapping. */
Karl Meakin738bee12024-01-12 15:19:45 +00001614 dlog_error("%s: cannot map recv page\n", __func__);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001615 vm_ptable_defrag(vm_locked, local_page_pool);
Karl Meakin738bee12024-01-12 15:19:45 +00001616 ret = ffa_error(FFA_NO_MEMORY);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001617 goto fail_undo_send;
1618 }
Karl Meakin738bee12024-01-12 15:19:45 +00001619 } else {
1620 ret = arch_other_world_vm_configure_rxtx_map(
1621 vm_locked, local_page_pool, pa_send_begin, pa_send_end,
1622 pa_recv_begin, pa_recv_end);
1623 if (ret.func != FFA_SUCCESS_32) {
1624 goto out;
1625 }
Andrew Sculle1322792019-07-01 17:46:10 +01001626 }
1627
Karl Meakin07a69ab2025-02-07 14:53:19 +00001628 /* Get extra send/recv pages mapping mode for the given VM ID. */
1629 extra_mode = arch_mm_extra_mode_from_vm(vm_locked.vm->id);
Olivier Deprez96a2a262020-06-11 17:21:38 +02001630
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001631 /*
1632 * For EL0 partitions, since both the partition and the hypervisor code
1633 * use the EL2&0 translation regime, it is critical to mark the mappings
1634 * of the send and recv buffers as non-global in the TLB. For one, if we
1635 * dont mark it as non-global, it would cause TLB conflicts since there
1636 * would be an identity mapping with non-global attribute in the
1637 * partitions page tables, but another identity mapping in the
1638 * hypervisor page tables with the global attribute. The other issue is
1639 * one of security, we dont want other partitions to be able to access
1640 * other partitions buffers through cached translations.
1641 */
1642 if (vm_locked.vm->el0_partition) {
Karl Meakin07a69ab2025-02-07 14:53:19 +00001643 extra_mode |= MM_MODE_NG;
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001644 }
1645
Karl Meakin07a69ab2025-02-07 14:53:19 +00001646 ret = api_vm_configure_stage1(mm_stage1_locked, vm_locked,
1647 pa_send_begin, pa_send_end, pa_recv_begin,
1648 pa_recv_end, extra_mode, local_page_pool);
Karl Meakin738bee12024-01-12 15:19:45 +00001649 if (ret.func != FFA_SUCCESS_32) {
Andrew Sculle1322792019-07-01 17:46:10 +01001650 goto fail_undo_send_and_recv;
1651 }
1652
Manish Pandeyd34f8892020-06-19 17:41:07 +01001653 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001654 goto out;
1655
Andrew Sculle1322792019-07-01 17:46:10 +01001656fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001657 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001658 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001659
1660fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001661 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001662 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001663
1664out:
Andrew Sculle1322792019-07-01 17:46:10 +01001665 return ret;
1666}
1667
Karl Meakinb9705e22024-04-05 13:58:28 +01001668/**
1669 * Read the buffer addresses and VM ID of an FFA_RXTX_MAP request. Handles
1670 * forwarded messages by reading from the hypervisor's TX buffer.
1671 *
1672 * Returns the VM/SP ID on success.
1673 *
1674 * Returns `HF_INVALID_VM_ID` when the arguments provided via the ABI
1675 * FFA_RXTX_MAP indicate the SPMC should retrieve the RXTX description from the
1676 * Hypervisor RXTX buffers, and the hypervisor hasn't given its own RXTX buffers
1677 * for the SPMC to map.
1678 */
1679static ffa_id_t api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send,
1680 ipaddr_t *recv, uint32_t *page_count)
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001681{
Karl Meakinb9705e22024-04-05 13:58:28 +01001682 bool forwarded;
1683 struct vm_locked vm_locked;
1684 ffa_id_t owner_vm_id;
1685 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc;
1686 struct ffa_composite_memory_region *rx_region;
1687 struct ffa_composite_memory_region *tx_region;
1688
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001689 /*
1690 * If the message has been forwarded the effective addresses are in
1691 * hypervisor's TX buffer.
1692 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001693 forwarded = (current_vm->id == HF_OTHER_WORLD_ID) &&
1694 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1695 (*page_count == 0);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001696
1697 if (forwarded) {
Karl Meakinb9705e22024-04-05 13:58:28 +01001698 vm_locked = vm_lock(current_vm);
1699 endpoint_desc = (struct ffa_endpoint_rx_tx_descriptor *)
1700 vm_locked.vm->mailbox.send;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001701
Karl Meakinb9705e22024-04-05 13:58:28 +01001702 if (endpoint_desc == NULL) {
1703 dlog_error(
1704 "Trying to access RXTX description, but "
1705 "hypervisor has not provided RXTX buffers\n");
1706 vm_unlock(&vm_locked);
1707 return HF_INVALID_VM_ID;
1708 }
1709
1710 rx_region = ffa_endpoint_get_rx_memory_region(endpoint_desc);
1711 tx_region = ffa_endpoint_get_tx_memory_region(endpoint_desc);
1712
1713 owner_vm_id = endpoint_desc->endpoint_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001714 *recv = ipa_init(rx_region->constituents[0].address);
1715 *send = ipa_init(tx_region->constituents[0].address);
1716 *page_count = rx_region->constituents[0].page_count;
J-Alves28ad9b42022-12-08 12:19:43 +00001717
1718 vm_unlock(&vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001719 } else {
Karl Meakinb9705e22024-04-05 13:58:28 +01001720 owner_vm_id = current_vm->id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001721 }
Karl Meakinb9705e22024-04-05 13:58:28 +01001722
1723 return owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001724}
Karl Meakinb9705e22024-04-05 13:58:28 +01001725
Andrew Sculle1322792019-07-01 17:46:10 +01001726/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001727 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001728 * must not be shared. Locking of the page tables combined with a local memory
1729 * pool ensures there will always be enough memory to recover from any errors
1730 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1731 * by another core which could result in this transaction being unable to roll
1732 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001733 *
1734 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001735 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001736 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001737 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001738 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001739 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001740 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001741 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001742struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001743 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001744{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001745 struct ffa_value ret;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001746 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001747 struct mm_stage1_locked mm_stage1_locked;
1748 struct mpool local_page_pool;
J-Alves19e20cf2023-08-02 12:48:55 +01001749 ffa_id_t owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001750
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001751 /*
1752 * Get the original buffer addresses and VM ID in case of forwarded
1753 * message.
1754 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001755 owner_vm_id = api_get_rxtx_description(current->vm, &send, &recv,
1756 &page_count);
1757 if (owner_vm_id == HF_INVALID_VM_ID) {
1758 return ffa_error(FFA_INVALID_PARAMETERS);
1759 }
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001760
Karl Meakin117c8082024-12-04 16:03:28 +00001761 owner_vm_locked = ffa_vm_find_locked_create(owner_vm_id);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001762 if (owner_vm_locked.vm == NULL) {
1763 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1764 owner_vm_id);
1765 return ffa_error(FFA_DENIED);
1766 }
Andrew Scull220e6212018-12-21 18:09:00 +00001767
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001768 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001769 * Create a local pool so any freed memory can't be used by another
1770 * thread. This is to ensure the original mapping can be restored if any
1771 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001772 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001773 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1774
Manish Pandeyd34f8892020-06-19 17:41:07 +01001775 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001776
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001777 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1778 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001779 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001780 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001781 }
1782
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001783 /* Forward buffer mapping to SPMC if coming from a VM. */
Karl Meakin117c8082024-12-04 16:03:28 +00001784 ffa_setup_rxtx_map_forward(owner_vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001785
Federico Recanati9f1b6532022-04-14 13:15:28 +02001786 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001787
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001788exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001789 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001790 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001791 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001792
1793 return ret;
1794}
1795
1796/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001797 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1798 * translation regime of the caller. Unmap the region for the hypervisor and
1799 * set the memory region to owned and exclusive for the component. Since the
1800 * memory region mapped in the page table, when the buffers were originally
1801 * created we can safely remap it.
1802 *
1803 * Returns:
1804 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1805 * behalf of the caller.
1806 * - FFA_SUCCESS on success if no further action is needed.
1807 */
J-Alves19e20cf2023-08-02 12:48:55 +01001808struct ffa_value api_ffa_rxtx_unmap(ffa_id_t allocator_id, struct vcpu *current)
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001809{
1810 struct vm *vm = current->vm;
1811 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001812 ffa_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001813 struct mm_stage1_locked mm_stage1_locked;
1814 paddr_t send_pa_begin;
1815 paddr_t send_pa_end;
1816 paddr_t recv_pa_begin;
1817 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001818 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001819
J-Alves661e1b72023-08-02 13:39:40 +01001820 if (vm->id == HF_HYPERVISOR_VM_ID && !ffa_is_vm_id(allocator_id)) {
Olivier Deprez106c8ce2024-02-08 11:49:02 +01001821 dlog_verbose(
J-Alves9bd324a2023-01-12 10:52:52 +00001822 "The Hypervisor must specify a valid VM ID in register "
1823 "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n");
1824 return ffa_error(FFA_INVALID_PARAMETERS);
1825 }
1826
Federico Recanati8da9e332022-02-10 11:00:17 +01001827 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1828 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
J-Alves9bd324a2023-01-12 10:52:52 +00001829 dlog_error(
1830 "The register W1 (containing ID) must be 0 at virtual "
1831 "instances.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001832 return ffa_error(FFA_INVALID_PARAMETERS);
1833 }
1834
1835 /* VM ID of which buffers have to be unmapped. */
1836 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1837
Karl Meakin117c8082024-12-04 16:03:28 +00001838 vm_locked = ffa_vm_find_locked(owner_vm_id);
Federico Recanati8da9e332022-02-10 11:00:17 +01001839 vm = vm_locked.vm;
1840 if (vm == NULL) {
1841 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1842 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001843 return ffa_error(FFA_INVALID_PARAMETERS);
1844 }
1845
1846 /* Get send and receive buffers. */
1847 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001848 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001849 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001850 ret = ffa_error(FFA_INVALID_PARAMETERS);
1851 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001852 }
1853
1854 /* Currently a mailbox size of 1 page is assumed. */
1855 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1856 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1857 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1858 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1859
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001860 mm_stage1_locked = mm_lock_stage1();
1861
Federico Recanati8da9e332022-02-10 11:00:17 +01001862 /* Reset stage 2 mapping only for virtual FF-A instances. */
1863 if (vm_id_is_current_world(owner_vm_id)) {
1864 /*
1865 * Set the memory region of the buffers back to the default mode
1866 * for the VM. Since this memory region was already mapped for
1867 * the RXTX buffers we can safely remap them.
1868 */
1869 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1870 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1871 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001872
Federico Recanati8da9e332022-02-10 11:00:17 +01001873 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1874 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1875 &api_page_pool, NULL));
Karl Meakin738bee12024-01-12 15:19:45 +00001876 } else {
1877 ret = arch_other_world_vm_configure_rxtx_unmap(
1878 vm_locked, &api_page_pool, send_pa_begin, send_pa_end,
1879 recv_pa_begin, recv_pa_end);
1880 if (ret.func != FFA_SUCCESS_32) {
1881 goto out;
1882 }
Federico Recanati8da9e332022-02-10 11:00:17 +01001883 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001884
1885 /* Unmap the buffers in the partition manager. */
1886 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1887 &api_page_pool));
1888 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1889 &api_page_pool));
1890
1891 vm->mailbox.send = NULL;
1892 vm->mailbox.recv = NULL;
Karl Meakin117c8082024-12-04 16:03:28 +00001893 ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001894
Federico Recanati8da9e332022-02-10 11:00:17 +01001895 /* Forward buffer unmapping to SPMC if coming from a VM. */
Karl Meakin117c8082024-12-04 16:03:28 +00001896 ffa_setup_rxtx_unmap_forward(vm_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001897
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001898 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001899
1900out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001901 vm_unlock(&vm_locked);
1902
Federico Recanati8da9e332022-02-10 11:00:17 +01001903 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001904}
1905
Karl Meakin355d5c22024-12-12 18:04:56 +00001906static struct ffa_value api_ffa_msg_send2_copy_data(
1907 struct ffa_partition_rxtx_header *header, struct vm *receiver_vm,
Karl Meakina78337f2024-12-12 23:30:06 +00001908 struct vm_locked sender_locked)
Karl Meakin355d5c22024-12-12 18:04:56 +00001909{
Karl Meakina78337f2024-12-12 23:30:06 +00001910 const void *sender_tx_buffer = sender_locked.vm->mailbox.send;
Karl Meakin355d5c22024-12-12 18:04:56 +00001911 uint32_t total_size;
Karl Meakina78337f2024-12-12 23:30:06 +00001912 uint32_t min_offset;
Karl Meakin355d5c22024-12-12 18:04:56 +00001913
Karl Meakina78337f2024-12-12 23:30:06 +00001914 switch (sender_locked.vm->ffa_version) {
1915 case FFA_VERSION_1_0:
1916 dlog_verbose("Indirect messaging not supported in v1.0\n");
1917 return ffa_error(FFA_NOT_SUPPORTED);
1918 case FFA_VERSION_1_1:
1919 min_offset = FFA_RXTX_HEADER_SIZE_V1_1;
1920 break;
1921 default:
1922 min_offset = FFA_RXTX_HEADER_SIZE;
1923 break;
1924 }
1925
1926 if (header->offset < min_offset) {
1927 dlog_error(
1928 "Indirect message payload overlaps with header (%u < "
1929 "%u, version = %#x)\n",
1930 header->offset, min_offset,
1931 sender_locked.vm->ffa_version);
Karl Meakin355d5c22024-12-12 18:04:56 +00001932 return ffa_error(FFA_INVALID_PARAMETERS);
1933 }
1934
Karl Meakina78337f2024-12-12 23:30:06 +00001935 /*
1936 * Check the size of transfer.
1937 * Check for overflow in the sum so that very large offsets and/or sizes
1938 * do not pass the check.
1939 */
1940 if (add_overflow(header->offset, header->size, &total_size)) {
1941 dlog_error(
1942 "Overflow calculating message size (offset = %u, size "
1943 "= %u)\n",
1944 header->offset, header->size);
1945 return ffa_error(FFA_INVALID_PARAMETERS);
1946 }
1947
1948 if (total_size > FFA_MSG_PAYLOAD_MAX) {
1949 dlog_error("Message is too big (%u > %zu)\n", total_size,
1950 FFA_MSG_PAYLOAD_MAX);
Karl Meakin355d5c22024-12-12 18:04:56 +00001951 return ffa_error(FFA_INVALID_PARAMETERS);
1952 }
1953
1954 /* Copy data. */
1955 if (!memcpy_trapped(receiver_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1956 sender_tx_buffer, total_size)) {
1957 dlog_error(
Karl Meakina78337f2024-12-12 23:30:06 +00001958 "%s: Failed to copy message to receiver's (%x) RX "
Karl Meakin355d5c22024-12-12 18:04:56 +00001959 "buffer.\n",
1960 __func__, receiver_vm->id);
1961 return ffa_error(FFA_ABORTED);
1962 }
1963
1964 receiver_vm->mailbox.recv_size = total_size;
1965 receiver_vm->mailbox.recv_sender = header->sender;
1966 receiver_vm->mailbox.recv_func = FFA_MSG_SEND2_32;
1967 receiver_vm->mailbox.state = MAILBOX_STATE_FULL;
1968
1969 return (struct ffa_value){.func = FFA_SUCCESS_32};
1970}
1971
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001972/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001973 * Copies data from the sender's send buffer to the recipient's receive buffer
1974 * and notifies the receiver.
1975 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00001976struct ffa_value api_ffa_msg_send2(ffa_id_t sender_id, uint32_t flags,
Federico Recanati25053ee2022-03-14 15:01:53 +01001977 struct vcpu *current)
1978{
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00001979 struct vm *current_vm = current->vm;
1980 struct vm *receiver_vm;
1981 struct vm_locked receiver_locked;
Federico Recanati25053ee2022-03-14 15:01:53 +01001982 struct vm_locked sender_locked;
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00001983 const void *sender_tx_buffer;
Federico Recanati25053ee2022-03-14 15:01:53 +01001984 struct ffa_value ret;
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00001985 ffa_id_t header_sender_id;
1986 ffa_id_t header_receiver_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01001987
Karl Meakina5ea9092024-05-28 15:40:33 +01001988 alignas(8) struct ffa_partition_rxtx_header header;
1989
Federico Recanati25053ee2022-03-14 15:01:53 +01001990 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00001991 if (current_vm->id != HF_HYPERVISOR_VM_ID && sender_id != 0) {
Federico Recanati25053ee2022-03-14 15:01:53 +01001992 dlog_error("Sender VM ID must be zero.\n");
1993 return ffa_error(FFA_INVALID_PARAMETERS);
1994 }
1995
Federico Recanati25053ee2022-03-14 15:01:53 +01001996 /*
1997 * Get message sender's mailbox, which can be different to the `from` vm
1998 * when the message is forwarded.
1999 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002000 sender_id = (sender_id != 0) ? sender_id : current_vm->id;
2001 sender_locked = ffa_vm_find_locked(sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002002 if (sender_locked.vm == NULL) {
2003 dlog_error("Cannot send message from VM ID %#x, not found.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002004 sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002005 return ffa_error(FFA_DENIED);
2006 }
2007
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002008 sender_tx_buffer = sender_locked.vm->mailbox.send;
2009 if (sender_tx_buffer == NULL) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002010 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002011 sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002012 ret = ffa_error(FFA_DENIED);
2013 goto out_unlock_sender;
2014 }
2015
2016 /*
2017 * Copy message header as safety measure to avoid multiple accesses to
2018 * unsafe memory which could be 'corrupted' between safety checks and
2019 * final buffer copy.
Karl Meakina78337f2024-12-12 23:30:06 +00002020 * This includes the UUID added in v1.2. Messages that do not specify a
2021 * UUID (v1.1 or earlier) will leave the UUID unspecified, so this is
2022 * backwards compatible.
Federico Recanati25053ee2022-03-14 15:01:53 +01002023 */
Karl Meakinbf580c22024-12-12 14:31:06 +00002024 if (!memcpy_trapped(&header, sizeof(header), sender_tx_buffer,
J-Alves8a5fd9d2024-04-09 11:22:49 +01002025 FFA_RXTX_HEADER_SIZE)) {
2026 dlog_error(
2027 "%s: Failed to copy message from sender's(%x) TX "
2028 "buffer.\n",
2029 __func__, sender_locked.vm->id);
2030 ret = ffa_error(FFA_ABORTED);
2031 goto out_unlock_sender;
2032 }
2033
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002034 header_sender_id = header.sender;
2035 header_receiver_id = header.receiver;
Federico Recanati25053ee2022-03-14 15:01:53 +01002036
2037 /* Ensure Sender IDs from API and from message header match. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002038 if (sender_id != header_sender_id) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002039 dlog_error(
2040 "Message sender VM ID (%#x) doesn't match header's VM "
2041 "ID (%#x).\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002042 sender_id, header_sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002043 ret = ffa_error(FFA_INVALID_PARAMETERS);
2044 goto out_unlock_sender;
2045 }
2046
2047 /* Disallow reflexive requests as this suggests an error in the VM. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002048 if (header_receiver_id == header_sender_id) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002049 dlog_error("Sender and receive VM IDs must be different.\n");
2050 ret = ffa_error(FFA_INVALID_PARAMETERS);
2051 goto out_unlock_sender;
2052 }
2053
Federico Recanati7b39ff22022-03-14 15:01:53 +01002054 /* `flags` can be set only at secure virtual FF-A instances. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002055 if (ffa_is_vm_id(header_sender_id) && flags != 0) {
Federico Recanati7b39ff22022-03-14 15:01:53 +01002056 dlog_error("flags must be zero.\n");
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002057 ret = ffa_error(FFA_INVALID_PARAMETERS);
2058 goto out_unlock_sender;
Federico Recanati7b39ff22022-03-14 15:01:53 +01002059 }
2060
Federico Recanati25053ee2022-03-14 15:01:53 +01002061 /*
2062 * Check if the message has to be forwarded to the SPMC, in
2063 * this case return, the SPMC will handle the buffer copy.
2064 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002065 if (ffa_indirect_msg_send2_forward(header_receiver_id, header_sender_id,
2066 &ret)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002067 goto out_unlock_sender;
2068 }
2069
2070 /* Ensure the receiver VM exists. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002071 receiver_locked = ffa_vm_find_locked(header_receiver_id);
2072 receiver_vm = receiver_locked.vm;
Federico Recanati25053ee2022-03-14 15:01:53 +01002073
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002074 if (receiver_vm == NULL) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002075 dlog_error("Cannot deliver message to VM %#x, not found.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002076 header_receiver_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002077 ret = ffa_error(FFA_INVALID_PARAMETERS);
2078 goto out_unlock_sender;
2079 }
2080
2081 /*
2082 * Check sender and receiver can use indirect messages.
2083 * Sender is the VM/SP who originally sent the message, not the
2084 * hypervisor possibly relaying it.
2085 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002086 if (!ffa_indirect_msg_is_supported(sender_locked, receiver_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002087 dlog_verbose("VM %#x doesn't support indirect message\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002088 header_sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002089 ret = ffa_error(FFA_DENIED);
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002090 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002091 }
2092
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002093 if (vm_is_mailbox_busy(receiver_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002094 dlog_error(
2095 "Cannot deliver message to VM %#x, RX buffer not "
2096 "ready.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002097 header_receiver_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002098 ret = ffa_error(FFA_BUSY);
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002099 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002100 }
2101
Federico Recanati644f0462022-03-17 12:04:00 +01002102 /* Acquire receiver's RX buffer. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002103 if (!ffa_setup_acquire_receiver_rx(receiver_locked, &ret)) {
2104 dlog_error("Failed to acquire RX buffer for VM %#x\n",
2105 receiver_vm->id);
2106 goto out_unlock_both;
Federico Recanati644f0462022-03-17 12:04:00 +01002107 }
2108
Karl Meakina78337f2024-12-12 23:30:06 +00002109 ret = api_ffa_msg_send2_copy_data(&header, receiver_vm, sender_locked);
Karl Meakin355d5c22024-12-12 18:04:56 +00002110 if (ret.func != FFA_SUCCESS_32) {
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002111 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002112 }
2113
J-Alves3bb82592023-01-23 11:20:54 +00002114 /*
2115 * Set framework notifications, only if the SP has enabled
2116 * receipt of notifications.
2117 * If VMs have provided the RX buffer it is implied they already
2118 * support indirect messaging, and therefore framework notifications.
2119 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002120 if (ffa_is_vm_id(receiver_locked.vm->id) ||
2121 vm_are_notifications_enabled(receiver_locked.vm)) {
J-Alves3bb82592023-01-23 11:20:54 +00002122 ffa_notifications_bitmap_t rx_buffer_full =
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002123 ffa_is_vm_id(header_sender_id)
J-Alves3bb82592023-01-23 11:20:54 +00002124 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
2125 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
Federico Recanati25053ee2022-03-14 15:01:53 +01002126
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002127 vm_notifications_framework_set_pending(receiver_locked,
J-Alves3bb82592023-01-23 11:20:54 +00002128 rx_buffer_full);
2129
2130 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2131 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2132 vcpu_index(current));
Karl Meakin117c8082024-12-04 16:03:28 +00002133 ffa_notifications_sri_trigger_not_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002134 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00002135 ffa_notifications_sri_set_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002136 }
Federico Recanati25053ee2022-03-14 15:01:53 +01002137 }
2138
2139 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2140
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002141out_unlock_both:
2142 vm_unlock(&receiver_locked);
Federico Recanati25053ee2022-03-14 15:01:53 +01002143
2144out_unlock_sender:
2145 vm_unlock(&sender_locked);
2146
2147 return ret;
2148}
2149
2150/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002151 * Releases the caller's mailbox so that a new message can be received. The
2152 * caller must have copied out all data they wish to preserve as new messages
2153 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002154 *
2155 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002156 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2157 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002158 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2159 * - FFA_SUCCESS on success if no further action is needed.
2160 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002161 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002162 * hf_mailbox_waiter_get.
2163 */
J-Alves19e20cf2023-08-02 12:48:55 +01002164struct ffa_value api_ffa_rx_release(ffa_id_t receiver_id, struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002165{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002166 struct vm *current_vm = current->vm;
2167 struct vm *vm;
2168 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01002169 ffa_id_t current_vm_id = current_vm->id;
2170 ffa_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002171 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002172
Federico Recanati7bef0b92022-03-17 14:56:22 +01002173 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2174 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2175 dlog_error("Invalid `receiver_id`, must be zero.\n");
2176 return ffa_error(FFA_INVALID_PARAMETERS);
2177 }
2178
2179 /*
2180 * VM ID to be released: `receiver_id` if message has been forwarded by
2181 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2182 */
2183 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2184 release_vm_id = current_vm_id;
2185 } else {
2186 release_vm_id = receiver_id;
2187 }
2188
Karl Meakin117c8082024-12-04 16:03:28 +00002189 vm_locked = ffa_vm_find_locked(release_vm_id);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002190 vm = vm_locked.vm;
2191 if (vm == NULL) {
2192 dlog_error("No buffer registered for VM ID %#x.\n",
2193 release_vm_id);
2194 return ffa_error(FFA_INVALID_PARAMETERS);
2195 }
2196
Karl Meakin117c8082024-12-04 16:03:28 +00002197 if (ffa_setup_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002198 goto out;
2199 }
2200
Karl Meakin1064a9c2024-06-04 17:20:43 +01002201 ret = api_release_mailbox(vm_locked);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002202
2203out:
2204 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002205
2206 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002207}
Andrew Walbran318f5732018-11-20 16:23:42 +00002208
2209/**
Federico Recanati644f0462022-03-17 12:04:00 +01002210 * Acquire ownership of an RX buffer before writing to it. Both
2211 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2212 * could contend for the same buffer. SPMC owns VM's RX buffer after
2213 * it's mapped in its translation regime. This ABI should be
2214 * used by the Hypervisor to get the ownership of a VM's RX buffer
2215 * from the SPMC solving the aforementioned possible contention.
2216 *
2217 * Returns:
2218 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2219 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2220 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2221 */
J-Alves19e20cf2023-08-02 12:48:55 +01002222struct ffa_value api_ffa_rx_acquire(ffa_id_t receiver_id, struct vcpu *current)
Federico Recanati644f0462022-03-17 12:04:00 +01002223{
2224 struct vm_locked receiver_locked;
2225 struct vm *receiver;
2226 struct ffa_value ret;
2227
2228 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
J-Alves661e1b72023-08-02 13:39:40 +01002229 !ffa_is_vm_id(receiver_id)) {
Federico Recanati644f0462022-03-17 12:04:00 +01002230 dlog_error(
2231 "FFA_RX_ACQUIRE not supported at this FF-A "
2232 "instance.\n");
2233 return ffa_error(FFA_NOT_SUPPORTED);
2234 }
2235
Karl Meakin117c8082024-12-04 16:03:28 +00002236 receiver_locked = ffa_vm_find_locked(receiver_id);
Federico Recanati644f0462022-03-17 12:04:00 +01002237 receiver = receiver_locked.vm;
2238
2239 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2240 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2241 receiver_id);
2242 ret = ffa_error(FFA_INVALID_PARAMETERS);
2243 goto out;
2244 }
2245
2246 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2247 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2248 ret = ffa_error(FFA_DENIED);
2249 goto out;
2250 }
2251
J-Alvese8c8c2b2022-12-16 15:34:48 +00002252 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002253
2254 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2255
2256out:
2257 vm_unlock(&receiver_locked);
2258
2259 return ret;
2260}
2261
J-Alvescc542042024-07-24 19:13:22 +01002262/*
2263 * Returns true if intid relates with either of those:
2264 * - NPI
2265 * - ME
2266 * - Virtual Timer.
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002267 * - IPI
J-Alvescc542042024-07-24 19:13:22 +01002268 *
2269 * These are VIs with no expected interrupt descriptor.
2270 */
2271static bool api_is_maintenance_virtual_interrupt(uint32_t intid)
2272{
2273 return intid == HF_NOTIFICATION_PENDING_INTID ||
2274 intid == HF_MANAGED_EXIT_INTID ||
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002275 intid == HF_VIRTUAL_TIMER_INTID || intid == HF_IPI_INTID;
J-Alvescc542042024-07-24 19:13:22 +01002276}
2277
Federico Recanati644f0462022-03-17 12:04:00 +01002278/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002279 * Enables or disables a given interrupt ID for the calling vCPU.
2280 *
2281 * Returns 0 on success, or -1 if the intid is invalid.
2282 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002283int64_t api_interrupt_enable(uint32_t intid, bool enable,
2284 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002285{
Manish Pandey35e452f2021-02-18 21:36:34 +00002286 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002287 struct interrupts *interrupts = &current->interrupts;
J-Alvescc542042024-07-24 19:13:22 +01002288 struct interrupt_descriptor *int_desc = NULL;
2289 struct vm *vm = current->vm;
2290 struct vm_locked vm_locked;
2291
2292 int64_t ret = -1;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002293
Andrew Walbran318f5732018-11-20 16:23:42 +00002294 if (intid >= HF_NUM_INTIDS) {
2295 return -1;
2296 }
2297
J-Alvescc542042024-07-24 19:13:22 +01002298 vm_locked = vm_lock(vm);
Manish Pandey35e452f2021-02-18 21:36:34 +00002299 current_locked = vcpu_lock(current);
J-Alvescc542042024-07-24 19:13:22 +01002300
2301 int_desc = vm_interrupt_set_enable(vm_locked, intid, enable);
2302
2303 if (!api_is_maintenance_virtual_interrupt(intid)) {
2304 if (int_desc == NULL) {
2305 dlog_error("%s: invalid interrupt ID.\n", __func__);
2306 goto out;
2307 }
2308
2309 plat_interrupts_configure_interrupt(*int_desc);
2310 }
2311
Daniel Boulbyd21e9b32025-02-13 15:53:21 +00002312 /*
2313 * The type must be set first so that the correct count is modfied.
2314 */
2315 vcpu_virt_interrupt_set_type(interrupts, intid, type);
2316 vcpu_virt_interrupt_enable(current_locked, intid, enable);
Andrew Walbran318f5732018-11-20 16:23:42 +00002317
J-Alvescc542042024-07-24 19:13:22 +01002318 ret = 0;
2319
2320out:
2321 vm_unlock(&vm_locked);
Manish Pandey35e452f2021-02-18 21:36:34 +00002322 vcpu_unlock(&current_locked);
J-Alvescc542042024-07-24 19:13:22 +01002323
2324 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002325}
2326
2327/**
2328 * Returns the ID of the next pending interrupt for the calling vCPU, and
2329 * acknowledges it (i.e. marks it as no longer pending). Returns
2330 * HF_INVALID_INTID if there are no pending interrupts.
2331 */
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002332uint32_t api_interrupt_get(struct vcpu_locked current_locked)
Andrew Walbran318f5732018-11-20 16:23:42 +00002333{
Daniel Boulby5df87962025-02-06 11:15:08 +00002334 return vcpu_virt_interrupt_get_pending_and_enabled(current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002335}
2336
2337/**
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002338 * Negotiate the FF-A version to be used for this FF-A instance.
2339 * See section 13.2 of the FF-A v1.2 ALP1 spec.
2340 *
2341 * Returns Hafnium's version number (`FFA_VERSION_COMPILED`) on success.
2342 * Returns `FFA_NOT_SUPPORTED` on error:
2343 * - The version is invalid (highest bit set).
2344 * - The requested version is incompatible.
2345 * - The version has already been negotiated and cannot be changed.
2346 */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002347struct ffa_value api_ffa_version(struct vcpu *current,
Karl Meakin0e617d92024-04-05 12:55:22 +01002348 enum ffa_version requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002349{
Karl Meakin0e617d92024-04-05 12:55:22 +01002350 static_assert(sizeof(enum ffa_version) == 4,
2351 "enum ffa_version must be 4 bytes wide");
2352
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002353 const struct ffa_value error = {.func = (uint32_t)FFA_NOT_SUPPORTED};
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002354 struct vm_locked current_vm_locked;
2355
Karl Meakin5be8dfa2025-03-27 14:58:43 +00002356 uint16_t compiled_major = ffa_version_get_major(FFA_VERSION_COMPILED);
2357 uint16_t compiled_minor = ffa_version_get_minor(FFA_VERSION_COMPILED);
2358 uint16_t requested_major;
2359 uint16_t requested_minor;
2360 uint16_t vm_major;
2361 uint16_t vm_minor;
2362
Karl Meakin0e617d92024-04-05 12:55:22 +01002363 if (!ffa_version_is_valid(requested_version)) {
2364 dlog_error(
2365 "FFA_VERSION: requested version %#x is invalid "
2366 "(highest bit must be zero)\n",
2367 requested_version);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002368 return error;
Andrew Walbran9fd29072020-04-22 12:12:14 +01002369 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002370
Karl Meakin5be8dfa2025-03-27 14:58:43 +00002371 requested_major = ffa_version_get_major(requested_version);
2372 requested_minor = ffa_version_get_minor(requested_version);
2373
Karl Meakin0e617d92024-04-05 12:55:22 +01002374 if (!ffa_versions_are_compatible(requested_version,
2375 FFA_VERSION_COMPILED)) {
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002376 dlog_error(
2377 "FFA_VERSION: requested version v%u.%u is not "
Karl Meakin5be8dfa2025-03-27 14:58:43 +00002378 "compatible with compiled version v%u.%u\n",
2379 requested_major, requested_minor, compiled_major,
2380 compiled_minor);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002381 return error;
J-Alves3cc9d6f2023-07-20 16:46:15 +01002382 }
2383
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002384 current_vm_locked = vm_lock(current->vm);
Karl Meakin5be8dfa2025-03-27 14:58:43 +00002385 vm_major = ffa_version_get_major(current_vm_locked.vm->ffa_version);
2386 vm_minor = ffa_version_get_minor(current_vm_locked.vm->ffa_version);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002387
2388 if (current_vm_locked.vm->ffa_version_negotiated &&
2389 requested_version != current_vm_locked.vm->ffa_version) {
2390 vm_unlock(&current_vm_locked);
2391 dlog_error(
Karl Meakin5be8dfa2025-03-27 14:58:43 +00002392 "FFA_VERSION: Cannot change FF-A version from v%u.%u "
2393 "to v%u.%u after other FF-A calls have been made\n",
2394 vm_major, vm_minor, requested_major, requested_minor);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002395 return error;
2396 }
2397
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002398 current_vm_locked.vm->ffa_version = requested_version;
2399 vm_unlock(&current_vm_locked);
2400
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002401 return (struct ffa_value){.func = (uint32_t)FFA_VERSION_COMPILED};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002402}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002403
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002404/**
Karl Meakin49ec1e42024-05-10 13:08:24 +01002405 * Helper for success return of FFA_FEATURES.
J-Alves6f72ca82021-11-01 12:34:58 +00002406 */
2407struct ffa_value api_ffa_feature_success(uint32_t arg2)
2408{
2409 return (struct ffa_value){
Karl Meakin49ec1e42024-05-10 13:08:24 +01002410 .func = FFA_SUCCESS_32,
2411 .arg1 = 0U,
2412 .arg2 = arg2,
2413 };
J-Alves6f72ca82021-11-01 12:34:58 +00002414}
2415
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002416static struct ffa_value ffa_features_function(uint32_t func,
2417 uint32_t input_property,
2418 struct vcpu *current)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002419{
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002420 switch (func) {
J-Alves6f72ca82021-11-01 12:34:58 +00002421 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002422 case FFA_ERROR_32:
2423 case FFA_SUCCESS_32:
2424 case FFA_INTERRUPT_32:
2425 case FFA_VERSION_32:
2426 case FFA_FEATURES_32:
2427 case FFA_RX_RELEASE_32:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002428 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002429 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002430 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002431 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002432 case FFA_RUN_32:
J-Alves95fbb312024-03-20 15:19:16 +00002433 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002434 case FFA_MEM_DONATE_32:
2435 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00002436 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002437 case FFA_MEM_SHARE_32:
J-Alves95fbb312024-03-20 15:19:16 +00002438 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002439 case FFA_MEM_RETRIEVE_RESP_32:
2440 case FFA_MEM_RELINQUISH_32:
2441 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002442 case FFA_MEM_FRAG_RX_32:
2443 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002444 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002445 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002446 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002447 case FFA_MSG_SEND_DIRECT_REQ_32:
Karl Meakinf923da32025-03-27 17:06:44 +00002448 return api_ffa_feature_success(0);
Karl Meakin0e617d92024-04-05 12:55:22 +01002449
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002450 /* FF-A v1.1 features. */
2451 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002452 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2453 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2454 case FFA_NOTIFICATION_BIND_32:
2455 case FFA_NOTIFICATION_UNBIND_32:
2456 case FFA_NOTIFICATION_SET_32:
2457 case FFA_NOTIFICATION_GET_32:
2458 case FFA_NOTIFICATION_INFO_GET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002459 case FFA_MSG_SEND2_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002460 if (FFA_VERSION_1_1 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002461 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002462 }
Karl Meakinf923da32025-03-27 17:06:44 +00002463 return api_ffa_feature_success(0);
Karl Meakin0e617d92024-04-05 12:55:22 +01002464
Kathleen Capellae4fe2962023-09-01 17:08:47 -04002465 /* FF-A v1.2 features. */
2466 case FFA_CONSOLE_LOG_32:
2467 case FFA_CONSOLE_LOG_64:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002468 case FFA_PARTITION_INFO_GET_REGS_64:
Kathleen Capella41fea932023-06-23 17:39:28 -04002469 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002470 case FFA_MSG_SEND_DIRECT_RESP2_64:
Karl Meakin0e617d92024-04-05 12:55:22 +01002471 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002472 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002473 }
2474
Karl Meakin49ec1e42024-05-10 13:08:24 +01002475 return api_ffa_feature_success(0);
2476
Karl Meakin5a222642024-06-20 10:23:37 +01002477 /* These functions are only supported on S-EL0 partitions. */
2478 case FFA_MEM_PERM_GET_32:
2479 case FFA_MEM_PERM_SET_32:
2480 case FFA_MEM_PERM_GET_64:
2481 case FFA_MEM_PERM_SET_64:
2482 if (!(vm_id_is_current_world(current->vm->id) &&
Karl Meakin5be8dfa2025-03-27 14:58:43 +00002483 current->vm->el0_partition)) {
Karl Meakin5a222642024-06-20 10:23:37 +01002484 dlog_verbose(
Karl Meakinf923da32025-03-27 17:06:44 +00002485 "FFA_FEATURES: %s is only supported on S-EL0 "
Karl Meakin5a222642024-06-20 10:23:37 +01002486 "partitions\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002487 ffa_func_name(func));
Karl Meakin5a222642024-06-20 10:23:37 +01002488 return ffa_error(FFA_NOT_SUPPORTED);
2489 }
2490 return api_ffa_feature_success(0);
2491
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002492 case FFA_SECONDARY_EP_REGISTER_64:
2493 if (FFA_VERSION_COMPILED < FFA_VERSION_1_1) {
2494 return ffa_error(FFA_NOT_SUPPORTED);
2495 }
2496
2497 if (!(vm_id_is_current_world(current->vm->id) &&
2498 current->vm->vcpu_count > 1)) {
2499 dlog_verbose(
2500 "FFA_FEATURE: %s is only supported on SPs with "
2501 "more than 1 vCPU\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002502 ffa_func_name(func));
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002503 return ffa_error(FFA_NOT_SUPPORTED);
2504 }
2505 return api_ffa_feature_success(0);
2506
Karl Meakin650cb142024-06-20 10:31:57 +01002507 /*
2508 * This function is restricted to the secure virtual FF-A instance (i.e.
2509 * only report success to SPs).
2510 */
2511 case FFA_YIELD_32:
2512 if (!vm_id_is_current_world(current->vm->id)) {
2513 dlog_verbose(
2514 "FFA_FEATURES: %s is only supported at secure "
2515 "virtual FF-A instance\n",
2516 ffa_func_name(FFA_YIELD_32));
2517 return ffa_error(FFA_NOT_SUPPORTED);
2518 }
2519 return api_ffa_feature_success(0);
2520
Karl Meakin0fd67292024-02-06 17:33:05 +00002521 case FFA_RXTX_MAP_64: {
2522 uint32_t arg2 = 0;
2523 struct ffa_features_rxtx_map_params params = {
2524 .min_buf_size = FFA_RXTX_MAP_MIN_BUF_4K,
2525 .mbz = 0,
2526 .max_buf_size =
Karl Meakin5be8dfa2025-03-27 14:58:43 +00002527 (current->vm->ffa_version >= FFA_VERSION_1_2)
Karl Meakin0fd67292024-02-06 17:33:05 +00002528 ? FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT
2529 : 0,
2530 };
2531
2532 memcpy_s(&arg2, sizeof(arg2), &params, sizeof(params));
Karl Meakin49ec1e42024-05-10 13:08:24 +01002533 return api_ffa_feature_success(arg2);
Karl Meakin0fd67292024-02-06 17:33:05 +00002534 }
2535
J-Alves95fbb312024-03-20 15:19:16 +00002536 case FFA_MEM_RETRIEVE_REQ_64:
Karl Meakin49ec1e42024-05-10 13:08:24 +01002537 case FFA_MEM_RETRIEVE_REQ_32: {
2538 if (ANY_BITS_SET(input_property,
2539 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2540 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT) ||
2541 IS_BIT_SET(input_property,
2542 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT)) {
2543 dlog_warning(
2544 "FFA_FEATURES: Bits[%u:%u] and Bit[%u] of "
2545 "input_property should be 0 (input_property = "
2546 "%#x)\n",
2547 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2548 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT,
2549 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT,
Karl Meakin34b8ae92023-01-13 13:33:07 +00002550 input_property);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002551 }
2552
Karl Meakin5be8dfa2025-03-27 14:58:43 +00002553 if (current->vm->ffa_version >= FFA_VERSION_1_1 &&
Karl Meakin0e617d92024-04-05 12:55:22 +01002554 (input_property &
2555 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
Karl Meakin4e8e4792024-07-05 16:28:42 +01002556 dlog_verbose(
2557 "FFA_FEATURES: NS bit support must be 1\n");
Karl Meakin6fd6c1d2024-05-13 12:09:13 +01002558 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002559 }
2560
2561 return api_ffa_feature_success(
2562 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
Karl Meakin49ec1e42024-05-10 13:08:24 +01002563 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT |
Karl Meakin34b8ae92023-01-13 13:33:07 +00002564 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
Karl Meakin49ec1e42024-05-10 13:08:24 +01002565 }
J-Alves6f72ca82021-11-01 12:34:58 +00002566
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002567 default:
2568 return ffa_error(FFA_NOT_SUPPORTED);
2569 }
2570}
2571
2572static struct ffa_value ffa_features_feature(enum ffa_feature_id feature,
2573 uint32_t input_property,
2574 struct vcpu *current)
2575{
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002576 if (ANY_BITS_SET(feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2577 FFA_FEATURES_FEATURE_MBZ_LO_BIT)) {
2578 dlog_verbose(
2579 "FFA_FEATURES: feature ID %#x is invalid (bits [%u:%u] "
2580 "must be zero)\n",
2581 feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2582 FFA_FEATURES_FEATURE_MBZ_LO_BIT);
2583 return ffa_error(FFA_NOT_SUPPORTED);
2584 }
2585 if (input_property != 0) {
2586 dlog_verbose(
2587 "FFA_FEATURES: input_property must be 0 "
2588 "(input_property = %#x)\n",
2589 input_property);
2590 return ffa_error(FFA_NOT_SUPPORTED);
2591 }
2592
2593 switch (feature) {
J-Alves6f72ca82021-11-01 12:34:58 +00002594 /* Check support of a feature provided respective feature ID. */
Karl Meakin17c9ad32024-04-12 16:41:00 +01002595
2596 /*
2597 * For NPI and MEI, report the IDs as supported only to partitions at
2598 * the virtual FF-A instances.
2599 */
J-Alves6f72ca82021-11-01 12:34:58 +00002600 case FFA_FEATURE_NPI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002601 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002602 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002603 }
2604
Karl Meakin147a4e82025-03-27 15:12:27 +00002605 if (current->vm->el0_partition) {
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002606 return ffa_error(FFA_NOT_SUPPORTED);
2607 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002608 if (!vm_id_is_current_world(current->vm->id)) {
2609 return ffa_error(FFA_NOT_SUPPORTED);
2610 }
J-Alves6f72ca82021-11-01 12:34:58 +00002611 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
Karl Meakin17c9ad32024-04-12 16:41:00 +01002612
2613 case FFA_FEATURE_MEI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002614 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002615 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002616 }
Karl Meakin147a4e82025-03-27 15:12:27 +00002617 if (current->vm->el0_partition) {
J-Alvesc4d9ae82024-05-14 10:15:03 +01002618 return ffa_error(FFA_NOT_SUPPORTED);
2619 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002620 if (!vm_id_is_current_world(current->vm->id)) {
2621 return ffa_error(FFA_NOT_SUPPORTED);
2622 }
2623 return api_ffa_feature_success(HF_MANAGED_EXIT_INTID);
2624
J-Alves6f72ca82021-11-01 12:34:58 +00002625 case FFA_FEATURE_SRI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002626 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002627 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002628 }
2629
Karl Meakincb6642c2024-02-27 14:43:45 +00002630 if (!ffa_is_vm_id(current->vm->id)) {
2631 return ffa_error(FFA_NOT_SUPPORTED);
2632 }
J-Alves6f72ca82021-11-01 12:34:58 +00002633 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
Karl Meakin0e617d92024-04-05 12:55:22 +01002634
J-Alves6f72ca82021-11-01 12:34:58 +00002635 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002636 default:
Karl Meakin4271ff92024-05-13 12:33:15 +01002637 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002638 }
2639}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002640
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002641/**
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002642 * Discovery function returning information about the implementation of optional
2643 * FF-A interfaces. See section 13.3 of the FF-A v1.2 ALP1 spec.
2644 *
2645 * `function_or_feature_id` is interpreted as either a function ID or a feature
2646 * ID, depending on the value of bit 31.
2647 * When it is a feature ID, bits [30:8] MBZ and input_property MBZ.
2648 *
2649 * Returns `FFA_SUCCESS` if the interface is supported.
2650 * Returns `FFA_NOT_SUPPORTED` if the interface is not supported or the
2651 * parameters are invalid.
2652 */
2653struct ffa_value api_ffa_features(uint32_t function_or_feature_id,
2654 uint32_t input_property, struct vcpu *current)
2655{
2656 return IS_BIT_UNSET(function_or_feature_id, FFA_FEATURES_FEATURE_BIT)
2657 ? ffa_features_feature(function_or_feature_id,
2658 input_property, current)
2659 : ffa_features_function(function_or_feature_id,
2660 input_property, current);
2661}
2662
2663/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002664 * FF-A specification states that x2/w2 Must Be Zero for FFA_MSG_SEND_DIRECT_REQ
2665 * and FFA_MSG_SEND_DIRECT_RESP interfaces when used for partition messages. See
2666 * FF-A v1.2 Table 16.6: FFA_MSG_SEND_DIRECT_REQ function syntax.
J-Alves645eabe2021-02-22 16:08:27 +00002667 */
2668static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2669{
2670 return args.arg2 == 0U;
2671}
2672
2673/**
J-Alves76d99af2021-03-10 17:42:11 +00002674 * Limits size of arguments in ffa_value structure to 32-bit.
2675 */
2676static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2677{
2678 return (struct ffa_value){
2679 .func = (uint32_t)args.func,
2680 .arg1 = (uint32_t)args.arg1,
Karl Meakin06e8b732024-09-20 18:26:49 +01002681 .arg2 = (uint32_t)args.arg2,
J-Alves76d99af2021-03-10 17:42:11 +00002682 .arg3 = (uint32_t)args.arg3,
2683 .arg4 = (uint32_t)args.arg4,
2684 .arg5 = (uint32_t)args.arg5,
2685 .arg6 = (uint32_t)args.arg6,
2686 .arg7 = (uint32_t)args.arg7,
2687 };
2688}
2689
2690/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002691 * Helper to copy direct message payload, depending on SMC used, direct
Karl Meakin06e8b732024-09-20 18:26:49 +01002692 * messaging interface used, and expected registers size. Can be used for both
2693 * framework messages and standard messages.
J-Alves76d99af2021-03-10 17:42:11 +00002694 */
2695static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2696{
Karl Meakin06e8b732024-09-20 18:26:49 +01002697 switch (args.func) {
2698 case FFA_MSG_SEND_DIRECT_REQ_32:
2699 case FFA_MSG_SEND_DIRECT_RESP_32:
2700 args = api_ffa_value_copy32(args);
2701 if (!ffa_is_framework_msg(args)) {
2702 args.arg2 = 0;
2703 }
2704 break;
J-Alves76d99af2021-03-10 17:42:11 +00002705
Karl Meakin06e8b732024-09-20 18:26:49 +01002706 case FFA_MSG_SEND_DIRECT_REQ_64:
2707 case FFA_MSG_SEND_DIRECT_RESP_64:
2708 if (!ffa_is_framework_msg(args)) {
2709 args.arg2 = 0;
2710 }
2711 break;
Kathleen Capella41fea932023-06-23 17:39:28 -04002712
Karl Meakin06e8b732024-09-20 18:26:49 +01002713 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002714 args.extended_val.valid = true;
Karl Meakin06e8b732024-09-20 18:26:49 +01002715 break;
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002716
Karl Meakin06e8b732024-09-20 18:26:49 +01002717 case FFA_MSG_SEND_DIRECT_RESP2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002718 args.arg2 = 0;
2719 args.arg3 = 0;
Karl Meakin06e8b732024-09-20 18:26:49 +01002720 break;
2721 default:
2722 panic("Invalid direct message function %#x\n", args.func);
2723 break;
Kathleen Capella087e5022023-09-07 18:04:15 -04002724 }
2725
Kathleen Capella41fea932023-06-23 17:39:28 -04002726 return args;
2727}
2728
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002729/**
2730 * Return a pointer to the first UUID in `uuids` that is equal to
2731 * `target_uuid`. If `target_uuid` is 0-0-0-0, it matches any UUID.
2732 */
2733static struct ffa_uuid *ffa_uuid_find(struct ffa_uuid *uuids,
2734 size_t uuids_count,
2735 struct ffa_uuid target_uuid)
2736{
2737 if (ffa_uuid_is_null(&target_uuid)) {
2738 return &uuids[0];
2739 }
2740
2741 for (size_t i = 0; i < uuids_count; i++) {
2742 if (ffa_uuid_is_null(&uuids[i])) {
2743 break;
2744 }
2745 if (ffa_uuid_equal(&target_uuid, &uuids[i])) {
2746 return &uuids[i];
2747 }
2748 }
2749 return NULL;
2750}
2751
Kathleen Capella41fea932023-06-23 17:39:28 -04002752static bool api_ffa_dir_msg_req2_is_uuid_valid(struct vm *receiver_vm,
2753 struct ffa_value args)
2754{
2755 struct ffa_uuid target_uuid;
2756
Karl Meakin9478e322024-09-23 17:47:09 +01002757 ffa_uuid_from_u64x2(args.arg2, args.arg3, &target_uuid);
Kathleen Capella41fea932023-06-23 17:39:28 -04002758
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002759 return ffa_uuid_find(receiver_vm->uuids, PARTITION_MAX_UUIDS,
2760 target_uuid) != NULL;
J-Alves76d99af2021-03-10 17:42:11 +00002761}
2762
2763/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002764 * Send an FF-A direct message request.
Kathleen Capella41fea932023-06-23 17:39:28 -04002765 * This handler covers both FFA_MSG_SEND_DIRECT_REQ_32/64
2766 * and FFA_MSG_SEND_DIRECT_REQ2_64 (introduced in FF-A v1.2) with function-based
2767 * checks to accomodate for the difference between the ABIs.
2768 *
2769 * FFA_MSG_SEND_DIRECT_REQ2_64 works mostly the same as
2770 * FFA_MSG_SEND_DIRECT_REQ_32/64, but adds the ability to send a direct message
2771 * request to a specified UUID within a partition and the usage of an extended
2772 * range of registers (x4-x17, instead of x4-x7) to be used as part of the
2773 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002774 */
Karl Meakin13f09812024-10-28 16:33:23 +00002775struct ffa_value api_ffa_msg_send_direct_req(struct ffa_value args,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002776 struct vcpu *current,
2777 struct vcpu **next)
2778{
Karl Meakin13f09812024-10-28 16:33:23 +00002779 ffa_id_t sender_vm_id = ffa_sender(args);
2780 ffa_id_t receiver_vm_id = ffa_receiver(args);
J-Alves17228f72021-04-20 17:13:19 +01002781 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002782 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002783 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002784 struct vcpu *receiver_vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002785 struct vcpu_locked current_locked;
2786 struct vcpu_locked receiver_vcpu_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002787 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002788 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002789
Kathleen Capella41fea932023-06-23 17:39:28 -04002790 if ((args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2791 args.func == FFA_MSG_SEND_DIRECT_REQ_64) &&
Karl Meakin06e8b732024-09-20 18:26:49 +01002792 !ffa_is_framework_msg(args) &&
Kathleen Capella41fea932023-06-23 17:39:28 -04002793 !api_ffa_dir_msg_is_arg2_zero(args)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002794 dlog_verbose("Direct messaging: w2 must be zero (w2 = %#lx)\n",
2795 args.arg2);
J-Alves645eabe2021-02-22 16:08:27 +00002796 return ffa_error(FFA_INVALID_PARAMETERS);
2797 }
2798
Karl Meakin06e8b732024-09-20 18:26:49 +01002799 if (ffa_is_framework_msg(args) &&
Karl Meakinfa1dcb82025-02-10 16:47:50 +00002800 ffa_direct_msg_handle_framework_msg(args, &ret)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002801 return ret;
2802 }
2803
Karl Meakin117c8082024-12-04 16:03:28 +00002804 if (!ffa_direct_msg_is_direct_request_valid(current, sender_vm_id,
2805 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002806 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002807 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002808 }
2809
Karl Meakin117c8082024-12-04 16:03:28 +00002810 if (ffa_direct_msg_direct_request_forward(receiver_vm_id, args, &ret)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002811 dlog_verbose("Direct message request forwarded\n");
J-Alves17228f72021-04-20 17:13:19 +01002812 return ret;
2813 }
2814
J-Alvesbd32c972024-02-02 16:20:04 +00002815 ret = api_ffa_interrupt_return(0);
J-Alves17228f72021-04-20 17:13:19 +01002816
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002817 receiver_vm = vm_find(receiver_vm_id);
2818 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002819 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002820 return ffa_error(FFA_INVALID_PARAMETERS);
2821 }
2822
Kathleen Capella41fea932023-06-23 17:39:28 -04002823 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64 &&
2824 !api_ffa_dir_msg_req2_is_uuid_valid(receiver_vm, args)) {
2825 dlog_verbose("UUID unrecognized for this VM\n");
2826 return ffa_error(FFA_INVALID_PARAMETERS);
2827 }
2828
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002829 /*
J-Alves439ac972021-11-18 17:32:03 +00002830 * Check if sender supports sending direct message req, and if
2831 * receiver supports receipt of direct message requests.
2832 */
Karl Meakin117c8082024-12-04 16:03:28 +00002833 if (!ffa_direct_msg_is_direct_request_supported(
2834 current->vm, receiver_vm, args.func)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002835 dlog_verbose("Direct message request not supported\n");
J-Alves439ac972021-11-18 17:32:03 +00002836 return ffa_error(FFA_DENIED);
2837 }
2838
2839 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002840 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002841 * UP (migratable) or MP partitions with a number of vCPUs matching the
2842 * number of PEs in the system. It further states that MP partitions
2843 * accepting direct request messages cannot migrate.
2844 */
J-Alvesad6a0432021-04-09 16:06:21 +01002845 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002846 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002847 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002848 return ffa_error(FFA_INVALID_PARAMETERS);
2849 }
2850
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002851 /*
2852 * If VM must be locked, it must be done before any of its vCPUs are
2853 * locked.
2854 */
J-Alves6e2abc62021-12-02 14:58:56 +00002855 receiver_locked = vm_lock(receiver_vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002856
2857 /* Lock both vCPUs at once to avoid deadlock. */
2858 vcpus_locked = vcpu_lock_both(current, receiver_vcpu);
2859 current_locked = vcpus_locked.vcpu1;
2860 receiver_vcpu_locked = vcpus_locked.vcpu2;
2861
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002862 /*
2863 * If destination vCPU is executing or already received an
2864 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2865 * busy. There is a brief period of time where the vCPU state has
2866 * changed but regs_available is still false thus consider this case as
2867 * the vCPU not yet ready to receive a direct message request.
2868 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002869 if (is_ffa_direct_msg_request_ongoing(receiver_vcpu_locked) ||
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002870 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2871 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002872 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002873 ret = ffa_error(FFA_BUSY);
2874 goto out;
2875 }
2876
Karl Meakin117c8082024-12-04 16:03:28 +00002877 if (!ffa_cpu_cycles_check_runtime_state_transition(
J-Alves7ea3f7e2024-03-27 11:05:11 +00002878 current_locked, sender_vm_id, HF_INVALID_VM_ID,
2879 receiver_vcpu_locked, args.func, &next_state)) {
2880 ret = ffa_error(FFA_DENIED);
2881 goto out;
2882 }
2883
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002884 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2885 memory_order_relaxed)) {
2886 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04002887 dlog_verbose(
2888 "Receiver VM %#x aborted, cannot run vCPU %u\n",
2889 receiver_vcpu->vm->id,
2890 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002891 receiver_vcpu->state = VCPU_STATE_ABORTED;
2892 }
2893
2894 ret = ffa_error(FFA_ABORTED);
2895 goto out;
2896 }
2897
2898 switch (receiver_vcpu->state) {
2899 case VCPU_STATE_OFF:
2900 case VCPU_STATE_RUNNING:
2901 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002902 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002903 case VCPU_STATE_BLOCKED:
2904 case VCPU_STATE_PREEMPTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002905 ret = ffa_error(FFA_BUSY);
2906 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002907 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002908 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002909 * We expect target vCPU to be in WAITING state after either
2910 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002911 */
2912 break;
2913 }
2914
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05002915 /* Inject timer interrupt if timer has expired. */
Daniel Boulbyd49d0772025-01-15 11:19:36 +00002916 api_inject_arch_timer_interrupt(receiver_vcpu_locked);
Madhukar Pappireddy106bfc32024-09-25 15:47:11 -05002917 timer_migrate_to_other_cpu(current->cpu, receiver_vcpu_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002918
2919 /* The receiver vCPU runs upon direct message invocation */
2920 receiver_vcpu->cpu = current->cpu;
2921 receiver_vcpu->state = VCPU_STATE_RUNNING;
2922 receiver_vcpu->regs_available = false;
Kathleen Capellae468c112023-12-13 17:56:28 -05002923 receiver_vcpu->direct_request_origin.is_ffa_req2 =
2924 (args.func == FFA_MSG_SEND_DIRECT_REQ2_64);
2925 receiver_vcpu->direct_request_origin.vm_id = sender_vm_id;
Karl Meakin06e8b732024-09-20 18:26:49 +01002926 receiver_vcpu->direct_request_origin.is_framework =
2927 ffa_is_framework_msg(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002928
J-Alves76d99af2021-03-10 17:42:11 +00002929 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002930
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002931 assert(!vm_id_is_current_world(current->vm->id) ||
2932 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002933 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002934
Karl Meakin117c8082024-12-04 16:03:28 +00002935 ffa_direct_msg_wind_call_chain_ffa_direct_req(
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07002936 current_locked, receiver_vcpu_locked, sender_vm_id);
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05002937
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002938 /* Switch to receiver vCPU targeted to by direct msg request */
2939 *next = receiver_vcpu;
2940
J-Alves6e2abc62021-12-02 14:58:56 +00002941 if (!receiver_locked.vm->el0_partition) {
2942 /*
2943 * If the scheduler in the system is giving CPU cycles to the
2944 * receiver, due to pending notifications, inject the NPI
2945 * interrupt. Following call assumes that '*next' has been set
2946 * to receiver_vcpu.
2947 */
Karl Meakin117c8082024-12-04 16:03:28 +00002948 ffa_interrupts_inject_notification_pending_interrupt(
Daniel Boulbyd49d0772025-01-15 11:19:36 +00002949 receiver_vcpu_locked, receiver_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00002950 }
2951
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002952 /*
2953 * Since this flow will lead to a VM switch, the return value will not
2954 * be applied to current vCPU.
2955 */
2956
2957out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002958 vcpu_unlock(&receiver_vcpu_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00002959 vm_unlock(&receiver_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002960 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002961
2962 return ret;
2963}
2964
2965/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002966 * Resume the target vCPU after the current vCPU sent a direct response.
2967 * Current vCPU moves to waiting state.
2968 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002969void api_ffa_resume_direct_resp_target(struct vcpu_locked current_locked,
2970 struct vcpu **next,
J-Alves19e20cf2023-08-02 12:48:55 +01002971 ffa_id_t receiver_vm_id,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002972 struct ffa_value to_ret,
2973 bool is_nwd_call_chain)
2974{
Karl Meakinfa1dcb82025-02-10 16:47:50 +00002975 if (ffa_direct_msg_is_spmd_lp_id(receiver_vm_id) ||
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07002976 !vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002977 *next = api_switch_to_other_world(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002978 VCPU_STATE_WAITING);
2979
2980 /* End of NWd scheduled call chain. */
2981 assert(!is_nwd_call_chain ||
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002982 (current_locked.vcpu->call_chain.prev_node == NULL));
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002983 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002984 *next = api_switch_to_primary(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002985 VCPU_STATE_WAITING);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002986 } else if (vm_id_is_current_world(receiver_vm_id)) {
2987 /*
2988 * It is expected the receiver_vm_id to be from an SP, otherwise
Karl Meakin117c8082024-12-04 16:03:28 +00002989 * 'ffa_direct_msg_is_direct_response_valid' should have
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002990 * made function return error before getting to this point.
2991 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002992 *next = api_switch_to_vm(current_locked, to_ret,
2993 VCPU_STATE_WAITING, receiver_vm_id);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002994 } else {
2995 panic("Invalid direct message response invocation");
2996 }
2997}
2998
Karl Meakin50ef9112024-10-28 16:41:44 +00002999static bool api_ffa_msg_send_direct_resp_validate_args(struct ffa_value args,
3000 struct vcpu *current)
3001{
3002 ffa_id_t sender_vm_id = ffa_sender(args);
3003 ffa_id_t receiver_vm_id = ffa_receiver(args);
3004
3005 /*
3006 * If using FFA_MSG_SEND_DIRECT_RESP, the caller's
3007 * - x2 MBZ for partition messages
3008 * - x8-x17 SBZ if caller's FF-A version >= FF-A v1.2
3009 */
3010 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) {
3011 if (!ffa_is_framework_msg(args) &&
3012 !api_ffa_dir_msg_is_arg2_zero(args)) {
3013 dlog_verbose("%s: w2 Must Be Zero",
3014 ffa_func_name(args.func));
3015 return false;
3016 }
3017
3018 if (current->vm->ffa_version >= FFA_VERSION_1_2 &&
3019 !api_extended_args_are_zero(&args)) {
3020 return false;
3021 }
3022 }
3023
Karl Meakin117c8082024-12-04 16:03:28 +00003024 if (!ffa_direct_msg_is_direct_response_valid(current, sender_vm_id,
3025 receiver_vm_id)) {
Karl Meakin50ef9112024-10-28 16:41:44 +00003026 dlog_verbose("Invalid direct response call.\n");
3027 return false;
3028 }
3029
3030 return true;
3031}
3032
3033static bool api_ffa_msg_send_direct_resp_validate_ongoing_request(
3034 struct ffa_value args, struct vcpu_locked current_locked)
3035{
3036 bool req_framework =
3037 current_locked.vcpu->direct_request_origin.is_framework;
3038 bool resp_framework = ffa_is_framework_msg(args);
3039 bool received_req2 =
3040 current_locked.vcpu->direct_request_origin.is_ffa_req2;
3041
3042 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
3043 return false;
3044 }
3045
3046 if (req_framework && !resp_framework) {
3047 dlog_verbose(
3048 "Mismatch in framework message bit: request was a %s "
3049 "message, but response is a %s message\n",
3050 req_framework ? "framework" : "non-framework",
3051 resp_framework ? "framework" : "non-framework");
3052 return false;
3053 }
3054
3055 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64 && received_req2) {
3056 dlog_verbose(
3057 "FFA_MSG_SEND_DIRECT_RESP must be used with "
3058 "FFA_MSG_SEND_DIRECT_REQ\n");
3059 return false;
3060 }
3061
3062 if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64 && !received_req2) {
3063 dlog_verbose(
3064 "FFA_MSG_SEND_DIRECT_RESP2 must be used with "
3065 "FFA_MSG_SEND_DIRECT_REQ2\n");
3066 return false;
3067 }
3068
3069 return true;
3070}
3071
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003072/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003073 * Send an FF-A direct message response.
Kathleen Capella087e5022023-09-07 18:04:15 -04003074 * This handler covers both FFA_MSG_SEND_DIRECT_RESP_32/64
3075 * and FFA_MSG_SEND_DIRECT_RESP2_64 (introduced in FF-A v1.2) with
3076 * function-based checks to accomodate for the difference between the ABIs.
3077 *
3078 * FFA_MSG_SEND_DIRECT_RESP2_64 is used to respond to requests sent via
3079 * FFA_MSG_SEND_DIRECT_REQ2_64 and adds the usage of an extended range
3080 * of registers (x4-x17, instead of x4-x7) to be used as part of the
3081 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003082 */
Karl Meakin13f09812024-10-28 16:33:23 +00003083struct ffa_value api_ffa_msg_send_direct_resp(struct ffa_value args,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003084 struct vcpu *current,
3085 struct vcpu **next)
3086{
Karl Meakin13f09812024-10-28 16:33:23 +00003087 ffa_id_t sender_vm_id = ffa_sender(args);
3088 ffa_id_t receiver_vm_id = ffa_receiver(args);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003089 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003090 struct vcpu_locked next_locked = (struct vcpu_locked){
3091 .vcpu = NULL,
3092 };
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003093 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003094 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003095 struct ffa_value signal_interrupt =
3096 (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003097 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
3098 struct two_vcpu_locked vcpus_locked;
J-Alves645eabe2021-02-22 16:08:27 +00003099
Karl Meakin50ef9112024-10-28 16:41:44 +00003100 if (!api_ffa_msg_send_direct_resp_validate_args(args, current)) {
J-Alvesaa336102021-03-01 13:02:45 +00003101 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003102 }
3103
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003104 current_locked = vcpu_lock(current);
3105
Karl Meakin117c8082024-12-04 16:03:28 +00003106 if (!ffa_cpu_cycles_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003107 current_locked, sender_vm_id, receiver_vm_id, next_locked,
3108 args.func, &next_state)) {
3109 ret = ffa_error(FFA_DENIED);
3110 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05003111 }
3112
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003113 assert(!vm_id_is_current_world(current->vm->id) ||
3114 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003115
Karl Meakin50ef9112024-10-28 16:41:44 +00003116 if (!api_ffa_msg_send_direct_resp_validate_ongoing_request(
3117 args, current_locked)) {
Kathleen Capellae468c112023-12-13 17:56:28 -05003118 ret = ffa_error(FFA_DENIED);
3119 goto out;
3120 }
3121
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003122 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Madhukar Pappireddy7945bb52024-08-20 15:22:41 -05003123 CHECK(current->scheduling_mode != SPMC_MODE);
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003124
Madhukar Pappireddy025a4512024-10-14 22:09:19 -05003125 plat_interrupts_set_priority_mask(
3126 current->prev_interrupt_priority);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003127 /*
3128 * A SP may be signaled a managed exit but actually not trap
3129 * the virtual interrupt, probably because it has virtual
3130 * interrupts masked, and emit direct resp. In this case the
3131 * managed exit operation is considered completed and it would
3132 * also need to clear the pending managed exit flag for the SP
3133 * vCPU.
3134 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003135 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003136
Daniel Boulbyd7992232025-03-06 17:09:49 +00003137 vcpu_virt_interrupt_clear(current_locked,
3138 HF_MANAGED_EXIT_INTID);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003139 }
3140
Kathleen Capellae468c112023-12-13 17:56:28 -05003141 /* Clear direct request origin vm_id and request type for the caller. */
3142 current->direct_request_origin.is_ffa_req2 = false;
3143 current->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003144
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003145 api_ffa_resume_direct_resp_target(current_locked, next, receiver_vm_id,
3146 to_ret, false);
3147
3148 /*
3149 * Unlock current vCPU to allow it to be locked together with next
3150 * vcpu.
3151 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003152 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003153
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003154 /* Lock both vCPUs at once to avoid deadlock. */
3155 vcpus_locked = vcpu_lock_both(current, *next);
3156 current_locked = vcpus_locked.vcpu1;
3157 next_locked = vcpus_locked.vcpu2;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003158
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05003159 /* Inject timer interrupt if timer has expired. */
Daniel Boulbyd49d0772025-01-15 11:19:36 +00003160 api_inject_arch_timer_interrupt(next_locked);
Karl Meakin117c8082024-12-04 16:03:28 +00003161 ffa_direct_msg_unwind_call_chain_ffa_direct_resp(current_locked,
3162 next_locked);
J-Alvesbd32c972024-02-02 16:20:04 +00003163
3164 /*
J-Alves4796d112025-02-12 14:39:00 +00003165 * Check if there is a pending interrupt, and if the partition
3166 * is expects to notify the scheduler or resume straight away.
3167 * Either trigger SRI for later donation of CPU cycles, or
3168 * eret `FFA_INTERRUPT` back to the caller.
J-Alvesbd32c972024-02-02 16:20:04 +00003169 */
Karl Meakin117c8082024-12-04 16:03:28 +00003170 if (ffa_interrupts_intercept_call(current_locked, next_locked,
3171 &signal_interrupt)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003172 ret = signal_interrupt;
3173 *next = NULL;
3174 }
3175
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003176 vcpu_unlock(&next_locked);
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05003177
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003178out:
3179 vcpu_unlock(&current_locked);
3180 return ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003181}
3182
J-Alves84658fc2021-06-17 14:37:32 +01003183static bool api_memory_region_check_flags(
3184 struct ffa_memory_region *memory_region, uint32_t share_func)
3185{
3186 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00003187 case FFA_MEM_SHARE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003188 case FFA_MEM_SHARE_32:
3189 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3190 0U) {
3191 return false;
3192 }
3193 /* Intentional fall-through */
J-Alves95fbb312024-03-20 15:19:16 +00003194 case FFA_MEM_LEND_64:
J-Alves84658fc2021-06-17 14:37:32 +01003195 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00003196 case FFA_MEM_DONATE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003197 case FFA_MEM_DONATE_32: {
3198 /* Bits 31:2 Must Be Zero. */
3199 ffa_memory_receiver_flags_t to_mask =
3200 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
3201 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
3202
3203 if ((memory_region->flags & to_mask) != 0U) {
3204 return false;
3205 }
3206 break;
3207 }
3208 default:
3209 panic("Check for mem send calls only.\n");
3210 }
3211
3212 /* Last check reserved values are 0 */
3213 return true;
3214}
3215
J-Alves33c47bf2022-09-29 11:36:20 +01003216/*
3217 * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0.
3218 */
3219static void api_ffa_memory_region_v1_1_from_v1_0(
3220 struct ffa_memory_region_v1_0 *memory_region_v1_0,
3221 struct ffa_memory_region *memory_region_v1_1)
3222{
3223 memory_region_v1_1->sender = memory_region_v1_0->sender;
J-Alves00847062022-10-03 17:08:44 +01003224 memory_region_v1_1->handle = memory_region_v1_0->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003225 memory_region_v1_1->attributes =
3226 ffa_memory_attributes_extend(memory_region_v1_0->attributes);
J-Alves33c47bf2022-09-29 11:36:20 +01003227 memory_region_v1_1->flags = memory_region_v1_0->flags;
3228 memory_region_v1_1->tag = memory_region_v1_0->tag;
3229 memory_region_v1_1->memory_access_desc_size =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003230 sizeof(struct ffa_memory_access_v1_0);
J-Alves33c47bf2022-09-29 11:36:20 +01003231 memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003232 memory_region_v1_1->receivers_offset = sizeof(struct ffa_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003233
3234 /* Zero reserved fields. */
3235 for (uint32_t i = 0; i < 3U; i++) {
3236 memory_region_v1_1->reserved[i] = 0U;
3237 }
3238}
3239
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003240/**
3241 * Updates a v1.0 transaction descriptor to v1.1. This gives us the
3242 * memory_access_desc_size field we need for forwards compatability.
3243 * Copy the receivers and composite descriptors to the new struct.
3244 * We also check the fields in the v1.0 transaction descriptor and return:
3245 * - FFA_ERROR FFA_INVALID_PARAMETERS: If any of the fields are not valid
3246 * values, eg the reserved fields are not 0, receiver_count is too large or
3247 * composite offsets are not 0 for retrieve requests or in bounds for send
3248 * requests.
3249 * - FFA ERROR FFA_NOT_SUPPORTED: If an invalid ffa_version is supplied to the
3250 * function. Or the fragment length is more than a single page.
3251 * - FFA_ERROR FFA_NO_MEMORY: If we do not have enough memory for a scratch
3252 * memory transaction descriptor.
3253 * - FFA_SUCCESS: If a successful update has occured.
J-Alves33c47bf2022-09-29 11:36:20 +01003254 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003255static struct ffa_value api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3256 void *allocated, uint32_t *fragment_length, uint32_t *total_length,
Karl Meakin0e617d92024-04-05 12:55:22 +01003257 enum ffa_version ffa_version, bool send_transaction)
J-Alves33c47bf2022-09-29 11:36:20 +01003258{
3259 struct ffa_memory_region_v1_0 *memory_region_v1_0;
3260 struct ffa_memory_region *memory_region_v1_1 = NULL;
3261 struct ffa_composite_memory_region *composite_v1_0;
3262 struct ffa_composite_memory_region *composite_v1_1;
3263 size_t receivers_length;
3264 size_t space_left;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003265 size_t receivers_end;
J-Alves33c47bf2022-09-29 11:36:20 +01003266 size_t composite_offset_v1_1;
3267 size_t composite_offset_v1_0;
3268 size_t fragment_constituents_size;
3269 size_t fragment_length_v1_1;
3270
J-Alves33c47bf2022-09-29 11:36:20 +01003271 assert(fragment_length != NULL);
3272 assert(total_length != NULL);
3273
Karl Meakin0e617d92024-04-05 12:55:22 +01003274 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves33c47bf2022-09-29 11:36:20 +01003275 return (struct ffa_value){.func = FFA_SUCCESS_32};
3276 }
3277
Karl Meakin0e617d92024-04-05 12:55:22 +01003278 if (ffa_version != FFA_VERSION_1_0) {
J-Alves33c47bf2022-09-29 11:36:20 +01003279 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
3280 ffa_version);
3281 return ffa_error(FFA_NOT_SUPPORTED);
3282 }
3283
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003284 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003285 "Updating memory transaction descriptor from v1.0 to v1.1.\n");
J-Alves33c47bf2022-09-29 11:36:20 +01003286
3287 memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated;
3288
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003289 receivers_length = sizeof(struct ffa_memory_access_v1_0) *
J-Alves33c47bf2022-09-29 11:36:20 +01003290 memory_region_v1_0->receiver_count;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003291 receivers_end = sizeof(struct ffa_memory_region) + receivers_length;
J-Alves33c47bf2022-09-29 11:36:20 +01003292
3293 /*
3294 * Check the specified composite offset of v1.0 descriptor, and that all
3295 * receivers were configured with the same offset.
3296 */
3297 composite_offset_v1_0 =
3298 memory_region_v1_0->receivers[0].composite_memory_region_offset;
3299
J-Alves33c47bf2022-09-29 11:36:20 +01003300 /* Determine the composite offset for v1.1 descriptor. */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003301 if (send_transaction) {
3302 fragment_constituents_size =
3303 *fragment_length - composite_offset_v1_0 -
3304 sizeof(struct ffa_composite_memory_region);
3305 fragment_length_v1_1 =
3306 receivers_end +
3307 sizeof(struct ffa_composite_memory_region) +
3308 fragment_constituents_size;
3309 composite_offset_v1_1 = receivers_end;
3310 } else {
3311 fragment_constituents_size = 0;
3312 fragment_length_v1_1 = receivers_end;
3313 composite_offset_v1_1 = 0;
3314 }
J-Alves33c47bf2022-09-29 11:36:20 +01003315
3316 /*
3317 * Currently only support the simpler cases: memory transaction
3318 * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE.
3319 * TODO: allocate the entries needed for this fragment_length_v1_1.
3320 * - Check corner when v1.1 descriptor converted size surpasses
3321 * the size of the entry.
3322 */
3323 if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) {
3324 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003325 "Translation of FF-A v1.0 descriptors for over %lu is "
J-Alves33c47bf2022-09-29 11:36:20 +01003326 "unsupported.",
3327 MM_PPOOL_ENTRY_SIZE);
3328 return ffa_error(FFA_NOT_SUPPORTED);
3329 }
3330
3331 space_left = fragment_length_v1_1;
3332
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003333 /*
3334 * Allocate a page of memory to construct the v1.1 memory descriptor.
3335 * Earlier we checked that the fragment_length_v1_1 would not be larger
3336 * than a page.
3337 */
J-Alves33c47bf2022-09-29 11:36:20 +01003338 memory_region_v1_1 =
3339 (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
3340 if (memory_region_v1_1 == NULL) {
3341 return ffa_error(FFA_NO_MEMORY);
3342 }
3343
3344 /* Translate header from v1.0 to v1.1. */
3345 api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0,
3346 memory_region_v1_1);
3347
3348 space_left -= sizeof(struct ffa_memory_region);
3349
3350 /* Copy memory access information. */
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003351 memcpy_s((uint8_t *)memory_region_v1_1 +
3352 memory_region_v1_1->receivers_offset,
3353 space_left, memory_region_v1_0->receivers, receivers_length);
J-Alves33c47bf2022-09-29 11:36:20 +01003354
3355 /* Initialize the memory access descriptors with composite offset. */
3356 for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) {
3357 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003358 ffa_memory_region_get_receiver(memory_region_v1_1, i);
3359 assert(receiver != NULL);
J-Alves33c47bf2022-09-29 11:36:20 +01003360 receiver->composite_memory_region_offset =
3361 composite_offset_v1_1;
3362 }
3363
3364 space_left -= receivers_length;
3365
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003366 /* Composite memory descriptors to copy. */
3367 if (send_transaction) {
3368 /* Init v1.1 composite. */
3369 composite_v1_1 = (struct ffa_composite_memory_region
3370 *)((uint8_t *)memory_region_v1_1 +
3371 composite_offset_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003372
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003373 composite_v1_0 = ffa_memory_region_get_composite_v1_0(
3374 memory_region_v1_0, 0);
3375 composite_v1_1->constituent_count =
3376 composite_v1_0->constituent_count;
3377 composite_v1_1->page_count = composite_v1_0->page_count;
J-Alves33c47bf2022-09-29 11:36:20 +01003378
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003379 space_left -= sizeof(struct ffa_composite_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003380
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003381 /* Initialize v1.1 constituents. */
3382 memcpy_s(composite_v1_1->constituents, space_left,
3383 composite_v1_0->constituents,
3384 fragment_constituents_size);
J-Alves33c47bf2022-09-29 11:36:20 +01003385
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003386 space_left -= fragment_constituents_size;
3387 }
3388
J-Alves33c47bf2022-09-29 11:36:20 +01003389 assert(space_left == 0U);
3390
J-Alves33c47bf2022-09-29 11:36:20 +01003391 /*
3392 * Remove the v1.0 fragment size, and resultant size of v1.1 fragment.
3393 */
3394 *total_length = *total_length - *fragment_length + fragment_length_v1_1;
3395 *fragment_length = fragment_length_v1_1;
3396
3397 /*
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003398 * After successfully updating to v1.1 copy the descriptor to the
3399 * internal buffer given as a parameter (used to prevent TOCTOU attacks)
3400 * and free the scratch memory used to construct it.
J-Alves33c47bf2022-09-29 11:36:20 +01003401 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003402 memcpy_s(allocated, MM_PPOOL_ENTRY_SIZE, memory_region_v1_1,
3403 *fragment_length);
3404 mpool_free(&api_page_pool, memory_region_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003405
3406 return (struct ffa_value){.func = FFA_SUCCESS_32};
3407}
3408
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003409struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
3410 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003411 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003412{
3413 struct vm *from = current->vm;
3414 struct vm *to;
3415 const void *from_msg;
J-Alves33c47bf2022-09-29 11:36:20 +01003416 void *allocated_entry;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003417 struct ffa_memory_region *memory_region = NULL;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003418 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01003419 bool targets_other_world = false;
Karl Meakin0e617d92024-04-05 12:55:22 +01003420 enum ffa_version ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003421
3422 if (ipa_addr(address) != 0 || page_count != 0) {
3423 /*
3424 * Hafnium only supports passing the descriptor in the TX
3425 * mailbox.
3426 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003427 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003428 }
3429
Andrew Walbranca808b12020-05-15 17:22:28 +01003430 if (fragment_length > length) {
3431 dlog_verbose(
3432 "Fragment length %d greater than total length %d.\n",
3433 fragment_length, length);
3434 return ffa_error(FFA_INVALID_PARAMETERS);
3435 }
J-Alves33c47bf2022-09-29 11:36:20 +01003436
3437 if (fragment_length > HF_MAILBOX_SIZE ||
3438 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003439 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003440 }
3441
3442 /*
3443 * Check that the sender has configured its send buffer. If the TX
3444 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3445 * be safely accessed after releasing the lock since the TX mailbox
3446 * address can only be configured once.
3447 */
3448 sl_lock(&from->lock);
3449 from_msg = from->mailbox.send;
J-Alves33c47bf2022-09-29 11:36:20 +01003450 ffa_version = from->ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003451 sl_unlock(&from->lock);
3452
3453 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003454 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003455 }
3456
3457 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003458 * Copy the memory region descriptor to a fresh page from the memory
3459 * pool. This prevents the sender from changing it underneath us, and
3460 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003461 */
J-Alves33c47bf2022-09-29 11:36:20 +01003462 allocated_entry = mpool_alloc(&api_page_pool);
3463 if (allocated_entry == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003464 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003465 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003466 }
J-Alves33c47bf2022-09-29 11:36:20 +01003467
J-Alves8f2150d2024-03-28 16:15:56 +00003468 if (!memcpy_trapped(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg,
3469 fragment_length)) {
3470 dlog_error(
3471 "%s: Failed to copy FF-A memory region descriptor.\n",
3472 __func__);
leisened7ed3d2024-08-06 17:25:18 +08003473 ret = ffa_error(FFA_ABORTED);
3474 goto out;
J-Alves8f2150d2024-03-28 16:15:56 +00003475 }
J-Alves33c47bf2022-09-29 11:36:20 +01003476
J-Alves8bc47f32025-03-21 10:23:51 +00003477 /*
3478 * Out-of-bounds accesses should be eliminated by the sanity checks
3479 * below.
3480 */
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003481 if (!ffa_memory_region_sanity_check(allocated_entry, ffa_version,
3482 fragment_length, true)) {
3483 ret = ffa_error(FFA_INVALID_PARAMETERS);
3484 goto out;
3485 }
3486
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003487 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3488 allocated_entry, &fragment_length, &length, ffa_version, true);
J-Alves33c47bf2022-09-29 11:36:20 +01003489 if (ret.func != FFA_SUCCESS_32) {
3490 goto out;
3491 }
3492
J-Alves8bc47f32025-03-21 10:23:51 +00003493 memory_region = allocated_entry;
3494
J-Alves33c47bf2022-09-29 11:36:20 +01003495 if (fragment_length < sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003496 memory_region->memory_access_desc_size) {
J-Alves33c47bf2022-09-29 11:36:20 +01003497 dlog_verbose(
3498 "Initial fragment length %d smaller than header size "
Karl Meakine8937d92024-03-19 16:04:25 +00003499 "%lu.\n",
J-Alves33c47bf2022-09-29 11:36:20 +01003500 fragment_length,
3501 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003502 memory_region->memory_access_desc_size);
3503 ret = ffa_error(FFA_INVALID_PARAMETERS);
3504 goto out;
J-Alves33c47bf2022-09-29 11:36:20 +01003505 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003506
J-Alves84658fc2021-06-17 14:37:32 +01003507 if (!api_memory_region_check_flags(memory_region, share_func)) {
3508 dlog_verbose(
3509 "Memory region reserved arguments must be zero.\n");
3510 ret = ffa_error(FFA_INVALID_PARAMETERS);
3511 goto out;
3512 }
3513
J-Alves363f5722022-04-25 17:37:37 +01003514 if (memory_region->receiver_count == 0U) {
3515 dlog_verbose("Receiver count can't be 0.\n");
3516 ret = ffa_error(FFA_INVALID_PARAMETERS);
3517 goto out;
3518 }
3519
J-Alves95fbb312024-03-20 15:19:16 +00003520 if ((share_func == FFA_MEM_DONATE_32 ||
3521 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01003522 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003523 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003524 "FFA_MEM_DONATE only supports one recipient. Specified "
3525 "%u\n",
J-Alves363f5722022-04-25 17:37:37 +01003526 memory_region->receiver_count);
3527 ret = ffa_error(FFA_INVALID_PARAMETERS);
3528 goto out;
3529 }
3530
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003531 /*
3532 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01003533 * If there is a receiver from the other world, track it for later
3534 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003535 */
J-Alves363f5722022-04-25 17:37:37 +01003536 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003537 struct ffa_memory_access *receiver =
3538 ffa_memory_region_get_receiver(memory_region, i);
J-Alvesc9227c82024-04-24 21:00:58 +01003539 ffa_id_t receiver_id;
3540
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003541 assert(receiver != NULL);
J-Alvesc9227c82024-04-24 21:00:58 +01003542
3543 receiver_id = receiver->receiver_permissions.receiver;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003544
J-Alves363f5722022-04-25 17:37:37 +01003545 to = vm_find(receiver_id);
3546
J-Alves7de61af2024-03-27 10:29:46 +00003547 if ((vm_id_is_current_world(receiver_id) && to == NULL) ||
3548 to == from) {
J-Alves363f5722022-04-25 17:37:37 +01003549 dlog_verbose("%s: invalid receiver.\n", __func__);
3550 ret = ffa_error(FFA_INVALID_PARAMETERS);
3551 goto out;
3552 }
3553
Karl Meakin117c8082024-12-04 16:03:28 +00003554 if (!ffa_memory_is_send_valid(
J-Alvesc9227c82024-04-24 21:00:58 +01003555 receiver_id, from->id, share_func,
3556 memory_region->receiver_count > 1)) {
J-Alves363f5722022-04-25 17:37:37 +01003557 ret = ffa_error(FFA_DENIED);
3558 goto out;
3559 }
3560
3561 /* Capture if any of the receivers is from the other world. */
3562 if (!targets_other_world) {
3563 targets_other_world =
3564 !vm_id_is_current_world(receiver_id);
3565 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003566 }
3567
J-Alves363f5722022-04-25 17:37:37 +01003568 if (targets_other_world) {
Karl Meakin117c8082024-12-04 16:03:28 +00003569 ret = ffa_memory_other_world_mem_send(
J-Alves66652252022-07-06 09:49:51 +01003570 from, share_func, &memory_region, length,
3571 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003572 } else {
3573 struct vm_locked from_locked = vm_lock(from);
3574
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003575 ret = ffa_memory_send(from_locked, memory_region, length,
3576 fragment_length, share_func,
3577 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003578 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003579 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003580 * make sure we don't free it.
3581 */
3582 memory_region = NULL;
3583
3584 vm_unlock(&from_locked);
3585 }
3586
3587out:
3588 if (memory_region != NULL) {
3589 mpool_free(&api_page_pool, memory_region);
3590 }
3591
3592 return ret;
3593}
3594
J-Alvesc7270b72024-09-12 10:16:34 +01003595/**
3596 * An FFA_MEM_RETRIEVE_REQ from the hypervisor must specify the handle of the
3597 * memory transaction it is querying and all other fields must be 0.
3598 */
3599static bool api_ffa_memory_hypervisor_retrieve_request_validate(
3600 struct ffa_memory_region *request, enum ffa_version version)
3601{
3602 switch (version) {
3603 case FFA_VERSION_1_0: {
3604 struct ffa_memory_region_v1_0 *request_v1_0 =
3605 (struct ffa_memory_region_v1_0 *)request;
3606
3607 return request_v1_0->sender == 0U &&
3608 request_v1_0->attributes.shareability == 0U &&
3609 request_v1_0->attributes.cacheability == 0U &&
3610 request_v1_0->attributes.type == 0U &&
3611 request_v1_0->attributes.security == 0U &&
3612 request_v1_0->flags == 0U && request_v1_0->tag == 0U &&
3613 request_v1_0->receiver_count == 0U &&
Karl Meakin117c8082024-12-04 16:03:28 +00003614 ffa_memory_is_handle_allocated_by_current_world(
J-Alvesc7270b72024-09-12 10:16:34 +01003615 request_v1_0->handle);
3616 }
3617 default:
3618 return request->sender == 0U &&
3619 request->attributes.shareability == 0U &&
3620 request->attributes.cacheability == 0U &&
3621 request->attributes.type == 0U &&
3622 request->attributes.security == 0U &&
3623 request->flags == 0U && request->tag == 0U &&
3624 request->memory_access_desc_size == 0U &&
3625 request->receiver_count == 0U &&
3626 request->receivers_offset == 0U &&
Karl Meakin117c8082024-12-04 16:03:28 +00003627 ffa_memory_is_handle_allocated_by_current_world(
J-Alvesc7270b72024-09-12 10:16:34 +01003628 request->handle);
3629 }
3630}
3631
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003632struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3633 uint32_t fragment_length,
3634 ipaddr_t address, uint32_t page_count,
3635 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003636{
3637 struct vm *to = current->vm;
3638 struct vm_locked to_locked;
3639 const void *to_msg;
J-Alves00847062022-10-03 17:08:44 +01003640 void *retrieve_msg;
3641 struct ffa_memory_region *retrieve_request = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003642 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003643 struct ffa_value ret;
Karl Meakin0e617d92024-04-05 12:55:22 +01003644 enum ffa_version ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003645
3646 if (ipa_addr(address) != 0 || page_count != 0) {
3647 /*
3648 * Hafnium only supports passing the descriptor in the TX
3649 * mailbox.
3650 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003651 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003652 }
3653
Andrew Walbrana65a1322020-04-06 19:32:32 +01003654 if (fragment_length != length) {
J-Alves33c47bf2022-09-29 11:36:20 +01003655 dlog_verbose("Fragmentation not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003656 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003657 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003658
J-Alves00847062022-10-03 17:08:44 +01003659 retrieve_msg = cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003660 message_buffer_size = cpu_get_buffer_size(current->cpu);
3661 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3662 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003663 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003664 }
3665
3666 to_locked = vm_lock(to);
3667 to_msg = to->mailbox.send;
J-Alves00847062022-10-03 17:08:44 +01003668 ffa_version = to->ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003669
3670 if (to_msg == NULL) {
3671 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003672 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003673 goto out;
3674 }
3675
3676 /*
3677 * Copy the retrieve request descriptor to an internal buffer, so that
3678 * the caller can't change it underneath us.
3679 */
J-Alves8f2150d2024-03-28 16:15:56 +00003680 if (!memcpy_trapped(retrieve_msg, message_buffer_size, to_msg,
3681 length)) {
3682 dlog_error(
3683 "%s: Failed to copy FF-A retrieve request "
3684 "descriptor.\n",
3685 __func__);
3686 ret = ffa_error(FFA_ABORTED);
3687 goto out;
3688 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003689
J-Alvese8c8c2b2022-12-16 15:34:48 +00003690 if ((vm_is_mailbox_other_world_owned(to_locked) &&
Karl Meakin117c8082024-12-04 16:03:28 +00003691 !ffa_setup_acquire_receiver_rx(to_locked, &ret)) ||
J-Alvese8c8c2b2022-12-16 15:34:48 +00003692 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003693 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003694 * Can't retrieve memory information if the mailbox is
3695 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003696 */
J-Alves59ed0042022-07-28 18:26:41 +01003697 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003698 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003699 goto out;
3700 }
3701
Daniel Boulby296ee702023-11-28 13:36:55 +00003702 if (!is_ffa_hypervisor_retrieve_request(retrieve_msg)) {
J-Alves8bc47f32025-03-21 10:23:51 +00003703 /*
3704 * The checks from function below should guarantee there are no
3705 * invalid values, and the accesses that follow can't be out of
3706 * bounds.
3707 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003708 if (!ffa_memory_region_sanity_check(retrieve_msg, ffa_version,
3709 fragment_length, false)) {
3710 ret = ffa_error(FFA_INVALID_PARAMETERS);
3711 goto out;
3712 }
3713 /*
3714 * If required, transform the retrieve request to FF-A v1.1.
3715 */
3716 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3717 retrieve_msg, &fragment_length, &length, ffa_version,
3718 false);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003719
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003720 if (ret.func != FFA_SUCCESS_32) {
3721 goto out;
3722 }
J-Alvesc7270b72024-09-12 10:16:34 +01003723 } else {
3724 if (!api_ffa_memory_hypervisor_retrieve_request_validate(
3725 retrieve_msg, ffa_version)) {
3726 dlog_verbose(
3727 "All fields except the handle in the "
3728 "memory access descriptor must be zero for a "
3729 "hypervisor retrieve request.\n");
3730 ret = ffa_error(FFA_INVALID_PARAMETERS);
3731 goto out;
3732 }
J-Alves00847062022-10-03 17:08:44 +01003733 }
3734
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003735 retrieve_request = retrieve_msg;
Kathleen Capella578784f2023-09-01 18:03:27 -04003736
Karl Meakin117c8082024-12-04 16:03:28 +00003737 if (ffa_memory_is_handle_allocated_by_current_world(
J-Alvesb5084cf2022-07-06 14:20:12 +01003738 retrieve_request->handle)) {
3739 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3740 &api_page_pool);
3741 } else {
Daniel Boulbyfdd59f72023-10-19 16:43:19 +01003742 dlog_error("Invalid FF-A memory handle.\n");
3743 ret = ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesb5084cf2022-07-06 14:20:12 +01003744 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003745out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003746 vm_unlock(&to_locked);
3747 return ret;
3748}
3749
J-Alvesa39a8442024-04-26 15:41:47 +01003750/**
3751 * Copies the memory relinquish descriptor from the partition's TX buffer, to
3752 * the buffer of the local CPU. Do it safely, and return error if:
3753 * - FFA_ABORTED: if the `memcpy_trapped` fails.
3754 * - FFA_INVALID_PARAMETERS: if the size of the full memory relinquish
3755 * descriptor doesn't fit the local CPU buffer.
3756 *
3757 * Returns FFA_SUCCESS if copying goes well, and sets 'out_relinquish'
3758 * to the address of the cpu buffer with the relinquish descriptor.
3759 */
3760static struct ffa_value api_get_ffa_mem_relinquish_descriptor(
3761 struct vcpu *current, const void *from_msg,
3762 struct ffa_mem_relinquish **out_relinquish)
3763{
3764 struct ffa_mem_relinquish *relinquish_request;
3765 uint32_t from_msg_size;
3766 uint32_t total_from_msg_size;
3767 uint32_t dst_size;
3768 vaddr_t dst;
3769 vaddr_t src;
3770
3771 assert(from_msg != NULL);
3772 assert(out_relinquish != NULL);
3773
3774 /*
3775 * Copy the relinquish descriptor to an internal buffer, so that the
3776 * caller can't change it underneath us.
3777 */
3778 relinquish_request =
3779 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
3780
3781 /* Set the destination for the copy. */
3782 dst = va_from_ptr(relinquish_request);
3783 src = va_from_ptr(from_msg);
3784
3785 dst_size = cpu_get_buffer_size(current->cpu);
3786
3787 /* Only copy the size to start with. */
3788 from_msg_size = sizeof(struct ffa_mem_relinquish);
3789 total_from_msg_size = from_msg_size;
3790
3791 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3792 from_msg_size)) {
3793 dlog_error(
3794 "%s: Failed to copy FF-A memory relinquish "
3795 "descriptor.\n",
3796 __func__);
3797 return ffa_error(FFA_ABORTED);
3798 }
3799
3800 if (relinquish_request->endpoint_count != 1) {
3801 dlog_error("%s: relinquish descriptor must have 1 endpoint\n",
3802 __func__);
3803 return ffa_error(FFA_INVALID_PARAMETERS);
3804 }
3805
3806 /*
3807 * Increment the `dst` to the position right after the copied header.
3808 * Increment the `src` to point at the list of endpoints.
3809 *
3810 * Calculate the new `dst_size` which is the size of the allocated cpu
3811 * buffer, minus the size of the copied memory relinquish header.
3812 *
3813 * Only after the above: determine the new `from_msg_size` in accordance
3814 * to the endpoint count.
3815 */
3816 dst = va_add(dst, from_msg_size);
3817 src = va_add(src, from_msg_size);
3818
3819 /*
3820 * Check if it is safe to copy the rest of the message.
3821 * This also serves as a santiy check to 'endpoint_count'.
3822 * The size of what is left in the descriptor, based on endpoint_count,
3823 * shall not be bigger than the size of the mailbox minus the size of
3824 * the header which was previously copied in this function.
3825 */
3826 dst_size -= from_msg_size;
3827 from_msg_size = relinquish_request->endpoint_count * sizeof(ffa_id_t);
3828 total_from_msg_size += from_msg_size;
3829
3830 if (total_from_msg_size > HF_MAILBOX_SIZE ||
3831 total_from_msg_size > dst_size) {
3832 dlog_verbose(
3833 "Relinquish message too long. Endpoint count: %u\n",
3834 relinquish_request->endpoint_count);
3835 return ffa_error(FFA_INVALID_PARAMETERS);
3836 }
3837
3838 /* Copy the remaining fragment. */
3839 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3840 from_msg_size)) {
3841 dlog_error("%s: Failed to copy FF-A relinquish request.\n",
3842 __func__);
3843 return ffa_error(FFA_ABORTED);
3844 }
3845
3846 /*
3847 * Set the output address for the relinquish descriptor to the current
3848 * cpu's buffer.
3849 */
3850 *out_relinquish = relinquish_request;
3851
3852 return (struct ffa_value){.func = FFA_SUCCESS_32};
3853}
3854
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003855struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003856{
3857 struct vm *from = current->vm;
3858 struct vm_locked from_locked;
3859 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003860 struct ffa_value ret;
J-Alvesa39a8442024-04-26 15:41:47 +01003861 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003862
3863 from_locked = vm_lock(from);
3864 from_msg = from->mailbox.send;
3865
3866 if (from_msg == NULL) {
3867 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003868 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003869 goto out;
3870 }
3871
J-Alvesa39a8442024-04-26 15:41:47 +01003872 ret = api_get_ffa_mem_relinquish_descriptor(current, from_msg,
3873 &relinquish_request);
3874
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003875 /*
J-Alvesa39a8442024-04-26 15:41:47 +01003876 * If the descriptor was safely copied, continue with the handling of
3877 * the retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003878 */
J-Alvesa39a8442024-04-26 15:41:47 +01003879 if (ret.func == FFA_SUCCESS_32) {
3880 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3881 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003882 }
J-Alves8f2150d2024-03-28 16:15:56 +00003883
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003884out:
3885 vm_unlock(&from_locked);
3886 return ret;
3887}
3888
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003889struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3890 ffa_memory_region_flags_t flags,
3891 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003892{
3893 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003894 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003895
Karl Meakin117c8082024-12-04 16:03:28 +00003896 if (ffa_memory_is_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003897 struct vm_locked to_locked = vm_lock(to);
3898
Andrew Walbranca808b12020-05-15 17:22:28 +01003899 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003900 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003901
Andrew Walbran290b0c92020-02-03 16:37:14 +00003902 vm_unlock(&to_locked);
3903 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00003904 ret = ffa_memory_other_world_mem_reclaim(to, handle, flags,
3905 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003906 }
3907
3908 return ret;
3909}
3910
3911struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
3912 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003913 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003914 struct vcpu *current)
3915{
3916 struct vm *to = current->vm;
3917 struct vm_locked to_locked;
3918 struct ffa_value ret;
3919
3920 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01003921 if (vm_id_is_current_world(to->id)) {
3922 if (sender_vm_id != 0) {
3923 dlog_verbose("%s: Invalid sender.\n", __func__);
3924 return ffa_error(FFA_INVALID_PARAMETERS);
3925 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003926 }
3927
3928 to_locked = vm_lock(to);
3929
J-Alves122f1a12022-12-12 15:55:42 +00003930 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003931 /*
3932 * Can't retrieve memory information if the mailbox is not
3933 * available.
3934 */
J-Alves59ed0042022-07-28 18:26:41 +01003935 dlog_verbose("%s: RX buffer not ready partition %x.\n",
3936 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003937 ret = ffa_error(FFA_BUSY);
3938 goto out;
3939 }
3940
J-Alvesc3fd9752024-04-04 11:45:33 +01003941 /*
3942 * Whilst copying the fragments, initialize the remaining constituents
3943 * in the CPU's internal structure, and later copy from the CPU buffer
3944 * into the partition's RX buffer. In the case SPMC is doing a retrieve
3945 * request for a VM/Hypervisor in an RME enabled system, there is no
3946 * guarantee the RX buffer is in the NS PAS. Accessing the buffer with
3947 * the wrong security state attribute would then result in an GPF.
3948 * The fragment is initialized in an internal buffer, and is later
3949 * copied to the RX buffer using the 'memcpy_trapped' which allows to
3950 * smoothly terminate the operation if the access has been preempted by
3951 * a GPF exception.
3952 */
3953 ret = ffa_memory_retrieve_continue(
3954 to_locked, handle, fragment_offset, sender_vm_id,
3955 cpu_get_buffer(current->cpu), &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003956out:
3957 vm_unlock(&to_locked);
3958 return ret;
3959}
3960
3961struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
3962 uint32_t fragment_length,
J-Alves19e20cf2023-08-02 12:48:55 +01003963 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003964 struct vcpu *current)
3965{
3966 struct vm *from = current->vm;
3967 const void *from_msg;
3968 void *fragment_copy;
3969 struct ffa_value ret;
3970
3971 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01003972 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
3973 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01003974 return ffa_error(FFA_INVALID_PARAMETERS);
3975 }
3976
3977 /*
3978 * Check that the sender has configured its send buffer. If the TX
3979 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3980 * be safely accessed after releasing the lock since the TX mailbox
3981 * address can only be configured once.
3982 */
3983 sl_lock(&from->lock);
3984 from_msg = from->mailbox.send;
3985 sl_unlock(&from->lock);
3986
3987 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01003988 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003989 return ffa_error(FFA_INVALID_PARAMETERS);
3990 }
3991
3992 /*
3993 * Copy the fragment to a fresh page from the memory pool. This prevents
3994 * the sender from changing it underneath us, and also lets us keep it
3995 * around in the share state table if needed.
3996 */
3997 if (fragment_length > HF_MAILBOX_SIZE ||
3998 fragment_length > MM_PPOOL_ENTRY_SIZE) {
3999 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004000 "Fragment length %d larger than mailbox size %zu.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01004001 fragment_length, HF_MAILBOX_SIZE);
4002 return ffa_error(FFA_INVALID_PARAMETERS);
4003 }
4004 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
4005 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
4006 0) {
4007 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
4008 return ffa_error(FFA_INVALID_PARAMETERS);
4009 }
4010 fragment_copy = mpool_alloc(&api_page_pool);
4011 if (fragment_copy == NULL) {
4012 dlog_verbose("Failed to allocate fragment copy.\n");
4013 return ffa_error(FFA_NO_MEMORY);
4014 }
J-Alves8f2150d2024-03-28 16:15:56 +00004015
4016 if (!memcpy_trapped(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg,
4017 fragment_length)) {
4018 dlog_error("%s: Failed to copy fragment.\n", __func__);
4019 return ffa_error(FFA_ABORTED);
4020 }
Andrew Walbranca808b12020-05-15 17:22:28 +01004021
4022 /*
4023 * Hafnium doesn't support fragmentation of memory retrieve requests
4024 * (because it doesn't support caller-specified mappings, so a request
4025 * will never be larger than a single page), so this must be part of a
4026 * memory send (i.e. donate, lend or share) request.
4027 *
4028 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01004029 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01004030 */
Karl Meakin117c8082024-12-04 16:03:28 +00004031 if (ffa_memory_is_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004032 struct vm_locked from_locked = vm_lock(from);
4033
4034 ret = ffa_memory_send_continue(from_locked, fragment_copy,
4035 fragment_length, handle,
4036 &api_page_pool);
4037 /*
4038 * `ffa_memory_send_continue` takes ownership of the
4039 * fragment_copy, so we don't need to free it here.
4040 */
4041 vm_unlock(&from_locked);
4042 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00004043 ret = ffa_memory_other_world_mem_send_continue(
J-Alvesfdd29272022-07-19 13:16:31 +01004044 from, fragment_copy, fragment_length, handle,
4045 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00004046 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00004047
4048 return ret;
4049}
Max Shvetsov40108e72020-08-27 12:39:50 +01004050
Olivier Deprezd614d322021-06-18 15:21:00 +02004051/**
4052 * Register an entry point for a vCPU in warm boot cases.
4053 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
4054 */
Max Shvetsov40108e72020-08-27 12:39:50 +01004055struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
4056 struct vcpu *current)
4057{
4058 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01004059 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01004060
Olivier Deprezd614d322021-06-18 15:21:00 +02004061 /*
4062 * Reject if interface is not supported at this FF-A instance
4063 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
4064 */
Karl Meakin117c8082024-12-04 16:03:28 +00004065 if (!ffa_setup_is_secondary_ep_register_supported() ||
Karl Meakin82041822024-05-20 11:14:34 +01004066 vm_is_up(current->vm)) {
Olivier Deprezd614d322021-06-18 15:21:00 +02004067 return ffa_error(FFA_NOT_SUPPORTED);
4068 }
4069
4070 /*
4071 * No further check is made on the address validity
4072 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
4073 * from the VM or vCPU structure.
4074 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
4075 * For each SP [...] the Framework assumes that the same entry point
4076 * address is used for initializing any execution context during a
4077 * secondary cold boot.
4078 * If this function is invoked multiple times, then the entry point
4079 * address specified in the last valid invocation must be used by the
4080 * callee.
4081 */
Olivier Deprezb2808332023-02-02 15:25:40 +01004082 current_locked = vcpu_lock(current);
4083 if (current->rt_model != RTM_SP_INIT) {
4084 dlog_error(
4085 "FFA_SECONDARY_EP_REGISTER can only be called while "
4086 "vCPU in run-time state for initialization.\n");
4087 vcpu_unlock(&current_locked);
4088 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02004089 }
Olivier Deprezb2808332023-02-02 15:25:40 +01004090 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02004091
Olivier Deprezb2808332023-02-02 15:25:40 +01004092 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01004093 vm_locked.vm->secondary_ep = entry_point;
4094 vm_unlock(&vm_locked);
4095
Olivier Deprezb2808332023-02-02 15:25:40 +01004096 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01004097}
J-Alvesa0f317d2021-06-09 13:31:59 +01004098
J-Alves19e20cf2023-08-02 12:48:55 +01004099struct ffa_value api_ffa_notification_bitmap_create(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004100 ffa_vcpu_count_t vcpu_count,
4101 struct vcpu *current)
4102{
J-Alves7ccaccf2024-02-01 14:25:23 +00004103 const struct ffa_value ret =
Karl Meakin117c8082024-12-04 16:03:28 +00004104 ffa_notifications_is_bitmap_access_valid(current, vm_id);
J-Alves7ccaccf2024-02-01 14:25:23 +00004105
4106 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4107 dlog_verbose(
4108 "FFA_NOTIFICATION_BITMAP_CREATE to be used by "
4109 "hypervisor for valid NWd VM IDs only (%x).\n",
4110 vm_id);
4111 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004112 }
4113
Karl Meakin117c8082024-12-04 16:03:28 +00004114 return ffa_notifications_bitmap_create(vm_id, vcpu_count);
J-Alvesa0f317d2021-06-09 13:31:59 +01004115}
4116
J-Alves19e20cf2023-08-02 12:48:55 +01004117struct ffa_value api_ffa_notification_bitmap_destroy(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004118 struct vcpu *current)
4119{
J-Alves7ccaccf2024-02-01 14:25:23 +00004120 const struct ffa_value ret =
Karl Meakin117c8082024-12-04 16:03:28 +00004121 ffa_notifications_is_bitmap_access_valid(current, vm_id);
J-Alves7ccaccf2024-02-01 14:25:23 +00004122
4123 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4124 dlog_verbose(
4125 "FFA_NOTIFICATION_BITMAP_DESTROY to be used by "
4126 "hypervisor for valid NWd VM IDs only (%x).\n",
4127 vm_id);
4128 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004129 }
4130
Karl Meakin117c8082024-12-04 16:03:28 +00004131 return ffa_notifications_bitmap_destroy(vm_id);
J-Alvesa0f317d2021-06-09 13:31:59 +01004132}
J-Alvesc003a7a2021-03-18 13:06:53 +00004133
4134struct ffa_value api_ffa_notification_update_bindings(
J-Alves19e20cf2023-08-02 12:48:55 +01004135 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesc003a7a2021-03-18 13:06:53 +00004136 ffa_notifications_bitmap_t notifications, bool is_bind,
4137 struct vcpu *current)
4138{
4139 struct ffa_value ret = {.func = FFA_SUCCESS_32};
4140 struct vm_locked receiver_locked;
4141 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
J-Alves19e20cf2023-08-02 12:48:55 +01004142 const ffa_id_t id_to_update = is_bind ? sender_vm_id : HF_INVALID_VM_ID;
4143 const ffa_id_t id_to_validate =
J-Alvesc003a7a2021-03-18 13:06:53 +00004144 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01004145 const uint32_t flags_mbz =
4146 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
4147
4148 if ((flags_mbz & flags) != 0U) {
4149 return ffa_error(FFA_INVALID_PARAMETERS);
4150 }
J-Alvesc003a7a2021-03-18 13:06:53 +00004151
Karl Meakin117c8082024-12-04 16:03:28 +00004152 if (!ffa_notifications_is_bind_valid(current, sender_vm_id,
4153 receiver_vm_id)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004154 dlog_verbose("Invalid use of notifications bind interface.\n");
4155 return ffa_error(FFA_INVALID_PARAMETERS);
4156 }
4157
Karl Meakin117c8082024-12-04 16:03:28 +00004158 if (ffa_notifications_update_bindings_forward(
J-Alvesb15e9402021-09-08 11:44:42 +01004159 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
4160 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01004161 return ret;
4162 }
4163
J-Alvesc003a7a2021-03-18 13:06:53 +00004164 if (notifications == 0U) {
Karl Meakine8937d92024-03-19 16:04:25 +00004165 dlog_verbose("No notifications have been specified %lx.\n",
J-Alves1512acc2024-02-01 16:57:10 +00004166 notifications);
J-Alvesc003a7a2021-03-18 13:06:53 +00004167 return ffa_error(FFA_INVALID_PARAMETERS);
4168 }
4169
4170 /**
4171 * This check assumes receiver is the current VM, and has been enforced
Karl Meakin117c8082024-12-04 16:03:28 +00004172 * by 'ffa_notifications_is_bind_valid'.
J-Alvesc003a7a2021-03-18 13:06:53 +00004173 */
Karl Meakin117c8082024-12-04 16:03:28 +00004174 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004175
4176 if (receiver_locked.vm == NULL) {
4177 dlog_verbose("Receiver doesn't exist!\n");
J-Alves7333dcf2024-02-01 17:25:10 +00004178 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesc003a7a2021-03-18 13:06:53 +00004179 }
4180
J-Alves3e55c4d2024-10-09 14:24:07 +01004181 if (receiver_locked.vm->ffa_version < FFA_VERSION_1_1) {
4182 dlog_verbose(
4183 "%s: caller (%x) version should be GE to FF-A v1.1.\n",
4184 __func__, receiver_locked.vm->id);
4185 ret = ffa_error(FFA_NOT_SUPPORTED);
4186 goto out;
4187 }
4188
J-Alves09ff9d82021-11-02 11:55:20 +00004189 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004190 dlog_verbose("Notifications are not enabled.\n");
4191 ret = ffa_error(FFA_NOT_SUPPORTED);
4192 goto out;
4193 }
4194
4195 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
4196 vm_find(sender_vm_id) == NULL) {
4197 dlog_verbose("Sender VM does not exist!\n");
4198 ret = ffa_error(FFA_INVALID_PARAMETERS);
4199 goto out;
4200 }
4201
4202 /*
4203 * Can't bind/unbind notifications if at least one is bound to a
4204 * different sender.
4205 */
4206 if (!vm_notifications_validate_bound_sender(
J-Alves661e1b72023-08-02 13:39:40 +01004207 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_validate,
4208 notifications)) {
J-Alvesf0208142024-02-01 17:28:58 +00004209 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004210 "Sender %x not permitted to set notifications %lx to "
J-Alvesf0208142024-02-01 17:28:58 +00004211 "%x.\n",
4212 sender_vm_id, notifications, receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004213 ret = ffa_error(FFA_DENIED);
4214 goto out;
4215 }
4216
4217 /**
4218 * Check if there is a pending notification within those specified in
4219 * the bitmap.
4220 */
4221 if (vm_are_notifications_pending(receiver_locked,
J-Alves661e1b72023-08-02 13:39:40 +01004222 ffa_is_vm_id(sender_vm_id),
J-Alvesc003a7a2021-03-18 13:06:53 +00004223 notifications)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004224 dlog_verbose("Notifications within '%lx' pending.\n",
J-Alvesc003a7a2021-03-18 13:06:53 +00004225 notifications);
4226 ret = ffa_error(FFA_DENIED);
4227 goto out;
4228 }
4229
4230 vm_notifications_update_bindings(
J-Alves661e1b72023-08-02 13:39:40 +01004231 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_update,
J-Alvesc003a7a2021-03-18 13:06:53 +00004232 notifications, is_per_vcpu && is_bind);
4233
4234out:
4235 vm_unlock(&receiver_locked);
4236 return ret;
4237}
J-Alvesaa79c012021-07-09 14:29:45 +01004238
4239struct ffa_value api_ffa_notification_set(
J-Alves19e20cf2023-08-02 12:48:55 +01004240 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesaa79c012021-07-09 14:29:45 +01004241 ffa_notifications_bitmap_t notifications, struct vcpu *current)
4242{
4243 struct ffa_value ret;
4244 struct vm_locked receiver_locked;
J-Alvesaa79c012021-07-09 14:29:45 +01004245 /*
4246 * Check if is per-vCPU or global, and extracting vCPU ID according
4247 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
4248 */
4249 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
4250 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
J-Alves6b754a12024-02-14 14:26:30 +00004251 const uint32_t flags_mbz =
4252 ~(FFA_NOTIFICATIONS_FLAG_PER_VCPU |
4253 FFA_NOTIFICATIONS_FLAG_DELAY_SRI | (0xFFFFU << 16));
J-Alves41c5da32024-06-19 10:05:05 +01004254 const bool delay_sri = (FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) != 0U;
J-Alves6b754a12024-02-14 14:26:30 +00004255
4256 if ((flags_mbz & flags) != 0U) {
4257 dlog_verbose("%s: caller shouldn't set bits that MBZ.\n",
4258 __func__);
4259 return ffa_error(FFA_INVALID_PARAMETERS);
4260 }
J-Alvesaa79c012021-07-09 14:29:45 +01004261
J-Alves30ac91d2024-02-14 15:33:12 +00004262 /* Global notifications must target any vCPU. */
4263 if (!is_per_vcpu && vcpu_id != 0U) {
4264 dlog_verbose(
4265 "For global notifications vCPU ID MBZ in call to set "
4266 "notifications.\n");
4267 return ffa_error(FFA_INVALID_PARAMETERS);
4268 }
4269
Karl Meakin117c8082024-12-04 16:03:28 +00004270 if (!ffa_notifications_is_set_valid(current, sender_vm_id,
4271 receiver_vm_id)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004272 dlog_verbose("Invalid use of notifications set interface.\n");
4273 return ffa_error(FFA_INVALID_PARAMETERS);
4274 }
4275
4276 if (notifications == 0U) {
4277 dlog_verbose("No notifications have been specified.\n");
4278 return ffa_error(FFA_INVALID_PARAMETERS);
4279 }
4280
J-Alves41c5da32024-06-19 10:05:05 +01004281 /*
4282 * The 'Delay Schedule Receiver interrupt flag' only applies to the
4283 * secure virtual FF-A instance.
4284 */
4285 if (!vm_id_is_current_world(sender_vm_id) && delay_sri) {
4286 dlog_verbose(
4287 "The delay SRI flag can only be set at the secure "
4288 "virtual FF-A instance.\n");
4289 return ffa_error(FFA_INVALID_PARAMETERS);
4290 }
4291
Karl Meakin117c8082024-12-04 16:03:28 +00004292 if (ffa_notifications_set_forward(sender_vm_id, receiver_vm_id, flags,
4293 notifications, &ret)) {
J-Alvesde7bd2f2021-09-09 19:54:35 +01004294 return ret;
4295 }
4296
J-Alvesaa79c012021-07-09 14:29:45 +01004297 /*
4298 * This check assumes receiver is the current VM, and has been enforced
Karl Meakin117c8082024-12-04 16:03:28 +00004299 * by 'ffa_notifications_is_set_valid'.
J-Alvesaa79c012021-07-09 14:29:45 +01004300 */
Karl Meakin117c8082024-12-04 16:03:28 +00004301 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesaa79c012021-07-09 14:29:45 +01004302
4303 if (receiver_locked.vm == NULL) {
4304 dlog_verbose("Receiver ID is not valid.\n");
4305 return ffa_error(FFA_INVALID_PARAMETERS);
4306 }
4307
J-Alves09ff9d82021-11-02 11:55:20 +00004308 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004309 dlog_verbose("Receiver's notifications not enabled.\n");
4310 ret = ffa_error(FFA_DENIED);
4311 goto out;
4312 }
4313
4314 /*
J-Alves89c22132024-02-14 15:44:41 +00004315 * Check the if the notifications are bound as global if per-vCPU flag
4316 * is set, or if they are bound as per-vCPU and caller setting as
4317 * global. In either case, return FFA_INVALID_PARAMETERS.
4318 */
4319 if (vm_notifications_validate_binding(
4320 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4321 notifications, !is_per_vcpu)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004322 dlog_verbose("Notifications in %lx are %s\n", notifications,
J-Alves89c22132024-02-14 15:44:41 +00004323 !is_per_vcpu ? "global" : "per-vCPU");
4324 ret = ffa_error(FFA_INVALID_PARAMETERS);
4325 goto out;
4326 }
4327
4328 /*
J-Alvesaa79c012021-07-09 14:29:45 +01004329 * If notifications are not bound to the sender, they wouldn't be
4330 * enabled either for the receiver.
4331 */
4332 if (!vm_notifications_validate_binding(
J-Alves661e1b72023-08-02 13:39:40 +01004333 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4334 notifications, is_per_vcpu)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004335 dlog_verbose("Notifications bindings not valid.\n");
4336 ret = ffa_error(FFA_DENIED);
4337 goto out;
4338 }
4339
4340 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
4341 dlog_verbose("Invalid VCPU ID!\n");
4342 ret = ffa_error(FFA_INVALID_PARAMETERS);
4343 goto out;
4344 }
4345
J-Alves7461ef22021-10-18 17:21:33 +01004346 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00004347 vm_notifications_partition_set_pending(
J-Alves661e1b72023-08-02 13:39:40 +01004348 receiver_locked, ffa_is_vm_id(sender_vm_id), notifications,
J-Alves5a16c962022-03-25 12:32:51 +00004349 vcpu_id, is_per_vcpu);
4350
Karl Meakine8937d92024-03-19 16:04:25 +00004351 dlog_verbose("Set the notifications: %lx.\n", notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004352
J-Alves41c5da32024-06-19 10:05:05 +01004353 if (!delay_sri) {
J-Alves13394022021-06-30 13:48:49 +01004354 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
4355 vcpu_index(current));
Karl Meakin117c8082024-12-04 16:03:28 +00004356 ffa_notifications_sri_trigger_not_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004357 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00004358 ffa_notifications_sri_set_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004359 }
J-Alvesaa79c012021-07-09 14:29:45 +01004360
J-Alves13394022021-06-30 13:48:49 +01004361 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01004362out:
4363 vm_unlock(&receiver_locked);
4364
4365 return ret;
4366}
4367
4368static struct ffa_value api_ffa_notification_get_success_return(
4369 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
4370 ffa_notifications_bitmap_t from_framework)
4371{
4372 return (struct ffa_value){
4373 .func = FFA_SUCCESS_32,
4374 .arg1 = 0U,
4375 .arg2 = (uint32_t)from_sp,
4376 .arg3 = (uint32_t)(from_sp >> 32),
4377 .arg4 = (uint32_t)from_vm,
4378 .arg5 = (uint32_t)(from_vm >> 32),
4379 .arg6 = (uint32_t)from_framework,
4380 .arg7 = (uint32_t)(from_framework >> 32),
4381 };
4382}
4383
J-Alves19e20cf2023-08-02 12:48:55 +01004384struct ffa_value api_ffa_notification_get(ffa_id_t receiver_vm_id,
J-Alvesaa79c012021-07-09 14:29:45 +01004385 ffa_vcpu_index_t vcpu_id,
4386 uint32_t flags, struct vcpu *current)
4387{
J-Alves663682a2022-03-25 13:56:51 +00004388 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01004389 ffa_notifications_bitmap_t sp_notifications = 0;
4390 ffa_notifications_bitmap_t vm_notifications = 0;
4391 struct vm_locked receiver_locked;
4392 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01004393 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
4394 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
4395 FFA_NOTIFICATION_FLAG_BITMAP_SP |
4396 FFA_NOTIFICATION_FLAG_BITMAP_VM);
4397
4398 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
4399 if ((flags & flags_mbz) != 0U) {
4400 dlog_verbose(
4401 "Invalid flags bit(s) set in notifications get. [31:4] "
4402 "MBZ(%x)\n",
4403 flags);
4404 return ffa_error(FFA_INVALID_PARAMETERS);
4405 }
J-Alvesaa79c012021-07-09 14:29:45 +01004406
4407 /*
J-Alvesfc95a302022-04-22 14:18:23 +01004408 * Following check should capture wrong uses of the interface,
4409 * depending on whether Hafnium is SPMC or hypervisor. On the
4410 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01004411 */
Karl Meakin117c8082024-12-04 16:03:28 +00004412 if (!ffa_notifications_is_get_valid(current, receiver_vm_id, flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004413 dlog_verbose("Invalid use of notifications get interface.\n");
4414 return ffa_error(FFA_INVALID_PARAMETERS);
4415 }
4416
4417 /*
4418 * This check assumes receiver is the current VM, and has been enforced
Karl Meakinfa1dcb82025-02-10 16:47:50 +00004419 * by `ffa_notifications_is_get_valid`.
J-Alvesaa79c012021-07-09 14:29:45 +01004420 */
Karl Meakin117c8082024-12-04 16:03:28 +00004421 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesaa79c012021-07-09 14:29:45 +01004422
4423 /*
Karl Meakinfa1dcb82025-02-10 16:47:50 +00004424 * `ffa_notifications_is_get_valid` ensures following is never
J-Alvesaa79c012021-07-09 14:29:45 +01004425 * true.
4426 */
4427 CHECK(receiver_locked.vm != NULL);
4428
J-Alves60458812024-03-22 11:57:10 +00004429 if (receiver_locked.vm->vcpu_count <= vcpu_id) {
J-Alves1abb3342022-01-05 11:59:10 +00004430 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004431 "Invalid VCPU ID %u. vcpu count %u current core: "
4432 "%zu!\n",
J-Alves1abb3342022-01-05 11:59:10 +00004433 vcpu_id, receiver_locked.vm->vcpu_count,
4434 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01004435 ret = ffa_error(FFA_INVALID_PARAMETERS);
4436 goto out;
4437 }
4438
4439 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
Karl Meakin117c8082024-12-04 16:03:28 +00004440 ret = ffa_notifications_get_from_sp(receiver_locked, vcpu_id,
4441 &sp_notifications);
Karl Meakinf9c73ce2024-07-30 17:37:13 +01004442 if (ret.func == FFA_ERROR_32) {
J-Alves98ff9562021-09-09 14:39:41 +01004443 dlog_verbose("Failed to get notifications from sps.");
4444 goto out;
4445 }
J-Alvesaa79c012021-07-09 14:29:45 +01004446 }
4447
4448 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00004449 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01004450 receiver_locked, true, vcpu_id);
4451 }
4452
J-Alvesd605a092022-03-28 14:20:48 +01004453 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
4454 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
Karl Meakin117c8082024-12-04 16:03:28 +00004455 ret = ffa_notifications_get_framework_notifications(
Karl Meakinf9c73ce2024-07-30 17:37:13 +01004456 receiver_locked, &framework_notifications, flags,
4457 vcpu_id);
4458 if (ret.func == FFA_ERROR_32) {
J-Alvesd605a092022-03-28 14:20:48 +01004459 dlog_verbose(
4460 "Failed to get notifications from "
4461 "framework.\n");
4462 goto out;
4463 }
4464 }
4465
J-Alves663682a2022-03-25 13:56:51 +00004466 ret = api_ffa_notification_get_success_return(
4467 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004468
4469out:
4470 vm_unlock(&receiver_locked);
4471
4472 return ret;
4473}
J-Alvesc8e8a222021-06-08 17:33:52 +01004474
4475/**
4476 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
4477 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
4478 */
4479static struct ffa_value api_ffa_notification_info_get_success_return(
4480 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01004481 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01004482{
4483 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
4484
4485 /*
4486 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
4487 * hold the list of ids.
4488 */
4489 memcpy_s(&ret.arg3,
4490 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
4491 sizeof(ids[0]) * ids_count);
4492
4493 /*
4494 * According to the spec x2 should have:
4495 * - Bit flagging if there are more notifications pending;
4496 * - The total number of elements (i.e. total list size);
4497 * - The number of VCPU IDs within each VM specific list.
4498 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004499 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
4500 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
4501 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01004502
4503 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
4504 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
4505
4506 for (unsigned int i = 0; i < lists_count; i++) {
4507 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
4508 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
4509 }
4510
4511 return ret;
4512}
4513
4514struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
4515{
4516 /*
4517 * Following set of variables should be populated with the return info.
4518 * At a successfull handling of this interface, they should be used
4519 * to populate the 'ret' structure in accordance to the table 17.29
4520 * of the FF-A v1.1 Beta0 specification.
4521 */
4522 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
4523 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
4524 uint32_t lists_count = 0;
4525 uint32_t ids_count = 0;
4526 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01004527 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004528
4529 /*
4530 * This interface can only be called at NS virtual/physical FF-A
4531 * instance by the endpoint implementing the primary scheduler and the
4532 * Hypervisor/OS kernel.
4533 * In the SPM, following check passes if call has been forwarded from
4534 * the hypervisor.
4535 */
Karl Meakin5e996992024-05-20 11:27:07 +01004536
4537 if (!vm_is_primary(current->vm)) {
J-Alvesc8e8a222021-06-08 17:33:52 +01004538 dlog_verbose(
4539 "Only the receiver's scheduler can use this "
4540 "interface\n");
4541 return ffa_error(FFA_NOT_SUPPORTED);
4542 }
4543
J-Alvesca058c22021-09-10 14:02:07 +01004544 /*
4545 * Forward call to the other world, and fill the arrays used to assemble
4546 * return.
4547 */
Karl Meakin117c8082024-12-04 16:03:28 +00004548 ffa_notifications_info_get_forward(ids, &ids_count, lists_sizes,
4549 &lists_count,
4550 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
J-Alvesca058c22021-09-10 14:02:07 +01004551
4552 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
4553
J-Alvesc8e8a222021-06-08 17:33:52 +01004554 /* Get notifications' info from this world */
4555 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
4556 ++index) {
4557 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
4558
4559 list_is_full = vm_notifications_info_get(
4560 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
4561 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4562
4563 vm_unlock(&vm_locked);
4564 }
4565
4566 if (!list_is_full) {
4567 /* Grab notifications info from other world */
Karl Meakin117c8082024-12-04 16:03:28 +00004568 ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01004569 ids, &ids_count, lists_sizes, &lists_count,
4570 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4571 }
4572
4573 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01004574 dlog_verbose(
4575 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01004576 result = ffa_error(FFA_NO_DATA);
4577 } else {
4578 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01004579 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01004580 }
4581
J-Alves13394022021-06-30 13:48:49 +01004582 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004583}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004584
Karl Meakin04f86c92025-03-11 15:47:29 +00004585/*
4586 * Calculate the end of the memory range (`base_addr + page_count * PAGE_SIZE`)
4587 * and write the result to `*res`.
4588 * Returns whether any of the intermediate operations overflowed.
4589 */
4590static bool api_memory_range_end(vaddr_t base_addr, uint32_t page_count,
4591 vaddr_t *res)
4592{
4593 uint64_t range_size;
4594 uintvaddr_t end_addr;
4595
4596 if (mul_overflow(page_count, PAGE_SIZE, &range_size)) {
4597 return true;
4598 }
4599
4600 if (add_overflow(va_addr(base_addr), range_size, &end_addr)) {
4601 return true;
4602 }
4603
4604 *res = va_init(end_addr);
4605 return false;
4606}
4607
Karl Meakined91e992025-02-01 16:16:27 +00004608struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, uint32_t page_count,
4609 struct vcpu *current)
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004610{
4611 struct vm_locked vm_locked;
Karl Meakincdecdba2025-02-01 15:36:58 +00004612 struct ffa_value ret;
4613 bool mode_ret;
4614 uint32_t mode;
Karl Meakined91e992025-02-01 16:16:27 +00004615 vaddr_t end_addr;
4616
4617 /**
4618 * The size of the memory region is calculated as (page_count + 1) *
4619 * granule size to ensure backwards compatability: v1.2 or earlier
4620 * callers, who leave `arg2` as 0, will get the correct behaviour
4621 * (querying a single page).
Karl Meakin04f86c92025-03-11 15:47:29 +00004622 *
4623 * Any overflow will be caught by the check against zero.
Karl Meakined91e992025-02-01 16:16:27 +00004624 */
4625 page_count += 1;
Karl Meakin04f86c92025-03-11 15:47:29 +00004626
4627 /* Empty ranges should be disallowed, as should ranges that overflow */
4628 if (page_count == 0) {
4629 dlog_error("FFA_MEM_PERM_GET: page_count was zero\n");
4630 return ffa_error(FFA_INVALID_PARAMETERS);
4631 }
4632
4633 if (api_memory_range_end(base_addr, page_count, &end_addr)) {
4634 dlog_error("FFA_MEM_PERM_GET: overflow calculating end_addr\n");
4635 return ffa_error(FFA_INVALID_PARAMETERS);
4636 }
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004637
Karl Meakin117c8082024-12-04 16:03:28 +00004638 if (!ffa_memory_is_mem_perm_get_valid(current)) {
Karl Meakincdecdba2025-02-01 15:36:58 +00004639 dlog_error("FFA_MEM_PERM_GET: not allowed\n");
J-Alves5a099172024-02-22 14:34:51 +00004640 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004641 }
4642
Karl Meakincdecdba2025-02-01 15:36:58 +00004643 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
4644 dlog_error(
4645 "FFA_MEM_PERM_GET: base addr %#016lx is not page "
4646 "aligned\n",
4647 va_addr(base_addr));
4648 return ffa_error(FFA_INVALID_PARAMETERS);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004649 }
4650
4651 vm_locked = vm_lock(current->vm);
4652
4653 /*
Karl Meakin9f568582025-02-20 17:12:32 +00004654 * mm_get_mode_partial is used to check if the given base_addr page is
4655 * already mapped. If the page is unmapped, return error. If the page is
4656 * mapped appropriate attributes are returned to the caller. Note that
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004657 * 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 */
Karl Meakin9f568582025-02-20 17:12:32 +00004663 mode_ret = mm_get_mode_partial(&vm_locked.vm->ptable, base_addr,
4664 end_addr, &mode, &end_addr);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004665 if (!mode_ret || (mode & MM_MODE_INVALID)) {
Karl Meakined91e992025-02-01 16:16:27 +00004666 dlog_error(
4667 "FFA_MEM_PERM_GET: cannot find permission for range "
4668 "%#016lx - %#016lx\n",
4669 va_addr(base_addr), va_addr(end_addr));
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004670 ret = ffa_error(FFA_INVALID_PARAMETERS);
4671 goto out;
4672 }
Karl Meakin9f568582025-02-20 17:12:32 +00004673 page_count = (va_addr(end_addr) - va_addr(base_addr)) / PAGE_SIZE;
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004674
4675 /* No memory should be marked RWX */
4676 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
4677 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
4678
4679 /*
4680 * S-EL0 partitions are expected to have all their pages marked as
4681 * non-global.
4682 */
4683 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
4684 (MM_MODE_NG | MM_MODE_USER));
4685
Karl Meakin80220052025-02-20 14:43:34 +00004686 ret = (struct ffa_value){
4687 .func = FFA_SUCCESS_32,
Karl Meakined91e992025-02-01 16:16:27 +00004688 /* Same logic as for the input page count. */
4689 .arg3 = page_count - 1,
Karl Meakin80220052025-02-20 14:43:34 +00004690 };
Karl Meakincdecdba2025-02-01 15:36:58 +00004691
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004692 if (mode & MM_MODE_W) {
4693 /* No memory should be writeable but not readable. */
4694 CHECK(mode & MM_MODE_R);
Karl Meakin80220052025-02-20 14:43:34 +00004695 ret.arg2 = FFA_MEM_PERM_RW;
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004696 } else if (mode & MM_MODE_R) {
Karl Meakin80220052025-02-20 14:43:34 +00004697 ret.arg2 = FFA_MEM_PERM_RX;
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004698 if (!(mode & MM_MODE_X)) {
Karl Meakin80220052025-02-20 14:43:34 +00004699 ret.arg2 = FFA_MEM_PERM_RO;
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004700 }
4701 }
4702out:
4703 vm_unlock(&vm_locked);
4704 return ret;
4705}
4706
4707struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
Karl Meakin80220052025-02-20 14:43:34 +00004708 enum ffa_mem_perm mem_perm,
4709 struct vcpu *current)
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004710{
4711 struct vm_locked vm_locked;
4712 struct ffa_value ret;
Karl Meakincdecdba2025-02-01 15:36:58 +00004713 bool mode_ret;
Karl Meakin07a69ab2025-02-07 14:53:19 +00004714 mm_mode_t original_mode;
4715 mm_mode_t new_mode;
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004716 struct mpool local_page_pool;
Karl Meakin04f86c92025-03-11 15:47:29 +00004717 vaddr_t end_addr;
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004718
Karl Meakin117c8082024-12-04 16:03:28 +00004719 if (!ffa_memory_is_mem_perm_set_valid(current)) {
Karl Meakincdecdba2025-02-01 15:36:58 +00004720 dlog_error("FFA_MEM_PERM_SET: not allowed\n");
J-Alves5a099172024-02-22 14:34:51 +00004721 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004722 }
4723
Karl Meakincdecdba2025-02-01 15:36:58 +00004724 if (!current->vm->el0_partition) {
4725 dlog_error("FFA_MEM_PERM_SET: VM %#x is not an EL0 partition\n",
4726 current->vm->id);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004727 return ffa_error(FFA_DENIED);
4728 }
4729
4730 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
Karl Meakincdecdba2025-02-01 15:36:58 +00004731 dlog_error(
4732 "FFA_MEM_PERM_SET: base addr %#016lx is not page "
4733 "aligned\n",
4734 va_addr(base_addr));
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004735 return ffa_error(FFA_INVALID_PARAMETERS);
4736 }
4737
Karl Meakin04f86c92025-03-11 15:47:29 +00004738 /* Empty ranges should be disallowed, as should ranges that overflow */
4739 if (page_count == 0) {
4740 dlog_error("FFA_MEM_PERM_SET: page_count was zero\n");
4741 return ffa_error(FFA_INVALID_PARAMETERS);
4742 }
4743
4744 if (api_memory_range_end(base_addr, page_count, &end_addr)) {
4745 dlog_error("FFA_MEM_PERM_SET: overflow calculating end_addr\n");
4746 return ffa_error(FFA_INVALID_PARAMETERS);
4747 }
4748
Karl Meakin80220052025-02-20 14:43:34 +00004749 switch (mem_perm) {
4750 case FFA_MEM_PERM_RO:
4751 new_mode = MM_MODE_R | MM_MODE_USER | MM_MODE_NG;
4752 break;
4753 case FFA_MEM_PERM_RW:
4754 new_mode = MM_MODE_R | MM_MODE_W | MM_MODE_USER | MM_MODE_NG;
4755 break;
4756 case FFA_MEM_PERM_RX:
4757 new_mode = MM_MODE_R | MM_MODE_X | MM_MODE_USER | MM_MODE_NG;
4758 break;
4759 default:
Karl Meakincdecdba2025-02-01 15:36:58 +00004760 dlog_error("FFA_MEM_PERM_SET: invalid permissions %#x\n",
4761 mem_perm);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004762 return ffa_error(FFA_INVALID_PARAMETERS);
4763 }
4764
4765 /*
4766 * Create a local pool so any freed memory can't be used by another
4767 * thread. This is to ensure the original mapping can be restored if any
4768 * stage of the process fails.
4769 */
4770 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
4771
4772 vm_locked = vm_lock(current->vm);
4773
4774 /*
4775 * All regions accessible by the partition are mapped during boot. If we
4776 * cannot get a successful translation for the page range, the request
4777 * to change permissions is rejected.
4778 * mm_get_mode is used to check if the given address range is already
4779 * mapped. If the range is unmapped, return error. If the range is
4780 * mapped appropriate attributes are returned to the caller. Note that
4781 * mm_get_mode returns true if the address is in the valid VA range as
4782 * supported by the architecture and MMU configurations, as opposed to
4783 * whether a page is mapped or not. For a page to be known as mapped,
4784 * the API must return true AND the returned mode must not have
4785 * MM_MODE_INVALID set.
4786 */
4787
4788 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4789 va_add(base_addr, page_count * PAGE_SIZE),
4790 &original_mode);
4791 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
Karl Meakincdecdba2025-02-01 15:36:58 +00004792 dlog_error(
4793 "FFA_MEM_PERM_SET: range %#016lx - %#016lx is not "
4794 "mapped\n",
4795 va_addr(base_addr), va_addr(end_addr));
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004796 ret = ffa_error(FFA_INVALID_PARAMETERS);
4797 goto out;
4798 }
4799
4800 /* Device memory cannot be marked as executable */
4801 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
Karl Meakincdecdba2025-02-01 15:36:58 +00004802 dlog_error(
4803 "FFA_MEM_PERM_SET: cannot set device memory as "
4804 "executable\n");
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004805 ret = ffa_error(FFA_INVALID_PARAMETERS);
4806 goto out;
4807 }
4808
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004809 /*
4810 * Safe to re-map memory, since we know the requested permissions are
4811 * valid, and the memory requested to be re-mapped is also valid.
4812 */
Karl Meakincdecdba2025-02-01 15:36:58 +00004813 if (!mm_identity_prepare(&vm_locked.vm->ptable, pa_from_va(base_addr),
4814 pa_from_va(end_addr), new_mode,
4815 &local_page_pool)) {
4816 dlog_error(
4817 "FFA_MEM_PERM_SET: remapping memory range %#016lx - "
4818 "%#016lx failed\n",
4819 va_addr(base_addr), va_addr(end_addr));
4820
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004821 /*
4822 * Defrag the table into the local page pool.
4823 * mm_identity_prepare could have allocated or freed pages to
4824 * split blocks or tables etc.
4825 */
4826 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
4827
4828 /*
4829 * Guaranteed to succeed mapping with old mode since the mapping
4830 * with old mode already existed and we have a local page pool
4831 * that should have sufficient memory to go back to the original
4832 * state.
4833 */
4834 CHECK(mm_identity_prepare(
4835 &vm_locked.vm->ptable, pa_from_va(base_addr),
Karl Meakincdecdba2025-02-01 15:36:58 +00004836 pa_from_va(end_addr), original_mode, &local_page_pool));
4837 mm_identity_commit(&vm_locked.vm->ptable, pa_from_va(base_addr),
4838 pa_from_va(end_addr), original_mode,
4839 &local_page_pool);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004840
4841 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
4842 ret = ffa_error(FFA_NO_MEMORY);
4843 goto out;
4844 }
4845
Karl Meakincdecdba2025-02-01 15:36:58 +00004846 mm_identity_commit(&vm_locked.vm->ptable, pa_from_va(base_addr),
4847 pa_from_va(end_addr), new_mode, &local_page_pool);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004848
4849 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
4850
4851out:
4852 mpool_fini(&local_page_pool);
4853 vm_unlock(&vm_locked);
4854
4855 return ret;
4856}
Maksims Svecovs71b76702022-05-20 15:32:58 +01004857
4858/**
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004859 * Send the contents of the given vCPU's log buffer to the log, preceded
Karl Meakin705b56e2024-05-22 10:04:32 +01004860 * by the VM ID and followed by a newline.
4861 */
Karl Meakin6f1f1212024-07-16 10:18:16 +01004862void api_flush_log_buffer(struct vcpu_locked *vcpu_locked)
Karl Meakin705b56e2024-05-22 10:04:32 +01004863{
Karl Meakin6f1f1212024-07-16 10:18:16 +01004864 /*
4865 * NOTE: This line is parsed by `hftest.py`.
4866 * If you change the format, make sure to update
4867 * `HFTEST_CTRL_JSON_REGEX` as well.
4868 */
4869 struct vcpu *vcpu = vcpu_locked->vcpu;
4870 struct log_buffer *buffer = &vcpu->log_buffer;
4871 ffa_id_t vm_id = vcpu->vm->id;
4872 ffa_id_t vcpu_id = vcpu_index(vcpu);
4873
Karl Meakin7de26952024-06-14 14:50:19 +01004874 buffer->chars[buffer->len] = '\0';
Olivier Deprezbd432092024-07-26 14:01:12 +02004875 dlog("[%x %u] %s\n", vm_id, vcpu_id, buffer->chars);
Karl Meakin7de26952024-06-14 14:50:19 +01004876 buffer->len = 0;
Karl Meakin705b56e2024-05-22 10:04:32 +01004877}
4878
4879/**
Karl Meakin740f74f2024-02-14 18:04:48 +00004880 * Implements FF-A v1.2 FFA_CONSOLE_LOG ABI for buffered logging.
Maksims Svecovs71b76702022-05-20 15:32:58 +01004881 */
4882struct ffa_value api_ffa_console_log(const struct ffa_value args,
4883 struct vcpu *current)
4884{
Karl Meakin740f74f2024-02-14 18:04:48 +00004885 /* Maximum number of characters is 128: 16 registers of 8 bytes each. */
4886 char chars[128] = {0};
Karl Meakin0e617d92024-04-05 12:55:22 +01004887 const bool v1_2 = current->vm->ffa_version >= FFA_VERSION_1_2;
Karl Meakin740f74f2024-02-14 18:04:48 +00004888 const bool log32 = args.func == FFA_CONSOLE_LOG_32;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004889
Karl Meakin740f74f2024-02-14 18:04:48 +00004890 /*
4891 * 32bit: always 6 registers
4892 * 64bit and less than v1.2: 6 registers
4893 * 64bit and v1.2 or greater: 16 registers
4894 */
Karl Meakin66a38bd2024-05-28 16:00:56 +01004895 /* NOLINTNEXTLINE(readability-avoid-nested-conditional-operator) */
Karl Meakin740f74f2024-02-14 18:04:48 +00004896 const size_t registers_max = log32 ? 6 : (v1_2 ? 16 : 6);
4897 const size_t chars_max =
4898 registers_max * (log32 ? sizeof(uint32_t) : sizeof(uint64_t));
4899 const size_t chars_count = args.arg1;
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004900 struct vcpu_locked vcpu_locked;
4901 struct log_buffer *log_buffer;
Karl Meakin740f74f2024-02-14 18:04:48 +00004902
4903 assert(args.func == FFA_CONSOLE_LOG_32 ||
4904 args.func == FFA_CONSOLE_LOG_64);
4905
4906 if (chars_count == 0 || chars_count > chars_max) {
Maksims Svecovs71b76702022-05-20 15:32:58 +01004907 return ffa_error(FFA_INVALID_PARAMETERS);
4908 }
4909
Karl Meakin740f74f2024-02-14 18:04:48 +00004910 if (log32) {
4911 uint32_t *registers = (uint32_t *)chars;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004912
Karl Meakin740f74f2024-02-14 18:04:48 +00004913 registers[0] = args.arg2 & 0xffffffff;
4914 registers[1] = args.arg3 & 0xffffffff;
4915 registers[2] = args.arg4 & 0xffffffff;
4916 registers[3] = args.arg5 & 0xffffffff;
4917 registers[4] = args.arg6 & 0xffffffff;
4918 registers[5] = args.arg7 & 0xffffffff;
4919 } else {
4920 uint64_t *registers = (uint64_t *)chars;
4921
4922 registers[0] = args.arg2;
4923 registers[1] = args.arg3;
4924 registers[2] = args.arg4;
4925 registers[3] = args.arg5;
4926 registers[4] = args.arg6;
4927 registers[5] = args.arg7;
4928 if (v1_2) {
4929 registers[6] = args.extended_val.arg8;
4930 registers[7] = args.extended_val.arg9;
4931 registers[8] = args.extended_val.arg10;
4932 registers[9] = args.extended_val.arg11;
4933 registers[10] = args.extended_val.arg12;
4934 registers[11] = args.extended_val.arg13;
4935 registers[12] = args.extended_val.arg14;
4936 registers[13] = args.extended_val.arg15;
4937 registers[14] = args.extended_val.arg16;
4938 registers[15] = args.extended_val.arg17;
4939 }
4940 }
4941
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004942 vcpu_locked = vcpu_lock(current);
4943 log_buffer = &current->log_buffer;
Karl Meakin7de26952024-06-14 14:50:19 +01004944
Karl Meakin740f74f2024-02-14 18:04:48 +00004945 for (size_t i = 0; i < chars_count; i++) {
4946 bool flush = false;
4947 const char c = chars[i];
4948
4949 if (c == '\n' || c == '\0') {
4950 flush = true;
4951 } else {
J-Alves33b52f82025-03-21 10:55:31 +00004952 log_buffer->chars[log_buffer->len] = c;
4953 log_buffer->len++;
4954 assert(log_buffer->len <= LOG_BUFFER_SIZE);
Karl Meakin7de26952024-06-14 14:50:19 +01004955 flush = log_buffer->len == LOG_BUFFER_SIZE;
Karl Meakin740f74f2024-02-14 18:04:48 +00004956 }
4957
4958 if (flush) {
Karl Meakin6f1f1212024-07-16 10:18:16 +01004959 api_flush_log_buffer(&vcpu_locked);
Karl Meakin740f74f2024-02-14 18:04:48 +00004960 }
4961 }
Maksims Svecovs71b76702022-05-20 15:32:58 +01004962
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004963 vcpu_unlock(&vcpu_locked);
Maksims Svecovs71b76702022-05-20 15:32:58 +01004964 return (struct ffa_value){.func = FFA_SUCCESS_32};
4965}
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004966
4967/**
4968 * Send an IPI interrupt to a target vcpu belonging to the
4969 * sender that isn't itself.
4970 */
Daniel Boulby84d49b62024-11-04 18:25:59 +00004971int64_t api_hf_interrupt_send_ipi(uint32_t target_vcpu_id, struct vcpu *current)
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004972{
4973 struct vm *vm = current->vm;
4974 ffa_vcpu_index_t target_vcpu_index = vcpu_id_to_index(target_vcpu_id);
4975
J-Alves5dbf9322025-02-21 13:02:02 +00004976 if (target_vcpu_index >= vm->vcpu_count) {
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004977 dlog_verbose("Invalid vCPU %d for IPI.\n", target_vcpu_id);
4978 return -1;
4979 }
4980
4981 dlog_verbose("Injecting IPI to target vCPU%d for %#x\n", target_vcpu_id,
4982 vm->id);
4983
J-Alves5dbf9322025-02-21 13:02:02 +00004984 /*
4985 * If the SP is targeting the current vCPU, inject the IPI VI,
4986 * to avoid trapping into Hafnium.
4987 */
4988 if (target_vcpu_index == cpu_index(current->cpu)) {
4989 struct vcpu_locked current_locked = vcpu_lock(current);
4990
4991 vcpu_virt_interrupt_inject(current_locked, HF_IPI_INTID);
4992
4993 vcpu_unlock(&current_locked);
4994 } else {
4995 hf_ipi_send_interrupt(vm, target_vcpu_index);
4996 }
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004997
4998 return 0;
4999}