1 /*
2  * Copyright (c) 2014-2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: i_qdf_atomic.h
22  * This file provides OS dependent atomic APIs.
23  */
24 
25 #ifndef I_QDF_ATOMIC_H
26 #define I_QDF_ATOMIC_H
27 
28 #include <qdf_status.h>         /* QDF_STATUS */
29 #include <linux/atomic.h>
30 #include <linux/bitops.h>
31 
32 typedef atomic_t __qdf_atomic_t;
33 
34 /**
35  * __qdf_atomic_init() - initialize an atomic type variable
36  * @v: A pointer to an opaque atomic variable
37  *
38  * Return: QDF_STATUS
39  */
__qdf_atomic_init(__qdf_atomic_t * v)40 static inline QDF_STATUS __qdf_atomic_init(__qdf_atomic_t *v)
41 {
42 	atomic_set(v, 0);
43 	return QDF_STATUS_SUCCESS;
44 }
45 
46 /**
47  * __qdf_atomic_read() - read the value of an atomic variable
48  * @v: A pointer to an opaque atomic variable
49  *
50  * Return: The current value of the variable
51  */
__qdf_atomic_read(__qdf_atomic_t * v)52 static inline int32_t __qdf_atomic_read(__qdf_atomic_t *v)
53 {
54 	return atomic_read(v);
55 }
56 
57 /**
58  * __qdf_atomic_inc() - increment the value of an atomic variable
59  * @v: A pointer to an opaque atomic variable
60  *
61  * Return: None
62  */
__qdf_atomic_inc(__qdf_atomic_t * v)63 static inline void __qdf_atomic_inc(__qdf_atomic_t *v)
64 {
65 	atomic_inc(v);
66 }
67 
68 /**
69  * __qdf_atomic_dec() - decrement the value of an atomic variable
70  * @v: A pointer to an opaque atomic variable
71  *
72  * Return: None
73  */
__qdf_atomic_dec(__qdf_atomic_t * v)74 static inline void __qdf_atomic_dec(__qdf_atomic_t *v)
75 {
76 	atomic_dec(v);
77 }
78 
79 /**
80  * __qdf_atomic_add() - add a value to the value of an atomic variable
81  * @i: The amount by which to increase the atomic counter
82  * @v: A pointer to an opaque atomic variable
83  *
84  * Return: None
85  */
__qdf_atomic_add(int i,__qdf_atomic_t * v)86 static inline void __qdf_atomic_add(int i, __qdf_atomic_t *v)
87 {
88 	atomic_add(i, v);
89 }
90 
91 /**
92  * __qdf_atomic_sub() - Subtract a value from an atomic variable
93  * @i: the amount by which to decrease the atomic counter
94  * @v: a pointer to an opaque atomic variable
95  *
96  * Return: none
97  */
__qdf_atomic_sub(int i,__qdf_atomic_t * v)98 static inline void __qdf_atomic_sub(int i, __qdf_atomic_t *v)
99 {
100 	atomic_sub(i, v);
101 }
102 
103 /**
104  * __qdf_atomic_dec_and_test() - decrement an atomic variable and check if the
105  * new value is zero
106  * @v: A pointer to an opaque atomic variable
107  *
108  * Return:
109  * true (non-zero) if the new value is zero,
110  * false (0) if the new value is non-zero
111  */
__qdf_atomic_dec_and_test(__qdf_atomic_t * v)112 static inline int32_t __qdf_atomic_dec_and_test(__qdf_atomic_t *v)
113 {
114 	return atomic_dec_and_test(v);
115 }
116 
117 /**
118  * __qdf_atomic_set() - set a value to the value of an atomic variable
119  * @v: A pointer to an opaque atomic variable
120  * @i: value to assign
121  *
122  * Return: None
123  */
__qdf_atomic_set(__qdf_atomic_t * v,int i)124 static inline void __qdf_atomic_set(__qdf_atomic_t *v, int i)
125 {
126 	atomic_set(v, i);
127 }
128 
129 /**
130  * __qdf_atomic_inc_return() - return the incremented value of an atomic variable
131  * @v: A pointer to an opaque atomic variable
132  *
133  * Return: The current value of the variable
134  */
__qdf_atomic_inc_return(__qdf_atomic_t * v)135 static inline int32_t __qdf_atomic_inc_return(__qdf_atomic_t *v)
136 {
137 	return atomic_inc_return(v);
138 }
139 
140 /**
141  * __qdf_atomic_dec_return() - return the decremented value of an atomic
142  * variable
143  * @v: A pointer to an opaque atomic variable
144  *
145  * Return: The current value of the variable
146  */
__qdf_atomic_dec_return(__qdf_atomic_t * v)147 static inline int32_t __qdf_atomic_dec_return(__qdf_atomic_t *v)
148 {
149 	return atomic_dec_return(v);
150 }
151 
152 /**
153  * __qdf_atomic_dec_if_positive() - Decrement an atomic variable if its
154  * value is positive
155  * @v: A pointer to an opaque atomic variable
156  *
157  * Return: The old value of the variable minus 1
158  */
__qdf_atomic_dec_if_positive(__qdf_atomic_t * v)159 static inline int32_t __qdf_atomic_dec_if_positive(__qdf_atomic_t *v)
160 {
161 	return atomic_dec_if_positive(v);
162 }
163 
164 /**
165  * __qdf_atomic_inc_not_zero() - increment if not zero
166  * @v: A pointer to an opaque atomic variable
167  *
168  * Return: Returns non-zero on successful increment and zero otherwise
169  */
__qdf_atomic_inc_not_zero(__qdf_atomic_t * v)170 static inline int32_t __qdf_atomic_inc_not_zero(__qdf_atomic_t *v)
171 {
172 	return atomic_inc_not_zero(v);
173 }
174 
175 /**
176  * __qdf_atomic_set_bit - Atomically set a bit in memory
177  * @nr: bit to set
178  * @addr: the address to start counting from
179  *
180  * Return: none
181  */
__qdf_atomic_set_bit(int nr,volatile unsigned long * addr)182 static inline  void __qdf_atomic_set_bit(int nr, volatile unsigned long *addr)
183 {
184 	set_bit(nr, addr);
185 }
186 
187 /**
188  * __qdf_atomic_clear_bit - Atomically clear a bit in memory
189  * @nr: bit to clear
190  * @addr: the address to start counting from
191  *
192  * Return: none
193  */
__qdf_atomic_clear_bit(int nr,volatile unsigned long * addr)194 static inline void __qdf_atomic_clear_bit(int nr, volatile unsigned long *addr)
195 {
196 	clear_bit(nr, addr);
197 }
198 
199 /**
200  * __qdf_atomic_change_bit - Atomically toggle a bit in memory
201  * from addr
202  * @nr: bit to change
203  * @addr: the address to start counting from
204  *
205  * Return: none
206  */
__qdf_atomic_change_bit(int nr,volatile unsigned long * addr)207 static inline void __qdf_atomic_change_bit(int nr, volatile unsigned long *addr)
208 {
209 	change_bit(nr, addr);
210 }
211 
212 /**
213  * __qdf_atomic_test_and_set_bit - Atomically set a bit and return its old value
214  * @nr: Bit to set
215  * @addr: the address to start counting from
216  *
217  * Return: return nr bit old value
218  */
__qdf_atomic_test_and_set_bit(int nr,volatile unsigned long * addr)219 static inline int __qdf_atomic_test_and_set_bit(int nr,
220 						volatile unsigned long *addr)
221 {
222 	return test_and_set_bit(nr, addr);
223 }
224 
225 /**
226  * __qdf_atomic_test_and_clear_bit - Atomically clear a bit and return its old
227  * value
228  * @nr: bit to clear
229  * @addr: the address to start counting from
230  *
231  * Return: return nr bit old value
232  */
__qdf_atomic_test_and_clear_bit(int nr,volatile unsigned long * addr)233 static inline int __qdf_atomic_test_and_clear_bit(int nr,
234 						  volatile unsigned long *addr)
235 {
236 	return test_and_clear_bit(nr, addr);
237 }
238 
239 /**
240  * __qdf_atomic_test_and_change_bit - Atomically toggle a bit and return its old
241  * value
242  * @nr: bit to change
243  * @addr: the address to start counting from
244  *
245  * Return: return nr bit old value
246  */
__qdf_atomic_test_and_change_bit(int nr,volatile unsigned long * addr)247 static inline int __qdf_atomic_test_and_change_bit(int nr,
248 						volatile unsigned long *addr)
249 {
250 	return test_and_change_bit(nr, addr);
251 }
252 
253 /**
254  * __qdf_atomic_test_bit - Atomically get the nr-th bit value starting from addr
255  * @nr: bit to get
256  * @addr: the address to start counting from
257  *
258  * Return: return nr bit value
259  */
__qdf_atomic_test_bit(int nr,volatile unsigned long * addr)260 static inline int __qdf_atomic_test_bit(int nr, volatile unsigned long *addr)
261 {
262 	return test_bit(nr, addr);
263 }
264 
265 #endif
266