atomic.h 10.0 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  arch/arm/include/asm/atomic.h
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13
 *
 *  Copyright (C) 1996 Russell King.
 *  Copyright (C) 2002 Deep Blue Solutions Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#ifndef __ASM_ARM_ATOMIC_H
#define __ASM_ARM_ATOMIC_H

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

#define ATOMIC_INIT(i)	{ (i) }

#ifdef __KERNEL__

24 25 26 27 28
/*
 * On ARM, ordinary assignment (str instruction) doesn't clear the local
 * strex/ldrex monitor on some implementations. The reason we can use it for
 * atomic_set() is the clrex or dummy strex done on every exception return.
 */
29
#define atomic_read(v)	(*(volatile int *)&(v)->counter)
30
#define atomic_set(v,i)	(((v)->counter) = (i))
L
Linus Torvalds 已提交
31 32 33 34 35 36

#if __LINUX_ARM_ARCH__ >= 6

/*
 * ARMv6 UP and SMP safe atomic ops.  We use load exclusive and
 * store exclusive to ensure that these are atomic.  We may loop
37
 * to ensure that the update happens.
L
Linus Torvalds 已提交
38
 */
39 40 41 42 43 44
static inline void atomic_add(int i, atomic_t *v)
{
	unsigned long tmp;
	int result;

	__asm__ __volatile__("@ atomic_add\n"
45 46 47
"1:	ldrex	%0, [%3]\n"
"	add	%0, %0, %4\n"
"	strex	%1, %0, [%3]\n"
48 49
"	teq	%1, #0\n"
"	bne	1b"
50
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
51 52 53 54
	: "r" (&v->counter), "Ir" (i)
	: "cc");
}

L
Linus Torvalds 已提交
55 56 57 58 59
static inline int atomic_add_return(int i, atomic_t *v)
{
	unsigned long tmp;
	int result;

60 61
	smp_mb();

L
Linus Torvalds 已提交
62
	__asm__ __volatile__("@ atomic_add_return\n"
63 64 65
"1:	ldrex	%0, [%3]\n"
"	add	%0, %0, %4\n"
"	strex	%1, %0, [%3]\n"
L
Linus Torvalds 已提交
66 67
"	teq	%1, #0\n"
"	bne	1b"
68
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
L
Linus Torvalds 已提交
69 70 71
	: "r" (&v->counter), "Ir" (i)
	: "cc");

72 73
	smp_mb();

L
Linus Torvalds 已提交
74 75 76
	return result;
}

77 78 79 80 81 82
static inline void atomic_sub(int i, atomic_t *v)
{
	unsigned long tmp;
	int result;

	__asm__ __volatile__("@ atomic_sub\n"
83 84 85
"1:	ldrex	%0, [%3]\n"
"	sub	%0, %0, %4\n"
"	strex	%1, %0, [%3]\n"
86 87
"	teq	%1, #0\n"
"	bne	1b"
88
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
89 90 91 92
	: "r" (&v->counter), "Ir" (i)
	: "cc");
}

L
Linus Torvalds 已提交
93 94 95 96 97
static inline int atomic_sub_return(int i, atomic_t *v)
{
	unsigned long tmp;
	int result;

98 99
	smp_mb();

L
Linus Torvalds 已提交
100
	__asm__ __volatile__("@ atomic_sub_return\n"
101 102 103
"1:	ldrex	%0, [%3]\n"
"	sub	%0, %0, %4\n"
"	strex	%1, %0, [%3]\n"
L
Linus Torvalds 已提交
104 105
"	teq	%1, #0\n"
"	bne	1b"
106
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
L
Linus Torvalds 已提交
107 108 109
	: "r" (&v->counter), "Ir" (i)
	: "cc");

110 111
	smp_mb();

L
Linus Torvalds 已提交
112 113 114
	return result;
}

N
Nick Piggin 已提交
115 116
static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new)
{
117
	unsigned long oldval, res;
N
Nick Piggin 已提交
118

119 120
	smp_mb();

N
Nick Piggin 已提交
121 122
	do {
		__asm__ __volatile__("@ atomic_cmpxchg\n"
123
		"ldrex	%1, [%3]\n"
124
		"mov	%0, #0\n"
125 126 127
		"teq	%1, %4\n"
		"strexeq %0, %5, [%3]\n"
		    : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
N
Nick Piggin 已提交
128 129 130 131
		    : "r" (&ptr->counter), "Ir" (old), "r" (new)
		    : "cc");
	} while (res);

132 133
	smp_mb();

N
Nick Piggin 已提交
134 135 136
	return oldval;
}

L
Linus Torvalds 已提交
137 138 139 140 141
static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
{
	unsigned long tmp, tmp2;

	__asm__ __volatile__("@ atomic_clear_mask\n"
142 143 144
"1:	ldrex	%0, [%3]\n"
"	bic	%0, %0, %4\n"
"	strex	%1, %0, [%3]\n"
L
Linus Torvalds 已提交
145 146
"	teq	%1, #0\n"
"	bne	1b"
147
	: "=&r" (tmp), "=&r" (tmp2), "+Qo" (*addr)
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	: "r" (addr), "Ir" (mask)
	: "cc");
}

#else /* ARM_ARCH_6 */

#ifdef CONFIG_SMP
#error SMP not supported on pre-ARMv6 CPUs
#endif

static inline int atomic_add_return(int i, atomic_t *v)
{
	unsigned long flags;
	int val;

163
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
164 165
	val = v->counter;
	v->counter = val += i;
166
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
167 168 169

	return val;
}
170
#define atomic_add(i, v)	(void) atomic_add_return(i, v)
L
Linus Torvalds 已提交
171 172 173 174 175 176

static inline int atomic_sub_return(int i, atomic_t *v)
{
	unsigned long flags;
	int val;

177
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
178 179
	val = v->counter;
	v->counter = val -= i;
180
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
181 182 183

	return val;
}
184
#define atomic_sub(i, v)	(void) atomic_sub_return(i, v)
L
Linus Torvalds 已提交
185

N
Nick Piggin 已提交
186 187 188 189 190
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
{
	int ret;
	unsigned long flags;

191
	raw_local_irq_save(flags);
N
Nick Piggin 已提交
192 193 194
	ret = v->counter;
	if (likely(ret == old))
		v->counter = new;
195
	raw_local_irq_restore(flags);
N
Nick Piggin 已提交
196 197 198 199

	return ret;
}

L
Linus Torvalds 已提交
200 201 202 203
static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
{
	unsigned long flags;

204
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
205
	*addr &= ~mask;
206
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
207 208 209 210
}

#endif /* __LINUX_ARM_ARCH__ */

211 212
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))

213
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
N
Nick Piggin 已提交
214 215 216 217 218 219
{
	int c, old;

	c = atomic_read(v);
	while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
		c = old;
220
	return c;
N
Nick Piggin 已提交
221 222
}

223 224
#define atomic_inc(v)		atomic_add(1, v)
#define atomic_dec(v)		atomic_sub(1, v)
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233

#define atomic_inc_and_test(v)	(atomic_add_return(1, v) == 0)
#define atomic_dec_and_test(v)	(atomic_sub_return(1, v) == 0)
#define atomic_inc_return(v)    (atomic_add_return(1, v))
#define atomic_dec_return(v)    (atomic_sub_return(1, v))
#define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)

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

234 235 236 237
#define smp_mb__before_atomic_dec()	smp_mb()
#define smp_mb__after_atomic_dec()	smp_mb()
#define smp_mb__before_atomic_inc()	smp_mb()
#define smp_mb__after_atomic_inc()	smp_mb()
L
Linus Torvalds 已提交
238

239 240
#ifndef CONFIG_GENERIC_ATOMIC64
typedef struct {
241
	long long counter;
242 243 244 245
} atomic64_t;

#define ATOMIC64_INIT(i) { (i) }

246
#ifdef CONFIG_ARM_LPAE
247
static inline long long atomic64_read(const atomic64_t *v)
248
{
249
	long long result;
250 251 252 253 254 255 256 257 258 259

	__asm__ __volatile__("@ atomic64_read\n"
"	ldrd	%0, %H0, [%1]"
	: "=&r" (result)
	: "r" (&v->counter), "Qo" (v->counter)
	);

	return result;
}

260
static inline void atomic64_set(atomic64_t *v, long long i)
261 262 263 264 265 266 267 268
{
	__asm__ __volatile__("@ atomic64_set\n"
"	strd	%2, %H2, [%1]"
	: "=Qo" (v->counter)
	: "r" (&v->counter), "r" (i)
	);
}
#else
269
static inline long long atomic64_read(const atomic64_t *v)
270
{
271
	long long result;
272 273 274 275

	__asm__ __volatile__("@ atomic64_read\n"
"	ldrexd	%0, %H0, [%1]"
	: "=&r" (result)
276
	: "r" (&v->counter), "Qo" (v->counter)
277 278 279 280 281
	);

	return result;
}

282
static inline void atomic64_set(atomic64_t *v, long long i)
283
{
284
	long long tmp;
285 286

	__asm__ __volatile__("@ atomic64_set\n"
287 288
"1:	ldrexd	%0, %H0, [%2]\n"
"	strexd	%0, %3, %H3, [%2]\n"
289 290
"	teq	%0, #0\n"
"	bne	1b"
291
	: "=&r" (tmp), "=Qo" (v->counter)
292 293 294
	: "r" (&v->counter), "r" (i)
	: "cc");
}
295
#endif
296

297
static inline void atomic64_add(long long i, atomic64_t *v)
298
{
299
	long long result;
300 301 302
	unsigned long tmp;

	__asm__ __volatile__("@ atomic64_add\n"
303 304 305 306
"1:	ldrexd	%0, %H0, [%3]\n"
"	adds	%0, %0, %4\n"
"	adc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
307 308
"	teq	%1, #0\n"
"	bne	1b"
309
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
310 311 312 313
	: "r" (&v->counter), "r" (i)
	: "cc");
}

314
static inline long long atomic64_add_return(long long i, atomic64_t *v)
315
{
316
	long long result;
317 318 319 320 321
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_add_return\n"
322 323 324 325
"1:	ldrexd	%0, %H0, [%3]\n"
"	adds	%0, %0, %4\n"
"	adc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
326 327
"	teq	%1, #0\n"
"	bne	1b"
328
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
329 330 331 332 333 334 335 336
	: "r" (&v->counter), "r" (i)
	: "cc");

	smp_mb();

	return result;
}

337
static inline void atomic64_sub(long long i, atomic64_t *v)
338
{
339
	long long result;
340 341 342
	unsigned long tmp;

	__asm__ __volatile__("@ atomic64_sub\n"
343 344 345 346
"1:	ldrexd	%0, %H0, [%3]\n"
"	subs	%0, %0, %4\n"
"	sbc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
347 348
"	teq	%1, #0\n"
"	bne	1b"
349
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
350 351 352 353
	: "r" (&v->counter), "r" (i)
	: "cc");
}

354
static inline long long atomic64_sub_return(long long i, atomic64_t *v)
355
{
356
	long long result;
357 358 359 360 361
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_sub_return\n"
362 363 364 365
"1:	ldrexd	%0, %H0, [%3]\n"
"	subs	%0, %0, %4\n"
"	sbc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
366 367
"	teq	%1, #0\n"
"	bne	1b"
368
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
369 370 371 372 373 374 375 376
	: "r" (&v->counter), "r" (i)
	: "cc");

	smp_mb();

	return result;
}

377 378
static inline long long atomic64_cmpxchg(atomic64_t *ptr, long long old,
					long long new)
379
{
380
	long long oldval;
381 382 383 384 385 386
	unsigned long res;

	smp_mb();

	do {
		__asm__ __volatile__("@ atomic64_cmpxchg\n"
387
		"ldrexd		%1, %H1, [%3]\n"
388
		"mov		%0, #0\n"
389 390 391 392
		"teq		%1, %4\n"
		"teqeq		%H1, %H4\n"
		"strexdeq	%0, %5, %H5, [%3]"
		: "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
393 394 395 396 397 398 399 400 401
		: "r" (&ptr->counter), "r" (old), "r" (new)
		: "cc");
	} while (res);

	smp_mb();

	return oldval;
}

402
static inline long long atomic64_xchg(atomic64_t *ptr, long long new)
403
{
404
	long long result;
405 406 407 408 409
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_xchg\n"
410 411
"1:	ldrexd	%0, %H0, [%3]\n"
"	strexd	%1, %4, %H4, [%3]\n"
412 413
"	teq	%1, #0\n"
"	bne	1b"
414
	: "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter)
415 416 417 418 419 420 421 422
	: "r" (&ptr->counter), "r" (new)
	: "cc");

	smp_mb();

	return result;
}

423
static inline long long atomic64_dec_if_positive(atomic64_t *v)
424
{
425
	long long result;
426 427 428 429 430
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_dec_if_positive\n"
431
"1:	ldrexd	%0, %H0, [%3]\n"
432 433 434 435
"	subs	%0, %0, #1\n"
"	sbc	%H0, %H0, #0\n"
"	teq	%H0, #0\n"
"	bmi	2f\n"
436
"	strexd	%1, %0, %H0, [%3]\n"
437 438 439
"	teq	%1, #0\n"
"	bne	1b\n"
"2:"
440
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
441 442 443 444 445 446 447 448
	: "r" (&v->counter)
	: "cc");

	smp_mb();

	return result;
}

449
static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
450
{
451
	long long val;
452 453 454 455 456 457
	unsigned long tmp;
	int ret = 1;

	smp_mb();

	__asm__ __volatile__("@ atomic64_add_unless\n"
458 459 460
"1:	ldrexd	%0, %H0, [%4]\n"
"	teq	%0, %5\n"
"	teqeq	%H0, %H5\n"
461 462
"	moveq	%1, #0\n"
"	beq	2f\n"
463 464 465
"	adds	%0, %0, %6\n"
"	adc	%H0, %H0, %H6\n"
"	strexd	%2, %0, %H0, [%4]\n"
466 467 468
"	teq	%2, #0\n"
"	bne	1b\n"
"2:"
469
	: "=&r" (val), "+r" (ret), "=&r" (tmp), "+Qo" (v->counter)
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	: "r" (&v->counter), "r" (u), "r" (a)
	: "cc");

	if (ret)
		smp_mb();

	return ret;
}

#define atomic64_add_negative(a, v)	(atomic64_add_return((a), (v)) < 0)
#define atomic64_inc(v)			atomic64_add(1LL, (v))
#define atomic64_inc_return(v)		atomic64_add_return(1LL, (v))
#define atomic64_inc_and_test(v)	(atomic64_inc_return(v) == 0)
#define atomic64_sub_and_test(a, v)	(atomic64_sub_return((a), (v)) == 0)
#define atomic64_dec(v)			atomic64_sub(1LL, (v))
#define atomic64_dec_return(v)		atomic64_sub_return(1LL, (v))
#define atomic64_dec_and_test(v)	(atomic64_dec_return((v)) == 0)
#define atomic64_inc_not_zero(v)	atomic64_add_unless((v), 1LL, 0LL)

489
#endif /* !CONFIG_GENERIC_ATOMIC64 */
L
Linus Torvalds 已提交
490 491
#endif
#endif