spinlock.h 6.9 KB

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