summaryrefslogtreecommitdiff
path: root/src/drivers/plic.cc
blob: a13537c702a177c469f3e41f3ce24c66ae8d5851 (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
#include "common/types.h"
#include "kernel/riscv.h"
#include "drivers/plic.h"

// QEMU Virt PLIC address constants
constexpr uint64 PLIC_BASE = 0x0c000000L;
constexpr uint64 PLIC_PRIORITY = PLIC_BASE + 0x0;
constexpr uint64 PLIC_PENDING = PLIC_BASE + 0x1000;

// S-Mode interrupt enable register: each Hart occupies 0x100 bytes
// Context 1 is Hart 0 S-Mode, Context 3 is Hart 1 S-Mode...
inline uint64 PLIC_SENABLE(int hart)
{
    return PLIC_BASE + 0x2080 + static_cast<uint64>(hart) * 0x100;
}

// S-Mode priority threshold and claim register: 0x2000 bytes per Hart
inline uint64 PLIC_STHRESHOLD(int hart)
{
    return PLIC_BASE + 0x201000 + static_cast<uint64>(hart) * 0x2000;
}

inline uint64 PLIC_SCLAIM(int hart)
{
    return PLIC_BASE + 0x201004 + static_cast<uint64>(hart) * 0x2000;
}

namespace PILC
{
    void init()
    {
        *reinterpret_cast<uint32 *>(PLIC_PRIORITY + 10 * 4) = 1; // UART
        *reinterpret_cast<uint32 *>(PLIC_PRIORITY + 1 * 4) = 1; // VirtIO 0
    }

    void inithart()
    {
        int hart = static_cast<int>(r_tp()); // 获取当前 hartid

        // Enable UART and VirtIO interrupts for the current Hart's S-Mode
        // Set bit 10 and bit 1 in the register bitmap
        *reinterpret_cast<uint32 *>(PLIC_SENABLE(hart)) = (1 << 10) | (1 << 1);

        // Set the current Hart's S-Mode priority threshold to 0
        // Only interrupts with a priority higher than this threshold will be delivered
        *reinterpret_cast<uint32 *>(PLIC_STHRESHOLD(hart)) = 0;
    }

    int claim()
    {
        int hart = static_cast<int>(r_tp());
        return *reinterpret_cast<uint32 *>(PLIC_SCLAIM(hart));
    }

    void complete(int irq)
    {
        int hart = static_cast<int>(r_tp());
        *reinterpret_cast<uint32 *>(PLIC_SCLAIM(hart)) = static_cast<uint32>(irq);
    }
} // namespace PILC