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
522
523
524
525
526
527
528
|
/*
* 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 <rtthread.h>
#ifndef PIC_DISABLE_DM
#include <rtdevice.h>
#endif
#define DBG_TAG "pic.plic"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include <csr.h>
#include <cpu.h>
#include <cache.h>
#include <bitmap.h>
/*
* This driver implements a version of the RISC-V PLIC with the actual layout
* specified in chapter 8 of the SiFive U5 Coreplex Series Manual:
*
* https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
*/
#define MAX_DEVICES 1024
#define MAX_CONTEXTS 15872
/*
* Each interrupt source has a priority register associated with it.
* We always hardwire it to one in Linux.
*/
#define PRIORITY_BASE 0
#define PRIORITY_PER_ID 4
#ifdef ARCH_RISCV_M_MODE
#define CONTEXT_ENABLE_MODE_OFFSET 0
#define CONTEXT_BASE_MODE_OFFSET 0
#else
#define CONTEXT_ENABLE_MODE_OFFSET 0x80
#define CONTEXT_BASE_MODE_OFFSET 0x1000
#endif
/*
* Each hart context has a vector of interrupt enable bits associated with it.
* There's one bit for each interrupt source.
*/
#define CONTEXT_ENABLE_BASE (0x2000 + CONTEXT_ENABLE_MODE_OFFSET)
#define CONTEXT_ENABLE_SIZE(hartid) ((2 * hartid) * 0x80)
/*
* Each hart context has a set of control registers associated with it. Right
* now there's only two: a source priority threshold over which the hart will
* take an interrupt, and a register to claim interrupts.
*/
#define CONTEXT_BASE (0x200000 + CONTEXT_BASE_MODE_OFFSET)
#define CONTEXT_SIZE(hartid) ((2 * hartid) * 0x1000)
#define CONTEXT_THRESHOLD 0x00
#define CONTEXT_CLAIM 0x04
#define PLIC_DISABLE_THRESHOLD 0x7
#define PLIC_ENABLE_THRESHOLD 0
#define PLIC_HWIRQ_START 1
#define PLIC_QUIRK_EDGE_INTERRUPT 0
struct plic
{
#ifndef PIC_DISABLE_DM
struct rt_pic parent;
struct rt_ofw_node *np;
int parent_irq;
#endif
void *regs;
rt_bitmap_t quirks;
rt_uint32_t irqs_count;
};
struct plic_percpu
{
void *hart_base;
void *enable_base;
rt_bool_t valid;
struct rt_spinlock enable_lock;
};
static struct plic_percpu plic_percpu[RT_CPUS_NR] rt_align(L1_CACHE_BYTES);
static void plic_set_threshold_raw(struct plic_percpu *percpu, rt_uint32_t threshold)
{
if (!percpu->valid)
{
return;
}
/* Priority must be > threshold to trigger an interrupt */
HWREG32(percpu->hart_base + CONTEXT_THRESHOLD) = threshold;
}
static void plic_toggle(struct plic_percpu *percpu, int hwirq, rt_bool_t enable)
{
rt_ubase_t level;
rt_uint32_t *reg, hwirq_mask;
if (!percpu->valid)
{
return;
}
level = rt_spin_lock_irqsave(&percpu->enable_lock);
#ifdef SOC_SPACEMIT_K1
if (!enable)
{
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
}
#endif
reg = percpu->enable_base + (hwirq / 32) * sizeof(rt_uint32_t);
hwirq_mask = 1 << (hwirq % 32);
if (enable)
{
HWREG32(reg) |= hwirq_mask;
}
else
{
HWREG32(reg) &= ~hwirq_mask;
}
#ifdef SOC_SPACEMIT_K1
if (enable)
{
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
}
#endif
rt_spin_unlock_irqrestore(&percpu->enable_lock, level);
}
static void plic_irq_unmask_raw(int cpu, int hwirq)
{
struct plic_percpu *percpu = &plic_percpu[cpu];
plic_toggle(percpu, hwirq, RT_TRUE);
}
#if !defined(SOC_SPACEMIT_K1) || defined(PIC_DISABLE_DM)
static void plic_irq_mask_raw(int cpu, int hwirq)
{
struct plic_percpu *percpu = &plic_percpu[cpu];
plic_toggle(percpu, hwirq, RT_FALSE);
}
#endif
static void plic_irq_ack_or_eoi(int cpu, int hwirq)
{
struct plic_percpu *percpu = &plic_percpu[cpu];
if (percpu->valid)
{
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
}
}
static rt_bool_t plic_handler_isr_raw(struct plic *plic,
rt_bool_t (*callback)(int hwirq, void *data), void *data)
{
int hwirq;
void *claim;
rt_bool_t res = RT_FALSE;
struct plic_percpu *percpu = &plic_percpu[rt_hw_cpu_id()];
claim = percpu->hart_base + CONTEXT_CLAIM;
while ((hwirq = HWREG32(claim)))
{
res = callback(hwirq, data) || res;
}
return res;
}
static void plic_percpu_common_init(struct plic *plic)
{
struct plic_percpu *percpu = &plic_percpu[rt_hw_cpu_id()];
plic_set_threshold_raw(percpu, PLIC_ENABLE_THRESHOLD);
}
static struct plic *plic_common_init(rt_ubase_t plic_base, rt_size_t plic_size,
rt_size_t ndev, rt_bitmap_t quirks)
{
#ifdef PIC_DISABLE_DM
static struct plic non_dm_plic;
struct plic *plic = &non_dm_plic;
rt_memset(plic, 0, sizeof(*plic));
#else
struct plic *plic = rt_calloc(1, sizeof(*plic));
if (!plic)
{
rt_set_errno(-RT_ENOMEM);
return RT_NULL;
}
#endif
plic->regs = rt_ioremap((void *)plic_base, plic_size);
if (!plic->regs)
{
rt_set_errno(-RT_EIO);
#ifndef PIC_DISABLE_DM
rt_free(plic);
#endif
return RT_NULL;
}
plic->quirks = quirks;
plic->irqs_count = ndev;
for (int cpu = 0, hartid; cpu < RT_CPUS_NR; ++cpu)
{
struct plic_percpu *percpu = &plic_percpu[cpu];
#ifndef RT_USING_SMP
hartid = boot_cpu_hartid;
#else
if (!riscv_cpu_id_available(cpu))
{
continue;
}
hartid = riscv_cpu_id_to_hartid(cpu);
#endif
percpu->hart_base = plic->regs + CONTEXT_BASE + CONTEXT_SIZE(hartid);
percpu->enable_base = plic->regs + CONTEXT_ENABLE_BASE + CONTEXT_ENABLE_SIZE(hartid);
percpu->valid = RT_TRUE;
rt_spin_lock_init(&percpu->enable_lock);
}
/* Set default priority */
for (int hwirq = PLIC_HWIRQ_START; hwirq <= plic->irqs_count; ++hwirq)
{
HWREG32(plic->regs + PRIORITY_BASE + hwirq * PRIORITY_PER_ID) = PLIC_ENABLE_THRESHOLD + 1;
}
return plic;
}
#ifndef PIC_DISABLE_DM
static void plic_handler(int hwirq, void *data);
static rt_err_t plic_irq_init(struct rt_pic *pic)
{
struct plic *plic = pic->priv_data;
if (plic->parent_irq < 0)
{
struct rt_ofw_cell_args irq_args;
if (rt_ofw_parse_irq_cells(plic->np, 0, &irq_args))
{
LOG_E("%s: Parent IRQ is not available", rt_ofw_node_full_name(plic->np));
goto _percpu_init;
}
/* Create mapping */
irq_args.args[0] = RV_IRQ_EXT;
plic->parent_irq = rt_ofw_map_irq(&irq_args);
rt_hw_interrupt_install(plic->parent_irq, plic_handler, plic, "PLIC");
}
rt_hw_interrupt_umask(plic->parent_irq);
_percpu_init:
plic_percpu_common_init(plic);
return RT_EOK;
}
static void plic_irq_ack(struct rt_pic_irq *pirq)
{
if (pirq->mode == RT_IRQ_MODE_EDGE_RISING)
{
plic_irq_ack_or_eoi(rt_hw_cpu_id(), pirq->hwirq);
}
}
static void plic_irq_unmask(struct rt_pic_irq *pirq)
{
int cpu, count = 0;
#ifdef SOC_SPACEMIT_K1
struct plic *plic = pirq->pic->priv_data;
#endif
rt_bitmap_for_each_set_bit(pirq->affinity, cpu, RT_CPUS_NR)
{
plic_irq_unmask_raw(cpu, pirq->hwirq);
++count;
}
if (!count)
{
int boot_cpu_id = riscv_hartid_to_cpu_id(boot_cpu_hartid);
RT_IRQ_AFFINITY_SET(pirq->affinity, boot_cpu_id);
plic_irq_unmask_raw(boot_cpu_id, pirq->hwirq);
}
#ifdef SOC_SPACEMIT_K1
HWREG32(plic->regs + PRIORITY_BASE + pirq->hwirq * PRIORITY_PER_ID) =
PLIC_ENABLE_THRESHOLD + 1;
#endif
}
static void plic_irq_mask(struct rt_pic_irq *pirq)
{
#ifdef SOC_SPACEMIT_K1
struct plic *plic = pirq->pic->priv_data;
/* K1 masks a live source by priority; keep its context enable bit set. */
HWREG32(plic->regs + PRIORITY_BASE + pirq->hwirq * PRIORITY_PER_ID) =
PLIC_ENABLE_THRESHOLD;
#else
int cpu;
rt_bitmap_for_each_set_bit(pirq->affinity, cpu, RT_CPUS_NR)
{
plic_irq_mask_raw(cpu, pirq->hwirq);
}
#endif
}
static void plic_irq_eoi(struct rt_pic_irq *pirq)
{
if (pirq->mode == RT_IRQ_MODE_LEVEL_HIGH)
{
plic_irq_ack_or_eoi(rt_hw_cpu_id(), pirq->hwirq);
}
}
static rt_err_t plic_irq_set_priority(struct rt_pic_irq *pirq, rt_uint32_t priority)
{
struct plic *plic = pirq->pic->priv_data;
priority = rt_min_t(rt_uint32_t, priority, PLIC_DISABLE_THRESHOLD);
HWREG32(plic->regs + PRIORITY_BASE + pirq->hwirq * PRIORITY_PER_ID) = priority;
return RT_EOK;
}
static rt_err_t plic_irq_set_affinity(struct rt_pic_irq *pirq, rt_bitmap_t *affinity)
{
int cpu, count = 0;
rt_bitmap_for_each_clear_bit(affinity, cpu, RT_CPUS_NR)
{
plic_toggle(&plic_percpu[cpu], pirq->hwirq, RT_FALSE);
}
rt_bitmap_for_each_set_bit(affinity, cpu, RT_CPUS_NR)
{
if (!plic_percpu[cpu].valid)
{
continue;
}
plic_toggle(&plic_percpu[cpu], pirq->hwirq, RT_TRUE);
++count;
}
return count ? RT_EOK : -RT_EINVAL;
}
static rt_err_t plic_irq_set_triger_mode(struct rt_pic_irq *pirq, rt_uint32_t mode)
{
struct plic *plic = pirq->pic->priv_data;
if (rt_bitmap_test_bit(&plic->quirks, PLIC_QUIRK_EDGE_INTERRUPT))
{
return -RT_ENOSYS;
}
if (mode == RT_IRQ_MODE_EDGE_RISING || mode == RT_IRQ_MODE_LEVEL_HIGH)
{
return RT_EOK;
}
return -RT_ENOSYS;
}
static int plic_irq_map(struct rt_pic *pic, int hwirq, rt_uint32_t mode)
{
struct rt_pic_irq *pirq = rt_pic_find_irq(pic, hwirq - PLIC_HWIRQ_START);
pirq->mode = mode;
return rt_pic_config_irq(pic, hwirq - PLIC_HWIRQ_START, hwirq);
}
static rt_err_t plic_irq_parse(struct rt_pic *pic, struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq)
{
struct plic *plic = pic->priv_data;
out_pirq->hwirq = args->args[0];
if (rt_bitmap_test_bit(&plic->quirks, PLIC_QUIRK_EDGE_INTERRUPT))
{
out_pirq->mode = args->args[1];
}
else
{
out_pirq->mode = RT_IRQ_MODE_LEVEL_HIGH;
}
return RT_EOK;
}
const static struct rt_pic_ops plic_ops =
{
.name = "SiFive-PLIC",
.irq_init = plic_irq_init,
.irq_ack = plic_irq_ack,
.irq_mask = plic_irq_mask,
.irq_unmask = plic_irq_unmask,
.irq_eoi = plic_irq_eoi,
.irq_set_priority = plic_irq_set_priority,
.irq_set_affinity = plic_irq_set_affinity,
.irq_set_triger_mode = plic_irq_set_triger_mode,
.irq_map = plic_irq_map,
.irq_parse = plic_irq_parse,
};
static rt_bool_t plic_common_handler_isr(int hwirq, void *data)
{
rt_bool_t res;
struct plic *plic = data;
struct rt_pic_irq *pirq = rt_pic_find_irq(&plic->parent, hwirq - PLIC_HWIRQ_START);
plic_irq_ack(pirq);
res = rt_pic_handle_isr(pirq) == RT_EOK;
plic_irq_eoi(pirq);
return res;
}
static void plic_handler(int hwirq, void *data)
{
struct plic *plic = data;
plic_handler_isr_raw(plic, plic_common_handler_isr, plic);
}
static rt_err_t plic_ofw_init(struct rt_ofw_node *np, const struct rt_ofw_node_id *id)
{
rt_err_t err;
rt_uint32_t irqs_count;
rt_uint64_t plic_base, plic_size;
rt_bitmap_t quirks;
struct plic *plic;
if ((err = rt_ofw_get_address(np, 0, &plic_base, &plic_size)))
{
return err;
}
if ((err = rt_ofw_prop_read_u32(np, "riscv,ndev", &irqs_count)))
{
return err;
}
quirks = id->data ? *(rt_ubase_t *)id->data : 0;
if (!(plic = plic_common_init(plic_base, plic_size, irqs_count, quirks)))
{
return rt_get_errno();
}
plic->np = np;
plic->parent_irq = -1;
plic->parent.priv_data = plic;
plic->parent.ops = &plic_ops;
rt_ofw_data(np) = &plic->parent;
rt_pic_linear_irq(&plic->parent, plic->irqs_count);
rt_pic_user_extends(&plic->parent);
return RT_EOK;
}
static rt_bitmap_t andestech_nceplic100_quirk_edge = RT_BIT(PLIC_QUIRK_EDGE_INTERRUPT);
static rt_bitmap_t thead_c900_quirk_edge = RT_BIT(PLIC_QUIRK_EDGE_INTERRUPT);
static const struct rt_ofw_node_id plic_ofw_ids[] =
{
{ .compatible = "sifive,plic-1.0.0" },
{ .compatible = "riscv,plic0" },
{ .compatible = "andestech,nceplic100", .data = &andestech_nceplic100_quirk_edge },
{ .compatible = "thead,c900-plic", .data = &thead_c900_quirk_edge },
{ /* sentinel */ }
};
RT_PIC_OFW_DECLARE(plic, plic_ofw_ids, plic_ofw_init);
#endif /* PIC_DISABLE_DM */
|