semaphore.c 5.68 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/* $Id: semaphore.c,v 1.9 2001/11/18 00:12:56 davem Exp $
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6
 * semaphore.c: Sparc64 semaphore implementation.
 *
 * This is basically the PPC semaphore scheme ported to use
 * the sparc64 atomic instructions, so see the PPC code for
 * credits.
Linus Torvalds's avatar
Linus Torvalds committed
7 8 9
 */

#include <linux/sched.h>
10
#include <linux/errno.h>
Linus Torvalds's avatar
Linus Torvalds committed
11 12

/*
Linus Torvalds's avatar
Linus Torvalds committed
13 14
 * Atomically update sem->count.
 * This does the equivalent of the following:
Linus Torvalds's avatar
Linus Torvalds committed
15
 *
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19
 *	old_count = sem->count;
 *	tmp = MAX(old_count, 0) + incr;
 *	sem->count = tmp;
 *	return old_count;
Linus Torvalds's avatar
Linus Torvalds committed
20
 */
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34
static __inline__ int __sem_update_count(struct semaphore *sem, int incr)
{
	int old_count, tmp;

	__asm__ __volatile__("\n"
"	! __sem_update_count old_count(%0) tmp(%1) incr(%4) &sem->count(%3)\n"
"1:	ldsw	[%3], %0\n"
"	mov	%0, %1\n"
"	cmp	%0, 0\n"
"	movl	%%icc, 0, %1\n"
"	add	%1, %4, %1\n"
"	cas	[%3], %0, %1\n"
"	cmp	%0, %1\n"
"	bne,pn	%%icc, 1b\n"
Linus Torvalds's avatar
Linus Torvalds committed
35
"	 membar #StoreLoad | #StoreStore\n"
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38 39 40 41 42
	: "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
	: "r" (&sem->count), "r" (incr), "m" (sem->count)
	: "cc");

	return old_count;
}

43
static void __up(struct semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
44
{
Linus Torvalds's avatar
Linus Torvalds committed
45
	__sem_update_count(sem, 1);
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48
	wake_up(&sem->wait);
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
void up(struct semaphore *sem)
{
	/* This atomically does:
	 * 	old_val = sem->count;
	 *	new_val = sem->count + 1;
	 *	sem->count = new_val;
	 *	if (old_val < 0)
	 *		__up(sem);
	 *
	 * The (old_val < 0) test is equivalent to
	 * the more straightforward (new_val <= 0),
	 * but it is easier to test the former because
	 * of how the CAS instruction works.
	 */

	__asm__ __volatile__("\n"
"	! up sem(%0)\n"
"	membar	#StoreLoad | #LoadLoad\n"
"1:	lduw	[%0], %%g5\n"
"	add	%%g5, 1, %%g7\n"
"	cas	[%0], %%g5, %%g7\n"
"	cmp	%%g5, %%g7\n"
"	bne,pn	%%icc, 1b\n"
"	 addcc	%%g7, 1, %%g0\n"
"	ble,pn	%%icc, 3f\n"
"	 membar	#StoreLoad | #StoreStore\n"
"2:\n"
"	.subsection 2\n"
"3:	mov	%0, %%g5\n"
"	save	%%sp, -160, %%sp\n"
"	mov	%%g1, %%l1\n"
"	mov	%%g2, %%l2\n"
"	mov	%%g3, %%l3\n"
"	call	%1\n"
"	 mov	%%g5, %%o0\n"
"	mov	%%l1, %%g1\n"
"	mov	%%l2, %%g2\n"
"	ba,pt	%%xcc, 2b\n"
"	 restore %%l3, %%g0, %%g3\n"
"	.previous\n"
	: : "r" (sem), "i" (__up)
	: "g5", "g7", "memory", "cc");
}

static void __down(struct semaphore * sem)
Linus Torvalds's avatar
Linus Torvalds committed
94 95
{
	struct task_struct *tsk = current;
Linus Torvalds's avatar
Linus Torvalds committed
96 97
	DECLARE_WAITQUEUE(wait, tsk);

Linus Torvalds's avatar
Linus Torvalds committed
98 99
	tsk->state = TASK_UNINTERRUPTIBLE;
	add_wait_queue_exclusive(&sem->wait, &wait);
Linus Torvalds's avatar
Linus Torvalds committed
100

Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104
	while (__sem_update_count(sem, -1) <= 0) {
		schedule();
		tsk->state = TASK_UNINTERRUPTIBLE;
	}
Linus Torvalds's avatar
Linus Torvalds committed
105
	remove_wait_queue(&sem->wait, &wait);
Linus Torvalds's avatar
Linus Torvalds committed
106
	tsk->state = TASK_RUNNING;
Linus Torvalds's avatar
Linus Torvalds committed
107

Linus Torvalds's avatar
Linus Torvalds committed
108
	wake_up(&sem->wait);
Linus Torvalds's avatar
Linus Torvalds committed
109 110
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
void down(struct semaphore *sem)
{
	/* This atomically does:
	 * 	old_val = sem->count;
	 *	new_val = sem->count - 1;
	 *	sem->count = new_val;
	 *	if (old_val < 1)
	 *		__down(sem);
	 *
	 * The (old_val < 1) test is equivalent to
	 * the more straightforward (new_val < 0),
	 * but it is easier to test the former because
	 * of how the CAS instruction works.
	 */

	__asm__ __volatile__("\n"
"	! down sem(%0)\n"
"1:	lduw	[%0], %%g5\n"
"	sub	%%g5, 1, %%g7\n"
"	cas	[%0], %%g5, %%g7\n"
"	cmp	%%g5, %%g7\n"
"	bne,pn	%%icc, 1b\n"
"	 cmp	%%g7, 1\n"
"	bl,pn	%%icc, 3f\n"
"	 membar	#StoreLoad | #StoreStore\n"
"2:\n"
"	.subsection 2\n"
"3:	mov	%0, %%g5\n"
"	save	%%sp, -160, %%sp\n"
"	mov	%%g1, %%l1\n"
"	mov	%%g2, %%l2\n"
"	mov	%%g3, %%l3\n"
"	call	%1\n"
"	 mov	%%g5, %%o0\n"
"	mov	%%l1, %%g1\n"
"	mov	%%l2, %%g2\n"
"	ba,pt	%%xcc, 2b\n"
"	 restore %%l3, %%g0, %%g3\n"
"	.previous\n"
	: : "r" (sem), "i" (__down)
	: "g5", "g7", "memory", "cc");
}

int down_trylock(struct semaphore *sem)
{
	int ret;

	/* This atomically does:
	 * 	old_val = sem->count;
	 *	new_val = sem->count - 1;
	 *	if (old_val < 1) {
	 *		ret = 1;
	 *	} else {
	 *		sem->count = new_val;
	 *		ret = 0;
	 *	}
	 *
	 * The (old_val < 1) test is equivalent to
	 * the more straightforward (new_val < 0),
	 * but it is easier to test the former because
	 * of how the CAS instruction works.
	 */

	__asm__ __volatile__("\n"
"	! down_trylock sem(%1) ret(%0)\n"
"1:	lduw	[%1], %%g5\n"
"	sub	%%g5, 1, %%g7\n"
"	cmp	%%g5, 1\n"
"	bl,pn	%%icc, 2f\n"
"	 mov	1, %0\n"
"	cas	[%1], %%g5, %%g7\n"
"	cmp	%%g5, %%g7\n"
"	bne,pn	%%icc, 1b\n"
"	 mov	0, %0\n"
"	membar	#StoreLoad | #StoreStore\n"
"2:\n"
	: "=&r" (ret)
	: "r" (sem)
	: "g5", "g7", "memory", "cc");

	return ret;
}

static int __down_interruptible(struct semaphore * sem)
Linus Torvalds's avatar
Linus Torvalds committed
195
{
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198
	int retval = 0;
	struct task_struct *tsk = current;
	DECLARE_WAITQUEUE(wait, tsk);
Linus Torvalds's avatar
Linus Torvalds committed
199

Linus Torvalds's avatar
Linus Torvalds committed
200 201
	tsk->state = TASK_INTERRUPTIBLE;
	add_wait_queue_exclusive(&sem->wait, &wait);
Linus Torvalds's avatar
Linus Torvalds committed
202

Linus Torvalds's avatar
Linus Torvalds committed
203 204 205 206 207 208 209 210 211 212 213 214 215
	while (__sem_update_count(sem, -1) <= 0) {
		if (signal_pending(current)) {
			__sem_update_count(sem, 0);
			retval = -EINTR;
			break;
		}
		schedule();
		tsk->state = TASK_INTERRUPTIBLE;
	}
	tsk->state = TASK_RUNNING;
	remove_wait_queue(&sem->wait, &wait);
	wake_up(&sem->wait);
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
216
}
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

int down_interruptible(struct semaphore *sem)
{
	int ret = 0;
	
	/* This atomically does:
	 * 	old_val = sem->count;
	 *	new_val = sem->count - 1;
	 *	sem->count = new_val;
	 *	if (old_val < 1)
	 *		ret = __down_interruptible(sem);
	 *
	 * The (old_val < 1) test is equivalent to
	 * the more straightforward (new_val < 0),
	 * but it is easier to test the former because
	 * of how the CAS instruction works.
	 */

	__asm__ __volatile__("\n"
"	! down_interruptible sem(%2) ret(%0)\n"
"1:	lduw	[%2], %%g5\n"
"	sub	%%g5, 1, %%g7\n"
"	cas	[%2], %%g5, %%g7\n"
"	cmp	%%g5, %%g7\n"
"	bne,pn	%%icc, 1b\n"
"	 cmp	%%g7, 1\n"
"	bl,pn	%%icc, 3f\n"
"	 membar	#StoreLoad | #StoreStore\n"
"2:\n"
"	.subsection 2\n"
"3:	mov	%2, %%g5\n"
"	save	%%sp, -160, %%sp\n"
"	mov	%%g1, %%l1\n"
"	mov	%%g2, %%l2\n"
"	mov	%%g3, %%l3\n"
"	call	%3\n"
"	 mov	%%g5, %%o0\n"
"	mov	%%l1, %%g1\n"
"	mov	%%l2, %%g2\n"
"	mov	%%l3, %%g3\n"
"	ba,pt	%%xcc, 2b\n"
"	 restore %%o0, %%g0, %0\n"
"	.previous\n"
	: "=r" (ret)
	: "0" (ret), "r" (sem), "i" (__down_interruptible)
	: "g5", "g7", "memory", "cc");
	return ret;
}