summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/freedom-metal/src/drivers/sifive_clic0.c
blob: 3f213847dea3d7459914fe7318bdec24529fe6d1 (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
/* Copyright 2018 SiFive, Inc */
/* SPDX-License-Identifier: Apache-2.0 */

#include <metal/machine/platform.h>

#ifdef METAL_SIFIVE_CLIC0

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

typedef enum metal_priv_mode_ {
    METAL_PRIV_M_MODE = 0,
    METAL_PRIV_MU_MODE = 1,
    METAL_PRIV_MSU_MODE = 2
} metal_priv_mode;

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_PRIV_M_MODE,
                .nlbits = 0,
                .nvbit = METAL_CLIC_NONVECTOR
            };

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;
    uintptr_t hartid = __metal_myhart_id();
    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, 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 */
    level = level >> (METAL_CLIC_MAX_NLBITS - cfg.nlbits);

    nmmask = ~( ((uint8_t)(-1)) >> (cfg.nmbits) );
    nlmask = ((uint8_t)(-1)) >> (cfg.nmbits + cfg.nlbits);
    mask = ~(nlmask | nmmask);
  
    val = __METAL_ACCESS_ONCE((__metal_io_u8 *)(control_base +
                                       METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                                       METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id));
    __METAL_ACCESS_ONCE((__metal_io_u8 *)(control_base +
                                 METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                                 METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id)) = __METAL_SET_FIELD(val, mask, level);
    return 0;
}

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;
    } else {
        nlbits = cfg.nlbits;
    }

    mask = ((1 << nlbits) - 1) << (8 - (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, npmask, 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);
        priority = priority >> (8 - npbits);

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

        val = __METAL_ACCESS_ONCE((__metal_io_u8 *)(control_base +
                                       METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                                       METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id));
        __METAL_ACCESS_ONCE((__metal_io_u8 *)(control_base +
                                     METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                                     METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id)) = __METAL_SET_FIELD(val, mask, 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 (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));
    /* Ensure its value is 1 bit wide */
    enable &= 0x1;
    __METAL_ACCESS_ONCE((__metal_io_u8 *)(control_base +
                                 METAL_SIFIVE_CLIC0_MMODE_APERTURE +
                                 METAL_SIFIVE_CLIC0_CLICINTCTL_BASE + id)) = __METAL_SET_FIELD(val, mask, enable);
    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);

    if (id >= num_subinterrupts) {
        return -1;
    }
    __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);

    if (id >= num_subinterrupts) {
        return -1;
    }
    __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)
{       
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts((struct metal_interrupt *)clic);

    if ((id >= METAL_INTERRUPT_ID_LC0) && (id < num_subinterrupts)) {
    }
    return 0;
}

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

    if ((id >= METAL_INTERRUPT_ID_LC0) && (id < num_subinterrupts)) {
    }
    return 0;
}

void __metal_clic0_configure_privilege (struct __metal_driver_sifive_clic0 *clic, metal_priv_mode priv)
{
    struct __metal_clic_cfg cfg = __metal_clic0_configuration(clic, NULL);

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

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

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

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) __attribute__((aligned(64)));
void __metal_clic0_handler (int id, void *priv)
{
    int idx;
    struct __metal_driver_sifive_clic0 *clic = priv;
    int num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts((struct metal_interrupt *)clic);

    idx = id - METAL_INTERRUPT_ID_LC0;
    if ( (idx < num_subinterrupts) && (clic->metal_mtvt_table[idx]) ) {
        clic->metal_mtvt_table[idx](id, clic->metal_exint_table[idx].exint_data);
    }
}

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

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_VECTOR_MODE,
                                          &__metal_clic0_handler);

        /*
         * 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);
        for (int i = 0; i < num_subinterrupts; i++) {
            clic->metal_mtvt_table[i] = NULL;
            clic->metal_exint_table[i].sub_int = NULL;
            clic->metal_exint_table[i].exint_data = NULL;
            __metal_clic0_interrupt_disable(clic, i);
            __metal_clic0_interrupt_set_level(clic, i, level);
        }
	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);

    /* Register its interrupts with parent controller */
    if ( id < METAL_INTERRUPT_ID_LC0) {
        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. 
     */
    id -= METAL_INTERRUPT_ID_LC0;
    num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(controller);
    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_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, metal_vector_mode mode)
{
    int num_subinterrupts;
    struct __metal_driver_sifive_clic0 *clic =
                              (struct __metal_driver_sifive_clic0 *)(controller);

    if (id == METAL_INTERRUPT_ID_BASE) {
        if (mode == METAL_SELECTIVE_VECTOR_MODE) {
            __metal_controller_interrupt_vector(mode, &__metal_clic0_handler);
            return 0;
        }
        if (mode == METAL_HARDWARE_VECTOR_MODE) {
            __metal_controller_interrupt_vector(mode, &clic->metal_mtvt_table);
            return 0;
        }
    }
    num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(controller);
    if ((id >= METAL_INTERRUPT_ID_LC0) && (id < num_subinterrupts)) {
        if ((mode == METAL_SELECTIVE_VECTOR_MODE) &&
             __metal_controller_interrupt_is_selective_vectored()) {
            __metal_clic0_interrupt_set_vector(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);

    if (id == METAL_INTERRUPT_ID_BASE) {
        __metal_controller_interrupt_vector(METAL_SELECTIVE_VECTOR_MODE, &__metal_clic0_handler);
        return 0;
    }
    num_subinterrupts = __metal_driver_sifive_clic0_num_subinterrupts(controller);
    if ((id >= METAL_INTERRUPT_ID_LC0) && (id < num_subinterrupts)) {
        if  (__metal_controller_interrupt_is_selective_vectored()) {
            __metal_clic0_interrupt_set_vector(clic, id, METAL_DISABLE);
            return 0;
        }
    }
    return -1;
}

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_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.command_request    = __metal_driver_sifive_clic0_command_request,
    .clic_vtable.mtimecmp_set       = __metal_driver_sifive_clic0_mtimecmp_set,
};

#endif /* METAL_SIFIVE_CLIC0 */