aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips/kernel/semaphore.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/mips/kernel/semaphore.c')
-rw-r--r--arch/mips/kernel/semaphore.c164
1 files changed, 164 insertions, 0 deletions
diff --git a/arch/mips/kernel/semaphore.c b/arch/mips/kernel/semaphore.c
new file mode 100644
index 000000000000..9c40fe5a8e8d
--- /dev/null
+++ b/arch/mips/kernel/semaphore.c
@@ -0,0 +1,164 @@
1/*
2 * MIPS-specific semaphore code.
3 *
4 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
5 * Copyright (C) 2004 Ralf Baechle <ralf@linux-mips.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * April 2001 - Reworked by Paul Mackerras <paulus@samba.org>
13 * to eliminate the SMP races in the old version between the updates
14 * of `count' and `waking'. Now we use negative `count' values to
15 * indicate that some process(es) are waiting for the semaphore.
16 */
17
18#include <linux/module.h>
19#include <linux/sched.h>
20#include <linux/init.h>
21#include <asm/atomic.h>
22#include <asm/cpu-features.h>
23#include <asm/errno.h>
24#include <asm/semaphore.h>
25#include <asm/war.h>
26/*
27 * Atomically update sem->count.
28 * This does the equivalent of the following:
29 *
30 * old_count = sem->count;
31 * tmp = MAX(old_count, 0) + incr;
32 * sem->count = tmp;
33 * return old_count;
34 *
35 * On machines without lld/scd we need a spinlock to make the manipulation of
36 * sem->count and sem->waking atomic. Scalability isn't an issue because
37 * this lock is used on UP only so it's just an empty variable.
38 */
39static inline int __sem_update_count(struct semaphore *sem, int incr)
40{
41 int old_count, tmp;
42
43 if (cpu_has_llsc && R10000_LLSC_WAR) {
44 __asm__ __volatile__(
45 "1: ll %0, %2 \n"
46 " sra %1, %0, 31 \n"
47 " not %1 \n"
48 " and %1, %0, %1 \n"
49 " add %1, %1, %3 \n"
50 " sc %1, %2 \n"
51 " beqzl %1, 1b \n"
52 : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
53 : "r" (incr), "m" (sem->count));
54 } else if (cpu_has_llsc) {
55 __asm__ __volatile__(
56 "1: ll %0, %2 \n"
57 " sra %1, %0, 31 \n"
58 " not %1 \n"
59 " and %1, %0, %1 \n"
60 " add %1, %1, %3 \n"
61 " sc %1, %2 \n"
62 " beqz %1, 1b \n"
63 : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
64 : "r" (incr), "m" (sem->count));
65 } else {
66 static DEFINE_SPINLOCK(semaphore_lock);
67 unsigned long flags;
68
69 spin_lock_irqsave(&semaphore_lock, flags);
70 old_count = atomic_read(&sem->count);
71 tmp = max_t(int, old_count, 0) + incr;
72 atomic_set(&sem->count, tmp);
73 spin_unlock_irqrestore(&semaphore_lock, flags);
74 }
75
76 return old_count;
77}
78
79void __up(struct semaphore *sem)
80{
81 /*
82 * Note that we incremented count in up() before we came here,
83 * but that was ineffective since the result was <= 0, and
84 * any negative value of count is equivalent to 0.
85 * This ends up setting count to 1, unless count is now > 0
86 * (i.e. because some other cpu has called up() in the meantime),
87 * in which case we just increment count.
88 */
89 __sem_update_count(sem, 1);
90 wake_up(&sem->wait);
91}
92
93EXPORT_SYMBOL(__up);
94
95/*
96 * Note that when we come in to __down or __down_interruptible,
97 * we have already decremented count, but that decrement was
98 * ineffective since the result was < 0, and any negative value
99 * of count is equivalent to 0.
100 * Thus it is only when we decrement count from some value > 0
101 * that we have actually got the semaphore.
102 */
103void __sched __down(struct semaphore *sem)
104{
105 struct task_struct *tsk = current;
106 DECLARE_WAITQUEUE(wait, tsk);
107
108 __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
109 add_wait_queue_exclusive(&sem->wait, &wait);
110
111 /*
112 * Try to get the semaphore. If the count is > 0, then we've
113 * got the semaphore; we decrement count and exit the loop.
114 * If the count is 0 or negative, we set it to -1, indicating
115 * that we are asleep, and then sleep.
116 */
117 while (__sem_update_count(sem, -1) <= 0) {
118 schedule();
119 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
120 }
121 remove_wait_queue(&sem->wait, &wait);
122 __set_task_state(tsk, TASK_RUNNING);
123
124 /*
125 * If there are any more sleepers, wake one of them up so
126 * that it can either get the semaphore, or set count to -1
127 * indicating that there are still processes sleeping.
128 */
129 wake_up(&sem->wait);
130}
131
132EXPORT_SYMBOL(__down);
133
134int __sched __down_interruptible(struct semaphore * sem)
135{
136 int retval = 0;
137 struct task_struct *tsk = current;
138 DECLARE_WAITQUEUE(wait, tsk);
139
140 __set_task_state(tsk, TASK_INTERRUPTIBLE);
141 add_wait_queue_exclusive(&sem->wait, &wait);
142
143 while (__sem_update_count(sem, -1) <= 0) {
144 if (signal_pending(current)) {
145 /*
146 * A signal is pending - give up trying.
147 * Set sem->count to 0 if it is negative,
148 * since we are no longer sleeping.
149 */
150 __sem_update_count(sem, 0);
151 retval = -EINTR;
152 break;
153 }
154 schedule();
155 set_task_state(tsk, TASK_INTERRUPTIBLE);
156 }
157 remove_wait_queue(&sem->wait, &wait);
158 __set_task_state(tsk, TASK_RUNNING);
159
160 wake_up(&sem->wait);
161 return retval;
162}
163
164EXPORT_SYMBOL(__down_interruptible);