spinlock.h 7.0 KB

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