blob: 3ea57aab289ffda06da4f56feb8156d2d42b7613 [file] [log] [blame]
Edison Ai1c266ae2019-03-20 11:21:21 +08001/*
Tamas Band28286e2020-11-27 12:58:39 +00002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Edison Ai1c266ae2019-03-20 11:21:21 +08003 *
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>
Kevin Peng0979b0e2021-06-15 10:54:53 +080012#include "cmsis.h"
Tamas Band28286e2020-11-27 12:58:39 +000013#include "fih.h"
Edison Ai1c266ae2019-03-20 11:21:21 +080014#include "tfm_secure_api.h"
David Hu520dcd02019-11-18 16:04:36 +080015#ifdef TFM_MULTI_CORE_TOPOLOGY
16#include "tfm_multi_core.h"
17#endif
Mate Toth-Palb9c33552019-07-10 16:13:20 +020018#include "tfm_plat_defs.h"
Edison Ai1c266ae2019-03-20 11:21:21 +080019
Kevin Peng0979b0e2021-06-15 10:54:53 +080020/*
21 * Quantized default IRQ priority, the value is:
22 * (Number of configurable priority) / 4: (1UL << __NVIC_PRIO_BITS) / 4
23 */
24#define DEFAULT_IRQ_PRIORITY (1UL << (__NVIC_PRIO_BITS - 2))
25
Edison Ai1c266ae2019-03-20 11:21:21 +080026/**
27 * \brief Holds peripheral specific data fields required to manage the
28 * peripherals isolation
29 *
30 * This structure has to be defined in the platform directory, and may have
31 * different definition for each platform. The structure should contain fields
32 * that describe the peripheral for the functions that are prototyped in this
33 * file and are responsible for configuring the isolation of the peripherals.
34 *
35 * Pointers to structures of this type are managed by the SPM, and passed to the
36 * necessary function on isolation request. The pointers are also defined by the
37 * platform in the header file tfm_peripherals_def.h. For details on this, see
38 * the documentation of that file.
39 */
Ken Liu172f1e32021-02-05 16:31:03 +080040struct platform_data_t;
Edison Ai1c266ae2019-03-20 11:21:21 +080041
Mate Toth-Pal4341de02018-10-02 12:55:47 +020042enum irq_target_state_t {
43 TFM_IRQ_TARGET_STATE_SECURE,
44 TFM_IRQ_TARGET_STATE_NON_SECURE,
45};
46
Tamas Band28286e2020-11-27 12:58:39 +000047#ifdef TFM_FIH_PROFILE_ON
48#ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT
49/**
50 * \brief This function initialises the HW used for isolation, and sets the
51 * default configuration for them.
52 * This function is called during TF-M core early startup, after DB init
53 *
54 * \return Returns values as specified by FIH specific platform error code.
55 */
56fih_int tfm_spm_hal_setup_isolation_hw(void);
57#endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
58
59/**
60 * \brief Configure peripherals for a partition based on the platform data and
Mingyang Sun61f8fbc2021-06-04 17:49:56 +080061 * partition privilege
Tamas Band28286e2020-11-27 12:58:39 +000062 *
63 * This function is called during partition initialisation (before calling the
64 * init function for the partition)
65 *
Mingyang Sun61f8fbc2021-06-04 17:49:56 +080066 * \param[in] privileged Whether the partition is privileged.
Tamas Band28286e2020-11-27 12:58:39 +000067 * \param[in] platform_data The platform fields of the partition DB record to
68 * be used for configuration.
69 *
70 * \return Returns values as specified by FIH specific platform error code
71 */
72fih_int tfm_spm_hal_configure_default_isolation(
Mingyang Sun61f8fbc2021-06-04 17:49:56 +080073 bool privileged,
Tamas Band28286e2020-11-27 12:58:39 +000074 const struct platform_data_t *platform_data);
75/**
76 * \brief Configures the system debug properties.
77 * The default configuration of this function should disable secure debug
78 * when either DAUTH_NONE or DAUTH_NS_ONLY define is set. It is up to the
79 * platform owner to decide if secure debug can be turned on in their
80 * system, if DAUTH_FULL define is present.
81 * The DAUTH_CHIP_DEFAULT define should not be considered a safe default
82 * option unless explicitly noted by the chip vendor.
83 * The implementation has to expect that one of those defines is going to
84 * be set. Otherwise, a compile error needs to be triggered.
85 *
86 * \return Returns values as specified by FIH specific platform error code
87 */
88fih_int tfm_spm_hal_init_debug(void);
89
90/**
91 * \brief This function verifies the settings of HW used for memory isolation,
92 * to make sure that important settings was not skipped due to fault
93 * injection attacks.
94 *
95 * This function is called during TF-M core late startup, before passing
96 * execution to non-secure code.
97 *
98 * \return Returns values as specified by FIH specific platform error code
99 */
100fih_int tfm_spm_hal_verify_isolation_hw(void);
101#else /* TFM_FIH_PROFILE_ON */
Edison Ai1dfd7b12020-02-23 14:16:08 +0800102#ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT
Edison Ai1c266ae2019-03-20 11:21:21 +0800103/**
104 * \brief This function initialises the HW used for isolation, and sets the
105 * default configuration for them.
106 * This function is called during TF-M core early startup, after DB init
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200107 *
108 * \return Returns values as specified by the \ref tfm_plat_err_t
Edison Ai1c266ae2019-03-20 11:21:21 +0800109 */
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200110enum tfm_plat_err_t tfm_spm_hal_setup_isolation_hw(void);
Edison Ai1dfd7b12020-02-23 14:16:08 +0800111#endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
Edison Ai1c266ae2019-03-20 11:21:21 +0800112
113/**
Mate Toth-Pal5e6d0342019-11-22 11:43:20 +0100114 * \brief Configure peripherals for a partition based on the platform data and
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800115 * partition privilege
Edison Ai1c266ae2019-03-20 11:21:21 +0800116 *
117 * This function is called during partition initialisation (before calling the
118 * init function for the partition)
119 *
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800120 * \param[in] privileged Whether the partition is privileged.
Edison Ai1c266ae2019-03-20 11:21:21 +0800121 * \param[in] platform_data The platform fields of the partition DB record to
Edison Ai6be3df12020-02-14 22:14:33 +0800122 * be used for configuration.
123 *
124 * \return Returns values as specified by the \ref tfm_plat_err_t
Edison Ai1c266ae2019-03-20 11:21:21 +0800125 */
Edison Ai6be3df12020-02-14 22:14:33 +0800126enum tfm_plat_err_t tfm_spm_hal_configure_default_isolation(
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800127 bool priviledged,
Ken Liu172f1e32021-02-05 16:31:03 +0800128 const struct platform_data_t *platform_data);
Edison Ai1c266ae2019-03-20 11:21:21 +0800129/**
130 * \brief Configures the system debug properties.
131 * The default configuration of this function should disable secure debug
132 * when either DAUTH_NONE or DAUTH_NS_ONLY define is set. It is up to the
133 * platform owner to decide if secure debug can be turned on in their
134 * system, if DAUTH_FULL define is present.
135 * The DAUTH_CHIP_DEFAULT define should not be considered a safe default
136 * option unless explicitly noted by the chip vendor.
137 * The implementation has to expect that one of those defines is going to
138 * be set. Otherwise, a compile error needs to be triggered.
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200139 *
140 * \return Returns values as specified by the \ref tfm_plat_err_t
Edison Ai1c266ae2019-03-20 11:21:21 +0800141 */
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200142enum tfm_plat_err_t tfm_spm_hal_init_debug(void);
Tamas Band28286e2020-11-27 12:58:39 +0000143#endif /* TFM_FIH_PROFILE_ON */
Edison Ai1c266ae2019-03-20 11:21:21 +0800144
145/**
Mate Toth-Pal3e2ebd02019-05-07 14:22:16 +0200146 * \brief Enables the fault handlers and sets priorities.
147 *
148 * Secure fault (if present) must have the highest possible priority
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200149 *
150 * \return Returns values as specified by the \ref tfm_plat_err_t
Edison Ai1c266ae2019-03-20 11:21:21 +0800151 */
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200152enum tfm_plat_err_t tfm_spm_hal_enable_fault_handlers(void);
Edison Ai1c266ae2019-03-20 11:21:21 +0800153
154/**
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +0100155 * \brief Configures the system reset request properties
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200156 *
157 * \return Returns values as specified by the \ref tfm_plat_err_t
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +0100158 */
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200159enum tfm_plat_err_t tfm_spm_hal_system_reset_cfg(void);
Edison Ai1c266ae2019-03-20 11:21:21 +0800160
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +0100161/**
Edison Ai1c266ae2019-03-20 11:21:21 +0800162 * \brief Configures all external interrupts to target the
163 * NS state, apart for the ones associated to secure
164 * peripherals (plus MPC and PPC)
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200165 *
166 * \return Returns values as specified by the \ref tfm_plat_err_t
Edison Ai1c266ae2019-03-20 11:21:21 +0800167 */
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200168enum tfm_plat_err_t tfm_spm_hal_nvic_interrupt_target_state_cfg(void);
Edison Ai1c266ae2019-03-20 11:21:21 +0800169
170/**
171 * \brief This function enable the interrupts associated
172 * to the secure peripherals (plus the isolation boundary violation
173 * interrupts)
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200174 *
175 * \return Returns values as specified by the \ref tfm_plat_err_t
Edison Ai1c266ae2019-03-20 11:21:21 +0800176 */
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200177enum tfm_plat_err_t tfm_spm_hal_nvic_interrupt_enable(void);
Edison Ai1c266ae2019-03-20 11:21:21 +0800178
179/**
180 * \brief Get the VTOR value of non-secure image
181 *
182 * \return Returns the address where the vector table of the non-secure image
183 * is located
184 */
185uint32_t tfm_spm_hal_get_ns_VTOR(void);
186
187/**
188 * \brief Get the initial address of non-secure image main stack
189 *
190 * \return Returns the initial non-secure MSP
191 */
192uint32_t tfm_spm_hal_get_ns_MSP(void);
193
194/**
195 * \brief Get the entry point of the non-secure image
196 *
197 * \return Returns the address of the non-secure image entry point
198 */
199uint32_t tfm_spm_hal_get_ns_entry_point(void);
200
Mate Toth-Pal94925722019-06-27 15:10:48 +0200201/**
202 * \brief Set the priority of a secure IRQ
203 *
204 * \param[in] irq_line The IRQ to set the priority for. Might be less than 0
Mate Toth-Pal94925722019-06-27 15:10:48 +0200205 *
206 * \details This function sets the priority for the IRQ passed in the parameter.
Mate Toth-Palb9c33552019-07-10 16:13:20 +0200207 *
208 * \return Returns values as specified by the \ref tfm_plat_err_t
Kevin Peng0979b0e2021-06-15 10:54:53 +0800209 *
210 * \note The priority value must be less than the value of PendSV (0x80) and
211 * greater than SVC (0x0).
212 * Platforms are responsible for the priority values assignment to each
213 * IRQ based on their platforms and use cases.
Mate Toth-Pal94925722019-06-27 15:10:48 +0200214 */
Kevin Peng0979b0e2021-06-15 10:54:53 +0800215enum tfm_plat_err_t tfm_spm_hal_set_secure_irq_priority(IRQn_Type irq_line);
Edison Ai1c266ae2019-03-20 11:21:21 +0800216
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200217/**
218 * \brief Clears a pending IRQ
219 *
220 * \param[in] irq_line The IRQ to clear pending for.
221 */
TTornblomfaf74f52020-03-04 17:56:27 +0100222void tfm_spm_hal_clear_pending_irq(IRQn_Type irq_line);
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200223
224/**
225 * \brief Enables an IRQ
226 *
227 * \param[in] irq_line The IRQ to be enabled.
228 */
TTornblomfaf74f52020-03-04 17:56:27 +0100229void tfm_spm_hal_enable_irq(IRQn_Type irq_line);
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200230
231/**
232 * \brief Disables an IRQ
233 *
234 * \param[in] irq_line The IRQ to be disabled
235 */
TTornblomfaf74f52020-03-04 17:56:27 +0100236void tfm_spm_hal_disable_irq(IRQn_Type irq_line);
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200237
238/**
239 * \brief Set the target state of an IRQ
240 *
241 * \param[in] irq_line The IRQ to set the priority for.
242 * \param[in] target_state Target state to ret for the IRQ.
243 *
244 * \return TFM_IRQ_TARGET_STATE_SECURE if interrupt is assigned
245 * to Secure
246 * TFM_IRQ_TARGET_STATE_NON_SECURE if interrupt is
247 * assigned to Non-Secure
248 */
249enum irq_target_state_t tfm_spm_hal_set_irq_target_state(
TTornblomfaf74f52020-03-04 17:56:27 +0100250 IRQn_Type irq_line,
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200251 enum irq_target_state_t target_state);
252
David Hu520dcd02019-11-18 16:04:36 +0800253#ifdef TFM_MULTI_CORE_TOPOLOGY
254/**
255 * \brief Performs the necessary actions to start the non-secure CPU running
256 * the code at the specified address.
257 *
258 * \param[in] start_addr The entry point address of non-secure code.
259 */
260void tfm_spm_hal_boot_ns_cpu(uintptr_t start_addr);
261
262/**
263 * \brief Called on the secure CPU.
264 * Flags that the secure CPU has completed its initialization
265 * Waits, if necessary, for the non-secure CPU to flag that
266 * it has completed its initialisation
267 */
268void tfm_spm_hal_wait_for_ns_cpu_ready(void);
269
270/**
271 * \brief Retrieve the current active security configuration information and
272 * fills the \ref security_attr_info_t.
273 *
274 * \param[in] p Base address of target memory region
275 * \param[in] s Size of target memory region
276 * \param[out] p_attr Address of \ref security_attr_info_t to be filled
277 *
278 * \return void
279 */
280void tfm_spm_hal_get_mem_security_attr(const void *p, size_t s,
281 struct security_attr_info_t *p_attr);
282
283/**
284 * \brief Retrieve the secure memory protection configuration information and
285 * fills the \ref mem_attr_info_t.
286 *
287 * \param[in] p Base address of target memory region
288 * \param[in] s Size of target memory region
289 * \param[out] p_attr Address of \ref mem_attr_info_t to be filled
290 *
291 * \return void
292 */
293void tfm_spm_hal_get_secure_access_attr(const void *p, size_t s,
294 struct mem_attr_info_t *p_attr);
295
296/**
297 * \brief Retrieve the non-secure memory protection configuration information
298 * and fills the \ref mem_attr_info_t.
299 *
300 * \param[in] p Base address of target memory region
301 * \param[in] s Size of target memory region
302 * \param[out] p_attr Address of \ref mem_attr_info_t to be filled
303 *
304 * \return void
305 */
306void tfm_spm_hal_get_ns_access_attr(const void *p, size_t s,
307 struct mem_attr_info_t *p_attr);
308
309#endif /*TFM_MULTI_CORE_TOPOLOGY*/
310
Ioannis Glaropoulos130248c2020-07-30 15:09:01 +0200311#if !defined(__SAUREGION_PRESENT) || (__SAUREGION_PRESENT == 0)
312/**
313 * \brief Platform-specific check whether the current partition has access to a memory range
314 *
315 * The function checks whether the current partition has access to a memory range,
316 * taking into consideration the implementation-defined attribution unit that is
317 * present on a particular platform.
318 *
319 * \param[in] p The start address of the range to check
320 * \param[in] s The size of the range to check
321 * \param[in] flags The flags to pass to the cmse_check_address_range func
322 *
323 * \return True if the access is granted, false otherwise.
324 */
Ken Liuce58bfc2021-05-12 17:54:48 +0800325bool tfm_spm_hal_has_access_to_region(const void *p, size_t s, int flags);
Ioannis Glaropoulos130248c2020-07-30 15:09:01 +0200326#endif /* !defined(__SAUREGION_PRESENT) || (__SAUREGION_PRESENT == 0) */
327
Edison Ai1c266ae2019-03-20 11:21:21 +0800328#endif /* __TFM_SPM_HAL_H__ */