1 /*
2 * Copyright (c) 2019-2020, 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 * os_wrapper/mutex.h by TF-M(tag: TF-Mv1.1). The implementation is based
28 * 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 * In the static allocation, the RAM is required to hold the semaphore's
40 * state.
41 */
42 StaticSemaphore_t xSecureMutexBuffer;
43 #endif
44
os_wrapper_mutex_create(void)45 void * os_wrapper_mutex_create( void )
46 {
47 SemaphoreHandle_t xMutexHandle = NULL;
48
49 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
50 xMutexHandle = xSemaphoreCreateMutex();
51 #elif( configSUPPORT_STATIC_ALLOCATION == 1 )
52 xMutexHandle = xSemaphoreCreateMutexStatic( &xSecureMutexBuffer );
53 #endif
54 return ( void * ) xMutexHandle;
55 }
56 /*-----------------------------------------------------------*/
57
os_wrapper_mutex_acquire(void * handle,uint32_t timeout)58 uint32_t os_wrapper_mutex_acquire( void * handle, uint32_t timeout )
59 {
60 BaseType_t xRet;
61
62 if( ! handle )
63 return OS_WRAPPER_ERROR;
64
65 xRet = xSemaphoreTake( ( SemaphoreHandle_t ) handle,
66 ( timeout == OS_WRAPPER_WAIT_FOREVER ) ?
67 portMAX_DELAY : ( TickType_t ) timeout );
68
69 if( xRet != pdPASS )
70 return OS_WRAPPER_ERROR;
71 else
72 return OS_WRAPPER_SUCCESS;
73 }
74 /*-----------------------------------------------------------*/
75
os_wrapper_mutex_release(void * handle)76 uint32_t os_wrapper_mutex_release( void * handle )
77 {
78 BaseType_t xRet;
79
80 if( !handle )
81 return OS_WRAPPER_ERROR;
82
83 xRet = xSemaphoreGive( ( SemaphoreHandle_t ) handle );
84
85 if( xRet != pdPASS )
86 return OS_WRAPPER_ERROR;
87 else
88 return OS_WRAPPER_SUCCESS;
89 }
90 /*-----------------------------------------------------------*/
91
os_wrapper_mutex_delete(void * handle)92 uint32_t os_wrapper_mutex_delete( void * handle )
93 {
94 vSemaphoreDelete( ( SemaphoreHandle_t ) handle );
95
96 return OS_WRAPPER_SUCCESS;
97 }
98 /*-----------------------------------------------------------*/
99