blob: 06b469890df28c7cbc55ada968d11980a3b65cd2 [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"
14#include "hf/ffa/interrupts.h"
Karl Meakin5a365d32024-11-08 23:55:03 +000015#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
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. */
252 plat_ffa_vcpu_allow_interrupts(current);
253
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
311/**
312 * The invocation of FFA_MSG_WAIT at secure virtual FF-A instance is compliant
313 * with FF-A v1.1 EAC0 specification. It only performs the state transition
314 * from RUNNING to WAITING for the following Partition runtime models:
315 * RTM_FFA_RUN, RTM_SEC_INTERRUPT, RTM_SP_INIT.
316 */
Karl Meakin117c8082024-12-04 16:03:28 +0000317struct ffa_value ffa_cpu_cycles_msg_wait_prepare(
318 struct vcpu_locked current_locked, struct vcpu **next)
Karl Meakin5a365d32024-11-08 23:55:03 +0000319{
320 struct ffa_value ret = api_ffa_interrupt_return(0);
321 struct vcpu *current = current_locked.vcpu;
322
323 switch (current->rt_model) {
324 case RTM_SP_INIT:
325 if (!sp_boot_next(current_locked, next)) {
326 ret = ffa_msg_wait_complete(current_locked, next);
327
328 if (plat_ffa_msg_wait_intercept(current_locked, next,
329 &ret)) {
330 }
331 }
332 break;
333 case RTM_SEC_INTERRUPT:
334 /*
335 * Either resume the preempted SP or complete the FFA_MSG_WAIT.
336 */
337 assert(current->preempted_vcpu != NULL);
338 plat_ffa_preempted_vcpu_resume(current_locked, next);
339
340 if (plat_ffa_msg_wait_intercept(current_locked, next, &ret)) {
341 break;
342 }
343
344 /*
345 * If CPU cycles were allocated through FFA_RUN interface,
346 * allow the interrupts(if they were masked earlier) before
347 * returning control to NWd.
348 */
349 plat_ffa_vcpu_allow_interrupts(current);
350 break;
351 case RTM_FFA_RUN:
352 ret = ffa_msg_wait_complete(current_locked, next);
353
354 if (plat_ffa_msg_wait_intercept(current_locked, next, &ret)) {
355 break;
356 }
357
358 /*
359 * If CPU cycles were allocated through FFA_RUN interface,
360 * allow the interrupts(if they were masked earlier) before
361 * returning control to NWd.
362 */
363 plat_ffa_vcpu_allow_interrupts(current);
364
365 break;
366 default:
367 panic("%s: unexpected runtime model %x for [%x %x]",
368 current->rt_model, current->vm->id,
369 cpu_index(current->cpu));
370 }
371
372 vcpu_unlock(&current_locked);
373
374 return ret;
375}
376
377/**
378 * Enforce action of an SP in response to non-secure or other-secure interrupt
379 * by changing the priority mask. Effectively, physical interrupts shall not
380 * trigger which has the same effect as queueing interrupts.
381 */
382static void plat_ffa_vcpu_queue_interrupts(
383 struct vcpu_locked receiver_vcpu_locked)
384{
385 struct vcpu *receiver_vcpu = receiver_vcpu_locked.vcpu;
386 uint8_t current_priority;
387
388 /* Save current value of priority mask. */
389 current_priority = plat_interrupts_get_priority_mask();
390 receiver_vcpu->prev_interrupt_priority = current_priority;
391
392 if (receiver_vcpu->vm->other_s_interrupts_action ==
393 OTHER_S_INT_ACTION_QUEUED ||
394 receiver_vcpu->scheduling_mode == SPMC_MODE) {
395 /*
396 * If secure interrupts not masked yet, mask them now. We could
397 * enter SPMC scheduled mode when an EL3 SPMD Logical partition
398 * sends a direct request, and we are making the IMPDEF choice
399 * to mask interrupts when such a situation occurs. This keeps
400 * design simple.
401 */
402 if (current_priority > SWD_MASK_ALL_INT) {
403 plat_interrupts_set_priority_mask(SWD_MASK_ALL_INT);
404 }
405 } else if (receiver_vcpu->vm->ns_interrupts_action ==
406 NS_ACTION_QUEUED) {
407 /* If non secure interrupts not masked yet, mask them now. */
408 if (current_priority > SWD_MASK_NS_INT) {
409 plat_interrupts_set_priority_mask(SWD_MASK_NS_INT);
410 }
411 }
412}
413
414/*
415 * Initialize the scheduling mode and/or Partition Runtime model of the target
416 * SP upon being resumed by an FFA_RUN ABI.
417 */
Karl Meakin117c8082024-12-04 16:03:28 +0000418void ffa_cpu_cycles_init_schedule_mode_ffa_runeld_prepare(
419 struct vcpu_locked current_locked, struct vcpu_locked target_locked)
Karl Meakin5a365d32024-11-08 23:55:03 +0000420{
421 struct vcpu *vcpu = target_locked.vcpu;
422 struct vcpu *current = current_locked.vcpu;
423
424 /*
425 * Scenario 1 in Table 8.4; Therefore SPMC could be resuming a vCPU
426 * that was part of NWd scheduled mode.
427 */
428 CHECK(vcpu->scheduling_mode != SPMC_MODE);
429
430 /* Section 8.2.3 bullet 4.2 of spec FF-A v1.1 EAC0. */
431 if (vcpu->state == VCPU_STATE_WAITING) {
432 assert(vcpu->rt_model == RTM_SP_INIT ||
433 vcpu->rt_model == RTM_NONE);
434 vcpu->rt_model = RTM_FFA_RUN;
435
436 if (!vm_id_is_current_world(current->vm->id) ||
437 (current->scheduling_mode == NWD_MODE)) {
438 vcpu->scheduling_mode = NWD_MODE;
439 }
440 } else {
441 /* SP vCPU would have been pre-empted earlier or blocked. */
442 CHECK(vcpu->state == VCPU_STATE_PREEMPTED ||
443 vcpu->state == VCPU_STATE_BLOCKED);
444 }
445
446 plat_ffa_vcpu_queue_interrupts(target_locked);
447}
448
449/*
450 * Prepare to yield execution back to the VM/SP that allocated CPU cycles and
451 * move to BLOCKED state. If the CPU cycles were allocated to the current
452 * execution context by the SPMC to handle secure virtual interrupt, then
453 * FFA_YIELD invocation is essentially a no-op.
454 */
Karl Meakin117c8082024-12-04 16:03:28 +0000455struct ffa_value ffa_cpu_cycles_yield_prepare(struct vcpu_locked current_locked,
456 struct vcpu **next,
457 uint32_t timeout_low,
458 uint32_t timeout_high)
Karl Meakin5a365d32024-11-08 23:55:03 +0000459{
460 struct ffa_value ret_args = (struct ffa_value){.func = FFA_SUCCESS_32};
461 struct vcpu *current = current_locked.vcpu;
462 struct ffa_value ret = {
463 .func = FFA_YIELD_32,
464 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
465 .arg2 = timeout_low,
466 .arg3 = timeout_high,
467 };
468
469 switch (current->rt_model) {
470 case RTM_FFA_DIR_REQ:
471 assert(current->direct_request_origin.vm_id !=
472 HF_INVALID_VM_ID);
473 if (current->call_chain.prev_node == NULL) {
474 /*
475 * Relinquish cycles to the NWd VM that sent direct
476 * request message to the current SP.
477 */
478 *next = api_switch_to_other_world(current_locked, ret,
479 VCPU_STATE_BLOCKED);
480 } else {
481 /*
482 * Relinquish cycles to the SP that sent direct request
483 * message to the current SP.
484 */
485 *next = api_switch_to_vm(
486 current_locked, ret, VCPU_STATE_BLOCKED,
487 current->direct_request_origin.vm_id);
488 }
489 break;
490 case RTM_SEC_INTERRUPT: {
491 /*
492 * SPMC does not implement a scheduler needed to resume the
493 * current vCPU upon timeout expiration. Hence, SPMC makes the
494 * implementation defined choice to treat FFA_YIELD invocation
495 * as a no-op if the SP execution context is in the secure
496 * interrupt runtime model. This does not violate FF-A spec as
497 * the spec does not mandate timeout to be honored. Moreover,
498 * timeout specified by an endpoint is just a hint to the
499 * partition manager which allocated CPU cycles.
500 * Resume the current vCPU.
501 */
502 *next = NULL;
503 break;
504 }
505 default:
506 CHECK(current->rt_model == RTM_FFA_RUN);
507 *next = api_switch_to_primary(current_locked, ret,
508 VCPU_STATE_BLOCKED);
509 break;
510 }
511
512 /*
513 * Before yielding CPU cycles, allow the interrupts(if they were
514 * masked earlier).
515 */
516 if (*next != NULL) {
517 plat_ffa_vcpu_allow_interrupts(current);
518 }
519
520 return ret_args;
521}