1 /*
2 * Copyright (c) 2020-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 "linux/spinlock.h"
33 #include "hdf_log.h"
34 #include "osal_mem.h"
35
36 #define HDF_LOG_TAG osal_spinlock
37
OsalSpinInit(OsalSpinlock * spinlock)38 int32_t OsalSpinInit(OsalSpinlock *spinlock)
39 {
40 spinlock_t *spin = NULL;
41
42 if (spinlock == NULL) {
43 HDF_LOGE("%s invalid param", __func__);
44 return HDF_ERR_INVALID_PARAM;
45 }
46
47 spin = (spinlock_t *)OsalMemCalloc(sizeof(*spin));
48 if (spin == NULL) {
49 HDF_LOGE("%s malloc fail", __func__);
50 spinlock->realSpinlock = NULL;
51 return HDF_ERR_MALLOC_FAIL;
52 }
53
54 spin_lock_init(spin);
55 spinlock->realSpinlock = spin;
56
57 return HDF_SUCCESS;
58 }
59
OsalSpinDestroy(OsalSpinlock * spinlock)60 int32_t OsalSpinDestroy(OsalSpinlock *spinlock)
61 {
62 if (spinlock == NULL || spinlock->realSpinlock == NULL) {
63 HDF_LOGE("%s invalid param", __func__);
64 return HDF_ERR_INVALID_PARAM;
65 }
66
67 OsalMemFree(spinlock->realSpinlock);
68 spinlock->realSpinlock = NULL;
69
70 return HDF_SUCCESS;
71 }
72
OsalSpinLock(OsalSpinlock * spinlock)73 int32_t OsalSpinLock(OsalSpinlock *spinlock)
74 {
75 if (spinlock == NULL || spinlock->realSpinlock == NULL) {
76 HDF_LOGE("%s invalid param", __func__);
77 return HDF_ERR_INVALID_PARAM;
78 }
79
80 spin_lock((spinlock_t *)spinlock->realSpinlock);
81
82 return HDF_SUCCESS;
83 }
84
OsalSpinUnlock(OsalSpinlock * spinlock)85 int32_t OsalSpinUnlock(OsalSpinlock *spinlock)
86 {
87 if (spinlock == NULL || spinlock->realSpinlock == NULL) {
88 HDF_LOGE("%s invalid param", __func__);
89 return HDF_ERR_INVALID_PARAM;
90 }
91
92 spin_unlock((spinlock_t *)spinlock->realSpinlock);
93
94 return HDF_SUCCESS;
95 }
96
OsalSpinLockIrq(OsalSpinlock * spinlock)97 int32_t OsalSpinLockIrq(OsalSpinlock *spinlock)
98 {
99 #ifdef LOSCFG_KERNEL_SMP
100 if (spinlock == NULL || spinlock->realSpinlock == NULL) {
101 HDF_LOGE("%s invalid param %d", __func__, __LINE__);
102 return HDF_ERR_INVALID_PARAM;
103 }
104
105 LOS_IntLock();
106 LOS_SpinLock((spinlock_t *)spinlock->realSpinlock);
107 #else
108 (void)spinlock;
109 LOS_IntLock();
110 #endif
111 return HDF_SUCCESS;
112 }
113
OsalSpinUnlockIrq(OsalSpinlock * spinlock)114 int32_t OsalSpinUnlockIrq(OsalSpinlock *spinlock)
115 {
116 #ifdef LOSCFG_KERNEL_SMP
117 if (spinlock == NULL || spinlock->realSpinlock == NULL) {
118 HDF_LOGE("%s invalid param %d", __func__, __LINE__);
119 return HDF_ERR_INVALID_PARAM;
120 }
121
122 LOS_SpinUnlock((spinlock_t *)spinlock->realSpinlock);
123 LOS_IntUnLock();
124 #else
125 (void)spinlock;
126 LOS_IntUnLock();
127 #endif
128
129 return HDF_SUCCESS;
130 }
131
OsalSpinLockIrqSave(OsalSpinlock * spinlock,uint32_t * flags)132 int32_t OsalSpinLockIrqSave(OsalSpinlock *spinlock, uint32_t *flags)
133 {
134 uint32_t temp = 0;
135 if (spinlock == NULL || spinlock->realSpinlock == NULL || flags == NULL) {
136 HDF_LOGE("%s invalid param %d", __func__, __LINE__);
137 return HDF_ERR_INVALID_PARAM;
138 }
139 spin_lock_irqsave((spinlock_t *)spinlock->realSpinlock, temp);
140 *flags = temp;
141
142 return HDF_SUCCESS;
143 }
144
OsalSpinUnlockIrqRestore(OsalSpinlock * spinlock,uint32_t * flags)145 int32_t OsalSpinUnlockIrqRestore(OsalSpinlock *spinlock, uint32_t *flags)
146 {
147 if (spinlock == NULL || spinlock->realSpinlock == NULL || flags == NULL) {
148 HDF_LOGE("%s invalid param %d", __func__, __LINE__);
149 return HDF_ERR_INVALID_PARAM;
150 }
151 spin_unlock_irqrestore((spinlock_t *)spinlock->realSpinlock, *flags);
152
153 return HDF_SUCCESS;
154 }
155
156