spinlock.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <[email protected]>
  7. * Copyright (C) 1999 Walt Drummond <[email protected]>
  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 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. /* This could be optimised with ARCH_HAS_MMIOWB */
  61. mmiowb();
  62. asm volatile ("ld2.bias %0=[%1]" : "=r"(tmp) : "r"(p));
  63. WRITE_ONCE(*p, (tmp + 2) & ~1);
  64. }
  65. static inline int __ticket_spin_is_locked(arch_spinlock_t *lock)
  66. {
  67. long tmp = READ_ONCE(lock->lock);
  68. return !!(((tmp >> TICKET_SHIFT) ^ tmp) & TICKET_MASK);
  69. }
  70. static inline int __ticket_spin_is_contended(arch_spinlock_t *lock)
  71. {
  72. long tmp = READ_ONCE(lock->lock);
  73. return ((tmp - (tmp >> TICKET_SHIFT)) & TICKET_MASK) > 1;
  74. }
  75. static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  76. {
  77. return !(((lock.lock >> TICKET_SHIFT) ^ lock.lock) & TICKET_MASK);
  78. }
  79. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  80. {
  81. return __ticket_spin_is_locked(lock);
  82. }
  83. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  84. {
  85. return __ticket_spin_is_contended(lock);
  86. }
  87. #define arch_spin_is_contended arch_spin_is_contended
  88. static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
  89. {
  90. __ticket_spin_lock(lock);
  91. }
  92. static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
  93. {
  94. return __ticket_spin_trylock(lock);
  95. }
  96. static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
  97. {
  98. __ticket_spin_unlock(lock);
  99. }
  100. #ifdef ASM_SUPPORTED
  101. static __always_inline void
  102. arch_read_lock(arch_rwlock_t *lock)
  103. {
  104. unsigned long flags = 0;
  105. __asm__ __volatile__ (
  106. "tbit.nz p6, p0 = %1,%2\n"
  107. "br.few 3f\n"
  108. "1:\n"
  109. "fetchadd4.rel r2 = [%0], -1;;\n"
  110. "(p6) ssm psr.i\n"
  111. "2:\n"
  112. "hint @pause\n"
  113. "ld4 r2 = [%0];;\n"
  114. "cmp4.lt p7,p0 = r2, r0\n"
  115. "(p7) br.cond.spnt.few 2b\n"
  116. "(p6) rsm psr.i\n"
  117. ";;\n"
  118. "3:\n"
  119. "fetchadd4.acq r2 = [%0], 1;;\n"
  120. "cmp4.lt p7,p0 = r2, r0\n"
  121. "(p7) br.cond.spnt.few 1b\n"
  122. : : "r"(lock), "r"(flags), "i"(IA64_PSR_I_BIT)
  123. : "p6", "p7", "r2", "memory");
  124. }
  125. #else /* !ASM_SUPPORTED */
  126. #define arch_read_lock(rw) \
  127. do { \
  128. arch_rwlock_t *__read_lock_ptr = (rw); \
  129. \
  130. while (unlikely(ia64_fetchadd(1, (int *) __read_lock_ptr, acq) < 0)) { \
  131. ia64_fetchadd(-1, (int *) __read_lock_ptr, rel); \
  132. while (*(volatile int *)__read_lock_ptr < 0) \
  133. cpu_relax(); \
  134. } \
  135. } while (0)
  136. #endif /* !ASM_SUPPORTED */
  137. #define arch_read_unlock(rw) \
  138. do { \
  139. arch_rwlock_t *__read_lock_ptr = (rw); \
  140. ia64_fetchadd(-1, (int *) __read_lock_ptr, rel); \
  141. } while (0)
  142. #ifdef ASM_SUPPORTED
  143. static __always_inline void
  144. arch_write_lock(arch_rwlock_t *lock)
  145. {
  146. unsigned long flags = 0;
  147. __asm__ __volatile__ (
  148. "tbit.nz p6, p0 = %1, %2\n"
  149. "mov ar.ccv = r0\n"
  150. "dep r29 = -1, r0, 31, 1\n"
  151. "br.few 3f;;\n"
  152. "1:\n"
  153. "(p6) ssm psr.i\n"
  154. "2:\n"
  155. "hint @pause\n"
  156. "ld4 r2 = [%0];;\n"
  157. "cmp4.eq p0,p7 = r0, r2\n"
  158. "(p7) br.cond.spnt.few 2b\n"
  159. "(p6) rsm psr.i\n"
  160. ";;\n"
  161. "3:\n"
  162. "cmpxchg4.acq r2 = [%0], r29, ar.ccv;;\n"
  163. "cmp4.eq p0,p7 = r0, r2\n"
  164. "(p7) br.cond.spnt.few 1b;;\n"
  165. : : "r"(lock), "r"(flags), "i"(IA64_PSR_I_BIT)
  166. : "ar.ccv", "p6", "p7", "r2", "r29", "memory");
  167. }
  168. #define arch_write_trylock(rw) \
  169. ({ \
  170. register long result; \
  171. \
  172. __asm__ __volatile__ ( \
  173. "mov ar.ccv = r0\n" \
  174. "dep r29 = -1, r0, 31, 1;;\n" \
  175. "cmpxchg4.acq %0 = [%1], r29, ar.ccv\n" \
  176. : "=r"(result) : "r"(rw) : "ar.ccv", "r29", "memory"); \
  177. (result == 0); \
  178. })
  179. static inline void arch_write_unlock(arch_rwlock_t *x)
  180. {
  181. u8 *y = (u8 *)x;
  182. barrier();
  183. asm volatile ("st1.rel.nta [%0] = r0\n\t" :: "r"(y+3) : "memory" );
  184. }
  185. #else /* !ASM_SUPPORTED */
  186. #define arch_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 arch_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 arch_write_unlock(arch_rwlock_t *x)
  204. {
  205. barrier();
  206. x->write_lock = 0;
  207. }
  208. #endif /* !ASM_SUPPORTED */
  209. static inline int arch_read_trylock(arch_rwlock_t *x)
  210. {
  211. union {
  212. arch_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. #endif /* _ASM_IA64_SPINLOCK_H */