Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 1 | /* |
Tamas Ban | d28286e | 2020-11-27 12:58:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, Arm Limited. All rights reserved. |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef __TFM_SPM_HAL_H__ |
| 9 | #define __TFM_SPM_HAL_H__ |
| 10 | |
| 11 | #include <stdint.h> |
Tamas Ban | d28286e | 2020-11-27 12:58:39 +0000 | [diff] [blame] | 12 | #include "fih.h" |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 13 | #include "tfm_secure_api.h" |
David Hu | 520dcd0 | 2019-11-18 16:04:36 +0800 | [diff] [blame] | 14 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 15 | #include "tfm_multi_core.h" |
| 16 | #endif |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 17 | #include "tfm_plat_defs.h" |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * \brief Holds peripheral specific data fields required to manage the |
| 21 | * peripherals isolation |
| 22 | * |
| 23 | * This structure has to be defined in the platform directory, and may have |
| 24 | * different definition for each platform. The structure should contain fields |
| 25 | * that describe the peripheral for the functions that are prototyped in this |
| 26 | * file and are responsible for configuring the isolation of the peripherals. |
| 27 | * |
| 28 | * Pointers to structures of this type are managed by the SPM, and passed to the |
| 29 | * necessary function on isolation request. The pointers are also defined by the |
| 30 | * platform in the header file tfm_peripherals_def.h. For details on this, see |
| 31 | * the documentation of that file. |
| 32 | */ |
Ken Liu | 172f1e3 | 2021-02-05 16:31:03 +0800 | [diff] [blame] | 33 | struct platform_data_t; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 34 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 35 | enum irq_target_state_t { |
| 36 | TFM_IRQ_TARGET_STATE_SECURE, |
| 37 | TFM_IRQ_TARGET_STATE_NON_SECURE, |
| 38 | }; |
| 39 | |
Tamas Ban | d28286e | 2020-11-27 12:58:39 +0000 | [diff] [blame] | 40 | #ifdef TFM_FIH_PROFILE_ON |
| 41 | #ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT |
| 42 | /** |
| 43 | * \brief This function initialises the HW used for isolation, and sets the |
| 44 | * default configuration for them. |
| 45 | * This function is called during TF-M core early startup, after DB init |
| 46 | * |
| 47 | * \return Returns values as specified by FIH specific platform error code. |
| 48 | */ |
| 49 | fih_int tfm_spm_hal_setup_isolation_hw(void); |
| 50 | #endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */ |
| 51 | |
| 52 | /** |
| 53 | * \brief Configure peripherals for a partition based on the platform data and |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 54 | * partition privilege |
Tamas Ban | d28286e | 2020-11-27 12:58:39 +0000 | [diff] [blame] | 55 | * |
| 56 | * This function is called during partition initialisation (before calling the |
| 57 | * init function for the partition) |
| 58 | * |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 59 | * \param[in] privileged Whether the partition is privileged. |
Tamas Ban | d28286e | 2020-11-27 12:58:39 +0000 | [diff] [blame] | 60 | * \param[in] platform_data The platform fields of the partition DB record to |
| 61 | * be used for configuration. |
| 62 | * |
| 63 | * \return Returns values as specified by FIH specific platform error code |
| 64 | */ |
| 65 | fih_int tfm_spm_hal_configure_default_isolation( |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 66 | bool privileged, |
Tamas Ban | d28286e | 2020-11-27 12:58:39 +0000 | [diff] [blame] | 67 | const struct platform_data_t *platform_data); |
| 68 | /** |
| 69 | * \brief Configures the system debug properties. |
| 70 | * The default configuration of this function should disable secure debug |
| 71 | * when either DAUTH_NONE or DAUTH_NS_ONLY define is set. It is up to the |
| 72 | * platform owner to decide if secure debug can be turned on in their |
| 73 | * system, if DAUTH_FULL define is present. |
| 74 | * The DAUTH_CHIP_DEFAULT define should not be considered a safe default |
| 75 | * option unless explicitly noted by the chip vendor. |
| 76 | * The implementation has to expect that one of those defines is going to |
| 77 | * be set. Otherwise, a compile error needs to be triggered. |
| 78 | * |
| 79 | * \return Returns values as specified by FIH specific platform error code |
| 80 | */ |
| 81 | fih_int tfm_spm_hal_init_debug(void); |
| 82 | |
| 83 | /** |
| 84 | * \brief This function verifies the settings of HW used for memory isolation, |
| 85 | * to make sure that important settings was not skipped due to fault |
| 86 | * injection attacks. |
| 87 | * |
| 88 | * This function is called during TF-M core late startup, before passing |
| 89 | * execution to non-secure code. |
| 90 | * |
| 91 | * \return Returns values as specified by FIH specific platform error code |
| 92 | */ |
| 93 | fih_int tfm_spm_hal_verify_isolation_hw(void); |
| 94 | #else /* TFM_FIH_PROFILE_ON */ |
Edison Ai | 1dfd7b1 | 2020-02-23 14:16:08 +0800 | [diff] [blame] | 95 | #ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 96 | /** |
| 97 | * \brief This function initialises the HW used for isolation, and sets the |
| 98 | * default configuration for them. |
| 99 | * This function is called during TF-M core early startup, after DB init |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 100 | * |
| 101 | * \return Returns values as specified by the \ref tfm_plat_err_t |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 102 | */ |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 103 | enum tfm_plat_err_t tfm_spm_hal_setup_isolation_hw(void); |
Edison Ai | 1dfd7b1 | 2020-02-23 14:16:08 +0800 | [diff] [blame] | 104 | #endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */ |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 105 | |
| 106 | /** |
Mate Toth-Pal | 5e6d034 | 2019-11-22 11:43:20 +0100 | [diff] [blame] | 107 | * \brief Configure peripherals for a partition based on the platform data and |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 108 | * partition privilege |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 109 | * |
| 110 | * This function is called during partition initialisation (before calling the |
| 111 | * init function for the partition) |
| 112 | * |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 113 | * \param[in] privileged Whether the partition is privileged. |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 114 | * \param[in] platform_data The platform fields of the partition DB record to |
Edison Ai | 6be3df1 | 2020-02-14 22:14:33 +0800 | [diff] [blame] | 115 | * be used for configuration. |
| 116 | * |
| 117 | * \return Returns values as specified by the \ref tfm_plat_err_t |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 118 | */ |
Edison Ai | 6be3df1 | 2020-02-14 22:14:33 +0800 | [diff] [blame] | 119 | enum tfm_plat_err_t tfm_spm_hal_configure_default_isolation( |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 120 | bool priviledged, |
Ken Liu | 172f1e3 | 2021-02-05 16:31:03 +0800 | [diff] [blame] | 121 | const struct platform_data_t *platform_data); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 122 | /** |
| 123 | * \brief Configures the system debug properties. |
| 124 | * The default configuration of this function should disable secure debug |
| 125 | * when either DAUTH_NONE or DAUTH_NS_ONLY define is set. It is up to the |
| 126 | * platform owner to decide if secure debug can be turned on in their |
| 127 | * system, if DAUTH_FULL define is present. |
| 128 | * The DAUTH_CHIP_DEFAULT define should not be considered a safe default |
| 129 | * option unless explicitly noted by the chip vendor. |
| 130 | * The implementation has to expect that one of those defines is going to |
| 131 | * be set. Otherwise, a compile error needs to be triggered. |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 132 | * |
| 133 | * \return Returns values as specified by the \ref tfm_plat_err_t |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 134 | */ |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 135 | enum tfm_plat_err_t tfm_spm_hal_init_debug(void); |
Tamas Ban | d28286e | 2020-11-27 12:58:39 +0000 | [diff] [blame] | 136 | #endif /* TFM_FIH_PROFILE_ON */ |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 137 | |
| 138 | /** |
Mate Toth-Pal | 3e2ebd0 | 2019-05-07 14:22:16 +0200 | [diff] [blame] | 139 | * \brief Enables the fault handlers and sets priorities. |
| 140 | * |
| 141 | * Secure fault (if present) must have the highest possible priority |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 142 | * |
| 143 | * \return Returns values as specified by the \ref tfm_plat_err_t |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 144 | */ |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 145 | enum tfm_plat_err_t tfm_spm_hal_enable_fault_handlers(void); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 146 | |
| 147 | /** |
Marc Moreno Berengue | 8e0fa7a | 2018-10-04 18:25:13 +0100 | [diff] [blame] | 148 | * \brief Configures the system reset request properties |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 149 | * |
| 150 | * \return Returns values as specified by the \ref tfm_plat_err_t |
Marc Moreno Berengue | 8e0fa7a | 2018-10-04 18:25:13 +0100 | [diff] [blame] | 151 | */ |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 152 | enum tfm_plat_err_t tfm_spm_hal_system_reset_cfg(void); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 153 | |
Marc Moreno Berengue | 8e0fa7a | 2018-10-04 18:25:13 +0100 | [diff] [blame] | 154 | /** |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 155 | * \brief Configures all external interrupts to target the |
| 156 | * NS state, apart for the ones associated to secure |
| 157 | * peripherals (plus MPC and PPC) |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 158 | * |
| 159 | * \return Returns values as specified by the \ref tfm_plat_err_t |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 160 | */ |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 161 | enum tfm_plat_err_t tfm_spm_hal_nvic_interrupt_target_state_cfg(void); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * \brief This function enable the interrupts associated |
| 165 | * to the secure peripherals (plus the isolation boundary violation |
| 166 | * interrupts) |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 167 | * |
| 168 | * \return Returns values as specified by the \ref tfm_plat_err_t |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 169 | */ |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 170 | enum tfm_plat_err_t tfm_spm_hal_nvic_interrupt_enable(void); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * \brief Get the VTOR value of non-secure image |
| 174 | * |
| 175 | * \return Returns the address where the vector table of the non-secure image |
| 176 | * is located |
| 177 | */ |
| 178 | uint32_t tfm_spm_hal_get_ns_VTOR(void); |
| 179 | |
| 180 | /** |
| 181 | * \brief Get the initial address of non-secure image main stack |
| 182 | * |
| 183 | * \return Returns the initial non-secure MSP |
| 184 | */ |
| 185 | uint32_t tfm_spm_hal_get_ns_MSP(void); |
| 186 | |
| 187 | /** |
| 188 | * \brief Get the entry point of the non-secure image |
| 189 | * |
| 190 | * \return Returns the address of the non-secure image entry point |
| 191 | */ |
| 192 | uint32_t tfm_spm_hal_get_ns_entry_point(void); |
| 193 | |
Mate Toth-Pal | 9492572 | 2019-06-27 15:10:48 +0200 | [diff] [blame] | 194 | /** |
| 195 | * \brief Set the priority of a secure IRQ |
| 196 | * |
| 197 | * \param[in] irq_line The IRQ to set the priority for. Might be less than 0 |
| 198 | * \param[in] priority The priority to set. [0..255] |
| 199 | * |
| 200 | * \details This function sets the priority for the IRQ passed in the parameter. |
| 201 | * The precision of the priority value might be adjusted to match the |
| 202 | * available priority bits in the underlying target platform. |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 203 | * |
| 204 | * \return Returns values as specified by the \ref tfm_plat_err_t |
Mate Toth-Pal | 9492572 | 2019-06-27 15:10:48 +0200 | [diff] [blame] | 205 | */ |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 206 | enum tfm_plat_err_t tfm_spm_hal_set_secure_irq_priority(IRQn_Type irq_line, |
Mate Toth-Pal | b9c3355 | 2019-07-10 16:13:20 +0200 | [diff] [blame] | 207 | uint32_t priority); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 208 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 209 | /** |
| 210 | * \brief Clears a pending IRQ |
| 211 | * |
| 212 | * \param[in] irq_line The IRQ to clear pending for. |
| 213 | */ |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 214 | void tfm_spm_hal_clear_pending_irq(IRQn_Type irq_line); |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * \brief Enables an IRQ |
| 218 | * |
| 219 | * \param[in] irq_line The IRQ to be enabled. |
| 220 | */ |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 221 | void tfm_spm_hal_enable_irq(IRQn_Type irq_line); |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 222 | |
| 223 | /** |
| 224 | * \brief Disables an IRQ |
| 225 | * |
| 226 | * \param[in] irq_line The IRQ to be disabled |
| 227 | */ |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 228 | void tfm_spm_hal_disable_irq(IRQn_Type irq_line); |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * \brief Set the target state of an IRQ |
| 232 | * |
| 233 | * \param[in] irq_line The IRQ to set the priority for. |
| 234 | * \param[in] target_state Target state to ret for the IRQ. |
| 235 | * |
| 236 | * \return TFM_IRQ_TARGET_STATE_SECURE if interrupt is assigned |
| 237 | * to Secure |
| 238 | * TFM_IRQ_TARGET_STATE_NON_SECURE if interrupt is |
| 239 | * assigned to Non-Secure |
| 240 | */ |
| 241 | enum irq_target_state_t tfm_spm_hal_set_irq_target_state( |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 242 | IRQn_Type irq_line, |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 243 | enum irq_target_state_t target_state); |
| 244 | |
David Hu | 520dcd0 | 2019-11-18 16:04:36 +0800 | [diff] [blame] | 245 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 246 | /** |
| 247 | * \brief Performs the necessary actions to start the non-secure CPU running |
| 248 | * the code at the specified address. |
| 249 | * |
| 250 | * \param[in] start_addr The entry point address of non-secure code. |
| 251 | */ |
| 252 | void tfm_spm_hal_boot_ns_cpu(uintptr_t start_addr); |
| 253 | |
| 254 | /** |
| 255 | * \brief Called on the secure CPU. |
| 256 | * Flags that the secure CPU has completed its initialization |
| 257 | * Waits, if necessary, for the non-secure CPU to flag that |
| 258 | * it has completed its initialisation |
| 259 | */ |
| 260 | void tfm_spm_hal_wait_for_ns_cpu_ready(void); |
| 261 | |
| 262 | /** |
| 263 | * \brief Retrieve the current active security configuration information and |
| 264 | * fills the \ref security_attr_info_t. |
| 265 | * |
| 266 | * \param[in] p Base address of target memory region |
| 267 | * \param[in] s Size of target memory region |
| 268 | * \param[out] p_attr Address of \ref security_attr_info_t to be filled |
| 269 | * |
| 270 | * \return void |
| 271 | */ |
| 272 | void tfm_spm_hal_get_mem_security_attr(const void *p, size_t s, |
| 273 | struct security_attr_info_t *p_attr); |
| 274 | |
| 275 | /** |
| 276 | * \brief Retrieve the secure memory protection configuration information and |
| 277 | * fills the \ref mem_attr_info_t. |
| 278 | * |
| 279 | * \param[in] p Base address of target memory region |
| 280 | * \param[in] s Size of target memory region |
| 281 | * \param[out] p_attr Address of \ref mem_attr_info_t to be filled |
| 282 | * |
| 283 | * \return void |
| 284 | */ |
| 285 | void tfm_spm_hal_get_secure_access_attr(const void *p, size_t s, |
| 286 | struct mem_attr_info_t *p_attr); |
| 287 | |
| 288 | /** |
| 289 | * \brief Retrieve the non-secure memory protection configuration information |
| 290 | * and fills the \ref mem_attr_info_t. |
| 291 | * |
| 292 | * \param[in] p Base address of target memory region |
| 293 | * \param[in] s Size of target memory region |
| 294 | * \param[out] p_attr Address of \ref mem_attr_info_t to be filled |
| 295 | * |
| 296 | * \return void |
| 297 | */ |
| 298 | void tfm_spm_hal_get_ns_access_attr(const void *p, size_t s, |
| 299 | struct mem_attr_info_t *p_attr); |
| 300 | |
| 301 | #endif /*TFM_MULTI_CORE_TOPOLOGY*/ |
| 302 | |
Ioannis Glaropoulos | 130248c | 2020-07-30 15:09:01 +0200 | [diff] [blame] | 303 | #if !defined(__SAUREGION_PRESENT) || (__SAUREGION_PRESENT == 0) |
| 304 | /** |
| 305 | * \brief Platform-specific check whether the current partition has access to a memory range |
| 306 | * |
| 307 | * The function checks whether the current partition has access to a memory range, |
| 308 | * taking into consideration the implementation-defined attribution unit that is |
| 309 | * present on a particular platform. |
| 310 | * |
| 311 | * \param[in] p The start address of the range to check |
| 312 | * \param[in] s The size of the range to check |
| 313 | * \param[in] flags The flags to pass to the cmse_check_address_range func |
| 314 | * |
| 315 | * \return True if the access is granted, false otherwise. |
| 316 | */ |
| 317 | bool tfm_spm_hal_has_access_to_region(const void *p, size_t s, |
| 318 | int flags); |
| 319 | #endif /* !defined(__SAUREGION_PRESENT) || (__SAUREGION_PRESENT == 0) */ |
| 320 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 321 | #endif /* __TFM_SPM_HAL_H__ */ |