atomic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 much more slower
  6. * than regular operations.
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. *
  12. * Copyright (C) 1996, 97, 99, 2000, 03, 04, 06 by Ralf Baechle
  13. */
  14. #ifndef _ASM_ATOMIC_H
  15. #define _ASM_ATOMIC_H
  16. #include <linux/irqflags.h>
  17. #include <linux/types.h>
  18. #include <asm/barrier.h>
  19. #include <asm/compiler.h>
  20. #include <asm/cpu-features.h>
  21. #include <asm/cmpxchg.h>
  22. #include <asm/war.h>
  23. /*
  24. * Using a branch-likely instruction to check the result of an sc instruction
  25. * works around a bug present in R10000 CPUs prior to revision 3.0 that could
  26. * cause ll-sc sequences to execute non-atomically.
  27. */
  28. #if R10000_LLSC_WAR
  29. # define __scbeqz "beqzl"
  30. #else
  31. # define __scbeqz "beqz"
  32. #endif
  33. #define ATOMIC_INIT(i) { (i) }
  34. /*
  35. * atomic_read - read atomic variable
  36. * @v: pointer of type atomic_t
  37. *
  38. * Atomically reads the value of @v.
  39. */
  40. #define atomic_read(v) READ_ONCE((v)->counter)
  41. /*
  42. * atomic_set - set atomic variable
  43. * @v: pointer of type atomic_t
  44. * @i: required value
  45. *
  46. * Atomically sets the value of @v to @i.
  47. */
  48. #define atomic_set(v, i) WRITE_ONCE((v)->counter, (i))
  49. #define ATOMIC_OP(op, c_op, asm_op) \
  50. static __inline__ void atomic_##op(int i, atomic_t * v) \
  51. { \
  52. if (kernel_uses_llsc) { \
  53. int temp; \
  54. \
  55. __asm__ __volatile__( \
  56. " .set "MIPS_ISA_LEVEL" \n" \
  57. "1: ll %0, %1 # atomic_" #op " \n" \
  58. " " #asm_op " %0, %2 \n" \
  59. " sc %0, %1 \n" \
  60. "\t" __scbeqz " %0, 1b \n" \
  61. " .set mips0 \n" \
  62. : "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (v->counter) \
  63. : "Ir" (i)); \
  64. } else { \
  65. unsigned long flags; \
  66. \
  67. raw_local_irq_save(flags); \
  68. v->counter c_op i; \
  69. raw_local_irq_restore(flags); \
  70. } \
  71. }
  72. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  73. static __inline__ int atomic_##op##_return_relaxed(int i, atomic_t * v) \
  74. { \
  75. int result; \
  76. \
  77. if (kernel_uses_llsc) { \
  78. int temp; \
  79. \
  80. __asm__ __volatile__( \
  81. " .set "MIPS_ISA_LEVEL" \n" \
  82. "1: ll %1, %2 # atomic_" #op "_return \n" \
  83. " " #asm_op " %0, %1, %3 \n" \
  84. " sc %0, %2 \n" \
  85. "\t" __scbeqz " %0, 1b \n" \
  86. " " #asm_op " %0, %1, %3 \n" \
  87. " .set mips0 \n" \
  88. : "=&r" (result), "=&r" (temp), \
  89. "+" GCC_OFF_SMALL_ASM() (v->counter) \
  90. : "Ir" (i)); \
  91. } else { \
  92. unsigned long flags; \
  93. \
  94. raw_local_irq_save(flags); \
  95. result = v->counter; \
  96. result c_op i; \
  97. v->counter = result; \
  98. raw_local_irq_restore(flags); \
  99. } \
  100. \
  101. return result; \
  102. }
  103. #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
  104. static __inline__ int atomic_fetch_##op##_relaxed(int i, atomic_t * v) \
  105. { \
  106. int result; \
  107. \
  108. if (kernel_uses_llsc) { \
  109. int temp; \
  110. \
  111. __asm__ __volatile__( \
  112. " .set "MIPS_ISA_LEVEL" \n" \
  113. "1: ll %1, %2 # atomic_fetch_" #op " \n" \
  114. " " #asm_op " %0, %1, %3 \n" \
  115. " sc %0, %2 \n" \
  116. "\t" __scbeqz " %0, 1b \n" \
  117. " move %0, %1 \n" \
  118. " .set mips0 \n" \
  119. : "=&r" (result), "=&r" (temp), \
  120. "+" GCC_OFF_SMALL_ASM() (v->counter) \
  121. : "Ir" (i)); \
  122. } else { \
  123. unsigned long flags; \
  124. \
  125. raw_local_irq_save(flags); \
  126. result = v->counter; \
  127. v->counter c_op i; \
  128. raw_local_irq_restore(flags); \
  129. } \
  130. \
  131. return result; \
  132. }
  133. #define ATOMIC_OPS(op, c_op, asm_op) \
  134. ATOMIC_OP(op, c_op, asm_op) \
  135. ATOMIC_OP_RETURN(op, c_op, asm_op) \
  136. ATOMIC_FETCH_OP(op, c_op, asm_op)
  137. ATOMIC_OPS(add, +=, addu)
  138. ATOMIC_OPS(sub, -=, subu)
  139. #define atomic_add_return_relaxed atomic_add_return_relaxed
  140. #define atomic_sub_return_relaxed atomic_sub_return_relaxed
  141. #define atomic_fetch_add_relaxed atomic_fetch_add_relaxed
  142. #define atomic_fetch_sub_relaxed atomic_fetch_sub_relaxed
  143. #undef ATOMIC_OPS
  144. #define ATOMIC_OPS(op, c_op, asm_op) \
  145. ATOMIC_OP(op, c_op, asm_op) \
  146. ATOMIC_FETCH_OP(op, c_op, asm_op)
  147. ATOMIC_OPS(and, &=, and)
  148. ATOMIC_OPS(or, |=, or)
  149. ATOMIC_OPS(xor, ^=, xor)
  150. #define atomic_fetch_and_relaxed atomic_fetch_and_relaxed
  151. #define atomic_fetch_or_relaxed atomic_fetch_or_relaxed
  152. #define atomic_fetch_xor_relaxed atomic_fetch_xor_relaxed
  153. #undef ATOMIC_OPS
  154. #undef ATOMIC_FETCH_OP
  155. #undef ATOMIC_OP_RETURN
  156. #undef ATOMIC_OP
  157. /*
  158. * atomic_sub_if_positive - conditionally subtract integer from atomic variable
  159. * @i: integer value to subtract
  160. * @v: pointer of type atomic_t
  161. *
  162. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  163. * The function returns the old value of @v minus @i.
  164. */
  165. static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
  166. {
  167. int result;
  168. smp_mb__before_llsc();
  169. if (kernel_uses_llsc) {
  170. int temp;
  171. __asm__ __volatile__(
  172. " .set "MIPS_ISA_LEVEL" \n"
  173. "1: ll %1, %2 # atomic_sub_if_positive\n"
  174. " subu %0, %1, %3 \n"
  175. " move %1, %0 \n"
  176. " bltz %0, 1f \n"
  177. " sc %1, %2 \n"
  178. "\t" __scbeqz " %1, 1b \n"
  179. "1: \n"
  180. " .set mips0 \n"
  181. : "=&r" (result), "=&r" (temp),
  182. "+" GCC_OFF_SMALL_ASM() (v->counter)
  183. : "Ir" (i));
  184. } else {
  185. unsigned long flags;
  186. raw_local_irq_save(flags);
  187. result = v->counter;
  188. result -= i;
  189. if (result >= 0)
  190. v->counter = result;
  191. raw_local_irq_restore(flags);
  192. }
  193. smp_llsc_mb();
  194. return result;
  195. }
  196. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  197. #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
  198. /*
  199. * atomic_dec_if_positive - decrement by 1 if old value positive
  200. * @v: pointer of type atomic_t
  201. */
  202. #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
  203. #ifdef CONFIG_64BIT
  204. #define ATOMIC64_INIT(i) { (i) }
  205. /*
  206. * atomic64_read - read atomic variable
  207. * @v: pointer of type atomic64_t
  208. *
  209. */
  210. #define atomic64_read(v) READ_ONCE((v)->counter)
  211. /*
  212. * atomic64_set - set atomic variable
  213. * @v: pointer of type atomic64_t
  214. * @i: required value
  215. */
  216. #define atomic64_set(v, i) WRITE_ONCE((v)->counter, (i))
  217. #define ATOMIC64_OP(op, c_op, asm_op) \
  218. static __inline__ void atomic64_##op(long i, atomic64_t * v) \
  219. { \
  220. if (kernel_uses_llsc) { \
  221. long temp; \
  222. \
  223. __asm__ __volatile__( \
  224. " .set "MIPS_ISA_LEVEL" \n" \
  225. "1: lld %0, %1 # atomic64_" #op " \n" \
  226. " " #asm_op " %0, %2 \n" \
  227. " scd %0, %1 \n" \
  228. "\t" __scbeqz " %0, 1b \n" \
  229. " .set mips0 \n" \
  230. : "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (v->counter) \
  231. : "Ir" (i)); \
  232. } else { \
  233. unsigned long flags; \
  234. \
  235. raw_local_irq_save(flags); \
  236. v->counter c_op i; \
  237. raw_local_irq_restore(flags); \
  238. } \
  239. }
  240. #define ATOMIC64_OP_RETURN(op, c_op, asm_op) \
  241. static __inline__ long atomic64_##op##_return_relaxed(long i, atomic64_t * v) \
  242. { \
  243. long result; \
  244. \
  245. if (kernel_uses_llsc) { \
  246. long temp; \
  247. \
  248. __asm__ __volatile__( \
  249. " .set "MIPS_ISA_LEVEL" \n" \
  250. "1: lld %1, %2 # atomic64_" #op "_return\n" \
  251. " " #asm_op " %0, %1, %3 \n" \
  252. " scd %0, %2 \n" \
  253. "\t" __scbeqz " %0, 1b \n" \
  254. " " #asm_op " %0, %1, %3 \n" \
  255. " .set mips0 \n" \
  256. : "=&r" (result), "=&r" (temp), \
  257. "+" GCC_OFF_SMALL_ASM() (v->counter) \
  258. : "Ir" (i)); \
  259. } else { \
  260. unsigned long flags; \
  261. \
  262. raw_local_irq_save(flags); \
  263. result = v->counter; \
  264. result c_op i; \
  265. v->counter = result; \
  266. raw_local_irq_restore(flags); \
  267. } \
  268. \
  269. return result; \
  270. }
  271. #define ATOMIC64_FETCH_OP(op, c_op, asm_op) \
  272. static __inline__ long atomic64_fetch_##op##_relaxed(long i, atomic64_t * v) \
  273. { \
  274. long result; \
  275. \
  276. if (kernel_uses_llsc && R10000_LLSC_WAR) { \
  277. long temp; \
  278. \
  279. __asm__ __volatile__( \
  280. " .set "MIPS_ISA_LEVEL" \n" \
  281. "1: lld %1, %2 # atomic64_fetch_" #op "\n" \
  282. " " #asm_op " %0, %1, %3 \n" \
  283. " scd %0, %2 \n" \
  284. "\t" __scbeqz " %0, 1b \n" \
  285. " move %0, %1 \n" \
  286. " .set mips0 \n" \
  287. : "=&r" (result), "=&r" (temp), \
  288. "+" GCC_OFF_SMALL_ASM() (v->counter) \
  289. : "Ir" (i)); \
  290. } else { \
  291. unsigned long flags; \
  292. \
  293. raw_local_irq_save(flags); \
  294. result = v->counter; \
  295. v->counter c_op i; \
  296. raw_local_irq_restore(flags); \
  297. } \
  298. \
  299. return result; \
  300. }
  301. #define ATOMIC64_OPS(op, c_op, asm_op) \
  302. ATOMIC64_OP(op, c_op, asm_op) \
  303. ATOMIC64_OP_RETURN(op, c_op, asm_op) \
  304. ATOMIC64_FETCH_OP(op, c_op, asm_op)
  305. ATOMIC64_OPS(add, +=, daddu)
  306. ATOMIC64_OPS(sub, -=, dsubu)
  307. #define atomic64_add_return_relaxed atomic64_add_return_relaxed
  308. #define atomic64_sub_return_relaxed atomic64_sub_return_relaxed
  309. #define atomic64_fetch_add_relaxed atomic64_fetch_add_relaxed
  310. #define atomic64_fetch_sub_relaxed atomic64_fetch_sub_relaxed
  311. #undef ATOMIC64_OPS
  312. #define ATOMIC64_OPS(op, c_op, asm_op) \
  313. ATOMIC64_OP(op, c_op, asm_op) \
  314. ATOMIC64_FETCH_OP(op, c_op, asm_op)
  315. ATOMIC64_OPS(and, &=, and)
  316. ATOMIC64_OPS(or, |=, or)
  317. ATOMIC64_OPS(xor, ^=, xor)
  318. #define atomic64_fetch_and_relaxed atomic64_fetch_and_relaxed
  319. #define atomic64_fetch_or_relaxed atomic64_fetch_or_relaxed
  320. #define atomic64_fetch_xor_relaxed atomic64_fetch_xor_relaxed
  321. #undef ATOMIC64_OPS
  322. #undef ATOMIC64_FETCH_OP
  323. #undef ATOMIC64_OP_RETURN
  324. #undef ATOMIC64_OP
  325. /*
  326. * atomic64_sub_if_positive - conditionally subtract integer from atomic
  327. * variable
  328. * @i: integer value to subtract
  329. * @v: pointer of type atomic64_t
  330. *
  331. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  332. * The function returns the old value of @v minus @i.
  333. */
  334. static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
  335. {
  336. long result;
  337. smp_mb__before_llsc();
  338. if (kernel_uses_llsc) {
  339. long temp;
  340. __asm__ __volatile__(
  341. " .set "MIPS_ISA_LEVEL" \n"
  342. "1: lld %1, %2 # atomic64_sub_if_positive\n"
  343. " dsubu %0, %1, %3 \n"
  344. " move %1, %0 \n"
  345. " bltz %0, 1f \n"
  346. " scd %1, %2 \n"
  347. "\t" __scbeqz " %1, 1b \n"
  348. "1: \n"
  349. " .set mips0 \n"
  350. : "=&r" (result), "=&r" (temp),
  351. "+" GCC_OFF_SMALL_ASM() (v->counter)
  352. : "Ir" (i));
  353. } else {
  354. unsigned long flags;
  355. raw_local_irq_save(flags);
  356. result = v->counter;
  357. result -= i;
  358. if (result >= 0)
  359. v->counter = result;
  360. raw_local_irq_restore(flags);
  361. }
  362. smp_llsc_mb();
  363. return result;
  364. }
  365. #define atomic64_cmpxchg(v, o, n) \
  366. ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
  367. #define atomic64_xchg(v, new) (xchg(&((v)->counter), (new)))
  368. /*
  369. * atomic64_dec_if_positive - decrement by 1 if old value positive
  370. * @v: pointer of type atomic64_t
  371. */
  372. #define atomic64_dec_if_positive(v) atomic64_sub_if_positive(1, v)
  373. #endif /* CONFIG_64BIT */
  374. #endif /* _ASM_ATOMIC_H */