summaryrefslogtreecommitdiff
path: root/bsp/xuantie/libraries/xuantie_libraries/lib_cpu/clint.c
blob: 86e15f68226ade27866e9f765ffe016e057838c8 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018/12/23     Bernard      The first version
 * 2018/12/27     Jesven       Add secondary cpu boot
 * 2023/11/10     WangShun     Adapted to Xuantie C908MP
 */

#include <stdint.h>
#include <rthw.h>
#include <rtthread.h>
#include <csi_core.h>
#include <soc.h>
#include <drv/tick.h>
#include <asm/riscv_csr.h>

enum
{
    CRXX_MP_CORE0 = 0,
    CRXX_MP_CORE1,
    CRXX_MP_CORE2,
    CRXX_MP_CORE3,
    CRXX_MP_CORE4,
    CRXX_MP_CORE5,
    CRXX_MP_CORE6,
    CRXX_MP_CORE7,
};

/**
 * @brief    This function is used to suspend a specified kernel software interrupt.
 *
 * @note     Calling this function will trigger inter core interrupts.
 *
 * @param    id is the ID of the specific physical core.
 *
 */
void clint_ipi_send(uint64_t id)
{
    CLINT_Type *clint = (CLINT_Type *)CORET_BASE;

#if defined(CONFIG_RISCV_SMODE) && CONFIG_RISCV_SMODE
    switch (id)
    {
    case CRXX_MP_CORE0:
        clint->SSIP0 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE1:
        clint->SSIP1 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE2:
        clint->SSIP2 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE3:
        clint->SSIP3 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE4:
        clint->SSIP4 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE5:
        clint->SSIP5 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE6:
        clint->SSIP6 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE7:
        clint->SSIP7 |= (uint32_t)0x1;
        break;
    default:
        break;
    }
#else
    switch (id)
    {
    case CRXX_MP_CORE0:
        clint->MSIP0 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE1:
        clint->MSIP1 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE2:
        clint->MSIP2 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE3:
        clint->MSIP3 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE4:
        clint->MSIP4 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE5:
        clint->MSIP5 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE6:
        clint->MSIP6 |= (uint32_t)0x1;
        break;
    case CRXX_MP_CORE7:
        clint->MSIP7 |= (uint32_t)0x1;
        break;
    default:
        break;
    }
#endif
}

/**
 * @brief    This function is used to clear the specified kernel software interrupt.
 *
 * @note     Calling this function will clear the inter core interrupt suspend flag.
 *
 * @param    id is the ID of the specific physical core.
 *
 */
void clint_ipi_clear(uint64_t id)
{
    CLINT_Type *clint = (CLINT_Type *)CORET_BASE;

#if defined(CONFIG_RISCV_SMODE) && CONFIG_RISCV_SMODE
    switch (id)
    {
    case CRXX_MP_CORE0:
        clint->SSIP0 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE1:
        clint->SSIP1 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE2:
        clint->SSIP2 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE3:
        clint->SSIP3 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE4:
        clint->SSIP4 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE5:
        clint->SSIP5 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE6:
        clint->SSIP6 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE7:
        clint->SSIP7 &= ~(uint32_t)0x1;
        break;
    default:
        break;
    }
#else
    switch (id)
    {
    case CRXX_MP_CORE0:
        clint->MSIP0 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE1:
        clint->MSIP1 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE2:
        clint->MSIP2 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE3:
        clint->MSIP3 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE4:
        clint->MSIP4 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE5:
        clint->MSIP5 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE6:
        clint->MSIP6 &= ~(uint32_t)0x1;
        break;
    case CRXX_MP_CORE7:
        clint->MSIP7 &= ~(uint32_t)0x1;
        break;
    default:
        break;
    }
#endif
}

/**
 * @brief    This function is used to configure timers for specific physical cores.
 *
 * @note     Calling this function will configure Heartbeat Time.
 *
 * @param    NULL.
 *
 */
int rt_hw_tick_init(void)
{
    int core_id = rt_hw_cpu_id();

    if (core_id == CRXX_MP_CORE0)
    {
        return 0;
    }
    csi_tick_init();
    return 0;
}

/**
 * @brief    This function is used to enable software interrupts for specific physical cores.
 *
 * @note     Calling this function will enable software interruption.
 *
 * @param    NULL.
 *
 */
int rt_hw_clint_ipi_enable(void)
{
    /* Set the Machine-Software bit in MIE */
    rv_csr_set(0x304, SR_MIE);
    return 0;
}

/**
 * @brief    This function is used to initialize the interrupt management function unique to the physical core.
 *
 * @note     Calling this function will close the specific core external interrupt and set the trigger threshold to 0.
 *
 * @param    NULL.
 *
 */
void rt_hw_scondary_interrupt_init(void)
{
    int i;
    int core_id = 0;
    core_id = rt_hw_cpu_id();

    if (core_id == CRXX_MP_CORE0)
    {
        return;
    }
    else if (core_id == CRXX_MP_CORE1)
    {
        for (i = 0; i < (CONFIG_IRQ_NUM + 31) / 32; i++)
        {
            PLIC->PLIC_H1_MIE[i] = 0;
            PLIC->PLIC_H1_SIE[i] = 0;
        }

        /* set hart threshold 0, enable all interrupt */
        PLIC->PLIC_H1_MTH = 0;
        PLIC->PLIC_H1_STH = 0;
    }
    else if (core_id == CRXX_MP_CORE2)
    {
        for (i = 0; i < (CONFIG_IRQ_NUM + 31) / 32; i++)
        {
            PLIC->PLIC_H2_MIE[i] = 0;
            PLIC->PLIC_H2_SIE[i] = 0;
        }

        /* set hart threshold 0, enable all interrupt */
        PLIC->PLIC_H2_MTH = 0;
        PLIC->PLIC_H2_STH = 0;
    }
    else if (core_id == CRXX_MP_CORE3)
    {
        for (i = 0; i < (CONFIG_IRQ_NUM + 31) / 32; i++)
        {
            PLIC->PLIC_H3_MIE[i] = 0;
            PLIC->PLIC_H3_SIE[i] = 0;
        }

        /* set hart threshold 0, enable all interrupt */
        PLIC->PLIC_H3_MTH = 0;
        PLIC->PLIC_H3_STH = 0;
    }
    else if (core_id == CRXX_MP_CORE4)
    {
        for (i = 0; i < (CONFIG_IRQ_NUM + 31) / 32; i++)
        {
            PLIC->PLIC_H4_MIE[i] = 0;
            PLIC->PLIC_H4_SIE[i] = 0;
        }

        /* set hart threshold 0, enable all interrupt */
        PLIC->PLIC_H4_MTH = 0;
        PLIC->PLIC_H4_STH = 0;
    }
    else if (core_id == CRXX_MP_CORE5)
    {
        for (i = 0; i < (CONFIG_IRQ_NUM + 31) / 32; i++)
        {
            PLIC->PLIC_H5_MIE[i] = 0;
            PLIC->PLIC_H5_SIE[i] = 0;
        }

        /* set hart threshold 0, enable all interrupt */
        PLIC->PLIC_H5_MTH = 0;
        PLIC->PLIC_H5_STH = 0;
    }
    else if (core_id == CRXX_MP_CORE6)
    {
        for (i = 0; i < (CONFIG_IRQ_NUM + 31) / 32; i++)
        {
            PLIC->PLIC_H6_MIE[i] = 0;
            PLIC->PLIC_H6_SIE[i] = 0;
        }

        /* set hart threshold 0, enable all interrupt */
        PLIC->PLIC_H6_MTH = 0;
        PLIC->PLIC_H6_STH = 0;
    }
    else if (core_id == CRXX_MP_CORE7)
    {
        for (i = 0; i < (CONFIG_IRQ_NUM + 31) / 32; i++)
        {
            PLIC->PLIC_H7_MIE[i] = 0;
            PLIC->PLIC_H7_SIE[i] = 0;
        }

        /* set hart threshold 0, enable all interrupt */
        PLIC->PLIC_H7_MTH = 0;
        PLIC->PLIC_H7_STH = 0;
    }
    else
    {
        rt_kprintf("Illegal CPU core, ID = %d", core_id);
        while (1)
            ;
    }

    /* enable MEIE & MSIE */
    uint32_t mie = __get_MIE();
    mie |= (1 << 11 | 1 << 7 | 1 << 3);
#if CONFIG_ECC_L1_ENABLE
    mie |= (1 << 16);
#endif
    __set_MIE(mie);
}

/**
 * @brief    Software interrupt handling function.
 *
 * @note     This function is used to respond to inter core interrupts.
 *
 * @param    NULL.
 *
 */
void ipi_irq(void)
{
    int core_id = 0;
    core_id = rt_hw_cpu_id();
    clint_ipi_clear(core_id);
    rt_schedule();
}