spinlock.h 7.1 KB

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