local.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/cmpxchg.h>
  11. typedef struct {
  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. __asm__ __volatile__(
  28. " " __AMADD " %1, %2, %0 \n"
  29. : "+ZB" (l->a.counter), "=&r" (result)
  30. : "r" (i)
  31. : "memory");
  32. result = result + i;
  33. return result;
  34. }
  35. static inline long local_sub_return(long i, local_t *l)
  36. {
  37. unsigned long result;
  38. __asm__ __volatile__(
  39. " " __AMADD "%1, %2, %0 \n"
  40. : "+ZB" (l->a.counter), "=&r" (result)
  41. : "r" (-i)
  42. : "memory");
  43. result = result - i;
  44. return result;
  45. }
  46. static inline long local_cmpxchg(local_t *l, long old, long new)
  47. {
  48. return cmpxchg_local(&l->a.counter, old, new);
  49. }
  50. static inline bool local_try_cmpxchg(local_t *l, long *old, long new)
  51. {
  52. return try_cmpxchg_local(&l->a.counter,
  53. (typeof(l->a.counter) *) old, new);
  54. }
  55. #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
  56. /**
  57. * local_add_unless - add unless the number is already a given value
  58. * @l: pointer of type local_t
  59. * @a: the amount to add to l...
  60. * @u: ...unless l is equal to u.
  61. *
  62. * Atomically adds @a to @l, if @v was not already @u.
  63. * Returns true if the addition was done.
  64. */
  65. static inline bool
  66. local_add_unless(local_t *l, long a, long u)
  67. {
  68. long c = local_read(l);
  69. do {
  70. if (unlikely(c == u))
  71. return false;
  72. } while (!local_try_cmpxchg(l, &c, c + a));
  73. return true;
  74. }
  75. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  76. #define local_dec_return(l) local_sub_return(1, (l))
  77. #define local_inc_return(l) local_add_return(1, (l))
  78. /*
  79. * local_sub_and_test - subtract value from variable and test result
  80. * @i: integer value to subtract
  81. * @l: pointer of type local_t
  82. *
  83. * Atomically subtracts @i from @l and returns
  84. * true if the result is zero, or false for all
  85. * other cases.
  86. */
  87. #define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
  88. /*
  89. * local_inc_and_test - increment and test
  90. * @l: pointer of type local_t
  91. *
  92. * Atomically increments @l by 1
  93. * and returns true if the result is zero, or false for all
  94. * other cases.
  95. */
  96. #define local_inc_and_test(l) (local_inc_return(l) == 0)
  97. /*
  98. * local_dec_and_test - decrement by 1 and test
  99. * @l: pointer of type local_t
  100. *
  101. * Atomically decrements @l by 1 and
  102. * returns true if the result is 0, or false for all other
  103. * cases.
  104. */
  105. #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
  106. /*
  107. * local_add_negative - add and test if negative
  108. * @l: pointer of type local_t
  109. * @i: integer value to add
  110. *
  111. * Atomically adds @i to @l and returns true
  112. * if the result is negative, or false when
  113. * result is greater than or equal to zero.
  114. */
  115. #define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
  116. /* Use these for per-cpu local_t variables: on some archs they are
  117. * much more efficient than these naive implementations. Note they take
  118. * a variable, not an address.
  119. */
  120. #define __local_inc(l) ((l)->a.counter++)
  121. #define __local_dec(l) ((l)->a.counter++)
  122. #define __local_add(i, l) ((l)->a.counter += (i))
  123. #define __local_sub(i, l) ((l)->a.counter -= (i))
  124. #endif /* _ARCH_LOONGARCH_LOCAL_H */