spinlock.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <asm/atomic.h>
  14. #include <asm/intrinsics.h>
  15. #include <asm/system.h>
  16. #define __raw_spin_lock_init(x) ((x)->lock = 0)
  17. /*
  18. * Ticket locks are conceptually two parts, one indicating the current head of
  19. * the queue, and the other indicating the current tail. The lock is acquired
  20. * by atomically noting the tail and incrementing it by one (thus adding
  21. * ourself to the queue and noting our position), then waiting until the head
  22. * becomes equal to the the initial value of the tail.
  23. *
  24. * 63 32 31 0
  25. * +----------------------------------------------------+
  26. * | next_ticket_number | now_serving |
  27. * +----------------------------------------------------+
  28. */
  29. #define TICKET_SHIFT 32
  30. static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock)
  31. {
  32. int *p = (int *)&lock->lock, turn, now_serving;
  33. now_serving = *p;
  34. turn = ia64_fetchadd(1, p+1, acq);
  35. if (turn == now_serving)
  36. return;
  37. do {
  38. cpu_relax();
  39. } while (ACCESS_ONCE(*p) != turn);
  40. }
  41. static __always_inline int __ticket_spin_trylock(raw_spinlock_t *lock)
  42. {
  43. long tmp = ACCESS_ONCE(lock->lock), try;
  44. if (!(((tmp >> TICKET_SHIFT) ^ tmp) & ((1L << TICKET_SHIFT) - 1))) {
  45. try = tmp + (1L << TICKET_SHIFT);
  46. return ia64_cmpxchg(acq, &lock->lock, tmp, try, sizeof (tmp)) == tmp;
  47. }
  48. return 0;
  49. }
  50. static __always_inline void __ticket_spin_unlock(raw_spinlock_t *lock)
  51. {
  52. int *p = (int *)&lock->lock;
  53. (void)ia64_fetchadd(1, p, rel);
  54. }
  55. static inline int __ticket_spin_is_locked(raw_spinlock_t *lock)
  56. {
  57. long tmp = ACCESS_ONCE(lock->lock);
  58. return !!(((tmp >> TICKET_SHIFT) ^ tmp) & ((1L << TICKET_SHIFT) - 1));
  59. }
  60. static inline int __ticket_spin_is_contended(raw_spinlock_t *lock)
  61. {
  62. long tmp = ACCESS_ONCE(lock->lock);
  63. return (((tmp >> TICKET_SHIFT) - tmp) & ((1L << TICKET_SHIFT) - 1)) > 1;
  64. }
  65. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  66. {
  67. return __ticket_spin_is_locked(lock);
  68. }
  69. static inline int __raw_spin_is_contended(raw_spinlock_t *lock)
  70. {
  71. return __ticket_spin_is_contended(lock);
  72. }
  73. #define __raw_spin_is_contended __raw_spin_is_contended
  74. static __always_inline void __raw_spin_lock(raw_spinlock_t *lock)
  75. {
  76. __ticket_spin_lock(lock);
  77. }
  78. static __always_inline int __raw_spin_trylock(raw_spinlock_t *lock)
  79. {
  80. return __ticket_spin_trylock(lock);
  81. }
  82. static __always_inline void __raw_spin_unlock(raw_spinlock_t *lock)
  83. {
  84. __ticket_spin_unlock(lock);
  85. }
  86. static __always_inline void __raw_spin_lock_flags(raw_spinlock_t *lock,
  87. unsigned long flags)
  88. {
  89. __raw_spin_lock(lock);
  90. }
  91. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  92. {
  93. while (__raw_spin_is_locked(lock))
  94. cpu_relax();
  95. }
  96. #define __raw_read_can_lock(rw) (*(volatile int *)(rw) >= 0)
  97. #define __raw_write_can_lock(rw) (*(volatile int *)(rw) == 0)
  98. #ifdef ASM_SUPPORTED
  99. static __always_inline void
  100. __raw_read_lock_flags(raw_rwlock_t *lock, unsigned long flags)
  101. {
  102. __asm__ __volatile__ (
  103. "tbit.nz p6, p0 = %1,%2\n"
  104. "br.few 3f\n"
  105. "1:\n"
  106. "fetchadd4.rel r2 = [%0], -1;;\n"
  107. "(p6) ssm psr.i\n"
  108. "2:\n"
  109. "hint @pause\n"
  110. "ld4 r2 = [%0];;\n"
  111. "cmp4.lt p7,p0 = r2, r0\n"
  112. "(p7) br.cond.spnt.few 2b\n"
  113. "(p6) rsm psr.i\n"
  114. ";;\n"
  115. "3:\n"
  116. "fetchadd4.acq r2 = [%0], 1;;\n"
  117. "cmp4.lt p7,p0 = r2, r0\n"
  118. "(p7) br.cond.spnt.few 1b\n"
  119. : : "r"(lock), "r"(flags), "i"(IA64_PSR_I_BIT)
  120. : "p6", "p7", "r2", "memory");
  121. }
  122. #define __raw_read_lock(lock) __raw_read_lock_flags(lock, 0)
  123. #else /* !ASM_SUPPORTED */
  124. #define __raw_read_lock_flags(rw, flags) __raw_read_lock(rw)
  125. #define __raw_read_lock(rw) \
  126. do { \
  127. raw_rwlock_t *__read_lock_ptr = (rw); \
  128. \
  129. while (unlikely(ia64_fetchadd(1, (int *) __read_lock_ptr, acq) < 0)) { \
  130. ia64_fetchadd(-1, (int *) __read_lock_ptr, rel); \
  131. while (*(volatile int *)__read_lock_ptr < 0) \
  132. cpu_relax(); \
  133. } \
  134. } while (0)
  135. #endif /* !ASM_SUPPORTED */
  136. #define __raw_read_unlock(rw) \
  137. do { \
  138. raw_rwlock_t *__read_lock_ptr = (rw); \
  139. ia64_fetchadd(-1, (int *) __read_lock_ptr, rel); \
  140. } while (0)
  141. #ifdef ASM_SUPPORTED
  142. static __always_inline void
  143. __raw_write_lock_flags(raw_rwlock_t *lock, unsigned long flags)
  144. {
  145. __asm__ __volatile__ (
  146. "tbit.nz p6, p0 = %1, %2\n"
  147. "mov ar.ccv = r0\n"
  148. "dep r29 = -1, r0, 31, 1\n"
  149. "br.few 3f;;\n"
  150. "1:\n"
  151. "(p6) ssm psr.i\n"
  152. "2:\n"
  153. "hint @pause\n"
  154. "ld4 r2 = [%0];;\n"
  155. "cmp4.eq p0,p7 = r0, r2\n"
  156. "(p7) br.cond.spnt.few 2b\n"
  157. "(p6) rsm psr.i\n"
  158. ";;\n"
  159. "3:\n"
  160. "cmpxchg4.acq r2 = [%0], r29, ar.ccv;;\n"
  161. "cmp4.eq p0,p7 = r0, r2\n"
  162. "(p7) br.cond.spnt.few 1b;;\n"
  163. : : "r"(lock), "r"(flags), "i"(IA64_PSR_I_BIT)
  164. : "ar.ccv", "p6", "p7", "r2", "r29", "memory");
  165. }
  166. #define __raw_write_lock(rw) __raw_write_lock_flags(rw, 0)
  167. #define __raw_write_trylock(rw) \
  168. ({ \
  169. register long result; \
  170. \
  171. __asm__ __volatile__ ( \
  172. "mov ar.ccv = r0\n" \
  173. "dep r29 = -1, r0, 31, 1;;\n" \
  174. "cmpxchg4.acq %0 = [%1], r29, ar.ccv\n" \
  175. : "=r"(result) : "r"(rw) : "ar.ccv", "r29", "memory"); \
  176. (result == 0); \
  177. })
  178. static inline void __raw_write_unlock(raw_rwlock_t *x)
  179. {
  180. u8 *y = (u8 *)x;
  181. barrier();
  182. asm volatile ("st1.rel.nta [%0] = r0\n\t" :: "r"(y+3) : "memory" );
  183. }
  184. #else /* !ASM_SUPPORTED */
  185. #define __raw_write_lock_flags(l, flags) __raw_write_lock(l)
  186. #define __raw_write_lock(l) \
  187. ({ \
  188. __u64 ia64_val, ia64_set_val = ia64_dep_mi(-1, 0, 31, 1); \
  189. __u32 *ia64_write_lock_ptr = (__u32 *) (l); \
  190. do { \
  191. while (*ia64_write_lock_ptr) \
  192. ia64_barrier(); \
  193. ia64_val = ia64_cmpxchg4_acq(ia64_write_lock_ptr, ia64_set_val, 0); \
  194. } while (ia64_val); \
  195. })
  196. #define __raw_write_trylock(rw) \
  197. ({ \
  198. __u64 ia64_val; \
  199. __u64 ia64_set_val = ia64_dep_mi(-1, 0, 31,1); \
  200. ia64_val = ia64_cmpxchg4_acq((__u32 *)(rw), ia64_set_val, 0); \
  201. (ia64_val == 0); \
  202. })
  203. static inline void __raw_write_unlock(raw_rwlock_t *x)
  204. {
  205. barrier();
  206. x->write_lock = 0;
  207. }
  208. #endif /* !ASM_SUPPORTED */
  209. static inline int __raw_read_trylock(raw_rwlock_t *x)
  210. {
  211. union {
  212. raw_rwlock_t lock;
  213. __u32 word;
  214. } old, new;
  215. old.lock = new.lock = *x;
  216. old.lock.write_lock = new.lock.write_lock = 0;
  217. ++new.lock.read_counter;
  218. return (u32)ia64_cmpxchg4_acq((__u32 *)(x), new.word, old.word) == old.word;
  219. }
  220. #define _raw_spin_relax(lock) cpu_relax()
  221. #define _raw_read_relax(lock) cpu_relax()
  222. #define _raw_write_relax(lock) cpu_relax()
  223. #endif /* _ASM_IA64_SPINLOCK_H */