spinlock_32.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. int curr = READ_ONCE(lock->current_ticket);
  58. int next = READ_ONCE(lock->next_ticket);
  59. /* Return immediately if unlocked. */
  60. if (next == curr)
  61. return;
  62. /* Wait until the current locker has released the lock. */
  63. do {
  64. delay_backoff(iterations++);
  65. } while (READ_ONCE(lock->current_ticket) == curr);
  66. }
  67. EXPORT_SYMBOL(arch_spin_unlock_wait);
  68. /*
  69. * The low byte is always reserved to be the marker for a "tns" operation
  70. * since the low bit is set to "1" by a tns. The next seven bits are
  71. * zeroes. The next byte holds the "next" writer value, i.e. the ticket
  72. * available for the next task that wants to write. The third byte holds
  73. * the current writer value, i.e. the writer who holds the current ticket.
  74. * If current == next == 0, there are no interested writers.
  75. */
  76. #define WR_NEXT_SHIFT _WR_NEXT_SHIFT
  77. #define WR_CURR_SHIFT _WR_CURR_SHIFT
  78. #define WR_WIDTH _WR_WIDTH
  79. #define WR_MASK ((1 << WR_WIDTH) - 1)
  80. /*
  81. * The last eight bits hold the active reader count. This has to be
  82. * zero before a writer can start to write.
  83. */
  84. #define RD_COUNT_SHIFT _RD_COUNT_SHIFT
  85. #define RD_COUNT_WIDTH _RD_COUNT_WIDTH
  86. #define RD_COUNT_MASK ((1 << RD_COUNT_WIDTH) - 1)
  87. /*
  88. * We can get the read lock if everything but the reader bits (which
  89. * are in the high part of the word) is zero, i.e. no active or
  90. * waiting writers, no tns.
  91. *
  92. * We guard the tns/store-back with an interrupt critical section to
  93. * preserve the semantic that the same read lock can be acquired in an
  94. * interrupt context.
  95. */
  96. int arch_read_trylock(arch_rwlock_t *rwlock)
  97. {
  98. u32 val;
  99. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 1);
  100. val = __insn_tns((int *)&rwlock->lock);
  101. if (likely((val << _RD_COUNT_WIDTH) == 0)) {
  102. val += 1 << RD_COUNT_SHIFT;
  103. rwlock->lock = val;
  104. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  105. BUG_ON(val == 0); /* we don't expect wraparound */
  106. return 1;
  107. }
  108. if ((val & 1) == 0)
  109. rwlock->lock = val;
  110. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  111. return 0;
  112. }
  113. EXPORT_SYMBOL(arch_read_trylock);
  114. /*
  115. * Spin doing arch_read_trylock() until we acquire the lock.
  116. * ISSUE: This approach can permanently starve readers. A reader who sees
  117. * a writer could instead take a ticket lock (just like a writer would),
  118. * and atomically enter read mode (with 1 reader) when it gets the ticket.
  119. * This way both readers and writers would always make forward progress
  120. * in a finite time.
  121. */
  122. void arch_read_lock(arch_rwlock_t *rwlock)
  123. {
  124. u32 iterations = 0;
  125. while (unlikely(!arch_read_trylock(rwlock)))
  126. delay_backoff(iterations++);
  127. }
  128. EXPORT_SYMBOL(arch_read_lock);
  129. void arch_read_unlock(arch_rwlock_t *rwlock)
  130. {
  131. u32 val, iterations = 0;
  132. mb(); /* guarantee anything modified under the lock is visible */
  133. for (;;) {
  134. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 1);
  135. val = __insn_tns((int *)&rwlock->lock);
  136. if (likely((val & 1) == 0)) {
  137. rwlock->lock = val - (1 << _RD_COUNT_SHIFT);
  138. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  139. break;
  140. }
  141. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  142. delay_backoff(iterations++);
  143. }
  144. }
  145. EXPORT_SYMBOL(arch_read_unlock);
  146. /*
  147. * We don't need an interrupt critical section here (unlike for
  148. * arch_read_lock) since we should never use a bare write lock where
  149. * it could be interrupted by code that could try to re-acquire it.
  150. */
  151. void arch_write_lock(arch_rwlock_t *rwlock)
  152. {
  153. /*
  154. * The trailing underscore on this variable (and curr_ below)
  155. * reminds us that the high bits are garbage; we mask them out
  156. * when we compare them.
  157. */
  158. u32 my_ticket_;
  159. u32 iterations = 0;
  160. u32 val = __insn_tns((int *)&rwlock->lock);
  161. if (likely(val == 0)) {
  162. rwlock->lock = 1 << _WR_NEXT_SHIFT;
  163. return;
  164. }
  165. /*
  166. * Wait until there are no readers, then bump up the next
  167. * field and capture the ticket value.
  168. */
  169. for (;;) {
  170. if (!(val & 1)) {
  171. if ((val >> RD_COUNT_SHIFT) == 0)
  172. break;
  173. rwlock->lock = val;
  174. }
  175. delay_backoff(iterations++);
  176. val = __insn_tns((int *)&rwlock->lock);
  177. }
  178. /* Take out the next ticket and extract my ticket value. */
  179. rwlock->lock = __insn_addb(val, 1 << WR_NEXT_SHIFT);
  180. my_ticket_ = val >> WR_NEXT_SHIFT;
  181. /* Wait until the "current" field matches our ticket. */
  182. for (;;) {
  183. u32 curr_ = val >> WR_CURR_SHIFT;
  184. u32 delta = ((my_ticket_ - curr_) & WR_MASK);
  185. if (likely(delta == 0))
  186. break;
  187. /* Delay based on how many lock-holders are still out there. */
  188. relax((256 / CYCLES_PER_RELAX_LOOP) * delta);
  189. /*
  190. * Get a non-tns value to check; we don't need to tns
  191. * it ourselves. Since we're not tns'ing, we retry
  192. * more rapidly to get a valid value.
  193. */
  194. while ((val = rwlock->lock) & 1)
  195. relax(4);
  196. }
  197. }
  198. EXPORT_SYMBOL(arch_write_lock);
  199. int arch_write_trylock(arch_rwlock_t *rwlock)
  200. {
  201. u32 val = __insn_tns((int *)&rwlock->lock);
  202. /*
  203. * If a tns is in progress, or there's a waiting or active locker,
  204. * or active readers, we can't take the lock, so give up.
  205. */
  206. if (unlikely(val != 0)) {
  207. if (!(val & 1))
  208. rwlock->lock = val;
  209. return 0;
  210. }
  211. /* Set the "next" field to mark it locked. */
  212. rwlock->lock = 1 << _WR_NEXT_SHIFT;
  213. return 1;
  214. }
  215. EXPORT_SYMBOL(arch_write_trylock);
  216. void arch_write_unlock(arch_rwlock_t *rwlock)
  217. {
  218. u32 val, eq, mask;
  219. mb(); /* guarantee anything modified under the lock is visible */
  220. val = __insn_tns((int *)&rwlock->lock);
  221. if (likely(val == (1 << _WR_NEXT_SHIFT))) {
  222. rwlock->lock = 0;
  223. return;
  224. }
  225. while (unlikely(val & 1)) {
  226. /* Limited backoff since we are the highest-priority task. */
  227. relax(4);
  228. val = __insn_tns((int *)&rwlock->lock);
  229. }
  230. mask = 1 << WR_CURR_SHIFT;
  231. val = __insn_addb(val, mask);
  232. eq = __insn_seqb(val, val << (WR_CURR_SHIFT - WR_NEXT_SHIFT));
  233. val = __insn_mz(eq & mask, val);
  234. rwlock->lock = val;
  235. }
  236. EXPORT_SYMBOL(arch_write_unlock);