spinlock.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #ifndef _ASM_IA64_SPINLOCK_H
  2. #define _ASM_IA64_SPINLOCK_H
  3. /*
  4. * Copyright (C) 1998-2003 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  7. *
  8. * This file is used for SMP configurations only.
  9. */
  10. #include <linux/compiler.h>
  11. #include <linux/kernel.h>
  12. #include <linux/bitops.h>
  13. #include <linux/atomic.h>
  14. #include <asm/intrinsics.h>
  15. #define arch_spin_lock_init(x) ((x)->lock = 0)
  16. /*
  17. * Ticket locks are conceptually two parts, one indicating the current head of
  18. * the queue, and the other indicating the current tail. The lock is acquired
  19. * by atomically noting the tail and incrementing it by one (thus adding
  20. * ourself to the queue and noting our position), then waiting until the head
  21. * becomes equal to the the initial value of the tail.
  22. * The pad bits in the middle are used to prevent the next_ticket number
  23. * overflowing into the now_serving number.
  24. *
  25. * 31 17 16 15 14 0
  26. * +----------------------------------------------------+
  27. * | now_serving | padding | next_ticket |
  28. * +----------------------------------------------------+
  29. */
  30. #define TICKET_SHIFT 17
  31. #define TICKET_BITS 15
  32. #define TICKET_MASK ((1 << TICKET_BITS) - 1)
  33. static __always_inline void __ticket_spin_lock(arch_spinlock_t *lock)
  34. {
  35. int *p = (int *)&lock->lock, ticket, serve;
  36. ticket = ia64_fetchadd(1, p, acq);
  37. if (!(((ticket >> TICKET_SHIFT) ^ ticket) & TICKET_MASK))
  38. return;
  39. ia64_invala();
  40. for (;;) {
  41. asm volatile ("ld4.c.nc %0=[%1]" : "=r"(serve) : "r"(p) : "memory");
  42. if (!(((serve >> TICKET_SHIFT) ^ ticket) & TICKET_MASK))
  43. return;
  44. cpu_relax();
  45. }
  46. }
  47. static __always_inline int __ticket_spin_trylock(arch_spinlock_t *lock)
  48. {
  49. int tmp = ACCESS_ONCE(lock->lock);
  50. if (!(((tmp >> TICKET_SHIFT) ^ tmp) & TICKET_MASK))
  51. return ia64_cmpxchg(acq, &lock->lock, tmp, tmp + 1, sizeof (tmp)) == tmp;
  52. return 0;
  53. }
  54. static __always_inline void __ticket_spin_unlock(arch_spinlock_t *lock)
  55. {
  56. unsigned short *p = (unsigned short *)&lock->lock + 1, tmp;
  57. asm volatile ("ld2.bias %0=[%1]" : "=r"(tmp) : "r"(p));
  58. ACCESS_ONCE(*p) = (tmp + 2) & ~1;
  59. }
  60. static __always_inline void __ticket_spin_unlock_wait(arch_spinlock_t *lock)
  61. {
  62. int *p = (int *)&lock->lock, ticket;
  63. ia64_invala();
  64. for (;;) {
  65. asm volatile ("ld4.c.nc %0=[%1]" : "=r"(ticket) : "r"(p) : "memory");
  66. if (!(((ticket >> TICKET_SHIFT) ^ ticket) & TICKET_MASK))
  67. return;
  68. cpu_relax();
  69. }
  70. }
  71. static inline int __ticket_spin_is_locked(arch_spinlock_t *lock)
  72. {
  73. long tmp = ACCESS_ONCE(lock->lock);
  74. return !!(((tmp >> TICKET_SHIFT) ^ tmp) & TICKET_MASK);
  75. }
  76. static inline int __ticket_spin_is_contended(arch_spinlock_t *lock)
  77. {
  78. long tmp = ACCESS_ONCE(lock->lock);
  79. return ((tmp - (tmp >> TICKET_SHIFT)) & TICKET_MASK) > 1;
  80. }
  81. static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  82. {
  83. return !(((lock.lock >> TICKET_SHIFT) ^ lock.lock) & TICKET_MASK);
  84. }
  85. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  86. {
  87. return __ticket_spin_is_locked(lock);
  88. }
  89. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  90. {
  91. return __ticket_spin_is_contended(lock);
  92. }
  93. #define arch_spin_is_contended arch_spin_is_contended
  94. static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
  95. {
  96. __ticket_spin_lock(lock);
  97. }
  98. static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
  99. {
  100. return __ticket_spin_trylock(lock);
  101. }
  102. static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
  103. {
  104. __ticket_spin_unlock(lock);
  105. }
  106. static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
  107. unsigned long flags)
  108. {
  109. arch_spin_lock(lock);
  110. }
  111. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  112. {
  113. __ticket_spin_unlock_wait(lock);
  114. }
  115. #define arch_read_can_lock(rw) (*(volatile int *)(rw) >= 0)
  116. #define arch_write_can_lock(rw) (*(volatile int *)(rw) == 0)
  117. #ifdef ASM_SUPPORTED
  118. static __always_inline void
  119. arch_read_lock_flags(arch_rwlock_t *lock, unsigned long flags)
  120. {
  121. __asm__ __volatile__ (
  122. "tbit.nz p6, p0 = %1,%2\n"
  123. "br.few 3f\n"
  124. "1:\n"
  125. "fetchadd4.rel r2 = [%0], -1;;\n"
  126. "(p6) ssm psr.i\n"
  127. "2:\n"
  128. "hint @pause\n"
  129. "ld4 r2 = [%0];;\n"
  130. "cmp4.lt p7,p0 = r2, r0\n"
  131. "(p7) br.cond.spnt.few 2b\n"
  132. "(p6) rsm psr.i\n"
  133. ";;\n"
  134. "3:\n"
  135. "fetchadd4.acq r2 = [%0], 1;;\n"
  136. "cmp4.lt p7,p0 = r2, r0\n"
  137. "(p7) br.cond.spnt.few 1b\n"
  138. : : "r"(lock), "r"(flags), "i"(IA64_PSR_I_BIT)
  139. : "p6", "p7", "r2", "memory");
  140. }
  141. #define arch_read_lock(lock) arch_read_lock_flags(lock, 0)
  142. #else /* !ASM_SUPPORTED */
  143. #define arch_read_lock_flags(rw, flags) arch_read_lock(rw)
  144. #define arch_read_lock(rw) \
  145. do { \
  146. arch_rwlock_t *__read_lock_ptr = (rw); \
  147. \
  148. while (unlikely(ia64_fetchadd(1, (int *) __read_lock_ptr, acq) < 0)) { \
  149. ia64_fetchadd(-1, (int *) __read_lock_ptr, rel); \
  150. while (*(volatile int *)__read_lock_ptr < 0) \
  151. cpu_relax(); \
  152. } \
  153. } while (0)
  154. #endif /* !ASM_SUPPORTED */
  155. #define arch_read_unlock(rw) \
  156. do { \
  157. arch_rwlock_t *__read_lock_ptr = (rw); \
  158. ia64_fetchadd(-1, (int *) __read_lock_ptr, rel); \
  159. } while (0)
  160. #ifdef ASM_SUPPORTED
  161. static __always_inline void
  162. arch_write_lock_flags(arch_rwlock_t *lock, unsigned long flags)
  163. {
  164. __asm__ __volatile__ (
  165. "tbit.nz p6, p0 = %1, %2\n"
  166. "mov ar.ccv = r0\n"
  167. "dep r29 = -1, r0, 31, 1\n"
  168. "br.few 3f;;\n"
  169. "1:\n"
  170. "(p6) ssm psr.i\n"
  171. "2:\n"
  172. "hint @pause\n"
  173. "ld4 r2 = [%0];;\n"
  174. "cmp4.eq p0,p7 = r0, r2\n"
  175. "(p7) br.cond.spnt.few 2b\n"
  176. "(p6) rsm psr.i\n"
  177. ";;\n"
  178. "3:\n"
  179. "cmpxchg4.acq r2 = [%0], r29, ar.ccv;;\n"
  180. "cmp4.eq p0,p7 = r0, r2\n"
  181. "(p7) br.cond.spnt.few 1b;;\n"
  182. : : "r"(lock), "r"(flags), "i"(IA64_PSR_I_BIT)
  183. : "ar.ccv", "p6", "p7", "r2", "r29", "memory");
  184. }
  185. #define arch_write_lock(rw) arch_write_lock_flags(rw, 0)
  186. #define arch_write_trylock(rw) \
  187. ({ \
  188. register long result; \
  189. \
  190. __asm__ __volatile__ ( \
  191. "mov ar.ccv = r0\n" \
  192. "dep r29 = -1, r0, 31, 1;;\n" \
  193. "cmpxchg4.acq %0 = [%1], r29, ar.ccv\n" \
  194. : "=r"(result) : "r"(rw) : "ar.ccv", "r29", "memory"); \
  195. (result == 0); \
  196. })
  197. static inline void arch_write_unlock(arch_rwlock_t *x)
  198. {
  199. u8 *y = (u8 *)x;
  200. barrier();
  201. asm volatile ("st1.rel.nta [%0] = r0\n\t" :: "r"(y+3) : "memory" );
  202. }
  203. #else /* !ASM_SUPPORTED */
  204. #define arch_write_lock_flags(l, flags) arch_write_lock(l)
  205. #define arch_write_lock(l) \
  206. ({ \
  207. __u64 ia64_val, ia64_set_val = ia64_dep_mi(-1, 0, 31, 1); \
  208. __u32 *ia64_write_lock_ptr = (__u32 *) (l); \
  209. do { \
  210. while (*ia64_write_lock_ptr) \
  211. ia64_barrier(); \
  212. ia64_val = ia64_cmpxchg4_acq(ia64_write_lock_ptr, ia64_set_val, 0); \
  213. } while (ia64_val); \
  214. })
  215. #define arch_write_trylock(rw) \
  216. ({ \
  217. __u64 ia64_val; \
  218. __u64 ia64_set_val = ia64_dep_mi(-1, 0, 31,1); \
  219. ia64_val = ia64_cmpxchg4_acq((__u32 *)(rw), ia64_set_val, 0); \
  220. (ia64_val == 0); \
  221. })
  222. static inline void arch_write_unlock(arch_rwlock_t *x)
  223. {
  224. barrier();
  225. x->write_lock = 0;
  226. }
  227. #endif /* !ASM_SUPPORTED */
  228. static inline int arch_read_trylock(arch_rwlock_t *x)
  229. {
  230. union {
  231. arch_rwlock_t lock;
  232. __u32 word;
  233. } old, new;
  234. old.lock = new.lock = *x;
  235. old.lock.write_lock = new.lock.write_lock = 0;
  236. ++new.lock.read_counter;
  237. return (u32)ia64_cmpxchg4_acq((__u32 *)(x), new.word, old.word) == old.word;
  238. }
  239. #define arch_spin_relax(lock) cpu_relax()
  240. #define arch_read_relax(lock) cpu_relax()
  241. #define arch_write_relax(lock) cpu_relax()
  242. #endif /* _ASM_IA64_SPINLOCK_H */