Software APIs
Data Structures | Macros | Typedefs | Enumerations | Functions
ibex.h File Reference

(c534d13)

This header provides Ibex-specific functions and enums, such as cycle-accurate busy loops. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "sw/device/lib/arch/device.h"
#include "sw/device/lib/base/macros.h"
#include "sw/device/lib/base/math.h"
#include "sw/device/lib/base/stdasm.h"

Go to the source code of this file.

Data Structures

struct  ibex_timeout
 A spinwait timeout type. More...
 

Macros

#define IBEX_SPIN_FOR(expr, timeout_usec)
 Convenience macro to spin with timeout in microseconds. More...
 
#define IBEX_TRY_SPIN_FOR(expr, timeout_usec)
 Convenience macro to spin with timeout in microseconds. More...
 

Typedefs

typedef enum ibex_exc ibex_exc_t
 An Ibex exception type. More...
 
typedef enum ibex_internal_irq ibex_internal_irq_t
 An Ibex internal IRQ type. More...
 
typedef struct ibex_timeout ibex_timeout_t
 A spinwait timeout type.
 

Enumerations

enum  ibex_exc {
  kIbexExcInstrMisaligned = 0,
  kIbexExcInstrAccessFault = 1,
  kIbexExcIllegalInstrFault = 2,
  kIbexExcBreakpoint = 3,
  kIbexExcLoadAccessFault = 5,
  kIbexExcStoreAccessFault = 7,
  kIbexExcUserECall = 8,
  kIbexExcMachineECall = 11,
  kIbexExcMax = 31
}
 An Ibex exception type. More...
 
enum  ibex_internal_irq {
  kIbexInternalIrqLoadInteg = 0xffffffe0,
  kIbexInternalIrqNmi = 0x8000001f
}
 An Ibex internal IRQ type. More...
 

Functions

OT_WARN_UNUSED_RESULT uint64_t ibex_mcycle_read (void)
 Read the cycle counter. More...
 
OT_WARN_UNUSED_RESULT uint32_t ibex_mcause_read (void)
 Reads the mcause register. More...
 
OT_WARN_UNUSED_RESULT uint32_t ibex_mtval_read (void)
 Reads the mtval register. More...
 
OT_WARN_UNUSED_RESULT uint32_t ibex_mepc_read (void)
 Reads the mepc register. More...
 
void ibex_mepc_write (uint32_t mepc)
 Writes the mepc register. More...
 
OT_WARN_UNUSED_RESULT ibex_timeout_t ibex_timeout_init (uint32_t timeout_usec)
 Initializes the ibex timeout based on current mcycle count. More...
 
OT_WARN_UNUSED_RESULT bool ibex_timeout_check (const ibex_timeout_t *timeout)
 Check whether the timeout has expired. More...
 
OT_WARN_UNUSED_RESULT uint64_t ibex_timeout_elapsed (const ibex_timeout_t *timeout)
 Returns the time elapsed in microseconds since ibex_timeout_init was called. More...
 

Detailed Description

This header provides Ibex-specific functions and enums, such as cycle-accurate busy loops.

Definition in file ibex.h.


Data Structure Documentation

◆ ibex_timeout

struct ibex_timeout

A spinwait timeout type.

Definition at line 56 of file ibex.h.

Data Fields
uint64_t cycles The number of cycles to timeout.
uint64_t start The initial cycle count.

Macro Definition Documentation

◆ IBEX_SPIN_FOR

#define IBEX_SPIN_FOR (   expr,
  timeout_usec 
)
Value:
do { \
const ibex_timeout_t timeout_ = ibex_timeout_init(timeout_usec); \
while (!(expr)) { \
CHECK(!ibex_timeout_check(&timeout_), \
"Timed out after %d usec (%d CPU cycles) waiting for " #expr, \
(uint32_t)timeout_usec, (uint32_t)timeout_.cycles); \
} \
} while (0)

Convenience macro to spin with timeout in microseconds.

Parameters
exprAn expression that is evaluated multiple times until true.
timeout_usecTimeout in microseconds.

Definition at line 215 of file ibex.h.

◆ IBEX_TRY_SPIN_FOR

#define IBEX_TRY_SPIN_FOR (   expr,
  timeout_usec 
)
Value:
do { \
const ibex_timeout_t timeout_ = ibex_timeout_init(timeout_usec); \
while (!(expr)) { \
if (ibex_timeout_check(&timeout_)) { \
return DEADLINE_EXCEEDED(); \
} \
} \
} while (0)

Convenience macro to spin with timeout in microseconds.

Parameters
exprAn expression that is evaluated multiple times until true.
timeout_usecTimeout in microseconds.
Returns
kDeadlineExceeded in case of timeout.

Definition at line 232 of file ibex.h.

Typedef Documentation

◆ ibex_exc_t

typedef enum ibex_exc ibex_exc_t

An Ibex exception type.

This enum is used to decode RISC-V exception causes generated by Ibex.

◆ ibex_internal_irq_t

An Ibex internal IRQ type.

This enum is used to decode RISC-V internal IRQs generated by Ibex.

Enumeration Type Documentation

◆ ibex_exc

enum ibex_exc

An Ibex exception type.

This enum is used to decode RISC-V exception causes generated by Ibex.

Definition at line 31 of file ibex.h.

◆ ibex_internal_irq

An Ibex internal IRQ type.

This enum is used to decode RISC-V internal IRQs generated by Ibex.

Definition at line 48 of file ibex.h.

Function Documentation

◆ ibex_mcause_read()

OT_WARN_UNUSED_RESULT uint32_t ibex_mcause_read ( void  )

Reads the mcause register.

When an exception is encountered, the corresponding exception code is stored in mcause register.

A list of the exception codes can be found at: https://ibex-core.readthedocs.io/en/latest/03_reference/ exception_interrupts.html::exceptions

Definition at line 9 of file ibex.c.

◆ ibex_mcycle_read()

OT_WARN_UNUSED_RESULT uint64_t ibex_mcycle_read ( void  )
inline

Read the cycle counter.

The value of the counter is stored across two 32-bit registers: mcycle and mcycleh. This function is guaranteed to return a valid 64-bit cycle counter value, even if mcycle overflows before reading mcycleh.

Adapted from: The RISC-V Instruction Set Manual, Volume I: Unprivileged ISA V20191213, pp. 61.

Definition at line 78 of file ibex.h.

◆ ibex_mepc_read()

OT_WARN_UNUSED_RESULT uint32_t ibex_mepc_read ( void  )

Reads the mepc register.

When an exception is encountered, the current program counter is saved in mepc, and the core jumps to the exception address. When an MRET instruction is executed, the value from mepc replaces the current program counter.

From the Ibex documentation (found at https://ibex-core.readthedocs.io/en/latest/03_reference/cs_registers.html)

Please note that in case of a fault, mepc must be modified to hold the address of the next instruction, which can be at the 2byte (16bit) or 4byte (32bit) offset, dependent on the fault cause instruction type (standard or compressed).

Returns
The mepc register value.

Definition at line 21 of file ibex.c.

◆ ibex_mepc_write()

void ibex_mepc_write ( uint32_t  mepc)

Writes the mepc register.

When an exception is encountered, the current program counter is saved in mepc, and the core jumps to the exception address. When an MRET instruction is executed, the value from mepc replaces the current program counter.

From the Ibex documentation (found at https://ibex-core.readthedocs.io/en/latest/03_reference/cs_registers.html)

Please note that in case of a fault, mepc must be modified to hold the address of the next instruction, which can be at the 2byte (16bit) or 4byte (32bit) offset, dependent on the fault cause instruction type (standard or compressed).

Parameters
Thenew value to be written to the mepc register.

Definition at line 27 of file ibex.c.

◆ ibex_mtval_read()

OT_WARN_UNUSED_RESULT uint32_t ibex_mtval_read ( void  )

Reads the mtval register.

When an exception is encountered, the Machine Trap Value (mtval) register can holds exception-specific information to assist software in handling the trap.

From the Ibex documentation (found at https://ibex-core.readthedocs.io/en/latest/03_reference/cs_registers.html)

  • In the case of errors in the load-store unit mtval holds the address of the transaction causing the error.
  • If a transaction is misaligned, mtval holds the address of the missing transaction part.
  • In the case of illegal instruction exceptions, mtval holds the actual faulting instruction.
  • For all other exceptions, mtval is 0.

Definition at line 15 of file ibex.c.

◆ ibex_timeout_check()

OT_WARN_UNUSED_RESULT bool ibex_timeout_check ( const ibex_timeout_t timeout)
inline

Check whether the timeout has expired.

Parameters
timeoutHolds the counter start value.
Returns
True if the timeout has expired and false otherwise.

Definition at line 190 of file ibex.h.

◆ ibex_timeout_elapsed()

OT_WARN_UNUSED_RESULT uint64_t ibex_timeout_elapsed ( const ibex_timeout_t timeout)
inline

Returns the time elapsed in microseconds since ibex_timeout_init was called.

Parameters
timeoutHolds the counter start value..
Returns
Time elapsed in microseconds.

Definition at line 202 of file ibex.h.

◆ ibex_timeout_init()

OT_WARN_UNUSED_RESULT ibex_timeout_t ibex_timeout_init ( uint32_t  timeout_usec)
inline

Initializes the ibex timeout based on current mcycle count.

Parameters
timeout_usecTimeout in microseconds.
Returns
The initialized timeout value.

Definition at line 176 of file ibex.h.