xref: /linux/arch/riscv/include/asm/assembler.h (revision 8c994eff8fcfe8ecb1f1dbebed25b4d7bb75be12)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2023 StarFive Technology Co., Ltd.
4  *
5  * Author: Jee Heng Sia <jeeheng.sia@starfivetech.com>
6  */
7 
8 #ifndef __ASSEMBLY__
9 #error "Only include this from assembly code"
10 #endif
11 
12 #ifndef __ASM_ASSEMBLER_H
13 #define __ASM_ASSEMBLER_H
14 
15 #include <asm/asm.h>
16 #include <asm/asm-offsets.h>
17 #include <asm/csr.h>
18 
19 /*
20  * suspend_restore_csrs - restore CSRs
21  */
22 	.macro suspend_restore_csrs
23 		REG_L	t0, (SUSPEND_CONTEXT_REGS + PT_EPC)(a0)
24 		csrw	CSR_EPC, t0
25 		REG_L	t0, (SUSPEND_CONTEXT_REGS + PT_STATUS)(a0)
26 		csrw	CSR_STATUS, t0
27 		REG_L	t0, (SUSPEND_CONTEXT_REGS + PT_BADADDR)(a0)
28 		csrw	CSR_TVAL, t0
29 		REG_L	t0, (SUSPEND_CONTEXT_REGS + PT_CAUSE)(a0)
30 		csrw	CSR_CAUSE, t0
31 	.endm
32 
33 /*
34  * suspend_restore_regs - Restore registers (except A0 and T0-T6)
35  */
36 	.macro suspend_restore_regs
37 		REG_L	ra, (SUSPEND_CONTEXT_REGS + PT_RA)(a0)
38 		REG_L	sp, (SUSPEND_CONTEXT_REGS + PT_SP)(a0)
39 		REG_L	gp, (SUSPEND_CONTEXT_REGS + PT_GP)(a0)
40 		REG_L	tp, (SUSPEND_CONTEXT_REGS + PT_TP)(a0)
41 		REG_L	s0, (SUSPEND_CONTEXT_REGS + PT_S0)(a0)
42 		REG_L	s1, (SUSPEND_CONTEXT_REGS + PT_S1)(a0)
43 		REG_L	a1, (SUSPEND_CONTEXT_REGS + PT_A1)(a0)
44 		REG_L	a2, (SUSPEND_CONTEXT_REGS + PT_A2)(a0)
45 		REG_L	a3, (SUSPEND_CONTEXT_REGS + PT_A3)(a0)
46 		REG_L	a4, (SUSPEND_CONTEXT_REGS + PT_A4)(a0)
47 		REG_L	a5, (SUSPEND_CONTEXT_REGS + PT_A5)(a0)
48 		REG_L	a6, (SUSPEND_CONTEXT_REGS + PT_A6)(a0)
49 		REG_L	a7, (SUSPEND_CONTEXT_REGS + PT_A7)(a0)
50 		REG_L	s2, (SUSPEND_CONTEXT_REGS + PT_S2)(a0)
51 		REG_L	s3, (SUSPEND_CONTEXT_REGS + PT_S3)(a0)
52 		REG_L	s4, (SUSPEND_CONTEXT_REGS + PT_S4)(a0)
53 		REG_L	s5, (SUSPEND_CONTEXT_REGS + PT_S5)(a0)
54 		REG_L	s6, (SUSPEND_CONTEXT_REGS + PT_S6)(a0)
55 		REG_L	s7, (SUSPEND_CONTEXT_REGS + PT_S7)(a0)
56 		REG_L	s8, (SUSPEND_CONTEXT_REGS + PT_S8)(a0)
57 		REG_L	s9, (SUSPEND_CONTEXT_REGS + PT_S9)(a0)
58 		REG_L	s10, (SUSPEND_CONTEXT_REGS + PT_S10)(a0)
59 		REG_L	s11, (SUSPEND_CONTEXT_REGS + PT_S11)(a0)
60 	.endm
61 
62 /*
63  * copy_page - copy 1 page (4KB) of data from source to destination
64  * @a0 - destination
65  * @a1 - source
66  */
67 	.macro	copy_page a0, a1
68 		lui	a2, 0x1
69 		add	a2, a2, a0
70 1 :
71 		REG_L	t0, 0(a1)
72 		REG_L	t1, SZREG(a1)
73 
74 		REG_S	t0, 0(a0)
75 		REG_S	t1, SZREG(a0)
76 
77 		addi	a0, a0, 2 * SZREG
78 		addi	a1, a1, 2 * SZREG
79 		bne	a2, a0, 1b
80 	.endm
81 
82 #endif	/* __ASM_ASSEMBLER_H */
83