From: Artem Shimko <a.shimko.dev@gmail.com>
To: Sudeep Holla <sudeep.holla@arm.com>,
Cristian Marussi <cristian.marussi@arm.com>
Cc: Artem Shimko <a.shimko.dev@gmail.com>,
arm-scmi@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] firmware: arm_scmi: extract domain ID validation helpers
Date: Thu, 22 Jan 2026 19:17:27 +0300 [thread overview]
Message-ID: <20260122161728.1225583-1-a.shimko.dev@gmail.com> (raw)
Refactor the voltage protocol implementation by moving repeated domain
ID boundary checks into dedicated inline functions. This reduces code
duplication and improves maintainability.
The new helpers are scmi_voltage_domain_validate_id() which validates
a domain ID and returns an error code, and scmi_voltage_domain_lookup()
which returns a pointer to the domain structure or an error pointer.
These are used consistently across the voltage operations.
No functional changes are introduced - the validation logic remains
identical to the original inline checks.
Signed-off-by: Artem Shimko <a.shimko.dev@gmail.com>
---
Hello maintainers and reviewers,
Continuing the refactoring work from the previous one [1], this patch
extracts repeated domain ID boundary checks in the voltage protocol into
dedicated helper functions. This improves code maintainability by reducing
duplication and centralizing the validation logic.
The patch introduces two inline helpers:
* scmi_voltage_domain_validate_id(): validates domain ID and returns an
error code
* scmi_voltage_domain_lookup(): returns pointer to domain struct or error
Thank you!
[1] https://lore.kernel.org/all/20251103161044.2269377-1-a.shimko.dev@gmail.com/T/#u
--
Regards,
artem
drivers/firmware/arm_scmi/voltage.c | 59 ++++++++++++++++++++---------
1 file changed, 41 insertions(+), 18 deletions(-)
diff --git a/drivers/firmware/arm_scmi/voltage.c b/drivers/firmware/arm_scmi/voltage.c
index 17127880e10a..62010df24053 100644
--- a/drivers/firmware/arm_scmi/voltage.c
+++ b/drivers/firmware/arm_scmi/voltage.c
@@ -262,15 +262,37 @@ static int scmi_voltage_descriptors_get(const struct scmi_protocol_handle *ph,
return ret;
}
+static inline int
+scmi_voltage_domain_validate_id(const struct scmi_protocol_handle *ph, u32 domain_id)
+{
+ struct voltage_info *vinfo = ph->get_priv(ph);
+
+ if (domain_id >= vinfo->num_domains)
+ return -EINVAL;
+
+ return 0;
+}
+
+static inline struct scmi_voltage_info *
+scmi_voltage_domain_lookup(const struct scmi_protocol_handle *ph, u32 domain_id)
+{
+ struct voltage_info *vinfo = ph->get_priv(ph);
+
+ if (domain_id >= vinfo->num_domains)
+ return ERR_PTR(-EINVAL);
+
+ return vinfo->domains + domain_id;
+}
+
static int __scmi_voltage_get_u32(const struct scmi_protocol_handle *ph,
u8 cmd_id, u32 domain_id, u32 *value)
{
int ret;
struct scmi_xfer *t;
- struct voltage_info *vinfo = ph->get_priv(ph);
- if (domain_id >= vinfo->num_domains)
- return -EINVAL;
+ ret = scmi_voltage_domain_validate_id(ph, domain_id);
+ if (ret)
+ return ret;
ret = ph->xops->xfer_get_init(ph, cmd_id, sizeof(__le32), 0, &t);
if (ret)
@@ -290,11 +312,11 @@ static int scmi_voltage_config_set(const struct scmi_protocol_handle *ph,
{
int ret;
struct scmi_xfer *t;
- struct voltage_info *vinfo = ph->get_priv(ph);
struct scmi_msg_cmd_config_set *cmd;
- if (domain_id >= vinfo->num_domains)
- return -EINVAL;
+ ret = scmi_voltage_domain_validate_id(ph, domain_id);
+ if (ret)
+ return ret;
ret = ph->xops->xfer_get_init(ph, VOLTAGE_CONFIG_SET,
sizeof(*cmd), 0, &t);
@@ -325,25 +347,23 @@ static int scmi_voltage_level_set(const struct scmi_protocol_handle *ph,
{
int ret;
struct scmi_xfer *t;
- struct voltage_info *vinfo = ph->get_priv(ph);
struct scmi_msg_cmd_level_set *cmd;
- struct scmi_voltage_info *v;
+ struct scmi_voltage_info *vi;
- if (domain_id >= vinfo->num_domains)
- return -EINVAL;
+ vi = scmi_voltage_domain_lookup(ph, domain_id);
+ if (IS_ERR(vi))
+ return PTR_ERR(vi);
ret = ph->xops->xfer_get_init(ph, VOLTAGE_LEVEL_SET,
sizeof(*cmd), 0, &t);
if (ret)
return ret;
- v = vinfo->domains + domain_id;
-
cmd = t->tx.buf;
cmd->domain_id = cpu_to_le32(domain_id);
cmd->voltage_level = cpu_to_le32(volt_uV);
- if (!v->async_level_set || mode != SCMI_VOLTAGE_LEVEL_SET_AUTO) {
+ if (!vi->async_level_set || mode != SCMI_VOLTAGE_LEVEL_SET_AUTO) {
cmd->flags = cpu_to_le32(0x0);
ret = ph->xops->do_xfer(ph, t);
} else {
@@ -356,7 +376,7 @@ static int scmi_voltage_level_set(const struct scmi_protocol_handle *ph,
if (le32_to_cpu(resp->domain_id) == domain_id)
dev_dbg(ph->dev,
"Voltage domain %d set async to %d\n",
- v->id,
+ vi->id,
le32_to_cpu(resp->voltage_level));
else
ret = -EPROTO;
@@ -377,13 +397,16 @@ static int scmi_voltage_level_get(const struct scmi_protocol_handle *ph,
static const struct scmi_voltage_info * __must_check
scmi_voltage_info_get(const struct scmi_protocol_handle *ph, u32 domain_id)
{
- struct voltage_info *vinfo = ph->get_priv(ph);
+ struct scmi_voltage_info *vi;
- if (domain_id >= vinfo->num_domains ||
- !vinfo->domains[domain_id].num_levels)
+ vi = scmi_voltage_domain_lookup(ph, domain_id);
+ if (IS_ERR(vi))
return NULL;
- return vinfo->domains + domain_id;
+ if (!vi->num_levels)
+ return NULL;
+
+ return vi;
}
static int scmi_voltage_domains_num_get(const struct scmi_protocol_handle *ph)
--
2.43.0
next reply other threads:[~2026-01-22 16:17 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-22 16:17 Artem Shimko [this message]
2026-03-17 12:05 ` [PATCH] firmware: arm_scmi: extract domain ID validation helpers Artem Shimko
2026-03-17 13:37 ` Sudeep Holla
2026-03-17 14:27 ` Artem Shimko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260122161728.1225583-1-a.shimko.dev@gmail.com \
--to=a.shimko.dev@gmail.com \
--cc=arm-scmi@vger.kernel.org \
--cc=cristian.marussi@arm.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sudeep.holla@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox