Debian Codegraph @

include/gcc/s390x/ck_pr.h

raw
1 /*
2 * Copyright 2009-2015 Samy Al Bahra.
3 * Copyright 2017 Neale Ferguson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #ifndef CK_PR_S390X_H
29 #define CK_PR_S390X_H
30
31 #ifndef CK_PR_H
32 #error Do not include this file directly, use ck_pr.h
33 #endif
34
35 #include <ck_cc.h>
36 #include <ck_md.h>
37
38 /*
39 * The following represent supported atomic operations.
40 * These operations may be emulated.
41 */
42 #include "ck_f_pr.h"
43
44 /*
45 * Minimum interface requirement met.
46 */
47 #define CK_F_PR
48
49 /*
50 * This bounces the hardware thread from low to medium
51 * priority. I am unsure of the benefits of this approach
52 * but it is used by the Linux kernel.
53 */
54 CK_CC_INLINE static void
55 ck_pr_stall(void)
56 {
57 __sync_synchronize();
58 return;
59 }
60
61 #define CK_PR_FENCE(T) \
62 CK_CC_INLINE static void \
63 ck_pr_fence_strict_##T(void) \
64 { \
65 __sync_synchronize(); \
66 }
67
68 /*
69 * These are derived from:
70 * http://www.ibm.com/developerworks/systems/articles/powerpc.html
71 */
72 CK_PR_FENCE(atomic)
73 CK_PR_FENCE(atomic_store)
74 CK_PR_FENCE(atomic_load)
75 CK_PR_FENCE(store_atomic)
76 CK_PR_FENCE(load_atomic)
77 CK_PR_FENCE(store)
78 CK_PR_FENCE(store_load)
79 CK_PR_FENCE(load)
80 CK_PR_FENCE(load_store)
81 CK_PR_FENCE(memory)
82 CK_PR_FENCE(acquire)
83 CK_PR_FENCE(release)
84 CK_PR_FENCE(acqrel)
85 CK_PR_FENCE(lock)
86 CK_PR_FENCE(unlock)
87
88 #undef CK_PR_FENCE
89
90 #define CK_PR_LOAD(S, M, T, C, I) \
91 CK_CC_INLINE static T \
92 ck_pr_md_load_##S(const M *target) \
93 { \
94 T r; \
95 __asm__ __volatile__(I "\t%0, %1\n" \
96 : "=r" (r) \
97 : "Q" (*(const C *)target) \
98 : "memory"); \
99 return (r); \
100 }
101
102 CK_PR_LOAD(ptr, void, void *, uint64_t, "lg")
103
104 #define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)
105
106 CK_PR_LOAD_S(64, uint64_t, "lg")
107 CK_PR_LOAD_S(32, uint32_t, "llgf")
108 CK_PR_LOAD_S(16, uint16_t, "llgh")
109 CK_PR_LOAD_S(8, uint8_t, "llgc")
110 CK_PR_LOAD_S(uint, unsigned int, "llgf")
111 CK_PR_LOAD_S(int, int, "llgf")
112 CK_PR_LOAD_S(short, short, "lgh")
113 CK_PR_LOAD_S(char, char, "lgb")
114 #ifndef CK_PR_DISABLE_DOUBLE
115 CK_CC_INLINE static double
116 ck_pr_md_load_double(const double *target)
117 {
118 double r;
119 __asm__ __volatile__("ld %0, %1\n"
120 : "=f" (r)
121 : "Q" (*(const double *)target)
122 : "memory");
123 return (r);
124 }
125 #endif
126
127 #undef CK_PR_LOAD_S
128 #undef CK_PR_LOAD
129
130 #define CK_PR_STORE(S, M, T, C, I) \
131 CK_CC_INLINE static void \
132 ck_pr_md_store_##S(M *target, T v) \
133 { \
134 __asm__ __volatile__(I "\t%1, %0\n" \
135 : "=Q" (*(C *)target) \
136 : "r" (v) \
137 : "memory"); \
138 return; \
139 }
140
141 CK_PR_STORE(ptr, void, const void *, uint64_t, "stg")
142
143 #define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)
144
145 CK_PR_STORE_S(64, uint64_t, "stg")
146 CK_PR_STORE_S(32, uint32_t, "st")
147 CK_PR_STORE_S(16, uint16_t, "sth")
148 CK_PR_STORE_S(8, uint8_t, "stc")
149 CK_PR_STORE_S(uint, unsigned int, "st")
150 CK_PR_STORE_S(int, int, "st")
151 CK_PR_STORE_S(short, short, "sth")
152 CK_PR_STORE_S(char, char, "stc")
153 #ifndef CK_PR_DISABLE_DOUBLE
154 CK_CC_INLINE static void
155 ck_pr_md_store_double(double *target, double v)
156 {
157 __asm__ __volatile__(" std %1, %0\n"
158 : "=Q" (*(double *)target)
159 : "f" (v)
160 : "0", "memory");
161 }
162 #endif
163
164 #undef CK_PR_STORE_S
165 #undef CK_PR_STORE
166
167 CK_CC_INLINE static bool
168 ck_pr_cas_64_value(uint64_t *target, uint64_t compare, uint64_t set, uint64_t *value)
169 {
170 *value = __sync_val_compare_and_swap(target,compare,set);
171 return (*value == compare);
172 }
173
174 CK_CC_INLINE static bool
175 ck_pr_cas_ptr_value(void *target, void *compare, void *set, void *value)
176 {
177 uintptr_t previous;
178
179 previous = __sync_val_compare_and_swap((uintptr_t *) target,
180 (uintptr_t) compare,
181 (uintptr_t) set);
182 *((uintptr_t *) value) = previous;
183 return (previous == (uintptr_t) compare);
184 }
185
186 CK_CC_INLINE static bool
187 ck_pr_cas_64(uint64_t *target, uint64_t compare, uint64_t set)
188 {
189 return(__sync_bool_compare_and_swap(target,compare,set));
190 }
191
192 CK_CC_INLINE static bool
193 ck_pr_cas_ptr(void *target, void *compare, void *set)
194 {
195 return(__sync_bool_compare_and_swap((uintptr_t *) target,
196 (uintptr_t) compare,
197 (uintptr_t) set));
198 }
199
200 #define CK_PR_CAS(N, T) \
201 CK_CC_INLINE static bool \
202 ck_pr_cas_##N##_value(T *target, T compare, T set, T *value) \
203 { \
204 *value = __sync_val_compare_and_swap(target, \
205 compare, \
206 set); \
207 return(*value == compare); \
208 } \
209 CK_CC_INLINE static bool \
210 ck_pr_cas_##N(T *target, T compare, T set) \
211 { \
212 return(__sync_bool_compare_and_swap(target, \
213 compare, \
214 set)); \
215 }
216
217 CK_PR_CAS(32, uint32_t)
218 CK_PR_CAS(uint, unsigned int)
219 CK_PR_CAS(int, int)
220
221 #undef CK_PR_CAS
222
223 CK_CC_INLINE static void *
224 ck_pr_fas_ptr(void *target, void *v)
225 {
226 return((void *)__atomic_exchange_n((uintptr_t *) target, (uintptr_t) v, __ATOMIC_ACQUIRE));
227 }
228
229 #define CK_PR_FAS(N, M, T) \
230 CK_CC_INLINE static T \
231 ck_pr_fas_##N(M *target, T v) \
232 { \
233 return(__atomic_exchange_n(target, v, __ATOMIC_ACQUIRE)); \
234 }
235
236 CK_PR_FAS(64, uint64_t, uint64_t)
237 CK_PR_FAS(32, uint32_t, uint32_t)
238 CK_PR_FAS(int, int, int)
239 CK_PR_FAS(uint, unsigned int, unsigned int)
240
241 #ifndef CK_PR_DISABLE_DOUBLE
242 CK_CC_INLINE static double
243 ck_pr_fas_double(double *target, double *v)
244 {
245 double previous;
246
247 __asm__ __volatile__ (" lg 1,%2\n"
248 "0: lg 0,%1\n"
249 " csg 0,1,%1\n"
250 " jnz 0b\n"
251 " ldgr %0,0\n"
252 : "=f" (previous)
253 : "Q" (target), "Q" (v)
254 : "0", "1", "cc", "memory");
255 return (previous);
256 }
257 #endif
258
259 #undef CK_PR_FAS
260
261 /*
262 * Atomic store-only binary operations.
263 */
264 #define CK_PR_BINARY(K, S, M, T) \
265 CK_CC_INLINE static void \
266 ck_pr_##K##_##S(M *target, T d) \
267 { \
268 d = __sync_fetch_and_##K((T *)target, d); \
269 return; \
270 }
271
272 #define CK_PR_BINARY_S(K, S, T) CK_PR_BINARY(K, S, T, T)
273
274 #define CK_PR_GENERATE(K) \
275 CK_PR_BINARY(K, ptr, void, void *) \
276 CK_PR_BINARY_S(K, char, char) \
277 CK_PR_BINARY_S(K, int, int) \
278 CK_PR_BINARY_S(K, uint, unsigned int) \
279 CK_PR_BINARY_S(K, 64, uint64_t) \
280 CK_PR_BINARY_S(K, 32, uint32_t) \
281 CK_PR_BINARY_S(K, 16, uint16_t) \
282 CK_PR_BINARY_S(K, 8, uint8_t)
283
284 CK_PR_GENERATE(add)
285 CK_PR_GENERATE(sub)
286 CK_PR_GENERATE(and)
287 CK_PR_GENERATE(or)
288 CK_PR_GENERATE(xor)
289
290 #undef CK_PR_GENERATE
291 #undef CK_PR_BINARY_S
292 #undef CK_PR_BINARY
293
294 #define CK_PR_UNARY(S, M, T) \
295 CK_CC_INLINE static void \
296 ck_pr_inc_##S(M *target) \
297 { \
298 ck_pr_add_##S(target, (T)1); \
299 return; \
300 } \
301 CK_CC_INLINE static void \
302 ck_pr_dec_##S(M *target) \
303 { \
304 ck_pr_sub_##S(target, (T)1); \
305 return; \
306 }
307
308 #define CK_PR_UNARY_X(S, M) \
309 CK_CC_INLINE static void \
310 ck_pr_not_##S(M *target) \
311 { \
312 M newval; \
313 do { \
314 newval = ~(*target); \
315 } while (!__sync_bool_compare_and_swap(target, \
316 *target, \
317 newval)); \
318 } \
319 CK_CC_INLINE static void \
320 ck_pr_neg_##S(M *target) \
321 { \
322 M newval; \
323 do { \
324 newval = -(*target); \
325 } while (!__sync_bool_compare_and_swap(target, \
326 *target, \
327 newval)); \
328 }
329
330 #define CK_PR_UNARY_S(S, M) CK_PR_UNARY(S, M, M) \
331 CK_PR_UNARY_X(S, M)
332
333 CK_PR_UNARY(ptr, void, void *)
334 CK_PR_UNARY_S(char, char)
335 CK_PR_UNARY_S(int, int)
336 CK_PR_UNARY_S(uint, unsigned int)
337 CK_PR_UNARY_S(64, uint64_t)
338 CK_PR_UNARY_S(32, uint32_t)
339 CK_PR_UNARY_S(16, uint16_t)
340 CK_PR_UNARY_S(8, uint8_t)
341
342 #undef CK_PR_UNARY_S
343 #undef CK_PR_UNARY
344
345 CK_CC_INLINE static void *
346 ck_pr_faa_ptr(void *target, uintptr_t delta)
347 {
348 uintptr_t previous;
349
350 previous = __sync_fetch_and_add((uintptr_t *) target, delta);
351
352 return (void *)(previous);
353 }
354
355 #define CK_PR_FAA(S, T) \
356 CK_CC_INLINE static T \
357 ck_pr_faa_##S(T *target, T delta) \
358 { \
359 T previous; \
360 \
361 previous = __sync_fetch_and_add(target, delta); \
362 \
363 return (previous); \
364 }
365
366 CK_PR_FAA(64, uint64_t)
367 CK_PR_FAA(32, uint32_t)
368 CK_PR_FAA(uint, unsigned int)
369 CK_PR_FAA(int, int)
370
371 #undef CK_PR_FAA
372
373 #endif /* CK_PR_S390X_H */