spinlock.h 6.9 KB

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