Pavona Software APIs
clkmgr_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_CLKMGR_TESTUTILS_H_
6#define OPENTITAN_SW_DEVICE_LIB_TESTING_CLKMGR_TESTUTILS_H_
7
8#include "sw/device/lib/base/status.h"
11#include "sw/device/lib/testing/test_framework/check.h"
12
13#define MODULE_ID MAKE_MODULE_ID('c', 'm', 'h')
14
15/**
16 * Returns the transactional block's clock status.
17 *
18 * @param clkmgr A clkmgr DIF handle.
19 * @param params clkmgr hardware instance parameters.
20 * @param clock The transactional clock ID.
21 * @return The transactional block's clock status.
22 */
23static inline bool clkmgr_testutils_get_trans_clock_status(
24 const dif_clkmgr_t *clkmgr, dif_clkmgr_hintable_clock_t clock) {
25 dif_toggle_t state;
26 dif_result_t res =
27 dif_clkmgr_hintable_clock_get_enabled(clkmgr, clock, &state);
28 return res == kDifOk && state == kDifToggleEnabled;
29}
30
31/**
32 * Verifies clock gating for transactional clocks.
33 *
34 * For a transactional block, the clock is gated only if the software enables
35 * the clock gating AND the block is idle. This function sets the clock gating
36 * hint bit to 0 and spinwaits until the clock value matches the expected. It
37 * sets the hint back to 1 afterwards. Inlined due to latency-sensitivity.
38 *
39 * @param clkmgr A clkmgr DIF handle.
40 * @param clock The transactional clock ID.
41 * @param exp_clock_enabled Expected clock status.
42 * @param timeout_usec Timeout in microseconds.
43 * @return The result of the operation.
44 */
46inline status_t clkmgr_testutils_check_trans_clock_gating(
47 const dif_clkmgr_t *clkmgr, dif_clkmgr_hintable_clock_t clock,
48 bool exp_clock_enabled, uint32_t timeout_usec) {
49 TRY(dif_clkmgr_hintable_clock_set_hint(clkmgr, clock, 0x0));
50
51 IBEX_TRY_SPIN_FOR(clkmgr_testutils_get_trans_clock_status(clkmgr, clock) ==
52 exp_clock_enabled,
53 timeout_usec);
54
55 TRY(dif_clkmgr_hintable_clock_set_hint(clkmgr, clock, 0x1));
56 return OK_STATUS();
57}
58
59/**
60 * Enables clock measurements.
61 *
62 * This enables measurements with lo and hi count bounds for a given clock.
63 *
64 * @param clkmgr A clkmgr DIF handle.
65 * @param clock The clock to be measured.
66 * @param lo_threshold Expected minimum cycle count.
67 * @param hi_threshold Expected maximum cycle count.
68 * @return The result of the operation.
69 */
71status_t clkmgr_testutils_enable_clock_count(const dif_clkmgr_t *clkmgr,
72 dif_clkmgr_measure_clock_t clock,
73 uint32_t lo_threshold,
74 uint32_t hi_threshold);
75
76/**
77 * Enables all clock measurements with expected thresholds.
78 *
79 * If jitter is disabled the thresholds are configured tightly.
80 * If jitter is enabled the min threshold allows more variability.
81 *
82 * @param clkmgr A clkmgr DIF handle.
83 * @param jitter_enabled If true relax the min threshold.
84 * @param external_clk If true the external clock is enabled.
85 * @param low_speed If true and external clock is enabled, the external
86 * clock is running at 48 Mhz.
87 * @return The result of the operation.
88 */
90status_t clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
91 const dif_clkmgr_t *clkmgr, bool jitter_enabled, bool external_clk,
92 bool low_speed);
93
94/**
95 * Enables all clock measurements with thresholds that cause failures.
96 *
97 * Configure the counters to trigger an error by setting them correctly
98 * and then halving each of the thresholds. Triggering errors can also be
99 * done by configuring the clock thresholds for external clock without
100 * configuring the external clock to be active; however, that is not
101 * possible for tops without support for external clocks, like dragonfly.
102
103 * @param clkmgr A clkmgr DIF handle.
104 * @return The result of the operation.
105 */
107status_t clkmgr_testutils_enable_clock_counts_with_failing_thresholds(
108 const dif_clkmgr_t *clkmgr);
109
110/**
111 * Checks that there are no clock measurement errors.
112 *
113 * @param clkmgr A clkmgr DIF handle.
114 * @return Return `kInternal` if any measurement has errors, otherwise `kOk(0)`.
115 */
117status_t clkmgr_testutils_check_measurement_counts(const dif_clkmgr_t *clkmgr);
118
119/**
120 * Check all measurement enables.
121 *
122 * @param clkmgr A clkmgr DIF handle.
123 * @param expected_status The expected status of the enables.
124 * @return False if any enable status is unexpected.
125 * @return Return `kInternal` in case of errors, otherwise `kOk(res)`, where
126 * `res` is false if any enable status is unexpected`.
127 */
129status_t clkmgr_testutils_check_measurement_enables(
130 const dif_clkmgr_t *clkmgr, dif_toggle_t expected_status);
131
132/**
133 * Disable all clock measurements.
134 *
135 * @param clkmgr A clkmgr DIF handle.
136 * @return The result of the operation.
137 */
139status_t clkmgr_testutils_disable_clock_counts(const dif_clkmgr_t *clkmgr);
140
141/**
142 * Switch to use external clock and wait until the switching is done.
143 *
144 * @param clkmgr A clkmgr DIF handle.
145 * @param is_low_speed Is external clock in low speed mode or not.
146 * @return The result of the operation.
147 */
149status_t clkmgr_testutils_enable_external_clock_blocking(
150 const dif_clkmgr_t *clkmgr, bool is_low_speed);
151
152/**
153 * Verifies the given clock state.
154 *
155 * @param clkmgr A clkmgr DIF handle.
156 * @param clock_id The transactional clock ID.
157 * @param expected_state Expected clock state.
158 * @return Return `kInternal` in case of errors, otherwise `kOk(0)`.
159 */
160#define CLKMGR_TESTUTILS_CHECK_CLOCK_HINT(clkmgr, clock_id, expected_state) \
161 ({ \
162 dif_toggle_t clock_state; \
163 TRY(dif_clkmgr_hintable_clock_get_enabled(&clkmgr, clock_id, \
164 &clock_state)); \
165 TRY_CHECK(clock_state == expected_state, \
166 "Clock enabled state is (%d) and not as expected (%d).", \
167 clock_state, expected_state); \
168 OK_STATUS(); \
169 })
170
171/**
172 * Set and verifies the given clock state.
173 *
174 * Note: The clkmgr register access occurs on a clock that is circa 4 times
175 * slower than the hintable clock being manipulated, which means that
176 * reading back immediately runs the risk of the hint state not yet
177 * having taken effect.
178 * Conversely if we delay too long - say one microsecond - then the
179 * controlled IP block may have returned to its idle state.
180 *
181 * @param clkmgr A clkmgr DIF handle.
182 * @param clock_id The transactional clock ID.
183 * @param new_state Clock state to be set.
184 * @param expected_state Expected clock state.
185 * @return Return `kInternal` in case of errors, otherwise `kOk(0)`.
186 */
187#define CLKMGR_TESTUTILS_SET_AND_CHECK_CLOCK_HINT(clkmgr, clock_id, new_state, \
188 expected_state) \
189 ({ \
190 /* The hintable clock shall be read _almost_ immediately after setting it, \
191 * so we check for any errors afterwards. */ \
192 dif_result_t set_res = \
193 dif_clkmgr_hintable_clock_set_hint(&clkmgr, clock_id, new_state); \
194 dif_toggle_t hint_state; \
195 /* This read back mainly serves to introduce a sub-microsecond delay to \
196 * accommodate the fact that clkmgr register access occurs on a clock that \
197 * is circa 4 times slower than the software-hintable clock. */ \
198 dif_result_t hint_get_res = \
199 dif_clkmgr_hintable_clock_get_hint(&clkmgr, clock_id, &hint_state); \
200 /* Now check the new clock enable state. */ \
201 dif_toggle_t clock_state; \
202 dif_result_t get_res = dif_clkmgr_hintable_clock_get_enabled( \
203 &clkmgr, clock_id, &clock_state); \
204 TRY(set_res); \
205 TRY(hint_get_res); \
206 TRY(get_res); \
207 TRY_CHECK(hint_state == new_state, \
208 "Clock hint state is (%d) and not as requested (%d).", \
209 hint_state, new_state); \
210 TRY_CHECK(clock_state == expected_state, \
211 "Clock enabled state is (%d) and not as expected (%d).", \
212 clock_state, expected_state); \
213 OK_STATUS(); \
214 })
215
216#undef MODULE_ID
217
218#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_CLKMGR_TESTUTILS_H_