2ac772467e5ae742ea0d4c1288be302bcc2485e6
[musl] / arch / sh / atomic_arch.h
1 #define LLSC_CLOBBERS "r0", "t", "memory"
2 #define LLSC_START(mem) "synco\n"  \
3         "0:     movli.l @" mem ", r0\n"
4 #define LLSC_END(mem)              \
5         "1:     movco.l r0, @" mem "\n"    \
6         "       bf 0b\n"                   \
7         "       synco\n"
8
9 static inline int __sh_cas_llsc(volatile int *p, int t, int s)
10 {
11         int old;
12         __asm__ __volatile__(
13                 LLSC_START("%1")
14                 "       mov r0, %0\n"
15                 "       cmp/eq %0, %2\n"
16                 "       bf 1f\n"
17                 "       mov %3, r0\n"
18                 LLSC_END("%1")
19                 : "=&r"(old) : "r"(p), "r"(t), "r"(s) : LLSC_CLOBBERS);
20         return old;
21 }
22
23 static inline int __sh_swap_llsc(volatile int *x, int v)
24 {
25         int old;
26         __asm__ __volatile__(
27                 LLSC_START("%1")
28                 "       mov r0, %0\n"
29                 "       mov %2, r0\n"
30                 LLSC_END("%1")
31                 : "=&r"(old) : "r"(x), "r"(v) : LLSC_CLOBBERS);
32         return old;
33 }
34
35 static inline int __sh_fetch_add_llsc(volatile int *x, int v)
36 {
37         int old;
38         __asm__ __volatile__(
39                 LLSC_START("%1")
40                 "       mov r0, %0\n"
41                 "       add %2, r0\n"
42                 LLSC_END("%1")
43                 : "=&r"(old) : "r"(x), "r"(v) : LLSC_CLOBBERS);
44         return old;
45 }
46
47 static inline void __sh_store_llsc(volatile int *p, int x)
48 {
49         __asm__ __volatile__(
50                 "       synco\n"
51                 "       mov.l %1, @%0\n"
52                 "       synco\n"
53                 : : "r"(p), "r"(x) : "memory");
54 }
55
56 static inline void __sh_and_llsc(volatile int *x, int v)
57 {
58         __asm__ __volatile__(
59                 LLSC_START("%0")
60                 "       and %1, r0\n"
61                 LLSC_END("%0")
62                 : : "r"(x), "r"(v) : LLSC_CLOBBERS);
63 }
64
65 static inline void __sh_or_llsc(volatile int *x, int v)
66 {
67         __asm__ __volatile__(
68                 LLSC_START("%0")
69                 "       or %1, r0\n"
70                 LLSC_END("%0")
71                 : : "r"(x), "r"(v) : LLSC_CLOBBERS);
72 }
73
74 #ifdef __SH4A__
75 #define a_cas(p,t,s)     __sh_cas_llsc(p,t,s)
76 #define a_swap(x,v)      __sh_swap_llsc(x,v)
77 #define a_fetch_add(x,v) __sh_fetch_add_llsc(x, v)
78 #define a_store(x,v)     __sh_store_llsc(x, v)
79 #define a_and(x,v)       __sh_and_llsc(x, v)
80 #define a_or(x,v)        __sh_or_llsc(x, v)
81 #else
82
83 int  __sh_cas(volatile int *, int, int);
84 int  __sh_swap(volatile int *, int);
85 int  __sh_fetch_add(volatile int *, int);
86 void __sh_store(volatile int *, int);
87 void __sh_and(volatile int *, int);
88 void __sh_or(volatile int *, int);
89
90 #define a_cas(p,t,s)     __sh_cas(p,t,s)
91 #define a_swap(x,v)      __sh_swap(x,v)
92 #define a_fetch_add(x,v) __sh_fetch_add(x, v)
93 #define a_store(x,v)     __sh_store(x, v)
94 #define a_and(x,v)       __sh_and(x, v)
95 #define a_or(x,v)        __sh_or(x, v)
96 #endif