summaryrefslogtreecommitdiffstats
path: root/include/gcc/s390x/ck_pr.h
blob: 8ad22b2d2c12327ff075506bfbaff8dd2b8c6fd0 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * Copyright 2009-2015 Samy Al Bahra.
 * Copyright 2017 Neale Ferguson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef CK_PR_S390X_H
#define CK_PR_S390X_H

#ifndef CK_PR_H
#error Do not include this file directly, use ck_pr.h
#endif

#include <ck_cc.h>
#include <ck_md.h>

/*
 * The following represent supported atomic operations.
 * These operations may be emulated.
 */
#include "ck_f_pr.h"

/*
 * Minimum interface requirement met.
 */
#define CK_F_PR

/*
 * This bounces the hardware thread from low to medium
 * priority. I am unsure of the benefits of this approach
 * but it is used by the Linux kernel.
 */
CK_CC_INLINE static void
ck_pr_stall(void)
{
	__sync_synchronize();
	return;
}

#define CK_PR_FENCE(T)					\
	CK_CC_INLINE static void			\
	ck_pr_fence_strict_##T(void)			\
	{						\
		__sync_synchronize();			\
	}

/*
 * These are derived from:
 *     http://www.ibm.com/developerworks/systems/articles/powerpc.html
 */
CK_PR_FENCE(atomic)
CK_PR_FENCE(atomic_store)
CK_PR_FENCE(atomic_load)
CK_PR_FENCE(store_atomic)
CK_PR_FENCE(load_atomic)
CK_PR_FENCE(store)
CK_PR_FENCE(store_load)
CK_PR_FENCE(load)
CK_PR_FENCE(load_store)
CK_PR_FENCE(memory)
CK_PR_FENCE(acquire)
CK_PR_FENCE(release)
CK_PR_FENCE(acqrel)
CK_PR_FENCE(lock)
CK_PR_FENCE(unlock)

#undef CK_PR_FENCE

#define CK_PR_LOAD(S, M, T, C, I)					\
	CK_CC_INLINE static T						\
	ck_pr_md_load_##S(const M *target)				\
	{								\
		T r;							\
		__asm__ __volatile__(I "\t%0, %1\n"			\
					: "=r" (r)			\
					: "Q"  (*(const C *)target)	\
					: "memory");			\
		return (r);						\
	}

CK_PR_LOAD(ptr, void, void *, uint64_t, "lg")

#define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)

CK_PR_LOAD_S(64, uint64_t, "lg")
CK_PR_LOAD_S(32, uint32_t, "llgf")
CK_PR_LOAD_S(16, uint16_t, "llgh")
CK_PR_LOAD_S(8, uint8_t, "llgc")
CK_PR_LOAD_S(uint, unsigned int, "llgf")
CK_PR_LOAD_S(int, int, "llgf")
CK_PR_LOAD_S(short, short, "lgh")
CK_PR_LOAD_S(char, char, "lgb")
#ifndef CK_PR_DISABLE_DOUBLE
CK_CC_INLINE static double
ck_pr_md_load_double(const double *target)
{
	double r;
	__asm__ __volatile__("ld  %0, %1\n"
				: "=f" (r)
				: "Q"  (*(const double *)target)   
				: "memory");
	return (r);			
}
#endif

#undef CK_PR_LOAD_S
#undef CK_PR_LOAD

#define CK_PR_STORE(S, M, T, C, I)				\
	CK_CC_INLINE static void				\
	ck_pr_md_store_##S(M *target, T v)			\
	{							\
		__asm__ __volatile__(I "\t%1, %0\n"		\
					: "=Q" (*(C *)target)	\
					: "r" (v)		\
					: "memory");		\
		return;						\
	}

CK_PR_STORE(ptr, void, const void *, uint64_t, "stg")

#define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)

CK_PR_STORE_S(64, uint64_t, "stg")
CK_PR_STORE_S(32, uint32_t, "st")
CK_PR_STORE_S(16, uint16_t, "sth")
CK_PR_STORE_S(8, uint8_t, "stc")
CK_PR_STORE_S(uint, unsigned int, "st")
CK_PR_STORE_S(int, int, "st")
CK_PR_STORE_S(short, short, "sth")
CK_PR_STORE_S(char, char, "stc")
#ifndef CK_PR_DISABLE_DOUBLE
CK_CC_INLINE static void
ck_pr_md_store_double(double *target, double v)
{
	__asm__ __volatile__(" std  %1, %0\n"
				: "=Q" (*(double *)target)   
				: "f"  (v)
				: "0", "memory");
}
#endif

#undef CK_PR_STORE_S
#undef CK_PR_STORE

CK_CC_INLINE static bool
ck_pr_cas_64_value(uint64_t *target, uint64_t compare, uint64_t set, uint64_t *value)
{
	*value = __sync_val_compare_and_swap(target,compare,set);
        return (*value == compare);
}

CK_CC_INLINE static bool
ck_pr_cas_ptr_value(void *target, void *compare, void *set, void *value)
{
	uintptr_t previous;

	previous = __sync_val_compare_and_swap((uintptr_t *) target,
					       (uintptr_t) compare,
					       (uintptr_t) set);
	*((uintptr_t *) value) = previous;
        return (previous == (uintptr_t) compare);
}

CK_CC_INLINE static bool
ck_pr_cas_64(uint64_t *target, uint64_t compare, uint64_t set)
{
	return(__sync_bool_compare_and_swap(target,compare,set));
}

CK_CC_INLINE static bool
ck_pr_cas_ptr(void *target, void *compare, void *set)
{
	return(__sync_bool_compare_and_swap((uintptr_t *) target,
					    (uintptr_t) compare,
					    (uintptr_t) set));
}

#define CK_PR_CAS(N, T)							\
	CK_CC_INLINE static bool					\
	ck_pr_cas_##N##_value(T *target, T compare, T set, T *value)	\
	{								\
		*value = __sync_val_compare_and_swap(target,		\
						     compare,		\
						     set);		\
		return(*value == compare);				\
	}								\
	CK_CC_INLINE static bool					\
	ck_pr_cas_##N(T *target, T compare, T set)			\
	{								\
		return(__sync_bool_compare_and_swap(target,		\
						   compare,		\
						   set));		\
	}

CK_PR_CAS(32, uint32_t)
CK_PR_CAS(uint, unsigned int)
CK_PR_CAS(int, int)

#undef CK_PR_CAS

CK_CC_INLINE static void *
ck_pr_fas_ptr(void *target, void *v)
{
	return((void *)__atomic_exchange_n((uintptr_t *) target, (uintptr_t) v, __ATOMIC_ACQUIRE));
}

#define CK_PR_FAS(N, M, T)							\
	CK_CC_INLINE static T							\
	ck_pr_fas_##N(M *target, T v)						\
	{									\
		return(__atomic_exchange_n(target, v, __ATOMIC_ACQUIRE));	\
	}

CK_PR_FAS(64, uint64_t, uint64_t)
CK_PR_FAS(32, uint32_t, uint32_t)
CK_PR_FAS(int, int, int)
CK_PR_FAS(uint, unsigned int, unsigned int)

#ifndef CK_PR_DISABLE_DOUBLE
CK_CC_INLINE static double
ck_pr_fas_double(double *target, double *v)
{
	double previous;

	__asm__ __volatile__ ("	   lg   1,%2\n"
			      "0:  lg	0,%1\n"
			      "    csg	0,1,%1\n"
			      "    jnz	0b\n"
			      "    ldgr %0,0\n"
			      : "=f" (previous) 
			      : "Q" (target), "Q" (v)
			      : "0", "1", "cc", "memory");
	return (previous);
}
#endif

#undef CK_PR_FAS

/*
 * Atomic store-only binary operations.
 */
#define CK_PR_BINARY(K, S, M, T)				\
	CK_CC_INLINE static void				\
	ck_pr_##K##_##S(M *target, T d)				\
	{							\
		d = __sync_fetch_and_##K((T *)target, d);	\
		return;						\
	}

#define CK_PR_BINARY_S(K, S, T) CK_PR_BINARY(K, S, T, T)

#define CK_PR_GENERATE(K)			\
	CK_PR_BINARY(K, ptr, void, void *)	\
	CK_PR_BINARY_S(K, char, char)		\
	CK_PR_BINARY_S(K, int, int)		\
	CK_PR_BINARY_S(K, uint, unsigned int)	\
	CK_PR_BINARY_S(K, 64, uint64_t)		\
	CK_PR_BINARY_S(K, 32, uint32_t)		\
	CK_PR_BINARY_S(K, 16, uint16_t)		\
	CK_PR_BINARY_S(K, 8, uint8_t)

CK_PR_GENERATE(add)
CK_PR_GENERATE(sub)
CK_PR_GENERATE(and)
CK_PR_GENERATE(or)
CK_PR_GENERATE(xor)

#undef CK_PR_GENERATE
#undef CK_PR_BINARY_S
#undef CK_PR_BINARY

#define CK_PR_UNARY(S, M, T)			\
	CK_CC_INLINE static void		\
	ck_pr_inc_##S(M *target)		\
	{					\
		ck_pr_add_##S(target, (T)1);	\
		return;				\
	}					\
	CK_CC_INLINE static void		\
	ck_pr_dec_##S(M *target)		\
	{					\
		ck_pr_sub_##S(target, (T)1);	\
		return;				\
	}

#define CK_PR_UNARY_X(S, M) 	  					\
	CK_CC_INLINE static void					\
	ck_pr_not_##S(M *target)					\
	{								\
		M newval;						\
		do {							\
			newval = ~(*target);				\
		} while (!__sync_bool_compare_and_swap(target, 		\
						      *target,		\
						      newval));		\
	}								\
	CK_CC_INLINE static void					\
	ck_pr_neg_##S(M *target)					\
	{								\
		M newval;						\
		do {							\
			newval = -(*target);				\
		} while (!__sync_bool_compare_and_swap(target, 		\
						      *target,		\
						      newval));		\
	}								

#define CK_PR_UNARY_S(S, M) CK_PR_UNARY(S, M, M) \
			    CK_PR_UNARY_X(S, M)

CK_PR_UNARY(ptr, void, void *)
CK_PR_UNARY_S(char, char)
CK_PR_UNARY_S(int, int)
CK_PR_UNARY_S(uint, unsigned int)
CK_PR_UNARY_S(64, uint64_t)
CK_PR_UNARY_S(32, uint32_t)
CK_PR_UNARY_S(16, uint16_t)
CK_PR_UNARY_S(8, uint8_t)

#undef CK_PR_UNARY_S
#undef CK_PR_UNARY

CK_CC_INLINE static void *
ck_pr_faa_ptr(void *target, uintptr_t delta)
{
	uintptr_t previous;

	previous = __sync_fetch_and_add((uintptr_t *) target, delta);

	return (void *)(previous);
}

#define CK_PR_FAA(S, T)							\
	CK_CC_INLINE static T						\
	ck_pr_faa_##S(T *target, T delta)				\
	{								\
		T previous;						\
									\
		previous = __sync_fetch_and_add(target, delta);		\
									\
		return (previous);					\
	}

CK_PR_FAA(64, uint64_t)
CK_PR_FAA(32, uint32_t)
CK_PR_FAA(uint, unsigned int)
CK_PR_FAA(int, int)

#undef CK_PR_FAA

#endif /* CK_PR_S390X_H */