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 * Checks that there are no clock measurement errors.
96 *
97 * @param clkmgr A clkmgr DIF handle.
98 * @return Return `kInternal` if any measurement has errors, otherwise `kOk(0)`.
99 */
101status_t clkmgr_testutils_check_measurement_counts(const dif_clkmgr_t *clkmgr);
102
103/**
104 * Check all measurement enables.
105 *
106 * @param clkmgr A clkmgr DIF handle.
107 * @param expected_status The expected status of the enables.
108 * @return False if any enable status is unexpected.
109 * @return Return `kInternal` in case of errors, otherwise `kOk(res)`, where
110 * `res` is false if any enable status is unexpected`.
111 */
113status_t clkmgr_testutils_check_measurement_enables(
114 const dif_clkmgr_t *clkmgr, dif_toggle_t expected_status);
115
116/**
117 * Disable all clock measurements.
118 *
119 * @param clkmgr A clkmgr DIF handle.
120 * @return The result of the operation.
121 */
123status_t clkmgr_testutils_disable_clock_counts(const dif_clkmgr_t *clkmgr);
124
125/**
126 * Switch to use external clock and wait until the switching is done.
127 *
128 * @param clkmgr A clkmgr DIF handle.
129 * @param is_low_speed Is external clock in low speed mode or not.
130 * @return The result of the operation.
131 */
133status_t clkmgr_testutils_enable_external_clock_blocking(
134 const dif_clkmgr_t *clkmgr, bool is_low_speed);
135
136/**
137 * Verifies the given clock state.
138 *
139 * @param clkmgr A clkmgr DIF handle.
140 * @param clock_id The transactional clock ID.
141 * @param expected_state Expected clock state.
142 * @return Return `kInternal` in case of errors, otherwise `kOk(0)`.
143 */
144#define CLKMGR_TESTUTILS_CHECK_CLOCK_HINT(clkmgr, clock_id, expected_state) \
145 ({ \
146 dif_toggle_t clock_state; \
147 TRY(dif_clkmgr_hintable_clock_get_enabled(&clkmgr, clock_id, \
148 &clock_state)); \
149 TRY_CHECK(clock_state == expected_state, \
150 "Clock enabled state is (%d) and not as expected (%d).", \
151 clock_state, expected_state); \
152 OK_STATUS(); \
153 })
154
155/**
156 * Set and verifies the given clock state.
157 *
158 * Note: The clkmgr register access occurs on a clock that is circa 4 times
159 * slower than the hintable clock being manipulated, which means that
160 * reading back immediately runs the risk of the hint state not yet
161 * having taken effect.
162 * Conversely if we delay too long - say one microsecond - then the
163 * controlled IP block may have returned to its idle state.
164 *
165 * @param clkmgr A clkmgr DIF handle.
166 * @param clock_id The transactional clock ID.
167 * @param new_state Clock state to be set.
168 * @param expected_state Expected clock state.
169 * @return Return `kInternal` in case of errors, otherwise `kOk(0)`.
170 */
171#define CLKMGR_TESTUTILS_SET_AND_CHECK_CLOCK_HINT(clkmgr, clock_id, new_state, \
172 expected_state) \
173 ({ \
174 /* The hintable clock shall be read _almost_ immediately after setting it, \
175 * so we check for any errors afterwards. */ \
176 dif_result_t set_res = \
177 dif_clkmgr_hintable_clock_set_hint(&clkmgr, clock_id, new_state); \
178 dif_toggle_t hint_state; \
179 /* This read back mainly serves to introduce a sub-microsecond delay to \
180 * accommodate the fact that clkmgr register access occurs on a clock that \
181 * is circa 4 times slower than the software-hintable clock. */ \
182 dif_result_t hint_get_res = \
183 dif_clkmgr_hintable_clock_get_hint(&clkmgr, clock_id, &hint_state); \
184 /* Now check the new clock enable state. */ \
185 dif_toggle_t clock_state; \
186 dif_result_t get_res = dif_clkmgr_hintable_clock_get_enabled( \
187 &clkmgr, clock_id, &clock_state); \
188 TRY(set_res); \
189 TRY(hint_get_res); \
190 TRY(get_res); \
191 TRY_CHECK(hint_state == new_state, \
192 "Clock hint state is (%d) and not as requested (%d).", \
193 hint_state, new_state); \
194 TRY_CHECK(clock_state == expected_state, \
195 "Clock enabled state is (%d) and not as expected (%d).", \
196 clock_state, expected_state); \
197 OK_STATUS(); \
198 })
199
200#undef MODULE_ID
201
202#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_CLKMGR_TESTUTILS_H_