blob: 9537487844f8c71c79359484a5b767959c441074 [file] [log] [blame]
Kevin Peng62a87112020-07-07 15:07:46 +08001/*
Feder Liang7abe9a42021-12-03 17:54:58 +08002 * Copyright (c) 2017-2022, Arm Limited. All rights reserved.
Chris Brand5c8b6392022-05-20 14:46:12 -07003 * Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
4 * or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
Kevin Peng62a87112020-07-07 15:07:46 +08005 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
8 */
9
10#include "tfm_api.h"
11#include "cmsis_os2.h"
Xinyu Zhang4c640e82021-09-22 15:25:09 +080012#include "cmsis_compiler.h"
Kevin Peng62a87112020-07-07 15:07:46 +080013#include "tfm_ns_interface.h"
Xinyu Zhangeebbea32021-09-01 15:26:39 +080014#include "tfm_nsid_manager.h"
Kevin Peng342ec682022-04-29 10:36:58 +080015#include "test_app.h"
Kevin Peng62a87112020-07-07 15:07:46 +080016#include "tfm_plat_ns.h"
Raef Coles5ee45ed2020-09-24 11:25:44 +010017#include "driver/Driver_USART.h"
Kevin Peng62a87112020-07-07 15:07:46 +080018#include "device_cfg.h"
Chris Brand5c8b6392022-05-20 14:46:12 -070019#ifdef TFM_PARTITION_NS_AGENT_MAILBOX
Kevin Peng62a87112020-07-07 15:07:46 +080020#include "tfm_multi_core_api.h"
21#include "tfm_ns_mailbox.h"
22#endif
Summer Qin77894232020-08-28 11:24:15 +080023#include "tfm_log.h"
Kevin Peng62a87112020-07-07 15:07:46 +080024#include "uart_stdout.h"
Gabor Toth608e92f2022-09-06 12:41:41 +020025#if (CONFIG_TFM_FLOAT_ABI >= 1)
Feder Liang7abe9a42021-12-03 17:54:58 +080026#include "cmsis.h"
27#endif
Kevin Peng62a87112020-07-07 15:07:46 +080028
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 Huacba69e2021-09-10 15:36:48 +080035#if defined(__ARMCC_VERSION)
36#if (__ARMCC_VERSION == 6110004)
Kevin Peng62a87112020-07-07 15:07:46 +080037/* 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 Huacba69e2021-09-10 15:36:48 +080042
43/* Avoids the semihosting issue */
44#if (__ARMCC_VERSION >= 6010050)
45__asm(" .global __ARM_use_no_argv\n");
46#endif
47#endif
48
Kevin Peng62a87112020-07-07 15:07:46 +080049/**
50 * \brief List of RTOS thread attributes
51 */
Kevin Peng62a87112020-07-07 15:07:46 +080052static const osThreadAttr_t thread_attr = {
53 .name = "test_thread",
Xinyu Zhangeebbea32021-09-01 15:26:39 +080054 .stack_size = 4096U,
55 .tz_module = ((TZ_ModuleId_t)TFM_DEFAULT_NSID)
Kevin Peng62a87112020-07-07 15:07:46 +080056};
Kevin Peng342ec682022-04-29 10:36:58 +080057/**
58 * \brief Static globals to hold RTOS related quantities,
59 * main thread
60 */
61static osThreadFunc_t thread_func = test_app;
Kevin Peng62a87112020-07-07 15:07:46 +080062
David Hu98adf322020-09-01 16:18:46 +080063#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
64static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
David Hu98adf322020-09-01 16:18:46 +080065static const osThreadAttr_t mailbox_thread_attr = {
66 .name = "mailbox_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010067 .stack_size = 1024U
David Hu98adf322020-09-01 16:18:46 +080068};
69#endif
70
Chris Brand5c8b6392022-05-20 14:46:12 -070071#ifdef TFM_PARTITION_NS_AGENT_MAILBOX
Kevin Peng62a87112020-07-07 15:07:46 +080072static struct ns_mailbox_queue_t ns_mailbox_queue;
73
74static void tfm_ns_multi_core_boot(void)
75{
76 int32_t ret;
77
Chris Branda1499292021-10-28 12:02:05 -070078 LOG_MSG("Non-secure code running on non-secure core.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080079
80 if (tfm_ns_wait_for_s_cpu_ready()) {
Chris Branda1499292021-10-28 12:02:05 -070081 LOG_MSG("Error sync'ing with secure core.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080082
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 Branda1499292021-10-28 12:02:05 -070090 LOG_MSG("Non-secure mailbox initialization failed.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080091
92 /* Avoid undefined behavior after NS mailbox initialization failed */
93 for (;;) {
94 }
95 }
96}
Chris Brand5c8b6392022-05-20 14:46:12 -070097#endif /* TFM_PARTITION_NS_AGENT_MAILBOX */
98
99#ifdef CONFIG_TFM_USE_TRUSTZONE
David Hucdc51fb2021-04-06 18:10:46 +0800100extern uint32_t tfm_ns_interface_init(void);
Kevin Peng62a87112020-07-07 15:07:46 +0800101#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 Liang7abe9a42021-12-03 17:54:58 +0800129
130__WEAK int32_t tfm_ns_cp_init(void)
131{
Gabor Toth608e92f2022-09-06 12:41:41 +0200132#if (CONFIG_TFM_FLOAT_ABI >= 1)
Feder Liang7abe9a42021-12-03 17:54:58 +0800133#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 Peng62a87112020-07-07 15:07:46 +0800142/**
143 * \brief main() function
144 */
145#ifndef __GNUC__
146__attribute__((noreturn))
147#endif
148int main(void)
149{
Kevin Peng62a87112020-07-07 15:07:46 +0800150 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
151 /* Avoid undefined behavior if platform init failed */
152 while(1);
153 }
154
Feder Liang7abe9a42021-12-03 17:54:58 +0800155 if (tfm_ns_cp_init() != ARM_DRIVER_OK) {
156 /* Avoid undefined behavior if co-porcessor init failed */
157 while(1);
158 }
159
David Hu4ae00fe2021-01-27 17:48:07 +0800160 (void) osKernelInitialize();
161
Chris Brand5c8b6392022-05-20 14:46:12 -0700162#ifdef TFM_PARTITION_NS_AGENT_MAILBOX
Kevin Peng62a87112020-07-07 15:07:46 +0800163 tfm_ns_multi_core_boot();
Chris Brand5c8b6392022-05-20 14:46:12 -0700164#endif
165
166#ifdef CONFIG_TFM_USE_TRUSTZONE
Kevin Peng62a87112020-07-07 15:07:46 +0800167 /* Initialize the TFM NS interface */
168 tfm_ns_interface_init();
David Hucdc51fb2021-04-06 18:10:46 +0800169#endif
Kevin Peng62a87112020-07-07 15:07:46 +0800170
David Hu98adf322020-09-01 16:18:46 +0800171#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
172 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
173#endif
174
Kevin Peng62a87112020-07-07 15:07:46 +0800175 (void) osThreadNew(thread_func, NULL, &thread_attr);
Kevin Peng62a87112020-07-07 15:07:46 +0800176
177 LOG_MSG("Non-Secure system starting...\r\n");
178 (void) osKernelStart();
179
180 /* Reached only in case of error */
181 for (;;) {
182 }
183}