xref: /linux/arch/arc/include/asm/entry-compact.h (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
1 /*
2  * Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com)
3  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Vineetg: March 2009 (Supporting 2 levels of Interrupts)
10  *  Stack switching code can no longer reliably rely on the fact that
11  *  if we are NOT in user mode, stack is switched to kernel mode.
12  *  e.g. L2 IRQ interrupted a L1 ISR which had not yet completed
13  *  it's prologue including stack switching from user mode
14  *
15  * Vineetg: Aug 28th 2008: Bug #94984
16  *  -Zero Overhead Loop Context shd be cleared when entering IRQ/EXcp/Trap
17  *   Normally CPU does this automatically, however when doing FAKE rtie,
18  *   we also need to explicitly do this. The problem in macros
19  *   FAKE_RET_FROM_EXCPN and FAKE_RET_FROM_EXCPN_LOCK_IRQ was that this bit
20  *   was being "CLEARED" rather then "SET". Actually "SET" clears ZOL context
21  *
22  * Vineetg: May 5th 2008
23  *  -Modified CALLEE_REG save/restore macros to handle the fact that
24  *      r25 contains the kernel current task ptr
25  *  - Defined Stack Switching Macro to be reused in all intr/excp hdlrs
26  *  - Shaved off 11 instructions from RESTORE_ALL_INT1 by using the
27  *      address Write back load ld.ab instead of seperate ld/add instn
28  *
29  * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
30  */
31 
32 #ifndef __ASM_ARC_ENTRY_COMPACT_H
33 #define __ASM_ARC_ENTRY_COMPACT_H
34 
35 #include <asm/asm-offsets.h>
36 #include <asm/irqflags-compact.h>
37 #include <asm/thread_info.h>	/* For THREAD_SIZE */
38 
39 /*--------------------------------------------------------------
40  * Switch to Kernel Mode stack if SP points to User Mode stack
41  *
42  * Entry   : r9 contains pre-IRQ/exception/trap status32
43  * Exit    : SP set to K mode stack
44  *           SP at the time of entry (K/U) saved @ pt_regs->sp
45  * Clobbers: r9
46  *-------------------------------------------------------------*/
47 
48 .macro SWITCH_TO_KERNEL_STK
49 
50 	/* User Mode when this happened ? Yes: Proceed to switch stack */
51 	bbit1   r9, STATUS_U_BIT, 88f
52 
53 	/* OK we were already in kernel mode when this event happened, thus can
54 	 * assume SP is kernel mode SP. _NO_ need to do any stack switching
55 	 */
56 
57 #ifdef CONFIG_ARC_COMPACT_IRQ_LEVELS
58 	/* However....
59 	 * If Level 2 Interrupts enabled, we may end up with a corner case:
60 	 * 1. User Task executing
61 	 * 2. L1 IRQ taken, ISR starts (CPU auto-switched to KERNEL mode)
62 	 * 3. But before it could switch SP from USER to KERNEL stack
63 	 *      a L2 IRQ "Interrupts" L1
64 	 * Thay way although L2 IRQ happened in Kernel mode, stack is still
65 	 * not switched.
66 	 * To handle this, we may need to switch stack even if in kernel mode
67 	 * provided SP has values in range of USER mode stack ( < 0x7000_0000 )
68 	 */
69 	brlo sp, VMALLOC_START, 88f
70 
71 	/* TODO: vineetg:
72 	 * We need to be a bit more cautious here. What if a kernel bug in
73 	 * L1 ISR, caused SP to go whaco (some small value which looks like
74 	 * USER stk) and then we take L2 ISR.
75 	 * Above brlo alone would treat it as a valid L1-L2 sceanrio
76 	 * instead of shouting alound
77 	 * The only feasible way is to make sure this L2 happened in
78 	 * L1 prelogue ONLY i.e. ilink2 is less than a pre-set marker in
79 	 * L1 ISR before it switches stack
80 	 */
81 
82 #endif
83 
84     /*------Intr/Ecxp happened in kernel mode, SP already setup ------ */
85 	/* save it nevertheless @ pt_regs->sp for uniformity */
86 
87 	b.d	66f
88 	st	sp, [sp, PT_sp - SZ_PT_REGS]
89 
90 88: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */
91 
92 	GET_CURR_TASK_ON_CPU   r9
93 
94 	/* With current tsk in r9, get it's kernel mode stack base */
95 	GET_TSK_STACK_BASE  r9, r9
96 
97 	/* save U mode SP @ pt_regs->sp */
98 	st	sp, [r9, PT_sp - SZ_PT_REGS]
99 
100 	/* final SP switch */
101 	mov	sp, r9
102 66:
103 .endm
104 
105 /*------------------------------------------------------------
106  * "FAKE" a rtie to return from CPU Exception context
107  * This is to re-enable Exceptions within exception
108  * Look at EV_ProtV to see how this is actually used
109  *-------------------------------------------------------------*/
110 
111 .macro FAKE_RET_FROM_EXCPN
112 
113 	ld  r9, [sp, PT_status32]
114 	bic r9, r9, (STATUS_U_MASK|STATUS_DE_MASK)
115 	bset  r9, r9, STATUS_L_BIT
116 	sr  r9, [erstatus]
117 	mov r9, 55f
118 	sr  r9, [eret]
119 
120 	rtie
121 55:
122 .endm
123 
124 /*--------------------------------------------------------------
125  * For early Exception/ISR Prologue, a core reg is temporarily needed to
126  * code the rest of prolog (stack switching). This is done by stashing
127  * it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP).
128  *
129  * Before saving the full regfile - this reg is restored back, only
130  * to be saved again on kernel mode stack, as part of pt_regs.
131  *-------------------------------------------------------------*/
132 .macro PROLOG_FREEUP_REG	reg, mem
133 #ifdef CONFIG_SMP
134 	sr  \reg, [ARC_REG_SCRATCH_DATA0]
135 #else
136 	st  \reg, [\mem]
137 #endif
138 .endm
139 
140 .macro PROLOG_RESTORE_REG	reg, mem
141 #ifdef CONFIG_SMP
142 	lr  \reg, [ARC_REG_SCRATCH_DATA0]
143 #else
144 	ld  \reg, [\mem]
145 #endif
146 .endm
147 
148 /*--------------------------------------------------------------
149  * Exception Entry prologue
150  * -Switches stack to K mode (if not already)
151  * -Saves the register file
152  *
153  * After this it is safe to call the "C" handlers
154  *-------------------------------------------------------------*/
155 .macro EXCEPTION_PROLOGUE
156 
157 	/* Need at least 1 reg to code the early exception prologue */
158 	PROLOG_FREEUP_REG r9, @ex_saved_reg1
159 
160 	/* U/K mode at time of exception (stack not switched if already K) */
161 	lr  r9, [erstatus]
162 
163 	/* ARC700 doesn't provide auto-stack switching */
164 	SWITCH_TO_KERNEL_STK
165 
166 #ifdef CONFIG_ARC_CURR_IN_REG
167 	/* Treat r25 as scratch reg (save on stack) and load with "current" */
168 	PUSH    r25
169 	GET_CURR_TASK_ON_CPU   r25
170 #else
171 	sub     sp, sp, 4
172 #endif
173 
174 	st.a	r0, [sp, -8]    /* orig_r0 needed for syscall (skip ECR slot) */
175 	sub	sp, sp, 4	/* skip pt_regs->sp, already saved above */
176 
177 	/* Restore r9 used to code the early prologue */
178 	PROLOG_RESTORE_REG  r9, @ex_saved_reg1
179 
180 	/* now we are ready to save the regfile */
181 	SAVE_R0_TO_R12
182 	PUSH	gp
183 	PUSH	fp
184 	PUSH	blink
185 	PUSHAX	eret
186 	PUSHAX	erstatus
187 	PUSH	lp_count
188 	PUSHAX	lp_end
189 	PUSHAX	lp_start
190 	PUSHAX	erbta
191 
192 	lr	r9, [ecr]
193 	st      r9, [sp, PT_event]    /* EV_Trap expects r9 to have ECR */
194 .endm
195 
196 /*--------------------------------------------------------------
197  * Restore all registers used by system call or Exceptions
198  * SP should always be pointing to the next free stack element
199  * when entering this macro.
200  *
201  * NOTE:
202  *
203  * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
204  * for memory load operations. If used in that way interrupts are deffered
205  * by hardware and that is not good.
206  *-------------------------------------------------------------*/
207 .macro EXCEPTION_EPILOGUE
208 	POPAX	erbta
209 	POPAX	lp_start
210 	POPAX	lp_end
211 
212 	POP	r9
213 	mov	lp_count, r9	;LD to lp_count is not allowed
214 
215 	POPAX	erstatus
216 	POPAX	eret
217 	POP	blink
218 	POP	fp
219 	POP	gp
220 	RESTORE_R12_TO_R0
221 
222 	ld  sp, [sp] /* restore original sp */
223 	/* orig_r0, ECR, user_r25 skipped automatically */
224 .endm
225 
226 /* Dummy ECR values for Interrupts */
227 #define event_IRQ1		0x0031abcd
228 #define event_IRQ2		0x0032abcd
229 
230 .macro INTERRUPT_PROLOGUE  LVL
231 
232 	/* free up r9 as scratchpad */
233 	PROLOG_FREEUP_REG r9, @int\LVL\()_saved_reg
234 
235 	/* Which mode (user/kernel) was the system in when intr occured */
236 	lr  r9, [status32_l\LVL\()]
237 
238 	SWITCH_TO_KERNEL_STK
239 
240 #ifdef CONFIG_ARC_CURR_IN_REG
241 	/* Treat r25 as scratch reg (save on stack) and load with "current" */
242 	PUSH    r25
243 	GET_CURR_TASK_ON_CPU   r25
244 #else
245 	sub     sp, sp, 4
246 #endif
247 
248 	PUSH	0x003\LVL\()abcd    /* Dummy ECR */
249 	sub	sp, sp, 8	    /* skip orig_r0 (not needed)
250 				       skip pt_regs->sp, already saved above */
251 
252 	/* Restore r9 used to code the early prologue */
253 	PROLOG_RESTORE_REG  r9, @int\LVL\()_saved_reg
254 
255 	SAVE_R0_TO_R12
256 	PUSH	gp
257 	PUSH	fp
258 	PUSH	blink
259 	PUSH	ilink\LVL\()
260 	PUSHAX	status32_l\LVL\()
261 	PUSH	lp_count
262 	PUSHAX	lp_end
263 	PUSHAX	lp_start
264 	PUSHAX	bta_l\LVL\()
265 .endm
266 
267 /*--------------------------------------------------------------
268  * Restore all registers used by interrupt handlers.
269  *
270  * NOTE:
271  *
272  * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
273  * for memory load operations. If used in that way interrupts are deffered
274  * by hardware and that is not good.
275  *-------------------------------------------------------------*/
276 .macro INTERRUPT_EPILOGUE  LVL
277 	POPAX	bta_l\LVL\()
278 	POPAX	lp_start
279 	POPAX	lp_end
280 
281 	POP	r9
282 	mov	lp_count, r9	;LD to lp_count is not allowed
283 
284 	POPAX	status32_l\LVL\()
285 	POP	ilink\LVL\()
286 	POP	blink
287 	POP	fp
288 	POP	gp
289 	RESTORE_R12_TO_R0
290 
291 	ld  sp, [sp] /* restore original sp */
292 	/* orig_r0, ECR, user_r25 skipped automatically */
293 .endm
294 
295 /* Get thread_info of "current" tsk */
296 .macro GET_CURR_THR_INFO_FROM_SP  reg
297 	bic \reg, sp, (THREAD_SIZE - 1)
298 .endm
299 
300 /* Get CPU-ID of this core */
301 .macro  GET_CPU_ID  reg
302 	lr  \reg, [identity]
303 	lsr \reg, \reg, 8
304 	bmsk \reg, \reg, 7
305 .endm
306 
307 #endif  /* __ASM_ARC_ENTRY_COMPACT_H */
308