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