2 * arch/ubicom32/include/asm/semaphore.h
3 * Interrupt-safe semaphores for Ubicom32 architecture.
5 * (C) Copyright 2009, Ubicom, Inc.
6 * (C) Copyright 1996 Linus Torvalds
7 * m68k version by Andreas Schwab
8 * Copyright (C) 2004 Microtronix Datacom Ltd
10 * This file is part of the Ubicom32 Linux Kernel Port.
12 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
13 * it and/or modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 2 of the
15 * License, or (at your option) any later version.
17 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
18 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
20 * the GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with the Ubicom32 Linux Kernel Port. If not,
24 * see <http://www.gnu.org/licenses/>.
26 * Ubicom32 implementation derived from (with many thanks):
31 #ifndef _ASM_UBICOM32_SEMAPHORE_H
32 #define _ASM_UBICOM32_SEMAPHORE_H
34 #define RW_LOCK_BIAS 0x01000000
38 #include <linux/linkage.h>
39 #include <linux/wait.h>
40 #include <linux/spinlock.h>
41 #include <linux/rwsem.h>
43 #include <asm/system.h>
44 #include <asm/atomic.h>
49 wait_queue_head_t wait;
52 #define __SEMAPHORE_INITIALIZER(name, n) \
54 .count = ATOMIC_INIT(n), \
55 .waking = ATOMIC_INIT(0), \
56 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
59 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
60 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
62 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
63 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
65 static inline void sema_init (struct semaphore *sem, int val)
67 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
70 static inline void init_MUTEX (struct semaphore *sem)
75 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
80 asmlinkage void __down_failed(void /* special register calling convention */);
81 asmlinkage int __down_failed_interruptible(void /* params in registers */);
82 asmlinkage int __down_failed_trylock(void /* params in registers */);
83 asmlinkage void __up_wakeup(void /* special register calling convention */);
85 asmlinkage void __down(struct semaphore * sem);
86 asmlinkage int __down_interruptible(struct semaphore * sem);
87 asmlinkage int __down_trylock(struct semaphore * sem);
88 asmlinkage void __up(struct semaphore * sem);
90 extern spinlock_t semaphore_wake_lock;
93 * This is ugly, but we want the default case to fall through.
94 * "down_failed" is a special asm handler that calls the C
95 * routine that actually waits.
97 static inline void down(struct semaphore * sem)
101 if (atomic_dec_return(&sem->count) < 0)
105 static inline int down_interruptible(struct semaphore * sem)
112 if(atomic_dec_return(&sem->count) < 0)
113 ret = __down_interruptible(sem);
117 static inline int down_trylock(struct semaphore * sem)
121 if (atomic_dec_return (&sem->count) < 0)
122 ret = __down_trylock(sem);
127 * Note! This is subtle. We jump to wake people up only if
128 * the semaphore was negative (== somebody was waiting on it).
129 * The default case (no contention) will result in NO
130 * jumps for both down() and up().
132 static inline void up(struct semaphore * sem)
134 if (atomic_inc_return(&sem->count) <= 0)
138 #endif /* __ASSEMBLY__ */
140 #endif /* _ASM_UBICOM32_SEMAPHORE_H */