blob: 4785f3940923b58363687c728f3373feae51bbad [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 }
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 }
Karl Meakin5a365d32024-11-08 23:55:03 +0000160 }
161
162out:
163 vcpu_unlock(&target_locked);
164 return ret;
165}
166
167/**
168 * SPMC scheduled call chain is completely unwound.
169 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000170static void ffa_cpu_cycles_exit_spmc_schedule_mode(
171 struct vcpu_locked current_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000172{
173 struct vcpu *current;
174
175 current = current_locked.vcpu;
176 assert(current->call_chain.next_node == NULL);
177 CHECK(current->scheduling_mode == SPMC_MODE);
178
179 current->scheduling_mode = NONE;
180 current->rt_model = RTM_NONE;
181}
182
183/**
184 * A SP in running state could have been pre-empted by a secure interrupt. SPM
185 * would switch the execution to the vCPU of target SP responsible for interupt
186 * handling. Upon completion of interrupt handling, vCPU performs interrupt
187 * signal completion through FFA_MSG_WAIT ABI (provided it was in waiting state
188 * when interrupt was signaled).
189 *
190 * SPM then resumes the original SP that was initially pre-empted.
191 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000192static struct ffa_value ffa_cpu_cycles_preempted_vcpu_resume(
Karl Meakin5a365d32024-11-08 23:55:03 +0000193 struct vcpu_locked current_locked, struct vcpu **next)
194{
195 struct ffa_value ffa_ret = (struct ffa_value){.func = FFA_MSG_WAIT_32};
196 struct vcpu *target_vcpu;
197 struct vcpu *current = current_locked.vcpu;
198 struct vcpu_locked target_locked;
199 struct two_vcpu_locked vcpus_locked;
200
201 CHECK(current->preempted_vcpu != NULL);
202 CHECK(current->preempted_vcpu->state == VCPU_STATE_PREEMPTED);
203
204 target_vcpu = current->preempted_vcpu;
205 vcpu_unlock(&current_locked);
206
207 /* Lock both vCPUs at once to avoid deadlock. */
208 vcpus_locked = vcpu_lock_both(current, target_vcpu);
209 current_locked = vcpus_locked.vcpu1;
210 target_locked = vcpus_locked.vcpu2;
211
212 /* Reset the fields tracking secure interrupt processing. */
213 vcpu_secure_interrupt_complete(current_locked);
214
215 /* SPMC scheduled call chain is completely unwound. */
Karl Meakinca38ef92025-02-13 14:20:23 +0000216 ffa_cpu_cycles_exit_spmc_schedule_mode(current_locked);
Karl Meakin5a365d32024-11-08 23:55:03 +0000217 assert(current->call_chain.prev_node == NULL);
218
219 current->state = VCPU_STATE_WAITING;
220
221 vcpu_set_running(target_locked, NULL);
222
223 vcpu_unlock(&target_locked);
224
225 /* Restore interrupt priority mask. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000226 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000227
228 /* The pre-empted vCPU should be run. */
229 *next = target_vcpu;
230
231 return ffa_ret;
232}
233
234static struct ffa_value ffa_msg_wait_complete(struct vcpu_locked current_locked,
235 struct vcpu **next)
236{
237 struct vcpu *current = current_locked.vcpu;
238
239 current->scheduling_mode = NONE;
240 current->rt_model = RTM_NONE;
241
242 /* Relinquish control back to the NWd. */
243 *next = api_switch_to_other_world(
244 current_locked, (struct ffa_value){.func = FFA_MSG_WAIT_32},
245 VCPU_STATE_WAITING);
246
247 return api_ffa_interrupt_return(0);
248}
249
250/**
251 * Deals with the common case of intercepting an FFA_MSG_WAIT call.
252 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000253static bool ffa_cpu_cycles_msg_wait_intercept(struct vcpu_locked current_locked,
254 struct vcpu **next,
255 struct ffa_value *ffa_ret)
Karl Meakin5a365d32024-11-08 23:55:03 +0000256{
257 struct two_vcpu_locked both_vcpu_locks;
258 struct vcpu *current = current_locked.vcpu;
259 bool ret = false;
260
261 assert(next != NULL);
262 assert(*next != NULL);
263
264 vcpu_unlock(&current_locked);
265
266 both_vcpu_locks = vcpu_lock_both(current, *next);
267
268 /*
269 * Check if there are any pending secure virtual interrupts to
270 * be handled. The `next` should have a pointer to the current
271 * vCPU. Intercept call will set `ret` to FFA_INTERRUPT and the
272 * respective interrupt id.
273 */
Karl Meakin117c8082024-12-04 16:03:28 +0000274 if (ffa_interrupts_intercept_call(both_vcpu_locks.vcpu1,
275 both_vcpu_locks.vcpu2, ffa_ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000276 *next = NULL;
277 ret = true;
278 }
279
280 vcpu_unlock(&both_vcpu_locks.vcpu2);
281
282 return ret;
283}
284
Karl Meakin936ec1e2025-01-31 13:17:11 +0000285static bool sp_boot_next(struct vcpu_locked current_locked, struct vcpu **next)
286{
287 struct vcpu *vcpu_next = NULL;
288 struct vcpu *current = current_locked.vcpu;
289 struct vm *next_vm;
290 size_t cpu_indx = cpu_index(current->cpu);
291
292 if (current->cpu->last_sp_initialized) {
293 return false;
294 }
295
296 if (!atomic_load_explicit(&current->vm->aborting,
297 memory_order_relaxed)) {
298 /* vCPU has just returned from successful initialization. */
299 dlog_verbose(
300 "Initialized execution context of VM: %#x on CPU: %zu, "
301 "boot_order: %u\n",
302 current->vm->id, cpu_index(current->cpu),
303 current->vm->boot_order);
304 }
305
306 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
307 next_vm = vm_get_next_boot(current->vm);
308 } else {
309 /* SP boot chain on secondary CPU. */
310 next_vm = vm_get_next_boot_secondary_core(current->vm);
311 }
312
313 current->state = VCPU_STATE_WAITING;
314 current->rt_model = RTM_NONE;
315 current->scheduling_mode = NONE;
316
317 /*
318 * Pick next SP's vCPU to be booted. Once all SPs have booted
319 * (next_vm is NULL), then return execution to NWd.
320 */
321 if (next_vm == NULL) {
322 current->cpu->last_sp_initialized = true;
323 goto out;
324 }
325
326 vcpu_next = vm_get_vcpu(next_vm, cpu_indx);
327
328 /*
329 * An SP's execution context needs to be bootstrapped if:
330 * - It has never been initialized before.
331 * - Or it was turned off when the CPU, on which it was pinned, was
332 * powered down.
333 */
334 if (vcpu_next->rt_model == RTM_SP_INIT ||
335 vcpu_next->state == VCPU_STATE_OFF) {
336 vcpu_next->rt_model = RTM_SP_INIT;
337 arch_regs_reset(vcpu_next);
338 vcpu_next->cpu = current->cpu;
339 vcpu_next->state = VCPU_STATE_RUNNING;
340 vcpu_next->regs_available = false;
341 vcpu_set_phys_core_idx(vcpu_next);
342 arch_regs_set_pc_arg(&vcpu_next->regs,
343 vcpu_next->vm->secondary_ep, 0ULL);
344
345 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
346 /*
347 * Boot information is passed by the SPMC to the SP's
348 * execution context only on the primary CPU.
349 */
350 vcpu_set_boot_info_gp_reg(vcpu_next);
351 }
352
353 *next = vcpu_next;
354
355 return true;
356 }
357out:
358 dlog_notice("Finished bootstrapping all SPs on CPU%lx\n", cpu_indx);
359 return false;
360}
361
Karl Meakin5a365d32024-11-08 23:55:03 +0000362/**
363 * The invocation of FFA_MSG_WAIT at secure virtual FF-A instance is compliant
364 * with FF-A v1.1 EAC0 specification. It only performs the state transition
365 * from RUNNING to WAITING for the following Partition runtime models:
366 * RTM_FFA_RUN, RTM_SEC_INTERRUPT, RTM_SP_INIT.
367 */
Karl Meakin117c8082024-12-04 16:03:28 +0000368struct ffa_value ffa_cpu_cycles_msg_wait_prepare(
369 struct vcpu_locked current_locked, struct vcpu **next)
Karl Meakin5a365d32024-11-08 23:55:03 +0000370{
371 struct ffa_value ret = api_ffa_interrupt_return(0);
372 struct vcpu *current = current_locked.vcpu;
373
374 switch (current->rt_model) {
375 case RTM_SP_INIT:
376 if (!sp_boot_next(current_locked, next)) {
377 ret = ffa_msg_wait_complete(current_locked, next);
378
Karl Meakinca38ef92025-02-13 14:20:23 +0000379 if (ffa_cpu_cycles_msg_wait_intercept(current_locked,
380 next, &ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000381 }
382 }
383 break;
384 case RTM_SEC_INTERRUPT:
385 /*
386 * Either resume the preempted SP or complete the FFA_MSG_WAIT.
387 */
388 assert(current->preempted_vcpu != NULL);
Karl Meakinca38ef92025-02-13 14:20:23 +0000389 ffa_cpu_cycles_preempted_vcpu_resume(current_locked, next);
Karl Meakin5a365d32024-11-08 23:55:03 +0000390
Karl Meakinca38ef92025-02-13 14:20:23 +0000391 if (ffa_cpu_cycles_msg_wait_intercept(current_locked, next,
392 &ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000393 break;
394 }
395
396 /*
397 * If CPU cycles were allocated through FFA_RUN interface,
398 * allow the interrupts(if they were masked earlier) before
399 * returning control to NWd.
400 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000401 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000402 break;
403 case RTM_FFA_RUN:
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, next,
407 &ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000408 break;
409 }
410
411 /*
412 * If CPU cycles were allocated through FFA_RUN interface,
413 * allow the interrupts(if they were masked earlier) before
414 * returning control to NWd.
415 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000416 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000417
418 break;
419 default:
420 panic("%s: unexpected runtime model %x for [%x %x]",
421 current->rt_model, current->vm->id,
422 cpu_index(current->cpu));
423 }
424
425 vcpu_unlock(&current_locked);
426
427 return ret;
428}
429
Karl Meakin5a365d32024-11-08 23:55:03 +0000430/*
431 * Initialize the scheduling mode and/or Partition Runtime model of the target
432 * SP upon being resumed by an FFA_RUN ABI.
433 */
J-Alves5d65fe72025-02-18 16:02:32 +0000434void ffa_cpu_cycles_init_schedule_mode_ffa_run(
Karl Meakin117c8082024-12-04 16:03:28 +0000435 struct vcpu_locked current_locked, struct vcpu_locked target_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000436{
437 struct vcpu *vcpu = target_locked.vcpu;
438 struct vcpu *current = current_locked.vcpu;
439
440 /*
441 * Scenario 1 in Table 8.4; Therefore SPMC could be resuming a vCPU
442 * that was part of NWd scheduled mode.
443 */
444 CHECK(vcpu->scheduling_mode != SPMC_MODE);
445
446 /* Section 8.2.3 bullet 4.2 of spec FF-A v1.1 EAC0. */
447 if (vcpu->state == VCPU_STATE_WAITING) {
448 assert(vcpu->rt_model == RTM_SP_INIT ||
449 vcpu->rt_model == RTM_NONE);
450 vcpu->rt_model = RTM_FFA_RUN;
451
452 if (!vm_id_is_current_world(current->vm->id) ||
453 (current->scheduling_mode == NWD_MODE)) {
454 vcpu->scheduling_mode = NWD_MODE;
455 }
456 } else {
457 /* SP vCPU would have been pre-empted earlier or blocked. */
458 CHECK(vcpu->state == VCPU_STATE_PREEMPTED ||
459 vcpu->state == VCPU_STATE_BLOCKED);
460 }
461
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000462 ffa_interrupts_mask(target_locked);
Karl Meakin5a365d32024-11-08 23:55:03 +0000463}
464
465/*
466 * Prepare to yield execution back to the VM/SP that allocated CPU cycles and
467 * move to BLOCKED state. If the CPU cycles were allocated to the current
468 * execution context by the SPMC to handle secure virtual interrupt, then
469 * FFA_YIELD invocation is essentially a no-op.
470 */
Karl Meakin117c8082024-12-04 16:03:28 +0000471struct ffa_value ffa_cpu_cycles_yield_prepare(struct vcpu_locked current_locked,
472 struct vcpu **next,
473 uint32_t timeout_low,
474 uint32_t timeout_high)
Karl Meakin5a365d32024-11-08 23:55:03 +0000475{
476 struct ffa_value ret_args = (struct ffa_value){.func = FFA_SUCCESS_32};
477 struct vcpu *current = current_locked.vcpu;
478 struct ffa_value ret = {
479 .func = FFA_YIELD_32,
480 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
481 .arg2 = timeout_low,
482 .arg3 = timeout_high,
483 };
484
485 switch (current->rt_model) {
486 case RTM_FFA_DIR_REQ:
487 assert(current->direct_request_origin.vm_id !=
488 HF_INVALID_VM_ID);
489 if (current->call_chain.prev_node == NULL) {
490 /*
491 * Relinquish cycles to the NWd VM that sent direct
492 * request message to the current SP.
493 */
494 *next = api_switch_to_other_world(current_locked, ret,
495 VCPU_STATE_BLOCKED);
496 } else {
497 /*
498 * Relinquish cycles to the SP that sent direct request
499 * message to the current SP.
500 */
501 *next = api_switch_to_vm(
502 current_locked, ret, VCPU_STATE_BLOCKED,
503 current->direct_request_origin.vm_id);
504 }
505 break;
506 case RTM_SEC_INTERRUPT: {
507 /*
508 * SPMC does not implement a scheduler needed to resume the
509 * current vCPU upon timeout expiration. Hence, SPMC makes the
510 * implementation defined choice to treat FFA_YIELD invocation
511 * as a no-op if the SP execution context is in the secure
512 * interrupt runtime model. This does not violate FF-A spec as
513 * the spec does not mandate timeout to be honored. Moreover,
514 * timeout specified by an endpoint is just a hint to the
515 * partition manager which allocated CPU cycles.
516 * Resume the current vCPU.
517 */
518 *next = NULL;
519 break;
520 }
521 default:
522 CHECK(current->rt_model == RTM_FFA_RUN);
523 *next = api_switch_to_primary(current_locked, ret,
524 VCPU_STATE_BLOCKED);
525 break;
526 }
527
528 /*
529 * Before yielding CPU cycles, allow the interrupts(if they were
530 * masked earlier).
531 */
532 if (*next != NULL) {
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000533 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000534 }
535
536 return ret_args;
537}
Karl Meakin936ec1e2025-01-31 13:17:11 +0000538
Karl Meakin936ec1e2025-01-31 13:17:11 +0000539/**
540 * Validates the Runtime model for FFA_RUN. Refer to section 7.2 of the FF-A
541 * v1.1 EAC0 spec.
542 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000543static bool ffa_cpu_cycles_check_rtm_ffa_run(struct vcpu_locked current_locked,
544 struct vcpu_locked locked_vcpu,
545 uint32_t func,
546 enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000547{
548 switch (func) {
549 case FFA_MSG_SEND_DIRECT_REQ_64:
550 case FFA_MSG_SEND_DIRECT_REQ_32:
551 case FFA_MSG_SEND_DIRECT_REQ2_64:
552 /* Fall through. */
553 case FFA_RUN_32: {
554 /* Rules 1,2 section 7.2 EAC0 spec. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000555 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
556 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000557 return false;
558 }
559 *next_state = VCPU_STATE_BLOCKED;
560 return true;
561 }
562 case FFA_MSG_WAIT_32:
563 /* Rule 4 section 7.2 EAC0 spec. Fall through. */
564 *next_state = VCPU_STATE_WAITING;
565 return true;
566 case FFA_YIELD_32:
567 /* Rule 5 section 7.2 EAC0 spec. */
568 *next_state = VCPU_STATE_BLOCKED;
569 return true;
570 case FFA_MSG_SEND_DIRECT_RESP_64:
571 case FFA_MSG_SEND_DIRECT_RESP_32:
572 case FFA_MSG_SEND_DIRECT_RESP2_64:
573 /* Rule 3 section 7.2 EAC0 spec. Fall through. */
574 default:
575 /* Deny state transitions by default. */
576 return false;
577 }
578}
579
580/**
581 * Validates the Runtime model for FFA_MSG_SEND_DIRECT_REQ and
582 * FFA_MSG_SEND_DIRECT_REQ2. Refer to section 8.3 of the FF-A
583 * v1.2 spec.
584 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000585static bool ffa_cpu_cycles_check_rtm_ffa_dir_req(
586 struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu,
587 ffa_id_t receiver_vm_id, uint32_t func, enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000588{
589 switch (func) {
590 case FFA_MSG_SEND_DIRECT_REQ_64:
591 case FFA_MSG_SEND_DIRECT_REQ_32:
592 case FFA_MSG_SEND_DIRECT_REQ2_64:
593 /* Fall through. */
594 case FFA_RUN_32: {
595 /* Rules 1,2. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000596 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
597 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000598 return false;
599 }
600
601 *next_state = VCPU_STATE_BLOCKED;
602 return true;
603 }
604 case FFA_MSG_SEND_DIRECT_RESP_64:
605 case FFA_MSG_SEND_DIRECT_RESP_32: {
606 case FFA_MSG_SEND_DIRECT_RESP2_64:
607 /* Rule 3. */
608 if (current_locked.vcpu->direct_request_origin.vm_id ==
609 receiver_vm_id) {
610 *next_state = VCPU_STATE_WAITING;
611 return true;
612 }
613
614 return false;
615 }
616 case FFA_YIELD_32:
617 /* Rule 3, section 8.3 of FF-A v1.2 spec. */
618 *next_state = VCPU_STATE_BLOCKED;
619 return true;
620 case FFA_MSG_WAIT_32:
621 /* Rule 4. Fall through. */
622 default:
623 /* Deny state transitions by default. */
624 return false;
625 }
626}
627
628/**
629 * Validates the Runtime model for Secure interrupt handling. Refer to section
630 * 8.4 of the FF-A v1.2 ALP0 spec.
631 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000632static bool ffa_cpu_cycles_check_rtm_sec_interrupt(
633 struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu,
634 uint32_t func, enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000635{
636 struct vcpu *current = current_locked.vcpu;
637 struct vcpu *vcpu = locked_vcpu.vcpu;
638
639 CHECK(current->scheduling_mode == SPMC_MODE);
640
641 switch (func) {
642 case FFA_MSG_SEND_DIRECT_REQ_64:
643 case FFA_MSG_SEND_DIRECT_REQ_32:
644 case FFA_MSG_SEND_DIRECT_REQ2_64:
645 /* Rule 3. */
646 *next_state = VCPU_STATE_BLOCKED;
647 return true;
648 case FFA_RUN_32: {
649 /* Rule 6. */
650 if (vcpu->state == VCPU_STATE_PREEMPTED) {
651 *next_state = VCPU_STATE_BLOCKED;
652 return true;
653 }
654
655 return false;
656 }
657 case FFA_MSG_WAIT_32:
658 /* Rule 2. */
659 *next_state = VCPU_STATE_WAITING;
660 return true;
661 case FFA_YIELD_32:
662 /* Rule 3, section 8.4 of FF-A v1.2 spec. */
663 *next_state = VCPU_STATE_BLOCKED;
664 return true;
665 case FFA_MSG_SEND_DIRECT_RESP_64:
666 case FFA_MSG_SEND_DIRECT_RESP_32:
667 case FFA_MSG_SEND_DIRECT_RESP2_64:
668 /* Rule 5. Fall through. */
669 default:
670 /* Deny state transitions by default. */
671 return false;
672 }
673}
674
675/**
676 * Validates the Runtime model for SP initialization. Refer to section
677 * 8.3 of the FF-A v1.2 ALP0 spec.
678 */
Karl Meakinca38ef92025-02-13 14:20:23 +0000679static bool ffa_cpu_cycles_check_rtm_sp_init(struct vcpu_locked locked_vcpu,
680 uint32_t func,
681 enum vcpu_state *next_state)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000682{
683 switch (func) {
684 case FFA_MSG_SEND_DIRECT_REQ_64:
685 case FFA_MSG_SEND_DIRECT_REQ_32:
686 case FFA_MSG_SEND_DIRECT_REQ2_64: {
687 struct vcpu *vcpu = locked_vcpu.vcpu;
688
689 assert(vcpu != NULL);
690 /* Rule 1. */
691 if (vcpu->rt_model != RTM_SP_INIT) {
692 *next_state = VCPU_STATE_BLOCKED;
693 return true;
694 }
695
696 return false;
697 }
698 case FFA_MSG_WAIT_32:
699 /* Rule 2. Fall through. */
700 case FFA_ERROR_32:
701 /* Rule 3. */
702 *next_state = VCPU_STATE_WAITING;
703 return true;
704 case FFA_YIELD_32:
705 /* Rule 4. Fall through. */
706 case FFA_RUN_32:
707 /* Rule 6. Fall through. */
708 case FFA_MSG_SEND_DIRECT_RESP_64:
709 case FFA_MSG_SEND_DIRECT_RESP_32:
710 case FFA_MSG_SEND_DIRECT_RESP2_64:
711 /* Rule 5. Fall through. */
712 default:
713 /* Deny state transitions by default. */
714 return false;
715 }
716}
717
718/**
719 * Check if the runtime model (state machine) of the current SP supports the
720 * given FF-A ABI invocation. If yes, next_state represents the state to which
721 * the current vcpu would transition upon the FF-A ABI invocation as determined
722 * by the Partition runtime model.
723 */
724bool ffa_cpu_cycles_check_runtime_state_transition(
725 struct vcpu_locked current_locked, ffa_id_t vm_id,
726 ffa_id_t receiver_vm_id, struct vcpu_locked locked_vcpu, uint32_t func,
727 enum vcpu_state *next_state)
728{
729 bool allowed = false;
730 struct vcpu *current = current_locked.vcpu;
731
732 assert(current != NULL);
733
734 /* Perform state transition checks only for Secure Partitions. */
735 if (!vm_id_is_current_world(vm_id)) {
736 return true;
737 }
738
739 switch (current->rt_model) {
740 case RTM_FFA_RUN:
Karl Meakinca38ef92025-02-13 14:20:23 +0000741 allowed = ffa_cpu_cycles_check_rtm_ffa_run(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000742 current_locked, locked_vcpu, func, next_state);
743 break;
744 case RTM_FFA_DIR_REQ:
Karl Meakinca38ef92025-02-13 14:20:23 +0000745 allowed = ffa_cpu_cycles_check_rtm_ffa_dir_req(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000746 current_locked, locked_vcpu, receiver_vm_id, func,
747 next_state);
748 break;
749 case RTM_SEC_INTERRUPT:
Karl Meakinca38ef92025-02-13 14:20:23 +0000750 allowed = ffa_cpu_cycles_check_rtm_sec_interrupt(
Karl Meakin936ec1e2025-01-31 13:17:11 +0000751 current_locked, locked_vcpu, func, next_state);
752 break;
753 case RTM_SP_INIT:
Karl Meakinca38ef92025-02-13 14:20:23 +0000754 allowed = ffa_cpu_cycles_check_rtm_sp_init(locked_vcpu, func,
755 next_state);
Karl Meakin936ec1e2025-01-31 13:17:11 +0000756 break;
757 default:
758 dlog_error(
759 "Illegal Runtime Model specified by SP%x on CPU%zx\n",
760 current->vm->id, cpu_index(current->cpu));
761 allowed = false;
762 break;
763 }
764
765 if (!allowed) {
766 dlog_verbose("State transition denied\n");
767 }
768
769 return allowed;
770}
771
772/*
773 * Handle FFA_ERROR_32 call according to the given error code.
774 *
775 * Error codes other than FFA_ABORTED, and cases of FFA_ABORTED not
776 * in RTM_SP_INIT runtime model, not implemented. Refer to section 8.5
777 * of FF-A 1.2 spec.
778 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000779struct ffa_value ffa_cpu_cycles_error_32(struct vcpu *current,
780 struct vcpu **next,
781 enum ffa_error error_code)
Karl Meakin936ec1e2025-01-31 13:17:11 +0000782{
783 struct vcpu_locked current_locked;
784 struct vm_locked vm_locked;
785 enum partition_runtime_model rt_model;
786 struct ffa_value ret = api_ffa_interrupt_return(0);
787
788 vm_locked = vm_lock(current->vm);
789 current_locked = vcpu_lock(current);
790 rt_model = current_locked.vcpu->rt_model;
791
792 if (error_code == FFA_ABORTED && rt_model == RTM_SP_INIT) {
793 dlog_error("Aborting SP %#x from vCPU %u\n", current->vm->id,
794 vcpu_index(current));
795
796 atomic_store_explicit(&current->vm->aborting, true,
797 memory_order_relaxed);
798
799 ffa_vm_free_resources(vm_locked);
800
801 if (sp_boot_next(current_locked, next)) {
802 goto out;
803 }
804
805 /*
806 * Relinquish control back to the NWd. Return
807 * FFA_MSG_WAIT_32 to indicate to SPMD that SPMC
808 * has successfully finished initialization.
809 */
810 *next = api_switch_to_other_world(
811 current_locked,
812 (struct ffa_value){.func = FFA_MSG_WAIT_32},
813 VCPU_STATE_ABORTED);
814
815 goto out;
816 }
817 ret = ffa_error(FFA_NOT_SUPPORTED);
818out:
819 vcpu_unlock(&current_locked);
820 vm_unlock(&vm_locked);
821 return ret;
822}