blob: deb2249011708b1c6ac1a042a9b5ba194677ae66 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
J-Alves13318e32021-02-22 17:21:00 +00002 * Copyright 2021 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"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010033
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000034static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010035 "Partition information descriptor size doesn't match the one in "
36 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000037static_assert(sizeof(struct ffa_partition_info) == 24,
38 "Partition information descriptor size doesn't match the one in "
39 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Fuad Tabbae4efcc32020-07-16 15:37:27 +010040
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000041/*
42 * To eliminate the risk of deadlocks, we define a partial order for the
43 * acquisition of locks held concurrently by the same physical CPU. Our current
44 * ordering requirements are as follows:
45 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010046 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000047 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010048 * Locks of the same kind require the lock of lowest address to be locked first,
49 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000050 */
51
Andrew Scullaa039b32018-10-04 15:02:26 +010052static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010053 "Currently, a page is mapped for the send and receive buffers so "
54 "the maximum request is the size of a page.");
55
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000056static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
57 "The page pool entry size must be at least as big as the mailbox "
58 "size, so that memory region descriptors can be copied from the "
59 "mailbox for memory sharing.");
60
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000061static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000062
63/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000064 * Initialises the API page pool by taking ownership of the contents of the
65 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000066 */
67void api_init(struct mpool *ppool)
68{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000069 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000070}
71
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010072/**
J-Alvesad6a0432021-04-09 16:06:21 +010073 * Get target VM vCPU:
74 * If VM is UP then return first vCPU.
75 * If VM is MP then return vCPU whose index matches current CPU index.
76 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050077struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +010078{
79 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
80 struct vcpu *vcpu = NULL;
81
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050082 CHECK((vm != NULL) && (current != NULL));
83
J-Alvesad6a0432021-04-09 16:06:21 +010084 if (vm->vcpu_count == 1) {
85 vcpu = vm_get_vcpu(vm, 0);
86 } else if (current_cpu_index < vm->vcpu_count) {
87 vcpu = vm_get_vcpu(vm, current_cpu_index);
88 }
89
90 return vcpu;
91}
92
93/**
J-Alvesfe7f7372020-11-09 11:32:12 +000094 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
95 * is given as argument of the function.
96 *
97 * Called to change the context between SPs for direct messaging (when Hafnium
98 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
99 *
100 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100101 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000102 * - MP with pinned Execution Contexts.
103 */
104static struct vcpu *api_switch_to_vm(struct vcpu *current,
105 struct ffa_value to_ret,
106 enum vcpu_state vcpu_state,
107 ffa_vm_id_t to_id)
108{
109 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100110 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000111
112 CHECK(next != NULL);
113
114 /* Set the return value for the target VM. */
115 arch_regs_set_retval(&next->regs, to_ret);
116
117 /* Set the current vCPU state. */
118 sl_lock(&current->lock);
119 current->state = vcpu_state;
120 sl_unlock(&current->lock);
121
122 return next;
123}
124
125/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000126 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100127 *
128 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100129 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100130 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500131struct vcpu *api_switch_to_primary(struct vcpu *current,
132 struct ffa_value primary_ret,
133 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100134{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000135 /*
136 * If the secondary is blocked but has a timer running, sleep until the
137 * timer fires rather than indefinitely.
138 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100139 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100140 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
141 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100142 if (arch_timer_enabled_current()) {
143 uint64_t remaining_ns =
144 arch_timer_remaining_ns_current();
145
146 if (remaining_ns == 0) {
147 /*
148 * Timer is pending, so the current vCPU should
149 * be run again right away.
150 */
Andrew Walbran7e824602020-10-22 16:51:40 +0100151 primary_ret = (struct ffa_value){
152 .func = FFA_INTERRUPT_32};
153
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100154 } else {
155 primary_ret.arg2 = remaining_ns;
156 }
157 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100158 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100159 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000160 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100161 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000162
163 default:
164 /* Do nothing. */
165 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000166 }
167
J-Alvesfe7f7372020-11-09 11:32:12 +0000168 return api_switch_to_vm(current, primary_ret, secondary_state,
169 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100170}
171
172/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200173 * Choose next vCPU to run to be the counterpart vCPU in the other
174 * world (run the normal world if currently running in the secure
175 * world). Set current vCPU state to the given vcpu_state parameter.
176 * Set FF-A return values to the target vCPU in the other world.
177 *
178 * Called in context of a direct message response from a secure
179 * partition to a VM.
180 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100181struct vcpu *api_switch_to_other_world(struct vcpu *current,
182 struct ffa_value other_world_ret,
183 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200184{
J-Alvesfe7f7372020-11-09 11:32:12 +0000185 return api_switch_to_vm(current, other_world_ret, vcpu_state,
186 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200187}
188
189/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100190 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
191 * registers the `from` VM to be notified when it becomes available.
192 */
193static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
194{
195 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
196 to.vm->mailbox.recv == NULL) {
197 /*
198 * Fail if the receiver isn't currently ready to receive data,
199 * setting up for notification if requested.
200 */
201 if (notify) {
202 struct wait_entry *entry =
203 vm_get_wait_entry(from, to.vm->id);
204
205 /* Append waiter only if it's not there yet. */
206 if (list_empty(&entry->wait_links)) {
207 list_append(&to.vm->mailbox.waiter_list,
208 &entry->wait_links);
209 }
210 }
211
212 return true;
213 }
214
215 return false;
216}
217
218/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000219 * Returns true if the given vCPU is executing in context of an
220 * FFA_MSG_SEND_DIRECT_REQ invocation.
221 */
222static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
223{
224 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
225}
226
227/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100228 * Returns true if the VM owning the given vCPU is supporting managed exit and
229 * the vCPU is currently processing a managed exit.
230 */
231static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
232{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100233 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100234 vcpu_locked.vcpu->processing_managed_exit);
235}
236
237/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000238 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000239 */
240struct vcpu *api_preempt(struct vcpu *current)
241{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100242 struct ffa_value ret = {
243 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200244 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000245 };
246
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500247 return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED);
Andrew Scull33fecd32019-01-08 14:48:27 +0000248}
249
250/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000251 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000252 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100253 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100254struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100255{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100256 struct ffa_value ret = {
257 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
258 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100259 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000260
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000261 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100262 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100263}
264
265/**
Andrew Walbran33645652019-04-15 12:29:31 +0100266 * Puts the current vCPU in off mode, and returns to the primary VM.
267 */
268struct vcpu *api_vcpu_off(struct vcpu *current)
269{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100270 struct ffa_value ret = {
271 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
272 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100273 };
274
275 /*
276 * Disable the timer, so the scheduler doesn't get told to call back
277 * based on it.
278 */
279 arch_timer_disable_current();
280
281 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
282}
283
284/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500285 * The current vCPU is blocked on some resource and needs to relinquish
286 * control back to the execution context of the endpoint that originally
287 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000288 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000289struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000290{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000291 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
292 struct vcpu_locked current_locked;
293 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000294
295 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000296 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000297 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000298 }
299
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000300 current_locked = vcpu_lock(current);
301 is_direct_request_ongoing =
302 is_ffa_direct_msg_request_ongoing(current_locked);
303 vcpu_unlock(&current_locked);
304
305 if (is_direct_request_ongoing) {
306 return ffa_error(FFA_DENIED);
307 }
308
309 *next = api_switch_to_primary(
310 current,
311 (struct ffa_value){.func = FFA_YIELD_32,
312 .arg1 = ffa_vm_vcpu(current->vm->id,
313 vcpu_index(current))},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500314 VCPU_STATE_BLOCKED);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000315
316 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000317}
318
319/**
Andrew Walbran33645652019-04-15 12:29:31 +0100320 * Switches to the primary so that it can switch to the target, or kick it if it
321 * is already running on a different physical CPU.
322 */
323struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
324{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100325 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100326 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
328 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100329 };
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500330 return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED);
Andrew Walbran33645652019-04-15 12:29:31 +0100331}
332
333/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000334 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000335 */
336struct vcpu *api_abort(struct vcpu *current)
337{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000339
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100340 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000341 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000342
343 if (current->vm->id == HF_PRIMARY_VM_ID) {
344 /* TODO: what to do when the primary aborts? */
345 for (;;) {
346 /* Do nothing. */
347 }
348 }
349
350 atomic_store_explicit(&current->vm->aborting, true,
351 memory_order_relaxed);
352
353 /* TODO: free resources once all vCPUs abort. */
354
Andrew Sculld6ee1102019-04-05 22:12:42 +0100355 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000356}
357
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000358/*
359 * Format the partition info descriptors according to the version supported
360 * by the endpoint and return the size of the array created.
361 */
362static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000363 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000364 uint32_t vm_count)
365{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000366 struct vm *vm = vm_locked.vm;
367 uint32_t version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100368 uint32_t partition_info_size;
369 uint32_t buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000370
Daniel Boulby191b5f02022-02-17 17:26:14 +0000371 if (msg_receiver_busy(vm_locked, NULL, false)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000372 /*
373 * Can't retrieve memory information if the mailbox is not
374 * available.
375 */
376 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000377 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000378 }
379
Daniel Boulby191b5f02022-02-17 17:26:14 +0000380 if (version == MAKE_FFA_VERSION(1, 0)) {
381 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
382
Daniel Boulby835e1592022-05-30 17:38:51 +0100383 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
384 buffer_size = partition_info_size * vm_count;
385 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000386 dlog_error(
387 "Partition information does not fit in the "
388 "VM's RX "
389 "buffer.\n");
390 return ffa_error(FFA_NO_MEMORY);
391 }
392
393 for (uint32_t i = 0; i < vm_count; i++) {
394 /*
395 * Populate the VM's RX buffer with the partition
396 * information.
397 */
398 recv_mailbox[i].vm_id = partitions[i].vm_id;
399 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
400 recv_mailbox[i].properties = partitions[i].properties;
401 }
402
403 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100404 partition_info_size = sizeof(struct ffa_partition_info);
405 buffer_size = partition_info_size * vm_count;
406 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000407 dlog_error(
408 "Partition information does not fit in the "
409 "VM's RX "
410 "buffer.\n");
411 return ffa_error(FFA_NO_MEMORY);
412 }
413
414 /* Populate the VM's RX buffer with the partition information.
415 */
Daniel Boulby835e1592022-05-30 17:38:51 +0100416 memcpy_s(vm->mailbox.recv, HF_MAILBOX_SIZE, partitions,
417 buffer_size);
Daniel Boulby191b5f02022-02-17 17:26:14 +0000418 }
419
Daniel Boulby835e1592022-05-30 17:38:51 +0100420 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000421
422 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000423 vm->mailbox.recv_sender = HF_VM_ID_BASE;
424 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
425 vm->mailbox.state = MAILBOX_STATE_READ;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000426
Daniel Boulby835e1592022-05-30 17:38:51 +0100427 /*
428 * Return the count of partition information descriptors in w2
429 * and the size of the descriptors in w3.
430 */
431 return (struct ffa_value){.func = FFA_SUCCESS_32,
432 .arg2 = vm_count,
433 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000434}
435
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100436struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000437 const struct ffa_uuid *uuid,
438 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100439{
440 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100441 ffa_vm_count_t vm_count = 0;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000442 bool count_flag = (flags && FFA_PARTITION_COUNT_FLAG_MASK) ==
443 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100444 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100445 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000446 struct vm_locked vm_locked;
447 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100448
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000449 /* Bits 31:1 Must Be Zero */
450 if ((flags & ~FFA_PARTITION_COUNT_FLAG) != 0) {
451 return ffa_error(FFA_INVALID_PARAMETERS);
452 }
453
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100454 /*
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000455 * No need to count if we are returning the number of paritions as we
456 * already know this.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100457 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000458 if (uuid_is_null && count_flag) {
459 vm_count = vm_get_count();
460 } else {
461 /*
462 * Iterate through the VMs to find the ones with a matching
463 * UUID. A Null UUID retrieves information for all VMs.
464 */
465 for (uint16_t index = 0; index < vm_get_count(); ++index) {
466 struct vm *vm = vm_find_index(index);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100467
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000468 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
469 uint16_t array_index = vm_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100470
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000471 ++vm_count;
472 if (count_flag) {
473 continue;
474 }
475
476 partitions[array_index].vm_id = vm->id;
477 partitions[array_index].vcpu_count =
478 vm->vcpu_count;
479 partitions[array_index].properties =
480 plat_ffa_partition_properties(
481 current_vm->id, vm);
482 partitions[array_index].properties |=
483 vm_are_notifications_enabled(vm)
484 ? FFA_PARTITION_NOTIFICATION
485 : 0;
Daniel Boulbyce541842022-05-31 15:54:31 +0100486 if (uuid_is_null) {
487 partitions[array_index].uuid = vm->uuid;
488 }
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000489 }
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100490 }
491 }
492
Olivier Depreze562e542020-06-11 17:31:54 +0200493 /* If UUID is Null vm_count must not be zero at this stage. */
494 CHECK(!uuid_is_null || vm_count != 0);
495
496 /*
497 * When running the Hypervisor:
498 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
499 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000500 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200501 * a secure partition and the query is forwarded to the SPMC.
502 * When running the SPMC:
503 * - If UUID is non-Null and vm_count is zero it means there is no such
504 * partition identified in the system.
505 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000506 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200507
508 /*
509 * Unrecognized UUID: does not match any of the VMs (or SPs)
510 * and is not Null.
511 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100512 if (vm_count == 0) {
513 return ffa_error(FFA_INVALID_PARAMETERS);
514 }
515
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000516 /*
517 * If the count flag is set we don't need to return the partition info
518 * descriptors.
519 */
520 if (count_flag) {
521 return (struct ffa_value){.func = FFA_SUCCESS_32,
522 .arg2 = vm_count};
523 }
524
Daniel Boulby191b5f02022-02-17 17:26:14 +0000525 vm_locked = vm_lock(current_vm);
526 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
527 vm_count);
528 vm_unlock(&vm_locked);
529 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100530}
531
Andrew Scull9726c252019-01-23 13:44:19 +0000532/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000533 * Returns the ID of the VM.
534 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100535struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000536{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100537 return (struct ffa_value){.func = FFA_SUCCESS_32,
538 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000539}
540
541/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200542 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
543 * FF-A instances.
544 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000545 */
546struct ffa_value api_ffa_spm_id_get(void)
547{
J-Alves3829fc02021-03-18 12:49:18 +0000548#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
549 /*
550 * Return the SPMC ID that was fetched during FF-A
551 * initialization.
552 */
553 return (struct ffa_value){.func = FFA_SUCCESS_32,
554 .arg2 = arch_ffa_spmc_id_get()};
555#else
556 return ffa_error(FFA_NOT_SUPPORTED);
557#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000558}
559
560/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000561 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000562 * function to indicate that register state for the given vCPU has been saved
563 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000564 */
565void api_regs_state_saved(struct vcpu *vcpu)
566{
567 sl_lock(&vcpu->lock);
568 vcpu->regs_available = true;
569 sl_unlock(&vcpu->lock);
570}
571
572/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000573 * Retrieves the next waiter and removes it from the wait list if the VM's
574 * mailbox is in a writable state.
575 */
576static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
577{
578 struct wait_entry *entry;
579 struct vm *vm = locked_vm.vm;
580
Andrew Sculld6ee1102019-04-05 22:12:42 +0100581 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000582 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
583 /* The mailbox is not writable or there are no waiters. */
584 return NULL;
585 }
586
587 /* Remove waiter from the wait list. */
588 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
589 wait_links);
590 list_remove(&entry->wait_links);
591 return entry;
592}
593
594/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000595 * Assuming that the arguments have already been checked by the caller, injects
596 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
597 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
598 * is next run, which is up to the scheduler.
599 *
600 * Returns:
601 * - 0 on success if no further action is needed.
602 * - 1 if it was called by the primary VM and the primary VM now needs to wake
603 * up or kick the target vCPU.
604 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100605int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
606 uint32_t intid, struct vcpu *current,
607 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000608{
Manish Pandey35e452f2021-02-18 21:36:34 +0000609 struct vcpu *target_vcpu = target_locked.vcpu;
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100610 uint32_t intid_index = INTID_INDEX(intid);
611 uint32_t intid_mask = INTID_MASK(1U, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000612 int64_t ret = 0;
613
Andrew Walbran508e63c2018-12-20 17:02:37 +0000614 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000615 * We only need to change state and (maybe) trigger a virtual interrupt
616 * if it is enabled and was not previously pending. Otherwise we can
617 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000618 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000619 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
620 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000621 intid_mask)) {
622 goto out;
623 }
624
625 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000626 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100627 intid_mask) == INTID_MASK(INTERRUPT_TYPE_IRQ, intid)) {
Manish Pandey35e452f2021-02-18 21:36:34 +0000628 vcpu_irq_count_increment(target_locked);
629 } else {
630 vcpu_fiq_count_increment(target_locked);
631 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000632
633 /*
634 * Only need to update state if there was not already an
635 * interrupt enabled and pending.
636 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000637 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000638 goto out;
639 }
640
Andrew Walbran508e63c2018-12-20 17:02:37 +0000641 if (current->vm->id == HF_PRIMARY_VM_ID) {
642 /*
643 * If the call came from the primary VM, let it know that it
644 * should run or kick the target vCPU.
645 */
646 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000647 } else if (current != target_vcpu && next != NULL) {
648 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000649 }
650
651out:
652 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000653 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000654
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200655 return ret;
656}
657
658/* Wrapper to internal_interrupt_inject with locking of target vCPU */
659static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
660 uint32_t intid, struct vcpu *current,
661 struct vcpu **next)
662{
663 int64_t ret;
664 struct vcpu_locked target_locked;
665
666 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100667 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200668 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000669
670 return ret;
671}
672
673/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100674 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
675 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100676 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100677static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100678{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000679 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100680 case FFA_MSG_SEND_32:
681 return (struct ffa_value){
682 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000683 .arg1 = (receiver->mailbox.recv_sender << 16) |
684 receiver->id,
685 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000686 default:
687 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000688 dlog_error("Tried to return an invalid message function %#x\n",
689 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100690 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000691 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100692}
693
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600694struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
695 struct ffa_value *args)
696{
697 struct ffa_value ret;
698
699 if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U ||
700 args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
701 args->arg7 != 0U) {
702 return ffa_error(FFA_INVALID_PARAMETERS);
703 }
704
705 if (plat_ffa_msg_wait_prepare(current, next, &ret)) {
706 return ret;
707 }
708
709 return api_ffa_msg_recv(true, current, next);
710}
711
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100712/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000713 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000714 * value needs to be forced onto the vCPU.
715 */
J-Alves6e2abc62021-12-02 14:58:56 +0000716static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100717 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000718{
Max Shvetsov40108e72020-08-27 12:39:50 +0100719 struct vcpu_locked vcpu_locked;
720 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000721 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500722 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
J-Alves6e2abc62021-12-02 14:58:56 +0000723 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000724
Andrew Scullb06d1752019-02-04 10:15:48 +0000725 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000726 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000727 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100728 * The VM lock is not needed in the common case so it must only be taken
729 * when it is going to be needed. This ensures there are no inter-vCPU
730 * dependencies in the common run case meaning the sensitive context
731 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000732 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100733 vcpu_locked = vcpu_lock(vcpu);
734
735#if SECURE_WORLD == 1
J-Alves7ac49052022-02-08 17:20:53 +0000736 bool is_vcpu_reset_and_start = vcpu_secondary_reset_and_start(
737 vcpu_locked, vcpu->vm->secondary_ep, 0);
738 if (is_vcpu_reset_and_start) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100739 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
740 __func__, vcpu->vm->id, current->cpu->id);
741 }
742
743#endif
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000744 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000745 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
746 (!vcpu->vm->el0_partition &&
747 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
748 vcpu->state == VCPU_STATE_BLOCKED ||
749 vcpu->state == VCPU_STATE_PREEMPTED));
750
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000751 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100752 vcpu_unlock(&vcpu_locked);
753 vm_locked = vm_lock(vcpu->vm);
754 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000755 }
756
757 /*
758 * If the vCPU is already running somewhere then we can't run it here
759 * simultaneously. While it is actually running then the state should be
760 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
761 * stops running but while Hafnium is in the process of switching back
762 * to the primary there will be a brief period while the state has been
763 * updated but `regs_available` is still false (until
764 * `api_regs_state_saved` is called). We can't start running it again
765 * until this has finished, so count this state as still running for the
766 * purposes of this check.
767 */
768 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
769 /*
770 * vCPU is running on another pCPU.
771 *
772 * It's okay not to return the sleep duration here because the
773 * other physical CPU that is currently running this vCPU will
774 * return the sleep duration if needed.
775 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100776 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000777 ret = false;
778 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000779 }
Andrew Scull9726c252019-01-23 13:44:19 +0000780
781 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100782 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100783 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000784 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100785 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000786 }
787 ret = false;
788 goto out;
789 }
790
Andrew Walbran508e63c2018-12-20 17:02:37 +0000791 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100792 case VCPU_STATE_RUNNING:
793 case VCPU_STATE_OFF:
794 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000795 ret = false;
796 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000797
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500798 case VCPU_STATE_WAITING:
799 /*
800 * An initial FFA_RUN is necessary for secondary VM/SP to reach
801 * the message wait loop.
802 */
803 if (!vcpu->is_bootstrapped) {
804 vcpu->is_bootstrapped = true;
805 break;
806 }
807
J-Alves6e2abc62021-12-02 14:58:56 +0000808 assert(need_vm_lock == true);
809 if (!vm_locked.vm->el0_partition &&
810 plat_ffa_inject_notification_pending_interrupt(
811 vcpu_locked, current, vm_locked)) {
812 break;
813 }
814
Andrew Scullb06d1752019-02-04 10:15:48 +0000815 /*
816 * A pending message allows the vCPU to run so the message can
817 * be delivered directly.
818 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100819 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100820 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100821 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100822 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000823 break;
824 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500825
826 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
827 break;
828 }
829
830 if (arch_timer_enabled(&vcpu->regs)) {
831 timer_remaining_ns =
832 arch_timer_remaining_ns(&vcpu->regs);
833 if (timer_remaining_ns == 0) {
834 break;
835 }
836 } else {
837 dlog_verbose("Timer disabled\n");
838 }
839 run_ret->func = FFA_MSG_WAIT_32;
840 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
841 run_ret->arg2 = timer_remaining_ns;
842 ret = false;
843 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100844 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +0000845 if (need_vm_lock &&
846 plat_ffa_inject_notification_pending_interrupt(
847 vcpu_locked, current, vm_locked)) {
848 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
849 break;
850 }
851
Andrew Scullb06d1752019-02-04 10:15:48 +0000852 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000853 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000854 break;
855 }
856
Andrew Walbran508e63c2018-12-20 17:02:37 +0000857 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100858 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000859 arch_timer_remaining_ns(&vcpu->regs);
860
861 /*
862 * The timer expired so allow the interrupt to be
863 * delivered.
864 */
865 if (timer_remaining_ns == 0) {
866 break;
867 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100868 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000869
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100870 /*
871 * The vCPU is not ready to run, return the appropriate code to
872 * the primary which called vcpu_run.
873 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500874 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100875 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
876 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000877
878 ret = false;
879 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000880
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500881 case VCPU_STATE_BLOCKED:
882 /* A blocked vCPU is run unconditionally. Fall through. */
883 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +0000884 /* Check NPI is to be injected here. */
885 if (need_vm_lock) {
886 plat_ffa_inject_notification_pending_interrupt(
887 vcpu_locked, current, vm_locked);
888 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000889 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500890 default:
891 /*
892 * Execution not expected to reach here. Deny the request
893 * gracefully.
894 */
895 *run_ret = ffa_error(FFA_DENIED);
896 ret = false;
897 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000898 }
899
Andrew Scullb06d1752019-02-04 10:15:48 +0000900 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000901 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100902 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000903
J-Alves7ac49052022-02-08 17:20:53 +0000904#if SECURE_WORLD == 1
905 /* Set the designated GP register with the vCPU ID. */
906 if (is_vcpu_reset_and_start) {
907 vcpu_set_phys_core_idx(vcpu_locked.vcpu);
908 }
909#endif
910
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000911 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000912 * Mark the registers as unavailable now that we're about to reflect
913 * them onto the real registers. This will also prevent another physical
914 * CPU from trying to read these registers.
915 */
916 vcpu->regs_available = false;
917
918 ret = true;
919
920out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100921 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000922 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100923 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000924 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000925 return ret;
926}
927
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100928struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500929 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100930{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100931 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100932 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100933 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100934
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -0800935 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500936 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100937 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100938
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700939 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
940 return ret;
941 }
942
Andrew Scull19503262018-09-20 14:48:39 +0100943 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100944 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100945 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100946 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100947 }
948
Fuad Tabbaed294af2019-12-20 10:43:01 +0000949 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100950 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100951 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100952 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100953
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000954 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100955 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000956 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000957 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100958 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000959
Andrew Walbran508e63c2018-12-20 17:02:37 +0000960 /*
961 * Inject timer interrupt if timer has expired. It's safe to access
962 * vcpu->regs here because api_vcpu_prepare_run already made sure that
963 * regs_available was true (and then set it to false) before returning
964 * true.
965 */
966 if (arch_timer_pending(&vcpu->regs)) {
967 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100968 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
969 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000970
971 /*
972 * Set the mask bit so the hardware interrupt doesn't fire
973 * again. Ideally we wouldn't do this because it affects what
974 * the secondary vCPU sees, but if we don't then we end up with
975 * a loop of the interrupt firing each time we try to return to
976 * the secondary vCPU.
977 */
978 arch_timer_mask(&vcpu->regs);
979 }
980
Fuad Tabbaed294af2019-12-20 10:43:01 +0000981 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000982 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000983
Andrew Scull33fecd32019-01-08 14:48:27 +0000984 /*
985 * Set a placeholder return code to the scheduler. This will be
986 * overwritten when the switch back to the primary occurs.
987 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 ret.func = FFA_INTERRUPT_32;
Andrew Walbran7e824602020-10-22 16:51:40 +0100989 ret.arg1 = 0;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100990 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000991
Andrew Scull6d2db332018-10-10 15:28:17 +0100992out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100993 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100994}
995
996/**
Andrew Scull81e85092018-12-12 12:56:20 +0000997 * Check that the mode indicates memory that is valid, owned and exclusive.
998 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100999static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001000{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001001 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1002 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001003}
1004
1005/**
Andrew Walbranc8a01972020-09-22 11:23:30 +01001006 * Determines the value to be returned by api_ffa_rxtx_map and
1007 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
1008 * there are waiters, it also switches back to the primary VM for it to wake
1009 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001010 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001011static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
1012 struct vcpu *current,
1013 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001014{
1015 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001016
1017 if (list_empty(&vm->mailbox.waiter_list)) {
1018 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001019 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001020 }
1021
1022 if (vm->id == HF_PRIMARY_VM_ID) {
1023 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001024 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001025 }
1026
1027 /*
1028 * Switch back to the primary VM, informing it that there are waiters
1029 * that need to be notified.
1030 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001031 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001032 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001033 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001034
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001035 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001036}
1037
1038/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001039 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001040 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001041static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1042 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001043 paddr_t pa_send_begin, paddr_t pa_send_end,
1044 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001045 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001046 struct mpool *local_page_pool)
1047{
1048 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001049
1050 /* Map the send page as read-only in the hypervisor address space. */
1051 vm_locked.vm->mailbox.send =
1052 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001053 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001054 if (!vm_locked.vm->mailbox.send) {
1055 /* TODO: partial defrag of failed range. */
1056 /* Recover any memory consumed in failed mapping. */
1057 mm_defrag(mm_stage1_locked, local_page_pool);
1058 goto fail;
1059 }
1060
1061 /*
1062 * Map the receive page as writable in the hypervisor address space. On
1063 * failure, unmap the send page before returning.
1064 */
1065 vm_locked.vm->mailbox.recv =
1066 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001067 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001068 if (!vm_locked.vm->mailbox.recv) {
1069 /* TODO: partial defrag of failed range. */
1070 /* Recover any memory consumed in failed mapping. */
1071 mm_defrag(mm_stage1_locked, local_page_pool);
1072 goto fail_undo_send;
1073 }
1074
1075 ret = true;
1076 goto out;
1077
1078 /*
1079 * The following mappings will not require more memory than is available
1080 * in the local pool.
1081 */
1082fail_undo_send:
1083 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001084 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1085 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001086
1087fail:
1088 ret = false;
1089
1090out:
Andrew Sculle1322792019-07-01 17:46:10 +01001091 return ret;
1092}
1093
1094/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001095 * Sanity checks and configures the send and receive pages in the VM stage-2
1096 * and hypervisor stage-1 page tables.
1097 *
1098 * Returns:
1099 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001100 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001101 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1102 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001103 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001104 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001105 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001106
1107struct ffa_value api_vm_configure_pages(
1108 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1109 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1110 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001111{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001112 struct ffa_value ret;
1113 paddr_t pa_send_begin;
1114 paddr_t pa_send_end;
1115 paddr_t pa_recv_begin;
1116 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001117 uint32_t orig_send_mode = 0;
1118 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001119 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001120
1121 /* We only allow these to be setup once. */
1122 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1123 ret = ffa_error(FFA_DENIED);
1124 goto out;
1125 }
1126
1127 /* Hafnium only supports a fixed size of RX/TX buffers. */
1128 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1129 ret = ffa_error(FFA_INVALID_PARAMETERS);
1130 goto out;
1131 }
1132
1133 /* Fail if addresses are not page-aligned. */
1134 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1135 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1136 ret = ffa_error(FFA_INVALID_PARAMETERS);
1137 goto out;
1138 }
1139
1140 /* Convert to physical addresses. */
1141 pa_send_begin = pa_from_ipa(send);
1142 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1143 pa_recv_begin = pa_from_ipa(recv);
1144 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1145
1146 /* Fail if the same page is used for the send and receive pages. */
1147 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1148 ret = ffa_error(FFA_INVALID_PARAMETERS);
1149 goto out;
1150 }
Andrew Sculle1322792019-07-01 17:46:10 +01001151
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001152 /* Set stage 2 translation tables only for virtual FF-A instances. */
1153 if (vm_id_is_current_world(vm_locked.vm->id)) {
1154 /*
1155 * Ensure the pages are valid, owned and exclusive to the VM and
1156 * that the VM has the required access to the memory.
1157 */
1158 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1159 &orig_send_mode) ||
1160 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1161 (orig_send_mode & MM_MODE_R) == 0 ||
1162 (orig_send_mode & MM_MODE_W) == 0) {
1163 dlog_error(
1164 "VM doesn't have required access rights to map "
1165 "TX buffer in stage 2.\n");
1166 ret = ffa_error(FFA_INVALID_PARAMETERS);
1167 goto out;
1168 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001169
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001170 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1171 &orig_recv_mode) ||
1172 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1173 (orig_recv_mode & MM_MODE_R) == 0) {
1174 dlog_error(
1175 "VM doesn't have required access rights to map "
1176 "RX buffer in stage 2.\n");
1177 ret = ffa_error(FFA_INVALID_PARAMETERS);
1178 goto out;
1179 }
Andrew Sculle1322792019-07-01 17:46:10 +01001180
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001181 /* Take memory ownership away from the VM and mark as shared. */
1182 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1183 MM_MODE_W;
1184 if (vm_locked.vm->el0_partition) {
1185 mode |= MM_MODE_USER | MM_MODE_NG;
1186 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001187
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001188 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1189 mode, local_page_pool, NULL)) {
1190 dlog_error(
1191 "Cannot allocate a new entry in stage 2 "
1192 "translation table.\n");
1193 ret = ffa_error(FFA_NO_MEMORY);
1194 goto out;
1195 }
Andrew Sculle1322792019-07-01 17:46:10 +01001196
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001197 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1198 if (vm_locked.vm->el0_partition) {
1199 mode |= MM_MODE_USER | MM_MODE_NG;
1200 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001201
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001202 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1203 mode, local_page_pool, NULL)) {
1204 /* TODO: partial defrag of failed range. */
1205 /* Recover any memory consumed in failed mapping. */
1206 vm_ptable_defrag(vm_locked, local_page_pool);
1207 goto fail_undo_send;
1208 }
Andrew Sculle1322792019-07-01 17:46:10 +01001209 }
1210
Olivier Deprez96a2a262020-06-11 17:21:38 +02001211 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1212 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1213
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001214 /*
1215 * For EL0 partitions, since both the partition and the hypervisor code
1216 * use the EL2&0 translation regime, it is critical to mark the mappings
1217 * of the send and recv buffers as non-global in the TLB. For one, if we
1218 * dont mark it as non-global, it would cause TLB conflicts since there
1219 * would be an identity mapping with non-global attribute in the
1220 * partitions page tables, but another identity mapping in the
1221 * hypervisor page tables with the global attribute. The other issue is
1222 * one of security, we dont want other partitions to be able to access
1223 * other partitions buffers through cached translations.
1224 */
1225 if (vm_locked.vm->el0_partition) {
1226 extra_attributes |= MM_MODE_NG;
1227 }
1228
Manish Pandeyd34f8892020-06-19 17:41:07 +01001229 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1230 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001231 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001232 goto fail_undo_send_and_recv;
1233 }
1234
Manish Pandeyd34f8892020-06-19 17:41:07 +01001235 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001236 goto out;
1237
Andrew Sculle1322792019-07-01 17:46:10 +01001238fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001239 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001240 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001241
1242fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001243 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001244 orig_send_mode, local_page_pool, NULL));
1245 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001246
1247out:
Andrew Sculle1322792019-07-01 17:46:10 +01001248 return ret;
1249}
1250
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001251static void api_get_rxtx_description(struct vm_locked vm_locked, ipaddr_t *send,
1252 ipaddr_t *recv, uint32_t *page_count,
1253 ffa_vm_id_t *owner_vm_id)
1254{
1255 /*
1256 * If the message has been forwarded the effective addresses are in
1257 * hypervisor's TX buffer.
1258 */
1259 bool forwarded = (vm_locked.vm->id == HF_OTHER_WORLD_ID) &&
1260 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1261 (*page_count == 0);
1262
1263 if (forwarded) {
1264 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc =
1265 (struct ffa_endpoint_rx_tx_descriptor *)
1266 vm_locked.vm->mailbox.send;
1267 struct ffa_composite_memory_region *rx_region =
1268 ffa_enpoint_get_rx_memory_region(endpoint_desc);
1269 struct ffa_composite_memory_region *tx_region =
1270 ffa_enpoint_get_tx_memory_region(endpoint_desc);
1271
1272 *owner_vm_id = endpoint_desc->endpoint_id;
1273 *recv = ipa_init(rx_region->constituents[0].address);
1274 *send = ipa_init(tx_region->constituents[0].address);
1275 *page_count = rx_region->constituents[0].page_count;
1276 } else {
1277 *owner_vm_id = vm_locked.vm->id;
1278 }
1279}
Andrew Sculle1322792019-07-01 17:46:10 +01001280/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001281 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001282 * must not be shared. Locking of the page tables combined with a local memory
1283 * pool ensures there will always be enough memory to recover from any errors
1284 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1285 * by another core which could result in this transaction being unable to roll
1286 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001287 *
1288 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001289 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001290 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001291 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001292 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001293 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001294 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001295 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001296struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001297 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001298{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001299 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001300 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001301 struct vm_locked vm_locked;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001302 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001303 struct mm_stage1_locked mm_stage1_locked;
1304 struct mpool local_page_pool;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001305 ffa_vm_id_t owner_vm_id;
1306
1307 vm_locked = vm_lock(vm);
1308 /*
1309 * Get the original buffer addresses and VM ID in case of forwarded
1310 * message.
1311 */
1312 api_get_rxtx_description(vm_locked, &send, &recv, &page_count,
1313 &owner_vm_id);
1314 vm_unlock(&vm_locked);
1315
1316 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1317 if (owner_vm_locked.vm == NULL) {
1318 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1319 owner_vm_id);
1320 return ffa_error(FFA_DENIED);
1321 }
Andrew Scull220e6212018-12-21 18:09:00 +00001322
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001323 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001324 * Create a local pool so any freed memory can't be used by another
1325 * thread. This is to ensure the original mapping can be restored if any
1326 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001327 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001328 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1329
Manish Pandeyd34f8892020-06-19 17:41:07 +01001330 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001331
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001332 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1333 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001334 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001335 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001336 }
1337
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001338 /* Forward buffer mapping to SPMC if coming from a VM. */
1339 plat_ffa_rxtx_map_forward(owner_vm_locked);
1340
Federico Recanati9f1b6532022-04-14 13:15:28 +02001341 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001342
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001343exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001344 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001345 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001346 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001347
1348 return ret;
1349}
1350
1351/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001352 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1353 * translation regime of the caller. Unmap the region for the hypervisor and
1354 * set the memory region to owned and exclusive for the component. Since the
1355 * memory region mapped in the page table, when the buffers were originally
1356 * created we can safely remap it.
1357 *
1358 * Returns:
1359 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1360 * behalf of the caller.
1361 * - FFA_SUCCESS on success if no further action is needed.
1362 */
1363struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1364 struct vcpu *current)
1365{
1366 struct vm *vm = current->vm;
1367 struct vm_locked vm_locked;
Federico Recanati8da9e332022-02-10 11:00:17 +01001368 ffa_vm_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001369 struct mm_stage1_locked mm_stage1_locked;
1370 paddr_t send_pa_begin;
1371 paddr_t send_pa_end;
1372 paddr_t recv_pa_begin;
1373 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001374 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001375
Federico Recanati8da9e332022-02-10 11:00:17 +01001376 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1377 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
1378 dlog_error("`allocator_id` must be 0 at virtual instances.\n");
1379 return ffa_error(FFA_INVALID_PARAMETERS);
1380 }
1381
1382 /* VM ID of which buffers have to be unmapped. */
1383 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1384
1385 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1386 vm = vm_locked.vm;
1387 if (vm == NULL) {
1388 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1389 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001390 return ffa_error(FFA_INVALID_PARAMETERS);
1391 }
1392
1393 /* Get send and receive buffers. */
1394 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001395 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001396 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001397 ret = ffa_error(FFA_INVALID_PARAMETERS);
1398 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001399 }
1400
1401 /* Currently a mailbox size of 1 page is assumed. */
1402 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1403 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1404 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1405 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1406
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001407 mm_stage1_locked = mm_lock_stage1();
1408
Federico Recanati8da9e332022-02-10 11:00:17 +01001409 /* Reset stage 2 mapping only for virtual FF-A instances. */
1410 if (vm_id_is_current_world(owner_vm_id)) {
1411 /*
1412 * Set the memory region of the buffers back to the default mode
1413 * for the VM. Since this memory region was already mapped for
1414 * the RXTX buffers we can safely remap them.
1415 */
1416 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1417 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1418 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001419
Federico Recanati8da9e332022-02-10 11:00:17 +01001420 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1421 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1422 &api_page_pool, NULL));
1423 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001424
1425 /* Unmap the buffers in the partition manager. */
1426 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1427 &api_page_pool));
1428 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1429 &api_page_pool));
1430
1431 vm->mailbox.send = NULL;
1432 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001433 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001434
Federico Recanati8da9e332022-02-10 11:00:17 +01001435 /* Forward buffer unmapping to SPMC if coming from a VM. */
1436 plat_ffa_rxtx_unmap_forward(owner_vm_id);
1437
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001438 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001439
1440out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001441 vm_unlock(&vm_locked);
1442
Federico Recanati8da9e332022-02-10 11:00:17 +01001443 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001444}
1445
1446/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001447 * Notifies the `to` VM about the message currently in its mailbox, possibly
1448 * with the help of the primary VM.
1449 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001450static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1451 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001452{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001453 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1454 struct ffa_value primary_ret = {
1455 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001456 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001457 };
1458
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001459 /* Messages for the primary VM are delivered directly. */
1460 if (to.vm->id == HF_PRIMARY_VM_ID) {
1461 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001462 * Only tell the primary VM the size and other details if the
1463 * message is for it, to avoid leaking data about messages for
1464 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001465 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001466 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001467
1468 to.vm->mailbox.state = MAILBOX_STATE_READ;
1469 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001470 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001471 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001472 }
1473
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001474 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1475
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001476 /* Messages for the TEE are sent on via the dispatcher. */
1477 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001478 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001479
Olivier Deprez112d2b52020-09-30 07:39:23 +02001480 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001481 /*
1482 * After the call to the TEE completes it must have finished
1483 * reading its RX buffer, so it is ready for another message.
1484 */
1485 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001486 /*
1487 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001488 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001489 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001490 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001491 }
1492
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001493 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001494 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001495 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001496 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001497 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001498
1499 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001500}
1501
1502/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001503 * Copies data from the sender's send buffer to the recipient's receive buffer
1504 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001505 *
1506 * If the recipient's receive buffer is busy, it can optionally register the
1507 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001508 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001509struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1510 ffa_vm_id_t receiver_vm_id, uint32_t size,
1511 uint32_t attributes, struct vcpu *current,
1512 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001513{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001514 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001515 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001516 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001517 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001518 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001519 struct vcpu_locked current_locked;
1520 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001521 bool notify =
1522 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001523
Andrew Walbran70bc8622019-10-07 14:15:58 +01001524 /* Ensure sender VM ID corresponds to the current VM. */
1525 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001526 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001527 }
1528
1529 /* Disallow reflexive requests as this suggests an error in the VM. */
1530 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001531 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001532 }
1533
1534 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001535 if (size > FFA_MSG_PAYLOAD_MAX) {
1536 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001537 }
1538
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001539 /*
1540 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1541 * invocation.
1542 */
1543 current_locked = vcpu_lock(current);
1544 is_direct_request_ongoing =
1545 is_ffa_direct_msg_request_ongoing(current_locked);
1546 vcpu_unlock(&current_locked);
1547
1548 if (is_direct_request_ongoing) {
1549 return ffa_error(FFA_DENIED);
1550 }
1551
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001552 /* Ensure the receiver VM exists. */
1553 to = vm_find(receiver_vm_id);
1554 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001555 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001556 }
1557
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001558 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001559 * Check that the sender has configured its send buffer. If the tx
1560 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1561 * be safely accessed after releasing the lock since the tx mailbox
1562 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001563 */
1564 sl_lock(&from->lock);
1565 from_msg = from->mailbox.send;
1566 sl_unlock(&from->lock);
1567
1568 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001569 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001570 }
1571
Andrew Walbran82d6d152019-12-24 15:02:06 +00001572 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001573
Andrew Walbran82d6d152019-12-24 15:02:06 +00001574 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001575 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001576 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001577 }
1578
Andrew Walbran82d6d152019-12-24 15:02:06 +00001579 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001580 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001581 to->mailbox.recv_size = size;
1582 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001583 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001584 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001585
1586out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001587 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001588
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001589 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001590}
1591
1592/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001593 * Checks whether the vCPU's attempt to block for a message has already been
1594 * interrupted or whether it is allowed to block.
1595 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001596bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001597{
Manish Pandey35e452f2021-02-18 21:36:34 +00001598 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001599 bool interrupted;
1600
Manish Pandey35e452f2021-02-18 21:36:34 +00001601 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001602
1603 /*
1604 * Don't block if there are enabled and pending interrupts, to match
1605 * behaviour of wait_for_interrupt.
1606 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001607 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001608
Manish Pandey35e452f2021-02-18 21:36:34 +00001609 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001610
1611 return interrupted;
1612}
1613
1614/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001615 * Receives a message from the mailbox. If one isn't available, this function
1616 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001617 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001618 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001619 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001620struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1621 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001622{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001623 bool is_direct_request_ongoing;
1624 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001625 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001626 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001627 bool is_from_secure_world =
1628 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001629
Andrew Scullaa039b32018-10-04 15:02:26 +01001630 /*
1631 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001632 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001633 */
J-Alvesb37fd082020-10-22 12:29:21 +01001634 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001635 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001636 }
1637
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001638 /*
1639 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1640 * invocation.
1641 */
1642 current_locked = vcpu_lock(current);
1643 is_direct_request_ongoing =
1644 is_ffa_direct_msg_request_ongoing(current_locked);
1645 vcpu_unlock(&current_locked);
1646
1647 if (is_direct_request_ongoing) {
1648 return ffa_error(FFA_DENIED);
1649 }
1650
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001651 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001652
Andrew Scullaa039b32018-10-04 15:02:26 +01001653 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001654 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1655 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001656 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001657 goto out;
1658 }
1659
1660 /* No pending message so fail if not allowed to block. */
1661 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001662 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001663 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001664 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001665
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001666 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001667 * From this point onward this call can only be interrupted or a message
1668 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001669 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001670 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001671 return_code = ffa_error(FFA_INTERRUPTED);
1672 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001673 goto out;
1674 }
1675
J-Alvesb37fd082020-10-22 12:29:21 +01001676 if (is_from_secure_world) {
1677 /* Return to other world if caller is a SP. */
1678 *next = api_switch_to_other_world(
1679 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001680 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001681 } else {
1682 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001683 struct ffa_value run_return = {
1684 .func = FFA_MSG_WAIT_32,
1685 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001686 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001687
Andrew Walbranb4816552018-12-05 17:35:42 +00001688 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001689 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001690 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001691out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001692 sl_unlock(&vm->lock);
1693
Jose Marinho3e2442f2019-03-12 13:30:37 +00001694 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001695}
1696
1697/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001698 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1699 * by this function, the caller must have called api_mailbox_send before with
1700 * the notify argument set to true, and this call must have failed because the
1701 * mailbox was not available.
1702 *
1703 * It should be called repeatedly to retrieve a list of VMs.
1704 *
1705 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1706 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001707 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001708int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001709{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001710 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001711 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001712 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001713
1714 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001715 if (list_empty(&vm->mailbox.ready_list)) {
1716 ret = -1;
1717 goto exit;
1718 }
1719
1720 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1721 ready_links);
1722 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001723 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001724
1725exit:
1726 sl_unlock(&vm->lock);
1727 return ret;
1728}
1729
1730/**
1731 * Retrieves the next VM waiting to be notified that the mailbox of the
1732 * specified VM became writable. Only primary VMs are allowed to call this.
1733 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001734 * Returns -1 on failure or if there are no waiters; the VM id of the next
1735 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001736 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001737int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001738{
1739 struct vm *vm;
1740 struct vm_locked locked;
1741 struct wait_entry *entry;
1742 struct vm *waiting_vm;
1743
1744 /* Only primary VMs are allowed to call this function. */
1745 if (current->vm->id != HF_PRIMARY_VM_ID) {
1746 return -1;
1747 }
1748
Andrew Walbran42347a92019-05-09 13:59:03 +01001749 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001750 if (vm == NULL) {
1751 return -1;
1752 }
1753
Fuad Tabbaed294af2019-12-20 10:43:01 +00001754 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001755 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001756 entry = api_fetch_waiter(locked);
1757 vm_unlock(&locked);
1758
1759 if (entry == NULL) {
1760 return -1;
1761 }
1762
1763 /* Enqueue notification to waiting VM. */
1764 waiting_vm = entry->waiting_vm;
1765
1766 sl_lock(&waiting_vm->lock);
1767 if (list_empty(&entry->ready_links)) {
1768 list_append(&waiting_vm->mailbox.ready_list,
1769 &entry->ready_links);
1770 }
1771 sl_unlock(&waiting_vm->lock);
1772
1773 return waiting_vm->id;
1774}
1775
1776/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001777 * Releases the caller's mailbox so that a new message can be received. The
1778 * caller must have copied out all data they wish to preserve as new messages
1779 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001780 *
1781 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001782 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1783 * - FFA_SUCCESS on success if no further action is needed.
1784 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001785 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001786 * hf_mailbox_waiter_get.
1787 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001788struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001789{
1790 struct vm *vm = current->vm;
1791 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001792 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001793
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001794 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001795 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001796 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001797 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001798 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001799 break;
1800
Andrew Sculld6ee1102019-04-05 22:12:42 +01001801 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001802 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001803 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001804 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001805 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001806 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001807
1808 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001809}
Andrew Walbran318f5732018-11-20 16:23:42 +00001810
1811/**
1812 * Enables or disables a given interrupt ID for the calling vCPU.
1813 *
1814 * Returns 0 on success, or -1 if the intid is invalid.
1815 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001816int64_t api_interrupt_enable(uint32_t intid, bool enable,
1817 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001818{
Manish Pandey35e452f2021-02-18 21:36:34 +00001819 struct vcpu_locked current_locked;
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001820 uint32_t intid_index = INTID_INDEX(intid);
1821 uint32_t intid_mask = INTID_MASK(1U, intid);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001822
Andrew Walbran318f5732018-11-20 16:23:42 +00001823 if (intid >= HF_NUM_INTIDS) {
1824 return -1;
1825 }
1826
Manish Pandey35e452f2021-02-18 21:36:34 +00001827 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001828 if (enable) {
Maksims Svecovs953dee52022-04-01 12:15:36 +01001829 if (type == INTERRUPT_TYPE_IRQ) {
1830 current->interrupts.interrupt_type[intid_index] &=
1831 ~intid_mask;
1832 } else if (type == INTERRUPT_TYPE_FIQ) {
1833 current->interrupts.interrupt_type[intid_index] |=
1834 intid_mask;
1835 }
1836
Andrew Walbran3d84a262018-12-13 14:41:19 +00001837 /*
1838 * If it is pending and was not enabled before, increment the
1839 * count.
1840 */
1841 if (current->interrupts.interrupt_pending[intid_index] &
1842 ~current->interrupts.interrupt_enabled[intid_index] &
1843 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001844 if ((current->interrupts.interrupt_type[intid_index] &
1845 intid_mask) ==
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001846 INTID_MASK(INTERRUPT_TYPE_IRQ, intid)) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001847 vcpu_irq_count_increment(current_locked);
1848 } else {
1849 vcpu_fiq_count_increment(current_locked);
1850 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001851 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001852 current->interrupts.interrupt_enabled[intid_index] |=
1853 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001854 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001855 /*
1856 * If it is pending and was enabled before, decrement the count.
1857 */
1858 if (current->interrupts.interrupt_pending[intid_index] &
1859 current->interrupts.interrupt_enabled[intid_index] &
1860 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001861 if ((current->interrupts.interrupt_type[intid_index] &
1862 intid_mask) ==
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001863 INTID_MASK(INTERRUPT_TYPE_IRQ, intid)) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001864 vcpu_irq_count_decrement(current_locked);
1865 } else {
1866 vcpu_fiq_count_decrement(current_locked);
1867 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001868 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001869 current->interrupts.interrupt_enabled[intid_index] &=
1870 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001871 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001872 }
1873
Manish Pandey35e452f2021-02-18 21:36:34 +00001874 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001875 return 0;
1876}
1877
1878/**
1879 * Returns the ID of the next pending interrupt for the calling vCPU, and
1880 * acknowledges it (i.e. marks it as no longer pending). Returns
1881 * HF_INVALID_INTID if there are no pending interrupts.
1882 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001883uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001884{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07001885 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00001886 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001887 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001888
1889 /*
1890 * Find the first enabled and pending interrupt ID, return it, and
1891 * deactivate it.
1892 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001893 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001894 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1895 uint32_t enabled_and_pending =
1896 current->interrupts.interrupt_enabled[i] &
1897 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001898
Andrew Walbran318f5732018-11-20 16:23:42 +00001899 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001900 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001901 uint32_t intid_mask = 1U << bit_index;
1902
Andrew Walbran3d84a262018-12-13 14:41:19 +00001903 /*
1904 * Mark it as no longer pending and decrement the count.
1905 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001906 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1907
1908 if ((current->interrupts.interrupt_type[i] &
1909 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1910 vcpu_irq_count_decrement(current_locked);
1911 } else {
1912 vcpu_fiq_count_decrement(current_locked);
1913 }
1914
Andrew Walbran3d84a262018-12-13 14:41:19 +00001915 first_interrupt =
1916 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001917 break;
1918 }
1919 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001920
Manish Pandey35e452f2021-02-18 21:36:34 +00001921 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001922 return first_interrupt;
1923}
1924
1925/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001926 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001927 * given VM and vCPU.
1928 */
1929static inline bool is_injection_allowed(uint32_t target_vm_id,
1930 struct vcpu *current)
1931{
1932 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001933
Andrew Walbran318f5732018-11-20 16:23:42 +00001934 /*
1935 * The primary VM is allowed to inject interrupts into any VM. Secondary
1936 * VMs are only allowed to inject interrupts into their own vCPUs.
1937 */
1938 return current_vm_id == HF_PRIMARY_VM_ID ||
1939 current_vm_id == target_vm_id;
1940}
1941
1942/**
1943 * Injects a virtual interrupt of the given ID into the given target vCPU.
1944 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1945 * when the vCPU is next run, which is up to the scheduler.
1946 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001947 * Returns:
1948 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1949 * ID is invalid, or the current VM is not allowed to inject interrupts to
1950 * the target VM.
1951 * - 0 on success if no further action is needed.
1952 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1953 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001954 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001955int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1956 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001957 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001958{
Andrew Walbran318f5732018-11-20 16:23:42 +00001959 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001960 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001961
1962 if (intid >= HF_NUM_INTIDS) {
1963 return -1;
1964 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001965
Andrew Walbran318f5732018-11-20 16:23:42 +00001966 if (target_vm == NULL) {
1967 return -1;
1968 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001969
Andrew Walbran318f5732018-11-20 16:23:42 +00001970 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001971 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001972 return -1;
1973 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001974
Andrew Walbran318f5732018-11-20 16:23:42 +00001975 if (!is_injection_allowed(target_vm_id, current)) {
1976 return -1;
1977 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001978
Andrew Walbrane1310df2019-04-29 17:28:28 +01001979 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001980
Manish Pandey35e452f2021-02-18 21:36:34 +00001981 dlog_verbose(
1982 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1983 "%u\n",
1984 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1985 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001986 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001987}
Andrew Scull6386f252018-12-06 13:29:10 +00001988
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001989/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001990struct ffa_value api_ffa_version(struct vcpu *current,
1991 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001992{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001993 struct vm_locked current_vm_locked;
1994
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001995 /*
1996 * Ensure that both major and minor revision representation occupies at
1997 * most 15 bits.
1998 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001999 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002000 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002001 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002002 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002003 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01002004 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00002005 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01002006 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002007
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002008 current_vm_locked = vm_lock(current->vm);
2009 current_vm_locked.vm->ffa_version = requested_version;
2010 vm_unlock(&current_vm_locked);
2011
Daniel Boulby6e32c612021-02-17 15:09:41 +00002012 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002013}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002014
2015int64_t api_debug_log(char c, struct vcpu *current)
2016{
Andrew Sculld54e1be2019-08-20 11:09:42 +01002017 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002018 struct vm *vm = current->vm;
2019 struct vm_locked vm_locked = vm_lock(vm);
2020
Andrew Sculld54e1be2019-08-20 11:09:42 +01002021 if (c == '\n' || c == '\0') {
2022 flush = true;
2023 } else {
2024 vm->log_buffer[vm->log_buffer_length++] = c;
2025 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
2026 }
2027
2028 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01002029 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
2030 vm->log_buffer_length);
2031 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002032 }
2033
2034 vm_unlock(&vm_locked);
2035
2036 return 0;
2037}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002038
2039/**
J-Alves6f72ca82021-11-01 12:34:58 +00002040 * Helper for success return of FFA_FEATURES, for when it is used to query
2041 * an interrupt ID.
2042 */
2043struct ffa_value api_ffa_feature_success(uint32_t arg2)
2044{
2045 return (struct ffa_value){
2046 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
2047}
2048
2049/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002050 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002051 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002052 */
J-Alves6f72ca82021-11-01 12:34:58 +00002053struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002054{
J-Alves6f72ca82021-11-01 12:34:58 +00002055 /*
2056 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
2057 * if using a feature ID.
2058 */
2059 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
J-Alvesff676c12022-05-13 17:25:33 +01002060 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0U) {
J-Alves6f72ca82021-11-01 12:34:58 +00002061 return ffa_error(FFA_NOT_SUPPORTED);
2062 }
2063
2064 switch (feature_function_id) {
2065 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002066 case FFA_ERROR_32:
2067 case FFA_SUCCESS_32:
2068 case FFA_INTERRUPT_32:
2069 case FFA_VERSION_32:
2070 case FFA_FEATURES_32:
2071 case FFA_RX_RELEASE_32:
2072 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002073 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002074 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002075 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002076 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002077 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002078 case FFA_MEM_DONATE_32:
2079 case FFA_MEM_LEND_32:
2080 case FFA_MEM_SHARE_32:
2081 case FFA_MEM_RETRIEVE_REQ_32:
2082 case FFA_MEM_RETRIEVE_RESP_32:
2083 case FFA_MEM_RELINQUISH_32:
2084 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002085 case FFA_MEM_FRAG_RX_32:
2086 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002087 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002088 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002089 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002090 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00002091#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002092 /* FF-A v1.1 features. */
2093 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002094 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2095 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2096 case FFA_NOTIFICATION_BIND_32:
2097 case FFA_NOTIFICATION_UNBIND_32:
2098 case FFA_NOTIFICATION_SET_32:
2099 case FFA_NOTIFICATION_GET_32:
2100 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07002101 case FFA_MEM_PERM_GET_32:
2102 case FFA_MEM_PERM_SET_32:
2103 case FFA_MEM_PERM_GET_64:
2104 case FFA_MEM_PERM_SET_64:
J-Alves3829fc02021-03-18 12:49:18 +00002105#endif
2106 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00002107
2108#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2109 /* Check support of a feature provided respective feature ID. */
2110 case FFA_FEATURE_NPI:
2111 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2112 case FFA_FEATURE_SRI:
2113 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2114#endif
2115 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002116 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002117 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002118 }
2119}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002120
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002121/**
J-Alves645eabe2021-02-22 16:08:27 +00002122 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2123 * interfaces.
2124 */
2125static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2126{
2127 return args.arg2 == 0U;
2128}
2129
2130/**
J-Alves76d99af2021-03-10 17:42:11 +00002131 * Limits size of arguments in ffa_value structure to 32-bit.
2132 */
2133static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2134{
2135 return (struct ffa_value){
2136 .func = (uint32_t)args.func,
2137 .arg1 = (uint32_t)args.arg1,
2138 .arg2 = (uint32_t)0,
2139 .arg3 = (uint32_t)args.arg3,
2140 .arg4 = (uint32_t)args.arg4,
2141 .arg5 = (uint32_t)args.arg5,
2142 .arg6 = (uint32_t)args.arg6,
2143 .arg7 = (uint32_t)args.arg7,
2144 };
2145}
2146
2147/**
2148 * Helper to copy direct message payload, depending on SMC used and expected
2149 * registers size.
2150 */
2151static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2152{
2153 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2154 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2155 return api_ffa_value_copy32(args);
2156 }
2157
2158 return (struct ffa_value){
2159 .func = args.func,
2160 .arg1 = args.arg1,
2161 .arg2 = 0,
2162 .arg3 = args.arg3,
2163 .arg4 = args.arg4,
2164 .arg5 = args.arg5,
2165 .arg6 = args.arg6,
2166 .arg7 = args.arg7,
2167 };
2168}
2169
2170/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002171 * Send an FF-A direct message request.
2172 */
2173struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2174 ffa_vm_id_t receiver_vm_id,
2175 struct ffa_value args,
2176 struct vcpu *current,
2177 struct vcpu **next)
2178{
J-Alves17228f72021-04-20 17:13:19 +01002179 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002180 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002181 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002182 struct vcpu *receiver_vcpu;
2183 struct two_vcpu_locked vcpus_locked;
2184
J-Alves645eabe2021-02-22 16:08:27 +00002185 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2186 return ffa_error(FFA_INVALID_PARAMETERS);
2187 }
2188
Olivier Deprez55a189e2021-06-09 15:45:27 +02002189 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2190 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002191 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002192 }
2193
Olivier Deprez55a189e2021-06-09 15:45:27 +02002194 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002195 return ret;
2196 }
2197
2198 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2199
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002200 receiver_vm = vm_find(receiver_vm_id);
2201 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002202 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002203 return ffa_error(FFA_INVALID_PARAMETERS);
2204 }
2205
2206 /*
J-Alves439ac972021-11-18 17:32:03 +00002207 * Check if sender supports sending direct message req, and if
2208 * receiver supports receipt of direct message requests.
2209 */
2210 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2211 return ffa_error(FFA_DENIED);
2212 }
2213
2214 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002215 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002216 * UP (migratable) or MP partitions with a number of vCPUs matching the
2217 * number of PEs in the system. It further states that MP partitions
2218 * accepting direct request messages cannot migrate.
2219 */
J-Alvesad6a0432021-04-09 16:06:21 +01002220 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002221 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002222 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002223 return ffa_error(FFA_INVALID_PARAMETERS);
2224 }
2225
J-Alves6e2abc62021-12-02 14:58:56 +00002226 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002227 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2228
2229 /*
2230 * If destination vCPU is executing or already received an
2231 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2232 * busy. There is a brief period of time where the vCPU state has
2233 * changed but regs_available is still false thus consider this case as
2234 * the vCPU not yet ready to receive a direct message request.
2235 */
2236 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2237 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2238 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002239 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002240 ret = ffa_error(FFA_BUSY);
2241 goto out;
2242 }
2243
2244 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2245 memory_order_relaxed)) {
2246 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002247 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002248 receiver_vcpu->vm->id,
2249 vcpu_index(receiver_vcpu));
2250 receiver_vcpu->state = VCPU_STATE_ABORTED;
2251 }
2252
2253 ret = ffa_error(FFA_ABORTED);
2254 goto out;
2255 }
2256
2257 switch (receiver_vcpu->state) {
2258 case VCPU_STATE_OFF:
2259 case VCPU_STATE_RUNNING:
2260 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002261 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002262 case VCPU_STATE_BLOCKED:
2263 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002264 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2265 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002266 ret = ffa_error(FFA_BUSY);
2267 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002268 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002269 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002270 * We expect target vCPU to be in WAITING state after either
2271 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002272 */
2273 break;
2274 }
2275
2276 /* Inject timer interrupt if any pending */
2277 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002278 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2279 HF_VIRTUAL_TIMER_INTID, current,
2280 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002281
2282 arch_timer_mask(&receiver_vcpu->regs);
2283 }
2284
2285 /* The receiver vCPU runs upon direct message invocation */
2286 receiver_vcpu->cpu = current->cpu;
2287 receiver_vcpu->state = VCPU_STATE_RUNNING;
2288 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002289 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002290
J-Alves76d99af2021-03-10 17:42:11 +00002291 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002292
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002293 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002294
2295 /* Switch to receiver vCPU targeted to by direct msg request */
2296 *next = receiver_vcpu;
2297
J-Alves6e2abc62021-12-02 14:58:56 +00002298 if (!receiver_locked.vm->el0_partition) {
2299 /*
2300 * If the scheduler in the system is giving CPU cycles to the
2301 * receiver, due to pending notifications, inject the NPI
2302 * interrupt. Following call assumes that '*next' has been set
2303 * to receiver_vcpu.
2304 */
2305 plat_ffa_inject_notification_pending_interrupt(
2306 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2307 ? vcpus_locked.vcpu1
2308 : vcpus_locked.vcpu2,
2309 current, receiver_locked);
2310 }
2311
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002312 /*
2313 * Since this flow will lead to a VM switch, the return value will not
2314 * be applied to current vCPU.
2315 */
2316
2317out:
2318 sl_unlock(&receiver_vcpu->lock);
2319 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002320 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002321
2322 return ret;
2323}
2324
2325/**
2326 * Send an FF-A direct message response.
2327 */
2328struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2329 ffa_vm_id_t receiver_vm_id,
2330 struct ffa_value args,
2331 struct vcpu *current,
2332 struct vcpu **next)
2333{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002334 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002335
2336 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2337 return ffa_error(FFA_INVALID_PARAMETERS);
2338 }
2339
J-Alves76d99af2021-03-10 17:42:11 +00002340 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002341
Olivier Deprez55a189e2021-06-09 15:45:27 +02002342 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2343 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002344 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002345 }
2346
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002347 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002348 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002349 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002350 * No need for REQ/RESP state management as managed exit does
2351 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002352 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002353 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2354 vcpu_unlock(&current_locked);
2355 return ffa_error(FFA_DENIED);
2356 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002357
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002358 /*
2359 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2360 * a secure interrupt to a SP that is performing managed exit.
2361 * We have taken a implementation defined choice to not allow
2362 * Managed exit while a SP is processing a secure interrupt.
2363 */
2364 CHECK(!current->processing_secure_interrupt);
2365
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06002366 plat_interrupts_set_priority_mask(current->priority_mask);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002367 current->processing_managed_exit = false;
2368 } else {
2369 /*
2370 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2371 * defined originator.
2372 */
2373 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2374 /*
2375 * Sending direct response but direct request origin
2376 * vCPU is not set.
2377 */
2378 vcpu_unlock(&current_locked);
2379 return ffa_error(FFA_DENIED);
2380 }
2381
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002382 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002383 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2384 vcpu_unlock(&current_locked);
2385 return ffa_error(FFA_DENIED);
2386 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002387 }
2388
2389 /* Clear direct request origin for the caller. */
2390 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2391
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002392 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002393
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002394 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002395 *next = api_switch_to_other_world(
2396 current, to_ret,
2397 /*
2398 * Current vcpu sent a direct response. It moves to
2399 * waiting state.
2400 */
2401 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002402 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002403 *next = api_switch_to_primary(
2404 current, to_ret,
2405 /*
2406 * Current vcpu sent a direct response. It moves to
2407 * waiting state.
2408 */
2409 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002410 } else if (vm_id_is_current_world(receiver_vm_id)) {
2411 /*
2412 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002413 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002414 * made function return error before getting to this point.
2415 */
2416 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002417 /*
2418 * current vcpu sent a direct response.
2419 * It moves to waiting state.
2420 */
2421 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002422 } else {
2423 panic("Invalid direct message response invocation");
2424 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002425
2426 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2427}
2428
J-Alves84658fc2021-06-17 14:37:32 +01002429static bool api_memory_region_check_flags(
2430 struct ffa_memory_region *memory_region, uint32_t share_func)
2431{
2432 switch (share_func) {
2433 case FFA_MEM_SHARE_32:
2434 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2435 0U) {
2436 return false;
2437 }
2438 /* Intentional fall-through */
2439 case FFA_MEM_LEND_32:
2440 case FFA_MEM_DONATE_32: {
2441 /* Bits 31:2 Must Be Zero. */
2442 ffa_memory_receiver_flags_t to_mask =
2443 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2444 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2445
2446 if ((memory_region->flags & to_mask) != 0U) {
2447 return false;
2448 }
2449 break;
2450 }
2451 default:
2452 panic("Check for mem send calls only.\n");
2453 }
2454
2455 /* Last check reserved values are 0 */
2456 return true;
2457}
2458
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002459struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2460 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002461 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002462{
2463 struct vm *from = current->vm;
2464 struct vm *to;
2465 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002466 struct ffa_memory_region *memory_region;
2467 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01002468 bool targets_other_world = false;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002469
2470 if (ipa_addr(address) != 0 || page_count != 0) {
2471 /*
2472 * Hafnium only supports passing the descriptor in the TX
2473 * mailbox.
2474 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002475 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002476 }
2477
Andrew Walbranca808b12020-05-15 17:22:28 +01002478 if (fragment_length > length) {
2479 dlog_verbose(
2480 "Fragment length %d greater than total length %d.\n",
2481 fragment_length, length);
2482 return ffa_error(FFA_INVALID_PARAMETERS);
2483 }
2484 if (fragment_length < sizeof(struct ffa_memory_region) +
2485 sizeof(struct ffa_memory_access)) {
2486 dlog_verbose(
2487 "Initial fragment length %d smaller than header size "
2488 "%d.\n",
2489 fragment_length,
2490 sizeof(struct ffa_memory_region) +
2491 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002492 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002493 }
2494
2495 /*
2496 * Check that the sender has configured its send buffer. If the TX
2497 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2498 * be safely accessed after releasing the lock since the TX mailbox
2499 * address can only be configured once.
2500 */
2501 sl_lock(&from->lock);
2502 from_msg = from->mailbox.send;
2503 sl_unlock(&from->lock);
2504
2505 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002506 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002507 }
2508
2509 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002510 * Copy the memory region descriptor to a fresh page from the memory
2511 * pool. This prevents the sender from changing it underneath us, and
2512 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002513 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002514 if (fragment_length > HF_MAILBOX_SIZE ||
2515 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002516 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002517 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002518 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002519 if (memory_region == NULL) {
2520 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002521 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002522 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002523 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002524
2525 /* The sender must match the caller. */
2526 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002527 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002528 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002529 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002530 }
2531
J-Alves84658fc2021-06-17 14:37:32 +01002532 if (!api_memory_region_check_flags(memory_region, share_func)) {
2533 dlog_verbose(
2534 "Memory region reserved arguments must be zero.\n");
2535 ret = ffa_error(FFA_INVALID_PARAMETERS);
2536 goto out;
2537 }
2538
J-Alves363f5722022-04-25 17:37:37 +01002539 if (memory_region->receiver_count == 0U) {
2540 dlog_verbose("Receiver count can't be 0.\n");
2541 ret = ffa_error(FFA_INVALID_PARAMETERS);
2542 goto out;
2543 }
2544
2545 if (share_func == FFA_MEM_DONATE_32 &&
2546 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002547 dlog_verbose(
J-Alves363f5722022-04-25 17:37:37 +01002548 "FFA_MEM_DONATE only supports one recipient. "
2549 "Specified %u\n",
2550 memory_region->receiver_count);
2551 ret = ffa_error(FFA_INVALID_PARAMETERS);
2552 goto out;
2553 }
2554
2555 if (memory_region->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
2556 dlog_verbose(
2557 "Max number of recipients supported is %u "
2558 "specified %u\n",
2559 MAX_MEM_SHARE_RECIPIENTS,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002560 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002561 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002562 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002563 }
2564
2565 /*
2566 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01002567 * If there is a receiver from the other world, track it for later
2568 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002569 */
J-Alves363f5722022-04-25 17:37:37 +01002570 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
2571 ffa_vm_id_t receiver_id =
2572 memory_region->receivers[i]
2573 .receiver_permissions.receiver;
2574 to = vm_find(receiver_id);
2575
2576 if (vm_id_is_current_world(receiver_id) &&
2577 (to == NULL || to == from)) {
2578 dlog_verbose("%s: invalid receiver.\n", __func__);
2579 ret = ffa_error(FFA_INVALID_PARAMETERS);
2580 goto out;
2581 }
2582
2583 if (!plat_ffa_is_memory_send_valid(receiver_id, share_func)) {
2584 ret = ffa_error(FFA_DENIED);
2585 goto out;
2586 }
2587
2588 /* Capture if any of the receivers is from the other world. */
2589 if (!targets_other_world) {
2590 targets_other_world =
2591 !vm_id_is_current_world(receiver_id);
2592 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002593 }
2594
J-Alves363f5722022-04-25 17:37:37 +01002595 /* Allow for one memory region to be shared to the TEE. */
2596 if (targets_other_world) {
2597 assert(memory_region->receiver_count == 1 &&
2598 to->id == HF_TEE_VM_ID);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002599 /*
2600 * The 'to' VM lock is only needed in the case that it is the
2601 * TEE VM.
2602 */
2603 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2604
2605 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002606 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002607 goto out_unlock;
2608 }
2609
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002610 ret = ffa_memory_tee_send(
2611 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2612 length, fragment_length, share_func, &api_page_pool);
2613 /*
2614 * ffa_tee_memory_send takes ownership of the memory_region, so
2615 * make sure we don't free it.
2616 */
2617 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002618
2619 out_unlock:
2620 vm_unlock(&vm_to_from_lock.vm1);
2621 vm_unlock(&vm_to_from_lock.vm2);
2622 } else {
2623 struct vm_locked from_locked = vm_lock(from);
2624
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002625 ret = ffa_memory_send(from_locked, memory_region, length,
2626 fragment_length, share_func,
2627 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002628 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002629 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002630 * make sure we don't free it.
2631 */
2632 memory_region = NULL;
2633
2634 vm_unlock(&from_locked);
2635 }
2636
2637out:
2638 if (memory_region != NULL) {
2639 mpool_free(&api_page_pool, memory_region);
2640 }
2641
2642 return ret;
2643}
2644
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002645struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2646 uint32_t fragment_length,
2647 ipaddr_t address, uint32_t page_count,
2648 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002649{
2650 struct vm *to = current->vm;
2651 struct vm_locked to_locked;
2652 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002653 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002654 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002655 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002656
2657 if (ipa_addr(address) != 0 || page_count != 0) {
2658 /*
2659 * Hafnium only supports passing the descriptor in the TX
2660 * mailbox.
2661 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002662 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002663 }
2664
Andrew Walbrana65a1322020-04-06 19:32:32 +01002665 if (fragment_length != length) {
2666 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002667 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002668 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002669
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002670 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002671 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002672 message_buffer_size = cpu_get_buffer_size(current->cpu);
2673 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2674 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002675 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002676 }
2677
2678 to_locked = vm_lock(to);
2679 to_msg = to->mailbox.send;
2680
2681 if (to_msg == NULL) {
2682 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002683 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002684 goto out;
2685 }
2686
2687 /*
2688 * Copy the retrieve request descriptor to an internal buffer, so that
2689 * the caller can't change it underneath us.
2690 */
2691 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2692
2693 if (msg_receiver_busy(to_locked, NULL, false)) {
2694 /*
2695 * Can't retrieve memory information if the mailbox is not
2696 * available.
2697 */
2698 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002699 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002700 goto out;
2701 }
2702
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002703 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2704 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002705
2706out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002707 vm_unlock(&to_locked);
2708 return ret;
2709}
2710
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002711struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002712{
2713 struct vm *from = current->vm;
2714 struct vm_locked from_locked;
2715 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002716 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002717 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002718 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002719 uint32_t length;
2720
2721 from_locked = vm_lock(from);
2722 from_msg = from->mailbox.send;
2723
2724 if (from_msg == NULL) {
2725 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002726 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002727 goto out;
2728 }
2729
2730 /*
2731 * Calculate length from relinquish descriptor before copying. We will
2732 * check again later to make sure it hasn't changed.
2733 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002734 length = sizeof(struct ffa_mem_relinquish) +
2735 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2736 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002737 /*
2738 * Copy the relinquish descriptor to an internal buffer, so that the
2739 * caller can't change it underneath us.
2740 */
2741 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002742 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002743 message_buffer_size = cpu_get_buffer_size(current->cpu);
2744 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2745 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002746 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002747 goto out;
2748 }
2749 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2750
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002751 if (sizeof(struct ffa_mem_relinquish) +
2752 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002753 length) {
2754 dlog_verbose(
2755 "Endpoint count changed while copying to internal "
2756 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002757 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002758 goto out;
2759 }
2760
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002761 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2762 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002763
2764out:
2765 vm_unlock(&from_locked);
2766 return ret;
2767}
2768
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002769struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2770 ffa_memory_region_flags_t flags,
2771 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002772{
2773 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002774 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002775
Olivier Deprez55a189e2021-06-09 15:45:27 +02002776 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002777 struct vm_locked to_locked = vm_lock(to);
2778
Andrew Walbranca808b12020-05-15 17:22:28 +01002779 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002780 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002781
Andrew Walbran290b0c92020-02-03 16:37:14 +00002782 vm_unlock(&to_locked);
2783 } else {
2784 struct vm *from = vm_find(HF_TEE_VM_ID);
2785 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2786
Andrew Walbranca808b12020-05-15 17:22:28 +01002787 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2788 vm_to_from_lock.vm2, handle, flags,
2789 &api_page_pool);
2790
2791 vm_unlock(&vm_to_from_lock.vm1);
2792 vm_unlock(&vm_to_from_lock.vm2);
2793 }
2794
2795 return ret;
2796}
2797
2798struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2799 uint32_t fragment_offset,
2800 ffa_vm_id_t sender_vm_id,
2801 struct vcpu *current)
2802{
2803 struct vm *to = current->vm;
2804 struct vm_locked to_locked;
2805 struct ffa_value ret;
2806
2807 /* Sender ID MBZ at virtual instance. */
2808 if (sender_vm_id != 0) {
2809 return ffa_error(FFA_INVALID_PARAMETERS);
2810 }
2811
2812 to_locked = vm_lock(to);
2813
2814 if (msg_receiver_busy(to_locked, NULL, false)) {
2815 /*
2816 * Can't retrieve memory information if the mailbox is not
2817 * available.
2818 */
2819 dlog_verbose("RX buffer not ready.\n");
2820 ret = ffa_error(FFA_BUSY);
2821 goto out;
2822 }
2823
2824 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2825 &api_page_pool);
2826
2827out:
2828 vm_unlock(&to_locked);
2829 return ret;
2830}
2831
2832struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2833 uint32_t fragment_length,
2834 ffa_vm_id_t sender_vm_id,
2835 struct vcpu *current)
2836{
2837 struct vm *from = current->vm;
2838 const void *from_msg;
2839 void *fragment_copy;
2840 struct ffa_value ret;
2841
2842 /* Sender ID MBZ at virtual instance. */
2843 if (sender_vm_id != 0) {
2844 return ffa_error(FFA_INVALID_PARAMETERS);
2845 }
2846
2847 /*
2848 * Check that the sender has configured its send buffer. If the TX
2849 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2850 * be safely accessed after releasing the lock since the TX mailbox
2851 * address can only be configured once.
2852 */
2853 sl_lock(&from->lock);
2854 from_msg = from->mailbox.send;
2855 sl_unlock(&from->lock);
2856
2857 if (from_msg == NULL) {
2858 return ffa_error(FFA_INVALID_PARAMETERS);
2859 }
2860
2861 /*
2862 * Copy the fragment to a fresh page from the memory pool. This prevents
2863 * the sender from changing it underneath us, and also lets us keep it
2864 * around in the share state table if needed.
2865 */
2866 if (fragment_length > HF_MAILBOX_SIZE ||
2867 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2868 dlog_verbose(
2869 "Fragment length %d larger than mailbox size %d.\n",
2870 fragment_length, HF_MAILBOX_SIZE);
2871 return ffa_error(FFA_INVALID_PARAMETERS);
2872 }
2873 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2874 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2875 0) {
2876 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2877 return ffa_error(FFA_INVALID_PARAMETERS);
2878 }
2879 fragment_copy = mpool_alloc(&api_page_pool);
2880 if (fragment_copy == NULL) {
2881 dlog_verbose("Failed to allocate fragment copy.\n");
2882 return ffa_error(FFA_NO_MEMORY);
2883 }
2884 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2885
2886 /*
2887 * Hafnium doesn't support fragmentation of memory retrieve requests
2888 * (because it doesn't support caller-specified mappings, so a request
2889 * will never be larger than a single page), so this must be part of a
2890 * memory send (i.e. donate, lend or share) request.
2891 *
2892 * We can tell from the handle whether the memory transaction is for the
2893 * TEE or not.
2894 */
2895 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2896 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2897 struct vm_locked from_locked = vm_lock(from);
2898
2899 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2900 fragment_length, handle,
2901 &api_page_pool);
2902 /*
2903 * `ffa_memory_send_continue` takes ownership of the
2904 * fragment_copy, so we don't need to free it here.
2905 */
2906 vm_unlock(&from_locked);
2907 } else {
2908 struct vm *to = vm_find(HF_TEE_VM_ID);
2909 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2910
2911 /*
2912 * The TEE RX buffer state is checked in
2913 * `ffa_memory_tee_send_continue` rather than here, as we need
2914 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2915 * than FFA_ERROR FFA_BUSY in case it is busy.
2916 */
2917
2918 ret = ffa_memory_tee_send_continue(
2919 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2920 fragment_length, handle, &api_page_pool);
2921 /*
2922 * `ffa_memory_tee_send_continue` takes ownership of the
2923 * fragment_copy, so we don't need to free it here.
2924 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002925
2926 vm_unlock(&vm_to_from_lock.vm1);
2927 vm_unlock(&vm_to_from_lock.vm2);
2928 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002929
2930 return ret;
2931}
Max Shvetsov40108e72020-08-27 12:39:50 +01002932
Olivier Deprezd614d322021-06-18 15:21:00 +02002933/**
2934 * Register an entry point for a vCPU in warm boot cases.
2935 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
2936 */
Max Shvetsov40108e72020-08-27 12:39:50 +01002937struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2938 struct vcpu *current)
2939{
2940 struct vm_locked vm_locked;
Olivier Deprezd614d322021-06-18 15:21:00 +02002941 struct ffa_value ret = ffa_error(FFA_DENIED);
Max Shvetsov40108e72020-08-27 12:39:50 +01002942
Olivier Deprezd614d322021-06-18 15:21:00 +02002943 /*
2944 * Reject if interface is not supported at this FF-A instance
2945 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
2946 */
2947 if (!plat_ffa_is_secondary_ep_register_supported() ||
2948 current->vm->vcpu_count == 1) {
2949 return ffa_error(FFA_NOT_SUPPORTED);
2950 }
2951
2952 /*
2953 * No further check is made on the address validity
2954 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
2955 * from the VM or vCPU structure.
2956 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
2957 * For each SP [...] the Framework assumes that the same entry point
2958 * address is used for initializing any execution context during a
2959 * secondary cold boot.
2960 * If this function is invoked multiple times, then the entry point
2961 * address specified in the last valid invocation must be used by the
2962 * callee.
2963 */
Max Shvetsov40108e72020-08-27 12:39:50 +01002964 vm_locked = vm_lock(current->vm);
Olivier Deprezd614d322021-06-18 15:21:00 +02002965 if (vm_locked.vm->initialized) {
2966 goto out;
2967 }
2968
Max Shvetsov40108e72020-08-27 12:39:50 +01002969 vm_locked.vm->secondary_ep = entry_point;
Olivier Deprezd614d322021-06-18 15:21:00 +02002970
2971 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2972
2973out:
Max Shvetsov40108e72020-08-27 12:39:50 +01002974 vm_unlock(&vm_locked);
2975
Olivier Deprezd614d322021-06-18 15:21:00 +02002976 return ret;
Max Shvetsov40108e72020-08-27 12:39:50 +01002977}
J-Alvesa0f317d2021-06-09 13:31:59 +01002978
2979struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
2980 ffa_vcpu_count_t vcpu_count,
2981 struct vcpu *current)
2982{
2983 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2984 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
2985 vm_id);
2986 return ffa_error(FFA_NOT_SUPPORTED);
2987 }
2988
2989 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
2990}
2991
2992struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
2993 struct vcpu *current)
2994{
2995 /*
2996 * Validity of use of this interface is the same as for bitmap create.
2997 */
2998 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2999 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
3000 vm_id);
3001 return ffa_error(FFA_NOT_SUPPORTED);
3002 }
3003
3004 return plat_ffa_notifications_bitmap_destroy(vm_id);
3005}
J-Alvesc003a7a2021-03-18 13:06:53 +00003006
3007struct ffa_value api_ffa_notification_update_bindings(
3008 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3009 ffa_notifications_bitmap_t notifications, bool is_bind,
3010 struct vcpu *current)
3011{
3012 struct ffa_value ret = {.func = FFA_SUCCESS_32};
3013 struct vm_locked receiver_locked;
3014 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3015 const ffa_vm_id_t id_to_update =
3016 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
3017 const ffa_vm_id_t id_to_validate =
3018 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
3019
3020 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
3021 receiver_vm_id)) {
3022 dlog_verbose("Invalid use of notifications bind interface.\n");
3023 return ffa_error(FFA_INVALID_PARAMETERS);
3024 }
3025
J-Alvesb15e9402021-09-08 11:44:42 +01003026 if (plat_ffa_notifications_update_bindings_forward(
3027 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
3028 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01003029 return ret;
3030 }
3031
J-Alvesc003a7a2021-03-18 13:06:53 +00003032 if (notifications == 0U) {
3033 dlog_verbose("No notifications have been specified.\n");
3034 return ffa_error(FFA_INVALID_PARAMETERS);
3035 }
3036
3037 /**
3038 * This check assumes receiver is the current VM, and has been enforced
3039 * by 'plat_ffa_is_notifications_bind_valid'.
3040 */
3041 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3042
3043 if (receiver_locked.vm == NULL) {
3044 dlog_verbose("Receiver doesn't exist!\n");
3045 return ffa_error(FFA_DENIED);
3046 }
3047
J-Alves09ff9d82021-11-02 11:55:20 +00003048 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00003049 dlog_verbose("Notifications are not enabled.\n");
3050 ret = ffa_error(FFA_NOT_SUPPORTED);
3051 goto out;
3052 }
3053
3054 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
3055 vm_find(sender_vm_id) == NULL) {
3056 dlog_verbose("Sender VM does not exist!\n");
3057 ret = ffa_error(FFA_INVALID_PARAMETERS);
3058 goto out;
3059 }
3060
3061 /*
3062 * Can't bind/unbind notifications if at least one is bound to a
3063 * different sender.
3064 */
3065 if (!vm_notifications_validate_bound_sender(
3066 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3067 id_to_validate, notifications)) {
3068 dlog_verbose("Notifications are bound to other sender.\n");
3069 ret = ffa_error(FFA_DENIED);
3070 goto out;
3071 }
3072
3073 /**
3074 * Check if there is a pending notification within those specified in
3075 * the bitmap.
3076 */
3077 if (vm_are_notifications_pending(receiver_locked,
3078 plat_ffa_is_vm_id(sender_vm_id),
3079 notifications)) {
3080 dlog_verbose("Notifications within '%x' pending.\n",
3081 notifications);
3082 ret = ffa_error(FFA_DENIED);
3083 goto out;
3084 }
3085
3086 vm_notifications_update_bindings(
3087 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
3088 notifications, is_per_vcpu && is_bind);
3089
3090out:
3091 vm_unlock(&receiver_locked);
3092 return ret;
3093}
J-Alvesaa79c012021-07-09 14:29:45 +01003094
3095struct ffa_value api_ffa_notification_set(
3096 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3097 ffa_notifications_bitmap_t notifications, struct vcpu *current)
3098{
3099 struct ffa_value ret;
3100 struct vm_locked receiver_locked;
3101
3102 /*
3103 * Check if is per-vCPU or global, and extracting vCPU ID according
3104 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
3105 */
3106 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3107 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
3108
J-Alvesaa79c012021-07-09 14:29:45 +01003109 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
3110 receiver_vm_id)) {
3111 dlog_verbose("Invalid use of notifications set interface.\n");
3112 return ffa_error(FFA_INVALID_PARAMETERS);
3113 }
3114
3115 if (notifications == 0U) {
3116 dlog_verbose("No notifications have been specified.\n");
3117 return ffa_error(FFA_INVALID_PARAMETERS);
3118 }
3119
J-Alvesde7bd2f2021-09-09 19:54:35 +01003120 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
3121 flags, notifications, &ret)) {
3122 return ret;
3123 }
3124
J-Alvesaa79c012021-07-09 14:29:45 +01003125 /*
3126 * This check assumes receiver is the current VM, and has been enforced
3127 * by 'plat_ffa_is_notification_set_valid'.
3128 */
3129 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3130
3131 if (receiver_locked.vm == NULL) {
3132 dlog_verbose("Receiver ID is not valid.\n");
3133 return ffa_error(FFA_INVALID_PARAMETERS);
3134 }
3135
J-Alves09ff9d82021-11-02 11:55:20 +00003136 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003137 dlog_verbose("Receiver's notifications not enabled.\n");
3138 ret = ffa_error(FFA_DENIED);
3139 goto out;
3140 }
3141
3142 /*
3143 * If notifications are not bound to the sender, they wouldn't be
3144 * enabled either for the receiver.
3145 */
3146 if (!vm_notifications_validate_binding(
3147 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3148 sender_vm_id, notifications, is_per_vcpu)) {
3149 dlog_verbose("Notifications bindings not valid.\n");
3150 ret = ffa_error(FFA_DENIED);
3151 goto out;
3152 }
3153
3154 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3155 dlog_verbose("Invalid VCPU ID!\n");
3156 ret = ffa_error(FFA_INVALID_PARAMETERS);
3157 goto out;
3158 }
3159
J-Alves7461ef22021-10-18 17:21:33 +01003160 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00003161 vm_notifications_partition_set_pending(
3162 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), notifications,
3163 vcpu_id, is_per_vcpu);
3164
J-Alvesaa79c012021-07-09 14:29:45 +01003165 dlog_verbose("Set the notifications: %x.\n", notifications);
3166
J-Alves13394022021-06-30 13:48:49 +01003167 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3168 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3169 vcpu_index(current));
3170 plat_ffa_sri_trigger_not_delayed(current->cpu);
3171 } else {
3172 plat_ffa_sri_state_set(DELAYED);
3173 }
J-Alvesaa79c012021-07-09 14:29:45 +01003174
J-Alves13394022021-06-30 13:48:49 +01003175 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003176out:
3177 vm_unlock(&receiver_locked);
3178
3179 return ret;
3180}
3181
3182static struct ffa_value api_ffa_notification_get_success_return(
3183 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3184 ffa_notifications_bitmap_t from_framework)
3185{
3186 return (struct ffa_value){
3187 .func = FFA_SUCCESS_32,
3188 .arg1 = 0U,
3189 .arg2 = (uint32_t)from_sp,
3190 .arg3 = (uint32_t)(from_sp >> 32),
3191 .arg4 = (uint32_t)from_vm,
3192 .arg5 = (uint32_t)(from_vm >> 32),
3193 .arg6 = (uint32_t)from_framework,
3194 .arg7 = (uint32_t)(from_framework >> 32),
3195 };
3196}
3197
3198struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3199 ffa_vcpu_index_t vcpu_id,
3200 uint32_t flags, struct vcpu *current)
3201{
J-Alves663682a2022-03-25 13:56:51 +00003202 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01003203 ffa_notifications_bitmap_t sp_notifications = 0;
3204 ffa_notifications_bitmap_t vm_notifications = 0;
3205 struct vm_locked receiver_locked;
3206 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01003207 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
3208 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
3209 FFA_NOTIFICATION_FLAG_BITMAP_SP |
3210 FFA_NOTIFICATION_FLAG_BITMAP_VM);
3211
3212 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
3213 if ((flags & flags_mbz) != 0U) {
3214 dlog_verbose(
3215 "Invalid flags bit(s) set in notifications get. [31:4] "
3216 "MBZ(%x)\n",
3217 flags);
3218 return ffa_error(FFA_INVALID_PARAMETERS);
3219 }
J-Alvesaa79c012021-07-09 14:29:45 +01003220
3221 /*
J-Alvesfc95a302022-04-22 14:18:23 +01003222 * Following check should capture wrong uses of the interface,
3223 * depending on whether Hafnium is SPMC or hypervisor. On the
3224 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01003225 */
J-Alvesfc95a302022-04-22 14:18:23 +01003226 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
3227 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003228 dlog_verbose("Invalid use of notifications get interface.\n");
3229 return ffa_error(FFA_INVALID_PARAMETERS);
3230 }
3231
3232 /*
3233 * This check assumes receiver is the current VM, and has been enforced
3234 * by `plat_ffa_is_notifications_get_valid`.
3235 */
3236 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3237
3238 /*
3239 * `plat_ffa_is_notifications_get_valid` ensures following is never
3240 * true.
3241 */
3242 CHECK(receiver_locked.vm != NULL);
3243
3244 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3245 (receiver_locked.vm->vcpu_count != 1 &&
3246 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003247 dlog_verbose(
3248 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3249 vcpu_id, receiver_locked.vm->vcpu_count,
3250 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003251 ret = ffa_error(FFA_INVALID_PARAMETERS);
3252 goto out;
3253 }
3254
3255 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003256 if (!plat_ffa_notifications_get_from_sp(
3257 receiver_locked, vcpu_id, &sp_notifications,
3258 &ret)) {
3259 dlog_verbose("Failed to get notifications from sps.");
3260 goto out;
3261 }
J-Alvesaa79c012021-07-09 14:29:45 +01003262 }
3263
3264 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00003265 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01003266 receiver_locked, true, vcpu_id);
3267 }
3268
J-Alvesd605a092022-03-28 14:20:48 +01003269 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
3270 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
3271 if (!plat_ffa_notifications_get_framework_notifications(
3272 receiver_locked, &framework_notifications, flags,
3273 vcpu_id, &ret)) {
3274 dlog_verbose(
3275 "Failed to get notifications from "
3276 "framework.\n");
3277 goto out;
3278 }
3279 }
3280
J-Alves663682a2022-03-25 13:56:51 +00003281 ret = api_ffa_notification_get_success_return(
3282 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01003283
J-Alvesfe23ebe2021-10-13 16:07:07 +01003284 /*
3285 * If there are no more pending notifications, change `sri_state` to
3286 * handled.
3287 */
3288 if (vm_is_notifications_pending_count_zero()) {
3289 plat_ffa_sri_state_set(HANDLED);
3290 }
3291
J-Alves6e2abc62021-12-02 14:58:56 +00003292 if (!receiver_locked.vm->el0_partition &&
3293 !vm_are_global_notifications_pending(receiver_locked)) {
3294 vm_notifications_set_npi_injected(receiver_locked, false);
3295 }
3296
J-Alvesaa79c012021-07-09 14:29:45 +01003297out:
3298 vm_unlock(&receiver_locked);
3299
3300 return ret;
3301}
J-Alvesc8e8a222021-06-08 17:33:52 +01003302
3303/**
3304 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3305 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3306 */
3307static struct ffa_value api_ffa_notification_info_get_success_return(
3308 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003309 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003310{
3311 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3312
3313 /*
3314 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3315 * hold the list of ids.
3316 */
3317 memcpy_s(&ret.arg3,
3318 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3319 sizeof(ids[0]) * ids_count);
3320
3321 /*
3322 * According to the spec x2 should have:
3323 * - Bit flagging if there are more notifications pending;
3324 * - The total number of elements (i.e. total list size);
3325 * - The number of VCPU IDs within each VM specific list.
3326 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003327 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3328 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3329 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003330
3331 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3332 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3333
3334 for (unsigned int i = 0; i < lists_count; i++) {
3335 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3336 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3337 }
3338
3339 return ret;
3340}
3341
3342struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3343{
3344 /*
3345 * Following set of variables should be populated with the return info.
3346 * At a successfull handling of this interface, they should be used
3347 * to populate the 'ret' structure in accordance to the table 17.29
3348 * of the FF-A v1.1 Beta0 specification.
3349 */
3350 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3351 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3352 uint32_t lists_count = 0;
3353 uint32_t ids_count = 0;
3354 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003355 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003356
3357 /*
3358 * This interface can only be called at NS virtual/physical FF-A
3359 * instance by the endpoint implementing the primary scheduler and the
3360 * Hypervisor/OS kernel.
3361 * In the SPM, following check passes if call has been forwarded from
3362 * the hypervisor.
3363 */
3364 if (current->vm->id != HF_PRIMARY_VM_ID) {
3365 dlog_verbose(
3366 "Only the receiver's scheduler can use this "
3367 "interface\n");
3368 return ffa_error(FFA_NOT_SUPPORTED);
3369 }
3370
J-Alvesca058c22021-09-10 14:02:07 +01003371 /*
3372 * Forward call to the other world, and fill the arrays used to assemble
3373 * return.
3374 */
3375 plat_ffa_notification_info_get_forward(
3376 ids, &ids_count, lists_sizes, &lists_count,
3377 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3378
3379 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3380
J-Alvesc8e8a222021-06-08 17:33:52 +01003381 /* Get notifications' info from this world */
3382 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3383 ++index) {
3384 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3385
3386 list_is_full = vm_notifications_info_get(
3387 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3388 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3389
3390 vm_unlock(&vm_locked);
3391 }
3392
3393 if (!list_is_full) {
3394 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003395 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003396 ids, &ids_count, lists_sizes, &lists_count,
3397 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3398 }
3399
3400 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003401 dlog_verbose(
3402 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003403 result = ffa_error(FFA_NO_DATA);
3404 } else {
3405 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003406 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003407 }
3408
J-Alvesfe23ebe2021-10-13 16:07:07 +01003409 plat_ffa_sri_state_set(HANDLED);
3410
J-Alves13394022021-06-30 13:48:49 +01003411 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003412}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003413
3414struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3415{
3416 struct vm_locked vm_locked;
3417 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3418 bool mode_ret = false;
3419 uint32_t mode = 0;
3420
3421 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3422 return ffa_error(FFA_NOT_SUPPORTED);
3423 }
3424
3425 if (!(current->vm->el0_partition)) {
3426 return ffa_error(FFA_DENIED);
3427 }
3428
3429 vm_locked = vm_lock(current->vm);
3430
3431 /*
3432 * mm_get_mode is used to check if the given base_addr page is already
3433 * mapped. If the page is unmapped, return error. If the page is mapped
3434 * appropriate attributes are returned to the caller. Note that
3435 * mm_get_mode returns true if the address is in the valid VA range as
3436 * supported by the architecture and MMU configurations, as opposed to
3437 * whether a page is mapped or not. For a page to be known as mapped,
3438 * the API must return true AND the returned mode must not have
3439 * MM_MODE_INVALID set.
3440 */
3441 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3442 va_add(base_addr, PAGE_SIZE), &mode);
3443 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3444 ret = ffa_error(FFA_INVALID_PARAMETERS);
3445 goto out;
3446 }
3447
3448 /* No memory should be marked RWX */
3449 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3450 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3451
3452 /*
3453 * S-EL0 partitions are expected to have all their pages marked as
3454 * non-global.
3455 */
3456 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3457 (MM_MODE_NG | MM_MODE_USER));
3458
3459 if (mode & MM_MODE_W) {
3460 /* No memory should be writeable but not readable. */
3461 CHECK(mode & MM_MODE_R);
3462 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3463 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3464 } else if (mode & MM_MODE_R) {
3465 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3466 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3467 if (!(mode & MM_MODE_X)) {
3468 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3469 }
3470 }
3471out:
3472 vm_unlock(&vm_locked);
3473 return ret;
3474}
3475
3476struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3477 uint32_t mem_perm, struct vcpu *current)
3478{
3479 struct vm_locked vm_locked;
3480 struct ffa_value ret;
3481 bool mode_ret = false;
3482 uint32_t original_mode;
3483 uint32_t new_mode;
3484 struct mpool local_page_pool;
3485
3486 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3487 return ffa_error(FFA_NOT_SUPPORTED);
3488 }
3489
3490 if (!(current->vm->el0_partition)) {
3491 return ffa_error(FFA_DENIED);
3492 }
3493
3494 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3495 return ffa_error(FFA_INVALID_PARAMETERS);
3496 }
3497
3498 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3499 (mem_perm != FFA_MEM_PERM_RX)) {
3500 return ffa_error(FFA_INVALID_PARAMETERS);
3501 }
3502
3503 /*
3504 * Create a local pool so any freed memory can't be used by another
3505 * thread. This is to ensure the original mapping can be restored if any
3506 * stage of the process fails.
3507 */
3508 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3509
3510 vm_locked = vm_lock(current->vm);
3511
3512 /*
3513 * All regions accessible by the partition are mapped during boot. If we
3514 * cannot get a successful translation for the page range, the request
3515 * to change permissions is rejected.
3516 * mm_get_mode is used to check if the given address range is already
3517 * mapped. If the range is unmapped, return error. If the range is
3518 * mapped appropriate attributes are returned to the caller. Note that
3519 * mm_get_mode returns true if the address is in the valid VA range as
3520 * supported by the architecture and MMU configurations, as opposed to
3521 * whether a page is mapped or not. For a page to be known as mapped,
3522 * the API must return true AND the returned mode must not have
3523 * MM_MODE_INVALID set.
3524 */
3525
3526 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3527 va_add(base_addr, page_count * PAGE_SIZE),
3528 &original_mode);
3529 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3530 ret = ffa_error(FFA_INVALID_PARAMETERS);
3531 goto out;
3532 }
3533
3534 /* Device memory cannot be marked as executable */
3535 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3536 ret = ffa_error(FFA_INVALID_PARAMETERS);
3537 goto out;
3538 }
3539
3540 new_mode = MM_MODE_USER | MM_MODE_NG;
3541
3542 if (mem_perm == FFA_MEM_PERM_RW) {
3543 new_mode |= MM_MODE_R | MM_MODE_W;
3544 } else if (mem_perm == FFA_MEM_PERM_RX) {
3545 new_mode |= MM_MODE_R | MM_MODE_X;
3546 } else if (mem_perm == FFA_MEM_PERM_RO) {
3547 new_mode |= MM_MODE_R;
3548 }
3549
3550 /*
3551 * Safe to re-map memory, since we know the requested permissions are
3552 * valid, and the memory requested to be re-mapped is also valid.
3553 */
3554 if (!mm_identity_prepare(
3555 &vm_locked.vm->ptable, pa_from_va(base_addr),
3556 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3557 new_mode, &local_page_pool)) {
3558 /*
3559 * Defrag the table into the local page pool.
3560 * mm_identity_prepare could have allocated or freed pages to
3561 * split blocks or tables etc.
3562 */
3563 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3564
3565 /*
3566 * Guaranteed to succeed mapping with old mode since the mapping
3567 * with old mode already existed and we have a local page pool
3568 * that should have sufficient memory to go back to the original
3569 * state.
3570 */
3571 CHECK(mm_identity_prepare(
3572 &vm_locked.vm->ptable, pa_from_va(base_addr),
3573 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3574 original_mode, &local_page_pool));
3575 mm_identity_commit(
3576 &vm_locked.vm->ptable, pa_from_va(base_addr),
3577 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3578 original_mode, &local_page_pool);
3579
3580 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3581 ret = ffa_error(FFA_NO_MEMORY);
3582 goto out;
3583 }
3584
3585 mm_identity_commit(
3586 &vm_locked.vm->ptable, pa_from_va(base_addr),
3587 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3588 &local_page_pool);
3589
3590 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3591
3592out:
3593 mpool_fini(&local_page_pool);
3594 vm_unlock(&vm_locked);
3595
3596 return ret;
3597}
Maksims Svecovs71b76702022-05-20 15:32:58 +01003598
3599/**
3600 * Helper function for FFA_CONSOLE_LOG ABI.
3601 * Writes number of characters to a given VM buffer.
3602 */
3603static rsize_t arg_to_char_helper(struct vm_locked from_locked,
3604 const uint64_t src, rsize_t src_size,
3605 rsize_t to_write)
3606{
3607 bool flush = false;
3608 char c;
3609 rsize_t size = src_size < to_write ? src_size : to_write;
3610 rsize_t written = 0;
3611
3612 if (size == 0) {
3613 return 0;
3614 }
3615
3616 while (written < size) {
3617 c = ((char *)&src)[written++];
3618 if (c == '\n' || c == '\0') {
3619 flush = true;
3620 } else {
3621 from_locked.vm->log_buffer
3622 [from_locked.vm->log_buffer_length++] = c;
3623 flush = (from_locked.vm->log_buffer_length ==
3624 LOG_BUFFER_SIZE);
3625 }
3626
3627 if (flush) {
3628 dlog_flush_vm_buffer(from_locked.vm->id,
3629 from_locked.vm->log_buffer,
3630 from_locked.vm->log_buffer_length);
3631 from_locked.vm->log_buffer_length = 0;
3632 }
3633 }
3634
3635 return written;
3636}
3637
3638/**
3639 * Implements FFA_CONSOLE_LOG buffered logging.
3640 */
3641struct ffa_value api_ffa_console_log(const struct ffa_value args,
3642 struct vcpu *current)
3643{
3644 struct vm *vm = current->vm;
3645 struct vm_locked vm_locked;
3646 size_t chars_in_param = args.func == FFA_CONSOLE_LOG_32
3647 ? sizeof(uint32_t)
3648 : sizeof(uint64_t);
3649 size_t total_to_write = args.arg1;
3650
3651 if (total_to_write == 0 || total_to_write > chars_in_param * 6) {
3652 return ffa_error(FFA_INVALID_PARAMETERS);
3653 }
3654
3655 vm_locked = vm_lock(vm);
3656
3657 total_to_write -= arg_to_char_helper(vm_locked, args.arg2,
3658 chars_in_param, total_to_write);
3659 total_to_write -= arg_to_char_helper(vm_locked, args.arg3,
3660 chars_in_param, total_to_write);
3661 total_to_write -= arg_to_char_helper(vm_locked, args.arg4,
3662 chars_in_param, total_to_write);
3663 total_to_write -= arg_to_char_helper(vm_locked, args.arg5,
3664 chars_in_param, total_to_write);
3665 total_to_write -= arg_to_char_helper(vm_locked, args.arg6,
3666 chars_in_param, total_to_write);
3667 arg_to_char_helper(vm_locked, args.arg7, chars_in_param,
3668 total_to_write);
3669
3670 vm_unlock(&vm_locked);
3671
3672 return (struct ffa_value){.func = FFA_SUCCESS_32};
3673}