spinlock_32.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <asm/processor.h>
  17. #include <arch/spr_def.h>
  18. #include "spinlock_common.h"
  19. void arch_spin_lock(arch_spinlock_t *lock)
  20. {
  21. int my_ticket;
  22. int iterations = 0;
  23. int delta;
  24. while ((my_ticket = __insn_tns((void *)&lock->next_ticket)) & 1)
  25. delay_backoff(iterations++);
  26. /* Increment the next ticket number, implicitly releasing tns lock. */
  27. lock->next_ticket = my_ticket + TICKET_QUANTUM;
  28. /* Wait until it's our turn. */
  29. while ((delta = my_ticket - lock->current_ticket) != 0)
  30. relax((128 / CYCLES_PER_RELAX_LOOP) * delta);
  31. }
  32. EXPORT_SYMBOL(arch_spin_lock);
  33. int arch_spin_trylock(arch_spinlock_t *lock)
  34. {
  35. /*
  36. * Grab a ticket; no need to retry if it's busy, we'll just
  37. * treat that the same as "locked", since someone else
  38. * will lock it momentarily anyway.
  39. */
  40. int my_ticket = __insn_tns((void *)&lock->next_ticket);
  41. if (my_ticket == lock->current_ticket) {
  42. /* Not currently locked, so lock it by keeping this ticket. */
  43. lock->next_ticket = my_ticket + TICKET_QUANTUM;
  44. /* Success! */
  45. return 1;
  46. }
  47. if (!(my_ticket & 1)) {
  48. /* Release next_ticket. */
  49. lock->next_ticket = my_ticket;
  50. }
  51. return 0;
  52. }
  53. EXPORT_SYMBOL(arch_spin_trylock);
  54. void arch_spin_unlock_wait(arch_spinlock_t *lock)
  55. {
  56. u32 iterations = 0;
  57. while (arch_spin_is_locked(lock))
  58. delay_backoff(iterations++);
  59. }
  60. EXPORT_SYMBOL(arch_spin_unlock_wait);
  61. /*
  62. * The low byte is always reserved to be the marker for a "tns" operation
  63. * since the low bit is set to "1" by a tns. The next seven bits are
  64. * zeroes. The next byte holds the "next" writer value, i.e. the ticket
  65. * available for the next task that wants to write. The third byte holds
  66. * the current writer value, i.e. the writer who holds the current ticket.
  67. * If current == next == 0, there are no interested writers.
  68. */
  69. #define WR_NEXT_SHIFT _WR_NEXT_SHIFT
  70. #define WR_CURR_SHIFT _WR_CURR_SHIFT
  71. #define WR_WIDTH _WR_WIDTH
  72. #define WR_MASK ((1 << WR_WIDTH) - 1)
  73. /*
  74. * The last eight bits hold the active reader count. This has to be
  75. * zero before a writer can start to write.
  76. */
  77. #define RD_COUNT_SHIFT _RD_COUNT_SHIFT
  78. #define RD_COUNT_WIDTH _RD_COUNT_WIDTH
  79. #define RD_COUNT_MASK ((1 << RD_COUNT_WIDTH) - 1)
  80. /*
  81. * We can get the read lock if everything but the reader bits (which
  82. * are in the high part of the word) is zero, i.e. no active or
  83. * waiting writers, no tns.
  84. *
  85. * We guard the tns/store-back with an interrupt critical section to
  86. * preserve the semantic that the same read lock can be acquired in an
  87. * interrupt context.
  88. */
  89. inline int arch_read_trylock(arch_rwlock_t *rwlock)
  90. {
  91. u32 val;
  92. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 1);
  93. val = __insn_tns((int *)&rwlock->lock);
  94. if (likely((val << _RD_COUNT_WIDTH) == 0)) {
  95. val += 1 << RD_COUNT_SHIFT;
  96. rwlock->lock = val;
  97. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  98. BUG_ON(val == 0); /* we don't expect wraparound */
  99. return 1;
  100. }
  101. if ((val & 1) == 0)
  102. rwlock->lock = val;
  103. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  104. return 0;
  105. }
  106. EXPORT_SYMBOL(arch_read_trylock);
  107. /*
  108. * Spin doing arch_read_trylock() until we acquire the lock.
  109. * ISSUE: This approach can permanently starve readers. A reader who sees
  110. * a writer could instead take a ticket lock (just like a writer would),
  111. * and atomically enter read mode (with 1 reader) when it gets the ticket.
  112. * This way both readers and writers would always make forward progress
  113. * in a finite time.
  114. */
  115. void arch_read_lock(arch_rwlock_t *rwlock)
  116. {
  117. u32 iterations = 0;
  118. while (unlikely(!arch_read_trylock(rwlock)))
  119. delay_backoff(iterations++);
  120. }
  121. EXPORT_SYMBOL(arch_read_lock);
  122. void arch_read_unlock(arch_rwlock_t *rwlock)
  123. {
  124. u32 val, iterations = 0;
  125. mb(); /* guarantee anything modified under the lock is visible */
  126. for (;;) {
  127. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 1);
  128. val = __insn_tns((int *)&rwlock->lock);
  129. if (likely((val & 1) == 0)) {
  130. rwlock->lock = val - (1 << _RD_COUNT_SHIFT);
  131. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  132. break;
  133. }
  134. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  135. delay_backoff(iterations++);
  136. }
  137. }
  138. EXPORT_SYMBOL(arch_read_unlock);
  139. /*
  140. * We don't need an interrupt critical section here (unlike for
  141. * arch_read_lock) since we should never use a bare write lock where
  142. * it could be interrupted by code that could try to re-acquire it.
  143. */
  144. void arch_write_lock(arch_rwlock_t *rwlock)
  145. {
  146. /*
  147. * The trailing underscore on this variable (and curr_ below)
  148. * reminds us that the high bits are garbage; we mask them out
  149. * when we compare them.
  150. */
  151. u32 my_ticket_;
  152. u32 iterations = 0;
  153. u32 val = __insn_tns((int *)&rwlock->lock);
  154. if (likely(val == 0)) {
  155. rwlock->lock = 1 << _WR_NEXT_SHIFT;
  156. return;
  157. }
  158. /*
  159. * Wait until there are no readers, then bump up the next
  160. * field and capture the ticket value.
  161. */
  162. for (;;) {
  163. if (!(val & 1)) {
  164. if ((val >> RD_COUNT_SHIFT) == 0)
  165. break;
  166. rwlock->lock = val;
  167. }
  168. delay_backoff(iterations++);
  169. val = __insn_tns((int *)&rwlock->lock);
  170. }
  171. /* Take out the next ticket and extract my ticket value. */
  172. rwlock->lock = __insn_addb(val, 1 << WR_NEXT_SHIFT);
  173. my_ticket_ = val >> WR_NEXT_SHIFT;
  174. /* Wait until the "current" field matches our ticket. */
  175. for (;;) {
  176. u32 curr_ = val >> WR_CURR_SHIFT;
  177. u32 delta = ((my_ticket_ - curr_) & WR_MASK);
  178. if (likely(delta == 0))
  179. break;
  180. /* Delay based on how many lock-holders are still out there. */
  181. relax((256 / CYCLES_PER_RELAX_LOOP) * delta);
  182. /*
  183. * Get a non-tns value to check; we don't need to tns
  184. * it ourselves. Since we're not tns'ing, we retry
  185. * more rapidly to get a valid value.
  186. */
  187. while ((val = rwlock->lock) & 1)
  188. relax(4);
  189. }
  190. }
  191. EXPORT_SYMBOL(arch_write_lock);
  192. int arch_write_trylock(arch_rwlock_t *rwlock)
  193. {
  194. u32 val = __insn_tns((int *)&rwlock->lock);
  195. /*
  196. * If a tns is in progress, or there's a waiting or active locker,
  197. * or active readers, we can't take the lock, so give up.
  198. */
  199. if (unlikely(val != 0)) {
  200. if (!(val & 1))
  201. rwlock->lock = val;
  202. return 0;
  203. }
  204. /* Set the "next" field to mark it locked. */
  205. rwlock->lock = 1 << _WR_NEXT_SHIFT;
  206. return 1;
  207. }
  208. EXPORT_SYMBOL(arch_write_trylock);
  209. void arch_write_unlock(arch_rwlock_t *rwlock)
  210. {
  211. u32 val, eq, mask;
  212. mb(); /* guarantee anything modified under the lock is visible */
  213. val = __insn_tns((int *)&rwlock->lock);
  214. if (likely(val == (1 << _WR_NEXT_SHIFT))) {
  215. rwlock->lock = 0;
  216. return;
  217. }
  218. while (unlikely(val & 1)) {
  219. /* Limited backoff since we are the highest-priority task. */
  220. relax(4);
  221. val = __insn_tns((int *)&rwlock->lock);
  222. }
  223. mask = 1 << WR_CURR_SHIFT;
  224. val = __insn_addb(val, mask);
  225. eq = __insn_seqb(val, val << (WR_CURR_SHIFT - WR_NEXT_SHIFT));
  226. val = __insn_mz(eq & mask, val);
  227. rwlock->lock = val;
  228. }
  229. EXPORT_SYMBOL(arch_write_unlock);