summaryrefslogtreecommitdiff
path: root/libcpu/risc-v/common/pic/pic-riscv-intc.c
blob: 238634d85c77945da6b24847ea6e9dbd6097d8d2 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-01-30     GuEe-GUI     first version
 */

#include <rthw.h>
#include <rtatomic.h>
#include <rtthread.h>
#ifndef PIC_DISABLE_DM
#include <rtdevice.h>
#endif

#define DBG_TAG "pic.riscv"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include <csr.h>
#include <sbi.h>
#include <cpu.h>
#include <isa_ext.h>
#include <riscv_timer.h>

#include "pic-riscv-intc-andes.h"
#include "pic-riscv-clint.h"
#ifdef RT_PIC_RISCV_AIA
#include "pic-riscv-imsic.h"
#endif

struct riscv_intc
{
#ifndef PIC_DISABLE_DM
    struct rt_pic parent;
#endif /* !PIC_DISABLE_DM */

    rt_uint32_t irqs_base;
    rt_uint32_t irqs_count;
    rt_uint32_t irqs_def_count;

#ifdef RT_USING_SMP
    rt_uint32_t ipi_base;
    rt_atomic_t ipi_enable[RT_CPUS_NR];
    rt_atomic_t ipi_pending[RT_CPUS_NR];
#endif
};

static struct riscv_intc system_rv_intc;

static rt_bool_t riscv_intc_use_aia(void)
{
#if defined(RT_PIC_RISCV_AIA) && !defined(PIC_DISABLE_DM)
    return riscv_isa_ext_test(SxAIA) && imsic_ofw_is_available();
#else
    return RT_FALSE;
#endif
}

rt_inline void riscv_intc_irq_mask_raw(struct riscv_intc *rv_intc, int hwirq)
{
#ifdef RT_USING_SMP
    if (hwirq >= rv_intc->ipi_base)
    {
        rt_atomic_t *ipi_enable = &rv_intc->ipi_enable[rt_hw_cpu_id()];

        rt_hw_atomic_and(ipi_enable, ~BIT(hwirq - rv_intc->ipi_base));

        if (rt_hw_atomic_load(ipi_enable) != 0)
        {
            return;
        }

        hwirq = RV_IRQ_SOFT;
    }
#endif /* RT_USING_SMP */

#if BITS_PER_LONG == 32
    if (hwirq >= BITS_PER_LONG)
    {
        csr_clear(CSR_IEH, BIT(hwirq - BITS_PER_LONG));
    }
    else
#endif /* BITS_PER_LONG == 32 */
    {
        csr_clear(CSR_IE, BIT(hwirq));
    }
}

rt_inline void riscv_intc_irq_unmask_raw(struct riscv_intc *rv_intc, int hwirq)
{
#ifdef RT_USING_SMP
    if (hwirq >= rv_intc->ipi_base)
    {
        rt_atomic_t *ipi_enable = &rv_intc->ipi_enable[rt_hw_cpu_id()];

        rt_hw_atomic_or(ipi_enable, BIT(hwirq - rv_intc->ipi_base));
        hwirq = RV_IRQ_SOFT;
    }
#endif /* RT_USING_SMP */

#if BITS_PER_LONG == 32
    if (hwirq >= BITS_PER_LONG)
    {
        csr_set(CSR_IEH, BIT(hwirq - BITS_PER_LONG));
    }
    else
#endif /* BITS_PER_LONG == 32 */
    {
        csr_set(CSR_IE, BIT(hwirq));
    }
}

rt_inline void andes_intc_irq_mask_raw(struct riscv_intc *rv_intc, int hwirq)
{
    /*
     * Andes specific S-mode local interrupt causes (hwirq)
     * are defined as (256 + n) and controlled by n-th bit of SLIE.
     */
    rt_uint32_t mask;

#ifdef RT_USING_SMP
    if (hwirq >= rv_intc->ipi_base)
    {
        rt_atomic_t *ipi_enable = &rv_intc->ipi_enable[rt_hw_cpu_id()];

        rt_hw_atomic_and(ipi_enable, ~BIT(hwirq - rv_intc->ipi_base));

        if (rt_hw_atomic_load(ipi_enable) != 0)
        {
            return;
        }

        hwirq = RV_IRQ_SOFT;
    }
#endif /* RT_USING_SMP */

    mask = BIT(hwirq % BITS_PER_LONG);

    if (hwirq < ANDES_SLI_CAUSE_BASE)
    {
        csr_clear(CSR_IE, mask);
    }
    else
    {
        csr_clear(ANDES_CSR_SLIE, mask);
    }
}

rt_inline void andes_intc_irq_unmask_raw(struct riscv_intc *rv_intc, int hwirq)
{
    rt_uint32_t mask;

#ifdef RT_USING_SMP
    if (hwirq >= rv_intc->ipi_base)
    {
        rt_atomic_t *ipi_enable = &rv_intc->ipi_enable[rt_hw_cpu_id()];

        rt_hw_atomic_or(ipi_enable, BIT(hwirq - rv_intc->ipi_base));
        hwirq = RV_IRQ_SOFT;
    }
#endif /* RT_USING_SMP */

    mask = BIT(hwirq % BITS_PER_LONG);

    if (hwirq < ANDES_SLI_CAUSE_BASE)
    {
        csr_set(CSR_IE, mask);
    }
    else
    {
        csr_set(ANDES_CSR_SLIE, mask);
    }
}

rt_inline void riscv_intc_irq_send_ipi_raw(struct riscv_intc *rv_intc, int ipi, rt_bitmap_t *cpumask)
{
#if defined(RT_USING_SMP) || defined(ARCH_RISCV_M_MODE)
    int cpu;

#ifdef RT_USING_SMP
    RT_ASSERT(ipi >= rv_intc->ipi_base && ipi < rv_intc->ipi_base + RT_MAX_IPI);
    ipi -= rv_intc->ipi_base;
#endif

    rt_bitmap_for_each_set_bit(cpumask, cpu, RT_CPUS_NR)
    {
        if (!riscv_cpu_id_available(cpu))
        {
            continue;
        }

    #ifdef RT_USING_SMP
        rt_hw_atomic_or(&rv_intc->ipi_pending[cpu], BIT(ipi));
    #endif

    #ifndef ARCH_RISCV_M_MODE
        if (riscv_aclint_ipi_base)
    #endif
        {
            clint_send_ipi(cpu);
        }
    }
#endif /* RT_USING_SMP || ARCH_RISCV_M_MODE */

#ifndef ARCH_RISCV_M_MODE
    if (!riscv_aclint_ipi_base)
    {
        sbi_send_ipi((const unsigned long *)cpumask);
    }
#endif /* !ARCH_RISCV_M_MODE */
}

rt_inline rt_bool_t riscv_intc_common_handler_raw(struct riscv_intc *rv_intc, int hwirq,
        rt_bool_t (*callback)(int hwirq, void *data), void *data)
{
#ifdef RT_USING_SMP
    if (hwirq != RV_IRQ_SOFT)
#endif
    {
        if (hwirq != RV_IRQ_TIMER)
        {
            return callback(hwirq, data);
        }
        else
        {
            riscv_timer_isr();
            return RT_TRUE;
        }
    }
#ifdef RT_USING_SMP
    else
    {
        int ipi;
        rt_bool_t res = RT_TRUE;
        rt_bitmap_t pending;

    #ifndef ARCH_RISCV_M_MODE
        if (riscv_aclint_ipi_base)
    #endif
        {
            clint_clear_ipi();
        }
    #ifndef ARCH_RISCV_M_MODE
        else
        {
            csr_clear(CSR_IP, IE_SIE);
        }
    #endif

        pending = rt_hw_atomic_exchange(&rv_intc->ipi_pending[rt_hw_cpu_id()], 0);

        rt_bitmap_for_each_set_bit(&pending, ipi, RT_MAX_IPI)
        {
            res = callback(rv_intc->ipi_base + ipi, data) || res;
        }

        return res;
    }
#endif /* RT_USING_SMP */
}

rt_inline rt_bool_t riscv_intc_handler_raw(struct riscv_intc *rv_intc, rt_ubase_t scause,
        rt_bool_t (*callback)(int hwirq, void *data), void *data)
{
    return riscv_intc_common_handler_raw(rv_intc, scause & ~CAUSE_IRQ_FLAG, callback, data);
}

rt_inline rt_bool_t riscv_intc_aia_handler_raw(struct riscv_intc *rv_intc,
        rt_bool_t (*callback)(int hwirq, void *data), void *data)
{
    rt_ubase_t topi;
    rt_bool_t res = RT_FALSE;

    while ((topi = csr_read(CSR_TOPI) >> TOPI_IID_SHIFT))
    {
        res = riscv_intc_common_handler_raw(rv_intc, topi, callback, data) || res;
    }

    return res;
}

static void riscv_intc_common_init(struct riscv_intc *rv_intc, rt_bool_t is_andes)
{
    rt_memset(rv_intc, 0, sizeof(*rv_intc));
    rv_intc->irqs_def_count = RT_BITS_PER_LONG;

    if (is_andes)
    {
        rv_intc->irqs_base = ANDES_SLI_CAUSE_BASE;
        rv_intc->irqs_count = ANDES_RV_IRQ_LAST;
    }
    else
    {
        rv_intc->irqs_base = BITS_PER_LONG;
        rv_intc->irqs_count = 0;
    }

    if (riscv_intc_use_aia())
    {
        rv_intc->irqs_def_count = 64;
    }
    else
    {
        rv_intc->irqs_def_count = IRQ_LOCAL_MAX;
    }

#ifdef RT_USING_SMP
    rv_intc->irqs_def_count += RT_MAX_IPI;
#endif
}

#ifndef PIC_DISABLE_DM
static void riscv_intc_irq_mask(struct rt_pic_irq *pirq)
{
    RT_IRQ_AFFINITY_CLEAR(pirq->affinity, rt_hw_cpu_id());
    riscv_intc_irq_mask_raw(pirq->pic->priv_data, pirq->hwirq);
}

static void riscv_intc_irq_unmask(struct rt_pic_irq *pirq)
{
    RT_IRQ_AFFINITY_SET(pirq->affinity, rt_hw_cpu_id());
    riscv_intc_irq_unmask_raw(pirq->pic->priv_data, pirq->hwirq);
}

static void andes_intc_irq_mask(struct rt_pic_irq *pirq)
{
    andes_intc_irq_mask_raw(pirq->pic->priv_data, pirq->hwirq);
    RT_IRQ_AFFINITY_CLEAR(pirq->affinity, rt_hw_cpu_id());
}

static void andes_intc_irq_unmask(struct rt_pic_irq *pirq)
{
    RT_IRQ_AFFINITY_SET(pirq->affinity, rt_hw_cpu_id());
    andes_intc_irq_unmask_raw(pirq->pic->priv_data, pirq->hwirq);
}

static void riscv_intc_irq_send_ipi(struct rt_pic_irq *pirq, rt_bitmap_t *cpumask)
{
    struct riscv_intc *rv_intc = pirq->pic->priv_data;

    riscv_intc_irq_send_ipi_raw(rv_intc, pirq->hwirq, cpumask);
}

static int riscv_intc_irq_map(struct rt_pic *pic, int hwirq, rt_uint32_t mode)
{
    int irq, irq_index = -1;
    struct riscv_intc *rv_intc = rt_container_of(pic, struct riscv_intc, parent);

    if (hwirq >= rv_intc->irqs_def_count &&
        (hwirq < rv_intc->irqs_base || hwirq >= rv_intc->irqs_base + rv_intc->irqs_count))
    {
        return -RT_EINVAL;
    }

    if (hwirq < rv_intc->irqs_def_count)
    {
        irq_index = hwirq;
    }
    else if (hwirq >= rv_intc->irqs_base &&
            hwirq < rv_intc->irqs_base + rv_intc->irqs_count)
    {
        irq_index = rv_intc->irqs_def_count + hwirq - rv_intc->irqs_base;
    }

    if (irq_index >= 0)
    {
        irq = rt_pic_config_irq(pic, hwirq, irq_index);
    }
    else
    {
        irq = -1;
    }

    return irq;
}

static rt_err_t riscv_intc_irq_parse(struct rt_pic *pic,
        struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq)
{
    rt_err_t err = RT_EOK;

    if (args->args_count == 1)
    {
        out_pirq->hwirq = args->args[0];
        out_pirq->mode = RT_IRQ_MODE_EDGE_RISING;
    }
    else
    {
        err = -RT_EINVAL;
    }

    return err;
}

const static struct rt_pic_ops riscv_intc_ops =
{
    .name = "RISC-V-INTC",
    .irq_mask = riscv_intc_irq_mask,
    .irq_unmask = riscv_intc_irq_unmask,
    .irq_send_ipi = riscv_intc_irq_send_ipi,
    .irq_map = riscv_intc_irq_map,
    .irq_parse = riscv_intc_irq_parse,
};

const static struct rt_pic_ops andes_intc_ops =
{
    .name = "ANDES-INTC",
    .irq_mask = andes_intc_irq_mask,
    .irq_unmask = andes_intc_irq_unmask,
    .irq_send_ipi = riscv_intc_irq_send_ipi,
    .irq_map = riscv_intc_irq_map,
    .irq_parse = riscv_intc_irq_parse,
};

static rt_bool_t riscv_intc_common_handler_isr(int hwirq, void *data)
{
    struct rt_pic_irq *pirq;
    struct riscv_intc *rv_intc = data;

#ifdef RT_USING_SMP
    if (hwirq < rv_intc->ipi_base)
#endif
    {
        pirq = rt_pic_find_irq(&rv_intc->parent, hwirq);
    }
#ifdef RT_USING_SMP
    else
    {
        pirq = rt_pic_find_ipi(&rv_intc->parent, hwirq - rv_intc->ipi_base);
    }
#endif

    return rt_pic_handle_isr(pirq) == RT_EOK;
}

static rt_bool_t riscv_intc_handler(void *data)
{
    rt_ubase_t cause;
    struct riscv_intc *rv_intc = data;

    cause = csr_read(CSR_CAUSE);

    return riscv_intc_handler_raw(rv_intc, cause, riscv_intc_common_handler_isr, data);
}

static rt_bool_t riscv_intc_aia_handler(void *data)
{
    struct riscv_intc *rv_intc = data;
    rt_ubase_t cause;

    cause = csr_read(CSR_CAUSE) & ~CAUSE_IRQ_FLAG;
    if (cause == RV_IRQ_SOFT)
    {
        return riscv_intc_common_handler_raw(rv_intc, cause,
                riscv_intc_common_handler_isr, data);
    }

    return riscv_intc_aia_handler_raw(rv_intc, riscv_intc_common_handler_isr, data);
}

static rt_err_t riscv_intc_ofw_init(struct rt_ofw_node *np, const struct rt_ofw_node_id *id)
{
    const struct rt_pic_ops *ops;
    struct riscv_intc *rv_intc = &system_rv_intc;
    struct rt_ofw_node *pnp = rt_ofw_get_parent(np);

    /* Only boot CPU to init */
    if (rt_ofw_get_cpu_hwid(pnp, 0) != boot_cpu_hartid)
    {
        rt_ofw_node_put(pnp);
        rt_ofw_data(np) = &rv_intc->parent;

        return RT_EOK;
    }
    rt_ofw_node_put(pnp);

    ops = id->data;
    riscv_intc_common_init(rv_intc, ops == &andes_intc_ops);

    riscv_clint_ofw_init();

    if (riscv_intc_use_aia())
    {
        rt_pic_add_traps(riscv_intc_aia_handler, rv_intc);
    }
    else
    {
        rt_pic_add_traps(riscv_intc_handler, rv_intc);
    }

    rv_intc->parent.priv_data = rv_intc;
    rv_intc->parent.ops = ops;
    rt_ofw_data(np) = &rv_intc->parent;

    rt_pic_linear_irq(&rv_intc->parent, rv_intc->irqs_def_count + rv_intc->irqs_count);

#ifdef RT_USING_SMP
    rv_intc->ipi_base = rv_intc->parent.irq_nr - RT_MAX_IPI;

    for (int ipi = 0; ipi < RT_MAX_IPI; ++ipi)
    {
        rt_pic_config_ipi(&rv_intc->parent, ipi, rv_intc->ipi_base + ipi);
    }
#endif /* RT_USING_SMP */

    rt_pic_user_extends(&rv_intc->parent);

    return RT_EOK;
}

static const struct rt_ofw_node_id riscv_intc_ofw_ids[] =
{
    { .compatible = "riscv,cpu-intc", (void *)&riscv_intc_ops },
    { .compatible = "andestech,cpu-intc", (void *)&andes_intc_ops },
    { /* sentinel */ }
};
RT_PIC_OFW_DECLARE(riscv_intc, riscv_intc_ofw_ids, riscv_intc_ofw_init);
#endif /* !PIC_DISABLE_DM */