atomic.h 4.7 KB

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