blob: 10af541b168108f4d207df6aa1e82010dd4d26f9 [file] [log] [blame]
Karl Meakin5a365d32024-11-08 23:55:03 +00001/*
2 * Copyright 2024 The Hafnium Authors.
3 *
4 * 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.
7 */
8
9#include "hf/arch/gicv3.h"
Karl Meakin5a365d32024-11-08 23:55:03 +000010
11#include "hf/api.h"
12#include "hf/check.h"
Karl Meakin902af082024-11-28 14:58:38 +000013#include "hf/ffa.h"
Karl Meakinfa1dcb82025-02-10 16:47:50 +000014#include "hf/ffa/direct_messaging.h"
Karl Meakin902af082024-11-28 14:58:38 +000015#include "hf/ffa/interrupts.h"
Karl Meakin936ec1e2025-01-31 13:17:11 +000016#include "hf/ffa/vm.h"
17#include "hf/ffa_internal.h"
Karl Meakin5a365d32024-11-08 23:55:03 +000018#include "hf/plat/interrupts.h"
19#include "hf/vm.h"
20
Karl Meakin117c8082024-12-04 16:03:28 +000021bool ffa_cpu_cycles_run_forward(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
22 struct ffa_value *ret)
Karl Meakin5a365d32024-11-08 23:55:03 +000023{
24 (void)vm_id;
25 (void)vcpu_idx;
26 (void)ret;
27
28 return false;
29}
30
31/**
32 * Check if current VM can resume target VM using FFA_RUN ABI.
33 */
Karl Meakin117c8082024-12-04 16:03:28 +000034bool ffa_cpu_cycles_run_checks(struct vcpu_locked current_locked,
35 ffa_id_t target_vm_id, ffa_vcpu_index_t vcpu_idx,
36 struct ffa_value *run_ret, struct vcpu **next)
Karl Meakin5a365d32024-11-08 23:55:03 +000037{
38 /*
39 * Under the Partition runtime model specified in FF-A v1.1-Beta0 spec,
40 * SP can invoke FFA_RUN to resume target SP.
41 */
42 struct vcpu *target_vcpu;
43 struct vcpu *current = current_locked.vcpu;
44 bool ret = true;
45 struct vm *vm;
46 struct vcpu_locked target_locked;
47 struct two_vcpu_locked vcpus_locked;
48
49 vm = vm_find(target_vm_id);
50 if (vm == NULL) {
51 return false;
52 }
53
54 if (vm_is_mp(vm) && vm_is_mp(current->vm) &&
55 vcpu_idx != cpu_index(current->cpu)) {
56 dlog_verbose("vcpu_idx (%d) != pcpu index (%zu)\n", vcpu_idx,
57 cpu_index(current->cpu));
58 return false;
59 }
60
61 target_vcpu = api_ffa_get_vm_vcpu(vm, current);
62
63 vcpu_unlock(&current_locked);
64
65 /* Lock both vCPUs at once to avoid deadlock. */
66 vcpus_locked = vcpu_lock_both(current, target_vcpu);
67 current_locked = vcpus_locked.vcpu1;
68 target_locked = vcpus_locked.vcpu2;
69
70 /* Only the primary VM can turn ON a vCPU that is currently OFF. */
71 if (!vm_is_primary(current->vm) &&
72 target_vcpu->state == VCPU_STATE_OFF) {
73 run_ret->arg2 = FFA_DENIED;
74 ret = false;
75 goto out;
76 }
77
78 /*
79 * An SPx can resume another SPy only when SPy is in PREEMPTED or
80 * BLOCKED state.
81 */
82 if (vm_id_is_current_world(current->vm->id) &&
83 vm_id_is_current_world(target_vm_id)) {
84 /* Target SP must be in preempted or blocked state. */
85 if (target_vcpu->state != VCPU_STATE_PREEMPTED &&
86 target_vcpu->state != VCPU_STATE_BLOCKED) {
87 run_ret->arg2 = FFA_DENIED;
88 ret = false;
89 goto out;
90 }
91 }
92
93 /* A SP cannot invoke FFA_RUN to resume a normal world VM. */
94 if (!vm_id_is_current_world(target_vm_id)) {
95 run_ret->arg2 = FFA_DENIED;
96 ret = false;
97 goto out;
98 }
99
Karl Meakin5a365d32024-11-08 23:55:03 +0000100 if (vm_id_is_current_world(current->vm->id)) {
101 /*
102 * Refer FF-A v1.1 EAC0 spec section 8.3.2.2.1
103 * Signaling an Other S-Int in blocked state
104 */
105 if (current->preempted_vcpu != NULL) {
106 /*
107 * After the target SP execution context has handled
108 * the interrupt, it uses the FFA_RUN ABI to resume
109 * the request due to which it had entered the blocked
110 * state earlier.
111 * Deny the state transition if the SP didnt perform the
112 * deactivation of the secure virtual interrupt.
113 */
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000114 if (vcpu_virt_interrupt_count_get(current_locked) > 0) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000115 run_ret->arg2 = FFA_DENIED;
116 ret = false;
117 goto out;
118 }
119
120 /*
121 * Refer Figure 8.13 Scenario 1: Implementation choice:
122 * SPMC left all intermediate SP execution contexts in
123 * blocked state. Hence, SPMC now bypasses the
124 * intermediate these execution contexts and resumes the
125 * SP execution context that was originally preempted.
126 */
127 *next = current->preempted_vcpu;
128 if (target_vcpu != current->preempted_vcpu) {
129 dlog_verbose("Skipping intermediate vCPUs\n");
130 }
Karl Meakin5a365d32024-11-08 23:55:03 +0000131
132 /*
133 * Clear fields corresponding to secure interrupt
134 * handling.
135 */
136 vcpu_secure_interrupt_complete(current_locked);
137 }
138 }
139
140 /* Check if a vCPU of SP is being resumed. */
141 if (vm_id_is_current_world(target_vm_id)) {
142 /*
143 * A call chain cannot span CPUs. The target vCPU can only be
144 * resumed by FFA_RUN on present CPU.
145 */
146 if ((target_vcpu->call_chain.prev_node != NULL ||
147 target_vcpu->call_chain.next_node != NULL) &&
148 (target_vcpu->cpu != current->cpu)) {
149 run_ret->arg2 = FFA_DENIED;
150 ret = false;
151 goto out;
152 }
Karl Meakin5a365d32024-11-08 23:55:03 +0000153 }
154
155out:
156 vcpu_unlock(&target_locked);
157 return ret;
158}
159
160/**
161 * SPMC scheduled call chain is completely unwound.
162 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000163static void ffa_cpu_cycles_exit_spmc_schedule_mode(
164 struct vcpu_locked current_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000165{
166 struct vcpu *current;
167
168 current = current_locked.vcpu;
169 assert(current->call_chain.next_node == NULL);
170 CHECK(current->scheduling_mode == SPMC_MODE);
171
172 current->scheduling_mode = NONE;
173 current->rt_model = RTM_NONE;
174}
175
176/**
177 * A SP in running state could have been pre-empted by a secure interrupt. SPM
178 * would switch the execution to the vCPU of target SP responsible for interupt
179 * handling. Upon completion of interrupt handling, vCPU performs interrupt
180 * signal completion through FFA_MSG_WAIT ABI (provided it was in waiting state
181 * when interrupt was signaled).
182 *
183 * SPM then resumes the original SP that was initially pre-empted.
184 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000185static struct ffa_value ffa_cpu_cycles_preempted_vcpu_resume(
Karl Meakin5a365d32024-11-08 23:55:03 +0000186 struct vcpu_locked current_locked, struct vcpu **next)
187{
188 struct ffa_value ffa_ret = (struct ffa_value){.func = FFA_MSG_WAIT_32};
189 struct vcpu *target_vcpu;
190 struct vcpu *current = current_locked.vcpu;
191 struct vcpu_locked target_locked;
192 struct two_vcpu_locked vcpus_locked;
193
194 CHECK(current->preempted_vcpu != NULL);
195 CHECK(current->preempted_vcpu->state == VCPU_STATE_PREEMPTED);
196
197 target_vcpu = current->preempted_vcpu;
198 vcpu_unlock(&current_locked);
199
200 /* Lock both vCPUs at once to avoid deadlock. */
201 vcpus_locked = vcpu_lock_both(current, target_vcpu);
202 current_locked = vcpus_locked.vcpu1;
203 target_locked = vcpus_locked.vcpu2;
204
205 /* Reset the fields tracking secure interrupt processing. */
206 vcpu_secure_interrupt_complete(current_locked);
207
208 /* SPMC scheduled call chain is completely unwound. */
Karl Meakinca38ef92025-02-13 14:20:23 +0000209 ffa_cpu_cycles_exit_spmc_schedule_mode(current_locked);
Karl Meakin5a365d32024-11-08 23:55:03 +0000210 assert(current->call_chain.prev_node == NULL);
211
212 current->state = VCPU_STATE_WAITING;
213
214 vcpu_set_running(target_locked, NULL);
215
216 vcpu_unlock(&target_locked);
217
218 /* Restore interrupt priority mask. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000219 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000220
221 /* The pre-empted vCPU should be run. */
222 *next = target_vcpu;
223
224 return ffa_ret;
225}
226
227static struct ffa_value ffa_msg_wait_complete(struct vcpu_locked current_locked,
228 struct vcpu **next)
229{
230 struct vcpu *current = current_locked.vcpu;
231
232 current->scheduling_mode = NONE;
233 current->rt_model = RTM_NONE;
234
Daniel Boulbyd7992232025-03-06 17:09:49 +0000235 /*
236 * We no longer need to do a managed exit so clear the interrupt if
237 * needed.
238 */
239 vcpu_virt_interrupt_clear(current_locked, HF_MANAGED_EXIT_INTID);
240
Karl Meakin5a365d32024-11-08 23:55:03 +0000241 /* Relinquish control back to the NWd. */
242 *next = api_switch_to_other_world(
243 current_locked, (struct ffa_value){.func = FFA_MSG_WAIT_32},
244 VCPU_STATE_WAITING);
245
246 return api_ffa_interrupt_return(0);
247}
248
249/**
250 * Deals with the common case of intercepting an FFA_MSG_WAIT call.
251 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000252static bool ffa_cpu_cycles_msg_wait_intercept(struct vcpu_locked current_locked,
253 struct vcpu **next,
254 struct ffa_value *ffa_ret)
Karl Meakin5a365d32024-11-08 23:55:03 +0000255{
256 struct two_vcpu_locked both_vcpu_locks;
257 struct vcpu *current = current_locked.vcpu;
258 bool ret = false;
259
260 assert(next != NULL);
261 assert(*next != NULL);
262
263 vcpu_unlock(&current_locked);
264
265 both_vcpu_locks = vcpu_lock_both(current, *next);
266
267 /*
J-Alves4796d112025-02-12 14:39:00 +0000268 * Check if there is a pending interrupt, and if the partition
269 * is expects to notify the scheduler or resume straight away.
270 * Either trigger SRI for later donation of CPU cycles, or
271 * eret `FFA_INTERRUPT` back to the caller.
Karl Meakin5a365d32024-11-08 23:55:03 +0000272 */
Karl Meakin117c8082024-12-04 16:03:28 +0000273 if (ffa_interrupts_intercept_call(both_vcpu_locks.vcpu1,
274 both_vcpu_locks.vcpu2, ffa_ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000275 *next = NULL;
276 ret = true;
277 }
278
279 vcpu_unlock(&both_vcpu_locks.vcpu2);
280
281 return ret;
282}
283
Karl Meakin936ec1e2025-01-31 13:17:11 +0000284static bool sp_boot_next(struct vcpu_locked current_locked, struct vcpu **next)
285{
286 struct vcpu *vcpu_next = NULL;
287 struct vcpu *current = current_locked.vcpu;
288 struct vm *next_vm;
289 size_t cpu_indx = cpu_index(current->cpu);
290
291 if (current->cpu->last_sp_initialized) {
292 return false;
293 }
294
295 if (!atomic_load_explicit(&current->vm->aborting,
296 memory_order_relaxed)) {
297 /* vCPU has just returned from successful initialization. */
298 dlog_verbose(
299 "Initialized execution context of VM: %#x on CPU: %zu, "
300 "boot_order: %u\n",
301 current->vm->id, cpu_index(current->cpu),
302 current->vm->boot_order);
303 }
304
305 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
306 next_vm = vm_get_next_boot(current->vm);
307 } else {
308 /* SP boot chain on secondary CPU. */
309 next_vm = vm_get_next_boot_secondary_core(current->vm);
310 }
311
312 current->state = VCPU_STATE_WAITING;
313 current->rt_model = RTM_NONE;
314 current->scheduling_mode = NONE;
315
316 /*
317 * Pick next SP's vCPU to be booted. Once all SPs have booted
318 * (next_vm is NULL), then return execution to NWd.
319 */
320 if (next_vm == NULL) {
321 current->cpu->last_sp_initialized = true;
322 goto out;
323 }
324
325 vcpu_next = vm_get_vcpu(next_vm, cpu_indx);
326
327 /*
328 * An SP's execution context needs to be bootstrapped if:
329 * - It has never been initialized before.
330 * - Or it was turned off when the CPU, on which it was pinned, was
331 * powered down.
332 */
333 if (vcpu_next->rt_model == RTM_SP_INIT ||
334 vcpu_next->state == VCPU_STATE_OFF) {
335 vcpu_next->rt_model = RTM_SP_INIT;
336 arch_regs_reset(vcpu_next);
337 vcpu_next->cpu = current->cpu;
338 vcpu_next->state = VCPU_STATE_RUNNING;
339 vcpu_next->regs_available = false;
340 vcpu_set_phys_core_idx(vcpu_next);
341 arch_regs_set_pc_arg(&vcpu_next->regs,
342 vcpu_next->vm->secondary_ep, 0ULL);
343
344 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
345 /*
346 * Boot information is passed by the SPMC to the SP's
347 * execution context only on the primary CPU.
348 */
349 vcpu_set_boot_info_gp_reg(vcpu_next);
350 }
351
352 *next = vcpu_next;
353
354 return true;
355 }
356out:
357 dlog_notice("Finished bootstrapping all SPs on CPU%lx\n", cpu_indx);
358 return false;
359}
360
Karl Meakin5a365d32024-11-08 23:55:03 +0000361/**
362 * The invocation of FFA_MSG_WAIT at secure virtual FF-A instance is compliant
363 * with FF-A v1.1 EAC0 specification. It only performs the state transition
364 * from RUNNING to WAITING for the following Partition runtime models:
365 * RTM_FFA_RUN, RTM_SEC_INTERRUPT, RTM_SP_INIT.
366 */
Karl Meakin117c8082024-12-04 16:03:28 +0000367struct ffa_value ffa_cpu_cycles_msg_wait_prepare(
368 struct vcpu_locked current_locked, struct vcpu **next)
Karl Meakin5a365d32024-11-08 23:55:03 +0000369{
370 struct ffa_value ret = api_ffa_interrupt_return(0);
371 struct vcpu *current = current_locked.vcpu;
372
373 switch (current->rt_model) {
374 case RTM_SP_INIT:
375 if (!sp_boot_next(current_locked, next)) {
376 ret = ffa_msg_wait_complete(current_locked, next);
377
J-Alves1ef5fc42025-02-27 17:15:29 +0000378 ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
379 &ret);
Karl Meakin5a365d32024-11-08 23:55:03 +0000380 }
381 break;
382 case RTM_SEC_INTERRUPT:
383 /*
384 * Either resume the preempted SP or complete the FFA_MSG_WAIT.
385 */
386 assert(current->preempted_vcpu != NULL);
Karl Meakinca38ef92025-02-13 14:20:23 +0000387 ffa_cpu_cycles_preempted_vcpu_resume(current_locked, next);
Karl Meakin5a365d32024-11-08 23:55:03 +0000388
J-Alves1ef5fc42025-02-27 17:15:29 +0000389 if (!ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
390 &ret)) {
391 /*
392 * If CPU cycles were allocated through FFA_RUN
393 * interface, allow the interrupts(if they were masked
394 * earlier) before returning control to NWd.
395 */
396 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000397 }
398
Karl Meakin5a365d32024-11-08 23:55:03 +0000399 break;
400 case RTM_FFA_RUN:
401 ret = ffa_msg_wait_complete(current_locked, next);
402
J-Alves1ef5fc42025-02-27 17:15:29 +0000403 if (!ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
404 &ret)) {
405 /*
406 * If CPU cycles were allocated through FFA_RUN
407 * interface, allow the interrupts(if they were masked
408 * earlier) before returning control to NWd.
409 */
410 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000411 }
412
Karl Meakin5a365d32024-11-08 23:55:03 +0000413 break;
414 default:
415 panic("%s: unexpected runtime model %x for [%x %x]",
416 current->rt_model, current->vm->id,
417 cpu_index(current->cpu));
418 }
419
420 vcpu_unlock(&current_locked);
421
422 return ret;
423}
424
Karl Meakin5a365d32024-11-08 23:55:03 +0000425/*
426 * Initialize the scheduling mode and/or Partition Runtime model of the target
427 * SP upon being resumed by an FFA_RUN ABI.
428 */
J-Alves5d65fe72025-02-18 16:02:32 +0000429void ffa_cpu_cycles_init_schedule_mode_ffa_run(
Karl Meakin117c8082024-12-04 16:03:28 +0000430 struct vcpu_locked current_locked, struct vcpu_locked target_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000431{
432 struct vcpu *vcpu = target_locked.vcpu;
433 struct vcpu *current = current_locked.vcpu;
434
435 /*
436 * Scenario 1 in Table 8.4; Therefore SPMC could be resuming a vCPU
437 * that was part of NWd scheduled mode.
438 */
439 CHECK(vcpu->scheduling_mode != SPMC_MODE);
440
441 /* Section 8.2.3 bullet 4.2 of spec FF-A v1.1 EAC0. */
442 if (vcpu->state == VCPU_STATE_WAITING) {
443 assert(vcpu->rt_model == RTM_SP_INIT ||
444 vcpu->rt_model == RTM_NONE);
445 vcpu->rt_model = RTM_FFA_RUN;
446
447 if (!vm_id_is_current_world(current->vm->id) ||
448 (current->scheduling_mode == NWD_MODE)) {
449 vcpu->scheduling_mode = NWD_MODE;
450 }
451 } else {
452 /* SP vCPU would have been pre-empted earlier or blocked. */
453 CHECK(vcpu->state == VCPU_STATE_PREEMPTED ||
454 vcpu->state == VCPU_STATE_BLOCKED);
455 }
456
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000457 ffa_interrupts_mask(target_locked);
Karl Meakin5a365d32024-11-08 23:55:03 +0000458}
459
460/*
461 * Prepare to yield execution back to the VM/SP that allocated CPU cycles and
462 * move to BLOCKED state. If the CPU cycles were allocated to the current
463 * execution context by the SPMC to handle secure virtual interrupt, then
464 * FFA_YIELD invocation is essentially a no-op.
465 */
Karl Meakin117c8082024-12-04 16:03:28 +0000466struct ffa_value ffa_cpu_cycles_yield_prepare(struct vcpu_locked current_locked,
467 struct vcpu **next,
468 uint32_t timeout_low,
469 uint32_t timeout_high)
Karl Meakin5a365d32024-11-08 23:55:03 +0000470{
471 struct ffa_value ret_args = (struct ffa_value){.func = FFA_SUCCESS_32};
472 struct vcpu *current = current_locked.vcpu;
473 struct ffa_value ret = {
474 .func = FFA_YIELD_32,
475 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
476 .arg2 = timeout_low,
477 .arg3 = timeout_high,
478 };
479
480 switch (current->rt_model) {
481 case RTM_FFA_DIR_REQ:
482 assert(current->direct_request_origin.vm_id !=
483 HF_INVALID_VM_ID);
484 if (current->call_chain.prev_node == NULL) {
485 /*
486 * Relinquish cycles to the NWd VM that sent direct
487 * request message to the current SP.
488 */
489 *next = api_switch_to_other_world(current_locked, ret,
490 VCPU_STATE_BLOCKED);
491 } else {
492 /*
493 * Relinquish cycles to the SP that sent direct request
494 * message to the current SP.
495 */
496 *next = api_switch_to_vm(
497 current_locked, ret, VCPU_STATE_BLOCKED,
498 current->direct_request_origin.vm_id);
499 }
500 break;
501 case RTM_SEC_INTERRUPT: {
502 /*
503 * SPMC does not implement a scheduler needed to resume the
504 * current vCPU upon timeout expiration. Hence, SPMC makes the
505 * implementation defined choice to treat FFA_YIELD invocation
506 * as a no-op if the SP execution context is in the secure
507 * interrupt runtime model. This does not violate FF-A spec as
508 * the spec does not mandate timeout to be honored. Moreover,
509 * timeout specified by an endpoint is just a hint to the
510 * partition manager which allocated CPU cycles.
511 * Resume the current vCPU.
512 */
513 *next = NULL;
514 break;
515 }
516 default:
517 CHECK(current->rt_model == RTM_FFA_RUN);
518 *next = api_switch_to_primary(current_locked, ret,
519 VCPU_STATE_BLOCKED);
520 break;
521 }
522
523 /*
524 * Before yielding CPU cycles, allow the interrupts(if they were
525 * masked earlier).
526 */
527 if (*next != NULL) {
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000528 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000529 }
530
531 return ret_args;
532}
Karl Meakin936ec1e2025-01-31 13:17:11 +0000533
Karl Meakin936ec1e2025-01-31 13:17:11 +0000534/**
535 * Validates the Runtime model for FFA_RUN. Refer to section 7.2 of the FF-A
536 * v1.1 EAC0 spec.
537 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000538static bool ffa_cpu_cycles_check_rtm_ffa_run(struct vcpu_locked current_locked,
539 struct vcpu_locked locked_vcpu,
540 uint32_t func,
541 enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000542{
543 switch (func) {
544 case FFA_MSG_SEND_DIRECT_REQ_64:
545 case FFA_MSG_SEND_DIRECT_REQ_32:
546 case FFA_MSG_SEND_DIRECT_REQ2_64:
547 /* Fall through. */
548 case FFA_RUN_32: {
549 /* Rules 1,2 section 7.2 EAC0 spec. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000550 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
551 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000552 return false;
553 }
554 *next_state = VCPU_STATE_BLOCKED;
555 return true;
556 }
557 case FFA_MSG_WAIT_32:
558 /* Rule 4 section 7.2 EAC0 spec. Fall through. */
559 *next_state = VCPU_STATE_WAITING;
560 return true;
561 case FFA_YIELD_32:
562 /* Rule 5 section 7.2 EAC0 spec. */
563 *next_state = VCPU_STATE_BLOCKED;
564 return true;
565 case FFA_MSG_SEND_DIRECT_RESP_64:
566 case FFA_MSG_SEND_DIRECT_RESP_32:
567 case FFA_MSG_SEND_DIRECT_RESP2_64:
568 /* Rule 3 section 7.2 EAC0 spec. Fall through. */
569 default:
570 /* Deny state transitions by default. */
571 return false;
572 }
573}
574
575/**
576 * Validates the Runtime model for FFA_MSG_SEND_DIRECT_REQ and
577 * FFA_MSG_SEND_DIRECT_REQ2. Refer to section 8.3 of the FF-A
578 * v1.2 spec.
579 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000580static bool ffa_cpu_cycles_check_rtm_ffa_dir_req(
581 struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu,
582 ffa_id_t receiver_vm_id, uint32_t func, enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000583{
584 switch (func) {
585 case FFA_MSG_SEND_DIRECT_REQ_64:
586 case FFA_MSG_SEND_DIRECT_REQ_32:
587 case FFA_MSG_SEND_DIRECT_REQ2_64:
588 /* Fall through. */
589 case FFA_RUN_32: {
590 /* Rules 1,2. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000591 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
592 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000593 return false;
594 }
595
596 *next_state = VCPU_STATE_BLOCKED;
597 return true;
598 }
599 case FFA_MSG_SEND_DIRECT_RESP_64:
600 case FFA_MSG_SEND_DIRECT_RESP_32: {
601 case FFA_MSG_SEND_DIRECT_RESP2_64:
602 /* Rule 3. */
603 if (current_locked.vcpu->direct_request_origin.vm_id ==
604 receiver_vm_id) {
605 *next_state = VCPU_STATE_WAITING;
606 return true;
607 }
608
609 return false;
610 }
611 case FFA_YIELD_32:
612 /* Rule 3, section 8.3 of FF-A v1.2 spec. */
613 *next_state = VCPU_STATE_BLOCKED;
614 return true;
615 case FFA_MSG_WAIT_32:
616 /* Rule 4. Fall through. */
617 default:
618 /* Deny state transitions by default. */
619 return false;
620 }
621}
622
623/**
624 * Validates the Runtime model for Secure interrupt handling. Refer to section
625 * 8.4 of the FF-A v1.2 ALP0 spec.
626 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000627static bool ffa_cpu_cycles_check_rtm_sec_interrupt(
628 struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu,
629 uint32_t func, enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000630{
631 struct vcpu *current = current_locked.vcpu;
632 struct vcpu *vcpu = locked_vcpu.vcpu;
633
634 CHECK(current->scheduling_mode == SPMC_MODE);
635
636 switch (func) {
637 case FFA_MSG_SEND_DIRECT_REQ_64:
638 case FFA_MSG_SEND_DIRECT_REQ_32:
639 case FFA_MSG_SEND_DIRECT_REQ2_64:
640 /* Rule 3. */
641 *next_state = VCPU_STATE_BLOCKED;
642 return true;
643 case FFA_RUN_32: {
644 /* Rule 6. */
645 if (vcpu->state == VCPU_STATE_PREEMPTED) {
646 *next_state = VCPU_STATE_BLOCKED;
647 return true;
648 }
649
650 return false;
651 }
652 case FFA_MSG_WAIT_32:
653 /* Rule 2. */
654 *next_state = VCPU_STATE_WAITING;
655 return true;
656 case FFA_YIELD_32:
657 /* Rule 3, section 8.4 of FF-A v1.2 spec. */
658 *next_state = VCPU_STATE_BLOCKED;
659 return true;
660 case FFA_MSG_SEND_DIRECT_RESP_64:
661 case FFA_MSG_SEND_DIRECT_RESP_32:
662 case FFA_MSG_SEND_DIRECT_RESP2_64:
663 /* Rule 5. Fall through. */
664 default:
665 /* Deny state transitions by default. */
666 return false;
667 }
668}
669
670/**
671 * Validates the Runtime model for SP initialization. Refer to section
672 * 8.3 of the FF-A v1.2 ALP0 spec.
673 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000674static bool ffa_cpu_cycles_check_rtm_sp_init(struct vcpu_locked locked_vcpu,
675 uint32_t func,
676 enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000677{
678 switch (func) {
679 case FFA_MSG_SEND_DIRECT_REQ_64:
680 case FFA_MSG_SEND_DIRECT_REQ_32:
681 case FFA_MSG_SEND_DIRECT_REQ2_64: {
682 struct vcpu *vcpu = locked_vcpu.vcpu;
683
684 assert(vcpu != NULL);
685 /* Rule 1. */
686 if (vcpu->rt_model != RTM_SP_INIT) {
687 *next_state = VCPU_STATE_BLOCKED;
688 return true;
689 }
690
691 return false;
692 }
693 case FFA_MSG_WAIT_32:
694 /* Rule 2. Fall through. */
695 case FFA_ERROR_32:
696 /* Rule 3. */
697 *next_state = VCPU_STATE_WAITING;
698 return true;
699 case FFA_YIELD_32:
700 /* Rule 4. Fall through. */
701 case FFA_RUN_32:
702 /* Rule 6. Fall through. */
703 case FFA_MSG_SEND_DIRECT_RESP_64:
704 case FFA_MSG_SEND_DIRECT_RESP_32:
705 case FFA_MSG_SEND_DIRECT_RESP2_64:
706 /* Rule 5. Fall through. */
707 default:
708 /* Deny state transitions by default. */
709 return false;
710 }
711}
712
713/**
714 * Check if the runtime model (state machine) of the current SP supports the
715 * given FF-A ABI invocation. If yes, next_state represents the state to which
716 * the current vcpu would transition upon the FF-A ABI invocation as determined
717 * by the Partition runtime model.
718 */
719bool ffa_cpu_cycles_check_runtime_state_transition(
720 struct vcpu_locked current_locked, ffa_id_t vm_id,
721 ffa_id_t receiver_vm_id, struct vcpu_locked locked_vcpu, uint32_t func,
722 enum vcpu_state *next_state)
723{
724 bool allowed = false;
725 struct vcpu *current = current_locked.vcpu;
726
727 assert(current != NULL);
728
729 /* Perform state transition checks only for Secure Partitions. */
730 if (!vm_id_is_current_world(vm_id)) {
731 return true;
732 }
733
734 switch (current->rt_model) {
735 case RTM_FFA_RUN:
Karl Meakinca38ef92025-02-13 14:20:23 +0000736 allowed = ffa_cpu_cycles_check_rtm_ffa_run(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000737 current_locked, locked_vcpu, func, next_state);
738 break;
739 case RTM_FFA_DIR_REQ:
Karl Meakinca38ef92025-02-13 14:20:23 +0000740 allowed = ffa_cpu_cycles_check_rtm_ffa_dir_req(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000741 current_locked, locked_vcpu, receiver_vm_id, func,
742 next_state);
743 break;
744 case RTM_SEC_INTERRUPT:
Karl Meakinca38ef92025-02-13 14:20:23 +0000745 allowed = ffa_cpu_cycles_check_rtm_sec_interrupt(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000746 current_locked, locked_vcpu, func, next_state);
747 break;
748 case RTM_SP_INIT:
Karl Meakinca38ef92025-02-13 14:20:23 +0000749 allowed = ffa_cpu_cycles_check_rtm_sp_init(locked_vcpu, func,
750 next_state);
Karl Meakin936ec1e2025-01-31 13:17:11 +0000751 break;
752 default:
753 dlog_error(
754 "Illegal Runtime Model specified by SP%x on CPU%zx\n",
755 current->vm->id, cpu_index(current->cpu));
756 allowed = false;
757 break;
758 }
759
760 if (!allowed) {
761 dlog_verbose("State transition denied\n");
762 }
763
764 return allowed;
765}
766
767/*
768 * Handle FFA_ERROR_32 call according to the given error code.
769 *
770 * Error codes other than FFA_ABORTED, and cases of FFA_ABORTED not
771 * in RTM_SP_INIT runtime model, not implemented. Refer to section 8.5
772 * of FF-A 1.2 spec.
773 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000774struct ffa_value ffa_cpu_cycles_error_32(struct vcpu *current,
775 struct vcpu **next,
776 enum ffa_error error_code)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000777{
778 struct vcpu_locked current_locked;
779 struct vm_locked vm_locked;
780 enum partition_runtime_model rt_model;
781 struct ffa_value ret = api_ffa_interrupt_return(0);
782
783 vm_locked = vm_lock(current->vm);
784 current_locked = vcpu_lock(current);
785 rt_model = current_locked.vcpu->rt_model;
786
787 if (error_code == FFA_ABORTED && rt_model == RTM_SP_INIT) {
788 dlog_error("Aborting SP %#x from vCPU %u\n", current->vm->id,
789 vcpu_index(current));
790
791 atomic_store_explicit(&current->vm->aborting, true,
792 memory_order_relaxed);
793
794 ffa_vm_free_resources(vm_locked);
795
796 if (sp_boot_next(current_locked, next)) {
797 goto out;
798 }
799
800 /*
801 * Relinquish control back to the NWd. Return
802 * FFA_MSG_WAIT_32 to indicate to SPMD that SPMC
803 * has successfully finished initialization.
804 */
805 *next = api_switch_to_other_world(
806 current_locked,
807 (struct ffa_value){.func = FFA_MSG_WAIT_32},
808 VCPU_STATE_ABORTED);
809
810 goto out;
811 }
812 ret = ffa_error(FFA_NOT_SUPPORTED);
813out:
814 vcpu_unlock(&current_locked);
815 vm_unlock(&vm_locked);
816 return ret;
817}