blob: 4868fa76084587897cfd3392bc6fe6ab05729274 [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 Meakinfa1dcb82025-02-10 16:47:50 +000016#include "hf/ffa/direct_messaging.h"
J-Alvesce42f7a2025-02-10 13:57:41 +000017#include "hf/ffa/notifications.h"
Karl Meakin902af082024-11-28 14:58:38 +000018#include "hf/ffa/vm.h"
Karl Meakin8e58ddc2024-11-08 23:19:34 +000019#include "hf/hf_ipi.h"
20#include "hf/vm.h"
21
22/**
Daniel Boulbyaa386fd2025-02-07 15:01:20 +000023 * This function has been deprecated and it's contents moved into
24 * api_interrupt_get in order to align the bitmap and queue for tracking
25 * interupts.
Karl Meakin8e58ddc2024-11-08 23:19:34 +000026 * Returns 0 on success, or -1 otherwise.
27 */
Karl Meakin117c8082024-12-04 16:03:28 +000028int64_t ffa_interrupts_deactivate(uint32_t pint_id, uint32_t vint_id,
29 struct vcpu *current)
Karl Meakin8e58ddc2024-11-08 23:19:34 +000030{
Daniel Boulby3c1506b2025-02-25 10:49:51 +000031 (void)pint_id;
32 (void)vint_id;
Daniel Boulbyaa386fd2025-02-07 15:01:20 +000033 (void)current;
34 return 0;
Karl Meakin8e58ddc2024-11-08 23:19:34 +000035}
36
Karl Meakinca38ef92025-02-13 14:20:23 +000037static struct vcpu *ffa_interrupts_find_target_vcpu_secure_interrupt(
Karl Meakin8e58ddc2024-11-08 23:19:34 +000038 struct vcpu *current, uint32_t interrupt_id)
39{
40 /*
41 * Find which VM/SP owns this interrupt. We then find the
42 * corresponding vCPU context for this CPU.
43 */
44 for (ffa_vm_count_t index = 0; index < vm_get_count(); ++index) {
45 struct vm *vm = vm_find_index(index);
46
J-Alvesa89a0a02025-03-17 11:18:20 +000047 for (uint32_t j = 0; j < VM_MANIFEST_MAX_INTERRUPTS; j++) {
Karl Meakin8e58ddc2024-11-08 23:19:34 +000048 struct interrupt_descriptor int_desc =
49 vm->interrupt_desc[j];
50
51 /*
52 * Interrupt descriptors are populated
53 * contiguously.
54 */
55 if (!int_desc.valid) {
56 break;
57 }
58 if (int_desc.interrupt_id == interrupt_id) {
59 return api_ffa_get_vm_vcpu(vm, current);
60 }
61 }
62 }
63
64 return NULL;
65}
66
Karl Meakinca38ef92025-02-13 14:20:23 +000067static struct vcpu *ffa_interrupts_find_target_vcpu(struct vcpu *current,
J-Alvesde211782025-02-07 14:44:39 +000068 uint32_t interrupt_id,
69 uint32_t *v_intid)
Karl Meakin8e58ddc2024-11-08 23:19:34 +000070{
71 struct vcpu *target_vcpu;
72
J-Alvesde211782025-02-07 14:44:39 +000073 assert(current != NULL);
74 assert(v_intid != NULL);
75
76 *v_intid = interrupt_id;
77
Karl Meakin8e58ddc2024-11-08 23:19:34 +000078 switch (interrupt_id) {
J-Alvesde211782025-02-07 14:44:39 +000079 case SPURIOUS_INTID_OTHER_WORLD:
80 /*
81 * Spurious interrupt ID indicating that there are no pending
82 * interrupts to acknowledge. For such scenarios, resume the
83 * current vCPU.
84 */
85 target_vcpu = NULL;
86 break;
Karl Meakin8e58ddc2024-11-08 23:19:34 +000087 case HF_IPI_INTID:
Daniel Boulby7011b5a2024-10-15 18:27:26 +010088 /*
89 * Get the next vCPU with a pending IPI. If all vCPUs
90 * have had their IPIs handled this will return NULL.
91 */
92 target_vcpu = hf_ipi_get_pending_target_vcpu(current);
Karl Meakin8e58ddc2024-11-08 23:19:34 +000093 break;
J-Alvesde211782025-02-07 14:44:39 +000094 case ARM_SEL2_TIMER_PHYS_INT:
95 /* Disable the S-EL2 physical timer */
96 host_timer_disable();
97 target_vcpu = timer_find_target_vcpu(current);
98
99 if (target_vcpu != NULL) {
100 *v_intid = HF_VIRTUAL_TIMER_INTID;
101 }
102 /*
103 * It is possible for target_vcpu to be NULL in case of spurious
104 * timer interrupt.
105 */
106 break;
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000107 case ARM_EL1_VIRT_TIMER_PHYS_INT:
Karl Meakin402b1fe2025-03-20 14:52:55 +0000108 [[fallthrough]];
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000109 case ARM_EL1_PHYS_TIMER_PHYS_INT:
110 panic("Timer interrupt not expected to fire: %u\n",
111 interrupt_id);
112 default:
Karl Meakinca38ef92025-02-13 14:20:23 +0000113 target_vcpu = ffa_interrupts_find_target_vcpu_secure_interrupt(
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000114 current, interrupt_id);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000115
Daniel Boulby7011b5a2024-10-15 18:27:26 +0100116 /* The target vCPU for a secure interrupt cannot be NULL. */
117 CHECK(target_vcpu != NULL);
118 }
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000119
120 return target_vcpu;
121}
122
123/*
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000124 * If the current vCPU is being preempted, record this in the target vCPU
125 * and set the current states to VCPU_STATE_PREEMPTED.
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000126 */
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000127static void ffa_interrupts_set_preempted_vcpu(
128 struct vcpu_locked target_vcpu_locked,
129 struct vcpu_locked current_locked)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000130{
131 struct vcpu *target_vcpu = target_vcpu_locked.vcpu;
132 struct vcpu *preempted_vcpu = current_locked.vcpu;
133
J-Alvesac62a852025-02-07 19:03:07 +0000134 assert(target_vcpu != NULL);
135 assert(preempted_vcpu != NULL);
136
137 target_vcpu->preempted_vcpu = preempted_vcpu;
138 preempted_vcpu->state = VCPU_STATE_PREEMPTED;
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000139}
140
141/**
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000142 * If the interrupts were indeed masked by SPMC before an SP's vCPU was resumed,
143 * restore the priority mask thereby allowing the interrupts to be delivered.
144 */
145void ffa_interrupts_unmask(struct vcpu *current)
146{
147 plat_interrupts_set_priority_mask(current->prev_interrupt_priority);
148}
149
150/**
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000151 * Enforce action of an SP in response to non-secure or other-secure interrupt
152 * by changing the priority mask. Effectively, physical interrupts shall not
153 * trigger which has the same effect as queueing interrupts.
154 */
Karl Meakinfa1dcb82025-02-10 16:47:50 +0000155void ffa_interrupts_mask(struct vcpu_locked receiver_vcpu_locked)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000156{
157 struct vcpu *receiver_vcpu = receiver_vcpu_locked.vcpu;
158 uint8_t current_priority;
159
160 /* Save current value of priority mask. */
161 current_priority = plat_interrupts_get_priority_mask();
162 receiver_vcpu->prev_interrupt_priority = current_priority;
163
164 if (receiver_vcpu->vm->other_s_interrupts_action ==
165 OTHER_S_INT_ACTION_QUEUED ||
166 receiver_vcpu->scheduling_mode == SPMC_MODE) {
167 /*
168 * If secure interrupts not masked yet, mask them now. We could
169 * enter SPMC scheduled mode when an EL3 SPMD Logical partition
170 * sends a direct request, and we are making the IMPDEF choice
171 * to mask interrupts when such a situation occurs. This keeps
172 * design simple.
173 */
174 if (current_priority > SWD_MASK_ALL_INT) {
175 plat_interrupts_set_priority_mask(SWD_MASK_ALL_INT);
176 }
177 } else if (receiver_vcpu->vm->ns_interrupts_action ==
178 NS_ACTION_QUEUED) {
179 /* If non secure interrupts not masked yet, mask them now. */
180 if (current_priority > SWD_MASK_NS_INT) {
181 plat_interrupts_set_priority_mask(SWD_MASK_NS_INT);
182 }
183 }
184}
185
J-Alves86e883a2025-03-13 14:06:33 +0000186/**
187 * Change the state of both current vCPU and the target vCPU.
188 * For S-EL0 partitions it will pop from the queue and write to the vCPU
189 * the return FFA_INTERRUPT(virtual interrupt).
190 * For S-EL1 partitions, it peeks to the queue to get the next interrupt
191 * ID, so it can be included in the return. Partition should still call
192 * `hf_interrupt_get()`.
193 *
194 * If `interrupt_return` is passed as NULL, the function will write to
195 * partition context.
196 * Otherwise, it will be used to return the ffa_value with the FFA_INTERRUPT
197 * ABI.
198 *
199 * Returns the injected virtual interrupt ID.
200 */
201static uint32_t interrupt_resume_waiting(struct vcpu_locked current_locked,
202 struct vcpu_locked target_vcpu_locked)
J-Alves20160602025-02-07 17:46:22 +0000203{
J-Alves20160602025-02-07 17:46:22 +0000204 struct vcpu *target_vcpu = target_vcpu_locked.vcpu;
J-Alves86e883a2025-03-13 14:06:33 +0000205 /*
206 * Since S-EL0 partitions will not receive the interrupt through a vIRQ
207 * signal in addition to the FFA_INTERRUPT ERET, make the interrupt no
208 * longer pending at this point. Otherwise keep it as pending for
209 * when the S-EL1 parition calls hf_interrupt_get.
210 */
211 uint32_t pending_intid =
212 target_vcpu_locked.vcpu->vm->el0_partition
213 ? vcpu_virt_interrupt_get_pending_and_enabled(
214 target_vcpu_locked)
215 : vcpu_virt_interrupt_peek_pending_and_enabled(
216 target_vcpu_locked);
J-Alves20160602025-02-07 17:46:22 +0000217
218 /* FF-A v1.1 EAC0 Table 8.2 case 1 and Table 12.10. */
219 vcpu_enter_secure_interrupt_rtm(target_vcpu_locked);
220 ffa_interrupts_mask(target_vcpu_locked);
J-Alves20160602025-02-07 17:46:22 +0000221 ffa_interrupts_set_preempted_vcpu(target_vcpu_locked, current_locked);
222
J-Alves20160602025-02-07 17:46:22 +0000223 if (target_vcpu->cpu != current_locked.vcpu->cpu) {
224 /*
225 * The target vcpu could have migrated to a different
226 * physical CPU. SPMC will migrate it to current
227 * physical CPU and resume it.
228 */
229 assert(target_vcpu->vm->vcpu_count == 1);
230 target_vcpu->cpu = current_locked.vcpu->cpu;
231 }
232
J-Alves86e883a2025-03-13 14:06:33 +0000233 return pending_intid;
J-Alves20160602025-02-07 17:46:22 +0000234}
235
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000236/**
J-Alvesac62a852025-02-07 19:03:07 +0000237 * Handles the secure interrupt according to the target vCPU's state.
238 * Returns the next vCPU to resume accordingly.
239 * If it returns NULL, the current vCPU shall be resumed.
240 * This might be if the target vCPU is the current vCPU, or if the
241 * target vCPU is not in a state in which it can be resumed to handle
242 * the secure interrupt.
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000243 */
J-Alvesac62a852025-02-07 19:03:07 +0000244static struct vcpu *ffa_interrupts_signal_secure_interrupt(
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000245 struct vcpu_locked current_locked,
246 struct vcpu_locked target_vcpu_locked, uint32_t v_intid)
247{
248 struct vcpu *target_vcpu = target_vcpu_locked.vcpu;
249 struct vcpu *current = current_locked.vcpu;
250 struct vcpu *next = NULL;
251
J-Alves7e7fce02025-02-07 15:14:56 +0000252 /*
253 * The target vcpu has migrated to a different physical
254 * CPU. Hence, it cannot be resumed on this CPU, SPMC
255 * resumes current vCPU.
256 */
257 if (target_vcpu->cpu != current_locked.vcpu->cpu) {
258 assert(target_vcpu->vm->vcpu_count == 1);
259 }
260
J-Alvesac62a852025-02-07 19:03:07 +0000261 /* Secure interrupt signaling and queuing for SP. */
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000262 switch (target_vcpu->state) {
J-Alves20160602025-02-07 17:46:22 +0000263 case VCPU_STATE_WAITING:
Madhukar Pappireddyd90e4b02025-07-24 15:21:52 -0500264 /*
265 * regs_available == false indicates the vCPU context has not
266 * yet been saved, even though state was just set to WAITING.
267 * Between marking WAITING and calling api_regs_state_saved, the
268 * vCPU is still logically RUNNING. Treating it as WAITING here
269 * would open a narrow race where another SPMC on a different
270 * CPU could steal the vCPU lock and resume execution
271 * prematurely. To avoid this, bail out when regs_available is
272 * false and let the vCPU continue until its registers are
273 * saved.
274 */
275 if (!target_vcpu->regs_available) {
276 /* Interrupt has been injected in the vCPU state. */
277 break;
278 }
279
J-Alvesce42f7a2025-02-10 13:57:41 +0000280 if (!target_vcpu->vm->sri_policy.intr_while_waiting) {
J-Alves86e883a2025-03-13 14:06:33 +0000281 uint32_t inject_int_id = interrupt_resume_waiting(
282 current_locked, target_vcpu_locked);
283 struct ffa_value int_ret =
284 api_ffa_interrupt_return(inject_int_id);
285
286 if (inject_int_id != 0) {
287 assert(v_intid == inject_int_id);
288 }
289
290 next = target_vcpu;
291
292 vcpu_set_running(target_vcpu_locked, &int_ret);
J-Alvesce42f7a2025-02-10 13:57:41 +0000293 } else {
294 dlog_verbose(
295 "%s: SP is waiting, SRI delayed due to "
296 "interrupt. Partition %x, vcpu %x, interrupt "
297 "%x\n",
298 __func__, target_vcpu->vm->id,
299 vcpu_index(target_vcpu), v_intid);
300 ffa_notifications_sri_set_delayed(target_vcpu->cpu);
301 }
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000302 break;
303 case VCPU_STATE_BLOCKED:
J-Alvesac62a852025-02-07 19:03:07 +0000304 if (!target_vcpu->vm->el0_partition &&
305 target_vcpu->cpu == current_locked.vcpu->cpu &&
J-Alves7e7fce02025-02-07 15:14:56 +0000306 ffa_direct_msg_precedes_in_call_chain(current_locked,
307 target_vcpu_locked)) {
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000308 struct ffa_value ret_interrupt =
309 api_ffa_interrupt_return(0);
310
311 /*
312 * If the target vCPU ran earlier in the same call
313 * chain as the current vCPU, SPMC leaves all
314 * intermediate execution contexts in blocked state and
315 * resumes the target vCPU for handling secure
316 * interrupt.
317 * Under the current design, there is only one possible
318 * scenario in which this could happen: both the
319 * preempted (i.e. current) and target vCPU are in the
320 * same NWd scheduled call chain and is described in the
321 * Scenario 1 of Table 8.4 in EAC0 spec.
322 */
323 assert(current_locked.vcpu->scheduling_mode ==
324 NWD_MODE);
325 assert(target_vcpu->scheduling_mode == NWD_MODE);
326
327 /*
328 * The execution preempted the call chain that involved
329 * the targeted and the current SPs.
330 * The targetted SP is set running, whilst the
331 * preempted SP is set PREEMPTED.
332 */
333 vcpu_set_running(target_vcpu_locked, &ret_interrupt);
334
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000335 ffa_interrupts_set_preempted_vcpu(target_vcpu_locked,
336 current_locked);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000337 next = target_vcpu;
J-Alvesac62a852025-02-07 19:03:07 +0000338 break;
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000339 }
J-Alvesac62a852025-02-07 19:03:07 +0000340
341 /*
342 * `next` is NULL.
343 * Either:
344 * - EL0 paritition can't be resumed when in blocked state.
345 * - The target vCPU has migrated to a different
346 * physical CPU. Hence, it cannot be resumed on this
347 * CPU, SPMC resumes current vCPU.
348 * - The target vCPU cannot be resumed now because it is
349 * in BLOCKED state (it yielded CPU cycles using
350 * FFA_YIELD). SPMC queues the virtual interrupt and
351 * resumes the current vCPU which could belong to either
352 * a VM or a SP.
353 */
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000354 break;
355 case VCPU_STATE_PREEMPTED:
J-Alves7e7fce02025-02-07 15:14:56 +0000356 /*
357 * We do not resume a target vCPU that has been already
358 * pre-empted by an interrupt. Make the vIRQ pending for
359 * target SP(i.e., queue the interrupt) and continue to
360 * resume current vCPU. Refer to section 8.3.2.1 bullet
361 * 3 in the FF-A v1.1 EAC0 spec.
362 */
J-Alvesac62a852025-02-07 19:03:07 +0000363 if (!target_vcpu->vm->el0_partition &&
364 target_vcpu->cpu == current_locked.vcpu->cpu &&
J-Alves7e7fce02025-02-07 15:14:56 +0000365 current->vm->id == HF_OTHER_WORLD_ID) {
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000366 /*
J-Alves7e7fce02025-02-07 15:14:56 +0000367 * The target vCPU must have been preempted by a
368 * non secure interrupt. It could not have been
369 * preempted by a secure interrupt as current
370 * SPMC implementation does not allow secure
371 * interrupt prioritization. Moreover, the
372 * target vCPU should have been in Normal World
373 * scheduled mode as SPMC scheduled mode call
374 * chain cannot be preempted by a non secure
375 * interrupt.
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000376 */
J-Alves7e7fce02025-02-07 15:14:56 +0000377 CHECK(target_vcpu->scheduling_mode == NWD_MODE);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000378 }
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000379 break;
380 case VCPU_STATE_RUNNING:
J-Alvesac62a852025-02-07 19:03:07 +0000381 /*
382 * Interrupt has been injected in the vCPU state.
383 */
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000384 break;
385 case VCPU_STATE_BLOCKED_INTERRUPT:
386 /* WFI is no-op for SP. Fall through. */
387 default:
388 /*
389 * vCPU of Target SP cannot be in OFF/ABORTED state if it has
390 * to handle secure interrupt.
391 */
392 panic("Secure interrupt cannot be signaled to target SP\n");
393 break;
394 }
395
396 return next;
397}
398
399/**
400 * Obtain the physical interrupt that triggered from the interrupt controller,
401 * and inject the corresponding virtual interrupt to the target vCPU.
402 * When PEs executing in the Normal World, and secure interrupts trigger,
403 * execution is trapped into EL3. SPMD then routes the interrupt to SPMC
404 * through FFA_INTERRUPT_32 ABI synchronously using eret conduit.
405 */
Karl Meakin117c8082024-12-04 16:03:28 +0000406void ffa_interrupts_handle_secure_interrupt(struct vcpu *current,
407 struct vcpu **next)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000408{
409 struct vcpu *target_vcpu;
410 struct vcpu_locked target_vcpu_locked =
411 (struct vcpu_locked){.vcpu = NULL};
412 struct vcpu_locked current_locked;
413 uint32_t intid;
414 struct vm_locked target_vm_locked;
415 uint32_t v_intid;
416
417 /* Find pending interrupt id. This also activates the interrupt. */
418 intid = plat_interrupts_get_pending_interrupt_id();
419 v_intid = intid;
420
J-Alvesde211782025-02-07 14:44:39 +0000421 /* Get the target vCPU and get the virtual interrupt ID. */
422 target_vcpu = ffa_interrupts_find_target_vcpu(current, intid, &v_intid);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000423
424 /*
J-Alvesde211782025-02-07 14:44:39 +0000425 * Spurious interrupt ID indicates there is no pending interrupt to
426 * acknowledge so we do not need to call end of interrupt.
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000427 */
J-Alvesde211782025-02-07 14:44:39 +0000428 if (v_intid != SPURIOUS_INTID_OTHER_WORLD) {
429 /*
430 * End the interrupt to drop the running priority. It also
431 * deactivates the physical interrupt. If not, the interrupt
432 * could trigger again after resuming current vCPU.
433 */
434 plat_interrupts_end_of_interrupt(intid);
435 }
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000436
Daniel Boulby7011b5a2024-10-15 18:27:26 +0100437 if (target_vcpu == NULL) {
438 /* No further handling required. Resume the current vCPU. */
439 *next = NULL;
440 return;
441 }
442
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000443 target_vm_locked = vm_lock(target_vcpu->vm);
444
445 if (target_vcpu == current) {
446 current_locked = vcpu_lock(current);
447 target_vcpu_locked = current_locked;
448 } else {
449 struct two_vcpu_locked vcpus_locked;
450 /* Lock both vCPUs at once to avoid deadlock. */
451 vcpus_locked = vcpu_lock_both(current, target_vcpu);
452 current_locked = vcpus_locked.vcpu1;
453 target_vcpu_locked = vcpus_locked.vcpu2;
454 }
455
456 /*
457 * A race condition can occur with the execution contexts belonging to
458 * an MP SP. An interrupt targeting the execution context on present
459 * core can trigger while the execution context of this SP on a
460 * different core is being aborted. In such scenario, the physical
461 * interrupts beloning to the aborted SP are disabled and the current
462 * execution context is resumed.
463 */
464 if (target_vcpu->state == VCPU_STATE_ABORTED ||
465 atomic_load_explicit(&target_vcpu->vm->aborting,
466 memory_order_relaxed)) {
467 /* Clear fields corresponding to secure interrupt handling. */
468 vcpu_secure_interrupt_complete(target_vcpu_locked);
Karl Meakin117c8082024-12-04 16:03:28 +0000469 ffa_vm_disable_interrupts(target_vm_locked);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000470
471 /* Resume current vCPU. */
472 *next = NULL;
473 } else {
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000474 /* Set the interrupt pending in the target vCPU. */
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000475 vcpu_virt_interrupt_inject(target_vcpu_locked, v_intid);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000476
477 switch (intid) {
478 case HF_IPI_INTID:
479 if (hf_ipi_handle(target_vcpu_locked)) {
480 *next = NULL;
481 break;
482 }
483 /*
484 * Fall through in the case handling has not been fully
485 * completed.
486 */
Karl Meakin402b1fe2025-03-20 14:52:55 +0000487 [[fallthrough]];
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000488 default:
489 /*
490 * Either invoke the handler related to partitions from
491 * S-EL0 or from S-EL1.
492 */
J-Alvesac62a852025-02-07 19:03:07 +0000493 *next = ffa_interrupts_signal_secure_interrupt(
494 current_locked, target_vcpu_locked, v_intid);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000495 }
496 }
497
498 if (target_vcpu_locked.vcpu != NULL) {
499 vcpu_unlock(&target_vcpu_locked);
500 }
501
502 vcpu_unlock(&current_locked);
503 vm_unlock(&target_vm_locked);
504}
505
Karl Meakin117c8082024-12-04 16:03:28 +0000506bool ffa_interrupts_inject_notification_pending_interrupt(
Daniel Boulbyd49d0772025-01-15 11:19:36 +0000507 struct vcpu_locked target_locked, struct vm_locked receiver_locked)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000508{
509 struct vm *next_vm = target_locked.vcpu->vm;
510 bool ret = false;
511
512 /*
513 * Inject the NPI if:
514 * - The targeted VM ID is from this world (i.e. if it is an SP).
Daniel Boulby6e046112025-02-25 17:33:48 +0000515 * - The partition has global pending notifications or there are
516 * pending per-vCPU notifications in the next vCPU.
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000517 */
518 if (vm_id_is_current_world(next_vm->id) &&
519 (vm_are_per_vcpu_notifications_pending(
520 receiver_locked, vcpu_index(target_locked.vcpu)) ||
Daniel Boulby6e046112025-02-25 17:33:48 +0000521 vm_are_global_notifications_pending(receiver_locked))) {
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000522 vcpu_virt_interrupt_inject(target_locked,
523 HF_NOTIFICATION_PENDING_INTID);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000524 ret = true;
525 }
526
527 return ret;
528}
529
Karl Meakin117c8082024-12-04 16:03:28 +0000530struct vcpu *ffa_interrupts_unwind_nwd_call_chain(struct vcpu *current_vcpu)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000531{
532 struct vcpu *next;
533 struct two_vcpu_locked both_vcpu_locked;
534
535 /*
536 * The action specified by SP in its manifest is ``Non-secure interrupt
537 * is signaled``. Refer to section 8.2.4 rules and guidelines bullet 4.
538 * Hence, the call chain starts unwinding. The current vCPU must have
539 * been a part of NWd scheduled call chain. Therefore, it is pre-empted
540 * and execution is either handed back to the normal world or to the
541 * previous SP vCPU in the call chain through the FFA_INTERRUPT ABI.
542 * The api_preempt() call is equivalent to calling
543 * api_switch_to_other_world for current vCPU passing FFA_INTERRUPT. The
544 * SP can be resumed later by FFA_RUN.
545 */
546 CHECK(current_vcpu->scheduling_mode == NWD_MODE);
547 assert(current_vcpu->call_chain.next_node == NULL);
548
549 if (current_vcpu->call_chain.prev_node == NULL) {
550 /* End of NWd scheduled call chain */
551 return api_preempt(current_vcpu);
552 }
553
554 next = current_vcpu->call_chain.prev_node;
555 CHECK(next != NULL);
556
557 /*
558 * Lock both vCPUs. Strictly speaking, it may not be necessary since
559 * next is guaranteed to be in BLOCKED state as it is the predecessor of
560 * the current vCPU in the present call chain.
561 */
562 both_vcpu_locked = vcpu_lock_both(current_vcpu, next);
563
564 /* Removing a node from an existing call chain. */
565 current_vcpu->call_chain.prev_node = NULL;
566 current_vcpu->state = VCPU_STATE_PREEMPTED;
567
568 /*
569 * SPMC applies the runtime model till when the vCPU transitions from
570 * running to waiting state. Moreover, the SP continues to remain in
571 * its CPU cycle allocation mode. Hence, rt_model and scheduling_mode
572 * are not changed here.
573 */
574 assert(next->state == VCPU_STATE_BLOCKED);
575 assert(next->call_chain.next_node == current_vcpu);
576
577 next->call_chain.next_node = NULL;
578
579 vcpu_set_running(both_vcpu_locked.vcpu2,
580 &(struct ffa_value){
581 .func = FFA_INTERRUPT_32,
582 .arg1 = ffa_vm_vcpu(current_vcpu->vm->id,
583 vcpu_index(current_vcpu)),
584 });
585
586 sl_unlock(&next->lock);
587 sl_unlock(&current_vcpu->lock);
588
589 return next;
590}
591
Karl Meakinca38ef92025-02-13 14:20:23 +0000592static void ffa_interrupts_enable_virtual_maintenance_interrupts(
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000593 struct vcpu_locked current_locked)
594{
595 struct vcpu *current;
596 struct interrupts *interrupts;
597 struct vm *vm;
598
599 current = current_locked.vcpu;
600 interrupts = &current->interrupts;
601 vm = current->vm;
602
Karl Meakin117c8082024-12-04 16:03:28 +0000603 if (ffa_vm_managed_exit_supported(vm)) {
Daniel Boulbyd633a612025-03-07 18:08:04 +0000604 vcpu_virt_interrupt_enable(current_locked,
605 HF_MANAGED_EXIT_INTID, true);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000606 /*
607 * SPMC decides the interrupt type for Managed exit signal based
608 * on the partition manifest.
609 */
610 if (vm->me_signal_virq) {
611 vcpu_virt_interrupt_set_type(interrupts,
612 HF_MANAGED_EXIT_INTID,
613 INTERRUPT_TYPE_IRQ);
614 } else {
615 vcpu_virt_interrupt_set_type(interrupts,
616 HF_MANAGED_EXIT_INTID,
617 INTERRUPT_TYPE_FIQ);
618 }
619 }
620
621 if (vm->notifications.enabled) {
Daniel Boulbyd633a612025-03-07 18:08:04 +0000622 vcpu_virt_interrupt_enable(current_locked,
623 HF_NOTIFICATION_PENDING_INTID, true);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000624 }
625}
626
627/**
628 * Enable relevant virtual interrupts for Secure Partitions.
629 * For all SPs, any applicable virtual maintenance interrupts are enabled.
630 * Additionally, for S-EL0 partitions, all the interrupts declared in the
631 * partition manifest are enabled at the virtual interrupt controller
632 * interface early during the boot stage as an S-EL0 SP need not call
633 * HF_INTERRUPT_ENABLE hypervisor ABI explicitly.
634 */
Karl Meakin117c8082024-12-04 16:03:28 +0000635void ffa_interrupts_enable_virtual_interrupts(struct vcpu_locked current_locked,
636 struct vm_locked vm_locked)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000637{
638 struct vcpu *current;
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000639 struct vm *vm;
640
641 current = current_locked.vcpu;
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000642 vm = current->vm;
643 assert(vm == vm_locked.vm);
644
645 if (vm->el0_partition) {
646 for (uint32_t k = 0; k < VM_MANIFEST_MAX_INTERRUPTS; k++) {
647 struct interrupt_descriptor int_desc;
648
649 int_desc = vm_locked.vm->interrupt_desc[k];
650
651 /* Interrupt descriptors are populated contiguously. */
652 if (!int_desc.valid) {
653 break;
654 }
Daniel Boulbyd633a612025-03-07 18:08:04 +0000655 vcpu_virt_interrupt_enable(current_locked,
656 int_desc.interrupt_id, true);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000657 }
658 }
659
Karl Meakinca38ef92025-02-13 14:20:23 +0000660 ffa_interrupts_enable_virtual_maintenance_interrupts(current_locked);
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000661}
662
663/**
664 * Reconfigure the interrupt belonging to the current partition at runtime.
665 * At present, this paravirtualized interface only allows the following
666 * commands which signify what change is being requested by the current
667 * partition:
668 * - Change the target CPU of the interrupt.
669 * - Change the security state of the interrupt.
670 * - Enable or disable the physical interrupt.
671 */
Karl Meakin117c8082024-12-04 16:03:28 +0000672int64_t ffa_interrupts_reconfigure(uint32_t int_id, uint32_t command,
673 uint32_t value, struct vcpu *current)
Karl Meakin8e58ddc2024-11-08 23:19:34 +0000674{
675 struct vm *vm = current->vm;
676 struct vm_locked vm_locked;
677 int64_t ret = -1;
678 struct interrupt_descriptor *int_desc = NULL;
679
680 /*
681 * Lock VM to protect interrupt descriptor from being modified
682 * concurrently.
683 */
684 vm_locked = vm_lock(vm);
685
686 switch (command) {
687 case INT_RECONFIGURE_TARGET_PE:
688 /* Here, value represents the target PE index. */
689 if (value >= MAX_CPUS) {
690 dlog_verbose(
691 "Illegal target PE index specified while "
692 "reconfiguring interrupt %x\n",
693 int_id);
694 goto out_unlock;
695 }
696
697 /*
698 * An UP SP cannot reconfigure an interrupt to be targetted to
699 * any other physical CPU except the one it is currently
700 * running on.
701 */
702 if (vm_is_up(vm) && value != cpu_index(current->cpu)) {
703 dlog_verbose(
704 "Illegal target PE index specified by current "
705 "UP SP\n");
706 goto out_unlock;
707 }
708
709 /* Configure the interrupt to be routed to a specific CPU. */
710 int_desc = vm_interrupt_set_target_mpidr(
711 vm_locked, int_id, cpu_find_index(value)->id);
712 break;
713 case INT_RECONFIGURE_SEC_STATE:
714 /* Specify the new security state of the interrupt. */
715 if (value != INT_DESC_SEC_STATE_NS &&
716 value != INT_DESC_SEC_STATE_S) {
717 dlog_verbose(
718 "Illegal value %x specified while "
719 "reconfiguring interrupt %x\n",
720 value, int_id);
721 goto out_unlock;
722 }
723 int_desc = vm_interrupt_set_sec_state(vm_locked, int_id, value);
724 break;
725 case INT_RECONFIGURE_ENABLE:
726 /* Enable or disable the interrupt. */
727 if (value != INT_DISABLE && value != INT_ENABLE) {
728 dlog_verbose(
729 "Illegal value %x specified while "
730 "reconfiguring interrupt %x\n",
731 value, int_id);
732 goto out_unlock;
733 } else {
734 int_desc = vm_interrupt_set_enable(vm_locked, int_id,
735 value == INT_ENABLE);
736 }
737 break;
738 default:
739 dlog_verbose("Interrupt reconfigure: Unsupported command %x\n",
740 command);
741 goto out_unlock;
742 }
743
744 /* Check if the interrupt belongs to the current SP. */
745 if (int_desc == NULL) {
746 dlog_verbose("Interrupt %x does not belong to current SP\n",
747 int_id);
748 goto out_unlock;
749 }
750
751 ret = 0;
752 plat_interrupts_reconfigure_interrupt(*int_desc);
753
754out_unlock:
755 vm_unlock(&vm_locked);
756
757 return ret;
758}
759
Karl Meakin8d245542025-01-31 13:19:25 +0000760bool ffa_interrupts_intercept_call(struct vcpu_locked current_locked,
761 struct vcpu_locked next_locked,
J-Alves86e883a2025-03-13 14:06:33 +0000762 struct ffa_value *interrupt_ret)
Karl Meakin8d245542025-01-31 13:19:25 +0000763{
J-Alves4796d112025-02-12 14:39:00 +0000764 uint32_t intid;
765 struct vm *current_vm = current_locked.vcpu->vm;
766
767 /* No pending interrupts, no need to intercept or trigger SRI. */
768 if (vcpu_virt_interrupt_count_get(current_locked) == 0U) {
769 return false;
770 }
771
772 /*
773 * Do not intercept the call.
774 * Let the partition go into waiting state as planned.
775 * Pend the SRI on the next world switch to the NWd.
776 */
777 if (current_vm->sri_policy.intr_pending_entry_wait) {
778 dlog_verbose(
779 "Partition entry to wait. Interrupts pending. Send "
780 "SRI.\n");
781 ffa_notifications_sri_set_delayed(current_locked.vcpu->cpu);
782 return false;
783 }
784
J-Alves86e883a2025-03-13 14:06:33 +0000785 /**
786 * At this point the handling of ABIs which can be intercepted by
787 * 'ffa_interrupts_intercept_call' did all the partition/vCPU state
788 * changes assuming there were no interrupts pending, and the call
789 * wouldn't be preempted.
790 * So it helps to think the current partition/vCPU have changed.
791 * If the call is intercepted, the current partition is left in
792 * preempted state, and execution is given to the target of the
793 * interrupt. In the arguments to interrupt_resume_waiting, pass
794 * "next_locked" and "current_locked" in the arguments for current and
795 * next vCPU, respectively. This is according to the description
796 * above.
Daniel Boulby4b9add52025-02-25 11:02:00 +0000797 */
J-Alves86e883a2025-03-13 14:06:33 +0000798 intid = interrupt_resume_waiting(next_locked, current_locked);
Karl Meakin8d245542025-01-31 13:19:25 +0000799
J-Alves86e883a2025-03-13 14:06:33 +0000800 assert(interrupt_ret != NULL);
Karl Meakin8d245542025-01-31 13:19:25 +0000801
J-Alves4796d112025-02-12 14:39:00 +0000802 dlog_verbose("%s: Pending interrupt %d, intercepting FF-A call.\n",
803 __func__, intid);
Karl Meakin8d245542025-01-31 13:19:25 +0000804
J-Alves86e883a2025-03-13 14:06:33 +0000805 *interrupt_ret = api_ffa_interrupt_return(intid);
806
807 vcpu_set_running(current_locked, NULL);
808
J-Alves4796d112025-02-12 14:39:00 +0000809 return true;
Karl Meakin8d245542025-01-31 13:19:25 +0000810}