blob: 0d628bd94863f78e0601d53ff6239e80e222fcb2 [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;
367 struct ffa_partition_info partitions[MAX_VMS];
368
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
391 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
392 if (vm_count == 0) {
393 return ffa_error(FFA_INVALID_PARAMETERS);
394 }
395
396 size = vm_count * sizeof(partitions[0]);
397 if (size > FFA_MSG_PAYLOAD_MAX) {
398 dlog_error(
399 "Partition information does not fit in the VM's RX "
400 "buffer.\n");
401 return ffa_error(FFA_NO_MEMORY);
402 }
403
404 /*
405 * Partition information is returned in the VM's RX buffer, which is why
406 * the lock is needed.
407 */
408 current_vm_locked = vm_lock(current_vm);
409
410 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
411 /*
412 * Can't retrieve memory information if the mailbox is not
413 * available.
414 */
415 dlog_verbose("RX buffer not ready.\n");
416 ret = ffa_error(FFA_BUSY);
417 goto out_unlock;
418 }
419
420 /* Populate the VM's RX buffer with the partition information. */
421 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
422 size);
423 current_vm->mailbox.recv_size = size;
424 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
425 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
426 current_vm->mailbox.state = MAILBOX_STATE_READ;
427
428 /* Return the count of partition information descriptors in w2. */
429 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
430
431out_unlock:
432 vm_unlock(&current_vm_locked);
433
434 return ret;
435}
436
Andrew Scull9726c252019-01-23 13:44:19 +0000437/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000438 * Returns the ID of the VM.
439 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100440struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000441{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100442 return (struct ffa_value){.func = FFA_SUCCESS_32,
443 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000444}
445
446/**
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000447 * Returns the ID of the SPMC.
448 */
449struct ffa_value api_ffa_spm_id_get(void)
450{
J-Alves3829fc02021-03-18 12:49:18 +0000451#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
452 /*
453 * Return the SPMC ID that was fetched during FF-A
454 * initialization.
455 */
456 return (struct ffa_value){.func = FFA_SUCCESS_32,
457 .arg2 = arch_ffa_spmc_id_get()};
458#else
459 return ffa_error(FFA_NOT_SUPPORTED);
460#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000461}
462
463/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000464 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000465 * function to indicate that register state for the given vCPU has been saved
466 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000467 */
468void api_regs_state_saved(struct vcpu *vcpu)
469{
470 sl_lock(&vcpu->lock);
471 vcpu->regs_available = true;
472 sl_unlock(&vcpu->lock);
473}
474
475/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000476 * Retrieves the next waiter and removes it from the wait list if the VM's
477 * mailbox is in a writable state.
478 */
479static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
480{
481 struct wait_entry *entry;
482 struct vm *vm = locked_vm.vm;
483
Andrew Sculld6ee1102019-04-05 22:12:42 +0100484 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000485 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
486 /* The mailbox is not writable or there are no waiters. */
487 return NULL;
488 }
489
490 /* Remove waiter from the wait list. */
491 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
492 wait_links);
493 list_remove(&entry->wait_links);
494 return entry;
495}
496
497/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000498 * Assuming that the arguments have already been checked by the caller, injects
499 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
500 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
501 * is next run, which is up to the scheduler.
502 *
503 * Returns:
504 * - 0 on success if no further action is needed.
505 * - 1 if it was called by the primary VM and the primary VM now needs to wake
506 * up or kick the target vCPU.
507 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100508int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
509 uint32_t intid, struct vcpu *current,
510 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000511{
Manish Pandey35e452f2021-02-18 21:36:34 +0000512 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000513 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000514 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
515 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000516 int64_t ret = 0;
517
Andrew Walbran508e63c2018-12-20 17:02:37 +0000518 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000519 * We only need to change state and (maybe) trigger a virtual interrupt
520 * if it is enabled and was not previously pending. Otherwise we can
521 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000522 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000523 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
524 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000525 intid_mask)) {
526 goto out;
527 }
528
529 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000530 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
531 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
532 vcpu_irq_count_increment(target_locked);
533 } else {
534 vcpu_fiq_count_increment(target_locked);
535 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000536
537 /*
538 * Only need to update state if there was not already an
539 * interrupt enabled and pending.
540 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000541 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000542 goto out;
543 }
544
Andrew Walbran508e63c2018-12-20 17:02:37 +0000545 if (current->vm->id == HF_PRIMARY_VM_ID) {
546 /*
547 * If the call came from the primary VM, let it know that it
548 * should run or kick the target vCPU.
549 */
550 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000551 } else if (current != target_vcpu && next != NULL) {
552 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000553 }
554
555out:
556 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000557 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000558
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200559 return ret;
560}
561
562/* Wrapper to internal_interrupt_inject with locking of target vCPU */
563static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
564 uint32_t intid, struct vcpu *current,
565 struct vcpu **next)
566{
567 int64_t ret;
568 struct vcpu_locked target_locked;
569
570 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100571 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200572 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000573
574 return ret;
575}
576
577/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100578 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
579 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100580 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100581static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100582{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000583 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100584 case FFA_MSG_SEND_32:
585 return (struct ffa_value){
586 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000587 .arg1 = (receiver->mailbox.recv_sender << 16) |
588 receiver->id,
589 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000590 default:
591 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000592 dlog_error("Tried to return an invalid message function %#x\n",
593 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100594 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000595 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100596}
597
598/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000599 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000600 * value needs to be forced onto the vCPU.
601 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000602static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100603 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000604{
Max Shvetsov40108e72020-08-27 12:39:50 +0100605 struct vcpu_locked vcpu_locked;
606 struct vm_locked vm_locked;
Andrew Scullb06d1752019-02-04 10:15:48 +0000607 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000608 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500609 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000610
Andrew Scullb06d1752019-02-04 10:15:48 +0000611 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000612 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000613 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100614 * The VM lock is not needed in the common case so it must only be taken
615 * when it is going to be needed. This ensures there are no inter-vCPU
616 * dependencies in the common run case meaning the sensitive context
617 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000618 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100619 vcpu_locked = vcpu_lock(vcpu);
620
621#if SECURE_WORLD == 1
622
623 if (vcpu_secondary_reset_and_start(vcpu_locked, vcpu->vm->secondary_ep,
624 0)) {
625 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
626 __func__, vcpu->vm->id, current->cpu->id);
627 }
628
629#endif
Andrew Scullb06d1752019-02-04 10:15:48 +0000630
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000631 /* The VM needs to be locked to deliver mailbox messages. */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500632 need_vm_lock = vcpu->state == VCPU_STATE_WAITING;
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000633 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100634 vcpu_unlock(&vcpu_locked);
635 vm_locked = vm_lock(vcpu->vm);
636 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000637 }
638
639 /*
640 * If the vCPU is already running somewhere then we can't run it here
641 * simultaneously. While it is actually running then the state should be
642 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
643 * stops running but while Hafnium is in the process of switching back
644 * to the primary there will be a brief period while the state has been
645 * updated but `regs_available` is still false (until
646 * `api_regs_state_saved` is called). We can't start running it again
647 * until this has finished, so count this state as still running for the
648 * purposes of this check.
649 */
650 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
651 /*
652 * vCPU is running on another pCPU.
653 *
654 * It's okay not to return the sleep duration here because the
655 * other physical CPU that is currently running this vCPU will
656 * return the sleep duration if needed.
657 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100658 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000659 ret = false;
660 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000661 }
Andrew Scull9726c252019-01-23 13:44:19 +0000662
663 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100664 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100665 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000666 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100667 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000668 }
669 ret = false;
670 goto out;
671 }
672
Andrew Walbran508e63c2018-12-20 17:02:37 +0000673 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100674 case VCPU_STATE_RUNNING:
675 case VCPU_STATE_OFF:
676 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000677 ret = false;
678 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000679
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500680 case VCPU_STATE_WAITING:
681 /*
682 * An initial FFA_RUN is necessary for secondary VM/SP to reach
683 * the message wait loop.
684 */
685 if (!vcpu->is_bootstrapped) {
686 vcpu->is_bootstrapped = true;
687 break;
688 }
689
Andrew Scullb06d1752019-02-04 10:15:48 +0000690 /*
691 * A pending message allows the vCPU to run so the message can
692 * be delivered directly.
693 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100694 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100695 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100696 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100697 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000698 break;
699 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500700
701 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
702 break;
703 }
704
705 if (arch_timer_enabled(&vcpu->regs)) {
706 timer_remaining_ns =
707 arch_timer_remaining_ns(&vcpu->regs);
708 if (timer_remaining_ns == 0) {
709 break;
710 }
711 } else {
712 dlog_verbose("Timer disabled\n");
713 }
714 run_ret->func = FFA_MSG_WAIT_32;
715 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
716 run_ret->arg2 = timer_remaining_ns;
717 ret = false;
718 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100719 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000720 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000721 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000722 break;
723 }
724
Andrew Walbran508e63c2018-12-20 17:02:37 +0000725 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100726 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000727 arch_timer_remaining_ns(&vcpu->regs);
728
729 /*
730 * The timer expired so allow the interrupt to be
731 * delivered.
732 */
733 if (timer_remaining_ns == 0) {
734 break;
735 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100736 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000737
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100738 /*
739 * The vCPU is not ready to run, return the appropriate code to
740 * the primary which called vcpu_run.
741 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500742 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100743 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
744 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000745
746 ret = false;
747 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000748
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500749 case VCPU_STATE_BLOCKED:
750 /* A blocked vCPU is run unconditionally. Fall through. */
751 case VCPU_STATE_PREEMPTED:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000752 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500753 default:
754 /*
755 * Execution not expected to reach here. Deny the request
756 * gracefully.
757 */
758 *run_ret = ffa_error(FFA_DENIED);
759 ret = false;
760 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000761 }
762
Andrew Scullb06d1752019-02-04 10:15:48 +0000763 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000764 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100765 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000766
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000767 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000768 * Mark the registers as unavailable now that we're about to reflect
769 * them onto the real registers. This will also prevent another physical
770 * CPU from trying to read these registers.
771 */
772 vcpu->regs_available = false;
773
774 ret = true;
775
776out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100777 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000778 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100779 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000780 }
781
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000782 return ret;
783}
784
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100785struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500786 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100787{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100788 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100789 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100790 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100791
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500792 if (!plat_ffa_run_checks(current, vm_id, &ret, next)) {
793 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100794 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100795
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700796 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
797 return ret;
798 }
799
Andrew Scull19503262018-09-20 14:48:39 +0100800 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100801 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100802 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100803 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100804 }
805
Fuad Tabbaed294af2019-12-20 10:43:01 +0000806 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100807 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100808 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100809 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100810
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000811 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100812 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000813 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000814 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100815 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000816
Andrew Walbran508e63c2018-12-20 17:02:37 +0000817 /*
818 * Inject timer interrupt if timer has expired. It's safe to access
819 * vcpu->regs here because api_vcpu_prepare_run already made sure that
820 * regs_available was true (and then set it to false) before returning
821 * true.
822 */
823 if (arch_timer_pending(&vcpu->regs)) {
824 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100825 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
826 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000827
828 /*
829 * Set the mask bit so the hardware interrupt doesn't fire
830 * again. Ideally we wouldn't do this because it affects what
831 * the secondary vCPU sees, but if we don't then we end up with
832 * a loop of the interrupt firing each time we try to return to
833 * the secondary vCPU.
834 */
835 arch_timer_mask(&vcpu->regs);
836 }
837
Fuad Tabbaed294af2019-12-20 10:43:01 +0000838 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000839 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000840
Andrew Scull33fecd32019-01-08 14:48:27 +0000841 /*
842 * Set a placeholder return code to the scheduler. This will be
843 * overwritten when the switch back to the primary occurs.
844 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100845 ret.func = FFA_INTERRUPT_32;
846 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100847 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000848
Andrew Scull6d2db332018-10-10 15:28:17 +0100849out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100850 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100851}
852
853/**
Andrew Scull81e85092018-12-12 12:56:20 +0000854 * Check that the mode indicates memory that is valid, owned and exclusive.
855 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100856static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000857{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100858 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
859 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000860}
861
862/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100863 * Determines the value to be returned by api_ffa_rxtx_map and
864 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
865 * there are waiters, it also switches back to the primary VM for it to wake
866 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000867 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100868static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
869 struct vcpu *current,
870 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000871{
872 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000873
874 if (list_empty(&vm->mailbox.waiter_list)) {
875 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100876 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000877 }
878
879 if (vm->id == HF_PRIMARY_VM_ID) {
880 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000882 }
883
884 /*
885 * Switch back to the primary VM, informing it that there are waiters
886 * that need to be notified.
887 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000888 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100889 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500890 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000891
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100892 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000893}
894
895/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100896 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100897 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100898static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
899 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100900 paddr_t pa_send_begin, paddr_t pa_send_end,
901 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200902 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100903 struct mpool *local_page_pool)
904{
905 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100906
907 /* Map the send page as read-only in the hypervisor address space. */
908 vm_locked.vm->mailbox.send =
909 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200910 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100911 if (!vm_locked.vm->mailbox.send) {
912 /* TODO: partial defrag of failed range. */
913 /* Recover any memory consumed in failed mapping. */
914 mm_defrag(mm_stage1_locked, local_page_pool);
915 goto fail;
916 }
917
918 /*
919 * Map the receive page as writable in the hypervisor address space. On
920 * failure, unmap the send page before returning.
921 */
922 vm_locked.vm->mailbox.recv =
923 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200924 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100925 if (!vm_locked.vm->mailbox.recv) {
926 /* TODO: partial defrag of failed range. */
927 /* Recover any memory consumed in failed mapping. */
928 mm_defrag(mm_stage1_locked, local_page_pool);
929 goto fail_undo_send;
930 }
931
932 ret = true;
933 goto out;
934
935 /*
936 * The following mappings will not require more memory than is available
937 * in the local pool.
938 */
939fail_undo_send:
940 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100941 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
942 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100943
944fail:
945 ret = false;
946
947out:
Andrew Sculle1322792019-07-01 17:46:10 +0100948 return ret;
949}
950
951/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100952 * Sanity checks and configures the send and receive pages in the VM stage-2
953 * and hypervisor stage-1 page tables.
954 *
955 * Returns:
956 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100957 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +0100958 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
959 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100960 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +0100961 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100962 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100963
964struct ffa_value api_vm_configure_pages(
965 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
966 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
967 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100968{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100969 struct ffa_value ret;
970 paddr_t pa_send_begin;
971 paddr_t pa_send_end;
972 paddr_t pa_recv_begin;
973 paddr_t pa_recv_end;
974 uint32_t orig_send_mode;
975 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200976 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100977
978 /* We only allow these to be setup once. */
979 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
980 ret = ffa_error(FFA_DENIED);
981 goto out;
982 }
983
984 /* Hafnium only supports a fixed size of RX/TX buffers. */
985 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
986 ret = ffa_error(FFA_INVALID_PARAMETERS);
987 goto out;
988 }
989
990 /* Fail if addresses are not page-aligned. */
991 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
992 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
993 ret = ffa_error(FFA_INVALID_PARAMETERS);
994 goto out;
995 }
996
997 /* Convert to physical addresses. */
998 pa_send_begin = pa_from_ipa(send);
999 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1000 pa_recv_begin = pa_from_ipa(recv);
1001 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1002
1003 /* Fail if the same page is used for the send and receive pages. */
1004 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1005 ret = ffa_error(FFA_INVALID_PARAMETERS);
1006 goto out;
1007 }
Andrew Sculle1322792019-07-01 17:46:10 +01001008
1009 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001010 * Ensure the pages are valid, owned and exclusive to the VM and that
1011 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +01001012 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001013 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1014 &orig_send_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001015 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1016 (orig_send_mode & MM_MODE_R) == 0 ||
1017 (orig_send_mode & MM_MODE_W) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001018 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001019 goto out;
1020 }
1021
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001022 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1023 &orig_recv_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001024 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1025 (orig_recv_mode & MM_MODE_R) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001026 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001027 goto out;
1028 }
Andrew Sculle1322792019-07-01 17:46:10 +01001029
1030 /* Take memory ownership away from the VM and mark as shared. */
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001031 uint32_t mode =
1032 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W;
1033 if (vm_locked.vm->el0_partition) {
1034 mode |= MM_MODE_USER | MM_MODE_NG;
1035 }
1036
1037 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, mode,
1038 local_page_pool, NULL)) {
Manish Pandeyd34f8892020-06-19 17:41:07 +01001039 ret = ffa_error(FFA_NO_MEMORY);
1040 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001041 }
1042
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001043 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1044 if (vm_locked.vm->el0_partition) {
1045 mode |= MM_MODE_USER | MM_MODE_NG;
1046 }
1047
1048 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, mode,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001049 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001050 /* TODO: partial defrag of failed range. */
1051 /* Recover any memory consumed in failed mapping. */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001052 vm_ptable_defrag(vm_locked, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001053 goto fail_undo_send;
1054 }
1055
Olivier Deprez96a2a262020-06-11 17:21:38 +02001056 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1057 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1058
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001059 /*
1060 * For EL0 partitions, since both the partition and the hypervisor code
1061 * use the EL2&0 translation regime, it is critical to mark the mappings
1062 * of the send and recv buffers as non-global in the TLB. For one, if we
1063 * dont mark it as non-global, it would cause TLB conflicts since there
1064 * would be an identity mapping with non-global attribute in the
1065 * partitions page tables, but another identity mapping in the
1066 * hypervisor page tables with the global attribute. The other issue is
1067 * one of security, we dont want other partitions to be able to access
1068 * other partitions buffers through cached translations.
1069 */
1070 if (vm_locked.vm->el0_partition) {
1071 extra_attributes |= MM_MODE_NG;
1072 }
1073
Manish Pandeyd34f8892020-06-19 17:41:07 +01001074 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1075 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001076 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001077 goto fail_undo_send_and_recv;
1078 }
1079
Manish Pandeyd34f8892020-06-19 17:41:07 +01001080 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001081 goto out;
1082
Andrew Sculle1322792019-07-01 17:46:10 +01001083fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001084 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001085 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001086
1087fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001088 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001089 orig_send_mode, local_page_pool, NULL));
1090 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001091
1092out:
Andrew Sculle1322792019-07-01 17:46:10 +01001093 return ret;
1094}
1095
1096/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001097 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001098 * must not be shared. Locking of the page tables combined with a local memory
1099 * pool ensures there will always be enough memory to recover from any errors
1100 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1101 * by another core which could result in this transaction being unable to roll
1102 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001103 *
1104 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001105 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001106 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001107 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001108 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001109 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001110 * - FFA_SUCCESS on success if no further action is needed.
1111 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001112 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001113 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001114struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
1115 uint32_t page_count, struct vcpu *current,
1116 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001117{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001118 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001119 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001120 struct vm_locked vm_locked;
1121 struct mm_stage1_locked mm_stage1_locked;
1122 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001123
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001124 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001125 * Create a local pool so any freed memory can't be used by another
1126 * thread. This is to ensure the original mapping can be restored if any
1127 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001128 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001129 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1130
Andrew Sculle1322792019-07-01 17:46:10 +01001131 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001132 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001133
Manish Pandeyd34f8892020-06-19 17:41:07 +01001134 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1135 page_count, &local_page_pool);
1136 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001137 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001138 }
1139
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001140 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001141 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001142
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001143exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001144 mpool_fini(&local_page_pool);
1145
1146 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001147 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001148
1149 return ret;
1150}
1151
1152/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001153 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1154 * translation regime of the caller. Unmap the region for the hypervisor and
1155 * set the memory region to owned and exclusive for the component. Since the
1156 * memory region mapped in the page table, when the buffers were originally
1157 * created we can safely remap it.
1158 *
1159 * Returns:
1160 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1161 * behalf of the caller.
1162 * - FFA_SUCCESS on success if no further action is needed.
1163 */
1164struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1165 struct vcpu *current)
1166{
1167 struct vm *vm = current->vm;
1168 struct vm_locked vm_locked;
1169 struct mm_stage1_locked mm_stage1_locked;
1170 paddr_t send_pa_begin;
1171 paddr_t send_pa_end;
1172 paddr_t recv_pa_begin;
1173 paddr_t recv_pa_end;
1174
1175 /*
1176 * Check there is a buffer pair registered on behalf of the caller.
1177 * Since forwarding is not yet supported the allocator ID MBZ.
1178 */
1179 if (allocator_id != 0) {
1180 dlog_error(
1181 "Forwarding MAP/UNMAP from the hypervisor is not yet "
1182 "supported so vm id must be zero.\n");
1183 return ffa_error(FFA_INVALID_PARAMETERS);
1184 }
1185
1186 /* Get send and receive buffers. */
1187 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001188 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001189 "No buffer pair registered on behalf of the caller.\n");
1190 return ffa_error(FFA_INVALID_PARAMETERS);
1191 }
1192
1193 /* Currently a mailbox size of 1 page is assumed. */
1194 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1195 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1196 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1197 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1198
1199 vm_locked = vm_lock(vm);
1200 mm_stage1_locked = mm_lock_stage1();
1201
1202 /*
1203 * Set the memory region of the buffers back to the default mode
1204 * for the VM. Since this memory region was already mapped for the
1205 * RXTX buffers we can safely remap them.
1206 */
1207 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1208 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1209 NULL));
1210
1211 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1212 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1213 NULL));
1214
1215 /* Unmap the buffers in the partition manager. */
1216 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1217 &api_page_pool));
1218 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1219 &api_page_pool));
1220
1221 vm->mailbox.send = NULL;
1222 vm->mailbox.recv = NULL;
1223
1224 mm_unlock_stage1(&mm_stage1_locked);
1225 vm_unlock(&vm_locked);
1226
1227 return (struct ffa_value){.func = FFA_SUCCESS_32};
1228}
1229
1230/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001231 * Notifies the `to` VM about the message currently in its mailbox, possibly
1232 * with the help of the primary VM.
1233 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001234static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1235 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001236{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001237 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1238 struct ffa_value primary_ret = {
1239 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001240 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001241 };
1242
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001243 /* Messages for the primary VM are delivered directly. */
1244 if (to.vm->id == HF_PRIMARY_VM_ID) {
1245 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001246 * Only tell the primary VM the size and other details if the
1247 * message is for it, to avoid leaking data about messages for
1248 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001249 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001250 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001251
1252 to.vm->mailbox.state = MAILBOX_STATE_READ;
1253 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001254 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001255 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001256 }
1257
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001258 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1259
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001260 /* Messages for the TEE are sent on via the dispatcher. */
1261 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001262 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001263
Olivier Deprez112d2b52020-09-30 07:39:23 +02001264 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001265 /*
1266 * After the call to the TEE completes it must have finished
1267 * reading its RX buffer, so it is ready for another message.
1268 */
1269 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001270 /*
1271 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001272 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001273 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001274 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001275 }
1276
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001277 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001278 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001279 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001280 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001281 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001282
1283 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001284}
1285
1286/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001287 * Copies data from the sender's send buffer to the recipient's receive buffer
1288 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001289 *
1290 * If the recipient's receive buffer is busy, it can optionally register the
1291 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001292 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001293struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1294 ffa_vm_id_t receiver_vm_id, uint32_t size,
1295 uint32_t attributes, struct vcpu *current,
1296 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001297{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001298 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001299 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001300 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001301 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001302 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001303 struct vcpu_locked current_locked;
1304 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001305 bool notify =
1306 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001307
Andrew Walbran70bc8622019-10-07 14:15:58 +01001308 /* Ensure sender VM ID corresponds to the current VM. */
1309 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001310 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001311 }
1312
1313 /* Disallow reflexive requests as this suggests an error in the VM. */
1314 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001315 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001316 }
1317
1318 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001319 if (size > FFA_MSG_PAYLOAD_MAX) {
1320 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001321 }
1322
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001323 /*
1324 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1325 * invocation.
1326 */
1327 current_locked = vcpu_lock(current);
1328 is_direct_request_ongoing =
1329 is_ffa_direct_msg_request_ongoing(current_locked);
1330 vcpu_unlock(&current_locked);
1331
1332 if (is_direct_request_ongoing) {
1333 return ffa_error(FFA_DENIED);
1334 }
1335
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001336 /* Ensure the receiver VM exists. */
1337 to = vm_find(receiver_vm_id);
1338 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001339 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001340 }
1341
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001342 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001343 * Check that the sender has configured its send buffer. If the tx
1344 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1345 * be safely accessed after releasing the lock since the tx mailbox
1346 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001347 */
1348 sl_lock(&from->lock);
1349 from_msg = from->mailbox.send;
1350 sl_unlock(&from->lock);
1351
1352 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001353 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001354 }
1355
Andrew Walbran82d6d152019-12-24 15:02:06 +00001356 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001357
Andrew Walbran82d6d152019-12-24 15:02:06 +00001358 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001359 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001360 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001361 }
1362
Andrew Walbran82d6d152019-12-24 15:02:06 +00001363 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001364 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001365 to->mailbox.recv_size = size;
1366 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001367 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001368 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001369
1370out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001371 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001372
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001373 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001374}
1375
1376/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001377 * Checks whether the vCPU's attempt to block for a message has already been
1378 * interrupted or whether it is allowed to block.
1379 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001380bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001381{
Manish Pandey35e452f2021-02-18 21:36:34 +00001382 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001383 bool interrupted;
1384
Manish Pandey35e452f2021-02-18 21:36:34 +00001385 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001386
1387 /*
1388 * Don't block if there are enabled and pending interrupts, to match
1389 * behaviour of wait_for_interrupt.
1390 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001391 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001392
Manish Pandey35e452f2021-02-18 21:36:34 +00001393 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001394
1395 return interrupted;
1396}
1397
1398/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001399 * Receives a message from the mailbox. If one isn't available, this function
1400 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001401 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001402 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001403 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001404struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1405 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001406{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001407 bool is_direct_request_ongoing;
1408 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001409 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001410 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001411 bool is_from_secure_world =
1412 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001413
Andrew Scullaa039b32018-10-04 15:02:26 +01001414 /*
1415 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001416 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001417 */
J-Alvesb37fd082020-10-22 12:29:21 +01001418 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001419 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001420 }
1421
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001422 /*
1423 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1424 * invocation.
1425 */
1426 current_locked = vcpu_lock(current);
1427 is_direct_request_ongoing =
1428 is_ffa_direct_msg_request_ongoing(current_locked);
1429 vcpu_unlock(&current_locked);
1430
1431 if (is_direct_request_ongoing) {
1432 return ffa_error(FFA_DENIED);
1433 }
1434
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001435 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001436
Andrew Scullaa039b32018-10-04 15:02:26 +01001437 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001438 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1439 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001440 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001441 goto out;
1442 }
1443
1444 /* No pending message so fail if not allowed to block. */
1445 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001447 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001448 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001449
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001450 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001451 * From this point onward this call can only be interrupted or a message
1452 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001453 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001454 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 return_code = ffa_error(FFA_INTERRUPTED);
1456 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001457 goto out;
1458 }
1459
J-Alvesb37fd082020-10-22 12:29:21 +01001460 if (is_from_secure_world) {
1461 /* Return to other world if caller is a SP. */
1462 *next = api_switch_to_other_world(
1463 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001464 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001465 } else {
1466 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001467 struct ffa_value run_return = {
1468 .func = FFA_MSG_WAIT_32,
1469 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001470 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001471
Andrew Walbranb4816552018-12-05 17:35:42 +00001472 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001473 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001474 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001475out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001476 sl_unlock(&vm->lock);
1477
Jose Marinho3e2442f2019-03-12 13:30:37 +00001478 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001479}
1480
1481/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001482 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1483 * by this function, the caller must have called api_mailbox_send before with
1484 * the notify argument set to true, and this call must have failed because the
1485 * mailbox was not available.
1486 *
1487 * It should be called repeatedly to retrieve a list of VMs.
1488 *
1489 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1490 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001491 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001492int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001493{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001494 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001495 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001496 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001497
1498 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001499 if (list_empty(&vm->mailbox.ready_list)) {
1500 ret = -1;
1501 goto exit;
1502 }
1503
1504 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1505 ready_links);
1506 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001507 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001508
1509exit:
1510 sl_unlock(&vm->lock);
1511 return ret;
1512}
1513
1514/**
1515 * Retrieves the next VM waiting to be notified that the mailbox of the
1516 * specified VM became writable. Only primary VMs are allowed to call this.
1517 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001518 * Returns -1 on failure or if there are no waiters; the VM id of the next
1519 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001520 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001521int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001522{
1523 struct vm *vm;
1524 struct vm_locked locked;
1525 struct wait_entry *entry;
1526 struct vm *waiting_vm;
1527
1528 /* Only primary VMs are allowed to call this function. */
1529 if (current->vm->id != HF_PRIMARY_VM_ID) {
1530 return -1;
1531 }
1532
Andrew Walbran42347a92019-05-09 13:59:03 +01001533 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001534 if (vm == NULL) {
1535 return -1;
1536 }
1537
Fuad Tabbaed294af2019-12-20 10:43:01 +00001538 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001539 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001540 entry = api_fetch_waiter(locked);
1541 vm_unlock(&locked);
1542
1543 if (entry == NULL) {
1544 return -1;
1545 }
1546
1547 /* Enqueue notification to waiting VM. */
1548 waiting_vm = entry->waiting_vm;
1549
1550 sl_lock(&waiting_vm->lock);
1551 if (list_empty(&entry->ready_links)) {
1552 list_append(&waiting_vm->mailbox.ready_list,
1553 &entry->ready_links);
1554 }
1555 sl_unlock(&waiting_vm->lock);
1556
1557 return waiting_vm->id;
1558}
1559
1560/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001561 * Releases the caller's mailbox so that a new message can be received. The
1562 * caller must have copied out all data they wish to preserve as new messages
1563 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001564 *
1565 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001566 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1567 * - FFA_SUCCESS on success if no further action is needed.
1568 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001569 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001570 * hf_mailbox_waiter_get.
1571 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001572struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001573{
1574 struct vm *vm = current->vm;
1575 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001576 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001577
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001578 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001579 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001580 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001581 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001582 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001583 break;
1584
Andrew Sculld6ee1102019-04-05 22:12:42 +01001585 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001586 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001587 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001588 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001589 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001590 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001591
1592 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001593}
Andrew Walbran318f5732018-11-20 16:23:42 +00001594
1595/**
1596 * Enables or disables a given interrupt ID for the calling vCPU.
1597 *
1598 * Returns 0 on success, or -1 if the intid is invalid.
1599 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001600int64_t api_interrupt_enable(uint32_t intid, bool enable,
1601 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001602{
Manish Pandey35e452f2021-02-18 21:36:34 +00001603 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001604 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001605 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1606 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001607
Andrew Walbran318f5732018-11-20 16:23:42 +00001608 if (intid >= HF_NUM_INTIDS) {
1609 return -1;
1610 }
1611
Manish Pandey35e452f2021-02-18 21:36:34 +00001612 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001613 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001614 /*
1615 * If it is pending and was not enabled before, increment the
1616 * count.
1617 */
1618 if (current->interrupts.interrupt_pending[intid_index] &
1619 ~current->interrupts.interrupt_enabled[intid_index] &
1620 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001621 if ((current->interrupts.interrupt_type[intid_index] &
1622 intid_mask) ==
1623 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1624 vcpu_irq_count_increment(current_locked);
1625 } else {
1626 vcpu_fiq_count_increment(current_locked);
1627 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001628 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001629 current->interrupts.interrupt_enabled[intid_index] |=
1630 intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001631
1632 if (type == INTERRUPT_TYPE_IRQ) {
1633 current->interrupts.interrupt_type[intid_index] &=
1634 ~intid_mask;
1635 } else if (type == INTERRUPT_TYPE_FIQ) {
1636 current->interrupts.interrupt_type[intid_index] |=
1637 intid_mask;
1638 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001639 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001640 /*
1641 * If it is pending and was enabled before, decrement the count.
1642 */
1643 if (current->interrupts.interrupt_pending[intid_index] &
1644 current->interrupts.interrupt_enabled[intid_index] &
1645 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001646 if ((current->interrupts.interrupt_type[intid_index] &
1647 intid_mask) ==
1648 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1649 vcpu_irq_count_decrement(current_locked);
1650 } else {
1651 vcpu_fiq_count_decrement(current_locked);
1652 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001653 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001654 current->interrupts.interrupt_enabled[intid_index] &=
1655 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001656 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001657 }
1658
Manish Pandey35e452f2021-02-18 21:36:34 +00001659 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001660 return 0;
1661}
1662
1663/**
1664 * Returns the ID of the next pending interrupt for the calling vCPU, and
1665 * acknowledges it (i.e. marks it as no longer pending). Returns
1666 * HF_INVALID_INTID if there are no pending interrupts.
1667 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001668uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001669{
1670 uint8_t i;
1671 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001672 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001673
1674 /*
1675 * Find the first enabled and pending interrupt ID, return it, and
1676 * deactivate it.
1677 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001678 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001679 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1680 uint32_t enabled_and_pending =
1681 current->interrupts.interrupt_enabled[i] &
1682 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001683
Andrew Walbran318f5732018-11-20 16:23:42 +00001684 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001685 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001686 uint32_t intid_mask = 1U << bit_index;
1687
Andrew Walbran3d84a262018-12-13 14:41:19 +00001688 /*
1689 * Mark it as no longer pending and decrement the count.
1690 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001691 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1692
1693 if ((current->interrupts.interrupt_type[i] &
1694 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1695 vcpu_irq_count_decrement(current_locked);
1696 } else {
1697 vcpu_fiq_count_decrement(current_locked);
1698 }
1699
Andrew Walbran3d84a262018-12-13 14:41:19 +00001700 first_interrupt =
1701 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001702 break;
1703 }
1704 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001705
Manish Pandey35e452f2021-02-18 21:36:34 +00001706 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001707 return first_interrupt;
1708}
1709
1710/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001711 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001712 * given VM and vCPU.
1713 */
1714static inline bool is_injection_allowed(uint32_t target_vm_id,
1715 struct vcpu *current)
1716{
1717 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001718
Andrew Walbran318f5732018-11-20 16:23:42 +00001719 /*
1720 * The primary VM is allowed to inject interrupts into any VM. Secondary
1721 * VMs are only allowed to inject interrupts into their own vCPUs.
1722 */
1723 return current_vm_id == HF_PRIMARY_VM_ID ||
1724 current_vm_id == target_vm_id;
1725}
1726
1727/**
1728 * Injects a virtual interrupt of the given ID into the given target vCPU.
1729 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1730 * when the vCPU is next run, which is up to the scheduler.
1731 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001732 * Returns:
1733 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1734 * ID is invalid, or the current VM is not allowed to inject interrupts to
1735 * the target VM.
1736 * - 0 on success if no further action is needed.
1737 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1738 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001739 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001740int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1741 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001742 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001743{
Andrew Walbran318f5732018-11-20 16:23:42 +00001744 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001745 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001746
1747 if (intid >= HF_NUM_INTIDS) {
1748 return -1;
1749 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001750
Andrew Walbran318f5732018-11-20 16:23:42 +00001751 if (target_vm == NULL) {
1752 return -1;
1753 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001754
Andrew Walbran318f5732018-11-20 16:23:42 +00001755 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001756 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001757 return -1;
1758 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001759
Andrew Walbran318f5732018-11-20 16:23:42 +00001760 if (!is_injection_allowed(target_vm_id, current)) {
1761 return -1;
1762 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001763
Andrew Walbrane1310df2019-04-29 17:28:28 +01001764 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001765
Manish Pandey35e452f2021-02-18 21:36:34 +00001766 dlog_verbose(
1767 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1768 "%u\n",
1769 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1770 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001771 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001772}
Andrew Scull6386f252018-12-06 13:29:10 +00001773
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001774/** Returns the version of the implemented FF-A specification. */
1775struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001776{
1777 /*
1778 * Ensure that both major and minor revision representation occupies at
1779 * most 15 bits.
1780 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001781 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001782 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001783 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001784 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001785 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001786 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001787 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001788 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001789
Daniel Boulby6e32c612021-02-17 15:09:41 +00001790 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001791}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001792
1793int64_t api_debug_log(char c, struct vcpu *current)
1794{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001795 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001796 struct vm *vm = current->vm;
1797 struct vm_locked vm_locked = vm_lock(vm);
1798
Andrew Sculld54e1be2019-08-20 11:09:42 +01001799 if (c == '\n' || c == '\0') {
1800 flush = true;
1801 } else {
1802 vm->log_buffer[vm->log_buffer_length++] = c;
1803 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1804 }
1805
1806 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001807 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1808 vm->log_buffer_length);
1809 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001810 }
1811
1812 vm_unlock(&vm_locked);
1813
1814 return 0;
1815}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001816
1817/**
J-Alves6f72ca82021-11-01 12:34:58 +00001818 * Helper for success return of FFA_FEATURES, for when it is used to query
1819 * an interrupt ID.
1820 */
1821struct ffa_value api_ffa_feature_success(uint32_t arg2)
1822{
1823 return (struct ffa_value){
1824 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
1825}
1826
1827/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001828 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001829 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001830 */
J-Alves6f72ca82021-11-01 12:34:58 +00001831struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001832{
J-Alves6f72ca82021-11-01 12:34:58 +00001833 /*
1834 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
1835 * if using a feature ID.
1836 */
1837 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
1838 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0) {
1839 return ffa_error(FFA_NOT_SUPPORTED);
1840 }
1841
1842 switch (feature_function_id) {
1843 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001844 case FFA_ERROR_32:
1845 case FFA_SUCCESS_32:
1846 case FFA_INTERRUPT_32:
1847 case FFA_VERSION_32:
1848 case FFA_FEATURES_32:
1849 case FFA_RX_RELEASE_32:
1850 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001851 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001852 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001853 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001854 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001855 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001856 case FFA_MEM_DONATE_32:
1857 case FFA_MEM_LEND_32:
1858 case FFA_MEM_SHARE_32:
1859 case FFA_MEM_RETRIEVE_REQ_32:
1860 case FFA_MEM_RETRIEVE_RESP_32:
1861 case FFA_MEM_RELINQUISH_32:
1862 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001863 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001864 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001865 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001866 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00001867#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00001868 /* FF-A v1.1 features. */
1869 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00001870 case FFA_NOTIFICATION_BITMAP_CREATE_32:
1871 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
1872 case FFA_NOTIFICATION_BIND_32:
1873 case FFA_NOTIFICATION_UNBIND_32:
1874 case FFA_NOTIFICATION_SET_32:
1875 case FFA_NOTIFICATION_GET_32:
1876 case FFA_NOTIFICATION_INFO_GET_64:
J-Alves3829fc02021-03-18 12:49:18 +00001877#endif
1878 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00001879
1880#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
1881 /* Check support of a feature provided respective feature ID. */
1882 case FFA_FEATURE_NPI:
1883 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
1884 case FFA_FEATURE_SRI:
1885 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
1886#endif
1887 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001888 default:
J-Alves6f72ca82021-11-01 12:34:58 +00001889 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001890 }
1891}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001892
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001893/**
J-Alves645eabe2021-02-22 16:08:27 +00001894 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
1895 * interfaces.
1896 */
1897static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
1898{
1899 return args.arg2 == 0U;
1900}
1901
1902/**
J-Alves76d99af2021-03-10 17:42:11 +00001903 * Limits size of arguments in ffa_value structure to 32-bit.
1904 */
1905static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
1906{
1907 return (struct ffa_value){
1908 .func = (uint32_t)args.func,
1909 .arg1 = (uint32_t)args.arg1,
1910 .arg2 = (uint32_t)0,
1911 .arg3 = (uint32_t)args.arg3,
1912 .arg4 = (uint32_t)args.arg4,
1913 .arg5 = (uint32_t)args.arg5,
1914 .arg6 = (uint32_t)args.arg6,
1915 .arg7 = (uint32_t)args.arg7,
1916 };
1917}
1918
1919/**
1920 * Helper to copy direct message payload, depending on SMC used and expected
1921 * registers size.
1922 */
1923static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
1924{
1925 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
1926 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
1927 return api_ffa_value_copy32(args);
1928 }
1929
1930 return (struct ffa_value){
1931 .func = args.func,
1932 .arg1 = args.arg1,
1933 .arg2 = 0,
1934 .arg3 = args.arg3,
1935 .arg4 = args.arg4,
1936 .arg5 = args.arg5,
1937 .arg6 = args.arg6,
1938 .arg7 = args.arg7,
1939 };
1940}
1941
1942/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001943 * Send an FF-A direct message request.
1944 */
1945struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1946 ffa_vm_id_t receiver_vm_id,
1947 struct ffa_value args,
1948 struct vcpu *current,
1949 struct vcpu **next)
1950{
J-Alves17228f72021-04-20 17:13:19 +01001951 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001952 struct vm *receiver_vm;
1953 struct vcpu *receiver_vcpu;
1954 struct two_vcpu_locked vcpus_locked;
1955
J-Alves645eabe2021-02-22 16:08:27 +00001956 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1957 return ffa_error(FFA_INVALID_PARAMETERS);
1958 }
1959
Olivier Deprez55a189e2021-06-09 15:45:27 +02001960 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
1961 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001962 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001963 }
1964
Olivier Deprez55a189e2021-06-09 15:45:27 +02001965 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01001966 return ret;
1967 }
1968
1969 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
1970
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001971 receiver_vm = vm_find(receiver_vm_id);
1972 if (receiver_vm == NULL) {
1973 return ffa_error(FFA_INVALID_PARAMETERS);
1974 }
1975
1976 /*
1977 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1978 * UP (migratable) or MP partitions with a number of vCPUs matching the
1979 * number of PEs in the system. It further states that MP partitions
1980 * accepting direct request messages cannot migrate.
1981 */
J-Alvesad6a0432021-04-09 16:06:21 +01001982 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001983 if (receiver_vcpu == NULL) {
1984 return ffa_error(FFA_INVALID_PARAMETERS);
1985 }
1986
1987 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1988
1989 /*
1990 * If destination vCPU is executing or already received an
1991 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1992 * busy. There is a brief period of time where the vCPU state has
1993 * changed but regs_available is still false thus consider this case as
1994 * the vCPU not yet ready to receive a direct message request.
1995 */
1996 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1997 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1998 !receiver_vcpu->regs_available) {
1999 ret = ffa_error(FFA_BUSY);
2000 goto out;
2001 }
2002
2003 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2004 memory_order_relaxed)) {
2005 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002006 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002007 receiver_vcpu->vm->id,
2008 vcpu_index(receiver_vcpu));
2009 receiver_vcpu->state = VCPU_STATE_ABORTED;
2010 }
2011
2012 ret = ffa_error(FFA_ABORTED);
2013 goto out;
2014 }
2015
2016 switch (receiver_vcpu->state) {
2017 case VCPU_STATE_OFF:
2018 case VCPU_STATE_RUNNING:
2019 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002020 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002021 case VCPU_STATE_BLOCKED:
2022 case VCPU_STATE_PREEMPTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002023 ret = ffa_error(FFA_BUSY);
2024 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002025 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002026 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002027 * We expect target vCPU to be in WAITING state after either
2028 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002029 */
2030 break;
2031 }
2032
2033 /* Inject timer interrupt if any pending */
2034 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002035 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2036 HF_VIRTUAL_TIMER_INTID, current,
2037 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002038
2039 arch_timer_mask(&receiver_vcpu->regs);
2040 }
2041
2042 /* The receiver vCPU runs upon direct message invocation */
2043 receiver_vcpu->cpu = current->cpu;
2044 receiver_vcpu->state = VCPU_STATE_RUNNING;
2045 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002046 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002047
J-Alves76d99af2021-03-10 17:42:11 +00002048 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002049
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002050 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002051
2052 /* Switch to receiver vCPU targeted to by direct msg request */
2053 *next = receiver_vcpu;
2054
2055 /*
2056 * Since this flow will lead to a VM switch, the return value will not
2057 * be applied to current vCPU.
2058 */
2059
2060out:
2061 sl_unlock(&receiver_vcpu->lock);
2062 sl_unlock(&current->lock);
2063
2064 return ret;
2065}
2066
2067/**
2068 * Send an FF-A direct message response.
2069 */
2070struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2071 ffa_vm_id_t receiver_vm_id,
2072 struct ffa_value args,
2073 struct vcpu *current,
2074 struct vcpu **next)
2075{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002076 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002077
2078 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2079 return ffa_error(FFA_INVALID_PARAMETERS);
2080 }
2081
J-Alves76d99af2021-03-10 17:42:11 +00002082 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002083
Olivier Deprez55a189e2021-06-09 15:45:27 +02002084 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2085 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002086 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002087 }
2088
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002089 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002090 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002091 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002092 * No need for REQ/RESP state management as managed exit does
2093 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002094 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002095 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2096 vcpu_unlock(&current_locked);
2097 return ffa_error(FFA_DENIED);
2098 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002099
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002100 /*
2101 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2102 * a secure interrupt to a SP that is performing managed exit.
2103 * We have taken a implementation defined choice to not allow
2104 * Managed exit while a SP is processing a secure interrupt.
2105 */
2106 CHECK(!current->processing_secure_interrupt);
2107
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002108 plat_interrupts_set_priority_mask(0xff);
2109 current->processing_managed_exit = false;
2110 } else {
2111 /*
2112 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2113 * defined originator.
2114 */
2115 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2116 /*
2117 * Sending direct response but direct request origin
2118 * vCPU is not set.
2119 */
2120 vcpu_unlock(&current_locked);
2121 return ffa_error(FFA_DENIED);
2122 }
2123
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002124 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002125 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2126 vcpu_unlock(&current_locked);
2127 return ffa_error(FFA_DENIED);
2128 }
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002129
2130 /*
2131 * Per FF-A v1.1 Beta0 section 7.4, if a secure interrupt is
2132 * handled by an SP in RUNNING state the existing runtime model
2133 * is preserved. Hence, per section 7.3 bullet 3, SP can use
2134 * FFA_MSG_SEND_DIRECT_RESP to return a response after
2135 * interrupt completion.
2136 */
2137 if (current->processing_secure_interrupt) {
2138 /* There is no preempted vCPU to resume. */
2139 CHECK(current->preempted_vcpu == NULL);
2140
2141 /* Unmask interrupts. */
2142 plat_interrupts_set_priority_mask(0xff);
2143
2144 /*
2145 * Clear fields corresponding to secure interrupt
2146 * handling.
2147 */
2148 current->processing_secure_interrupt = false;
2149 current->secure_interrupt_deactivated = false;
2150 current->current_sec_interrupt_id = 0;
2151 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002152 }
2153
2154 /* Clear direct request origin for the caller. */
2155 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2156
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002157 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002158
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002159 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002160 *next = api_switch_to_other_world(
2161 current, to_ret,
2162 /*
2163 * Current vcpu sent a direct response. It moves to
2164 * waiting state.
2165 */
2166 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002167 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002168 *next = api_switch_to_primary(
2169 current, to_ret,
2170 /*
2171 * Current vcpu sent a direct response. It moves to
2172 * waiting state.
2173 */
2174 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002175 } else if (vm_id_is_current_world(receiver_vm_id)) {
2176 /*
2177 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002178 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002179 * made function return error before getting to this point.
2180 */
2181 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002182 /*
2183 * current vcpu sent a direct response.
2184 * It moves to waiting state.
2185 */
2186 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002187 } else {
2188 panic("Invalid direct message response invocation");
2189 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002190
2191 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2192}
2193
J-Alves84658fc2021-06-17 14:37:32 +01002194static bool api_memory_region_check_flags(
2195 struct ffa_memory_region *memory_region, uint32_t share_func)
2196{
2197 switch (share_func) {
2198 case FFA_MEM_SHARE_32:
2199 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2200 0U) {
2201 return false;
2202 }
2203 /* Intentional fall-through */
2204 case FFA_MEM_LEND_32:
2205 case FFA_MEM_DONATE_32: {
2206 /* Bits 31:2 Must Be Zero. */
2207 ffa_memory_receiver_flags_t to_mask =
2208 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2209 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2210
2211 if ((memory_region->flags & to_mask) != 0U) {
2212 return false;
2213 }
2214 break;
2215 }
2216 default:
2217 panic("Check for mem send calls only.\n");
2218 }
2219
2220 /* Last check reserved values are 0 */
2221 return true;
2222}
2223
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002224struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2225 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002226 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002227{
2228 struct vm *from = current->vm;
2229 struct vm *to;
2230 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002231 struct ffa_memory_region *memory_region;
2232 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002233
2234 if (ipa_addr(address) != 0 || page_count != 0) {
2235 /*
2236 * Hafnium only supports passing the descriptor in the TX
2237 * mailbox.
2238 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002239 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002240 }
2241
Andrew Walbranca808b12020-05-15 17:22:28 +01002242 if (fragment_length > length) {
2243 dlog_verbose(
2244 "Fragment length %d greater than total length %d.\n",
2245 fragment_length, length);
2246 return ffa_error(FFA_INVALID_PARAMETERS);
2247 }
2248 if (fragment_length < sizeof(struct ffa_memory_region) +
2249 sizeof(struct ffa_memory_access)) {
2250 dlog_verbose(
2251 "Initial fragment length %d smaller than header size "
2252 "%d.\n",
2253 fragment_length,
2254 sizeof(struct ffa_memory_region) +
2255 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002256 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002257 }
2258
2259 /*
2260 * Check that the sender has configured its send buffer. If the TX
2261 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2262 * be safely accessed after releasing the lock since the TX mailbox
2263 * address can only be configured once.
2264 */
2265 sl_lock(&from->lock);
2266 from_msg = from->mailbox.send;
2267 sl_unlock(&from->lock);
2268
2269 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002270 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002271 }
2272
2273 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002274 * Copy the memory region descriptor to a fresh page from the memory
2275 * pool. This prevents the sender from changing it underneath us, and
2276 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002277 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002278 if (fragment_length > HF_MAILBOX_SIZE ||
2279 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002280 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002281 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002282 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002283 if (memory_region == NULL) {
2284 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002285 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002286 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002287 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002288
2289 /* The sender must match the caller. */
2290 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002291 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002292 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002293 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002294 }
2295
J-Alves84658fc2021-06-17 14:37:32 +01002296 if (!api_memory_region_check_flags(memory_region, share_func)) {
2297 dlog_verbose(
2298 "Memory region reserved arguments must be zero.\n");
2299 ret = ffa_error(FFA_INVALID_PARAMETERS);
2300 goto out;
2301 }
2302
Andrew Walbrana65a1322020-04-06 19:32:32 +01002303 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002304 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002305 dlog_verbose(
2306 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002307 "endpoint memory access descriptors, expected 1).\n",
2308 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002309 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002310 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002311 }
2312
2313 /*
2314 * Ensure that the receiver VM exists and isn't the same as the sender.
2315 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002316 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002317 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002318 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002319 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002320 goto out;
2321 }
2322
2323 if (to->id == HF_TEE_VM_ID) {
2324 /*
2325 * The 'to' VM lock is only needed in the case that it is the
2326 * TEE VM.
2327 */
2328 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2329
2330 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002331 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002332 goto out_unlock;
2333 }
2334
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002335 ret = ffa_memory_tee_send(
2336 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2337 length, fragment_length, share_func, &api_page_pool);
2338 /*
2339 * ffa_tee_memory_send takes ownership of the memory_region, so
2340 * make sure we don't free it.
2341 */
2342 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002343
2344 out_unlock:
2345 vm_unlock(&vm_to_from_lock.vm1);
2346 vm_unlock(&vm_to_from_lock.vm2);
2347 } else {
2348 struct vm_locked from_locked = vm_lock(from);
2349
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002350 ret = ffa_memory_send(from_locked, memory_region, length,
2351 fragment_length, share_func,
2352 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002353 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002354 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002355 * make sure we don't free it.
2356 */
2357 memory_region = NULL;
2358
2359 vm_unlock(&from_locked);
2360 }
2361
2362out:
2363 if (memory_region != NULL) {
2364 mpool_free(&api_page_pool, memory_region);
2365 }
2366
2367 return ret;
2368}
2369
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002370struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2371 uint32_t fragment_length,
2372 ipaddr_t address, uint32_t page_count,
2373 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002374{
2375 struct vm *to = current->vm;
2376 struct vm_locked to_locked;
2377 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002378 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002379 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002380 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002381
2382 if (ipa_addr(address) != 0 || page_count != 0) {
2383 /*
2384 * Hafnium only supports passing the descriptor in the TX
2385 * mailbox.
2386 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002387 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002388 }
2389
Andrew Walbrana65a1322020-04-06 19:32:32 +01002390 if (fragment_length != length) {
2391 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002392 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002393 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002394
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002395 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002396 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002397 message_buffer_size = cpu_get_buffer_size(current->cpu);
2398 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2399 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002400 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002401 }
2402
2403 to_locked = vm_lock(to);
2404 to_msg = to->mailbox.send;
2405
2406 if (to_msg == NULL) {
2407 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002408 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002409 goto out;
2410 }
2411
2412 /*
2413 * Copy the retrieve request descriptor to an internal buffer, so that
2414 * the caller can't change it underneath us.
2415 */
2416 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2417
2418 if (msg_receiver_busy(to_locked, NULL, false)) {
2419 /*
2420 * Can't retrieve memory information if the mailbox is not
2421 * available.
2422 */
2423 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002424 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002425 goto out;
2426 }
2427
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002428 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2429 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002430
2431out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002432 vm_unlock(&to_locked);
2433 return ret;
2434}
2435
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002436struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002437{
2438 struct vm *from = current->vm;
2439 struct vm_locked from_locked;
2440 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002441 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002442 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002443 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002444 uint32_t length;
2445
2446 from_locked = vm_lock(from);
2447 from_msg = from->mailbox.send;
2448
2449 if (from_msg == NULL) {
2450 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002451 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002452 goto out;
2453 }
2454
2455 /*
2456 * Calculate length from relinquish descriptor before copying. We will
2457 * check again later to make sure it hasn't changed.
2458 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002459 length = sizeof(struct ffa_mem_relinquish) +
2460 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2461 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002462 /*
2463 * Copy the relinquish descriptor to an internal buffer, so that the
2464 * caller can't change it underneath us.
2465 */
2466 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002467 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002468 message_buffer_size = cpu_get_buffer_size(current->cpu);
2469 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2470 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002471 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002472 goto out;
2473 }
2474 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2475
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002476 if (sizeof(struct ffa_mem_relinquish) +
2477 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002478 length) {
2479 dlog_verbose(
2480 "Endpoint count changed while copying to internal "
2481 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002482 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002483 goto out;
2484 }
2485
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002486 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2487 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002488
2489out:
2490 vm_unlock(&from_locked);
2491 return ret;
2492}
2493
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002494struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2495 ffa_memory_region_flags_t flags,
2496 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002497{
2498 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002499 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002500
Olivier Deprez55a189e2021-06-09 15:45:27 +02002501 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002502 struct vm_locked to_locked = vm_lock(to);
2503
Andrew Walbranca808b12020-05-15 17:22:28 +01002504 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002505 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002506
Andrew Walbran290b0c92020-02-03 16:37:14 +00002507 vm_unlock(&to_locked);
2508 } else {
2509 struct vm *from = vm_find(HF_TEE_VM_ID);
2510 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2511
Andrew Walbranca808b12020-05-15 17:22:28 +01002512 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2513 vm_to_from_lock.vm2, handle, flags,
2514 &api_page_pool);
2515
2516 vm_unlock(&vm_to_from_lock.vm1);
2517 vm_unlock(&vm_to_from_lock.vm2);
2518 }
2519
2520 return ret;
2521}
2522
2523struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2524 uint32_t fragment_offset,
2525 ffa_vm_id_t sender_vm_id,
2526 struct vcpu *current)
2527{
2528 struct vm *to = current->vm;
2529 struct vm_locked to_locked;
2530 struct ffa_value ret;
2531
2532 /* Sender ID MBZ at virtual instance. */
2533 if (sender_vm_id != 0) {
2534 return ffa_error(FFA_INVALID_PARAMETERS);
2535 }
2536
2537 to_locked = vm_lock(to);
2538
2539 if (msg_receiver_busy(to_locked, NULL, false)) {
2540 /*
2541 * Can't retrieve memory information if the mailbox is not
2542 * available.
2543 */
2544 dlog_verbose("RX buffer not ready.\n");
2545 ret = ffa_error(FFA_BUSY);
2546 goto out;
2547 }
2548
2549 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2550 &api_page_pool);
2551
2552out:
2553 vm_unlock(&to_locked);
2554 return ret;
2555}
2556
2557struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2558 uint32_t fragment_length,
2559 ffa_vm_id_t sender_vm_id,
2560 struct vcpu *current)
2561{
2562 struct vm *from = current->vm;
2563 const void *from_msg;
2564 void *fragment_copy;
2565 struct ffa_value ret;
2566
2567 /* Sender ID MBZ at virtual instance. */
2568 if (sender_vm_id != 0) {
2569 return ffa_error(FFA_INVALID_PARAMETERS);
2570 }
2571
2572 /*
2573 * Check that the sender has configured its send buffer. If the TX
2574 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2575 * be safely accessed after releasing the lock since the TX mailbox
2576 * address can only be configured once.
2577 */
2578 sl_lock(&from->lock);
2579 from_msg = from->mailbox.send;
2580 sl_unlock(&from->lock);
2581
2582 if (from_msg == NULL) {
2583 return ffa_error(FFA_INVALID_PARAMETERS);
2584 }
2585
2586 /*
2587 * Copy the fragment to a fresh page from the memory pool. This prevents
2588 * the sender from changing it underneath us, and also lets us keep it
2589 * around in the share state table if needed.
2590 */
2591 if (fragment_length > HF_MAILBOX_SIZE ||
2592 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2593 dlog_verbose(
2594 "Fragment length %d larger than mailbox size %d.\n",
2595 fragment_length, HF_MAILBOX_SIZE);
2596 return ffa_error(FFA_INVALID_PARAMETERS);
2597 }
2598 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2599 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2600 0) {
2601 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2602 return ffa_error(FFA_INVALID_PARAMETERS);
2603 }
2604 fragment_copy = mpool_alloc(&api_page_pool);
2605 if (fragment_copy == NULL) {
2606 dlog_verbose("Failed to allocate fragment copy.\n");
2607 return ffa_error(FFA_NO_MEMORY);
2608 }
2609 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2610
2611 /*
2612 * Hafnium doesn't support fragmentation of memory retrieve requests
2613 * (because it doesn't support caller-specified mappings, so a request
2614 * will never be larger than a single page), so this must be part of a
2615 * memory send (i.e. donate, lend or share) request.
2616 *
2617 * We can tell from the handle whether the memory transaction is for the
2618 * TEE or not.
2619 */
2620 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2621 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2622 struct vm_locked from_locked = vm_lock(from);
2623
2624 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2625 fragment_length, handle,
2626 &api_page_pool);
2627 /*
2628 * `ffa_memory_send_continue` takes ownership of the
2629 * fragment_copy, so we don't need to free it here.
2630 */
2631 vm_unlock(&from_locked);
2632 } else {
2633 struct vm *to = vm_find(HF_TEE_VM_ID);
2634 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2635
2636 /*
2637 * The TEE RX buffer state is checked in
2638 * `ffa_memory_tee_send_continue` rather than here, as we need
2639 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2640 * than FFA_ERROR FFA_BUSY in case it is busy.
2641 */
2642
2643 ret = ffa_memory_tee_send_continue(
2644 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2645 fragment_length, handle, &api_page_pool);
2646 /*
2647 * `ffa_memory_tee_send_continue` takes ownership of the
2648 * fragment_copy, so we don't need to free it here.
2649 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002650
2651 vm_unlock(&vm_to_from_lock.vm1);
2652 vm_unlock(&vm_to_from_lock.vm2);
2653 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002654
2655 return ret;
2656}
Max Shvetsov40108e72020-08-27 12:39:50 +01002657
2658struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2659 struct vcpu *current)
2660{
2661 struct vm_locked vm_locked;
2662
2663 vm_locked = vm_lock(current->vm);
2664 vm_locked.vm->secondary_ep = entry_point;
2665 vm_unlock(&vm_locked);
2666
2667 return (struct ffa_value){.func = FFA_SUCCESS_32};
2668}
J-Alvesa0f317d2021-06-09 13:31:59 +01002669
2670struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
2671 ffa_vcpu_count_t vcpu_count,
2672 struct vcpu *current)
2673{
2674 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2675 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
2676 vm_id);
2677 return ffa_error(FFA_NOT_SUPPORTED);
2678 }
2679
2680 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
2681}
2682
2683struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
2684 struct vcpu *current)
2685{
2686 /*
2687 * Validity of use of this interface is the same as for bitmap create.
2688 */
2689 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2690 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
2691 vm_id);
2692 return ffa_error(FFA_NOT_SUPPORTED);
2693 }
2694
2695 return plat_ffa_notifications_bitmap_destroy(vm_id);
2696}
J-Alvesc003a7a2021-03-18 13:06:53 +00002697
2698struct ffa_value api_ffa_notification_update_bindings(
2699 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2700 ffa_notifications_bitmap_t notifications, bool is_bind,
2701 struct vcpu *current)
2702{
2703 struct ffa_value ret = {.func = FFA_SUCCESS_32};
2704 struct vm_locked receiver_locked;
2705 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2706 const ffa_vm_id_t id_to_update =
2707 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
2708 const ffa_vm_id_t id_to_validate =
2709 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
2710
2711 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
2712 receiver_vm_id)) {
2713 dlog_verbose("Invalid use of notifications bind interface.\n");
2714 return ffa_error(FFA_INVALID_PARAMETERS);
2715 }
2716
J-Alvesb15e9402021-09-08 11:44:42 +01002717 if (plat_ffa_notifications_update_bindings_forward(
2718 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
2719 &ret)) {
2720 dlog_verbose("Forwarding call to other world.\n");
2721 return ret;
2722 }
2723
J-Alvesc003a7a2021-03-18 13:06:53 +00002724 if (notifications == 0U) {
2725 dlog_verbose("No notifications have been specified.\n");
2726 return ffa_error(FFA_INVALID_PARAMETERS);
2727 }
2728
2729 /**
2730 * This check assumes receiver is the current VM, and has been enforced
2731 * by 'plat_ffa_is_notifications_bind_valid'.
2732 */
2733 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2734
2735 if (receiver_locked.vm == NULL) {
2736 dlog_verbose("Receiver doesn't exist!\n");
2737 return ffa_error(FFA_DENIED);
2738 }
2739
J-Alves09ff9d82021-11-02 11:55:20 +00002740 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00002741 dlog_verbose("Notifications are not enabled.\n");
2742 ret = ffa_error(FFA_NOT_SUPPORTED);
2743 goto out;
2744 }
2745
2746 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
2747 vm_find(sender_vm_id) == NULL) {
2748 dlog_verbose("Sender VM does not exist!\n");
2749 ret = ffa_error(FFA_INVALID_PARAMETERS);
2750 goto out;
2751 }
2752
2753 /*
2754 * Can't bind/unbind notifications if at least one is bound to a
2755 * different sender.
2756 */
2757 if (!vm_notifications_validate_bound_sender(
2758 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2759 id_to_validate, notifications)) {
2760 dlog_verbose("Notifications are bound to other sender.\n");
2761 ret = ffa_error(FFA_DENIED);
2762 goto out;
2763 }
2764
2765 /**
2766 * Check if there is a pending notification within those specified in
2767 * the bitmap.
2768 */
2769 if (vm_are_notifications_pending(receiver_locked,
2770 plat_ffa_is_vm_id(sender_vm_id),
2771 notifications)) {
2772 dlog_verbose("Notifications within '%x' pending.\n",
2773 notifications);
2774 ret = ffa_error(FFA_DENIED);
2775 goto out;
2776 }
2777
2778 vm_notifications_update_bindings(
2779 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
2780 notifications, is_per_vcpu && is_bind);
2781
2782out:
2783 vm_unlock(&receiver_locked);
2784 return ret;
2785}
J-Alvesaa79c012021-07-09 14:29:45 +01002786
2787struct ffa_value api_ffa_notification_set(
2788 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2789 ffa_notifications_bitmap_t notifications, struct vcpu *current)
2790{
2791 struct ffa_value ret;
2792 struct vm_locked receiver_locked;
2793
2794 /*
2795 * Check if is per-vCPU or global, and extracting vCPU ID according
2796 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
2797 */
2798 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2799 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
2800
J-Alvesaa79c012021-07-09 14:29:45 +01002801 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
2802 receiver_vm_id)) {
2803 dlog_verbose("Invalid use of notifications set interface.\n");
2804 return ffa_error(FFA_INVALID_PARAMETERS);
2805 }
2806
2807 if (notifications == 0U) {
2808 dlog_verbose("No notifications have been specified.\n");
2809 return ffa_error(FFA_INVALID_PARAMETERS);
2810 }
2811
J-Alvesde7bd2f2021-09-09 19:54:35 +01002812 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
2813 flags, notifications, &ret)) {
2814 return ret;
2815 }
2816
J-Alvesaa79c012021-07-09 14:29:45 +01002817 /*
2818 * This check assumes receiver is the current VM, and has been enforced
2819 * by 'plat_ffa_is_notification_set_valid'.
2820 */
2821 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2822
2823 if (receiver_locked.vm == NULL) {
2824 dlog_verbose("Receiver ID is not valid.\n");
2825 return ffa_error(FFA_INVALID_PARAMETERS);
2826 }
2827
J-Alves09ff9d82021-11-02 11:55:20 +00002828 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01002829 dlog_verbose("Receiver's notifications not enabled.\n");
2830 ret = ffa_error(FFA_DENIED);
2831 goto out;
2832 }
2833
2834 /*
2835 * If notifications are not bound to the sender, they wouldn't be
2836 * enabled either for the receiver.
2837 */
2838 if (!vm_notifications_validate_binding(
2839 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2840 sender_vm_id, notifications, is_per_vcpu)) {
2841 dlog_verbose("Notifications bindings not valid.\n");
2842 ret = ffa_error(FFA_DENIED);
2843 goto out;
2844 }
2845
2846 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
2847 dlog_verbose("Invalid VCPU ID!\n");
2848 ret = ffa_error(FFA_INVALID_PARAMETERS);
2849 goto out;
2850 }
2851
J-Alves7461ef22021-10-18 17:21:33 +01002852 /* Set notifications pending. */
J-Alvesaa79c012021-07-09 14:29:45 +01002853 vm_notifications_set(receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2854 notifications, vcpu_id, is_per_vcpu);
2855 dlog_verbose("Set the notifications: %x.\n", notifications);
2856
J-Alves13394022021-06-30 13:48:49 +01002857 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
2858 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
2859 vcpu_index(current));
2860 plat_ffa_sri_trigger_not_delayed(current->cpu);
2861 } else {
2862 plat_ffa_sri_state_set(DELAYED);
2863 }
J-Alvesaa79c012021-07-09 14:29:45 +01002864
J-Alves7461ef22021-10-18 17:21:33 +01002865 /*
2866 * If notifications set are per-vCPU and the receiver is SP, the
2867 * Notifications Pending Interrupt can be injected now.
2868 * If not, it should be injected when the scheduler gives it CPU cycles
2869 * in a specific vCPU.
2870 */
2871 if (is_per_vcpu && vm_id_is_current_world(receiver_vm_id)) {
2872 struct vcpu *target_vcpu =
2873 vm_get_vcpu(receiver_locked.vm, vcpu_id);
2874
2875 dlog_verbose("Per-vCPU notification, pending NPI.\n");
J-Alves6f72ca82021-11-01 12:34:58 +00002876 internal_interrupt_inject(target_vcpu,
2877 HF_NOTIFICATION_PENDING_INTID,
2878 current, NULL);
J-Alves7461ef22021-10-18 17:21:33 +01002879 }
2880
J-Alves13394022021-06-30 13:48:49 +01002881 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01002882out:
2883 vm_unlock(&receiver_locked);
2884
2885 return ret;
2886}
2887
2888static struct ffa_value api_ffa_notification_get_success_return(
2889 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
2890 ffa_notifications_bitmap_t from_framework)
2891{
2892 return (struct ffa_value){
2893 .func = FFA_SUCCESS_32,
2894 .arg1 = 0U,
2895 .arg2 = (uint32_t)from_sp,
2896 .arg3 = (uint32_t)(from_sp >> 32),
2897 .arg4 = (uint32_t)from_vm,
2898 .arg5 = (uint32_t)(from_vm >> 32),
2899 .arg6 = (uint32_t)from_framework,
2900 .arg7 = (uint32_t)(from_framework >> 32),
2901 };
2902}
2903
2904struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
2905 ffa_vcpu_index_t vcpu_id,
2906 uint32_t flags, struct vcpu *current)
2907{
2908 /* TODO: get framework notifications, when these are supported. */
2909 ffa_notifications_bitmap_t sp_notifications = 0;
2910 ffa_notifications_bitmap_t vm_notifications = 0;
2911 struct vm_locked receiver_locked;
2912 struct ffa_value ret;
2913
2914 /*
2915 * Following check should capture wrong uses of the interface, depending
2916 * on whether Hafnium is SPMC or hypervisor.
2917 * On the rest of the function it is assumed this condition is met.
2918 */
2919 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id)) {
2920 dlog_verbose("Invalid use of notifications get interface.\n");
2921 return ffa_error(FFA_INVALID_PARAMETERS);
2922 }
2923
2924 /*
2925 * This check assumes receiver is the current VM, and has been enforced
2926 * by `plat_ffa_is_notifications_get_valid`.
2927 */
2928 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2929
2930 /*
2931 * `plat_ffa_is_notifications_get_valid` ensures following is never
2932 * true.
2933 */
2934 CHECK(receiver_locked.vm != NULL);
2935
2936 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
2937 (receiver_locked.vm->vcpu_count != 1 &&
2938 cpu_index(current->cpu) != vcpu_id)) {
2939 dlog_verbose("Invalid VCPU ID!\n");
2940 ret = ffa_error(FFA_INVALID_PARAMETERS);
2941 goto out;
2942 }
2943
2944 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01002945 if (!plat_ffa_notifications_get_from_sp(
2946 receiver_locked, vcpu_id, &sp_notifications,
2947 &ret)) {
2948 dlog_verbose("Failed to get notifications from sps.");
2949 goto out;
2950 }
J-Alvesaa79c012021-07-09 14:29:45 +01002951 }
2952
2953 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
2954 vm_notifications = vm_notifications_get_pending_and_clear(
2955 receiver_locked, true, vcpu_id);
2956 }
2957
2958 ret = api_ffa_notification_get_success_return(sp_notifications,
2959 vm_notifications, 0);
2960
J-Alvesfe23ebe2021-10-13 16:07:07 +01002961 /*
2962 * If there are no more pending notifications, change `sri_state` to
2963 * handled.
2964 */
2965 if (vm_is_notifications_pending_count_zero()) {
2966 plat_ffa_sri_state_set(HANDLED);
2967 }
2968
J-Alvesaa79c012021-07-09 14:29:45 +01002969out:
2970 vm_unlock(&receiver_locked);
2971
2972 return ret;
2973}
J-Alvesc8e8a222021-06-08 17:33:52 +01002974
2975/**
2976 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
2977 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
2978 */
2979static struct ffa_value api_ffa_notification_info_get_success_return(
2980 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01002981 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01002982{
2983 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
2984
2985 /*
2986 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
2987 * hold the list of ids.
2988 */
2989 memcpy_s(&ret.arg3,
2990 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
2991 sizeof(ids[0]) * ids_count);
2992
2993 /*
2994 * According to the spec x2 should have:
2995 * - Bit flagging if there are more notifications pending;
2996 * - The total number of elements (i.e. total list size);
2997 * - The number of VCPU IDs within each VM specific list.
2998 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01002999 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3000 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3001 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003002
3003 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3004 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3005
3006 for (unsigned int i = 0; i < lists_count; i++) {
3007 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3008 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3009 }
3010
3011 return ret;
3012}
3013
3014struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3015{
3016 /*
3017 * Following set of variables should be populated with the return info.
3018 * At a successfull handling of this interface, they should be used
3019 * to populate the 'ret' structure in accordance to the table 17.29
3020 * of the FF-A v1.1 Beta0 specification.
3021 */
3022 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3023 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3024 uint32_t lists_count = 0;
3025 uint32_t ids_count = 0;
3026 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003027 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003028
3029 /*
3030 * This interface can only be called at NS virtual/physical FF-A
3031 * instance by the endpoint implementing the primary scheduler and the
3032 * Hypervisor/OS kernel.
3033 * In the SPM, following check passes if call has been forwarded from
3034 * the hypervisor.
3035 */
3036 if (current->vm->id != HF_PRIMARY_VM_ID) {
3037 dlog_verbose(
3038 "Only the receiver's scheduler can use this "
3039 "interface\n");
3040 return ffa_error(FFA_NOT_SUPPORTED);
3041 }
3042
J-Alvesca058c22021-09-10 14:02:07 +01003043 /*
3044 * Forward call to the other world, and fill the arrays used to assemble
3045 * return.
3046 */
3047 plat_ffa_notification_info_get_forward(
3048 ids, &ids_count, lists_sizes, &lists_count,
3049 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3050
3051 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3052
J-Alvesc8e8a222021-06-08 17:33:52 +01003053 /* Get notifications' info from this world */
3054 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3055 ++index) {
3056 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3057
3058 list_is_full = vm_notifications_info_get(
3059 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3060 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3061
3062 vm_unlock(&vm_locked);
3063 }
3064
3065 if (!list_is_full) {
3066 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003067 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003068 ids, &ids_count, lists_sizes, &lists_count,
3069 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3070 }
3071
3072 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003073 dlog_verbose(
3074 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003075 result = ffa_error(FFA_NO_DATA);
3076 } else {
3077 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003078 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003079 }
3080
J-Alvesfe23ebe2021-10-13 16:07:07 +01003081 plat_ffa_sri_state_set(HANDLED);
3082
J-Alves13394022021-06-30 13:48:49 +01003083 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003084}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003085
3086struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3087{
3088 struct vm_locked vm_locked;
3089 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3090 bool mode_ret = false;
3091 uint32_t mode = 0;
3092
3093 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3094 return ffa_error(FFA_NOT_SUPPORTED);
3095 }
3096
3097 if (!(current->vm->el0_partition)) {
3098 return ffa_error(FFA_DENIED);
3099 }
3100
3101 vm_locked = vm_lock(current->vm);
3102
3103 /*
3104 * mm_get_mode is used to check if the given base_addr page is already
3105 * mapped. If the page is unmapped, return error. If the page is mapped
3106 * appropriate attributes are returned to the caller. Note that
3107 * mm_get_mode returns true if the address is in the valid VA range as
3108 * supported by the architecture and MMU configurations, as opposed to
3109 * whether a page is mapped or not. For a page to be known as mapped,
3110 * the API must return true AND the returned mode must not have
3111 * MM_MODE_INVALID set.
3112 */
3113 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3114 va_add(base_addr, PAGE_SIZE), &mode);
3115 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3116 ret = ffa_error(FFA_INVALID_PARAMETERS);
3117 goto out;
3118 }
3119
3120 /* No memory should be marked RWX */
3121 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3122 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3123
3124 /*
3125 * S-EL0 partitions are expected to have all their pages marked as
3126 * non-global.
3127 */
3128 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3129 (MM_MODE_NG | MM_MODE_USER));
3130
3131 if (mode & MM_MODE_W) {
3132 /* No memory should be writeable but not readable. */
3133 CHECK(mode & MM_MODE_R);
3134 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3135 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3136 } else if (mode & MM_MODE_R) {
3137 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3138 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3139 if (!(mode & MM_MODE_X)) {
3140 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3141 }
3142 }
3143out:
3144 vm_unlock(&vm_locked);
3145 return ret;
3146}
3147
3148struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3149 uint32_t mem_perm, struct vcpu *current)
3150{
3151 struct vm_locked vm_locked;
3152 struct ffa_value ret;
3153 bool mode_ret = false;
3154 uint32_t original_mode;
3155 uint32_t new_mode;
3156 struct mpool local_page_pool;
3157
3158 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3159 return ffa_error(FFA_NOT_SUPPORTED);
3160 }
3161
3162 if (!(current->vm->el0_partition)) {
3163 return ffa_error(FFA_DENIED);
3164 }
3165
3166 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3167 return ffa_error(FFA_INVALID_PARAMETERS);
3168 }
3169
3170 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3171 (mem_perm != FFA_MEM_PERM_RX)) {
3172 return ffa_error(FFA_INVALID_PARAMETERS);
3173 }
3174
3175 /*
3176 * Create a local pool so any freed memory can't be used by another
3177 * thread. This is to ensure the original mapping can be restored if any
3178 * stage of the process fails.
3179 */
3180 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3181
3182 vm_locked = vm_lock(current->vm);
3183
3184 /*
3185 * All regions accessible by the partition are mapped during boot. If we
3186 * cannot get a successful translation for the page range, the request
3187 * to change permissions is rejected.
3188 * mm_get_mode is used to check if the given address range is already
3189 * mapped. If the range is unmapped, return error. If the range is
3190 * mapped appropriate attributes are returned to the caller. Note that
3191 * mm_get_mode returns true if the address is in the valid VA range as
3192 * supported by the architecture and MMU configurations, as opposed to
3193 * whether a page is mapped or not. For a page to be known as mapped,
3194 * the API must return true AND the returned mode must not have
3195 * MM_MODE_INVALID set.
3196 */
3197
3198 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3199 va_add(base_addr, page_count * PAGE_SIZE),
3200 &original_mode);
3201 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3202 ret = ffa_error(FFA_INVALID_PARAMETERS);
3203 goto out;
3204 }
3205
3206 /* Device memory cannot be marked as executable */
3207 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3208 ret = ffa_error(FFA_INVALID_PARAMETERS);
3209 goto out;
3210 }
3211
3212 new_mode = MM_MODE_USER | MM_MODE_NG;
3213
3214 if (mem_perm == FFA_MEM_PERM_RW) {
3215 new_mode |= MM_MODE_R | MM_MODE_W;
3216 } else if (mem_perm == FFA_MEM_PERM_RX) {
3217 new_mode |= MM_MODE_R | MM_MODE_X;
3218 } else if (mem_perm == FFA_MEM_PERM_RO) {
3219 new_mode |= MM_MODE_R;
3220 }
3221
3222 /*
3223 * Safe to re-map memory, since we know the requested permissions are
3224 * valid, and the memory requested to be re-mapped is also valid.
3225 */
3226 if (!mm_identity_prepare(
3227 &vm_locked.vm->ptable, pa_from_va(base_addr),
3228 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3229 new_mode, &local_page_pool)) {
3230 /*
3231 * Defrag the table into the local page pool.
3232 * mm_identity_prepare could have allocated or freed pages to
3233 * split blocks or tables etc.
3234 */
3235 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3236
3237 /*
3238 * Guaranteed to succeed mapping with old mode since the mapping
3239 * with old mode already existed and we have a local page pool
3240 * that should have sufficient memory to go back to the original
3241 * state.
3242 */
3243 CHECK(mm_identity_prepare(
3244 &vm_locked.vm->ptable, pa_from_va(base_addr),
3245 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3246 original_mode, &local_page_pool));
3247 mm_identity_commit(
3248 &vm_locked.vm->ptable, pa_from_va(base_addr),
3249 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3250 original_mode, &local_page_pool);
3251
3252 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3253 ret = ffa_error(FFA_NO_MEMORY);
3254 goto out;
3255 }
3256
3257 mm_identity_commit(
3258 &vm_locked.vm->ptable, pa_from_va(base_addr),
3259 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3260 &local_page_pool);
3261
3262 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3263
3264out:
3265 mpool_fini(&local_page_pool);
3266 vm_unlock(&vm_locked);
3267
3268 return ret;
3269}