local.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_LOCAL_H
  3. #define _ASM_X86_LOCAL_H
  4. #include <linux/percpu.h>
  5. #include <linux/atomic.h>
  6. #include <asm/asm.h>
  7. typedef struct {
  8. atomic_long_t a;
  9. } local_t;
  10. #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
  11. #define local_read(l) atomic_long_read(&(l)->a)
  12. #define local_set(l, i) atomic_long_set(&(l)->a, (i))
  13. static inline void local_inc(local_t *l)
  14. {
  15. asm volatile(_ASM_INC "%0"
  16. : "+m" (l->a.counter));
  17. }
  18. static inline void local_dec(local_t *l)
  19. {
  20. asm volatile(_ASM_DEC "%0"
  21. : "+m" (l->a.counter));
  22. }
  23. static inline void local_add(long i, local_t *l)
  24. {
  25. asm volatile(_ASM_ADD "%1,%0"
  26. : "+m" (l->a.counter)
  27. : "ir" (i));
  28. }
  29. static inline void local_sub(long i, local_t *l)
  30. {
  31. asm volatile(_ASM_SUB "%1,%0"
  32. : "+m" (l->a.counter)
  33. : "ir" (i));
  34. }
  35. /**
  36. * local_sub_and_test - subtract value from variable and test result
  37. * @i: integer value to subtract
  38. * @l: pointer to type local_t
  39. *
  40. * Atomically subtracts @i from @l and returns
  41. * true if the result is zero, or false for all
  42. * other cases.
  43. */
  44. static inline bool local_sub_and_test(long i, local_t *l)
  45. {
  46. return GEN_BINARY_RMWcc(_ASM_SUB, l->a.counter, e, "er", i);
  47. }
  48. /**
  49. * local_dec_and_test - decrement and test
  50. * @l: pointer to type local_t
  51. *
  52. * Atomically decrements @l by 1 and
  53. * returns true if the result is 0, or false for all other
  54. * cases.
  55. */
  56. static inline bool local_dec_and_test(local_t *l)
  57. {
  58. return GEN_UNARY_RMWcc(_ASM_DEC, l->a.counter, e);
  59. }
  60. /**
  61. * local_inc_and_test - increment and test
  62. * @l: pointer to type local_t
  63. *
  64. * Atomically increments @l by 1
  65. * and returns true if the result is zero, or false for all
  66. * other cases.
  67. */
  68. static inline bool local_inc_and_test(local_t *l)
  69. {
  70. return GEN_UNARY_RMWcc(_ASM_INC, l->a.counter, e);
  71. }
  72. /**
  73. * local_add_negative - add and test if negative
  74. * @i: integer value to add
  75. * @l: pointer to type local_t
  76. *
  77. * Atomically adds @i to @l and returns true
  78. * if the result is negative, or false when
  79. * result is greater than or equal to zero.
  80. */
  81. static inline bool local_add_negative(long i, local_t *l)
  82. {
  83. return GEN_BINARY_RMWcc(_ASM_ADD, l->a.counter, s, "er", i);
  84. }
  85. /**
  86. * local_add_return - add and return
  87. * @i: integer value to add
  88. * @l: pointer to type local_t
  89. *
  90. * Atomically adds @i to @l and returns @i + @l
  91. */
  92. static inline long local_add_return(long i, local_t *l)
  93. {
  94. long __i = i;
  95. asm volatile(_ASM_XADD "%0, %1;"
  96. : "+r" (i), "+m" (l->a.counter)
  97. : : "memory");
  98. return i + __i;
  99. }
  100. static inline long local_sub_return(long i, local_t *l)
  101. {
  102. return local_add_return(-i, l);
  103. }
  104. #define local_inc_return(l) (local_add_return(1, l))
  105. #define local_dec_return(l) (local_sub_return(1, l))
  106. #define local_cmpxchg(l, o, n) \
  107. (cmpxchg_local(&((l)->a.counter), (o), (n)))
  108. /* Always has a lock prefix */
  109. #define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
  110. /**
  111. * local_add_unless - add unless the number is a given value
  112. * @l: pointer of type local_t
  113. * @a: the amount to add to l...
  114. * @u: ...unless l is equal to u.
  115. *
  116. * Atomically adds @a to @l, so long as it was not @u.
  117. * Returns non-zero if @l was not @u, and zero otherwise.
  118. */
  119. #define local_add_unless(l, a, u) \
  120. ({ \
  121. long c, old; \
  122. c = local_read((l)); \
  123. for (;;) { \
  124. if (unlikely(c == (u))) \
  125. break; \
  126. old = local_cmpxchg((l), c, c + (a)); \
  127. if (likely(old == c)) \
  128. break; \
  129. c = old; \
  130. } \
  131. c != (u); \
  132. })
  133. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  134. /* On x86_32, these are no better than the atomic variants.
  135. * On x86-64 these are better than the atomic variants on SMP kernels
  136. * because they dont use a lock prefix.
  137. */
  138. #define __local_inc(l) local_inc(l)
  139. #define __local_dec(l) local_dec(l)
  140. #define __local_add(i, l) local_add((i), (l))
  141. #define __local_sub(i, l) local_sub((i), (l))
  142. #endif /* _ASM_X86_LOCAL_H */