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