barrier.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2006 by Ralf Baechle (ralf@linux-mips.org)
  7. */
  8. #ifndef __ASM_BARRIER_H
  9. #define __ASM_BARRIER_H
  10. /*
  11. * read_barrier_depends - Flush all pending reads that subsequents reads
  12. * depend on.
  13. *
  14. * No data-dependent reads from memory-like regions are ever reordered
  15. * over this barrier. All reads preceding this primitive are guaranteed
  16. * to access memory (but not necessarily other CPUs' caches) before any
  17. * reads following this primitive that depend on the data return by
  18. * any of the preceding reads. This primitive is much lighter weight than
  19. * rmb() on most CPUs, and is never heavier weight than is
  20. * rmb().
  21. *
  22. * These ordering constraints are respected by both the local CPU
  23. * and the compiler.
  24. *
  25. * Ordering is not guaranteed by anything other than these primitives,
  26. * not even by data dependencies. See the documentation for
  27. * memory_barrier() for examples and URLs to more information.
  28. *
  29. * For example, the following code would force ordering (the initial
  30. * value of "a" is zero, "b" is one, and "p" is "&a"):
  31. *
  32. * <programlisting>
  33. * CPU 0 CPU 1
  34. *
  35. * b = 2;
  36. * memory_barrier();
  37. * p = &b; q = p;
  38. * read_barrier_depends();
  39. * d = *q;
  40. * </programlisting>
  41. *
  42. * because the read of "*q" depends on the read of "p" and these
  43. * two reads are separated by a read_barrier_depends(). However,
  44. * the following code, with the same initial values for "a" and "b":
  45. *
  46. * <programlisting>
  47. * CPU 0 CPU 1
  48. *
  49. * a = 2;
  50. * memory_barrier();
  51. * b = 3; y = b;
  52. * read_barrier_depends();
  53. * x = a;
  54. * </programlisting>
  55. *
  56. * does not enforce ordering, since there is no data dependency between
  57. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  58. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  59. * in cases like this where there are no data dependencies.
  60. */
  61. #define read_barrier_depends() do { } while(0)
  62. #define smp_read_barrier_depends() do { } while(0)
  63. #ifdef CONFIG_CPU_HAS_SYNC
  64. #define __sync() \
  65. __asm__ __volatile__( \
  66. ".set push\n\t" \
  67. ".set noreorder\n\t" \
  68. ".set mips2\n\t" \
  69. "sync\n\t" \
  70. ".set pop" \
  71. : /* no output */ \
  72. : /* no input */ \
  73. : "memory")
  74. #else
  75. #define __sync() do { } while(0)
  76. #endif
  77. #define __fast_iob() \
  78. __asm__ __volatile__( \
  79. ".set push\n\t" \
  80. ".set noreorder\n\t" \
  81. "lw $0,%0\n\t" \
  82. "nop\n\t" \
  83. ".set pop" \
  84. : /* no output */ \
  85. : "m" (*(int *)CKSEG1) \
  86. : "memory")
  87. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  88. # define OCTEON_SYNCW_STR ".set push\n.set arch=octeon\nsyncw\nsyncw\n.set pop\n"
  89. # define __syncw() __asm__ __volatile__(OCTEON_SYNCW_STR : : : "memory")
  90. # define fast_wmb() __syncw()
  91. # define fast_rmb() barrier()
  92. # define fast_mb() __sync()
  93. # define fast_iob() do { } while (0)
  94. #else /* ! CONFIG_CPU_CAVIUM_OCTEON */
  95. # define fast_wmb() __sync()
  96. # define fast_rmb() __sync()
  97. # define fast_mb() __sync()
  98. # ifdef CONFIG_SGI_IP28
  99. # define fast_iob() \
  100. __asm__ __volatile__( \
  101. ".set push\n\t" \
  102. ".set noreorder\n\t" \
  103. "lw $0,%0\n\t" \
  104. "sync\n\t" \
  105. "lw $0,%0\n\t" \
  106. ".set pop" \
  107. : /* no output */ \
  108. : "m" (*(int *)CKSEG1ADDR(0x1fa00004)) \
  109. : "memory")
  110. # else
  111. # define fast_iob() \
  112. do { \
  113. __sync(); \
  114. __fast_iob(); \
  115. } while (0)
  116. # endif
  117. #endif /* CONFIG_CPU_CAVIUM_OCTEON */
  118. #ifdef CONFIG_CPU_HAS_WB
  119. #include <asm/wbflush.h>
  120. #define wmb() fast_wmb()
  121. #define rmb() fast_rmb()
  122. #define mb() wbflush()
  123. #define iob() wbflush()
  124. #else /* !CONFIG_CPU_HAS_WB */
  125. #define wmb() fast_wmb()
  126. #define rmb() fast_rmb()
  127. #define mb() fast_mb()
  128. #define iob() fast_iob()
  129. #endif /* !CONFIG_CPU_HAS_WB */
  130. #if defined(CONFIG_WEAK_ORDERING) && defined(CONFIG_SMP)
  131. # ifdef CONFIG_CPU_CAVIUM_OCTEON
  132. # define smp_mb() __sync()
  133. # define smp_rmb() barrier()
  134. # define smp_wmb() __syncw()
  135. # else
  136. # define smp_mb() __asm__ __volatile__("sync" : : :"memory")
  137. # define smp_rmb() __asm__ __volatile__("sync" : : :"memory")
  138. # define smp_wmb() __asm__ __volatile__("sync" : : :"memory")
  139. # endif
  140. #else
  141. #define smp_mb() barrier()
  142. #define smp_rmb() barrier()
  143. #define smp_wmb() barrier()
  144. #endif
  145. #if defined(CONFIG_WEAK_REORDERING_BEYOND_LLSC) && defined(CONFIG_SMP)
  146. #define __WEAK_LLSC_MB " sync \n"
  147. #else
  148. #define __WEAK_LLSC_MB " \n"
  149. #endif
  150. #define set_mb(var, value) \
  151. do { var = value; smp_mb(); } while (0)
  152. #define smp_llsc_mb() __asm__ __volatile__(__WEAK_LLSC_MB : : :"memory")
  153. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  154. #define smp_mb__before_llsc() smp_wmb()
  155. /* Cause previous writes to become visible on all CPUs as soon as possible */
  156. #define nudge_writes() __asm__ __volatile__(".set push\n\t" \
  157. ".set arch=octeon\n\t" \
  158. "syncw\n\t" \
  159. ".set pop" : : : "memory")
  160. #else
  161. #define smp_mb__before_llsc() smp_llsc_mb()
  162. #define nudge_writes() mb()
  163. #endif
  164. #endif /* __ASM_BARRIER_H */