blob: a387803b9a52828bad049068fc4d4bd9b6b430c1 [file] [log] [blame]
Kevin Peng62a87112020-07-07 15:07:46 +08001/*
Antonio de Angelis8f05b042025-07-11 21:55:54 +01002 * 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
Kevin Peng62a87112020-07-07 15:07:46 +080010#include "cmsis_os2.h"
Xinyu Zhang4c640e82021-09-22 15:25:09 +080011#include "cmsis_compiler.h"
Kevin Peng62a87112020-07-07 15:07:46 +080012#include "tfm_ns_interface.h"
Xinyu Zhangeebbea32021-09-01 15:26:39 +080013#include "tfm_nsid_manager.h"
Kevin Peng342ec682022-04-29 10:36:58 +080014#include "test_app.h"
Kevin Peng62a87112020-07-07 15:07:46 +080015#include "tfm_plat_ns.h"
Anton Komlevc0ad6042023-08-29 18:23:26 +010016#include "Driver_USART.h"
Kevin Peng62a87112020-07-07 15:07:46 +080017#include "device_cfg.h"
Jamie Foxcd0c2212023-11-28 17:47:53 +000018#ifdef TFM_NS_MAILBOX_API
Kevin Peng62a87112020-07-07 15:07:46 +080019#include "tfm_multi_core_api.h"
20#include "tfm_ns_mailbox.h"
21#endif
Antonio de Angelis8f05b042025-07-11 21:55:54 +010022#include "tfm_log.h"
Kevin Peng62a87112020-07-07 15:07:46 +080023#include "uart_stdout.h"
Gabor Toth608e92f2022-09-06 12:41:41 +020024#if (CONFIG_TFM_FLOAT_ABI >= 1)
Feder Liang7abe9a42021-12-03 17:54:58 +080025#include "cmsis.h"
26#endif
Kevin Peng62a87112020-07-07 15:07:46 +080027
28/**
29 * \brief Modified table template for user defined SVC functions
30 *
31 * \details RTX has a weak definition of osRtxUserSVC, which
32 * is overridden here
33 */
David Huacba69e2021-09-10 15:36:48 +080034#if defined(__ARMCC_VERSION)
35#if (__ARMCC_VERSION == 6110004)
Kevin Peng62a87112020-07-07 15:07:46 +080036/* Workaround needed for a bug in Armclang 6.11, more details at:
37 * http://www.keil.com/support/docs/4089.htm
38 */
39__attribute__((section(".gnu.linkonce")))
40#endif
David Huacba69e2021-09-10 15:36:48 +080041
42/* Avoids the semihosting issue */
43#if (__ARMCC_VERSION >= 6010050)
44__asm(" .global __ARM_use_no_argv\n");
45#endif
46#endif
47
Kevin Peng62a87112020-07-07 15:07:46 +080048/**
49 * \brief List of RTOS thread attributes
50 */
Kevin Peng62a87112020-07-07 15:07:46 +080051static const osThreadAttr_t thread_attr = {
52 .name = "test_thread",
Xinyu Zhangeebbea32021-09-01 15:26:39 +080053 .stack_size = 4096U,
54 .tz_module = ((TZ_ModuleId_t)TFM_DEFAULT_NSID)
Kevin Peng62a87112020-07-07 15:07:46 +080055};
Kevin Peng342ec682022-04-29 10:36:58 +080056/**
57 * \brief Static globals to hold RTOS related quantities,
58 * main thread
59 */
60static osThreadFunc_t thread_func = test_app;
Kevin Peng62a87112020-07-07 15:07:46 +080061
David Hu98adf322020-09-01 16:18:46 +080062#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
63static osThreadFunc_t mailbox_thread_func = tfm_ns_mailbox_thread_runner;
David Hu98adf322020-09-01 16:18:46 +080064static const osThreadAttr_t mailbox_thread_attr = {
65 .name = "mailbox_thread",
Robert Rostohar26ebd142020-12-21 16:48:58 +010066 .stack_size = 1024U
David Hu98adf322020-09-01 16:18:46 +080067};
68#endif
69
Jamie Foxcd0c2212023-11-28 17:47:53 +000070#ifdef TFM_NS_MAILBOX_API
Kevin Peng62a87112020-07-07 15:07:46 +080071static struct ns_mailbox_queue_t ns_mailbox_queue;
72
73static void tfm_ns_multi_core_boot(void)
74{
75 int32_t ret;
76
Chris Branda1499292021-10-28 12:02:05 -070077 LOG_MSG("Non-secure code running on non-secure core.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080078
79 if (tfm_ns_wait_for_s_cpu_ready()) {
Chris Branda1499292021-10-28 12:02:05 -070080 LOG_MSG("Error sync'ing with secure core.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080081
82 /* Avoid undefined behavior after multi-core sync-up failed */
83 for (;;) {
84 }
85 }
86
87 ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
88 if (ret != MAILBOX_SUCCESS) {
Chris Branda1499292021-10-28 12:02:05 -070089 LOG_MSG("Non-secure mailbox initialization failed.\r\n");
Kevin Peng62a87112020-07-07 15:07:46 +080090
91 /* Avoid undefined behavior after NS mailbox initialization failed */
92 for (;;) {
93 }
94 }
95}
Jamie Foxcd0c2212023-11-28 17:47:53 +000096#else /* TFM_NS_MAILBOX_API */
David Hucdc51fb2021-04-06 18:10:46 +080097extern uint32_t tfm_ns_interface_init(void);
Jamie Foxcd0c2212023-11-28 17:47:53 +000098#endif /* TFM_NS_MAILBOX_API */
Kevin Peng62a87112020-07-07 15:07:46 +080099
100/**
101 * \brief Platform peripherals and devices initialization.
102 * Can be overridden for platform specific initialization.
103 *
104 * \return ARM_DRIVER_OK if the initialization succeeds
105 */
106__WEAK int32_t tfm_ns_platform_init(void)
107{
108 stdio_init();
109
110 return ARM_DRIVER_OK;
111}
112
113/**
114 * \brief Platform peripherals and devices de-initialization.
115 * Can be overridden for platform specific initialization.
116 *
117 * \return ARM_DRIVER_OK if the de-initialization succeeds
118 */
119__WEAK int32_t tfm_ns_platform_uninit(void)
120{
121 stdio_uninit();
122
123 return ARM_DRIVER_OK;
124}
125
Feder Liang7abe9a42021-12-03 17:54:58 +0800126
127__WEAK int32_t tfm_ns_cp_init(void)
128{
Gabor Toth608e92f2022-09-06 12:41:41 +0200129#if (CONFIG_TFM_FLOAT_ABI >= 1)
Feder Liang7abe9a42021-12-03 17:54:58 +0800130#ifdef __GNUC__
131 /* Enable NSPE privileged and unprivilged access to the FP Extension */
132 SCB->CPACR |= (3U << 10U*2U) /* enable CP10 full access */
133 | (3U << 11U*2U); /* enable CP11 full access */
134#endif
135#endif
136 return ARM_DRIVER_OK;
137}
138
Kevin Peng62a87112020-07-07 15:07:46 +0800139/**
140 * \brief main() function
141 */
142#ifndef __GNUC__
143__attribute__((noreturn))
144#endif
145int main(void)
146{
Kevin Peng62a87112020-07-07 15:07:46 +0800147 if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
148 /* Avoid undefined behavior if platform init failed */
149 while(1);
150 }
151
Feder Liang7abe9a42021-12-03 17:54:58 +0800152 if (tfm_ns_cp_init() != ARM_DRIVER_OK) {
153 /* Avoid undefined behavior if co-porcessor init failed */
154 while(1);
155 }
156
David Hu4ae00fe2021-01-27 17:48:07 +0800157 (void) osKernelInitialize();
158
Jamie Foxcd0c2212023-11-28 17:47:53 +0000159#ifdef TFM_NS_MAILBOX_API
Kevin Peng62a87112020-07-07 15:07:46 +0800160 tfm_ns_multi_core_boot();
Jamie Foxcd0c2212023-11-28 17:47:53 +0000161#else
Kevin Peng62a87112020-07-07 15:07:46 +0800162 /* Initialize the TFM NS interface */
163 tfm_ns_interface_init();
David Hucdc51fb2021-04-06 18:10:46 +0800164#endif
Kevin Peng62a87112020-07-07 15:07:46 +0800165
David Hu98adf322020-09-01 16:18:46 +0800166#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
167 (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
168#endif
169
Kevin Peng62a87112020-07-07 15:07:46 +0800170 (void) osThreadNew(thread_func, NULL, &thread_attr);
Kevin Peng62a87112020-07-07 15:07:46 +0800171
172 LOG_MSG("Non-Secure system starting...\r\n");
173 (void) osKernelStart();
174
175 /* Reached only in case of error */
176 for (;;) {
177 }
178}