barrier.h 5.7 KB

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