blob: 8447d89fbe72639a6a0ce2db68694554bd7c61e8 [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#include <linux/list.h>
17#include <linux/spinlock.h>
18#include <asm/atomic.h>
19#include <asm/system.h>
20
21/*
22 * the semaphore definition
23 */
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100024#ifdef CONFIG_PPC64
25# define RWSEM_ACTIVE_MASK 0xffffffffL
26#else
27# define RWSEM_ACTIVE_MASK 0x0000ffffL
28#endif
29
30#define RWSEM_UNLOCKED_VALUE 0x00000000L
31#define RWSEM_ACTIVE_BIAS 0x00000001L
32#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
34#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100035
36struct rw_semaphore {
37 long count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 spinlock_t wait_lock;
39 struct list_head wait_list;
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +100040#ifdef CONFIG_DEBUG_LOCK_ALLOC
41 struct lockdep_map dep_map;
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +100045#ifdef CONFIG_DEBUG_LOCK_ALLOC
46# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
47#else
48# define __RWSEM_DEP_MAP_INIT(lockname)
49#endif
50
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100051#define __RWSEM_INITIALIZER(name) \
52{ \
53 RWSEM_UNLOCKED_VALUE, \
54 __SPIN_LOCK_UNLOCKED((name).wait_lock), \
55 LIST_HEAD_INIT((name).wait_list) \
56 __RWSEM_DEP_MAP_INIT(name) \
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define DECLARE_RWSEM(name) \
60 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
61
62extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
63extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
64extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
65extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
66
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +100067extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
68 struct lock_class_key *key);
69
70#define init_rwsem(sem) \
71 do { \
72 static struct lock_class_key __key; \
73 \
74 __init_rwsem((sem), #sem, &__key); \
75 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * lock for reading
79 */
80static inline void __down_read(struct rw_semaphore *sem)
81{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100082 if (unlikely(atomic_long_inc_return((atomic_long_t *)&sem->count) <= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 rwsem_down_read_failed(sem);
84}
85
86static inline int __down_read_trylock(struct rw_semaphore *sem)
87{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +100088 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 while ((tmp = sem->count) >= 0) {
91 if (tmp == cmpxchg(&sem->count, tmp,
92 tmp + RWSEM_ACTIVE_READ_BIAS)) {
93 return 1;
94 }
95 }
96 return 0;
97}
98
99/*
100 * lock for writing
101 */
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +1000102static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000104 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000106 tmp = atomic_long_add_return(RWSEM_ACTIVE_WRITE_BIAS,
107 (atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
109 rwsem_down_write_failed(sem);
110}
111
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +1000112static inline void __down_write(struct rw_semaphore *sem)
113{
114 __down_write_nested(sem, 0);
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static inline int __down_write_trylock(struct rw_semaphore *sem)
118{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000119 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
122 RWSEM_ACTIVE_WRITE_BIAS);
123 return tmp == RWSEM_UNLOCKED_VALUE;
124}
125
126/*
127 * unlock after reading
128 */
129static inline void __up_read(struct rw_semaphore *sem)
130{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000131 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000133 tmp = atomic_long_dec_return((atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
135 rwsem_wake(sem);
136}
137
138/*
139 * unlock after writing
140 */
141static inline void __up_write(struct rw_semaphore *sem)
142{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000143 if (unlikely(atomic_long_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
144 (atomic_long_t *)&sem->count) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 rwsem_wake(sem);
146}
147
148/*
149 * implement atomic add functionality
150 */
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000151static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000153 atomic_long_add(delta, (atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/*
157 * downgrade write lock to read lock
158 */
159static inline void __downgrade_write(struct rw_semaphore *sem)
160{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000161 long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000163 tmp = atomic_long_add_return(-RWSEM_WAITING_BIAS,
164 (atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (tmp < 0)
166 rwsem_downgrade_wake(sem);
167}
168
169/*
170 * implement exchange and add functionality
171 */
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000172static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000174 return atomic_long_add_return(delta, (atomic_long_t *)&sem->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Rik Van Rieleb92f4e2005-10-29 18:15:44 -0700177static inline int rwsem_is_locked(struct rw_semaphore *sem)
178{
Benjamin Herrenschmidt529b7302010-08-24 14:41:48 +1000179 return sem->count != 0;
Rik Van Rieleb92f4e2005-10-29 18:15:44 -0700180}
181
Jon Loeliger342e73b2005-09-22 14:55:30 -0500182#endif /* __KERNEL__ */
183#endif /* _ASM_POWERPC_RWSEM_H */