Five EmbedDev logo Five EmbedDev

An Embedded RISC-V Blog

Controllers

The standard interrupt controller is the machine level interrupt handling done by the core. This is very limited and leaves much to be defined by the platform intergrator. This quick reference deals with that controller. The other available controllers are:

RISC-V Reset and NMI

These vectors are implementation defined.

RISC-V Interrupts

These are the interrupts that are defined by the base ISA.

Assertion and Service

Vectored or Direct

Vectored or direct interrupts are both possible.

External Interrupts

The exception/interrupt handling seems to be designed around a few root interrupt sources, with external multiplexing of platform interrupts.

Machine

The entry procedure for a interrupt I into machine mode:

The exit procedure for a machine interrupt when mret is executed.

Name mip,
mcause,
Exception_Code
mtvec
Mode==Vectored
Base + Offset
Mode Description Condition
msi 3 0x0000000c m Machine Software Interrupt mip.msip = Memory mapped control registers allow this bit to be written to assert a software interrupt. (It is used to provide a software interrupt from other harts).
mti 7 0x0000001c m Machine Timer Interrupt mip.mtip = mtime >= mtimecmp (mtimecmp needs to be written to clear the pending bit).
mei 11 0x0000002c m Machine External Interrupt mip.meip = External interrupt signal asserted
ssi 1 0x00000004 s Supervisor Software Interrupt mip.ssi = Implementation defined.
sti 5 0x00000014 s Supervisor Timer Interrupt mip.sti = Implementation defined.
sei 9 0x00000024 s Supervisor External Interrupt mip.sei = Implementation defined.
usi 0 0x00000000 u User Software Interrupt mip.usi = Implementation defined.
uti 4 0x00000010 u User Timer Interrupt mip.uti = Implementation defined.
uei 8 0x00000020 u User External Interrupt mip.uei = Implementation defined.
platform_defined 16+ 0x00000040 m Optional platform defined interrupt sources. mip.platform_defined = Implementation defined.

Supervisor

Supervisor-level interrupts require support of the s mode privilege. The mideleg register needs to set by m mode to enable s mode to take the interrupts.

Supervisor mode CSRs matching machine mode CSRs are used to implement supervisor interrupts and exceptions. Some are a restricted view to the machine mode CSRs, with dedicated bits for supervisor status, while others are replicated to dedicated registers.

Restricted View CSRs sstatus, sip, sie
Replicated CSRs scause, sepc, stvec, sscratch

The entry procedure for a interrupt I into supervisor mode (Assuming mideleg[I] is set):

The exit procedure for a supervisor interrupt when sret is executed.

Supervisor interrupts assuming mideleg.ssi, mideleg.sti, mideleg.sei are set:

Name sip,
scause,
Exception_Code
stvec
Mode==Vectored
Base + Offset
Mode Description Condition
ssi 1 0x00000004 s Supervisor Software Interrupt sip.ssip = Implementation dependent, CSR is written by software on the local hart to assert a software interrupt.
sti 5 0x00000014 s Supervisor Timer Interrupt sip.stip = Implementation dependent, Written my 'm' mode timer interrupt handler, an SEE call needs to be made to clear this).
sei 9 0x00000024 s Supervisor External Interrupt sip.seip = Implementation dependent, External interrupt signal asserted

User

User-level interrupts are optional for implementation. They rely on the n extension. This specification is not yet complete.

User interrupts assuming sideleg.usi, sideleg.uti, sideleg.uei are set:

Name uip,
ucause,
Exception_Code
utvec
Mode==Vectored
Base + Offset
Mode Description Condition
usi 0 0x00000000 u User Software Interrupt
uti 4 0x00000010 u User Timer Interrupt
uei 8 0x00000020 u User External Interrupt

Available for Platform Use

mcause.exception_code values greater than 16 are available for platform use, allowing use if bits 16..mxlen-1 in mip and mie. The platform can define a fixed priority scheme. These can be used to implement a fast vectored interrupt scheme.

RISC-V Synchronous Exceptions

These are the exceptions that are defined by the base ISA.

The entry procedure for an exception E into machine mode:

The exit procedure using mret is the same as for interrupts.

As for interrupts exception E can be taken in supervisor or user mode if available and configured via medeleg or sedeleg. The exception needs to be generated in the same or lower privilege level.

Delegation to Lower Privilege Mode

There are two methods to delegate exception E to lower privilege modes:

  1. Enter in ‘m’ mode. Write mstatus.mpp = lower privilege mode. Execute mret
  2. Configure medeleg[E]. The exception E will be taken in s mode when it occurs in s mode or lower privilege. (NOT when it occurs in m mode.)

RISC-V Scratch Registers

Scratch CSRs are available for each privilege trap level.

Interrupt Handlers In C

For GCC RISC-V Function Attributes are used to mark a function as an interrupt handler. These are also supported by LLVM

void f (void) __attribute__ ((interrupt ("user")));

As a parameter user, supervisor, and machine can be specified as the type. (To select the correct return instruction).