| 1 | /* |
| 2 | * arch/ubicom32/include/asm/semaphore.h |
| 3 | * Interrupt-safe semaphores for Ubicom32 architecture. |
| 4 | * |
| 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 |
| 9 | * |
| 10 | * This file is part of the Ubicom32 Linux Kernel Port. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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/>. |
| 25 | * |
| 26 | * Ubicom32 implementation derived from (with many thanks): |
| 27 | * arch/m68knommu |
| 28 | * arch/blackfin |
| 29 | * arch/parisc |
| 30 | */ |
| 31 | #ifndef _ASM_UBICOM32_SEMAPHORE_H |
| 32 | #define _ASM_UBICOM32_SEMAPHORE_H |
| 33 | |
| 34 | #define RW_LOCK_BIAS 0x01000000 |
| 35 | |
| 36 | #ifndef __ASSEMBLY__ |
| 37 | |
| 38 | #include <linux/linkage.h> |
| 39 | #include <linux/wait.h> |
| 40 | #include <linux/spinlock.h> |
| 41 | #include <linux/rwsem.h> |
| 42 | |
| 43 | #include <asm/system.h> |
| 44 | #include <asm/atomic.h> |
| 45 | |
| 46 | struct semaphore { |
| 47 | atomic_t count; |
| 48 | atomic_t waking; |
| 49 | wait_queue_head_t wait; |
| 50 | }; |
| 51 | |
| 52 | #define __SEMAPHORE_INITIALIZER(name, n) \ |
| 53 | { \ |
| 54 | .count = ATOMIC_INIT(n), \ |
| 55 | .waking = ATOMIC_INIT(0), \ |
| 56 | .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ |
| 57 | } |
| 58 | |
| 59 | #define __DECLARE_SEMAPHORE_GENERIC(name,count) \ |
| 60 | struct semaphore name = __SEMAPHORE_INITIALIZER(name,count) |
| 61 | |
| 62 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1) |
| 63 | #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0) |
| 64 | |
| 65 | static inline void sema_init (struct semaphore *sem, int val) |
| 66 | { |
| 67 | *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val); |
| 68 | } |
| 69 | |
| 70 | static inline void init_MUTEX (struct semaphore *sem) |
| 71 | { |
| 72 | sema_init(sem, 1); |
| 73 | } |
| 74 | |
| 75 | static inline void init_MUTEX_LOCKED (struct semaphore *sem) |
| 76 | { |
| 77 | sema_init(sem, 0); |
| 78 | } |
| 79 | |
| 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 */); |
| 84 | |
| 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); |
| 89 | |
| 90 | extern spinlock_t semaphore_wake_lock; |
| 91 | |
| 92 | /* |
| 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. |
| 96 | */ |
| 97 | static inline void down(struct semaphore * sem) |
| 98 | { |
| 99 | might_sleep(); |
| 100 | |
| 101 | if (atomic_dec_return(&sem->count) < 0) |
| 102 | __down(sem); |
| 103 | } |
| 104 | |
| 105 | static inline int down_interruptible(struct semaphore * sem) |
| 106 | { |
| 107 | int ret = 0; |
| 108 | |
| 109 | |
| 110 | might_sleep(); |
| 111 | |
| 112 | if(atomic_dec_return(&sem->count) < 0) |
| 113 | ret = __down_interruptible(sem); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static inline int down_trylock(struct semaphore * sem) |
| 118 | { |
| 119 | int ret = 0; |
| 120 | |
| 121 | if (atomic_dec_return (&sem->count) < 0) |
| 122 | ret = __down_trylock(sem); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | /* |
| 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(). |
| 131 | */ |
| 132 | static inline void up(struct semaphore * sem) |
| 133 | { |
| 134 | if (atomic_inc_return(&sem->count) <= 0) |
| 135 | __up(sem); |
| 136 | } |
| 137 | |
| 138 | #endif /* __ASSEMBLY__ */ |
| 139 | |
| 140 | #endif /* _ASM_UBICOM32_SEMAPHORE_H */ |
| 141 | |