Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 1 | /* |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2022, Arm Limited. All rights reserved. |
Chris Brand | 5c8b639 | 2022-05-20 14:46:12 -0700 | [diff] [blame] | 3 | * Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company) |
| 4 | * or an affiliate of Cypress Semiconductor Corporation. All rights reserved. |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 5 | * |
| 6 | * SPDX-License-Identifier: BSD-3-Clause |
| 7 | * |
| 8 | */ |
| 9 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 10 | #include "cmsis_os2.h" |
Xinyu Zhang | 4c640e8 | 2021-09-22 15:25:09 +0800 | [diff] [blame] | 11 | #include "cmsis_compiler.h" |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 12 | #include "tfm_ns_interface.h" |
Xinyu Zhang | eebbea3 | 2021-09-01 15:26:39 +0800 | [diff] [blame] | 13 | #include "tfm_nsid_manager.h" |
Kevin Peng | 342ec68 | 2022-04-29 10:36:58 +0800 | [diff] [blame] | 14 | #include "test_app.h" |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 15 | #include "tfm_plat_ns.h" |
Anton Komlev | c0ad604 | 2023-08-29 18:23:26 +0100 | [diff] [blame] | 16 | #include "Driver_USART.h" |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 17 | #include "device_cfg.h" |
Chris Brand | 5c8b639 | 2022-05-20 14:46:12 -0700 | [diff] [blame] | 18 | #ifdef TFM_PARTITION_NS_AGENT_MAILBOX |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 19 | #include "tfm_multi_core_api.h" |
| 20 | #include "tfm_ns_mailbox.h" |
| 21 | #endif |
Summer Qin | 7789423 | 2020-08-28 11:24:15 +0800 | [diff] [blame] | 22 | #include "tfm_log.h" |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 23 | #include "uart_stdout.h" |
Gabor Toth | 608e92f | 2022-09-06 12:41:41 +0200 | [diff] [blame] | 24 | #if (CONFIG_TFM_FLOAT_ABI >= 1) |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame] | 25 | #include "cmsis.h" |
| 26 | #endif |
Jianliang Shen | 407bfef | 2023-08-07 16:37:25 +0800 | [diff] [blame] | 27 | #ifdef CONFIG_TFM_ENALBE_PROFILING |
| 28 | #include "tfm_ns_profiling.h" |
| 29 | #endif |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * \brief Modified table template for user defined SVC functions |
| 33 | * |
| 34 | * \details RTX has a weak definition of osRtxUserSVC, which |
| 35 | * is overridden here |
| 36 | */ |
David Hu | acba69e | 2021-09-10 15:36:48 +0800 | [diff] [blame] | 37 | #if defined(__ARMCC_VERSION) |
| 38 | #if (__ARMCC_VERSION == 6110004) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 39 | /* Workaround needed for a bug in Armclang 6.11, more details at: |
| 40 | * http://www.keil.com/support/docs/4089.htm |
| 41 | */ |
| 42 | __attribute__((section(".gnu.linkonce"))) |
| 43 | #endif |
David Hu | acba69e | 2021-09-10 15:36:48 +0800 | [diff] [blame] | 44 | |
| 45 | /* Avoids the semihosting issue */ |
| 46 | #if (__ARMCC_VERSION >= 6010050) |
| 47 | __asm(" .global __ARM_use_no_argv\n"); |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Jianliang Shen | 407bfef | 2023-08-07 16:37:25 +0800 | [diff] [blame] | 51 | #ifdef CONFIG_TFM_ENALBE_PROFILING |
| 52 | |
| 53 | #if TFM_NS_REG_TEST || PSA_API_TEST_NS |
| 54 | #error "TF-M NS profiling shall not run together with NS regression tests" |
| 55 | #endif |
| 56 | /** |
| 57 | * \brief List of Profiling thread attributes |
| 58 | */ |
| 59 | const osThreadAttr_t thread_attr = { |
| 60 | .name = "profiling_thread", |
| 61 | .stack_size = 4096U, |
| 62 | }; |
| 63 | osThreadFunc_t thread_func = tfm_ns_profiling; |
| 64 | #else |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 65 | /** |
| 66 | * \brief List of RTOS thread attributes |
| 67 | */ |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 68 | static const osThreadAttr_t thread_attr = { |
| 69 | .name = "test_thread", |
Xinyu Zhang | eebbea3 | 2021-09-01 15:26:39 +0800 | [diff] [blame] | 70 | .stack_size = 4096U, |
| 71 | .tz_module = ((TZ_ModuleId_t)TFM_DEFAULT_NSID) |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 72 | }; |
Kevin Peng | 342ec68 | 2022-04-29 10:36:58 +0800 | [diff] [blame] | 73 | /** |
| 74 | * \brief Static globals to hold RTOS related quantities, |
| 75 | * main thread |
| 76 | */ |
| 77 | static osThreadFunc_t thread_func = test_app; |
Jianliang Shen | 407bfef | 2023-08-07 16:37:25 +0800 | [diff] [blame] | 78 | #endif |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 79 | |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 80 | #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD |
| 81 | static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner; |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 82 | static const osThreadAttr_t mailbox_thread_attr = { |
| 83 | .name = "mailbox_thread", |
Robert Rostohar | 26ebd14 | 2020-12-21 16:48:58 +0100 | [diff] [blame] | 84 | .stack_size = 1024U |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 85 | }; |
| 86 | #endif |
| 87 | |
Chris Brand | 5c8b639 | 2022-05-20 14:46:12 -0700 | [diff] [blame] | 88 | #ifdef TFM_PARTITION_NS_AGENT_MAILBOX |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 89 | static struct ns_mailbox_queue_t ns_mailbox_queue; |
| 90 | |
| 91 | static void tfm_ns_multi_core_boot(void) |
| 92 | { |
| 93 | int32_t ret; |
| 94 | |
Chris Brand | a149929 | 2021-10-28 12:02:05 -0700 | [diff] [blame] | 95 | LOG_MSG("Non-secure code running on non-secure core.\r\n"); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 96 | |
| 97 | if (tfm_ns_wait_for_s_cpu_ready()) { |
Chris Brand | a149929 | 2021-10-28 12:02:05 -0700 | [diff] [blame] | 98 | LOG_MSG("Error sync'ing with secure core.\r\n"); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 99 | |
| 100 | /* Avoid undefined behavior after multi-core sync-up failed */ |
| 101 | for (;;) { |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | ret = tfm_ns_mailbox_init(&ns_mailbox_queue); |
| 106 | if (ret != MAILBOX_SUCCESS) { |
Chris Brand | a149929 | 2021-10-28 12:02:05 -0700 | [diff] [blame] | 107 | LOG_MSG("Non-secure mailbox initialization failed.\r\n"); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 108 | |
| 109 | /* Avoid undefined behavior after NS mailbox initialization failed */ |
| 110 | for (;;) { |
| 111 | } |
| 112 | } |
| 113 | } |
Chris Brand | 5c8b639 | 2022-05-20 14:46:12 -0700 | [diff] [blame] | 114 | #endif /* TFM_PARTITION_NS_AGENT_MAILBOX */ |
| 115 | |
| 116 | #ifdef CONFIG_TFM_USE_TRUSTZONE |
David Hu | cdc51fb | 2021-04-06 18:10:46 +0800 | [diff] [blame] | 117 | extern uint32_t tfm_ns_interface_init(void); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | /** |
| 121 | * \brief Platform peripherals and devices initialization. |
| 122 | * Can be overridden for platform specific initialization. |
| 123 | * |
| 124 | * \return ARM_DRIVER_OK if the initialization succeeds |
| 125 | */ |
| 126 | __WEAK int32_t tfm_ns_platform_init(void) |
| 127 | { |
| 128 | stdio_init(); |
| 129 | |
| 130 | return ARM_DRIVER_OK; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * \brief Platform peripherals and devices de-initialization. |
| 135 | * Can be overridden for platform specific initialization. |
| 136 | * |
| 137 | * \return ARM_DRIVER_OK if the de-initialization succeeds |
| 138 | */ |
| 139 | __WEAK int32_t tfm_ns_platform_uninit(void) |
| 140 | { |
| 141 | stdio_uninit(); |
| 142 | |
| 143 | return ARM_DRIVER_OK; |
| 144 | } |
| 145 | |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame] | 146 | |
| 147 | __WEAK int32_t tfm_ns_cp_init(void) |
| 148 | { |
Gabor Toth | 608e92f | 2022-09-06 12:41:41 +0200 | [diff] [blame] | 149 | #if (CONFIG_TFM_FLOAT_ABI >= 1) |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame] | 150 | #ifdef __GNUC__ |
| 151 | /* Enable NSPE privileged and unprivilged access to the FP Extension */ |
| 152 | SCB->CPACR |= (3U << 10U*2U) /* enable CP10 full access */ |
| 153 | | (3U << 11U*2U); /* enable CP11 full access */ |
| 154 | #endif |
| 155 | #endif |
| 156 | return ARM_DRIVER_OK; |
| 157 | } |
| 158 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 159 | /** |
| 160 | * \brief main() function |
| 161 | */ |
| 162 | #ifndef __GNUC__ |
| 163 | __attribute__((noreturn)) |
| 164 | #endif |
| 165 | int main(void) |
| 166 | { |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 167 | if (tfm_ns_platform_init() != ARM_DRIVER_OK) { |
| 168 | /* Avoid undefined behavior if platform init failed */ |
| 169 | while(1); |
| 170 | } |
| 171 | |
Feder Liang | 7abe9a4 | 2021-12-03 17:54:58 +0800 | [diff] [blame] | 172 | if (tfm_ns_cp_init() != ARM_DRIVER_OK) { |
| 173 | /* Avoid undefined behavior if co-porcessor init failed */ |
| 174 | while(1); |
| 175 | } |
| 176 | |
David Hu | 4ae00fe | 2021-01-27 17:48:07 +0800 | [diff] [blame] | 177 | (void) osKernelInitialize(); |
| 178 | |
Chris Brand | 5c8b639 | 2022-05-20 14:46:12 -0700 | [diff] [blame] | 179 | #ifdef TFM_PARTITION_NS_AGENT_MAILBOX |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 180 | tfm_ns_multi_core_boot(); |
Chris Brand | 5c8b639 | 2022-05-20 14:46:12 -0700 | [diff] [blame] | 181 | #endif |
| 182 | |
| 183 | #ifdef CONFIG_TFM_USE_TRUSTZONE |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 184 | /* Initialize the TFM NS interface */ |
| 185 | tfm_ns_interface_init(); |
David Hu | cdc51fb | 2021-04-06 18:10:46 +0800 | [diff] [blame] | 186 | #endif |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 187 | |
David Hu | 98adf32 | 2020-09-01 16:18:46 +0800 | [diff] [blame] | 188 | #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD |
| 189 | (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr); |
| 190 | #endif |
| 191 | |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 192 | (void) osThreadNew(thread_func, NULL, &thread_attr); |
Kevin Peng | 62a8711 | 2020-07-07 15:07:46 +0800 | [diff] [blame] | 193 | |
| 194 | LOG_MSG("Non-Secure system starting...\r\n"); |
| 195 | (void) osKernelStart(); |
| 196 | |
| 197 | /* Reached only in case of error */ |
| 198 | for (;;) { |
| 199 | } |
| 200 | } |