spinlock.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #ifndef __ASM_SPINLOCK_H
  2. #define __ASM_SPINLOCK_H
  3. #include <asm/atomic.h>
  4. #include <asm/rwlock.h>
  5. #include <asm/page.h>
  6. #include <linux/config.h>
  7. #include <linux/compiler.h>
  8. asmlinkage int printk(const char * fmt, ...)
  9. __attribute__ ((format (printf, 1, 2)));
  10. /*
  11. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  12. */
  13. typedef struct {
  14. volatile unsigned int slock;
  15. #ifdef CONFIG_DEBUG_SPINLOCK
  16. unsigned magic;
  17. #endif
  18. #ifdef CONFIG_PREEMPT
  19. unsigned int break_lock;
  20. #endif
  21. } spinlock_t;
  22. #define SPINLOCK_MAGIC 0xdead4ead
  23. #ifdef CONFIG_DEBUG_SPINLOCK
  24. #define SPINLOCK_MAGIC_INIT , SPINLOCK_MAGIC
  25. #else
  26. #define SPINLOCK_MAGIC_INIT /* */
  27. #endif
  28. #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
  29. #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
  30. /*
  31. * Simple spin lock operations. There are two variants, one clears IRQ's
  32. * on the local processor, one does not.
  33. *
  34. * We make no fairness assumptions. They have a cost.
  35. */
  36. #define spin_is_locked(x) (*(volatile signed char *)(&(x)->slock) <= 0)
  37. #define spin_unlock_wait(x) do { barrier(); } while(spin_is_locked(x))
  38. #define spin_lock_string \
  39. "\n1:\t" \
  40. "lock ; decb %0\n\t" \
  41. "jns 3f\n" \
  42. "2:\t" \
  43. "rep;nop\n\t" \
  44. "cmpb $0,%0\n\t" \
  45. "jle 2b\n\t" \
  46. "jmp 1b\n" \
  47. "3:\n\t"
  48. #define spin_lock_string_flags \
  49. "\n1:\t" \
  50. "lock ; decb %0\n\t" \
  51. "jns 4f\n\t" \
  52. "2:\t" \
  53. "testl $0x200, %1\n\t" \
  54. "jz 3f\n\t" \
  55. "sti\n\t" \
  56. "3:\t" \
  57. "rep;nop\n\t" \
  58. "cmpb $0, %0\n\t" \
  59. "jle 3b\n\t" \
  60. "cli\n\t" \
  61. "jmp 1b\n" \
  62. "4:\n\t"
  63. /*
  64. * This works. Despite all the confusion.
  65. * (except on PPro SMP or if we are using OOSTORE)
  66. * (PPro errata 66, 92)
  67. */
  68. #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
  69. #define spin_unlock_string \
  70. "movb $1,%0" \
  71. :"=m" (lock->slock) : : "memory"
  72. static inline void _raw_spin_unlock(spinlock_t *lock)
  73. {
  74. #ifdef CONFIG_DEBUG_SPINLOCK
  75. BUG_ON(lock->magic != SPINLOCK_MAGIC);
  76. BUG_ON(!spin_is_locked(lock));
  77. #endif
  78. __asm__ __volatile__(
  79. spin_unlock_string
  80. );
  81. }
  82. #else
  83. #define spin_unlock_string \
  84. "xchgb %b0, %1" \
  85. :"=q" (oldval), "=m" (lock->slock) \
  86. :"0" (oldval) : "memory"
  87. static inline void _raw_spin_unlock(spinlock_t *lock)
  88. {
  89. char oldval = 1;
  90. #ifdef CONFIG_DEBUG_SPINLOCK
  91. BUG_ON(lock->magic != SPINLOCK_MAGIC);
  92. BUG_ON(!spin_is_locked(lock));
  93. #endif
  94. __asm__ __volatile__(
  95. spin_unlock_string
  96. );
  97. }
  98. #endif
  99. static inline int _raw_spin_trylock(spinlock_t *lock)
  100. {
  101. char oldval;
  102. __asm__ __volatile__(
  103. "xchgb %b0,%1"
  104. :"=q" (oldval), "=m" (lock->slock)
  105. :"0" (0) : "memory");
  106. return oldval > 0;
  107. }
  108. static inline void _raw_spin_lock(spinlock_t *lock)
  109. {
  110. #ifdef CONFIG_DEBUG_SPINLOCK
  111. if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
  112. printk("eip: %p\n", __builtin_return_address(0));
  113. BUG();
  114. }
  115. #endif
  116. __asm__ __volatile__(
  117. spin_lock_string
  118. :"=m" (lock->slock) : : "memory");
  119. }
  120. static inline void _raw_spin_lock_flags (spinlock_t *lock, unsigned long flags)
  121. {
  122. #ifdef CONFIG_DEBUG_SPINLOCK
  123. if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
  124. printk("eip: %p\n", __builtin_return_address(0));
  125. BUG();
  126. }
  127. #endif
  128. __asm__ __volatile__(
  129. spin_lock_string_flags
  130. :"=m" (lock->slock) : "r" (flags) : "memory");
  131. }
  132. /*
  133. * Read-write spinlocks, allowing multiple readers
  134. * but only one writer.
  135. *
  136. * NOTE! it is quite common to have readers in interrupts
  137. * but no interrupt writers. For those circumstances we
  138. * can "mix" irq-safe locks - any writer needs to get a
  139. * irq-safe write-lock, but readers can get non-irqsafe
  140. * read-locks.
  141. */
  142. typedef struct {
  143. volatile unsigned int lock;
  144. #ifdef CONFIG_DEBUG_SPINLOCK
  145. unsigned magic;
  146. #endif
  147. #ifdef CONFIG_PREEMPT
  148. unsigned int break_lock;
  149. #endif
  150. } rwlock_t;
  151. #define RWLOCK_MAGIC 0xdeaf1eed
  152. #ifdef CONFIG_DEBUG_SPINLOCK
  153. #define RWLOCK_MAGIC_INIT , RWLOCK_MAGIC
  154. #else
  155. #define RWLOCK_MAGIC_INIT /* */
  156. #endif
  157. #define RW_LOCK_UNLOCKED (rwlock_t) { RW_LOCK_BIAS RWLOCK_MAGIC_INIT }
  158. #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
  159. /**
  160. * read_can_lock - would read_trylock() succeed?
  161. * @lock: the rwlock in question.
  162. */
  163. #define read_can_lock(x) ((int)(x)->lock > 0)
  164. /**
  165. * write_can_lock - would write_trylock() succeed?
  166. * @lock: the rwlock in question.
  167. */
  168. #define write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
  169. /*
  170. * On x86, we implement read-write locks as a 32-bit counter
  171. * with the high bit (sign) being the "contended" bit.
  172. *
  173. * The inline assembly is non-obvious. Think about it.
  174. *
  175. * Changed to use the same technique as rw semaphores. See
  176. * semaphore.h for details. -ben
  177. */
  178. /* the spinlock helpers are in arch/i386/kernel/semaphore.c */
  179. static inline void _raw_read_lock(rwlock_t *rw)
  180. {
  181. #ifdef CONFIG_DEBUG_SPINLOCK
  182. BUG_ON(rw->magic != RWLOCK_MAGIC);
  183. #endif
  184. __build_read_lock(rw, "__read_lock_failed");
  185. }
  186. static inline void _raw_write_lock(rwlock_t *rw)
  187. {
  188. #ifdef CONFIG_DEBUG_SPINLOCK
  189. BUG_ON(rw->magic != RWLOCK_MAGIC);
  190. #endif
  191. __build_write_lock(rw, "__write_lock_failed");
  192. }
  193. #define _raw_read_unlock(rw) asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory")
  194. #define _raw_write_unlock(rw) asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
  195. static inline int _raw_read_trylock(rwlock_t *lock)
  196. {
  197. atomic_t *count = (atomic_t *)lock;
  198. atomic_dec(count);
  199. if (atomic_read(count) >= 0)
  200. return 1;
  201. atomic_inc(count);
  202. return 0;
  203. }
  204. static inline int _raw_write_trylock(rwlock_t *lock)
  205. {
  206. atomic_t *count = (atomic_t *)lock;
  207. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  208. return 1;
  209. atomic_add(RW_LOCK_BIAS, count);
  210. return 0;
  211. }
  212. #endif /* __ASM_SPINLOCK_H */