atomic.h 16.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Atomic operations that C can't guarantee us.  Useful for
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11
 * resource counting etc..
 *
 * But use these as seldom as possible since they are much more slower
 * than regular operations.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
12
 * Copyright (C) 1996, 97, 99, 2000, 03, 04, 06 by Ralf Baechle
L
Linus Torvalds 已提交
13 14 15 16
 */
#ifndef _ASM_ATOMIC_H
#define _ASM_ATOMIC_H

17
#include <linux/irqflags.h>
18
#include <linux/types.h>
19
#include <asm/barrier.h>
L
Linus Torvalds 已提交
20
#include <asm/cpu-features.h>
21
#include <asm/cmpxchg.h>
L
Linus Torvalds 已提交
22 23
#include <asm/war.h>

R
Ralf Baechle 已提交
24
#define ATOMIC_INIT(i)	  { (i) }
L
Linus Torvalds 已提交
25 26 27 28 29 30 31

/*
 * atomic_read - read atomic variable
 * @v: pointer of type atomic_t
 *
 * Atomically reads the value of @v.
 */
32
#define atomic_read(v)		(*(volatile int *)&(v)->counter)
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40

/*
 * atomic_set - set atomic variable
 * @v: pointer of type atomic_t
 * @i: required value
 *
 * Atomically sets the value of @v to @i.
 */
41
#define atomic_set(v, i)		((v)->counter = (i))
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51

/*
 * atomic_add - add integer to atomic variable
 * @i: integer value to add
 * @v: pointer of type atomic_t
 *
 * Atomically adds @i to @v.
 */
static __inline__ void atomic_add(int i, atomic_t * v)
{
52
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
53
		int temp;
L
Linus Torvalds 已提交
54 55

		__asm__ __volatile__(
56
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
57 58 59 60
		"1:	ll	%0, %1		# atomic_add		\n"
		"	addu	%0, %2					\n"
		"	sc	%0, %1					\n"
		"	beqzl	%0, 1b					\n"
61
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
62 63
		: "=&r" (temp), "+m" (v->counter)
		: "Ir" (i));
64
	} else if (kernel_uses_llsc) {
65
		int temp;
L
Linus Torvalds 已提交
66

67 68
		do {
			__asm__ __volatile__(
69
			"	.set	arch=r4000			\n"
70 71 72 73
			"	ll	%0, %1		# atomic_add	\n"
			"	addu	%0, %2				\n"
			"	sc	%0, %1				\n"
			"	.set	mips0				\n"
J
Joshua Kinard 已提交
74 75
			: "=&r" (temp), "+m" (v->counter)
			: "Ir" (i));
76
		} while (unlikely(!temp));
L
Linus Torvalds 已提交
77 78 79
	} else {
		unsigned long flags;

80
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
81
		v->counter += i;
82
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92 93 94
	}
}

/*
 * atomic_sub - subtract the atomic variable
 * @i: integer value to subtract
 * @v: pointer of type atomic_t
 *
 * Atomically subtracts @i from @v.
 */
static __inline__ void atomic_sub(int i, atomic_t * v)
{
95
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
96
		int temp;
L
Linus Torvalds 已提交
97 98

		__asm__ __volatile__(
99
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
100 101 102 103
		"1:	ll	%0, %1		# atomic_sub		\n"
		"	subu	%0, %2					\n"
		"	sc	%0, %1					\n"
		"	beqzl	%0, 1b					\n"
104
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
105 106
		: "=&r" (temp), "+m" (v->counter)
		: "Ir" (i));
107
	} else if (kernel_uses_llsc) {
108
		int temp;
L
Linus Torvalds 已提交
109

110 111
		do {
			__asm__ __volatile__(
112
			"	.set	arch=r4000			\n"
113 114 115 116
			"	ll	%0, %1		# atomic_sub	\n"
			"	subu	%0, %2				\n"
			"	sc	%0, %1				\n"
			"	.set	mips0				\n"
J
Joshua Kinard 已提交
117 118
			: "=&r" (temp), "+m" (v->counter)
			: "Ir" (i));
119
		} while (unlikely(!temp));
L
Linus Torvalds 已提交
120 121 122
	} else {
		unsigned long flags;

123
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
124
		v->counter -= i;
125
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133
	}
}

/*
 * Same as above, but return the result value
 */
static __inline__ int atomic_add_return(int i, atomic_t * v)
{
134
	int result;
L
Linus Torvalds 已提交
135

136
	smp_mb__before_llsc();
137

138
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
139
		int temp;
L
Linus Torvalds 已提交
140 141

		__asm__ __volatile__(
142
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
143 144 145 146 147
		"1:	ll	%1, %2		# atomic_add_return	\n"
		"	addu	%0, %1, %3				\n"
		"	sc	%0, %2					\n"
		"	beqzl	%0, 1b					\n"
		"	addu	%0, %1, %3				\n"
148
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
149 150
		: "=&r" (result), "=&r" (temp), "+m" (v->counter)
		: "Ir" (i));
151
	} else if (kernel_uses_llsc) {
152
		int temp;
L
Linus Torvalds 已提交
153

154 155
		do {
			__asm__ __volatile__(
156
			"	.set	arch=r4000			\n"
157 158 159 160
			"	ll	%1, %2	# atomic_add_return	\n"
			"	addu	%0, %1, %3			\n"
			"	sc	%0, %2				\n"
			"	.set	mips0				\n"
J
Joshua Kinard 已提交
161 162
			: "=&r" (result), "=&r" (temp), "+m" (v->counter)
			: "Ir" (i));
163 164 165
		} while (unlikely(!result));

		result = temp + i;
L
Linus Torvalds 已提交
166 167 168
	} else {
		unsigned long flags;

169
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
170 171 172
		result = v->counter;
		result += i;
		v->counter = result;
173
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
174 175
	}

176
	smp_llsc_mb();
177

L
Linus Torvalds 已提交
178 179 180 181 182
	return result;
}

static __inline__ int atomic_sub_return(int i, atomic_t * v)
{
183
	int result;
L
Linus Torvalds 已提交
184

185
	smp_mb__before_llsc();
186

187
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
188
		int temp;
L
Linus Torvalds 已提交
189 190

		__asm__ __volatile__(
191
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
192 193 194 195 196
		"1:	ll	%1, %2		# atomic_sub_return	\n"
		"	subu	%0, %1, %3				\n"
		"	sc	%0, %2					\n"
		"	beqzl	%0, 1b					\n"
		"	subu	%0, %1, %3				\n"
197
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
198 199 200
		: "=&r" (result), "=&r" (temp), "=m" (v->counter)
		: "Ir" (i), "m" (v->counter)
		: "memory");
201 202

		result = temp - i;
203
	} else if (kernel_uses_llsc) {
204
		int temp;
L
Linus Torvalds 已提交
205

206 207
		do {
			__asm__ __volatile__(
208
			"	.set	arch=r4000			\n"
209 210 211 212
			"	ll	%1, %2	# atomic_sub_return	\n"
			"	subu	%0, %1, %3			\n"
			"	sc	%0, %2				\n"
			"	.set	mips0				\n"
J
Joshua Kinard 已提交
213 214
			: "=&r" (result), "=&r" (temp), "+m" (v->counter)
			: "Ir" (i));
215 216 217
		} while (unlikely(!result));

		result = temp - i;
L
Linus Torvalds 已提交
218 219 220
	} else {
		unsigned long flags;

221
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
222 223 224
		result = v->counter;
		result -= i;
		v->counter = result;
225
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
226 227
	}

228
	smp_llsc_mb();
229

L
Linus Torvalds 已提交
230 231 232 233
	return result;
}

/*
A
Arnaud Giersch 已提交
234 235
 * atomic_sub_if_positive - conditionally subtract integer from atomic variable
 * @i: integer value to subtract
L
Linus Torvalds 已提交
236 237
 * @v: pointer of type atomic_t
 *
A
Arnaud Giersch 已提交
238 239
 * Atomically test @v and subtract @i if @v is greater or equal than @i.
 * The function returns the old value of @v minus @i.
L
Linus Torvalds 已提交
240 241 242
 */
static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
{
243
	int result;
L
Linus Torvalds 已提交
244

245
	smp_mb__before_llsc();
246

247
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
248
		int temp;
L
Linus Torvalds 已提交
249 250

		__asm__ __volatile__(
251
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
252 253 254 255
		"1:	ll	%1, %2		# atomic_sub_if_positive\n"
		"	subu	%0, %1, %3				\n"
		"	bltz	%0, 1f					\n"
		"	sc	%0, %2					\n"
256
		"	.set	noreorder				\n"
L
Linus Torvalds 已提交
257
		"	beqzl	%0, 1b					\n"
258 259
		"	 subu	%0, %1, %3				\n"
		"	.set	reorder					\n"
L
Linus Torvalds 已提交
260
		"1:							\n"
261
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
262
		: "=&r" (result), "=&r" (temp), "+m" (v->counter)
L
Linus Torvalds 已提交
263 264
		: "Ir" (i), "m" (v->counter)
		: "memory");
265
	} else if (kernel_uses_llsc) {
266
		int temp;
L
Linus Torvalds 已提交
267 268

		__asm__ __volatile__(
269
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
270 271 272 273
		"1:	ll	%1, %2		# atomic_sub_if_positive\n"
		"	subu	%0, %1, %3				\n"
		"	bltz	%0, 1f					\n"
		"	sc	%0, %2					\n"
274
		"	.set	noreorder				\n"
275
		"	beqz	%0, 1b					\n"
276 277
		"	 subu	%0, %1, %3				\n"
		"	.set	reorder					\n"
278
		"1:							\n"
279
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
280 281
		: "=&r" (result), "=&r" (temp), "+m" (v->counter)
		: "Ir" (i));
L
Linus Torvalds 已提交
282 283 284
	} else {
		unsigned long flags;

285
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
286 287 288 289
		result = v->counter;
		result -= i;
		if (result >= 0)
			v->counter = result;
290
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
291 292
	}

293
	smp_llsc_mb();
294

L
Linus Torvalds 已提交
295 296 297
	return result;
}

298 299
#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
#define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
N
Nick Piggin 已提交
300

N
Nick Piggin 已提交
301
/**
302
 * __atomic_add_unless - add unless the number is a given value
N
Nick Piggin 已提交
303 304 305 306 307
 * @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.
308
 * Returns the old value of @v.
N
Nick Piggin 已提交
309
 */
310
static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
311 312 313 314 315 316 317 318 319 320 321
{
	int c, old;
	c = atomic_read(v);
	for (;;) {
		if (unlikely(c == (u)))
			break;
		old = atomic_cmpxchg((v), c, c + (a));
		if (likely(old == c))
			break;
		c = old;
	}
322
	return c;
323
}
N
Nick Piggin 已提交
324

325 326
#define atomic_dec_return(v) atomic_sub_return(1, (v))
#define atomic_inc_return(v) atomic_add_return(1, (v))
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335 336

/*
 * atomic_sub_and_test - subtract value from variable and test result
 * @i: integer value to subtract
 * @v: pointer of type atomic_t
 *
 * Atomically subtracts @i from @v and returns
 * true if the result is zero, or false for all
 * other cases.
 */
337
#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

/*
 * 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)

/*
 * atomic_dec_and_test - decrement by 1 and test
 * @v: pointer of type atomic_t
 *
 * Atomically decrements @v by 1 and
 * returns true if the result is 0, or false for all other
 * cases.
 */
#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)

/*
 * atomic_dec_if_positive - decrement by 1 if old value positive
 * @v: pointer of type atomic_t
 */
#define atomic_dec_if_positive(v)	atomic_sub_if_positive(1, v)

/*
 * atomic_inc - increment atomic variable
 * @v: pointer of type atomic_t
 *
 * Atomically increments @v by 1.
 */
371
#define atomic_inc(v) atomic_add(1, (v))
L
Linus Torvalds 已提交
372 373 374 375 376 377 378

/*
 * atomic_dec - decrement and test
 * @v: pointer of type atomic_t
 *
 * Atomically decrements @v by 1.
 */
379
#define atomic_dec(v) atomic_sub(1, (v))
L
Linus Torvalds 已提交
380 381 382 383 384 385 386 387 388 389

/*
 * atomic_add_negative - add and test if negative
 * @v: pointer of type atomic_t
 * @i: integer value to add
 *
 * Atomically adds @i to @v and returns true
 * if the result is negative, or false when
 * result is greater than or equal to zero.
 */
390
#define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
L
Linus Torvalds 已提交
391

392
#ifdef CONFIG_64BIT
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400

#define ATOMIC64_INIT(i)    { (i) }

/*
 * atomic64_read - read atomic variable
 * @v: pointer of type atomic64_t
 *
 */
401
#define atomic64_read(v)	(*(volatile long *)&(v)->counter)
L
Linus Torvalds 已提交
402 403 404 405 406 407

/*
 * atomic64_set - set atomic variable
 * @v: pointer of type atomic64_t
 * @i: required value
 */
408
#define atomic64_set(v, i)	((v)->counter = (i))
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418

/*
 * atomic64_add - add integer to atomic variable
 * @i: integer value to add
 * @v: pointer of type atomic64_t
 *
 * Atomically adds @i to @v.
 */
static __inline__ void atomic64_add(long i, atomic64_t * v)
{
419
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
420
		long temp;
L
Linus Torvalds 已提交
421 422

		__asm__ __volatile__(
423
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
424
		"1:	lld	%0, %1		# atomic64_add		\n"
425
		"	daddu	%0, %2					\n"
L
Linus Torvalds 已提交
426 427
		"	scd	%0, %1					\n"
		"	beqzl	%0, 1b					\n"
428
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
429 430
		: "=&r" (temp), "+m" (v->counter)
		: "Ir" (i));
431
	} else if (kernel_uses_llsc) {
432
		long temp;
L
Linus Torvalds 已提交
433

434 435
		do {
			__asm__ __volatile__(
436
			"	.set	arch=r4000			\n"
437 438 439 440
			"	lld	%0, %1		# atomic64_add	\n"
			"	daddu	%0, %2				\n"
			"	scd	%0, %1				\n"
			"	.set	mips0				\n"
J
Joshua Kinard 已提交
441 442
			: "=&r" (temp), "+m" (v->counter)
			: "Ir" (i));
443
		} while (unlikely(!temp));
L
Linus Torvalds 已提交
444 445 446
	} else {
		unsigned long flags;

447
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
448
		v->counter += i;
449
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457 458 459 460 461
	}
}

/*
 * atomic64_sub - subtract the atomic variable
 * @i: integer value to subtract
 * @v: pointer of type atomic64_t
 *
 * Atomically subtracts @i from @v.
 */
static __inline__ void atomic64_sub(long i, atomic64_t * v)
{
462
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
463
		long temp;
L
Linus Torvalds 已提交
464 465

		__asm__ __volatile__(
466
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
467
		"1:	lld	%0, %1		# atomic64_sub		\n"
468
		"	dsubu	%0, %2					\n"
L
Linus Torvalds 已提交
469 470
		"	scd	%0, %1					\n"
		"	beqzl	%0, 1b					\n"
471
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
472 473
		: "=&r" (temp), "+m" (v->counter)
		: "Ir" (i));
474
	} else if (kernel_uses_llsc) {
475
		long temp;
L
Linus Torvalds 已提交
476

477 478
		do {
			__asm__ __volatile__(
479
			"	.set	arch=r4000			\n"
480 481 482 483
			"	lld	%0, %1		# atomic64_sub	\n"
			"	dsubu	%0, %2				\n"
			"	scd	%0, %1				\n"
			"	.set	mips0				\n"
J
Joshua Kinard 已提交
484 485
			: "=&r" (temp), "+m" (v->counter)
			: "Ir" (i));
486
		} while (unlikely(!temp));
L
Linus Torvalds 已提交
487 488 489
	} else {
		unsigned long flags;

490
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
491
		v->counter -= i;
492
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500
	}
}

/*
 * Same as above, but return the result value
 */
static __inline__ long atomic64_add_return(long i, atomic64_t * v)
{
501
	long result;
L
Linus Torvalds 已提交
502

503
	smp_mb__before_llsc();
504

505
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
506
		long temp;
L
Linus Torvalds 已提交
507 508

		__asm__ __volatile__(
509
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
510
		"1:	lld	%1, %2		# atomic64_add_return	\n"
511
		"	daddu	%0, %1, %3				\n"
L
Linus Torvalds 已提交
512 513
		"	scd	%0, %2					\n"
		"	beqzl	%0, 1b					\n"
514
		"	daddu	%0, %1, %3				\n"
515
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
516 517
		: "=&r" (result), "=&r" (temp), "+m" (v->counter)
		: "Ir" (i));
518
	} else if (kernel_uses_llsc) {
519
		long temp;
L
Linus Torvalds 已提交
520

521 522
		do {
			__asm__ __volatile__(
523
			"	.set	arch=r4000			\n"
524 525 526 527 528 529 530 531 532 533
			"	lld	%1, %2	# atomic64_add_return	\n"
			"	daddu	%0, %1, %3			\n"
			"	scd	%0, %2				\n"
			"	.set	mips0				\n"
			: "=&r" (result), "=&r" (temp), "=m" (v->counter)
			: "Ir" (i), "m" (v->counter)
			: "memory");
		} while (unlikely(!result));

		result = temp + i;
L
Linus Torvalds 已提交
534 535 536
	} else {
		unsigned long flags;

537
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
538 539 540
		result = v->counter;
		result += i;
		v->counter = result;
541
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
542 543
	}

544
	smp_llsc_mb();
545

L
Linus Torvalds 已提交
546 547 548 549 550
	return result;
}

static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
{
551
	long result;
L
Linus Torvalds 已提交
552

553
	smp_mb__before_llsc();
554

555
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
556
		long temp;
L
Linus Torvalds 已提交
557 558

		__asm__ __volatile__(
559
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
560
		"1:	lld	%1, %2		# atomic64_sub_return	\n"
561
		"	dsubu	%0, %1, %3				\n"
L
Linus Torvalds 已提交
562 563
		"	scd	%0, %2					\n"
		"	beqzl	%0, 1b					\n"
564
		"	dsubu	%0, %1, %3				\n"
565
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
566 567 568
		: "=&r" (result), "=&r" (temp), "=m" (v->counter)
		: "Ir" (i), "m" (v->counter)
		: "memory");
569
	} else if (kernel_uses_llsc) {
570
		long temp;
L
Linus Torvalds 已提交
571

572 573
		do {
			__asm__ __volatile__(
574
			"	.set	arch=r4000			\n"
575 576 577 578 579 580 581 582 583 584
			"	lld	%1, %2	# atomic64_sub_return	\n"
			"	dsubu	%0, %1, %3			\n"
			"	scd	%0, %2				\n"
			"	.set	mips0				\n"
			: "=&r" (result), "=&r" (temp), "=m" (v->counter)
			: "Ir" (i), "m" (v->counter)
			: "memory");
		} while (unlikely(!result));

		result = temp - i;
L
Linus Torvalds 已提交
585 586 587
	} else {
		unsigned long flags;

588
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
589 590 591
		result = v->counter;
		result -= i;
		v->counter = result;
592
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
593 594
	}

595
	smp_llsc_mb();
596

L
Linus Torvalds 已提交
597 598 599 600
	return result;
}

/*
A
Arnaud Giersch 已提交
601 602
 * atomic64_sub_if_positive - conditionally subtract integer from atomic variable
 * @i: integer value to subtract
L
Linus Torvalds 已提交
603 604
 * @v: pointer of type atomic64_t
 *
A
Arnaud Giersch 已提交
605 606
 * Atomically test @v and subtract @i if @v is greater or equal than @i.
 * The function returns the old value of @v minus @i.
L
Linus Torvalds 已提交
607 608 609
 */
static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
{
610
	long result;
L
Linus Torvalds 已提交
611

612
	smp_mb__before_llsc();
613

614
	if (kernel_uses_llsc && R10000_LLSC_WAR) {
615
		long temp;
L
Linus Torvalds 已提交
616 617

		__asm__ __volatile__(
618
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
619 620 621 622
		"1:	lld	%1, %2		# atomic64_sub_if_positive\n"
		"	dsubu	%0, %1, %3				\n"
		"	bltz	%0, 1f					\n"
		"	scd	%0, %2					\n"
623
		"	.set	noreorder				\n"
L
Linus Torvalds 已提交
624
		"	beqzl	%0, 1b					\n"
625 626
		"	 dsubu	%0, %1, %3				\n"
		"	.set	reorder					\n"
L
Linus Torvalds 已提交
627
		"1:							\n"
628
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
629 630 631
		: "=&r" (result), "=&r" (temp), "=m" (v->counter)
		: "Ir" (i), "m" (v->counter)
		: "memory");
632
	} else if (kernel_uses_llsc) {
633
		long temp;
L
Linus Torvalds 已提交
634 635

		__asm__ __volatile__(
636
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
637 638 639 640
		"1:	lld	%1, %2		# atomic64_sub_if_positive\n"
		"	dsubu	%0, %1, %3				\n"
		"	bltz	%0, 1f					\n"
		"	scd	%0, %2					\n"
641
		"	.set	noreorder				\n"
642
		"	beqz	%0, 1b					\n"
643 644
		"	 dsubu	%0, %1, %3				\n"
		"	.set	reorder					\n"
645
		"1:							\n"
646
		"	.set	mips0					\n"
J
Joshua Kinard 已提交
647 648
		: "=&r" (result), "=&r" (temp), "+m" (v->counter)
		: "Ir" (i));
L
Linus Torvalds 已提交
649 650 651
	} else {
		unsigned long flags;

652
		raw_local_irq_save(flags);
L
Linus Torvalds 已提交
653 654 655 656
		result = v->counter;
		result -= i;
		if (result >= 0)
			v->counter = result;
657
		raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
658 659
	}

660
	smp_llsc_mb();
661

L
Linus Torvalds 已提交
662 663 664
	return result;
}

665
#define atomic64_cmpxchg(v, o, n) \
666
	((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
667 668 669 670 671 672 673 674 675
#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.
676
 * Returns the old value of @v.
677
 */
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
{
	long c, old;
	c = atomic64_read(v);
	for (;;) {
		if (unlikely(c == (u)))
			break;
		old = atomic64_cmpxchg((v), c, c + (a));
		if (likely(old == c))
			break;
		c = old;
	}
	return c != (u);
}

693 694
#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)

695 696
#define atomic64_dec_return(v) atomic64_sub_return(1, (v))
#define atomic64_inc_return(v) atomic64_add_return(1, (v))
L
Linus Torvalds 已提交
697 698 699 700 701 702 703 704 705 706

/*
 * atomic64_sub_and_test - subtract value from variable and test result
 * @i: integer value to subtract
 * @v: pointer of type atomic64_t
 *
 * Atomically subtracts @i from @v and returns
 * true if the result is zero, or false for all
 * other cases.
 */
707
#define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0)
L
Linus Torvalds 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

/*
 * 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)

/*
 * atomic64_dec_and_test - decrement by 1 and test
 * @v: pointer of type atomic64_t
 *
 * Atomically decrements @v by 1 and
 * returns true if the result is 0, or false for all other
 * cases.
 */
#define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)

/*
 * atomic64_dec_if_positive - decrement by 1 if old value positive
 * @v: pointer of type atomic64_t
 */
#define atomic64_dec_if_positive(v)	atomic64_sub_if_positive(1, v)

/*
 * atomic64_inc - increment atomic variable
 * @v: pointer of type atomic64_t
 *
 * Atomically increments @v by 1.
 */
741
#define atomic64_inc(v) atomic64_add(1, (v))
L
Linus Torvalds 已提交
742 743 744 745 746 747 748

/*
 * atomic64_dec - decrement and test
 * @v: pointer of type atomic64_t
 *
 * Atomically decrements @v by 1.
 */
749
#define atomic64_dec(v) atomic64_sub(1, (v))
L
Linus Torvalds 已提交
750 751 752 753 754 755 756 757 758 759

/*
 * atomic64_add_negative - add and test if negative
 * @v: pointer of type atomic64_t
 * @i: integer value to add
 *
 * Atomically adds @i to @v and returns true
 * if the result is negative, or false when
 * result is greater than or equal to zero.
 */
760
#define atomic64_add_negative(i, v) (atomic64_add_return(i, (v)) < 0)
L
Linus Torvalds 已提交
761

762
#endif /* CONFIG_64BIT */
L
Linus Torvalds 已提交
763 764

#endif /* _ASM_ATOMIC_H */