Software APIs
alert_handler_testutils.c
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#include "sw/device/lib/testing/alert_handler_testutils.h"
6
7#include "hw/top/dt/dt_alert_handler.h" // Generated
8#include "hw/top/dt/dt_api.h" // Generated
13#include "sw/device/lib/testing/test_framework/check.h"
14
15#include "hw/top/alert_handler_regs.h" // Generated
16
17#define MODULE_ID MAKE_MODULE_ID('a', 'h', 't')
18
19const char kAlertClassName[] = {'A', 'B', 'C', 'D'};
20static_assert(ARRAYSIZE(kAlertClassName) == ALERT_HANDLER_PARAM_N_CLASSES,
21 "Expected four alert classes!");
22
23/**
24 * This is used to traverse the dump treating it as an array of bits, and
25 * extract a number of bits placing them in a uint32_t. The word and bit index
26 * are updated by num_bits before returning.
27 */
28static uint32_t get_next_n_bits(
29 int num_bits, const dif_rstmgr_alert_info_dump_segment_t *dump,
30 int *word_index, int *bit_index) {
31 CHECK(num_bits <= 32);
32 CHECK(*bit_index < 32);
33 uint32_t word = dump[*word_index] >> *bit_index;
34 if (*bit_index + num_bits >= 32) {
35 (*word_index) += 1;
36 *bit_index = *bit_index + num_bits - 32;
37 } else {
38 *bit_index += num_bits;
39 }
40 word &= (1 << num_bits) - 1;
41 return word;
42}
43
44status_t alert_handler_testutils_info_parse(
45 const dif_rstmgr_alert_info_dump_segment_t *dump, int dump_size,
46 alert_handler_testutils_info_t *info) {
47 int word_index = 0;
48 int bit_index = 0;
49 for (int i = 0; i < ALERT_HANDLER_PARAM_N_CLASSES; ++i) {
50 info->class_esc_state[i] =
51 get_next_n_bits(3, dump, &word_index, &bit_index);
52 }
53 for (int i = 0; i < ALERT_HANDLER_PARAM_N_CLASSES; ++i) {
54 info->class_esc_cnt[i] = get_next_n_bits(32, dump, &word_index, &bit_index);
55 }
56 for (int i = 0; i < ALERT_HANDLER_PARAM_N_CLASSES; ++i) {
57 info->class_accum_cnt[i] =
58 (uint16_t)get_next_n_bits(16, dump, &word_index, &bit_index);
59 }
60 info->loc_alert_cause =
61 (uint8_t)get_next_n_bits(7, dump, &word_index, &bit_index);
62 TRY_CHECK(word_index < dump_size);
63 for (int i = 0; i < ALERT_HANDLER_PARAM_N_ALERTS; ++i) {
64 info->alert_cause[i] = get_next_n_bits(1, dump, &word_index, &bit_index);
65 }
66 TRY_CHECK(word_index < dump_size);
67 return OK_STATUS();
68}
69
70void alert_handler_testutils_info_dump(
71 const alert_handler_testutils_info_t *info) {
72 LOG_INFO("alert_info:");
73 LOG_INFO("esc_state [0]=%x, [1]=%x, [2]=%x, [3]=%x", info->class_esc_state[0],
74 info->class_esc_state[1], info->class_esc_state[2],
75 info->class_esc_state[3]);
76 LOG_INFO("esc_cnt [0]=0x%x, [1]=0x%x, [2]=0x%x, [3]=0x%x",
77 info->class_esc_cnt[0], info->class_esc_cnt[1],
78 info->class_esc_cnt[2], info->class_esc_cnt[3]);
79 LOG_INFO("accum_cnt [0]=0x%x, [1]=0x%x, [2]=0x%x, [3]=0x%x",
80 info->class_accum_cnt[0], info->class_accum_cnt[1],
81 info->class_accum_cnt[2], info->class_accum_cnt[3]);
82 LOG_INFO("loc_alert_cause=0x%x", info->loc_alert_cause);
83 int set_count = 0;
84 LOG_INFO("alert_cause bits set:");
85 // Typically very few bits are set, so it is more clear to only show the
86 // on bits.
87 for (int i = 0; i < ALERT_HANDLER_PARAM_N_ALERTS; ++i) {
88 if (info->alert_cause[i]) {
89 LOG_INFO("alert_cause[%d] = 1", i);
90 ++set_count;
91 }
92 }
93 if (set_count == 0) {
94 LOG_INFO("No bits set");
95 }
96}
97
98status_t alert_handler_testutils_configure_all(
99 const dif_alert_handler_t *alert_handler, dif_alert_handler_config_t config,
100 dif_toggle_t locked) {
101 TRY_CHECK(alert_handler != NULL);
102 TRY_CHECK(dif_is_valid_toggle(locked));
103
104 // Check lengths of alert, local alert, and class arrays.
105 TRY_CHECK((config.alerts_len > 0 && config.alerts != NULL &&
106 config.alert_classes != NULL) ||
107 (config.alerts_len == 0 && config.alerts == NULL &&
108 config.alert_classes == NULL));
109 TRY_CHECK((config.local_alerts_len > 0 && config.local_alerts != NULL &&
110 config.local_alert_classes != NULL) ||
111 (config.local_alerts_len == 0 && config.local_alerts == NULL &&
112 config.local_alert_classes == NULL));
113 TRY_CHECK((config.classes_len > 0 && config.classes != NULL &&
114 config.class_configs != NULL) ||
115 (config.classes_len == 0 && config.classes == NULL &&
116 config.class_configs == NULL));
117
118 // Check that the provided ping timeout actually fits in the timeout
119 // register, which is smaller than a native word length.
120 TRY_CHECK(
121 config.ping_timeout <=
122 ALERT_HANDLER_PING_TIMEOUT_CYC_SHADOWED_PING_TIMEOUT_CYC_SHADOWED_MASK);
123
124 // Configure and enable the requested alerts.
125 for (int i = 0; i < config.alerts_len; ++i) {
126 TRY(dif_alert_handler_configure_alert(alert_handler, config.alerts[i],
127 config.alert_classes[i],
128 kDifToggleEnabled, locked));
129 }
130
131 // Configure and enable the requested local alerts.
132 for (int i = 0; i < config.local_alerts_len; ++i) {
133 TRY(dif_alert_handler_configure_local_alert(
134 alert_handler, config.local_alerts[i], config.local_alert_classes[i],
135 kDifToggleEnabled, locked));
136 }
137
138 // Configure and enable the requested classes.
139 for (int i = 0; i < config.classes_len; ++i) {
140 TRY(dif_alert_handler_configure_class(alert_handler, config.classes[i],
141 config.class_configs[i],
142 kDifToggleEnabled, locked));
143 }
144
145 // Configure the ping timer.
146 TRY(dif_alert_handler_configure_ping_timer(alert_handler, config.ping_timeout,
147 kDifToggleEnabled, locked));
148
149 return OK_STATUS();
150}
151
152status_t alert_handler_testutils_get_cycles_from_us(uint64_t microseconds,
153 uint32_t *cycles) {
154 uint64_t cycles_ = udiv64_slow(
157 1000000,
158 /*rem_out=*/NULL);
159 TRY_CHECK(cycles_ < UINT32_MAX,
160 "The value 0x%08x%08x can't fit into the 32 bits timer counter.",
161 (uint32_t)(cycles_ >> 32), (uint32_t)cycles_);
162 *cycles = (uint32_t)cycles_;
163 return OK_STATUS();
164}
165
166uint32_t alert_handler_testutils_cycle_rescaling_factor(void) {
167 return kDeviceType == kDeviceSimDV ? 1 : 10;
168}
169
170static status_t alert_handler_class_info_log(
171 const dif_alert_handler_t *alert_handler,
172 dif_alert_handler_class_t alert_class) {
174 TRY(dif_alert_handler_get_class_state(alert_handler, alert_class, &state));
175
176 uint16_t num_alerts;
177 TRY(dif_alert_handler_get_accumulator(alert_handler, alert_class,
178 &num_alerts));
179
180 if (num_alerts > 0) {
181 LOG_INFO("Alert class %c state: %d, acc_cnt: %d",
182 kAlertClassName[alert_class], state, num_alerts);
183
184 for (dif_alert_handler_alert_t alert = 0;
185 alert < ALERT_HANDLER_PARAM_N_ALERTS; ++alert) {
186 bool is_cause;
187 TRY(dif_alert_handler_alert_is_cause(alert_handler, alert, &is_cause));
188 if (is_cause) {
189 LOG_INFO("Alert %d is set", alert);
190 }
191 }
192
193 bool can_clear;
194 TRY(dif_alert_handler_escalation_can_clear(alert_handler, alert_class,
195 &can_clear));
196 if (can_clear) {
197 TRY(dif_alert_handler_escalation_clear(alert_handler, alert_class));
198 } else {
199 LOG_INFO("Alert class %c can't be cleared", kAlertClassName[alert_class]);
200 }
201 }
202
203 return OK_STATUS();
204}
205
206static status_t alert_handler_class_log(
207 const dif_alert_handler_t *alert_handler,
208 dif_alert_handler_class_t alert_class) {
210 TRY(dif_alert_handler_get_class_state(alert_handler, alert_class, &state));
211
212 uint16_t num_alerts;
213 TRY(dif_alert_handler_get_accumulator(alert_handler, alert_class,
214 &num_alerts));
215
216 if (num_alerts > 0) {
217 LOG_INFO("Alert class %c state: %d, acc_cnt: %d",
218 kAlertClassName[alert_class], state, num_alerts);
219 for (dif_alert_handler_alert_t alert = 0;
220 alert < ALERT_HANDLER_PARAM_N_ALERTS; ++alert) {
221 bool is_cause;
222 TRY(dif_alert_handler_alert_is_cause(alert_handler, alert, &is_cause));
223 if (is_cause) {
224 LOG_INFO("Alert %d is set", alert);
225 }
226 }
227 }
228
229 return OK_STATUS();
230}
231
232status_t alert_handler_testutils_status_log(
233 const dif_alert_handler_t *alert_handler) {
234 TRY_CHECK(alert_handler != NULL);
235
236 for (dif_alert_handler_class_t alert_class = 0;
237 alert_class < ALERT_HANDLER_PARAM_N_CLASSES; ++alert_class) {
238 TRY(alert_handler_class_log(alert_handler, alert_class));
239 }
240
241 return OK_STATUS();
242}
243
244status_t alert_handler_testutils_dump_log(const dif_rstmgr_t *rstmgr) {
245 TRY_CHECK(rstmgr != NULL);
246
248 size_t seg_size;
249 alert_handler_testutils_info_t actual_info;
250
251 uint32_t log_count = 0;
252
253 CHECK_DIF_OK(dif_rstmgr_alert_info_dump_read(
254 rstmgr, dump, DIF_RSTMGR_ALERT_INFO_MAX_SIZE, &seg_size));
255 CHECK(seg_size <= INT_MAX, "seg_size must fit in int");
256 CHECK_STATUS_OK(
257 alert_handler_testutils_info_parse(dump, (int)seg_size, &actual_info));
258
259 for (dif_alert_handler_class_t alert_class = 0;
260 alert_class < ALERT_HANDLER_PARAM_N_CLASSES; ++alert_class) {
261 if (actual_info.class_esc_state[alert_class] != kCstateIdle) {
262 LOG_INFO("crashdump - Alert class %c state: %d, acc_cnt: %d, esc_cnt: %d",
263 kAlertClassName[alert_class],
264 actual_info.class_esc_state[alert_class],
265 actual_info.class_accum_cnt[alert_class],
266 actual_info.class_esc_cnt[alert_class]);
267 log_count++;
268 }
269 }
270 for (dif_alert_handler_alert_t alert = 0;
271 alert < ALERT_HANDLER_PARAM_N_ALERTS; ++alert) {
272 if (actual_info.alert_cause[alert]) {
273 LOG_INFO("crashdump - Alert %d is set", alert);
274 log_count++;
275 }
276 }
277
278 if (log_count == 0) {
279 LOG_INFO("crashdump - No alerts reported");
280 }
281
282 return OK_STATUS();
283}
284
285status_t alert_handler_testutils_dump_enable(
286 const dif_alert_handler_t *alert_handler, const dif_rstmgr_t *rstmgr) {
287 TRY_CHECK(alert_handler != NULL);
288 TRY_CHECK(rstmgr != NULL);
289
290 uint32_t enable_count = 0;
291 for (dif_alert_handler_class_t alert_class = 0;
292 alert_class < ALERT_HANDLER_PARAM_N_CLASSES; ++alert_class) {
293 bool is_enabled;
294 TRY(dif_alert_handler_is_class_enabled(alert_handler, alert_class,
295 &is_enabled));
296 if (is_enabled) {
297 TRY(dif_alert_handler_crash_dump_trigger_set(
298 alert_handler, alert_class, kDifAlertHandlerClassStatePhase3));
299 enable_count++;
300 }
301 }
302
303 if (enable_count) {
304 CHECK_DIF_OK(dif_rstmgr_alert_info_set_enabled(rstmgr, kDifToggleEnabled));
305 }
306
307 return OK_STATUS();
308}