barrier.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 __ASSEMBLER__
  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. #define gsb_ack() asm volatile(GSB_ACK_BARRIER_INSN : : : "memory")
  38. #define gsb_sys() asm volatile(GSB_SYS_BARRIER_INSN : : : "memory")
  39. #ifdef CONFIG_ARM64_PSEUDO_NMI
  40. #define pmr_sync() \
  41. do { \
  42. asm volatile( \
  43. ALTERNATIVE_CB("dsb sy", \
  44. ARM64_HAS_GIC_PRIO_RELAXED_SYNC, \
  45. alt_cb_patch_nops) \
  46. ); \
  47. } while(0)
  48. #else
  49. #define pmr_sync() do {} while (0)
  50. #endif
  51. #define __mb() dsb(sy)
  52. #define __rmb() dsb(ld)
  53. #define __wmb() dsb(st)
  54. #define __dma_mb() dmb(osh)
  55. #define __dma_rmb() dmb(oshld)
  56. #define __dma_wmb() dmb(oshst)
  57. #define io_stop_wc() dgh()
  58. #define tsb_csync() \
  59. do { \
  60. /* \
  61. * CPUs affected by Arm Erratum 2054223 or 2067961 needs \
  62. * another TSB to ensure the trace is flushed. The barriers \
  63. * don't have to be strictly back to back, as long as the \
  64. * CPU is in trace prohibited state. \
  65. */ \
  66. if (cpus_have_final_cap(ARM64_WORKAROUND_TSB_FLUSH_FAILURE)) \
  67. __tsb_csync(); \
  68. __tsb_csync(); \
  69. } while (0)
  70. /*
  71. * Generate a mask for array_index__nospec() that is ~0UL when 0 <= idx < sz
  72. * and 0 otherwise.
  73. */
  74. #define array_index_mask_nospec array_index_mask_nospec
  75. static inline unsigned long array_index_mask_nospec(unsigned long idx,
  76. unsigned long sz)
  77. {
  78. unsigned long mask;
  79. asm volatile(
  80. " cmp %1, %2\n"
  81. " sbc %0, xzr, xzr\n"
  82. : "=r" (mask)
  83. : "r" (idx), "Ir" (sz)
  84. : "cc");
  85. csdb();
  86. return mask;
  87. }
  88. /*
  89. * Ensure that reads of the counter are treated the same as memory reads
  90. * for the purposes of ordering by subsequent memory barriers.
  91. *
  92. * This insanity brought to you by speculative system register reads,
  93. * out-of-order memory accesses, sequence locks and Thomas Gleixner.
  94. *
  95. * https://lore.kernel.org/r/alpine.DEB.2.21.1902081950260.1662@nanos.tec.linutronix.de/
  96. */
  97. #define arch_counter_enforce_ordering(val) do { \
  98. u64 tmp, _val = (val); \
  99. \
  100. asm volatile( \
  101. " eor %0, %1, %1\n" \
  102. " add %0, sp, %0\n" \
  103. " ldr xzr, [%0]" \
  104. : "=r" (tmp) : "r" (_val)); \
  105. } while (0)
  106. #define __smp_mb() dmb(ish)
  107. #define __smp_rmb() dmb(ishld)
  108. #define __smp_wmb() dmb(ishst)
  109. #define __smp_store_release(p, v) \
  110. do { \
  111. typeof(p) __p = (p); \
  112. union { __unqual_scalar_typeof(*p) __val; char __c[1]; } __u = \
  113. { .__val = (__force __unqual_scalar_typeof(*p)) (v) }; \
  114. compiletime_assert_atomic_type(*p); \
  115. kasan_check_write(__p, sizeof(*p)); \
  116. switch (sizeof(*p)) { \
  117. case 1: \
  118. asm volatile ("stlrb %w1, %0" \
  119. : "=Q" (*__p) \
  120. : "rZ" (*(__u8 *)__u.__c) \
  121. : "memory"); \
  122. break; \
  123. case 2: \
  124. asm volatile ("stlrh %w1, %0" \
  125. : "=Q" (*__p) \
  126. : "rZ" (*(__u16 *)__u.__c) \
  127. : "memory"); \
  128. break; \
  129. case 4: \
  130. asm volatile ("stlr %w1, %0" \
  131. : "=Q" (*__p) \
  132. : "rZ" (*(__u32 *)__u.__c) \
  133. : "memory"); \
  134. break; \
  135. case 8: \
  136. asm volatile ("stlr %x1, %0" \
  137. : "=Q" (*__p) \
  138. : "rZ" (*(__u64 *)__u.__c) \
  139. : "memory"); \
  140. break; \
  141. } \
  142. } while (0)
  143. #define __smp_load_acquire(p) \
  144. ({ \
  145. union { __unqual_scalar_typeof(*p) __val; char __c[1]; } __u; \
  146. typeof(p) __p = (p); \
  147. compiletime_assert_atomic_type(*p); \
  148. kasan_check_read(__p, sizeof(*p)); \
  149. switch (sizeof(*p)) { \
  150. case 1: \
  151. asm volatile ("ldarb %w0, %1" \
  152. : "=r" (*(__u8 *)__u.__c) \
  153. : "Q" (*__p) : "memory"); \
  154. break; \
  155. case 2: \
  156. asm volatile ("ldarh %w0, %1" \
  157. : "=r" (*(__u16 *)__u.__c) \
  158. : "Q" (*__p) : "memory"); \
  159. break; \
  160. case 4: \
  161. asm volatile ("ldar %w0, %1" \
  162. : "=r" (*(__u32 *)__u.__c) \
  163. : "Q" (*__p) : "memory"); \
  164. break; \
  165. case 8: \
  166. asm volatile ("ldar %0, %1" \
  167. : "=r" (*(__u64 *)__u.__c) \
  168. : "Q" (*__p) : "memory"); \
  169. break; \
  170. } \
  171. (typeof(*p))__u.__val; \
  172. })
  173. #define smp_cond_load_relaxed(ptr, cond_expr) \
  174. ({ \
  175. typeof(ptr) __PTR = (ptr); \
  176. __unqual_scalar_typeof(*ptr) VAL; \
  177. for (;;) { \
  178. VAL = READ_ONCE(*__PTR); \
  179. if (cond_expr) \
  180. break; \
  181. __cmpwait_relaxed(__PTR, VAL); \
  182. } \
  183. (typeof(*ptr))VAL; \
  184. })
  185. #define smp_cond_load_acquire(ptr, cond_expr) \
  186. ({ \
  187. typeof(ptr) __PTR = (ptr); \
  188. __unqual_scalar_typeof(*ptr) VAL; \
  189. for (;;) { \
  190. VAL = smp_load_acquire(__PTR); \
  191. if (cond_expr) \
  192. break; \
  193. __cmpwait_relaxed(__PTR, VAL); \
  194. } \
  195. (typeof(*ptr))VAL; \
  196. })
  197. #include <asm-generic/barrier.h>
  198. #endif /* __ASSEMBLER__ */
  199. #endif /* __ASM_BARRIER_H */