[PATCH 1/2] clk: scmi: register all scmi clock by name with CCF

From: Patrick Delaunay patrick.delaunay@st.com
This patch implements SCMI APIs to retrieve the number and the name of SCMI clocks using SCMI_PROTOCOL_ATTRIBUTES messages.
Signed-off-by: Gabriel Fernandez gabriel.fernandez@st.com Signed-off-by: Patrick Delaunay patrick.delaunay@st.com Signed-off-by: Etienne Carriere etienne.carriere@linaro.org --- drivers/clk/clk_scmi.c | 101 +++++++++++++++++++++++++++++++++++++++ include/scmi_protocols.h | 43 +++++++++++++++++ 2 files changed, 144 insertions(+)
diff --git a/drivers/clk/clk_scmi.c b/drivers/clk/clk_scmi.c index 93a4819501..2a4c10a427 100644 --- a/drivers/clk/clk_scmi.c +++ b/drivers/clk/clk_scmi.c @@ -8,6 +8,50 @@ #include <scmi_agent.h> #include <scmi_protocols.h> #include <asm/types.h> +#include <linux/clk-provider.h> + +static u16 scmi_clk_get_num_clock(struct udevice *dev) +{ + struct scmi_clk_protocol_attr_out out; + struct scmi_msg msg = { + .protocol_id = SCMI_PROTOCOL_ID_CLOCK, + .message_id = SCMI_PROTOCOL_ATTRIBUTES, + .out_msg = (u8 *)&out, + .out_msg_sz = sizeof(out), + }; + int ret; + + ret = devm_scmi_process_msg(dev->parent, &msg); + if (ret) + return ret; + + return out.attributes & SCMI_CLK_PROTO_ATTR_COUNT_MASK; +} + +static int scmi_clk_get_attibute(struct udevice *dev, int clkid, char **name) +{ + struct scmi_clk_attribute_in in = { + .clock_id = clkid, + }; + struct scmi_clk_attribute_out out; + struct scmi_msg msg = { + .protocol_id = SCMI_PROTOCOL_ID_CLOCK, + .message_id = SCMI_CLOCK_ATTRIBUTES, + .in_msg = (u8 *)&in, + .in_msg_sz = sizeof(in), + .out_msg = (u8 *)&out, + .out_msg_sz = sizeof(out), + }; + int ret; + + ret = devm_scmi_process_msg(dev->parent, &msg); + if (ret) + return ret; + + *name = out.clock_name; + + return 0; +}
static int scmi_clk_gate(struct clk *clk, int enable) { @@ -85,6 +129,62 @@ static ulong scmi_clk_set_rate(struct clk *clk, ulong rate) return scmi_clk_get_rate(clk); }
+static struct clk *scmi_clk_register(struct udevice *dev, const char *name) +{ + struct clk *clk; + int ret; + + clk = kzalloc(sizeof(*clk), GFP_KERNEL); + if (!clk) + return ERR_PTR(-ENOMEM); + + ret = clk_register(clk, dev->driver->name, name, dev->name); + if (ret) { + kfree(clk); + return ERR_PTR(ret); + } + + return clk; +} + +static int scmi_clk_probe(struct udevice *dev) +{ + struct clk *clk; + int num_clocks; + int i; + + if (!CONFIG_IS_ENABLED(CLK_CCF)) + return 0; + + /* register CCF children: CLK UCLASS, no probed again */ + if (device_get_uclass_id(dev->parent) == UCLASS_CLK) + return 0; + + num_clocks = scmi_clk_get_num_clock(dev); + if (!num_clocks) + return 0; + + for (i = 0; i < num_clocks; i++) { + char *name; + + if (!scmi_clk_get_attibute(dev, i, &name)) { + char *clock_name = strdup(name); + + clk = scmi_clk_register(dev, clock_name); + if (!IS_ERR(clk)) { + clk->id = i; + clk_request(dev, clk); + } else { + free(clock_name); + + return PTR_ERR(clk); + } + } + } + + return 0; +} + static const struct clk_ops scmi_clk_ops = { .enable = scmi_clk_enable, .disable = scmi_clk_disable, @@ -96,4 +196,5 @@ U_BOOT_DRIVER(scmi_clock) = { .name = "scmi_clk", .id = UCLASS_CLK, .ops = &scmi_clk_ops, + .probe = &scmi_clk_probe, }; diff --git a/include/scmi_protocols.h b/include/scmi_protocols.h index 2db71697e8..3fc8ec95ef 100644 --- a/include/scmi_protocols.h +++ b/include/scmi_protocols.h @@ -40,22 +40,65 @@ enum scmi_status_code { SCMI_PROTOCOL_ERROR = -10, };
+/* + * Generic message IDs + */ +enum scmi_discovery_id { + SCMI_PROTOCOL_VERSION = 0x0, + SCMI_PROTOCOL_ATTRIBUTES = 0x1, + SCMI_PROTOCOL_MESSAGE_ATTRIBUTES = 0x2, +}; + /* * SCMI Clock Protocol */
enum scmi_clock_message_id { + SCMI_CLOCK_ATTRIBUTES = 0x3, SCMI_CLOCK_RATE_SET = 0x5, SCMI_CLOCK_RATE_GET = 0x6, SCMI_CLOCK_CONFIG_SET = 0x7, };
+#define SCMI_CLK_PROTO_ATTR_COUNT_MASK GENMASK(15, 0) #define SCMI_CLK_RATE_ASYNC_NOTIFY BIT(0) #define SCMI_CLK_RATE_ASYNC_NORESP (BIT(0) | BIT(1)) #define SCMI_CLK_RATE_ROUND_DOWN 0 #define SCMI_CLK_RATE_ROUND_UP BIT(2) #define SCMI_CLK_RATE_ROUND_CLOSEST BIT(3)
+#define SCMI_CLOCK_NAME_LENGTH_MAX 16 + +/** + * struct scmi_clk_get_nb_out - Response for SCMI_PROTOCOL_ATTRIBUTES command + * @status: SCMI command status + * @attributes: Attributes of the clock protocol, mainly number of clocks exposed + */ +struct scmi_clk_protocol_attr_out { + s32 status; + u32 attributes; +}; + +/** + * struct scmi_clk_attribute_in - Message payload for SCMI_CLOCK_ATTRIBUTES command + * @clock_id: SCMI clock ID + */ +struct scmi_clk_attribute_in { + u32 clock_id; +}; + +/** + * struct scmi_clk_get_nb_out - Response payload for SCMI_CLOCK_ATTRIBUTES command + * @status: SCMI command status + * @attributes: clock attributes + * @clock_name: name of the clock + */ +struct scmi_clk_attribute_out { + s32 status; + u32 attributes; + char clock_name[SCMI_CLOCK_NAME_LENGTH_MAX]; +}; + /** * struct scmi_clk_state_in - Message payload for CLOCK_CONFIG_SET command * @clock_id: SCMI clock ID

Since SCMI clock are discovered because of integration in the CCF, update SCMI emulation in sandbox accordingly. Sandbox must emulate all clocks exposed by SCMI server since CCF clock discovery will query all of them even if some clocks have no consumer. This change adds clock discovery support in the sandbox and changes clock IDs so that less clock shall be managed as each SCMI clock ID is actually an index identifier.
For consistency, update also sandbox SCMI reset controller test to conform with the IDs being also indices of exposed resources as per SCMI specification.
Signed-off-by: Etienne Carriere etienne.carriere@linaro.org --- arch/sandbox/dts/test.dts | 4 +- arch/sandbox/include/asm/scmi_test.h | 2 - drivers/firmware/scmi/sandbox-scmi_agent.c | 102 ++++++++++++++++++--- test/dm/scmi.c | 29 ++++-- 4 files changed, 113 insertions(+), 24 deletions(-)
diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts index fe26ced31d..91e488239b 100644 --- a/arch/sandbox/dts/test.dts +++ b/arch/sandbox/dts/test.dts @@ -1346,8 +1346,8 @@
sandbox_scmi { compatible = "sandbox,scmi-devices"; - clocks = <&clk_scmi0 7>, <&clk_scmi0 3>, <&clk_scmi1 1>; - resets = <&reset_scmi0 3>; + clocks = <&clk_scmi0 2>, <&clk_scmi0 0>, <&clk_scmi1 0>; + resets = <&reset_scmi0 0>; regul0-supply = <®ul0_scmi0>; regul1-supply = <®ul1_scmi0>; }; diff --git a/arch/sandbox/include/asm/scmi_test.h b/arch/sandbox/include/asm/scmi_test.h index 2930e686d7..bce18deeea 100644 --- a/arch/sandbox/include/asm/scmi_test.h +++ b/arch/sandbox/include/asm/scmi_test.h @@ -17,7 +17,6 @@ struct sandbox_scmi_service; * @rate: Clock rate in Hertz */ struct sandbox_scmi_clk { - uint id; bool enabled; ulong rate; }; @@ -28,7 +27,6 @@ struct sandbox_scmi_clk { * @asserted: Reset control state: true if asserted, false if desasserted */ struct sandbox_scmi_reset { - uint id; bool asserted; };
diff --git a/drivers/firmware/scmi/sandbox-scmi_agent.c b/drivers/firmware/scmi/sandbox-scmi_agent.c index 4b968205c2..dee9e8fb58 100644 --- a/drivers/firmware/scmi/sandbox-scmi_agent.c +++ b/drivers/firmware/scmi/sandbox-scmi_agent.c @@ -27,7 +27,7 @@ * See IDs in scmi0_clk[]/scmi0_reset[] and "sandbox-scmi-agent@0" in test.dts. * * Agent #1 simulates 1 clock. - * See IDs in scmi1_clk[] and "sandbox-scmi-agent@1" in test.dts. + * See scmi1_clk[] and "sandbox-scmi-agent@1" in test.dts. * * All clocks and regulators are default disabled and reset controller down. * @@ -40,12 +40,13 @@ #define SANDBOX_SCMI_AGENT_COUNT 2
static struct sandbox_scmi_clk scmi0_clk[] = { - { .id = 7, .rate = 1000 }, - { .id = 3, .rate = 333 }, + { .rate = 333 }, + { .rate = 200 }, + { .rate = 1000 }, };
static struct sandbox_scmi_reset scmi0_reset[] = { - { .id = 3 }, + { .asserted = false }, };
static struct sandbox_scmi_voltd scmi0_voltd[] = { @@ -54,7 +55,7 @@ static struct sandbox_scmi_voltd scmi0_voltd[] = { };
static struct sandbox_scmi_clk scmi1_clk[] = { - { .id = 1, .rate = 44 }, + { .rate = 44 }, };
/* The list saves to simulted end devices references for test purpose */ @@ -102,7 +103,6 @@ static struct sandbox_scmi_clk *get_scmi_clk_state(uint agent_id, uint clock_id) { struct sandbox_scmi_clk *target = NULL; size_t target_count = 0; - size_t n;
switch (agent_id) { case 0: @@ -117,9 +117,8 @@ static struct sandbox_scmi_clk *get_scmi_clk_state(uint agent_id, uint clock_id) return NULL; }
- for (n = 0; n < target_count; n++) - if (target[n].id == clock_id) - return target + n; + if (clock_id < target_count) + return target + clock_id;
return NULL; } @@ -127,14 +126,21 @@ static struct sandbox_scmi_clk *get_scmi_clk_state(uint agent_id, uint clock_id) static struct sandbox_scmi_reset *get_scmi_reset_state(uint agent_id, uint reset_id) { - size_t n; + struct sandbox_scmi_reset *target = NULL; + size_t target_count = 0;
- if (agent_id == 0) { - for (n = 0; n < ARRAY_SIZE(scmi0_reset); n++) - if (scmi0_reset[n].id == reset_id) - return scmi0_reset + n; + switch (agent_id) { + case 0: + target = scmi0_reset; + target_count = ARRAY_SIZE(scmi0_reset); + break; + default: + return NULL; }
+ if (reset_id < target_count) + return target + reset_id; + return NULL; }
@@ -156,6 +162,70 @@ static struct sandbox_scmi_voltd *get_scmi_voltd_state(uint agent_id, * Sandbox SCMI agent ops */
+static int sandbox_scmi_clock_protocol_attribs(struct udevice *dev, + struct scmi_msg *msg) +{ + struct sandbox_scmi_agent *agent = dev_get_priv(dev); + struct scmi_clk_protocol_attr_out *out = NULL; + + if (!msg->out_msg || msg->out_msg_sz < sizeof(*out)) + return -EINVAL; + + out = (struct scmi_clk_protocol_attr_out *)msg->out_msg; + + switch (agent->idx) { + case 0: + out->attributes = ARRAY_SIZE(scmi0_clk); + out->status = SCMI_SUCCESS; + break; + case 1: + out->attributes = ARRAY_SIZE(scmi1_clk); + out->status = SCMI_SUCCESS; + break; + default: + out->status = SCMI_INVALID_PARAMETERS; + break; + } + + return 0; +} + +static int sandbox_scmi_clock_attribs(struct udevice *dev, struct scmi_msg *msg) +{ + struct sandbox_scmi_agent *agent = dev_get_priv(dev); + struct scmi_clk_attribute_in *in = NULL; + struct scmi_clk_attribute_out *out = NULL; + struct sandbox_scmi_clk *clk_state = NULL; + int ret; + + if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || + !msg->out_msg || msg->out_msg_sz < sizeof(*out)) + return -EINVAL; + + in = (struct scmi_clk_attribute_in *)msg->in_msg; + out = (struct scmi_clk_attribute_out *)msg->out_msg; + + clk_state = get_scmi_clk_state(agent->idx, in->clock_id); + if (!clk_state) { + dev_err(dev, "Unexpected clock ID %u\n", in->clock_id); + + out->status = SCMI_NOT_FOUND; + } else { + memset(out, 0, sizeof(*out)); + + if (clk_state->enabled) + out->attributes = 1; + + ret = snprintf(out->clock_name, sizeof(out->clock_name), + "clk%u", in->clock_id); + assert(ret > 0 && ret < sizeof(out->clock_name)); + + out->status = SCMI_SUCCESS; + } + + return 0; +} + static int sandbox_scmi_clock_rate_set(struct udevice *dev, struct scmi_msg *msg) { @@ -479,6 +549,10 @@ static int sandbox_scmi_test_process_msg(struct udevice *dev, switch (msg->protocol_id) { case SCMI_PROTOCOL_ID_CLOCK: switch (msg->message_id) { + case SCMI_PROTOCOL_ATTRIBUTES: + return sandbox_scmi_clock_protocol_attribs(dev, msg); + case SCMI_CLOCK_ATTRIBUTES: + return sandbox_scmi_clock_attribs(dev, msg); case SCMI_CLOCK_RATE_SET: return sandbox_scmi_clock_rate_set(dev, msg); case SCMI_CLOCK_RATE_GET: diff --git a/test/dm/scmi.c b/test/dm/scmi.c index c938e6d4fc..7d0a6799b7 100644 --- a/test/dm/scmi.c +++ b/test/dm/scmi.c @@ -58,7 +58,7 @@ static int ut_assert_scmi_state_postprobe(struct unit_test_state *uts, ut_asserteq(2, scmi_ctx->agent_count);
ut_assertnonnull(agent0); - ut_asserteq(2, agent0->clk_count); + ut_asserteq(3, agent0->clk_count); ut_assertnonnull(agent0->clk); ut_asserteq(1, agent0->reset_count); ut_assertnonnull(agent0->reset); @@ -128,6 +128,15 @@ static int dm_test_scmi_clocks(struct unit_test_state *uts) if (ret) return ret;
+ /* + * As stated in sandbox test.dts: + * - Sandbox device clock #0 relates to SCMI server #0 clock #2 + * - Sandbox device clock #1 relates to SCMI server #0 clock #0 + * - Sandbox device clock #2 relates to SCMI server #1 clock #0 + * + * Note there exists a SCMI server #0 clock #1 but is it not + * used but the sandbox test device. + */ scmi_devices = sandbox_scmi_devices_ctx(dev); scmi_ctx = sandbox_scmi_service_ctx(); agent0 = scmi_ctx->agent[0]; @@ -141,8 +150,9 @@ static int dm_test_scmi_clocks(struct unit_test_state *uts) ret_dev = clk_set_rate(&scmi_devices->clk[1], 1088); ut_assert(!ret_dev || ret_dev == 1088);
- ut_asserteq(1000, agent0->clk[0].rate); - ut_asserteq(1088, agent0->clk[1].rate); + ut_asserteq(1088, agent0->clk[0].rate); + ut_asserteq(200, agent0->clk[1].rate); + ut_asserteq(1000, agent0->clk[2 ].rate); ut_asserteq(44, agent1->clk[0].rate);
ut_asserteq(1000, clk_get_rate(&scmi_devices->clk[0])); @@ -156,20 +166,23 @@ static int dm_test_scmi_clocks(struct unit_test_state *uts) /* Test SCMI clocks gating manipulation */ ut_assert(!agent0->clk[0].enabled); ut_assert(!agent0->clk[1].enabled); + ut_assert(!agent0->clk[2].enabled); ut_assert(!agent1->clk[0].enabled);
- ut_asserteq(0, clk_enable(&scmi_devices->clk[1])); + ut_asserteq(0, clk_enable(&scmi_devices->clk[0])); ut_asserteq(0, clk_enable(&scmi_devices->clk[2]));
ut_assert(!agent0->clk[0].enabled); - ut_assert(agent0->clk[1].enabled); + ut_assert(!agent0->clk[1].enabled); + ut_assert(agent0->clk[2].enabled); ut_assert(agent1->clk[0].enabled);
- ut_assertok(clk_disable(&scmi_devices->clk[1])); + ut_assertok(clk_disable(&scmi_devices->clk[0])); ut_assertok(clk_disable(&scmi_devices->clk[2]));
ut_assert(!agent0->clk[0].enabled); ut_assert(!agent0->clk[1].enabled); + ut_assert(!agent0->clk[2].enabled); ut_assert(!agent1->clk[0].enabled);
return release_sandbox_scmi_test_devices(uts, dev); @@ -188,6 +201,10 @@ static int dm_test_scmi_resets(struct unit_test_state *uts) if (ret) return ret;
+ /* + * As stated in sandbox test.dts: + * - Sandbox device reset ctrl #0 relates to SCMI #0 reset domain #0 + */ scmi_devices = sandbox_scmi_devices_ctx(dev); scmi_ctx = sandbox_scmi_service_ctx(); agent0 = scmi_ctx->agent[0];

On Wed, 12 May 2021 at 08:55, Etienne Carriere etienne.carriere@linaro.org wrote:
Since SCMI clock are discovered because of integration in the CCF, update SCMI emulation in sandbox accordingly. Sandbox must emulate all clocks exposed by SCMI server since CCF clock discovery will query all of them even if some clocks have no consumer. This change adds clock discovery support in the sandbox and changes clock IDs so that less clock shall be managed as each SCMI clock ID is actually an index identifier.
For consistency, update also sandbox SCMI reset controller test to conform with the IDs being also indices of exposed resources as per SCMI specification.
Signed-off-by: Etienne Carriere etienne.carriere@linaro.org
arch/sandbox/dts/test.dts | 4 +- arch/sandbox/include/asm/scmi_test.h | 2 - drivers/firmware/scmi/sandbox-scmi_agent.c | 102 ++++++++++++++++++--- test/dm/scmi.c | 29 ++++-- 4 files changed, 113 insertions(+), 24 deletions(-)
Reviewed-by: Simon Glass sjg@chromium.org

Hi,
On Wed, 12 May 2021 at 08:55, Etienne Carriere etienne.carriere@linaro.org wrote:
From: Patrick Delaunay patrick.delaunay@st.com
This patch implements SCMI APIs to retrieve the number and the name of SCMI clocks using SCMI_PROTOCOL_ATTRIBUTES messages.
Signed-off-by: Gabriel Fernandez gabriel.fernandez@st.com Signed-off-by: Patrick Delaunay patrick.delaunay@st.com Signed-off-by: Etienne Carriere etienne.carriere@linaro.org
drivers/clk/clk_scmi.c | 101 +++++++++++++++++++++++++++++++++++++++ include/scmi_protocols.h | 43 +++++++++++++++++ 2 files changed, 144 insertions(+)
Reviewed-by: Simon Glass sjg@chromium.org
I notice that this code uses pointers to return error codes. We tend to avoid that in U-Boot and use an integer return instead, with the pointer returned as a parameter. We have things like log_msg_ret() to help with that. I don't have strong feelings about this if the goal is to make the code match linux code. But if this code is for U-Boot only, please do avoid it.
Regards, Simon
participants (2)
-
Etienne Carriere
-
Simon Glass