barrier.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  4. */
  5. #ifndef _ASM_POWERPC_BARRIER_H
  6. #define _ASM_POWERPC_BARRIER_H
  7. #include <asm/asm-const.h>
  8. #ifndef __ASSEMBLY__
  9. #include <asm/ppc-opcode.h>
  10. #endif
  11. /*
  12. * Memory barrier.
  13. * The sync instruction guarantees that all memory accesses initiated
  14. * by this processor have been performed (with respect to all other
  15. * mechanisms that access memory). The eieio instruction is a barrier
  16. * providing an ordering (separately) for (a) cacheable stores and (b)
  17. * loads and stores to non-cacheable memory (e.g. I/O devices).
  18. *
  19. * mb() prevents loads and stores being reordered across this point.
  20. * rmb() prevents loads being reordered across this point.
  21. * wmb() prevents stores being reordered across this point.
  22. *
  23. * *mb() variants without smp_ prefix must order all types of memory
  24. * operations with one another. sync is the only instruction sufficient
  25. * to do this.
  26. *
  27. * For the smp_ barriers, ordering is for cacheable memory operations
  28. * only. We have to use the sync instruction for smp_mb(), since lwsync
  29. * doesn't order loads with respect to previous stores. Lwsync can be
  30. * used for smp_rmb() and smp_wmb().
  31. *
  32. * However, on CPUs that don't support lwsync, lwsync actually maps to a
  33. * heavy-weight sync, so smp_wmb() can be a lighter-weight eieio.
  34. */
  35. #define mb() __asm__ __volatile__ ("sync" : : : "memory")
  36. #define rmb() __asm__ __volatile__ ("sync" : : : "memory")
  37. #define wmb() __asm__ __volatile__ ("sync" : : : "memory")
  38. /* The sub-arch has lwsync */
  39. #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_E500MC)
  40. # define SMPWMB LWSYNC
  41. #elif defined(CONFIG_BOOKE)
  42. # define SMPWMB mbar
  43. #else
  44. # define SMPWMB eieio
  45. #endif
  46. /* clang defines this macro for a builtin, which will not work with runtime patching */
  47. #undef __lwsync
  48. #define __lwsync() __asm__ __volatile__ (stringify_in_c(LWSYNC) : : :"memory")
  49. #define dma_rmb() __lwsync()
  50. #define dma_wmb() __asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
  51. #define __smp_lwsync() __lwsync()
  52. #define __smp_mb() mb()
  53. #define __smp_rmb() __lwsync()
  54. #define __smp_wmb() __asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
  55. /*
  56. * This is a barrier which prevents following instructions from being
  57. * started until the value of the argument x is known. For example, if
  58. * x is a variable loaded from memory, this prevents following
  59. * instructions from being executed until the load has been performed.
  60. */
  61. #define data_barrier(x) \
  62. asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
  63. #define __smp_store_release(p, v) \
  64. do { \
  65. compiletime_assert_atomic_type(*p); \
  66. __smp_lwsync(); \
  67. WRITE_ONCE(*p, v); \
  68. } while (0)
  69. #define __smp_load_acquire(p) \
  70. ({ \
  71. typeof(*p) ___p1 = READ_ONCE(*p); \
  72. compiletime_assert_atomic_type(*p); \
  73. __smp_lwsync(); \
  74. ___p1; \
  75. })
  76. #ifdef CONFIG_PPC_BOOK3S_64
  77. #define NOSPEC_BARRIER_SLOT nop
  78. #elif defined(CONFIG_PPC_E500)
  79. #define NOSPEC_BARRIER_SLOT nop; nop
  80. #endif
  81. #ifdef CONFIG_PPC_BARRIER_NOSPEC
  82. /*
  83. * Prevent execution of subsequent instructions until preceding branches have
  84. * been fully resolved and are no longer executing speculatively.
  85. */
  86. #define barrier_nospec_asm NOSPEC_BARRIER_FIXUP_SECTION; NOSPEC_BARRIER_SLOT
  87. // This also acts as a compiler barrier due to the memory clobber.
  88. #define barrier_nospec() asm (stringify_in_c(barrier_nospec_asm) ::: "memory")
  89. #else /* !CONFIG_PPC_BARRIER_NOSPEC */
  90. #define barrier_nospec_asm
  91. #define barrier_nospec()
  92. #endif /* CONFIG_PPC_BARRIER_NOSPEC */
  93. /*
  94. * pmem_wmb() ensures that all stores for which the modification
  95. * are written to persistent storage by preceding dcbfps/dcbstps
  96. * instructions have updated persistent storage before any data
  97. * access or data transfer caused by subsequent instructions is
  98. * initiated.
  99. */
  100. #define pmem_wmb() __asm__ __volatile__(PPC_PHWSYNC ::: "memory")
  101. #include <asm-generic/barrier.h>
  102. #endif /* _ASM_POWERPC_BARRIER_H */