spinlock.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999
  4. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  5. *
  6. * Derived from "include/asm-i386/spinlock.h"
  7. */
  8. #ifndef __ASM_SPINLOCK_H
  9. #define __ASM_SPINLOCK_H
  10. #include <linux/smp.h>
  11. #include <asm/barrier.h>
  12. #include <asm/processor.h>
  13. #define SPINLOCK_LOCKVAL (S390_lowcore.spinlock_lockval)
  14. extern int spin_retry;
  15. static inline int
  16. _raw_compare_and_swap(unsigned int *lock, unsigned int old, unsigned int new)
  17. {
  18. return __sync_bool_compare_and_swap(lock, old, new);
  19. }
  20. /*
  21. * Simple spin lock operations. There are two variants, one clears IRQ's
  22. * on the local processor, one does not.
  23. *
  24. * We make no fairness assumptions. They have a cost.
  25. *
  26. * (the type definitions are in asm/spinlock_types.h)
  27. */
  28. void arch_lock_relax(unsigned int cpu);
  29. void arch_spin_lock_wait(arch_spinlock_t *);
  30. int arch_spin_trylock_retry(arch_spinlock_t *);
  31. void arch_spin_lock_wait_flags(arch_spinlock_t *, unsigned long flags);
  32. static inline void arch_spin_relax(arch_spinlock_t *lock)
  33. {
  34. arch_lock_relax(lock->lock);
  35. }
  36. static inline u32 arch_spin_lockval(int cpu)
  37. {
  38. return ~cpu;
  39. }
  40. static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  41. {
  42. return lock.lock == 0;
  43. }
  44. static inline int arch_spin_is_locked(arch_spinlock_t *lp)
  45. {
  46. return ACCESS_ONCE(lp->lock) != 0;
  47. }
  48. static inline int arch_spin_trylock_once(arch_spinlock_t *lp)
  49. {
  50. barrier();
  51. return likely(arch_spin_value_unlocked(*lp) &&
  52. _raw_compare_and_swap(&lp->lock, 0, SPINLOCK_LOCKVAL));
  53. }
  54. static inline void arch_spin_lock(arch_spinlock_t *lp)
  55. {
  56. if (!arch_spin_trylock_once(lp))
  57. arch_spin_lock_wait(lp);
  58. }
  59. static inline void arch_spin_lock_flags(arch_spinlock_t *lp,
  60. unsigned long flags)
  61. {
  62. if (!arch_spin_trylock_once(lp))
  63. arch_spin_lock_wait_flags(lp, flags);
  64. }
  65. static inline int arch_spin_trylock(arch_spinlock_t *lp)
  66. {
  67. if (!arch_spin_trylock_once(lp))
  68. return arch_spin_trylock_retry(lp);
  69. return 1;
  70. }
  71. static inline void arch_spin_unlock(arch_spinlock_t *lp)
  72. {
  73. typecheck(unsigned int, lp->lock);
  74. asm volatile(
  75. "st %1,%0\n"
  76. : "+Q" (lp->lock)
  77. : "d" (0)
  78. : "cc", "memory");
  79. }
  80. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  81. {
  82. while (arch_spin_is_locked(lock))
  83. arch_spin_relax(lock);
  84. smp_acquire__after_ctrl_dep();
  85. }
  86. /*
  87. * Read-write spinlocks, allowing multiple readers
  88. * but only one writer.
  89. *
  90. * NOTE! it is quite common to have readers in interrupts
  91. * but no interrupt writers. For those circumstances we
  92. * can "mix" irq-safe locks - any writer needs to get a
  93. * irq-safe write-lock, but readers can get non-irqsafe
  94. * read-locks.
  95. */
  96. /**
  97. * read_can_lock - would read_trylock() succeed?
  98. * @lock: the rwlock in question.
  99. */
  100. #define arch_read_can_lock(x) ((int)(x)->lock >= 0)
  101. /**
  102. * write_can_lock - would write_trylock() succeed?
  103. * @lock: the rwlock in question.
  104. */
  105. #define arch_write_can_lock(x) ((x)->lock == 0)
  106. extern int _raw_read_trylock_retry(arch_rwlock_t *lp);
  107. extern int _raw_write_trylock_retry(arch_rwlock_t *lp);
  108. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  109. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  110. static inline int arch_read_trylock_once(arch_rwlock_t *rw)
  111. {
  112. unsigned int old = ACCESS_ONCE(rw->lock);
  113. return likely((int) old >= 0 &&
  114. _raw_compare_and_swap(&rw->lock, old, old + 1));
  115. }
  116. static inline int arch_write_trylock_once(arch_rwlock_t *rw)
  117. {
  118. unsigned int old = ACCESS_ONCE(rw->lock);
  119. return likely(old == 0 &&
  120. _raw_compare_and_swap(&rw->lock, 0, 0x80000000));
  121. }
  122. #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
  123. #define __RAW_OP_OR "lao"
  124. #define __RAW_OP_AND "lan"
  125. #define __RAW_OP_ADD "laa"
  126. #define __RAW_LOCK(ptr, op_val, op_string) \
  127. ({ \
  128. unsigned int old_val; \
  129. \
  130. typecheck(unsigned int *, ptr); \
  131. asm volatile( \
  132. op_string " %0,%2,%1\n" \
  133. "bcr 14,0\n" \
  134. : "=d" (old_val), "+Q" (*ptr) \
  135. : "d" (op_val) \
  136. : "cc", "memory"); \
  137. old_val; \
  138. })
  139. #define __RAW_UNLOCK(ptr, op_val, op_string) \
  140. ({ \
  141. unsigned int old_val; \
  142. \
  143. typecheck(unsigned int *, ptr); \
  144. asm volatile( \
  145. op_string " %0,%2,%1\n" \
  146. : "=d" (old_val), "+Q" (*ptr) \
  147. : "d" (op_val) \
  148. : "cc", "memory"); \
  149. old_val; \
  150. })
  151. extern void _raw_read_lock_wait(arch_rwlock_t *lp);
  152. extern void _raw_write_lock_wait(arch_rwlock_t *lp, unsigned int prev);
  153. static inline void arch_read_lock(arch_rwlock_t *rw)
  154. {
  155. unsigned int old;
  156. old = __RAW_LOCK(&rw->lock, 1, __RAW_OP_ADD);
  157. if ((int) old < 0)
  158. _raw_read_lock_wait(rw);
  159. }
  160. static inline void arch_read_unlock(arch_rwlock_t *rw)
  161. {
  162. __RAW_UNLOCK(&rw->lock, -1, __RAW_OP_ADD);
  163. }
  164. static inline void arch_write_lock(arch_rwlock_t *rw)
  165. {
  166. unsigned int old;
  167. old = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR);
  168. if (old != 0)
  169. _raw_write_lock_wait(rw, old);
  170. rw->owner = SPINLOCK_LOCKVAL;
  171. }
  172. static inline void arch_write_unlock(arch_rwlock_t *rw)
  173. {
  174. rw->owner = 0;
  175. __RAW_UNLOCK(&rw->lock, 0x7fffffff, __RAW_OP_AND);
  176. }
  177. #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  178. extern void _raw_read_lock_wait(arch_rwlock_t *lp);
  179. extern void _raw_write_lock_wait(arch_rwlock_t *lp);
  180. static inline void arch_read_lock(arch_rwlock_t *rw)
  181. {
  182. if (!arch_read_trylock_once(rw))
  183. _raw_read_lock_wait(rw);
  184. }
  185. static inline void arch_read_unlock(arch_rwlock_t *rw)
  186. {
  187. unsigned int old;
  188. do {
  189. old = ACCESS_ONCE(rw->lock);
  190. } while (!_raw_compare_and_swap(&rw->lock, old, old - 1));
  191. }
  192. static inline void arch_write_lock(arch_rwlock_t *rw)
  193. {
  194. if (!arch_write_trylock_once(rw))
  195. _raw_write_lock_wait(rw);
  196. rw->owner = SPINLOCK_LOCKVAL;
  197. }
  198. static inline void arch_write_unlock(arch_rwlock_t *rw)
  199. {
  200. typecheck(unsigned int, rw->lock);
  201. rw->owner = 0;
  202. asm volatile(
  203. "st %1,%0\n"
  204. : "+Q" (rw->lock)
  205. : "d" (0)
  206. : "cc", "memory");
  207. }
  208. #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  209. static inline int arch_read_trylock(arch_rwlock_t *rw)
  210. {
  211. if (!arch_read_trylock_once(rw))
  212. return _raw_read_trylock_retry(rw);
  213. return 1;
  214. }
  215. static inline int arch_write_trylock(arch_rwlock_t *rw)
  216. {
  217. if (!arch_write_trylock_once(rw) && !_raw_write_trylock_retry(rw))
  218. return 0;
  219. rw->owner = SPINLOCK_LOCKVAL;
  220. return 1;
  221. }
  222. static inline void arch_read_relax(arch_rwlock_t *rw)
  223. {
  224. arch_lock_relax(rw->owner);
  225. }
  226. static inline void arch_write_relax(arch_rwlock_t *rw)
  227. {
  228. arch_lock_relax(rw->owner);
  229. }
  230. #endif /* __ASM_SPINLOCK_H */