1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020, Linaro Limited
6 #define LOG_CATEGORY UCLASS_SCMI_AGENT
11 #include <scmi_agent.h>
12 #include <scmi_agent-uclass.h>
13 #include <scmi_protocols.h>
15 #include <asm/scmi_test.h>
16 #include <dm/device_compat.h>
19 * The sandbox SCMI agent driver simulates to some extend a SCMI message
20 * processing. It simulates few of the SCMI services for some of the
21 * SCMI protocols embedded in U-Boot. Currently:
22 * - SCMI clock protocol: emulate 2 agents each exposing few clocks
23 * - SCMI reset protocol: emulate 1 agent exposing a reset controller
24 * - SCMI voltage domain protocol: emulate 1 agent exposing 2 regulators
26 * Agent #0 simulates 2 clocks, 1 reset domain and 1 voltage domain.
27 * See IDs in scmi0_clk[]/scmi0_reset[] and "sandbox-scmi-agent@0" in test.dts.
29 * Agent #1 simulates 1 clock.
30 * See IDs in scmi1_clk[] and "sandbox-scmi-agent@1" in test.dts.
32 * All clocks and regulators are default disabled and reset controller down.
34 * This Driver exports sandbox_scmi_service_ctx() for the test sequence to
35 * get the state of the simulated services (clock state, rate, ...) and
36 * check back-end device state reflects the request send through the
37 * various uclass devices, as clocks and reset controllers.
40 #define SANDBOX_SCMI_AGENT_COUNT 2
42 static struct sandbox_scmi_clk scmi0_clk[] = {
43 { .id = 7, .rate = 1000 },
44 { .id = 3, .rate = 333 },
47 static struct sandbox_scmi_reset scmi0_reset[] = {
51 static struct sandbox_scmi_voltd scmi0_voltd[] = {
52 { .id = 0, .voltage_uv = 3300000 },
53 { .id = 1, .voltage_uv = 1800000 },
56 static struct sandbox_scmi_clk scmi1_clk[] = {
57 { .id = 1, .rate = 44 },
60 /* The list saves to simulted end devices references for test purpose */
61 struct sandbox_scmi_agent *sandbox_scmi_agent_list[SANDBOX_SCMI_AGENT_COUNT];
63 static struct sandbox_scmi_service sandbox_scmi_service_state = {
64 .agent = sandbox_scmi_agent_list,
65 .agent_count = SANDBOX_SCMI_AGENT_COUNT,
68 struct sandbox_scmi_service *sandbox_scmi_service_ctx(void)
70 return &sandbox_scmi_service_state;
73 static void debug_print_agent_state(struct udevice *dev, char *str)
75 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
77 dev_dbg(dev, "Dump sandbox_scmi_agent %u: %s\n", agent->idx, str);
78 dev_dbg(dev, " scmi%u_clk (%zu): %d/%ld, %d/%ld, %d/%ld, ...\n",
81 agent->clk_count ? agent->clk[0].enabled : -1,
82 agent->clk_count ? agent->clk[0].rate : -1,
83 agent->clk_count > 1 ? agent->clk[1].enabled : -1,
84 agent->clk_count > 1 ? agent->clk[1].rate : -1,
85 agent->clk_count > 2 ? agent->clk[2].enabled : -1,
86 agent->clk_count > 2 ? agent->clk[2].rate : -1);
87 dev_dbg(dev, " scmi%u_reset (%zu): %d, %d, ...\n",
90 agent->reset_count ? agent->reset[0].asserted : -1,
91 agent->reset_count > 1 ? agent->reset[1].asserted : -1);
92 dev_dbg(dev, " scmi%u_voltd (%zu): %u/%d, %u/%d, ...\n",
95 agent->voltd_count ? agent->voltd[0].enabled : -1,
96 agent->voltd_count ? agent->voltd[0].voltage_uv : -1,
97 agent->voltd_count ? agent->voltd[1].enabled : -1,
98 agent->voltd_count ? agent->voltd[1].voltage_uv : -1);
101 static struct sandbox_scmi_clk *get_scmi_clk_state(uint agent_id, uint clock_id)
103 struct sandbox_scmi_clk *target = NULL;
104 size_t target_count = 0;
110 target_count = ARRAY_SIZE(scmi0_clk);
114 target_count = ARRAY_SIZE(scmi1_clk);
120 for (n = 0; n < target_count; n++)
121 if (target[n].id == clock_id)
127 static struct sandbox_scmi_reset *get_scmi_reset_state(uint agent_id,
133 for (n = 0; n < ARRAY_SIZE(scmi0_reset); n++)
134 if (scmi0_reset[n].id == reset_id)
135 return scmi0_reset + n;
141 static struct sandbox_scmi_voltd *get_scmi_voltd_state(uint agent_id,
147 for (n = 0; n < ARRAY_SIZE(scmi0_voltd); n++)
148 if (scmi0_voltd[n].id == domain_id)
149 return scmi0_voltd + n;
156 * Sandbox SCMI agent ops
159 static int sandbox_scmi_clock_rate_set(struct udevice *dev,
160 struct scmi_msg *msg)
162 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
163 struct scmi_clk_rate_set_in *in = NULL;
164 struct scmi_clk_rate_set_out *out = NULL;
165 struct sandbox_scmi_clk *clk_state = NULL;
167 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
168 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
171 in = (struct scmi_clk_rate_set_in *)msg->in_msg;
172 out = (struct scmi_clk_rate_set_out *)msg->out_msg;
174 clk_state = get_scmi_clk_state(agent->idx, in->clock_id);
176 dev_err(dev, "Unexpected clock ID %u\n", in->clock_id);
178 out->status = SCMI_NOT_FOUND;
180 u64 rate = ((u64)in->rate_msb << 32) + in->rate_lsb;
182 clk_state->rate = (ulong)rate;
184 out->status = SCMI_SUCCESS;
190 static int sandbox_scmi_clock_rate_get(struct udevice *dev,
191 struct scmi_msg *msg)
193 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
194 struct scmi_clk_rate_get_in *in = NULL;
195 struct scmi_clk_rate_get_out *out = NULL;
196 struct sandbox_scmi_clk *clk_state = NULL;
198 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
199 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
202 in = (struct scmi_clk_rate_get_in *)msg->in_msg;
203 out = (struct scmi_clk_rate_get_out *)msg->out_msg;
205 clk_state = get_scmi_clk_state(agent->idx, in->clock_id);
207 dev_err(dev, "Unexpected clock ID %u\n", in->clock_id);
209 out->status = SCMI_NOT_FOUND;
211 out->rate_msb = (u32)((u64)clk_state->rate >> 32);
212 out->rate_lsb = (u32)clk_state->rate;
214 out->status = SCMI_SUCCESS;
220 static int sandbox_scmi_clock_gate(struct udevice *dev, struct scmi_msg *msg)
222 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
223 struct scmi_clk_state_in *in = NULL;
224 struct scmi_clk_state_out *out = NULL;
225 struct sandbox_scmi_clk *clk_state = NULL;
227 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
228 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
231 in = (struct scmi_clk_state_in *)msg->in_msg;
232 out = (struct scmi_clk_state_out *)msg->out_msg;
234 clk_state = get_scmi_clk_state(agent->idx, in->clock_id);
236 dev_err(dev, "Unexpected clock ID %u\n", in->clock_id);
238 out->status = SCMI_NOT_FOUND;
239 } else if (in->attributes > 1) {
240 out->status = SCMI_PROTOCOL_ERROR;
242 clk_state->enabled = in->attributes;
244 out->status = SCMI_SUCCESS;
250 static int sandbox_scmi_rd_attribs(struct udevice *dev, struct scmi_msg *msg)
252 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
253 struct scmi_rd_attr_in *in = NULL;
254 struct scmi_rd_attr_out *out = NULL;
255 struct sandbox_scmi_reset *reset_state = NULL;
257 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
258 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
261 in = (struct scmi_rd_attr_in *)msg->in_msg;
262 out = (struct scmi_rd_attr_out *)msg->out_msg;
264 reset_state = get_scmi_reset_state(agent->idx, in->domain_id);
266 dev_err(dev, "Unexpected reset domain ID %u\n", in->domain_id);
268 out->status = SCMI_NOT_FOUND;
270 memset(out, 0, sizeof(*out));
271 snprintf(out->name, sizeof(out->name), "rd%u", in->domain_id);
273 out->status = SCMI_SUCCESS;
279 static int sandbox_scmi_rd_reset(struct udevice *dev, struct scmi_msg *msg)
281 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
282 struct scmi_rd_reset_in *in = NULL;
283 struct scmi_rd_reset_out *out = NULL;
284 struct sandbox_scmi_reset *reset_state = NULL;
286 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
287 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
290 in = (struct scmi_rd_reset_in *)msg->in_msg;
291 out = (struct scmi_rd_reset_out *)msg->out_msg;
293 reset_state = get_scmi_reset_state(agent->idx, in->domain_id);
295 dev_err(dev, "Unexpected reset domain ID %u\n", in->domain_id);
297 out->status = SCMI_NOT_FOUND;
298 } else if (in->reset_state > 1) {
299 dev_err(dev, "Invalid reset domain input attribute value\n");
301 out->status = SCMI_INVALID_PARAMETERS;
303 if (in->flags & SCMI_RD_RESET_FLAG_CYCLE) {
304 if (in->flags & SCMI_RD_RESET_FLAG_ASYNC) {
305 out->status = SCMI_NOT_SUPPORTED;
307 /* Ends deasserted whatever current state */
308 reset_state->asserted = false;
309 out->status = SCMI_SUCCESS;
312 reset_state->asserted = in->flags &
313 SCMI_RD_RESET_FLAG_ASSERT;
315 out->status = SCMI_SUCCESS;
322 static int sandbox_scmi_voltd_attribs(struct udevice *dev, struct scmi_msg *msg)
324 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
325 struct scmi_voltd_attr_in *in = NULL;
326 struct scmi_voltd_attr_out *out = NULL;
327 struct sandbox_scmi_voltd *voltd_state = NULL;
329 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
330 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
333 in = (struct scmi_voltd_attr_in *)msg->in_msg;
334 out = (struct scmi_voltd_attr_out *)msg->out_msg;
336 voltd_state = get_scmi_voltd_state(agent->idx, in->domain_id);
338 dev_err(dev, "Unexpected domain ID %u\n", in->domain_id);
340 out->status = SCMI_NOT_FOUND;
342 memset(out, 0, sizeof(*out));
343 snprintf(out->name, sizeof(out->name), "regu%u", in->domain_id);
345 out->status = SCMI_SUCCESS;
351 static int sandbox_scmi_voltd_config_set(struct udevice *dev,
352 struct scmi_msg *msg)
354 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
355 struct scmi_voltd_config_set_in *in = NULL;
356 struct scmi_voltd_config_set_out *out = NULL;
357 struct sandbox_scmi_voltd *voltd_state = NULL;
359 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
360 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
363 in = (struct scmi_voltd_config_set_in *)msg->in_msg;
364 out = (struct scmi_voltd_config_set_out *)msg->out_msg;
366 voltd_state = get_scmi_voltd_state(agent->idx, in->domain_id);
368 dev_err(dev, "Unexpected domain ID %u\n", in->domain_id);
370 out->status = SCMI_NOT_FOUND;
371 } else if (in->config & ~SCMI_VOLTD_CONFIG_MASK) {
372 dev_err(dev, "Invalid config value 0x%x\n", in->config);
374 out->status = SCMI_INVALID_PARAMETERS;
375 } else if (in->config != SCMI_VOLTD_CONFIG_ON &&
376 in->config != SCMI_VOLTD_CONFIG_OFF) {
377 dev_err(dev, "Unexpected custom value 0x%x\n", in->config);
379 out->status = SCMI_INVALID_PARAMETERS;
381 voltd_state->enabled = in->config == SCMI_VOLTD_CONFIG_ON;
382 out->status = SCMI_SUCCESS;
388 static int sandbox_scmi_voltd_config_get(struct udevice *dev,
389 struct scmi_msg *msg)
391 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
392 struct scmi_voltd_config_get_in *in = NULL;
393 struct scmi_voltd_config_get_out *out = NULL;
394 struct sandbox_scmi_voltd *voltd_state = NULL;
396 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
397 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
400 in = (struct scmi_voltd_config_get_in *)msg->in_msg;
401 out = (struct scmi_voltd_config_get_out *)msg->out_msg;
403 voltd_state = get_scmi_voltd_state(agent->idx, in->domain_id);
405 dev_err(dev, "Unexpected domain ID %u\n", in->domain_id);
407 out->status = SCMI_NOT_FOUND;
409 if (voltd_state->enabled)
410 out->config = SCMI_VOLTD_CONFIG_ON;
412 out->config = SCMI_VOLTD_CONFIG_OFF;
414 out->status = SCMI_SUCCESS;
420 static int sandbox_scmi_voltd_level_set(struct udevice *dev,
421 struct scmi_msg *msg)
423 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
424 struct scmi_voltd_level_set_in *in = NULL;
425 struct scmi_voltd_level_set_out *out = NULL;
426 struct sandbox_scmi_voltd *voltd_state = NULL;
428 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
429 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
432 in = (struct scmi_voltd_level_set_in *)msg->in_msg;
433 out = (struct scmi_voltd_level_set_out *)msg->out_msg;
435 voltd_state = get_scmi_voltd_state(agent->idx, in->domain_id);
437 dev_err(dev, "Unexpected domain ID %u\n", in->domain_id);
439 out->status = SCMI_NOT_FOUND;
441 voltd_state->voltage_uv = in->voltage_level;
442 out->status = SCMI_SUCCESS;
448 static int sandbox_scmi_voltd_level_get(struct udevice *dev,
449 struct scmi_msg *msg)
451 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
452 struct scmi_voltd_level_get_in *in = NULL;
453 struct scmi_voltd_level_get_out *out = NULL;
454 struct sandbox_scmi_voltd *voltd_state = NULL;
456 if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) ||
457 !msg->out_msg || msg->out_msg_sz < sizeof(*out))
460 in = (struct scmi_voltd_level_get_in *)msg->in_msg;
461 out = (struct scmi_voltd_level_get_out *)msg->out_msg;
463 voltd_state = get_scmi_voltd_state(agent->idx, in->domain_id);
465 dev_err(dev, "Unexpected domain ID %u\n", in->domain_id);
467 out->status = SCMI_NOT_FOUND;
469 out->voltage_level = voltd_state->voltage_uv;
470 out->status = SCMI_SUCCESS;
476 static int sandbox_scmi_test_process_msg(struct udevice *dev,
477 struct scmi_msg *msg)
479 switch (msg->protocol_id) {
480 case SCMI_PROTOCOL_ID_CLOCK:
481 switch (msg->message_id) {
482 case SCMI_CLOCK_RATE_SET:
483 return sandbox_scmi_clock_rate_set(dev, msg);
484 case SCMI_CLOCK_RATE_GET:
485 return sandbox_scmi_clock_rate_get(dev, msg);
486 case SCMI_CLOCK_CONFIG_SET:
487 return sandbox_scmi_clock_gate(dev, msg);
492 case SCMI_PROTOCOL_ID_RESET_DOMAIN:
493 switch (msg->message_id) {
494 case SCMI_RESET_DOMAIN_ATTRIBUTES:
495 return sandbox_scmi_rd_attribs(dev, msg);
496 case SCMI_RESET_DOMAIN_RESET:
497 return sandbox_scmi_rd_reset(dev, msg);
502 case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
503 switch (msg->message_id) {
504 case SCMI_VOLTAGE_DOMAIN_ATTRIBUTES:
505 return sandbox_scmi_voltd_attribs(dev, msg);
506 case SCMI_VOLTAGE_DOMAIN_CONFIG_SET:
507 return sandbox_scmi_voltd_config_set(dev, msg);
508 case SCMI_VOLTAGE_DOMAIN_CONFIG_GET:
509 return sandbox_scmi_voltd_config_get(dev, msg);
510 case SCMI_VOLTAGE_DOMAIN_LEVEL_SET:
511 return sandbox_scmi_voltd_level_set(dev, msg);
512 case SCMI_VOLTAGE_DOMAIN_LEVEL_GET:
513 return sandbox_scmi_voltd_level_get(dev, msg);
518 case SCMI_PROTOCOL_ID_BASE:
519 case SCMI_PROTOCOL_ID_POWER_DOMAIN:
520 case SCMI_PROTOCOL_ID_SYSTEM:
521 case SCMI_PROTOCOL_ID_PERF:
522 case SCMI_PROTOCOL_ID_SENSOR:
523 *(u32 *)msg->out_msg = SCMI_NOT_SUPPORTED;
529 dev_err(dev, "%s(%s): Unhandled protocol_id %#x/message_id %#x\n",
530 __func__, dev->name, msg->protocol_id, msg->message_id);
532 if (msg->out_msg_sz < sizeof(u32))
535 /* Intentionnaly report unhandled IDs through the SCMI return code */
536 *(u32 *)msg->out_msg = SCMI_PROTOCOL_ERROR;
540 static int sandbox_scmi_test_remove(struct udevice *dev)
542 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
544 debug_print_agent_state(dev, "removed");
546 /* We only need to dereference the agent in the context */
547 sandbox_scmi_service_ctx()->agent[agent->idx] = NULL;
552 static int sandbox_scmi_test_probe(struct udevice *dev)
554 static const char basename[] = "sandbox-scmi-agent@";
555 struct sandbox_scmi_agent *agent = dev_get_priv(dev);
556 const size_t basename_size = sizeof(basename) - 1;
558 if (strncmp(basename, dev->name, basename_size))
561 switch (dev->name[basename_size]) {
563 *agent = (struct sandbox_scmi_agent){
566 .clk_count = ARRAY_SIZE(scmi0_clk),
567 .reset = scmi0_reset,
568 .reset_count = ARRAY_SIZE(scmi0_reset),
569 .voltd = scmi0_voltd,
570 .voltd_count = ARRAY_SIZE(scmi0_voltd),
574 *agent = (struct sandbox_scmi_agent){
577 .clk_count = ARRAY_SIZE(scmi1_clk),
581 dev_err(dev, "%s(): Unexpected agent ID %s\n",
582 __func__, dev->name + basename_size);
586 debug_print_agent_state(dev, "probed");
588 /* Save reference for tests purpose */
589 sandbox_scmi_service_ctx()->agent[agent->idx] = agent;
594 static const struct udevice_id sandbox_scmi_test_ids[] = {
595 { .compatible = "sandbox,scmi-agent" },
599 struct scmi_agent_ops sandbox_scmi_test_ops = {
600 .process_msg = sandbox_scmi_test_process_msg,
603 U_BOOT_DRIVER(sandbox_scmi_agent) = {
604 .name = "sandbox-scmi_agent",
605 .id = UCLASS_SCMI_AGENT,
606 .of_match = sandbox_scmi_test_ids,
607 .priv_auto = sizeof(struct sandbox_scmi_agent),
608 .probe = sandbox_scmi_test_probe,
609 .remove = sandbox_scmi_test_remove,
610 .ops = &sandbox_scmi_test_ops,