blob: 645a8e8565de18e2fd2c8413b2f096f78efaab04 [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 */
114 if (!vcpu_is_interrupt_queue_empty(current_locked)) {
115 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 }
131 /*
132 * This flag should not have been set by SPMC when it
133 * signaled the virtual interrupt to the SP while SP was
134 * in WAITING or BLOCKED states. Refer the embedded
135 * comment in vcpu.h file for further description.
136 */
137 assert(!current->requires_deactivate_call);
138
139 /*
140 * Clear fields corresponding to secure interrupt
141 * handling.
142 */
143 vcpu_secure_interrupt_complete(current_locked);
144 }
145 }
146
147 /* Check if a vCPU of SP is being resumed. */
148 if (vm_id_is_current_world(target_vm_id)) {
149 /*
150 * A call chain cannot span CPUs. The target vCPU can only be
151 * resumed by FFA_RUN on present CPU.
152 */
153 if ((target_vcpu->call_chain.prev_node != NULL ||
154 target_vcpu->call_chain.next_node != NULL) &&
155 (target_vcpu->cpu != current->cpu)) {
156 run_ret->arg2 = FFA_DENIED;
157 ret = false;
158 goto out;
159 }
160
161 if (!vcpu_is_interrupt_queue_empty(target_locked)) {
162 /*
163 * Consider the following scenarios: a secure interrupt
164 * triggered in normal world and is targeted to an SP.
165 * Scenario A): The target SP's vCPU was preempted by a
166 * non secure interrupt.
167 * Scenario B): The target SP's vCPU was in blocked
168 * state after it yielded CPU cycles to
169 * normal world using FFA_YIELD.
170 * In both the scenarios, SPMC would have injected a
171 * virtual interrupt and set the appropriate flags after
172 * de-activating the secure physical interrupt. SPMC did
173 * not resume the target vCPU at that moment.
174 */
175 assert(target_vcpu->state == VCPU_STATE_PREEMPTED ||
176 target_vcpu->state == VCPU_STATE_BLOCKED);
177 assert(vcpu_interrupt_count_get(target_locked) > 0);
178
179 /*
180 * This check is to ensure the target SP vCPU could
181 * only be a part of NWd scheduled call chain. FF-A v1.1
182 * spec prohibits an SPMC scheduled call chain to be
183 * preempted by a non secure interrupt.
184 */
185 CHECK(target_vcpu->scheduling_mode == NWD_MODE);
186 }
187 }
188
189out:
190 vcpu_unlock(&target_locked);
191 return ret;
192}
193
194/**
195 * SPMC scheduled call chain is completely unwound.
196 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000197static void ffa_cpu_cycles_exit_spmc_schedule_mode(
198 struct vcpu_locked current_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000199{
200 struct vcpu *current;
201
202 current = current_locked.vcpu;
203 assert(current->call_chain.next_node == NULL);
204 CHECK(current->scheduling_mode == SPMC_MODE);
205
206 current->scheduling_mode = NONE;
207 current->rt_model = RTM_NONE;
208}
209
210/**
211 * A SP in running state could have been pre-empted by a secure interrupt. SPM
212 * would switch the execution to the vCPU of target SP responsible for interupt
213 * handling. Upon completion of interrupt handling, vCPU performs interrupt
214 * signal completion through FFA_MSG_WAIT ABI (provided it was in waiting state
215 * when interrupt was signaled).
216 *
217 * SPM then resumes the original SP that was initially pre-empted.
218 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000219static struct ffa_value ffa_cpu_cycles_preempted_vcpu_resume(
Karl Meakin5a365d32024-11-08 23:55:03 +0000220 struct vcpu_locked current_locked, struct vcpu **next)
221{
222 struct ffa_value ffa_ret = (struct ffa_value){.func = FFA_MSG_WAIT_32};
223 struct vcpu *target_vcpu;
224 struct vcpu *current = current_locked.vcpu;
225 struct vcpu_locked target_locked;
226 struct two_vcpu_locked vcpus_locked;
227
228 CHECK(current->preempted_vcpu != NULL);
229 CHECK(current->preempted_vcpu->state == VCPU_STATE_PREEMPTED);
230
231 target_vcpu = current->preempted_vcpu;
232 vcpu_unlock(&current_locked);
233
234 /* Lock both vCPUs at once to avoid deadlock. */
235 vcpus_locked = vcpu_lock_both(current, target_vcpu);
236 current_locked = vcpus_locked.vcpu1;
237 target_locked = vcpus_locked.vcpu2;
238
239 /* Reset the fields tracking secure interrupt processing. */
240 vcpu_secure_interrupt_complete(current_locked);
241
242 /* SPMC scheduled call chain is completely unwound. */
Karl Meakinca38ef92025-02-13 14:20:23 +0000243 ffa_cpu_cycles_exit_spmc_schedule_mode(current_locked);
Karl Meakin5a365d32024-11-08 23:55:03 +0000244 assert(current->call_chain.prev_node == NULL);
245
246 current->state = VCPU_STATE_WAITING;
247
248 vcpu_set_running(target_locked, NULL);
249
250 vcpu_unlock(&target_locked);
251
252 /* Restore interrupt priority mask. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000253 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000254
255 /* The pre-empted vCPU should be run. */
256 *next = target_vcpu;
257
258 return ffa_ret;
259}
260
261static struct ffa_value ffa_msg_wait_complete(struct vcpu_locked current_locked,
262 struct vcpu **next)
263{
264 struct vcpu *current = current_locked.vcpu;
265
266 current->scheduling_mode = NONE;
267 current->rt_model = RTM_NONE;
268
269 /* Relinquish control back to the NWd. */
270 *next = api_switch_to_other_world(
271 current_locked, (struct ffa_value){.func = FFA_MSG_WAIT_32},
272 VCPU_STATE_WAITING);
273
274 return api_ffa_interrupt_return(0);
275}
276
277/**
278 * Deals with the common case of intercepting an FFA_MSG_WAIT call.
279 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000280static bool ffa_cpu_cycles_msg_wait_intercept(struct vcpu_locked current_locked,
281 struct vcpu **next,
282 struct ffa_value *ffa_ret)
Karl Meakin5a365d32024-11-08 23:55:03 +0000283{
284 struct two_vcpu_locked both_vcpu_locks;
285 struct vcpu *current = current_locked.vcpu;
286 bool ret = false;
287
288 assert(next != NULL);
289 assert(*next != NULL);
290
291 vcpu_unlock(&current_locked);
292
293 both_vcpu_locks = vcpu_lock_both(current, *next);
294
295 /*
296 * Check if there are any pending secure virtual interrupts to
297 * be handled. The `next` should have a pointer to the current
298 * vCPU. Intercept call will set `ret` to FFA_INTERRUPT and the
299 * respective interrupt id.
300 */
Karl Meakin117c8082024-12-04 16:03:28 +0000301 if (ffa_interrupts_intercept_call(both_vcpu_locks.vcpu1,
302 both_vcpu_locks.vcpu2, ffa_ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000303 *next = NULL;
304 ret = true;
305 }
306
307 vcpu_unlock(&both_vcpu_locks.vcpu2);
308
309 return ret;
310}
311
Karl Meakin936ec1e2025-01-31 13:17:11 +0000312static bool sp_boot_next(struct vcpu_locked current_locked, struct vcpu **next)
313{
314 struct vcpu *vcpu_next = NULL;
315 struct vcpu *current = current_locked.vcpu;
316 struct vm *next_vm;
317 size_t cpu_indx = cpu_index(current->cpu);
318
319 if (current->cpu->last_sp_initialized) {
320 return false;
321 }
322
323 if (!atomic_load_explicit(&current->vm->aborting,
324 memory_order_relaxed)) {
325 /* vCPU has just returned from successful initialization. */
326 dlog_verbose(
327 "Initialized execution context of VM: %#x on CPU: %zu, "
328 "boot_order: %u\n",
329 current->vm->id, cpu_index(current->cpu),
330 current->vm->boot_order);
331 }
332
333 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
334 next_vm = vm_get_next_boot(current->vm);
335 } else {
336 /* SP boot chain on secondary CPU. */
337 next_vm = vm_get_next_boot_secondary_core(current->vm);
338 }
339
340 current->state = VCPU_STATE_WAITING;
341 current->rt_model = RTM_NONE;
342 current->scheduling_mode = NONE;
343
344 /*
345 * Pick next SP's vCPU to be booted. Once all SPs have booted
346 * (next_vm is NULL), then return execution to NWd.
347 */
348 if (next_vm == NULL) {
349 current->cpu->last_sp_initialized = true;
350 goto out;
351 }
352
353 vcpu_next = vm_get_vcpu(next_vm, cpu_indx);
354
355 /*
356 * An SP's execution context needs to be bootstrapped if:
357 * - It has never been initialized before.
358 * - Or it was turned off when the CPU, on which it was pinned, was
359 * powered down.
360 */
361 if (vcpu_next->rt_model == RTM_SP_INIT ||
362 vcpu_next->state == VCPU_STATE_OFF) {
363 vcpu_next->rt_model = RTM_SP_INIT;
364 arch_regs_reset(vcpu_next);
365 vcpu_next->cpu = current->cpu;
366 vcpu_next->state = VCPU_STATE_RUNNING;
367 vcpu_next->regs_available = false;
368 vcpu_set_phys_core_idx(vcpu_next);
369 arch_regs_set_pc_arg(&vcpu_next->regs,
370 vcpu_next->vm->secondary_ep, 0ULL);
371
372 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
373 /*
374 * Boot information is passed by the SPMC to the SP's
375 * execution context only on the primary CPU.
376 */
377 vcpu_set_boot_info_gp_reg(vcpu_next);
378 }
379
380 *next = vcpu_next;
381
382 return true;
383 }
384out:
385 dlog_notice("Finished bootstrapping all SPs on CPU%lx\n", cpu_indx);
386 return false;
387}
388
Karl Meakin5a365d32024-11-08 23:55:03 +0000389/**
390 * The invocation of FFA_MSG_WAIT at secure virtual FF-A instance is compliant
391 * with FF-A v1.1 EAC0 specification. It only performs the state transition
392 * from RUNNING to WAITING for the following Partition runtime models:
393 * RTM_FFA_RUN, RTM_SEC_INTERRUPT, RTM_SP_INIT.
394 */
Karl Meakin117c8082024-12-04 16:03:28 +0000395struct ffa_value ffa_cpu_cycles_msg_wait_prepare(
396 struct vcpu_locked current_locked, struct vcpu **next)
Karl Meakin5a365d32024-11-08 23:55:03 +0000397{
398 struct ffa_value ret = api_ffa_interrupt_return(0);
399 struct vcpu *current = current_locked.vcpu;
400
401 switch (current->rt_model) {
402 case RTM_SP_INIT:
403 if (!sp_boot_next(current_locked, next)) {
404 ret = ffa_msg_wait_complete(current_locked, next);
405
Karl Meakinca38ef92025-02-13 14:20:23 +0000406 if (ffa_cpu_cycles_msg_wait_intercept(current_locked,
407 next, &ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000408 }
409 }
410 break;
411 case RTM_SEC_INTERRUPT:
412 /*
413 * Either resume the preempted SP or complete the FFA_MSG_WAIT.
414 */
415 assert(current->preempted_vcpu != NULL);
Karl Meakinca38ef92025-02-13 14:20:23 +0000416 ffa_cpu_cycles_preempted_vcpu_resume(current_locked, next);
Karl Meakin5a365d32024-11-08 23:55:03 +0000417
Karl Meakinca38ef92025-02-13 14:20:23 +0000418 if (ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
419 &ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000420 break;
421 }
422
423 /*
424 * If CPU cycles were allocated through FFA_RUN interface,
425 * allow the interrupts(if they were masked earlier) before
426 * returning control to NWd.
427 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000428 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000429 break;
430 case RTM_FFA_RUN:
431 ret = ffa_msg_wait_complete(current_locked, next);
432
Karl Meakinca38ef92025-02-13 14:20:23 +0000433 if (ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
434 &ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000435 break;
436 }
437
438 /*
439 * If CPU cycles were allocated through FFA_RUN interface,
440 * allow the interrupts(if they were masked earlier) before
441 * returning control to NWd.
442 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000443 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000444
445 break;
446 default:
447 panic("%s: unexpected runtime model %x for [%x %x]",
448 current->rt_model, current->vm->id,
449 cpu_index(current->cpu));
450 }
451
452 vcpu_unlock(&current_locked);
453
454 return ret;
455}
456
Karl Meakin5a365d32024-11-08 23:55:03 +0000457/*
458 * Initialize the scheduling mode and/or Partition Runtime model of the target
459 * SP upon being resumed by an FFA_RUN ABI.
460 */
Karl Meakin117c8082024-12-04 16:03:28 +0000461void ffa_cpu_cycles_init_schedule_mode_ffa_runeld_prepare(
462 struct vcpu_locked current_locked, struct vcpu_locked target_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000463{
464 struct vcpu *vcpu = target_locked.vcpu;
465 struct vcpu *current = current_locked.vcpu;
466
467 /*
468 * Scenario 1 in Table 8.4; Therefore SPMC could be resuming a vCPU
469 * that was part of NWd scheduled mode.
470 */
471 CHECK(vcpu->scheduling_mode != SPMC_MODE);
472
473 /* Section 8.2.3 bullet 4.2 of spec FF-A v1.1 EAC0. */
474 if (vcpu->state == VCPU_STATE_WAITING) {
475 assert(vcpu->rt_model == RTM_SP_INIT ||
476 vcpu->rt_model == RTM_NONE);
477 vcpu->rt_model = RTM_FFA_RUN;
478
479 if (!vm_id_is_current_world(current->vm->id) ||
480 (current->scheduling_mode == NWD_MODE)) {
481 vcpu->scheduling_mode = NWD_MODE;
482 }
483 } else {
484 /* SP vCPU would have been pre-empted earlier or blocked. */
485 CHECK(vcpu->state == VCPU_STATE_PREEMPTED ||
486 vcpu->state == VCPU_STATE_BLOCKED);
487 }
488
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000489 ffa_interrupts_mask(target_locked);
Karl Meakin5a365d32024-11-08 23:55:03 +0000490}
491
492/*
493 * Prepare to yield execution back to the VM/SP that allocated CPU cycles and
494 * move to BLOCKED state. If the CPU cycles were allocated to the current
495 * execution context by the SPMC to handle secure virtual interrupt, then
496 * FFA_YIELD invocation is essentially a no-op.
497 */
Karl Meakin117c8082024-12-04 16:03:28 +0000498struct ffa_value ffa_cpu_cycles_yield_prepare(struct vcpu_locked current_locked,
499 struct vcpu **next,
500 uint32_t timeout_low,
501 uint32_t timeout_high)
Karl Meakin5a365d32024-11-08 23:55:03 +0000502{
503 struct ffa_value ret_args = (struct ffa_value){.func = FFA_SUCCESS_32};
504 struct vcpu *current = current_locked.vcpu;
505 struct ffa_value ret = {
506 .func = FFA_YIELD_32,
507 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
508 .arg2 = timeout_low,
509 .arg3 = timeout_high,
510 };
511
512 switch (current->rt_model) {
513 case RTM_FFA_DIR_REQ:
514 assert(current->direct_request_origin.vm_id !=
515 HF_INVALID_VM_ID);
516 if (current->call_chain.prev_node == NULL) {
517 /*
518 * Relinquish cycles to the NWd VM that sent direct
519 * request message to the current SP.
520 */
521 *next = api_switch_to_other_world(current_locked, ret,
522 VCPU_STATE_BLOCKED);
523 } else {
524 /*
525 * Relinquish cycles to the SP that sent direct request
526 * message to the current SP.
527 */
528 *next = api_switch_to_vm(
529 current_locked, ret, VCPU_STATE_BLOCKED,
530 current->direct_request_origin.vm_id);
531 }
532 break;
533 case RTM_SEC_INTERRUPT: {
534 /*
535 * SPMC does not implement a scheduler needed to resume the
536 * current vCPU upon timeout expiration. Hence, SPMC makes the
537 * implementation defined choice to treat FFA_YIELD invocation
538 * as a no-op if the SP execution context is in the secure
539 * interrupt runtime model. This does not violate FF-A spec as
540 * the spec does not mandate timeout to be honored. Moreover,
541 * timeout specified by an endpoint is just a hint to the
542 * partition manager which allocated CPU cycles.
543 * Resume the current vCPU.
544 */
545 *next = NULL;
546 break;
547 }
548 default:
549 CHECK(current->rt_model == RTM_FFA_RUN);
550 *next = api_switch_to_primary(current_locked, ret,
551 VCPU_STATE_BLOCKED);
552 break;
553 }
554
555 /*
556 * Before yielding CPU cycles, allow the interrupts(if they were
557 * masked earlier).
558 */
559 if (*next != NULL) {
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000560 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000561 }
562
563 return ret_args;
564}
Karl Meakin936ec1e2025-01-31 13:17:11 +0000565
Karl Meakin936ec1e2025-01-31 13:17:11 +0000566/**
567 * Validates the Runtime model for FFA_RUN. Refer to section 7.2 of the FF-A
568 * v1.1 EAC0 spec.
569 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000570static bool ffa_cpu_cycles_check_rtm_ffa_run(struct vcpu_locked current_locked,
571 struct vcpu_locked locked_vcpu,
572 uint32_t func,
573 enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000574{
575 switch (func) {
576 case FFA_MSG_SEND_DIRECT_REQ_64:
577 case FFA_MSG_SEND_DIRECT_REQ_32:
578 case FFA_MSG_SEND_DIRECT_REQ2_64:
579 /* Fall through. */
580 case FFA_RUN_32: {
581 /* Rules 1,2 section 7.2 EAC0 spec. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000582 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
583 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000584 return false;
585 }
586 *next_state = VCPU_STATE_BLOCKED;
587 return true;
588 }
589 case FFA_MSG_WAIT_32:
590 /* Rule 4 section 7.2 EAC0 spec. Fall through. */
591 *next_state = VCPU_STATE_WAITING;
592 return true;
593 case FFA_YIELD_32:
594 /* Rule 5 section 7.2 EAC0 spec. */
595 *next_state = VCPU_STATE_BLOCKED;
596 return true;
597 case FFA_MSG_SEND_DIRECT_RESP_64:
598 case FFA_MSG_SEND_DIRECT_RESP_32:
599 case FFA_MSG_SEND_DIRECT_RESP2_64:
600 /* Rule 3 section 7.2 EAC0 spec. Fall through. */
601 default:
602 /* Deny state transitions by default. */
603 return false;
604 }
605}
606
607/**
608 * Validates the Runtime model for FFA_MSG_SEND_DIRECT_REQ and
609 * FFA_MSG_SEND_DIRECT_REQ2. Refer to section 8.3 of the FF-A
610 * v1.2 spec.
611 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000612static bool ffa_cpu_cycles_check_rtm_ffa_dir_req(
613 struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu,
614 ffa_id_t receiver_vm_id, uint32_t func, enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000615{
616 switch (func) {
617 case FFA_MSG_SEND_DIRECT_REQ_64:
618 case FFA_MSG_SEND_DIRECT_REQ_32:
619 case FFA_MSG_SEND_DIRECT_REQ2_64:
620 /* Fall through. */
621 case FFA_RUN_32: {
622 /* Rules 1,2. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000623 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
624 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000625 return false;
626 }
627
628 *next_state = VCPU_STATE_BLOCKED;
629 return true;
630 }
631 case FFA_MSG_SEND_DIRECT_RESP_64:
632 case FFA_MSG_SEND_DIRECT_RESP_32: {
633 case FFA_MSG_SEND_DIRECT_RESP2_64:
634 /* Rule 3. */
635 if (current_locked.vcpu->direct_request_origin.vm_id ==
636 receiver_vm_id) {
637 *next_state = VCPU_STATE_WAITING;
638 return true;
639 }
640
641 return false;
642 }
643 case FFA_YIELD_32:
644 /* Rule 3, section 8.3 of FF-A v1.2 spec. */
645 *next_state = VCPU_STATE_BLOCKED;
646 return true;
647 case FFA_MSG_WAIT_32:
648 /* Rule 4. Fall through. */
649 default:
650 /* Deny state transitions by default. */
651 return false;
652 }
653}
654
655/**
656 * Validates the Runtime model for Secure interrupt handling. Refer to section
657 * 8.4 of the FF-A v1.2 ALP0 spec.
658 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000659static bool ffa_cpu_cycles_check_rtm_sec_interrupt(
660 struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu,
661 uint32_t func, enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000662{
663 struct vcpu *current = current_locked.vcpu;
664 struct vcpu *vcpu = locked_vcpu.vcpu;
665
666 CHECK(current->scheduling_mode == SPMC_MODE);
667
668 switch (func) {
669 case FFA_MSG_SEND_DIRECT_REQ_64:
670 case FFA_MSG_SEND_DIRECT_REQ_32:
671 case FFA_MSG_SEND_DIRECT_REQ2_64:
672 /* Rule 3. */
673 *next_state = VCPU_STATE_BLOCKED;
674 return true;
675 case FFA_RUN_32: {
676 /* Rule 6. */
677 if (vcpu->state == VCPU_STATE_PREEMPTED) {
678 *next_state = VCPU_STATE_BLOCKED;
679 return true;
680 }
681
682 return false;
683 }
684 case FFA_MSG_WAIT_32:
685 /* Rule 2. */
686 *next_state = VCPU_STATE_WAITING;
687 return true;
688 case FFA_YIELD_32:
689 /* Rule 3, section 8.4 of FF-A v1.2 spec. */
690 *next_state = VCPU_STATE_BLOCKED;
691 return true;
692 case FFA_MSG_SEND_DIRECT_RESP_64:
693 case FFA_MSG_SEND_DIRECT_RESP_32:
694 case FFA_MSG_SEND_DIRECT_RESP2_64:
695 /* Rule 5. Fall through. */
696 default:
697 /* Deny state transitions by default. */
698 return false;
699 }
700}
701
702/**
703 * Validates the Runtime model for SP initialization. Refer to section
704 * 8.3 of the FF-A v1.2 ALP0 spec.
705 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000706static bool ffa_cpu_cycles_check_rtm_sp_init(struct vcpu_locked locked_vcpu,
707 uint32_t func,
708 enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000709{
710 switch (func) {
711 case FFA_MSG_SEND_DIRECT_REQ_64:
712 case FFA_MSG_SEND_DIRECT_REQ_32:
713 case FFA_MSG_SEND_DIRECT_REQ2_64: {
714 struct vcpu *vcpu = locked_vcpu.vcpu;
715
716 assert(vcpu != NULL);
717 /* Rule 1. */
718 if (vcpu->rt_model != RTM_SP_INIT) {
719 *next_state = VCPU_STATE_BLOCKED;
720 return true;
721 }
722
723 return false;
724 }
725 case FFA_MSG_WAIT_32:
726 /* Rule 2. Fall through. */
727 case FFA_ERROR_32:
728 /* Rule 3. */
729 *next_state = VCPU_STATE_WAITING;
730 return true;
731 case FFA_YIELD_32:
732 /* Rule 4. Fall through. */
733 case FFA_RUN_32:
734 /* Rule 6. Fall through. */
735 case FFA_MSG_SEND_DIRECT_RESP_64:
736 case FFA_MSG_SEND_DIRECT_RESP_32:
737 case FFA_MSG_SEND_DIRECT_RESP2_64:
738 /* Rule 5. Fall through. */
739 default:
740 /* Deny state transitions by default. */
741 return false;
742 }
743}
744
745/**
746 * Check if the runtime model (state machine) of the current SP supports the
747 * given FF-A ABI invocation. If yes, next_state represents the state to which
748 * the current vcpu would transition upon the FF-A ABI invocation as determined
749 * by the Partition runtime model.
750 */
751bool ffa_cpu_cycles_check_runtime_state_transition(
752 struct vcpu_locked current_locked, ffa_id_t vm_id,
753 ffa_id_t receiver_vm_id, struct vcpu_locked locked_vcpu, uint32_t func,
754 enum vcpu_state *next_state)
755{
756 bool allowed = false;
757 struct vcpu *current = current_locked.vcpu;
758
759 assert(current != NULL);
760
761 /* Perform state transition checks only for Secure Partitions. */
762 if (!vm_id_is_current_world(vm_id)) {
763 return true;
764 }
765
766 switch (current->rt_model) {
767 case RTM_FFA_RUN:
Karl Meakinca38ef92025-02-13 14:20:23 +0000768 allowed = ffa_cpu_cycles_check_rtm_ffa_run(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000769 current_locked, locked_vcpu, func, next_state);
770 break;
771 case RTM_FFA_DIR_REQ:
Karl Meakinca38ef92025-02-13 14:20:23 +0000772 allowed = ffa_cpu_cycles_check_rtm_ffa_dir_req(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000773 current_locked, locked_vcpu, receiver_vm_id, func,
774 next_state);
775 break;
776 case RTM_SEC_INTERRUPT:
Karl Meakinca38ef92025-02-13 14:20:23 +0000777 allowed = ffa_cpu_cycles_check_rtm_sec_interrupt(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000778 current_locked, locked_vcpu, func, next_state);
779 break;
780 case RTM_SP_INIT:
Karl Meakinca38ef92025-02-13 14:20:23 +0000781 allowed = ffa_cpu_cycles_check_rtm_sp_init(locked_vcpu, func,
782 next_state);
Karl Meakin936ec1e2025-01-31 13:17:11 +0000783 break;
784 default:
785 dlog_error(
786 "Illegal Runtime Model specified by SP%x on CPU%zx\n",
787 current->vm->id, cpu_index(current->cpu));
788 allowed = false;
789 break;
790 }
791
792 if (!allowed) {
793 dlog_verbose("State transition denied\n");
794 }
795
796 return allowed;
797}
798
799/*
800 * Handle FFA_ERROR_32 call according to the given error code.
801 *
802 * Error codes other than FFA_ABORTED, and cases of FFA_ABORTED not
803 * in RTM_SP_INIT runtime model, not implemented. Refer to section 8.5
804 * of FF-A 1.2 spec.
805 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000806struct ffa_value ffa_cpu_cycles_error_32(struct vcpu *current,
807 struct vcpu **next,
808 enum ffa_error error_code)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000809{
810 struct vcpu_locked current_locked;
811 struct vm_locked vm_locked;
812 enum partition_runtime_model rt_model;
813 struct ffa_value ret = api_ffa_interrupt_return(0);
814
815 vm_locked = vm_lock(current->vm);
816 current_locked = vcpu_lock(current);
817 rt_model = current_locked.vcpu->rt_model;
818
819 if (error_code == FFA_ABORTED && rt_model == RTM_SP_INIT) {
820 dlog_error("Aborting SP %#x from vCPU %u\n", current->vm->id,
821 vcpu_index(current));
822
823 atomic_store_explicit(&current->vm->aborting, true,
824 memory_order_relaxed);
825
826 ffa_vm_free_resources(vm_locked);
827
828 if (sp_boot_next(current_locked, next)) {
829 goto out;
830 }
831
832 /*
833 * Relinquish control back to the NWd. Return
834 * FFA_MSG_WAIT_32 to indicate to SPMD that SPMC
835 * has successfully finished initialization.
836 */
837 *next = api_switch_to_other_world(
838 current_locked,
839 (struct ffa_value){.func = FFA_MSG_WAIT_32},
840 VCPU_STATE_ABORTED);
841
842 goto out;
843 }
844 ret = ffa_error(FFA_NOT_SUPPORTED);
845out:
846 vcpu_unlock(&current_locked);
847 vm_unlock(&vm_locked);
848 return ret;
849}