blob: 8189142d30065bd1f56804d372a9a08112c04dee [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.
Kevin Peng62a87112020-07-07 15:07:46 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "tfm_api.h"
9#include "cmsis_os2.h"
Xinyu Zhang4c640e82021-09-22 15:25:09 +080010#include "cmsis_compiler.h"
Kevin Peng62a87112020-07-07 15:07:46 +080011#include "tfm_ns_interface.h"
Xinyu Zhangeebbea32021-09-01 15:26:39 +080012#include "tfm_nsid_manager.h"
Kevin Peng342ec682022-04-29 10:36:58 +080013#include "test_app.h"
Kevin Peng62a87112020-07-07 15:07:46 +080014#include "tfm_plat_ns.h"
Raef Coles5ee45ed2020-09-24 11:25:44 +010015#include "driver/Driver_USART.h"
Kevin Peng62a87112020-07-07 15:07:46 +080016#include "device_cfg.h"
17#ifdef TFM_MULTI_CORE_TOPOLOGY
18#include "tfm_multi_core_api.h"
19#include "tfm_ns_mailbox.h"
20#endif
Summer Qin77894232020-08-28 11:24:15 +080021#include "tfm_log.h"
Kevin Peng62a87112020-07-07 15:07:46 +080022#include "uart_stdout.h"
Feder Liang7abe9a42021-12-03 17:54:58 +080023#if (CONFIG_TFM_FP >= 1)
24#include "cmsis.h"
25#endif
Kevin Peng62a87112020-07-07 15:07:46 +080026
27/**
28 * \brief Modified table template for user defined SVC functions
29 *
30 * \details RTX has a weak definition of osRtxUserSVC, which
31 * is overridden here
32 */
David Huacba69e2021-09-10 15:36:48 +080033#if defined(__ARMCC_VERSION)
34#if (__ARMCC_VERSION == 6110004)
Kevin Peng62a87112020-07-07 15:07:46 +080035/* Workaround needed for a bug in Armclang 6.11, more details at:
36 * http://www.keil.com/support/docs/4089.htm
37 */
38__attribute__((section(".gnu.linkonce")))
39#endif
David Huacba69e2021-09-10 15:36:48 +080040
41/* Avoids the semihosting issue */
42#if (__ARMCC_VERSION >= 6010050)
43__asm(" .global __ARM_use_no_argv\n");
44#endif
45#endif
46
Kevin Peng62a87112020-07-07 15:07:46 +080047/**
48 * \brief List of RTOS thread attributes
49 */
Kevin Peng62a87112020-07-07 15:07:46 +080050static const osThreadAttr_t thread_attr = {
51 .name = "test_thread",
Xinyu Zhangeebbea32021-09-01 15:26:39 +080052 .stack_size = 4096U,
53 .tz_module = ((TZ_ModuleId_t)TFM_DEFAULT_NSID)
Kevin Peng62a87112020-07-07 15:07:46 +080054};
Kevin Peng342ec682022-04-29 10:36:58 +080055/**
56 * \brief Static globals to hold RTOS related quantities,
57 * main thread
58 */
59static osThreadFunc_t thread_func = test_app;
Kevin Peng62a87112020-07-07 15:07:46 +080060
David Hu98adf322020-09-01 16:18:46 +080061#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
62static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
David Hu98adf322020-09-01 16:18:46 +080063static const osThreadAttr_t mailbox_thread_attr = {
64 .name = "mailbox_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010065 .stack_size = 1024U
David Hu98adf322020-09-01 16:18:46 +080066};
67#endif
68
Kevin Peng62a87112020-07-07 15:07:46 +080069#ifdef TFM_MULTI_CORE_TOPOLOGY
70static struct ns_mailbox_queue_t ns_mailbox_queue;
71
72static void tfm_ns_multi_core_boot(void)
73{
74 int32_t ret;
75
Chris Branda1499292021-10-28 12:02:05 -070076 LOG_MSG("Non-secure code running on non-secure core.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080077
78 if (tfm_ns_wait_for_s_cpu_ready()) {
Chris Branda1499292021-10-28 12:02:05 -070079 LOG_MSG("Error sync'ing with secure core.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080080
81 /* Avoid undefined behavior after multi-core sync-up failed */
82 for (;;) {
83 }
84 }
85
86 ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
87 if (ret != MAILBOX_SUCCESS) {
Chris Branda1499292021-10-28 12:02:05 -070088 LOG_MSG("Non-secure mailbox initialization failed.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080089
90 /* Avoid undefined behavior after NS mailbox initialization failed */
91 for (;;) {
92 }
93 }
94}
David Hucdc51fb2021-04-06 18:10:46 +080095#else
96extern uint32_t tfm_ns_interface_init(void);
Kevin Peng62a87112020-07-07 15:07:46 +080097#endif
98
99/**
100 * \brief Platform peripherals and devices initialization.
101 * Can be overridden for platform specific initialization.
102 *
103 * \return ARM_DRIVER_OK if the initialization succeeds
104 */
105__WEAK int32_t tfm_ns_platform_init(void)
106{
107 stdio_init();
108
109 return ARM_DRIVER_OK;
110}
111
112/**
113 * \brief Platform peripherals and devices de-initialization.
114 * Can be overridden for platform specific initialization.
115 *
116 * \return ARM_DRIVER_OK if the de-initialization succeeds
117 */
118__WEAK int32_t tfm_ns_platform_uninit(void)
119{
120 stdio_uninit();
121
122 return ARM_DRIVER_OK;
123}
124
Feder Liang7abe9a42021-12-03 17:54:58 +0800125
126__WEAK int32_t tfm_ns_cp_init(void)
127{
128#if (CONFIG_TFM_FP >= 1)
129#ifdef __GNUC__
130 /* Enable NSPE privileged and unprivilged access to the FP Extension */
131 SCB->CPACR |= (3U << 10U*2U) /* enable CP10 full access */
132 | (3U << 11U*2U); /* enable CP11 full access */
133#endif
134#endif
135 return ARM_DRIVER_OK;
136}
137
Kevin Peng62a87112020-07-07 15:07:46 +0800138/**
139 * \brief main() function
140 */
141#ifndef __GNUC__
142__attribute__((noreturn))
143#endif
144int main(void)
145{
Kevin Peng62a87112020-07-07 15:07:46 +0800146 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
147 /* Avoid undefined behavior if platform init failed */
148 while(1);
149 }
150
Feder Liang7abe9a42021-12-03 17:54:58 +0800151 if (tfm_ns_cp_init() != ARM_DRIVER_OK) {
152 /* Avoid undefined behavior if co-porcessor init failed */
153 while(1);
154 }
155
David Hu4ae00fe2021-01-27 17:48:07 +0800156 (void) osKernelInitialize();
157
Kevin Peng62a87112020-07-07 15:07:46 +0800158#ifdef TFM_MULTI_CORE_TOPOLOGY
159 tfm_ns_multi_core_boot();
David Hucdc51fb2021-04-06 18:10:46 +0800160#else
Kevin Peng62a87112020-07-07 15:07:46 +0800161 /* Initialize the TFM NS interface */
162 tfm_ns_interface_init();
David Hucdc51fb2021-04-06 18:10:46 +0800163#endif
Kevin Peng62a87112020-07-07 15:07:46 +0800164
David Hu98adf322020-09-01 16:18:46 +0800165#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
166 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
167#endif
168
Kevin Peng62a87112020-07-07 15:07:46 +0800169 (void) osThreadNew(thread_func, NULL, &thread_attr);
Kevin Peng62a87112020-07-07 15:07:46 +0800170
171 LOG_MSG("Non-Secure system starting...\r\n");
172 (void) osKernelStart();
173
174 /* Reached only in case of error */
175 for (;;) {
176 }
177}