This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / include / asm-m32r / semaphore.h
1 #ifndef _ASM_M32R_SEMAPHORE_H
2 #define _ASM_M32R_SEMAPHORE_H
3
4 #include <linux/linkage.h>
5
6 #ifdef __KERNEL__
7
8 /*
9  * SMP- and interrupt-safe semaphores..
10  *
11  * Copyright (C) 1996  Linus Torvalds
12  * Copyright (C) 2004  Hirokazu Takata <takata at linux-m32r.org>
13  */
14
15 #include <linux/config.h>
16 #include <linux/wait.h>
17 #include <linux/rwsem.h>
18 #include <asm/system.h>
19 #include <asm/atomic.h>
20
21 #undef LOAD
22 #undef STORE
23 #ifdef CONFIG_SMP
24 #define LOAD    "lock"
25 #define STORE   "unlock"
26 #else
27 #define LOAD    "ld"
28 #define STORE   "st"
29 #endif
30
31 struct semaphore {
32         atomic_t count;
33         int sleepers;
34         wait_queue_head_t wait;
35 };
36
37 #define __SEMAPHORE_INITIALIZER(name, n)                                \
38 {                                                                       \
39         .count          = ATOMIC_INIT(n),                               \
40         .sleepers       = 0,                                            \
41         .wait           = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)    \
42 }
43
44 #define __MUTEX_INITIALIZER(name) \
45         __SEMAPHORE_INITIALIZER(name,1)
46
47 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
48         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
49
50 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
51 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
52
53 static inline void sema_init (struct semaphore *sem, int val)
54 {
55 /*
56  *      *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
57  *
58  * i'd rather use the more flexible initialization above, but sadly
59  * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
60  */
61         atomic_set(&sem->count, val);
62         sem->sleepers = 0;
63         init_waitqueue_head(&sem->wait);
64 }
65
66 static inline void init_MUTEX (struct semaphore *sem)
67 {
68         sema_init(sem, 1);
69 }
70
71 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
72 {
73         sema_init(sem, 0);
74 }
75
76 asmlinkage void __down_failed(void /* special register calling convention */);
77 asmlinkage int  __down_failed_interruptible(void  /* params in registers */);
78 asmlinkage int  __down_failed_trylock(void  /* params in registers */);
79 asmlinkage void __up_wakeup(void /* special register calling convention */);
80
81 asmlinkage void __down(struct semaphore * sem);
82 asmlinkage int  __down_interruptible(struct semaphore * sem);
83 asmlinkage int  __down_trylock(struct semaphore * sem);
84 asmlinkage void __up(struct semaphore * sem);
85
86 /*
87  * Atomically decrement the semaphore's count.  If it goes negative,
88  * block the calling thread in the TASK_UNINTERRUPTIBLE state.
89  */
90 static inline void down(struct semaphore * sem)
91 {
92         unsigned long flags;
93         long count;
94
95         might_sleep();
96         local_irq_save(flags);
97         __asm__ __volatile__ (
98                 "# down                         \n\t"
99                 DCACHE_CLEAR("%0", "r4", "%1")
100                 LOAD"   %0, @%1;                \n\t"
101                 "addi   %0, #-1;                \n\t"
102                 STORE"  %0, @%1;                \n\t"
103                 : "=&r" (count)
104                 : "r" (&sem->count)
105                 : "memory"
106 #ifdef CONFIG_CHIP_M32700_TS1
107                 , "r4"
108 #endif  /* CONFIG_CHIP_M32700_TS1 */
109         );
110         local_irq_restore(flags);
111
112         if (unlikely(count < 0))
113                 __down(sem);
114 }
115
116 /*
117  * Interruptible try to acquire a semaphore.  If we obtained
118  * it, return zero.  If we were interrupted, returns -EINTR
119  */
120 static inline int down_interruptible(struct semaphore * sem)
121 {
122         unsigned long flags;
123         long count;
124         int result = 0;
125
126         might_sleep();
127         local_irq_save(flags);
128         __asm__ __volatile__ (
129                 "# down_interruptible           \n\t"
130                 DCACHE_CLEAR("%0", "r4", "%1")
131                 LOAD"   %0, @%1;                \n\t"
132                 "addi   %0, #-1;                \n\t"
133                 STORE"  %0, @%1;                \n\t"
134                 : "=&r" (count)
135                 : "r" (&sem->count)
136                 : "memory"
137 #ifdef CONFIG_CHIP_M32700_TS1
138                 , "r4"
139 #endif  /* CONFIG_CHIP_M32700_TS1 */
140         );
141         local_irq_restore(flags);
142
143         if (unlikely(count < 0))
144                 result = __down_interruptible(sem);
145
146         return result;
147 }
148
149 /*
150  * Non-blockingly attempt to down() a semaphore.
151  * Returns zero if we acquired it
152  */
153 static inline int down_trylock(struct semaphore * sem)
154 {
155         unsigned long flags;
156         long count;
157         int result = 0;
158
159         local_irq_save(flags);
160         __asm__ __volatile__ (
161                 "# down_trylock                 \n\t"
162                 DCACHE_CLEAR("%0", "r4", "%1")
163                 LOAD"   %0, @%1;                \n\t"
164                 "addi   %0, #-1;                \n\t"
165                 STORE"  %0, @%1;                \n\t"
166                 : "=&r" (count)
167                 : "r" (&sem->count)
168                 : "memory"
169 #ifdef CONFIG_CHIP_M32700_TS1
170                 , "r4"
171 #endif  /* CONFIG_CHIP_M32700_TS1 */
172         );
173         local_irq_restore(flags);
174
175         if (unlikely(count < 0))
176                 result = __down_trylock(sem);
177
178         return result;
179 }
180
181 /*
182  * Note! This is subtle. We jump to wake people up only if
183  * the semaphore was negative (== somebody was waiting on it).
184  * The default case (no contention) will result in NO
185  * jumps for both down() and up().
186  */
187 static inline void up(struct semaphore * sem)
188 {
189         unsigned long flags;
190         long count;
191
192         local_irq_save(flags);
193         __asm__ __volatile__ (
194                 "# up                           \n\t"
195                 DCACHE_CLEAR("%0", "r4", "%1")
196                 LOAD"   %0, @%1;                \n\t"
197                 "addi   %0, #1;                 \n\t"
198                 STORE"  %0, @%1;                \n\t"
199                 : "=&r" (count)
200                 : "r" (&sem->count)
201                 : "memory"
202 #ifdef CONFIG_CHIP_M32700_TS1
203                 , "r4"
204 #endif  /* CONFIG_CHIP_M32700_TS1 */
205         );
206         local_irq_restore(flags);
207
208         if (unlikely(count <= 0))
209                 __up(sem);
210 }
211
212 #endif  /* __KERNEL__ */
213
214 #endif  /* _ASM_M32R_SEMAPHORE_H */