atomic.h 7.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright IBM Corp. 1999, 2009
3 4 5
 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
 *	      Denis Joseph Barrow,
 *	      Arnd Bergmann <arndb@de.ibm.com>,
L
Linus Torvalds 已提交
6
 *
7 8
 * Atomic operations that C can't guarantee us.
 * Useful for resource counting etc.
L
Lucas De Marchi 已提交
9
 * s390 uses 'Compare And Swap' for atomicity in SMP environment.
L
Linus Torvalds 已提交
10 11 12
 *
 */

13 14 15
#ifndef __ARCH_S390_ATOMIC__
#define __ARCH_S390_ATOMIC__

16 17
#include <linux/compiler.h>
#include <linux/types.h>
18
#include <asm/barrier.h>
19
#include <asm/cmpxchg.h>
L
Linus Torvalds 已提交
20 21 22

#define ATOMIC_INIT(i)  { (i) }

23 24
#define __ATOMIC_NO_BARRIER	"\n"

25 26 27 28 29
#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES

#define __ATOMIC_OR	"lao"
#define __ATOMIC_AND	"lan"
#define __ATOMIC_ADD	"laa"
30
#define __ATOMIC_BARRIER "bcr	14,0\n"
31

32
#define __ATOMIC_LOOP(ptr, op_val, op_string, __barrier)		\
33 34
({									\
	int old_val;							\
35 36
									\
	typecheck(atomic_t *, ptr);					\
37
	asm volatile(							\
38
		__barrier						\
39
		op_string "	%0,%2,%1\n"				\
40
		__barrier						\
41
		: "=d" (old_val), "+Q" ((ptr)->counter)			\
42 43 44 45 46 47 48 49 50 51
		: "d" (op_val)						\
		: "cc", "memory");					\
	old_val;							\
})

#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */

#define __ATOMIC_OR	"or"
#define __ATOMIC_AND	"nr"
#define __ATOMIC_ADD	"ar"
52
#define __ATOMIC_BARRIER "\n"
53

54
#define __ATOMIC_LOOP(ptr, op_val, op_string, __barrier)		\
55
({									\
56
	int old_val, new_val;						\
57 58
									\
	typecheck(atomic_t *, ptr);					\
59 60 61 62 63 64
	asm volatile(							\
		"	l	%0,%2\n"				\
		"0:	lr	%1,%0\n"				\
		op_string "	%1,%3\n"				\
		"	cs	%0,%1,%2\n"				\
		"	jl	0b"					\
65 66
		: "=&d" (old_val), "=&d" (new_val), "+Q" ((ptr)->counter)\
		: "d" (op_val)						\
67
		: "cc", "memory");					\
68
	old_val;							\
L
Linus Torvalds 已提交
69
})
70

71 72
#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */

73 74
static inline int atomic_read(const atomic_t *v)
{
H
Heiko Carstens 已提交
75 76 77 78 79 80
	int c;

	asm volatile(
		"	l	%0,%1\n"
		: "=d" (c) : "Q" (v->counter));
	return c;
81 82 83 84
}

static inline void atomic_set(atomic_t *v, int i)
{
H
Heiko Carstens 已提交
85 86 87
	asm volatile(
		"	st	%1,%0\n"
		: "=Q" (v->counter) : "d" (i));
88
}
L
Linus Torvalds 已提交
89

90
static inline int atomic_add_return(int i, atomic_t *v)
L
Linus Torvalds 已提交
91
{
92
	return __ATOMIC_LOOP(v, i, __ATOMIC_ADD, __ATOMIC_BARRIER) + i;
L
Linus Torvalds 已提交
93
}
94

95 96 97 98 99 100 101 102 103
static inline void atomic_add(int i, atomic_t *v)
{
#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
	if (__builtin_constant_p(i) && (i > -129) && (i < 128)) {
		asm volatile(
			"asi	%0,%1\n"
			: "+Q" (v->counter)
			: "i" (i)
			: "cc", "memory");
104
		return;
105 106
	}
#endif
107
	__ATOMIC_LOOP(v, i, __ATOMIC_ADD, __ATOMIC_NO_BARRIER);
108 109
}

110
#define atomic_add_negative(_i, _v)	(atomic_add_return(_i, _v) < 0)
111
#define atomic_inc(_v)			atomic_add(1, _v)
112 113
#define atomic_inc_return(_v)		atomic_add_return(1, _v)
#define atomic_inc_and_test(_v)		(atomic_add_return(1, _v) == 0)
114
#define atomic_sub(_i, _v)		atomic_add(-(int)(_i), _v)
115
#define atomic_sub_return(_i, _v)	atomic_add_return(-(int)(_i), _v)
116
#define atomic_sub_and_test(_i, _v)	(atomic_sub_return(_i, _v) == 0)
117
#define atomic_dec(_v)			atomic_sub(1, _v)
118 119
#define atomic_dec_return(_v)		atomic_sub_return(1, _v)
#define atomic_dec_and_test(_v)		(atomic_sub_return(1, _v) == 0)
L
Linus Torvalds 已提交
120

121
static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
L
Linus Torvalds 已提交
122
{
123
	__ATOMIC_LOOP(v, ~mask, __ATOMIC_AND, __ATOMIC_NO_BARRIER);
L
Linus Torvalds 已提交
124
}
125

126
static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
L
Linus Torvalds 已提交
127
{
128
	__ATOMIC_LOOP(v, mask, __ATOMIC_OR, __ATOMIC_NO_BARRIER);
L
Linus Torvalds 已提交
129
}
130

131 132
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))

133
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
134
{
135 136
	asm volatile(
		"	cs	%0,%2,%1"
137 138
		: "+d" (old), "+Q" (v->counter)
		: "d" (new)
139
		: "cc", "memory");
140 141 142
	return old;
}

143
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
144 145 146
{
	int c, old;
	c = atomic_read(v);
147 148 149 150 151 152
	for (;;) {
		if (unlikely(c == u))
			break;
		old = atomic_cmpxchg(v, c, c + a);
		if (likely(old == c))
			break;
153
		c = old;
154
	}
155
	return c;
156 157 158
}


159
#undef __ATOMIC_LOOP
L
Linus Torvalds 已提交
160 161 162

#define ATOMIC64_INIT(i)  { (i) }

163 164
#define __ATOMIC64_NO_BARRIER	"\n"

165 166 167 168 169
#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES

#define __ATOMIC64_OR	"laog"
#define __ATOMIC64_AND	"lang"
#define __ATOMIC64_ADD	"laag"
170
#define __ATOMIC64_BARRIER "bcr	14,0\n"
171

172
#define __ATOMIC64_LOOP(ptr, op_val, op_string, __barrier)		\
173 174
({									\
	long long old_val;						\
175 176
									\
	typecheck(atomic64_t *, ptr);					\
177
	asm volatile(							\
178
		__barrier						\
179
		op_string "	%0,%2,%1\n"				\
180
		__barrier						\
181
		: "=d" (old_val), "+Q" ((ptr)->counter)			\
182 183 184 185 186 187 188 189 190 191
		: "d" (op_val)						\
		: "cc", "memory");					\
	old_val;							\
})

#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */

#define __ATOMIC64_OR	"ogr"
#define __ATOMIC64_AND	"ngr"
#define __ATOMIC64_ADD	"agr"
192
#define __ATOMIC64_BARRIER "\n"
193

194
#define __ATOMIC64_LOOP(ptr, op_val, op_string, __barrier)		\
195
({									\
196
	long long old_val, new_val;					\
197 198
									\
	typecheck(atomic64_t *, ptr);					\
199 200 201 202 203 204
	asm volatile(							\
		"	lg	%0,%2\n"				\
		"0:	lgr	%1,%0\n"				\
		op_string "	%1,%3\n"				\
		"	csg	%0,%1,%2\n"				\
		"	jl	0b"					\
205 206
		: "=&d" (old_val), "=&d" (new_val), "+Q" ((ptr)->counter)\
		: "d" (op_val)						\
207
		: "cc", "memory");					\
208
	old_val;							\
L
Linus Torvalds 已提交
209
})
210

211 212
#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */

213 214
static inline long long atomic64_read(const atomic64_t *v)
{
H
Heiko Carstens 已提交
215 216 217 218 219 220
	long long c;

	asm volatile(
		"	lg	%0,%1\n"
		: "=d" (c) : "Q" (v->counter));
	return c;
221 222 223 224
}

static inline void atomic64_set(atomic64_t *v, long long i)
{
H
Heiko Carstens 已提交
225 226 227
	asm volatile(
		"	stg	%1,%0\n"
		: "=Q" (v->counter) : "d" (i));
228
}
L
Linus Torvalds 已提交
229

230
static inline long long atomic64_add_return(long long i, atomic64_t *v)
L
Linus Torvalds 已提交
231
{
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	return __ATOMIC64_LOOP(v, i, __ATOMIC64_ADD, __ATOMIC64_BARRIER) + i;
}

static inline void atomic64_add(long long i, atomic64_t *v)
{
#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
	if (__builtin_constant_p(i) && (i > -129) && (i < 128)) {
		asm volatile(
			"agsi	%0,%1\n"
			: "+Q" (v->counter)
			: "i" (i)
			: "cc", "memory");
		return;
	}
#endif
	__ATOMIC64_LOOP(v, i, __ATOMIC64_ADD, __ATOMIC64_NO_BARRIER);
L
Linus Torvalds 已提交
248
}
249

250
static inline void atomic64_clear_mask(unsigned long mask, atomic64_t *v)
L
Linus Torvalds 已提交
251
{
252
	__ATOMIC64_LOOP(v, ~mask, __ATOMIC64_AND, __ATOMIC64_NO_BARRIER);
L
Linus Torvalds 已提交
253
}
254

255
static inline void atomic64_set_mask(unsigned long mask, atomic64_t *v)
L
Linus Torvalds 已提交
256
{
257
	__ATOMIC64_LOOP(v, mask, __ATOMIC64_OR, __ATOMIC64_NO_BARRIER);
L
Linus Torvalds 已提交
258 259
}

260 261
#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))

262
static inline long long atomic64_cmpxchg(atomic64_t *v,
263 264
					     long long old, long long new)
{
265 266
	asm volatile(
		"	csg	%0,%2,%1"
267 268
		: "+d" (old), "+Q" (v->counter)
		: "d" (new)
269
		: "cc", "memory");
270 271
	return old;
}
L
Linus Torvalds 已提交
272

273
#undef __ATOMIC64_LOOP
274

275
static inline int atomic64_add_unless(atomic64_t *v, long long i, long long u)
L
Linus Torvalds 已提交
276
{
277
	long long c, old;
278

279
	c = atomic64_read(v);
280 281 282
	for (;;) {
		if (unlikely(c == u))
			break;
283
		old = atomic64_cmpxchg(v, c, c + i);
284 285
		if (likely(old == c))
			break;
286
		c = old;
287
	}
288
	return c != u;
L
Linus Torvalds 已提交
289 290
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static inline long long atomic64_dec_if_positive(atomic64_t *v)
{
	long long c, old, dec;

	c = atomic64_read(v);
	for (;;) {
		dec = c - 1;
		if (unlikely(dec < 0))
			break;
		old = atomic64_cmpxchg((v), c, dec);
		if (likely(old == c))
			break;
		c = old;
	}
	return dec;
}

308
#define atomic64_add_negative(_i, _v)	(atomic64_add_return(_i, _v) < 0)
309
#define atomic64_inc(_v)		atomic64_add(1, _v)
310 311
#define atomic64_inc_return(_v)		atomic64_add_return(1, _v)
#define atomic64_inc_and_test(_v)	(atomic64_add_return(1, _v) == 0)
312
#define atomic64_sub_return(_i, _v)	atomic64_add_return(-(long long)(_i), _v)
313
#define atomic64_sub(_i, _v)		atomic64_add(-(long long)(_i), _v)
314
#define atomic64_sub_and_test(_i, _v)	(atomic64_sub_return(_i, _v) == 0)
315
#define atomic64_dec(_v)		atomic64_sub(1, _v)
316 317 318
#define atomic64_dec_return(_v)		atomic64_sub_return(1, _v)
#define atomic64_dec_and_test(_v)	(atomic64_sub_return(1, _v) == 0)
#define atomic64_inc_not_zero(v)	atomic64_add_unless((v), 1, 0)
N
Nick Piggin 已提交
319

L
Linus Torvalds 已提交
320
#endif /* __ARCH_S390_ATOMIC__  */