source: rtems-libbsd/freebsd/sys/contrib/ck/include/gcc/ppc/ck_pr.h

6-freebsd-12
Last change on this file was 1af372a, checked in by Sebastian Huber <sebastian.huber@…>, on 09/14/18 at 12:04:09

ck: No hardware barriers in uniprocessor configs

Update #3472.

  • Property mode set to 100644
File size: 9.0 KB
Line 
1/*
2 * Copyright 2009-2015 Samy Al Bahra.
3 * Copyright 2012 João Fernandes.
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_PPC_H
29#define CK_PR_PPC_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 */
54CK_CC_INLINE static void
55ck_pr_stall(void)
56{
57
58        __asm__ __volatile__("or 1, 1, 1;"
59                             "or 2, 2, 2;" ::: "memory");
60        return;
61}
62
63#define CK_PR_FENCE(T, I)                               \
64        CK_CC_INLINE static void                        \
65        ck_pr_fence_strict_##T(void)                    \
66        {                                               \
67                __asm__ __volatile__(I ::: "memory");   \
68        }
69
70#ifdef RTEMS_SMP
71#ifdef CK_MD_PPC32_LWSYNC
72#define CK_PR_LWSYNCOP "lwsync"
73#else /* CK_MD_PPC32_LWSYNC_DISABLE */
74#define CK_PR_LWSYNCOP "sync"
75#endif
76#define CK_PR_SYNCOP "sync"
77#else /* !RTEMS_SMP */
78#define CK_PR_LWSYNCOP ""
79#define CK_PR_SYNCOP ""
80#endif /* RTEMS_SMP */
81
82CK_PR_FENCE(atomic, CK_PR_LWSYNCOP)
83CK_PR_FENCE(atomic_store, CK_PR_LWSYNCOP)
84CK_PR_FENCE(atomic_load, CK_PR_SYNCOP)
85CK_PR_FENCE(store_atomic, CK_PR_LWSYNCOP)
86CK_PR_FENCE(load_atomic, CK_PR_LWSYNCOP)
87CK_PR_FENCE(store, CK_PR_LWSYNCOP)
88CK_PR_FENCE(store_load, CK_PR_SYNCOP)
89CK_PR_FENCE(load, CK_PR_LWSYNCOP)
90CK_PR_FENCE(load_store, CK_PR_LWSYNCOP)
91CK_PR_FENCE(memory, CK_PR_SYNCOP)
92CK_PR_FENCE(acquire, CK_PR_LWSYNCOP)
93CK_PR_FENCE(release, CK_PR_LWSYNCOP)
94CK_PR_FENCE(acqrel, CK_PR_LWSYNCOP)
95CK_PR_FENCE(lock, CK_PR_LWSYNCOP)
96CK_PR_FENCE(unlock, CK_PR_LWSYNCOP)
97
98#undef CK_PR_LWSYNCOP
99#undef CK_PR_SYNCOP
100
101#undef CK_PR_FENCE
102
103#define CK_PR_LOAD(S, M, T, C, I)                                       \
104        CK_CC_INLINE static T                                           \
105        ck_pr_md_load_##S(const M *target)                              \
106        {                                                               \
107                T r;                                                    \
108                __asm__ __volatile__(I "%U1%X1 %0, %1"                  \
109                                        : "=r" (r)                      \
110                                        : "m"  (*(const C *)target)     \
111                                        : "memory");                    \
112                return (r);                                             \
113        }
114
115CK_PR_LOAD(ptr, void, void *, uint32_t, "lwz")
116
117#define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)
118
119CK_PR_LOAD_S(32, uint32_t, "lwz")
120CK_PR_LOAD_S(16, uint16_t, "lhz")
121CK_PR_LOAD_S(8, uint8_t, "lbz")
122CK_PR_LOAD_S(uint, unsigned int, "lwz")
123CK_PR_LOAD_S(int, int, "lwz")
124CK_PR_LOAD_S(short, short, "lhz")
125CK_PR_LOAD_S(char, char, "lbz")
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 "%U0%X0 %1, %0"          \
135                                        : "=m" (*(C *)target)   \
136                                        : "r" (v)               \
137                                        : "memory");            \
138                return;                                         \
139        }
140
141CK_PR_STORE(ptr, void, const void *, uint32_t, "stw")
142
143#define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)
144
145CK_PR_STORE_S(32, uint32_t, "stw")
146CK_PR_STORE_S(16, uint16_t, "sth")
147CK_PR_STORE_S(8, uint8_t, "stb")
148CK_PR_STORE_S(uint, unsigned int, "stw")
149CK_PR_STORE_S(int, int, "stw")
150CK_PR_STORE_S(short, short, "sth")
151CK_PR_STORE_S(char, char, "stb")
152
153#undef CK_PR_STORE_S
154#undef CK_PR_STORE
155
156#define CK_PR_CAS(N, T, M)                                              \
157        CK_CC_INLINE static bool                                        \
158        ck_pr_cas_##N##_value(M *target, T compare, T set, M *value)    \
159        {                                                               \
160                T previous;                                             \
161                __asm__ __volatile__("1:"                               \
162                                     "lwarx %0, 0, %1;"                 \
163                                     "cmpw  0, %0, %3;"                 \
164                                     "bne-  2f;"                        \
165                                     "stwcx. %2, 0, %1;"                \
166                                     "bne-  1b;"                        \
167                                     "2:"                               \
168                                        : "=&r" (previous)              \
169                                        : "r"   (target),               \
170                                          "r"   (set),                  \
171                                          "r"   (compare)               \
172                                        : "memory", "cc");              \
173                *(T *)value = previous;                                 \
174                return (previous == compare);                           \
175        }                                                               \
176        CK_CC_INLINE static bool                                        \
177        ck_pr_cas_##N(M *target, T compare, T set)                      \
178        {                                                               \
179                T previous;                                             \
180                __asm__ __volatile__("1:"                               \
181                                     "lwarx %0, 0, %1;"                 \
182                                     "cmpw  0, %0, %3;"                 \
183                                     "bne-  2f;"                        \
184                                     "stwcx. %2, 0, %1;"                \
185                                     "bne-  1b;"                        \
186                                     "2:"                               \
187                                        : "=&r" (previous)              \
188                                        : "r"   (target),               \
189                                          "r"   (set),                  \
190                                          "r"   (compare)               \
191                                        : "memory", "cc");              \
192                return (previous == compare);                           \
193        }
194
195CK_PR_CAS(ptr, void *, void)
196#define CK_PR_CAS_S(a, b) CK_PR_CAS(a, b, b)
197CK_PR_CAS_S(32, uint32_t)
198CK_PR_CAS_S(uint, unsigned int)
199CK_PR_CAS_S(int, int)
200
201#undef CK_PR_CAS_S
202#undef CK_PR_CAS
203
204#define CK_PR_FAS(N, M, T, W)                                   \
205        CK_CC_INLINE static T                                   \
206        ck_pr_fas_##N(M *target, T v)                           \
207        {                                                       \
208                T previous;                                     \
209                __asm__ __volatile__("1:"                       \
210                                     "l" W "arx %0, 0, %1;"     \
211                                     "st" W "cx. %2, 0, %1;"    \
212                                     "bne- 1b;"                 \
213                                        : "=&r" (previous)      \
214                                        : "r"   (target),       \
215                                          "r"   (v)             \
216                                        : "memory", "cc");      \
217                return (previous);                              \
218        }
219
220CK_PR_FAS(32, uint32_t, uint32_t, "w")
221CK_PR_FAS(ptr, void, void *, "w")
222CK_PR_FAS(int, int, int, "w")
223CK_PR_FAS(uint, unsigned int, unsigned int, "w")
224
225#undef CK_PR_FAS
226
227#define CK_PR_UNARY(O, N, M, T, I, W)                           \
228        CK_CC_INLINE static void                                \
229        ck_pr_##O##_##N(M *target)                              \
230        {                                                       \
231                T previous;                                     \
232                __asm__ __volatile__("1:"                       \
233                                     "l" W "arx %0, 0, %1;"     \
234                                      I ";"                     \
235                                     "st" W "cx. %0, 0, %1;"    \
236                                     "bne-  1b;"                \
237                                        : "=&r" (previous)      \
238                                        : "r"   (target)        \
239                                        : "memory", "cc");      \
240                return;                                         \
241        }
242
243CK_PR_UNARY(inc, ptr, void, void *, "addic %0, %0, 1", "w")
244CK_PR_UNARY(dec, ptr, void, void *, "addic %0, %0, -1", "w")
245CK_PR_UNARY(not, ptr, void, void *, "not %0, %0", "w")
246CK_PR_UNARY(neg, ptr, void, void *, "neg %0, %0", "w")
247
248#define CK_PR_UNARY_S(S, T, W)                                  \
249        CK_PR_UNARY(inc, S, T, T, "addic %0, %0, 1", W)         \
250        CK_PR_UNARY(dec, S, T, T, "addic %0, %0, -1", W)        \
251        CK_PR_UNARY(not, S, T, T, "not %0, %0", W)              \
252        CK_PR_UNARY(neg, S, T, T, "neg %0, %0", W)
253
254CK_PR_UNARY_S(32, uint32_t, "w")
255CK_PR_UNARY_S(uint, unsigned int, "w")
256CK_PR_UNARY_S(int, int, "w")
257
258#undef CK_PR_UNARY_S
259#undef CK_PR_UNARY
260
261#define CK_PR_BINARY(O, N, M, T, I, W)                          \
262        CK_CC_INLINE static void                                \
263        ck_pr_##O##_##N(M *target, T delta)                     \
264        {                                                       \
265                T previous;                                     \
266                __asm__ __volatile__("1:"                       \
267                                     "l" W "arx %0, 0, %1;"     \
268                                      I " %0, %2, %0;"          \
269                                     "st" W "cx. %0, 0, %1;"    \
270                                     "bne-  1b;"                \
271                                        : "=&r" (previous)      \
272                                        : "r"   (target),       \
273                                          "r"   (delta)         \
274                                        : "memory", "cc");      \
275                return;                                         \
276        }
277
278CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "w")
279CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "w")
280CK_PR_BINARY(or, ptr, void, uintptr_t, "or", "w")
281CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "w")
282CK_PR_BINARY(xor, ptr, void, uintptr_t, "xor", "w")
283
284#define CK_PR_BINARY_S(S, T, W)                 \
285        CK_PR_BINARY(and, S, T, T, "and", W)    \
286        CK_PR_BINARY(add, S, T, T, "add", W)    \
287        CK_PR_BINARY(or, S, T, T, "or", W)      \
288        CK_PR_BINARY(sub, S, T, T, "subf", W)   \
289        CK_PR_BINARY(xor, S, T, T, "xor", W)
290
291CK_PR_BINARY_S(32, uint32_t, "w")
292CK_PR_BINARY_S(uint, unsigned int, "w")
293CK_PR_BINARY_S(int, int, "w")
294
295#undef CK_PR_BINARY_S
296#undef CK_PR_BINARY
297
298CK_CC_INLINE static void *
299ck_pr_faa_ptr(void *target, uintptr_t delta)
300{
301        uintptr_t previous, r;
302
303        __asm__ __volatile__("1:"
304                             "lwarx %0, 0, %2;"
305                             "add %1, %3, %0;"
306                             "stwcx. %1, 0, %2;"
307                             "bne-  1b;"
308                                : "=&r" (previous),
309                                  "=&r" (r)
310                                : "r"   (target),
311                                  "r"   (delta)
312                                : "memory", "cc");
313
314        return (void *)(previous);
315}
316
317#define CK_PR_FAA(S, T, W)                                              \
318        CK_CC_INLINE static T                                           \
319        ck_pr_faa_##S(T *target, T delta)                               \
320        {                                                               \
321                T previous, r;                                          \
322                __asm__ __volatile__("1:"                               \
323                                     "l" W "arx %0, 0, %2;"             \
324                                     "add %1, %3, %0;"                  \
325                                     "st" W "cx. %1, 0, %2;"            \
326                                     "bne-  1b;"                        \
327                                        : "=&r" (previous),             \
328                                          "=&r" (r)                     \
329                                        : "r"   (target),               \
330                                          "r"   (delta)                 \
331                                        : "memory", "cc");              \
332                return (previous);                                      \
333        }
334
335CK_PR_FAA(32, uint32_t, "w")
336CK_PR_FAA(uint, unsigned int, "w")
337CK_PR_FAA(int, int, "w")
338
339#undef CK_PR_FAA
340
341#endif /* CK_PR_PPC_H */
342
Note: See TracBrowser for help on using the repository browser.