blob: 7f4c685c5397d3d187962453dd4967ef7c5c0a82 [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"
Antonio de Angelis05b24192019-07-04 15:28:46 +010013#include "tfm_ns_interface.h"
14
15/**
16 * This file contains an example implementation of the NS interface APIs
17 * described in tfm_ns_interface.h
18 *
19 */
Ashutosh Singhf4d88672017-11-29 13:35:43 +000020
21/**
22 * \brief struct ns_lock_state type
23 */
Antonio de Angelisdf5817d2019-06-20 16:07:26 +010024struct ns_lock_state {
Ashutosh Singhf4d88672017-11-29 13:35:43 +000025 bool init;
26 osMutexId_t id;
27};
28
29/**
30 * \brief ns_lock status
31 */
Antonio de Angelis05b24192019-07-04 15:28:46 +010032static struct ns_lock_state ns_lock = {.init=false, .id=NULL};
Ashutosh Singhf4d88672017-11-29 13:35:43 +000033
34/**
35 * \brief Mutex properties, NS lock
36 */
37static const osMutexAttr_t ns_lock_attrib = {
38 .name = "ns_lock",
Hugues de Valon32547602019-02-19 14:46:56 +000039 .attr_bits = osMutexPrioInherit,
40 .cb_mem = NULL,
41 .cb_size = 0U
Ashutosh Singhf4d88672017-11-29 13:35:43 +000042};
43
Antonio de Angelis05b24192019-07-04 15:28:46 +010044__attribute__((weak))
45uint32_t tfm_ns_interface_dispatch(veneer_fn fn,
46 uint32_t arg0, uint32_t arg1,
47 uint32_t arg2, uint32_t arg3)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000048{
Hugues de Valonca95c482019-06-18 16:12:11 +010049 int32_t result;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000050
51 /* Check the NS lock has been initialized */
52 if (ns_lock.init == false) {
Hugues de Valonca95c482019-06-18 16:12:11 +010053 return (int32_t)TFM_ERROR_GENERIC;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000054 }
55
Mate Toth-Pal1379e152018-07-30 17:38:29 +020056 /* TFM request protected by NS lock */
Antonio de Angelisdf5817d2019-06-20 16:07:26 +010057 if (osMutexAcquire(ns_lock.id, osWaitForever) != osOK) {
Hugues de Valonca95c482019-06-18 16:12:11 +010058 return (int32_t)TFM_ERROR_GENERIC;
Hugues de Valon32547602019-02-19 14:46:56 +000059 }
Ashutosh Singhf4d88672017-11-29 13:35:43 +000060
Mate Toth-Pal1379e152018-07-30 17:38:29 +020061 result = fn(arg0, arg1, arg2, arg3);
Ashutosh Singhf4d88672017-11-29 13:35:43 +000062
Hugues de Valon32547602019-02-19 14:46:56 +000063 if (osMutexRelease(ns_lock.id) != osOK) {
Hugues de Valonca95c482019-06-18 16:12:11 +010064 return (int32_t)TFM_ERROR_GENERIC;
Hugues de Valon32547602019-02-19 14:46:56 +000065 }
Mate Toth-Pal1379e152018-07-30 17:38:29 +020066
67 return result;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000068}
69
Antonio de Angelis05b24192019-07-04 15:28:46 +010070__attribute__((weak))
71enum tfm_status_e tfm_ns_interface_init()
Ashutosh Singhf4d88672017-11-29 13:35:43 +000072{
73 if (ns_lock.init == false) {
74 ns_lock.id = osMutexNew(&ns_lock_attrib);
75 ns_lock.init = true;
76 return TFM_SUCCESS;
Antonio de Angelisdf5817d2019-06-20 16:07:26 +010077 } else {
Ashutosh Singhf4d88672017-11-29 13:35:43 +000078 return TFM_ERROR_GENERIC;
79 }
80}