blob: f65bb10437931522a4e8740fd88f8e3a604f123a [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"
Olivier Deprez96a2a262020-06-11 17:21:38 +020013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000016#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020017#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000018
Andrew Scull877ae4b2019-07-02 12:52:33 +010019#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000020#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010021#include "hf/ffa_internal.h"
22#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000023#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010024#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010025#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010027#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010028#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "hf/vm.h"
30
Andrew Scullf35a5c92018-08-07 18:09:46 +010031#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032#include "vmapi/hf/ffa.h"
J-Alves126ab502022-09-29 11:37:33 +010033#include "vmapi/hf/ffa_v1_0.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010034
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000035static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010036 "Partition information descriptor size doesn't match the one in "
37 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000038static_assert(sizeof(struct ffa_partition_info) == 24,
39 "Partition information descriptor size doesn't match the one in "
40 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -080041static_assert((sizeof(struct ffa_partition_info) & 7) == 0,
42 "Partition information descriptor must be a multiple of 8 bytes"
43 " for ffa_partition_info_get_regs to work correctly. Information"
44 " from this structure are returned in 8 byte registers and the"
45 " count of 8 byte registers is returned by the ABI.");
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000046/*
47 * To eliminate the risk of deadlocks, we define a partial order for the
48 * acquisition of locks held concurrently by the same physical CPU. Our current
49 * ordering requirements are as follows:
50 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010051 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000052 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010053 * Locks of the same kind require the lock of lowest address to be locked first,
54 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000055 */
56
Andrew Scullaa039b32018-10-04 15:02:26 +010057static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010058 "Currently, a page is mapped for the send and receive buffers so "
59 "the maximum request is the size of a page.");
60
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
62 "The page pool entry size must be at least as big as the mailbox "
63 "size, so that memory region descriptors can be copied from the "
64 "mailbox for memory sharing.");
65
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -080066/*
67 * Maximum ffa_partition_info entries that can be returned by an invocation
68 * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available
69 * registers/args in struct ffa_value divided by size of struct
70 * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e.
71 * 15 uint64_t fields. For FF-A v1.1, this value should be 5.
72 */
73#define MAX_INFO_REGS_ENTRIES_PER_CALL \
74 ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info))
75static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5,
76 "FF-A v1.1 supports no more than 5 entries"
77 " per FFA_PARTITION_INFO_GET_REGS64 calls");
78
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000079static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000080
81/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000082 * Initialises the API page pool by taking ownership of the contents of the
83 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000084 */
85void api_init(struct mpool *ppool)
86{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000087 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000088}
89
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010090/**
J-Alvesad6a0432021-04-09 16:06:21 +010091 * Get target VM vCPU:
92 * If VM is UP then return first vCPU.
93 * If VM is MP then return vCPU whose index matches current CPU index.
94 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050095struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +010096{
97 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
98 struct vcpu *vcpu = NULL;
99
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500100 CHECK((vm != NULL) && (current != NULL));
101
J-Alvesad6a0432021-04-09 16:06:21 +0100102 if (vm->vcpu_count == 1) {
103 vcpu = vm_get_vcpu(vm, 0);
104 } else if (current_cpu_index < vm->vcpu_count) {
105 vcpu = vm_get_vcpu(vm, current_cpu_index);
106 }
107
108 return vcpu;
109}
110
111/**
J-Alvesfe7f7372020-11-09 11:32:12 +0000112 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
113 * is given as argument of the function.
114 *
115 * Called to change the context between SPs for direct messaging (when Hafnium
116 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
117 *
118 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100119 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000120 * - MP with pinned Execution Contexts.
121 */
122static struct vcpu *api_switch_to_vm(struct vcpu *current,
123 struct ffa_value to_ret,
124 enum vcpu_state vcpu_state,
125 ffa_vm_id_t to_id)
126{
127 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100128 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000129
130 CHECK(next != NULL);
131
132 /* Set the return value for the target VM. */
133 arch_regs_set_retval(&next->regs, to_ret);
134
135 /* Set the current vCPU state. */
136 sl_lock(&current->lock);
137 current->state = vcpu_state;
138 sl_unlock(&current->lock);
139
140 return next;
141}
142
143/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000144 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100145 *
146 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100147 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100148 */
J-Alves27b71962022-12-12 15:29:58 +0000149struct vcpu *api_switch_to_primary(struct vcpu *current,
150 struct ffa_value primary_ret,
151 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100152{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000153 /*
154 * If the secondary is blocked but has a timer running, sleep until the
155 * timer fires rather than indefinitely.
156 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100157 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100158 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
159 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100160 if (arch_timer_enabled_current()) {
161 uint64_t remaining_ns =
162 arch_timer_remaining_ns_current();
163
164 if (remaining_ns == 0) {
165 /*
166 * Timer is pending, so the current vCPU should
167 * be run again right away.
168 */
Andrew Walbran7e824602020-10-22 16:51:40 +0100169 primary_ret = (struct ffa_value){
170 .func = FFA_INTERRUPT_32};
171
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100172 } else {
173 primary_ret.arg2 = remaining_ns;
174 }
175 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100176 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100177 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000178 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100179 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000180
181 default:
182 /* Do nothing. */
183 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000184 }
185
J-Alvesfe7f7372020-11-09 11:32:12 +0000186 return api_switch_to_vm(current, primary_ret, secondary_state,
187 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100188}
189
190/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200191 * Choose next vCPU to run to be the counterpart vCPU in the other
192 * world (run the normal world if currently running in the secure
193 * world). Set current vCPU state to the given vcpu_state parameter.
194 * Set FF-A return values to the target vCPU in the other world.
195 *
196 * Called in context of a direct message response from a secure
197 * partition to a VM.
198 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100199struct vcpu *api_switch_to_other_world(struct vcpu *current,
200 struct ffa_value other_world_ret,
201 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200202{
J-Alvesfe7f7372020-11-09 11:32:12 +0000203 return api_switch_to_vm(current, other_world_ret, vcpu_state,
204 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200205}
206
207/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000208 * Returns true if the given vCPU is executing in context of an
209 * FFA_MSG_SEND_DIRECT_REQ invocation.
210 */
J-Alves27b71962022-12-12 15:29:58 +0000211bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000212{
213 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
214}
215
216/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100217 * Returns true if the VM owning the given vCPU is supporting managed exit and
218 * the vCPU is currently processing a managed exit.
219 */
220static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
221{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100222 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100223 vcpu_locked.vcpu->processing_managed_exit);
224}
225
226/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000227 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000228 */
229struct vcpu *api_preempt(struct vcpu *current)
230{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100231 struct ffa_value ret = {
232 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200233 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000234 };
235
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500236 return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED);
Andrew Scull33fecd32019-01-08 14:48:27 +0000237}
238
239/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000240 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000241 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100242 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100243struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100244{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100245 struct ffa_value ret = {
246 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
247 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100248 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000249
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000250 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100251 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100252}
253
254/**
Andrew Walbran33645652019-04-15 12:29:31 +0100255 * Puts the current vCPU in off mode, and returns to the primary VM.
256 */
257struct vcpu *api_vcpu_off(struct vcpu *current)
258{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100259 struct ffa_value ret = {
260 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
261 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100262 };
263
264 /*
265 * Disable the timer, so the scheduler doesn't get told to call back
266 * based on it.
267 */
268 arch_timer_disable_current();
269
270 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
271}
272
273/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500274 * The current vCPU is blocked on some resource and needs to relinquish
275 * control back to the execution context of the endpoint that originally
276 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000277 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000278struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000279{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000280 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
281 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500282 bool transition_allowed;
283 enum vcpu_state next_state = VCPU_STATE_BLOCKED;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000284
285 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000286 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000287 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000288 }
289
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000290 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500291 transition_allowed = plat_ffa_check_runtime_state_transition(
292 current, current->vm->id, HF_INVALID_VM_ID, NULL, FFA_YIELD_32,
293 &next_state);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000294 vcpu_unlock(&current_locked);
295
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500296 if (!transition_allowed) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000297 return ffa_error(FFA_DENIED);
298 }
299
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500300 assert(next_state == VCPU_STATE_BLOCKED);
301
Madhukar Pappireddy1f2f2132023-02-14 17:48:44 -0600302 plat_ffa_yield_prepare(current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000303 *next = api_switch_to_primary(
304 current,
305 (struct ffa_value){.func = FFA_YIELD_32,
306 .arg1 = ffa_vm_vcpu(current->vm->id,
307 vcpu_index(current))},
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500308 next_state);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000309
310 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000311}
312
313/**
Andrew Walbran33645652019-04-15 12:29:31 +0100314 * Switches to the primary so that it can switch to the target, or kick it if it
315 * is already running on a different physical CPU.
316 */
317struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
318{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100319 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100320 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100321 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
322 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100323 };
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500324 return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED);
Andrew Walbran33645652019-04-15 12:29:31 +0100325}
326
327/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000328 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000329 */
330struct vcpu *api_abort(struct vcpu *current)
331{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000333
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100334 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000335 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000336
337 if (current->vm->id == HF_PRIMARY_VM_ID) {
338 /* TODO: what to do when the primary aborts? */
339 for (;;) {
340 /* Do nothing. */
341 }
342 }
343
344 atomic_store_explicit(&current->vm->aborting, true,
345 memory_order_relaxed);
346
347 /* TODO: free resources once all vCPUs abort. */
348
Andrew Sculld6ee1102019-04-05 22:12:42 +0100349 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000350}
351
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000352/*
353 * Format the partition info descriptors according to the version supported
354 * by the endpoint and return the size of the array created.
355 */
356static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000357 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000358 uint32_t vm_count)
359{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000360 struct vm *vm = vm_locked.vm;
361 uint32_t version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100362 uint32_t partition_info_size;
363 uint32_t buffer_size;
Federico Recanati644f0462022-03-17 12:04:00 +0100364 struct ffa_value ret;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000365
J-Alvese8c8c2b2022-12-16 15:34:48 +0000366 /* Acquire receiver's RX buffer. */
367 if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) {
368 dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id);
369 return ret;
370 }
371
J-Alves122f1a12022-12-12 15:55:42 +0000372 if (vm_is_mailbox_busy(vm_locked)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000373 /*
374 * Can't retrieve memory information if the mailbox is not
375 * available.
376 */
377 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000378 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000379 }
380
Daniel Boulby191b5f02022-02-17 17:26:14 +0000381 if (version == MAKE_FFA_VERSION(1, 0)) {
382 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
383
Daniel Boulby835e1592022-05-30 17:38:51 +0100384 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
385 buffer_size = partition_info_size * vm_count;
386 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000387 dlog_error(
388 "Partition information does not fit in the "
389 "VM's RX "
390 "buffer.\n");
391 return ffa_error(FFA_NO_MEMORY);
392 }
393
394 for (uint32_t i = 0; i < vm_count; i++) {
395 /*
396 * Populate the VM's RX buffer with the partition
Kathleen Capella402fa852022-11-09 16:16:51 -0500397 * information. Clear properties bits that must be zero
398 * according to DEN0077A FF-A v1.0 REL Table 8.25.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000399 */
400 recv_mailbox[i].vm_id = partitions[i].vm_id;
401 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
Kathleen Capella402fa852022-11-09 16:16:51 -0500402 recv_mailbox[i].properties =
403 partitions[i].properties &
404 ~FFA_PARTITION_v1_0_RES_MASK;
Daniel Boulby191b5f02022-02-17 17:26:14 +0000405 }
406
407 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100408 partition_info_size = sizeof(struct ffa_partition_info);
409 buffer_size = partition_info_size * vm_count;
410 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000411 dlog_error(
412 "Partition information does not fit in the "
413 "VM's RX "
414 "buffer.\n");
415 return ffa_error(FFA_NO_MEMORY);
416 }
417
418 /* Populate the VM's RX buffer with the partition information.
419 */
Daniel Boulby835e1592022-05-30 17:38:51 +0100420 memcpy_s(vm->mailbox.recv, HF_MAILBOX_SIZE, partitions,
421 buffer_size);
Daniel Boulby191b5f02022-02-17 17:26:14 +0000422 }
423
Daniel Boulby835e1592022-05-30 17:38:51 +0100424 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000425
426 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000427 vm->mailbox.recv_sender = HF_VM_ID_BASE;
428 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000429 vm->mailbox.state = MAILBOX_STATE_FULL;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000430
Daniel Boulby835e1592022-05-30 17:38:51 +0100431 /*
432 * Return the count of partition information descriptors in w2
433 * and the size of the descriptors in w3.
434 */
435 return (struct ffa_value){.func = FFA_SUCCESS_32,
436 .arg2 = vm_count,
437 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000438}
439
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800440static void api_ffa_fill_partitions_info_array(
441 struct ffa_partition_info *partitions, size_t partitions_len,
442 const struct ffa_uuid *uuid, bool count_flag, ffa_vm_id_t vm_id,
443 ffa_vm_count_t *vm_count_out)
444{
445 ffa_vm_count_t vm_count = 0;
446 bool uuid_is_null = ffa_uuid_is_null(uuid);
447
448 assert(vm_get_count() <= partitions_len);
449
450 /*
451 * Iterate through the VMs to find the ones with a matching
452 * UUID. A Null UUID retrieves information for all VMs.
453 */
454 for (ffa_vm_count_t index = 0; index < vm_get_count(); ++index) {
455 struct vm *vm = vm_find_index(index);
456
457 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
458 uint16_t array_index = vm_count;
459
460 ++vm_count;
461 if (count_flag) {
462 continue;
463 }
464
465 partitions[array_index].vm_id = vm->id;
466 partitions[array_index].vcpu_count = vm->vcpu_count;
467 partitions[array_index].properties =
468 plat_ffa_partition_properties(vm_id, vm);
469 partitions[array_index].properties |=
470 vm_are_notifications_enabled(vm)
471 ? FFA_PARTITION_NOTIFICATION
472 : 0;
473 partitions[array_index].properties |=
474 FFA_PARTITION_AARCH64_EXEC;
475 if (uuid_is_null) {
476 partitions[array_index].uuid = vm->uuid;
477 }
478 }
479 }
480
481 *vm_count_out = vm_count;
482}
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800483
484static inline void api_ffa_pack_vmid_count_props(
485 uint64_t *xn, ffa_vm_id_t vm_id, ffa_vcpu_count_t vcpu_count,
486 ffa_partition_properties_t properties)
487{
488 *xn = (uint64_t)vm_id;
489 *xn |= (uint64_t)vcpu_count << 16;
490 *xn |= (uint64_t)properties << 32;
491}
492
493static inline void api_ffa_pack_uuid(uint64_t *xn_1, uint64_t *xn_2,
494 struct ffa_uuid *uuid)
495{
496 *xn_1 = (uint64_t)uuid->uuid[0];
497 *xn_1 |= (uint64_t)uuid->uuid[1] << 32;
498 *xn_2 = (uint64_t)uuid->uuid[2];
499 *xn_2 |= (uint64_t)uuid->uuid[3] << 32;
500}
501
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800502struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current,
503 const struct ffa_uuid *uuid,
504 const uint16_t start_index,
505 const uint16_t tag)
506{
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800507 struct vm *current_vm = current->vm;
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800508 static struct ffa_partition_info partitions[2 * MAX_VMS];
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800509 bool uuid_is_null = ffa_uuid_is_null(uuid);
510 ffa_vm_count_t vm_count = 0;
511 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
512 uint16_t max_idx = 0;
513 uint16_t curr_idx = 0;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800514 uint8_t num_entries_to_ret = 0;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800515 uint8_t arg_idx = 3;
516
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800517 /* list of pointers to args in return value */
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800518 uint64_t *arg_ptrs[] = {
519 &(ret).func,
520 &(ret).arg1,
521 &(ret).arg2,
522 &(ret).arg3,
523 &(ret).arg4,
524 &(ret).arg5,
525 &(ret).arg6,
526 &(ret).arg7,
527 &(ret).extended_val.arg8,
528 &(ret).extended_val.arg9,
529 &(ret).extended_val.arg10,
530 &(ret).extended_val.arg11,
531 &(ret).extended_val.arg12,
532 &(ret).extended_val.arg13,
533 &(ret).extended_val.arg14,
534 &(ret).extended_val.arg15,
535 &(ret).extended_val.arg16,
536 &(ret).extended_val.arg17,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800537 };
538
539 /* TODO: Add support for using tags */
540 if (tag != 0) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800541 dlog_error("Tag not 0. Unsupported tag. %d\n", tag);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800542 return ffa_error(FFA_RETRY);
543 }
544
545 memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions));
546
547 api_ffa_fill_partitions_info_array(partitions, ARRAY_SIZE(partitions),
548 uuid, false, current_vm->id,
549 &vm_count);
550
551 /* If UUID is Null vm_count must not be zero at this stage. */
552 CHECK(!uuid_is_null || vm_count != 0);
553
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800554 /*
555 * When running the Hypervisor:
556 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
557 * it to fill with secure partitions information.
558 * TODO: Note that for this ABI, forwarding on every invocation when
559 * uuid is Null is inefficient,and if performance becomes a problem,
560 * this would be a good place to optimize using strategies such as
561 * caching info etc. For now, assuming this inefficiency is not a major
562 * issue.
563 * - If UUID is non-Null vm_count may be zero because the UUID matches
564 * a secure partition and the query is forwarded to the SPMC.
565 * When running the SPMC:
566 * - If UUID is non-Null and vm_count is zero it means there is no such
567 * partition identified in the system.
568 */
569 if (!plat_ffa_partition_info_get_regs_forward(
570 uuid, start_index, tag, partitions, ARRAY_SIZE(partitions),
571 &vm_count)) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800572 dlog_error("Failed to forward ffa_partition_info_get_regs.\n");
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800573 return ffa_error(FFA_DENIED);
574 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800575
576 /*
577 * Unrecognized UUID: does not match any of the VMs (or SPs)
578 * and is not Null.
579 */
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800580 if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800581 dlog_error(
582 "Invalid parameters. vm_count = %d (must not be zero "
583 "or > %d)\n",
584 vm_count, ARRAY_SIZE(partitions));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800585 return ffa_error(FFA_INVALID_PARAMETERS);
586 }
587
588 if (start_index >= vm_count) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800589 dlog_error(
590 "start index = %d vm_count = %d (start_index must be "
591 "less than vm_count)\n",
592 start_index, vm_count);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800593 return ffa_error(FFA_INVALID_PARAMETERS);
594 }
595
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800596 max_idx = vm_count - 1;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800597 num_entries_to_ret = (max_idx - start_index) + 1;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800598 num_entries_to_ret =
599 MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800600 curr_idx = start_index + num_entries_to_ret - 1;
601 assert(curr_idx <= max_idx);
602
603 ret.func = FFA_SUCCESS_64;
604 ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48;
605 ret.arg2 |= curr_idx << 16;
606 ret.arg2 |= max_idx;
607
608 if (num_entries_to_ret > 1) {
609 ret.extended_val.valid = 1;
610 }
611
612 for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800613 uint64_t *xn_0 = arg_ptrs[arg_idx++];
614 uint64_t *xn_1 = arg_ptrs[arg_idx++];
615 uint64_t *xn_2 = arg_ptrs[arg_idx++];
616
617 api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id,
618 partitions[idx].vcpu_count,
619 partitions[idx].properties);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800620 if (uuid_is_null) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800621 api_ffa_pack_uuid(xn_1, xn_2, &partitions[idx].uuid);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800622 }
623 assert(arg_idx < ARRAY_SIZE(arg_ptrs));
624 }
625
626 return ret;
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800627}
628
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100629struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000630 const struct ffa_uuid *uuid,
631 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100632{
633 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100634 ffa_vm_count_t vm_count = 0;
Olivier Deprez013f4d62022-11-21 15:46:20 +0100635 bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) ==
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000636 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100637 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100638 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000639 struct vm_locked vm_locked;
640 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100641
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000642 /* Bits 31:1 Must Be Zero */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100643 if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) {
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000644 return ffa_error(FFA_INVALID_PARAMETERS);
645 }
646
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100647 /*
Kathleen Capella402fa852022-11-09 16:16:51 -0500648 * No need to count if we are returning the number of partitions as we
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000649 * already know this.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100650 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000651 if (uuid_is_null && count_flag) {
652 vm_count = vm_get_count();
653 } else {
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800654 api_ffa_fill_partitions_info_array(
655 partitions, ARRAY_SIZE(partitions), uuid, count_flag,
656 current_vm->id, &vm_count);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100657 }
658
Olivier Depreze562e542020-06-11 17:31:54 +0200659 /* If UUID is Null vm_count must not be zero at this stage. */
660 CHECK(!uuid_is_null || vm_count != 0);
661
662 /*
663 * When running the Hypervisor:
664 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
665 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000666 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200667 * a secure partition and the query is forwarded to the SPMC.
668 * When running the SPMC:
669 * - If UUID is non-Null and vm_count is zero it means there is no such
670 * partition identified in the system.
671 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000672 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200673
674 /*
675 * Unrecognized UUID: does not match any of the VMs (or SPs)
676 * and is not Null.
677 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100678 if (vm_count == 0) {
679 return ffa_error(FFA_INVALID_PARAMETERS);
680 }
681
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000682 /*
683 * If the count flag is set we don't need to return the partition info
684 * descriptors.
685 */
686 if (count_flag) {
687 return (struct ffa_value){.func = FFA_SUCCESS_32,
688 .arg2 = vm_count};
689 }
690
Daniel Boulby191b5f02022-02-17 17:26:14 +0000691 vm_locked = vm_lock(current_vm);
692 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
693 vm_count);
694 vm_unlock(&vm_locked);
695 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100696}
697
Andrew Scull9726c252019-01-23 13:44:19 +0000698/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000699 * Returns the ID of the VM.
700 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000702{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100703 return (struct ffa_value){.func = FFA_SUCCESS_32,
704 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000705}
706
707/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200708 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
709 * FF-A instances.
710 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000711 */
712struct ffa_value api_ffa_spm_id_get(void)
713{
J-Alves3829fc02021-03-18 12:49:18 +0000714#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
715 /*
716 * Return the SPMC ID that was fetched during FF-A
717 * initialization.
718 */
719 return (struct ffa_value){.func = FFA_SUCCESS_32,
720 .arg2 = arch_ffa_spmc_id_get()};
721#else
722 return ffa_error(FFA_NOT_SUPPORTED);
723#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000724}
725
726/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000727 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000728 * function to indicate that register state for the given vCPU has been saved
729 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000730 */
731void api_regs_state_saved(struct vcpu *vcpu)
732{
733 sl_lock(&vcpu->lock);
734 vcpu->regs_available = true;
735 sl_unlock(&vcpu->lock);
736}
737
738/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000739 * Retrieves the next waiter and removes it from the wait list if the VM's
740 * mailbox is in a writable state.
741 */
742static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
743{
744 struct wait_entry *entry;
745 struct vm *vm = locked_vm.vm;
746
Andrew Sculld6ee1102019-04-05 22:12:42 +0100747 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000748 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
749 /* The mailbox is not writable or there are no waiters. */
750 return NULL;
751 }
752
753 /* Remove waiter from the wait list. */
754 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
755 wait_links);
756 list_remove(&entry->wait_links);
757 return entry;
758}
759
760/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000761 * Assuming that the arguments have already been checked by the caller, injects
762 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
763 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
764 * is next run, which is up to the scheduler.
765 *
766 * Returns:
767 * - 0 on success if no further action is needed.
768 * - 1 if it was called by the primary VM and the primary VM now needs to wake
769 * up or kick the target vCPU.
770 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100771int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
772 uint32_t intid, struct vcpu *current,
773 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000774{
Manish Pandey35e452f2021-02-18 21:36:34 +0000775 struct vcpu *target_vcpu = target_locked.vcpu;
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100776 struct interrupts *interrupts = &target_vcpu->interrupts;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000777 int64_t ret = 0;
778
Andrew Walbran508e63c2018-12-20 17:02:37 +0000779 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000780 * We only need to change state and (maybe) trigger a virtual interrupt
781 * if it is enabled and was not previously pending. Otherwise we can
782 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000783 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100784 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
785 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000786 goto out;
787 }
788
789 /* Increment the count. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100790 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000791
792 /*
793 * Only need to update state if there was not already an
794 * interrupt enabled and pending.
795 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000796 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000797 goto out;
798 }
799
Andrew Walbran508e63c2018-12-20 17:02:37 +0000800 if (current->vm->id == HF_PRIMARY_VM_ID) {
801 /*
802 * If the call came from the primary VM, let it know that it
803 * should run or kick the target vCPU.
804 */
805 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000806 } else if (current != target_vcpu && next != NULL) {
807 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000808 }
809
810out:
811 /* Either way, make it pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100812 vcpu_virt_interrupt_set_pending(interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000813
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200814 return ret;
815}
816
817/* Wrapper to internal_interrupt_inject with locking of target vCPU */
818static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
819 uint32_t intid, struct vcpu *current,
820 struct vcpu **next)
821{
822 int64_t ret;
823 struct vcpu_locked target_locked;
824
825 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100826 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200827 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000828
829 return ret;
830}
831
832/**
Federico Recanati25053ee2022-03-14 15:01:53 +0100833 * Constructs the return value from a successful FFA_MSG_POLL or
834 * FFA_MSG_WAIT call.
835 *
836 * Note: FFA_MSG_POLL is deprecated in FF-A v1.1 and should not be used with
837 * FFA_MSG_SEND2; no check is done on mixing FF-A v1.0 and FF-A v1.1 indirect
838 * message protocols.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100839 */
J-Alves27b71962022-12-12 15:29:58 +0000840struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100841{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000842 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100843 case FFA_MSG_SEND_32:
844 return (struct ffa_value){
845 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000846 .arg1 = (receiver->mailbox.recv_sender << 16) |
847 receiver->id,
848 .arg3 = receiver->mailbox.recv_size};
Federico Recanati25053ee2022-03-14 15:01:53 +0100849 case FFA_MSG_SEND2_32:
850 return (struct ffa_value){
851 .func = FFA_RUN_32,
852 /*
853 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
854 * Retrieving vCPU requires a rework of the function,
855 * while receiver ID must be set because it's checked by
856 * other APIs (eg: FFA_NOTIFICATION_GET).
857 */
858 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000859 default:
860 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000861 dlog_error("Tried to return an invalid message function %#x\n",
862 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100863 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000864 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100865}
866
J-Alvese8c8c2b2022-12-16 15:34:48 +0000867/**
868 * Change the state of mailbox to empty, such that the ownership is given to the
869 * Partition manager.
870 * Returns true if the mailbox was reset successfully, false otherwise.
871 */
872static bool api_release_mailbox(struct vm_locked vm_locked, int32_t *error_code)
873{
874 ffa_vm_id_t vm_id = vm_locked.vm->id;
875 int32_t error_code_to_ret = 0;
876 bool ret = false;
877
878 switch (vm_locked.vm->mailbox.state) {
879 case MAILBOX_STATE_EMPTY:
880 dlog_verbose("Mailbox of %x is empty.\n", vm_id);
881 error_code_to_ret = FFA_DENIED;
882 goto out;
883 case MAILBOX_STATE_FULL:
884 /* Check it doesn't have pending RX full notifications. */
885 if (vm_are_fwk_notifications_pending(vm_locked)) {
886 dlog_verbose(
887 "Mailbox of endpoint %x has pending "
888 "messages.\n",
889 vm_id);
890 error_code_to_ret = FFA_DENIED;
891 goto out;
892 }
893 break;
894 case MAILBOX_STATE_OTHER_WORLD_OWNED:
895 /*
896 * The SPMC shouldn't let SP's mailbox get into this state.
897 * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst
898 * the mailbox is in this state. In that case, we should report
899 * error.
900 */
901 if (vm_id_is_current_world(vm_id)) {
902 dlog_verbose(
903 "Mailbox of endpoint %x in a wrongful state.\n",
904 vm_id);
905 error_code_to_ret = FFA_ABORTED;
906 goto out;
907 }
908 break;
909 }
910
911 vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
912 ret = true;
913out:
914 if (error_code != NULL) {
915 *error_code = error_code_to_ret;
916 }
917
918 return ret;
919}
920
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600921struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
922 struct ffa_value *args)
923{
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500924 enum vcpu_state next_state = VCPU_STATE_WAITING;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000925 struct ffa_value ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600926
927 if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U ||
928 args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
929 args->arg7 != 0U) {
930 return ffa_error(FFA_INVALID_PARAMETERS);
931 }
932
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500933 if (!plat_ffa_check_runtime_state_transition(
934 current, current->vm->id, HF_INVALID_VM_ID, NULL,
935 FFA_MSG_WAIT_32, &next_state)) {
936 return ffa_error(FFA_DENIED);
937 }
938
939 assert(next_state == VCPU_STATE_WAITING);
940
J-Alvese8c8c2b2022-12-16 15:34:48 +0000941 ret = plat_ffa_msg_wait_prepare(current, next);
942
943 if (ret.func != FFA_ERROR_32) {
944 struct vm_locked vm_locked = vm_lock(current->vm);
945
946 api_release_mailbox(vm_locked, NULL);
947 vm_unlock(&vm_locked);
948 }
949
950 return ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600951}
952
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100953/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000954 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000955 * value needs to be forced onto the vCPU.
956 */
J-Alves6e2abc62021-12-02 14:58:56 +0000957static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100958 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000959{
Max Shvetsov40108e72020-08-27 12:39:50 +0100960 struct vcpu_locked vcpu_locked;
961 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000962 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500963 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Olivier Deprez04168ed2022-09-26 09:20:00 +0200964 bool vcpu_was_init_state = false;
J-Alves6e2abc62021-12-02 14:58:56 +0000965 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000966
Andrew Scullb06d1752019-02-04 10:15:48 +0000967 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000968 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000969 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100970 * The VM lock is not needed in the common case so it must only be taken
971 * when it is going to be needed. This ensures there are no inter-vCPU
972 * dependencies in the common run case meaning the sensitive context
973 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000974 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100975 vcpu_locked = vcpu_lock(vcpu);
976
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000977 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000978 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
979 (!vcpu->vm->el0_partition &&
980 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
981 vcpu->state == VCPU_STATE_BLOCKED ||
982 vcpu->state == VCPU_STATE_PREEMPTED));
983
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000984 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100985 vcpu_unlock(&vcpu_locked);
986 vm_locked = vm_lock(vcpu->vm);
987 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000988 }
989
990 /*
991 * If the vCPU is already running somewhere then we can't run it here
992 * simultaneously. While it is actually running then the state should be
993 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
994 * stops running but while Hafnium is in the process of switching back
995 * to the primary there will be a brief period while the state has been
996 * updated but `regs_available` is still false (until
997 * `api_regs_state_saved` is called). We can't start running it again
998 * until this has finished, so count this state as still running for the
999 * purposes of this check.
1000 */
1001 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
1002 /*
1003 * vCPU is running on another pCPU.
1004 *
1005 * It's okay not to return the sleep duration here because the
1006 * other physical CPU that is currently running this vCPU will
1007 * return the sleep duration if needed.
1008 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001009 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001010 ret = false;
1011 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001012 }
Andrew Scull9726c252019-01-23 13:44:19 +00001013
1014 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001015 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001016 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +00001017 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +01001018 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +00001019 }
1020 ret = false;
1021 goto out;
1022 }
1023
Andrew Walbran508e63c2018-12-20 17:02:37 +00001024 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001025 case VCPU_STATE_RUNNING:
1026 case VCPU_STATE_OFF:
1027 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001028 ret = false;
1029 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001030
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001031 case VCPU_STATE_WAITING:
1032 /*
Olivier Deprezb2808332023-02-02 15:25:40 +01001033 * An initial FFA_RUN is necessary for SP's secondary vCPUs to
1034 * reach the message wait loop.
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001035 */
Olivier Deprezb2808332023-02-02 15:25:40 +01001036 if (vcpu->rt_model == RTM_SP_INIT) {
1037 /*
1038 * TODO: this should be removed, but omitting it makes
1039 * normal world arch gicv3 tests failing.
1040 */
1041 vcpu->rt_model = RTM_NONE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001042
1043 vcpu_was_init_state = true;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001044 break;
1045 }
1046
J-Alves6e2abc62021-12-02 14:58:56 +00001047 assert(need_vm_lock == true);
1048 if (!vm_locked.vm->el0_partition &&
1049 plat_ffa_inject_notification_pending_interrupt(
1050 vcpu_locked, current, vm_locked)) {
Federico Recanati9dccc4b2022-04-27 12:52:36 +02001051 /* TODO: setting a return value to override
1052 * the placeholder (FFA_ERROR(INTERRUPTED))
1053 * set by FFA_MSG_WAIT. FF-A v1.1 allows
1054 * FFA_MSG_WAIT to successfully return even if
1055 * it didn't receive a message. TFTF tests are
1056 * still expecting an FFA_ERROR instead,
1057 * should be fixed?
1058 */
1059 arch_regs_set_retval(
1060 &vcpu->regs,
1061 (struct ffa_value){.func = FFA_RUN_32,
1062 // TODO: does it make sense
1063 // to set vCPU/receiver?
1064 .arg1 = 0});
J-Alves6e2abc62021-12-02 14:58:56 +00001065 break;
1066 }
1067
Andrew Scullb06d1752019-02-04 10:15:48 +00001068 /*
1069 * A pending message allows the vCPU to run so the message can
1070 * be delivered directly.
1071 */
J-Alvese8c8c2b2022-12-16 15:34:48 +00001072 if (vcpu->vm->mailbox.state == MAILBOX_STATE_FULL) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001073 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001074 ffa_msg_recv_return(vcpu->vm));
Andrew Scullb06d1752019-02-04 10:15:48 +00001075 break;
1076 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001077
1078 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
1079 break;
1080 }
1081
1082 if (arch_timer_enabled(&vcpu->regs)) {
1083 timer_remaining_ns =
1084 arch_timer_remaining_ns(&vcpu->regs);
1085 if (timer_remaining_ns == 0) {
1086 break;
1087 }
1088 } else {
1089 dlog_verbose("Timer disabled\n");
1090 }
1091 run_ret->func = FFA_MSG_WAIT_32;
1092 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1093 run_ret->arg2 = timer_remaining_ns;
1094 ret = false;
1095 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001096 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +00001097 if (need_vm_lock &&
1098 plat_ffa_inject_notification_pending_interrupt(
1099 vcpu_locked, current, vm_locked)) {
1100 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
1101 break;
1102 }
1103
Andrew Scullb06d1752019-02-04 10:15:48 +00001104 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +00001105 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +00001106 break;
1107 }
1108
Andrew Walbran508e63c2018-12-20 17:02:37 +00001109 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001110 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001111 arch_timer_remaining_ns(&vcpu->regs);
1112
1113 /*
1114 * The timer expired so allow the interrupt to be
1115 * delivered.
1116 */
1117 if (timer_remaining_ns == 0) {
1118 break;
1119 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001120 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001121
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001122 /*
1123 * The vCPU is not ready to run, return the appropriate code to
1124 * the primary which called vcpu_run.
1125 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001126 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001127 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1128 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +00001129
1130 ret = false;
1131 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001132
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001133 case VCPU_STATE_BLOCKED:
1134 /* A blocked vCPU is run unconditionally. Fall through. */
1135 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +00001136 /* Check NPI is to be injected here. */
1137 if (need_vm_lock) {
1138 plat_ffa_inject_notification_pending_interrupt(
1139 vcpu_locked, current, vm_locked);
1140 }
Andrew Walbran508e63c2018-12-20 17:02:37 +00001141 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001142 default:
1143 /*
1144 * Execution not expected to reach here. Deny the request
1145 * gracefully.
1146 */
1147 *run_ret = ffa_error(FFA_DENIED);
1148 ret = false;
1149 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001150 }
1151
Madhukar Pappireddy1480fce2022-06-21 18:09:25 -05001152 plat_ffa_init_schedule_mode_ffa_run(current, vcpu_locked);
1153
Andrew Scullb06d1752019-02-04 10:15:48 +00001154 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001155 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001156 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001157
Olivier Deprez04168ed2022-09-26 09:20:00 +02001158 if (vcpu_was_init_state) {
1159 vcpu_set_phys_core_idx(vcpu);
1160 vcpu_set_boot_info_gp_reg(vcpu);
J-Alves7ac49052022-02-08 17:20:53 +00001161 }
J-Alves7ac49052022-02-08 17:20:53 +00001162
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001163 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001164 * Mark the registers as unavailable now that we're about to reflect
1165 * them onto the real registers. This will also prevent another physical
1166 * CPU from trying to read these registers.
1167 */
1168 vcpu->regs_available = false;
1169
1170 ret = true;
1171
1172out:
Max Shvetsov40108e72020-08-27 12:39:50 +01001173 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001174 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +01001175 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001176 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001177 return ret;
1178}
1179
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001181 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001182{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001183 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001184 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001185 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001186 enum vcpu_state next_state = VCPU_STATE_BLOCKED;
1187 struct vcpu_locked current_locked;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001188
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -08001189 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001190 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001191 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001192
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001193 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
1194 return ret;
1195 }
1196
Andrew Scull19503262018-09-20 14:48:39 +01001197 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001198 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +01001199 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001200 goto out;
Andrew Scull19503262018-09-20 14:48:39 +01001201 }
1202
Fuad Tabbaed294af2019-12-20 10:43:01 +00001203 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001204 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001205 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001206 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001207
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001208 /*
1209 * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC
1210 * bypasses the intermediate execution contexts and resumes the
1211 * SP execution context that was originally preempted.
1212 */
1213 if (*next != NULL) {
1214 vcpu = *next;
1215 } else {
1216 vcpu = vm_get_vcpu(vm, vcpu_idx);
1217 }
1218
1219 if (!plat_ffa_check_runtime_state_transition(current, current->vm->id,
1220 HF_INVALID_VM_ID, vcpu,
1221 FFA_RUN_32, &next_state)) {
1222 return ffa_error(FFA_DENIED);
1223 }
1224
Andrew Scullb06d1752019-02-04 10:15:48 +00001225 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001226 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001227 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001228
Andrew Walbran508e63c2018-12-20 17:02:37 +00001229 /*
1230 * Inject timer interrupt if timer has expired. It's safe to access
1231 * vcpu->regs here because api_vcpu_prepare_run already made sure that
1232 * regs_available was true (and then set it to false) before returning
1233 * true.
1234 */
1235 if (arch_timer_pending(&vcpu->regs)) {
1236 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001237 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
1238 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001239
1240 /*
1241 * Set the mask bit so the hardware interrupt doesn't fire
1242 * again. Ideally we wouldn't do this because it affects what
1243 * the secondary vCPU sees, but if we don't then we end up with
1244 * a loop of the interrupt firing each time we try to return to
1245 * the secondary vCPU.
1246 */
1247 arch_timer_mask(&vcpu->regs);
1248 }
1249
Fuad Tabbaed294af2019-12-20 10:43:01 +00001250 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001251 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001252
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001253 assert(next_state == VCPU_STATE_BLOCKED);
1254 current_locked = vcpu_lock(current);
1255 current->state = VCPU_STATE_BLOCKED;
1256 vcpu_unlock(&current_locked);
1257
Andrew Scull33fecd32019-01-08 14:48:27 +00001258 /*
1259 * Set a placeholder return code to the scheduler. This will be
1260 * overwritten when the switch back to the primary occurs.
1261 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001262 ret.func = FFA_INTERRUPT_32;
Andrew Walbran7e824602020-10-22 16:51:40 +01001263 ret.arg1 = 0;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +01001264 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +00001265
Andrew Scull6d2db332018-10-10 15:28:17 +01001266out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001267 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001268}
1269
1270/**
Andrew Scull81e85092018-12-12 12:56:20 +00001271 * Check that the mode indicates memory that is valid, owned and exclusive.
1272 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001273static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001274{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001275 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1276 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001277}
1278
1279/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001280 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001281 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001282static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1283 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001284 paddr_t pa_send_begin, paddr_t pa_send_end,
1285 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001286 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001287 struct mpool *local_page_pool)
1288{
1289 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001290
1291 /* Map the send page as read-only in the hypervisor address space. */
1292 vm_locked.vm->mailbox.send =
1293 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001294 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001295 if (!vm_locked.vm->mailbox.send) {
Andrew Sculle1322792019-07-01 17:46:10 +01001296 goto fail;
1297 }
1298
1299 /*
1300 * Map the receive page as writable in the hypervisor address space. On
1301 * failure, unmap the send page before returning.
1302 */
1303 vm_locked.vm->mailbox.recv =
1304 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001305 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001306 if (!vm_locked.vm->mailbox.recv) {
Andrew Sculle1322792019-07-01 17:46:10 +01001307 goto fail_undo_send;
1308 }
1309
1310 ret = true;
1311 goto out;
1312
1313 /*
1314 * The following mappings will not require more memory than is available
1315 * in the local pool.
1316 */
1317fail_undo_send:
1318 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001319 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1320 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001321
1322fail:
1323 ret = false;
1324
1325out:
Andrew Sculle1322792019-07-01 17:46:10 +01001326 return ret;
1327}
1328
1329/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001330 * Sanity checks and configures the send and receive pages in the VM stage-2
1331 * and hypervisor stage-1 page tables.
1332 *
1333 * Returns:
1334 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001335 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001336 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1337 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001338 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001339 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001340 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001341
1342struct ffa_value api_vm_configure_pages(
1343 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1344 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1345 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001346{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001347 struct ffa_value ret;
1348 paddr_t pa_send_begin;
1349 paddr_t pa_send_end;
1350 paddr_t pa_recv_begin;
1351 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001352 uint32_t orig_send_mode = 0;
1353 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001354 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001355
1356 /* We only allow these to be setup once. */
1357 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1358 ret = ffa_error(FFA_DENIED);
1359 goto out;
1360 }
1361
1362 /* Hafnium only supports a fixed size of RX/TX buffers. */
1363 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1364 ret = ffa_error(FFA_INVALID_PARAMETERS);
1365 goto out;
1366 }
1367
1368 /* Fail if addresses are not page-aligned. */
1369 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1370 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1371 ret = ffa_error(FFA_INVALID_PARAMETERS);
1372 goto out;
1373 }
1374
1375 /* Convert to physical addresses. */
1376 pa_send_begin = pa_from_ipa(send);
1377 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1378 pa_recv_begin = pa_from_ipa(recv);
1379 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1380
1381 /* Fail if the same page is used for the send and receive pages. */
1382 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1383 ret = ffa_error(FFA_INVALID_PARAMETERS);
1384 goto out;
1385 }
Andrew Sculle1322792019-07-01 17:46:10 +01001386
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001387 /* Set stage 2 translation tables only for virtual FF-A instances. */
1388 if (vm_id_is_current_world(vm_locked.vm->id)) {
1389 /*
1390 * Ensure the pages are valid, owned and exclusive to the VM and
1391 * that the VM has the required access to the memory.
1392 */
1393 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1394 &orig_send_mode) ||
1395 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1396 (orig_send_mode & MM_MODE_R) == 0 ||
1397 (orig_send_mode & MM_MODE_W) == 0) {
1398 dlog_error(
1399 "VM doesn't have required access rights to map "
1400 "TX buffer in stage 2.\n");
1401 ret = ffa_error(FFA_INVALID_PARAMETERS);
1402 goto out;
1403 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001404
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001405 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1406 &orig_recv_mode) ||
1407 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1408 (orig_recv_mode & MM_MODE_R) == 0) {
1409 dlog_error(
1410 "VM doesn't have required access rights to map "
1411 "RX buffer in stage 2.\n");
1412 ret = ffa_error(FFA_INVALID_PARAMETERS);
1413 goto out;
1414 }
Andrew Sculle1322792019-07-01 17:46:10 +01001415
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001416 /* Take memory ownership away from the VM and mark as shared. */
1417 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1418 MM_MODE_W;
1419 if (vm_locked.vm->el0_partition) {
1420 mode |= MM_MODE_USER | MM_MODE_NG;
1421 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001422
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001423 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1424 mode, local_page_pool, NULL)) {
1425 dlog_error(
1426 "Cannot allocate a new entry in stage 2 "
1427 "translation table.\n");
1428 ret = ffa_error(FFA_NO_MEMORY);
1429 goto out;
1430 }
Andrew Sculle1322792019-07-01 17:46:10 +01001431
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001432 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1433 if (vm_locked.vm->el0_partition) {
1434 mode |= MM_MODE_USER | MM_MODE_NG;
1435 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001436
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001437 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1438 mode, local_page_pool, NULL)) {
1439 /* TODO: partial defrag of failed range. */
1440 /* Recover any memory consumed in failed mapping. */
1441 vm_ptable_defrag(vm_locked, local_page_pool);
1442 goto fail_undo_send;
1443 }
Andrew Sculle1322792019-07-01 17:46:10 +01001444 }
1445
Olivier Deprez96a2a262020-06-11 17:21:38 +02001446 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1447 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1448
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001449 /*
1450 * For EL0 partitions, since both the partition and the hypervisor code
1451 * use the EL2&0 translation regime, it is critical to mark the mappings
1452 * of the send and recv buffers as non-global in the TLB. For one, if we
1453 * dont mark it as non-global, it would cause TLB conflicts since there
1454 * would be an identity mapping with non-global attribute in the
1455 * partitions page tables, but another identity mapping in the
1456 * hypervisor page tables with the global attribute. The other issue is
1457 * one of security, we dont want other partitions to be able to access
1458 * other partitions buffers through cached translations.
1459 */
1460 if (vm_locked.vm->el0_partition) {
1461 extra_attributes |= MM_MODE_NG;
1462 }
1463
Manish Pandeyd34f8892020-06-19 17:41:07 +01001464 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1465 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001466 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001467 goto fail_undo_send_and_recv;
1468 }
1469
Manish Pandeyd34f8892020-06-19 17:41:07 +01001470 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001471 goto out;
1472
Andrew Sculle1322792019-07-01 17:46:10 +01001473fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001474 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001475 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001476
1477fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001478 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001479 orig_send_mode, local_page_pool, NULL));
1480 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001481
1482out:
Andrew Sculle1322792019-07-01 17:46:10 +01001483 return ret;
1484}
1485
J-Alves28ad9b42022-12-08 12:19:43 +00001486static void api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001487 ipaddr_t *recv, uint32_t *page_count,
1488 ffa_vm_id_t *owner_vm_id)
1489{
1490 /*
1491 * If the message has been forwarded the effective addresses are in
1492 * hypervisor's TX buffer.
1493 */
J-Alves28ad9b42022-12-08 12:19:43 +00001494 bool forwarded = (current_vm->id == HF_OTHER_WORLD_ID) &&
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001495 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1496 (*page_count == 0);
1497
1498 if (forwarded) {
J-Alves28ad9b42022-12-08 12:19:43 +00001499 struct vm_locked vm_locked = vm_lock(current_vm);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001500 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc =
1501 (struct ffa_endpoint_rx_tx_descriptor *)
1502 vm_locked.vm->mailbox.send;
1503 struct ffa_composite_memory_region *rx_region =
1504 ffa_enpoint_get_rx_memory_region(endpoint_desc);
1505 struct ffa_composite_memory_region *tx_region =
1506 ffa_enpoint_get_tx_memory_region(endpoint_desc);
1507
1508 *owner_vm_id = endpoint_desc->endpoint_id;
1509 *recv = ipa_init(rx_region->constituents[0].address);
1510 *send = ipa_init(tx_region->constituents[0].address);
1511 *page_count = rx_region->constituents[0].page_count;
J-Alves28ad9b42022-12-08 12:19:43 +00001512
1513 vm_unlock(&vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001514 } else {
J-Alves28ad9b42022-12-08 12:19:43 +00001515 *owner_vm_id = current_vm->id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001516 }
1517}
Andrew Sculle1322792019-07-01 17:46:10 +01001518/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001519 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001520 * must not be shared. Locking of the page tables combined with a local memory
1521 * pool ensures there will always be enough memory to recover from any errors
1522 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1523 * by another core which could result in this transaction being unable to roll
1524 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001525 *
1526 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001527 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001528 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001529 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001530 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001531 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001532 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001533 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001535 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001536{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 struct ffa_value ret;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001538 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001539 struct mm_stage1_locked mm_stage1_locked;
1540 struct mpool local_page_pool;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001541 ffa_vm_id_t owner_vm_id;
1542
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001543 /*
1544 * Get the original buffer addresses and VM ID in case of forwarded
1545 * message.
1546 */
J-Alves28ad9b42022-12-08 12:19:43 +00001547 api_get_rxtx_description(current->vm, &send, &recv, &page_count,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001548 &owner_vm_id);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001549
1550 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1551 if (owner_vm_locked.vm == NULL) {
1552 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1553 owner_vm_id);
1554 return ffa_error(FFA_DENIED);
1555 }
Andrew Scull220e6212018-12-21 18:09:00 +00001556
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001557 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001558 * Create a local pool so any freed memory can't be used by another
1559 * thread. This is to ensure the original mapping can be restored if any
1560 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001561 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001562 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1563
Manish Pandeyd34f8892020-06-19 17:41:07 +01001564 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001565
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001566 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1567 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001568 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001569 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001570 }
1571
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001572 /* Forward buffer mapping to SPMC if coming from a VM. */
1573 plat_ffa_rxtx_map_forward(owner_vm_locked);
1574
Federico Recanati9f1b6532022-04-14 13:15:28 +02001575 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001576
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001577exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001578 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001579 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001580 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001581
1582 return ret;
1583}
1584
1585/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001586 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1587 * translation regime of the caller. Unmap the region for the hypervisor and
1588 * set the memory region to owned and exclusive for the component. Since the
1589 * memory region mapped in the page table, when the buffers were originally
1590 * created we can safely remap it.
1591 *
1592 * Returns:
1593 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1594 * behalf of the caller.
1595 * - FFA_SUCCESS on success if no further action is needed.
1596 */
1597struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1598 struct vcpu *current)
1599{
1600 struct vm *vm = current->vm;
1601 struct vm_locked vm_locked;
Federico Recanati8da9e332022-02-10 11:00:17 +01001602 ffa_vm_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001603 struct mm_stage1_locked mm_stage1_locked;
1604 paddr_t send_pa_begin;
1605 paddr_t send_pa_end;
1606 paddr_t recv_pa_begin;
1607 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001608 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001609
J-Alves9bd324a2023-01-12 10:52:52 +00001610 if (vm->id == HF_HYPERVISOR_VM_ID && !plat_ffa_is_vm_id(allocator_id)) {
1611 dlog_error(
1612 "The Hypervisor must specify a valid VM ID in register "
1613 "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n");
1614 return ffa_error(FFA_INVALID_PARAMETERS);
1615 }
1616
Federico Recanati8da9e332022-02-10 11:00:17 +01001617 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1618 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
J-Alves9bd324a2023-01-12 10:52:52 +00001619 dlog_error(
1620 "The register W1 (containing ID) must be 0 at virtual "
1621 "instances.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001622 return ffa_error(FFA_INVALID_PARAMETERS);
1623 }
1624
1625 /* VM ID of which buffers have to be unmapped. */
1626 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1627
1628 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1629 vm = vm_locked.vm;
1630 if (vm == NULL) {
1631 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1632 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001633 return ffa_error(FFA_INVALID_PARAMETERS);
1634 }
1635
1636 /* Get send and receive buffers. */
1637 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001638 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001639 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001640 ret = ffa_error(FFA_INVALID_PARAMETERS);
1641 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001642 }
1643
1644 /* Currently a mailbox size of 1 page is assumed. */
1645 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1646 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1647 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1648 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1649
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001650 mm_stage1_locked = mm_lock_stage1();
1651
Federico Recanati8da9e332022-02-10 11:00:17 +01001652 /* Reset stage 2 mapping only for virtual FF-A instances. */
1653 if (vm_id_is_current_world(owner_vm_id)) {
1654 /*
1655 * Set the memory region of the buffers back to the default mode
1656 * for the VM. Since this memory region was already mapped for
1657 * the RXTX buffers we can safely remap them.
1658 */
1659 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1660 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1661 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001662
Federico Recanati8da9e332022-02-10 11:00:17 +01001663 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1664 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1665 &api_page_pool, NULL));
1666 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001667
1668 /* Unmap the buffers in the partition manager. */
1669 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1670 &api_page_pool));
1671 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1672 &api_page_pool));
1673
1674 vm->mailbox.send = NULL;
1675 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001676 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001677
Federico Recanati8da9e332022-02-10 11:00:17 +01001678 /* Forward buffer unmapping to SPMC if coming from a VM. */
J-Alves70079932022-12-07 17:32:20 +00001679 plat_ffa_rxtx_unmap_forward(vm_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001680
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001681 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001682
1683out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001684 vm_unlock(&vm_locked);
1685
Federico Recanati8da9e332022-02-10 11:00:17 +01001686 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001687}
1688
1689/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001690 * Copies data from the sender's send buffer to the recipient's receive buffer
1691 * and notifies the receiver.
1692 */
1693struct ffa_value api_ffa_msg_send2(ffa_vm_id_t sender_vm_id, uint32_t flags,
1694 struct vcpu *current)
1695{
1696 struct vm *from = current->vm;
1697 struct vm *to;
1698 struct vm_locked to_locked;
1699 ffa_vm_id_t msg_sender_id;
1700 struct vm_locked sender_locked;
1701 const void *from_msg;
1702 struct ffa_value ret;
1703 struct ffa_partition_rxtx_header header;
1704 ffa_vm_id_t sender_id;
1705 ffa_vm_id_t receiver_id;
1706 uint32_t msg_size;
1707 ffa_notifications_bitmap_t rx_buffer_full;
1708
1709 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
1710 if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) {
1711 dlog_error("Sender VM ID must be zero.\n");
1712 return ffa_error(FFA_INVALID_PARAMETERS);
1713 }
1714
Federico Recanati25053ee2022-03-14 15:01:53 +01001715 /*
1716 * Get message sender's mailbox, which can be different to the `from` vm
1717 * when the message is forwarded.
1718 */
1719 msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id;
1720 sender_locked = plat_ffa_vm_find_locked(msg_sender_id);
1721 if (sender_locked.vm == NULL) {
1722 dlog_error("Cannot send message from VM ID %#x, not found.\n",
1723 msg_sender_id);
1724 return ffa_error(FFA_DENIED);
1725 }
1726
1727 from_msg = sender_locked.vm->mailbox.send;
1728 if (from_msg == NULL) {
1729 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
1730 msg_sender_id);
1731 ret = ffa_error(FFA_DENIED);
1732 goto out_unlock_sender;
1733 }
1734
1735 /*
1736 * Copy message header as safety measure to avoid multiple accesses to
1737 * unsafe memory which could be 'corrupted' between safety checks and
1738 * final buffer copy.
1739 */
1740 memcpy_s(&header, FFA_RXTX_HEADER_SIZE, from_msg, FFA_RXTX_HEADER_SIZE);
1741 sender_id = ffa_rxtx_header_sender(&header);
1742 receiver_id = ffa_rxtx_header_receiver(&header);
1743
1744 /* Ensure Sender IDs from API and from message header match. */
1745 if (msg_sender_id != sender_id) {
1746 dlog_error(
1747 "Message sender VM ID (%#x) doesn't match header's VM "
1748 "ID (%#x).\n",
1749 msg_sender_id, sender_id);
1750 ret = ffa_error(FFA_INVALID_PARAMETERS);
1751 goto out_unlock_sender;
1752 }
1753
1754 /* Disallow reflexive requests as this suggests an error in the VM. */
1755 if (receiver_id == sender_id) {
1756 dlog_error("Sender and receive VM IDs must be different.\n");
1757 ret = ffa_error(FFA_INVALID_PARAMETERS);
1758 goto out_unlock_sender;
1759 }
1760
Federico Recanati7b39ff22022-03-14 15:01:53 +01001761 /* `flags` can be set only at secure virtual FF-A instances. */
1762 if (plat_ffa_is_vm_id(sender_id) && (flags != 0)) {
1763 dlog_error("flags must be zero.\n");
1764 return ffa_error(FFA_INVALID_PARAMETERS);
1765 }
1766
Federico Recanati25053ee2022-03-14 15:01:53 +01001767 /*
1768 * Check if the message has to be forwarded to the SPMC, in
1769 * this case return, the SPMC will handle the buffer copy.
1770 */
1771 if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) {
1772 goto out_unlock_sender;
1773 }
1774
1775 /* Ensure the receiver VM exists. */
1776 to_locked = plat_ffa_vm_find_locked(receiver_id);
1777 to = to_locked.vm;
1778
1779 if (to == NULL) {
1780 dlog_error("Cannot deliver message to VM %#x, not found.\n",
1781 receiver_id);
1782 ret = ffa_error(FFA_INVALID_PARAMETERS);
1783 goto out_unlock_sender;
1784 }
1785
1786 /*
1787 * Check sender and receiver can use indirect messages.
1788 * Sender is the VM/SP who originally sent the message, not the
1789 * hypervisor possibly relaying it.
1790 */
1791 if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) {
1792 dlog_verbose("VM %#x doesn't support indirect message\n",
1793 sender_id);
1794 ret = ffa_error(FFA_DENIED);
1795 goto out;
1796 }
1797
J-Alvese8c8c2b2022-12-16 15:34:48 +00001798 if (vm_is_mailbox_busy(to_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01001799 dlog_error(
1800 "Cannot deliver message to VM %#x, RX buffer not "
1801 "ready.\n",
1802 receiver_id);
1803 ret = ffa_error(FFA_BUSY);
1804 goto out;
1805 }
1806
Federico Recanati644f0462022-03-17 12:04:00 +01001807 /* Acquire receiver's RX buffer. */
1808 if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) {
1809 dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id);
1810 goto out;
1811 }
1812
Federico Recanati25053ee2022-03-14 15:01:53 +01001813 /* Check the size of transfer. */
1814 msg_size = FFA_RXTX_HEADER_SIZE + header.size;
1815 if ((msg_size > FFA_PARTITION_MSG_PAYLOAD_MAX) ||
1816 (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
1817 dlog_error("Message is too big.\n");
1818 ret = ffa_error(FFA_INVALID_PARAMETERS);
1819 goto out;
1820 }
1821
1822 /* Copy data. */
1823 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, msg_size);
1824 to->mailbox.recv_size = msg_size;
1825 to->mailbox.recv_sender = sender_id;
1826 to->mailbox.recv_func = FFA_MSG_SEND2_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001827 to->mailbox.state = MAILBOX_STATE_FULL;
Federico Recanati25053ee2022-03-14 15:01:53 +01001828
1829 rx_buffer_full = plat_ffa_is_vm_id(sender_id)
1830 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
1831 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
1832 vm_notifications_framework_set_pending(to_locked, rx_buffer_full);
1833
1834 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
1835 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
1836 vcpu_index(current));
1837 plat_ffa_sri_trigger_not_delayed(current->cpu);
1838 } else {
1839 plat_ffa_sri_state_set(DELAYED);
1840 }
1841
1842 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1843
1844out:
1845 vm_unlock(&to_locked);
1846
1847out_unlock_sender:
1848 vm_unlock(&sender_locked);
1849
1850 return ret;
1851}
1852
1853/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001854 * Checks whether the vCPU's attempt to block for a message has already been
1855 * interrupted or whether it is allowed to block.
1856 */
Olivier Deprezd8e18882022-07-15 14:46:41 +02001857static bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001858{
Manish Pandey35e452f2021-02-18 21:36:34 +00001859 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001860 bool interrupted;
1861
Manish Pandey35e452f2021-02-18 21:36:34 +00001862 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001863
1864 /*
1865 * Don't block if there are enabled and pending interrupts, to match
1866 * behaviour of wait_for_interrupt.
1867 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001868 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001869
Manish Pandey35e452f2021-02-18 21:36:34 +00001870 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001871
1872 return interrupted;
1873}
1874
1875/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001876 * Receives a message from the mailbox. If one isn't available, this function
1877 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001878 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001879 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001880 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001881struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1882 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001883{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001884 bool is_direct_request_ongoing;
1885 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001886 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001887 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001888 bool is_from_secure_world =
1889 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001890
Andrew Scullaa039b32018-10-04 15:02:26 +01001891 /*
1892 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001893 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001894 */
J-Alvesb37fd082020-10-22 12:29:21 +01001895 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001896 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001897 }
1898
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001899 /*
1900 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1901 * invocation.
1902 */
1903 current_locked = vcpu_lock(current);
1904 is_direct_request_ongoing =
1905 is_ffa_direct_msg_request_ongoing(current_locked);
1906 vcpu_unlock(&current_locked);
1907
1908 if (is_direct_request_ongoing) {
1909 return ffa_error(FFA_DENIED);
1910 }
1911
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001912 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001913
Andrew Scullaa039b32018-10-04 15:02:26 +01001914 /* Return pending messages without blocking. */
J-Alvese8c8c2b2022-12-16 15:34:48 +00001915 if (vm->mailbox.state == MAILBOX_STATE_FULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001916 return_code = ffa_msg_recv_return(vm);
Federico Recanati25053ee2022-03-14 15:01:53 +01001917 if (return_code.func == FFA_MSG_SEND_32) {
J-Alvese8c8c2b2022-12-16 15:34:48 +00001918 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Federico Recanati25053ee2022-03-14 15:01:53 +01001919 }
Jose Marinho3e2442f2019-03-12 13:30:37 +00001920 goto out;
1921 }
1922
1923 /* No pending message so fail if not allowed to block. */
1924 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001925 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001926 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001927 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001928
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001929 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001930 * From this point onward this call can only be interrupted or a message
1931 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001932 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001933 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001934 return_code = ffa_error(FFA_INTERRUPTED);
1935 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001936 goto out;
1937 }
1938
J-Alvesb37fd082020-10-22 12:29:21 +01001939 if (is_from_secure_world) {
1940 /* Return to other world if caller is a SP. */
1941 *next = api_switch_to_other_world(
1942 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001943 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001944 } else {
1945 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001946 struct ffa_value run_return = {
1947 .func = FFA_MSG_WAIT_32,
1948 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001949 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001950
Andrew Walbranb4816552018-12-05 17:35:42 +00001951 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001952 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001953 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001954out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001955 sl_unlock(&vm->lock);
1956
Jose Marinho3e2442f2019-03-12 13:30:37 +00001957 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001958}
1959
1960/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001961 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1962 * by this function, the caller must have called api_mailbox_send before with
1963 * the notify argument set to true, and this call must have failed because the
1964 * mailbox was not available.
1965 *
1966 * It should be called repeatedly to retrieve a list of VMs.
1967 *
1968 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1969 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001970 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001971int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001972{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001973 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001974 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001975 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001976
1977 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001978 if (list_empty(&vm->mailbox.ready_list)) {
1979 ret = -1;
1980 goto exit;
1981 }
1982
1983 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1984 ready_links);
1985 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001986 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001987
1988exit:
1989 sl_unlock(&vm->lock);
1990 return ret;
1991}
1992
1993/**
1994 * Retrieves the next VM waiting to be notified that the mailbox of the
1995 * specified VM became writable. Only primary VMs are allowed to call this.
1996 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001997 * Returns -1 on failure or if there are no waiters; the VM id of the next
1998 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001999 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002000int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002001{
2002 struct vm *vm;
2003 struct vm_locked locked;
2004 struct wait_entry *entry;
2005 struct vm *waiting_vm;
2006
2007 /* Only primary VMs are allowed to call this function. */
2008 if (current->vm->id != HF_PRIMARY_VM_ID) {
2009 return -1;
2010 }
2011
Andrew Walbran42347a92019-05-09 13:59:03 +01002012 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002013 if (vm == NULL) {
2014 return -1;
2015 }
2016
Fuad Tabbaed294af2019-12-20 10:43:01 +00002017 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01002018 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002019 entry = api_fetch_waiter(locked);
2020 vm_unlock(&locked);
2021
2022 if (entry == NULL) {
2023 return -1;
2024 }
2025
2026 /* Enqueue notification to waiting VM. */
2027 waiting_vm = entry->waiting_vm;
2028
2029 sl_lock(&waiting_vm->lock);
2030 if (list_empty(&entry->ready_links)) {
2031 list_append(&waiting_vm->mailbox.ready_list,
2032 &entry->ready_links);
2033 }
2034 sl_unlock(&waiting_vm->lock);
2035
2036 return waiting_vm->id;
2037}
2038
2039/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002040 * Releases the caller's mailbox so that a new message can be received. The
2041 * caller must have copied out all data they wish to preserve as new messages
2042 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002043 *
2044 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002045 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2046 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002047 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2048 * - FFA_SUCCESS on success if no further action is needed.
2049 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002050 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002051 * hf_mailbox_waiter_get.
2052 */
Federico Recanati7bef0b92022-03-17 14:56:22 +01002053struct ffa_value api_ffa_rx_release(ffa_vm_id_t receiver_id,
J-Alvese8c8c2b2022-12-16 15:34:48 +00002054 struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002055{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002056 struct vm *current_vm = current->vm;
2057 struct vm *vm;
2058 struct vm_locked vm_locked;
2059 ffa_vm_id_t current_vm_id = current_vm->id;
2060 ffa_vm_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002061 struct ffa_value ret;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002062 int32_t error_code;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002063
Federico Recanati7bef0b92022-03-17 14:56:22 +01002064 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2065 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2066 dlog_error("Invalid `receiver_id`, must be zero.\n");
2067 return ffa_error(FFA_INVALID_PARAMETERS);
2068 }
2069
2070 /*
2071 * VM ID to be released: `receiver_id` if message has been forwarded by
2072 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2073 */
2074 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2075 release_vm_id = current_vm_id;
2076 } else {
2077 release_vm_id = receiver_id;
2078 }
2079
2080 vm_locked = plat_ffa_vm_find_locked(release_vm_id);
2081 vm = vm_locked.vm;
2082 if (vm == NULL) {
2083 dlog_error("No buffer registered for VM ID %#x.\n",
2084 release_vm_id);
2085 return ffa_error(FFA_INVALID_PARAMETERS);
2086 }
2087
J-Alvese8c8c2b2022-12-16 15:34:48 +00002088 if (plat_ffa_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002089 goto out;
2090 }
2091
J-Alvese8c8c2b2022-12-16 15:34:48 +00002092 if (!api_release_mailbox(vm_locked, &error_code)) {
2093 ret = ffa_error(error_code);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002094 goto out;
2095 }
2096
J-Alvese8c8c2b2022-12-16 15:34:48 +00002097 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Federico Recanati7bef0b92022-03-17 14:56:22 +01002098
2099out:
2100 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002101
2102 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002103}
Andrew Walbran318f5732018-11-20 16:23:42 +00002104
2105/**
Federico Recanati644f0462022-03-17 12:04:00 +01002106 * Acquire ownership of an RX buffer before writing to it. Both
2107 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2108 * could contend for the same buffer. SPMC owns VM's RX buffer after
2109 * it's mapped in its translation regime. This ABI should be
2110 * used by the Hypervisor to get the ownership of a VM's RX buffer
2111 * from the SPMC solving the aforementioned possible contention.
2112 *
2113 * Returns:
2114 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2115 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2116 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2117 */
2118struct ffa_value api_ffa_rx_acquire(ffa_vm_id_t receiver_id,
2119 struct vcpu *current)
2120{
2121 struct vm_locked receiver_locked;
2122 struct vm *receiver;
2123 struct ffa_value ret;
2124
2125 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
2126 !plat_ffa_is_vm_id(receiver_id)) {
2127 dlog_error(
2128 "FFA_RX_ACQUIRE not supported at this FF-A "
2129 "instance.\n");
2130 return ffa_error(FFA_NOT_SUPPORTED);
2131 }
2132
2133 receiver_locked = plat_ffa_vm_find_locked(receiver_id);
2134 receiver = receiver_locked.vm;
2135
2136 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2137 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2138 receiver_id);
2139 ret = ffa_error(FFA_INVALID_PARAMETERS);
2140 goto out;
2141 }
2142
2143 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2144 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2145 ret = ffa_error(FFA_DENIED);
2146 goto out;
2147 }
2148
J-Alvese8c8c2b2022-12-16 15:34:48 +00002149 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002150
2151 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2152
2153out:
2154 vm_unlock(&receiver_locked);
2155
2156 return ret;
2157}
2158
2159/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002160 * Enables or disables a given interrupt ID for the calling vCPU.
2161 *
2162 * Returns 0 on success, or -1 if the intid is invalid.
2163 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002164int64_t api_interrupt_enable(uint32_t intid, bool enable,
2165 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002166{
Manish Pandey35e452f2021-02-18 21:36:34 +00002167 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002168 struct interrupts *interrupts = &current->interrupts;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002169
Andrew Walbran318f5732018-11-20 16:23:42 +00002170 if (intid >= HF_NUM_INTIDS) {
2171 return -1;
2172 }
2173
Manish Pandey35e452f2021-02-18 21:36:34 +00002174 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00002175 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002176 /*
2177 * If it is pending and was not enabled before, increment the
2178 * count.
2179 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002180 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2181 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2182 vcpu_interrupt_count_increment(current_locked,
2183 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002184 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002185 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2186 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002187 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002188 /*
2189 * If it is pending and was enabled before, decrement the count.
2190 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002191 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2192 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2193 vcpu_interrupt_count_decrement(current_locked,
2194 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002195 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002196 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2197 vcpu_virt_interrupt_set_type(interrupts, intid,
2198 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002199 }
2200
Manish Pandey35e452f2021-02-18 21:36:34 +00002201 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002202 return 0;
2203}
2204
Madhukar Pappireddya1019112022-06-21 18:57:44 -05002205static void api_interrupt_clear_decrement(struct vcpu_locked locked_vcpu,
2206 struct interrupts *interrupts,
2207 uint32_t intid)
2208{
2209 vcpu_virt_interrupt_clear_pending(interrupts, intid);
2210 vcpu_interrupt_count_decrement(locked_vcpu, interrupts, intid);
2211}
2212
Andrew Walbran318f5732018-11-20 16:23:42 +00002213/**
2214 * Returns the ID of the next pending interrupt for the calling vCPU, and
2215 * acknowledges it (i.e. marks it as no longer pending). Returns
2216 * HF_INVALID_INTID if there are no pending interrupts.
2217 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00002218uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002219{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002220 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002221 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00002222 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002223 struct interrupts *interrupts = &current->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002224
2225 /*
2226 * Find the first enabled and pending interrupt ID, return it, and
2227 * deactivate it.
2228 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002229 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00002230 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2231 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002232 interrupts->interrupt_enabled.bitmap[i] &
2233 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002234
Andrew Walbran318f5732018-11-20 16:23:42 +00002235 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002236 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002237
2238 first_interrupt =
2239 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002240
Andrew Walbran3d84a262018-12-13 14:41:19 +00002241 /*
2242 * Mark it as no longer pending and decrement the count.
2243 */
Madhukar Pappireddya1019112022-06-21 18:57:44 -05002244 api_interrupt_clear_decrement(
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002245 current_locked, interrupts, first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002246 break;
2247 }
2248 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002249
Manish Pandey35e452f2021-02-18 21:36:34 +00002250 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002251 return first_interrupt;
2252}
2253
2254/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00002255 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00002256 * given VM and vCPU.
2257 */
2258static inline bool is_injection_allowed(uint32_t target_vm_id,
2259 struct vcpu *current)
2260{
2261 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002262
Andrew Walbran318f5732018-11-20 16:23:42 +00002263 /*
2264 * The primary VM is allowed to inject interrupts into any VM. Secondary
2265 * VMs are only allowed to inject interrupts into their own vCPUs.
2266 */
2267 return current_vm_id == HF_PRIMARY_VM_ID ||
2268 current_vm_id == target_vm_id;
2269}
2270
2271/**
2272 * Injects a virtual interrupt of the given ID into the given target vCPU.
2273 * This doesn't cause the vCPU to actually be run immediately; it will be taken
2274 * when the vCPU is next run, which is up to the scheduler.
2275 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00002276 * Returns:
2277 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
2278 * ID is invalid, or the current VM is not allowed to inject interrupts to
2279 * the target VM.
2280 * - 0 on success if no further action is needed.
2281 * - 1 if it was called by the primary VM and the primary VM now needs to wake
2282 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00002283 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002284int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
2285 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01002286 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00002287{
Andrew Walbran318f5732018-11-20 16:23:42 +00002288 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01002289 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00002290
2291 if (intid >= HF_NUM_INTIDS) {
2292 return -1;
2293 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002294
Andrew Walbran318f5732018-11-20 16:23:42 +00002295 if (target_vm == NULL) {
2296 return -1;
2297 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002298
Andrew Walbran318f5732018-11-20 16:23:42 +00002299 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00002300 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00002301 return -1;
2302 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002303
Andrew Walbran318f5732018-11-20 16:23:42 +00002304 if (!is_injection_allowed(target_vm_id, current)) {
2305 return -1;
2306 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002307
Andrew Walbrane1310df2019-04-29 17:28:28 +01002308 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00002309
Manish Pandey35e452f2021-02-18 21:36:34 +00002310 dlog_verbose(
2311 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
2312 "%u\n",
2313 intid, target_vm_id, target_vcpu_idx, current->vm->id,
2314 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01002315 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00002316}
Andrew Scull6386f252018-12-06 13:29:10 +00002317
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002318/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002319struct ffa_value api_ffa_version(struct vcpu *current,
2320 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002321{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002322 struct vm_locked current_vm_locked;
2323
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002324 /*
2325 * Ensure that both major and minor revision representation occupies at
2326 * most 15 bits.
2327 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002328 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002329 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002330 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002331 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002332 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01002333 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00002334 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01002335 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002336
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002337 current_vm_locked = vm_lock(current->vm);
2338 current_vm_locked.vm->ffa_version = requested_version;
2339 vm_unlock(&current_vm_locked);
2340
Daniel Boulby6e32c612021-02-17 15:09:41 +00002341 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002342}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002343
2344int64_t api_debug_log(char c, struct vcpu *current)
2345{
Andrew Sculld54e1be2019-08-20 11:09:42 +01002346 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002347 struct vm *vm = current->vm;
2348 struct vm_locked vm_locked = vm_lock(vm);
2349
Andrew Sculld54e1be2019-08-20 11:09:42 +01002350 if (c == '\n' || c == '\0') {
2351 flush = true;
2352 } else {
2353 vm->log_buffer[vm->log_buffer_length++] = c;
2354 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
2355 }
2356
2357 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01002358 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
2359 vm->log_buffer_length);
2360 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002361 }
2362
2363 vm_unlock(&vm_locked);
2364
2365 return 0;
2366}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002367
2368/**
J-Alves6f72ca82021-11-01 12:34:58 +00002369 * Helper for success return of FFA_FEATURES, for when it is used to query
2370 * an interrupt ID.
2371 */
2372struct ffa_value api_ffa_feature_success(uint32_t arg2)
2373{
2374 return (struct ffa_value){
2375 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
2376}
2377
2378/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002379 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002380 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002381 */
Karl Meakin34b8ae92023-01-13 13:33:07 +00002382struct ffa_value api_ffa_features(uint32_t feature_function_id,
2383 uint32_t input_property, uint32_t ffa_version)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002384{
J-Alves6f72ca82021-11-01 12:34:58 +00002385 /*
2386 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
2387 * if using a feature ID.
2388 */
2389 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
J-Alvesff676c12022-05-13 17:25:33 +01002390 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0U) {
J-Alves6f72ca82021-11-01 12:34:58 +00002391 return ffa_error(FFA_NOT_SUPPORTED);
2392 }
2393
Karl Meakin34b8ae92023-01-13 13:33:07 +00002394 if (feature_function_id != FFA_MEM_RETRIEVE_REQ_32 &&
2395 input_property != 0U) {
2396 dlog_verbose(
2397 "input_property must be zero.\ninput_property = %u.\n",
2398 input_property);
2399 return ffa_error(FFA_INVALID_PARAMETERS);
2400 }
2401
J-Alves6f72ca82021-11-01 12:34:58 +00002402 switch (feature_function_id) {
2403 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002404 case FFA_ERROR_32:
2405 case FFA_SUCCESS_32:
2406 case FFA_INTERRUPT_32:
2407 case FFA_VERSION_32:
2408 case FFA_FEATURES_32:
2409 case FFA_RX_RELEASE_32:
2410 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002411 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002412 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002413 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002414 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002415 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002416 case FFA_MEM_DONATE_32:
2417 case FFA_MEM_LEND_32:
2418 case FFA_MEM_SHARE_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002419 case FFA_MEM_RETRIEVE_RESP_32:
2420 case FFA_MEM_RELINQUISH_32:
2421 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002422 case FFA_MEM_FRAG_RX_32:
2423 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002424 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002425 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002426 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002427 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00002428#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002429 /* FF-A v1.1 features. */
2430 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002431 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2432 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2433 case FFA_NOTIFICATION_BIND_32:
2434 case FFA_NOTIFICATION_UNBIND_32:
2435 case FFA_NOTIFICATION_SET_32:
2436 case FFA_NOTIFICATION_GET_32:
2437 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07002438 case FFA_MEM_PERM_GET_32:
2439 case FFA_MEM_PERM_SET_32:
2440 case FFA_MEM_PERM_GET_64:
2441 case FFA_MEM_PERM_SET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002442 case FFA_MSG_SEND2_32:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002443 case FFA_PARTITION_INFO_GET_REGS_64:
J-Alves3829fc02021-03-18 12:49:18 +00002444#endif
2445 return (struct ffa_value){.func = FFA_SUCCESS_32};
Karl Meakin34b8ae92023-01-13 13:33:07 +00002446 case FFA_MEM_RETRIEVE_REQ_32:
2447 if ((input_property & FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_MASK) !=
2448 0U) {
2449 dlog_verbose(
2450 "Bits[31:2] and Bit[0] of input_property must "
2451 "be zero.\ninput_property = %u.\n",
2452 input_property);
2453 return ffa_error(FFA_INVALID_PARAMETERS);
2454 }
2455
2456 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
2457 if ((input_property &
2458 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
2459 dlog_verbose("NS bit support must be 1.\n");
2460 return ffa_error(FFA_INVALID_PARAMETERS);
2461 }
2462 }
2463
2464 return api_ffa_feature_success(
2465 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
2466 (input_property &
2467 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) |
2468 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
J-Alves6f72ca82021-11-01 12:34:58 +00002469
2470#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2471 /* Check support of a feature provided respective feature ID. */
2472 case FFA_FEATURE_NPI:
2473 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2474 case FFA_FEATURE_SRI:
2475 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2476#endif
2477 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002478 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002479 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002480 }
2481}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002482
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002483/**
J-Alves645eabe2021-02-22 16:08:27 +00002484 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2485 * interfaces.
2486 */
2487static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2488{
2489 return args.arg2 == 0U;
2490}
2491
2492/**
J-Alves76d99af2021-03-10 17:42:11 +00002493 * Limits size of arguments in ffa_value structure to 32-bit.
2494 */
2495static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2496{
2497 return (struct ffa_value){
2498 .func = (uint32_t)args.func,
2499 .arg1 = (uint32_t)args.arg1,
2500 .arg2 = (uint32_t)0,
2501 .arg3 = (uint32_t)args.arg3,
2502 .arg4 = (uint32_t)args.arg4,
2503 .arg5 = (uint32_t)args.arg5,
2504 .arg6 = (uint32_t)args.arg6,
2505 .arg7 = (uint32_t)args.arg7,
2506 };
2507}
2508
2509/**
2510 * Helper to copy direct message payload, depending on SMC used and expected
2511 * registers size.
2512 */
2513static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2514{
2515 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2516 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2517 return api_ffa_value_copy32(args);
2518 }
2519
2520 return (struct ffa_value){
2521 .func = args.func,
2522 .arg1 = args.arg1,
2523 .arg2 = 0,
2524 .arg3 = args.arg3,
2525 .arg4 = args.arg4,
2526 .arg5 = args.arg5,
2527 .arg6 = args.arg6,
2528 .arg7 = args.arg7,
2529 };
2530}
2531
2532/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002533 * Send an FF-A direct message request.
2534 */
2535struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2536 ffa_vm_id_t receiver_vm_id,
2537 struct ffa_value args,
2538 struct vcpu *current,
2539 struct vcpu **next)
2540{
J-Alves17228f72021-04-20 17:13:19 +01002541 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002542 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002543 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002544 struct vcpu *receiver_vcpu;
2545 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002546 enum vcpu_state next_state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002547
J-Alves645eabe2021-02-22 16:08:27 +00002548 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2549 return ffa_error(FFA_INVALID_PARAMETERS);
2550 }
2551
Olivier Deprez55a189e2021-06-09 15:45:27 +02002552 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2553 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002554 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002555 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002556 }
2557
Olivier Deprez55a189e2021-06-09 15:45:27 +02002558 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002559 return ret;
2560 }
2561
2562 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2563
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002564 receiver_vm = vm_find(receiver_vm_id);
2565 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002566 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002567 return ffa_error(FFA_INVALID_PARAMETERS);
2568 }
2569
2570 /*
J-Alves439ac972021-11-18 17:32:03 +00002571 * Check if sender supports sending direct message req, and if
2572 * receiver supports receipt of direct message requests.
2573 */
2574 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2575 return ffa_error(FFA_DENIED);
2576 }
2577
2578 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002579 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002580 * UP (migratable) or MP partitions with a number of vCPUs matching the
2581 * number of PEs in the system. It further states that MP partitions
2582 * accepting direct request messages cannot migrate.
2583 */
J-Alvesad6a0432021-04-09 16:06:21 +01002584 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002585 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002586 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002587 return ffa_error(FFA_INVALID_PARAMETERS);
2588 }
2589
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002590 if (!plat_ffa_check_runtime_state_transition(
2591 current, sender_vm_id, HF_INVALID_VM_ID, receiver_vcpu,
2592 args.func, &next_state)) {
2593 return ffa_error(FFA_DENIED);
2594 }
2595
J-Alves6e2abc62021-12-02 14:58:56 +00002596 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002597 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2598
2599 /*
2600 * If destination vCPU is executing or already received an
2601 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2602 * busy. There is a brief period of time where the vCPU state has
2603 * changed but regs_available is still false thus consider this case as
2604 * the vCPU not yet ready to receive a direct message request.
2605 */
2606 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2607 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2608 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002609 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002610 ret = ffa_error(FFA_BUSY);
2611 goto out;
2612 }
2613
2614 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2615 memory_order_relaxed)) {
2616 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002617 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002618 receiver_vcpu->vm->id,
2619 vcpu_index(receiver_vcpu));
2620 receiver_vcpu->state = VCPU_STATE_ABORTED;
2621 }
2622
2623 ret = ffa_error(FFA_ABORTED);
2624 goto out;
2625 }
2626
2627 switch (receiver_vcpu->state) {
2628 case VCPU_STATE_OFF:
2629 case VCPU_STATE_RUNNING:
2630 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002631 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002632 case VCPU_STATE_BLOCKED:
2633 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002634 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2635 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002636 ret = ffa_error(FFA_BUSY);
2637 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002638 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002639 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002640 * We expect target vCPU to be in WAITING state after either
2641 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002642 */
2643 break;
2644 }
2645
2646 /* Inject timer interrupt if any pending */
2647 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002648 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2649 HF_VIRTUAL_TIMER_INTID, current,
2650 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002651
2652 arch_timer_mask(&receiver_vcpu->regs);
2653 }
2654
2655 /* The receiver vCPU runs upon direct message invocation */
2656 receiver_vcpu->cpu = current->cpu;
2657 receiver_vcpu->state = VCPU_STATE_RUNNING;
2658 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002659 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002660
J-Alves76d99af2021-03-10 17:42:11 +00002661 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002662
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002663 assert(next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002664 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002665
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05002666 plat_ffa_wind_call_chain_ffa_direct_req(vcpus_locked.vcpu2,
2667 vcpus_locked.vcpu1);
2668
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002669 /* Switch to receiver vCPU targeted to by direct msg request */
2670 *next = receiver_vcpu;
2671
J-Alves6e2abc62021-12-02 14:58:56 +00002672 if (!receiver_locked.vm->el0_partition) {
2673 /*
2674 * If the scheduler in the system is giving CPU cycles to the
2675 * receiver, due to pending notifications, inject the NPI
2676 * interrupt. Following call assumes that '*next' has been set
2677 * to receiver_vcpu.
2678 */
2679 plat_ffa_inject_notification_pending_interrupt(
2680 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2681 ? vcpus_locked.vcpu1
2682 : vcpus_locked.vcpu2,
2683 current, receiver_locked);
2684 }
2685
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002686 /*
2687 * Since this flow will lead to a VM switch, the return value will not
2688 * be applied to current vCPU.
2689 */
2690
2691out:
2692 sl_unlock(&receiver_vcpu->lock);
2693 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002694 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002695
2696 return ret;
2697}
2698
2699/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002700 * Resume the target vCPU after the current vCPU sent a direct response.
2701 * Current vCPU moves to waiting state.
2702 */
2703void api_ffa_resume_direct_resp_target(struct vcpu *current, struct vcpu **next,
2704 ffa_vm_id_t receiver_vm_id,
2705 struct ffa_value to_ret,
2706 bool is_nwd_call_chain)
2707{
2708 if (!vm_id_is_current_world(receiver_vm_id)) {
2709 *next = api_switch_to_other_world(current, to_ret,
2710 VCPU_STATE_WAITING);
2711
2712 /* End of NWd scheduled call chain. */
2713 assert(!is_nwd_call_chain ||
2714 (current->call_chain.prev_node == NULL));
2715 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
2716 *next = api_switch_to_primary(current, to_ret,
2717 VCPU_STATE_WAITING);
2718
2719 /* Removing a node from NWd scheduled call chain. */
2720 if (is_nwd_call_chain) {
2721 vcpu_call_chain_remove_node(current, *next);
2722 }
2723 } else if (vm_id_is_current_world(receiver_vm_id)) {
2724 /*
2725 * It is expected the receiver_vm_id to be from an SP, otherwise
2726 * 'plat_ffa_is_direct_response_valid' should have
2727 * made function return error before getting to this point.
2728 */
2729 *next = api_switch_to_vm(current, to_ret, VCPU_STATE_WAITING,
2730 receiver_vm_id);
2731 } else {
2732 panic("Invalid direct message response invocation");
2733 }
2734}
2735
2736/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002737 * Send an FF-A direct message response.
2738 */
2739struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2740 ffa_vm_id_t receiver_vm_id,
2741 struct ffa_value args,
2742 struct vcpu *current,
2743 struct vcpu **next)
2744{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002745 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002746 enum vcpu_state next_state = VCPU_STATE_WAITING;
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002747 struct ffa_value signal_interrupt =
2748 (struct ffa_value){.func = FFA_INTERRUPT_32};
J-Alves645eabe2021-02-22 16:08:27 +00002749
2750 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2751 return ffa_error(FFA_INVALID_PARAMETERS);
2752 }
2753
J-Alves76d99af2021-03-10 17:42:11 +00002754 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002755
Olivier Deprez55a189e2021-06-09 15:45:27 +02002756 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2757 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002758 dlog_verbose("Invalid direct response call.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002759 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002760 }
2761
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002762 if (!plat_ffa_check_runtime_state_transition(current, sender_vm_id,
2763 receiver_vm_id, NULL,
2764 args.func, &next_state)) {
2765 return ffa_error(FFA_DENIED);
2766 }
2767
Madhukar Pappireddy5fd32482022-01-07 14:53:26 -06002768 if (plat_ffa_is_direct_response_interrupted(current)) {
2769 return ffa_error(FFA_INTERRUPTED);
2770 }
2771
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002772 assert(next_state == VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002773 current_locked = vcpu_lock(current);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05002774
2775 /*
2776 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2777 * defined originator.
2778 */
2779 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2780 /*
2781 * Sending direct response but direct request origin
2782 * vCPU is not set.
2783 */
2784 vcpu_unlock(&current_locked);
2785 return ffa_error(FFA_DENIED);
2786 }
2787
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002788 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002789 /*
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05002790 * Per FF-A v1.1 EAC0 section 8.3.1.2.1 rule 6, SPMC can signal
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002791 * a secure interrupt to a SP that is performing managed exit.
2792 * We have taken a implementation defined choice to not allow
2793 * Managed exit while a SP is processing a secure interrupt.
2794 */
2795 CHECK(!current->processing_secure_interrupt);
2796
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06002797 plat_interrupts_set_priority_mask(current->priority_mask);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05002798 /*
2799 * A SP may be signaled a managed exit but actually not trap
2800 * the virtual interrupt, probably because it has virtual
2801 * interrupts masked, and emit direct resp. In this case the
2802 * managed exit operation is considered completed and it would
2803 * also need to clear the pending managed exit flag for the SP
2804 * vCPU.
2805 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002806 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05002807 struct interrupts *interrupts = &current->interrupts;
2808
2809 if (vcpu_is_virt_interrupt_pending(interrupts,
2810 HF_MANAGED_EXIT_INTID)) {
2811 api_interrupt_clear_decrement(current_locked,
2812 interrupts,
2813 HF_MANAGED_EXIT_INTID);
2814 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002815 }
2816
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002817 if (plat_ffa_intercept_direct_response(current_locked, next, to_ret,
2818 &signal_interrupt)) {
2819 vcpu_unlock(&current_locked);
2820 return signal_interrupt;
2821 }
2822
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002823 /* Clear direct request origin for the caller. */
2824 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2825
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002826 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002827
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002828 api_ffa_resume_direct_resp_target(current, next, receiver_vm_id, to_ret,
2829 false);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002830
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05002831 plat_ffa_unwind_call_chain_ffa_direct_resp(current, *next);
2832
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002833 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2834}
2835
J-Alves84658fc2021-06-17 14:37:32 +01002836static bool api_memory_region_check_flags(
2837 struct ffa_memory_region *memory_region, uint32_t share_func)
2838{
2839 switch (share_func) {
2840 case FFA_MEM_SHARE_32:
2841 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2842 0U) {
2843 return false;
2844 }
2845 /* Intentional fall-through */
2846 case FFA_MEM_LEND_32:
2847 case FFA_MEM_DONATE_32: {
2848 /* Bits 31:2 Must Be Zero. */
2849 ffa_memory_receiver_flags_t to_mask =
2850 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2851 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2852
2853 if ((memory_region->flags & to_mask) != 0U) {
2854 return false;
2855 }
2856 break;
2857 }
2858 default:
2859 panic("Check for mem send calls only.\n");
2860 }
2861
2862 /* Last check reserved values are 0 */
2863 return true;
2864}
2865
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002866struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2867 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002868 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002869{
2870 struct vm *from = current->vm;
2871 struct vm *to;
2872 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002873 struct ffa_memory_region *memory_region;
2874 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01002875 bool targets_other_world = false;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002876
2877 if (ipa_addr(address) != 0 || page_count != 0) {
2878 /*
2879 * Hafnium only supports passing the descriptor in the TX
2880 * mailbox.
2881 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002882 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002883 }
2884
Andrew Walbranca808b12020-05-15 17:22:28 +01002885 if (fragment_length > length) {
2886 dlog_verbose(
2887 "Fragment length %d greater than total length %d.\n",
2888 fragment_length, length);
2889 return ffa_error(FFA_INVALID_PARAMETERS);
2890 }
2891 if (fragment_length < sizeof(struct ffa_memory_region) +
2892 sizeof(struct ffa_memory_access)) {
2893 dlog_verbose(
2894 "Initial fragment length %d smaller than header size "
2895 "%d.\n",
2896 fragment_length,
2897 sizeof(struct ffa_memory_region) +
2898 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002899 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002900 }
2901
2902 /*
2903 * Check that the sender has configured its send buffer. If the TX
2904 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2905 * be safely accessed after releasing the lock since the TX mailbox
2906 * address can only be configured once.
2907 */
2908 sl_lock(&from->lock);
2909 from_msg = from->mailbox.send;
2910 sl_unlock(&from->lock);
2911
2912 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002913 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002914 }
2915
2916 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002917 * Copy the memory region descriptor to a fresh page from the memory
2918 * pool. This prevents the sender from changing it underneath us, and
2919 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002920 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002921 if (fragment_length > HF_MAILBOX_SIZE ||
2922 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002923 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002924 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002925 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002926 if (memory_region == NULL) {
2927 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002928 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002929 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002930 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002931
J-Alves84658fc2021-06-17 14:37:32 +01002932 if (!api_memory_region_check_flags(memory_region, share_func)) {
2933 dlog_verbose(
2934 "Memory region reserved arguments must be zero.\n");
2935 ret = ffa_error(FFA_INVALID_PARAMETERS);
2936 goto out;
2937 }
2938
J-Alves363f5722022-04-25 17:37:37 +01002939 if (memory_region->receiver_count == 0U) {
2940 dlog_verbose("Receiver count can't be 0.\n");
2941 ret = ffa_error(FFA_INVALID_PARAMETERS);
2942 goto out;
2943 }
2944
2945 if (share_func == FFA_MEM_DONATE_32 &&
2946 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002947 dlog_verbose(
J-Alves363f5722022-04-25 17:37:37 +01002948 "FFA_MEM_DONATE only supports one recipient. "
2949 "Specified %u\n",
2950 memory_region->receiver_count);
2951 ret = ffa_error(FFA_INVALID_PARAMETERS);
2952 goto out;
2953 }
2954
2955 if (memory_region->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
2956 dlog_verbose(
2957 "Max number of recipients supported is %u "
2958 "specified %u\n",
2959 MAX_MEM_SHARE_RECIPIENTS,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002960 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002961 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002962 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002963 }
2964
2965 /*
2966 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01002967 * If there is a receiver from the other world, track it for later
2968 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002969 */
J-Alves363f5722022-04-25 17:37:37 +01002970 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
2971 ffa_vm_id_t receiver_id =
2972 memory_region->receivers[i]
2973 .receiver_permissions.receiver;
2974 to = vm_find(receiver_id);
2975
2976 if (vm_id_is_current_world(receiver_id) &&
2977 (to == NULL || to == from)) {
2978 dlog_verbose("%s: invalid receiver.\n", __func__);
2979 ret = ffa_error(FFA_INVALID_PARAMETERS);
2980 goto out;
2981 }
2982
2983 if (!plat_ffa_is_memory_send_valid(receiver_id, share_func)) {
2984 ret = ffa_error(FFA_DENIED);
2985 goto out;
2986 }
2987
2988 /* Capture if any of the receivers is from the other world. */
2989 if (!targets_other_world) {
2990 targets_other_world =
2991 !vm_id_is_current_world(receiver_id);
2992 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002993 }
2994
J-Alves363f5722022-04-25 17:37:37 +01002995 if (targets_other_world) {
J-Alves66652252022-07-06 09:49:51 +01002996 ret = plat_ffa_other_world_mem_send(
2997 from, share_func, &memory_region, length,
2998 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002999 } else {
3000 struct vm_locked from_locked = vm_lock(from);
3001
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003002 ret = ffa_memory_send(from_locked, memory_region, length,
3003 fragment_length, share_func,
3004 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003005 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003006 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003007 * make sure we don't free it.
3008 */
3009 memory_region = NULL;
3010
3011 vm_unlock(&from_locked);
3012 }
3013
3014out:
3015 if (memory_region != NULL) {
3016 mpool_free(&api_page_pool, memory_region);
3017 }
3018
3019 return ret;
3020}
3021
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003022struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3023 uint32_t fragment_length,
3024 ipaddr_t address, uint32_t page_count,
3025 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003026{
3027 struct vm *to = current->vm;
3028 struct vm_locked to_locked;
3029 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003030 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003031 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003032 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003033
3034 if (ipa_addr(address) != 0 || page_count != 0) {
3035 /*
3036 * Hafnium only supports passing the descriptor in the TX
3037 * mailbox.
3038 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003039 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003040 }
3041
Andrew Walbrana65a1322020-04-06 19:32:32 +01003042 if (fragment_length != length) {
3043 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003044 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003045 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003046
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003047 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003048 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003049 message_buffer_size = cpu_get_buffer_size(current->cpu);
3050 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3051 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003052 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003053 }
3054
3055 to_locked = vm_lock(to);
3056 to_msg = to->mailbox.send;
3057
3058 if (to_msg == NULL) {
3059 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003060 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003061 goto out;
3062 }
3063
3064 /*
3065 * Copy the retrieve request descriptor to an internal buffer, so that
3066 * the caller can't change it underneath us.
3067 */
3068 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
3069
J-Alvese8c8c2b2022-12-16 15:34:48 +00003070 if ((vm_is_mailbox_other_world_owned(to_locked) &&
3071 !plat_ffa_acquire_receiver_rx(to_locked, &ret)) ||
3072 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003073 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003074 * Can't retrieve memory information if the mailbox is
3075 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003076 */
J-Alves59ed0042022-07-28 18:26:41 +01003077 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003078 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003079 goto out;
3080 }
3081
J-Alvesb5084cf2022-07-06 14:20:12 +01003082 if (plat_ffa_memory_handle_allocated_by_current_world(
3083 retrieve_request->handle)) {
3084 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3085 &api_page_pool);
3086 } else {
3087 ret = plat_ffa_other_world_mem_retrieve(
3088 to_locked, retrieve_request, length, &api_page_pool);
3089 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003090out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003091 vm_unlock(&to_locked);
3092 return ret;
3093}
3094
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003095struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003096{
3097 struct vm *from = current->vm;
3098 struct vm_locked from_locked;
3099 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003100 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003101 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003102 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003103 uint32_t length;
3104
3105 from_locked = vm_lock(from);
3106 from_msg = from->mailbox.send;
3107
3108 if (from_msg == NULL) {
3109 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003110 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003111 goto out;
3112 }
3113
3114 /*
3115 * Calculate length from relinquish descriptor before copying. We will
3116 * check again later to make sure it hasn't changed.
3117 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003118 length = sizeof(struct ffa_mem_relinquish) +
3119 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
3120 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003121 /*
3122 * Copy the relinquish descriptor to an internal buffer, so that the
3123 * caller can't change it underneath us.
3124 */
3125 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003126 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003127 message_buffer_size = cpu_get_buffer_size(current->cpu);
3128 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3129 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003130 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003131 goto out;
3132 }
3133 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
3134
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003135 if (sizeof(struct ffa_mem_relinquish) +
3136 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003137 length) {
3138 dlog_verbose(
3139 "Endpoint count changed while copying to internal "
3140 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003141 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003142 goto out;
3143 }
3144
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003145 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3146 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003147
3148out:
3149 vm_unlock(&from_locked);
3150 return ret;
3151}
3152
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003153struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3154 ffa_memory_region_flags_t flags,
3155 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003156{
3157 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003158 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003159
Olivier Deprez55a189e2021-06-09 15:45:27 +02003160 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003161 struct vm_locked to_locked = vm_lock(to);
3162
Andrew Walbranca808b12020-05-15 17:22:28 +01003163 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003164 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003165
Andrew Walbran290b0c92020-02-03 16:37:14 +00003166 vm_unlock(&to_locked);
3167 } else {
J-Alvesfc19b372022-07-06 12:17:35 +01003168 ret = plat_ffa_other_world_mem_reclaim(to, handle, flags,
3169 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003170 }
3171
3172 return ret;
3173}
3174
3175struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
3176 uint32_t fragment_offset,
3177 ffa_vm_id_t sender_vm_id,
3178 struct vcpu *current)
3179{
3180 struct vm *to = current->vm;
3181 struct vm_locked to_locked;
3182 struct ffa_value ret;
3183
3184 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01003185 if (vm_id_is_current_world(to->id)) {
3186 if (sender_vm_id != 0) {
3187 dlog_verbose("%s: Invalid sender.\n", __func__);
3188 return ffa_error(FFA_INVALID_PARAMETERS);
3189 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003190 }
3191
3192 to_locked = vm_lock(to);
3193
J-Alves122f1a12022-12-12 15:55:42 +00003194 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003195 /*
3196 * Can't retrieve memory information if the mailbox is not
3197 * available.
3198 */
J-Alves59ed0042022-07-28 18:26:41 +01003199 dlog_verbose("%s: RX buffer not ready partition %x.\n",
3200 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003201 ret = ffa_error(FFA_BUSY);
3202 goto out;
3203 }
3204
3205 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01003206 sender_vm_id, &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003207out:
3208 vm_unlock(&to_locked);
3209 return ret;
3210}
3211
3212struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
3213 uint32_t fragment_length,
3214 ffa_vm_id_t sender_vm_id,
3215 struct vcpu *current)
3216{
3217 struct vm *from = current->vm;
3218 const void *from_msg;
3219 void *fragment_copy;
3220 struct ffa_value ret;
3221
3222 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01003223 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
3224 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01003225 return ffa_error(FFA_INVALID_PARAMETERS);
3226 }
3227
3228 /*
3229 * Check that the sender has configured its send buffer. If the TX
3230 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3231 * be safely accessed after releasing the lock since the TX mailbox
3232 * address can only be configured once.
3233 */
3234 sl_lock(&from->lock);
3235 from_msg = from->mailbox.send;
3236 sl_unlock(&from->lock);
3237
3238 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01003239 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003240 return ffa_error(FFA_INVALID_PARAMETERS);
3241 }
3242
3243 /*
3244 * Copy the fragment to a fresh page from the memory pool. This prevents
3245 * the sender from changing it underneath us, and also lets us keep it
3246 * around in the share state table if needed.
3247 */
3248 if (fragment_length > HF_MAILBOX_SIZE ||
3249 fragment_length > MM_PPOOL_ENTRY_SIZE) {
3250 dlog_verbose(
3251 "Fragment length %d larger than mailbox size %d.\n",
3252 fragment_length, HF_MAILBOX_SIZE);
3253 return ffa_error(FFA_INVALID_PARAMETERS);
3254 }
3255 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
3256 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
3257 0) {
3258 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
3259 return ffa_error(FFA_INVALID_PARAMETERS);
3260 }
3261 fragment_copy = mpool_alloc(&api_page_pool);
3262 if (fragment_copy == NULL) {
3263 dlog_verbose("Failed to allocate fragment copy.\n");
3264 return ffa_error(FFA_NO_MEMORY);
3265 }
3266 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
3267
3268 /*
3269 * Hafnium doesn't support fragmentation of memory retrieve requests
3270 * (because it doesn't support caller-specified mappings, so a request
3271 * will never be larger than a single page), so this must be part of a
3272 * memory send (i.e. donate, lend or share) request.
3273 *
3274 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01003275 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01003276 */
J-Alvesfdd29272022-07-19 13:16:31 +01003277 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003278 struct vm_locked from_locked = vm_lock(from);
3279
3280 ret = ffa_memory_send_continue(from_locked, fragment_copy,
3281 fragment_length, handle,
3282 &api_page_pool);
3283 /*
3284 * `ffa_memory_send_continue` takes ownership of the
3285 * fragment_copy, so we don't need to free it here.
3286 */
3287 vm_unlock(&from_locked);
3288 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01003289 ret = plat_ffa_other_world_mem_send_continue(
3290 from, fragment_copy, fragment_length, handle,
3291 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003292 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003293
3294 return ret;
3295}
Max Shvetsov40108e72020-08-27 12:39:50 +01003296
Olivier Deprezd614d322021-06-18 15:21:00 +02003297/**
3298 * Register an entry point for a vCPU in warm boot cases.
3299 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
3300 */
Max Shvetsov40108e72020-08-27 12:39:50 +01003301struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
3302 struct vcpu *current)
3303{
3304 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01003305 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01003306
Olivier Deprezd614d322021-06-18 15:21:00 +02003307 /*
3308 * Reject if interface is not supported at this FF-A instance
3309 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
3310 */
3311 if (!plat_ffa_is_secondary_ep_register_supported() ||
3312 current->vm->vcpu_count == 1) {
3313 return ffa_error(FFA_NOT_SUPPORTED);
3314 }
3315
3316 /*
3317 * No further check is made on the address validity
3318 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
3319 * from the VM or vCPU structure.
3320 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
3321 * For each SP [...] the Framework assumes that the same entry point
3322 * address is used for initializing any execution context during a
3323 * secondary cold boot.
3324 * If this function is invoked multiple times, then the entry point
3325 * address specified in the last valid invocation must be used by the
3326 * callee.
3327 */
Olivier Deprezb2808332023-02-02 15:25:40 +01003328 current_locked = vcpu_lock(current);
3329 if (current->rt_model != RTM_SP_INIT) {
3330 dlog_error(
3331 "FFA_SECONDARY_EP_REGISTER can only be called while "
3332 "vCPU in run-time state for initialization.\n");
3333 vcpu_unlock(&current_locked);
3334 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02003335 }
Olivier Deprezb2808332023-02-02 15:25:40 +01003336 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02003337
Olivier Deprezb2808332023-02-02 15:25:40 +01003338 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01003339 vm_locked.vm->secondary_ep = entry_point;
3340 vm_unlock(&vm_locked);
3341
Olivier Deprezb2808332023-02-02 15:25:40 +01003342 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01003343}
J-Alvesa0f317d2021-06-09 13:31:59 +01003344
3345struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
3346 ffa_vcpu_count_t vcpu_count,
3347 struct vcpu *current)
3348{
3349 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
3350 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
3351 vm_id);
3352 return ffa_error(FFA_NOT_SUPPORTED);
3353 }
3354
3355 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
3356}
3357
3358struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
3359 struct vcpu *current)
3360{
3361 /*
3362 * Validity of use of this interface is the same as for bitmap create.
3363 */
3364 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
3365 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
3366 vm_id);
3367 return ffa_error(FFA_NOT_SUPPORTED);
3368 }
3369
3370 return plat_ffa_notifications_bitmap_destroy(vm_id);
3371}
J-Alvesc003a7a2021-03-18 13:06:53 +00003372
3373struct ffa_value api_ffa_notification_update_bindings(
3374 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3375 ffa_notifications_bitmap_t notifications, bool is_bind,
3376 struct vcpu *current)
3377{
3378 struct ffa_value ret = {.func = FFA_SUCCESS_32};
3379 struct vm_locked receiver_locked;
3380 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3381 const ffa_vm_id_t id_to_update =
3382 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
3383 const ffa_vm_id_t id_to_validate =
3384 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01003385 const uint32_t flags_mbz =
3386 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
3387
3388 if ((flags_mbz & flags) != 0U) {
3389 return ffa_error(FFA_INVALID_PARAMETERS);
3390 }
J-Alvesc003a7a2021-03-18 13:06:53 +00003391
3392 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
3393 receiver_vm_id)) {
3394 dlog_verbose("Invalid use of notifications bind interface.\n");
3395 return ffa_error(FFA_INVALID_PARAMETERS);
3396 }
3397
J-Alvesb15e9402021-09-08 11:44:42 +01003398 if (plat_ffa_notifications_update_bindings_forward(
3399 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
3400 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01003401 return ret;
3402 }
3403
J-Alvesc003a7a2021-03-18 13:06:53 +00003404 if (notifications == 0U) {
3405 dlog_verbose("No notifications have been specified.\n");
3406 return ffa_error(FFA_INVALID_PARAMETERS);
3407 }
3408
3409 /**
3410 * This check assumes receiver is the current VM, and has been enforced
3411 * by 'plat_ffa_is_notifications_bind_valid'.
3412 */
3413 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3414
3415 if (receiver_locked.vm == NULL) {
3416 dlog_verbose("Receiver doesn't exist!\n");
3417 return ffa_error(FFA_DENIED);
3418 }
3419
J-Alves09ff9d82021-11-02 11:55:20 +00003420 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00003421 dlog_verbose("Notifications are not enabled.\n");
3422 ret = ffa_error(FFA_NOT_SUPPORTED);
3423 goto out;
3424 }
3425
3426 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
3427 vm_find(sender_vm_id) == NULL) {
3428 dlog_verbose("Sender VM does not exist!\n");
3429 ret = ffa_error(FFA_INVALID_PARAMETERS);
3430 goto out;
3431 }
3432
3433 /*
3434 * Can't bind/unbind notifications if at least one is bound to a
3435 * different sender.
3436 */
3437 if (!vm_notifications_validate_bound_sender(
3438 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3439 id_to_validate, notifications)) {
3440 dlog_verbose("Notifications are bound to other sender.\n");
3441 ret = ffa_error(FFA_DENIED);
3442 goto out;
3443 }
3444
3445 /**
3446 * Check if there is a pending notification within those specified in
3447 * the bitmap.
3448 */
3449 if (vm_are_notifications_pending(receiver_locked,
3450 plat_ffa_is_vm_id(sender_vm_id),
3451 notifications)) {
3452 dlog_verbose("Notifications within '%x' pending.\n",
3453 notifications);
3454 ret = ffa_error(FFA_DENIED);
3455 goto out;
3456 }
3457
3458 vm_notifications_update_bindings(
3459 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
3460 notifications, is_per_vcpu && is_bind);
3461
3462out:
3463 vm_unlock(&receiver_locked);
3464 return ret;
3465}
J-Alvesaa79c012021-07-09 14:29:45 +01003466
3467struct ffa_value api_ffa_notification_set(
3468 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3469 ffa_notifications_bitmap_t notifications, struct vcpu *current)
3470{
3471 struct ffa_value ret;
3472 struct vm_locked receiver_locked;
3473
3474 /*
3475 * Check if is per-vCPU or global, and extracting vCPU ID according
3476 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
3477 */
3478 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3479 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
3480
J-Alvesaa79c012021-07-09 14:29:45 +01003481 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
3482 receiver_vm_id)) {
3483 dlog_verbose("Invalid use of notifications set interface.\n");
3484 return ffa_error(FFA_INVALID_PARAMETERS);
3485 }
3486
3487 if (notifications == 0U) {
3488 dlog_verbose("No notifications have been specified.\n");
3489 return ffa_error(FFA_INVALID_PARAMETERS);
3490 }
3491
J-Alvesde7bd2f2021-09-09 19:54:35 +01003492 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
3493 flags, notifications, &ret)) {
3494 return ret;
3495 }
3496
J-Alvesaa79c012021-07-09 14:29:45 +01003497 /*
3498 * This check assumes receiver is the current VM, and has been enforced
3499 * by 'plat_ffa_is_notification_set_valid'.
3500 */
3501 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3502
3503 if (receiver_locked.vm == NULL) {
3504 dlog_verbose("Receiver ID is not valid.\n");
3505 return ffa_error(FFA_INVALID_PARAMETERS);
3506 }
3507
J-Alves09ff9d82021-11-02 11:55:20 +00003508 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003509 dlog_verbose("Receiver's notifications not enabled.\n");
3510 ret = ffa_error(FFA_DENIED);
3511 goto out;
3512 }
3513
3514 /*
3515 * If notifications are not bound to the sender, they wouldn't be
3516 * enabled either for the receiver.
3517 */
3518 if (!vm_notifications_validate_binding(
3519 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3520 sender_vm_id, notifications, is_per_vcpu)) {
3521 dlog_verbose("Notifications bindings not valid.\n");
3522 ret = ffa_error(FFA_DENIED);
3523 goto out;
3524 }
3525
3526 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3527 dlog_verbose("Invalid VCPU ID!\n");
3528 ret = ffa_error(FFA_INVALID_PARAMETERS);
3529 goto out;
3530 }
3531
J-Alves7461ef22021-10-18 17:21:33 +01003532 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00003533 vm_notifications_partition_set_pending(
3534 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), notifications,
3535 vcpu_id, is_per_vcpu);
3536
J-Alvesaa79c012021-07-09 14:29:45 +01003537 dlog_verbose("Set the notifications: %x.\n", notifications);
3538
J-Alves13394022021-06-30 13:48:49 +01003539 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3540 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3541 vcpu_index(current));
3542 plat_ffa_sri_trigger_not_delayed(current->cpu);
3543 } else {
3544 plat_ffa_sri_state_set(DELAYED);
3545 }
J-Alvesaa79c012021-07-09 14:29:45 +01003546
J-Alves13394022021-06-30 13:48:49 +01003547 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003548out:
3549 vm_unlock(&receiver_locked);
3550
3551 return ret;
3552}
3553
3554static struct ffa_value api_ffa_notification_get_success_return(
3555 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3556 ffa_notifications_bitmap_t from_framework)
3557{
3558 return (struct ffa_value){
3559 .func = FFA_SUCCESS_32,
3560 .arg1 = 0U,
3561 .arg2 = (uint32_t)from_sp,
3562 .arg3 = (uint32_t)(from_sp >> 32),
3563 .arg4 = (uint32_t)from_vm,
3564 .arg5 = (uint32_t)(from_vm >> 32),
3565 .arg6 = (uint32_t)from_framework,
3566 .arg7 = (uint32_t)(from_framework >> 32),
3567 };
3568}
3569
3570struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3571 ffa_vcpu_index_t vcpu_id,
3572 uint32_t flags, struct vcpu *current)
3573{
J-Alves663682a2022-03-25 13:56:51 +00003574 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01003575 ffa_notifications_bitmap_t sp_notifications = 0;
3576 ffa_notifications_bitmap_t vm_notifications = 0;
3577 struct vm_locked receiver_locked;
3578 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01003579 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
3580 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
3581 FFA_NOTIFICATION_FLAG_BITMAP_SP |
3582 FFA_NOTIFICATION_FLAG_BITMAP_VM);
3583
3584 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
3585 if ((flags & flags_mbz) != 0U) {
3586 dlog_verbose(
3587 "Invalid flags bit(s) set in notifications get. [31:4] "
3588 "MBZ(%x)\n",
3589 flags);
3590 return ffa_error(FFA_INVALID_PARAMETERS);
3591 }
J-Alvesaa79c012021-07-09 14:29:45 +01003592
3593 /*
J-Alvesfc95a302022-04-22 14:18:23 +01003594 * Following check should capture wrong uses of the interface,
3595 * depending on whether Hafnium is SPMC or hypervisor. On the
3596 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01003597 */
J-Alvesfc95a302022-04-22 14:18:23 +01003598 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
3599 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003600 dlog_verbose("Invalid use of notifications get interface.\n");
3601 return ffa_error(FFA_INVALID_PARAMETERS);
3602 }
3603
3604 /*
3605 * This check assumes receiver is the current VM, and has been enforced
3606 * by `plat_ffa_is_notifications_get_valid`.
3607 */
3608 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3609
3610 /*
3611 * `plat_ffa_is_notifications_get_valid` ensures following is never
3612 * true.
3613 */
3614 CHECK(receiver_locked.vm != NULL);
3615
3616 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3617 (receiver_locked.vm->vcpu_count != 1 &&
3618 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003619 dlog_verbose(
3620 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3621 vcpu_id, receiver_locked.vm->vcpu_count,
3622 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003623 ret = ffa_error(FFA_INVALID_PARAMETERS);
3624 goto out;
3625 }
3626
3627 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003628 if (!plat_ffa_notifications_get_from_sp(
3629 receiver_locked, vcpu_id, &sp_notifications,
3630 &ret)) {
3631 dlog_verbose("Failed to get notifications from sps.");
3632 goto out;
3633 }
J-Alvesaa79c012021-07-09 14:29:45 +01003634 }
3635
3636 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00003637 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01003638 receiver_locked, true, vcpu_id);
3639 }
3640
J-Alvesd605a092022-03-28 14:20:48 +01003641 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
3642 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
3643 if (!plat_ffa_notifications_get_framework_notifications(
3644 receiver_locked, &framework_notifications, flags,
3645 vcpu_id, &ret)) {
3646 dlog_verbose(
3647 "Failed to get notifications from "
3648 "framework.\n");
3649 goto out;
3650 }
3651 }
3652
J-Alves663682a2022-03-25 13:56:51 +00003653 ret = api_ffa_notification_get_success_return(
3654 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01003655
J-Alvesfe23ebe2021-10-13 16:07:07 +01003656 /*
3657 * If there are no more pending notifications, change `sri_state` to
3658 * handled.
3659 */
3660 if (vm_is_notifications_pending_count_zero()) {
3661 plat_ffa_sri_state_set(HANDLED);
3662 }
3663
J-Alves6e2abc62021-12-02 14:58:56 +00003664 if (!receiver_locked.vm->el0_partition &&
3665 !vm_are_global_notifications_pending(receiver_locked)) {
3666 vm_notifications_set_npi_injected(receiver_locked, false);
3667 }
3668
J-Alvesaa79c012021-07-09 14:29:45 +01003669out:
3670 vm_unlock(&receiver_locked);
3671
3672 return ret;
3673}
J-Alvesc8e8a222021-06-08 17:33:52 +01003674
3675/**
3676 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3677 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3678 */
3679static struct ffa_value api_ffa_notification_info_get_success_return(
3680 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003681 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003682{
3683 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3684
3685 /*
3686 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3687 * hold the list of ids.
3688 */
3689 memcpy_s(&ret.arg3,
3690 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3691 sizeof(ids[0]) * ids_count);
3692
3693 /*
3694 * According to the spec x2 should have:
3695 * - Bit flagging if there are more notifications pending;
3696 * - The total number of elements (i.e. total list size);
3697 * - The number of VCPU IDs within each VM specific list.
3698 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003699 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3700 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3701 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003702
3703 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3704 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3705
3706 for (unsigned int i = 0; i < lists_count; i++) {
3707 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3708 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3709 }
3710
3711 return ret;
3712}
3713
3714struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3715{
3716 /*
3717 * Following set of variables should be populated with the return info.
3718 * At a successfull handling of this interface, they should be used
3719 * to populate the 'ret' structure in accordance to the table 17.29
3720 * of the FF-A v1.1 Beta0 specification.
3721 */
3722 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3723 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3724 uint32_t lists_count = 0;
3725 uint32_t ids_count = 0;
3726 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003727 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003728
3729 /*
3730 * This interface can only be called at NS virtual/physical FF-A
3731 * instance by the endpoint implementing the primary scheduler and the
3732 * Hypervisor/OS kernel.
3733 * In the SPM, following check passes if call has been forwarded from
3734 * the hypervisor.
3735 */
3736 if (current->vm->id != HF_PRIMARY_VM_ID) {
3737 dlog_verbose(
3738 "Only the receiver's scheduler can use this "
3739 "interface\n");
3740 return ffa_error(FFA_NOT_SUPPORTED);
3741 }
3742
J-Alvesca058c22021-09-10 14:02:07 +01003743 /*
3744 * Forward call to the other world, and fill the arrays used to assemble
3745 * return.
3746 */
3747 plat_ffa_notification_info_get_forward(
3748 ids, &ids_count, lists_sizes, &lists_count,
3749 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3750
3751 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3752
J-Alvesc8e8a222021-06-08 17:33:52 +01003753 /* Get notifications' info from this world */
3754 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3755 ++index) {
3756 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3757
3758 list_is_full = vm_notifications_info_get(
3759 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3760 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3761
3762 vm_unlock(&vm_locked);
3763 }
3764
3765 if (!list_is_full) {
3766 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003767 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003768 ids, &ids_count, lists_sizes, &lists_count,
3769 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3770 }
3771
3772 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003773 dlog_verbose(
3774 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003775 result = ffa_error(FFA_NO_DATA);
3776 } else {
3777 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003778 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003779 }
3780
J-Alvesfe23ebe2021-10-13 16:07:07 +01003781 plat_ffa_sri_state_set(HANDLED);
3782
J-Alves13394022021-06-30 13:48:49 +01003783 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003784}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003785
3786struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3787{
3788 struct vm_locked vm_locked;
3789 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3790 bool mode_ret = false;
3791 uint32_t mode = 0;
3792
3793 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3794 return ffa_error(FFA_NOT_SUPPORTED);
3795 }
3796
3797 if (!(current->vm->el0_partition)) {
3798 return ffa_error(FFA_DENIED);
3799 }
3800
3801 vm_locked = vm_lock(current->vm);
3802
3803 /*
3804 * mm_get_mode is used to check if the given base_addr page is already
3805 * mapped. If the page is unmapped, return error. If the page is mapped
3806 * appropriate attributes are returned to the caller. Note that
3807 * mm_get_mode returns true if the address is in the valid VA range as
3808 * supported by the architecture and MMU configurations, as opposed to
3809 * whether a page is mapped or not. For a page to be known as mapped,
3810 * the API must return true AND the returned mode must not have
3811 * MM_MODE_INVALID set.
3812 */
3813 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3814 va_add(base_addr, PAGE_SIZE), &mode);
3815 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3816 ret = ffa_error(FFA_INVALID_PARAMETERS);
3817 goto out;
3818 }
3819
3820 /* No memory should be marked RWX */
3821 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3822 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3823
3824 /*
3825 * S-EL0 partitions are expected to have all their pages marked as
3826 * non-global.
3827 */
3828 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3829 (MM_MODE_NG | MM_MODE_USER));
3830
3831 if (mode & MM_MODE_W) {
3832 /* No memory should be writeable but not readable. */
3833 CHECK(mode & MM_MODE_R);
3834 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3835 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3836 } else if (mode & MM_MODE_R) {
3837 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3838 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3839 if (!(mode & MM_MODE_X)) {
3840 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3841 }
3842 }
3843out:
3844 vm_unlock(&vm_locked);
3845 return ret;
3846}
3847
3848struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3849 uint32_t mem_perm, struct vcpu *current)
3850{
3851 struct vm_locked vm_locked;
3852 struct ffa_value ret;
3853 bool mode_ret = false;
3854 uint32_t original_mode;
3855 uint32_t new_mode;
3856 struct mpool local_page_pool;
3857
3858 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3859 return ffa_error(FFA_NOT_SUPPORTED);
3860 }
3861
3862 if (!(current->vm->el0_partition)) {
3863 return ffa_error(FFA_DENIED);
3864 }
3865
3866 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3867 return ffa_error(FFA_INVALID_PARAMETERS);
3868 }
3869
3870 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3871 (mem_perm != FFA_MEM_PERM_RX)) {
3872 return ffa_error(FFA_INVALID_PARAMETERS);
3873 }
3874
3875 /*
3876 * Create a local pool so any freed memory can't be used by another
3877 * thread. This is to ensure the original mapping can be restored if any
3878 * stage of the process fails.
3879 */
3880 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3881
3882 vm_locked = vm_lock(current->vm);
3883
3884 /*
3885 * All regions accessible by the partition are mapped during boot. If we
3886 * cannot get a successful translation for the page range, the request
3887 * to change permissions is rejected.
3888 * mm_get_mode is used to check if the given address range is already
3889 * mapped. If the range is unmapped, return error. If the range is
3890 * mapped appropriate attributes are returned to the caller. Note that
3891 * mm_get_mode returns true if the address is in the valid VA range as
3892 * supported by the architecture and MMU configurations, as opposed to
3893 * whether a page is mapped or not. For a page to be known as mapped,
3894 * the API must return true AND the returned mode must not have
3895 * MM_MODE_INVALID set.
3896 */
3897
3898 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3899 va_add(base_addr, page_count * PAGE_SIZE),
3900 &original_mode);
3901 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3902 ret = ffa_error(FFA_INVALID_PARAMETERS);
3903 goto out;
3904 }
3905
3906 /* Device memory cannot be marked as executable */
3907 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3908 ret = ffa_error(FFA_INVALID_PARAMETERS);
3909 goto out;
3910 }
3911
3912 new_mode = MM_MODE_USER | MM_MODE_NG;
3913
3914 if (mem_perm == FFA_MEM_PERM_RW) {
3915 new_mode |= MM_MODE_R | MM_MODE_W;
3916 } else if (mem_perm == FFA_MEM_PERM_RX) {
3917 new_mode |= MM_MODE_R | MM_MODE_X;
3918 } else if (mem_perm == FFA_MEM_PERM_RO) {
3919 new_mode |= MM_MODE_R;
3920 }
3921
3922 /*
3923 * Safe to re-map memory, since we know the requested permissions are
3924 * valid, and the memory requested to be re-mapped is also valid.
3925 */
3926 if (!mm_identity_prepare(
3927 &vm_locked.vm->ptable, pa_from_va(base_addr),
3928 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3929 new_mode, &local_page_pool)) {
3930 /*
3931 * Defrag the table into the local page pool.
3932 * mm_identity_prepare could have allocated or freed pages to
3933 * split blocks or tables etc.
3934 */
3935 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3936
3937 /*
3938 * Guaranteed to succeed mapping with old mode since the mapping
3939 * with old mode already existed and we have a local page pool
3940 * that should have sufficient memory to go back to the original
3941 * state.
3942 */
3943 CHECK(mm_identity_prepare(
3944 &vm_locked.vm->ptable, pa_from_va(base_addr),
3945 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3946 original_mode, &local_page_pool));
3947 mm_identity_commit(
3948 &vm_locked.vm->ptable, pa_from_va(base_addr),
3949 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3950 original_mode, &local_page_pool);
3951
3952 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3953 ret = ffa_error(FFA_NO_MEMORY);
3954 goto out;
3955 }
3956
3957 mm_identity_commit(
3958 &vm_locked.vm->ptable, pa_from_va(base_addr),
3959 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3960 &local_page_pool);
3961
3962 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3963
3964out:
3965 mpool_fini(&local_page_pool);
3966 vm_unlock(&vm_locked);
3967
3968 return ret;
3969}
Maksims Svecovs71b76702022-05-20 15:32:58 +01003970
3971/**
3972 * Helper function for FFA_CONSOLE_LOG ABI.
3973 * Writes number of characters to a given VM buffer.
3974 */
3975static rsize_t arg_to_char_helper(struct vm_locked from_locked,
3976 const uint64_t src, rsize_t src_size,
3977 rsize_t to_write)
3978{
3979 bool flush = false;
3980 char c;
3981 rsize_t size = src_size < to_write ? src_size : to_write;
3982 rsize_t written = 0;
3983
3984 if (size == 0) {
3985 return 0;
3986 }
3987
3988 while (written < size) {
3989 c = ((char *)&src)[written++];
3990 if (c == '\n' || c == '\0') {
3991 flush = true;
3992 } else {
3993 from_locked.vm->log_buffer
3994 [from_locked.vm->log_buffer_length++] = c;
3995 flush = (from_locked.vm->log_buffer_length ==
3996 LOG_BUFFER_SIZE);
3997 }
3998
3999 if (flush) {
4000 dlog_flush_vm_buffer(from_locked.vm->id,
4001 from_locked.vm->log_buffer,
4002 from_locked.vm->log_buffer_length);
4003 from_locked.vm->log_buffer_length = 0;
4004 }
4005 }
4006
4007 return written;
4008}
4009
4010/**
4011 * Implements FFA_CONSOLE_LOG buffered logging.
4012 */
4013struct ffa_value api_ffa_console_log(const struct ffa_value args,
4014 struct vcpu *current)
4015{
4016 struct vm *vm = current->vm;
4017 struct vm_locked vm_locked;
4018 size_t chars_in_param = args.func == FFA_CONSOLE_LOG_32
4019 ? sizeof(uint32_t)
4020 : sizeof(uint64_t);
4021 size_t total_to_write = args.arg1;
4022
4023 if (total_to_write == 0 || total_to_write > chars_in_param * 6) {
4024 return ffa_error(FFA_INVALID_PARAMETERS);
4025 }
4026
4027 vm_locked = vm_lock(vm);
4028
4029 total_to_write -= arg_to_char_helper(vm_locked, args.arg2,
4030 chars_in_param, total_to_write);
4031 total_to_write -= arg_to_char_helper(vm_locked, args.arg3,
4032 chars_in_param, total_to_write);
4033 total_to_write -= arg_to_char_helper(vm_locked, args.arg4,
4034 chars_in_param, total_to_write);
4035 total_to_write -= arg_to_char_helper(vm_locked, args.arg5,
4036 chars_in_param, total_to_write);
4037 total_to_write -= arg_to_char_helper(vm_locked, args.arg6,
4038 chars_in_param, total_to_write);
4039 arg_to_char_helper(vm_locked, args.arg7, chars_in_param,
4040 total_to_write);
4041
4042 vm_unlock(&vm_locked);
4043
4044 return (struct ffa_value){.func = FFA_SUCCESS_32};
4045}