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: qdf_atomic.h 22 * This file provides OS abstraction for atomic APIs. 23 */ 24 25 #ifndef _QDF_ATOMIC_H 26 #define _QDF_ATOMIC_H 27 28 #include <i_qdf_atomic.h> 29 30 /** 31 * typedef qdf_atomic_t - atomic type of variable 32 * 33 * Use this when you want a simple resource counter etc. which is atomic 34 * across multiple CPU's. These maybe slower than usual counters on some 35 * platforms/OS'es, so use them with caution. 36 */ 37 typedef __qdf_atomic_t qdf_atomic_t; 38 39 /** 40 * qdf_atomic_init() - initialize an atomic type variable 41 * @v: A pointer to an opaque atomic variable 42 * 43 * Return: None 44 */ 45 static inline QDF_STATUS qdf_atomic_init(qdf_atomic_t *v) 46 { 47 return __qdf_atomic_init(v); 48 } 49 50 /** 51 * qdf_atomic_read() - read the value of an atomic variable 52 * @v: A pointer to an opaque atomic variable 53 * 54 * Return: The current value of the variable 55 */ 56 static inline int32_t qdf_atomic_read(qdf_atomic_t *v) 57 { 58 return __qdf_atomic_read(v); 59 } 60 61 /** 62 * qdf_atomic_inc() - increment the value of an atomic variable 63 * @v: A pointer to an opaque atomic variable 64 * 65 * Return: None 66 */ 67 static inline void qdf_atomic_inc(qdf_atomic_t *v) 68 { 69 __qdf_atomic_inc(v); 70 } 71 72 /** 73 * qdf_atomic_dec() - decrement the value of an atomic variable 74 * @v: A pointer to an opaque atomic variable 75 * 76 * Return: None 77 */ 78 static inline void qdf_atomic_dec(qdf_atomic_t *v) 79 { 80 __qdf_atomic_dec(v); 81 } 82 83 /** 84 * qdf_atomic_add() - add a value to the value of an atomic variable 85 * @i: The amount by which to increase the atomic counter 86 * @v: A pointer to an opaque atomic variable 87 * 88 * Return: None 89 */ 90 static inline void qdf_atomic_add(int i, qdf_atomic_t *v) 91 { 92 __qdf_atomic_add(i, v); 93 } 94 95 /** 96 * qdf_atomic_sub() - Subtract a value from an atomic variable 97 * @i: the amount by which to decrease the atomic counter 98 * @v: a pointer to an opaque atomic variable 99 * 100 * Return: none 101 */ 102 static inline void qdf_atomic_sub(int i, qdf_atomic_t *v) 103 { 104 __qdf_atomic_sub(i, v); 105 } 106 107 /** 108 * qdf_atomic_dec_and_test() - decrement an atomic variable and check if the 109 * new value is zero 110 * @v: A pointer to an opaque atomic variable 111 * 112 * Return: 113 * true (non-zero) if the new value is zero, 114 * false (0) if the new value is non-zero 115 */ 116 static inline int32_t qdf_atomic_dec_and_test(qdf_atomic_t *v) 117 { 118 return __qdf_atomic_dec_and_test(v); 119 } 120 121 /** 122 * qdf_atomic_set() - set a value to the value of an atomic variable 123 * @v: A pointer to an opaque atomic variable 124 * @i: required value to set 125 * 126 * Atomically sets the value of v to i 127 * Return: None 128 */ 129 static inline void qdf_atomic_set(qdf_atomic_t *v, int i) 130 { 131 __qdf_atomic_set(v, i); 132 } 133 134 /** 135 * qdf_atomic_inc_return() - return the incremented value of an atomic variable 136 * @v: A pointer to an opaque atomic variable 137 * 138 * Return: The current value of the variable 139 */ 140 static inline int32_t qdf_atomic_inc_return(qdf_atomic_t *v) 141 { 142 return __qdf_atomic_inc_return(v); 143 } 144 145 /** 146 * qdf_atomic_dec_return() - return the decremented value of an atomic 147 * variable 148 * @v: A pointer to an opaque atomic variable 149 * 150 * Return: The current value of the variable 151 */ 152 static inline int32_t qdf_atomic_dec_return(qdf_atomic_t *v) 153 { 154 return __qdf_atomic_dec_return(v); 155 } 156 157 /** 158 * qdf_atomic_dec_if_positive() - Decrement an atomic variable if its 159 * value is positive 160 * @v: A pointer to an opaque atomic variable 161 * 162 * Return: The old value of the variable minus 1 163 */ 164 static inline int32_t qdf_atomic_dec_if_positive(qdf_atomic_t *v) 165 { 166 return __qdf_atomic_dec_if_positive(v); 167 } 168 169 /** 170 * qdf_atomic_inc_not_zero() - increment if not zero 171 * @v: A pointer to an opaque atomic variable 172 * 173 * Return: Returns non-zero on successful increment and zero otherwise 174 */ 175 static inline int32_t qdf_atomic_inc_not_zero(qdf_atomic_t *v) 176 { 177 return __qdf_atomic_inc_not_zero(v); 178 } 179 180 /** 181 * qdf_atomic_set_bit - Atomically set a bit in memory 182 * @nr: bit to set 183 * @addr: the address to start counting from 184 * 185 * Return: none 186 */ 187 static inline void qdf_atomic_set_bit(int nr, volatile unsigned long *addr) 188 { 189 __qdf_atomic_set_bit(nr, addr); 190 } 191 192 /** 193 * qdf_atomic_clear_bit - Atomically clear a bit in memory 194 * @nr: bit to clear 195 * @addr: the address to start counting from 196 * 197 * Return: none 198 */ 199 static inline void qdf_atomic_clear_bit(int nr, volatile unsigned long *addr) 200 { 201 __qdf_atomic_clear_bit(nr, addr); 202 } 203 204 /** 205 * qdf_atomic_change_bit - Atomically toggle a bit in memory 206 * from addr 207 * @nr: bit to change 208 * @addr: the address to start counting from 209 * 210 * Return: none 211 */ 212 static inline void qdf_atomic_change_bit(int nr, volatile unsigned long *addr) 213 { 214 __qdf_atomic_change_bit(nr, addr); 215 } 216 217 /** 218 * qdf_atomic_test_and_set_bit - Atomically set a bit and return its old value 219 * @nr: Bit to set 220 * @addr: the address to start counting from 221 * 222 * Return: return nr bit old value 223 */ 224 static inline int qdf_atomic_test_and_set_bit(int nr, 225 volatile unsigned long *addr) 226 { 227 return __qdf_atomic_test_and_set_bit(nr, addr); 228 } 229 230 /** 231 * qdf_atomic_test_and_clear_bit - Atomically clear a bit and return its old 232 * value 233 * @nr: bit to clear 234 * @addr: the address to start counting from 235 * 236 * Return: return nr bit old value 237 */ 238 static inline int qdf_atomic_test_and_clear_bit(int nr, 239 volatile unsigned long *addr) 240 { 241 return __qdf_atomic_test_and_clear_bit(nr, addr); 242 } 243 244 /** 245 * qdf_atomic_test_and_change_bit - Atomically toggle a bit and return its old 246 * value 247 * @nr: bit to change 248 * @addr: the address to start counting from 249 * 250 * Return: return nr bit old value 251 */ 252 static inline int qdf_atomic_test_and_change_bit(int nr, 253 volatile unsigned long *addr) 254 { 255 return __qdf_atomic_test_and_change_bit(nr, addr); 256 } 257 258 /** 259 * qdf_atomic_test_bit - Atomically get the nr-th bit value starting from addr 260 * @nr: bit to get 261 * @addr: the address to start counting from 262 * 263 * Return: return nr bit value 264 */ 265 static inline int qdf_atomic_test_bit(int nr, volatile unsigned long *addr) 266 { 267 return __qdf_atomic_test_bit(nr, addr); 268 } 269 270 #endif 271