barrier.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #define fast_wmb() __sync()
  88. #define fast_rmb() __sync()
  89. #define fast_mb() __sync()
  90. #ifdef CONFIG_SGI_IP28
  91. #define fast_iob() \
  92. __asm__ __volatile__( \
  93. ".set push\n\t" \
  94. ".set noreorder\n\t" \
  95. "lw $0,%0\n\t" \
  96. "sync\n\t" \
  97. "lw $0,%0\n\t" \
  98. ".set pop" \
  99. : /* no output */ \
  100. : "m" (*(int *)CKSEG1ADDR(0x1fa00004)) \
  101. : "memory")
  102. #else
  103. #define fast_iob() \
  104. do { \
  105. __sync(); \
  106. __fast_iob(); \
  107. } while (0)
  108. #endif
  109. #ifdef CONFIG_CPU_HAS_WB
  110. #include <asm/wbflush.h>
  111. #define wmb() fast_wmb()
  112. #define rmb() fast_rmb()
  113. #define mb() wbflush()
  114. #define iob() wbflush()
  115. #else /* !CONFIG_CPU_HAS_WB */
  116. #define wmb() fast_wmb()
  117. #define rmb() fast_rmb()
  118. #define mb() fast_mb()
  119. #define iob() fast_iob()
  120. #endif /* !CONFIG_CPU_HAS_WB */
  121. #if defined(CONFIG_WEAK_ORDERING) && defined(CONFIG_SMP)
  122. #define __WEAK_ORDERING_MB " sync \n"
  123. #else
  124. #define __WEAK_ORDERING_MB " \n"
  125. #endif
  126. #if defined(CONFIG_WEAK_REORDERING_BEYOND_LLSC) && defined(CONFIG_SMP)
  127. #define __WEAK_LLSC_MB " sync \n"
  128. #else
  129. #define __WEAK_LLSC_MB " \n"
  130. #endif
  131. #define smp_mb() __asm__ __volatile__(__WEAK_ORDERING_MB : : :"memory")
  132. #define smp_rmb() __asm__ __volatile__(__WEAK_ORDERING_MB : : :"memory")
  133. #define smp_wmb() __asm__ __volatile__(__WEAK_ORDERING_MB : : :"memory")
  134. #define set_mb(var, value) \
  135. do { var = value; smp_mb(); } while (0)
  136. #define smp_llsc_mb() __asm__ __volatile__(__WEAK_LLSC_MB : : :"memory")
  137. #define smp_llsc_rmb() __asm__ __volatile__(__WEAK_LLSC_MB : : :"memory")
  138. #define smp_llsc_wmb() __asm__ __volatile__(__WEAK_LLSC_MB : : :"memory")
  139. #endif /* __ASM_BARRIER_H */