barrier.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef _ASM_X86_BARRIER_H
  2. #define _ASM_X86_BARRIER_H
  3. #include <asm/alternative.h>
  4. #include <asm/nops.h>
  5. /*
  6. * Force strict CPU ordering.
  7. * And yes, this is required on UP too when we're talking
  8. * to devices.
  9. */
  10. #ifdef CONFIG_X86_32
  11. /*
  12. * Some non-Intel clones support out of order store. wmb() ceases to be a
  13. * nop for these.
  14. */
  15. #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
  16. #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
  17. #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
  18. #else
  19. #define mb() asm volatile("mfence":::"memory")
  20. #define rmb() asm volatile("lfence":::"memory")
  21. #define wmb() asm volatile("sfence" ::: "memory")
  22. #endif
  23. /**
  24. * read_barrier_depends - Flush all pending reads that subsequents reads
  25. * depend on.
  26. *
  27. * No data-dependent reads from memory-like regions are ever reordered
  28. * over this barrier. All reads preceding this primitive are guaranteed
  29. * to access memory (but not necessarily other CPUs' caches) before any
  30. * reads following this primitive that depend on the data return by
  31. * any of the preceding reads. This primitive is much lighter weight than
  32. * rmb() on most CPUs, and is never heavier weight than is
  33. * rmb().
  34. *
  35. * These ordering constraints are respected by both the local CPU
  36. * and the compiler.
  37. *
  38. * Ordering is not guaranteed by anything other than these primitives,
  39. * not even by data dependencies. See the documentation for
  40. * memory_barrier() for examples and URLs to more information.
  41. *
  42. * For example, the following code would force ordering (the initial
  43. * value of "a" is zero, "b" is one, and "p" is "&a"):
  44. *
  45. * <programlisting>
  46. * CPU 0 CPU 1
  47. *
  48. * b = 2;
  49. * memory_barrier();
  50. * p = &b; q = p;
  51. * read_barrier_depends();
  52. * d = *q;
  53. * </programlisting>
  54. *
  55. * because the read of "*q" depends on the read of "p" and these
  56. * two reads are separated by a read_barrier_depends(). However,
  57. * the following code, with the same initial values for "a" and "b":
  58. *
  59. * <programlisting>
  60. * CPU 0 CPU 1
  61. *
  62. * a = 2;
  63. * memory_barrier();
  64. * b = 3; y = b;
  65. * read_barrier_depends();
  66. * x = a;
  67. * </programlisting>
  68. *
  69. * does not enforce ordering, since there is no data dependency between
  70. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  71. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  72. * in cases like this where there are no data dependencies.
  73. **/
  74. #define read_barrier_depends() do { } while (0)
  75. #ifdef CONFIG_SMP
  76. #define smp_mb() mb()
  77. #ifdef CONFIG_X86_PPRO_FENCE
  78. # define smp_rmb() rmb()
  79. #else
  80. # define smp_rmb() barrier()
  81. #endif
  82. #ifdef CONFIG_X86_OOSTORE
  83. # define smp_wmb() wmb()
  84. #else
  85. # define smp_wmb() barrier()
  86. #endif
  87. #define smp_read_barrier_depends() read_barrier_depends()
  88. #define set_mb(var, value) do { (void)xchg(&var, value); } while (0)
  89. #else
  90. #define smp_mb() barrier()
  91. #define smp_rmb() barrier()
  92. #define smp_wmb() barrier()
  93. #define smp_read_barrier_depends() do { } while (0)
  94. #define set_mb(var, value) do { var = value; barrier(); } while (0)
  95. #endif
  96. /*
  97. * Stop RDTSC speculation. This is needed when you need to use RDTSC
  98. * (or get_cycles or vread that possibly accesses the TSC) in a defined
  99. * code region.
  100. *
  101. * (Could use an alternative three way for this if there was one.)
  102. */
  103. static __always_inline void rdtsc_barrier(void)
  104. {
  105. alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
  106. alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
  107. }
  108. #endif /* _ASM_X86_BARRIER_H */