1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020, Linaro Limited
5 * Tests scmi_agent uclass and the SCMI drivers implemented in other
6 * uclass devices probe when a SCMI server exposes resources.
8 * Note in test.dts the protocol@10 node in scmi node. Protocol 0x10 is not
9 * implemented in U-Boot SCMI components but the implementation is expected
10 * to not complain on unknown protocol IDs, as long as it is not used. Note
11 * in test.dts tests that SCMI drivers probing does not fail for such an
12 * unknown SCMI protocol ID.
18 #include <scmi_agent.h>
19 #include <scmi_agent-uclass.h>
20 #include <scmi_protocols.h>
22 #include <asm/scmi_test.h>
23 #include <dm/device-internal.h>
25 #include <power/regulator.h>
28 static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts,
29 struct sandbox_scmi_agent *agent,
32 struct sandbox_scmi_devices *scmi_devices;
34 /* Device references to check context against test sequence */
35 scmi_devices = sandbox_scmi_devices_ctx(dev);
36 ut_assertnonnull(scmi_devices);
37 ut_asserteq(2, scmi_devices->clk_count);
38 ut_asserteq(1, scmi_devices->reset_count);
39 ut_asserteq(2, scmi_devices->regul_count);
41 /* State of the simulated SCMI server exposed */
42 ut_asserteq(3, agent->clk_count);
43 ut_assertnonnull(agent->clk);
44 ut_asserteq(1, agent->reset_count);
45 ut_assertnonnull(agent->reset);
46 ut_asserteq(2, agent->voltd_count);
47 ut_assertnonnull(agent->voltd);
52 static int load_sandbox_scmi_test_devices(struct unit_test_state *uts,
53 struct sandbox_scmi_agent **ctx,
56 struct udevice *agent_dev;
58 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
60 ut_assertnonnull(agent_dev);
62 *ctx = sandbox_scmi_agent_ctx(agent_dev);
63 ut_assertnonnull(*ctx);
66 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_scmi",
68 ut_assertnonnull(*dev);
70 return ut_assert_scmi_state_postprobe(uts, *ctx, *dev);
73 static int release_sandbox_scmi_test_devices(struct unit_test_state *uts,
77 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
83 * Test SCMI states when loading and releasing resources
84 * related to SCMI drivers.
86 static int dm_test_scmi_sandbox_agent(struct unit_test_state *uts)
88 struct sandbox_scmi_agent *ctx;
89 struct udevice *dev = NULL;
92 ret = load_sandbox_scmi_test_devices(uts, &ctx, &dev);
94 ret = release_sandbox_scmi_test_devices(uts, dev);
98 DM_TEST(dm_test_scmi_sandbox_agent, UTF_SCAN_FDT);
100 static int dm_test_scmi_base(struct unit_test_state *uts)
102 struct udevice *agent_dev, *base;
103 struct scmi_agent_priv *priv;
104 u32 version, num_agents, num_protocols, impl_version;
105 u32 attributes, agent_id;
106 u8 *vendor, *agent_name, *protocols;
110 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
112 ut_assertnonnull(agent_dev);
113 ut_assertnonnull(priv = dev_get_uclass_plat(agent_dev));
114 ut_assertnonnull(base = scmi_get_protocol(agent_dev,
115 SCMI_PROTOCOL_ID_BASE));
118 ret = scmi_base_protocol_version(base, &version);
120 ut_asserteq(priv->version, version);
122 /* protocol attributes */
123 ret = scmi_base_protocol_attrs(base, &num_agents, &num_protocols);
125 ut_asserteq(priv->num_agents, num_agents);
126 ut_asserteq(priv->num_protocols, num_protocols);
128 /* discover vendor */
129 ret = scmi_base_discover_vendor(base, &vendor);
131 ut_asserteq_str(priv->vendor, vendor);
134 /* message attributes */
135 ret = scmi_base_protocol_message_attrs(base,
136 SCMI_BASE_DISCOVER_SUB_VENDOR,
139 ut_assertok(attributes);
141 /* discover sub vendor */
142 ret = scmi_base_discover_sub_vendor(base, &vendor);
144 ut_asserteq_str(priv->sub_vendor, vendor);
148 ret = scmi_base_discover_impl_version(base, &impl_version);
150 ut_asserteq(priv->impl_version, impl_version);
152 /* discover agent (my self) */
153 ret = scmi_base_discover_agent(base, 0xffffffff, &agent_id,
156 ut_asserteq(priv->agent_id, agent_id);
157 ut_asserteq_str(priv->agent_name, agent_name);
160 /* discover protocols */
161 ret = scmi_base_discover_list_protocols(base, &protocols);
162 ut_asserteq(num_protocols, ret);
163 ut_asserteq_mem(priv->protocols, protocols, sizeof(u8) * num_protocols);
167 * NOTE: Sandbox SCMI driver handles device-0 only. It supports setting
168 * access and protocol permissions, but doesn't allow unsetting them nor
169 * resetting the configurations.
171 /* set device permissions */
172 ret = scmi_base_set_device_permissions(base, agent_id, 0,
173 SCMI_BASE_SET_DEVICE_PERMISSIONS_ACCESS);
174 ut_assertok(ret); /* SCMI_SUCCESS */
175 ret = scmi_base_set_device_permissions(base, agent_id, 1,
176 SCMI_BASE_SET_DEVICE_PERMISSIONS_ACCESS);
177 ut_asserteq(-ENOENT, ret); /* SCMI_NOT_FOUND */
178 ret = scmi_base_set_device_permissions(base, agent_id, 0, 0);
179 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
181 /* set protocol permissions */
182 ret = scmi_base_set_protocol_permissions(base, agent_id, 0,
183 SCMI_PROTOCOL_ID_CLOCK,
184 SCMI_BASE_SET_PROTOCOL_PERMISSIONS_ACCESS);
185 ut_assertok(ret); /* SCMI_SUCCESS */
186 ret = scmi_base_set_protocol_permissions(base, agent_id, 1,
187 SCMI_PROTOCOL_ID_CLOCK,
188 SCMI_BASE_SET_PROTOCOL_PERMISSIONS_ACCESS);
189 ut_asserteq(-ENOENT, ret); /* SCMI_NOT_FOUND */
190 ret = scmi_base_set_protocol_permissions(base, agent_id, 0,
191 SCMI_PROTOCOL_ID_CLOCK, 0);
192 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
194 /* reset agent configuration */
195 ret = scmi_base_reset_agent_configuration(base, agent_id, 0);
196 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
197 ret = scmi_base_reset_agent_configuration(base, agent_id,
198 SCMI_BASE_RESET_ALL_ACCESS_PERMISSIONS);
199 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
200 ret = scmi_base_reset_agent_configuration(base, agent_id, 0);
201 ut_asserteq(-EACCES, ret); /* SCMI_DENIED */
205 DM_TEST(dm_test_scmi_base, UTF_SCAN_FDT);
207 static int dm_test_scmi_cmd(struct unit_test_state *uts)
209 struct udevice *agent_dev;
213 if (!CONFIG_IS_ENABLED(CMD_SCMI))
217 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
219 ut_assertnonnull(agent_dev);
222 * Estimate the number of provided protocols.
223 * This estimation is correct as far as a corresponding
224 * protocol support is added to sandbox fake serer.
226 if (CONFIG_IS_ENABLED(POWER_DOMAIN))
228 if (CONFIG_IS_ENABLED(CLK_SCMI))
230 if (CONFIG_IS_ENABLED(RESET_SCMI))
232 if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
236 ut_assertok(run_command("scmi info", 0));
238 ut_assert_nextline("SCMI device: scmi");
239 snprintf(cmd_out, 30, " protocol version: 0x%x",
240 SCMI_BASE_PROTOCOL_VERSION);
241 ut_assert_nextline(cmd_out);
242 ut_assert_nextline(" # of agents: 2");
243 ut_assert_nextline(" 0: platform");
244 ut_assert_nextline(" > 1: OSPM");
245 snprintf(cmd_out, 30, " # of protocols: %d", num_proto);
246 ut_assert_nextline(cmd_out);
247 if (CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN))
248 ut_assert_nextline(" Power domain management");
249 if (CONFIG_IS_ENABLED(CLK_SCMI))
250 ut_assert_nextline(" Clock management");
251 if (CONFIG_IS_ENABLED(RESET_SCMI))
252 ut_assert_nextline(" Reset domain management");
253 if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
254 ut_assert_nextline(" Voltage domain management");
255 ut_assert_nextline(" vendor: U-Boot");
256 ut_assert_nextline(" sub vendor: Sandbox");
257 ut_assert_nextline(" impl version: 0x1");
259 ut_assert_console_end();
262 ut_assertok(run_command("scmi perm_dev 1 0 1", 0));
263 ut_assert_console_end();
265 ut_assert(run_command("scmi perm_dev 1 0 0", 0));
266 ut_assert_nextline("Denying access to device:0 failed (-13)");
267 ut_assert_console_end();
269 /* scmi perm_proto */
270 ut_assertok(run_command("scmi perm_proto 1 0 14 1", 0));
271 ut_assert_console_end();
273 ut_assert(run_command("scmi perm_proto 1 0 14 0", 0));
274 ut_assert_nextline("Denying access to protocol:0x14 on device:0 failed (-13)");
275 ut_assert_console_end();
278 ut_assert(run_command("scmi reset 1 1", 0));
279 ut_assert_nextline("Reset failed (-13)");
280 ut_assert_console_end();
284 DM_TEST(dm_test_scmi_cmd, UTF_SCAN_FDT | UTF_CONSOLE);
286 static int dm_test_scmi_power_domains(struct unit_test_state *uts)
288 struct sandbox_scmi_agent *agent;
289 struct sandbox_scmi_devices *scmi_devices;
290 struct udevice *agent_dev, *pwd, *dev;
291 u32 version, count, attributes, pstate;
297 if (!CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN))
301 ut_assertok(load_sandbox_scmi_test_devices(uts, &agent, &dev));
302 ut_assertnonnull(agent);
303 scmi_devices = sandbox_scmi_devices_ctx(dev);
304 ut_assertnonnull(scmi_devices);
305 ut_asserteq(2, scmi_devices->pwdom->id); /* in test.dts */
307 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
309 ut_assertnonnull(agent_dev);
310 pwd = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_POWER_DOMAIN);
311 ut_assertnonnull(pwd);
314 * SCMI Power domain management protocol interfaces
317 ret = scmi_generic_protocol_version(pwd, SCMI_PROTOCOL_ID_POWER_DOMAIN,
320 ut_asserteq(agent->pwdom_version, version);
322 /* protocol attributes */
323 ret = scmi_pwd_protocol_attrs(pwd, &count, &stats_addr, &stats_len);
325 ut_asserteq(agent->pwdom_count, count);
326 ut_asserteq(0, stats_len);
328 /* protocol message attributes */
329 ret = scmi_pwd_protocol_message_attrs(pwd, SCMI_PWD_STATE_SET,
332 ret = scmi_pwd_protocol_message_attrs(pwd, SCMI_PWD_STATE_NOTIFY,
334 ut_asserteq(-ENOENT, ret); /* the protocol not supported */
336 /* power domain attributes */
337 ret = scmi_pwd_attrs(pwd, 0, &attributes, &name);
339 ut_asserteq_str("power-domain--0", name);
342 ret = scmi_pwd_attrs(pwd, 10, &attributes, &name);
343 ut_asserteq(-ENOENT, ret); /* domain-10 doesn't exist */
345 /* power domain state set/get */
346 ret = scmi_pwd_state_set(pwd, 0, 0, 0);
348 ret = scmi_pwd_state_get(pwd, 0, &pstate);
350 ut_asserteq(0, pstate); /* ON */
352 ret = scmi_pwd_state_set(pwd, 0, 0, SCMI_PWD_PSTATE_TYPE_LOST);
354 ret = scmi_pwd_state_get(pwd, 0, &pstate);
356 ut_asserteq(SCMI_PWD_PSTATE_TYPE_LOST, pstate); /* OFF */
358 ret = scmi_pwd_state_set(pwd, 0, 10, 0);
359 ut_asserteq(-ENOENT, ret);
361 /* power domain name get */
362 ret = scmi_pwd_name_get(pwd, 0, &name);
364 ut_asserteq_str("power-domain--0-extended", name);
367 ret = scmi_pwd_name_get(pwd, 10, &name);
368 ut_asserteq(-ENOENT, ret); /* domain-10 doesn't exist */
371 * U-Boot driver model interfaces
373 /* power_domain_on */
374 ret = power_domain_on(scmi_devices->pwdom);
376 ret = scmi_pwd_state_get(pwd, scmi_devices->pwdom->id, &pstate);
378 ut_asserteq(0, pstate); /* ON */
380 /* power_domain_off */
381 ret = power_domain_off(scmi_devices->pwdom);
383 ret = scmi_pwd_state_get(pwd, scmi_devices->pwdom->id, &pstate);
385 ut_asserteq(SCMI_PWD_PSTATE_TYPE_LOST, pstate); /* OFF */
387 return release_sandbox_scmi_test_devices(uts, dev);
389 DM_TEST(dm_test_scmi_power_domains, UTF_SCAN_FDT);
391 static int dm_test_scmi_clocks(struct unit_test_state *uts)
393 struct sandbox_scmi_agent *agent;
394 struct sandbox_scmi_devices *scmi_devices;
395 struct udevice *agent_dev, *clock_dev, *dev;
399 if (!CONFIG_IS_ENABLED(CLK_SCMI))
402 ret = load_sandbox_scmi_test_devices(uts, &agent, &dev);
406 scmi_devices = sandbox_scmi_devices_ctx(dev);
407 ut_assertnonnull(scmi_devices);
409 /* Sandbox SCMI clock protocol has its own channel */
410 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
412 ut_assertnonnull(agent_dev);
413 clock_dev = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_CLOCK);
414 ut_assertnonnull(clock_dev);
415 ut_asserteq(0x14, sandbox_scmi_channel_id(clock_dev));
417 /* Test SCMI clocks rate manipulation */
418 ut_asserteq(333, agent->clk[0].rate);
419 ut_asserteq(200, agent->clk[1].rate);
420 ut_asserteq(1000, agent->clk[2].rate);
422 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
423 ut_asserteq(333, clk_get_rate(&scmi_devices->clk[1]));
425 ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088);
426 ut_assert(!ret_dev || ret_dev == 1088);
428 ut_asserteq(1088, agent->clk[0].rate);
429 ut_asserteq(200, agent->clk[1].rate);
430 ut_asserteq(1000, agent->clk[2].rate);
432 ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0]));
433 ut_asserteq(1088, clk_get_rate(&scmi_devices->clk[1]));
435 /* restore original rate for further tests */
436 ret_dev = clk_set_rate(&scmi_devices->clk[1], 333);
437 ut_assert(!ret_dev || ret_dev == 333);
439 /* Test SCMI clocks gating manipulation */
440 ut_assert(!agent->clk[0].enabled);
441 ut_assert(!agent->clk[1].enabled);
442 ut_assert(!agent->clk[2].enabled);
444 ut_asserteq(0, clk_enable(&scmi_devices->clk[1]));
446 ut_assert(agent->clk[0].enabled);
447 ut_assert(!agent->clk[1].enabled);
448 ut_assert(!agent->clk[2].enabled);
450 ut_assertok(clk_disable(&scmi_devices->clk[1]));
452 ut_assert(!agent->clk[0].enabled);
453 ut_assert(!agent->clk[1].enabled);
454 ut_assert(!agent->clk[2].enabled);
456 return release_sandbox_scmi_test_devices(uts, dev);
458 DM_TEST(dm_test_scmi_clocks, UTF_SCAN_FDT);
460 static int dm_test_scmi_resets(struct unit_test_state *uts)
462 struct sandbox_scmi_agent *agent;
463 struct sandbox_scmi_devices *scmi_devices;
464 struct udevice *agent_dev, *reset_dev, *dev = NULL;
467 if (!CONFIG_IS_ENABLED(RESET_SCMI))
470 ret = load_sandbox_scmi_test_devices(uts, &agent, &dev);
474 scmi_devices = sandbox_scmi_devices_ctx(dev);
475 ut_assertnonnull(scmi_devices);
477 /* Sandbox SCMI reset protocol doesn't have its own channel */
478 ut_assertok(uclass_get_device_by_name(UCLASS_SCMI_AGENT, "scmi",
480 ut_assertnonnull(agent_dev);
481 reset_dev = scmi_get_protocol(agent_dev, SCMI_PROTOCOL_ID_RESET_DOMAIN);
482 ut_assertnonnull(reset_dev);
483 ut_asserteq(0x0, sandbox_scmi_channel_id(reset_dev));
485 /* Test SCMI resect controller manipulation */
486 ut_assert(!agent->reset[0].asserted);
488 ut_assertok(reset_assert(&scmi_devices->reset[0]));
489 ut_assert(agent->reset[0].asserted);
491 ut_assertok(reset_deassert(&scmi_devices->reset[0]));
492 ut_assert(!agent->reset[0].asserted);
494 return release_sandbox_scmi_test_devices(uts, dev);
496 DM_TEST(dm_test_scmi_resets, UTF_SCAN_FDT);
498 static int dm_test_scmi_voltage_domains(struct unit_test_state *uts)
500 struct sandbox_scmi_agent *agent;
501 struct sandbox_scmi_devices *scmi_devices;
502 struct dm_regulator_uclass_plat *uc_pdata;
504 struct udevice *regul0_dev;
506 if (!CONFIG_IS_ENABLED(DM_REGULATOR_SCMI))
509 ut_assertok(load_sandbox_scmi_test_devices(uts, &agent, &dev));
511 scmi_devices = sandbox_scmi_devices_ctx(dev);
512 ut_assertnonnull(scmi_devices);
514 /* Set/Get an SCMI voltage domain level */
515 regul0_dev = scmi_devices->regul[0];
516 ut_assert(regul0_dev);
518 uc_pdata = dev_get_uclass_plat(regul0_dev);
521 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->min_uV));
522 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->min_uV);
524 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->min_uV);
526 ut_assertok(regulator_set_value(regul0_dev, uc_pdata->max_uV));
527 ut_asserteq(agent->voltd[0].voltage_uv, uc_pdata->max_uV);
529 ut_assert(regulator_get_value(regul0_dev) == uc_pdata->max_uV);
531 /* Enable/disable SCMI voltage domains */
532 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
533 ut_assertok(regulator_set_enable(scmi_devices->regul[1], false));
534 ut_assert(!agent->voltd[0].enabled);
535 ut_assert(!agent->voltd[1].enabled);
537 ut_assertok(regulator_set_enable(scmi_devices->regul[0], true));
538 ut_assert(agent->voltd[0].enabled);
539 ut_assert(!agent->voltd[1].enabled);
541 ut_assertok(regulator_set_enable(scmi_devices->regul[1], true));
542 ut_assert(agent->voltd[0].enabled);
543 ut_assert(agent->voltd[1].enabled);
545 ut_assertok(regulator_set_enable(scmi_devices->regul[0], false));
546 ut_assert(!agent->voltd[0].enabled);
547 ut_assert(agent->voltd[1].enabled);
549 return release_sandbox_scmi_test_devices(uts, dev);
551 DM_TEST(dm_test_scmi_voltage_domains, UTF_SCAN_FDT);