local.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #ifndef _ARCH_LOONGARCH_LOCAL_H
  6. #define _ARCH_LOONGARCH_LOCAL_H
  7. #include <linux/percpu.h>
  8. #include <linux/bitops.h>
  9. #include <linux/atomic.h>
  10. #include <asm/asm.h>
  11. #include <asm/cmpxchg.h>
  12. typedef struct {
  13. atomic_long_t a;
  14. } local_t;
  15. #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
  16. #define local_read(l) atomic_long_read(&(l)->a)
  17. #define local_set(l, i) atomic_long_set(&(l)->a, (i))
  18. #define local_add(i, l) atomic_long_add((i), (&(l)->a))
  19. #define local_sub(i, l) atomic_long_sub((i), (&(l)->a))
  20. #define local_inc(l) atomic_long_inc(&(l)->a)
  21. #define local_dec(l) atomic_long_dec(&(l)->a)
  22. /*
  23. * Same as above, but return the result value
  24. */
  25. #ifdef CONFIG_CPU_HAS_AMO
  26. static inline long local_add_return(long i, local_t *l)
  27. {
  28. unsigned long result;
  29. __asm__ __volatile__(
  30. " " __AMADD " %1, %2, %0 \n"
  31. : "+ZB" (l->a.counter), "=&r" (result)
  32. : "r" (i)
  33. : "memory");
  34. result = result + i;
  35. return result;
  36. }
  37. static inline long local_sub_return(long i, local_t *l)
  38. {
  39. unsigned long result;
  40. __asm__ __volatile__(
  41. " " __AMADD "%1, %2, %0 \n"
  42. : "+ZB" (l->a.counter), "=&r" (result)
  43. : "r" (-i)
  44. : "memory");
  45. result = result - i;
  46. return result;
  47. }
  48. #else
  49. static inline long local_add_return(long i, local_t *l)
  50. {
  51. unsigned long result, temp;
  52. __asm__ __volatile__(
  53. "1:" __LL "%1, %2 # local_add_return \n"
  54. __stringify(LONG_ADD) " %0, %1, %3 \n"
  55. __SC "%0, %2 \n"
  56. " beq %0, $r0, 1b \n"
  57. __stringify(LONG_ADD) " %0, %1, %3 \n"
  58. : "=&r" (result), "=&r" (temp), "=ZC" (l->a.counter)
  59. : "r" (i), "ZC" (l->a.counter)
  60. : "memory");
  61. return result;
  62. }
  63. static inline long local_sub_return(long i, local_t *l)
  64. {
  65. unsigned long result, temp;
  66. __asm__ __volatile__(
  67. "1:" __LL "%1, %2 # local_sub_return \n"
  68. __stringify(LONG_SUB) " %0, %1, %3 \n"
  69. __SC "%0, %2 \n"
  70. " beq %0, $r0, 1b \n"
  71. __stringify(LONG_SUB) " %0, %1, %3 \n"
  72. : "=&r" (result), "=&r" (temp), "=ZC" (l->a.counter)
  73. : "r" (i), "ZC" (l->a.counter)
  74. : "memory");
  75. return result;
  76. }
  77. #endif
  78. static inline long local_cmpxchg(local_t *l, long old, long new)
  79. {
  80. return cmpxchg_local(&l->a.counter, old, new);
  81. }
  82. static inline bool local_try_cmpxchg(local_t *l, long *old, long new)
  83. {
  84. return try_cmpxchg_local(&l->a.counter,
  85. (typeof(l->a.counter) *) old, new);
  86. }
  87. #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
  88. /**
  89. * local_add_unless - add unless the number is already a given value
  90. * @l: pointer of type local_t
  91. * @a: the amount to add to l...
  92. * @u: ...unless l is equal to u.
  93. *
  94. * Atomically adds @a to @l, if @v was not already @u.
  95. * Returns true if the addition was done.
  96. */
  97. static inline bool
  98. local_add_unless(local_t *l, long a, long u)
  99. {
  100. long c = local_read(l);
  101. do {
  102. if (unlikely(c == u))
  103. return false;
  104. } while (!local_try_cmpxchg(l, &c, c + a));
  105. return true;
  106. }
  107. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  108. #define local_dec_return(l) local_sub_return(1, (l))
  109. #define local_inc_return(l) local_add_return(1, (l))
  110. /*
  111. * local_sub_and_test - subtract value from variable and test result
  112. * @i: integer value to subtract
  113. * @l: pointer of type local_t
  114. *
  115. * Atomically subtracts @i from @l and returns
  116. * true if the result is zero, or false for all
  117. * other cases.
  118. */
  119. #define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
  120. /*
  121. * local_inc_and_test - increment and test
  122. * @l: pointer of type local_t
  123. *
  124. * Atomically increments @l by 1
  125. * and returns true if the result is zero, or false for all
  126. * other cases.
  127. */
  128. #define local_inc_and_test(l) (local_inc_return(l) == 0)
  129. /*
  130. * local_dec_and_test - decrement by 1 and test
  131. * @l: pointer of type local_t
  132. *
  133. * Atomically decrements @l by 1 and
  134. * returns true if the result is 0, or false for all other
  135. * cases.
  136. */
  137. #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
  138. /*
  139. * local_add_negative - add and test if negative
  140. * @l: pointer of type local_t
  141. * @i: integer value to add
  142. *
  143. * Atomically adds @i to @l and returns true
  144. * if the result is negative, or false when
  145. * result is greater than or equal to zero.
  146. */
  147. #define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
  148. /* Use these for per-cpu local_t variables: on some archs they are
  149. * much more efficient than these naive implementations. Note they take
  150. * a variable, not an address.
  151. */
  152. #define __local_inc(l) ((l)->a.counter++)
  153. #define __local_dec(l) ((l)->a.counter++)
  154. #define __local_add(i, l) ((l)->a.counter += (i))
  155. #define __local_sub(i, l) ((l)->a.counter -= (i))
  156. #endif /* _ARCH_LOONGARCH_LOCAL_H */