spinlock.h 7.5 KB

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