spinlock_32.c 7.6 KB

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