atomic.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* Macros for atomic functionality for tile.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by Walter Lee (walt@tilera.com)
  4. This file is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. This file is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* Provides macros for common atomic functionality. */
  20. #ifndef _ATOMIC_H_
  21. #define _ATOMIC_H_
  22. #ifdef __tilegx__
  23. /* Atomic instruction macros
  24. The macros provided by atomic.h simplify access to the TILE-Gx
  25. architecture's atomic instructions. The architecture provides a
  26. variety of atomic instructions, including "exchange", "compare and
  27. exchange", "fetch and ADD", "fetch and AND", "fetch and OR", and
  28. "fetch and ADD if greater than or equal to zero".
  29. No barrier or fence semantics are implied by any of the atomic
  30. instructions for manipulating memory; you must specify the barriers
  31. that you wish explicitly, using the provided macros.
  32. Any integral 32- or 64-bit value can be used as the argument
  33. to these macros, such as "int", "long long", "unsigned long", etc.
  34. The pointers must be aligned to 4 or 8 bytes for 32- or 64-bit data.
  35. The "exchange" and "compare and exchange" macros may also take
  36. pointer values. We use the pseudo-type "VAL" in the documentation
  37. to indicate the use of an appropriate type. */
  38. #else
  39. /* Atomic instruction macros
  40. The macros provided by atomic.h simplify access to the Tile
  41. architecture's atomic instructions. Since the architecture
  42. supports test-and-set as its only in-silicon atomic operation, many
  43. of the operations provided by this header are implemented as
  44. fast-path calls to Linux emulation routines.
  45. Using the kernel for atomic operations allows userspace to take
  46. advantage of the kernel's existing atomic-integer support (managed
  47. by a distributed array of locks). The kernel provides proper
  48. ordering among simultaneous atomic operations on different cores,
  49. and guarantees a process cannot be context-switched part way
  50. through an atomic operation. By virtue of sharing the kernel
  51. atomic implementation, the userspace atomic operations
  52. are compatible with the atomic methods provided by the kernel's
  53. futex() syscall API. Note that these operations never cause Linux
  54. kernel scheduling, and are in fact invisible to the kernel; they
  55. simply act as regular function calls but with an elevated privilege
  56. level. Note that the kernel's distributed lock array is hashed by
  57. using only VA bits from the atomic value's address (to avoid the
  58. performance hit of page table locking and multiple page-table
  59. lookups to get the PA) and only the VA bits that are below page
  60. granularity (to properly lock simultaneous accesses to the same
  61. page mapped at different VAs). As a result, simultaneous atomic
  62. operations on values whose addresses are at the same offset on a
  63. page will contend in the kernel for the same lock array element.
  64. No barrier or fence semantics are implied by any of the atomic
  65. instructions for manipulating memory; you must specify the barriers
  66. that you wish explicitly, using the provided macros.
  67. Any integral 32- or 64-bit value can be used as the argument
  68. to these macros, such as "int", "long long", "unsigned long", etc.
  69. The pointers must be aligned to 4 or 8 bytes for 32- or 64-bit data.
  70. The "exchange" and "compare and exchange" macros may also take
  71. pointer values. We use the pseudo-type "VAL" in the documentation
  72. to indicate the use of an appropriate type.
  73. The 32-bit routines are implemented using a single kernel fast
  74. syscall, as is the 64-bit compare-and-exchange. The other 64-bit
  75. routines are implemented by looping over the 64-bit
  76. compare-and-exchange routine, so may be potentially less efficient. */
  77. #endif
  78. #ifdef __tilegx__
  79. #define SPR_CMPEXCH_VALUE 0x2780
  80. #else
  81. #define __NR_FAST_cmpxchg -1
  82. #define __NR_FAST_atomic_update -2
  83. #define __NR_FAST_cmpxchg64 -3
  84. #endif
  85. /* 32-bit integer compare-and-exchange. */
  86. static __inline __attribute__ ((always_inline))
  87. int arch_atomic_val_compare_and_exchange_4 (volatile int *mem,
  88. int oldval, int newval)
  89. {
  90. #ifdef __tilegx__
  91. __insn_mtspr (SPR_CMPEXCH_VALUE, oldval);
  92. return __insn_cmpexch4 (mem, newval);
  93. #else
  94. int result;
  95. __asm__ __volatile__ ("swint1":"=R00" (result),
  96. "=m" (*mem):"R10" (__NR_FAST_cmpxchg), "R00" (mem),
  97. "R01" (oldval), "R02" (newval), "m" (*mem):"r20",
  98. "r21", "r22", "r23", "r24", "r25", "r26", "r27",
  99. "r28", "r29", "memory");
  100. return result;
  101. #endif
  102. }
  103. /* 64-bit integer compare-and-exchange. */
  104. static __inline __attribute__ ((always_inline))
  105. long long arch_atomic_val_compare_and_exchange_8 (volatile long long
  106. *mem, long long oldval,
  107. long long newval)
  108. {
  109. #ifdef __tilegx__
  110. __insn_mtspr (SPR_CMPEXCH_VALUE, oldval);
  111. return __insn_cmpexch (mem, newval);
  112. #else
  113. unsigned int result_lo, result_hi;
  114. unsigned int oldval_lo = oldval & 0xffffffffu, oldval_hi = oldval >> 32;
  115. unsigned int newval_lo = newval & 0xffffffffu, newval_hi = newval >> 32;
  116. __asm__ __volatile__ ("swint1":"=R00" (result_lo), "=R01" (result_hi),
  117. "=m" (*mem):"R10" (__NR_FAST_cmpxchg64), "R00" (mem),
  118. "R02" (oldval_lo), "R03" (oldval_hi),
  119. "R04" (newval_lo), "R05" (newval_hi),
  120. "m" (*mem):"r20", "r21", "r22", "r23", "r24", "r25",
  121. "r26", "r27", "r28", "r29", "memory");
  122. return ((long long) result_hi) << 32 | result_lo;
  123. #endif
  124. }
  125. /* This non-existent symbol is called for sizes other than "4" and "8",
  126. indicating a bug in the caller. */
  127. extern int __arch_atomic_error_bad_argument_size (void)
  128. __attribute__ ((warning ("sizeof atomic argument not 4 or 8")));
  129. #define arch_atomic_val_compare_and_exchange(mem, o, n) \
  130. __extension__ ({ \
  131. (__typeof(*(mem)))(__typeof(*(mem)-*(mem))) \
  132. ((sizeof(*(mem)) == 8) ? \
  133. arch_atomic_val_compare_and_exchange_8( \
  134. (volatile long long*)(mem), (__typeof((o)-(o)))(o), \
  135. (__typeof((n)-(n)))(n)) : \
  136. (sizeof(*(mem)) == 4) ? \
  137. arch_atomic_val_compare_and_exchange_4( \
  138. (volatile int*)(mem), (__typeof((o)-(o)))(o), \
  139. (__typeof((n)-(n)))(n)) : \
  140. __arch_atomic_error_bad_argument_size()); \
  141. })
  142. #define arch_atomic_bool_compare_and_exchange(mem, o, n) \
  143. __extension__ ({ \
  144. __typeof(o) __o = (o); \
  145. __builtin_expect( \
  146. __o == arch_atomic_val_compare_and_exchange((mem), __o, (n)), 1); \
  147. })
  148. /* Loop with compare_and_exchange until we guess the correct value.
  149. Normally "expr" will be an expression using __old and __value. */
  150. #define __arch_atomic_update_cmpxchg(mem, value, expr) \
  151. __extension__ ({ \
  152. __typeof(value) __value = (value); \
  153. __typeof(*(mem)) *__mem = (mem), __old = *__mem, __guess; \
  154. do { \
  155. __guess = __old; \
  156. __old = arch_atomic_val_compare_and_exchange(__mem, __old, (expr)); \
  157. } while (__builtin_expect(__old != __guess, 0)); \
  158. __old; \
  159. })
  160. #ifdef __tilegx__
  161. /* Generic atomic op with 8- or 4-byte variant.
  162. The _mask, _addend, and _expr arguments are ignored on tilegx. */
  163. #define __arch_atomic_update(mem, value, op, _mask, _addend, _expr) \
  164. __extension__ ({ \
  165. ((__typeof(*(mem))) \
  166. ((sizeof(*(mem)) == 8) ? (__typeof(*(mem)-*(mem)))__insn_##op( \
  167. (volatile void *)(mem), \
  168. (long long)(__typeof((value)-(value)))(value)) : \
  169. (sizeof(*(mem)) == 4) ? (int)__insn_##op##4( \
  170. (volatile void *)(mem), \
  171. (int)(__typeof((value)-(value)))(value)) : \
  172. __arch_atomic_error_bad_argument_size())); \
  173. })
  174. #else
  175. /* This uses TILEPro's fast syscall support to atomically compute:
  176. int old = *ptr;
  177. *ptr = (old & mask) + addend;
  178. return old;
  179. This primitive can be used for atomic exchange, add, or, and.
  180. Only 32-bit support is provided. */
  181. static __inline __attribute__ ((always_inline))
  182. int
  183. __arch_atomic_update_4 (volatile int *mem, int mask, int addend)
  184. {
  185. int result;
  186. __asm__ __volatile__ ("swint1":"=R00" (result),
  187. "=m" (*mem):"R10" (__NR_FAST_atomic_update),
  188. "R00" (mem), "R01" (mask), "R02" (addend),
  189. "m" (*mem):"r20", "r21", "r22", "r23", "r24", "r25",
  190. "r26", "r27", "r28", "r29", "memory");
  191. return result;
  192. }
  193. /* Generic atomic op with 8- or 4-byte variant.
  194. The _op argument is ignored on tilepro. */
  195. #define __arch_atomic_update(mem, value, _op, mask, addend, expr) \
  196. __extension__ ({ \
  197. (__typeof(*(mem)))(__typeof(*(mem)-*(mem))) \
  198. ((sizeof(*(mem)) == 8) ? \
  199. __arch_atomic_update_cmpxchg((mem), (value), (expr)) : \
  200. (sizeof(*(mem)) == 4) ? \
  201. __arch_atomic_update_4((volatile int*)(mem), \
  202. (__typeof((mask)-(mask)))(mask), \
  203. (__typeof((addend)-(addend)))(addend)) : \
  204. __arch_atomic_error_bad_argument_size()); \
  205. })
  206. #endif /* __tilegx__ */
  207. #define arch_atomic_exchange(mem, newvalue) \
  208. __arch_atomic_update(mem, newvalue, exch, 0, newvalue, __value)
  209. #define arch_atomic_add(mem, value) \
  210. __arch_atomic_update(mem, value, fetchadd, -1, value, __old + __value)
  211. #define arch_atomic_sub(mem, value) arch_atomic_add((mem), -(value))
  212. #define arch_atomic_increment(mem) arch_atomic_add((mem), 1)
  213. #define arch_atomic_decrement(mem) arch_atomic_add((mem), -1)
  214. #define arch_atomic_and(mem, mask) \
  215. __arch_atomic_update(mem, mask, fetchand, mask, 0, __old & __value)
  216. #define arch_atomic_or(mem, mask) \
  217. __arch_atomic_update(mem, mask, fetchor, ~mask, mask, __old | __value)
  218. #define arch_atomic_xor(mem, mask) \
  219. __arch_atomic_update_cmpxchg(mem, mask, __old ^ __value)
  220. #define arch_atomic_nand(mem, mask) \
  221. __arch_atomic_update_cmpxchg(mem, mask, ~(__old & __value))
  222. #define arch_atomic_bit_set(mem, bit) \
  223. __extension__ ({ \
  224. __typeof(*(mem)) __mask = (__typeof(*(mem)))1 << (bit); \
  225. __mask & arch_atomic_or((mem), __mask); \
  226. })
  227. #define arch_atomic_bit_clear(mem, bit) \
  228. __extension__ ({ \
  229. __typeof(*(mem)) __mask = (__typeof(*(mem)))1 << (bit); \
  230. __mask & arch_atomic_and((mem), ~__mask); \
  231. })
  232. #ifdef __tilegx__
  233. /* Atomically store a new value to memory.
  234. Note that you can freely use types of any size here, unlike the
  235. other atomic routines, which require 32- or 64-bit types.
  236. This accessor is provided for compatibility with TILEPro, which
  237. required an explicit atomic operation for stores that needed
  238. to be atomic with respect to other atomic methods in this header. */
  239. #define arch_atomic_write(mem, value) ((void) (*(mem) = (value)))
  240. #else
  241. #define arch_atomic_write(mem, value) \
  242. do { \
  243. __typeof(mem) __aw_mem = (mem); \
  244. __typeof(value) __aw_val = (value); \
  245. unsigned int *__aw_mem32, __aw_intval, __aw_val32, __aw_off, __aw_mask; \
  246. __aw_intval = (__typeof((value) - (value)))__aw_val; \
  247. switch (sizeof(*__aw_mem)) { \
  248. case 8: \
  249. __arch_atomic_update_cmpxchg(__aw_mem, __aw_val, __value); \
  250. break; \
  251. case 4: \
  252. __arch_atomic_update_4((int *)__aw_mem, 0, __aw_intval); \
  253. break; \
  254. case 2: \
  255. __aw_off = 8 * ((long)__aw_mem & 0x2); \
  256. __aw_mask = 0xffffU << __aw_off; \
  257. __aw_mem32 = (unsigned int *)((long)__aw_mem & ~0x2); \
  258. __aw_val32 = (__aw_intval << __aw_off) & __aw_mask; \
  259. __arch_atomic_update_cmpxchg(__aw_mem32, __aw_val32, \
  260. (__old & ~__aw_mask) | __value); \
  261. break; \
  262. case 1: \
  263. __aw_off = 8 * ((long)__aw_mem & 0x3); \
  264. __aw_mask = 0xffU << __aw_off; \
  265. __aw_mem32 = (unsigned int *)((long)__aw_mem & ~0x3); \
  266. __aw_val32 = (__aw_intval << __aw_off) & __aw_mask; \
  267. __arch_atomic_update_cmpxchg(__aw_mem32, __aw_val32, \
  268. (__old & ~__aw_mask) | __value); \
  269. break; \
  270. } \
  271. } while (0)
  272. #endif
  273. /* Compiler barrier.
  274. This macro prevents loads or stores from being moved by the compiler
  275. across the macro. Any loaded value that was loaded before this
  276. macro must then be reloaded by the compiler. */
  277. #define arch_atomic_compiler_barrier() __asm__ __volatile__("" ::: "memory")
  278. /* Full memory barrier.
  279. This macro has the semantics of arch_atomic_compiler_barrer(), but also
  280. ensures that previous stores are visible to other cores, and that
  281. all previous loaded values have been placed into their target
  282. register on this core. */
  283. #define arch_atomic_full_barrier() __insn_mf()
  284. /* Read memory barrier.
  285. Ensure that all reads by this processor that occurred prior to the
  286. read memory barrier have completed, and that no reads that occur
  287. after the read memory barrier on this processor are initiated
  288. before the barrier.
  289. On current TILE chips a read barrier is implemented as a full barrier,
  290. but this may not be true in later versions of the architecture.
  291. See also arch_atomic_acquire_barrier() for the appropriate idiom to use
  292. to ensure no reads are lifted above an atomic lock instruction. */
  293. #define arch_atomic_read_barrier() arch_atomic_full_barrier()
  294. /* Write memory barrier.
  295. Ensure that all writes by this processor that occurred prior to the
  296. write memory barrier have completed, and that no writes that occur
  297. after the write memory barrier on this processor are initiated
  298. before the barrier.
  299. On current TILE chips a write barrier is implemented as a full barrier,
  300. but this may not be true in later versions of the architecture.
  301. See also arch_atomic_release_barrier() for the appropriate idiom to use
  302. to ensure all writes are complete prior to an atomic unlock instruction. */
  303. #define arch_atomic_write_barrier() arch_atomic_full_barrier()
  304. /* Lock acquisition barrier.
  305. Ensure that no load operations that follow this macro in the
  306. program can issue prior to the barrier. Without such a barrier,
  307. the compiler can reorder them to issue earlier, or the hardware can
  308. issue them speculatively. The latter is not currently done in the
  309. Tile microarchitecture, but using this operation improves
  310. portability to future implementations.
  311. This operation is intended to be used as part of the "acquire"
  312. path for locking, that is, when entering a critical section.
  313. This should be done after the atomic operation that actually
  314. acquires the lock, and in conjunction with a "control dependency"
  315. that checks the atomic operation result to see if the lock was
  316. in fact acquired. See the arch_atomic_read_barrier() macro
  317. for a heavier-weight barrier to use in certain unusual constructs,
  318. or arch_atomic_acquire_barrier_value() if no control dependency exists. */
  319. #define arch_atomic_acquire_barrier() arch_atomic_compiler_barrier()
  320. /* Lock release barrier.
  321. Ensure that no store operations that precede this macro in the
  322. program complete subsequent to the barrier. Without such a
  323. barrier, the compiler can reorder stores to issue later, or stores
  324. can be still outstanding in the memory network.
  325. This operation is intended to be used as part of the "release" path
  326. for locking, that is, when leaving a critical section. This should
  327. be done before the operation (such as a store of zero) that
  328. actually releases the lock. */
  329. #define arch_atomic_release_barrier() arch_atomic_write_barrier()
  330. /* Barrier until the read of a particular value is complete.
  331. This is occasionally useful when constructing certain locking
  332. scenarios. For example, you might write a routine that issues an
  333. atomic instruction to enter a critical section, then reads one or
  334. more values within the critical section without checking to see if
  335. the critical section was in fact acquired, and only later checks
  336. the atomic instruction result to see if the lock was acquired. If
  337. so the routine could properly release the lock and know that the
  338. values that were read were valid.
  339. In this scenario, it is required to wait for the result of the
  340. atomic instruction, even if the value itself is not checked. This
  341. guarantees that if the atomic instruction succeeded in taking the lock,
  342. the lock was held before any reads in the critical section issued. */
  343. #define arch_atomic_acquire_barrier_value(val) \
  344. __asm__ __volatile__("move %0, %0" :: "r"(val))
  345. /* Access the given variable in memory exactly once.
  346. In some contexts, an algorithm may need to force access to memory,
  347. since otherwise the compiler may think it can optimize away a
  348. memory load or store; for example, in a loop when polling memory to
  349. see if another cpu has updated it yet. Generally this is only
  350. required for certain very carefully hand-tuned algorithms; using it
  351. unnecessarily may result in performance losses.
  352. A related use of this macro is to ensure that the compiler does not
  353. rematerialize the value of "x" by reloading it from memory
  354. unexpectedly; the "volatile" marking will prevent the compiler from
  355. being able to rematerialize. This is helpful if an algorithm needs
  356. to read a variable without locking, but needs it to have the same
  357. value if it ends up being used several times within the algorithm.
  358. Note that multiple uses of this macro are guaranteed to be ordered,
  359. i.e. the compiler will not reorder stores or loads that are wrapped
  360. in arch_atomic_access_once(). */
  361. #define arch_atomic_access_once(x) (*(volatile __typeof(x) *)&(x))
  362. #endif /* !_ATOMIC_H_ */