|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2018, Arm Limited. All rights reserved. |
| 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 "rtx_os.h" |
| 12 | +#include "cmsis_os2.h" |
| 13 | + |
| 14 | +#include "tfm_api.h" |
| 15 | +#include "tfm_ns_lock.h" |
| 16 | + |
| 17 | +/** |
| 18 | + * \brief struct ns_lock_state type |
| 19 | + */ |
| 20 | +struct ns_lock_state |
| 21 | +{ |
| 22 | + bool init; |
| 23 | + osMutexId_t id; |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * \brief ns_lock status |
| 28 | + */ |
| 29 | +static struct ns_lock_state ns_lock = {.init=false, .id=NULL}; |
| 30 | + |
| 31 | +/** |
| 32 | + * \brief Mutex properties, NS lock |
| 33 | + */ |
| 34 | + |
| 35 | +static osRtxMutex_t ns_lock_cb = { 0 }; |
| 36 | + |
| 37 | +static const osMutexAttr_t ns_lock_attrib = { |
| 38 | + .name = "ns_lock", |
| 39 | + .attr_bits = osMutexPrioInherit, |
| 40 | + .cb_mem = &ns_lock_cb, |
| 41 | + .cb_size = sizeof(ns_lock_cb) |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * \brief NS world, NS lock based dispatcher |
| 46 | + */ |
| 47 | +uint32_t tfm_ns_lock_dispatch(veneer_fn fn, |
| 48 | + uint32_t arg0, uint32_t arg1, |
| 49 | + uint32_t arg2, uint32_t arg3) |
| 50 | +{ |
| 51 | + uint32_t result; |
| 52 | + |
| 53 | + /* Check the NS lock has been initialized */ |
| 54 | + if (ns_lock.init == false) { |
| 55 | + return TFM_ERROR_GENERIC; |
| 56 | + } |
| 57 | + |
| 58 | + /* TFM request protected by NS lock */ |
| 59 | + osMutexAcquire(ns_lock.id,osWaitForever); |
| 60 | + |
| 61 | + result = fn(arg0, arg1, arg2, arg3); |
| 62 | + |
| 63 | + osMutexRelease(ns_lock.id); |
| 64 | + |
| 65 | + return result; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * \brief NS world, Init NS lock |
| 70 | + */ |
| 71 | +uint32_t tfm_ns_lock_init() |
| 72 | +{ |
| 73 | + if (ns_lock.init == false) { |
| 74 | + ns_lock.id = osMutexNew(&ns_lock_attrib); |
| 75 | + ns_lock.init = true; |
| 76 | + return TFM_SUCCESS; |
| 77 | + } |
| 78 | + else { |
| 79 | + return TFM_ERROR_GENERIC; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +bool tfm_ns_lock_get_init_state() |
| 84 | +{ |
| 85 | + return ns_lock.init; |
| 86 | +} |
| 87 | + |
0 commit comments