Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 1 | /* |
| 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/hf_ipi.h" |
| 10 | |
Karl Meakin | 7a664f6 | 2024-07-24 17:20:29 +0100 | [diff] [blame] | 11 | #include "hf/arch/plat/ffa/notifications.h" |
Daniel Boulby | 960be20 | 2024-08-22 10:53:11 +0100 | [diff] [blame] | 12 | |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 13 | #include "hf/cpu.h" |
| 14 | #include "hf/plat/interrupts.h" |
| 15 | |
| 16 | /** Interrupt priority for Inter-Processor Interrupt. */ |
| 17 | #define IPI_PRIORITY 0x0U |
| 18 | |
| 19 | /** |
| 20 | * Initialize the IPI SGI. |
| 21 | */ |
| 22 | void hf_ipi_init_interrupt(void) |
| 23 | { |
| 24 | /* Configure as a Secure SGI. */ |
| 25 | struct interrupt_descriptor ipi_desc = { |
| 26 | .interrupt_id = HF_IPI_INTID, |
| 27 | .type = INT_DESC_TYPE_SGI, |
| 28 | .sec_state = INT_DESC_SEC_STATE_S, |
| 29 | .priority = IPI_PRIORITY, |
| 30 | .valid = true, |
Daniel Boulby | 84d49b6 | 2024-11-04 18:25:59 +0000 | [diff] [blame] | 31 | .enabled = true, |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | plat_interrupts_configure_interrupt(ipi_desc); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns the target_vcpu for the pending IPI on the current CPU and |
| 39 | * resets the item in the list to NULL to show it has been retrieved. |
| 40 | */ |
| 41 | struct vcpu *hf_ipi_get_pending_target_vcpu(struct cpu *current) |
| 42 | { |
Daniel Boulby | 84d49b6 | 2024-11-04 18:25:59 +0000 | [diff] [blame] | 43 | struct vcpu *ret; |
| 44 | |
| 45 | sl_lock(¤t->lock); |
| 46 | |
| 47 | ret = current->ipi_target_vcpu; |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 48 | |
| 49 | current->ipi_target_vcpu = NULL; |
Daniel Boulby | 84d49b6 | 2024-11-04 18:25:59 +0000 | [diff] [blame] | 50 | |
| 51 | sl_unlock(¤t->lock); |
| 52 | |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Send and record the IPI for the target vCPU. |
| 58 | */ |
| 59 | void hf_ipi_send_interrupt(struct vm *vm, ffa_vcpu_index_t target_vcpu_index) |
| 60 | { |
| 61 | struct vcpu *target_vcpu = vm_get_vcpu(vm, target_vcpu_index); |
| 62 | struct cpu *target_cpu = target_vcpu->cpu; |
| 63 | |
Daniel Boulby | 84d49b6 | 2024-11-04 18:25:59 +0000 | [diff] [blame] | 64 | sl_lock(&target_cpu->lock); |
| 65 | |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 66 | target_cpu->ipi_target_vcpu = target_vcpu; |
Daniel Boulby | 84d49b6 | 2024-11-04 18:25:59 +0000 | [diff] [blame] | 67 | |
| 68 | sl_unlock(&target_cpu->lock); |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 69 | plat_interrupts_send_sgi(HF_IPI_INTID, target_cpu, true); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * IPI IRQ specific handling for the secure interrupt for each vCPU state: |
J-Alves | 180a65a | 2024-10-08 17:12:08 +0100 | [diff] [blame] | 74 | * - WAITING: Trigger an SRI so the NWd can schedule to target vCPU to run. |
| 75 | * - RUNNING: |
| 76 | * - PREEMPTED/BLOCKED: Return and allow the normal secure interrupt handling |
| 77 | * to handle the interrupt as usual. |
| 78 | * For all cases we must also mark the interrupt as complete from the |
| 79 | * SPMC perspective. |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 80 | * Returns True if the IPI SGI has been handled. |
| 81 | * False if further secure interrupt handling is required. |
| 82 | */ |
| 83 | bool hf_ipi_handle(struct vcpu_locked target_vcpu_locked) |
| 84 | { |
| 85 | struct vcpu *target_vcpu = target_vcpu_locked.vcpu; |
| 86 | |
| 87 | switch (target_vcpu->state) { |
Daniel Boulby | 960be20 | 2024-08-22 10:53:11 +0100 | [diff] [blame] | 88 | case VCPU_STATE_WAITING: |
Daniel Boulby | 960be20 | 2024-08-22 10:53:11 +0100 | [diff] [blame] | 89 | plat_ffa_sri_trigger_not_delayed(target_vcpu->cpu); |
| 90 | return true; |
J-Alves | 180a65a | 2024-10-08 17:12:08 +0100 | [diff] [blame] | 91 | case VCPU_STATE_RUNNING: |
| 92 | case VCPU_STATE_BLOCKED: |
| 93 | case VCPU_STATE_PREEMPTED: |
| 94 | /* |
| 95 | * Let the normal secure interrupt handling handle the |
| 96 | * interrupt as usual. |
| 97 | */ |
| 98 | return false; |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 99 | default: |
J-Alves | 180a65a | 2024-10-08 17:12:08 +0100 | [diff] [blame] | 100 | dlog_error("Unexpected state: %u handling an IPI for [%x %u]", |
| 101 | target_vcpu->state, target_vcpu->vm->id, |
| 102 | vcpu_index(target_vcpu)); |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | } |