xref: /openbmc/linux/tools/arch/x86/include/asm/atomic.h (revision c10d12e3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _TOOLS_LINUX_ASM_X86_ATOMIC_H
3 #define _TOOLS_LINUX_ASM_X86_ATOMIC_H
4 
5 #include <linux/compiler.h>
6 #include <linux/types.h>
7 #include "rmwcc.h"
8 
9 #define LOCK_PREFIX "\n\tlock; "
10 
11 #include <asm/cmpxchg.h>
12 
13 /*
14  * Atomic operations that C can't guarantee us.  Useful for
15  * resource counting etc..
16  */
17 
18 #define ATOMIC_INIT(i)	{ (i) }
19 
20 /**
21  * atomic_read - read atomic variable
22  * @v: pointer of type atomic_t
23  *
24  * Atomically reads the value of @v.
25  */
26 static inline int atomic_read(const atomic_t *v)
27 {
28 	return READ_ONCE((v)->counter);
29 }
30 
31 /**
32  * atomic_set - set atomic variable
33  * @v: pointer of type atomic_t
34  * @i: required value
35  *
36  * Atomically sets the value of @v to @i.
37  */
38 static inline void atomic_set(atomic_t *v, int i)
39 {
40 	v->counter = i;
41 }
42 
43 /**
44  * atomic_inc - increment atomic variable
45  * @v: pointer of type atomic_t
46  *
47  * Atomically increments @v by 1.
48  */
49 static inline void atomic_inc(atomic_t *v)
50 {
51 	asm volatile(LOCK_PREFIX "incl %0"
52 		     : "+m" (v->counter));
53 }
54 
55 /**
56  * atomic_dec_and_test - decrement and test
57  * @v: pointer of type atomic_t
58  *
59  * Atomically decrements @v by 1 and
60  * returns true if the result is 0, or false for all other
61  * cases.
62  */
63 static inline int atomic_dec_and_test(atomic_t *v)
64 {
65 	GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e");
66 }
67 
68 static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
69 {
70 	return cmpxchg(&v->counter, old, new);
71 }
72 
73 #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */
74