blob: 41fadc700d33a18a7b47f9569ead1eb0e3e25242 [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 */
74static struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
75{
76 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
77 struct vcpu *vcpu = NULL;
78
79 if (vm->vcpu_count == 1) {
80 vcpu = vm_get_vcpu(vm, 0);
81 } else if (current_cpu_index < vm->vcpu_count) {
82 vcpu = vm_get_vcpu(vm, current_cpu_index);
83 }
84
85 return vcpu;
86}
87
88/**
J-Alvesfe7f7372020-11-09 11:32:12 +000089 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
90 * is given as argument of the function.
91 *
92 * Called to change the context between SPs for direct messaging (when Hafnium
93 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
94 *
95 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +010096 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +000097 * - MP with pinned Execution Contexts.
98 */
99static struct vcpu *api_switch_to_vm(struct vcpu *current,
100 struct ffa_value to_ret,
101 enum vcpu_state vcpu_state,
102 ffa_vm_id_t to_id)
103{
104 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100105 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000106
107 CHECK(next != NULL);
108
109 /* Set the return value for the target VM. */
110 arch_regs_set_retval(&next->regs, to_ret);
111
112 /* Set the current vCPU state. */
113 sl_lock(&current->lock);
114 current->state = vcpu_state;
115 sl_unlock(&current->lock);
116
117 return next;
118}
119
120/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000121 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100122 *
123 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100124 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100125 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100126static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100127 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000128 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100129{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000130 /*
131 * If the secondary is blocked but has a timer running, sleep until the
132 * timer fires rather than indefinitely.
133 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100134 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100135 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
136 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100137 if (arch_timer_enabled_current()) {
138 uint64_t remaining_ns =
139 arch_timer_remaining_ns_current();
140
141 if (remaining_ns == 0) {
142 /*
143 * Timer is pending, so the current vCPU should
144 * be run again right away.
145 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100146 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100147 /*
148 * primary_ret.arg1 should already be set to the
149 * current VM ID and vCPU ID.
150 */
151 primary_ret.arg2 = 0;
152 } else {
153 primary_ret.arg2 = remaining_ns;
154 }
155 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100156 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100157 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000158 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100159 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000160
161 default:
162 /* Do nothing. */
163 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000164 }
165
J-Alvesfe7f7372020-11-09 11:32:12 +0000166 return api_switch_to_vm(current, primary_ret, secondary_state,
167 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100168}
169
170/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200171 * Choose next vCPU to run to be the counterpart vCPU in the other
172 * world (run the normal world if currently running in the secure
173 * world). Set current vCPU state to the given vcpu_state parameter.
174 * Set FF-A return values to the target vCPU in the other world.
175 *
176 * Called in context of a direct message response from a secure
177 * partition to a VM.
178 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100179struct vcpu *api_switch_to_other_world(struct vcpu *current,
180 struct ffa_value other_world_ret,
181 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200182{
J-Alvesfe7f7372020-11-09 11:32:12 +0000183 return api_switch_to_vm(current, other_world_ret, vcpu_state,
184 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200185}
186
187/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100188 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
189 * registers the `from` VM to be notified when it becomes available.
190 */
191static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
192{
193 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
194 to.vm->mailbox.recv == NULL) {
195 /*
196 * Fail if the receiver isn't currently ready to receive data,
197 * setting up for notification if requested.
198 */
199 if (notify) {
200 struct wait_entry *entry =
201 vm_get_wait_entry(from, to.vm->id);
202
203 /* Append waiter only if it's not there yet. */
204 if (list_empty(&entry->wait_links)) {
205 list_append(&to.vm->mailbox.waiter_list,
206 &entry->wait_links);
207 }
208 }
209
210 return true;
211 }
212
213 return false;
214}
215
216/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000217 * Returns true if the given vCPU is executing in context of an
218 * FFA_MSG_SEND_DIRECT_REQ invocation.
219 */
220static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
221{
222 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
223}
224
225/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100226 * Returns true if the VM owning the given vCPU is supporting managed exit and
227 * the vCPU is currently processing a managed exit.
228 */
229static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
230{
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100231 return (vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100232 vcpu_locked.vcpu->processing_managed_exit);
233}
234
235/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000236 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000237 */
238struct vcpu *api_preempt(struct vcpu *current)
239{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100240 struct ffa_value ret = {
241 .func = FFA_INTERRUPT_32,
242 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000243 };
244
Andrew Sculld6ee1102019-04-05 22:12:42 +0100245 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000246}
247
248/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000249 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000250 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100251 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100252struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100253{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100254 struct ffa_value ret = {
255 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
256 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100257 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000258
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000259 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100260 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100261}
262
263/**
Andrew Walbran33645652019-04-15 12:29:31 +0100264 * Puts the current vCPU in off mode, and returns to the primary VM.
265 */
266struct vcpu *api_vcpu_off(struct vcpu *current)
267{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100268 struct ffa_value ret = {
269 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
270 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100271 };
272
273 /*
274 * Disable the timer, so the scheduler doesn't get told to call back
275 * based on it.
276 */
277 arch_timer_disable_current();
278
279 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
280}
281
282/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000283 * Returns to the primary VM to allow this CPU to be used for other tasks as the
284 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100285 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000286 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000287struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000288{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000289 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
290 struct vcpu_locked current_locked;
291 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000292
293 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000294 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000295 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000296 }
297
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000298 current_locked = vcpu_lock(current);
299 is_direct_request_ongoing =
300 is_ffa_direct_msg_request_ongoing(current_locked);
301 vcpu_unlock(&current_locked);
302
303 if (is_direct_request_ongoing) {
304 return ffa_error(FFA_DENIED);
305 }
306
307 *next = api_switch_to_primary(
308 current,
309 (struct ffa_value){.func = FFA_YIELD_32,
310 .arg1 = ffa_vm_vcpu(current->vm->id,
311 vcpu_index(current))},
312 VCPU_STATE_READY);
313
314 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000315}
316
317/**
Andrew Walbran33645652019-04-15 12:29:31 +0100318 * Switches to the primary so that it can switch to the target, or kick it if it
319 * is already running on a different physical CPU.
320 */
321struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
322{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100323 struct ffa_value ret = {
324 .func = HF_FFA_RUN_WAKE_UP,
325 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
326 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100327 };
328 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
329}
330
331/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000332 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000333 */
334struct vcpu *api_abort(struct vcpu *current)
335{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000337
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100338 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000339 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000340
341 if (current->vm->id == HF_PRIMARY_VM_ID) {
342 /* TODO: what to do when the primary aborts? */
343 for (;;) {
344 /* Do nothing. */
345 }
346 }
347
348 atomic_store_explicit(&current->vm->aborting, true,
349 memory_order_relaxed);
350
351 /* TODO: free resources once all vCPUs abort. */
352
Andrew Sculld6ee1102019-04-05 22:12:42 +0100353 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000354}
355
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100356struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
357 const struct ffa_uuid *uuid)
358{
359 struct vm *current_vm = current->vm;
360 struct vm_locked current_vm_locked;
361 ffa_vm_count_t vm_count = 0;
362 bool uuid_is_null = ffa_uuid_is_null(uuid);
363 struct ffa_value ret;
364 uint32_t size;
365 struct ffa_partition_info partitions[MAX_VMS];
366
367 /*
368 * Iterate through the VMs to find the ones with a matching UUID.
369 * A Null UUID retrieves information for all VMs.
370 */
371 for (uint16_t index = 0; index < vm_get_count(); ++index) {
372 const struct vm *vm = vm_find_index(index);
373
374 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
375 partitions[vm_count].vm_id = vm->id;
376 partitions[vm_count].vcpu_count = vm->vcpu_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100377 partitions[vm_count].properties =
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100378 plat_ffa_partition_properties(current_vm->id,
379 vm);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100380
381 ++vm_count;
382 }
383 }
384
385 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
386 if (vm_count == 0) {
387 return ffa_error(FFA_INVALID_PARAMETERS);
388 }
389
390 size = vm_count * sizeof(partitions[0]);
391 if (size > FFA_MSG_PAYLOAD_MAX) {
392 dlog_error(
393 "Partition information does not fit in the VM's RX "
394 "buffer.\n");
395 return ffa_error(FFA_NO_MEMORY);
396 }
397
398 /*
399 * Partition information is returned in the VM's RX buffer, which is why
400 * the lock is needed.
401 */
402 current_vm_locked = vm_lock(current_vm);
403
404 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
405 /*
406 * Can't retrieve memory information if the mailbox is not
407 * available.
408 */
409 dlog_verbose("RX buffer not ready.\n");
410 ret = ffa_error(FFA_BUSY);
411 goto out_unlock;
412 }
413
414 /* Populate the VM's RX buffer with the partition information. */
415 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
416 size);
417 current_vm->mailbox.recv_size = size;
418 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
419 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
420 current_vm->mailbox.state = MAILBOX_STATE_READ;
421
422 /* Return the count of partition information descriptors in w2. */
423 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
424
425out_unlock:
426 vm_unlock(&current_vm_locked);
427
428 return ret;
429}
430
Andrew Scull9726c252019-01-23 13:44:19 +0000431/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000432 * Returns the ID of the VM.
433 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100434struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000435{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100436 return (struct ffa_value){.func = FFA_SUCCESS_32,
437 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000438}
439
440/**
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000441 * Returns the ID of the SPMC.
442 */
443struct ffa_value api_ffa_spm_id_get(void)
444{
445 struct ffa_value ret = ffa_error(FFA_NOT_SUPPORTED);
446
447 if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED) {
448 /*
449 * Return the SPMC ID that was fetched during FF-A
450 * initialization.
451 */
452 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
453 .arg2 = arch_ffa_spmc_id_get()};
454 }
455 return ret;
456}
457
458/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000459 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000460 * function to indicate that register state for the given vCPU has been saved
461 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000462 */
463void api_regs_state_saved(struct vcpu *vcpu)
464{
465 sl_lock(&vcpu->lock);
466 vcpu->regs_available = true;
467 sl_unlock(&vcpu->lock);
468}
469
470/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000471 * Retrieves the next waiter and removes it from the wait list if the VM's
472 * mailbox is in a writable state.
473 */
474static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
475{
476 struct wait_entry *entry;
477 struct vm *vm = locked_vm.vm;
478
Andrew Sculld6ee1102019-04-05 22:12:42 +0100479 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000480 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
481 /* The mailbox is not writable or there are no waiters. */
482 return NULL;
483 }
484
485 /* Remove waiter from the wait list. */
486 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
487 wait_links);
488 list_remove(&entry->wait_links);
489 return entry;
490}
491
492/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000493 * Assuming that the arguments have already been checked by the caller, injects
494 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
495 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
496 * is next run, which is up to the scheduler.
497 *
498 * Returns:
499 * - 0 on success if no further action is needed.
500 * - 1 if it was called by the primary VM and the primary VM now needs to wake
501 * up or kick the target vCPU.
502 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100503int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
504 uint32_t intid, struct vcpu *current,
505 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000506{
Manish Pandey35e452f2021-02-18 21:36:34 +0000507 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000508 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000509 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
510 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000511 int64_t ret = 0;
512
Andrew Walbran508e63c2018-12-20 17:02:37 +0000513 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000514 * We only need to change state and (maybe) trigger a virtual interrupt
515 * if it is enabled and was not previously pending. Otherwise we can
516 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000517 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000518 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
519 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000520 intid_mask)) {
521 goto out;
522 }
523
524 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000525 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
526 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
527 vcpu_irq_count_increment(target_locked);
528 } else {
529 vcpu_fiq_count_increment(target_locked);
530 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000531
532 /*
533 * Only need to update state if there was not already an
534 * interrupt enabled and pending.
535 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000536 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000537 goto out;
538 }
539
Andrew Walbran508e63c2018-12-20 17:02:37 +0000540 if (current->vm->id == HF_PRIMARY_VM_ID) {
541 /*
542 * If the call came from the primary VM, let it know that it
543 * should run or kick the target vCPU.
544 */
545 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000546 } else if (current != target_vcpu && next != NULL) {
547 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000548 }
549
550out:
551 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000552 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000553
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200554 return ret;
555}
556
557/* Wrapper to internal_interrupt_inject with locking of target vCPU */
558static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
559 uint32_t intid, struct vcpu *current,
560 struct vcpu **next)
561{
562 int64_t ret;
563 struct vcpu_locked target_locked;
564
565 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100566 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200567 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000568
569 return ret;
570}
571
572/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100573 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
574 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100575 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100576static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100577{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000578 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100579 case FFA_MSG_SEND_32:
580 return (struct ffa_value){
581 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000582 .arg1 = (receiver->mailbox.recv_sender << 16) |
583 receiver->id,
584 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000585 default:
586 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000587 dlog_error("Tried to return an invalid message function %#x\n",
588 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100589 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000590 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100591}
592
593/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000594 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000595 * value needs to be forced onto the vCPU.
596 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000597static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100598 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000599{
Max Shvetsov40108e72020-08-27 12:39:50 +0100600 struct vcpu_locked vcpu_locked;
601 struct vm_locked vm_locked;
Andrew Scullb06d1752019-02-04 10:15:48 +0000602 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000603 bool ret;
604
Andrew Scullb06d1752019-02-04 10:15:48 +0000605 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000606 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000607 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100608 * The VM lock is not needed in the common case so it must only be taken
609 * when it is going to be needed. This ensures there are no inter-vCPU
610 * dependencies in the common run case meaning the sensitive context
611 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000612 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100613 vcpu_locked = vcpu_lock(vcpu);
614
615#if SECURE_WORLD == 1
616
617 if (vcpu_secondary_reset_and_start(vcpu_locked, vcpu->vm->secondary_ep,
618 0)) {
619 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
620 __func__, vcpu->vm->id, current->cpu->id);
621 }
622
623#endif
Andrew Scullb06d1752019-02-04 10:15:48 +0000624
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000625 /* The VM needs to be locked to deliver mailbox messages. */
626 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
627 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100628 vcpu_unlock(&vcpu_locked);
629 vm_locked = vm_lock(vcpu->vm);
630 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000631 }
632
633 /*
634 * If the vCPU is already running somewhere then we can't run it here
635 * simultaneously. While it is actually running then the state should be
636 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
637 * stops running but while Hafnium is in the process of switching back
638 * to the primary there will be a brief period while the state has been
639 * updated but `regs_available` is still false (until
640 * `api_regs_state_saved` is called). We can't start running it again
641 * until this has finished, so count this state as still running for the
642 * purposes of this check.
643 */
644 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
645 /*
646 * vCPU is running on another pCPU.
647 *
648 * It's okay not to return the sleep duration here because the
649 * other physical CPU that is currently running this vCPU will
650 * return the sleep duration if needed.
651 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100652 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000653 ret = false;
654 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000655 }
Andrew Scull9726c252019-01-23 13:44:19 +0000656
657 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100658 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100659 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000660 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100661 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000662 }
663 ret = false;
664 goto out;
665 }
666
Andrew Walbran508e63c2018-12-20 17:02:37 +0000667 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100668 case VCPU_STATE_RUNNING:
669 case VCPU_STATE_OFF:
670 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000671 ret = false;
672 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000673
Andrew Sculld6ee1102019-04-05 22:12:42 +0100674 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000675 /*
676 * A pending message allows the vCPU to run so the message can
677 * be delivered directly.
678 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100679 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100680 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100681 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100682 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000683 break;
684 }
685 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100686 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000687 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000688 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000689 break;
690 }
691
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100692 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
693
Andrew Walbran508e63c2018-12-20 17:02:37 +0000694 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100695 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000696 arch_timer_remaining_ns(&vcpu->regs);
697
698 /*
699 * The timer expired so allow the interrupt to be
700 * delivered.
701 */
702 if (timer_remaining_ns == 0) {
703 break;
704 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100705 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000706
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100707 /*
708 * The vCPU is not ready to run, return the appropriate code to
709 * the primary which called vcpu_run.
710 */
711 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100712 ? FFA_MSG_WAIT_32
713 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100714 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
715 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000716
717 ret = false;
718 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000719
Andrew Sculld6ee1102019-04-05 22:12:42 +0100720 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000721 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000722 }
723
Andrew Scullb06d1752019-02-04 10:15:48 +0000724 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000725 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100726 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000727
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000728 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000729 * Mark the registers as unavailable now that we're about to reflect
730 * them onto the real registers. This will also prevent another physical
731 * CPU from trying to read these registers.
732 */
733 vcpu->regs_available = false;
734
735 ret = true;
736
737out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100738 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000739 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100740 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000741 }
742
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000743 return ret;
744}
745
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100746struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
747 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100748{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100749 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100750 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100751 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100752
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000753 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100754 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100755 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100756 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100757 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100758
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000759 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100760 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100761 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100762 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100763
Andrew Scull19503262018-09-20 14:48:39 +0100764 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100765 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100766 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100767 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100768 }
769
Fuad Tabbaed294af2019-12-20 10:43:01 +0000770 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100771 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100772 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100773 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100774
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000775 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100776 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000777 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000778 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100779 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000780
Andrew Walbran508e63c2018-12-20 17:02:37 +0000781 /*
782 * Inject timer interrupt if timer has expired. It's safe to access
783 * vcpu->regs here because api_vcpu_prepare_run already made sure that
784 * regs_available was true (and then set it to false) before returning
785 * true.
786 */
787 if (arch_timer_pending(&vcpu->regs)) {
788 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100789 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
790 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000791
792 /*
793 * Set the mask bit so the hardware interrupt doesn't fire
794 * again. Ideally we wouldn't do this because it affects what
795 * the secondary vCPU sees, but if we don't then we end up with
796 * a loop of the interrupt firing each time we try to return to
797 * the secondary vCPU.
798 */
799 arch_timer_mask(&vcpu->regs);
800 }
801
Fuad Tabbaed294af2019-12-20 10:43:01 +0000802 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000803 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000804
Andrew Scull33fecd32019-01-08 14:48:27 +0000805 /*
806 * Set a placeholder return code to the scheduler. This will be
807 * overwritten when the switch back to the primary occurs.
808 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100809 ret.func = FFA_INTERRUPT_32;
810 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100811 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000812
Andrew Scull6d2db332018-10-10 15:28:17 +0100813out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100814 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100815}
816
817/**
Andrew Scull81e85092018-12-12 12:56:20 +0000818 * Check that the mode indicates memory that is valid, owned and exclusive.
819 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100820static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000821{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100822 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
823 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000824}
825
826/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100827 * Determines the value to be returned by api_ffa_rxtx_map and
828 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
829 * there are waiters, it also switches back to the primary VM for it to wake
830 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000831 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100832static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
833 struct vcpu *current,
834 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000835{
836 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000837
838 if (list_empty(&vm->mailbox.waiter_list)) {
839 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100840 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000841 }
842
843 if (vm->id == HF_PRIMARY_VM_ID) {
844 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100845 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000846 }
847
848 /*
849 * Switch back to the primary VM, informing it that there are waiters
850 * that need to be notified.
851 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000852 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100853 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000854 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000855
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100856 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000857}
858
859/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100860 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100861 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100862static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
863 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100864 paddr_t pa_send_begin, paddr_t pa_send_end,
865 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200866 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100867 struct mpool *local_page_pool)
868{
869 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100870
871 /* Map the send page as read-only in the hypervisor address space. */
872 vm_locked.vm->mailbox.send =
873 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200874 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100875 if (!vm_locked.vm->mailbox.send) {
876 /* TODO: partial defrag of failed range. */
877 /* Recover any memory consumed in failed mapping. */
878 mm_defrag(mm_stage1_locked, local_page_pool);
879 goto fail;
880 }
881
882 /*
883 * Map the receive page as writable in the hypervisor address space. On
884 * failure, unmap the send page before returning.
885 */
886 vm_locked.vm->mailbox.recv =
887 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200888 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100889 if (!vm_locked.vm->mailbox.recv) {
890 /* TODO: partial defrag of failed range. */
891 /* Recover any memory consumed in failed mapping. */
892 mm_defrag(mm_stage1_locked, local_page_pool);
893 goto fail_undo_send;
894 }
895
896 ret = true;
897 goto out;
898
899 /*
900 * The following mappings will not require more memory than is available
901 * in the local pool.
902 */
903fail_undo_send:
904 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100905 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
906 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100907
908fail:
909 ret = false;
910
911out:
Andrew Sculle1322792019-07-01 17:46:10 +0100912 return ret;
913}
914
915/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100916 * Sanity checks and configures the send and receive pages in the VM stage-2
917 * and hypervisor stage-1 page tables.
918 *
919 * Returns:
920 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100921 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +0100922 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
923 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100924 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +0100925 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100926 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100927
928struct ffa_value api_vm_configure_pages(
929 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
930 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
931 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100932{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100933 struct ffa_value ret;
934 paddr_t pa_send_begin;
935 paddr_t pa_send_end;
936 paddr_t pa_recv_begin;
937 paddr_t pa_recv_end;
938 uint32_t orig_send_mode;
939 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200940 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100941
942 /* We only allow these to be setup once. */
943 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
944 ret = ffa_error(FFA_DENIED);
945 goto out;
946 }
947
948 /* Hafnium only supports a fixed size of RX/TX buffers. */
949 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
950 ret = ffa_error(FFA_INVALID_PARAMETERS);
951 goto out;
952 }
953
954 /* Fail if addresses are not page-aligned. */
955 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
956 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
957 ret = ffa_error(FFA_INVALID_PARAMETERS);
958 goto out;
959 }
960
961 /* Convert to physical addresses. */
962 pa_send_begin = pa_from_ipa(send);
963 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
964 pa_recv_begin = pa_from_ipa(recv);
965 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
966
967 /* Fail if the same page is used for the send and receive pages. */
968 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
969 ret = ffa_error(FFA_INVALID_PARAMETERS);
970 goto out;
971 }
Andrew Sculle1322792019-07-01 17:46:10 +0100972
973 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100974 * Ensure the pages are valid, owned and exclusive to the VM and that
975 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +0100976 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100977 if (!mm_vm_get_mode(&vm_locked.vm->ptable, send,
978 ipa_add(send, PAGE_SIZE), &orig_send_mode) ||
979 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
980 (orig_send_mode & MM_MODE_R) == 0 ||
981 (orig_send_mode & MM_MODE_W) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100982 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +0100983 goto out;
984 }
985
986 if (!mm_vm_get_mode(&vm_locked.vm->ptable, recv,
987 ipa_add(recv, PAGE_SIZE), &orig_recv_mode) ||
988 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
989 (orig_recv_mode & MM_MODE_R) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +0100990 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +0100991 goto out;
992 }
Andrew Sculle1322792019-07-01 17:46:10 +0100993
994 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000995 if (!vm_identity_map(
996 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100997 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100998 local_page_pool, NULL)) {
999 ret = ffa_error(FFA_NO_MEMORY);
1000 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001001 }
1002
Andrew Scull3c257452019-11-26 13:32:50 +00001003 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1004 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001005 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001006 /* TODO: partial defrag of failed range. */
1007 /* Recover any memory consumed in failed mapping. */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001008 mm_vm_defrag(&vm_locked.vm->ptable, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001009 goto fail_undo_send;
1010 }
1011
Olivier Deprez96a2a262020-06-11 17:21:38 +02001012 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1013 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1014
Manish Pandeyd34f8892020-06-19 17:41:07 +01001015 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1016 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001017 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001018 goto fail_undo_send_and_recv;
1019 }
1020
Manish Pandeyd34f8892020-06-19 17:41:07 +01001021 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001022 goto out;
1023
Andrew Sculle1322792019-07-01 17:46:10 +01001024fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001025 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001026 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001027
1028fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001029 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001030 orig_send_mode, local_page_pool, NULL));
1031 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001032
1033out:
Andrew Sculle1322792019-07-01 17:46:10 +01001034 return ret;
1035}
1036
1037/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001038 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001039 * must not be shared. Locking of the page tables combined with a local memory
1040 * pool ensures there will always be enough memory to recover from any errors
1041 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1042 * by another core which could result in this transaction being unable to roll
1043 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001044 *
1045 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001046 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001047 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001048 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001049 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001050 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 * - FFA_SUCCESS on success if no further action is needed.
1052 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001053 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001054 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001055struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
1056 uint32_t page_count, struct vcpu *current,
1057 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001058{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001059 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001060 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001061 struct vm_locked vm_locked;
1062 struct mm_stage1_locked mm_stage1_locked;
1063 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001064
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001065 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001066 * Create a local pool so any freed memory can't be used by another
1067 * thread. This is to ensure the original mapping can be restored if any
1068 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001069 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001070 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1071
Andrew Sculle1322792019-07-01 17:46:10 +01001072 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001073 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001074
Manish Pandeyd34f8892020-06-19 17:41:07 +01001075 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1076 page_count, &local_page_pool);
1077 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001078 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001079 }
1080
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001081 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001082 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001083
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001084exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001085 mpool_fini(&local_page_pool);
1086
1087 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001088 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001089
1090 return ret;
1091}
1092
1093/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001094 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1095 * translation regime of the caller. Unmap the region for the hypervisor and
1096 * set the memory region to owned and exclusive for the component. Since the
1097 * memory region mapped in the page table, when the buffers were originally
1098 * created we can safely remap it.
1099 *
1100 * Returns:
1101 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1102 * behalf of the caller.
1103 * - FFA_SUCCESS on success if no further action is needed.
1104 */
1105struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1106 struct vcpu *current)
1107{
1108 struct vm *vm = current->vm;
1109 struct vm_locked vm_locked;
1110 struct mm_stage1_locked mm_stage1_locked;
1111 paddr_t send_pa_begin;
1112 paddr_t send_pa_end;
1113 paddr_t recv_pa_begin;
1114 paddr_t recv_pa_end;
1115
1116 /*
1117 * Check there is a buffer pair registered on behalf of the caller.
1118 * Since forwarding is not yet supported the allocator ID MBZ.
1119 */
1120 if (allocator_id != 0) {
1121 dlog_error(
1122 "Forwarding MAP/UNMAP from the hypervisor is not yet "
1123 "supported so vm id must be zero.\n");
1124 return ffa_error(FFA_INVALID_PARAMETERS);
1125 }
1126
1127 /* Get send and receive buffers. */
1128 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
1129 dlog_error(
1130 "No buffer pair registered on behalf of the caller.\n");
1131 return ffa_error(FFA_INVALID_PARAMETERS);
1132 }
1133
1134 /* Currently a mailbox size of 1 page is assumed. */
1135 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1136 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1137 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1138 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1139
1140 vm_locked = vm_lock(vm);
1141 mm_stage1_locked = mm_lock_stage1();
1142
1143 /*
1144 * Set the memory region of the buffers back to the default mode
1145 * for the VM. Since this memory region was already mapped for the
1146 * RXTX buffers we can safely remap them.
1147 */
1148 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1149 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1150 NULL));
1151
1152 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1153 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1154 NULL));
1155
1156 /* Unmap the buffers in the partition manager. */
1157 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1158 &api_page_pool));
1159 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1160 &api_page_pool));
1161
1162 vm->mailbox.send = NULL;
1163 vm->mailbox.recv = NULL;
1164
1165 mm_unlock_stage1(&mm_stage1_locked);
1166 vm_unlock(&vm_locked);
1167
1168 return (struct ffa_value){.func = FFA_SUCCESS_32};
1169}
1170
1171/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001172 * Notifies the `to` VM about the message currently in its mailbox, possibly
1173 * with the help of the primary VM.
1174 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001175static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1176 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001177{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001178 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1179 struct ffa_value primary_ret = {
1180 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001181 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001182 };
1183
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001184 /* Messages for the primary VM are delivered directly. */
1185 if (to.vm->id == HF_PRIMARY_VM_ID) {
1186 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001187 * Only tell the primary VM the size and other details if the
1188 * message is for it, to avoid leaking data about messages for
1189 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001190 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001191 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001192
1193 to.vm->mailbox.state = MAILBOX_STATE_READ;
1194 *next = api_switch_to_primary(current, primary_ret,
1195 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001196 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001197 }
1198
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001199 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1200
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001201 /* Messages for the TEE are sent on via the dispatcher. */
1202 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001203 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001204
Olivier Deprez112d2b52020-09-30 07:39:23 +02001205 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001206 /*
1207 * After the call to the TEE completes it must have finished
1208 * reading its RX buffer, so it is ready for another message.
1209 */
1210 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001211 /*
1212 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001213 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001214 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001215 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001216 }
1217
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001218 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001219 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001220 *next = api_switch_to_primary(current, primary_ret,
1221 VCPU_STATE_READY);
1222 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001223
1224 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001225}
1226
1227/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001228 * Copies data from the sender's send buffer to the recipient's receive buffer
1229 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001230 *
1231 * If the recipient's receive buffer is busy, it can optionally register the
1232 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001233 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001234struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1235 ffa_vm_id_t receiver_vm_id, uint32_t size,
1236 uint32_t attributes, struct vcpu *current,
1237 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001238{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001239 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001240 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001241 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001242 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001243 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001244 struct vcpu_locked current_locked;
1245 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001246 bool notify =
1247 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001248
Andrew Walbran70bc8622019-10-07 14:15:58 +01001249 /* Ensure sender VM ID corresponds to the current VM. */
1250 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001251 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001252 }
1253
1254 /* Disallow reflexive requests as this suggests an error in the VM. */
1255 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001256 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001257 }
1258
1259 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001260 if (size > FFA_MSG_PAYLOAD_MAX) {
1261 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001262 }
1263
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001264 /*
1265 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1266 * invocation.
1267 */
1268 current_locked = vcpu_lock(current);
1269 is_direct_request_ongoing =
1270 is_ffa_direct_msg_request_ongoing(current_locked);
1271 vcpu_unlock(&current_locked);
1272
1273 if (is_direct_request_ongoing) {
1274 return ffa_error(FFA_DENIED);
1275 }
1276
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001277 /* Ensure the receiver VM exists. */
1278 to = vm_find(receiver_vm_id);
1279 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001280 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001281 }
1282
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001283 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001284 * Check that the sender has configured its send buffer. If the tx
1285 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1286 * be safely accessed after releasing the lock since the tx mailbox
1287 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001288 */
1289 sl_lock(&from->lock);
1290 from_msg = from->mailbox.send;
1291 sl_unlock(&from->lock);
1292
1293 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001294 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001295 }
1296
Andrew Walbran82d6d152019-12-24 15:02:06 +00001297 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001298
Andrew Walbran82d6d152019-12-24 15:02:06 +00001299 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001300 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001301 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001302 }
1303
Andrew Walbran82d6d152019-12-24 15:02:06 +00001304 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001305 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001306 to->mailbox.recv_size = size;
1307 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001308 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001309 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001310
1311out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001312 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001313
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001314 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001315}
1316
1317/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001318 * Checks whether the vCPU's attempt to block for a message has already been
1319 * interrupted or whether it is allowed to block.
1320 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001321bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001322{
Manish Pandey35e452f2021-02-18 21:36:34 +00001323 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001324 bool interrupted;
1325
Manish Pandey35e452f2021-02-18 21:36:34 +00001326 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001327
1328 /*
1329 * Don't block if there are enabled and pending interrupts, to match
1330 * behaviour of wait_for_interrupt.
1331 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001332 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001333
Manish Pandey35e452f2021-02-18 21:36:34 +00001334 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001335
1336 return interrupted;
1337}
1338
1339/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001340 * Receives a message from the mailbox. If one isn't available, this function
1341 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001342 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001343 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001344 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001345struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1346 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001347{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001348 bool is_direct_request_ongoing;
1349 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001350 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001351 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001352 bool is_from_secure_world =
1353 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001354
Andrew Scullaa039b32018-10-04 15:02:26 +01001355 /*
1356 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001357 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001358 */
J-Alvesb37fd082020-10-22 12:29:21 +01001359 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001360 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001361 }
1362
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001363 /*
1364 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1365 * invocation.
1366 */
1367 current_locked = vcpu_lock(current);
1368 is_direct_request_ongoing =
1369 is_ffa_direct_msg_request_ongoing(current_locked);
1370 vcpu_unlock(&current_locked);
1371
1372 if (is_direct_request_ongoing) {
1373 return ffa_error(FFA_DENIED);
1374 }
1375
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001376 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001377
Andrew Scullaa039b32018-10-04 15:02:26 +01001378 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001379 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1380 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001381 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001382 goto out;
1383 }
1384
1385 /* No pending message so fail if not allowed to block. */
1386 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001387 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001388 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001389 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001390
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001391 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001392 * From this point onward this call can only be interrupted or a message
1393 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001394 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001395 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001396 return_code = ffa_error(FFA_INTERRUPTED);
1397 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001398 goto out;
1399 }
1400
J-Alvesb37fd082020-10-22 12:29:21 +01001401 if (is_from_secure_world) {
1402 /* Return to other world if caller is a SP. */
1403 *next = api_switch_to_other_world(
1404 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
1405 VCPU_STATE_BLOCKED_MAILBOX);
1406 } else {
1407 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 struct ffa_value run_return = {
1409 .func = FFA_MSG_WAIT_32,
1410 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001411 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001412
Andrew Walbranb4816552018-12-05 17:35:42 +00001413 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001414 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001415 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001416out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001417 sl_unlock(&vm->lock);
1418
Jose Marinho3e2442f2019-03-12 13:30:37 +00001419 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001420}
1421
1422/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001423 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1424 * by this function, the caller must have called api_mailbox_send before with
1425 * the notify argument set to true, and this call must have failed because the
1426 * mailbox was not available.
1427 *
1428 * It should be called repeatedly to retrieve a list of VMs.
1429 *
1430 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1431 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001432 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001433int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001434{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001435 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001436 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001437 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001438
1439 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001440 if (list_empty(&vm->mailbox.ready_list)) {
1441 ret = -1;
1442 goto exit;
1443 }
1444
1445 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1446 ready_links);
1447 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001448 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001449
1450exit:
1451 sl_unlock(&vm->lock);
1452 return ret;
1453}
1454
1455/**
1456 * Retrieves the next VM waiting to be notified that the mailbox of the
1457 * specified VM became writable. Only primary VMs are allowed to call this.
1458 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001459 * Returns -1 on failure or if there are no waiters; the VM id of the next
1460 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001461 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001462int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001463{
1464 struct vm *vm;
1465 struct vm_locked locked;
1466 struct wait_entry *entry;
1467 struct vm *waiting_vm;
1468
1469 /* Only primary VMs are allowed to call this function. */
1470 if (current->vm->id != HF_PRIMARY_VM_ID) {
1471 return -1;
1472 }
1473
Andrew Walbran42347a92019-05-09 13:59:03 +01001474 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001475 if (vm == NULL) {
1476 return -1;
1477 }
1478
Fuad Tabbaed294af2019-12-20 10:43:01 +00001479 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001480 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001481 entry = api_fetch_waiter(locked);
1482 vm_unlock(&locked);
1483
1484 if (entry == NULL) {
1485 return -1;
1486 }
1487
1488 /* Enqueue notification to waiting VM. */
1489 waiting_vm = entry->waiting_vm;
1490
1491 sl_lock(&waiting_vm->lock);
1492 if (list_empty(&entry->ready_links)) {
1493 list_append(&waiting_vm->mailbox.ready_list,
1494 &entry->ready_links);
1495 }
1496 sl_unlock(&waiting_vm->lock);
1497
1498 return waiting_vm->id;
1499}
1500
1501/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001502 * Releases the caller's mailbox so that a new message can be received. The
1503 * caller must have copied out all data they wish to preserve as new messages
1504 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001505 *
1506 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001507 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1508 * - FFA_SUCCESS on success if no further action is needed.
1509 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001510 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001511 * hf_mailbox_waiter_get.
1512 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001513struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001514{
1515 struct vm *vm = current->vm;
1516 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001517 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001518
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001519 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001520 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001521 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001522 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001523 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001524 break;
1525
Andrew Sculld6ee1102019-04-05 22:12:42 +01001526 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001527 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001528 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001529 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001530 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001531 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001532
1533 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001534}
Andrew Walbran318f5732018-11-20 16:23:42 +00001535
1536/**
1537 * Enables or disables a given interrupt ID for the calling vCPU.
1538 *
1539 * Returns 0 on success, or -1 if the intid is invalid.
1540 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001541int64_t api_interrupt_enable(uint32_t intid, bool enable,
1542 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001543{
Manish Pandey35e452f2021-02-18 21:36:34 +00001544 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001545 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001546 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1547 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001548
Andrew Walbran318f5732018-11-20 16:23:42 +00001549 if (intid >= HF_NUM_INTIDS) {
1550 return -1;
1551 }
1552
Manish Pandey35e452f2021-02-18 21:36:34 +00001553 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001554 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001555 /*
1556 * If it is pending and was not enabled before, increment the
1557 * count.
1558 */
1559 if (current->interrupts.interrupt_pending[intid_index] &
1560 ~current->interrupts.interrupt_enabled[intid_index] &
1561 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001562 if ((current->interrupts.interrupt_type[intid_index] &
1563 intid_mask) ==
1564 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1565 vcpu_irq_count_increment(current_locked);
1566 } else {
1567 vcpu_fiq_count_increment(current_locked);
1568 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001569 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001570 current->interrupts.interrupt_enabled[intid_index] |=
1571 intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001572
1573 if (type == INTERRUPT_TYPE_IRQ) {
1574 current->interrupts.interrupt_type[intid_index] &=
1575 ~intid_mask;
1576 } else if (type == INTERRUPT_TYPE_FIQ) {
1577 current->interrupts.interrupt_type[intid_index] |=
1578 intid_mask;
1579 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001580 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001581 /*
1582 * If it is pending and was enabled before, decrement the count.
1583 */
1584 if (current->interrupts.interrupt_pending[intid_index] &
1585 current->interrupts.interrupt_enabled[intid_index] &
1586 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001587 if ((current->interrupts.interrupt_type[intid_index] &
1588 intid_mask) ==
1589 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1590 vcpu_irq_count_decrement(current_locked);
1591 } else {
1592 vcpu_fiq_count_decrement(current_locked);
1593 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001594 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001595 current->interrupts.interrupt_enabled[intid_index] &=
1596 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001597 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001598 }
1599
Manish Pandey35e452f2021-02-18 21:36:34 +00001600 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001601 return 0;
1602}
1603
1604/**
1605 * Returns the ID of the next pending interrupt for the calling vCPU, and
1606 * acknowledges it (i.e. marks it as no longer pending). Returns
1607 * HF_INVALID_INTID if there are no pending interrupts.
1608 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001609uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001610{
1611 uint8_t i;
1612 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001613 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001614
1615 /*
1616 * Find the first enabled and pending interrupt ID, return it, and
1617 * deactivate it.
1618 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001619 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001620 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1621 uint32_t enabled_and_pending =
1622 current->interrupts.interrupt_enabled[i] &
1623 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001624
Andrew Walbran318f5732018-11-20 16:23:42 +00001625 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001626 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001627 uint32_t intid_mask = 1U << bit_index;
1628
Andrew Walbran3d84a262018-12-13 14:41:19 +00001629 /*
1630 * Mark it as no longer pending and decrement the count.
1631 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001632 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1633
1634 if ((current->interrupts.interrupt_type[i] &
1635 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1636 vcpu_irq_count_decrement(current_locked);
1637 } else {
1638 vcpu_fiq_count_decrement(current_locked);
1639 }
1640
Andrew Walbran3d84a262018-12-13 14:41:19 +00001641 first_interrupt =
1642 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001643 break;
1644 }
1645 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001646
Manish Pandey35e452f2021-02-18 21:36:34 +00001647 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001648 return first_interrupt;
1649}
1650
1651/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001652 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001653 * given VM and vCPU.
1654 */
1655static inline bool is_injection_allowed(uint32_t target_vm_id,
1656 struct vcpu *current)
1657{
1658 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001659
Andrew Walbran318f5732018-11-20 16:23:42 +00001660 /*
1661 * The primary VM is allowed to inject interrupts into any VM. Secondary
1662 * VMs are only allowed to inject interrupts into their own vCPUs.
1663 */
1664 return current_vm_id == HF_PRIMARY_VM_ID ||
1665 current_vm_id == target_vm_id;
1666}
1667
1668/**
1669 * Injects a virtual interrupt of the given ID into the given target vCPU.
1670 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1671 * when the vCPU is next run, which is up to the scheduler.
1672 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001673 * Returns:
1674 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1675 * ID is invalid, or the current VM is not allowed to inject interrupts to
1676 * the target VM.
1677 * - 0 on success if no further action is needed.
1678 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1679 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001680 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001681int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1682 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001683 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001684{
Andrew Walbran318f5732018-11-20 16:23:42 +00001685 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001686 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001687
1688 if (intid >= HF_NUM_INTIDS) {
1689 return -1;
1690 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001691
Andrew Walbran318f5732018-11-20 16:23:42 +00001692 if (target_vm == NULL) {
1693 return -1;
1694 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001695
Andrew Walbran318f5732018-11-20 16:23:42 +00001696 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001697 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001698 return -1;
1699 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001700
Andrew Walbran318f5732018-11-20 16:23:42 +00001701 if (!is_injection_allowed(target_vm_id, current)) {
1702 return -1;
1703 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001704
Andrew Walbrane1310df2019-04-29 17:28:28 +01001705 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001706
Manish Pandey35e452f2021-02-18 21:36:34 +00001707 dlog_verbose(
1708 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1709 "%u\n",
1710 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1711 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001712 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001713}
Andrew Scull6386f252018-12-06 13:29:10 +00001714
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001715/** Returns the version of the implemented FF-A specification. */
1716struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001717{
1718 /*
1719 * Ensure that both major and minor revision representation occupies at
1720 * most 15 bits.
1721 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001722 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001723 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001724 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001725 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001726 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001727 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001728 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001729 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001730
Daniel Boulby6e32c612021-02-17 15:09:41 +00001731 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001732}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001733
1734int64_t api_debug_log(char c, struct vcpu *current)
1735{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001736 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001737 struct vm *vm = current->vm;
1738 struct vm_locked vm_locked = vm_lock(vm);
1739
Andrew Sculld54e1be2019-08-20 11:09:42 +01001740 if (c == '\n' || c == '\0') {
1741 flush = true;
1742 } else {
1743 vm->log_buffer[vm->log_buffer_length++] = c;
1744 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1745 }
1746
1747 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001748 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1749 vm->log_buffer_length);
1750 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001751 }
1752
1753 vm_unlock(&vm_locked);
1754
1755 return 0;
1756}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001757
1758/**
1759 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001760 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001761 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001762struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001763{
1764 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001765 case FFA_ERROR_32:
1766 case FFA_SUCCESS_32:
1767 case FFA_INTERRUPT_32:
1768 case FFA_VERSION_32:
1769 case FFA_FEATURES_32:
1770 case FFA_RX_RELEASE_32:
1771 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001772 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001773 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001774 case FFA_ID_GET_32:
1775 case FFA_MSG_POLL_32:
1776 case FFA_MSG_WAIT_32:
1777 case FFA_YIELD_32:
1778 case FFA_RUN_32:
1779 case FFA_MSG_SEND_32:
1780 case FFA_MEM_DONATE_32:
1781 case FFA_MEM_LEND_32:
1782 case FFA_MEM_SHARE_32:
1783 case FFA_MEM_RETRIEVE_REQ_32:
1784 case FFA_MEM_RETRIEVE_RESP_32:
1785 case FFA_MEM_RELINQUISH_32:
1786 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001787 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001788 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001789 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001790 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001791 return (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00001792 /* FF-A v1.1 features. */
1793 case FFA_SPM_ID_GET_32:
1794 if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED) {
1795 return (struct ffa_value){.func = FFA_SUCCESS_32};
1796 }
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001797 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001798 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001799 }
1800}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001801
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001802/**
J-Alves645eabe2021-02-22 16:08:27 +00001803 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
1804 * interfaces.
1805 */
1806static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
1807{
1808 return args.arg2 == 0U;
1809}
1810
1811/**
J-Alves76d99af2021-03-10 17:42:11 +00001812 * Limits size of arguments in ffa_value structure to 32-bit.
1813 */
1814static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
1815{
1816 return (struct ffa_value){
1817 .func = (uint32_t)args.func,
1818 .arg1 = (uint32_t)args.arg1,
1819 .arg2 = (uint32_t)0,
1820 .arg3 = (uint32_t)args.arg3,
1821 .arg4 = (uint32_t)args.arg4,
1822 .arg5 = (uint32_t)args.arg5,
1823 .arg6 = (uint32_t)args.arg6,
1824 .arg7 = (uint32_t)args.arg7,
1825 };
1826}
1827
1828/**
1829 * Helper to copy direct message payload, depending on SMC used and expected
1830 * registers size.
1831 */
1832static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
1833{
1834 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
1835 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
1836 return api_ffa_value_copy32(args);
1837 }
1838
1839 return (struct ffa_value){
1840 .func = args.func,
1841 .arg1 = args.arg1,
1842 .arg2 = 0,
1843 .arg3 = args.arg3,
1844 .arg4 = args.arg4,
1845 .arg5 = args.arg5,
1846 .arg6 = args.arg6,
1847 .arg7 = args.arg7,
1848 };
1849}
1850
1851/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001852 * Send an FF-A direct message request.
1853 */
1854struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1855 ffa_vm_id_t receiver_vm_id,
1856 struct ffa_value args,
1857 struct vcpu *current,
1858 struct vcpu **next)
1859{
J-Alves17228f72021-04-20 17:13:19 +01001860 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001861 struct vm *receiver_vm;
1862 struct vcpu *receiver_vcpu;
1863 struct two_vcpu_locked vcpus_locked;
1864
J-Alves645eabe2021-02-22 16:08:27 +00001865 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1866 return ffa_error(FFA_INVALID_PARAMETERS);
1867 }
1868
Olivier Deprez55a189e2021-06-09 15:45:27 +02001869 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
1870 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001871 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001872 }
1873
Olivier Deprez55a189e2021-06-09 15:45:27 +02001874 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01001875 return ret;
1876 }
1877
1878 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
1879
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001880 receiver_vm = vm_find(receiver_vm_id);
1881 if (receiver_vm == NULL) {
1882 return ffa_error(FFA_INVALID_PARAMETERS);
1883 }
1884
1885 /*
1886 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1887 * UP (migratable) or MP partitions with a number of vCPUs matching the
1888 * number of PEs in the system. It further states that MP partitions
1889 * accepting direct request messages cannot migrate.
1890 */
J-Alvesad6a0432021-04-09 16:06:21 +01001891 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001892 if (receiver_vcpu == NULL) {
1893 return ffa_error(FFA_INVALID_PARAMETERS);
1894 }
1895
1896 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1897
1898 /*
1899 * If destination vCPU is executing or already received an
1900 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1901 * busy. There is a brief period of time where the vCPU state has
1902 * changed but regs_available is still false thus consider this case as
1903 * the vCPU not yet ready to receive a direct message request.
1904 */
1905 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1906 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1907 !receiver_vcpu->regs_available) {
1908 ret = ffa_error(FFA_BUSY);
1909 goto out;
1910 }
1911
1912 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1913 memory_order_relaxed)) {
1914 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001915 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001916 receiver_vcpu->vm->id,
1917 vcpu_index(receiver_vcpu));
1918 receiver_vcpu->state = VCPU_STATE_ABORTED;
1919 }
1920
1921 ret = ffa_error(FFA_ABORTED);
1922 goto out;
1923 }
1924
1925 switch (receiver_vcpu->state) {
1926 case VCPU_STATE_OFF:
1927 case VCPU_STATE_RUNNING:
1928 case VCPU_STATE_ABORTED:
1929 case VCPU_STATE_READY:
1930 case VCPU_STATE_BLOCKED_INTERRUPT:
1931 ret = ffa_error(FFA_BUSY);
1932 goto out;
1933 case VCPU_STATE_BLOCKED_MAILBOX:
1934 /*
1935 * Expect target vCPU to be blocked after having called
1936 * ffa_msg_wait or sent a direct message response.
1937 */
1938 break;
1939 }
1940
1941 /* Inject timer interrupt if any pending */
1942 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01001943 api_interrupt_inject_locked(vcpus_locked.vcpu1,
1944 HF_VIRTUAL_TIMER_INTID, current,
1945 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001946
1947 arch_timer_mask(&receiver_vcpu->regs);
1948 }
1949
1950 /* The receiver vCPU runs upon direct message invocation */
1951 receiver_vcpu->cpu = current->cpu;
1952 receiver_vcpu->state = VCPU_STATE_RUNNING;
1953 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001954 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001955
J-Alves76d99af2021-03-10 17:42:11 +00001956 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001957
1958 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1959
1960 /* Switch to receiver vCPU targeted to by direct msg request */
1961 *next = receiver_vcpu;
1962
1963 /*
1964 * Since this flow will lead to a VM switch, the return value will not
1965 * be applied to current vCPU.
1966 */
1967
1968out:
1969 sl_unlock(&receiver_vcpu->lock);
1970 sl_unlock(&current->lock);
1971
1972 return ret;
1973}
1974
1975/**
1976 * Send an FF-A direct message response.
1977 */
1978struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1979 ffa_vm_id_t receiver_vm_id,
1980 struct ffa_value args,
1981 struct vcpu *current,
1982 struct vcpu **next)
1983{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001984 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00001985
1986 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1987 return ffa_error(FFA_INVALID_PARAMETERS);
1988 }
1989
J-Alves76d99af2021-03-10 17:42:11 +00001990 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001991
Olivier Deprez55a189e2021-06-09 15:45:27 +02001992 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
1993 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001994 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001995 }
1996
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001997 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01001998 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001999 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002000 * No need for REQ/RESP state management as managed exit does
2001 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002002 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002003 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2004 vcpu_unlock(&current_locked);
2005 return ffa_error(FFA_DENIED);
2006 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002007
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002008 plat_interrupts_set_priority_mask(0xff);
2009 current->processing_managed_exit = false;
2010 } else {
2011 /*
2012 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2013 * defined originator.
2014 */
2015 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2016 /*
2017 * Sending direct response but direct request origin
2018 * vCPU is not set.
2019 */
2020 vcpu_unlock(&current_locked);
2021 return ffa_error(FFA_DENIED);
2022 }
2023
2024 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2025 vcpu_unlock(&current_locked);
2026 return ffa_error(FFA_DENIED);
2027 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002028 }
2029
2030 /* Clear direct request origin for the caller. */
2031 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2032
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002033 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002034
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002035 if (!vm_id_is_current_world(receiver_vm_id)) {
J-Alvesfe7f7372020-11-09 11:32:12 +00002036 *next = api_switch_to_other_world(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002037 VCPU_STATE_BLOCKED_MAILBOX);
2038 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
J-Alvesfe7f7372020-11-09 11:32:12 +00002039 *next = api_switch_to_primary(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002040 VCPU_STATE_BLOCKED_MAILBOX);
J-Alvesfe7f7372020-11-09 11:32:12 +00002041 } else if (vm_id_is_current_world(receiver_vm_id)) {
2042 /*
2043 * It is expected the receiver_vm_id to be from an SP, otherwise
2044 * 'arch_other_world_is_direct_response_valid' should have
2045 * made function return error before getting to this point.
2046 */
2047 *next = api_switch_to_vm(current, to_ret,
2048 VCPU_STATE_BLOCKED_MAILBOX,
2049 receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002050 } else {
2051 panic("Invalid direct message response invocation");
2052 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002053
2054 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2055}
2056
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002057struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2058 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002059 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002060{
2061 struct vm *from = current->vm;
2062 struct vm *to;
2063 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002064 struct ffa_memory_region *memory_region;
2065 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002066
2067 if (ipa_addr(address) != 0 || page_count != 0) {
2068 /*
2069 * Hafnium only supports passing the descriptor in the TX
2070 * mailbox.
2071 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002072 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002073 }
2074
Andrew Walbranca808b12020-05-15 17:22:28 +01002075 if (fragment_length > length) {
2076 dlog_verbose(
2077 "Fragment length %d greater than total length %d.\n",
2078 fragment_length, length);
2079 return ffa_error(FFA_INVALID_PARAMETERS);
2080 }
2081 if (fragment_length < sizeof(struct ffa_memory_region) +
2082 sizeof(struct ffa_memory_access)) {
2083 dlog_verbose(
2084 "Initial fragment length %d smaller than header size "
2085 "%d.\n",
2086 fragment_length,
2087 sizeof(struct ffa_memory_region) +
2088 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002089 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002090 }
2091
2092 /*
2093 * Check that the sender has configured its send buffer. If the TX
2094 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2095 * be safely accessed after releasing the lock since the TX mailbox
2096 * address can only be configured once.
2097 */
2098 sl_lock(&from->lock);
2099 from_msg = from->mailbox.send;
2100 sl_unlock(&from->lock);
2101
2102 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002103 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002104 }
2105
2106 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002107 * Copy the memory region descriptor to a fresh page from the memory
2108 * pool. This prevents the sender from changing it underneath us, and
2109 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002110 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002111 if (fragment_length > HF_MAILBOX_SIZE ||
2112 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002113 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002114 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002115 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002116 if (memory_region == NULL) {
2117 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002118 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002119 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002120 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002121
2122 /* The sender must match the caller. */
2123 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002124 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002125 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002126 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002127 }
2128
Andrew Walbrana65a1322020-04-06 19:32:32 +01002129 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002130 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002131 dlog_verbose(
2132 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002133 "endpoint memory access descriptors, expected 1).\n",
2134 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002135 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002136 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002137 }
2138
2139 /*
2140 * Ensure that the receiver VM exists and isn't the same as the sender.
2141 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002142 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002143 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002144 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002145 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002146 goto out;
2147 }
2148
2149 if (to->id == HF_TEE_VM_ID) {
2150 /*
2151 * The 'to' VM lock is only needed in the case that it is the
2152 * TEE VM.
2153 */
2154 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2155
2156 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002157 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002158 goto out_unlock;
2159 }
2160
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002161 ret = ffa_memory_tee_send(
2162 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2163 length, fragment_length, share_func, &api_page_pool);
2164 /*
2165 * ffa_tee_memory_send takes ownership of the memory_region, so
2166 * make sure we don't free it.
2167 */
2168 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002169
2170 out_unlock:
2171 vm_unlock(&vm_to_from_lock.vm1);
2172 vm_unlock(&vm_to_from_lock.vm2);
2173 } else {
2174 struct vm_locked from_locked = vm_lock(from);
2175
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002176 ret = ffa_memory_send(from_locked, memory_region, length,
2177 fragment_length, share_func,
2178 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002179 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002180 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002181 * make sure we don't free it.
2182 */
2183 memory_region = NULL;
2184
2185 vm_unlock(&from_locked);
2186 }
2187
2188out:
2189 if (memory_region != NULL) {
2190 mpool_free(&api_page_pool, memory_region);
2191 }
2192
2193 return ret;
2194}
2195
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002196struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2197 uint32_t fragment_length,
2198 ipaddr_t address, uint32_t page_count,
2199 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002200{
2201 struct vm *to = current->vm;
2202 struct vm_locked to_locked;
2203 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002204 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002205 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002206 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002207
2208 if (ipa_addr(address) != 0 || page_count != 0) {
2209 /*
2210 * Hafnium only supports passing the descriptor in the TX
2211 * mailbox.
2212 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002213 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002214 }
2215
Andrew Walbrana65a1322020-04-06 19:32:32 +01002216 if (fragment_length != length) {
2217 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002218 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002219 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002220
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002221 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002222 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002223 message_buffer_size = cpu_get_buffer_size(current->cpu);
2224 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2225 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002226 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002227 }
2228
2229 to_locked = vm_lock(to);
2230 to_msg = to->mailbox.send;
2231
2232 if (to_msg == NULL) {
2233 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002234 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002235 goto out;
2236 }
2237
2238 /*
2239 * Copy the retrieve request descriptor to an internal buffer, so that
2240 * the caller can't change it underneath us.
2241 */
2242 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2243
2244 if (msg_receiver_busy(to_locked, NULL, false)) {
2245 /*
2246 * Can't retrieve memory information if the mailbox is not
2247 * available.
2248 */
2249 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002250 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002251 goto out;
2252 }
2253
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002254 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2255 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002256
2257out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002258 vm_unlock(&to_locked);
2259 return ret;
2260}
2261
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002262struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002263{
2264 struct vm *from = current->vm;
2265 struct vm_locked from_locked;
2266 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002267 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002268 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002269 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002270 uint32_t length;
2271
2272 from_locked = vm_lock(from);
2273 from_msg = from->mailbox.send;
2274
2275 if (from_msg == NULL) {
2276 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002277 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002278 goto out;
2279 }
2280
2281 /*
2282 * Calculate length from relinquish descriptor before copying. We will
2283 * check again later to make sure it hasn't changed.
2284 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002285 length = sizeof(struct ffa_mem_relinquish) +
2286 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2287 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002288 /*
2289 * Copy the relinquish descriptor to an internal buffer, so that the
2290 * caller can't change it underneath us.
2291 */
2292 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002293 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002294 message_buffer_size = cpu_get_buffer_size(current->cpu);
2295 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2296 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002297 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002298 goto out;
2299 }
2300 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2301
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002302 if (sizeof(struct ffa_mem_relinquish) +
2303 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002304 length) {
2305 dlog_verbose(
2306 "Endpoint count changed while copying to internal "
2307 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002308 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002309 goto out;
2310 }
2311
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002312 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2313 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002314
2315out:
2316 vm_unlock(&from_locked);
2317 return ret;
2318}
2319
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002320struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2321 ffa_memory_region_flags_t flags,
2322 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002323{
2324 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002325 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002326
Olivier Deprez55a189e2021-06-09 15:45:27 +02002327 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002328 struct vm_locked to_locked = vm_lock(to);
2329
Andrew Walbranca808b12020-05-15 17:22:28 +01002330 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002331 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002332
Andrew Walbran290b0c92020-02-03 16:37:14 +00002333 vm_unlock(&to_locked);
2334 } else {
2335 struct vm *from = vm_find(HF_TEE_VM_ID);
2336 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2337
Andrew Walbranca808b12020-05-15 17:22:28 +01002338 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2339 vm_to_from_lock.vm2, handle, flags,
2340 &api_page_pool);
2341
2342 vm_unlock(&vm_to_from_lock.vm1);
2343 vm_unlock(&vm_to_from_lock.vm2);
2344 }
2345
2346 return ret;
2347}
2348
2349struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2350 uint32_t fragment_offset,
2351 ffa_vm_id_t sender_vm_id,
2352 struct vcpu *current)
2353{
2354 struct vm *to = current->vm;
2355 struct vm_locked to_locked;
2356 struct ffa_value ret;
2357
2358 /* Sender ID MBZ at virtual instance. */
2359 if (sender_vm_id != 0) {
2360 return ffa_error(FFA_INVALID_PARAMETERS);
2361 }
2362
2363 to_locked = vm_lock(to);
2364
2365 if (msg_receiver_busy(to_locked, NULL, false)) {
2366 /*
2367 * Can't retrieve memory information if the mailbox is not
2368 * available.
2369 */
2370 dlog_verbose("RX buffer not ready.\n");
2371 ret = ffa_error(FFA_BUSY);
2372 goto out;
2373 }
2374
2375 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2376 &api_page_pool);
2377
2378out:
2379 vm_unlock(&to_locked);
2380 return ret;
2381}
2382
2383struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2384 uint32_t fragment_length,
2385 ffa_vm_id_t sender_vm_id,
2386 struct vcpu *current)
2387{
2388 struct vm *from = current->vm;
2389 const void *from_msg;
2390 void *fragment_copy;
2391 struct ffa_value ret;
2392
2393 /* Sender ID MBZ at virtual instance. */
2394 if (sender_vm_id != 0) {
2395 return ffa_error(FFA_INVALID_PARAMETERS);
2396 }
2397
2398 /*
2399 * Check that the sender has configured its send buffer. If the TX
2400 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2401 * be safely accessed after releasing the lock since the TX mailbox
2402 * address can only be configured once.
2403 */
2404 sl_lock(&from->lock);
2405 from_msg = from->mailbox.send;
2406 sl_unlock(&from->lock);
2407
2408 if (from_msg == NULL) {
2409 return ffa_error(FFA_INVALID_PARAMETERS);
2410 }
2411
2412 /*
2413 * Copy the fragment to a fresh page from the memory pool. This prevents
2414 * the sender from changing it underneath us, and also lets us keep it
2415 * around in the share state table if needed.
2416 */
2417 if (fragment_length > HF_MAILBOX_SIZE ||
2418 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2419 dlog_verbose(
2420 "Fragment length %d larger than mailbox size %d.\n",
2421 fragment_length, HF_MAILBOX_SIZE);
2422 return ffa_error(FFA_INVALID_PARAMETERS);
2423 }
2424 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2425 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2426 0) {
2427 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2428 return ffa_error(FFA_INVALID_PARAMETERS);
2429 }
2430 fragment_copy = mpool_alloc(&api_page_pool);
2431 if (fragment_copy == NULL) {
2432 dlog_verbose("Failed to allocate fragment copy.\n");
2433 return ffa_error(FFA_NO_MEMORY);
2434 }
2435 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2436
2437 /*
2438 * Hafnium doesn't support fragmentation of memory retrieve requests
2439 * (because it doesn't support caller-specified mappings, so a request
2440 * will never be larger than a single page), so this must be part of a
2441 * memory send (i.e. donate, lend or share) request.
2442 *
2443 * We can tell from the handle whether the memory transaction is for the
2444 * TEE or not.
2445 */
2446 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2447 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2448 struct vm_locked from_locked = vm_lock(from);
2449
2450 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2451 fragment_length, handle,
2452 &api_page_pool);
2453 /*
2454 * `ffa_memory_send_continue` takes ownership of the
2455 * fragment_copy, so we don't need to free it here.
2456 */
2457 vm_unlock(&from_locked);
2458 } else {
2459 struct vm *to = vm_find(HF_TEE_VM_ID);
2460 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2461
2462 /*
2463 * The TEE RX buffer state is checked in
2464 * `ffa_memory_tee_send_continue` rather than here, as we need
2465 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2466 * than FFA_ERROR FFA_BUSY in case it is busy.
2467 */
2468
2469 ret = ffa_memory_tee_send_continue(
2470 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2471 fragment_length, handle, &api_page_pool);
2472 /*
2473 * `ffa_memory_tee_send_continue` takes ownership of the
2474 * fragment_copy, so we don't need to free it here.
2475 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002476
2477 vm_unlock(&vm_to_from_lock.vm1);
2478 vm_unlock(&vm_to_from_lock.vm2);
2479 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002480
2481 return ret;
2482}
Max Shvetsov40108e72020-08-27 12:39:50 +01002483
2484struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2485 struct vcpu *current)
2486{
2487 struct vm_locked vm_locked;
2488
2489 vm_locked = vm_lock(current->vm);
2490 vm_locked.vm->secondary_ep = entry_point;
2491 vm_unlock(&vm_locked);
2492
2493 return (struct ffa_value){.func = FFA_SUCCESS_32};
2494}