barrier.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __BARRIER_H
  3. #define __BARRIER_H
  4. #include <asm/compiler.h>
  5. #define mb() __asm__ __volatile__("mb": : :"memory")
  6. #define rmb() __asm__ __volatile__("mb": : :"memory")
  7. #define wmb() __asm__ __volatile__("wmb": : :"memory")
  8. /**
  9. * read_barrier_depends - Flush all pending reads that subsequents reads
  10. * depend on.
  11. *
  12. * No data-dependent reads from memory-like regions are ever reordered
  13. * over this barrier. All reads preceding this primitive are guaranteed
  14. * to access memory (but not necessarily other CPUs' caches) before any
  15. * reads following this primitive that depend on the data return by
  16. * any of the preceding reads. This primitive is much lighter weight than
  17. * rmb() on most CPUs, and is never heavier weight than is
  18. * rmb().
  19. *
  20. * These ordering constraints are respected by both the local CPU
  21. * and the compiler.
  22. *
  23. * Ordering is not guaranteed by anything other than these primitives,
  24. * not even by data dependencies. See the documentation for
  25. * memory_barrier() for examples and URLs to more information.
  26. *
  27. * For example, the following code would force ordering (the initial
  28. * value of "a" is zero, "b" is one, and "p" is "&a"):
  29. *
  30. * <programlisting>
  31. * CPU 0 CPU 1
  32. *
  33. * b = 2;
  34. * memory_barrier();
  35. * p = &b; q = p;
  36. * read_barrier_depends();
  37. * d = *q;
  38. * </programlisting>
  39. *
  40. * because the read of "*q" depends on the read of "p" and these
  41. * two reads are separated by a read_barrier_depends(). However,
  42. * the following code, with the same initial values for "a" and "b":
  43. *
  44. * <programlisting>
  45. * CPU 0 CPU 1
  46. *
  47. * a = 2;
  48. * memory_barrier();
  49. * b = 3; y = b;
  50. * read_barrier_depends();
  51. * x = a;
  52. * </programlisting>
  53. *
  54. * does not enforce ordering, since there is no data dependency between
  55. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  56. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  57. * in cases like this where there are no data dependencies.
  58. */
  59. #define read_barrier_depends() __asm__ __volatile__("mb": : :"memory")
  60. #ifdef CONFIG_SMP
  61. #define __ASM_SMP_MB "\tmb\n"
  62. #else
  63. #define __ASM_SMP_MB
  64. #endif
  65. #include <asm-generic/barrier.h>
  66. #endif /* __BARRIER_H */