1/*
2 * BK Id: SCCS/s.semaphore.c 1.12 05/17/01 18:14:22 cort
3 */
4/*
5 * PowerPC-specific semaphore code.
6 *
7 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * April 2001 - Reworked by Paul Mackerras <paulus@samba.org>
15 * to eliminate the SMP races in the old version between the updates
16 * of `count' and `waking'.  Now we use negative `count' values to
17 * indicate that some process(es) are waiting for the semaphore.
18 */
19
20#include <linux/sched.h>
21#include <asm/atomic.h>
22#include <asm/semaphore.h>
23
24/*
25 * Atomically update sem->count.
26 * This does the equivalent of the following:
27 *
28 *	old_count = sem->count;
29 *	tmp = MAX(old_count, 0) + incr;
30 *	sem->count = tmp;
31 *	return old_count;
32 */
33static inline int __sem_update_count(struct semaphore *sem, int incr)
34{
35	int old_count, tmp;
36
37	__asm__ __volatile__("\n"
38"1:	lwarx	%0,0,%3\n"
39"	srawi	%1,%0,31\n"
40"	andc	%1,%0,%1\n"
41"	add	%1,%1,%4\n"
42"	stwcx.	%1,0,%3\n"
43"	bne	1b"
44	: "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
45	: "r" (&sem->count), "r" (incr), "m" (sem->count)
46	: "cc");
47
48	return old_count;
49}
50
51void __up(struct semaphore *sem)
52{
53	/*
54	 * Note that we incremented count in up() before we came here,
55	 * but that was ineffective since the result was <= 0, and
56	 * any negative value of count is equivalent to 0.
57	 * This ends up setting count to 1, unless count is now > 0
58	 * (i.e. because some other cpu has called up() in the meantime),
59	 * in which case we just increment count.
60	 */
61	__sem_update_count(sem, 1);
62	wake_up(&sem->wait);
63}
64
65/*
66 * Note that when we come in to __down or __down_interruptible,
67 * we have already decremented count, but that decrement was
68 * ineffective since the result was < 0, and any negative value
69 * of count is equivalent to 0.
70 * Thus it is only when we decrement count from some value > 0
71 * that we have actually got the semaphore.
72 */
73void __down(struct semaphore *sem)
74{
75	struct task_struct *tsk = current;
76	DECLARE_WAITQUEUE(wait, tsk);
77
78	tsk->state = TASK_UNINTERRUPTIBLE;
79	add_wait_queue_exclusive(&sem->wait, &wait);
80	smp_wmb();
81
82	/*
83	 * Try to get the semaphore.  If the count is > 0, then we've
84	 * got the semaphore; we decrement count and exit the loop.
85	 * If the count is 0 or negative, we set it to -1, indicating
86	 * that we are asleep, and then sleep.
87	 */
88	while (__sem_update_count(sem, -1) <= 0) {
89		schedule();
90		tsk->state = TASK_UNINTERRUPTIBLE;
91	}
92	remove_wait_queue(&sem->wait, &wait);
93	tsk->state = TASK_RUNNING;
94
95	/*
96	 * If there are any more sleepers, wake one of them up so
97	 * that it can either get the semaphore, or set count to -1
98	 * indicating that there are still processes sleeping.
99	 */
100	wake_up(&sem->wait);
101}
102
103int __down_interruptible(struct semaphore * sem)
104{
105	int retval = 0;
106	struct task_struct *tsk = current;
107	DECLARE_WAITQUEUE(wait, tsk);
108
109	tsk->state = TASK_INTERRUPTIBLE;
110	add_wait_queue_exclusive(&sem->wait, &wait);
111	smp_wmb();
112
113	while (__sem_update_count(sem, -1) <= 0) {
114		if (signal_pending(current)) {
115			/*
116			 * A signal is pending - give up trying.
117			 * Set sem->count to 0 if it is negative,
118			 * since we are no longer sleeping.
119			 */
120			__sem_update_count(sem, 0);
121			retval = -EINTR;
122			break;
123		}
124		schedule();
125		tsk->state = TASK_INTERRUPTIBLE;
126	}
127	tsk->state = TASK_RUNNING;
128	remove_wait_queue(&sem->wait, &wait);
129	wake_up(&sem->wait);
130	return retval;
131}
132