Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 1 | /* |
Xinyu Zhang | a16228b | 2023-01-12 14:54:04 +0800 | [diff] [blame] | 2 | * Copyright (c) 2021-2023, Arm Limited. All rights reserved. |
Chris Brand | 8b58ebd | 2022-10-18 17:02:25 -0700 | [diff] [blame] | 3 | * Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon |
| 4 | * company) or an affiliate of Cypress Semiconductor Corporation. All rights |
| 5 | * reserved. |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 6 | * |
| 7 | * SPDX-License-Identifier: BSD-3-Clause |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include "interrupt.h" |
| 12 | |
| 13 | #include "bitops.h" |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 14 | #include "current.h" |
Kevin Peng | b42ed86 | 2022-08-08 14:44:02 +0800 | [diff] [blame] | 15 | #include "svc_num.h" |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 16 | #include "tfm_arch.h" |
| 17 | #include "tfm_hal_interrupt.h" |
| 18 | #include "tfm_hal_isolation.h" |
| 19 | #include "thread.h" |
| 20 | #include "utilities.h" |
| 21 | |
| 22 | #include "load/spm_load_api.h" |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame^] | 23 | #include "ffm/backend.h" |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 24 | |
Chendi Sun | 0f7d282 | 2022-10-28 12:24:12 +0800 | [diff] [blame] | 25 | extern uintptr_t spm_boundary; |
| 26 | |
Sherry Zhang | d6dbe51 | 2022-03-23 16:42:32 +0800 | [diff] [blame] | 27 | #if TFM_LVL != 1 |
| 28 | extern void tfm_flih_func_return(psa_flih_result_t result); |
| 29 | |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 30 | __attribute__((naked)) |
| 31 | static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt, |
| 32 | uintptr_t fn_flih, |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 33 | void *curr_component) |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 34 | { |
Sherry Zhang | 9714d96 | 2022-03-02 10:52:46 +0800 | [diff] [blame] | 35 | __ASM volatile("SVC "M2S(TFM_SVC_PREPARE_DEPRIV_FLIH)" \n" |
| 36 | "BX LR \n" |
| 37 | ); |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 38 | } |
| 39 | |
Kevin Peng | f7a20d8 | 2021-12-13 14:38:37 +0800 | [diff] [blame] | 40 | uint32_t tfm_flih_prepare_depriv_flih(struct partition_t *p_owner_sp, |
| 41 | uintptr_t flih_func) |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 42 | { |
| 43 | struct partition_t *p_curr_sp; |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 44 | uintptr_t sp_base, sp_limit, curr_stack, ctx_stack; |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 45 | struct context_ctrl_t flih_ctx_ctrl; |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 46 | fih_int fih_rc = FIH_FAILURE; |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 47 | |
| 48 | /* Come too early before runtime setup, should not happen. */ |
| 49 | if (!CURRENT_THREAD) { |
| 50 | tfm_core_panic(); |
| 51 | } |
| 52 | |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 53 | p_curr_sp = GET_CURRENT_COMPONENT(); |
| 54 | sp_base = LOAD_ALLOCED_STACK_ADDR(p_owner_sp->p_ldinf) |
| 55 | + p_owner_sp->p_ldinf->stack_size; |
| 56 | sp_limit = LOAD_ALLOCED_STACK_ADDR(p_owner_sp->p_ldinf); |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 57 | |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 58 | curr_stack = (uintptr_t)__get_PSP(); |
| 59 | if (curr_stack < sp_base && curr_stack > sp_limit) { |
| 60 | /* The IRQ Partition's stack is being used */ |
| 61 | ctx_stack = curr_stack; |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 62 | } else { |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 63 | ctx_stack = |
| 64 | ((struct context_ctrl_t *)p_owner_sp->thrd.p_context_ctrl)->sp; |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 65 | } |
| 66 | |
Chendi Sun | 0f7d282 | 2022-10-28 12:24:12 +0800 | [diff] [blame] | 67 | if (tfm_hal_boundary_need_switch(p_curr_sp->boundary, |
| 68 | p_owner_sp->boundary)) { |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 69 | FIH_CALL(tfm_hal_activate_boundary, fih_rc, |
| 70 | p_owner_sp->p_ldinf, p_owner_sp->boundary); |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
| 74 | * The CURRENT_COMPONENT has been stored on MSP by the SVC call, safe to |
| 75 | * update it. |
| 76 | */ |
| 77 | SET_CURRENT_COMPONENT(p_owner_sp); |
| 78 | |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 79 | flih_ctx_ctrl.sp_limit = sp_limit; |
| 80 | flih_ctx_ctrl.sp = ctx_stack; |
| 81 | |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 82 | tfm_arch_init_context(&flih_ctx_ctrl, |
Kevin Peng | f7a20d8 | 2021-12-13 14:38:37 +0800 | [diff] [blame] | 83 | flih_func, NULL, |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 84 | (uintptr_t)tfm_flih_func_return); |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 85 | |
| 86 | (void)tfm_arch_refresh_hardware_context(&flih_ctx_ctrl); |
| 87 | |
| 88 | return flih_ctx_ctrl.exc_ret; |
| 89 | } |
| 90 | |
| 91 | /* Go back to ISR from FLIH functions */ |
Kevin Peng | f7a20d8 | 2021-12-13 14:38:37 +0800 | [diff] [blame] | 92 | uint32_t tfm_flih_return_to_isr(psa_flih_result_t result, |
| 93 | struct context_flih_ret_t *p_ctx_flih_ret) |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 94 | { |
| 95 | struct partition_t *p_prev_sp, *p_owner_sp; |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 96 | fih_int fih_rc = FIH_FAILURE; |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 97 | |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 98 | p_prev_sp = (struct partition_t *)(p_ctx_flih_ret->state_ctx.r2); |
| 99 | p_owner_sp = GET_CURRENT_COMPONENT(); |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 100 | |
Chendi Sun | 0f7d282 | 2022-10-28 12:24:12 +0800 | [diff] [blame] | 101 | if (tfm_hal_boundary_need_switch(p_owner_sp->boundary, |
| 102 | p_prev_sp->boundary)) { |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 103 | FIH_CALL(tfm_hal_activate_boundary, fih_rc, |
| 104 | p_prev_sp->p_ldinf, p_prev_sp->boundary); |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 107 | /* Restore current component */ |
| 108 | SET_CURRENT_COMPONENT(p_prev_sp); |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 109 | |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 110 | tfm_arch_set_psplim(p_ctx_flih_ret->psplim); |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 111 | __set_PSP(p_ctx_flih_ret->psp); |
| 112 | |
| 113 | /* Set FLIH result to the ISR */ |
| 114 | p_ctx_flih_ret->state_ctx.r0 = (uint32_t)result; |
| 115 | |
Xinyu Zhang | a16228b | 2023-01-12 14:54:04 +0800 | [diff] [blame] | 116 | return EXC_RETURN_HANDLER; |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 117 | } |
Sherry Zhang | d6dbe51 | 2022-03-23 16:42:32 +0800 | [diff] [blame] | 118 | #endif |
| 119 | |
Chris Brand | 10a2acb | 2022-10-18 17:12:27 -0700 | [diff] [blame] | 120 | const struct irq_load_info_t *get_irq_info_for_signal( |
Sherry Zhang | d6dbe51 | 2022-03-23 16:42:32 +0800 | [diff] [blame] | 121 | const struct partition_load_info_t *p_ldinf, |
| 122 | psa_signal_t signal) |
| 123 | { |
| 124 | size_t i; |
Chris Brand | 10a2acb | 2022-10-18 17:12:27 -0700 | [diff] [blame] | 125 | const struct irq_load_info_t *irq_info; |
Sherry Zhang | d6dbe51 | 2022-03-23 16:42:32 +0800 | [diff] [blame] | 126 | |
| 127 | if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) { |
| 128 | return NULL; |
| 129 | } |
| 130 | |
Chris Brand | 8b58ebd | 2022-10-18 17:02:25 -0700 | [diff] [blame] | 131 | irq_info = LOAD_INFO_IRQ(p_ldinf); |
Sherry Zhang | d6dbe51 | 2022-03-23 16:42:32 +0800 | [diff] [blame] | 132 | for (i = 0; i < p_ldinf->nirqs; i++) { |
| 133 | if (irq_info[i].signal == signal) { |
| 134 | return &irq_info[i]; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return NULL; |
| 139 | } |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 140 | |
Chris Brand | 10a2acb | 2022-10-18 17:12:27 -0700 | [diff] [blame] | 141 | void spm_handle_interrupt(void *p_pt, const struct irq_load_info_t *p_ildi) |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 142 | { |
| 143 | psa_flih_result_t flih_result; |
| 144 | struct partition_t *p_part; |
| 145 | |
| 146 | if (!p_pt || !p_ildi) { |
| 147 | tfm_core_panic(); |
| 148 | } |
| 149 | |
| 150 | p_part = (struct partition_t *)p_pt; |
| 151 | |
| 152 | if (p_ildi->pid != p_part->p_ldinf->pid) { |
| 153 | tfm_core_panic(); |
| 154 | } |
| 155 | |
| 156 | if (p_ildi->flih_func == NULL) { |
| 157 | /* SLIH Model Handling */ |
| 158 | tfm_hal_irq_disable(p_ildi->source); |
| 159 | flih_result = PSA_FLIH_SIGNAL; |
| 160 | } else { |
| 161 | /* FLIH Model Handling */ |
Sherry Zhang | d6dbe51 | 2022-03-23 16:42:32 +0800 | [diff] [blame] | 162 | #if TFM_LVL == 1 |
| 163 | flih_result = p_ildi->flih_func(); |
| 164 | #else |
Roman Mazurak | 830b06e | 2022-11-21 20:06:16 +0200 | [diff] [blame] | 165 | if (!tfm_hal_boundary_need_switch(spm_boundary, |
Chendi Sun | 0f7d282 | 2022-10-28 12:24:12 +0800 | [diff] [blame] | 166 | p_part->boundary)) { |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 167 | flih_result = p_ildi->flih_func(); |
| 168 | } else { |
| 169 | flih_result = tfm_flih_deprivileged_handling( |
| 170 | p_part, |
| 171 | (uintptr_t)p_ildi->flih_func, |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 172 | GET_CURRENT_COMPONENT()); |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 173 | } |
Sherry Zhang | d6dbe51 | 2022-03-23 16:42:32 +0800 | [diff] [blame] | 174 | #endif |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (flih_result == PSA_FLIH_SIGNAL) { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame^] | 178 | backend_assert_signal(p_pt, p_ildi->signal); |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 179 | /* In SFN backend, there is only one thread, no thread switch. */ |
| 180 | #if CONFIG_TFM_SPM_BACKEND_SFN != 1 |
Kevin Peng | 8a57969 | 2021-12-15 13:44:42 +0800 | [diff] [blame] | 181 | if (THRD_EXPECTING_SCHEDULE()) { |
| 182 | tfm_arch_trigger_pendsv(); |
| 183 | } |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 184 | #endif |
Kevin Peng | 3f67b2e | 2021-10-18 17:47:27 +0800 | [diff] [blame] | 185 | } |
| 186 | } |