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/prefetch.h>
16
#include <linux/types.h>
17 18 19
#include <linux/irqflags.h>
#include <asm/barrier.h>
#include <asm/cmpxchg.h>
L
Linus Torvalds 已提交
20 21 22 23 24

#define ATOMIC_INIT(i)	{ (i) }

#ifdef __KERNEL__

25 26 27 28 29
/*
 * 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.
 */
30
#define atomic_read(v)	(*(volatile int *)&(v)->counter)
31
#define atomic_set(v,i)	(((v)->counter) = (i))
L
Linus Torvalds 已提交
32 33 34 35 36 37

#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
38
 * to ensure that the update happens.
L
Linus Torvalds 已提交
39
 */
40 41 42 43 44
static inline void atomic_add(int i, atomic_t *v)
{
	unsigned long tmp;
	int result;

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

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

62 63
	smp_mb();

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

74 75
	smp_mb();

L
Linus Torvalds 已提交
76 77 78
	return result;
}

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

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

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

101 102
	smp_mb();

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

113 114
	smp_mb();

L
Linus Torvalds 已提交
115 116 117
	return result;
}

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

122 123
	smp_mb();

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

135 136
	smp_mb();

N
Nick Piggin 已提交
137 138 139
	return oldval;
}

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

144
	prefetchw(addr);
L
Linus Torvalds 已提交
145
	__asm__ __volatile__("@ atomic_clear_mask\n"
146 147 148
"1:	ldrex	%0, [%3]\n"
"	bic	%0, %0, %4\n"
"	strex	%1, %0, [%3]\n"
L
Linus Torvalds 已提交
149 150
"	teq	%1, #0\n"
"	bne	1b"
151
	: "=&r" (tmp), "=&r" (tmp2), "+Qo" (*addr)
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	: "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;

167
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
168 169
	val = v->counter;
	v->counter = val += i;
170
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
171 172 173

	return val;
}
174
#define atomic_add(i, v)	(void) atomic_add_return(i, v)
L
Linus Torvalds 已提交
175 176 177 178 179 180

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

181
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
182 183
	val = v->counter;
	v->counter = val -= i;
184
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
185 186 187

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

N
Nick Piggin 已提交
190 191 192 193 194
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
{
	int ret;
	unsigned long flags;

195
	raw_local_irq_save(flags);
N
Nick Piggin 已提交
196 197 198
	ret = v->counter;
	if (likely(ret == old))
		v->counter = new;
199
	raw_local_irq_restore(flags);
N
Nick Piggin 已提交
200 201 202 203

	return ret;
}

L
Linus Torvalds 已提交
204 205 206 207
static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
{
	unsigned long flags;

208
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
209
	*addr &= ~mask;
210
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
211 212 213 214
}

#endif /* __LINUX_ARM_ARCH__ */

215 216
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))

217
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
N
Nick Piggin 已提交
218 219 220 221 222 223
{
	int c, old;

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

227 228
#define atomic_inc(v)		atomic_add(1, v)
#define atomic_dec(v)		atomic_sub(1, v)
L
Linus Torvalds 已提交
229 230 231 232 233 234 235 236 237

#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)

238 239 240 241
#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 已提交
242

243 244 245 246 247 248 249
#ifndef CONFIG_GENERIC_ATOMIC64
typedef struct {
	u64 __aligned(8) counter;
} atomic64_t;

#define ATOMIC64_INIT(i) { (i) }

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
#ifdef CONFIG_ARM_LPAE
static inline u64 atomic64_read(const atomic64_t *v)
{
	u64 result;

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

	return result;
}

static inline void atomic64_set(atomic64_t *v, u64 i)
{
	__asm__ __volatile__("@ atomic64_set\n"
"	strd	%2, %H2, [%1]"
	: "=Qo" (v->counter)
	: "r" (&v->counter), "r" (i)
	);
}
#else
273
static inline u64 atomic64_read(const atomic64_t *v)
274 275 276 277 278 279
{
	u64 result;

	__asm__ __volatile__("@ atomic64_read\n"
"	ldrexd	%0, %H0, [%1]"
	: "=&r" (result)
280
	: "r" (&v->counter), "Qo" (v->counter)
281 282 283 284 285 286 287 288 289
	);

	return result;
}

static inline void atomic64_set(atomic64_t *v, u64 i)
{
	u64 tmp;

290
	prefetchw(&v->counter);
291
	__asm__ __volatile__("@ atomic64_set\n"
292 293
"1:	ldrexd	%0, %H0, [%2]\n"
"	strexd	%0, %3, %H3, [%2]\n"
294 295
"	teq	%0, #0\n"
"	bne	1b"
296
	: "=&r" (tmp), "=Qo" (v->counter)
297 298 299
	: "r" (&v->counter), "r" (i)
	: "cc");
}
300
#endif
301 302 303 304 305 306

static inline void atomic64_add(u64 i, atomic64_t *v)
{
	u64 result;
	unsigned long tmp;

307
	prefetchw(&v->counter);
308
	__asm__ __volatile__("@ atomic64_add\n"
309 310 311 312
"1:	ldrexd	%0, %H0, [%3]\n"
"	adds	%0, %0, %4\n"
"	adc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
313 314
"	teq	%1, #0\n"
"	bne	1b"
315
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
316 317 318 319 320 321 322 323 324 325 326 327
	: "r" (&v->counter), "r" (i)
	: "cc");
}

static inline u64 atomic64_add_return(u64 i, atomic64_t *v)
{
	u64 result;
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_add_return\n"
328 329 330 331
"1:	ldrexd	%0, %H0, [%3]\n"
"	adds	%0, %0, %4\n"
"	adc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
332 333
"	teq	%1, #0\n"
"	bne	1b"
334
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
335 336 337 338 339 340 341 342 343 344 345 346 347
	: "r" (&v->counter), "r" (i)
	: "cc");

	smp_mb();

	return result;
}

static inline void atomic64_sub(u64 i, atomic64_t *v)
{
	u64 result;
	unsigned long tmp;

348
	prefetchw(&v->counter);
349
	__asm__ __volatile__("@ atomic64_sub\n"
350 351 352 353
"1:	ldrexd	%0, %H0, [%3]\n"
"	subs	%0, %0, %4\n"
"	sbc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
354 355
"	teq	%1, #0\n"
"	bne	1b"
356
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
357 358 359 360 361 362 363 364 365 366 367 368
	: "r" (&v->counter), "r" (i)
	: "cc");
}

static inline u64 atomic64_sub_return(u64 i, atomic64_t *v)
{
	u64 result;
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_sub_return\n"
369 370 371 372
"1:	ldrexd	%0, %H0, [%3]\n"
"	subs	%0, %0, %4\n"
"	sbc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
373 374
"	teq	%1, #0\n"
"	bne	1b"
375
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	: "r" (&v->counter), "r" (i)
	: "cc");

	smp_mb();

	return result;
}

static inline u64 atomic64_cmpxchg(atomic64_t *ptr, u64 old, u64 new)
{
	u64 oldval;
	unsigned long res;

	smp_mb();

	do {
		__asm__ __volatile__("@ atomic64_cmpxchg\n"
393
		"ldrexd		%1, %H1, [%3]\n"
394
		"mov		%0, #0\n"
395 396 397 398
		"teq		%1, %4\n"
		"teqeq		%H1, %H4\n"
		"strexdeq	%0, %5, %H5, [%3]"
		: "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
		: "r" (&ptr->counter), "r" (old), "r" (new)
		: "cc");
	} while (res);

	smp_mb();

	return oldval;
}

static inline u64 atomic64_xchg(atomic64_t *ptr, u64 new)
{
	u64 result;
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_xchg\n"
416 417
"1:	ldrexd	%0, %H0, [%3]\n"
"	strexd	%1, %4, %H4, [%3]\n"
418 419
"	teq	%1, #0\n"
"	bne	1b"
420
	: "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter)
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	: "r" (&ptr->counter), "r" (new)
	: "cc");

	smp_mb();

	return result;
}

static inline u64 atomic64_dec_if_positive(atomic64_t *v)
{
	u64 result;
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_dec_if_positive\n"
437
"1:	ldrexd	%0, %H0, [%3]\n"
438 439 440 441
"	subs	%0, %0, #1\n"
"	sbc	%H0, %H0, #0\n"
"	teq	%H0, #0\n"
"	bmi	2f\n"
442
"	strexd	%1, %0, %H0, [%3]\n"
443 444 445
"	teq	%1, #0\n"
"	bne	1b\n"
"2:"
446
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	: "r" (&v->counter)
	: "cc");

	smp_mb();

	return result;
}

static inline int atomic64_add_unless(atomic64_t *v, u64 a, u64 u)
{
	u64 val;
	unsigned long tmp;
	int ret = 1;

	smp_mb();

	__asm__ __volatile__("@ atomic64_add_unless\n"
464 465 466
"1:	ldrexd	%0, %H0, [%4]\n"
"	teq	%0, %5\n"
"	teqeq	%H0, %H5\n"
467 468
"	moveq	%1, #0\n"
"	beq	2f\n"
469 470 471
"	adds	%0, %0, %6\n"
"	adc	%H0, %H0, %H6\n"
"	strexd	%2, %0, %H0, [%4]\n"
472 473 474
"	teq	%2, #0\n"
"	bne	1b\n"
"2:"
475
	: "=&r" (val), "+r" (ret), "=&r" (tmp), "+Qo" (v->counter)
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	: "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)

495
#endif /* !CONFIG_GENERIC_ATOMIC64 */
L
Linus Torvalds 已提交
496 497
#endif
#endif