vserver 1.9.3
[linux-2.6.git] / include / asm-sparc / semaphore.h
1 #ifndef _SPARC_SEMAPHORE_H
2 #define _SPARC_SEMAPHORE_H
3
4 /* Dinky, good for nothing, just barely irq safe, Sparc semaphores. */
5
6 #ifdef __KERNEL__
7
8 #include <asm/atomic.h>
9 #include <linux/wait.h>
10 #include <linux/rwsem.h>
11
12 struct semaphore {
13         atomic24_t count;
14         int sleepers;
15         wait_queue_head_t wait;
16 };
17
18 #define __SEMAPHORE_INITIALIZER(name, n)                                \
19 {                                                                       \
20         .count          = ATOMIC24_INIT(n),                             \
21         .sleepers       = 0,                                            \
22         .wait           = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)    \
23 }
24
25 #define __MUTEX_INITIALIZER(name) \
26         __SEMAPHORE_INITIALIZER(name,1)
27
28 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
29         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
30
31 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
32 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
33
34 static inline void sema_init (struct semaphore *sem, int val)
35 {
36         atomic24_set(&sem->count, val);
37         sem->sleepers = 0;
38         init_waitqueue_head(&sem->wait);
39 }
40
41 static inline void init_MUTEX (struct semaphore *sem)
42 {
43         sema_init(sem, 1);
44 }
45
46 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
47 {
48         sema_init(sem, 0);
49 }
50
51 extern void __down(struct semaphore * sem);
52 extern int __down_interruptible(struct semaphore * sem);
53 extern int __down_trylock(struct semaphore * sem);
54 extern void __up(struct semaphore * sem);
55
56 static inline void down(struct semaphore * sem)
57 {
58         register volatile int *ptr asm("g1");
59         register int increment asm("g2");
60
61         might_sleep();
62
63         ptr = &(sem->count.counter);
64         increment = 1;
65
66         __asm__ __volatile__(
67         "mov    %%o7, %%g4\n\t"
68         "call   ___atomic24_sub\n\t"
69         " add   %%o7, 8, %%o7\n\t"
70         "tst    %%g2\n\t"
71         "bl     2f\n\t"
72         " nop\n"
73         "1:\n\t"
74         ".subsection 2\n"
75         "2:\n\t"
76         "save   %%sp, -64, %%sp\n\t"
77         "mov    %%g1, %%l1\n\t"
78         "mov    %%g5, %%l5\n\t"
79         "call   %3\n\t"
80         " mov   %%g1, %%o0\n\t"
81         "mov    %%l1, %%g1\n\t"
82         "ba     1b\n\t"
83         " restore %%l5, %%g0, %%g5\n\t"
84         ".previous\n"
85         : "=&r" (increment)
86         : "0" (increment), "r" (ptr), "i" (__down)
87         : "g3", "g4", "g7", "memory", "cc");
88 }
89
90 static inline int down_interruptible(struct semaphore * sem)
91 {
92         register volatile int *ptr asm("g1");
93         register int increment asm("g2");
94
95         might_sleep();
96
97         ptr = &(sem->count.counter);
98         increment = 1;
99
100         __asm__ __volatile__(
101         "mov    %%o7, %%g4\n\t"
102         "call   ___atomic24_sub\n\t"
103         " add   %%o7, 8, %%o7\n\t"
104         "tst    %%g2\n\t"
105         "bl     2f\n\t"
106         " clr   %%g2\n"
107         "1:\n\t"
108         ".subsection 2\n"
109         "2:\n\t"
110         "save   %%sp, -64, %%sp\n\t"
111         "mov    %%g1, %%l1\n\t"
112         "mov    %%g5, %%l5\n\t"
113         "call   %3\n\t"
114         " mov   %%g1, %%o0\n\t"
115         "mov    %%l1, %%g1\n\t"
116         "mov    %%l5, %%g5\n\t"
117         "ba     1b\n\t"
118         " restore %%o0, %%g0, %%g2\n\t"
119         ".previous\n"
120         : "=&r" (increment)
121         : "0" (increment), "r" (ptr), "i" (__down_interruptible)
122         : "g3", "g4", "g7", "memory", "cc");
123
124         return increment;
125 }
126
127 static inline int down_trylock(struct semaphore * sem)
128 {
129         register volatile int *ptr asm("g1");
130         register int increment asm("g2");
131
132         ptr = &(sem->count.counter);
133         increment = 1;
134
135         __asm__ __volatile__(
136         "mov    %%o7, %%g4\n\t"
137         "call   ___atomic24_sub\n\t"
138         " add   %%o7, 8, %%o7\n\t"
139         "tst    %%g2\n\t"
140         "bl     2f\n\t"
141         " clr   %%g2\n"
142         "1:\n\t"
143         ".subsection 2\n"
144         "2:\n\t"
145         "save   %%sp, -64, %%sp\n\t"
146         "mov    %%g1, %%l1\n\t"
147         "mov    %%g5, %%l5\n\t"
148         "call   %3\n\t"
149         " mov   %%g1, %%o0\n\t"
150         "mov    %%l1, %%g1\n\t"
151         "mov    %%l5, %%g5\n\t"
152         "ba     1b\n\t"
153         " restore %%o0, %%g0, %%g2\n\t"
154         ".previous\n"
155         : "=&r" (increment)
156         : "0" (increment), "r" (ptr), "i" (__down_trylock)
157         : "g3", "g4", "g7", "memory", "cc");
158
159         return increment;
160 }
161
162 static inline void up(struct semaphore * sem)
163 {
164         register volatile int *ptr asm("g1");
165         register int increment asm("g2");
166
167         ptr = &(sem->count.counter);
168         increment = 1;
169
170         __asm__ __volatile__(
171         "mov    %%o7, %%g4\n\t"
172         "call   ___atomic24_add\n\t"
173         " add   %%o7, 8, %%o7\n\t"
174         "tst    %%g2\n\t"
175         "ble    2f\n\t"
176         " nop\n"
177         "1:\n\t"
178         ".subsection 2\n"
179         "2:\n\t"
180         "save   %%sp, -64, %%sp\n\t"
181         "mov    %%g1, %%l1\n\t"
182         "mov    %%g5, %%l5\n\t"
183         "call   %3\n\t"
184         " mov   %%g1, %%o0\n\t"
185         "mov    %%l1, %%g1\n\t"
186         "ba     1b\n\t"
187         " restore %%l5, %%g0, %%g5\n\t"
188         ".previous\n"
189         : "=&r" (increment)
190         : "0" (increment), "r" (ptr), "i" (__up)
191         : "g3", "g4", "g7", "memory", "cc");
192 }       
193
194 #endif /* __KERNEL__ */
195
196 #endif /* !(_SPARC_SEMAPHORE_H) */