spinlock.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #ifndef __ASM_SPINLOCK_H
  2. #define __ASM_SPINLOCK_H
  3. #ifdef __KERNEL__
  4. /*
  5. * Simple spin lock operations.
  6. *
  7. * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM
  8. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  9. * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM
  10. * Rework to support virtual processors
  11. *
  12. * Type of int is used as a full 64b word is not necessary.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. * (the type definitions are in asm/spinlock_types.h)
  20. */
  21. #include <linux/irqflags.h>
  22. #ifdef CONFIG_PPC64
  23. #include <asm/paca.h>
  24. #include <asm/hvcall.h>
  25. #include <asm/iseries/hv_call.h>
  26. #endif
  27. #include <asm/asm-compat.h>
  28. #include <asm/synch.h>
  29. #define __raw_spin_is_locked(x) ((x)->slock != 0)
  30. #ifdef CONFIG_PPC64
  31. /* use 0x800000yy when locked, where yy == CPU number */
  32. #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
  33. #else
  34. #define LOCK_TOKEN 1
  35. #endif
  36. #if defined(CONFIG_PPC64) && defined(CONFIG_SMP)
  37. #define CLEAR_IO_SYNC (get_paca()->io_sync = 0)
  38. #define SYNC_IO do { \
  39. if (unlikely(get_paca()->io_sync)) { \
  40. mb(); \
  41. get_paca()->io_sync = 0; \
  42. } \
  43. } while (0)
  44. #else
  45. #define CLEAR_IO_SYNC
  46. #define SYNC_IO
  47. #endif
  48. /*
  49. * This returns the old value in the lock, so we succeeded
  50. * in getting the lock if the return value is 0.
  51. */
  52. static inline unsigned long __spin_trylock(raw_spinlock_t *lock)
  53. {
  54. unsigned long tmp, token;
  55. token = LOCK_TOKEN;
  56. __asm__ __volatile__(
  57. "1: lwarx %0,0,%2\n\
  58. cmpwi 0,%0,0\n\
  59. bne- 2f\n\
  60. stwcx. %1,0,%2\n\
  61. bne- 1b\n\
  62. isync\n\
  63. 2:" : "=&r" (tmp)
  64. : "r" (token), "r" (&lock->slock)
  65. : "cr0", "memory");
  66. return tmp;
  67. }
  68. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  69. {
  70. CLEAR_IO_SYNC;
  71. return __spin_trylock(lock) == 0;
  72. }
  73. /*
  74. * On a system with shared processors (that is, where a physical
  75. * processor is multiplexed between several virtual processors),
  76. * there is no point spinning on a lock if the holder of the lock
  77. * isn't currently scheduled on a physical processor. Instead
  78. * we detect this situation and ask the hypervisor to give the
  79. * rest of our timeslice to the lock holder.
  80. *
  81. * So that we can tell which virtual processor is holding a lock,
  82. * we put 0x80000000 | smp_processor_id() in the lock when it is
  83. * held. Conveniently, we have a word in the paca that holds this
  84. * value.
  85. */
  86. #if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
  87. /* We only yield to the hypervisor if we are in shared processor mode */
  88. #define SHARED_PROCESSOR (get_lppaca()->shared_proc)
  89. extern void __spin_yield(raw_spinlock_t *lock);
  90. extern void __rw_yield(raw_rwlock_t *lock);
  91. #else /* SPLPAR || ISERIES */
  92. #define __spin_yield(x) barrier()
  93. #define __rw_yield(x) barrier()
  94. #define SHARED_PROCESSOR 0
  95. #endif
  96. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  97. {
  98. CLEAR_IO_SYNC;
  99. while (1) {
  100. if (likely(__spin_trylock(lock) == 0))
  101. break;
  102. do {
  103. HMT_low();
  104. if (SHARED_PROCESSOR)
  105. __spin_yield(lock);
  106. } while (unlikely(lock->slock != 0));
  107. HMT_medium();
  108. }
  109. }
  110. static inline
  111. void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
  112. {
  113. unsigned long flags_dis;
  114. CLEAR_IO_SYNC;
  115. while (1) {
  116. if (likely(__spin_trylock(lock) == 0))
  117. break;
  118. local_save_flags(flags_dis);
  119. local_irq_restore(flags);
  120. do {
  121. HMT_low();
  122. if (SHARED_PROCESSOR)
  123. __spin_yield(lock);
  124. } while (unlikely(lock->slock != 0));
  125. HMT_medium();
  126. local_irq_restore(flags_dis);
  127. }
  128. }
  129. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  130. {
  131. SYNC_IO;
  132. __asm__ __volatile__("# __raw_spin_unlock\n\t"
  133. LWSYNC_ON_SMP: : :"memory");
  134. lock->slock = 0;
  135. }
  136. #ifdef CONFIG_PPC64
  137. extern void __raw_spin_unlock_wait(raw_spinlock_t *lock);
  138. #else
  139. #define __raw_spin_unlock_wait(lock) \
  140. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  141. #endif
  142. /*
  143. * Read-write spinlocks, allowing multiple readers
  144. * but only one writer.
  145. *
  146. * NOTE! it is quite common to have readers in interrupts
  147. * but no interrupt writers. For those circumstances we
  148. * can "mix" irq-safe locks - any writer needs to get a
  149. * irq-safe write-lock, but readers can get non-irqsafe
  150. * read-locks.
  151. */
  152. #define __raw_read_can_lock(rw) ((rw)->lock >= 0)
  153. #define __raw_write_can_lock(rw) (!(rw)->lock)
  154. #ifdef CONFIG_PPC64
  155. #define __DO_SIGN_EXTEND "extsw %0,%0\n"
  156. #define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
  157. #else
  158. #define __DO_SIGN_EXTEND
  159. #define WRLOCK_TOKEN (-1)
  160. #endif
  161. /*
  162. * This returns the old value in the lock + 1,
  163. * so we got a read lock if the return value is > 0.
  164. */
  165. static inline long __read_trylock(raw_rwlock_t *rw)
  166. {
  167. long tmp;
  168. __asm__ __volatile__(
  169. "1: lwarx %0,0,%1\n"
  170. __DO_SIGN_EXTEND
  171. " addic. %0,%0,1\n\
  172. ble- 2f\n"
  173. PPC405_ERR77(0,%1)
  174. " stwcx. %0,0,%1\n\
  175. bne- 1b\n\
  176. isync\n\
  177. 2:" : "=&r" (tmp)
  178. : "r" (&rw->lock)
  179. : "cr0", "xer", "memory");
  180. return tmp;
  181. }
  182. /*
  183. * This returns the old value in the lock,
  184. * so we got the write lock if the return value is 0.
  185. */
  186. static inline long __write_trylock(raw_rwlock_t *rw)
  187. {
  188. long tmp, token;
  189. token = WRLOCK_TOKEN;
  190. __asm__ __volatile__(
  191. "1: lwarx %0,0,%2\n\
  192. cmpwi 0,%0,0\n\
  193. bne- 2f\n"
  194. PPC405_ERR77(0,%1)
  195. " stwcx. %1,0,%2\n\
  196. bne- 1b\n\
  197. isync\n\
  198. 2:" : "=&r" (tmp)
  199. : "r" (token), "r" (&rw->lock)
  200. : "cr0", "memory");
  201. return tmp;
  202. }
  203. static inline void __raw_read_lock(raw_rwlock_t *rw)
  204. {
  205. while (1) {
  206. if (likely(__read_trylock(rw) > 0))
  207. break;
  208. do {
  209. HMT_low();
  210. if (SHARED_PROCESSOR)
  211. __rw_yield(rw);
  212. } while (unlikely(rw->lock < 0));
  213. HMT_medium();
  214. }
  215. }
  216. static inline void __raw_write_lock(raw_rwlock_t *rw)
  217. {
  218. while (1) {
  219. if (likely(__write_trylock(rw) == 0))
  220. break;
  221. do {
  222. HMT_low();
  223. if (SHARED_PROCESSOR)
  224. __rw_yield(rw);
  225. } while (unlikely(rw->lock != 0));
  226. HMT_medium();
  227. }
  228. }
  229. static inline int __raw_read_trylock(raw_rwlock_t *rw)
  230. {
  231. return __read_trylock(rw) > 0;
  232. }
  233. static inline int __raw_write_trylock(raw_rwlock_t *rw)
  234. {
  235. return __write_trylock(rw) == 0;
  236. }
  237. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  238. {
  239. long tmp;
  240. __asm__ __volatile__(
  241. "# read_unlock\n\t"
  242. LWSYNC_ON_SMP
  243. "1: lwarx %0,0,%1\n\
  244. addic %0,%0,-1\n"
  245. PPC405_ERR77(0,%1)
  246. " stwcx. %0,0,%1\n\
  247. bne- 1b"
  248. : "=&r"(tmp)
  249. : "r"(&rw->lock)
  250. : "cr0", "xer", "memory");
  251. }
  252. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  253. {
  254. __asm__ __volatile__("# write_unlock\n\t"
  255. LWSYNC_ON_SMP: : :"memory");
  256. rw->lock = 0;
  257. }
  258. #define __raw_read_lock_flags(lock, flags) __raw_read_lock(lock)
  259. #define __raw_write_lock_flags(lock, flags) __raw_write_lock(lock)
  260. #define _raw_spin_relax(lock) __spin_yield(lock)
  261. #define _raw_read_relax(lock) __rw_yield(lock)
  262. #define _raw_write_relax(lock) __rw_yield(lock)
  263. #endif /* __KERNEL__ */
  264. #endif /* __ASM_SPINLOCK_H */