summaryrefslogtreecommitdiff
path: root/ports/risc-v64/gnu/example_build/bananapi-f3/csr.h
blob: c5dfc6308199d4589ac73cdde9f26cd50e29c562 (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
/***************************************************************************
 * Copyright (c) 2026 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
 **************************************************************************/

/* RISC-V S-mode CSR helpers
 *
 * Privilege level:  Supervisor (S-mode).
 * Reference:        RISC-V Privileged Specification
 *
 * Bit positions per the RISC-V Privileged Specification:
 *
 *   sstatus:  SIE(1)  SPIE(5)  SPP(8)  FS(13:14)  SUM(18)  MXR(19)
 *   sie:      SSIE(1) STIE(5)  SEIE(9)
 *   scause:   Interrupt bit = 63;  Code 1=SSI, 5=STI, 9=SEI
 */

#ifndef RISCV_CSR_H
#define RISCV_CSR_H

#define SSTATUS_SIE             (1L << 1)    /* Supervisor Interrupt Enable     */
#define SSTATUS_SPIE            (1L << 5)    /* Previous SIE (saved on trap)    */
#define SSTATUS_SPP_MASK        (1L << 8)
#define SSTATUS_SPP_S           (1L << 8)    /* SPP = Supervisor                */
#define SSTATUS_SPP_U           (0L << 8)    /* SPP = User                      */
#define SSTATUS_FS              (3L << 13)   /* FP unit state (Off/Init/Clean/Dirty) */

#define SIE_SSIE                (1L << 1)    /* S-mode software interrupt       */
#define SIE_STIE                (1L << 5)    /* S-mode timer interrupt          */
#define SIE_SEIE                (1L << 9)    /* S-mode external interrupt       */

#ifndef __ASSEMBLER__

#include <stdint.h>

/*
 * Return the hart ID of the running core.
 *
 * mhartid is an M-mode CSR and cannot be read from S-mode.  When booted
 * from U-Boot only hart 0 is active (secondary harts remain parked in
 * OpenSBI HSM), so we return 0.  An SMP extension would need to pass
 * the hart ID through a0 or shared memory at boot.
 */
static inline uint64_t riscv_get_core(void)
{
    return 0;
}


static inline uint64_t riscv_read_sstatus(void)
{
    uint64_t x;
    asm volatile("csrr %0, sstatus" : "=r" (x));
    return x;
}

static inline void riscv_write_sstatus(uint64_t x)
{
    asm volatile("csrw sstatus, %0" : : "r" (x));
}


static inline void riscv_sintr_on(void)
{
    riscv_write_sstatus(riscv_read_sstatus() | SSTATUS_SIE);
}

static inline void riscv_sintr_off(void)
{
    riscv_write_sstatus(riscv_read_sstatus() & ~SSTATUS_SIE);
}

static inline int riscv_sintr_get(void)
{
    return (riscv_read_sstatus() & SSTATUS_SIE) != 0;
}

static inline void riscv_sintr_restore(int enabled)
{
    if (enabled)
        riscv_sintr_on();
    else
        riscv_sintr_off();
}

/* Unified names used by BSP drivers (uart.c, etc.). */
#define riscv_intr_on       riscv_sintr_on
#define riscv_intr_off      riscv_sintr_off
#define riscv_intr_get      riscv_sintr_get
#define riscv_intr_restore  riscv_sintr_restore

#endif /* __ASSEMBLER__ */
#endif /* RISCV_CSR_H */