local.h 4.5 KB

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