Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 1 | /* |
Kevin Peng | 67a8754 | 2019-01-28 09:53:58 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, Arm Limited. All rights reserved. |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | #include <stdint.h> |
| 8 | #include <stdbool.h> |
| 9 | |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 10 | #include "cmsis_os2.h" |
| 11 | |
| 12 | #include "tfm_api.h" |
Antonio de Angelis | 05b2419 | 2019-07-04 15:28:46 +0100 | [diff] [blame^] | 13 | #include "tfm_ns_interface.h" |
| 14 | |
| 15 | /** |
| 16 | * This file contains an example implementation of the NS interface APIs |
| 17 | * described in tfm_ns_interface.h |
| 18 | * |
| 19 | */ |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * \brief struct ns_lock_state type |
| 23 | */ |
Antonio de Angelis | df5817d | 2019-06-20 16:07:26 +0100 | [diff] [blame] | 24 | struct ns_lock_state { |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 25 | bool init; |
| 26 | osMutexId_t id; |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * \brief ns_lock status |
| 31 | */ |
Antonio de Angelis | 05b2419 | 2019-07-04 15:28:46 +0100 | [diff] [blame^] | 32 | static struct ns_lock_state ns_lock = {.init=false, .id=NULL}; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * \brief Mutex properties, NS lock |
| 36 | */ |
| 37 | static const osMutexAttr_t ns_lock_attrib = { |
| 38 | .name = "ns_lock", |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame] | 39 | .attr_bits = osMutexPrioInherit, |
| 40 | .cb_mem = NULL, |
| 41 | .cb_size = 0U |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Antonio de Angelis | 05b2419 | 2019-07-04 15:28:46 +0100 | [diff] [blame^] | 44 | __attribute__((weak)) |
| 45 | uint32_t tfm_ns_interface_dispatch(veneer_fn fn, |
| 46 | uint32_t arg0, uint32_t arg1, |
| 47 | uint32_t arg2, uint32_t arg3) |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 48 | { |
Hugues de Valon | ca95c48 | 2019-06-18 16:12:11 +0100 | [diff] [blame] | 49 | int32_t result; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 50 | |
| 51 | /* Check the NS lock has been initialized */ |
| 52 | if (ns_lock.init == false) { |
Hugues de Valon | ca95c48 | 2019-06-18 16:12:11 +0100 | [diff] [blame] | 53 | return (int32_t)TFM_ERROR_GENERIC; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 56 | /* TFM request protected by NS lock */ |
Antonio de Angelis | df5817d | 2019-06-20 16:07:26 +0100 | [diff] [blame] | 57 | if (osMutexAcquire(ns_lock.id, osWaitForever) != osOK) { |
Hugues de Valon | ca95c48 | 2019-06-18 16:12:11 +0100 | [diff] [blame] | 58 | return (int32_t)TFM_ERROR_GENERIC; |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame] | 59 | } |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 60 | |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 61 | result = fn(arg0, arg1, arg2, arg3); |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 62 | |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame] | 63 | if (osMutexRelease(ns_lock.id) != osOK) { |
Hugues de Valon | ca95c48 | 2019-06-18 16:12:11 +0100 | [diff] [blame] | 64 | return (int32_t)TFM_ERROR_GENERIC; |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame] | 65 | } |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 66 | |
| 67 | return result; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Antonio de Angelis | 05b2419 | 2019-07-04 15:28:46 +0100 | [diff] [blame^] | 70 | __attribute__((weak)) |
| 71 | enum tfm_status_e tfm_ns_interface_init() |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 72 | { |
| 73 | if (ns_lock.init == false) { |
| 74 | ns_lock.id = osMutexNew(&ns_lock_attrib); |
| 75 | ns_lock.init = true; |
| 76 | return TFM_SUCCESS; |
Antonio de Angelis | df5817d | 2019-06-20 16:07:26 +0100 | [diff] [blame] | 77 | } else { |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 78 | return TFM_ERROR_GENERIC; |
| 79 | } |
| 80 | } |