xref: /openbmc/linux/arch/xtensa/kernel/coprocessor.S (revision 068ac0db)
1/*
2 * arch/xtensa/kernel/coprocessor.S
3 *
4 * Xtensa processor configuration-specific table of coprocessor and
5 * other custom register layout information.
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License.  See the file "COPYING" in the main directory of this archive
9 * for more details.
10 *
11 * Copyright (C) 2003 - 2007 Tensilica Inc.
12 */
13
14
15#include <linux/linkage.h>
16#include <asm/asm-offsets.h>
17#include <asm/asmmacro.h>
18#include <asm/coprocessor.h>
19#include <asm/current.h>
20#include <asm/regs.h>
21
22#if XTENSA_HAVE_COPROCESSORS
23
24/*
25 * Macros for lazy context switch.
26 */
27
28#define SAVE_CP_REGS(x)							\
29	.if XTENSA_HAVE_COPROCESSOR(x);					\
30		.align 4;						\
31	.Lsave_cp_regs_cp##x:						\
32		xchal_cp##x##_store a2 a4 a5 a6 a7;			\
33		jx	a0;						\
34	.endif
35
36#define SAVE_CP_REGS_TAB(x)						\
37	.if XTENSA_HAVE_COPROCESSOR(x);					\
38		.long .Lsave_cp_regs_cp##x;				\
39	.else;								\
40		.long 0;						\
41	.endif;								\
42	.long THREAD_XTREGS_CP##x
43
44
45#define LOAD_CP_REGS(x)							\
46	.if XTENSA_HAVE_COPROCESSOR(x);					\
47		.align 4;						\
48	.Lload_cp_regs_cp##x:						\
49		xchal_cp##x##_load a2 a4 a5 a6 a7;			\
50		jx	a0;						\
51	.endif
52
53#define LOAD_CP_REGS_TAB(x)						\
54	.if XTENSA_HAVE_COPROCESSOR(x);					\
55		.long .Lload_cp_regs_cp##x;				\
56	.else;								\
57		.long 0;						\
58	.endif;								\
59	.long THREAD_XTREGS_CP##x
60
61	SAVE_CP_REGS(0)
62	SAVE_CP_REGS(1)
63	SAVE_CP_REGS(2)
64	SAVE_CP_REGS(3)
65	SAVE_CP_REGS(4)
66	SAVE_CP_REGS(5)
67	SAVE_CP_REGS(6)
68	SAVE_CP_REGS(7)
69
70	LOAD_CP_REGS(0)
71	LOAD_CP_REGS(1)
72	LOAD_CP_REGS(2)
73	LOAD_CP_REGS(3)
74	LOAD_CP_REGS(4)
75	LOAD_CP_REGS(5)
76	LOAD_CP_REGS(6)
77	LOAD_CP_REGS(7)
78
79	.section ".rodata", "a"
80	.align 4
81.Lsave_cp_regs_jump_table:
82	SAVE_CP_REGS_TAB(0)
83	SAVE_CP_REGS_TAB(1)
84	SAVE_CP_REGS_TAB(2)
85	SAVE_CP_REGS_TAB(3)
86	SAVE_CP_REGS_TAB(4)
87	SAVE_CP_REGS_TAB(5)
88	SAVE_CP_REGS_TAB(6)
89	SAVE_CP_REGS_TAB(7)
90
91.Lload_cp_regs_jump_table:
92	LOAD_CP_REGS_TAB(0)
93	LOAD_CP_REGS_TAB(1)
94	LOAD_CP_REGS_TAB(2)
95	LOAD_CP_REGS_TAB(3)
96	LOAD_CP_REGS_TAB(4)
97	LOAD_CP_REGS_TAB(5)
98	LOAD_CP_REGS_TAB(6)
99	LOAD_CP_REGS_TAB(7)
100
101	.previous
102
103/*
104 * coprocessor_flush(struct thread_info*, index)
105 *                             a2        a3
106 *
107 * Save coprocessor registers for coprocessor 'index'.
108 * The register values are saved to or loaded from the coprocessor area
109 * inside the task_info structure.
110 *
111 * Note that this function doesn't update the coprocessor_owner information!
112 *
113 */
114
115ENTRY(coprocessor_flush)
116
117	/* reserve 4 bytes on stack to save a0 */
118	abi_entry(4)
119
120	s32i	a0, a1, 0
121	movi	a0, .Lsave_cp_regs_jump_table
122	addx8	a3, a3, a0
123	l32i	a4, a3, 4
124	l32i	a3, a3, 0
125	add	a2, a2, a4
126	beqz	a3, 1f
127	callx0	a3
1281:	l32i	a0, a1, 0
129
130	abi_ret(4)
131
132ENDPROC(coprocessor_flush)
133
134/*
135 * Entry condition:
136 *
137 *   a0:	trashed, original value saved on stack (PT_AREG0)
138 *   a1:	a1
139 *   a2:	new stack pointer, original in DEPC
140 *   a3:	a3
141 *   depc:	a2, original value saved on stack (PT_DEPC)
142 *   excsave_1:	dispatch table
143 *
144 *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
145 *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
146 */
147
148ENTRY(fast_coprocessor_double)
149
150	wsr	a0, excsave1
151	call0	unrecoverable_exception
152
153ENDPROC(fast_coprocessor_double)
154
155ENTRY(fast_coprocessor)
156
157	/* Save remaining registers a1-a3 and SAR */
158
159	s32i	a3, a2, PT_AREG3
160	rsr	a3, sar
161	s32i	a1, a2, PT_AREG1
162	s32i	a3, a2, PT_SAR
163	mov	a1, a2
164	rsr	a2, depc
165	s32i	a2, a1, PT_AREG2
166
167	/*
168	 * The hal macros require up to 4 temporary registers. We use a3..a6.
169	 */
170
171	s32i	a4, a1, PT_AREG4
172	s32i	a5, a1, PT_AREG5
173	s32i	a6, a1, PT_AREG6
174
175	/* Find coprocessor number. Subtract first CP EXCCAUSE from EXCCAUSE */
176
177	rsr	a3, exccause
178	addi	a3, a3, -EXCCAUSE_COPROCESSOR0_DISABLED
179
180	/* Set corresponding CPENABLE bit -> (sar:cp-index, a3: 1<<cp-index)*/
181
182	ssl	a3			# SAR: 32 - coprocessor_number
183	movi	a2, 1
184	rsr	a0, cpenable
185	sll	a2, a2
186	or	a0, a0, a2
187	wsr	a0, cpenable
188	rsync
189
190	/* Retrieve previous owner. (a3 still holds CP number) */
191
192	movi	a0, coprocessor_owner	# list of owners
193	addx4	a0, a3, a0		# entry for CP
194	l32i	a4, a0, 0
195
196	beqz	a4, 1f			# skip 'save' if no previous owner
197
198	/* Disable coprocessor for previous owner. (a2 = 1 << CP number) */
199
200	l32i	a5, a4, THREAD_CPENABLE
201	xor	a5, a5, a2		# (1 << cp-id) still in a2
202	s32i	a5, a4, THREAD_CPENABLE
203
204	/*
205	 * Get context save area and 'call' save routine.
206	 * (a4 still holds previous owner (thread_info), a3 CP number)
207	 */
208
209	movi	a5, .Lsave_cp_regs_jump_table
210	movi	a0, 2f			# a0: 'return' address
211	addx8	a3, a3, a5		# a3: coprocessor number
212	l32i	a2, a3, 4		# a2: xtregs offset
213	l32i	a3, a3, 0		# a3: jump address
214	add	a2, a2, a4
215	jx	a3
216
217	/* Note that only a0 and a1 were preserved. */
218
2192:	rsr	a3, exccause
220	addi	a3, a3, -EXCCAUSE_COPROCESSOR0_DISABLED
221	movi	a0, coprocessor_owner
222	addx4	a0, a3, a0
223
224	/* Set new 'owner' (a0 points to the CP owner, a3 contains the CP nr) */
225
2261:	GET_THREAD_INFO (a4, a1)
227	s32i	a4, a0, 0
228
229	/* Get context save area and 'call' load routine. */
230
231	movi	a5, .Lload_cp_regs_jump_table
232	movi	a0, 1f
233	addx8	a3, a3, a5
234	l32i	a2, a3, 4		# a2: xtregs offset
235	l32i	a3, a3, 0		# a3: jump address
236	add	a2, a2, a4
237	jx	a3
238
239	/* Restore all registers and return from exception handler. */
240
2411:	l32i	a6, a1, PT_AREG6
242	l32i	a5, a1, PT_AREG5
243	l32i	a4, a1, PT_AREG4
244
245	l32i	a0, a1, PT_SAR
246	l32i	a3, a1, PT_AREG3
247	l32i	a2, a1, PT_AREG2
248	wsr	a0, sar
249	l32i	a0, a1, PT_AREG0
250	l32i	a1, a1, PT_AREG1
251
252	rfe
253
254ENDPROC(fast_coprocessor)
255
256	.data
257
258ENTRY(coprocessor_owner)
259
260	.fill XCHAL_CP_MAX, 4, 0
261
262END(coprocessor_owner)
263
264#endif /* XTENSA_HAVE_COPROCESSORS */
265