spinlock.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef __ASM_SPINLOCK_H
  9. #define __ASM_SPINLOCK_H
  10. #include <asm/spinlock_types.h>
  11. #include <asm/processor.h>
  12. #include <asm/barrier.h>
  13. #define arch_spin_is_locked(x) ((x)->slock != __ARCH_SPIN_LOCK_UNLOCKED__)
  14. #ifdef CONFIG_ARC_HAS_LLSC
  15. static inline void arch_spin_lock(arch_spinlock_t *lock)
  16. {
  17. unsigned int val;
  18. smp_mb();
  19. __asm__ __volatile__(
  20. "1: llock %[val], [%[slock]] \n"
  21. " breq %[val], %[LOCKED], 1b \n" /* spin while LOCKED */
  22. " scond %[LOCKED], [%[slock]] \n" /* acquire */
  23. " bnz 1b \n"
  24. " \n"
  25. : [val] "=&r" (val)
  26. : [slock] "r" (&(lock->slock)),
  27. [LOCKED] "r" (__ARCH_SPIN_LOCK_LOCKED__)
  28. : "memory", "cc");
  29. smp_mb();
  30. }
  31. /* 1 - lock taken successfully */
  32. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  33. {
  34. unsigned int val, got_it = 0;
  35. smp_mb();
  36. __asm__ __volatile__(
  37. "1: llock %[val], [%[slock]] \n"
  38. " breq %[val], %[LOCKED], 4f \n" /* already LOCKED, just bail */
  39. " scond %[LOCKED], [%[slock]] \n" /* acquire */
  40. " bnz 1b \n"
  41. " mov %[got_it], 1 \n"
  42. "4: \n"
  43. " \n"
  44. : [val] "=&r" (val),
  45. [got_it] "+&r" (got_it)
  46. : [slock] "r" (&(lock->slock)),
  47. [LOCKED] "r" (__ARCH_SPIN_LOCK_LOCKED__)
  48. : "memory", "cc");
  49. smp_mb();
  50. return got_it;
  51. }
  52. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  53. {
  54. smp_mb();
  55. lock->slock = __ARCH_SPIN_LOCK_UNLOCKED__;
  56. smp_mb();
  57. }
  58. /*
  59. * Read-write spinlocks, allowing multiple readers but only one writer.
  60. * Unfair locking as Writers could be starved indefinitely by Reader(s)
  61. */
  62. static inline void arch_read_lock(arch_rwlock_t *rw)
  63. {
  64. unsigned int val;
  65. smp_mb();
  66. /*
  67. * zero means writer holds the lock exclusively, deny Reader.
  68. * Otherwise grant lock to first/subseq reader
  69. *
  70. * if (rw->counter > 0) {
  71. * rw->counter--;
  72. * ret = 1;
  73. * }
  74. */
  75. __asm__ __volatile__(
  76. "1: llock %[val], [%[rwlock]] \n"
  77. " brls %[val], %[WR_LOCKED], 1b\n" /* <= 0: spin while write locked */
  78. " sub %[val], %[val], 1 \n" /* reader lock */
  79. " scond %[val], [%[rwlock]] \n"
  80. " bnz 1b \n"
  81. " \n"
  82. : [val] "=&r" (val)
  83. : [rwlock] "r" (&(rw->counter)),
  84. [WR_LOCKED] "ir" (0)
  85. : "memory", "cc");
  86. smp_mb();
  87. }
  88. /* 1 - lock taken successfully */
  89. static inline int arch_read_trylock(arch_rwlock_t *rw)
  90. {
  91. unsigned int val, got_it = 0;
  92. smp_mb();
  93. __asm__ __volatile__(
  94. "1: llock %[val], [%[rwlock]] \n"
  95. " brls %[val], %[WR_LOCKED], 4f\n" /* <= 0: already write locked, bail */
  96. " sub %[val], %[val], 1 \n" /* counter-- */
  97. " scond %[val], [%[rwlock]] \n"
  98. " bnz 1b \n" /* retry if collided with someone */
  99. " mov %[got_it], 1 \n"
  100. " \n"
  101. "4: ; --- done --- \n"
  102. : [val] "=&r" (val),
  103. [got_it] "+&r" (got_it)
  104. : [rwlock] "r" (&(rw->counter)),
  105. [WR_LOCKED] "ir" (0)
  106. : "memory", "cc");
  107. smp_mb();
  108. return got_it;
  109. }
  110. static inline void arch_write_lock(arch_rwlock_t *rw)
  111. {
  112. unsigned int val;
  113. smp_mb();
  114. /*
  115. * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
  116. * deny writer. Otherwise if unlocked grant to writer
  117. * Hence the claim that Linux rwlocks are unfair to writers.
  118. * (can be starved for an indefinite time by readers).
  119. *
  120. * if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
  121. * rw->counter = 0;
  122. * ret = 1;
  123. * }
  124. */
  125. __asm__ __volatile__(
  126. "1: llock %[val], [%[rwlock]] \n"
  127. " brne %[val], %[UNLOCKED], 1b \n" /* while !UNLOCKED spin */
  128. " mov %[val], %[WR_LOCKED] \n"
  129. " scond %[val], [%[rwlock]] \n"
  130. " bnz 1b \n"
  131. " \n"
  132. : [val] "=&r" (val)
  133. : [rwlock] "r" (&(rw->counter)),
  134. [UNLOCKED] "ir" (__ARCH_RW_LOCK_UNLOCKED__),
  135. [WR_LOCKED] "ir" (0)
  136. : "memory", "cc");
  137. smp_mb();
  138. }
  139. /* 1 - lock taken successfully */
  140. static inline int arch_write_trylock(arch_rwlock_t *rw)
  141. {
  142. unsigned int val, got_it = 0;
  143. smp_mb();
  144. __asm__ __volatile__(
  145. "1: llock %[val], [%[rwlock]] \n"
  146. " brne %[val], %[UNLOCKED], 4f \n" /* !UNLOCKED, bail */
  147. " mov %[val], %[WR_LOCKED] \n"
  148. " scond %[val], [%[rwlock]] \n"
  149. " bnz 1b \n" /* retry if collided with someone */
  150. " mov %[got_it], 1 \n"
  151. " \n"
  152. "4: ; --- done --- \n"
  153. : [val] "=&r" (val),
  154. [got_it] "+&r" (got_it)
  155. : [rwlock] "r" (&(rw->counter)),
  156. [UNLOCKED] "ir" (__ARCH_RW_LOCK_UNLOCKED__),
  157. [WR_LOCKED] "ir" (0)
  158. : "memory", "cc");
  159. smp_mb();
  160. return got_it;
  161. }
  162. static inline void arch_read_unlock(arch_rwlock_t *rw)
  163. {
  164. unsigned int val;
  165. smp_mb();
  166. /*
  167. * rw->counter++;
  168. */
  169. __asm__ __volatile__(
  170. "1: llock %[val], [%[rwlock]] \n"
  171. " add %[val], %[val], 1 \n"
  172. " scond %[val], [%[rwlock]] \n"
  173. " bnz 1b \n"
  174. " \n"
  175. : [val] "=&r" (val)
  176. : [rwlock] "r" (&(rw->counter))
  177. : "memory", "cc");
  178. smp_mb();
  179. }
  180. static inline void arch_write_unlock(arch_rwlock_t *rw)
  181. {
  182. smp_mb();
  183. rw->counter = __ARCH_RW_LOCK_UNLOCKED__;
  184. smp_mb();
  185. }
  186. #else /* !CONFIG_ARC_HAS_LLSC */
  187. static inline void arch_spin_lock(arch_spinlock_t *lock)
  188. {
  189. unsigned int val = __ARCH_SPIN_LOCK_LOCKED__;
  190. /*
  191. * This smp_mb() is technically superfluous, we only need the one
  192. * after the lock for providing the ACQUIRE semantics.
  193. * However doing the "right" thing was regressing hackbench
  194. * so keeping this, pending further investigation
  195. */
  196. smp_mb();
  197. __asm__ __volatile__(
  198. "1: ex %0, [%1] \n"
  199. #ifdef CONFIG_EZNPS_MTM_EXT
  200. " .word %3 \n"
  201. #endif
  202. " breq %0, %2, 1b \n"
  203. : "+&r" (val)
  204. : "r"(&(lock->slock)), "ir"(__ARCH_SPIN_LOCK_LOCKED__)
  205. #ifdef CONFIG_EZNPS_MTM_EXT
  206. , "i"(CTOP_INST_SCHD_RW)
  207. #endif
  208. : "memory");
  209. /*
  210. * ACQUIRE barrier to ensure load/store after taking the lock
  211. * don't "bleed-up" out of the critical section (leak-in is allowed)
  212. * http://www.spinics.net/lists/kernel/msg2010409.html
  213. *
  214. * ARCv2 only has load-load, store-store and all-all barrier
  215. * thus need the full all-all barrier
  216. */
  217. smp_mb();
  218. }
  219. /* 1 - lock taken successfully */
  220. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  221. {
  222. unsigned int val = __ARCH_SPIN_LOCK_LOCKED__;
  223. smp_mb();
  224. __asm__ __volatile__(
  225. "1: ex %0, [%1] \n"
  226. : "+r" (val)
  227. : "r"(&(lock->slock))
  228. : "memory");
  229. smp_mb();
  230. return (val == __ARCH_SPIN_LOCK_UNLOCKED__);
  231. }
  232. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  233. {
  234. unsigned int val = __ARCH_SPIN_LOCK_UNLOCKED__;
  235. /*
  236. * RELEASE barrier: given the instructions avail on ARCv2, full barrier
  237. * is the only option
  238. */
  239. smp_mb();
  240. /*
  241. * EX is not really required here, a simple STore of 0 suffices.
  242. * However this causes tasklist livelocks in SystemC based SMP virtual
  243. * platforms where the systemc core scheduler uses EX as a cue for
  244. * moving to next core. Do a git log of this file for details
  245. */
  246. __asm__ __volatile__(
  247. " ex %0, [%1] \n"
  248. : "+r" (val)
  249. : "r"(&(lock->slock))
  250. : "memory");
  251. /*
  252. * superfluous, but keeping for now - see pairing version in
  253. * arch_spin_lock above
  254. */
  255. smp_mb();
  256. }
  257. /*
  258. * Read-write spinlocks, allowing multiple readers but only one writer.
  259. * Unfair locking as Writers could be starved indefinitely by Reader(s)
  260. *
  261. * The spinlock itself is contained in @counter and access to it is
  262. * serialized with @lock_mutex.
  263. */
  264. /* 1 - lock taken successfully */
  265. static inline int arch_read_trylock(arch_rwlock_t *rw)
  266. {
  267. int ret = 0;
  268. unsigned long flags;
  269. local_irq_save(flags);
  270. arch_spin_lock(&(rw->lock_mutex));
  271. /*
  272. * zero means writer holds the lock exclusively, deny Reader.
  273. * Otherwise grant lock to first/subseq reader
  274. */
  275. if (rw->counter > 0) {
  276. rw->counter--;
  277. ret = 1;
  278. }
  279. arch_spin_unlock(&(rw->lock_mutex));
  280. local_irq_restore(flags);
  281. smp_mb();
  282. return ret;
  283. }
  284. /* 1 - lock taken successfully */
  285. static inline int arch_write_trylock(arch_rwlock_t *rw)
  286. {
  287. int ret = 0;
  288. unsigned long flags;
  289. local_irq_save(flags);
  290. arch_spin_lock(&(rw->lock_mutex));
  291. /*
  292. * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
  293. * deny writer. Otherwise if unlocked grant to writer
  294. * Hence the claim that Linux rwlocks are unfair to writers.
  295. * (can be starved for an indefinite time by readers).
  296. */
  297. if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
  298. rw->counter = 0;
  299. ret = 1;
  300. }
  301. arch_spin_unlock(&(rw->lock_mutex));
  302. local_irq_restore(flags);
  303. return ret;
  304. }
  305. static inline void arch_read_lock(arch_rwlock_t *rw)
  306. {
  307. while (!arch_read_trylock(rw))
  308. cpu_relax();
  309. }
  310. static inline void arch_write_lock(arch_rwlock_t *rw)
  311. {
  312. while (!arch_write_trylock(rw))
  313. cpu_relax();
  314. }
  315. static inline void arch_read_unlock(arch_rwlock_t *rw)
  316. {
  317. unsigned long flags;
  318. local_irq_save(flags);
  319. arch_spin_lock(&(rw->lock_mutex));
  320. rw->counter++;
  321. arch_spin_unlock(&(rw->lock_mutex));
  322. local_irq_restore(flags);
  323. }
  324. static inline void arch_write_unlock(arch_rwlock_t *rw)
  325. {
  326. unsigned long flags;
  327. local_irq_save(flags);
  328. arch_spin_lock(&(rw->lock_mutex));
  329. rw->counter = __ARCH_RW_LOCK_UNLOCKED__;
  330. arch_spin_unlock(&(rw->lock_mutex));
  331. local_irq_restore(flags);
  332. }
  333. #endif
  334. #endif /* __ASM_SPINLOCK_H */