barrier.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /*
  9. * Memory barrier.
  10. * The sync instruction guarantees that all memory accesses initiated
  11. * by this processor have been performed (with respect to all other
  12. * mechanisms that access memory). The eieio instruction is a barrier
  13. * providing an ordering (separately) for (a) cacheable stores and (b)
  14. * loads and stores to non-cacheable memory (e.g. I/O devices).
  15. *
  16. * mb() prevents loads and stores being reordered across this point.
  17. * rmb() prevents loads being reordered across this point.
  18. * wmb() prevents stores being reordered across this point.
  19. * read_barrier_depends() prevents data-dependent loads being reordered
  20. * across this point (nop on PPC).
  21. *
  22. * *mb() variants without smp_ prefix must order all types of memory
  23. * operations with one another. sync is the only instruction sufficient
  24. * to do this.
  25. *
  26. * For the smp_ barriers, ordering is for cacheable memory operations
  27. * only. We have to use the sync instruction for smp_mb(), since lwsync
  28. * doesn't order loads with respect to previous stores. Lwsync can be
  29. * used for smp_rmb() and smp_wmb().
  30. *
  31. * However, on CPUs that don't support lwsync, lwsync actually maps to a
  32. * heavy-weight sync, so smp_wmb() can be a lighter-weight eieio.
  33. */
  34. #define mb() __asm__ __volatile__ ("sync" : : : "memory")
  35. #define rmb() __asm__ __volatile__ ("sync" : : : "memory")
  36. #define wmb() __asm__ __volatile__ ("sync" : : : "memory")
  37. /* The sub-arch has lwsync */
  38. #if defined(__powerpc64__) || defined(CONFIG_PPC_E500MC)
  39. # define SMPWMB LWSYNC
  40. #else
  41. # define SMPWMB eieio
  42. #endif
  43. #define __lwsync() __asm__ __volatile__ (stringify_in_c(LWSYNC) : : :"memory")
  44. #define dma_rmb() __lwsync()
  45. #define dma_wmb() __asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
  46. #define __smp_lwsync() __lwsync()
  47. #define __smp_mb() mb()
  48. #define __smp_rmb() __lwsync()
  49. #define __smp_wmb() __asm__ __volatile__ (stringify_in_c(SMPWMB) : : :"memory")
  50. /*
  51. * This is a barrier which prevents following instructions from being
  52. * started until the value of the argument x is known. For example, if
  53. * x is a variable loaded from memory, this prevents following
  54. * instructions from being executed until the load has been performed.
  55. */
  56. #define data_barrier(x) \
  57. asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
  58. #define __smp_store_release(p, v) \
  59. do { \
  60. compiletime_assert_atomic_type(*p); \
  61. __smp_lwsync(); \
  62. WRITE_ONCE(*p, v); \
  63. } while (0)
  64. #define __smp_load_acquire(p) \
  65. ({ \
  66. typeof(*p) ___p1 = READ_ONCE(*p); \
  67. compiletime_assert_atomic_type(*p); \
  68. __smp_lwsync(); \
  69. ___p1; \
  70. })
  71. #ifdef CONFIG_PPC_BOOK3S_64
  72. /*
  73. * Prevent execution of subsequent instructions until preceding branches have
  74. * been fully resolved and are no longer executing speculatively.
  75. */
  76. #define barrier_nospec_asm NOSPEC_BARRIER_FIXUP_SECTION; nop
  77. // This also acts as a compiler barrier due to the memory clobber.
  78. #define barrier_nospec() asm (stringify_in_c(barrier_nospec_asm) ::: "memory")
  79. #else /* !CONFIG_PPC_BOOK3S_64 */
  80. #define barrier_nospec_asm
  81. #define barrier_nospec()
  82. #endif
  83. #include <asm-generic/barrier.h>
  84. #endif /* _ASM_POWERPC_BARRIER_H */