blob: 8dfd8236ba59d7188ea3ed8bdc69ae11397a4843 [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
J-Alves4c334a32025-03-18 11:49:26 +0000227static void ffa_msg_wait_complete(struct vcpu_locked current_locked,
228 struct vcpu **next)
Karl Meakin5a365d32024-11-08 23:55:03 +0000229{
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);
Karl Meakin5a365d32024-11-08 23:55:03 +0000245}
246
247/**
248 * Deals with the common case of intercepting an FFA_MSG_WAIT call.
249 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000250static bool ffa_cpu_cycles_msg_wait_intercept(struct vcpu_locked current_locked,
251 struct vcpu **next,
252 struct ffa_value *ffa_ret)
Karl Meakin5a365d32024-11-08 23:55:03 +0000253{
254 struct two_vcpu_locked both_vcpu_locks;
255 struct vcpu *current = current_locked.vcpu;
256 bool ret = false;
257
258 assert(next != NULL);
259 assert(*next != NULL);
260
261 vcpu_unlock(&current_locked);
262
263 both_vcpu_locks = vcpu_lock_both(current, *next);
264
265 /*
J-Alves4796d112025-02-12 14:39:00 +0000266 * Check if there is a pending interrupt, and if the partition
267 * is expects to notify the scheduler or resume straight away.
268 * Either trigger SRI for later donation of CPU cycles, or
269 * eret `FFA_INTERRUPT` back to the caller.
Karl Meakin5a365d32024-11-08 23:55:03 +0000270 */
Karl Meakin117c8082024-12-04 16:03:28 +0000271 if (ffa_interrupts_intercept_call(both_vcpu_locks.vcpu1,
272 both_vcpu_locks.vcpu2, ffa_ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000273 *next = NULL;
274 ret = true;
275 }
276
277 vcpu_unlock(&both_vcpu_locks.vcpu2);
278
279 return ret;
280}
281
Karl Meakin936ec1e2025-01-31 13:17:11 +0000282static bool sp_boot_next(struct vcpu_locked current_locked, struct vcpu **next)
283{
284 struct vcpu *vcpu_next = NULL;
285 struct vcpu *current = current_locked.vcpu;
286 struct vm *next_vm;
287 size_t cpu_indx = cpu_index(current->cpu);
288
289 if (current->cpu->last_sp_initialized) {
290 return false;
291 }
292
293 if (!atomic_load_explicit(&current->vm->aborting,
294 memory_order_relaxed)) {
295 /* vCPU has just returned from successful initialization. */
296 dlog_verbose(
297 "Initialized execution context of VM: %#x on CPU: %zu, "
298 "boot_order: %u\n",
299 current->vm->id, cpu_index(current->cpu),
300 current->vm->boot_order);
301 }
302
303 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
304 next_vm = vm_get_next_boot(current->vm);
305 } else {
306 /* SP boot chain on secondary CPU. */
307 next_vm = vm_get_next_boot_secondary_core(current->vm);
308 }
309
310 current->state = VCPU_STATE_WAITING;
311 current->rt_model = RTM_NONE;
312 current->scheduling_mode = NONE;
313
314 /*
315 * Pick next SP's vCPU to be booted. Once all SPs have booted
316 * (next_vm is NULL), then return execution to NWd.
317 */
318 if (next_vm == NULL) {
319 current->cpu->last_sp_initialized = true;
320 goto out;
321 }
322
323 vcpu_next = vm_get_vcpu(next_vm, cpu_indx);
324
325 /*
326 * An SP's execution context needs to be bootstrapped if:
327 * - It has never been initialized before.
328 * - Or it was turned off when the CPU, on which it was pinned, was
329 * powered down.
330 */
331 if (vcpu_next->rt_model == RTM_SP_INIT ||
332 vcpu_next->state == VCPU_STATE_OFF) {
333 vcpu_next->rt_model = RTM_SP_INIT;
334 arch_regs_reset(vcpu_next);
335 vcpu_next->cpu = current->cpu;
Madhukar Pappireddyd6c055d2025-05-08 15:35:46 -0500336 vcpu_next->state = VCPU_STATE_STARTING;
Karl Meakin936ec1e2025-01-31 13:17:11 +0000337 vcpu_next->regs_available = false;
338 vcpu_set_phys_core_idx(vcpu_next);
339 arch_regs_set_pc_arg(&vcpu_next->regs,
340 vcpu_next->vm->secondary_ep, 0ULL);
341
342 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
343 /*
344 * Boot information is passed by the SPMC to the SP's
345 * execution context only on the primary CPU.
346 */
347 vcpu_set_boot_info_gp_reg(vcpu_next);
348 }
349
350 *next = vcpu_next;
351
352 return true;
353 }
354out:
355 dlog_notice("Finished bootstrapping all SPs on CPU%lx\n", cpu_indx);
356 return false;
357}
358
Karl Meakin5a365d32024-11-08 23:55:03 +0000359/**
360 * The invocation of FFA_MSG_WAIT at secure virtual FF-A instance is compliant
361 * with FF-A v1.1 EAC0 specification. It only performs the state transition
362 * from RUNNING to WAITING for the following Partition runtime models:
363 * RTM_FFA_RUN, RTM_SEC_INTERRUPT, RTM_SP_INIT.
364 */
Karl Meakin117c8082024-12-04 16:03:28 +0000365struct ffa_value ffa_cpu_cycles_msg_wait_prepare(
366 struct vcpu_locked current_locked, struct vcpu **next)
Karl Meakin5a365d32024-11-08 23:55:03 +0000367{
368 struct ffa_value ret = api_ffa_interrupt_return(0);
369 struct vcpu *current = current_locked.vcpu;
370
371 switch (current->rt_model) {
372 case RTM_SP_INIT:
373 if (!sp_boot_next(current_locked, next)) {
J-Alves4c334a32025-03-18 11:49:26 +0000374 ffa_msg_wait_complete(current_locked, next);
Karl Meakin5a365d32024-11-08 23:55:03 +0000375
J-Alves1ef5fc42025-02-27 17:15:29 +0000376 ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
377 &ret);
Karl Meakin5a365d32024-11-08 23:55:03 +0000378 }
379 break;
380 case RTM_SEC_INTERRUPT:
381 /*
382 * Either resume the preempted SP or complete the FFA_MSG_WAIT.
383 */
384 assert(current->preempted_vcpu != NULL);
Karl Meakinca38ef92025-02-13 14:20:23 +0000385 ffa_cpu_cycles_preempted_vcpu_resume(current_locked, next);
Karl Meakin5a365d32024-11-08 23:55:03 +0000386
J-Alves1ef5fc42025-02-27 17:15:29 +0000387 if (!ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
388 &ret)) {
389 /*
390 * If CPU cycles were allocated through FFA_RUN
391 * interface, allow the interrupts(if they were masked
392 * earlier) before returning control to NWd.
393 */
394 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000395 }
396
Karl Meakin5a365d32024-11-08 23:55:03 +0000397 break;
398 case RTM_FFA_RUN:
J-Alves4c334a32025-03-18 11:49:26 +0000399 ffa_msg_wait_complete(current_locked, next);
Karl Meakin5a365d32024-11-08 23:55:03 +0000400
J-Alves1ef5fc42025-02-27 17:15:29 +0000401 if (!ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
402 &ret)) {
403 /*
404 * If CPU cycles were allocated through FFA_RUN
405 * interface, allow the interrupts(if they were masked
406 * earlier) before returning control to NWd.
407 */
408 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000409 }
410
Karl Meakin5a365d32024-11-08 23:55:03 +0000411 break;
412 default:
413 panic("%s: unexpected runtime model %x for [%x %x]",
414 current->rt_model, current->vm->id,
415 cpu_index(current->cpu));
416 }
417
418 vcpu_unlock(&current_locked);
419
420 return ret;
421}
422
Karl Meakin5a365d32024-11-08 23:55:03 +0000423/*
424 * Initialize the scheduling mode and/or Partition Runtime model of the target
425 * SP upon being resumed by an FFA_RUN ABI.
426 */
J-Alves5d65fe72025-02-18 16:02:32 +0000427void ffa_cpu_cycles_init_schedule_mode_ffa_run(
Karl Meakin117c8082024-12-04 16:03:28 +0000428 struct vcpu_locked current_locked, struct vcpu_locked target_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000429{
430 struct vcpu *vcpu = target_locked.vcpu;
431 struct vcpu *current = current_locked.vcpu;
432
433 /*
434 * Scenario 1 in Table 8.4; Therefore SPMC could be resuming a vCPU
435 * that was part of NWd scheduled mode.
436 */
437 CHECK(vcpu->scheduling_mode != SPMC_MODE);
438
439 /* Section 8.2.3 bullet 4.2 of spec FF-A v1.1 EAC0. */
440 if (vcpu->state == VCPU_STATE_WAITING) {
441 assert(vcpu->rt_model == RTM_SP_INIT ||
442 vcpu->rt_model == RTM_NONE);
443 vcpu->rt_model = RTM_FFA_RUN;
444
445 if (!vm_id_is_current_world(current->vm->id) ||
446 (current->scheduling_mode == NWD_MODE)) {
447 vcpu->scheduling_mode = NWD_MODE;
448 }
449 } else {
450 /* SP vCPU would have been pre-empted earlier or blocked. */
451 CHECK(vcpu->state == VCPU_STATE_PREEMPTED ||
452 vcpu->state == VCPU_STATE_BLOCKED);
453 }
454
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000455 ffa_interrupts_mask(target_locked);
Karl Meakin5a365d32024-11-08 23:55:03 +0000456}
457
458/*
459 * Prepare to yield execution back to the VM/SP that allocated CPU cycles and
460 * move to BLOCKED state. If the CPU cycles were allocated to the current
461 * execution context by the SPMC to handle secure virtual interrupt, then
462 * FFA_YIELD invocation is essentially a no-op.
463 */
Karl Meakin117c8082024-12-04 16:03:28 +0000464struct ffa_value ffa_cpu_cycles_yield_prepare(struct vcpu_locked current_locked,
465 struct vcpu **next,
466 uint32_t timeout_low,
467 uint32_t timeout_high)
Karl Meakin5a365d32024-11-08 23:55:03 +0000468{
469 struct ffa_value ret_args = (struct ffa_value){.func = FFA_SUCCESS_32};
470 struct vcpu *current = current_locked.vcpu;
471 struct ffa_value ret = {
472 .func = FFA_YIELD_32,
473 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
474 .arg2 = timeout_low,
475 .arg3 = timeout_high,
476 };
477
478 switch (current->rt_model) {
479 case RTM_FFA_DIR_REQ:
480 assert(current->direct_request_origin.vm_id !=
481 HF_INVALID_VM_ID);
482 if (current->call_chain.prev_node == NULL) {
483 /*
484 * Relinquish cycles to the NWd VM that sent direct
485 * request message to the current SP.
486 */
487 *next = api_switch_to_other_world(current_locked, ret,
488 VCPU_STATE_BLOCKED);
489 } else {
490 /*
491 * Relinquish cycles to the SP that sent direct request
492 * message to the current SP.
493 */
494 *next = api_switch_to_vm(
495 current_locked, ret, VCPU_STATE_BLOCKED,
496 current->direct_request_origin.vm_id);
497 }
498 break;
499 case RTM_SEC_INTERRUPT: {
500 /*
501 * SPMC does not implement a scheduler needed to resume the
502 * current vCPU upon timeout expiration. Hence, SPMC makes the
503 * implementation defined choice to treat FFA_YIELD invocation
504 * as a no-op if the SP execution context is in the secure
505 * interrupt runtime model. This does not violate FF-A spec as
506 * the spec does not mandate timeout to be honored. Moreover,
507 * timeout specified by an endpoint is just a hint to the
508 * partition manager which allocated CPU cycles.
509 * Resume the current vCPU.
510 */
511 *next = NULL;
512 break;
513 }
514 default:
515 CHECK(current->rt_model == RTM_FFA_RUN);
516 *next = api_switch_to_primary(current_locked, ret,
517 VCPU_STATE_BLOCKED);
518 break;
519 }
520
521 /*
522 * Before yielding CPU cycles, allow the interrupts(if they were
523 * masked earlier).
524 */
525 if (*next != NULL) {
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000526 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000527 }
528
529 return ret_args;
530}
Karl Meakin936ec1e2025-01-31 13:17:11 +0000531
Karl Meakin936ec1e2025-01-31 13:17:11 +0000532/**
533 * Validates the Runtime model for FFA_RUN. Refer to section 7.2 of the FF-A
534 * v1.1 EAC0 spec.
535 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000536static bool ffa_cpu_cycles_check_rtm_ffa_run(struct vcpu_locked current_locked,
537 struct vcpu_locked locked_vcpu,
538 uint32_t func,
539 enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000540{
541 switch (func) {
542 case FFA_MSG_SEND_DIRECT_REQ_64:
543 case FFA_MSG_SEND_DIRECT_REQ_32:
544 case FFA_MSG_SEND_DIRECT_REQ2_64:
Karl Meakin402b1fe2025-03-20 14:52:55 +0000545 [[fallthrough]];
Karl Meakin936ec1e2025-01-31 13:17:11 +0000546 case FFA_RUN_32: {
547 /* Rules 1,2 section 7.2 EAC0 spec. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000548 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
549 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000550 return false;
551 }
552 *next_state = VCPU_STATE_BLOCKED;
553 return true;
554 }
555 case FFA_MSG_WAIT_32:
556 /* Rule 4 section 7.2 EAC0 spec. Fall through. */
557 *next_state = VCPU_STATE_WAITING;
558 return true;
559 case FFA_YIELD_32:
560 /* Rule 5 section 7.2 EAC0 spec. */
561 *next_state = VCPU_STATE_BLOCKED;
562 return true;
Madhukar Pappireddya34e6a12025-03-17 17:43:56 -0500563 case FFA_ABORT_32:
564 case FFA_ABORT_64:
565 /* Rule I0072 in section 7.2.4 of FF-A v1.3 ALP2 spec. */
566 *next_state = VCPU_STATE_ABORTED;
567 return true;
Karl Meakin936ec1e2025-01-31 13:17:11 +0000568 case FFA_MSG_SEND_DIRECT_RESP_64:
569 case FFA_MSG_SEND_DIRECT_RESP_32:
570 case FFA_MSG_SEND_DIRECT_RESP2_64:
571 /* Rule 3 section 7.2 EAC0 spec. Fall through. */
572 default:
573 /* Deny state transitions by default. */
574 return false;
575 }
576}
577
578/**
579 * Validates the Runtime model for FFA_MSG_SEND_DIRECT_REQ and
580 * FFA_MSG_SEND_DIRECT_REQ2. Refer to section 8.3 of the FF-A
581 * v1.2 spec.
582 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000583static bool ffa_cpu_cycles_check_rtm_ffa_dir_req(
584 struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu,
585 ffa_id_t receiver_vm_id, uint32_t func, enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000586{
Madhukar Pappireddy454b91d2025-02-10 13:05:46 -0600587 /*
588 * SPMC denies invocation if the SP's vCPU is processing a PSCI power
589 * management operation.
590 */
591 if (current_locked.vcpu->pwr_mgmt_op != PWR_MGMT_NONE) {
592 switch (func) {
593 case FFA_MSG_SEND_DIRECT_REQ_64:
594 case FFA_MSG_SEND_DIRECT_REQ_32:
595 case FFA_MSG_SEND_DIRECT_REQ2_64:
596 case FFA_RUN_32:
597 case FFA_YIELD_32:
598 dlog_verbose(
599 "State transition denied during power "
600 "management operation\n");
601 return false;
602 default:
603 break;
604 }
605 }
606
Karl Meakin936ec1e2025-01-31 13:17:11 +0000607 switch (func) {
608 case FFA_MSG_SEND_DIRECT_REQ_64:
609 case FFA_MSG_SEND_DIRECT_REQ_32:
610 case FFA_MSG_SEND_DIRECT_REQ2_64:
Karl Meakin402b1fe2025-03-20 14:52:55 +0000611 [[fallthrough]];
Karl Meakin936ec1e2025-01-31 13:17:11 +0000612 case FFA_RUN_32: {
613 /* Rules 1,2. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000614 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
615 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000616 return false;
617 }
618
619 *next_state = VCPU_STATE_BLOCKED;
620 return true;
621 }
622 case FFA_MSG_SEND_DIRECT_RESP_64:
Karl Meakin402b1fe2025-03-20 14:52:55 +0000623 case FFA_MSG_SEND_DIRECT_RESP_32:
624 case FFA_MSG_SEND_DIRECT_RESP2_64: {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000625 /* Rule 3. */
626 if (current_locked.vcpu->direct_request_origin.vm_id ==
627 receiver_vm_id) {
628 *next_state = VCPU_STATE_WAITING;
629 return true;
630 }
631
632 return false;
633 }
634 case FFA_YIELD_32:
635 /* Rule 3, section 8.3 of FF-A v1.2 spec. */
636 *next_state = VCPU_STATE_BLOCKED;
637 return true;
Madhukar Pappireddya34e6a12025-03-17 17:43:56 -0500638 case FFA_ABORT_32:
639 case FFA_ABORT_64:
640 /* Rule I0072 in section 7.2.4 of FF-A v1.3 ALP2 spec. */
641 *next_state = VCPU_STATE_ABORTED;
642 return true;
Karl Meakin936ec1e2025-01-31 13:17:11 +0000643 case FFA_MSG_WAIT_32:
644 /* Rule 4. Fall through. */
645 default:
646 /* Deny state transitions by default. */
647 return false;
648 }
649}
650
651/**
652 * Validates the Runtime model for Secure interrupt handling. Refer to section
653 * 8.4 of the FF-A v1.2 ALP0 spec.
654 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000655static bool ffa_cpu_cycles_check_rtm_sec_interrupt(
656 struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu,
657 uint32_t func, enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000658{
659 struct vcpu *current = current_locked.vcpu;
660 struct vcpu *vcpu = locked_vcpu.vcpu;
661
662 CHECK(current->scheduling_mode == SPMC_MODE);
663
664 switch (func) {
665 case FFA_MSG_SEND_DIRECT_REQ_64:
666 case FFA_MSG_SEND_DIRECT_REQ_32:
667 case FFA_MSG_SEND_DIRECT_REQ2_64:
668 /* Rule 3. */
669 *next_state = VCPU_STATE_BLOCKED;
670 return true;
671 case FFA_RUN_32: {
672 /* Rule 6. */
673 if (vcpu->state == VCPU_STATE_PREEMPTED) {
674 *next_state = VCPU_STATE_BLOCKED;
675 return true;
676 }
677
678 return false;
679 }
680 case FFA_MSG_WAIT_32:
681 /* Rule 2. */
682 *next_state = VCPU_STATE_WAITING;
683 return true;
684 case FFA_YIELD_32:
685 /* Rule 3, section 8.4 of FF-A v1.2 spec. */
686 *next_state = VCPU_STATE_BLOCKED;
687 return true;
Madhukar Pappireddya34e6a12025-03-17 17:43:56 -0500688 case FFA_ABORT_32:
689 case FFA_ABORT_64:
690 /* Rule I0072 in section 7.2.4 of FF-A v1.3 ALP2 spec. */
691 *next_state = VCPU_STATE_ABORTED;
692 return true;
Karl Meakin936ec1e2025-01-31 13:17:11 +0000693 case FFA_MSG_SEND_DIRECT_RESP_64:
694 case FFA_MSG_SEND_DIRECT_RESP_32:
695 case FFA_MSG_SEND_DIRECT_RESP2_64:
696 /* Rule 5. Fall through. */
697 default:
698 /* Deny state transitions by default. */
699 return false;
700 }
701}
702
703/**
704 * Validates the Runtime model for SP initialization. Refer to section
705 * 8.3 of the FF-A v1.2 ALP0 spec.
706 */
Madhukar Pappireddya34e6a12025-03-17 17:43:56 -0500707static bool ffa_cpu_cycles_check_rtm_sp_init(struct vcpu_locked current_locked,
708 struct vcpu_locked locked_vcpu,
Karl Meakinca38ef92025-02-13 14:20:23 +0000709 uint32_t func,
710 enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000711{
Madhukar Pappireddyd6c055d2025-05-08 15:35:46 -0500712 assert(current_locked.vcpu->state == VCPU_STATE_STARTING);
713
Karl Meakin936ec1e2025-01-31 13:17:11 +0000714 switch (func) {
715 case FFA_MSG_SEND_DIRECT_REQ_64:
716 case FFA_MSG_SEND_DIRECT_REQ_32:
717 case FFA_MSG_SEND_DIRECT_REQ2_64: {
718 struct vcpu *vcpu = locked_vcpu.vcpu;
719
720 assert(vcpu != NULL);
721 /* Rule 1. */
722 if (vcpu->rt_model != RTM_SP_INIT) {
723 *next_state = VCPU_STATE_BLOCKED;
724 return true;
725 }
726
727 return false;
728 }
729 case FFA_MSG_WAIT_32:
Madhukar Pappireddya34e6a12025-03-17 17:43:56 -0500730 /* Rule 2. */
Karl Meakin936ec1e2025-01-31 13:17:11 +0000731 *next_state = VCPU_STATE_WAITING;
732 return true;
Madhukar Pappireddya34e6a12025-03-17 17:43:56 -0500733 case FFA_ERROR_32:
734 /* Refer rule I0096 in FF-A v1.3 ALP2 spec. */
735 if (current_locked.vcpu->vm->ffa_version > FFA_VERSION_1_2) {
736 return false;
737 }
738
739 *next_state = VCPU_STATE_WAITING;
740 return true;
741 case FFA_ABORT_32:
742 case FFA_ABORT_64:
743 /* Rule I0072 in section 7.2.4 of FF-A v1.3 ALP2 spec. */
744 *next_state = VCPU_STATE_ABORTED;
745 return true;
Karl Meakin936ec1e2025-01-31 13:17:11 +0000746 case FFA_YIELD_32:
747 /* Rule 4. Fall through. */
748 case FFA_RUN_32:
749 /* Rule 6. Fall through. */
750 case FFA_MSG_SEND_DIRECT_RESP_64:
751 case FFA_MSG_SEND_DIRECT_RESP_32:
752 case FFA_MSG_SEND_DIRECT_RESP2_64:
753 /* Rule 5. Fall through. */
754 default:
755 /* Deny state transitions by default. */
756 return false;
757 }
758}
759
760/**
761 * Check if the runtime model (state machine) of the current SP supports the
762 * given FF-A ABI invocation. If yes, next_state represents the state to which
763 * the current vcpu would transition upon the FF-A ABI invocation as determined
764 * by the Partition runtime model.
765 */
766bool ffa_cpu_cycles_check_runtime_state_transition(
767 struct vcpu_locked current_locked, ffa_id_t vm_id,
768 ffa_id_t receiver_vm_id, struct vcpu_locked locked_vcpu, uint32_t func,
769 enum vcpu_state *next_state)
770{
771 bool allowed = false;
772 struct vcpu *current = current_locked.vcpu;
773
774 assert(current != NULL);
775
776 /* Perform state transition checks only for Secure Partitions. */
777 if (!vm_id_is_current_world(vm_id)) {
778 return true;
779 }
780
781 switch (current->rt_model) {
782 case RTM_FFA_RUN:
Karl Meakinca38ef92025-02-13 14:20:23 +0000783 allowed = ffa_cpu_cycles_check_rtm_ffa_run(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000784 current_locked, locked_vcpu, func, next_state);
785 break;
786 case RTM_FFA_DIR_REQ:
Karl Meakinca38ef92025-02-13 14:20:23 +0000787 allowed = ffa_cpu_cycles_check_rtm_ffa_dir_req(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000788 current_locked, locked_vcpu, receiver_vm_id, func,
789 next_state);
790 break;
791 case RTM_SEC_INTERRUPT:
Karl Meakinca38ef92025-02-13 14:20:23 +0000792 allowed = ffa_cpu_cycles_check_rtm_sec_interrupt(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000793 current_locked, locked_vcpu, func, next_state);
794 break;
795 case RTM_SP_INIT:
Madhukar Pappireddya34e6a12025-03-17 17:43:56 -0500796 allowed = ffa_cpu_cycles_check_rtm_sp_init(
797 current_locked, locked_vcpu, func, next_state);
Karl Meakin936ec1e2025-01-31 13:17:11 +0000798 break;
799 default:
800 dlog_error(
801 "Illegal Runtime Model specified by SP%x on CPU%zx\n",
802 current->vm->id, cpu_index(current->cpu));
803 allowed = false;
804 break;
805 }
806
807 if (!allowed) {
808 dlog_verbose("State transition denied\n");
809 }
810
811 return allowed;
812}
813
814/*
815 * Handle FFA_ERROR_32 call according to the given error code.
816 *
817 * Error codes other than FFA_ABORTED, and cases of FFA_ABORTED not
818 * in RTM_SP_INIT runtime model, not implemented. Refer to section 8.5
819 * of FF-A 1.2 spec.
820 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000821struct ffa_value ffa_cpu_cycles_error_32(struct vcpu *current,
822 struct vcpu **next,
823 enum ffa_error error_code)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000824{
825 struct vcpu_locked current_locked;
826 struct vm_locked vm_locked;
827 enum partition_runtime_model rt_model;
828 struct ffa_value ret = api_ffa_interrupt_return(0);
829
830 vm_locked = vm_lock(current->vm);
831 current_locked = vcpu_lock(current);
832 rt_model = current_locked.vcpu->rt_model;
833
834 if (error_code == FFA_ABORTED && rt_model == RTM_SP_INIT) {
835 dlog_error("Aborting SP %#x from vCPU %u\n", current->vm->id,
836 vcpu_index(current));
837
838 atomic_store_explicit(&current->vm->aborting, true,
839 memory_order_relaxed);
840
841 ffa_vm_free_resources(vm_locked);
842
843 if (sp_boot_next(current_locked, next)) {
844 goto out;
845 }
846
847 /*
848 * Relinquish control back to the NWd. Return
849 * FFA_MSG_WAIT_32 to indicate to SPMD that SPMC
850 * has successfully finished initialization.
851 */
852 *next = api_switch_to_other_world(
853 current_locked,
854 (struct ffa_value){.func = FFA_MSG_WAIT_32},
855 VCPU_STATE_ABORTED);
856
857 goto out;
858 }
859 ret = ffa_error(FFA_NOT_SUPPORTED);
860out:
861 vcpu_unlock(&current_locked);
862 vm_unlock(&vm_locked);
863 return ret;
864}
Madhukar Pappireddy04cf9e82025-03-17 17:32:02 -0500865
866struct ffa_value ffa_cpu_cycles_abort(struct vcpu_locked current_locked,
867 struct vcpu **next)
868{
869 struct ffa_value to_ret = ffa_error(FFA_ABORTED);
870 enum vcpu_state next_state = VCPU_STATE_ABORTED;
871
872 /*
873 * Relinquish control back to the NWd.
874 * TODO: Support for abort actions will be added in further patches.
875 */
876 *next = api_switch_to_primary(current_locked, to_ret, next_state);
877
878 return (struct ffa_value){.func = FFA_SUCCESS_32};
879}