1 #ifndef _ASM_M32R_SEMAPHORE_H
2 #define _ASM_M32R_SEMAPHORE_H
4 #include <linux/linkage.h>
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/system.h>
19 #include <asm/atomic.h>
25 #define STORE "unlock"
34 wait_queue_head_t wait;
37 #define __SEMAPHORE_INITIALIZER(name, n) \
39 .count = ATOMIC_INIT(n), \
41 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
44 #define __MUTEX_INITIALIZER(name) \
45 __SEMAPHORE_INITIALIZER(name,1)
47 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
48 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
50 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
51 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
53 static inline void sema_init (struct semaphore *sem, int val)
56 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
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.
61 atomic_set(&sem->count, val);
63 init_waitqueue_head(&sem->wait);
66 static inline void init_MUTEX (struct semaphore *sem)
71 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
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 */);
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);
87 * Atomically decrement the semaphore's count. If it goes negative,
88 * block the calling thread in the TASK_UNINTERRUPTIBLE state.
90 static inline void down(struct semaphore * sem)
96 local_irq_save(flags);
97 __asm__ __volatile__ (
99 DCACHE_CLEAR("%0", "r4", "%1")
102 STORE" %0, @%1; \n\t"
106 #ifdef CONFIG_CHIP_M32700_TS1
108 #endif /* CONFIG_CHIP_M32700_TS1 */
110 local_irq_restore(flags);
112 if (unlikely(count < 0))
117 * Interruptible try to acquire a semaphore. If we obtained
118 * it, return zero. If we were interrupted, returns -EINTR
120 static inline int down_interruptible(struct semaphore * sem)
127 local_irq_save(flags);
128 __asm__ __volatile__ (
129 "# down_interruptible \n\t"
130 DCACHE_CLEAR("%0", "r4", "%1")
133 STORE" %0, @%1; \n\t"
137 #ifdef CONFIG_CHIP_M32700_TS1
139 #endif /* CONFIG_CHIP_M32700_TS1 */
141 local_irq_restore(flags);
143 if (unlikely(count < 0))
144 result = __down_interruptible(sem);
150 * Non-blockingly attempt to down() a semaphore.
151 * Returns zero if we acquired it
153 static inline int down_trylock(struct semaphore * sem)
159 local_irq_save(flags);
160 __asm__ __volatile__ (
161 "# down_trylock \n\t"
162 DCACHE_CLEAR("%0", "r4", "%1")
165 STORE" %0, @%1; \n\t"
169 #ifdef CONFIG_CHIP_M32700_TS1
171 #endif /* CONFIG_CHIP_M32700_TS1 */
173 local_irq_restore(flags);
175 if (unlikely(count < 0))
176 result = __down_trylock(sem);
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().
187 static inline void up(struct semaphore * sem)
192 local_irq_save(flags);
193 __asm__ __volatile__ (
195 DCACHE_CLEAR("%0", "r4", "%1")
198 STORE" %0, @%1; \n\t"
202 #ifdef CONFIG_CHIP_M32700_TS1
204 #endif /* CONFIG_CHIP_M32700_TS1 */
206 local_irq_restore(flags);
208 if (unlikely(count <= 0))
212 #endif /* __KERNEL__ */
214 #endif /* _ASM_M32R_SEMAPHORE_H */