1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 */
5#ifndef _ASM_ASMMACRO_H
6#define _ASM_ASMMACRO_H
7
8#include <asm/asm-offsets.h>
9#include <asm/regdef.h>
10#include <asm/fpregdef.h>
11#include <asm/loongarch.h>
12
13	.macro	parse_v var val
14	\var	= \val
15	.endm
16
17	.macro	parse_r var r
18	\var	= -1
19	.ifc	\r, $r0
20	\var	= 0
21	.endif
22	.ifc	\r, $r1
23	\var	= 1
24	.endif
25	.ifc	\r, $r2
26	\var	= 2
27	.endif
28	.ifc	\r, $r3
29	\var	= 3
30	.endif
31	.ifc	\r, $r4
32	\var	= 4
33	.endif
34	.ifc	\r, $r5
35	\var	= 5
36	.endif
37	.ifc	\r, $r6
38	\var	= 6
39	.endif
40	.ifc	\r, $r7
41	\var	= 7
42	.endif
43	.ifc	\r, $r8
44	\var	= 8
45	.endif
46	.ifc	\r, $r9
47	\var	= 9
48	.endif
49	.ifc	\r, $r10
50	\var	= 10
51	.endif
52	.ifc	\r, $r11
53	\var	= 11
54	.endif
55	.ifc	\r, $r12
56	\var	= 12
57	.endif
58	.ifc	\r, $r13
59	\var	= 13
60	.endif
61	.ifc	\r, $r14
62	\var	= 14
63	.endif
64	.ifc	\r, $r15
65	\var	= 15
66	.endif
67	.ifc	\r, $r16
68	\var	= 16
69	.endif
70	.ifc	\r, $r17
71	\var	= 17
72	.endif
73	.ifc	\r, $r18
74	\var	= 18
75	.endif
76	.ifc	\r, $r19
77	\var	= 19
78	.endif
79	.ifc	\r, $r20
80	\var	= 20
81	.endif
82	.ifc	\r, $r21
83	\var	= 21
84	.endif
85	.ifc	\r, $r22
86	\var	= 22
87	.endif
88	.ifc	\r, $r23
89	\var	= 23
90	.endif
91	.ifc	\r, $r24
92	\var	= 24
93	.endif
94	.ifc	\r, $r25
95	\var	= 25
96	.endif
97	.ifc	\r, $r26
98	\var	= 26
99	.endif
100	.ifc	\r, $r27
101	\var	= 27
102	.endif
103	.ifc	\r, $r28
104	\var	= 28
105	.endif
106	.ifc	\r, $r29
107	\var	= 29
108	.endif
109	.ifc	\r, $r30
110	\var	= 30
111	.endif
112	.ifc	\r, $r31
113	\var	= 31
114	.endif
115	.iflt	\var
116	.error	"Unable to parse register name \r"
117	.endif
118	.endm
119
120	.macro	cpu_save_nonscratch thread
121	stptr.d	s0, \thread, THREAD_REG23
122	stptr.d	s1, \thread, THREAD_REG24
123	stptr.d	s2, \thread, THREAD_REG25
124	stptr.d	s3, \thread, THREAD_REG26
125	stptr.d	s4, \thread, THREAD_REG27
126	stptr.d	s5, \thread, THREAD_REG28
127	stptr.d	s6, \thread, THREAD_REG29
128	stptr.d	s7, \thread, THREAD_REG30
129	stptr.d	s8, \thread, THREAD_REG31
130	stptr.d	sp, \thread, THREAD_REG03
131	stptr.d	fp, \thread, THREAD_REG22
132	.endm
133
134	.macro	cpu_restore_nonscratch thread
135	ldptr.d	s0, \thread, THREAD_REG23
136	ldptr.d	s1, \thread, THREAD_REG24
137	ldptr.d	s2, \thread, THREAD_REG25
138	ldptr.d	s3, \thread, THREAD_REG26
139	ldptr.d	s4, \thread, THREAD_REG27
140	ldptr.d	s5, \thread, THREAD_REG28
141	ldptr.d	s6, \thread, THREAD_REG29
142	ldptr.d	s7, \thread, THREAD_REG30
143	ldptr.d	s8, \thread, THREAD_REG31
144	ldptr.d	ra, \thread, THREAD_REG01
145	ldptr.d	sp, \thread, THREAD_REG03
146	ldptr.d	fp, \thread, THREAD_REG22
147	.endm
148
149	.macro fpu_save_csr thread tmp
150	movfcsr2gr	\tmp, fcsr0
151	stptr.w	\tmp, \thread, THREAD_FCSR
152	.endm
153
154	.macro fpu_restore_csr thread tmp
155	ldptr.w	\tmp, \thread, THREAD_FCSR
156	movgr2fcsr	fcsr0, \tmp
157	.endm
158
159	.macro fpu_save_cc thread tmp0 tmp1
160	movcf2gr	\tmp0, $fcc0
161	move	\tmp1, \tmp0
162	movcf2gr	\tmp0, $fcc1
163	bstrins.d	\tmp1, \tmp0, 15, 8
164	movcf2gr	\tmp0, $fcc2
165	bstrins.d	\tmp1, \tmp0, 23, 16
166	movcf2gr	\tmp0, $fcc3
167	bstrins.d	\tmp1, \tmp0, 31, 24
168	movcf2gr	\tmp0, $fcc4
169	bstrins.d	\tmp1, \tmp0, 39, 32
170	movcf2gr	\tmp0, $fcc5
171	bstrins.d	\tmp1, \tmp0, 47, 40
172	movcf2gr	\tmp0, $fcc6
173	bstrins.d	\tmp1, \tmp0, 55, 48
174	movcf2gr	\tmp0, $fcc7
175	bstrins.d	\tmp1, \tmp0, 63, 56
176	stptr.d		\tmp1, \thread, THREAD_FCC
177	.endm
178
179	.macro fpu_restore_cc thread tmp0 tmp1
180	ldptr.d	\tmp0, \thread, THREAD_FCC
181	bstrpick.d	\tmp1, \tmp0, 7, 0
182	movgr2cf	$fcc0, \tmp1
183	bstrpick.d	\tmp1, \tmp0, 15, 8
184	movgr2cf	$fcc1, \tmp1
185	bstrpick.d	\tmp1, \tmp0, 23, 16
186	movgr2cf	$fcc2, \tmp1
187	bstrpick.d	\tmp1, \tmp0, 31, 24
188	movgr2cf	$fcc3, \tmp1
189	bstrpick.d	\tmp1, \tmp0, 39, 32
190	movgr2cf	$fcc4, \tmp1
191	bstrpick.d	\tmp1, \tmp0, 47, 40
192	movgr2cf	$fcc5, \tmp1
193	bstrpick.d	\tmp1, \tmp0, 55, 48
194	movgr2cf	$fcc6, \tmp1
195	bstrpick.d	\tmp1, \tmp0, 63, 56
196	movgr2cf	$fcc7, \tmp1
197	.endm
198
199	.macro	fpu_save_double thread tmp
200	li.w	\tmp, THREAD_FPR0
201	PTR_ADD \tmp, \tmp, \thread
202	fst.d	$f0, \tmp, THREAD_FPR0  - THREAD_FPR0
203	fst.d	$f1, \tmp, THREAD_FPR1  - THREAD_FPR0
204	fst.d	$f2, \tmp, THREAD_FPR2  - THREAD_FPR0
205	fst.d	$f3, \tmp, THREAD_FPR3  - THREAD_FPR0
206	fst.d	$f4, \tmp, THREAD_FPR4  - THREAD_FPR0
207	fst.d	$f5, \tmp, THREAD_FPR5  - THREAD_FPR0
208	fst.d	$f6, \tmp, THREAD_FPR6  - THREAD_FPR0
209	fst.d	$f7, \tmp, THREAD_FPR7  - THREAD_FPR0
210	fst.d	$f8, \tmp, THREAD_FPR8  - THREAD_FPR0
211	fst.d	$f9, \tmp, THREAD_FPR9  - THREAD_FPR0
212	fst.d	$f10, \tmp, THREAD_FPR10 - THREAD_FPR0
213	fst.d	$f11, \tmp, THREAD_FPR11 - THREAD_FPR0
214	fst.d	$f12, \tmp, THREAD_FPR12 - THREAD_FPR0
215	fst.d	$f13, \tmp, THREAD_FPR13 - THREAD_FPR0
216	fst.d	$f14, \tmp, THREAD_FPR14 - THREAD_FPR0
217	fst.d	$f15, \tmp, THREAD_FPR15 - THREAD_FPR0
218	fst.d	$f16, \tmp, THREAD_FPR16 - THREAD_FPR0
219	fst.d	$f17, \tmp, THREAD_FPR17 - THREAD_FPR0
220	fst.d	$f18, \tmp, THREAD_FPR18 - THREAD_FPR0
221	fst.d	$f19, \tmp, THREAD_FPR19 - THREAD_FPR0
222	fst.d	$f20, \tmp, THREAD_FPR20 - THREAD_FPR0
223	fst.d	$f21, \tmp, THREAD_FPR21 - THREAD_FPR0
224	fst.d	$f22, \tmp, THREAD_FPR22 - THREAD_FPR0
225	fst.d	$f23, \tmp, THREAD_FPR23 - THREAD_FPR0
226	fst.d	$f24, \tmp, THREAD_FPR24 - THREAD_FPR0
227	fst.d	$f25, \tmp, THREAD_FPR25 - THREAD_FPR0
228	fst.d	$f26, \tmp, THREAD_FPR26 - THREAD_FPR0
229	fst.d	$f27, \tmp, THREAD_FPR27 - THREAD_FPR0
230	fst.d	$f28, \tmp, THREAD_FPR28 - THREAD_FPR0
231	fst.d	$f29, \tmp, THREAD_FPR29 - THREAD_FPR0
232	fst.d	$f30, \tmp, THREAD_FPR30 - THREAD_FPR0
233	fst.d	$f31, \tmp, THREAD_FPR31 - THREAD_FPR0
234	.endm
235
236	.macro	fpu_restore_double thread tmp
237	li.w	\tmp, THREAD_FPR0
238	PTR_ADD \tmp, \tmp, \thread
239	fld.d	$f0, \tmp, THREAD_FPR0  - THREAD_FPR0
240	fld.d	$f1, \tmp, THREAD_FPR1  - THREAD_FPR0
241	fld.d	$f2, \tmp, THREAD_FPR2  - THREAD_FPR0
242	fld.d	$f3, \tmp, THREAD_FPR3  - THREAD_FPR0
243	fld.d	$f4, \tmp, THREAD_FPR4  - THREAD_FPR0
244	fld.d	$f5, \tmp, THREAD_FPR5  - THREAD_FPR0
245	fld.d	$f6, \tmp, THREAD_FPR6  - THREAD_FPR0
246	fld.d	$f7, \tmp, THREAD_FPR7  - THREAD_FPR0
247	fld.d	$f8, \tmp, THREAD_FPR8  - THREAD_FPR0
248	fld.d	$f9, \tmp, THREAD_FPR9  - THREAD_FPR0
249	fld.d	$f10, \tmp, THREAD_FPR10 - THREAD_FPR0
250	fld.d	$f11, \tmp, THREAD_FPR11 - THREAD_FPR0
251	fld.d	$f12, \tmp, THREAD_FPR12 - THREAD_FPR0
252	fld.d	$f13, \tmp, THREAD_FPR13 - THREAD_FPR0
253	fld.d	$f14, \tmp, THREAD_FPR14 - THREAD_FPR0
254	fld.d	$f15, \tmp, THREAD_FPR15 - THREAD_FPR0
255	fld.d	$f16, \tmp, THREAD_FPR16 - THREAD_FPR0
256	fld.d	$f17, \tmp, THREAD_FPR17 - THREAD_FPR0
257	fld.d	$f18, \tmp, THREAD_FPR18 - THREAD_FPR0
258	fld.d	$f19, \tmp, THREAD_FPR19 - THREAD_FPR0
259	fld.d	$f20, \tmp, THREAD_FPR20 - THREAD_FPR0
260	fld.d	$f21, \tmp, THREAD_FPR21 - THREAD_FPR0
261	fld.d	$f22, \tmp, THREAD_FPR22 - THREAD_FPR0
262	fld.d	$f23, \tmp, THREAD_FPR23 - THREAD_FPR0
263	fld.d	$f24, \tmp, THREAD_FPR24 - THREAD_FPR0
264	fld.d	$f25, \tmp, THREAD_FPR25 - THREAD_FPR0
265	fld.d	$f26, \tmp, THREAD_FPR26 - THREAD_FPR0
266	fld.d	$f27, \tmp, THREAD_FPR27 - THREAD_FPR0
267	fld.d	$f28, \tmp, THREAD_FPR28 - THREAD_FPR0
268	fld.d	$f29, \tmp, THREAD_FPR29 - THREAD_FPR0
269	fld.d	$f30, \tmp, THREAD_FPR30 - THREAD_FPR0
270	fld.d	$f31, \tmp, THREAD_FPR31 - THREAD_FPR0
271	.endm
272
273.macro not dst src
274	nor	\dst, \src, zero
275.endm
276
277#endif /* _ASM_ASMMACRO_H */
278