summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1-RevB_FreedomStudio/freedom-metal/src/drivers/sifive_clic0.c
blob: bf8fa16a0ce476eaf00dc8e9ae442bb1f159784f (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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/* Copyright 2018 SiFive, Inc */
/* SPDX-License-Identifier: Apache-2.0 */

#include <metal/machine/platform.h>

#ifdef METAL_SIFIVE_CLIC0

#include <metal/drivers/sifive_clic0.h>
#include <metal/io.h>
#include <metal/machine.h>
#include <metal/shutdown.h>
#include <stdint.h>

#define CLIC0_MAX_INTERRUPTS 4096

typedef enum metal_clic_vector_ {
    METAL_CLIC_NONVECTOR = 0,
    METAL_CLIC_VECTORED = 1
} metal_clic_vector;

struct __metal_clic_cfg {
    unsigned char : 1, nmbits : 2, nlbits : 4, nvbit : 1;
};

const struct __metal_clic_cfg __metal_clic_defaultcfg = {
    .nmbits = METAL_INTR_PRIV_M_MODE,
    .nlbits = 0,
    .nvbit = METAL_CLIC_NONVECTOR};

void __metal_clic0_handler(int id, void *priv) __attribute__((aligned(64)));

void __metal_clic0_default_vector_handler(void)
    __attribute__((interrupt, aligned(64)));

struct __metal_clic_cfg
__metal_clic0_configuration(struct __metal_driver_sifive_clic0 *clic,
                            struct __metal_clic_cfg *cfg) {
    volatile unsigned char val;
    struct __metal_clic_cfg cliccfg;
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);

    if (cfg) {
        val = cfg->nmbits << 5 | cfg->nlbits << 1 | cfg->nvbit;
        __METAL_ACCESS_ONCE(
            (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                              METAL_SIFIVE_CLIC0_CLICCFG)) = val;
    }
    val = __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICCFG));
    cliccfg.nmbits = (val & METAL_SIFIVE_CLIC0_CLICCFG_NMBITS_MASK) >> 5;
    cliccfg.nlbits = (val & METAL_SIFIVE_CLIC0_CLICCFG_NLBITS_MASK) >> 1;
    cliccfg.nvbit = val & METAL_SIFIVE_CLIC0_CLICCFG_NVBIT_MASK;
    return cliccfg;
}

int __metal_clic0_interrupt_set_mode(struct __metal_driver_sifive_clic0 *clic,
                                     int id, int mode) {
    uint8_t mask, val;
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);

    if (mode >= (cfg.nmbits << 1)) {
        /* Do nothing, mode request same or exceed what configured in CLIC */
        return 0;
    }

    /* Mask out nmbits and retain other values */
    mask = ((uint8_t)(-1)) >> cfg.nmbits;
    val =
        __METAL_ACCESS_ONCE(
            (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                              METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id)) &
        mask;
    __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id)) =
        val | (mode << (8 - cfg.nmbits));
    return 0;
}

int __metal_clic0_interrupt_set_level(struct __metal_driver_sifive_clic0 *clic,
                                      int id, unsigned int level) {

    uint8_t mask, nmmask, nlmask, val;
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);

    /* Drop the LSBs that don't fit in nlbits */
    nlmask = (uint8_t)(-1) >> (cfg.nmbits + cfg.nlbits);
    nmmask = ~((uint8_t)(-1) >> (cfg.nmbits));
    mask = ~(nlmask | nmmask);

    level &= mask;
    val = __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id));
    val &= ~mask;
    __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id)) =
        (val | level);

    return 0;
}

unsigned int
__metal_clic0_interrupt_get_level(struct __metal_driver_sifive_clic0 *clic,
                                  int id) {
    int level;
    uint8_t mask, val, freebits, nlbits;
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_intbits =
        __metal_driver_sifive_clic0_num_intbits((struct metal_interrupt *)clic);

    if ((cfg.nmbits + cfg.nlbits) >= num_intbits) {
        nlbits = (num_intbits >= cfg.nmbits) ? (num_intbits - cfg.nmbits) : 0;
    } else {
        nlbits = __METAL_MIN((num_intbits - cfg.nmbits), cfg.nlbits);
    }

    mask = ((1 << nlbits) - 1)
           << (METAL_CLIC_MAX_NLBITS - (cfg.nmbits + nlbits));
    freebits = ((1 << METAL_CLIC_MAX_NLBITS) - 1) >> nlbits;

    if (mask == 0) {
        level = (1 << METAL_CLIC_MAX_NLBITS) - 1;
    } else {
        val = __METAL_ACCESS_ONCE(
            (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                              METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id));
        val = __METAL_GET_FIELD(val, mask);
        level = (val << (METAL_CLIC_MAX_NLBITS - nlbits)) | freebits;
    }

    return level;
}

int __metal_clic0_interrupt_set_priority(
    struct __metal_driver_sifive_clic0 *clic, int id, int priority) {

    uint8_t mask, nlmask, val, npbits;
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_intbits =
        __metal_driver_sifive_clic0_num_intbits((struct metal_interrupt *)clic);

    if ((cfg.nmbits + cfg.nlbits) <= num_intbits) {
        npbits = num_intbits - (cfg.nmbits + cfg.nlbits);

        mask = (uint8_t)(-1) >> (cfg.nmbits + cfg.nlbits + npbits);
        nlmask = ~((uint8_t)(-1) >> (cfg.nmbits + cfg.nlbits));
        mask = ~(mask | nlmask);

        priority &= mask;
        val = __METAL_ACCESS_ONCE(
            (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                              METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id));
        val &= ~mask;
        __METAL_ACCESS_ONCE(
            (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                              METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id)) =
            (val | priority);
    }
    return 0;
}

int __metal_clic0_interrupt_get_priority(
    struct __metal_driver_sifive_clic0 *clic, int id) {
    int priority;
    uint8_t mask, val, freebits, nlbits;
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_intbits =
        __metal_driver_sifive_clic0_num_intbits((struct metal_interrupt *)clic);

    if ((cfg.nmbits + cfg.nlbits) >= num_intbits) {
        nlbits = num_intbits - cfg.nmbits;
    } else {
        nlbits = cfg.nlbits;
    }

    mask = ((1 << nlbits) - 1) << (8 - (cfg.nmbits + nlbits));
    freebits = ((1 << METAL_CLIC_MAX_NLBITS) - 1) >> nlbits;

    if (mask == 0) {
        priority = (1 << METAL_CLIC_MAX_NLBITS) - 1;
    } else {
        val = __METAL_ACCESS_ONCE(
            (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                              METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id));
        priority = __METAL_GET_FIELD(val, freebits);
    }
    return priority;
}

int __metal_clic0_interrupt_set_vector_mode(
    struct __metal_driver_sifive_clic0 *clic, int id, int enable) {
    uint8_t mask, val;
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_intbits =
        __metal_driver_sifive_clic0_num_intbits((struct metal_interrupt *)clic);

    mask = 1 << (8 - num_intbits);
    val = __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id));
    if (enable) {
        val |= mask;
    } else {
        val &= ~mask;
    }
    __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id)) = val;
    return 0;
}

int __metal_clic0_interrupt_is_vectored(
    struct __metal_driver_sifive_clic0 *clic, int id) {
    uint8_t mask, val;
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_intbits =
        __metal_driver_sifive_clic0_num_intbits((struct metal_interrupt *)clic);

    mask = 1 << (8 - num_intbits);
    val = __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id));
    return __METAL_GET_FIELD(val, mask);
}

int __metal_clic0_interrupt_enable(struct __metal_driver_sifive_clic0 *clic,
                                   int id) {
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(
        (struct metal_interrupt *)clic);

    __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTIE_BASE + id)) =
        METAL_ENABLE;
    return 0;
}

int __metal_clic0_interrupt_disable(struct __metal_driver_sifive_clic0 *clic,
                                    int id) {
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(
        (struct metal_interrupt *)clic);

    __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTIE_BASE + id)) =
        METAL_DISABLE;
    return 0;
}

int __metal_clic0_interrupt_is_enabled(struct __metal_driver_sifive_clic0 *clic,
                                       int id) {
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(
        (struct metal_interrupt *)clic);

    if (id >= num_subinterrupts) {
        return 0;
    }
    return __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTIE_BASE + id));
}

int __metal_clic0_interrupt_is_pending(struct __metal_driver_sifive_clic0 *clic,
                                       int id) {
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(
        (struct metal_interrupt *)clic);

    if (id >= num_subinterrupts) {
        return 0;
    }
    return __METAL_ACCESS_ONCE(
        (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                          METAL_SIFIVE_CLIC0_CLICINTIP_BASE + id));
}

int __metal_clic0_interrupt_set(struct __metal_driver_sifive_clic0 *clic,
                                int id) {
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(
        (struct metal_interrupt *)clic);

    if (id < num_subinterrupts) {
        __METAL_ACCESS_ONCE(
            (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                              METAL_SIFIVE_CLIC0_CLICINTIP_BASE + id)) =
            METAL_ENABLE;
    }
    return 0;
}

int __metal_clic0_interrupt_clear(struct __metal_driver_sifive_clic0 *clic,
                                  int id) {
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(
        (struct metal_interrupt *)clic);

    if (id < num_subinterrupts) {
        __METAL_ACCESS_ONCE(
            (__metal_io_u8 *)(control_base + METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                              METAL_SIFIVE_CLIC0_CLICINTIP_BASE + id)) =
            METAL_DISABLE;
    }
    return 0;
}

int __metal_clic0_configure_set_vector_mode(
    struct __metal_driver_sifive_clic0 *clic, metal_vector_mode mode) {
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);

    switch (mode) {
    case METAL_SELECTIVE_NONVECTOR_MODE:
        cfg.nvbit = METAL_CLIC_NONVECTOR;
        __metal_controller_interrupt_vector(mode, &clic->metal_mtvt_table);
        break;
    case METAL_SELECTIVE_VECTOR_MODE:
        cfg.nvbit = METAL_CLIC_VECTORED;
        __metal_controller_interrupt_vector(mode, &clic->metal_mtvt_table);
        break;
    case METAL_HARDWARE_VECTOR_MODE:
        cfg.nvbit = METAL_CLIC_VECTORED;
        __metal_controller_interrupt_vector(mode, &clic->metal_mtvt_table);
        break;
    default:
        return -1;
    }
    __metal_clic0_configuration(clic, &cfg);
    return 0;
}

metal_vector_mode __metal_clic0_configure_get_vector_mode(
    struct __metal_driver_sifive_clic0 *clic) {
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);
    metal_vector_mode mode = __metal_controller_interrupt_vector_mode();

    if (mode == METAL_SELECTIVE_VECTOR_MODE) {
        if (cfg.nvbit) {
            return METAL_SELECTIVE_VECTOR_MODE;
        } else {
            return METAL_SELECTIVE_NONVECTOR_MODE;
        }
    } else {
        return mode;
    }
}

int __metal_clic0_configure_set_privilege(
    struct __metal_driver_sifive_clic0 *clic, metal_intr_priv_mode priv) {
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);

    cfg.nmbits = priv;
    __metal_clic0_configuration(clic, &cfg);
    return 0;
}

metal_intr_priv_mode __metal_clic0_configure_get_privilege(
    struct __metal_driver_sifive_clic0 *clic) {
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);

    return cfg.nmbits;
}

int __metal_clic0_configure_set_level(struct __metal_driver_sifive_clic0 *clic,
                                      int level) {
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);

    cfg.nlbits = level & 0xF;
    __metal_clic0_configuration(clic, &cfg);
    return 0;
}

int __metal_clic0_configure_get_level(
    struct __metal_driver_sifive_clic0 *clic) {
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);

    return cfg.nlbits;
}

unsigned long long
__metal_clic0_mtime_get(struct __metal_driver_sifive_clic0 *clic) {
    __metal_io_u32 lo, hi;
    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);

    /* Guard against rollover when reading */
    do {
        hi = __METAL_ACCESS_ONCE(
            (__metal_io_u32 *)(control_base + METAL_SIFIVE_CLIC0_MTIME + 4));
        lo = __METAL_ACCESS_ONCE(
            (__metal_io_u32 *)(control_base + METAL_SIFIVE_CLIC0_MTIME));
    } while (__METAL_ACCESS_ONCE((__metal_io_u32 *)(control_base +
                                                    METAL_SIFIVE_CLIC0_MTIME +
                                                    4)) != hi);

    return (((unsigned long long)hi) << 32) | lo;
}

int __metal_driver_sifive_clic0_mtimecmp_set(struct metal_interrupt *controller,
                                             int hartid,
                                             unsigned long long time) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);

    unsigned long control_base = __metal_driver_sifive_clic0_control_base(
        (struct metal_interrupt *)clic);
    /* Per spec, the RISC-V MTIME/MTIMECMP registers are 64 bit,
     * and are NOT internally latched for multiword transfers.
     * Need to be careful about sequencing to avoid triggering
     * spurious interrupts: For that set the high word to a max
     * value first.
     */
    __METAL_ACCESS_ONCE((__metal_io_u32 *)(control_base + (8 * hartid) +
                                           METAL_SIFIVE_CLIC0_MTIMECMP_BASE +
                                           4)) = 0xFFFFFFFF;
    __METAL_ACCESS_ONCE((__metal_io_u32 *)(control_base + (8 * hartid) +
                                           METAL_SIFIVE_CLIC0_MTIMECMP_BASE)) =
        (__metal_io_u32)time;
    __METAL_ACCESS_ONCE((__metal_io_u32 *)(control_base + (8 * hartid) +
                                           METAL_SIFIVE_CLIC0_MTIMECMP_BASE +
                                           4)) = (__metal_io_u32)(time >> 32);
    return 0;
}

void __metal_clic0_handler(int id, void *priv) {
    struct __metal_driver_sifive_clic0 *clic = priv;
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(
        (struct metal_interrupt *)clic);

    if ((id < num_subinterrupts) && (clic->metal_exint_table[id].handler)) {
        clic->metal_exint_table[id].handler(
            id, clic->metal_exint_table[id].exint_data);
    }
}

void __metal_clic0_default_handler(int id, void *priv) { metal_shutdown(300); }

void __metal_clic0_default_vector_handler(void) { metal_shutdown(400); }

void __metal_driver_sifive_clic0_init(struct metal_interrupt *controller) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);

    if (!clic->init_done) {
        int level, max_levels, line, num_interrupts, num_subinterrupts;
        struct __metal_clic_cfg cfg = __metal_clic_defaultcfg;
        struct metal_interrupt *intc =
            __metal_driver_sifive_clic0_interrupt_parent(controller);

        /* Initialize ist parent controller, aka cpu_intc. */
        intc->vtable->interrupt_init(intc);
        __metal_controller_interrupt_vector(METAL_SELECTIVE_NONVECTOR_MODE,
                                            &clic->metal_mtvt_table);

        /*
         * Register its interrupts with with parent controller,
         * aka sw, timer and ext to its default isr
         */
        num_interrupts = __metal_driver_sifive_clic0_num_interrupts(controller);
        for (int i = 0; i < num_interrupts; i++) {
            line = __metal_driver_sifive_clic0_interrupt_lines(controller, i);
            intc->vtable->interrupt_register(intc, line, NULL, clic);
        }

        /* Default CLIC mode to per dts */
        max_levels = __metal_driver_sifive_clic0_max_levels(controller);
        cfg.nlbits = (max_levels > METAL_CLIC_MAX_NLBITS)
                         ? METAL_CLIC_MAX_NLBITS
                         : max_levels;
        __metal_clic0_configuration(clic, &cfg);

        level = (1 << cfg.nlbits) - 1;
        num_subinterrupts =
            __metal_driver_sifive_clic0_num_subinterrupts(controller);
        clic->metal_mtvt_table[0] = &__metal_clic0_handler;
        __metal_clic0_interrupt_disable(clic, 0);
        __metal_clic0_interrupt_set_level(clic, 0, level);
        for (int i = 1; i < CLIC0_MAX_INTERRUPTS; i++) {
            if (i < num_subinterrupts) {
                clic->metal_mtvt_table[i] = NULL;
                clic->metal_exint_table[i].handler = NULL;
                clic->metal_exint_table[i].sub_int = NULL;
                clic->metal_exint_table[i].exint_data = NULL;
                __metal_clic0_interrupt_set_level(clic, i, level);
            }
            __metal_clic0_interrupt_disable(clic, i);
        }
        clic->init_done = 1;
    }
}

int __metal_driver_sifive_clic0_register(struct metal_interrupt *controller,
                                         int id, metal_interrupt_handler_t isr,
                                         void *priv) {
    int rc = -1;
    int num_subinterrupts;
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    struct metal_interrupt *intc =
        __metal_driver_sifive_clic0_interrupt_parent(controller);
    metal_vector_mode mode = __metal_clic0_configure_get_vector_mode(clic);

    if (((mode == METAL_SELECTIVE_VECTOR_MODE) &&
         (__metal_clic0_interrupt_is_vectored(clic, id))) ||
        (mode == METAL_HARDWARE_VECTOR_MODE) || (mode == METAL_VECTOR_MODE) ||
        (mode == METAL_DIRECT_MODE)) {
        return rc;
    }

    /* Register its interrupts with parent controller */
    if (id < METAL_INTERRUPT_ID_CSW) {
        return intc->vtable->interrupt_register(intc, id, isr, priv);
    }

    /*
     * CLIC (sub-interrupts) devices interrupts start at 16 but offset from 0
     * Reset the IDs to reflects this.
     */
    num_subinterrupts =
        __metal_driver_sifive_clic0_num_subinterrupts(controller);
    if (id < num_subinterrupts) {
        if (isr) {
            clic->metal_exint_table[id].handler = isr;
            clic->metal_exint_table[id].exint_data = priv;
        } else {
            clic->metal_exint_table[id].handler = __metal_clic0_default_handler;
            clic->metal_exint_table[id].sub_int = priv;
        }
        rc = 0;
    }
    return rc;
}

int __metal_driver_sifive_clic0_vector_register(
    struct metal_interrupt *controller, int id,
    metal_interrupt_vector_handler_t isr, void *priv) {
    int rc = -1;
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    struct metal_interrupt *intc =
        __metal_driver_sifive_clic0_interrupt_parent(controller);
    int num_subinterrupts =
        __metal_driver_sifive_clic0_num_subinterrupts(controller);
    metal_vector_mode mode = __metal_clic0_configure_get_vector_mode(clic);

    if ((mode != METAL_SELECTIVE_VECTOR_MODE) &&
        (mode != METAL_HARDWARE_VECTOR_MODE)) {
        return rc;
    }
    if ((mode == METAL_SELECTIVE_VECTOR_MODE) &&
        (__metal_clic0_interrupt_is_vectored(clic, id) == 0)) {
        return rc;
    }
    if (id < num_subinterrupts) {
        if (isr) {
            clic->metal_mtvt_table[id] = isr;
            clic->metal_exint_table[id].exint_data = priv;
        } else {
            clic->metal_mtvt_table[id] = __metal_clic0_default_vector_handler;
            clic->metal_exint_table[id].sub_int = priv;
        }
        rc = 0;
    }
    return rc;
}

int __metal_driver_sifive_clic0_enable(struct metal_interrupt *controller,
                                       int id) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_interrupt_enable(clic, id);
}

int __metal_driver_sifive_clic0_disable(struct metal_interrupt *controller,
                                        int id) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_interrupt_disable(clic, id);
}

int __metal_driver_sifive_clic0_enable_interrupt_vector(
    struct metal_interrupt *controller, int id) {
    int rc = -1;
    int num_subinterrupts =
        __metal_driver_sifive_clic0_num_subinterrupts(controller);
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    metal_vector_mode mode = __metal_clic0_configure_get_vector_mode(clic);

    if ((mode != METAL_SELECTIVE_VECTOR_MODE) &&
        (mode != METAL_HARDWARE_VECTOR_MODE)) {
        return rc;
    }
    if (id < num_subinterrupts) {
        __metal_clic0_interrupt_set_vector_mode(clic, id, METAL_ENABLE);
        return 0;
    }
    return -1;
}

int __metal_driver_sifive_clic0_disable_interrupt_vector(
    struct metal_interrupt *controller, int id) {
    int num_subinterrupts;
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);

    num_subinterrupts =
        __metal_driver_sifive_clic0_num_subinterrupts(controller);
    if (id < num_subinterrupts) {
        __metal_clic0_interrupt_set_vector_mode(clic, id, METAL_DISABLE);
        return 0;
    }
    return -1;
}

metal_vector_mode __metal_driver_sifive_clic0_get_vector_mode(
    struct metal_interrupt *controller) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_configure_get_vector_mode(clic);
}

int __metal_driver_sifive_clic0_set_vector_mode(
    struct metal_interrupt *controller, metal_vector_mode mode) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_configure_set_vector_mode(clic, mode);
}

metal_intr_priv_mode
__metal_driver_sifive_clic0_get_privilege(struct metal_interrupt *controller) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_configure_get_privilege(clic);
}

int __metal_driver_sifive_clic0_set_privilege(
    struct metal_interrupt *controller, metal_intr_priv_mode priv) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_configure_set_privilege(clic, priv);
}

unsigned int
__metal_driver_sifive_clic0_get_threshold(struct metal_interrupt *controller) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_configure_get_level(clic);
}

int __metal_driver_sifive_clic0_set_threshold(
    struct metal_interrupt *controller, unsigned int level) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_configure_set_level(clic, level);
}

unsigned int
__metal_driver_sifive_clic0_get_priority(struct metal_interrupt *controller,
                                         int id) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_interrupt_get_priority(clic, id);
}

int __metal_driver_sifive_clic0_set_priority(struct metal_interrupt *controller,
                                             int id, unsigned int priority) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_interrupt_set_priority(clic, id, priority);
}

unsigned int __metal_driver_sifive_clic0_get_preemptive_level(
    struct metal_interrupt *controller, int id) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_interrupt_get_level(clic, id);
}

int __metal_driver_sifive_clic0_set_preemptive_level(
    struct metal_interrupt *controller, int id, unsigned int level) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    __metal_clic0_interrupt_set_level(clic, id, level);
    return (__metal_clic0_interrupt_set_priority(clic, id, level));
}

int __metal_driver_sifive_clic0_clear_interrupt(
    struct metal_interrupt *controller, int id) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_interrupt_clear(clic, id);
}

int __metal_driver_sifive_clic0_set_interrupt(
    struct metal_interrupt *controller, int id) {
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    return __metal_clic0_interrupt_set(clic, id);
}

int __metal_driver_sifive_clic0_command_request(
    struct metal_interrupt *controller, int command, void *data) {
    int hartid;
    int rc = -1;
    struct __metal_driver_sifive_clic0 *clic =
        (struct __metal_driver_sifive_clic0 *)(controller);
    unsigned long control_base =
        __metal_driver_sifive_clic0_control_base(controller);

    switch (command) {
    case METAL_TIMER_MTIME_GET:
        if (data) {
            *(unsigned long long *)data = __metal_clic0_mtime_get(clic);
            rc = 0;
        }
        break;
    case METAL_SOFTWARE_IPI_CLEAR:
        if (data) {
            hartid = *(int *)data;
            __METAL_ACCESS_ONCE((
                __metal_io_u32 *)(control_base + (hartid * 4))) = METAL_DISABLE;
            __METAL_ACCESS_ONCE(
                (__metal_io_u8 *)(control_base +
                                  METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                                  METAL_SIFIVE_CLIC0_CLICINTIP_BASE)) =
                METAL_DISABLE;
            rc = 0;
        }
        break;
    case METAL_SOFTWARE_IPI_SET:
        if (data) {
            hartid = *(int *)data;
            __METAL_ACCESS_ONCE(
                (__metal_io_u32 *)(control_base + (hartid * 4))) = METAL_ENABLE;
            __METAL_ACCESS_ONCE(
                (__metal_io_u8 *)(control_base +
                                  METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                                  METAL_SIFIVE_CLIC0_CLICINTIP_BASE)) =
                METAL_ENABLE;
            rc = 0;
        }
        break;
    case METAL_SOFTWARE_MSIP_GET:
        rc = 0;
        if (data) {
            hartid = *(int *)data;
            rc = __METAL_ACCESS_ONCE(
                (__metal_io_u32 *)(control_base + (hartid * 4)));
        }
        break;
    default:
        break;
    }

    return rc;
}
__METAL_DEFINE_VTABLE(__metal_driver_vtable_sifive_clic0) = {
    .clic_vtable.interrupt_init = __metal_driver_sifive_clic0_init,
    .clic_vtable.interrupt_register = __metal_driver_sifive_clic0_register,
    .clic_vtable.interrupt_vector_register =
        __metal_driver_sifive_clic0_vector_register,
    .clic_vtable.interrupt_enable = __metal_driver_sifive_clic0_enable,
    .clic_vtable.interrupt_disable = __metal_driver_sifive_clic0_disable,
    .clic_vtable.interrupt_vector_enable =
        __metal_driver_sifive_clic0_enable_interrupt_vector,
    .clic_vtable.interrupt_vector_disable =
        __metal_driver_sifive_clic0_disable_interrupt_vector,
    .clic_vtable.interrupt_get_vector_mode =
        __metal_driver_sifive_clic0_get_vector_mode,
    .clic_vtable.interrupt_set_vector_mode =
        __metal_driver_sifive_clic0_set_vector_mode,
    .clic_vtable.interrupt_get_privilege =
        __metal_driver_sifive_clic0_get_privilege,
    .clic_vtable.interrupt_set_privilege =
        __metal_driver_sifive_clic0_set_privilege,
    .clic_vtable.interrupt_get_threshold =
        __metal_driver_sifive_clic0_get_threshold,
    .clic_vtable.interrupt_set_threshold =
        __metal_driver_sifive_clic0_set_threshold,
    .clic_vtable.interrupt_get_priority =
        __metal_driver_sifive_clic0_get_priority,
    .clic_vtable.interrupt_set_priority =
        __metal_driver_sifive_clic0_set_priority,
    .clic_vtable.interrupt_get_preemptive_level =
        __metal_driver_sifive_clic0_get_preemptive_level,
    .clic_vtable.interrupt_set_preemptive_level =
        __metal_driver_sifive_clic0_set_preemptive_level,
    .clic_vtable.interrupt_clear = __metal_driver_sifive_clic0_clear_interrupt,
    .clic_vtable.interrupt_set = __metal_driver_sifive_clic0_set_interrupt,
    .clic_vtable.command_request = __metal_driver_sifive_clic0_command_request,
    .clic_vtable.mtimecmp_set = __metal_driver_sifive_clic0_mtimecmp_set,
};

#endif /* METAL_SIFIVE_CLIC0 */

typedef int no_empty_translation_units;