Skip to content

Commit a79d3ce

Browse files
authored
Merge pull request #12271 from jainvikas8/jae-feature-twincpu-6-mbed-os-integration
Make cypress psoc64 TFM ready and also add TF-M initialization
2 parents 41309c6 + 9b7ef82 commit a79d3ce

File tree

10 files changed

+611
-40
lines changed

10 files changed

+611
-40
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2020 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed_error.h"
19+
#include "tfm_multi_core_api.h"
20+
#include "tfm_ns_mailbox.h"
21+
#include "platform_multicore.h"
22+
#include "tfm_ns_interface.h"
23+
24+
static struct ns_mailbox_queue_t ns_mailbox_queue;
25+
26+
void mbed_tfm_init(void)
27+
{
28+
/*
29+
* In case the of dual CPU, we need to initialize IPC, mailbox
30+
* and NS interface after the RTOS has started to enable
31+
* communication from Secure and Non-Secure cores.
32+
*/
33+
int32_t ret;
34+
35+
ret = tfm_ns_wait_for_s_cpu_ready();
36+
/*
37+
* Normally would expect "TFM_SUCCESS" returned here by TF-M, as this
38+
* isn't a mailbox function. There may be some platforms other than PSoC6,
39+
* which doesn't require tfm_ns_wait_for_s_cpu_ready() implementation.
40+
* "PLATFORM_MAILBOX_SUCCESS" is a low-level error code and should be
41+
* replaced by "TFM_SUCCESS".
42+
* As the function definition has been imported from the TF-M, therefore
43+
* a issue has been raised - https://developer.trustedfirmware.org/T660
44+
*/
45+
if (ret != PLATFORM_MAILBOX_SUCCESS) {
46+
/* Avoid undefined behavior after multi-core sync-up failed */
47+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM,
48+
MBED_ERROR_CODE_INITIALIZATION_FAILED),
49+
"Failed to sync-up multi-core");
50+
}
51+
52+
ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
53+
if (ret != MAILBOX_SUCCESS) {
54+
/* Avoid undefined behavior after NS mailbox initialization failed */
55+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM,
56+
MBED_ERROR_CODE_INITIALIZATION_FAILED),
57+
"Failed to initialize NS mailbox");
58+
}
59+
60+
ret = tfm_ns_interface_init();
61+
if (ret != TFM_SUCCESS) {
62+
/* Avoid undefined behavior after NS interface initialization failed */
63+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM,
64+
MBED_ERROR_CODE_INITIALIZATION_FAILED),
65+
"Failed to initialize NS interface");
66+
}
67+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2020 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed_error.h"
19+
#include "tfm_ns_interface.h"
20+
21+
void mbed_tfm_init(void)
22+
{
23+
/*
24+
* In case of V8-M, we need to initialize NS interface
25+
* after the RTOS has started to enable
26+
* communication from Secure and Non-Secure cores.
27+
*/
28+
int32_t ret;
29+
30+
ret = tfm_ns_interface_init();
31+
if (ret != TFM_SUCCESS) {
32+
/* Avoid undefined behavior after NS interface initialization failed */
33+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM,
34+
MBED_ERROR_CODE_INITIALIZATION_FAILED),
35+
"Failed to initialize NS interface");
36+
}
37+
}

rtos/source/TARGET_CORTEX/mbed_boot.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2016 ARM Limited
2+
* Copyright (c) 2006-2020 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -95,6 +95,7 @@ void mbed_init(void)
9595
void mbed_start(void)
9696
{
9797
mbed_toolchain_init();
98+
mbed_tfm_init();
9899
mbed_main();
99100
mbed_error_initialize();
100101
main();
@@ -110,6 +111,11 @@ MBED_WEAK void software_init_hook_rtos()
110111
// Nothing by default
111112
}
112113

114+
MBED_WEAK void mbed_tfm_init(void)
115+
{
116+
// Nothing by default
117+
}
118+
113119
MBED_WEAK void mbed_main(void)
114120
{
115121
// Nothing by default

rtos/source/TARGET_CORTEX/mbed_boot.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2018-2019 ARM Limited
2+
* Copyright (c) 2018-2020 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -47,6 +47,7 @@ extern "C" {
4747
* - Start scheduler
4848
* - main thread calls start mbed
4949
* 4. Start mbed
50+
* - Call mbed_tfm_init
5051
* - Call mbed_main
5152
* - Call main
5253
*
@@ -149,6 +150,20 @@ void mbed_toolchain_init(void);
149150
*/
150151
void mbed_sdk_init(void);
151152

153+
/**
154+
* TF-M specific application hook for running code before mbed_main
155+
*
156+
* This is a weak function which can be overridden by an application
157+
* to allow code to run before mbed_main is called.
158+
* Some TF-M supported platforms require synchronization between
159+
* Secure and Non-Secure cores, therefore these tasks are done here.
160+
*
161+
* Preconditions:
162+
* - The RTOS has been started by a call to mbed_rtos_start
163+
* - The toolchain has been initialized by a call to mbed_toolchain_init
164+
*/
165+
void mbed_tfm_init(void);
166+
152167
/**
153168
* Application hook for running code before main
154169
*
@@ -157,6 +172,7 @@ void mbed_sdk_init(void);
157172
*
158173
* Preconditions:
159174
* - The RTOS has been started by a call to mbed_rtos_start
175+
* - TFM support has been initialized by a call to mbed_tfm_init.
160176
* - The toolchain has been initialized by a call to mbed_toolchain_init
161177
*/
162178
void mbed_main(void);

targets/TARGET_Cypress/TARGET_PSOC6/TARGET_CY8CKIT_064S2_4343W/device/COMPONENT_CM4/TOOLCHAIN_ARM/cyb06xxa_cm4.sct

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
;*******************************************************************************
2828
;* \copyright
2929
;* Copyright 2016-2019 Cypress Semiconductor Corporation
30+
;* Copyright 2020 Arm Limited
3031
;* SPDX-License-Identifier: Apache-2.0
3132
;*
3233
;* Licensed under the Apache License, Version 2.0 (the "License");
@@ -42,8 +43,10 @@
4243
;* limitations under the License.
4344
;******************************************************************************/
4445

46+
#include "../../../partition/region_defs.h"
47+
4548
#if !defined(MBED_ROM_START)
46-
#define MBED_ROM_START 0x10000000
49+
#define MBED_ROM_START NS_CODE_START
4750
#endif
4851

4952
;* MBED_APP_START is being used by the bootloader build script and
@@ -55,7 +58,7 @@
5558
#endif
5659

5760
#if !defined(MBED_ROM_SIZE)
58-
#define MBED_ROM_SIZE 0x001D0000
61+
#define MBED_ROM_SIZE NS_CODE_SIZE
5962
#endif
6063

6164
;* MBED_APP_SIZE is being used by the bootloader build script and
@@ -67,19 +70,19 @@
6770
#endif
6871

6972
#if !defined(MBED_RAM_START)
70-
#define MBED_RAM_START 0x08000000
73+
#define MBED_RAM_START NS_DATA_START
7174
#endif
7275

7376
#if !defined(MBED_RAM_SIZE)
74-
#define MBED_RAM_SIZE 0x000EA000
77+
#define MBED_RAM_SIZE NS_DATA_SIZE
7578
#endif
7679

7780
#if !defined(MBED_BOOT_STACK_SIZE)
78-
#define MBED_BOOT_STACK_SIZE 0x400
81+
#define MBED_BOOT_STACK_SIZE NS_MSP_STACK_SIZE
7982
#endif
8083

81-
; The size of the stack section at the end of CM4 SRAM
82-
#define STACK_SIZE MBED_BOOT_STACK_SIZE
84+
; Shared memory area between Non-secure and Secure
85+
#define MBED_DATA_SHARED_SIZE NS_DATA_SHARED_SIZE
8386

8487
; The defines below describe the location and size of blocks of memory in the target.
8588
; Use these defines to specify the memory regions available for allocation.
@@ -92,9 +95,6 @@
9295
#define FLASH_START MBED_APP_START
9396
#define FLASH_SIZE MBED_APP_SIZE
9497

95-
; The size of the MCU boot header area at the start of FLASH
96-
#define BOOT_HEADER_SIZE 0x00000400
97-
9898
; The following defines describe a 32K flash region used for EEPROM emulation.
9999
; This region can also be used as the general purpose flash.
100100
; You can assign sections to this memory region for only one of the cores.
@@ -136,7 +136,7 @@
136136
; Cortex-M4 application flash area
137137
LR_IROM1 FLASH_START FLASH_SIZE
138138
{
139-
ER_FLASH_VECTORS +BOOT_HEADER_SIZE
139+
ER_FLASH_VECTORS +0
140140
{
141141
* (RESET, +FIRST)
142142
}
@@ -166,15 +166,27 @@ LR_IROM1 FLASH_START FLASH_SIZE
166166
}
167167

168168
; Application heap area (HEAP)
169-
ARM_LIB_HEAP +0 EMPTY RAM_START+RAM_SIZE-STACK_SIZE-ImageLimit(RW_IRAM1)
170-
{
169+
ARM_LIB_HEAP +0 ALIGN 4 EMPTY RAM_START+RAM_SIZE-MBED_BOOT_STACK_SIZE-MBED_DATA_SHARED_SIZE-ImageLimit(RW_IRAM1)
170+
{
171171
}
172-
172+
173173
; Stack region growing down
174-
ARM_LIB_STACK RAM_START+RAM_SIZE EMPTY -STACK_SIZE
174+
ARM_LIB_STACK RAM_START+RAM_SIZE-MBED_DATA_SHARED_SIZE ALIGN 4 EMPTY -MBED_BOOT_STACK_SIZE
175+
{
176+
}
177+
178+
; Stack area overflowed within RAM
179+
ScatterAssert(ImageBase(ARM_LIB_STACK) + ImageLength(ARM_LIB_STACK) == RAM_START+RAM_SIZE-MBED_DATA_SHARED_SIZE)
180+
181+
; Shared region
182+
ARM_LIB_SHARED RAM_START+RAM_SIZE-MBED_DATA_SHARED_SIZE ALIGN 4 EMPTY MBED_DATA_SHARED_SIZE
175183
{
176-
}
177-
184+
}
185+
186+
; Shared area overflowed within RAM
187+
ScatterAssert(ImageBase(ARM_LIB_SHARED) + ImageLength(ARM_LIB_SHARED) == RAM_START+RAM_SIZE)
188+
189+
178190
; Used for the digital signature of the secure application and the
179191
; Bootloader SDK application. The size of the section depends on the required
180192
; data size.

targets/TARGET_Cypress/TARGET_PSOC6/TARGET_CY8CKIT_064S2_4343W/device/COMPONENT_CM4/TOOLCHAIN_GCC_ARM/cyb06xxa_cm4.ld

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
********************************************************************************
2121
* \copyright
2222
* Copyright 2016-2019 Cypress Semiconductor Corporation
23+
* Copyright 2020 Arm Limited
2324
* SPDX-License-Identifier: Apache-2.0
2425
*
2526
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -40,47 +41,47 @@ SEARCH_DIR(.)
4041
GROUP(-lgcc -lc -lnosys)
4142
ENTRY(Reset_Handler)
4243

44+
#include "../../../partition/region_defs.h"
45+
4346
#if !defined(MBED_ROM_START)
44-
#define MBED_ROM_START 0x10000000
47+
#define MBED_ROM_START NS_CODE_START
4548
#endif
4649

4750
/* MBED_APP_START is being used by the bootloader build script and
4851
* will be calculate by the system. Without bootloader the MBED_APP_START
4952
* is equal to MBED_ROM_START
5053
*/
5154
#if !defined(MBED_APP_START)
52-
#define MBED_APP_START MBED_ROM_START
55+
#define MBED_APP_START MBED_ROM_START
5356
#endif
5457

5558
#if !defined(MBED_ROM_SIZE)
56-
#define MBED_ROM_SIZE 0x001D0000
59+
#define MBED_ROM_SIZE NS_CODE_SIZE
5760
#endif
5861

5962
/* MBED_APP_SIZE is being used by the bootloader build script and
6063
* will be calculate by the system. Without bootloader the MBED_APP_SIZE
6164
* is equal to MBED_ROM_SIZE
6265
*/
6366
#if !defined(MBED_APP_SIZE)
64-
#define MBED_APP_SIZE MBED_ROM_SIZE
67+
#define MBED_APP_SIZE MBED_ROM_SIZE
6568
#endif
6669

6770
#if !defined(MBED_RAM_START)
68-
#define MBED_RAM_START 0x08000000
71+
#define MBED_RAM_START NS_DATA_START
6972
#endif
7073

7174
#if !defined(MBED_RAM_SIZE)
72-
#define MBED_RAM_SIZE 0x000EA000
75+
#define MBED_RAM_SIZE NS_DATA_SIZE
7376
#endif
7477

78+
/* Size of the stack section in CM4 SRAM area */
7579
#if !defined(MBED_BOOT_STACK_SIZE)
76-
#define MBED_BOOT_STACK_SIZE 0x400
80+
#define MBED_BOOT_STACK_SIZE NS_MSP_STACK_SIZE
7781
#endif
7882

79-
/* The size of the stack section at the end of CM4 SRAM */
80-
STACK_SIZE = MBED_BOOT_STACK_SIZE;
81-
82-
/* The size of the MCU boot header area at the start of FLASH */
83-
BOOT_HEADER_SIZE = 0x400;
83+
/* Shared memory area between Non-Secure and Secure */
84+
#define MBED_DATA_SHARED_SIZE NS_DATA_SHARED_SIZE
8485

8586
/* Force symbol to be entered in the output file as an undefined symbol. Doing
8687
* this may, for example, trigger linking of additional modules from standard
@@ -157,7 +158,7 @@ GROUP(libgcc.a libc.a libm.a libnosys.a)
157158
SECTIONS
158159
{
159160
/* Cortex-M4 application flash area */
160-
.text ORIGIN(flash) + BOOT_HEADER_SIZE :
161+
.text ORIGIN(flash) :
161162
{
162163
/* Cortex-M4 flash vector table */
163164
. = ALIGN(4);
@@ -330,20 +331,26 @@ SECTIONS
330331
__end__ = .;
331332
end = __end__;
332333
KEEP(*(.heap*))
333-
. = ORIGIN(ram) + LENGTH(ram) - STACK_SIZE;
334+
. = ORIGIN(ram) + LENGTH(ram) - MBED_BOOT_STACK_SIZE - MBED_DATA_SHARED_SIZE;
335+
. = ALIGN(4);
336+
__StackLimit = .;
334337
__HeapLimit = .;
335338
} > ram
336339

337340

338-
/* Set stack top to end of RAM, and stack limit move down by
339-
* size of stack_dummy section */
340-
__StackTop = ORIGIN(ram) + LENGTH(ram);
341-
__StackLimit = __StackTop - STACK_SIZE;
341+
__StackTop = (__StackLimit + MBED_BOOT_STACK_SIZE + 3) & 0xFFFFFFFC;
342342
PROVIDE(__stack = __StackTop);
343343

344-
/* Check if data + heap + stack exceeds RAM limit */
345-
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
344+
.shared __StackTop (NOLOAD):
345+
{
346+
__SharedStart = .;
347+
. += MBED_DATA_SHARED_SIZE;
348+
KEEP(*(.shared*))
349+
__SharedLimit = .;
350+
} > ram
346351

352+
/* Check if Shared area overflowed within RAM */
353+
ASSERT(__SharedLimit == ORIGIN(ram) + LENGTH(ram), "Shared area overflowed within RAM")
347354

348355
/* Used for the digital signature of the secure application and the Bootloader SDK application.
349356
* The size of the section depends on the required data size. */

0 commit comments

Comments
 (0)