blob: 20bdd1795e44a6ac7e648526fc9fc101f781e583 [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;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000610 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000611 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
612 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000613 int64_t ret = 0;
614
Andrew Walbran508e63c2018-12-20 17:02:37 +0000615 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000616 * We only need to change state and (maybe) trigger a virtual interrupt
617 * if it is enabled and was not previously pending. Otherwise we can
618 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000619 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000620 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
621 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000622 intid_mask)) {
623 goto out;
624 }
625
626 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000627 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
628 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
629 vcpu_irq_count_increment(target_locked);
630 } else {
631 vcpu_fiq_count_increment(target_locked);
632 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000633
634 /*
635 * Only need to update state if there was not already an
636 * interrupt enabled and pending.
637 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000638 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000639 goto out;
640 }
641
Andrew Walbran508e63c2018-12-20 17:02:37 +0000642 if (current->vm->id == HF_PRIMARY_VM_ID) {
643 /*
644 * If the call came from the primary VM, let it know that it
645 * should run or kick the target vCPU.
646 */
647 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000648 } else if (current != target_vcpu && next != NULL) {
649 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000650 }
651
652out:
653 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000654 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000655
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200656 return ret;
657}
658
659/* Wrapper to internal_interrupt_inject with locking of target vCPU */
660static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
661 uint32_t intid, struct vcpu *current,
662 struct vcpu **next)
663{
664 int64_t ret;
665 struct vcpu_locked target_locked;
666
667 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100668 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200669 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000670
671 return ret;
672}
673
674/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100675 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
676 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100677 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100678static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100679{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000680 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100681 case FFA_MSG_SEND_32:
682 return (struct ffa_value){
683 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000684 .arg1 = (receiver->mailbox.recv_sender << 16) |
685 receiver->id,
686 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000687 default:
688 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000689 dlog_error("Tried to return an invalid message function %#x\n",
690 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100691 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000692 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100693}
694
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600695struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
696 struct ffa_value *args)
697{
698 struct ffa_value ret;
699
700 if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U ||
701 args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
702 args->arg7 != 0U) {
703 return ffa_error(FFA_INVALID_PARAMETERS);
704 }
705
706 if (plat_ffa_msg_wait_prepare(current, next, &ret)) {
707 return ret;
708 }
709
710 return api_ffa_msg_recv(true, current, next);
711}
712
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100713/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000714 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000715 * value needs to be forced onto the vCPU.
716 */
J-Alves6e2abc62021-12-02 14:58:56 +0000717static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100718 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000719{
Max Shvetsov40108e72020-08-27 12:39:50 +0100720 struct vcpu_locked vcpu_locked;
721 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000722 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500723 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
J-Alves6e2abc62021-12-02 14:58:56 +0000724 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000725
Andrew Scullb06d1752019-02-04 10:15:48 +0000726 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000727 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000728 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100729 * The VM lock is not needed in the common case so it must only be taken
730 * when it is going to be needed. This ensures there are no inter-vCPU
731 * dependencies in the common run case meaning the sensitive context
732 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000733 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100734 vcpu_locked = vcpu_lock(vcpu);
735
736#if SECURE_WORLD == 1
J-Alves7ac49052022-02-08 17:20:53 +0000737 bool is_vcpu_reset_and_start = vcpu_secondary_reset_and_start(
738 vcpu_locked, vcpu->vm->secondary_ep, 0);
739 if (is_vcpu_reset_and_start) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100740 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
741 __func__, vcpu->vm->id, current->cpu->id);
742 }
743
744#endif
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000745 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000746 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
747 (!vcpu->vm->el0_partition &&
748 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
749 vcpu->state == VCPU_STATE_BLOCKED ||
750 vcpu->state == VCPU_STATE_PREEMPTED));
751
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000752 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100753 vcpu_unlock(&vcpu_locked);
754 vm_locked = vm_lock(vcpu->vm);
755 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000756 }
757
758 /*
759 * If the vCPU is already running somewhere then we can't run it here
760 * simultaneously. While it is actually running then the state should be
761 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
762 * stops running but while Hafnium is in the process of switching back
763 * to the primary there will be a brief period while the state has been
764 * updated but `regs_available` is still false (until
765 * `api_regs_state_saved` is called). We can't start running it again
766 * until this has finished, so count this state as still running for the
767 * purposes of this check.
768 */
769 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
770 /*
771 * vCPU is running on another pCPU.
772 *
773 * It's okay not to return the sleep duration here because the
774 * other physical CPU that is currently running this vCPU will
775 * return the sleep duration if needed.
776 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100777 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000778 ret = false;
779 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000780 }
Andrew Scull9726c252019-01-23 13:44:19 +0000781
782 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100783 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100784 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000785 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100786 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000787 }
788 ret = false;
789 goto out;
790 }
791
Andrew Walbran508e63c2018-12-20 17:02:37 +0000792 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100793 case VCPU_STATE_RUNNING:
794 case VCPU_STATE_OFF:
795 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000796 ret = false;
797 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000798
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500799 case VCPU_STATE_WAITING:
800 /*
801 * An initial FFA_RUN is necessary for secondary VM/SP to reach
802 * the message wait loop.
803 */
804 if (!vcpu->is_bootstrapped) {
805 vcpu->is_bootstrapped = true;
806 break;
807 }
808
J-Alves6e2abc62021-12-02 14:58:56 +0000809 assert(need_vm_lock == true);
810 if (!vm_locked.vm->el0_partition &&
811 plat_ffa_inject_notification_pending_interrupt(
812 vcpu_locked, current, vm_locked)) {
813 break;
814 }
815
Andrew Scullb06d1752019-02-04 10:15:48 +0000816 /*
817 * A pending message allows the vCPU to run so the message can
818 * be delivered directly.
819 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100820 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100821 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100822 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100823 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000824 break;
825 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500826
827 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
828 break;
829 }
830
831 if (arch_timer_enabled(&vcpu->regs)) {
832 timer_remaining_ns =
833 arch_timer_remaining_ns(&vcpu->regs);
834 if (timer_remaining_ns == 0) {
835 break;
836 }
837 } else {
838 dlog_verbose("Timer disabled\n");
839 }
840 run_ret->func = FFA_MSG_WAIT_32;
841 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
842 run_ret->arg2 = timer_remaining_ns;
843 ret = false;
844 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100845 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +0000846 if (need_vm_lock &&
847 plat_ffa_inject_notification_pending_interrupt(
848 vcpu_locked, current, vm_locked)) {
849 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
850 break;
851 }
852
Andrew Scullb06d1752019-02-04 10:15:48 +0000853 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000854 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000855 break;
856 }
857
Andrew Walbran508e63c2018-12-20 17:02:37 +0000858 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100859 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000860 arch_timer_remaining_ns(&vcpu->regs);
861
862 /*
863 * The timer expired so allow the interrupt to be
864 * delivered.
865 */
866 if (timer_remaining_ns == 0) {
867 break;
868 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100869 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000870
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100871 /*
872 * The vCPU is not ready to run, return the appropriate code to
873 * the primary which called vcpu_run.
874 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500875 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100876 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
877 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000878
879 ret = false;
880 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000881
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500882 case VCPU_STATE_BLOCKED:
883 /* A blocked vCPU is run unconditionally. Fall through. */
884 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +0000885 /* Check NPI is to be injected here. */
886 if (need_vm_lock) {
887 plat_ffa_inject_notification_pending_interrupt(
888 vcpu_locked, current, vm_locked);
889 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000890 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500891 default:
892 /*
893 * Execution not expected to reach here. Deny the request
894 * gracefully.
895 */
896 *run_ret = ffa_error(FFA_DENIED);
897 ret = false;
898 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000899 }
900
Andrew Scullb06d1752019-02-04 10:15:48 +0000901 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000902 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100903 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000904
J-Alves7ac49052022-02-08 17:20:53 +0000905#if SECURE_WORLD == 1
906 /* Set the designated GP register with the vCPU ID. */
907 if (is_vcpu_reset_and_start) {
908 vcpu_set_phys_core_idx(vcpu_locked.vcpu);
909 }
910#endif
911
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000912 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000913 * Mark the registers as unavailable now that we're about to reflect
914 * them onto the real registers. This will also prevent another physical
915 * CPU from trying to read these registers.
916 */
917 vcpu->regs_available = false;
918
919 ret = true;
920
921out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100922 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000923 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100924 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000925 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000926 return ret;
927}
928
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100929struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500930 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100931{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100932 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100933 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100934 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100935
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -0800936 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500937 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100938 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100939
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700940 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
941 return ret;
942 }
943
Andrew Scull19503262018-09-20 14:48:39 +0100944 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100945 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100946 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100947 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100948 }
949
Fuad Tabbaed294af2019-12-20 10:43:01 +0000950 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100951 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100952 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100953 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100954
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000955 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100956 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000957 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000958 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100959 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000960
Andrew Walbran508e63c2018-12-20 17:02:37 +0000961 /*
962 * Inject timer interrupt if timer has expired. It's safe to access
963 * vcpu->regs here because api_vcpu_prepare_run already made sure that
964 * regs_available was true (and then set it to false) before returning
965 * true.
966 */
967 if (arch_timer_pending(&vcpu->regs)) {
968 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100969 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
970 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000971
972 /*
973 * Set the mask bit so the hardware interrupt doesn't fire
974 * again. Ideally we wouldn't do this because it affects what
975 * the secondary vCPU sees, but if we don't then we end up with
976 * a loop of the interrupt firing each time we try to return to
977 * the secondary vCPU.
978 */
979 arch_timer_mask(&vcpu->regs);
980 }
981
Fuad Tabbaed294af2019-12-20 10:43:01 +0000982 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000983 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000984
Andrew Scull33fecd32019-01-08 14:48:27 +0000985 /*
986 * Set a placeholder return code to the scheduler. This will be
987 * overwritten when the switch back to the primary occurs.
988 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100989 ret.func = FFA_INTERRUPT_32;
Andrew Walbran7e824602020-10-22 16:51:40 +0100990 ret.arg1 = 0;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100991 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000992
Andrew Scull6d2db332018-10-10 15:28:17 +0100993out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100994 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100995}
996
997/**
Andrew Scull81e85092018-12-12 12:56:20 +0000998 * Check that the mode indicates memory that is valid, owned and exclusive.
999 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001000static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001001{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001002 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1003 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001004}
1005
1006/**
Andrew Walbranc8a01972020-09-22 11:23:30 +01001007 * Determines the value to be returned by api_ffa_rxtx_map and
1008 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
1009 * there are waiters, it also switches back to the primary VM for it to wake
1010 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001011 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001012static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
1013 struct vcpu *current,
1014 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001015{
1016 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001017
1018 if (list_empty(&vm->mailbox.waiter_list)) {
1019 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001020 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001021 }
1022
1023 if (vm->id == HF_PRIMARY_VM_ID) {
1024 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001025 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001026 }
1027
1028 /*
1029 * Switch back to the primary VM, informing it that there are waiters
1030 * that need to be notified.
1031 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001032 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001034 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001035
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001036 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001037}
1038
1039/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001040 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001041 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001042static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1043 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001044 paddr_t pa_send_begin, paddr_t pa_send_end,
1045 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001046 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001047 struct mpool *local_page_pool)
1048{
1049 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001050
1051 /* Map the send page as read-only in the hypervisor address space. */
1052 vm_locked.vm->mailbox.send =
1053 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001054 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001055 if (!vm_locked.vm->mailbox.send) {
1056 /* TODO: partial defrag of failed range. */
1057 /* Recover any memory consumed in failed mapping. */
1058 mm_defrag(mm_stage1_locked, local_page_pool);
1059 goto fail;
1060 }
1061
1062 /*
1063 * Map the receive page as writable in the hypervisor address space. On
1064 * failure, unmap the send page before returning.
1065 */
1066 vm_locked.vm->mailbox.recv =
1067 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001068 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001069 if (!vm_locked.vm->mailbox.recv) {
1070 /* TODO: partial defrag of failed range. */
1071 /* Recover any memory consumed in failed mapping. */
1072 mm_defrag(mm_stage1_locked, local_page_pool);
1073 goto fail_undo_send;
1074 }
1075
1076 ret = true;
1077 goto out;
1078
1079 /*
1080 * The following mappings will not require more memory than is available
1081 * in the local pool.
1082 */
1083fail_undo_send:
1084 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001085 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1086 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001087
1088fail:
1089 ret = false;
1090
1091out:
Andrew Sculle1322792019-07-01 17:46:10 +01001092 return ret;
1093}
1094
1095/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001096 * Sanity checks and configures the send and receive pages in the VM stage-2
1097 * and hypervisor stage-1 page tables.
1098 *
1099 * Returns:
1100 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001101 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001102 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1103 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001104 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001105 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001106 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001107
1108struct ffa_value api_vm_configure_pages(
1109 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1110 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1111 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001112{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001113 struct ffa_value ret;
1114 paddr_t pa_send_begin;
1115 paddr_t pa_send_end;
1116 paddr_t pa_recv_begin;
1117 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001118 uint32_t orig_send_mode = 0;
1119 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001120 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001121
1122 /* We only allow these to be setup once. */
1123 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1124 ret = ffa_error(FFA_DENIED);
1125 goto out;
1126 }
1127
1128 /* Hafnium only supports a fixed size of RX/TX buffers. */
1129 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1130 ret = ffa_error(FFA_INVALID_PARAMETERS);
1131 goto out;
1132 }
1133
1134 /* Fail if addresses are not page-aligned. */
1135 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1136 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1137 ret = ffa_error(FFA_INVALID_PARAMETERS);
1138 goto out;
1139 }
1140
1141 /* Convert to physical addresses. */
1142 pa_send_begin = pa_from_ipa(send);
1143 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1144 pa_recv_begin = pa_from_ipa(recv);
1145 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1146
1147 /* Fail if the same page is used for the send and receive pages. */
1148 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1149 ret = ffa_error(FFA_INVALID_PARAMETERS);
1150 goto out;
1151 }
Andrew Sculle1322792019-07-01 17:46:10 +01001152
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001153 /* Set stage 2 translation tables only for virtual FF-A instances. */
1154 if (vm_id_is_current_world(vm_locked.vm->id)) {
1155 /*
1156 * Ensure the pages are valid, owned and exclusive to the VM and
1157 * that the VM has the required access to the memory.
1158 */
1159 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1160 &orig_send_mode) ||
1161 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1162 (orig_send_mode & MM_MODE_R) == 0 ||
1163 (orig_send_mode & MM_MODE_W) == 0) {
1164 dlog_error(
1165 "VM doesn't have required access rights to map "
1166 "TX buffer in stage 2.\n");
1167 ret = ffa_error(FFA_INVALID_PARAMETERS);
1168 goto out;
1169 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001170
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001171 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1172 &orig_recv_mode) ||
1173 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1174 (orig_recv_mode & MM_MODE_R) == 0) {
1175 dlog_error(
1176 "VM doesn't have required access rights to map "
1177 "RX buffer in stage 2.\n");
1178 ret = ffa_error(FFA_INVALID_PARAMETERS);
1179 goto out;
1180 }
Andrew Sculle1322792019-07-01 17:46:10 +01001181
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001182 /* Take memory ownership away from the VM and mark as shared. */
1183 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1184 MM_MODE_W;
1185 if (vm_locked.vm->el0_partition) {
1186 mode |= MM_MODE_USER | MM_MODE_NG;
1187 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001188
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001189 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1190 mode, local_page_pool, NULL)) {
1191 dlog_error(
1192 "Cannot allocate a new entry in stage 2 "
1193 "translation table.\n");
1194 ret = ffa_error(FFA_NO_MEMORY);
1195 goto out;
1196 }
Andrew Sculle1322792019-07-01 17:46:10 +01001197
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001198 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1199 if (vm_locked.vm->el0_partition) {
1200 mode |= MM_MODE_USER | MM_MODE_NG;
1201 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001202
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001203 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1204 mode, local_page_pool, NULL)) {
1205 /* TODO: partial defrag of failed range. */
1206 /* Recover any memory consumed in failed mapping. */
1207 vm_ptable_defrag(vm_locked, local_page_pool);
1208 goto fail_undo_send;
1209 }
Andrew Sculle1322792019-07-01 17:46:10 +01001210 }
1211
Olivier Deprez96a2a262020-06-11 17:21:38 +02001212 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1213 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1214
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001215 /*
1216 * For EL0 partitions, since both the partition and the hypervisor code
1217 * use the EL2&0 translation regime, it is critical to mark the mappings
1218 * of the send and recv buffers as non-global in the TLB. For one, if we
1219 * dont mark it as non-global, it would cause TLB conflicts since there
1220 * would be an identity mapping with non-global attribute in the
1221 * partitions page tables, but another identity mapping in the
1222 * hypervisor page tables with the global attribute. The other issue is
1223 * one of security, we dont want other partitions to be able to access
1224 * other partitions buffers through cached translations.
1225 */
1226 if (vm_locked.vm->el0_partition) {
1227 extra_attributes |= MM_MODE_NG;
1228 }
1229
Manish Pandeyd34f8892020-06-19 17:41:07 +01001230 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1231 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001232 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001233 goto fail_undo_send_and_recv;
1234 }
1235
Manish Pandeyd34f8892020-06-19 17:41:07 +01001236 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001237 goto out;
1238
Andrew Sculle1322792019-07-01 17:46:10 +01001239fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001240 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001241 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001242
1243fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001244 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001245 orig_send_mode, local_page_pool, NULL));
1246 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001247
1248out:
Andrew Sculle1322792019-07-01 17:46:10 +01001249 return ret;
1250}
1251
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001252static void api_get_rxtx_description(struct vm_locked vm_locked, ipaddr_t *send,
1253 ipaddr_t *recv, uint32_t *page_count,
1254 ffa_vm_id_t *owner_vm_id)
1255{
1256 /*
1257 * If the message has been forwarded the effective addresses are in
1258 * hypervisor's TX buffer.
1259 */
1260 bool forwarded = (vm_locked.vm->id == HF_OTHER_WORLD_ID) &&
1261 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1262 (*page_count == 0);
1263
1264 if (forwarded) {
1265 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc =
1266 (struct ffa_endpoint_rx_tx_descriptor *)
1267 vm_locked.vm->mailbox.send;
1268 struct ffa_composite_memory_region *rx_region =
1269 ffa_enpoint_get_rx_memory_region(endpoint_desc);
1270 struct ffa_composite_memory_region *tx_region =
1271 ffa_enpoint_get_tx_memory_region(endpoint_desc);
1272
1273 *owner_vm_id = endpoint_desc->endpoint_id;
1274 *recv = ipa_init(rx_region->constituents[0].address);
1275 *send = ipa_init(tx_region->constituents[0].address);
1276 *page_count = rx_region->constituents[0].page_count;
1277 } else {
1278 *owner_vm_id = vm_locked.vm->id;
1279 }
1280}
Andrew Sculle1322792019-07-01 17:46:10 +01001281/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001282 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001283 * must not be shared. Locking of the page tables combined with a local memory
1284 * pool ensures there will always be enough memory to recover from any errors
1285 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1286 * by another core which could result in this transaction being unable to roll
1287 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001288 *
1289 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001290 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001291 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001292 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001293 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001294 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001295 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001296 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001297struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001298 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001299{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001300 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001301 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001302 struct vm_locked vm_locked;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001303 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001304 struct mm_stage1_locked mm_stage1_locked;
1305 struct mpool local_page_pool;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001306 ffa_vm_id_t owner_vm_id;
1307
1308 vm_locked = vm_lock(vm);
1309 /*
1310 * Get the original buffer addresses and VM ID in case of forwarded
1311 * message.
1312 */
1313 api_get_rxtx_description(vm_locked, &send, &recv, &page_count,
1314 &owner_vm_id);
1315 vm_unlock(&vm_locked);
1316
1317 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1318 if (owner_vm_locked.vm == NULL) {
1319 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1320 owner_vm_id);
1321 return ffa_error(FFA_DENIED);
1322 }
Andrew Scull220e6212018-12-21 18:09:00 +00001323
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001324 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001325 * Create a local pool so any freed memory can't be used by another
1326 * thread. This is to ensure the original mapping can be restored if any
1327 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001328 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001329 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1330
Manish Pandeyd34f8892020-06-19 17:41:07 +01001331 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001332
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001333 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1334 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001335 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001336 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001337 }
1338
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001339 /* Forward buffer mapping to SPMC if coming from a VM. */
1340 plat_ffa_rxtx_map_forward(owner_vm_locked);
1341
Federico Recanati9f1b6532022-04-14 13:15:28 +02001342 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001343
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001344exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001345 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001346 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001347 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001348
1349 return ret;
1350}
1351
1352/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001353 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1354 * translation regime of the caller. Unmap the region for the hypervisor and
1355 * set the memory region to owned and exclusive for the component. Since the
1356 * memory region mapped in the page table, when the buffers were originally
1357 * created we can safely remap it.
1358 *
1359 * Returns:
1360 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1361 * behalf of the caller.
1362 * - FFA_SUCCESS on success if no further action is needed.
1363 */
1364struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1365 struct vcpu *current)
1366{
1367 struct vm *vm = current->vm;
1368 struct vm_locked vm_locked;
Federico Recanati8da9e332022-02-10 11:00:17 +01001369 ffa_vm_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001370 struct mm_stage1_locked mm_stage1_locked;
1371 paddr_t send_pa_begin;
1372 paddr_t send_pa_end;
1373 paddr_t recv_pa_begin;
1374 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001375 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001376
Federico Recanati8da9e332022-02-10 11:00:17 +01001377 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1378 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
1379 dlog_error("`allocator_id` must be 0 at virtual instances.\n");
1380 return ffa_error(FFA_INVALID_PARAMETERS);
1381 }
1382
1383 /* VM ID of which buffers have to be unmapped. */
1384 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1385
1386 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1387 vm = vm_locked.vm;
1388 if (vm == NULL) {
1389 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1390 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001391 return ffa_error(FFA_INVALID_PARAMETERS);
1392 }
1393
1394 /* Get send and receive buffers. */
1395 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001396 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001397 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001398 ret = ffa_error(FFA_INVALID_PARAMETERS);
1399 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001400 }
1401
1402 /* Currently a mailbox size of 1 page is assumed. */
1403 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1404 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1405 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1406 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1407
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001408 mm_stage1_locked = mm_lock_stage1();
1409
Federico Recanati8da9e332022-02-10 11:00:17 +01001410 /* Reset stage 2 mapping only for virtual FF-A instances. */
1411 if (vm_id_is_current_world(owner_vm_id)) {
1412 /*
1413 * Set the memory region of the buffers back to the default mode
1414 * for the VM. Since this memory region was already mapped for
1415 * the RXTX buffers we can safely remap them.
1416 */
1417 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1418 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1419 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001420
Federico Recanati8da9e332022-02-10 11:00:17 +01001421 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1422 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1423 &api_page_pool, NULL));
1424 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001425
1426 /* Unmap the buffers in the partition manager. */
1427 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1428 &api_page_pool));
1429 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1430 &api_page_pool));
1431
1432 vm->mailbox.send = NULL;
1433 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001434 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001435
Federico Recanati8da9e332022-02-10 11:00:17 +01001436 /* Forward buffer unmapping to SPMC if coming from a VM. */
1437 plat_ffa_rxtx_unmap_forward(owner_vm_id);
1438
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001439 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001440
1441out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001442 vm_unlock(&vm_locked);
1443
Federico Recanati8da9e332022-02-10 11:00:17 +01001444 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001445}
1446
1447/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001448 * Notifies the `to` VM about the message currently in its mailbox, possibly
1449 * with the help of the primary VM.
1450 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001451static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1452 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001453{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001454 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1455 struct ffa_value primary_ret = {
1456 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001457 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001458 };
1459
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001460 /* Messages for the primary VM are delivered directly. */
1461 if (to.vm->id == HF_PRIMARY_VM_ID) {
1462 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001463 * Only tell the primary VM the size and other details if the
1464 * message is for it, to avoid leaking data about messages for
1465 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001466 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001467 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001468
1469 to.vm->mailbox.state = MAILBOX_STATE_READ;
1470 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001471 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001472 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001473 }
1474
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001475 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1476
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001477 /* Messages for the TEE are sent on via the dispatcher. */
1478 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001479 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001480
Olivier Deprez112d2b52020-09-30 07:39:23 +02001481 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001482 /*
1483 * After the call to the TEE completes it must have finished
1484 * reading its RX buffer, so it is ready for another message.
1485 */
1486 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001487 /*
1488 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001489 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001490 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001491 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001492 }
1493
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001494 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001495 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001496 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001497 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001498 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001499
1500 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001501}
1502
1503/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001504 * Copies data from the sender's send buffer to the recipient's receive buffer
1505 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001506 *
1507 * If the recipient's receive buffer is busy, it can optionally register the
1508 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001509 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001510struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1511 ffa_vm_id_t receiver_vm_id, uint32_t size,
1512 uint32_t attributes, struct vcpu *current,
1513 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001514{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001515 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001516 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001517 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001518 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001519 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001520 struct vcpu_locked current_locked;
1521 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001522 bool notify =
1523 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001524
Andrew Walbran70bc8622019-10-07 14:15:58 +01001525 /* Ensure sender VM ID corresponds to the current VM. */
1526 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001527 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001528 }
1529
1530 /* Disallow reflexive requests as this suggests an error in the VM. */
1531 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001532 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001533 }
1534
1535 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001536 if (size > FFA_MSG_PAYLOAD_MAX) {
1537 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001538 }
1539
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001540 /*
1541 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1542 * invocation.
1543 */
1544 current_locked = vcpu_lock(current);
1545 is_direct_request_ongoing =
1546 is_ffa_direct_msg_request_ongoing(current_locked);
1547 vcpu_unlock(&current_locked);
1548
1549 if (is_direct_request_ongoing) {
1550 return ffa_error(FFA_DENIED);
1551 }
1552
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001553 /* Ensure the receiver VM exists. */
1554 to = vm_find(receiver_vm_id);
1555 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001556 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001557 }
1558
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001559 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001560 * Check that the sender has configured its send buffer. If the tx
1561 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1562 * be safely accessed after releasing the lock since the tx mailbox
1563 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001564 */
1565 sl_lock(&from->lock);
1566 from_msg = from->mailbox.send;
1567 sl_unlock(&from->lock);
1568
1569 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001570 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001571 }
1572
Andrew Walbran82d6d152019-12-24 15:02:06 +00001573 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001574
Andrew Walbran82d6d152019-12-24 15:02:06 +00001575 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001576 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001577 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001578 }
1579
Andrew Walbran82d6d152019-12-24 15:02:06 +00001580 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001581 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001582 to->mailbox.recv_size = size;
1583 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001584 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001585 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001586
1587out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001588 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001589
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001590 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001591}
1592
1593/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001594 * Checks whether the vCPU's attempt to block for a message has already been
1595 * interrupted or whether it is allowed to block.
1596 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001597bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001598{
Manish Pandey35e452f2021-02-18 21:36:34 +00001599 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001600 bool interrupted;
1601
Manish Pandey35e452f2021-02-18 21:36:34 +00001602 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001603
1604 /*
1605 * Don't block if there are enabled and pending interrupts, to match
1606 * behaviour of wait_for_interrupt.
1607 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001608 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001609
Manish Pandey35e452f2021-02-18 21:36:34 +00001610 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001611
1612 return interrupted;
1613}
1614
1615/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001616 * Receives a message from the mailbox. If one isn't available, this function
1617 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001618 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001619 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001620 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001621struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1622 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001623{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001624 bool is_direct_request_ongoing;
1625 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001626 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001627 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001628 bool is_from_secure_world =
1629 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001630
Andrew Scullaa039b32018-10-04 15:02:26 +01001631 /*
1632 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001633 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001634 */
J-Alvesb37fd082020-10-22 12:29:21 +01001635 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001636 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001637 }
1638
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001639 /*
1640 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1641 * invocation.
1642 */
1643 current_locked = vcpu_lock(current);
1644 is_direct_request_ongoing =
1645 is_ffa_direct_msg_request_ongoing(current_locked);
1646 vcpu_unlock(&current_locked);
1647
1648 if (is_direct_request_ongoing) {
1649 return ffa_error(FFA_DENIED);
1650 }
1651
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001652 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001653
Andrew Scullaa039b32018-10-04 15:02:26 +01001654 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001655 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1656 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001657 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001658 goto out;
1659 }
1660
1661 /* No pending message so fail if not allowed to block. */
1662 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001663 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001664 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001665 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001666
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001667 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001668 * From this point onward this call can only be interrupted or a message
1669 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001670 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001671 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001672 return_code = ffa_error(FFA_INTERRUPTED);
1673 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001674 goto out;
1675 }
1676
J-Alvesb37fd082020-10-22 12:29:21 +01001677 if (is_from_secure_world) {
1678 /* Return to other world if caller is a SP. */
1679 *next = api_switch_to_other_world(
1680 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001681 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001682 } else {
1683 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001684 struct ffa_value run_return = {
1685 .func = FFA_MSG_WAIT_32,
1686 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001687 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001688
Andrew Walbranb4816552018-12-05 17:35:42 +00001689 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001690 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001691 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001692out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001693 sl_unlock(&vm->lock);
1694
Jose Marinho3e2442f2019-03-12 13:30:37 +00001695 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001696}
1697
1698/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001699 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1700 * by this function, the caller must have called api_mailbox_send before with
1701 * the notify argument set to true, and this call must have failed because the
1702 * mailbox was not available.
1703 *
1704 * It should be called repeatedly to retrieve a list of VMs.
1705 *
1706 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1707 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001708 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001709int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001710{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001711 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001712 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001713 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001714
1715 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001716 if (list_empty(&vm->mailbox.ready_list)) {
1717 ret = -1;
1718 goto exit;
1719 }
1720
1721 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1722 ready_links);
1723 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001724 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001725
1726exit:
1727 sl_unlock(&vm->lock);
1728 return ret;
1729}
1730
1731/**
1732 * Retrieves the next VM waiting to be notified that the mailbox of the
1733 * specified VM became writable. Only primary VMs are allowed to call this.
1734 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001735 * Returns -1 on failure or if there are no waiters; the VM id of the next
1736 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001737 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001738int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001739{
1740 struct vm *vm;
1741 struct vm_locked locked;
1742 struct wait_entry *entry;
1743 struct vm *waiting_vm;
1744
1745 /* Only primary VMs are allowed to call this function. */
1746 if (current->vm->id != HF_PRIMARY_VM_ID) {
1747 return -1;
1748 }
1749
Andrew Walbran42347a92019-05-09 13:59:03 +01001750 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001751 if (vm == NULL) {
1752 return -1;
1753 }
1754
Fuad Tabbaed294af2019-12-20 10:43:01 +00001755 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001756 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001757 entry = api_fetch_waiter(locked);
1758 vm_unlock(&locked);
1759
1760 if (entry == NULL) {
1761 return -1;
1762 }
1763
1764 /* Enqueue notification to waiting VM. */
1765 waiting_vm = entry->waiting_vm;
1766
1767 sl_lock(&waiting_vm->lock);
1768 if (list_empty(&entry->ready_links)) {
1769 list_append(&waiting_vm->mailbox.ready_list,
1770 &entry->ready_links);
1771 }
1772 sl_unlock(&waiting_vm->lock);
1773
1774 return waiting_vm->id;
1775}
1776
1777/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001778 * Releases the caller's mailbox so that a new message can be received. The
1779 * caller must have copied out all data they wish to preserve as new messages
1780 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001781 *
1782 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001783 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1784 * - FFA_SUCCESS on success if no further action is needed.
1785 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001786 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001787 * hf_mailbox_waiter_get.
1788 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001789struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001790{
1791 struct vm *vm = current->vm;
1792 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001793 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001794
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001795 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001796 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001797 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001798 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001799 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001800 break;
1801
Andrew Sculld6ee1102019-04-05 22:12:42 +01001802 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001803 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001804 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001805 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001806 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001807 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001808
1809 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001810}
Andrew Walbran318f5732018-11-20 16:23:42 +00001811
1812/**
1813 * Enables or disables a given interrupt ID for the calling vCPU.
1814 *
1815 * Returns 0 on success, or -1 if the intid is invalid.
1816 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001817int64_t api_interrupt_enable(uint32_t intid, bool enable,
1818 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001819{
Manish Pandey35e452f2021-02-18 21:36:34 +00001820 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001821 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001822 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1823 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001824
Andrew Walbran318f5732018-11-20 16:23:42 +00001825 if (intid >= HF_NUM_INTIDS) {
1826 return -1;
1827 }
1828
Manish Pandey35e452f2021-02-18 21:36:34 +00001829 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001830 if (enable) {
Maksims Svecovs953dee52022-04-01 12:15:36 +01001831 if (type == INTERRUPT_TYPE_IRQ) {
1832 current->interrupts.interrupt_type[intid_index] &=
1833 ~intid_mask;
1834 } else if (type == INTERRUPT_TYPE_FIQ) {
1835 current->interrupts.interrupt_type[intid_index] |=
1836 intid_mask;
1837 }
1838
Andrew Walbran3d84a262018-12-13 14:41:19 +00001839 /*
1840 * If it is pending and was not enabled before, increment the
1841 * count.
1842 */
1843 if (current->interrupts.interrupt_pending[intid_index] &
1844 ~current->interrupts.interrupt_enabled[intid_index] &
1845 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001846 if ((current->interrupts.interrupt_type[intid_index] &
1847 intid_mask) ==
1848 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1849 vcpu_irq_count_increment(current_locked);
1850 } else {
1851 vcpu_fiq_count_increment(current_locked);
1852 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001853 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001854 current->interrupts.interrupt_enabled[intid_index] |=
1855 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001856 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001857 /*
1858 * If it is pending and was enabled before, decrement the count.
1859 */
1860 if (current->interrupts.interrupt_pending[intid_index] &
1861 current->interrupts.interrupt_enabled[intid_index] &
1862 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001863 if ((current->interrupts.interrupt_type[intid_index] &
1864 intid_mask) ==
1865 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1866 vcpu_irq_count_decrement(current_locked);
1867 } else {
1868 vcpu_fiq_count_decrement(current_locked);
1869 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001870 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001871 current->interrupts.interrupt_enabled[intid_index] &=
1872 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001873 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001874 }
1875
Manish Pandey35e452f2021-02-18 21:36:34 +00001876 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001877 return 0;
1878}
1879
1880/**
1881 * Returns the ID of the next pending interrupt for the calling vCPU, and
1882 * acknowledges it (i.e. marks it as no longer pending). Returns
1883 * HF_INVALID_INTID if there are no pending interrupts.
1884 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001885uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001886{
1887 uint8_t i;
1888 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001889 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001890
1891 /*
1892 * Find the first enabled and pending interrupt ID, return it, and
1893 * deactivate it.
1894 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001895 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001896 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1897 uint32_t enabled_and_pending =
1898 current->interrupts.interrupt_enabled[i] &
1899 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001900
Andrew Walbran318f5732018-11-20 16:23:42 +00001901 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001902 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001903 uint32_t intid_mask = 1U << bit_index;
1904
Andrew Walbran3d84a262018-12-13 14:41:19 +00001905 /*
1906 * Mark it as no longer pending and decrement the count.
1907 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001908 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1909
1910 if ((current->interrupts.interrupt_type[i] &
1911 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1912 vcpu_irq_count_decrement(current_locked);
1913 } else {
1914 vcpu_fiq_count_decrement(current_locked);
1915 }
1916
Andrew Walbran3d84a262018-12-13 14:41:19 +00001917 first_interrupt =
1918 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001919 break;
1920 }
1921 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001922
Manish Pandey35e452f2021-02-18 21:36:34 +00001923 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001924 return first_interrupt;
1925}
1926
1927/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001928 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001929 * given VM and vCPU.
1930 */
1931static inline bool is_injection_allowed(uint32_t target_vm_id,
1932 struct vcpu *current)
1933{
1934 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001935
Andrew Walbran318f5732018-11-20 16:23:42 +00001936 /*
1937 * The primary VM is allowed to inject interrupts into any VM. Secondary
1938 * VMs are only allowed to inject interrupts into their own vCPUs.
1939 */
1940 return current_vm_id == HF_PRIMARY_VM_ID ||
1941 current_vm_id == target_vm_id;
1942}
1943
1944/**
1945 * Injects a virtual interrupt of the given ID into the given target vCPU.
1946 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1947 * when the vCPU is next run, which is up to the scheduler.
1948 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001949 * Returns:
1950 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1951 * ID is invalid, or the current VM is not allowed to inject interrupts to
1952 * the target VM.
1953 * - 0 on success if no further action is needed.
1954 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1955 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001956 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001957int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1958 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001959 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001960{
Andrew Walbran318f5732018-11-20 16:23:42 +00001961 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001962 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001963
1964 if (intid >= HF_NUM_INTIDS) {
1965 return -1;
1966 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001967
Andrew Walbran318f5732018-11-20 16:23:42 +00001968 if (target_vm == NULL) {
1969 return -1;
1970 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001971
Andrew Walbran318f5732018-11-20 16:23:42 +00001972 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001973 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001974 return -1;
1975 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001976
Andrew Walbran318f5732018-11-20 16:23:42 +00001977 if (!is_injection_allowed(target_vm_id, current)) {
1978 return -1;
1979 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001980
Andrew Walbrane1310df2019-04-29 17:28:28 +01001981 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001982
Manish Pandey35e452f2021-02-18 21:36:34 +00001983 dlog_verbose(
1984 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1985 "%u\n",
1986 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1987 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001988 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001989}
Andrew Scull6386f252018-12-06 13:29:10 +00001990
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001991/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001992struct ffa_value api_ffa_version(struct vcpu *current,
1993 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001994{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001995 struct vm_locked current_vm_locked;
1996
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001997 /*
1998 * Ensure that both major and minor revision representation occupies at
1999 * most 15 bits.
2000 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002001 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002002 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002003 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002004 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002005 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01002006 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00002007 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01002008 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002009
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002010 current_vm_locked = vm_lock(current->vm);
2011 current_vm_locked.vm->ffa_version = requested_version;
2012 vm_unlock(&current_vm_locked);
2013
Daniel Boulby6e32c612021-02-17 15:09:41 +00002014 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002015}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002016
2017int64_t api_debug_log(char c, struct vcpu *current)
2018{
Andrew Sculld54e1be2019-08-20 11:09:42 +01002019 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002020 struct vm *vm = current->vm;
2021 struct vm_locked vm_locked = vm_lock(vm);
2022
Andrew Sculld54e1be2019-08-20 11:09:42 +01002023 if (c == '\n' || c == '\0') {
2024 flush = true;
2025 } else {
2026 vm->log_buffer[vm->log_buffer_length++] = c;
2027 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
2028 }
2029
2030 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01002031 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
2032 vm->log_buffer_length);
2033 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002034 }
2035
2036 vm_unlock(&vm_locked);
2037
2038 return 0;
2039}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002040
2041/**
J-Alves6f72ca82021-11-01 12:34:58 +00002042 * Helper for success return of FFA_FEATURES, for when it is used to query
2043 * an interrupt ID.
2044 */
2045struct ffa_value api_ffa_feature_success(uint32_t arg2)
2046{
2047 return (struct ffa_value){
2048 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
2049}
2050
2051/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002052 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002053 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002054 */
J-Alves6f72ca82021-11-01 12:34:58 +00002055struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002056{
J-Alves6f72ca82021-11-01 12:34:58 +00002057 /*
2058 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
2059 * if using a feature ID.
2060 */
2061 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
J-Alvesff676c12022-05-13 17:25:33 +01002062 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0U) {
J-Alves6f72ca82021-11-01 12:34:58 +00002063 return ffa_error(FFA_NOT_SUPPORTED);
2064 }
2065
2066 switch (feature_function_id) {
2067 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002068 case FFA_ERROR_32:
2069 case FFA_SUCCESS_32:
2070 case FFA_INTERRUPT_32:
2071 case FFA_VERSION_32:
2072 case FFA_FEATURES_32:
2073 case FFA_RX_RELEASE_32:
2074 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002075 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002076 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002077 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002078 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002079 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002080 case FFA_MEM_DONATE_32:
2081 case FFA_MEM_LEND_32:
2082 case FFA_MEM_SHARE_32:
2083 case FFA_MEM_RETRIEVE_REQ_32:
2084 case FFA_MEM_RETRIEVE_RESP_32:
2085 case FFA_MEM_RELINQUISH_32:
2086 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002087 case FFA_MEM_FRAG_RX_32:
2088 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002089 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002090 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002091 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002092 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00002093#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002094 /* FF-A v1.1 features. */
2095 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002096 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2097 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2098 case FFA_NOTIFICATION_BIND_32:
2099 case FFA_NOTIFICATION_UNBIND_32:
2100 case FFA_NOTIFICATION_SET_32:
2101 case FFA_NOTIFICATION_GET_32:
2102 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07002103 case FFA_MEM_PERM_GET_32:
2104 case FFA_MEM_PERM_SET_32:
2105 case FFA_MEM_PERM_GET_64:
2106 case FFA_MEM_PERM_SET_64:
J-Alves3829fc02021-03-18 12:49:18 +00002107#endif
2108 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00002109
2110#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2111 /* Check support of a feature provided respective feature ID. */
2112 case FFA_FEATURE_NPI:
2113 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2114 case FFA_FEATURE_SRI:
2115 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2116#endif
2117 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002118 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002119 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002120 }
2121}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002122
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002123/**
J-Alves645eabe2021-02-22 16:08:27 +00002124 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2125 * interfaces.
2126 */
2127static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2128{
2129 return args.arg2 == 0U;
2130}
2131
2132/**
J-Alves76d99af2021-03-10 17:42:11 +00002133 * Limits size of arguments in ffa_value structure to 32-bit.
2134 */
2135static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2136{
2137 return (struct ffa_value){
2138 .func = (uint32_t)args.func,
2139 .arg1 = (uint32_t)args.arg1,
2140 .arg2 = (uint32_t)0,
2141 .arg3 = (uint32_t)args.arg3,
2142 .arg4 = (uint32_t)args.arg4,
2143 .arg5 = (uint32_t)args.arg5,
2144 .arg6 = (uint32_t)args.arg6,
2145 .arg7 = (uint32_t)args.arg7,
2146 };
2147}
2148
2149/**
2150 * Helper to copy direct message payload, depending on SMC used and expected
2151 * registers size.
2152 */
2153static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2154{
2155 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2156 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2157 return api_ffa_value_copy32(args);
2158 }
2159
2160 return (struct ffa_value){
2161 .func = args.func,
2162 .arg1 = args.arg1,
2163 .arg2 = 0,
2164 .arg3 = args.arg3,
2165 .arg4 = args.arg4,
2166 .arg5 = args.arg5,
2167 .arg6 = args.arg6,
2168 .arg7 = args.arg7,
2169 };
2170}
2171
2172/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002173 * Send an FF-A direct message request.
2174 */
2175struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2176 ffa_vm_id_t receiver_vm_id,
2177 struct ffa_value args,
2178 struct vcpu *current,
2179 struct vcpu **next)
2180{
J-Alves17228f72021-04-20 17:13:19 +01002181 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002182 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002183 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002184 struct vcpu *receiver_vcpu;
2185 struct two_vcpu_locked vcpus_locked;
2186
J-Alves645eabe2021-02-22 16:08:27 +00002187 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2188 return ffa_error(FFA_INVALID_PARAMETERS);
2189 }
2190
Olivier Deprez55a189e2021-06-09 15:45:27 +02002191 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2192 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002193 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002194 }
2195
Olivier Deprez55a189e2021-06-09 15:45:27 +02002196 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002197 return ret;
2198 }
2199
2200 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2201
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002202 receiver_vm = vm_find(receiver_vm_id);
2203 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002204 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002205 return ffa_error(FFA_INVALID_PARAMETERS);
2206 }
2207
2208 /*
J-Alves439ac972021-11-18 17:32:03 +00002209 * Check if sender supports sending direct message req, and if
2210 * receiver supports receipt of direct message requests.
2211 */
2212 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2213 return ffa_error(FFA_DENIED);
2214 }
2215
2216 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002217 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002218 * UP (migratable) or MP partitions with a number of vCPUs matching the
2219 * number of PEs in the system. It further states that MP partitions
2220 * accepting direct request messages cannot migrate.
2221 */
J-Alvesad6a0432021-04-09 16:06:21 +01002222 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002223 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002224 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002225 return ffa_error(FFA_INVALID_PARAMETERS);
2226 }
2227
J-Alves6e2abc62021-12-02 14:58:56 +00002228 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002229 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2230
2231 /*
2232 * If destination vCPU is executing or already received an
2233 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2234 * busy. There is a brief period of time where the vCPU state has
2235 * changed but regs_available is still false thus consider this case as
2236 * the vCPU not yet ready to receive a direct message request.
2237 */
2238 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2239 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2240 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002241 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002242 ret = ffa_error(FFA_BUSY);
2243 goto out;
2244 }
2245
2246 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2247 memory_order_relaxed)) {
2248 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002249 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002250 receiver_vcpu->vm->id,
2251 vcpu_index(receiver_vcpu));
2252 receiver_vcpu->state = VCPU_STATE_ABORTED;
2253 }
2254
2255 ret = ffa_error(FFA_ABORTED);
2256 goto out;
2257 }
2258
2259 switch (receiver_vcpu->state) {
2260 case VCPU_STATE_OFF:
2261 case VCPU_STATE_RUNNING:
2262 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002263 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002264 case VCPU_STATE_BLOCKED:
2265 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002266 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2267 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002268 ret = ffa_error(FFA_BUSY);
2269 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002270 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002271 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002272 * We expect target vCPU to be in WAITING state after either
2273 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002274 */
2275 break;
2276 }
2277
2278 /* Inject timer interrupt if any pending */
2279 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002280 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2281 HF_VIRTUAL_TIMER_INTID, current,
2282 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002283
2284 arch_timer_mask(&receiver_vcpu->regs);
2285 }
2286
2287 /* The receiver vCPU runs upon direct message invocation */
2288 receiver_vcpu->cpu = current->cpu;
2289 receiver_vcpu->state = VCPU_STATE_RUNNING;
2290 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002291 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002292
J-Alves76d99af2021-03-10 17:42:11 +00002293 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002294
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002295 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002296
2297 /* Switch to receiver vCPU targeted to by direct msg request */
2298 *next = receiver_vcpu;
2299
J-Alves6e2abc62021-12-02 14:58:56 +00002300 if (!receiver_locked.vm->el0_partition) {
2301 /*
2302 * If the scheduler in the system is giving CPU cycles to the
2303 * receiver, due to pending notifications, inject the NPI
2304 * interrupt. Following call assumes that '*next' has been set
2305 * to receiver_vcpu.
2306 */
2307 plat_ffa_inject_notification_pending_interrupt(
2308 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2309 ? vcpus_locked.vcpu1
2310 : vcpus_locked.vcpu2,
2311 current, receiver_locked);
2312 }
2313
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002314 /*
2315 * Since this flow will lead to a VM switch, the return value will not
2316 * be applied to current vCPU.
2317 */
2318
2319out:
2320 sl_unlock(&receiver_vcpu->lock);
2321 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002322 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002323
2324 return ret;
2325}
2326
2327/**
2328 * Send an FF-A direct message response.
2329 */
2330struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2331 ffa_vm_id_t receiver_vm_id,
2332 struct ffa_value args,
2333 struct vcpu *current,
2334 struct vcpu **next)
2335{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002336 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002337
2338 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2339 return ffa_error(FFA_INVALID_PARAMETERS);
2340 }
2341
J-Alves76d99af2021-03-10 17:42:11 +00002342 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002343
Olivier Deprez55a189e2021-06-09 15:45:27 +02002344 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2345 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002346 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002347 }
2348
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002349 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002350 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002351 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002352 * No need for REQ/RESP state management as managed exit does
2353 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002354 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002355 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2356 vcpu_unlock(&current_locked);
2357 return ffa_error(FFA_DENIED);
2358 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002359
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002360 /*
2361 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2362 * a secure interrupt to a SP that is performing managed exit.
2363 * We have taken a implementation defined choice to not allow
2364 * Managed exit while a SP is processing a secure interrupt.
2365 */
2366 CHECK(!current->processing_secure_interrupt);
2367
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06002368 plat_interrupts_set_priority_mask(current->priority_mask);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002369 current->processing_managed_exit = false;
2370 } else {
2371 /*
2372 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2373 * defined originator.
2374 */
2375 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2376 /*
2377 * Sending direct response but direct request origin
2378 * vCPU is not set.
2379 */
2380 vcpu_unlock(&current_locked);
2381 return ffa_error(FFA_DENIED);
2382 }
2383
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002384 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002385 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2386 vcpu_unlock(&current_locked);
2387 return ffa_error(FFA_DENIED);
2388 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002389 }
2390
2391 /* Clear direct request origin for the caller. */
2392 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2393
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002394 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002395
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002396 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002397 *next = api_switch_to_other_world(
2398 current, to_ret,
2399 /*
2400 * Current vcpu sent a direct response. It moves to
2401 * waiting state.
2402 */
2403 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002404 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002405 *next = api_switch_to_primary(
2406 current, to_ret,
2407 /*
2408 * Current vcpu sent a direct response. It moves to
2409 * waiting state.
2410 */
2411 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002412 } else if (vm_id_is_current_world(receiver_vm_id)) {
2413 /*
2414 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002415 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002416 * made function return error before getting to this point.
2417 */
2418 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002419 /*
2420 * current vcpu sent a direct response.
2421 * It moves to waiting state.
2422 */
2423 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002424 } else {
2425 panic("Invalid direct message response invocation");
2426 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002427
2428 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2429}
2430
J-Alves84658fc2021-06-17 14:37:32 +01002431static bool api_memory_region_check_flags(
2432 struct ffa_memory_region *memory_region, uint32_t share_func)
2433{
2434 switch (share_func) {
2435 case FFA_MEM_SHARE_32:
2436 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2437 0U) {
2438 return false;
2439 }
2440 /* Intentional fall-through */
2441 case FFA_MEM_LEND_32:
2442 case FFA_MEM_DONATE_32: {
2443 /* Bits 31:2 Must Be Zero. */
2444 ffa_memory_receiver_flags_t to_mask =
2445 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2446 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2447
2448 if ((memory_region->flags & to_mask) != 0U) {
2449 return false;
2450 }
2451 break;
2452 }
2453 default:
2454 panic("Check for mem send calls only.\n");
2455 }
2456
2457 /* Last check reserved values are 0 */
2458 return true;
2459}
2460
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002461struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2462 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002463 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002464{
2465 struct vm *from = current->vm;
2466 struct vm *to;
2467 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002468 struct ffa_memory_region *memory_region;
2469 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002470
2471 if (ipa_addr(address) != 0 || page_count != 0) {
2472 /*
2473 * Hafnium only supports passing the descriptor in the TX
2474 * mailbox.
2475 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002476 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002477 }
2478
Andrew Walbranca808b12020-05-15 17:22:28 +01002479 if (fragment_length > length) {
2480 dlog_verbose(
2481 "Fragment length %d greater than total length %d.\n",
2482 fragment_length, length);
2483 return ffa_error(FFA_INVALID_PARAMETERS);
2484 }
2485 if (fragment_length < sizeof(struct ffa_memory_region) +
2486 sizeof(struct ffa_memory_access)) {
2487 dlog_verbose(
2488 "Initial fragment length %d smaller than header size "
2489 "%d.\n",
2490 fragment_length,
2491 sizeof(struct ffa_memory_region) +
2492 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002493 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002494 }
2495
2496 /*
2497 * Check that the sender has configured its send buffer. If the TX
2498 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2499 * be safely accessed after releasing the lock since the TX mailbox
2500 * address can only be configured once.
2501 */
2502 sl_lock(&from->lock);
2503 from_msg = from->mailbox.send;
2504 sl_unlock(&from->lock);
2505
2506 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002507 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002508 }
2509
2510 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002511 * Copy the memory region descriptor to a fresh page from the memory
2512 * pool. This prevents the sender from changing it underneath us, and
2513 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002514 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002515 if (fragment_length > HF_MAILBOX_SIZE ||
2516 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002517 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002518 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002519 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002520 if (memory_region == NULL) {
2521 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002522 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002523 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002524 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002525
2526 /* The sender must match the caller. */
2527 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002528 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002529 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002530 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002531 }
2532
J-Alves84658fc2021-06-17 14:37:32 +01002533 if (!api_memory_region_check_flags(memory_region, share_func)) {
2534 dlog_verbose(
2535 "Memory region reserved arguments must be zero.\n");
2536 ret = ffa_error(FFA_INVALID_PARAMETERS);
2537 goto out;
2538 }
2539
Andrew Walbrana65a1322020-04-06 19:32:32 +01002540 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002541 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002542 dlog_verbose(
2543 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002544 "endpoint memory access descriptors, expected 1).\n",
2545 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002546 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002547 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002548 }
2549
2550 /*
2551 * Ensure that the receiver VM exists and isn't the same as the sender.
2552 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002553 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002554 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002555 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002556 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002557 goto out;
2558 }
2559
Maksims Svecovsa3d570c2021-12-08 11:16:32 +00002560 if (!plat_ffa_is_memory_send_valid(to->id, share_func)) {
2561 ret = ffa_error(FFA_DENIED);
2562 goto out;
2563 }
2564
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002565 if (to->id == HF_TEE_VM_ID) {
2566 /*
2567 * The 'to' VM lock is only needed in the case that it is the
2568 * TEE VM.
2569 */
2570 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2571
2572 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002573 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002574 goto out_unlock;
2575 }
2576
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002577 ret = ffa_memory_tee_send(
2578 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2579 length, fragment_length, share_func, &api_page_pool);
2580 /*
2581 * ffa_tee_memory_send takes ownership of the memory_region, so
2582 * make sure we don't free it.
2583 */
2584 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002585
2586 out_unlock:
2587 vm_unlock(&vm_to_from_lock.vm1);
2588 vm_unlock(&vm_to_from_lock.vm2);
2589 } else {
2590 struct vm_locked from_locked = vm_lock(from);
2591
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002592 ret = ffa_memory_send(from_locked, memory_region, length,
2593 fragment_length, share_func,
2594 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002595 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002596 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002597 * make sure we don't free it.
2598 */
2599 memory_region = NULL;
2600
2601 vm_unlock(&from_locked);
2602 }
2603
2604out:
2605 if (memory_region != NULL) {
2606 mpool_free(&api_page_pool, memory_region);
2607 }
2608
2609 return ret;
2610}
2611
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002612struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2613 uint32_t fragment_length,
2614 ipaddr_t address, uint32_t page_count,
2615 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002616{
2617 struct vm *to = current->vm;
2618 struct vm_locked to_locked;
2619 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002620 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002621 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002622 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002623
2624 if (ipa_addr(address) != 0 || page_count != 0) {
2625 /*
2626 * Hafnium only supports passing the descriptor in the TX
2627 * mailbox.
2628 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002629 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002630 }
2631
Andrew Walbrana65a1322020-04-06 19:32:32 +01002632 if (fragment_length != length) {
2633 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002634 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002635 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002636
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002637 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002638 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002639 message_buffer_size = cpu_get_buffer_size(current->cpu);
2640 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2641 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002642 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002643 }
2644
2645 to_locked = vm_lock(to);
2646 to_msg = to->mailbox.send;
2647
2648 if (to_msg == NULL) {
2649 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002650 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002651 goto out;
2652 }
2653
2654 /*
2655 * Copy the retrieve request descriptor to an internal buffer, so that
2656 * the caller can't change it underneath us.
2657 */
2658 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2659
2660 if (msg_receiver_busy(to_locked, NULL, false)) {
2661 /*
2662 * Can't retrieve memory information if the mailbox is not
2663 * available.
2664 */
2665 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002666 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002667 goto out;
2668 }
2669
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002670 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2671 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002672
2673out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002674 vm_unlock(&to_locked);
2675 return ret;
2676}
2677
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002678struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002679{
2680 struct vm *from = current->vm;
2681 struct vm_locked from_locked;
2682 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002683 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002684 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002685 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002686 uint32_t length;
2687
2688 from_locked = vm_lock(from);
2689 from_msg = from->mailbox.send;
2690
2691 if (from_msg == NULL) {
2692 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002693 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002694 goto out;
2695 }
2696
2697 /*
2698 * Calculate length from relinquish descriptor before copying. We will
2699 * check again later to make sure it hasn't changed.
2700 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002701 length = sizeof(struct ffa_mem_relinquish) +
2702 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2703 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002704 /*
2705 * Copy the relinquish descriptor to an internal buffer, so that the
2706 * caller can't change it underneath us.
2707 */
2708 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002709 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002710 message_buffer_size = cpu_get_buffer_size(current->cpu);
2711 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2712 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002713 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002714 goto out;
2715 }
2716 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2717
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002718 if (sizeof(struct ffa_mem_relinquish) +
2719 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002720 length) {
2721 dlog_verbose(
2722 "Endpoint count changed while copying to internal "
2723 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002724 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002725 goto out;
2726 }
2727
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002728 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2729 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002730
2731out:
2732 vm_unlock(&from_locked);
2733 return ret;
2734}
2735
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002736struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2737 ffa_memory_region_flags_t flags,
2738 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002739{
2740 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002741 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002742
Olivier Deprez55a189e2021-06-09 15:45:27 +02002743 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002744 struct vm_locked to_locked = vm_lock(to);
2745
Andrew Walbranca808b12020-05-15 17:22:28 +01002746 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002747 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002748
Andrew Walbran290b0c92020-02-03 16:37:14 +00002749 vm_unlock(&to_locked);
2750 } else {
2751 struct vm *from = vm_find(HF_TEE_VM_ID);
2752 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2753
Andrew Walbranca808b12020-05-15 17:22:28 +01002754 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2755 vm_to_from_lock.vm2, handle, flags,
2756 &api_page_pool);
2757
2758 vm_unlock(&vm_to_from_lock.vm1);
2759 vm_unlock(&vm_to_from_lock.vm2);
2760 }
2761
2762 return ret;
2763}
2764
2765struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2766 uint32_t fragment_offset,
2767 ffa_vm_id_t sender_vm_id,
2768 struct vcpu *current)
2769{
2770 struct vm *to = current->vm;
2771 struct vm_locked to_locked;
2772 struct ffa_value ret;
2773
2774 /* Sender ID MBZ at virtual instance. */
2775 if (sender_vm_id != 0) {
2776 return ffa_error(FFA_INVALID_PARAMETERS);
2777 }
2778
2779 to_locked = vm_lock(to);
2780
2781 if (msg_receiver_busy(to_locked, NULL, false)) {
2782 /*
2783 * Can't retrieve memory information if the mailbox is not
2784 * available.
2785 */
2786 dlog_verbose("RX buffer not ready.\n");
2787 ret = ffa_error(FFA_BUSY);
2788 goto out;
2789 }
2790
2791 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2792 &api_page_pool);
2793
2794out:
2795 vm_unlock(&to_locked);
2796 return ret;
2797}
2798
2799struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2800 uint32_t fragment_length,
2801 ffa_vm_id_t sender_vm_id,
2802 struct vcpu *current)
2803{
2804 struct vm *from = current->vm;
2805 const void *from_msg;
2806 void *fragment_copy;
2807 struct ffa_value ret;
2808
2809 /* Sender ID MBZ at virtual instance. */
2810 if (sender_vm_id != 0) {
2811 return ffa_error(FFA_INVALID_PARAMETERS);
2812 }
2813
2814 /*
2815 * Check that the sender has configured its send buffer. If the TX
2816 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2817 * be safely accessed after releasing the lock since the TX mailbox
2818 * address can only be configured once.
2819 */
2820 sl_lock(&from->lock);
2821 from_msg = from->mailbox.send;
2822 sl_unlock(&from->lock);
2823
2824 if (from_msg == NULL) {
2825 return ffa_error(FFA_INVALID_PARAMETERS);
2826 }
2827
2828 /*
2829 * Copy the fragment to a fresh page from the memory pool. This prevents
2830 * the sender from changing it underneath us, and also lets us keep it
2831 * around in the share state table if needed.
2832 */
2833 if (fragment_length > HF_MAILBOX_SIZE ||
2834 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2835 dlog_verbose(
2836 "Fragment length %d larger than mailbox size %d.\n",
2837 fragment_length, HF_MAILBOX_SIZE);
2838 return ffa_error(FFA_INVALID_PARAMETERS);
2839 }
2840 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2841 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2842 0) {
2843 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2844 return ffa_error(FFA_INVALID_PARAMETERS);
2845 }
2846 fragment_copy = mpool_alloc(&api_page_pool);
2847 if (fragment_copy == NULL) {
2848 dlog_verbose("Failed to allocate fragment copy.\n");
2849 return ffa_error(FFA_NO_MEMORY);
2850 }
2851 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2852
2853 /*
2854 * Hafnium doesn't support fragmentation of memory retrieve requests
2855 * (because it doesn't support caller-specified mappings, so a request
2856 * will never be larger than a single page), so this must be part of a
2857 * memory send (i.e. donate, lend or share) request.
2858 *
2859 * We can tell from the handle whether the memory transaction is for the
2860 * TEE or not.
2861 */
2862 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2863 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2864 struct vm_locked from_locked = vm_lock(from);
2865
2866 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2867 fragment_length, handle,
2868 &api_page_pool);
2869 /*
2870 * `ffa_memory_send_continue` takes ownership of the
2871 * fragment_copy, so we don't need to free it here.
2872 */
2873 vm_unlock(&from_locked);
2874 } else {
2875 struct vm *to = vm_find(HF_TEE_VM_ID);
2876 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2877
2878 /*
2879 * The TEE RX buffer state is checked in
2880 * `ffa_memory_tee_send_continue` rather than here, as we need
2881 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2882 * than FFA_ERROR FFA_BUSY in case it is busy.
2883 */
2884
2885 ret = ffa_memory_tee_send_continue(
2886 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2887 fragment_length, handle, &api_page_pool);
2888 /*
2889 * `ffa_memory_tee_send_continue` takes ownership of the
2890 * fragment_copy, so we don't need to free it here.
2891 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002892
2893 vm_unlock(&vm_to_from_lock.vm1);
2894 vm_unlock(&vm_to_from_lock.vm2);
2895 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002896
2897 return ret;
2898}
Max Shvetsov40108e72020-08-27 12:39:50 +01002899
Olivier Deprezd614d322021-06-18 15:21:00 +02002900/**
2901 * Register an entry point for a vCPU in warm boot cases.
2902 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
2903 */
Max Shvetsov40108e72020-08-27 12:39:50 +01002904struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2905 struct vcpu *current)
2906{
2907 struct vm_locked vm_locked;
Olivier Deprezd614d322021-06-18 15:21:00 +02002908 struct ffa_value ret = ffa_error(FFA_DENIED);
Max Shvetsov40108e72020-08-27 12:39:50 +01002909
Olivier Deprezd614d322021-06-18 15:21:00 +02002910 /*
2911 * Reject if interface is not supported at this FF-A instance
2912 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
2913 */
2914 if (!plat_ffa_is_secondary_ep_register_supported() ||
2915 current->vm->vcpu_count == 1) {
2916 return ffa_error(FFA_NOT_SUPPORTED);
2917 }
2918
2919 /*
2920 * No further check is made on the address validity
2921 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
2922 * from the VM or vCPU structure.
2923 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
2924 * For each SP [...] the Framework assumes that the same entry point
2925 * address is used for initializing any execution context during a
2926 * secondary cold boot.
2927 * If this function is invoked multiple times, then the entry point
2928 * address specified in the last valid invocation must be used by the
2929 * callee.
2930 */
Max Shvetsov40108e72020-08-27 12:39:50 +01002931 vm_locked = vm_lock(current->vm);
Olivier Deprezd614d322021-06-18 15:21:00 +02002932 if (vm_locked.vm->initialized) {
2933 goto out;
2934 }
2935
Max Shvetsov40108e72020-08-27 12:39:50 +01002936 vm_locked.vm->secondary_ep = entry_point;
Olivier Deprezd614d322021-06-18 15:21:00 +02002937
2938 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2939
2940out:
Max Shvetsov40108e72020-08-27 12:39:50 +01002941 vm_unlock(&vm_locked);
2942
Olivier Deprezd614d322021-06-18 15:21:00 +02002943 return ret;
Max Shvetsov40108e72020-08-27 12:39:50 +01002944}
J-Alvesa0f317d2021-06-09 13:31:59 +01002945
2946struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
2947 ffa_vcpu_count_t vcpu_count,
2948 struct vcpu *current)
2949{
2950 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2951 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
2952 vm_id);
2953 return ffa_error(FFA_NOT_SUPPORTED);
2954 }
2955
2956 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
2957}
2958
2959struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
2960 struct vcpu *current)
2961{
2962 /*
2963 * Validity of use of this interface is the same as for bitmap create.
2964 */
2965 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2966 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
2967 vm_id);
2968 return ffa_error(FFA_NOT_SUPPORTED);
2969 }
2970
2971 return plat_ffa_notifications_bitmap_destroy(vm_id);
2972}
J-Alvesc003a7a2021-03-18 13:06:53 +00002973
2974struct ffa_value api_ffa_notification_update_bindings(
2975 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2976 ffa_notifications_bitmap_t notifications, bool is_bind,
2977 struct vcpu *current)
2978{
2979 struct ffa_value ret = {.func = FFA_SUCCESS_32};
2980 struct vm_locked receiver_locked;
2981 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2982 const ffa_vm_id_t id_to_update =
2983 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
2984 const ffa_vm_id_t id_to_validate =
2985 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
2986
2987 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
2988 receiver_vm_id)) {
2989 dlog_verbose("Invalid use of notifications bind interface.\n");
2990 return ffa_error(FFA_INVALID_PARAMETERS);
2991 }
2992
J-Alvesb15e9402021-09-08 11:44:42 +01002993 if (plat_ffa_notifications_update_bindings_forward(
2994 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
2995 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01002996 return ret;
2997 }
2998
J-Alvesc003a7a2021-03-18 13:06:53 +00002999 if (notifications == 0U) {
3000 dlog_verbose("No notifications have been specified.\n");
3001 return ffa_error(FFA_INVALID_PARAMETERS);
3002 }
3003
3004 /**
3005 * This check assumes receiver is the current VM, and has been enforced
3006 * by 'plat_ffa_is_notifications_bind_valid'.
3007 */
3008 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3009
3010 if (receiver_locked.vm == NULL) {
3011 dlog_verbose("Receiver doesn't exist!\n");
3012 return ffa_error(FFA_DENIED);
3013 }
3014
J-Alves09ff9d82021-11-02 11:55:20 +00003015 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00003016 dlog_verbose("Notifications are not enabled.\n");
3017 ret = ffa_error(FFA_NOT_SUPPORTED);
3018 goto out;
3019 }
3020
3021 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
3022 vm_find(sender_vm_id) == NULL) {
3023 dlog_verbose("Sender VM does not exist!\n");
3024 ret = ffa_error(FFA_INVALID_PARAMETERS);
3025 goto out;
3026 }
3027
3028 /*
3029 * Can't bind/unbind notifications if at least one is bound to a
3030 * different sender.
3031 */
3032 if (!vm_notifications_validate_bound_sender(
3033 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3034 id_to_validate, notifications)) {
3035 dlog_verbose("Notifications are bound to other sender.\n");
3036 ret = ffa_error(FFA_DENIED);
3037 goto out;
3038 }
3039
3040 /**
3041 * Check if there is a pending notification within those specified in
3042 * the bitmap.
3043 */
3044 if (vm_are_notifications_pending(receiver_locked,
3045 plat_ffa_is_vm_id(sender_vm_id),
3046 notifications)) {
3047 dlog_verbose("Notifications within '%x' pending.\n",
3048 notifications);
3049 ret = ffa_error(FFA_DENIED);
3050 goto out;
3051 }
3052
3053 vm_notifications_update_bindings(
3054 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
3055 notifications, is_per_vcpu && is_bind);
3056
3057out:
3058 vm_unlock(&receiver_locked);
3059 return ret;
3060}
J-Alvesaa79c012021-07-09 14:29:45 +01003061
3062struct ffa_value api_ffa_notification_set(
3063 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3064 ffa_notifications_bitmap_t notifications, struct vcpu *current)
3065{
3066 struct ffa_value ret;
3067 struct vm_locked receiver_locked;
3068
3069 /*
3070 * Check if is per-vCPU or global, and extracting vCPU ID according
3071 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
3072 */
3073 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3074 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
3075
J-Alvesaa79c012021-07-09 14:29:45 +01003076 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
3077 receiver_vm_id)) {
3078 dlog_verbose("Invalid use of notifications set interface.\n");
3079 return ffa_error(FFA_INVALID_PARAMETERS);
3080 }
3081
3082 if (notifications == 0U) {
3083 dlog_verbose("No notifications have been specified.\n");
3084 return ffa_error(FFA_INVALID_PARAMETERS);
3085 }
3086
J-Alvesde7bd2f2021-09-09 19:54:35 +01003087 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
3088 flags, notifications, &ret)) {
3089 return ret;
3090 }
3091
J-Alvesaa79c012021-07-09 14:29:45 +01003092 /*
3093 * This check assumes receiver is the current VM, and has been enforced
3094 * by 'plat_ffa_is_notification_set_valid'.
3095 */
3096 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3097
3098 if (receiver_locked.vm == NULL) {
3099 dlog_verbose("Receiver ID is not valid.\n");
3100 return ffa_error(FFA_INVALID_PARAMETERS);
3101 }
3102
J-Alves09ff9d82021-11-02 11:55:20 +00003103 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003104 dlog_verbose("Receiver's notifications not enabled.\n");
3105 ret = ffa_error(FFA_DENIED);
3106 goto out;
3107 }
3108
3109 /*
3110 * If notifications are not bound to the sender, they wouldn't be
3111 * enabled either for the receiver.
3112 */
3113 if (!vm_notifications_validate_binding(
3114 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3115 sender_vm_id, notifications, is_per_vcpu)) {
3116 dlog_verbose("Notifications bindings not valid.\n");
3117 ret = ffa_error(FFA_DENIED);
3118 goto out;
3119 }
3120
3121 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3122 dlog_verbose("Invalid VCPU ID!\n");
3123 ret = ffa_error(FFA_INVALID_PARAMETERS);
3124 goto out;
3125 }
3126
J-Alves7461ef22021-10-18 17:21:33 +01003127 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00003128 vm_notifications_partition_set_pending(
3129 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), notifications,
3130 vcpu_id, is_per_vcpu);
3131
J-Alvesaa79c012021-07-09 14:29:45 +01003132 dlog_verbose("Set the notifications: %x.\n", notifications);
3133
J-Alves13394022021-06-30 13:48:49 +01003134 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3135 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3136 vcpu_index(current));
3137 plat_ffa_sri_trigger_not_delayed(current->cpu);
3138 } else {
3139 plat_ffa_sri_state_set(DELAYED);
3140 }
J-Alvesaa79c012021-07-09 14:29:45 +01003141
J-Alves13394022021-06-30 13:48:49 +01003142 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003143out:
3144 vm_unlock(&receiver_locked);
3145
3146 return ret;
3147}
3148
3149static struct ffa_value api_ffa_notification_get_success_return(
3150 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3151 ffa_notifications_bitmap_t from_framework)
3152{
3153 return (struct ffa_value){
3154 .func = FFA_SUCCESS_32,
3155 .arg1 = 0U,
3156 .arg2 = (uint32_t)from_sp,
3157 .arg3 = (uint32_t)(from_sp >> 32),
3158 .arg4 = (uint32_t)from_vm,
3159 .arg5 = (uint32_t)(from_vm >> 32),
3160 .arg6 = (uint32_t)from_framework,
3161 .arg7 = (uint32_t)(from_framework >> 32),
3162 };
3163}
3164
3165struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3166 ffa_vcpu_index_t vcpu_id,
3167 uint32_t flags, struct vcpu *current)
3168{
J-Alves663682a2022-03-25 13:56:51 +00003169 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01003170 ffa_notifications_bitmap_t sp_notifications = 0;
3171 ffa_notifications_bitmap_t vm_notifications = 0;
3172 struct vm_locked receiver_locked;
3173 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01003174 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
3175 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
3176 FFA_NOTIFICATION_FLAG_BITMAP_SP |
3177 FFA_NOTIFICATION_FLAG_BITMAP_VM);
3178
3179 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
3180 if ((flags & flags_mbz) != 0U) {
3181 dlog_verbose(
3182 "Invalid flags bit(s) set in notifications get. [31:4] "
3183 "MBZ(%x)\n",
3184 flags);
3185 return ffa_error(FFA_INVALID_PARAMETERS);
3186 }
J-Alvesaa79c012021-07-09 14:29:45 +01003187
3188 /*
J-Alvesfc95a302022-04-22 14:18:23 +01003189 * Following check should capture wrong uses of the interface,
3190 * depending on whether Hafnium is SPMC or hypervisor. On the
3191 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01003192 */
J-Alvesfc95a302022-04-22 14:18:23 +01003193 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
3194 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003195 dlog_verbose("Invalid use of notifications get interface.\n");
3196 return ffa_error(FFA_INVALID_PARAMETERS);
3197 }
3198
3199 /*
3200 * This check assumes receiver is the current VM, and has been enforced
3201 * by `plat_ffa_is_notifications_get_valid`.
3202 */
3203 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3204
3205 /*
3206 * `plat_ffa_is_notifications_get_valid` ensures following is never
3207 * true.
3208 */
3209 CHECK(receiver_locked.vm != NULL);
3210
3211 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3212 (receiver_locked.vm->vcpu_count != 1 &&
3213 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003214 dlog_verbose(
3215 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3216 vcpu_id, receiver_locked.vm->vcpu_count,
3217 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003218 ret = ffa_error(FFA_INVALID_PARAMETERS);
3219 goto out;
3220 }
3221
3222 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003223 if (!plat_ffa_notifications_get_from_sp(
3224 receiver_locked, vcpu_id, &sp_notifications,
3225 &ret)) {
3226 dlog_verbose("Failed to get notifications from sps.");
3227 goto out;
3228 }
J-Alvesaa79c012021-07-09 14:29:45 +01003229 }
3230
3231 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00003232 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01003233 receiver_locked, true, vcpu_id);
3234 }
3235
J-Alvesd605a092022-03-28 14:20:48 +01003236 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
3237 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
3238 if (!plat_ffa_notifications_get_framework_notifications(
3239 receiver_locked, &framework_notifications, flags,
3240 vcpu_id, &ret)) {
3241 dlog_verbose(
3242 "Failed to get notifications from "
3243 "framework.\n");
3244 goto out;
3245 }
3246 }
3247
J-Alves663682a2022-03-25 13:56:51 +00003248 ret = api_ffa_notification_get_success_return(
3249 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01003250
J-Alvesfe23ebe2021-10-13 16:07:07 +01003251 /*
3252 * If there are no more pending notifications, change `sri_state` to
3253 * handled.
3254 */
3255 if (vm_is_notifications_pending_count_zero()) {
3256 plat_ffa_sri_state_set(HANDLED);
3257 }
3258
J-Alves6e2abc62021-12-02 14:58:56 +00003259 if (!receiver_locked.vm->el0_partition &&
3260 !vm_are_global_notifications_pending(receiver_locked)) {
3261 vm_notifications_set_npi_injected(receiver_locked, false);
3262 }
3263
J-Alvesaa79c012021-07-09 14:29:45 +01003264out:
3265 vm_unlock(&receiver_locked);
3266
3267 return ret;
3268}
J-Alvesc8e8a222021-06-08 17:33:52 +01003269
3270/**
3271 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3272 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3273 */
3274static struct ffa_value api_ffa_notification_info_get_success_return(
3275 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003276 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003277{
3278 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3279
3280 /*
3281 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3282 * hold the list of ids.
3283 */
3284 memcpy_s(&ret.arg3,
3285 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3286 sizeof(ids[0]) * ids_count);
3287
3288 /*
3289 * According to the spec x2 should have:
3290 * - Bit flagging if there are more notifications pending;
3291 * - The total number of elements (i.e. total list size);
3292 * - The number of VCPU IDs within each VM specific list.
3293 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003294 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3295 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3296 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003297
3298 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3299 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3300
3301 for (unsigned int i = 0; i < lists_count; i++) {
3302 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3303 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3304 }
3305
3306 return ret;
3307}
3308
3309struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3310{
3311 /*
3312 * Following set of variables should be populated with the return info.
3313 * At a successfull handling of this interface, they should be used
3314 * to populate the 'ret' structure in accordance to the table 17.29
3315 * of the FF-A v1.1 Beta0 specification.
3316 */
3317 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3318 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3319 uint32_t lists_count = 0;
3320 uint32_t ids_count = 0;
3321 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003322 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003323
3324 /*
3325 * This interface can only be called at NS virtual/physical FF-A
3326 * instance by the endpoint implementing the primary scheduler and the
3327 * Hypervisor/OS kernel.
3328 * In the SPM, following check passes if call has been forwarded from
3329 * the hypervisor.
3330 */
3331 if (current->vm->id != HF_PRIMARY_VM_ID) {
3332 dlog_verbose(
3333 "Only the receiver's scheduler can use this "
3334 "interface\n");
3335 return ffa_error(FFA_NOT_SUPPORTED);
3336 }
3337
J-Alvesca058c22021-09-10 14:02:07 +01003338 /*
3339 * Forward call to the other world, and fill the arrays used to assemble
3340 * return.
3341 */
3342 plat_ffa_notification_info_get_forward(
3343 ids, &ids_count, lists_sizes, &lists_count,
3344 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3345
3346 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3347
J-Alvesc8e8a222021-06-08 17:33:52 +01003348 /* Get notifications' info from this world */
3349 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3350 ++index) {
3351 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3352
3353 list_is_full = vm_notifications_info_get(
3354 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3355 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3356
3357 vm_unlock(&vm_locked);
3358 }
3359
3360 if (!list_is_full) {
3361 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003362 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003363 ids, &ids_count, lists_sizes, &lists_count,
3364 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3365 }
3366
3367 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003368 dlog_verbose(
3369 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003370 result = ffa_error(FFA_NO_DATA);
3371 } else {
3372 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003373 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003374 }
3375
J-Alvesfe23ebe2021-10-13 16:07:07 +01003376 plat_ffa_sri_state_set(HANDLED);
3377
J-Alves13394022021-06-30 13:48:49 +01003378 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003379}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003380
3381struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3382{
3383 struct vm_locked vm_locked;
3384 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3385 bool mode_ret = false;
3386 uint32_t mode = 0;
3387
3388 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3389 return ffa_error(FFA_NOT_SUPPORTED);
3390 }
3391
3392 if (!(current->vm->el0_partition)) {
3393 return ffa_error(FFA_DENIED);
3394 }
3395
3396 vm_locked = vm_lock(current->vm);
3397
3398 /*
3399 * mm_get_mode is used to check if the given base_addr page is already
3400 * mapped. If the page is unmapped, return error. If the page is mapped
3401 * appropriate attributes are returned to the caller. Note that
3402 * mm_get_mode returns true if the address is in the valid VA range as
3403 * supported by the architecture and MMU configurations, as opposed to
3404 * whether a page is mapped or not. For a page to be known as mapped,
3405 * the API must return true AND the returned mode must not have
3406 * MM_MODE_INVALID set.
3407 */
3408 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3409 va_add(base_addr, PAGE_SIZE), &mode);
3410 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3411 ret = ffa_error(FFA_INVALID_PARAMETERS);
3412 goto out;
3413 }
3414
3415 /* No memory should be marked RWX */
3416 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3417 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3418
3419 /*
3420 * S-EL0 partitions are expected to have all their pages marked as
3421 * non-global.
3422 */
3423 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3424 (MM_MODE_NG | MM_MODE_USER));
3425
3426 if (mode & MM_MODE_W) {
3427 /* No memory should be writeable but not readable. */
3428 CHECK(mode & MM_MODE_R);
3429 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3430 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3431 } else if (mode & MM_MODE_R) {
3432 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3433 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3434 if (!(mode & MM_MODE_X)) {
3435 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3436 }
3437 }
3438out:
3439 vm_unlock(&vm_locked);
3440 return ret;
3441}
3442
3443struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3444 uint32_t mem_perm, struct vcpu *current)
3445{
3446 struct vm_locked vm_locked;
3447 struct ffa_value ret;
3448 bool mode_ret = false;
3449 uint32_t original_mode;
3450 uint32_t new_mode;
3451 struct mpool local_page_pool;
3452
3453 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3454 return ffa_error(FFA_NOT_SUPPORTED);
3455 }
3456
3457 if (!(current->vm->el0_partition)) {
3458 return ffa_error(FFA_DENIED);
3459 }
3460
3461 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3462 return ffa_error(FFA_INVALID_PARAMETERS);
3463 }
3464
3465 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3466 (mem_perm != FFA_MEM_PERM_RX)) {
3467 return ffa_error(FFA_INVALID_PARAMETERS);
3468 }
3469
3470 /*
3471 * Create a local pool so any freed memory can't be used by another
3472 * thread. This is to ensure the original mapping can be restored if any
3473 * stage of the process fails.
3474 */
3475 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3476
3477 vm_locked = vm_lock(current->vm);
3478
3479 /*
3480 * All regions accessible by the partition are mapped during boot. If we
3481 * cannot get a successful translation for the page range, the request
3482 * to change permissions is rejected.
3483 * mm_get_mode is used to check if the given address range is already
3484 * mapped. If the range is unmapped, return error. If the range is
3485 * mapped appropriate attributes are returned to the caller. Note that
3486 * mm_get_mode returns true if the address is in the valid VA range as
3487 * supported by the architecture and MMU configurations, as opposed to
3488 * whether a page is mapped or not. For a page to be known as mapped,
3489 * the API must return true AND the returned mode must not have
3490 * MM_MODE_INVALID set.
3491 */
3492
3493 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3494 va_add(base_addr, page_count * PAGE_SIZE),
3495 &original_mode);
3496 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3497 ret = ffa_error(FFA_INVALID_PARAMETERS);
3498 goto out;
3499 }
3500
3501 /* Device memory cannot be marked as executable */
3502 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3503 ret = ffa_error(FFA_INVALID_PARAMETERS);
3504 goto out;
3505 }
3506
3507 new_mode = MM_MODE_USER | MM_MODE_NG;
3508
3509 if (mem_perm == FFA_MEM_PERM_RW) {
3510 new_mode |= MM_MODE_R | MM_MODE_W;
3511 } else if (mem_perm == FFA_MEM_PERM_RX) {
3512 new_mode |= MM_MODE_R | MM_MODE_X;
3513 } else if (mem_perm == FFA_MEM_PERM_RO) {
3514 new_mode |= MM_MODE_R;
3515 }
3516
3517 /*
3518 * Safe to re-map memory, since we know the requested permissions are
3519 * valid, and the memory requested to be re-mapped is also valid.
3520 */
3521 if (!mm_identity_prepare(
3522 &vm_locked.vm->ptable, pa_from_va(base_addr),
3523 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3524 new_mode, &local_page_pool)) {
3525 /*
3526 * Defrag the table into the local page pool.
3527 * mm_identity_prepare could have allocated or freed pages to
3528 * split blocks or tables etc.
3529 */
3530 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3531
3532 /*
3533 * Guaranteed to succeed mapping with old mode since the mapping
3534 * with old mode already existed and we have a local page pool
3535 * that should have sufficient memory to go back to the original
3536 * state.
3537 */
3538 CHECK(mm_identity_prepare(
3539 &vm_locked.vm->ptable, pa_from_va(base_addr),
3540 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3541 original_mode, &local_page_pool));
3542 mm_identity_commit(
3543 &vm_locked.vm->ptable, pa_from_va(base_addr),
3544 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3545 original_mode, &local_page_pool);
3546
3547 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3548 ret = ffa_error(FFA_NO_MEMORY);
3549 goto out;
3550 }
3551
3552 mm_identity_commit(
3553 &vm_locked.vm->ptable, pa_from_va(base_addr),
3554 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3555 &local_page_pool);
3556
3557 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3558
3559out:
3560 mpool_fini(&local_page_pool);
3561 vm_unlock(&vm_locked);
3562
3563 return ret;
3564}
Maksims Svecovs71b76702022-05-20 15:32:58 +01003565
3566/**
3567 * Helper function for FFA_CONSOLE_LOG ABI.
3568 * Writes number of characters to a given VM buffer.
3569 */
3570static rsize_t arg_to_char_helper(struct vm_locked from_locked,
3571 const uint64_t src, rsize_t src_size,
3572 rsize_t to_write)
3573{
3574 bool flush = false;
3575 char c;
3576 rsize_t size = src_size < to_write ? src_size : to_write;
3577 rsize_t written = 0;
3578
3579 if (size == 0) {
3580 return 0;
3581 }
3582
3583 while (written < size) {
3584 c = ((char *)&src)[written++];
3585 if (c == '\n' || c == '\0') {
3586 flush = true;
3587 } else {
3588 from_locked.vm->log_buffer
3589 [from_locked.vm->log_buffer_length++] = c;
3590 flush = (from_locked.vm->log_buffer_length ==
3591 LOG_BUFFER_SIZE);
3592 }
3593
3594 if (flush) {
3595 dlog_flush_vm_buffer(from_locked.vm->id,
3596 from_locked.vm->log_buffer,
3597 from_locked.vm->log_buffer_length);
3598 from_locked.vm->log_buffer_length = 0;
3599 }
3600 }
3601
3602 return written;
3603}
3604
3605/**
3606 * Implements FFA_CONSOLE_LOG buffered logging.
3607 */
3608struct ffa_value api_ffa_console_log(const struct ffa_value args,
3609 struct vcpu *current)
3610{
3611 struct vm *vm = current->vm;
3612 struct vm_locked vm_locked;
3613 size_t chars_in_param = args.func == FFA_CONSOLE_LOG_32
3614 ? sizeof(uint32_t)
3615 : sizeof(uint64_t);
3616 size_t total_to_write = args.arg1;
3617
3618 if (total_to_write == 0 || total_to_write > chars_in_param * 6) {
3619 return ffa_error(FFA_INVALID_PARAMETERS);
3620 }
3621
3622 vm_locked = vm_lock(vm);
3623
3624 total_to_write -= arg_to_char_helper(vm_locked, args.arg2,
3625 chars_in_param, total_to_write);
3626 total_to_write -= arg_to_char_helper(vm_locked, args.arg3,
3627 chars_in_param, total_to_write);
3628 total_to_write -= arg_to_char_helper(vm_locked, args.arg4,
3629 chars_in_param, total_to_write);
3630 total_to_write -= arg_to_char_helper(vm_locked, args.arg5,
3631 chars_in_param, total_to_write);
3632 total_to_write -= arg_to_char_helper(vm_locked, args.arg6,
3633 chars_in_param, total_to_write);
3634 arg_to_char_helper(vm_locked, args.arg7, chars_in_param,
3635 total_to_write);
3636
3637 vm_unlock(&vm_locked);
3638
3639 return (struct ffa_value){.func = FFA_SUCCESS_32};
3640}