xref: /linux/arch/riscv/include/asm/insn.h (revision d6fd48ef)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2020 SiFive
4  */
5 
6 #ifndef _ASM_RISCV_INSN_H
7 #define _ASM_RISCV_INSN_H
8 
9 #include <linux/bits.h>
10 
11 #define RV_INSN_FUNCT3_MASK	GENMASK(14, 12)
12 #define RV_INSN_FUNCT3_OPOFF	12
13 #define RV_INSN_OPCODE_MASK	GENMASK(6, 0)
14 #define RV_INSN_OPCODE_OPOFF	0
15 #define RV_INSN_FUNCT12_OPOFF	20
16 
17 #define RV_ENCODE_FUNCT3(f_)	(RVG_FUNCT3_##f_ << RV_INSN_FUNCT3_OPOFF)
18 #define RV_ENCODE_FUNCT12(f_)	(RVG_FUNCT12_##f_ << RV_INSN_FUNCT12_OPOFF)
19 
20 /* The bit field of immediate value in I-type instruction */
21 #define RV_I_IMM_SIGN_OPOFF	31
22 #define RV_I_IMM_11_0_OPOFF	20
23 #define RV_I_IMM_SIGN_OFF	12
24 #define RV_I_IMM_11_0_OFF	0
25 #define RV_I_IMM_11_0_MASK	GENMASK(11, 0)
26 
27 /* The bit field of immediate value in J-type instruction */
28 #define RV_J_IMM_SIGN_OPOFF	31
29 #define RV_J_IMM_10_1_OPOFF	21
30 #define RV_J_IMM_11_OPOFF	20
31 #define RV_J_IMM_19_12_OPOFF	12
32 #define RV_J_IMM_SIGN_OFF	20
33 #define RV_J_IMM_10_1_OFF	1
34 #define RV_J_IMM_11_OFF		11
35 #define RV_J_IMM_19_12_OFF	12
36 #define RV_J_IMM_10_1_MASK	GENMASK(9, 0)
37 #define RV_J_IMM_11_MASK	GENMASK(0, 0)
38 #define RV_J_IMM_19_12_MASK	GENMASK(7, 0)
39 
40 /*
41  * U-type IMMs contain the upper 20bits [31:20] of an immediate with
42  * the rest filled in by zeros, so no shifting required. Similarly,
43  * bit31 contains the signed state, so no sign extension necessary.
44  */
45 #define RV_U_IMM_SIGN_OPOFF	31
46 #define RV_U_IMM_31_12_OPOFF	0
47 #define RV_U_IMM_31_12_MASK	GENMASK(31, 12)
48 
49 /* The bit field of immediate value in B-type instruction */
50 #define RV_B_IMM_SIGN_OPOFF	31
51 #define RV_B_IMM_10_5_OPOFF	25
52 #define RV_B_IMM_4_1_OPOFF	8
53 #define RV_B_IMM_11_OPOFF	7
54 #define RV_B_IMM_SIGN_OFF	12
55 #define RV_B_IMM_10_5_OFF	5
56 #define RV_B_IMM_4_1_OFF	1
57 #define RV_B_IMM_11_OFF		11
58 #define RV_B_IMM_10_5_MASK	GENMASK(5, 0)
59 #define RV_B_IMM_4_1_MASK	GENMASK(3, 0)
60 #define RV_B_IMM_11_MASK	GENMASK(0, 0)
61 
62 /* The register offset in RVG instruction */
63 #define RVG_RS1_OPOFF		15
64 #define RVG_RS2_OPOFF		20
65 #define RVG_RD_OPOFF		7
66 #define RVG_RD_MASK		GENMASK(4, 0)
67 
68 /* The bit field of immediate value in RVC J instruction */
69 #define RVC_J_IMM_SIGN_OPOFF	12
70 #define RVC_J_IMM_4_OPOFF	11
71 #define RVC_J_IMM_9_8_OPOFF	9
72 #define RVC_J_IMM_10_OPOFF	8
73 #define RVC_J_IMM_6_OPOFF	7
74 #define RVC_J_IMM_7_OPOFF	6
75 #define RVC_J_IMM_3_1_OPOFF	3
76 #define RVC_J_IMM_5_OPOFF	2
77 #define RVC_J_IMM_SIGN_OFF	11
78 #define RVC_J_IMM_4_OFF		4
79 #define RVC_J_IMM_9_8_OFF	8
80 #define RVC_J_IMM_10_OFF	10
81 #define RVC_J_IMM_6_OFF		6
82 #define RVC_J_IMM_7_OFF		7
83 #define RVC_J_IMM_3_1_OFF	1
84 #define RVC_J_IMM_5_OFF		5
85 #define RVC_J_IMM_4_MASK	GENMASK(0, 0)
86 #define RVC_J_IMM_9_8_MASK	GENMASK(1, 0)
87 #define RVC_J_IMM_10_MASK	GENMASK(0, 0)
88 #define RVC_J_IMM_6_MASK	GENMASK(0, 0)
89 #define RVC_J_IMM_7_MASK	GENMASK(0, 0)
90 #define RVC_J_IMM_3_1_MASK	GENMASK(2, 0)
91 #define RVC_J_IMM_5_MASK	GENMASK(0, 0)
92 
93 /* The bit field of immediate value in RVC B instruction */
94 #define RVC_B_IMM_SIGN_OPOFF	12
95 #define RVC_B_IMM_4_3_OPOFF	10
96 #define RVC_B_IMM_7_6_OPOFF	5
97 #define RVC_B_IMM_2_1_OPOFF	3
98 #define RVC_B_IMM_5_OPOFF	2
99 #define RVC_B_IMM_SIGN_OFF	8
100 #define RVC_B_IMM_4_3_OFF	3
101 #define RVC_B_IMM_7_6_OFF	6
102 #define RVC_B_IMM_2_1_OFF	1
103 #define RVC_B_IMM_5_OFF		5
104 #define RVC_B_IMM_4_3_MASK	GENMASK(1, 0)
105 #define RVC_B_IMM_7_6_MASK	GENMASK(1, 0)
106 #define RVC_B_IMM_2_1_MASK	GENMASK(1, 0)
107 #define RVC_B_IMM_5_MASK	GENMASK(0, 0)
108 
109 #define RVC_INSN_FUNCT4_MASK	GENMASK(15, 12)
110 #define RVC_INSN_FUNCT4_OPOFF	12
111 #define RVC_INSN_FUNCT3_MASK	GENMASK(15, 13)
112 #define RVC_INSN_FUNCT3_OPOFF	13
113 #define RVC_INSN_J_RS2_MASK	GENMASK(6, 2)
114 #define RVC_INSN_OPCODE_MASK	GENMASK(1, 0)
115 #define RVC_ENCODE_FUNCT3(f_)	(RVC_FUNCT3_##f_ << RVC_INSN_FUNCT3_OPOFF)
116 #define RVC_ENCODE_FUNCT4(f_)	(RVC_FUNCT4_##f_ << RVC_INSN_FUNCT4_OPOFF)
117 
118 /* The register offset in RVC op=C0 instruction */
119 #define RVC_C0_RS1_OPOFF	7
120 #define RVC_C0_RS2_OPOFF	2
121 #define RVC_C0_RD_OPOFF		2
122 
123 /* The register offset in RVC op=C1 instruction */
124 #define RVC_C1_RS1_OPOFF	7
125 #define RVC_C1_RS2_OPOFF	2
126 #define RVC_C1_RD_OPOFF		7
127 
128 /* The register offset in RVC op=C2 instruction */
129 #define RVC_C2_RS1_OPOFF	7
130 #define RVC_C2_RS2_OPOFF	2
131 #define RVC_C2_RD_OPOFF		7
132 
133 /* parts of opcode for RVG*/
134 #define RVG_OPCODE_FENCE	0x0f
135 #define RVG_OPCODE_AUIPC	0x17
136 #define RVG_OPCODE_BRANCH	0x63
137 #define RVG_OPCODE_JALR		0x67
138 #define RVG_OPCODE_JAL		0x6f
139 #define RVG_OPCODE_SYSTEM	0x73
140 
141 /* parts of opcode for RVC*/
142 #define RVC_OPCODE_C0		0x0
143 #define RVC_OPCODE_C1		0x1
144 #define RVC_OPCODE_C2		0x2
145 
146 /* parts of funct3 code for I, M, A extension*/
147 #define RVG_FUNCT3_JALR		0x0
148 #define RVG_FUNCT3_BEQ		0x0
149 #define RVG_FUNCT3_BNE		0x1
150 #define RVG_FUNCT3_BLT		0x4
151 #define RVG_FUNCT3_BGE		0x5
152 #define RVG_FUNCT3_BLTU		0x6
153 #define RVG_FUNCT3_BGEU		0x7
154 
155 /* parts of funct3 code for C extension*/
156 #define RVC_FUNCT3_C_BEQZ	0x6
157 #define RVC_FUNCT3_C_BNEZ	0x7
158 #define RVC_FUNCT3_C_J		0x5
159 #define RVC_FUNCT3_C_JAL	0x1
160 #define RVC_FUNCT4_C_JR		0x8
161 #define RVC_FUNCT4_C_JALR	0x9
162 #define RVC_FUNCT4_C_EBREAK	0x9
163 
164 #define RVG_FUNCT12_EBREAK	0x1
165 #define RVG_FUNCT12_SRET	0x102
166 
167 #define RVG_MATCH_AUIPC		(RVG_OPCODE_AUIPC)
168 #define RVG_MATCH_JALR		(RV_ENCODE_FUNCT3(JALR) | RVG_OPCODE_JALR)
169 #define RVG_MATCH_JAL		(RVG_OPCODE_JAL)
170 #define RVG_MATCH_FENCE		(RVG_OPCODE_FENCE)
171 #define RVG_MATCH_BEQ		(RV_ENCODE_FUNCT3(BEQ) | RVG_OPCODE_BRANCH)
172 #define RVG_MATCH_BNE		(RV_ENCODE_FUNCT3(BNE) | RVG_OPCODE_BRANCH)
173 #define RVG_MATCH_BLT		(RV_ENCODE_FUNCT3(BLT) | RVG_OPCODE_BRANCH)
174 #define RVG_MATCH_BGE		(RV_ENCODE_FUNCT3(BGE) | RVG_OPCODE_BRANCH)
175 #define RVG_MATCH_BLTU		(RV_ENCODE_FUNCT3(BLTU) | RVG_OPCODE_BRANCH)
176 #define RVG_MATCH_BGEU		(RV_ENCODE_FUNCT3(BGEU) | RVG_OPCODE_BRANCH)
177 #define RVG_MATCH_EBREAK	(RV_ENCODE_FUNCT12(EBREAK) | RVG_OPCODE_SYSTEM)
178 #define RVG_MATCH_SRET		(RV_ENCODE_FUNCT12(SRET) | RVG_OPCODE_SYSTEM)
179 #define RVC_MATCH_C_BEQZ	(RVC_ENCODE_FUNCT3(C_BEQZ) | RVC_OPCODE_C1)
180 #define RVC_MATCH_C_BNEZ	(RVC_ENCODE_FUNCT3(C_BNEZ) | RVC_OPCODE_C1)
181 #define RVC_MATCH_C_J		(RVC_ENCODE_FUNCT3(C_J) | RVC_OPCODE_C1)
182 #define RVC_MATCH_C_JAL		(RVC_ENCODE_FUNCT3(C_JAL) | RVC_OPCODE_C1)
183 #define RVC_MATCH_C_JR		(RVC_ENCODE_FUNCT4(C_JR) | RVC_OPCODE_C2)
184 #define RVC_MATCH_C_JALR	(RVC_ENCODE_FUNCT4(C_JALR) | RVC_OPCODE_C2)
185 #define RVC_MATCH_C_EBREAK	(RVC_ENCODE_FUNCT4(C_EBREAK) | RVC_OPCODE_C2)
186 
187 #define RVG_MASK_AUIPC		(RV_INSN_OPCODE_MASK)
188 #define RVG_MASK_JALR		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
189 #define RVG_MASK_JAL		(RV_INSN_OPCODE_MASK)
190 #define RVG_MASK_FENCE		(RV_INSN_OPCODE_MASK)
191 #define RVC_MASK_C_JALR		(RVC_INSN_FUNCT4_MASK | RVC_INSN_J_RS2_MASK | RVC_INSN_OPCODE_MASK)
192 #define RVC_MASK_C_JR		(RVC_INSN_FUNCT4_MASK | RVC_INSN_J_RS2_MASK | RVC_INSN_OPCODE_MASK)
193 #define RVC_MASK_C_JAL		(RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
194 #define RVC_MASK_C_J		(RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
195 #define RVG_MASK_BEQ		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
196 #define RVG_MASK_BNE		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
197 #define RVG_MASK_BLT		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
198 #define RVG_MASK_BGE		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
199 #define RVG_MASK_BLTU		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
200 #define RVG_MASK_BGEU		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
201 #define RVC_MASK_C_BEQZ		(RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
202 #define RVC_MASK_C_BNEZ		(RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
203 #define RVC_MASK_C_EBREAK	0xffff
204 #define RVG_MASK_EBREAK		0xffffffff
205 #define RVG_MASK_SRET		0xffffffff
206 
207 #define __INSN_LENGTH_MASK	_UL(0x3)
208 #define __INSN_LENGTH_GE_32	_UL(0x3)
209 #define __INSN_OPCODE_MASK	_UL(0x7F)
210 #define __INSN_BRANCH_OPCODE	_UL(RVG_OPCODE_BRANCH)
211 
212 #define __RISCV_INSN_FUNCS(name, mask, val)				\
213 static __always_inline bool riscv_insn_is_##name(u32 code)		\
214 {									\
215 	BUILD_BUG_ON(~(mask) & (val));					\
216 	return (code & (mask)) == (val);				\
217 }									\
218 
219 #if __riscv_xlen == 32
220 /* C.JAL is an RV32C-only instruction */
221 __RISCV_INSN_FUNCS(c_jal, RVC_MASK_C_JAL, RVC_MATCH_C_JAL)
222 #else
223 #define riscv_insn_is_c_jal(opcode) 0
224 #endif
225 __RISCV_INSN_FUNCS(auipc, RVG_MASK_AUIPC, RVG_MATCH_AUIPC)
226 __RISCV_INSN_FUNCS(jalr, RVG_MASK_JALR, RVG_MATCH_JALR)
227 __RISCV_INSN_FUNCS(jal, RVG_MASK_JAL, RVG_MATCH_JAL)
228 __RISCV_INSN_FUNCS(c_jr, RVC_MASK_C_JR, RVC_MATCH_C_JR)
229 __RISCV_INSN_FUNCS(c_jalr, RVC_MASK_C_JALR, RVC_MATCH_C_JALR)
230 __RISCV_INSN_FUNCS(c_j, RVC_MASK_C_J, RVC_MATCH_C_J)
231 __RISCV_INSN_FUNCS(beq, RVG_MASK_BEQ, RVG_MATCH_BEQ)
232 __RISCV_INSN_FUNCS(bne, RVG_MASK_BNE, RVG_MATCH_BNE)
233 __RISCV_INSN_FUNCS(blt, RVG_MASK_BLT, RVG_MATCH_BLT)
234 __RISCV_INSN_FUNCS(bge, RVG_MASK_BGE, RVG_MATCH_BGE)
235 __RISCV_INSN_FUNCS(bltu, RVG_MASK_BLTU, RVG_MATCH_BLTU)
236 __RISCV_INSN_FUNCS(bgeu, RVG_MASK_BGEU, RVG_MATCH_BGEU)
237 __RISCV_INSN_FUNCS(c_beqz, RVC_MASK_C_BEQZ, RVC_MATCH_C_BEQZ)
238 __RISCV_INSN_FUNCS(c_bnez, RVC_MASK_C_BNEZ, RVC_MATCH_C_BNEZ)
239 __RISCV_INSN_FUNCS(c_ebreak, RVC_MASK_C_EBREAK, RVC_MATCH_C_EBREAK)
240 __RISCV_INSN_FUNCS(ebreak, RVG_MASK_EBREAK, RVG_MATCH_EBREAK)
241 __RISCV_INSN_FUNCS(sret, RVG_MASK_SRET, RVG_MATCH_SRET)
242 __RISCV_INSN_FUNCS(fence, RVG_MASK_FENCE, RVG_MATCH_FENCE);
243 
244 /* special case to catch _any_ system instruction */
245 static __always_inline bool riscv_insn_is_system(u32 code)
246 {
247 	return (code & RV_INSN_OPCODE_MASK) == RVG_OPCODE_SYSTEM;
248 }
249 
250 /* special case to catch _any_ branch instruction */
251 static __always_inline bool riscv_insn_is_branch(u32 code)
252 {
253 	return (code & RV_INSN_OPCODE_MASK) == RVG_OPCODE_BRANCH;
254 }
255 
256 #define RV_IMM_SIGN(x) (-(((x) >> 31) & 1))
257 #define RVC_IMM_SIGN(x) (-(((x) >> 12) & 1))
258 #define RV_X(X, s, mask)  (((X) >> (s)) & (mask))
259 #define RVC_X(X, s, mask) RV_X(X, s, mask)
260 
261 #define RV_EXTRACT_RD_REG(x) \
262 	({typeof(x) x_ = (x); \
263 	(RV_X(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
264 
265 #define RV_EXTRACT_UTYPE_IMM(x) \
266 	({typeof(x) x_ = (x); \
267 	(RV_X(x_, RV_U_IMM_31_12_OPOFF, RV_U_IMM_31_12_MASK)); })
268 
269 #define RV_EXTRACT_JTYPE_IMM(x) \
270 	({typeof(x) x_ = (x); \
271 	(RV_X(x_, RV_J_IMM_10_1_OPOFF, RV_J_IMM_10_1_MASK) << RV_J_IMM_10_1_OFF) | \
272 	(RV_X(x_, RV_J_IMM_11_OPOFF, RV_J_IMM_11_MASK) << RV_J_IMM_11_OFF) | \
273 	(RV_X(x_, RV_J_IMM_19_12_OPOFF, RV_J_IMM_19_12_MASK) << RV_J_IMM_19_12_OFF) | \
274 	(RV_IMM_SIGN(x_) << RV_J_IMM_SIGN_OFF); })
275 
276 #define RV_EXTRACT_ITYPE_IMM(x) \
277 	({typeof(x) x_ = (x); \
278 	(RV_X(x_, RV_I_IMM_11_0_OPOFF, RV_I_IMM_11_0_MASK)) | \
279 	(RV_IMM_SIGN(x_) << RV_I_IMM_SIGN_OFF); })
280 
281 #define RV_EXTRACT_BTYPE_IMM(x) \
282 	({typeof(x) x_ = (x); \
283 	(RV_X(x_, RV_B_IMM_4_1_OPOFF, RV_B_IMM_4_1_MASK) << RV_B_IMM_4_1_OFF) | \
284 	(RV_X(x_, RV_B_IMM_10_5_OPOFF, RV_B_IMM_10_5_MASK) << RV_B_IMM_10_5_OFF) | \
285 	(RV_X(x_, RV_B_IMM_11_OPOFF, RV_B_IMM_11_MASK) << RV_B_IMM_11_OFF) | \
286 	(RV_IMM_SIGN(x_) << RV_B_IMM_SIGN_OFF); })
287 
288 #define RVC_EXTRACT_JTYPE_IMM(x) \
289 	({typeof(x) x_ = (x); \
290 	(RVC_X(x_, RVC_J_IMM_3_1_OPOFF, RVC_J_IMM_3_1_MASK) << RVC_J_IMM_3_1_OFF) | \
291 	(RVC_X(x_, RVC_J_IMM_4_OPOFF, RVC_J_IMM_4_MASK) << RVC_J_IMM_4_OFF) | \
292 	(RVC_X(x_, RVC_J_IMM_5_OPOFF, RVC_J_IMM_5_MASK) << RVC_J_IMM_5_OFF) | \
293 	(RVC_X(x_, RVC_J_IMM_6_OPOFF, RVC_J_IMM_6_MASK) << RVC_J_IMM_6_OFF) | \
294 	(RVC_X(x_, RVC_J_IMM_7_OPOFF, RVC_J_IMM_7_MASK) << RVC_J_IMM_7_OFF) | \
295 	(RVC_X(x_, RVC_J_IMM_9_8_OPOFF, RVC_J_IMM_9_8_MASK) << RVC_J_IMM_9_8_OFF) | \
296 	(RVC_X(x_, RVC_J_IMM_10_OPOFF, RVC_J_IMM_10_MASK) << RVC_J_IMM_10_OFF) | \
297 	(RVC_IMM_SIGN(x_) << RVC_J_IMM_SIGN_OFF); })
298 
299 #define RVC_EXTRACT_BTYPE_IMM(x) \
300 	({typeof(x) x_ = (x); \
301 	(RVC_X(x_, RVC_B_IMM_2_1_OPOFF, RVC_B_IMM_2_1_MASK) << RVC_B_IMM_2_1_OFF) | \
302 	(RVC_X(x_, RVC_B_IMM_4_3_OPOFF, RVC_B_IMM_4_3_MASK) << RVC_B_IMM_4_3_OFF) | \
303 	(RVC_X(x_, RVC_B_IMM_5_OPOFF, RVC_B_IMM_5_MASK) << RVC_B_IMM_5_OFF) | \
304 	(RVC_X(x_, RVC_B_IMM_7_6_OPOFF, RVC_B_IMM_7_6_MASK) << RVC_B_IMM_7_6_OFF) | \
305 	(RVC_IMM_SIGN(x_) << RVC_B_IMM_SIGN_OFF); })
306 
307 /*
308  * Get the immediate from a J-type instruction.
309  *
310  * @insn: instruction to process
311  * Return: immediate
312  */
313 static inline s32 riscv_insn_extract_jtype_imm(u32 insn)
314 {
315 	return RV_EXTRACT_JTYPE_IMM(insn);
316 }
317 
318 /*
319  * Update a J-type instruction with an immediate value.
320  *
321  * @insn: pointer to the jtype instruction
322  * @imm: the immediate to insert into the instruction
323  */
324 static inline void riscv_insn_insert_jtype_imm(u32 *insn, s32 imm)
325 {
326 	/* drop the old IMMs, all jal IMM bits sit at 31:12 */
327 	*insn &= ~GENMASK(31, 12);
328 	*insn |= (RV_X(imm, RV_J_IMM_10_1_OFF, RV_J_IMM_10_1_MASK) << RV_J_IMM_10_1_OPOFF) |
329 		 (RV_X(imm, RV_J_IMM_11_OFF, RV_J_IMM_11_MASK) << RV_J_IMM_11_OPOFF) |
330 		 (RV_X(imm, RV_J_IMM_19_12_OFF, RV_J_IMM_19_12_MASK) << RV_J_IMM_19_12_OPOFF) |
331 		 (RV_X(imm, RV_J_IMM_SIGN_OFF, 1) << RV_J_IMM_SIGN_OPOFF);
332 }
333 
334 /*
335  * Put together one immediate from a U-type and I-type instruction pair.
336  *
337  * The U-type contains an upper immediate, meaning bits[31:12] with [11:0]
338  * being zero, while the I-type contains a 12bit immediate.
339  * Combined these can encode larger 32bit values and are used for example
340  * in auipc + jalr pairs to allow larger jumps.
341  *
342  * @utype_insn: instruction containing the upper immediate
343  * @itype_insn: instruction
344  * Return: combined immediate
345  */
346 static inline s32 riscv_insn_extract_utype_itype_imm(u32 utype_insn, u32 itype_insn)
347 {
348 	s32 imm;
349 
350 	imm = RV_EXTRACT_UTYPE_IMM(utype_insn);
351 	imm += RV_EXTRACT_ITYPE_IMM(itype_insn);
352 
353 	return imm;
354 }
355 
356 /*
357  * Update a set of two instructions (U-type + I-type) with an immediate value.
358  *
359  * Used for example in auipc+jalrs pairs the U-type instructions contains
360  * a 20bit upper immediate representing bits[31:12], while the I-type
361  * instruction contains a 12bit immediate representing bits[11:0].
362  *
363  * This also takes into account that both separate immediates are
364  * considered as signed values, so if the I-type immediate becomes
365  * negative (BIT(11) set) the U-type part gets adjusted.
366  *
367  * @utype_insn: pointer to the utype instruction of the pair
368  * @itype_insn: pointer to the itype instruction of the pair
369  * @imm: the immediate to insert into the two instructions
370  */
371 static inline void riscv_insn_insert_utype_itype_imm(u32 *utype_insn, u32 *itype_insn, s32 imm)
372 {
373 	/* drop possible old IMM values */
374 	*utype_insn &= ~(RV_U_IMM_31_12_MASK);
375 	*itype_insn &= ~(RV_I_IMM_11_0_MASK << RV_I_IMM_11_0_OPOFF);
376 
377 	/* add the adapted IMMs */
378 	*utype_insn |= (imm & RV_U_IMM_31_12_MASK) + ((imm & BIT(11)) << 1);
379 	*itype_insn |= ((imm & RV_I_IMM_11_0_MASK) << RV_I_IMM_11_0_OPOFF);
380 }
381 #endif /* _ASM_RISCV_INSN_H */
382