summaryrefslogtreecommitdiffstats
path: root/include/asm-ppc/semaphore-helper.h
blob: 3033272d9731af8d30f2a6c5f06c2272eb8961dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef _PPC_SEMAPHORE_HELPER_H
#define _PPC_SEMAPHORE_HELPER_H

/*
 * SMP- and interrupt-safe semaphores..
 *
 * (C) Copyright 1996 Linus Torvalds
 * Adapted for PowerPC by Gary Thomas and Paul Mackerras
 */

#include <asm/atomic.h>

/*
 * These two (wake_one_more and waking_non_zero) _must_ execute
 * atomically wrt each other.
 *
 * This is trivially done with load with reservation and
 * store conditional on the ppc.
 */

static inline void wake_one_more(struct semaphore * sem)
{
	atomic_inc(&sem->waking);
}

static inline int waking_non_zero(struct semaphore *sem)
{
	int ret, tmp;

	/* Atomic decrement sem->waking iff it is > 0 */
	__asm__ __volatile__(
		"1:	lwarx %1,0,%2\n"	/* tmp = sem->waking */
		"	cmpwi 0,%1,0\n"		/* test tmp */
		"	addic %1,%1,-1\n"	/* --tmp */
		"	ble- 2f\n"		/* exit if tmp was <= 0 */
		"	stwcx. %1,0,%2\n"	/* update sem->waking */
		"	bne- 1b\n"		/* try again if update failed*/
		"	li %0,1\n"		/* ret = 1 */
		"2:"
		: "=r" (ret), "=&r" (tmp)
		: "r" (&sem->waking), "0" (0)
		: "cr0", "memory");

	return ret;
}
/*
 * waking_non_zero_interruptible:
 *	1	got the lock
 *	0	go to sleep
 *	-EINTR	interrupted
 */
static inline int waking_non_zero_interruptible(struct semaphore *sem,
						struct task_struct *tsk)
{
	int ret, tmp;

	/* Atomic decrement sem->waking iff it is > 0 */
	__asm__ __volatile__(
		"1:	lwarx %1,0,%2\n"	/* tmp = sem->waking */
		"	cmpwi 0,%1,0\n"		/* test tmp */
		"	addic %1,%1,-1\n"	/* --tmp */
		"	ble- 2f\n"		/* exit if tmp was <= 0 */
		"	stwcx. %1,0,%2\n"	/* update sem->waking */
		"	bne- 1b\n"		/* try again if update failed*/
		"	li %0,1\n"		/* ret = 1 */
		"2:"
		: "=r" (ret), "=&r" (tmp)
		: "r" (&sem->waking), "0" (0)
		: "cr0", "memory");

	if (ret == 0 && signal_pending(tsk)) {
		atomic_inc(&sem->count);
		ret = -EINTR;
	}
	return ret;
}

/*
 * waking_non_zero_trylock:
 *	1	failed to lock
 *	0	got the lock
 */
static inline int waking_non_zero_trylock(struct semaphore *sem)
{
	int ret, tmp;

	/* Atomic decrement sem->waking iff it is > 0 */
	__asm__ __volatile__(
		"1:	lwarx %1,0,%2\n"	/* tmp = sem->waking */
		"	cmpwi 0,%1,0\n"		/* test tmp */
		"	addic %1,%1,-1\n"	/* --tmp */
		"	ble- 2f\n"		/* exit if tmp was <= 0 */
		"	stwcx. %1,0,%2\n"	/* update sem->waking */
		"	bne- 1b\n"		/* try again if update failed*/
		"	li %0,0\n"		/* ret = 0 */
		"2:"
		: "=r" (ret), "=&r" (tmp)
		: "r" (&sem->waking), "0" (1)
		: "cr0", "memory");

	if (ret)
		atomic_inc(&sem->count);

	return ret;
}

#endif /* _PPC_SEMAPHORE_HELPER_H */