spinlock.h 6.6 KB

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