barrier.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifndef _ASM_TILE_BARRIER_H
  15. #define _ASM_TILE_BARRIER_H
  16. #ifndef __ASSEMBLY__
  17. #include <linux/types.h>
  18. #include <arch/chip.h>
  19. #include <arch/spr_def.h>
  20. #include <asm/timex.h>
  21. /*
  22. * read_barrier_depends - Flush all pending reads that subsequents reads
  23. * depend on.
  24. *
  25. * No data-dependent reads from memory-like regions are ever reordered
  26. * over this barrier. All reads preceding this primitive are guaranteed
  27. * to access memory (but not necessarily other CPUs' caches) before any
  28. * reads following this primitive that depend on the data return by
  29. * any of the preceding reads. This primitive is much lighter weight than
  30. * rmb() on most CPUs, and is never heavier weight than is
  31. * rmb().
  32. *
  33. * These ordering constraints are respected by both the local CPU
  34. * and the compiler.
  35. *
  36. * Ordering is not guaranteed by anything other than these primitives,
  37. * not even by data dependencies. See the documentation for
  38. * memory_barrier() for examples and URLs to more information.
  39. *
  40. * For example, the following code would force ordering (the initial
  41. * value of "a" is zero, "b" is one, and "p" is "&a"):
  42. *
  43. * <programlisting>
  44. * CPU 0 CPU 1
  45. *
  46. * b = 2;
  47. * memory_barrier();
  48. * p = &b; q = p;
  49. * read_barrier_depends();
  50. * d = *q;
  51. * </programlisting>
  52. *
  53. * because the read of "*q" depends on the read of "p" and these
  54. * two reads are separated by a read_barrier_depends(). However,
  55. * the following code, with the same initial values for "a" and "b":
  56. *
  57. * <programlisting>
  58. * CPU 0 CPU 1
  59. *
  60. * a = 2;
  61. * memory_barrier();
  62. * b = 3; y = b;
  63. * read_barrier_depends();
  64. * x = a;
  65. * </programlisting>
  66. *
  67. * does not enforce ordering, since there is no data dependency between
  68. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  69. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  70. * in cases like this where there are no data dependencies.
  71. */
  72. #define read_barrier_depends() do { } while (0)
  73. #define __sync() __insn_mf()
  74. #include <hv/syscall_public.h>
  75. /*
  76. * Issue an uncacheable load to each memory controller, then
  77. * wait until those loads have completed.
  78. */
  79. static inline void __mb_incoherent(void)
  80. {
  81. long clobber_r10;
  82. asm volatile("swint2"
  83. : "=R10" (clobber_r10)
  84. : "R10" (HV_SYS_fence_incoherent)
  85. : "r0", "r1", "r2", "r3", "r4",
  86. "r5", "r6", "r7", "r8", "r9",
  87. "r11", "r12", "r13", "r14",
  88. "r15", "r16", "r17", "r18", "r19",
  89. "r20", "r21", "r22", "r23", "r24",
  90. "r25", "r26", "r27", "r28", "r29");
  91. }
  92. /* Fence to guarantee visibility of stores to incoherent memory. */
  93. static inline void
  94. mb_incoherent(void)
  95. {
  96. __insn_mf();
  97. {
  98. #if CHIP_HAS_TILE_WRITE_PENDING()
  99. const unsigned long WRITE_TIMEOUT_CYCLES = 400;
  100. unsigned long start = get_cycles_low();
  101. do {
  102. if (__insn_mfspr(SPR_TILE_WRITE_PENDING) == 0)
  103. return;
  104. } while ((get_cycles_low() - start) < WRITE_TIMEOUT_CYCLES);
  105. #endif /* CHIP_HAS_TILE_WRITE_PENDING() */
  106. (void) __mb_incoherent();
  107. }
  108. }
  109. #define fast_wmb() __sync()
  110. #define fast_rmb() __sync()
  111. #define fast_mb() __sync()
  112. #define fast_iob() mb_incoherent()
  113. #define wmb() fast_wmb()
  114. #define rmb() fast_rmb()
  115. #define mb() fast_mb()
  116. #define iob() fast_iob()
  117. #ifdef CONFIG_SMP
  118. #define smp_mb() mb()
  119. #define smp_rmb() rmb()
  120. #define smp_wmb() wmb()
  121. #define smp_read_barrier_depends() read_barrier_depends()
  122. #else
  123. #define smp_mb() barrier()
  124. #define smp_rmb() barrier()
  125. #define smp_wmb() barrier()
  126. #define smp_read_barrier_depends() do { } while (0)
  127. #endif
  128. #define set_mb(var, value) \
  129. do { var = value; mb(); } while (0)
  130. #endif /* !__ASSEMBLY__ */
  131. #endif /* _ASM_TILE_BARRIER_H */