blob: de2bf5b6bc4c212bde4a73b6c1595fff4cc28e6b [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/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200467 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
468 * FF-A instances.
469 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000470 */
471struct ffa_value api_ffa_spm_id_get(void)
472{
J-Alves3829fc02021-03-18 12:49:18 +0000473#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
474 /*
475 * Return the SPMC ID that was fetched during FF-A
476 * initialization.
477 */
478 return (struct ffa_value){.func = FFA_SUCCESS_32,
479 .arg2 = arch_ffa_spmc_id_get()};
480#else
481 return ffa_error(FFA_NOT_SUPPORTED);
482#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000483}
484
485/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000486 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000487 * function to indicate that register state for the given vCPU has been saved
488 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000489 */
490void api_regs_state_saved(struct vcpu *vcpu)
491{
492 sl_lock(&vcpu->lock);
493 vcpu->regs_available = true;
494 sl_unlock(&vcpu->lock);
495}
496
497/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000498 * Retrieves the next waiter and removes it from the wait list if the VM's
499 * mailbox is in a writable state.
500 */
501static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
502{
503 struct wait_entry *entry;
504 struct vm *vm = locked_vm.vm;
505
Andrew Sculld6ee1102019-04-05 22:12:42 +0100506 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000507 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
508 /* The mailbox is not writable or there are no waiters. */
509 return NULL;
510 }
511
512 /* Remove waiter from the wait list. */
513 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
514 wait_links);
515 list_remove(&entry->wait_links);
516 return entry;
517}
518
519/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000520 * Assuming that the arguments have already been checked by the caller, injects
521 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
522 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
523 * is next run, which is up to the scheduler.
524 *
525 * Returns:
526 * - 0 on success if no further action is needed.
527 * - 1 if it was called by the primary VM and the primary VM now needs to wake
528 * up or kick the target vCPU.
529 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100530int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
531 uint32_t intid, struct vcpu *current,
532 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000533{
Manish Pandey35e452f2021-02-18 21:36:34 +0000534 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000535 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000536 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
537 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000538 int64_t ret = 0;
539
Andrew Walbran508e63c2018-12-20 17:02:37 +0000540 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000541 * We only need to change state and (maybe) trigger a virtual interrupt
542 * if it is enabled and was not previously pending. Otherwise we can
543 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000544 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000545 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
546 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000547 intid_mask)) {
548 goto out;
549 }
550
551 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000552 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
553 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
554 vcpu_irq_count_increment(target_locked);
555 } else {
556 vcpu_fiq_count_increment(target_locked);
557 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000558
559 /*
560 * Only need to update state if there was not already an
561 * interrupt enabled and pending.
562 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000563 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000564 goto out;
565 }
566
Andrew Walbran508e63c2018-12-20 17:02:37 +0000567 if (current->vm->id == HF_PRIMARY_VM_ID) {
568 /*
569 * If the call came from the primary VM, let it know that it
570 * should run or kick the target vCPU.
571 */
572 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000573 } else if (current != target_vcpu && next != NULL) {
574 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000575 }
576
577out:
578 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000579 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000580
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200581 return ret;
582}
583
584/* Wrapper to internal_interrupt_inject with locking of target vCPU */
585static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
586 uint32_t intid, struct vcpu *current,
587 struct vcpu **next)
588{
589 int64_t ret;
590 struct vcpu_locked target_locked;
591
592 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100593 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200594 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000595
596 return ret;
597}
598
599/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100600 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
601 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100602 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100603static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100604{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000605 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100606 case FFA_MSG_SEND_32:
607 return (struct ffa_value){
608 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000609 .arg1 = (receiver->mailbox.recv_sender << 16) |
610 receiver->id,
611 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000612 default:
613 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000614 dlog_error("Tried to return an invalid message function %#x\n",
615 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100616 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000617 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100618}
619
620/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000621 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000622 * value needs to be forced onto the vCPU.
623 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000624static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100625 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000626{
Max Shvetsov40108e72020-08-27 12:39:50 +0100627 struct vcpu_locked vcpu_locked;
628 struct vm_locked vm_locked;
Andrew Scullb06d1752019-02-04 10:15:48 +0000629 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000630 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500631 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000632
Andrew Scullb06d1752019-02-04 10:15:48 +0000633 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000634 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000635 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100636 * The VM lock is not needed in the common case so it must only be taken
637 * when it is going to be needed. This ensures there are no inter-vCPU
638 * dependencies in the common run case meaning the sensitive context
639 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000640 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100641 vcpu_locked = vcpu_lock(vcpu);
642
643#if SECURE_WORLD == 1
644
645 if (vcpu_secondary_reset_and_start(vcpu_locked, vcpu->vm->secondary_ep,
646 0)) {
647 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
648 __func__, vcpu->vm->id, current->cpu->id);
649 }
650
651#endif
Andrew Scullb06d1752019-02-04 10:15:48 +0000652
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000653 /* The VM needs to be locked to deliver mailbox messages. */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500654 need_vm_lock = vcpu->state == VCPU_STATE_WAITING;
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000655 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100656 vcpu_unlock(&vcpu_locked);
657 vm_locked = vm_lock(vcpu->vm);
658 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000659 }
660
661 /*
662 * If the vCPU is already running somewhere then we can't run it here
663 * simultaneously. While it is actually running then the state should be
664 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
665 * stops running but while Hafnium is in the process of switching back
666 * to the primary there will be a brief period while the state has been
667 * updated but `regs_available` is still false (until
668 * `api_regs_state_saved` is called). We can't start running it again
669 * until this has finished, so count this state as still running for the
670 * purposes of this check.
671 */
672 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
673 /*
674 * vCPU is running on another pCPU.
675 *
676 * It's okay not to return the sleep duration here because the
677 * other physical CPU that is currently running this vCPU will
678 * return the sleep duration if needed.
679 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100680 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000681 ret = false;
682 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000683 }
Andrew Scull9726c252019-01-23 13:44:19 +0000684
685 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100686 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100687 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000688 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100689 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000690 }
691 ret = false;
692 goto out;
693 }
694
Andrew Walbran508e63c2018-12-20 17:02:37 +0000695 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100696 case VCPU_STATE_RUNNING:
697 case VCPU_STATE_OFF:
698 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000699 ret = false;
700 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000701
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500702 case VCPU_STATE_WAITING:
703 /*
704 * An initial FFA_RUN is necessary for secondary VM/SP to reach
705 * the message wait loop.
706 */
707 if (!vcpu->is_bootstrapped) {
708 vcpu->is_bootstrapped = true;
709 break;
710 }
711
Andrew Scullb06d1752019-02-04 10:15:48 +0000712 /*
713 * A pending message allows the vCPU to run so the message can
714 * be delivered directly.
715 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100716 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100717 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100718 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100719 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000720 break;
721 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500722
723 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
724 break;
725 }
726
727 if (arch_timer_enabled(&vcpu->regs)) {
728 timer_remaining_ns =
729 arch_timer_remaining_ns(&vcpu->regs);
730 if (timer_remaining_ns == 0) {
731 break;
732 }
733 } else {
734 dlog_verbose("Timer disabled\n");
735 }
736 run_ret->func = FFA_MSG_WAIT_32;
737 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
738 run_ret->arg2 = timer_remaining_ns;
739 ret = false;
740 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100741 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000742 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000743 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000744 break;
745 }
746
Andrew Walbran508e63c2018-12-20 17:02:37 +0000747 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100748 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000749 arch_timer_remaining_ns(&vcpu->regs);
750
751 /*
752 * The timer expired so allow the interrupt to be
753 * delivered.
754 */
755 if (timer_remaining_ns == 0) {
756 break;
757 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100758 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000759
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100760 /*
761 * The vCPU is not ready to run, return the appropriate code to
762 * the primary which called vcpu_run.
763 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500764 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100765 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
766 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000767
768 ret = false;
769 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000770
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500771 case VCPU_STATE_BLOCKED:
772 /* A blocked vCPU is run unconditionally. Fall through. */
773 case VCPU_STATE_PREEMPTED:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000774 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500775 default:
776 /*
777 * Execution not expected to reach here. Deny the request
778 * gracefully.
779 */
780 *run_ret = ffa_error(FFA_DENIED);
781 ret = false;
782 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000783 }
784
Andrew Scullb06d1752019-02-04 10:15:48 +0000785 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000786 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100787 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000788
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000789 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000790 * Mark the registers as unavailable now that we're about to reflect
791 * them onto the real registers. This will also prevent another physical
792 * CPU from trying to read these registers.
793 */
794 vcpu->regs_available = false;
795
796 ret = true;
797
798out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100799 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000800 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100801 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000802 }
803
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000804 return ret;
805}
806
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100807struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500808 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100809{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100810 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100811 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100812 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100813
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500814 if (!plat_ffa_run_checks(current, vm_id, &ret, next)) {
815 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100816 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100817
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700818 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
819 return ret;
820 }
821
Andrew Scull19503262018-09-20 14:48:39 +0100822 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100823 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100824 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100825 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100826 }
827
Fuad Tabbaed294af2019-12-20 10:43:01 +0000828 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100829 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100830 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100831 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100832
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000833 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100834 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000835 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000836 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100837 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000838
Andrew Walbran508e63c2018-12-20 17:02:37 +0000839 /*
840 * Inject timer interrupt if timer has expired. It's safe to access
841 * vcpu->regs here because api_vcpu_prepare_run already made sure that
842 * regs_available was true (and then set it to false) before returning
843 * true.
844 */
845 if (arch_timer_pending(&vcpu->regs)) {
846 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100847 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
848 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000849
850 /*
851 * Set the mask bit so the hardware interrupt doesn't fire
852 * again. Ideally we wouldn't do this because it affects what
853 * the secondary vCPU sees, but if we don't then we end up with
854 * a loop of the interrupt firing each time we try to return to
855 * the secondary vCPU.
856 */
857 arch_timer_mask(&vcpu->regs);
858 }
859
Fuad Tabbaed294af2019-12-20 10:43:01 +0000860 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000861 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000862
Andrew Scull33fecd32019-01-08 14:48:27 +0000863 /*
864 * Set a placeholder return code to the scheduler. This will be
865 * overwritten when the switch back to the primary occurs.
866 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100867 ret.func = FFA_INTERRUPT_32;
868 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100869 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000870
Andrew Scull6d2db332018-10-10 15:28:17 +0100871out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100872 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100873}
874
875/**
Andrew Scull81e85092018-12-12 12:56:20 +0000876 * Check that the mode indicates memory that is valid, owned and exclusive.
877 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100878static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000879{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100880 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
881 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000882}
883
884/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100885 * Determines the value to be returned by api_ffa_rxtx_map and
886 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
887 * there are waiters, it also switches back to the primary VM for it to wake
888 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000889 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100890static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
891 struct vcpu *current,
892 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000893{
894 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000895
896 if (list_empty(&vm->mailbox.waiter_list)) {
897 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000899 }
900
901 if (vm->id == HF_PRIMARY_VM_ID) {
902 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100903 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000904 }
905
906 /*
907 * Switch back to the primary VM, informing it that there are waiters
908 * that need to be notified.
909 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000910 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100911 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500912 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000913
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100914 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000915}
916
917/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100918 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100919 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100920static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
921 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100922 paddr_t pa_send_begin, paddr_t pa_send_end,
923 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200924 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100925 struct mpool *local_page_pool)
926{
927 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100928
929 /* Map the send page as read-only in the hypervisor address space. */
930 vm_locked.vm->mailbox.send =
931 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200932 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100933 if (!vm_locked.vm->mailbox.send) {
934 /* TODO: partial defrag of failed range. */
935 /* Recover any memory consumed in failed mapping. */
936 mm_defrag(mm_stage1_locked, local_page_pool);
937 goto fail;
938 }
939
940 /*
941 * Map the receive page as writable in the hypervisor address space. On
942 * failure, unmap the send page before returning.
943 */
944 vm_locked.vm->mailbox.recv =
945 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200946 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100947 if (!vm_locked.vm->mailbox.recv) {
948 /* TODO: partial defrag of failed range. */
949 /* Recover any memory consumed in failed mapping. */
950 mm_defrag(mm_stage1_locked, local_page_pool);
951 goto fail_undo_send;
952 }
953
954 ret = true;
955 goto out;
956
957 /*
958 * The following mappings will not require more memory than is available
959 * in the local pool.
960 */
961fail_undo_send:
962 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100963 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
964 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100965
966fail:
967 ret = false;
968
969out:
Andrew Sculle1322792019-07-01 17:46:10 +0100970 return ret;
971}
972
973/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100974 * Sanity checks and configures the send and receive pages in the VM stage-2
975 * and hypervisor stage-1 page tables.
976 *
977 * Returns:
978 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100979 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +0100980 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
981 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100982 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +0100983 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100984 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100985
986struct ffa_value api_vm_configure_pages(
987 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
988 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
989 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100990{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100991 struct ffa_value ret;
992 paddr_t pa_send_begin;
993 paddr_t pa_send_end;
994 paddr_t pa_recv_begin;
995 paddr_t pa_recv_end;
996 uint32_t orig_send_mode;
997 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200998 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100999
1000 /* We only allow these to be setup once. */
1001 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1002 ret = ffa_error(FFA_DENIED);
1003 goto out;
1004 }
1005
1006 /* Hafnium only supports a fixed size of RX/TX buffers. */
1007 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1008 ret = ffa_error(FFA_INVALID_PARAMETERS);
1009 goto out;
1010 }
1011
1012 /* Fail if addresses are not page-aligned. */
1013 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1014 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1015 ret = ffa_error(FFA_INVALID_PARAMETERS);
1016 goto out;
1017 }
1018
1019 /* Convert to physical addresses. */
1020 pa_send_begin = pa_from_ipa(send);
1021 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1022 pa_recv_begin = pa_from_ipa(recv);
1023 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1024
1025 /* Fail if the same page is used for the send and receive pages. */
1026 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1027 ret = ffa_error(FFA_INVALID_PARAMETERS);
1028 goto out;
1029 }
Andrew Sculle1322792019-07-01 17:46:10 +01001030
1031 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001032 * Ensure the pages are valid, owned and exclusive to the VM and that
1033 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +01001034 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001035 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1036 &orig_send_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001037 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1038 (orig_send_mode & MM_MODE_R) == 0 ||
1039 (orig_send_mode & MM_MODE_W) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001040 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001041 goto out;
1042 }
1043
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001044 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1045 &orig_recv_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001046 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1047 (orig_recv_mode & MM_MODE_R) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001048 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001049 goto out;
1050 }
Andrew Sculle1322792019-07-01 17:46:10 +01001051
1052 /* Take memory ownership away from the VM and mark as shared. */
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001053 uint32_t mode =
1054 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W;
1055 if (vm_locked.vm->el0_partition) {
1056 mode |= MM_MODE_USER | MM_MODE_NG;
1057 }
1058
1059 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, mode,
1060 local_page_pool, NULL)) {
Manish Pandeyd34f8892020-06-19 17:41:07 +01001061 ret = ffa_error(FFA_NO_MEMORY);
1062 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001063 }
1064
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001065 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1066 if (vm_locked.vm->el0_partition) {
1067 mode |= MM_MODE_USER | MM_MODE_NG;
1068 }
1069
1070 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, mode,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001071 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001072 /* TODO: partial defrag of failed range. */
1073 /* Recover any memory consumed in failed mapping. */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001074 vm_ptable_defrag(vm_locked, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001075 goto fail_undo_send;
1076 }
1077
Olivier Deprez96a2a262020-06-11 17:21:38 +02001078 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1079 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1080
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001081 /*
1082 * For EL0 partitions, since both the partition and the hypervisor code
1083 * use the EL2&0 translation regime, it is critical to mark the mappings
1084 * of the send and recv buffers as non-global in the TLB. For one, if we
1085 * dont mark it as non-global, it would cause TLB conflicts since there
1086 * would be an identity mapping with non-global attribute in the
1087 * partitions page tables, but another identity mapping in the
1088 * hypervisor page tables with the global attribute. The other issue is
1089 * one of security, we dont want other partitions to be able to access
1090 * other partitions buffers through cached translations.
1091 */
1092 if (vm_locked.vm->el0_partition) {
1093 extra_attributes |= MM_MODE_NG;
1094 }
1095
Manish Pandeyd34f8892020-06-19 17:41:07 +01001096 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1097 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001098 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001099 goto fail_undo_send_and_recv;
1100 }
1101
Manish Pandeyd34f8892020-06-19 17:41:07 +01001102 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001103 goto out;
1104
Andrew Sculle1322792019-07-01 17:46:10 +01001105fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001106 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001107 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001108
1109fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001110 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001111 orig_send_mode, local_page_pool, NULL));
1112 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001113
1114out:
Andrew Sculle1322792019-07-01 17:46:10 +01001115 return ret;
1116}
1117
1118/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001119 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001120 * must not be shared. Locking of the page tables combined with a local memory
1121 * pool ensures there will always be enough memory to recover from any errors
1122 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1123 * by another core which could result in this transaction being unable to roll
1124 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001125 *
1126 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001127 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001128 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001129 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001130 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001131 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001132 * - FFA_SUCCESS on success if no further action is needed.
1133 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001134 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001135 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001136struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
1137 uint32_t page_count, struct vcpu *current,
1138 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001139{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001140 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001141 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001142 struct vm_locked vm_locked;
1143 struct mm_stage1_locked mm_stage1_locked;
1144 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001145
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001146 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001147 * Create a local pool so any freed memory can't be used by another
1148 * thread. This is to ensure the original mapping can be restored if any
1149 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001150 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001151 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1152
Andrew Sculle1322792019-07-01 17:46:10 +01001153 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001154 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001155
Manish Pandeyd34f8892020-06-19 17:41:07 +01001156 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1157 page_count, &local_page_pool);
1158 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001159 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001160 }
1161
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001162 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001163 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001164
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001165exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001166 mpool_fini(&local_page_pool);
1167
1168 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001169 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001170
1171 return ret;
1172}
1173
1174/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001175 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1176 * translation regime of the caller. Unmap the region for the hypervisor and
1177 * set the memory region to owned and exclusive for the component. Since the
1178 * memory region mapped in the page table, when the buffers were originally
1179 * created we can safely remap it.
1180 *
1181 * Returns:
1182 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1183 * behalf of the caller.
1184 * - FFA_SUCCESS on success if no further action is needed.
1185 */
1186struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1187 struct vcpu *current)
1188{
1189 struct vm *vm = current->vm;
1190 struct vm_locked vm_locked;
1191 struct mm_stage1_locked mm_stage1_locked;
1192 paddr_t send_pa_begin;
1193 paddr_t send_pa_end;
1194 paddr_t recv_pa_begin;
1195 paddr_t recv_pa_end;
1196
1197 /*
1198 * Check there is a buffer pair registered on behalf of the caller.
1199 * Since forwarding is not yet supported the allocator ID MBZ.
1200 */
1201 if (allocator_id != 0) {
1202 dlog_error(
1203 "Forwarding MAP/UNMAP from the hypervisor is not yet "
1204 "supported so vm id must be zero.\n");
1205 return ffa_error(FFA_INVALID_PARAMETERS);
1206 }
1207
1208 /* Get send and receive buffers. */
1209 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001210 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001211 "No buffer pair registered on behalf of the caller.\n");
1212 return ffa_error(FFA_INVALID_PARAMETERS);
1213 }
1214
1215 /* Currently a mailbox size of 1 page is assumed. */
1216 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1217 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1218 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1219 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1220
1221 vm_locked = vm_lock(vm);
1222 mm_stage1_locked = mm_lock_stage1();
1223
1224 /*
1225 * Set the memory region of the buffers back to the default mode
1226 * for the VM. Since this memory region was already mapped for the
1227 * RXTX buffers we can safely remap them.
1228 */
1229 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1230 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1231 NULL));
1232
1233 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1234 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1235 NULL));
1236
1237 /* Unmap the buffers in the partition manager. */
1238 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1239 &api_page_pool));
1240 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1241 &api_page_pool));
1242
1243 vm->mailbox.send = NULL;
1244 vm->mailbox.recv = NULL;
1245
1246 mm_unlock_stage1(&mm_stage1_locked);
1247 vm_unlock(&vm_locked);
1248
1249 return (struct ffa_value){.func = FFA_SUCCESS_32};
1250}
1251
1252/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001253 * Notifies the `to` VM about the message currently in its mailbox, possibly
1254 * with the help of the primary VM.
1255 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001256static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1257 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001258{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001259 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1260 struct ffa_value primary_ret = {
1261 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001262 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001263 };
1264
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001265 /* Messages for the primary VM are delivered directly. */
1266 if (to.vm->id == HF_PRIMARY_VM_ID) {
1267 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001268 * Only tell the primary VM the size and other details if the
1269 * message is for it, to avoid leaking data about messages for
1270 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001271 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001272 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001273
1274 to.vm->mailbox.state = MAILBOX_STATE_READ;
1275 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001276 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001277 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001278 }
1279
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001280 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1281
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001282 /* Messages for the TEE are sent on via the dispatcher. */
1283 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001284 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001285
Olivier Deprez112d2b52020-09-30 07:39:23 +02001286 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001287 /*
1288 * After the call to the TEE completes it must have finished
1289 * reading its RX buffer, so it is ready for another message.
1290 */
1291 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001292 /*
1293 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001294 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001295 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001296 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001297 }
1298
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001299 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001300 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001301 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001302 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001303 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001304
1305 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001306}
1307
1308/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001309 * Copies data from the sender's send buffer to the recipient's receive buffer
1310 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001311 *
1312 * If the recipient's receive buffer is busy, it can optionally register the
1313 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001314 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001315struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1316 ffa_vm_id_t receiver_vm_id, uint32_t size,
1317 uint32_t attributes, struct vcpu *current,
1318 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001319{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001320 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001321 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001322 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001323 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001324 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001325 struct vcpu_locked current_locked;
1326 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001327 bool notify =
1328 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001329
Andrew Walbran70bc8622019-10-07 14:15:58 +01001330 /* Ensure sender VM ID corresponds to the current VM. */
1331 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001332 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001333 }
1334
1335 /* Disallow reflexive requests as this suggests an error in the VM. */
1336 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001337 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001338 }
1339
1340 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001341 if (size > FFA_MSG_PAYLOAD_MAX) {
1342 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001343 }
1344
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001345 /*
1346 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1347 * invocation.
1348 */
1349 current_locked = vcpu_lock(current);
1350 is_direct_request_ongoing =
1351 is_ffa_direct_msg_request_ongoing(current_locked);
1352 vcpu_unlock(&current_locked);
1353
1354 if (is_direct_request_ongoing) {
1355 return ffa_error(FFA_DENIED);
1356 }
1357
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001358 /* Ensure the receiver VM exists. */
1359 to = vm_find(receiver_vm_id);
1360 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001361 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001362 }
1363
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001364 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001365 * Check that the sender has configured its send buffer. If the tx
1366 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1367 * be safely accessed after releasing the lock since the tx mailbox
1368 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001369 */
1370 sl_lock(&from->lock);
1371 from_msg = from->mailbox.send;
1372 sl_unlock(&from->lock);
1373
1374 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001375 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001376 }
1377
Andrew Walbran82d6d152019-12-24 15:02:06 +00001378 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001379
Andrew Walbran82d6d152019-12-24 15:02:06 +00001380 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001381 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001382 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001383 }
1384
Andrew Walbran82d6d152019-12-24 15:02:06 +00001385 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001386 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001387 to->mailbox.recv_size = size;
1388 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001389 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001390 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001391
1392out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001393 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001394
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001395 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001396}
1397
1398/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001399 * Checks whether the vCPU's attempt to block for a message has already been
1400 * interrupted or whether it is allowed to block.
1401 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001402bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001403{
Manish Pandey35e452f2021-02-18 21:36:34 +00001404 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001405 bool interrupted;
1406
Manish Pandey35e452f2021-02-18 21:36:34 +00001407 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001408
1409 /*
1410 * Don't block if there are enabled and pending interrupts, to match
1411 * behaviour of wait_for_interrupt.
1412 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001413 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001414
Manish Pandey35e452f2021-02-18 21:36:34 +00001415 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001416
1417 return interrupted;
1418}
1419
1420/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001421 * Receives a message from the mailbox. If one isn't available, this function
1422 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001423 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001424 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001425 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001426struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1427 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001428{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001429 bool is_direct_request_ongoing;
1430 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001431 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001432 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001433 bool is_from_secure_world =
1434 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001435
Andrew Scullaa039b32018-10-04 15:02:26 +01001436 /*
1437 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001438 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001439 */
J-Alvesb37fd082020-10-22 12:29:21 +01001440 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001441 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001442 }
1443
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001444 /*
1445 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1446 * invocation.
1447 */
1448 current_locked = vcpu_lock(current);
1449 is_direct_request_ongoing =
1450 is_ffa_direct_msg_request_ongoing(current_locked);
1451 vcpu_unlock(&current_locked);
1452
1453 if (is_direct_request_ongoing) {
1454 return ffa_error(FFA_DENIED);
1455 }
1456
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001457 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001458
Andrew Scullaa039b32018-10-04 15:02:26 +01001459 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001460 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1461 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001462 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001463 goto out;
1464 }
1465
1466 /* No pending message so fail if not allowed to block. */
1467 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001468 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001469 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001470 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001471
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001472 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001473 * From this point onward this call can only be interrupted or a message
1474 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001475 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001476 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001477 return_code = ffa_error(FFA_INTERRUPTED);
1478 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001479 goto out;
1480 }
1481
J-Alvesb37fd082020-10-22 12:29:21 +01001482 if (is_from_secure_world) {
1483 /* Return to other world if caller is a SP. */
1484 *next = api_switch_to_other_world(
1485 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001486 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001487 } else {
1488 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001489 struct ffa_value run_return = {
1490 .func = FFA_MSG_WAIT_32,
1491 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001492 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001493
Andrew Walbranb4816552018-12-05 17:35:42 +00001494 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001495 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001496 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001497out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001498 sl_unlock(&vm->lock);
1499
Jose Marinho3e2442f2019-03-12 13:30:37 +00001500 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001501}
1502
1503/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001504 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1505 * by this function, the caller must have called api_mailbox_send before with
1506 * the notify argument set to true, and this call must have failed because the
1507 * mailbox was not available.
1508 *
1509 * It should be called repeatedly to retrieve a list of VMs.
1510 *
1511 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1512 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001513 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001514int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001515{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001516 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001517 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001518 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001519
1520 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001521 if (list_empty(&vm->mailbox.ready_list)) {
1522 ret = -1;
1523 goto exit;
1524 }
1525
1526 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1527 ready_links);
1528 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001529 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001530
1531exit:
1532 sl_unlock(&vm->lock);
1533 return ret;
1534}
1535
1536/**
1537 * Retrieves the next VM waiting to be notified that the mailbox of the
1538 * specified VM became writable. Only primary VMs are allowed to call this.
1539 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001540 * Returns -1 on failure or if there are no waiters; the VM id of the next
1541 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001542 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001543int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001544{
1545 struct vm *vm;
1546 struct vm_locked locked;
1547 struct wait_entry *entry;
1548 struct vm *waiting_vm;
1549
1550 /* Only primary VMs are allowed to call this function. */
1551 if (current->vm->id != HF_PRIMARY_VM_ID) {
1552 return -1;
1553 }
1554
Andrew Walbran42347a92019-05-09 13:59:03 +01001555 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001556 if (vm == NULL) {
1557 return -1;
1558 }
1559
Fuad Tabbaed294af2019-12-20 10:43:01 +00001560 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001561 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001562 entry = api_fetch_waiter(locked);
1563 vm_unlock(&locked);
1564
1565 if (entry == NULL) {
1566 return -1;
1567 }
1568
1569 /* Enqueue notification to waiting VM. */
1570 waiting_vm = entry->waiting_vm;
1571
1572 sl_lock(&waiting_vm->lock);
1573 if (list_empty(&entry->ready_links)) {
1574 list_append(&waiting_vm->mailbox.ready_list,
1575 &entry->ready_links);
1576 }
1577 sl_unlock(&waiting_vm->lock);
1578
1579 return waiting_vm->id;
1580}
1581
1582/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001583 * Releases the caller's mailbox so that a new message can be received. The
1584 * caller must have copied out all data they wish to preserve as new messages
1585 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001586 *
1587 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001588 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1589 * - FFA_SUCCESS on success if no further action is needed.
1590 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001591 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001592 * hf_mailbox_waiter_get.
1593 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001594struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001595{
1596 struct vm *vm = current->vm;
1597 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001598 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001599
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001600 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001601 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001602 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001603 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001604 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001605 break;
1606
Andrew Sculld6ee1102019-04-05 22:12:42 +01001607 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001608 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001609 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001610 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001611 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001612 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001613
1614 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001615}
Andrew Walbran318f5732018-11-20 16:23:42 +00001616
1617/**
1618 * Enables or disables a given interrupt ID for the calling vCPU.
1619 *
1620 * Returns 0 on success, or -1 if the intid is invalid.
1621 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001622int64_t api_interrupt_enable(uint32_t intid, bool enable,
1623 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001624{
Manish Pandey35e452f2021-02-18 21:36:34 +00001625 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001626 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001627 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1628 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001629
Andrew Walbran318f5732018-11-20 16:23:42 +00001630 if (intid >= HF_NUM_INTIDS) {
1631 return -1;
1632 }
1633
Manish Pandey35e452f2021-02-18 21:36:34 +00001634 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001635 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001636 /*
1637 * If it is pending and was not enabled before, increment the
1638 * count.
1639 */
1640 if (current->interrupts.interrupt_pending[intid_index] &
1641 ~current->interrupts.interrupt_enabled[intid_index] &
1642 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001643 if ((current->interrupts.interrupt_type[intid_index] &
1644 intid_mask) ==
1645 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1646 vcpu_irq_count_increment(current_locked);
1647 } else {
1648 vcpu_fiq_count_increment(current_locked);
1649 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001650 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001651 current->interrupts.interrupt_enabled[intid_index] |=
1652 intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001653
1654 if (type == INTERRUPT_TYPE_IRQ) {
1655 current->interrupts.interrupt_type[intid_index] &=
1656 ~intid_mask;
1657 } else if (type == INTERRUPT_TYPE_FIQ) {
1658 current->interrupts.interrupt_type[intid_index] |=
1659 intid_mask;
1660 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001661 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001662 /*
1663 * If it is pending and was enabled before, decrement the count.
1664 */
1665 if (current->interrupts.interrupt_pending[intid_index] &
1666 current->interrupts.interrupt_enabled[intid_index] &
1667 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001668 if ((current->interrupts.interrupt_type[intid_index] &
1669 intid_mask) ==
1670 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1671 vcpu_irq_count_decrement(current_locked);
1672 } else {
1673 vcpu_fiq_count_decrement(current_locked);
1674 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001675 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001676 current->interrupts.interrupt_enabled[intid_index] &=
1677 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001678 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001679 }
1680
Manish Pandey35e452f2021-02-18 21:36:34 +00001681 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001682 return 0;
1683}
1684
1685/**
1686 * Returns the ID of the next pending interrupt for the calling vCPU, and
1687 * acknowledges it (i.e. marks it as no longer pending). Returns
1688 * HF_INVALID_INTID if there are no pending interrupts.
1689 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001690uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001691{
1692 uint8_t i;
1693 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001694 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001695
1696 /*
1697 * Find the first enabled and pending interrupt ID, return it, and
1698 * deactivate it.
1699 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001700 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001701 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1702 uint32_t enabled_and_pending =
1703 current->interrupts.interrupt_enabled[i] &
1704 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001705
Andrew Walbran318f5732018-11-20 16:23:42 +00001706 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001707 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001708 uint32_t intid_mask = 1U << bit_index;
1709
Andrew Walbran3d84a262018-12-13 14:41:19 +00001710 /*
1711 * Mark it as no longer pending and decrement the count.
1712 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001713 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1714
1715 if ((current->interrupts.interrupt_type[i] &
1716 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1717 vcpu_irq_count_decrement(current_locked);
1718 } else {
1719 vcpu_fiq_count_decrement(current_locked);
1720 }
1721
Andrew Walbran3d84a262018-12-13 14:41:19 +00001722 first_interrupt =
1723 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001724 break;
1725 }
1726 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001727
Manish Pandey35e452f2021-02-18 21:36:34 +00001728 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001729 return first_interrupt;
1730}
1731
1732/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001733 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001734 * given VM and vCPU.
1735 */
1736static inline bool is_injection_allowed(uint32_t target_vm_id,
1737 struct vcpu *current)
1738{
1739 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001740
Andrew Walbran318f5732018-11-20 16:23:42 +00001741 /*
1742 * The primary VM is allowed to inject interrupts into any VM. Secondary
1743 * VMs are only allowed to inject interrupts into their own vCPUs.
1744 */
1745 return current_vm_id == HF_PRIMARY_VM_ID ||
1746 current_vm_id == target_vm_id;
1747}
1748
1749/**
1750 * Injects a virtual interrupt of the given ID into the given target vCPU.
1751 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1752 * when the vCPU is next run, which is up to the scheduler.
1753 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001754 * Returns:
1755 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1756 * ID is invalid, or the current VM is not allowed to inject interrupts to
1757 * the target VM.
1758 * - 0 on success if no further action is needed.
1759 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1760 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001761 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001762int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1763 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001764 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001765{
Andrew Walbran318f5732018-11-20 16:23:42 +00001766 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001767 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001768
1769 if (intid >= HF_NUM_INTIDS) {
1770 return -1;
1771 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001772
Andrew Walbran318f5732018-11-20 16:23:42 +00001773 if (target_vm == NULL) {
1774 return -1;
1775 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001776
Andrew Walbran318f5732018-11-20 16:23:42 +00001777 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001778 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001779 return -1;
1780 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001781
Andrew Walbran318f5732018-11-20 16:23:42 +00001782 if (!is_injection_allowed(target_vm_id, current)) {
1783 return -1;
1784 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001785
Andrew Walbrane1310df2019-04-29 17:28:28 +01001786 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001787
Manish Pandey35e452f2021-02-18 21:36:34 +00001788 dlog_verbose(
1789 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1790 "%u\n",
1791 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1792 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001793 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001794}
Andrew Scull6386f252018-12-06 13:29:10 +00001795
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001796/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001797struct ffa_value api_ffa_version(struct vcpu *current,
1798 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001799{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001800 struct vm_locked current_vm_locked;
1801
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001802 /*
1803 * Ensure that both major and minor revision representation occupies at
1804 * most 15 bits.
1805 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001806 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001807 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001808 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001809 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001810 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001811 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001812 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001813 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001814
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001815 current_vm_locked = vm_lock(current->vm);
1816 current_vm_locked.vm->ffa_version = requested_version;
1817 vm_unlock(&current_vm_locked);
1818
Daniel Boulby6e32c612021-02-17 15:09:41 +00001819 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001820}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001821
1822int64_t api_debug_log(char c, struct vcpu *current)
1823{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001824 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001825 struct vm *vm = current->vm;
1826 struct vm_locked vm_locked = vm_lock(vm);
1827
Andrew Sculld54e1be2019-08-20 11:09:42 +01001828 if (c == '\n' || c == '\0') {
1829 flush = true;
1830 } else {
1831 vm->log_buffer[vm->log_buffer_length++] = c;
1832 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1833 }
1834
1835 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001836 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1837 vm->log_buffer_length);
1838 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001839 }
1840
1841 vm_unlock(&vm_locked);
1842
1843 return 0;
1844}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001845
1846/**
J-Alves6f72ca82021-11-01 12:34:58 +00001847 * Helper for success return of FFA_FEATURES, for when it is used to query
1848 * an interrupt ID.
1849 */
1850struct ffa_value api_ffa_feature_success(uint32_t arg2)
1851{
1852 return (struct ffa_value){
1853 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
1854}
1855
1856/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001857 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001858 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001859 */
J-Alves6f72ca82021-11-01 12:34:58 +00001860struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001861{
J-Alves6f72ca82021-11-01 12:34:58 +00001862 /*
1863 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
1864 * if using a feature ID.
1865 */
1866 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
1867 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0) {
1868 return ffa_error(FFA_NOT_SUPPORTED);
1869 }
1870
1871 switch (feature_function_id) {
1872 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001873 case FFA_ERROR_32:
1874 case FFA_SUCCESS_32:
1875 case FFA_INTERRUPT_32:
1876 case FFA_VERSION_32:
1877 case FFA_FEATURES_32:
1878 case FFA_RX_RELEASE_32:
1879 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001880 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001881 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001882 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001883 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001884 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001885 case FFA_MEM_DONATE_32:
1886 case FFA_MEM_LEND_32:
1887 case FFA_MEM_SHARE_32:
1888 case FFA_MEM_RETRIEVE_REQ_32:
1889 case FFA_MEM_RETRIEVE_RESP_32:
1890 case FFA_MEM_RELINQUISH_32:
1891 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001892 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001893 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001894 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001895 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00001896#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00001897 /* FF-A v1.1 features. */
1898 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00001899 case FFA_NOTIFICATION_BITMAP_CREATE_32:
1900 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
1901 case FFA_NOTIFICATION_BIND_32:
1902 case FFA_NOTIFICATION_UNBIND_32:
1903 case FFA_NOTIFICATION_SET_32:
1904 case FFA_NOTIFICATION_GET_32:
1905 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07001906 case FFA_MEM_PERM_GET_32:
1907 case FFA_MEM_PERM_SET_32:
1908 case FFA_MEM_PERM_GET_64:
1909 case FFA_MEM_PERM_SET_64:
J-Alves3829fc02021-03-18 12:49:18 +00001910#endif
1911 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00001912
1913#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
1914 /* Check support of a feature provided respective feature ID. */
1915 case FFA_FEATURE_NPI:
1916 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
1917 case FFA_FEATURE_SRI:
1918 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
1919#endif
1920 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001921 default:
J-Alves6f72ca82021-11-01 12:34:58 +00001922 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001923 }
1924}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001925
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001926/**
J-Alves645eabe2021-02-22 16:08:27 +00001927 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
1928 * interfaces.
1929 */
1930static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
1931{
1932 return args.arg2 == 0U;
1933}
1934
1935/**
J-Alves76d99af2021-03-10 17:42:11 +00001936 * Limits size of arguments in ffa_value structure to 32-bit.
1937 */
1938static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
1939{
1940 return (struct ffa_value){
1941 .func = (uint32_t)args.func,
1942 .arg1 = (uint32_t)args.arg1,
1943 .arg2 = (uint32_t)0,
1944 .arg3 = (uint32_t)args.arg3,
1945 .arg4 = (uint32_t)args.arg4,
1946 .arg5 = (uint32_t)args.arg5,
1947 .arg6 = (uint32_t)args.arg6,
1948 .arg7 = (uint32_t)args.arg7,
1949 };
1950}
1951
1952/**
1953 * Helper to copy direct message payload, depending on SMC used and expected
1954 * registers size.
1955 */
1956static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
1957{
1958 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
1959 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
1960 return api_ffa_value_copy32(args);
1961 }
1962
1963 return (struct ffa_value){
1964 .func = args.func,
1965 .arg1 = args.arg1,
1966 .arg2 = 0,
1967 .arg3 = args.arg3,
1968 .arg4 = args.arg4,
1969 .arg5 = args.arg5,
1970 .arg6 = args.arg6,
1971 .arg7 = args.arg7,
1972 };
1973}
1974
1975/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001976 * Send an FF-A direct message request.
1977 */
1978struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1979 ffa_vm_id_t receiver_vm_id,
1980 struct ffa_value args,
1981 struct vcpu *current,
1982 struct vcpu **next)
1983{
J-Alves17228f72021-04-20 17:13:19 +01001984 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001985 struct vm *receiver_vm;
1986 struct vcpu *receiver_vcpu;
1987 struct two_vcpu_locked vcpus_locked;
1988
J-Alves645eabe2021-02-22 16:08:27 +00001989 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1990 return ffa_error(FFA_INVALID_PARAMETERS);
1991 }
1992
Olivier Deprez55a189e2021-06-09 15:45:27 +02001993 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
1994 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001995 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001996 }
1997
Olivier Deprez55a189e2021-06-09 15:45:27 +02001998 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01001999 return ret;
2000 }
2001
2002 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2003
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002004 receiver_vm = vm_find(receiver_vm_id);
2005 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002006 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002007 return ffa_error(FFA_INVALID_PARAMETERS);
2008 }
2009
2010 /*
2011 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
2012 * UP (migratable) or MP partitions with a number of vCPUs matching the
2013 * number of PEs in the system. It further states that MP partitions
2014 * accepting direct request messages cannot migrate.
2015 */
J-Alvesad6a0432021-04-09 16:06:21 +01002016 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002017 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002018 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002019 return ffa_error(FFA_INVALID_PARAMETERS);
2020 }
2021
2022 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2023
2024 /*
2025 * If destination vCPU is executing or already received an
2026 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2027 * busy. There is a brief period of time where the vCPU state has
2028 * changed but regs_available is still false thus consider this case as
2029 * the vCPU not yet ready to receive a direct message request.
2030 */
2031 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2032 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2033 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002034 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002035 ret = ffa_error(FFA_BUSY);
2036 goto out;
2037 }
2038
2039 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2040 memory_order_relaxed)) {
2041 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002042 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002043 receiver_vcpu->vm->id,
2044 vcpu_index(receiver_vcpu));
2045 receiver_vcpu->state = VCPU_STATE_ABORTED;
2046 }
2047
2048 ret = ffa_error(FFA_ABORTED);
2049 goto out;
2050 }
2051
2052 switch (receiver_vcpu->state) {
2053 case VCPU_STATE_OFF:
2054 case VCPU_STATE_RUNNING:
2055 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002056 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002057 case VCPU_STATE_BLOCKED:
2058 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002059 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2060 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002061 ret = ffa_error(FFA_BUSY);
2062 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002063 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002064 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002065 * We expect target vCPU to be in WAITING state after either
2066 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002067 */
2068 break;
2069 }
2070
2071 /* Inject timer interrupt if any pending */
2072 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002073 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2074 HF_VIRTUAL_TIMER_INTID, current,
2075 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002076
2077 arch_timer_mask(&receiver_vcpu->regs);
2078 }
2079
2080 /* The receiver vCPU runs upon direct message invocation */
2081 receiver_vcpu->cpu = current->cpu;
2082 receiver_vcpu->state = VCPU_STATE_RUNNING;
2083 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002084 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002085
J-Alves76d99af2021-03-10 17:42:11 +00002086 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002087
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002088 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002089
2090 /* Switch to receiver vCPU targeted to by direct msg request */
2091 *next = receiver_vcpu;
2092
2093 /*
2094 * Since this flow will lead to a VM switch, the return value will not
2095 * be applied to current vCPU.
2096 */
2097
2098out:
2099 sl_unlock(&receiver_vcpu->lock);
2100 sl_unlock(&current->lock);
2101
2102 return ret;
2103}
2104
2105/**
2106 * Send an FF-A direct message response.
2107 */
2108struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2109 ffa_vm_id_t receiver_vm_id,
2110 struct ffa_value args,
2111 struct vcpu *current,
2112 struct vcpu **next)
2113{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002114 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002115
2116 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2117 return ffa_error(FFA_INVALID_PARAMETERS);
2118 }
2119
J-Alves76d99af2021-03-10 17:42:11 +00002120 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002121
Olivier Deprez55a189e2021-06-09 15:45:27 +02002122 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2123 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002124 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002125 }
2126
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002127 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002128 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002129 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002130 * No need for REQ/RESP state management as managed exit does
2131 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002132 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002133 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2134 vcpu_unlock(&current_locked);
2135 return ffa_error(FFA_DENIED);
2136 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002137
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002138 /*
2139 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2140 * a secure interrupt to a SP that is performing managed exit.
2141 * We have taken a implementation defined choice to not allow
2142 * Managed exit while a SP is processing a secure interrupt.
2143 */
2144 CHECK(!current->processing_secure_interrupt);
2145
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002146 plat_interrupts_set_priority_mask(0xff);
2147 current->processing_managed_exit = false;
2148 } else {
2149 /*
2150 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2151 * defined originator.
2152 */
2153 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2154 /*
2155 * Sending direct response but direct request origin
2156 * vCPU is not set.
2157 */
2158 vcpu_unlock(&current_locked);
2159 return ffa_error(FFA_DENIED);
2160 }
2161
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002162 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002163 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2164 vcpu_unlock(&current_locked);
2165 return ffa_error(FFA_DENIED);
2166 }
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002167
2168 /*
2169 * Per FF-A v1.1 Beta0 section 7.4, if a secure interrupt is
2170 * handled by an SP in RUNNING state the existing runtime model
2171 * is preserved. Hence, per section 7.3 bullet 3, SP can use
2172 * FFA_MSG_SEND_DIRECT_RESP to return a response after
2173 * interrupt completion.
2174 */
2175 if (current->processing_secure_interrupt) {
2176 /* There is no preempted vCPU to resume. */
2177 CHECK(current->preempted_vcpu == NULL);
2178
2179 /* Unmask interrupts. */
2180 plat_interrupts_set_priority_mask(0xff);
2181
2182 /*
2183 * Clear fields corresponding to secure interrupt
2184 * handling.
2185 */
2186 current->processing_secure_interrupt = false;
2187 current->secure_interrupt_deactivated = false;
2188 current->current_sec_interrupt_id = 0;
2189 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002190 }
2191
2192 /* Clear direct request origin for the caller. */
2193 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2194
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002195 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002196
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002197 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002198 *next = api_switch_to_other_world(
2199 current, to_ret,
2200 /*
2201 * Current vcpu sent a direct response. It moves to
2202 * waiting state.
2203 */
2204 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002205 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002206 *next = api_switch_to_primary(
2207 current, to_ret,
2208 /*
2209 * Current vcpu sent a direct response. It moves to
2210 * waiting state.
2211 */
2212 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002213 } else if (vm_id_is_current_world(receiver_vm_id)) {
2214 /*
2215 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002216 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002217 * made function return error before getting to this point.
2218 */
2219 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002220 /*
2221 * current vcpu sent a direct response.
2222 * It moves to waiting state.
2223 */
2224 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002225 } else {
2226 panic("Invalid direct message response invocation");
2227 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002228
2229 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2230}
2231
J-Alves84658fc2021-06-17 14:37:32 +01002232static bool api_memory_region_check_flags(
2233 struct ffa_memory_region *memory_region, uint32_t share_func)
2234{
2235 switch (share_func) {
2236 case FFA_MEM_SHARE_32:
2237 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2238 0U) {
2239 return false;
2240 }
2241 /* Intentional fall-through */
2242 case FFA_MEM_LEND_32:
2243 case FFA_MEM_DONATE_32: {
2244 /* Bits 31:2 Must Be Zero. */
2245 ffa_memory_receiver_flags_t to_mask =
2246 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2247 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2248
2249 if ((memory_region->flags & to_mask) != 0U) {
2250 return false;
2251 }
2252 break;
2253 }
2254 default:
2255 panic("Check for mem send calls only.\n");
2256 }
2257
2258 /* Last check reserved values are 0 */
2259 return true;
2260}
2261
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002262struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2263 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002264 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002265{
2266 struct vm *from = current->vm;
2267 struct vm *to;
2268 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002269 struct ffa_memory_region *memory_region;
2270 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002271
2272 if (ipa_addr(address) != 0 || page_count != 0) {
2273 /*
2274 * Hafnium only supports passing the descriptor in the TX
2275 * mailbox.
2276 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002277 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002278 }
2279
Andrew Walbranca808b12020-05-15 17:22:28 +01002280 if (fragment_length > length) {
2281 dlog_verbose(
2282 "Fragment length %d greater than total length %d.\n",
2283 fragment_length, length);
2284 return ffa_error(FFA_INVALID_PARAMETERS);
2285 }
2286 if (fragment_length < sizeof(struct ffa_memory_region) +
2287 sizeof(struct ffa_memory_access)) {
2288 dlog_verbose(
2289 "Initial fragment length %d smaller than header size "
2290 "%d.\n",
2291 fragment_length,
2292 sizeof(struct ffa_memory_region) +
2293 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002294 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002295 }
2296
2297 /*
2298 * Check that the sender has configured its send buffer. If the TX
2299 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2300 * be safely accessed after releasing the lock since the TX mailbox
2301 * address can only be configured once.
2302 */
2303 sl_lock(&from->lock);
2304 from_msg = from->mailbox.send;
2305 sl_unlock(&from->lock);
2306
2307 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002308 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002309 }
2310
2311 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002312 * Copy the memory region descriptor to a fresh page from the memory
2313 * pool. This prevents the sender from changing it underneath us, and
2314 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002315 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002316 if (fragment_length > HF_MAILBOX_SIZE ||
2317 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002318 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002319 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002320 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002321 if (memory_region == NULL) {
2322 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002323 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002324 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002325 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002326
2327 /* The sender must match the caller. */
2328 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002329 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002330 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002331 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002332 }
2333
J-Alves84658fc2021-06-17 14:37:32 +01002334 if (!api_memory_region_check_flags(memory_region, share_func)) {
2335 dlog_verbose(
2336 "Memory region reserved arguments must be zero.\n");
2337 ret = ffa_error(FFA_INVALID_PARAMETERS);
2338 goto out;
2339 }
2340
Andrew Walbrana65a1322020-04-06 19:32:32 +01002341 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002342 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002343 dlog_verbose(
2344 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002345 "endpoint memory access descriptors, expected 1).\n",
2346 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002347 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002348 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002349 }
2350
2351 /*
2352 * Ensure that the receiver VM exists and isn't the same as the sender.
2353 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002354 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002355 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002356 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002357 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002358 goto out;
2359 }
2360
2361 if (to->id == HF_TEE_VM_ID) {
2362 /*
2363 * The 'to' VM lock is only needed in the case that it is the
2364 * TEE VM.
2365 */
2366 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2367
2368 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002369 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002370 goto out_unlock;
2371 }
2372
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002373 ret = ffa_memory_tee_send(
2374 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2375 length, fragment_length, share_func, &api_page_pool);
2376 /*
2377 * ffa_tee_memory_send takes ownership of the memory_region, so
2378 * make sure we don't free it.
2379 */
2380 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002381
2382 out_unlock:
2383 vm_unlock(&vm_to_from_lock.vm1);
2384 vm_unlock(&vm_to_from_lock.vm2);
2385 } else {
2386 struct vm_locked from_locked = vm_lock(from);
2387
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002388 ret = ffa_memory_send(from_locked, memory_region, length,
2389 fragment_length, share_func,
2390 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002391 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002392 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002393 * make sure we don't free it.
2394 */
2395 memory_region = NULL;
2396
2397 vm_unlock(&from_locked);
2398 }
2399
2400out:
2401 if (memory_region != NULL) {
2402 mpool_free(&api_page_pool, memory_region);
2403 }
2404
2405 return ret;
2406}
2407
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002408struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2409 uint32_t fragment_length,
2410 ipaddr_t address, uint32_t page_count,
2411 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002412{
2413 struct vm *to = current->vm;
2414 struct vm_locked to_locked;
2415 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002416 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002417 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002418 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002419
2420 if (ipa_addr(address) != 0 || page_count != 0) {
2421 /*
2422 * Hafnium only supports passing the descriptor in the TX
2423 * mailbox.
2424 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002425 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002426 }
2427
Andrew Walbrana65a1322020-04-06 19:32:32 +01002428 if (fragment_length != length) {
2429 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002430 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002431 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002432
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002433 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002434 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002435 message_buffer_size = cpu_get_buffer_size(current->cpu);
2436 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2437 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002438 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002439 }
2440
2441 to_locked = vm_lock(to);
2442 to_msg = to->mailbox.send;
2443
2444 if (to_msg == NULL) {
2445 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002446 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002447 goto out;
2448 }
2449
2450 /*
2451 * Copy the retrieve request descriptor to an internal buffer, so that
2452 * the caller can't change it underneath us.
2453 */
2454 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2455
2456 if (msg_receiver_busy(to_locked, NULL, false)) {
2457 /*
2458 * Can't retrieve memory information if the mailbox is not
2459 * available.
2460 */
2461 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002462 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002463 goto out;
2464 }
2465
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002466 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2467 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002468
2469out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002470 vm_unlock(&to_locked);
2471 return ret;
2472}
2473
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002474struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002475{
2476 struct vm *from = current->vm;
2477 struct vm_locked from_locked;
2478 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002479 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002480 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002481 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002482 uint32_t length;
2483
2484 from_locked = vm_lock(from);
2485 from_msg = from->mailbox.send;
2486
2487 if (from_msg == NULL) {
2488 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002489 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002490 goto out;
2491 }
2492
2493 /*
2494 * Calculate length from relinquish descriptor before copying. We will
2495 * check again later to make sure it hasn't changed.
2496 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002497 length = sizeof(struct ffa_mem_relinquish) +
2498 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2499 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002500 /*
2501 * Copy the relinquish descriptor to an internal buffer, so that the
2502 * caller can't change it underneath us.
2503 */
2504 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002505 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002506 message_buffer_size = cpu_get_buffer_size(current->cpu);
2507 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2508 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002509 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002510 goto out;
2511 }
2512 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2513
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002514 if (sizeof(struct ffa_mem_relinquish) +
2515 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002516 length) {
2517 dlog_verbose(
2518 "Endpoint count changed while copying to internal "
2519 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002520 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002521 goto out;
2522 }
2523
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002524 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2525 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002526
2527out:
2528 vm_unlock(&from_locked);
2529 return ret;
2530}
2531
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002532struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2533 ffa_memory_region_flags_t flags,
2534 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002535{
2536 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002537 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002538
Olivier Deprez55a189e2021-06-09 15:45:27 +02002539 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002540 struct vm_locked to_locked = vm_lock(to);
2541
Andrew Walbranca808b12020-05-15 17:22:28 +01002542 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002543 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002544
Andrew Walbran290b0c92020-02-03 16:37:14 +00002545 vm_unlock(&to_locked);
2546 } else {
2547 struct vm *from = vm_find(HF_TEE_VM_ID);
2548 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2549
Andrew Walbranca808b12020-05-15 17:22:28 +01002550 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2551 vm_to_from_lock.vm2, handle, flags,
2552 &api_page_pool);
2553
2554 vm_unlock(&vm_to_from_lock.vm1);
2555 vm_unlock(&vm_to_from_lock.vm2);
2556 }
2557
2558 return ret;
2559}
2560
2561struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2562 uint32_t fragment_offset,
2563 ffa_vm_id_t sender_vm_id,
2564 struct vcpu *current)
2565{
2566 struct vm *to = current->vm;
2567 struct vm_locked to_locked;
2568 struct ffa_value ret;
2569
2570 /* Sender ID MBZ at virtual instance. */
2571 if (sender_vm_id != 0) {
2572 return ffa_error(FFA_INVALID_PARAMETERS);
2573 }
2574
2575 to_locked = vm_lock(to);
2576
2577 if (msg_receiver_busy(to_locked, NULL, false)) {
2578 /*
2579 * Can't retrieve memory information if the mailbox is not
2580 * available.
2581 */
2582 dlog_verbose("RX buffer not ready.\n");
2583 ret = ffa_error(FFA_BUSY);
2584 goto out;
2585 }
2586
2587 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2588 &api_page_pool);
2589
2590out:
2591 vm_unlock(&to_locked);
2592 return ret;
2593}
2594
2595struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2596 uint32_t fragment_length,
2597 ffa_vm_id_t sender_vm_id,
2598 struct vcpu *current)
2599{
2600 struct vm *from = current->vm;
2601 const void *from_msg;
2602 void *fragment_copy;
2603 struct ffa_value ret;
2604
2605 /* Sender ID MBZ at virtual instance. */
2606 if (sender_vm_id != 0) {
2607 return ffa_error(FFA_INVALID_PARAMETERS);
2608 }
2609
2610 /*
2611 * Check that the sender has configured its send buffer. If the TX
2612 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2613 * be safely accessed after releasing the lock since the TX mailbox
2614 * address can only be configured once.
2615 */
2616 sl_lock(&from->lock);
2617 from_msg = from->mailbox.send;
2618 sl_unlock(&from->lock);
2619
2620 if (from_msg == NULL) {
2621 return ffa_error(FFA_INVALID_PARAMETERS);
2622 }
2623
2624 /*
2625 * Copy the fragment to a fresh page from the memory pool. This prevents
2626 * the sender from changing it underneath us, and also lets us keep it
2627 * around in the share state table if needed.
2628 */
2629 if (fragment_length > HF_MAILBOX_SIZE ||
2630 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2631 dlog_verbose(
2632 "Fragment length %d larger than mailbox size %d.\n",
2633 fragment_length, HF_MAILBOX_SIZE);
2634 return ffa_error(FFA_INVALID_PARAMETERS);
2635 }
2636 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2637 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2638 0) {
2639 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2640 return ffa_error(FFA_INVALID_PARAMETERS);
2641 }
2642 fragment_copy = mpool_alloc(&api_page_pool);
2643 if (fragment_copy == NULL) {
2644 dlog_verbose("Failed to allocate fragment copy.\n");
2645 return ffa_error(FFA_NO_MEMORY);
2646 }
2647 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2648
2649 /*
2650 * Hafnium doesn't support fragmentation of memory retrieve requests
2651 * (because it doesn't support caller-specified mappings, so a request
2652 * will never be larger than a single page), so this must be part of a
2653 * memory send (i.e. donate, lend or share) request.
2654 *
2655 * We can tell from the handle whether the memory transaction is for the
2656 * TEE or not.
2657 */
2658 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2659 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2660 struct vm_locked from_locked = vm_lock(from);
2661
2662 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2663 fragment_length, handle,
2664 &api_page_pool);
2665 /*
2666 * `ffa_memory_send_continue` takes ownership of the
2667 * fragment_copy, so we don't need to free it here.
2668 */
2669 vm_unlock(&from_locked);
2670 } else {
2671 struct vm *to = vm_find(HF_TEE_VM_ID);
2672 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2673
2674 /*
2675 * The TEE RX buffer state is checked in
2676 * `ffa_memory_tee_send_continue` rather than here, as we need
2677 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2678 * than FFA_ERROR FFA_BUSY in case it is busy.
2679 */
2680
2681 ret = ffa_memory_tee_send_continue(
2682 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2683 fragment_length, handle, &api_page_pool);
2684 /*
2685 * `ffa_memory_tee_send_continue` takes ownership of the
2686 * fragment_copy, so we don't need to free it here.
2687 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002688
2689 vm_unlock(&vm_to_from_lock.vm1);
2690 vm_unlock(&vm_to_from_lock.vm2);
2691 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002692
2693 return ret;
2694}
Max Shvetsov40108e72020-08-27 12:39:50 +01002695
2696struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2697 struct vcpu *current)
2698{
2699 struct vm_locked vm_locked;
2700
2701 vm_locked = vm_lock(current->vm);
2702 vm_locked.vm->secondary_ep = entry_point;
2703 vm_unlock(&vm_locked);
2704
2705 return (struct ffa_value){.func = FFA_SUCCESS_32};
2706}
J-Alvesa0f317d2021-06-09 13:31:59 +01002707
2708struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
2709 ffa_vcpu_count_t vcpu_count,
2710 struct vcpu *current)
2711{
2712 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2713 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
2714 vm_id);
2715 return ffa_error(FFA_NOT_SUPPORTED);
2716 }
2717
2718 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
2719}
2720
2721struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
2722 struct vcpu *current)
2723{
2724 /*
2725 * Validity of use of this interface is the same as for bitmap create.
2726 */
2727 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2728 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
2729 vm_id);
2730 return ffa_error(FFA_NOT_SUPPORTED);
2731 }
2732
2733 return plat_ffa_notifications_bitmap_destroy(vm_id);
2734}
J-Alvesc003a7a2021-03-18 13:06:53 +00002735
2736struct ffa_value api_ffa_notification_update_bindings(
2737 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2738 ffa_notifications_bitmap_t notifications, bool is_bind,
2739 struct vcpu *current)
2740{
2741 struct ffa_value ret = {.func = FFA_SUCCESS_32};
2742 struct vm_locked receiver_locked;
2743 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2744 const ffa_vm_id_t id_to_update =
2745 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
2746 const ffa_vm_id_t id_to_validate =
2747 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
2748
2749 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
2750 receiver_vm_id)) {
2751 dlog_verbose("Invalid use of notifications bind interface.\n");
2752 return ffa_error(FFA_INVALID_PARAMETERS);
2753 }
2754
J-Alvesb15e9402021-09-08 11:44:42 +01002755 if (plat_ffa_notifications_update_bindings_forward(
2756 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
2757 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01002758 return ret;
2759 }
2760
J-Alvesc003a7a2021-03-18 13:06:53 +00002761 if (notifications == 0U) {
2762 dlog_verbose("No notifications have been specified.\n");
2763 return ffa_error(FFA_INVALID_PARAMETERS);
2764 }
2765
2766 /**
2767 * This check assumes receiver is the current VM, and has been enforced
2768 * by 'plat_ffa_is_notifications_bind_valid'.
2769 */
2770 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2771
2772 if (receiver_locked.vm == NULL) {
2773 dlog_verbose("Receiver doesn't exist!\n");
2774 return ffa_error(FFA_DENIED);
2775 }
2776
J-Alves09ff9d82021-11-02 11:55:20 +00002777 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00002778 dlog_verbose("Notifications are not enabled.\n");
2779 ret = ffa_error(FFA_NOT_SUPPORTED);
2780 goto out;
2781 }
2782
2783 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
2784 vm_find(sender_vm_id) == NULL) {
2785 dlog_verbose("Sender VM does not exist!\n");
2786 ret = ffa_error(FFA_INVALID_PARAMETERS);
2787 goto out;
2788 }
2789
2790 /*
2791 * Can't bind/unbind notifications if at least one is bound to a
2792 * different sender.
2793 */
2794 if (!vm_notifications_validate_bound_sender(
2795 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2796 id_to_validate, notifications)) {
2797 dlog_verbose("Notifications are bound to other sender.\n");
2798 ret = ffa_error(FFA_DENIED);
2799 goto out;
2800 }
2801
2802 /**
2803 * Check if there is a pending notification within those specified in
2804 * the bitmap.
2805 */
2806 if (vm_are_notifications_pending(receiver_locked,
2807 plat_ffa_is_vm_id(sender_vm_id),
2808 notifications)) {
2809 dlog_verbose("Notifications within '%x' pending.\n",
2810 notifications);
2811 ret = ffa_error(FFA_DENIED);
2812 goto out;
2813 }
2814
2815 vm_notifications_update_bindings(
2816 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
2817 notifications, is_per_vcpu && is_bind);
2818
2819out:
2820 vm_unlock(&receiver_locked);
2821 return ret;
2822}
J-Alvesaa79c012021-07-09 14:29:45 +01002823
2824struct ffa_value api_ffa_notification_set(
2825 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2826 ffa_notifications_bitmap_t notifications, struct vcpu *current)
2827{
2828 struct ffa_value ret;
2829 struct vm_locked receiver_locked;
2830
2831 /*
2832 * Check if is per-vCPU or global, and extracting vCPU ID according
2833 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
2834 */
2835 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2836 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
2837
J-Alvesaa79c012021-07-09 14:29:45 +01002838 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
2839 receiver_vm_id)) {
2840 dlog_verbose("Invalid use of notifications set interface.\n");
2841 return ffa_error(FFA_INVALID_PARAMETERS);
2842 }
2843
2844 if (notifications == 0U) {
2845 dlog_verbose("No notifications have been specified.\n");
2846 return ffa_error(FFA_INVALID_PARAMETERS);
2847 }
2848
J-Alvesde7bd2f2021-09-09 19:54:35 +01002849 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
2850 flags, notifications, &ret)) {
2851 return ret;
2852 }
2853
J-Alvesaa79c012021-07-09 14:29:45 +01002854 /*
2855 * This check assumes receiver is the current VM, and has been enforced
2856 * by 'plat_ffa_is_notification_set_valid'.
2857 */
2858 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2859
2860 if (receiver_locked.vm == NULL) {
2861 dlog_verbose("Receiver ID is not valid.\n");
2862 return ffa_error(FFA_INVALID_PARAMETERS);
2863 }
2864
J-Alves09ff9d82021-11-02 11:55:20 +00002865 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01002866 dlog_verbose("Receiver's notifications not enabled.\n");
2867 ret = ffa_error(FFA_DENIED);
2868 goto out;
2869 }
2870
2871 /*
2872 * If notifications are not bound to the sender, they wouldn't be
2873 * enabled either for the receiver.
2874 */
2875 if (!vm_notifications_validate_binding(
2876 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2877 sender_vm_id, notifications, is_per_vcpu)) {
2878 dlog_verbose("Notifications bindings not valid.\n");
2879 ret = ffa_error(FFA_DENIED);
2880 goto out;
2881 }
2882
2883 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
2884 dlog_verbose("Invalid VCPU ID!\n");
2885 ret = ffa_error(FFA_INVALID_PARAMETERS);
2886 goto out;
2887 }
2888
J-Alves7461ef22021-10-18 17:21:33 +01002889 /* Set notifications pending. */
J-Alvesaa79c012021-07-09 14:29:45 +01002890 vm_notifications_set(receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2891 notifications, vcpu_id, is_per_vcpu);
2892 dlog_verbose("Set the notifications: %x.\n", notifications);
2893
J-Alves13394022021-06-30 13:48:49 +01002894 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2895 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2896 vcpu_index(current));
2897 plat_ffa_sri_trigger_not_delayed(current->cpu);
2898 } else {
2899 plat_ffa_sri_state_set(DELAYED);
2900 }
J-Alvesaa79c012021-07-09 14:29:45 +01002901
J-Alves7461ef22021-10-18 17:21:33 +01002902 /*
2903 * If notifications set are per-vCPU and the receiver is SP, the
2904 * Notifications Pending Interrupt can be injected now.
2905 * If not, it should be injected when the scheduler gives it CPU cycles
2906 * in a specific vCPU.
2907 */
2908 if (is_per_vcpu && vm_id_is_current_world(receiver_vm_id)) {
2909 struct vcpu *target_vcpu =
2910 vm_get_vcpu(receiver_locked.vm, vcpu_id);
2911
2912 dlog_verbose("Per-vCPU notification, pending NPI.\n");
J-Alves6f72ca82021-11-01 12:34:58 +00002913 internal_interrupt_inject(target_vcpu,
2914 HF_NOTIFICATION_PENDING_INTID,
2915 current, NULL);
J-Alves7461ef22021-10-18 17:21:33 +01002916 }
2917
J-Alves13394022021-06-30 13:48:49 +01002918 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01002919out:
2920 vm_unlock(&receiver_locked);
2921
2922 return ret;
2923}
2924
2925static struct ffa_value api_ffa_notification_get_success_return(
2926 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
2927 ffa_notifications_bitmap_t from_framework)
2928{
2929 return (struct ffa_value){
2930 .func = FFA_SUCCESS_32,
2931 .arg1 = 0U,
2932 .arg2 = (uint32_t)from_sp,
2933 .arg3 = (uint32_t)(from_sp >> 32),
2934 .arg4 = (uint32_t)from_vm,
2935 .arg5 = (uint32_t)(from_vm >> 32),
2936 .arg6 = (uint32_t)from_framework,
2937 .arg7 = (uint32_t)(from_framework >> 32),
2938 };
2939}
2940
2941struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
2942 ffa_vcpu_index_t vcpu_id,
2943 uint32_t flags, struct vcpu *current)
2944{
2945 /* TODO: get framework notifications, when these are supported. */
2946 ffa_notifications_bitmap_t sp_notifications = 0;
2947 ffa_notifications_bitmap_t vm_notifications = 0;
2948 struct vm_locked receiver_locked;
2949 struct ffa_value ret;
2950
2951 /*
2952 * Following check should capture wrong uses of the interface, depending
2953 * on whether Hafnium is SPMC or hypervisor.
2954 * On the rest of the function it is assumed this condition is met.
2955 */
2956 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id)) {
2957 dlog_verbose("Invalid use of notifications get interface.\n");
2958 return ffa_error(FFA_INVALID_PARAMETERS);
2959 }
2960
2961 /*
2962 * This check assumes receiver is the current VM, and has been enforced
2963 * by `plat_ffa_is_notifications_get_valid`.
2964 */
2965 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2966
2967 /*
2968 * `plat_ffa_is_notifications_get_valid` ensures following is never
2969 * true.
2970 */
2971 CHECK(receiver_locked.vm != NULL);
2972
2973 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
2974 (receiver_locked.vm->vcpu_count != 1 &&
2975 cpu_index(current->cpu) != vcpu_id)) {
2976 dlog_verbose("Invalid VCPU ID!\n");
2977 ret = ffa_error(FFA_INVALID_PARAMETERS);
2978 goto out;
2979 }
2980
2981 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01002982 if (!plat_ffa_notifications_get_from_sp(
2983 receiver_locked, vcpu_id, &sp_notifications,
2984 &ret)) {
2985 dlog_verbose("Failed to get notifications from sps.");
2986 goto out;
2987 }
J-Alvesaa79c012021-07-09 14:29:45 +01002988 }
2989
2990 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
2991 vm_notifications = vm_notifications_get_pending_and_clear(
2992 receiver_locked, true, vcpu_id);
2993 }
2994
2995 ret = api_ffa_notification_get_success_return(sp_notifications,
2996 vm_notifications, 0);
2997
J-Alvesfe23ebe2021-10-13 16:07:07 +01002998 /*
2999 * If there are no more pending notifications, change `sri_state` to
3000 * handled.
3001 */
3002 if (vm_is_notifications_pending_count_zero()) {
3003 plat_ffa_sri_state_set(HANDLED);
3004 }
3005
J-Alvesaa79c012021-07-09 14:29:45 +01003006out:
3007 vm_unlock(&receiver_locked);
3008
3009 return ret;
3010}
J-Alvesc8e8a222021-06-08 17:33:52 +01003011
3012/**
3013 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3014 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3015 */
3016static struct ffa_value api_ffa_notification_info_get_success_return(
3017 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003018 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003019{
3020 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3021
3022 /*
3023 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3024 * hold the list of ids.
3025 */
3026 memcpy_s(&ret.arg3,
3027 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3028 sizeof(ids[0]) * ids_count);
3029
3030 /*
3031 * According to the spec x2 should have:
3032 * - Bit flagging if there are more notifications pending;
3033 * - The total number of elements (i.e. total list size);
3034 * - The number of VCPU IDs within each VM specific list.
3035 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003036 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3037 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3038 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003039
3040 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3041 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3042
3043 for (unsigned int i = 0; i < lists_count; i++) {
3044 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3045 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3046 }
3047
3048 return ret;
3049}
3050
3051struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3052{
3053 /*
3054 * Following set of variables should be populated with the return info.
3055 * At a successfull handling of this interface, they should be used
3056 * to populate the 'ret' structure in accordance to the table 17.29
3057 * of the FF-A v1.1 Beta0 specification.
3058 */
3059 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3060 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3061 uint32_t lists_count = 0;
3062 uint32_t ids_count = 0;
3063 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003064 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003065
3066 /*
3067 * This interface can only be called at NS virtual/physical FF-A
3068 * instance by the endpoint implementing the primary scheduler and the
3069 * Hypervisor/OS kernel.
3070 * In the SPM, following check passes if call has been forwarded from
3071 * the hypervisor.
3072 */
3073 if (current->vm->id != HF_PRIMARY_VM_ID) {
3074 dlog_verbose(
3075 "Only the receiver's scheduler can use this "
3076 "interface\n");
3077 return ffa_error(FFA_NOT_SUPPORTED);
3078 }
3079
J-Alvesca058c22021-09-10 14:02:07 +01003080 /*
3081 * Forward call to the other world, and fill the arrays used to assemble
3082 * return.
3083 */
3084 plat_ffa_notification_info_get_forward(
3085 ids, &ids_count, lists_sizes, &lists_count,
3086 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3087
3088 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3089
J-Alvesc8e8a222021-06-08 17:33:52 +01003090 /* Get notifications' info from this world */
3091 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3092 ++index) {
3093 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3094
3095 list_is_full = vm_notifications_info_get(
3096 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3097 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3098
3099 vm_unlock(&vm_locked);
3100 }
3101
3102 if (!list_is_full) {
3103 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003104 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003105 ids, &ids_count, lists_sizes, &lists_count,
3106 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3107 }
3108
3109 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003110 dlog_verbose(
3111 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003112 result = ffa_error(FFA_NO_DATA);
3113 } else {
3114 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003115 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003116 }
3117
J-Alvesfe23ebe2021-10-13 16:07:07 +01003118 plat_ffa_sri_state_set(HANDLED);
3119
J-Alves13394022021-06-30 13:48:49 +01003120 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003121}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003122
3123struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3124{
3125 struct vm_locked vm_locked;
3126 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3127 bool mode_ret = false;
3128 uint32_t mode = 0;
3129
3130 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3131 return ffa_error(FFA_NOT_SUPPORTED);
3132 }
3133
3134 if (!(current->vm->el0_partition)) {
3135 return ffa_error(FFA_DENIED);
3136 }
3137
3138 vm_locked = vm_lock(current->vm);
3139
3140 /*
3141 * mm_get_mode is used to check if the given base_addr page is already
3142 * mapped. If the page is unmapped, return error. If the page is mapped
3143 * appropriate attributes are returned to the caller. Note that
3144 * mm_get_mode returns true if the address is in the valid VA range as
3145 * supported by the architecture and MMU configurations, as opposed to
3146 * whether a page is mapped or not. For a page to be known as mapped,
3147 * the API must return true AND the returned mode must not have
3148 * MM_MODE_INVALID set.
3149 */
3150 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3151 va_add(base_addr, PAGE_SIZE), &mode);
3152 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3153 ret = ffa_error(FFA_INVALID_PARAMETERS);
3154 goto out;
3155 }
3156
3157 /* No memory should be marked RWX */
3158 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3159 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3160
3161 /*
3162 * S-EL0 partitions are expected to have all their pages marked as
3163 * non-global.
3164 */
3165 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3166 (MM_MODE_NG | MM_MODE_USER));
3167
3168 if (mode & MM_MODE_W) {
3169 /* No memory should be writeable but not readable. */
3170 CHECK(mode & MM_MODE_R);
3171 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3172 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3173 } else if (mode & MM_MODE_R) {
3174 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3175 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3176 if (!(mode & MM_MODE_X)) {
3177 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3178 }
3179 }
3180out:
3181 vm_unlock(&vm_locked);
3182 return ret;
3183}
3184
3185struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3186 uint32_t mem_perm, struct vcpu *current)
3187{
3188 struct vm_locked vm_locked;
3189 struct ffa_value ret;
3190 bool mode_ret = false;
3191 uint32_t original_mode;
3192 uint32_t new_mode;
3193 struct mpool local_page_pool;
3194
3195 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3196 return ffa_error(FFA_NOT_SUPPORTED);
3197 }
3198
3199 if (!(current->vm->el0_partition)) {
3200 return ffa_error(FFA_DENIED);
3201 }
3202
3203 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3204 return ffa_error(FFA_INVALID_PARAMETERS);
3205 }
3206
3207 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3208 (mem_perm != FFA_MEM_PERM_RX)) {
3209 return ffa_error(FFA_INVALID_PARAMETERS);
3210 }
3211
3212 /*
3213 * Create a local pool so any freed memory can't be used by another
3214 * thread. This is to ensure the original mapping can be restored if any
3215 * stage of the process fails.
3216 */
3217 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3218
3219 vm_locked = vm_lock(current->vm);
3220
3221 /*
3222 * All regions accessible by the partition are mapped during boot. If we
3223 * cannot get a successful translation for the page range, the request
3224 * to change permissions is rejected.
3225 * mm_get_mode is used to check if the given address range is already
3226 * mapped. If the range is unmapped, return error. If the range is
3227 * mapped appropriate attributes are returned to the caller. Note that
3228 * mm_get_mode returns true if the address is in the valid VA range as
3229 * supported by the architecture and MMU configurations, as opposed to
3230 * whether a page is mapped or not. For a page to be known as mapped,
3231 * the API must return true AND the returned mode must not have
3232 * MM_MODE_INVALID set.
3233 */
3234
3235 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3236 va_add(base_addr, page_count * PAGE_SIZE),
3237 &original_mode);
3238 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3239 ret = ffa_error(FFA_INVALID_PARAMETERS);
3240 goto out;
3241 }
3242
3243 /* Device memory cannot be marked as executable */
3244 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3245 ret = ffa_error(FFA_INVALID_PARAMETERS);
3246 goto out;
3247 }
3248
3249 new_mode = MM_MODE_USER | MM_MODE_NG;
3250
3251 if (mem_perm == FFA_MEM_PERM_RW) {
3252 new_mode |= MM_MODE_R | MM_MODE_W;
3253 } else if (mem_perm == FFA_MEM_PERM_RX) {
3254 new_mode |= MM_MODE_R | MM_MODE_X;
3255 } else if (mem_perm == FFA_MEM_PERM_RO) {
3256 new_mode |= MM_MODE_R;
3257 }
3258
3259 /*
3260 * Safe to re-map memory, since we know the requested permissions are
3261 * valid, and the memory requested to be re-mapped is also valid.
3262 */
3263 if (!mm_identity_prepare(
3264 &vm_locked.vm->ptable, pa_from_va(base_addr),
3265 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3266 new_mode, &local_page_pool)) {
3267 /*
3268 * Defrag the table into the local page pool.
3269 * mm_identity_prepare could have allocated or freed pages to
3270 * split blocks or tables etc.
3271 */
3272 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3273
3274 /*
3275 * Guaranteed to succeed mapping with old mode since the mapping
3276 * with old mode already existed and we have a local page pool
3277 * that should have sufficient memory to go back to the original
3278 * state.
3279 */
3280 CHECK(mm_identity_prepare(
3281 &vm_locked.vm->ptable, pa_from_va(base_addr),
3282 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3283 original_mode, &local_page_pool));
3284 mm_identity_commit(
3285 &vm_locked.vm->ptable, pa_from_va(base_addr),
3286 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3287 original_mode, &local_page_pool);
3288
3289 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3290 ret = ffa_error(FFA_NO_MEMORY);
3291 goto out;
3292 }
3293
3294 mm_identity_commit(
3295 &vm_locked.vm->ptable, pa_from_va(base_addr),
3296 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3297 &local_page_pool);
3298
3299 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3300
3301out:
3302 mpool_fini(&local_page_pool);
3303 vm_unlock(&vm_locked);
3304
3305 return ret;
3306}