local.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #define local_cmpxchg(l, o, n) \
  47. ((long)cmpxchg_local(&((l)->a.counter), (o), (n)))
  48. #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
  49. /**
  50. * local_add_unless - add unless the number is a given value
  51. * @l: pointer of type local_t
  52. * @a: the amount to add to l...
  53. * @u: ...unless l is equal to u.
  54. *
  55. * Atomically adds @a to @l, so long as it was not @u.
  56. * Returns non-zero if @l was not @u, and zero otherwise.
  57. */
  58. #define local_add_unless(l, a, u) \
  59. ({ \
  60. long c, old; \
  61. c = local_read(l); \
  62. while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \
  63. c = old; \
  64. c != (u); \
  65. })
  66. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  67. #define local_dec_return(l) local_sub_return(1, (l))
  68. #define local_inc_return(l) local_add_return(1, (l))
  69. /*
  70. * local_sub_and_test - subtract value from variable and test result
  71. * @i: integer value to subtract
  72. * @l: pointer of type local_t
  73. *
  74. * Atomically subtracts @i from @l and returns
  75. * true if the result is zero, or false for all
  76. * other cases.
  77. */
  78. #define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
  79. /*
  80. * local_inc_and_test - increment and test
  81. * @l: pointer of type local_t
  82. *
  83. * Atomically increments @l by 1
  84. * and returns true if the result is zero, or false for all
  85. * other cases.
  86. */
  87. #define local_inc_and_test(l) (local_inc_return(l) == 0)
  88. /*
  89. * local_dec_and_test - decrement by 1 and test
  90. * @l: pointer of type local_t
  91. *
  92. * Atomically decrements @l by 1 and
  93. * returns true if the result is 0, or false for all other
  94. * cases.
  95. */
  96. #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
  97. /*
  98. * local_add_negative - add and test if negative
  99. * @l: pointer of type local_t
  100. * @i: integer value to add
  101. *
  102. * Atomically adds @i to @l and returns true
  103. * if the result is negative, or false when
  104. * result is greater than or equal to zero.
  105. */
  106. #define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
  107. /* Use these for per-cpu local_t variables: on some archs they are
  108. * much more efficient than these naive implementations. Note they take
  109. * a variable, not an address.
  110. */
  111. #define __local_inc(l) ((l)->a.counter++)
  112. #define __local_dec(l) ((l)->a.counter++)
  113. #define __local_add(i, l) ((l)->a.counter += (i))
  114. #define __local_sub(i, l) ((l)->a.counter -= (i))
  115. #endif /* _ARCH_LOONGARCH_LOCAL_H */