18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
38c2ecf20Sopenharmony_ci * Copyright (C) 2004 Microtronix Datacom Ltd.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
68c2ecf20Sopenharmony_ci * License. See the file "COPYING" in the main directory of this archive
78c2ecf20Sopenharmony_ci * for more details.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef _ASM_NIOS2_ENTRY_H
118c2ecf20Sopenharmony_ci#define _ASM_NIOS2_ENTRY_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#ifdef __ASSEMBLY__
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <asm/processor.h>
168c2ecf20Sopenharmony_ci#include <asm/registers.h>
178c2ecf20Sopenharmony_ci#include <asm/asm-offsets.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Standard Nios2 interrupt entry and exit macros.
218c2ecf20Sopenharmony_ci * Must be called with interrupts disabled.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci.macro SAVE_ALL
248c2ecf20Sopenharmony_ci	rdctl	r24, estatus
258c2ecf20Sopenharmony_ci	andi	r24, r24, ESTATUS_EU
268c2ecf20Sopenharmony_ci	beq	r24, r0, 1f /* In supervisor mode, already on kernel stack */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	movia	r24, _current_thread	/* Switch to current kernel stack */
298c2ecf20Sopenharmony_ci	ldw	r24, 0(r24)		/* using the thread_info */
308c2ecf20Sopenharmony_ci	addi	r24, r24, THREAD_SIZE-PT_REGS_SIZE
318c2ecf20Sopenharmony_ci	stw	sp, PT_SP(r24)		/* Save user stack before changing */
328c2ecf20Sopenharmony_ci	mov	sp, r24
338c2ecf20Sopenharmony_ci	br	2f
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci1 :	mov	r24, sp
368c2ecf20Sopenharmony_ci	addi	sp, sp, -PT_REGS_SIZE	/* Backup the kernel stack pointer */
378c2ecf20Sopenharmony_ci	stw	r24, PT_SP(sp)
388c2ecf20Sopenharmony_ci2 :	stw	r1, PT_R1(sp)
398c2ecf20Sopenharmony_ci	stw	r2, PT_R2(sp)
408c2ecf20Sopenharmony_ci	stw	r3, PT_R3(sp)
418c2ecf20Sopenharmony_ci	stw	r4, PT_R4(sp)
428c2ecf20Sopenharmony_ci	stw	r5, PT_R5(sp)
438c2ecf20Sopenharmony_ci	stw	r6, PT_R6(sp)
448c2ecf20Sopenharmony_ci	stw	r7, PT_R7(sp)
458c2ecf20Sopenharmony_ci	stw	r8, PT_R8(sp)
468c2ecf20Sopenharmony_ci	stw	r9, PT_R9(sp)
478c2ecf20Sopenharmony_ci	stw	r10, PT_R10(sp)
488c2ecf20Sopenharmony_ci	stw	r11, PT_R11(sp)
498c2ecf20Sopenharmony_ci	stw	r12, PT_R12(sp)
508c2ecf20Sopenharmony_ci	stw	r13, PT_R13(sp)
518c2ecf20Sopenharmony_ci	stw	r14, PT_R14(sp)
528c2ecf20Sopenharmony_ci	stw	r15, PT_R15(sp)
538c2ecf20Sopenharmony_ci	movi	r24, -1
548c2ecf20Sopenharmony_ci	stw	r24, PT_ORIG_R2(sp)
558c2ecf20Sopenharmony_ci	stw	r7, PT_ORIG_R7(sp)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	stw	ra, PT_RA(sp)
588c2ecf20Sopenharmony_ci	stw	fp, PT_FP(sp)
598c2ecf20Sopenharmony_ci	stw	gp, PT_GP(sp)
608c2ecf20Sopenharmony_ci	rdctl	r24, estatus
618c2ecf20Sopenharmony_ci	stw	r24, PT_ESTATUS(sp)
628c2ecf20Sopenharmony_ci	stw	ea, PT_EA(sp)
638c2ecf20Sopenharmony_ci.endm
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci.macro RESTORE_ALL
668c2ecf20Sopenharmony_ci	ldw	r1, PT_R1(sp)		/* Restore registers */
678c2ecf20Sopenharmony_ci	ldw	r2, PT_R2(sp)
688c2ecf20Sopenharmony_ci	ldw	r3, PT_R3(sp)
698c2ecf20Sopenharmony_ci	ldw	r4, PT_R4(sp)
708c2ecf20Sopenharmony_ci	ldw	r5, PT_R5(sp)
718c2ecf20Sopenharmony_ci	ldw	r6, PT_R6(sp)
728c2ecf20Sopenharmony_ci	ldw	r7, PT_R7(sp)
738c2ecf20Sopenharmony_ci	ldw	r8, PT_R8(sp)
748c2ecf20Sopenharmony_ci	ldw	r9, PT_R9(sp)
758c2ecf20Sopenharmony_ci	ldw	r10, PT_R10(sp)
768c2ecf20Sopenharmony_ci	ldw	r11, PT_R11(sp)
778c2ecf20Sopenharmony_ci	ldw	r12, PT_R12(sp)
788c2ecf20Sopenharmony_ci	ldw	r13, PT_R13(sp)
798c2ecf20Sopenharmony_ci	ldw	r14, PT_R14(sp)
808c2ecf20Sopenharmony_ci	ldw	r15, PT_R15(sp)
818c2ecf20Sopenharmony_ci	ldw	ra, PT_RA(sp)
828c2ecf20Sopenharmony_ci	ldw	fp, PT_FP(sp)
838c2ecf20Sopenharmony_ci	ldw	gp, PT_GP(sp)
848c2ecf20Sopenharmony_ci	ldw	r24, PT_ESTATUS(sp)
858c2ecf20Sopenharmony_ci	wrctl	estatus, r24
868c2ecf20Sopenharmony_ci	ldw	ea, PT_EA(sp)
878c2ecf20Sopenharmony_ci	ldw	sp, PT_SP(sp)		/* Restore sp last */
888c2ecf20Sopenharmony_ci.endm
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci.macro	SAVE_SWITCH_STACK
918c2ecf20Sopenharmony_ci	addi	sp, sp, -SWITCH_STACK_SIZE
928c2ecf20Sopenharmony_ci	stw	r16, SW_R16(sp)
938c2ecf20Sopenharmony_ci	stw	r17, SW_R17(sp)
948c2ecf20Sopenharmony_ci	stw	r18, SW_R18(sp)
958c2ecf20Sopenharmony_ci	stw	r19, SW_R19(sp)
968c2ecf20Sopenharmony_ci	stw	r20, SW_R20(sp)
978c2ecf20Sopenharmony_ci	stw	r21, SW_R21(sp)
988c2ecf20Sopenharmony_ci	stw	r22, SW_R22(sp)
998c2ecf20Sopenharmony_ci	stw	r23, SW_R23(sp)
1008c2ecf20Sopenharmony_ci	stw	fp, SW_FP(sp)
1018c2ecf20Sopenharmony_ci	stw	gp, SW_GP(sp)
1028c2ecf20Sopenharmony_ci	stw	ra, SW_RA(sp)
1038c2ecf20Sopenharmony_ci.endm
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci.macro	RESTORE_SWITCH_STACK
1068c2ecf20Sopenharmony_ci	ldw	r16, SW_R16(sp)
1078c2ecf20Sopenharmony_ci	ldw	r17, SW_R17(sp)
1088c2ecf20Sopenharmony_ci	ldw	r18, SW_R18(sp)
1098c2ecf20Sopenharmony_ci	ldw	r19, SW_R19(sp)
1108c2ecf20Sopenharmony_ci	ldw	r20, SW_R20(sp)
1118c2ecf20Sopenharmony_ci	ldw	r21, SW_R21(sp)
1128c2ecf20Sopenharmony_ci	ldw	r22, SW_R22(sp)
1138c2ecf20Sopenharmony_ci	ldw	r23, SW_R23(sp)
1148c2ecf20Sopenharmony_ci	ldw	fp, SW_FP(sp)
1158c2ecf20Sopenharmony_ci	ldw	gp, SW_GP(sp)
1168c2ecf20Sopenharmony_ci	ldw	ra, SW_RA(sp)
1178c2ecf20Sopenharmony_ci	addi	sp, sp, SWITCH_STACK_SIZE
1188c2ecf20Sopenharmony_ci.endm
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
1218c2ecf20Sopenharmony_ci#endif /* _ASM_NIOS2_ENTRY_H */
122