blob: 8daf10d0108ed48c132e4f9125d2b0ff81afd3e9 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Federico Recanati25053ee2022-03-14 15:01:53 +01002 * Copyright 2022 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +000012#include "hf/arch/ffa.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000016#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020017#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000018
Andrew Scull877ae4b2019-07-02 12:52:33 +010019#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000020#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010021#include "hf/ffa_internal.h"
22#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000023#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010024#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010025#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010027#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010028#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "hf/vm.h"
30
Andrew Scullf35a5c92018-08-07 18:09:46 +010031#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032#include "vmapi/hf/ffa.h"
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 */
Olivier Deprezd8e18882022-07-15 14:46:41 +0200131static struct vcpu *api_switch_to_primary(struct vcpu *current,
132 struct ffa_value primary_ret,
133 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100134{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000135 /*
136 * If the secondary is blocked but has a timer running, sleep until the
137 * timer fires rather than indefinitely.
138 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100139 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100140 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
141 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100142 if (arch_timer_enabled_current()) {
143 uint64_t remaining_ns =
144 arch_timer_remaining_ns_current();
145
146 if (remaining_ns == 0) {
147 /*
148 * Timer is pending, so the current vCPU should
149 * be run again right away.
150 */
Andrew Walbran7e824602020-10-22 16:51:40 +0100151 primary_ret = (struct ffa_value){
152 .func = FFA_INTERRUPT_32};
153
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100154 } else {
155 primary_ret.arg2 = remaining_ns;
156 }
157 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100158 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100159 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000160 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100161 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000162
163 default:
164 /* Do nothing. */
165 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000166 }
167
J-Alvesfe7f7372020-11-09 11:32:12 +0000168 return api_switch_to_vm(current, primary_ret, secondary_state,
169 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100170}
171
172/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200173 * Choose next vCPU to run to be the counterpart vCPU in the other
174 * world (run the normal world if currently running in the secure
175 * world). Set current vCPU state to the given vcpu_state parameter.
176 * Set FF-A return values to the target vCPU in the other world.
177 *
178 * Called in context of a direct message response from a secure
179 * partition to a VM.
180 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100181struct vcpu *api_switch_to_other_world(struct vcpu *current,
182 struct ffa_value other_world_ret,
183 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200184{
J-Alvesfe7f7372020-11-09 11:32:12 +0000185 return api_switch_to_vm(current, other_world_ret, vcpu_state,
186 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200187}
188
189/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100190 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
191 * registers the `from` VM to be notified when it becomes available.
192 */
193static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
194{
195 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
196 to.vm->mailbox.recv == NULL) {
197 /*
198 * Fail if the receiver isn't currently ready to receive data,
199 * setting up for notification if requested.
200 */
201 if (notify) {
202 struct wait_entry *entry =
203 vm_get_wait_entry(from, to.vm->id);
204
205 /* Append waiter only if it's not there yet. */
206 if (list_empty(&entry->wait_links)) {
207 list_append(&to.vm->mailbox.waiter_list,
208 &entry->wait_links);
209 }
210 }
211
212 return true;
213 }
214
215 return false;
216}
217
218/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000219 * Returns true if the given vCPU is executing in context of an
220 * FFA_MSG_SEND_DIRECT_REQ invocation.
221 */
222static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
223{
224 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
225}
226
227/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100228 * Returns true if the VM owning the given vCPU is supporting managed exit and
229 * the vCPU is currently processing a managed exit.
230 */
231static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
232{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100233 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100234 vcpu_locked.vcpu->processing_managed_exit);
235}
236
237/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000238 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000239 */
240struct vcpu *api_preempt(struct vcpu *current)
241{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100242 struct ffa_value ret = {
243 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200244 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000245 };
246
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500247 return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED);
Andrew Scull33fecd32019-01-08 14:48:27 +0000248}
249
250/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000251 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000252 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100253 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100254struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100255{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100256 struct ffa_value ret = {
257 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
258 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100259 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000260
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000261 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100262 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100263}
264
265/**
Andrew Walbran33645652019-04-15 12:29:31 +0100266 * Puts the current vCPU in off mode, and returns to the primary VM.
267 */
268struct vcpu *api_vcpu_off(struct vcpu *current)
269{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100270 struct ffa_value ret = {
271 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
272 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100273 };
274
275 /*
276 * Disable the timer, so the scheduler doesn't get told to call back
277 * based on it.
278 */
279 arch_timer_disable_current();
280
281 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
282}
283
284/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500285 * The current vCPU is blocked on some resource and needs to relinquish
286 * control back to the execution context of the endpoint that originally
287 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000288 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000289struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000290{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000291 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
292 struct vcpu_locked current_locked;
293 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000294
295 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000296 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000297 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000298 }
299
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000300 current_locked = vcpu_lock(current);
301 is_direct_request_ongoing =
302 is_ffa_direct_msg_request_ongoing(current_locked);
303 vcpu_unlock(&current_locked);
304
305 if (is_direct_request_ongoing) {
306 return ffa_error(FFA_DENIED);
307 }
308
309 *next = api_switch_to_primary(
310 current,
311 (struct ffa_value){.func = FFA_YIELD_32,
312 .arg1 = ffa_vm_vcpu(current->vm->id,
313 vcpu_index(current))},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500314 VCPU_STATE_BLOCKED);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000315
316 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000317}
318
319/**
Andrew Walbran33645652019-04-15 12:29:31 +0100320 * Switches to the primary so that it can switch to the target, or kick it if it
321 * is already running on a different physical CPU.
322 */
323struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
324{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100325 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100326 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
328 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100329 };
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500330 return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED);
Andrew Walbran33645652019-04-15 12:29:31 +0100331}
332
333/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000334 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000335 */
336struct vcpu *api_abort(struct vcpu *current)
337{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000339
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100340 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000341 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000342
343 if (current->vm->id == HF_PRIMARY_VM_ID) {
344 /* TODO: what to do when the primary aborts? */
345 for (;;) {
346 /* Do nothing. */
347 }
348 }
349
350 atomic_store_explicit(&current->vm->aborting, true,
351 memory_order_relaxed);
352
353 /* TODO: free resources once all vCPUs abort. */
354
Andrew Sculld6ee1102019-04-05 22:12:42 +0100355 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000356}
357
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000358/*
359 * Format the partition info descriptors according to the version supported
360 * by the endpoint and return the size of the array created.
361 */
362static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000363 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000364 uint32_t vm_count)
365{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000366 struct vm *vm = vm_locked.vm;
367 uint32_t version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100368 uint32_t partition_info_size;
369 uint32_t buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000370
Daniel Boulby191b5f02022-02-17 17:26:14 +0000371 if (msg_receiver_busy(vm_locked, NULL, false)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000372 /*
373 * Can't retrieve memory information if the mailbox is not
374 * available.
375 */
376 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000377 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000378 }
379
Daniel Boulby191b5f02022-02-17 17:26:14 +0000380 if (version == MAKE_FFA_VERSION(1, 0)) {
381 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
382
Daniel Boulby835e1592022-05-30 17:38:51 +0100383 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
384 buffer_size = partition_info_size * vm_count;
385 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000386 dlog_error(
387 "Partition information does not fit in the "
388 "VM's RX "
389 "buffer.\n");
390 return ffa_error(FFA_NO_MEMORY);
391 }
392
393 for (uint32_t i = 0; i < vm_count; i++) {
394 /*
395 * Populate the VM's RX buffer with the partition
396 * information.
397 */
398 recv_mailbox[i].vm_id = partitions[i].vm_id;
399 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
400 recv_mailbox[i].properties = partitions[i].properties;
401 }
402
403 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100404 partition_info_size = sizeof(struct ffa_partition_info);
405 buffer_size = partition_info_size * vm_count;
406 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000407 dlog_error(
408 "Partition information does not fit in the "
409 "VM's RX "
410 "buffer.\n");
411 return ffa_error(FFA_NO_MEMORY);
412 }
413
414 /* Populate the VM's RX buffer with the partition information.
415 */
Daniel Boulby835e1592022-05-30 17:38:51 +0100416 memcpy_s(vm->mailbox.recv, HF_MAILBOX_SIZE, partitions,
417 buffer_size);
Daniel Boulby191b5f02022-02-17 17:26:14 +0000418 }
419
Daniel Boulby835e1592022-05-30 17:38:51 +0100420 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000421
422 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000423 vm->mailbox.recv_sender = HF_VM_ID_BASE;
424 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
425 vm->mailbox.state = MAILBOX_STATE_READ;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000426
Daniel Boulby835e1592022-05-30 17:38:51 +0100427 /*
428 * Return the count of partition information descriptors in w2
429 * and the size of the descriptors in w3.
430 */
431 return (struct ffa_value){.func = FFA_SUCCESS_32,
432 .arg2 = vm_count,
433 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000434}
435
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100436struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000437 const struct ffa_uuid *uuid,
438 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100439{
440 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100441 ffa_vm_count_t vm_count = 0;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000442 bool count_flag = (flags && FFA_PARTITION_COUNT_FLAG_MASK) ==
443 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100444 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100445 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000446 struct vm_locked vm_locked;
447 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100448
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000449 /* Bits 31:1 Must Be Zero */
450 if ((flags & ~FFA_PARTITION_COUNT_FLAG) != 0) {
451 return ffa_error(FFA_INVALID_PARAMETERS);
452 }
453
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100454 /*
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000455 * No need to count if we are returning the number of paritions as we
456 * already know this.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100457 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000458 if (uuid_is_null && count_flag) {
459 vm_count = vm_get_count();
460 } else {
461 /*
462 * Iterate through the VMs to find the ones with a matching
463 * UUID. A Null UUID retrieves information for all VMs.
464 */
465 for (uint16_t index = 0; index < vm_get_count(); ++index) {
466 struct vm *vm = vm_find_index(index);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100467
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000468 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
469 uint16_t array_index = vm_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100470
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000471 ++vm_count;
472 if (count_flag) {
473 continue;
474 }
475
476 partitions[array_index].vm_id = vm->id;
477 partitions[array_index].vcpu_count =
478 vm->vcpu_count;
479 partitions[array_index].properties =
480 plat_ffa_partition_properties(
481 current_vm->id, vm);
482 partitions[array_index].properties |=
483 vm_are_notifications_enabled(vm)
484 ? FFA_PARTITION_NOTIFICATION
485 : 0;
Daniel Boulbyce541842022-05-31 15:54:31 +0100486 if (uuid_is_null) {
487 partitions[array_index].uuid = vm->uuid;
488 }
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000489 }
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100490 }
491 }
492
Olivier Depreze562e542020-06-11 17:31:54 +0200493 /* If UUID is Null vm_count must not be zero at this stage. */
494 CHECK(!uuid_is_null || vm_count != 0);
495
496 /*
497 * When running the Hypervisor:
498 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
499 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000500 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200501 * a secure partition and the query is forwarded to the SPMC.
502 * When running the SPMC:
503 * - If UUID is non-Null and vm_count is zero it means there is no such
504 * partition identified in the system.
505 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000506 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200507
508 /*
509 * Unrecognized UUID: does not match any of the VMs (or SPs)
510 * and is not Null.
511 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100512 if (vm_count == 0) {
513 return ffa_error(FFA_INVALID_PARAMETERS);
514 }
515
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000516 /*
517 * If the count flag is set we don't need to return the partition info
518 * descriptors.
519 */
520 if (count_flag) {
521 return (struct ffa_value){.func = FFA_SUCCESS_32,
522 .arg2 = vm_count};
523 }
524
Daniel Boulby191b5f02022-02-17 17:26:14 +0000525 vm_locked = vm_lock(current_vm);
526 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
527 vm_count);
528 vm_unlock(&vm_locked);
529 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100530}
531
Andrew Scull9726c252019-01-23 13:44:19 +0000532/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000533 * Returns the ID of the VM.
534 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100535struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000536{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100537 return (struct ffa_value){.func = FFA_SUCCESS_32,
538 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000539}
540
541/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200542 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
543 * FF-A instances.
544 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000545 */
546struct ffa_value api_ffa_spm_id_get(void)
547{
J-Alves3829fc02021-03-18 12:49:18 +0000548#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
549 /*
550 * Return the SPMC ID that was fetched during FF-A
551 * initialization.
552 */
553 return (struct ffa_value){.func = FFA_SUCCESS_32,
554 .arg2 = arch_ffa_spmc_id_get()};
555#else
556 return ffa_error(FFA_NOT_SUPPORTED);
557#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000558}
559
560/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000561 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000562 * function to indicate that register state for the given vCPU has been saved
563 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000564 */
565void api_regs_state_saved(struct vcpu *vcpu)
566{
567 sl_lock(&vcpu->lock);
568 vcpu->regs_available = true;
569 sl_unlock(&vcpu->lock);
570}
571
572/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000573 * Retrieves the next waiter and removes it from the wait list if the VM's
574 * mailbox is in a writable state.
575 */
576static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
577{
578 struct wait_entry *entry;
579 struct vm *vm = locked_vm.vm;
580
Andrew Sculld6ee1102019-04-05 22:12:42 +0100581 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000582 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
583 /* The mailbox is not writable or there are no waiters. */
584 return NULL;
585 }
586
587 /* Remove waiter from the wait list. */
588 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
589 wait_links);
590 list_remove(&entry->wait_links);
591 return entry;
592}
593
594/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000595 * Assuming that the arguments have already been checked by the caller, injects
596 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
597 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
598 * is next run, which is up to the scheduler.
599 *
600 * Returns:
601 * - 0 on success if no further action is needed.
602 * - 1 if it was called by the primary VM and the primary VM now needs to wake
603 * up or kick the target vCPU.
604 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100605int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
606 uint32_t intid, struct vcpu *current,
607 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000608{
Manish Pandey35e452f2021-02-18 21:36:34 +0000609 struct vcpu *target_vcpu = target_locked.vcpu;
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100610 uint32_t intid_index = INTID_INDEX(intid);
611 uint32_t intid_mask = INTID_MASK(1U, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000612 int64_t ret = 0;
613
Andrew Walbran508e63c2018-12-20 17:02:37 +0000614 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000615 * We only need to change state and (maybe) trigger a virtual interrupt
616 * if it is enabled and was not previously pending. Otherwise we can
617 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000618 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000619 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
620 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000621 intid_mask)) {
622 goto out;
623 }
624
625 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000626 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100627 intid_mask) == INTID_MASK(INTERRUPT_TYPE_IRQ, intid)) {
Manish Pandey35e452f2021-02-18 21:36:34 +0000628 vcpu_irq_count_increment(target_locked);
629 } else {
630 vcpu_fiq_count_increment(target_locked);
631 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000632
633 /*
634 * Only need to update state if there was not already an
635 * interrupt enabled and pending.
636 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000637 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000638 goto out;
639 }
640
Andrew Walbran508e63c2018-12-20 17:02:37 +0000641 if (current->vm->id == HF_PRIMARY_VM_ID) {
642 /*
643 * If the call came from the primary VM, let it know that it
644 * should run or kick the target vCPU.
645 */
646 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000647 } else if (current != target_vcpu && next != NULL) {
648 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000649 }
650
651out:
652 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000653 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000654
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200655 return ret;
656}
657
658/* Wrapper to internal_interrupt_inject with locking of target vCPU */
659static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
660 uint32_t intid, struct vcpu *current,
661 struct vcpu **next)
662{
663 int64_t ret;
664 struct vcpu_locked target_locked;
665
666 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100667 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200668 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000669
670 return ret;
671}
672
673/**
Federico Recanati25053ee2022-03-14 15:01:53 +0100674 * Constructs the return value from a successful FFA_MSG_POLL or
675 * FFA_MSG_WAIT call.
676 *
677 * Note: FFA_MSG_POLL is deprecated in FF-A v1.1 and should not be used with
678 * FFA_MSG_SEND2; no check is done on mixing FF-A v1.0 and FF-A v1.1 indirect
679 * message protocols.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100680 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100681static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100682{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000683 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100684 case FFA_MSG_SEND_32:
685 return (struct ffa_value){
686 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000687 .arg1 = (receiver->mailbox.recv_sender << 16) |
688 receiver->id,
689 .arg3 = receiver->mailbox.recv_size};
Federico Recanati25053ee2022-03-14 15:01:53 +0100690 case FFA_MSG_SEND2_32:
691 return (struct ffa_value){
692 .func = FFA_RUN_32,
693 /*
694 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
695 * Retrieving vCPU requires a rework of the function,
696 * while receiver ID must be set because it's checked by
697 * other APIs (eg: FFA_NOTIFICATION_GET).
698 */
699 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000700 default:
701 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000702 dlog_error("Tried to return an invalid message function %#x\n",
703 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100704 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000705 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100706}
707
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600708struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
709 struct ffa_value *args)
710{
711 struct ffa_value ret;
712
713 if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U ||
714 args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
715 args->arg7 != 0U) {
716 return ffa_error(FFA_INVALID_PARAMETERS);
717 }
718
719 if (plat_ffa_msg_wait_prepare(current, next, &ret)) {
720 return ret;
721 }
722
723 return api_ffa_msg_recv(true, current, next);
724}
725
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100726/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000727 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000728 * value needs to be forced onto the vCPU.
729 */
J-Alves6e2abc62021-12-02 14:58:56 +0000730static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100731 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000732{
Max Shvetsov40108e72020-08-27 12:39:50 +0100733 struct vcpu_locked vcpu_locked;
734 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000735 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500736 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
J-Alves6e2abc62021-12-02 14:58:56 +0000737 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000738
Andrew Scullb06d1752019-02-04 10:15:48 +0000739 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000740 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000741 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100742 * The VM lock is not needed in the common case so it must only be taken
743 * when it is going to be needed. This ensures there are no inter-vCPU
744 * dependencies in the common run case meaning the sensitive context
745 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000746 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100747 vcpu_locked = vcpu_lock(vcpu);
748
749#if SECURE_WORLD == 1
J-Alves7ac49052022-02-08 17:20:53 +0000750 bool is_vcpu_reset_and_start = vcpu_secondary_reset_and_start(
751 vcpu_locked, vcpu->vm->secondary_ep, 0);
752 if (is_vcpu_reset_and_start) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100753 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
754 __func__, vcpu->vm->id, current->cpu->id);
755 }
756
757#endif
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000758 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000759 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
760 (!vcpu->vm->el0_partition &&
761 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
762 vcpu->state == VCPU_STATE_BLOCKED ||
763 vcpu->state == VCPU_STATE_PREEMPTED));
764
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000765 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100766 vcpu_unlock(&vcpu_locked);
767 vm_locked = vm_lock(vcpu->vm);
768 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000769 }
770
771 /*
772 * If the vCPU is already running somewhere then we can't run it here
773 * simultaneously. While it is actually running then the state should be
774 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
775 * stops running but while Hafnium is in the process of switching back
776 * to the primary there will be a brief period while the state has been
777 * updated but `regs_available` is still false (until
778 * `api_regs_state_saved` is called). We can't start running it again
779 * until this has finished, so count this state as still running for the
780 * purposes of this check.
781 */
782 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
783 /*
784 * vCPU is running on another pCPU.
785 *
786 * It's okay not to return the sleep duration here because the
787 * other physical CPU that is currently running this vCPU will
788 * return the sleep duration if needed.
789 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100790 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000791 ret = false;
792 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000793 }
Andrew Scull9726c252019-01-23 13:44:19 +0000794
795 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100796 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100797 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000798 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100799 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000800 }
801 ret = false;
802 goto out;
803 }
804
Andrew Walbran508e63c2018-12-20 17:02:37 +0000805 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100806 case VCPU_STATE_RUNNING:
807 case VCPU_STATE_OFF:
808 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000809 ret = false;
810 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000811
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500812 case VCPU_STATE_WAITING:
813 /*
814 * An initial FFA_RUN is necessary for secondary VM/SP to reach
815 * the message wait loop.
816 */
817 if (!vcpu->is_bootstrapped) {
818 vcpu->is_bootstrapped = true;
819 break;
820 }
821
J-Alves6e2abc62021-12-02 14:58:56 +0000822 assert(need_vm_lock == true);
823 if (!vm_locked.vm->el0_partition &&
824 plat_ffa_inject_notification_pending_interrupt(
825 vcpu_locked, current, vm_locked)) {
826 break;
827 }
828
Andrew Scullb06d1752019-02-04 10:15:48 +0000829 /*
830 * A pending message allows the vCPU to run so the message can
831 * be delivered directly.
832 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100833 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100834 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100835 ffa_msg_recv_return(vcpu->vm));
Federico Recanati25053ee2022-03-14 15:01:53 +0100836 if (vcpu->vm->mailbox.recv_func == FFA_MSG_SEND_32) {
837 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
838 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000839 break;
840 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500841
842 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
843 break;
844 }
845
846 if (arch_timer_enabled(&vcpu->regs)) {
847 timer_remaining_ns =
848 arch_timer_remaining_ns(&vcpu->regs);
849 if (timer_remaining_ns == 0) {
850 break;
851 }
852 } else {
853 dlog_verbose("Timer disabled\n");
854 }
855 run_ret->func = FFA_MSG_WAIT_32;
856 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
857 run_ret->arg2 = timer_remaining_ns;
858 ret = false;
859 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100860 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +0000861 if (need_vm_lock &&
862 plat_ffa_inject_notification_pending_interrupt(
863 vcpu_locked, current, vm_locked)) {
864 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
865 break;
866 }
867
Andrew Scullb06d1752019-02-04 10:15:48 +0000868 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000869 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000870 break;
871 }
872
Andrew Walbran508e63c2018-12-20 17:02:37 +0000873 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100874 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000875 arch_timer_remaining_ns(&vcpu->regs);
876
877 /*
878 * The timer expired so allow the interrupt to be
879 * delivered.
880 */
881 if (timer_remaining_ns == 0) {
882 break;
883 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100884 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000885
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100886 /*
887 * The vCPU is not ready to run, return the appropriate code to
888 * the primary which called vcpu_run.
889 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500890 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100891 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
892 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000893
894 ret = false;
895 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000896
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500897 case VCPU_STATE_BLOCKED:
898 /* A blocked vCPU is run unconditionally. Fall through. */
899 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +0000900 /* Check NPI is to be injected here. */
901 if (need_vm_lock) {
902 plat_ffa_inject_notification_pending_interrupt(
903 vcpu_locked, current, vm_locked);
904 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000905 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500906 default:
907 /*
908 * Execution not expected to reach here. Deny the request
909 * gracefully.
910 */
911 *run_ret = ffa_error(FFA_DENIED);
912 ret = false;
913 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000914 }
915
Andrew Scullb06d1752019-02-04 10:15:48 +0000916 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000917 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100918 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000919
J-Alves7ac49052022-02-08 17:20:53 +0000920#if SECURE_WORLD == 1
921 /* Set the designated GP register with the vCPU ID. */
922 if (is_vcpu_reset_and_start) {
923 vcpu_set_phys_core_idx(vcpu_locked.vcpu);
924 }
925#endif
926
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000927 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000928 * Mark the registers as unavailable now that we're about to reflect
929 * them onto the real registers. This will also prevent another physical
930 * CPU from trying to read these registers.
931 */
932 vcpu->regs_available = false;
933
934 ret = true;
935
936out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100937 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000938 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100939 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000940 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000941 return ret;
942}
943
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500945 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100946{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100947 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100948 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100949 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100950
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -0800951 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500952 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100953 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100954
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700955 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
956 return ret;
957 }
958
Andrew Scull19503262018-09-20 14:48:39 +0100959 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100960 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100961 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100962 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100963 }
964
Fuad Tabbaed294af2019-12-20 10:43:01 +0000965 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100966 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100967 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100968 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100969
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000970 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100971 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000972 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000973 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100974 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000975
Andrew Walbran508e63c2018-12-20 17:02:37 +0000976 /*
977 * Inject timer interrupt if timer has expired. It's safe to access
978 * vcpu->regs here because api_vcpu_prepare_run already made sure that
979 * regs_available was true (and then set it to false) before returning
980 * true.
981 */
982 if (arch_timer_pending(&vcpu->regs)) {
983 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100984 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
985 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000986
987 /*
988 * Set the mask bit so the hardware interrupt doesn't fire
989 * again. Ideally we wouldn't do this because it affects what
990 * the secondary vCPU sees, but if we don't then we end up with
991 * a loop of the interrupt firing each time we try to return to
992 * the secondary vCPU.
993 */
994 arch_timer_mask(&vcpu->regs);
995 }
996
Fuad Tabbaed294af2019-12-20 10:43:01 +0000997 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000998 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000999
Andrew Scull33fecd32019-01-08 14:48:27 +00001000 /*
1001 * Set a placeholder return code to the scheduler. This will be
1002 * overwritten when the switch back to the primary occurs.
1003 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001004 ret.func = FFA_INTERRUPT_32;
Andrew Walbran7e824602020-10-22 16:51:40 +01001005 ret.arg1 = 0;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +01001006 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +00001007
Andrew Scull6d2db332018-10-10 15:28:17 +01001008out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001009 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001010}
1011
1012/**
Andrew Scull81e85092018-12-12 12:56:20 +00001013 * Check that the mode indicates memory that is valid, owned and exclusive.
1014 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001015static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001016{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001017 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1018 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001019}
1020
1021/**
Andrew Walbranc8a01972020-09-22 11:23:30 +01001022 * Determines the value to be returned by api_ffa_rxtx_map and
1023 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
1024 * there are waiters, it also switches back to the primary VM for it to wake
1025 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001026 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001027static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
1028 struct vcpu *current,
1029 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001030{
1031 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001032
1033 if (list_empty(&vm->mailbox.waiter_list)) {
1034 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001035 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001036 }
1037
1038 if (vm->id == HF_PRIMARY_VM_ID) {
1039 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001040 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001041 }
1042
1043 /*
1044 * Switch back to the primary VM, informing it that there are waiters
1045 * that need to be notified.
1046 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001047 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001048 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001049 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001050
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001052}
1053
1054/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001055 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001056 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001057static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1058 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001059 paddr_t pa_send_begin, paddr_t pa_send_end,
1060 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001061 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001062 struct mpool *local_page_pool)
1063{
1064 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001065
1066 /* Map the send page as read-only in the hypervisor address space. */
1067 vm_locked.vm->mailbox.send =
1068 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001069 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001070 if (!vm_locked.vm->mailbox.send) {
1071 /* TODO: partial defrag of failed range. */
1072 /* Recover any memory consumed in failed mapping. */
1073 mm_defrag(mm_stage1_locked, local_page_pool);
1074 goto fail;
1075 }
1076
1077 /*
1078 * Map the receive page as writable in the hypervisor address space. On
1079 * failure, unmap the send page before returning.
1080 */
1081 vm_locked.vm->mailbox.recv =
1082 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001083 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001084 if (!vm_locked.vm->mailbox.recv) {
1085 /* TODO: partial defrag of failed range. */
1086 /* Recover any memory consumed in failed mapping. */
1087 mm_defrag(mm_stage1_locked, local_page_pool);
1088 goto fail_undo_send;
1089 }
1090
1091 ret = true;
1092 goto out;
1093
1094 /*
1095 * The following mappings will not require more memory than is available
1096 * in the local pool.
1097 */
1098fail_undo_send:
1099 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001100 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1101 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001102
1103fail:
1104 ret = false;
1105
1106out:
Andrew Sculle1322792019-07-01 17:46:10 +01001107 return ret;
1108}
1109
1110/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001111 * Sanity checks and configures the send and receive pages in the VM stage-2
1112 * and hypervisor stage-1 page tables.
1113 *
1114 * Returns:
1115 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001116 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001117 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1118 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001119 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001120 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001121 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001122
1123struct ffa_value api_vm_configure_pages(
1124 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1125 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1126 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001127{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001128 struct ffa_value ret;
1129 paddr_t pa_send_begin;
1130 paddr_t pa_send_end;
1131 paddr_t pa_recv_begin;
1132 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001133 uint32_t orig_send_mode = 0;
1134 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001135 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001136
1137 /* We only allow these to be setup once. */
1138 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1139 ret = ffa_error(FFA_DENIED);
1140 goto out;
1141 }
1142
1143 /* Hafnium only supports a fixed size of RX/TX buffers. */
1144 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1145 ret = ffa_error(FFA_INVALID_PARAMETERS);
1146 goto out;
1147 }
1148
1149 /* Fail if addresses are not page-aligned. */
1150 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1151 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1152 ret = ffa_error(FFA_INVALID_PARAMETERS);
1153 goto out;
1154 }
1155
1156 /* Convert to physical addresses. */
1157 pa_send_begin = pa_from_ipa(send);
1158 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1159 pa_recv_begin = pa_from_ipa(recv);
1160 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1161
1162 /* Fail if the same page is used for the send and receive pages. */
1163 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1164 ret = ffa_error(FFA_INVALID_PARAMETERS);
1165 goto out;
1166 }
Andrew Sculle1322792019-07-01 17:46:10 +01001167
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001168 /* Set stage 2 translation tables only for virtual FF-A instances. */
1169 if (vm_id_is_current_world(vm_locked.vm->id)) {
1170 /*
1171 * Ensure the pages are valid, owned and exclusive to the VM and
1172 * that the VM has the required access to the memory.
1173 */
1174 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1175 &orig_send_mode) ||
1176 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1177 (orig_send_mode & MM_MODE_R) == 0 ||
1178 (orig_send_mode & MM_MODE_W) == 0) {
1179 dlog_error(
1180 "VM doesn't have required access rights to map "
1181 "TX buffer in stage 2.\n");
1182 ret = ffa_error(FFA_INVALID_PARAMETERS);
1183 goto out;
1184 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001185
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001186 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1187 &orig_recv_mode) ||
1188 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1189 (orig_recv_mode & MM_MODE_R) == 0) {
1190 dlog_error(
1191 "VM doesn't have required access rights to map "
1192 "RX buffer in stage 2.\n");
1193 ret = ffa_error(FFA_INVALID_PARAMETERS);
1194 goto out;
1195 }
Andrew Sculle1322792019-07-01 17:46:10 +01001196
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001197 /* Take memory ownership away from the VM and mark as shared. */
1198 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1199 MM_MODE_W;
1200 if (vm_locked.vm->el0_partition) {
1201 mode |= MM_MODE_USER | MM_MODE_NG;
1202 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001203
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001204 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1205 mode, local_page_pool, NULL)) {
1206 dlog_error(
1207 "Cannot allocate a new entry in stage 2 "
1208 "translation table.\n");
1209 ret = ffa_error(FFA_NO_MEMORY);
1210 goto out;
1211 }
Andrew Sculle1322792019-07-01 17:46:10 +01001212
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001213 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1214 if (vm_locked.vm->el0_partition) {
1215 mode |= MM_MODE_USER | MM_MODE_NG;
1216 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001217
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001218 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1219 mode, local_page_pool, NULL)) {
1220 /* TODO: partial defrag of failed range. */
1221 /* Recover any memory consumed in failed mapping. */
1222 vm_ptable_defrag(vm_locked, local_page_pool);
1223 goto fail_undo_send;
1224 }
Andrew Sculle1322792019-07-01 17:46:10 +01001225 }
1226
Olivier Deprez96a2a262020-06-11 17:21:38 +02001227 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1228 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1229
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001230 /*
1231 * For EL0 partitions, since both the partition and the hypervisor code
1232 * use the EL2&0 translation regime, it is critical to mark the mappings
1233 * of the send and recv buffers as non-global in the TLB. For one, if we
1234 * dont mark it as non-global, it would cause TLB conflicts since there
1235 * would be an identity mapping with non-global attribute in the
1236 * partitions page tables, but another identity mapping in the
1237 * hypervisor page tables with the global attribute. The other issue is
1238 * one of security, we dont want other partitions to be able to access
1239 * other partitions buffers through cached translations.
1240 */
1241 if (vm_locked.vm->el0_partition) {
1242 extra_attributes |= MM_MODE_NG;
1243 }
1244
Manish Pandeyd34f8892020-06-19 17:41:07 +01001245 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1246 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001247 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001248 goto fail_undo_send_and_recv;
1249 }
1250
Manish Pandeyd34f8892020-06-19 17:41:07 +01001251 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001252 goto out;
1253
Andrew Sculle1322792019-07-01 17:46:10 +01001254fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001255 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001256 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001257
1258fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001259 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001260 orig_send_mode, local_page_pool, NULL));
1261 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001262
1263out:
Andrew Sculle1322792019-07-01 17:46:10 +01001264 return ret;
1265}
1266
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001267static void api_get_rxtx_description(struct vm_locked vm_locked, ipaddr_t *send,
1268 ipaddr_t *recv, uint32_t *page_count,
1269 ffa_vm_id_t *owner_vm_id)
1270{
1271 /*
1272 * If the message has been forwarded the effective addresses are in
1273 * hypervisor's TX buffer.
1274 */
1275 bool forwarded = (vm_locked.vm->id == HF_OTHER_WORLD_ID) &&
1276 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1277 (*page_count == 0);
1278
1279 if (forwarded) {
1280 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc =
1281 (struct ffa_endpoint_rx_tx_descriptor *)
1282 vm_locked.vm->mailbox.send;
1283 struct ffa_composite_memory_region *rx_region =
1284 ffa_enpoint_get_rx_memory_region(endpoint_desc);
1285 struct ffa_composite_memory_region *tx_region =
1286 ffa_enpoint_get_tx_memory_region(endpoint_desc);
1287
1288 *owner_vm_id = endpoint_desc->endpoint_id;
1289 *recv = ipa_init(rx_region->constituents[0].address);
1290 *send = ipa_init(tx_region->constituents[0].address);
1291 *page_count = rx_region->constituents[0].page_count;
1292 } else {
1293 *owner_vm_id = vm_locked.vm->id;
1294 }
1295}
Andrew Sculle1322792019-07-01 17:46:10 +01001296/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001297 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001298 * must not be shared. Locking of the page tables combined with a local memory
1299 * pool ensures there will always be enough memory to recover from any errors
1300 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1301 * by another core which could result in this transaction being unable to roll
1302 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001303 *
1304 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001305 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001306 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001307 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001308 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001309 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001310 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001311 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001312struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001313 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001314{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001315 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001316 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001317 struct vm_locked vm_locked;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001318 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001319 struct mm_stage1_locked mm_stage1_locked;
1320 struct mpool local_page_pool;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001321 ffa_vm_id_t owner_vm_id;
1322
1323 vm_locked = vm_lock(vm);
1324 /*
1325 * Get the original buffer addresses and VM ID in case of forwarded
1326 * message.
1327 */
1328 api_get_rxtx_description(vm_locked, &send, &recv, &page_count,
1329 &owner_vm_id);
1330 vm_unlock(&vm_locked);
1331
1332 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1333 if (owner_vm_locked.vm == NULL) {
1334 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1335 owner_vm_id);
1336 return ffa_error(FFA_DENIED);
1337 }
Andrew Scull220e6212018-12-21 18:09:00 +00001338
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001339 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001340 * Create a local pool so any freed memory can't be used by another
1341 * thread. This is to ensure the original mapping can be restored if any
1342 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001343 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001344 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1345
Manish Pandeyd34f8892020-06-19 17:41:07 +01001346 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001347
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001348 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1349 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001350 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001351 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001352 }
1353
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001354 /* Forward buffer mapping to SPMC if coming from a VM. */
1355 plat_ffa_rxtx_map_forward(owner_vm_locked);
1356
Federico Recanati9f1b6532022-04-14 13:15:28 +02001357 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001358
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001359exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001360 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001361 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001362 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001363
1364 return ret;
1365}
1366
1367/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001368 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1369 * translation regime of the caller. Unmap the region for the hypervisor and
1370 * set the memory region to owned and exclusive for the component. Since the
1371 * memory region mapped in the page table, when the buffers were originally
1372 * created we can safely remap it.
1373 *
1374 * Returns:
1375 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1376 * behalf of the caller.
1377 * - FFA_SUCCESS on success if no further action is needed.
1378 */
1379struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1380 struct vcpu *current)
1381{
1382 struct vm *vm = current->vm;
1383 struct vm_locked vm_locked;
Federico Recanati8da9e332022-02-10 11:00:17 +01001384 ffa_vm_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001385 struct mm_stage1_locked mm_stage1_locked;
1386 paddr_t send_pa_begin;
1387 paddr_t send_pa_end;
1388 paddr_t recv_pa_begin;
1389 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001390 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001391
Federico Recanati8da9e332022-02-10 11:00:17 +01001392 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1393 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
1394 dlog_error("`allocator_id` must be 0 at virtual instances.\n");
1395 return ffa_error(FFA_INVALID_PARAMETERS);
1396 }
1397
1398 /* VM ID of which buffers have to be unmapped. */
1399 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1400
1401 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1402 vm = vm_locked.vm;
1403 if (vm == NULL) {
1404 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1405 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001406 return ffa_error(FFA_INVALID_PARAMETERS);
1407 }
1408
1409 /* Get send and receive buffers. */
1410 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001411 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001412 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001413 ret = ffa_error(FFA_INVALID_PARAMETERS);
1414 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001415 }
1416
1417 /* Currently a mailbox size of 1 page is assumed. */
1418 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1419 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1420 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1421 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1422
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001423 mm_stage1_locked = mm_lock_stage1();
1424
Federico Recanati8da9e332022-02-10 11:00:17 +01001425 /* Reset stage 2 mapping only for virtual FF-A instances. */
1426 if (vm_id_is_current_world(owner_vm_id)) {
1427 /*
1428 * Set the memory region of the buffers back to the default mode
1429 * for the VM. Since this memory region was already mapped for
1430 * the RXTX buffers we can safely remap them.
1431 */
1432 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1433 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1434 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001435
Federico Recanati8da9e332022-02-10 11:00:17 +01001436 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1437 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1438 &api_page_pool, NULL));
1439 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001440
1441 /* Unmap the buffers in the partition manager. */
1442 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1443 &api_page_pool));
1444 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1445 &api_page_pool));
1446
1447 vm->mailbox.send = NULL;
1448 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001449 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001450
Federico Recanati8da9e332022-02-10 11:00:17 +01001451 /* Forward buffer unmapping to SPMC if coming from a VM. */
1452 plat_ffa_rxtx_unmap_forward(owner_vm_id);
1453
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001454 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001455
1456out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001457 vm_unlock(&vm_locked);
1458
Federico Recanati8da9e332022-02-10 11:00:17 +01001459 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001460}
1461
1462/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001463 * Notifies the `to` VM about the message currently in its mailbox, possibly
1464 * with the help of the primary VM.
1465 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001466static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1467 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001468{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1470 struct ffa_value primary_ret = {
1471 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001472 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001473 };
1474
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001475 /* Messages for the primary VM are delivered directly. */
1476 if (to.vm->id == HF_PRIMARY_VM_ID) {
1477 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001478 * Only tell the primary VM the size and other details if the
1479 * message is for it, to avoid leaking data about messages for
1480 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001481 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001482 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001483
1484 to.vm->mailbox.state = MAILBOX_STATE_READ;
1485 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001486 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001487 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001488 }
1489
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001490 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1491
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001492 /* Messages for the TEE are sent on via the dispatcher. */
1493 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001494 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001495
Olivier Deprez112d2b52020-09-30 07:39:23 +02001496 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001497 /*
1498 * After the call to the TEE completes it must have finished
1499 * reading its RX buffer, so it is ready for another message.
1500 */
1501 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001502 /*
1503 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001504 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001505 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001506 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001507 }
1508
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001509 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001510 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001511 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001512 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001513 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001514
1515 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001516}
1517
1518/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001519 * Copies data from the sender's send buffer to the recipient's receive buffer
1520 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001521 *
1522 * If the recipient's receive buffer is busy, it can optionally register the
1523 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001524 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001525struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1526 ffa_vm_id_t receiver_vm_id, uint32_t size,
1527 uint32_t attributes, struct vcpu *current,
1528 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001529{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001530 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001531 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001532 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001533 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001535 struct vcpu_locked current_locked;
1536 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 bool notify =
1538 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001539
Andrew Walbran70bc8622019-10-07 14:15:58 +01001540 /* Ensure sender VM ID corresponds to the current VM. */
1541 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001542 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001543 }
1544
1545 /* Disallow reflexive requests as this suggests an error in the VM. */
1546 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001547 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001548 }
1549
1550 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001551 if (size > FFA_MSG_PAYLOAD_MAX) {
1552 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001553 }
1554
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001555 /*
1556 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1557 * invocation.
1558 */
1559 current_locked = vcpu_lock(current);
1560 is_direct_request_ongoing =
1561 is_ffa_direct_msg_request_ongoing(current_locked);
1562 vcpu_unlock(&current_locked);
1563
1564 if (is_direct_request_ongoing) {
1565 return ffa_error(FFA_DENIED);
1566 }
1567
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001568 /* Ensure the receiver VM exists. */
1569 to = vm_find(receiver_vm_id);
1570 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001571 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001572 }
1573
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001574 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001575 * Check that the sender has configured its send buffer. If the tx
1576 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1577 * be safely accessed after releasing the lock since the tx mailbox
1578 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001579 */
1580 sl_lock(&from->lock);
1581 from_msg = from->mailbox.send;
1582 sl_unlock(&from->lock);
1583
1584 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001585 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001586 }
1587
Andrew Walbran82d6d152019-12-24 15:02:06 +00001588 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001589
Andrew Walbran82d6d152019-12-24 15:02:06 +00001590 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001591 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001592 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001593 }
1594
Andrew Walbran82d6d152019-12-24 15:02:06 +00001595 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001596 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001597 to->mailbox.recv_size = size;
1598 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001599 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001600 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001601
1602out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001603 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001604
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001605 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001606}
1607
1608/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001609 * Copies data from the sender's send buffer to the recipient's receive buffer
1610 * and notifies the receiver.
1611 */
1612struct ffa_value api_ffa_msg_send2(ffa_vm_id_t sender_vm_id, uint32_t flags,
1613 struct vcpu *current)
1614{
1615 struct vm *from = current->vm;
1616 struct vm *to;
1617 struct vm_locked to_locked;
1618 ffa_vm_id_t msg_sender_id;
1619 struct vm_locked sender_locked;
1620 const void *from_msg;
1621 struct ffa_value ret;
1622 struct ffa_partition_rxtx_header header;
1623 ffa_vm_id_t sender_id;
1624 ffa_vm_id_t receiver_id;
1625 uint32_t msg_size;
1626 ffa_notifications_bitmap_t rx_buffer_full;
1627
1628 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
1629 if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) {
1630 dlog_error("Sender VM ID must be zero.\n");
1631 return ffa_error(FFA_INVALID_PARAMETERS);
1632 }
1633
1634 /* `flags` can be set only at secure virtual FF-A instances. */
1635 if (plat_ffa_is_vm_id(sender_vm_id) && (flags != 0)) {
1636 dlog_error("flags must be zero.\n");
1637 return ffa_error(FFA_INVALID_PARAMETERS);
1638 }
1639
1640 /*
1641 * Get message sender's mailbox, which can be different to the `from` vm
1642 * when the message is forwarded.
1643 */
1644 msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id;
1645 sender_locked = plat_ffa_vm_find_locked(msg_sender_id);
1646 if (sender_locked.vm == NULL) {
1647 dlog_error("Cannot send message from VM ID %#x, not found.\n",
1648 msg_sender_id);
1649 return ffa_error(FFA_DENIED);
1650 }
1651
1652 from_msg = sender_locked.vm->mailbox.send;
1653 if (from_msg == NULL) {
1654 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
1655 msg_sender_id);
1656 ret = ffa_error(FFA_DENIED);
1657 goto out_unlock_sender;
1658 }
1659
1660 /*
1661 * Copy message header as safety measure to avoid multiple accesses to
1662 * unsafe memory which could be 'corrupted' between safety checks and
1663 * final buffer copy.
1664 */
1665 memcpy_s(&header, FFA_RXTX_HEADER_SIZE, from_msg, FFA_RXTX_HEADER_SIZE);
1666 sender_id = ffa_rxtx_header_sender(&header);
1667 receiver_id = ffa_rxtx_header_receiver(&header);
1668
1669 /* Ensure Sender IDs from API and from message header match. */
1670 if (msg_sender_id != sender_id) {
1671 dlog_error(
1672 "Message sender VM ID (%#x) doesn't match header's VM "
1673 "ID (%#x).\n",
1674 msg_sender_id, sender_id);
1675 ret = ffa_error(FFA_INVALID_PARAMETERS);
1676 goto out_unlock_sender;
1677 }
1678
1679 /* Disallow reflexive requests as this suggests an error in the VM. */
1680 if (receiver_id == sender_id) {
1681 dlog_error("Sender and receive VM IDs must be different.\n");
1682 ret = ffa_error(FFA_INVALID_PARAMETERS);
1683 goto out_unlock_sender;
1684 }
1685
1686 /*
1687 * Check if the message has to be forwarded to the SPMC, in
1688 * this case return, the SPMC will handle the buffer copy.
1689 */
1690 if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) {
1691 goto out_unlock_sender;
1692 }
1693
1694 /* Ensure the receiver VM exists. */
1695 to_locked = plat_ffa_vm_find_locked(receiver_id);
1696 to = to_locked.vm;
1697
1698 if (to == NULL) {
1699 dlog_error("Cannot deliver message to VM %#x, not found.\n",
1700 receiver_id);
1701 ret = ffa_error(FFA_INVALID_PARAMETERS);
1702 goto out_unlock_sender;
1703 }
1704
1705 /*
1706 * Check sender and receiver can use indirect messages.
1707 * Sender is the VM/SP who originally sent the message, not the
1708 * hypervisor possibly relaying it.
1709 */
1710 if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) {
1711 dlog_verbose("VM %#x doesn't support indirect message\n",
1712 sender_id);
1713 ret = ffa_error(FFA_DENIED);
1714 goto out;
1715 }
1716
1717 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
1718 to->mailbox.recv == NULL) {
1719 dlog_error(
1720 "Cannot deliver message to VM %#x, RX buffer not "
1721 "ready.\n",
1722 receiver_id);
1723 ret = ffa_error(FFA_BUSY);
1724 goto out;
1725 }
1726
1727 /* Check the size of transfer. */
1728 msg_size = FFA_RXTX_HEADER_SIZE + header.size;
1729 if ((msg_size > FFA_PARTITION_MSG_PAYLOAD_MAX) ||
1730 (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
1731 dlog_error("Message is too big.\n");
1732 ret = ffa_error(FFA_INVALID_PARAMETERS);
1733 goto out;
1734 }
1735
1736 /* Copy data. */
1737 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, msg_size);
1738 to->mailbox.recv_size = msg_size;
1739 to->mailbox.recv_sender = sender_id;
1740 to->mailbox.recv_func = FFA_MSG_SEND2_32;
1741 to->mailbox.state = MAILBOX_STATE_RECEIVED;
1742
1743 rx_buffer_full = plat_ffa_is_vm_id(sender_id)
1744 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
1745 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
1746 vm_notifications_framework_set_pending(to_locked, rx_buffer_full);
1747
1748 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
1749 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
1750 vcpu_index(current));
1751 plat_ffa_sri_trigger_not_delayed(current->cpu);
1752 } else {
1753 plat_ffa_sri_state_set(DELAYED);
1754 }
1755
1756 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1757
1758out:
1759 vm_unlock(&to_locked);
1760
1761out_unlock_sender:
1762 vm_unlock(&sender_locked);
1763
1764 return ret;
1765}
1766
1767/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001768 * Checks whether the vCPU's attempt to block for a message has already been
1769 * interrupted or whether it is allowed to block.
1770 */
Olivier Deprezd8e18882022-07-15 14:46:41 +02001771static bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001772{
Manish Pandey35e452f2021-02-18 21:36:34 +00001773 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001774 bool interrupted;
1775
Manish Pandey35e452f2021-02-18 21:36:34 +00001776 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001777
1778 /*
1779 * Don't block if there are enabled and pending interrupts, to match
1780 * behaviour of wait_for_interrupt.
1781 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001782 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001783
Manish Pandey35e452f2021-02-18 21:36:34 +00001784 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001785
1786 return interrupted;
1787}
1788
1789/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001790 * Receives a message from the mailbox. If one isn't available, this function
1791 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001792 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001793 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001794 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001795struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1796 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001797{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001798 bool is_direct_request_ongoing;
1799 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001800 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001801 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001802 bool is_from_secure_world =
1803 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001804
Andrew Scullaa039b32018-10-04 15:02:26 +01001805 /*
1806 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001807 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001808 */
J-Alvesb37fd082020-10-22 12:29:21 +01001809 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001810 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001811 }
1812
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001813 /*
1814 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1815 * invocation.
1816 */
1817 current_locked = vcpu_lock(current);
1818 is_direct_request_ongoing =
1819 is_ffa_direct_msg_request_ongoing(current_locked);
1820 vcpu_unlock(&current_locked);
1821
1822 if (is_direct_request_ongoing) {
1823 return ffa_error(FFA_DENIED);
1824 }
1825
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001826 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001827
Andrew Scullaa039b32018-10-04 15:02:26 +01001828 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001829 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001830 return_code = ffa_msg_recv_return(vm);
Federico Recanati25053ee2022-03-14 15:01:53 +01001831 if (return_code.func == FFA_MSG_SEND_32) {
1832 vm->mailbox.state = MAILBOX_STATE_READ;
1833 }
Jose Marinho3e2442f2019-03-12 13:30:37 +00001834 goto out;
1835 }
1836
1837 /* No pending message so fail if not allowed to block. */
1838 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001839 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001840 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001841 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001842
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001843 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001844 * From this point onward this call can only be interrupted or a message
1845 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001846 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001847 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001848 return_code = ffa_error(FFA_INTERRUPTED);
1849 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001850 goto out;
1851 }
1852
J-Alvesb37fd082020-10-22 12:29:21 +01001853 if (is_from_secure_world) {
1854 /* Return to other world if caller is a SP. */
1855 *next = api_switch_to_other_world(
1856 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001857 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001858 } else {
1859 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001860 struct ffa_value run_return = {
1861 .func = FFA_MSG_WAIT_32,
1862 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001863 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001864
Andrew Walbranb4816552018-12-05 17:35:42 +00001865 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001866 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001867 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001868out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001869 sl_unlock(&vm->lock);
1870
Jose Marinho3e2442f2019-03-12 13:30:37 +00001871 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001872}
1873
1874/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001875 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1876 * by this function, the caller must have called api_mailbox_send before with
1877 * the notify argument set to true, and this call must have failed because the
1878 * mailbox was not available.
1879 *
1880 * It should be called repeatedly to retrieve a list of VMs.
1881 *
1882 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1883 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001884 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001885int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001886{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001887 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001888 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001889 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001890
1891 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001892 if (list_empty(&vm->mailbox.ready_list)) {
1893 ret = -1;
1894 goto exit;
1895 }
1896
1897 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1898 ready_links);
1899 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001900 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001901
1902exit:
1903 sl_unlock(&vm->lock);
1904 return ret;
1905}
1906
1907/**
1908 * Retrieves the next VM waiting to be notified that the mailbox of the
1909 * specified VM became writable. Only primary VMs are allowed to call this.
1910 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001911 * Returns -1 on failure or if there are no waiters; the VM id of the next
1912 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001913 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001914int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001915{
1916 struct vm *vm;
1917 struct vm_locked locked;
1918 struct wait_entry *entry;
1919 struct vm *waiting_vm;
1920
1921 /* Only primary VMs are allowed to call this function. */
1922 if (current->vm->id != HF_PRIMARY_VM_ID) {
1923 return -1;
1924 }
1925
Andrew Walbran42347a92019-05-09 13:59:03 +01001926 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001927 if (vm == NULL) {
1928 return -1;
1929 }
1930
Fuad Tabbaed294af2019-12-20 10:43:01 +00001931 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001932 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001933 entry = api_fetch_waiter(locked);
1934 vm_unlock(&locked);
1935
1936 if (entry == NULL) {
1937 return -1;
1938 }
1939
1940 /* Enqueue notification to waiting VM. */
1941 waiting_vm = entry->waiting_vm;
1942
1943 sl_lock(&waiting_vm->lock);
1944 if (list_empty(&entry->ready_links)) {
1945 list_append(&waiting_vm->mailbox.ready_list,
1946 &entry->ready_links);
1947 }
1948 sl_unlock(&waiting_vm->lock);
1949
1950 return waiting_vm->id;
1951}
1952
1953/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001954 * Releases the caller's mailbox so that a new message can be received. The
1955 * caller must have copied out all data they wish to preserve as new messages
1956 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001957 *
1958 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001959 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1960 * - FFA_SUCCESS on success if no further action is needed.
1961 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001962 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001963 * hf_mailbox_waiter_get.
1964 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001965struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001966{
1967 struct vm *vm = current->vm;
1968 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001969 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001970
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001971 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001972 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001973 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001974 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001975 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001976 break;
1977
Andrew Sculld6ee1102019-04-05 22:12:42 +01001978 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001979 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001980 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001981 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001982 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001983 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001984
1985 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001986}
Andrew Walbran318f5732018-11-20 16:23:42 +00001987
1988/**
1989 * Enables or disables a given interrupt ID for the calling vCPU.
1990 *
1991 * Returns 0 on success, or -1 if the intid is invalid.
1992 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001993int64_t api_interrupt_enable(uint32_t intid, bool enable,
1994 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001995{
Manish Pandey35e452f2021-02-18 21:36:34 +00001996 struct vcpu_locked current_locked;
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001997 uint32_t intid_index = INTID_INDEX(intid);
1998 uint32_t intid_mask = INTID_MASK(1U, intid);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001999
Andrew Walbran318f5732018-11-20 16:23:42 +00002000 if (intid >= HF_NUM_INTIDS) {
2001 return -1;
2002 }
2003
Manish Pandey35e452f2021-02-18 21:36:34 +00002004 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00002005 if (enable) {
Maksims Svecovs953dee52022-04-01 12:15:36 +01002006 if (type == INTERRUPT_TYPE_IRQ) {
2007 current->interrupts.interrupt_type[intid_index] &=
2008 ~intid_mask;
2009 } else if (type == INTERRUPT_TYPE_FIQ) {
2010 current->interrupts.interrupt_type[intid_index] |=
2011 intid_mask;
2012 }
2013
Andrew Walbran3d84a262018-12-13 14:41:19 +00002014 /*
2015 * If it is pending and was not enabled before, increment the
2016 * count.
2017 */
2018 if (current->interrupts.interrupt_pending[intid_index] &
2019 ~current->interrupts.interrupt_enabled[intid_index] &
2020 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00002021 if ((current->interrupts.interrupt_type[intid_index] &
2022 intid_mask) ==
Daniel Boulby801f8ef2022-06-27 14:21:01 +01002023 INTID_MASK(INTERRUPT_TYPE_IRQ, intid)) {
Manish Pandey35e452f2021-02-18 21:36:34 +00002024 vcpu_irq_count_increment(current_locked);
2025 } else {
2026 vcpu_fiq_count_increment(current_locked);
2027 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00002028 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002029 current->interrupts.interrupt_enabled[intid_index] |=
2030 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00002031 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002032 /*
2033 * If it is pending and was enabled before, decrement the count.
2034 */
2035 if (current->interrupts.interrupt_pending[intid_index] &
2036 current->interrupts.interrupt_enabled[intid_index] &
2037 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00002038 if ((current->interrupts.interrupt_type[intid_index] &
2039 intid_mask) ==
Daniel Boulby801f8ef2022-06-27 14:21:01 +01002040 INTID_MASK(INTERRUPT_TYPE_IRQ, intid)) {
Manish Pandey35e452f2021-02-18 21:36:34 +00002041 vcpu_irq_count_decrement(current_locked);
2042 } else {
2043 vcpu_fiq_count_decrement(current_locked);
2044 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00002045 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002046 current->interrupts.interrupt_enabled[intid_index] &=
2047 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00002048 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00002049 }
2050
Manish Pandey35e452f2021-02-18 21:36:34 +00002051 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002052 return 0;
2053}
2054
2055/**
2056 * Returns the ID of the next pending interrupt for the calling vCPU, and
2057 * acknowledges it (i.e. marks it as no longer pending). Returns
2058 * HF_INVALID_INTID if there are no pending interrupts.
2059 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00002060uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002061{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002062 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002063 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00002064 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00002065
2066 /*
2067 * Find the first enabled and pending interrupt ID, return it, and
2068 * deactivate it.
2069 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002070 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00002071 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2072 uint32_t enabled_and_pending =
2073 current->interrupts.interrupt_enabled[i] &
2074 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002075
Andrew Walbran318f5732018-11-20 16:23:42 +00002076 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002077 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00002078 uint32_t intid_mask = 1U << bit_index;
2079
Andrew Walbran3d84a262018-12-13 14:41:19 +00002080 /*
2081 * Mark it as no longer pending and decrement the count.
2082 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002083 current->interrupts.interrupt_pending[i] &= ~intid_mask;
2084
2085 if ((current->interrupts.interrupt_type[i] &
2086 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
2087 vcpu_irq_count_decrement(current_locked);
2088 } else {
2089 vcpu_fiq_count_decrement(current_locked);
2090 }
2091
Andrew Walbran3d84a262018-12-13 14:41:19 +00002092 first_interrupt =
2093 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00002094 break;
2095 }
2096 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002097
Manish Pandey35e452f2021-02-18 21:36:34 +00002098 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002099 return first_interrupt;
2100}
2101
2102/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00002103 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00002104 * given VM and vCPU.
2105 */
2106static inline bool is_injection_allowed(uint32_t target_vm_id,
2107 struct vcpu *current)
2108{
2109 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002110
Andrew Walbran318f5732018-11-20 16:23:42 +00002111 /*
2112 * The primary VM is allowed to inject interrupts into any VM. Secondary
2113 * VMs are only allowed to inject interrupts into their own vCPUs.
2114 */
2115 return current_vm_id == HF_PRIMARY_VM_ID ||
2116 current_vm_id == target_vm_id;
2117}
2118
2119/**
2120 * Injects a virtual interrupt of the given ID into the given target vCPU.
2121 * This doesn't cause the vCPU to actually be run immediately; it will be taken
2122 * when the vCPU is next run, which is up to the scheduler.
2123 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00002124 * Returns:
2125 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
2126 * ID is invalid, or the current VM is not allowed to inject interrupts to
2127 * the target VM.
2128 * - 0 on success if no further action is needed.
2129 * - 1 if it was called by the primary VM and the primary VM now needs to wake
2130 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00002131 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002132int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
2133 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01002134 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00002135{
Andrew Walbran318f5732018-11-20 16:23:42 +00002136 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01002137 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00002138
2139 if (intid >= HF_NUM_INTIDS) {
2140 return -1;
2141 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002142
Andrew Walbran318f5732018-11-20 16:23:42 +00002143 if (target_vm == NULL) {
2144 return -1;
2145 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002146
Andrew Walbran318f5732018-11-20 16:23:42 +00002147 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00002148 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00002149 return -1;
2150 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002151
Andrew Walbran318f5732018-11-20 16:23:42 +00002152 if (!is_injection_allowed(target_vm_id, current)) {
2153 return -1;
2154 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002155
Andrew Walbrane1310df2019-04-29 17:28:28 +01002156 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00002157
Manish Pandey35e452f2021-02-18 21:36:34 +00002158 dlog_verbose(
2159 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
2160 "%u\n",
2161 intid, target_vm_id, target_vcpu_idx, current->vm->id,
2162 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01002163 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00002164}
Andrew Scull6386f252018-12-06 13:29:10 +00002165
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002166/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002167struct ffa_value api_ffa_version(struct vcpu *current,
2168 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002169{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002170 struct vm_locked current_vm_locked;
2171
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002172 /*
2173 * Ensure that both major and minor revision representation occupies at
2174 * most 15 bits.
2175 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002176 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002177 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002178 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002179 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002180 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01002181 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00002182 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01002183 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002184
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002185 current_vm_locked = vm_lock(current->vm);
2186 current_vm_locked.vm->ffa_version = requested_version;
2187 vm_unlock(&current_vm_locked);
2188
Daniel Boulby6e32c612021-02-17 15:09:41 +00002189 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002190}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002191
2192int64_t api_debug_log(char c, struct vcpu *current)
2193{
Andrew Sculld54e1be2019-08-20 11:09:42 +01002194 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002195 struct vm *vm = current->vm;
2196 struct vm_locked vm_locked = vm_lock(vm);
2197
Andrew Sculld54e1be2019-08-20 11:09:42 +01002198 if (c == '\n' || c == '\0') {
2199 flush = true;
2200 } else {
2201 vm->log_buffer[vm->log_buffer_length++] = c;
2202 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
2203 }
2204
2205 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01002206 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
2207 vm->log_buffer_length);
2208 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002209 }
2210
2211 vm_unlock(&vm_locked);
2212
2213 return 0;
2214}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002215
2216/**
J-Alves6f72ca82021-11-01 12:34:58 +00002217 * Helper for success return of FFA_FEATURES, for when it is used to query
2218 * an interrupt ID.
2219 */
2220struct ffa_value api_ffa_feature_success(uint32_t arg2)
2221{
2222 return (struct ffa_value){
2223 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
2224}
2225
2226/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002227 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002228 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002229 */
J-Alves6f72ca82021-11-01 12:34:58 +00002230struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002231{
J-Alves6f72ca82021-11-01 12:34:58 +00002232 /*
2233 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
2234 * if using a feature ID.
2235 */
2236 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
J-Alvesff676c12022-05-13 17:25:33 +01002237 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0U) {
J-Alves6f72ca82021-11-01 12:34:58 +00002238 return ffa_error(FFA_NOT_SUPPORTED);
2239 }
2240
2241 switch (feature_function_id) {
2242 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002243 case FFA_ERROR_32:
2244 case FFA_SUCCESS_32:
2245 case FFA_INTERRUPT_32:
2246 case FFA_VERSION_32:
2247 case FFA_FEATURES_32:
2248 case FFA_RX_RELEASE_32:
2249 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002250 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002251 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002252 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002253 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002254 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002255 case FFA_MEM_DONATE_32:
2256 case FFA_MEM_LEND_32:
2257 case FFA_MEM_SHARE_32:
2258 case FFA_MEM_RETRIEVE_REQ_32:
2259 case FFA_MEM_RETRIEVE_RESP_32:
2260 case FFA_MEM_RELINQUISH_32:
2261 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002262 case FFA_MEM_FRAG_RX_32:
2263 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002264 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002265 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002266 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002267 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00002268#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002269 /* FF-A v1.1 features. */
2270 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002271 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2272 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2273 case FFA_NOTIFICATION_BIND_32:
2274 case FFA_NOTIFICATION_UNBIND_32:
2275 case FFA_NOTIFICATION_SET_32:
2276 case FFA_NOTIFICATION_GET_32:
2277 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07002278 case FFA_MEM_PERM_GET_32:
2279 case FFA_MEM_PERM_SET_32:
2280 case FFA_MEM_PERM_GET_64:
2281 case FFA_MEM_PERM_SET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002282 case FFA_MSG_SEND2_32:
J-Alves3829fc02021-03-18 12:49:18 +00002283#endif
2284 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00002285
2286#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2287 /* Check support of a feature provided respective feature ID. */
2288 case FFA_FEATURE_NPI:
2289 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2290 case FFA_FEATURE_SRI:
2291 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2292#endif
2293 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002294 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002295 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002296 }
2297}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002298
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002299/**
J-Alves645eabe2021-02-22 16:08:27 +00002300 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2301 * interfaces.
2302 */
2303static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2304{
2305 return args.arg2 == 0U;
2306}
2307
2308/**
J-Alves76d99af2021-03-10 17:42:11 +00002309 * Limits size of arguments in ffa_value structure to 32-bit.
2310 */
2311static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2312{
2313 return (struct ffa_value){
2314 .func = (uint32_t)args.func,
2315 .arg1 = (uint32_t)args.arg1,
2316 .arg2 = (uint32_t)0,
2317 .arg3 = (uint32_t)args.arg3,
2318 .arg4 = (uint32_t)args.arg4,
2319 .arg5 = (uint32_t)args.arg5,
2320 .arg6 = (uint32_t)args.arg6,
2321 .arg7 = (uint32_t)args.arg7,
2322 };
2323}
2324
2325/**
2326 * Helper to copy direct message payload, depending on SMC used and expected
2327 * registers size.
2328 */
2329static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2330{
2331 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2332 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2333 return api_ffa_value_copy32(args);
2334 }
2335
2336 return (struct ffa_value){
2337 .func = args.func,
2338 .arg1 = args.arg1,
2339 .arg2 = 0,
2340 .arg3 = args.arg3,
2341 .arg4 = args.arg4,
2342 .arg5 = args.arg5,
2343 .arg6 = args.arg6,
2344 .arg7 = args.arg7,
2345 };
2346}
2347
2348/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002349 * Send an FF-A direct message request.
2350 */
2351struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2352 ffa_vm_id_t receiver_vm_id,
2353 struct ffa_value args,
2354 struct vcpu *current,
2355 struct vcpu **next)
2356{
J-Alves17228f72021-04-20 17:13:19 +01002357 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002358 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002359 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002360 struct vcpu *receiver_vcpu;
2361 struct two_vcpu_locked vcpus_locked;
2362
J-Alves645eabe2021-02-22 16:08:27 +00002363 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2364 return ffa_error(FFA_INVALID_PARAMETERS);
2365 }
2366
Olivier Deprez55a189e2021-06-09 15:45:27 +02002367 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2368 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002369 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002370 }
2371
Olivier Deprez55a189e2021-06-09 15:45:27 +02002372 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002373 return ret;
2374 }
2375
2376 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2377
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002378 receiver_vm = vm_find(receiver_vm_id);
2379 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002380 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002381 return ffa_error(FFA_INVALID_PARAMETERS);
2382 }
2383
2384 /*
J-Alves439ac972021-11-18 17:32:03 +00002385 * Check if sender supports sending direct message req, and if
2386 * receiver supports receipt of direct message requests.
2387 */
2388 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2389 return ffa_error(FFA_DENIED);
2390 }
2391
2392 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002393 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002394 * UP (migratable) or MP partitions with a number of vCPUs matching the
2395 * number of PEs in the system. It further states that MP partitions
2396 * accepting direct request messages cannot migrate.
2397 */
J-Alvesad6a0432021-04-09 16:06:21 +01002398 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002399 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002400 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002401 return ffa_error(FFA_INVALID_PARAMETERS);
2402 }
2403
J-Alves6e2abc62021-12-02 14:58:56 +00002404 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002405 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2406
2407 /*
2408 * If destination vCPU is executing or already received an
2409 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2410 * busy. There is a brief period of time where the vCPU state has
2411 * changed but regs_available is still false thus consider this case as
2412 * the vCPU not yet ready to receive a direct message request.
2413 */
2414 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2415 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2416 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002417 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002418 ret = ffa_error(FFA_BUSY);
2419 goto out;
2420 }
2421
2422 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2423 memory_order_relaxed)) {
2424 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002425 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002426 receiver_vcpu->vm->id,
2427 vcpu_index(receiver_vcpu));
2428 receiver_vcpu->state = VCPU_STATE_ABORTED;
2429 }
2430
2431 ret = ffa_error(FFA_ABORTED);
2432 goto out;
2433 }
2434
2435 switch (receiver_vcpu->state) {
2436 case VCPU_STATE_OFF:
2437 case VCPU_STATE_RUNNING:
2438 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002439 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002440 case VCPU_STATE_BLOCKED:
2441 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002442 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2443 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002444 ret = ffa_error(FFA_BUSY);
2445 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002446 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002447 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002448 * We expect target vCPU to be in WAITING state after either
2449 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002450 */
2451 break;
2452 }
2453
2454 /* Inject timer interrupt if any pending */
2455 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002456 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2457 HF_VIRTUAL_TIMER_INTID, current,
2458 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002459
2460 arch_timer_mask(&receiver_vcpu->regs);
2461 }
2462
2463 /* The receiver vCPU runs upon direct message invocation */
2464 receiver_vcpu->cpu = current->cpu;
2465 receiver_vcpu->state = VCPU_STATE_RUNNING;
2466 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002467 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002468
J-Alves76d99af2021-03-10 17:42:11 +00002469 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002470
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002471 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002472
2473 /* Switch to receiver vCPU targeted to by direct msg request */
2474 *next = receiver_vcpu;
2475
J-Alves6e2abc62021-12-02 14:58:56 +00002476 if (!receiver_locked.vm->el0_partition) {
2477 /*
2478 * If the scheduler in the system is giving CPU cycles to the
2479 * receiver, due to pending notifications, inject the NPI
2480 * interrupt. Following call assumes that '*next' has been set
2481 * to receiver_vcpu.
2482 */
2483 plat_ffa_inject_notification_pending_interrupt(
2484 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2485 ? vcpus_locked.vcpu1
2486 : vcpus_locked.vcpu2,
2487 current, receiver_locked);
2488 }
2489
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002490 /*
2491 * Since this flow will lead to a VM switch, the return value will not
2492 * be applied to current vCPU.
2493 */
2494
2495out:
2496 sl_unlock(&receiver_vcpu->lock);
2497 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002498 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002499
2500 return ret;
2501}
2502
2503/**
2504 * Send an FF-A direct message response.
2505 */
2506struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2507 ffa_vm_id_t receiver_vm_id,
2508 struct ffa_value args,
2509 struct vcpu *current,
2510 struct vcpu **next)
2511{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002512 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002513
2514 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2515 return ffa_error(FFA_INVALID_PARAMETERS);
2516 }
2517
J-Alves76d99af2021-03-10 17:42:11 +00002518 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002519
Olivier Deprez55a189e2021-06-09 15:45:27 +02002520 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2521 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002522 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002523 }
2524
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002525 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002526 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002527 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002528 * No need for REQ/RESP state management as managed exit does
2529 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002530 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002531 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2532 vcpu_unlock(&current_locked);
2533 return ffa_error(FFA_DENIED);
2534 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002535
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002536 /*
2537 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2538 * a secure interrupt to a SP that is performing managed exit.
2539 * We have taken a implementation defined choice to not allow
2540 * Managed exit while a SP is processing a secure interrupt.
2541 */
2542 CHECK(!current->processing_secure_interrupt);
2543
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06002544 plat_interrupts_set_priority_mask(current->priority_mask);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002545 current->processing_managed_exit = false;
2546 } else {
2547 /*
2548 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2549 * defined originator.
2550 */
2551 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2552 /*
2553 * Sending direct response but direct request origin
2554 * vCPU is not set.
2555 */
2556 vcpu_unlock(&current_locked);
2557 return ffa_error(FFA_DENIED);
2558 }
2559
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002560 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002561 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2562 vcpu_unlock(&current_locked);
2563 return ffa_error(FFA_DENIED);
2564 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002565 }
2566
2567 /* Clear direct request origin for the caller. */
2568 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2569
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002570 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002571
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002572 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002573 *next = api_switch_to_other_world(
2574 current, to_ret,
2575 /*
2576 * Current vcpu sent a direct response. It moves to
2577 * waiting state.
2578 */
2579 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002580 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002581 *next = api_switch_to_primary(
2582 current, to_ret,
2583 /*
2584 * Current vcpu sent a direct response. It moves to
2585 * waiting state.
2586 */
2587 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002588 } else if (vm_id_is_current_world(receiver_vm_id)) {
2589 /*
2590 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002591 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002592 * made function return error before getting to this point.
2593 */
2594 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002595 /*
2596 * current vcpu sent a direct response.
2597 * It moves to waiting state.
2598 */
2599 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002600 } else {
2601 panic("Invalid direct message response invocation");
2602 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002603
2604 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2605}
2606
J-Alves84658fc2021-06-17 14:37:32 +01002607static bool api_memory_region_check_flags(
2608 struct ffa_memory_region *memory_region, uint32_t share_func)
2609{
2610 switch (share_func) {
2611 case FFA_MEM_SHARE_32:
2612 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2613 0U) {
2614 return false;
2615 }
2616 /* Intentional fall-through */
2617 case FFA_MEM_LEND_32:
2618 case FFA_MEM_DONATE_32: {
2619 /* Bits 31:2 Must Be Zero. */
2620 ffa_memory_receiver_flags_t to_mask =
2621 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2622 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2623
2624 if ((memory_region->flags & to_mask) != 0U) {
2625 return false;
2626 }
2627 break;
2628 }
2629 default:
2630 panic("Check for mem send calls only.\n");
2631 }
2632
2633 /* Last check reserved values are 0 */
2634 return true;
2635}
2636
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002637struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2638 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002639 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002640{
2641 struct vm *from = current->vm;
2642 struct vm *to;
2643 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002644 struct ffa_memory_region *memory_region;
2645 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01002646 bool targets_other_world = false;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002647
2648 if (ipa_addr(address) != 0 || page_count != 0) {
2649 /*
2650 * Hafnium only supports passing the descriptor in the TX
2651 * mailbox.
2652 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002653 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002654 }
2655
Andrew Walbranca808b12020-05-15 17:22:28 +01002656 if (fragment_length > length) {
2657 dlog_verbose(
2658 "Fragment length %d greater than total length %d.\n",
2659 fragment_length, length);
2660 return ffa_error(FFA_INVALID_PARAMETERS);
2661 }
2662 if (fragment_length < sizeof(struct ffa_memory_region) +
2663 sizeof(struct ffa_memory_access)) {
2664 dlog_verbose(
2665 "Initial fragment length %d smaller than header size "
2666 "%d.\n",
2667 fragment_length,
2668 sizeof(struct ffa_memory_region) +
2669 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002670 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002671 }
2672
2673 /*
2674 * Check that the sender has configured its send buffer. If the TX
2675 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2676 * be safely accessed after releasing the lock since the TX mailbox
2677 * address can only be configured once.
2678 */
2679 sl_lock(&from->lock);
2680 from_msg = from->mailbox.send;
2681 sl_unlock(&from->lock);
2682
2683 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002684 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002685 }
2686
2687 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002688 * Copy the memory region descriptor to a fresh page from the memory
2689 * pool. This prevents the sender from changing it underneath us, and
2690 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002691 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002692 if (fragment_length > HF_MAILBOX_SIZE ||
2693 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002694 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002695 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002696 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002697 if (memory_region == NULL) {
2698 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002699 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002700 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002701 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002702
2703 /* The sender must match the caller. */
2704 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002705 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002706 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002707 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002708 }
2709
J-Alves84658fc2021-06-17 14:37:32 +01002710 if (!api_memory_region_check_flags(memory_region, share_func)) {
2711 dlog_verbose(
2712 "Memory region reserved arguments must be zero.\n");
2713 ret = ffa_error(FFA_INVALID_PARAMETERS);
2714 goto out;
2715 }
2716
J-Alves363f5722022-04-25 17:37:37 +01002717 if (memory_region->receiver_count == 0U) {
2718 dlog_verbose("Receiver count can't be 0.\n");
2719 ret = ffa_error(FFA_INVALID_PARAMETERS);
2720 goto out;
2721 }
2722
2723 if (share_func == FFA_MEM_DONATE_32 &&
2724 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002725 dlog_verbose(
J-Alves363f5722022-04-25 17:37:37 +01002726 "FFA_MEM_DONATE only supports one recipient. "
2727 "Specified %u\n",
2728 memory_region->receiver_count);
2729 ret = ffa_error(FFA_INVALID_PARAMETERS);
2730 goto out;
2731 }
2732
2733 if (memory_region->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
2734 dlog_verbose(
2735 "Max number of recipients supported is %u "
2736 "specified %u\n",
2737 MAX_MEM_SHARE_RECIPIENTS,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002738 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002739 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002740 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002741 }
2742
2743 /*
2744 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01002745 * If there is a receiver from the other world, track it for later
2746 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002747 */
J-Alves363f5722022-04-25 17:37:37 +01002748 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
2749 ffa_vm_id_t receiver_id =
2750 memory_region->receivers[i]
2751 .receiver_permissions.receiver;
2752 to = vm_find(receiver_id);
2753
2754 if (vm_id_is_current_world(receiver_id) &&
2755 (to == NULL || to == from)) {
2756 dlog_verbose("%s: invalid receiver.\n", __func__);
2757 ret = ffa_error(FFA_INVALID_PARAMETERS);
2758 goto out;
2759 }
2760
2761 if (!plat_ffa_is_memory_send_valid(receiver_id, share_func)) {
2762 ret = ffa_error(FFA_DENIED);
2763 goto out;
2764 }
2765
2766 /* Capture if any of the receivers is from the other world. */
2767 if (!targets_other_world) {
2768 targets_other_world =
2769 !vm_id_is_current_world(receiver_id);
2770 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002771 }
2772
J-Alves363f5722022-04-25 17:37:37 +01002773 /* Allow for one memory region to be shared to the TEE. */
2774 if (targets_other_world) {
2775 assert(memory_region->receiver_count == 1 &&
2776 to->id == HF_TEE_VM_ID);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002777 /*
2778 * The 'to' VM lock is only needed in the case that it is the
2779 * TEE VM.
2780 */
2781 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2782
2783 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002784 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002785 goto out_unlock;
2786 }
2787
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002788 ret = ffa_memory_tee_send(
2789 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2790 length, fragment_length, share_func, &api_page_pool);
2791 /*
2792 * ffa_tee_memory_send takes ownership of the memory_region, so
2793 * make sure we don't free it.
2794 */
2795 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002796
2797 out_unlock:
2798 vm_unlock(&vm_to_from_lock.vm1);
2799 vm_unlock(&vm_to_from_lock.vm2);
2800 } else {
2801 struct vm_locked from_locked = vm_lock(from);
2802
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002803 ret = ffa_memory_send(from_locked, memory_region, length,
2804 fragment_length, share_func,
2805 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002806 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002807 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002808 * make sure we don't free it.
2809 */
2810 memory_region = NULL;
2811
2812 vm_unlock(&from_locked);
2813 }
2814
2815out:
2816 if (memory_region != NULL) {
2817 mpool_free(&api_page_pool, memory_region);
2818 }
2819
2820 return ret;
2821}
2822
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002823struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2824 uint32_t fragment_length,
2825 ipaddr_t address, uint32_t page_count,
2826 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002827{
2828 struct vm *to = current->vm;
2829 struct vm_locked to_locked;
2830 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002831 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002832 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002833 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002834
2835 if (ipa_addr(address) != 0 || page_count != 0) {
2836 /*
2837 * Hafnium only supports passing the descriptor in the TX
2838 * mailbox.
2839 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002840 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002841 }
2842
Andrew Walbrana65a1322020-04-06 19:32:32 +01002843 if (fragment_length != length) {
2844 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002845 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002846 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002847
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002848 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002849 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002850 message_buffer_size = cpu_get_buffer_size(current->cpu);
2851 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2852 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002853 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002854 }
2855
2856 to_locked = vm_lock(to);
2857 to_msg = to->mailbox.send;
2858
2859 if (to_msg == NULL) {
2860 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002861 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002862 goto out;
2863 }
2864
2865 /*
2866 * Copy the retrieve request descriptor to an internal buffer, so that
2867 * the caller can't change it underneath us.
2868 */
2869 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2870
2871 if (msg_receiver_busy(to_locked, NULL, false)) {
2872 /*
2873 * Can't retrieve memory information if the mailbox is not
2874 * available.
2875 */
2876 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002877 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002878 goto out;
2879 }
2880
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002881 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2882 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002883
2884out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002885 vm_unlock(&to_locked);
2886 return ret;
2887}
2888
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002889struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002890{
2891 struct vm *from = current->vm;
2892 struct vm_locked from_locked;
2893 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002894 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002895 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002896 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002897 uint32_t length;
2898
2899 from_locked = vm_lock(from);
2900 from_msg = from->mailbox.send;
2901
2902 if (from_msg == NULL) {
2903 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002904 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002905 goto out;
2906 }
2907
2908 /*
2909 * Calculate length from relinquish descriptor before copying. We will
2910 * check again later to make sure it hasn't changed.
2911 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002912 length = sizeof(struct ffa_mem_relinquish) +
2913 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2914 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002915 /*
2916 * Copy the relinquish descriptor to an internal buffer, so that the
2917 * caller can't change it underneath us.
2918 */
2919 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002920 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002921 message_buffer_size = cpu_get_buffer_size(current->cpu);
2922 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2923 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002924 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002925 goto out;
2926 }
2927 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2928
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002929 if (sizeof(struct ffa_mem_relinquish) +
2930 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002931 length) {
2932 dlog_verbose(
2933 "Endpoint count changed while copying to internal "
2934 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002935 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002936 goto out;
2937 }
2938
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002939 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2940 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002941
2942out:
2943 vm_unlock(&from_locked);
2944 return ret;
2945}
2946
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002947struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2948 ffa_memory_region_flags_t flags,
2949 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002950{
2951 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002952 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002953
Olivier Deprez55a189e2021-06-09 15:45:27 +02002954 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002955 struct vm_locked to_locked = vm_lock(to);
2956
Andrew Walbranca808b12020-05-15 17:22:28 +01002957 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002958 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002959
Andrew Walbran290b0c92020-02-03 16:37:14 +00002960 vm_unlock(&to_locked);
2961 } else {
2962 struct vm *from = vm_find(HF_TEE_VM_ID);
2963 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2964
Andrew Walbranca808b12020-05-15 17:22:28 +01002965 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2966 vm_to_from_lock.vm2, handle, flags,
2967 &api_page_pool);
2968
2969 vm_unlock(&vm_to_from_lock.vm1);
2970 vm_unlock(&vm_to_from_lock.vm2);
2971 }
2972
2973 return ret;
2974}
2975
2976struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2977 uint32_t fragment_offset,
2978 ffa_vm_id_t sender_vm_id,
2979 struct vcpu *current)
2980{
2981 struct vm *to = current->vm;
2982 struct vm_locked to_locked;
2983 struct ffa_value ret;
2984
2985 /* Sender ID MBZ at virtual instance. */
2986 if (sender_vm_id != 0) {
2987 return ffa_error(FFA_INVALID_PARAMETERS);
2988 }
2989
2990 to_locked = vm_lock(to);
2991
2992 if (msg_receiver_busy(to_locked, NULL, false)) {
2993 /*
2994 * Can't retrieve memory information if the mailbox is not
2995 * available.
2996 */
2997 dlog_verbose("RX buffer not ready.\n");
2998 ret = ffa_error(FFA_BUSY);
2999 goto out;
3000 }
3001
3002 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
3003 &api_page_pool);
3004
3005out:
3006 vm_unlock(&to_locked);
3007 return ret;
3008}
3009
3010struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
3011 uint32_t fragment_length,
3012 ffa_vm_id_t sender_vm_id,
3013 struct vcpu *current)
3014{
3015 struct vm *from = current->vm;
3016 const void *from_msg;
3017 void *fragment_copy;
3018 struct ffa_value ret;
3019
3020 /* Sender ID MBZ at virtual instance. */
3021 if (sender_vm_id != 0) {
3022 return ffa_error(FFA_INVALID_PARAMETERS);
3023 }
3024
3025 /*
3026 * Check that the sender has configured its send buffer. If the TX
3027 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3028 * be safely accessed after releasing the lock since the TX mailbox
3029 * address can only be configured once.
3030 */
3031 sl_lock(&from->lock);
3032 from_msg = from->mailbox.send;
3033 sl_unlock(&from->lock);
3034
3035 if (from_msg == NULL) {
3036 return ffa_error(FFA_INVALID_PARAMETERS);
3037 }
3038
3039 /*
3040 * Copy the fragment to a fresh page from the memory pool. This prevents
3041 * the sender from changing it underneath us, and also lets us keep it
3042 * around in the share state table if needed.
3043 */
3044 if (fragment_length > HF_MAILBOX_SIZE ||
3045 fragment_length > MM_PPOOL_ENTRY_SIZE) {
3046 dlog_verbose(
3047 "Fragment length %d larger than mailbox size %d.\n",
3048 fragment_length, HF_MAILBOX_SIZE);
3049 return ffa_error(FFA_INVALID_PARAMETERS);
3050 }
3051 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
3052 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
3053 0) {
3054 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
3055 return ffa_error(FFA_INVALID_PARAMETERS);
3056 }
3057 fragment_copy = mpool_alloc(&api_page_pool);
3058 if (fragment_copy == NULL) {
3059 dlog_verbose("Failed to allocate fragment copy.\n");
3060 return ffa_error(FFA_NO_MEMORY);
3061 }
3062 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
3063
3064 /*
3065 * Hafnium doesn't support fragmentation of memory retrieve requests
3066 * (because it doesn't support caller-specified mappings, so a request
3067 * will never be larger than a single page), so this must be part of a
3068 * memory send (i.e. donate, lend or share) request.
3069 *
3070 * We can tell from the handle whether the memory transaction is for the
3071 * TEE or not.
3072 */
3073 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
3074 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
3075 struct vm_locked from_locked = vm_lock(from);
3076
3077 ret = ffa_memory_send_continue(from_locked, fragment_copy,
3078 fragment_length, handle,
3079 &api_page_pool);
3080 /*
3081 * `ffa_memory_send_continue` takes ownership of the
3082 * fragment_copy, so we don't need to free it here.
3083 */
3084 vm_unlock(&from_locked);
3085 } else {
3086 struct vm *to = vm_find(HF_TEE_VM_ID);
3087 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
3088
3089 /*
3090 * The TEE RX buffer state is checked in
3091 * `ffa_memory_tee_send_continue` rather than here, as we need
3092 * to return `FFA_MEM_FRAG_RX` with the current offset rather
3093 * than FFA_ERROR FFA_BUSY in case it is busy.
3094 */
3095
3096 ret = ffa_memory_tee_send_continue(
3097 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
3098 fragment_length, handle, &api_page_pool);
3099 /*
3100 * `ffa_memory_tee_send_continue` takes ownership of the
3101 * fragment_copy, so we don't need to free it here.
3102 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00003103
3104 vm_unlock(&vm_to_from_lock.vm1);
3105 vm_unlock(&vm_to_from_lock.vm2);
3106 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003107
3108 return ret;
3109}
Max Shvetsov40108e72020-08-27 12:39:50 +01003110
Olivier Deprezd614d322021-06-18 15:21:00 +02003111/**
3112 * Register an entry point for a vCPU in warm boot cases.
3113 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
3114 */
Max Shvetsov40108e72020-08-27 12:39:50 +01003115struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
3116 struct vcpu *current)
3117{
3118 struct vm_locked vm_locked;
Olivier Deprezd614d322021-06-18 15:21:00 +02003119 struct ffa_value ret = ffa_error(FFA_DENIED);
Max Shvetsov40108e72020-08-27 12:39:50 +01003120
Olivier Deprezd614d322021-06-18 15:21:00 +02003121 /*
3122 * Reject if interface is not supported at this FF-A instance
3123 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
3124 */
3125 if (!plat_ffa_is_secondary_ep_register_supported() ||
3126 current->vm->vcpu_count == 1) {
3127 return ffa_error(FFA_NOT_SUPPORTED);
3128 }
3129
3130 /*
3131 * No further check is made on the address validity
3132 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
3133 * from the VM or vCPU structure.
3134 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
3135 * For each SP [...] the Framework assumes that the same entry point
3136 * address is used for initializing any execution context during a
3137 * secondary cold boot.
3138 * If this function is invoked multiple times, then the entry point
3139 * address specified in the last valid invocation must be used by the
3140 * callee.
3141 */
Max Shvetsov40108e72020-08-27 12:39:50 +01003142 vm_locked = vm_lock(current->vm);
Olivier Deprezd614d322021-06-18 15:21:00 +02003143 if (vm_locked.vm->initialized) {
3144 goto out;
3145 }
3146
Max Shvetsov40108e72020-08-27 12:39:50 +01003147 vm_locked.vm->secondary_ep = entry_point;
Olivier Deprezd614d322021-06-18 15:21:00 +02003148
3149 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3150
3151out:
Max Shvetsov40108e72020-08-27 12:39:50 +01003152 vm_unlock(&vm_locked);
3153
Olivier Deprezd614d322021-06-18 15:21:00 +02003154 return ret;
Max Shvetsov40108e72020-08-27 12:39:50 +01003155}
J-Alvesa0f317d2021-06-09 13:31:59 +01003156
3157struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
3158 ffa_vcpu_count_t vcpu_count,
3159 struct vcpu *current)
3160{
3161 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
3162 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
3163 vm_id);
3164 return ffa_error(FFA_NOT_SUPPORTED);
3165 }
3166
3167 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
3168}
3169
3170struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
3171 struct vcpu *current)
3172{
3173 /*
3174 * Validity of use of this interface is the same as for bitmap create.
3175 */
3176 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
3177 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
3178 vm_id);
3179 return ffa_error(FFA_NOT_SUPPORTED);
3180 }
3181
3182 return plat_ffa_notifications_bitmap_destroy(vm_id);
3183}
J-Alvesc003a7a2021-03-18 13:06:53 +00003184
3185struct ffa_value api_ffa_notification_update_bindings(
3186 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3187 ffa_notifications_bitmap_t notifications, bool is_bind,
3188 struct vcpu *current)
3189{
3190 struct ffa_value ret = {.func = FFA_SUCCESS_32};
3191 struct vm_locked receiver_locked;
3192 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3193 const ffa_vm_id_t id_to_update =
3194 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
3195 const ffa_vm_id_t id_to_validate =
3196 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
3197
3198 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
3199 receiver_vm_id)) {
3200 dlog_verbose("Invalid use of notifications bind interface.\n");
3201 return ffa_error(FFA_INVALID_PARAMETERS);
3202 }
3203
J-Alvesb15e9402021-09-08 11:44:42 +01003204 if (plat_ffa_notifications_update_bindings_forward(
3205 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
3206 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01003207 return ret;
3208 }
3209
J-Alvesc003a7a2021-03-18 13:06:53 +00003210 if (notifications == 0U) {
3211 dlog_verbose("No notifications have been specified.\n");
3212 return ffa_error(FFA_INVALID_PARAMETERS);
3213 }
3214
3215 /**
3216 * This check assumes receiver is the current VM, and has been enforced
3217 * by 'plat_ffa_is_notifications_bind_valid'.
3218 */
3219 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3220
3221 if (receiver_locked.vm == NULL) {
3222 dlog_verbose("Receiver doesn't exist!\n");
3223 return ffa_error(FFA_DENIED);
3224 }
3225
J-Alves09ff9d82021-11-02 11:55:20 +00003226 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00003227 dlog_verbose("Notifications are not enabled.\n");
3228 ret = ffa_error(FFA_NOT_SUPPORTED);
3229 goto out;
3230 }
3231
3232 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
3233 vm_find(sender_vm_id) == NULL) {
3234 dlog_verbose("Sender VM does not exist!\n");
3235 ret = ffa_error(FFA_INVALID_PARAMETERS);
3236 goto out;
3237 }
3238
3239 /*
3240 * Can't bind/unbind notifications if at least one is bound to a
3241 * different sender.
3242 */
3243 if (!vm_notifications_validate_bound_sender(
3244 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3245 id_to_validate, notifications)) {
3246 dlog_verbose("Notifications are bound to other sender.\n");
3247 ret = ffa_error(FFA_DENIED);
3248 goto out;
3249 }
3250
3251 /**
3252 * Check if there is a pending notification within those specified in
3253 * the bitmap.
3254 */
3255 if (vm_are_notifications_pending(receiver_locked,
3256 plat_ffa_is_vm_id(sender_vm_id),
3257 notifications)) {
3258 dlog_verbose("Notifications within '%x' pending.\n",
3259 notifications);
3260 ret = ffa_error(FFA_DENIED);
3261 goto out;
3262 }
3263
3264 vm_notifications_update_bindings(
3265 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
3266 notifications, is_per_vcpu && is_bind);
3267
3268out:
3269 vm_unlock(&receiver_locked);
3270 return ret;
3271}
J-Alvesaa79c012021-07-09 14:29:45 +01003272
3273struct ffa_value api_ffa_notification_set(
3274 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3275 ffa_notifications_bitmap_t notifications, struct vcpu *current)
3276{
3277 struct ffa_value ret;
3278 struct vm_locked receiver_locked;
3279
3280 /*
3281 * Check if is per-vCPU or global, and extracting vCPU ID according
3282 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
3283 */
3284 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3285 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
3286
J-Alvesaa79c012021-07-09 14:29:45 +01003287 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
3288 receiver_vm_id)) {
3289 dlog_verbose("Invalid use of notifications set interface.\n");
3290 return ffa_error(FFA_INVALID_PARAMETERS);
3291 }
3292
3293 if (notifications == 0U) {
3294 dlog_verbose("No notifications have been specified.\n");
3295 return ffa_error(FFA_INVALID_PARAMETERS);
3296 }
3297
J-Alvesde7bd2f2021-09-09 19:54:35 +01003298 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
3299 flags, notifications, &ret)) {
3300 return ret;
3301 }
3302
J-Alvesaa79c012021-07-09 14:29:45 +01003303 /*
3304 * This check assumes receiver is the current VM, and has been enforced
3305 * by 'plat_ffa_is_notification_set_valid'.
3306 */
3307 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3308
3309 if (receiver_locked.vm == NULL) {
3310 dlog_verbose("Receiver ID is not valid.\n");
3311 return ffa_error(FFA_INVALID_PARAMETERS);
3312 }
3313
J-Alves09ff9d82021-11-02 11:55:20 +00003314 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003315 dlog_verbose("Receiver's notifications not enabled.\n");
3316 ret = ffa_error(FFA_DENIED);
3317 goto out;
3318 }
3319
3320 /*
3321 * If notifications are not bound to the sender, they wouldn't be
3322 * enabled either for the receiver.
3323 */
3324 if (!vm_notifications_validate_binding(
3325 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3326 sender_vm_id, notifications, is_per_vcpu)) {
3327 dlog_verbose("Notifications bindings not valid.\n");
3328 ret = ffa_error(FFA_DENIED);
3329 goto out;
3330 }
3331
3332 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3333 dlog_verbose("Invalid VCPU ID!\n");
3334 ret = ffa_error(FFA_INVALID_PARAMETERS);
3335 goto out;
3336 }
3337
J-Alves7461ef22021-10-18 17:21:33 +01003338 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00003339 vm_notifications_partition_set_pending(
3340 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), notifications,
3341 vcpu_id, is_per_vcpu);
3342
J-Alvesaa79c012021-07-09 14:29:45 +01003343 dlog_verbose("Set the notifications: %x.\n", notifications);
3344
J-Alves13394022021-06-30 13:48:49 +01003345 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3346 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3347 vcpu_index(current));
3348 plat_ffa_sri_trigger_not_delayed(current->cpu);
3349 } else {
3350 plat_ffa_sri_state_set(DELAYED);
3351 }
J-Alvesaa79c012021-07-09 14:29:45 +01003352
J-Alves13394022021-06-30 13:48:49 +01003353 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003354out:
3355 vm_unlock(&receiver_locked);
3356
3357 return ret;
3358}
3359
3360static struct ffa_value api_ffa_notification_get_success_return(
3361 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3362 ffa_notifications_bitmap_t from_framework)
3363{
3364 return (struct ffa_value){
3365 .func = FFA_SUCCESS_32,
3366 .arg1 = 0U,
3367 .arg2 = (uint32_t)from_sp,
3368 .arg3 = (uint32_t)(from_sp >> 32),
3369 .arg4 = (uint32_t)from_vm,
3370 .arg5 = (uint32_t)(from_vm >> 32),
3371 .arg6 = (uint32_t)from_framework,
3372 .arg7 = (uint32_t)(from_framework >> 32),
3373 };
3374}
3375
3376struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3377 ffa_vcpu_index_t vcpu_id,
3378 uint32_t flags, struct vcpu *current)
3379{
J-Alves663682a2022-03-25 13:56:51 +00003380 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01003381 ffa_notifications_bitmap_t sp_notifications = 0;
3382 ffa_notifications_bitmap_t vm_notifications = 0;
3383 struct vm_locked receiver_locked;
3384 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01003385 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
3386 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
3387 FFA_NOTIFICATION_FLAG_BITMAP_SP |
3388 FFA_NOTIFICATION_FLAG_BITMAP_VM);
3389
3390 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
3391 if ((flags & flags_mbz) != 0U) {
3392 dlog_verbose(
3393 "Invalid flags bit(s) set in notifications get. [31:4] "
3394 "MBZ(%x)\n",
3395 flags);
3396 return ffa_error(FFA_INVALID_PARAMETERS);
3397 }
J-Alvesaa79c012021-07-09 14:29:45 +01003398
3399 /*
J-Alvesfc95a302022-04-22 14:18:23 +01003400 * Following check should capture wrong uses of the interface,
3401 * depending on whether Hafnium is SPMC or hypervisor. On the
3402 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01003403 */
J-Alvesfc95a302022-04-22 14:18:23 +01003404 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
3405 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003406 dlog_verbose("Invalid use of notifications get interface.\n");
3407 return ffa_error(FFA_INVALID_PARAMETERS);
3408 }
3409
3410 /*
3411 * This check assumes receiver is the current VM, and has been enforced
3412 * by `plat_ffa_is_notifications_get_valid`.
3413 */
3414 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3415
3416 /*
3417 * `plat_ffa_is_notifications_get_valid` ensures following is never
3418 * true.
3419 */
3420 CHECK(receiver_locked.vm != NULL);
3421
3422 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3423 (receiver_locked.vm->vcpu_count != 1 &&
3424 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003425 dlog_verbose(
3426 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3427 vcpu_id, receiver_locked.vm->vcpu_count,
3428 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003429 ret = ffa_error(FFA_INVALID_PARAMETERS);
3430 goto out;
3431 }
3432
3433 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003434 if (!plat_ffa_notifications_get_from_sp(
3435 receiver_locked, vcpu_id, &sp_notifications,
3436 &ret)) {
3437 dlog_verbose("Failed to get notifications from sps.");
3438 goto out;
3439 }
J-Alvesaa79c012021-07-09 14:29:45 +01003440 }
3441
3442 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00003443 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01003444 receiver_locked, true, vcpu_id);
3445 }
3446
J-Alvesd605a092022-03-28 14:20:48 +01003447 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
3448 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
3449 if (!plat_ffa_notifications_get_framework_notifications(
3450 receiver_locked, &framework_notifications, flags,
3451 vcpu_id, &ret)) {
3452 dlog_verbose(
3453 "Failed to get notifications from "
3454 "framework.\n");
3455 goto out;
3456 }
3457 }
3458
J-Alves663682a2022-03-25 13:56:51 +00003459 ret = api_ffa_notification_get_success_return(
3460 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01003461
J-Alvesfe23ebe2021-10-13 16:07:07 +01003462 /*
3463 * If there are no more pending notifications, change `sri_state` to
3464 * handled.
3465 */
3466 if (vm_is_notifications_pending_count_zero()) {
3467 plat_ffa_sri_state_set(HANDLED);
3468 }
3469
J-Alves6e2abc62021-12-02 14:58:56 +00003470 if (!receiver_locked.vm->el0_partition &&
3471 !vm_are_global_notifications_pending(receiver_locked)) {
3472 vm_notifications_set_npi_injected(receiver_locked, false);
3473 }
3474
J-Alvesaa79c012021-07-09 14:29:45 +01003475out:
3476 vm_unlock(&receiver_locked);
3477
3478 return ret;
3479}
J-Alvesc8e8a222021-06-08 17:33:52 +01003480
3481/**
3482 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3483 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3484 */
3485static struct ffa_value api_ffa_notification_info_get_success_return(
3486 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003487 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003488{
3489 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3490
3491 /*
3492 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3493 * hold the list of ids.
3494 */
3495 memcpy_s(&ret.arg3,
3496 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3497 sizeof(ids[0]) * ids_count);
3498
3499 /*
3500 * According to the spec x2 should have:
3501 * - Bit flagging if there are more notifications pending;
3502 * - The total number of elements (i.e. total list size);
3503 * - The number of VCPU IDs within each VM specific list.
3504 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003505 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3506 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3507 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003508
3509 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3510 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3511
3512 for (unsigned int i = 0; i < lists_count; i++) {
3513 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3514 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3515 }
3516
3517 return ret;
3518}
3519
3520struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3521{
3522 /*
3523 * Following set of variables should be populated with the return info.
3524 * At a successfull handling of this interface, they should be used
3525 * to populate the 'ret' structure in accordance to the table 17.29
3526 * of the FF-A v1.1 Beta0 specification.
3527 */
3528 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3529 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3530 uint32_t lists_count = 0;
3531 uint32_t ids_count = 0;
3532 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003533 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003534
3535 /*
3536 * This interface can only be called at NS virtual/physical FF-A
3537 * instance by the endpoint implementing the primary scheduler and the
3538 * Hypervisor/OS kernel.
3539 * In the SPM, following check passes if call has been forwarded from
3540 * the hypervisor.
3541 */
3542 if (current->vm->id != HF_PRIMARY_VM_ID) {
3543 dlog_verbose(
3544 "Only the receiver's scheduler can use this "
3545 "interface\n");
3546 return ffa_error(FFA_NOT_SUPPORTED);
3547 }
3548
J-Alvesca058c22021-09-10 14:02:07 +01003549 /*
3550 * Forward call to the other world, and fill the arrays used to assemble
3551 * return.
3552 */
3553 plat_ffa_notification_info_get_forward(
3554 ids, &ids_count, lists_sizes, &lists_count,
3555 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3556
3557 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3558
J-Alvesc8e8a222021-06-08 17:33:52 +01003559 /* Get notifications' info from this world */
3560 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3561 ++index) {
3562 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3563
3564 list_is_full = vm_notifications_info_get(
3565 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3566 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3567
3568 vm_unlock(&vm_locked);
3569 }
3570
3571 if (!list_is_full) {
3572 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003573 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003574 ids, &ids_count, lists_sizes, &lists_count,
3575 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3576 }
3577
3578 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003579 dlog_verbose(
3580 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003581 result = ffa_error(FFA_NO_DATA);
3582 } else {
3583 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003584 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003585 }
3586
J-Alvesfe23ebe2021-10-13 16:07:07 +01003587 plat_ffa_sri_state_set(HANDLED);
3588
J-Alves13394022021-06-30 13:48:49 +01003589 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003590}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003591
3592struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3593{
3594 struct vm_locked vm_locked;
3595 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3596 bool mode_ret = false;
3597 uint32_t mode = 0;
3598
3599 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3600 return ffa_error(FFA_NOT_SUPPORTED);
3601 }
3602
3603 if (!(current->vm->el0_partition)) {
3604 return ffa_error(FFA_DENIED);
3605 }
3606
3607 vm_locked = vm_lock(current->vm);
3608
3609 /*
3610 * mm_get_mode is used to check if the given base_addr page is already
3611 * mapped. If the page is unmapped, return error. If the page is mapped
3612 * appropriate attributes are returned to the caller. Note that
3613 * mm_get_mode returns true if the address is in the valid VA range as
3614 * supported by the architecture and MMU configurations, as opposed to
3615 * whether a page is mapped or not. For a page to be known as mapped,
3616 * the API must return true AND the returned mode must not have
3617 * MM_MODE_INVALID set.
3618 */
3619 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3620 va_add(base_addr, PAGE_SIZE), &mode);
3621 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3622 ret = ffa_error(FFA_INVALID_PARAMETERS);
3623 goto out;
3624 }
3625
3626 /* No memory should be marked RWX */
3627 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3628 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3629
3630 /*
3631 * S-EL0 partitions are expected to have all their pages marked as
3632 * non-global.
3633 */
3634 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3635 (MM_MODE_NG | MM_MODE_USER));
3636
3637 if (mode & MM_MODE_W) {
3638 /* No memory should be writeable but not readable. */
3639 CHECK(mode & MM_MODE_R);
3640 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3641 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3642 } else if (mode & MM_MODE_R) {
3643 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3644 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3645 if (!(mode & MM_MODE_X)) {
3646 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3647 }
3648 }
3649out:
3650 vm_unlock(&vm_locked);
3651 return ret;
3652}
3653
3654struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3655 uint32_t mem_perm, struct vcpu *current)
3656{
3657 struct vm_locked vm_locked;
3658 struct ffa_value ret;
3659 bool mode_ret = false;
3660 uint32_t original_mode;
3661 uint32_t new_mode;
3662 struct mpool local_page_pool;
3663
3664 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3665 return ffa_error(FFA_NOT_SUPPORTED);
3666 }
3667
3668 if (!(current->vm->el0_partition)) {
3669 return ffa_error(FFA_DENIED);
3670 }
3671
3672 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3673 return ffa_error(FFA_INVALID_PARAMETERS);
3674 }
3675
3676 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3677 (mem_perm != FFA_MEM_PERM_RX)) {
3678 return ffa_error(FFA_INVALID_PARAMETERS);
3679 }
3680
3681 /*
3682 * Create a local pool so any freed memory can't be used by another
3683 * thread. This is to ensure the original mapping can be restored if any
3684 * stage of the process fails.
3685 */
3686 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3687
3688 vm_locked = vm_lock(current->vm);
3689
3690 /*
3691 * All regions accessible by the partition are mapped during boot. If we
3692 * cannot get a successful translation for the page range, the request
3693 * to change permissions is rejected.
3694 * mm_get_mode is used to check if the given address range is already
3695 * mapped. If the range is unmapped, return error. If the range is
3696 * mapped appropriate attributes are returned to the caller. Note that
3697 * mm_get_mode returns true if the address is in the valid VA range as
3698 * supported by the architecture and MMU configurations, as opposed to
3699 * whether a page is mapped or not. For a page to be known as mapped,
3700 * the API must return true AND the returned mode must not have
3701 * MM_MODE_INVALID set.
3702 */
3703
3704 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3705 va_add(base_addr, page_count * PAGE_SIZE),
3706 &original_mode);
3707 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3708 ret = ffa_error(FFA_INVALID_PARAMETERS);
3709 goto out;
3710 }
3711
3712 /* Device memory cannot be marked as executable */
3713 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3714 ret = ffa_error(FFA_INVALID_PARAMETERS);
3715 goto out;
3716 }
3717
3718 new_mode = MM_MODE_USER | MM_MODE_NG;
3719
3720 if (mem_perm == FFA_MEM_PERM_RW) {
3721 new_mode |= MM_MODE_R | MM_MODE_W;
3722 } else if (mem_perm == FFA_MEM_PERM_RX) {
3723 new_mode |= MM_MODE_R | MM_MODE_X;
3724 } else if (mem_perm == FFA_MEM_PERM_RO) {
3725 new_mode |= MM_MODE_R;
3726 }
3727
3728 /*
3729 * Safe to re-map memory, since we know the requested permissions are
3730 * valid, and the memory requested to be re-mapped is also valid.
3731 */
3732 if (!mm_identity_prepare(
3733 &vm_locked.vm->ptable, pa_from_va(base_addr),
3734 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3735 new_mode, &local_page_pool)) {
3736 /*
3737 * Defrag the table into the local page pool.
3738 * mm_identity_prepare could have allocated or freed pages to
3739 * split blocks or tables etc.
3740 */
3741 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3742
3743 /*
3744 * Guaranteed to succeed mapping with old mode since the mapping
3745 * with old mode already existed and we have a local page pool
3746 * that should have sufficient memory to go back to the original
3747 * state.
3748 */
3749 CHECK(mm_identity_prepare(
3750 &vm_locked.vm->ptable, pa_from_va(base_addr),
3751 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3752 original_mode, &local_page_pool));
3753 mm_identity_commit(
3754 &vm_locked.vm->ptable, pa_from_va(base_addr),
3755 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3756 original_mode, &local_page_pool);
3757
3758 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3759 ret = ffa_error(FFA_NO_MEMORY);
3760 goto out;
3761 }
3762
3763 mm_identity_commit(
3764 &vm_locked.vm->ptable, pa_from_va(base_addr),
3765 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3766 &local_page_pool);
3767
3768 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3769
3770out:
3771 mpool_fini(&local_page_pool);
3772 vm_unlock(&vm_locked);
3773
3774 return ret;
3775}
Maksims Svecovs71b76702022-05-20 15:32:58 +01003776
3777/**
3778 * Helper function for FFA_CONSOLE_LOG ABI.
3779 * Writes number of characters to a given VM buffer.
3780 */
3781static rsize_t arg_to_char_helper(struct vm_locked from_locked,
3782 const uint64_t src, rsize_t src_size,
3783 rsize_t to_write)
3784{
3785 bool flush = false;
3786 char c;
3787 rsize_t size = src_size < to_write ? src_size : to_write;
3788 rsize_t written = 0;
3789
3790 if (size == 0) {
3791 return 0;
3792 }
3793
3794 while (written < size) {
3795 c = ((char *)&src)[written++];
3796 if (c == '\n' || c == '\0') {
3797 flush = true;
3798 } else {
3799 from_locked.vm->log_buffer
3800 [from_locked.vm->log_buffer_length++] = c;
3801 flush = (from_locked.vm->log_buffer_length ==
3802 LOG_BUFFER_SIZE);
3803 }
3804
3805 if (flush) {
3806 dlog_flush_vm_buffer(from_locked.vm->id,
3807 from_locked.vm->log_buffer,
3808 from_locked.vm->log_buffer_length);
3809 from_locked.vm->log_buffer_length = 0;
3810 }
3811 }
3812
3813 return written;
3814}
3815
3816/**
3817 * Implements FFA_CONSOLE_LOG buffered logging.
3818 */
3819struct ffa_value api_ffa_console_log(const struct ffa_value args,
3820 struct vcpu *current)
3821{
3822 struct vm *vm = current->vm;
3823 struct vm_locked vm_locked;
3824 size_t chars_in_param = args.func == FFA_CONSOLE_LOG_32
3825 ? sizeof(uint32_t)
3826 : sizeof(uint64_t);
3827 size_t total_to_write = args.arg1;
3828
3829 if (total_to_write == 0 || total_to_write > chars_in_param * 6) {
3830 return ffa_error(FFA_INVALID_PARAMETERS);
3831 }
3832
3833 vm_locked = vm_lock(vm);
3834
3835 total_to_write -= arg_to_char_helper(vm_locked, args.arg2,
3836 chars_in_param, total_to_write);
3837 total_to_write -= arg_to_char_helper(vm_locked, args.arg3,
3838 chars_in_param, total_to_write);
3839 total_to_write -= arg_to_char_helper(vm_locked, args.arg4,
3840 chars_in_param, total_to_write);
3841 total_to_write -= arg_to_char_helper(vm_locked, args.arg5,
3842 chars_in_param, total_to_write);
3843 total_to_write -= arg_to_char_helper(vm_locked, args.arg6,
3844 chars_in_param, total_to_write);
3845 arg_to_char_helper(vm_locked, args.arg7, chars_in_param,
3846 total_to_write);
3847
3848 vm_unlock(&vm_locked);
3849
3850 return (struct ffa_value){.func = FFA_SUCCESS_32};
3851}