atomic_64.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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) WRITE_ONCE((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 void atomic_add(int i, atomic_t *v)
  29. {
  30. __insn_fetchadd4((void *)&v->counter, i);
  31. }
  32. static inline int atomic_add_return(int i, atomic_t *v)
  33. {
  34. int val;
  35. smp_mb(); /* barrier for proper semantics */
  36. val = __insn_fetchadd4((void *)&v->counter, i) + i;
  37. barrier(); /* the "+ i" above will wait on memory */
  38. return val;
  39. }
  40. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  41. {
  42. int guess, oldval = v->counter;
  43. do {
  44. if (oldval == u)
  45. break;
  46. guess = oldval;
  47. oldval = cmpxchg(&v->counter, guess, guess + a);
  48. } while (guess != oldval);
  49. return oldval;
  50. }
  51. static inline void atomic_and(int i, atomic_t *v)
  52. {
  53. __insn_fetchand4((void *)&v->counter, i);
  54. }
  55. static inline void atomic_or(int i, atomic_t *v)
  56. {
  57. __insn_fetchor4((void *)&v->counter, i);
  58. }
  59. static inline void atomic_xor(int i, atomic_t *v)
  60. {
  61. int guess, oldval = v->counter;
  62. do {
  63. guess = oldval;
  64. __insn_mtspr(SPR_CMPEXCH_VALUE, guess);
  65. oldval = __insn_cmpexch4(&v->counter, guess ^ i);
  66. } while (guess != oldval);
  67. }
  68. /* Now the true 64-bit operations. */
  69. #define ATOMIC64_INIT(i) { (i) }
  70. #define atomic64_read(v) READ_ONCE((v)->counter)
  71. #define atomic64_set(v, i) WRITE_ONCE((v)->counter, (i))
  72. static inline void atomic64_add(long i, atomic64_t *v)
  73. {
  74. __insn_fetchadd((void *)&v->counter, i);
  75. }
  76. static inline long atomic64_add_return(long i, atomic64_t *v)
  77. {
  78. int val;
  79. smp_mb(); /* barrier for proper semantics */
  80. val = __insn_fetchadd((void *)&v->counter, i) + i;
  81. barrier(); /* the "+ i" above will wait on memory */
  82. return val;
  83. }
  84. static inline long atomic64_add_unless(atomic64_t *v, long a, long u)
  85. {
  86. long guess, oldval = v->counter;
  87. do {
  88. if (oldval == u)
  89. break;
  90. guess = oldval;
  91. oldval = cmpxchg(&v->counter, guess, guess + a);
  92. } while (guess != oldval);
  93. return oldval != u;
  94. }
  95. static inline void atomic64_and(long i, atomic64_t *v)
  96. {
  97. __insn_fetchand((void *)&v->counter, i);
  98. }
  99. static inline void atomic64_or(long i, atomic64_t *v)
  100. {
  101. __insn_fetchor((void *)&v->counter, i);
  102. }
  103. static inline void atomic64_xor(long i, atomic64_t *v)
  104. {
  105. long guess, oldval = v->counter;
  106. do {
  107. guess = oldval;
  108. __insn_mtspr(SPR_CMPEXCH_VALUE, guess);
  109. oldval = __insn_cmpexch(&v->counter, guess ^ i);
  110. } while (guess != oldval);
  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. #endif /* !__ASSEMBLY__ */
  124. #endif /* _ASM_TILE_ATOMIC_64_H */