blob: bb1e2cdeb9bff0ec6322680196c11e5e676b2ca6 [file] [log] [blame]
Jon Loeliger342e73b2005-09-22 14:55:30 -05001#ifndef _ASM_POWERPC_RWSEM_H
2#define _ASM_POWERPC_RWSEM_H
3
Robert P. J. Day20b31b52007-07-18 23:36:36 +10004#ifndef _LINUX_RWSEM_H
5#error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
6#endif
7
Jon Loeliger342e73b2005-09-22 14:55:30 -05008#ifdef __KERNEL__
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010/*
Stephen Rothwellb8b572e2008-08-01 15:20:30 +100011 * R/W semaphores for PPC using the stuff in lib/rwsem.c.
12 * Adapted largely from include/asm-i386/rwsem.h
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * by Paul Mackerras <paulus@samba.org>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/*
17 * the semaphore definition
18 */
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100019#ifdef CONFIG_PPC64
20# define RWSEM_ACTIVE_MASK 0xffffffffL
21#else
22# define RWSEM_ACTIVE_MASK 0x0000ffffL
23#endif
24
25#define RWSEM_UNLOCKED_VALUE 0x00000000L
26#define RWSEM_ACTIVE_BIAS 0x00000001L
27#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
29#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
32 * lock for reading
33 */
34static inline void __down_read(struct rw_semaphore *sem)
35{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100036 if (unlikely(atomic_long_inc_return((atomic_long_t *)&sem->count) <= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 rwsem_down_read_failed(sem);
38}
39
40static inline int __down_read_trylock(struct rw_semaphore *sem)
41{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100042 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 while ((tmp = sem->count) >= 0) {
45 if (tmp == cmpxchg(&sem->count, tmp,
46 tmp + RWSEM_ACTIVE_READ_BIAS)) {
47 return 1;
48 }
49 }
50 return 0;
51}
52
53/*
54 * lock for writing
55 */
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +100056static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100058 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100060 tmp = atomic_long_add_return(RWSEM_ACTIVE_WRITE_BIAS,
61 (atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
63 rwsem_down_write_failed(sem);
64}
65
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +100066static inline void __down_write(struct rw_semaphore *sem)
67{
68 __down_write_nested(sem, 0);
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static inline int __down_write_trylock(struct rw_semaphore *sem)
72{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100073 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
76 RWSEM_ACTIVE_WRITE_BIAS);
77 return tmp == RWSEM_UNLOCKED_VALUE;
78}
79
80/*
81 * unlock after reading
82 */
83static inline void __up_read(struct rw_semaphore *sem)
84{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100085 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100087 tmp = atomic_long_dec_return((atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
89 rwsem_wake(sem);
90}
91
92/*
93 * unlock after writing
94 */
95static inline void __up_write(struct rw_semaphore *sem)
96{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100097 if (unlikely(atomic_long_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
98 (atomic_long_t *)&sem->count) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 rwsem_wake(sem);
100}
101
102/*
103 * implement atomic add functionality
104 */
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000105static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000107 atomic_long_add(delta, (atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110/*
111 * downgrade write lock to read lock
112 */
113static inline void __downgrade_write(struct rw_semaphore *sem)
114{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000115 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000117 tmp = atomic_long_add_return(-RWSEM_WAITING_BIAS,
118 (atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (tmp < 0)
120 rwsem_downgrade_wake(sem);
121}
122
123/*
124 * implement exchange and add functionality
125 */
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000126static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000128 return atomic_long_add_return(delta, (atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Jon Loeliger342e73b2005-09-22 14:55:30 -0500131#endif /* __KERNEL__ */
132#endif /* _ASM_POWERPC_RWSEM_H */