atomic.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* atomic.h: These still suck, but the I-cache hit rate is higher.
  2. *
  3. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  4. * Copyright (C) 2000 Anton Blanchard (anton@linuxcare.com.au)
  5. * Copyright (C) 2007 Kyle McMartin (kyle@parisc-linux.org)
  6. *
  7. * Additions by Keith M Wesolowski (wesolows@foobazco.org) based
  8. * on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>.
  9. */
  10. #ifndef __ARCH_SPARC_ATOMIC__
  11. #define __ARCH_SPARC_ATOMIC__
  12. #include <linux/types.h>
  13. typedef struct { volatile int counter; } atomic_t;
  14. #ifdef __KERNEL__
  15. /* Emulate cmpxchg() the same way we emulate atomics,
  16. * by hashing the object address and indexing into an array
  17. * of spinlocks to get a bit of performance...
  18. *
  19. * See arch/sparc/lib/atomic32.c for implementation.
  20. *
  21. * Cribbed from <asm-parisc/atomic.h>
  22. */
  23. #define __HAVE_ARCH_CMPXCHG 1
  24. /* bug catcher for when unsupported size is used - won't link */
  25. extern void __cmpxchg_called_with_bad_pointer(void);
  26. /* we only need to support cmpxchg of a u32 on sparc */
  27. extern unsigned long __cmpxchg_u32(volatile u32 *m, u32 old, u32 new_);
  28. /* don't worry...optimizer will get rid of most of this */
  29. static __inline__ unsigned long
  30. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
  31. {
  32. switch(size) {
  33. case 4:
  34. return __cmpxchg_u32((u32 *)ptr, (u32)old, (u32)new_);
  35. default:
  36. __cmpxchg_called_with_bad_pointer();
  37. break;
  38. }
  39. return old;
  40. }
  41. #define cmpxchg(ptr,o,n) ({ \
  42. __typeof__(*(ptr)) _o_ = (o); \
  43. __typeof__(*(ptr)) _n_ = (n); \
  44. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  45. (unsigned long)_n_, sizeof(*(ptr))); \
  46. })
  47. #define ATOMIC_INIT(i) { (i) }
  48. extern int __atomic_add_return(int, atomic_t *);
  49. extern int atomic_cmpxchg(atomic_t *, int, int);
  50. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  51. extern int atomic_add_unless(atomic_t *, int, int);
  52. extern void atomic_set(atomic_t *, int);
  53. #define atomic_read(v) ((v)->counter)
  54. #define atomic_add(i, v) ((void)__atomic_add_return( (int)(i), (v)))
  55. #define atomic_sub(i, v) ((void)__atomic_add_return(-(int)(i), (v)))
  56. #define atomic_inc(v) ((void)__atomic_add_return( 1, (v)))
  57. #define atomic_dec(v) ((void)__atomic_add_return( -1, (v)))
  58. #define atomic_add_return(i, v) (__atomic_add_return( (int)(i), (v)))
  59. #define atomic_sub_return(i, v) (__atomic_add_return(-(int)(i), (v)))
  60. #define atomic_inc_return(v) (__atomic_add_return( 1, (v)))
  61. #define atomic_dec_return(v) (__atomic_add_return( -1, (v)))
  62. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  63. /*
  64. * atomic_inc_and_test - increment and test
  65. * @v: pointer of type atomic_t
  66. *
  67. * Atomically increments @v by 1
  68. * and returns true if the result is zero, or false for all
  69. * other cases.
  70. */
  71. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  72. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  73. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  74. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  75. /* This is the old 24-bit implementation. It's still used internally
  76. * by some sparc-specific code, notably the semaphore implementation.
  77. */
  78. typedef struct { volatile int counter; } atomic24_t;
  79. #ifndef CONFIG_SMP
  80. #define ATOMIC24_INIT(i) { (i) }
  81. #define atomic24_read(v) ((v)->counter)
  82. #define atomic24_set(v, i) (((v)->counter) = i)
  83. #else
  84. /* We do the bulk of the actual work out of line in two common
  85. * routines in assembler, see arch/sparc/lib/atomic.S for the
  86. * "fun" details.
  87. *
  88. * For SMP the trick is you embed the spin lock byte within
  89. * the word, use the low byte so signedness is easily retained
  90. * via a quick arithmetic shift. It looks like this:
  91. *
  92. * ----------------------------------------
  93. * | signed 24-bit counter value | lock | atomic_t
  94. * ----------------------------------------
  95. * 31 8 7 0
  96. */
  97. #define ATOMIC24_INIT(i) { ((i) << 8) }
  98. static inline int atomic24_read(const atomic24_t *v)
  99. {
  100. int ret = v->counter;
  101. while(ret & 0xff)
  102. ret = v->counter;
  103. return ret >> 8;
  104. }
  105. #define atomic24_set(v, i) (((v)->counter) = ((i) << 8))
  106. #endif
  107. static inline int __atomic24_add(int i, atomic24_t *v)
  108. {
  109. register volatile int *ptr asm("g1");
  110. register int increment asm("g2");
  111. register int tmp1 asm("g3");
  112. register int tmp2 asm("g4");
  113. register int tmp3 asm("g7");
  114. ptr = &v->counter;
  115. increment = i;
  116. __asm__ __volatile__(
  117. "mov %%o7, %%g4\n\t"
  118. "call ___atomic24_add\n\t"
  119. " add %%o7, 8, %%o7\n"
  120. : "=&r" (increment), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
  121. : "0" (increment), "r" (ptr)
  122. : "memory", "cc");
  123. return increment;
  124. }
  125. static inline int __atomic24_sub(int i, atomic24_t *v)
  126. {
  127. register volatile int *ptr asm("g1");
  128. register int increment asm("g2");
  129. register int tmp1 asm("g3");
  130. register int tmp2 asm("g4");
  131. register int tmp3 asm("g7");
  132. ptr = &v->counter;
  133. increment = i;
  134. __asm__ __volatile__(
  135. "mov %%o7, %%g4\n\t"
  136. "call ___atomic24_sub\n\t"
  137. " add %%o7, 8, %%o7\n"
  138. : "=&r" (increment), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
  139. : "0" (increment), "r" (ptr)
  140. : "memory", "cc");
  141. return increment;
  142. }
  143. #define atomic24_add(i, v) ((void)__atomic24_add((i), (v)))
  144. #define atomic24_sub(i, v) ((void)__atomic24_sub((i), (v)))
  145. #define atomic24_dec_return(v) __atomic24_sub(1, (v))
  146. #define atomic24_inc_return(v) __atomic24_add(1, (v))
  147. #define atomic24_sub_and_test(i, v) (__atomic24_sub((i), (v)) == 0)
  148. #define atomic24_dec_and_test(v) (__atomic24_sub(1, (v)) == 0)
  149. #define atomic24_inc(v) ((void)__atomic24_add(1, (v)))
  150. #define atomic24_dec(v) ((void)__atomic24_sub(1, (v)))
  151. #define atomic24_add_negative(i, v) (__atomic24_add((i), (v)) < 0)
  152. /* Atomic operations are already serializing */
  153. #define smp_mb__before_atomic_dec() barrier()
  154. #define smp_mb__after_atomic_dec() barrier()
  155. #define smp_mb__before_atomic_inc() barrier()
  156. #define smp_mb__after_atomic_inc() barrier()
  157. #endif /* !(__KERNEL__) */
  158. #include <asm-generic/atomic.h>
  159. #endif /* !(__ARCH_SPARC_ATOMIC__) */