spinlock_32.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "spinlock_common.h"
  18. void arch_spin_lock(arch_spinlock_t *lock)
  19. {
  20. int my_ticket;
  21. int iterations = 0;
  22. int delta;
  23. while ((my_ticket = __insn_tns((void *)&lock->next_ticket)) & 1)
  24. delay_backoff(iterations++);
  25. /* Increment the next ticket number, implicitly releasing tns lock. */
  26. lock->next_ticket = my_ticket + TICKET_QUANTUM;
  27. /* Wait until it's our turn. */
  28. while ((delta = my_ticket - lock->current_ticket) != 0)
  29. relax((128 / CYCLES_PER_RELAX_LOOP) * delta);
  30. }
  31. EXPORT_SYMBOL(arch_spin_lock);
  32. int arch_spin_trylock(arch_spinlock_t *lock)
  33. {
  34. /*
  35. * Grab a ticket; no need to retry if it's busy, we'll just
  36. * treat that the same as "locked", since someone else
  37. * will lock it momentarily anyway.
  38. */
  39. int my_ticket = __insn_tns((void *)&lock->next_ticket);
  40. if (my_ticket == lock->current_ticket) {
  41. /* Not currently locked, so lock it by keeping this ticket. */
  42. lock->next_ticket = my_ticket + TICKET_QUANTUM;
  43. /* Success! */
  44. return 1;
  45. }
  46. if (!(my_ticket & 1)) {
  47. /* Release next_ticket. */
  48. lock->next_ticket = my_ticket;
  49. }
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(arch_spin_trylock);
  53. void arch_spin_unlock_wait(arch_spinlock_t *lock)
  54. {
  55. u32 iterations = 0;
  56. while (arch_spin_is_locked(lock))
  57. delay_backoff(iterations++);
  58. }
  59. EXPORT_SYMBOL(arch_spin_unlock_wait);
  60. /*
  61. * The low byte is always reserved to be the marker for a "tns" operation
  62. * since the low bit is set to "1" by a tns. The next seven bits are
  63. * zeroes. The next byte holds the "next" writer value, i.e. the ticket
  64. * available for the next task that wants to write. The third byte holds
  65. * the current writer value, i.e. the writer who holds the current ticket.
  66. * If current == next == 0, there are no interested writers.
  67. */
  68. #define WR_NEXT_SHIFT _WR_NEXT_SHIFT
  69. #define WR_CURR_SHIFT _WR_CURR_SHIFT
  70. #define WR_WIDTH _WR_WIDTH
  71. #define WR_MASK ((1 << WR_WIDTH) - 1)
  72. /*
  73. * The last eight bits hold the active reader count. This has to be
  74. * zero before a writer can start to write.
  75. */
  76. #define RD_COUNT_SHIFT _RD_COUNT_SHIFT
  77. #define RD_COUNT_WIDTH _RD_COUNT_WIDTH
  78. #define RD_COUNT_MASK ((1 << RD_COUNT_WIDTH) - 1)
  79. /* Lock the word, spinning until there are no tns-ers. */
  80. static inline u32 get_rwlock(arch_rwlock_t *rwlock)
  81. {
  82. u32 iterations = 0;
  83. for (;;) {
  84. u32 val = __insn_tns((int *)&rwlock->lock);
  85. if (unlikely(val & 1)) {
  86. delay_backoff(iterations++);
  87. continue;
  88. }
  89. return val;
  90. }
  91. }
  92. int arch_read_trylock_slow(arch_rwlock_t *rwlock)
  93. {
  94. u32 val = get_rwlock(rwlock);
  95. int locked = (val << RD_COUNT_WIDTH) == 0;
  96. rwlock->lock = val + (locked << RD_COUNT_SHIFT);
  97. return locked;
  98. }
  99. EXPORT_SYMBOL(arch_read_trylock_slow);
  100. void arch_read_unlock_slow(arch_rwlock_t *rwlock)
  101. {
  102. u32 val = get_rwlock(rwlock);
  103. rwlock->lock = val - (1 << RD_COUNT_SHIFT);
  104. }
  105. EXPORT_SYMBOL(arch_read_unlock_slow);
  106. void arch_write_unlock_slow(arch_rwlock_t *rwlock, u32 val)
  107. {
  108. u32 eq, mask = 1 << WR_CURR_SHIFT;
  109. while (unlikely(val & 1)) {
  110. /* Limited backoff since we are the highest-priority task. */
  111. relax(4);
  112. val = __insn_tns((int *)&rwlock->lock);
  113. }
  114. val = __insn_addb(val, mask);
  115. eq = __insn_seqb(val, val << (WR_CURR_SHIFT - WR_NEXT_SHIFT));
  116. val = __insn_mz(eq & mask, val);
  117. rwlock->lock = val;
  118. }
  119. EXPORT_SYMBOL(arch_write_unlock_slow);
  120. /*
  121. * We spin until everything but the reader bits (which are in the high
  122. * part of the word) are zero, i.e. no active or waiting writers, no tns.
  123. *
  124. * ISSUE: This approach can permanently starve readers. A reader who sees
  125. * a writer could instead take a ticket lock (just like a writer would),
  126. * and atomically enter read mode (with 1 reader) when it gets the ticket.
  127. * This way both readers and writers will always make forward progress
  128. * in a finite time.
  129. */
  130. void arch_read_lock_slow(arch_rwlock_t *rwlock, u32 val)
  131. {
  132. u32 iterations = 0;
  133. do {
  134. if (!(val & 1))
  135. rwlock->lock = val;
  136. delay_backoff(iterations++);
  137. val = __insn_tns((int *)&rwlock->lock);
  138. } while ((val << RD_COUNT_WIDTH) != 0);
  139. rwlock->lock = val + (1 << RD_COUNT_SHIFT);
  140. }
  141. EXPORT_SYMBOL(arch_read_lock_slow);
  142. void arch_write_lock_slow(arch_rwlock_t *rwlock, u32 val)
  143. {
  144. /*
  145. * The trailing underscore on this variable (and curr_ below)
  146. * reminds us that the high bits are garbage; we mask them out
  147. * when we compare them.
  148. */
  149. u32 my_ticket_;
  150. u32 iterations = 0;
  151. /*
  152. * Wait until there are no readers, then bump up the next
  153. * field and capture the ticket value.
  154. */
  155. for (;;) {
  156. if (!(val & 1)) {
  157. if ((val >> RD_COUNT_SHIFT) == 0)
  158. break;
  159. rwlock->lock = val;
  160. }
  161. delay_backoff(iterations++);
  162. val = __insn_tns((int *)&rwlock->lock);
  163. }
  164. /* Take out the next ticket and extract my ticket value. */
  165. rwlock->lock = __insn_addb(val, 1 << WR_NEXT_SHIFT);
  166. my_ticket_ = val >> WR_NEXT_SHIFT;
  167. /* Wait until the "current" field matches our ticket. */
  168. for (;;) {
  169. u32 curr_ = val >> WR_CURR_SHIFT;
  170. u32 delta = ((my_ticket_ - curr_) & WR_MASK);
  171. if (likely(delta == 0))
  172. break;
  173. /* Delay based on how many lock-holders are still out there. */
  174. relax((256 / CYCLES_PER_RELAX_LOOP) * delta);
  175. /*
  176. * Get a non-tns value to check; we don't need to tns
  177. * it ourselves. Since we're not tns'ing, we retry
  178. * more rapidly to get a valid value.
  179. */
  180. while ((val = rwlock->lock) & 1)
  181. relax(4);
  182. }
  183. }
  184. EXPORT_SYMBOL(arch_write_lock_slow);
  185. int __tns_atomic_acquire(atomic_t *lock)
  186. {
  187. int ret;
  188. u32 iterations = 0;
  189. BUG_ON(__insn_mfspr(SPR_INTERRUPT_CRITICAL_SECTION));
  190. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 1);
  191. while ((ret = __insn_tns((void *)&lock->counter)) == 1)
  192. delay_backoff(iterations++);
  193. return ret;
  194. }
  195. void __tns_atomic_release(atomic_t *p, int v)
  196. {
  197. p->counter = v;
  198. __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);
  199. }