atomic.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. ATOMIC_OP_RETURN(sub, sub, rKs21)
  40. ATOMIC_OP_RETURN(add, add, r)
  41. #define ATOMIC_OP(op, asm_op) \
  42. ATOMIC_OP_RETURN(op, asm_op, r) \
  43. static inline void atomic_##op(int i, atomic_t *v) \
  44. { \
  45. (void)__atomic_##op##_return(i, v); \
  46. }
  47. ATOMIC_OP(and, and)
  48. ATOMIC_OP(or, or)
  49. ATOMIC_OP(xor, eor)
  50. #undef ATOMIC_OP
  51. #undef ATOMIC_OP_RETURN
  52. /*
  53. * Probably found the reason why we want to use sub with the signed 21-bit
  54. * limit, it uses one less register than the add instruction that can add up to
  55. * 32-bit values.
  56. *
  57. * Both instructions are 32-bit, to use a 16-bit instruction the immediate is
  58. * very small; 4 bit.
  59. *
  60. * sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate.
  61. * add 32-bit, type II, adds two register values together.
  62. */
  63. #define IS_21BIT_CONST(i) \
  64. (__builtin_constant_p(i) && ((i) >= -1048575) && ((i) <= 1048576))
  65. /*
  66. * atomic_add_return - add integer to atomic variable
  67. * @i: integer value to add
  68. * @v: pointer of type atomic_t
  69. *
  70. * Atomically adds @i to @v. Returns the resulting value.
  71. */
  72. static inline int atomic_add_return(int i, atomic_t *v)
  73. {
  74. if (IS_21BIT_CONST(i))
  75. return __atomic_sub_return(-i, v);
  76. return __atomic_add_return(i, v);
  77. }
  78. /*
  79. * atomic_sub_return - subtract the atomic variable
  80. * @i: integer value to subtract
  81. * @v: pointer of type atomic_t
  82. *
  83. * Atomically subtracts @i from @v. Returns the resulting value.
  84. */
  85. static inline int atomic_sub_return(int i, atomic_t *v)
  86. {
  87. if (IS_21BIT_CONST(i))
  88. return __atomic_sub_return(i, v);
  89. return __atomic_add_return(-i, v);
  90. }
  91. /*
  92. * __atomic_add_unless - add unless the number is a given value
  93. * @v: pointer of type atomic_t
  94. * @a: the amount to add to v...
  95. * @u: ...unless v is equal to u.
  96. *
  97. * Atomically adds @a to @v, so long as it was not @u.
  98. * Returns the old value of @v.
  99. */
  100. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  101. {
  102. int tmp, old = atomic_read(v);
  103. if (IS_21BIT_CONST(a)) {
  104. asm volatile(
  105. "/* __atomic_sub_unless */\n"
  106. "1: ssrf 5\n"
  107. " ld.w %0, %2\n"
  108. " cp.w %0, %4\n"
  109. " breq 1f\n"
  110. " sub %0, %3\n"
  111. " stcond %1, %0\n"
  112. " brne 1b\n"
  113. "1:"
  114. : "=&r"(tmp), "=o"(v->counter)
  115. : "m"(v->counter), "rKs21"(-a), "rKs21"(u)
  116. : "cc", "memory");
  117. } else {
  118. asm volatile(
  119. "/* __atomic_add_unless */\n"
  120. "1: ssrf 5\n"
  121. " ld.w %0, %2\n"
  122. " cp.w %0, %4\n"
  123. " breq 1f\n"
  124. " add %0, %3\n"
  125. " stcond %1, %0\n"
  126. " brne 1b\n"
  127. "1:"
  128. : "=&r"(tmp), "=o"(v->counter)
  129. : "m"(v->counter), "r"(a), "ir"(u)
  130. : "cc", "memory");
  131. }
  132. return old;
  133. }
  134. #undef IS_21BIT_CONST
  135. /*
  136. * atomic_sub_if_positive - conditionally subtract integer from atomic variable
  137. * @i: integer value to subtract
  138. * @v: pointer of type atomic_t
  139. *
  140. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  141. * The function returns the old value of @v minus @i.
  142. */
  143. static inline int atomic_sub_if_positive(int i, atomic_t *v)
  144. {
  145. int result;
  146. asm volatile(
  147. "/* atomic_sub_if_positive */\n"
  148. "1: ssrf 5\n"
  149. " ld.w %0, %2\n"
  150. " sub %0, %3\n"
  151. " brlt 1f\n"
  152. " stcond %1, %0\n"
  153. " brne 1b\n"
  154. "1:"
  155. : "=&r"(result), "=o"(v->counter)
  156. : "m"(v->counter), "ir"(i)
  157. : "cc", "memory");
  158. return result;
  159. }
  160. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  161. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  162. #define atomic_sub(i, v) (void)atomic_sub_return(i, v)
  163. #define atomic_add(i, v) (void)atomic_add_return(i, v)
  164. #define atomic_dec(v) atomic_sub(1, (v))
  165. #define atomic_inc(v) atomic_add(1, (v))
  166. #define atomic_dec_return(v) atomic_sub_return(1, v)
  167. #define atomic_inc_return(v) atomic_add_return(1, v)
  168. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  169. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  170. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  171. #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
  172. #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
  173. #endif /* __ASM_AVR32_ATOMIC_H */