blob: 57543768cc4a6b3468c1ebc6411c48497c73f0be [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
Karl Meakin117c8082024-12-04 16:03:28 +00001388 ffa_cpu_cycles_init_schedule_mode_ffa_runeld_prepare(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,
1967 const void *sender_tx_buffer)
1968{
1969 uint32_t total_size;
1970
1971 if (header->offset != FFA_RXTX_HEADER_SIZE) {
1972 dlog_error("Indirect msg payload must follow the header.\n");
1973 return ffa_error(FFA_INVALID_PARAMETERS);
1974 }
1975
1976 /* Check the size of transfer. */
1977 total_size = header->offset + header->size;
1978 if ((total_size > FFA_MSG_PAYLOAD_MAX) ||
1979 (header->size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
1980 dlog_error("Message is too big.\n");
1981 return ffa_error(FFA_INVALID_PARAMETERS);
1982 }
1983
1984 /* Copy data. */
1985 if (!memcpy_trapped(receiver_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1986 sender_tx_buffer, total_size)) {
1987 dlog_error(
1988 "%s: Failed to copy message to receiver's(%x) RX "
1989 "buffer.\n",
1990 __func__, receiver_vm->id);
1991 return ffa_error(FFA_ABORTED);
1992 }
1993
1994 receiver_vm->mailbox.recv_size = total_size;
1995 receiver_vm->mailbox.recv_sender = header->sender;
1996 receiver_vm->mailbox.recv_func = FFA_MSG_SEND2_32;
1997 receiver_vm->mailbox.state = MAILBOX_STATE_FULL;
1998
1999 return (struct ffa_value){.func = FFA_SUCCESS_32};
2000}
2001
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002002/**
Federico Recanati25053ee2022-03-14 15:01:53 +01002003 * Copies data from the sender's send buffer to the recipient's receive buffer
2004 * and notifies the receiver.
2005 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002006struct ffa_value api_ffa_msg_send2(ffa_id_t sender_id, uint32_t flags,
Federico Recanati25053ee2022-03-14 15:01:53 +01002007 struct vcpu *current)
2008{
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002009 struct vm *current_vm = current->vm;
2010 struct vm *receiver_vm;
2011 struct vm_locked receiver_locked;
Federico Recanati25053ee2022-03-14 15:01:53 +01002012 struct vm_locked sender_locked;
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002013 const void *sender_tx_buffer;
Federico Recanati25053ee2022-03-14 15:01:53 +01002014 struct ffa_value ret;
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002015 ffa_id_t header_sender_id;
2016 ffa_id_t header_receiver_id;
Federico Recanati25053ee2022-03-14 15:01:53 +01002017
Karl Meakina5ea9092024-05-28 15:40:33 +01002018 alignas(8) struct ffa_partition_rxtx_header header;
2019
Federico Recanati25053ee2022-03-14 15:01:53 +01002020 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002021 if (current_vm->id != HF_HYPERVISOR_VM_ID && sender_id != 0) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002022 dlog_error("Sender VM ID must be zero.\n");
2023 return ffa_error(FFA_INVALID_PARAMETERS);
2024 }
2025
Federico Recanati25053ee2022-03-14 15:01:53 +01002026 /*
2027 * Get message sender's mailbox, which can be different to the `from` vm
2028 * when the message is forwarded.
2029 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002030 sender_id = (sender_id != 0) ? sender_id : current_vm->id;
2031 sender_locked = ffa_vm_find_locked(sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002032 if (sender_locked.vm == NULL) {
2033 dlog_error("Cannot send message from VM ID %#x, not found.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002034 sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002035 return ffa_error(FFA_DENIED);
2036 }
2037
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002038 sender_tx_buffer = sender_locked.vm->mailbox.send;
2039 if (sender_tx_buffer == NULL) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002040 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002041 sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002042 ret = ffa_error(FFA_DENIED);
2043 goto out_unlock_sender;
2044 }
2045
2046 /*
2047 * Copy message header as safety measure to avoid multiple accesses to
2048 * unsafe memory which could be 'corrupted' between safety checks and
2049 * final buffer copy.
2050 */
Karl Meakinbf580c22024-12-12 14:31:06 +00002051 if (!memcpy_trapped(&header, sizeof(header), sender_tx_buffer,
J-Alves8a5fd9d2024-04-09 11:22:49 +01002052 FFA_RXTX_HEADER_SIZE)) {
2053 dlog_error(
2054 "%s: Failed to copy message from sender's(%x) TX "
2055 "buffer.\n",
2056 __func__, sender_locked.vm->id);
2057 ret = ffa_error(FFA_ABORTED);
2058 goto out_unlock_sender;
2059 }
2060
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002061 header_sender_id = header.sender;
2062 header_receiver_id = header.receiver;
Federico Recanati25053ee2022-03-14 15:01:53 +01002063
2064 /* Ensure Sender IDs from API and from message header match. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002065 if (sender_id != header_sender_id) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002066 dlog_error(
2067 "Message sender VM ID (%#x) doesn't match header's VM "
2068 "ID (%#x).\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002069 sender_id, header_sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002070 ret = ffa_error(FFA_INVALID_PARAMETERS);
2071 goto out_unlock_sender;
2072 }
2073
2074 /* Disallow reflexive requests as this suggests an error in the VM. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002075 if (header_receiver_id == header_sender_id) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002076 dlog_error("Sender and receive VM IDs must be different.\n");
2077 ret = ffa_error(FFA_INVALID_PARAMETERS);
2078 goto out_unlock_sender;
2079 }
2080
Federico Recanati7b39ff22022-03-14 15:01:53 +01002081 /* `flags` can be set only at secure virtual FF-A instances. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002082 if (ffa_is_vm_id(header_sender_id) && flags != 0) {
Federico Recanati7b39ff22022-03-14 15:01:53 +01002083 dlog_error("flags must be zero.\n");
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002084 ret = ffa_error(FFA_INVALID_PARAMETERS);
2085 goto out_unlock_sender;
Federico Recanati7b39ff22022-03-14 15:01:53 +01002086 }
2087
Federico Recanati25053ee2022-03-14 15:01:53 +01002088 /*
2089 * Check if the message has to be forwarded to the SPMC, in
2090 * this case return, the SPMC will handle the buffer copy.
2091 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002092 if (ffa_indirect_msg_send2_forward(header_receiver_id, header_sender_id,
2093 &ret)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002094 goto out_unlock_sender;
2095 }
2096
2097 /* Ensure the receiver VM exists. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002098 receiver_locked = ffa_vm_find_locked(header_receiver_id);
2099 receiver_vm = receiver_locked.vm;
Federico Recanati25053ee2022-03-14 15:01:53 +01002100
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002101 if (receiver_vm == NULL) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002102 dlog_error("Cannot deliver message to VM %#x, not found.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002103 header_receiver_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002104 ret = ffa_error(FFA_INVALID_PARAMETERS);
2105 goto out_unlock_sender;
2106 }
2107
2108 /*
2109 * Check sender and receiver can use indirect messages.
2110 * Sender is the VM/SP who originally sent the message, not the
2111 * hypervisor possibly relaying it.
2112 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002113 if (!ffa_indirect_msg_is_supported(sender_locked, receiver_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002114 dlog_verbose("VM %#x doesn't support indirect message\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002115 header_sender_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002116 ret = ffa_error(FFA_DENIED);
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002117 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002118 }
2119
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002120 if (vm_is_mailbox_busy(receiver_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01002121 dlog_error(
2122 "Cannot deliver message to VM %#x, RX buffer not "
2123 "ready.\n",
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002124 header_receiver_id);
Federico Recanati25053ee2022-03-14 15:01:53 +01002125 ret = ffa_error(FFA_BUSY);
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002126 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002127 }
2128
Federico Recanati644f0462022-03-17 12:04:00 +01002129 /* Acquire receiver's RX buffer. */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002130 if (!ffa_setup_acquire_receiver_rx(receiver_locked, &ret)) {
2131 dlog_error("Failed to acquire RX buffer for VM %#x\n",
2132 receiver_vm->id);
2133 goto out_unlock_both;
Federico Recanati644f0462022-03-17 12:04:00 +01002134 }
2135
Federico Recanati25053ee2022-03-14 15:01:53 +01002136 /* Check the size of transfer. */
Karl Meakin355d5c22024-12-12 18:04:56 +00002137 ret = api_ffa_msg_send2_copy_data(&header, receiver_vm,
2138 sender_tx_buffer);
2139 if (ret.func != FFA_SUCCESS_32) {
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002140 goto out_unlock_both;
Federico Recanati25053ee2022-03-14 15:01:53 +01002141 }
2142
J-Alves3bb82592023-01-23 11:20:54 +00002143 /*
2144 * Set framework notifications, only if the SP has enabled
2145 * receipt of notifications.
2146 * If VMs have provided the RX buffer it is implied they already
2147 * support indirect messaging, and therefore framework notifications.
2148 */
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002149 if (ffa_is_vm_id(receiver_locked.vm->id) ||
2150 vm_are_notifications_enabled(receiver_locked.vm)) {
J-Alves3bb82592023-01-23 11:20:54 +00002151 ffa_notifications_bitmap_t rx_buffer_full =
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002152 ffa_is_vm_id(header_sender_id)
J-Alves3bb82592023-01-23 11:20:54 +00002153 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
2154 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
Federico Recanati25053ee2022-03-14 15:01:53 +01002155
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002156 vm_notifications_framework_set_pending(receiver_locked,
J-Alves3bb82592023-01-23 11:20:54 +00002157 rx_buffer_full);
2158
2159 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2160 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2161 vcpu_index(current));
Karl Meakin117c8082024-12-04 16:03:28 +00002162 ffa_notifications_sri_trigger_not_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002163 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00002164 ffa_notifications_sri_set_delayed(current->cpu);
J-Alves3bb82592023-01-23 11:20:54 +00002165 }
Federico Recanati25053ee2022-03-14 15:01:53 +01002166 }
2167
2168 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2169
Karl Meakin8e3d7bc2024-12-12 15:52:19 +00002170out_unlock_both:
2171 vm_unlock(&receiver_locked);
Federico Recanati25053ee2022-03-14 15:01:53 +01002172
2173out_unlock_sender:
2174 vm_unlock(&sender_locked);
2175
2176 return ret;
2177}
2178
2179/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002180 * Releases the caller's mailbox so that a new message can be received. The
2181 * caller must have copied out all data they wish to preserve as new messages
2182 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002183 *
2184 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002185 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2186 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002187 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2188 * - FFA_SUCCESS on success if no further action is needed.
2189 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002190 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002191 * hf_mailbox_waiter_get.
2192 */
J-Alves19e20cf2023-08-02 12:48:55 +01002193struct ffa_value api_ffa_rx_release(ffa_id_t receiver_id, struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002194{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002195 struct vm *current_vm = current->vm;
2196 struct vm *vm;
2197 struct vm_locked vm_locked;
J-Alves19e20cf2023-08-02 12:48:55 +01002198 ffa_id_t current_vm_id = current_vm->id;
2199 ffa_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002200 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002201
Federico Recanati7bef0b92022-03-17 14:56:22 +01002202 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2203 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2204 dlog_error("Invalid `receiver_id`, must be zero.\n");
2205 return ffa_error(FFA_INVALID_PARAMETERS);
2206 }
2207
2208 /*
2209 * VM ID to be released: `receiver_id` if message has been forwarded by
2210 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2211 */
2212 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2213 release_vm_id = current_vm_id;
2214 } else {
2215 release_vm_id = receiver_id;
2216 }
2217
Karl Meakin117c8082024-12-04 16:03:28 +00002218 vm_locked = ffa_vm_find_locked(release_vm_id);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002219 vm = vm_locked.vm;
2220 if (vm == NULL) {
2221 dlog_error("No buffer registered for VM ID %#x.\n",
2222 release_vm_id);
2223 return ffa_error(FFA_INVALID_PARAMETERS);
2224 }
2225
Karl Meakin117c8082024-12-04 16:03:28 +00002226 if (ffa_setup_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002227 goto out;
2228 }
2229
Karl Meakin1064a9c2024-06-04 17:20:43 +01002230 ret = api_release_mailbox(vm_locked);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002231
2232out:
2233 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002234
2235 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002236}
Andrew Walbran318f5732018-11-20 16:23:42 +00002237
2238/**
Federico Recanati644f0462022-03-17 12:04:00 +01002239 * Acquire ownership of an RX buffer before writing to it. Both
2240 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2241 * could contend for the same buffer. SPMC owns VM's RX buffer after
2242 * it's mapped in its translation regime. This ABI should be
2243 * used by the Hypervisor to get the ownership of a VM's RX buffer
2244 * from the SPMC solving the aforementioned possible contention.
2245 *
2246 * Returns:
2247 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2248 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2249 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2250 */
J-Alves19e20cf2023-08-02 12:48:55 +01002251struct ffa_value api_ffa_rx_acquire(ffa_id_t receiver_id, struct vcpu *current)
Federico Recanati644f0462022-03-17 12:04:00 +01002252{
2253 struct vm_locked receiver_locked;
2254 struct vm *receiver;
2255 struct ffa_value ret;
2256
2257 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
J-Alves661e1b72023-08-02 13:39:40 +01002258 !ffa_is_vm_id(receiver_id)) {
Federico Recanati644f0462022-03-17 12:04:00 +01002259 dlog_error(
2260 "FFA_RX_ACQUIRE not supported at this FF-A "
2261 "instance.\n");
2262 return ffa_error(FFA_NOT_SUPPORTED);
2263 }
2264
Karl Meakin117c8082024-12-04 16:03:28 +00002265 receiver_locked = ffa_vm_find_locked(receiver_id);
Federico Recanati644f0462022-03-17 12:04:00 +01002266 receiver = receiver_locked.vm;
2267
2268 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2269 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2270 receiver_id);
2271 ret = ffa_error(FFA_INVALID_PARAMETERS);
2272 goto out;
2273 }
2274
2275 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2276 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2277 ret = ffa_error(FFA_DENIED);
2278 goto out;
2279 }
2280
J-Alvese8c8c2b2022-12-16 15:34:48 +00002281 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002282
2283 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2284
2285out:
2286 vm_unlock(&receiver_locked);
2287
2288 return ret;
2289}
2290
J-Alvescc542042024-07-24 19:13:22 +01002291/*
2292 * Returns true if intid relates with either of those:
2293 * - NPI
2294 * - ME
2295 * - Virtual Timer.
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002296 * - IPI
J-Alvescc542042024-07-24 19:13:22 +01002297 *
2298 * These are VIs with no expected interrupt descriptor.
2299 */
2300static bool api_is_maintenance_virtual_interrupt(uint32_t intid)
2301{
2302 return intid == HF_NOTIFICATION_PENDING_INTID ||
2303 intid == HF_MANAGED_EXIT_INTID ||
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01002304 intid == HF_VIRTUAL_TIMER_INTID || intid == HF_IPI_INTID;
J-Alvescc542042024-07-24 19:13:22 +01002305}
2306
Federico Recanati644f0462022-03-17 12:04:00 +01002307/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002308 * Enables or disables a given interrupt ID for the calling vCPU.
2309 *
2310 * Returns 0 on success, or -1 if the intid is invalid.
2311 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002312int64_t api_interrupt_enable(uint32_t intid, bool enable,
2313 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002314{
Manish Pandey35e452f2021-02-18 21:36:34 +00002315 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002316 struct interrupts *interrupts = &current->interrupts;
J-Alvescc542042024-07-24 19:13:22 +01002317 struct interrupt_descriptor *int_desc = NULL;
2318 struct vm *vm = current->vm;
2319 struct vm_locked vm_locked;
2320
2321 int64_t ret = -1;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002322
Andrew Walbran318f5732018-11-20 16:23:42 +00002323 if (intid >= HF_NUM_INTIDS) {
2324 return -1;
2325 }
2326
J-Alvescc542042024-07-24 19:13:22 +01002327 vm_locked = vm_lock(vm);
Manish Pandey35e452f2021-02-18 21:36:34 +00002328 current_locked = vcpu_lock(current);
J-Alvescc542042024-07-24 19:13:22 +01002329
2330 int_desc = vm_interrupt_set_enable(vm_locked, intid, enable);
2331
2332 if (!api_is_maintenance_virtual_interrupt(intid)) {
2333 if (int_desc == NULL) {
2334 dlog_error("%s: invalid interrupt ID.\n", __func__);
2335 goto out;
2336 }
2337
2338 plat_interrupts_configure_interrupt(*int_desc);
2339 }
2340
Andrew Walbran318f5732018-11-20 16:23:42 +00002341 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002342 /*
2343 * If it is pending and was not enabled before, increment the
2344 * count.
2345 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002346 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2347 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2348 vcpu_interrupt_count_increment(current_locked,
2349 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002350 }
J-Alvescc542042024-07-24 19:13:22 +01002351
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002352 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2353 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002354 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002355 /*
2356 * If it is pending and was enabled before, decrement the count.
2357 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002358 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2359 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2360 vcpu_interrupt_count_decrement(current_locked,
2361 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002362 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002363 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2364 vcpu_virt_interrupt_set_type(interrupts, intid,
2365 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002366 }
2367
J-Alvescc542042024-07-24 19:13:22 +01002368 ret = 0;
2369
2370out:
2371 vm_unlock(&vm_locked);
Manish Pandey35e452f2021-02-18 21:36:34 +00002372 vcpu_unlock(&current_locked);
J-Alvescc542042024-07-24 19:13:22 +01002373
2374 return ret;
Andrew Walbran318f5732018-11-20 16:23:42 +00002375}
2376
2377/**
2378 * Returns the ID of the next pending interrupt for the calling vCPU, and
2379 * acknowledges it (i.e. marks it as no longer pending). Returns
2380 * HF_INVALID_INTID if there are no pending interrupts.
2381 */
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002382uint32_t api_interrupt_get(struct vcpu_locked current_locked)
Andrew Walbran318f5732018-11-20 16:23:42 +00002383{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002384 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002385 uint32_t first_interrupt = HF_INVALID_INTID;
Madhukar Pappireddyc64d0642024-08-07 16:55:46 -05002386 struct interrupts *interrupts = &current_locked.vcpu->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002387
2388 /*
2389 * Find the first enabled and pending interrupt ID, return it, and
2390 * deactivate it.
2391 */
Andrew Walbran318f5732018-11-20 16:23:42 +00002392 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2393 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002394 interrupts->interrupt_enabled.bitmap[i] &
2395 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002396
Andrew Walbran318f5732018-11-20 16:23:42 +00002397 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002398 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002399
2400 first_interrupt =
2401 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002402
Andrew Walbran3d84a262018-12-13 14:41:19 +00002403 /*
2404 * Mark it as no longer pending and decrement the count.
2405 */
J-Alvesb8730e92024-08-07 18:28:55 +01002406 vcpu_interrupt_clear_decrement(current_locked,
2407 first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002408 break;
2409 }
2410 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002411
Andrew Walbran318f5732018-11-20 16:23:42 +00002412 return first_interrupt;
2413}
2414
2415/**
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002416 * Negotiate the FF-A version to be used for this FF-A instance.
2417 * See section 13.2 of the FF-A v1.2 ALP1 spec.
2418 *
2419 * Returns Hafnium's version number (`FFA_VERSION_COMPILED`) on success.
2420 * Returns `FFA_NOT_SUPPORTED` on error:
2421 * - The version is invalid (highest bit set).
2422 * - The requested version is incompatible.
2423 * - The version has already been negotiated and cannot be changed.
2424 */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002425struct ffa_value api_ffa_version(struct vcpu *current,
Karl Meakin0e617d92024-04-05 12:55:22 +01002426 enum ffa_version requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002427{
Karl Meakin0e617d92024-04-05 12:55:22 +01002428 static_assert(sizeof(enum ffa_version) == 4,
2429 "enum ffa_version must be 4 bytes wide");
2430
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002431 const struct ffa_value error = {.func = (uint32_t)FFA_NOT_SUPPORTED};
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002432 struct vm_locked current_vm_locked;
2433
Karl Meakin0e617d92024-04-05 12:55:22 +01002434 if (!ffa_version_is_valid(requested_version)) {
2435 dlog_error(
2436 "FFA_VERSION: requested version %#x is invalid "
2437 "(highest bit must be zero)\n",
2438 requested_version);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002439 return error;
Andrew Walbran9fd29072020-04-22 12:12:14 +01002440 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002441
Karl Meakin0e617d92024-04-05 12:55:22 +01002442 if (!ffa_versions_are_compatible(requested_version,
2443 FFA_VERSION_COMPILED)) {
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002444 dlog_error(
2445 "FFA_VERSION: requested version v%u.%u is not "
2446 "compatible with v%u.%u\n",
2447 ffa_version_get_major(requested_version),
2448 ffa_version_get_minor(requested_version),
2449 ffa_version_get_major(FFA_VERSION_COMPILED),
2450 ffa_version_get_minor(FFA_VERSION_COMPILED));
2451 return error;
J-Alves3cc9d6f2023-07-20 16:46:15 +01002452 }
2453
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002454 current_vm_locked = vm_lock(current->vm);
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002455
2456 if (current_vm_locked.vm->ffa_version_negotiated &&
2457 requested_version != current_vm_locked.vm->ffa_version) {
2458 vm_unlock(&current_vm_locked);
2459 dlog_error(
2460 "FFA_VERSION: Cannot change FF-A version after other "
2461 "FF-A calls have been made\n");
2462 return error;
2463 }
2464
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002465 current_vm_locked.vm->ffa_version = requested_version;
2466 vm_unlock(&current_vm_locked);
2467
Karl Meakin6eeec8e2024-03-07 18:07:20 +00002468 return (struct ffa_value){.func = (uint32_t)FFA_VERSION_COMPILED};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002469}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002470
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002471/**
Karl Meakin49ec1e42024-05-10 13:08:24 +01002472 * Helper for success return of FFA_FEATURES.
J-Alves6f72ca82021-11-01 12:34:58 +00002473 */
2474struct ffa_value api_ffa_feature_success(uint32_t arg2)
2475{
2476 return (struct ffa_value){
Karl Meakin49ec1e42024-05-10 13:08:24 +01002477 .func = FFA_SUCCESS_32,
2478 .arg1 = 0U,
2479 .arg2 = arg2,
2480 };
J-Alves6f72ca82021-11-01 12:34:58 +00002481}
2482
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002483static struct ffa_value ffa_features_function(uint32_t func,
2484 uint32_t input_property,
2485 struct vcpu *current)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002486{
Karl Meakin0e617d92024-04-05 12:55:22 +01002487 const enum ffa_version ffa_version = current->vm->ffa_version;
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002488 const bool el0_partition = current->vm->el0_partition;
2489
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002490 switch (func) {
J-Alves6f72ca82021-11-01 12:34:58 +00002491 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002492 case FFA_ERROR_32:
2493 case FFA_SUCCESS_32:
2494 case FFA_INTERRUPT_32:
2495 case FFA_VERSION_32:
2496 case FFA_FEATURES_32:
2497 case FFA_RX_RELEASE_32:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002498 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002499 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002500 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002501 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002502 case FFA_RUN_32:
J-Alves95fbb312024-03-20 15:19:16 +00002503 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002504 case FFA_MEM_DONATE_32:
2505 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00002506 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002507 case FFA_MEM_SHARE_32:
J-Alves95fbb312024-03-20 15:19:16 +00002508 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002509 case FFA_MEM_RETRIEVE_RESP_32:
2510 case FFA_MEM_RELINQUISH_32:
2511 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002512 case FFA_MEM_FRAG_RX_32:
2513 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002514 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002515 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002516 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002517 case FFA_MSG_SEND_DIRECT_REQ_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002518
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002519 /* FF-A v1.1 features. */
2520 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002521 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2522 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2523 case FFA_NOTIFICATION_BIND_32:
2524 case FFA_NOTIFICATION_UNBIND_32:
2525 case FFA_NOTIFICATION_SET_32:
2526 case FFA_NOTIFICATION_GET_32:
2527 case FFA_NOTIFICATION_INFO_GET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002528 case FFA_MSG_SEND2_32:
Karl Meakin0e617d92024-04-05 12:55:22 +01002529 if (FFA_VERSION_1_1 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002530 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002531 }
2532
Kathleen Capellae4fe2962023-09-01 17:08:47 -04002533 /* FF-A v1.2 features. */
2534 case FFA_CONSOLE_LOG_32:
2535 case FFA_CONSOLE_LOG_64:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002536 case FFA_PARTITION_INFO_GET_REGS_64:
Kathleen Capella41fea932023-06-23 17:39:28 -04002537 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002538 case FFA_MSG_SEND_DIRECT_RESP2_64:
Karl Meakin0e617d92024-04-05 12:55:22 +01002539 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002540 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002541 }
2542
Karl Meakin49ec1e42024-05-10 13:08:24 +01002543 return api_ffa_feature_success(0);
2544
Karl Meakin5a222642024-06-20 10:23:37 +01002545 /* These functions are only supported on S-EL0 partitions. */
2546 case FFA_MEM_PERM_GET_32:
2547 case FFA_MEM_PERM_SET_32:
2548 case FFA_MEM_PERM_GET_64:
2549 case FFA_MEM_PERM_SET_64:
2550 if (!(vm_id_is_current_world(current->vm->id) &&
2551 el0_partition)) {
2552 dlog_verbose(
2553 "FFA_FEATURE: %s is only supported on S-EL0 "
2554 "partitions\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002555 ffa_func_name(func));
Karl Meakin5a222642024-06-20 10:23:37 +01002556 return ffa_error(FFA_NOT_SUPPORTED);
2557 }
2558 return api_ffa_feature_success(0);
2559
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002560 case FFA_SECONDARY_EP_REGISTER_64:
2561 if (FFA_VERSION_COMPILED < FFA_VERSION_1_1) {
2562 return ffa_error(FFA_NOT_SUPPORTED);
2563 }
2564
2565 if (!(vm_id_is_current_world(current->vm->id) &&
2566 current->vm->vcpu_count > 1)) {
2567 dlog_verbose(
2568 "FFA_FEATURE: %s is only supported on SPs with "
2569 "more than 1 vCPU\n",
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002570 ffa_func_name(func));
Karl Meakin1a8c0cd2024-06-20 10:26:12 +01002571 return ffa_error(FFA_NOT_SUPPORTED);
2572 }
2573 return api_ffa_feature_success(0);
2574
Karl Meakin650cb142024-06-20 10:31:57 +01002575 /*
2576 * This function is restricted to the secure virtual FF-A instance (i.e.
2577 * only report success to SPs).
2578 */
2579 case FFA_YIELD_32:
2580 if (!vm_id_is_current_world(current->vm->id)) {
2581 dlog_verbose(
2582 "FFA_FEATURES: %s is only supported at secure "
2583 "virtual FF-A instance\n",
2584 ffa_func_name(FFA_YIELD_32));
2585 return ffa_error(FFA_NOT_SUPPORTED);
2586 }
2587 return api_ffa_feature_success(0);
2588
Karl Meakin0fd67292024-02-06 17:33:05 +00002589 case FFA_RXTX_MAP_64: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002590 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002591 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002592 }
2593
Karl Meakin0fd67292024-02-06 17:33:05 +00002594 uint32_t arg2 = 0;
2595 struct ffa_features_rxtx_map_params params = {
2596 .min_buf_size = FFA_RXTX_MAP_MIN_BUF_4K,
2597 .mbz = 0,
2598 .max_buf_size =
Karl Meakin0e617d92024-04-05 12:55:22 +01002599 (ffa_version >= FFA_VERSION_1_2)
Karl Meakin0fd67292024-02-06 17:33:05 +00002600 ? FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT
2601 : 0,
2602 };
2603
2604 memcpy_s(&arg2, sizeof(arg2), &params, sizeof(params));
Karl Meakin49ec1e42024-05-10 13:08:24 +01002605 return api_ffa_feature_success(arg2);
Karl Meakin0fd67292024-02-06 17:33:05 +00002606 }
2607
J-Alves95fbb312024-03-20 15:19:16 +00002608 case FFA_MEM_RETRIEVE_REQ_64:
Karl Meakin49ec1e42024-05-10 13:08:24 +01002609 case FFA_MEM_RETRIEVE_REQ_32: {
Karl Meakin0e617d92024-04-05 12:55:22 +01002610 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002611 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002612 }
2613
Karl Meakin49ec1e42024-05-10 13:08:24 +01002614 if (ANY_BITS_SET(input_property,
2615 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2616 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT) ||
2617 IS_BIT_SET(input_property,
2618 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT)) {
2619 dlog_warning(
2620 "FFA_FEATURES: Bits[%u:%u] and Bit[%u] of "
2621 "input_property should be 0 (input_property = "
2622 "%#x)\n",
2623 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT,
2624 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT,
2625 FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT,
Karl Meakin34b8ae92023-01-13 13:33:07 +00002626 input_property);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002627 }
2628
Karl Meakin0e617d92024-04-05 12:55:22 +01002629 if (ffa_version >= FFA_VERSION_1_1 &&
2630 (input_property &
2631 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
Karl Meakin4e8e4792024-07-05 16:28:42 +01002632 dlog_verbose(
2633 "FFA_FEATURES: NS bit support must be 1\n");
Karl Meakin6fd6c1d2024-05-13 12:09:13 +01002634 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin34b8ae92023-01-13 13:33:07 +00002635 }
2636
2637 return api_ffa_feature_success(
2638 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
Karl Meakin49ec1e42024-05-10 13:08:24 +01002639 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT |
Karl Meakin34b8ae92023-01-13 13:33:07 +00002640 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
Karl Meakin49ec1e42024-05-10 13:08:24 +01002641 }
J-Alves6f72ca82021-11-01 12:34:58 +00002642
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002643 default:
2644 return ffa_error(FFA_NOT_SUPPORTED);
2645 }
2646}
2647
2648static struct ffa_value ffa_features_feature(enum ffa_feature_id feature,
2649 uint32_t input_property,
2650 struct vcpu *current)
2651{
2652 const bool el0_partition = current->vm->el0_partition;
2653
2654 if (ANY_BITS_SET(feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2655 FFA_FEATURES_FEATURE_MBZ_LO_BIT)) {
2656 dlog_verbose(
2657 "FFA_FEATURES: feature ID %#x is invalid (bits [%u:%u] "
2658 "must be zero)\n",
2659 feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT,
2660 FFA_FEATURES_FEATURE_MBZ_LO_BIT);
2661 return ffa_error(FFA_NOT_SUPPORTED);
2662 }
2663 if (input_property != 0) {
2664 dlog_verbose(
2665 "FFA_FEATURES: input_property must be 0 "
2666 "(input_property = %#x)\n",
2667 input_property);
2668 return ffa_error(FFA_NOT_SUPPORTED);
2669 }
2670
2671 switch (feature) {
J-Alves6f72ca82021-11-01 12:34:58 +00002672 /* Check support of a feature provided respective feature ID. */
Karl Meakin17c9ad32024-04-12 16:41:00 +01002673
2674 /*
2675 * For NPI and MEI, report the IDs as supported only to partitions at
2676 * the virtual FF-A instances.
2677 */
J-Alves6f72ca82021-11-01 12:34:58 +00002678 case FFA_FEATURE_NPI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002679 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002680 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002681 }
2682
Karl Meakinf1ed5f12024-02-22 15:57:36 +00002683 if (el0_partition) {
2684 return ffa_error(FFA_NOT_SUPPORTED);
2685 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002686 if (!vm_id_is_current_world(current->vm->id)) {
2687 return ffa_error(FFA_NOT_SUPPORTED);
2688 }
J-Alves6f72ca82021-11-01 12:34:58 +00002689 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
Karl Meakin17c9ad32024-04-12 16:41:00 +01002690
2691 case FFA_FEATURE_MEI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002692 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002693 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002694 }
J-Alvesc4d9ae82024-05-14 10:15:03 +01002695 if (el0_partition) {
2696 return ffa_error(FFA_NOT_SUPPORTED);
2697 }
Karl Meakin17c9ad32024-04-12 16:41:00 +01002698 if (!vm_id_is_current_world(current->vm->id)) {
2699 return ffa_error(FFA_NOT_SUPPORTED);
2700 }
2701 return api_ffa_feature_success(HF_MANAGED_EXIT_INTID);
2702
J-Alves6f72ca82021-11-01 12:34:58 +00002703 case FFA_FEATURE_SRI:
Karl Meakin0e617d92024-04-05 12:55:22 +01002704 if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) {
Karl Meakin4271ff92024-05-13 12:33:15 +01002705 return ffa_error(FFA_NOT_SUPPORTED);
Karl Meakin0e617d92024-04-05 12:55:22 +01002706 }
2707
Karl Meakincb6642c2024-02-27 14:43:45 +00002708 if (!ffa_is_vm_id(current->vm->id)) {
2709 return ffa_error(FFA_NOT_SUPPORTED);
2710 }
J-Alves6f72ca82021-11-01 12:34:58 +00002711 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
Karl Meakin0e617d92024-04-05 12:55:22 +01002712
J-Alves6f72ca82021-11-01 12:34:58 +00002713 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002714 default:
Karl Meakin4271ff92024-05-13 12:33:15 +01002715 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002716 }
2717}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002718
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002719/**
Karl Meakin9c5b1d32024-06-24 12:10:36 +01002720 * Discovery function returning information about the implementation of optional
2721 * FF-A interfaces. See section 13.3 of the FF-A v1.2 ALP1 spec.
2722 *
2723 * `function_or_feature_id` is interpreted as either a function ID or a feature
2724 * ID, depending on the value of bit 31.
2725 * When it is a feature ID, bits [30:8] MBZ and input_property MBZ.
2726 *
2727 * Returns `FFA_SUCCESS` if the interface is supported.
2728 * Returns `FFA_NOT_SUPPORTED` if the interface is not supported or the
2729 * parameters are invalid.
2730 */
2731struct ffa_value api_ffa_features(uint32_t function_or_feature_id,
2732 uint32_t input_property, struct vcpu *current)
2733{
2734 return IS_BIT_UNSET(function_or_feature_id, FFA_FEATURES_FEATURE_BIT)
2735 ? ffa_features_feature(function_or_feature_id,
2736 input_property, current)
2737 : ffa_features_function(function_or_feature_id,
2738 input_property, current);
2739}
2740
2741/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002742 * FF-A specification states that x2/w2 Must Be Zero for FFA_MSG_SEND_DIRECT_REQ
2743 * and FFA_MSG_SEND_DIRECT_RESP interfaces when used for partition messages. See
2744 * FF-A v1.2 Table 16.6: FFA_MSG_SEND_DIRECT_REQ function syntax.
J-Alves645eabe2021-02-22 16:08:27 +00002745 */
2746static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2747{
2748 return args.arg2 == 0U;
2749}
2750
2751/**
J-Alves76d99af2021-03-10 17:42:11 +00002752 * Limits size of arguments in ffa_value structure to 32-bit.
2753 */
2754static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2755{
2756 return (struct ffa_value){
2757 .func = (uint32_t)args.func,
2758 .arg1 = (uint32_t)args.arg1,
Karl Meakin06e8b732024-09-20 18:26:49 +01002759 .arg2 = (uint32_t)args.arg2,
J-Alves76d99af2021-03-10 17:42:11 +00002760 .arg3 = (uint32_t)args.arg3,
2761 .arg4 = (uint32_t)args.arg4,
2762 .arg5 = (uint32_t)args.arg5,
2763 .arg6 = (uint32_t)args.arg6,
2764 .arg7 = (uint32_t)args.arg7,
2765 };
2766}
2767
2768/**
Kathleen Capella41fea932023-06-23 17:39:28 -04002769 * Helper to copy direct message payload, depending on SMC used, direct
Karl Meakin06e8b732024-09-20 18:26:49 +01002770 * messaging interface used, and expected registers size. Can be used for both
2771 * framework messages and standard messages.
J-Alves76d99af2021-03-10 17:42:11 +00002772 */
2773static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2774{
Karl Meakin06e8b732024-09-20 18:26:49 +01002775 switch (args.func) {
2776 case FFA_MSG_SEND_DIRECT_REQ_32:
2777 case FFA_MSG_SEND_DIRECT_RESP_32:
2778 args = api_ffa_value_copy32(args);
2779 if (!ffa_is_framework_msg(args)) {
2780 args.arg2 = 0;
2781 }
2782 break;
J-Alves76d99af2021-03-10 17:42:11 +00002783
Karl Meakin06e8b732024-09-20 18:26:49 +01002784 case FFA_MSG_SEND_DIRECT_REQ_64:
2785 case FFA_MSG_SEND_DIRECT_RESP_64:
2786 if (!ffa_is_framework_msg(args)) {
2787 args.arg2 = 0;
2788 }
2789 break;
Kathleen Capella41fea932023-06-23 17:39:28 -04002790
Karl Meakin06e8b732024-09-20 18:26:49 +01002791 case FFA_MSG_SEND_DIRECT_REQ2_64:
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002792 args.extended_val.valid = true;
Karl Meakin06e8b732024-09-20 18:26:49 +01002793 break;
Kathleen Capelladb6a08b2023-10-04 13:42:39 -04002794
Karl Meakin06e8b732024-09-20 18:26:49 +01002795 case FFA_MSG_SEND_DIRECT_RESP2_64:
Kathleen Capella087e5022023-09-07 18:04:15 -04002796 args.arg2 = 0;
2797 args.arg3 = 0;
Karl Meakin06e8b732024-09-20 18:26:49 +01002798 break;
2799 default:
2800 panic("Invalid direct message function %#x\n", args.func);
2801 break;
Kathleen Capella087e5022023-09-07 18:04:15 -04002802 }
2803
Kathleen Capella41fea932023-06-23 17:39:28 -04002804 return args;
2805}
2806
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002807/**
2808 * Return a pointer to the first UUID in `uuids` that is equal to
2809 * `target_uuid`. If `target_uuid` is 0-0-0-0, it matches any UUID.
2810 */
2811static struct ffa_uuid *ffa_uuid_find(struct ffa_uuid *uuids,
2812 size_t uuids_count,
2813 struct ffa_uuid target_uuid)
2814{
2815 if (ffa_uuid_is_null(&target_uuid)) {
2816 return &uuids[0];
2817 }
2818
2819 for (size_t i = 0; i < uuids_count; i++) {
2820 if (ffa_uuid_is_null(&uuids[i])) {
2821 break;
2822 }
2823 if (ffa_uuid_equal(&target_uuid, &uuids[i])) {
2824 return &uuids[i];
2825 }
2826 }
2827 return NULL;
2828}
2829
Kathleen Capella41fea932023-06-23 17:39:28 -04002830static bool api_ffa_dir_msg_req2_is_uuid_valid(struct vm *receiver_vm,
2831 struct ffa_value args)
2832{
2833 struct ffa_uuid target_uuid;
2834
Karl Meakin9478e322024-09-23 17:47:09 +01002835 ffa_uuid_from_u64x2(args.arg2, args.arg3, &target_uuid);
Kathleen Capella41fea932023-06-23 17:39:28 -04002836
Karl Meakinfb9c2a22024-08-19 14:29:37 +01002837 return ffa_uuid_find(receiver_vm->uuids, PARTITION_MAX_UUIDS,
2838 target_uuid) != NULL;
J-Alves76d99af2021-03-10 17:42:11 +00002839}
2840
2841/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002842 * Send an FF-A direct message request.
Kathleen Capella41fea932023-06-23 17:39:28 -04002843 * This handler covers both FFA_MSG_SEND_DIRECT_REQ_32/64
2844 * and FFA_MSG_SEND_DIRECT_REQ2_64 (introduced in FF-A v1.2) with function-based
2845 * checks to accomodate for the difference between the ABIs.
2846 *
2847 * FFA_MSG_SEND_DIRECT_REQ2_64 works mostly the same as
2848 * FFA_MSG_SEND_DIRECT_REQ_32/64, but adds the ability to send a direct message
2849 * request to a specified UUID within a partition and the usage of an extended
2850 * range of registers (x4-x17, instead of x4-x7) to be used as part of the
2851 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002852 */
Karl Meakin13f09812024-10-28 16:33:23 +00002853struct ffa_value api_ffa_msg_send_direct_req(struct ffa_value args,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002854 struct vcpu *current,
2855 struct vcpu **next)
2856{
Karl Meakin13f09812024-10-28 16:33:23 +00002857 ffa_id_t sender_vm_id = ffa_sender(args);
2858 ffa_id_t receiver_vm_id = ffa_receiver(args);
J-Alves17228f72021-04-20 17:13:19 +01002859 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002860 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002861 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002862 struct vcpu *receiver_vcpu;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002863 struct vcpu_locked current_locked;
2864 struct vcpu_locked receiver_vcpu_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002865 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05002866 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002867
Kathleen Capella41fea932023-06-23 17:39:28 -04002868 if ((args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2869 args.func == FFA_MSG_SEND_DIRECT_REQ_64) &&
Karl Meakin06e8b732024-09-20 18:26:49 +01002870 !ffa_is_framework_msg(args) &&
Kathleen Capella41fea932023-06-23 17:39:28 -04002871 !api_ffa_dir_msg_is_arg2_zero(args)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002872 dlog_verbose("Direct messaging: w2 must be zero (w2 = %#lx)\n",
2873 args.arg2);
J-Alves645eabe2021-02-22 16:08:27 +00002874 return ffa_error(FFA_INVALID_PARAMETERS);
2875 }
2876
Karl Meakin06e8b732024-09-20 18:26:49 +01002877 if (ffa_is_framework_msg(args) &&
2878 plat_ffa_handle_framework_msg(args, &ret)) {
2879 return ret;
2880 }
2881
Karl Meakin117c8082024-12-04 16:03:28 +00002882 if (!ffa_direct_msg_is_direct_request_valid(current, sender_vm_id,
2883 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002884 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002885 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002886 }
2887
Karl Meakin117c8082024-12-04 16:03:28 +00002888 if (ffa_direct_msg_direct_request_forward(receiver_vm_id, args, &ret)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002889 dlog_verbose("Direct message request forwarded\n");
J-Alves17228f72021-04-20 17:13:19 +01002890 return ret;
2891 }
2892
J-Alvesbd32c972024-02-02 16:20:04 +00002893 ret = api_ffa_interrupt_return(0);
J-Alves17228f72021-04-20 17:13:19 +01002894
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002895 receiver_vm = vm_find(receiver_vm_id);
2896 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002897 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002898 return ffa_error(FFA_INVALID_PARAMETERS);
2899 }
2900
Kathleen Capella41fea932023-06-23 17:39:28 -04002901 if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64 &&
2902 !api_ffa_dir_msg_req2_is_uuid_valid(receiver_vm, args)) {
2903 dlog_verbose("UUID unrecognized for this VM\n");
2904 return ffa_error(FFA_INVALID_PARAMETERS);
2905 }
2906
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002907 /*
J-Alves439ac972021-11-18 17:32:03 +00002908 * Check if sender supports sending direct message req, and if
2909 * receiver supports receipt of direct message requests.
2910 */
Karl Meakin117c8082024-12-04 16:03:28 +00002911 if (!ffa_direct_msg_is_direct_request_supported(
2912 current->vm, receiver_vm, args.func)) {
Karl Meakin06e8b732024-09-20 18:26:49 +01002913 dlog_verbose("Direct message request not supported\n");
J-Alves439ac972021-11-18 17:32:03 +00002914 return ffa_error(FFA_DENIED);
2915 }
2916
2917 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002918 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002919 * UP (migratable) or MP partitions with a number of vCPUs matching the
2920 * number of PEs in the system. It further states that MP partitions
2921 * accepting direct request messages cannot migrate.
2922 */
J-Alvesad6a0432021-04-09 16:06:21 +01002923 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002924 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002925 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002926 return ffa_error(FFA_INVALID_PARAMETERS);
2927 }
2928
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002929 /*
2930 * If VM must be locked, it must be done before any of its vCPUs are
2931 * locked.
2932 */
J-Alves6e2abc62021-12-02 14:58:56 +00002933 receiver_locked = vm_lock(receiver_vm);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002934
2935 /* Lock both vCPUs at once to avoid deadlock. */
2936 vcpus_locked = vcpu_lock_both(current, receiver_vcpu);
2937 current_locked = vcpus_locked.vcpu1;
2938 receiver_vcpu_locked = vcpus_locked.vcpu2;
2939
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002940 /*
2941 * If destination vCPU is executing or already received an
2942 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2943 * busy. There is a brief period of time where the vCPU state has
2944 * changed but regs_available is still false thus consider this case as
2945 * the vCPU not yet ready to receive a direct message request.
2946 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06002947 if (is_ffa_direct_msg_request_ongoing(receiver_vcpu_locked) ||
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002948 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2949 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002950 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002951 ret = ffa_error(FFA_BUSY);
2952 goto out;
2953 }
2954
Karl Meakin117c8082024-12-04 16:03:28 +00002955 if (!ffa_cpu_cycles_check_runtime_state_transition(
J-Alves7ea3f7e2024-03-27 11:05:11 +00002956 current_locked, sender_vm_id, HF_INVALID_VM_ID,
2957 receiver_vcpu_locked, args.func, &next_state)) {
2958 ret = ffa_error(FFA_DENIED);
2959 goto out;
2960 }
2961
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002962 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2963 memory_order_relaxed)) {
2964 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Kathleen Capella6e9765b2023-07-11 17:44:58 -04002965 dlog_verbose(
2966 "Receiver VM %#x aborted, cannot run vCPU %u\n",
2967 receiver_vcpu->vm->id,
2968 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002969 receiver_vcpu->state = VCPU_STATE_ABORTED;
2970 }
2971
2972 ret = ffa_error(FFA_ABORTED);
2973 goto out;
2974 }
2975
2976 switch (receiver_vcpu->state) {
2977 case VCPU_STATE_OFF:
2978 case VCPU_STATE_RUNNING:
2979 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002980 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002981 case VCPU_STATE_BLOCKED:
2982 case VCPU_STATE_PREEMPTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002983 ret = ffa_error(FFA_BUSY);
2984 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002985 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002986 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002987 * We expect target vCPU to be in WAITING state after either
2988 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002989 */
2990 break;
2991 }
2992
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05002993 /* Inject timer interrupt if timer has expired. */
2994 api_inject_arch_timer_interrupt(current_locked, receiver_vcpu_locked);
Madhukar Pappireddy106bfc32024-09-25 15:47:11 -05002995 timer_migrate_to_other_cpu(current->cpu, receiver_vcpu_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002996
2997 /* The receiver vCPU runs upon direct message invocation */
2998 receiver_vcpu->cpu = current->cpu;
2999 receiver_vcpu->state = VCPU_STATE_RUNNING;
3000 receiver_vcpu->regs_available = false;
Kathleen Capellae468c112023-12-13 17:56:28 -05003001 receiver_vcpu->direct_request_origin.is_ffa_req2 =
3002 (args.func == FFA_MSG_SEND_DIRECT_REQ2_64);
3003 receiver_vcpu->direct_request_origin.vm_id = sender_vm_id;
Karl Meakin06e8b732024-09-20 18:26:49 +01003004 receiver_vcpu->direct_request_origin.is_framework =
3005 ffa_is_framework_msg(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003006
J-Alves76d99af2021-03-10 17:42:11 +00003007 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003008
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003009 assert(!vm_id_is_current_world(current->vm->id) ||
3010 next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05003011 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003012
Karl Meakin117c8082024-12-04 16:03:28 +00003013 ffa_direct_msg_wind_call_chain_ffa_direct_req(
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07003014 current_locked, receiver_vcpu_locked, sender_vm_id);
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05003015
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003016 /* Switch to receiver vCPU targeted to by direct msg request */
3017 *next = receiver_vcpu;
3018
J-Alves6e2abc62021-12-02 14:58:56 +00003019 if (!receiver_locked.vm->el0_partition) {
3020 /*
3021 * If the scheduler in the system is giving CPU cycles to the
3022 * receiver, due to pending notifications, inject the NPI
3023 * interrupt. Following call assumes that '*next' has been set
3024 * to receiver_vcpu.
3025 */
Karl Meakin117c8082024-12-04 16:03:28 +00003026 ffa_interrupts_inject_notification_pending_interrupt(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003027 receiver_vcpu_locked, current_locked, receiver_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003028 }
3029
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003030 /*
3031 * Since this flow will lead to a VM switch, the return value will not
3032 * be applied to current vCPU.
3033 */
3034
3035out:
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003036 vcpu_unlock(&receiver_vcpu_locked);
J-Alves6e2abc62021-12-02 14:58:56 +00003037 vm_unlock(&receiver_locked);
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003038 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003039
3040 return ret;
3041}
3042
3043/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003044 * Resume the target vCPU after the current vCPU sent a direct response.
3045 * Current vCPU moves to waiting state.
3046 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003047void api_ffa_resume_direct_resp_target(struct vcpu_locked current_locked,
3048 struct vcpu **next,
J-Alves19e20cf2023-08-02 12:48:55 +01003049 ffa_id_t receiver_vm_id,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003050 struct ffa_value to_ret,
3051 bool is_nwd_call_chain)
3052{
Raghu Krishnamurthya9ccf122023-03-27 20:42:01 -07003053 if (plat_ffa_is_spmd_lp_id(receiver_vm_id) ||
3054 !vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003055 *next = api_switch_to_other_world(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003056 VCPU_STATE_WAITING);
3057
3058 /* End of NWd scheduled call chain. */
3059 assert(!is_nwd_call_chain ||
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003060 (current_locked.vcpu->call_chain.prev_node == NULL));
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003061 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003062 *next = api_switch_to_primary(current_locked, to_ret,
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003063 VCPU_STATE_WAITING);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003064 } else if (vm_id_is_current_world(receiver_vm_id)) {
3065 /*
3066 * It is expected the receiver_vm_id to be from an SP, otherwise
Karl Meakin117c8082024-12-04 16:03:28 +00003067 * 'ffa_direct_msg_is_direct_response_valid' should have
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003068 * made function return error before getting to this point.
3069 */
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003070 *next = api_switch_to_vm(current_locked, to_ret,
3071 VCPU_STATE_WAITING, receiver_vm_id);
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003072 } else {
3073 panic("Invalid direct message response invocation");
3074 }
3075}
3076
Karl Meakin50ef9112024-10-28 16:41:44 +00003077static bool api_ffa_msg_send_direct_resp_validate_args(struct ffa_value args,
3078 struct vcpu *current)
3079{
3080 ffa_id_t sender_vm_id = ffa_sender(args);
3081 ffa_id_t receiver_vm_id = ffa_receiver(args);
3082
3083 /*
3084 * If using FFA_MSG_SEND_DIRECT_RESP, the caller's
3085 * - x2 MBZ for partition messages
3086 * - x8-x17 SBZ if caller's FF-A version >= FF-A v1.2
3087 */
3088 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) {
3089 if (!ffa_is_framework_msg(args) &&
3090 !api_ffa_dir_msg_is_arg2_zero(args)) {
3091 dlog_verbose("%s: w2 Must Be Zero",
3092 ffa_func_name(args.func));
3093 return false;
3094 }
3095
3096 if (current->vm->ffa_version >= FFA_VERSION_1_2 &&
3097 !api_extended_args_are_zero(&args)) {
3098 return false;
3099 }
3100 }
3101
Karl Meakin117c8082024-12-04 16:03:28 +00003102 if (!ffa_direct_msg_is_direct_response_valid(current, sender_vm_id,
3103 receiver_vm_id)) {
Karl Meakin50ef9112024-10-28 16:41:44 +00003104 dlog_verbose("Invalid direct response call.\n");
3105 return false;
3106 }
3107
3108 return true;
3109}
3110
3111static bool api_ffa_msg_send_direct_resp_validate_ongoing_request(
3112 struct ffa_value args, struct vcpu_locked current_locked)
3113{
3114 bool req_framework =
3115 current_locked.vcpu->direct_request_origin.is_framework;
3116 bool resp_framework = ffa_is_framework_msg(args);
3117 bool received_req2 =
3118 current_locked.vcpu->direct_request_origin.is_ffa_req2;
3119
3120 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
3121 return false;
3122 }
3123
3124 if (req_framework && !resp_framework) {
3125 dlog_verbose(
3126 "Mismatch in framework message bit: request was a %s "
3127 "message, but response is a %s message\n",
3128 req_framework ? "framework" : "non-framework",
3129 resp_framework ? "framework" : "non-framework");
3130 return false;
3131 }
3132
3133 if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64 && received_req2) {
3134 dlog_verbose(
3135 "FFA_MSG_SEND_DIRECT_RESP must be used with "
3136 "FFA_MSG_SEND_DIRECT_REQ\n");
3137 return false;
3138 }
3139
3140 if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64 && !received_req2) {
3141 dlog_verbose(
3142 "FFA_MSG_SEND_DIRECT_RESP2 must be used with "
3143 "FFA_MSG_SEND_DIRECT_REQ2\n");
3144 return false;
3145 }
3146
3147 return true;
3148}
3149
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003150/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003151 * Send an FF-A direct message response.
Kathleen Capella087e5022023-09-07 18:04:15 -04003152 * This handler covers both FFA_MSG_SEND_DIRECT_RESP_32/64
3153 * and FFA_MSG_SEND_DIRECT_RESP2_64 (introduced in FF-A v1.2) with
3154 * function-based checks to accomodate for the difference between the ABIs.
3155 *
3156 * FFA_MSG_SEND_DIRECT_RESP2_64 is used to respond to requests sent via
3157 * FFA_MSG_SEND_DIRECT_REQ2_64 and adds the usage of an extended range
3158 * of registers (x4-x17, instead of x4-x7) to be used as part of the
3159 * message payload.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003160 */
Karl Meakin13f09812024-10-28 16:33:23 +00003161struct ffa_value api_ffa_msg_send_direct_resp(struct ffa_value args,
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003162 struct vcpu *current,
3163 struct vcpu **next)
3164{
Karl Meakin13f09812024-10-28 16:33:23 +00003165 ffa_id_t sender_vm_id = ffa_sender(args);
3166 ffa_id_t receiver_vm_id = ffa_receiver(args);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003167 struct vcpu_locked current_locked;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003168 struct vcpu_locked next_locked = (struct vcpu_locked){
3169 .vcpu = NULL,
3170 };
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003171 enum vcpu_state next_state = VCPU_STATE_RUNNING;
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003172 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05003173 struct ffa_value signal_interrupt =
3174 (struct ffa_value){.func = FFA_INTERRUPT_32};
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003175 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
3176 struct two_vcpu_locked vcpus_locked;
J-Alves645eabe2021-02-22 16:08:27 +00003177
Karl Meakin50ef9112024-10-28 16:41:44 +00003178 if (!api_ffa_msg_send_direct_resp_validate_args(args, current)) {
J-Alvesaa336102021-03-01 13:02:45 +00003179 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003180 }
3181
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003182 current_locked = vcpu_lock(current);
3183
Karl Meakin117c8082024-12-04 16:03:28 +00003184 if (!ffa_cpu_cycles_check_runtime_state_transition(
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003185 current_locked, sender_vm_id, receiver_vm_id, next_locked,
3186 args.func, &next_state)) {
3187 ret = ffa_error(FFA_DENIED);
3188 goto out;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05003189 }
3190
Madhukar Pappireddyd456ac22023-06-26 15:29:34 -05003191 assert(!vm_id_is_current_world(current->vm->id) ||
3192 next_state == VCPU_STATE_WAITING);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05003193
Karl Meakin50ef9112024-10-28 16:41:44 +00003194 if (!api_ffa_msg_send_direct_resp_validate_ongoing_request(
3195 args, current_locked)) {
Kathleen Capellae468c112023-12-13 17:56:28 -05003196 ret = ffa_error(FFA_DENIED);
3197 goto out;
3198 }
3199
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003200 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003201 struct interrupts *interrupts = &current->interrupts;
3202
Madhukar Pappireddy7945bb52024-08-20 15:22:41 -05003203 CHECK(current->scheduling_mode != SPMC_MODE);
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05003204
Madhukar Pappireddy025a4512024-10-14 22:09:19 -05003205 plat_interrupts_set_priority_mask(
3206 current->prev_interrupt_priority);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003207 /*
3208 * A SP may be signaled a managed exit but actually not trap
3209 * the virtual interrupt, probably because it has virtual
3210 * interrupts masked, and emit direct resp. In this case the
3211 * managed exit operation is considered completed and it would
3212 * also need to clear the pending managed exit flag for the SP
3213 * vCPU.
3214 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01003215 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003216
3217 if (vcpu_is_virt_interrupt_pending(interrupts,
3218 HF_MANAGED_EXIT_INTID)) {
J-Alvesb8730e92024-08-07 18:28:55 +01003219 vcpu_interrupt_clear_decrement(current_locked,
3220 HF_MANAGED_EXIT_INTID);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05003221 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003222 }
3223
Kathleen Capellae468c112023-12-13 17:56:28 -05003224 /* Clear direct request origin vm_id and request type for the caller. */
3225 current->direct_request_origin.is_ffa_req2 = false;
3226 current->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003227
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003228 api_ffa_resume_direct_resp_target(current_locked, next, receiver_vm_id,
3229 to_ret, false);
3230
3231 /*
3232 * Unlock current vCPU to allow it to be locked together with next
3233 * vcpu.
3234 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02003235 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003236
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003237 /* Lock both vCPUs at once to avoid deadlock. */
3238 vcpus_locked = vcpu_lock_both(current, *next);
3239 current_locked = vcpus_locked.vcpu1;
3240 next_locked = vcpus_locked.vcpu2;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003241
Madhukar Pappireddy32424db2024-09-25 15:06:19 -05003242 /* Inject timer interrupt if timer has expired. */
3243 api_inject_arch_timer_interrupt(current_locked, next_locked);
Karl Meakin117c8082024-12-04 16:03:28 +00003244 ffa_direct_msg_unwind_call_chain_ffa_direct_resp(current_locked,
3245 next_locked);
J-Alvesbd32c972024-02-02 16:20:04 +00003246
3247 /*
3248 * Check if there is a pending secure interrupt.
3249 * If there is, return back to the caller with FFA_INTERRUPT,
3250 * and set the `next` vcpu in a preempted state.
3251 */
Karl Meakin117c8082024-12-04 16:03:28 +00003252 if (ffa_interrupts_intercept_call(current_locked, next_locked,
3253 &signal_interrupt)) {
J-Alvesbd32c972024-02-02 16:20:04 +00003254 ret = signal_interrupt;
3255 *next = NULL;
3256 }
3257
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003258 vcpu_unlock(&next_locked);
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05003259
Madhukar Pappireddybd10e572023-03-06 16:39:49 -06003260out:
3261 vcpu_unlock(&current_locked);
3262 return ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00003263}
3264
J-Alves84658fc2021-06-17 14:37:32 +01003265static bool api_memory_region_check_flags(
3266 struct ffa_memory_region *memory_region, uint32_t share_func)
3267{
3268 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00003269 case FFA_MEM_SHARE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003270 case FFA_MEM_SHARE_32:
3271 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3272 0U) {
3273 return false;
3274 }
3275 /* Intentional fall-through */
J-Alves95fbb312024-03-20 15:19:16 +00003276 case FFA_MEM_LEND_64:
J-Alves84658fc2021-06-17 14:37:32 +01003277 case FFA_MEM_LEND_32:
J-Alves95fbb312024-03-20 15:19:16 +00003278 case FFA_MEM_DONATE_64:
J-Alves84658fc2021-06-17 14:37:32 +01003279 case FFA_MEM_DONATE_32: {
3280 /* Bits 31:2 Must Be Zero. */
3281 ffa_memory_receiver_flags_t to_mask =
3282 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
3283 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
3284
3285 if ((memory_region->flags & to_mask) != 0U) {
3286 return false;
3287 }
3288 break;
3289 }
3290 default:
3291 panic("Check for mem send calls only.\n");
3292 }
3293
3294 /* Last check reserved values are 0 */
3295 return true;
3296}
3297
J-Alves33c47bf2022-09-29 11:36:20 +01003298/*
3299 * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0.
3300 */
3301static void api_ffa_memory_region_v1_1_from_v1_0(
3302 struct ffa_memory_region_v1_0 *memory_region_v1_0,
3303 struct ffa_memory_region *memory_region_v1_1)
3304{
3305 memory_region_v1_1->sender = memory_region_v1_0->sender;
J-Alves00847062022-10-03 17:08:44 +01003306 memory_region_v1_1->handle = memory_region_v1_0->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003307 memory_region_v1_1->attributes =
3308 ffa_memory_attributes_extend(memory_region_v1_0->attributes);
J-Alves33c47bf2022-09-29 11:36:20 +01003309 memory_region_v1_1->flags = memory_region_v1_0->flags;
3310 memory_region_v1_1->tag = memory_region_v1_0->tag;
3311 memory_region_v1_1->memory_access_desc_size =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003312 sizeof(struct ffa_memory_access_v1_0);
J-Alves33c47bf2022-09-29 11:36:20 +01003313 memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003314 memory_region_v1_1->receivers_offset = sizeof(struct ffa_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003315
3316 /* Zero reserved fields. */
3317 for (uint32_t i = 0; i < 3U; i++) {
3318 memory_region_v1_1->reserved[i] = 0U;
3319 }
3320}
3321
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003322/**
3323 * Updates a v1.0 transaction descriptor to v1.1. This gives us the
3324 * memory_access_desc_size field we need for forwards compatability.
3325 * Copy the receivers and composite descriptors to the new struct.
3326 * We also check the fields in the v1.0 transaction descriptor and return:
3327 * - FFA_ERROR FFA_INVALID_PARAMETERS: If any of the fields are not valid
3328 * values, eg the reserved fields are not 0, receiver_count is too large or
3329 * composite offsets are not 0 for retrieve requests or in bounds for send
3330 * requests.
3331 * - FFA ERROR FFA_NOT_SUPPORTED: If an invalid ffa_version is supplied to the
3332 * function. Or the fragment length is more than a single page.
3333 * - FFA_ERROR FFA_NO_MEMORY: If we do not have enough memory for a scratch
3334 * memory transaction descriptor.
3335 * - FFA_SUCCESS: If a successful update has occured.
J-Alves33c47bf2022-09-29 11:36:20 +01003336 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003337static struct ffa_value api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3338 void *allocated, uint32_t *fragment_length, uint32_t *total_length,
Karl Meakin0e617d92024-04-05 12:55:22 +01003339 enum ffa_version ffa_version, bool send_transaction)
J-Alves33c47bf2022-09-29 11:36:20 +01003340{
3341 struct ffa_memory_region_v1_0 *memory_region_v1_0;
3342 struct ffa_memory_region *memory_region_v1_1 = NULL;
3343 struct ffa_composite_memory_region *composite_v1_0;
3344 struct ffa_composite_memory_region *composite_v1_1;
3345 size_t receivers_length;
3346 size_t space_left;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003347 size_t receivers_end;
J-Alves33c47bf2022-09-29 11:36:20 +01003348 size_t composite_offset_v1_1;
3349 size_t composite_offset_v1_0;
3350 size_t fragment_constituents_size;
3351 size_t fragment_length_v1_1;
3352
J-Alves33c47bf2022-09-29 11:36:20 +01003353 assert(fragment_length != NULL);
3354 assert(total_length != NULL);
3355
Karl Meakin0e617d92024-04-05 12:55:22 +01003356 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves33c47bf2022-09-29 11:36:20 +01003357 return (struct ffa_value){.func = FFA_SUCCESS_32};
3358 }
3359
Karl Meakin0e617d92024-04-05 12:55:22 +01003360 if (ffa_version != FFA_VERSION_1_0) {
J-Alves33c47bf2022-09-29 11:36:20 +01003361 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
3362 ffa_version);
3363 return ffa_error(FFA_NOT_SUPPORTED);
3364 }
3365
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003366 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003367 "Updating memory transaction descriptor from v1.0 to v1.1.\n");
J-Alves33c47bf2022-09-29 11:36:20 +01003368
3369 memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated;
3370
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003371 receivers_length = sizeof(struct ffa_memory_access_v1_0) *
J-Alves33c47bf2022-09-29 11:36:20 +01003372 memory_region_v1_0->receiver_count;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003373 receivers_end = sizeof(struct ffa_memory_region) + receivers_length;
J-Alves33c47bf2022-09-29 11:36:20 +01003374
3375 /*
3376 * Check the specified composite offset of v1.0 descriptor, and that all
3377 * receivers were configured with the same offset.
3378 */
3379 composite_offset_v1_0 =
3380 memory_region_v1_0->receivers[0].composite_memory_region_offset;
3381
J-Alves33c47bf2022-09-29 11:36:20 +01003382 /* Determine the composite offset for v1.1 descriptor. */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003383 if (send_transaction) {
3384 fragment_constituents_size =
3385 *fragment_length - composite_offset_v1_0 -
3386 sizeof(struct ffa_composite_memory_region);
3387 fragment_length_v1_1 =
3388 receivers_end +
3389 sizeof(struct ffa_composite_memory_region) +
3390 fragment_constituents_size;
3391 composite_offset_v1_1 = receivers_end;
3392 } else {
3393 fragment_constituents_size = 0;
3394 fragment_length_v1_1 = receivers_end;
3395 composite_offset_v1_1 = 0;
3396 }
J-Alves33c47bf2022-09-29 11:36:20 +01003397
3398 /*
3399 * Currently only support the simpler cases: memory transaction
3400 * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE.
3401 * TODO: allocate the entries needed for this fragment_length_v1_1.
3402 * - Check corner when v1.1 descriptor converted size surpasses
3403 * the size of the entry.
3404 */
3405 if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) {
3406 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003407 "Translation of FF-A v1.0 descriptors for over %lu is "
J-Alves33c47bf2022-09-29 11:36:20 +01003408 "unsupported.",
3409 MM_PPOOL_ENTRY_SIZE);
3410 return ffa_error(FFA_NOT_SUPPORTED);
3411 }
3412
3413 space_left = fragment_length_v1_1;
3414
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003415 /*
3416 * Allocate a page of memory to construct the v1.1 memory descriptor.
3417 * Earlier we checked that the fragment_length_v1_1 would not be larger
3418 * than a page.
3419 */
J-Alves33c47bf2022-09-29 11:36:20 +01003420 memory_region_v1_1 =
3421 (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
3422 if (memory_region_v1_1 == NULL) {
3423 return ffa_error(FFA_NO_MEMORY);
3424 }
3425
3426 /* Translate header from v1.0 to v1.1. */
3427 api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0,
3428 memory_region_v1_1);
3429
3430 space_left -= sizeof(struct ffa_memory_region);
3431
3432 /* Copy memory access information. */
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01003433 memcpy_s((uint8_t *)memory_region_v1_1 +
3434 memory_region_v1_1->receivers_offset,
3435 space_left, memory_region_v1_0->receivers, receivers_length);
J-Alves33c47bf2022-09-29 11:36:20 +01003436
3437 /* Initialize the memory access descriptors with composite offset. */
3438 for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) {
3439 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003440 ffa_memory_region_get_receiver(memory_region_v1_1, i);
3441 assert(receiver != NULL);
J-Alves33c47bf2022-09-29 11:36:20 +01003442 receiver->composite_memory_region_offset =
3443 composite_offset_v1_1;
3444 }
3445
3446 space_left -= receivers_length;
3447
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003448 /* Composite memory descriptors to copy. */
3449 if (send_transaction) {
3450 /* Init v1.1 composite. */
3451 composite_v1_1 = (struct ffa_composite_memory_region
3452 *)((uint8_t *)memory_region_v1_1 +
3453 composite_offset_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003454
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003455 composite_v1_0 = ffa_memory_region_get_composite_v1_0(
3456 memory_region_v1_0, 0);
3457 composite_v1_1->constituent_count =
3458 composite_v1_0->constituent_count;
3459 composite_v1_1->page_count = composite_v1_0->page_count;
J-Alves33c47bf2022-09-29 11:36:20 +01003460
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003461 space_left -= sizeof(struct ffa_composite_memory_region);
J-Alves33c47bf2022-09-29 11:36:20 +01003462
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003463 /* Initialize v1.1 constituents. */
3464 memcpy_s(composite_v1_1->constituents, space_left,
3465 composite_v1_0->constituents,
3466 fragment_constituents_size);
J-Alves33c47bf2022-09-29 11:36:20 +01003467
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003468 space_left -= fragment_constituents_size;
3469 }
3470
J-Alves33c47bf2022-09-29 11:36:20 +01003471 assert(space_left == 0U);
3472
J-Alves33c47bf2022-09-29 11:36:20 +01003473 /*
3474 * Remove the v1.0 fragment size, and resultant size of v1.1 fragment.
3475 */
3476 *total_length = *total_length - *fragment_length + fragment_length_v1_1;
3477 *fragment_length = fragment_length_v1_1;
3478
3479 /*
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003480 * After successfully updating to v1.1 copy the descriptor to the
3481 * internal buffer given as a parameter (used to prevent TOCTOU attacks)
3482 * and free the scratch memory used to construct it.
J-Alves33c47bf2022-09-29 11:36:20 +01003483 */
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003484 memcpy_s(allocated, MM_PPOOL_ENTRY_SIZE, memory_region_v1_1,
3485 *fragment_length);
3486 mpool_free(&api_page_pool, memory_region_v1_1);
J-Alves33c47bf2022-09-29 11:36:20 +01003487
3488 return (struct ffa_value){.func = FFA_SUCCESS_32};
3489}
3490
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003491struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
3492 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003493 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003494{
3495 struct vm *from = current->vm;
3496 struct vm *to;
3497 const void *from_msg;
J-Alves33c47bf2022-09-29 11:36:20 +01003498 void *allocated_entry;
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003499 struct ffa_memory_region *memory_region = NULL;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003500 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01003501 bool targets_other_world = false;
Karl Meakin0e617d92024-04-05 12:55:22 +01003502 enum ffa_version ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003503
3504 if (ipa_addr(address) != 0 || page_count != 0) {
3505 /*
3506 * Hafnium only supports passing the descriptor in the TX
3507 * mailbox.
3508 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003509 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003510 }
3511
Andrew Walbranca808b12020-05-15 17:22:28 +01003512 if (fragment_length > length) {
3513 dlog_verbose(
3514 "Fragment length %d greater than total length %d.\n",
3515 fragment_length, length);
3516 return ffa_error(FFA_INVALID_PARAMETERS);
3517 }
J-Alves33c47bf2022-09-29 11:36:20 +01003518
3519 if (fragment_length > HF_MAILBOX_SIZE ||
3520 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003521 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003522 }
3523
3524 /*
3525 * Check that the sender has configured its send buffer. If the TX
3526 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3527 * be safely accessed after releasing the lock since the TX mailbox
3528 * address can only be configured once.
3529 */
3530 sl_lock(&from->lock);
3531 from_msg = from->mailbox.send;
J-Alves33c47bf2022-09-29 11:36:20 +01003532 ffa_version = from->ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003533 sl_unlock(&from->lock);
3534
3535 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003536 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003537 }
3538
3539 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003540 * Copy the memory region descriptor to a fresh page from the memory
3541 * pool. This prevents the sender from changing it underneath us, and
3542 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003543 */
J-Alves33c47bf2022-09-29 11:36:20 +01003544 allocated_entry = mpool_alloc(&api_page_pool);
3545 if (allocated_entry == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003546 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003547 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003548 }
J-Alves33c47bf2022-09-29 11:36:20 +01003549
J-Alves8f2150d2024-03-28 16:15:56 +00003550 if (!memcpy_trapped(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg,
3551 fragment_length)) {
3552 dlog_error(
3553 "%s: Failed to copy FF-A memory region descriptor.\n",
3554 __func__);
3555 return ffa_error(FFA_ABORTED);
3556 }
J-Alves33c47bf2022-09-29 11:36:20 +01003557
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003558 if (!ffa_memory_region_sanity_check(allocated_entry, ffa_version,
3559 fragment_length, true)) {
3560 ret = ffa_error(FFA_INVALID_PARAMETERS);
3561 goto out;
3562 }
3563
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003564 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3565 allocated_entry, &fragment_length, &length, ffa_version, true);
J-Alves33c47bf2022-09-29 11:36:20 +01003566 if (ret.func != FFA_SUCCESS_32) {
3567 goto out;
3568 }
3569
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003570 memory_region = allocated_entry;
3571
J-Alves33c47bf2022-09-29 11:36:20 +01003572 if (fragment_length < sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003573 memory_region->memory_access_desc_size) {
J-Alves33c47bf2022-09-29 11:36:20 +01003574 dlog_verbose(
3575 "Initial fragment length %d smaller than header size "
Karl Meakine8937d92024-03-19 16:04:25 +00003576 "%lu.\n",
J-Alves33c47bf2022-09-29 11:36:20 +01003577 fragment_length,
3578 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003579 memory_region->memory_access_desc_size);
3580 ret = ffa_error(FFA_INVALID_PARAMETERS);
3581 goto out;
J-Alves33c47bf2022-09-29 11:36:20 +01003582 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003583
J-Alves84658fc2021-06-17 14:37:32 +01003584 if (!api_memory_region_check_flags(memory_region, share_func)) {
3585 dlog_verbose(
3586 "Memory region reserved arguments must be zero.\n");
3587 ret = ffa_error(FFA_INVALID_PARAMETERS);
3588 goto out;
3589 }
3590
J-Alves363f5722022-04-25 17:37:37 +01003591 if (memory_region->receiver_count == 0U) {
3592 dlog_verbose("Receiver count can't be 0.\n");
3593 ret = ffa_error(FFA_INVALID_PARAMETERS);
3594 goto out;
3595 }
3596
J-Alves95fbb312024-03-20 15:19:16 +00003597 if ((share_func == FFA_MEM_DONATE_32 ||
3598 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01003599 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003600 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003601 "FFA_MEM_DONATE only supports one recipient. Specified "
3602 "%u\n",
J-Alves363f5722022-04-25 17:37:37 +01003603 memory_region->receiver_count);
3604 ret = ffa_error(FFA_INVALID_PARAMETERS);
3605 goto out;
3606 }
3607
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003608 /*
3609 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01003610 * If there is a receiver from the other world, track it for later
3611 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003612 */
J-Alves363f5722022-04-25 17:37:37 +01003613 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003614 struct ffa_memory_access *receiver =
3615 ffa_memory_region_get_receiver(memory_region, i);
J-Alvesc9227c82024-04-24 21:00:58 +01003616 ffa_id_t receiver_id;
3617
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003618 assert(receiver != NULL);
J-Alvesc9227c82024-04-24 21:00:58 +01003619
3620 receiver_id = receiver->receiver_permissions.receiver;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003621
J-Alves363f5722022-04-25 17:37:37 +01003622 to = vm_find(receiver_id);
3623
J-Alves7de61af2024-03-27 10:29:46 +00003624 if ((vm_id_is_current_world(receiver_id) && to == NULL) ||
3625 to == from) {
J-Alves363f5722022-04-25 17:37:37 +01003626 dlog_verbose("%s: invalid receiver.\n", __func__);
3627 ret = ffa_error(FFA_INVALID_PARAMETERS);
3628 goto out;
3629 }
3630
Karl Meakin117c8082024-12-04 16:03:28 +00003631 if (!ffa_memory_is_send_valid(
J-Alvesc9227c82024-04-24 21:00:58 +01003632 receiver_id, from->id, share_func,
3633 memory_region->receiver_count > 1)) {
J-Alves363f5722022-04-25 17:37:37 +01003634 ret = ffa_error(FFA_DENIED);
3635 goto out;
3636 }
3637
3638 /* Capture if any of the receivers is from the other world. */
3639 if (!targets_other_world) {
3640 targets_other_world =
3641 !vm_id_is_current_world(receiver_id);
3642 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003643 }
3644
J-Alves363f5722022-04-25 17:37:37 +01003645 if (targets_other_world) {
Karl Meakin117c8082024-12-04 16:03:28 +00003646 ret = ffa_memory_other_world_mem_send(
J-Alves66652252022-07-06 09:49:51 +01003647 from, share_func, &memory_region, length,
3648 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003649 } else {
3650 struct vm_locked from_locked = vm_lock(from);
3651
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003652 ret = ffa_memory_send(from_locked, memory_region, length,
3653 fragment_length, share_func,
3654 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003655 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003656 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003657 * make sure we don't free it.
3658 */
3659 memory_region = NULL;
3660
3661 vm_unlock(&from_locked);
3662 }
3663
3664out:
3665 if (memory_region != NULL) {
3666 mpool_free(&api_page_pool, memory_region);
3667 }
3668
3669 return ret;
3670}
3671
J-Alvesc7270b72024-09-12 10:16:34 +01003672/**
3673 * An FFA_MEM_RETRIEVE_REQ from the hypervisor must specify the handle of the
3674 * memory transaction it is querying and all other fields must be 0.
3675 */
3676static bool api_ffa_memory_hypervisor_retrieve_request_validate(
3677 struct ffa_memory_region *request, enum ffa_version version)
3678{
3679 switch (version) {
3680 case FFA_VERSION_1_0: {
3681 struct ffa_memory_region_v1_0 *request_v1_0 =
3682 (struct ffa_memory_region_v1_0 *)request;
3683
3684 return request_v1_0->sender == 0U &&
3685 request_v1_0->attributes.shareability == 0U &&
3686 request_v1_0->attributes.cacheability == 0U &&
3687 request_v1_0->attributes.type == 0U &&
3688 request_v1_0->attributes.security == 0U &&
3689 request_v1_0->flags == 0U && request_v1_0->tag == 0U &&
3690 request_v1_0->receiver_count == 0U &&
Karl Meakin117c8082024-12-04 16:03:28 +00003691 ffa_memory_is_handle_allocated_by_current_world(
J-Alvesc7270b72024-09-12 10:16:34 +01003692 request_v1_0->handle);
3693 }
3694 default:
3695 return request->sender == 0U &&
3696 request->attributes.shareability == 0U &&
3697 request->attributes.cacheability == 0U &&
3698 request->attributes.type == 0U &&
3699 request->attributes.security == 0U &&
3700 request->flags == 0U && request->tag == 0U &&
3701 request->memory_access_desc_size == 0U &&
3702 request->receiver_count == 0U &&
3703 request->receivers_offset == 0U &&
Karl Meakin117c8082024-12-04 16:03:28 +00003704 ffa_memory_is_handle_allocated_by_current_world(
J-Alvesc7270b72024-09-12 10:16:34 +01003705 request->handle);
3706 }
3707}
3708
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003709struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3710 uint32_t fragment_length,
3711 ipaddr_t address, uint32_t page_count,
3712 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003713{
3714 struct vm *to = current->vm;
3715 struct vm_locked to_locked;
3716 const void *to_msg;
J-Alves00847062022-10-03 17:08:44 +01003717 void *retrieve_msg;
3718 struct ffa_memory_region *retrieve_request = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003719 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003720 struct ffa_value ret;
Karl Meakin0e617d92024-04-05 12:55:22 +01003721 enum ffa_version ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003722
3723 if (ipa_addr(address) != 0 || page_count != 0) {
3724 /*
3725 * Hafnium only supports passing the descriptor in the TX
3726 * mailbox.
3727 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003728 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003729 }
3730
Andrew Walbrana65a1322020-04-06 19:32:32 +01003731 if (fragment_length != length) {
J-Alves33c47bf2022-09-29 11:36:20 +01003732 dlog_verbose("Fragmentation not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003733 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003734 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003735
J-Alves00847062022-10-03 17:08:44 +01003736 retrieve_msg = cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003737 message_buffer_size = cpu_get_buffer_size(current->cpu);
3738 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3739 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003740 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003741 }
3742
3743 to_locked = vm_lock(to);
3744 to_msg = to->mailbox.send;
J-Alves00847062022-10-03 17:08:44 +01003745 ffa_version = to->ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003746
3747 if (to_msg == NULL) {
3748 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003749 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003750 goto out;
3751 }
3752
3753 /*
3754 * Copy the retrieve request descriptor to an internal buffer, so that
3755 * the caller can't change it underneath us.
3756 */
J-Alves8f2150d2024-03-28 16:15:56 +00003757 if (!memcpy_trapped(retrieve_msg, message_buffer_size, to_msg,
3758 length)) {
3759 dlog_error(
3760 "%s: Failed to copy FF-A retrieve request "
3761 "descriptor.\n",
3762 __func__);
3763 ret = ffa_error(FFA_ABORTED);
3764 goto out;
3765 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003766
J-Alvese8c8c2b2022-12-16 15:34:48 +00003767 if ((vm_is_mailbox_other_world_owned(to_locked) &&
Karl Meakin117c8082024-12-04 16:03:28 +00003768 !ffa_setup_acquire_receiver_rx(to_locked, &ret)) ||
J-Alvese8c8c2b2022-12-16 15:34:48 +00003769 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003770 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003771 * Can't retrieve memory information if the mailbox is
3772 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003773 */
J-Alves59ed0042022-07-28 18:26:41 +01003774 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003775 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003776 goto out;
3777 }
3778
Daniel Boulby296ee702023-11-28 13:36:55 +00003779 if (!is_ffa_hypervisor_retrieve_request(retrieve_msg)) {
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003780 if (!ffa_memory_region_sanity_check(retrieve_msg, ffa_version,
3781 fragment_length, false)) {
3782 ret = ffa_error(FFA_INVALID_PARAMETERS);
3783 goto out;
3784 }
3785 /*
3786 * If required, transform the retrieve request to FF-A v1.1.
3787 */
3788 ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0(
3789 retrieve_msg, &fragment_length, &length, ffa_version,
3790 false);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01003791
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003792 if (ret.func != FFA_SUCCESS_32) {
3793 goto out;
3794 }
J-Alvesc7270b72024-09-12 10:16:34 +01003795 } else {
3796 if (!api_ffa_memory_hypervisor_retrieve_request_validate(
3797 retrieve_msg, ffa_version)) {
3798 dlog_verbose(
3799 "All fields except the handle in the "
3800 "memory access descriptor must be zero for a "
3801 "hypervisor retrieve request.\n");
3802 ret = ffa_error(FFA_INVALID_PARAMETERS);
3803 goto out;
3804 }
J-Alves00847062022-10-03 17:08:44 +01003805 }
3806
Daniel Boulby36e0bdb2023-10-09 12:00:41 +01003807 retrieve_request = retrieve_msg;
Kathleen Capella578784f2023-09-01 18:03:27 -04003808
Karl Meakin117c8082024-12-04 16:03:28 +00003809 if (ffa_memory_is_handle_allocated_by_current_world(
J-Alvesb5084cf2022-07-06 14:20:12 +01003810 retrieve_request->handle)) {
3811 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3812 &api_page_pool);
3813 } else {
Daniel Boulbyfdd59f72023-10-19 16:43:19 +01003814 dlog_error("Invalid FF-A memory handle.\n");
3815 ret = ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesb5084cf2022-07-06 14:20:12 +01003816 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003817out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003818 vm_unlock(&to_locked);
3819 return ret;
3820}
3821
J-Alvesa39a8442024-04-26 15:41:47 +01003822/**
3823 * Copies the memory relinquish descriptor from the partition's TX buffer, to
3824 * the buffer of the local CPU. Do it safely, and return error if:
3825 * - FFA_ABORTED: if the `memcpy_trapped` fails.
3826 * - FFA_INVALID_PARAMETERS: if the size of the full memory relinquish
3827 * descriptor doesn't fit the local CPU buffer.
3828 *
3829 * Returns FFA_SUCCESS if copying goes well, and sets 'out_relinquish'
3830 * to the address of the cpu buffer with the relinquish descriptor.
3831 */
3832static struct ffa_value api_get_ffa_mem_relinquish_descriptor(
3833 struct vcpu *current, const void *from_msg,
3834 struct ffa_mem_relinquish **out_relinquish)
3835{
3836 struct ffa_mem_relinquish *relinquish_request;
3837 uint32_t from_msg_size;
3838 uint32_t total_from_msg_size;
3839 uint32_t dst_size;
3840 vaddr_t dst;
3841 vaddr_t src;
3842
3843 assert(from_msg != NULL);
3844 assert(out_relinquish != NULL);
3845
3846 /*
3847 * Copy the relinquish descriptor to an internal buffer, so that the
3848 * caller can't change it underneath us.
3849 */
3850 relinquish_request =
3851 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
3852
3853 /* Set the destination for the copy. */
3854 dst = va_from_ptr(relinquish_request);
3855 src = va_from_ptr(from_msg);
3856
3857 dst_size = cpu_get_buffer_size(current->cpu);
3858
3859 /* Only copy the size to start with. */
3860 from_msg_size = sizeof(struct ffa_mem_relinquish);
3861 total_from_msg_size = from_msg_size;
3862
3863 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3864 from_msg_size)) {
3865 dlog_error(
3866 "%s: Failed to copy FF-A memory relinquish "
3867 "descriptor.\n",
3868 __func__);
3869 return ffa_error(FFA_ABORTED);
3870 }
3871
3872 if (relinquish_request->endpoint_count != 1) {
3873 dlog_error("%s: relinquish descriptor must have 1 endpoint\n",
3874 __func__);
3875 return ffa_error(FFA_INVALID_PARAMETERS);
3876 }
3877
3878 /*
3879 * Increment the `dst` to the position right after the copied header.
3880 * Increment the `src` to point at the list of endpoints.
3881 *
3882 * Calculate the new `dst_size` which is the size of the allocated cpu
3883 * buffer, minus the size of the copied memory relinquish header.
3884 *
3885 * Only after the above: determine the new `from_msg_size` in accordance
3886 * to the endpoint count.
3887 */
3888 dst = va_add(dst, from_msg_size);
3889 src = va_add(src, from_msg_size);
3890
3891 /*
3892 * Check if it is safe to copy the rest of the message.
3893 * This also serves as a santiy check to 'endpoint_count'.
3894 * The size of what is left in the descriptor, based on endpoint_count,
3895 * shall not be bigger than the size of the mailbox minus the size of
3896 * the header which was previously copied in this function.
3897 */
3898 dst_size -= from_msg_size;
3899 from_msg_size = relinquish_request->endpoint_count * sizeof(ffa_id_t);
3900 total_from_msg_size += from_msg_size;
3901
3902 if (total_from_msg_size > HF_MAILBOX_SIZE ||
3903 total_from_msg_size > dst_size) {
3904 dlog_verbose(
3905 "Relinquish message too long. Endpoint count: %u\n",
3906 relinquish_request->endpoint_count);
3907 return ffa_error(FFA_INVALID_PARAMETERS);
3908 }
3909
3910 /* Copy the remaining fragment. */
3911 if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src),
3912 from_msg_size)) {
3913 dlog_error("%s: Failed to copy FF-A relinquish request.\n",
3914 __func__);
3915 return ffa_error(FFA_ABORTED);
3916 }
3917
3918 /*
3919 * Set the output address for the relinquish descriptor to the current
3920 * cpu's buffer.
3921 */
3922 *out_relinquish = relinquish_request;
3923
3924 return (struct ffa_value){.func = FFA_SUCCESS_32};
3925}
3926
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003927struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003928{
3929 struct vm *from = current->vm;
3930 struct vm_locked from_locked;
3931 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003932 struct ffa_value ret;
J-Alvesa39a8442024-04-26 15:41:47 +01003933 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003934
3935 from_locked = vm_lock(from);
3936 from_msg = from->mailbox.send;
3937
3938 if (from_msg == NULL) {
3939 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003940 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003941 goto out;
3942 }
3943
J-Alvesa39a8442024-04-26 15:41:47 +01003944 ret = api_get_ffa_mem_relinquish_descriptor(current, from_msg,
3945 &relinquish_request);
3946
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003947 /*
J-Alvesa39a8442024-04-26 15:41:47 +01003948 * If the descriptor was safely copied, continue with the handling of
3949 * the retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003950 */
J-Alvesa39a8442024-04-26 15:41:47 +01003951 if (ret.func == FFA_SUCCESS_32) {
3952 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3953 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003954 }
J-Alves8f2150d2024-03-28 16:15:56 +00003955
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003956out:
3957 vm_unlock(&from_locked);
3958 return ret;
3959}
3960
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003961struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3962 ffa_memory_region_flags_t flags,
3963 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003964{
3965 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003966 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003967
Karl Meakin117c8082024-12-04 16:03:28 +00003968 if (ffa_memory_is_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003969 struct vm_locked to_locked = vm_lock(to);
3970
Andrew Walbranca808b12020-05-15 17:22:28 +01003971 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003972 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003973
Andrew Walbran290b0c92020-02-03 16:37:14 +00003974 vm_unlock(&to_locked);
3975 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00003976 ret = ffa_memory_other_world_mem_reclaim(to, handle, flags,
3977 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003978 }
3979
3980 return ret;
3981}
3982
3983struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
3984 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003985 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003986 struct vcpu *current)
3987{
3988 struct vm *to = current->vm;
3989 struct vm_locked to_locked;
3990 struct ffa_value ret;
3991
3992 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01003993 if (vm_id_is_current_world(to->id)) {
3994 if (sender_vm_id != 0) {
3995 dlog_verbose("%s: Invalid sender.\n", __func__);
3996 return ffa_error(FFA_INVALID_PARAMETERS);
3997 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003998 }
3999
4000 to_locked = vm_lock(to);
4001
J-Alves122f1a12022-12-12 15:55:42 +00004002 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004003 /*
4004 * Can't retrieve memory information if the mailbox is not
4005 * available.
4006 */
J-Alves59ed0042022-07-28 18:26:41 +01004007 dlog_verbose("%s: RX buffer not ready partition %x.\n",
4008 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01004009 ret = ffa_error(FFA_BUSY);
4010 goto out;
4011 }
4012
J-Alvesc3fd9752024-04-04 11:45:33 +01004013 /*
4014 * Whilst copying the fragments, initialize the remaining constituents
4015 * in the CPU's internal structure, and later copy from the CPU buffer
4016 * into the partition's RX buffer. In the case SPMC is doing a retrieve
4017 * request for a VM/Hypervisor in an RME enabled system, there is no
4018 * guarantee the RX buffer is in the NS PAS. Accessing the buffer with
4019 * the wrong security state attribute would then result in an GPF.
4020 * The fragment is initialized in an internal buffer, and is later
4021 * copied to the RX buffer using the 'memcpy_trapped' which allows to
4022 * smoothly terminate the operation if the access has been preempted by
4023 * a GPF exception.
4024 */
4025 ret = ffa_memory_retrieve_continue(
4026 to_locked, handle, fragment_offset, sender_vm_id,
4027 cpu_get_buffer(current->cpu), &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01004028out:
4029 vm_unlock(&to_locked);
4030 return ret;
4031}
4032
4033struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
4034 uint32_t fragment_length,
J-Alves19e20cf2023-08-02 12:48:55 +01004035 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01004036 struct vcpu *current)
4037{
4038 struct vm *from = current->vm;
4039 const void *from_msg;
4040 void *fragment_copy;
4041 struct ffa_value ret;
4042
4043 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01004044 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
4045 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01004046 return ffa_error(FFA_INVALID_PARAMETERS);
4047 }
4048
4049 /*
4050 * Check that the sender has configured its send buffer. If the TX
4051 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
4052 * be safely accessed after releasing the lock since the TX mailbox
4053 * address can only be configured once.
4054 */
4055 sl_lock(&from->lock);
4056 from_msg = from->mailbox.send;
4057 sl_unlock(&from->lock);
4058
4059 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01004060 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01004061 return ffa_error(FFA_INVALID_PARAMETERS);
4062 }
4063
4064 /*
4065 * Copy the fragment to a fresh page from the memory pool. This prevents
4066 * the sender from changing it underneath us, and also lets us keep it
4067 * around in the share state table if needed.
4068 */
4069 if (fragment_length > HF_MAILBOX_SIZE ||
4070 fragment_length > MM_PPOOL_ENTRY_SIZE) {
4071 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004072 "Fragment length %d larger than mailbox size %zu.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01004073 fragment_length, HF_MAILBOX_SIZE);
4074 return ffa_error(FFA_INVALID_PARAMETERS);
4075 }
4076 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
4077 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
4078 0) {
4079 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
4080 return ffa_error(FFA_INVALID_PARAMETERS);
4081 }
4082 fragment_copy = mpool_alloc(&api_page_pool);
4083 if (fragment_copy == NULL) {
4084 dlog_verbose("Failed to allocate fragment copy.\n");
4085 return ffa_error(FFA_NO_MEMORY);
4086 }
J-Alves8f2150d2024-03-28 16:15:56 +00004087
4088 if (!memcpy_trapped(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg,
4089 fragment_length)) {
4090 dlog_error("%s: Failed to copy fragment.\n", __func__);
4091 return ffa_error(FFA_ABORTED);
4092 }
Andrew Walbranca808b12020-05-15 17:22:28 +01004093
4094 /*
4095 * Hafnium doesn't support fragmentation of memory retrieve requests
4096 * (because it doesn't support caller-specified mappings, so a request
4097 * will never be larger than a single page), so this must be part of a
4098 * memory send (i.e. donate, lend or share) request.
4099 *
4100 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01004101 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01004102 */
Karl Meakin117c8082024-12-04 16:03:28 +00004103 if (ffa_memory_is_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01004104 struct vm_locked from_locked = vm_lock(from);
4105
4106 ret = ffa_memory_send_continue(from_locked, fragment_copy,
4107 fragment_length, handle,
4108 &api_page_pool);
4109 /*
4110 * `ffa_memory_send_continue` takes ownership of the
4111 * fragment_copy, so we don't need to free it here.
4112 */
4113 vm_unlock(&from_locked);
4114 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00004115 ret = ffa_memory_other_world_mem_send_continue(
J-Alvesfdd29272022-07-19 13:16:31 +01004116 from, fragment_copy, fragment_length, handle,
4117 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00004118 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00004119
4120 return ret;
4121}
Max Shvetsov40108e72020-08-27 12:39:50 +01004122
Olivier Deprezd614d322021-06-18 15:21:00 +02004123/**
4124 * Register an entry point for a vCPU in warm boot cases.
4125 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
4126 */
Max Shvetsov40108e72020-08-27 12:39:50 +01004127struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
4128 struct vcpu *current)
4129{
4130 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01004131 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01004132
Olivier Deprezd614d322021-06-18 15:21:00 +02004133 /*
4134 * Reject if interface is not supported at this FF-A instance
4135 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
4136 */
Karl Meakin117c8082024-12-04 16:03:28 +00004137 if (!ffa_setup_is_secondary_ep_register_supported() ||
Karl Meakin82041822024-05-20 11:14:34 +01004138 vm_is_up(current->vm)) {
Olivier Deprezd614d322021-06-18 15:21:00 +02004139 return ffa_error(FFA_NOT_SUPPORTED);
4140 }
4141
4142 /*
4143 * No further check is made on the address validity
4144 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
4145 * from the VM or vCPU structure.
4146 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
4147 * For each SP [...] the Framework assumes that the same entry point
4148 * address is used for initializing any execution context during a
4149 * secondary cold boot.
4150 * If this function is invoked multiple times, then the entry point
4151 * address specified in the last valid invocation must be used by the
4152 * callee.
4153 */
Olivier Deprezb2808332023-02-02 15:25:40 +01004154 current_locked = vcpu_lock(current);
4155 if (current->rt_model != RTM_SP_INIT) {
4156 dlog_error(
4157 "FFA_SECONDARY_EP_REGISTER can only be called while "
4158 "vCPU in run-time state for initialization.\n");
4159 vcpu_unlock(&current_locked);
4160 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02004161 }
Olivier Deprezb2808332023-02-02 15:25:40 +01004162 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02004163
Olivier Deprezb2808332023-02-02 15:25:40 +01004164 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01004165 vm_locked.vm->secondary_ep = entry_point;
4166 vm_unlock(&vm_locked);
4167
Olivier Deprezb2808332023-02-02 15:25:40 +01004168 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01004169}
J-Alvesa0f317d2021-06-09 13:31:59 +01004170
J-Alves19e20cf2023-08-02 12:48:55 +01004171struct ffa_value api_ffa_notification_bitmap_create(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004172 ffa_vcpu_count_t vcpu_count,
4173 struct vcpu *current)
4174{
J-Alves7ccaccf2024-02-01 14:25:23 +00004175 const struct ffa_value ret =
Karl Meakin117c8082024-12-04 16:03:28 +00004176 ffa_notifications_is_bitmap_access_valid(current, vm_id);
J-Alves7ccaccf2024-02-01 14:25:23 +00004177
4178 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4179 dlog_verbose(
4180 "FFA_NOTIFICATION_BITMAP_CREATE to be used by "
4181 "hypervisor for valid NWd VM IDs only (%x).\n",
4182 vm_id);
4183 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004184 }
4185
Karl Meakin117c8082024-12-04 16:03:28 +00004186 return ffa_notifications_bitmap_create(vm_id, vcpu_count);
J-Alvesa0f317d2021-06-09 13:31:59 +01004187}
4188
J-Alves19e20cf2023-08-02 12:48:55 +01004189struct ffa_value api_ffa_notification_bitmap_destroy(ffa_id_t vm_id,
J-Alvesa0f317d2021-06-09 13:31:59 +01004190 struct vcpu *current)
4191{
J-Alves7ccaccf2024-02-01 14:25:23 +00004192 const struct ffa_value ret =
Karl Meakin117c8082024-12-04 16:03:28 +00004193 ffa_notifications_is_bitmap_access_valid(current, vm_id);
J-Alves7ccaccf2024-02-01 14:25:23 +00004194
4195 if (ffa_func_id(ret) != FFA_SUCCESS_32) {
4196 dlog_verbose(
4197 "FFA_NOTIFICATION_BITMAP_DESTROY to be used by "
4198 "hypervisor for valid NWd VM IDs only (%x).\n",
4199 vm_id);
4200 return ret;
J-Alvesa0f317d2021-06-09 13:31:59 +01004201 }
4202
Karl Meakin117c8082024-12-04 16:03:28 +00004203 return ffa_notifications_bitmap_destroy(vm_id);
J-Alvesa0f317d2021-06-09 13:31:59 +01004204}
J-Alvesc003a7a2021-03-18 13:06:53 +00004205
4206struct ffa_value api_ffa_notification_update_bindings(
J-Alves19e20cf2023-08-02 12:48:55 +01004207 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesc003a7a2021-03-18 13:06:53 +00004208 ffa_notifications_bitmap_t notifications, bool is_bind,
4209 struct vcpu *current)
4210{
4211 struct ffa_value ret = {.func = FFA_SUCCESS_32};
4212 struct vm_locked receiver_locked;
4213 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
J-Alves19e20cf2023-08-02 12:48:55 +01004214 const ffa_id_t id_to_update = is_bind ? sender_vm_id : HF_INVALID_VM_ID;
4215 const ffa_id_t id_to_validate =
J-Alvesc003a7a2021-03-18 13:06:53 +00004216 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01004217 const uint32_t flags_mbz =
4218 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
4219
4220 if ((flags_mbz & flags) != 0U) {
4221 return ffa_error(FFA_INVALID_PARAMETERS);
4222 }
J-Alvesc003a7a2021-03-18 13:06:53 +00004223
Karl Meakin117c8082024-12-04 16:03:28 +00004224 if (!ffa_notifications_is_bind_valid(current, sender_vm_id,
4225 receiver_vm_id)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004226 dlog_verbose("Invalid use of notifications bind interface.\n");
4227 return ffa_error(FFA_INVALID_PARAMETERS);
4228 }
4229
Karl Meakin117c8082024-12-04 16:03:28 +00004230 if (ffa_notifications_update_bindings_forward(
J-Alvesb15e9402021-09-08 11:44:42 +01004231 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
4232 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01004233 return ret;
4234 }
4235
J-Alvesc003a7a2021-03-18 13:06:53 +00004236 if (notifications == 0U) {
Karl Meakine8937d92024-03-19 16:04:25 +00004237 dlog_verbose("No notifications have been specified %lx.\n",
J-Alves1512acc2024-02-01 16:57:10 +00004238 notifications);
J-Alvesc003a7a2021-03-18 13:06:53 +00004239 return ffa_error(FFA_INVALID_PARAMETERS);
4240 }
4241
4242 /**
4243 * This check assumes receiver is the current VM, and has been enforced
Karl Meakin117c8082024-12-04 16:03:28 +00004244 * by 'ffa_notifications_is_bind_valid'.
J-Alvesc003a7a2021-03-18 13:06:53 +00004245 */
Karl Meakin117c8082024-12-04 16:03:28 +00004246 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004247
4248 if (receiver_locked.vm == NULL) {
4249 dlog_verbose("Receiver doesn't exist!\n");
J-Alves7333dcf2024-02-01 17:25:10 +00004250 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alvesc003a7a2021-03-18 13:06:53 +00004251 }
4252
J-Alves3e55c4d2024-10-09 14:24:07 +01004253 if (receiver_locked.vm->ffa_version < FFA_VERSION_1_1) {
4254 dlog_verbose(
4255 "%s: caller (%x) version should be GE to FF-A v1.1.\n",
4256 __func__, receiver_locked.vm->id);
4257 ret = ffa_error(FFA_NOT_SUPPORTED);
4258 goto out;
4259 }
4260
J-Alves09ff9d82021-11-02 11:55:20 +00004261 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00004262 dlog_verbose("Notifications are not enabled.\n");
4263 ret = ffa_error(FFA_NOT_SUPPORTED);
4264 goto out;
4265 }
4266
4267 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
4268 vm_find(sender_vm_id) == NULL) {
4269 dlog_verbose("Sender VM does not exist!\n");
4270 ret = ffa_error(FFA_INVALID_PARAMETERS);
4271 goto out;
4272 }
4273
4274 /*
4275 * Can't bind/unbind notifications if at least one is bound to a
4276 * different sender.
4277 */
4278 if (!vm_notifications_validate_bound_sender(
J-Alves661e1b72023-08-02 13:39:40 +01004279 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_validate,
4280 notifications)) {
J-Alvesf0208142024-02-01 17:28:58 +00004281 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004282 "Sender %x not permitted to set notifications %lx to "
J-Alvesf0208142024-02-01 17:28:58 +00004283 "%x.\n",
4284 sender_vm_id, notifications, receiver_vm_id);
J-Alvesc003a7a2021-03-18 13:06:53 +00004285 ret = ffa_error(FFA_DENIED);
4286 goto out;
4287 }
4288
4289 /**
4290 * Check if there is a pending notification within those specified in
4291 * the bitmap.
4292 */
4293 if (vm_are_notifications_pending(receiver_locked,
J-Alves661e1b72023-08-02 13:39:40 +01004294 ffa_is_vm_id(sender_vm_id),
J-Alvesc003a7a2021-03-18 13:06:53 +00004295 notifications)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004296 dlog_verbose("Notifications within '%lx' pending.\n",
J-Alvesc003a7a2021-03-18 13:06:53 +00004297 notifications);
4298 ret = ffa_error(FFA_DENIED);
4299 goto out;
4300 }
4301
4302 vm_notifications_update_bindings(
J-Alves661e1b72023-08-02 13:39:40 +01004303 receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_update,
J-Alvesc003a7a2021-03-18 13:06:53 +00004304 notifications, is_per_vcpu && is_bind);
4305
4306out:
4307 vm_unlock(&receiver_locked);
4308 return ret;
4309}
J-Alvesaa79c012021-07-09 14:29:45 +01004310
4311struct ffa_value api_ffa_notification_set(
J-Alves19e20cf2023-08-02 12:48:55 +01004312 ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags,
J-Alvesaa79c012021-07-09 14:29:45 +01004313 ffa_notifications_bitmap_t notifications, struct vcpu *current)
4314{
4315 struct ffa_value ret;
4316 struct vm_locked receiver_locked;
J-Alvesaa79c012021-07-09 14:29:45 +01004317 /*
4318 * Check if is per-vCPU or global, and extracting vCPU ID according
4319 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
4320 */
4321 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
4322 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
J-Alves6b754a12024-02-14 14:26:30 +00004323 const uint32_t flags_mbz =
4324 ~(FFA_NOTIFICATIONS_FLAG_PER_VCPU |
4325 FFA_NOTIFICATIONS_FLAG_DELAY_SRI | (0xFFFFU << 16));
J-Alves41c5da32024-06-19 10:05:05 +01004326 const bool delay_sri = (FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) != 0U;
J-Alves6b754a12024-02-14 14:26:30 +00004327
4328 if ((flags_mbz & flags) != 0U) {
4329 dlog_verbose("%s: caller shouldn't set bits that MBZ.\n",
4330 __func__);
4331 return ffa_error(FFA_INVALID_PARAMETERS);
4332 }
J-Alvesaa79c012021-07-09 14:29:45 +01004333
J-Alves30ac91d2024-02-14 15:33:12 +00004334 /* Global notifications must target any vCPU. */
4335 if (!is_per_vcpu && vcpu_id != 0U) {
4336 dlog_verbose(
4337 "For global notifications vCPU ID MBZ in call to set "
4338 "notifications.\n");
4339 return ffa_error(FFA_INVALID_PARAMETERS);
4340 }
4341
Karl Meakin117c8082024-12-04 16:03:28 +00004342 if (!ffa_notifications_is_set_valid(current, sender_vm_id,
4343 receiver_vm_id)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004344 dlog_verbose("Invalid use of notifications set interface.\n");
4345 return ffa_error(FFA_INVALID_PARAMETERS);
4346 }
4347
4348 if (notifications == 0U) {
4349 dlog_verbose("No notifications have been specified.\n");
4350 return ffa_error(FFA_INVALID_PARAMETERS);
4351 }
4352
J-Alves41c5da32024-06-19 10:05:05 +01004353 /*
4354 * The 'Delay Schedule Receiver interrupt flag' only applies to the
4355 * secure virtual FF-A instance.
4356 */
4357 if (!vm_id_is_current_world(sender_vm_id) && delay_sri) {
4358 dlog_verbose(
4359 "The delay SRI flag can only be set at the secure "
4360 "virtual FF-A instance.\n");
4361 return ffa_error(FFA_INVALID_PARAMETERS);
4362 }
4363
Karl Meakin117c8082024-12-04 16:03:28 +00004364 if (ffa_notifications_set_forward(sender_vm_id, receiver_vm_id, flags,
4365 notifications, &ret)) {
J-Alvesde7bd2f2021-09-09 19:54:35 +01004366 return ret;
4367 }
4368
J-Alvesaa79c012021-07-09 14:29:45 +01004369 /*
4370 * This check assumes receiver is the current VM, and has been enforced
Karl Meakin117c8082024-12-04 16:03:28 +00004371 * by 'ffa_notifications_is_set_valid'.
J-Alvesaa79c012021-07-09 14:29:45 +01004372 */
Karl Meakin117c8082024-12-04 16:03:28 +00004373 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesaa79c012021-07-09 14:29:45 +01004374
4375 if (receiver_locked.vm == NULL) {
4376 dlog_verbose("Receiver ID is not valid.\n");
4377 return ffa_error(FFA_INVALID_PARAMETERS);
4378 }
4379
J-Alves09ff9d82021-11-02 11:55:20 +00004380 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004381 dlog_verbose("Receiver's notifications not enabled.\n");
4382 ret = ffa_error(FFA_DENIED);
4383 goto out;
4384 }
4385
4386 /*
J-Alves89c22132024-02-14 15:44:41 +00004387 * Check the if the notifications are bound as global if per-vCPU flag
4388 * is set, or if they are bound as per-vCPU and caller setting as
4389 * global. In either case, return FFA_INVALID_PARAMETERS.
4390 */
4391 if (vm_notifications_validate_binding(
4392 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4393 notifications, !is_per_vcpu)) {
Karl Meakine8937d92024-03-19 16:04:25 +00004394 dlog_verbose("Notifications in %lx are %s\n", notifications,
J-Alves89c22132024-02-14 15:44:41 +00004395 !is_per_vcpu ? "global" : "per-vCPU");
4396 ret = ffa_error(FFA_INVALID_PARAMETERS);
4397 goto out;
4398 }
4399
4400 /*
J-Alvesaa79c012021-07-09 14:29:45 +01004401 * If notifications are not bound to the sender, they wouldn't be
4402 * enabled either for the receiver.
4403 */
4404 if (!vm_notifications_validate_binding(
J-Alves661e1b72023-08-02 13:39:40 +01004405 receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id,
4406 notifications, is_per_vcpu)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004407 dlog_verbose("Notifications bindings not valid.\n");
4408 ret = ffa_error(FFA_DENIED);
4409 goto out;
4410 }
4411
4412 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
4413 dlog_verbose("Invalid VCPU ID!\n");
4414 ret = ffa_error(FFA_INVALID_PARAMETERS);
4415 goto out;
4416 }
4417
J-Alves7461ef22021-10-18 17:21:33 +01004418 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00004419 vm_notifications_partition_set_pending(
J-Alves661e1b72023-08-02 13:39:40 +01004420 receiver_locked, ffa_is_vm_id(sender_vm_id), notifications,
J-Alves5a16c962022-03-25 12:32:51 +00004421 vcpu_id, is_per_vcpu);
4422
Karl Meakine8937d92024-03-19 16:04:25 +00004423 dlog_verbose("Set the notifications: %lx.\n", notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004424
J-Alves41c5da32024-06-19 10:05:05 +01004425 if (!delay_sri) {
J-Alves13394022021-06-30 13:48:49 +01004426 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
4427 vcpu_index(current));
Karl Meakin117c8082024-12-04 16:03:28 +00004428 ffa_notifications_sri_trigger_not_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004429 } else {
Karl Meakin117c8082024-12-04 16:03:28 +00004430 ffa_notifications_sri_set_delayed(current->cpu);
J-Alves13394022021-06-30 13:48:49 +01004431 }
J-Alvesaa79c012021-07-09 14:29:45 +01004432
J-Alves13394022021-06-30 13:48:49 +01004433 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01004434out:
4435 vm_unlock(&receiver_locked);
4436
4437 return ret;
4438}
4439
4440static struct ffa_value api_ffa_notification_get_success_return(
4441 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
4442 ffa_notifications_bitmap_t from_framework)
4443{
4444 return (struct ffa_value){
4445 .func = FFA_SUCCESS_32,
4446 .arg1 = 0U,
4447 .arg2 = (uint32_t)from_sp,
4448 .arg3 = (uint32_t)(from_sp >> 32),
4449 .arg4 = (uint32_t)from_vm,
4450 .arg5 = (uint32_t)(from_vm >> 32),
4451 .arg6 = (uint32_t)from_framework,
4452 .arg7 = (uint32_t)(from_framework >> 32),
4453 };
4454}
4455
J-Alves19e20cf2023-08-02 12:48:55 +01004456struct ffa_value api_ffa_notification_get(ffa_id_t receiver_vm_id,
J-Alvesaa79c012021-07-09 14:29:45 +01004457 ffa_vcpu_index_t vcpu_id,
4458 uint32_t flags, struct vcpu *current)
4459{
J-Alves663682a2022-03-25 13:56:51 +00004460 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01004461 ffa_notifications_bitmap_t sp_notifications = 0;
4462 ffa_notifications_bitmap_t vm_notifications = 0;
4463 struct vm_locked receiver_locked;
4464 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01004465 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
4466 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
4467 FFA_NOTIFICATION_FLAG_BITMAP_SP |
4468 FFA_NOTIFICATION_FLAG_BITMAP_VM);
4469
4470 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
4471 if ((flags & flags_mbz) != 0U) {
4472 dlog_verbose(
4473 "Invalid flags bit(s) set in notifications get. [31:4] "
4474 "MBZ(%x)\n",
4475 flags);
4476 return ffa_error(FFA_INVALID_PARAMETERS);
4477 }
J-Alvesaa79c012021-07-09 14:29:45 +01004478
4479 /*
J-Alvesfc95a302022-04-22 14:18:23 +01004480 * Following check should capture wrong uses of the interface,
4481 * depending on whether Hafnium is SPMC or hypervisor. On the
4482 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01004483 */
Karl Meakin117c8082024-12-04 16:03:28 +00004484 if (!ffa_notifications_is_get_valid(current, receiver_vm_id, flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01004485 dlog_verbose("Invalid use of notifications get interface.\n");
4486 return ffa_error(FFA_INVALID_PARAMETERS);
4487 }
4488
4489 /*
4490 * This check assumes receiver is the current VM, and has been enforced
4491 * by `plat_ffa_is_notifications_get_valid`.
4492 */
Karl Meakin117c8082024-12-04 16:03:28 +00004493 receiver_locked = ffa_vm_find_locked(receiver_vm_id);
J-Alvesaa79c012021-07-09 14:29:45 +01004494
4495 /*
4496 * `plat_ffa_is_notifications_get_valid` ensures following is never
4497 * true.
4498 */
4499 CHECK(receiver_locked.vm != NULL);
4500
J-Alves60458812024-03-22 11:57:10 +00004501 if (receiver_locked.vm->vcpu_count <= vcpu_id) {
J-Alves1abb3342022-01-05 11:59:10 +00004502 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004503 "Invalid VCPU ID %u. vcpu count %u current core: "
4504 "%zu!\n",
J-Alves1abb3342022-01-05 11:59:10 +00004505 vcpu_id, receiver_locked.vm->vcpu_count,
4506 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01004507 ret = ffa_error(FFA_INVALID_PARAMETERS);
4508 goto out;
4509 }
4510
4511 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
Karl Meakin117c8082024-12-04 16:03:28 +00004512 ret = ffa_notifications_get_from_sp(receiver_locked, vcpu_id,
4513 &sp_notifications);
Karl Meakinf9c73ce2024-07-30 17:37:13 +01004514 if (ret.func == FFA_ERROR_32) {
J-Alves98ff9562021-09-09 14:39:41 +01004515 dlog_verbose("Failed to get notifications from sps.");
4516 goto out;
4517 }
J-Alvesaa79c012021-07-09 14:29:45 +01004518 }
4519
4520 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00004521 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01004522 receiver_locked, true, vcpu_id);
4523 }
4524
J-Alvesd605a092022-03-28 14:20:48 +01004525 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
4526 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
Karl Meakin117c8082024-12-04 16:03:28 +00004527 ret = ffa_notifications_get_framework_notifications(
Karl Meakinf9c73ce2024-07-30 17:37:13 +01004528 receiver_locked, &framework_notifications, flags,
4529 vcpu_id);
4530 if (ret.func == FFA_ERROR_32) {
J-Alvesd605a092022-03-28 14:20:48 +01004531 dlog_verbose(
4532 "Failed to get notifications from "
4533 "framework.\n");
4534 goto out;
4535 }
4536 }
4537
J-Alves663682a2022-03-25 13:56:51 +00004538 ret = api_ffa_notification_get_success_return(
4539 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01004540
J-Alves6e2abc62021-12-02 14:58:56 +00004541 if (!receiver_locked.vm->el0_partition &&
4542 !vm_are_global_notifications_pending(receiver_locked)) {
4543 vm_notifications_set_npi_injected(receiver_locked, false);
4544 }
4545
J-Alvesaa79c012021-07-09 14:29:45 +01004546out:
4547 vm_unlock(&receiver_locked);
4548
4549 return ret;
4550}
J-Alvesc8e8a222021-06-08 17:33:52 +01004551
4552/**
4553 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
4554 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
4555 */
4556static struct ffa_value api_ffa_notification_info_get_success_return(
4557 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01004558 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01004559{
4560 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
4561
4562 /*
4563 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
4564 * hold the list of ids.
4565 */
4566 memcpy_s(&ret.arg3,
4567 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
4568 sizeof(ids[0]) * ids_count);
4569
4570 /*
4571 * According to the spec x2 should have:
4572 * - Bit flagging if there are more notifications pending;
4573 * - The total number of elements (i.e. total list size);
4574 * - The number of VCPU IDs within each VM specific list.
4575 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004576 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
4577 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
4578 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01004579
4580 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
4581 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
4582
4583 for (unsigned int i = 0; i < lists_count; i++) {
4584 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
4585 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
4586 }
4587
4588 return ret;
4589}
4590
4591struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
4592{
4593 /*
4594 * Following set of variables should be populated with the return info.
4595 * At a successfull handling of this interface, they should be used
4596 * to populate the 'ret' structure in accordance to the table 17.29
4597 * of the FF-A v1.1 Beta0 specification.
4598 */
4599 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
4600 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
4601 uint32_t lists_count = 0;
4602 uint32_t ids_count = 0;
4603 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01004604 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004605
4606 /*
4607 * This interface can only be called at NS virtual/physical FF-A
4608 * instance by the endpoint implementing the primary scheduler and the
4609 * Hypervisor/OS kernel.
4610 * In the SPM, following check passes if call has been forwarded from
4611 * the hypervisor.
4612 */
Karl Meakin5e996992024-05-20 11:27:07 +01004613
4614 if (!vm_is_primary(current->vm)) {
J-Alvesc8e8a222021-06-08 17:33:52 +01004615 dlog_verbose(
4616 "Only the receiver's scheduler can use this "
4617 "interface\n");
4618 return ffa_error(FFA_NOT_SUPPORTED);
4619 }
4620
J-Alvesca058c22021-09-10 14:02:07 +01004621 /*
4622 * Forward call to the other world, and fill the arrays used to assemble
4623 * return.
4624 */
Karl Meakin117c8082024-12-04 16:03:28 +00004625 ffa_notifications_info_get_forward(ids, &ids_count, lists_sizes,
4626 &lists_count,
4627 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
J-Alvesca058c22021-09-10 14:02:07 +01004628
4629 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
4630
J-Alvesc8e8a222021-06-08 17:33:52 +01004631 /* Get notifications' info from this world */
4632 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
4633 ++index) {
4634 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
4635
4636 list_is_full = vm_notifications_info_get(
4637 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
4638 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4639
4640 vm_unlock(&vm_locked);
4641 }
4642
4643 if (!list_is_full) {
4644 /* Grab notifications info from other world */
Karl Meakin117c8082024-12-04 16:03:28 +00004645 ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01004646 ids, &ids_count, lists_sizes, &lists_count,
4647 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4648 }
4649
4650 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01004651 dlog_verbose(
4652 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01004653 result = ffa_error(FFA_NO_DATA);
4654 } else {
4655 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01004656 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01004657 }
4658
J-Alves13394022021-06-30 13:48:49 +01004659 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004660}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004661
4662struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
4663{
4664 struct vm_locked vm_locked;
4665 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
4666 bool mode_ret = false;
4667 uint32_t mode = 0;
4668
Karl Meakin117c8082024-12-04 16:03:28 +00004669 if (!ffa_memory_is_mem_perm_get_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004670 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004671 }
4672
4673 if (!(current->vm->el0_partition)) {
4674 return ffa_error(FFA_DENIED);
4675 }
4676
4677 vm_locked = vm_lock(current->vm);
4678
4679 /*
4680 * mm_get_mode is used to check if the given base_addr page is already
4681 * mapped. If the page is unmapped, return error. If the page is mapped
4682 * appropriate attributes are returned to the caller. Note that
4683 * mm_get_mode returns true if the address is in the valid VA range as
4684 * supported by the architecture and MMU configurations, as opposed to
4685 * whether a page is mapped or not. For a page to be known as mapped,
4686 * the API must return true AND the returned mode must not have
4687 * MM_MODE_INVALID set.
4688 */
4689 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4690 va_add(base_addr, PAGE_SIZE), &mode);
4691 if (!mode_ret || (mode & MM_MODE_INVALID)) {
4692 ret = ffa_error(FFA_INVALID_PARAMETERS);
4693 goto out;
4694 }
4695
4696 /* No memory should be marked RWX */
4697 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
4698 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
4699
4700 /*
4701 * S-EL0 partitions are expected to have all their pages marked as
4702 * non-global.
4703 */
4704 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
4705 (MM_MODE_NG | MM_MODE_USER));
4706
4707 if (mode & MM_MODE_W) {
4708 /* No memory should be writeable but not readable. */
4709 CHECK(mode & MM_MODE_R);
4710 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4711 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
4712 } else if (mode & MM_MODE_R) {
4713 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4714 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
4715 if (!(mode & MM_MODE_X)) {
4716 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
4717 }
4718 }
4719out:
4720 vm_unlock(&vm_locked);
4721 return ret;
4722}
4723
4724struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
4725 uint32_t mem_perm, struct vcpu *current)
4726{
4727 struct vm_locked vm_locked;
4728 struct ffa_value ret;
4729 bool mode_ret = false;
4730 uint32_t original_mode;
4731 uint32_t new_mode;
4732 struct mpool local_page_pool;
4733
Karl Meakin117c8082024-12-04 16:03:28 +00004734 if (!ffa_memory_is_mem_perm_set_valid(current)) {
J-Alves5a099172024-02-22 14:34:51 +00004735 return ffa_error(FFA_DENIED);
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004736 }
4737
4738 if (!(current->vm->el0_partition)) {
4739 return ffa_error(FFA_DENIED);
4740 }
4741
4742 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
4743 return ffa_error(FFA_INVALID_PARAMETERS);
4744 }
4745
4746 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
4747 (mem_perm != FFA_MEM_PERM_RX)) {
4748 return ffa_error(FFA_INVALID_PARAMETERS);
4749 }
4750
4751 /*
4752 * Create a local pool so any freed memory can't be used by another
4753 * thread. This is to ensure the original mapping can be restored if any
4754 * stage of the process fails.
4755 */
4756 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
4757
4758 vm_locked = vm_lock(current->vm);
4759
4760 /*
4761 * All regions accessible by the partition are mapped during boot. If we
4762 * cannot get a successful translation for the page range, the request
4763 * to change permissions is rejected.
4764 * mm_get_mode is used to check if the given address range is already
4765 * mapped. If the range is unmapped, return error. If the range is
4766 * mapped appropriate attributes are returned to the caller. Note that
4767 * mm_get_mode returns true if the address is in the valid VA range as
4768 * supported by the architecture and MMU configurations, as opposed to
4769 * whether a page is mapped or not. For a page to be known as mapped,
4770 * the API must return true AND the returned mode must not have
4771 * MM_MODE_INVALID set.
4772 */
4773
4774 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4775 va_add(base_addr, page_count * PAGE_SIZE),
4776 &original_mode);
4777 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
4778 ret = ffa_error(FFA_INVALID_PARAMETERS);
4779 goto out;
4780 }
4781
4782 /* Device memory cannot be marked as executable */
4783 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
4784 ret = ffa_error(FFA_INVALID_PARAMETERS);
4785 goto out;
4786 }
4787
4788 new_mode = MM_MODE_USER | MM_MODE_NG;
4789
4790 if (mem_perm == FFA_MEM_PERM_RW) {
4791 new_mode |= MM_MODE_R | MM_MODE_W;
4792 } else if (mem_perm == FFA_MEM_PERM_RX) {
4793 new_mode |= MM_MODE_R | MM_MODE_X;
4794 } else if (mem_perm == FFA_MEM_PERM_RO) {
4795 new_mode |= MM_MODE_R;
4796 }
4797
4798 /*
4799 * Safe to re-map memory, since we know the requested permissions are
4800 * valid, and the memory requested to be re-mapped is also valid.
4801 */
4802 if (!mm_identity_prepare(
4803 &vm_locked.vm->ptable, pa_from_va(base_addr),
4804 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4805 new_mode, &local_page_pool)) {
4806 /*
4807 * Defrag the table into the local page pool.
4808 * mm_identity_prepare could have allocated or freed pages to
4809 * split blocks or tables etc.
4810 */
4811 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
4812
4813 /*
4814 * Guaranteed to succeed mapping with old mode since the mapping
4815 * with old mode already existed and we have a local page pool
4816 * that should have sufficient memory to go back to the original
4817 * state.
4818 */
4819 CHECK(mm_identity_prepare(
4820 &vm_locked.vm->ptable, pa_from_va(base_addr),
4821 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4822 original_mode, &local_page_pool));
4823 mm_identity_commit(
4824 &vm_locked.vm->ptable, pa_from_va(base_addr),
4825 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4826 original_mode, &local_page_pool);
4827
4828 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
4829 ret = ffa_error(FFA_NO_MEMORY);
4830 goto out;
4831 }
4832
4833 mm_identity_commit(
4834 &vm_locked.vm->ptable, pa_from_va(base_addr),
4835 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
4836 &local_page_pool);
4837
4838 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
4839
4840out:
4841 mpool_fini(&local_page_pool);
4842 vm_unlock(&vm_locked);
4843
4844 return ret;
4845}
Maksims Svecovs71b76702022-05-20 15:32:58 +01004846
4847/**
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004848 * Send the contents of the given vCPU's log buffer to the log, preceded
Karl Meakin705b56e2024-05-22 10:04:32 +01004849 * by the VM ID and followed by a newline.
4850 */
Karl Meakin6f1f1212024-07-16 10:18:16 +01004851void api_flush_log_buffer(struct vcpu_locked *vcpu_locked)
Karl Meakin705b56e2024-05-22 10:04:32 +01004852{
Karl Meakin6f1f1212024-07-16 10:18:16 +01004853 /*
4854 * NOTE: This line is parsed by `hftest.py`.
4855 * If you change the format, make sure to update
4856 * `HFTEST_CTRL_JSON_REGEX` as well.
4857 */
4858 struct vcpu *vcpu = vcpu_locked->vcpu;
4859 struct log_buffer *buffer = &vcpu->log_buffer;
4860 ffa_id_t vm_id = vcpu->vm->id;
4861 ffa_id_t vcpu_id = vcpu_index(vcpu);
4862
Karl Meakin7de26952024-06-14 14:50:19 +01004863 buffer->chars[buffer->len] = '\0';
Olivier Deprezbd432092024-07-26 14:01:12 +02004864 dlog("[%x %u] %s\n", vm_id, vcpu_id, buffer->chars);
Karl Meakin7de26952024-06-14 14:50:19 +01004865 buffer->len = 0;
Karl Meakin705b56e2024-05-22 10:04:32 +01004866}
4867
4868/**
Karl Meakin740f74f2024-02-14 18:04:48 +00004869 * Implements FF-A v1.2 FFA_CONSOLE_LOG ABI for buffered logging.
Maksims Svecovs71b76702022-05-20 15:32:58 +01004870 */
4871struct ffa_value api_ffa_console_log(const struct ffa_value args,
4872 struct vcpu *current)
4873{
Karl Meakin740f74f2024-02-14 18:04:48 +00004874 /* Maximum number of characters is 128: 16 registers of 8 bytes each. */
4875 char chars[128] = {0};
Karl Meakin0e617d92024-04-05 12:55:22 +01004876 const bool v1_2 = current->vm->ffa_version >= FFA_VERSION_1_2;
Karl Meakin740f74f2024-02-14 18:04:48 +00004877 const bool log32 = args.func == FFA_CONSOLE_LOG_32;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004878
Karl Meakin740f74f2024-02-14 18:04:48 +00004879 /*
4880 * 32bit: always 6 registers
4881 * 64bit and less than v1.2: 6 registers
4882 * 64bit and v1.2 or greater: 16 registers
4883 */
Karl Meakin66a38bd2024-05-28 16:00:56 +01004884 /* NOLINTNEXTLINE(readability-avoid-nested-conditional-operator) */
Karl Meakin740f74f2024-02-14 18:04:48 +00004885 const size_t registers_max = log32 ? 6 : (v1_2 ? 16 : 6);
4886 const size_t chars_max =
4887 registers_max * (log32 ? sizeof(uint32_t) : sizeof(uint64_t));
4888 const size_t chars_count = args.arg1;
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004889 struct vcpu_locked vcpu_locked;
4890 struct log_buffer *log_buffer;
Karl Meakin740f74f2024-02-14 18:04:48 +00004891
4892 assert(args.func == FFA_CONSOLE_LOG_32 ||
4893 args.func == FFA_CONSOLE_LOG_64);
4894
4895 if (chars_count == 0 || chars_count > chars_max) {
Maksims Svecovs71b76702022-05-20 15:32:58 +01004896 return ffa_error(FFA_INVALID_PARAMETERS);
4897 }
4898
Karl Meakin740f74f2024-02-14 18:04:48 +00004899 if (log32) {
4900 uint32_t *registers = (uint32_t *)chars;
Maksims Svecovs71b76702022-05-20 15:32:58 +01004901
Karl Meakin740f74f2024-02-14 18:04:48 +00004902 registers[0] = args.arg2 & 0xffffffff;
4903 registers[1] = args.arg3 & 0xffffffff;
4904 registers[2] = args.arg4 & 0xffffffff;
4905 registers[3] = args.arg5 & 0xffffffff;
4906 registers[4] = args.arg6 & 0xffffffff;
4907 registers[5] = args.arg7 & 0xffffffff;
4908 } else {
4909 uint64_t *registers = (uint64_t *)chars;
4910
4911 registers[0] = args.arg2;
4912 registers[1] = args.arg3;
4913 registers[2] = args.arg4;
4914 registers[3] = args.arg5;
4915 registers[4] = args.arg6;
4916 registers[5] = args.arg7;
4917 if (v1_2) {
4918 registers[6] = args.extended_val.arg8;
4919 registers[7] = args.extended_val.arg9;
4920 registers[8] = args.extended_val.arg10;
4921 registers[9] = args.extended_val.arg11;
4922 registers[10] = args.extended_val.arg12;
4923 registers[11] = args.extended_val.arg13;
4924 registers[12] = args.extended_val.arg14;
4925 registers[13] = args.extended_val.arg15;
4926 registers[14] = args.extended_val.arg16;
4927 registers[15] = args.extended_val.arg17;
4928 }
4929 }
4930
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004931 vcpu_locked = vcpu_lock(current);
4932 log_buffer = &current->log_buffer;
Karl Meakin7de26952024-06-14 14:50:19 +01004933
Karl Meakin740f74f2024-02-14 18:04:48 +00004934 for (size_t i = 0; i < chars_count; i++) {
4935 bool flush = false;
4936 const char c = chars[i];
4937
4938 if (c == '\n' || c == '\0') {
4939 flush = true;
4940 } else {
Karl Meakin7de26952024-06-14 14:50:19 +01004941 log_buffer->chars[log_buffer->len++] = c;
4942 flush = log_buffer->len == LOG_BUFFER_SIZE;
Karl Meakin740f74f2024-02-14 18:04:48 +00004943 }
4944
4945 if (flush) {
Karl Meakin6f1f1212024-07-16 10:18:16 +01004946 api_flush_log_buffer(&vcpu_locked);
Karl Meakin740f74f2024-02-14 18:04:48 +00004947 }
4948 }
Maksims Svecovs71b76702022-05-20 15:32:58 +01004949
Karl Meakinc5cebbc2024-06-17 11:30:27 +01004950 vcpu_unlock(&vcpu_locked);
Maksims Svecovs71b76702022-05-20 15:32:58 +01004951 return (struct ffa_value){.func = FFA_SUCCESS_32};
4952}
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004953
4954/**
4955 * Send an IPI interrupt to a target vcpu belonging to the
4956 * sender that isn't itself.
4957 */
Daniel Boulby84d49b62024-11-04 18:25:59 +00004958int64_t api_hf_interrupt_send_ipi(uint32_t target_vcpu_id, struct vcpu *current)
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004959{
4960 struct vm *vm = current->vm;
4961 ffa_vcpu_index_t target_vcpu_index = vcpu_id_to_index(target_vcpu_id);
4962
Daniel Boulby84d49b62024-11-04 18:25:59 +00004963 if (target_vcpu_index >= vm->vcpu_count ||
Daniel Boulbyf3cf28c2024-08-22 10:46:23 +01004964 target_vcpu_index == cpu_index(current->cpu)) {
4965 dlog_verbose("Invalid vCPU %d for IPI.\n", target_vcpu_id);
4966 return -1;
4967 }
4968
4969 dlog_verbose("Injecting IPI to target vCPU%d for %#x\n", target_vcpu_id,
4970 vm->id);
4971
4972 hf_ipi_send_interrupt(vm, target_vcpu_index);
4973
4974 return 0;
4975}