local.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef _ARCH_POWERPC_LOCAL_H
  2. #define _ARCH_POWERPC_LOCAL_H
  3. #include <linux/percpu.h>
  4. #include <linux/atomic.h>
  5. typedef struct
  6. {
  7. atomic_long_t a;
  8. } local_t;
  9. #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
  10. #define local_read(l) atomic_long_read(&(l)->a)
  11. #define local_set(l,i) atomic_long_set(&(l)->a, (i))
  12. #define local_add(i,l) atomic_long_add((i),(&(l)->a))
  13. #define local_sub(i,l) atomic_long_sub((i),(&(l)->a))
  14. #define local_inc(l) atomic_long_inc(&(l)->a)
  15. #define local_dec(l) atomic_long_dec(&(l)->a)
  16. static __inline__ long local_add_return(long a, local_t *l)
  17. {
  18. long t;
  19. __asm__ __volatile__(
  20. "1:" PPC_LLARX(%0,0,%2,0) " # local_add_return\n\
  21. add %0,%1,%0\n"
  22. PPC405_ERR77(0,%2)
  23. PPC_STLCX "%0,0,%2 \n\
  24. bne- 1b"
  25. : "=&r" (t)
  26. : "r" (a), "r" (&(l->a.counter))
  27. : "cc", "memory");
  28. return t;
  29. }
  30. #define local_add_negative(a, l) (local_add_return((a), (l)) < 0)
  31. static __inline__ long local_sub_return(long a, local_t *l)
  32. {
  33. long t;
  34. __asm__ __volatile__(
  35. "1:" PPC_LLARX(%0,0,%2,0) " # local_sub_return\n\
  36. subf %0,%1,%0\n"
  37. PPC405_ERR77(0,%2)
  38. PPC_STLCX "%0,0,%2 \n\
  39. bne- 1b"
  40. : "=&r" (t)
  41. : "r" (a), "r" (&(l->a.counter))
  42. : "cc", "memory");
  43. return t;
  44. }
  45. static __inline__ long local_inc_return(local_t *l)
  46. {
  47. long t;
  48. __asm__ __volatile__(
  49. "1:" PPC_LLARX(%0,0,%1,0) " # local_inc_return\n\
  50. addic %0,%0,1\n"
  51. PPC405_ERR77(0,%1)
  52. PPC_STLCX "%0,0,%1 \n\
  53. bne- 1b"
  54. : "=&r" (t)
  55. : "r" (&(l->a.counter))
  56. : "cc", "xer", "memory");
  57. return t;
  58. }
  59. /*
  60. * local_inc_and_test - increment and test
  61. * @l: pointer of type local_t
  62. *
  63. * Atomically increments @l by 1
  64. * and returns true if the result is zero, or false for all
  65. * other cases.
  66. */
  67. #define local_inc_and_test(l) (local_inc_return(l) == 0)
  68. static __inline__ long local_dec_return(local_t *l)
  69. {
  70. long t;
  71. __asm__ __volatile__(
  72. "1:" PPC_LLARX(%0,0,%1,0) " # local_dec_return\n\
  73. addic %0,%0,-1\n"
  74. PPC405_ERR77(0,%1)
  75. PPC_STLCX "%0,0,%1\n\
  76. bne- 1b"
  77. : "=&r" (t)
  78. : "r" (&(l->a.counter))
  79. : "cc", "xer", "memory");
  80. return t;
  81. }
  82. #define local_cmpxchg(l, o, n) \
  83. (cmpxchg_local(&((l)->a.counter), (o), (n)))
  84. #define local_xchg(l, n) (xchg_local(&((l)->a.counter), (n)))
  85. /**
  86. * local_add_unless - add unless the number is a given value
  87. * @l: pointer of type local_t
  88. * @a: the amount to add to v...
  89. * @u: ...unless v is equal to u.
  90. *
  91. * Atomically adds @a to @l, so long as it was not @u.
  92. * Returns non-zero if @l was not @u, and zero otherwise.
  93. */
  94. static __inline__ int local_add_unless(local_t *l, long a, long u)
  95. {
  96. long t;
  97. __asm__ __volatile__ (
  98. "1:" PPC_LLARX(%0,0,%1,0) " # local_add_unless\n\
  99. cmpw 0,%0,%3 \n\
  100. beq- 2f \n\
  101. add %0,%2,%0 \n"
  102. PPC405_ERR77(0,%2)
  103. PPC_STLCX "%0,0,%1 \n\
  104. bne- 1b \n"
  105. " subf %0,%2,%0 \n\
  106. 2:"
  107. : "=&r" (t)
  108. : "r" (&(l->a.counter)), "r" (a), "r" (u)
  109. : "cc", "memory");
  110. return t != u;
  111. }
  112. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  113. #define local_sub_and_test(a, l) (local_sub_return((a), (l)) == 0)
  114. #define local_dec_and_test(l) (local_dec_return((l)) == 0)
  115. /*
  116. * Atomically test *l and decrement if it is greater than 0.
  117. * The function returns the old value of *l minus 1.
  118. */
  119. static __inline__ long local_dec_if_positive(local_t *l)
  120. {
  121. long t;
  122. __asm__ __volatile__(
  123. "1:" PPC_LLARX(%0,0,%1,0) " # local_dec_if_positive\n\
  124. cmpwi %0,1\n\
  125. addi %0,%0,-1\n\
  126. blt- 2f\n"
  127. PPC405_ERR77(0,%1)
  128. PPC_STLCX "%0,0,%1\n\
  129. bne- 1b"
  130. "\n\
  131. 2:" : "=&b" (t)
  132. : "r" (&(l->a.counter))
  133. : "cc", "memory");
  134. return t;
  135. }
  136. /* Use these for per-cpu local_t variables: on some archs they are
  137. * much more efficient than these naive implementations. Note they take
  138. * a variable, not an address.
  139. */
  140. #define __local_inc(l) ((l)->a.counter++)
  141. #define __local_dec(l) ((l)->a.counter++)
  142. #define __local_add(i,l) ((l)->a.counter+=(i))
  143. #define __local_sub(i,l) ((l)->a.counter-=(i))
  144. #endif /* _ARCH_POWERPC_LOCAL_H */