blob: a664811591c6ba5e44df0905a87f68e9bad0b81e [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
J-Alves13318e32021-02-22 17:21:00 +00002 * Copyright 2021 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +000012#include "hf/arch/ffa.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000016#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020017#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000018
Andrew Scull877ae4b2019-07-02 12:52:33 +010019#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000020#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010021#include "hf/ffa_internal.h"
22#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000023#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010024#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010025#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010027#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010028#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "hf/vm.h"
30
Andrew Scullf35a5c92018-08-07 18:09:46 +010031#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010033
Fuad Tabbae4efcc32020-07-16 15:37:27 +010034static_assert(sizeof(struct ffa_partition_info) == 8,
35 "Partition information descriptor size doesn't match the one in "
36 "the FF-A 1.0 EAC specification, Table 82.");
37
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000038/*
39 * To eliminate the risk of deadlocks, we define a partial order for the
40 * acquisition of locks held concurrently by the same physical CPU. Our current
41 * ordering requirements are as follows:
42 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010043 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000044 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010045 * Locks of the same kind require the lock of lowest address to be locked first,
46 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000047 */
48
Andrew Scullaa039b32018-10-04 15:02:26 +010049static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010050 "Currently, a page is mapped for the send and receive buffers so "
51 "the maximum request is the size of a page.");
52
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000053static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
54 "The page pool entry size must be at least as big as the mailbox "
55 "size, so that memory region descriptors can be copied from the "
56 "mailbox for memory sharing.");
57
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000058static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000059
60/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000061 * Initialises the API page pool by taking ownership of the contents of the
62 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000063 */
64void api_init(struct mpool *ppool)
65{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000066 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000067}
68
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010069/**
J-Alvesad6a0432021-04-09 16:06:21 +010070 * Get target VM vCPU:
71 * If VM is UP then return first vCPU.
72 * If VM is MP then return vCPU whose index matches current CPU index.
73 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050074struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +010075{
76 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
77 struct vcpu *vcpu = NULL;
78
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050079 CHECK((vm != NULL) && (current != NULL));
80
J-Alvesad6a0432021-04-09 16:06:21 +010081 if (vm->vcpu_count == 1) {
82 vcpu = vm_get_vcpu(vm, 0);
83 } else if (current_cpu_index < vm->vcpu_count) {
84 vcpu = vm_get_vcpu(vm, current_cpu_index);
85 }
86
87 return vcpu;
88}
89
90/**
J-Alvesfe7f7372020-11-09 11:32:12 +000091 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
92 * is given as argument of the function.
93 *
94 * Called to change the context between SPs for direct messaging (when Hafnium
95 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
96 *
97 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +010098 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +000099 * - MP with pinned Execution Contexts.
100 */
101static struct vcpu *api_switch_to_vm(struct vcpu *current,
102 struct ffa_value to_ret,
103 enum vcpu_state vcpu_state,
104 ffa_vm_id_t to_id)
105{
106 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100107 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000108
109 CHECK(next != NULL);
110
111 /* Set the return value for the target VM. */
112 arch_regs_set_retval(&next->regs, to_ret);
113
114 /* Set the current vCPU state. */
115 sl_lock(&current->lock);
116 current->state = vcpu_state;
117 sl_unlock(&current->lock);
118
119 return next;
120}
121
122/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000123 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100124 *
125 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100126 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100127 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500128struct vcpu *api_switch_to_primary(struct vcpu *current,
129 struct ffa_value primary_ret,
130 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100131{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000132 /*
133 * If the secondary is blocked but has a timer running, sleep until the
134 * timer fires rather than indefinitely.
135 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100136 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100137 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
138 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100139 if (arch_timer_enabled_current()) {
140 uint64_t remaining_ns =
141 arch_timer_remaining_ns_current();
142
143 if (remaining_ns == 0) {
144 /*
145 * Timer is pending, so the current vCPU should
146 * be run again right away.
147 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100148 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100149 /*
150 * primary_ret.arg1 should already be set to the
151 * current VM ID and vCPU ID.
152 */
153 primary_ret.arg2 = 0;
154 } 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,
244 .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 = {
326 .func = HF_FFA_RUN_WAKE_UP,
327 .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
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100358struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
359 const struct ffa_uuid *uuid)
360{
361 struct vm *current_vm = current->vm;
362 struct vm_locked current_vm_locked;
363 ffa_vm_count_t vm_count = 0;
364 bool uuid_is_null = ffa_uuid_is_null(uuid);
365 struct ffa_value ret;
366 uint32_t size;
Olivier Depreze562e542020-06-11 17:31:54 +0200367 struct ffa_partition_info partitions[2 * MAX_VMS];
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100368
369 /*
370 * Iterate through the VMs to find the ones with a matching UUID.
371 * A Null UUID retrieves information for all VMs.
372 */
373 for (uint16_t index = 0; index < vm_get_count(); ++index) {
J-Alves09ff9d82021-11-02 11:55:20 +0000374 struct vm *vm = vm_find_index(index);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100375
376 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
377 partitions[vm_count].vm_id = vm->id;
378 partitions[vm_count].vcpu_count = vm->vcpu_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100379 partitions[vm_count].properties =
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100380 plat_ffa_partition_properties(current_vm->id,
381 vm);
J-Alves09ff9d82021-11-02 11:55:20 +0000382 partitions[vm_count].properties |=
383 vm_are_notifications_enabled(vm)
384 ? FFA_PARTITION_NOTIFICATION
385 : 0;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100386
387 ++vm_count;
388 }
389 }
390
Olivier Depreze562e542020-06-11 17:31:54 +0200391 /* If UUID is Null vm_count must not be zero at this stage. */
392 CHECK(!uuid_is_null || vm_count != 0);
393
394 /*
395 * When running the Hypervisor:
396 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
397 * it to fill with secure partitions information.
398 * - If UUID is non-Null vm_count may be zero because the UUID matches
399 * a secure partition and the query is forwarded to the SPMC.
400 * When running the SPMC:
401 * - If UUID is non-Null and vm_count is zero it means there is no such
402 * partition identified in the system.
403 */
404 plat_ffa_partition_info_get_forward(uuid, partitions, &vm_count);
405
406 /*
407 * Unrecognized UUID: does not match any of the VMs (or SPs)
408 * and is not Null.
409 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100410 if (vm_count == 0) {
411 return ffa_error(FFA_INVALID_PARAMETERS);
412 }
413
414 size = vm_count * sizeof(partitions[0]);
415 if (size > FFA_MSG_PAYLOAD_MAX) {
416 dlog_error(
417 "Partition information does not fit in the VM's RX "
418 "buffer.\n");
419 return ffa_error(FFA_NO_MEMORY);
420 }
421
422 /*
423 * Partition information is returned in the VM's RX buffer, which is why
424 * the lock is needed.
425 */
426 current_vm_locked = vm_lock(current_vm);
427
428 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
429 /*
430 * Can't retrieve memory information if the mailbox is not
431 * available.
432 */
433 dlog_verbose("RX buffer not ready.\n");
434 ret = ffa_error(FFA_BUSY);
435 goto out_unlock;
436 }
437
438 /* Populate the VM's RX buffer with the partition information. */
439 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
440 size);
441 current_vm->mailbox.recv_size = size;
Olivier Depreze562e542020-06-11 17:31:54 +0200442
443 /* Sender is Hypervisor in the normal world (TEE in secure world). */
444 current_vm->mailbox.recv_sender = HF_VM_ID_BASE;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100445 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
446 current_vm->mailbox.state = MAILBOX_STATE_READ;
447
448 /* Return the count of partition information descriptors in w2. */
449 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
450
451out_unlock:
452 vm_unlock(&current_vm_locked);
453
454 return ret;
455}
456
Andrew Scull9726c252019-01-23 13:44:19 +0000457/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000458 * Returns the ID of the VM.
459 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100460struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000461{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100462 return (struct ffa_value){.func = FFA_SUCCESS_32,
463 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000464}
465
466/**
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000467 * Returns the ID of the SPMC.
468 */
469struct ffa_value api_ffa_spm_id_get(void)
470{
J-Alves3829fc02021-03-18 12:49:18 +0000471#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
472 /*
473 * Return the SPMC ID that was fetched during FF-A
474 * initialization.
475 */
476 return (struct ffa_value){.func = FFA_SUCCESS_32,
477 .arg2 = arch_ffa_spmc_id_get()};
478#else
479 return ffa_error(FFA_NOT_SUPPORTED);
480#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000481}
482
483/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000484 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000485 * function to indicate that register state for the given vCPU has been saved
486 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000487 */
488void api_regs_state_saved(struct vcpu *vcpu)
489{
490 sl_lock(&vcpu->lock);
491 vcpu->regs_available = true;
492 sl_unlock(&vcpu->lock);
493}
494
495/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000496 * Retrieves the next waiter and removes it from the wait list if the VM's
497 * mailbox is in a writable state.
498 */
499static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
500{
501 struct wait_entry *entry;
502 struct vm *vm = locked_vm.vm;
503
Andrew Sculld6ee1102019-04-05 22:12:42 +0100504 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000505 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
506 /* The mailbox is not writable or there are no waiters. */
507 return NULL;
508 }
509
510 /* Remove waiter from the wait list. */
511 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
512 wait_links);
513 list_remove(&entry->wait_links);
514 return entry;
515}
516
517/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000518 * Assuming that the arguments have already been checked by the caller, injects
519 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
520 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
521 * is next run, which is up to the scheduler.
522 *
523 * Returns:
524 * - 0 on success if no further action is needed.
525 * - 1 if it was called by the primary VM and the primary VM now needs to wake
526 * up or kick the target vCPU.
527 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100528int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
529 uint32_t intid, struct vcpu *current,
530 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000531{
Manish Pandey35e452f2021-02-18 21:36:34 +0000532 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000533 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000534 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
535 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000536 int64_t ret = 0;
537
Andrew Walbran508e63c2018-12-20 17:02:37 +0000538 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000539 * We only need to change state and (maybe) trigger a virtual interrupt
540 * if it is enabled and was not previously pending. Otherwise we can
541 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000542 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000543 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
544 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000545 intid_mask)) {
546 goto out;
547 }
548
549 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000550 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
551 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
552 vcpu_irq_count_increment(target_locked);
553 } else {
554 vcpu_fiq_count_increment(target_locked);
555 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000556
557 /*
558 * Only need to update state if there was not already an
559 * interrupt enabled and pending.
560 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000561 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000562 goto out;
563 }
564
Andrew Walbran508e63c2018-12-20 17:02:37 +0000565 if (current->vm->id == HF_PRIMARY_VM_ID) {
566 /*
567 * If the call came from the primary VM, let it know that it
568 * should run or kick the target vCPU.
569 */
570 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000571 } else if (current != target_vcpu && next != NULL) {
572 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000573 }
574
575out:
576 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000577 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000578
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200579 return ret;
580}
581
582/* Wrapper to internal_interrupt_inject with locking of target vCPU */
583static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
584 uint32_t intid, struct vcpu *current,
585 struct vcpu **next)
586{
587 int64_t ret;
588 struct vcpu_locked target_locked;
589
590 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100591 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200592 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000593
594 return ret;
595}
596
597/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100598 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
599 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100600 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100601static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100602{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000603 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100604 case FFA_MSG_SEND_32:
605 return (struct ffa_value){
606 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000607 .arg1 = (receiver->mailbox.recv_sender << 16) |
608 receiver->id,
609 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000610 default:
611 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000612 dlog_error("Tried to return an invalid message function %#x\n",
613 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100614 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000615 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100616}
617
618/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000619 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000620 * value needs to be forced onto the vCPU.
621 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000622static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100623 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000624{
Max Shvetsov40108e72020-08-27 12:39:50 +0100625 struct vcpu_locked vcpu_locked;
626 struct vm_locked vm_locked;
Andrew Scullb06d1752019-02-04 10:15:48 +0000627 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000628 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500629 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000630
Andrew Scullb06d1752019-02-04 10:15:48 +0000631 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000632 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000633 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100634 * The VM lock is not needed in the common case so it must only be taken
635 * when it is going to be needed. This ensures there are no inter-vCPU
636 * dependencies in the common run case meaning the sensitive context
637 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000638 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100639 vcpu_locked = vcpu_lock(vcpu);
640
641#if SECURE_WORLD == 1
642
643 if (vcpu_secondary_reset_and_start(vcpu_locked, vcpu->vm->secondary_ep,
644 0)) {
645 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
646 __func__, vcpu->vm->id, current->cpu->id);
647 }
648
649#endif
Andrew Scullb06d1752019-02-04 10:15:48 +0000650
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000651 /* The VM needs to be locked to deliver mailbox messages. */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500652 need_vm_lock = vcpu->state == VCPU_STATE_WAITING;
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000653 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100654 vcpu_unlock(&vcpu_locked);
655 vm_locked = vm_lock(vcpu->vm);
656 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000657 }
658
659 /*
660 * If the vCPU is already running somewhere then we can't run it here
661 * simultaneously. While it is actually running then the state should be
662 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
663 * stops running but while Hafnium is in the process of switching back
664 * to the primary there will be a brief period while the state has been
665 * updated but `regs_available` is still false (until
666 * `api_regs_state_saved` is called). We can't start running it again
667 * until this has finished, so count this state as still running for the
668 * purposes of this check.
669 */
670 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
671 /*
672 * vCPU is running on another pCPU.
673 *
674 * It's okay not to return the sleep duration here because the
675 * other physical CPU that is currently running this vCPU will
676 * return the sleep duration if needed.
677 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100678 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000679 ret = false;
680 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000681 }
Andrew Scull9726c252019-01-23 13:44:19 +0000682
683 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100684 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100685 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000686 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100687 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000688 }
689 ret = false;
690 goto out;
691 }
692
Andrew Walbran508e63c2018-12-20 17:02:37 +0000693 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100694 case VCPU_STATE_RUNNING:
695 case VCPU_STATE_OFF:
696 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000697 ret = false;
698 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000699
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500700 case VCPU_STATE_WAITING:
701 /*
702 * An initial FFA_RUN is necessary for secondary VM/SP to reach
703 * the message wait loop.
704 */
705 if (!vcpu->is_bootstrapped) {
706 vcpu->is_bootstrapped = true;
707 break;
708 }
709
Andrew Scullb06d1752019-02-04 10:15:48 +0000710 /*
711 * A pending message allows the vCPU to run so the message can
712 * be delivered directly.
713 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100714 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100715 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100717 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000718 break;
719 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500720
721 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
722 break;
723 }
724
725 if (arch_timer_enabled(&vcpu->regs)) {
726 timer_remaining_ns =
727 arch_timer_remaining_ns(&vcpu->regs);
728 if (timer_remaining_ns == 0) {
729 break;
730 }
731 } else {
732 dlog_verbose("Timer disabled\n");
733 }
734 run_ret->func = FFA_MSG_WAIT_32;
735 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
736 run_ret->arg2 = timer_remaining_ns;
737 ret = false;
738 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100739 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000740 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000741 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000742 break;
743 }
744
Andrew Walbran508e63c2018-12-20 17:02:37 +0000745 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100746 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000747 arch_timer_remaining_ns(&vcpu->regs);
748
749 /*
750 * The timer expired so allow the interrupt to be
751 * delivered.
752 */
753 if (timer_remaining_ns == 0) {
754 break;
755 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100756 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000757
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100758 /*
759 * The vCPU is not ready to run, return the appropriate code to
760 * the primary which called vcpu_run.
761 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500762 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100763 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
764 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000765
766 ret = false;
767 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000768
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500769 case VCPU_STATE_BLOCKED:
770 /* A blocked vCPU is run unconditionally. Fall through. */
771 case VCPU_STATE_PREEMPTED:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000772 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500773 default:
774 /*
775 * Execution not expected to reach here. Deny the request
776 * gracefully.
777 */
778 *run_ret = ffa_error(FFA_DENIED);
779 ret = false;
780 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000781 }
782
Andrew Scullb06d1752019-02-04 10:15:48 +0000783 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000784 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100785 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000786
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000787 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000788 * Mark the registers as unavailable now that we're about to reflect
789 * them onto the real registers. This will also prevent another physical
790 * CPU from trying to read these registers.
791 */
792 vcpu->regs_available = false;
793
794 ret = true;
795
796out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100797 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000798 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100799 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000800 }
801
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000802 return ret;
803}
804
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100805struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500806 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100807{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100808 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100809 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100810 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100811
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500812 if (!plat_ffa_run_checks(current, vm_id, &ret, next)) {
813 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100814 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100815
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700816 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
817 return ret;
818 }
819
Andrew Scull19503262018-09-20 14:48:39 +0100820 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100821 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100822 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100823 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100824 }
825
Fuad Tabbaed294af2019-12-20 10:43:01 +0000826 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100827 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100828 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100829 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100830
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000831 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100832 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000833 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000834 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100835 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000836
Andrew Walbran508e63c2018-12-20 17:02:37 +0000837 /*
838 * Inject timer interrupt if timer has expired. It's safe to access
839 * vcpu->regs here because api_vcpu_prepare_run already made sure that
840 * regs_available was true (and then set it to false) before returning
841 * true.
842 */
843 if (arch_timer_pending(&vcpu->regs)) {
844 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100845 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
846 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000847
848 /*
849 * Set the mask bit so the hardware interrupt doesn't fire
850 * again. Ideally we wouldn't do this because it affects what
851 * the secondary vCPU sees, but if we don't then we end up with
852 * a loop of the interrupt firing each time we try to return to
853 * the secondary vCPU.
854 */
855 arch_timer_mask(&vcpu->regs);
856 }
857
Fuad Tabbaed294af2019-12-20 10:43:01 +0000858 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000859 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000860
Andrew Scull33fecd32019-01-08 14:48:27 +0000861 /*
862 * Set a placeholder return code to the scheduler. This will be
863 * overwritten when the switch back to the primary occurs.
864 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100865 ret.func = FFA_INTERRUPT_32;
866 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100867 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000868
Andrew Scull6d2db332018-10-10 15:28:17 +0100869out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100870 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100871}
872
873/**
Andrew Scull81e85092018-12-12 12:56:20 +0000874 * Check that the mode indicates memory that is valid, owned and exclusive.
875 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100876static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000877{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100878 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
879 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000880}
881
882/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100883 * Determines the value to be returned by api_ffa_rxtx_map and
884 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
885 * there are waiters, it also switches back to the primary VM for it to wake
886 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000887 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100888static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
889 struct vcpu *current,
890 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000891{
892 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000893
894 if (list_empty(&vm->mailbox.waiter_list)) {
895 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100896 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000897 }
898
899 if (vm->id == HF_PRIMARY_VM_ID) {
900 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100901 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000902 }
903
904 /*
905 * Switch back to the primary VM, informing it that there are waiters
906 * that need to be notified.
907 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000908 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100909 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500910 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000911
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100912 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000913}
914
915/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100916 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100917 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100918static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
919 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100920 paddr_t pa_send_begin, paddr_t pa_send_end,
921 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200922 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100923 struct mpool *local_page_pool)
924{
925 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100926
927 /* Map the send page as read-only in the hypervisor address space. */
928 vm_locked.vm->mailbox.send =
929 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200930 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100931 if (!vm_locked.vm->mailbox.send) {
932 /* TODO: partial defrag of failed range. */
933 /* Recover any memory consumed in failed mapping. */
934 mm_defrag(mm_stage1_locked, local_page_pool);
935 goto fail;
936 }
937
938 /*
939 * Map the receive page as writable in the hypervisor address space. On
940 * failure, unmap the send page before returning.
941 */
942 vm_locked.vm->mailbox.recv =
943 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200944 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100945 if (!vm_locked.vm->mailbox.recv) {
946 /* TODO: partial defrag of failed range. */
947 /* Recover any memory consumed in failed mapping. */
948 mm_defrag(mm_stage1_locked, local_page_pool);
949 goto fail_undo_send;
950 }
951
952 ret = true;
953 goto out;
954
955 /*
956 * The following mappings will not require more memory than is available
957 * in the local pool.
958 */
959fail_undo_send:
960 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100961 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
962 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100963
964fail:
965 ret = false;
966
967out:
Andrew Sculle1322792019-07-01 17:46:10 +0100968 return ret;
969}
970
971/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100972 * Sanity checks and configures the send and receive pages in the VM stage-2
973 * and hypervisor stage-1 page tables.
974 *
975 * Returns:
976 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100977 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +0100978 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
979 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100980 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +0100981 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100982 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100983
984struct ffa_value api_vm_configure_pages(
985 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
986 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
987 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100988{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100989 struct ffa_value ret;
990 paddr_t pa_send_begin;
991 paddr_t pa_send_end;
992 paddr_t pa_recv_begin;
993 paddr_t pa_recv_end;
994 uint32_t orig_send_mode;
995 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200996 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100997
998 /* We only allow these to be setup once. */
999 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1000 ret = ffa_error(FFA_DENIED);
1001 goto out;
1002 }
1003
1004 /* Hafnium only supports a fixed size of RX/TX buffers. */
1005 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1006 ret = ffa_error(FFA_INVALID_PARAMETERS);
1007 goto out;
1008 }
1009
1010 /* Fail if addresses are not page-aligned. */
1011 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1012 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1013 ret = ffa_error(FFA_INVALID_PARAMETERS);
1014 goto out;
1015 }
1016
1017 /* Convert to physical addresses. */
1018 pa_send_begin = pa_from_ipa(send);
1019 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1020 pa_recv_begin = pa_from_ipa(recv);
1021 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1022
1023 /* Fail if the same page is used for the send and receive pages. */
1024 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1025 ret = ffa_error(FFA_INVALID_PARAMETERS);
1026 goto out;
1027 }
Andrew Sculle1322792019-07-01 17:46:10 +01001028
1029 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001030 * Ensure the pages are valid, owned and exclusive to the VM and that
1031 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +01001032 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001033 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1034 &orig_send_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001035 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1036 (orig_send_mode & MM_MODE_R) == 0 ||
1037 (orig_send_mode & MM_MODE_W) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001038 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001039 goto out;
1040 }
1041
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001042 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1043 &orig_recv_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001044 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1045 (orig_recv_mode & MM_MODE_R) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001046 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001047 goto out;
1048 }
Andrew Sculle1322792019-07-01 17:46:10 +01001049
1050 /* Take memory ownership away from the VM and mark as shared. */
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001051 uint32_t mode =
1052 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W;
1053 if (vm_locked.vm->el0_partition) {
1054 mode |= MM_MODE_USER | MM_MODE_NG;
1055 }
1056
1057 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, mode,
1058 local_page_pool, NULL)) {
Manish Pandeyd34f8892020-06-19 17:41:07 +01001059 ret = ffa_error(FFA_NO_MEMORY);
1060 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001061 }
1062
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001063 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1064 if (vm_locked.vm->el0_partition) {
1065 mode |= MM_MODE_USER | MM_MODE_NG;
1066 }
1067
1068 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, mode,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001069 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001070 /* TODO: partial defrag of failed range. */
1071 /* Recover any memory consumed in failed mapping. */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001072 vm_ptable_defrag(vm_locked, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001073 goto fail_undo_send;
1074 }
1075
Olivier Deprez96a2a262020-06-11 17:21:38 +02001076 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1077 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1078
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001079 /*
1080 * For EL0 partitions, since both the partition and the hypervisor code
1081 * use the EL2&0 translation regime, it is critical to mark the mappings
1082 * of the send and recv buffers as non-global in the TLB. For one, if we
1083 * dont mark it as non-global, it would cause TLB conflicts since there
1084 * would be an identity mapping with non-global attribute in the
1085 * partitions page tables, but another identity mapping in the
1086 * hypervisor page tables with the global attribute. The other issue is
1087 * one of security, we dont want other partitions to be able to access
1088 * other partitions buffers through cached translations.
1089 */
1090 if (vm_locked.vm->el0_partition) {
1091 extra_attributes |= MM_MODE_NG;
1092 }
1093
Manish Pandeyd34f8892020-06-19 17:41:07 +01001094 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1095 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001096 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001097 goto fail_undo_send_and_recv;
1098 }
1099
Manish Pandeyd34f8892020-06-19 17:41:07 +01001100 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001101 goto out;
1102
Andrew Sculle1322792019-07-01 17:46:10 +01001103fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001104 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001105 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001106
1107fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001108 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001109 orig_send_mode, local_page_pool, NULL));
1110 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001111
1112out:
Andrew Sculle1322792019-07-01 17:46:10 +01001113 return ret;
1114}
1115
1116/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001117 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001118 * must not be shared. Locking of the page tables combined with a local memory
1119 * pool ensures there will always be enough memory to recover from any errors
1120 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1121 * by another core which could result in this transaction being unable to roll
1122 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001123 *
1124 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001125 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001126 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001127 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001128 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001129 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001130 * - FFA_SUCCESS on success if no further action is needed.
1131 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001132 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001133 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001134struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
1135 uint32_t page_count, struct vcpu *current,
1136 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001137{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001138 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001140 struct vm_locked vm_locked;
1141 struct mm_stage1_locked mm_stage1_locked;
1142 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001143
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001144 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001145 * Create a local pool so any freed memory can't be used by another
1146 * thread. This is to ensure the original mapping can be restored if any
1147 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001148 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001149 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1150
Andrew Sculle1322792019-07-01 17:46:10 +01001151 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001152 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001153
Manish Pandeyd34f8892020-06-19 17:41:07 +01001154 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1155 page_count, &local_page_pool);
1156 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001157 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001158 }
1159
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001160 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001161 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001162
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001163exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001164 mpool_fini(&local_page_pool);
1165
1166 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001167 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001168
1169 return ret;
1170}
1171
1172/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001173 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1174 * translation regime of the caller. Unmap the region for the hypervisor and
1175 * set the memory region to owned and exclusive for the component. Since the
1176 * memory region mapped in the page table, when the buffers were originally
1177 * created we can safely remap it.
1178 *
1179 * Returns:
1180 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1181 * behalf of the caller.
1182 * - FFA_SUCCESS on success if no further action is needed.
1183 */
1184struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1185 struct vcpu *current)
1186{
1187 struct vm *vm = current->vm;
1188 struct vm_locked vm_locked;
1189 struct mm_stage1_locked mm_stage1_locked;
1190 paddr_t send_pa_begin;
1191 paddr_t send_pa_end;
1192 paddr_t recv_pa_begin;
1193 paddr_t recv_pa_end;
1194
1195 /*
1196 * Check there is a buffer pair registered on behalf of the caller.
1197 * Since forwarding is not yet supported the allocator ID MBZ.
1198 */
1199 if (allocator_id != 0) {
1200 dlog_error(
1201 "Forwarding MAP/UNMAP from the hypervisor is not yet "
1202 "supported so vm id must be zero.\n");
1203 return ffa_error(FFA_INVALID_PARAMETERS);
1204 }
1205
1206 /* Get send and receive buffers. */
1207 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001208 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001209 "No buffer pair registered on behalf of the caller.\n");
1210 return ffa_error(FFA_INVALID_PARAMETERS);
1211 }
1212
1213 /* Currently a mailbox size of 1 page is assumed. */
1214 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1215 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1216 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1217 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1218
1219 vm_locked = vm_lock(vm);
1220 mm_stage1_locked = mm_lock_stage1();
1221
1222 /*
1223 * Set the memory region of the buffers back to the default mode
1224 * for the VM. Since this memory region was already mapped for the
1225 * RXTX buffers we can safely remap them.
1226 */
1227 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1228 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1229 NULL));
1230
1231 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1232 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1233 NULL));
1234
1235 /* Unmap the buffers in the partition manager. */
1236 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1237 &api_page_pool));
1238 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1239 &api_page_pool));
1240
1241 vm->mailbox.send = NULL;
1242 vm->mailbox.recv = NULL;
1243
1244 mm_unlock_stage1(&mm_stage1_locked);
1245 vm_unlock(&vm_locked);
1246
1247 return (struct ffa_value){.func = FFA_SUCCESS_32};
1248}
1249
1250/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001251 * Notifies the `to` VM about the message currently in its mailbox, possibly
1252 * with the help of the primary VM.
1253 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001254static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1255 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001256{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001257 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1258 struct ffa_value primary_ret = {
1259 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001260 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001261 };
1262
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001263 /* Messages for the primary VM are delivered directly. */
1264 if (to.vm->id == HF_PRIMARY_VM_ID) {
1265 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001266 * Only tell the primary VM the size and other details if the
1267 * message is for it, to avoid leaking data about messages for
1268 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001269 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001270 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001271
1272 to.vm->mailbox.state = MAILBOX_STATE_READ;
1273 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001274 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001275 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001276 }
1277
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001278 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1279
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001280 /* Messages for the TEE are sent on via the dispatcher. */
1281 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001282 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001283
Olivier Deprez112d2b52020-09-30 07:39:23 +02001284 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001285 /*
1286 * After the call to the TEE completes it must have finished
1287 * reading its RX buffer, so it is ready for another message.
1288 */
1289 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001290 /*
1291 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001292 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001293 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001294 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001295 }
1296
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001297 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001298 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001299 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001300 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001301 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001302
1303 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001304}
1305
1306/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001307 * Copies data from the sender's send buffer to the recipient's receive buffer
1308 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001309 *
1310 * If the recipient's receive buffer is busy, it can optionally register the
1311 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001312 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001313struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1314 ffa_vm_id_t receiver_vm_id, uint32_t size,
1315 uint32_t attributes, struct vcpu *current,
1316 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001317{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001318 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001319 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001320 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001321 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001322 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001323 struct vcpu_locked current_locked;
1324 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001325 bool notify =
1326 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001327
Andrew Walbran70bc8622019-10-07 14:15:58 +01001328 /* Ensure sender VM ID corresponds to the current VM. */
1329 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001330 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001331 }
1332
1333 /* Disallow reflexive requests as this suggests an error in the VM. */
1334 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001335 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001336 }
1337
1338 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001339 if (size > FFA_MSG_PAYLOAD_MAX) {
1340 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001341 }
1342
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001343 /*
1344 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1345 * invocation.
1346 */
1347 current_locked = vcpu_lock(current);
1348 is_direct_request_ongoing =
1349 is_ffa_direct_msg_request_ongoing(current_locked);
1350 vcpu_unlock(&current_locked);
1351
1352 if (is_direct_request_ongoing) {
1353 return ffa_error(FFA_DENIED);
1354 }
1355
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001356 /* Ensure the receiver VM exists. */
1357 to = vm_find(receiver_vm_id);
1358 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001359 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001360 }
1361
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001362 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001363 * Check that the sender has configured its send buffer. If the tx
1364 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1365 * be safely accessed after releasing the lock since the tx mailbox
1366 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001367 */
1368 sl_lock(&from->lock);
1369 from_msg = from->mailbox.send;
1370 sl_unlock(&from->lock);
1371
1372 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001373 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001374 }
1375
Andrew Walbran82d6d152019-12-24 15:02:06 +00001376 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001377
Andrew Walbran82d6d152019-12-24 15:02:06 +00001378 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001379 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001380 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001381 }
1382
Andrew Walbran82d6d152019-12-24 15:02:06 +00001383 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001384 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001385 to->mailbox.recv_size = size;
1386 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001387 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001388 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001389
1390out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001391 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001392
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001393 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001394}
1395
1396/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001397 * Checks whether the vCPU's attempt to block for a message has already been
1398 * interrupted or whether it is allowed to block.
1399 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001400bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001401{
Manish Pandey35e452f2021-02-18 21:36:34 +00001402 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001403 bool interrupted;
1404
Manish Pandey35e452f2021-02-18 21:36:34 +00001405 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001406
1407 /*
1408 * Don't block if there are enabled and pending interrupts, to match
1409 * behaviour of wait_for_interrupt.
1410 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001411 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001412
Manish Pandey35e452f2021-02-18 21:36:34 +00001413 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001414
1415 return interrupted;
1416}
1417
1418/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001419 * Receives a message from the mailbox. If one isn't available, this function
1420 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001421 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001422 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001423 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001424struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1425 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001426{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001427 bool is_direct_request_ongoing;
1428 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001429 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001430 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001431 bool is_from_secure_world =
1432 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001433
Andrew Scullaa039b32018-10-04 15:02:26 +01001434 /*
1435 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001436 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001437 */
J-Alvesb37fd082020-10-22 12:29:21 +01001438 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001439 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001440 }
1441
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001442 /*
1443 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1444 * invocation.
1445 */
1446 current_locked = vcpu_lock(current);
1447 is_direct_request_ongoing =
1448 is_ffa_direct_msg_request_ongoing(current_locked);
1449 vcpu_unlock(&current_locked);
1450
1451 if (is_direct_request_ongoing) {
1452 return ffa_error(FFA_DENIED);
1453 }
1454
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001455 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001456
Andrew Scullaa039b32018-10-04 15:02:26 +01001457 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001458 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1459 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001460 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001461 goto out;
1462 }
1463
1464 /* No pending message so fail if not allowed to block. */
1465 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001466 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001467 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001468 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001469
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001470 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001471 * From this point onward this call can only be interrupted or a message
1472 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001474 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001475 return_code = ffa_error(FFA_INTERRUPTED);
1476 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001477 goto out;
1478 }
1479
J-Alvesb37fd082020-10-22 12:29:21 +01001480 if (is_from_secure_world) {
1481 /* Return to other world if caller is a SP. */
1482 *next = api_switch_to_other_world(
1483 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001484 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001485 } else {
1486 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001487 struct ffa_value run_return = {
1488 .func = FFA_MSG_WAIT_32,
1489 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001490 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001491
Andrew Walbranb4816552018-12-05 17:35:42 +00001492 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001493 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001494 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001495out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001496 sl_unlock(&vm->lock);
1497
Jose Marinho3e2442f2019-03-12 13:30:37 +00001498 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001499}
1500
1501/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001502 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1503 * by this function, the caller must have called api_mailbox_send before with
1504 * the notify argument set to true, and this call must have failed because the
1505 * mailbox was not available.
1506 *
1507 * It should be called repeatedly to retrieve a list of VMs.
1508 *
1509 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1510 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001511 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001512int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001513{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001514 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001515 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001516 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001517
1518 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001519 if (list_empty(&vm->mailbox.ready_list)) {
1520 ret = -1;
1521 goto exit;
1522 }
1523
1524 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1525 ready_links);
1526 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001527 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001528
1529exit:
1530 sl_unlock(&vm->lock);
1531 return ret;
1532}
1533
1534/**
1535 * Retrieves the next VM waiting to be notified that the mailbox of the
1536 * specified VM became writable. Only primary VMs are allowed to call this.
1537 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001538 * Returns -1 on failure or if there are no waiters; the VM id of the next
1539 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001540 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001541int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001542{
1543 struct vm *vm;
1544 struct vm_locked locked;
1545 struct wait_entry *entry;
1546 struct vm *waiting_vm;
1547
1548 /* Only primary VMs are allowed to call this function. */
1549 if (current->vm->id != HF_PRIMARY_VM_ID) {
1550 return -1;
1551 }
1552
Andrew Walbran42347a92019-05-09 13:59:03 +01001553 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001554 if (vm == NULL) {
1555 return -1;
1556 }
1557
Fuad Tabbaed294af2019-12-20 10:43:01 +00001558 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001559 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001560 entry = api_fetch_waiter(locked);
1561 vm_unlock(&locked);
1562
1563 if (entry == NULL) {
1564 return -1;
1565 }
1566
1567 /* Enqueue notification to waiting VM. */
1568 waiting_vm = entry->waiting_vm;
1569
1570 sl_lock(&waiting_vm->lock);
1571 if (list_empty(&entry->ready_links)) {
1572 list_append(&waiting_vm->mailbox.ready_list,
1573 &entry->ready_links);
1574 }
1575 sl_unlock(&waiting_vm->lock);
1576
1577 return waiting_vm->id;
1578}
1579
1580/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001581 * Releases the caller's mailbox so that a new message can be received. The
1582 * caller must have copied out all data they wish to preserve as new messages
1583 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001584 *
1585 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001586 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1587 * - FFA_SUCCESS on success if no further action is needed.
1588 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001589 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001590 * hf_mailbox_waiter_get.
1591 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001592struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001593{
1594 struct vm *vm = current->vm;
1595 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001596 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001597
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001598 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001599 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001600 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001601 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001602 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001603 break;
1604
Andrew Sculld6ee1102019-04-05 22:12:42 +01001605 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001606 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001607 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001608 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001609 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001610 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001611
1612 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001613}
Andrew Walbran318f5732018-11-20 16:23:42 +00001614
1615/**
1616 * Enables or disables a given interrupt ID for the calling vCPU.
1617 *
1618 * Returns 0 on success, or -1 if the intid is invalid.
1619 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001620int64_t api_interrupt_enable(uint32_t intid, bool enable,
1621 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001622{
Manish Pandey35e452f2021-02-18 21:36:34 +00001623 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001624 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001625 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1626 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001627
Andrew Walbran318f5732018-11-20 16:23:42 +00001628 if (intid >= HF_NUM_INTIDS) {
1629 return -1;
1630 }
1631
Manish Pandey35e452f2021-02-18 21:36:34 +00001632 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001633 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001634 /*
1635 * If it is pending and was not enabled before, increment the
1636 * count.
1637 */
1638 if (current->interrupts.interrupt_pending[intid_index] &
1639 ~current->interrupts.interrupt_enabled[intid_index] &
1640 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001641 if ((current->interrupts.interrupt_type[intid_index] &
1642 intid_mask) ==
1643 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1644 vcpu_irq_count_increment(current_locked);
1645 } else {
1646 vcpu_fiq_count_increment(current_locked);
1647 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001648 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001649 current->interrupts.interrupt_enabled[intid_index] |=
1650 intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001651
1652 if (type == INTERRUPT_TYPE_IRQ) {
1653 current->interrupts.interrupt_type[intid_index] &=
1654 ~intid_mask;
1655 } else if (type == INTERRUPT_TYPE_FIQ) {
1656 current->interrupts.interrupt_type[intid_index] |=
1657 intid_mask;
1658 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001659 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001660 /*
1661 * If it is pending and was enabled before, decrement the count.
1662 */
1663 if (current->interrupts.interrupt_pending[intid_index] &
1664 current->interrupts.interrupt_enabled[intid_index] &
1665 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001666 if ((current->interrupts.interrupt_type[intid_index] &
1667 intid_mask) ==
1668 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1669 vcpu_irq_count_decrement(current_locked);
1670 } else {
1671 vcpu_fiq_count_decrement(current_locked);
1672 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001673 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001674 current->interrupts.interrupt_enabled[intid_index] &=
1675 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001676 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001677 }
1678
Manish Pandey35e452f2021-02-18 21:36:34 +00001679 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001680 return 0;
1681}
1682
1683/**
1684 * Returns the ID of the next pending interrupt for the calling vCPU, and
1685 * acknowledges it (i.e. marks it as no longer pending). Returns
1686 * HF_INVALID_INTID if there are no pending interrupts.
1687 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001688uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001689{
1690 uint8_t i;
1691 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001692 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001693
1694 /*
1695 * Find the first enabled and pending interrupt ID, return it, and
1696 * deactivate it.
1697 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001698 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001699 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1700 uint32_t enabled_and_pending =
1701 current->interrupts.interrupt_enabled[i] &
1702 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001703
Andrew Walbran318f5732018-11-20 16:23:42 +00001704 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001705 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001706 uint32_t intid_mask = 1U << bit_index;
1707
Andrew Walbran3d84a262018-12-13 14:41:19 +00001708 /*
1709 * Mark it as no longer pending and decrement the count.
1710 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001711 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1712
1713 if ((current->interrupts.interrupt_type[i] &
1714 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1715 vcpu_irq_count_decrement(current_locked);
1716 } else {
1717 vcpu_fiq_count_decrement(current_locked);
1718 }
1719
Andrew Walbran3d84a262018-12-13 14:41:19 +00001720 first_interrupt =
1721 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001722 break;
1723 }
1724 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001725
Manish Pandey35e452f2021-02-18 21:36:34 +00001726 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001727 return first_interrupt;
1728}
1729
1730/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001731 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001732 * given VM and vCPU.
1733 */
1734static inline bool is_injection_allowed(uint32_t target_vm_id,
1735 struct vcpu *current)
1736{
1737 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001738
Andrew Walbran318f5732018-11-20 16:23:42 +00001739 /*
1740 * The primary VM is allowed to inject interrupts into any VM. Secondary
1741 * VMs are only allowed to inject interrupts into their own vCPUs.
1742 */
1743 return current_vm_id == HF_PRIMARY_VM_ID ||
1744 current_vm_id == target_vm_id;
1745}
1746
1747/**
1748 * Injects a virtual interrupt of the given ID into the given target vCPU.
1749 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1750 * when the vCPU is next run, which is up to the scheduler.
1751 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001752 * Returns:
1753 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1754 * ID is invalid, or the current VM is not allowed to inject interrupts to
1755 * the target VM.
1756 * - 0 on success if no further action is needed.
1757 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1758 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001759 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001760int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1761 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001762 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001763{
Andrew Walbran318f5732018-11-20 16:23:42 +00001764 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001765 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001766
1767 if (intid >= HF_NUM_INTIDS) {
1768 return -1;
1769 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001770
Andrew Walbran318f5732018-11-20 16:23:42 +00001771 if (target_vm == NULL) {
1772 return -1;
1773 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001774
Andrew Walbran318f5732018-11-20 16:23:42 +00001775 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001776 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001777 return -1;
1778 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001779
Andrew Walbran318f5732018-11-20 16:23:42 +00001780 if (!is_injection_allowed(target_vm_id, current)) {
1781 return -1;
1782 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001783
Andrew Walbrane1310df2019-04-29 17:28:28 +01001784 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001785
Manish Pandey35e452f2021-02-18 21:36:34 +00001786 dlog_verbose(
1787 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1788 "%u\n",
1789 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1790 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001791 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001792}
Andrew Scull6386f252018-12-06 13:29:10 +00001793
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001794/** Returns the version of the implemented FF-A specification. */
1795struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001796{
1797 /*
1798 * Ensure that both major and minor revision representation occupies at
1799 * most 15 bits.
1800 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001801 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001802 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001803 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001804 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001805 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001806 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001807 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001808 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001809
Daniel Boulby6e32c612021-02-17 15:09:41 +00001810 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001811}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001812
1813int64_t api_debug_log(char c, struct vcpu *current)
1814{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001815 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001816 struct vm *vm = current->vm;
1817 struct vm_locked vm_locked = vm_lock(vm);
1818
Andrew Sculld54e1be2019-08-20 11:09:42 +01001819 if (c == '\n' || c == '\0') {
1820 flush = true;
1821 } else {
1822 vm->log_buffer[vm->log_buffer_length++] = c;
1823 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1824 }
1825
1826 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001827 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1828 vm->log_buffer_length);
1829 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001830 }
1831
1832 vm_unlock(&vm_locked);
1833
1834 return 0;
1835}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001836
1837/**
J-Alves6f72ca82021-11-01 12:34:58 +00001838 * Helper for success return of FFA_FEATURES, for when it is used to query
1839 * an interrupt ID.
1840 */
1841struct ffa_value api_ffa_feature_success(uint32_t arg2)
1842{
1843 return (struct ffa_value){
1844 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
1845}
1846
1847/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001848 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001849 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001850 */
J-Alves6f72ca82021-11-01 12:34:58 +00001851struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001852{
J-Alves6f72ca82021-11-01 12:34:58 +00001853 /*
1854 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
1855 * if using a feature ID.
1856 */
1857 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
1858 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0) {
1859 return ffa_error(FFA_NOT_SUPPORTED);
1860 }
1861
1862 switch (feature_function_id) {
1863 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001864 case FFA_ERROR_32:
1865 case FFA_SUCCESS_32:
1866 case FFA_INTERRUPT_32:
1867 case FFA_VERSION_32:
1868 case FFA_FEATURES_32:
1869 case FFA_RX_RELEASE_32:
1870 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001871 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001872 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001873 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001874 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001875 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001876 case FFA_MEM_DONATE_32:
1877 case FFA_MEM_LEND_32:
1878 case FFA_MEM_SHARE_32:
1879 case FFA_MEM_RETRIEVE_REQ_32:
1880 case FFA_MEM_RETRIEVE_RESP_32:
1881 case FFA_MEM_RELINQUISH_32:
1882 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001883 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001884 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001885 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001886 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00001887#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00001888 /* FF-A v1.1 features. */
1889 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00001890 case FFA_NOTIFICATION_BITMAP_CREATE_32:
1891 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
1892 case FFA_NOTIFICATION_BIND_32:
1893 case FFA_NOTIFICATION_UNBIND_32:
1894 case FFA_NOTIFICATION_SET_32:
1895 case FFA_NOTIFICATION_GET_32:
1896 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07001897 case FFA_MEM_PERM_GET_32:
1898 case FFA_MEM_PERM_SET_32:
1899 case FFA_MEM_PERM_GET_64:
1900 case FFA_MEM_PERM_SET_64:
J-Alves3829fc02021-03-18 12:49:18 +00001901#endif
1902 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00001903
1904#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
1905 /* Check support of a feature provided respective feature ID. */
1906 case FFA_FEATURE_NPI:
1907 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
1908 case FFA_FEATURE_SRI:
1909 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
1910#endif
1911 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001912 default:
J-Alves6f72ca82021-11-01 12:34:58 +00001913 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001914 }
1915}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001916
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001917/**
J-Alves645eabe2021-02-22 16:08:27 +00001918 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
1919 * interfaces.
1920 */
1921static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
1922{
1923 return args.arg2 == 0U;
1924}
1925
1926/**
J-Alves76d99af2021-03-10 17:42:11 +00001927 * Limits size of arguments in ffa_value structure to 32-bit.
1928 */
1929static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
1930{
1931 return (struct ffa_value){
1932 .func = (uint32_t)args.func,
1933 .arg1 = (uint32_t)args.arg1,
1934 .arg2 = (uint32_t)0,
1935 .arg3 = (uint32_t)args.arg3,
1936 .arg4 = (uint32_t)args.arg4,
1937 .arg5 = (uint32_t)args.arg5,
1938 .arg6 = (uint32_t)args.arg6,
1939 .arg7 = (uint32_t)args.arg7,
1940 };
1941}
1942
1943/**
1944 * Helper to copy direct message payload, depending on SMC used and expected
1945 * registers size.
1946 */
1947static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
1948{
1949 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
1950 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
1951 return api_ffa_value_copy32(args);
1952 }
1953
1954 return (struct ffa_value){
1955 .func = args.func,
1956 .arg1 = args.arg1,
1957 .arg2 = 0,
1958 .arg3 = args.arg3,
1959 .arg4 = args.arg4,
1960 .arg5 = args.arg5,
1961 .arg6 = args.arg6,
1962 .arg7 = args.arg7,
1963 };
1964}
1965
1966/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001967 * Send an FF-A direct message request.
1968 */
1969struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1970 ffa_vm_id_t receiver_vm_id,
1971 struct ffa_value args,
1972 struct vcpu *current,
1973 struct vcpu **next)
1974{
J-Alves17228f72021-04-20 17:13:19 +01001975 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001976 struct vm *receiver_vm;
1977 struct vcpu *receiver_vcpu;
1978 struct two_vcpu_locked vcpus_locked;
1979
J-Alves645eabe2021-02-22 16:08:27 +00001980 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1981 return ffa_error(FFA_INVALID_PARAMETERS);
1982 }
1983
Olivier Deprez55a189e2021-06-09 15:45:27 +02001984 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
1985 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001986 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001987 }
1988
Olivier Deprez55a189e2021-06-09 15:45:27 +02001989 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01001990 return ret;
1991 }
1992
1993 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
1994
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001995 receiver_vm = vm_find(receiver_vm_id);
1996 if (receiver_vm == NULL) {
1997 return ffa_error(FFA_INVALID_PARAMETERS);
1998 }
1999
2000 /*
2001 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
2002 * UP (migratable) or MP partitions with a number of vCPUs matching the
2003 * number of PEs in the system. It further states that MP partitions
2004 * accepting direct request messages cannot migrate.
2005 */
J-Alvesad6a0432021-04-09 16:06:21 +01002006 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002007 if (receiver_vcpu == NULL) {
2008 return ffa_error(FFA_INVALID_PARAMETERS);
2009 }
2010
2011 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2012
2013 /*
2014 * If destination vCPU is executing or already received an
2015 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2016 * busy. There is a brief period of time where the vCPU state has
2017 * changed but regs_available is still false thus consider this case as
2018 * the vCPU not yet ready to receive a direct message request.
2019 */
2020 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2021 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2022 !receiver_vcpu->regs_available) {
2023 ret = ffa_error(FFA_BUSY);
2024 goto out;
2025 }
2026
2027 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2028 memory_order_relaxed)) {
2029 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002030 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002031 receiver_vcpu->vm->id,
2032 vcpu_index(receiver_vcpu));
2033 receiver_vcpu->state = VCPU_STATE_ABORTED;
2034 }
2035
2036 ret = ffa_error(FFA_ABORTED);
2037 goto out;
2038 }
2039
2040 switch (receiver_vcpu->state) {
2041 case VCPU_STATE_OFF:
2042 case VCPU_STATE_RUNNING:
2043 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002044 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002045 case VCPU_STATE_BLOCKED:
2046 case VCPU_STATE_PREEMPTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002047 ret = ffa_error(FFA_BUSY);
2048 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002049 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002050 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002051 * We expect target vCPU to be in WAITING state after either
2052 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002053 */
2054 break;
2055 }
2056
2057 /* Inject timer interrupt if any pending */
2058 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002059 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2060 HF_VIRTUAL_TIMER_INTID, current,
2061 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002062
2063 arch_timer_mask(&receiver_vcpu->regs);
2064 }
2065
2066 /* The receiver vCPU runs upon direct message invocation */
2067 receiver_vcpu->cpu = current->cpu;
2068 receiver_vcpu->state = VCPU_STATE_RUNNING;
2069 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002070 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002071
J-Alves76d99af2021-03-10 17:42:11 +00002072 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002073
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002074 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002075
2076 /* Switch to receiver vCPU targeted to by direct msg request */
2077 *next = receiver_vcpu;
2078
2079 /*
2080 * Since this flow will lead to a VM switch, the return value will not
2081 * be applied to current vCPU.
2082 */
2083
2084out:
2085 sl_unlock(&receiver_vcpu->lock);
2086 sl_unlock(&current->lock);
2087
2088 return ret;
2089}
2090
2091/**
2092 * Send an FF-A direct message response.
2093 */
2094struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2095 ffa_vm_id_t receiver_vm_id,
2096 struct ffa_value args,
2097 struct vcpu *current,
2098 struct vcpu **next)
2099{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002100 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002101
2102 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2103 return ffa_error(FFA_INVALID_PARAMETERS);
2104 }
2105
J-Alves76d99af2021-03-10 17:42:11 +00002106 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002107
Olivier Deprez55a189e2021-06-09 15:45:27 +02002108 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2109 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002110 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002111 }
2112
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002113 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002114 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002115 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002116 * No need for REQ/RESP state management as managed exit does
2117 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002118 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002119 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2120 vcpu_unlock(&current_locked);
2121 return ffa_error(FFA_DENIED);
2122 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002123
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002124 /*
2125 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2126 * a secure interrupt to a SP that is performing managed exit.
2127 * We have taken a implementation defined choice to not allow
2128 * Managed exit while a SP is processing a secure interrupt.
2129 */
2130 CHECK(!current->processing_secure_interrupt);
2131
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002132 plat_interrupts_set_priority_mask(0xff);
2133 current->processing_managed_exit = false;
2134 } else {
2135 /*
2136 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2137 * defined originator.
2138 */
2139 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2140 /*
2141 * Sending direct response but direct request origin
2142 * vCPU is not set.
2143 */
2144 vcpu_unlock(&current_locked);
2145 return ffa_error(FFA_DENIED);
2146 }
2147
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002148 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002149 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2150 vcpu_unlock(&current_locked);
2151 return ffa_error(FFA_DENIED);
2152 }
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002153
2154 /*
2155 * Per FF-A v1.1 Beta0 section 7.4, if a secure interrupt is
2156 * handled by an SP in RUNNING state the existing runtime model
2157 * is preserved. Hence, per section 7.3 bullet 3, SP can use
2158 * FFA_MSG_SEND_DIRECT_RESP to return a response after
2159 * interrupt completion.
2160 */
2161 if (current->processing_secure_interrupt) {
2162 /* There is no preempted vCPU to resume. */
2163 CHECK(current->preempted_vcpu == NULL);
2164
2165 /* Unmask interrupts. */
2166 plat_interrupts_set_priority_mask(0xff);
2167
2168 /*
2169 * Clear fields corresponding to secure interrupt
2170 * handling.
2171 */
2172 current->processing_secure_interrupt = false;
2173 current->secure_interrupt_deactivated = false;
2174 current->current_sec_interrupt_id = 0;
2175 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002176 }
2177
2178 /* Clear direct request origin for the caller. */
2179 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2180
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002181 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002182
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002183 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002184 *next = api_switch_to_other_world(
2185 current, to_ret,
2186 /*
2187 * Current vcpu sent a direct response. It moves to
2188 * waiting state.
2189 */
2190 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002191 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002192 *next = api_switch_to_primary(
2193 current, to_ret,
2194 /*
2195 * Current vcpu sent a direct response. It moves to
2196 * waiting state.
2197 */
2198 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002199 } else if (vm_id_is_current_world(receiver_vm_id)) {
2200 /*
2201 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002202 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002203 * made function return error before getting to this point.
2204 */
2205 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002206 /*
2207 * current vcpu sent a direct response.
2208 * It moves to waiting state.
2209 */
2210 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002211 } else {
2212 panic("Invalid direct message response invocation");
2213 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002214
2215 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2216}
2217
J-Alves84658fc2021-06-17 14:37:32 +01002218static bool api_memory_region_check_flags(
2219 struct ffa_memory_region *memory_region, uint32_t share_func)
2220{
2221 switch (share_func) {
2222 case FFA_MEM_SHARE_32:
2223 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2224 0U) {
2225 return false;
2226 }
2227 /* Intentional fall-through */
2228 case FFA_MEM_LEND_32:
2229 case FFA_MEM_DONATE_32: {
2230 /* Bits 31:2 Must Be Zero. */
2231 ffa_memory_receiver_flags_t to_mask =
2232 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2233 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2234
2235 if ((memory_region->flags & to_mask) != 0U) {
2236 return false;
2237 }
2238 break;
2239 }
2240 default:
2241 panic("Check for mem send calls only.\n");
2242 }
2243
2244 /* Last check reserved values are 0 */
2245 return true;
2246}
2247
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002248struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2249 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002250 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002251{
2252 struct vm *from = current->vm;
2253 struct vm *to;
2254 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002255 struct ffa_memory_region *memory_region;
2256 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002257
2258 if (ipa_addr(address) != 0 || page_count != 0) {
2259 /*
2260 * Hafnium only supports passing the descriptor in the TX
2261 * mailbox.
2262 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002263 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002264 }
2265
Andrew Walbranca808b12020-05-15 17:22:28 +01002266 if (fragment_length > length) {
2267 dlog_verbose(
2268 "Fragment length %d greater than total length %d.\n",
2269 fragment_length, length);
2270 return ffa_error(FFA_INVALID_PARAMETERS);
2271 }
2272 if (fragment_length < sizeof(struct ffa_memory_region) +
2273 sizeof(struct ffa_memory_access)) {
2274 dlog_verbose(
2275 "Initial fragment length %d smaller than header size "
2276 "%d.\n",
2277 fragment_length,
2278 sizeof(struct ffa_memory_region) +
2279 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002280 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002281 }
2282
2283 /*
2284 * Check that the sender has configured its send buffer. If the TX
2285 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2286 * be safely accessed after releasing the lock since the TX mailbox
2287 * address can only be configured once.
2288 */
2289 sl_lock(&from->lock);
2290 from_msg = from->mailbox.send;
2291 sl_unlock(&from->lock);
2292
2293 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002294 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002295 }
2296
2297 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002298 * Copy the memory region descriptor to a fresh page from the memory
2299 * pool. This prevents the sender from changing it underneath us, and
2300 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002301 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002302 if (fragment_length > HF_MAILBOX_SIZE ||
2303 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002304 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002305 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002306 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002307 if (memory_region == NULL) {
2308 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002309 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002310 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002311 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002312
2313 /* The sender must match the caller. */
2314 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002315 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002316 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002317 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002318 }
2319
J-Alves84658fc2021-06-17 14:37:32 +01002320 if (!api_memory_region_check_flags(memory_region, share_func)) {
2321 dlog_verbose(
2322 "Memory region reserved arguments must be zero.\n");
2323 ret = ffa_error(FFA_INVALID_PARAMETERS);
2324 goto out;
2325 }
2326
Andrew Walbrana65a1322020-04-06 19:32:32 +01002327 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002328 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002329 dlog_verbose(
2330 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002331 "endpoint memory access descriptors, expected 1).\n",
2332 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002333 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002334 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002335 }
2336
2337 /*
2338 * Ensure that the receiver VM exists and isn't the same as the sender.
2339 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002340 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002341 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002342 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002343 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002344 goto out;
2345 }
2346
2347 if (to->id == HF_TEE_VM_ID) {
2348 /*
2349 * The 'to' VM lock is only needed in the case that it is the
2350 * TEE VM.
2351 */
2352 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2353
2354 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002355 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002356 goto out_unlock;
2357 }
2358
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002359 ret = ffa_memory_tee_send(
2360 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2361 length, fragment_length, share_func, &api_page_pool);
2362 /*
2363 * ffa_tee_memory_send takes ownership of the memory_region, so
2364 * make sure we don't free it.
2365 */
2366 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002367
2368 out_unlock:
2369 vm_unlock(&vm_to_from_lock.vm1);
2370 vm_unlock(&vm_to_from_lock.vm2);
2371 } else {
2372 struct vm_locked from_locked = vm_lock(from);
2373
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002374 ret = ffa_memory_send(from_locked, memory_region, length,
2375 fragment_length, share_func,
2376 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002377 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002378 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002379 * make sure we don't free it.
2380 */
2381 memory_region = NULL;
2382
2383 vm_unlock(&from_locked);
2384 }
2385
2386out:
2387 if (memory_region != NULL) {
2388 mpool_free(&api_page_pool, memory_region);
2389 }
2390
2391 return ret;
2392}
2393
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002394struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2395 uint32_t fragment_length,
2396 ipaddr_t address, uint32_t page_count,
2397 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002398{
2399 struct vm *to = current->vm;
2400 struct vm_locked to_locked;
2401 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002402 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002403 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002404 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002405
2406 if (ipa_addr(address) != 0 || page_count != 0) {
2407 /*
2408 * Hafnium only supports passing the descriptor in the TX
2409 * mailbox.
2410 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002411 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002412 }
2413
Andrew Walbrana65a1322020-04-06 19:32:32 +01002414 if (fragment_length != length) {
2415 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002416 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002417 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002418
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002419 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002420 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002421 message_buffer_size = cpu_get_buffer_size(current->cpu);
2422 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2423 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002424 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002425 }
2426
2427 to_locked = vm_lock(to);
2428 to_msg = to->mailbox.send;
2429
2430 if (to_msg == NULL) {
2431 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002432 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002433 goto out;
2434 }
2435
2436 /*
2437 * Copy the retrieve request descriptor to an internal buffer, so that
2438 * the caller can't change it underneath us.
2439 */
2440 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2441
2442 if (msg_receiver_busy(to_locked, NULL, false)) {
2443 /*
2444 * Can't retrieve memory information if the mailbox is not
2445 * available.
2446 */
2447 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002448 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002449 goto out;
2450 }
2451
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002452 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2453 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002454
2455out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002456 vm_unlock(&to_locked);
2457 return ret;
2458}
2459
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002460struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002461{
2462 struct vm *from = current->vm;
2463 struct vm_locked from_locked;
2464 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002465 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002466 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002467 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002468 uint32_t length;
2469
2470 from_locked = vm_lock(from);
2471 from_msg = from->mailbox.send;
2472
2473 if (from_msg == NULL) {
2474 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002475 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002476 goto out;
2477 }
2478
2479 /*
2480 * Calculate length from relinquish descriptor before copying. We will
2481 * check again later to make sure it hasn't changed.
2482 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002483 length = sizeof(struct ffa_mem_relinquish) +
2484 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2485 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002486 /*
2487 * Copy the relinquish descriptor to an internal buffer, so that the
2488 * caller can't change it underneath us.
2489 */
2490 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002491 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002492 message_buffer_size = cpu_get_buffer_size(current->cpu);
2493 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2494 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002495 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002496 goto out;
2497 }
2498 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2499
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002500 if (sizeof(struct ffa_mem_relinquish) +
2501 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002502 length) {
2503 dlog_verbose(
2504 "Endpoint count changed while copying to internal "
2505 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002506 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002507 goto out;
2508 }
2509
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002510 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2511 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002512
2513out:
2514 vm_unlock(&from_locked);
2515 return ret;
2516}
2517
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002518struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2519 ffa_memory_region_flags_t flags,
2520 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002521{
2522 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002523 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002524
Olivier Deprez55a189e2021-06-09 15:45:27 +02002525 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002526 struct vm_locked to_locked = vm_lock(to);
2527
Andrew Walbranca808b12020-05-15 17:22:28 +01002528 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002529 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002530
Andrew Walbran290b0c92020-02-03 16:37:14 +00002531 vm_unlock(&to_locked);
2532 } else {
2533 struct vm *from = vm_find(HF_TEE_VM_ID);
2534 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2535
Andrew Walbranca808b12020-05-15 17:22:28 +01002536 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2537 vm_to_from_lock.vm2, handle, flags,
2538 &api_page_pool);
2539
2540 vm_unlock(&vm_to_from_lock.vm1);
2541 vm_unlock(&vm_to_from_lock.vm2);
2542 }
2543
2544 return ret;
2545}
2546
2547struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2548 uint32_t fragment_offset,
2549 ffa_vm_id_t sender_vm_id,
2550 struct vcpu *current)
2551{
2552 struct vm *to = current->vm;
2553 struct vm_locked to_locked;
2554 struct ffa_value ret;
2555
2556 /* Sender ID MBZ at virtual instance. */
2557 if (sender_vm_id != 0) {
2558 return ffa_error(FFA_INVALID_PARAMETERS);
2559 }
2560
2561 to_locked = vm_lock(to);
2562
2563 if (msg_receiver_busy(to_locked, NULL, false)) {
2564 /*
2565 * Can't retrieve memory information if the mailbox is not
2566 * available.
2567 */
2568 dlog_verbose("RX buffer not ready.\n");
2569 ret = ffa_error(FFA_BUSY);
2570 goto out;
2571 }
2572
2573 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2574 &api_page_pool);
2575
2576out:
2577 vm_unlock(&to_locked);
2578 return ret;
2579}
2580
2581struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2582 uint32_t fragment_length,
2583 ffa_vm_id_t sender_vm_id,
2584 struct vcpu *current)
2585{
2586 struct vm *from = current->vm;
2587 const void *from_msg;
2588 void *fragment_copy;
2589 struct ffa_value ret;
2590
2591 /* Sender ID MBZ at virtual instance. */
2592 if (sender_vm_id != 0) {
2593 return ffa_error(FFA_INVALID_PARAMETERS);
2594 }
2595
2596 /*
2597 * Check that the sender has configured its send buffer. If the TX
2598 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2599 * be safely accessed after releasing the lock since the TX mailbox
2600 * address can only be configured once.
2601 */
2602 sl_lock(&from->lock);
2603 from_msg = from->mailbox.send;
2604 sl_unlock(&from->lock);
2605
2606 if (from_msg == NULL) {
2607 return ffa_error(FFA_INVALID_PARAMETERS);
2608 }
2609
2610 /*
2611 * Copy the fragment to a fresh page from the memory pool. This prevents
2612 * the sender from changing it underneath us, and also lets us keep it
2613 * around in the share state table if needed.
2614 */
2615 if (fragment_length > HF_MAILBOX_SIZE ||
2616 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2617 dlog_verbose(
2618 "Fragment length %d larger than mailbox size %d.\n",
2619 fragment_length, HF_MAILBOX_SIZE);
2620 return ffa_error(FFA_INVALID_PARAMETERS);
2621 }
2622 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2623 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2624 0) {
2625 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2626 return ffa_error(FFA_INVALID_PARAMETERS);
2627 }
2628 fragment_copy = mpool_alloc(&api_page_pool);
2629 if (fragment_copy == NULL) {
2630 dlog_verbose("Failed to allocate fragment copy.\n");
2631 return ffa_error(FFA_NO_MEMORY);
2632 }
2633 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2634
2635 /*
2636 * Hafnium doesn't support fragmentation of memory retrieve requests
2637 * (because it doesn't support caller-specified mappings, so a request
2638 * will never be larger than a single page), so this must be part of a
2639 * memory send (i.e. donate, lend or share) request.
2640 *
2641 * We can tell from the handle whether the memory transaction is for the
2642 * TEE or not.
2643 */
2644 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2645 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2646 struct vm_locked from_locked = vm_lock(from);
2647
2648 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2649 fragment_length, handle,
2650 &api_page_pool);
2651 /*
2652 * `ffa_memory_send_continue` takes ownership of the
2653 * fragment_copy, so we don't need to free it here.
2654 */
2655 vm_unlock(&from_locked);
2656 } else {
2657 struct vm *to = vm_find(HF_TEE_VM_ID);
2658 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2659
2660 /*
2661 * The TEE RX buffer state is checked in
2662 * `ffa_memory_tee_send_continue` rather than here, as we need
2663 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2664 * than FFA_ERROR FFA_BUSY in case it is busy.
2665 */
2666
2667 ret = ffa_memory_tee_send_continue(
2668 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2669 fragment_length, handle, &api_page_pool);
2670 /*
2671 * `ffa_memory_tee_send_continue` takes ownership of the
2672 * fragment_copy, so we don't need to free it here.
2673 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002674
2675 vm_unlock(&vm_to_from_lock.vm1);
2676 vm_unlock(&vm_to_from_lock.vm2);
2677 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002678
2679 return ret;
2680}
Max Shvetsov40108e72020-08-27 12:39:50 +01002681
2682struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2683 struct vcpu *current)
2684{
2685 struct vm_locked vm_locked;
2686
2687 vm_locked = vm_lock(current->vm);
2688 vm_locked.vm->secondary_ep = entry_point;
2689 vm_unlock(&vm_locked);
2690
2691 return (struct ffa_value){.func = FFA_SUCCESS_32};
2692}
J-Alvesa0f317d2021-06-09 13:31:59 +01002693
2694struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
2695 ffa_vcpu_count_t vcpu_count,
2696 struct vcpu *current)
2697{
2698 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2699 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
2700 vm_id);
2701 return ffa_error(FFA_NOT_SUPPORTED);
2702 }
2703
2704 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
2705}
2706
2707struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
2708 struct vcpu *current)
2709{
2710 /*
2711 * Validity of use of this interface is the same as for bitmap create.
2712 */
2713 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2714 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
2715 vm_id);
2716 return ffa_error(FFA_NOT_SUPPORTED);
2717 }
2718
2719 return plat_ffa_notifications_bitmap_destroy(vm_id);
2720}
J-Alvesc003a7a2021-03-18 13:06:53 +00002721
2722struct ffa_value api_ffa_notification_update_bindings(
2723 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2724 ffa_notifications_bitmap_t notifications, bool is_bind,
2725 struct vcpu *current)
2726{
2727 struct ffa_value ret = {.func = FFA_SUCCESS_32};
2728 struct vm_locked receiver_locked;
2729 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2730 const ffa_vm_id_t id_to_update =
2731 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
2732 const ffa_vm_id_t id_to_validate =
2733 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
2734
2735 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
2736 receiver_vm_id)) {
2737 dlog_verbose("Invalid use of notifications bind interface.\n");
2738 return ffa_error(FFA_INVALID_PARAMETERS);
2739 }
2740
J-Alvesb15e9402021-09-08 11:44:42 +01002741 if (plat_ffa_notifications_update_bindings_forward(
2742 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
2743 &ret)) {
2744 dlog_verbose("Forwarding call to other world.\n");
2745 return ret;
2746 }
2747
J-Alvesc003a7a2021-03-18 13:06:53 +00002748 if (notifications == 0U) {
2749 dlog_verbose("No notifications have been specified.\n");
2750 return ffa_error(FFA_INVALID_PARAMETERS);
2751 }
2752
2753 /**
2754 * This check assumes receiver is the current VM, and has been enforced
2755 * by 'plat_ffa_is_notifications_bind_valid'.
2756 */
2757 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2758
2759 if (receiver_locked.vm == NULL) {
2760 dlog_verbose("Receiver doesn't exist!\n");
2761 return ffa_error(FFA_DENIED);
2762 }
2763
J-Alves09ff9d82021-11-02 11:55:20 +00002764 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00002765 dlog_verbose("Notifications are not enabled.\n");
2766 ret = ffa_error(FFA_NOT_SUPPORTED);
2767 goto out;
2768 }
2769
2770 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
2771 vm_find(sender_vm_id) == NULL) {
2772 dlog_verbose("Sender VM does not exist!\n");
2773 ret = ffa_error(FFA_INVALID_PARAMETERS);
2774 goto out;
2775 }
2776
2777 /*
2778 * Can't bind/unbind notifications if at least one is bound to a
2779 * different sender.
2780 */
2781 if (!vm_notifications_validate_bound_sender(
2782 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2783 id_to_validate, notifications)) {
2784 dlog_verbose("Notifications are bound to other sender.\n");
2785 ret = ffa_error(FFA_DENIED);
2786 goto out;
2787 }
2788
2789 /**
2790 * Check if there is a pending notification within those specified in
2791 * the bitmap.
2792 */
2793 if (vm_are_notifications_pending(receiver_locked,
2794 plat_ffa_is_vm_id(sender_vm_id),
2795 notifications)) {
2796 dlog_verbose("Notifications within '%x' pending.\n",
2797 notifications);
2798 ret = ffa_error(FFA_DENIED);
2799 goto out;
2800 }
2801
2802 vm_notifications_update_bindings(
2803 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
2804 notifications, is_per_vcpu && is_bind);
2805
2806out:
2807 vm_unlock(&receiver_locked);
2808 return ret;
2809}
J-Alvesaa79c012021-07-09 14:29:45 +01002810
2811struct ffa_value api_ffa_notification_set(
2812 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2813 ffa_notifications_bitmap_t notifications, struct vcpu *current)
2814{
2815 struct ffa_value ret;
2816 struct vm_locked receiver_locked;
2817
2818 /*
2819 * Check if is per-vCPU or global, and extracting vCPU ID according
2820 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
2821 */
2822 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2823 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
2824
J-Alvesaa79c012021-07-09 14:29:45 +01002825 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
2826 receiver_vm_id)) {
2827 dlog_verbose("Invalid use of notifications set interface.\n");
2828 return ffa_error(FFA_INVALID_PARAMETERS);
2829 }
2830
2831 if (notifications == 0U) {
2832 dlog_verbose("No notifications have been specified.\n");
2833 return ffa_error(FFA_INVALID_PARAMETERS);
2834 }
2835
J-Alvesde7bd2f2021-09-09 19:54:35 +01002836 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
2837 flags, notifications, &ret)) {
2838 return ret;
2839 }
2840
J-Alvesaa79c012021-07-09 14:29:45 +01002841 /*
2842 * This check assumes receiver is the current VM, and has been enforced
2843 * by 'plat_ffa_is_notification_set_valid'.
2844 */
2845 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2846
2847 if (receiver_locked.vm == NULL) {
2848 dlog_verbose("Receiver ID is not valid.\n");
2849 return ffa_error(FFA_INVALID_PARAMETERS);
2850 }
2851
J-Alves09ff9d82021-11-02 11:55:20 +00002852 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01002853 dlog_verbose("Receiver's notifications not enabled.\n");
2854 ret = ffa_error(FFA_DENIED);
2855 goto out;
2856 }
2857
2858 /*
2859 * If notifications are not bound to the sender, they wouldn't be
2860 * enabled either for the receiver.
2861 */
2862 if (!vm_notifications_validate_binding(
2863 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2864 sender_vm_id, notifications, is_per_vcpu)) {
2865 dlog_verbose("Notifications bindings not valid.\n");
2866 ret = ffa_error(FFA_DENIED);
2867 goto out;
2868 }
2869
2870 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
2871 dlog_verbose("Invalid VCPU ID!\n");
2872 ret = ffa_error(FFA_INVALID_PARAMETERS);
2873 goto out;
2874 }
2875
J-Alves7461ef22021-10-18 17:21:33 +01002876 /* Set notifications pending. */
J-Alvesaa79c012021-07-09 14:29:45 +01002877 vm_notifications_set(receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2878 notifications, vcpu_id, is_per_vcpu);
2879 dlog_verbose("Set the notifications: %x.\n", notifications);
2880
J-Alves13394022021-06-30 13:48:49 +01002881 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2882 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2883 vcpu_index(current));
2884 plat_ffa_sri_trigger_not_delayed(current->cpu);
2885 } else {
2886 plat_ffa_sri_state_set(DELAYED);
2887 }
J-Alvesaa79c012021-07-09 14:29:45 +01002888
J-Alves7461ef22021-10-18 17:21:33 +01002889 /*
2890 * If notifications set are per-vCPU and the receiver is SP, the
2891 * Notifications Pending Interrupt can be injected now.
2892 * If not, it should be injected when the scheduler gives it CPU cycles
2893 * in a specific vCPU.
2894 */
2895 if (is_per_vcpu && vm_id_is_current_world(receiver_vm_id)) {
2896 struct vcpu *target_vcpu =
2897 vm_get_vcpu(receiver_locked.vm, vcpu_id);
2898
2899 dlog_verbose("Per-vCPU notification, pending NPI.\n");
J-Alves6f72ca82021-11-01 12:34:58 +00002900 internal_interrupt_inject(target_vcpu,
2901 HF_NOTIFICATION_PENDING_INTID,
2902 current, NULL);
J-Alves7461ef22021-10-18 17:21:33 +01002903 }
2904
J-Alves13394022021-06-30 13:48:49 +01002905 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01002906out:
2907 vm_unlock(&receiver_locked);
2908
2909 return ret;
2910}
2911
2912static struct ffa_value api_ffa_notification_get_success_return(
2913 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
2914 ffa_notifications_bitmap_t from_framework)
2915{
2916 return (struct ffa_value){
2917 .func = FFA_SUCCESS_32,
2918 .arg1 = 0U,
2919 .arg2 = (uint32_t)from_sp,
2920 .arg3 = (uint32_t)(from_sp >> 32),
2921 .arg4 = (uint32_t)from_vm,
2922 .arg5 = (uint32_t)(from_vm >> 32),
2923 .arg6 = (uint32_t)from_framework,
2924 .arg7 = (uint32_t)(from_framework >> 32),
2925 };
2926}
2927
2928struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
2929 ffa_vcpu_index_t vcpu_id,
2930 uint32_t flags, struct vcpu *current)
2931{
2932 /* TODO: get framework notifications, when these are supported. */
2933 ffa_notifications_bitmap_t sp_notifications = 0;
2934 ffa_notifications_bitmap_t vm_notifications = 0;
2935 struct vm_locked receiver_locked;
2936 struct ffa_value ret;
2937
2938 /*
2939 * Following check should capture wrong uses of the interface, depending
2940 * on whether Hafnium is SPMC or hypervisor.
2941 * On the rest of the function it is assumed this condition is met.
2942 */
2943 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id)) {
2944 dlog_verbose("Invalid use of notifications get interface.\n");
2945 return ffa_error(FFA_INVALID_PARAMETERS);
2946 }
2947
2948 /*
2949 * This check assumes receiver is the current VM, and has been enforced
2950 * by `plat_ffa_is_notifications_get_valid`.
2951 */
2952 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2953
2954 /*
2955 * `plat_ffa_is_notifications_get_valid` ensures following is never
2956 * true.
2957 */
2958 CHECK(receiver_locked.vm != NULL);
2959
2960 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
2961 (receiver_locked.vm->vcpu_count != 1 &&
2962 cpu_index(current->cpu) != vcpu_id)) {
2963 dlog_verbose("Invalid VCPU ID!\n");
2964 ret = ffa_error(FFA_INVALID_PARAMETERS);
2965 goto out;
2966 }
2967
2968 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01002969 if (!plat_ffa_notifications_get_from_sp(
2970 receiver_locked, vcpu_id, &sp_notifications,
2971 &ret)) {
2972 dlog_verbose("Failed to get notifications from sps.");
2973 goto out;
2974 }
J-Alvesaa79c012021-07-09 14:29:45 +01002975 }
2976
2977 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
2978 vm_notifications = vm_notifications_get_pending_and_clear(
2979 receiver_locked, true, vcpu_id);
2980 }
2981
2982 ret = api_ffa_notification_get_success_return(sp_notifications,
2983 vm_notifications, 0);
2984
J-Alvesfe23ebe2021-10-13 16:07:07 +01002985 /*
2986 * If there are no more pending notifications, change `sri_state` to
2987 * handled.
2988 */
2989 if (vm_is_notifications_pending_count_zero()) {
2990 plat_ffa_sri_state_set(HANDLED);
2991 }
2992
J-Alvesaa79c012021-07-09 14:29:45 +01002993out:
2994 vm_unlock(&receiver_locked);
2995
2996 return ret;
2997}
J-Alvesc8e8a222021-06-08 17:33:52 +01002998
2999/**
3000 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3001 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3002 */
3003static struct ffa_value api_ffa_notification_info_get_success_return(
3004 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003005 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003006{
3007 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3008
3009 /*
3010 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3011 * hold the list of ids.
3012 */
3013 memcpy_s(&ret.arg3,
3014 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3015 sizeof(ids[0]) * ids_count);
3016
3017 /*
3018 * According to the spec x2 should have:
3019 * - Bit flagging if there are more notifications pending;
3020 * - The total number of elements (i.e. total list size);
3021 * - The number of VCPU IDs within each VM specific list.
3022 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003023 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3024 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3025 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003026
3027 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3028 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3029
3030 for (unsigned int i = 0; i < lists_count; i++) {
3031 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3032 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3033 }
3034
3035 return ret;
3036}
3037
3038struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3039{
3040 /*
3041 * Following set of variables should be populated with the return info.
3042 * At a successfull handling of this interface, they should be used
3043 * to populate the 'ret' structure in accordance to the table 17.29
3044 * of the FF-A v1.1 Beta0 specification.
3045 */
3046 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3047 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3048 uint32_t lists_count = 0;
3049 uint32_t ids_count = 0;
3050 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003051 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003052
3053 /*
3054 * This interface can only be called at NS virtual/physical FF-A
3055 * instance by the endpoint implementing the primary scheduler and the
3056 * Hypervisor/OS kernel.
3057 * In the SPM, following check passes if call has been forwarded from
3058 * the hypervisor.
3059 */
3060 if (current->vm->id != HF_PRIMARY_VM_ID) {
3061 dlog_verbose(
3062 "Only the receiver's scheduler can use this "
3063 "interface\n");
3064 return ffa_error(FFA_NOT_SUPPORTED);
3065 }
3066
J-Alvesca058c22021-09-10 14:02:07 +01003067 /*
3068 * Forward call to the other world, and fill the arrays used to assemble
3069 * return.
3070 */
3071 plat_ffa_notification_info_get_forward(
3072 ids, &ids_count, lists_sizes, &lists_count,
3073 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3074
3075 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3076
J-Alvesc8e8a222021-06-08 17:33:52 +01003077 /* Get notifications' info from this world */
3078 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3079 ++index) {
3080 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3081
3082 list_is_full = vm_notifications_info_get(
3083 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3084 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3085
3086 vm_unlock(&vm_locked);
3087 }
3088
3089 if (!list_is_full) {
3090 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003091 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003092 ids, &ids_count, lists_sizes, &lists_count,
3093 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3094 }
3095
3096 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003097 dlog_verbose(
3098 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003099 result = ffa_error(FFA_NO_DATA);
3100 } else {
3101 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003102 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003103 }
3104
J-Alvesfe23ebe2021-10-13 16:07:07 +01003105 plat_ffa_sri_state_set(HANDLED);
3106
J-Alves13394022021-06-30 13:48:49 +01003107 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003108}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003109
3110struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3111{
3112 struct vm_locked vm_locked;
3113 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3114 bool mode_ret = false;
3115 uint32_t mode = 0;
3116
3117 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3118 return ffa_error(FFA_NOT_SUPPORTED);
3119 }
3120
3121 if (!(current->vm->el0_partition)) {
3122 return ffa_error(FFA_DENIED);
3123 }
3124
3125 vm_locked = vm_lock(current->vm);
3126
3127 /*
3128 * mm_get_mode is used to check if the given base_addr page is already
3129 * mapped. If the page is unmapped, return error. If the page is mapped
3130 * appropriate attributes are returned to the caller. Note that
3131 * mm_get_mode returns true if the address is in the valid VA range as
3132 * supported by the architecture and MMU configurations, as opposed to
3133 * whether a page is mapped or not. For a page to be known as mapped,
3134 * the API must return true AND the returned mode must not have
3135 * MM_MODE_INVALID set.
3136 */
3137 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3138 va_add(base_addr, PAGE_SIZE), &mode);
3139 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3140 ret = ffa_error(FFA_INVALID_PARAMETERS);
3141 goto out;
3142 }
3143
3144 /* No memory should be marked RWX */
3145 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3146 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3147
3148 /*
3149 * S-EL0 partitions are expected to have all their pages marked as
3150 * non-global.
3151 */
3152 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3153 (MM_MODE_NG | MM_MODE_USER));
3154
3155 if (mode & MM_MODE_W) {
3156 /* No memory should be writeable but not readable. */
3157 CHECK(mode & MM_MODE_R);
3158 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3159 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3160 } else if (mode & MM_MODE_R) {
3161 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3162 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3163 if (!(mode & MM_MODE_X)) {
3164 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3165 }
3166 }
3167out:
3168 vm_unlock(&vm_locked);
3169 return ret;
3170}
3171
3172struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3173 uint32_t mem_perm, struct vcpu *current)
3174{
3175 struct vm_locked vm_locked;
3176 struct ffa_value ret;
3177 bool mode_ret = false;
3178 uint32_t original_mode;
3179 uint32_t new_mode;
3180 struct mpool local_page_pool;
3181
3182 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3183 return ffa_error(FFA_NOT_SUPPORTED);
3184 }
3185
3186 if (!(current->vm->el0_partition)) {
3187 return ffa_error(FFA_DENIED);
3188 }
3189
3190 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3191 return ffa_error(FFA_INVALID_PARAMETERS);
3192 }
3193
3194 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3195 (mem_perm != FFA_MEM_PERM_RX)) {
3196 return ffa_error(FFA_INVALID_PARAMETERS);
3197 }
3198
3199 /*
3200 * Create a local pool so any freed memory can't be used by another
3201 * thread. This is to ensure the original mapping can be restored if any
3202 * stage of the process fails.
3203 */
3204 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3205
3206 vm_locked = vm_lock(current->vm);
3207
3208 /*
3209 * All regions accessible by the partition are mapped during boot. If we
3210 * cannot get a successful translation for the page range, the request
3211 * to change permissions is rejected.
3212 * mm_get_mode is used to check if the given address range is already
3213 * mapped. If the range is unmapped, return error. If the range is
3214 * mapped appropriate attributes are returned to the caller. Note that
3215 * mm_get_mode returns true if the address is in the valid VA range as
3216 * supported by the architecture and MMU configurations, as opposed to
3217 * whether a page is mapped or not. For a page to be known as mapped,
3218 * the API must return true AND the returned mode must not have
3219 * MM_MODE_INVALID set.
3220 */
3221
3222 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3223 va_add(base_addr, page_count * PAGE_SIZE),
3224 &original_mode);
3225 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3226 ret = ffa_error(FFA_INVALID_PARAMETERS);
3227 goto out;
3228 }
3229
3230 /* Device memory cannot be marked as executable */
3231 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3232 ret = ffa_error(FFA_INVALID_PARAMETERS);
3233 goto out;
3234 }
3235
3236 new_mode = MM_MODE_USER | MM_MODE_NG;
3237
3238 if (mem_perm == FFA_MEM_PERM_RW) {
3239 new_mode |= MM_MODE_R | MM_MODE_W;
3240 } else if (mem_perm == FFA_MEM_PERM_RX) {
3241 new_mode |= MM_MODE_R | MM_MODE_X;
3242 } else if (mem_perm == FFA_MEM_PERM_RO) {
3243 new_mode |= MM_MODE_R;
3244 }
3245
3246 /*
3247 * Safe to re-map memory, since we know the requested permissions are
3248 * valid, and the memory requested to be re-mapped is also valid.
3249 */
3250 if (!mm_identity_prepare(
3251 &vm_locked.vm->ptable, pa_from_va(base_addr),
3252 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3253 new_mode, &local_page_pool)) {
3254 /*
3255 * Defrag the table into the local page pool.
3256 * mm_identity_prepare could have allocated or freed pages to
3257 * split blocks or tables etc.
3258 */
3259 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3260
3261 /*
3262 * Guaranteed to succeed mapping with old mode since the mapping
3263 * with old mode already existed and we have a local page pool
3264 * that should have sufficient memory to go back to the original
3265 * state.
3266 */
3267 CHECK(mm_identity_prepare(
3268 &vm_locked.vm->ptable, pa_from_va(base_addr),
3269 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3270 original_mode, &local_page_pool));
3271 mm_identity_commit(
3272 &vm_locked.vm->ptable, pa_from_va(base_addr),
3273 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3274 original_mode, &local_page_pool);
3275
3276 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3277 ret = ffa_error(FFA_NO_MEMORY);
3278 goto out;
3279 }
3280
3281 mm_identity_commit(
3282 &vm_locked.vm->ptable, pa_from_va(base_addr),
3283 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3284 &local_page_pool);
3285
3286 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3287
3288out:
3289 mpool_fini(&local_page_pool);
3290 vm_unlock(&vm_locked);
3291
3292 return ret;
3293}