atomic.h 10.2 KB
Newer Older
1 2 3
#ifndef _ASM_POWERPC_ATOMIC_H_
#define _ASM_POWERPC_ATOMIC_H_

L
Linus Torvalds 已提交
4 5 6 7 8
/*
 * PowerPC atomic operations
 */

#ifdef __KERNEL__
9 10
#include <linux/types.h>
#include <asm/cmpxchg.h>
11
#include <asm/barrier.h>
L
Linus Torvalds 已提交
12

13
#define ATOMIC_INIT(i)		{ (i) }
L
Linus Torvalds 已提交
14

15 16 17 18 19 20 21 22 23 24 25 26 27
static __inline__ int atomic_read(const atomic_t *v)
{
	int t;

	__asm__ __volatile__("lwz%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));

	return t;
}

static __inline__ void atomic_set(atomic_t *v, int i)
{
	__asm__ __volatile__("stw%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
}
L
Linus Torvalds 已提交
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
#define ATOMIC_OP(op, asm_op)						\
static __inline__ void atomic_##op(int a, atomic_t *v)			\
{									\
	int t;								\
									\
	__asm__ __volatile__(						\
"1:	lwarx	%0,0,%3		# atomic_" #op "\n"			\
	#asm_op " %0,%2,%0\n"						\
	PPC405_ERR77(0,%3)						\
"	stwcx.	%0,0,%3 \n"						\
"	bne-	1b\n"							\
	: "=&r" (t), "+m" (v->counter)					\
	: "r" (a), "r" (&v->counter)					\
	: "cc");							\
}									\

#define ATOMIC_OP_RETURN(op, asm_op)					\
static __inline__ int atomic_##op##_return(int a, atomic_t *v)		\
{									\
	int t;								\
									\
	__asm__ __volatile__(						\
	PPC_ATOMIC_ENTRY_BARRIER					\
"1:	lwarx	%0,0,%2		# atomic_" #op "_return\n"		\
	#asm_op " %0,%1,%0\n"						\
	PPC405_ERR77(0,%2)						\
"	stwcx.	%0,0,%2 \n"						\
"	bne-	1b\n"							\
	PPC_ATOMIC_EXIT_BARRIER						\
	: "=&r" (t)							\
	: "r" (a), "r" (&v->counter)					\
	: "cc", "memory");						\
									\
	return t;							\
L
Linus Torvalds 已提交
63 64
}

65
#define ATOMIC_OPS(op, asm_op) ATOMIC_OP(op, asm_op) ATOMIC_OP_RETURN(op, asm_op)
L
Linus Torvalds 已提交
66

67 68
ATOMIC_OPS(add, add)
ATOMIC_OPS(sub, subf)
L
Linus Torvalds 已提交
69

70 71 72 73
ATOMIC_OP(and, and)
ATOMIC_OP(or, or)
ATOMIC_OP(xor, xor)

74 75 76
#undef ATOMIC_OPS
#undef ATOMIC_OP_RETURN
#undef ATOMIC_OP
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89

#define atomic_add_negative(a, v)	(atomic_add_return((a), (v)) < 0)

static __inline__ void atomic_inc(atomic_t *v)
{
	int t;

	__asm__ __volatile__(
"1:	lwarx	%0,0,%2		# atomic_inc\n\
	addic	%0,%0,1\n"
	PPC405_ERR77(0,%2)
"	stwcx.	%0,0,%2 \n\
	bne-	1b"
90 91
	: "=&r" (t), "+m" (v->counter)
	: "r" (&v->counter)
92
	: "cc", "xer");
L
Linus Torvalds 已提交
93 94 95 96 97 98 99
}

static __inline__ int atomic_inc_return(atomic_t *v)
{
	int t;

	__asm__ __volatile__(
100
	PPC_ATOMIC_ENTRY_BARRIER
L
Linus Torvalds 已提交
101 102 103 104 105
"1:	lwarx	%0,0,%1		# atomic_inc_return\n\
	addic	%0,%0,1\n"
	PPC405_ERR77(0,%1)
"	stwcx.	%0,0,%1 \n\
	bne-	1b"
106
	PPC_ATOMIC_EXIT_BARRIER
L
Linus Torvalds 已提交
107 108
	: "=&r" (t)
	: "r" (&v->counter)
109
	: "cc", "xer", "memory");
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	return t;
}

/*
 * atomic_inc_and_test - increment and test
 * @v: pointer of type atomic_t
 *
 * Atomically increments @v by 1
 * and returns true if the result is zero, or false for all
 * other cases.
 */
#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)

static __inline__ void atomic_dec(atomic_t *v)
{
	int t;

	__asm__ __volatile__(
"1:	lwarx	%0,0,%2		# atomic_dec\n\
	addic	%0,%0,-1\n"
	PPC405_ERR77(0,%2)\
"	stwcx.	%0,0,%2\n\
	bne-	1b"
134 135
	: "=&r" (t), "+m" (v->counter)
	: "r" (&v->counter)
136
	: "cc", "xer");
L
Linus Torvalds 已提交
137 138 139 140 141 142 143
}

static __inline__ int atomic_dec_return(atomic_t *v)
{
	int t;

	__asm__ __volatile__(
144
	PPC_ATOMIC_ENTRY_BARRIER
L
Linus Torvalds 已提交
145 146 147 148 149
"1:	lwarx	%0,0,%1		# atomic_dec_return\n\
	addic	%0,%0,-1\n"
	PPC405_ERR77(0,%1)
"	stwcx.	%0,0,%1\n\
	bne-	1b"
150
	PPC_ATOMIC_EXIT_BARRIER
L
Linus Torvalds 已提交
151 152
	: "=&r" (t)
	: "r" (&v->counter)
153
	: "cc", "xer", "memory");
L
Linus Torvalds 已提交
154 155 156 157

	return t;
}

158
#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
159
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
N
Nick Piggin 已提交
160

N
Nick Piggin 已提交
161
/**
162
 * __atomic_add_unless - add unless the number is a given value
N
Nick Piggin 已提交
163 164 165 166 167
 * @v: pointer of type atomic_t
 * @a: the amount to add to v...
 * @u: ...unless v is equal to u.
 *
 * Atomically adds @a to @v, so long as it was not @u.
168
 * Returns the old value of @v.
N
Nick Piggin 已提交
169
 */
170
static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
171 172 173 174
{
	int t;

	__asm__ __volatile__ (
175
	PPC_ATOMIC_ENTRY_BARRIER
176
"1:	lwarx	%0,0,%1		# __atomic_add_unless\n\
177 178 179 180 181 182
	cmpw	0,%0,%3 \n\
	beq-	2f \n\
	add	%0,%2,%0 \n"
	PPC405_ERR77(0,%2)
"	stwcx.	%0,0,%1 \n\
	bne-	1b \n"
183
	PPC_ATOMIC_EXIT_BARRIER
184 185 186 187 188 189
"	subf	%0,%2,%0 \n\
2:"
	: "=&r" (t)
	: "r" (&v->counter), "r" (a), "r" (u)
	: "cc", "memory");

190
	return t;
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
/**
 * atomic_inc_not_zero - increment unless the number is zero
 * @v: pointer of type atomic_t
 *
 * Atomically increments @v by 1, so long as @v is non-zero.
 * Returns non-zero if @v was non-zero, and zero otherwise.
 */
static __inline__ int atomic_inc_not_zero(atomic_t *v)
{
	int t1, t2;

	__asm__ __volatile__ (
	PPC_ATOMIC_ENTRY_BARRIER
"1:	lwarx	%0,0,%2		# atomic_inc_not_zero\n\
	cmpwi	0,%0,0\n\
	beq-	2f\n\
	addic	%1,%0,1\n"
	PPC405_ERR77(0,%2)
"	stwcx.	%1,0,%2\n\
	bne-	1b\n"
	PPC_ATOMIC_EXIT_BARRIER
	"\n\
2:"
	: "=&r" (t1), "=&r" (t2)
	: "r" (&v->counter)
	: "cc", "xer", "memory");

	return t1;
}
#define atomic_inc_not_zero(v) atomic_inc_not_zero((v))
N
Nick Piggin 已提交
223

L
Linus Torvalds 已提交
224 225 226 227 228
#define atomic_sub_and_test(a, v)	(atomic_sub_return((a), (v)) == 0)
#define atomic_dec_and_test(v)		(atomic_dec_return((v)) == 0)

/*
 * Atomically test *v and decrement if it is greater than 0.
229 230
 * The function returns the old value of *v minus 1, even if
 * the atomic variable, v, was not decremented.
L
Linus Torvalds 已提交
231 232 233 234 235 236
 */
static __inline__ int atomic_dec_if_positive(atomic_t *v)
{
	int t;

	__asm__ __volatile__(
237
	PPC_ATOMIC_ENTRY_BARRIER
L
Linus Torvalds 已提交
238
"1:	lwarx	%0,0,%1		# atomic_dec_if_positive\n\
239 240
	cmpwi	%0,1\n\
	addi	%0,%0,-1\n\
L
Linus Torvalds 已提交
241 242 243 244
	blt-	2f\n"
	PPC405_ERR77(0,%1)
"	stwcx.	%0,0,%1\n\
	bne-	1b"
245
	PPC_ATOMIC_EXIT_BARRIER
L
Linus Torvalds 已提交
246
	"\n\
247
2:"	: "=&b" (t)
L
Linus Torvalds 已提交
248 249 250 251 252
	: "r" (&v->counter)
	: "cc", "memory");

	return t;
}
253
#define atomic_dec_if_positive atomic_dec_if_positive
L
Linus Torvalds 已提交
254

255 256 257 258
#ifdef __powerpc64__

#define ATOMIC64_INIT(i)	{ (i) }

259 260 261 262 263 264 265 266 267 268 269 270 271
static __inline__ long atomic64_read(const atomic64_t *v)
{
	long t;

	__asm__ __volatile__("ld%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));

	return t;
}

static __inline__ void atomic64_set(atomic64_t *v, long i)
{
	__asm__ __volatile__("std%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
}
272

273 274 275 276 277 278 279 280 281 282 283 284 285
#define ATOMIC64_OP(op, asm_op)						\
static __inline__ void atomic64_##op(long a, atomic64_t *v)		\
{									\
	long t;								\
									\
	__asm__ __volatile__(						\
"1:	ldarx	%0,0,%3		# atomic64_" #op "\n"			\
	#asm_op " %0,%2,%0\n"						\
"	stdcx.	%0,0,%3 \n"						\
"	bne-	1b\n"							\
	: "=&r" (t), "+m" (v->counter)					\
	: "r" (a), "r" (&v->counter)					\
	: "cc");							\
286 287
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
#define ATOMIC64_OP_RETURN(op, asm_op)					\
static __inline__ long atomic64_##op##_return(long a, atomic64_t *v)	\
{									\
	long t;								\
									\
	__asm__ __volatile__(						\
	PPC_ATOMIC_ENTRY_BARRIER					\
"1:	ldarx	%0,0,%2		# atomic64_" #op "_return\n"		\
	#asm_op " %0,%1,%0\n"						\
"	stdcx.	%0,0,%2 \n"						\
"	bne-	1b\n"							\
	PPC_ATOMIC_EXIT_BARRIER						\
	: "=&r" (t)							\
	: "r" (a), "r" (&v->counter)					\
	: "cc", "memory");						\
									\
	return t;							\
305 306
}

307
#define ATOMIC64_OPS(op, asm_op) ATOMIC64_OP(op, asm_op) ATOMIC64_OP_RETURN(op, asm_op)
308

309 310
ATOMIC64_OPS(add, add)
ATOMIC64_OPS(sub, subf)
311 312 313
ATOMIC64_OP(and, and)
ATOMIC64_OP(or, or)
ATOMIC64_OP(xor, xor)
314

315 316 317
#undef ATOMIC64_OPS
#undef ATOMIC64_OP_RETURN
#undef ATOMIC64_OP
318

319
#define atomic64_add_negative(a, v)	(atomic64_add_return((a), (v)) < 0)
320 321 322 323 324 325 326 327 328 329

static __inline__ void atomic64_inc(atomic64_t *v)
{
	long t;

	__asm__ __volatile__(
"1:	ldarx	%0,0,%2		# atomic64_inc\n\
	addic	%0,%0,1\n\
	stdcx.	%0,0,%2 \n\
	bne-	1b"
330 331
	: "=&r" (t), "+m" (v->counter)
	: "r" (&v->counter)
332
	: "cc", "xer");
333 334 335 336 337 338 339
}

static __inline__ long atomic64_inc_return(atomic64_t *v)
{
	long t;

	__asm__ __volatile__(
340
	PPC_ATOMIC_ENTRY_BARRIER
341 342 343 344
"1:	ldarx	%0,0,%1		# atomic64_inc_return\n\
	addic	%0,%0,1\n\
	stdcx.	%0,0,%1 \n\
	bne-	1b"
345
	PPC_ATOMIC_EXIT_BARRIER
346 347
	: "=&r" (t)
	: "r" (&v->counter)
348
	: "cc", "xer", "memory");
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

	return t;
}

/*
 * atomic64_inc_and_test - increment and test
 * @v: pointer of type atomic64_t
 *
 * Atomically increments @v by 1
 * and returns true if the result is zero, or false for all
 * other cases.
 */
#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)

static __inline__ void atomic64_dec(atomic64_t *v)
{
	long t;

	__asm__ __volatile__(
"1:	ldarx	%0,0,%2		# atomic64_dec\n\
	addic	%0,%0,-1\n\
	stdcx.	%0,0,%2\n\
	bne-	1b"
372 373
	: "=&r" (t), "+m" (v->counter)
	: "r" (&v->counter)
374
	: "cc", "xer");
375 376 377 378 379 380 381
}

static __inline__ long atomic64_dec_return(atomic64_t *v)
{
	long t;

	__asm__ __volatile__(
382
	PPC_ATOMIC_ENTRY_BARRIER
383 384 385 386
"1:	ldarx	%0,0,%1		# atomic64_dec_return\n\
	addic	%0,%0,-1\n\
	stdcx.	%0,0,%1\n\
	bne-	1b"
387
	PPC_ATOMIC_EXIT_BARRIER
388 389
	: "=&r" (t)
	: "r" (&v->counter)
390
	: "cc", "xer", "memory");
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

	return t;
}

#define atomic64_sub_and_test(a, v)	(atomic64_sub_return((a), (v)) == 0)
#define atomic64_dec_and_test(v)	(atomic64_dec_return((v)) == 0)

/*
 * Atomically test *v and decrement if it is greater than 0.
 * The function returns the old value of *v minus 1.
 */
static __inline__ long atomic64_dec_if_positive(atomic64_t *v)
{
	long t;

	__asm__ __volatile__(
407
	PPC_ATOMIC_ENTRY_BARRIER
408 409 410 411 412
"1:	ldarx	%0,0,%1		# atomic64_dec_if_positive\n\
	addic.	%0,%0,-1\n\
	blt-	2f\n\
	stdcx.	%0,0,%1\n\
	bne-	1b"
413
	PPC_ATOMIC_EXIT_BARRIER
414 415 416
	"\n\
2:"	: "=&r" (t)
	: "r" (&v->counter)
417
	: "cc", "xer", "memory");
418 419 420 421

	return t;
}

422
#define atomic64_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
423 424 425 426 427 428 429 430 431
#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))

/**
 * atomic64_add_unless - add unless the number is a given value
 * @v: pointer of type atomic64_t
 * @a: the amount to add to v...
 * @u: ...unless v is equal to u.
 *
 * Atomically adds @a to @v, so long as it was not @u.
432
 * Returns the old value of @v.
433 434 435 436 437 438
 */
static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
{
	long t;

	__asm__ __volatile__ (
439
	PPC_ATOMIC_ENTRY_BARRIER
440
"1:	ldarx	%0,0,%1		# __atomic_add_unless\n\
441 442 443 444 445
	cmpd	0,%0,%3 \n\
	beq-	2f \n\
	add	%0,%2,%0 \n"
"	stdcx.	%0,0,%1 \n\
	bne-	1b \n"
446
	PPC_ATOMIC_EXIT_BARRIER
447 448 449 450 451 452 453 454 455
"	subf	%0,%2,%0 \n\
2:"
	: "=&r" (t)
	: "r" (&v->counter), "r" (a), "r" (u)
	: "cc", "memory");

	return t != u;
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
/**
 * atomic_inc64_not_zero - increment unless the number is zero
 * @v: pointer of type atomic64_t
 *
 * Atomically increments @v by 1, so long as @v is non-zero.
 * Returns non-zero if @v was non-zero, and zero otherwise.
 */
static __inline__ long atomic64_inc_not_zero(atomic64_t *v)
{
	long t1, t2;

	__asm__ __volatile__ (
	PPC_ATOMIC_ENTRY_BARRIER
"1:	ldarx	%0,0,%2		# atomic64_inc_not_zero\n\
	cmpdi	0,%0,0\n\
	beq-	2f\n\
	addic	%1,%0,1\n\
	stdcx.	%1,0,%2\n\
	bne-	1b\n"
	PPC_ATOMIC_EXIT_BARRIER
	"\n\
2:"
	: "=&r" (t1), "=&r" (t2)
	: "r" (&v->counter)
	: "cc", "xer", "memory");

	return t1;
}
484

485 486
#endif /* __powerpc64__ */

L
Linus Torvalds 已提交
487
#endif /* __KERNEL__ */
488
#endif /* _ASM_POWERPC_ATOMIC_H_ */