1 /*
2  * Copyright (c) 2019-2024, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10  * the Software, and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 /*
26  * This file contains the implementation of APIs which are defined in
27  * \interface/include/os_wrapper/mutex.h by TF-M(tag: TF-Mv2.0.0).
28  * The implementation is based on FreeRTOS mutex type semaphore.
29  */
30 
31 #include "os_wrapper/mutex.h"
32 
33 #include "FreeRTOS.h"
34 #include "semphr.h"
35 #include "mpu_wrappers.h"
36 
37 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
38 
39 /*
40  * In the static allocation, the RAM is required to hold the semaphore's
41  * state.
42  */
43     StaticSemaphore_t xSecureMutexBuffer;
44 #endif
45 
os_wrapper_mutex_create(void)46 void * os_wrapper_mutex_create( void )
47 {
48     SemaphoreHandle_t xMutexHandle = NULL;
49 
50     #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
51         xMutexHandle = xSemaphoreCreateMutex();
52     #elif ( configSUPPORT_STATIC_ALLOCATION == 1 )
53         xMutexHandle = xSemaphoreCreateMutexStatic( &xSecureMutexBuffer );
54     #endif
55     return ( void * ) xMutexHandle;
56 }
57 /*-----------------------------------------------------------*/
58 
os_wrapper_mutex_acquire(void * handle,uint32_t timeout)59 uint32_t os_wrapper_mutex_acquire( void * handle,
60                                    uint32_t timeout )
61 {
62     BaseType_t xRet;
63 
64     if( !handle )
65     {
66         return OS_WRAPPER_ERROR;
67     }
68 
69     xRet = xSemaphoreTake( ( SemaphoreHandle_t ) handle,
70                            ( timeout == OS_WRAPPER_WAIT_FOREVER ) ?
71                            portMAX_DELAY : ( TickType_t ) timeout );
72 
73     if( xRet != pdPASS )
74     {
75         return OS_WRAPPER_ERROR;
76     }
77     else
78     {
79         return OS_WRAPPER_SUCCESS;
80     }
81 }
82 /*-----------------------------------------------------------*/
83 
os_wrapper_mutex_release(void * handle)84 uint32_t os_wrapper_mutex_release( void * handle )
85 {
86     BaseType_t xRet;
87 
88     if( !handle )
89     {
90         return OS_WRAPPER_ERROR;
91     }
92 
93     xRet = xSemaphoreGive( ( SemaphoreHandle_t ) handle );
94 
95     if( xRet != pdPASS )
96     {
97         return OS_WRAPPER_ERROR;
98     }
99     else
100     {
101         return OS_WRAPPER_SUCCESS;
102     }
103 }
104 /*-----------------------------------------------------------*/
105 
os_wrapper_mutex_delete(void * handle)106 uint32_t os_wrapper_mutex_delete( void * handle )
107 {
108     vSemaphoreDelete( ( SemaphoreHandle_t ) handle );
109 
110     return OS_WRAPPER_SUCCESS;
111 }
112 /*-----------------------------------------------------------*/
113