blob: 53e926728416fc638104ab635a6dc9fe222cab64 [file] [log] [blame]
Karl Meakin8e58ddc2024-11-08 23:19:34 +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/plat/interrupts.h"
10
11#include "hf/arch/gicv3.h"
12#include "hf/arch/host_timer.h"
Karl Meakin8e58ddc2024-11-08 23:19:34 +000013
14#include "hf/api.h"
15#include "hf/check.h"
Karl Meakin902af082024-11-28 14:58:38 +000016#include "hf/ffa/vm.h"
Karl Meakin8e58ddc2024-11-08 23:19:34 +000017#include "hf/hf_ipi.h"
18#include "hf/vm.h"
19
20/**
21 * Drops the current interrupt priority and deactivate the given interrupt ID
22 * for the calling vCPU.
23 *
24 * Returns 0 on success, or -1 otherwise.
25 */
Karl Meakin117c8082024-12-04 16:03:28 +000026int64_t ffa_interrupts_deactivate(uint32_t pint_id, uint32_t vint_id,
27 struct vcpu *current)
Karl Meakin8e58ddc2024-11-08 23:19:34 +000028{
29 struct vcpu_locked current_locked;
30 uint32_t int_id;
31 int ret = 0;
32
33 current_locked = vcpu_lock(current);
34 if (vint_id >= HF_NUM_INTIDS) {
35 ret = -1;
36 goto out;
37 }
38
39 /*
40 * Current implementation maps virtual interrupt to physical interrupt.
41 */
42 if (pint_id != vint_id) {
43 ret = -1;
44 goto out;
45 }
46
47 /*
48 * A malicious SP could de-activate an interrupt that does not belong to
49 * it. Return error to indicate failure.
50 */
51 if (!vcpu_interrupt_queue_peek(current_locked, &int_id)) {
52 dlog_error("No virtual interrupt to be deactivated\n");
53 ret = -1;
54 goto out;
55 }
56
57 if (int_id != vint_id) {
58 dlog_error("Unknown interrupt being deactivated %u\n", vint_id);
59 ret = -1;
60 goto out;
61 }
62
63 if (current->requires_deactivate_call) {
64 /* There is no preempted vCPU to resume. */
65 assert(current->preempted_vcpu == NULL);
66
67 vcpu_secure_interrupt_complete(current_locked);
68 }
69
70 /*
71 * Now that the virtual interrupt has been serviced and deactivated,
72 * remove it from the queue, if it was pending.
73 */
74 vcpu_interrupt_queue_pop(current_locked, &int_id);
75 assert(vint_id == int_id);
76out:
77 vcpu_unlock(&current_locked);
78 return ret;
79}
80
81static struct vcpu *plat_ffa_find_target_vcpu_secure_interrupt(
82 struct vcpu *current, uint32_t interrupt_id)
83{
84 /*
85 * Find which VM/SP owns this interrupt. We then find the
86 * corresponding vCPU context for this CPU.
87 */
88 for (ffa_vm_count_t index = 0; index < vm_get_count(); ++index) {
89 struct vm *vm = vm_find_index(index);
90
91 for (uint32_t j = 0; j < HF_NUM_INTIDS; j++) {
92 struct interrupt_descriptor int_desc =
93 vm->interrupt_desc[j];
94
95 /*
96 * Interrupt descriptors are populated
97 * contiguously.
98 */
99 if (!int_desc.valid) {
100 break;
101 }
102 if (int_desc.interrupt_id == interrupt_id) {
103 return api_ffa_get_vm_vcpu(vm, current);
104 }
105 }
106 }
107
108 return NULL;
109}
110
111static struct vcpu *plat_ffa_find_target_vcpu(struct vcpu *current,
112 uint32_t interrupt_id)
113{
114 struct vcpu *target_vcpu;
115
116 switch (interrupt_id) {
117 case HF_IPI_INTID:
Daniel Boulby7011b5a2024-10-15 18:27:26 +0100118 /*
119 * Get the next vCPU with a pending IPI. If all vCPUs
120 * have had their IPIs handled this will return NULL.
121 */
122 target_vcpu = hf_ipi_get_pending_target_vcpu(current);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000123 break;
124 case ARM_EL1_VIRT_TIMER_PHYS_INT:
125 /* Fall through */
126 case ARM_EL1_PHYS_TIMER_PHYS_INT:
127 panic("Timer interrupt not expected to fire: %u\n",
128 interrupt_id);
129 default:
130 target_vcpu = plat_ffa_find_target_vcpu_secure_interrupt(
131 current, interrupt_id);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000132
Daniel Boulby7011b5a2024-10-15 18:27:26 +0100133 /* The target vCPU for a secure interrupt cannot be NULL. */
134 CHECK(target_vcpu != NULL);
135 }
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000136
137 return target_vcpu;
138}
139
140/*
141 * Queue the pending virtual interrupt for target vcpu. Necessary fields
142 * tracking the secure interrupt processing are set accordingly.
143 */
144static void plat_ffa_queue_vint(struct vcpu_locked target_vcpu_locked,
145 uint32_t vint_id,
146 struct vcpu_locked current_locked)
147{
148 struct vcpu *target_vcpu = target_vcpu_locked.vcpu;
149 struct vcpu *preempted_vcpu = current_locked.vcpu;
150
151 if (preempted_vcpu != NULL) {
152 target_vcpu->preempted_vcpu = preempted_vcpu;
153 preempted_vcpu->state = VCPU_STATE_PREEMPTED;
154 }
155
156 /* Queue the pending virtual interrupt for target vcpu. */
157 if (!vcpu_interrupt_queue_push(target_vcpu_locked, vint_id)) {
158 panic("Exhausted interrupt queue for vcpu of SP: %x\n",
159 target_vcpu->vm->id);
160 }
161}
162
163/**
164 * Enforce action of an SP in response to non-secure or other-secure interrupt
165 * by changing the priority mask. Effectively, physical interrupts shall not
166 * trigger which has the same effect as queueing interrupts.
167 */
168static void plat_ffa_vcpu_queue_interrupts(
169 struct vcpu_locked receiver_vcpu_locked)
170{
171 struct vcpu *receiver_vcpu = receiver_vcpu_locked.vcpu;
172 uint8_t current_priority;
173
174 /* Save current value of priority mask. */
175 current_priority = plat_interrupts_get_priority_mask();
176 receiver_vcpu->prev_interrupt_priority = current_priority;
177
178 if (receiver_vcpu->vm->other_s_interrupts_action ==
179 OTHER_S_INT_ACTION_QUEUED ||
180 receiver_vcpu->scheduling_mode == SPMC_MODE) {
181 /*
182 * If secure interrupts not masked yet, mask them now. We could
183 * enter SPMC scheduled mode when an EL3 SPMD Logical partition
184 * sends a direct request, and we are making the IMPDEF choice
185 * to mask interrupts when such a situation occurs. This keeps
186 * design simple.
187 */
188 if (current_priority > SWD_MASK_ALL_INT) {
189 plat_interrupts_set_priority_mask(SWD_MASK_ALL_INT);
190 }
191 } else if (receiver_vcpu->vm->ns_interrupts_action ==
192 NS_ACTION_QUEUED) {
193 /* If non secure interrupts not masked yet, mask them now. */
194 if (current_priority > SWD_MASK_NS_INT) {
195 plat_interrupts_set_priority_mask(SWD_MASK_NS_INT);
196 }
197 }
198}
199
200/**
201 * Handles the secure interrupt according to the target vCPU's state
202 * in the case the owner of the interrupt is an S-EL0 partition.
203 */
204static struct vcpu *plat_ffa_signal_secure_interrupt_sel0(
205 struct vcpu_locked current_locked,
206 struct vcpu_locked target_vcpu_locked, uint32_t v_intid)
207{
208 struct vcpu *target_vcpu = target_vcpu_locked.vcpu;
209 struct vcpu *next;
210
211 /* Secure interrupt signaling and queuing for S-EL0 SP. */
212 switch (target_vcpu->state) {
213 case VCPU_STATE_WAITING:
214 if (target_vcpu->cpu == current_locked.vcpu->cpu) {
215 struct ffa_value ret_interrupt =
216 api_ffa_interrupt_return(v_intid);
217
218 /* FF-A v1.1 EAC0 Table 8.1 case 1 and Table 12.10. */
219 dlog_verbose("S-EL0: Secure interrupt signaled: %x\n",
220 target_vcpu->vm->id);
221
222 vcpu_enter_secure_interrupt_rtm(target_vcpu_locked);
223 plat_ffa_vcpu_queue_interrupts(target_vcpu_locked);
224
225 vcpu_set_running(target_vcpu_locked, &ret_interrupt);
226
227 /*
228 * If the execution was in NWd as well, set the vCPU
229 * in preempted state as well.
230 */
231 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
232 current_locked);
233
234 /* Switch to target vCPU responsible for this interrupt.
235 */
236 next = target_vcpu;
237 } else {
238 dlog_verbose("S-EL0: Secure interrupt queued: %x\n",
239 target_vcpu->vm->id);
240 /*
241 * The target vcpu has migrated to a different physical
242 * CPU. Hence, it cannot be resumed on this CPU, SPMC
243 * resumes current vCPU.
244 */
245 next = NULL;
246 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
247 (struct vcpu_locked){.vcpu = NULL});
248 }
249 break;
250 case VCPU_STATE_BLOCKED:
251 case VCPU_STATE_PREEMPTED:
252 case VCPU_STATE_RUNNING:
253 dlog_verbose("S-EL0: Secure interrupt queued: %x\n",
254 target_vcpu->vm->id);
255 /*
256 * The target vCPU cannot be resumed, SPMC resumes current
257 * vCPU.
258 */
259 next = NULL;
260 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
261 (struct vcpu_locked){.vcpu = NULL});
262 break;
263 default:
264 panic("Secure interrupt cannot be signaled to target SP\n");
265 break;
266 }
267
268 return next;
269}
270
271static bool is_predecessor_in_call_chain(struct vcpu_locked current_locked,
272 struct vcpu_locked target_locked)
273{
274 struct vcpu *prev_node;
275 struct vcpu *current = current_locked.vcpu;
276 struct vcpu *target = target_locked.vcpu;
277
278 assert(current != NULL);
279 assert(target != NULL);
280
281 prev_node = current->call_chain.prev_node;
282
283 while (prev_node != NULL) {
284 if (prev_node == target) {
285 return true;
286 }
287
288 /* The target vCPU is not it's immediate predecessor. */
289 prev_node = prev_node->call_chain.prev_node;
290 }
291
292 /* Search terminated. Reached start of call chain. */
293 return false;
294}
295
296/**
297 * Handles the secure interrupt according to the target vCPU's state
298 * in the case the owner of the interrupt is an S-EL1 partition.
299 */
300static struct vcpu *plat_ffa_signal_secure_interrupt_sel1(
301 struct vcpu_locked current_locked,
302 struct vcpu_locked target_vcpu_locked, uint32_t v_intid)
303{
304 struct vcpu *target_vcpu = target_vcpu_locked.vcpu;
305 struct vcpu *current = current_locked.vcpu;
306 struct vcpu *next = NULL;
307
308 /* Secure interrupt signaling and queuing for S-EL1 SP. */
309 switch (target_vcpu->state) {
310 case VCPU_STATE_WAITING:
311 if (target_vcpu->cpu == current_locked.vcpu->cpu) {
312 struct ffa_value ret_interrupt =
313 api_ffa_interrupt_return(v_intid);
314
315 /* FF-A v1.1 EAC0 Table 8.2 case 1 and Table 12.10. */
316 vcpu_enter_secure_interrupt_rtm(target_vcpu_locked);
317 plat_ffa_vcpu_queue_interrupts(target_vcpu_locked);
318
319 /*
320 * Ideally, we have to mask non-secure interrupts here
321 * since the spec mandates that SPMC should make sure
322 * SPMC scheduled call chain cannot be preempted by a
323 * non-secure interrupt. However, our current design
324 * takes care of it implicitly.
325 */
326 vcpu_set_running(target_vcpu_locked, &ret_interrupt);
327
328 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
329 current_locked);
330 next = target_vcpu;
331 } else {
332 /*
333 * The target vcpu has migrated to a different physical
334 * CPU. Hence, it cannot be resumed on this CPU, SPMC
335 * resumes current vCPU.
336 */
337 assert(target_vcpu->vm->vcpu_count == 1);
338 dlog_verbose("S-EL1: Secure interrupt queued: %x\n",
339 target_vcpu->vm->id);
340 next = NULL;
341 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
342 (struct vcpu_locked){.vcpu = NULL});
343 }
344 break;
345 case VCPU_STATE_BLOCKED:
346 if (target_vcpu->cpu != current_locked.vcpu->cpu) {
347 /*
348 * The target vcpu has migrated to a different physical
349 * CPU. Hence, it cannot be resumed on this CPU, SPMC
350 * resumes current vCPU.
351 */
352 assert(target_vcpu->vm->vcpu_count == 1);
353 next = NULL;
354 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
355 (struct vcpu_locked){.vcpu = NULL});
356 } else if (is_predecessor_in_call_chain(current_locked,
357 target_vcpu_locked)) {
358 struct ffa_value ret_interrupt =
359 api_ffa_interrupt_return(0);
360
361 /*
362 * If the target vCPU ran earlier in the same call
363 * chain as the current vCPU, SPMC leaves all
364 * intermediate execution contexts in blocked state and
365 * resumes the target vCPU for handling secure
366 * interrupt.
367 * Under the current design, there is only one possible
368 * scenario in which this could happen: both the
369 * preempted (i.e. current) and target vCPU are in the
370 * same NWd scheduled call chain and is described in the
371 * Scenario 1 of Table 8.4 in EAC0 spec.
372 */
373 assert(current_locked.vcpu->scheduling_mode ==
374 NWD_MODE);
375 assert(target_vcpu->scheduling_mode == NWD_MODE);
376
377 /*
378 * The execution preempted the call chain that involved
379 * the targeted and the current SPs.
380 * The targetted SP is set running, whilst the
381 * preempted SP is set PREEMPTED.
382 */
383 vcpu_set_running(target_vcpu_locked, &ret_interrupt);
384
385 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
386 current_locked);
387
388 next = target_vcpu;
389 } else {
390 /*
391 * The target vCPU cannot be resumed now because it is
392 * in BLOCKED state (it yielded CPU cycles using
393 * FFA_YIELD). SPMC queues the virtual interrupt and
394 * resumes the current vCPU which could belong to either
395 * a VM or a SP.
396 */
397 next = NULL;
398 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
399 (struct vcpu_locked){.vcpu = NULL});
400 }
401 break;
402 case VCPU_STATE_PREEMPTED:
403 if (target_vcpu->cpu == current_locked.vcpu->cpu) {
404 /*
405 * We do not resume a target vCPU that has been already
406 * pre-empted by an interrupt. Make the vIRQ pending for
407 * target SP(i.e., queue the interrupt) and continue to
408 * resume current vCPU. Refer to section 8.3.2.1 bullet
409 * 3 in the FF-A v1.1 EAC0 spec.
410 */
411
412 if (current->vm->id == HF_OTHER_WORLD_ID) {
413 /*
414 * The target vCPU must have been preempted by a
415 * non secure interrupt. It could not have been
416 * preempted by a secure interrupt as current
417 * SPMC implementation does not allow secure
418 * interrupt prioritization. Moreover, the
419 * target vCPU should have been in Normal World
420 * scheduled mode as SPMC scheduled mode call
421 * chain cannot be preempted by a non secure
422 * interrupt.
423 */
424 CHECK(target_vcpu->scheduling_mode == NWD_MODE);
425 }
426 } else {
427 /*
428 * The target vcpu has migrated to a different physical
429 * CPU. Hence, it cannot be resumed on this CPU, SPMC
430 * resumes current vCPU.
431 */
432 assert(target_vcpu->vm->vcpu_count == 1);
433 }
434
435 next = NULL;
436 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
437 (struct vcpu_locked){.vcpu = NULL});
438
439 break;
440 case VCPU_STATE_RUNNING:
441 if (current == target_vcpu) {
442 /*
443 * This is the special scenario where the current
444 * running execution context also happens to be the
445 * target of the secure interrupt. In this case, it
446 * needs to signal completion of secure interrupt
447 * implicitly. Refer to the embedded comment in vcpu.h
448 * file for the description of this variable.
449 */
450
451 current->requires_deactivate_call = true;
452 } else {
453 /*
454 * The target vcpu has migrated to a different physical
455 * CPU. Hence, it cannot be resumed on this CPU, SPMC
456 * resumes current vCPU.
457 */
458 assert(target_vcpu->vm->vcpu_count == 1);
459 }
460 next = NULL;
461 plat_ffa_queue_vint(target_vcpu_locked, v_intid,
462 (struct vcpu_locked){.vcpu = NULL});
463 break;
464 case VCPU_STATE_BLOCKED_INTERRUPT:
465 /* WFI is no-op for SP. Fall through. */
466 default:
467 /*
468 * vCPU of Target SP cannot be in OFF/ABORTED state if it has
469 * to handle secure interrupt.
470 */
471 panic("Secure interrupt cannot be signaled to target SP\n");
472 break;
473 }
474
475 return next;
476}
477
478/**
479 * Obtain the physical interrupt that triggered from the interrupt controller,
480 * and inject the corresponding virtual interrupt to the target vCPU.
481 * When PEs executing in the Normal World, and secure interrupts trigger,
482 * execution is trapped into EL3. SPMD then routes the interrupt to SPMC
483 * through FFA_INTERRUPT_32 ABI synchronously using eret conduit.
484 */
Karl Meakin117c8082024-12-04 16:03:28 +0000485void ffa_interrupts_handle_secure_interrupt(struct vcpu *current,
486 struct vcpu **next)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000487{
488 struct vcpu *target_vcpu;
489 struct vcpu_locked target_vcpu_locked =
490 (struct vcpu_locked){.vcpu = NULL};
491 struct vcpu_locked current_locked;
492 uint32_t intid;
493 struct vm_locked target_vm_locked;
494 uint32_t v_intid;
495
496 /* Find pending interrupt id. This also activates the interrupt. */
497 intid = plat_interrupts_get_pending_interrupt_id();
498 v_intid = intid;
499
500 switch (intid) {
501 case ARM_SEL2_TIMER_PHYS_INT:
502 /* Disable the S-EL2 physical timer */
503 host_timer_disable();
504 target_vcpu = timer_find_target_vcpu(current);
505
506 if (target_vcpu != NULL) {
507 v_intid = HF_VIRTUAL_TIMER_INTID;
508 break;
509 }
510 /*
511 * It is possible for target_vcpu to be NULL in case of spurious
512 * timer interrupt. Fall through.
513 */
514 case SPURIOUS_INTID_OTHER_WORLD:
515 /*
516 * Spurious interrupt ID indicating that there are no pending
517 * interrupts to acknowledge. For such scenarios, resume the
518 * current vCPU.
519 */
520 *next = NULL;
521 return;
522 default:
523 target_vcpu = plat_ffa_find_target_vcpu(current, intid);
524 break;
525 }
526
527 /*
528 * End the interrupt to drop the running priority. It also deactivates
529 * the physical interrupt. If not, the interrupt could trigger again
530 * after resuming current vCPU.
531 */
532 plat_interrupts_end_of_interrupt(intid);
533
Daniel Boulby7011b5a2024-10-15 18:27:26 +0100534 if (target_vcpu == NULL) {
535 /* No further handling required. Resume the current vCPU. */
536 *next = NULL;
537 return;
538 }
539
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000540 target_vm_locked = vm_lock(target_vcpu->vm);
541
542 if (target_vcpu == current) {
543 current_locked = vcpu_lock(current);
544 target_vcpu_locked = current_locked;
545 } else {
546 struct two_vcpu_locked vcpus_locked;
547 /* Lock both vCPUs at once to avoid deadlock. */
548 vcpus_locked = vcpu_lock_both(current, target_vcpu);
549 current_locked = vcpus_locked.vcpu1;
550 target_vcpu_locked = vcpus_locked.vcpu2;
551 }
552
553 /*
554 * A race condition can occur with the execution contexts belonging to
555 * an MP SP. An interrupt targeting the execution context on present
556 * core can trigger while the execution context of this SP on a
557 * different core is being aborted. In such scenario, the physical
558 * interrupts beloning to the aborted SP are disabled and the current
559 * execution context is resumed.
560 */
561 if (target_vcpu->state == VCPU_STATE_ABORTED ||
562 atomic_load_explicit(&target_vcpu->vm->aborting,
563 memory_order_relaxed)) {
564 /* Clear fields corresponding to secure interrupt handling. */
565 vcpu_secure_interrupt_complete(target_vcpu_locked);
Karl Meakin117c8082024-12-04 16:03:28 +0000566 ffa_vm_disable_interrupts(target_vm_locked);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000567
568 /* Resume current vCPU. */
569 *next = NULL;
570 } else {
571 /*
572 * SPMC has started handling a secure interrupt with a clean
573 * slate. This signal should be false unless there was a bug in
574 * source code. Hence, use assert rather than CHECK.
575 */
576 assert(!target_vcpu->requires_deactivate_call);
577
578 /* Set the interrupt pending in the target vCPU. */
579 vcpu_interrupt_inject(target_vcpu_locked, v_intid);
580
581 switch (intid) {
582 case HF_IPI_INTID:
583 if (hf_ipi_handle(target_vcpu_locked)) {
584 *next = NULL;
585 break;
586 }
587 /*
588 * Fall through in the case handling has not been fully
589 * completed.
590 */
591 default:
592 /*
593 * Either invoke the handler related to partitions from
594 * S-EL0 or from S-EL1.
595 */
596 *next = target_vcpu_locked.vcpu->vm->el0_partition
597 ? plat_ffa_signal_secure_interrupt_sel0(
598 current_locked,
599 target_vcpu_locked, v_intid)
600 : plat_ffa_signal_secure_interrupt_sel1(
601 current_locked,
602 target_vcpu_locked, v_intid);
603 }
604 }
605
606 if (target_vcpu_locked.vcpu != NULL) {
607 vcpu_unlock(&target_vcpu_locked);
608 }
609
610 vcpu_unlock(&current_locked);
611 vm_unlock(&target_vm_locked);
612}
613
Karl Meakin117c8082024-12-04 16:03:28 +0000614bool ffa_interrupts_inject_notification_pending_interrupt(
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000615 struct vcpu_locked target_locked, struct vcpu_locked current_locked,
616 struct vm_locked receiver_locked)
617{
618 struct vm *next_vm = target_locked.vcpu->vm;
619 bool ret = false;
620
621 /*
622 * Inject the NPI if:
623 * - The targeted VM ID is from this world (i.e. if it is an SP).
624 * - The partition has global pending notifications and an NPI hasn't
625 * been injected yet.
626 * - There are pending per-vCPU notifications in the next vCPU.
627 */
628 if (vm_id_is_current_world(next_vm->id) &&
629 (vm_are_per_vcpu_notifications_pending(
630 receiver_locked, vcpu_index(target_locked.vcpu)) ||
631 (vm_are_global_notifications_pending(receiver_locked) &&
632 !vm_notifications_is_npi_injected(receiver_locked)))) {
633 api_interrupt_inject_locked(target_locked,
634 HF_NOTIFICATION_PENDING_INTID,
635 current_locked, NULL);
636 vm_notifications_set_npi_injected(receiver_locked, true);
637 ret = true;
638 }
639
640 return ret;
641}
642
Karl Meakin117c8082024-12-04 16:03:28 +0000643struct vcpu *ffa_interrupts_unwind_nwd_call_chain(struct vcpu *current_vcpu)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000644{
645 struct vcpu *next;
646 struct two_vcpu_locked both_vcpu_locked;
647
648 /*
649 * The action specified by SP in its manifest is ``Non-secure interrupt
650 * is signaled``. Refer to section 8.2.4 rules and guidelines bullet 4.
651 * Hence, the call chain starts unwinding. The current vCPU must have
652 * been a part of NWd scheduled call chain. Therefore, it is pre-empted
653 * and execution is either handed back to the normal world or to the
654 * previous SP vCPU in the call chain through the FFA_INTERRUPT ABI.
655 * The api_preempt() call is equivalent to calling
656 * api_switch_to_other_world for current vCPU passing FFA_INTERRUPT. The
657 * SP can be resumed later by FFA_RUN.
658 */
659 CHECK(current_vcpu->scheduling_mode == NWD_MODE);
660 assert(current_vcpu->call_chain.next_node == NULL);
661
662 if (current_vcpu->call_chain.prev_node == NULL) {
663 /* End of NWd scheduled call chain */
664 return api_preempt(current_vcpu);
665 }
666
667 next = current_vcpu->call_chain.prev_node;
668 CHECK(next != NULL);
669
670 /*
671 * Lock both vCPUs. Strictly speaking, it may not be necessary since
672 * next is guaranteed to be in BLOCKED state as it is the predecessor of
673 * the current vCPU in the present call chain.
674 */
675 both_vcpu_locked = vcpu_lock_both(current_vcpu, next);
676
677 /* Removing a node from an existing call chain. */
678 current_vcpu->call_chain.prev_node = NULL;
679 current_vcpu->state = VCPU_STATE_PREEMPTED;
680
681 /*
682 * SPMC applies the runtime model till when the vCPU transitions from
683 * running to waiting state. Moreover, the SP continues to remain in
684 * its CPU cycle allocation mode. Hence, rt_model and scheduling_mode
685 * are not changed here.
686 */
687 assert(next->state == VCPU_STATE_BLOCKED);
688 assert(next->call_chain.next_node == current_vcpu);
689
690 next->call_chain.next_node = NULL;
691
692 vcpu_set_running(both_vcpu_locked.vcpu2,
693 &(struct ffa_value){
694 .func = FFA_INTERRUPT_32,
695 .arg1 = ffa_vm_vcpu(current_vcpu->vm->id,
696 vcpu_index(current_vcpu)),
697 });
698
699 sl_unlock(&next->lock);
700 sl_unlock(&current_vcpu->lock);
701
702 return next;
703}
704
705static void plat_ffa_enable_virtual_maintenance_interrupts(
706 struct vcpu_locked current_locked)
707{
708 struct vcpu *current;
709 struct interrupts *interrupts;
710 struct vm *vm;
711
712 current = current_locked.vcpu;
713 interrupts = &current->interrupts;
714 vm = current->vm;
715
Karl Meakin117c8082024-12-04 16:03:28 +0000716 if (ffa_vm_managed_exit_supported(vm)) {
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000717 vcpu_virt_interrupt_set_enabled(interrupts,
718 HF_MANAGED_EXIT_INTID);
719 /*
720 * SPMC decides the interrupt type for Managed exit signal based
721 * on the partition manifest.
722 */
723 if (vm->me_signal_virq) {
724 vcpu_virt_interrupt_set_type(interrupts,
725 HF_MANAGED_EXIT_INTID,
726 INTERRUPT_TYPE_IRQ);
727 } else {
728 vcpu_virt_interrupt_set_type(interrupts,
729 HF_MANAGED_EXIT_INTID,
730 INTERRUPT_TYPE_FIQ);
731 }
732 }
733
734 if (vm->notifications.enabled) {
735 vcpu_virt_interrupt_set_enabled(interrupts,
736 HF_NOTIFICATION_PENDING_INTID);
737 }
738}
739
740/**
741 * Enable relevant virtual interrupts for Secure Partitions.
742 * For all SPs, any applicable virtual maintenance interrupts are enabled.
743 * Additionally, for S-EL0 partitions, all the interrupts declared in the
744 * partition manifest are enabled at the virtual interrupt controller
745 * interface early during the boot stage as an S-EL0 SP need not call
746 * HF_INTERRUPT_ENABLE hypervisor ABI explicitly.
747 */
Karl Meakin117c8082024-12-04 16:03:28 +0000748void ffa_interrupts_enable_virtual_interrupts(struct vcpu_locked current_locked,
749 struct vm_locked vm_locked)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000750{
751 struct vcpu *current;
752 struct interrupts *interrupts;
753 struct vm *vm;
754
755 current = current_locked.vcpu;
756 interrupts = &current->interrupts;
757 vm = current->vm;
758 assert(vm == vm_locked.vm);
759
760 if (vm->el0_partition) {
761 for (uint32_t k = 0; k < VM_MANIFEST_MAX_INTERRUPTS; k++) {
762 struct interrupt_descriptor int_desc;
763
764 int_desc = vm_locked.vm->interrupt_desc[k];
765
766 /* Interrupt descriptors are populated contiguously. */
767 if (!int_desc.valid) {
768 break;
769 }
770 vcpu_virt_interrupt_set_enabled(interrupts,
771 int_desc.interrupt_id);
772 }
773 }
774
775 plat_ffa_enable_virtual_maintenance_interrupts(current_locked);
776}
777
778/**
779 * Reconfigure the interrupt belonging to the current partition at runtime.
780 * At present, this paravirtualized interface only allows the following
781 * commands which signify what change is being requested by the current
782 * partition:
783 * - Change the target CPU of the interrupt.
784 * - Change the security state of the interrupt.
785 * - Enable or disable the physical interrupt.
786 */
Karl Meakin117c8082024-12-04 16:03:28 +0000787int64_t ffa_interrupts_reconfigure(uint32_t int_id, uint32_t command,
788 uint32_t value, struct vcpu *current)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000789{
790 struct vm *vm = current->vm;
791 struct vm_locked vm_locked;
792 int64_t ret = -1;
793 struct interrupt_descriptor *int_desc = NULL;
794
795 /*
796 * Lock VM to protect interrupt descriptor from being modified
797 * concurrently.
798 */
799 vm_locked = vm_lock(vm);
800
801 switch (command) {
802 case INT_RECONFIGURE_TARGET_PE:
803 /* Here, value represents the target PE index. */
804 if (value >= MAX_CPUS) {
805 dlog_verbose(
806 "Illegal target PE index specified while "
807 "reconfiguring interrupt %x\n",
808 int_id);
809 goto out_unlock;
810 }
811
812 /*
813 * An UP SP cannot reconfigure an interrupt to be targetted to
814 * any other physical CPU except the one it is currently
815 * running on.
816 */
817 if (vm_is_up(vm) && value != cpu_index(current->cpu)) {
818 dlog_verbose(
819 "Illegal target PE index specified by current "
820 "UP SP\n");
821 goto out_unlock;
822 }
823
824 /* Configure the interrupt to be routed to a specific CPU. */
825 int_desc = vm_interrupt_set_target_mpidr(
826 vm_locked, int_id, cpu_find_index(value)->id);
827 break;
828 case INT_RECONFIGURE_SEC_STATE:
829 /* Specify the new security state of the interrupt. */
830 if (value != INT_DESC_SEC_STATE_NS &&
831 value != INT_DESC_SEC_STATE_S) {
832 dlog_verbose(
833 "Illegal value %x specified while "
834 "reconfiguring interrupt %x\n",
835 value, int_id);
836 goto out_unlock;
837 }
838 int_desc = vm_interrupt_set_sec_state(vm_locked, int_id, value);
839 break;
840 case INT_RECONFIGURE_ENABLE:
841 /* Enable or disable the interrupt. */
842 if (value != INT_DISABLE && value != INT_ENABLE) {
843 dlog_verbose(
844 "Illegal value %x specified while "
845 "reconfiguring interrupt %x\n",
846 value, int_id);
847 goto out_unlock;
848 } else {
849 int_desc = vm_interrupt_set_enable(vm_locked, int_id,
850 value == INT_ENABLE);
851 }
852 break;
853 default:
854 dlog_verbose("Interrupt reconfigure: Unsupported command %x\n",
855 command);
856 goto out_unlock;
857 }
858
859 /* Check if the interrupt belongs to the current SP. */
860 if (int_desc == NULL) {
861 dlog_verbose("Interrupt %x does not belong to current SP\n",
862 int_id);
863 goto out_unlock;
864 }
865
866 ret = 0;
867 plat_interrupts_reconfigure_interrupt(*int_desc);
868
869out_unlock:
870 vm_unlock(&vm_locked);
871
872 return ret;
873}
874
875/* Returns the virtual interrupt id to be handled by SP. */
Karl Meakin117c8082024-12-04 16:03:28 +0000876uint32_t ffa_interrupts_get(struct vcpu_locked current_locked)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000877{
878 uint32_t int_id;
879
880 /*
881 * If there are any virtual interrupts in the queue, return the first
882 * entry. Else, return the pending interrupt from the bitmap.
883 */
884 if (vcpu_interrupt_queue_peek(current_locked, &int_id)) {
885 struct interrupts *interrupts;
886
887 /*
888 * Mark the virtual interrupt as no longer pending and decrement
889 * the count.
890 */
891 interrupts = &current_locked.vcpu->interrupts;
892 vcpu_virt_interrupt_clear_pending(interrupts, int_id);
893 vcpu_interrupt_count_decrement(current_locked, interrupts,
894 int_id);
895
896 return int_id;
897 }
898
899 return api_interrupt_get(current_locked);
900}