18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __SPARC_SWITCH_TO_H
38c2ecf20Sopenharmony_ci#define __SPARC_SWITCH_TO_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <asm/smp.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciextern struct thread_info *current_set[NR_CPUS];
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * Flush windows so that the VM switch which follows
118c2ecf20Sopenharmony_ci * would not pull the stack from under us.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * SWITCH_ENTER and SWITCH_DO_LAZY_FPU do not work yet (e.g. SMP does not work)
148c2ecf20Sopenharmony_ci * XXX WTF is the above comment? Found in late teen 2.4.x.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
178c2ecf20Sopenharmony_ci#define SWITCH_ENTER(prv) \
188c2ecf20Sopenharmony_ci	do {			\
198c2ecf20Sopenharmony_ci	if (test_tsk_thread_flag(prv, TIF_USEDFPU)) { \
208c2ecf20Sopenharmony_ci		put_psr(get_psr() | PSR_EF); \
218c2ecf20Sopenharmony_ci		fpsave(&(prv)->thread.float_regs[0], &(prv)->thread.fsr, \
228c2ecf20Sopenharmony_ci		       &(prv)->thread.fpqueue[0], &(prv)->thread.fpqdepth); \
238c2ecf20Sopenharmony_ci		clear_tsk_thread_flag(prv, TIF_USEDFPU); \
248c2ecf20Sopenharmony_ci		(prv)->thread.kregs->psr &= ~PSR_EF; \
258c2ecf20Sopenharmony_ci	} \
268c2ecf20Sopenharmony_ci	} while(0)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define SWITCH_DO_LAZY_FPU(next)	/* */
298c2ecf20Sopenharmony_ci#else
308c2ecf20Sopenharmony_ci#define SWITCH_ENTER(prv)		/* */
318c2ecf20Sopenharmony_ci#define SWITCH_DO_LAZY_FPU(nxt)	\
328c2ecf20Sopenharmony_ci	do {			\
338c2ecf20Sopenharmony_ci	if (last_task_used_math != (nxt))		\
348c2ecf20Sopenharmony_ci		(nxt)->thread.kregs->psr&=~PSR_EF;	\
358c2ecf20Sopenharmony_ci	} while(0)
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define prepare_arch_switch(next) do { \
398c2ecf20Sopenharmony_ci	__asm__ __volatile__( \
408c2ecf20Sopenharmony_ci	".globl\tflush_patch_switch\nflush_patch_switch:\n\t" \
418c2ecf20Sopenharmony_ci	"save %sp, -0x40, %sp; save %sp, -0x40, %sp; save %sp, -0x40, %sp\n\t" \
428c2ecf20Sopenharmony_ci	"save %sp, -0x40, %sp; save %sp, -0x40, %sp; save %sp, -0x40, %sp\n\t" \
438c2ecf20Sopenharmony_ci	"save %sp, -0x40, %sp\n\t" \
448c2ecf20Sopenharmony_ci	"restore; restore; restore; restore; restore; restore; restore"); \
458c2ecf20Sopenharmony_ci} while(0)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	/* Much care has gone into this code, do not touch it.
488c2ecf20Sopenharmony_ci	 *
498c2ecf20Sopenharmony_ci	 * We need to loadup regs l0/l1 for the newly forked child
508c2ecf20Sopenharmony_ci	 * case because the trap return path relies on those registers
518c2ecf20Sopenharmony_ci	 * holding certain values, gcc is told that they are clobbered.
528c2ecf20Sopenharmony_ci	 * Gcc needs registers for 3 values in and 1 value out, so we
538c2ecf20Sopenharmony_ci	 * clobber every non-fixed-usage register besides l2/l3/o4/o5.  -DaveM
548c2ecf20Sopenharmony_ci	 *
558c2ecf20Sopenharmony_ci	 * Hey Dave, that do not touch sign is too much of an incentive
568c2ecf20Sopenharmony_ci	 * - Anton & Pete
578c2ecf20Sopenharmony_ci	 */
588c2ecf20Sopenharmony_ci#define switch_to(prev, next, last) do {						\
598c2ecf20Sopenharmony_ci	SWITCH_ENTER(prev);								\
608c2ecf20Sopenharmony_ci	SWITCH_DO_LAZY_FPU(next);							\
618c2ecf20Sopenharmony_ci	cpumask_set_cpu(smp_processor_id(), mm_cpumask(next->active_mm));		\
628c2ecf20Sopenharmony_ci	__asm__ __volatile__(								\
638c2ecf20Sopenharmony_ci	"sethi	%%hi(here - 0x8), %%o7\n\t"						\
648c2ecf20Sopenharmony_ci	"mov	%%g6, %%g3\n\t"								\
658c2ecf20Sopenharmony_ci	"or	%%o7, %%lo(here - 0x8), %%o7\n\t"					\
668c2ecf20Sopenharmony_ci	"rd	%%psr, %%g4\n\t"							\
678c2ecf20Sopenharmony_ci	"std	%%sp, [%%g6 + %4]\n\t"							\
688c2ecf20Sopenharmony_ci	"rd	%%wim, %%g5\n\t"							\
698c2ecf20Sopenharmony_ci	"wr	%%g4, 0x20, %%psr\n\t"							\
708c2ecf20Sopenharmony_ci	"nop\n\t"									\
718c2ecf20Sopenharmony_ci	"std	%%g4, [%%g6 + %3]\n\t"							\
728c2ecf20Sopenharmony_ci	"ldd	[%2 + %3], %%g4\n\t"							\
738c2ecf20Sopenharmony_ci	"mov	%2, %%g6\n\t"								\
748c2ecf20Sopenharmony_ci	".globl	patchme_store_new_current\n"						\
758c2ecf20Sopenharmony_ci"patchme_store_new_current:\n\t"							\
768c2ecf20Sopenharmony_ci	"st	%2, [%1]\n\t"								\
778c2ecf20Sopenharmony_ci	"wr	%%g4, 0x20, %%psr\n\t"							\
788c2ecf20Sopenharmony_ci	"nop\n\t"									\
798c2ecf20Sopenharmony_ci	"nop\n\t"									\
808c2ecf20Sopenharmony_ci	"nop\n\t"	/* LEON needs all 3 nops: load to %sp depends on CWP. */		\
818c2ecf20Sopenharmony_ci	"ldd	[%%g6 + %4], %%sp\n\t"							\
828c2ecf20Sopenharmony_ci	"wr	%%g5, 0x0, %%wim\n\t"							\
838c2ecf20Sopenharmony_ci	"ldd	[%%sp + 0x00], %%l0\n\t"						\
848c2ecf20Sopenharmony_ci	"ldd	[%%sp + 0x38], %%i6\n\t"						\
858c2ecf20Sopenharmony_ci	"wr	%%g4, 0x0, %%psr\n\t"							\
868c2ecf20Sopenharmony_ci	"nop\n\t"									\
878c2ecf20Sopenharmony_ci	"nop\n\t"									\
888c2ecf20Sopenharmony_ci	"jmpl	%%o7 + 0x8, %%g0\n\t"							\
898c2ecf20Sopenharmony_ci	" ld	[%%g3 + %5], %0\n\t"							\
908c2ecf20Sopenharmony_ci	"here:\n"									\
918c2ecf20Sopenharmony_ci        : "=&r" (last)									\
928c2ecf20Sopenharmony_ci        : "r" (&(current_set[hard_smp_processor_id()])),	\
938c2ecf20Sopenharmony_ci	  "r" (task_thread_info(next)),				\
948c2ecf20Sopenharmony_ci	  "i" (TI_KPSR),					\
958c2ecf20Sopenharmony_ci	  "i" (TI_KSP),						\
968c2ecf20Sopenharmony_ci	  "i" (TI_TASK)						\
978c2ecf20Sopenharmony_ci	:       "g1", "g2", "g3", "g4", "g5",       "g7",	\
988c2ecf20Sopenharmony_ci	  "l0", "l1",       "l3", "l4", "l5", "l6", "l7",	\
998c2ecf20Sopenharmony_ci	  "i0", "i1", "i2", "i3", "i4", "i5",			\
1008c2ecf20Sopenharmony_ci	  "o0", "o1", "o2", "o3",                   "o7");	\
1018c2ecf20Sopenharmony_ci	} while(0)
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_civoid fpsave(unsigned long *fpregs, unsigned long *fsr,
1048c2ecf20Sopenharmony_ci	    void *fpqueue, unsigned long *fpqdepth);
1058c2ecf20Sopenharmony_civoid synchronize_user_stack(void);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci#endif /* __SPARC_SWITCH_TO_H */
108