atomic.h 9.2 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.
 */
L
Linus Torvalds 已提交
27
#define atomic_read(v)	((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 43 44 45 46 47 48 49 50 51 52
static inline void atomic_add(int i, atomic_t *v)
{
	unsigned long tmp;
	int result;

	__asm__ __volatile__("@ atomic_add\n"
"1:	ldrex	%0, [%2]\n"
"	add	%0, %0, %3\n"
"	strex	%1, %0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "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 61 62 63 64 65 66 67 68 69
	__asm__ __volatile__("@ atomic_add_return\n"
"1:	ldrex	%0, [%2]\n"
"	add	%0, %0, %3\n"
"	strex	%1, %0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "r" (&v->counter), "Ir" (i)
	: "cc");

70 71
	smp_mb();

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

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static inline void atomic_sub(int i, atomic_t *v)
{
	unsigned long tmp;
	int result;

	__asm__ __volatile__("@ atomic_sub\n"
"1:	ldrex	%0, [%2]\n"
"	sub	%0, %0, %3\n"
"	strex	%1, %0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "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 99 100 101 102 103 104 105 106 107
	__asm__ __volatile__("@ atomic_sub_return\n"
"1:	ldrex	%0, [%2]\n"
"	sub	%0, %0, %3\n"
"	strex	%1, %0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "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 121
	do {
		__asm__ __volatile__("@ atomic_cmpxchg\n"
		"ldrex	%1, [%2]\n"
122
		"mov	%0, #0\n"
N
Nick Piggin 已提交
123 124 125 126 127 128 129
		"teq	%1, %3\n"
		"strexeq %0, %4, [%2]\n"
		    : "=&r" (res), "=&r" (oldval)
		    : "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
"1:	ldrex	%0, [%2]\n"
L
Linus Torvalds 已提交
141
"	bic	%0, %0, %3\n"
142
"	strex	%1, %0, [%2]\n"
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (tmp), "=&r" (tmp2)
	: "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 221
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;
}
#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

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

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

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

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
#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)
	: "r" (&v->counter)
	);

	return result;
}

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

	__asm__ __volatile__("@ atomic64_set\n"
"1:	ldrexd	%0, %H0, [%1]\n"
"	strexd	%0, %2, %H2, [%1]\n"
"	teq	%0, #0\n"
"	bne	1b"
	: "=&r" (tmp)
	: "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"
"1:	ldrexd	%0, %H0, [%2]\n"
"	adds	%0, %0, %3\n"
"	adc	%H0, %H0, %H3\n"
"	strexd	%1, %0, %H0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "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"
"1:	ldrexd	%0, %H0, [%2]\n"
"	adds	%0, %0, %3\n"
"	adc	%H0, %H0, %H3\n"
"	strexd	%1, %0, %H0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "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"
"1:	ldrexd	%0, %H0, [%2]\n"
"	subs	%0, %0, %3\n"
"	sbc	%H0, %H0, %H3\n"
"	strexd	%1, %0, %H0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "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"
"1:	ldrexd	%0, %H0, [%2]\n"
"	subs	%0, %0, %3\n"
"	sbc	%H0, %H0, %H3\n"
"	strexd	%1, %0, %H0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "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"
		"ldrexd		%1, %H1, [%2]\n"
		"mov		%0, #0\n"
		"teq		%1, %3\n"
		"teqeq		%H1, %H3\n"
		"strexdeq	%0, %4, %H4, [%2]"
		: "=&r" (res), "=&r" (oldval)
		: "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"
"1:	ldrexd	%0, %H0, [%2]\n"
"	strexd	%1, %3, %H3, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (result), "=&r" (tmp)
	: "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"
"1:	ldrexd	%0, %H0, [%2]\n"
"	subs	%0, %0, #1\n"
"	sbc	%H0, %H0, #0\n"
"	teq	%H0, #0\n"
"	bmi	2f\n"
"	strexd	%1, %0, %H0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b\n"
"2:"
	: "=&r" (result), "=&r" (tmp)
	: "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"
"1:	ldrexd	%0, %H0, [%3]\n"
"	teq	%0, %4\n"
"	teqeq	%H0, %H4\n"
"	moveq	%1, #0\n"
"	beq	2f\n"
"	adds	%0, %0, %5\n"
"	adc	%H0, %H0, %H5\n"
"	strexd	%2, %0, %H0, [%3]\n"
"	teq	%2, #0\n"
"	bne	1b\n"
"2:"
	: "=&r" (val), "=&r" (ret), "=&r" (tmp)
	: "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
466
#include <asm-generic/atomic-long.h>
L
Linus Torvalds 已提交
467 468
#endif
#endif