blob: d4f5993839ef49cb155e36d49b51a6e2e5b922a1 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Federico Recanati25053ee2022-03-14 15:01:53 +01002 * Copyright 2022 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +000012#include "hf/arch/ffa.h"
J-Alves0a21db12024-03-28 12:43:30 +000013#include "hf/arch/memcpy_trapped.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020014#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020015#include "hf/arch/other_world.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000016#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000017
Karl Meakin49ec1e42024-05-10 13:08:24 +010018#include "hf/bits.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010019#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000020#include "hf/dlog.h"
Karl Meakin902af082024-11-28 14:58:38 +000021#include "hf/ffa.h"
22#include "hf/ffa/cpu_cycles.h"
23#include "hf/ffa/direct_messaging.h"
24#include "hf/ffa/ffa_memory.h"
25#include "hf/ffa/indirect_messaging.h"
26#include "hf/ffa/interrupts.h"
27#include "hf/ffa/notifications.h"
Karl Meakin902af082024-11-28 14:58:38 +000028#include "hf/ffa/setup_and_discovery.h"
29#include "hf/ffa/vm.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010030#include "hf/ffa_internal.h"
31#include "hf/ffa_memory.h"
J-Alves33c47bf2022-09-29 11:36:20 +010032#include "hf/ffa_v1_0.h"
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +010033#include "hf/hf_ipi.h"
Andrew Scull6386f252018-12-06 13:29:10 +000034#include "hf/mm.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010035#include "hf/plat/interrupts.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010036#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010037#include "hf/std.h"
Madhukar Pappireddy32424db2024-09-25 15:06:19 -050038#include "hf/timer_mgmt.h"
Karl Meakinc5cebbc2024-06-17 11:30:27 +010039#include "hf/vcpu.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010040#include "hf/vm.h"
41
Andrew Scullf35a5c92018-08-07 18:09:46 +010042#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043#include "vmapi/hf/ffa.h"
J-Alves126ab502022-09-29 11:37:33 +010044#include "vmapi/hf/ffa_v1_0.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010045
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000046static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010047 "Partition information descriptor size doesn't match the one in "
48 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000049static_assert(sizeof(struct ffa_partition_info) == 24,
50 "Partition information descriptor size doesn't match the one in "
51 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -080052static_assert((sizeof(struct ffa_partition_info) & 7) == 0,
53 "Partition information descriptor must be a multiple of 8 bytes"
54 " for ffa_partition_info_get_regs to work correctly. Information"
55 " from this structure are returned in 8 byte registers and the"
56 " count of 8 byte registers is returned by the ABI.");
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000057/*
58 * To eliminate the risk of deadlocks, we define a partial order for the
59 * acquisition of locks held concurrently by the same physical CPU. Our current
60 * ordering requirements are as follows:
61 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010062 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000063 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010064 * Locks of the same kind require the lock of lowest address to be locked first,
65 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000066 */
67
Andrew Scullaa039b32018-10-04 15:02:26 +010068static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010069 "Currently, a page is mapped for the send and receive buffers so "
70 "the maximum request is the size of a page.");
71
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000072static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
73 "The page pool entry size must be at least as big as the mailbox "
74 "size, so that memory region descriptors can be copied from the "
75 "mailbox for memory sharing.");
76
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -080077/*
78 * Maximum ffa_partition_info entries that can be returned by an invocation
79 * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available
80 * registers/args in struct ffa_value divided by size of struct
81 * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e.
82 * 15 uint64_t fields. For FF-A v1.1, this value should be 5.
83 */
84#define MAX_INFO_REGS_ENTRIES_PER_CALL \
85 ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info))
86static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5,
87 "FF-A v1.1 supports no more than 5 entries"
88 " per FFA_PARTITION_INFO_GET_REGS64 calls");
89
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000090static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000091
92/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000093 * Initialises the API page pool by taking ownership of the contents of the
94 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000095 */
96void api_init(struct mpool *ppool)
97{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000098 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000099}
100
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100101/**
J-Alvesad6a0432021-04-09 16:06:21 +0100102 * Get target VM vCPU:
103 * If VM is UP then return first vCPU.
104 * If VM is MP then return vCPU whose index matches current CPU index.
105 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500106struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +0100107{
108 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
109 struct vcpu *vcpu = NULL;
110
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500111 CHECK((vm != NULL) && (current != NULL));
112
Karl Meakin82041822024-05-20 11:14:34 +0100113 if (vm_is_up(vm)) {
J-Alvesad6a0432021-04-09 16:06:21 +0100114 vcpu = vm_get_vcpu(vm, 0);
115 } else if (current_cpu_index < vm->vcpu_count) {
116 vcpu = vm_get_vcpu(vm, current_cpu_index);
117 }
118
119 return vcpu;
120}
121
122/**
J-Alvesfe7f7372020-11-09 11:32:12 +0000123 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
124 * is given as argument of the function.
125 *
126 * Called to change the context between SPs for direct messaging (when Hafnium
127 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
128 *
129 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100130 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000131 * - MP with pinned Execution Contexts.
132 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600133struct vcpu *api_switch_to_vm(struct vcpu_locked current_locked,
134 struct ffa_value to_ret,
J-Alves19e20cf2023-08-02 12:48:55 +0100135 enum vcpu_state vcpu_state, ffa_id_t to_id)
J-Alvesfe7f7372020-11-09 11:32:12 +0000136{
137 struct vm *to_vm = vm_find(to_id);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600138 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current_locked.vcpu);
J-Alvesfe7f7372020-11-09 11:32:12 +0000139
140 CHECK(next != NULL);
141
142 /* Set the return value for the target VM. */
143 arch_regs_set_retval(&next->regs, to_ret);
144
145 /* Set the current vCPU state. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600146 current_locked.vcpu->state = vcpu_state;
J-Alvesfe7f7372020-11-09 11:32:12 +0000147
148 return next;
149}
150
151/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000152 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100153 *
154 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100155 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100156 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600157struct vcpu *api_switch_to_primary(struct vcpu_locked current_locked,
J-Alves27b71962022-12-12 15:29:58 +0000158 struct ffa_value primary_ret,
159 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100160{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600161 return api_switch_to_vm(current_locked, primary_ret, secondary_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000162 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100163}
164
165/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200166 * Choose next vCPU to run to be the counterpart vCPU in the other
167 * world (run the normal world if currently running in the secure
168 * world). Set current vCPU state to the given vcpu_state parameter.
169 * Set FF-A return values to the target vCPU in the other world.
170 *
171 * Called in context of a direct message response from a secure
172 * partition to a VM.
173 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600174struct vcpu *api_switch_to_other_world(struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100175 struct ffa_value other_world_ret,
176 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200177{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600178 return api_switch_to_vm(current_locked, other_world_ret, vcpu_state,
J-Alvesfe7f7372020-11-09 11:32:12 +0000179 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200180}
181
182/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000183 * Returns true if the given vCPU is executing in context of an
184 * FFA_MSG_SEND_DIRECT_REQ invocation.
185 */
J-Alves27b71962022-12-12 15:29:58 +0000186bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000187{
Kathleen Capellae468c112023-12-13 17:56:28 -0500188 return locked.vcpu->direct_request_origin.vm_id != HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000189}
190
191/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100192 * Returns true if the VM owning the given vCPU is supporting managed exit and
193 * the vCPU is currently processing a managed exit.
194 */
195static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
196{
Karl Meakin117c8082024-12-04 16:03:28 +0000197 return (ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100198 vcpu_locked.vcpu->processing_managed_exit);
199}
200
201/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000202 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000203 */
204struct vcpu *api_preempt(struct vcpu *current)
205{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600206 struct vcpu_locked current_locked;
207 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100208 struct ffa_value ret = {
209 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200210 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000211 };
212
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600213 current_locked = vcpu_lock(current);
214 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_PREEMPTED);
215 vcpu_unlock(&current_locked);
216
217 return next;
Andrew Scull33fecd32019-01-08 14:48:27 +0000218}
219
220/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000221 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000222 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100223 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100224struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100225{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600226 struct vcpu_locked current_locked;
227 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100228 struct ffa_value ret = {
229 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
230 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100231 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000232
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600233 current_locked = vcpu_lock(current);
234 next = api_switch_to_primary(current_locked, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100235 VCPU_STATE_BLOCKED_INTERRUPT);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600236 vcpu_unlock(&current_locked);
237
238 return next;
Andrew Scullaa039b32018-10-04 15:02:26 +0100239}
240
241/**
Andrew Walbran33645652019-04-15 12:29:31 +0100242 * Puts the current vCPU in off mode, and returns to the primary VM.
243 */
244struct vcpu *api_vcpu_off(struct vcpu *current)
245{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600246 struct vcpu_locked current_locked;
247 struct vcpu *next;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100248 struct ffa_value ret = {
249 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
250 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100251 };
252
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600253 current_locked = vcpu_lock(current);
Andrew Walbran33645652019-04-15 12:29:31 +0100254
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600255 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_OFF);
256 vcpu_unlock(&current_locked);
257
258 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100259}
260
261/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500262 * The current vCPU is blocked on some resource and needs to relinquish
263 * control back to the execution context of the endpoint that originally
264 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000265 */
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500266struct ffa_value api_yield(struct vcpu *current, struct vcpu **next,
267 struct ffa_value *args)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000268{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000269 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
270 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500271 bool transition_allowed;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500272 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500273 uint32_t timeout_low = 0;
274 uint32_t timeout_high = 0;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600275 struct vcpu_locked next_locked = (struct vcpu_locked){
276 .vcpu = NULL,
277 };
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500278
279 if (args != NULL) {
280 if (args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
281 args->arg7 != 0U) {
282 dlog_error(
283 "Parameters passed through registers X4-X7 "
284 "must be zero\n");
285 return ffa_error(FFA_INVALID_PARAMETERS);
286 }
287 timeout_low = (uint32_t)args->arg2 & 0xFFFFFFFF;
288 timeout_high = (uint32_t)args->arg3 & 0xFFFFFFFF;
289 }
Andrew Scull66d62bf2019-02-01 13:54:10 +0000290
Karl Meakin5e996992024-05-20 11:27:07 +0100291 if (vm_is_primary(current->vm)) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000292 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000293 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000294 }
295
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000296 current_locked = vcpu_lock(current);
Karl Meakin117c8082024-12-04 16:03:28 +0000297 transition_allowed = ffa_cpu_cycles_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600298 current_locked, current->vm->id, HF_INVALID_VM_ID, next_locked,
299 FFA_YIELD_32, &next_state);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000300
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500301 if (!transition_allowed) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600302 ret = ffa_error(FFA_DENIED);
303 goto out;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000304 }
305
Madhukar Pappireddy65764072023-05-23 17:18:58 -0500306 /*
307 * The current vCPU is expected to move to BLOCKED state. However,
308 * under certain circumstances, it is allowed for the current vCPU
309 * to be resumed immediately without ever moving to BLOCKED state. One
310 * such scenario occurs when an SP's execution context attempts to
311 * yield cycles while handling secure interrupt. Refer to the comments
Karl Meakin117c8082024-12-04 16:03:28 +0000312 * in the SPMC variant of the ffa_cpu_cycles_yield_prepare function.
Madhukar Pappireddy65764072023-05-23 17:18:58 -0500313 */
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -0500314 assert(!vm_id_is_current_world(current->vm->id) ||
315 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500316
Karl Meakin117c8082024-12-04 16:03:28 +0000317 ret = ffa_cpu_cycles_yield_prepare(current_locked, next, timeout_low,
318 timeout_high);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600319out:
320 vcpu_unlock(&current_locked);
321 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000322}
323
324/**
Andrew Walbran33645652019-04-15 12:29:31 +0100325 * Switches to the primary so that it can switch to the target, or kick it if it
326 * is already running on a different physical CPU.
327 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600328static struct vcpu *api_wake_up_locked(struct vcpu_locked current_locked,
329 struct vcpu *target_vcpu)
Andrew Walbran33645652019-04-15 12:29:31 +0100330{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100331 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100332 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100333 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
334 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100335 };
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600336
337 return api_switch_to_primary(current_locked, ret, VCPU_STATE_BLOCKED);
338}
339
340struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
341{
342 struct vcpu_locked current_locked;
343 struct vcpu *next;
344
345 current_locked = vcpu_lock(current);
346 next = api_wake_up_locked(current_locked, target_vcpu);
347 vcpu_unlock(&current_locked);
348
349 return next;
Andrew Walbran33645652019-04-15 12:29:31 +0100350}
351
352/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000353 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000354 */
355struct vcpu *api_abort(struct vcpu *current)
356{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100357 struct ffa_value ret = ffa_error(FFA_ABORTED);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600358 struct vcpu_locked current_locked;
359 struct vcpu *next;
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500360 struct vm_locked vm_locked;
Andrew Scull9726c252019-01-23 13:44:19 +0000361
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100362 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000363 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000364
Karl Meakin5e996992024-05-20 11:27:07 +0100365 if (vm_is_primary(current->vm)) {
Andrew Scull9726c252019-01-23 13:44:19 +0000366 /* TODO: what to do when the primary aborts? */
367 for (;;) {
368 /* Do nothing. */
369 }
370 }
371
372 atomic_store_explicit(&current->vm->aborting, true,
373 memory_order_relaxed);
374
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500375 vm_locked = vm_lock(current->vm);
Karl Meakin117c8082024-12-04 16:03:28 +0000376 ffa_vm_free_resources(vm_locked);
Madhukar Pappireddy9e09d7a2023-08-08 14:53:49 -0500377 vm_unlock(&vm_locked);
Andrew Scull9726c252019-01-23 13:44:19 +0000378
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600379 current_locked = vcpu_lock(current);
380 next = api_switch_to_primary(current_locked, ret, VCPU_STATE_ABORTED);
381 vcpu_unlock(&current_locked);
382
383 return next;
Andrew Scull9726c252019-01-23 13:44:19 +0000384}
385
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000386/*
387 * Format the partition info descriptors according to the version supported
388 * by the endpoint and return the size of the array created.
389 */
390static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000391 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000392 uint32_t vm_count)
393{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000394 struct vm *vm = vm_locked.vm;
Karl Meakin0e617d92024-04-05 12:55:22 +0100395 enum ffa_version version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100396 uint32_t partition_info_size;
397 uint32_t buffer_size;
Federico Recanati644f0462022-03-17 12:04:00 +0100398 struct ffa_value ret;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000399
J-Alvese8c8c2b2022-12-16 15:34:48 +0000400 /* Acquire receiver's RX buffer. */
Karl Meakin117c8082024-12-04 16:03:28 +0000401 if (!ffa_setup_acquire_receiver_rx(vm_locked, &ret)) {
J-Alvese8c8c2b2022-12-16 15:34:48 +0000402 dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id);
403 return ret;
404 }
405
J-Alves122f1a12022-12-12 15:55:42 +0000406 if (vm_is_mailbox_busy(vm_locked)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000407 /*
408 * Can't retrieve memory information if the mailbox is not
409 * available.
410 */
411 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000412 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000413 }
414
Karl Meakin0e617d92024-04-05 12:55:22 +0100415 if (version == FFA_VERSION_1_0) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000416 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
417
Daniel Boulby835e1592022-05-30 17:38:51 +0100418 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
419 buffer_size = partition_info_size * vm_count;
420 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000421 dlog_error(
422 "Partition information does not fit in the "
J-Alves0a21db12024-03-28 12:43:30 +0000423 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000424 return ffa_error(FFA_NO_MEMORY);
425 }
426
427 for (uint32_t i = 0; i < vm_count; i++) {
428 /*
429 * Populate the VM's RX buffer with the partition
Kathleen Capella402fa852022-11-09 16:16:51 -0500430 * information. Clear properties bits that must be zero
431 * according to DEN0077A FF-A v1.0 REL Table 8.25.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000432 */
433 recv_mailbox[i].vm_id = partitions[i].vm_id;
434 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
Kathleen Capella402fa852022-11-09 16:16:51 -0500435 recv_mailbox[i].properties =
436 partitions[i].properties &
437 ~FFA_PARTITION_v1_0_RES_MASK;
Daniel Boulby191b5f02022-02-17 17:26:14 +0000438 }
439
440 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100441 partition_info_size = sizeof(struct ffa_partition_info);
442 buffer_size = partition_info_size * vm_count;
J-Alves0a21db12024-03-28 12:43:30 +0000443
Daniel Boulby835e1592022-05-30 17:38:51 +0100444 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000445 dlog_error(
446 "Partition information does not fit in the "
J-Alves0a824e92024-04-26 16:20:12 +0100447 "VM's RX buffer.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000448 return ffa_error(FFA_NO_MEMORY);
449 }
450
J-Alves0a21db12024-03-28 12:43:30 +0000451 /*
452 * Populate the VM's RX buffer with the partition information.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000453 */
J-Alves0a21db12024-03-28 12:43:30 +0000454 if (!memcpy_trapped(vm->mailbox.recv, HF_MAILBOX_SIZE,
455 partitions, buffer_size)) {
456 dlog_error(
457 "%s: Failed to copy ffa_partition_info "
458 "descriptor\n",
459 __func__);
460 return ffa_error(FFA_ABORTED);
461 }
Daniel Boulby191b5f02022-02-17 17:26:14 +0000462 }
463
Daniel Boulby835e1592022-05-30 17:38:51 +0100464 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000465
466 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000467 vm->mailbox.recv_sender = HF_VM_ID_BASE;
468 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000469 vm->mailbox.state = MAILBOX_STATE_FULL;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000470
Daniel Boulby835e1592022-05-30 17:38:51 +0100471 /*
472 * Return the count of partition information descriptors in w2
473 * and the size of the descriptors in w3.
474 */
475 return (struct ffa_value){.func = FFA_SUCCESS_32,
476 .arg2 = vm_count,
477 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000478}
479
J-Alves7fb0fdb2024-10-09 11:28:07 +0100480/**
481 * Set properties accoridng to the version of the partition, and the FF-A
482 * features it supports.
483 */
484static ffa_partition_properties_t api_ffa_partitions_info_get_properties(
485 ffa_id_t caller_id, struct vm *vm)
486{
487 ffa_partition_properties_t properties;
488
Karl Meakin117c8082024-12-04 16:03:28 +0000489 properties = ffa_setup_partition_properties(caller_id, vm);
J-Alves7fb0fdb2024-10-09 11:28:07 +0100490 properties |= FFA_PARTITION_AARCH64_EXEC;
491
492 if (vm->ffa_version >= FFA_VERSION_1_1) {
493 properties |= vm_are_notifications_enabled(vm)
494 ? FFA_PARTITION_NOTIFICATION
495 : 0;
496 properties |= vm->messaging_method & FFA_PARTITION_INDIRECT_MSG;
497 }
498
Karl Meakina603e082024-08-02 17:57:27 +0100499 /*
500 * Only populate on calls from normal world,
501 * and if SP supports receiving direct message requests.
502 */
503 if (ffa_is_vm_id(caller_id) &&
504 (properties & FFA_PARTITION_DIRECT_REQ_RECV) != 0) {
505 if (vm->vm_availability_messages.vm_created) {
506 properties |= FFA_PARTITION_VM_CREATED;
507 }
508 if (vm->vm_availability_messages.vm_destroyed) {
509 properties |= FFA_PARTITION_VM_DESTROYED;
510 }
511 }
J-Alves7fb0fdb2024-10-09 11:28:07 +0100512 return properties;
513}
514
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100515static void api_ffa_fill_partition_info(
J-Alves7fb0fdb2024-10-09 11:28:07 +0100516 struct ffa_partition_info *out_partition, struct vm *vm,
517 ffa_id_t caller_id)
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800518{
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100519 out_partition->vm_id = vm->id;
520 out_partition->vcpu_count = vm->vcpu_count;
J-Alves7fb0fdb2024-10-09 11:28:07 +0100521 out_partition->properties =
522 api_ffa_partitions_info_get_properties(caller_id, vm);
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100523}
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800524
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100525/**
526 * Find VMs with UUID matching `uuid_to_find` , and fill `out_partitions` with
527 * partition infos. Returns number of VMs that matched.
528 * A null UUID matches against any VM.
529 * If `count_flag` is true, no partition infos are written to `out_partitions`,
530 * only the number of VMs that matched is returned.
531 */
532static ffa_vm_count_t api_ffa_fill_partitions_info_array(
533 struct ffa_partition_info out_partitions[], size_t out_partitions_len,
J-Alves7fb0fdb2024-10-09 11:28:07 +0100534 const struct ffa_uuid *uuid_to_find, bool count_flag,
Daniel Boulby48546b12025-01-08 18:22:13 +0000535 ffa_id_t caller_id, enum ffa_version caller_version)
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100536{
537 ffa_vm_count_t vms_found = 0;
538 bool match_any = ffa_uuid_is_null(uuid_to_find);
539
540 assert(vm_get_count() <= out_partitions_len);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800541
542 /*
543 * Iterate through the VMs to find the ones with a matching
544 * UUID. A Null UUID retrieves information for all VMs.
545 */
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100546 for (ffa_vm_count_t vm_idx = 0; vm_idx < vm_get_count(); vm_idx++) {
547 struct vm *vm = vm_find_index(vm_idx);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800548
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100549 for (size_t uuid_idx = 0; uuid_idx < PARTITION_MAX_UUIDS;
550 uuid_idx++) {
551 struct ffa_uuid uuid = vm->uuids[uuid_idx];
552 struct ffa_partition_info *out_partition =
553 &out_partitions[vms_found];
554
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400555 /*
556 * Null UUID indicates reaching the end of a
557 * partition's array of UUIDs.
558 */
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100559 if (ffa_uuid_is_null(&uuid)) {
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400560 break;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800561 }
562
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100563 if (match_any || ffa_uuid_equal(uuid_to_find, &uuid)) {
564 vms_found++;
565
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400566 if (count_flag) {
567 continue;
568 }
569
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100570 api_ffa_fill_partition_info(out_partition, vm,
J-Alves7fb0fdb2024-10-09 11:28:07 +0100571 caller_id);
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100572 if (match_any) {
573 out_partition->uuid = uuid;
Kathleen Capellaa1de3c52023-08-28 20:10:02 -0400574 }
Daniel Boulby48546b12025-01-08 18:22:13 +0000575
576 /*
577 * Multiple UUIDs for a partition was only
578 * introduced in FF-A v1.2, so for any version
579 * less than v1.2 return only one UUID per
580 * partition.
581 */
582 if (caller_version < FFA_VERSION_1_2) {
583 break;
584 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800585 }
586 }
587 }
588
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100589 return vms_found;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800590}
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800591
592static inline void api_ffa_pack_vmid_count_props(
J-Alves19e20cf2023-08-02 12:48:55 +0100593 uint64_t *xn, ffa_id_t vm_id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800594 ffa_partition_properties_t properties)
595{
596 *xn = (uint64_t)vm_id;
597 *xn |= (uint64_t)vcpu_count << 16;
598 *xn |= (uint64_t)properties << 32;
599}
600
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700601/**
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700602 * This function forwards the FFA_PARTITION_INFO_GET_REGS ABI to the other world
603 * when hafnium is the hypervisor to determine the secure partitions. When
604 * hafnium is the SPMC, this function forwards the call to the SPMD to discover
605 * SPMD logical partitions. The function returns true when partition information
606 * is filled in the partitions array and false if there are errors. Note that
607 * the SPMD and SPMC may return an FF-A error code of FFA_NOT_SUPPORTED when
608 * there are no SPMD logical partitions or no secure partitions respectively,
609 * and this is not considered a failure of the forwarded call. A caller is
610 * expected to check the return value before consuming the information in the
611 * partitions array passed in and ret_count.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700612 */
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700613static bool api_ffa_partition_info_get_regs_forward(
614 const struct ffa_uuid *uuid, const uint16_t tag,
615 struct ffa_partition_info *partitions, uint16_t partitions_len,
616 ffa_vm_count_t *ret_count)
617{
618 (void)tag;
619 struct ffa_value ret;
620 uint16_t last_index = UINT16_MAX;
621 uint16_t curr_index = 0;
622 uint16_t start_index = 0;
623
Karl Meakin117c8082024-12-04 16:03:28 +0000624 if (!ffa_setup_partition_info_get_regs_forward_allowed()) {
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700625 return true;
626 }
627
628 while (start_index <= last_index) {
629 ret = ffa_partition_info_get_regs(uuid, start_index, 0);
630 if (ffa_func_id(ret) != FFA_SUCCESS_64) {
631 /*
632 * If there are no logical partitions, SPMD returns
633 * NOT_SUPPORTED, that is not an error. If there are no
634 * secure partitions the SPMC returns NOT_SUPPORTED.
635 */
636 if ((ffa_func_id(ret) == FFA_ERROR_32) &&
637 (ffa_error_code(ret) == FFA_NOT_SUPPORTED)) {
638 return true;
639 }
640
641 return false;
642 }
643
644 if (!api_ffa_fill_partition_info_from_regs(
645 ret, start_index, partitions, partitions_len,
646 ret_count)) {
647 return false;
648 }
649
650 last_index = ffa_partition_info_regs_get_last_idx(ret);
651 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
652 start_index = curr_index + 1;
653 }
654 return true;
655}
656
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700657bool api_ffa_fill_partition_info_from_regs(
658 struct ffa_value ret, uint16_t start_index,
659 struct ffa_partition_info *partitions, uint16_t partitions_len,
660 ffa_vm_count_t *ret_count)
661{
662 uint16_t vm_count = *ret_count;
663 uint16_t curr_index = 0;
664 uint8_t num_entries = 0;
665 uint8_t idx = 0;
666 /* List of pointers to args in return value. */
667 uint64_t *arg_ptrs[] = {
668 &ret.arg3,
669 &ret.arg4,
670 &ret.arg5,
671 &ret.arg6,
672 &ret.arg7,
673 &ret.extended_val.arg8,
674 &ret.extended_val.arg9,
675 &ret.extended_val.arg10,
676 &ret.extended_val.arg11,
677 &ret.extended_val.arg12,
678 &ret.extended_val.arg13,
679 &ret.extended_val.arg14,
680 &ret.extended_val.arg15,
681 &ret.extended_val.arg16,
682 &ret.extended_val.arg17,
683 };
684
685 if (vm_count > partitions_len) {
686 return false;
687 }
688
689 /*
690 * Tags are currently unused in the implementation. Expect it to be
691 * zero since the implementation does not provide a tag when calling
692 * the FFA_PARTITION_INFO_GET_REGS ABI.
693 */
694 assert(ffa_partition_info_regs_get_tag(ret) == 0);
695
696 /*
697 * Hafnium expects the size of the returned descriptor to be equal to
698 * the size of the structure in the FF-A 1.1 specification. When future
699 * enhancements are made, this assert can be relaxed.
700 */
701 assert(ffa_partition_info_regs_get_desc_size(ret) ==
702 sizeof(struct ffa_partition_info));
703
704 curr_index = ffa_partition_info_regs_get_curr_idx(ret);
705
706 /* FF-A 1.2 ALP0, section 14.9.2 Usage rule 7. */
707 assert(start_index <= curr_index);
708
709 num_entries = curr_index - start_index + 1;
710 if (num_entries > (partitions_len - vm_count) ||
711 num_entries > MAX_INFO_REGS_ENTRIES_PER_CALL) {
712 return false;
713 }
714
715 while (num_entries) {
716 uint64_t info = *(arg_ptrs[(ptrdiff_t)(idx++)]);
717 uint64_t uuid_lo = *(arg_ptrs[(ptrdiff_t)(idx++)]);
718 uint64_t uuid_high = *(arg_ptrs[(ptrdiff_t)(idx++)]);
719
720 partitions[vm_count].vm_id = info & 0xFFFF;
721 partitions[vm_count].vcpu_count = (info >> 16) & 0xFFFF;
722 partitions[vm_count].properties = (info >> 32);
723 partitions[vm_count].uuid.uuid[0] = uuid_lo & 0xFFFFFFFF;
724 partitions[vm_count].uuid.uuid[1] =
725 (uuid_lo >> 32) & 0xFFFFFFFF;
726 partitions[vm_count].uuid.uuid[2] = uuid_high & 0xFFFFFFFF;
727 partitions[vm_count].uuid.uuid[3] =
728 (uuid_high >> 32) & 0xFFFFFFFF;
729 vm_count++;
730 num_entries--;
731 }
732
733 *ret_count = vm_count;
734 return true;
735}
736
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800737struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current,
738 const struct ffa_uuid *uuid,
739 const uint16_t start_index,
740 const uint16_t tag)
741{
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800742 struct vm *current_vm = current->vm;
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800743 static struct ffa_partition_info partitions[2 * MAX_VMS];
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800744 bool uuid_is_null = ffa_uuid_is_null(uuid);
745 ffa_vm_count_t vm_count = 0;
746 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
747 uint16_t max_idx = 0;
748 uint16_t curr_idx = 0;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800749 uint8_t num_entries_to_ret = 0;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800750 uint8_t arg_idx = 3;
751
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800752 /* list of pointers to args in return value */
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800753 uint64_t *arg_ptrs[] = {
754 &(ret).func,
755 &(ret).arg1,
756 &(ret).arg2,
757 &(ret).arg3,
758 &(ret).arg4,
759 &(ret).arg5,
760 &(ret).arg6,
761 &(ret).arg7,
762 &(ret).extended_val.arg8,
763 &(ret).extended_val.arg9,
764 &(ret).extended_val.arg10,
765 &(ret).extended_val.arg11,
766 &(ret).extended_val.arg12,
767 &(ret).extended_val.arg13,
768 &(ret).extended_val.arg14,
769 &(ret).extended_val.arg15,
770 &(ret).extended_val.arg16,
771 &(ret).extended_val.arg17,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800772 };
773
774 /* TODO: Add support for using tags */
775 if (tag != 0) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800776 dlog_error("Tag not 0. Unsupported tag. %d\n", tag);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800777 return ffa_error(FFA_RETRY);
778 }
779
780 memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions));
781
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100782 vm_count = api_ffa_fill_partitions_info_array(
Daniel Boulby48546b12025-01-08 18:22:13 +0000783 partitions, ARRAY_SIZE(partitions), uuid, false, current_vm->id,
784 current_vm->ffa_version);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800785
786 /* If UUID is Null vm_count must not be zero at this stage. */
787 CHECK(!uuid_is_null || vm_count != 0);
788
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800789 /*
790 * When running the Hypervisor:
791 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
792 * it to fill with secure partitions information.
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700793 * When running the SPMC, the SPMC forwards the call to the SPMD to
794 * discover any EL3 SPMD logical partitions, if the call came from an
795 * SP. Otherwise, the call is not forwarded.
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800796 * TODO: Note that for this ABI, forwarding on every invocation when
797 * uuid is Null is inefficient,and if performance becomes a problem,
798 * this would be a good place to optimize using strategies such as
799 * caching info etc. For now, assuming this inefficiency is not a major
800 * issue.
801 * - If UUID is non-Null vm_count may be zero because the UUID matches
802 * a secure partition and the query is forwarded to the SPMC.
803 * When running the SPMC:
804 * - If UUID is non-Null and vm_count is zero it means there is no such
805 * partition identified in the system.
806 */
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700807 if (vm_id_is_current_world(current_vm->id)) {
Raghu Krishnamurthye74d6532023-06-07 12:21:54 -0700808 if (!api_ffa_partition_info_get_regs_forward(
809 uuid, tag, partitions, ARRAY_SIZE(partitions),
810 &vm_count)) {
Raghu Krishnamurthy2d2b0f42023-04-23 09:54:45 -0700811 dlog_error(
812 "Failed to forward "
813 "ffa_partition_info_get_regs.\n");
814 return ffa_error(FFA_DENIED);
815 }
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800816 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800817
818 /*
819 * Unrecognized UUID: does not match any of the VMs (or SPs)
820 * and is not Null.
821 */
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800822 if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) {
Olivier Deprez89da0a42023-09-01 17:38:33 +0200823 dlog_verbose(
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800824 "Invalid parameters. vm_count = %d (must not be zero "
Karl Meakine8937d92024-03-19 16:04:25 +0000825 "or > %lu)\n",
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800826 vm_count, ARRAY_SIZE(partitions));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800827 return ffa_error(FFA_INVALID_PARAMETERS);
828 }
829
830 if (start_index >= vm_count) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800831 dlog_error(
832 "start index = %d vm_count = %d (start_index must be "
833 "less than vm_count)\n",
834 start_index, vm_count);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800835 return ffa_error(FFA_INVALID_PARAMETERS);
836 }
837
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800838 max_idx = vm_count - 1;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800839 num_entries_to_ret = (max_idx - start_index) + 1;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800840 num_entries_to_ret =
841 MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800842 curr_idx = start_index + num_entries_to_ret - 1;
843 assert(curr_idx <= max_idx);
844
845 ret.func = FFA_SUCCESS_64;
846 ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48;
847 ret.arg2 |= curr_idx << 16;
848 ret.arg2 |= max_idx;
849
850 if (num_entries_to_ret > 1) {
851 ret.extended_val.valid = 1;
852 }
853
854 for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800855 uint64_t *xn_0 = arg_ptrs[arg_idx++];
856 uint64_t *xn_1 = arg_ptrs[arg_idx++];
857 uint64_t *xn_2 = arg_ptrs[arg_idx++];
858
859 api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id,
860 partitions[idx].vcpu_count,
861 partitions[idx].properties);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800862 if (uuid_is_null) {
Karl Meakin9478e322024-09-23 17:47:09 +0100863 ffa_uuid_to_u64x2(xn_1, xn_2, &partitions[idx].uuid);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800864 }
Raghu Krishnamurthycded31b2023-04-23 15:07:35 -0700865 assert(arg_idx <= ARRAY_SIZE(arg_ptrs));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800866 }
867
868 return ret;
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800869}
870
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100871struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000872 const struct ffa_uuid *uuid,
873 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100874{
875 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100876 ffa_vm_count_t vm_count = 0;
Olivier Deprez013f4d62022-11-21 15:46:20 +0100877 bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) ==
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000878 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100879 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100880 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000881 struct vm_locked vm_locked;
882 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100883
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000884 /* Bits 31:1 Must Be Zero */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100885 if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) {
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000886 return ffa_error(FFA_INVALID_PARAMETERS);
887 }
888
Karl Meakinfb9c2a22024-08-19 14:29:37 +0100889 vm_count = api_ffa_fill_partitions_info_array(
890 partitions, ARRAY_SIZE(partitions), uuid, count_flag,
Daniel Boulby48546b12025-01-08 18:22:13 +0000891 current_vm->id, current_vm->ffa_version);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100892
Olivier Depreze562e542020-06-11 17:31:54 +0200893 /* If UUID is Null vm_count must not be zero at this stage. */
894 CHECK(!uuid_is_null || vm_count != 0);
895
896 /*
897 * When running the Hypervisor:
898 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
899 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000900 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200901 * a secure partition and the query is forwarded to the SPMC.
902 * When running the SPMC:
903 * - If UUID is non-Null and vm_count is zero it means there is no such
904 * partition identified in the system.
905 */
Karl Meakin117c8082024-12-04 16:03:28 +0000906 vm_count = ffa_setup_partition_info_get_forward(uuid, flags, partitions,
907 vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200908
909 /*
910 * Unrecognized UUID: does not match any of the VMs (or SPs)
911 * and is not Null.
912 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100913 if (vm_count == 0) {
914 return ffa_error(FFA_INVALID_PARAMETERS);
915 }
916
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000917 /*
918 * If the count flag is set we don't need to return the partition info
919 * descriptors.
920 */
921 if (count_flag) {
922 return (struct ffa_value){.func = FFA_SUCCESS_32,
923 .arg2 = vm_count};
924 }
925
Daniel Boulby191b5f02022-02-17 17:26:14 +0000926 vm_locked = vm_lock(current_vm);
927 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
928 vm_count);
929 vm_unlock(&vm_locked);
930 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100931}
932
Andrew Scull9726c252019-01-23 13:44:19 +0000933/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000934 * Returns the ID of the VM.
935 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100936struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000937{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100938 return (struct ffa_value){.func = FFA_SUCCESS_32,
939 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000940}
941
942/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200943 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
944 * FF-A instances.
945 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000946 */
947struct ffa_value api_ffa_spm_id_get(void)
948{
Karl Meakin0e617d92024-04-05 12:55:22 +0100949 if (FFA_VERSION_1_1 <= FFA_VERSION_COMPILED) {
950 /*
951 * Return the SPMC ID that was fetched during FF-A
952 * initialization.
953 */
954 return (struct ffa_value){.func = FFA_SUCCESS_32,
955 .arg2 = arch_ffa_spmc_id_get()};
956 }
957
J-Alves3829fc02021-03-18 12:49:18 +0000958 return ffa_error(FFA_NOT_SUPPORTED);
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000959}
960
961/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000962 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000963 * function to indicate that register state for the given vCPU has been saved
964 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000965 */
966void api_regs_state_saved(struct vcpu *vcpu)
967{
968 sl_lock(&vcpu->lock);
969 vcpu->regs_available = true;
970 sl_unlock(&vcpu->lock);
971}
J-Alves0ffce752024-10-09 14:37:10 +0100972
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000973/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000974 * Assuming that the arguments have already been checked by the caller, injects
975 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
976 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
977 * is next run, which is up to the scheduler.
978 *
979 * Returns:
980 * - 0 on success if no further action is needed.
981 * - 1 if it was called by the primary VM and the primary VM now needs to wake
982 * up or kick the target vCPU.
983 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100984int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600985 uint32_t intid,
986 struct vcpu_locked current_locked,
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100987 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000988{
Manish Pandey35e452f2021-02-18 21:36:34 +0000989 struct vcpu *target_vcpu = target_locked.vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600990 struct vcpu *current = current_locked.vcpu;
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100991 struct interrupts *interrupts = &target_vcpu->interrupts;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000992 int64_t ret = 0;
993
Andrew Walbran508e63c2018-12-20 17:02:37 +0000994 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000995 * We only need to change state and (maybe) trigger a virtual interrupt
996 * if it is enabled and was not previously pending. Otherwise we can
997 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000998 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100999 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
1000 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
Andrew Walbran508e63c2018-12-20 17:02:37 +00001001 goto out;
1002 }
1003
1004 /* Increment the count. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01001005 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001006
1007 /*
1008 * Only need to update state if there was not already an
1009 * interrupt enabled and pending.
1010 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001011 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +00001012 goto out;
1013 }
1014
Karl Meakin5e996992024-05-20 11:27:07 +01001015 if (vm_is_primary(current->vm)) {
Andrew Walbran508e63c2018-12-20 17:02:37 +00001016 /*
1017 * If the call came from the primary VM, let it know that it
1018 * should run or kick the target vCPU.
1019 */
1020 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +00001021 } else if (current != target_vcpu && next != NULL) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001022 *next = api_wake_up_locked(current_locked, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001023 }
1024
1025out:
1026 /* Either way, make it pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01001027 vcpu_virt_interrupt_set_pending(interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001028
Olivier Deprezc19a6ea2020-08-06 11:16:07 +02001029 return ret;
1030}
1031
Andrew Walbran508e63c2018-12-20 17:02:37 +00001032/**
J-Alves2ced1672022-12-12 14:35:38 +00001033 * Constructs the return value from a successful FFA_MSG_WAIT call, when used
1034 * with FFA_MSG_SEND_32.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001035 */
J-Alves27b71962022-12-12 15:29:58 +00001036struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001037{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001038 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001039 case FFA_MSG_SEND_32:
1040 return (struct ffa_value){
1041 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001042 .arg1 = (receiver->mailbox.recv_sender << 16) |
1043 receiver->id,
1044 .arg3 = receiver->mailbox.recv_size};
J-Alvesd9e7c8f2024-09-11 13:37:25 +01001045 default:
Federico Recanati25053ee2022-03-14 15:01:53 +01001046 return (struct ffa_value){
1047 .func = FFA_RUN_32,
1048 /*
1049 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
1050 * Retrieving vCPU requires a rework of the function,
1051 * while receiver ID must be set because it's checked by
1052 * other APIs (eg: FFA_NOTIFICATION_GET).
1053 */
1054 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001055 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001056}
1057
J-Alvese8c8c2b2022-12-16 15:34:48 +00001058/**
1059 * Change the state of mailbox to empty, such that the ownership is given to the
1060 * Partition manager.
Karl Meakin1064a9c2024-06-04 17:20:43 +01001061 * Returns FFA_SUCCESS if the mailbox was reset successfully, FFA_ERROR
1062 * otherwise.
J-Alvese8c8c2b2022-12-16 15:34:48 +00001063 */
Karl Meakin1064a9c2024-06-04 17:20:43 +01001064static struct ffa_value api_release_mailbox(struct vm_locked vm_locked)
J-Alvese8c8c2b2022-12-16 15:34:48 +00001065{
Karl Meakin1064a9c2024-06-04 17:20:43 +01001066 struct ffa_value ret = {.func = FFA_SUCCESS_32};
J-Alves19e20cf2023-08-02 12:48:55 +01001067 ffa_id_t vm_id = vm_locked.vm->id;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001068
1069 switch (vm_locked.vm->mailbox.state) {
1070 case MAILBOX_STATE_EMPTY:
1071 dlog_verbose("Mailbox of %x is empty.\n", vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001072 ret = ffa_error(FFA_DENIED);
J-Alves31f8bef2023-07-14 12:46:28 +01001073 break;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001074 case MAILBOX_STATE_FULL:
1075 /* Check it doesn't have pending RX full notifications. */
1076 if (vm_are_fwk_notifications_pending(vm_locked)) {
1077 dlog_verbose(
1078 "Mailbox of endpoint %x has pending "
1079 "messages.\n",
1080 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001081 ret = ffa_error(FFA_DENIED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001082 }
1083 break;
1084 case MAILBOX_STATE_OTHER_WORLD_OWNED:
1085 /*
1086 * The SPMC shouldn't let SP's mailbox get into this state.
1087 * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst
1088 * the mailbox is in this state. In that case, we should report
1089 * error.
1090 */
1091 if (vm_id_is_current_world(vm_id)) {
1092 dlog_verbose(
Karl Meakin1064a9c2024-06-04 17:20:43 +01001093 "Mailbox of endpoint %x is in an incorrect "
1094 "state.\n",
J-Alvese8c8c2b2022-12-16 15:34:48 +00001095 vm_id);
Karl Meakin1064a9c2024-06-04 17:20:43 +01001096 ret = ffa_error(FFA_ABORTED);
J-Alvese8c8c2b2022-12-16 15:34:48 +00001097 }
1098 break;
1099 }
1100
Karl Meakin1064a9c2024-06-04 17:20:43 +01001101 if (ret.func == FFA_SUCCESS_32) {
1102 vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001103 }
1104
Karl Meakin1064a9c2024-06-04 17:20:43 +01001105 return ret;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001106}
1107
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001108/*
1109 * Helper to check if extended arguments (corresponding to regs x8-x17)
1110 * are zeroed out.
1111 */
Kathleen Capella036cc592023-11-30 18:26:15 -05001112bool api_extended_args_are_zero(struct ffa_value *args)
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001113{
1114 if (args->extended_val.arg8 != 0U || args->extended_val.arg9 != 0U ||
1115 args->extended_val.arg10 != 0U || args->extended_val.arg11 != 0U ||
1116 args->extended_val.arg12 != 0U || args->extended_val.arg13 != 0U ||
1117 args->extended_val.arg14 != 0U || args->extended_val.arg15 != 0U ||
1118 args->extended_val.arg16 != 0U || args->extended_val.arg17 != 0U) {
1119 return false;
1120 }
1121 return true;
1122}
1123
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001124static void api_ffa_msg_wait_rx_release(struct vcpu *current)
1125{
1126 struct vm_locked vm_locked;
1127
Karl Meakin117c8082024-12-04 16:03:28 +00001128 vm_locked = ffa_vm_find_locked(current->vm->id);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001129 if (vm_locked.vm == NULL) {
1130 return;
1131 }
1132
1133 api_release_mailbox(vm_locked);
1134
1135 if (vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY) {
1136 dlog_warning("Mailbox not released to producer\n");
1137 }
1138
1139 vm_unlock(&vm_locked);
1140}
1141
Kathleen Capella7253bd52024-08-14 17:36:09 -04001142static bool api_retain_rx_buffer_ownership(struct ffa_value args)
1143{
1144 return ((args.arg2 & FFA_MSG_WAIT_FLAG_RETAIN_RX) != 0U);
1145}
1146
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001147struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
1148 struct ffa_value *args)
1149{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001150 struct vcpu_locked current_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001151 enum vcpu_state next_state = VCPU_STATE_RUNNING;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001152 struct ffa_value ret;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001153 struct vcpu_locked next_locked = (struct vcpu_locked){
1154 .vcpu = NULL,
1155 };
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001156
Kathleen Capella7253bd52024-08-14 17:36:09 -04001157 if (args->arg1 != 0U || args->arg3 != 0U || args->arg4 != 0U ||
1158 args->arg5 != 0U || args->arg6 != 0U || args->arg7 != 0U) {
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001159 return ffa_error(FFA_INVALID_PARAMETERS);
1160 }
1161
Kathleen Capella7253bd52024-08-14 17:36:09 -04001162 if (current->vm->ffa_version >= FFA_VERSION_1_2) {
1163 if (!api_extended_args_are_zero(args)) {
1164 return ffa_error(FFA_INVALID_PARAMETERS);
1165 }
1166 } else {
1167 if (args->arg2 != 0U) {
1168 return ffa_error(FFA_INVALID_PARAMETERS);
1169 }
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04001170 }
1171
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001172 current_locked = vcpu_lock(current);
Karl Meakin117c8082024-12-04 16:03:28 +00001173 if (!ffa_cpu_cycles_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001174 current_locked, current->vm->id, HF_INVALID_VM_ID,
1175 next_locked, FFA_MSG_WAIT_32, &next_state)) {
1176 ret = ffa_error(FFA_DENIED);
1177 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001178 }
1179
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001180 assert(!vm_id_is_current_world(current->vm->id) ||
1181 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001182
Karl Meakin117c8082024-12-04 16:03:28 +00001183 ret = ffa_cpu_cycles_msg_wait_prepare(current_locked, next);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001184
1185 /*
1186 * To maintain partial ordering of locks, release vCPU lock before
1187 * releasing the VM's RX buffer, a process which requires locking the
1188 * VM.
1189 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001190out:
1191 vcpu_unlock(&current_locked);
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001192
Kathleen Capella7253bd52024-08-14 17:36:09 -04001193 if (ret.func != FFA_ERROR_32 &&
1194 !api_retain_rx_buffer_ownership(*args)) {
Kathleen Capellabe1a0b72024-08-16 12:56:39 -04001195 api_ffa_msg_wait_rx_release(current);
1196 }
J-Alvese8c8c2b2022-12-16 15:34:48 +00001197 return ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -06001198}
1199
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001200/**
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05001201 * Inject virtual timer interrupt to next vCPU if its timer has expired.
1202 */
1203static void api_inject_arch_timer_interrupt(struct vcpu_locked current_locked,
1204 struct vcpu_locked next_locked)
1205{
1206 struct vcpu *next = next_locked.vcpu;
1207
1208 if (arch_timer_expired(&next->regs)) {
1209 /* Make virtual timer interrupt pending. */
1210 api_interrupt_inject_locked(next_locked, HF_VIRTUAL_TIMER_INTID,
1211 current_locked, NULL);
1212 vcpu_interrupt_queue_push(next_locked, HF_VIRTUAL_TIMER_INTID);
1213 }
1214}
1215
1216/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001217 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001218 * value needs to be forced onto the vCPU.
1219 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001220static bool api_vcpu_prepare_run(struct vcpu_locked current_locked,
1221 struct vcpu_locked vcpu_next_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001222 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001223{
Max Shvetsov40108e72020-08-27 12:39:50 +01001224 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001225 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001226 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001227 bool vcpu_was_init_state = false;
J-Alves6e2abc62021-12-02 14:58:56 +00001228 bool need_vm_lock;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001229 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001230
J-Alves478faac2024-10-23 10:35:57 +01001231 const struct ffa_value ffa_run_abi =
1232 (struct ffa_value){.func = FFA_RUN_32};
1233 const struct ffa_value *ffa_run_ret = NULL;
1234
Andrew Scullb06d1752019-02-04 10:15:48 +00001235 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001236 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +00001237 *
Andrew Scull4caadaf2019-07-03 13:13:47 +01001238 * The VM lock is not needed in the common case so it must only be taken
1239 * when it is going to be needed. This ensures there are no inter-vCPU
1240 * dependencies in the common run case meaning the sensitive context
1241 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +00001242 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001243 struct vcpu *vcpu = vcpu_next_locked.vcpu;
1244 struct vcpu *current = current_locked.vcpu;
Max Shvetsov40108e72020-08-27 12:39:50 +01001245
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001246 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +00001247 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
1248 (!vcpu->vm->el0_partition &&
1249 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
1250 vcpu->state == VCPU_STATE_BLOCKED ||
1251 vcpu->state == VCPU_STATE_PREEMPTED));
1252
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001253 if (need_vm_lock) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001254 vcpu_unlock(&vcpu_next_locked);
1255 vcpu_unlock(&current_locked);
Max Shvetsov40108e72020-08-27 12:39:50 +01001256 vm_locked = vm_lock(vcpu->vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001257
1258 /* Lock both vCPUs at once to avoid deadlock. */
1259 vcpus_locked = vcpu_lock_both(current, vcpu);
1260 current_locked = vcpus_locked.vcpu1;
1261 vcpu_next_locked = vcpus_locked.vcpu2;
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001262 }
1263
1264 /*
1265 * If the vCPU is already running somewhere then we can't run it here
1266 * simultaneously. While it is actually running then the state should be
1267 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
1268 * stops running but while Hafnium is in the process of switching back
1269 * to the primary there will be a brief period while the state has been
1270 * updated but `regs_available` is still false (until
1271 * `api_regs_state_saved` is called). We can't start running it again
1272 * until this has finished, so count this state as still running for the
1273 * purposes of this check.
1274 */
1275 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
1276 /*
1277 * vCPU is running on another pCPU.
1278 *
1279 * It's okay not to return the sleep duration here because the
1280 * other physical CPU that is currently running this vCPU will
1281 * return the sleep duration if needed.
1282 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001283 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001284 ret = false;
1285 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001286 }
Andrew Scull9726c252019-01-23 13:44:19 +00001287
1288 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001289 if (vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04001290 dlog_verbose("VM %#x was aborted, cannot run vCPU %u\n",
1291 vcpu->vm->id, vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +01001292 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +00001293 }
Kathleen Capella6ab05132023-05-10 12:27:35 -04001294 *run_ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +00001295 ret = false;
1296 goto out;
1297 }
1298
Andrew Walbran508e63c2018-12-20 17:02:37 +00001299 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001300 case VCPU_STATE_RUNNING:
1301 case VCPU_STATE_OFF:
1302 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001303 ret = false;
1304 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001305
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001306 case VCPU_STATE_WAITING:
1307 /*
Olivier Deprezb2808332023-02-02 15:25:40 +01001308 * An initial FFA_RUN is necessary for SP's secondary vCPUs to
1309 * reach the message wait loop.
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001310 */
Olivier Deprezb2808332023-02-02 15:25:40 +01001311 if (vcpu->rt_model == RTM_SP_INIT) {
1312 /*
1313 * TODO: this should be removed, but omitting it makes
1314 * normal world arch gicv3 tests failing.
1315 */
1316 vcpu->rt_model = RTM_NONE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001317
1318 vcpu_was_init_state = true;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001319 break;
1320 }
1321
J-Alves6e2abc62021-12-02 14:58:56 +00001322 assert(need_vm_lock == true);
J-Alves478faac2024-10-23 10:35:57 +01001323 if (!vm_locked.vm->el0_partition) {
Karl Meakin117c8082024-12-04 16:03:28 +00001324 ffa_interrupts_inject_notification_pending_interrupt(
J-Alves478faac2024-10-23 10:35:57 +01001325 vcpu_next_locked, current_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001326 }
1327
J-Alves478faac2024-10-23 10:35:57 +01001328 /* Provide reference to the return value. */
1329 ffa_run_ret = &ffa_run_abi;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001330
J-Alves478faac2024-10-23 10:35:57 +01001331 break;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001332 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +00001333 if (need_vm_lock &&
Karl Meakin117c8082024-12-04 16:03:28 +00001334 ffa_interrupts_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001335 vcpu_next_locked, current_locked, vm_locked)) {
1336 assert(vcpu_interrupt_count_get(vcpu_next_locked) > 0);
J-Alves6e2abc62021-12-02 14:58:56 +00001337 break;
1338 }
1339
Andrew Scullb06d1752019-02-04 10:15:48 +00001340 /* Allow virtual interrupts to be delivered. */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001341 if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +00001342 break;
1343 }
1344
Andrew Walbran508e63c2018-12-20 17:02:37 +00001345 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001346 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001347 arch_timer_remaining_ns(&vcpu->regs);
1348
1349 /*
1350 * The timer expired so allow the interrupt to be
1351 * delivered.
1352 */
1353 if (timer_remaining_ns == 0) {
1354 break;
1355 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001356 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001357
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001358 /*
1359 * The vCPU is not ready to run, return the appropriate code to
1360 * the primary which called vcpu_run.
1361 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001362 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001363 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1364 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +00001365
1366 ret = false;
1367 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001368
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001369 case VCPU_STATE_BLOCKED:
1370 /* A blocked vCPU is run unconditionally. Fall through. */
1371 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +00001372 /* Check NPI is to be injected here. */
1373 if (need_vm_lock) {
Karl Meakin117c8082024-12-04 16:03:28 +00001374 ffa_interrupts_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001375 vcpu_next_locked, current_locked, vm_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00001376 }
Andrew Walbran508e63c2018-12-20 17:02:37 +00001377 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001378 default:
1379 /*
1380 * Execution not expected to reach here. Deny the request
1381 * gracefully.
1382 */
1383 *run_ret = ffa_error(FFA_DENIED);
1384 ret = false;
1385 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001386 }
1387
J-Alves5d65fe72025-02-18 16:02:32 +00001388 ffa_cpu_cycles_init_schedule_mode_ffa_run(current_locked,
1389 vcpu_next_locked);
Madhukar Pappireddy1480fce2022-06-21 18:09:25 -05001390
Madhukar Pappireddy106bfc32024-09-25 15:47:11 -05001391 timer_migrate_to_other_cpu(current_locked.vcpu->cpu, vcpu_next_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001392 vcpu->cpu = current_locked.vcpu->cpu;
J-Alves478faac2024-10-23 10:35:57 +01001393
1394 vcpu_set_running(vcpu_next_locked, ffa_run_ret);
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 ret = true;
1402
1403out:
Andrew Scullb06d1752019-02-04 10:15:48 +00001404 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +01001405 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001406 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001407 return ret;
1408}
1409
J-Alves19e20cf2023-08-02 12:48:55 +01001410struct ffa_value api_ffa_run(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001411 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001412{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001413 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001414 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001415 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001416 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001417 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001418 struct vcpu_locked vcpu_next_locked;
1419 struct two_vcpu_locked vcpus_locked;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001420
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001421 current_locked = vcpu_lock(current);
Karl Meakin117c8082024-12-04 16:03:28 +00001422 if (!ffa_cpu_cycles_run_checks(current_locked, vm_id, vcpu_idx, &ret,
1423 next)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001424 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001425 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001426
Karl Meakin117c8082024-12-04 16:03:28 +00001427 if (ffa_cpu_cycles_run_forward(vm_id, vcpu_idx, &ret)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001428 goto out;
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001429 }
1430
Andrew Scull19503262018-09-20 14:48:39 +01001431 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001432 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +01001433 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001434 goto out;
Andrew Scull19503262018-09-20 14:48:39 +01001435 }
1436
Fuad Tabbaed294af2019-12-20 10:43:01 +00001437 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001438 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001439 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001440 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001441
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001442 /*
1443 * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC
1444 * bypasses the intermediate execution contexts and resumes the
1445 * SP execution context that was originally preempted.
1446 */
1447 if (*next != NULL) {
1448 vcpu = *next;
1449 } else {
1450 vcpu = vm_get_vcpu(vm, vcpu_idx);
1451 }
1452
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001453 /*
1454 * Unlock current vCPU to allow it to be locked together with next
1455 * vcpu.
1456 */
1457 vcpu_unlock(&current_locked);
1458
1459 /* Lock both vCPUs at once to avoid deadlock. */
1460 vcpus_locked = vcpu_lock_both(current, vcpu);
1461 current_locked = vcpus_locked.vcpu1;
1462 vcpu_next_locked = vcpus_locked.vcpu2;
1463
Karl Meakin117c8082024-12-04 16:03:28 +00001464 if (!ffa_cpu_cycles_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001465 current_locked, current->vm->id, HF_INVALID_VM_ID,
1466 vcpu_next_locked, FFA_RUN_32, &next_state)) {
1467 ret = ffa_error(FFA_DENIED);
1468 goto out_vcpu;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001469 }
1470
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001471 if (!api_vcpu_prepare_run(current_locked, vcpu_next_locked, &ret)) {
1472 goto out_vcpu;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001473 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001474
Fuad Tabbaed294af2019-12-20 10:43:01 +00001475 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001476 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001477
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05001478 assert(!vm_id_is_current_world(current->vm->id) ||
1479 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001480 current->state = VCPU_STATE_BLOCKED;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001481
Andrew Scull33fecd32019-01-08 14:48:27 +00001482 /*
1483 * Set a placeholder return code to the scheduler. This will be
1484 * overwritten when the switch back to the primary occurs.
1485 */
J-Alvesbd32c972024-02-02 16:20:04 +00001486 ret = api_ffa_interrupt_return(0);
Andrew Scull33fecd32019-01-08 14:48:27 +00001487
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001488out_vcpu:
1489 vcpu_unlock(&vcpu_next_locked);
1490
Andrew Scull6d2db332018-10-10 15:28:17 +01001491out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06001492 vcpu_unlock(&current_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001493 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001494}
1495
1496/**
Andrew Scull81e85092018-12-12 12:56:20 +00001497 * Check that the mode indicates memory that is valid, owned and exclusive.
1498 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001499static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001500{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001501 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1502 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001503}
1504
1505/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001506 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001507 */
Karl Meakin738bee12024-01-12 15:19:45 +00001508static struct ffa_value api_vm_configure_stage1(
1509 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1510 paddr_t pa_send_begin, paddr_t pa_send_end, paddr_t pa_recv_begin,
1511 paddr_t pa_recv_end, uint32_t extra_attributes,
1512 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001513{
Karl Meakin738bee12024-01-12 15:19:45 +00001514 struct ffa_value ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001515
Karl Meakin738bee12024-01-12 15:19:45 +00001516 /*
1517 * Map the send page as read-only in the SPMC/hypervisor address space.
1518 */
Andrew Sculle1322792019-07-01 17:46:10 +01001519 vm_locked.vm->mailbox.send =
1520 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001521 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001522 if (!vm_locked.vm->mailbox.send) {
Karl Meakin738bee12024-01-12 15:19:45 +00001523 ret = ffa_error(FFA_NO_MEMORY);
1524 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001525 }
1526
1527 /*
Karl Meakin738bee12024-01-12 15:19:45 +00001528 * Map the receive page as writable in the SPMC/hypervisor address
1529 * space. On failure, unmap the send page before returning.
Andrew Sculle1322792019-07-01 17:46:10 +01001530 */
1531 vm_locked.vm->mailbox.recv =
1532 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001533 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001534 if (!vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001535 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001536 goto fail_undo_send;
1537 }
1538
Karl Meakin738bee12024-01-12 15:19:45 +00001539 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001540 goto out;
1541
1542 /*
1543 * The following mappings will not require more memory than is available
1544 * in the local pool.
1545 */
1546fail_undo_send:
1547 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001548 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1549 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001550
Andrew Sculle1322792019-07-01 17:46:10 +01001551out:
Andrew Sculle1322792019-07-01 17:46:10 +01001552 return ret;
1553}
1554
1555/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001556 * Sanity checks and configures the send and receive pages in the VM stage-2
1557 * and hypervisor stage-1 page tables.
1558 *
1559 * Returns:
1560 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001561 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001562 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1563 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001564 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001565 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001566 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001567
1568struct ffa_value api_vm_configure_pages(
1569 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1570 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1571 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001572{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001573 struct ffa_value ret;
1574 paddr_t pa_send_begin;
1575 paddr_t pa_send_end;
1576 paddr_t pa_recv_begin;
1577 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001578 uint32_t orig_send_mode = 0;
1579 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001580 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001581
1582 /* We only allow these to be setup once. */
1583 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
Karl Meakin738bee12024-01-12 15:19:45 +00001584 dlog_error("%s: Mailboxes have already been setup for VM %#x\n",
1585 __func__, vm_locked.vm->id);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001586 ret = ffa_error(FFA_DENIED);
1587 goto out;
1588 }
1589
1590 /* Hafnium only supports a fixed size of RX/TX buffers. */
1591 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
Karl Meakine8937d92024-03-19 16:04:25 +00001592 dlog_error("%s: Page count must be %zu, it is %d\n", __func__,
Karl Meakin738bee12024-01-12 15:19:45 +00001593 HF_MAILBOX_SIZE / FFA_PAGE_SIZE, page_count);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001594 ret = ffa_error(FFA_INVALID_PARAMETERS);
1595 goto out;
1596 }
1597
1598 /* Fail if addresses are not page-aligned. */
1599 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1600 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001601 dlog_error("%s: Mailbox buffers not page-aligned\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001602 ret = ffa_error(FFA_INVALID_PARAMETERS);
1603 goto out;
1604 }
1605
1606 /* Convert to physical addresses. */
1607 pa_send_begin = pa_from_ipa(send);
1608 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1609 pa_recv_begin = pa_from_ipa(recv);
1610 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1611
1612 /* Fail if the same page is used for the send and receive pages. */
1613 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Karl Meakin738bee12024-01-12 15:19:45 +00001614 dlog_error("%s: Mailbox buffers overlap\n", __func__);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001615 ret = ffa_error(FFA_INVALID_PARAMETERS);
1616 goto out;
1617 }
Andrew Sculle1322792019-07-01 17:46:10 +01001618
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001619 /* Set stage 2 translation tables only for virtual FF-A instances. */
1620 if (vm_id_is_current_world(vm_locked.vm->id)) {
1621 /*
1622 * Ensure the pages are valid, owned and exclusive to the VM and
1623 * that the VM has the required access to the memory.
1624 */
1625 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1626 &orig_send_mode) ||
1627 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1628 (orig_send_mode & MM_MODE_R) == 0 ||
1629 (orig_send_mode & MM_MODE_W) == 0) {
1630 dlog_error(
1631 "VM doesn't have required access rights to map "
1632 "TX buffer in stage 2.\n");
1633 ret = ffa_error(FFA_INVALID_PARAMETERS);
1634 goto out;
1635 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001636
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001637 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1638 &orig_recv_mode) ||
1639 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1640 (orig_recv_mode & MM_MODE_R) == 0) {
1641 dlog_error(
1642 "VM doesn't have required access rights to map "
1643 "RX buffer in stage 2.\n");
1644 ret = ffa_error(FFA_INVALID_PARAMETERS);
1645 goto out;
1646 }
Andrew Sculle1322792019-07-01 17:46:10 +01001647
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001648 /* Take memory ownership away from the VM and mark as shared. */
1649 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1650 MM_MODE_W;
1651 if (vm_locked.vm->el0_partition) {
1652 mode |= MM_MODE_USER | MM_MODE_NG;
1653 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001654
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001655 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1656 mode, local_page_pool, NULL)) {
1657 dlog_error(
1658 "Cannot allocate a new entry in stage 2 "
1659 "translation table.\n");
1660 ret = ffa_error(FFA_NO_MEMORY);
1661 goto out;
1662 }
Andrew Sculle1322792019-07-01 17:46:10 +01001663
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001664 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1665 if (vm_locked.vm->el0_partition) {
1666 mode |= MM_MODE_USER | MM_MODE_NG;
1667 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001668
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001669 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1670 mode, local_page_pool, NULL)) {
1671 /* TODO: partial defrag of failed range. */
1672 /* Recover any memory consumed in failed mapping. */
Karl Meakin738bee12024-01-12 15:19:45 +00001673 dlog_error("%s: cannot map recv page\n", __func__);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001674 vm_ptable_defrag(vm_locked, local_page_pool);
Karl Meakin738bee12024-01-12 15:19:45 +00001675 ret = ffa_error(FFA_NO_MEMORY);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001676 goto fail_undo_send;
1677 }
Karl Meakin738bee12024-01-12 15:19:45 +00001678 } else {
1679 ret = arch_other_world_vm_configure_rxtx_map(
1680 vm_locked, local_page_pool, pa_send_begin, pa_send_end,
1681 pa_recv_begin, pa_recv_end);
1682 if (ret.func != FFA_SUCCESS_32) {
1683 goto out;
1684 }
Andrew Sculle1322792019-07-01 17:46:10 +01001685 }
1686
Olivier Deprez96a2a262020-06-11 17:21:38 +02001687 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1688 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1689
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001690 /*
1691 * For EL0 partitions, since both the partition and the hypervisor code
1692 * use the EL2&0 translation regime, it is critical to mark the mappings
1693 * of the send and recv buffers as non-global in the TLB. For one, if we
1694 * dont mark it as non-global, it would cause TLB conflicts since there
1695 * would be an identity mapping with non-global attribute in the
1696 * partitions page tables, but another identity mapping in the
1697 * hypervisor page tables with the global attribute. The other issue is
1698 * one of security, we dont want other partitions to be able to access
1699 * other partitions buffers through cached translations.
1700 */
1701 if (vm_locked.vm->el0_partition) {
1702 extra_attributes |= MM_MODE_NG;
1703 }
1704
Karl Meakin738bee12024-01-12 15:19:45 +00001705 ret = api_vm_configure_stage1(
1706 mm_stage1_locked, vm_locked, pa_send_begin, pa_send_end,
1707 pa_recv_begin, pa_recv_end, extra_attributes, local_page_pool);
1708 if (ret.func != FFA_SUCCESS_32) {
Andrew Sculle1322792019-07-01 17:46:10 +01001709 goto fail_undo_send_and_recv;
1710 }
1711
Manish Pandeyd34f8892020-06-19 17:41:07 +01001712 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001713 goto out;
1714
Andrew Sculle1322792019-07-01 17:46:10 +01001715fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001716 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001717 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001718
1719fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001720 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001721 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001722
1723out:
Andrew Sculle1322792019-07-01 17:46:10 +01001724 return ret;
1725}
1726
Karl Meakinb9705e22024-04-05 13:58:28 +01001727/**
1728 * Read the buffer addresses and VM ID of an FFA_RXTX_MAP request. Handles
1729 * forwarded messages by reading from the hypervisor's TX buffer.
1730 *
1731 * Returns the VM/SP ID on success.
1732 *
1733 * Returns `HF_INVALID_VM_ID` when the arguments provided via the ABI
1734 * FFA_RXTX_MAP indicate the SPMC should retrieve the RXTX description from the
1735 * Hypervisor RXTX buffers, and the hypervisor hasn't given its own RXTX buffers
1736 * for the SPMC to map.
1737 */
1738static ffa_id_t api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send,
1739 ipaddr_t *recv, uint32_t *page_count)
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001740{
Karl Meakinb9705e22024-04-05 13:58:28 +01001741 bool forwarded;
1742 struct vm_locked vm_locked;
1743 ffa_id_t owner_vm_id;
1744 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc;
1745 struct ffa_composite_memory_region *rx_region;
1746 struct ffa_composite_memory_region *tx_region;
1747
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001748 /*
1749 * If the message has been forwarded the effective addresses are in
1750 * hypervisor's TX buffer.
1751 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001752 forwarded = (current_vm->id == HF_OTHER_WORLD_ID) &&
1753 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1754 (*page_count == 0);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001755
1756 if (forwarded) {
Karl Meakinb9705e22024-04-05 13:58:28 +01001757 vm_locked = vm_lock(current_vm);
1758 endpoint_desc = (struct ffa_endpoint_rx_tx_descriptor *)
1759 vm_locked.vm->mailbox.send;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001760
Karl Meakinb9705e22024-04-05 13:58:28 +01001761 if (endpoint_desc == NULL) {
1762 dlog_error(
1763 "Trying to access RXTX description, but "
1764 "hypervisor has not provided RXTX buffers\n");
1765 vm_unlock(&vm_locked);
1766 return HF_INVALID_VM_ID;
1767 }
1768
1769 rx_region = ffa_endpoint_get_rx_memory_region(endpoint_desc);
1770 tx_region = ffa_endpoint_get_tx_memory_region(endpoint_desc);
1771
1772 owner_vm_id = endpoint_desc->endpoint_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001773 *recv = ipa_init(rx_region->constituents[0].address);
1774 *send = ipa_init(tx_region->constituents[0].address);
1775 *page_count = rx_region->constituents[0].page_count;
J-Alves28ad9b42022-12-08 12:19:43 +00001776
1777 vm_unlock(&vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001778 } else {
Karl Meakinb9705e22024-04-05 13:58:28 +01001779 owner_vm_id = current_vm->id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001780 }
Karl Meakinb9705e22024-04-05 13:58:28 +01001781
1782 return owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001783}
Karl Meakinb9705e22024-04-05 13:58:28 +01001784
Andrew Sculle1322792019-07-01 17:46:10 +01001785/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001786 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001787 * must not be shared. Locking of the page tables combined with a local memory
1788 * pool ensures there will always be enough memory to recover from any errors
1789 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1790 * by another core which could result in this transaction being unable to roll
1791 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001792 *
1793 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001794 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001795 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001796 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001797 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001798 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001799 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001800 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001801struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001802 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001803{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001804 struct ffa_value ret;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001805 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001806 struct mm_stage1_locked mm_stage1_locked;
1807 struct mpool local_page_pool;
J-Alves19e20cf2023-08-02 12:48:55 +01001808 ffa_id_t owner_vm_id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001809
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001810 /*
1811 * Get the original buffer addresses and VM ID in case of forwarded
1812 * message.
1813 */
Karl Meakinb9705e22024-04-05 13:58:28 +01001814 owner_vm_id = api_get_rxtx_description(current->vm, &send, &recv,
1815 &page_count);
1816 if (owner_vm_id == HF_INVALID_VM_ID) {
1817 return ffa_error(FFA_INVALID_PARAMETERS);
1818 }
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001819
Karl Meakin117c8082024-12-04 16:03:28 +00001820 owner_vm_locked = ffa_vm_find_locked_create(owner_vm_id);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001821 if (owner_vm_locked.vm == NULL) {
1822 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1823 owner_vm_id);
1824 return ffa_error(FFA_DENIED);
1825 }
Andrew Scull220e6212018-12-21 18:09:00 +00001826
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001827 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001828 * Create a local pool so any freed memory can't be used by another
1829 * thread. This is to ensure the original mapping can be restored if any
1830 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001831 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001832 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1833
Manish Pandeyd34f8892020-06-19 17:41:07 +01001834 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001835
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001836 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1837 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001838 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001839 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001840 }
1841
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001842 /* Forward buffer mapping to SPMC if coming from a VM. */
Karl Meakin117c8082024-12-04 16:03:28 +00001843 ffa_setup_rxtx_map_forward(owner_vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001844
Federico Recanati9f1b6532022-04-14 13:15:28 +02001845 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001846
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001847exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001848 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001849 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001850 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001851
1852 return ret;
1853}
1854
1855/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001856 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1857 * translation regime of the caller. Unmap the region for the hypervisor and
1858 * set the memory region to owned and exclusive for the component. Since the
1859 * memory region mapped in the page table, when the buffers were originally
1860 * created we can safely remap it.
1861 *
1862 * Returns:
1863 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1864 * behalf of the caller.
1865 * - FFA_SUCCESS on success if no further action is needed.
1866 */
J-Alves19e20cf2023-08-02 12:48:55 +01001867struct ffa_value api_ffa_rxtx_unmap(ffa_id_t allocator_id, struct vcpu *current)
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001868{
1869 struct vm *vm = current->vm;
1870 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01001871 ffa_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001872 struct mm_stage1_locked mm_stage1_locked;
1873 paddr_t send_pa_begin;
1874 paddr_t send_pa_end;
1875 paddr_t recv_pa_begin;
1876 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001877 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001878
J-Alves661e1b72023-08-02 13:39:40 +01001879 if (vm->id == HF_HYPERVISOR_VM_ID && !ffa_is_vm_id(allocator_id)) {
Olivier Deprez106c8ce2024-02-08 11:49:02 +01001880 dlog_verbose(
J-Alves9bd324a2023-01-12 10:52:52 +00001881 "The Hypervisor must specify a valid VM ID in register "
1882 "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n");
1883 return ffa_error(FFA_INVALID_PARAMETERS);
1884 }
1885
Federico Recanati8da9e332022-02-10 11:00:17 +01001886 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1887 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
J-Alves9bd324a2023-01-12 10:52:52 +00001888 dlog_error(
1889 "The register W1 (containing ID) must be 0 at virtual "
1890 "instances.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001891 return ffa_error(FFA_INVALID_PARAMETERS);
1892 }
1893
1894 /* VM ID of which buffers have to be unmapped. */
1895 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1896
Karl Meakin117c8082024-12-04 16:03:28 +00001897 vm_locked = ffa_vm_find_locked(owner_vm_id);
Federico Recanati8da9e332022-02-10 11:00:17 +01001898 vm = vm_locked.vm;
1899 if (vm == NULL) {
1900 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1901 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001902 return ffa_error(FFA_INVALID_PARAMETERS);
1903 }
1904
1905 /* Get send and receive buffers. */
1906 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001907 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001908 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001909 ret = ffa_error(FFA_INVALID_PARAMETERS);
1910 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001911 }
1912
1913 /* Currently a mailbox size of 1 page is assumed. */
1914 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1915 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1916 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1917 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1918
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001919 mm_stage1_locked = mm_lock_stage1();
1920
Federico Recanati8da9e332022-02-10 11:00:17 +01001921 /* Reset stage 2 mapping only for virtual FF-A instances. */
1922 if (vm_id_is_current_world(owner_vm_id)) {
1923 /*
1924 * Set the memory region of the buffers back to the default mode
1925 * for the VM. Since this memory region was already mapped for
1926 * the RXTX buffers we can safely remap them.
1927 */
1928 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1929 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1930 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001931
Federico Recanati8da9e332022-02-10 11:00:17 +01001932 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1933 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1934 &api_page_pool, NULL));
Karl Meakin738bee12024-01-12 15:19:45 +00001935 } else {
1936 ret = arch_other_world_vm_configure_rxtx_unmap(
1937 vm_locked, &api_page_pool, send_pa_begin, send_pa_end,
1938 recv_pa_begin, recv_pa_end);
1939 if (ret.func != FFA_SUCCESS_32) {
1940 goto out;
1941 }
Federico Recanati8da9e332022-02-10 11:00:17 +01001942 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001943
1944 /* Unmap the buffers in the partition manager. */
1945 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1946 &api_page_pool));
1947 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1948 &api_page_pool));
1949
1950 vm->mailbox.send = NULL;
1951 vm->mailbox.recv = NULL;
Karl Meakin117c8082024-12-04 16:03:28 +00001952 ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001953
Federico Recanati8da9e332022-02-10 11:00:17 +01001954 /* Forward buffer unmapping to SPMC if coming from a VM. */
Karl Meakin117c8082024-12-04 16:03:28 +00001955 ffa_setup_rxtx_unmap_forward(vm_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001956
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001957 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001958
1959out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001960 vm_unlock(&vm_locked);
1961
Federico Recanati8da9e332022-02-10 11:00:17 +01001962 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001963}
1964
Karl Meakin355d5c22024-12-12 18:04:56 +00001965static struct ffa_value api_ffa_msg_send2_copy_data(
1966 struct ffa_partition_rxtx_header *header, struct vm *receiver_vm,
Karl Meakina78337f2024-12-12 23:30:06 +00001967 struct vm_locked sender_locked)
Karl Meakin355d5c22024-12-12 18:04:56 +00001968{
Karl Meakina78337f2024-12-12 23:30:06 +00001969 const void *sender_tx_buffer = sender_locked.vm->mailbox.send;
Karl Meakin355d5c22024-12-12 18:04:56 +00001970 uint32_t total_size;
Karl Meakina78337f2024-12-12 23:30:06 +00001971 uint32_t min_offset;
Karl Meakin355d5c22024-12-12 18:04:56 +00001972
Karl Meakina78337f2024-12-12 23:30:06 +00001973 switch (sender_locked.vm->ffa_version) {
1974 case FFA_VERSION_1_0:
1975 dlog_verbose("Indirect messaging not supported in v1.0\n");
1976 return ffa_error(FFA_NOT_SUPPORTED);
1977 case FFA_VERSION_1_1:
1978 min_offset = FFA_RXTX_HEADER_SIZE_V1_1;
1979 break;
1980 default:
1981 min_offset = FFA_RXTX_HEADER_SIZE;
1982 break;
1983 }
1984
1985 if (header->offset < min_offset) {
1986 dlog_error(
1987 "Indirect message payload overlaps with header (%u < "
1988 "%u, version = %#x)\n",
1989 header->offset, min_offset,
1990 sender_locked.vm->ffa_version);
Karl Meakin355d5c22024-12-12 18:04:56 +00001991 return ffa_error(FFA_INVALID_PARAMETERS);
1992 }
1993
Karl Meakina78337f2024-12-12 23:30:06 +00001994 /*
1995 * Check the size of transfer.
1996 * Check for overflow in the sum so that very large offsets and/or sizes
1997 * do not pass the check.
1998 */
1999 if (add_overflow(header->offset, header->size, &total_size)) {
2000 dlog_error(
2001 "Overflow calculating message size (offset = %u, size "
2002 "= %u)\n",
2003 header->offset, header->size);
2004 return ffa_error(FFA_INVALID_PARAMETERS);
2005 }
2006
2007 if (total_size > FFA_MSG_PAYLOAD_MAX) {
2008 dlog_error("Message is too big (%u > %zu)\n", total_size,
2009 FFA_MSG_PAYLOAD_MAX);
Karl Meakin355d5c22024-12-12 18:04:56 +00002010 return ffa_error(FFA_INVALID_PARAMETERS);
2011 }
2012
2013 /* Copy data. */
2014 if (!memcpy_trapped(receiver_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
2015 sender_tx_buffer, total_size)) {
2016 dlog_error(
Karl Meakina78337f2024-12-12 23:30:06 +00002017 "%s: Failed to copy message to receiver's (%x) RX "
Karl Meakin355d5c22024-12-12 18:04:56 +00002018 "buffer.\n",
2019 __func__, receiver_vm->id);
2020 return ffa_error(FFA_ABORTED);
2021 }
2022
2023 receiver_vm->mailbox.recv_size = total_size;
2024 receiver_vm->mailbox.recv_sender = header->sender;
2025 receiver_vm->mailbox.recv_func = FFA_MSG_SEND2_32;
2026 receiver_vm->mailbox.state = MAILBOX_STATE_FULL;
2027
2028 return (struct ffa_value){.func = FFA_SUCCESS_32};
2029}
2030
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002031/**
Federico Recanati25053ee2022-03-14 15:01:53 +01002032 * Copies data from the sender's send buffer to the recipient's receive buffer
2033 * and notifies the receiver.
2034 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002035struct ffa_value api_ffa_msg_send2(ffa_id_t sender_id, uint32_t flags,
Federico Recanati25053ee2022-03-14 15:01:53 +01002036 struct vcpu *current)
2037{
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002038 struct vm *current_vm = current->vm;
2039 struct vm *receiver_vm;
2040 struct vm_locked receiver_locked;
Federico Recanati25053ee2022-03-14 15:01:53 +01002041 struct vm_locked sender_locked;
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002042 const void *sender_tx_buffer;
Federico Recanati25053ee2022-03-14 15:01:53 +01002043 struct ffa_value ret;
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002044 ffa_id_t header_sender_id;
2045 ffa_id_t header_receiver_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01002046
Karl Meakina5ea9092024-05-28 15:40:33 +01002047 alignas(8) struct ffa_partition_rxtx_header header;
2048
Federico Recanati25053ee2022-03-14 15:01:53 +01002049 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002050 if (current_vm->id != HF_HYPERVISOR_VM_ID && sender_id != 0) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002051 dlog_error("Sender VM ID must be zero.\n");
2052 return ffa_error(FFA_INVALID_PARAMETERS);
2053 }
2054
Federico Recanati25053ee2022-03-14 15:01:53 +01002055 /*
2056 * Get message sender's mailbox, which can be different to the `from` vm
2057 * when the message is forwarded.
2058 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002059 sender_id = (sender_id != 0) ? sender_id : current_vm->id;
2060 sender_locked = ffa_vm_find_locked(sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002061 if (sender_locked.vm == NULL) {
2062 dlog_error("Cannot send message from VM ID %#x, not found.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002063 sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002064 return ffa_error(FFA_DENIED);
2065 }
2066
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002067 sender_tx_buffer = sender_locked.vm->mailbox.send;
2068 if (sender_tx_buffer == NULL) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002069 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002070 sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002071 ret = ffa_error(FFA_DENIED);
2072 goto out_unlock_sender;
2073 }
2074
2075 /*
2076 * Copy message header as safety measure to avoid multiple accesses to
2077 * unsafe memory which could be 'corrupted' between safety checks and
2078 * final buffer copy.
Karl Meakina78337f2024-12-12 23:30:06 +00002079 * This includes the UUID added in v1.2. Messages that do not specify a
2080 * UUID (v1.1 or earlier) will leave the UUID unspecified, so this is
2081 * backwards compatible.
Federico Recanati25053ee2022-03-14 15:01:53 +01002082 */
Karl Meakinbf580c22024-12-12 14:31:06 +00002083 if (!memcpy_trapped(&header, sizeof(header), sender_tx_buffer,
J-Alves8a5fd9d2024-04-09 11:22:49 +01002084 FFA_RXTX_HEADER_SIZE)) {
2085 dlog_error(
2086 "%s: Failed to copy message from sender's(%x) TX "
2087 "buffer.\n",
2088 __func__, sender_locked.vm->id);
2089 ret = ffa_error(FFA_ABORTED);
2090 goto out_unlock_sender;
2091 }
2092
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002093 header_sender_id = header.sender;
2094 header_receiver_id = header.receiver;
Federico Recanati25053ee2022-03-14 15:01:53 +01002095
2096 /* Ensure Sender IDs from API and from message header match. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002097 if (sender_id != header_sender_id) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002098 dlog_error(
2099 "Message sender VM ID (%#x) doesn't match header's VM "
2100 "ID (%#x).\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002101 sender_id, header_sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002102 ret = ffa_error(FFA_INVALID_PARAMETERS);
2103 goto out_unlock_sender;
2104 }
2105
2106 /* Disallow reflexive requests as this suggests an error in the VM. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002107 if (header_receiver_id == header_sender_id) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002108 dlog_error("Sender and receive VM IDs must be different.\n");
2109 ret = ffa_error(FFA_INVALID_PARAMETERS);
2110 goto out_unlock_sender;
2111 }
2112
Federico Recanati7b39ff22022-03-14 15:01:53 +01002113 /* `flags` can be set only at secure virtual FF-A instances. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002114 if (ffa_is_vm_id(header_sender_id) && flags != 0) {
Federico Recanati7b39ff22022-03-14 15:01:53 +01002115 dlog_error("flags must be zero.\n");
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002116 ret = ffa_error(FFA_INVALID_PARAMETERS);
2117 goto out_unlock_sender;
Federico Recanati7b39ff22022-03-14 15:01:53 +01002118 }
2119
Federico Recanati25053ee2022-03-14 15:01:53 +01002120 /*
2121 * Check if the message has to be forwarded to the SPMC, in
2122 * this case return, the SPMC will handle the buffer copy.
2123 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002124 if (ffa_indirect_msg_send2_forward(header_receiver_id, header_sender_id,
2125 &ret)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002126 goto out_unlock_sender;
2127 }
2128
2129 /* Ensure the receiver VM exists. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002130 receiver_locked = ffa_vm_find_locked(header_receiver_id);
2131 receiver_vm = receiver_locked.vm;
Federico Recanati25053ee2022-03-14 15:01:53 +01002132
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002133 if (receiver_vm == NULL) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002134 dlog_error("Cannot deliver message to VM %#x, not found.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002135 header_receiver_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002136 ret = ffa_error(FFA_INVALID_PARAMETERS);
2137 goto out_unlock_sender;
2138 }
2139
2140 /*
2141 * Check sender and receiver can use indirect messages.
2142 * Sender is the VM/SP who originally sent the message, not the
2143 * hypervisor possibly relaying it.
2144 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002145 if (!ffa_indirect_msg_is_supported(sender_locked, receiver_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002146 dlog_verbose("VM %#x doesn't support indirect message\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002147 header_sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002148 ret = ffa_error(FFA_DENIED);
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002149 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002150 }
2151
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002152 if (vm_is_mailbox_busy(receiver_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002153 dlog_error(
2154 "Cannot deliver message to VM %#x, RX buffer not "
2155 "ready.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002156 header_receiver_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002157 ret = ffa_error(FFA_BUSY);
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002158 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002159 }
2160
Federico Recanati644f0462022-03-17 12:04:00 +01002161 /* Acquire receiver's RX buffer. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002162 if (!ffa_setup_acquire_receiver_rx(receiver_locked, &ret)) {
2163 dlog_error("Failed to acquire RX buffer for VM %#x\n",
2164 receiver_vm->id);
2165 goto out_unlock_both;
Federico Recanati644f0462022-03-17 12:04:00 +01002166 }
2167
Karl Meakina78337f2024-12-12 23:30:06 +00002168 ret = api_ffa_msg_send2_copy_data(&header, receiver_vm, sender_locked);
Karl Meakin355d5c22024-12-12 18:04:56 +00002169 if (ret.func != FFA_SUCCESS_32) {
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002170 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002171 }
2172
J-Alves3bb82592023-01-23 11:20:54 +00002173 /*
2174 * Set framework notifications, only if the SP has enabled
2175 * receipt of notifications.
2176 * If VMs have provided the RX buffer it is implied they already
2177 * support indirect messaging, and therefore framework notifications.
2178 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002179 if (ffa_is_vm_id(receiver_locked.vm->id) ||
2180 vm_are_notifications_enabled(receiver_locked.vm)) {
J-Alves3bb82592023-01-23 11:20:54 +00002181 ffa_notifications_bitmap_t rx_buffer_full =
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002182 ffa_is_vm_id(header_sender_id)
J-Alves3bb82592023-01-23 11:20:54 +00002183 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
2184 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
Federico Recanati25053ee2022-03-14 15:01:53 +01002185
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002186 vm_notifications_framework_set_pending(receiver_locked,
J-Alves3bb82592023-01-23 11:20:54 +00002187 rx_buffer_full);
2188
2189 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2190 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2191 vcpu_index(current));
Karl Meakin117c8082024-12-04 16:03:28 +00002192 ffa_notifications_sri_trigger_not_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002193 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00002194 ffa_notifications_sri_set_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002195 }
Federico Recanati25053ee2022-03-14 15:01:53 +01002196 }
2197
2198 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2199
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002200out_unlock_both:
2201 vm_unlock(&receiver_locked);
Federico Recanati25053ee2022-03-14 15:01:53 +01002202
2203out_unlock_sender:
2204 vm_unlock(&sender_locked);
2205
2206 return ret;
2207}
2208
2209/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002210 * Releases the caller's mailbox so that a new message can be received. The
2211 * caller must have copied out all data they wish to preserve as new messages
2212 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002213 *
2214 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002215 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2216 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002217 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2218 * - FFA_SUCCESS on success if no further action is needed.
2219 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002220 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002221 * hf_mailbox_waiter_get.
2222 */
J-Alves19e20cf2023-08-02 12:48:55 +01002223struct ffa_value api_ffa_rx_release(ffa_id_t receiver_id, struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002224{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002225 struct vm *current_vm = current->vm;
2226 struct vm *vm;
2227 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01002228 ffa_id_t current_vm_id = current_vm->id;
2229 ffa_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002230 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002231
Federico Recanati7bef0b92022-03-17 14:56:22 +01002232 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2233 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2234 dlog_error("Invalid `receiver_id`, must be zero.\n");
2235 return ffa_error(FFA_INVALID_PARAMETERS);
2236 }
2237
2238 /*
2239 * VM ID to be released: `receiver_id` if message has been forwarded by
2240 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2241 */
2242 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2243 release_vm_id = current_vm_id;
2244 } else {
2245 release_vm_id = receiver_id;
2246 }
2247
Karl Meakin117c8082024-12-04 16:03:28 +00002248 vm_locked = ffa_vm_find_locked(release_vm_id);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002249 vm = vm_locked.vm;
2250 if (vm == NULL) {
2251 dlog_error("No buffer registered for VM ID %#x.\n",
2252 release_vm_id);
2253 return ffa_error(FFA_INVALID_PARAMETERS);
2254 }
2255
Karl Meakin117c8082024-12-04 16:03:28 +00002256 if (ffa_setup_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002257 goto out;
2258 }
2259
Karl Meakin1064a9c2024-06-04 17:20:43 +01002260 ret = api_release_mailbox(vm_locked);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002261
2262out:
2263 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002264
2265 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002266}
Andrew Walbran318f5732018-11-20 16:23:42 +00002267
2268/**
Federico Recanati644f0462022-03-17 12:04:00 +01002269 * Acquire ownership of an RX buffer before writing to it. Both
2270 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2271 * could contend for the same buffer. SPMC owns VM's RX buffer after
2272 * it's mapped in its translation regime. This ABI should be
2273 * used by the Hypervisor to get the ownership of a VM's RX buffer
2274 * from the SPMC solving the aforementioned possible contention.
2275 *
2276 * Returns:
2277 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2278 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2279 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2280 */
J-Alves19e20cf2023-08-02 12:48:55 +01002281struct ffa_value api_ffa_rx_acquire(ffa_id_t receiver_id, struct vcpu *current)
Federico Recanati644f0462022-03-17 12:04:00 +01002282{
2283 struct vm_locked receiver_locked;
2284 struct vm *receiver;
2285 struct ffa_value ret;
2286
2287 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
J-Alves661e1b72023-08-02 13:39:40 +01002288 !ffa_is_vm_id(receiver_id)) {
Federico Recanati644f0462022-03-17 12:04:00 +01002289 dlog_error(
2290 "FFA_RX_ACQUIRE not supported at this FF-A "
2291 "instance.\n");
2292 return ffa_error(FFA_NOT_SUPPORTED);
2293 }
2294
Karl Meakin117c8082024-12-04 16:03:28 +00002295 receiver_locked = ffa_vm_find_locked(receiver_id);
Federico Recanati644f0462022-03-17 12:04:00 +01002296 receiver = receiver_locked.vm;
2297
2298 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2299 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2300 receiver_id);
2301 ret = ffa_error(FFA_INVALID_PARAMETERS);
2302 goto out;
2303 }
2304
2305 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2306 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2307 ret = ffa_error(FFA_DENIED);
2308 goto out;
2309 }
2310
J-Alvese8c8c2b2022-12-16 15:34:48 +00002311 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002312
2313 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2314
2315out:
2316 vm_unlock(&receiver_locked);
2317
2318 return ret;
2319}
2320
J-Alvescc542042024-07-24 19:13:22 +01002321/*
2322 * Returns true if intid relates with either of those:
2323 * - NPI
2324 * - ME
2325 * - Virtual Timer.
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002326 * - IPI
J-Alvescc542042024-07-24 19:13:22 +01002327 *
2328 * These are VIs with no expected interrupt descriptor.
2329 */
2330static bool api_is_maintenance_virtual_interrupt(uint32_t intid)
2331{
2332 return intid == HF_NOTIFICATION_PENDING_INTID ||
2333 intid == HF_MANAGED_EXIT_INTID ||
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002334 intid == HF_VIRTUAL_TIMER_INTID || intid == HF_IPI_INTID;
J-Alvescc542042024-07-24 19:13:22 +01002335}
2336
Federico Recanati644f0462022-03-17 12:04:00 +01002337/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002338 * Enables or disables a given interrupt ID for the calling vCPU.
2339 *
2340 * Returns 0 on success, or -1 if the intid is invalid.
2341 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002342int64_t api_interrupt_enable(uint32_t intid, bool enable,
2343 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002344{
Manish Pandey35e452f2021-02-18 21:36:34 +00002345 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002346 struct interrupts *interrupts = &current->interrupts;
J-Alvescc542042024-07-24 19:13:22 +01002347 struct interrupt_descriptor *int_desc = NULL;
2348 struct vm *vm = current->vm;
2349 struct vm_locked vm_locked;
2350
2351 int64_t ret = -1;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002352
Andrew Walbran318f5732018-11-20 16:23:42 +00002353 if (intid >= HF_NUM_INTIDS) {
2354 return -1;
2355 }
2356
J-Alvescc542042024-07-24 19:13:22 +01002357 vm_locked = vm_lock(vm);
Manish Pandey35e452f2021-02-18 21:36:34 +00002358 current_locked = vcpu_lock(current);
J-Alvescc542042024-07-24 19:13:22 +01002359
2360 int_desc = vm_interrupt_set_enable(vm_locked, intid, enable);
2361
2362 if (!api_is_maintenance_virtual_interrupt(intid)) {
2363 if (int_desc == NULL) {
2364 dlog_error("%s: invalid interrupt ID.\n", __func__);
2365 goto out;
2366 }
2367
2368 plat_interrupts_configure_interrupt(*int_desc);
2369 }
2370
Andrew Walbran318f5732018-11-20 16:23:42 +00002371 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002372 /*
2373 * If it is pending and was not enabled before, increment the
2374 * count.
2375 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002376 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2377 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2378 vcpu_interrupt_count_increment(current_locked,
2379 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002380 }
J-Alvescc542042024-07-24 19:13:22 +01002381
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002382 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2383 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002384 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002385 /*
2386 * If it is pending and was enabled before, decrement the count.
2387 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002388 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2389 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2390 vcpu_interrupt_count_decrement(current_locked,
2391 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002392 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002393 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2394 vcpu_virt_interrupt_set_type(interrupts, intid,
2395 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002396 }
2397
J-Alvescc542042024-07-24 19:13:22 +01002398 ret = 0;
2399
2400out:
2401 vm_unlock(&vm_locked);
Manish Pandey35e452f2021-02-18 21:36:34 +00002402 vcpu_unlock(&current_locked);
J-Alvescc542042024-07-24 19:13:22 +01002403
2404 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002405}
2406
2407/**
2408 * Returns the ID of the next pending interrupt for the calling vCPU, and
2409 * acknowledges it (i.e. marks it as no longer pending). Returns
2410 * HF_INVALID_INTID if there are no pending interrupts.
2411 */
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002412uint32_t api_interrupt_get(struct vcpu_locked current_locked)
Andrew Walbran318f5732018-11-20 16:23:42 +00002413{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002414 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002415 uint32_t first_interrupt = HF_INVALID_INTID;
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002416 struct interrupts *interrupts = &current_locked.vcpu->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002417
2418 /*
2419 * Find the first enabled and pending interrupt ID, return it, and
2420 * deactivate it.
2421 */
Andrew Walbran318f5732018-11-20 16:23:42 +00002422 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2423 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002424 interrupts->interrupt_enabled.bitmap[i] &
2425 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002426
Andrew Walbran318f5732018-11-20 16:23:42 +00002427 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002428 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002429
2430 first_interrupt =
2431 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002432
Andrew Walbran3d84a262018-12-13 14:41:19 +00002433 /*
2434 * Mark it as no longer pending and decrement the count.
2435 */
J-Alvesb8730e92024-08-07 18:28:55 +01002436 vcpu_interrupt_clear_decrement(current_locked,
2437 first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002438 break;
2439 }
2440 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002441
Andrew Walbran318f5732018-11-20 16:23:42 +00002442 return first_interrupt;
2443}
2444
2445/**
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002446 * Negotiate the FF-A version to be used for this FF-A instance.
2447 * See section 13.2 of the FF-A v1.2 ALP1 spec.
2448 *
2449 * Returns Hafnium's version number (`FFA_VERSION_COMPILED`) on success.
2450 * Returns `FFA_NOT_SUPPORTED` on error:
2451 * - The version is invalid (highest bit set).
2452 * - The requested version is incompatible.
2453 * - The version has already been negotiated and cannot be changed.
2454 */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002455struct ffa_value api_ffa_version(struct vcpu *current,
Karl Meakin0e617d92024-04-05 12:55:22 +01002456 enum ffa_version requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002457{
Karl Meakin0e617d92024-04-05 12:55:22 +01002458 static_assert(sizeof(enum ffa_version) == 4,
2459 "enum ffa_version must be 4 bytes wide");
2460
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002461 const struct ffa_value error = {.func = (uint32_t)FFA_NOT_SUPPORTED};
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002462 struct vm_locked current_vm_locked;
2463
Karl Meakin0e617d92024-04-05 12:55:22 +01002464 if (!ffa_version_is_valid(requested_version)) {
2465 dlog_error(
2466 "FFA_VERSION: requested version %#x is invalid "
2467 "(highest bit must be zero)\n",
2468 requested_version);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002469 return error;
Andrew Walbran9fd29072020-04-22 12:12:14 +01002470 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002471
Karl Meakin0e617d92024-04-05 12:55:22 +01002472 if (!ffa_versions_are_compatible(requested_version,
2473 FFA_VERSION_COMPILED)) {
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002474 dlog_error(
2475 "FFA_VERSION: requested version v%u.%u is not "
2476 "compatible with v%u.%u\n",
2477 ffa_version_get_major(requested_version),
2478 ffa_version_get_minor(requested_version),
2479 ffa_version_get_major(FFA_VERSION_COMPILED),
2480 ffa_version_get_minor(FFA_VERSION_COMPILED));
2481 return error;
J-Alves3cc9d6f2023-07-20 16:46:15 +01002482 }
2483
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002484 current_vm_locked = vm_lock(current->vm);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002485
2486 if (current_vm_locked.vm->ffa_version_negotiated &&
2487 requested_version != current_vm_locked.vm->ffa_version) {
2488 vm_unlock(&current_vm_locked);
2489 dlog_error(
2490 "FFA_VERSION: Cannot change FF-A version after other "
2491 "FF-A calls have been made\n");
2492 return error;
2493 }
2494
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002495 current_vm_locked.vm->ffa_version = requested_version;
2496 vm_unlock(&current_vm_locked);
2497
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002498 return (struct ffa_value){.func = (uint32_t)FFA_VERSION_COMPILED};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002499}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002500
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002501/**
Karl Meakin49ec1e42024-05-10 13:08:24 +01002502 * Helper for success return of FFA_FEATURES.
J-Alves6f72ca82021-11-01 12:34:58 +00002503 */
2504struct ffa_value api_ffa_feature_success(uint32_t arg2)
2505{
2506 return (struct ffa_value){
Karl Meakin49ec1e42024-05-10 13:08:24 +01002507 .func = FFA_SUCCESS_32,
2508 .arg1 = 0U,
2509 .arg2 = arg2,
2510 };
J-Alves6f72ca82021-11-01 12:34:58 +00002511}
2512
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002513static struct ffa_value ffa_features_function(uint32_t func,
2514 uint32_t input_property,
2515 struct vcpu *current)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002516{
Karl Meakin0e617d92024-04-05 12:55:22 +01002517 const enum ffa_version ffa_version = current->vm->ffa_version;
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002518 const bool el0_partition = current->vm->el0_partition;
2519
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002520 switch (func) {
J-Alves6f72ca82021-11-01 12:34:58 +00002521 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002522 case FFA_ERROR_32:
2523 case FFA_SUCCESS_32:
2524 case FFA_INTERRUPT_32:
2525 case FFA_VERSION_32:
2526 case FFA_FEATURES_32:
2527 case FFA_RX_RELEASE_32:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002528 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002529 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002530 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002531 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002532 case FFA_RUN_32:
J-Alves95fbb312024-03-20 15:19:16 +00002533 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002534 case FFA_MEM_DONATE_32:
2535 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00002536 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002537 case FFA_MEM_SHARE_32:
J-Alves95fbb312024-03-20 15:19:16 +00002538 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002539 case FFA_MEM_RETRIEVE_RESP_32:
2540 case FFA_MEM_RELINQUISH_32:
2541 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002542 case FFA_MEM_FRAG_RX_32:
2543 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002544 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002545 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002546 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002547 case FFA_MSG_SEND_DIRECT_REQ_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002548
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002549 /* FF-A v1.1 features. */
2550 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002551 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2552 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2553 case FFA_NOTIFICATION_BIND_32:
2554 case FFA_NOTIFICATION_UNBIND_32:
2555 case FFA_NOTIFICATION_SET_32:
2556 case FFA_NOTIFICATION_GET_32:
2557 case FFA_NOTIFICATION_INFO_GET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002558 case FFA_MSG_SEND2_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002559 if (FFA_VERSION_1_1 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002560 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002561 }
2562
Kathleen Capellae4fe2962023-09-01 17:08:47 -04002563 /* FF-A v1.2 features. */
2564 case FFA_CONSOLE_LOG_32:
2565 case FFA_CONSOLE_LOG_64:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002566 case FFA_PARTITION_INFO_GET_REGS_64:
Kathleen Capella41fea932023-06-23 17:39:28 -04002567 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002568 case FFA_MSG_SEND_DIRECT_RESP2_64:
Karl Meakin0e617d92024-04-05 12:55:22 +01002569 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002570 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002571 }
2572
Karl Meakin49ec1e42024-05-10 13:08:24 +01002573 return api_ffa_feature_success(0);
2574
Karl Meakin5a222642024-06-20 10:23:37 +01002575 /* These functions are only supported on S-EL0 partitions. */
2576 case FFA_MEM_PERM_GET_32:
2577 case FFA_MEM_PERM_SET_32:
2578 case FFA_MEM_PERM_GET_64:
2579 case FFA_MEM_PERM_SET_64:
2580 if (!(vm_id_is_current_world(current->vm->id) &&
2581 el0_partition)) {
2582 dlog_verbose(
2583 "FFA_FEATURE: %s is only supported on S-EL0 "
2584 "partitions\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002585 ffa_func_name(func));
Karl Meakin5a222642024-06-20 10:23:37 +01002586 return ffa_error(FFA_NOT_SUPPORTED);
2587 }
2588 return api_ffa_feature_success(0);
2589
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002590 case FFA_SECONDARY_EP_REGISTER_64:
2591 if (FFA_VERSION_COMPILED < FFA_VERSION_1_1) {
2592 return ffa_error(FFA_NOT_SUPPORTED);
2593 }
2594
2595 if (!(vm_id_is_current_world(current->vm->id) &&
2596 current->vm->vcpu_count > 1)) {
2597 dlog_verbose(
2598 "FFA_FEATURE: %s is only supported on SPs with "
2599 "more than 1 vCPU\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002600 ffa_func_name(func));
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002601 return ffa_error(FFA_NOT_SUPPORTED);
2602 }
2603 return api_ffa_feature_success(0);
2604
Karl Meakin650cb142024-06-20 10:31:57 +01002605 /*
2606 * This function is restricted to the secure virtual FF-A instance (i.e.
2607 * only report success to SPs).
2608 */
2609 case FFA_YIELD_32:
2610 if (!vm_id_is_current_world(current->vm->id)) {
2611 dlog_verbose(
2612 "FFA_FEATURES: %s is only supported at secure "
2613 "virtual FF-A instance\n",
2614 ffa_func_name(FFA_YIELD_32));
2615 return ffa_error(FFA_NOT_SUPPORTED);
2616 }
2617 return api_ffa_feature_success(0);
2618
Karl Meakin0fd67292024-02-06 17:33:05 +00002619 case FFA_RXTX_MAP_64: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002620 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002621 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002622 }
2623
Karl Meakin0fd67292024-02-06 17:33:05 +00002624 uint32_t arg2 = 0;
2625 struct ffa_features_rxtx_map_params params = {
2626 .min_buf_size = FFA_RXTX_MAP_MIN_BUF_4K,
2627 .mbz = 0,
2628 .max_buf_size =
Karl Meakin0e617d92024-04-05 12:55:22 +01002629 (ffa_version >= FFA_VERSION_1_2)
Karl Meakin0fd67292024-02-06 17:33:05 +00002630 ? FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT
2631 : 0,
2632 };
2633
2634 memcpy_s(&arg2, sizeof(arg2), &params, sizeof(params));
Karl Meakin49ec1e42024-05-10 13:08:24 +01002635 return api_ffa_feature_success(arg2);
Karl Meakin0fd67292024-02-06 17:33:05 +00002636 }
2637
J-Alves95fbb312024-03-20 15:19:16 +00002638 case FFA_MEM_RETRIEVE_REQ_64:
Karl Meakin49ec1e42024-05-10 13:08:24 +01002639 case FFA_MEM_RETRIEVE_REQ_32: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002640 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002641 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002642 }
2643
Karl Meakin49ec1e42024-05-10 13:08:24 +01002644 if (ANY_BITS_SET(input_property,
2645 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2646 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT) ||
2647 IS_BIT_SET(input_property,
2648 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT)) {
2649 dlog_warning(
2650 "FFA_FEATURES: Bits[%u:%u] and Bit[%u] of "
2651 "input_property should be 0 (input_property = "
2652 "%#x)\n",
2653 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2654 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT,
2655 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT,
Karl Meakin34b8ae92023-01-13 13:33:07 +00002656 input_property);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002657 }
2658
Karl Meakin0e617d92024-04-05 12:55:22 +01002659 if (ffa_version >= FFA_VERSION_1_1 &&
2660 (input_property &
2661 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
Karl Meakin4e8e4792024-07-05 16:28:42 +01002662 dlog_verbose(
2663 "FFA_FEATURES: NS bit support must be 1\n");
Karl Meakin6fd6c1d2024-05-13 12:09:13 +01002664 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002665 }
2666
2667 return api_ffa_feature_success(
2668 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
Karl Meakin49ec1e42024-05-10 13:08:24 +01002669 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT |
Karl Meakin34b8ae92023-01-13 13:33:07 +00002670 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
Karl Meakin49ec1e42024-05-10 13:08:24 +01002671 }
J-Alves6f72ca82021-11-01 12:34:58 +00002672
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002673 default:
2674 return ffa_error(FFA_NOT_SUPPORTED);
2675 }
2676}
2677
2678static struct ffa_value ffa_features_feature(enum ffa_feature_id feature,
2679 uint32_t input_property,
2680 struct vcpu *current)
2681{
2682 const bool el0_partition = current->vm->el0_partition;
2683
2684 if (ANY_BITS_SET(feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2685 FFA_FEATURES_FEATURE_MBZ_LO_BIT)) {
2686 dlog_verbose(
2687 "FFA_FEATURES: feature ID %#x is invalid (bits [%u:%u] "
2688 "must be zero)\n",
2689 feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2690 FFA_FEATURES_FEATURE_MBZ_LO_BIT);
2691 return ffa_error(FFA_NOT_SUPPORTED);
2692 }
2693 if (input_property != 0) {
2694 dlog_verbose(
2695 "FFA_FEATURES: input_property must be 0 "
2696 "(input_property = %#x)\n",
2697 input_property);
2698 return ffa_error(FFA_NOT_SUPPORTED);
2699 }
2700
2701 switch (feature) {
J-Alves6f72ca82021-11-01 12:34:58 +00002702 /* Check support of a feature provided respective feature ID. */
Karl Meakin17c9ad32024-04-12 16:41:00 +01002703
2704 /*
2705 * For NPI and MEI, report the IDs as supported only to partitions at
2706 * the virtual FF-A instances.
2707 */
J-Alves6f72ca82021-11-01 12:34:58 +00002708 case FFA_FEATURE_NPI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002709 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002710 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002711 }
2712
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002713 if (el0_partition) {
2714 return ffa_error(FFA_NOT_SUPPORTED);
2715 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002716 if (!vm_id_is_current_world(current->vm->id)) {
2717 return ffa_error(FFA_NOT_SUPPORTED);
2718 }
J-Alves6f72ca82021-11-01 12:34:58 +00002719 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
Karl Meakin17c9ad32024-04-12 16:41:00 +01002720
2721 case FFA_FEATURE_MEI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002722 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002723 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002724 }
J-Alvesc4d9ae82024-05-14 10:15:03 +01002725 if (el0_partition) {
2726 return ffa_error(FFA_NOT_SUPPORTED);
2727 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002728 if (!vm_id_is_current_world(current->vm->id)) {
2729 return ffa_error(FFA_NOT_SUPPORTED);
2730 }
2731 return api_ffa_feature_success(HF_MANAGED_EXIT_INTID);
2732
J-Alves6f72ca82021-11-01 12:34:58 +00002733 case FFA_FEATURE_SRI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002734 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002735 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002736 }
2737
Karl Meakincb6642c2024-02-27 14:43:45 +00002738 if (!ffa_is_vm_id(current->vm->id)) {
2739 return ffa_error(FFA_NOT_SUPPORTED);
2740 }
J-Alves6f72ca82021-11-01 12:34:58 +00002741 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
Karl Meakin0e617d92024-04-05 12:55:22 +01002742
J-Alves6f72ca82021-11-01 12:34:58 +00002743 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002744 default:
Karl Meakin4271ff92024-05-13 12:33:15 +01002745 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002746 }
2747}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002748
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002749/**
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002750 * Discovery function returning information about the implementation of optional
2751 * FF-A interfaces. See section 13.3 of the FF-A v1.2 ALP1 spec.
2752 *
2753 * `function_or_feature_id` is interpreted as either a function ID or a feature
2754 * ID, depending on the value of bit 31.
2755 * When it is a feature ID, bits [30:8] MBZ and input_property MBZ.
2756 *
2757 * Returns `FFA_SUCCESS` if the interface is supported.
2758 * Returns `FFA_NOT_SUPPORTED` if the interface is not supported or the
2759 * parameters are invalid.
2760 */
2761struct ffa_value api_ffa_features(uint32_t function_or_feature_id,
2762 uint32_t input_property, struct vcpu *current)
2763{
2764 return IS_BIT_UNSET(function_or_feature_id, FFA_FEATURES_FEATURE_BIT)
2765 ? ffa_features_feature(function_or_feature_id,
2766 input_property, current)
2767 : ffa_features_function(function_or_feature_id,
2768 input_property, current);
2769}
2770
2771/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002772 * FF-A specification states that x2/w2 Must Be Zero for FFA_MSG_SEND_DIRECT_REQ
2773 * and FFA_MSG_SEND_DIRECT_RESP interfaces when used for partition messages. See
2774 * FF-A v1.2 Table 16.6: FFA_MSG_SEND_DIRECT_REQ function syntax.
J-Alves645eabe2021-02-22 16:08:27 +00002775 */
2776static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2777{
2778 return args.arg2 == 0U;
2779}
2780
2781/**
J-Alves76d99af2021-03-10 17:42:11 +00002782 * Limits size of arguments in ffa_value structure to 32-bit.
2783 */
2784static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2785{
2786 return (struct ffa_value){
2787 .func = (uint32_t)args.func,
2788 .arg1 = (uint32_t)args.arg1,
Karl Meakin06e8b732024-09-20 18:26:49 +01002789 .arg2 = (uint32_t)args.arg2,
J-Alves76d99af2021-03-10 17:42:11 +00002790 .arg3 = (uint32_t)args.arg3,
2791 .arg4 = (uint32_t)args.arg4,
2792 .arg5 = (uint32_t)args.arg5,
2793 .arg6 = (uint32_t)args.arg6,
2794 .arg7 = (uint32_t)args.arg7,
2795 };
2796}
2797
2798/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002799 * Helper to copy direct message payload, depending on SMC used, direct
Karl Meakin06e8b732024-09-20 18:26:49 +01002800 * messaging interface used, and expected registers size. Can be used for both
2801 * framework messages and standard messages.
J-Alves76d99af2021-03-10 17:42:11 +00002802 */
2803static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2804{
Karl Meakin06e8b732024-09-20 18:26:49 +01002805 switch (args.func) {
2806 case FFA_MSG_SEND_DIRECT_REQ_32:
2807 case FFA_MSG_SEND_DIRECT_RESP_32:
2808 args = api_ffa_value_copy32(args);
2809 if (!ffa_is_framework_msg(args)) {
2810 args.arg2 = 0;
2811 }
2812 break;
J-Alves76d99af2021-03-10 17:42:11 +00002813
Karl Meakin06e8b732024-09-20 18:26:49 +01002814 case FFA_MSG_SEND_DIRECT_REQ_64:
2815 case FFA_MSG_SEND_DIRECT_RESP_64:
2816 if (!ffa_is_framework_msg(args)) {
2817 args.arg2 = 0;
2818 }
2819 break;
Kathleen Capella41fea932023-06-23 17:39:28 -04002820
Karl Meakin06e8b732024-09-20 18:26:49 +01002821 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002822 args.extended_val.valid = true;
Karl Meakin06e8b732024-09-20 18:26:49 +01002823 break;
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002824
Karl Meakin06e8b732024-09-20 18:26:49 +01002825 case FFA_MSG_SEND_DIRECT_RESP2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002826 args.arg2 = 0;
2827 args.arg3 = 0;
Karl Meakin06e8b732024-09-20 18:26:49 +01002828 break;
2829 default:
2830 panic("Invalid direct message function %#x\n", args.func);
2831 break;
Kathleen Capella087e5022023-09-07 18:04:15 -04002832 }
2833
Kathleen Capella41fea932023-06-23 17:39:28 -04002834 return args;
2835}
2836
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002837/**
2838 * Return a pointer to the first UUID in `uuids` that is equal to
2839 * `target_uuid`. If `target_uuid` is 0-0-0-0, it matches any UUID.
2840 */
2841static struct ffa_uuid *ffa_uuid_find(struct ffa_uuid *uuids,
2842 size_t uuids_count,
2843 struct ffa_uuid target_uuid)
2844{
2845 if (ffa_uuid_is_null(&target_uuid)) {
2846 return &uuids[0];
2847 }
2848
2849 for (size_t i = 0; i < uuids_count; i++) {
2850 if (ffa_uuid_is_null(&uuids[i])) {
2851 break;
2852 }
2853 if (ffa_uuid_equal(&target_uuid, &uuids[i])) {
2854 return &uuids[i];
2855 }
2856 }
2857 return NULL;
2858}
2859
Kathleen Capella41fea932023-06-23 17:39:28 -04002860static bool api_ffa_dir_msg_req2_is_uuid_valid(struct vm *receiver_vm,
2861 struct ffa_value args)
2862{
2863 struct ffa_uuid target_uuid;
2864
Karl Meakin9478e322024-09-23 17:47:09 +01002865 ffa_uuid_from_u64x2(args.arg2, args.arg3, &target_uuid);
Kathleen Capella41fea932023-06-23 17:39:28 -04002866
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002867 return ffa_uuid_find(receiver_vm->uuids, PARTITION_MAX_UUIDS,
2868 target_uuid) != NULL;
J-Alves76d99af2021-03-10 17:42:11 +00002869}
2870
2871/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002872 * Send an FF-A direct message request.
Kathleen Capella41fea932023-06-23 17:39:28 -04002873 * This handler covers both FFA_MSG_SEND_DIRECT_REQ_32/64
2874 * and FFA_MSG_SEND_DIRECT_REQ2_64 (introduced in FF-A v1.2) with function-based
2875 * checks to accomodate for the difference between the ABIs.
2876 *
2877 * FFA_MSG_SEND_DIRECT_REQ2_64 works mostly the same as
2878 * FFA_MSG_SEND_DIRECT_REQ_32/64, but adds the ability to send a direct message
2879 * request to a specified UUID within a partition and the usage of an extended
2880 * range of registers (x4-x17, instead of x4-x7) to be used as part of the
2881 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002882 */
Karl Meakin13f09812024-10-28 16:33:23 +00002883struct ffa_value api_ffa_msg_send_direct_req(struct ffa_value args,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002884 struct vcpu *current,
2885 struct vcpu **next)
2886{
Karl Meakin13f09812024-10-28 16:33:23 +00002887 ffa_id_t sender_vm_id = ffa_sender(args);
2888 ffa_id_t receiver_vm_id = ffa_receiver(args);
J-Alves17228f72021-04-20 17:13:19 +01002889 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002890 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002891 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002892 struct vcpu *receiver_vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002893 struct vcpu_locked current_locked;
2894 struct vcpu_locked receiver_vcpu_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002895 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002896 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002897
Kathleen Capella41fea932023-06-23 17:39:28 -04002898 if ((args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2899 args.func == FFA_MSG_SEND_DIRECT_REQ_64) &&
Karl Meakin06e8b732024-09-20 18:26:49 +01002900 !ffa_is_framework_msg(args) &&
Kathleen Capella41fea932023-06-23 17:39:28 -04002901 !api_ffa_dir_msg_is_arg2_zero(args)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002902 dlog_verbose("Direct messaging: w2 must be zero (w2 = %#lx)\n",
2903 args.arg2);
J-Alves645eabe2021-02-22 16:08:27 +00002904 return ffa_error(FFA_INVALID_PARAMETERS);
2905 }
2906
Karl Meakin06e8b732024-09-20 18:26:49 +01002907 if (ffa_is_framework_msg(args) &&
Karl Meakinfa1dcb82025-02-10 16:47:50 +00002908 ffa_direct_msg_handle_framework_msg(args, &ret)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002909 return ret;
2910 }
2911
Karl Meakin117c8082024-12-04 16:03:28 +00002912 if (!ffa_direct_msg_is_direct_request_valid(current, sender_vm_id,
2913 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002914 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002915 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002916 }
2917
Karl Meakin117c8082024-12-04 16:03:28 +00002918 if (ffa_direct_msg_direct_request_forward(receiver_vm_id, args, &ret)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002919 dlog_verbose("Direct message request forwarded\n");
J-Alves17228f72021-04-20 17:13:19 +01002920 return ret;
2921 }
2922
J-Alvesbd32c972024-02-02 16:20:04 +00002923 ret = api_ffa_interrupt_return(0);
J-Alves17228f72021-04-20 17:13:19 +01002924
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002925 receiver_vm = vm_find(receiver_vm_id);
2926 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002927 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002928 return ffa_error(FFA_INVALID_PARAMETERS);
2929 }
2930
Kathleen Capella41fea932023-06-23 17:39:28 -04002931 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64 &&
2932 !api_ffa_dir_msg_req2_is_uuid_valid(receiver_vm, args)) {
2933 dlog_verbose("UUID unrecognized for this VM\n");
2934 return ffa_error(FFA_INVALID_PARAMETERS);
2935 }
2936
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002937 /*
J-Alves439ac972021-11-18 17:32:03 +00002938 * Check if sender supports sending direct message req, and if
2939 * receiver supports receipt of direct message requests.
2940 */
Karl Meakin117c8082024-12-04 16:03:28 +00002941 if (!ffa_direct_msg_is_direct_request_supported(
2942 current->vm, receiver_vm, args.func)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002943 dlog_verbose("Direct message request not supported\n");
J-Alves439ac972021-11-18 17:32:03 +00002944 return ffa_error(FFA_DENIED);
2945 }
2946
2947 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002948 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002949 * UP (migratable) or MP partitions with a number of vCPUs matching the
2950 * number of PEs in the system. It further states that MP partitions
2951 * accepting direct request messages cannot migrate.
2952 */
J-Alvesad6a0432021-04-09 16:06:21 +01002953 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002954 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002955 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002956 return ffa_error(FFA_INVALID_PARAMETERS);
2957 }
2958
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002959 /*
2960 * If VM must be locked, it must be done before any of its vCPUs are
2961 * locked.
2962 */
J-Alves6e2abc62021-12-02 14:58:56 +00002963 receiver_locked = vm_lock(receiver_vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002964
2965 /* Lock both vCPUs at once to avoid deadlock. */
2966 vcpus_locked = vcpu_lock_both(current, receiver_vcpu);
2967 current_locked = vcpus_locked.vcpu1;
2968 receiver_vcpu_locked = vcpus_locked.vcpu2;
2969
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002970 /*
2971 * If destination vCPU is executing or already received an
2972 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2973 * busy. There is a brief period of time where the vCPU state has
2974 * changed but regs_available is still false thus consider this case as
2975 * the vCPU not yet ready to receive a direct message request.
2976 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002977 if (is_ffa_direct_msg_request_ongoing(receiver_vcpu_locked) ||
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002978 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2979 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002980 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002981 ret = ffa_error(FFA_BUSY);
2982 goto out;
2983 }
2984
Karl Meakin117c8082024-12-04 16:03:28 +00002985 if (!ffa_cpu_cycles_check_runtime_state_transition(
J-Alves7ea3f7e2024-03-27 11:05:11 +00002986 current_locked, sender_vm_id, HF_INVALID_VM_ID,
2987 receiver_vcpu_locked, args.func, &next_state)) {
2988 ret = ffa_error(FFA_DENIED);
2989 goto out;
2990 }
2991
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002992 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2993 memory_order_relaxed)) {
2994 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04002995 dlog_verbose(
2996 "Receiver VM %#x aborted, cannot run vCPU %u\n",
2997 receiver_vcpu->vm->id,
2998 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002999 receiver_vcpu->state = VCPU_STATE_ABORTED;
3000 }
3001
3002 ret = ffa_error(FFA_ABORTED);
3003 goto out;
3004 }
3005
3006 switch (receiver_vcpu->state) {
3007 case VCPU_STATE_OFF:
3008 case VCPU_STATE_RUNNING:
3009 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003010 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003011 case VCPU_STATE_BLOCKED:
3012 case VCPU_STATE_PREEMPTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003013 ret = ffa_error(FFA_BUSY);
3014 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003015 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003016 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003017 * We expect target vCPU to be in WAITING state after either
3018 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003019 */
3020 break;
3021 }
3022
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05003023 /* Inject timer interrupt if timer has expired. */
3024 api_inject_arch_timer_interrupt(current_locked, receiver_vcpu_locked);
Madhukar Pappireddy106bfc32024-09-25 15:47:11 -05003025 timer_migrate_to_other_cpu(current->cpu, receiver_vcpu_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003026
3027 /* The receiver vCPU runs upon direct message invocation */
3028 receiver_vcpu->cpu = current->cpu;
3029 receiver_vcpu->state = VCPU_STATE_RUNNING;
3030 receiver_vcpu->regs_available = false;
Kathleen Capellae468c112023-12-13 17:56:28 -05003031 receiver_vcpu->direct_request_origin.is_ffa_req2 =
3032 (args.func == FFA_MSG_SEND_DIRECT_REQ2_64);
3033 receiver_vcpu->direct_request_origin.vm_id = sender_vm_id;
Karl Meakin06e8b732024-09-20 18:26:49 +01003034 receiver_vcpu->direct_request_origin.is_framework =
3035 ffa_is_framework_msg(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003036
J-Alves76d99af2021-03-10 17:42:11 +00003037 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003038
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003039 assert(!vm_id_is_current_world(current->vm->id) ||
3040 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003041 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003042
Karl Meakin117c8082024-12-04 16:03:28 +00003043 ffa_direct_msg_wind_call_chain_ffa_direct_req(
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07003044 current_locked, receiver_vcpu_locked, sender_vm_id);
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05003045
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003046 /* Switch to receiver vCPU targeted to by direct msg request */
3047 *next = receiver_vcpu;
3048
J-Alves6e2abc62021-12-02 14:58:56 +00003049 if (!receiver_locked.vm->el0_partition) {
3050 /*
3051 * If the scheduler in the system is giving CPU cycles to the
3052 * receiver, due to pending notifications, inject the NPI
3053 * interrupt. Following call assumes that '*next' has been set
3054 * to receiver_vcpu.
3055 */
Karl Meakin117c8082024-12-04 16:03:28 +00003056 ffa_interrupts_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003057 receiver_vcpu_locked, current_locked, receiver_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003058 }
3059
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003060 /*
3061 * Since this flow will lead to a VM switch, the return value will not
3062 * be applied to current vCPU.
3063 */
3064
3065out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003066 vcpu_unlock(&receiver_vcpu_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003067 vm_unlock(&receiver_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003068 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003069
3070 return ret;
3071}
3072
3073/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003074 * Resume the target vCPU after the current vCPU sent a direct response.
3075 * Current vCPU moves to waiting state.
3076 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003077void api_ffa_resume_direct_resp_target(struct vcpu_locked current_locked,
3078 struct vcpu **next,
J-Alves19e20cf2023-08-02 12:48:55 +01003079 ffa_id_t receiver_vm_id,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003080 struct ffa_value to_ret,
3081 bool is_nwd_call_chain)
3082{
Karl Meakinfa1dcb82025-02-10 16:47:50 +00003083 if (ffa_direct_msg_is_spmd_lp_id(receiver_vm_id) ||
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07003084 !vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003085 *next = api_switch_to_other_world(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003086 VCPU_STATE_WAITING);
3087
3088 /* End of NWd scheduled call chain. */
3089 assert(!is_nwd_call_chain ||
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003090 (current_locked.vcpu->call_chain.prev_node == NULL));
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003091 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003092 *next = api_switch_to_primary(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003093 VCPU_STATE_WAITING);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003094 } else if (vm_id_is_current_world(receiver_vm_id)) {
3095 /*
3096 * It is expected the receiver_vm_id to be from an SP, otherwise
Karl Meakin117c8082024-12-04 16:03:28 +00003097 * 'ffa_direct_msg_is_direct_response_valid' should have
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003098 * made function return error before getting to this point.
3099 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003100 *next = api_switch_to_vm(current_locked, to_ret,
3101 VCPU_STATE_WAITING, receiver_vm_id);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003102 } else {
3103 panic("Invalid direct message response invocation");
3104 }
3105}
3106
Karl Meakin50ef9112024-10-28 16:41:44 +00003107static bool api_ffa_msg_send_direct_resp_validate_args(struct ffa_value args,
3108 struct vcpu *current)
3109{
3110 ffa_id_t sender_vm_id = ffa_sender(args);
3111 ffa_id_t receiver_vm_id = ffa_receiver(args);
3112
3113 /*
3114 * If using FFA_MSG_SEND_DIRECT_RESP, the caller's
3115 * - x2 MBZ for partition messages
3116 * - x8-x17 SBZ if caller's FF-A version >= FF-A v1.2
3117 */
3118 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) {
3119 if (!ffa_is_framework_msg(args) &&
3120 !api_ffa_dir_msg_is_arg2_zero(args)) {
3121 dlog_verbose("%s: w2 Must Be Zero",
3122 ffa_func_name(args.func));
3123 return false;
3124 }
3125
3126 if (current->vm->ffa_version >= FFA_VERSION_1_2 &&
3127 !api_extended_args_are_zero(&args)) {
3128 return false;
3129 }
3130 }
3131
Karl Meakin117c8082024-12-04 16:03:28 +00003132 if (!ffa_direct_msg_is_direct_response_valid(current, sender_vm_id,
3133 receiver_vm_id)) {
Karl Meakin50ef9112024-10-28 16:41:44 +00003134 dlog_verbose("Invalid direct response call.\n");
3135 return false;
3136 }
3137
3138 return true;
3139}
3140
3141static bool api_ffa_msg_send_direct_resp_validate_ongoing_request(
3142 struct ffa_value args, struct vcpu_locked current_locked)
3143{
3144 bool req_framework =
3145 current_locked.vcpu->direct_request_origin.is_framework;
3146 bool resp_framework = ffa_is_framework_msg(args);
3147 bool received_req2 =
3148 current_locked.vcpu->direct_request_origin.is_ffa_req2;
3149
3150 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
3151 return false;
3152 }
3153
3154 if (req_framework && !resp_framework) {
3155 dlog_verbose(
3156 "Mismatch in framework message bit: request was a %s "
3157 "message, but response is a %s message\n",
3158 req_framework ? "framework" : "non-framework",
3159 resp_framework ? "framework" : "non-framework");
3160 return false;
3161 }
3162
3163 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64 && received_req2) {
3164 dlog_verbose(
3165 "FFA_MSG_SEND_DIRECT_RESP must be used with "
3166 "FFA_MSG_SEND_DIRECT_REQ\n");
3167 return false;
3168 }
3169
3170 if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64 && !received_req2) {
3171 dlog_verbose(
3172 "FFA_MSG_SEND_DIRECT_RESP2 must be used with "
3173 "FFA_MSG_SEND_DIRECT_REQ2\n");
3174 return false;
3175 }
3176
3177 return true;
3178}
3179
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003180/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003181 * Send an FF-A direct message response.
Kathleen Capella087e5022023-09-07 18:04:15 -04003182 * This handler covers both FFA_MSG_SEND_DIRECT_RESP_32/64
3183 * and FFA_MSG_SEND_DIRECT_RESP2_64 (introduced in FF-A v1.2) with
3184 * function-based checks to accomodate for the difference between the ABIs.
3185 *
3186 * FFA_MSG_SEND_DIRECT_RESP2_64 is used to respond to requests sent via
3187 * FFA_MSG_SEND_DIRECT_REQ2_64 and adds the usage of an extended range
3188 * of registers (x4-x17, instead of x4-x7) to be used as part of the
3189 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003190 */
Karl Meakin13f09812024-10-28 16:33:23 +00003191struct ffa_value api_ffa_msg_send_direct_resp(struct ffa_value args,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003192 struct vcpu *current,
3193 struct vcpu **next)
3194{
Karl Meakin13f09812024-10-28 16:33:23 +00003195 ffa_id_t sender_vm_id = ffa_sender(args);
3196 ffa_id_t receiver_vm_id = ffa_receiver(args);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003197 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003198 struct vcpu_locked next_locked = (struct vcpu_locked){
3199 .vcpu = NULL,
3200 };
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003201 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003202 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003203 struct ffa_value signal_interrupt =
3204 (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003205 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
3206 struct two_vcpu_locked vcpus_locked;
J-Alves645eabe2021-02-22 16:08:27 +00003207
Karl Meakin50ef9112024-10-28 16:41:44 +00003208 if (!api_ffa_msg_send_direct_resp_validate_args(args, current)) {
J-Alvesaa336102021-03-01 13:02:45 +00003209 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003210 }
3211
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003212 current_locked = vcpu_lock(current);
3213
Karl Meakin117c8082024-12-04 16:03:28 +00003214 if (!ffa_cpu_cycles_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003215 current_locked, sender_vm_id, receiver_vm_id, next_locked,
3216 args.func, &next_state)) {
3217 ret = ffa_error(FFA_DENIED);
3218 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05003219 }
3220
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003221 assert(!vm_id_is_current_world(current->vm->id) ||
3222 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003223
Karl Meakin50ef9112024-10-28 16:41:44 +00003224 if (!api_ffa_msg_send_direct_resp_validate_ongoing_request(
3225 args, current_locked)) {
Kathleen Capellae468c112023-12-13 17:56:28 -05003226 ret = ffa_error(FFA_DENIED);
3227 goto out;
3228 }
3229
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003230 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003231 struct interrupts *interrupts = &current->interrupts;
3232
Madhukar Pappireddy7945bb52024-08-20 15:22:41 -05003233 CHECK(current->scheduling_mode != SPMC_MODE);
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003234
Madhukar Pappireddy025a4512024-10-14 22:09:19 -05003235 plat_interrupts_set_priority_mask(
3236 current->prev_interrupt_priority);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003237 /*
3238 * A SP may be signaled a managed exit but actually not trap
3239 * the virtual interrupt, probably because it has virtual
3240 * interrupts masked, and emit direct resp. In this case the
3241 * managed exit operation is considered completed and it would
3242 * also need to clear the pending managed exit flag for the SP
3243 * vCPU.
3244 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003245 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003246
3247 if (vcpu_is_virt_interrupt_pending(interrupts,
3248 HF_MANAGED_EXIT_INTID)) {
J-Alvesb8730e92024-08-07 18:28:55 +01003249 vcpu_interrupt_clear_decrement(current_locked,
3250 HF_MANAGED_EXIT_INTID);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003251 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003252 }
3253
Kathleen Capellae468c112023-12-13 17:56:28 -05003254 /* Clear direct request origin vm_id and request type for the caller. */
3255 current->direct_request_origin.is_ffa_req2 = false;
3256 current->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003257
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003258 api_ffa_resume_direct_resp_target(current_locked, next, receiver_vm_id,
3259 to_ret, false);
3260
3261 /*
3262 * Unlock current vCPU to allow it to be locked together with next
3263 * vcpu.
3264 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003265 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003266
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003267 /* Lock both vCPUs at once to avoid deadlock. */
3268 vcpus_locked = vcpu_lock_both(current, *next);
3269 current_locked = vcpus_locked.vcpu1;
3270 next_locked = vcpus_locked.vcpu2;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003271
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05003272 /* Inject timer interrupt if timer has expired. */
3273 api_inject_arch_timer_interrupt(current_locked, next_locked);
Karl Meakin117c8082024-12-04 16:03:28 +00003274 ffa_direct_msg_unwind_call_chain_ffa_direct_resp(current_locked,
3275 next_locked);
J-Alvesbd32c972024-02-02 16:20:04 +00003276
3277 /*
3278 * Check if there is a pending secure interrupt.
3279 * If there is, return back to the caller with FFA_INTERRUPT,
3280 * and set the `next` vcpu in a preempted state.
3281 */
Karl Meakin117c8082024-12-04 16:03:28 +00003282 if (ffa_interrupts_intercept_call(current_locked, next_locked,
3283 &signal_interrupt)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003284 ret = signal_interrupt;
3285 *next = NULL;
3286 }
3287
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003288 vcpu_unlock(&next_locked);
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05003289
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003290out:
3291 vcpu_unlock(&current_locked);
3292 return ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003293}
3294
J-Alves84658fc2021-06-17 14:37:32 +01003295static bool api_memory_region_check_flags(
3296 struct ffa_memory_region *memory_region, uint32_t share_func)
3297{
3298 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00003299 case FFA_MEM_SHARE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003300 case FFA_MEM_SHARE_32:
3301 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3302 0U) {
3303 return false;
3304 }
3305 /* Intentional fall-through */
J-Alves95fbb312024-03-20 15:19:16 +00003306 case FFA_MEM_LEND_64:
J-Alves84658fc2021-06-17 14:37:32 +01003307 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00003308 case FFA_MEM_DONATE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003309 case FFA_MEM_DONATE_32: {
3310 /* Bits 31:2 Must Be Zero. */
3311 ffa_memory_receiver_flags_t to_mask =
3312 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
3313 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
3314
3315 if ((memory_region->flags & to_mask) != 0U) {
3316 return false;
3317 }
3318 break;
3319 }
3320 default:
3321 panic("Check for mem send calls only.\n");
3322 }
3323
3324 /* Last check reserved values are 0 */
3325 return true;
3326}
3327
J-Alves33c47bf2022-09-29 11:36:20 +01003328/*
3329 * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0.
3330 */
3331static void api_ffa_memory_region_v1_1_from_v1_0(
3332 struct ffa_memory_region_v1_0 *memory_region_v1_0,
3333 struct ffa_memory_region *memory_region_v1_1)
3334{
3335 memory_region_v1_1->sender = memory_region_v1_0->sender;
J-Alves00847062022-10-03 17:08:44 +01003336 memory_region_v1_1->handle = memory_region_v1_0->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003337 memory_region_v1_1->attributes =
3338 ffa_memory_attributes_extend(memory_region_v1_0->attributes);
J-Alves33c47bf2022-09-29 11:36:20 +01003339 memory_region_v1_1->flags = memory_region_v1_0->flags;
3340 memory_region_v1_1->tag = memory_region_v1_0->tag;
3341 memory_region_v1_1->memory_access_desc_size =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003342 sizeof(struct ffa_memory_access_v1_0);
J-Alves33c47bf2022-09-29 11:36:20 +01003343 memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003344 memory_region_v1_1->receivers_offset = sizeof(struct ffa_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003345
3346 /* Zero reserved fields. */
3347 for (uint32_t i = 0; i < 3U; i++) {
3348 memory_region_v1_1->reserved[i] = 0U;
3349 }
3350}
3351
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003352/**
3353 * Updates a v1.0 transaction descriptor to v1.1. This gives us the
3354 * memory_access_desc_size field we need for forwards compatability.
3355 * Copy the receivers and composite descriptors to the new struct.
3356 * We also check the fields in the v1.0 transaction descriptor and return:
3357 * - FFA_ERROR FFA_INVALID_PARAMETERS: If any of the fields are not valid
3358 * values, eg the reserved fields are not 0, receiver_count is too large or
3359 * composite offsets are not 0 for retrieve requests or in bounds for send
3360 * requests.
3361 * - FFA ERROR FFA_NOT_SUPPORTED: If an invalid ffa_version is supplied to the
3362 * function. Or the fragment length is more than a single page.
3363 * - FFA_ERROR FFA_NO_MEMORY: If we do not have enough memory for a scratch
3364 * memory transaction descriptor.
3365 * - FFA_SUCCESS: If a successful update has occured.
J-Alves33c47bf2022-09-29 11:36:20 +01003366 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003367static struct ffa_value api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3368 void *allocated, uint32_t *fragment_length, uint32_t *total_length,
Karl Meakin0e617d92024-04-05 12:55:22 +01003369 enum ffa_version ffa_version, bool send_transaction)
J-Alves33c47bf2022-09-29 11:36:20 +01003370{
3371 struct ffa_memory_region_v1_0 *memory_region_v1_0;
3372 struct ffa_memory_region *memory_region_v1_1 = NULL;
3373 struct ffa_composite_memory_region *composite_v1_0;
3374 struct ffa_composite_memory_region *composite_v1_1;
3375 size_t receivers_length;
3376 size_t space_left;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003377 size_t receivers_end;
J-Alves33c47bf2022-09-29 11:36:20 +01003378 size_t composite_offset_v1_1;
3379 size_t composite_offset_v1_0;
3380 size_t fragment_constituents_size;
3381 size_t fragment_length_v1_1;
3382
J-Alves33c47bf2022-09-29 11:36:20 +01003383 assert(fragment_length != NULL);
3384 assert(total_length != NULL);
3385
Karl Meakin0e617d92024-04-05 12:55:22 +01003386 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves33c47bf2022-09-29 11:36:20 +01003387 return (struct ffa_value){.func = FFA_SUCCESS_32};
3388 }
3389
Karl Meakin0e617d92024-04-05 12:55:22 +01003390 if (ffa_version != FFA_VERSION_1_0) {
J-Alves33c47bf2022-09-29 11:36:20 +01003391 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
3392 ffa_version);
3393 return ffa_error(FFA_NOT_SUPPORTED);
3394 }
3395
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003396 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003397 "Updating memory transaction descriptor from v1.0 to v1.1.\n");
J-Alves33c47bf2022-09-29 11:36:20 +01003398
3399 memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated;
3400
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003401 receivers_length = sizeof(struct ffa_memory_access_v1_0) *
J-Alves33c47bf2022-09-29 11:36:20 +01003402 memory_region_v1_0->receiver_count;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003403 receivers_end = sizeof(struct ffa_memory_region) + receivers_length;
J-Alves33c47bf2022-09-29 11:36:20 +01003404
3405 /*
3406 * Check the specified composite offset of v1.0 descriptor, and that all
3407 * receivers were configured with the same offset.
3408 */
3409 composite_offset_v1_0 =
3410 memory_region_v1_0->receivers[0].composite_memory_region_offset;
3411
J-Alves33c47bf2022-09-29 11:36:20 +01003412 /* Determine the composite offset for v1.1 descriptor. */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003413 if (send_transaction) {
3414 fragment_constituents_size =
3415 *fragment_length - composite_offset_v1_0 -
3416 sizeof(struct ffa_composite_memory_region);
3417 fragment_length_v1_1 =
3418 receivers_end +
3419 sizeof(struct ffa_composite_memory_region) +
3420 fragment_constituents_size;
3421 composite_offset_v1_1 = receivers_end;
3422 } else {
3423 fragment_constituents_size = 0;
3424 fragment_length_v1_1 = receivers_end;
3425 composite_offset_v1_1 = 0;
3426 }
J-Alves33c47bf2022-09-29 11:36:20 +01003427
3428 /*
3429 * Currently only support the simpler cases: memory transaction
3430 * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE.
3431 * TODO: allocate the entries needed for this fragment_length_v1_1.
3432 * - Check corner when v1.1 descriptor converted size surpasses
3433 * the size of the entry.
3434 */
3435 if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) {
3436 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003437 "Translation of FF-A v1.0 descriptors for over %lu is "
J-Alves33c47bf2022-09-29 11:36:20 +01003438 "unsupported.",
3439 MM_PPOOL_ENTRY_SIZE);
3440 return ffa_error(FFA_NOT_SUPPORTED);
3441 }
3442
3443 space_left = fragment_length_v1_1;
3444
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003445 /*
3446 * Allocate a page of memory to construct the v1.1 memory descriptor.
3447 * Earlier we checked that the fragment_length_v1_1 would not be larger
3448 * than a page.
3449 */
J-Alves33c47bf2022-09-29 11:36:20 +01003450 memory_region_v1_1 =
3451 (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
3452 if (memory_region_v1_1 == NULL) {
3453 return ffa_error(FFA_NO_MEMORY);
3454 }
3455
3456 /* Translate header from v1.0 to v1.1. */
3457 api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0,
3458 memory_region_v1_1);
3459
3460 space_left -= sizeof(struct ffa_memory_region);
3461
3462 /* Copy memory access information. */
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003463 memcpy_s((uint8_t *)memory_region_v1_1 +
3464 memory_region_v1_1->receivers_offset,
3465 space_left, memory_region_v1_0->receivers, receivers_length);
J-Alves33c47bf2022-09-29 11:36:20 +01003466
3467 /* Initialize the memory access descriptors with composite offset. */
3468 for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) {
3469 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003470 ffa_memory_region_get_receiver(memory_region_v1_1, i);
3471 assert(receiver != NULL);
J-Alves33c47bf2022-09-29 11:36:20 +01003472 receiver->composite_memory_region_offset =
3473 composite_offset_v1_1;
3474 }
3475
3476 space_left -= receivers_length;
3477
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003478 /* Composite memory descriptors to copy. */
3479 if (send_transaction) {
3480 /* Init v1.1 composite. */
3481 composite_v1_1 = (struct ffa_composite_memory_region
3482 *)((uint8_t *)memory_region_v1_1 +
3483 composite_offset_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003484
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003485 composite_v1_0 = ffa_memory_region_get_composite_v1_0(
3486 memory_region_v1_0, 0);
3487 composite_v1_1->constituent_count =
3488 composite_v1_0->constituent_count;
3489 composite_v1_1->page_count = composite_v1_0->page_count;
J-Alves33c47bf2022-09-29 11:36:20 +01003490
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003491 space_left -= sizeof(struct ffa_composite_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003492
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003493 /* Initialize v1.1 constituents. */
3494 memcpy_s(composite_v1_1->constituents, space_left,
3495 composite_v1_0->constituents,
3496 fragment_constituents_size);
J-Alves33c47bf2022-09-29 11:36:20 +01003497
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003498 space_left -= fragment_constituents_size;
3499 }
3500
J-Alves33c47bf2022-09-29 11:36:20 +01003501 assert(space_left == 0U);
3502
J-Alves33c47bf2022-09-29 11:36:20 +01003503 /*
3504 * Remove the v1.0 fragment size, and resultant size of v1.1 fragment.
3505 */
3506 *total_length = *total_length - *fragment_length + fragment_length_v1_1;
3507 *fragment_length = fragment_length_v1_1;
3508
3509 /*
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003510 * After successfully updating to v1.1 copy the descriptor to the
3511 * internal buffer given as a parameter (used to prevent TOCTOU attacks)
3512 * and free the scratch memory used to construct it.
J-Alves33c47bf2022-09-29 11:36:20 +01003513 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003514 memcpy_s(allocated, MM_PPOOL_ENTRY_SIZE, memory_region_v1_1,
3515 *fragment_length);
3516 mpool_free(&api_page_pool, memory_region_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003517
3518 return (struct ffa_value){.func = FFA_SUCCESS_32};
3519}
3520
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003521struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
3522 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003523 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003524{
3525 struct vm *from = current->vm;
3526 struct vm *to;
3527 const void *from_msg;
J-Alves33c47bf2022-09-29 11:36:20 +01003528 void *allocated_entry;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003529 struct ffa_memory_region *memory_region = NULL;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003530 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01003531 bool targets_other_world = false;
Karl Meakin0e617d92024-04-05 12:55:22 +01003532 enum ffa_version ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003533
3534 if (ipa_addr(address) != 0 || page_count != 0) {
3535 /*
3536 * Hafnium only supports passing the descriptor in the TX
3537 * mailbox.
3538 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003539 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003540 }
3541
Andrew Walbranca808b12020-05-15 17:22:28 +01003542 if (fragment_length > length) {
3543 dlog_verbose(
3544 "Fragment length %d greater than total length %d.\n",
3545 fragment_length, length);
3546 return ffa_error(FFA_INVALID_PARAMETERS);
3547 }
J-Alves33c47bf2022-09-29 11:36:20 +01003548
3549 if (fragment_length > HF_MAILBOX_SIZE ||
3550 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003551 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003552 }
3553
3554 /*
3555 * Check that the sender has configured its send buffer. If the TX
3556 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3557 * be safely accessed after releasing the lock since the TX mailbox
3558 * address can only be configured once.
3559 */
3560 sl_lock(&from->lock);
3561 from_msg = from->mailbox.send;
J-Alves33c47bf2022-09-29 11:36:20 +01003562 ffa_version = from->ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003563 sl_unlock(&from->lock);
3564
3565 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003566 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003567 }
3568
3569 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003570 * Copy the memory region descriptor to a fresh page from the memory
3571 * pool. This prevents the sender from changing it underneath us, and
3572 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003573 */
J-Alves33c47bf2022-09-29 11:36:20 +01003574 allocated_entry = mpool_alloc(&api_page_pool);
3575 if (allocated_entry == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003576 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003577 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003578 }
J-Alves33c47bf2022-09-29 11:36:20 +01003579
J-Alves8f2150d2024-03-28 16:15:56 +00003580 if (!memcpy_trapped(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg,
3581 fragment_length)) {
3582 dlog_error(
3583 "%s: Failed to copy FF-A memory region descriptor.\n",
3584 __func__);
3585 return ffa_error(FFA_ABORTED);
3586 }
J-Alves33c47bf2022-09-29 11:36:20 +01003587
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003588 if (!ffa_memory_region_sanity_check(allocated_entry, ffa_version,
3589 fragment_length, true)) {
3590 ret = ffa_error(FFA_INVALID_PARAMETERS);
3591 goto out;
3592 }
3593
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003594 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3595 allocated_entry, &fragment_length, &length, ffa_version, true);
J-Alves33c47bf2022-09-29 11:36:20 +01003596 if (ret.func != FFA_SUCCESS_32) {
3597 goto out;
3598 }
3599
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003600 memory_region = allocated_entry;
3601
J-Alves33c47bf2022-09-29 11:36:20 +01003602 if (fragment_length < sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003603 memory_region->memory_access_desc_size) {
J-Alves33c47bf2022-09-29 11:36:20 +01003604 dlog_verbose(
3605 "Initial fragment length %d smaller than header size "
Karl Meakine8937d92024-03-19 16:04:25 +00003606 "%lu.\n",
J-Alves33c47bf2022-09-29 11:36:20 +01003607 fragment_length,
3608 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003609 memory_region->memory_access_desc_size);
3610 ret = ffa_error(FFA_INVALID_PARAMETERS);
3611 goto out;
J-Alves33c47bf2022-09-29 11:36:20 +01003612 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003613
J-Alves84658fc2021-06-17 14:37:32 +01003614 if (!api_memory_region_check_flags(memory_region, share_func)) {
3615 dlog_verbose(
3616 "Memory region reserved arguments must be zero.\n");
3617 ret = ffa_error(FFA_INVALID_PARAMETERS);
3618 goto out;
3619 }
3620
J-Alves363f5722022-04-25 17:37:37 +01003621 if (memory_region->receiver_count == 0U) {
3622 dlog_verbose("Receiver count can't be 0.\n");
3623 ret = ffa_error(FFA_INVALID_PARAMETERS);
3624 goto out;
3625 }
3626
J-Alves95fbb312024-03-20 15:19:16 +00003627 if ((share_func == FFA_MEM_DONATE_32 ||
3628 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01003629 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003630 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003631 "FFA_MEM_DONATE only supports one recipient. Specified "
3632 "%u\n",
J-Alves363f5722022-04-25 17:37:37 +01003633 memory_region->receiver_count);
3634 ret = ffa_error(FFA_INVALID_PARAMETERS);
3635 goto out;
3636 }
3637
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003638 /*
3639 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01003640 * If there is a receiver from the other world, track it for later
3641 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003642 */
J-Alves363f5722022-04-25 17:37:37 +01003643 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003644 struct ffa_memory_access *receiver =
3645 ffa_memory_region_get_receiver(memory_region, i);
J-Alvesc9227c82024-04-24 21:00:58 +01003646 ffa_id_t receiver_id;
3647
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003648 assert(receiver != NULL);
J-Alvesc9227c82024-04-24 21:00:58 +01003649
3650 receiver_id = receiver->receiver_permissions.receiver;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003651
J-Alves363f5722022-04-25 17:37:37 +01003652 to = vm_find(receiver_id);
3653
J-Alves7de61af2024-03-27 10:29:46 +00003654 if ((vm_id_is_current_world(receiver_id) && to == NULL) ||
3655 to == from) {
J-Alves363f5722022-04-25 17:37:37 +01003656 dlog_verbose("%s: invalid receiver.\n", __func__);
3657 ret = ffa_error(FFA_INVALID_PARAMETERS);
3658 goto out;
3659 }
3660
Karl Meakin117c8082024-12-04 16:03:28 +00003661 if (!ffa_memory_is_send_valid(
J-Alvesc9227c82024-04-24 21:00:58 +01003662 receiver_id, from->id, share_func,
3663 memory_region->receiver_count > 1)) {
J-Alves363f5722022-04-25 17:37:37 +01003664 ret = ffa_error(FFA_DENIED);
3665 goto out;
3666 }
3667
3668 /* Capture if any of the receivers is from the other world. */
3669 if (!targets_other_world) {
3670 targets_other_world =
3671 !vm_id_is_current_world(receiver_id);
3672 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003673 }
3674
J-Alves363f5722022-04-25 17:37:37 +01003675 if (targets_other_world) {
Karl Meakin117c8082024-12-04 16:03:28 +00003676 ret = ffa_memory_other_world_mem_send(
J-Alves66652252022-07-06 09:49:51 +01003677 from, share_func, &memory_region, length,
3678 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003679 } else {
3680 struct vm_locked from_locked = vm_lock(from);
3681
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003682 ret = ffa_memory_send(from_locked, memory_region, length,
3683 fragment_length, share_func,
3684 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003685 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003686 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003687 * make sure we don't free it.
3688 */
3689 memory_region = NULL;
3690
3691 vm_unlock(&from_locked);
3692 }
3693
3694out:
3695 if (memory_region != NULL) {
3696 mpool_free(&api_page_pool, memory_region);
3697 }
3698
3699 return ret;
3700}
3701
J-Alvesc7270b72024-09-12 10:16:34 +01003702/**
3703 * An FFA_MEM_RETRIEVE_REQ from the hypervisor must specify the handle of the
3704 * memory transaction it is querying and all other fields must be 0.
3705 */
3706static bool api_ffa_memory_hypervisor_retrieve_request_validate(
3707 struct ffa_memory_region *request, enum ffa_version version)
3708{
3709 switch (version) {
3710 case FFA_VERSION_1_0: {
3711 struct ffa_memory_region_v1_0 *request_v1_0 =
3712 (struct ffa_memory_region_v1_0 *)request;
3713
3714 return request_v1_0->sender == 0U &&
3715 request_v1_0->attributes.shareability == 0U &&
3716 request_v1_0->attributes.cacheability == 0U &&
3717 request_v1_0->attributes.type == 0U &&
3718 request_v1_0->attributes.security == 0U &&
3719 request_v1_0->flags == 0U && request_v1_0->tag == 0U &&
3720 request_v1_0->receiver_count == 0U &&
Karl Meakin117c8082024-12-04 16:03:28 +00003721 ffa_memory_is_handle_allocated_by_current_world(
J-Alvesc7270b72024-09-12 10:16:34 +01003722 request_v1_0->handle);
3723 }
3724 default:
3725 return request->sender == 0U &&
3726 request->attributes.shareability == 0U &&
3727 request->attributes.cacheability == 0U &&
3728 request->attributes.type == 0U &&
3729 request->attributes.security == 0U &&
3730 request->flags == 0U && request->tag == 0U &&
3731 request->memory_access_desc_size == 0U &&
3732 request->receiver_count == 0U &&
3733 request->receivers_offset == 0U &&
Karl Meakin117c8082024-12-04 16:03:28 +00003734 ffa_memory_is_handle_allocated_by_current_world(
J-Alvesc7270b72024-09-12 10:16:34 +01003735 request->handle);
3736 }
3737}
3738
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003739struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3740 uint32_t fragment_length,
3741 ipaddr_t address, uint32_t page_count,
3742 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003743{
3744 struct vm *to = current->vm;
3745 struct vm_locked to_locked;
3746 const void *to_msg;
J-Alves00847062022-10-03 17:08:44 +01003747 void *retrieve_msg;
3748 struct ffa_memory_region *retrieve_request = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003749 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003750 struct ffa_value ret;
Karl Meakin0e617d92024-04-05 12:55:22 +01003751 enum ffa_version ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003752
3753 if (ipa_addr(address) != 0 || page_count != 0) {
3754 /*
3755 * Hafnium only supports passing the descriptor in the TX
3756 * mailbox.
3757 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003758 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003759 }
3760
Andrew Walbrana65a1322020-04-06 19:32:32 +01003761 if (fragment_length != length) {
J-Alves33c47bf2022-09-29 11:36:20 +01003762 dlog_verbose("Fragmentation not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003763 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003764 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003765
J-Alves00847062022-10-03 17:08:44 +01003766 retrieve_msg = cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003767 message_buffer_size = cpu_get_buffer_size(current->cpu);
3768 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3769 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003770 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003771 }
3772
3773 to_locked = vm_lock(to);
3774 to_msg = to->mailbox.send;
J-Alves00847062022-10-03 17:08:44 +01003775 ffa_version = to->ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003776
3777 if (to_msg == NULL) {
3778 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003779 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003780 goto out;
3781 }
3782
3783 /*
3784 * Copy the retrieve request descriptor to an internal buffer, so that
3785 * the caller can't change it underneath us.
3786 */
J-Alves8f2150d2024-03-28 16:15:56 +00003787 if (!memcpy_trapped(retrieve_msg, message_buffer_size, to_msg,
3788 length)) {
3789 dlog_error(
3790 "%s: Failed to copy FF-A retrieve request "
3791 "descriptor.\n",
3792 __func__);
3793 ret = ffa_error(FFA_ABORTED);
3794 goto out;
3795 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003796
J-Alvese8c8c2b2022-12-16 15:34:48 +00003797 if ((vm_is_mailbox_other_world_owned(to_locked) &&
Karl Meakin117c8082024-12-04 16:03:28 +00003798 !ffa_setup_acquire_receiver_rx(to_locked, &ret)) ||
J-Alvese8c8c2b2022-12-16 15:34:48 +00003799 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003800 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003801 * Can't retrieve memory information if the mailbox is
3802 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003803 */
J-Alves59ed0042022-07-28 18:26:41 +01003804 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003805 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003806 goto out;
3807 }
3808
Daniel Boulby296ee702023-11-28 13:36:55 +00003809 if (!is_ffa_hypervisor_retrieve_request(retrieve_msg)) {
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003810 if (!ffa_memory_region_sanity_check(retrieve_msg, ffa_version,
3811 fragment_length, false)) {
3812 ret = ffa_error(FFA_INVALID_PARAMETERS);
3813 goto out;
3814 }
3815 /*
3816 * If required, transform the retrieve request to FF-A v1.1.
3817 */
3818 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3819 retrieve_msg, &fragment_length, &length, ffa_version,
3820 false);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003821
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003822 if (ret.func != FFA_SUCCESS_32) {
3823 goto out;
3824 }
J-Alvesc7270b72024-09-12 10:16:34 +01003825 } else {
3826 if (!api_ffa_memory_hypervisor_retrieve_request_validate(
3827 retrieve_msg, ffa_version)) {
3828 dlog_verbose(
3829 "All fields except the handle in the "
3830 "memory access descriptor must be zero for a "
3831 "hypervisor retrieve request.\n");
3832 ret = ffa_error(FFA_INVALID_PARAMETERS);
3833 goto out;
3834 }
J-Alves00847062022-10-03 17:08:44 +01003835 }
3836
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003837 retrieve_request = retrieve_msg;
Kathleen Capella578784f2023-09-01 18:03:27 -04003838
Karl Meakin117c8082024-12-04 16:03:28 +00003839 if (ffa_memory_is_handle_allocated_by_current_world(
J-Alvesb5084cf2022-07-06 14:20:12 +01003840 retrieve_request->handle)) {
3841 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3842 &api_page_pool);
3843 } else {
Daniel Boulbyfdd59f72023-10-19 16:43:19 +01003844 dlog_error("Invalid FF-A memory handle.\n");
3845 ret = ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesb5084cf2022-07-06 14:20:12 +01003846 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003847out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003848 vm_unlock(&to_locked);
3849 return ret;
3850}
3851
J-Alvesa39a8442024-04-26 15:41:47 +01003852/**
3853 * Copies the memory relinquish descriptor from the partition's TX buffer, to
3854 * the buffer of the local CPU. Do it safely, and return error if:
3855 * - FFA_ABORTED: if the `memcpy_trapped` fails.
3856 * - FFA_INVALID_PARAMETERS: if the size of the full memory relinquish
3857 * descriptor doesn't fit the local CPU buffer.
3858 *
3859 * Returns FFA_SUCCESS if copying goes well, and sets 'out_relinquish'
3860 * to the address of the cpu buffer with the relinquish descriptor.
3861 */
3862static struct ffa_value api_get_ffa_mem_relinquish_descriptor(
3863 struct vcpu *current, const void *from_msg,
3864 struct ffa_mem_relinquish **out_relinquish)
3865{
3866 struct ffa_mem_relinquish *relinquish_request;
3867 uint32_t from_msg_size;
3868 uint32_t total_from_msg_size;
3869 uint32_t dst_size;
3870 vaddr_t dst;
3871 vaddr_t src;
3872
3873 assert(from_msg != NULL);
3874 assert(out_relinquish != NULL);
3875
3876 /*
3877 * Copy the relinquish descriptor to an internal buffer, so that the
3878 * caller can't change it underneath us.
3879 */
3880 relinquish_request =
3881 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
3882
3883 /* Set the destination for the copy. */
3884 dst = va_from_ptr(relinquish_request);
3885 src = va_from_ptr(from_msg);
3886
3887 dst_size = cpu_get_buffer_size(current->cpu);
3888
3889 /* Only copy the size to start with. */
3890 from_msg_size = sizeof(struct ffa_mem_relinquish);
3891 total_from_msg_size = from_msg_size;
3892
3893 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3894 from_msg_size)) {
3895 dlog_error(
3896 "%s: Failed to copy FF-A memory relinquish "
3897 "descriptor.\n",
3898 __func__);
3899 return ffa_error(FFA_ABORTED);
3900 }
3901
3902 if (relinquish_request->endpoint_count != 1) {
3903 dlog_error("%s: relinquish descriptor must have 1 endpoint\n",
3904 __func__);
3905 return ffa_error(FFA_INVALID_PARAMETERS);
3906 }
3907
3908 /*
3909 * Increment the `dst` to the position right after the copied header.
3910 * Increment the `src` to point at the list of endpoints.
3911 *
3912 * Calculate the new `dst_size` which is the size of the allocated cpu
3913 * buffer, minus the size of the copied memory relinquish header.
3914 *
3915 * Only after the above: determine the new `from_msg_size` in accordance
3916 * to the endpoint count.
3917 */
3918 dst = va_add(dst, from_msg_size);
3919 src = va_add(src, from_msg_size);
3920
3921 /*
3922 * Check if it is safe to copy the rest of the message.
3923 * This also serves as a santiy check to 'endpoint_count'.
3924 * The size of what is left in the descriptor, based on endpoint_count,
3925 * shall not be bigger than the size of the mailbox minus the size of
3926 * the header which was previously copied in this function.
3927 */
3928 dst_size -= from_msg_size;
3929 from_msg_size = relinquish_request->endpoint_count * sizeof(ffa_id_t);
3930 total_from_msg_size += from_msg_size;
3931
3932 if (total_from_msg_size > HF_MAILBOX_SIZE ||
3933 total_from_msg_size > dst_size) {
3934 dlog_verbose(
3935 "Relinquish message too long. Endpoint count: %u\n",
3936 relinquish_request->endpoint_count);
3937 return ffa_error(FFA_INVALID_PARAMETERS);
3938 }
3939
3940 /* Copy the remaining fragment. */
3941 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3942 from_msg_size)) {
3943 dlog_error("%s: Failed to copy FF-A relinquish request.\n",
3944 __func__);
3945 return ffa_error(FFA_ABORTED);
3946 }
3947
3948 /*
3949 * Set the output address for the relinquish descriptor to the current
3950 * cpu's buffer.
3951 */
3952 *out_relinquish = relinquish_request;
3953
3954 return (struct ffa_value){.func = FFA_SUCCESS_32};
3955}
3956
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003957struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003958{
3959 struct vm *from = current->vm;
3960 struct vm_locked from_locked;
3961 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003962 struct ffa_value ret;
J-Alvesa39a8442024-04-26 15:41:47 +01003963 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003964
3965 from_locked = vm_lock(from);
3966 from_msg = from->mailbox.send;
3967
3968 if (from_msg == NULL) {
3969 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003970 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003971 goto out;
3972 }
3973
J-Alvesa39a8442024-04-26 15:41:47 +01003974 ret = api_get_ffa_mem_relinquish_descriptor(current, from_msg,
3975 &relinquish_request);
3976
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003977 /*
J-Alvesa39a8442024-04-26 15:41:47 +01003978 * If the descriptor was safely copied, continue with the handling of
3979 * the retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003980 */
J-Alvesa39a8442024-04-26 15:41:47 +01003981 if (ret.func == FFA_SUCCESS_32) {
3982 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3983 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003984 }
J-Alves8f2150d2024-03-28 16:15:56 +00003985
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003986out:
3987 vm_unlock(&from_locked);
3988 return ret;
3989}
3990
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003991struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3992 ffa_memory_region_flags_t flags,
3993 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003994{
3995 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003996 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003997
Karl Meakin117c8082024-12-04 16:03:28 +00003998 if (ffa_memory_is_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003999 struct vm_locked to_locked = vm_lock(to);
4000
Andrew Walbranca808b12020-05-15 17:22:28 +01004001 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004002 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004003
Andrew Walbran290b0c92020-02-03 16:37:14 +00004004 vm_unlock(&to_locked);
4005 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00004006 ret = ffa_memory_other_world_mem_reclaim(to, handle, flags,
4007 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01004008 }
4009
4010 return ret;
4011}
4012
4013struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
4014 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01004015 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01004016 struct vcpu *current)
4017{
4018 struct vm *to = current->vm;
4019 struct vm_locked to_locked;
4020 struct ffa_value ret;
4021
4022 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01004023 if (vm_id_is_current_world(to->id)) {
4024 if (sender_vm_id != 0) {
4025 dlog_verbose("%s: Invalid sender.\n", __func__);
4026 return ffa_error(FFA_INVALID_PARAMETERS);
4027 }
Andrew Walbranca808b12020-05-15 17:22:28 +01004028 }
4029
4030 to_locked = vm_lock(to);
4031
J-Alves122f1a12022-12-12 15:55:42 +00004032 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004033 /*
4034 * Can't retrieve memory information if the mailbox is not
4035 * available.
4036 */
J-Alves59ed0042022-07-28 18:26:41 +01004037 dlog_verbose("%s: RX buffer not ready partition %x.\n",
4038 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01004039 ret = ffa_error(FFA_BUSY);
4040 goto out;
4041 }
4042
J-Alvesc3fd9752024-04-04 11:45:33 +01004043 /*
4044 * Whilst copying the fragments, initialize the remaining constituents
4045 * in the CPU's internal structure, and later copy from the CPU buffer
4046 * into the partition's RX buffer. In the case SPMC is doing a retrieve
4047 * request for a VM/Hypervisor in an RME enabled system, there is no
4048 * guarantee the RX buffer is in the NS PAS. Accessing the buffer with
4049 * the wrong security state attribute would then result in an GPF.
4050 * The fragment is initialized in an internal buffer, and is later
4051 * copied to the RX buffer using the 'memcpy_trapped' which allows to
4052 * smoothly terminate the operation if the access has been preempted by
4053 * a GPF exception.
4054 */
4055 ret = ffa_memory_retrieve_continue(
4056 to_locked, handle, fragment_offset, sender_vm_id,
4057 cpu_get_buffer(current->cpu), &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01004058out:
4059 vm_unlock(&to_locked);
4060 return ret;
4061}
4062
4063struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
4064 uint32_t fragment_length,
J-Alves19e20cf2023-08-02 12:48:55 +01004065 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01004066 struct vcpu *current)
4067{
4068 struct vm *from = current->vm;
4069 const void *from_msg;
4070 void *fragment_copy;
4071 struct ffa_value ret;
4072
4073 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01004074 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
4075 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01004076 return ffa_error(FFA_INVALID_PARAMETERS);
4077 }
4078
4079 /*
4080 * Check that the sender has configured its send buffer. If the TX
4081 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
4082 * be safely accessed after releasing the lock since the TX mailbox
4083 * address can only be configured once.
4084 */
4085 sl_lock(&from->lock);
4086 from_msg = from->mailbox.send;
4087 sl_unlock(&from->lock);
4088
4089 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01004090 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01004091 return ffa_error(FFA_INVALID_PARAMETERS);
4092 }
4093
4094 /*
4095 * Copy the fragment to a fresh page from the memory pool. This prevents
4096 * the sender from changing it underneath us, and also lets us keep it
4097 * around in the share state table if needed.
4098 */
4099 if (fragment_length > HF_MAILBOX_SIZE ||
4100 fragment_length > MM_PPOOL_ENTRY_SIZE) {
4101 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004102 "Fragment length %d larger than mailbox size %zu.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01004103 fragment_length, HF_MAILBOX_SIZE);
4104 return ffa_error(FFA_INVALID_PARAMETERS);
4105 }
4106 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
4107 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
4108 0) {
4109 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
4110 return ffa_error(FFA_INVALID_PARAMETERS);
4111 }
4112 fragment_copy = mpool_alloc(&api_page_pool);
4113 if (fragment_copy == NULL) {
4114 dlog_verbose("Failed to allocate fragment copy.\n");
4115 return ffa_error(FFA_NO_MEMORY);
4116 }
J-Alves8f2150d2024-03-28 16:15:56 +00004117
4118 if (!memcpy_trapped(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg,
4119 fragment_length)) {
4120 dlog_error("%s: Failed to copy fragment.\n", __func__);
4121 return ffa_error(FFA_ABORTED);
4122 }
Andrew Walbranca808b12020-05-15 17:22:28 +01004123
4124 /*
4125 * Hafnium doesn't support fragmentation of memory retrieve requests
4126 * (because it doesn't support caller-specified mappings, so a request
4127 * will never be larger than a single page), so this must be part of a
4128 * memory send (i.e. donate, lend or share) request.
4129 *
4130 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01004131 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01004132 */
Karl Meakin117c8082024-12-04 16:03:28 +00004133 if (ffa_memory_is_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004134 struct vm_locked from_locked = vm_lock(from);
4135
4136 ret = ffa_memory_send_continue(from_locked, fragment_copy,
4137 fragment_length, handle,
4138 &api_page_pool);
4139 /*
4140 * `ffa_memory_send_continue` takes ownership of the
4141 * fragment_copy, so we don't need to free it here.
4142 */
4143 vm_unlock(&from_locked);
4144 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00004145 ret = ffa_memory_other_world_mem_send_continue(
J-Alvesfdd29272022-07-19 13:16:31 +01004146 from, fragment_copy, fragment_length, handle,
4147 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00004148 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00004149
4150 return ret;
4151}
Max Shvetsov40108e72020-08-27 12:39:50 +01004152
Olivier Deprezd614d322021-06-18 15:21:00 +02004153/**
4154 * Register an entry point for a vCPU in warm boot cases.
4155 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
4156 */
Max Shvetsov40108e72020-08-27 12:39:50 +01004157struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
4158 struct vcpu *current)
4159{
4160 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01004161 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01004162
Olivier Deprezd614d322021-06-18 15:21:00 +02004163 /*
4164 * Reject if interface is not supported at this FF-A instance
4165 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
4166 */
Karl Meakin117c8082024-12-04 16:03:28 +00004167 if (!ffa_setup_is_secondary_ep_register_supported() ||
Karl Meakin82041822024-05-20 11:14:34 +01004168 vm_is_up(current->vm)) {
Olivier Deprezd614d322021-06-18 15:21:00 +02004169 return ffa_error(FFA_NOT_SUPPORTED);
4170 }
4171
4172 /*
4173 * No further check is made on the address validity
4174 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
4175 * from the VM or vCPU structure.
4176 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
4177 * For each SP [...] the Framework assumes that the same entry point
4178 * address is used for initializing any execution context during a
4179 * secondary cold boot.
4180 * If this function is invoked multiple times, then the entry point
4181 * address specified in the last valid invocation must be used by the
4182 * callee.
4183 */
Olivier Deprezb2808332023-02-02 15:25:40 +01004184 current_locked = vcpu_lock(current);
4185 if (current->rt_model != RTM_SP_INIT) {
4186 dlog_error(
4187 "FFA_SECONDARY_EP_REGISTER can only be called while "
4188 "vCPU in run-time state for initialization.\n");
4189 vcpu_unlock(&current_locked);
4190 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02004191 }
Olivier Deprezb2808332023-02-02 15:25:40 +01004192 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02004193
Olivier Deprezb2808332023-02-02 15:25:40 +01004194 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01004195 vm_locked.vm->secondary_ep = entry_point;
4196 vm_unlock(&vm_locked);
4197
Olivier Deprezb2808332023-02-02 15:25:40 +01004198 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01004199}
J-Alvesa0f317d2021-06-09 13:31:59 +01004200
J-Alves19e20cf2023-08-02 12:48:55 +01004201struct ffa_value api_ffa_notification_bitmap_create(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004202 ffa_vcpu_count_t vcpu_count,
4203 struct vcpu *current)
4204{
J-Alves7ccaccf2024-02-01 14:25:23 +00004205 const struct ffa_value ret =
Karl Meakin117c8082024-12-04 16:03:28 +00004206 ffa_notifications_is_bitmap_access_valid(current, vm_id);
J-Alves7ccaccf2024-02-01 14:25:23 +00004207
4208 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4209 dlog_verbose(
4210 "FFA_NOTIFICATION_BITMAP_CREATE to be used by "
4211 "hypervisor for valid NWd VM IDs only (%x).\n",
4212 vm_id);
4213 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004214 }
4215
Karl Meakin117c8082024-12-04 16:03:28 +00004216 return ffa_notifications_bitmap_create(vm_id, vcpu_count);
J-Alvesa0f317d2021-06-09 13:31:59 +01004217}
4218
J-Alves19e20cf2023-08-02 12:48:55 +01004219struct ffa_value api_ffa_notification_bitmap_destroy(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004220 struct vcpu *current)
4221{
J-Alves7ccaccf2024-02-01 14:25:23 +00004222 const struct ffa_value ret =
Karl Meakin117c8082024-12-04 16:03:28 +00004223 ffa_notifications_is_bitmap_access_valid(current, vm_id);
J-Alves7ccaccf2024-02-01 14:25:23 +00004224
4225 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4226 dlog_verbose(
4227 "FFA_NOTIFICATION_BITMAP_DESTROY to be used by "
4228 "hypervisor for valid NWd VM IDs only (%x).\n",
4229 vm_id);
4230 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004231 }
4232
Karl Meakin117c8082024-12-04 16:03:28 +00004233 return ffa_notifications_bitmap_destroy(vm_id);
J-Alvesa0f317d2021-06-09 13:31:59 +01004234}
J-Alvesc003a7a2021-03-18 13:06:53 +00004235
4236struct ffa_value api_ffa_notification_update_bindings(
J-Alves19e20cf2023-08-02 12:48:55 +01004237 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesc003a7a2021-03-18 13:06:53 +00004238 ffa_notifications_bitmap_t notifications, bool is_bind,
4239 struct vcpu *current)
4240{
4241 struct ffa_value ret = {.func = FFA_SUCCESS_32};
4242 struct vm_locked receiver_locked;
4243 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
J-Alves19e20cf2023-08-02 12:48:55 +01004244 const ffa_id_t id_to_update = is_bind ? sender_vm_id : HF_INVALID_VM_ID;
4245 const ffa_id_t id_to_validate =
J-Alvesc003a7a2021-03-18 13:06:53 +00004246 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01004247 const uint32_t flags_mbz =
4248 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
4249
4250 if ((flags_mbz & flags) != 0U) {
4251 return ffa_error(FFA_INVALID_PARAMETERS);
4252 }
J-Alvesc003a7a2021-03-18 13:06:53 +00004253
Karl Meakin117c8082024-12-04 16:03:28 +00004254 if (!ffa_notifications_is_bind_valid(current, sender_vm_id,
4255 receiver_vm_id)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004256 dlog_verbose("Invalid use of notifications bind interface.\n");
4257 return ffa_error(FFA_INVALID_PARAMETERS);
4258 }
4259
Karl Meakin117c8082024-12-04 16:03:28 +00004260 if (ffa_notifications_update_bindings_forward(
J-Alvesb15e9402021-09-08 11:44:42 +01004261 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
4262 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01004263 return ret;
4264 }
4265
J-Alvesc003a7a2021-03-18 13:06:53 +00004266 if (notifications == 0U) {
Karl Meakine8937d92024-03-19 16:04:25 +00004267 dlog_verbose("No notifications have been specified %lx.\n",
J-Alves1512acc2024-02-01 16:57:10 +00004268 notifications);
J-Alvesc003a7a2021-03-18 13:06:53 +00004269 return ffa_error(FFA_INVALID_PARAMETERS);
4270 }
4271
4272 /**
4273 * This check assumes receiver is the current VM, and has been enforced
Karl Meakin117c8082024-12-04 16:03:28 +00004274 * by 'ffa_notifications_is_bind_valid'.
J-Alvesc003a7a2021-03-18 13:06:53 +00004275 */
Karl Meakin117c8082024-12-04 16:03:28 +00004276 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004277
4278 if (receiver_locked.vm == NULL) {
4279 dlog_verbose("Receiver doesn't exist!\n");
J-Alves7333dcf2024-02-01 17:25:10 +00004280 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesc003a7a2021-03-18 13:06:53 +00004281 }
4282
J-Alves3e55c4d2024-10-09 14:24:07 +01004283 if (receiver_locked.vm->ffa_version < FFA_VERSION_1_1) {
4284 dlog_verbose(
4285 "%s: caller (%x) version should be GE to FF-A v1.1.\n",
4286 __func__, receiver_locked.vm->id);
4287 ret = ffa_error(FFA_NOT_SUPPORTED);
4288 goto out;
4289 }
4290
J-Alves09ff9d82021-11-02 11:55:20 +00004291 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004292 dlog_verbose("Notifications are not enabled.\n");
4293 ret = ffa_error(FFA_NOT_SUPPORTED);
4294 goto out;
4295 }
4296
4297 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
4298 vm_find(sender_vm_id) == NULL) {
4299 dlog_verbose("Sender VM does not exist!\n");
4300 ret = ffa_error(FFA_INVALID_PARAMETERS);
4301 goto out;
4302 }
4303
4304 /*
4305 * Can't bind/unbind notifications if at least one is bound to a
4306 * different sender.
4307 */
4308 if (!vm_notifications_validate_bound_sender(
J-Alves661e1b72023-08-02 13:39:40 +01004309 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_validate,
4310 notifications)) {
J-Alvesf0208142024-02-01 17:28:58 +00004311 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004312 "Sender %x not permitted to set notifications %lx to "
J-Alvesf0208142024-02-01 17:28:58 +00004313 "%x.\n",
4314 sender_vm_id, notifications, receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004315 ret = ffa_error(FFA_DENIED);
4316 goto out;
4317 }
4318
4319 /**
4320 * Check if there is a pending notification within those specified in
4321 * the bitmap.
4322 */
4323 if (vm_are_notifications_pending(receiver_locked,
J-Alves661e1b72023-08-02 13:39:40 +01004324 ffa_is_vm_id(sender_vm_id),
J-Alvesc003a7a2021-03-18 13:06:53 +00004325 notifications)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004326 dlog_verbose("Notifications within '%lx' pending.\n",
J-Alvesc003a7a2021-03-18 13:06:53 +00004327 notifications);
4328 ret = ffa_error(FFA_DENIED);
4329 goto out;
4330 }
4331
4332 vm_notifications_update_bindings(
J-Alves661e1b72023-08-02 13:39:40 +01004333 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_update,
J-Alvesc003a7a2021-03-18 13:06:53 +00004334 notifications, is_per_vcpu && is_bind);
4335
4336out:
4337 vm_unlock(&receiver_locked);
4338 return ret;
4339}
J-Alvesaa79c012021-07-09 14:29:45 +01004340
4341struct ffa_value api_ffa_notification_set(
J-Alves19e20cf2023-08-02 12:48:55 +01004342 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesaa79c012021-07-09 14:29:45 +01004343 ffa_notifications_bitmap_t notifications, struct vcpu *current)
4344{
4345 struct ffa_value ret;
4346 struct vm_locked receiver_locked;
J-Alvesaa79c012021-07-09 14:29:45 +01004347 /*
4348 * Check if is per-vCPU or global, and extracting vCPU ID according
4349 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
4350 */
4351 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
4352 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
J-Alves6b754a12024-02-14 14:26:30 +00004353 const uint32_t flags_mbz =
4354 ~(FFA_NOTIFICATIONS_FLAG_PER_VCPU |
4355 FFA_NOTIFICATIONS_FLAG_DELAY_SRI | (0xFFFFU << 16));
J-Alves41c5da32024-06-19 10:05:05 +01004356 const bool delay_sri = (FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) != 0U;
J-Alves6b754a12024-02-14 14:26:30 +00004357
4358 if ((flags_mbz & flags) != 0U) {
4359 dlog_verbose("%s: caller shouldn't set bits that MBZ.\n",
4360 __func__);
4361 return ffa_error(FFA_INVALID_PARAMETERS);
4362 }
J-Alvesaa79c012021-07-09 14:29:45 +01004363
J-Alves30ac91d2024-02-14 15:33:12 +00004364 /* Global notifications must target any vCPU. */
4365 if (!is_per_vcpu && vcpu_id != 0U) {
4366 dlog_verbose(
4367 "For global notifications vCPU ID MBZ in call to set "
4368 "notifications.\n");
4369 return ffa_error(FFA_INVALID_PARAMETERS);
4370 }
4371
Karl Meakin117c8082024-12-04 16:03:28 +00004372 if (!ffa_notifications_is_set_valid(current, sender_vm_id,
4373 receiver_vm_id)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004374 dlog_verbose("Invalid use of notifications set interface.\n");
4375 return ffa_error(FFA_INVALID_PARAMETERS);
4376 }
4377
4378 if (notifications == 0U) {
4379 dlog_verbose("No notifications have been specified.\n");
4380 return ffa_error(FFA_INVALID_PARAMETERS);
4381 }
4382
J-Alves41c5da32024-06-19 10:05:05 +01004383 /*
4384 * The 'Delay Schedule Receiver interrupt flag' only applies to the
4385 * secure virtual FF-A instance.
4386 */
4387 if (!vm_id_is_current_world(sender_vm_id) && delay_sri) {
4388 dlog_verbose(
4389 "The delay SRI flag can only be set at the secure "
4390 "virtual FF-A instance.\n");
4391 return ffa_error(FFA_INVALID_PARAMETERS);
4392 }
4393
Karl Meakin117c8082024-12-04 16:03:28 +00004394 if (ffa_notifications_set_forward(sender_vm_id, receiver_vm_id, flags,
4395 notifications, &ret)) {
J-Alvesde7bd2f2021-09-09 19:54:35 +01004396 return ret;
4397 }
4398
J-Alvesaa79c012021-07-09 14:29:45 +01004399 /*
4400 * This check assumes receiver is the current VM, and has been enforced
Karl Meakin117c8082024-12-04 16:03:28 +00004401 * by 'ffa_notifications_is_set_valid'.
J-Alvesaa79c012021-07-09 14:29:45 +01004402 */
Karl Meakin117c8082024-12-04 16:03:28 +00004403 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesaa79c012021-07-09 14:29:45 +01004404
4405 if (receiver_locked.vm == NULL) {
4406 dlog_verbose("Receiver ID is not valid.\n");
4407 return ffa_error(FFA_INVALID_PARAMETERS);
4408 }
4409
J-Alves09ff9d82021-11-02 11:55:20 +00004410 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004411 dlog_verbose("Receiver's notifications not enabled.\n");
4412 ret = ffa_error(FFA_DENIED);
4413 goto out;
4414 }
4415
4416 /*
J-Alves89c22132024-02-14 15:44:41 +00004417 * Check the if the notifications are bound as global if per-vCPU flag
4418 * is set, or if they are bound as per-vCPU and caller setting as
4419 * global. In either case, return FFA_INVALID_PARAMETERS.
4420 */
4421 if (vm_notifications_validate_binding(
4422 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4423 notifications, !is_per_vcpu)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004424 dlog_verbose("Notifications in %lx are %s\n", notifications,
J-Alves89c22132024-02-14 15:44:41 +00004425 !is_per_vcpu ? "global" : "per-vCPU");
4426 ret = ffa_error(FFA_INVALID_PARAMETERS);
4427 goto out;
4428 }
4429
4430 /*
J-Alvesaa79c012021-07-09 14:29:45 +01004431 * If notifications are not bound to the sender, they wouldn't be
4432 * enabled either for the receiver.
4433 */
4434 if (!vm_notifications_validate_binding(
J-Alves661e1b72023-08-02 13:39:40 +01004435 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4436 notifications, is_per_vcpu)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004437 dlog_verbose("Notifications bindings not valid.\n");
4438 ret = ffa_error(FFA_DENIED);
4439 goto out;
4440 }
4441
4442 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
4443 dlog_verbose("Invalid VCPU ID!\n");
4444 ret = ffa_error(FFA_INVALID_PARAMETERS);
4445 goto out;
4446 }
4447
J-Alves7461ef22021-10-18 17:21:33 +01004448 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00004449 vm_notifications_partition_set_pending(
J-Alves661e1b72023-08-02 13:39:40 +01004450 receiver_locked, ffa_is_vm_id(sender_vm_id), notifications,
J-Alves5a16c962022-03-25 12:32:51 +00004451 vcpu_id, is_per_vcpu);
4452
Karl Meakine8937d92024-03-19 16:04:25 +00004453 dlog_verbose("Set the notifications: %lx.\n", notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004454
J-Alves41c5da32024-06-19 10:05:05 +01004455 if (!delay_sri) {
J-Alves13394022021-06-30 13:48:49 +01004456 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
4457 vcpu_index(current));
Karl Meakin117c8082024-12-04 16:03:28 +00004458 ffa_notifications_sri_trigger_not_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004459 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00004460 ffa_notifications_sri_set_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004461 }
J-Alvesaa79c012021-07-09 14:29:45 +01004462
J-Alves13394022021-06-30 13:48:49 +01004463 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01004464out:
4465 vm_unlock(&receiver_locked);
4466
4467 return ret;
4468}
4469
4470static struct ffa_value api_ffa_notification_get_success_return(
4471 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
4472 ffa_notifications_bitmap_t from_framework)
4473{
4474 return (struct ffa_value){
4475 .func = FFA_SUCCESS_32,
4476 .arg1 = 0U,
4477 .arg2 = (uint32_t)from_sp,
4478 .arg3 = (uint32_t)(from_sp >> 32),
4479 .arg4 = (uint32_t)from_vm,
4480 .arg5 = (uint32_t)(from_vm >> 32),
4481 .arg6 = (uint32_t)from_framework,
4482 .arg7 = (uint32_t)(from_framework >> 32),
4483 };
4484}
4485
J-Alves19e20cf2023-08-02 12:48:55 +01004486struct ffa_value api_ffa_notification_get(ffa_id_t receiver_vm_id,
J-Alvesaa79c012021-07-09 14:29:45 +01004487 ffa_vcpu_index_t vcpu_id,
4488 uint32_t flags, struct vcpu *current)
4489{
J-Alves663682a2022-03-25 13:56:51 +00004490 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01004491 ffa_notifications_bitmap_t sp_notifications = 0;
4492 ffa_notifications_bitmap_t vm_notifications = 0;
4493 struct vm_locked receiver_locked;
4494 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01004495 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
4496 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
4497 FFA_NOTIFICATION_FLAG_BITMAP_SP |
4498 FFA_NOTIFICATION_FLAG_BITMAP_VM);
4499
4500 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
4501 if ((flags & flags_mbz) != 0U) {
4502 dlog_verbose(
4503 "Invalid flags bit(s) set in notifications get. [31:4] "
4504 "MBZ(%x)\n",
4505 flags);
4506 return ffa_error(FFA_INVALID_PARAMETERS);
4507 }
J-Alvesaa79c012021-07-09 14:29:45 +01004508
4509 /*
J-Alvesfc95a302022-04-22 14:18:23 +01004510 * Following check should capture wrong uses of the interface,
4511 * depending on whether Hafnium is SPMC or hypervisor. On the
4512 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01004513 */
Karl Meakin117c8082024-12-04 16:03:28 +00004514 if (!ffa_notifications_is_get_valid(current, receiver_vm_id, flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004515 dlog_verbose("Invalid use of notifications get interface.\n");
4516 return ffa_error(FFA_INVALID_PARAMETERS);
4517 }
4518
4519 /*
4520 * This check assumes receiver is the current VM, and has been enforced
Karl Meakinfa1dcb82025-02-10 16:47:50 +00004521 * by `ffa_notifications_is_get_valid`.
J-Alvesaa79c012021-07-09 14:29:45 +01004522 */
Karl Meakin117c8082024-12-04 16:03:28 +00004523 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesaa79c012021-07-09 14:29:45 +01004524
4525 /*
Karl Meakinfa1dcb82025-02-10 16:47:50 +00004526 * `ffa_notifications_is_get_valid` ensures following is never
J-Alvesaa79c012021-07-09 14:29:45 +01004527 * true.
4528 */
4529 CHECK(receiver_locked.vm != NULL);
4530
J-Alves60458812024-03-22 11:57:10 +00004531 if (receiver_locked.vm->vcpu_count <= vcpu_id) {
J-Alves1abb3342022-01-05 11:59:10 +00004532 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004533 "Invalid VCPU ID %u. vcpu count %u current core: "
4534 "%zu!\n",
J-Alves1abb3342022-01-05 11:59:10 +00004535 vcpu_id, receiver_locked.vm->vcpu_count,
4536 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01004537 ret = ffa_error(FFA_INVALID_PARAMETERS);
4538 goto out;
4539 }
4540
4541 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
Karl Meakin117c8082024-12-04 16:03:28 +00004542 ret = ffa_notifications_get_from_sp(receiver_locked, vcpu_id,
4543 &sp_notifications);
Karl Meakinf9c73ce2024-07-30 17:37:13 +01004544 if (ret.func == FFA_ERROR_32) {
J-Alves98ff9562021-09-09 14:39:41 +01004545 dlog_verbose("Failed to get notifications from sps.");
4546 goto out;
4547 }
J-Alvesaa79c012021-07-09 14:29:45 +01004548 }
4549
4550 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00004551 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01004552 receiver_locked, true, vcpu_id);
4553 }
4554
J-Alvesd605a092022-03-28 14:20:48 +01004555 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
4556 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
Karl Meakin117c8082024-12-04 16:03:28 +00004557 ret = ffa_notifications_get_framework_notifications(
Karl Meakinf9c73ce2024-07-30 17:37:13 +01004558 receiver_locked, &framework_notifications, flags,
4559 vcpu_id);
4560 if (ret.func == FFA_ERROR_32) {
J-Alvesd605a092022-03-28 14:20:48 +01004561 dlog_verbose(
4562 "Failed to get notifications from "
4563 "framework.\n");
4564 goto out;
4565 }
4566 }
4567
J-Alves663682a2022-03-25 13:56:51 +00004568 ret = api_ffa_notification_get_success_return(
4569 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004570
J-Alves6e2abc62021-12-02 14:58:56 +00004571 if (!receiver_locked.vm->el0_partition &&
4572 !vm_are_global_notifications_pending(receiver_locked)) {
4573 vm_notifications_set_npi_injected(receiver_locked, false);
4574 }
4575
J-Alvesaa79c012021-07-09 14:29:45 +01004576out:
4577 vm_unlock(&receiver_locked);
4578
4579 return ret;
4580}
J-Alvesc8e8a222021-06-08 17:33:52 +01004581
4582/**
4583 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
4584 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
4585 */
4586static struct ffa_value api_ffa_notification_info_get_success_return(
4587 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01004588 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01004589{
4590 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
4591
4592 /*
4593 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
4594 * hold the list of ids.
4595 */
4596 memcpy_s(&ret.arg3,
4597 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
4598 sizeof(ids[0]) * ids_count);
4599
4600 /*
4601 * According to the spec x2 should have:
4602 * - Bit flagging if there are more notifications pending;
4603 * - The total number of elements (i.e. total list size);
4604 * - The number of VCPU IDs within each VM specific list.
4605 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004606 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
4607 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
4608 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01004609
4610 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
4611 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
4612
4613 for (unsigned int i = 0; i < lists_count; i++) {
4614 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
4615 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
4616 }
4617
4618 return ret;
4619}
4620
4621struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
4622{
4623 /*
4624 * Following set of variables should be populated with the return info.
4625 * At a successfull handling of this interface, they should be used
4626 * to populate the 'ret' structure in accordance to the table 17.29
4627 * of the FF-A v1.1 Beta0 specification.
4628 */
4629 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
4630 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
4631 uint32_t lists_count = 0;
4632 uint32_t ids_count = 0;
4633 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01004634 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004635
4636 /*
4637 * This interface can only be called at NS virtual/physical FF-A
4638 * instance by the endpoint implementing the primary scheduler and the
4639 * Hypervisor/OS kernel.
4640 * In the SPM, following check passes if call has been forwarded from
4641 * the hypervisor.
4642 */
Karl Meakin5e996992024-05-20 11:27:07 +01004643
4644 if (!vm_is_primary(current->vm)) {
J-Alvesc8e8a222021-06-08 17:33:52 +01004645 dlog_verbose(
4646 "Only the receiver's scheduler can use this "
4647 "interface\n");
4648 return ffa_error(FFA_NOT_SUPPORTED);
4649 }
4650
J-Alvesca058c22021-09-10 14:02:07 +01004651 /*
4652 * Forward call to the other world, and fill the arrays used to assemble
4653 * return.
4654 */
Karl Meakin117c8082024-12-04 16:03:28 +00004655 ffa_notifications_info_get_forward(ids, &ids_count, lists_sizes,
4656 &lists_count,
4657 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
J-Alvesca058c22021-09-10 14:02:07 +01004658
4659 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
4660
J-Alvesc8e8a222021-06-08 17:33:52 +01004661 /* Get notifications' info from this world */
4662 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
4663 ++index) {
4664 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
4665
4666 list_is_full = vm_notifications_info_get(
4667 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
4668 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4669
4670 vm_unlock(&vm_locked);
4671 }
4672
4673 if (!list_is_full) {
4674 /* Grab notifications info from other world */
Karl Meakin117c8082024-12-04 16:03:28 +00004675 ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01004676 ids, &ids_count, lists_sizes, &lists_count,
4677 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4678 }
4679
4680 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01004681 dlog_verbose(
4682 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01004683 result = ffa_error(FFA_NO_DATA);
4684 } else {
4685 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01004686 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01004687 }
4688
J-Alves13394022021-06-30 13:48:49 +01004689 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004690}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004691
4692struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
4693{
4694 struct vm_locked vm_locked;
4695 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
4696 bool mode_ret = false;
4697 uint32_t mode = 0;
4698
Karl Meakin117c8082024-12-04 16:03:28 +00004699 if (!ffa_memory_is_mem_perm_get_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004700 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004701 }
4702
4703 if (!(current->vm->el0_partition)) {
4704 return ffa_error(FFA_DENIED);
4705 }
4706
4707 vm_locked = vm_lock(current->vm);
4708
4709 /*
4710 * mm_get_mode is used to check if the given base_addr page is already
4711 * mapped. If the page is unmapped, return error. If the page is mapped
4712 * appropriate attributes are returned to the caller. Note that
4713 * mm_get_mode returns true if the address is in the valid VA range as
4714 * supported by the architecture and MMU configurations, as opposed to
4715 * whether a page is mapped or not. For a page to be known as mapped,
4716 * the API must return true AND the returned mode must not have
4717 * MM_MODE_INVALID set.
4718 */
4719 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4720 va_add(base_addr, PAGE_SIZE), &mode);
4721 if (!mode_ret || (mode & MM_MODE_INVALID)) {
4722 ret = ffa_error(FFA_INVALID_PARAMETERS);
4723 goto out;
4724 }
4725
4726 /* No memory should be marked RWX */
4727 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
4728 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
4729
4730 /*
4731 * S-EL0 partitions are expected to have all their pages marked as
4732 * non-global.
4733 */
4734 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
4735 (MM_MODE_NG | MM_MODE_USER));
4736
4737 if (mode & MM_MODE_W) {
4738 /* No memory should be writeable but not readable. */
4739 CHECK(mode & MM_MODE_R);
4740 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4741 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
4742 } else if (mode & MM_MODE_R) {
4743 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4744 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
4745 if (!(mode & MM_MODE_X)) {
4746 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
4747 }
4748 }
4749out:
4750 vm_unlock(&vm_locked);
4751 return ret;
4752}
4753
4754struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
4755 uint32_t mem_perm, struct vcpu *current)
4756{
4757 struct vm_locked vm_locked;
4758 struct ffa_value ret;
4759 bool mode_ret = false;
4760 uint32_t original_mode;
4761 uint32_t new_mode;
4762 struct mpool local_page_pool;
4763
Karl Meakin117c8082024-12-04 16:03:28 +00004764 if (!ffa_memory_is_mem_perm_set_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004765 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004766 }
4767
4768 if (!(current->vm->el0_partition)) {
4769 return ffa_error(FFA_DENIED);
4770 }
4771
4772 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
4773 return ffa_error(FFA_INVALID_PARAMETERS);
4774 }
4775
4776 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
4777 (mem_perm != FFA_MEM_PERM_RX)) {
4778 return ffa_error(FFA_INVALID_PARAMETERS);
4779 }
4780
4781 /*
4782 * Create a local pool so any freed memory can't be used by another
4783 * thread. This is to ensure the original mapping can be restored if any
4784 * stage of the process fails.
4785 */
4786 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
4787
4788 vm_locked = vm_lock(current->vm);
4789
4790 /*
4791 * All regions accessible by the partition are mapped during boot. If we
4792 * cannot get a successful translation for the page range, the request
4793 * to change permissions is rejected.
4794 * mm_get_mode is used to check if the given address range is already
4795 * mapped. If the range is unmapped, return error. If the range is
4796 * mapped appropriate attributes are returned to the caller. Note that
4797 * mm_get_mode returns true if the address is in the valid VA range as
4798 * supported by the architecture and MMU configurations, as opposed to
4799 * whether a page is mapped or not. For a page to be known as mapped,
4800 * the API must return true AND the returned mode must not have
4801 * MM_MODE_INVALID set.
4802 */
4803
4804 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4805 va_add(base_addr, page_count * PAGE_SIZE),
4806 &original_mode);
4807 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
4808 ret = ffa_error(FFA_INVALID_PARAMETERS);
4809 goto out;
4810 }
4811
4812 /* Device memory cannot be marked as executable */
4813 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
4814 ret = ffa_error(FFA_INVALID_PARAMETERS);
4815 goto out;
4816 }
4817
4818 new_mode = MM_MODE_USER | MM_MODE_NG;
4819
4820 if (mem_perm == FFA_MEM_PERM_RW) {
4821 new_mode |= MM_MODE_R | MM_MODE_W;
4822 } else if (mem_perm == FFA_MEM_PERM_RX) {
4823 new_mode |= MM_MODE_R | MM_MODE_X;
4824 } else if (mem_perm == FFA_MEM_PERM_RO) {
4825 new_mode |= MM_MODE_R;
4826 }
4827
4828 /*
4829 * Safe to re-map memory, since we know the requested permissions are
4830 * valid, and the memory requested to be re-mapped is also valid.
4831 */
4832 if (!mm_identity_prepare(
4833 &vm_locked.vm->ptable, pa_from_va(base_addr),
4834 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4835 new_mode, &local_page_pool)) {
4836 /*
4837 * Defrag the table into the local page pool.
4838 * mm_identity_prepare could have allocated or freed pages to
4839 * split blocks or tables etc.
4840 */
4841 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
4842
4843 /*
4844 * Guaranteed to succeed mapping with old mode since the mapping
4845 * with old mode already existed and we have a local page pool
4846 * that should have sufficient memory to go back to the original
4847 * state.
4848 */
4849 CHECK(mm_identity_prepare(
4850 &vm_locked.vm->ptable, pa_from_va(base_addr),
4851 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4852 original_mode, &local_page_pool));
4853 mm_identity_commit(
4854 &vm_locked.vm->ptable, pa_from_va(base_addr),
4855 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4856 original_mode, &local_page_pool);
4857
4858 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
4859 ret = ffa_error(FFA_NO_MEMORY);
4860 goto out;
4861 }
4862
4863 mm_identity_commit(
4864 &vm_locked.vm->ptable, pa_from_va(base_addr),
4865 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
4866 &local_page_pool);
4867
4868 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
4869
4870out:
4871 mpool_fini(&local_page_pool);
4872 vm_unlock(&vm_locked);
4873
4874 return ret;
4875}
Maksims Svecovs71b76702022-05-20 15:32:58 +01004876
4877/**
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004878 * Send the contents of the given vCPU's log buffer to the log, preceded
Karl Meakin705b56e2024-05-22 10:04:32 +01004879 * by the VM ID and followed by a newline.
4880 */
Karl Meakin6f1f1212024-07-16 10:18:16 +01004881void api_flush_log_buffer(struct vcpu_locked *vcpu_locked)
Karl Meakin705b56e2024-05-22 10:04:32 +01004882{
Karl Meakin6f1f1212024-07-16 10:18:16 +01004883 /*
4884 * NOTE: This line is parsed by `hftest.py`.
4885 * If you change the format, make sure to update
4886 * `HFTEST_CTRL_JSON_REGEX` as well.
4887 */
4888 struct vcpu *vcpu = vcpu_locked->vcpu;
4889 struct log_buffer *buffer = &vcpu->log_buffer;
4890 ffa_id_t vm_id = vcpu->vm->id;
4891 ffa_id_t vcpu_id = vcpu_index(vcpu);
4892
Karl Meakin7de26952024-06-14 14:50:19 +01004893 buffer->chars[buffer->len] = '\0';
Olivier Deprezbd432092024-07-26 14:01:12 +02004894 dlog("[%x %u] %s\n", vm_id, vcpu_id, buffer->chars);
Karl Meakin7de26952024-06-14 14:50:19 +01004895 buffer->len = 0;
Karl Meakin705b56e2024-05-22 10:04:32 +01004896}
4897
4898/**
Karl Meakin740f74f2024-02-14 18:04:48 +00004899 * Implements FF-A v1.2 FFA_CONSOLE_LOG ABI for buffered logging.
Maksims Svecovs71b76702022-05-20 15:32:58 +01004900 */
4901struct ffa_value api_ffa_console_log(const struct ffa_value args,
4902 struct vcpu *current)
4903{
Karl Meakin740f74f2024-02-14 18:04:48 +00004904 /* Maximum number of characters is 128: 16 registers of 8 bytes each. */
4905 char chars[128] = {0};
Karl Meakin0e617d92024-04-05 12:55:22 +01004906 const bool v1_2 = current->vm->ffa_version >= FFA_VERSION_1_2;
Karl Meakin740f74f2024-02-14 18:04:48 +00004907 const bool log32 = args.func == FFA_CONSOLE_LOG_32;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004908
Karl Meakin740f74f2024-02-14 18:04:48 +00004909 /*
4910 * 32bit: always 6 registers
4911 * 64bit and less than v1.2: 6 registers
4912 * 64bit and v1.2 or greater: 16 registers
4913 */
Karl Meakin66a38bd2024-05-28 16:00:56 +01004914 /* NOLINTNEXTLINE(readability-avoid-nested-conditional-operator) */
Karl Meakin740f74f2024-02-14 18:04:48 +00004915 const size_t registers_max = log32 ? 6 : (v1_2 ? 16 : 6);
4916 const size_t chars_max =
4917 registers_max * (log32 ? sizeof(uint32_t) : sizeof(uint64_t));
4918 const size_t chars_count = args.arg1;
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004919 struct vcpu_locked vcpu_locked;
4920 struct log_buffer *log_buffer;
Karl Meakin740f74f2024-02-14 18:04:48 +00004921
4922 assert(args.func == FFA_CONSOLE_LOG_32 ||
4923 args.func == FFA_CONSOLE_LOG_64);
4924
4925 if (chars_count == 0 || chars_count > chars_max) {
Maksims Svecovs71b76702022-05-20 15:32:58 +01004926 return ffa_error(FFA_INVALID_PARAMETERS);
4927 }
4928
Karl Meakin740f74f2024-02-14 18:04:48 +00004929 if (log32) {
4930 uint32_t *registers = (uint32_t *)chars;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004931
Karl Meakin740f74f2024-02-14 18:04:48 +00004932 registers[0] = args.arg2 & 0xffffffff;
4933 registers[1] = args.arg3 & 0xffffffff;
4934 registers[2] = args.arg4 & 0xffffffff;
4935 registers[3] = args.arg5 & 0xffffffff;
4936 registers[4] = args.arg6 & 0xffffffff;
4937 registers[5] = args.arg7 & 0xffffffff;
4938 } else {
4939 uint64_t *registers = (uint64_t *)chars;
4940
4941 registers[0] = args.arg2;
4942 registers[1] = args.arg3;
4943 registers[2] = args.arg4;
4944 registers[3] = args.arg5;
4945 registers[4] = args.arg6;
4946 registers[5] = args.arg7;
4947 if (v1_2) {
4948 registers[6] = args.extended_val.arg8;
4949 registers[7] = args.extended_val.arg9;
4950 registers[8] = args.extended_val.arg10;
4951 registers[9] = args.extended_val.arg11;
4952 registers[10] = args.extended_val.arg12;
4953 registers[11] = args.extended_val.arg13;
4954 registers[12] = args.extended_val.arg14;
4955 registers[13] = args.extended_val.arg15;
4956 registers[14] = args.extended_val.arg16;
4957 registers[15] = args.extended_val.arg17;
4958 }
4959 }
4960
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004961 vcpu_locked = vcpu_lock(current);
4962 log_buffer = &current->log_buffer;
Karl Meakin7de26952024-06-14 14:50:19 +01004963
Karl Meakin740f74f2024-02-14 18:04:48 +00004964 for (size_t i = 0; i < chars_count; i++) {
4965 bool flush = false;
4966 const char c = chars[i];
4967
4968 if (c == '\n' || c == '\0') {
4969 flush = true;
4970 } else {
Karl Meakin7de26952024-06-14 14:50:19 +01004971 log_buffer->chars[log_buffer->len++] = c;
4972 flush = log_buffer->len == LOG_BUFFER_SIZE;
Karl Meakin740f74f2024-02-14 18:04:48 +00004973 }
4974
4975 if (flush) {
Karl Meakin6f1f1212024-07-16 10:18:16 +01004976 api_flush_log_buffer(&vcpu_locked);
Karl Meakin740f74f2024-02-14 18:04:48 +00004977 }
4978 }
Maksims Svecovs71b76702022-05-20 15:32:58 +01004979
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004980 vcpu_unlock(&vcpu_locked);
Maksims Svecovs71b76702022-05-20 15:32:58 +01004981 return (struct ffa_value){.func = FFA_SUCCESS_32};
4982}
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004983
4984/**
4985 * Send an IPI interrupt to a target vcpu belonging to the
4986 * sender that isn't itself.
4987 */
Daniel Boulby84d49b62024-11-04 18:25:59 +00004988int64_t api_hf_interrupt_send_ipi(uint32_t target_vcpu_id, struct vcpu *current)
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004989{
4990 struct vm *vm = current->vm;
4991 ffa_vcpu_index_t target_vcpu_index = vcpu_id_to_index(target_vcpu_id);
4992
Daniel Boulby84d49b62024-11-04 18:25:59 +00004993 if (target_vcpu_index >= vm->vcpu_count ||
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004994 target_vcpu_index == cpu_index(current->cpu)) {
4995 dlog_verbose("Invalid vCPU %d for IPI.\n", target_vcpu_id);
4996 return -1;
4997 }
4998
4999 dlog_verbose("Injecting IPI to target vCPU%d for %#x\n", target_vcpu_id,
5000 vm->id);
5001
5002 hf_ipi_send_interrupt(vm, target_vcpu_index);
5003
5004 return 0;
5005}