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 "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 * Represents the hardware alert crash dump in a more software-friendly manner.
34 */
36 bool alert_cause[ALERT_HANDLER_PARAM_N_ALERTS];
37 uint8_t loc_alert_cause; // 7bit
38 uint16_t class_accum_cnt[ALERT_HANDLER_PARAM_N_CLASSES]; // 4x16bit
39 uint32_t class_esc_cnt[ALERT_HANDLER_PARAM_N_CLASSES]; // 4x32bit
40 alert_handler_class_state_t
41 class_esc_state[ALERT_HANDLER_PARAM_N_CLASSES]; // 4x3bit
42} alert_handler_testutils_info_t;
43
44/**
45 * Converts the hardware alert crash dump into an
46 * `alert_handler_testutils_info_t`. This makes it easier to compare and display
47 * the different fields.
48 * @param dump Buffer containing the dump.
49 * @param dump_size The size of the `dump` in words.
50 * @param[out] info The parsed info.
51 * @return The result of the operation.
52 */
54status_t alert_handler_testutils_info_parse(
55 const dif_rstmgr_alert_info_dump_segment_t *dump, int dump_size,
56 alert_handler_testutils_info_t *info);
57
58/**
59 * Displays an alert_handler_testutils_info_t as strings.
60 */
61void alert_handler_testutils_info_dump(
62 const alert_handler_testutils_info_t *info);
63
64/**
65 * Configures alert handler with all required runtime information.
66 *
67 * This operation is lock-protected, meaning once the configuration is locked,
68 * it cannot be reconfigured until after a system reset. If `locked` is set to
69 * `kDifToggleEnabled`, then every lockable configuration will be locked.
70 * Otherwise, configurations may only be locked by performing subsequent calls
71 * to each component-specific locking DIF: `dif_alert_handler_lock_*(...)`.
72 *
73 * @param alert_handler An alert handler handle.
74 * @param config Runtime configuration parameters.
75 * @param locked The locked state to set for each configuration.
76 * @return The result of the operation.
77 */
79status_t alert_handler_testutils_configure_all(
80 const dif_alert_handler_t *alert_handler, dif_alert_handler_config_t config,
81 dif_toggle_t locked);
82
83/**
84 * Returns the number of cycles corresponding to the given microseconds.
85 *
86 * @param microseconds The number of microseconds.
87 * @param[out] cycles The number of AON clock cycles.
88 * @return The result of the operation.
89 */
91status_t alert_handler_testutils_get_cycles_from_us(uint64_t microseconds,
92 uint32_t *cycles);
93
94/**
95 * Returns a scaling factor for conversion of time to cycles.
96 *
97 * Setting escalation cycle counts must be such that the ISRs have time to
98 * complete before the next escalation stage starts. We tend to generate cycle
99 * counts from time durations, and given that the relevant clock frequencies
100 * are much lower in non-sim_dv devices we may end up having to set quite high
101 * time durations to avoid incomplete ISRs for fpga or verilator.
102 *
103 * This function allows the time durations to be set for the sim_dv clock
104 * frequencies, and other platforms get the cycle count rescaled by a factor
105 * of 10. It should be used in the conversion of time duration to cycle counts,
106 * as in
107 * cycles = udiv64_slow(micros * clockFreqHz, 1000000, NULL) *
108 * cycle_rescaling_factor();
109 */
110uint32_t alert_handler_testutils_cycle_rescaling_factor(void);
111
112#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_ALERT_HANDLER_TESTUTILS_H_