xref: /freebsd/sys/contrib/ck/include/gcc/ppc/ck_pr.h (revision 14b841d4a8e6c25d964f5e3bbda379a1541a27ec)
11fb62fb0SOlivier Houchard /*
21fb62fb0SOlivier Houchard  * Copyright 2009-2015 Samy Al Bahra.
31fb62fb0SOlivier Houchard  * Copyright 2012 João Fernandes.
41fb62fb0SOlivier Houchard  * All rights reserved.
51fb62fb0SOlivier Houchard  *
61fb62fb0SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
71fb62fb0SOlivier Houchard  * modification, are permitted provided that the following conditions
81fb62fb0SOlivier Houchard  * are met:
91fb62fb0SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
101fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
111fb62fb0SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
121fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
131fb62fb0SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
141fb62fb0SOlivier Houchard  *
151fb62fb0SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161fb62fb0SOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171fb62fb0SOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181fb62fb0SOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191fb62fb0SOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201fb62fb0SOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211fb62fb0SOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221fb62fb0SOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231fb62fb0SOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241fb62fb0SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251fb62fb0SOlivier Houchard  * SUCH DAMAGE.
261fb62fb0SOlivier Houchard  */
271fb62fb0SOlivier Houchard 
281fb62fb0SOlivier Houchard #ifndef CK_PR_PPC_H
291fb62fb0SOlivier Houchard #define CK_PR_PPC_H
301fb62fb0SOlivier Houchard 
311fb62fb0SOlivier Houchard #ifndef CK_PR_H
321fb62fb0SOlivier Houchard #error Do not include this file directly, use ck_pr.h
331fb62fb0SOlivier Houchard #endif
341fb62fb0SOlivier Houchard 
351fb62fb0SOlivier Houchard #include <ck_cc.h>
361fb62fb0SOlivier Houchard #include <ck_md.h>
371fb62fb0SOlivier Houchard 
381fb62fb0SOlivier Houchard /*
391fb62fb0SOlivier Houchard  * The following represent supported atomic operations.
401fb62fb0SOlivier Houchard  * These operations may be emulated.
411fb62fb0SOlivier Houchard  */
421fb62fb0SOlivier Houchard #include "ck_f_pr.h"
431fb62fb0SOlivier Houchard 
441fb62fb0SOlivier Houchard /*
451fb62fb0SOlivier Houchard  * Minimum interface requirement met.
461fb62fb0SOlivier Houchard  */
471fb62fb0SOlivier Houchard #define CK_F_PR
481fb62fb0SOlivier Houchard 
491fb62fb0SOlivier Houchard /*
501fb62fb0SOlivier Houchard  * This bounces the hardware thread from low to medium
511fb62fb0SOlivier Houchard  * priority. I am unsure of the benefits of this approach
521fb62fb0SOlivier Houchard  * but it is used by the Linux kernel.
531fb62fb0SOlivier Houchard  */
541fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_pr_stall(void)551fb62fb0SOlivier Houchard ck_pr_stall(void)
561fb62fb0SOlivier Houchard {
571fb62fb0SOlivier Houchard 
581fb62fb0SOlivier Houchard 	__asm__ __volatile__("or 1, 1, 1;"
591fb62fb0SOlivier Houchard 			     "or 2, 2, 2;" ::: "memory");
601fb62fb0SOlivier Houchard 	return;
611fb62fb0SOlivier Houchard }
621fb62fb0SOlivier Houchard 
631fb62fb0SOlivier Houchard #define CK_PR_FENCE(T, I)				\
641fb62fb0SOlivier Houchard 	CK_CC_INLINE static void			\
651fb62fb0SOlivier Houchard 	ck_pr_fence_strict_##T(void)			\
661fb62fb0SOlivier Houchard 	{						\
671fb62fb0SOlivier Houchard 		__asm__ __volatile__(I ::: "memory");   \
681fb62fb0SOlivier Houchard 	}
691fb62fb0SOlivier Houchard 
70*d8f1ed8dSOlivier Houchard #ifdef CK_MD_PPC32_LWSYNC
71*d8f1ed8dSOlivier Houchard #define CK_PR_LWSYNCOP "lwsync"
72*d8f1ed8dSOlivier Houchard #else /* CK_MD_PPC32_LWSYNC_DISABLE */
73*d8f1ed8dSOlivier Houchard #define CK_PR_LWSYNCOP "sync"
74*d8f1ed8dSOlivier Houchard #endif
75*d8f1ed8dSOlivier Houchard 
CK_PR_FENCE(atomic,CK_PR_LWSYNCOP)76*d8f1ed8dSOlivier Houchard CK_PR_FENCE(atomic, CK_PR_LWSYNCOP)
77*d8f1ed8dSOlivier Houchard CK_PR_FENCE(atomic_store, CK_PR_LWSYNCOP)
781fb62fb0SOlivier Houchard CK_PR_FENCE(atomic_load, "sync")
79*d8f1ed8dSOlivier Houchard CK_PR_FENCE(store_atomic, CK_PR_LWSYNCOP)
80*d8f1ed8dSOlivier Houchard CK_PR_FENCE(load_atomic, CK_PR_LWSYNCOP)
81*d8f1ed8dSOlivier Houchard CK_PR_FENCE(store, CK_PR_LWSYNCOP)
821fb62fb0SOlivier Houchard CK_PR_FENCE(store_load, "sync")
83*d8f1ed8dSOlivier Houchard CK_PR_FENCE(load, CK_PR_LWSYNCOP)
84*d8f1ed8dSOlivier Houchard CK_PR_FENCE(load_store, CK_PR_LWSYNCOP)
851fb62fb0SOlivier Houchard CK_PR_FENCE(memory, "sync")
86*d8f1ed8dSOlivier Houchard CK_PR_FENCE(acquire, CK_PR_LWSYNCOP)
87*d8f1ed8dSOlivier Houchard CK_PR_FENCE(release, CK_PR_LWSYNCOP)
88*d8f1ed8dSOlivier Houchard CK_PR_FENCE(acqrel, CK_PR_LWSYNCOP)
89*d8f1ed8dSOlivier Houchard CK_PR_FENCE(lock, CK_PR_LWSYNCOP)
90*d8f1ed8dSOlivier Houchard CK_PR_FENCE(unlock, CK_PR_LWSYNCOP)
91*d8f1ed8dSOlivier Houchard 
92*d8f1ed8dSOlivier Houchard #undef CK_PR_LWSYNCOP
931fb62fb0SOlivier Houchard 
941fb62fb0SOlivier Houchard #undef CK_PR_FENCE
951fb62fb0SOlivier Houchard 
961fb62fb0SOlivier Houchard #define CK_PR_LOAD(S, M, T, C, I)					\
971fb62fb0SOlivier Houchard 	CK_CC_INLINE static T						\
981fb62fb0SOlivier Houchard 	ck_pr_md_load_##S(const M *target)				\
991fb62fb0SOlivier Houchard 	{								\
1001fb62fb0SOlivier Houchard 		T r;							\
1011fb62fb0SOlivier Houchard 		__asm__ __volatile__(I "%U1%X1 %0, %1"			\
1021fb62fb0SOlivier Houchard 					: "=r" (r)			\
1031fb62fb0SOlivier Houchard 					: "m"  (*(const C *)target)	\
1041fb62fb0SOlivier Houchard 					: "memory");			\
1051fb62fb0SOlivier Houchard 		return (r);						\
1061fb62fb0SOlivier Houchard 	}
1071fb62fb0SOlivier Houchard 
1081fb62fb0SOlivier Houchard CK_PR_LOAD(ptr, void, void *, uint32_t, "lwz")
1091fb62fb0SOlivier Houchard 
1101fb62fb0SOlivier Houchard #define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)
1111fb62fb0SOlivier Houchard 
1121fb62fb0SOlivier Houchard CK_PR_LOAD_S(32, uint32_t, "lwz")
1131fb62fb0SOlivier Houchard CK_PR_LOAD_S(16, uint16_t, "lhz")
1141fb62fb0SOlivier Houchard CK_PR_LOAD_S(8, uint8_t, "lbz")
1151fb62fb0SOlivier Houchard CK_PR_LOAD_S(uint, unsigned int, "lwz")
1161fb62fb0SOlivier Houchard CK_PR_LOAD_S(int, int, "lwz")
1171fb62fb0SOlivier Houchard CK_PR_LOAD_S(short, short, "lhz")
1181fb62fb0SOlivier Houchard CK_PR_LOAD_S(char, char, "lbz")
1191fb62fb0SOlivier Houchard 
1201fb62fb0SOlivier Houchard #undef CK_PR_LOAD_S
1211fb62fb0SOlivier Houchard #undef CK_PR_LOAD
1221fb62fb0SOlivier Houchard 
1231fb62fb0SOlivier Houchard #define CK_PR_STORE(S, M, T, C, I)				\
1241fb62fb0SOlivier Houchard 	CK_CC_INLINE static void				\
1251fb62fb0SOlivier Houchard 	ck_pr_md_store_##S(M *target, T v)			\
1261fb62fb0SOlivier Houchard 	{							\
1271fb62fb0SOlivier Houchard 		__asm__ __volatile__(I "%U0%X0 %1, %0"		\
1281fb62fb0SOlivier Houchard 					: "=m" (*(C *)target)	\
1291fb62fb0SOlivier Houchard 					: "r" (v)		\
1301fb62fb0SOlivier Houchard 					: "memory");		\
1311fb62fb0SOlivier Houchard 		return;						\
1321fb62fb0SOlivier Houchard 	}
1331fb62fb0SOlivier Houchard 
1341fb62fb0SOlivier Houchard CK_PR_STORE(ptr, void, const void *, uint32_t, "stw")
1351fb62fb0SOlivier Houchard 
1361fb62fb0SOlivier Houchard #define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)
1371fb62fb0SOlivier Houchard 
1381fb62fb0SOlivier Houchard CK_PR_STORE_S(32, uint32_t, "stw")
1391fb62fb0SOlivier Houchard CK_PR_STORE_S(16, uint16_t, "sth")
1401fb62fb0SOlivier Houchard CK_PR_STORE_S(8, uint8_t, "stb")
1411fb62fb0SOlivier Houchard CK_PR_STORE_S(uint, unsigned int, "stw")
1421fb62fb0SOlivier Houchard CK_PR_STORE_S(int, int, "stw")
1431fb62fb0SOlivier Houchard CK_PR_STORE_S(short, short, "sth")
1441fb62fb0SOlivier Houchard CK_PR_STORE_S(char, char, "stb")
1451fb62fb0SOlivier Houchard 
1461fb62fb0SOlivier Houchard #undef CK_PR_STORE_S
1471fb62fb0SOlivier Houchard #undef CK_PR_STORE
1481fb62fb0SOlivier Houchard 
1491fb62fb0SOlivier Houchard #define CK_PR_CAS(N, T, M)						\
1501fb62fb0SOlivier Houchard 	CK_CC_INLINE static bool					\
1511fb62fb0SOlivier Houchard 	ck_pr_cas_##N##_value(M *target, T compare, T set, M *value)	\
1521fb62fb0SOlivier Houchard 	{								\
1531fb62fb0SOlivier Houchard 		T previous;						\
1541fb62fb0SOlivier Houchard 		__asm__ __volatile__("1:"				\
1551fb62fb0SOlivier Houchard 				     "lwarx %0, 0, %1;"			\
1561fb62fb0SOlivier Houchard 				     "cmpw  0, %0, %3;"			\
1571fb62fb0SOlivier Houchard 				     "bne-  2f;"			\
1581fb62fb0SOlivier Houchard 				     "stwcx. %2, 0, %1;"		\
1591fb62fb0SOlivier Houchard 				     "bne-  1b;"			\
1601fb62fb0SOlivier Houchard 				     "2:"				\
1611fb62fb0SOlivier Houchard 					: "=&r" (previous)		\
1621fb62fb0SOlivier Houchard 					: "r"   (target),		\
1631fb62fb0SOlivier Houchard 					  "r"   (set),			\
1641fb62fb0SOlivier Houchard 					  "r"   (compare)		\
1651fb62fb0SOlivier Houchard 					: "memory", "cc");		\
1661fb62fb0SOlivier Houchard 		*(T *)value = previous; 				\
1671fb62fb0SOlivier Houchard 		return (previous == compare);				\
1681fb62fb0SOlivier Houchard 	}								\
1691fb62fb0SOlivier Houchard 	CK_CC_INLINE static bool					\
1701fb62fb0SOlivier Houchard 	ck_pr_cas_##N(M *target, T compare, T set)			\
1711fb62fb0SOlivier Houchard 	{								\
1721fb62fb0SOlivier Houchard 		T previous;						\
1731fb62fb0SOlivier Houchard 		__asm__ __volatile__("1:"				\
1741fb62fb0SOlivier Houchard 				     "lwarx %0, 0, %1;"			\
1751fb62fb0SOlivier Houchard 				     "cmpw  0, %0, %3;"			\
1761fb62fb0SOlivier Houchard 				     "bne-  2f;"			\
1771fb62fb0SOlivier Houchard 				     "stwcx. %2, 0, %1;"		\
1781fb62fb0SOlivier Houchard 				     "bne-  1b;"			\
1791fb62fb0SOlivier Houchard 				     "2:"				\
1801fb62fb0SOlivier Houchard 					: "=&r" (previous)		\
1811fb62fb0SOlivier Houchard 					: "r"   (target),		\
1821fb62fb0SOlivier Houchard 					  "r"   (set),			\
1831fb62fb0SOlivier Houchard 					  "r"   (compare)		\
1841fb62fb0SOlivier Houchard 					: "memory", "cc");		\
1851fb62fb0SOlivier Houchard 		return (previous == compare);				\
1861fb62fb0SOlivier Houchard 	}
1871fb62fb0SOlivier Houchard 
1881fb62fb0SOlivier Houchard CK_PR_CAS(ptr, void *, void)
1891fb62fb0SOlivier Houchard #define CK_PR_CAS_S(a, b) CK_PR_CAS(a, b, b)
1901fb62fb0SOlivier Houchard CK_PR_CAS_S(32, uint32_t)
1911fb62fb0SOlivier Houchard CK_PR_CAS_S(uint, unsigned int)
1921fb62fb0SOlivier Houchard CK_PR_CAS_S(int, int)
1931fb62fb0SOlivier Houchard 
1941fb62fb0SOlivier Houchard #undef CK_PR_CAS_S
1951fb62fb0SOlivier Houchard #undef CK_PR_CAS
1961fb62fb0SOlivier Houchard 
1971fb62fb0SOlivier Houchard #define CK_PR_FAS(N, M, T, W)					\
1981fb62fb0SOlivier Houchard 	CK_CC_INLINE static T					\
1991fb62fb0SOlivier Houchard 	ck_pr_fas_##N(M *target, T v)				\
2001fb62fb0SOlivier Houchard 	{							\
2011fb62fb0SOlivier Houchard 		T previous;					\
2021fb62fb0SOlivier Houchard 		__asm__ __volatile__("1:"			\
2031fb62fb0SOlivier Houchard 				     "l" W "arx %0, 0, %1;"	\
2041fb62fb0SOlivier Houchard 				     "st" W "cx. %2, 0, %1;"	\
2051fb62fb0SOlivier Houchard 				     "bne- 1b;"			\
2061fb62fb0SOlivier Houchard 					: "=&r" (previous)	\
2071fb62fb0SOlivier Houchard 					: "r"   (target),	\
2081fb62fb0SOlivier Houchard 					  "r"   (v)		\
2091fb62fb0SOlivier Houchard 					: "memory", "cc");	\
2101fb62fb0SOlivier Houchard 		return (previous);				\
2111fb62fb0SOlivier Houchard 	}
2121fb62fb0SOlivier Houchard 
2131fb62fb0SOlivier Houchard CK_PR_FAS(32, uint32_t, uint32_t, "w")
2141fb62fb0SOlivier Houchard CK_PR_FAS(ptr, void, void *, "w")
2151fb62fb0SOlivier Houchard CK_PR_FAS(int, int, int, "w")
2161fb62fb0SOlivier Houchard CK_PR_FAS(uint, unsigned int, unsigned int, "w")
2171fb62fb0SOlivier Houchard 
2181fb62fb0SOlivier Houchard #undef CK_PR_FAS
2191fb62fb0SOlivier Houchard 
2201fb62fb0SOlivier Houchard #define CK_PR_UNARY(O, N, M, T, I, W)				\
2211fb62fb0SOlivier Houchard 	CK_CC_INLINE static void				\
2221fb62fb0SOlivier Houchard 	ck_pr_##O##_##N(M *target)				\
2231fb62fb0SOlivier Houchard 	{							\
2241fb62fb0SOlivier Houchard 		T previous;					\
2251fb62fb0SOlivier Houchard 		__asm__ __volatile__("1:"			\
2261fb62fb0SOlivier Houchard 				     "l" W "arx %0, 0, %1;"	\
2271fb62fb0SOlivier Houchard 				      I ";"			\
2281fb62fb0SOlivier Houchard 				     "st" W "cx. %0, 0, %1;"	\
2291fb62fb0SOlivier Houchard 				     "bne-  1b;"		\
2301fb62fb0SOlivier Houchard 					: "=&r" (previous)	\
2311fb62fb0SOlivier Houchard 					: "r"   (target)	\
2321fb62fb0SOlivier Houchard 					: "memory", "cc");	\
2331fb62fb0SOlivier Houchard 		return;						\
2341fb62fb0SOlivier Houchard 	}
2351fb62fb0SOlivier Houchard 
2361fb62fb0SOlivier Houchard CK_PR_UNARY(inc, ptr, void, void *, "addic %0, %0, 1", "w")
2371fb62fb0SOlivier Houchard CK_PR_UNARY(dec, ptr, void, void *, "addic %0, %0, -1", "w")
2381fb62fb0SOlivier Houchard CK_PR_UNARY(not, ptr, void, void *, "not %0, %0", "w")
2391fb62fb0SOlivier Houchard CK_PR_UNARY(neg, ptr, void, void *, "neg %0, %0", "w")
2401fb62fb0SOlivier Houchard 
2411fb62fb0SOlivier Houchard #define CK_PR_UNARY_S(S, T, W)					\
2421fb62fb0SOlivier Houchard 	CK_PR_UNARY(inc, S, T, T, "addic %0, %0, 1", W)		\
2431fb62fb0SOlivier Houchard 	CK_PR_UNARY(dec, S, T, T, "addic %0, %0, -1", W)	\
2441fb62fb0SOlivier Houchard 	CK_PR_UNARY(not, S, T, T, "not %0, %0", W)		\
2451fb62fb0SOlivier Houchard 	CK_PR_UNARY(neg, S, T, T, "neg %0, %0", W)
2461fb62fb0SOlivier Houchard 
2471fb62fb0SOlivier Houchard CK_PR_UNARY_S(32, uint32_t, "w")
2481fb62fb0SOlivier Houchard CK_PR_UNARY_S(uint, unsigned int, "w")
2491fb62fb0SOlivier Houchard CK_PR_UNARY_S(int, int, "w")
2501fb62fb0SOlivier Houchard 
2511fb62fb0SOlivier Houchard #undef CK_PR_UNARY_S
2521fb62fb0SOlivier Houchard #undef CK_PR_UNARY
2531fb62fb0SOlivier Houchard 
2541fb62fb0SOlivier Houchard #define CK_PR_BINARY(O, N, M, T, I, W)				\
2551fb62fb0SOlivier Houchard 	CK_CC_INLINE static void				\
2561fb62fb0SOlivier Houchard 	ck_pr_##O##_##N(M *target, T delta)			\
2571fb62fb0SOlivier Houchard 	{							\
2581fb62fb0SOlivier Houchard 		T previous;					\
2591fb62fb0SOlivier Houchard 		__asm__ __volatile__("1:"			\
2601fb62fb0SOlivier Houchard 				     "l" W "arx %0, 0, %1;"	\
2611fb62fb0SOlivier Houchard 				      I " %0, %2, %0;"		\
2621fb62fb0SOlivier Houchard 				     "st" W "cx. %0, 0, %1;"	\
2631fb62fb0SOlivier Houchard 				     "bne-  1b;"		\
2641fb62fb0SOlivier Houchard 					: "=&r" (previous)	\
2651fb62fb0SOlivier Houchard 					: "r"   (target),	\
2661fb62fb0SOlivier Houchard 					  "r"   (delta)		\
2671fb62fb0SOlivier Houchard 					: "memory", "cc");	\
2681fb62fb0SOlivier Houchard 		return;						\
2691fb62fb0SOlivier Houchard 	}
2701fb62fb0SOlivier Houchard 
2711fb62fb0SOlivier Houchard CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "w")
2721fb62fb0SOlivier Houchard CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "w")
2731fb62fb0SOlivier Houchard CK_PR_BINARY(or, ptr, void, uintptr_t, "or", "w")
2741fb62fb0SOlivier Houchard CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "w")
2751fb62fb0SOlivier Houchard CK_PR_BINARY(xor, ptr, void, uintptr_t, "xor", "w")
2761fb62fb0SOlivier Houchard 
2771fb62fb0SOlivier Houchard #define CK_PR_BINARY_S(S, T, W)			\
2781fb62fb0SOlivier Houchard 	CK_PR_BINARY(and, S, T, T, "and", W)	\
2791fb62fb0SOlivier Houchard 	CK_PR_BINARY(add, S, T, T, "add", W)	\
2801fb62fb0SOlivier Houchard 	CK_PR_BINARY(or, S, T, T, "or", W)	\
2811fb62fb0SOlivier Houchard 	CK_PR_BINARY(sub, S, T, T, "subf", W)	\
2821fb62fb0SOlivier Houchard 	CK_PR_BINARY(xor, S, T, T, "xor", W)
2831fb62fb0SOlivier Houchard 
2841fb62fb0SOlivier Houchard CK_PR_BINARY_S(32, uint32_t, "w")
2851fb62fb0SOlivier Houchard CK_PR_BINARY_S(uint, unsigned int, "w")
2861fb62fb0SOlivier Houchard CK_PR_BINARY_S(int, int, "w")
2871fb62fb0SOlivier Houchard 
2881fb62fb0SOlivier Houchard #undef CK_PR_BINARY_S
2891fb62fb0SOlivier Houchard #undef CK_PR_BINARY
2901fb62fb0SOlivier Houchard 
2911fb62fb0SOlivier Houchard CK_CC_INLINE static void *
2921fb62fb0SOlivier Houchard ck_pr_faa_ptr(void *target, uintptr_t delta)
2931fb62fb0SOlivier Houchard {
2941fb62fb0SOlivier Houchard 	uintptr_t previous, r;
2951fb62fb0SOlivier Houchard 
2961fb62fb0SOlivier Houchard 	__asm__ __volatile__("1:"
2971fb62fb0SOlivier Houchard 			     "lwarx %0, 0, %2;"
2981fb62fb0SOlivier Houchard 			     "add %1, %3, %0;"
2991fb62fb0SOlivier Houchard 			     "stwcx. %1, 0, %2;"
3001fb62fb0SOlivier Houchard 			     "bne-  1b;"
3011fb62fb0SOlivier Houchard 				: "=&r" (previous),
3021fb62fb0SOlivier Houchard 				  "=&r" (r)
3031fb62fb0SOlivier Houchard 				: "r"   (target),
3041fb62fb0SOlivier Houchard 				  "r"   (delta)
3051fb62fb0SOlivier Houchard 				: "memory", "cc");
3061fb62fb0SOlivier Houchard 
3071fb62fb0SOlivier Houchard 	return (void *)(previous);
3081fb62fb0SOlivier Houchard }
3091fb62fb0SOlivier Houchard 
3101fb62fb0SOlivier Houchard #define CK_PR_FAA(S, T, W)						\
3111fb62fb0SOlivier Houchard 	CK_CC_INLINE static T						\
3121fb62fb0SOlivier Houchard 	ck_pr_faa_##S(T *target, T delta)				\
3131fb62fb0SOlivier Houchard 	{								\
3141fb62fb0SOlivier Houchard 		T previous, r;						\
3151fb62fb0SOlivier Houchard 		__asm__ __volatile__("1:"				\
3161fb62fb0SOlivier Houchard 				     "l" W "arx %0, 0, %2;"		\
3171fb62fb0SOlivier Houchard 				     "add %1, %3, %0;"			\
3181fb62fb0SOlivier Houchard 				     "st" W "cx. %1, 0, %2;"		\
3191fb62fb0SOlivier Houchard 				     "bne-  1b;"			\
3201fb62fb0SOlivier Houchard 					: "=&r" (previous),		\
3211fb62fb0SOlivier Houchard 					  "=&r" (r)			\
3221fb62fb0SOlivier Houchard 					: "r"   (target),		\
3231fb62fb0SOlivier Houchard 					  "r"   (delta)			\
3241fb62fb0SOlivier Houchard 					: "memory", "cc");		\
3251fb62fb0SOlivier Houchard 		return (previous);					\
3261fb62fb0SOlivier Houchard 	}
3271fb62fb0SOlivier Houchard 
3281fb62fb0SOlivier Houchard CK_PR_FAA(32, uint32_t, "w")
3291fb62fb0SOlivier Houchard CK_PR_FAA(uint, unsigned int, "w")
3301fb62fb0SOlivier Houchard CK_PR_FAA(int, int, "w")
3311fb62fb0SOlivier Houchard 
3321fb62fb0SOlivier Houchard #undef CK_PR_FAA
3331fb62fb0SOlivier Houchard 
3341fb62fb0SOlivier Houchard #endif /* CK_PR_PPC_H */
3351fb62fb0SOlivier Houchard 
336