Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 1 | /* |
Antonio de Angelis | d9cd66e | 2018-03-27 11:19:59 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, 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 | |
| 10 | #include "cmsis.h" |
| 11 | #include "cmsis_os2.h" |
| 12 | |
| 13 | #include "tfm_api.h" |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame^] | 14 | #include "tfm_ns_lock.h" |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * \brief struct ns_lock_state type |
| 18 | */ |
| 19 | struct ns_lock_state |
| 20 | { |
| 21 | bool init; |
| 22 | osMutexId_t id; |
| 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * \brief ns_lock status |
| 27 | */ |
| 28 | static struct ns_lock_state ns_lock = {.init=false, .id=NULL}; |
| 29 | |
| 30 | /** |
| 31 | * \brief Mutex properties, NS lock |
| 32 | */ |
| 33 | static const osMutexAttr_t ns_lock_attrib = { |
| 34 | .name = "ns_lock", |
| 35 | .attr_bits = osMutexPrioInherit |
| 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 */ |
| 53 | osMutexAcquire(ns_lock.id,osWaitForever); |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 54 | |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame^] | 55 | result = fn(arg0, arg1, arg2, arg3); |
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 | osMutexRelease(ns_lock.id); |
| 58 | |
| 59 | return result; |
Ashutosh Singh | f4d8867 | 2017-11-29 13:35:43 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * \brief NS world, Init NS lock |
| 64 | */ |
| 65 | uint32_t tfm_ns_lock_init() |
| 66 | { |
| 67 | if (ns_lock.init == false) { |
| 68 | ns_lock.id = osMutexNew(&ns_lock_attrib); |
| 69 | ns_lock.init = true; |
| 70 | return TFM_SUCCESS; |
| 71 | } |
| 72 | else { |
| 73 | return TFM_ERROR_GENERIC; |
| 74 | } |
| 75 | } |
Mate Toth-Pal | 1379e15 | 2018-07-30 17:38:29 +0200 | [diff] [blame^] | 76 | |
| 77 | bool tfm_ns_lock_get_init_state() |
| 78 | { |
| 79 | return ns_lock.init; |
| 80 | } |
| 81 | |