atomic.h 9.7 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 121
	int oldval;
	unsigned long res;
N
Nick Piggin 已提交
122

123 124
	smp_mb();

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

136 137
	smp_mb();

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

L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151
#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;

152
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
153 154
	val = v->counter;
	v->counter = val += i;
155
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
156 157 158

	return val;
}
159
#define atomic_add(i, v)	(void) atomic_add_return(i, v)
L
Linus Torvalds 已提交
160 161 162 163 164 165

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

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

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

N
Nick Piggin 已提交
175 176 177 178 179
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
{
	int ret;
	unsigned long flags;

180
	raw_local_irq_save(flags);
N
Nick Piggin 已提交
181 182 183
	ret = v->counter;
	if (likely(ret == old))
		v->counter = new;
184
	raw_local_irq_restore(flags);
N
Nick Piggin 已提交
185 186 187 188

	return ret;
}

L
Linus Torvalds 已提交
189 190
#endif /* __LINUX_ARM_ARCH__ */

191 192
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))

193
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
N
Nick Piggin 已提交
194 195 196 197 198 199
{
	int c, old;

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

203 204
#define atomic_inc(v)		atomic_add(1, v)
#define atomic_dec(v)		atomic_sub(1, v)
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213

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

214 215 216 217
#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 已提交
218

219 220
#ifndef CONFIG_GENERIC_ATOMIC64
typedef struct {
221
	long long counter;
222 223 224 225
} atomic64_t;

#define ATOMIC64_INIT(i) { (i) }

226
#ifdef CONFIG_ARM_LPAE
227
static inline long long atomic64_read(const atomic64_t *v)
228
{
229
	long long result;
230 231 232 233 234 235 236 237 238 239

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

	return result;
}

240
static inline void atomic64_set(atomic64_t *v, long long i)
241 242 243 244 245 246 247 248
{
	__asm__ __volatile__("@ atomic64_set\n"
"	strd	%2, %H2, [%1]"
	: "=Qo" (v->counter)
	: "r" (&v->counter), "r" (i)
	);
}
#else
249
static inline long long atomic64_read(const atomic64_t *v)
250
{
251
	long long result;
252 253 254 255

	__asm__ __volatile__("@ atomic64_read\n"
"	ldrexd	%0, %H0, [%1]"
	: "=&r" (result)
256
	: "r" (&v->counter), "Qo" (v->counter)
257 258 259 260 261
	);

	return result;
}

262
static inline void atomic64_set(atomic64_t *v, long long i)
263
{
264
	long long tmp;
265

266
	prefetchw(&v->counter);
267
	__asm__ __volatile__("@ atomic64_set\n"
268 269
"1:	ldrexd	%0, %H0, [%2]\n"
"	strexd	%0, %3, %H3, [%2]\n"
270 271
"	teq	%0, #0\n"
"	bne	1b"
272
	: "=&r" (tmp), "=Qo" (v->counter)
273 274 275
	: "r" (&v->counter), "r" (i)
	: "cc");
}
276
#endif
277

278
static inline void atomic64_add(long long i, atomic64_t *v)
279
{
280
	long long result;
281 282
	unsigned long tmp;

283
	prefetchw(&v->counter);
284
	__asm__ __volatile__("@ atomic64_add\n"
285
"1:	ldrexd	%0, %H0, [%3]\n"
286 287
"	adds	%Q0, %Q0, %Q4\n"
"	adc	%R0, %R0, %R4\n"
288
"	strexd	%1, %0, %H0, [%3]\n"
289 290
"	teq	%1, #0\n"
"	bne	1b"
291
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
292 293 294 295
	: "r" (&v->counter), "r" (i)
	: "cc");
}

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

	smp_mb();

	__asm__ __volatile__("@ atomic64_add_return\n"
304
"1:	ldrexd	%0, %H0, [%3]\n"
305 306
"	adds	%Q0, %Q0, %Q4\n"
"	adc	%R0, %R0, %R4\n"
307
"	strexd	%1, %0, %H0, [%3]\n"
308 309
"	teq	%1, #0\n"
"	bne	1b"
310
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
311 312 313 314 315 316 317 318
	: "r" (&v->counter), "r" (i)
	: "cc");

	smp_mb();

	return result;
}

319
static inline void atomic64_sub(long long i, atomic64_t *v)
320
{
321
	long long result;
322 323
	unsigned long tmp;

324
	prefetchw(&v->counter);
325
	__asm__ __volatile__("@ atomic64_sub\n"
326
"1:	ldrexd	%0, %H0, [%3]\n"
327 328
"	subs	%Q0, %Q0, %Q4\n"
"	sbc	%R0, %R0, %R4\n"
329
"	strexd	%1, %0, %H0, [%3]\n"
330 331
"	teq	%1, #0\n"
"	bne	1b"
332
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
333 334 335 336
	: "r" (&v->counter), "r" (i)
	: "cc");
}

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

	smp_mb();

	__asm__ __volatile__("@ atomic64_sub_return\n"
345
"1:	ldrexd	%0, %H0, [%3]\n"
346 347
"	subs	%Q0, %Q0, %Q4\n"
"	sbc	%R0, %R0, %R4\n"
348
"	strexd	%1, %0, %H0, [%3]\n"
349 350
"	teq	%1, #0\n"
"	bne	1b"
351
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
352 353 354 355 356 357 358 359
	: "r" (&v->counter), "r" (i)
	: "cc");

	smp_mb();

	return result;
}

360 361
static inline long long atomic64_cmpxchg(atomic64_t *ptr, long long old,
					long long new)
362
{
363
	long long oldval;
364 365 366 367 368 369
	unsigned long res;

	smp_mb();

	do {
		__asm__ __volatile__("@ atomic64_cmpxchg\n"
370
		"ldrexd		%1, %H1, [%3]\n"
371
		"mov		%0, #0\n"
372 373 374 375
		"teq		%1, %4\n"
		"teqeq		%H1, %H4\n"
		"strexdeq	%0, %5, %H5, [%3]"
		: "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
376 377 378 379 380 381 382 383 384
		: "r" (&ptr->counter), "r" (old), "r" (new)
		: "cc");
	} while (res);

	smp_mb();

	return oldval;
}

385
static inline long long atomic64_xchg(atomic64_t *ptr, long long new)
386
{
387
	long long result;
388 389 390 391 392
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_xchg\n"
393 394
"1:	ldrexd	%0, %H0, [%3]\n"
"	strexd	%1, %4, %H4, [%3]\n"
395 396
"	teq	%1, #0\n"
"	bne	1b"
397
	: "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter)
398 399 400 401 402 403 404 405
	: "r" (&ptr->counter), "r" (new)
	: "cc");

	smp_mb();

	return result;
}

406
static inline long long atomic64_dec_if_positive(atomic64_t *v)
407
{
408
	long long result;
409 410 411 412 413
	unsigned long tmp;

	smp_mb();

	__asm__ __volatile__("@ atomic64_dec_if_positive\n"
414
"1:	ldrexd	%0, %H0, [%3]\n"
415 416 417
"	subs	%Q0, %Q0, #1\n"
"	sbc	%R0, %R0, #0\n"
"	teq	%R0, #0\n"
418
"	bmi	2f\n"
419
"	strexd	%1, %0, %H0, [%3]\n"
420 421 422
"	teq	%1, #0\n"
"	bne	1b\n"
"2:"
423
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
424 425 426 427 428 429 430 431
	: "r" (&v->counter)
	: "cc");

	smp_mb();

	return result;
}

432
static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
433
{
434
	long long val;
435 436 437 438 439 440
	unsigned long tmp;
	int ret = 1;

	smp_mb();

	__asm__ __volatile__("@ atomic64_add_unless\n"
441 442 443
"1:	ldrexd	%0, %H0, [%4]\n"
"	teq	%0, %5\n"
"	teqeq	%H0, %H5\n"
444 445
"	moveq	%1, #0\n"
"	beq	2f\n"
446 447
"	adds	%Q0, %Q0, %Q6\n"
"	adc	%R0, %R0, %R6\n"
448
"	strexd	%2, %0, %H0, [%4]\n"
449 450 451
"	teq	%2, #0\n"
"	bne	1b\n"
"2:"
452
	: "=&r" (val), "+r" (ret), "=&r" (tmp), "+Qo" (v->counter)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	: "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)

472
#endif /* !CONFIG_GENERIC_ATOMIC64 */
L
Linus Torvalds 已提交
473 474
#endif
#endif