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 
12 #include "sw/device/lib/base/csr.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * Get the FPGA version value from the USR_ACCESS register.
21  *
22  * @return FPGA version.
23  */
25 uint32_t ibex_fpga_version(void);
26 
27 #ifdef OT_PLATFORM_RV32
28 /**
29  * Set the MCYCLE counter register to zero.
30  */
31 inline void ibex_mcycle_zero(void) {
32  CSR_WRITE(CSR_REG_MCYCLE, 0);
33  CSR_WRITE(CSR_REG_MCYCLEH, 0);
34 }
35 
36 /**
37  * Read the low 32 bits of the MCYCLE counter.
38  */
40 inline uint32_t ibex_mcycle32(void) {
41  uint32_t val;
42  CSR_READ(CSR_REG_MCYCLE, &val);
43  return val;
44 }
45 
46 /**
47  * Read the 64-bit MCYCLE counter.
48  */
50 inline uint64_t ibex_mcycle(void) {
51  uint32_t lo, hi, hi2;
52  do {
53  CSR_READ(CSR_REG_MCYCLEH, &hi);
54  CSR_READ(CSR_REG_MCYCLE, &lo);
55  CSR_READ(CSR_REG_MCYCLEH, &hi2);
56  } while (hi != hi2);
57  return ((uint64_t)hi << 32) | lo;
58 }
59 
60 /**
61  * Convert from microseconds to CPU cycles.
62  */
63 inline uint64_t ibex_time_to_cycles(uint64_t time_us) {
64  return to_cpu_cycles(time_us);
65 }
66 #else
67 extern void ibex_mcycle_zero(void);
68 extern uint32_t ibex_mcycle32(void);
69 extern uint64_t ibex_mcycle(void);
70 extern uint64_t ibex_time_to_cycles(uint64_t time_us);
71 #endif
72 
73 /**
74  * An Ibex exception type for silicon_creator code.
75  *
76  * This enum is used to decode RISC-V exception causes generated by Ibex that
77  * are handled in silicon_creator code.
78  */
79 typedef enum ibex_exception_code {
80  kIbexExceptionCodeLoadAccessFault = 5,
81  kIbexExceptionCodeMax = 31,
82 } ibex_exception_code_t;
83 
84 /**
85  * The following constants represent the expected number of sec_mmio register
86  * writes performed by functions in provided in this module. See
87  * `SEC_MMIO_WRITE_INCREMENT()` for more details.
88  *
89  * Example:
90  * ```
91  * ibex_addr_remap_0_set(...);
92  * SEC_MMIO_WRITE_INCREMENT(kAddressTranslationSecMmioConfigure);
93  * ```
94  */
95 enum {
96  kAddressTranslationSecMmioConfigure = 6,
97 };
98 
99 /**
100  * Configure the instruction and data bus in the address translation slot 0.
101  *
102  * @param matching_addr When an incoming transaction matches the matching
103  * region, it is redirected to the new address. If a transaction does not match,
104  * then it is directly passed through.
105  * @param remap_addr The region where the matched transtaction will be
106  * redirected to.
107  * @param size The size of the regions mapped.
108  */
109 void ibex_addr_remap_0_set(uint32_t matching_addr, uint32_t remap_addr,
110  size_t size);
111 
112 /**
113  * Configure the instruction and data bus in the address translation slot 1.
114  *
115  * @param matching_addr When an incoming transaction matches the matching
116  * region, it is redirected to the new address. If a transaction does not match,
117  * then it is directly passed through.
118  * @param remap_addr The region where the matched transtaction will be
119  * redirected to.
120  * @param size The size of the regions mapped.
121  */
122 void ibex_addr_remap_1_set(uint32_t matching_addr, uint32_t remap_addr,
123  size_t size);
124 
125 /**
126  * Get the remap target address.
127  *
128  * Returns zero if the remap window is not in use.
129  *
130  * @param index Which window to lock (0 or 1).
131  * @return The remap target address.
132  */
133 uint32_t ibex_addr_remap_get(uint32_t index);
134 
135 /**
136  * Lock the remap windows so they cannot be reprogrammed.
137  * This function locks the given the IBUS and DBUS windows simultaneously.
138  *
139  * @param index Which window to lock (0 or 1).
140  */
141 void ibex_addr_remap_lockdown(uint32_t index);
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_IBEX_H_