1 /*
2  * include/asm/processor.h
3  *
4  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5  */
6 
7 #ifndef __ASM_SPARC64_PROCESSOR_H
8 #define __ASM_SPARC64_PROCESSOR_H
9 
10 /*
11  * Sparc64 implementation of macro that returns current
12  * instruction pointer ("program counter").
13  */
14 #define current_text_addr() ({ void *pc; __asm__("rd %%pc, %0" : "=r" (pc)); pc; })
15 
16 #include <asm/asi.h>
17 #include <asm/pstate.h>
18 #include <asm/ptrace.h>
19 #include <asm/page.h>
20 
21 /* Don't hold the runqueue lock over context switch */
22 #define __ARCH_WANT_UNLOCKED_CTXSW
23 
24 /* The sparc has no problems with write protection */
25 #define wp_works_ok 1
26 #define wp_works_ok__is_a_macro /* for versions in ksyms.c */
27 
28 /*
29  * User lives in his very own context, and cannot reference us. Note
30  * that TASK_SIZE is a misnomer, it really gives maximum user virtual
31  * address that the kernel will allocate out.
32  *
33  * XXX No longer using virtual page tables, kill this upper limit...
34  */
35 #define VA_BITS		44
36 #ifndef __ASSEMBLY__
37 #define VPTE_SIZE	(1UL << (VA_BITS - PAGE_SHIFT + 3))
38 #else
39 #define VPTE_SIZE	(1 << (VA_BITS - PAGE_SHIFT + 3))
40 #endif
41 
42 #define TASK_SIZE_OF(tsk) \
43 	(test_tsk_thread_flag(tsk,TIF_32BIT) ? \
44 	 (1UL << 32UL) : ((unsigned long)-VPTE_SIZE))
45 #define TASK_SIZE	TASK_SIZE_OF(current)
46 #ifdef __KERNEL__
47 
48 #define STACK_TOP32	((1UL << 32UL) - PAGE_SIZE)
49 #define STACK_TOP64	(0x0000080000000000UL - (1UL << 32UL))
50 
51 #define STACK_TOP	(test_thread_flag(TIF_32BIT) ? \
52 			 STACK_TOP32 : STACK_TOP64)
53 
54 #define STACK_TOP_MAX	STACK_TOP64
55 
56 #endif
57 
58 #ifndef __ASSEMBLY__
59 
60 typedef struct {
61 	unsigned char seg;
62 } mm_segment_t;
63 
64 /* The Sparc processor specific thread struct. */
65 /* XXX This should die, everything can go into thread_info now. */
66 struct thread_struct {
67 #ifdef CONFIG_DEBUG_SPINLOCK
68 	/* How many spinlocks held by this thread.
69 	 * Used with spin lock debugging to catch tasks
70 	 * sleeping illegally with locks held.
71 	 */
72 	int smp_lock_count;
73 	unsigned int smp_lock_pc;
74 #else
75 	int dummy; /* f'in gcc bug... */
76 #endif
77 };
78 
79 #endif /* !(__ASSEMBLY__) */
80 
81 #ifndef CONFIG_DEBUG_SPINLOCK
82 #define INIT_THREAD  {			\
83 	0,				\
84 }
85 #else /* CONFIG_DEBUG_SPINLOCK */
86 #define INIT_THREAD  {					\
87 /* smp_lock_count, smp_lock_pc, */			\
88    0,		   0,					\
89 }
90 #endif /* !(CONFIG_DEBUG_SPINLOCK) */
91 
92 #ifndef __ASSEMBLY__
93 
94 #include <linux/types.h>
95 
96 /* Return saved PC of a blocked thread. */
97 struct task_struct;
98 extern unsigned long thread_saved_pc(struct task_struct *);
99 
100 /* On Uniprocessor, even in RMO processes see TSO semantics */
101 #ifdef CONFIG_SMP
102 #define TSTATE_INITIAL_MM	TSTATE_TSO
103 #else
104 #define TSTATE_INITIAL_MM	TSTATE_RMO
105 #endif
106 
107 /* Do necessary setup to start up a newly executed thread. */
108 #define start_thread(regs, pc, sp) \
109 do { \
110 	unsigned long __asi = ASI_PNF; \
111 	regs->tstate = (regs->tstate & (TSTATE_CWP)) | (TSTATE_INITIAL_MM|TSTATE_IE) | (__asi << 24UL); \
112 	regs->tpc = ((pc & (~3)) - 4); \
113 	regs->tnpc = regs->tpc + 4; \
114 	regs->y = 0; \
115 	set_thread_wstate(1 << 3); \
116 	if (current_thread_info()->utraps) { \
117 		if (*(current_thread_info()->utraps) < 2) \
118 			kfree(current_thread_info()->utraps); \
119 		else \
120 			(*(current_thread_info()->utraps))--; \
121 		current_thread_info()->utraps = NULL; \
122 	} \
123 	__asm__ __volatile__( \
124 	"stx		%%g0, [%0 + %2 + 0x00]\n\t" \
125 	"stx		%%g0, [%0 + %2 + 0x08]\n\t" \
126 	"stx		%%g0, [%0 + %2 + 0x10]\n\t" \
127 	"stx		%%g0, [%0 + %2 + 0x18]\n\t" \
128 	"stx		%%g0, [%0 + %2 + 0x20]\n\t" \
129 	"stx		%%g0, [%0 + %2 + 0x28]\n\t" \
130 	"stx		%%g0, [%0 + %2 + 0x30]\n\t" \
131 	"stx		%%g0, [%0 + %2 + 0x38]\n\t" \
132 	"stx		%%g0, [%0 + %2 + 0x40]\n\t" \
133 	"stx		%%g0, [%0 + %2 + 0x48]\n\t" \
134 	"stx		%%g0, [%0 + %2 + 0x50]\n\t" \
135 	"stx		%%g0, [%0 + %2 + 0x58]\n\t" \
136 	"stx		%%g0, [%0 + %2 + 0x60]\n\t" \
137 	"stx		%%g0, [%0 + %2 + 0x68]\n\t" \
138 	"stx		%1,   [%0 + %2 + 0x70]\n\t" \
139 	"stx		%%g0, [%0 + %2 + 0x78]\n\t" \
140 	"wrpr		%%g0, (1 << 3), %%wstate\n\t" \
141 	: \
142 	: "r" (regs), "r" (sp - sizeof(struct reg_window) - STACK_BIAS), \
143 	  "i" ((const unsigned long)(&((struct pt_regs *)0)->u_regs[0]))); \
144 } while (0)
145 
146 #define start_thread32(regs, pc, sp) \
147 do { \
148 	unsigned long __asi = ASI_PNF; \
149 	pc &= 0x00000000ffffffffUL; \
150 	sp &= 0x00000000ffffffffUL; \
151 	regs->tstate = (regs->tstate & (TSTATE_CWP))|(TSTATE_INITIAL_MM|TSTATE_IE|TSTATE_AM) | (__asi << 24UL); \
152 	regs->tpc = ((pc & (~3)) - 4); \
153 	regs->tnpc = regs->tpc + 4; \
154 	regs->y = 0; \
155 	set_thread_wstate(2 << 3); \
156 	if (current_thread_info()->utraps) { \
157 		if (*(current_thread_info()->utraps) < 2) \
158 			kfree(current_thread_info()->utraps); \
159 		else \
160 			(*(current_thread_info()->utraps))--; \
161 		current_thread_info()->utraps = NULL; \
162 	} \
163 	__asm__ __volatile__( \
164 	"stx		%%g0, [%0 + %2 + 0x00]\n\t" \
165 	"stx		%%g0, [%0 + %2 + 0x08]\n\t" \
166 	"stx		%%g0, [%0 + %2 + 0x10]\n\t" \
167 	"stx		%%g0, [%0 + %2 + 0x18]\n\t" \
168 	"stx		%%g0, [%0 + %2 + 0x20]\n\t" \
169 	"stx		%%g0, [%0 + %2 + 0x28]\n\t" \
170 	"stx		%%g0, [%0 + %2 + 0x30]\n\t" \
171 	"stx		%%g0, [%0 + %2 + 0x38]\n\t" \
172 	"stx		%%g0, [%0 + %2 + 0x40]\n\t" \
173 	"stx		%%g0, [%0 + %2 + 0x48]\n\t" \
174 	"stx		%%g0, [%0 + %2 + 0x50]\n\t" \
175 	"stx		%%g0, [%0 + %2 + 0x58]\n\t" \
176 	"stx		%%g0, [%0 + %2 + 0x60]\n\t" \
177 	"stx		%%g0, [%0 + %2 + 0x68]\n\t" \
178 	"stx		%1,   [%0 + %2 + 0x70]\n\t" \
179 	"stx		%%g0, [%0 + %2 + 0x78]\n\t" \
180 	"wrpr		%%g0, (2 << 3), %%wstate\n\t" \
181 	: \
182 	: "r" (regs), "r" (sp - sizeof(struct reg_window32)), \
183 	  "i" ((const unsigned long)(&((struct pt_regs *)0)->u_regs[0]))); \
184 } while (0)
185 
186 /* Free all resources held by a thread. */
187 #define release_thread(tsk)		do { } while (0)
188 
189 /* Prepare to copy thread state - unlazy all lazy status */
190 #define prepare_to_copy(tsk)	do { } while (0)
191 
192 extern pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags);
193 
194 extern unsigned long get_wchan(struct task_struct *task);
195 
196 #define task_pt_regs(tsk) (task_thread_info(tsk)->kregs)
197 #define KSTK_EIP(tsk)  (task_pt_regs(tsk)->tpc)
198 #define KSTK_ESP(tsk)  (task_pt_regs(tsk)->u_regs[UREG_FP])
199 
200 #define cpu_relax()	barrier()
201 
202 /* Prefetch support.  This is tuned for UltraSPARC-III and later.
203  * UltraSPARC-I will treat these as nops, and UltraSPARC-II has
204  * a shallower prefetch queue than later chips.
205  */
206 #define ARCH_HAS_PREFETCH
207 #define ARCH_HAS_PREFETCHW
208 #define ARCH_HAS_SPINLOCK_PREFETCH
209 
210 static inline void prefetch(const void *x)
211 {
212 	/* We do not use the read prefetch mnemonic because that
213 	 * prefetches into the prefetch-cache which only is accessible
214 	 * by floating point operations in UltraSPARC-III and later.
215 	 * By contrast, "#one_write" prefetches into the L2 cache
216 	 * in shared state.
217 	 */
218 	__asm__ __volatile__("prefetch [%0], #one_write"
219 			     : /* no outputs */
220 			     : "r" (x));
221 }
222 
223 static inline void prefetchw(const void *x)
224 {
225 	/* The most optimal prefetch to use for writes is
226 	 * "#n_writes".  This brings the cacheline into the
227 	 * L2 cache in "owned" state.
228 	 */
229 	__asm__ __volatile__("prefetch [%0], #n_writes"
230 			     : /* no outputs */
231 			     : "r" (x));
232 }
233 
234 #define spin_lock_prefetch(x)	prefetchw(x)
235 
236 #define HAVE_ARCH_PICK_MMAP_LAYOUT
237 
238 #endif /* !(__ASSEMBLY__) */
239 
240 #endif /* !(__ASM_SPARC64_PROCESSOR_H) */
241