barrier.h 4.9 KB

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