Software APIs
alert_handler_testutils.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_LIB_TESTING_ALERT_HANDLER_TESTUTILS_H_
6#define OPENTITAN_SW_DEVICE_LIB_TESTING_ALERT_HANDLER_TESTUTILS_H_
7
8#include <stdbool.h>
9
10#include "sw/device/lib/base/status.h"
14
15#include "hw/top/alert_handler_regs.h"
16
17enum {
18 kAlertHandlerTestutilsDefaultPingTimeout = 256,
19};
20
21typedef enum alert_handler_class_state {
22 kCstateIdle = 0,
23 kCstateTimeout = 1,
24 kCstateTerminal = 3,
25 kCstatePhase0 = 4,
26 kCstatePhase1 = 5,
27 kCstatePhase2 = 6,
28 kCstatePhase3 = 7,
29 kCstateFsmError = 2
30} alert_handler_class_state_t;
31
32/**
33 * Alert crash dump layout, LSB first: the escalation states of all classes,
34 * then their escalation counts, then their accumulator counts, then the local
35 * alert and alert cause bits. See
36 * https://opentitan.org/book/hw/top_earlgrey/ip_autogen/alert_handler/doc/theory_of_operation.html#crashdump-output
37 * for Earlgrey as an example.
38 */
39enum {
40 kAlertHandlerClassEscStateBits = 3,
41 kAlertHandlerClassEscCntBits = ALERT_HANDLER_PARAM_ESC_CNT_DW,
42 kAlertHandlerClassAccumCntBits = ALERT_HANDLER_PARAM_ACCU_CNT_DW,
43 kAlertHandlerCrashdumpBits =
44 ALERT_HANDLER_PARAM_N_CLASSES *
45 (kAlertHandlerClassEscStateBits + kAlertHandlerClassEscCntBits +
46 kAlertHandlerClassAccumCntBits) +
47 ALERT_HANDLER_PARAM_N_LOC_ALERT + ALERT_HANDLER_PARAM_N_ALERTS,
48 kAlertHandlerCrashdumpWords = (kAlertHandlerCrashdumpBits + 31) / 32,
49};
50static_assert(kAlertHandlerCrashdumpWords <= DIF_RSTMGR_ALERT_INFO_MAX_SIZE,
51 "Alert crash dump does not fit the rstmgr alert info buffer");
52
53/**
54 * Represents the hardware alert crash dump in a more software-friendly manner.
55 */
57 bool alert_cause[ALERT_HANDLER_PARAM_N_ALERTS];
58 uint8_t loc_alert_cause;
59 uint16_t class_accum_cnt[ALERT_HANDLER_PARAM_N_CLASSES];
60 uint32_t class_esc_cnt[ALERT_HANDLER_PARAM_N_CLASSES];
61 alert_handler_class_state_t class_esc_state[ALERT_HANDLER_PARAM_N_CLASSES];
62} alert_handler_testutils_info_t;
63static_assert(ALERT_HANDLER_PARAM_N_LOC_ALERT <= 8 &&
64 kAlertHandlerClassAccumCntBits <= 16 &&
65 kAlertHandlerClassEscCntBits <= 32,
66 "Crash dump fields do not fit alert_handler_testutils_info_t");
67
68/**
69 * Converts the hardware alert crash dump into an
70 * `alert_handler_testutils_info_t`. This makes it easier to compare and display
71 * the different fields.
72 * @param dump Buffer containing the dump.
73 * @param dump_size The size of the `dump` in words.
74 * @param[out] info The parsed info.
75 * @return The result of the operation.
76 */
78status_t alert_handler_testutils_info_parse(
79 const dif_rstmgr_alert_info_dump_segment_t *dump, int dump_size,
80 alert_handler_testutils_info_t *info);
81
82/**
83 * Displays an alert_handler_testutils_info_t as strings.
84 */
85void alert_handler_testutils_info_dump(
86 const alert_handler_testutils_info_t *info);
87
88/**
89 * Configures alert handler with all required runtime information.
90 *
91 * This operation is lock-protected, meaning once the configuration is locked,
92 * it cannot be reconfigured until after a system reset. If `locked` is set to
93 * `kDifToggleEnabled`, then every lockable configuration will be locked.
94 * Otherwise, configurations may only be locked by performing subsequent calls
95 * to each component-specific locking DIF: `dif_alert_handler_lock_*(...)`.
96 *
97 * @param alert_handler An alert handler handle.
98 * @param config Runtime configuration parameters.
99 * @param locked The locked state to set for each configuration.
100 * @return The result of the operation.
101 */
103status_t alert_handler_testutils_configure_all(
104 const dif_alert_handler_t *alert_handler, dif_alert_handler_config_t config,
105 dif_toggle_t locked);
106
107/**
108 * Returns the number of cycles corresponding to the given microseconds.
109 *
110 * @param microseconds The number of microseconds.
111 * @param[out] cycles The number of AON clock cycles.
112 * @return The result of the operation.
113 */
115status_t alert_handler_testutils_get_cycles_from_us(uint64_t microseconds,
116 uint32_t *cycles);
117
118/**
119 * Returns a scaling factor for conversion of time to cycles.
120 *
121 * Setting escalation cycle counts must be such that the ISRs have time to
122 * complete before the next escalation stage starts. We tend to generate cycle
123 * counts from time durations, and given that the relevant clock frequencies
124 * are much lower in non-sim_dv devices we may end up having to set quite high
125 * time durations to avoid incomplete ISRs for fpga or verilator.
126 *
127 * This function allows the time durations to be set for the sim_dv clock
128 * frequencies, and other platforms get the cycle count rescaled by a factor
129 * of 10. It should be used in the conversion of time duration to cycle counts,
130 * as in
131 * cycles = udiv64_slow(micros * clockFreqHz, 1000000, NULL) *
132 * cycle_rescaling_factor();
133 */
134uint32_t alert_handler_testutils_cycle_rescaling_factor(void);
135
136/**
137 * Logs the status of the alert handler.
138 *
139 * This function will log the state of each alert class, including the
140 * number of alerts that have been accumulated.
141 *
142 * Only the alert classes that have accumulated alerts will be logged.
143 *
144 * @param alert_handler An alert handler handle.
145 * @return The result of the operation.
146 */
148status_t alert_handler_testutils_status_log(
149 const dif_alert_handler_t *alert_handler);
150
151/**
152 * Logs the alert handler crash dump.
153 *
154 * This function will log the state of each alert class, including the
155 * number of alerts that have been accumulated.
156 *
157 * Only alerts that have been set will be logged, as well as the alert
158 * classes that are in non-idle states.
159 *
160 * @param rstmgr A RSTMGR handle.
161 * @return The result of the operation.
162 */
164status_t alert_handler_testutils_dump_log(const dif_rstmgr_t *rstmgr);
165
166/**
167 * Enables the alert handler crash dump.
168 *
169 * This function will enable the alert handler crash dump, which will
170 * cause the alert handler to dump its state to the RSTMGR when an
171 * alert is triggered.
172 *
173 * @param alert_handler An alert handler handle.
174 * @param rstmgr A RSTMGR handle.
175 * @return The result of the operation.
176 */
178status_t alert_handler_testutils_dump_enable(
179 const dif_alert_handler_t *alert_handler, const dif_rstmgr_t *rstmgr);
180
181#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_ALERT_HANDLER_TESTUTILS_H_