spinlock.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <asm/atomic.h>
  13. #include <asm/bitops.h>
  14. #include <asm/intrinsics.h>
  15. #include <asm/system.h>
  16. #define __raw_spin_lock_init(x) ((x)->lock = 0)
  17. #ifdef ASM_SUPPORTED
  18. /*
  19. * Try to get the lock. If we fail to get the lock, make a non-standard call to
  20. * ia64_spinlock_contention(). We do not use a normal call because that would force all
  21. * callers of __raw_spin_lock() to be non-leaf routines. Instead, ia64_spinlock_contention() is
  22. * carefully coded to touch only those registers that __raw_spin_lock() marks "clobbered".
  23. */
  24. #define IA64_SPINLOCK_CLOBBERS "ar.ccv", "ar.pfs", "p14", "p15", "r27", "r28", "r29", "r30", "b6", "memory"
  25. static inline void
  26. __raw_spin_lock_flags (raw_spinlock_t *lock, unsigned long flags)
  27. {
  28. register volatile unsigned int *ptr asm ("r31") = &lock->lock;
  29. #if (__GNUC__ == 3 && __GNUC_MINOR__ < 3)
  30. # ifdef CONFIG_ITANIUM
  31. /* don't use brl on Itanium... */
  32. asm volatile ("{\n\t"
  33. " mov ar.ccv = r0\n\t"
  34. " mov r28 = ip\n\t"
  35. " mov r30 = 1;;\n\t"
  36. "}\n\t"
  37. "cmpxchg4.acq r30 = [%1], r30, ar.ccv\n\t"
  38. "movl r29 = ia64_spinlock_contention_pre3_4;;\n\t"
  39. "cmp4.ne p14, p0 = r30, r0\n\t"
  40. "mov b6 = r29;;\n\t"
  41. "mov r27=%2\n\t"
  42. "(p14) br.cond.spnt.many b6"
  43. : "=r"(ptr) : "r"(ptr), "r" (flags) : IA64_SPINLOCK_CLOBBERS);
  44. # else
  45. asm volatile ("{\n\t"
  46. " mov ar.ccv = r0\n\t"
  47. " mov r28 = ip\n\t"
  48. " mov r30 = 1;;\n\t"
  49. "}\n\t"
  50. "cmpxchg4.acq r30 = [%1], r30, ar.ccv;;\n\t"
  51. "cmp4.ne p14, p0 = r30, r0\n\t"
  52. "mov r27=%2\n\t"
  53. "(p14) brl.cond.spnt.many ia64_spinlock_contention_pre3_4;;"
  54. : "=r"(ptr) : "r"(ptr), "r" (flags) : IA64_SPINLOCK_CLOBBERS);
  55. # endif /* CONFIG_MCKINLEY */
  56. #else
  57. # ifdef CONFIG_ITANIUM
  58. /* don't use brl on Itanium... */
  59. /* mis-declare, so we get the entry-point, not it's function descriptor: */
  60. asm volatile ("mov r30 = 1\n\t"
  61. "mov r27=%2\n\t"
  62. "mov ar.ccv = r0;;\n\t"
  63. "cmpxchg4.acq r30 = [%0], r30, ar.ccv\n\t"
  64. "movl r29 = ia64_spinlock_contention;;\n\t"
  65. "cmp4.ne p14, p0 = r30, r0\n\t"
  66. "mov b6 = r29;;\n\t"
  67. "(p14) br.call.spnt.many b6 = b6"
  68. : "=r"(ptr) : "r"(ptr), "r" (flags) : IA64_SPINLOCK_CLOBBERS);
  69. # else
  70. asm volatile ("mov r30 = 1\n\t"
  71. "mov r27=%2\n\t"
  72. "mov ar.ccv = r0;;\n\t"
  73. "cmpxchg4.acq r30 = [%0], r30, ar.ccv;;\n\t"
  74. "cmp4.ne p14, p0 = r30, r0\n\t"
  75. "(p14) brl.call.spnt.many b6=ia64_spinlock_contention;;"
  76. : "=r"(ptr) : "r"(ptr), "r" (flags) : IA64_SPINLOCK_CLOBBERS);
  77. # endif /* CONFIG_MCKINLEY */
  78. #endif
  79. }
  80. #define __raw_spin_lock(lock) __raw_spin_lock_flags(lock, 0)
  81. /* Unlock by doing an ordered store and releasing the cacheline with nta */
  82. static inline void __raw_spin_unlock(raw_spinlock_t *x) {
  83. barrier();
  84. asm volatile ("st4.rel.nta [%0] = r0\n\t" :: "r"(x));
  85. }
  86. #else /* !ASM_SUPPORTED */
  87. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  88. # define __raw_spin_lock(x) \
  89. do { \
  90. __u32 *ia64_spinlock_ptr = (__u32 *) (x); \
  91. __u64 ia64_spinlock_val; \
  92. ia64_spinlock_val = ia64_cmpxchg4_acq(ia64_spinlock_ptr, 1, 0); \
  93. if (unlikely(ia64_spinlock_val)) { \
  94. do { \
  95. while (*ia64_spinlock_ptr) \
  96. ia64_barrier(); \
  97. ia64_spinlock_val = ia64_cmpxchg4_acq(ia64_spinlock_ptr, 1, 0); \
  98. } while (ia64_spinlock_val); \
  99. } \
  100. } while (0)
  101. #define __raw_spin_unlock(x) do { barrier(); ((raw_spinlock_t *) x)->lock = 0; } while (0)
  102. #endif /* !ASM_SUPPORTED */
  103. #define __raw_spin_is_locked(x) ((x)->lock != 0)
  104. #define __raw_spin_trylock(x) (cmpxchg_acq(&(x)->lock, 0, 1) == 0)
  105. #define __raw_spin_unlock_wait(lock) \
  106. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  107. #define __raw_read_can_lock(rw) (*(volatile int *)(rw) >= 0)
  108. #define __raw_write_can_lock(rw) (*(volatile int *)(rw) == 0)
  109. #define __raw_read_lock(rw) \
  110. do { \
  111. raw_rwlock_t *__read_lock_ptr = (rw); \
  112. \
  113. while (unlikely(ia64_fetchadd(1, (int *) __read_lock_ptr, acq) < 0)) { \
  114. ia64_fetchadd(-1, (int *) __read_lock_ptr, rel); \
  115. while (*(volatile int *)__read_lock_ptr < 0) \
  116. cpu_relax(); \
  117. } \
  118. } while (0)
  119. #define __raw_read_unlock(rw) \
  120. do { \
  121. raw_rwlock_t *__read_lock_ptr = (rw); \
  122. ia64_fetchadd(-1, (int *) __read_lock_ptr, rel); \
  123. } while (0)
  124. #ifdef ASM_SUPPORTED
  125. #define __raw_write_lock(rw) \
  126. do { \
  127. __asm__ __volatile__ ( \
  128. "mov ar.ccv = r0\n" \
  129. "dep r29 = -1, r0, 31, 1;;\n" \
  130. "1:\n" \
  131. "ld4 r2 = [%0];;\n" \
  132. "cmp4.eq p0,p7 = r0,r2\n" \
  133. "(p7) br.cond.spnt.few 1b \n" \
  134. "cmpxchg4.acq r2 = [%0], r29, ar.ccv;;\n" \
  135. "cmp4.eq p0,p7 = r0, r2\n" \
  136. "(p7) br.cond.spnt.few 1b;;\n" \
  137. :: "r"(rw) : "ar.ccv", "p7", "r2", "r29", "memory"); \
  138. } while(0)
  139. #define __raw_write_trylock(rw) \
  140. ({ \
  141. register long result; \
  142. \
  143. __asm__ __volatile__ ( \
  144. "mov ar.ccv = r0\n" \
  145. "dep r29 = -1, r0, 31, 1;;\n" \
  146. "cmpxchg4.acq %0 = [%1], r29, ar.ccv\n" \
  147. : "=r"(result) : "r"(rw) : "ar.ccv", "r29", "memory"); \
  148. (result == 0); \
  149. })
  150. static inline void __raw_write_unlock(raw_rwlock_t *x)
  151. {
  152. u8 *y = (u8 *)x;
  153. barrier();
  154. asm volatile ("st1.rel.nta [%0] = r0\n\t" :: "r"(y+3) : "memory" );
  155. }
  156. #else /* !ASM_SUPPORTED */
  157. #define __raw_write_lock(l) \
  158. ({ \
  159. __u64 ia64_val, ia64_set_val = ia64_dep_mi(-1, 0, 31, 1); \
  160. __u32 *ia64_write_lock_ptr = (__u32 *) (l); \
  161. do { \
  162. while (*ia64_write_lock_ptr) \
  163. ia64_barrier(); \
  164. ia64_val = ia64_cmpxchg4_acq(ia64_write_lock_ptr, ia64_set_val, 0); \
  165. } while (ia64_val); \
  166. })
  167. #define __raw_write_trylock(rw) \
  168. ({ \
  169. __u64 ia64_val; \
  170. __u64 ia64_set_val = ia64_dep_mi(-1, 0, 31,1); \
  171. ia64_val = ia64_cmpxchg4_acq((__u32 *)(rw), ia64_set_val, 0); \
  172. (ia64_val == 0); \
  173. })
  174. static inline void __raw_write_unlock(raw_rwlock_t *x)
  175. {
  176. barrier();
  177. x->write_lock = 0;
  178. }
  179. #endif /* !ASM_SUPPORTED */
  180. static inline int __raw_read_trylock(raw_rwlock_t *x)
  181. {
  182. union {
  183. raw_rwlock_t lock;
  184. __u32 word;
  185. } old, new;
  186. old.lock = new.lock = *x;
  187. old.lock.write_lock = new.lock.write_lock = 0;
  188. ++new.lock.read_counter;
  189. return (u32)ia64_cmpxchg4_acq((__u32 *)(x), new.word, old.word) == old.word;
  190. }
  191. #endif /* _ASM_IA64_SPINLOCK_H */