atomic.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  2. * Copyright (C) 2006 Kyle McMartin <kyle@parisc-linux.org>
  3. */
  4. #ifndef _ASM_PARISC_ATOMIC_H_
  5. #define _ASM_PARISC_ATOMIC_H_
  6. #include <linux/types.h>
  7. #include <asm/system.h>
  8. /*
  9. * Atomic operations that C can't guarantee us. Useful for
  10. * resource counting etc..
  11. *
  12. * And probably incredibly slow on parisc. OTOH, we don't
  13. * have to write any serious assembly. prumpf
  14. */
  15. #ifdef CONFIG_SMP
  16. #include <asm/spinlock.h>
  17. #include <asm/cache.h> /* we use L1_CACHE_BYTES */
  18. /* Use an array of spinlocks for our atomic_ts.
  19. * Hash function to index into a different SPINLOCK.
  20. * Since "a" is usually an address, use one spinlock per cacheline.
  21. */
  22. # define ATOMIC_HASH_SIZE 4
  23. # define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) a)/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
  24. extern raw_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
  25. /* Can't use raw_spin_lock_irq because of #include problems, so
  26. * this is the substitute */
  27. #define _atomic_spin_lock_irqsave(l,f) do { \
  28. raw_spinlock_t *s = ATOMIC_HASH(l); \
  29. local_irq_save(f); \
  30. __raw_spin_lock(s); \
  31. } while(0)
  32. #define _atomic_spin_unlock_irqrestore(l,f) do { \
  33. raw_spinlock_t *s = ATOMIC_HASH(l); \
  34. __raw_spin_unlock(s); \
  35. local_irq_restore(f); \
  36. } while(0)
  37. #else
  38. # define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
  39. # define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
  40. #endif
  41. /* This should get optimized out since it's never called.
  42. ** Or get a link error if xchg is used "wrong".
  43. */
  44. extern void __xchg_called_with_bad_pointer(void);
  45. /* __xchg32/64 defined in arch/parisc/lib/bitops.c */
  46. extern unsigned long __xchg8(char, char *);
  47. extern unsigned long __xchg32(int, int *);
  48. #ifdef __LP64__
  49. extern unsigned long __xchg64(unsigned long, unsigned long *);
  50. #endif
  51. /* optimizer better get rid of switch since size is a constant */
  52. static __inline__ unsigned long
  53. __xchg(unsigned long x, __volatile__ void * ptr, int size)
  54. {
  55. switch(size) {
  56. #ifdef __LP64__
  57. case 8: return __xchg64(x,(unsigned long *) ptr);
  58. #endif
  59. case 4: return __xchg32((int) x, (int *) ptr);
  60. case 1: return __xchg8((char) x, (char *) ptr);
  61. }
  62. __xchg_called_with_bad_pointer();
  63. return x;
  64. }
  65. /*
  66. ** REVISIT - Abandoned use of LDCW in xchg() for now:
  67. ** o need to test sizeof(*ptr) to avoid clearing adjacent bytes
  68. ** o and while we are at it, could __LP64__ code use LDCD too?
  69. **
  70. ** if (__builtin_constant_p(x) && (x == NULL))
  71. ** if (((unsigned long)p & 0xf) == 0)
  72. ** return __ldcw(p);
  73. */
  74. #define xchg(ptr,x) \
  75. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  76. #define __HAVE_ARCH_CMPXCHG 1
  77. /* bug catcher for when unsupported size is used - won't link */
  78. extern void __cmpxchg_called_with_bad_pointer(void);
  79. /* __cmpxchg_u32/u64 defined in arch/parisc/lib/bitops.c */
  80. extern unsigned long __cmpxchg_u32(volatile unsigned int *m, unsigned int old, unsigned int new_);
  81. extern unsigned long __cmpxchg_u64(volatile unsigned long *ptr, unsigned long old, unsigned long new_);
  82. /* don't worry...optimizer will get rid of most of this */
  83. static __inline__ unsigned long
  84. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
  85. {
  86. switch(size) {
  87. #ifdef __LP64__
  88. case 8: return __cmpxchg_u64((unsigned long *)ptr, old, new_);
  89. #endif
  90. case 4: return __cmpxchg_u32((unsigned int *)ptr, (unsigned int) old, (unsigned int) new_);
  91. }
  92. __cmpxchg_called_with_bad_pointer();
  93. return old;
  94. }
  95. #define cmpxchg(ptr,o,n) \
  96. ({ \
  97. __typeof__(*(ptr)) _o_ = (o); \
  98. __typeof__(*(ptr)) _n_ = (n); \
  99. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  100. (unsigned long)_n_, sizeof(*(ptr))); \
  101. })
  102. /* Note that we need not lock read accesses - aligned word writes/reads
  103. * are atomic, so a reader never sees unconsistent values.
  104. *
  105. * Cache-line alignment would conflict with, for example, linux/module.h
  106. */
  107. typedef struct { volatile int counter; } atomic_t;
  108. /* It's possible to reduce all atomic operations to either
  109. * __atomic_add_return, atomic_set and atomic_read (the latter
  110. * is there only for consistency).
  111. */
  112. static __inline__ int __atomic_add_return(int i, atomic_t *v)
  113. {
  114. int ret;
  115. unsigned long flags;
  116. _atomic_spin_lock_irqsave(v, flags);
  117. ret = (v->counter += i);
  118. _atomic_spin_unlock_irqrestore(v, flags);
  119. return ret;
  120. }
  121. static __inline__ void atomic_set(atomic_t *v, int i)
  122. {
  123. unsigned long flags;
  124. _atomic_spin_lock_irqsave(v, flags);
  125. v->counter = i;
  126. _atomic_spin_unlock_irqrestore(v, flags);
  127. }
  128. static __inline__ int atomic_read(const atomic_t *v)
  129. {
  130. return v->counter;
  131. }
  132. /* exported interface */
  133. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  134. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  135. /**
  136. * atomic_add_unless - add unless the number is a given value
  137. * @v: pointer of type atomic_t
  138. * @a: the amount to add to v...
  139. * @u: ...unless v is equal to u.
  140. *
  141. * Atomically adds @a to @v, so long as it was not @u.
  142. * Returns non-zero if @v was not @u, and zero otherwise.
  143. */
  144. #define atomic_add_unless(v, a, u) \
  145. ({ \
  146. int c, old; \
  147. c = atomic_read(v); \
  148. while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
  149. c = old; \
  150. c != (u); \
  151. })
  152. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  153. #define atomic_add(i,v) ((void)(__atomic_add_return( ((int)i),(v))))
  154. #define atomic_sub(i,v) ((void)(__atomic_add_return(-((int)i),(v))))
  155. #define atomic_inc(v) ((void)(__atomic_add_return( 1,(v))))
  156. #define atomic_dec(v) ((void)(__atomic_add_return( -1,(v))))
  157. #define atomic_add_return(i,v) (__atomic_add_return( ((int)i),(v)))
  158. #define atomic_sub_return(i,v) (__atomic_add_return(-((int)i),(v)))
  159. #define atomic_inc_return(v) (__atomic_add_return( 1,(v)))
  160. #define atomic_dec_return(v) (__atomic_add_return( -1,(v)))
  161. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  162. /*
  163. * atomic_inc_and_test - increment and test
  164. * @v: pointer of type atomic_t
  165. *
  166. * Atomically increments @v by 1
  167. * and returns true if the result is zero, or false for all
  168. * other cases.
  169. */
  170. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  171. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  172. #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
  173. #define ATOMIC_INIT(i) ((atomic_t) { (i) })
  174. #define smp_mb__before_atomic_dec() smp_mb()
  175. #define smp_mb__after_atomic_dec() smp_mb()
  176. #define smp_mb__before_atomic_inc() smp_mb()
  177. #define smp_mb__after_atomic_inc() smp_mb()
  178. #ifdef __LP64__
  179. typedef struct { volatile s64 counter; } atomic64_t;
  180. #define ATOMIC64_INIT(i) ((atomic64_t) { (i) })
  181. static __inline__ int
  182. __atomic64_add_return(s64 i, atomic64_t *v)
  183. {
  184. int ret;
  185. unsigned long flags;
  186. _atomic_spin_lock_irqsave(v, flags);
  187. ret = (v->counter += i);
  188. _atomic_spin_unlock_irqrestore(v, flags);
  189. return ret;
  190. }
  191. static __inline__ void
  192. atomic64_set(atomic64_t *v, s64 i)
  193. {
  194. unsigned long flags;
  195. _atomic_spin_lock_irqsave(v, flags);
  196. v->counter = i;
  197. _atomic_spin_unlock_irqrestore(v, flags);
  198. }
  199. static __inline__ s64
  200. atomic64_read(const atomic64_t *v)
  201. {
  202. return v->counter;
  203. }
  204. #define atomic64_add(i,v) ((void)(__atomic64_add_return( ((s64)i),(v))))
  205. #define atomic64_sub(i,v) ((void)(__atomic64_add_return(-((s64)i),(v))))
  206. #define atomic64_inc(v) ((void)(__atomic64_add_return( 1,(v))))
  207. #define atomic64_dec(v) ((void)(__atomic64_add_return( -1,(v))))
  208. #define atomic64_add_return(i,v) (__atomic64_add_return( ((s64)i),(v)))
  209. #define atomic64_sub_return(i,v) (__atomic64_add_return(-((s64)i),(v)))
  210. #define atomic64_inc_return(v) (__atomic64_add_return( 1,(v)))
  211. #define atomic64_dec_return(v) (__atomic64_add_return( -1,(v)))
  212. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  213. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  214. #define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
  215. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i),(v)) == 0)
  216. #endif /* __LP64__ */
  217. #include <asm-generic/atomic.h>
  218. #endif /* _ASM_PARISC_ATOMIC_H_ */