From: "Nuno Sá" <noname.nuno@gmail.com>
To: David Lechner <dlechner@baylibre.com>, Mark Brown <broonie@kernel.org>
Cc: "Martin Sperl" <kernel@martin.sperl.org>,
"David Jander" <david@protonic.nl>,
"Jonathan Cameron" <jic23@kernel.org>,
"Michael Hennerich" <michael.hennerich@analog.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Alain Volmat" <alain.volmat@foss.st.com>,
"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-stm32@st-md-mailman.stormreply.com,
linux-arm-kernel@lists.infradead.org, linux-iio@vger.kernel.org
Subject: Re: [PATCH 1/5] spi: add spi_optimize_message() APIs
Date: Tue, 13 Feb 2024 10:53:56 +0100 [thread overview]
Message-ID: <92e7e0acf6d8746a07729924982acbfea777c468.camel@gmail.com> (raw)
In-Reply-To: <20240212-mainline-spi-precook-message-v1-1-a2373cd72d36@baylibre.com>
On Mon, 2024-02-12 at 17:26 -0600, David Lechner wrote:
> This adds a new spi_optimize_message() function that can be used to
> optimize SPI messages that are used more than once. Peripheral drivers
> that use the same message multiple times can use this API to perform SPI
> message validation and controller-specific optimizations once and then
> reuse the message while avoiding the overhead of revalidating the
> message on each spi_(a)sync() call.
>
> Internally, the SPI core will also call this function for each message
> if the peripheral driver did not explicitly call it. This is done to so
> that controller drivers don't have to have multiple code paths for
> optimized and non-optimized messages.
>
> A hook is provided for controller drivers to perform controller-specific
> optimizations.
>
> Suggested-by: Martin Sperl <kernel@martin.sperl.org>
> Link:
> https://lore.kernel.org/linux-spi/39DEC004-10A1-47EF-9D77-276188D2580C@martin.sperl.org/
> Signed-off-by: David Lechner <dlechner@baylibre.com>
> ---
> drivers/spi/spi.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++-
> -
> include/linux/spi/spi.h | 19 +++++++
> 2 files changed, 160 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
> index c2b10e2c75f0..5bac215d7009 100644
> --- a/drivers/spi/spi.c
> +++ b/drivers/spi/spi.c
> @@ -2106,6 +2106,41 @@ struct spi_message *spi_get_next_queued_message(struct
> spi_controller *ctlr)
> }
> EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
>
> +/**
> + * __spi_unoptimize_message - shared implementation of
> spi_unoptimize_message()
> + * and spi_maybe_unoptimize_message()
> + * @msg: the message to unoptimize
> + *
> + * Periperhal drivers should use spi_unoptimize_message() and callers inside
> + * core should use spi_maybe_unoptimize_message() rather than calling this
> + * function directly.
> + *
> + * It is not valid to call this on a message that is not currently optimized.
> + */
> +static void __spi_unoptimize_message(struct spi_message *msg)
> +{
> + struct spi_controller *ctlr = msg->spi->controller;
> +
> + if (ctlr->unoptimize_message)
> + ctlr->unoptimize_message(msg);
> +
> + msg->optimized = false;
> + msg->opt_state = NULL;
> +}
> +
> +/**
> + * spi_maybe_unoptimize_message - unoptimize msg not managed by a peripheral
> + * @msg: the message to unoptimize
> + *
> + * This function is used to unoptimize a message if and only if it was
> + * optimized by the core (via spi_maybe_optimize_message()).
> + */
> +static void spi_maybe_unoptimize_message(struct spi_message *msg)
> +{
> + if (!msg->pre_optimized && msg->optimized)
> + __spi_unoptimize_message(msg);
> +}
> +
> /**
> * spi_finalize_current_message() - the current message is complete
> * @ctlr: the controller to return the message to
> @@ -2153,6 +2188,8 @@ void spi_finalize_current_message(struct spi_controller
> *ctlr)
>
> mesg->prepared = false;
>
> + spi_maybe_unoptimize_message(mesg);
> +
> WRITE_ONCE(ctlr->cur_msg_incomplete, false);
> smp_mb(); /* See __spi_pump_transfer_message()... */
> if (READ_ONCE(ctlr->cur_msg_need_completion))
> @@ -4194,6 +4231,99 @@ static int __spi_validate(struct spi_device *spi,
> struct spi_message *message)
> return 0;
> }
>
> +/**
> + * __spi_optimize_message - shared implementation for spi_optimize_message()
> + * and spi_maybe_optimize_message()
> + * @spi: the device that will be used for the message
> + * @msg: the message to optimize
> + * @pre_optimized: whether the message is considered pre-optimized or not
> + *
> + * Peripheral drivers will call spi_optimize_message() and the spi core will
> + * call spi_maybe_optimize_message() instead of calling this directly.
> + *
> + * It is not valid to call this on a message that has already been optimized.
> + *
> + * Return: zero on success, else a negative error code
> + */
> +static int __spi_optimize_message(struct spi_device *spi,
> + struct spi_message *msg,
> + bool pre_optimized)
> +{
> + struct spi_controller *ctlr = spi->controller;
> + int ret;
> +
> + ret = __spi_validate(spi, msg);
> + if (ret)
> + return ret;
> +
> + if (ctlr->optimize_message) {
> + ret = ctlr->optimize_message(msg);
> + if (ret)
> + return ret;
> + }
Not really sure what are the spi core guarantees or what controllers should be
expecting but I'll still ask :). Do we need to care about locking in here?
Mainly on the controller callback? For spi device related data I guess it's up
to the peripheral driver not to do anything weird or to properly protect the spi
message?
- Nuno Sá
next prev parent reply other threads:[~2024-02-13 9:50 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-12 23:26 [PATCH 0/5] spi: add support for pre-cooking messages David Lechner
2024-02-12 23:26 ` [PATCH 1/5] spi: add spi_optimize_message() APIs David Lechner
2024-02-13 9:53 ` Nuno Sá [this message]
2024-02-13 15:38 ` David Lechner
2024-02-13 17:55 ` Mark Brown
2024-02-13 17:25 ` Jonathan Cameron
2024-02-13 19:20 ` David Lechner
2024-02-13 18:55 ` Mark Brown
2024-02-13 19:26 ` David Lechner
2024-02-13 19:28 ` Mark Brown
2024-02-12 23:26 ` [PATCH 2/5] spi: move splitting transfers to spi_optimize_message() David Lechner
2024-02-13 17:35 ` Jonathan Cameron
2024-02-12 23:26 ` [PATCH 3/5] spi: stm32: move splitting transfers to optimize_message David Lechner
2024-02-12 23:26 ` [PATCH 4/5] spi: axi-spi-engine: move message compile " David Lechner
2024-02-12 23:26 ` [PATCH 5/5] iio: adc: ad7380: use spi_optimize_message() David Lechner
2024-02-13 9:51 ` Nuno Sá
2024-02-13 15:27 ` David Lechner
2024-02-13 16:08 ` Nuno Sá
2024-02-13 17:31 ` Jonathan Cameron
2024-02-13 18:59 ` David Lechner
2024-02-13 17:28 ` Jonathan Cameron
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=92e7e0acf6d8746a07729924982acbfea777c468.camel@gmail.com \
--to=noname.nuno@gmail.com \
--cc=alain.volmat@foss.st.com \
--cc=alexandre.torgue@foss.st.com \
--cc=broonie@kernel.org \
--cc=david@protonic.nl \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=kernel@martin.sperl.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=linux-stm32@st-md-mailman.stormreply.com \
--cc=mcoquelin.stm32@gmail.com \
--cc=michael.hennerich@analog.com \
--cc=nuno.sa@analog.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;
as well as URLs for NNTP newsgroup(s).