2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1994, 95, 96, 99, 2001 Ralf Baechle
7 * Copyright (C) 1994, 1995, 1996 Paul M. Antoine.
8 * Copyright (C) 1999 Silicon Graphics, Inc.
9 * Copyright (C) 2007 Maciej W. Rozycki
11 #ifndef _ASM_STACKFRAME_H
12 #define _ASM_STACKFRAME_H
14 #include <linux/threads.h>
17 #include <asm/asmmacro.h>
18 #include <asm/mipsregs.h>
19 #include <asm/asm-offsets.h>
22 * For SMTC kernel, global IE should be left set, and interrupts
23 * controlled exclusively via IXMT.
25 #ifdef CONFIG_MIPS_MT_SMTC
27 #elif defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
33 #ifdef CONFIG_MIPS_MT_SMTC
34 #include <asm/mipsmtregs.h>
35 #endif /* CONFIG_MIPS_MT_SMTC */
45 #ifdef CONFIG_CPU_HAS_SMARTMIPS
59 LONG_S $10, PT_R10(sp)
60 LONG_S $11, PT_R11(sp)
61 LONG_S $12, PT_R12(sp)
62 #ifndef CONFIG_CPU_HAS_SMARTMIPS
66 LONG_S $13, PT_R13(sp)
67 LONG_S $14, PT_R14(sp)
68 LONG_S $15, PT_R15(sp)
69 LONG_S $24, PT_R24(sp)
70 #ifndef CONFIG_CPU_HAS_SMARTMIPS
76 LONG_S $16, PT_R16(sp)
77 LONG_S $17, PT_R17(sp)
78 LONG_S $18, PT_R18(sp)
79 LONG_S $19, PT_R19(sp)
80 LONG_S $20, PT_R20(sp)
81 LONG_S $21, PT_R21(sp)
82 LONG_S $22, PT_R22(sp)
83 LONG_S $23, PT_R23(sp)
84 LONG_S $30, PT_R30(sp)
88 #ifdef CONFIG_MIPS_MT_SMTC
89 #define PTEBASE_SHIFT 19 /* TCBIND */
90 #define CPU_ID_REG CP0_TCBIND
91 #define CPU_ID_MFC0 mfc0
92 #elif defined(CONFIG_MIPS_PGD_C0_CONTEXT)
93 #define PTEBASE_SHIFT 48 /* XCONTEXT */
94 #define CPU_ID_REG CP0_XCONTEXT
95 #define CPU_ID_MFC0 MFC0
97 #define PTEBASE_SHIFT 23 /* CONTEXT */
98 #define CPU_ID_REG CP0_CONTEXT
99 #define CPU_ID_MFC0 MFC0
101 .macro get_saved_sp /* SMP variation */
102 CPU_ID_MFC0 k0, CPU_ID_REG
103 #if defined(CONFIG_32BIT) || defined(KBUILD_64BIT_SYM32)
104 lui k1, %hi(kernelsp)
106 lui k1, %highest(kernelsp)
107 daddiu k1, %higher(kernelsp)
109 daddiu k1, %hi(kernelsp)
112 LONG_SRL k0, PTEBASE_SHIFT
114 LONG_L k1, %lo(kernelsp)(k1)
117 .macro set_saved_sp stackp temp temp2
118 CPU_ID_MFC0 \temp, CPU_ID_REG
119 LONG_SRL \temp, PTEBASE_SHIFT
120 LONG_S \stackp, kernelsp(\temp)
123 .macro get_saved_sp /* Uniprocessor variation */
124 #ifdef CONFIG_CPU_JUMP_WORKAROUNDS
126 * Clear BTB (branch target buffer), forbid RAS (return address
127 * stack) to workaround the Out-of-order Issue in Loongson2F
128 * via its diagnostic register.
142 #endif /* CONFIG_CPU_JUMP_WORKAROUNDS */
143 #if defined(CONFIG_32BIT) || defined(KBUILD_64BIT_SYM32)
144 lui k1, %hi(kernelsp)
146 lui k1, %highest(kernelsp)
147 daddiu k1, %higher(kernelsp)
149 daddiu k1, %hi(kernelsp)
152 LONG_L k1, %lo(kernelsp)(k1)
155 .macro set_saved_sp stackp temp temp2
156 LONG_S \stackp, kernelsp
165 sll k0, 3 /* extract cu0 bit */
170 /* Called from user mode, new stack. */
172 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
174 PTR_SUBU sp, k1, PT_SIZE
177 8: PTR_SUBU k1, PT_SIZE
182 LONG_S k0, PT_R29(sp)
185 * You might think that you don't need to save $0,
186 * but the FPU emulator and gdb remote debug stub
187 * need it to operate correctly
192 LONG_S v1, PT_STATUS(sp)
193 #ifdef CONFIG_MIPS_MT_SMTC
195 * Ideally, these instructions would be shuffled in
196 * to cover the pipeline delay.
199 mfc0 k0, CP0_TCSTATUS
201 LONG_S k0, PT_TCSTATUS(sp)
202 #endif /* CONFIG_MIPS_MT_SMTC */
206 LONG_S v1, PT_CAUSE(sp)
214 LONG_S v1, PT_EPC(sp)
215 LONG_S $25, PT_R25(sp)
216 LONG_S $28, PT_R28(sp)
217 LONG_S $31, PT_R31(sp)
218 ori $28, sp, _THREAD_MASK
219 xori $28, _THREAD_MASK
220 #ifdef CONFIG_CPU_CAVIUM_OCTEON
222 pref 0, 0($28) /* Prefetch the current pointer */
223 pref 0, PT_R31(sp) /* Prefetch the $31(ra) */
224 /* The Octeon multiplier state is affected by general multiply
225 instructions. It must be saved before and kernel code might
228 LONG_L v1, 0($28) /* Load the current pointer */
229 /* Restore $31(ra) that was changed by the jal */
230 LONG_L ra, PT_R31(sp)
231 pref 0, 0(v1) /* Prefetch the current thread */
251 #ifdef CONFIG_CPU_HAS_SMARTMIPS
252 LONG_L $24, PT_ACX(sp)
254 LONG_L $24, PT_HI(sp)
256 LONG_L $24, PT_LO(sp)
259 LONG_L $24, PT_LO(sp)
261 LONG_L $24, PT_HI(sp)
268 LONG_L $10, PT_R10(sp)
269 LONG_L $11, PT_R11(sp)
270 LONG_L $12, PT_R12(sp)
271 LONG_L $13, PT_R13(sp)
272 LONG_L $14, PT_R14(sp)
273 LONG_L $15, PT_R15(sp)
274 LONG_L $24, PT_R24(sp)
277 .macro RESTORE_STATIC
278 LONG_L $16, PT_R16(sp)
279 LONG_L $17, PT_R17(sp)
280 LONG_L $18, PT_R18(sp)
281 LONG_L $19, PT_R19(sp)
282 LONG_L $20, PT_R20(sp)
283 LONG_L $21, PT_R21(sp)
284 LONG_L $22, PT_R22(sp)
285 LONG_L $23, PT_R23(sp)
286 LONG_L $30, PT_R30(sp)
289 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
301 LONG_L v0, PT_STATUS(sp)
306 LONG_L $31, PT_R31(sp)
307 LONG_L $28, PT_R28(sp)
308 LONG_L $25, PT_R25(sp)
318 .macro RESTORE_SP_AND_RET
321 LONG_L k0, PT_EPC(sp)
322 LONG_L sp, PT_R29(sp)
333 #ifdef CONFIG_MIPS_MT_SMTC
336 * We need to make sure the read-modify-write
337 * of Status below isn't perturbed by an interrupt
338 * or cross-TC access, so we need to do at least a DMT,
339 * protected by an interrupt-inhibit. But setting IXMT
340 * also creates a few-cycle window where an IPI could
341 * be queued and not be detected before potentially
342 * returning to a WAIT or user-mode loop. It must be
345 * We're in the middle of a context switch, and
346 * we can't dispatch it directly without trashing
347 * some registers, so we'll try to detect this unlikely
348 * case and program a software interrupt in the VPE,
349 * as would be done for a cross-VPE IPI. To accommodate
350 * the handling of that case, we're doing a DVPE instead
351 * of just a DMT here to protect against other threads.
352 * This is a lot of cruft to cover a tiny window.
353 * If you can find a better design, implement it!
356 mfc0 v0, CP0_TCSTATUS
357 ori v0, TCSTATUS_IXMT
358 mtc0 v0, CP0_TCSTATUS
362 #endif /* CONFIG_MIPS_MT_SMTC */
363 #ifdef CONFIG_CPU_CAVIUM_OCTEON
364 /* Restore the Octeon multiplier state */
365 jal octeon_mult_restore
373 LONG_L v0, PT_STATUS(sp)
378 #ifdef CONFIG_MIPS_MT_SMTC
380 * Only after EXL/ERL have been restored to status can we
381 * restore TCStatus.IXMT.
383 LONG_L v1, PT_TCSTATUS(sp)
385 mfc0 a0, CP0_TCSTATUS
386 andi v1, TCSTATUS_IXMT
390 * We'd like to detect any IPIs queued in the tiny window
391 * above and request an software interrupt to service them
394 * Computing the offset into the IPIQ array of the executing
395 * TC's IPI queue in-line would be tedious. We use part of
396 * the TCContext register to hold 16 bits of offset that we
397 * can add in-line to find the queue head.
399 mfc0 v0, CP0_TCCONTEXT
406 * If we have a queue, provoke dispatch within the VPE by setting C_SW1
413 * This test should really never branch but
414 * let's be prudent here. Having atomized
415 * the shared register modifications, we can
416 * now EVPE, and must do so before interrupts
417 * are potentially re-enabled.
419 andi a1, a1, MVPCONTROL_EVP
423 /* We know that TCStatua.IXMT should be set from above */
424 xori a0, a0, TCSTATUS_IXMT
426 mtc0 a0, CP0_TCSTATUS
430 #endif /* CONFIG_MIPS_MT_SMTC */
431 LONG_L v1, PT_EPC(sp)
433 LONG_L $31, PT_R31(sp)
434 LONG_L $28, PT_R28(sp)
435 LONG_L $25, PT_R25(sp)
449 .macro RESTORE_SP_AND_RET
450 LONG_L sp, PT_R29(sp)
459 LONG_L sp, PT_R29(sp)
470 .macro RESTORE_ALL_AND_RET
479 * Move to kernel mode and disable interrupts.
480 * Set cp0 enable bit as sign that we're running on the kernel stack
483 #if !defined(CONFIG_MIPS_MT_SMTC)
485 li t1, ST0_CU0 | STATMASK
489 #else /* CONFIG_MIPS_MT_SMTC */
491 * For SMTC, we need to set privilege
492 * and disable interrupts only for the
493 * current TC, using the TCStatus register.
495 mfc0 t0, CP0_TCSTATUS
496 /* Fortunately CU 0 is in the same place in both registers */
497 /* Set TCU0, TMX, TKSU (for later inversion) and IXMT */
498 li t1, ST0_CU0 | 0x08001c00
500 /* Clear TKSU, leave IXMT */
502 mtc0 t0, CP0_TCSTATUS
504 /* We need to leave the global IE bit set, but clear EXL...*/
506 ori t0, ST0_EXL | ST0_ERL
507 xori t0, ST0_EXL | ST0_ERL
509 #endif /* CONFIG_MIPS_MT_SMTC */
514 * Move to kernel mode and enable interrupts.
515 * Set cp0 enable bit as sign that we're running on the kernel stack
518 #if !defined(CONFIG_MIPS_MT_SMTC)
520 li t1, ST0_CU0 | STATMASK
522 xori t0, STATMASK & ~1
524 #else /* CONFIG_MIPS_MT_SMTC */
526 * For SMTC, we need to set privilege
527 * and enable interrupts only for the
528 * current TC, using the TCStatus register.
531 mfc0 t0, CP0_TCSTATUS
532 /* Fortunately CU 0 is in the same place in both registers */
533 /* Set TCU0, TKSU (for later inversion) and IXMT */
534 li t1, ST0_CU0 | 0x08001c00
536 /* Clear TKSU *and* IXMT */
538 mtc0 t0, CP0_TCSTATUS
540 /* We need to leave the global IE bit set, but clear EXL...*/
545 /* irq_enable_hazard below should expand to EHB for 24K/34K cpus */
546 #endif /* CONFIG_MIPS_MT_SMTC */
551 * Just move to kernel mode and leave interrupts as they are. Note
552 * for the R3000 this means copying the previous enable from IEp.
553 * Set cp0 enable bit as sign that we're running on the kernel stack
556 #ifdef CONFIG_MIPS_MT_SMTC
558 * This gets baroque in SMTC. We want to
559 * protect the non-atomic clearing of EXL
560 * with DMT/EMT, but we don't want to take
561 * an interrupt while DMT is still in effect.
564 /* KMODE gets invoked from both reorder and noreorder code */
568 mfc0 v0, CP0_TCSTATUS
569 andi v1, v0, TCSTATUS_IXMT
570 ori v0, TCSTATUS_IXMT
571 mtc0 v0, CP0_TCSTATUS
575 * We don't know a priori if ra is "live"
581 #endif /* CONFIG_MIPS_MT_SMTC */
583 li t1, ST0_CU0 | (STATMASK & ~1)
584 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
590 xori t0, STATMASK & ~1
592 #ifdef CONFIG_MIPS_MT_SMTC
594 andi v0, v0, VPECONTROL_TE
599 mfc0 v0, CP0_TCSTATUS
600 /* Clear IXMT, then OR in previous value */
601 ori v0, TCSTATUS_IXMT
602 xori v0, TCSTATUS_IXMT
604 mtc0 v0, CP0_TCSTATUS
606 * irq_disable_hazard below should expand to EHB
610 #endif /* CONFIG_MIPS_MT_SMTC */
614 #endif /* _ASM_STACKFRAME_H */