From: Florian Fainelli <f.fainelli@gmail.com>
To: Cristian Marussi <cristian.marussi@arm.com>,
linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org
Cc: sudeep.holla@arm.com, lukasz.luba@arm.com,
james.quinlan@broadcom.com, Jonathan.Cameron@Huawei.com,
etienne.carriere@linaro.org, thara.gopinath@linaro.org,
vincent.guittot@linaro.org, souvik.chakravarty@arm.com
Subject: Re: [PATCH 01/11] firmware: arm_scmi: review protocol registration interface
Date: Wed, 14 Oct 2020 12:03:45 -0700 [thread overview]
Message-ID: <1b461304-2c25-3206-5799-b561ac6e0bd2@gmail.com> (raw)
In-Reply-To: <20201014150545.44807-2-cristian.marussi@arm.com>
On 10/14/20 8:05 AM, Cristian Marussi wrote:
> Extend common protocol registration routines and provide some new generic
> protocols' init/deinit helpers that tracks protocols' users and automatically
> perform the proper initialization/de-initialization on demand.
>
> Convert all protocols to use new registration schema while modifying only Base
> protocol to use also the new initialization helpers.
>
> All other standard protocols' initialization is still umodified and bound to
> SCMI devices probing.
>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> ---
[snip]
> +static struct scmi_protocol scmi_base = {
> + .id = SCMI_PROTOCOL_BASE,
> + .init = &scmi_base_protocol_init,
> + .ops = NULL,
> +};
This could be const I believe.
> +
> +DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(base, scmi_base)
> diff --git a/drivers/firmware/arm_scmi/bus.c b/drivers/firmware/arm_scmi/bus.c
> index 1377ec76a45d..afa2e4818a2b 100644
> --- a/drivers/firmware/arm_scmi/bus.c
> +++ b/drivers/firmware/arm_scmi/bus.c
> @@ -16,7 +16,7 @@
> #include "common.h"
>
> static DEFINE_IDA(scmi_bus_id);
> -static DEFINE_IDR(scmi_protocols);
> +static DEFINE_IDR(scmi_available_protocols);
> static DEFINE_SPINLOCK(protocol_lock);
>
> static const struct scmi_device_id *
> @@ -51,13 +51,29 @@ static int scmi_dev_match(struct device *dev, struct device_driver *drv)
> return 0;
> }
>
> +const struct scmi_protocol *scmi_get_protocol(int protocol_id)
> +{
> + const struct scmi_protocol *proto;
> +
> + proto = idr_find(&scmi_available_protocols, protocol_id);
> + if (!proto) {
> + pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id);
> + return NULL;
> + }
> +
> + pr_debug("GOT SCMI Protocol 0x%x\n", protocol_id);
> +
> + return proto;
> +}
> +
> static int scmi_protocol_init(int protocol_id, struct scmi_handle *handle)
> {
> - scmi_prot_init_fn_t fn = idr_find(&scmi_protocols, protocol_id);
> + const struct scmi_protocol *proto;
>
> - if (unlikely(!fn))
> + proto = idr_find(&scmi_available_protocols, protocol_id);
> + if (!proto)
> return -EINVAL;
> - return fn(handle);
> + return proto->init(handle);
> }
>
> static int scmi_protocol_dummy_init(struct scmi_handle *handle)
> @@ -84,7 +100,7 @@ static int scmi_dev_probe(struct device *dev)
> return ret;
>
> /* Skip protocol initialisation for additional devices */
> - idr_replace(&scmi_protocols, &scmi_protocol_dummy_init,
> + idr_replace(&scmi_available_protocols, &scmi_protocol_dummy_init,
> scmi_dev->protocol_id);
>
> return scmi_drv->probe(scmi_dev);
> @@ -194,26 +210,45 @@ void scmi_set_handle(struct scmi_device *scmi_dev)
> scmi_dev->handle = scmi_handle_get(&scmi_dev->dev);
> }
>
> -int scmi_protocol_register(int protocol_id, scmi_prot_init_fn_t fn)
> +int scmi_protocol_register(struct scmi_protocol *proto)
And this could probably take a const struct scmi_protocol here too as
you do not appear to be modifying proto, idr_alloc() may complain.
[snip]
>
> -DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(SCMI_PROTOCOL_CLOCK, clock)
> +static struct scmi_protocol scmi_clock = {
static const here too and throughout your submission.
[snip]
> + struct scmi_protocol_instance *protocols[SCMI_MAX_PROTO];
Humm that would be 2048 bytes on a 64-bit platform and 1024 bytes on a
32-bit platform, this is not so bad, but it is a bit wasteful given that
6-7 standard protocols are typically found, and most often no
proprietary protocols are registered. Not necessarily to be addressed
right now.
> + /* Ensure mutual exclusive access to protocols instance array */
> + struct mutex protocols_mtx;
> u8 *protocols_imp;
> struct list_head node;
> int users;
> @@ -519,6 +542,132 @@ int scmi_version_get(const struct scmi_handle *handle, u8 protocol,
> return ret;
> }
>
> +/**
> + * scmi_get_protocol_instance - Protocol initialization helper.
> + * @handle: A reference to the SCMI platform instance.
> + * @protocol_id: The protocol being requested.
> + *
> + * In case the required protocol has never been requested before for this
> + * instance, allocate and initialize all the needed structures while handling
> + * resource allocation with a dedicated per-protocol devres subgroup.
> + *
> + * Return: A reference to an initialized protocol instance or error on failure.
> + */
> +static struct scmi_protocol_instance * __must_check
> +scmi_get_protocol_instance(struct scmi_handle *handle, u8 protocol_id)
> +{
> + int ret = -ENOMEM;
> + void *gid;
> + struct scmi_protocol_instance *pi;
> + struct scmi_info *info = handle_to_scmi_info(handle);
> +
> + mutex_lock(&info->protocols_mtx);
> + /* Ensure protocols has been updated */
> + smp_rmb();
> + pi = info->protocols[protocol_id];
> +
> + if (!pi) {
> + const struct scmi_protocol *proto;
> +
> + /* Fail if protocol not registered on bus */
> + proto = scmi_get_protocol(protocol_id);
> + if (!proto) {
> + ret = -EINVAL;
You could return -ENODEV here and propagate that error code for
scmi_probe() to use as -is.
> + goto out;
> + }
> +
> + /* Protocol specific devres group */
> + gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
> + if (!gid)
> + goto out;
> +
> + pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
> + if (!pi)
> + goto clean;
> +
> + pi->gid = gid;
> + pi->proto = proto;
> + refcount_set(&pi->users, 1);
> + /* proto->init is assured NON NULL by scmi_protocol_register */
> + ret = pi->proto->init(handle);
> + if (ret)
> + goto clean;
> +
> + info->protocols[protocol_id] = pi;
> + /* Ensure initialized protocol is visible */
> + smp_wmb();
> +
> + devres_close_group(handle->dev, pi->gid);
> + dev_dbg(handle->dev, "Initialized protocol: 0x%X\n",
> + protocol_id);
> + } else {
> + refcount_inc(&pi->users);
> + }
You may be able to re-arrange the indentation and do:
if (pi) {
refcount_inc(&pi->users);
mutex_unlock(&info->protocols_mtx);
return pi;
}
to reduce the indentation level, and this would also have the fast path
tested first.
> + mutex_unlock(&info->protocols_mtx);
> +
> + return pi;
> +
> +clean:
> + devres_release_group(handle->dev, gid);
> +out:
> + mutex_unlock(&info->protocols_mtx);
> + return ERR_PTR(ret);
> +}
> +
> +/**
> + * scmi_acquire_protocol - Protocol acquire
> + * @handle: A reference to the SCMI platform instance.
> + * @protocol_id: The protocol being requested.
> + *
> + * Register a new user for the requested protocol on the specified SCMI
> + * platform instance, possibly triggering its initialization on first user.
> + *
> + * Return: 0 if protocol was acquired successfully.
> + */
> +int scmi_acquire_protocol(struct scmi_handle *handle, u8 protocol_id)
> +{
> + return IS_ERR(scmi_get_protocol_instance(handle, protocol_id));
> +}
> +
> +/**
> + * scmi_release_protocol - Protocol de-initialization helper.
> + * @handle: A reference to the SCMI platform instance.
> + * @protocol_id: The protocol being requested.
> + *
> + * Remove one user for the specified protocol and triggers de-initialization
> + * and resources de-allocation once the last user has gone.
> + */
> +void scmi_release_protocol(struct scmi_handle *handle, u8 protocol_id)
> +{
> + struct scmi_info *info = handle_to_scmi_info(handle);
> + struct scmi_protocol_instance *pi;
> +
> + mutex_lock(&info->protocols_mtx);
> + /* Ensure protocols has been updated */
> + smp_rmb();
> + pi = info->protocols[protocol_id];
> + if (WARN_ON(!pi)) {
> + mutex_unlock(&info->protocols_mtx);
> + return;
Maybe define an "out" label just to avoid the repetition?
> + }
> +
> + if (refcount_dec_and_test(&pi->users)) {
> + void *gid = pi->gid;
> +
> + if (pi->proto->deinit)
> + pi->proto->deinit(handle);
> +
> + info->protocols[protocol_id] = NULL;
> + /* Ensure deinitialized protocol is visible */
> + smp_wmb();
> +
> + devres_release_group(handle->dev, gid);
> + dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
> + protocol_id);
> + }
> + mutex_unlock(&info->protocols_mtx);
> +}
> +
> void scmi_setup_protocol_implemented(const struct scmi_handle *handle,
> u8 *prot_imp)
> {
> @@ -785,6 +934,7 @@ static int scmi_probe(struct platform_device *pdev)
> info->dev = dev;
> info->desc = desc;
> INIT_LIST_HEAD(&info->node);
> + mutex_init(&info->protocols_mtx);
>
> platform_set_drvdata(pdev, info);
> idr_init(&info->tx_idr);
> @@ -805,10 +955,14 @@ static int scmi_probe(struct platform_device *pdev)
> if (scmi_notification_init(handle))
> dev_err(dev, "SCMI Notifications NOT available.\n");
>
> - ret = scmi_base_protocol_init(handle);
> - if (ret) {
> - dev_err(dev, "unable to communicate with SCMI(%d)\n", ret);
> - return ret;
> + /*
> + * Trigger SCMI Base protocol initialization.
> + * It's mandatory and won't be ever released/deinit until the
> + * SCMI stack is shutdown/unloaded as a whole.
> + */
> + if (scmi_acquire_protocol(handle, SCMI_PROTOCOL_BASE)) {
> + dev_err(dev, "unable to communicate with SCMI\n");
> + return -ENODEV;
and you could do:
ret = scmi_acquire_protocol(.., ...);
if (ret) {
dev_err(dev, "unable to communicate with SCMI\n");
return ret;
}
Everything else looked good to me, thanks!
--
Florian
next prev parent reply other threads:[~2020-10-14 19:03 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-14 15:05 [PATCH 00/11] SCMI vendor protocols and modularization Cristian Marussi
2020-10-14 15:05 ` [PATCH 01/11] firmware: arm_scmi: review protocol registration interface Cristian Marussi
2020-10-14 19:03 ` Florian Fainelli [this message]
2020-10-14 20:20 ` Sudeep Holla
2020-10-15 8:42 ` Cristian Marussi
2020-10-15 9:41 ` Vincent Guittot
2020-10-15 9:58 ` Cristian Marussi
2020-10-21 2:46 ` Thara Gopinath
2020-10-21 10:08 ` Cristian Marussi
2020-10-14 15:05 ` [PATCH 02/11] firmware: arm_scmi: hide protocols' private data Cristian Marussi
2020-10-14 19:19 ` Florian Fainelli
2020-10-15 8:52 ` Cristian Marussi
2020-10-14 15:05 ` [PATCH 03/11] firmware: arm_scmi: introduce common protocol interface Cristian Marussi
2020-10-21 2:47 ` Thara Gopinath
2020-10-21 10:27 ` Cristian Marussi
2020-10-26 13:07 ` Thara Gopinath
2020-10-28 21:04 ` Cristian Marussi
2020-10-14 15:05 ` [PATCH 04/11] firmware: arm_scmi: refactor events registration Cristian Marussi
2020-10-14 15:05 ` [PATCH 05/11] firmware: arm_scmi: make notify_priv really private Cristian Marussi
2020-10-14 15:05 ` [PATCH 06/11] firmware: arm_scmi: add support for protocol modularization Cristian Marussi
2020-10-21 2:47 ` Thara Gopinath
2020-10-21 10:30 ` Cristian Marussi
2020-10-14 15:05 ` [PATCH 07/11] [DEBUG] firmware: arm_scmi: add example custom protocol Cristian Marussi
2020-10-14 15:05 ` [PATCH 08/11] [DEBUG] arm64: dts: juno: add example custom protocol support Cristian Marussi
2020-10-14 15:05 ` [PATCH 09/11] [DEBUG] firmware: arm_scmi: add example SCMI driver for custom protocol Cristian Marussi
2020-10-14 15:55 ` Randy Dunlap
2020-10-14 16:44 ` Cristian Marussi
2020-10-14 15:05 ` [PATCH 10/11] [DEBUG] firmware: arm_scmi: add custom_dummy SCMI devname Cristian Marussi
2020-10-21 2:49 ` Thara Gopinath
2020-10-21 11:35 ` Cristian Marussi
2020-10-26 12:37 ` Thara Gopinath
2020-10-28 21:28 ` Cristian Marussi
2020-10-14 15:05 ` [PATCH 11/11] [DEBUG][HACK] firmware: arm_scmi: force implemented protocol 0x99 Cristian Marussi
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=1b461304-2c25-3206-5799-b561ac6e0bd2@gmail.com \
--to=f.fainelli@gmail.com \
--cc=Jonathan.Cameron@Huawei.com \
--cc=cristian.marussi@arm.com \
--cc=etienne.carriere@linaro.org \
--cc=james.quinlan@broadcom.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lukasz.luba@arm.com \
--cc=souvik.chakravarty@arm.com \
--cc=sudeep.holla@arm.com \
--cc=thara.gopinath@linaro.org \
--cc=vincent.guittot@linaro.org \
/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
all inboxes | Powered by JetHome®