skge: NAPI/irq race fix
[linux-2.6/libata-dev.git] / include / asm-m32r / semaphore.h
blobbf447c52a0a126c8a26719885d5e081f73c3c728
1 #ifndef _ASM_M32R_SEMAPHORE_H
2 #define _ASM_M32R_SEMAPHORE_H
4 #include <linux/linkage.h>
6 #ifdef __KERNEL__
8 /*
9 * SMP- and interrupt-safe semaphores..
11 * Copyright (C) 1996 Linus Torvalds
12 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
15 #include <linux/config.h>
16 #include <linux/wait.h>
17 #include <linux/rwsem.h>
18 #include <asm/assembler.h>
19 #include <asm/system.h>
20 #include <asm/atomic.h>
22 struct semaphore {
23 atomic_t count;
24 int sleepers;
25 wait_queue_head_t wait;
28 #define __SEMAPHORE_INITIALIZER(name, n) \
29 { \
30 .count = ATOMIC_INIT(n), \
31 .sleepers = 0, \
32 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
35 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
36 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
38 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
39 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
41 static inline void sema_init (struct semaphore *sem, int val)
44 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
46 * i'd rather use the more flexible initialization above, but sadly
47 * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
49 atomic_set(&sem->count, val);
50 sem->sleepers = 0;
51 init_waitqueue_head(&sem->wait);
54 static inline void init_MUTEX (struct semaphore *sem)
56 sema_init(sem, 1);
59 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
61 sema_init(sem, 0);
64 asmlinkage void __down_failed(void /* special register calling convention */);
65 asmlinkage int __down_failed_interruptible(void /* params in registers */);
66 asmlinkage int __down_failed_trylock(void /* params in registers */);
67 asmlinkage void __up_wakeup(void /* special register calling convention */);
69 asmlinkage void __down(struct semaphore * sem);
70 asmlinkage int __down_interruptible(struct semaphore * sem);
71 asmlinkage int __down_trylock(struct semaphore * sem);
72 asmlinkage void __up(struct semaphore * sem);
75 * Atomically decrement the semaphore's count. If it goes negative,
76 * block the calling thread in the TASK_UNINTERRUPTIBLE state.
78 static inline void down(struct semaphore * sem)
80 unsigned long flags;
81 long count;
83 might_sleep();
84 local_irq_save(flags);
85 __asm__ __volatile__ (
86 "# down \n\t"
87 DCACHE_CLEAR("%0", "r4", "%1")
88 M32R_LOCK" %0, @%1; \n\t"
89 "addi %0, #-1; \n\t"
90 M32R_UNLOCK" %0, @%1; \n\t"
91 : "=&r" (count)
92 : "r" (&sem->count)
93 : "memory"
94 #ifdef CONFIG_CHIP_M32700_TS1
95 , "r4"
96 #endif /* CONFIG_CHIP_M32700_TS1 */
98 local_irq_restore(flags);
100 if (unlikely(count < 0))
101 __down(sem);
105 * Interruptible try to acquire a semaphore. If we obtained
106 * it, return zero. If we were interrupted, returns -EINTR
108 static inline int down_interruptible(struct semaphore * sem)
110 unsigned long flags;
111 long count;
112 int result = 0;
114 might_sleep();
115 local_irq_save(flags);
116 __asm__ __volatile__ (
117 "# down_interruptible \n\t"
118 DCACHE_CLEAR("%0", "r4", "%1")
119 M32R_LOCK" %0, @%1; \n\t"
120 "addi %0, #-1; \n\t"
121 M32R_UNLOCK" %0, @%1; \n\t"
122 : "=&r" (count)
123 : "r" (&sem->count)
124 : "memory"
125 #ifdef CONFIG_CHIP_M32700_TS1
126 , "r4"
127 #endif /* CONFIG_CHIP_M32700_TS1 */
129 local_irq_restore(flags);
131 if (unlikely(count < 0))
132 result = __down_interruptible(sem);
134 return result;
138 * Non-blockingly attempt to down() a semaphore.
139 * Returns zero if we acquired it
141 static inline int down_trylock(struct semaphore * sem)
143 unsigned long flags;
144 long count;
145 int result = 0;
147 local_irq_save(flags);
148 __asm__ __volatile__ (
149 "# down_trylock \n\t"
150 DCACHE_CLEAR("%0", "r4", "%1")
151 M32R_LOCK" %0, @%1; \n\t"
152 "addi %0, #-1; \n\t"
153 M32R_UNLOCK" %0, @%1; \n\t"
154 : "=&r" (count)
155 : "r" (&sem->count)
156 : "memory"
157 #ifdef CONFIG_CHIP_M32700_TS1
158 , "r4"
159 #endif /* CONFIG_CHIP_M32700_TS1 */
161 local_irq_restore(flags);
163 if (unlikely(count < 0))
164 result = __down_trylock(sem);
166 return result;
170 * Note! This is subtle. We jump to wake people up only if
171 * the semaphore was negative (== somebody was waiting on it).
172 * The default case (no contention) will result in NO
173 * jumps for both down() and up().
175 static inline void up(struct semaphore * sem)
177 unsigned long flags;
178 long count;
180 local_irq_save(flags);
181 __asm__ __volatile__ (
182 "# up \n\t"
183 DCACHE_CLEAR("%0", "r4", "%1")
184 M32R_LOCK" %0, @%1; \n\t"
185 "addi %0, #1; \n\t"
186 M32R_UNLOCK" %0, @%1; \n\t"
187 : "=&r" (count)
188 : "r" (&sem->count)
189 : "memory"
190 #ifdef CONFIG_CHIP_M32700_TS1
191 , "r4"
192 #endif /* CONFIG_CHIP_M32700_TS1 */
194 local_irq_restore(flags);
196 if (unlikely(count <= 0))
197 __up(sem);
200 #endif /* __KERNEL__ */
202 #endif /* _ASM_M32R_SEMAPHORE_H */