atomic_64.h 4.4 KB

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