blob: 3df0b57225075d389469c2059e87edacf9e3b4ce [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Federico Recanati25053ee2022-03-14 15:01:53 +01002 * Copyright 2022 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +000012#include "hf/arch/ffa.h"
J-Alves0a21db12024-03-28 12:43:30 +000013#include "hf/arch/memcpy_trapped.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020014#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020015#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020016#include "hf/arch/plat/ffa.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000017#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020018#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000019
Karl Meakin49ec1e42024-05-10 13:08:24 +010020#include "hf/bits.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010021#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000022#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010023#include "hf/ffa_internal.h"
24#include "hf/ffa_memory.h"
J-Alves33c47bf2022-09-29 11:36:20 +010025#include "hf/ffa_v1_0.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010027#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010028#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000029#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010030#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010031#include "hf/std.h"
Karl Meakinc5cebbc2024-06-17 11:30:27 +010032#include "hf/vcpu.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010033#include "hf/vm.h"
34
Andrew Scullf35a5c92018-08-07 18:09:46 +010035#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010036#include "vmapi/hf/ffa.h"
J-Alves126ab502022-09-29 11:37:33 +010037#include "vmapi/hf/ffa_v1_0.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010038
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000039static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010040 "Partition information descriptor size doesn't match the one in "
41 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000042static_assert(sizeof(struct ffa_partition_info) == 24,
43 "Partition information descriptor size doesn't match the one in "
44 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -080045static_assert((sizeof(struct ffa_partition_info) & 7) == 0,
46 "Partition information descriptor must be a multiple of 8 bytes"
47 " for ffa_partition_info_get_regs to work correctly. Information"
48 " from this structure are returned in 8 byte registers and the"
49 " count of 8 byte registers is returned by the ABI.");
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000050/*
51 * To eliminate the risk of deadlocks, we define a partial order for the
52 * acquisition of locks held concurrently by the same physical CPU. Our current
53 * ordering requirements are as follows:
54 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010055 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000056 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010057 * Locks of the same kind require the lock of lowest address to be locked first,
58 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000059 */
60
Andrew Scullaa039b32018-10-04 15:02:26 +010061static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010062 "Currently, a page is mapped for the send and receive buffers so "
63 "the maximum request is the size of a page.");
64
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000065static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
66 "The page pool entry size must be at least as big as the mailbox "
67 "size, so that memory region descriptors can be copied from the "
68 "mailbox for memory sharing.");
69
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -080070/*
71 * Maximum ffa_partition_info entries that can be returned by an invocation
72 * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available
73 * registers/args in struct ffa_value divided by size of struct
74 * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e.
75 * 15 uint64_t fields. For FF-A v1.1, this value should be 5.
76 */
77#define MAX_INFO_REGS_ENTRIES_PER_CALL \
78 ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info))
79static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5,
80 "FF-A v1.1 supports no more than 5 entries"
81 " per FFA_PARTITION_INFO_GET_REGS64 calls");
82
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000083static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000084
85/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000086 * Initialises the API page pool by taking ownership of the contents of the
87 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000088 */
89void api_init(struct mpool *ppool)
90{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000091 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000092}
93
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010094/**
J-Alvesad6a0432021-04-09 16:06:21 +010095 * Get target VM vCPU:
96 * If VM is UP then return first vCPU.
97 * If VM is MP then return vCPU whose index matches current CPU index.
98 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050099struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +0100100{
101 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
102 struct vcpu *vcpu = NULL;
103
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500104 CHECK((vm != NULL) && (current != NULL));
105
Karl Meakin82041822024-05-20 11:14:34 +0100106 if (vm_is_up(vm)) {
J-Alvesad6a0432021-04-09 16:06:21 +0100107 vcpu = vm_get_vcpu(vm, 0);
108 } else if (current_cpu_index < vm->vcpu_count) {
109 vcpu = vm_get_vcpu(vm, current_cpu_index);
110 }
111
112 return vcpu;
113}
114
115/**
J-Alvesfe7f7372020-11-09 11:32:12 +0000116 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
117 * is given as argument of the function.
118 *
119 * Called to change the context between SPs for direct messaging (when Hafnium
120 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
121 *
122 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100123 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000124 * - MP with pinned Execution Contexts.
125 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600126struct vcpu *api_switch_to_vm(struct vcpu_locked current_locked,
127 struct ffa_value to_ret,
J-Alves19e20cf2023-08-02 12:48:55 +0100128 enum vcpu_state vcpu_state, ffa_id_t to_id)
J-Alvesfe7f7372020-11-09 11:32:12 +0000129{
130 struct vm *to_vm = vm_find(to_id);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600131 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current_locked.vcpu);
J-Alvesfe7f7372020-11-09 11:32:12 +0000132
133 CHECK(next != NULL);
134
135 /* Set the return value for the target VM. */
136 arch_regs_set_retval(&next->regs, to_ret);
137
138 /* Set the current vCPU state. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600139 current_locked.vcpu->state = vcpu_state;
J-Alvesfe7f7372020-11-09 11:32:12 +0000140
141 return next;
142}
143
144/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000145 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100146 *
147 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100148 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100149 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600150struct vcpu *api_switch_to_primary(struct vcpu_locked current_locked,
J-Alves27b71962022-12-12 15:29:58 +0000151 struct ffa_value primary_ret,
152 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100153{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000154 /*
155 * If the secondary is blocked but has a timer running, sleep until the
156 * timer fires rather than indefinitely.
157 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100158 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100159 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
160 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100161 if (arch_timer_enabled_current()) {
162 uint64_t remaining_ns =
163 arch_timer_remaining_ns_current();
164
165 if (remaining_ns == 0) {
166 /*
167 * Timer is pending, so the current vCPU should
168 * be run again right away.
169 */
Andrew Walbran7e824602020-10-22 16:51:40 +0100170 primary_ret = (struct ffa_value){
171 .func = FFA_INTERRUPT_32};
172
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100173 } else {
174 primary_ret.arg2 = remaining_ns;
175 }
176 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100177 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100178 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000179 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100180 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000181
182 default:
183 /* Do nothing. */
184 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000185 }
186
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600187 return api_switch_to_vm(current_locked, primary_ret, secondary_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000188 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100189}
190
191/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200192 * Choose next vCPU to run to be the counterpart vCPU in the other
193 * world (run the normal world if currently running in the secure
194 * world). Set current vCPU state to the given vcpu_state parameter.
195 * Set FF-A return values to the target vCPU in the other world.
196 *
197 * Called in context of a direct message response from a secure
198 * partition to a VM.
199 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600200struct vcpu *api_switch_to_other_world(struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100201 struct ffa_value other_world_ret,
202 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200203{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600204 return api_switch_to_vm(current_locked, other_world_ret, vcpu_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000205 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200206}
207
208/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000209 * Returns true if the given vCPU is executing in context of an
210 * FFA_MSG_SEND_DIRECT_REQ invocation.
211 */
J-Alves27b71962022-12-12 15:29:58 +0000212bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000213{
Kathleen Capellae468c112023-12-13 17:56:28 -0500214 return locked.vcpu->direct_request_origin.vm_id != HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000215}
216
217/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100218 * Returns true if the VM owning the given vCPU is supporting managed exit and
219 * the vCPU is currently processing a managed exit.
220 */
221static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
222{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100223 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100224 vcpu_locked.vcpu->processing_managed_exit);
225}
226
227/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000228 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000229 */
230struct vcpu *api_preempt(struct vcpu *current)
231{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600232 struct vcpu_locked current_locked;
233 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100234 struct ffa_value ret = {
235 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200236 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000237 };
238
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600239 current_locked = vcpu_lock(current);
240 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_PREEMPTED);
241 vcpu_unlock(&current_locked);
242
243 return next;
Andrew Scull33fecd32019-01-08 14:48:27 +0000244}
245
246/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000247 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000248 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100249 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100250struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100251{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600252 struct vcpu_locked current_locked;
253 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100254 struct ffa_value ret = {
255 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
256 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100257 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000258
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600259 current_locked = vcpu_lock(current);
260 next = api_switch_to_primary(current_locked, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100261 VCPU_STATE_BLOCKED_INTERRUPT);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600262 vcpu_unlock(&current_locked);
263
264 return next;
Andrew Scullaa039b32018-10-04 15:02:26 +0100265}
266
267/**
Andrew Walbran33645652019-04-15 12:29:31 +0100268 * Puts the current vCPU in off mode, and returns to the primary VM.
269 */
270struct vcpu *api_vcpu_off(struct vcpu *current)
271{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600272 struct vcpu_locked current_locked;
273 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100274 struct ffa_value ret = {
275 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
276 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100277 };
278
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600279 current_locked = vcpu_lock(current);
Andrew Walbran33645652019-04-15 12:29:31 +0100280 /*
281 * Disable the timer, so the scheduler doesn't get told to call back
282 * based on it.
283 */
284 arch_timer_disable_current();
285
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600286 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_OFF);
287 vcpu_unlock(&current_locked);
288
289 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100290}
291
292/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500293 * The current vCPU is blocked on some resource and needs to relinquish
294 * control back to the execution context of the endpoint that originally
295 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000296 */
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500297struct ffa_value api_yield(struct vcpu *current, struct vcpu **next,
298 struct ffa_value *args)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000299{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000300 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
301 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500302 bool transition_allowed;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500303 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500304 uint32_t timeout_low = 0;
305 uint32_t timeout_high = 0;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600306 struct vcpu_locked next_locked = (struct vcpu_locked){
307 .vcpu = NULL,
308 };
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500309
310 if (args != NULL) {
311 if (args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
312 args->arg7 != 0U) {
313 dlog_error(
314 "Parameters passed through registers X4-X7 "
315 "must be zero\n");
316 return ffa_error(FFA_INVALID_PARAMETERS);
317 }
318 timeout_low = (uint32_t)args->arg2 & 0xFFFFFFFF;
319 timeout_high = (uint32_t)args->arg3 & 0xFFFFFFFF;
320 }
Andrew Scull66d62bf2019-02-01 13:54:10 +0000321
Karl Meakin5e996992024-05-20 11:27:07 +0100322 if (vm_is_primary(current->vm)) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000323 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000324 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000325 }
326
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000327 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500328 transition_allowed = plat_ffa_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600329 current_locked, current->vm->id, HF_INVALID_VM_ID, next_locked,
330 FFA_YIELD_32, &next_state);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000331
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500332 if (!transition_allowed) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600333 ret = ffa_error(FFA_DENIED);
334 goto out;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000335 }
336
Madhukar Pappireddy65764072023-05-23 17:18:58 -0500337 /*
338 * The current vCPU is expected to move to BLOCKED state. However,
339 * under certain circumstances, it is allowed for the current vCPU
340 * to be resumed immediately without ever moving to BLOCKED state. One
341 * such scenario occurs when an SP's execution context attempts to
342 * yield cycles while handling secure interrupt. Refer to the comments
343 * in the SPMC variant of the plat_ffa_yield_prepare function.
344 */
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500345 assert(!vm_id_is_current_world(current->vm->id) ||
346 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500347
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600348 ret = plat_ffa_yield_prepare(current_locked, next, timeout_low,
349 timeout_high);
350out:
351 vcpu_unlock(&current_locked);
352 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000353}
354
355/**
Andrew Walbran33645652019-04-15 12:29:31 +0100356 * Switches to the primary so that it can switch to the target, or kick it if it
357 * is already running on a different physical CPU.
358 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600359static struct vcpu *api_wake_up_locked(struct vcpu_locked current_locked,
360 struct vcpu *target_vcpu)
Andrew Walbran33645652019-04-15 12:29:31 +0100361{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100362 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100363 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
365 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100366 };
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600367
368 return api_switch_to_primary(current_locked, ret, VCPU_STATE_BLOCKED);
369}
370
371struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
372{
373 struct vcpu_locked current_locked;
374 struct vcpu *next;
375
376 current_locked = vcpu_lock(current);
377 next = api_wake_up_locked(current_locked, target_vcpu);
378 vcpu_unlock(&current_locked);
379
380 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100381}
382
383/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000384 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000385 */
386struct vcpu *api_abort(struct vcpu *current)
387{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100388 struct ffa_value ret = ffa_error(FFA_ABORTED);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600389 struct vcpu_locked current_locked;
390 struct vcpu *next;
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500391 struct vm_locked vm_locked;
Andrew Scull9726c252019-01-23 13:44:19 +0000392
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100393 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000394 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000395
Karl Meakin5e996992024-05-20 11:27:07 +0100396 if (vm_is_primary(current->vm)) {
Andrew Scull9726c252019-01-23 13:44:19 +0000397 /* TODO: what to do when the primary aborts? */
398 for (;;) {
399 /* Do nothing. */
400 }
401 }
402
403 atomic_store_explicit(&current->vm->aborting, true,
404 memory_order_relaxed);
405
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500406 vm_locked = vm_lock(current->vm);
407 plat_ffa_free_vm_resources(vm_locked);
408 vm_unlock(&vm_locked);
Andrew Scull9726c252019-01-23 13:44:19 +0000409
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600410 current_locked = vcpu_lock(current);
411 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_ABORTED);
412 vcpu_unlock(&current_locked);
413
414 return next;
Andrew Scull9726c252019-01-23 13:44:19 +0000415}
416
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000417/*
418 * Format the partition info descriptors according to the version supported
419 * by the endpoint and return the size of the array created.
420 */
421static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000422 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000423 uint32_t vm_count)
424{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000425 struct vm *vm = vm_locked.vm;
Karl Meakin0e617d92024-04-05 12:55:22 +0100426 enum ffa_version version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100427 uint32_t partition_info_size;
428 uint32_t buffer_size;
Federico Recanati644f0462022-03-17 12:04:00 +0100429 struct ffa_value ret;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000430
J-Alvese8c8c2b2022-12-16 15:34:48 +0000431 /* Acquire receiver's RX buffer. */
432 if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) {
433 dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id);
434 return ret;
435 }
436
J-Alves122f1a12022-12-12 15:55:42 +0000437 if (vm_is_mailbox_busy(vm_locked)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000438 /*
439 * Can't retrieve memory information if the mailbox is not
440 * available.
441 */
442 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000443 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000444 }
445
Karl Meakin0e617d92024-04-05 12:55:22 +0100446 if (version == FFA_VERSION_1_0) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000447 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
448
Daniel Boulby835e1592022-05-30 17:38:51 +0100449 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
450 buffer_size = partition_info_size * vm_count;
451 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000452 dlog_error(
453 "Partition information does not fit in the "
J-Alves0a21db12024-03-28 12:43:30 +0000454 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000455 return ffa_error(FFA_NO_MEMORY);
456 }
457
458 for (uint32_t i = 0; i < vm_count; i++) {
459 /*
460 * Populate the VM's RX buffer with the partition
Kathleen Capella402fa852022-11-09 16:16:51 -0500461 * information. Clear properties bits that must be zero
462 * according to DEN0077A FF-A v1.0 REL Table 8.25.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000463 */
464 recv_mailbox[i].vm_id = partitions[i].vm_id;
465 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
Kathleen Capella402fa852022-11-09 16:16:51 -0500466 recv_mailbox[i].properties =
467 partitions[i].properties &
468 ~FFA_PARTITION_v1_0_RES_MASK;
Daniel Boulby191b5f02022-02-17 17:26:14 +0000469 }
470
471 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100472 partition_info_size = sizeof(struct ffa_partition_info);
473 buffer_size = partition_info_size * vm_count;
J-Alves0a21db12024-03-28 12:43:30 +0000474
Daniel Boulby835e1592022-05-30 17:38:51 +0100475 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000476 dlog_error(
477 "Partition information does not fit in the "
J-Alves0a824e92024-04-26 16:20:12 +0100478 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000479 return ffa_error(FFA_NO_MEMORY);
480 }
481
J-Alves0a21db12024-03-28 12:43:30 +0000482 /*
483 * Populate the VM's RX buffer with the partition information.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000484 */
J-Alves0a21db12024-03-28 12:43:30 +0000485 if (!memcpy_trapped(vm->mailbox.recv, HF_MAILBOX_SIZE,
486 partitions, buffer_size)) {
487 dlog_error(
488 "%s: Failed to copy ffa_partition_info "
489 "descriptor\n",
490 __func__);
491 return ffa_error(FFA_ABORTED);
492 }
Daniel Boulby191b5f02022-02-17 17:26:14 +0000493 }
494
Daniel Boulby835e1592022-05-30 17:38:51 +0100495 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000496
497 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000498 vm->mailbox.recv_sender = HF_VM_ID_BASE;
499 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000500 vm->mailbox.state = MAILBOX_STATE_FULL;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000501
Daniel Boulby835e1592022-05-30 17:38:51 +0100502 /*
503 * Return the count of partition information descriptors in w2
504 * and the size of the descriptors in w3.
505 */
506 return (struct ffa_value){.func = FFA_SUCCESS_32,
507 .arg2 = vm_count,
508 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000509}
510
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800511static void api_ffa_fill_partitions_info_array(
512 struct ffa_partition_info *partitions, size_t partitions_len,
J-Alves19e20cf2023-08-02 12:48:55 +0100513 const struct ffa_uuid *uuid, bool count_flag, ffa_id_t vm_id,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800514 ffa_vm_count_t *vm_count_out)
515{
516 ffa_vm_count_t vm_count = 0;
517 bool uuid_is_null = ffa_uuid_is_null(uuid);
518
519 assert(vm_get_count() <= partitions_len);
520
521 /*
522 * Iterate through the VMs to find the ones with a matching
523 * UUID. A Null UUID retrieves information for all VMs.
524 */
525 for (ffa_vm_count_t index = 0; index < vm_get_count(); ++index) {
526 struct vm *vm = vm_find_index(index);
527
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400528 for (uint32_t i = 0; i < PARTITION_MAX_UUIDS; i++) {
529 /*
530 * Null UUID indicates reaching the end of a
531 * partition's array of UUIDs.
532 */
533 if (ffa_uuid_is_null(&vm->uuids[i])) {
534 break;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800535 }
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400536 if (uuid_is_null ||
537 ffa_uuid_equal(uuid, &vm->uuids[i])) {
538 uint16_t array_index = vm_count;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800539
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400540 ++vm_count;
541 if (count_flag) {
542 continue;
543 }
544
545 partitions[array_index].vm_id = vm->id;
546 partitions[array_index].vcpu_count =
547 vm->vcpu_count;
548 partitions[array_index].properties =
549 plat_ffa_partition_properties(vm_id,
550 vm);
551 partitions[array_index].properties |=
552 vm_are_notifications_enabled(vm)
553 ? FFA_PARTITION_NOTIFICATION
554 : 0;
555 partitions[array_index].properties |=
556 FFA_PARTITION_AARCH64_EXEC;
557 if (uuid_is_null) {
558 partitions[array_index].uuid =
559 vm->uuids[i];
560 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800561 }
562 }
563 }
564
565 *vm_count_out = vm_count;
566}
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800567
568static inline void api_ffa_pack_vmid_count_props(
J-Alves19e20cf2023-08-02 12:48:55 +0100569 uint64_t *xn, ffa_id_t vm_id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800570 ffa_partition_properties_t properties)
571{
572 *xn = (uint64_t)vm_id;
573 *xn |= (uint64_t)vcpu_count << 16;
574 *xn |= (uint64_t)properties << 32;
575}
576
577static inline void api_ffa_pack_uuid(uint64_t *xn_1, uint64_t *xn_2,
578 struct ffa_uuid *uuid)
579{
580 *xn_1 = (uint64_t)uuid->uuid[0];
581 *xn_1 |= (uint64_t)uuid->uuid[1] << 32;
582 *xn_2 = (uint64_t)uuid->uuid[2];
583 *xn_2 |= (uint64_t)uuid->uuid[3] << 32;
584}
585
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700586/**
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700587 * This function forwards the FFA_PARTITION_INFO_GET_REGS ABI to the other world
588 * when hafnium is the hypervisor to determine the secure partitions. When
589 * hafnium is the SPMC, this function forwards the call to the SPMD to discover
590 * SPMD logical partitions. The function returns true when partition information
591 * is filled in the partitions array and false if there are errors. Note that
592 * the SPMD and SPMC may return an FF-A error code of FFA_NOT_SUPPORTED when
593 * there are no SPMD logical partitions or no secure partitions respectively,
594 * and this is not considered a failure of the forwarded call. A caller is
595 * expected to check the return value before consuming the information in the
596 * partitions array passed in and ret_count.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700597 */
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700598static bool api_ffa_partition_info_get_regs_forward(
599 const struct ffa_uuid *uuid, const uint16_t tag,
600 struct ffa_partition_info *partitions, uint16_t partitions_len,
601 ffa_vm_count_t *ret_count)
602{
603 (void)tag;
604 struct ffa_value ret;
605 uint16_t last_index = UINT16_MAX;
606 uint16_t curr_index = 0;
607 uint16_t start_index = 0;
608
609 if (!plat_ffa_partition_info_get_regs_forward_allowed()) {
610 return true;
611 }
612
613 while (start_index <= last_index) {
614 ret = ffa_partition_info_get_regs(uuid, start_index, 0);
615 if (ffa_func_id(ret) != FFA_SUCCESS_64) {
616 /*
617 * If there are no logical partitions, SPMD returns
618 * NOT_SUPPORTED, that is not an error. If there are no
619 * secure partitions the SPMC returns NOT_SUPPORTED.
620 */
621 if ((ffa_func_id(ret) == FFA_ERROR_32) &&
622 (ffa_error_code(ret) == FFA_NOT_SUPPORTED)) {
623 return true;
624 }
625
626 return false;
627 }
628
629 if (!api_ffa_fill_partition_info_from_regs(
630 ret, start_index, partitions, partitions_len,
631 ret_count)) {
632 return false;
633 }
634
635 last_index = ffa_partition_info_regs_get_last_idx(ret);
636 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
637 start_index = curr_index + 1;
638 }
639 return true;
640}
641
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700642bool api_ffa_fill_partition_info_from_regs(
643 struct ffa_value ret, uint16_t start_index,
644 struct ffa_partition_info *partitions, uint16_t partitions_len,
645 ffa_vm_count_t *ret_count)
646{
647 uint16_t vm_count = *ret_count;
648 uint16_t curr_index = 0;
649 uint8_t num_entries = 0;
650 uint8_t idx = 0;
651 /* List of pointers to args in return value. */
652 uint64_t *arg_ptrs[] = {
653 &ret.arg3,
654 &ret.arg4,
655 &ret.arg5,
656 &ret.arg6,
657 &ret.arg7,
658 &ret.extended_val.arg8,
659 &ret.extended_val.arg9,
660 &ret.extended_val.arg10,
661 &ret.extended_val.arg11,
662 &ret.extended_val.arg12,
663 &ret.extended_val.arg13,
664 &ret.extended_val.arg14,
665 &ret.extended_val.arg15,
666 &ret.extended_val.arg16,
667 &ret.extended_val.arg17,
668 };
669
670 if (vm_count > partitions_len) {
671 return false;
672 }
673
674 /*
675 * Tags are currently unused in the implementation. Expect it to be
676 * zero since the implementation does not provide a tag when calling
677 * the FFA_PARTITION_INFO_GET_REGS ABI.
678 */
679 assert(ffa_partition_info_regs_get_tag(ret) == 0);
680
681 /*
682 * Hafnium expects the size of the returned descriptor to be equal to
683 * the size of the structure in the FF-A 1.1 specification. When future
684 * enhancements are made, this assert can be relaxed.
685 */
686 assert(ffa_partition_info_regs_get_desc_size(ret) ==
687 sizeof(struct ffa_partition_info));
688
689 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
690
691 /* FF-A 1.2 ALP0, section 14.9.2 Usage rule 7. */
692 assert(start_index <= curr_index);
693
694 num_entries = curr_index - start_index + 1;
695 if (num_entries > (partitions_len - vm_count) ||
696 num_entries > MAX_INFO_REGS_ENTRIES_PER_CALL) {
697 return false;
698 }
699
700 while (num_entries) {
701 uint64_t info = *(arg_ptrs[(ptrdiff_t)(idx++)]);
702 uint64_t uuid_lo = *(arg_ptrs[(ptrdiff_t)(idx++)]);
703 uint64_t uuid_high = *(arg_ptrs[(ptrdiff_t)(idx++)]);
704
705 partitions[vm_count].vm_id = info & 0xFFFF;
706 partitions[vm_count].vcpu_count = (info >> 16) & 0xFFFF;
707 partitions[vm_count].properties = (info >> 32);
708 partitions[vm_count].uuid.uuid[0] = uuid_lo & 0xFFFFFFFF;
709 partitions[vm_count].uuid.uuid[1] =
710 (uuid_lo >> 32) & 0xFFFFFFFF;
711 partitions[vm_count].uuid.uuid[2] = uuid_high & 0xFFFFFFFF;
712 partitions[vm_count].uuid.uuid[3] =
713 (uuid_high >> 32) & 0xFFFFFFFF;
714 vm_count++;
715 num_entries--;
716 }
717
718 *ret_count = vm_count;
719 return true;
720}
721
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800722struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current,
723 const struct ffa_uuid *uuid,
724 const uint16_t start_index,
725 const uint16_t tag)
726{
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800727 struct vm *current_vm = current->vm;
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800728 static struct ffa_partition_info partitions[2 * MAX_VMS];
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800729 bool uuid_is_null = ffa_uuid_is_null(uuid);
730 ffa_vm_count_t vm_count = 0;
731 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
732 uint16_t max_idx = 0;
733 uint16_t curr_idx = 0;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800734 uint8_t num_entries_to_ret = 0;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800735 uint8_t arg_idx = 3;
736
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800737 /* list of pointers to args in return value */
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800738 uint64_t *arg_ptrs[] = {
739 &(ret).func,
740 &(ret).arg1,
741 &(ret).arg2,
742 &(ret).arg3,
743 &(ret).arg4,
744 &(ret).arg5,
745 &(ret).arg6,
746 &(ret).arg7,
747 &(ret).extended_val.arg8,
748 &(ret).extended_val.arg9,
749 &(ret).extended_val.arg10,
750 &(ret).extended_val.arg11,
751 &(ret).extended_val.arg12,
752 &(ret).extended_val.arg13,
753 &(ret).extended_val.arg14,
754 &(ret).extended_val.arg15,
755 &(ret).extended_val.arg16,
756 &(ret).extended_val.arg17,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800757 };
758
759 /* TODO: Add support for using tags */
760 if (tag != 0) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800761 dlog_error("Tag not 0. Unsupported tag. %d\n", tag);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800762 return ffa_error(FFA_RETRY);
763 }
764
765 memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions));
766
767 api_ffa_fill_partitions_info_array(partitions, ARRAY_SIZE(partitions),
768 uuid, false, current_vm->id,
769 &vm_count);
770
771 /* If UUID is Null vm_count must not be zero at this stage. */
772 CHECK(!uuid_is_null || vm_count != 0);
773
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800774 /*
775 * When running the Hypervisor:
776 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
777 * it to fill with secure partitions information.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700778 * When running the SPMC, the SPMC forwards the call to the SPMD to
779 * discover any EL3 SPMD logical partitions, if the call came from an
780 * SP. Otherwise, the call is not forwarded.
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800781 * TODO: Note that for this ABI, forwarding on every invocation when
782 * uuid is Null is inefficient,and if performance becomes a problem,
783 * this would be a good place to optimize using strategies such as
784 * caching info etc. For now, assuming this inefficiency is not a major
785 * issue.
786 * - If UUID is non-Null vm_count may be zero because the UUID matches
787 * a secure partition and the query is forwarded to the SPMC.
788 * When running the SPMC:
789 * - If UUID is non-Null and vm_count is zero it means there is no such
790 * partition identified in the system.
791 */
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700792 if (vm_id_is_current_world(current_vm->id)) {
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700793 if (!api_ffa_partition_info_get_regs_forward(
794 uuid, tag, partitions, ARRAY_SIZE(partitions),
795 &vm_count)) {
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700796 dlog_error(
797 "Failed to forward "
798 "ffa_partition_info_get_regs.\n");
799 return ffa_error(FFA_DENIED);
800 }
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800801 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800802
803 /*
804 * Unrecognized UUID: does not match any of the VMs (or SPs)
805 * and is not Null.
806 */
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800807 if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) {
Olivier Deprez89da0a42023-09-01 17:38:33 +0200808 dlog_verbose(
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800809 "Invalid parameters. vm_count = %d (must not be zero "
Karl Meakine8937d92024-03-19 16:04:25 +0000810 "or > %lu)\n",
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800811 vm_count, ARRAY_SIZE(partitions));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800812 return ffa_error(FFA_INVALID_PARAMETERS);
813 }
814
815 if (start_index >= vm_count) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800816 dlog_error(
817 "start index = %d vm_count = %d (start_index must be "
818 "less than vm_count)\n",
819 start_index, vm_count);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800820 return ffa_error(FFA_INVALID_PARAMETERS);
821 }
822
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800823 max_idx = vm_count - 1;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800824 num_entries_to_ret = (max_idx - start_index) + 1;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800825 num_entries_to_ret =
826 MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800827 curr_idx = start_index + num_entries_to_ret - 1;
828 assert(curr_idx <= max_idx);
829
830 ret.func = FFA_SUCCESS_64;
831 ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48;
832 ret.arg2 |= curr_idx << 16;
833 ret.arg2 |= max_idx;
834
835 if (num_entries_to_ret > 1) {
836 ret.extended_val.valid = 1;
837 }
838
839 for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800840 uint64_t *xn_0 = arg_ptrs[arg_idx++];
841 uint64_t *xn_1 = arg_ptrs[arg_idx++];
842 uint64_t *xn_2 = arg_ptrs[arg_idx++];
843
844 api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id,
845 partitions[idx].vcpu_count,
846 partitions[idx].properties);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800847 if (uuid_is_null) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800848 api_ffa_pack_uuid(xn_1, xn_2, &partitions[idx].uuid);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800849 }
Raghu Krishnamurthycded31b2023-04-23 15:07:35 -0700850 assert(arg_idx <= ARRAY_SIZE(arg_ptrs));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800851 }
852
853 return ret;
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800854}
855
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100856struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000857 const struct ffa_uuid *uuid,
858 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100859{
860 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100861 ffa_vm_count_t vm_count = 0;
Olivier Deprez013f4d62022-11-21 15:46:20 +0100862 bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) ==
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000863 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100864 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100865 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000866 struct vm_locked vm_locked;
867 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100868
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000869 /* Bits 31:1 Must Be Zero */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100870 if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) {
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000871 return ffa_error(FFA_INVALID_PARAMETERS);
872 }
873
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400874 api_ffa_fill_partitions_info_array(partitions, ARRAY_SIZE(partitions),
875 uuid, count_flag, current_vm->id,
876 &vm_count);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100877
Olivier Depreze562e542020-06-11 17:31:54 +0200878 /* If UUID is Null vm_count must not be zero at this stage. */
879 CHECK(!uuid_is_null || vm_count != 0);
880
881 /*
882 * When running the Hypervisor:
883 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
884 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000885 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200886 * a secure partition and the query is forwarded to the SPMC.
887 * When running the SPMC:
888 * - If UUID is non-Null and vm_count is zero it means there is no such
889 * partition identified in the system.
890 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000891 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200892
893 /*
894 * Unrecognized UUID: does not match any of the VMs (or SPs)
895 * and is not Null.
896 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100897 if (vm_count == 0) {
898 return ffa_error(FFA_INVALID_PARAMETERS);
899 }
900
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000901 /*
902 * If the count flag is set we don't need to return the partition info
903 * descriptors.
904 */
905 if (count_flag) {
906 return (struct ffa_value){.func = FFA_SUCCESS_32,
907 .arg2 = vm_count};
908 }
909
Daniel Boulby191b5f02022-02-17 17:26:14 +0000910 vm_locked = vm_lock(current_vm);
911 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
912 vm_count);
913 vm_unlock(&vm_locked);
914 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100915}
916
Andrew Scull9726c252019-01-23 13:44:19 +0000917/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000918 * Returns the ID of the VM.
919 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100920struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000921{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100922 return (struct ffa_value){.func = FFA_SUCCESS_32,
923 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000924}
925
926/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200927 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
928 * FF-A instances.
929 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000930 */
931struct ffa_value api_ffa_spm_id_get(void)
932{
Karl Meakin0e617d92024-04-05 12:55:22 +0100933 if (FFA_VERSION_1_1 <= FFA_VERSION_COMPILED) {
934 /*
935 * Return the SPMC ID that was fetched during FF-A
936 * initialization.
937 */
938 return (struct ffa_value){.func = FFA_SUCCESS_32,
939 .arg2 = arch_ffa_spmc_id_get()};
940 }
941
J-Alves3829fc02021-03-18 12:49:18 +0000942 return ffa_error(FFA_NOT_SUPPORTED);
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000943}
944
945/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000946 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000947 * function to indicate that register state for the given vCPU has been saved
948 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000949 */
950void api_regs_state_saved(struct vcpu *vcpu)
951{
952 sl_lock(&vcpu->lock);
953 vcpu->regs_available = true;
954 sl_unlock(&vcpu->lock);
955}
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000956/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000957 * Assuming that the arguments have already been checked by the caller, injects
958 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
959 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
960 * is next run, which is up to the scheduler.
961 *
962 * Returns:
963 * - 0 on success if no further action is needed.
964 * - 1 if it was called by the primary VM and the primary VM now needs to wake
965 * up or kick the target vCPU.
966 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100967int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600968 uint32_t intid,
969 struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100970 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000971{
Manish Pandey35e452f2021-02-18 21:36:34 +0000972 struct vcpu *target_vcpu = target_locked.vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600973 struct vcpu *current = current_locked.vcpu;
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100974 struct interrupts *interrupts = &target_vcpu->interrupts;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000975 int64_t ret = 0;
976
Andrew Walbran508e63c2018-12-20 17:02:37 +0000977 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000978 * We only need to change state and (maybe) trigger a virtual interrupt
979 * if it is enabled and was not previously pending. Otherwise we can
980 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000981 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100982 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
983 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000984 goto out;
985 }
986
987 /* Increment the count. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100988 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000989
990 /*
991 * Only need to update state if there was not already an
992 * interrupt enabled and pending.
993 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000994 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000995 goto out;
996 }
997
Karl Meakin5e996992024-05-20 11:27:07 +0100998 if (vm_is_primary(current->vm)) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000999 /*
1000 * If the call came from the primary VM, let it know that it
1001 * should run or kick the target vCPU.
1002 */
1003 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +00001004 } else if (current != target_vcpu && next != NULL) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001005 *next = api_wake_up_locked(current_locked, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001006 }
1007
1008out:
1009 /* Either way, make it pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01001010 vcpu_virt_interrupt_set_pending(interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001011
Olivier Deprezc19a6ea2020-08-06 11:16:07 +02001012 return ret;
1013}
1014
Andrew Walbran508e63c2018-12-20 17:02:37 +00001015/**
J-Alves2ced1672022-12-12 14:35:38 +00001016 * Constructs the return value from a successful FFA_MSG_WAIT call, when used
1017 * with FFA_MSG_SEND_32.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001018 */
J-Alves27b71962022-12-12 15:29:58 +00001019struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001020{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001021 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001022 case FFA_MSG_SEND_32:
1023 return (struct ffa_value){
1024 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001025 .arg1 = (receiver->mailbox.recv_sender << 16) |
1026 receiver->id,
1027 .arg3 = receiver->mailbox.recv_size};
J-Alvesd9e7c8f2024-09-11 13:37:25 +01001028 default:
Federico Recanati25053ee2022-03-14 15:01:53 +01001029 return (struct ffa_value){
1030 .func = FFA_RUN_32,
1031 /*
1032 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
1033 * Retrieving vCPU requires a rework of the function,
1034 * while receiver ID must be set because it's checked by
1035 * other APIs (eg: FFA_NOTIFICATION_GET).
1036 */
1037 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001038 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001039}
1040
J-Alvese8c8c2b2022-12-16 15:34:48 +00001041/**
1042 * Change the state of mailbox to empty, such that the ownership is given to the
1043 * Partition manager.
Karl Meakin1064a9c2024-06-04 17:20:43 +01001044 * Returns FFA_SUCCESS if the mailbox was reset successfully, FFA_ERROR
1045 * otherwise.
J-Alvese8c8c2b2022-12-16 15:34:48 +00001046 */
Karl Meakin1064a9c2024-06-04 17:20:43 +01001047static struct ffa_value api_release_mailbox(struct vm_locked vm_locked)
J-Alvese8c8c2b2022-12-16 15:34:48 +00001048{
Karl Meakin1064a9c2024-06-04 17:20:43 +01001049 struct ffa_value ret = {.func = FFA_SUCCESS_32};
J-Alves19e20cf2023-08-02 12:48:55 +01001050 ffa_id_t vm_id = vm_locked.vm->id;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001051
1052 switch (vm_locked.vm->mailbox.state) {
1053 case MAILBOX_STATE_EMPTY:
1054 dlog_verbose("Mailbox of %x is empty.\n", vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001055 ret = ffa_error(FFA_DENIED);
J-Alves31f8bef2023-07-14 12:46:28 +01001056 break;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001057 case MAILBOX_STATE_FULL:
1058 /* Check it doesn't have pending RX full notifications. */
1059 if (vm_are_fwk_notifications_pending(vm_locked)) {
1060 dlog_verbose(
1061 "Mailbox of endpoint %x has pending "
1062 "messages.\n",
1063 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001064 ret = ffa_error(FFA_DENIED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001065 }
1066 break;
1067 case MAILBOX_STATE_OTHER_WORLD_OWNED:
1068 /*
1069 * The SPMC shouldn't let SP's mailbox get into this state.
1070 * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst
1071 * the mailbox is in this state. In that case, we should report
1072 * error.
1073 */
1074 if (vm_id_is_current_world(vm_id)) {
1075 dlog_verbose(
Karl Meakin1064a9c2024-06-04 17:20:43 +01001076 "Mailbox of endpoint %x is in an incorrect "
1077 "state.\n",
J-Alvese8c8c2b2022-12-16 15:34:48 +00001078 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001079 ret = ffa_error(FFA_ABORTED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001080 }
1081 break;
1082 }
1083
Karl Meakin1064a9c2024-06-04 17:20:43 +01001084 if (ret.func == FFA_SUCCESS_32) {
1085 vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001086 }
1087
Karl Meakin1064a9c2024-06-04 17:20:43 +01001088 return ret;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001089}
1090
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001091/*
1092 * Helper to check if extended arguments (corresponding to regs x8-x17)
1093 * are zeroed out.
1094 */
Kathleen Capella036cc592023-11-30 18:26:15 -05001095bool api_extended_args_are_zero(struct ffa_value *args)
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001096{
1097 if (args->extended_val.arg8 != 0U || args->extended_val.arg9 != 0U ||
1098 args->extended_val.arg10 != 0U || args->extended_val.arg11 != 0U ||
1099 args->extended_val.arg12 != 0U || args->extended_val.arg13 != 0U ||
1100 args->extended_val.arg14 != 0U || args->extended_val.arg15 != 0U ||
1101 args->extended_val.arg16 != 0U || args->extended_val.arg17 != 0U) {
1102 return false;
1103 }
1104 return true;
1105}
1106
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001107static void api_ffa_msg_wait_rx_release(struct vcpu *current)
1108{
1109 struct vm_locked vm_locked;
1110
1111 vm_locked = plat_ffa_vm_find_locked(current->vm->id);
1112 if (vm_locked.vm == NULL) {
1113 return;
1114 }
1115
1116 api_release_mailbox(vm_locked);
1117
1118 if (vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY) {
1119 dlog_warning("Mailbox not released to producer\n");
1120 }
1121
1122 vm_unlock(&vm_locked);
1123}
1124
Kathleen Capella7253bd52024-08-14 17:36:09 -04001125static bool api_retain_rx_buffer_ownership(struct ffa_value args)
1126{
1127 return ((args.arg2 & FFA_MSG_WAIT_FLAG_RETAIN_RX) != 0U);
1128}
1129
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001130struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
1131 struct ffa_value *args)
1132{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001133 struct vcpu_locked current_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001134 enum vcpu_state next_state = VCPU_STATE_RUNNING;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001135 struct ffa_value ret;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001136 struct vcpu_locked next_locked = (struct vcpu_locked){
1137 .vcpu = NULL,
1138 };
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001139
Kathleen Capella7253bd52024-08-14 17:36:09 -04001140 if (args->arg1 != 0U || args->arg3 != 0U || args->arg4 != 0U ||
1141 args->arg5 != 0U || args->arg6 != 0U || args->arg7 != 0U) {
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001142 return ffa_error(FFA_INVALID_PARAMETERS);
1143 }
1144
Kathleen Capella7253bd52024-08-14 17:36:09 -04001145 if (current->vm->ffa_version >= FFA_VERSION_1_2) {
1146 if (!api_extended_args_are_zero(args)) {
1147 return ffa_error(FFA_INVALID_PARAMETERS);
1148 }
1149 } else {
1150 if (args->arg2 != 0U) {
1151 return ffa_error(FFA_INVALID_PARAMETERS);
1152 }
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001153 }
1154
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001155 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001156 if (!plat_ffa_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001157 current_locked, current->vm->id, HF_INVALID_VM_ID,
1158 next_locked, FFA_MSG_WAIT_32, &next_state)) {
1159 ret = ffa_error(FFA_DENIED);
1160 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001161 }
1162
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001163 assert(!vm_id_is_current_world(current->vm->id) ||
1164 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001165
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001166 ret = plat_ffa_msg_wait_prepare(current_locked, next);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001167
1168 /*
1169 * To maintain partial ordering of locks, release vCPU lock before
1170 * releasing the VM's RX buffer, a process which requires locking the
1171 * VM.
1172 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001173out:
1174 vcpu_unlock(&current_locked);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001175
Kathleen Capella7253bd52024-08-14 17:36:09 -04001176 if (ret.func != FFA_ERROR_32 &&
1177 !api_retain_rx_buffer_ownership(*args)) {
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001178 api_ffa_msg_wait_rx_release(current);
1179 }
J-Alvese8c8c2b2022-12-16 15:34:48 +00001180 return ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001181}
1182
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001183/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001184 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001185 * value needs to be forced onto the vCPU.
1186 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001187static bool api_vcpu_prepare_run(struct vcpu_locked current_locked,
1188 struct vcpu_locked vcpu_next_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001189 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001190{
Max Shvetsov40108e72020-08-27 12:39:50 +01001191 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001192 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001193 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001194 bool vcpu_was_init_state = false;
J-Alves6e2abc62021-12-02 14:58:56 +00001195 bool need_vm_lock;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001196 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001197
Andrew Scullb06d1752019-02-04 10:15:48 +00001198 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001199 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +00001200 *
Andrew Scull4caadaf2019-07-03 13:13:47 +01001201 * The VM lock is not needed in the common case so it must only be taken
1202 * when it is going to be needed. This ensures there are no inter-vCPU
1203 * dependencies in the common run case meaning the sensitive context
1204 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +00001205 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001206 struct vcpu *vcpu = vcpu_next_locked.vcpu;
1207 struct vcpu *current = current_locked.vcpu;
Max Shvetsov40108e72020-08-27 12:39:50 +01001208
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001209 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +00001210 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
1211 (!vcpu->vm->el0_partition &&
1212 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
1213 vcpu->state == VCPU_STATE_BLOCKED ||
1214 vcpu->state == VCPU_STATE_PREEMPTED));
1215
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001216 if (need_vm_lock) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001217 vcpu_unlock(&vcpu_next_locked);
1218 vcpu_unlock(&current_locked);
Max Shvetsov40108e72020-08-27 12:39:50 +01001219 vm_locked = vm_lock(vcpu->vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001220
1221 /* Lock both vCPUs at once to avoid deadlock. */
1222 vcpus_locked = vcpu_lock_both(current, vcpu);
1223 current_locked = vcpus_locked.vcpu1;
1224 vcpu_next_locked = vcpus_locked.vcpu2;
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001225 }
1226
1227 /*
1228 * If the vCPU is already running somewhere then we can't run it here
1229 * simultaneously. While it is actually running then the state should be
1230 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
1231 * stops running but while Hafnium is in the process of switching back
1232 * to the primary there will be a brief period while the state has been
1233 * updated but `regs_available` is still false (until
1234 * `api_regs_state_saved` is called). We can't start running it again
1235 * until this has finished, so count this state as still running for the
1236 * purposes of this check.
1237 */
1238 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
1239 /*
1240 * vCPU is running on another pCPU.
1241 *
1242 * It's okay not to return the sleep duration here because the
1243 * other physical CPU that is currently running this vCPU will
1244 * return the sleep duration if needed.
1245 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001246 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001247 ret = false;
1248 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001249 }
Andrew Scull9726c252019-01-23 13:44:19 +00001250
1251 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001252 if (vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04001253 dlog_verbose("VM %#x was aborted, cannot run vCPU %u\n",
1254 vcpu->vm->id, vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +01001255 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +00001256 }
Kathleen Capella6ab05132023-05-10 12:27:35 -04001257 *run_ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +00001258 ret = false;
1259 goto out;
1260 }
1261
Andrew Walbran508e63c2018-12-20 17:02:37 +00001262 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001263 case VCPU_STATE_RUNNING:
1264 case VCPU_STATE_OFF:
1265 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001266 ret = false;
1267 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001268
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001269 case VCPU_STATE_WAITING:
1270 /*
Olivier Deprezb2808332023-02-02 15:25:40 +01001271 * An initial FFA_RUN is necessary for SP's secondary vCPUs to
1272 * reach the message wait loop.
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001273 */
Olivier Deprezb2808332023-02-02 15:25:40 +01001274 if (vcpu->rt_model == RTM_SP_INIT) {
1275 /*
1276 * TODO: this should be removed, but omitting it makes
1277 * normal world arch gicv3 tests failing.
1278 */
1279 vcpu->rt_model = RTM_NONE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001280
1281 vcpu_was_init_state = true;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001282 break;
1283 }
1284
J-Alves6e2abc62021-12-02 14:58:56 +00001285 assert(need_vm_lock == true);
1286 if (!vm_locked.vm->el0_partition &&
1287 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001288 vcpu_next_locked, current_locked, vm_locked)) {
Federico Recanati9dccc4b2022-04-27 12:52:36 +02001289 /* TODO: setting a return value to override
1290 * the placeholder (FFA_ERROR(INTERRUPTED))
1291 * set by FFA_MSG_WAIT. FF-A v1.1 allows
1292 * FFA_MSG_WAIT to successfully return even if
1293 * it didn't receive a message. TFTF tests are
1294 * still expecting an FFA_ERROR instead,
1295 * should be fixed?
1296 */
1297 arch_regs_set_retval(
1298 &vcpu->regs,
1299 (struct ffa_value){.func = FFA_RUN_32,
1300 // TODO: does it make sense
1301 // to set vCPU/receiver?
1302 .arg1 = 0});
J-Alves6e2abc62021-12-02 14:58:56 +00001303 break;
1304 }
1305
Andrew Scullb06d1752019-02-04 10:15:48 +00001306 /*
1307 * A pending message allows the vCPU to run so the message can
1308 * be delivered directly.
1309 */
J-Alvese8c8c2b2022-12-16 15:34:48 +00001310 if (vcpu->vm->mailbox.state == MAILBOX_STATE_FULL) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001311 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001312 ffa_msg_recv_return(vcpu->vm));
Andrew Scullb06d1752019-02-04 10:15:48 +00001313 break;
1314 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001315
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001316 if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001317 break;
1318 }
1319
1320 if (arch_timer_enabled(&vcpu->regs)) {
1321 timer_remaining_ns =
1322 arch_timer_remaining_ns(&vcpu->regs);
1323 if (timer_remaining_ns == 0) {
1324 break;
1325 }
1326 } else {
1327 dlog_verbose("Timer disabled\n");
1328 }
1329 run_ret->func = FFA_MSG_WAIT_32;
1330 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1331 run_ret->arg2 = timer_remaining_ns;
1332 ret = false;
1333 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001334 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +00001335 if (need_vm_lock &&
1336 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001337 vcpu_next_locked, current_locked, vm_locked)) {
1338 assert(vcpu_interrupt_count_get(vcpu_next_locked) > 0);
J-Alves6e2abc62021-12-02 14:58:56 +00001339 break;
1340 }
1341
Andrew Scullb06d1752019-02-04 10:15:48 +00001342 /* Allow virtual interrupts to be delivered. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001343 if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +00001344 break;
1345 }
1346
Andrew Walbran508e63c2018-12-20 17:02:37 +00001347 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001348 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001349 arch_timer_remaining_ns(&vcpu->regs);
1350
1351 /*
1352 * The timer expired so allow the interrupt to be
1353 * delivered.
1354 */
1355 if (timer_remaining_ns == 0) {
1356 break;
1357 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001358 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001359
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001360 /*
1361 * The vCPU is not ready to run, return the appropriate code to
1362 * the primary which called vcpu_run.
1363 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001364 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001365 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1366 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +00001367
1368 ret = false;
1369 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001370
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001371 case VCPU_STATE_BLOCKED:
1372 /* A blocked vCPU is run unconditionally. Fall through. */
1373 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +00001374 /* Check NPI is to be injected here. */
1375 if (need_vm_lock) {
1376 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001377 vcpu_next_locked, current_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001378 }
Andrew Walbran508e63c2018-12-20 17:02:37 +00001379 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001380 default:
1381 /*
1382 * Execution not expected to reach here. Deny the request
1383 * gracefully.
1384 */
1385 *run_ret = ffa_error(FFA_DENIED);
1386 ret = false;
1387 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001388 }
1389
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001390 plat_ffa_init_schedule_mode_ffa_run(current_locked, vcpu_next_locked);
Madhukar Pappireddy1480fce2022-06-21 18:09:25 -05001391
Andrew Scullb06d1752019-02-04 10:15:48 +00001392 /* It has been decided that the vCPU should be run. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001393 vcpu->cpu = current_locked.vcpu->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001394 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001395
Olivier Deprez04168ed2022-09-26 09:20:00 +02001396 if (vcpu_was_init_state) {
1397 vcpu_set_phys_core_idx(vcpu);
1398 vcpu_set_boot_info_gp_reg(vcpu);
J-Alves7ac49052022-02-08 17:20:53 +00001399 }
J-Alves7ac49052022-02-08 17:20:53 +00001400
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001401 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001402 * Mark the registers as unavailable now that we're about to reflect
1403 * them onto the real registers. This will also prevent another physical
1404 * CPU from trying to read these registers.
1405 */
1406 vcpu->regs_available = false;
1407
1408 ret = true;
1409
1410out:
Andrew Scullb06d1752019-02-04 10:15:48 +00001411 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +01001412 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001413 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001414 return ret;
1415}
1416
J-Alves19e20cf2023-08-02 12:48:55 +01001417struct ffa_value api_ffa_run(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001418 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001419{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001420 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001421 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001422 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001423 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001424 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001425 struct vcpu_locked vcpu_next_locked;
1426 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001427
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001428 current_locked = vcpu_lock(current);
1429 if (!plat_ffa_run_checks(current_locked, vm_id, vcpu_idx, &ret, next)) {
1430 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001431 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001432
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001433 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001434 goto out;
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001435 }
1436
Andrew Scull19503262018-09-20 14:48:39 +01001437 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001438 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +01001439 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001440 goto out;
Andrew Scull19503262018-09-20 14:48:39 +01001441 }
1442
Fuad Tabbaed294af2019-12-20 10:43:01 +00001443 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001444 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001445 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001446 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001447
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001448 /*
1449 * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC
1450 * bypasses the intermediate execution contexts and resumes the
1451 * SP execution context that was originally preempted.
1452 */
1453 if (*next != NULL) {
1454 vcpu = *next;
1455 } else {
1456 vcpu = vm_get_vcpu(vm, vcpu_idx);
1457 }
1458
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001459 /*
1460 * Unlock current vCPU to allow it to be locked together with next
1461 * vcpu.
1462 */
1463 vcpu_unlock(&current_locked);
1464
1465 /* Lock both vCPUs at once to avoid deadlock. */
1466 vcpus_locked = vcpu_lock_both(current, vcpu);
1467 current_locked = vcpus_locked.vcpu1;
1468 vcpu_next_locked = vcpus_locked.vcpu2;
1469
1470 if (!plat_ffa_check_runtime_state_transition(
1471 current_locked, current->vm->id, HF_INVALID_VM_ID,
1472 vcpu_next_locked, FFA_RUN_32, &next_state)) {
1473 ret = ffa_error(FFA_DENIED);
1474 goto out_vcpu;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001475 }
1476
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001477 if (!api_vcpu_prepare_run(current_locked, vcpu_next_locked, &ret)) {
1478 goto out_vcpu;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001479 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001480
Andrew Walbran508e63c2018-12-20 17:02:37 +00001481 /*
1482 * Inject timer interrupt if timer has expired. It's safe to access
1483 * vcpu->regs here because api_vcpu_prepare_run already made sure that
1484 * regs_available was true (and then set it to false) before returning
1485 * true.
1486 */
1487 if (arch_timer_pending(&vcpu->regs)) {
1488 /* Make virtual timer interrupt pending. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001489 api_interrupt_inject_locked(vcpu_next_locked,
1490 HF_VIRTUAL_TIMER_INTID,
1491 vcpu_next_locked, NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001492
1493 /*
1494 * Set the mask bit so the hardware interrupt doesn't fire
1495 * again. Ideally we wouldn't do this because it affects what
1496 * the secondary vCPU sees, but if we don't then we end up with
1497 * a loop of the interrupt firing each time we try to return to
1498 * the secondary vCPU.
1499 */
1500 arch_timer_mask(&vcpu->regs);
1501 }
1502
Fuad Tabbaed294af2019-12-20 10:43:01 +00001503 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001504 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001505
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001506 assert(!vm_id_is_current_world(current->vm->id) ||
1507 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001508 current->state = VCPU_STATE_BLOCKED;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001509
Andrew Scull33fecd32019-01-08 14:48:27 +00001510 /*
1511 * Set a placeholder return code to the scheduler. This will be
1512 * overwritten when the switch back to the primary occurs.
1513 */
J-Alvesbd32c972024-02-02 16:20:04 +00001514 ret = api_ffa_interrupt_return(0);
Andrew Scull33fecd32019-01-08 14:48:27 +00001515
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001516out_vcpu:
1517 vcpu_unlock(&vcpu_next_locked);
1518
Andrew Scull6d2db332018-10-10 15:28:17 +01001519out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001520 vcpu_unlock(&current_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001521 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001522}
1523
1524/**
Andrew Scull81e85092018-12-12 12:56:20 +00001525 * Check that the mode indicates memory that is valid, owned and exclusive.
1526 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001527static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001528{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001529 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1530 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001531}
1532
1533/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001534 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001535 */
Karl Meakin738bee12024-01-12 15:19:45 +00001536static struct ffa_value api_vm_configure_stage1(
1537 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1538 paddr_t pa_send_begin, paddr_t pa_send_end, paddr_t pa_recv_begin,
1539 paddr_t pa_recv_end, uint32_t extra_attributes,
1540 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001541{
Karl Meakin738bee12024-01-12 15:19:45 +00001542 struct ffa_value ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001543
Karl Meakin738bee12024-01-12 15:19:45 +00001544 /*
1545 * Map the send page as read-only in the SPMC/hypervisor address space.
1546 */
Andrew Sculle1322792019-07-01 17:46:10 +01001547 vm_locked.vm->mailbox.send =
1548 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001549 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001550 if (!vm_locked.vm->mailbox.send) {
Karl Meakin738bee12024-01-12 15:19:45 +00001551 ret = ffa_error(FFA_NO_MEMORY);
1552 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001553 }
1554
1555 /*
Karl Meakin738bee12024-01-12 15:19:45 +00001556 * Map the receive page as writable in the SPMC/hypervisor address
1557 * space. On failure, unmap the send page before returning.
Andrew Sculle1322792019-07-01 17:46:10 +01001558 */
1559 vm_locked.vm->mailbox.recv =
1560 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001561 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001562 if (!vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001563 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001564 goto fail_undo_send;
1565 }
1566
Karl Meakin738bee12024-01-12 15:19:45 +00001567 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001568 goto out;
1569
1570 /*
1571 * The following mappings will not require more memory than is available
1572 * in the local pool.
1573 */
1574fail_undo_send:
1575 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001576 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1577 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001578
Andrew Sculle1322792019-07-01 17:46:10 +01001579out:
Andrew Sculle1322792019-07-01 17:46:10 +01001580 return ret;
1581}
1582
1583/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001584 * Sanity checks and configures the send and receive pages in the VM stage-2
1585 * and hypervisor stage-1 page tables.
1586 *
1587 * Returns:
1588 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001589 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001590 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1591 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001592 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001593 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001594 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001595
1596struct ffa_value api_vm_configure_pages(
1597 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1598 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1599 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001600{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001601 struct ffa_value ret;
1602 paddr_t pa_send_begin;
1603 paddr_t pa_send_end;
1604 paddr_t pa_recv_begin;
1605 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001606 uint32_t orig_send_mode = 0;
1607 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001608 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001609
1610 /* We only allow these to be setup once. */
1611 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001612 dlog_error("%s: Mailboxes have already been setup for VM %#x\n",
1613 __func__, vm_locked.vm->id);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001614 ret = ffa_error(FFA_DENIED);
1615 goto out;
1616 }
1617
1618 /* Hafnium only supports a fixed size of RX/TX buffers. */
1619 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
Karl Meakine8937d92024-03-19 16:04:25 +00001620 dlog_error("%s: Page count must be %zu, it is %d\n", __func__,
Karl Meakin738bee12024-01-12 15:19:45 +00001621 HF_MAILBOX_SIZE / FFA_PAGE_SIZE, page_count);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001622 ret = ffa_error(FFA_INVALID_PARAMETERS);
1623 goto out;
1624 }
1625
1626 /* Fail if addresses are not page-aligned. */
1627 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1628 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001629 dlog_error("%s: Mailbox buffers not page-aligned\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001630 ret = ffa_error(FFA_INVALID_PARAMETERS);
1631 goto out;
1632 }
1633
1634 /* Convert to physical addresses. */
1635 pa_send_begin = pa_from_ipa(send);
1636 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1637 pa_recv_begin = pa_from_ipa(recv);
1638 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1639
1640 /* Fail if the same page is used for the send and receive pages. */
1641 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001642 dlog_error("%s: Mailbox buffers overlap\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001643 ret = ffa_error(FFA_INVALID_PARAMETERS);
1644 goto out;
1645 }
Andrew Sculle1322792019-07-01 17:46:10 +01001646
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001647 /* Set stage 2 translation tables only for virtual FF-A instances. */
1648 if (vm_id_is_current_world(vm_locked.vm->id)) {
1649 /*
1650 * Ensure the pages are valid, owned and exclusive to the VM and
1651 * that the VM has the required access to the memory.
1652 */
1653 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1654 &orig_send_mode) ||
1655 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1656 (orig_send_mode & MM_MODE_R) == 0 ||
1657 (orig_send_mode & MM_MODE_W) == 0) {
1658 dlog_error(
1659 "VM doesn't have required access rights to map "
1660 "TX buffer in stage 2.\n");
1661 ret = ffa_error(FFA_INVALID_PARAMETERS);
1662 goto out;
1663 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001664
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001665 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1666 &orig_recv_mode) ||
1667 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1668 (orig_recv_mode & MM_MODE_R) == 0) {
1669 dlog_error(
1670 "VM doesn't have required access rights to map "
1671 "RX buffer in stage 2.\n");
1672 ret = ffa_error(FFA_INVALID_PARAMETERS);
1673 goto out;
1674 }
Andrew Sculle1322792019-07-01 17:46:10 +01001675
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001676 /* Take memory ownership away from the VM and mark as shared. */
1677 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1678 MM_MODE_W;
1679 if (vm_locked.vm->el0_partition) {
1680 mode |= MM_MODE_USER | MM_MODE_NG;
1681 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001682
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001683 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1684 mode, local_page_pool, NULL)) {
1685 dlog_error(
1686 "Cannot allocate a new entry in stage 2 "
1687 "translation table.\n");
1688 ret = ffa_error(FFA_NO_MEMORY);
1689 goto out;
1690 }
Andrew Sculle1322792019-07-01 17:46:10 +01001691
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001692 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1693 if (vm_locked.vm->el0_partition) {
1694 mode |= MM_MODE_USER | MM_MODE_NG;
1695 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001696
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001697 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1698 mode, local_page_pool, NULL)) {
1699 /* TODO: partial defrag of failed range. */
1700 /* Recover any memory consumed in failed mapping. */
Karl Meakin738bee12024-01-12 15:19:45 +00001701 dlog_error("%s: cannot map recv page\n", __func__);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001702 vm_ptable_defrag(vm_locked, local_page_pool);
Karl Meakin738bee12024-01-12 15:19:45 +00001703 ret = ffa_error(FFA_NO_MEMORY);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001704 goto fail_undo_send;
1705 }
Karl Meakin738bee12024-01-12 15:19:45 +00001706 } else {
1707 ret = arch_other_world_vm_configure_rxtx_map(
1708 vm_locked, local_page_pool, pa_send_begin, pa_send_end,
1709 pa_recv_begin, pa_recv_end);
1710 if (ret.func != FFA_SUCCESS_32) {
1711 goto out;
1712 }
Andrew Sculle1322792019-07-01 17:46:10 +01001713 }
1714
Olivier Deprez96a2a262020-06-11 17:21:38 +02001715 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1716 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1717
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001718 /*
1719 * For EL0 partitions, since both the partition and the hypervisor code
1720 * use the EL2&0 translation regime, it is critical to mark the mappings
1721 * of the send and recv buffers as non-global in the TLB. For one, if we
1722 * dont mark it as non-global, it would cause TLB conflicts since there
1723 * would be an identity mapping with non-global attribute in the
1724 * partitions page tables, but another identity mapping in the
1725 * hypervisor page tables with the global attribute. The other issue is
1726 * one of security, we dont want other partitions to be able to access
1727 * other partitions buffers through cached translations.
1728 */
1729 if (vm_locked.vm->el0_partition) {
1730 extra_attributes |= MM_MODE_NG;
1731 }
1732
Karl Meakin738bee12024-01-12 15:19:45 +00001733 ret = api_vm_configure_stage1(
1734 mm_stage1_locked, vm_locked, pa_send_begin, pa_send_end,
1735 pa_recv_begin, pa_recv_end, extra_attributes, local_page_pool);
1736 if (ret.func != FFA_SUCCESS_32) {
Andrew Sculle1322792019-07-01 17:46:10 +01001737 goto fail_undo_send_and_recv;
1738 }
1739
Manish Pandeyd34f8892020-06-19 17:41:07 +01001740 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001741 goto out;
1742
Andrew Sculle1322792019-07-01 17:46:10 +01001743fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001744 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001745 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001746
1747fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001748 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001749 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001750
1751out:
Andrew Sculle1322792019-07-01 17:46:10 +01001752 return ret;
1753}
1754
Karl Meakinb9705e22024-04-05 13:58:28 +01001755/**
1756 * Read the buffer addresses and VM ID of an FFA_RXTX_MAP request. Handles
1757 * forwarded messages by reading from the hypervisor's TX buffer.
1758 *
1759 * Returns the VM/SP ID on success.
1760 *
1761 * Returns `HF_INVALID_VM_ID` when the arguments provided via the ABI
1762 * FFA_RXTX_MAP indicate the SPMC should retrieve the RXTX description from the
1763 * Hypervisor RXTX buffers, and the hypervisor hasn't given its own RXTX buffers
1764 * for the SPMC to map.
1765 */
1766static ffa_id_t api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send,
1767 ipaddr_t *recv, uint32_t *page_count)
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001768{
Karl Meakinb9705e22024-04-05 13:58:28 +01001769 bool forwarded;
1770 struct vm_locked vm_locked;
1771 ffa_id_t owner_vm_id;
1772 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc;
1773 struct ffa_composite_memory_region *rx_region;
1774 struct ffa_composite_memory_region *tx_region;
1775
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001776 /*
1777 * If the message has been forwarded the effective addresses are in
1778 * hypervisor's TX buffer.
1779 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001780 forwarded = (current_vm->id == HF_OTHER_WORLD_ID) &&
1781 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1782 (*page_count == 0);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001783
1784 if (forwarded) {
Karl Meakinb9705e22024-04-05 13:58:28 +01001785 vm_locked = vm_lock(current_vm);
1786 endpoint_desc = (struct ffa_endpoint_rx_tx_descriptor *)
1787 vm_locked.vm->mailbox.send;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001788
Karl Meakinb9705e22024-04-05 13:58:28 +01001789 if (endpoint_desc == NULL) {
1790 dlog_error(
1791 "Trying to access RXTX description, but "
1792 "hypervisor has not provided RXTX buffers\n");
1793 vm_unlock(&vm_locked);
1794 return HF_INVALID_VM_ID;
1795 }
1796
1797 rx_region = ffa_endpoint_get_rx_memory_region(endpoint_desc);
1798 tx_region = ffa_endpoint_get_tx_memory_region(endpoint_desc);
1799
1800 owner_vm_id = endpoint_desc->endpoint_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001801 *recv = ipa_init(rx_region->constituents[0].address);
1802 *send = ipa_init(tx_region->constituents[0].address);
1803 *page_count = rx_region->constituents[0].page_count;
J-Alves28ad9b42022-12-08 12:19:43 +00001804
1805 vm_unlock(&vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001806 } else {
Karl Meakinb9705e22024-04-05 13:58:28 +01001807 owner_vm_id = current_vm->id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001808 }
Karl Meakinb9705e22024-04-05 13:58:28 +01001809
1810 return owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001811}
Karl Meakinb9705e22024-04-05 13:58:28 +01001812
Andrew Sculle1322792019-07-01 17:46:10 +01001813/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001814 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001815 * must not be shared. Locking of the page tables combined with a local memory
1816 * pool ensures there will always be enough memory to recover from any errors
1817 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1818 * by another core which could result in this transaction being unable to roll
1819 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001820 *
1821 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001822 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001823 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001824 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001825 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001826 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001827 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001828 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001829struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001830 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001831{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001832 struct ffa_value ret;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001833 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001834 struct mm_stage1_locked mm_stage1_locked;
1835 struct mpool local_page_pool;
J-Alves19e20cf2023-08-02 12:48:55 +01001836 ffa_id_t owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001837
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001838 /*
1839 * Get the original buffer addresses and VM ID in case of forwarded
1840 * message.
1841 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001842 owner_vm_id = api_get_rxtx_description(current->vm, &send, &recv,
1843 &page_count);
1844 if (owner_vm_id == HF_INVALID_VM_ID) {
1845 return ffa_error(FFA_INVALID_PARAMETERS);
1846 }
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001847
1848 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1849 if (owner_vm_locked.vm == NULL) {
1850 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1851 owner_vm_id);
1852 return ffa_error(FFA_DENIED);
1853 }
Andrew Scull220e6212018-12-21 18:09:00 +00001854
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001855 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001856 * Create a local pool so any freed memory can't be used by another
1857 * thread. This is to ensure the original mapping can be restored if any
1858 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001859 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001860 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1861
Manish Pandeyd34f8892020-06-19 17:41:07 +01001862 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001863
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001864 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1865 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001866 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001867 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001868 }
1869
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001870 /* Forward buffer mapping to SPMC if coming from a VM. */
1871 plat_ffa_rxtx_map_forward(owner_vm_locked);
1872
Federico Recanati9f1b6532022-04-14 13:15:28 +02001873 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001874
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001875exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001876 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001877 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001878 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001879
1880 return ret;
1881}
1882
1883/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001884 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1885 * translation regime of the caller. Unmap the region for the hypervisor and
1886 * set the memory region to owned and exclusive for the component. Since the
1887 * memory region mapped in the page table, when the buffers were originally
1888 * created we can safely remap it.
1889 *
1890 * Returns:
1891 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1892 * behalf of the caller.
1893 * - FFA_SUCCESS on success if no further action is needed.
1894 */
J-Alves19e20cf2023-08-02 12:48:55 +01001895struct ffa_value api_ffa_rxtx_unmap(ffa_id_t allocator_id, struct vcpu *current)
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001896{
1897 struct vm *vm = current->vm;
1898 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001899 ffa_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001900 struct mm_stage1_locked mm_stage1_locked;
1901 paddr_t send_pa_begin;
1902 paddr_t send_pa_end;
1903 paddr_t recv_pa_begin;
1904 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001905 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001906
J-Alves661e1b72023-08-02 13:39:40 +01001907 if (vm->id == HF_HYPERVISOR_VM_ID && !ffa_is_vm_id(allocator_id)) {
Olivier Deprez106c8ce2024-02-08 11:49:02 +01001908 dlog_verbose(
J-Alves9bd324a2023-01-12 10:52:52 +00001909 "The Hypervisor must specify a valid VM ID in register "
1910 "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n");
1911 return ffa_error(FFA_INVALID_PARAMETERS);
1912 }
1913
Federico Recanati8da9e332022-02-10 11:00:17 +01001914 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1915 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
J-Alves9bd324a2023-01-12 10:52:52 +00001916 dlog_error(
1917 "The register W1 (containing ID) must be 0 at virtual "
1918 "instances.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001919 return ffa_error(FFA_INVALID_PARAMETERS);
1920 }
1921
1922 /* VM ID of which buffers have to be unmapped. */
1923 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1924
1925 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1926 vm = vm_locked.vm;
1927 if (vm == NULL) {
1928 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1929 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001930 return ffa_error(FFA_INVALID_PARAMETERS);
1931 }
1932
1933 /* Get send and receive buffers. */
1934 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001935 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001936 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001937 ret = ffa_error(FFA_INVALID_PARAMETERS);
1938 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001939 }
1940
1941 /* Currently a mailbox size of 1 page is assumed. */
1942 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1943 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1944 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1945 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1946
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001947 mm_stage1_locked = mm_lock_stage1();
1948
Federico Recanati8da9e332022-02-10 11:00:17 +01001949 /* Reset stage 2 mapping only for virtual FF-A instances. */
1950 if (vm_id_is_current_world(owner_vm_id)) {
1951 /*
1952 * Set the memory region of the buffers back to the default mode
1953 * for the VM. Since this memory region was already mapped for
1954 * the RXTX buffers we can safely remap them.
1955 */
1956 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1957 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1958 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001959
Federico Recanati8da9e332022-02-10 11:00:17 +01001960 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1961 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1962 &api_page_pool, NULL));
Karl Meakin738bee12024-01-12 15:19:45 +00001963 } else {
1964 ret = arch_other_world_vm_configure_rxtx_unmap(
1965 vm_locked, &api_page_pool, send_pa_begin, send_pa_end,
1966 recv_pa_begin, recv_pa_end);
1967 if (ret.func != FFA_SUCCESS_32) {
1968 goto out;
1969 }
Federico Recanati8da9e332022-02-10 11:00:17 +01001970 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001971
1972 /* Unmap the buffers in the partition manager. */
1973 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1974 &api_page_pool));
1975 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1976 &api_page_pool));
1977
1978 vm->mailbox.send = NULL;
1979 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001980 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001981
Federico Recanati8da9e332022-02-10 11:00:17 +01001982 /* Forward buffer unmapping to SPMC if coming from a VM. */
J-Alves70079932022-12-07 17:32:20 +00001983 plat_ffa_rxtx_unmap_forward(vm_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001984
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001985 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001986
1987out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001988 vm_unlock(&vm_locked);
1989
Federico Recanati8da9e332022-02-10 11:00:17 +01001990 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001991}
1992
1993/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001994 * Copies data from the sender's send buffer to the recipient's receive buffer
1995 * and notifies the receiver.
1996 */
J-Alves19e20cf2023-08-02 12:48:55 +01001997struct ffa_value api_ffa_msg_send2(ffa_id_t sender_vm_id, uint32_t flags,
Federico Recanati25053ee2022-03-14 15:01:53 +01001998 struct vcpu *current)
1999{
2000 struct vm *from = current->vm;
2001 struct vm *to;
2002 struct vm_locked to_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01002003 ffa_id_t msg_sender_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01002004 struct vm_locked sender_locked;
2005 const void *from_msg;
2006 struct ffa_value ret;
J-Alves19e20cf2023-08-02 12:48:55 +01002007 ffa_id_t sender_id;
2008 ffa_id_t receiver_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01002009 uint32_t msg_size;
Federico Recanati25053ee2022-03-14 15:01:53 +01002010
Karl Meakina5ea9092024-05-28 15:40:33 +01002011 alignas(8) struct ffa_partition_rxtx_header header;
2012
Federico Recanati25053ee2022-03-14 15:01:53 +01002013 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
2014 if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) {
2015 dlog_error("Sender VM ID must be zero.\n");
2016 return ffa_error(FFA_INVALID_PARAMETERS);
2017 }
2018
Federico Recanati25053ee2022-03-14 15:01:53 +01002019 /*
2020 * Get message sender's mailbox, which can be different to the `from` vm
2021 * when the message is forwarded.
2022 */
2023 msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id;
2024 sender_locked = plat_ffa_vm_find_locked(msg_sender_id);
2025 if (sender_locked.vm == NULL) {
2026 dlog_error("Cannot send message from VM ID %#x, not found.\n",
2027 msg_sender_id);
2028 return ffa_error(FFA_DENIED);
2029 }
2030
2031 from_msg = sender_locked.vm->mailbox.send;
2032 if (from_msg == NULL) {
2033 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
2034 msg_sender_id);
2035 ret = ffa_error(FFA_DENIED);
2036 goto out_unlock_sender;
2037 }
2038
2039 /*
2040 * Copy message header as safety measure to avoid multiple accesses to
2041 * unsafe memory which could be 'corrupted' between safety checks and
2042 * final buffer copy.
2043 */
J-Alves8a5fd9d2024-04-09 11:22:49 +01002044 if (!memcpy_trapped(&header, FFA_RXTX_HEADER_SIZE, from_msg,
2045 FFA_RXTX_HEADER_SIZE)) {
2046 dlog_error(
2047 "%s: Failed to copy message from sender's(%x) TX "
2048 "buffer.\n",
2049 __func__, sender_locked.vm->id);
2050 ret = ffa_error(FFA_ABORTED);
2051 goto out_unlock_sender;
2052 }
2053
Federico Recanati25053ee2022-03-14 15:01:53 +01002054 sender_id = ffa_rxtx_header_sender(&header);
2055 receiver_id = ffa_rxtx_header_receiver(&header);
2056
2057 /* Ensure Sender IDs from API and from message header match. */
2058 if (msg_sender_id != sender_id) {
2059 dlog_error(
2060 "Message sender VM ID (%#x) doesn't match header's VM "
2061 "ID (%#x).\n",
2062 msg_sender_id, sender_id);
2063 ret = ffa_error(FFA_INVALID_PARAMETERS);
2064 goto out_unlock_sender;
2065 }
2066
2067 /* Disallow reflexive requests as this suggests an error in the VM. */
2068 if (receiver_id == sender_id) {
2069 dlog_error("Sender and receive VM IDs must be different.\n");
2070 ret = ffa_error(FFA_INVALID_PARAMETERS);
2071 goto out_unlock_sender;
2072 }
2073
Federico Recanati7b39ff22022-03-14 15:01:53 +01002074 /* `flags` can be set only at secure virtual FF-A instances. */
J-Alves661e1b72023-08-02 13:39:40 +01002075 if (ffa_is_vm_id(sender_id) && (flags != 0)) {
Federico Recanati7b39ff22022-03-14 15:01:53 +01002076 dlog_error("flags must be zero.\n");
2077 return ffa_error(FFA_INVALID_PARAMETERS);
2078 }
2079
Olivier Deprez37b75112024-03-04 18:27:30 +01002080 if (header.offset != FFA_RXTX_HEADER_SIZE) {
2081 dlog_error("Indirect msg payload must follow the header.\n");
2082 return ffa_error(FFA_INVALID_PARAMETERS);
2083 }
2084
Federico Recanati25053ee2022-03-14 15:01:53 +01002085 /*
2086 * Check if the message has to be forwarded to the SPMC, in
2087 * this case return, the SPMC will handle the buffer copy.
2088 */
2089 if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) {
2090 goto out_unlock_sender;
2091 }
2092
2093 /* Ensure the receiver VM exists. */
2094 to_locked = plat_ffa_vm_find_locked(receiver_id);
2095 to = to_locked.vm;
2096
2097 if (to == NULL) {
2098 dlog_error("Cannot deliver message to VM %#x, not found.\n",
2099 receiver_id);
2100 ret = ffa_error(FFA_INVALID_PARAMETERS);
2101 goto out_unlock_sender;
2102 }
2103
2104 /*
2105 * Check sender and receiver can use indirect messages.
2106 * Sender is the VM/SP who originally sent the message, not the
2107 * hypervisor possibly relaying it.
2108 */
2109 if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) {
2110 dlog_verbose("VM %#x doesn't support indirect message\n",
2111 sender_id);
2112 ret = ffa_error(FFA_DENIED);
2113 goto out;
2114 }
2115
J-Alvese8c8c2b2022-12-16 15:34:48 +00002116 if (vm_is_mailbox_busy(to_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002117 dlog_error(
2118 "Cannot deliver message to VM %#x, RX buffer not "
2119 "ready.\n",
2120 receiver_id);
2121 ret = ffa_error(FFA_BUSY);
2122 goto out;
2123 }
2124
Federico Recanati644f0462022-03-17 12:04:00 +01002125 /* Acquire receiver's RX buffer. */
2126 if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) {
2127 dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id);
2128 goto out;
2129 }
2130
Federico Recanati25053ee2022-03-14 15:01:53 +01002131 /* Check the size of transfer. */
2132 msg_size = FFA_RXTX_HEADER_SIZE + header.size;
Olivier Deprezf1dd1e12024-03-04 18:21:01 +01002133 if ((msg_size > FFA_MSG_PAYLOAD_MAX) ||
Federico Recanati25053ee2022-03-14 15:01:53 +01002134 (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
2135 dlog_error("Message is too big.\n");
2136 ret = ffa_error(FFA_INVALID_PARAMETERS);
2137 goto out;
2138 }
2139
2140 /* Copy data. */
J-Alves8a5fd9d2024-04-09 11:22:49 +01002141 if (!memcpy_trapped(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg,
2142 msg_size)) {
2143 dlog_error(
2144 "%s: Failed to copy message to receiver's(%x) RX "
2145 "buffer.\n",
2146 __func__, to->id);
2147 ret = ffa_error(FFA_ABORTED);
2148 goto out;
2149 }
2150
Federico Recanati25053ee2022-03-14 15:01:53 +01002151 to->mailbox.recv_size = msg_size;
2152 to->mailbox.recv_sender = sender_id;
2153 to->mailbox.recv_func = FFA_MSG_SEND2_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002154 to->mailbox.state = MAILBOX_STATE_FULL;
Federico Recanati25053ee2022-03-14 15:01:53 +01002155
J-Alves3bb82592023-01-23 11:20:54 +00002156 /*
2157 * Set framework notifications, only if the SP has enabled
2158 * receipt of notifications.
2159 * If VMs have provided the RX buffer it is implied they already
2160 * support indirect messaging, and therefore framework notifications.
2161 */
2162 if (ffa_is_vm_id(to_locked.vm->id) ||
2163 vm_are_notifications_enabled(to_locked.vm)) {
2164 ffa_notifications_bitmap_t rx_buffer_full =
2165 ffa_is_vm_id(sender_id)
2166 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
2167 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
Federico Recanati25053ee2022-03-14 15:01:53 +01002168
J-Alves3bb82592023-01-23 11:20:54 +00002169 vm_notifications_framework_set_pending(to_locked,
2170 rx_buffer_full);
2171
2172 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2173 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2174 vcpu_index(current));
2175 plat_ffa_sri_trigger_not_delayed(current->cpu);
2176 } else {
2177 plat_ffa_sri_state_set(DELAYED);
2178 }
Federico Recanati25053ee2022-03-14 15:01:53 +01002179 }
2180
2181 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2182
2183out:
2184 vm_unlock(&to_locked);
2185
2186out_unlock_sender:
2187 vm_unlock(&sender_locked);
2188
2189 return ret;
2190}
2191
2192/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002193 * Releases the caller's mailbox so that a new message can be received. The
2194 * caller must have copied out all data they wish to preserve as new messages
2195 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002196 *
2197 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002198 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2199 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002200 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2201 * - FFA_SUCCESS on success if no further action is needed.
2202 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002203 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002204 * hf_mailbox_waiter_get.
2205 */
J-Alves19e20cf2023-08-02 12:48:55 +01002206struct ffa_value api_ffa_rx_release(ffa_id_t receiver_id, struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002207{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002208 struct vm *current_vm = current->vm;
2209 struct vm *vm;
2210 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01002211 ffa_id_t current_vm_id = current_vm->id;
2212 ffa_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002213 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002214
Federico Recanati7bef0b92022-03-17 14:56:22 +01002215 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2216 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2217 dlog_error("Invalid `receiver_id`, must be zero.\n");
2218 return ffa_error(FFA_INVALID_PARAMETERS);
2219 }
2220
2221 /*
2222 * VM ID to be released: `receiver_id` if message has been forwarded by
2223 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2224 */
2225 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2226 release_vm_id = current_vm_id;
2227 } else {
2228 release_vm_id = receiver_id;
2229 }
2230
2231 vm_locked = plat_ffa_vm_find_locked(release_vm_id);
2232 vm = vm_locked.vm;
2233 if (vm == NULL) {
2234 dlog_error("No buffer registered for VM ID %#x.\n",
2235 release_vm_id);
2236 return ffa_error(FFA_INVALID_PARAMETERS);
2237 }
2238
J-Alvese8c8c2b2022-12-16 15:34:48 +00002239 if (plat_ffa_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002240 goto out;
2241 }
2242
Karl Meakin1064a9c2024-06-04 17:20:43 +01002243 ret = api_release_mailbox(vm_locked);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002244
2245out:
2246 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002247
2248 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002249}
Andrew Walbran318f5732018-11-20 16:23:42 +00002250
2251/**
Federico Recanati644f0462022-03-17 12:04:00 +01002252 * Acquire ownership of an RX buffer before writing to it. Both
2253 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2254 * could contend for the same buffer. SPMC owns VM's RX buffer after
2255 * it's mapped in its translation regime. This ABI should be
2256 * used by the Hypervisor to get the ownership of a VM's RX buffer
2257 * from the SPMC solving the aforementioned possible contention.
2258 *
2259 * Returns:
2260 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2261 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2262 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2263 */
J-Alves19e20cf2023-08-02 12:48:55 +01002264struct ffa_value api_ffa_rx_acquire(ffa_id_t receiver_id, struct vcpu *current)
Federico Recanati644f0462022-03-17 12:04:00 +01002265{
2266 struct vm_locked receiver_locked;
2267 struct vm *receiver;
2268 struct ffa_value ret;
2269
2270 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
J-Alves661e1b72023-08-02 13:39:40 +01002271 !ffa_is_vm_id(receiver_id)) {
Federico Recanati644f0462022-03-17 12:04:00 +01002272 dlog_error(
2273 "FFA_RX_ACQUIRE not supported at this FF-A "
2274 "instance.\n");
2275 return ffa_error(FFA_NOT_SUPPORTED);
2276 }
2277
2278 receiver_locked = plat_ffa_vm_find_locked(receiver_id);
2279 receiver = receiver_locked.vm;
2280
2281 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2282 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2283 receiver_id);
2284 ret = ffa_error(FFA_INVALID_PARAMETERS);
2285 goto out;
2286 }
2287
2288 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2289 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2290 ret = ffa_error(FFA_DENIED);
2291 goto out;
2292 }
2293
J-Alvese8c8c2b2022-12-16 15:34:48 +00002294 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002295
2296 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2297
2298out:
2299 vm_unlock(&receiver_locked);
2300
2301 return ret;
2302}
2303
J-Alvescc542042024-07-24 19:13:22 +01002304/*
2305 * Returns true if intid relates with either of those:
2306 * - NPI
2307 * - ME
2308 * - Virtual Timer.
2309 *
2310 * These are VIs with no expected interrupt descriptor.
2311 */
2312static bool api_is_maintenance_virtual_interrupt(uint32_t intid)
2313{
2314 return intid == HF_NOTIFICATION_PENDING_INTID ||
2315 intid == HF_MANAGED_EXIT_INTID ||
2316 intid == HF_VIRTUAL_TIMER_INTID;
2317}
2318
Federico Recanati644f0462022-03-17 12:04:00 +01002319/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002320 * Enables or disables a given interrupt ID for the calling vCPU.
2321 *
2322 * Returns 0 on success, or -1 if the intid is invalid.
2323 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002324int64_t api_interrupt_enable(uint32_t intid, bool enable,
2325 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002326{
Manish Pandey35e452f2021-02-18 21:36:34 +00002327 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002328 struct interrupts *interrupts = &current->interrupts;
J-Alvescc542042024-07-24 19:13:22 +01002329 struct interrupt_descriptor *int_desc = NULL;
2330 struct vm *vm = current->vm;
2331 struct vm_locked vm_locked;
2332
2333 int64_t ret = -1;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002334
Andrew Walbran318f5732018-11-20 16:23:42 +00002335 if (intid >= HF_NUM_INTIDS) {
2336 return -1;
2337 }
2338
J-Alvescc542042024-07-24 19:13:22 +01002339 vm_locked = vm_lock(vm);
Manish Pandey35e452f2021-02-18 21:36:34 +00002340 current_locked = vcpu_lock(current);
J-Alvescc542042024-07-24 19:13:22 +01002341
2342 int_desc = vm_interrupt_set_enable(vm_locked, intid, enable);
2343
2344 if (!api_is_maintenance_virtual_interrupt(intid)) {
2345 if (int_desc == NULL) {
2346 dlog_error("%s: invalid interrupt ID.\n", __func__);
2347 goto out;
2348 }
2349
2350 plat_interrupts_configure_interrupt(*int_desc);
2351 }
2352
Andrew Walbran318f5732018-11-20 16:23:42 +00002353 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002354 /*
2355 * If it is pending and was not enabled before, increment the
2356 * count.
2357 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002358 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2359 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2360 vcpu_interrupt_count_increment(current_locked,
2361 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002362 }
J-Alvescc542042024-07-24 19:13:22 +01002363
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002364 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2365 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002366 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002367 /*
2368 * If it is pending and was enabled before, decrement the count.
2369 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002370 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2371 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2372 vcpu_interrupt_count_decrement(current_locked,
2373 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002374 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002375 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2376 vcpu_virt_interrupt_set_type(interrupts, intid,
2377 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002378 }
2379
J-Alvescc542042024-07-24 19:13:22 +01002380 ret = 0;
2381
2382out:
2383 vm_unlock(&vm_locked);
Manish Pandey35e452f2021-02-18 21:36:34 +00002384 vcpu_unlock(&current_locked);
J-Alvescc542042024-07-24 19:13:22 +01002385
2386 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002387}
2388
2389/**
2390 * Returns the ID of the next pending interrupt for the calling vCPU, and
2391 * acknowledges it (i.e. marks it as no longer pending). Returns
2392 * HF_INVALID_INTID if there are no pending interrupts.
2393 */
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002394uint32_t api_interrupt_get(struct vcpu_locked current_locked)
Andrew Walbran318f5732018-11-20 16:23:42 +00002395{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002396 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002397 uint32_t first_interrupt = HF_INVALID_INTID;
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002398 struct interrupts *interrupts = &current_locked.vcpu->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002399
2400 /*
2401 * Find the first enabled and pending interrupt ID, return it, and
2402 * deactivate it.
2403 */
Andrew Walbran318f5732018-11-20 16:23:42 +00002404 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2405 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002406 interrupts->interrupt_enabled.bitmap[i] &
2407 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002408
Andrew Walbran318f5732018-11-20 16:23:42 +00002409 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002410 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002411
2412 first_interrupt =
2413 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002414
Andrew Walbran3d84a262018-12-13 14:41:19 +00002415 /*
2416 * Mark it as no longer pending and decrement the count.
2417 */
J-Alvesb8730e92024-08-07 18:28:55 +01002418 vcpu_interrupt_clear_decrement(current_locked,
2419 first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002420 break;
2421 }
2422 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002423
Andrew Walbran318f5732018-11-20 16:23:42 +00002424 return first_interrupt;
2425}
2426
2427/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00002428 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00002429 * given VM and vCPU.
2430 */
2431static inline bool is_injection_allowed(uint32_t target_vm_id,
2432 struct vcpu *current)
2433{
2434 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002435
Andrew Walbran318f5732018-11-20 16:23:42 +00002436 /*
2437 * The primary VM is allowed to inject interrupts into any VM. Secondary
2438 * VMs are only allowed to inject interrupts into their own vCPUs.
2439 */
2440 return current_vm_id == HF_PRIMARY_VM_ID ||
2441 current_vm_id == target_vm_id;
2442}
2443
2444/**
2445 * Injects a virtual interrupt of the given ID into the given target vCPU.
2446 * This doesn't cause the vCPU to actually be run immediately; it will be taken
2447 * when the vCPU is next run, which is up to the scheduler.
2448 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00002449 * Returns:
2450 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
2451 * ID is invalid, or the current VM is not allowed to inject interrupts to
2452 * the target VM.
2453 * - 0 on success if no further action is needed.
2454 * - 1 if it was called by the primary VM and the primary VM now needs to wake
2455 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00002456 */
J-Alves19e20cf2023-08-02 12:48:55 +01002457int64_t api_interrupt_inject(ffa_id_t target_vm_id,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002458 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01002459 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00002460{
Andrew Walbran318f5732018-11-20 16:23:42 +00002461 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01002462 struct vm *target_vm = vm_find(target_vm_id);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002463 struct vcpu_locked current_locked;
2464 struct vcpu_locked target_locked;
2465 struct two_vcpu_locked vcpus_locked;
2466 int64_t ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002467
2468 if (intid >= HF_NUM_INTIDS) {
2469 return -1;
2470 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002471
Andrew Walbran318f5732018-11-20 16:23:42 +00002472 if (target_vm == NULL) {
2473 return -1;
2474 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002475
Andrew Walbran318f5732018-11-20 16:23:42 +00002476 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00002477 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00002478 return -1;
2479 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002480
Andrew Walbran318f5732018-11-20 16:23:42 +00002481 if (!is_injection_allowed(target_vm_id, current)) {
2482 return -1;
2483 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002484
Andrew Walbrane1310df2019-04-29 17:28:28 +01002485 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00002486
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002487 /* A VM could inject an interrupt for itself. */
2488 if (target_vcpu != current) {
2489 /* Lock both vCPUs at once to avoid deadlock. */
2490 vcpus_locked = vcpu_lock_both(current, target_vcpu);
2491 current_locked = vcpus_locked.vcpu1;
2492 target_locked = vcpus_locked.vcpu2;
2493 } else {
2494 current_locked = vcpu_lock(current);
2495 target_locked = current_locked;
2496 }
2497
Manish Pandey35e452f2021-02-18 21:36:34 +00002498 dlog_verbose(
2499 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
2500 "%u\n",
2501 intid, target_vm_id, target_vcpu_idx, current->vm->id,
2502 vcpu_index(current));
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002503 ret = api_interrupt_inject_locked(target_locked, intid, current_locked,
2504 next);
2505 if (target_vcpu != current) {
2506 vcpu_unlock(&target_locked);
2507 }
2508
2509 vcpu_unlock(&current_locked);
2510 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002511}
Andrew Scull6386f252018-12-06 13:29:10 +00002512
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002513/**
2514 * Negotiate the FF-A version to be used for this FF-A instance.
2515 * See section 13.2 of the FF-A v1.2 ALP1 spec.
2516 *
2517 * Returns Hafnium's version number (`FFA_VERSION_COMPILED`) on success.
2518 * Returns `FFA_NOT_SUPPORTED` on error:
2519 * - The version is invalid (highest bit set).
2520 * - The requested version is incompatible.
2521 * - The version has already been negotiated and cannot be changed.
2522 */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002523struct ffa_value api_ffa_version(struct vcpu *current,
Karl Meakin0e617d92024-04-05 12:55:22 +01002524 enum ffa_version requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002525{
Karl Meakin0e617d92024-04-05 12:55:22 +01002526 static_assert(sizeof(enum ffa_version) == 4,
2527 "enum ffa_version must be 4 bytes wide");
2528
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002529 const struct ffa_value error = {.func = (uint32_t)FFA_NOT_SUPPORTED};
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002530 struct vm_locked current_vm_locked;
2531
Karl Meakin0e617d92024-04-05 12:55:22 +01002532 if (!ffa_version_is_valid(requested_version)) {
2533 dlog_error(
2534 "FFA_VERSION: requested version %#x is invalid "
2535 "(highest bit must be zero)\n",
2536 requested_version);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002537 return error;
Andrew Walbran9fd29072020-04-22 12:12:14 +01002538 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002539
Karl Meakin0e617d92024-04-05 12:55:22 +01002540 if (!ffa_versions_are_compatible(requested_version,
2541 FFA_VERSION_COMPILED)) {
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002542 dlog_error(
2543 "FFA_VERSION: requested version v%u.%u is not "
2544 "compatible with v%u.%u\n",
2545 ffa_version_get_major(requested_version),
2546 ffa_version_get_minor(requested_version),
2547 ffa_version_get_major(FFA_VERSION_COMPILED),
2548 ffa_version_get_minor(FFA_VERSION_COMPILED));
2549 return error;
J-Alves3cc9d6f2023-07-20 16:46:15 +01002550 }
2551
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002552 current_vm_locked = vm_lock(current->vm);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002553
2554 if (current_vm_locked.vm->ffa_version_negotiated &&
2555 requested_version != current_vm_locked.vm->ffa_version) {
2556 vm_unlock(&current_vm_locked);
2557 dlog_error(
2558 "FFA_VERSION: Cannot change FF-A version after other "
2559 "FF-A calls have been made\n");
2560 return error;
2561 }
2562
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002563 current_vm_locked.vm->ffa_version = requested_version;
2564 vm_unlock(&current_vm_locked);
2565
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002566 return (struct ffa_value){.func = (uint32_t)FFA_VERSION_COMPILED};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002567}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002568
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002569/**
Karl Meakin49ec1e42024-05-10 13:08:24 +01002570 * Helper for success return of FFA_FEATURES.
J-Alves6f72ca82021-11-01 12:34:58 +00002571 */
2572struct ffa_value api_ffa_feature_success(uint32_t arg2)
2573{
2574 return (struct ffa_value){
Karl Meakin49ec1e42024-05-10 13:08:24 +01002575 .func = FFA_SUCCESS_32,
2576 .arg1 = 0U,
2577 .arg2 = arg2,
2578 };
J-Alves6f72ca82021-11-01 12:34:58 +00002579}
2580
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002581static struct ffa_value ffa_features_function(uint32_t func,
2582 uint32_t input_property,
2583 struct vcpu *current)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002584{
Karl Meakin0e617d92024-04-05 12:55:22 +01002585 const enum ffa_version ffa_version = current->vm->ffa_version;
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002586 const bool el0_partition = current->vm->el0_partition;
2587
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002588 switch (func) {
J-Alves6f72ca82021-11-01 12:34:58 +00002589 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002590 case FFA_ERROR_32:
2591 case FFA_SUCCESS_32:
2592 case FFA_INTERRUPT_32:
2593 case FFA_VERSION_32:
2594 case FFA_FEATURES_32:
2595 case FFA_RX_RELEASE_32:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002596 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002597 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002598 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002599 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002600 case FFA_RUN_32:
J-Alves95fbb312024-03-20 15:19:16 +00002601 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002602 case FFA_MEM_DONATE_32:
2603 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00002604 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002605 case FFA_MEM_SHARE_32:
J-Alves95fbb312024-03-20 15:19:16 +00002606 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002607 case FFA_MEM_RETRIEVE_RESP_32:
2608 case FFA_MEM_RELINQUISH_32:
2609 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002610 case FFA_MEM_FRAG_RX_32:
2611 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002612 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002613 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002614 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002615 case FFA_MSG_SEND_DIRECT_REQ_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002616
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002617 /* FF-A v1.1 features. */
2618 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002619 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2620 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2621 case FFA_NOTIFICATION_BIND_32:
2622 case FFA_NOTIFICATION_UNBIND_32:
2623 case FFA_NOTIFICATION_SET_32:
2624 case FFA_NOTIFICATION_GET_32:
2625 case FFA_NOTIFICATION_INFO_GET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002626 case FFA_MSG_SEND2_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002627 if (FFA_VERSION_1_1 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002628 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002629 }
2630
Kathleen Capellae4fe2962023-09-01 17:08:47 -04002631 /* FF-A v1.2 features. */
2632 case FFA_CONSOLE_LOG_32:
2633 case FFA_CONSOLE_LOG_64:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002634 case FFA_PARTITION_INFO_GET_REGS_64:
Kathleen Capella41fea932023-06-23 17:39:28 -04002635 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002636 case FFA_MSG_SEND_DIRECT_RESP2_64:
Karl Meakin0e617d92024-04-05 12:55:22 +01002637 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002638 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002639 }
2640
Karl Meakin49ec1e42024-05-10 13:08:24 +01002641 return api_ffa_feature_success(0);
2642
Karl Meakin5a222642024-06-20 10:23:37 +01002643 /* These functions are only supported on S-EL0 partitions. */
2644 case FFA_MEM_PERM_GET_32:
2645 case FFA_MEM_PERM_SET_32:
2646 case FFA_MEM_PERM_GET_64:
2647 case FFA_MEM_PERM_SET_64:
2648 if (!(vm_id_is_current_world(current->vm->id) &&
2649 el0_partition)) {
2650 dlog_verbose(
2651 "FFA_FEATURE: %s is only supported on S-EL0 "
2652 "partitions\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002653 ffa_func_name(func));
Karl Meakin5a222642024-06-20 10:23:37 +01002654 return ffa_error(FFA_NOT_SUPPORTED);
2655 }
2656 return api_ffa_feature_success(0);
2657
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002658 case FFA_SECONDARY_EP_REGISTER_64:
2659 if (FFA_VERSION_COMPILED < FFA_VERSION_1_1) {
2660 return ffa_error(FFA_NOT_SUPPORTED);
2661 }
2662
2663 if (!(vm_id_is_current_world(current->vm->id) &&
2664 current->vm->vcpu_count > 1)) {
2665 dlog_verbose(
2666 "FFA_FEATURE: %s is only supported on SPs with "
2667 "more than 1 vCPU\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002668 ffa_func_name(func));
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002669 return ffa_error(FFA_NOT_SUPPORTED);
2670 }
2671 return api_ffa_feature_success(0);
2672
Karl Meakin650cb142024-06-20 10:31:57 +01002673 /*
2674 * This function is restricted to the secure virtual FF-A instance (i.e.
2675 * only report success to SPs).
2676 */
2677 case FFA_YIELD_32:
2678 if (!vm_id_is_current_world(current->vm->id)) {
2679 dlog_verbose(
2680 "FFA_FEATURES: %s is only supported at secure "
2681 "virtual FF-A instance\n",
2682 ffa_func_name(FFA_YIELD_32));
2683 return ffa_error(FFA_NOT_SUPPORTED);
2684 }
2685 return api_ffa_feature_success(0);
2686
Karl Meakin0fd67292024-02-06 17:33:05 +00002687 case FFA_RXTX_MAP_64: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002688 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002689 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002690 }
2691
Karl Meakin0fd67292024-02-06 17:33:05 +00002692 uint32_t arg2 = 0;
2693 struct ffa_features_rxtx_map_params params = {
2694 .min_buf_size = FFA_RXTX_MAP_MIN_BUF_4K,
2695 .mbz = 0,
2696 .max_buf_size =
Karl Meakin0e617d92024-04-05 12:55:22 +01002697 (ffa_version >= FFA_VERSION_1_2)
Karl Meakin0fd67292024-02-06 17:33:05 +00002698 ? FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT
2699 : 0,
2700 };
2701
2702 memcpy_s(&arg2, sizeof(arg2), &params, sizeof(params));
Karl Meakin49ec1e42024-05-10 13:08:24 +01002703 return api_ffa_feature_success(arg2);
Karl Meakin0fd67292024-02-06 17:33:05 +00002704 }
2705
J-Alves95fbb312024-03-20 15:19:16 +00002706 case FFA_MEM_RETRIEVE_REQ_64:
Karl Meakin49ec1e42024-05-10 13:08:24 +01002707 case FFA_MEM_RETRIEVE_REQ_32: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002708 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002709 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002710 }
2711
Karl Meakin49ec1e42024-05-10 13:08:24 +01002712 if (ANY_BITS_SET(input_property,
2713 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2714 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT) ||
2715 IS_BIT_SET(input_property,
2716 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT)) {
2717 dlog_warning(
2718 "FFA_FEATURES: Bits[%u:%u] and Bit[%u] of "
2719 "input_property should be 0 (input_property = "
2720 "%#x)\n",
2721 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2722 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT,
2723 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT,
Karl Meakin34b8ae92023-01-13 13:33:07 +00002724 input_property);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002725 }
2726
Karl Meakin0e617d92024-04-05 12:55:22 +01002727 if (ffa_version >= FFA_VERSION_1_1 &&
2728 (input_property &
2729 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
Karl Meakin4e8e4792024-07-05 16:28:42 +01002730 dlog_verbose(
2731 "FFA_FEATURES: NS bit support must be 1\n");
Karl Meakin6fd6c1d2024-05-13 12:09:13 +01002732 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002733 }
2734
2735 return api_ffa_feature_success(
2736 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
Karl Meakin49ec1e42024-05-10 13:08:24 +01002737 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT |
Karl Meakin34b8ae92023-01-13 13:33:07 +00002738 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
Karl Meakin49ec1e42024-05-10 13:08:24 +01002739 }
J-Alves6f72ca82021-11-01 12:34:58 +00002740
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002741 default:
2742 return ffa_error(FFA_NOT_SUPPORTED);
2743 }
2744}
2745
2746static struct ffa_value ffa_features_feature(enum ffa_feature_id feature,
2747 uint32_t input_property,
2748 struct vcpu *current)
2749{
2750 const bool el0_partition = current->vm->el0_partition;
2751
2752 if (ANY_BITS_SET(feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2753 FFA_FEATURES_FEATURE_MBZ_LO_BIT)) {
2754 dlog_verbose(
2755 "FFA_FEATURES: feature ID %#x is invalid (bits [%u:%u] "
2756 "must be zero)\n",
2757 feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2758 FFA_FEATURES_FEATURE_MBZ_LO_BIT);
2759 return ffa_error(FFA_NOT_SUPPORTED);
2760 }
2761 if (input_property != 0) {
2762 dlog_verbose(
2763 "FFA_FEATURES: input_property must be 0 "
2764 "(input_property = %#x)\n",
2765 input_property);
2766 return ffa_error(FFA_NOT_SUPPORTED);
2767 }
2768
2769 switch (feature) {
J-Alves6f72ca82021-11-01 12:34:58 +00002770 /* Check support of a feature provided respective feature ID. */
Karl Meakin17c9ad32024-04-12 16:41:00 +01002771
2772 /*
2773 * For NPI and MEI, report the IDs as supported only to partitions at
2774 * the virtual FF-A instances.
2775 */
J-Alves6f72ca82021-11-01 12:34:58 +00002776 case FFA_FEATURE_NPI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002777 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002778 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002779 }
2780
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002781 if (el0_partition) {
2782 return ffa_error(FFA_NOT_SUPPORTED);
2783 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002784 if (!vm_id_is_current_world(current->vm->id)) {
2785 return ffa_error(FFA_NOT_SUPPORTED);
2786 }
J-Alves6f72ca82021-11-01 12:34:58 +00002787 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
Karl Meakin17c9ad32024-04-12 16:41:00 +01002788
2789 case FFA_FEATURE_MEI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002790 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002791 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002792 }
J-Alvesc4d9ae82024-05-14 10:15:03 +01002793 if (el0_partition) {
2794 return ffa_error(FFA_NOT_SUPPORTED);
2795 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002796 if (!vm_id_is_current_world(current->vm->id)) {
2797 return ffa_error(FFA_NOT_SUPPORTED);
2798 }
2799 return api_ffa_feature_success(HF_MANAGED_EXIT_INTID);
2800
J-Alves6f72ca82021-11-01 12:34:58 +00002801 case FFA_FEATURE_SRI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002802 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002803 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002804 }
2805
Karl Meakincb6642c2024-02-27 14:43:45 +00002806 if (!ffa_is_vm_id(current->vm->id)) {
2807 return ffa_error(FFA_NOT_SUPPORTED);
2808 }
J-Alves6f72ca82021-11-01 12:34:58 +00002809 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
Karl Meakin0e617d92024-04-05 12:55:22 +01002810
J-Alves6f72ca82021-11-01 12:34:58 +00002811 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002812 default:
Karl Meakin4271ff92024-05-13 12:33:15 +01002813 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002814 }
2815}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002816
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002817/**
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002818 * Discovery function returning information about the implementation of optional
2819 * FF-A interfaces. See section 13.3 of the FF-A v1.2 ALP1 spec.
2820 *
2821 * `function_or_feature_id` is interpreted as either a function ID or a feature
2822 * ID, depending on the value of bit 31.
2823 * When it is a feature ID, bits [30:8] MBZ and input_property MBZ.
2824 *
2825 * Returns `FFA_SUCCESS` if the interface is supported.
2826 * Returns `FFA_NOT_SUPPORTED` if the interface is not supported or the
2827 * parameters are invalid.
2828 */
2829struct ffa_value api_ffa_features(uint32_t function_or_feature_id,
2830 uint32_t input_property, struct vcpu *current)
2831{
2832 return IS_BIT_UNSET(function_or_feature_id, FFA_FEATURES_FEATURE_BIT)
2833 ? ffa_features_feature(function_or_feature_id,
2834 input_property, current)
2835 : ffa_features_function(function_or_feature_id,
2836 input_property, current);
2837}
2838
2839/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002840 * FF-A specification states that x2/w2 Must Be Zero for FFA_MSG_SEND_DIRECT_REQ
2841 * and FFA_MSG_SEND_DIRECT_RESP interfaces when used for partition messages. See
2842 * FF-A v1.2 Table 16.6: FFA_MSG_SEND_DIRECT_REQ function syntax.
J-Alves645eabe2021-02-22 16:08:27 +00002843 */
2844static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2845{
2846 return args.arg2 == 0U;
2847}
2848
2849/**
J-Alves76d99af2021-03-10 17:42:11 +00002850 * Limits size of arguments in ffa_value structure to 32-bit.
2851 */
2852static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2853{
2854 return (struct ffa_value){
2855 .func = (uint32_t)args.func,
2856 .arg1 = (uint32_t)args.arg1,
2857 .arg2 = (uint32_t)0,
2858 .arg3 = (uint32_t)args.arg3,
2859 .arg4 = (uint32_t)args.arg4,
2860 .arg5 = (uint32_t)args.arg5,
2861 .arg6 = (uint32_t)args.arg6,
2862 .arg7 = (uint32_t)args.arg7,
2863 };
2864}
2865
2866/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002867 * Helper to copy direct message payload, depending on SMC used, direct
2868 * messaging interface used, and expected registers size.
J-Alves76d99af2021-03-10 17:42:11 +00002869 */
2870static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2871{
2872 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2873 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2874 return api_ffa_value_copy32(args);
2875 }
2876
Kathleen Capella41fea932023-06-23 17:39:28 -04002877 if (args.func == FFA_MSG_SEND_DIRECT_REQ_64 ||
2878 args.func == FFA_MSG_SEND_DIRECT_RESP_64) {
2879 args.arg2 = 0;
2880 }
2881
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002882 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64) {
2883 args.extended_val.valid = true;
2884 }
2885
Kathleen Capella087e5022023-09-07 18:04:15 -04002886 if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64) {
2887 args.arg2 = 0;
2888 args.arg3 = 0;
2889 }
2890
Kathleen Capella41fea932023-06-23 17:39:28 -04002891 return args;
2892}
2893
2894static bool api_ffa_dir_msg_req2_is_uuid_valid(struct vm *receiver_vm,
2895 struct ffa_value args)
2896{
2897 struct ffa_uuid target_uuid;
Kathleen Capella422b10b2023-06-30 18:28:27 -04002898 uint16_t i;
Kathleen Capella41fea932023-06-23 17:39:28 -04002899
2900 ffa_uuid_unpack_from_uint64(args.arg2, args.arg3, &target_uuid);
2901
Kathleen Capella422b10b2023-06-30 18:28:27 -04002902 /* Allow for use of Nil UUID. */
2903 if (ffa_uuid_is_null(&target_uuid)) {
2904 return true;
2905 }
2906
2907 for (i = 0; i < PARTITION_MAX_UUIDS; i++) {
2908 if (ffa_uuid_is_null(&receiver_vm->uuids[i])) {
2909 break;
2910 }
2911 if (ffa_uuid_equal(&target_uuid, &receiver_vm->uuids[i])) {
2912 return true;
2913 }
2914 }
2915 return false;
J-Alves76d99af2021-03-10 17:42:11 +00002916}
2917
2918/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002919 * Send an FF-A direct message request.
Kathleen Capella41fea932023-06-23 17:39:28 -04002920 * This handler covers both FFA_MSG_SEND_DIRECT_REQ_32/64
2921 * and FFA_MSG_SEND_DIRECT_REQ2_64 (introduced in FF-A v1.2) with function-based
2922 * checks to accomodate for the difference between the ABIs.
2923 *
2924 * FFA_MSG_SEND_DIRECT_REQ2_64 works mostly the same as
2925 * FFA_MSG_SEND_DIRECT_REQ_32/64, but adds the ability to send a direct message
2926 * request to a specified UUID within a partition and the usage of an extended
2927 * range of registers (x4-x17, instead of x4-x7) to be used as part of the
2928 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002929 */
J-Alves19e20cf2023-08-02 12:48:55 +01002930struct ffa_value api_ffa_msg_send_direct_req(ffa_id_t sender_vm_id,
2931 ffa_id_t receiver_vm_id,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002932 struct ffa_value args,
2933 struct vcpu *current,
2934 struct vcpu **next)
2935{
J-Alves17228f72021-04-20 17:13:19 +01002936 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002937 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002938 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002939 struct vcpu *receiver_vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002940 struct vcpu_locked current_locked;
2941 struct vcpu_locked receiver_vcpu_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002942 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002943 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002944
Kathleen Capella41fea932023-06-23 17:39:28 -04002945 if ((args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2946 args.func == FFA_MSG_SEND_DIRECT_REQ_64) &&
2947 !api_ffa_dir_msg_is_arg2_zero(args)) {
J-Alves645eabe2021-02-22 16:08:27 +00002948 return ffa_error(FFA_INVALID_PARAMETERS);
2949 }
2950
Olivier Deprez55a189e2021-06-09 15:45:27 +02002951 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2952 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002953 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002954 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002955 }
2956
Olivier Deprez55a189e2021-06-09 15:45:27 +02002957 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002958 return ret;
2959 }
2960
J-Alvesbd32c972024-02-02 16:20:04 +00002961 ret = api_ffa_interrupt_return(0);
J-Alves17228f72021-04-20 17:13:19 +01002962
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002963 receiver_vm = vm_find(receiver_vm_id);
2964 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002965 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002966 return ffa_error(FFA_INVALID_PARAMETERS);
2967 }
2968
Kathleen Capella41fea932023-06-23 17:39:28 -04002969 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64 &&
2970 !api_ffa_dir_msg_req2_is_uuid_valid(receiver_vm, args)) {
2971 dlog_verbose("UUID unrecognized for this VM\n");
2972 return ffa_error(FFA_INVALID_PARAMETERS);
2973 }
2974
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002975 /*
J-Alves439ac972021-11-18 17:32:03 +00002976 * Check if sender supports sending direct message req, and if
2977 * receiver supports receipt of direct message requests.
2978 */
Kathleen Capella41fea932023-06-23 17:39:28 -04002979 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm,
2980 args.func)) {
J-Alves439ac972021-11-18 17:32:03 +00002981 return ffa_error(FFA_DENIED);
2982 }
2983
2984 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002985 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002986 * UP (migratable) or MP partitions with a number of vCPUs matching the
2987 * number of PEs in the system. It further states that MP partitions
2988 * accepting direct request messages cannot migrate.
2989 */
J-Alvesad6a0432021-04-09 16:06:21 +01002990 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002991 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002992 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002993 return ffa_error(FFA_INVALID_PARAMETERS);
2994 }
2995
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002996 /*
2997 * If VM must be locked, it must be done before any of its vCPUs are
2998 * locked.
2999 */
J-Alves6e2abc62021-12-02 14:58:56 +00003000 receiver_locked = vm_lock(receiver_vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003001
3002 /* Lock both vCPUs at once to avoid deadlock. */
3003 vcpus_locked = vcpu_lock_both(current, receiver_vcpu);
3004 current_locked = vcpus_locked.vcpu1;
3005 receiver_vcpu_locked = vcpus_locked.vcpu2;
3006
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003007 /*
3008 * If destination vCPU is executing or already received an
3009 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
3010 * busy. There is a brief period of time where the vCPU state has
3011 * changed but regs_available is still false thus consider this case as
3012 * the vCPU not yet ready to receive a direct message request.
3013 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003014 if (is_ffa_direct_msg_request_ongoing(receiver_vcpu_locked) ||
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003015 receiver_vcpu->state == VCPU_STATE_RUNNING ||
3016 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00003017 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003018 ret = ffa_error(FFA_BUSY);
3019 goto out;
3020 }
3021
J-Alves7ea3f7e2024-03-27 11:05:11 +00003022 if (!plat_ffa_check_runtime_state_transition(
3023 current_locked, sender_vm_id, HF_INVALID_VM_ID,
3024 receiver_vcpu_locked, args.func, &next_state)) {
3025 ret = ffa_error(FFA_DENIED);
3026 goto out;
3027 }
3028
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003029 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
3030 memory_order_relaxed)) {
3031 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04003032 dlog_verbose(
3033 "Receiver VM %#x aborted, cannot run vCPU %u\n",
3034 receiver_vcpu->vm->id,
3035 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003036 receiver_vcpu->state = VCPU_STATE_ABORTED;
3037 }
3038
3039 ret = ffa_error(FFA_ABORTED);
3040 goto out;
3041 }
3042
3043 switch (receiver_vcpu->state) {
3044 case VCPU_STATE_OFF:
3045 case VCPU_STATE_RUNNING:
3046 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003047 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003048 case VCPU_STATE_BLOCKED:
3049 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00003050 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
3051 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003052 ret = ffa_error(FFA_BUSY);
3053 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003054 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003055 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003056 * We expect target vCPU to be in WAITING state after either
3057 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003058 */
3059 break;
3060 }
3061
3062 /* Inject timer interrupt if any pending */
3063 if (arch_timer_pending(&receiver_vcpu->regs)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003064 api_interrupt_inject_locked(receiver_vcpu_locked,
3065 HF_VIRTUAL_TIMER_INTID,
3066 current_locked, NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003067
3068 arch_timer_mask(&receiver_vcpu->regs);
3069 }
3070
3071 /* The receiver vCPU runs upon direct message invocation */
3072 receiver_vcpu->cpu = current->cpu;
3073 receiver_vcpu->state = VCPU_STATE_RUNNING;
3074 receiver_vcpu->regs_available = false;
Kathleen Capellae468c112023-12-13 17:56:28 -05003075 receiver_vcpu->direct_request_origin.is_ffa_req2 =
3076 (args.func == FFA_MSG_SEND_DIRECT_REQ2_64);
3077 receiver_vcpu->direct_request_origin.vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003078
J-Alves76d99af2021-03-10 17:42:11 +00003079 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003080
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003081 assert(!vm_id_is_current_world(current->vm->id) ||
3082 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003083 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003084
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07003085 plat_ffa_wind_call_chain_ffa_direct_req(
3086 current_locked, receiver_vcpu_locked, sender_vm_id);
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05003087
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003088 /* Switch to receiver vCPU targeted to by direct msg request */
3089 *next = receiver_vcpu;
3090
J-Alves6e2abc62021-12-02 14:58:56 +00003091 if (!receiver_locked.vm->el0_partition) {
3092 /*
3093 * If the scheduler in the system is giving CPU cycles to the
3094 * receiver, due to pending notifications, inject the NPI
3095 * interrupt. Following call assumes that '*next' has been set
3096 * to receiver_vcpu.
3097 */
3098 plat_ffa_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003099 receiver_vcpu_locked, current_locked, receiver_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003100 }
3101
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003102 /*
3103 * Since this flow will lead to a VM switch, the return value will not
3104 * be applied to current vCPU.
3105 */
3106
3107out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003108 vcpu_unlock(&receiver_vcpu_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003109 vm_unlock(&receiver_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003110 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003111
3112 return ret;
3113}
3114
3115/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003116 * Resume the target vCPU after the current vCPU sent a direct response.
3117 * Current vCPU moves to waiting state.
3118 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003119void api_ffa_resume_direct_resp_target(struct vcpu_locked current_locked,
3120 struct vcpu **next,
J-Alves19e20cf2023-08-02 12:48:55 +01003121 ffa_id_t receiver_vm_id,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003122 struct ffa_value to_ret,
3123 bool is_nwd_call_chain)
3124{
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07003125 if (plat_ffa_is_spmd_lp_id(receiver_vm_id) ||
3126 !vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003127 *next = api_switch_to_other_world(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003128 VCPU_STATE_WAITING);
3129
3130 /* End of NWd scheduled call chain. */
3131 assert(!is_nwd_call_chain ||
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003132 (current_locked.vcpu->call_chain.prev_node == NULL));
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003133 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003134 *next = api_switch_to_primary(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003135 VCPU_STATE_WAITING);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003136 } else if (vm_id_is_current_world(receiver_vm_id)) {
3137 /*
3138 * It is expected the receiver_vm_id to be from an SP, otherwise
3139 * 'plat_ffa_is_direct_response_valid' should have
3140 * made function return error before getting to this point.
3141 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003142 *next = api_switch_to_vm(current_locked, to_ret,
3143 VCPU_STATE_WAITING, receiver_vm_id);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003144 } else {
3145 panic("Invalid direct message response invocation");
3146 }
3147}
3148
3149/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003150 * Send an FF-A direct message response.
Kathleen Capella087e5022023-09-07 18:04:15 -04003151 * This handler covers both FFA_MSG_SEND_DIRECT_RESP_32/64
3152 * and FFA_MSG_SEND_DIRECT_RESP2_64 (introduced in FF-A v1.2) with
3153 * function-based checks to accomodate for the difference between the ABIs.
3154 *
3155 * FFA_MSG_SEND_DIRECT_RESP2_64 is used to respond to requests sent via
3156 * FFA_MSG_SEND_DIRECT_REQ2_64 and adds the usage of an extended range
3157 * of registers (x4-x17, instead of x4-x7) to be used as part of the
3158 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003159 */
J-Alves19e20cf2023-08-02 12:48:55 +01003160struct ffa_value api_ffa_msg_send_direct_resp(ffa_id_t sender_vm_id,
3161 ffa_id_t receiver_vm_id,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003162 struct ffa_value args,
3163 struct vcpu *current,
3164 struct vcpu **next)
3165{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003166 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003167 struct vcpu_locked next_locked = (struct vcpu_locked){
3168 .vcpu = NULL,
3169 };
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003170 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003171 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003172 struct ffa_value signal_interrupt =
3173 (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003174 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
3175 struct two_vcpu_locked vcpus_locked;
Kathleen Capellae468c112023-12-13 17:56:28 -05003176 bool received_req2;
J-Alves645eabe2021-02-22 16:08:27 +00003177
Kathleen Capella35238872024-01-12 15:05:52 -05003178 /*
3179 * If using FFA_MSG_SEND_DIRECT_RESP, the caller's
3180 * - x2 MBZ for partition messages
3181 * - x8-x17 SBZ if caller's FF-A version >= FF-A v1.2
3182 */
3183 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) {
3184 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
3185 return ffa_error(FFA_INVALID_PARAMETERS);
3186 }
3187
Karl Meakin0e617d92024-04-05 12:55:22 +01003188 if (current->vm->ffa_version >= FFA_VERSION_1_2 &&
Kathleen Capella35238872024-01-12 15:05:52 -05003189 !api_extended_args_are_zero(&args)) {
3190 return ffa_error(FFA_INVALID_PARAMETERS);
3191 }
J-Alves645eabe2021-02-22 16:08:27 +00003192 }
3193
Olivier Deprez55a189e2021-06-09 15:45:27 +02003194 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
3195 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00003196 dlog_verbose("Invalid direct response call.\n");
J-Alvesaa336102021-03-01 13:02:45 +00003197 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003198 }
3199
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003200 current_locked = vcpu_lock(current);
3201
3202 if (!plat_ffa_check_runtime_state_transition(
3203 current_locked, sender_vm_id, receiver_vm_id, next_locked,
3204 args.func, &next_state)) {
3205 ret = ffa_error(FFA_DENIED);
3206 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05003207 }
3208
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003209 assert(!vm_id_is_current_world(current->vm->id) ||
3210 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003211
3212 /*
3213 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
3214 * defined originator.
3215 */
3216 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
3217 /*
3218 * Sending direct response but direct request origin
3219 * vCPU is not set.
3220 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003221 ret = ffa_error(FFA_DENIED);
3222 goto out;
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003223 }
3224
Kathleen Capellae468c112023-12-13 17:56:28 -05003225 received_req2 = current->direct_request_origin.is_ffa_req2;
3226
3227 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64 && received_req2) {
3228 dlog_verbose(
3229 "%s: FFA_MSG_SEND_DIRECT_RESP must be used with "
3230 "FFA_MSG_SEND_DIRECT_REQ.\n",
3231 __func__);
3232 ret = ffa_error(FFA_DENIED);
3233 goto out;
3234 } else if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64 &&
3235 !received_req2) {
3236 dlog_verbose(
3237 "%s: FFA_MSG_SEND_DIRECT_RESP2 must be used with "
3238 "FFA_MSG_SEND_DIRECT_REQ2.\n",
3239 __func__);
3240 ret = ffa_error(FFA_DENIED);
3241 goto out;
3242 }
3243
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003244 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003245 struct interrupts *interrupts = &current->interrupts;
3246
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003247 /*
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003248 * Per FF-A v1.1 EAC0 section 8.3.1.2.1 rule 6, SPMC can signal
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003249 * a secure interrupt to a SP that is performing managed exit.
3250 * We have taken a implementation defined choice to not allow
3251 * Managed exit while a SP is processing a secure interrupt.
3252 */
Madhukar Pappireddy7945bb52024-08-20 15:22:41 -05003253 CHECK(current->scheduling_mode != SPMC_MODE);
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003254
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06003255 plat_interrupts_set_priority_mask(current->priority_mask);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003256 /*
3257 * A SP may be signaled a managed exit but actually not trap
3258 * the virtual interrupt, probably because it has virtual
3259 * interrupts masked, and emit direct resp. In this case the
3260 * managed exit operation is considered completed and it would
3261 * also need to clear the pending managed exit flag for the SP
3262 * vCPU.
3263 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003264 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003265
3266 if (vcpu_is_virt_interrupt_pending(interrupts,
3267 HF_MANAGED_EXIT_INTID)) {
J-Alvesb8730e92024-08-07 18:28:55 +01003268 vcpu_interrupt_clear_decrement(current_locked,
3269 HF_MANAGED_EXIT_INTID);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003270 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003271 }
3272
Kathleen Capellae468c112023-12-13 17:56:28 -05003273 /* Clear direct request origin vm_id and request type for the caller. */
3274 current->direct_request_origin.is_ffa_req2 = false;
3275 current->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003276
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003277 api_ffa_resume_direct_resp_target(current_locked, next, receiver_vm_id,
3278 to_ret, false);
3279
3280 /*
3281 * Unlock current vCPU to allow it to be locked together with next
3282 * vcpu.
3283 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003284 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003285
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003286 /* Lock both vCPUs at once to avoid deadlock. */
3287 vcpus_locked = vcpu_lock_both(current, *next);
3288 current_locked = vcpus_locked.vcpu1;
3289 next_locked = vcpus_locked.vcpu2;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003290
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003291 plat_ffa_unwind_call_chain_ffa_direct_resp(current_locked, next_locked);
J-Alvesbd32c972024-02-02 16:20:04 +00003292
3293 /*
3294 * Check if there is a pending secure interrupt.
3295 * If there is, return back to the caller with FFA_INTERRUPT,
3296 * and set the `next` vcpu in a preempted state.
3297 */
3298 if (plat_ffa_intercept_call(current_locked, next_locked,
3299 &signal_interrupt)) {
3300 ret = signal_interrupt;
3301 *next = NULL;
3302 }
3303
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003304 vcpu_unlock(&next_locked);
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05003305
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003306out:
3307 vcpu_unlock(&current_locked);
3308 return ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003309}
3310
J-Alves84658fc2021-06-17 14:37:32 +01003311static bool api_memory_region_check_flags(
3312 struct ffa_memory_region *memory_region, uint32_t share_func)
3313{
3314 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00003315 case FFA_MEM_SHARE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003316 case FFA_MEM_SHARE_32:
3317 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3318 0U) {
3319 return false;
3320 }
3321 /* Intentional fall-through */
J-Alves95fbb312024-03-20 15:19:16 +00003322 case FFA_MEM_LEND_64:
J-Alves84658fc2021-06-17 14:37:32 +01003323 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00003324 case FFA_MEM_DONATE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003325 case FFA_MEM_DONATE_32: {
3326 /* Bits 31:2 Must Be Zero. */
3327 ffa_memory_receiver_flags_t to_mask =
3328 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
3329 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
3330
3331 if ((memory_region->flags & to_mask) != 0U) {
3332 return false;
3333 }
3334 break;
3335 }
3336 default:
3337 panic("Check for mem send calls only.\n");
3338 }
3339
3340 /* Last check reserved values are 0 */
3341 return true;
3342}
3343
J-Alves33c47bf2022-09-29 11:36:20 +01003344/*
3345 * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0.
3346 */
3347static void api_ffa_memory_region_v1_1_from_v1_0(
3348 struct ffa_memory_region_v1_0 *memory_region_v1_0,
3349 struct ffa_memory_region *memory_region_v1_1)
3350{
3351 memory_region_v1_1->sender = memory_region_v1_0->sender;
J-Alves00847062022-10-03 17:08:44 +01003352 memory_region_v1_1->handle = memory_region_v1_0->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003353 memory_region_v1_1->attributes =
3354 ffa_memory_attributes_extend(memory_region_v1_0->attributes);
J-Alves33c47bf2022-09-29 11:36:20 +01003355 memory_region_v1_1->flags = memory_region_v1_0->flags;
3356 memory_region_v1_1->tag = memory_region_v1_0->tag;
3357 memory_region_v1_1->memory_access_desc_size =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003358 sizeof(struct ffa_memory_access_v1_0);
J-Alves33c47bf2022-09-29 11:36:20 +01003359 memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003360 memory_region_v1_1->receivers_offset = sizeof(struct ffa_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003361
3362 /* Zero reserved fields. */
3363 for (uint32_t i = 0; i < 3U; i++) {
3364 memory_region_v1_1->reserved[i] = 0U;
3365 }
3366}
3367
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003368/**
3369 * Updates a v1.0 transaction descriptor to v1.1. This gives us the
3370 * memory_access_desc_size field we need for forwards compatability.
3371 * Copy the receivers and composite descriptors to the new struct.
3372 * We also check the fields in the v1.0 transaction descriptor and return:
3373 * - FFA_ERROR FFA_INVALID_PARAMETERS: If any of the fields are not valid
3374 * values, eg the reserved fields are not 0, receiver_count is too large or
3375 * composite offsets are not 0 for retrieve requests or in bounds for send
3376 * requests.
3377 * - FFA ERROR FFA_NOT_SUPPORTED: If an invalid ffa_version is supplied to the
3378 * function. Or the fragment length is more than a single page.
3379 * - FFA_ERROR FFA_NO_MEMORY: If we do not have enough memory for a scratch
3380 * memory transaction descriptor.
3381 * - FFA_SUCCESS: If a successful update has occured.
J-Alves33c47bf2022-09-29 11:36:20 +01003382 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003383static struct ffa_value api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3384 void *allocated, uint32_t *fragment_length, uint32_t *total_length,
Karl Meakin0e617d92024-04-05 12:55:22 +01003385 enum ffa_version ffa_version, bool send_transaction)
J-Alves33c47bf2022-09-29 11:36:20 +01003386{
3387 struct ffa_memory_region_v1_0 *memory_region_v1_0;
3388 struct ffa_memory_region *memory_region_v1_1 = NULL;
3389 struct ffa_composite_memory_region *composite_v1_0;
3390 struct ffa_composite_memory_region *composite_v1_1;
3391 size_t receivers_length;
3392 size_t space_left;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003393 size_t receivers_end;
J-Alves33c47bf2022-09-29 11:36:20 +01003394 size_t composite_offset_v1_1;
3395 size_t composite_offset_v1_0;
3396 size_t fragment_constituents_size;
3397 size_t fragment_length_v1_1;
3398
J-Alves33c47bf2022-09-29 11:36:20 +01003399 assert(fragment_length != NULL);
3400 assert(total_length != NULL);
3401
Karl Meakin0e617d92024-04-05 12:55:22 +01003402 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves33c47bf2022-09-29 11:36:20 +01003403 return (struct ffa_value){.func = FFA_SUCCESS_32};
3404 }
3405
Karl Meakin0e617d92024-04-05 12:55:22 +01003406 if (ffa_version != FFA_VERSION_1_0) {
J-Alves33c47bf2022-09-29 11:36:20 +01003407 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
3408 ffa_version);
3409 return ffa_error(FFA_NOT_SUPPORTED);
3410 }
3411
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003412 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003413 "Updating memory transaction descriptor from v1.0 to v1.1.\n");
J-Alves33c47bf2022-09-29 11:36:20 +01003414
3415 memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated;
3416
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003417 receivers_length = sizeof(struct ffa_memory_access_v1_0) *
J-Alves33c47bf2022-09-29 11:36:20 +01003418 memory_region_v1_0->receiver_count;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003419 receivers_end = sizeof(struct ffa_memory_region) + receivers_length;
J-Alves33c47bf2022-09-29 11:36:20 +01003420
3421 /*
3422 * Check the specified composite offset of v1.0 descriptor, and that all
3423 * receivers were configured with the same offset.
3424 */
3425 composite_offset_v1_0 =
3426 memory_region_v1_0->receivers[0].composite_memory_region_offset;
3427
J-Alves33c47bf2022-09-29 11:36:20 +01003428 /* Determine the composite offset for v1.1 descriptor. */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003429 if (send_transaction) {
3430 fragment_constituents_size =
3431 *fragment_length - composite_offset_v1_0 -
3432 sizeof(struct ffa_composite_memory_region);
3433 fragment_length_v1_1 =
3434 receivers_end +
3435 sizeof(struct ffa_composite_memory_region) +
3436 fragment_constituents_size;
3437 composite_offset_v1_1 = receivers_end;
3438 } else {
3439 fragment_constituents_size = 0;
3440 fragment_length_v1_1 = receivers_end;
3441 composite_offset_v1_1 = 0;
3442 }
J-Alves33c47bf2022-09-29 11:36:20 +01003443
3444 /*
3445 * Currently only support the simpler cases: memory transaction
3446 * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE.
3447 * TODO: allocate the entries needed for this fragment_length_v1_1.
3448 * - Check corner when v1.1 descriptor converted size surpasses
3449 * the size of the entry.
3450 */
3451 if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) {
3452 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003453 "Translation of FF-A v1.0 descriptors for over %lu is "
J-Alves33c47bf2022-09-29 11:36:20 +01003454 "unsupported.",
3455 MM_PPOOL_ENTRY_SIZE);
3456 return ffa_error(FFA_NOT_SUPPORTED);
3457 }
3458
3459 space_left = fragment_length_v1_1;
3460
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003461 /*
3462 * Allocate a page of memory to construct the v1.1 memory descriptor.
3463 * Earlier we checked that the fragment_length_v1_1 would not be larger
3464 * than a page.
3465 */
J-Alves33c47bf2022-09-29 11:36:20 +01003466 memory_region_v1_1 =
3467 (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
3468 if (memory_region_v1_1 == NULL) {
3469 return ffa_error(FFA_NO_MEMORY);
3470 }
3471
3472 /* Translate header from v1.0 to v1.1. */
3473 api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0,
3474 memory_region_v1_1);
3475
3476 space_left -= sizeof(struct ffa_memory_region);
3477
3478 /* Copy memory access information. */
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003479 memcpy_s((uint8_t *)memory_region_v1_1 +
3480 memory_region_v1_1->receivers_offset,
3481 space_left, memory_region_v1_0->receivers, receivers_length);
J-Alves33c47bf2022-09-29 11:36:20 +01003482
3483 /* Initialize the memory access descriptors with composite offset. */
3484 for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) {
3485 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003486 ffa_memory_region_get_receiver(memory_region_v1_1, i);
3487 assert(receiver != NULL);
J-Alves33c47bf2022-09-29 11:36:20 +01003488 receiver->composite_memory_region_offset =
3489 composite_offset_v1_1;
3490 }
3491
3492 space_left -= receivers_length;
3493
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003494 /* Composite memory descriptors to copy. */
3495 if (send_transaction) {
3496 /* Init v1.1 composite. */
3497 composite_v1_1 = (struct ffa_composite_memory_region
3498 *)((uint8_t *)memory_region_v1_1 +
3499 composite_offset_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003500
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003501 composite_v1_0 = ffa_memory_region_get_composite_v1_0(
3502 memory_region_v1_0, 0);
3503 composite_v1_1->constituent_count =
3504 composite_v1_0->constituent_count;
3505 composite_v1_1->page_count = composite_v1_0->page_count;
J-Alves33c47bf2022-09-29 11:36:20 +01003506
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003507 space_left -= sizeof(struct ffa_composite_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003508
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003509 /* Initialize v1.1 constituents. */
3510 memcpy_s(composite_v1_1->constituents, space_left,
3511 composite_v1_0->constituents,
3512 fragment_constituents_size);
J-Alves33c47bf2022-09-29 11:36:20 +01003513
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003514 space_left -= fragment_constituents_size;
3515 }
3516
J-Alves33c47bf2022-09-29 11:36:20 +01003517 assert(space_left == 0U);
3518
J-Alves33c47bf2022-09-29 11:36:20 +01003519 /*
3520 * Remove the v1.0 fragment size, and resultant size of v1.1 fragment.
3521 */
3522 *total_length = *total_length - *fragment_length + fragment_length_v1_1;
3523 *fragment_length = fragment_length_v1_1;
3524
3525 /*
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003526 * After successfully updating to v1.1 copy the descriptor to the
3527 * internal buffer given as a parameter (used to prevent TOCTOU attacks)
3528 * and free the scratch memory used to construct it.
J-Alves33c47bf2022-09-29 11:36:20 +01003529 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003530 memcpy_s(allocated, MM_PPOOL_ENTRY_SIZE, memory_region_v1_1,
3531 *fragment_length);
3532 mpool_free(&api_page_pool, memory_region_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003533
3534 return (struct ffa_value){.func = FFA_SUCCESS_32};
3535}
3536
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003537struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
3538 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003539 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003540{
3541 struct vm *from = current->vm;
3542 struct vm *to;
3543 const void *from_msg;
J-Alves33c47bf2022-09-29 11:36:20 +01003544 void *allocated_entry;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003545 struct ffa_memory_region *memory_region = NULL;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003546 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01003547 bool targets_other_world = false;
Karl Meakin0e617d92024-04-05 12:55:22 +01003548 enum ffa_version ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003549
3550 if (ipa_addr(address) != 0 || page_count != 0) {
3551 /*
3552 * Hafnium only supports passing the descriptor in the TX
3553 * mailbox.
3554 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003555 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003556 }
3557
Andrew Walbranca808b12020-05-15 17:22:28 +01003558 if (fragment_length > length) {
3559 dlog_verbose(
3560 "Fragment length %d greater than total length %d.\n",
3561 fragment_length, length);
3562 return ffa_error(FFA_INVALID_PARAMETERS);
3563 }
J-Alves33c47bf2022-09-29 11:36:20 +01003564
3565 if (fragment_length > HF_MAILBOX_SIZE ||
3566 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003567 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003568 }
3569
3570 /*
3571 * Check that the sender has configured its send buffer. If the TX
3572 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3573 * be safely accessed after releasing the lock since the TX mailbox
3574 * address can only be configured once.
3575 */
3576 sl_lock(&from->lock);
3577 from_msg = from->mailbox.send;
J-Alves33c47bf2022-09-29 11:36:20 +01003578 ffa_version = from->ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003579 sl_unlock(&from->lock);
3580
3581 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003582 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003583 }
3584
3585 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003586 * Copy the memory region descriptor to a fresh page from the memory
3587 * pool. This prevents the sender from changing it underneath us, and
3588 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003589 */
J-Alves33c47bf2022-09-29 11:36:20 +01003590 allocated_entry = mpool_alloc(&api_page_pool);
3591 if (allocated_entry == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003592 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003593 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003594 }
J-Alves33c47bf2022-09-29 11:36:20 +01003595
J-Alves8f2150d2024-03-28 16:15:56 +00003596 if (!memcpy_trapped(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg,
3597 fragment_length)) {
3598 dlog_error(
3599 "%s: Failed to copy FF-A memory region descriptor.\n",
3600 __func__);
3601 return ffa_error(FFA_ABORTED);
3602 }
J-Alves33c47bf2022-09-29 11:36:20 +01003603
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003604 if (!ffa_memory_region_sanity_check(allocated_entry, ffa_version,
3605 fragment_length, true)) {
3606 ret = ffa_error(FFA_INVALID_PARAMETERS);
3607 goto out;
3608 }
3609
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003610 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3611 allocated_entry, &fragment_length, &length, ffa_version, true);
J-Alves33c47bf2022-09-29 11:36:20 +01003612 if (ret.func != FFA_SUCCESS_32) {
3613 goto out;
3614 }
3615
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003616 memory_region = allocated_entry;
3617
J-Alves33c47bf2022-09-29 11:36:20 +01003618 if (fragment_length < sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003619 memory_region->memory_access_desc_size) {
J-Alves33c47bf2022-09-29 11:36:20 +01003620 dlog_verbose(
3621 "Initial fragment length %d smaller than header size "
Karl Meakine8937d92024-03-19 16:04:25 +00003622 "%lu.\n",
J-Alves33c47bf2022-09-29 11:36:20 +01003623 fragment_length,
3624 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003625 memory_region->memory_access_desc_size);
3626 ret = ffa_error(FFA_INVALID_PARAMETERS);
3627 goto out;
J-Alves33c47bf2022-09-29 11:36:20 +01003628 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003629
J-Alves84658fc2021-06-17 14:37:32 +01003630 if (!api_memory_region_check_flags(memory_region, share_func)) {
3631 dlog_verbose(
3632 "Memory region reserved arguments must be zero.\n");
3633 ret = ffa_error(FFA_INVALID_PARAMETERS);
3634 goto out;
3635 }
3636
J-Alves363f5722022-04-25 17:37:37 +01003637 if (memory_region->receiver_count == 0U) {
3638 dlog_verbose("Receiver count can't be 0.\n");
3639 ret = ffa_error(FFA_INVALID_PARAMETERS);
3640 goto out;
3641 }
3642
J-Alves95fbb312024-03-20 15:19:16 +00003643 if ((share_func == FFA_MEM_DONATE_32 ||
3644 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01003645 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003646 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003647 "FFA_MEM_DONATE only supports one recipient. Specified "
3648 "%u\n",
J-Alves363f5722022-04-25 17:37:37 +01003649 memory_region->receiver_count);
3650 ret = ffa_error(FFA_INVALID_PARAMETERS);
3651 goto out;
3652 }
3653
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003654 /*
3655 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01003656 * If there is a receiver from the other world, track it for later
3657 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003658 */
J-Alves363f5722022-04-25 17:37:37 +01003659 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003660 struct ffa_memory_access *receiver =
3661 ffa_memory_region_get_receiver(memory_region, i);
J-Alvesc9227c82024-04-24 21:00:58 +01003662 ffa_id_t receiver_id;
3663
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003664 assert(receiver != NULL);
J-Alvesc9227c82024-04-24 21:00:58 +01003665
3666 receiver_id = receiver->receiver_permissions.receiver;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003667
J-Alves363f5722022-04-25 17:37:37 +01003668 to = vm_find(receiver_id);
3669
J-Alves7de61af2024-03-27 10:29:46 +00003670 if ((vm_id_is_current_world(receiver_id) && to == NULL) ||
3671 to == from) {
J-Alves363f5722022-04-25 17:37:37 +01003672 dlog_verbose("%s: invalid receiver.\n", __func__);
3673 ret = ffa_error(FFA_INVALID_PARAMETERS);
3674 goto out;
3675 }
3676
J-Alvesc9227c82024-04-24 21:00:58 +01003677 if (!plat_ffa_is_memory_send_valid(
3678 receiver_id, from->id, share_func,
3679 memory_region->receiver_count > 1)) {
J-Alves363f5722022-04-25 17:37:37 +01003680 ret = ffa_error(FFA_DENIED);
3681 goto out;
3682 }
3683
3684 /* Capture if any of the receivers is from the other world. */
3685 if (!targets_other_world) {
3686 targets_other_world =
3687 !vm_id_is_current_world(receiver_id);
3688 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003689 }
3690
J-Alves363f5722022-04-25 17:37:37 +01003691 if (targets_other_world) {
J-Alves66652252022-07-06 09:49:51 +01003692 ret = plat_ffa_other_world_mem_send(
3693 from, share_func, &memory_region, length,
3694 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003695 } else {
3696 struct vm_locked from_locked = vm_lock(from);
3697
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003698 ret = ffa_memory_send(from_locked, memory_region, length,
3699 fragment_length, share_func,
3700 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003701 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003702 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003703 * make sure we don't free it.
3704 */
3705 memory_region = NULL;
3706
3707 vm_unlock(&from_locked);
3708 }
3709
3710out:
3711 if (memory_region != NULL) {
3712 mpool_free(&api_page_pool, memory_region);
3713 }
3714
3715 return ret;
3716}
3717
J-Alvesc7270b72024-09-12 10:16:34 +01003718/**
3719 * An FFA_MEM_RETRIEVE_REQ from the hypervisor must specify the handle of the
3720 * memory transaction it is querying and all other fields must be 0.
3721 */
3722static bool api_ffa_memory_hypervisor_retrieve_request_validate(
3723 struct ffa_memory_region *request, enum ffa_version version)
3724{
3725 switch (version) {
3726 case FFA_VERSION_1_0: {
3727 struct ffa_memory_region_v1_0 *request_v1_0 =
3728 (struct ffa_memory_region_v1_0 *)request;
3729
3730 return request_v1_0->sender == 0U &&
3731 request_v1_0->attributes.shareability == 0U &&
3732 request_v1_0->attributes.cacheability == 0U &&
3733 request_v1_0->attributes.type == 0U &&
3734 request_v1_0->attributes.security == 0U &&
3735 request_v1_0->flags == 0U && request_v1_0->tag == 0U &&
3736 request_v1_0->receiver_count == 0U &&
3737 plat_ffa_memory_handle_allocated_by_current_world(
3738 request_v1_0->handle);
3739 }
3740 default:
3741 return request->sender == 0U &&
3742 request->attributes.shareability == 0U &&
3743 request->attributes.cacheability == 0U &&
3744 request->attributes.type == 0U &&
3745 request->attributes.security == 0U &&
3746 request->flags == 0U && request->tag == 0U &&
3747 request->memory_access_desc_size == 0U &&
3748 request->receiver_count == 0U &&
3749 request->receivers_offset == 0U &&
3750 plat_ffa_memory_handle_allocated_by_current_world(
3751 request->handle);
3752 }
3753}
3754
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003755struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3756 uint32_t fragment_length,
3757 ipaddr_t address, uint32_t page_count,
3758 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003759{
3760 struct vm *to = current->vm;
3761 struct vm_locked to_locked;
3762 const void *to_msg;
J-Alves00847062022-10-03 17:08:44 +01003763 void *retrieve_msg;
3764 struct ffa_memory_region *retrieve_request = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003765 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003766 struct ffa_value ret;
Karl Meakin0e617d92024-04-05 12:55:22 +01003767 enum ffa_version ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003768
3769 if (ipa_addr(address) != 0 || page_count != 0) {
3770 /*
3771 * Hafnium only supports passing the descriptor in the TX
3772 * mailbox.
3773 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003774 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003775 }
3776
Andrew Walbrana65a1322020-04-06 19:32:32 +01003777 if (fragment_length != length) {
J-Alves33c47bf2022-09-29 11:36:20 +01003778 dlog_verbose("Fragmentation not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003779 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003780 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003781
J-Alves00847062022-10-03 17:08:44 +01003782 retrieve_msg = cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003783 message_buffer_size = cpu_get_buffer_size(current->cpu);
3784 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3785 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003786 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003787 }
3788
3789 to_locked = vm_lock(to);
3790 to_msg = to->mailbox.send;
J-Alves00847062022-10-03 17:08:44 +01003791 ffa_version = to->ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003792
3793 if (to_msg == NULL) {
3794 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003795 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003796 goto out;
3797 }
3798
3799 /*
3800 * Copy the retrieve request descriptor to an internal buffer, so that
3801 * the caller can't change it underneath us.
3802 */
J-Alves8f2150d2024-03-28 16:15:56 +00003803 if (!memcpy_trapped(retrieve_msg, message_buffer_size, to_msg,
3804 length)) {
3805 dlog_error(
3806 "%s: Failed to copy FF-A retrieve request "
3807 "descriptor.\n",
3808 __func__);
3809 ret = ffa_error(FFA_ABORTED);
3810 goto out;
3811 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003812
J-Alvese8c8c2b2022-12-16 15:34:48 +00003813 if ((vm_is_mailbox_other_world_owned(to_locked) &&
3814 !plat_ffa_acquire_receiver_rx(to_locked, &ret)) ||
3815 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003816 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003817 * Can't retrieve memory information if the mailbox is
3818 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003819 */
J-Alves59ed0042022-07-28 18:26:41 +01003820 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003821 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003822 goto out;
3823 }
3824
Daniel Boulby296ee702023-11-28 13:36:55 +00003825 if (!is_ffa_hypervisor_retrieve_request(retrieve_msg)) {
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003826 if (!ffa_memory_region_sanity_check(retrieve_msg, ffa_version,
3827 fragment_length, false)) {
3828 ret = ffa_error(FFA_INVALID_PARAMETERS);
3829 goto out;
3830 }
3831 /*
3832 * If required, transform the retrieve request to FF-A v1.1.
3833 */
3834 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3835 retrieve_msg, &fragment_length, &length, ffa_version,
3836 false);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003837
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003838 if (ret.func != FFA_SUCCESS_32) {
3839 goto out;
3840 }
J-Alvesc7270b72024-09-12 10:16:34 +01003841 } else {
3842 if (!api_ffa_memory_hypervisor_retrieve_request_validate(
3843 retrieve_msg, ffa_version)) {
3844 dlog_verbose(
3845 "All fields except the handle in the "
3846 "memory access descriptor must be zero for a "
3847 "hypervisor retrieve request.\n");
3848 ret = ffa_error(FFA_INVALID_PARAMETERS);
3849 goto out;
3850 }
J-Alves00847062022-10-03 17:08:44 +01003851 }
3852
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003853 retrieve_request = retrieve_msg;
Kathleen Capella578784f2023-09-01 18:03:27 -04003854
J-Alvesb5084cf2022-07-06 14:20:12 +01003855 if (plat_ffa_memory_handle_allocated_by_current_world(
3856 retrieve_request->handle)) {
3857 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3858 &api_page_pool);
3859 } else {
Daniel Boulbyfdd59f72023-10-19 16:43:19 +01003860 dlog_error("Invalid FF-A memory handle.\n");
3861 ret = ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesb5084cf2022-07-06 14:20:12 +01003862 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003863out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003864 vm_unlock(&to_locked);
3865 return ret;
3866}
3867
J-Alvesa39a8442024-04-26 15:41:47 +01003868/**
3869 * Copies the memory relinquish descriptor from the partition's TX buffer, to
3870 * the buffer of the local CPU. Do it safely, and return error if:
3871 * - FFA_ABORTED: if the `memcpy_trapped` fails.
3872 * - FFA_INVALID_PARAMETERS: if the size of the full memory relinquish
3873 * descriptor doesn't fit the local CPU buffer.
3874 *
3875 * Returns FFA_SUCCESS if copying goes well, and sets 'out_relinquish'
3876 * to the address of the cpu buffer with the relinquish descriptor.
3877 */
3878static struct ffa_value api_get_ffa_mem_relinquish_descriptor(
3879 struct vcpu *current, const void *from_msg,
3880 struct ffa_mem_relinquish **out_relinquish)
3881{
3882 struct ffa_mem_relinquish *relinquish_request;
3883 uint32_t from_msg_size;
3884 uint32_t total_from_msg_size;
3885 uint32_t dst_size;
3886 vaddr_t dst;
3887 vaddr_t src;
3888
3889 assert(from_msg != NULL);
3890 assert(out_relinquish != NULL);
3891
3892 /*
3893 * Copy the relinquish descriptor to an internal buffer, so that the
3894 * caller can't change it underneath us.
3895 */
3896 relinquish_request =
3897 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
3898
3899 /* Set the destination for the copy. */
3900 dst = va_from_ptr(relinquish_request);
3901 src = va_from_ptr(from_msg);
3902
3903 dst_size = cpu_get_buffer_size(current->cpu);
3904
3905 /* Only copy the size to start with. */
3906 from_msg_size = sizeof(struct ffa_mem_relinquish);
3907 total_from_msg_size = from_msg_size;
3908
3909 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3910 from_msg_size)) {
3911 dlog_error(
3912 "%s: Failed to copy FF-A memory relinquish "
3913 "descriptor.\n",
3914 __func__);
3915 return ffa_error(FFA_ABORTED);
3916 }
3917
3918 if (relinquish_request->endpoint_count != 1) {
3919 dlog_error("%s: relinquish descriptor must have 1 endpoint\n",
3920 __func__);
3921 return ffa_error(FFA_INVALID_PARAMETERS);
3922 }
3923
3924 /*
3925 * Increment the `dst` to the position right after the copied header.
3926 * Increment the `src` to point at the list of endpoints.
3927 *
3928 * Calculate the new `dst_size` which is the size of the allocated cpu
3929 * buffer, minus the size of the copied memory relinquish header.
3930 *
3931 * Only after the above: determine the new `from_msg_size` in accordance
3932 * to the endpoint count.
3933 */
3934 dst = va_add(dst, from_msg_size);
3935 src = va_add(src, from_msg_size);
3936
3937 /*
3938 * Check if it is safe to copy the rest of the message.
3939 * This also serves as a santiy check to 'endpoint_count'.
3940 * The size of what is left in the descriptor, based on endpoint_count,
3941 * shall not be bigger than the size of the mailbox minus the size of
3942 * the header which was previously copied in this function.
3943 */
3944 dst_size -= from_msg_size;
3945 from_msg_size = relinquish_request->endpoint_count * sizeof(ffa_id_t);
3946 total_from_msg_size += from_msg_size;
3947
3948 if (total_from_msg_size > HF_MAILBOX_SIZE ||
3949 total_from_msg_size > dst_size) {
3950 dlog_verbose(
3951 "Relinquish message too long. Endpoint count: %u\n",
3952 relinquish_request->endpoint_count);
3953 return ffa_error(FFA_INVALID_PARAMETERS);
3954 }
3955
3956 /* Copy the remaining fragment. */
3957 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3958 from_msg_size)) {
3959 dlog_error("%s: Failed to copy FF-A relinquish request.\n",
3960 __func__);
3961 return ffa_error(FFA_ABORTED);
3962 }
3963
3964 /*
3965 * Set the output address for the relinquish descriptor to the current
3966 * cpu's buffer.
3967 */
3968 *out_relinquish = relinquish_request;
3969
3970 return (struct ffa_value){.func = FFA_SUCCESS_32};
3971}
3972
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003973struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003974{
3975 struct vm *from = current->vm;
3976 struct vm_locked from_locked;
3977 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003978 struct ffa_value ret;
J-Alvesa39a8442024-04-26 15:41:47 +01003979 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003980
3981 from_locked = vm_lock(from);
3982 from_msg = from->mailbox.send;
3983
3984 if (from_msg == NULL) {
3985 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003986 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003987 goto out;
3988 }
3989
J-Alvesa39a8442024-04-26 15:41:47 +01003990 ret = api_get_ffa_mem_relinquish_descriptor(current, from_msg,
3991 &relinquish_request);
3992
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003993 /*
J-Alvesa39a8442024-04-26 15:41:47 +01003994 * If the descriptor was safely copied, continue with the handling of
3995 * the retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003996 */
J-Alvesa39a8442024-04-26 15:41:47 +01003997 if (ret.func == FFA_SUCCESS_32) {
3998 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3999 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004000 }
J-Alves8f2150d2024-03-28 16:15:56 +00004001
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004002out:
4003 vm_unlock(&from_locked);
4004 return ret;
4005}
4006
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004007struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
4008 ffa_memory_region_flags_t flags,
4009 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004010{
4011 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004012 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004013
Olivier Deprez55a189e2021-06-09 15:45:27 +02004014 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00004015 struct vm_locked to_locked = vm_lock(to);
4016
Andrew Walbranca808b12020-05-15 17:22:28 +01004017 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004018 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004019
Andrew Walbran290b0c92020-02-03 16:37:14 +00004020 vm_unlock(&to_locked);
4021 } else {
J-Alvesfc19b372022-07-06 12:17:35 +01004022 ret = plat_ffa_other_world_mem_reclaim(to, handle, flags,
4023 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01004024 }
4025
4026 return ret;
4027}
4028
4029struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
4030 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01004031 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01004032 struct vcpu *current)
4033{
4034 struct vm *to = current->vm;
4035 struct vm_locked to_locked;
4036 struct ffa_value ret;
4037
4038 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01004039 if (vm_id_is_current_world(to->id)) {
4040 if (sender_vm_id != 0) {
4041 dlog_verbose("%s: Invalid sender.\n", __func__);
4042 return ffa_error(FFA_INVALID_PARAMETERS);
4043 }
Andrew Walbranca808b12020-05-15 17:22:28 +01004044 }
4045
4046 to_locked = vm_lock(to);
4047
J-Alves122f1a12022-12-12 15:55:42 +00004048 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004049 /*
4050 * Can't retrieve memory information if the mailbox is not
4051 * available.
4052 */
J-Alves59ed0042022-07-28 18:26:41 +01004053 dlog_verbose("%s: RX buffer not ready partition %x.\n",
4054 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01004055 ret = ffa_error(FFA_BUSY);
4056 goto out;
4057 }
4058
4059 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01004060 sender_vm_id, &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01004061out:
4062 vm_unlock(&to_locked);
4063 return ret;
4064}
4065
4066struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
4067 uint32_t fragment_length,
J-Alves19e20cf2023-08-02 12:48:55 +01004068 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01004069 struct vcpu *current)
4070{
4071 struct vm *from = current->vm;
4072 const void *from_msg;
4073 void *fragment_copy;
4074 struct ffa_value ret;
4075
4076 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01004077 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
4078 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01004079 return ffa_error(FFA_INVALID_PARAMETERS);
4080 }
4081
4082 /*
4083 * Check that the sender has configured its send buffer. If the TX
4084 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
4085 * be safely accessed after releasing the lock since the TX mailbox
4086 * address can only be configured once.
4087 */
4088 sl_lock(&from->lock);
4089 from_msg = from->mailbox.send;
4090 sl_unlock(&from->lock);
4091
4092 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01004093 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01004094 return ffa_error(FFA_INVALID_PARAMETERS);
4095 }
4096
4097 /*
4098 * Copy the fragment to a fresh page from the memory pool. This prevents
4099 * the sender from changing it underneath us, and also lets us keep it
4100 * around in the share state table if needed.
4101 */
4102 if (fragment_length > HF_MAILBOX_SIZE ||
4103 fragment_length > MM_PPOOL_ENTRY_SIZE) {
4104 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004105 "Fragment length %d larger than mailbox size %zu.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01004106 fragment_length, HF_MAILBOX_SIZE);
4107 return ffa_error(FFA_INVALID_PARAMETERS);
4108 }
4109 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
4110 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
4111 0) {
4112 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
4113 return ffa_error(FFA_INVALID_PARAMETERS);
4114 }
4115 fragment_copy = mpool_alloc(&api_page_pool);
4116 if (fragment_copy == NULL) {
4117 dlog_verbose("Failed to allocate fragment copy.\n");
4118 return ffa_error(FFA_NO_MEMORY);
4119 }
J-Alves8f2150d2024-03-28 16:15:56 +00004120
4121 if (!memcpy_trapped(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg,
4122 fragment_length)) {
4123 dlog_error("%s: Failed to copy fragment.\n", __func__);
4124 return ffa_error(FFA_ABORTED);
4125 }
Andrew Walbranca808b12020-05-15 17:22:28 +01004126
4127 /*
4128 * Hafnium doesn't support fragmentation of memory retrieve requests
4129 * (because it doesn't support caller-specified mappings, so a request
4130 * will never be larger than a single page), so this must be part of a
4131 * memory send (i.e. donate, lend or share) request.
4132 *
4133 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01004134 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01004135 */
J-Alvesfdd29272022-07-19 13:16:31 +01004136 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004137 struct vm_locked from_locked = vm_lock(from);
4138
4139 ret = ffa_memory_send_continue(from_locked, fragment_copy,
4140 fragment_length, handle,
4141 &api_page_pool);
4142 /*
4143 * `ffa_memory_send_continue` takes ownership of the
4144 * fragment_copy, so we don't need to free it here.
4145 */
4146 vm_unlock(&from_locked);
4147 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01004148 ret = plat_ffa_other_world_mem_send_continue(
4149 from, fragment_copy, fragment_length, handle,
4150 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00004151 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00004152
4153 return ret;
4154}
Max Shvetsov40108e72020-08-27 12:39:50 +01004155
Olivier Deprezd614d322021-06-18 15:21:00 +02004156/**
4157 * Register an entry point for a vCPU in warm boot cases.
4158 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
4159 */
Max Shvetsov40108e72020-08-27 12:39:50 +01004160struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
4161 struct vcpu *current)
4162{
4163 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01004164 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01004165
Olivier Deprezd614d322021-06-18 15:21:00 +02004166 /*
4167 * Reject if interface is not supported at this FF-A instance
4168 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
4169 */
4170 if (!plat_ffa_is_secondary_ep_register_supported() ||
Karl Meakin82041822024-05-20 11:14:34 +01004171 vm_is_up(current->vm)) {
Olivier Deprezd614d322021-06-18 15:21:00 +02004172 return ffa_error(FFA_NOT_SUPPORTED);
4173 }
4174
4175 /*
4176 * No further check is made on the address validity
4177 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
4178 * from the VM or vCPU structure.
4179 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
4180 * For each SP [...] the Framework assumes that the same entry point
4181 * address is used for initializing any execution context during a
4182 * secondary cold boot.
4183 * If this function is invoked multiple times, then the entry point
4184 * address specified in the last valid invocation must be used by the
4185 * callee.
4186 */
Olivier Deprezb2808332023-02-02 15:25:40 +01004187 current_locked = vcpu_lock(current);
4188 if (current->rt_model != RTM_SP_INIT) {
4189 dlog_error(
4190 "FFA_SECONDARY_EP_REGISTER can only be called while "
4191 "vCPU in run-time state for initialization.\n");
4192 vcpu_unlock(&current_locked);
4193 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02004194 }
Olivier Deprezb2808332023-02-02 15:25:40 +01004195 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02004196
Olivier Deprezb2808332023-02-02 15:25:40 +01004197 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01004198 vm_locked.vm->secondary_ep = entry_point;
4199 vm_unlock(&vm_locked);
4200
Olivier Deprezb2808332023-02-02 15:25:40 +01004201 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01004202}
J-Alvesa0f317d2021-06-09 13:31:59 +01004203
J-Alves19e20cf2023-08-02 12:48:55 +01004204struct ffa_value api_ffa_notification_bitmap_create(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004205 ffa_vcpu_count_t vcpu_count,
4206 struct vcpu *current)
4207{
J-Alves7ccaccf2024-02-01 14:25:23 +00004208 const struct ffa_value ret =
4209 plat_ffa_is_notifications_bitmap_access_valid(current, vm_id);
4210
4211 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4212 dlog_verbose(
4213 "FFA_NOTIFICATION_BITMAP_CREATE to be used by "
4214 "hypervisor for valid NWd VM IDs only (%x).\n",
4215 vm_id);
4216 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004217 }
4218
4219 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
4220}
4221
J-Alves19e20cf2023-08-02 12:48:55 +01004222struct ffa_value api_ffa_notification_bitmap_destroy(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004223 struct vcpu *current)
4224{
J-Alves7ccaccf2024-02-01 14:25:23 +00004225 const struct ffa_value ret =
4226 plat_ffa_is_notifications_bitmap_access_valid(current, vm_id);
4227
4228 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4229 dlog_verbose(
4230 "FFA_NOTIFICATION_BITMAP_DESTROY to be used by "
4231 "hypervisor for valid NWd VM IDs only (%x).\n",
4232 vm_id);
4233 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004234 }
4235
4236 return plat_ffa_notifications_bitmap_destroy(vm_id);
4237}
J-Alvesc003a7a2021-03-18 13:06:53 +00004238
4239struct ffa_value api_ffa_notification_update_bindings(
J-Alves19e20cf2023-08-02 12:48:55 +01004240 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesc003a7a2021-03-18 13:06:53 +00004241 ffa_notifications_bitmap_t notifications, bool is_bind,
4242 struct vcpu *current)
4243{
4244 struct ffa_value ret = {.func = FFA_SUCCESS_32};
4245 struct vm_locked receiver_locked;
4246 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
J-Alves19e20cf2023-08-02 12:48:55 +01004247 const ffa_id_t id_to_update = is_bind ? sender_vm_id : HF_INVALID_VM_ID;
4248 const ffa_id_t id_to_validate =
J-Alvesc003a7a2021-03-18 13:06:53 +00004249 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01004250 const uint32_t flags_mbz =
4251 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
4252
4253 if ((flags_mbz & flags) != 0U) {
4254 return ffa_error(FFA_INVALID_PARAMETERS);
4255 }
J-Alvesc003a7a2021-03-18 13:06:53 +00004256
4257 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
4258 receiver_vm_id)) {
4259 dlog_verbose("Invalid use of notifications bind interface.\n");
4260 return ffa_error(FFA_INVALID_PARAMETERS);
4261 }
4262
J-Alvesb15e9402021-09-08 11:44:42 +01004263 if (plat_ffa_notifications_update_bindings_forward(
4264 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
4265 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01004266 return ret;
4267 }
4268
J-Alvesc003a7a2021-03-18 13:06:53 +00004269 if (notifications == 0U) {
Karl Meakine8937d92024-03-19 16:04:25 +00004270 dlog_verbose("No notifications have been specified %lx.\n",
J-Alves1512acc2024-02-01 16:57:10 +00004271 notifications);
J-Alvesc003a7a2021-03-18 13:06:53 +00004272 return ffa_error(FFA_INVALID_PARAMETERS);
4273 }
4274
4275 /**
4276 * This check assumes receiver is the current VM, and has been enforced
4277 * by 'plat_ffa_is_notifications_bind_valid'.
4278 */
4279 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4280
4281 if (receiver_locked.vm == NULL) {
4282 dlog_verbose("Receiver doesn't exist!\n");
J-Alves7333dcf2024-02-01 17:25:10 +00004283 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesc003a7a2021-03-18 13:06:53 +00004284 }
4285
J-Alves09ff9d82021-11-02 11:55:20 +00004286 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004287 dlog_verbose("Notifications are not enabled.\n");
4288 ret = ffa_error(FFA_NOT_SUPPORTED);
4289 goto out;
4290 }
4291
4292 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
4293 vm_find(sender_vm_id) == NULL) {
4294 dlog_verbose("Sender VM does not exist!\n");
4295 ret = ffa_error(FFA_INVALID_PARAMETERS);
4296 goto out;
4297 }
4298
4299 /*
4300 * Can't bind/unbind notifications if at least one is bound to a
4301 * different sender.
4302 */
4303 if (!vm_notifications_validate_bound_sender(
J-Alves661e1b72023-08-02 13:39:40 +01004304 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_validate,
4305 notifications)) {
J-Alvesf0208142024-02-01 17:28:58 +00004306 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004307 "Sender %x not permitted to set notifications %lx to "
J-Alvesf0208142024-02-01 17:28:58 +00004308 "%x.\n",
4309 sender_vm_id, notifications, receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004310 ret = ffa_error(FFA_DENIED);
4311 goto out;
4312 }
4313
4314 /**
4315 * Check if there is a pending notification within those specified in
4316 * the bitmap.
4317 */
4318 if (vm_are_notifications_pending(receiver_locked,
J-Alves661e1b72023-08-02 13:39:40 +01004319 ffa_is_vm_id(sender_vm_id),
J-Alvesc003a7a2021-03-18 13:06:53 +00004320 notifications)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004321 dlog_verbose("Notifications within '%lx' pending.\n",
J-Alvesc003a7a2021-03-18 13:06:53 +00004322 notifications);
4323 ret = ffa_error(FFA_DENIED);
4324 goto out;
4325 }
4326
4327 vm_notifications_update_bindings(
J-Alves661e1b72023-08-02 13:39:40 +01004328 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_update,
J-Alvesc003a7a2021-03-18 13:06:53 +00004329 notifications, is_per_vcpu && is_bind);
4330
4331out:
4332 vm_unlock(&receiver_locked);
4333 return ret;
4334}
J-Alvesaa79c012021-07-09 14:29:45 +01004335
4336struct ffa_value api_ffa_notification_set(
J-Alves19e20cf2023-08-02 12:48:55 +01004337 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesaa79c012021-07-09 14:29:45 +01004338 ffa_notifications_bitmap_t notifications, struct vcpu *current)
4339{
4340 struct ffa_value ret;
4341 struct vm_locked receiver_locked;
J-Alvesaa79c012021-07-09 14:29:45 +01004342 /*
4343 * Check if is per-vCPU or global, and extracting vCPU ID according
4344 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
4345 */
4346 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
4347 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
J-Alves6b754a12024-02-14 14:26:30 +00004348 const uint32_t flags_mbz =
4349 ~(FFA_NOTIFICATIONS_FLAG_PER_VCPU |
4350 FFA_NOTIFICATIONS_FLAG_DELAY_SRI | (0xFFFFU << 16));
J-Alves41c5da32024-06-19 10:05:05 +01004351 const bool delay_sri = (FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) != 0U;
J-Alves6b754a12024-02-14 14:26:30 +00004352
4353 if ((flags_mbz & flags) != 0U) {
4354 dlog_verbose("%s: caller shouldn't set bits that MBZ.\n",
4355 __func__);
4356 return ffa_error(FFA_INVALID_PARAMETERS);
4357 }
J-Alvesaa79c012021-07-09 14:29:45 +01004358
J-Alves30ac91d2024-02-14 15:33:12 +00004359 /* Global notifications must target any vCPU. */
4360 if (!is_per_vcpu && vcpu_id != 0U) {
4361 dlog_verbose(
4362 "For global notifications vCPU ID MBZ in call to set "
4363 "notifications.\n");
4364 return ffa_error(FFA_INVALID_PARAMETERS);
4365 }
4366
J-Alvesaa79c012021-07-09 14:29:45 +01004367 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
4368 receiver_vm_id)) {
4369 dlog_verbose("Invalid use of notifications set interface.\n");
4370 return ffa_error(FFA_INVALID_PARAMETERS);
4371 }
4372
4373 if (notifications == 0U) {
4374 dlog_verbose("No notifications have been specified.\n");
4375 return ffa_error(FFA_INVALID_PARAMETERS);
4376 }
4377
J-Alves41c5da32024-06-19 10:05:05 +01004378 /*
4379 * The 'Delay Schedule Receiver interrupt flag' only applies to the
4380 * secure virtual FF-A instance.
4381 */
4382 if (!vm_id_is_current_world(sender_vm_id) && delay_sri) {
4383 dlog_verbose(
4384 "The delay SRI flag can only be set at the secure "
4385 "virtual FF-A instance.\n");
4386 return ffa_error(FFA_INVALID_PARAMETERS);
4387 }
4388
J-Alvesde7bd2f2021-09-09 19:54:35 +01004389 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
4390 flags, notifications, &ret)) {
4391 return ret;
4392 }
4393
J-Alvesaa79c012021-07-09 14:29:45 +01004394 /*
4395 * This check assumes receiver is the current VM, and has been enforced
4396 * by 'plat_ffa_is_notification_set_valid'.
4397 */
4398 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4399
4400 if (receiver_locked.vm == NULL) {
4401 dlog_verbose("Receiver ID is not valid.\n");
4402 return ffa_error(FFA_INVALID_PARAMETERS);
4403 }
4404
J-Alves09ff9d82021-11-02 11:55:20 +00004405 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004406 dlog_verbose("Receiver's notifications not enabled.\n");
4407 ret = ffa_error(FFA_DENIED);
4408 goto out;
4409 }
4410
4411 /*
J-Alves89c22132024-02-14 15:44:41 +00004412 * Check the if the notifications are bound as global if per-vCPU flag
4413 * is set, or if they are bound as per-vCPU and caller setting as
4414 * global. In either case, return FFA_INVALID_PARAMETERS.
4415 */
4416 if (vm_notifications_validate_binding(
4417 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4418 notifications, !is_per_vcpu)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004419 dlog_verbose("Notifications in %lx are %s\n", notifications,
J-Alves89c22132024-02-14 15:44:41 +00004420 !is_per_vcpu ? "global" : "per-vCPU");
4421 ret = ffa_error(FFA_INVALID_PARAMETERS);
4422 goto out;
4423 }
4424
4425 /*
J-Alvesaa79c012021-07-09 14:29:45 +01004426 * If notifications are not bound to the sender, they wouldn't be
4427 * enabled either for the receiver.
4428 */
4429 if (!vm_notifications_validate_binding(
J-Alves661e1b72023-08-02 13:39:40 +01004430 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4431 notifications, is_per_vcpu)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004432 dlog_verbose("Notifications bindings not valid.\n");
4433 ret = ffa_error(FFA_DENIED);
4434 goto out;
4435 }
4436
4437 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
4438 dlog_verbose("Invalid VCPU ID!\n");
4439 ret = ffa_error(FFA_INVALID_PARAMETERS);
4440 goto out;
4441 }
4442
J-Alves7461ef22021-10-18 17:21:33 +01004443 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00004444 vm_notifications_partition_set_pending(
J-Alves661e1b72023-08-02 13:39:40 +01004445 receiver_locked, ffa_is_vm_id(sender_vm_id), notifications,
J-Alves5a16c962022-03-25 12:32:51 +00004446 vcpu_id, is_per_vcpu);
4447
Karl Meakine8937d92024-03-19 16:04:25 +00004448 dlog_verbose("Set the notifications: %lx.\n", notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004449
J-Alves41c5da32024-06-19 10:05:05 +01004450 if (!delay_sri) {
J-Alves13394022021-06-30 13:48:49 +01004451 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
4452 vcpu_index(current));
4453 plat_ffa_sri_trigger_not_delayed(current->cpu);
4454 } else {
4455 plat_ffa_sri_state_set(DELAYED);
4456 }
J-Alvesaa79c012021-07-09 14:29:45 +01004457
J-Alves13394022021-06-30 13:48:49 +01004458 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01004459out:
4460 vm_unlock(&receiver_locked);
4461
4462 return ret;
4463}
4464
4465static struct ffa_value api_ffa_notification_get_success_return(
4466 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
4467 ffa_notifications_bitmap_t from_framework)
4468{
4469 return (struct ffa_value){
4470 .func = FFA_SUCCESS_32,
4471 .arg1 = 0U,
4472 .arg2 = (uint32_t)from_sp,
4473 .arg3 = (uint32_t)(from_sp >> 32),
4474 .arg4 = (uint32_t)from_vm,
4475 .arg5 = (uint32_t)(from_vm >> 32),
4476 .arg6 = (uint32_t)from_framework,
4477 .arg7 = (uint32_t)(from_framework >> 32),
4478 };
4479}
4480
J-Alves19e20cf2023-08-02 12:48:55 +01004481struct ffa_value api_ffa_notification_get(ffa_id_t receiver_vm_id,
J-Alvesaa79c012021-07-09 14:29:45 +01004482 ffa_vcpu_index_t vcpu_id,
4483 uint32_t flags, struct vcpu *current)
4484{
J-Alves663682a2022-03-25 13:56:51 +00004485 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01004486 ffa_notifications_bitmap_t sp_notifications = 0;
4487 ffa_notifications_bitmap_t vm_notifications = 0;
4488 struct vm_locked receiver_locked;
4489 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01004490 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
4491 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
4492 FFA_NOTIFICATION_FLAG_BITMAP_SP |
4493 FFA_NOTIFICATION_FLAG_BITMAP_VM);
4494
4495 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
4496 if ((flags & flags_mbz) != 0U) {
4497 dlog_verbose(
4498 "Invalid flags bit(s) set in notifications get. [31:4] "
4499 "MBZ(%x)\n",
4500 flags);
4501 return ffa_error(FFA_INVALID_PARAMETERS);
4502 }
J-Alvesaa79c012021-07-09 14:29:45 +01004503
4504 /*
J-Alvesfc95a302022-04-22 14:18:23 +01004505 * Following check should capture wrong uses of the interface,
4506 * depending on whether Hafnium is SPMC or hypervisor. On the
4507 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01004508 */
J-Alvesfc95a302022-04-22 14:18:23 +01004509 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
4510 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004511 dlog_verbose("Invalid use of notifications get interface.\n");
4512 return ffa_error(FFA_INVALID_PARAMETERS);
4513 }
4514
4515 /*
4516 * This check assumes receiver is the current VM, and has been enforced
4517 * by `plat_ffa_is_notifications_get_valid`.
4518 */
4519 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
4520
4521 /*
4522 * `plat_ffa_is_notifications_get_valid` ensures following is never
4523 * true.
4524 */
4525 CHECK(receiver_locked.vm != NULL);
4526
J-Alves60458812024-03-22 11:57:10 +00004527 if (receiver_locked.vm->vcpu_count <= vcpu_id) {
J-Alves1abb3342022-01-05 11:59:10 +00004528 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004529 "Invalid VCPU ID %u. vcpu count %u current core: "
4530 "%zu!\n",
J-Alves1abb3342022-01-05 11:59:10 +00004531 vcpu_id, receiver_locked.vm->vcpu_count,
4532 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01004533 ret = ffa_error(FFA_INVALID_PARAMETERS);
4534 goto out;
4535 }
4536
4537 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01004538 if (!plat_ffa_notifications_get_from_sp(
4539 receiver_locked, vcpu_id, &sp_notifications,
4540 &ret)) {
4541 dlog_verbose("Failed to get notifications from sps.");
4542 goto out;
4543 }
J-Alvesaa79c012021-07-09 14:29:45 +01004544 }
4545
4546 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00004547 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01004548 receiver_locked, true, vcpu_id);
4549 }
4550
J-Alvesd605a092022-03-28 14:20:48 +01004551 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
4552 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
4553 if (!plat_ffa_notifications_get_framework_notifications(
4554 receiver_locked, &framework_notifications, flags,
4555 vcpu_id, &ret)) {
4556 dlog_verbose(
4557 "Failed to get notifications from "
4558 "framework.\n");
4559 goto out;
4560 }
4561 }
4562
J-Alves663682a2022-03-25 13:56:51 +00004563 ret = api_ffa_notification_get_success_return(
4564 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004565
J-Alvesfe23ebe2021-10-13 16:07:07 +01004566 /*
4567 * If there are no more pending notifications, change `sri_state` to
4568 * handled.
4569 */
4570 if (vm_is_notifications_pending_count_zero()) {
4571 plat_ffa_sri_state_set(HANDLED);
4572 }
4573
J-Alves6e2abc62021-12-02 14:58:56 +00004574 if (!receiver_locked.vm->el0_partition &&
4575 !vm_are_global_notifications_pending(receiver_locked)) {
4576 vm_notifications_set_npi_injected(receiver_locked, false);
4577 }
4578
J-Alvesaa79c012021-07-09 14:29:45 +01004579out:
4580 vm_unlock(&receiver_locked);
4581
4582 return ret;
4583}
J-Alvesc8e8a222021-06-08 17:33:52 +01004584
4585/**
4586 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
4587 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
4588 */
4589static struct ffa_value api_ffa_notification_info_get_success_return(
4590 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01004591 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01004592{
4593 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
4594
4595 /*
4596 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
4597 * hold the list of ids.
4598 */
4599 memcpy_s(&ret.arg3,
4600 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
4601 sizeof(ids[0]) * ids_count);
4602
4603 /*
4604 * According to the spec x2 should have:
4605 * - Bit flagging if there are more notifications pending;
4606 * - The total number of elements (i.e. total list size);
4607 * - The number of VCPU IDs within each VM specific list.
4608 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004609 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
4610 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
4611 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01004612
4613 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
4614 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
4615
4616 for (unsigned int i = 0; i < lists_count; i++) {
4617 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
4618 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
4619 }
4620
4621 return ret;
4622}
4623
4624struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
4625{
4626 /*
4627 * Following set of variables should be populated with the return info.
4628 * At a successfull handling of this interface, they should be used
4629 * to populate the 'ret' structure in accordance to the table 17.29
4630 * of the FF-A v1.1 Beta0 specification.
4631 */
4632 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
4633 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
4634 uint32_t lists_count = 0;
4635 uint32_t ids_count = 0;
4636 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01004637 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004638
4639 /*
4640 * This interface can only be called at NS virtual/physical FF-A
4641 * instance by the endpoint implementing the primary scheduler and the
4642 * Hypervisor/OS kernel.
4643 * In the SPM, following check passes if call has been forwarded from
4644 * the hypervisor.
4645 */
Karl Meakin5e996992024-05-20 11:27:07 +01004646
4647 if (!vm_is_primary(current->vm)) {
J-Alvesc8e8a222021-06-08 17:33:52 +01004648 dlog_verbose(
4649 "Only the receiver's scheduler can use this "
4650 "interface\n");
4651 return ffa_error(FFA_NOT_SUPPORTED);
4652 }
4653
J-Alvesca058c22021-09-10 14:02:07 +01004654 /*
4655 * Forward call to the other world, and fill the arrays used to assemble
4656 * return.
4657 */
4658 plat_ffa_notification_info_get_forward(
4659 ids, &ids_count, lists_sizes, &lists_count,
4660 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4661
4662 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
4663
J-Alvesc8e8a222021-06-08 17:33:52 +01004664 /* Get notifications' info from this world */
4665 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
4666 ++index) {
4667 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
4668
4669 list_is_full = vm_notifications_info_get(
4670 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
4671 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4672
4673 vm_unlock(&vm_locked);
4674 }
4675
4676 if (!list_is_full) {
4677 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004678 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01004679 ids, &ids_count, lists_sizes, &lists_count,
4680 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4681 }
4682
4683 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01004684 dlog_verbose(
4685 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01004686 result = ffa_error(FFA_NO_DATA);
4687 } else {
4688 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01004689 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01004690 }
4691
J-Alvesfe23ebe2021-10-13 16:07:07 +01004692 plat_ffa_sri_state_set(HANDLED);
4693
J-Alves13394022021-06-30 13:48:49 +01004694 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004695}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004696
4697struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
4698{
4699 struct vm_locked vm_locked;
4700 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
4701 bool mode_ret = false;
4702 uint32_t mode = 0;
4703
4704 if (!plat_ffa_is_mem_perm_get_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004705 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004706 }
4707
4708 if (!(current->vm->el0_partition)) {
4709 return ffa_error(FFA_DENIED);
4710 }
4711
4712 vm_locked = vm_lock(current->vm);
4713
4714 /*
4715 * mm_get_mode is used to check if the given base_addr page is already
4716 * mapped. If the page is unmapped, return error. If the page is mapped
4717 * appropriate attributes are returned to the caller. Note that
4718 * mm_get_mode returns true if the address is in the valid VA range as
4719 * supported by the architecture and MMU configurations, as opposed to
4720 * whether a page is mapped or not. For a page to be known as mapped,
4721 * the API must return true AND the returned mode must not have
4722 * MM_MODE_INVALID set.
4723 */
4724 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4725 va_add(base_addr, PAGE_SIZE), &mode);
4726 if (!mode_ret || (mode & MM_MODE_INVALID)) {
4727 ret = ffa_error(FFA_INVALID_PARAMETERS);
4728 goto out;
4729 }
4730
4731 /* No memory should be marked RWX */
4732 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
4733 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
4734
4735 /*
4736 * S-EL0 partitions are expected to have all their pages marked as
4737 * non-global.
4738 */
4739 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
4740 (MM_MODE_NG | MM_MODE_USER));
4741
4742 if (mode & MM_MODE_W) {
4743 /* No memory should be writeable but not readable. */
4744 CHECK(mode & MM_MODE_R);
4745 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4746 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
4747 } else if (mode & MM_MODE_R) {
4748 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4749 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
4750 if (!(mode & MM_MODE_X)) {
4751 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
4752 }
4753 }
4754out:
4755 vm_unlock(&vm_locked);
4756 return ret;
4757}
4758
4759struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
4760 uint32_t mem_perm, struct vcpu *current)
4761{
4762 struct vm_locked vm_locked;
4763 struct ffa_value ret;
4764 bool mode_ret = false;
4765 uint32_t original_mode;
4766 uint32_t new_mode;
4767 struct mpool local_page_pool;
4768
4769 if (!plat_ffa_is_mem_perm_set_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004770 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004771 }
4772
4773 if (!(current->vm->el0_partition)) {
4774 return ffa_error(FFA_DENIED);
4775 }
4776
4777 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
4778 return ffa_error(FFA_INVALID_PARAMETERS);
4779 }
4780
4781 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
4782 (mem_perm != FFA_MEM_PERM_RX)) {
4783 return ffa_error(FFA_INVALID_PARAMETERS);
4784 }
4785
4786 /*
4787 * Create a local pool so any freed memory can't be used by another
4788 * thread. This is to ensure the original mapping can be restored if any
4789 * stage of the process fails.
4790 */
4791 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
4792
4793 vm_locked = vm_lock(current->vm);
4794
4795 /*
4796 * All regions accessible by the partition are mapped during boot. If we
4797 * cannot get a successful translation for the page range, the request
4798 * to change permissions is rejected.
4799 * mm_get_mode is used to check if the given address range is already
4800 * mapped. If the range is unmapped, return error. If the range is
4801 * mapped appropriate attributes are returned to the caller. Note that
4802 * mm_get_mode returns true if the address is in the valid VA range as
4803 * supported by the architecture and MMU configurations, as opposed to
4804 * whether a page is mapped or not. For a page to be known as mapped,
4805 * the API must return true AND the returned mode must not have
4806 * MM_MODE_INVALID set.
4807 */
4808
4809 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4810 va_add(base_addr, page_count * PAGE_SIZE),
4811 &original_mode);
4812 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
4813 ret = ffa_error(FFA_INVALID_PARAMETERS);
4814 goto out;
4815 }
4816
4817 /* Device memory cannot be marked as executable */
4818 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
4819 ret = ffa_error(FFA_INVALID_PARAMETERS);
4820 goto out;
4821 }
4822
4823 new_mode = MM_MODE_USER | MM_MODE_NG;
4824
4825 if (mem_perm == FFA_MEM_PERM_RW) {
4826 new_mode |= MM_MODE_R | MM_MODE_W;
4827 } else if (mem_perm == FFA_MEM_PERM_RX) {
4828 new_mode |= MM_MODE_R | MM_MODE_X;
4829 } else if (mem_perm == FFA_MEM_PERM_RO) {
4830 new_mode |= MM_MODE_R;
4831 }
4832
4833 /*
4834 * Safe to re-map memory, since we know the requested permissions are
4835 * valid, and the memory requested to be re-mapped is also valid.
4836 */
4837 if (!mm_identity_prepare(
4838 &vm_locked.vm->ptable, pa_from_va(base_addr),
4839 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4840 new_mode, &local_page_pool)) {
4841 /*
4842 * Defrag the table into the local page pool.
4843 * mm_identity_prepare could have allocated or freed pages to
4844 * split blocks or tables etc.
4845 */
4846 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
4847
4848 /*
4849 * Guaranteed to succeed mapping with old mode since the mapping
4850 * with old mode already existed and we have a local page pool
4851 * that should have sufficient memory to go back to the original
4852 * state.
4853 */
4854 CHECK(mm_identity_prepare(
4855 &vm_locked.vm->ptable, pa_from_va(base_addr),
4856 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4857 original_mode, &local_page_pool));
4858 mm_identity_commit(
4859 &vm_locked.vm->ptable, pa_from_va(base_addr),
4860 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4861 original_mode, &local_page_pool);
4862
4863 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
4864 ret = ffa_error(FFA_NO_MEMORY);
4865 goto out;
4866 }
4867
4868 mm_identity_commit(
4869 &vm_locked.vm->ptable, pa_from_va(base_addr),
4870 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
4871 &local_page_pool);
4872
4873 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
4874
4875out:
4876 mpool_fini(&local_page_pool);
4877 vm_unlock(&vm_locked);
4878
4879 return ret;
4880}
Maksims Svecovs71b76702022-05-20 15:32:58 +01004881
4882/**
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004883 * Send the contents of the given vCPU's log buffer to the log, preceded
Karl Meakin705b56e2024-05-22 10:04:32 +01004884 * by the VM ID and followed by a newline.
4885 */
Karl Meakin6f1f1212024-07-16 10:18:16 +01004886void api_flush_log_buffer(struct vcpu_locked *vcpu_locked)
Karl Meakin705b56e2024-05-22 10:04:32 +01004887{
Karl Meakin6f1f1212024-07-16 10:18:16 +01004888 /*
4889 * NOTE: This line is parsed by `hftest.py`.
4890 * If you change the format, make sure to update
4891 * `HFTEST_CTRL_JSON_REGEX` as well.
4892 */
4893 struct vcpu *vcpu = vcpu_locked->vcpu;
4894 struct log_buffer *buffer = &vcpu->log_buffer;
4895 ffa_id_t vm_id = vcpu->vm->id;
4896 ffa_id_t vcpu_id = vcpu_index(vcpu);
4897
Karl Meakin7de26952024-06-14 14:50:19 +01004898 buffer->chars[buffer->len] = '\0';
Olivier Deprezbd432092024-07-26 14:01:12 +02004899 dlog("[%x %u] %s\n", vm_id, vcpu_id, buffer->chars);
Karl Meakin7de26952024-06-14 14:50:19 +01004900 buffer->len = 0;
Karl Meakin705b56e2024-05-22 10:04:32 +01004901}
4902
4903/**
Karl Meakin740f74f2024-02-14 18:04:48 +00004904 * Implements FF-A v1.2 FFA_CONSOLE_LOG ABI for buffered logging.
Maksims Svecovs71b76702022-05-20 15:32:58 +01004905 */
4906struct ffa_value api_ffa_console_log(const struct ffa_value args,
4907 struct vcpu *current)
4908{
Karl Meakin740f74f2024-02-14 18:04:48 +00004909 /* Maximum number of characters is 128: 16 registers of 8 bytes each. */
4910 char chars[128] = {0};
Karl Meakin0e617d92024-04-05 12:55:22 +01004911 const bool v1_2 = current->vm->ffa_version >= FFA_VERSION_1_2;
Karl Meakin740f74f2024-02-14 18:04:48 +00004912 const bool log32 = args.func == FFA_CONSOLE_LOG_32;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004913
Karl Meakin740f74f2024-02-14 18:04:48 +00004914 /*
4915 * 32bit: always 6 registers
4916 * 64bit and less than v1.2: 6 registers
4917 * 64bit and v1.2 or greater: 16 registers
4918 */
Karl Meakin66a38bd2024-05-28 16:00:56 +01004919 /* NOLINTNEXTLINE(readability-avoid-nested-conditional-operator) */
Karl Meakin740f74f2024-02-14 18:04:48 +00004920 const size_t registers_max = log32 ? 6 : (v1_2 ? 16 : 6);
4921 const size_t chars_max =
4922 registers_max * (log32 ? sizeof(uint32_t) : sizeof(uint64_t));
4923 const size_t chars_count = args.arg1;
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004924 struct vcpu_locked vcpu_locked;
4925 struct log_buffer *log_buffer;
Karl Meakin740f74f2024-02-14 18:04:48 +00004926
4927 assert(args.func == FFA_CONSOLE_LOG_32 ||
4928 args.func == FFA_CONSOLE_LOG_64);
4929
4930 if (chars_count == 0 || chars_count > chars_max) {
Maksims Svecovs71b76702022-05-20 15:32:58 +01004931 return ffa_error(FFA_INVALID_PARAMETERS);
4932 }
4933
Karl Meakin740f74f2024-02-14 18:04:48 +00004934 if (log32) {
4935 uint32_t *registers = (uint32_t *)chars;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004936
Karl Meakin740f74f2024-02-14 18:04:48 +00004937 registers[0] = args.arg2 & 0xffffffff;
4938 registers[1] = args.arg3 & 0xffffffff;
4939 registers[2] = args.arg4 & 0xffffffff;
4940 registers[3] = args.arg5 & 0xffffffff;
4941 registers[4] = args.arg6 & 0xffffffff;
4942 registers[5] = args.arg7 & 0xffffffff;
4943 } else {
4944 uint64_t *registers = (uint64_t *)chars;
4945
4946 registers[0] = args.arg2;
4947 registers[1] = args.arg3;
4948 registers[2] = args.arg4;
4949 registers[3] = args.arg5;
4950 registers[4] = args.arg6;
4951 registers[5] = args.arg7;
4952 if (v1_2) {
4953 registers[6] = args.extended_val.arg8;
4954 registers[7] = args.extended_val.arg9;
4955 registers[8] = args.extended_val.arg10;
4956 registers[9] = args.extended_val.arg11;
4957 registers[10] = args.extended_val.arg12;
4958 registers[11] = args.extended_val.arg13;
4959 registers[12] = args.extended_val.arg14;
4960 registers[13] = args.extended_val.arg15;
4961 registers[14] = args.extended_val.arg16;
4962 registers[15] = args.extended_val.arg17;
4963 }
4964 }
4965
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004966 vcpu_locked = vcpu_lock(current);
4967 log_buffer = &current->log_buffer;
Karl Meakin7de26952024-06-14 14:50:19 +01004968
Karl Meakin740f74f2024-02-14 18:04:48 +00004969 for (size_t i = 0; i < chars_count; i++) {
4970 bool flush = false;
4971 const char c = chars[i];
4972
4973 if (c == '\n' || c == '\0') {
4974 flush = true;
4975 } else {
Karl Meakin7de26952024-06-14 14:50:19 +01004976 log_buffer->chars[log_buffer->len++] = c;
4977 flush = log_buffer->len == LOG_BUFFER_SIZE;
Karl Meakin740f74f2024-02-14 18:04:48 +00004978 }
4979
4980 if (flush) {
Karl Meakin6f1f1212024-07-16 10:18:16 +01004981 api_flush_log_buffer(&vcpu_locked);
Karl Meakin740f74f2024-02-14 18:04:48 +00004982 }
4983 }
Maksims Svecovs71b76702022-05-20 15:32:58 +01004984
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004985 vcpu_unlock(&vcpu_locked);
Maksims Svecovs71b76702022-05-20 15:32:58 +01004986 return (struct ffa_value){.func = FFA_SUCCESS_32};
4987}