atomic.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef _ASM_PARISC_ATOMIC_H_
  2. #define _ASM_PARISC_ATOMIC_H_
  3. #include <linux/config.h>
  4. #include <asm/system.h>
  5. /* Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>. */
  6. /*
  7. * Atomic operations that C can't guarantee us. Useful for
  8. * resource counting etc..
  9. *
  10. * And probably incredibly slow on parisc. OTOH, we don't
  11. * have to write any serious assembly. prumpf
  12. */
  13. #ifdef CONFIG_SMP
  14. #include <asm/spinlock.h>
  15. #include <asm/cache.h> /* we use L1_CACHE_BYTES */
  16. /* Use an array of spinlocks for our atomic_ts.
  17. * Hash function to index into a different SPINLOCK.
  18. * Since "a" is usually an address, use one spinlock per cacheline.
  19. */
  20. # define ATOMIC_HASH_SIZE 4
  21. # define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) a)/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
  22. extern spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
  23. /* Can't use _raw_spin_lock_irq because of #include problems, so
  24. * this is the substitute */
  25. #define _atomic_spin_lock_irqsave(l,f) do { \
  26. spinlock_t *s = ATOMIC_HASH(l); \
  27. local_irq_save(f); \
  28. _raw_spin_lock(s); \
  29. } while(0)
  30. #define _atomic_spin_unlock_irqrestore(l,f) do { \
  31. spinlock_t *s = ATOMIC_HASH(l); \
  32. _raw_spin_unlock(s); \
  33. local_irq_restore(f); \
  34. } while(0)
  35. #else
  36. # define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
  37. # define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
  38. #endif
  39. /* Note that we need not lock read accesses - aligned word writes/reads
  40. * are atomic, so a reader never sees unconsistent values.
  41. *
  42. * Cache-line alignment would conflict with, for example, linux/module.h
  43. */
  44. typedef struct { volatile int counter; } atomic_t;
  45. /* This should get optimized out since it's never called.
  46. ** Or get a link error if xchg is used "wrong".
  47. */
  48. extern void __xchg_called_with_bad_pointer(void);
  49. /* __xchg32/64 defined in arch/parisc/lib/bitops.c */
  50. extern unsigned long __xchg8(char, char *);
  51. extern unsigned long __xchg32(int, int *);
  52. #ifdef __LP64__
  53. extern unsigned long __xchg64(unsigned long, unsigned long *);
  54. #endif
  55. /* optimizer better get rid of switch since size is a constant */
  56. static __inline__ unsigned long __xchg(unsigned long x, __volatile__ void * ptr,
  57. int size)
  58. {
  59. switch(size) {
  60. #ifdef __LP64__
  61. case 8: return __xchg64(x,(unsigned long *) ptr);
  62. #endif
  63. case 4: return __xchg32((int) x, (int *) ptr);
  64. case 1: return __xchg8((char) x, (char *) ptr);
  65. }
  66. __xchg_called_with_bad_pointer();
  67. return x;
  68. }
  69. /*
  70. ** REVISIT - Abandoned use of LDCW in xchg() for now:
  71. ** o need to test sizeof(*ptr) to avoid clearing adjacent bytes
  72. ** o and while we are at it, could __LP64__ code use LDCD too?
  73. **
  74. ** if (__builtin_constant_p(x) && (x == NULL))
  75. ** if (((unsigned long)p & 0xf) == 0)
  76. ** return __ldcw(p);
  77. */
  78. #define xchg(ptr,x) \
  79. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  80. #define __HAVE_ARCH_CMPXCHG 1
  81. /* bug catcher for when unsupported size is used - won't link */
  82. extern void __cmpxchg_called_with_bad_pointer(void);
  83. /* __cmpxchg_u32/u64 defined in arch/parisc/lib/bitops.c */
  84. extern unsigned long __cmpxchg_u32(volatile unsigned int *m, unsigned int old, unsigned int new_);
  85. extern unsigned long __cmpxchg_u64(volatile unsigned long *ptr, unsigned long old, unsigned long new_);
  86. /* don't worry...optimizer will get rid of most of this */
  87. static __inline__ unsigned long
  88. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
  89. {
  90. switch(size) {
  91. #ifdef __LP64__
  92. case 8: return __cmpxchg_u64((unsigned long *)ptr, old, new_);
  93. #endif
  94. case 4: return __cmpxchg_u32((unsigned int *)ptr, (unsigned int) old, (unsigned int) new_);
  95. }
  96. __cmpxchg_called_with_bad_pointer();
  97. return old;
  98. }
  99. #define cmpxchg(ptr,o,n) \
  100. ({ \
  101. __typeof__(*(ptr)) _o_ = (o); \
  102. __typeof__(*(ptr)) _n_ = (n); \
  103. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  104. (unsigned long)_n_, sizeof(*(ptr))); \
  105. })
  106. /* It's possible to reduce all atomic operations to either
  107. * __atomic_add_return, atomic_set and atomic_read (the latter
  108. * is there only for consistency).
  109. */
  110. static __inline__ int __atomic_add_return(int i, atomic_t *v)
  111. {
  112. int ret;
  113. unsigned long flags;
  114. _atomic_spin_lock_irqsave(v, flags);
  115. ret = (v->counter += i);
  116. _atomic_spin_unlock_irqrestore(v, flags);
  117. return ret;
  118. }
  119. static __inline__ void atomic_set(atomic_t *v, int i)
  120. {
  121. unsigned long flags;
  122. _atomic_spin_lock_irqsave(v, flags);
  123. v->counter = i;
  124. _atomic_spin_unlock_irqrestore(v, flags);
  125. }
  126. static __inline__ int atomic_read(const atomic_t *v)
  127. {
  128. return v->counter;
  129. }
  130. /* exported interface */
  131. #define atomic_add(i,v) ((void)(__atomic_add_return( ((int)i),(v))))
  132. #define atomic_sub(i,v) ((void)(__atomic_add_return(-((int)i),(v))))
  133. #define atomic_inc(v) ((void)(__atomic_add_return( 1,(v))))
  134. #define atomic_dec(v) ((void)(__atomic_add_return( -1,(v))))
  135. #define atomic_add_return(i,v) (__atomic_add_return( ((int)i),(v)))
  136. #define atomic_sub_return(i,v) (__atomic_add_return(-((int)i),(v)))
  137. #define atomic_inc_return(v) (__atomic_add_return( 1,(v)))
  138. #define atomic_dec_return(v) (__atomic_add_return( -1,(v)))
  139. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  140. /*
  141. * atomic_inc_and_test - increment and test
  142. * @v: pointer of type atomic_t
  143. *
  144. * Atomically increments @v by 1
  145. * and returns true if the result is zero, or false for all
  146. * other cases.
  147. */
  148. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  149. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  150. #define ATOMIC_INIT(i) { (i) }
  151. #define smp_mb__before_atomic_dec() smp_mb()
  152. #define smp_mb__after_atomic_dec() smp_mb()
  153. #define smp_mb__before_atomic_inc() smp_mb()
  154. #define smp_mb__after_atomic_inc() smp_mb()
  155. #endif