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 | */ |
---|
54 | CK_CC_INLINE static void |
---|
55 | ck_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 | |
---|
82 | CK_PR_FENCE(atomic, CK_PR_LWSYNCOP) |
---|
83 | CK_PR_FENCE(atomic_store, CK_PR_LWSYNCOP) |
---|
84 | CK_PR_FENCE(atomic_load, CK_PR_SYNCOP) |
---|
85 | CK_PR_FENCE(store_atomic, CK_PR_LWSYNCOP) |
---|
86 | CK_PR_FENCE(load_atomic, CK_PR_LWSYNCOP) |
---|
87 | CK_PR_FENCE(store, CK_PR_LWSYNCOP) |
---|
88 | CK_PR_FENCE(store_load, CK_PR_SYNCOP) |
---|
89 | CK_PR_FENCE(load, CK_PR_LWSYNCOP) |
---|
90 | CK_PR_FENCE(load_store, CK_PR_LWSYNCOP) |
---|
91 | CK_PR_FENCE(memory, CK_PR_SYNCOP) |
---|
92 | CK_PR_FENCE(acquire, CK_PR_LWSYNCOP) |
---|
93 | CK_PR_FENCE(release, CK_PR_LWSYNCOP) |
---|
94 | CK_PR_FENCE(acqrel, CK_PR_LWSYNCOP) |
---|
95 | CK_PR_FENCE(lock, CK_PR_LWSYNCOP) |
---|
96 | CK_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 | |
---|
115 | CK_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 | |
---|
119 | CK_PR_LOAD_S(32, uint32_t, "lwz") |
---|
120 | CK_PR_LOAD_S(16, uint16_t, "lhz") |
---|
121 | CK_PR_LOAD_S(8, uint8_t, "lbz") |
---|
122 | CK_PR_LOAD_S(uint, unsigned int, "lwz") |
---|
123 | CK_PR_LOAD_S(int, int, "lwz") |
---|
124 | CK_PR_LOAD_S(short, short, "lhz") |
---|
125 | CK_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 | |
---|
141 | CK_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 | |
---|
145 | CK_PR_STORE_S(32, uint32_t, "stw") |
---|
146 | CK_PR_STORE_S(16, uint16_t, "sth") |
---|
147 | CK_PR_STORE_S(8, uint8_t, "stb") |
---|
148 | CK_PR_STORE_S(uint, unsigned int, "stw") |
---|
149 | CK_PR_STORE_S(int, int, "stw") |
---|
150 | CK_PR_STORE_S(short, short, "sth") |
---|
151 | CK_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 | |
---|
195 | CK_PR_CAS(ptr, void *, void) |
---|
196 | #define CK_PR_CAS_S(a, b) CK_PR_CAS(a, b, b) |
---|
197 | CK_PR_CAS_S(32, uint32_t) |
---|
198 | CK_PR_CAS_S(uint, unsigned int) |
---|
199 | CK_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 | |
---|
220 | CK_PR_FAS(32, uint32_t, uint32_t, "w") |
---|
221 | CK_PR_FAS(ptr, void, void *, "w") |
---|
222 | CK_PR_FAS(int, int, int, "w") |
---|
223 | CK_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 | |
---|
243 | CK_PR_UNARY(inc, ptr, void, void *, "addic %0, %0, 1", "w") |
---|
244 | CK_PR_UNARY(dec, ptr, void, void *, "addic %0, %0, -1", "w") |
---|
245 | CK_PR_UNARY(not, ptr, void, void *, "not %0, %0", "w") |
---|
246 | CK_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 | |
---|
254 | CK_PR_UNARY_S(32, uint32_t, "w") |
---|
255 | CK_PR_UNARY_S(uint, unsigned int, "w") |
---|
256 | CK_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 | |
---|
278 | CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "w") |
---|
279 | CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "w") |
---|
280 | CK_PR_BINARY(or, ptr, void, uintptr_t, "or", "w") |
---|
281 | CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "w") |
---|
282 | CK_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 | |
---|
291 | CK_PR_BINARY_S(32, uint32_t, "w") |
---|
292 | CK_PR_BINARY_S(uint, unsigned int, "w") |
---|
293 | CK_PR_BINARY_S(int, int, "w") |
---|
294 | |
---|
295 | #undef CK_PR_BINARY_S |
---|
296 | #undef CK_PR_BINARY |
---|
297 | |
---|
298 | CK_CC_INLINE static void * |
---|
299 | ck_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 | |
---|
335 | CK_PR_FAA(32, uint32_t, "w") |
---|
336 | CK_PR_FAA(uint, unsigned int, "w") |
---|
337 | CK_PR_FAA(int, int, "w") |
---|
338 | |
---|
339 | #undef CK_PR_FAA |
---|
340 | |
---|
341 | #endif /* CK_PR_PPC_H */ |
---|
342 | |
---|