blob: c878db461e262d492e1435e61693d3936fa1863c [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"
10#include "hf/arch/plat/ffa.h"
11#include "hf/arch/plat/ffa/interrupts.h"
12
13#include "hf/api.h"
14#include "hf/check.h"
15#include "hf/plat/interrupts.h"
16#include "hf/vm.h"
17
18void plat_ffa_vcpu_allow_interrupts(struct vcpu *current);
19bool sp_boot_next(struct vcpu_locked current_locked, struct vcpu **next);
20
21bool plat_ffa_run_forward(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
22 struct ffa_value *ret)
23{
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 */
34bool plat_ffa_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)
37{
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
100 vcpu_secondary_reset_and_start(target_locked, vm->secondary_ep, 0);
101
102 if (vm_id_is_current_world(current->vm->id)) {
103 /*
104 * Refer FF-A v1.1 EAC0 spec section 8.3.2.2.1
105 * Signaling an Other S-Int in blocked state
106 */
107 if (current->preempted_vcpu != NULL) {
108 /*
109 * After the target SP execution context has handled
110 * the interrupt, it uses the FFA_RUN ABI to resume
111 * the request due to which it had entered the blocked
112 * state earlier.
113 * Deny the state transition if the SP didnt perform the
114 * deactivation of the secure virtual interrupt.
115 */
116 if (!vcpu_is_interrupt_queue_empty(current_locked)) {
117 run_ret->arg2 = FFA_DENIED;
118 ret = false;
119 goto out;
120 }
121
122 /*
123 * Refer Figure 8.13 Scenario 1: Implementation choice:
124 * SPMC left all intermediate SP execution contexts in
125 * blocked state. Hence, SPMC now bypasses the
126 * intermediate these execution contexts and resumes the
127 * SP execution context that was originally preempted.
128 */
129 *next = current->preempted_vcpu;
130 if (target_vcpu != current->preempted_vcpu) {
131 dlog_verbose("Skipping intermediate vCPUs\n");
132 }
133 /*
134 * This flag should not have been set by SPMC when it
135 * signaled the virtual interrupt to the SP while SP was
136 * in WAITING or BLOCKED states. Refer the embedded
137 * comment in vcpu.h file for further description.
138 */
139 assert(!current->requires_deactivate_call);
140
141 /*
142 * Clear fields corresponding to secure interrupt
143 * handling.
144 */
145 vcpu_secure_interrupt_complete(current_locked);
146 }
147 }
148
149 /* Check if a vCPU of SP is being resumed. */
150 if (vm_id_is_current_world(target_vm_id)) {
151 /*
152 * A call chain cannot span CPUs. The target vCPU can only be
153 * resumed by FFA_RUN on present CPU.
154 */
155 if ((target_vcpu->call_chain.prev_node != NULL ||
156 target_vcpu->call_chain.next_node != NULL) &&
157 (target_vcpu->cpu != current->cpu)) {
158 run_ret->arg2 = FFA_DENIED;
159 ret = false;
160 goto out;
161 }
162
163 if (!vcpu_is_interrupt_queue_empty(target_locked)) {
164 /*
165 * Consider the following scenarios: a secure interrupt
166 * triggered in normal world and is targeted to an SP.
167 * Scenario A): The target SP's vCPU was preempted by a
168 * non secure interrupt.
169 * Scenario B): The target SP's vCPU was in blocked
170 * state after it yielded CPU cycles to
171 * normal world using FFA_YIELD.
172 * In both the scenarios, SPMC would have injected a
173 * virtual interrupt and set the appropriate flags after
174 * de-activating the secure physical interrupt. SPMC did
175 * not resume the target vCPU at that moment.
176 */
177 assert(target_vcpu->state == VCPU_STATE_PREEMPTED ||
178 target_vcpu->state == VCPU_STATE_BLOCKED);
179 assert(vcpu_interrupt_count_get(target_locked) > 0);
180
181 /*
182 * This check is to ensure the target SP vCPU could
183 * only be a part of NWd scheduled call chain. FF-A v1.1
184 * spec prohibits an SPMC scheduled call chain to be
185 * preempted by a non secure interrupt.
186 */
187 CHECK(target_vcpu->scheduling_mode == NWD_MODE);
188 }
189 }
190
191out:
192 vcpu_unlock(&target_locked);
193 return ret;
194}
195
196/**
197 * SPMC scheduled call chain is completely unwound.
198 */
199static void plat_ffa_exit_spmc_schedule_mode(struct vcpu_locked current_locked)
200{
201 struct vcpu *current;
202
203 current = current_locked.vcpu;
204 assert(current->call_chain.next_node == NULL);
205 CHECK(current->scheduling_mode == SPMC_MODE);
206
207 current->scheduling_mode = NONE;
208 current->rt_model = RTM_NONE;
209}
210
211/**
212 * A SP in running state could have been pre-empted by a secure interrupt. SPM
213 * would switch the execution to the vCPU of target SP responsible for interupt
214 * handling. Upon completion of interrupt handling, vCPU performs interrupt
215 * signal completion through FFA_MSG_WAIT ABI (provided it was in waiting state
216 * when interrupt was signaled).
217 *
218 * SPM then resumes the original SP that was initially pre-empted.
219 */
220static struct ffa_value plat_ffa_preempted_vcpu_resume(
221 struct vcpu_locked current_locked, struct vcpu **next)
222{
223 struct ffa_value ffa_ret = (struct ffa_value){.func = FFA_MSG_WAIT_32};
224 struct vcpu *target_vcpu;
225 struct vcpu *current = current_locked.vcpu;
226 struct vcpu_locked target_locked;
227 struct two_vcpu_locked vcpus_locked;
228
229 CHECK(current->preempted_vcpu != NULL);
230 CHECK(current->preempted_vcpu->state == VCPU_STATE_PREEMPTED);
231
232 target_vcpu = current->preempted_vcpu;
233 vcpu_unlock(&current_locked);
234
235 /* Lock both vCPUs at once to avoid deadlock. */
236 vcpus_locked = vcpu_lock_both(current, target_vcpu);
237 current_locked = vcpus_locked.vcpu1;
238 target_locked = vcpus_locked.vcpu2;
239
240 /* Reset the fields tracking secure interrupt processing. */
241 vcpu_secure_interrupt_complete(current_locked);
242
243 /* SPMC scheduled call chain is completely unwound. */
244 plat_ffa_exit_spmc_schedule_mode(current_locked);
245 assert(current->call_chain.prev_node == NULL);
246
247 current->state = VCPU_STATE_WAITING;
248
249 vcpu_set_running(target_locked, NULL);
250
251 vcpu_unlock(&target_locked);
252
253 /* Restore interrupt priority mask. */
254 plat_ffa_vcpu_allow_interrupts(current);
255
256 /* The pre-empted vCPU should be run. */
257 *next = target_vcpu;
258
259 return ffa_ret;
260}
261
262static struct ffa_value ffa_msg_wait_complete(struct vcpu_locked current_locked,
263 struct vcpu **next)
264{
265 struct vcpu *current = current_locked.vcpu;
266
267 current->scheduling_mode = NONE;
268 current->rt_model = RTM_NONE;
269
270 /* Relinquish control back to the NWd. */
271 *next = api_switch_to_other_world(
272 current_locked, (struct ffa_value){.func = FFA_MSG_WAIT_32},
273 VCPU_STATE_WAITING);
274
275 return api_ffa_interrupt_return(0);
276}
277
278/**
279 * Deals with the common case of intercepting an FFA_MSG_WAIT call.
280 */
281static bool plat_ffa_msg_wait_intercept(struct vcpu_locked current_locked,
282 struct vcpu **next,
283 struct ffa_value *ffa_ret)
284{
285 struct two_vcpu_locked both_vcpu_locks;
286 struct vcpu *current = current_locked.vcpu;
287 bool ret = false;
288
289 assert(next != NULL);
290 assert(*next != NULL);
291
292 vcpu_unlock(&current_locked);
293
294 both_vcpu_locks = vcpu_lock_both(current, *next);
295
296 /*
297 * Check if there are any pending secure virtual interrupts to
298 * be handled. The `next` should have a pointer to the current
299 * vCPU. Intercept call will set `ret` to FFA_INTERRUPT and the
300 * respective interrupt id.
301 */
302 if (plat_ffa_intercept_call(both_vcpu_locks.vcpu1,
303 both_vcpu_locks.vcpu2, ffa_ret)) {
304 *next = NULL;
305 ret = true;
306 }
307
308 vcpu_unlock(&both_vcpu_locks.vcpu2);
309
310 return ret;
311}
312
313/**
314 * The invocation of FFA_MSG_WAIT at secure virtual FF-A instance is compliant
315 * with FF-A v1.1 EAC0 specification. It only performs the state transition
316 * from RUNNING to WAITING for the following Partition runtime models:
317 * RTM_FFA_RUN, RTM_SEC_INTERRUPT, RTM_SP_INIT.
318 */
319struct ffa_value plat_ffa_msg_wait_prepare(struct vcpu_locked current_locked,
320 struct vcpu **next)
321{
322 struct ffa_value ret = api_ffa_interrupt_return(0);
323 struct vcpu *current = current_locked.vcpu;
324
325 switch (current->rt_model) {
326 case RTM_SP_INIT:
327 if (!sp_boot_next(current_locked, next)) {
328 ret = ffa_msg_wait_complete(current_locked, next);
329
330 if (plat_ffa_msg_wait_intercept(current_locked, next,
331 &ret)) {
332 }
333 }
334 break;
335 case RTM_SEC_INTERRUPT:
336 /*
337 * Either resume the preempted SP or complete the FFA_MSG_WAIT.
338 */
339 assert(current->preempted_vcpu != NULL);
340 plat_ffa_preempted_vcpu_resume(current_locked, next);
341
342 if (plat_ffa_msg_wait_intercept(current_locked, next, &ret)) {
343 break;
344 }
345
346 /*
347 * If CPU cycles were allocated through FFA_RUN interface,
348 * allow the interrupts(if they were masked earlier) before
349 * returning control to NWd.
350 */
351 plat_ffa_vcpu_allow_interrupts(current);
352 break;
353 case RTM_FFA_RUN:
354 ret = ffa_msg_wait_complete(current_locked, next);
355
356 if (plat_ffa_msg_wait_intercept(current_locked, next, &ret)) {
357 break;
358 }
359
360 /*
361 * If CPU cycles were allocated through FFA_RUN interface,
362 * allow the interrupts(if they were masked earlier) before
363 * returning control to NWd.
364 */
365 plat_ffa_vcpu_allow_interrupts(current);
366
367 break;
368 default:
369 panic("%s: unexpected runtime model %x for [%x %x]",
370 current->rt_model, current->vm->id,
371 cpu_index(current->cpu));
372 }
373
374 vcpu_unlock(&current_locked);
375
376 return ret;
377}
378
379/**
380 * Enforce action of an SP in response to non-secure or other-secure interrupt
381 * by changing the priority mask. Effectively, physical interrupts shall not
382 * trigger which has the same effect as queueing interrupts.
383 */
384static void plat_ffa_vcpu_queue_interrupts(
385 struct vcpu_locked receiver_vcpu_locked)
386{
387 struct vcpu *receiver_vcpu = receiver_vcpu_locked.vcpu;
388 uint8_t current_priority;
389
390 /* Save current value of priority mask. */
391 current_priority = plat_interrupts_get_priority_mask();
392 receiver_vcpu->prev_interrupt_priority = current_priority;
393
394 if (receiver_vcpu->vm->other_s_interrupts_action ==
395 OTHER_S_INT_ACTION_QUEUED ||
396 receiver_vcpu->scheduling_mode == SPMC_MODE) {
397 /*
398 * If secure interrupts not masked yet, mask them now. We could
399 * enter SPMC scheduled mode when an EL3 SPMD Logical partition
400 * sends a direct request, and we are making the IMPDEF choice
401 * to mask interrupts when such a situation occurs. This keeps
402 * design simple.
403 */
404 if (current_priority > SWD_MASK_ALL_INT) {
405 plat_interrupts_set_priority_mask(SWD_MASK_ALL_INT);
406 }
407 } else if (receiver_vcpu->vm->ns_interrupts_action ==
408 NS_ACTION_QUEUED) {
409 /* If non secure interrupts not masked yet, mask them now. */
410 if (current_priority > SWD_MASK_NS_INT) {
411 plat_interrupts_set_priority_mask(SWD_MASK_NS_INT);
412 }
413 }
414}
415
416/*
417 * Initialize the scheduling mode and/or Partition Runtime model of the target
418 * SP upon being resumed by an FFA_RUN ABI.
419 */
420void plat_ffa_init_schedule_mode_ffa_run(struct vcpu_locked current_locked,
421 struct vcpu_locked target_locked)
422{
423 struct vcpu *vcpu = target_locked.vcpu;
424 struct vcpu *current = current_locked.vcpu;
425
426 /*
427 * Scenario 1 in Table 8.4; Therefore SPMC could be resuming a vCPU
428 * that was part of NWd scheduled mode.
429 */
430 CHECK(vcpu->scheduling_mode != SPMC_MODE);
431
432 /* Section 8.2.3 bullet 4.2 of spec FF-A v1.1 EAC0. */
433 if (vcpu->state == VCPU_STATE_WAITING) {
434 assert(vcpu->rt_model == RTM_SP_INIT ||
435 vcpu->rt_model == RTM_NONE);
436 vcpu->rt_model = RTM_FFA_RUN;
437
438 if (!vm_id_is_current_world(current->vm->id) ||
439 (current->scheduling_mode == NWD_MODE)) {
440 vcpu->scheduling_mode = NWD_MODE;
441 }
442 } else {
443 /* SP vCPU would have been pre-empted earlier or blocked. */
444 CHECK(vcpu->state == VCPU_STATE_PREEMPTED ||
445 vcpu->state == VCPU_STATE_BLOCKED);
446 }
447
448 plat_ffa_vcpu_queue_interrupts(target_locked);
449}
450
451/*
452 * Prepare to yield execution back to the VM/SP that allocated CPU cycles and
453 * move to BLOCKED state. If the CPU cycles were allocated to the current
454 * execution context by the SPMC to handle secure virtual interrupt, then
455 * FFA_YIELD invocation is essentially a no-op.
456 */
457struct ffa_value plat_ffa_yield_prepare(struct vcpu_locked current_locked,
458 struct vcpu **next,
459 uint32_t timeout_low,
460 uint32_t timeout_high)
461{
462 struct ffa_value ret_args = (struct ffa_value){.func = FFA_SUCCESS_32};
463 struct vcpu *current = current_locked.vcpu;
464 struct ffa_value ret = {
465 .func = FFA_YIELD_32,
466 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
467 .arg2 = timeout_low,
468 .arg3 = timeout_high,
469 };
470
471 switch (current->rt_model) {
472 case RTM_FFA_DIR_REQ:
473 assert(current->direct_request_origin.vm_id !=
474 HF_INVALID_VM_ID);
475 if (current->call_chain.prev_node == NULL) {
476 /*
477 * Relinquish cycles to the NWd VM that sent direct
478 * request message to the current SP.
479 */
480 *next = api_switch_to_other_world(current_locked, ret,
481 VCPU_STATE_BLOCKED);
482 } else {
483 /*
484 * Relinquish cycles to the SP that sent direct request
485 * message to the current SP.
486 */
487 *next = api_switch_to_vm(
488 current_locked, ret, VCPU_STATE_BLOCKED,
489 current->direct_request_origin.vm_id);
490 }
491 break;
492 case RTM_SEC_INTERRUPT: {
493 /*
494 * SPMC does not implement a scheduler needed to resume the
495 * current vCPU upon timeout expiration. Hence, SPMC makes the
496 * implementation defined choice to treat FFA_YIELD invocation
497 * as a no-op if the SP execution context is in the secure
498 * interrupt runtime model. This does not violate FF-A spec as
499 * the spec does not mandate timeout to be honored. Moreover,
500 * timeout specified by an endpoint is just a hint to the
501 * partition manager which allocated CPU cycles.
502 * Resume the current vCPU.
503 */
504 *next = NULL;
505 break;
506 }
507 default:
508 CHECK(current->rt_model == RTM_FFA_RUN);
509 *next = api_switch_to_primary(current_locked, ret,
510 VCPU_STATE_BLOCKED);
511 break;
512 }
513
514 /*
515 * Before yielding CPU cycles, allow the interrupts(if they were
516 * masked earlier).
517 */
518 if (*next != NULL) {
519 plat_ffa_vcpu_allow_interrupts(current);
520 }
521
522 return ret_args;
523}