xref: /openbmc/linux/tools/testing/selftests/powerpc/include/instructions.h (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
215ec3997SSimon Guo #ifndef _SELFTESTS_POWERPC_INSTRUCTIONS_H
315ec3997SSimon Guo #define _SELFTESTS_POWERPC_INSTRUCTIONS_H
415ec3997SSimon Guo 
515ec3997SSimon Guo #include <stdio.h>
615ec3997SSimon Guo #include <stdlib.h>
715ec3997SSimon Guo 
815ec3997SSimon Guo /* This defines the "copy" instruction from Power ISA 3.0 Book II, section 4.4. */
915ec3997SSimon Guo #define __COPY(RA, RB, L) \
1015ec3997SSimon Guo 	(0x7c00060c | (RA) << (31-15) | (RB) << (31-20) | (L) << (31-10))
1115ec3997SSimon Guo #define COPY(RA, RB, L) \
1215ec3997SSimon Guo 	.long __COPY((RA), (RB), (L))
1315ec3997SSimon Guo 
copy(void * i)1415ec3997SSimon Guo static inline void copy(void *i)
1515ec3997SSimon Guo {
1615ec3997SSimon Guo 	asm volatile(str(COPY(0, %0, 0))";"
1715ec3997SSimon Guo 			:
1815ec3997SSimon Guo 			: "b" (i)
1915ec3997SSimon Guo 			: "memory"
2015ec3997SSimon Guo 		    );
2115ec3997SSimon Guo }
2215ec3997SSimon Guo 
copy_first(void * i)2315ec3997SSimon Guo static inline void copy_first(void *i)
2415ec3997SSimon Guo {
2515ec3997SSimon Guo 	asm volatile(str(COPY(0, %0, 1))";"
2615ec3997SSimon Guo 			:
2715ec3997SSimon Guo 			: "b" (i)
2815ec3997SSimon Guo 			: "memory"
2915ec3997SSimon Guo 		    );
3015ec3997SSimon Guo }
3115ec3997SSimon Guo 
3215ec3997SSimon Guo /* This defines the "paste" instruction from Power ISA 3.0 Book II, section 4.4. */
3315ec3997SSimon Guo #define __PASTE(RA, RB, L, RC) \
3415ec3997SSimon Guo 	(0x7c00070c | (RA) << (31-15) | (RB) << (31-20) | (L) << (31-10) | (RC) << (31-31))
3515ec3997SSimon Guo #define PASTE(RA, RB, L, RC) \
3615ec3997SSimon Guo 	.long __PASTE((RA), (RB), (L), (RC))
3715ec3997SSimon Guo 
paste(void * i)3815ec3997SSimon Guo static inline int paste(void *i)
3915ec3997SSimon Guo {
4015ec3997SSimon Guo 	int cr;
4115ec3997SSimon Guo 
4215ec3997SSimon Guo 	asm volatile(str(PASTE(0, %1, 0, 0))";"
4315ec3997SSimon Guo 			"mfcr %0;"
4415ec3997SSimon Guo 			: "=r" (cr)
4515ec3997SSimon Guo 			: "b" (i)
4615ec3997SSimon Guo 			: "memory"
4715ec3997SSimon Guo 		    );
4815ec3997SSimon Guo 	return cr;
4915ec3997SSimon Guo }
5015ec3997SSimon Guo 
paste_last(void * i)5115ec3997SSimon Guo static inline int paste_last(void *i)
5215ec3997SSimon Guo {
5315ec3997SSimon Guo 	int cr;
5415ec3997SSimon Guo 
5515ec3997SSimon Guo 	asm volatile(str(PASTE(0, %1, 1, 1))";"
5615ec3997SSimon Guo 			"mfcr %0;"
5715ec3997SSimon Guo 			: "=r" (cr)
5815ec3997SSimon Guo 			: "b" (i)
5915ec3997SSimon Guo 			: "memory"
6015ec3997SSimon Guo 		    );
6115ec3997SSimon Guo 	return cr;
6215ec3997SSimon Guo }
6315ec3997SSimon Guo 
6415ec3997SSimon Guo #define PPC_INST_COPY                  __COPY(0, 0, 0)
6515ec3997SSimon Guo #define PPC_INST_COPY_FIRST            __COPY(0, 0, 1)
6615ec3997SSimon Guo #define PPC_INST_PASTE                 __PASTE(0, 0, 0, 0)
6715ec3997SSimon Guo #define PPC_INST_PASTE_LAST            __PASTE(0, 0, 1, 1)
6815ec3997SSimon Guo 
69*620a6473SJordan Niethe /* This defines the prefixed load/store instructions */
70*620a6473SJordan Niethe #ifdef __ASSEMBLY__
71*620a6473SJordan Niethe #  define stringify_in_c(...)	__VA_ARGS__
72*620a6473SJordan Niethe #else
73*620a6473SJordan Niethe #  define __stringify_in_c(...)	#__VA_ARGS__
74*620a6473SJordan Niethe #  define stringify_in_c(...)	__stringify_in_c(__VA_ARGS__) " "
75*620a6473SJordan Niethe #endif
76*620a6473SJordan Niethe 
77*620a6473SJordan Niethe #define __PPC_RA(a)	(((a) & 0x1f) << 16)
78*620a6473SJordan Niethe #define __PPC_RS(s)	(((s) & 0x1f) << 21)
79*620a6473SJordan Niethe #define __PPC_RT(t)	__PPC_RS(t)
80*620a6473SJordan Niethe #define __PPC_PREFIX_R(r)	(((r) & 0x1) << 20)
81*620a6473SJordan Niethe 
82*620a6473SJordan Niethe #define PPC_PREFIX_MLS			0x06000000
83*620a6473SJordan Niethe #define PPC_PREFIX_8LS			0x04000000
84*620a6473SJordan Niethe 
85*620a6473SJordan Niethe #define PPC_INST_LBZ			0x88000000
86*620a6473SJordan Niethe #define PPC_INST_LHZ			0xa0000000
87*620a6473SJordan Niethe #define PPC_INST_LHA			0xa8000000
88*620a6473SJordan Niethe #define PPC_INST_LWZ			0x80000000
89*620a6473SJordan Niethe #define PPC_INST_STB			0x98000000
90*620a6473SJordan Niethe #define PPC_INST_STH			0xb0000000
91*620a6473SJordan Niethe #define PPC_INST_STW			0x90000000
92*620a6473SJordan Niethe #define PPC_INST_STD			0xf8000000
93*620a6473SJordan Niethe #define PPC_INST_LFS			0xc0000000
94*620a6473SJordan Niethe #define PPC_INST_LFD			0xc8000000
95*620a6473SJordan Niethe #define PPC_INST_STFS			0xd0000000
96*620a6473SJordan Niethe #define PPC_INST_STFD			0xd8000000
97*620a6473SJordan Niethe 
98*620a6473SJordan Niethe #define PREFIX_MLS(instr, t, a, r, d)	stringify_in_c(.balign 64, , 4;)		\
99*620a6473SJordan Niethe 					stringify_in_c(.long PPC_PREFIX_MLS |		\
100*620a6473SJordan Niethe 						       __PPC_PREFIX_R(r) |		\
101*620a6473SJordan Niethe 						       (((d) >> 16) & 0x3ffff);)	\
102*620a6473SJordan Niethe 					stringify_in_c(.long (instr)  |			\
103*620a6473SJordan Niethe 						       __PPC_RT(t) |			\
104*620a6473SJordan Niethe 						       __PPC_RA(a) |			\
105*620a6473SJordan Niethe 						       ((d) & 0xffff);\n)
106*620a6473SJordan Niethe 
107*620a6473SJordan Niethe #define PREFIX_8LS(instr, t, a, r, d)	stringify_in_c(.balign 64, , 4;)		\
108*620a6473SJordan Niethe 					stringify_in_c(.long PPC_PREFIX_8LS |		\
109*620a6473SJordan Niethe 						       __PPC_PREFIX_R(r) |		\
110*620a6473SJordan Niethe 						       (((d) >> 16) & 0x3ffff);)	\
111*620a6473SJordan Niethe 					stringify_in_c(.long (instr)  |			\
112*620a6473SJordan Niethe 						       __PPC_RT(t) |			\
113*620a6473SJordan Niethe 						       __PPC_RA(a) |			\
114*620a6473SJordan Niethe 						       ((d) & 0xffff);\n)
115*620a6473SJordan Niethe 
116*620a6473SJordan Niethe /* Prefixed Integer Load/Store instructions */
117*620a6473SJordan Niethe #define PLBZ(t, a, r, d)		PREFIX_MLS(PPC_INST_LBZ, t, a, r, d)
118*620a6473SJordan Niethe #define PLHZ(t, a, r, d)		PREFIX_MLS(PPC_INST_LHZ, t, a, r, d)
119*620a6473SJordan Niethe #define PLHA(t, a, r, d)		PREFIX_MLS(PPC_INST_LHA, t, a, r, d)
120*620a6473SJordan Niethe #define PLWZ(t, a, r, d)		PREFIX_MLS(PPC_INST_LWZ, t, a, r, d)
121*620a6473SJordan Niethe #define PLWA(t, a, r, d)		PREFIX_8LS(0xa4000000, t, a, r, d)
122*620a6473SJordan Niethe #define PLD(t, a, r, d)			PREFIX_8LS(0xe4000000, t, a, r, d)
123*620a6473SJordan Niethe #define PLQ(t, a, r, d)			PREFIX_8LS(0xe0000000, t, a, r, d)
124*620a6473SJordan Niethe #define PSTB(s, a, r, d)		PREFIX_MLS(PPC_INST_STB, s, a, r, d)
125*620a6473SJordan Niethe #define PSTH(s, a, r, d)		PREFIX_MLS(PPC_INST_STH, s, a, r, d)
126*620a6473SJordan Niethe #define PSTW(s, a, r, d)		PREFIX_MLS(PPC_INST_STW, s, a, r, d)
127*620a6473SJordan Niethe #define PSTD(s, a, r, d)		PREFIX_8LS(0xf4000000, s, a, r, d)
128*620a6473SJordan Niethe #define PSTQ(s, a, r, d)		PREFIX_8LS(0xf0000000, s, a, r, d)
129*620a6473SJordan Niethe 
130*620a6473SJordan Niethe /* Prefixed Floating-Point Load/Store Instructions */
131*620a6473SJordan Niethe #define PLFS(frt, a, r, d)		PREFIX_MLS(PPC_INST_LFS, frt, a, r, d)
132*620a6473SJordan Niethe #define PLFD(frt, a, r, d)		PREFIX_MLS(PPC_INST_LFD, frt, a, r, d)
133*620a6473SJordan Niethe #define PSTFS(frs, a, r, d)		PREFIX_MLS(PPC_INST_STFS, frs, a, r, d)
134*620a6473SJordan Niethe #define PSTFD(frs, a, r, d)		PREFIX_MLS(PPC_INST_STFD, frs, a, r, d)
135*620a6473SJordan Niethe 
136*620a6473SJordan Niethe /* Prefixed VSX Load/Store Instructions */
137*620a6473SJordan Niethe #define PLXSD(vrt, a, r, d)		PREFIX_8LS(0xa8000000, vrt, a, r, d)
138*620a6473SJordan Niethe #define PLXSSP(vrt, a, r, d)		PREFIX_8LS(0xac000000, vrt, a, r, d)
139*620a6473SJordan Niethe #define PLXV0(s, a, r, d)		PREFIX_8LS(0xc8000000, s, a, r, d)
140*620a6473SJordan Niethe #define PLXV1(s, a, r, d)		PREFIX_8LS(0xcc000000, s, a, r, d)
141*620a6473SJordan Niethe #define PSTXSD(vrs, a, r, d)		PREFIX_8LS(0xb8000000, vrs, a, r, d)
142*620a6473SJordan Niethe #define PSTXSSP(vrs, a, r, d)		PREFIX_8LS(0xbc000000, vrs, a, r, d)
143*620a6473SJordan Niethe #define PSTXV0(s, a, r, d)		PREFIX_8LS(0xd8000000, s, a, r, d)
144*620a6473SJordan Niethe #define PSTXV1(s, a, r, d)		PREFIX_8LS(0xdc000000, s, a, r, d)
145*620a6473SJordan Niethe 
14615ec3997SSimon Guo #endif /* _SELFTESTS_POWERPC_INSTRUCTIONS_H */
147