blob: 59ef1495a529ad07ffa4cc1e79bf75e6574b828b [file] [log] [blame]
Ashutosh Singhf4d88672017-11-29 13:35:43 +00001/*
Kevin Peng67a87542019-01-28 09:53:58 +08002 * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
Ashutosh Singhf4d88672017-11-29 13:35:43 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7#include <stdint.h>
8#include <stdbool.h>
9
Ashutosh Singhf4d88672017-11-29 13:35:43 +000010#include "cmsis_os2.h"
11
12#include "tfm_api.h"
Mate Toth-Pal1379e152018-07-30 17:38:29 +020013#include "tfm_ns_lock.h"
Ashutosh Singhf4d88672017-11-29 13:35:43 +000014
15/**
16 * \brief struct ns_lock_state type
17 */
18struct ns_lock_state
19{
20 bool init;
21 osMutexId_t id;
22};
23
24/**
25 * \brief ns_lock status
26 */
27static struct ns_lock_state ns_lock = {.init=false, .id=NULL};
28
29/**
30 * \brief Mutex properties, NS lock
31 */
32static const osMutexAttr_t ns_lock_attrib = {
33 .name = "ns_lock",
34 .attr_bits = osMutexPrioInherit
35};
36
37/**
Ashutosh Singhf4d88672017-11-29 13:35:43 +000038 * \brief NS world, NS lock based dispatcher
39 */
Mate Toth-Pal1379e152018-07-30 17:38:29 +020040uint32_t tfm_ns_lock_dispatch(veneer_fn fn,
41 uint32_t arg0, uint32_t arg1,
42 uint32_t arg2, uint32_t arg3)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000043{
44 uint32_t result;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000045
46 /* Check the NS lock has been initialized */
47 if (ns_lock.init == false) {
48 return TFM_ERROR_GENERIC;
49 }
50
Mate Toth-Pal1379e152018-07-30 17:38:29 +020051 /* TFM request protected by NS lock */
52 osMutexAcquire(ns_lock.id,osWaitForever);
Ashutosh Singhf4d88672017-11-29 13:35:43 +000053
Mate Toth-Pal1379e152018-07-30 17:38:29 +020054 result = fn(arg0, arg1, arg2, arg3);
Ashutosh Singhf4d88672017-11-29 13:35:43 +000055
Mate Toth-Pal1379e152018-07-30 17:38:29 +020056 osMutexRelease(ns_lock.id);
57
58 return result;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000059}
60
61/**
62 * \brief NS world, Init NS lock
63 */
64uint32_t tfm_ns_lock_init()
65{
66 if (ns_lock.init == false) {
67 ns_lock.id = osMutexNew(&ns_lock_attrib);
68 ns_lock.init = true;
69 return TFM_SUCCESS;
70 }
71 else {
72 return TFM_ERROR_GENERIC;
73 }
74}
Mate Toth-Pal1379e152018-07-30 17:38:29 +020075
76bool tfm_ns_lock_get_init_state()
77{
78 return ns_lock.init;
79}
80