1 /* 2 * Copyright (c) 2014-2020 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_dec_return() - return the decremented value of an atomic 140 * variable 141 * @v: A pointer to an opaque atomic variable 142 * 143 * Return: The current value of the variable 144 */ 145 static inline int32_t __qdf_atomic_dec_return(__qdf_atomic_t *v) 146 { 147 return atomic_dec_return(v); 148 } 149 150 /** 151 * __qdf_atomic_inc_not_zero() - increment if not zero 152 * @v: A pointer to an opaque atomic variable 153 * 154 * Return: Returns non-zero on successful increment and zero otherwise 155 */ 156 static inline int32_t __qdf_atomic_inc_not_zero(__qdf_atomic_t *v) 157 { 158 return atomic_inc_not_zero(v); 159 } 160 161 /** 162 * __qdf_atomic_set_bit - Atomically set a bit in memory 163 * @nr: bit to set 164 * @addr: the address to start counting from 165 * 166 * Return: none 167 */ 168 static inline void __qdf_atomic_set_bit(int nr, volatile unsigned long *addr) 169 { 170 set_bit(nr, addr); 171 } 172 173 /** 174 * __qdf_atomic_clear_bit - Atomically clear a bit in memory 175 * @nr: bit to clear 176 * @addr: the address to start counting from 177 * 178 * Return: none 179 */ 180 static inline void __qdf_atomic_clear_bit(int nr, volatile unsigned long *addr) 181 { 182 clear_bit(nr, addr); 183 } 184 185 /** 186 * __qdf_atomic_change_bit - Atomically toggle a bit in memory 187 * from addr 188 * @nr: bit to change 189 * @addr: the address to start counting from 190 * 191 * Return: none 192 */ 193 static inline void __qdf_atomic_change_bit(int nr, volatile unsigned long *addr) 194 { 195 change_bit(nr, addr); 196 } 197 198 /** 199 * __qdf_atomic_test_and_set_bit - Atomically set a bit and return its old value 200 * @nr: Bit to set 201 * @addr: the address to start counting from 202 * 203 * Return: return nr bit old value 204 */ 205 static inline int __qdf_atomic_test_and_set_bit(int nr, 206 volatile unsigned long *addr) 207 { 208 return test_and_set_bit(nr, addr); 209 } 210 211 /** 212 * __qdf_atomic_test_and_clear_bit - Atomically clear a bit and return its old 213 * value 214 * @nr: bit to clear 215 * @addr: the address to start counting from 216 * 217 * Return: return nr bit old value 218 */ 219 static inline int __qdf_atomic_test_and_clear_bit(int nr, 220 volatile unsigned long *addr) 221 { 222 return test_and_clear_bit(nr, addr); 223 } 224 225 /** 226 * __qdf_atomic_test_and_change_bit - Atomically toggle a bit and return its old 227 * value 228 * @nr: bit to change 229 * @addr: the address to start counting from 230 * 231 * Return: return nr bit old value 232 */ 233 static inline int __qdf_atomic_test_and_change_bit(int nr, 234 volatile unsigned long *addr) 235 { 236 return test_and_change_bit(nr, addr); 237 } 238 239 /** 240 * __qdf_atomic_test_bit - Atomically get the nr-th bit value starting from addr 241 * @nr: bit to get 242 * @addr: the address to start counting from 243 * 244 * Return: return nr bit value 245 */ 246 static inline int __qdf_atomic_test_bit(int nr, volatile unsigned long *addr) 247 { 248 return test_bit(nr, addr); 249 } 250 251 #endif 252