5#include "sw/device/lib/testing/alert_handler_testutils.h"
7#include "hw/top/dt/alert_handler.h"
8#include "hw/top/dt/api.h"
13#include "sw/device/lib/testing/test_framework/check.h"
15#include "hw/top/alert_handler_regs.h"
17#define MODULE_ID MAKE_MODULE_ID('a', 'h', 't')
19const char kAlertClassName[] = {
'A',
'B',
'C',
'D'};
20static_assert(
ARRAYSIZE(kAlertClassName) == ALERT_HANDLER_PARAM_N_CLASSES,
21 "Expected four alert classes!");
28static uint32_t get_next_n_bits(
30 uint32_t *word_index, uint32_t *bit_index) {
31 CHECK(num_bits <= 32);
32 CHECK(*bit_index < 32);
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;
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) {
46 if (num_bits_word1 == num_bits) {
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;
56status_t alert_handler_testutils_info_parse(
58 alert_handler_testutils_info_t *info) {
59 uint32_t word_index = 0;
60 uint32_t bit_index = 0;
61 TRY_CHECK(dump_size >= kAlertHandlerCrashdumpWords,
62 "alert crash dump too short: %d words, expected %d", dump_size,
63 kAlertHandlerCrashdumpWords);
64 for (
int i = 0; i < ALERT_HANDLER_PARAM_N_CLASSES; ++i) {
65 info->class_esc_state[i] = get_next_n_bits(kAlertHandlerClassEscStateBits,
66 dump, &word_index, &bit_index);
68 for (
int i = 0; i < ALERT_HANDLER_PARAM_N_CLASSES; ++i) {
69 info->class_esc_cnt[i] = get_next_n_bits(kAlertHandlerClassEscCntBits, dump,
70 &word_index, &bit_index);
72 for (
int i = 0; i < ALERT_HANDLER_PARAM_N_CLASSES; ++i) {
73 info->class_accum_cnt[i] = (uint16_t)get_next_n_bits(
74 kAlertHandlerClassAccumCntBits, dump, &word_index, &bit_index);
76 info->loc_alert_cause = (uint8_t)get_next_n_bits(
77 ALERT_HANDLER_PARAM_N_LOC_ALERT, dump, &word_index, &bit_index);
78 for (
int i = 0; i < ALERT_HANDLER_PARAM_N_ALERTS; ++i) {
79 info->alert_cause[i] = get_next_n_bits(1, dump, &word_index, &bit_index);
84void alert_handler_testutils_info_dump(
85 const alert_handler_testutils_info_t *info) {
87 LOG_INFO(
"esc_state [0]=%x, [1]=%x, [2]=%x, [3]=%x", info->class_esc_state[0],
88 info->class_esc_state[1], info->class_esc_state[2],
89 info->class_esc_state[3]);
90 LOG_INFO(
"esc_cnt [0]=0x%x, [1]=0x%x, [2]=0x%x, [3]=0x%x",
91 info->class_esc_cnt[0], info->class_esc_cnt[1],
92 info->class_esc_cnt[2], info->class_esc_cnt[3]);
93 LOG_INFO(
"accum_cnt [0]=0x%x, [1]=0x%x, [2]=0x%x, [3]=0x%x",
94 info->class_accum_cnt[0], info->class_accum_cnt[1],
95 info->class_accum_cnt[2], info->class_accum_cnt[3]);
96 LOG_INFO(
"loc_alert_cause=0x%x", info->loc_alert_cause);
101 for (
int i = 0; i < ALERT_HANDLER_PARAM_N_ALERTS; ++i) {
102 if (info->alert_cause[i]) {
107 if (set_count == 0) {
112status_t alert_handler_testutils_configure_all(
115 TRY_CHECK(alert_handler != NULL);
116 TRY_CHECK(dif_is_valid_toggle(locked));
136 ALERT_HANDLER_PING_TIMEOUT_CYC_SHADOWED_PING_TIMEOUT_CYC_SHADOWED_MASK);
140 TRY(dif_alert_handler_configure_alert(alert_handler, config.
alerts[i],
147 TRY(dif_alert_handler_configure_local_alert(
154 TRY(dif_alert_handler_configure_class(alert_handler, config.
classes[i],
160 TRY(dif_alert_handler_configure_ping_timer(alert_handler, config.
ping_timeout,
166status_t alert_handler_testutils_get_cycles_from_us(uint64_t microseconds,
168 uint64_t cycles_ = udiv64_slow(
173 TRY_CHECK(cycles_ < UINT32_MAX,
174 "The value 0x%08x%08x can't fit into the 32 bits timer counter.",
175 (uint32_t)(cycles_ >> 32), (uint32_t)cycles_);
176 *cycles = (uint32_t)cycles_;
180uint32_t alert_handler_testutils_cycle_rescaling_factor(
void) {
184static status_t alert_handler_class_info_log(
188 TRY(dif_alert_handler_get_class_state(alert_handler, alert_class, &state));
191 TRY(dif_alert_handler_get_accumulator(alert_handler, alert_class,
194 if (num_alerts > 0) {
195 LOG_INFO(
"Alert class %c state: %d, acc_cnt: %d",
196 kAlertClassName[alert_class], state, num_alerts);
199 alert < ALERT_HANDLER_PARAM_N_ALERTS; ++alert) {
201 TRY(dif_alert_handler_alert_is_cause(alert_handler, alert, &is_cause));
208 TRY(dif_alert_handler_escalation_can_clear(alert_handler, alert_class,
211 TRY(dif_alert_handler_escalation_clear(alert_handler, alert_class));
213 LOG_INFO(
"Alert class %c can't be cleared", kAlertClassName[alert_class]);
220static status_t alert_handler_class_log(
224 TRY(dif_alert_handler_get_class_state(alert_handler, alert_class, &state));
227 TRY(dif_alert_handler_get_accumulator(alert_handler, alert_class,
230 if (num_alerts > 0) {
231 LOG_INFO(
"Alert class %c state: %d, acc_cnt: %d",
232 kAlertClassName[alert_class], state, num_alerts);
234 alert < ALERT_HANDLER_PARAM_N_ALERTS; ++alert) {
236 TRY(dif_alert_handler_alert_is_cause(alert_handler, alert, &is_cause));
246status_t alert_handler_testutils_status_log(
248 TRY_CHECK(alert_handler != NULL);
251 alert_class < ALERT_HANDLER_PARAM_N_CLASSES; ++alert_class) {
252 TRY(alert_handler_class_log(alert_handler, alert_class));
258status_t alert_handler_testutils_dump_log(
const dif_rstmgr_t *rstmgr) {
259 TRY_CHECK(rstmgr != NULL);
263 alert_handler_testutils_info_t actual_info;
265 uint32_t log_count = 0;
267 CHECK_DIF_OK(dif_rstmgr_alert_info_dump_read(
269 CHECK(seg_size <= INT_MAX,
"seg_size must fit in int");
271 alert_handler_testutils_info_parse(dump, (
int)seg_size, &actual_info));
274 alert_class < ALERT_HANDLER_PARAM_N_CLASSES; ++alert_class) {
275 if (actual_info.class_esc_state[alert_class] != kCstateIdle) {
276 LOG_INFO(
"crashdump - Alert class %c state: %d, acc_cnt: %d, esc_cnt: %d",
277 kAlertClassName[alert_class],
278 actual_info.class_esc_state[alert_class],
279 actual_info.class_accum_cnt[alert_class],
280 actual_info.class_esc_cnt[alert_class]);
285 alert < ALERT_HANDLER_PARAM_N_ALERTS; ++alert) {
286 if (actual_info.alert_cause[alert]) {
287 LOG_INFO(
"crashdump - Alert %d is set", alert);
292 if (log_count == 0) {
293 LOG_INFO(
"crashdump - No alerts reported");
299status_t alert_handler_testutils_dump_enable(
301 TRY_CHECK(alert_handler != NULL);
302 TRY_CHECK(rstmgr != NULL);
304 uint32_t enable_count = 0;
306 alert_class < ALERT_HANDLER_PARAM_N_CLASSES; ++alert_class) {
308 TRY(dif_alert_handler_is_class_enabled(alert_handler, alert_class,
311 TRY(dif_alert_handler_crash_dump_trigger_set(