1 /* 2 * Copyright (c) 2014-2018 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_set_bit - Atomically set a bit in memory 152 * @nr: bit to set 153 * @addr: the address to start counting from 154 * 155 * Return: none 156 */ 157 static inline void __qdf_atomic_set_bit(int nr, volatile unsigned long *addr) 158 { 159 set_bit(nr, addr); 160 } 161 162 /** 163 * __qdf_atomic_clear_bit - Atomically clear a bit in memory 164 * @nr: bit to clear 165 * @addr: the address to start counting from 166 * 167 * Return: none 168 */ 169 static inline void __qdf_atomic_clear_bit(int nr, volatile unsigned long *addr) 170 { 171 clear_bit(nr, addr); 172 } 173 174 /** 175 * __qdf_atomic_change_bit - Atomically toggle a bit in memory 176 * from addr 177 * @nr: bit to change 178 * @addr: the address to start counting from 179 * 180 * Return: none 181 */ 182 static inline void __qdf_atomic_change_bit(int nr, volatile unsigned long *addr) 183 { 184 change_bit(nr, addr); 185 } 186 187 /** 188 * __qdf_atomic_test_and_set_bit - Atomically set a bit and return its old value 189 * @nr: Bit to set 190 * @addr: the address to start counting from 191 * 192 * Return: return nr bit old value 193 */ 194 static inline int __qdf_atomic_test_and_set_bit(int nr, 195 volatile unsigned long *addr) 196 { 197 return test_and_set_bit(nr, addr); 198 } 199 200 /** 201 * __qdf_atomic_test_and_clear_bit - Atomically clear a bit and return its old 202 * value 203 * @nr: bit to clear 204 * @addr: the address to start counting from 205 * 206 * Return: return nr bit old value 207 */ 208 static inline int __qdf_atomic_test_and_clear_bit(int nr, 209 volatile unsigned long *addr) 210 { 211 return test_and_clear_bit(nr, addr); 212 } 213 214 /** 215 * __qdf_atomic_test_and_change_bit - Atomically toggle a bit and return its old 216 * value 217 * @nr: bit to change 218 * @addr: the address to start counting from 219 * 220 * Return: return nr bit old value 221 */ 222 static inline int __qdf_atomic_test_and_change_bit(int nr, 223 volatile unsigned long *addr) 224 { 225 return test_and_change_bit(nr, addr); 226 } 227 228 /** 229 * __qdf_atomic_test_bit - Atomically get the nr-th bit value starting from addr 230 * @nr: bit to get 231 * @addr: the address to start counting from 232 * 233 * Return: return nr bit value 234 */ 235 static inline int __qdf_atomic_test_bit(int nr, volatile unsigned long *addr) 236 { 237 return test_bit(nr, addr); 238 } 239 240 #endif 241