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" |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 13 | #include "tfm_ns_lock.h" |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * \brief struct ns_lock_state type |
| 17 | */ |
Antonio de Angelis | df5817d | 2019-06-20 16:07:26 +0100 | [diff] [blame^] | 18 | struct ns_lock_state { |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 19 | bool init; |
| 20 | osMutexId_t id; |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * \brief ns_lock status |
| 25 | */ |
Antonio de Angelis | df5817d | 2019-06-20 16:07:26 +0100 | [diff] [blame^] | 26 | static struct ns_lock_state ns_lock = {.init = false, .id = NULL}; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * \brief Mutex properties, NS lock |
| 30 | */ |
| 31 | static const osMutexAttr_t ns_lock_attrib = { |
| 32 | .name = "ns_lock", |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame] | 33 | .attr_bits = osMutexPrioInherit, |
| 34 | .cb_mem = NULL, |
| 35 | .cb_size = 0U |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /** |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 39 | * \brief NS world, NS lock based dispatcher |
| 40 | */ |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 41 | uint32_t tfm_ns_lock_dispatch(veneer_fn fn, |
| 42 | uint32_t arg0, uint32_t arg1, |
| 43 | uint32_t arg2, uint32_t arg3) |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 44 | { |
| 45 | uint32_t result; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 46 | |
| 47 | /* Check the NS lock has been initialized */ |
| 48 | if (ns_lock.init == false) { |
| 49 | return TFM_ERROR_GENERIC; |
| 50 | } |
| 51 | |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 52 | /* TFM request protected by NS lock */ |
Antonio de Angelis | df5817d | 2019-06-20 16:07:26 +0100 | [diff] [blame^] | 53 | if (osMutexAcquire(ns_lock.id, osWaitForever) != osOK) { |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame] | 54 | return TFM_ERROR_GENERIC; |
| 55 | } |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 56 | |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 57 | result = fn(arg0, arg1, arg2, arg3); |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 58 | |
Hugues de Valon | 3254760 | 2019-02-19 14:46:56 +0000 | [diff] [blame] | 59 | if (osMutexRelease(ns_lock.id) != osOK) { |
| 60 | return TFM_ERROR_GENERIC; |
| 61 | } |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame] | 62 | |
| 63 | return result; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * \brief NS world, Init NS lock |
| 68 | */ |
Antonio de Angelis | df5817d | 2019-06-20 16:07:26 +0100 | [diff] [blame^] | 69 | enum tfm_status_e tfm_ns_lock_init(void) |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 70 | { |
| 71 | if (ns_lock.init == false) { |
| 72 | ns_lock.id = osMutexNew(&ns_lock_attrib); |
| 73 | ns_lock.init = true; |
| 74 | return TFM_SUCCESS; |
Antonio de Angelis | df5817d | 2019-06-20 16:07:26 +0100 | [diff] [blame^] | 75 | } else { |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 76 | return TFM_ERROR_GENERIC; |
| 77 | } |
| 78 | } |