spinlock.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #ifndef __ASM_SPINLOCK_H
  2. #define __ASM_SPINLOCK_H
  3. /*
  4. * Simple spin lock operations.
  5. *
  6. * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM
  7. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  8. * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM
  9. * Rework to support virtual processors
  10. *
  11. * Type of int is used as a full 64b word is not necessary.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. *
  18. * (the type definitions are in asm/spinlock_types.h)
  19. */
  20. #include <linux/config.h>
  21. #include <asm/paca.h>
  22. #include <asm/hvcall.h>
  23. #include <asm/iSeries/HvCall.h>
  24. #define __raw_spin_is_locked(x) ((x)->slock != 0)
  25. /*
  26. * This returns the old value in the lock, so we succeeded
  27. * in getting the lock if the return value is 0.
  28. */
  29. static __inline__ unsigned long __spin_trylock(raw_spinlock_t *lock)
  30. {
  31. unsigned long tmp, tmp2;
  32. __asm__ __volatile__(
  33. " lwz %1,%3(13) # __spin_trylock\n\
  34. 1: lwarx %0,0,%2\n\
  35. cmpwi 0,%0,0\n\
  36. bne- 2f\n\
  37. stwcx. %1,0,%2\n\
  38. bne- 1b\n\
  39. isync\n\
  40. 2:" : "=&r" (tmp), "=&r" (tmp2)
  41. : "r" (&lock->slock), "i" (offsetof(struct paca_struct, lock_token))
  42. : "cr0", "memory");
  43. return tmp;
  44. }
  45. static int __inline__ __raw_spin_trylock(raw_spinlock_t *lock)
  46. {
  47. return __spin_trylock(lock) == 0;
  48. }
  49. /*
  50. * On a system with shared processors (that is, where a physical
  51. * processor is multiplexed between several virtual processors),
  52. * there is no point spinning on a lock if the holder of the lock
  53. * isn't currently scheduled on a physical processor. Instead
  54. * we detect this situation and ask the hypervisor to give the
  55. * rest of our timeslice to the lock holder.
  56. *
  57. * So that we can tell which virtual processor is holding a lock,
  58. * we put 0x80000000 | smp_processor_id() in the lock when it is
  59. * held. Conveniently, we have a word in the paca that holds this
  60. * value.
  61. */
  62. #if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
  63. /* We only yield to the hypervisor if we are in shared processor mode */
  64. #define SHARED_PROCESSOR (get_paca()->lppaca.shared_proc)
  65. extern void __spin_yield(raw_spinlock_t *lock);
  66. extern void __rw_yield(raw_rwlock_t *lock);
  67. #else /* SPLPAR || ISERIES */
  68. #define __spin_yield(x) barrier()
  69. #define __rw_yield(x) barrier()
  70. #define SHARED_PROCESSOR 0
  71. #endif
  72. static void __inline__ __raw_spin_lock(raw_spinlock_t *lock)
  73. {
  74. while (1) {
  75. if (likely(__spin_trylock(lock) == 0))
  76. break;
  77. do {
  78. HMT_low();
  79. if (SHARED_PROCESSOR)
  80. __spin_yield(lock);
  81. } while (unlikely(lock->slock != 0));
  82. HMT_medium();
  83. }
  84. }
  85. static void __inline__ __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
  86. {
  87. unsigned long flags_dis;
  88. while (1) {
  89. if (likely(__spin_trylock(lock) == 0))
  90. break;
  91. local_save_flags(flags_dis);
  92. local_irq_restore(flags);
  93. do {
  94. HMT_low();
  95. if (SHARED_PROCESSOR)
  96. __spin_yield(lock);
  97. } while (unlikely(lock->slock != 0));
  98. HMT_medium();
  99. local_irq_restore(flags_dis);
  100. }
  101. }
  102. static __inline__ void __raw_spin_unlock(raw_spinlock_t *lock)
  103. {
  104. __asm__ __volatile__("lwsync # __raw_spin_unlock": : :"memory");
  105. lock->slock = 0;
  106. }
  107. extern void __raw_spin_unlock_wait(raw_spinlock_t *lock);
  108. /*
  109. * Read-write spinlocks, allowing multiple readers
  110. * but only one writer.
  111. *
  112. * NOTE! it is quite common to have readers in interrupts
  113. * but no interrupt writers. For those circumstances we
  114. * can "mix" irq-safe locks - any writer needs to get a
  115. * irq-safe write-lock, but readers can get non-irqsafe
  116. * read-locks.
  117. */
  118. #define __raw_read_can_lock(rw) ((rw)->lock >= 0)
  119. #define __raw_write_can_lock(rw) (!(rw)->lock)
  120. /*
  121. * This returns the old value in the lock + 1,
  122. * so we got a read lock if the return value is > 0.
  123. */
  124. static long __inline__ __read_trylock(raw_rwlock_t *rw)
  125. {
  126. long tmp;
  127. __asm__ __volatile__(
  128. "1: lwarx %0,0,%1 # read_trylock\n\
  129. extsw %0,%0\n\
  130. addic. %0,%0,1\n\
  131. ble- 2f\n\
  132. stwcx. %0,0,%1\n\
  133. bne- 1b\n\
  134. isync\n\
  135. 2:" : "=&r" (tmp)
  136. : "r" (&rw->lock)
  137. : "cr0", "xer", "memory");
  138. return tmp;
  139. }
  140. /*
  141. * This returns the old value in the lock,
  142. * so we got the write lock if the return value is 0.
  143. */
  144. static __inline__ long __write_trylock(raw_rwlock_t *rw)
  145. {
  146. long tmp, tmp2;
  147. __asm__ __volatile__(
  148. " lwz %1,%3(13) # write_trylock\n\
  149. 1: lwarx %0,0,%2\n\
  150. cmpwi 0,%0,0\n\
  151. bne- 2f\n\
  152. stwcx. %1,0,%2\n\
  153. bne- 1b\n\
  154. isync\n\
  155. 2:" : "=&r" (tmp), "=&r" (tmp2)
  156. : "r" (&rw->lock), "i" (offsetof(struct paca_struct, lock_token))
  157. : "cr0", "memory");
  158. return tmp;
  159. }
  160. static void __inline__ __raw_read_lock(raw_rwlock_t *rw)
  161. {
  162. while (1) {
  163. if (likely(__read_trylock(rw) > 0))
  164. break;
  165. do {
  166. HMT_low();
  167. if (SHARED_PROCESSOR)
  168. __rw_yield(rw);
  169. } while (unlikely(rw->lock < 0));
  170. HMT_medium();
  171. }
  172. }
  173. static void __inline__ __raw_write_lock(raw_rwlock_t *rw)
  174. {
  175. while (1) {
  176. if (likely(__write_trylock(rw) == 0))
  177. break;
  178. do {
  179. HMT_low();
  180. if (SHARED_PROCESSOR)
  181. __rw_yield(rw);
  182. } while (unlikely(rw->lock != 0));
  183. HMT_medium();
  184. }
  185. }
  186. static int __inline__ __raw_read_trylock(raw_rwlock_t *rw)
  187. {
  188. return __read_trylock(rw) > 0;
  189. }
  190. static int __inline__ __raw_write_trylock(raw_rwlock_t *rw)
  191. {
  192. return __write_trylock(rw) == 0;
  193. }
  194. static void __inline__ __raw_read_unlock(raw_rwlock_t *rw)
  195. {
  196. long tmp;
  197. __asm__ __volatile__(
  198. "eieio # read_unlock\n\
  199. 1: lwarx %0,0,%1\n\
  200. addic %0,%0,-1\n\
  201. stwcx. %0,0,%1\n\
  202. bne- 1b"
  203. : "=&r"(tmp)
  204. : "r"(&rw->lock)
  205. : "cr0", "memory");
  206. }
  207. static __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
  208. {
  209. __asm__ __volatile__("lwsync # write_unlock": : :"memory");
  210. rw->lock = 0;
  211. }
  212. #endif /* __ASM_SPINLOCK_H */