1 /*
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
4  * for more details.
5  *
6  * Copyright (C) 2001 - 2008 Tensilica Inc.
7  * Copyright (C) 2015 Cadence Design Systems Inc.
8  */
9 
10 #ifndef _XTENSA_PROCESSOR_H
11 #define _XTENSA_PROCESSOR_H
12 
13 #include <variant/core.h>
14 
15 #include <linux/compiler.h>
16 #include <asm/ptrace.h>
17 #include <asm/types.h>
18 #include <asm/regs.h>
19 
20 /* Assertions. */
21 
22 #if (XCHAL_HAVE_WINDOWED != 1)
23 # error Linux requires the Xtensa Windowed Registers Option.
24 #endif
25 
26 #define ARCH_SLAB_MINALIGN	XCHAL_DATA_WIDTH
27 
28 /*
29  * User space process size: 1 GB.
30  * Windowed call ABI requires caller and callee to be located within the same
31  * 1 GB region. The C compiler places trampoline code on the stack for sources
32  * that take the address of a nested C function (a feature used by glibc), so
33  * the 1 GB requirement applies to the stack as well.
34  */
35 
36 #ifdef CONFIG_MMU
37 #define TASK_SIZE	__XTENSA_UL_CONST(0x40000000)
38 #else
39 #define TASK_SIZE	__XTENSA_UL_CONST(0xffffffff)
40 #endif
41 
42 #define STACK_TOP	TASK_SIZE
43 #define STACK_TOP_MAX	STACK_TOP
44 
45 /*
46  * General exception cause assigned to fake NMI. Fake NMI needs to be handled
47  * differently from other interrupts, but it uses common kernel entry/exit
48  * code.
49  */
50 
51 #define EXCCAUSE_MAPPED_NMI	62
52 
53 /*
54  * General exception cause assigned to debug exceptions. Debug exceptions go
55  * to their own vector, rather than the general exception vectors (user,
56  * kernel, double); and their specific causes are reported via DEBUGCAUSE
57  * rather than EXCCAUSE.  However it is sometimes convenient to redirect debug
58  * exceptions to the general exception mechanism.  To do this, an otherwise
59  * unused EXCCAUSE value was assigned to debug exceptions for this purpose.
60  */
61 
62 #define EXCCAUSE_MAPPED_DEBUG	63
63 
64 /*
65  * We use DEPC also as a flag to distinguish between double and regular
66  * exceptions. For performance reasons, DEPC might contain the value of
67  * EXCCAUSE for regular exceptions, so we use this definition to mark a
68  * valid double exception address.
69  * (Note: We use it in bgeui, so it should be 64, 128, or 256)
70  */
71 
72 #define VALID_DOUBLE_EXCEPTION_ADDRESS	64
73 
74 #define XTENSA_INT_LEVEL(intno) _XTENSA_INT_LEVEL(intno)
75 #define _XTENSA_INT_LEVEL(intno) XCHAL_INT##intno##_LEVEL
76 
77 #define XTENSA_INTLEVEL_MASK(level) _XTENSA_INTLEVEL_MASK(level)
78 #define _XTENSA_INTLEVEL_MASK(level) (XCHAL_INTLEVEL##level##_MASK)
79 
80 #define XTENSA_INTLEVEL_ANDBELOW_MASK(l) _XTENSA_INTLEVEL_ANDBELOW_MASK(l)
81 #define _XTENSA_INTLEVEL_ANDBELOW_MASK(l) (XCHAL_INTLEVEL##l##_ANDBELOW_MASK)
82 
83 #define PROFILING_INTLEVEL XTENSA_INT_LEVEL(XCHAL_PROFILING_INTERRUPT)
84 
85 /* LOCKLEVEL defines the interrupt level that masks all
86  * general-purpose interrupts.
87  */
88 #if defined(CONFIG_XTENSA_FAKE_NMI) && defined(XCHAL_PROFILING_INTERRUPT)
89 #define LOCKLEVEL (PROFILING_INTLEVEL - 1)
90 #else
91 #define LOCKLEVEL XCHAL_EXCM_LEVEL
92 #endif
93 
94 #define TOPLEVEL XCHAL_EXCM_LEVEL
95 #define XTENSA_FAKE_NMI (LOCKLEVEL < TOPLEVEL)
96 
97 /* WSBITS and WBBITS are the width of the WINDOWSTART and WINDOWBASE
98  * registers
99  */
100 #define WSBITS  (XCHAL_NUM_AREGS / 4)      /* width of WINDOWSTART in bits */
101 #define WBBITS  (XCHAL_NUM_AREGS_LOG2 - 2) /* width of WINDOWBASE in bits */
102 
103 #ifndef __ASSEMBLY__
104 
105 /* Build a valid return address for the specified call winsize.
106  * winsize must be 1 (call4), 2 (call8), or 3 (call12)
107  */
108 #define MAKE_RA_FOR_CALL(ra,ws)   (((ra) & 0x3fffffff) | (ws) << 30)
109 
110 /* Convert return address to a valid pc
111  * Note: We assume that the stack pointer is in the same 1GB ranges as the ra
112  */
113 #define MAKE_PC_FROM_RA(ra,sp)    (((ra) & 0x3fffffff) | ((sp) & 0xc0000000))
114 
115 /* Spill slot location for the register reg in the spill area under the stack
116  * pointer sp. reg must be in the range [0..4).
117  */
118 #define SPILL_SLOT(sp, reg) (*(((unsigned long *)(sp)) - 4 + (reg)))
119 
120 /* Spill slot location for the register reg in the spill area under the stack
121  * pointer sp for the call8. reg must be in the range [4..8).
122  */
123 #define SPILL_SLOT_CALL8(sp, reg) (*(((unsigned long *)(sp)) - 12 + (reg)))
124 
125 /* Spill slot location for the register reg in the spill area under the stack
126  * pointer sp for the call12. reg must be in the range [4..12).
127  */
128 #define SPILL_SLOT_CALL12(sp, reg) (*(((unsigned long *)(sp)) - 16 + (reg)))
129 
130 typedef struct {
131 	unsigned long seg;
132 } mm_segment_t;
133 
134 struct thread_struct {
135 
136 	/* kernel's return address and stack pointer for context switching */
137 	unsigned long ra; /* kernel's a0: return address and window call size */
138 	unsigned long sp; /* kernel's a1: stack pointer */
139 
140 	mm_segment_t current_ds;    /* see uaccess.h for example uses */
141 
142 	/* struct xtensa_cpuinfo info; */
143 
144 	unsigned long bad_vaddr; /* last user fault */
145 	unsigned long bad_uaddr; /* last kernel fault accessing user space */
146 	unsigned long error_code;
147 #ifdef CONFIG_HAVE_HW_BREAKPOINT
148 	struct perf_event *ptrace_bp[XCHAL_NUM_IBREAK];
149 	struct perf_event *ptrace_wp[XCHAL_NUM_DBREAK];
150 #endif
151 	/* Make structure 16 bytes aligned. */
152 	int align[0] __attribute__ ((aligned(16)));
153 };
154 
155 /* This decides where the kernel will search for a free chunk of vm
156  * space during mmap's.
157  */
158 #define TASK_UNMAPPED_BASE	(TASK_SIZE / 2)
159 
160 #define INIT_THREAD  \
161 {									\
162 	ra:		0, 						\
163 	sp:		sizeof(init_stack) + (long) &init_stack,	\
164 	current_ds:	{0},						\
165 	/*info:		{0}, */						\
166 	bad_vaddr:	0,						\
167 	bad_uaddr:	0,						\
168 	error_code:	0,						\
169 }
170 
171 
172 /*
173  * Do necessary setup to start up a newly executed thread.
174  * Note: We set-up ps as if we did a call4 to the new pc.
175  *       set_thread_state in signal.c depends on it.
176  */
177 #define USER_PS_VALUE ((1 << PS_WOE_BIT) |				\
178 		       (1 << PS_CALLINC_SHIFT) |			\
179 		       (USER_RING << PS_RING_SHIFT) |			\
180 		       (1 << PS_UM_BIT) |				\
181 		       (1 << PS_EXCM_BIT))
182 
183 /* Clearing a0 terminates the backtrace. */
184 #define start_thread(regs, new_pc, new_sp) \
185 	memset(regs, 0, sizeof(*regs)); \
186 	regs->pc = new_pc; \
187 	regs->ps = USER_PS_VALUE; \
188 	regs->areg[1] = new_sp; \
189 	regs->areg[0] = 0; \
190 	regs->wmask = 1; \
191 	regs->depc = 0; \
192 	regs->windowbase = 0; \
193 	regs->windowstart = 1;
194 
195 /* Forward declaration */
196 struct task_struct;
197 struct mm_struct;
198 
199 /* Free all resources held by a thread. */
200 #define release_thread(thread) do { } while(0)
201 
202 extern unsigned long get_wchan(struct task_struct *p);
203 
204 #define KSTK_EIP(tsk)		(task_pt_regs(tsk)->pc)
205 #define KSTK_ESP(tsk)		(task_pt_regs(tsk)->areg[1])
206 
207 #define cpu_relax()  barrier()
208 
209 /* Special register access. */
210 
211 #define WSR(v,sr) __asm__ __volatile__ ("wsr %0,"__stringify(sr) :: "a"(v));
212 #define RSR(v,sr) __asm__ __volatile__ ("rsr %0,"__stringify(sr) : "=a"(v));
213 
214 #define set_sr(x,sr) ({unsigned int v=(unsigned int)x; WSR(v,sr);})
215 #define get_sr(sr) ({unsigned int v; RSR(v,sr); v; })
216 
217 #ifndef XCHAL_HAVE_EXTERN_REGS
218 #define XCHAL_HAVE_EXTERN_REGS 0
219 #endif
220 
221 #if XCHAL_HAVE_EXTERN_REGS
222 
223 static inline void set_er(unsigned long value, unsigned long addr)
224 {
225 	asm volatile ("wer %0, %1" : : "a" (value), "a" (addr) : "memory");
226 }
227 
228 static inline unsigned long get_er(unsigned long addr)
229 {
230 	register unsigned long value;
231 	asm volatile ("rer %0, %1" : "=a" (value) : "a" (addr) : "memory");
232 	return value;
233 }
234 
235 #endif /* XCHAL_HAVE_EXTERN_REGS */
236 
237 #endif	/* __ASSEMBLY__ */
238 #endif	/* _XTENSA_PROCESSOR_H */
239