1310843Scognet/*
2310843Scognet * Copyright 2009-2016 Samy Al Bahra.
3310843Scognet * Copyright 2013-2016 Olivier Houchard.
4310843Scognet * Copyright 2016 Alexey Kopytov.
5310843Scognet * All rights reserved.
6310843Scognet *
7310843Scognet * Redistribution and use in source and binary forms, with or without
8310843Scognet * modification, are permitted provided that the following conditions
9310843Scognet * are met:
10310843Scognet * 1. Redistributions of source code must retain the above copyright
11310843Scognet *    notice, this list of conditions and the following disclaimer.
12310843Scognet * 2. Redistributions in binary form must reproduce the above copyright
13310843Scognet *    notice, this list of conditions and the following disclaimer in the
14310843Scognet *    documentation and/or other materials provided with the distribution.
15310843Scognet *
16310843Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17310843Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18310843Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19310843Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20310843Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21310843Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22310843Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23310843Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24310843Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25310843Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26310843Scognet * SUCH DAMAGE.
27310843Scognet */
28310843Scognet
29310843Scognet#ifndef CK_PR_AARCH64_LSE_H
30310843Scognet#define CK_PR_AARCH64_LSE_H
31310843Scognet
32310843Scognet#ifndef CK_PR_H
33310843Scognet#error Do not include this file directly, use ck_pr.h
34310843Scognet#endif
35310843Scognet
36310843ScognetCK_CC_INLINE static bool
37310843Scognetck_pr_cas_64_2_value(uint64_t target[2], uint64_t compare[2], uint64_t set[2], uint64_t value[2])
38310843Scognet{
39310843Scognet        uint64_t tmp1;
40310843Scognet        uint64_t tmp2;
41310843Scognet        register uint64_t x0 __asm__ ("x0") = compare[0];
42310843Scognet        register uint64_t x1 __asm__ ("x1") = compare[1];
43310843Scognet        register uint64_t x2 __asm__ ("x2") = set[0];
44310843Scognet        register uint64_t x3 __asm__ ("x3") = set[1];
45310843Scognet
46310843Scognet        __asm__ __volatile__("casp %0, %1, %4, %5, [%6];"
47310843Scognet                             "eor %2, %0, %7;"
48310843Scognet                             "eor %3, %1, %8;"
49310843Scognet                             "orr %2, %2, %3;"
50310843Scognet                             : "+&r" (x0), "+&r" (x1), "=&r" (tmp1), "=&r" (tmp2)
51310843Scognet                             : "r" (x2), "r" (x3), "r" (target), "r" (compare[0]), "r" (compare[1])
52310843Scognet                             : "memory");
53310843Scognet
54310843Scognet        value[0] = x0;
55310843Scognet        value[1] = x1;
56310843Scognet
57310843Scognet        return (!!tmp1);
58310843Scognet}
59310843Scognet
60310843ScognetCK_CC_INLINE static bool
61310843Scognetck_pr_cas_ptr_2_value(void *target, void *compare, void *set, void *value)
62310843Scognet{
63310843Scognet        return (ck_pr_cas_64_2_value(CK_CPP_CAST(uint64_t *, target),
64310843Scognet                                   CK_CPP_CAST(uint64_t *, compare),
65310843Scognet                                   CK_CPP_CAST(uint64_t *, set),
66310843Scognet                                   CK_CPP_CAST(uint64_t *, value)));
67310843Scognet}
68310843Scognet
69310843ScognetCK_CC_INLINE static bool
70310843Scognetck_pr_cas_64_2(uint64_t target[2], uint64_t compare[2], uint64_t set[2])
71310843Scognet{
72310843Scognet        register uint64_t x0 __asm__ ("x0") = compare[0];
73310843Scognet        register uint64_t x1 __asm__ ("x1") = compare[1];
74310843Scognet        register uint64_t x2 __asm__ ("x2") = set[0];
75310843Scognet        register uint64_t x3 __asm__ ("x3") = set[1];
76310843Scognet
77310843Scognet        __asm__ __volatile__("casp %0, %1, %2, %3, [%4];"
78310843Scognet                             "eor %0, %0, %5;"
79310843Scognet                             "eor %1, %1, %6;"
80310843Scognet                             "orr %0, %0, %1;"
81310843Scognet                             : "+&r" (x0), "+&r" (x1)
82310843Scognet                             : "r" (x2), "r" (x3), "r" (target), "r" (compare[0]), "r" (compare[1])
83310843Scognet                             : "memory");
84310843Scognet
85310843Scognet        return (!!x0);
86310843Scognet}
87310843ScognetCK_CC_INLINE static bool
88310843Scognetck_pr_cas_ptr_2(void *target, void *compare, void *set)
89310843Scognet{
90310843Scognet        return (ck_pr_cas_64_2(CK_CPP_CAST(uint64_t *, target),
91310843Scognet                             CK_CPP_CAST(uint64_t *, compare),
92310843Scognet                             CK_CPP_CAST(uint64_t *, set)));
93310843Scognet}
94310843Scognet
95310843Scognet
96310843Scognet#define CK_PR_CAS(N, M, T, W, R)					\
97310843Scognet        CK_CC_INLINE static bool					\
98310843Scognet        ck_pr_cas_##N##_value(M *target, T compare, T set, M *value)	\
99310843Scognet        {								\
100310843Scognet                  *(T *)value = compare;				\
101310843Scognet                __asm__ __volatile__(					\
102310843Scognet                                     "cas" W " %" R "0, %" R "2, [%1];"	\
103310843Scognet                    : "+&r" (*(T *)value)				\
104310843Scognet                    : "r"   (target),					\
105310843Scognet                    "r"   (set)						\
106310843Scognet                    : "memory");					\
107310843Scognet                return (*(T *)value == compare);                        \
108310843Scognet        }								\
109310843Scognet        CK_CC_INLINE static bool					\
110310843Scognet        ck_pr_cas_##N(M *target, T compare, T set)			\
111310843Scognet        {								\
112310843Scognet                T previous = compare;					\
113310843Scognet                __asm__ __volatile__(					\
114310843Scognet                                     "cas" W " %" R "0, %" R "2, [%1];"	\
115310843Scognet                    : "+&r" (previous)					\
116310843Scognet                    : "r"   (target),					\
117310843Scognet                    "r"   (set)						\
118310843Scognet                    : "memory");					\
119310843Scognet                return (previous == compare);   			\
120310843Scognet        }
121310843Scognet
122310843ScognetCK_PR_CAS(ptr, void, void *, "", "")
123310843Scognet
124310843Scognet#define CK_PR_CAS_S(N, M, W, R)	CK_PR_CAS(N, M, M, W, R)
125310843ScognetCK_PR_CAS_S(64, uint64_t, "", "")
126310843Scognet#ifndef CK_PR_DISABLE_DOUBLE
127310843ScognetCK_PR_CAS_S(double, double, "", "")
128310843Scognet#endif
129310843ScognetCK_PR_CAS_S(32, uint32_t, "", "w")
130310843ScognetCK_PR_CAS_S(uint, unsigned int, "", "w")
131310843ScognetCK_PR_CAS_S(int, int, "", "w")
132310843ScognetCK_PR_CAS_S(16, uint16_t, "h", "w")
133310843ScognetCK_PR_CAS_S(8, uint8_t, "b", "w")
134310843ScognetCK_PR_CAS_S(short, short, "h", "w")
135310843ScognetCK_PR_CAS_S(char, char, "b", "w")
136310843Scognet
137310843Scognet
138310843Scognet#undef CK_PR_CAS_S
139310843Scognet#undef CK_PR_CAS
140310843Scognet
141310843Scognet#define CK_PR_FAS(N, M, T, W, R)					\
142310843Scognet        CK_CC_INLINE static T						\
143310843Scognet        ck_pr_fas_##N(M *target, T v)					\
144310843Scognet        {								\
145310843Scognet                T previous;						\
146310843Scognet                __asm__ __volatile__(					\
147310843Scognet                                     "swp" W " %" R "2, %" R "0, [%1];"	\
148310843Scognet                                        : "=&r" (previous)		\
149310843Scognet                                        : "r"   (target),		\
150310843Scognet                                          "r"   (v)			\
151310843Scognet                                        : "memory");			\
152310843Scognet                return (previous);					\
153310843Scognet        }
154310843Scognet
155310843ScognetCK_PR_FAS(64, uint64_t, uint64_t, "", "")
156310843ScognetCK_PR_FAS(32, uint32_t, uint32_t, "", "w")
157310843ScognetCK_PR_FAS(ptr, void, void *, "", "")
158310843ScognetCK_PR_FAS(int, int, int, "", "w")
159310843ScognetCK_PR_FAS(uint, unsigned int, unsigned int, "", "w")
160310843ScognetCK_PR_FAS(16, uint16_t, uint16_t, "h", "w")
161310843ScognetCK_PR_FAS(8, uint8_t, uint8_t, "b", "w")
162310843ScognetCK_PR_FAS(short, short, short, "h", "w")
163310843ScognetCK_PR_FAS(char, char, char, "b", "w")
164310843Scognet
165310843Scognet
166310843Scognet#undef CK_PR_FAS
167310843Scognet
168310843Scognet#define CK_PR_UNARY(O, N, M, T, I, W, R, S)			\
169310843Scognet        CK_CC_INLINE static void				\
170310843Scognet        ck_pr_##O##_##N(M *target)				\
171310843Scognet        {							\
172310843Scognet                __asm__ __volatile__(I ";"			\
173310843Scognet                                     "st" S W " " R "0, [%0];"	\
174310843Scognet                                        :			\
175310843Scognet                                        : "r"   (target)	\
176310843Scognet                                        : "x0", "memory");	\
177310843Scognet                return;						\
178310843Scognet        }
179310843Scognet
180310843ScognetCK_PR_UNARY(inc, ptr, void, void *, "mov x0, 1", "", "x", "add")
181310843ScognetCK_PR_UNARY(dec, ptr, void, void *, "mov x0, -1", "", "x", "add")
182310843ScognetCK_PR_UNARY(not, ptr, void, void *, "mov x0, -1", "", "x", "eor")
183310843ScognetCK_PR_UNARY(inc, 64, uint64_t, uint64_t, "mov x0, 1", "", "x", "add")
184310843ScognetCK_PR_UNARY(dec, 64, uint64_t, uint64_t, "mov x0, -1", "", "x", "add")
185310843ScognetCK_PR_UNARY(not, 64, uint64_t, uint64_t, "mov x0, -1", "", "x", "eor")
186310843Scognet
187310843Scognet#define CK_PR_UNARY_S(S, T, W)					\
188310843Scognet        CK_PR_UNARY(inc, S, T, T, "mov w0, 1", W, "w", "add")	\
189310843Scognet        CK_PR_UNARY(dec, S, T, T, "mov w0, -1", W, "w", "add")	\
190310843Scognet        CK_PR_UNARY(not, S, T, T, "mov w0, -1", W, "w", "eor")	\
191310843Scognet
192310843ScognetCK_PR_UNARY_S(32, uint32_t, "")
193310843ScognetCK_PR_UNARY_S(uint, unsigned int, "")
194310843ScognetCK_PR_UNARY_S(int, int, "")
195310843ScognetCK_PR_UNARY_S(16, uint16_t, "h")
196310843ScognetCK_PR_UNARY_S(8, uint8_t, "b")
197310843ScognetCK_PR_UNARY_S(short, short, "h")
198310843ScognetCK_PR_UNARY_S(char, char, "b")
199310843Scognet
200310843Scognet#undef CK_PR_UNARY_S
201310843Scognet#undef CK_PR_UNARY
202310843Scognet
203310843Scognet#define CK_PR_BINARY(O, N, M, T, S, W, R, I)			\
204310843Scognet        CK_CC_INLINE static void				\
205310843Scognet        ck_pr_##O##_##N(M *target, T delta)			\
206310843Scognet        {							\
207310843Scognet                __asm__ __volatile__(I ";"			\
208310843Scognet                                     "st" S W " %" R "0, [%1];"	\
209310843Scognet                                        : "+&r" (delta)		\
210310843Scognet                                        : "r"   (target)	\
211310843Scognet                                        : "memory");		\
212310843Scognet                return;						\
213310843Scognet        }
214310843Scognet
215310843ScognetCK_PR_BINARY(and, ptr, void, uintptr_t, "clr", "", "", "mvn %0, %0")
216310843ScognetCK_PR_BINARY(add, ptr, void, uintptr_t, "add", "", "", "")
217310843ScognetCK_PR_BINARY(or, ptr, void, uintptr_t, "set", "", "", "")
218310843ScognetCK_PR_BINARY(sub, ptr, void, uintptr_t, "add", "", "", "neg %0, %0")
219310843ScognetCK_PR_BINARY(xor, ptr, void, uintptr_t, "eor", "", "", "")
220310843ScognetCK_PR_BINARY(and, 64, uint64_t, uint64_t, "clr", "", "", "mvn %0, %0")
221310843ScognetCK_PR_BINARY(add, 64, uint64_t, uint64_t, "add", "", "", "")
222310843ScognetCK_PR_BINARY(or, 64, uint64_t, uint64_t, "set", "", "", "")
223310843ScognetCK_PR_BINARY(sub, 64, uint64_t, uint64_t, "add", "", "", "neg %0, %0")
224310843ScognetCK_PR_BINARY(xor, 64, uint64_t, uint64_t, "eor", "", "", "")
225310843Scognet
226310843Scognet#define CK_PR_BINARY_S(S, T, W)						\
227310843Scognet        CK_PR_BINARY(and, S, T, T, "clr", W, "w", "mvn %w0, %w0")	\
228310843Scognet        CK_PR_BINARY(add, S, T, T, "add", W, "w", "")			\
229310843Scognet        CK_PR_BINARY(or, S, T, T, "set", W, "w", "")			\
230310843Scognet        CK_PR_BINARY(sub, S, T, T, "add", W, "w", "neg %w0, %w0")	\
231310843Scognet        CK_PR_BINARY(xor, S, T, T, "eor", W, "w", "")
232310843Scognet
233310843ScognetCK_PR_BINARY_S(32, uint32_t, "")
234310843ScognetCK_PR_BINARY_S(uint, unsigned int, "")
235310843ScognetCK_PR_BINARY_S(int, int, "")
236310843ScognetCK_PR_BINARY_S(16, uint16_t, "h")
237310843ScognetCK_PR_BINARY_S(8, uint8_t, "b")
238310843ScognetCK_PR_BINARY_S(short, short, "h")
239310843ScognetCK_PR_BINARY_S(char, char, "b")
240310843Scognet
241310843Scognet#undef CK_PR_BINARY_S
242310843Scognet#undef CK_PR_BINARY
243310843Scognet
244310843ScognetCK_CC_INLINE static void *
245310843Scognetck_pr_faa_ptr(void *target, uintptr_t delta)
246310843Scognet{
247310843Scognet        uintptr_t previous;
248310843Scognet
249310843Scognet        __asm__ __volatile__(
250310843Scognet                             "ldadd %2, %0, [%1];"
251310843Scognet                                : "=r" (previous)
252310843Scognet                                : "r"   (target),
253310843Scognet                                  "r"   (delta)
254310843Scognet                                : "memory");
255310843Scognet
256310843Scognet        return (void *)(previous);
257310843Scognet}
258310843Scognet
259310843ScognetCK_CC_INLINE static uint64_t
260310843Scognetck_pr_faa_64(uint64_t *target, uint64_t delta)
261310843Scognet{
262310843Scognet        uint64_t previous;
263310843Scognet
264310843Scognet        __asm__ __volatile__(
265310843Scognet                             "ldadd %2, %0, [%1];"
266310843Scognet                                : "=r" (previous)
267310843Scognet                                : "r"   (target),
268310843Scognet                                  "r"   (delta)
269310843Scognet                                : "memory");
270310843Scognet
271310843Scognet        return (previous);
272310843Scognet}
273310843Scognet
274310843Scognet#define CK_PR_FAA(S, T, W)						\
275310843Scognet        CK_CC_INLINE static T						\
276310843Scognet        ck_pr_faa_##S(T *target, T delta)				\
277310843Scognet        {								\
278310843Scognet                T previous;						\
279310843Scognet                __asm__ __volatile__(					\
280310843Scognet                                     "ldadd" W " %w2, %w0, [%1];"	\
281310843Scognet                                        : "=r" (previous)		\
282310843Scognet                                        : "r"   (target),		\
283310843Scognet                                          "r"   (delta)			\
284310843Scognet                                        : "memory");			\
285310843Scognet                return (previous);					\
286310843Scognet        }
287310843Scognet
288310843ScognetCK_PR_FAA(32, uint32_t, "")
289310843ScognetCK_PR_FAA(uint, unsigned int, "")
290310843ScognetCK_PR_FAA(int, int, "")
291310843ScognetCK_PR_FAA(16, uint16_t, "h")
292310843ScognetCK_PR_FAA(8, uint8_t, "b")
293310843ScognetCK_PR_FAA(short, short, "h")
294310843ScognetCK_PR_FAA(char, char, "b")
295310843Scognet
296310843Scognet#undef CK_PR_FAA
297310843Scognet
298310843Scognet#endif /* CK_PR_AARCH64_LSE_H */
299