atomic.h 9.5 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
#include <asm/system.h>
L
Linus Torvalds 已提交
17 18 19 20 21

#define ATOMIC_INIT(i)	{ (i) }

#ifdef __KERNEL__

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

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

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

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

58 59
	smp_mb();

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

70 71
	smp_mb();

L
Linus Torvalds 已提交
72 73 74
	return result;
}

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

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

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

96 97
	smp_mb();

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

108 109
	smp_mb();

L
Linus Torvalds 已提交
110 111 112
	return result;
}

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

117 118
	smp_mb();

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

130 131
	smp_mb();

N
Nick Piggin 已提交
132 133 134
	return oldval;
}

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

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

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

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

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

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

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

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

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

	return ret;
}

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

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

#endif /* __LINUX_ARM_ARCH__ */

209 210
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))

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

	c = atomic_read(v);
	while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
		c = old;
	return c != u;
}

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

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

232 233 234 235
#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 已提交
236

237 238 239 240 241 242 243 244 245 246 247 248 249 250
#ifndef CONFIG_GENERIC_ATOMIC64
typedef struct {
	u64 __aligned(8) counter;
} atomic64_t;

#define ATOMIC64_INIT(i) { (i) }

static inline u64 atomic64_read(atomic64_t *v)
{
	u64 result;

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

	return result;
}

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

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

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

	__asm__ __volatile__("@ atomic64_add\n"
277 278 279 280
"1:	ldrexd	%0, %H0, [%3]\n"
"	adds	%0, %0, %4\n"
"	adc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
281 282
"	teq	%1, #0\n"
"	bne	1b"
283
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
284 285 286 287 288 289 290 291 292 293 294 295
	: "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"
296 297 298 299
"1:	ldrexd	%0, %H0, [%3]\n"
"	adds	%0, %0, %4\n"
"	adc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
300 301
"	teq	%1, #0\n"
"	bne	1b"
302
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
303 304 305 306 307 308 309 310 311 312 313 314 315 316
	: "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;

	__asm__ __volatile__("@ atomic64_sub\n"
317 318 319 320
"1:	ldrexd	%0, %H0, [%3]\n"
"	subs	%0, %0, %4\n"
"	sbc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
321 322
"	teq	%1, #0\n"
"	bne	1b"
323
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
324 325 326 327 328 329 330 331 332 333 334 335
	: "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"
336 337 338 339
"1:	ldrexd	%0, %H0, [%3]\n"
"	subs	%0, %0, %4\n"
"	sbc	%H0, %H0, %H4\n"
"	strexd	%1, %0, %H0, [%3]\n"
340 341
"	teq	%1, #0\n"
"	bne	1b"
342
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	: "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"
360
		"ldrexd		%1, %H1, [%3]\n"
361
		"mov		%0, #0\n"
362 363 364 365
		"teq		%1, %4\n"
		"teqeq		%H1, %H4\n"
		"strexdeq	%0, %5, %H5, [%3]"
		: "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
		: "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"
383 384
"1:	ldrexd	%0, %H0, [%3]\n"
"	strexd	%1, %4, %H4, [%3]\n"
385 386
"	teq	%1, #0\n"
"	bne	1b"
387
	: "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter)
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
	: "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"
404
"1:	ldrexd	%0, %H0, [%3]\n"
405 406 407 408
"	subs	%0, %0, #1\n"
"	sbc	%H0, %H0, #0\n"
"	teq	%H0, #0\n"
"	bmi	2f\n"
409
"	strexd	%1, %0, %H0, [%3]\n"
410 411 412
"	teq	%1, #0\n"
"	bne	1b\n"
"2:"
413
	: "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	: "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"
431 432 433
"1:	ldrexd	%0, %H0, [%4]\n"
"	teq	%0, %5\n"
"	teqeq	%H0, %H5\n"
434 435
"	moveq	%1, #0\n"
"	beq	2f\n"
436 437 438
"	adds	%0, %0, %6\n"
"	adc	%H0, %H0, %H6\n"
"	strexd	%2, %0, %H0, [%4]\n"
439 440 441
"	teq	%2, #0\n"
"	bne	1b\n"
"2:"
442
	: "=&r" (val), "+r" (ret), "=&r" (tmp), "+Qo" (v->counter)
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	: "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)

#else /* !CONFIG_GENERIC_ATOMIC64 */
#include <asm-generic/atomic64.h>
#endif
465
#include <asm-generic/atomic-long.h>
L
Linus Torvalds 已提交
466 467
#endif
#endif