spinlock.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Out of line spinlock code.
  3. *
  4. * Copyright IBM Corp. 2004, 2006
  5. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  6. */
  7. #include <linux/types.h>
  8. #include <linux/module.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/init.h>
  11. #include <linux/smp.h>
  12. #include <asm/io.h>
  13. int spin_retry = -1;
  14. static int __init spin_retry_init(void)
  15. {
  16. if (spin_retry < 0)
  17. spin_retry = MACHINE_HAS_CAD ? 10 : 1000;
  18. return 0;
  19. }
  20. early_initcall(spin_retry_init);
  21. /**
  22. * spin_retry= parameter
  23. */
  24. static int __init spin_retry_setup(char *str)
  25. {
  26. spin_retry = simple_strtoul(str, &str, 0);
  27. return 1;
  28. }
  29. __setup("spin_retry=", spin_retry_setup);
  30. static inline void _raw_compare_and_delay(unsigned int *lock, unsigned int old)
  31. {
  32. asm(".insn rsy,0xeb0000000022,%0,0,%1" : : "d" (old), "Q" (*lock));
  33. }
  34. void arch_spin_lock_wait(arch_spinlock_t *lp)
  35. {
  36. unsigned int cpu = SPINLOCK_LOCKVAL;
  37. unsigned int owner;
  38. int count;
  39. while (1) {
  40. owner = ACCESS_ONCE(lp->lock);
  41. /* Try to get the lock if it is free. */
  42. if (!owner) {
  43. if (_raw_compare_and_swap(&lp->lock, 0, cpu))
  44. return;
  45. continue;
  46. }
  47. /* Check if the lock owner is running. */
  48. if (!smp_vcpu_scheduled(~owner)) {
  49. smp_yield_cpu(~owner);
  50. continue;
  51. }
  52. /* Loop for a while on the lock value. */
  53. count = spin_retry;
  54. do {
  55. if (MACHINE_HAS_CAD)
  56. _raw_compare_and_delay(&lp->lock, owner);
  57. owner = ACCESS_ONCE(lp->lock);
  58. } while (owner && count-- > 0);
  59. if (!owner)
  60. continue;
  61. /*
  62. * For multiple layers of hypervisors, e.g. z/VM + LPAR
  63. * yield the CPU if the lock is still unavailable.
  64. */
  65. if (!MACHINE_IS_LPAR)
  66. smp_yield_cpu(~owner);
  67. }
  68. }
  69. EXPORT_SYMBOL(arch_spin_lock_wait);
  70. void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags)
  71. {
  72. unsigned int cpu = SPINLOCK_LOCKVAL;
  73. unsigned int owner;
  74. int count;
  75. local_irq_restore(flags);
  76. while (1) {
  77. owner = ACCESS_ONCE(lp->lock);
  78. /* Try to get the lock if it is free. */
  79. if (!owner) {
  80. local_irq_disable();
  81. if (_raw_compare_and_swap(&lp->lock, 0, cpu))
  82. return;
  83. local_irq_restore(flags);
  84. }
  85. /* Check if the lock owner is running. */
  86. if (!smp_vcpu_scheduled(~owner)) {
  87. smp_yield_cpu(~owner);
  88. continue;
  89. }
  90. /* Loop for a while on the lock value. */
  91. count = spin_retry;
  92. do {
  93. if (MACHINE_HAS_CAD)
  94. _raw_compare_and_delay(&lp->lock, owner);
  95. owner = ACCESS_ONCE(lp->lock);
  96. } while (owner && count-- > 0);
  97. if (!owner)
  98. continue;
  99. /*
  100. * For multiple layers of hypervisors, e.g. z/VM + LPAR
  101. * yield the CPU if the lock is still unavailable.
  102. */
  103. if (!MACHINE_IS_LPAR)
  104. smp_yield_cpu(~owner);
  105. }
  106. }
  107. EXPORT_SYMBOL(arch_spin_lock_wait_flags);
  108. int arch_spin_trylock_retry(arch_spinlock_t *lp)
  109. {
  110. unsigned int cpu = SPINLOCK_LOCKVAL;
  111. unsigned int owner;
  112. int count;
  113. for (count = spin_retry; count > 0; count--) {
  114. owner = ACCESS_ONCE(lp->lock);
  115. /* Try to get the lock if it is free. */
  116. if (!owner) {
  117. if (_raw_compare_and_swap(&lp->lock, 0, cpu))
  118. return 1;
  119. } else if (MACHINE_HAS_CAD)
  120. _raw_compare_and_delay(&lp->lock, owner);
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(arch_spin_trylock_retry);
  125. void _raw_read_lock_wait(arch_rwlock_t *rw)
  126. {
  127. unsigned int owner, old;
  128. int count = spin_retry;
  129. #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
  130. __RAW_LOCK(&rw->lock, -1, __RAW_OP_ADD);
  131. #endif
  132. owner = 0;
  133. while (1) {
  134. if (count-- <= 0) {
  135. if (owner && !smp_vcpu_scheduled(~owner))
  136. smp_yield_cpu(~owner);
  137. count = spin_retry;
  138. }
  139. old = ACCESS_ONCE(rw->lock);
  140. owner = ACCESS_ONCE(rw->owner);
  141. if ((int) old < 0) {
  142. if (MACHINE_HAS_CAD)
  143. _raw_compare_and_delay(&rw->lock, old);
  144. continue;
  145. }
  146. if (_raw_compare_and_swap(&rw->lock, old, old + 1))
  147. return;
  148. }
  149. }
  150. EXPORT_SYMBOL(_raw_read_lock_wait);
  151. int _raw_read_trylock_retry(arch_rwlock_t *rw)
  152. {
  153. unsigned int old;
  154. int count = spin_retry;
  155. while (count-- > 0) {
  156. old = ACCESS_ONCE(rw->lock);
  157. if ((int) old < 0) {
  158. if (MACHINE_HAS_CAD)
  159. _raw_compare_and_delay(&rw->lock, old);
  160. continue;
  161. }
  162. if (_raw_compare_and_swap(&rw->lock, old, old + 1))
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. EXPORT_SYMBOL(_raw_read_trylock_retry);
  168. #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
  169. void _raw_write_lock_wait(arch_rwlock_t *rw, unsigned int prev)
  170. {
  171. unsigned int owner, old;
  172. int count = spin_retry;
  173. owner = 0;
  174. while (1) {
  175. if (count-- <= 0) {
  176. if (owner && !smp_vcpu_scheduled(~owner))
  177. smp_yield_cpu(~owner);
  178. count = spin_retry;
  179. }
  180. old = ACCESS_ONCE(rw->lock);
  181. owner = ACCESS_ONCE(rw->owner);
  182. smp_rmb();
  183. if ((int) old >= 0) {
  184. prev = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR);
  185. old = prev;
  186. }
  187. if ((old & 0x7fffffff) == 0 && (int) prev >= 0)
  188. break;
  189. if (MACHINE_HAS_CAD)
  190. _raw_compare_and_delay(&rw->lock, old);
  191. }
  192. }
  193. EXPORT_SYMBOL(_raw_write_lock_wait);
  194. #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  195. void _raw_write_lock_wait(arch_rwlock_t *rw)
  196. {
  197. unsigned int owner, old, prev;
  198. int count = spin_retry;
  199. prev = 0x80000000;
  200. owner = 0;
  201. while (1) {
  202. if (count-- <= 0) {
  203. if (owner && !smp_vcpu_scheduled(~owner))
  204. smp_yield_cpu(~owner);
  205. count = spin_retry;
  206. }
  207. old = ACCESS_ONCE(rw->lock);
  208. owner = ACCESS_ONCE(rw->owner);
  209. if ((int) old >= 0 &&
  210. _raw_compare_and_swap(&rw->lock, old, old | 0x80000000))
  211. prev = old;
  212. else
  213. smp_rmb();
  214. if ((old & 0x7fffffff) == 0 && (int) prev >= 0)
  215. break;
  216. if (MACHINE_HAS_CAD)
  217. _raw_compare_and_delay(&rw->lock, old);
  218. }
  219. }
  220. EXPORT_SYMBOL(_raw_write_lock_wait);
  221. #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  222. int _raw_write_trylock_retry(arch_rwlock_t *rw)
  223. {
  224. unsigned int old;
  225. int count = spin_retry;
  226. while (count-- > 0) {
  227. old = ACCESS_ONCE(rw->lock);
  228. if (old) {
  229. if (MACHINE_HAS_CAD)
  230. _raw_compare_and_delay(&rw->lock, old);
  231. continue;
  232. }
  233. if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(_raw_write_trylock_retry);
  239. void arch_lock_relax(unsigned int cpu)
  240. {
  241. if (!cpu)
  242. return;
  243. if (MACHINE_IS_LPAR && smp_vcpu_scheduled(~cpu))
  244. return;
  245. smp_yield_cpu(~cpu);
  246. }
  247. EXPORT_SYMBOL(arch_lock_relax);