summaryrefslogtreecommitdiff
path: root/libcpu/rh850/common/context_gcc.S
blob: 7a5086d65ddc8514bb6a8090d781d308a3a89a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (c) 2026, Linumiz
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2026-11-06     Parthiban    first version
 */

#include <rtconfig.h>
#include "rh850_asm.h"
#include "rh850_core.h"

#if defined(RT_USING_FPU) && RH850_CORE_HAS_SYNCE
#define RH850_SYNCE 1
#endif
#if defined(RT_USING_FPU) && RH850_CORE_HAS_FPEC
#define RH850_FPEC 1
#endif

/* Layout of struct rt_hw_stack_frame */
    .macro SAVE_CONTEXT
#ifdef RH850_SYNCE
    /* FPU results and FPI reports are complete before FPSR is read */
    synce
#endif
    pushsp  r1-r2
    pushsp  r4-r31
    add     -16, sp
    rh850_stsr RH850_SR_EIPC, r10
    st.w    r10, 0[sp]
    rh850_stsr RH850_SR_EIPSW, r10
    st.w    r10, 4[sp]
#ifdef RT_USING_FPU
    rh850_stsr RH850_SR_FPSR, r10
    st.w    r10, 8[sp]
    rh850_stsr RH850_SR_FPEPC, r10
    st.w    r10, 12[sp]
#endif
    .endm

/* While FPEC.FPIVD is 1, keep FPEPC and the FPSR cause bits */
    .macro RESTORE_FPU
    ld.w    8[sp], r10
#ifdef RH850_FPEC
    rh850_stsr RH850_SR_FPEC, r11
    andi    RH850_FPEC_FPIVD, r11, r11
    bz      .Lfpu_frame\@
    rh850_stsr RH850_SR_FPSR, r11
    andi    RH850_FPSR_XC_MASK, r11, r11
    andi    RH850_FPSR_XC_MASK, r10, r12
    xor     r12, r10
    or      r11, r10
    ldsr    r10, RH850_SR_FPSR
    br      .Lfpu_done\@
.Lfpu_frame\@:
#endif
    ldsr    r10, RH850_SR_FPSR
    ld.w    12[sp], r10
    ldsr    r10, RH850_SR_FPEPC
.Lfpu_done\@:
    .endm

    .macro RESTORE_CONTEXT
#ifdef RT_USING_FPU
    RESTORE_FPU
#endif
    ld.w    4[sp], r10
    ldsr    r10, RH850_SR_EIPSW
    ld.w    0[sp], r10
    ldsr    r10, RH850_SR_EIPC
    addi    16, sp, sp
    popsp   r4-r31
    popsp   r1-r2
    eiret
    .endm

    .text

    .globl C_SYM(rt_hw_interrupt_disable)
    .type C_SYM(rt_hw_interrupt_disable), @function
C_SYM(rt_hw_interrupt_disable):
    stsr    5, r10, 0
    di
    jmp     [lp]

    .globl C_SYM(rt_hw_interrupt_enable)
    .type C_SYM(rt_hw_interrupt_enable), @function
C_SYM(rt_hw_interrupt_enable):
    andi    RH850_PSW_ID, r6, r10
    bnz     1f
    ei
1:
    jmp     [lp]

/* EP = 1, so that EIRET leaves ISPR as it is */
    .globl C_SYM(rt_hw_context_switch_to)
    .type C_SYM(rt_hw_context_switch_to), @function
C_SYM(rt_hw_context_switch_to):
    rh850_stsr RH850_SR_PSW, r10
    ori     RH850_PSW_EP | RH850_PSW_ID, r10, r10
    ldsr    r10, RH850_SR_PSW
    ld.w    0[r6], sp
    RESTORE_CONTEXT

/* PSW.ID is 1 here, so a pending FPI of this thread is reported as a fault */
    .globl C_SYM(rt_hw_context_switch)
    .type C_SYM(rt_hw_context_switch), @function
C_SYM(rt_hw_context_switch):
#ifdef RH850_FPEC
    synce
    rh850_stsr RH850_SR_FPEC, r10
    andi    RH850_FPEC_FPIVD, r10, r10
    bz      1f
    jr      C_SYM(rh850_fpi_pending)
1:
#endif
    trap    0
    jmp     [lp]

    .globl rh850_trap0_entry
rh850_trap0_entry:
    SAVE_CONTEXT
    st.w    sp, 0[r6]
    ld.w    0[r7], sp
    RESTORE_CONTEXT

/* Return state and the GCC call-used registers of a nested interrupt */
    .macro SAVE_NESTED
#ifdef RH850_SYNCE
    synce
#endif
    pushsp  r1-r2
    pushsp  r5-r19
    pushsp  r30-r31
    add     -16, sp
    rh850_stsr RH850_SR_EIPC, r10
    st.w    r10, 0[sp]
    rh850_stsr RH850_SR_EIPSW, r10
    st.w    r10, 4[sp]
#ifdef RT_USING_FPU
    rh850_stsr RH850_SR_FPSR, r10
    st.w    r10, 8[sp]
    rh850_stsr RH850_SR_FPEPC, r10
    st.w    r10, 12[sp]
#endif
    .endm

    .macro RESTORE_NESTED
#ifdef RT_USING_FPU
    RESTORE_FPU
#endif
    ld.w    4[sp], r10
    ldsr    r10, RH850_SR_EIPSW
    ld.w    0[sp], r10
    ldsr    r10, RH850_SR_EIPC
    addi    16, sp, sp
    popsp   r30-r31
    popsp   r5-r19
    popsp   r1-r2
    eiret
    .endm

    .globl rh850_eiint_entry
rh850_eiint_entry:
    addi    -8, sp, sp
    st.w    r10, 0[sp]
    st.w    r11, 4[sp]
    mov     hilo(C_SYM(rh850_irq_nest)), r10
    ld.w    0[r10], r11
    cmp     r0, r11
    ld.w    0[sp], r10
    ld.w    4[sp], r11
    /* movea keeps the flags of cmp */
    movea   8, sp, sp
    bne     1f

    SAVE_CONTEXT
    mov     hilo(C_SYM(rh850_thread_sp)), r10
    st.w    sp, 0[r10]
    mov     hilo(__stack_top), sp
    br      2f

1:
    SAVE_NESTED

2:
    mov     hilo(C_SYM(rh850_irq_nest)), r10
    ld.w    0[r10], r11
    add     1, r11
    st.w    r11, 0[r10]
    /* EIIC is read before EI: a nested interrupt overwrites it */
    rh850_stsr RH850_SR_EIIC, r6
    jarl    C_SYM(rh850_irq_handle), lp
    mov     hilo(C_SYM(rh850_irq_nest)), r10
    ld.w    0[r10], r11
    add     -1, r11
    st.w    r11, 0[r10]
    cmp     r0, r11
    bne     3f

    mov     hilo(C_SYM(rt_thread_switch_interrupt_flag)), r10
    ld.w    0[r10], r11
    cmp     r0, r11
    be      4f
#ifdef RH850_FPEC
    /* A pending FPI is taken after EIRET: keep the switch for a later exit */
    rh850_stsr RH850_SR_FPEC, r11
    andi    RH850_FPEC_FPIVD, r11, r11
    bnz     4f
#endif
    st.w    r0, 0[r10]
    mov     hilo(C_SYM(rh850_thread_sp)), r10
    ld.w    0[r10], r11
    mov     hilo(C_SYM(rt_interrupt_from_thread)), r10
    ld.w    0[r10], r10
    st.w    r11, 0[r10]
    mov     hilo(C_SYM(rt_interrupt_to_thread)), r10
    ld.w    0[r10], r10
    ld.w    0[r10], sp
    /* EIRET with EP = 0 ends the interrupt in ISPR */
    RESTORE_CONTEXT

4:
    mov     hilo(C_SYM(rh850_thread_sp)), r10
    ld.w    0[r10], sp
    RESTORE_CONTEXT

3:
    RESTORE_NESTED