atomic_64.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2011 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. * Do not include directly; use <asm/atomic.h>.
  15. */
  16. #ifndef _ASM_TILE_ATOMIC_64_H
  17. #define _ASM_TILE_ATOMIC_64_H
  18. #ifndef __ASSEMBLY__
  19. #include <arch/spr_def.h>
  20. /* First, the 32-bit atomic ops that are "real" on our 64-bit platform. */
  21. #define atomic_set(v, i) ((v)->counter = (i))
  22. /*
  23. * The smp_mb() operations throughout are to support the fact that
  24. * Linux requires memory barriers before and after the operation,
  25. * on any routine which updates memory and returns a value.
  26. */
  27. static inline int atomic_cmpxchg(atomic_t *v, int o, int n)
  28. {
  29. int val;
  30. __insn_mtspr(SPR_CMPEXCH_VALUE, o);
  31. smp_mb(); /* barrier for proper semantics */
  32. val = __insn_cmpexch4((void *)&v->counter, n);
  33. smp_mb(); /* barrier for proper semantics */
  34. return val;
  35. }
  36. static inline int atomic_xchg(atomic_t *v, int n)
  37. {
  38. int val;
  39. smp_mb(); /* barrier for proper semantics */
  40. val = __insn_exch4((void *)&v->counter, n);
  41. smp_mb(); /* barrier for proper semantics */
  42. return val;
  43. }
  44. static inline void atomic_add(int i, atomic_t *v)
  45. {
  46. __insn_fetchadd4((void *)&v->counter, i);
  47. }
  48. static inline int atomic_add_return(int i, atomic_t *v)
  49. {
  50. int val;
  51. smp_mb(); /* barrier for proper semantics */
  52. val = __insn_fetchadd4((void *)&v->counter, i) + i;
  53. barrier(); /* the "+ i" above will wait on memory */
  54. return val;
  55. }
  56. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  57. {
  58. int guess, oldval = v->counter;
  59. do {
  60. if (oldval == u)
  61. break;
  62. guess = oldval;
  63. oldval = atomic_cmpxchg(v, guess, guess + a);
  64. } while (guess != oldval);
  65. return oldval != u;
  66. }
  67. /* Now the true 64-bit operations. */
  68. #define ATOMIC64_INIT(i) { (i) }
  69. #define atomic64_read(v) ((v)->counter)
  70. #define atomic64_set(v, i) ((v)->counter = (i))
  71. static inline long atomic64_cmpxchg(atomic64_t *v, long o, long n)
  72. {
  73. long val;
  74. smp_mb(); /* barrier for proper semantics */
  75. __insn_mtspr(SPR_CMPEXCH_VALUE, o);
  76. val = __insn_cmpexch((void *)&v->counter, n);
  77. smp_mb(); /* barrier for proper semantics */
  78. return val;
  79. }
  80. static inline long atomic64_xchg(atomic64_t *v, long n)
  81. {
  82. long val;
  83. smp_mb(); /* barrier for proper semantics */
  84. val = __insn_exch((void *)&v->counter, n);
  85. smp_mb(); /* barrier for proper semantics */
  86. return val;
  87. }
  88. static inline void atomic64_add(long i, atomic64_t *v)
  89. {
  90. __insn_fetchadd((void *)&v->counter, i);
  91. }
  92. static inline long atomic64_add_return(long i, atomic64_t *v)
  93. {
  94. int val;
  95. smp_mb(); /* barrier for proper semantics */
  96. val = __insn_fetchadd((void *)&v->counter, i) + i;
  97. barrier(); /* the "+ i" above will wait on memory */
  98. return val;
  99. }
  100. static inline long atomic64_add_unless(atomic64_t *v, long a, long u)
  101. {
  102. long guess, oldval = v->counter;
  103. do {
  104. if (oldval == u)
  105. break;
  106. guess = oldval;
  107. oldval = atomic64_cmpxchg(v, guess, guess + a);
  108. } while (guess != oldval);
  109. return oldval != u;
  110. }
  111. #define atomic64_sub_return(i, v) atomic64_add_return(-(i), (v))
  112. #define atomic64_sub(i, v) atomic64_add(-(i), (v))
  113. #define atomic64_inc_return(v) atomic64_add_return(1, (v))
  114. #define atomic64_dec_return(v) atomic64_sub_return(1, (v))
  115. #define atomic64_inc(v) atomic64_add(1, (v))
  116. #define atomic64_dec(v) atomic64_sub(1, (v))
  117. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  118. #define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
  119. #define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0)
  120. #define atomic64_add_negative(i, v) (atomic64_add_return((i), (v)) < 0)
  121. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  122. /* Atomic dec and inc don't implement barrier, so provide them if needed. */
  123. #define smp_mb__before_atomic_dec() smp_mb()
  124. #define smp_mb__after_atomic_dec() smp_mb()
  125. #define smp_mb__before_atomic_inc() smp_mb()
  126. #define smp_mb__after_atomic_inc() smp_mb()
  127. /* Define this to indicate that cmpxchg is an efficient operation. */
  128. #define __HAVE_ARCH_CMPXCHG
  129. #endif /* !__ASSEMBLY__ */
  130. #endif /* _ASM_TILE_ATOMIC_64_H */