atomic_32.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #ifndef _ASM_X86_ATOMIC_32_H
  2. #define _ASM_X86_ATOMIC_32_H
  3. #include <linux/compiler.h>
  4. #include <linux/types.h>
  5. #include <asm/processor.h>
  6. #include <asm/cmpxchg.h>
  7. /*
  8. * Atomic operations that C can't guarantee us. Useful for
  9. * resource counting etc..
  10. */
  11. #define ATOMIC_INIT(i) { (i) }
  12. /**
  13. * atomic_read - read atomic variable
  14. * @v: pointer of type atomic_t
  15. *
  16. * Atomically reads the value of @v.
  17. */
  18. static inline int atomic_read(const atomic_t *v)
  19. {
  20. return v->counter;
  21. }
  22. /**
  23. * atomic_set - set atomic variable
  24. * @v: pointer of type atomic_t
  25. * @i: required value
  26. *
  27. * Atomically sets the value of @v to @i.
  28. */
  29. static inline void atomic_set(atomic_t *v, int i)
  30. {
  31. v->counter = i;
  32. }
  33. /**
  34. * atomic_add - add integer to atomic variable
  35. * @i: integer value to add
  36. * @v: pointer of type atomic_t
  37. *
  38. * Atomically adds @i to @v.
  39. */
  40. static inline void atomic_add(int i, atomic_t *v)
  41. {
  42. asm volatile(LOCK_PREFIX "addl %1,%0"
  43. : "+m" (v->counter)
  44. : "ir" (i));
  45. }
  46. /**
  47. * atomic_sub - subtract integer from atomic variable
  48. * @i: integer value to subtract
  49. * @v: pointer of type atomic_t
  50. *
  51. * Atomically subtracts @i from @v.
  52. */
  53. static inline void atomic_sub(int i, atomic_t *v)
  54. {
  55. asm volatile(LOCK_PREFIX "subl %1,%0"
  56. : "+m" (v->counter)
  57. : "ir" (i));
  58. }
  59. /**
  60. * atomic_sub_and_test - subtract value from variable and test result
  61. * @i: integer value to subtract
  62. * @v: pointer of type atomic_t
  63. *
  64. * Atomically subtracts @i from @v and returns
  65. * true if the result is zero, or false for all
  66. * other cases.
  67. */
  68. static inline int atomic_sub_and_test(int i, atomic_t *v)
  69. {
  70. unsigned char c;
  71. asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
  72. : "+m" (v->counter), "=qm" (c)
  73. : "ir" (i) : "memory");
  74. return c;
  75. }
  76. /**
  77. * atomic_inc - increment atomic variable
  78. * @v: pointer of type atomic_t
  79. *
  80. * Atomically increments @v by 1.
  81. */
  82. static inline void atomic_inc(atomic_t *v)
  83. {
  84. asm volatile(LOCK_PREFIX "incl %0"
  85. : "+m" (v->counter));
  86. }
  87. /**
  88. * atomic_dec - decrement atomic variable
  89. * @v: pointer of type atomic_t
  90. *
  91. * Atomically decrements @v by 1.
  92. */
  93. static inline void atomic_dec(atomic_t *v)
  94. {
  95. asm volatile(LOCK_PREFIX "decl %0"
  96. : "+m" (v->counter));
  97. }
  98. /**
  99. * atomic_dec_and_test - decrement and test
  100. * @v: pointer of type atomic_t
  101. *
  102. * Atomically decrements @v by 1 and
  103. * returns true if the result is 0, or false for all other
  104. * cases.
  105. */
  106. static inline int atomic_dec_and_test(atomic_t *v)
  107. {
  108. unsigned char c;
  109. asm volatile(LOCK_PREFIX "decl %0; sete %1"
  110. : "+m" (v->counter), "=qm" (c)
  111. : : "memory");
  112. return c != 0;
  113. }
  114. /**
  115. * atomic_inc_and_test - increment and test
  116. * @v: pointer of type atomic_t
  117. *
  118. * Atomically increments @v by 1
  119. * and returns true if the result is zero, or false for all
  120. * other cases.
  121. */
  122. static inline int atomic_inc_and_test(atomic_t *v)
  123. {
  124. unsigned char c;
  125. asm volatile(LOCK_PREFIX "incl %0; sete %1"
  126. : "+m" (v->counter), "=qm" (c)
  127. : : "memory");
  128. return c != 0;
  129. }
  130. /**
  131. * atomic_add_negative - add and test if negative
  132. * @v: pointer of type atomic_t
  133. * @i: integer value to add
  134. *
  135. * Atomically adds @i to @v and returns true
  136. * if the result is negative, or false when
  137. * result is greater than or equal to zero.
  138. */
  139. static inline int atomic_add_negative(int i, atomic_t *v)
  140. {
  141. unsigned char c;
  142. asm volatile(LOCK_PREFIX "addl %2,%0; sets %1"
  143. : "+m" (v->counter), "=qm" (c)
  144. : "ir" (i) : "memory");
  145. return c;
  146. }
  147. /**
  148. * atomic_add_return - add integer and return
  149. * @v: pointer of type atomic_t
  150. * @i: integer value to add
  151. *
  152. * Atomically adds @i to @v and returns @i + @v
  153. */
  154. static inline int atomic_add_return(int i, atomic_t *v)
  155. {
  156. int __i;
  157. #ifdef CONFIG_M386
  158. unsigned long flags;
  159. if (unlikely(boot_cpu_data.x86 <= 3))
  160. goto no_xadd;
  161. #endif
  162. /* Modern 486+ processor */
  163. __i = i;
  164. asm volatile(LOCK_PREFIX "xaddl %0, %1"
  165. : "+r" (i), "+m" (v->counter)
  166. : : "memory");
  167. return i + __i;
  168. #ifdef CONFIG_M386
  169. no_xadd: /* Legacy 386 processor */
  170. local_irq_save(flags);
  171. __i = atomic_read(v);
  172. atomic_set(v, i + __i);
  173. local_irq_restore(flags);
  174. return i + __i;
  175. #endif
  176. }
  177. /**
  178. * atomic_sub_return - subtract integer and return
  179. * @v: pointer of type atomic_t
  180. * @i: integer value to subtract
  181. *
  182. * Atomically subtracts @i from @v and returns @v - @i
  183. */
  184. static inline int atomic_sub_return(int i, atomic_t *v)
  185. {
  186. return atomic_add_return(-i, v);
  187. }
  188. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  189. {
  190. return cmpxchg(&v->counter, old, new);
  191. }
  192. static inline int atomic_xchg(atomic_t *v, int new)
  193. {
  194. return xchg(&v->counter, new);
  195. }
  196. /**
  197. * atomic_add_unless - add unless the number is already a given value
  198. * @v: pointer of type atomic_t
  199. * @a: the amount to add to v...
  200. * @u: ...unless v is equal to u.
  201. *
  202. * Atomically adds @a to @v, so long as @v was not already @u.
  203. * Returns non-zero if @v was not @u, and zero otherwise.
  204. */
  205. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  206. {
  207. int c, old;
  208. c = atomic_read(v);
  209. for (;;) {
  210. if (unlikely(c == (u)))
  211. break;
  212. old = atomic_cmpxchg((v), c, c + (a));
  213. if (likely(old == c))
  214. break;
  215. c = old;
  216. }
  217. return c != (u);
  218. }
  219. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  220. #define atomic_inc_return(v) (atomic_add_return(1, v))
  221. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  222. /* These are x86-specific, used by some header files */
  223. #define atomic_clear_mask(mask, addr) \
  224. asm volatile(LOCK_PREFIX "andl %0,%1" \
  225. : : "r" (~(mask)), "m" (*(addr)) : "memory")
  226. #define atomic_set_mask(mask, addr) \
  227. asm volatile(LOCK_PREFIX "orl %0,%1" \
  228. : : "r" (mask), "m" (*(addr)) : "memory")
  229. /* Atomic operations are already serializing on x86 */
  230. #define smp_mb__before_atomic_dec() barrier()
  231. #define smp_mb__after_atomic_dec() barrier()
  232. #define smp_mb__before_atomic_inc() barrier()
  233. #define smp_mb__after_atomic_inc() barrier()
  234. /* An 64bit atomic type */
  235. typedef struct {
  236. u64 __aligned(8) counter;
  237. } atomic64_t;
  238. #define ATOMIC64_INIT(val) { (val) }
  239. /**
  240. * atomic64_read - read atomic64 variable
  241. * @ptr: pointer of type atomic64_t
  242. *
  243. * Atomically reads the value of @v.
  244. * Doesn't imply a read memory barrier.
  245. */
  246. #define __atomic64_read(ptr) ((ptr)->counter)
  247. extern u64 atomic64_cmpxchg(atomic64_t *ptr, u64 old_val, u64 new_val);
  248. /**
  249. * atomic64_xchg - xchg atomic64 variable
  250. * @ptr: pointer to type atomic64_t
  251. * @new_val: value to assign
  252. *
  253. * Atomically xchgs the value of @ptr to @new_val and returns
  254. * the old value.
  255. */
  256. extern u64 atomic64_xchg(atomic64_t *ptr, u64 new_val);
  257. /**
  258. * atomic64_set - set atomic64 variable
  259. * @ptr: pointer to type atomic64_t
  260. * @new_val: value to assign
  261. *
  262. * Atomically sets the value of @ptr to @new_val.
  263. */
  264. extern void atomic64_set(atomic64_t *ptr, u64 new_val);
  265. /**
  266. * atomic64_read - read atomic64 variable
  267. * @ptr: pointer to type atomic64_t
  268. *
  269. * Atomically reads the value of @ptr and returns it.
  270. */
  271. extern u64 atomic64_read(atomic64_t *ptr);
  272. /**
  273. * atomic64_add_return - add and return
  274. * @delta: integer value to add
  275. * @ptr: pointer to type atomic64_t
  276. *
  277. * Atomically adds @delta to @ptr and returns @delta + *@ptr
  278. */
  279. extern u64 atomic64_add_return(u64 delta, atomic64_t *ptr);
  280. /*
  281. * Other variants with different arithmetic operators:
  282. */
  283. extern u64 atomic64_sub_return(u64 delta, atomic64_t *ptr);
  284. extern u64 atomic64_inc_return(atomic64_t *ptr);
  285. extern u64 atomic64_dec_return(atomic64_t *ptr);
  286. /**
  287. * atomic64_add - add integer to atomic64 variable
  288. * @delta: integer value to add
  289. * @ptr: pointer to type atomic64_t
  290. *
  291. * Atomically adds @delta to @ptr.
  292. */
  293. extern void atomic64_add(u64 delta, atomic64_t *ptr);
  294. /**
  295. * atomic64_sub - subtract the atomic64 variable
  296. * @delta: integer value to subtract
  297. * @ptr: pointer to type atomic64_t
  298. *
  299. * Atomically subtracts @delta from @ptr.
  300. */
  301. extern void atomic64_sub(u64 delta, atomic64_t *ptr);
  302. /**
  303. * atomic64_sub_and_test - subtract value from variable and test result
  304. * @delta: integer value to subtract
  305. * @ptr: pointer to type atomic64_t
  306. *
  307. * Atomically subtracts @delta from @ptr and returns
  308. * true if the result is zero, or false for all
  309. * other cases.
  310. */
  311. extern int atomic64_sub_and_test(u64 delta, atomic64_t *ptr);
  312. /**
  313. * atomic64_inc - increment atomic64 variable
  314. * @ptr: pointer to type atomic64_t
  315. *
  316. * Atomically increments @ptr by 1.
  317. */
  318. extern void atomic64_inc(atomic64_t *ptr);
  319. /**
  320. * atomic64_dec - decrement atomic64 variable
  321. * @ptr: pointer to type atomic64_t
  322. *
  323. * Atomically decrements @ptr by 1.
  324. */
  325. extern void atomic64_dec(atomic64_t *ptr);
  326. /**
  327. * atomic64_dec_and_test - decrement and test
  328. * @ptr: pointer to type atomic64_t
  329. *
  330. * Atomically decrements @ptr by 1 and
  331. * returns true if the result is 0, or false for all other
  332. * cases.
  333. */
  334. extern int atomic64_dec_and_test(atomic64_t *ptr);
  335. /**
  336. * atomic64_inc_and_test - increment and test
  337. * @ptr: pointer to type atomic64_t
  338. *
  339. * Atomically increments @ptr by 1
  340. * and returns true if the result is zero, or false for all
  341. * other cases.
  342. */
  343. extern int atomic64_inc_and_test(atomic64_t *ptr);
  344. /**
  345. * atomic64_add_negative - add and test if negative
  346. * @delta: integer value to add
  347. * @ptr: pointer to type atomic64_t
  348. *
  349. * Atomically adds @delta to @ptr and returns true
  350. * if the result is negative, or false when
  351. * result is greater than or equal to zero.
  352. */
  353. extern int atomic64_add_negative(u64 delta, atomic64_t *ptr);
  354. #include <asm-generic/atomic-long.h>
  355. #endif /* _ASM_X86_ATOMIC_32_H */