atomic_64.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 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. /* Now the true 64-bit operations. */
  52. #define ATOMIC64_INIT(i) { (i) }
  53. #define atomic64_read(v) ((v)->counter)
  54. #define atomic64_set(v, i) ((v)->counter = (i))
  55. static inline void atomic64_add(long i, atomic64_t *v)
  56. {
  57. __insn_fetchadd((void *)&v->counter, i);
  58. }
  59. static inline long atomic64_add_return(long i, atomic64_t *v)
  60. {
  61. int val;
  62. smp_mb(); /* barrier for proper semantics */
  63. val = __insn_fetchadd((void *)&v->counter, i) + i;
  64. barrier(); /* the "+ i" above will wait on memory */
  65. return val;
  66. }
  67. static inline long atomic64_add_unless(atomic64_t *v, long a, long u)
  68. {
  69. long guess, oldval = v->counter;
  70. do {
  71. if (oldval == u)
  72. break;
  73. guess = oldval;
  74. oldval = cmpxchg(&v->counter, guess, guess + a);
  75. } while (guess != oldval);
  76. return oldval != u;
  77. }
  78. #define atomic64_sub_return(i, v) atomic64_add_return(-(i), (v))
  79. #define atomic64_sub(i, v) atomic64_add(-(i), (v))
  80. #define atomic64_inc_return(v) atomic64_add_return(1, (v))
  81. #define atomic64_dec_return(v) atomic64_sub_return(1, (v))
  82. #define atomic64_inc(v) atomic64_add(1, (v))
  83. #define atomic64_dec(v) atomic64_sub(1, (v))
  84. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  85. #define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
  86. #define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0)
  87. #define atomic64_add_negative(i, v) (atomic64_add_return((i), (v)) < 0)
  88. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  89. /* Atomic dec and inc don't implement barrier, so provide them if needed. */
  90. #define smp_mb__before_atomic_dec() smp_mb()
  91. #define smp_mb__after_atomic_dec() smp_mb()
  92. #define smp_mb__before_atomic_inc() smp_mb()
  93. #define smp_mb__after_atomic_inc() smp_mb()
  94. /* Define this to indicate that cmpxchg is an efficient operation. */
  95. #define __HAVE_ARCH_CMPXCHG
  96. #endif /* !__ASSEMBLY__ */
  97. #endif /* _ASM_TILE_ATOMIC_64_H */