atomic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* Low-level functions for atomic operations. Mips version.
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _MIPS_BITS_ATOMIC_H
  16. #define _MIPS_BITS_ATOMIC_H 1
  17. #include <inttypes.h>
  18. #include <sgidefs.h>
  19. typedef int32_t atomic32_t;
  20. typedef uint32_t uatomic32_t;
  21. typedef int_fast32_t atomic_fast32_t;
  22. typedef uint_fast32_t uatomic_fast32_t;
  23. typedef int64_t atomic64_t;
  24. typedef uint64_t uatomic64_t;
  25. typedef int_fast64_t atomic_fast64_t;
  26. typedef uint_fast64_t uatomic_fast64_t;
  27. typedef intptr_t atomicptr_t;
  28. typedef uintptr_t uatomicptr_t;
  29. typedef intmax_t atomic_max_t;
  30. typedef uintmax_t uatomic_max_t;
  31. #if _MIPS_SIM == _ABIO32
  32. #define MIPS_PUSH_MIPS2 ".set mips2\n\t"
  33. #else
  34. #define MIPS_PUSH_MIPS2
  35. #endif
  36. /* See the comments in <sys/asm.h> about the use of the sync instruction. */
  37. #ifndef MIPS_SYNC
  38. # define MIPS_SYNC sync
  39. #endif
  40. /* Certain revisions of the R10000 Processor need an LL/SC Workaround
  41. enabled. Revisions before 3.0 misbehave on atomic operations, and
  42. Revs 2.6 and lower deadlock after several seconds due to other errata.
  43. To quote the R10K Errata:
  44. Workaround: The basic idea is to inhibit the four instructions
  45. from simultaneously becoming active in R10000. Padding all
  46. ll/sc sequences with nops or changing the looping branch in the
  47. routines to a branch likely (which is always predicted taken
  48. by R10000) will work. The nops should go after the loop, and the
  49. number of them should be 28. This number could be decremented for
  50. each additional instruction in the ll/sc loop such as the lock
  51. modifier(s) between the ll and sc, the looping branch and its
  52. delay slot. For typical short routines with one ll/sc loop, any
  53. instructions after the loop could also count as a decrement. The
  54. nop workaround pollutes the cache more but would be a few cycles
  55. faster if all the code is in the cache and the looping branch
  56. is predicted not taken. */
  57. #ifdef _MIPS_ARCH_R10000
  58. #define R10K_BEQZ_INSN "beqzl"
  59. #else
  60. #define R10K_BEQZ_INSN "beqz"
  61. #endif
  62. #define MIPS_SYNC_STR_2(X) #X
  63. #define MIPS_SYNC_STR_1(X) MIPS_SYNC_STR_2(X)
  64. #define MIPS_SYNC_STR MIPS_SYNC_STR_1(MIPS_SYNC)
  65. /* Compare and exchange. For all of the "xxx" routines, we expect a
  66. "__prev" and a "__cmp" variable to be provided by the enclosing scope,
  67. in which values are returned. */
  68. #define __arch_compare_and_exchange_xxx_8_int(mem, newval, oldval, rel, acq) \
  69. (abort (), __prev = 0, __cmp = 0)
  70. #define __arch_compare_and_exchange_xxx_16_int(mem, newval, oldval, rel, acq) \
  71. (abort (), __prev = 0, __cmp = 0)
  72. #define __arch_compare_and_exchange_xxx_32_int(mem, newval, oldval, rel, acq) \
  73. __asm__ __volatile__ ( \
  74. ".set push\n\t" \
  75. MIPS_PUSH_MIPS2 \
  76. rel "\n" \
  77. "1:\t" \
  78. "ll %0,%5\n\t" \
  79. "move %1,$0\n\t" \
  80. "bne %0,%3,2f\n\t" \
  81. "move %1,%4\n\t" \
  82. "sc %1,%2\n\t" \
  83. R10K_BEQZ_INSN" %1,1b\n" \
  84. acq "\n\t" \
  85. ".set pop\n" \
  86. "2:\n\t" \
  87. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  88. : "r" (oldval), "r" (newval), "m" (*mem) \
  89. : "memory")
  90. #if _MIPS_SIM == _ABIO32
  91. /* We can't do an atomic 64-bit operation in O32. */
  92. #define __arch_compare_and_exchange_xxx_64_int(mem, newval, oldval, rel, acq) \
  93. (abort (), __prev = 0, __cmp = 0)
  94. #else
  95. #define __arch_compare_and_exchange_xxx_64_int(mem, newval, oldval, rel, acq) \
  96. __asm__ __volatile__ ("\n" \
  97. ".set push\n\t" \
  98. MIPS_PUSH_MIPS2 \
  99. rel "\n" \
  100. "1:\t" \
  101. "lld %0,%5\n\t" \
  102. "move %1,$0\n\t" \
  103. "bne %0,%3,2f\n\t" \
  104. "move %1,%4\n\t" \
  105. "scd %1,%2\n\t" \
  106. R10K_BEQZ_INSN" %1,1b\n" \
  107. acq "\n\t" \
  108. ".set pop\n" \
  109. "2:\n\t" \
  110. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  111. : "r" (oldval), "r" (newval), "m" (*mem) \
  112. : "memory")
  113. #endif
  114. /* For all "bool" routines, we return FALSE if exchange succesful. */
  115. #define __arch_compare_and_exchange_bool_8_int(mem, new, old, rel, acq) \
  116. ({ __typeof (*mem) __prev attribute_unused; int __cmp; \
  117. __arch_compare_and_exchange_xxx_8_int(mem, new, old, rel, acq); \
  118. !__cmp; })
  119. #define __arch_compare_and_exchange_bool_16_int(mem, new, old, rel, acq) \
  120. ({ __typeof (*mem) __prev attribute_unused; int __cmp; \
  121. __arch_compare_and_exchange_xxx_16_int(mem, new, old, rel, acq); \
  122. !__cmp; })
  123. #define __arch_compare_and_exchange_bool_32_int(mem, new, old, rel, acq) \
  124. ({ __typeof (*mem) __prev attribute_unused; int __cmp; \
  125. __arch_compare_and_exchange_xxx_32_int(mem, new, old, rel, acq); \
  126. !__cmp; })
  127. #define __arch_compare_and_exchange_bool_64_int(mem, new, old, rel, acq) \
  128. ({ __typeof (*mem) __prev attribute_unused; int __cmp; \
  129. __arch_compare_and_exchange_xxx_64_int(mem, new, old, rel, acq); \
  130. !__cmp; })
  131. /* For all "val" routines, return the old value whether exchange
  132. successful or not. */
  133. #define __arch_compare_and_exchange_val_8_int(mem, new, old, rel, acq) \
  134. ({ __typeof (*mem) __prev attribute_unused; int __cmp attribute_unused; \
  135. __arch_compare_and_exchange_xxx_8_int(mem, new, old, rel, acq); \
  136. (__typeof (*mem))__prev; })
  137. #define __arch_compare_and_exchange_val_16_int(mem, new, old, rel, acq) \
  138. ({ __typeof (*mem) __prev attribute_unused; int __cmp attribute_unused; \
  139. __arch_compare_and_exchange_xxx_16_int(mem, new, old, rel, acq); \
  140. (__typeof (*mem))__prev; })
  141. #define __arch_compare_and_exchange_val_32_int(mem, new, old, rel, acq) \
  142. ({ __typeof (*mem) __prev attribute_unused; int __cmp attribute_unused; \
  143. __arch_compare_and_exchange_xxx_32_int(mem, new, old, rel, acq); \
  144. (__typeof (*mem))__prev; })
  145. #define __arch_compare_and_exchange_val_64_int(mem, new, old, rel, acq) \
  146. ({ __typeof (*mem) __prev attribute_unused; int __cmp attribute_unused; \
  147. __arch_compare_and_exchange_xxx_64_int(mem, new, old, rel, acq); \
  148. (__typeof (*mem))__prev; })
  149. /* Compare and exchange with "acquire" semantics, ie barrier after. */
  150. #define atomic_compare_and_exchange_bool_acq(mem, new, old) \
  151. __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
  152. mem, new, old, "", MIPS_SYNC_STR)
  153. #define atomic_compare_and_exchange_val_acq(mem, new, old) \
  154. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  155. mem, new, old, "", MIPS_SYNC_STR)
  156. /* Compare and exchange with "release" semantics, ie barrier before. */
  157. #define atomic_compare_and_exchange_bool_rel(mem, new, old) \
  158. __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
  159. mem, new, old, MIPS_SYNC_STR, "")
  160. #define atomic_compare_and_exchange_val_rel(mem, new, old) \
  161. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  162. mem, new, old, MIPS_SYNC_STR, "")
  163. /* Atomic exchange (without compare). */
  164. #define __arch_exchange_xxx_8_int(mem, newval, rel, acq) \
  165. (abort (), 0)
  166. #define __arch_exchange_xxx_16_int(mem, newval, rel, acq) \
  167. (abort (), 0)
  168. #define __arch_exchange_xxx_32_int(mem, newval, rel, acq) \
  169. ({ __typeof (*mem) __prev; int __cmp; \
  170. __asm__ __volatile__ ("\n" \
  171. ".set push\n\t" \
  172. MIPS_PUSH_MIPS2 \
  173. rel "\n" \
  174. "1:\t" \
  175. "ll %0,%4\n\t" \
  176. "move %1,%3\n\t" \
  177. "sc %1,%2\n\t" \
  178. R10K_BEQZ_INSN" %1,1b\n" \
  179. acq "\n\t" \
  180. ".set pop\n" \
  181. "2:\n\t" \
  182. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  183. : "r" (newval), "m" (*mem) \
  184. : "memory"); \
  185. __prev; })
  186. #if _MIPS_SIM == _ABIO32
  187. /* We can't do an atomic 64-bit operation in O32. */
  188. #define __arch_exchange_xxx_64_int(mem, newval, rel, acq) \
  189. (abort (), 0)
  190. #else
  191. #define __arch_exchange_xxx_64_int(mem, newval, rel, acq) \
  192. ({ __typeof (*mem) __prev; int __cmp; \
  193. __asm__ __volatile__ ("\n" \
  194. ".set push\n\t" \
  195. MIPS_PUSH_MIPS2 \
  196. rel "\n" \
  197. "1:\n" \
  198. "lld %0,%4\n\t" \
  199. "move %1,%3\n\t" \
  200. "scd %1,%2\n\t" \
  201. R10K_BEQZ_INSN" %1,1b\n" \
  202. acq "\n\t" \
  203. ".set pop\n" \
  204. "2:\n\t" \
  205. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  206. : "r" (newval), "m" (*mem) \
  207. : "memory"); \
  208. __prev; })
  209. #endif
  210. #define atomic_exchange_acq(mem, value) \
  211. __atomic_val_bysize (__arch_exchange_xxx, int, mem, value, "", MIPS_SYNC_STR)
  212. #define atomic_exchange_rel(mem, value) \
  213. __atomic_val_bysize (__arch_exchange_xxx, int, mem, value, MIPS_SYNC_STR, "")
  214. /* Atomically add value and return the previous (unincremented) value. */
  215. #define __arch_exchange_and_add_8_int(mem, newval, rel, acq) \
  216. (abort (), (__typeof(*mem)) 0)
  217. #define __arch_exchange_and_add_16_int(mem, newval, rel, acq) \
  218. (abort (), (__typeof(*mem)) 0)
  219. #define __arch_exchange_and_add_32_int(mem, value, rel, acq) \
  220. ({ __typeof (*mem) __prev; int __cmp; \
  221. __asm__ __volatile__ ("\n" \
  222. ".set push\n\t" \
  223. MIPS_PUSH_MIPS2 \
  224. rel "\n" \
  225. "1:\t" \
  226. "ll %0,%4\n\t" \
  227. "addu %1,%0,%3\n\t" \
  228. "sc %1,%2\n\t" \
  229. R10K_BEQZ_INSN" %1,1b\n" \
  230. acq "\n\t" \
  231. ".set pop\n" \
  232. "2:\n\t" \
  233. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  234. : "r" (value), "m" (*mem) \
  235. : "memory"); \
  236. __prev; })
  237. #if _MIPS_SIM == _ABIO32
  238. /* We can't do an atomic 64-bit operation in O32. */
  239. #define __arch_exchange_and_add_64_int(mem, value, rel, acq) \
  240. (abort (), (__typeof(*mem)) 0)
  241. #else
  242. #define __arch_exchange_and_add_64_int(mem, value, rel, acq) \
  243. ({ __typeof (*mem) __prev; int __cmp; \
  244. __asm__ __volatile__ ( \
  245. ".set push\n\t" \
  246. MIPS_PUSH_MIPS2 \
  247. rel "\n" \
  248. "1:\t" \
  249. "lld %0,%4\n\t" \
  250. "daddu %1,%0,%3\n\t" \
  251. "scd %1,%2\n\t" \
  252. R10K_BEQZ_INSN" %1,1b\n" \
  253. acq "\n\t" \
  254. ".set pop\n" \
  255. "2:\n\t" \
  256. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  257. : "r" (value), "m" (*mem) \
  258. : "memory"); \
  259. __prev; })
  260. #endif
  261. /* ??? Barrier semantics for atomic_exchange_and_add appear to be
  262. undefined. Use full barrier for now, as that's safe. */
  263. #define atomic_exchange_and_add(mem, value) \
  264. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
  265. MIPS_SYNC_STR, MIPS_SYNC_STR)
  266. /* TODO: More atomic operations could be implemented efficiently; only the
  267. basic requirements are done. */
  268. #define atomic_full_barrier() \
  269. __asm__ __volatile__ (".set push\n\t" \
  270. MIPS_PUSH_MIPS2 \
  271. MIPS_SYNC_STR "\n\t" \
  272. ".set pop" : : : "memory")
  273. #endif /* bits/atomic.h */