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