aboutsummaryrefslogtreecommitdiff
path: root/test/dm/scmi.c
blob: be60b44b3b07bb6a3184b6b11133b9d987e1e32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020, Linaro Limited
 *
 * Tests scmi_agent uclass and the SCMI drivers implemented in other
 * uclass devices probe when a SCMI server exposes resources.
 *
 * Note in test.dts the protocol@10 node in agent 1. Protocol 0x10 is not
 * implemented in U-Boot SCMI components but the implementation is exepected
 * to not complain on unknown protocol IDs, as long as it is not used. Note
 * in test.dts tests that SCMI drivers probing does not fail for such an
 * unknown SCMI protocol ID.
 */

#include <common.h>
#include <clk.h>
#include <dm.h>
#include <reset.h>
#include <asm/scmi_test.h>
#include <dm/device-internal.h>
#include <dm/test.h>
#include <linux/kconfig.h>
#include <test/ut.h>

static int ut_assert_scmi_state_preprobe(struct unit_test_state *uts)
{
	struct sandbox_scmi_service *scmi_ctx = sandbox_scmi_service_ctx();

	ut_assertnonnull(scmi_ctx);
	if (scmi_ctx->agent_count)
		ut_asserteq(2, scmi_ctx->agent_count);

	return 0;
}

static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts,
					  struct udevice *dev)
{
	struct sandbox_scmi_devices *scmi_devices;
	struct sandbox_scmi_service *scmi_ctx;

	/* Device references to check context against test sequence */
	scmi_devices = sandbox_scmi_devices_ctx(dev);

	ut_assertnonnull(scmi_devices);
	if (IS_ENABLED(CONFIG_CLK_SCMI))
		ut_asserteq(3, scmi_devices->clk_count);
	if (IS_ENABLED(CONFIG_RESET_SCMI))
		ut_asserteq(1, scmi_devices->reset_count);

	/* State of the simulated SCMI server exposed */
	scmi_ctx = sandbox_scmi_service_ctx();

	ut_asserteq(2, scmi_ctx->agent_count);

	ut_assertnonnull(scmi_ctx->agent[0]);
	ut_asserteq(2, scmi_ctx->agent[0]->clk_count);
	ut_assertnonnull(scmi_ctx->agent[0]->clk);
	ut_asserteq(1, scmi_ctx->agent[0]->reset_count);
	ut_assertnonnull(scmi_ctx->agent[0]->reset);

	ut_assertnonnull(scmi_ctx->agent[1]);
	ut_assertnonnull(scmi_ctx->agent[1]->clk);
	ut_asserteq(1, scmi_ctx->agent[1]->clk_count);

	return 0;
}

static int load_sandbox_scmi_test_devices(struct unit_test_state *uts,
					  struct udevice **dev)
{
	int ret;

	ret = ut_assert_scmi_state_preprobe(uts);
	if (ret)
		return ret;

	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_scmi",
					      dev));
	ut_assertnonnull(*dev);

	return ut_assert_scmi_state_postprobe(uts, *dev);
}

static int release_sandbox_scmi_test_devices(struct unit_test_state *uts,
					     struct udevice *dev)
{
	ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));

	/* Not sure test devices are fully removed, agent may not be visible */
	return 0;
}

/*
 * Test SCMI states when loading and releasing resources
 * related to SCMI drivers.
 */
static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts)
{
	struct udevice *dev = NULL;
	int ret;

	ret = load_sandbox_scmi_test_devices(uts, &dev);
	if (!ret)
		ret = release_sandbox_scmi_test_devices(uts, dev);

	return ret;
}

DM_TEST(dm_test_scmi_sandbox_agent, UT_TESTF_SCAN_FDT);

static int dm_test_scmi_clocks(struct unit_test_state *uts)
{
	struct sandbox_scmi_devices *scmi_devices;
	struct sandbox_scmi_service *scmi_ctx;
	struct udevice *dev = NULL;
	int ret_dev;
	int ret;

	if (!IS_ENABLED(CONFIG_CLK_SCMI))
		return 0;

	ret = load_sandbox_scmi_test_devices(uts, &dev);
	if (ret)
		return ret;

	scmi_devices = sandbox_scmi_devices_ctx(dev);
	scmi_ctx = sandbox_scmi_service_ctx();

	/* Test SCMI clocks rate manipulation */
	ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
	ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1]));
	ut_asserteq(44, clk_get_rate(&scmi_devices->clk[2]));

	ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088);
	ut_assert(!ret_dev || ret_dev == 1088);

	ut_asserteq(1000, scmi_ctx->agent[0]->clk[0].rate);
	ut_asserteq(1088, scmi_ctx->agent[0]->clk[1].rate);
	ut_asserteq(44, scmi_ctx->agent[1]->clk[0].rate);

	ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
	ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1]));
	ut_asserteq(44, clk_get_rate(&scmi_devices->clk[2]));

	/* restore original rate for further tests */
	ret_dev = clk_set_rate(&scmi_devices->clk[1], 333);
	ut_assert(!ret_dev || ret_dev == 333);

	/* Test SCMI clocks gating manipulation */
	ut_assert(!scmi_ctx->agent[0]->clk[0].enabled);
	ut_assert(!scmi_ctx->agent[0]->clk[1].enabled);
	ut_assert(!scmi_ctx->agent[1]->clk[0].enabled);

	ut_asserteq(0, clk_enable(&scmi_devices->clk[1]));
	ut_asserteq(0, clk_enable(&scmi_devices->clk[2]));

	ut_assert(!scmi_ctx->agent[0]->clk[0].enabled);
	ut_assert(scmi_ctx->agent[0]->clk[1].enabled);
	ut_assert(scmi_ctx->agent[1]->clk[0].enabled);

	ut_assertok(clk_disable(&scmi_devices->clk[1]));
	ut_assertok(clk_disable(&scmi_devices->clk[2]));

	ut_assert(!scmi_ctx->agent[0]->clk[0].enabled);
	ut_assert(!scmi_ctx->agent[0]->clk[1].enabled);
	ut_assert(!scmi_ctx->agent[1]->clk[0].enabled);

	return release_sandbox_scmi_test_devices(uts, dev);
}

DM_TEST(dm_test_scmi_clocks, UT_TESTF_SCAN_FDT);

static int dm_test_scmi_resets(struct unit_test_state *uts)
{
	struct sandbox_scmi_devices *scmi_devices;
	struct sandbox_scmi_service *scmi_ctx;
	struct udevice *dev = NULL;
	int ret;

	if (!IS_ENABLED(CONFIG_RESET_SCMI))
		return 0;

	ret = load_sandbox_scmi_test_devices(uts, &dev);
	if (ret)
		return ret;

	scmi_devices = sandbox_scmi_devices_ctx(dev);
	scmi_ctx = sandbox_scmi_service_ctx();

	/* Test SCMI resect controller manipulation */
	ut_assert(!scmi_ctx->agent[0]->reset[0].asserted)

	ut_assertok(reset_assert(&scmi_devices->reset[0]));
	ut_assert(scmi_ctx->agent[0]->reset[0].asserted)

	ut_assertok(reset_deassert(&scmi_devices->reset[0]));
	ut_assert(!scmi_ctx->agent[0]->reset[0].asserted);

	return release_sandbox_scmi_test_devices(uts, dev);
}

DM_TEST(dm_test_scmi_resets, UT_TESTF_SCAN_FDT);