ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-m68k / semaphore.h
1 #ifndef _M68K_SEMAPHORE_H
2 #define _M68K_SEMAPHORE_H
3
4 #define RW_LOCK_BIAS             0x01000000
5
6 #ifndef __ASSEMBLY__
7
8 #include <linux/linkage.h>
9 #include <linux/wait.h>
10 #include <linux/spinlock.h>
11 #include <linux/rwsem.h>
12 #include <linux/stringify.h>
13
14 #include <asm/system.h>
15 #include <asm/atomic.h>
16
17 /*
18  * Interrupt-safe semaphores..
19  *
20  * (C) Copyright 1996 Linus Torvalds
21  *
22  * m68k version by Andreas Schwab
23  */
24
25
26 struct semaphore {
27         atomic_t count;
28         atomic_t waking;
29         wait_queue_head_t wait;
30 #if WAITQUEUE_DEBUG
31         long __magic;
32 #endif
33 };
34
35 #if WAITQUEUE_DEBUG
36 # define __SEM_DEBUG_INIT(name) \
37                 , (long)&(name).__magic
38 #else
39 # define __SEM_DEBUG_INIT(name)
40 #endif
41
42 #define __SEMAPHORE_INITIALIZER(name,count) \
43 { ATOMIC_INIT(count), ATOMIC_INIT(0), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
44         __SEM_DEBUG_INIT(name) }
45
46 #define __MUTEX_INITIALIZER(name) \
47         __SEMAPHORE_INITIALIZER(name,1)
48
49 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
50         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
51
52 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
53 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
54
55 static inline void sema_init(struct semaphore *sem, int val)
56 {
57         *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
58 }
59
60 static inline void init_MUTEX (struct semaphore *sem)
61 {
62         sema_init(sem, 1);
63 }
64
65 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
66 {
67         sema_init(sem, 0);
68 }
69
70 asmlinkage void __down_failed(void /* special register calling convention */);
71 asmlinkage int  __down_failed_interruptible(void  /* params in registers */);
72 asmlinkage int  __down_failed_trylock(void  /* params in registers */);
73 asmlinkage void __up_wakeup(void /* special register calling convention */);
74
75 asmlinkage void __down(struct semaphore * sem);
76 asmlinkage int  __down_interruptible(struct semaphore * sem);
77 asmlinkage int  __down_trylock(struct semaphore * sem);
78 asmlinkage void __up(struct semaphore * sem);
79
80 /*
81  * This is ugly, but we want the default case to fall through.
82  * "down_failed" is a special asm handler that calls the C
83  * routine that actually waits. See arch/m68k/lib/semaphore.S
84  */
85 static inline void down(struct semaphore *sem)
86 {
87         register struct semaphore *sem1 __asm__ ("%a1") = sem;
88
89 #if WAITQUEUE_DEBUG
90         CHECK_MAGIC(sem->__magic);
91 #endif
92         might_sleep();
93         __asm__ __volatile__(
94                 "| atomic down operation\n\t"
95                 "subql #1,%0@\n\t"
96                 "jmi 2f\n\t"
97                 "1:\n"
98                 LOCK_SECTION_START(".even\n\t")
99                 "2:\tpea 1b\n\t"
100                 "jbra __down_failed\n"
101                 LOCK_SECTION_END
102                 : /* no outputs */
103                 : "a" (sem1)
104                 : "memory");
105 }
106
107 static inline int down_interruptible(struct semaphore *sem)
108 {
109         register struct semaphore *sem1 __asm__ ("%a1") = sem;
110         register int result __asm__ ("%d0");
111
112 #if WAITQUEUE_DEBUG
113         CHECK_MAGIC(sem->__magic);
114 #endif
115         might_sleep();
116         __asm__ __volatile__(
117                 "| atomic interruptible down operation\n\t"
118                 "subql #1,%1@\n\t"
119                 "jmi 2f\n\t"
120                 "clrl %0\n"
121                 "1:\n"
122                 LOCK_SECTION_START(".even\n\t")
123                 "2:\tpea 1b\n\t"
124                 "jbra __down_failed_interruptible\n"
125                 LOCK_SECTION_END
126                 : "=d" (result)
127                 : "a" (sem1)
128                 : "memory");
129         return result;
130 }
131
132 static inline int down_trylock(struct semaphore *sem)
133 {
134         register struct semaphore *sem1 __asm__ ("%a1") = sem;
135         register int result __asm__ ("%d0");
136
137 #if WAITQUEUE_DEBUG
138         CHECK_MAGIC(sem->__magic);
139 #endif
140
141         __asm__ __volatile__(
142                 "| atomic down trylock operation\n\t"
143                 "subql #1,%1@\n\t"
144                 "jmi 2f\n\t"
145                 "clrl %0\n"
146                 "1:\n"
147                 LOCK_SECTION_START(".even\n\t")
148                 "2:\tpea 1b\n\t"
149                 "jbra __down_failed_trylock\n"
150                 LOCK_SECTION_END
151                 : "=d" (result)
152                 : "a" (sem1)
153                 : "memory");
154         return result;
155 }
156
157 /*
158  * Note! This is subtle. We jump to wake people up only if
159  * the semaphore was negative (== somebody was waiting on it).
160  * The default case (no contention) will result in NO
161  * jumps for both down() and up().
162  */
163 static inline void up(struct semaphore *sem)
164 {
165         register struct semaphore *sem1 __asm__ ("%a1") = sem;
166
167 #if WAITQUEUE_DEBUG
168         CHECK_MAGIC(sem->__magic);
169 #endif
170
171         __asm__ __volatile__(
172                 "| atomic up operation\n\t"
173                 "addql #1,%0@\n\t"
174                 "jle 2f\n"
175                 "1:\n"
176                 LOCK_SECTION_START(".even\n\t")
177                 "2:\t"
178                 "pea 1b\n\t"
179                 "jbra __up_wakeup\n"
180                 LOCK_SECTION_END
181                 : /* no outputs */
182                 : "a" (sem1)
183                 : "memory");
184 }
185
186 #endif /* __ASSEMBLY__ */
187
188 #endif