rwsem.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #ifndef _S390_RWSEM_H
  2. #define _S390_RWSEM_H
  3. /*
  4. * include/asm-s390/rwsem.h
  5. *
  6. * S390 version
  7. * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
  8. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. *
  10. * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
  11. */
  12. /*
  13. *
  14. * The MSW of the count is the negated number of active writers and waiting
  15. * lockers, and the LSW is the total number of active locks
  16. *
  17. * The lock count is initialized to 0 (no active and no waiting lockers).
  18. *
  19. * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
  20. * uncontended lock. This can be determined because XADD returns the old value.
  21. * Readers increment by 1 and see a positive value when uncontended, negative
  22. * if there are writers (and maybe) readers waiting (in which case it goes to
  23. * sleep).
  24. *
  25. * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
  26. * be extended to 65534 by manually checking the whole MSW rather than relying
  27. * on the S flag.
  28. *
  29. * The value of ACTIVE_BIAS supports up to 65535 active processes.
  30. *
  31. * This should be totally fair - if anything is waiting, a process that wants a
  32. * lock will go to the back of the queue. When the currently active lock is
  33. * released, if there's a writer at the front of the queue, then that and only
  34. * that will be woken up; if there's a bunch of consequtive readers at the
  35. * front, then they'll all be woken up, but no other readers will be.
  36. */
  37. #ifndef _LINUX_RWSEM_H
  38. #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
  39. #endif
  40. #ifdef __KERNEL__
  41. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *);
  42. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *);
  43. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
  44. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *);
  45. extern struct rw_semaphore *rwsem_downgrade_write(struct rw_semaphore *);
  46. #ifndef __s390x__
  47. #define RWSEM_UNLOCKED_VALUE 0x00000000
  48. #define RWSEM_ACTIVE_BIAS 0x00000001
  49. #define RWSEM_ACTIVE_MASK 0x0000ffff
  50. #define RWSEM_WAITING_BIAS (-0x00010000)
  51. #else /* __s390x__ */
  52. #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
  53. #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
  54. #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
  55. #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
  56. #endif /* __s390x__ */
  57. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  58. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  59. /*
  60. * initialisation
  61. */
  62. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  63. # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
  64. #else
  65. # define __RWSEM_DEP_MAP_INIT(lockname)
  66. #endif
  67. #define __RWSEM_INITIALIZER(name) \
  68. { RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED((name).wait.lock), \
  69. LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) }
  70. #define DECLARE_RWSEM(name) \
  71. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  72. static inline void init_rwsem(struct rw_semaphore *sem)
  73. {
  74. sem->count = RWSEM_UNLOCKED_VALUE;
  75. spin_lock_init(&sem->wait_lock);
  76. INIT_LIST_HEAD(&sem->wait_list);
  77. }
  78. extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  79. struct lock_class_key *key);
  80. #define init_rwsem(sem) \
  81. do { \
  82. static struct lock_class_key __key; \
  83. \
  84. __init_rwsem((sem), #sem, &__key); \
  85. } while (0)
  86. /*
  87. * lock for reading
  88. */
  89. static inline void __down_read(struct rw_semaphore *sem)
  90. {
  91. signed long old, new;
  92. asm volatile(
  93. #ifndef __s390x__
  94. " l %0,%2\n"
  95. "0: lr %1,%0\n"
  96. " ahi %1,%4\n"
  97. " cs %0,%1,%2\n"
  98. " jl 0b"
  99. #else /* __s390x__ */
  100. " lg %0,%2\n"
  101. "0: lgr %1,%0\n"
  102. " aghi %1,%4\n"
  103. " csg %0,%1,%2\n"
  104. " jl 0b"
  105. #endif /* __s390x__ */
  106. : "=&d" (old), "=&d" (new), "=Q" (sem->count)
  107. : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
  108. : "cc", "memory");
  109. if (old < 0)
  110. rwsem_down_read_failed(sem);
  111. }
  112. /*
  113. * trylock for reading -- returns 1 if successful, 0 if contention
  114. */
  115. static inline int __down_read_trylock(struct rw_semaphore *sem)
  116. {
  117. signed long old, new;
  118. asm volatile(
  119. #ifndef __s390x__
  120. " l %0,%2\n"
  121. "0: ltr %1,%0\n"
  122. " jm 1f\n"
  123. " ahi %1,%4\n"
  124. " cs %0,%1,%2\n"
  125. " jl 0b\n"
  126. "1:"
  127. #else /* __s390x__ */
  128. " lg %0,%2\n"
  129. "0: ltgr %1,%0\n"
  130. " jm 1f\n"
  131. " aghi %1,%4\n"
  132. " csg %0,%1,%2\n"
  133. " jl 0b\n"
  134. "1:"
  135. #endif /* __s390x__ */
  136. : "=&d" (old), "=&d" (new), "=Q" (sem->count)
  137. : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
  138. : "cc", "memory");
  139. return old >= 0 ? 1 : 0;
  140. }
  141. /*
  142. * lock for writing
  143. */
  144. static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
  145. {
  146. signed long old, new, tmp;
  147. tmp = RWSEM_ACTIVE_WRITE_BIAS;
  148. asm volatile(
  149. #ifndef __s390x__
  150. " l %0,%2\n"
  151. "0: lr %1,%0\n"
  152. " a %1,%4\n"
  153. " cs %0,%1,%2\n"
  154. " jl 0b"
  155. #else /* __s390x__ */
  156. " lg %0,%2\n"
  157. "0: lgr %1,%0\n"
  158. " ag %1,%4\n"
  159. " csg %0,%1,%2\n"
  160. " jl 0b"
  161. #endif /* __s390x__ */
  162. : "=&d" (old), "=&d" (new), "=Q" (sem->count)
  163. : "Q" (sem->count), "m" (tmp)
  164. : "cc", "memory");
  165. if (old != 0)
  166. rwsem_down_write_failed(sem);
  167. }
  168. static inline void __down_write(struct rw_semaphore *sem)
  169. {
  170. __down_write_nested(sem, 0);
  171. }
  172. /*
  173. * trylock for writing -- returns 1 if successful, 0 if contention
  174. */
  175. static inline int __down_write_trylock(struct rw_semaphore *sem)
  176. {
  177. signed long old;
  178. asm volatile(
  179. #ifndef __s390x__
  180. " l %0,%1\n"
  181. "0: ltr %0,%0\n"
  182. " jnz 1f\n"
  183. " cs %0,%3,%1\n"
  184. " jl 0b\n"
  185. #else /* __s390x__ */
  186. " lg %0,%1\n"
  187. "0: ltgr %0,%0\n"
  188. " jnz 1f\n"
  189. " csg %0,%3,%1\n"
  190. " jl 0b\n"
  191. #endif /* __s390x__ */
  192. "1:"
  193. : "=&d" (old), "=Q" (sem->count)
  194. : "Q" (sem->count), "d" (RWSEM_ACTIVE_WRITE_BIAS)
  195. : "cc", "memory");
  196. return (old == RWSEM_UNLOCKED_VALUE) ? 1 : 0;
  197. }
  198. /*
  199. * unlock after reading
  200. */
  201. static inline void __up_read(struct rw_semaphore *sem)
  202. {
  203. signed long old, new;
  204. asm volatile(
  205. #ifndef __s390x__
  206. " l %0,%2\n"
  207. "0: lr %1,%0\n"
  208. " ahi %1,%4\n"
  209. " cs %0,%1,%2\n"
  210. " jl 0b"
  211. #else /* __s390x__ */
  212. " lg %0,%2\n"
  213. "0: lgr %1,%0\n"
  214. " aghi %1,%4\n"
  215. " csg %0,%1,%2\n"
  216. " jl 0b"
  217. #endif /* __s390x__ */
  218. : "=&d" (old), "=&d" (new), "=Q" (sem->count)
  219. : "Q" (sem->count), "i" (-RWSEM_ACTIVE_READ_BIAS)
  220. : "cc", "memory");
  221. if (new < 0)
  222. if ((new & RWSEM_ACTIVE_MASK) == 0)
  223. rwsem_wake(sem);
  224. }
  225. /*
  226. * unlock after writing
  227. */
  228. static inline void __up_write(struct rw_semaphore *sem)
  229. {
  230. signed long old, new, tmp;
  231. tmp = -RWSEM_ACTIVE_WRITE_BIAS;
  232. asm volatile(
  233. #ifndef __s390x__
  234. " l %0,%2\n"
  235. "0: lr %1,%0\n"
  236. " a %1,%4\n"
  237. " cs %0,%1,%2\n"
  238. " jl 0b"
  239. #else /* __s390x__ */
  240. " lg %0,%2\n"
  241. "0: lgr %1,%0\n"
  242. " ag %1,%4\n"
  243. " csg %0,%1,%2\n"
  244. " jl 0b"
  245. #endif /* __s390x__ */
  246. : "=&d" (old), "=&d" (new), "=Q" (sem->count)
  247. : "Q" (sem->count), "m" (tmp)
  248. : "cc", "memory");
  249. if (new < 0)
  250. if ((new & RWSEM_ACTIVE_MASK) == 0)
  251. rwsem_wake(sem);
  252. }
  253. /*
  254. * downgrade write lock to read lock
  255. */
  256. static inline void __downgrade_write(struct rw_semaphore *sem)
  257. {
  258. signed long old, new, tmp;
  259. tmp = -RWSEM_WAITING_BIAS;
  260. asm volatile(
  261. #ifndef __s390x__
  262. " l %0,%2\n"
  263. "0: lr %1,%0\n"
  264. " a %1,%4\n"
  265. " cs %0,%1,%2\n"
  266. " jl 0b"
  267. #else /* __s390x__ */
  268. " lg %0,%2\n"
  269. "0: lgr %1,%0\n"
  270. " ag %1,%4\n"
  271. " csg %0,%1,%2\n"
  272. " jl 0b"
  273. #endif /* __s390x__ */
  274. : "=&d" (old), "=&d" (new), "=Q" (sem->count)
  275. : "Q" (sem->count), "m" (tmp)
  276. : "cc", "memory");
  277. if (new > 1)
  278. rwsem_downgrade_wake(sem);
  279. }
  280. /*
  281. * implement atomic add functionality
  282. */
  283. static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
  284. {
  285. signed long old, new;
  286. asm volatile(
  287. #ifndef __s390x__
  288. " l %0,%2\n"
  289. "0: lr %1,%0\n"
  290. " ar %1,%4\n"
  291. " cs %0,%1,%2\n"
  292. " jl 0b"
  293. #else /* __s390x__ */
  294. " lg %0,%2\n"
  295. "0: lgr %1,%0\n"
  296. " agr %1,%4\n"
  297. " csg %0,%1,%2\n"
  298. " jl 0b"
  299. #endif /* __s390x__ */
  300. : "=&d" (old), "=&d" (new), "=Q" (sem->count)
  301. : "Q" (sem->count), "d" (delta)
  302. : "cc", "memory");
  303. }
  304. /*
  305. * implement exchange and add functionality
  306. */
  307. static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
  308. {
  309. signed long old, new;
  310. asm volatile(
  311. #ifndef __s390x__
  312. " l %0,%2\n"
  313. "0: lr %1,%0\n"
  314. " ar %1,%4\n"
  315. " cs %0,%1,%2\n"
  316. " jl 0b"
  317. #else /* __s390x__ */
  318. " lg %0,%2\n"
  319. "0: lgr %1,%0\n"
  320. " agr %1,%4\n"
  321. " csg %0,%1,%2\n"
  322. " jl 0b"
  323. #endif /* __s390x__ */
  324. : "=&d" (old), "=&d" (new), "=Q" (sem->count)
  325. : "Q" (sem->count), "d" (delta)
  326. : "cc", "memory");
  327. return new;
  328. }
  329. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  330. {
  331. return (sem->count != 0);
  332. }
  333. #endif /* __KERNEL__ */
  334. #endif /* _S390_RWSEM_H */