atomic.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #ifndef _ASM_POWERPC_ATOMIC_H_
  2. #define _ASM_POWERPC_ATOMIC_H_
  3. /*
  4. * PowerPC atomic operations
  5. */
  6. #include <linux/types.h>
  7. #ifdef __KERNEL__
  8. #include <linux/compiler.h>
  9. #include <asm/synch.h>
  10. #include <asm/asm-compat.h>
  11. #include <asm/system.h>
  12. #define ATOMIC_INIT(i) { (i) }
  13. static __inline__ int atomic_read(const atomic_t *v)
  14. {
  15. int t;
  16. __asm__ __volatile__("lwz%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));
  17. return t;
  18. }
  19. static __inline__ void atomic_set(atomic_t *v, int i)
  20. {
  21. __asm__ __volatile__("stw%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
  22. }
  23. static __inline__ void atomic_add(int a, atomic_t *v)
  24. {
  25. int t;
  26. __asm__ __volatile__(
  27. "1: lwarx %0,0,%3 # atomic_add\n\
  28. add %0,%2,%0\n"
  29. PPC405_ERR77(0,%3)
  30. " stwcx. %0,0,%3 \n\
  31. bne- 1b"
  32. : "=&r" (t), "+m" (v->counter)
  33. : "r" (a), "r" (&v->counter)
  34. : "cc");
  35. }
  36. static __inline__ int atomic_add_return(int a, atomic_t *v)
  37. {
  38. int t;
  39. __asm__ __volatile__(
  40. PPC_ATOMIC_ENTRY_BARRIER
  41. "1: lwarx %0,0,%2 # atomic_add_return\n\
  42. add %0,%1,%0\n"
  43. PPC405_ERR77(0,%2)
  44. " stwcx. %0,0,%2 \n\
  45. bne- 1b"
  46. PPC_ATOMIC_EXIT_BARRIER
  47. : "=&r" (t)
  48. : "r" (a), "r" (&v->counter)
  49. : "cc", "memory");
  50. return t;
  51. }
  52. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  53. static __inline__ void atomic_sub(int a, atomic_t *v)
  54. {
  55. int t;
  56. __asm__ __volatile__(
  57. "1: lwarx %0,0,%3 # atomic_sub\n\
  58. subf %0,%2,%0\n"
  59. PPC405_ERR77(0,%3)
  60. " stwcx. %0,0,%3 \n\
  61. bne- 1b"
  62. : "=&r" (t), "+m" (v->counter)
  63. : "r" (a), "r" (&v->counter)
  64. : "cc");
  65. }
  66. static __inline__ int atomic_sub_return(int a, atomic_t *v)
  67. {
  68. int t;
  69. __asm__ __volatile__(
  70. PPC_ATOMIC_ENTRY_BARRIER
  71. "1: lwarx %0,0,%2 # atomic_sub_return\n\
  72. subf %0,%1,%0\n"
  73. PPC405_ERR77(0,%2)
  74. " stwcx. %0,0,%2 \n\
  75. bne- 1b"
  76. PPC_ATOMIC_EXIT_BARRIER
  77. : "=&r" (t)
  78. : "r" (a), "r" (&v->counter)
  79. : "cc", "memory");
  80. return t;
  81. }
  82. static __inline__ void atomic_inc(atomic_t *v)
  83. {
  84. int t;
  85. __asm__ __volatile__(
  86. "1: lwarx %0,0,%2 # atomic_inc\n\
  87. addic %0,%0,1\n"
  88. PPC405_ERR77(0,%2)
  89. " stwcx. %0,0,%2 \n\
  90. bne- 1b"
  91. : "=&r" (t), "+m" (v->counter)
  92. : "r" (&v->counter)
  93. : "cc", "xer");
  94. }
  95. static __inline__ int atomic_inc_return(atomic_t *v)
  96. {
  97. int t;
  98. __asm__ __volatile__(
  99. PPC_ATOMIC_ENTRY_BARRIER
  100. "1: lwarx %0,0,%1 # atomic_inc_return\n\
  101. addic %0,%0,1\n"
  102. PPC405_ERR77(0,%1)
  103. " stwcx. %0,0,%1 \n\
  104. bne- 1b"
  105. PPC_ATOMIC_EXIT_BARRIER
  106. : "=&r" (t)
  107. : "r" (&v->counter)
  108. : "cc", "xer", "memory");
  109. return t;
  110. }
  111. /*
  112. * atomic_inc_and_test - increment and test
  113. * @v: pointer of type atomic_t
  114. *
  115. * Atomically increments @v by 1
  116. * and returns true if the result is zero, or false for all
  117. * other cases.
  118. */
  119. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  120. static __inline__ void atomic_dec(atomic_t *v)
  121. {
  122. int t;
  123. __asm__ __volatile__(
  124. "1: lwarx %0,0,%2 # atomic_dec\n\
  125. addic %0,%0,-1\n"
  126. PPC405_ERR77(0,%2)\
  127. " stwcx. %0,0,%2\n\
  128. bne- 1b"
  129. : "=&r" (t), "+m" (v->counter)
  130. : "r" (&v->counter)
  131. : "cc", "xer");
  132. }
  133. static __inline__ int atomic_dec_return(atomic_t *v)
  134. {
  135. int t;
  136. __asm__ __volatile__(
  137. PPC_ATOMIC_ENTRY_BARRIER
  138. "1: lwarx %0,0,%1 # atomic_dec_return\n\
  139. addic %0,%0,-1\n"
  140. PPC405_ERR77(0,%1)
  141. " stwcx. %0,0,%1\n\
  142. bne- 1b"
  143. PPC_ATOMIC_EXIT_BARRIER
  144. : "=&r" (t)
  145. : "r" (&v->counter)
  146. : "cc", "xer", "memory");
  147. return t;
  148. }
  149. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  150. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  151. /**
  152. * __atomic_add_unless - add unless the number is a given value
  153. * @v: pointer of type atomic_t
  154. * @a: the amount to add to v...
  155. * @u: ...unless v is equal to u.
  156. *
  157. * Atomically adds @a to @v, so long as it was not @u.
  158. * Returns the old value of @v.
  159. */
  160. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  161. {
  162. int t;
  163. __asm__ __volatile__ (
  164. PPC_ATOMIC_ENTRY_BARRIER
  165. "1: lwarx %0,0,%1 # __atomic_add_unless\n\
  166. cmpw 0,%0,%3 \n\
  167. beq- 2f \n\
  168. add %0,%2,%0 \n"
  169. PPC405_ERR77(0,%2)
  170. " stwcx. %0,0,%1 \n\
  171. bne- 1b \n"
  172. PPC_ATOMIC_EXIT_BARRIER
  173. " subf %0,%2,%0 \n\
  174. 2:"
  175. : "=&r" (t)
  176. : "r" (&v->counter), "r" (a), "r" (u)
  177. : "cc", "memory");
  178. return t;
  179. }
  180. #define atomic_sub_and_test(a, v) (atomic_sub_return((a), (v)) == 0)
  181. #define atomic_dec_and_test(v) (atomic_dec_return((v)) == 0)
  182. /*
  183. * Atomically test *v and decrement if it is greater than 0.
  184. * The function returns the old value of *v minus 1, even if
  185. * the atomic variable, v, was not decremented.
  186. */
  187. static __inline__ int atomic_dec_if_positive(atomic_t *v)
  188. {
  189. int t;
  190. __asm__ __volatile__(
  191. PPC_ATOMIC_ENTRY_BARRIER
  192. "1: lwarx %0,0,%1 # atomic_dec_if_positive\n\
  193. cmpwi %0,1\n\
  194. addi %0,%0,-1\n\
  195. blt- 2f\n"
  196. PPC405_ERR77(0,%1)
  197. " stwcx. %0,0,%1\n\
  198. bne- 1b"
  199. PPC_ATOMIC_EXIT_BARRIER
  200. "\n\
  201. 2:" : "=&b" (t)
  202. : "r" (&v->counter)
  203. : "cc", "memory");
  204. return t;
  205. }
  206. #define smp_mb__before_atomic_dec() smp_mb()
  207. #define smp_mb__after_atomic_dec() smp_mb()
  208. #define smp_mb__before_atomic_inc() smp_mb()
  209. #define smp_mb__after_atomic_inc() smp_mb()
  210. #ifdef __powerpc64__
  211. #define ATOMIC64_INIT(i) { (i) }
  212. static __inline__ long atomic64_read(const atomic64_t *v)
  213. {
  214. long t;
  215. __asm__ __volatile__("ld%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));
  216. return t;
  217. }
  218. static __inline__ void atomic64_set(atomic64_t *v, long i)
  219. {
  220. __asm__ __volatile__("std%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
  221. }
  222. static __inline__ void atomic64_add(long a, atomic64_t *v)
  223. {
  224. long t;
  225. __asm__ __volatile__(
  226. "1: ldarx %0,0,%3 # atomic64_add\n\
  227. add %0,%2,%0\n\
  228. stdcx. %0,0,%3 \n\
  229. bne- 1b"
  230. : "=&r" (t), "+m" (v->counter)
  231. : "r" (a), "r" (&v->counter)
  232. : "cc");
  233. }
  234. static __inline__ long atomic64_add_return(long a, atomic64_t *v)
  235. {
  236. long t;
  237. __asm__ __volatile__(
  238. PPC_ATOMIC_ENTRY_BARRIER
  239. "1: ldarx %0,0,%2 # atomic64_add_return\n\
  240. add %0,%1,%0\n\
  241. stdcx. %0,0,%2 \n\
  242. bne- 1b"
  243. PPC_ATOMIC_EXIT_BARRIER
  244. : "=&r" (t)
  245. : "r" (a), "r" (&v->counter)
  246. : "cc", "memory");
  247. return t;
  248. }
  249. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  250. static __inline__ void atomic64_sub(long a, atomic64_t *v)
  251. {
  252. long t;
  253. __asm__ __volatile__(
  254. "1: ldarx %0,0,%3 # atomic64_sub\n\
  255. subf %0,%2,%0\n\
  256. stdcx. %0,0,%3 \n\
  257. bne- 1b"
  258. : "=&r" (t), "+m" (v->counter)
  259. : "r" (a), "r" (&v->counter)
  260. : "cc");
  261. }
  262. static __inline__ long atomic64_sub_return(long a, atomic64_t *v)
  263. {
  264. long t;
  265. __asm__ __volatile__(
  266. PPC_ATOMIC_ENTRY_BARRIER
  267. "1: ldarx %0,0,%2 # atomic64_sub_return\n\
  268. subf %0,%1,%0\n\
  269. stdcx. %0,0,%2 \n\
  270. bne- 1b"
  271. PPC_ATOMIC_EXIT_BARRIER
  272. : "=&r" (t)
  273. : "r" (a), "r" (&v->counter)
  274. : "cc", "memory");
  275. return t;
  276. }
  277. static __inline__ void atomic64_inc(atomic64_t *v)
  278. {
  279. long t;
  280. __asm__ __volatile__(
  281. "1: ldarx %0,0,%2 # atomic64_inc\n\
  282. addic %0,%0,1\n\
  283. stdcx. %0,0,%2 \n\
  284. bne- 1b"
  285. : "=&r" (t), "+m" (v->counter)
  286. : "r" (&v->counter)
  287. : "cc", "xer");
  288. }
  289. static __inline__ long atomic64_inc_return(atomic64_t *v)
  290. {
  291. long t;
  292. __asm__ __volatile__(
  293. PPC_ATOMIC_ENTRY_BARRIER
  294. "1: ldarx %0,0,%1 # atomic64_inc_return\n\
  295. addic %0,%0,1\n\
  296. stdcx. %0,0,%1 \n\
  297. bne- 1b"
  298. PPC_ATOMIC_EXIT_BARRIER
  299. : "=&r" (t)
  300. : "r" (&v->counter)
  301. : "cc", "xer", "memory");
  302. return t;
  303. }
  304. /*
  305. * atomic64_inc_and_test - increment and test
  306. * @v: pointer of type atomic64_t
  307. *
  308. * Atomically increments @v by 1
  309. * and returns true if the result is zero, or false for all
  310. * other cases.
  311. */
  312. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  313. static __inline__ void atomic64_dec(atomic64_t *v)
  314. {
  315. long t;
  316. __asm__ __volatile__(
  317. "1: ldarx %0,0,%2 # atomic64_dec\n\
  318. addic %0,%0,-1\n\
  319. stdcx. %0,0,%2\n\
  320. bne- 1b"
  321. : "=&r" (t), "+m" (v->counter)
  322. : "r" (&v->counter)
  323. : "cc", "xer");
  324. }
  325. static __inline__ long atomic64_dec_return(atomic64_t *v)
  326. {
  327. long t;
  328. __asm__ __volatile__(
  329. PPC_ATOMIC_ENTRY_BARRIER
  330. "1: ldarx %0,0,%1 # atomic64_dec_return\n\
  331. addic %0,%0,-1\n\
  332. stdcx. %0,0,%1\n\
  333. bne- 1b"
  334. PPC_ATOMIC_EXIT_BARRIER
  335. : "=&r" (t)
  336. : "r" (&v->counter)
  337. : "cc", "xer", "memory");
  338. return t;
  339. }
  340. #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
  341. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  342. /*
  343. * Atomically test *v and decrement if it is greater than 0.
  344. * The function returns the old value of *v minus 1.
  345. */
  346. static __inline__ long atomic64_dec_if_positive(atomic64_t *v)
  347. {
  348. long t;
  349. __asm__ __volatile__(
  350. PPC_ATOMIC_ENTRY_BARRIER
  351. "1: ldarx %0,0,%1 # atomic64_dec_if_positive\n\
  352. addic. %0,%0,-1\n\
  353. blt- 2f\n\
  354. stdcx. %0,0,%1\n\
  355. bne- 1b"
  356. PPC_ATOMIC_EXIT_BARRIER
  357. "\n\
  358. 2:" : "=&r" (t)
  359. : "r" (&v->counter)
  360. : "cc", "xer", "memory");
  361. return t;
  362. }
  363. #define atomic64_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  364. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  365. /**
  366. * atomic64_add_unless - add unless the number is a given value
  367. * @v: pointer of type atomic64_t
  368. * @a: the amount to add to v...
  369. * @u: ...unless v is equal to u.
  370. *
  371. * Atomically adds @a to @v, so long as it was not @u.
  372. * Returns the old value of @v.
  373. */
  374. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  375. {
  376. long t;
  377. __asm__ __volatile__ (
  378. PPC_ATOMIC_ENTRY_BARRIER
  379. "1: ldarx %0,0,%1 # __atomic_add_unless\n\
  380. cmpd 0,%0,%3 \n\
  381. beq- 2f \n\
  382. add %0,%2,%0 \n"
  383. " stdcx. %0,0,%1 \n\
  384. bne- 1b \n"
  385. PPC_ATOMIC_EXIT_BARRIER
  386. " subf %0,%2,%0 \n\
  387. 2:"
  388. : "=&r" (t)
  389. : "r" (&v->counter), "r" (a), "r" (u)
  390. : "cc", "memory");
  391. return t != u;
  392. }
  393. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  394. #endif /* __powerpc64__ */
  395. #endif /* __KERNEL__ */
  396. #endif /* _ASM_POWERPC_ATOMIC_H_ */