spinlock.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <asm/ppc-opcode.h>
  30. #define arch_spin_is_locked(x) ((x)->slock != 0)
  31. #ifdef CONFIG_PPC64
  32. /* use 0x800000yy when locked, where yy == CPU number */
  33. #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
  34. #else
  35. #define LOCK_TOKEN 1
  36. #endif
  37. #if defined(CONFIG_PPC64) && defined(CONFIG_SMP)
  38. #define CLEAR_IO_SYNC (get_paca()->io_sync = 0)
  39. #define SYNC_IO do { \
  40. if (unlikely(get_paca()->io_sync)) { \
  41. mb(); \
  42. get_paca()->io_sync = 0; \
  43. } \
  44. } while (0)
  45. #else
  46. #define CLEAR_IO_SYNC
  47. #define SYNC_IO
  48. #endif
  49. /*
  50. * This returns the old value in the lock, so we succeeded
  51. * in getting the lock if the return value is 0.
  52. */
  53. static inline unsigned long __arch_spin_trylock(arch_spinlock_t *lock)
  54. {
  55. unsigned long tmp, token;
  56. token = LOCK_TOKEN;
  57. __asm__ __volatile__(
  58. "1: " PPC_LWARX(%0,0,%2,1) "\n\
  59. cmpwi 0,%0,0\n\
  60. bne- 2f\n\
  61. stwcx. %1,0,%2\n\
  62. bne- 1b\n"
  63. PPC_ACQUIRE_BARRIER
  64. "2:"
  65. : "=&r" (tmp)
  66. : "r" (token), "r" (&lock->slock)
  67. : "cr0", "memory");
  68. return tmp;
  69. }
  70. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  71. {
  72. CLEAR_IO_SYNC;
  73. return __arch_spin_trylock(lock) == 0;
  74. }
  75. /*
  76. * On a system with shared processors (that is, where a physical
  77. * processor is multiplexed between several virtual processors),
  78. * there is no point spinning on a lock if the holder of the lock
  79. * isn't currently scheduled on a physical processor. Instead
  80. * we detect this situation and ask the hypervisor to give the
  81. * rest of our timeslice to the lock holder.
  82. *
  83. * So that we can tell which virtual processor is holding a lock,
  84. * we put 0x80000000 | smp_processor_id() in the lock when it is
  85. * held. Conveniently, we have a word in the paca that holds this
  86. * value.
  87. */
  88. #if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
  89. /* We only yield to the hypervisor if we are in shared processor mode */
  90. #define SHARED_PROCESSOR (get_lppaca()->shared_proc)
  91. extern void __spin_yield(arch_spinlock_t *lock);
  92. extern void __rw_yield(arch_rwlock_t *lock);
  93. #else /* SPLPAR || ISERIES */
  94. #define __spin_yield(x) barrier()
  95. #define __rw_yield(x) barrier()
  96. #define SHARED_PROCESSOR 0
  97. #endif
  98. static inline void arch_spin_lock(arch_spinlock_t *lock)
  99. {
  100. CLEAR_IO_SYNC;
  101. while (1) {
  102. if (likely(__arch_spin_trylock(lock) == 0))
  103. break;
  104. do {
  105. HMT_low();
  106. if (SHARED_PROCESSOR)
  107. __spin_yield(lock);
  108. } while (unlikely(lock->slock != 0));
  109. HMT_medium();
  110. }
  111. }
  112. static inline
  113. void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags)
  114. {
  115. unsigned long flags_dis;
  116. CLEAR_IO_SYNC;
  117. while (1) {
  118. if (likely(__arch_spin_trylock(lock) == 0))
  119. break;
  120. local_save_flags(flags_dis);
  121. local_irq_restore(flags);
  122. do {
  123. HMT_low();
  124. if (SHARED_PROCESSOR)
  125. __spin_yield(lock);
  126. } while (unlikely(lock->slock != 0));
  127. HMT_medium();
  128. local_irq_restore(flags_dis);
  129. }
  130. }
  131. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  132. {
  133. SYNC_IO;
  134. __asm__ __volatile__("# arch_spin_unlock\n\t"
  135. PPC_RELEASE_BARRIER: : :"memory");
  136. lock->slock = 0;
  137. }
  138. #ifdef CONFIG_PPC64
  139. extern void arch_spin_unlock_wait(arch_spinlock_t *lock);
  140. #else
  141. #define arch_spin_unlock_wait(lock) \
  142. do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
  143. #endif
  144. /*
  145. * Read-write spinlocks, allowing multiple readers
  146. * but only one writer.
  147. *
  148. * NOTE! it is quite common to have readers in interrupts
  149. * but no interrupt writers. For those circumstances we
  150. * can "mix" irq-safe locks - any writer needs to get a
  151. * irq-safe write-lock, but readers can get non-irqsafe
  152. * read-locks.
  153. */
  154. #define arch_read_can_lock(rw) ((rw)->lock >= 0)
  155. #define arch_write_can_lock(rw) (!(rw)->lock)
  156. #ifdef CONFIG_PPC64
  157. #define __DO_SIGN_EXTEND "extsw %0,%0\n"
  158. #define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
  159. #else
  160. #define __DO_SIGN_EXTEND
  161. #define WRLOCK_TOKEN (-1)
  162. #endif
  163. /*
  164. * This returns the old value in the lock + 1,
  165. * so we got a read lock if the return value is > 0.
  166. */
  167. static inline long __arch_read_trylock(arch_rwlock_t *rw)
  168. {
  169. long tmp;
  170. __asm__ __volatile__(
  171. "1: " PPC_LWARX(%0,0,%1,1) "\n"
  172. __DO_SIGN_EXTEND
  173. " addic. %0,%0,1\n\
  174. ble- 2f\n"
  175. PPC405_ERR77(0,%1)
  176. " stwcx. %0,0,%1\n\
  177. bne- 1b\n"
  178. PPC_ACQUIRE_BARRIER
  179. "2:" : "=&r" (tmp)
  180. : "r" (&rw->lock)
  181. : "cr0", "xer", "memory");
  182. return tmp;
  183. }
  184. /*
  185. * This returns the old value in the lock,
  186. * so we got the write lock if the return value is 0.
  187. */
  188. static inline long __arch_write_trylock(arch_rwlock_t *rw)
  189. {
  190. long tmp, token;
  191. token = WRLOCK_TOKEN;
  192. __asm__ __volatile__(
  193. "1: " PPC_LWARX(%0,0,%2,1) "\n\
  194. cmpwi 0,%0,0\n\
  195. bne- 2f\n"
  196. PPC405_ERR77(0,%1)
  197. " stwcx. %1,0,%2\n\
  198. bne- 1b\n"
  199. PPC_ACQUIRE_BARRIER
  200. "2:" : "=&r" (tmp)
  201. : "r" (token), "r" (&rw->lock)
  202. : "cr0", "memory");
  203. return tmp;
  204. }
  205. static inline void arch_read_lock(arch_rwlock_t *rw)
  206. {
  207. while (1) {
  208. if (likely(__arch_read_trylock(rw) > 0))
  209. break;
  210. do {
  211. HMT_low();
  212. if (SHARED_PROCESSOR)
  213. __rw_yield(rw);
  214. } while (unlikely(rw->lock < 0));
  215. HMT_medium();
  216. }
  217. }
  218. static inline void arch_write_lock(arch_rwlock_t *rw)
  219. {
  220. while (1) {
  221. if (likely(__arch_write_trylock(rw) == 0))
  222. break;
  223. do {
  224. HMT_low();
  225. if (SHARED_PROCESSOR)
  226. __rw_yield(rw);
  227. } while (unlikely(rw->lock != 0));
  228. HMT_medium();
  229. }
  230. }
  231. static inline int arch_read_trylock(arch_rwlock_t *rw)
  232. {
  233. return __arch_read_trylock(rw) > 0;
  234. }
  235. static inline int arch_write_trylock(arch_rwlock_t *rw)
  236. {
  237. return __arch_write_trylock(rw) == 0;
  238. }
  239. static inline void arch_read_unlock(arch_rwlock_t *rw)
  240. {
  241. long tmp;
  242. __asm__ __volatile__(
  243. "# read_unlock\n\t"
  244. PPC_RELEASE_BARRIER
  245. "1: lwarx %0,0,%1\n\
  246. addic %0,%0,-1\n"
  247. PPC405_ERR77(0,%1)
  248. " stwcx. %0,0,%1\n\
  249. bne- 1b"
  250. : "=&r"(tmp)
  251. : "r"(&rw->lock)
  252. : "cr0", "xer", "memory");
  253. }
  254. static inline void arch_write_unlock(arch_rwlock_t *rw)
  255. {
  256. __asm__ __volatile__("# write_unlock\n\t"
  257. PPC_RELEASE_BARRIER: : :"memory");
  258. rw->lock = 0;
  259. }
  260. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  261. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  262. #define arch_spin_relax(lock) __spin_yield(lock)
  263. #define arch_read_relax(lock) __rw_yield(lock)
  264. #define arch_write_relax(lock) __rw_yield(lock)
  265. #endif /* __KERNEL__ */
  266. #endif /* __ASM_SPINLOCK_H */