barrier.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/include/asm/barrier.h
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. */
  7. #ifndef __ASM_BARRIER_H
  8. #define __ASM_BARRIER_H
  9. #ifndef __ASSEMBLY__
  10. #include <linux/kasan-checks.h>
  11. #include <asm/alternative-macros.h>
  12. #define __nops(n) ".rept " #n "\nnop\n.endr\n"
  13. #define nops(n) asm volatile(__nops(n))
  14. #define sev() asm volatile("sev" : : : "memory")
  15. #define wfe() asm volatile("wfe" : : : "memory")
  16. #define wfet(val) asm volatile("msr s0_3_c1_c0_0, %0" \
  17. : : "r" (val) : "memory")
  18. #define wfi() asm volatile("wfi" : : : "memory")
  19. #define wfit(val) asm volatile("msr s0_3_c1_c0_1, %0" \
  20. : : "r" (val) : "memory")
  21. #define isb() asm volatile("isb" : : : "memory")
  22. #define dmb(opt) asm volatile("dmb " #opt : : : "memory")
  23. #define dsb(opt) asm volatile("dsb " #opt : : : "memory")
  24. #define psb_csync() asm volatile("hint #17" : : : "memory")
  25. #define __tsb_csync() asm volatile("hint #18" : : : "memory")
  26. #define csdb() asm volatile("hint #20" : : : "memory")
  27. /*
  28. * Data Gathering Hint:
  29. * This instruction prevents merging memory accesses with Normal-NC or
  30. * Device-GRE attributes before the hint instruction with any memory accesses
  31. * appearing after the hint instruction.
  32. */
  33. #define dgh() asm volatile("hint #6" : : : "memory")
  34. #define spec_bar() asm volatile(ALTERNATIVE("dsb nsh\nisb\n", \
  35. SB_BARRIER_INSN"nop\n", \
  36. ARM64_HAS_SB))
  37. #ifdef CONFIG_ARM64_PSEUDO_NMI
  38. #define pmr_sync() \
  39. do { \
  40. asm volatile( \
  41. ALTERNATIVE_CB("dsb sy", \
  42. ARM64_HAS_GIC_PRIO_RELAXED_SYNC, \
  43. alt_cb_patch_nops) \
  44. ); \
  45. } while(0)
  46. #else
  47. #define pmr_sync() do {} while (0)
  48. #endif
  49. #define __mb() dsb(sy)
  50. #define __rmb() dsb(ld)
  51. #define __wmb() dsb(st)
  52. #define __dma_mb() dmb(osh)
  53. #define __dma_rmb() dmb(oshld)
  54. #define __dma_wmb() dmb(oshst)
  55. #define io_stop_wc() dgh()
  56. #define tsb_csync() \
  57. do { \
  58. /* \
  59. * CPUs affected by Arm Erratum 2054223 or 2067961 needs \
  60. * another TSB to ensure the trace is flushed. The barriers \
  61. * don't have to be strictly back to back, as long as the \
  62. * CPU is in trace prohibited state. \
  63. */ \
  64. if (cpus_have_final_cap(ARM64_WORKAROUND_TSB_FLUSH_FAILURE)) \
  65. __tsb_csync(); \
  66. __tsb_csync(); \
  67. } while (0)
  68. /*
  69. * Generate a mask for array_index__nospec() that is ~0UL when 0 <= idx < sz
  70. * and 0 otherwise.
  71. */
  72. #define array_index_mask_nospec array_index_mask_nospec
  73. static inline unsigned long array_index_mask_nospec(unsigned long idx,
  74. unsigned long sz)
  75. {
  76. unsigned long mask;
  77. asm volatile(
  78. " cmp %1, %2\n"
  79. " sbc %0, xzr, xzr\n"
  80. : "=r" (mask)
  81. : "r" (idx), "Ir" (sz)
  82. : "cc");
  83. csdb();
  84. return mask;
  85. }
  86. /*
  87. * Ensure that reads of the counter are treated the same as memory reads
  88. * for the purposes of ordering by subsequent memory barriers.
  89. *
  90. * This insanity brought to you by speculative system register reads,
  91. * out-of-order memory accesses, sequence locks and Thomas Gleixner.
  92. *
  93. * https://lore.kernel.org/r/alpine.DEB.2.21.1902081950260.1662@nanos.tec.linutronix.de/
  94. */
  95. #define arch_counter_enforce_ordering(val) do { \
  96. u64 tmp, _val = (val); \
  97. \
  98. asm volatile( \
  99. " eor %0, %1, %1\n" \
  100. " add %0, sp, %0\n" \
  101. " ldr xzr, [%0]" \
  102. : "=r" (tmp) : "r" (_val)); \
  103. } while (0)
  104. #define __smp_mb() dmb(ish)
  105. #define __smp_rmb() dmb(ishld)
  106. #define __smp_wmb() dmb(ishst)
  107. #define __smp_store_release(p, v) \
  108. do { \
  109. typeof(p) __p = (p); \
  110. union { __unqual_scalar_typeof(*p) __val; char __c[1]; } __u = \
  111. { .__val = (__force __unqual_scalar_typeof(*p)) (v) }; \
  112. compiletime_assert_atomic_type(*p); \
  113. kasan_check_write(__p, sizeof(*p)); \
  114. switch (sizeof(*p)) { \
  115. case 1: \
  116. asm volatile ("stlrb %w1, %0" \
  117. : "=Q" (*__p) \
  118. : "rZ" (*(__u8 *)__u.__c) \
  119. : "memory"); \
  120. break; \
  121. case 2: \
  122. asm volatile ("stlrh %w1, %0" \
  123. : "=Q" (*__p) \
  124. : "rZ" (*(__u16 *)__u.__c) \
  125. : "memory"); \
  126. break; \
  127. case 4: \
  128. asm volatile ("stlr %w1, %0" \
  129. : "=Q" (*__p) \
  130. : "rZ" (*(__u32 *)__u.__c) \
  131. : "memory"); \
  132. break; \
  133. case 8: \
  134. asm volatile ("stlr %x1, %0" \
  135. : "=Q" (*__p) \
  136. : "rZ" (*(__u64 *)__u.__c) \
  137. : "memory"); \
  138. break; \
  139. } \
  140. } while (0)
  141. #define __smp_load_acquire(p) \
  142. ({ \
  143. union { __unqual_scalar_typeof(*p) __val; char __c[1]; } __u; \
  144. typeof(p) __p = (p); \
  145. compiletime_assert_atomic_type(*p); \
  146. kasan_check_read(__p, sizeof(*p)); \
  147. switch (sizeof(*p)) { \
  148. case 1: \
  149. asm volatile ("ldarb %w0, %1" \
  150. : "=r" (*(__u8 *)__u.__c) \
  151. : "Q" (*__p) : "memory"); \
  152. break; \
  153. case 2: \
  154. asm volatile ("ldarh %w0, %1" \
  155. : "=r" (*(__u16 *)__u.__c) \
  156. : "Q" (*__p) : "memory"); \
  157. break; \
  158. case 4: \
  159. asm volatile ("ldar %w0, %1" \
  160. : "=r" (*(__u32 *)__u.__c) \
  161. : "Q" (*__p) : "memory"); \
  162. break; \
  163. case 8: \
  164. asm volatile ("ldar %0, %1" \
  165. : "=r" (*(__u64 *)__u.__c) \
  166. : "Q" (*__p) : "memory"); \
  167. break; \
  168. } \
  169. (typeof(*p))__u.__val; \
  170. })
  171. #define smp_cond_load_relaxed(ptr, cond_expr) \
  172. ({ \
  173. typeof(ptr) __PTR = (ptr); \
  174. __unqual_scalar_typeof(*ptr) VAL; \
  175. for (;;) { \
  176. VAL = READ_ONCE(*__PTR); \
  177. if (cond_expr) \
  178. break; \
  179. __cmpwait_relaxed(__PTR, VAL); \
  180. } \
  181. (typeof(*ptr))VAL; \
  182. })
  183. #define smp_cond_load_acquire(ptr, cond_expr) \
  184. ({ \
  185. typeof(ptr) __PTR = (ptr); \
  186. __unqual_scalar_typeof(*ptr) VAL; \
  187. for (;;) { \
  188. VAL = smp_load_acquire(__PTR); \
  189. if (cond_expr) \
  190. break; \
  191. __cmpwait_relaxed(__PTR, VAL); \
  192. } \
  193. (typeof(*ptr))VAL; \
  194. })
  195. #include <asm-generic/barrier.h>
  196. #endif /* __ASSEMBLY__ */
  197. #endif /* __ASM_BARRIER_H */