barrier.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #if !CHIP_HAS_MF_WAITS_FOR_VICTIMS()
  75. #include <hv/syscall_public.h>
  76. /*
  77. * Issue an uncacheable load to each memory controller, then
  78. * wait until those loads have completed.
  79. */
  80. static inline void __mb_incoherent(void)
  81. {
  82. long clobber_r10;
  83. asm volatile("swint2"
  84. : "=R10" (clobber_r10)
  85. : "R10" (HV_SYS_fence_incoherent)
  86. : "r0", "r1", "r2", "r3", "r4",
  87. "r5", "r6", "r7", "r8", "r9",
  88. "r11", "r12", "r13", "r14",
  89. "r15", "r16", "r17", "r18", "r19",
  90. "r20", "r21", "r22", "r23", "r24",
  91. "r25", "r26", "r27", "r28", "r29");
  92. }
  93. #endif
  94. /* Fence to guarantee visibility of stores to incoherent memory. */
  95. static inline void
  96. mb_incoherent(void)
  97. {
  98. __insn_mf();
  99. #if !CHIP_HAS_MF_WAITS_FOR_VICTIMS()
  100. {
  101. #if CHIP_HAS_TILE_WRITE_PENDING()
  102. const unsigned long WRITE_TIMEOUT_CYCLES = 400;
  103. unsigned long start = get_cycles_low();
  104. do {
  105. if (__insn_mfspr(SPR_TILE_WRITE_PENDING) == 0)
  106. return;
  107. } while ((get_cycles_low() - start) < WRITE_TIMEOUT_CYCLES);
  108. #endif /* CHIP_HAS_TILE_WRITE_PENDING() */
  109. (void) __mb_incoherent();
  110. }
  111. #endif /* CHIP_HAS_MF_WAITS_FOR_VICTIMS() */
  112. }
  113. #define fast_wmb() __sync()
  114. #define fast_rmb() __sync()
  115. #define fast_mb() __sync()
  116. #define fast_iob() mb_incoherent()
  117. #define wmb() fast_wmb()
  118. #define rmb() fast_rmb()
  119. #define mb() fast_mb()
  120. #define iob() fast_iob()
  121. #ifdef CONFIG_SMP
  122. #define smp_mb() mb()
  123. #define smp_rmb() rmb()
  124. #define smp_wmb() wmb()
  125. #define smp_read_barrier_depends() read_barrier_depends()
  126. #else
  127. #define smp_mb() barrier()
  128. #define smp_rmb() barrier()
  129. #define smp_wmb() barrier()
  130. #define smp_read_barrier_depends() do { } while (0)
  131. #endif
  132. #define set_mb(var, value) \
  133. do { var = value; mb(); } while (0)
  134. #endif /* !__ASSEMBLY__ */
  135. #endif /* _ASM_TILE_BARRIER_H */