atomic.h 7.9 KB

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