1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "osal_spinlock.h"
32 #include "hdf_log.h"
33 #include "los_mux.h"
34
35 #define HDF_LOG_TAG osal_spinlock
36
37 #define HDF_INVALID_MUX_ID UINT32_MAX
38
OsalSpinInit(OsalSpinlock * spinlock)39 int32_t OsalSpinInit(OsalSpinlock *spinlock)
40 {
41 uint32_t ret;
42 uint32_t muxId = 0;
43
44 if (spinlock == NULL) {
45 HDF_LOGE("%s invalid param", __func__);
46 return HDF_ERR_INVALID_PARAM;
47 }
48
49 ret = LOS_MuxCreate(&muxId);
50 if (ret == LOS_OK) {
51 spinlock->realSpinlock = (void *)(uintptr_t)muxId;
52 } else {
53 spinlock->realSpinlock = (void *)(uintptr_t)HDF_INVALID_MUX_ID;
54 HDF_LOGE("%s create fail %u %d", __func__, ret, __LINE__);
55 return HDF_FAILURE;
56 }
57
58 return HDF_SUCCESS;
59 }
60
OsalSpinDestroy(OsalSpinlock * spinlock)61 int32_t OsalSpinDestroy(OsalSpinlock *spinlock)
62 {
63 uint32_t ret;
64
65 if (spinlock == NULL || spinlock->realSpinlock == (void *)(uintptr_t)HDF_INVALID_MUX_ID) {
66 HDF_LOGE("%s invalid param", __func__);
67 return HDF_ERR_INVALID_PARAM;
68 }
69
70 ret = LOS_MuxDelete((uint32_t)(uintptr_t)spinlock->realSpinlock);
71 if (ret != LOS_OK) {
72 HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__);
73 return HDF_FAILURE;
74 }
75
76 spinlock->realSpinlock = (void *)(uintptr_t)HDF_INVALID_MUX_ID;
77 return HDF_SUCCESS;
78 }
79
OsalSpinLock(OsalSpinlock * spinlock)80 int32_t OsalSpinLock(OsalSpinlock *spinlock)
81 {
82 uint32_t ret;
83
84 if (spinlock == NULL || spinlock->realSpinlock == (void *)(uintptr_t)HDF_INVALID_MUX_ID) {
85 HDF_LOGE("%s invalid param", __func__);
86 return HDF_ERR_INVALID_PARAM;
87 }
88
89 ret = LOS_MuxPend((uint32_t)(uintptr_t)spinlock->realSpinlock, LOS_MS2Tick(HDF_WAIT_FOREVER));
90 if (ret != LOS_OK) {
91 HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__);
92 return HDF_FAILURE;
93 }
94
95 return HDF_SUCCESS;
96 }
97
OsalSpinUnlock(OsalSpinlock * spinlock)98 int32_t OsalSpinUnlock(OsalSpinlock *spinlock)
99 {
100 uint32_t ret;
101
102 if (spinlock == NULL || spinlock->realSpinlock == (void *)(uintptr_t)HDF_INVALID_MUX_ID) {
103 HDF_LOGE("%s invalid param", __func__);
104 return HDF_ERR_INVALID_PARAM;
105 }
106
107 ret = LOS_MuxPost((uint32_t)(uintptr_t)spinlock->realSpinlock);
108 if (ret != LOS_OK) {
109 HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__);
110 return HDF_FAILURE;
111 }
112
113 return HDF_SUCCESS;
114 }
115
OsalSpinLockIrq(OsalSpinlock * spinlock)116 int32_t OsalSpinLockIrq(OsalSpinlock *spinlock)
117 {
118 return OsalSpinLock(spinlock);
119 }
120
OsalSpinUnlockIrq(OsalSpinlock * spinlock)121 int32_t OsalSpinUnlockIrq(OsalSpinlock *spinlock)
122 {
123 return OsalSpinUnlock(spinlock);
124 }
125
OsalSpinLockIrqSave(OsalSpinlock * spinlock,uint32_t * flags)126 int32_t OsalSpinLockIrqSave(OsalSpinlock *spinlock, uint32_t *flags)
127 {
128 (void)flags;
129 return OsalSpinLock(spinlock);
130 }
131
OsalSpinUnlockIrqRestore(OsalSpinlock * spinlock,uint32_t * flags)132 int32_t OsalSpinUnlockIrqRestore(OsalSpinlock *spinlock, uint32_t *flags)
133 {
134 (void)flags;
135 return OsalSpinUnlock(spinlock);
136 }
137