atomic.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Atomic operations that C can't guarantee us. Useful for
  3. * resource counting etc.
  4. *
  5. * But use these as seldom as possible since they are slower than
  6. * regular operations.
  7. *
  8. * Copyright (C) 2004-2006 Atmel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #ifndef __ASM_AVR32_ATOMIC_H
  15. #define __ASM_AVR32_ATOMIC_H
  16. #include <linux/types.h>
  17. #include <asm/cmpxchg.h>
  18. #define ATOMIC_INIT(i) { (i) }
  19. #define atomic_read(v) READ_ONCE((v)->counter)
  20. #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
  21. #define ATOMIC_OP_RETURN(op, asm_op, asm_con) \
  22. static inline int __atomic_##op##_return(int i, atomic_t *v) \
  23. { \
  24. int result; \
  25. \
  26. asm volatile( \
  27. "/* atomic_" #op "_return */\n" \
  28. "1: ssrf 5\n" \
  29. " ld.w %0, %2\n" \
  30. " " #asm_op " %0, %3\n" \
  31. " stcond %1, %0\n" \
  32. " brne 1b" \
  33. : "=&r" (result), "=o" (v->counter) \
  34. : "m" (v->counter), #asm_con (i) \
  35. : "cc"); \
  36. \
  37. return result; \
  38. }
  39. #define ATOMIC_FETCH_OP(op, asm_op, asm_con) \
  40. static inline int __atomic_fetch_##op(int i, atomic_t *v) \
  41. { \
  42. int result, val; \
  43. \
  44. asm volatile( \
  45. "/* atomic_fetch_" #op " */\n" \
  46. "1: ssrf 5\n" \
  47. " ld.w %0, %3\n" \
  48. " mov %1, %0\n" \
  49. " " #asm_op " %1, %4\n" \
  50. " stcond %2, %1\n" \
  51. " brne 1b" \
  52. : "=&r" (result), "=&r" (val), "=o" (v->counter) \
  53. : "m" (v->counter), #asm_con (i) \
  54. : "cc"); \
  55. \
  56. return result; \
  57. }
  58. ATOMIC_OP_RETURN(sub, sub, rKs21)
  59. ATOMIC_OP_RETURN(add, add, r)
  60. ATOMIC_FETCH_OP (sub, sub, rKs21)
  61. ATOMIC_FETCH_OP (add, add, r)
  62. #define ATOMIC_OPS(op, asm_op) \
  63. ATOMIC_OP_RETURN(op, asm_op, r) \
  64. static inline void atomic_##op(int i, atomic_t *v) \
  65. { \
  66. (void)__atomic_##op##_return(i, v); \
  67. } \
  68. ATOMIC_FETCH_OP(op, asm_op, r) \
  69. static inline int atomic_fetch_##op(int i, atomic_t *v) \
  70. { \
  71. return __atomic_fetch_##op(i, v); \
  72. }
  73. ATOMIC_OPS(and, and)
  74. ATOMIC_OPS(or, or)
  75. ATOMIC_OPS(xor, eor)
  76. #undef ATOMIC_OPS
  77. #undef ATOMIC_FETCH_OP
  78. #undef ATOMIC_OP_RETURN
  79. /*
  80. * Probably found the reason why we want to use sub with the signed 21-bit
  81. * limit, it uses one less register than the add instruction that can add up to
  82. * 32-bit values.
  83. *
  84. * Both instructions are 32-bit, to use a 16-bit instruction the immediate is
  85. * very small; 4 bit.
  86. *
  87. * sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate.
  88. * add 32-bit, type II, adds two register values together.
  89. */
  90. #define IS_21BIT_CONST(i) \
  91. (__builtin_constant_p(i) && ((i) >= -1048575) && ((i) <= 1048576))
  92. /*
  93. * atomic_add_return - add integer to atomic variable
  94. * @i: integer value to add
  95. * @v: pointer of type atomic_t
  96. *
  97. * Atomically adds @i to @v. Returns the resulting value.
  98. */
  99. static inline int atomic_add_return(int i, atomic_t *v)
  100. {
  101. if (IS_21BIT_CONST(i))
  102. return __atomic_sub_return(-i, v);
  103. return __atomic_add_return(i, v);
  104. }
  105. static inline int atomic_fetch_add(int i, atomic_t *v)
  106. {
  107. if (IS_21BIT_CONST(i))
  108. return __atomic_fetch_sub(-i, v);
  109. return __atomic_fetch_add(i, v);
  110. }
  111. /*
  112. * atomic_sub_return - subtract the atomic variable
  113. * @i: integer value to subtract
  114. * @v: pointer of type atomic_t
  115. *
  116. * Atomically subtracts @i from @v. Returns the resulting value.
  117. */
  118. static inline int atomic_sub_return(int i, atomic_t *v)
  119. {
  120. if (IS_21BIT_CONST(i))
  121. return __atomic_sub_return(i, v);
  122. return __atomic_add_return(-i, v);
  123. }
  124. static inline int atomic_fetch_sub(int i, atomic_t *v)
  125. {
  126. if (IS_21BIT_CONST(i))
  127. return __atomic_fetch_sub(i, v);
  128. return __atomic_fetch_add(-i, v);
  129. }
  130. /*
  131. * __atomic_add_unless - add unless the number is a given value
  132. * @v: pointer of type atomic_t
  133. * @a: the amount to add to v...
  134. * @u: ...unless v is equal to u.
  135. *
  136. * Atomically adds @a to @v, so long as it was not @u.
  137. * Returns the old value of @v.
  138. */
  139. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  140. {
  141. int tmp, old = atomic_read(v);
  142. if (IS_21BIT_CONST(a)) {
  143. asm volatile(
  144. "/* __atomic_sub_unless */\n"
  145. "1: ssrf 5\n"
  146. " ld.w %0, %2\n"
  147. " cp.w %0, %4\n"
  148. " breq 1f\n"
  149. " sub %0, %3\n"
  150. " stcond %1, %0\n"
  151. " brne 1b\n"
  152. "1:"
  153. : "=&r"(tmp), "=o"(v->counter)
  154. : "m"(v->counter), "rKs21"(-a), "rKs21"(u)
  155. : "cc", "memory");
  156. } else {
  157. asm volatile(
  158. "/* __atomic_add_unless */\n"
  159. "1: ssrf 5\n"
  160. " ld.w %0, %2\n"
  161. " cp.w %0, %4\n"
  162. " breq 1f\n"
  163. " add %0, %3\n"
  164. " stcond %1, %0\n"
  165. " brne 1b\n"
  166. "1:"
  167. : "=&r"(tmp), "=o"(v->counter)
  168. : "m"(v->counter), "r"(a), "ir"(u)
  169. : "cc", "memory");
  170. }
  171. return old;
  172. }
  173. #undef IS_21BIT_CONST
  174. /*
  175. * atomic_sub_if_positive - conditionally subtract integer from atomic variable
  176. * @i: integer value to subtract
  177. * @v: pointer of type atomic_t
  178. *
  179. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  180. * The function returns the old value of @v minus @i.
  181. */
  182. static inline int atomic_sub_if_positive(int i, atomic_t *v)
  183. {
  184. int result;
  185. asm volatile(
  186. "/* atomic_sub_if_positive */\n"
  187. "1: ssrf 5\n"
  188. " ld.w %0, %2\n"
  189. " sub %0, %3\n"
  190. " brlt 1f\n"
  191. " stcond %1, %0\n"
  192. " brne 1b\n"
  193. "1:"
  194. : "=&r"(result), "=o"(v->counter)
  195. : "m"(v->counter), "ir"(i)
  196. : "cc", "memory");
  197. return result;
  198. }
  199. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  200. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  201. #define atomic_sub(i, v) (void)atomic_sub_return(i, v)
  202. #define atomic_add(i, v) (void)atomic_add_return(i, v)
  203. #define atomic_dec(v) atomic_sub(1, (v))
  204. #define atomic_inc(v) atomic_add(1, (v))
  205. #define atomic_dec_return(v) atomic_sub_return(1, v)
  206. #define atomic_inc_return(v) atomic_add_return(1, v)
  207. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  208. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  209. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  210. #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
  211. #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
  212. #endif /* __ASM_AVR32_ATOMIC_H */