1 /*
2  * osal_spinlock.c
3  *
4  * osal driver
5  *
6  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  */
18 
19 #include "osal_spinlock.h"
20 #include <linux/export.h>
21 #include <linux/spinlock.h>
22 #include "hdf_log.h"
23 #include "osal_mem.h"
24 
25 #define HDF_LOG_TAG osal_spinlock
26 
OsalSpinInit(OsalSpinlock * spinlock)27 int32_t OsalSpinInit(OsalSpinlock *spinlock)
28 {
29 	spinlock_t *spin_tmp = NULL;
30 
31 	if (spinlock == NULL) {
32 		HDF_LOGE("%s invalid param", __func__);
33 		return HDF_ERR_INVALID_PARAM;
34 	}
35 
36 	spin_tmp = (spinlock_t *)OsalMemCalloc(sizeof(*spin_tmp));
37 	if (spin_tmp == NULL) {
38 		HDF_LOGE("malloc fail");
39 		spinlock->realSpinlock = NULL;
40 		return HDF_ERR_MALLOC_FAIL;
41 	}
42 	spin_lock_init(spin_tmp);
43 	spinlock->realSpinlock = (void *)spin_tmp;
44 
45 	return HDF_SUCCESS;
46 }
47 EXPORT_SYMBOL(OsalSpinInit);
48 
OsalSpinDestroy(OsalSpinlock * spinlock)49 int32_t OsalSpinDestroy(OsalSpinlock *spinlock)
50 {
51 	if (spinlock == NULL || spinlock->realSpinlock == NULL) {
52 		HDF_LOGE("%s invalid param", __func__);
53 		return HDF_ERR_INVALID_PARAM;
54 	}
55 
56 	OsalMemFree(spinlock->realSpinlock);
57 	spinlock->realSpinlock = NULL;
58 
59 	return HDF_SUCCESS;
60 }
61 EXPORT_SYMBOL(OsalSpinDestroy);
62 
OsalSpinLock(OsalSpinlock * spinlock)63 int32_t OsalSpinLock(OsalSpinlock *spinlock)
64 {
65 	if (spinlock == NULL || spinlock->realSpinlock == NULL) {
66 		HDF_LOGE("%s invalid param", __func__);
67 		return HDF_ERR_INVALID_PARAM;
68 	}
69 
70 	spin_lock((spinlock_t *)spinlock->realSpinlock);
71 
72 	return HDF_SUCCESS;
73 }
74 EXPORT_SYMBOL(OsalSpinLock);
75 
OsalSpinUnlock(OsalSpinlock * spinlock)76 int32_t OsalSpinUnlock(OsalSpinlock *spinlock)
77 {
78 	if (spinlock == NULL || spinlock->realSpinlock == NULL) {
79 		HDF_LOGE("%s invalid param", __func__);
80 		return HDF_ERR_INVALID_PARAM;
81 	}
82 
83 	spin_unlock((spinlock_t *)spinlock->realSpinlock);
84 
85 	return HDF_SUCCESS;
86 }
87 EXPORT_SYMBOL(OsalSpinUnlock);
88 
OsalSpinLockIrq(OsalSpinlock * spinlock)89 int32_t OsalSpinLockIrq(OsalSpinlock *spinlock)
90 {
91 	if (spinlock == NULL || spinlock->realSpinlock == NULL) {
92 		HDF_LOGE("%s invalid param", __func__);
93 		return HDF_ERR_INVALID_PARAM;
94 	}
95 
96 	spin_lock_irq((spinlock_t *)spinlock->realSpinlock);
97 
98 	return HDF_SUCCESS;
99 }
100 EXPORT_SYMBOL(OsalSpinLockIrq);
101 
OsalSpinUnlockIrq(OsalSpinlock * spinlock)102 int32_t OsalSpinUnlockIrq(OsalSpinlock *spinlock)
103 {
104 	if (spinlock == NULL || spinlock->realSpinlock == NULL) {
105 		HDF_LOGE("%s invalid param", __func__);
106 		return HDF_ERR_INVALID_PARAM;
107 	}
108 
109 	spin_unlock_irq((spinlock_t *)spinlock->realSpinlock);
110 
111 	return HDF_SUCCESS;
112 }
113 EXPORT_SYMBOL(OsalSpinUnlockIrq);
114 
OsalSpinLockIrqSave(OsalSpinlock * spinlock,uint32_t * flags)115 int32_t OsalSpinLockIrqSave(OsalSpinlock *spinlock, uint32_t *flags)
116 {
117 	unsigned long temp = 0;
118 
119 	if (spinlock == NULL || spinlock->realSpinlock == NULL || flags == NULL) {
120 		HDF_LOGE("%s invalid param %d", __func__, __LINE__);
121 		return HDF_ERR_INVALID_PARAM;
122 	}
123 
124 	spin_lock_irqsave((spinlock_t *)spinlock->realSpinlock, temp);
125 	*flags = temp;
126 
127 	return HDF_SUCCESS;
128 }
129 EXPORT_SYMBOL(OsalSpinLockIrqSave);
130 
OsalSpinUnlockIrqRestore(OsalSpinlock * spinlock,uint32_t * flags)131 int32_t OsalSpinUnlockIrqRestore(OsalSpinlock *spinlock, uint32_t *flags)
132 {
133 	if (spinlock == NULL || spinlock->realSpinlock == NULL || flags == NULL) {
134 		HDF_LOGE("%s invalid param %d", __func__, __LINE__);
135 		return HDF_ERR_INVALID_PARAM;
136 	}
137 
138 	spin_unlock_irqrestore((spinlock_t *)spinlock->realSpinlock, *flags);
139 
140 	return HDF_SUCCESS;
141 }
142 EXPORT_SYMBOL(OsalSpinUnlockIrqRestore);
143 
144