summaryrefslogtreecommitdiff
path: root/ports/risc-v64/gnu/example_build/bananapi-f3/plic.c
blob: 6b04b65b8f728d4a9837ffbb94ddca289bdd81cb (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
/***************************************************************************
 * Copyright (c) 2025 10xEngineers
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

#include "plic.h"
#include <stddef.h>

static irq_callback callbacks[MAX_CALLBACK_NUM];

#define PLIC_ENABLE(hart)    PLIC_SENABLE(hart)
#define PLIC_PRIORITY_REG(hart) PLIC_SPRIORITY(hart)
#define PLIC_CLAIM_REG(hart) PLIC_SCLAIM(hart)
#define PLIC_COMPLETE_REG(hart) PLIC_SCOMPLETE(hart)

void plic_irq_enable(int irqno)
{
    int hart = (int)riscv_get_core();
    uint32_t word = irqno / 32;
    uint32_t bit  = irqno % 32;
    volatile uint32_t *en = (volatile uint32_t *)(PLIC_ENABLE(hart) + word * 4);
    *en |= (1u << bit);
}

void plic_irq_disable(int irqno)
{
    int hart = (int)riscv_get_core();
    uint32_t word = irqno / 32;
    uint32_t bit  = irqno % 32;
    volatile uint32_t *en = (volatile uint32_t *)(PLIC_ENABLE(hart) + word * 4);
    *en &= ~(1u << bit);
}

void plic_prio_set(int irqno, int prio)
{
    PLIC_SET_PRIO(irqno, prio);
}

int plic_prio_get(int irqno)
{
    return PLIC_GET_PRIO(irqno);
}

int plic_register_callback(int irqno, irq_callback callback)
{
    if (!(irqno >= 0 && irqno < MAX_CALLBACK_NUM))
        return -1;
    callbacks[irqno] = callback;
    return 0;
}

int plic_unregister_callback(int irqno)
{
    return plic_register_callback(irqno, NULL);
}

int plic_init(void)
{
    int hart = (int)riscv_get_core();

    for (int i = 0; i < MAX_CALLBACK_NUM; i++)
        callbacks[i] = NULL;

    /* Mask everything for this hart. */
    for (int word = 0; word < (MAX_CALLBACK_NUM + 31) / 32; word++)
        *(volatile uint32_t *)(PLIC_ENABLE(hart) + word * 4) = 0;

    /* Set hart threshold to 0 so any non-zero priority IRQ can fire. */
    *(volatile uint32_t *)PLIC_PRIORITY_REG(hart) = 0;

    /*
     * Drain stale pending interrupts left over from a prior boot stage
     * (BootROM / OpenSBI / U-Boot).  We temporarily enable every source
     * so claim returns the actual highest-priority pending ID, then
     * complete whatever was claimed.  Loop until claim returns 0
     * (no more pending).  This follows the PLIC spec: claim returns 0
     * when nothing is pending for this context.
     */
    for (int word = 0; word < (PLIC_NUM_SOURCES + 31) / 32; word++)
        *(volatile uint32_t *)(PLIC_ENABLE(hart) + word * 4) = 0xFFFFFFFFu;

    for (;;) {
        uint32_t id = *(volatile uint32_t *)PLIC_CLAIM_REG(hart);
        if (id == 0)
            break;
        *(volatile uint32_t *)PLIC_COMPLETE_REG(hart) = id;
    }

    /* Re-mask everything; individual drivers will enable their sources. */
    for (int word = 0; word < (PLIC_NUM_SOURCES + 31) / 32; word++)
        *(volatile uint32_t *)(PLIC_ENABLE(hart) + word * 4) = 0;

    /*
     * Set default priority for every source to PLIC_DEFAULT_PRIORITY (2),
     * Priority 0 means "never pending" per the SiFive PLIC spec, so any 
     * source that should be active must have priority >= 1.  Individual 
     * drivers may override this with plic_prio_set() later.
     */
    for (int i = 1; i <= PLIC_NUM_SOURCES; i++)
        PLIC_SET_PRIO(i, PLIC_DEFAULT_PRIORITY);

    return 0;
}

int plic_claim(void)
{
    int hart = (int)riscv_get_core();
    return (int)*(volatile uint32_t *)PLIC_CLAIM_REG(hart);
}

void plic_complete(int irqno)
{
    int hart = (int)riscv_get_core();
    *(volatile uint32_t *)PLIC_COMPLETE_REG(hart) = (uint32_t)irqno;
}

int plic_irq_intr(void)
{
    int ret = -1;
    int irqno = plic_claim();
    if (irqno == 0)
        return ret;
    if (irqno < MAX_CALLBACK_NUM && callbacks[irqno] != NULL)
        ret = (callbacks[irqno])(irqno);
    plic_complete(irqno);
    return ret;
}