Software APIs
ibex.h
1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5#ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_IBEX_H_
6#define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_IBEX_H_
7
8#include <stddef.h>
9#include <stdint.h>
10
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19/**
20 * Get the FPGA version value from the USR_ACCESS register.
21 *
22 * @return FPGA version.
23 */
25uint32_t ibex_fpga_version(void);
26
27/**
28 * Get the number of address remapper slots.
29 *
30 * @return Number of remapper slots.
31 */
33size_t ibex_addr_remap_slots(void);
34
35#ifdef OT_PLATFORM_RV32
36/**
37 * Set the MCYCLE counter register to zero.
38 */
39inline void ibex_mcycle_zero(void) {
40 CSR_WRITE(CSR_REG_MCYCLE, 0);
41 CSR_WRITE(CSR_REG_MCYCLEH, 0);
42}
43
44/**
45 * Read the low 32 bits of the MCYCLE counter.
46 */
48inline uint32_t ibex_mcycle32(void) {
49 uint32_t val;
50 CSR_READ(CSR_REG_MCYCLE, &val);
51 return val;
52}
53
54/**
55 * Read the 64-bit MCYCLE counter.
56 */
58inline uint64_t ibex_mcycle(void) {
59 uint32_t lo, hi, hi2;
60 do {
61 CSR_READ(CSR_REG_MCYCLEH, &hi);
62 CSR_READ(CSR_REG_MCYCLE, &lo);
63 CSR_READ(CSR_REG_MCYCLEH, &hi2);
64 } while (hi != hi2);
65 return ((uint64_t)hi << 32) | lo;
66}
67
68/**
69 * Convert from microseconds to CPU cycles.
70 */
71inline uint64_t ibex_time_to_cycles(uint64_t time_us) {
72 return to_cpu_cycles(time_us);
73}
74#else
75extern void ibex_mcycle_zero(void);
76extern uint32_t ibex_mcycle32(void);
77extern uint64_t ibex_mcycle(void);
78extern uint64_t ibex_time_to_cycles(uint64_t time_us);
79#endif
80
81/**
82 * An Ibex exception type for silicon_creator code.
83 *
84 * This enum is used to decode RISC-V exception causes generated by Ibex that
85 * are handled in silicon_creator code.
86 */
87typedef enum ibex_exception_code {
88 kIbexExceptionCodeLoadAccessFault = 5,
89 kIbexExceptionCodeMax = 31,
90} ibex_exception_code_t;
91
92/**
93 * The following constants represent the expected number of sec_mmio register
94 * writes performed by functions in provided in this module. See
95 * `SEC_MMIO_WRITE_INCREMENT()` for more details.
96 *
97 * Example:
98 * ```
99 * ibex_addr_remap_0_set(...);
100 * SEC_MMIO_WRITE_INCREMENT(kAddressTranslationSecMmioConfigure);
101 * ```
102 */
103enum {
104 kAddressTranslationSecMmioConfigure = 6,
105};
106
107/**
108 * Configure the instruction and data bus in an address translation slot.
109 *
110 * @param slot Index of the address translation slot to configure.
111 * @param matching_addr When an incoming transaction matches the matching
112 * region, it is redirected to the new address. If a transaction does not match,
113 * then it is directly passed through.
114 * @param remap_addr The region where the matched transtaction will be
115 * redirected to.
116 * @param size The size of the regions mapped.
117 */
118void ibex_addr_remap_set(size_t slot, uint32_t matching_addr,
119 uint32_t remap_addr, size_t size);
120
121/**
122 * Get the remap target address.
123 *
124 * Returns zero if the address translation slot is not in use.
125 *
126 * @param slot Index of the addresss translation slot.
127 * @return The remap target address.
128 */
129uint32_t ibex_addr_remap_get(size_t slot);
130
131/**
132 * Lock a addresss translation so that it cannot be reprogrammed.
133 * This function locks the given IBUS and DBUS addresss translations
134 * simultaneously.
135 *
136 * @param slot Index of the addresss translation slot to lock.
137 */
138void ibex_addr_remap_lockdown(size_t slot);
139
140/**
141 * Verify that an address translation slot is enabled.
142 * The slot is considered enabled if both IBUS and DBUS are remapped for this
143 * slot index.
144 *
145 * @param slot Index of the address translation slot to check.
146 * @return True if the slot is enabled, false otherwise.
147 */
149bool ibex_addr_remap_is_enabled(size_t slot);
150
151/**
152 * Verify that an address translation slot remaps an address region to another
153 * one.
154 *
155 * This function checks that the region defined by (matching_addr, size) is
156 * redirected to remap_addr by the current address translation slot
157 * configuration.
158 *
159 * Both IBUS and DBUS address translations are checked but this function does
160 * not check if the slot is enabled.
161 *
162 * @param slot Index of the address translation slot to check.
163 * @param matching_addr Start address for the region to match for incoming
164 * transactions.
165 * @param remap_addr Start address for the region where the matched transaction
166 * will be redirected to.
167 * @param size The size of the regions mapped.
168 * @return True if the slot is matching expectations, false otherwise.
169 */
171bool ibex_addr_remap_verify(size_t slot, uint32_t matching_addr,
172 uint32_t remap_addr, size_t size);
173
174typedef enum ibex_nmi_source {
175 /**
176 * NMI alert handler.
177 */
178 kIbexNmiSourceAlert = 1 << 0,
179 /**
180 * NMI watchdog bark.
181 */
182 kIbexNmiSourceWdog = 1 << 1,
183 /**
184 * All NMIs.
185 */
186 kIbexNmiSourceAll = 0x3,
187} ibex_nmi_source_t;
188
189/**
190 * Enables the specified NMI sources.
191 *
192 * @param nmi_src The NMI source(s) to enable.
193 */
194void ibex_enable_nmi(ibex_nmi_source_t nmi_src);
195
196/**
197 * Clears the specified NMI sources.
198 *
199 * @param nmi_src The NMI source(s) to clear.
200 */
201void ibex_clear_nmi(ibex_nmi_source_t nmi_src);
202
203#ifdef __cplusplus
204}
205#endif
206#endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_IBEX_H_