upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / mips / kernel / semaphore.c
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/config.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/init.h>
22 #include <asm/atomic.h>
23 #include <asm/cpu-features.h>
24 #include <asm/errno.h>
25 #include <asm/semaphore.h>
26 #include <asm/war.h>
27 /*
28  * Atomically update sem->count.
29  * This does the equivalent of the following:
30  *
31  *      old_count = sem->count;
32  *      tmp = MAX(old_count, 0) + incr;
33  *      sem->count = tmp;
34  *      return old_count;
35  *
36  * On machines without lld/scd we need a spinlock to make the manipulation of
37  * sem->count and sem->waking atomic.  Scalability isn't an issue because
38  * this lock is used on UP only so it's just an empty variable.
39  */
40 static inline int __sem_update_count(struct semaphore *sem, int incr)
41 {
42         int old_count, tmp;
43
44         if (cpu_has_llsc && R10000_LLSC_WAR) {
45                 __asm__ __volatile__(
46                 "1:     ll      %0, %2                                  \n"
47                 "       sra     %1, %0, 31                              \n"
48                 "       not     %1                                      \n"
49                 "       and     %1, %0, %1                              \n"
50                 "       add     %1, %1, %3                              \n"
51                 "       sc      %1, %2                                  \n"
52                 "       beqzl   %1, 1b                                  \n"
53                 : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
54                 : "r" (incr), "m" (sem->count));
55         } else if (cpu_has_llsc) {
56                 __asm__ __volatile__(
57                 "1:     ll      %0, %2                                  \n"
58                 "       sra     %1, %0, 31                              \n"
59                 "       not     %1                                      \n"
60                 "       and     %1, %0, %1                              \n"
61                 "       add     %1, %1, %3                              \n"
62                 "       sc      %1, %2                                  \n"
63                 "       beqz    %1, 1b                                  \n"
64                 : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
65                 : "r" (incr), "m" (sem->count));
66         } else {
67                 static spinlock_t semaphore_lock = SPIN_LOCK_UNLOCKED;
68                 unsigned long flags;
69
70                 spin_lock_irqsave(&semaphore_lock, flags);
71                 old_count = atomic_read(&sem->count);
72                 tmp = max_t(int, old_count, 0) + incr;
73                 atomic_set(&sem->count, tmp);
74                 spin_unlock_irqrestore(&semaphore_lock, flags);
75         }
76
77         return old_count;
78 }
79
80 void __up(struct semaphore *sem)
81 {
82         /*
83          * Note that we incremented count in up() before we came here,
84          * but that was ineffective since the result was <= 0, and
85          * any negative value of count is equivalent to 0.
86          * This ends up setting count to 1, unless count is now > 0
87          * (i.e. because some other cpu has called up() in the meantime),
88          * in which case we just increment count.
89          */
90         __sem_update_count(sem, 1);
91         wake_up(&sem->wait);
92 }
93
94 EXPORT_SYMBOL(__up);
95
96 /*
97  * Note that when we come in to __down or __down_interruptible,
98  * we have already decremented count, but that decrement was
99  * ineffective since the result was < 0, and any negative value
100  * of count is equivalent to 0.
101  * Thus it is only when we decrement count from some value > 0
102  * that we have actually got the semaphore.
103  */
104 void __sched __down(struct semaphore *sem)
105 {
106         struct task_struct *tsk = current;
107         DECLARE_WAITQUEUE(wait, tsk);
108
109         __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
110         add_wait_queue_exclusive(&sem->wait, &wait);
111
112         /*
113          * Try to get the semaphore.  If the count is > 0, then we've
114          * got the semaphore; we decrement count and exit the loop.
115          * If the count is 0 or negative, we set it to -1, indicating
116          * that we are asleep, and then sleep.
117          */
118         while (__sem_update_count(sem, -1) <= 0) {
119                 schedule();
120                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
121         }
122         remove_wait_queue(&sem->wait, &wait);
123         __set_task_state(tsk, TASK_RUNNING);
124
125         /*
126          * If there are any more sleepers, wake one of them up so
127          * that it can either get the semaphore, or set count to -1
128          * indicating that there are still processes sleeping.
129          */
130         wake_up(&sem->wait);
131 }
132
133 EXPORT_SYMBOL(__down);
134
135 int __sched __down_interruptible(struct semaphore * sem)
136 {
137         int retval = 0;
138         struct task_struct *tsk = current;
139         DECLARE_WAITQUEUE(wait, tsk);
140
141         __set_task_state(tsk, TASK_INTERRUPTIBLE);
142         add_wait_queue_exclusive(&sem->wait, &wait);
143
144         while (__sem_update_count(sem, -1) <= 0) {
145                 if (signal_pending(current)) {
146                         /*
147                          * A signal is pending - give up trying.
148                          * Set sem->count to 0 if it is negative,
149                          * since we are no longer sleeping.
150                          */
151                         __sem_update_count(sem, 0);
152                         retval = -EINTR;
153                         break;
154                 }
155                 schedule();
156                 set_task_state(tsk, TASK_INTERRUPTIBLE);
157         }
158         remove_wait_queue(&sem->wait, &wait);
159         __set_task_state(tsk, TASK_RUNNING);
160
161         wake_up(&sem->wait);
162         return retval;
163 }
164
165 EXPORT_SYMBOL(__down_interruptible);