2 * arch/xtensa/kernel/coprocessor.S
4 * Xtensa processor configuration-specific table of coprocessor and
5 * other custom register layout information.
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
11 * Copyright (C) 2003 - 2007 Tensilica Inc.
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>
23 * Rules for coprocessor state manipulation on SMP:
25 * - a task may have live coprocessors only on one CPU.
27 * - whether coprocessor context of task T is live on some CPU is
28 * denoted by T's thread_info->cpenable.
30 * - non-zero thread_info->cpenable means that thread_info->cp_owner_cpu
31 * is valid in the T's thread_info. Zero thread_info->cpenable means that
32 * coprocessor context is valid in the T's thread_info.
34 * - if a coprocessor context of task T is live on CPU X, only CPU X changes
35 * T's thread_info->cpenable, cp_owner_cpu and coprocessor save area.
36 * This is done by making sure that for the task T with live coprocessor
37 * on CPU X cpenable SR is 0 when T runs on any other CPU Y.
38 * When fast_coprocessor exception is taken on CPU Y it goes to the
39 * C-level do_coprocessor that uses IPI to make CPU X flush T's coprocessors.
42 #if XTENSA_HAVE_COPROCESSORS
45 * Macros for lazy context switch.
48 #define SAVE_CP_REGS(x) \
49 .if XTENSA_HAVE_COPROCESSOR(x); \
51 .Lsave_cp_regs_cp##x: \
52 xchal_cp##x##_store a2 a3 a4 a5 a6; \
56 #define LOAD_CP_REGS(x) \
57 .if XTENSA_HAVE_COPROCESSOR(x); \
59 .Lload_cp_regs_cp##x: \
60 xchal_cp##x##_load a2 a3 a4 a5 a6; \
64 #define CP_REGS_TAB(x) \
65 .if XTENSA_HAVE_COPROCESSOR(x); \
66 .long .Lsave_cp_regs_cp##x; \
67 .long .Lload_cp_regs_cp##x; \
71 .long THREAD_XTREGS_CP##x
73 #define CP_REGS_TAB_SAVE 0
74 #define CP_REGS_TAB_LOAD 4
75 #define CP_REGS_TAB_OFFSET 8
111 * a0: trashed, original value saved on stack (PT_AREG0)
113 * a2: new stack pointer, original in DEPC
115 * depc: a2, original value saved on stack (PT_DEPC)
116 * excsave_1: dispatch table
118 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
119 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
122 ENTRY(fast_coprocessor)
124 s32i a3, a2, PT_AREG3
128 * Check if any coprocessor context is live on another CPU
129 * and if so go through the C-level coprocessor exception handler
130 * to flush it to memory.
132 GET_THREAD_INFO (a0, a2)
133 l32i a3, a0, THREAD_CPENABLE
134 beqz a3, .Lload_local
137 * Pairs with smp_wmb in local_coprocessor_release_all
138 * and with both memws below.
141 l32i a3, a0, THREAD_CPU
142 l32i a0, a0, THREAD_CP_OWNER_CPU
143 beq a0, a3, .Lload_local
146 l32i a3, a2, PT_AREG3
147 bbci.l a0, PS_UM_BIT, 1f
149 1: call0 kernel_exception
152 /* Save remaining registers a1-a3 and SAR */
156 s32i a1, a2, PT_AREG1
160 s32i a2, a1, PT_AREG2
162 /* The hal macros require up to 4 temporary registers. We use a3..a6. */
164 s32i a4, a1, PT_AREG4
165 s32i a5, a1, PT_AREG5
166 s32i a6, a1, PT_AREG6
167 s32i a7, a1, PT_AREG7
168 s32i a8, a1, PT_AREG8
169 s32i a9, a1, PT_AREG9
170 s32i a10, a1, PT_AREG10
172 /* Find coprocessor number. Subtract first CP EXCCAUSE from EXCCAUSE */
175 addi a3, a3, -EXCCAUSE_COPROCESSOR0_DISABLED
177 /* Set corresponding CPENABLE bit -> (sar:cp-index, a3: 1<<cp-index)*/
179 ssl a3 # SAR: 32 - coprocessor_number
187 /* Get coprocessor save/load table entry (a7). */
189 movi a7, .Lcp_regs_jump_table
193 /* Retrieve previous owner (a8). */
195 rsr a0, excsave1 # exc_table
196 addx4 a0, a3, a0 # entry for CP
197 l32i a8, a0, EXC_TABLE_COPROCESSOR_OWNER
199 /* Set new owner (a9). */
201 GET_THREAD_INFO (a9, a1)
202 l32i a4, a9, THREAD_CPU
203 s32i a9, a0, EXC_TABLE_COPROCESSOR_OWNER
204 s32i a4, a9, THREAD_CP_OWNER_CPU
207 * Enable coprocessor for the new owner. (a2 = 1 << CP number)
208 * This can be done before loading context into the coprocessor.
210 l32i a4, a9, THREAD_CPENABLE
214 * Make sure THREAD_CP_OWNER_CPU is in memory before updating
218 s32i a4, a9, THREAD_CPENABLE
220 beqz a8, 1f # skip 'save' if no previous owner
222 /* Disable coprocessor for previous owner. (a2 = 1 << CP number) */
224 l32i a10, a8, THREAD_CPENABLE
227 /* Get context save area and call save routine. */
229 l32i a2, a7, CP_REGS_TAB_OFFSET
230 l32i a3, a7, CP_REGS_TAB_SAVE
235 * Make sure coprocessor context and THREAD_CP_OWNER_CPU are in memory
236 * before updating THREAD_CPENABLE
239 s32i a10, a8, THREAD_CPENABLE
241 /* Get context save area and call load routine. */
243 l32i a2, a7, CP_REGS_TAB_OFFSET
244 l32i a3, a7, CP_REGS_TAB_LOAD
248 /* Restore all registers and return from exception handler. */
250 l32i a10, a1, PT_AREG10
251 l32i a9, a1, PT_AREG9
252 l32i a8, a1, PT_AREG8
253 l32i a7, a1, PT_AREG7
254 l32i a6, a1, PT_AREG6
255 l32i a5, a1, PT_AREG5
256 l32i a4, a1, PT_AREG4
259 l32i a3, a1, PT_AREG3
260 l32i a2, a1, PT_AREG2
262 l32i a0, a1, PT_AREG0
263 l32i a1, a1, PT_AREG1
267 ENDPROC(fast_coprocessor)
272 * coprocessor_flush(struct thread_info*, index)
275 * Save coprocessor registers for coprocessor 'index'.
276 * The register values are saved to or loaded from the coprocessor area
277 * inside the task_info structure.
279 * Note that this function doesn't update the coprocessor_owner information!
283 ENTRY(coprocessor_flush)
287 movi a4, .Lcp_regs_jump_table
290 l32i a4, a3, CP_REGS_TAB_SAVE
292 l32i a3, a3, CP_REGS_TAB_OFFSET
300 ENDPROC(coprocessor_flush)
302 #endif /* XTENSA_HAVE_COPROCESSORS */