1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #undef RSR
18 #define RSR(reg, at)         __asm__ __volatile__ ("rsr %0, %1" : "=r" (at) : "i" (reg))
19 #undef WSR
20 #define WSR(reg, at)         __asm__ __volatile__ ("wsr %0, %1" : : "r" (at), "i" (reg))
21 #define XSR(reg, at)         __asm__ __volatile__ ("xsr %0, %1" : "+r" (at) : "i" (reg))
22 
23 #define RER(reg, at)         __asm__ __volatile__ ("rer %0, %1" : "=r" (at) : "r" (reg))
24 
25 #define WITLB(at, as)        __asm__ __volatile__ ("witlb  %0, %1; \n isync \n " : : "r" (at), "r" (as))
26 #define WDTLB(at, as)        __asm__ __volatile__ ("wdtlb  %0, %1; \n dsync \n " : : "r" (at), "r" (as))
27 
28 /* The SET_STACK implements a setting a new stack pointer (sp or a1).
29  * to do this the need reset PS_WOE, reset WINDOWSTART, update SP, and return PS_WOE.
30  *
31  * Note: It has 2 implementations one for using in assembler files (*.S) and one for using in C.
32  *
33  * C code prototype for SET_STACK:
34  *   uint32_t ps_reg;
35  *   uint32_t w_base;
36  *   RSR(PS, ps_reg);
37  *   ps_reg &= ~(PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK);
38  *   WSR(PS, ps_reg);
39  *
40  *   RSR(WINDOWBASE, w_base);
41  *   WSR(WINDOWSTART, (1 << w_base));
42  *
43  *   asm volatile ( "movi sp, "XTSTR( (SOC_DRAM_LOW + (SOC_DRAM_HIGH - SOC_DRAM_LOW) / 2) )"");
44  *
45  *   RSR(PS, ps_reg);
46  *   ps_reg |= (PS_WOE_MASK);
47  *   WSR(PS, ps_reg);
48 */
49 #ifdef __ASSEMBLER__
50     .macro SET_STACK  new_sp tmp1 tmp2
51     rsr.ps \tmp1
52     movi \tmp2, ~(PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK)
53     and \tmp1, \tmp1, \tmp2
54     wsr.ps \tmp1
55     rsync
56 
57     rsr.windowbase \tmp1
58     ssl	\tmp1
59     movi \tmp1, 1
60     sll	\tmp1, \tmp1
61     wsr.windowstart \tmp1
62     rsync
63 
64     mov sp, \new_sp
65 
66     rsr.ps \tmp1
67     movi \tmp2, (PS_WOE)
68     or \tmp1, \tmp1, \tmp2
69     wsr.ps \tmp1
70     rsync
71     .endm
72 #else
73 #define SET_STACK(new_sp) \
74     do { \
75         uint32_t tmp1 = 0, tmp2 = 0; \
76         asm volatile ( \
77           "rsr.ps %1 \n"\
78           "movi %2, ~" XTSTR( PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK ) " \n"\
79           "and %1, %1, %2 \n"\
80           "wsr.ps %1 \n"\
81           "rsync \n"\
82           " \n"\
83           "rsr.windowbase %1 \n"\
84           "ssl	%1 \n"\
85           "movi %1, 1 \n"\
86           "sll	%1, %1 \n"\
87           "wsr.windowstart %1 \n"\
88           "rsync \n"\
89           " \n"\
90           "mov sp, %0 \n"\
91           "rsr.ps %1 \n"\
92           " \n"\
93           "movi %2, " XTSTR( PS_WOE_MASK ) "\n"\
94           " \n"\
95           "or %1, %1, %2 \n"\
96           "wsr.ps %1 \n"\
97           "rsync \n"\
98           : "+r"(new_sp), "+r"(tmp1), "+r"(tmp2)); \
99     } while (0);
100 #endif // __ASSEMBLER__
101