spinlock.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* MN10300 spinlock support
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #ifndef _ASM_SPINLOCK_H
  12. #define _ASM_SPINLOCK_H
  13. #include <linux/atomic.h>
  14. #include <asm/barrier.h>
  15. #include <asm/processor.h>
  16. #include <asm/rwlock.h>
  17. #include <asm/page.h>
  18. /*
  19. * Simple spin lock operations. There are two variants, one clears IRQ's
  20. * on the local processor, one does not.
  21. *
  22. * We make no fairness assumptions. They have a cost.
  23. */
  24. #define arch_spin_is_locked(x) (*(volatile signed char *)(&(x)->slock) != 0)
  25. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  26. {
  27. smp_cond_load_acquire(&lock->slock, !VAL);
  28. }
  29. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  30. {
  31. asm volatile(
  32. " bclr 1,(0,%0) \n"
  33. :
  34. : "a"(&lock->slock)
  35. : "memory", "cc");
  36. }
  37. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  38. {
  39. int ret;
  40. asm volatile(
  41. " mov 1,%0 \n"
  42. " bset %0,(%1) \n"
  43. " bne 1f \n"
  44. " clr %0 \n"
  45. "1: xor 1,%0 \n"
  46. : "=d"(ret)
  47. : "a"(&lock->slock)
  48. : "memory", "cc");
  49. return ret;
  50. }
  51. static inline void arch_spin_lock(arch_spinlock_t *lock)
  52. {
  53. asm volatile(
  54. "1: bset 1,(0,%0) \n"
  55. " bne 1b \n"
  56. :
  57. : "a"(&lock->slock)
  58. : "memory", "cc");
  59. }
  60. static inline void arch_spin_lock_flags(arch_spinlock_t *lock,
  61. unsigned long flags)
  62. {
  63. int temp;
  64. asm volatile(
  65. "1: bset 1,(0,%2) \n"
  66. " beq 3f \n"
  67. " mov %1,epsw \n"
  68. "2: mov (0,%2),%0 \n"
  69. " or %0,%0 \n"
  70. " bne 2b \n"
  71. " mov %3,%0 \n"
  72. " mov %0,epsw \n"
  73. " nop \n"
  74. " nop \n"
  75. " bra 1b\n"
  76. "3: \n"
  77. : "=&d" (temp)
  78. : "d" (flags), "a"(&lock->slock), "i"(EPSW_IE | MN10300_CLI_LEVEL)
  79. : "memory", "cc");
  80. }
  81. #ifdef __KERNEL__
  82. /*
  83. * Read-write spinlocks, allowing multiple readers
  84. * but only one writer.
  85. *
  86. * NOTE! it is quite common to have readers in interrupts
  87. * but no interrupt writers. For those circumstances we
  88. * can "mix" irq-safe locks - any writer needs to get a
  89. * irq-safe write-lock, but readers can get non-irqsafe
  90. * read-locks.
  91. */
  92. /**
  93. * read_can_lock - would read_trylock() succeed?
  94. * @lock: the rwlock in question.
  95. */
  96. #define arch_read_can_lock(x) ((int)(x)->lock > 0)
  97. /**
  98. * write_can_lock - would write_trylock() succeed?
  99. * @lock: the rwlock in question.
  100. */
  101. #define arch_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
  102. /*
  103. * On mn10300, we implement read-write locks as a 32-bit counter
  104. * with the high bit (sign) being the "contended" bit.
  105. */
  106. static inline void arch_read_lock(arch_rwlock_t *rw)
  107. {
  108. #if 0 //def CONFIG_MN10300_HAS_ATOMIC_OPS_UNIT
  109. __build_read_lock(rw, "__read_lock_failed");
  110. #else
  111. {
  112. atomic_t *count = (atomic_t *)rw;
  113. while (atomic_dec_return(count) < 0)
  114. atomic_inc(count);
  115. }
  116. #endif
  117. }
  118. static inline void arch_write_lock(arch_rwlock_t *rw)
  119. {
  120. #if 0 //def CONFIG_MN10300_HAS_ATOMIC_OPS_UNIT
  121. __build_write_lock(rw, "__write_lock_failed");
  122. #else
  123. {
  124. atomic_t *count = (atomic_t *)rw;
  125. while (!atomic_sub_and_test(RW_LOCK_BIAS, count))
  126. atomic_add(RW_LOCK_BIAS, count);
  127. }
  128. #endif
  129. }
  130. static inline void arch_read_unlock(arch_rwlock_t *rw)
  131. {
  132. #if 0 //def CONFIG_MN10300_HAS_ATOMIC_OPS_UNIT
  133. __build_read_unlock(rw);
  134. #else
  135. {
  136. atomic_t *count = (atomic_t *)rw;
  137. atomic_inc(count);
  138. }
  139. #endif
  140. }
  141. static inline void arch_write_unlock(arch_rwlock_t *rw)
  142. {
  143. #if 0 //def CONFIG_MN10300_HAS_ATOMIC_OPS_UNIT
  144. __build_write_unlock(rw);
  145. #else
  146. {
  147. atomic_t *count = (atomic_t *)rw;
  148. atomic_add(RW_LOCK_BIAS, count);
  149. }
  150. #endif
  151. }
  152. static inline int arch_read_trylock(arch_rwlock_t *lock)
  153. {
  154. atomic_t *count = (atomic_t *)lock;
  155. atomic_dec(count);
  156. if (atomic_read(count) >= 0)
  157. return 1;
  158. atomic_inc(count);
  159. return 0;
  160. }
  161. static inline int arch_write_trylock(arch_rwlock_t *lock)
  162. {
  163. atomic_t *count = (atomic_t *)lock;
  164. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  165. return 1;
  166. atomic_add(RW_LOCK_BIAS, count);
  167. return 0;
  168. }
  169. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  170. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  171. #define _raw_spin_relax(lock) cpu_relax()
  172. #define _raw_read_relax(lock) cpu_relax()
  173. #define _raw_write_relax(lock) cpu_relax()
  174. #endif /* __KERNEL__ */
  175. #endif /* _ASM_SPINLOCK_H */