blob: 16508b198fc90b47b6dc9f096d3e162f49cb34a4 [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 */
197static void plat_ffa_exit_spmc_schedule_mode(struct vcpu_locked current_locked)
198{
199 struct vcpu *current;
200
201 current = current_locked.vcpu;
202 assert(current->call_chain.next_node == NULL);
203 CHECK(current->scheduling_mode == SPMC_MODE);
204
205 current->scheduling_mode = NONE;
206 current->rt_model = RTM_NONE;
207}
208
209/**
210 * A SP in running state could have been pre-empted by a secure interrupt. SPM
211 * would switch the execution to the vCPU of target SP responsible for interupt
212 * handling. Upon completion of interrupt handling, vCPU performs interrupt
213 * signal completion through FFA_MSG_WAIT ABI (provided it was in waiting state
214 * when interrupt was signaled).
215 *
216 * SPM then resumes the original SP that was initially pre-empted.
217 */
218static struct ffa_value plat_ffa_preempted_vcpu_resume(
219 struct vcpu_locked current_locked, struct vcpu **next)
220{
221 struct ffa_value ffa_ret = (struct ffa_value){.func = FFA_MSG_WAIT_32};
222 struct vcpu *target_vcpu;
223 struct vcpu *current = current_locked.vcpu;
224 struct vcpu_locked target_locked;
225 struct two_vcpu_locked vcpus_locked;
226
227 CHECK(current->preempted_vcpu != NULL);
228 CHECK(current->preempted_vcpu->state == VCPU_STATE_PREEMPTED);
229
230 target_vcpu = current->preempted_vcpu;
231 vcpu_unlock(&current_locked);
232
233 /* Lock both vCPUs at once to avoid deadlock. */
234 vcpus_locked = vcpu_lock_both(current, target_vcpu);
235 current_locked = vcpus_locked.vcpu1;
236 target_locked = vcpus_locked.vcpu2;
237
238 /* Reset the fields tracking secure interrupt processing. */
239 vcpu_secure_interrupt_complete(current_locked);
240
241 /* SPMC scheduled call chain is completely unwound. */
242 plat_ffa_exit_spmc_schedule_mode(current_locked);
243 assert(current->call_chain.prev_node == NULL);
244
245 current->state = VCPU_STATE_WAITING;
246
247 vcpu_set_running(target_locked, NULL);
248
249 vcpu_unlock(&target_locked);
250
251 /* Restore interrupt priority mask. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000252 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000253
254 /* The pre-empted vCPU should be run. */
255 *next = target_vcpu;
256
257 return ffa_ret;
258}
259
260static struct ffa_value ffa_msg_wait_complete(struct vcpu_locked current_locked,
261 struct vcpu **next)
262{
263 struct vcpu *current = current_locked.vcpu;
264
265 current->scheduling_mode = NONE;
266 current->rt_model = RTM_NONE;
267
268 /* Relinquish control back to the NWd. */
269 *next = api_switch_to_other_world(
270 current_locked, (struct ffa_value){.func = FFA_MSG_WAIT_32},
271 VCPU_STATE_WAITING);
272
273 return api_ffa_interrupt_return(0);
274}
275
276/**
277 * Deals with the common case of intercepting an FFA_MSG_WAIT call.
278 */
279static bool plat_ffa_msg_wait_intercept(struct vcpu_locked current_locked,
280 struct vcpu **next,
281 struct ffa_value *ffa_ret)
282{
283 struct two_vcpu_locked both_vcpu_locks;
284 struct vcpu *current = current_locked.vcpu;
285 bool ret = false;
286
287 assert(next != NULL);
288 assert(*next != NULL);
289
290 vcpu_unlock(&current_locked);
291
292 both_vcpu_locks = vcpu_lock_both(current, *next);
293
294 /*
295 * Check if there are any pending secure virtual interrupts to
296 * be handled. The `next` should have a pointer to the current
297 * vCPU. Intercept call will set `ret` to FFA_INTERRUPT and the
298 * respective interrupt id.
299 */
Karl Meakin117c8082024-12-04 16:03:28 +0000300 if (ffa_interrupts_intercept_call(both_vcpu_locks.vcpu1,
301 both_vcpu_locks.vcpu2, ffa_ret)) {
Karl Meakin5a365d32024-11-08 23:55:03 +0000302 *next = NULL;
303 ret = true;
304 }
305
306 vcpu_unlock(&both_vcpu_locks.vcpu2);
307
308 return ret;
309}
310
Karl Meakin936ec1e2025-01-31 13:17:11 +0000311static bool sp_boot_next(struct vcpu_locked current_locked, struct vcpu **next)
312{
313 struct vcpu *vcpu_next = NULL;
314 struct vcpu *current = current_locked.vcpu;
315 struct vm *next_vm;
316 size_t cpu_indx = cpu_index(current->cpu);
317
318 if (current->cpu->last_sp_initialized) {
319 return false;
320 }
321
322 if (!atomic_load_explicit(&current->vm->aborting,
323 memory_order_relaxed)) {
324 /* vCPU has just returned from successful initialization. */
325 dlog_verbose(
326 "Initialized execution context of VM: %#x on CPU: %zu, "
327 "boot_order: %u\n",
328 current->vm->id, cpu_index(current->cpu),
329 current->vm->boot_order);
330 }
331
332 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
333 next_vm = vm_get_next_boot(current->vm);
334 } else {
335 /* SP boot chain on secondary CPU. */
336 next_vm = vm_get_next_boot_secondary_core(current->vm);
337 }
338
339 current->state = VCPU_STATE_WAITING;
340 current->rt_model = RTM_NONE;
341 current->scheduling_mode = NONE;
342
343 /*
344 * Pick next SP's vCPU to be booted. Once all SPs have booted
345 * (next_vm is NULL), then return execution to NWd.
346 */
347 if (next_vm == NULL) {
348 current->cpu->last_sp_initialized = true;
349 goto out;
350 }
351
352 vcpu_next = vm_get_vcpu(next_vm, cpu_indx);
353
354 /*
355 * An SP's execution context needs to be bootstrapped if:
356 * - It has never been initialized before.
357 * - Or it was turned off when the CPU, on which it was pinned, was
358 * powered down.
359 */
360 if (vcpu_next->rt_model == RTM_SP_INIT ||
361 vcpu_next->state == VCPU_STATE_OFF) {
362 vcpu_next->rt_model = RTM_SP_INIT;
363 arch_regs_reset(vcpu_next);
364 vcpu_next->cpu = current->cpu;
365 vcpu_next->state = VCPU_STATE_RUNNING;
366 vcpu_next->regs_available = false;
367 vcpu_set_phys_core_idx(vcpu_next);
368 arch_regs_set_pc_arg(&vcpu_next->regs,
369 vcpu_next->vm->secondary_ep, 0ULL);
370
371 if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) {
372 /*
373 * Boot information is passed by the SPMC to the SP's
374 * execution context only on the primary CPU.
375 */
376 vcpu_set_boot_info_gp_reg(vcpu_next);
377 }
378
379 *next = vcpu_next;
380
381 return true;
382 }
383out:
384 dlog_notice("Finished bootstrapping all SPs on CPU%lx\n", cpu_indx);
385 return false;
386}
387
Karl Meakin5a365d32024-11-08 23:55:03 +0000388/**
389 * The invocation of FFA_MSG_WAIT at secure virtual FF-A instance is compliant
390 * with FF-A v1.1 EAC0 specification. It only performs the state transition
391 * from RUNNING to WAITING for the following Partition runtime models:
392 * RTM_FFA_RUN, RTM_SEC_INTERRUPT, RTM_SP_INIT.
393 */
Karl Meakin117c8082024-12-04 16:03:28 +0000394struct ffa_value ffa_cpu_cycles_msg_wait_prepare(
395 struct vcpu_locked current_locked, struct vcpu **next)
Karl Meakin5a365d32024-11-08 23:55:03 +0000396{
397 struct ffa_value ret = api_ffa_interrupt_return(0);
398 struct vcpu *current = current_locked.vcpu;
399
400 switch (current->rt_model) {
401 case RTM_SP_INIT:
402 if (!sp_boot_next(current_locked, next)) {
403 ret = ffa_msg_wait_complete(current_locked, next);
404
405 if (plat_ffa_msg_wait_intercept(current_locked, next,
406 &ret)) {
407 }
408 }
409 break;
410 case RTM_SEC_INTERRUPT:
411 /*
412 * Either resume the preempted SP or complete the FFA_MSG_WAIT.
413 */
414 assert(current->preempted_vcpu != NULL);
415 plat_ffa_preempted_vcpu_resume(current_locked, next);
416
417 if (plat_ffa_msg_wait_intercept(current_locked, next, &ret)) {
418 break;
419 }
420
421 /*
422 * If CPU cycles were allocated through FFA_RUN interface,
423 * allow the interrupts(if they were masked earlier) before
424 * returning control to NWd.
425 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000426 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000427 break;
428 case RTM_FFA_RUN:
429 ret = ffa_msg_wait_complete(current_locked, next);
430
431 if (plat_ffa_msg_wait_intercept(current_locked, next, &ret)) {
432 break;
433 }
434
435 /*
436 * If CPU cycles were allocated through FFA_RUN interface,
437 * allow the interrupts(if they were masked earlier) before
438 * returning control to NWd.
439 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000440 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000441
442 break;
443 default:
444 panic("%s: unexpected runtime model %x for [%x %x]",
445 current->rt_model, current->vm->id,
446 cpu_index(current->cpu));
447 }
448
449 vcpu_unlock(&current_locked);
450
451 return ret;
452}
453
Karl Meakin5a365d32024-11-08 23:55:03 +0000454/*
455 * Initialize the scheduling mode and/or Partition Runtime model of the target
456 * SP upon being resumed by an FFA_RUN ABI.
457 */
Karl Meakin117c8082024-12-04 16:03:28 +0000458void ffa_cpu_cycles_init_schedule_mode_ffa_runeld_prepare(
459 struct vcpu_locked current_locked, struct vcpu_locked target_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000460{
461 struct vcpu *vcpu = target_locked.vcpu;
462 struct vcpu *current = current_locked.vcpu;
463
464 /*
465 * Scenario 1 in Table 8.4; Therefore SPMC could be resuming a vCPU
466 * that was part of NWd scheduled mode.
467 */
468 CHECK(vcpu->scheduling_mode != SPMC_MODE);
469
470 /* Section 8.2.3 bullet 4.2 of spec FF-A v1.1 EAC0. */
471 if (vcpu->state == VCPU_STATE_WAITING) {
472 assert(vcpu->rt_model == RTM_SP_INIT ||
473 vcpu->rt_model == RTM_NONE);
474 vcpu->rt_model = RTM_FFA_RUN;
475
476 if (!vm_id_is_current_world(current->vm->id) ||
477 (current->scheduling_mode == NWD_MODE)) {
478 vcpu->scheduling_mode = NWD_MODE;
479 }
480 } else {
481 /* SP vCPU would have been pre-empted earlier or blocked. */
482 CHECK(vcpu->state == VCPU_STATE_PREEMPTED ||
483 vcpu->state == VCPU_STATE_BLOCKED);
484 }
485
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000486 ffa_interrupts_mask(target_locked);
Karl Meakin5a365d32024-11-08 23:55:03 +0000487}
488
489/*
490 * Prepare to yield execution back to the VM/SP that allocated CPU cycles and
491 * move to BLOCKED state. If the CPU cycles were allocated to the current
492 * execution context by the SPMC to handle secure virtual interrupt, then
493 * FFA_YIELD invocation is essentially a no-op.
494 */
Karl Meakin117c8082024-12-04 16:03:28 +0000495struct ffa_value ffa_cpu_cycles_yield_prepare(struct vcpu_locked current_locked,
496 struct vcpu **next,
497 uint32_t timeout_low,
498 uint32_t timeout_high)
Karl Meakin5a365d32024-11-08 23:55:03 +0000499{
500 struct ffa_value ret_args = (struct ffa_value){.func = FFA_SUCCESS_32};
501 struct vcpu *current = current_locked.vcpu;
502 struct ffa_value ret = {
503 .func = FFA_YIELD_32,
504 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
505 .arg2 = timeout_low,
506 .arg3 = timeout_high,
507 };
508
509 switch (current->rt_model) {
510 case RTM_FFA_DIR_REQ:
511 assert(current->direct_request_origin.vm_id !=
512 HF_INVALID_VM_ID);
513 if (current->call_chain.prev_node == NULL) {
514 /*
515 * Relinquish cycles to the NWd VM that sent direct
516 * request message to the current SP.
517 */
518 *next = api_switch_to_other_world(current_locked, ret,
519 VCPU_STATE_BLOCKED);
520 } else {
521 /*
522 * Relinquish cycles to the SP that sent direct request
523 * message to the current SP.
524 */
525 *next = api_switch_to_vm(
526 current_locked, ret, VCPU_STATE_BLOCKED,
527 current->direct_request_origin.vm_id);
528 }
529 break;
530 case RTM_SEC_INTERRUPT: {
531 /*
532 * SPMC does not implement a scheduler needed to resume the
533 * current vCPU upon timeout expiration. Hence, SPMC makes the
534 * implementation defined choice to treat FFA_YIELD invocation
535 * as a no-op if the SP execution context is in the secure
536 * interrupt runtime model. This does not violate FF-A spec as
537 * the spec does not mandate timeout to be honored. Moreover,
538 * timeout specified by an endpoint is just a hint to the
539 * partition manager which allocated CPU cycles.
540 * Resume the current vCPU.
541 */
542 *next = NULL;
543 break;
544 }
545 default:
546 CHECK(current->rt_model == RTM_FFA_RUN);
547 *next = api_switch_to_primary(current_locked, ret,
548 VCPU_STATE_BLOCKED);
549 break;
550 }
551
552 /*
553 * Before yielding CPU cycles, allow the interrupts(if they were
554 * masked earlier).
555 */
556 if (*next != NULL) {
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000557 ffa_interrupts_unmask(current);
Karl Meakin5a365d32024-11-08 23:55:03 +0000558 }
559
560 return ret_args;
561}
Karl Meakin936ec1e2025-01-31 13:17:11 +0000562
Karl Meakin936ec1e2025-01-31 13:17:11 +0000563/**
564 * Validates the Runtime model for FFA_RUN. Refer to section 7.2 of the FF-A
565 * v1.1 EAC0 spec.
566 */
567static bool plat_ffa_check_rtm_ffa_run(struct vcpu_locked current_locked,
568 struct vcpu_locked locked_vcpu,
569 uint32_t func,
570 enum vcpu_state *next_state)
571{
572 switch (func) {
573 case FFA_MSG_SEND_DIRECT_REQ_64:
574 case FFA_MSG_SEND_DIRECT_REQ_32:
575 case FFA_MSG_SEND_DIRECT_REQ2_64:
576 /* Fall through. */
577 case FFA_RUN_32: {
578 /* Rules 1,2 section 7.2 EAC0 spec. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000579 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
580 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000581 return false;
582 }
583 *next_state = VCPU_STATE_BLOCKED;
584 return true;
585 }
586 case FFA_MSG_WAIT_32:
587 /* Rule 4 section 7.2 EAC0 spec. Fall through. */
588 *next_state = VCPU_STATE_WAITING;
589 return true;
590 case FFA_YIELD_32:
591 /* Rule 5 section 7.2 EAC0 spec. */
592 *next_state = VCPU_STATE_BLOCKED;
593 return true;
594 case FFA_MSG_SEND_DIRECT_RESP_64:
595 case FFA_MSG_SEND_DIRECT_RESP_32:
596 case FFA_MSG_SEND_DIRECT_RESP2_64:
597 /* Rule 3 section 7.2 EAC0 spec. Fall through. */
598 default:
599 /* Deny state transitions by default. */
600 return false;
601 }
602}
603
604/**
605 * Validates the Runtime model for FFA_MSG_SEND_DIRECT_REQ and
606 * FFA_MSG_SEND_DIRECT_REQ2. Refer to section 8.3 of the FF-A
607 * v1.2 spec.
608 */
609static bool plat_ffa_check_rtm_ffa_dir_req(struct vcpu_locked current_locked,
610 struct vcpu_locked locked_vcpu,
611 ffa_id_t receiver_vm_id,
612 uint32_t func,
613 enum vcpu_state *next_state)
614{
615 switch (func) {
616 case FFA_MSG_SEND_DIRECT_REQ_64:
617 case FFA_MSG_SEND_DIRECT_REQ_32:
618 case FFA_MSG_SEND_DIRECT_REQ2_64:
619 /* Fall through. */
620 case FFA_RUN_32: {
621 /* Rules 1,2. */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000622 if (ffa_direct_msg_precedes_in_call_chain(current_locked,
623 locked_vcpu)) {
Karl Meakin936ec1e2025-01-31 13:17:11 +0000624 return false;
625 }
626
627 *next_state = VCPU_STATE_BLOCKED;
628 return true;
629 }
630 case FFA_MSG_SEND_DIRECT_RESP_64:
631 case FFA_MSG_SEND_DIRECT_RESP_32: {
632 case FFA_MSG_SEND_DIRECT_RESP2_64:
633 /* Rule 3. */
634 if (current_locked.vcpu->direct_request_origin.vm_id ==
635 receiver_vm_id) {
636 *next_state = VCPU_STATE_WAITING;
637 return true;
638 }
639
640 return false;
641 }
642 case FFA_YIELD_32:
643 /* Rule 3, section 8.3 of FF-A v1.2 spec. */
644 *next_state = VCPU_STATE_BLOCKED;
645 return true;
646 case FFA_MSG_WAIT_32:
647 /* Rule 4. Fall through. */
648 default:
649 /* Deny state transitions by default. */
650 return false;
651 }
652}
653
654/**
655 * Validates the Runtime model for Secure interrupt handling. Refer to section
656 * 8.4 of the FF-A v1.2 ALP0 spec.
657 */
658static bool plat_ffa_check_rtm_sec_interrupt(struct vcpu_locked current_locked,
659 struct vcpu_locked locked_vcpu,
660 uint32_t func,
661 enum vcpu_state *next_state)
662{
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 */
706static bool plat_ffa_check_rtm_sp_init(struct vcpu_locked locked_vcpu,
707 uint32_t func,
708 enum vcpu_state *next_state)
709{
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:
768 allowed = plat_ffa_check_rtm_ffa_run(
769 current_locked, locked_vcpu, func, next_state);
770 break;
771 case RTM_FFA_DIR_REQ:
772 allowed = plat_ffa_check_rtm_ffa_dir_req(
773 current_locked, locked_vcpu, receiver_vm_id, func,
774 next_state);
775 break;
776 case RTM_SEC_INTERRUPT:
777 allowed = plat_ffa_check_rtm_sec_interrupt(
778 current_locked, locked_vcpu, func, next_state);
779 break;
780 case RTM_SP_INIT:
781 allowed = plat_ffa_check_rtm_sp_init(locked_vcpu, func,
782 next_state);
783 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}