blob: a81f6e1d8cf0c4149727c23a3cc7a71e622e126f [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 */
Antonio de Angelisdf5817d2019-06-20 16:07:26 +010018struct ns_lock_state {
Ashutosh Singhf4d88672017-11-29 13:35:43 +000019 bool init;
20 osMutexId_t id;
21};
22
23/**
24 * \brief ns_lock status
25 */
Antonio de Angelisdf5817d2019-06-20 16:07:26 +010026static struct ns_lock_state ns_lock = {.init = false, .id = NULL};
Ashutosh Singhf4d88672017-11-29 13:35:43 +000027
28/**
29 * \brief Mutex properties, NS lock
30 */
31static const osMutexAttr_t ns_lock_attrib = {
32 .name = "ns_lock",
Hugues de Valon32547602019-02-19 14:46:56 +000033 .attr_bits = osMutexPrioInherit,
34 .cb_mem = NULL,
35 .cb_size = 0U
Ashutosh Singhf4d88672017-11-29 13:35:43 +000036};
37
38/**
Ashutosh Singhf4d88672017-11-29 13:35:43 +000039 * \brief NS world, NS lock based dispatcher
40 */
Mate Toth-Pal1379e152018-07-30 17:38:29 +020041uint32_t tfm_ns_lock_dispatch(veneer_fn fn,
42 uint32_t arg0, uint32_t arg1,
43 uint32_t arg2, uint32_t arg3)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000044{
45 uint32_t result;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000046
47 /* Check the NS lock has been initialized */
48 if (ns_lock.init == false) {
49 return TFM_ERROR_GENERIC;
50 }
51
Mate Toth-Pal1379e152018-07-30 17:38:29 +020052 /* TFM request protected by NS lock */
Antonio de Angelisdf5817d2019-06-20 16:07:26 +010053 if (osMutexAcquire(ns_lock.id, osWaitForever) != osOK) {
Hugues de Valon32547602019-02-19 14:46:56 +000054 return TFM_ERROR_GENERIC;
55 }
Ashutosh Singhf4d88672017-11-29 13:35:43 +000056
Mate Toth-Pal1379e152018-07-30 17:38:29 +020057 result = fn(arg0, arg1, arg2, arg3);
Ashutosh Singhf4d88672017-11-29 13:35:43 +000058
Hugues de Valon32547602019-02-19 14:46:56 +000059 if (osMutexRelease(ns_lock.id) != osOK) {
60 return TFM_ERROR_GENERIC;
61 }
Mate Toth-Pal1379e152018-07-30 17:38:29 +020062
63 return result;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000064}
65
66/**
67 * \brief NS world, Init NS lock
68 */
Antonio de Angelisdf5817d2019-06-20 16:07:26 +010069enum tfm_status_e tfm_ns_lock_init(void)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000070{
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 Angelisdf5817d2019-06-20 16:07:26 +010075 } else {
Ashutosh Singhf4d88672017-11-29 13:35:43 +000076 return TFM_ERROR_GENERIC;
77 }
78}