blob: f90596d0d719e12db9a1293b5a09235771e66f20 [file] [log] [blame]
Miguel Young de la Sota475887f2020-08-10 16:40:09 -04001// Copyright lowRISC contributors.
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_DIF_DIF_ALERT_HANDLER_H_
6#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ALERT_HANDLER_H_
7
8/**
9 * @file
10 * @brief <a href="/hw/ip/alert_handler/doc/">Alert handler</a> Device Interface
11 * Functions
12 */
13
14#include <stdint.h>
15
Timothy Trippele3f8a822021-09-17 06:09:28 +000016#include "sw/device/lib/base/macros.h"
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040017#include "sw/device/lib/base/mmio.h"
Timothy Trippel915ba132021-09-24 01:23:38 +000018#include "sw/device/lib/dif/dif_base.h"
19
20#include "sw/device/lib/dif/autogen/dif_alert_handler_autogen.h"
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040021
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040022#ifdef __cplusplus
23extern "C" {
24#endif // __cplusplus
25
26/**
Timothy Trippel95be8e62022-01-14 18:11:43 -080027 * Helper X macro for defining enums and case statements related to alert
28 * classes. If an additional class is ever added to the hardware, this list can
29 * be updated.
30 */
31#define LIST_OF_CLASSES(X) \
32 X(A, 0) \
33 X(B, 1) \
34 X(C, 2) \
35 X(D, 3)
36
37/**
38 * Helper macro for defining a `dif_alert_handler_class_t` enumeration constant.
Timothy Trippel059578e2022-02-25 20:04:55 -080039 * @class_ Alert class of the enumeration constant.
40 * @value_ Value of the enumeration constant.
Timothy Trippel95be8e62022-01-14 18:11:43 -080041 */
42#define ALERT_CLASS_ENUM_INIT_(class_, value_) \
43 kDifAlertHandlerClass##class_ = value_,
44
45/**
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040046 * An alert class.
47 *
48 * An alert class roughly specifies how to deal with an alert. The class
49 * determines which interrupt handler is fired for an alert, as well as the
50 * fine-grained details of the escalation policy, for when the processor
51 * fails to respond to an alert quickly enough.
52 *
53 * Alert classes serve as the alert handler's IRQ types. There is one IRQ for
54 * each class. Whenever an alert fires, the corresponding class's IRQ is
55 * serviced by the processor (if enabled).
56 */
57typedef enum dif_alert_handler_class {
Timothy Trippel95be8e62022-01-14 18:11:43 -080058 LIST_OF_CLASSES(ALERT_CLASS_ENUM_INIT_)
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040059} dif_alert_handler_class_t;
60
61/**
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040062 * An alert, identified by a numeric id.
63 *
64 * Alerts are hardware-level events indicating that something catastrophic
Timothy Trippel26f70882022-01-06 15:10:41 -080065 * has happened. The alert handler consumes alerts, classifies them into a
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040066 * particular `dif_alert_handler_class_t`, and uses policy information attached
67 * to that class to handle it.
68 *
Timothy Trippel26f70882022-01-06 15:10:41 -080069 * The number of alerts is configurable at hardware-synthesis time.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040070 */
71typedef uint32_t dif_alert_handler_alert_t;
72
73/**
Timothy Trippel95be8e62022-01-14 18:11:43 -080074 * Helper X macro for defining enums and case statements related to local
75 * alerts. If an additional class is ever added to the hardware, this list can
76 * be updated.
77 */
78#define LIST_OF_LOC_ALERTS(X) \
79 X(kDifAlertHandlerLocalAlertAlertPingFail, 0) \
80 X(kDifAlertHandlerLocalAlertEscalationPingFail, 1) \
81 X(kDifAlertHandlerLocalAlertAlertIntegrityFail, 2) \
82 X(kDifAlertHandlerLocalAlertEscalationIntegrityFail, 3) \
83 X(kDifAlertHandlerLocalAlertBusIntegrityFail, 4) \
84 X(kDifAlertHandlerLocalAlertShadowedUpdateError, 5) \
85 X(kDifAlertHandlerLocalAlertShadowedStorageError, 6)
86
87/**
88 * Helper macro for defining a `dif_alert_handler_local_alert_t` enumeration
89 * constant.
90 * @name_ Name of the enumeration constant.
91 */
92#define LOC_ALERT_ENUM_INIT_(name_, value_) name_ = value_,
93
94/**
Miguel Young de la Sota475887f2020-08-10 16:40:09 -040095 * A local alert originating from within the alert handler itself.
96 *
97 * A local alert is exactly the same as a normal `dif_alert_handler_alert_t`,
98 * except that they use different functions for setting up classification and
99 * for getting causes.
100 */
101typedef enum dif_alert_handler_local_alert {
Timothy Trippel95be8e62022-01-14 18:11:43 -0800102 LIST_OF_LOC_ALERTS(LOC_ALERT_ENUM_INIT_)
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400103} dif_alert_handler_local_alert_t;
104
105/**
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400106 * An alert class state.
107 *
108 * This enum describes the sequence of states in the *escalation protocol*,
109 * which triggers under two different conditions:
Timothy Trippel3d578e52021-08-05 00:37:34 +0000110 * - If too many alerts of a particular class accumulate.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400111 * - If the software IRQ handler for that class times out.
112 *
113 * When either of these conditions is reached, phase 0 begins. This may trigger
114 * an escalation signal, and after a configured duration, proceed to phase 1.
115 * This process repeats until phase 3 ends, at which point the class enters a
116 * "bricked" terminal state, which cannot be exited except by reset.
117 *
118 * At any point, software may end the escalation protocol by calling
119 * `dif_alert_handler_escalation_clear()` (unless clearing is disabled).
120 * Successfully calling this function, or clearing the IRQ on time, will reset
Timothy Trippel26f70882022-01-06 15:10:41 -0800121 * the state back to idle. Note that this function cannot clear the terminal
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400122 * state; that state can only be cleared by resetting the chip.
123 */
124typedef enum dif_alert_handler_class_state {
125 /**
126 * The initial, idle state.
127 */
128 kDifAlertHandlerClassStateIdle,
129 /**
130 * The "timeout" state, that is, the IRQ has been fired and the clock is
131 * ticking for the processor to handle the alert.
132 */
133 kDifAlertHandlerClassStateTimeout,
134
135 /**
136 * The zeroth escalation phase.
137 */
138 kDifAlertHandlerClassStatePhase0,
139 /**
140 * The first escalation phase.
141 */
142 kDifAlertHandlerClassStatePhase1,
143 /**
144 * The second escalation phase.
145 */
146 kDifAlertHandlerClassStatePhase2,
147 /**
148 * The third escalation phase.
149 */
150 kDifAlertHandlerClassStatePhase3,
151
152 /**
153 * The terminal state. Most configurations will never reach this state, since
154 * one of the previous phases will use an escalation signal to reset the
155 * device.
156 */
157 kDifAlertHandlerClassStateTerminal,
158} dif_alert_handler_class_state_t;
159
160/**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800161 * An escalation signal, identified by a numeric ID.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400162 *
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800163 * An escalation signal is a generic "response" to failing to handle alert(s).
164 * The meaning of each escalation signal is determined by the chip.
165 *
166 * An alert class can be configured to raise various escalation signal(s) during
167 * various escalation phases as part of its escalation policy.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400168 */
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800169typedef uint32_t dif_alert_handler_escalation_signal_t;
170
171/**
172 * Runtime configuration for an escalation phase.
173 */
174typedef struct dif_alert_handler_escalation_phase {
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400175 /**
176 * The phase this configuration describes.
177 *
178 * It is an error for this to not be one of the `Phase` constants in
179 * `dif_alert_handler_class_state_t`.
180 */
181 dif_alert_handler_class_state_t phase;
182 /**
Timothy Trippel26f70882022-01-06 15:10:41 -0800183 * The escalation signal that should be triggered when this phase begins.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400184 */
Timothy Trippel26f70882022-01-06 15:10:41 -0800185 dif_alert_handler_escalation_signal_t signal;
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400186 /**
187 * The duration of this phase, in cycles.
188 */
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800189 uint32_t duration_cycles;
190} dif_alert_handler_escalation_phase_t;
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400191
192/**
193 * Runtime configuration for a particular alert class.
194 *
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800195 * This struct describes the escalation protocol for an alert class.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400196 */
197typedef struct dif_alert_handler_class_config {
198 /**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800199 * Whether to automatically lock the accumulation counter.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400200 *
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800201 * There are two ways to lock the accumulation counter (prevent it from being
202 * cleared once the class's escalation protocol has been triggered):
203 * 1. clear the write enable for the accumulation counter clear register, or
204 * 2. to set this configuration flag which will automatically clear the
205 * write enable for the accumulation counter clear register once the
206 * class's escalation protocol has been triggered.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400207 */
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800208 dif_toggle_t auto_lock_accumulation_counter;
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400209 /**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800210 * The threshold for the class accmulator which indicates the number of alerts
211 * that must fire because the class's escalation protocol will trigger.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400212 */
213 uint16_t accumulator_threshold;
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400214 /**
215 * The number of cycles this class's associated IRQ handler has to acknowledge
216 * the IRQ before escalation is triggered.
217 *
218 * A value of zero disables the timeout.
219 */
220 uint32_t irq_deadline_cycles;
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400221 /**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800222 * Escalation phases to be configured for this class.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400223 *
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800224 * Each escalation phase in this list will additionally be set as enabled for
225 * this class; phases not listed will have their escalation signals disabled.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400226 */
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800227 const dif_alert_handler_escalation_phase_t *escalation_phases;
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400228 /**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800229 * The length of the array `escalation_phases`.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400230 */
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800231 size_t escalation_phases_len;
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400232 /**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800233 * The escalation phase to capture the crashdump information in.
234 *
235 * It is an error for this to not be one of the `Phase` constants in
236 * `dif_alert_handler_class_state_t`.
237 *
238 * Note, it is recommended to capture the crashdump upon entering the first
239 * escalation phase that activates a countermeasure with many side-effects
240 * (e.g. life cycle state scrapping) in order to prevent spurious alert events
241 * from masking the original alert causes.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400242 */
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800243 dif_alert_handler_class_state_t crashdump_escalation_phase;
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400244} dif_alert_handler_class_config_t;
245
246/**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800247 * Runtime configuration for the alert handler.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400248 *
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800249 * This struct describes runtime information for a single-shot configuration of
250 * the alert handler hardware.
251 *
252 * Note, any of the array pointers may be NULL, in which case the associated
253 * length should be 0.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400254 */
255typedef struct dif_alert_handler_config {
256 /**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800257 * A list of alerts to configure.
258 */
259 dif_alert_handler_alert_t *alerts;
260 /**
261 * A list of classes to assign each alert to.
262 */
263 dif_alert_handler_class_t *alert_classes;
264 /**
265 * The lengths of the arrays `alerts` and `alert_classes`.
266 */
267 size_t alerts_len;
268
269 /**
270 * A list of local alerts to configure.
271 */
272 dif_alert_handler_local_alert_t *local_alerts;
273 /**
274 * A list of classes to assign each local alert to.
275 */
276 dif_alert_handler_class_t *local_alert_classes;
277 /**
278 * The lengths of the arrays `local_alerts` and `local_alert_classes`.
279 */
280 size_t local_alerts_len;
281
282 /**
283 * A list of alert classes to configure.
284 */
285 const dif_alert_handler_class_t *classes;
286 /**
287 * A list of alert class (escalation protocol) configurations.
288 */
289 const dif_alert_handler_class_config_t *class_configs;
290 /**
291 * The length of the arrays `classes` and `class_configs`.
292 */
293 size_t classes_len;
294
295 /**
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400296 * The alert ping timeout, in cycles.
297 *
Miguel Young de la Sotadd71d982020-09-17 15:40:16 -0400298 * The alert handler will regularly, at random intervals, ping alert
299 * sources. If a source fails to respond, a local alert will be raised.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400300 *
301 * The appropriate value will be dependent on all of the clocks involved on
302 * a chip.
303 *
304 * Note that the ping timer won't start until `dif_alert_handler_lock()` is
305 * successfully called.
Miguel Young de la Sotadd71d982020-09-17 15:40:16 -0400306 *
Timothy Trippel26f70882022-01-06 15:10:41 -0800307 * Note while this value must fit into the timeout register which is smaller
308 * than the native word length.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400309 */
310 uint32_t ping_timeout;
311
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400312} dif_alert_handler_config_t;
313
314/**
Timothy Trippel33e0d1e2022-01-07 13:56:41 -0800315 * Configures an alert in the alert handler.
316 *
Timothy Trippelfc964272022-01-10 19:03:20 -0800317 * This operation is lock-protected, meaning once the configuration is locked,
318 * it cannot be reconfigured until after a system reset.
Timothy Trippel33e0d1e2022-01-07 13:56:41 -0800319 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800320 * @param alert_handler An alert handler handle.
Timothy Trippel33e0d1e2022-01-07 13:56:41 -0800321 * @param alert The alert to be configured.
322 * @param alert_class The class to assign the alert to.
323 * @param enabled The enablement state to configure the alert in.
324 * @param locked The locked state to configure the alert in.
325 * @return The result of the operation.
326 */
327OT_WARN_UNUSED_RESULT
328dif_result_t dif_alert_handler_configure_alert(
329 const dif_alert_handler_t *alert_handler, dif_alert_handler_alert_t alert,
330 dif_alert_handler_class_t alert_class, dif_toggle_t enabled,
331 dif_toggle_t locked);
332
333/**
Timothy Trippelfc964272022-01-10 19:03:20 -0800334 * Configures a local alert in the alert handler.
335 *
336 * This operation is lock-protected, meaning once the configuration is locked,
337 * it cannot be reconfigured until after a system reset.
338 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800339 * @param alert_handler An alert handler handle.
Timothy Trippelfc964272022-01-10 19:03:20 -0800340 * @param local_alert The local alert to be configured.
341 * @param alert_class The class to assign the alert to.
342 * @param enabled The enablement state to configure the alert in.
343 * @param locked The locked state to configure the alert in.
344 * @return The result of the operation.
345 */
346OT_WARN_UNUSED_RESULT
347dif_result_t dif_alert_handler_configure_local_alert(
348 const dif_alert_handler_t *alert_handler,
349 dif_alert_handler_local_alert_t local_alert,
350 dif_alert_handler_class_t alert_class, dif_toggle_t enabled,
351 dif_toggle_t locked);
352
353/**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800354 * Configures the escalation protocol of an alert class in the alert handler.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400355 *
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800356 * This operation is lock-protected, meaning once the configuration is locked,
357 * it cannot be reconfigured until after a system reset.
358 *
359 * Note, regardless if the class is enabled or, IRQs will still fire based on
360 * the accumulation counter threshold configuration for the class, however, the
361 * escalation protocol will not trigger.
362 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800363 * @param alert_handler An alert handler handle.
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800364 * @param alert_class The class to be configured.
365 * @param config The escalation protocol configuration.
366 * @param enabled The enablement state of the class escalation protocol.
367 * @param locked The locked state to configure the class in.
Timothy Trippel95be8e62022-01-14 18:11:43 -0800368 * @return The result of the operation.
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800369 */
Timothy Trippel87a14512022-02-10 17:03:18 -0800370OT_WARN_UNUSED_RESULT
371dif_result_t dif_alert_handler_configure_class(
372 const dif_alert_handler_t *alert_handler,
373 dif_alert_handler_class_t alert_class,
374 dif_alert_handler_class_config_t config, dif_toggle_t enabled,
375 dif_toggle_t locked);
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800376
377/**
Timothy Trippelfe8409a2022-01-13 17:22:07 -0800378 * Configures the ping timer in the alert handler.
379 *
380 * This operation is lock-protected, meaning once the configuration is locked,
381 * it cannot be reconfigured until after a system reset.
382 *
383 * Note, the ping timer will only ping alerts that have been enabled AND locked.
384 * Therefore, this DIF should be invoked after configuring and enabling each
385 * (local) alert.
386 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800387 * @param alert_handler An alert handler handle.
Timothy Trippelfe8409a2022-01-13 17:22:07 -0800388 * @param ping_timeout The alert ping timeout, in cycles.
389 * @param enabled The enablement state to configure the ping timer in.
390 * @param locked The locked state to configure ping timer in.
Timothy Trippelcc147fa2022-01-13 17:41:23 -0800391 * @return The result of the operation.
Timothy Trippelfe8409a2022-01-13 17:22:07 -0800392 */
Miguel Young de la Sota82ff7f62022-03-03 10:54:56 -0500393OT_WARN_UNUSED_RESULT
394dif_result_t dif_alert_handler_configure_ping_timer(
Timothy Trippelfe8409a2022-01-13 17:22:07 -0800395 const dif_alert_handler_t *alert_handler, uint32_t ping_timeout,
396 dif_toggle_t enabled, dif_toggle_t locked);
397
398/**
Timothy Trippelcc147fa2022-01-13 17:41:23 -0800399 * Enables the ping timer in the alert handler.
400 *
401 * This operation is lock-protected, meaning once the configuration is locked,
402 * it cannot be reconfigured until after a system reset.
403 *
404 * Note, the ping timer will only ping alerts that have been enabled AND locked.
405 * Therefore, this DIF should be invoked after configuring and enabling each
406 * (local) alert.
407 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800408 * @param alert_handler An alert handler handle.
Timothy Trippelcc147fa2022-01-13 17:41:23 -0800409 * @param locked The locked state to configure ping timer in after enabling it.
410 * @return The result of the operation.
411 */
Miguel Young de la Sota82ff7f62022-03-03 10:54:56 -0500412OT_WARN_UNUSED_RESULT
413dif_result_t dif_alert_handler_ping_timer_set_enabled(
Timothy Trippelcc147fa2022-01-13 17:41:23 -0800414 const dif_alert_handler_t *alert_handler, dif_toggle_t locked);
415
416/**
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800417 * Locks out an alert handler alert configuration.
Timothy Trippel3dacc6f2022-01-19 13:39:10 -0800418 *
419 * This operation cannot be undone, and should be performed at the end of
420 * configuring the alert handler in early boot.
421 *
422 * This function is reentrant: calling it while functionality is locked will
423 * have no effect and return `kDifOk`.
424 *
425 * @param alert_handler An alert handler handle.
426 * @param alert The alert to lock.
427 * @return The result of the operation.
428 */
429OT_WARN_UNUSED_RESULT
430dif_result_t dif_alert_handler_lock_alert(
431 const dif_alert_handler_t *alert_handler, dif_alert_handler_alert_t alert);
432
433/**
434 * Checks whether an alert handler's alert is locked.
435 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800436 * @param alert_handler An alert handler handle.
Timothy Trippel3dacc6f2022-01-19 13:39:10 -0800437 * @param alert The alert to check is locked.
Timothy Trippel059578e2022-02-25 20:04:55 -0800438 * @param[out] is_locked Out-param for the locked state.
Timothy Trippel3dacc6f2022-01-19 13:39:10 -0800439 * @return The result of the operation.
440 */
441OT_WARN_UNUSED_RESULT
442dif_result_t dif_alert_handler_is_alert_locked(
443 const dif_alert_handler_t *alert_handler, dif_alert_handler_alert_t alert,
444 bool *is_locked);
445
446/**
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800447 * Locks out an alert handler local alert configuration.
448 *
449 * This operation cannot be undone, and should be performed at the end of
450 * configuring the alert handler in early boot.
451 *
452 * This function is reentrant: calling it while functionality is locked will
453 * have no effect and return `kDifOk`.
454 *
455 * @param alert_handler An alert handler handle.
456 * @param local_alert The local alert to lock.
457 * @return The result of the operation.
458 */
459OT_WARN_UNUSED_RESULT
460dif_result_t dif_alert_handler_lock_local_alert(
461 const dif_alert_handler_t *alert_handler,
462 dif_alert_handler_local_alert_t local_alert);
463
464/**
465 * Checks whether an alert handler's local alert is locked.
466 *
467 * @param alert_handler An alert handler handle.
468 * @param local_alert The local alert to check is locked.
Timothy Trippel059578e2022-02-25 20:04:55 -0800469 * @param[out] is_locked Out-param for the locked state.
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800470 * @return The result of the operation.
471 */
472OT_WARN_UNUSED_RESULT
473dif_result_t dif_alert_handler_is_local_alert_locked(
474 const dif_alert_handler_t *alert_handler,
475 dif_alert_handler_local_alert_t local_alert, bool *is_locked);
476
477/**
Timothy Trippelc1376762022-01-19 15:51:08 -0800478 * Locks out an alert handler class configuration.
479 *
480 * This operation cannot be undone, and should be performed at the end of
481 * configuring the alert handler in early boot.
482 *
483 * This function is reentrant: calling it while functionality is locked will
484 * have no effect and return `kDifOk`.
485 *
486 * @param alert_handler An alert handler handle.
487 * @param alert_class The alert class to lock.
488 * @return The result of the operation.
489 */
490OT_WARN_UNUSED_RESULT
491dif_result_t dif_alert_handler_lock_class(
492 const dif_alert_handler_t *alert_handler,
493 dif_alert_handler_class_t alert_class);
494
495/**
496 * Checks whether an alert handler's class is locked.
497 *
498 * @param alert_handler An alert handler handle.
499 * @param alert_class The alert class to check is locked.
Timothy Trippel059578e2022-02-25 20:04:55 -0800500 * @param[out] is_locked Out-param for the locked state.
Timothy Trippelc1376762022-01-19 15:51:08 -0800501 * @return The result of the operation.
502 */
503OT_WARN_UNUSED_RESULT
504dif_result_t dif_alert_handler_is_class_locked(
505 const dif_alert_handler_t *alert_handler,
506 dif_alert_handler_class_t alert_class, bool *is_locked);
507
508/**
Timothy Trippeleb0ca162022-01-12 16:06:10 -0800509 * Locks out alert handler ping timer configuration.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400510 *
511 * This operation cannot be undone, and should be performed at the end of
512 * configuring the alert handler in early boot.
513 *
514 * This function is reentrant: calling it while functionality is locked will
Timothy Trippel26f70882022-01-06 15:10:41 -0800515 * have no effect and return `kDifOk`.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400516 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800517 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400518 * @return The result of the operation.
519 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000520OT_WARN_UNUSED_RESULT
Timothy Trippel26f70882022-01-06 15:10:41 -0800521dif_result_t dif_alert_handler_lock_ping_timer(
522 const dif_alert_handler_t *alert_handler);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400523
524/**
Timothy Trippel26f70882022-01-06 15:10:41 -0800525 * Checks whether alert handler's ping timer is locked.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400526 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800527 * @param alert_handler An alert handler handle.
Timothy Trippel059578e2022-02-25 20:04:55 -0800528 * @param[out] is_locked Out-param for the locked state.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400529 * @return The result of the operation.
530 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000531OT_WARN_UNUSED_RESULT
Timothy Trippel26f70882022-01-06 15:10:41 -0800532dif_result_t dif_alert_handler_is_ping_timer_locked(
Timothy Trippel915ba132021-09-24 01:23:38 +0000533 const dif_alert_handler_t *alert_handler, bool *is_locked);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400534
535/**
536 * Checks whether an alert is one of the causes for an alert IRQ.
537 *
Miguel Young de la Sotadd71d982020-09-17 15:40:16 -0400538 * Note that multiple alerts may be causes at the same time.
539 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800540 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400541 * @param alert The alert to check.
Timothy Trippel059578e2022-02-25 20:04:55 -0800542 * @param[out] is_cause Out-param for whether this alert is a cause.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400543 * @return The result of the operation.
544 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000545OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000546dif_result_t dif_alert_handler_alert_is_cause(
Timothy Trippel936db9a2021-10-13 19:55:11 +0000547 const dif_alert_handler_t *alert_handler, dif_alert_handler_alert_t alert,
548 bool *is_cause);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400549
550/**
551 * Clears an alert from the cause vector, similar to an IRQ acknowledgement.
552 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800553 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400554 * @param alert The alert to acknowledge.
555 * @return The result of the operation.
556 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000557OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000558dif_result_t dif_alert_handler_alert_acknowledge(
Timothy Trippel936db9a2021-10-13 19:55:11 +0000559 const dif_alert_handler_t *alert_handler, dif_alert_handler_alert_t alert);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400560
561/**
Miguel Young de la Sotadd71d982020-09-17 15:40:16 -0400562 * Checks whether a local alert is one of the causes for an alert IRQ.
563 *
564 * Note that multiple alerts may be causes at the same time.
565 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800566 * @param alert_handler An alert handler handle.
Timothy Trippelacd00c72022-01-24 16:59:49 -0800567 * @param local_alert The local alert to check.
Timothy Trippel059578e2022-02-25 20:04:55 -0800568 * @param[out] is_cause Out-param for whether this alert is a cause.
Miguel Young de la Sotadd71d982020-09-17 15:40:16 -0400569 * @return The result of the operation.
570 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000571OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000572dif_result_t dif_alert_handler_local_alert_is_cause(
573 const dif_alert_handler_t *alert_handler,
Timothy Trippelacd00c72022-01-24 16:59:49 -0800574 dif_alert_handler_local_alert_t local_alert, bool *is_cause);
Miguel Young de la Sotadd71d982020-09-17 15:40:16 -0400575
576/**
577 * Clears a local alert from the cause vector, similar to an IRQ
578 * acknowledgement.
579 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800580 * @param alert_handler An alert handler handle.
Timothy Trippelacd00c72022-01-24 16:59:49 -0800581 * @param local_alert The local alert to acknowledge.
Miguel Young de la Sotadd71d982020-09-17 15:40:16 -0400582 * @return The result of the operation.
583 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000584OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000585dif_result_t dif_alert_handler_local_alert_acknowledge(
586 const dif_alert_handler_t *alert_handler,
Timothy Trippelacd00c72022-01-24 16:59:49 -0800587 dif_alert_handler_local_alert_t local_alert);
Miguel Young de la Sotadd71d982020-09-17 15:40:16 -0400588
589/**
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400590 * Checks whether software can clear escalations for this class.
591 *
592 * If `automatic_locking` has been set in a class's configuration, this
593 * function may suddenly begin returning `false` instead of `true` without
594 * software invervention, if escalation has been triggered.
595 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800596 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400597 * @param alert_class The class to check.
Timothy Trippel059578e2022-02-25 20:04:55 -0800598 * @param[out] can_clear Out-param for the clear enablement state.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400599 * @return The result of the operation.
600 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000601OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000602dif_result_t dif_alert_handler_escalation_can_clear(
603 const dif_alert_handler_t *alert_handler,
604 dif_alert_handler_class_t alert_class, bool *can_clear);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400605
606/**
607 * Disables escalation clearing for this class.
608 *
609 * This operation is similar to locking in that it cannot be undone.
610 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800611 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400612 * @param alert_class The class to disable clearing for.
613 * @return The result of the operation.
614 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000615OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000616dif_result_t dif_alert_handler_escalation_disable_clearing(
617 const dif_alert_handler_t *alert_handler,
618 dif_alert_handler_class_t alert_class);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400619
620/**
621 * Clears an on-going escalation, as well as the class accumulator.
622 *
623 * This operation can be disabled with
624 * `dif_alert_handler_escalation_disable_clearing()`.
625 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800626 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400627 * @param alert_class The class to clear an escalation for.
628 * @return The result of the operation.
629 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000630OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000631dif_result_t dif_alert_handler_escalation_clear(
632 const dif_alert_handler_t *alert_handler,
633 dif_alert_handler_class_t alert_class);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400634
635/**
636 * Gets the accumulator value for this class.
637 *
638 * This value is the number of alerts of this class that have been logged so
639 * far (more or less, since multiple alerts on the same cycle will be merged
640 * into one). Once this value equals the configured threshold, any followup
641 * alerts will immediately trigger the escalation protocol.
642 *
643 * This value is cleared as a side-effect of
644 * `dif_alert_handler_escalation_clear()`.
645 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800646 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400647 * @param alert_class The class to get the accumulator for.
Timothy Trippel059578e2022-02-25 20:04:55 -0800648 * @param[out] num_alerts Out-param for the number of alerts that have
649 * accumulated.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400650 * @return The result of the operation.
651 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000652OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000653dif_result_t dif_alert_handler_get_accumulator(
654 const dif_alert_handler_t *alert_handler,
Timothy Trippel47faff92022-01-24 23:09:56 -0800655 dif_alert_handler_class_t alert_class, uint16_t *num_alerts);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400656
657/**
658 * Gets the current value of the "escalation counter".
659 *
660 * The interpretation of this value depends on the value returned by
661 * `dif_alert_handler_class_state_get()`. If it is in the timeout state,
662 * it returns the number of cycles counted towards that cycle so far.
663 * If in an escalation phase, it returns the number of cycles that phase
664 * has been active for.
665 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800666 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400667 * @param alert_class The class to set the counter for.
Timothy Trippel059578e2022-02-25 20:04:55 -0800668 * @param[out] cycles Out-param for the counter.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400669 * @return The result of the operation.
670 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000671OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000672dif_result_t dif_alert_handler_get_escalation_counter(
673 const dif_alert_handler_t *alert_handler,
674 dif_alert_handler_class_t alert_class, uint32_t *cycles);
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400675
676/**
677 * Gets the current state of this class.
678 *
679 * See `dif_alert_handler_class_state_t` for potential states.
680 *
Timothy Trippel2e85ff12022-01-19 14:15:02 -0800681 * @param alert_handler An alert handler handle.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400682 * @param alert_class The class to get the state of
Timothy Trippel059578e2022-02-25 20:04:55 -0800683 * @param[out] state Out-param for the class state.
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400684 * @return The result of the operation.
685 */
Timothy Trippele3f8a822021-09-17 06:09:28 +0000686OT_WARN_UNUSED_RESULT
Timothy Trippel915ba132021-09-24 01:23:38 +0000687dif_result_t dif_alert_handler_get_class_state(
688 const dif_alert_handler_t *alert_handler,
689 dif_alert_handler_class_t alert_class,
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400690 dif_alert_handler_class_state_t *state);
691
692#ifdef __cplusplus
693} // extern "C"
694#endif // __cplusplus
695
696#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ALERT_HANDLER_H_