]> granicus.if.org Git - esp-idf/blob - components/freertos/xtensa_context.S
sleep: fix checking length of RTC data sections
[esp-idf] / components / freertos / xtensa_context.S
1 /*******************************************************************************
2 Copyright (c) 2006-2015 Cadence Design Systems Inc.
3
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 --------------------------------------------------------------------------------
23
24         XTENSA CONTEXT SAVE AND RESTORE ROUTINES
25
26 Low-level Call0 functions for handling generic context save and restore of
27 registers not specifically addressed by the interrupt vectors and handlers.
28 Those registers (not handled by these functions) are PC, PS, A0, A1 (SP).
29 Except for the calls to RTOS functions, this code is generic to Xtensa.
30
31 Note that in Call0 ABI, interrupt handlers are expected to preserve the callee-
32 save regs (A12-A15), which is always the case if the handlers are coded in C.
33 However A12, A13 are made available as scratch registers for interrupt dispatch
34 code, so are presumed saved anyway, and are always restored even in Call0 ABI.
35 Only A14, A15 are truly handled as callee-save regs.
36
37 Because Xtensa is a configurable architecture, this port supports all user
38 generated configurations (except restrictions stated in the release notes).
39 This is accomplished by conditional compilation using macros and functions
40 defined in the Xtensa HAL (hardware adaptation layer) for your configuration.
41 Only the processor state included in your configuration is saved and restored,
42 including any processor state added by user configuration options or TIE.
43
44 *******************************************************************************/
45
46 /*  Warn nicely if this file gets named with a lowercase .s instead of .S:  */
47 #define NOERROR #
48 NOERROR: .error "C preprocessor needed for this file: make sure its filename\
49  ends in uppercase .S, or use xt-xcc's -x assembler-with-cpp option."
50
51
52 #include "xtensa_rtos.h"
53 #include "xtensa_context.h"
54
55 #ifdef XT_USE_OVLY
56 #include <xtensa/overlay_os_asm.h>
57 #endif
58
59     .text
60
61
62
63 /*******************************************************************************
64
65 _xt_context_save
66
67     !! MUST BE CALLED ONLY BY 'CALL0' INSTRUCTION !!
68
69 Saves all Xtensa processor state except PC, PS, A0, A1 (SP), A12, A13, in the
70 interrupt stack frame defined in xtensa_rtos.h.
71 Its counterpart is _xt_context_restore (which also restores A12, A13).
72
73 Caller is expected to have saved PC, PS, A0, A1 (SP), A12, A13 in the frame.
74 This function preserves A12 & A13 in order to provide the caller with 2 scratch 
75 regs that need not be saved over the call to this function. The choice of which
76 2 regs to provide is governed by xthal_window_spill_nw and xthal_save_extra_nw,
77 to avoid moving data more than necessary. Caller can assign regs accordingly.
78
79 Entry Conditions:
80     A0  = Return address in caller.
81     A1  = Stack pointer of interrupted thread or handler ("interruptee").
82     Original A12, A13 have already been saved in the interrupt stack frame.
83     Other processor state except PC, PS, A0, A1 (SP), A12, A13, is as at the 
84     point of interruption.
85     If windowed ABI, PS.EXCM = 1 (exceptions disabled).
86
87 Exit conditions:
88     A0  = Return address in caller.
89     A1  = Stack pointer of interrupted thread or handler ("interruptee").
90     A12, A13 as at entry (preserved).
91     If windowed ABI, PS.EXCM = 1 (exceptions disabled).
92
93 *******************************************************************************/
94
95     .global _xt_context_save
96     .type   _xt_context_save,@function
97     .align  4
98         .literal_position
99     .align  4
100 _xt_context_save:
101
102     s32i    a2,  sp, XT_STK_A2
103     s32i    a3,  sp, XT_STK_A3
104     s32i    a4,  sp, XT_STK_A4
105     s32i    a5,  sp, XT_STK_A5
106     s32i    a6,  sp, XT_STK_A6
107     s32i    a7,  sp, XT_STK_A7
108     s32i    a8,  sp, XT_STK_A8
109     s32i    a9,  sp, XT_STK_A9
110     s32i    a10, sp, XT_STK_A10
111     s32i    a11, sp, XT_STK_A11
112
113     /*
114     Call0 ABI callee-saved regs a12-15 do not need to be saved here.
115     a12-13 are the caller's responsibility so it can use them as scratch.
116     So only need to save a14-a15 here for Windowed ABI (not Call0).
117     */
118     #ifndef __XTENSA_CALL0_ABI__
119     s32i    a14, sp, XT_STK_A14
120     s32i    a15, sp, XT_STK_A15
121     #endif
122
123     rsr     a3,  SAR
124     s32i    a3,  sp, XT_STK_SAR
125
126     #if XCHAL_HAVE_LOOPS
127     rsr     a3,  LBEG
128     s32i    a3,  sp, XT_STK_LBEG
129     rsr     a3,  LEND
130     s32i    a3,  sp, XT_STK_LEND
131     rsr     a3,  LCOUNT
132     s32i    a3,  sp, XT_STK_LCOUNT
133     #endif
134
135     #ifdef XT_USE_SWPRI
136     /* Save virtual priority mask */
137     movi    a3,  _xt_vpri_mask
138     l32i    a3,  a3, 0
139     s32i    a3,  sp, XT_STK_VPRI
140     #endif
141
142     #if XCHAL_EXTRA_SA_SIZE > 0 || !defined(__XTENSA_CALL0_ABI__)
143     mov     a9,  a0                     /* preserve ret addr */
144     #endif
145
146     #ifndef __XTENSA_CALL0_ABI__
147     /*
148     To spill the reg windows, temp. need pre-interrupt stack ptr and a4-15.
149     Need to save a9,12,13 temporarily (in frame temps) and recover originals.
150     Interrupts need to be disabled below XCHAL_EXCM_LEVEL and window overflow
151     and underflow exceptions disabled (assured by PS.EXCM == 1).
152     */
153     s32i    a12, sp, XT_STK_TMP0        /* temp. save stuff in stack frame */
154     s32i    a13, sp, XT_STK_TMP1    
155     s32i    a9,  sp, XT_STK_TMP2    
156
157     /*
158     Save the overlay state if we are supporting overlays. Since we just saved
159     three registers, we can conveniently use them here. Note that as of now,
160     overlays only work for windowed calling ABI.
161     */
162     #ifdef XT_USE_OVLY
163     l32i    a9,  sp, XT_STK_PC          /* recover saved PC */
164     _xt_overlay_get_state    a9, a12, a13
165     s32i    a9,  sp, XT_STK_OVLY        /* save overlay state */
166     #endif
167
168     l32i    a12, sp, XT_STK_A12         /* recover original a9,12,13 */
169     l32i    a13, sp, XT_STK_A13
170     l32i    a9,  sp, XT_STK_A9
171     addi    sp,  sp, XT_STK_FRMSZ       /* restore the interruptee's SP */
172     call0   xthal_window_spill_nw       /* preserves only a4,5,8,9,12,13 */
173     addi    sp,  sp, -XT_STK_FRMSZ
174     l32i    a12, sp, XT_STK_TMP0        /* recover stuff from stack frame */
175     l32i    a13, sp, XT_STK_TMP1    
176     l32i    a9,  sp, XT_STK_TMP2    
177     #endif
178
179     #if XCHAL_EXTRA_SA_SIZE > 0
180     /*  
181     NOTE: Normally the xthal_save_extra_nw macro only affects address
182     registers a2-a5. It is theoretically possible for Xtensa processor
183     designers to write TIE that causes more address registers to be
184     affected, but it is generally unlikely. If that ever happens,
185     more registers need to be saved/restored around this macro invocation.
186     Here we assume a9,12,13 are preserved.
187     Future Xtensa tools releases might limit the regs that can be affected.
188     */
189     addi    a2,  sp, XT_STK_EXTRA       /* where to save it */
190     # if XCHAL_EXTRA_SA_ALIGN > 16
191     movi    a3, -XCHAL_EXTRA_SA_ALIGN
192     and     a2, a2, a3                  /* align dynamically >16 bytes */
193     # endif
194     call0   xthal_save_extra_nw         /* destroys a0,2,3,4,5 */
195     #endif
196
197     #if XCHAL_EXTRA_SA_SIZE > 0 || !defined(__XTENSA_CALL0_ABI__)
198     mov     a0, a9                      /* retrieve ret addr */
199     #endif
200
201     ret
202
203 /*******************************************************************************
204
205 _xt_context_restore
206
207     !! MUST BE CALLED ONLY BY 'CALL0' INSTRUCTION !!
208
209 Restores all Xtensa processor state except PC, PS, A0, A1 (SP) (and in Call0
210 ABI, A14, A15 which are preserved by all interrupt handlers) from an interrupt 
211 stack frame defined in xtensa_rtos.h .
212 Its counterpart is _xt_context_save (whose caller saved A12, A13).
213
214 Caller is responsible to restore PC, PS, A0, A1 (SP).
215
216 Entry Conditions:
217     A0  = Return address in caller.
218     A1  = Stack pointer of interrupted thread or handler ("interruptee").
219
220 Exit conditions:
221     A0  = Return address in caller.
222     A1  = Stack pointer of interrupted thread or handler ("interruptee").
223     Other processor state except PC, PS, A0, A1 (SP), is as at the point 
224     of interruption.
225
226 *******************************************************************************/
227
228     .global _xt_context_restore
229     .type   _xt_context_restore,@function
230     .align  4
231         .literal_position
232     .align  4
233 _xt_context_restore:
234
235     #if XCHAL_EXTRA_SA_SIZE > 0
236     /*  
237     NOTE: Normally the xthal_restore_extra_nw macro only affects address
238     registers a2-a5. It is theoretically possible for Xtensa processor
239     designers to write TIE that causes more address registers to be
240     affected, but it is generally unlikely. If that ever happens,
241     more registers need to be saved/restored around this macro invocation.
242     Here we only assume a13 is preserved.
243     Future Xtensa tools releases might limit the regs that can be affected.
244     */
245     mov     a13, a0                     /* preserve ret addr */
246     addi    a2,  sp, XT_STK_EXTRA       /* where to find it */
247     # if XCHAL_EXTRA_SA_ALIGN > 16
248     movi    a3, -XCHAL_EXTRA_SA_ALIGN
249     and     a2, a2, a3                  /* align dynamically >16 bytes */
250     # endif
251     call0   xthal_restore_extra_nw      /* destroys a0,2,3,4,5 */
252     mov     a0,  a13                    /* retrieve ret addr */
253     #endif
254
255     #if XCHAL_HAVE_LOOPS
256     l32i    a2,  sp, XT_STK_LBEG
257     l32i    a3,  sp, XT_STK_LEND
258     wsr     a2,  LBEG
259     l32i    a2,  sp, XT_STK_LCOUNT
260     wsr     a3,  LEND
261     wsr     a2,  LCOUNT
262     #endif
263
264     #ifdef XT_USE_OVLY
265     /*
266     If we are using overlays, this is a good spot to check if we need
267     to restore an overlay for the incoming task. Here we have a bunch
268     of registers to spare. Note that this step is going to use a few
269     bytes of storage below SP (SP-20 to SP-32) if an overlay is going
270     to be restored.
271     */
272     l32i    a2,  sp, XT_STK_PC          /* retrieve PC */
273     l32i    a3,  sp, XT_STK_PS          /* retrieve PS */
274     l32i    a4,  sp, XT_STK_OVLY        /* retrieve overlay state */
275     l32i    a5,  sp, XT_STK_A1          /* retrieve stack ptr */
276     _xt_overlay_check_map    a2, a3, a4, a5, a6
277     s32i    a2,  sp, XT_STK_PC          /* save updated PC */
278     s32i    a3,  sp, XT_STK_PS          /* save updated PS */
279     #endif
280
281     #ifdef XT_USE_SWPRI
282     /* Restore virtual interrupt priority and interrupt enable */
283     movi    a3,  _xt_intdata
284     l32i    a4,  a3, 0                  /* a4 = _xt_intenable */
285     l32i    a5,  sp, XT_STK_VPRI        /* a5 = saved _xt_vpri_mask */
286     and     a4,  a4, a5
287     wsr     a4,  INTENABLE              /* update INTENABLE */
288     s32i    a5,  a3, 4                  /* restore _xt_vpri_mask */
289     #endif
290
291     l32i    a3,  sp, XT_STK_SAR
292     l32i    a2,  sp, XT_STK_A2
293     wsr     a3,  SAR
294     l32i    a3,  sp, XT_STK_A3
295     l32i    a4,  sp, XT_STK_A4
296     l32i    a5,  sp, XT_STK_A5
297     l32i    a6,  sp, XT_STK_A6
298     l32i    a7,  sp, XT_STK_A7
299     l32i    a8,  sp, XT_STK_A8
300     l32i    a9,  sp, XT_STK_A9
301     l32i    a10, sp, XT_STK_A10
302     l32i    a11, sp, XT_STK_A11
303
304     /*
305     Call0 ABI callee-saved regs a12-15 do not need to be restored here.
306     However a12-13 were saved for scratch before XT_RTOS_INT_ENTER(), 
307     so need to be restored anyway, despite being callee-saved in Call0.
308     */
309     l32i    a12, sp, XT_STK_A12
310     l32i    a13, sp, XT_STK_A13
311     #ifndef __XTENSA_CALL0_ABI__
312     l32i    a14, sp, XT_STK_A14
313     l32i    a15, sp, XT_STK_A15
314     #endif
315
316     ret
317
318
319 /*******************************************************************************
320
321 _xt_coproc_init
322
323 Initializes global co-processor management data, setting all co-processors
324 to "unowned". Leaves CPENABLE as it found it (does NOT clear it).
325
326 Called during initialization of the RTOS, before any threads run.
327
328 This may be called from normal Xtensa single-threaded application code which
329 might use co-processors. The Xtensa run-time initialization enables all 
330 co-processors. They must remain enabled here, else a co-processor exception
331 might occur outside of a thread, which the exception handler doesn't expect.
332
333 Entry Conditions:
334     Xtensa single-threaded run-time environment is in effect.
335     No thread is yet running.
336
337 Exit conditions:
338     None.
339
340 Obeys ABI conventions per prototype:
341     void _xt_coproc_init(void)
342
343 *******************************************************************************/
344
345 #if XCHAL_CP_NUM > 0
346
347     .global _xt_coproc_init
348     .type   _xt_coproc_init,@function
349     .align  4
350         .literal_position
351     .align  4
352 _xt_coproc_init:
353     ENTRY0
354
355     /* Initialize thread co-processor ownerships to 0 (unowned). */
356     movi    a2, _xt_coproc_owner_sa         /* a2 = base of owner array */
357     addi    a3, a2, (XCHAL_CP_MAX*portNUM_PROCESSORS) << 2       /* a3 = top+1 of owner array */
358     movi    a4, 0                           /* a4 = 0 (unowned) */
359 1:  s32i    a4, a2, 0
360     addi    a2, a2, 4
361     bltu    a2, a3, 1b
362
363     RET0
364
365 #endif
366
367
368 /*******************************************************************************
369
370 _xt_coproc_release
371
372 Releases any and all co-processors owned by a given thread. The thread is 
373 identified by it's co-processor state save area defined in xtensa_context.h .
374
375 Must be called before a thread's co-proc save area is deleted to avoid
376 memory corruption when the exception handler tries to save the state.
377 May be called when a thread terminates or completes but does not delete
378 the co-proc save area, to avoid the exception handler having to save the 
379 thread's co-proc state before another thread can use it (optimization).
380
381 Needs to be called on the processor the thread was running on. Unpinned threads
382 won't have an entry here because they get pinned as soon they use a coprocessor.
383
384 Entry Conditions:
385     A2  = Pointer to base of co-processor state save area.
386
387 Exit conditions:
388     None.
389
390 Obeys ABI conventions per prototype:
391     void _xt_coproc_release(void * coproc_sa_base)
392
393 *******************************************************************************/
394
395 #if XCHAL_CP_NUM > 0
396
397     .global _xt_coproc_release
398     .type   _xt_coproc_release,@function
399     .align  4
400         .literal_position
401     .align  4
402 _xt_coproc_release:
403     ENTRY0                                  /* a2 = base of save area */
404
405         getcoreid a5
406         movi    a3, XCHAL_CP_MAX << 2
407         mull    a5, a5, a3
408     movi    a3, _xt_coproc_owner_sa         /* a3 = base of owner array */
409         add     a3, a3, a5
410
411     addi    a4, a3, XCHAL_CP_MAX << 2       /* a4 = top+1 of owner array */
412     movi    a5, 0                           /* a5 = 0 (unowned) */
413
414     rsil    a6, XCHAL_EXCM_LEVEL            /* lock interrupts */
415
416 1:  l32i    a7, a3, 0                       /* a7 = owner at a3 */
417     bne     a2, a7, 2f                      /* if (coproc_sa_base == owner) */
418     s32i    a5, a3, 0                       /*   owner = unowned */
419 2:  addi    a3, a3, 1<<2                    /* a3 = next entry in owner array */
420     bltu    a3, a4, 1b                      /* repeat until end of array */
421
422 3:  wsr     a6, PS                          /* restore interrupts */
423
424     RET0
425
426 #endif
427
428
429 /*******************************************************************************
430 _xt_coproc_savecs
431
432 If there is a current thread and it has a coprocessor state save area, then
433 save all callee-saved state into this area. This function is called from the
434 solicited context switch handler. It calls a system-specific function to get
435 the coprocessor save area base address.
436
437 Entry conditions:
438     - The thread being switched out is still the current thread.
439     - CPENABLE state reflects which coprocessors are active.
440     - Registers have been saved/spilled already.
441
442 Exit conditions:
443     - All necessary CP callee-saved state has been saved.
444     - Registers a2-a7, a13-a15 have been trashed.
445
446 Must be called from assembly code only, using CALL0.
447 *******************************************************************************/
448 #if XCHAL_CP_NUM > 0
449
450     .extern     _xt_coproc_sa_offset   /* external reference */
451
452     .global     _xt_coproc_savecs
453     .type       _xt_coproc_savecs,@function
454     .align  4
455         .literal_position
456     .align      4
457 _xt_coproc_savecs:
458
459     /* At entry, CPENABLE should be showing which CPs are enabled. */
460
461     rsr     a2, CPENABLE                /* a2 = which CPs are enabled      */
462     beqz    a2, .Ldone                  /* quick exit if none              */
463     mov     a14, a0                     /* save return address             */
464     call0   XT_RTOS_CP_STATE            /* get address of CP save area     */
465     mov     a0, a14                     /* restore return address          */
466     beqz    a15, .Ldone                 /* if none then nothing to do      */
467     s16i    a2, a15, XT_CP_CS_ST        /* save mask of CPs being stored   */
468     movi    a13, _xt_coproc_sa_offset   /* array of CP save offsets        */
469     l32i    a15, a15, XT_CP_ASA         /* a15 = base of aligned save area */
470
471 #if XCHAL_CP0_SA_SIZE
472     bbci.l  a2, 0, 2f                   /* CP 0 not enabled                */
473     l32i    a14, a13, 0                 /* a14 = _xt_coproc_sa_offset[0]   */
474     add     a3, a14, a15                /* a3 = save area for CP 0         */
475     xchal_cp0_store a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
476 2:
477 #endif
478
479 #if XCHAL_CP1_SA_SIZE
480     bbci.l  a2, 1, 2f                   /* CP 1 not enabled                */
481     l32i    a14, a13, 4                 /* a14 = _xt_coproc_sa_offset[1]   */
482     add     a3, a14, a15                /* a3 = save area for CP 1         */
483     xchal_cp1_store a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
484 2:
485 #endif
486
487 #if XCHAL_CP2_SA_SIZE
488     bbci.l  a2, 2, 2f
489     l32i    a14, a13, 8
490     add     a3, a14, a15
491     xchal_cp2_store a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
492 2:
493 #endif
494
495 #if XCHAL_CP3_SA_SIZE
496     bbci.l  a2, 3, 2f
497     l32i    a14, a13, 12
498     add     a3, a14, a15
499     xchal_cp3_store a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
500 2:
501 #endif
502
503 #if XCHAL_CP4_SA_SIZE
504     bbci.l  a2, 4, 2f
505     l32i    a14, a13, 16
506     add     a3, a14, a15
507     xchal_cp4_store a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
508 2:
509 #endif
510
511 #if XCHAL_CP5_SA_SIZE
512     bbci.l  a2, 5, 2f
513     l32i    a14, a13, 20
514     add     a3, a14, a15
515     xchal_cp5_store a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
516 2:
517 #endif
518
519 #if XCHAL_CP6_SA_SIZE
520     bbci.l  a2, 6, 2f
521     l32i    a14, a13, 24
522     add     a3, a14, a15
523     xchal_cp6_store a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
524 2:
525 #endif
526
527 #if XCHAL_CP7_SA_SIZE
528     bbci.l  a2, 7, 2f
529     l32i    a14, a13, 28
530     add     a3, a14, a15
531     xchal_cp7_store a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
532 2:
533 #endif
534
535 .Ldone:
536     ret
537 #endif
538
539
540 /*******************************************************************************
541 _xt_coproc_restorecs
542
543 Restore any callee-saved coprocessor state for the incoming thread.
544 This function is called from coprocessor exception handling, when giving
545 ownership to a thread that solicited a context switch earlier. It calls a
546 system-specific function to get the coprocessor save area base address.
547
548 Entry conditions:
549     - The incoming thread is set as the current thread.
550     - CPENABLE is set up correctly for all required coprocessors.
551     - a2 = mask of coprocessors to be restored.
552
553 Exit conditions:
554     - All necessary CP callee-saved state has been restored.
555     - CPENABLE - unchanged.
556     - Registers a2-a7, a13-a15 have been trashed.
557
558 Must be called from assembly code only, using CALL0.
559 *******************************************************************************/
560 #if XCHAL_CP_NUM > 0
561
562     .global     _xt_coproc_restorecs
563     .type       _xt_coproc_restorecs,@function
564     .align  4
565         .literal_position
566     .align      4
567 _xt_coproc_restorecs:
568
569     mov     a14, a0                     /* save return address             */
570     call0   XT_RTOS_CP_STATE            /* get address of CP save area     */
571     mov     a0, a14                     /* restore return address          */
572     beqz    a15, .Ldone2                /* if none then nothing to do      */
573     l16ui   a3, a15, XT_CP_CS_ST        /* a3 = which CPs have been saved  */
574     xor     a3, a3, a2                  /* clear the ones being restored   */
575     s32i    a3, a15, XT_CP_CS_ST        /* update saved CP mask            */
576     movi    a13, _xt_coproc_sa_offset   /* array of CP save offsets        */
577     l32i    a15, a15, XT_CP_ASA         /* a15 = base of aligned save area */
578     
579 #if XCHAL_CP0_SA_SIZE
580     bbci.l  a2, 0, 2f                   /* CP 0 not enabled                */
581     l32i    a14, a13, 0                 /* a14 = _xt_coproc_sa_offset[0]   */
582     add     a3, a14, a15                /* a3 = save area for CP 0         */
583     xchal_cp0_load a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
584 2:  
585 #endif
586
587 #if XCHAL_CP1_SA_SIZE
588     bbci.l  a2, 1, 2f                   /* CP 1 not enabled                */
589     l32i    a14, a13, 4                 /* a14 = _xt_coproc_sa_offset[1]   */
590     add     a3, a14, a15                /* a3 = save area for CP 1         */
591     xchal_cp1_load a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
592 2:
593 #endif
594
595 #if XCHAL_CP2_SA_SIZE
596     bbci.l  a2, 2, 2f
597     l32i    a14, a13, 8
598     add     a3, a14, a15
599     xchal_cp2_load a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
600 2:
601 #endif
602
603 #if XCHAL_CP3_SA_SIZE
604     bbci.l  a2, 3, 2f
605     l32i    a14, a13, 12
606     add     a3, a14, a15
607     xchal_cp3_load a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
608 2:
609 #endif
610
611 #if XCHAL_CP4_SA_SIZE
612     bbci.l  a2, 4, 2f
613     l32i    a14, a13, 16
614     add     a3, a14, a15
615     xchal_cp4_load a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
616 2:
617 #endif
618
619 #if XCHAL_CP5_SA_SIZE
620     bbci.l  a2, 5, 2f
621     l32i    a14, a13, 20
622     add     a3, a14, a15
623     xchal_cp5_load a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
624 2:
625 #endif
626
627 #if XCHAL_CP6_SA_SIZE
628     bbci.l  a2, 6, 2f
629     l32i    a14, a13, 24
630     add     a3, a14, a15
631     xchal_cp6_load a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
632 2:
633 #endif
634
635 #if XCHAL_CP7_SA_SIZE
636     bbci.l  a2, 7, 2f
637     l32i    a14, a13, 28
638     add     a3, a14, a15
639     xchal_cp7_load a3, a4, a5, a6, a7 continue=0 ofs=-1 select=XTHAL_SAS_TIE|XTHAL_SAS_NOCC|XTHAL_SAS_CALE alloc=XTHAL_SAS_ALL
640 2:
641 #endif
642
643 .Ldone2:
644     ret
645
646 #endif
647
648