atomic.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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) (*(volatile int *)&(v)->counter)
  20. #define atomic_set(v, i) (((v)->counter) = i)
  21. /*
  22. * atomic_sub_return - subtract the atomic variable
  23. * @i: integer value to subtract
  24. * @v: pointer of type atomic_t
  25. *
  26. * Atomically subtracts @i from @v. Returns the resulting value.
  27. */
  28. static inline int atomic_sub_return(int i, atomic_t *v)
  29. {
  30. int result;
  31. asm volatile(
  32. "/* atomic_sub_return */\n"
  33. "1: ssrf 5\n"
  34. " ld.w %0, %2\n"
  35. " sub %0, %3\n"
  36. " stcond %1, %0\n"
  37. " brne 1b"
  38. : "=&r"(result), "=o"(v->counter)
  39. : "m"(v->counter), "rKs21"(i)
  40. : "cc");
  41. return result;
  42. }
  43. /*
  44. * atomic_add_return - add integer to atomic variable
  45. * @i: integer value to add
  46. * @v: pointer of type atomic_t
  47. *
  48. * Atomically adds @i to @v. Returns the resulting value.
  49. */
  50. static inline int atomic_add_return(int i, atomic_t *v)
  51. {
  52. int result;
  53. if (__builtin_constant_p(i) && (i >= -1048575) && (i <= 1048576))
  54. result = atomic_sub_return(-i, v);
  55. else
  56. asm volatile(
  57. "/* atomic_add_return */\n"
  58. "1: ssrf 5\n"
  59. " ld.w %0, %1\n"
  60. " add %0, %3\n"
  61. " stcond %2, %0\n"
  62. " brne 1b"
  63. : "=&r"(result), "=o"(v->counter)
  64. : "m"(v->counter), "r"(i)
  65. : "cc", "memory");
  66. return result;
  67. }
  68. /*
  69. * atomic_sub_unless - sub unless the number is a given value
  70. * @v: pointer of type atomic_t
  71. * @a: the amount to subtract from v...
  72. * @u: ...unless v is equal to u.
  73. *
  74. * Atomically subtract @a from @v, so long as it was not @u.
  75. * Returns the old value of @v.
  76. */
  77. static inline void atomic_sub_unless(atomic_t *v, int a, int u)
  78. {
  79. int tmp;
  80. asm volatile(
  81. "/* atomic_sub_unless */\n"
  82. "1: ssrf 5\n"
  83. " ld.w %0, %2\n"
  84. " cp.w %0, %4\n"
  85. " breq 1f\n"
  86. " sub %0, %3\n"
  87. " stcond %1, %0\n"
  88. " brne 1b\n"
  89. "1:"
  90. : "=&r"(tmp), "=o"(v->counter)
  91. : "m"(v->counter), "rKs21"(a), "rKs21"(u)
  92. : "cc", "memory");
  93. }
  94. /*
  95. * __atomic_add_unless - add unless the number is a given value
  96. * @v: pointer of type atomic_t
  97. * @a: the amount to add to v...
  98. * @u: ...unless v is equal to u.
  99. *
  100. * Atomically adds @a to @v, so long as it was not @u.
  101. * Returns the old value of @v.
  102. */
  103. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  104. {
  105. int tmp, old = atomic_read(v);
  106. if (__builtin_constant_p(a) && (a >= -1048575) && (a <= 1048576))
  107. atomic_sub_unless(v, -a, u);
  108. else {
  109. asm volatile(
  110. "/* __atomic_add_unless */\n"
  111. "1: ssrf 5\n"
  112. " ld.w %0, %2\n"
  113. " cp.w %0, %4\n"
  114. " breq 1f\n"
  115. " add %0, %3\n"
  116. " stcond %1, %0\n"
  117. " brne 1b\n"
  118. "1:"
  119. : "=&r"(tmp), "=o"(v->counter)
  120. : "m"(v->counter), "r"(a), "ir"(u)
  121. : "cc", "memory");
  122. }
  123. return old;
  124. }
  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. #define smp_mb__before_atomic_dec() barrier()
  164. #define smp_mb__after_atomic_dec() barrier()
  165. #define smp_mb__before_atomic_inc() barrier()
  166. #define smp_mb__after_atomic_inc() barrier()
  167. #endif /* __ASM_AVR32_ATOMIC_H */