From 9b7ef82b409c7b2019191e93f40d4276593fe45c Mon Sep 17 00:00:00 2001 From: Vikas Katariya Date: Wed, 25 Mar 2020 17:29:54 +0000 Subject: [PATCH] psa: Init IPC, Mailbox and Non-secure Interface These changes are for TFM_DUALCPU and TFM_V8M platforms. The initialization happens after mbed-os kernel is kicked off and when the thread is up and running. We are initializing the following for TFM_DUALCPU platforms: * IPC Interrupts for syncing multi-core platforms. * NS Mailbox to receive messages. * NS interface. We are only initializing Non-secure interface for TFM_V8M platforms. mbed_tfm_init() in tfm_mbed_boot.c overrides the WEAK mbed_tfm_init() for a specific platform. Signed-off-by: Vikas Katariya --- .../TARGET_TFM_DUALCPU/src/tfm_mbed_boot.c | 67 +++++++++++++++++++ .../TARGET_TFM_V8M/src/tfm_mbed_boot.c | 37 ++++++++++ rtos/source/TARGET_CORTEX/mbed_boot.c | 8 ++- rtos/source/TARGET_CORTEX/mbed_boot.h | 18 ++++- 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 features/FEATURE_PSA/TARGET_TFM/TARGET_TFM_DUALCPU/src/tfm_mbed_boot.c create mode 100644 features/FEATURE_PSA/TARGET_TFM/TARGET_TFM_V8M/src/tfm_mbed_boot.c diff --git a/features/FEATURE_PSA/TARGET_TFM/TARGET_TFM_DUALCPU/src/tfm_mbed_boot.c b/features/FEATURE_PSA/TARGET_TFM/TARGET_TFM_DUALCPU/src/tfm_mbed_boot.c new file mode 100644 index 0000000000..c8d595fe73 --- /dev/null +++ b/features/FEATURE_PSA/TARGET_TFM/TARGET_TFM_DUALCPU/src/tfm_mbed_boot.c @@ -0,0 +1,67 @@ +/* mbed Microcontroller Library + * Copyright (c) 2020 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed_error.h" +#include "tfm_multi_core_api.h" +#include "tfm_ns_mailbox.h" +#include "platform_multicore.h" +#include "tfm_ns_interface.h" + +static struct ns_mailbox_queue_t ns_mailbox_queue; + +void mbed_tfm_init(void) +{ + /* + * In case the of dual CPU, we need to initialize IPC, mailbox + * and NS interface after the RTOS has started to enable + * communication from Secure and Non-Secure cores. + */ + int32_t ret; + + ret = tfm_ns_wait_for_s_cpu_ready(); + /* + * Normally would expect "TFM_SUCCESS" returned here by TF-M, as this + * isn't a mailbox function. There may be some platforms other than PSoC6, + * which doesn't require tfm_ns_wait_for_s_cpu_ready() implementation. + * "PLATFORM_MAILBOX_SUCCESS" is a low-level error code and should be + * replaced by "TFM_SUCCESS". + * As the function definition has been imported from the TF-M, therefore + * a issue has been raised - https://developer.trustedfirmware.org/T660 + */ + if (ret != PLATFORM_MAILBOX_SUCCESS) { + /* Avoid undefined behavior after multi-core sync-up failed */ + MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, + MBED_ERROR_CODE_INITIALIZATION_FAILED), + "Failed to sync-up multi-core"); + } + + ret = tfm_ns_mailbox_init(&ns_mailbox_queue); + if (ret != MAILBOX_SUCCESS) { + /* Avoid undefined behavior after NS mailbox initialization failed */ + MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, + MBED_ERROR_CODE_INITIALIZATION_FAILED), + "Failed to initialize NS mailbox"); + } + + ret = tfm_ns_interface_init(); + if (ret != TFM_SUCCESS) { + /* Avoid undefined behavior after NS interface initialization failed */ + MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, + MBED_ERROR_CODE_INITIALIZATION_FAILED), + "Failed to initialize NS interface"); + } +} diff --git a/features/FEATURE_PSA/TARGET_TFM/TARGET_TFM_V8M/src/tfm_mbed_boot.c b/features/FEATURE_PSA/TARGET_TFM/TARGET_TFM_V8M/src/tfm_mbed_boot.c new file mode 100644 index 0000000000..87bcbbbf66 --- /dev/null +++ b/features/FEATURE_PSA/TARGET_TFM/TARGET_TFM_V8M/src/tfm_mbed_boot.c @@ -0,0 +1,37 @@ +/* mbed Microcontroller Library + * Copyright (c) 2020 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed_error.h" +#include "tfm_ns_interface.h" + +void mbed_tfm_init(void) +{ + /* + * In case of V8-M, we need to initialize NS interface + * after the RTOS has started to enable + * communication from Secure and Non-Secure cores. + */ + int32_t ret; + + ret = tfm_ns_interface_init(); + if (ret != TFM_SUCCESS) { + /* Avoid undefined behavior after NS interface initialization failed */ + MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, + MBED_ERROR_CODE_INITIALIZATION_FAILED), + "Failed to initialize NS interface"); + } +} diff --git a/rtos/source/TARGET_CORTEX/mbed_boot.c b/rtos/source/TARGET_CORTEX/mbed_boot.c index d63e12c014..18f2410bad 100644 --- a/rtos/source/TARGET_CORTEX/mbed_boot.c +++ b/rtos/source/TARGET_CORTEX/mbed_boot.c @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2006-2016 ARM Limited + * Copyright (c) 2006-2020 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -95,6 +95,7 @@ void mbed_init(void) void mbed_start(void) { mbed_toolchain_init(); + mbed_tfm_init(); mbed_main(); mbed_error_initialize(); main(); @@ -110,6 +111,11 @@ MBED_WEAK void software_init_hook_rtos() // Nothing by default } +MBED_WEAK void mbed_tfm_init(void) +{ + // Nothing by default +} + MBED_WEAK void mbed_main(void) { // Nothing by default diff --git a/rtos/source/TARGET_CORTEX/mbed_boot.h b/rtos/source/TARGET_CORTEX/mbed_boot.h index e7be74f1ea..c963706717 100644 --- a/rtos/source/TARGET_CORTEX/mbed_boot.h +++ b/rtos/source/TARGET_CORTEX/mbed_boot.h @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2018-2019 ARM Limited + * Copyright (c) 2018-2020 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -47,6 +47,7 @@ extern "C" { * - Start scheduler * - main thread calls start mbed * 4. Start mbed + * - Call mbed_tfm_init * - Call mbed_main * - Call main * @@ -149,6 +150,20 @@ void mbed_toolchain_init(void); */ void mbed_sdk_init(void); +/** + * TF-M specific application hook for running code before mbed_main + * + * This is a weak function which can be overridden by an application + * to allow code to run before mbed_main is called. + * Some TF-M supported platforms require synchronization between + * Secure and Non-Secure cores, therefore these tasks are done here. + * + * Preconditions: + * - The RTOS has been started by a call to mbed_rtos_start + * - The toolchain has been initialized by a call to mbed_toolchain_init + */ +void mbed_tfm_init(void); + /** * Application hook for running code before main * @@ -157,6 +172,7 @@ void mbed_sdk_init(void); * * Preconditions: * - The RTOS has been started by a call to mbed_rtos_start + * - TFM support has been initialized by a call to mbed_tfm_init. * - The toolchain has been initialized by a call to mbed_toolchain_init */ void mbed_main(void);