1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29
30
31
32
33
34
35
36
37
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 " .set mips3 \n"
46 "1: ll %0, %2 # __sem_update_count \n"
47 " sra %1, %0, 31 \n"
48 " not %1 \n"
49 " and %1, %0, %1 \n"
50 " addu %1, %1, %3 \n"
51 " sc %1, %2 \n"
52 " beqzl %1, 1b \n"
53 " .set mips0 \n"
54 : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
55 : "r" (incr), "m" (sem->count));
56 } else if (cpu_has_llsc) {
57 __asm__ __volatile__(
58 " .set mips3 \n"
59 "1: ll %0, %2 # __sem_update_count \n"
60 " sra %1, %0, 31 \n"
61 " not %1 \n"
62 " and %1, %0, %1 \n"
63 " addu %1, %1, %3 \n"
64 " sc %1, %2 \n"
65 " beqz %1, 1b \n"
66 " .set mips0 \n"
67 : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
68 : "r" (incr), "m" (sem->count));
69 } else {
70 static DEFINE_SPINLOCK(semaphore_lock);
71 unsigned long flags;
72
73 spin_lock_irqsave(&semaphore_lock, flags);
74 old_count = atomic_read(&sem->count);
75 tmp = max_t(int, old_count, 0) + incr;
76 atomic_set(&sem->count, tmp);
77 spin_unlock_irqrestore(&semaphore_lock, flags);
78 }
79
80 return old_count;
81}
82
83void __up(struct semaphore *sem)
84{
85
86
87
88
89
90
91
92
93 __sem_update_count(sem, 1);
94 wake_up(&sem->wait);
95}
96
97EXPORT_SYMBOL(__up);
98
99
100
101
102
103
104
105
106
107void __sched __down(struct semaphore *sem)
108{
109 struct task_struct *tsk = current;
110 DECLARE_WAITQUEUE(wait, tsk);
111
112 __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
113 add_wait_queue_exclusive(&sem->wait, &wait);
114
115
116
117
118
119
120
121 while (__sem_update_count(sem, -1) <= 0) {
122 schedule();
123 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
124 }
125 remove_wait_queue(&sem->wait, &wait);
126 __set_task_state(tsk, TASK_RUNNING);
127
128
129
130
131
132
133 wake_up(&sem->wait);
134}
135
136EXPORT_SYMBOL(__down);
137
138int __sched __down_interruptible(struct semaphore * sem)
139{
140 int retval = 0;
141 struct task_struct *tsk = current;
142 DECLARE_WAITQUEUE(wait, tsk);
143
144 __set_task_state(tsk, TASK_INTERRUPTIBLE);
145 add_wait_queue_exclusive(&sem->wait, &wait);
146
147 while (__sem_update_count(sem, -1) <= 0) {
148 if (signal_pending(current)) {
149
150
151
152
153
154 __sem_update_count(sem, 0);
155 retval = -EINTR;
156 break;
157 }
158 schedule();
159 set_task_state(tsk, TASK_INTERRUPTIBLE);
160 }
161 remove_wait_queue(&sem->wait, &wait);
162 __set_task_state(tsk, TASK_RUNNING);
163
164 wake_up(&sem->wait);
165 return retval;
166}
167
168EXPORT_SYMBOL(__down_interruptible);
169