atomic.h 14.7 KB
Newer Older
P
Palmer Dabbelt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Copyright (C) 2012 Regents of the University of California
 * Copyright (C) 2017 SiFive
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */

#ifndef _ASM_RISCV_ATOMIC_H
#define _ASM_RISCV_ATOMIC_H

#ifdef CONFIG_GENERIC_ATOMIC64
# include <asm-generic/atomic64.h>
#else
# if (__riscv_xlen < 64)
#  error "64-bit atomics require XLEN to be at least 64"
# endif
#endif

#include <asm/cmpxchg.h>
#include <asm/barrier.h>

#define ATOMIC_INIT(i)	{ (i) }
27 28 29 30 31 32 33 34 35 36 37 38 39 40

#define __atomic_op_acquire(op, args...)				\
({									\
	typeof(op##_relaxed(args)) __ret  = op##_relaxed(args);		\
	__asm__ __volatile__(RISCV_ACQUIRE_BARRIER "" ::: "memory");	\
	__ret;								\
})

#define __atomic_op_release(op, args...)				\
({									\
	__asm__ __volatile__(RISCV_RELEASE_BARRIER "" ::: "memory");	\
	op##_relaxed(args);						\
})

P
Palmer Dabbelt 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
static __always_inline int atomic_read(const atomic_t *v)
{
	return READ_ONCE(v->counter);
}
static __always_inline void atomic_set(atomic_t *v, int i)
{
	WRITE_ONCE(v->counter, i);
}

#ifndef CONFIG_GENERIC_ATOMIC64
#define ATOMIC64_INIT(i) { (i) }
static __always_inline long atomic64_read(const atomic64_t *v)
{
	return READ_ONCE(v->counter);
}
static __always_inline void atomic64_set(atomic64_t *v, long i)
{
	WRITE_ONCE(v->counter, i);
}
#endif

/*
 * First, the atomic ops that have no ordering constraints and therefor don't
 * have the AQ or RL bits set.  These don't return anything, so there's only
 * one version to worry about.
 */
67 68 69 70 71 72 73 74 75 76
#define ATOMIC_OP(op, asm_op, I, asm_type, c_type, prefix)		\
static __always_inline							\
void atomic##prefix##_##op(c_type i, atomic##prefix##_t *v)		\
{									\
	__asm__ __volatile__ (						\
		"	amo" #asm_op "." #asm_type " zero, %1, %0"	\
		: "+A" (v->counter)					\
		: "r" (I)						\
		: "memory");						\
}									\
P
Palmer Dabbelt 已提交
77 78

#ifdef CONFIG_GENERIC_ATOMIC64
79
#define ATOMIC_OPS(op, asm_op, I)					\
80
        ATOMIC_OP (op, asm_op, I, w,  int,   )
P
Palmer Dabbelt 已提交
81
#else
82 83
#define ATOMIC_OPS(op, asm_op, I)					\
        ATOMIC_OP (op, asm_op, I, w,  int,   )				\
84
        ATOMIC_OP (op, asm_op, I, d, long, 64)
P
Palmer Dabbelt 已提交
85 86
#endif

87 88 89 90 91
ATOMIC_OPS(add, add,  i)
ATOMIC_OPS(sub, add, -i)
ATOMIC_OPS(and, and,  i)
ATOMIC_OPS( or,  or,  i)
ATOMIC_OPS(xor, xor,  i)
P
Palmer Dabbelt 已提交
92 93 94 95 96

#undef ATOMIC_OP
#undef ATOMIC_OPS

/*
97
 * Atomic ops that have ordered, relaxed, acquire, and release variants.
P
Palmer Dabbelt 已提交
98 99 100
 * There's two flavors of these: the arithmatic ops have both fetch and return
 * versions, while the logical ops only have fetch versions.
 */
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#define ATOMIC_FETCH_OP(op, asm_op, I, asm_type, c_type, prefix)	\
static __always_inline							\
c_type atomic##prefix##_fetch_##op##_relaxed(c_type i,			\
					     atomic##prefix##_t *v)	\
{									\
	register c_type ret;						\
	__asm__ __volatile__ (						\
		"	amo" #asm_op "." #asm_type " %1, %2, %0"	\
		: "+A" (v->counter), "=r" (ret)				\
		: "r" (I)						\
		: "memory");						\
	return ret;							\
}									\
static __always_inline							\
c_type atomic##prefix##_fetch_##op(c_type i, atomic##prefix##_t *v)	\
{									\
	register c_type ret;						\
	__asm__ __volatile__ (						\
		"	amo" #asm_op "." #asm_type ".aqrl  %1, %2, %0"	\
		: "+A" (v->counter), "=r" (ret)				\
		: "r" (I)						\
		: "memory");						\
	return ret;							\
P
Palmer Dabbelt 已提交
124 125
}

126 127 128 129 130 131 132 133 134 135 136
#define ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_type, c_type, prefix)	\
static __always_inline							\
c_type atomic##prefix##_##op##_return_relaxed(c_type i,			\
					      atomic##prefix##_t *v)	\
{									\
        return atomic##prefix##_fetch_##op##_relaxed(i, v) c_op I;	\
}									\
static __always_inline							\
c_type atomic##prefix##_##op##_return(c_type i, atomic##prefix##_t *v)	\
{									\
        return atomic##prefix##_fetch_##op(i, v) c_op I;		\
P
Palmer Dabbelt 已提交
137 138 139
}

#ifdef CONFIG_GENERIC_ATOMIC64
140 141 142
#define ATOMIC_OPS(op, asm_op, c_op, I)					\
        ATOMIC_FETCH_OP( op, asm_op,       I, w,  int,   )		\
        ATOMIC_OP_RETURN(op, asm_op, c_op, I, w,  int,   )
P
Palmer Dabbelt 已提交
143
#else
144 145 146 147 148
#define ATOMIC_OPS(op, asm_op, c_op, I)					\
        ATOMIC_FETCH_OP( op, asm_op,       I, w,  int,   )		\
        ATOMIC_OP_RETURN(op, asm_op, c_op, I, w,  int,   )		\
        ATOMIC_FETCH_OP( op, asm_op,       I, d, long, 64)		\
        ATOMIC_OP_RETURN(op, asm_op, c_op, I, d, long, 64)
P
Palmer Dabbelt 已提交
149 150
#endif

151 152 153 154 155 156 157
ATOMIC_OPS(add, add, +,  i)
ATOMIC_OPS(sub, add, +, -i)

#define atomic_add_return_relaxed	atomic_add_return_relaxed
#define atomic_sub_return_relaxed	atomic_sub_return_relaxed
#define atomic_add_return		atomic_add_return
#define atomic_sub_return		atomic_sub_return
P
Palmer Dabbelt 已提交
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#define atomic_fetch_add_relaxed	atomic_fetch_add_relaxed
#define atomic_fetch_sub_relaxed	atomic_fetch_sub_relaxed
#define atomic_fetch_add		atomic_fetch_add
#define atomic_fetch_sub		atomic_fetch_sub

#ifndef CONFIG_GENERIC_ATOMIC64
#define atomic64_add_return_relaxed	atomic64_add_return_relaxed
#define atomic64_sub_return_relaxed	atomic64_sub_return_relaxed
#define atomic64_add_return		atomic64_add_return
#define atomic64_sub_return		atomic64_sub_return

#define atomic64_fetch_add_relaxed	atomic64_fetch_add_relaxed
#define atomic64_fetch_sub_relaxed	atomic64_fetch_sub_relaxed
#define atomic64_fetch_add		atomic64_fetch_add
#define atomic64_fetch_sub		atomic64_fetch_sub
#endif
P
Palmer Dabbelt 已提交
175 176 177 178

#undef ATOMIC_OPS

#ifdef CONFIG_GENERIC_ATOMIC64
179 180
#define ATOMIC_OPS(op, asm_op, I)					\
        ATOMIC_FETCH_OP(op, asm_op, I, w,  int,   )
P
Palmer Dabbelt 已提交
181
#else
182 183 184
#define ATOMIC_OPS(op, asm_op, I)					\
        ATOMIC_FETCH_OP(op, asm_op, I, w,  int,   )			\
        ATOMIC_FETCH_OP(op, asm_op, I, d, long, 64)
P
Palmer Dabbelt 已提交
185 186
#endif

187 188 189
ATOMIC_OPS(and, and, i)
ATOMIC_OPS( or,  or, i)
ATOMIC_OPS(xor, xor, i)
P
Palmer Dabbelt 已提交
190

191 192 193 194 195 196
#define atomic_fetch_and_relaxed	atomic_fetch_and_relaxed
#define atomic_fetch_or_relaxed		atomic_fetch_or_relaxed
#define atomic_fetch_xor_relaxed	atomic_fetch_xor_relaxed
#define atomic_fetch_and		atomic_fetch_and
#define atomic_fetch_or			atomic_fetch_or
#define atomic_fetch_xor		atomic_fetch_xor
P
Palmer Dabbelt 已提交
197

198 199 200 201 202 203 204 205
#ifndef CONFIG_GENERIC_ATOMIC64
#define atomic64_fetch_and_relaxed	atomic64_fetch_and_relaxed
#define atomic64_fetch_or_relaxed	atomic64_fetch_or_relaxed
#define atomic64_fetch_xor_relaxed	atomic64_fetch_xor_relaxed
#define atomic64_fetch_and		atomic64_fetch_and
#define atomic64_fetch_or		atomic64_fetch_or
#define atomic64_fetch_xor		atomic64_fetch_xor
#endif
P
Palmer Dabbelt 已提交
206 207 208 209 210 211 212 213 214

#undef ATOMIC_OPS

#undef ATOMIC_FETCH_OP
#undef ATOMIC_OP_RETURN

/*
 * The extra atomic operations that are constructed from one of the core
 * AMO-based operations above (aside from sub, which is easier to fit above).
215 216 217
 * These are required to perform a full barrier, but they're OK this way
 * because atomic_*_return is also required to perform a full barrier.
 *
P
Palmer Dabbelt 已提交
218
 */
219 220 221 222 223
#define ATOMIC_OP(op, func_op, comp_op, I, c_type, prefix)		\
static __always_inline							\
bool atomic##prefix##_##op(c_type i, atomic##prefix##_t *v)		\
{									\
	return atomic##prefix##_##func_op##_return(i, v) comp_op I;	\
P
Palmer Dabbelt 已提交
224 225 226
}

#ifdef CONFIG_GENERIC_ATOMIC64
227 228
#define ATOMIC_OPS(op, func_op, comp_op, I)				\
        ATOMIC_OP(op, func_op, comp_op, I,  int,   )
P
Palmer Dabbelt 已提交
229
#else
230 231 232
#define ATOMIC_OPS(op, func_op, comp_op, I)				\
        ATOMIC_OP(op, func_op, comp_op, I,  int,   )			\
        ATOMIC_OP(op, func_op, comp_op, I, long, 64)
P
Palmer Dabbelt 已提交
233 234 235 236 237 238 239 240 241
#endif

ATOMIC_OPS(add_and_test, add, ==, 0)
ATOMIC_OPS(sub_and_test, sub, ==, 0)
ATOMIC_OPS(add_negative, add,  <, 0)

#undef ATOMIC_OP
#undef ATOMIC_OPS

242 243 244 245 246
#define ATOMIC_OP(op, func_op, I, c_type, prefix)			\
static __always_inline							\
void atomic##prefix##_##op(atomic##prefix##_t *v)			\
{									\
	atomic##prefix##_##func_op(I, v);				\
P
Palmer Dabbelt 已提交
247 248
}

249 250 251 252 253 254 255 256 257 258
#define ATOMIC_FETCH_OP(op, func_op, I, c_type, prefix)			\
static __always_inline							\
c_type atomic##prefix##_fetch_##op##_relaxed(atomic##prefix##_t *v)	\
{									\
	return atomic##prefix##_fetch_##func_op##_relaxed(I, v);	\
}									\
static __always_inline							\
c_type atomic##prefix##_fetch_##op(atomic##prefix##_t *v)		\
{									\
	return atomic##prefix##_fetch_##func_op(I, v);			\
P
Palmer Dabbelt 已提交
259 260
}

261 262 263 264 265 266 267 268 269 270
#define ATOMIC_OP_RETURN(op, asm_op, c_op, I, c_type, prefix)		\
static __always_inline							\
c_type atomic##prefix##_##op##_return_relaxed(atomic##prefix##_t *v)	\
{									\
        return atomic##prefix##_fetch_##op##_relaxed(v) c_op I;		\
}									\
static __always_inline							\
c_type atomic##prefix##_##op##_return(atomic##prefix##_t *v)		\
{									\
        return atomic##prefix##_fetch_##op(v) c_op I;			\
P
Palmer Dabbelt 已提交
271 272 273
}

#ifdef CONFIG_GENERIC_ATOMIC64
274 275 276
#define ATOMIC_OPS(op, asm_op, c_op, I)					\
        ATOMIC_OP(       op, asm_op,       I,  int,   )			\
        ATOMIC_FETCH_OP( op, asm_op,       I,  int,   )			\
P
Palmer Dabbelt 已提交
277 278
        ATOMIC_OP_RETURN(op, asm_op, c_op, I,  int,   )
#else
279 280 281 282 283 284
#define ATOMIC_OPS(op, asm_op, c_op, I)					\
        ATOMIC_OP(       op, asm_op,       I,  int,   )			\
        ATOMIC_FETCH_OP( op, asm_op,       I,  int,   )			\
        ATOMIC_OP_RETURN(op, asm_op, c_op, I,  int,   )			\
        ATOMIC_OP(       op, asm_op,       I, long, 64)			\
        ATOMIC_FETCH_OP( op, asm_op,       I, long, 64)			\
P
Palmer Dabbelt 已提交
285 286 287 288 289 290
        ATOMIC_OP_RETURN(op, asm_op, c_op, I, long, 64)
#endif

ATOMIC_OPS(inc, add, +,  1)
ATOMIC_OPS(dec, add, +, -1)

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
#define atomic_inc_return_relaxed	atomic_inc_return_relaxed
#define atomic_dec_return_relaxed	atomic_dec_return_relaxed
#define atomic_inc_return		atomic_inc_return
#define atomic_dec_return		atomic_dec_return

#define atomic_fetch_inc_relaxed	atomic_fetch_inc_relaxed
#define atomic_fetch_dec_relaxed	atomic_fetch_dec_relaxed
#define atomic_fetch_inc		atomic_fetch_inc
#define atomic_fetch_dec		atomic_fetch_dec

#ifndef CONFIG_GENERIC_ATOMIC64
#define atomic64_inc_return_relaxed	atomic64_inc_return_relaxed
#define atomic64_dec_return_relaxed	atomic64_dec_return_relaxed
#define atomic64_inc_return		atomic64_inc_return
#define atomic64_dec_return		atomic64_dec_return

#define atomic64_fetch_inc_relaxed	atomic64_fetch_inc_relaxed
#define atomic64_fetch_dec_relaxed	atomic64_fetch_dec_relaxed
#define atomic64_fetch_inc		atomic64_fetch_inc
#define atomic64_fetch_dec		atomic64_fetch_dec
#endif

P
Palmer Dabbelt 已提交
313 314 315 316 317
#undef ATOMIC_OPS
#undef ATOMIC_OP
#undef ATOMIC_FETCH_OP
#undef ATOMIC_OP_RETURN

318 319 320 321 322
#define ATOMIC_OP(op, func_op, comp_op, I, prefix)			\
static __always_inline							\
bool atomic##prefix##_##op(atomic##prefix##_t *v)			\
{									\
	return atomic##prefix##_##func_op##_return(v) comp_op I;	\
P
Palmer Dabbelt 已提交
323 324 325 326 327 328 329 330 331 332 333
}

ATOMIC_OP(inc_and_test, inc, ==, 0,   )
ATOMIC_OP(dec_and_test, dec, ==, 0,   )
#ifndef CONFIG_GENERIC_ATOMIC64
ATOMIC_OP(inc_and_test, inc, ==, 0, 64)
ATOMIC_OP(dec_and_test, dec, ==, 0, 64)
#endif

#undef ATOMIC_OP

334
/* This is required to provide a full barrier on success. */
P
Palmer Dabbelt 已提交
335 336 337 338 339
static __always_inline int __atomic_add_unless(atomic_t *v, int a, int u)
{
       int prev, rc;

	__asm__ __volatile__ (
340 341 342 343 344 345 346
		"0:	lr.w     %[p],  %[c]\n"
		"	beq      %[p],  %[u], 1f\n"
		"	add      %[rc], %[p], %[a]\n"
		"	sc.w.rl  %[rc], %[rc], %[c]\n"
		"	bnez     %[rc], 0b\n"
		"	fence    rw, rw\n"
		"1:\n"
P
Palmer Dabbelt 已提交
347 348 349 350 351 352 353 354 355 356 357 358
		: [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
		: [a]"r" (a), [u]"r" (u)
		: "memory");
	return prev;
}

#ifndef CONFIG_GENERIC_ATOMIC64
static __always_inline long __atomic64_add_unless(atomic64_t *v, long a, long u)
{
       long prev, rc;

	__asm__ __volatile__ (
359 360 361 362 363 364 365
		"0:	lr.d     %[p],  %[c]\n"
		"	beq      %[p],  %[u], 1f\n"
		"	add      %[rc], %[p], %[a]\n"
		"	sc.d.rl  %[rc], %[rc], %[c]\n"
		"	bnez     %[rc], 0b\n"
		"	fence    rw, rw\n"
		"1:\n"
P
Palmer Dabbelt 已提交
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
		: [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
		: [a]"r" (a), [u]"r" (u)
		: "memory");
	return prev;
}

static __always_inline int atomic64_add_unless(atomic64_t *v, long a, long u)
{
	return __atomic64_add_unless(v, a, u) != u;
}
#endif

/*
 * The extra atomic operations that are constructed from one of the core
 * LR/SC-based operations above.
 */
static __always_inline int atomic_inc_not_zero(atomic_t *v)
{
        return __atomic_add_unless(v, 1, 0);
}

#ifndef CONFIG_GENERIC_ATOMIC64
static __always_inline long atomic64_inc_not_zero(atomic64_t *v)
{
        return atomic64_add_unless(v, 1, 0);
}
#endif

/*
 * atomic_{cmp,}xchg is required to have exactly the same ordering semantics as
396
 * {cmp,}xchg and the operations that return, so they need a full barrier.
P
Palmer Dabbelt 已提交
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
#define ATOMIC_OP(c_t, prefix, size)					\
static __always_inline							\
c_t atomic##prefix##_xchg_relaxed(atomic##prefix##_t *v, c_t n)		\
{									\
	return __xchg_relaxed(&(v->counter), n, size);			\
}									\
static __always_inline							\
c_t atomic##prefix##_xchg_acquire(atomic##prefix##_t *v, c_t n)		\
{									\
	return __xchg_acquire(&(v->counter), n, size);			\
}									\
static __always_inline							\
c_t atomic##prefix##_xchg_release(atomic##prefix##_t *v, c_t n)		\
{									\
	return __xchg_release(&(v->counter), n, size);			\
}									\
static __always_inline							\
c_t atomic##prefix##_xchg(atomic##prefix##_t *v, c_t n)			\
{									\
	return __xchg(&(v->counter), n, size);				\
}									\
static __always_inline							\
c_t atomic##prefix##_cmpxchg_relaxed(atomic##prefix##_t *v,		\
				     c_t o, c_t n)			\
{									\
	return __cmpxchg_relaxed(&(v->counter), o, n, size);		\
}									\
static __always_inline							\
c_t atomic##prefix##_cmpxchg_acquire(atomic##prefix##_t *v,		\
				     c_t o, c_t n)			\
{									\
	return __cmpxchg_acquire(&(v->counter), o, n, size);		\
}									\
static __always_inline							\
c_t atomic##prefix##_cmpxchg_release(atomic##prefix##_t *v,		\
				     c_t o, c_t n)			\
{									\
	return __cmpxchg_release(&(v->counter), o, n, size);		\
}									\
static __always_inline							\
c_t atomic##prefix##_cmpxchg(atomic##prefix##_t *v, c_t o, c_t n)	\
{									\
	return __cmpxchg(&(v->counter), o, n, size);			\
P
Palmer Dabbelt 已提交
441 442 443
}

#ifdef CONFIG_GENERIC_ATOMIC64
444 445
#define ATOMIC_OPS()							\
	ATOMIC_OP( int,   , 4)
P
Palmer Dabbelt 已提交
446
#else
447 448 449
#define ATOMIC_OPS()							\
	ATOMIC_OP( int,   , 4)						\
	ATOMIC_OP(long, 64, 8)
P
Palmer Dabbelt 已提交
450 451
#endif

452
ATOMIC_OPS()
P
Palmer Dabbelt 已提交
453 454 455 456 457 458 459 460 461

#undef ATOMIC_OPS
#undef ATOMIC_OP

static __always_inline int atomic_sub_if_positive(atomic_t *v, int offset)
{
       int prev, rc;

	__asm__ __volatile__ (
462 463 464 465 466 467 468
		"0:	lr.w     %[p],  %[c]\n"
		"	sub      %[rc], %[p], %[o]\n"
		"	bltz     %[rc], 1f\n"
		"	sc.w.rl  %[rc], %[rc], %[c]\n"
		"	bnez     %[rc], 0b\n"
		"	fence    rw, rw\n"
		"1:\n"
P
Palmer Dabbelt 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482
		: [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
		: [o]"r" (offset)
		: "memory");
	return prev - offset;
}

#define atomic_dec_if_positive(v)	atomic_sub_if_positive(v, 1)

#ifndef CONFIG_GENERIC_ATOMIC64
static __always_inline long atomic64_sub_if_positive(atomic64_t *v, int offset)
{
       long prev, rc;

	__asm__ __volatile__ (
483 484 485 486 487 488 489
		"0:	lr.d     %[p],  %[c]\n"
		"	sub      %[rc], %[p], %[o]\n"
		"	bltz     %[rc], 1f\n"
		"	sc.d.rl  %[rc], %[rc], %[c]\n"
		"	bnez     %[rc], 0b\n"
		"	fence    rw, rw\n"
		"1:\n"
P
Palmer Dabbelt 已提交
490 491 492 493 494 495 496 497 498 499
		: [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
		: [o]"r" (offset)
		: "memory");
	return prev - offset;
}

#define atomic64_dec_if_positive(v)	atomic64_sub_if_positive(v, 1)
#endif

#endif /* _ASM_RISCV_ATOMIC_H */