From: "Nuno Sá" <noname.nuno@gmail.com>
To: "David Lechner" <dlechner@baylibre.com>,
"Mark Brown" <broonie@kernel.org>,
"Jonathan Cameron" <jic23@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"Nuno Sá" <nuno.sa@analog.com>,
"Uwe Kleine-König" <ukleinek@kernel.org>
Cc: Michael Hennerich <Michael.Hennerich@analog.com>,
Lars-Peter Clausen <lars@metafoo.de>,
David Jander <david@protonic.nl>,
Martin Sperl <kernel@martin.sperl.org>,
linux-spi@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
linux-pwm@vger.kernel.org
Subject: Re: [PATCH RFC v4 03/15] spi: offload: add support for hardware triggers
Date: Fri, 25 Oct 2024 08:29:23 +0200 [thread overview]
Message-ID: <1715db810c98d5e8acc01d8348603c2426a4df4e.camel@gmail.com> (raw)
In-Reply-To: <9d801823-aa90-4b15-9dbb-9da6ad2cb3e4@baylibre.com>
On Thu, 2024-10-24 at 10:02 -0500, David Lechner wrote:
> On 10/24/24 9:04 AM, Nuno Sá wrote:
> > On Wed, 2024-10-23 at 15:59 -0500, David Lechner wrote:
> > > Extend SPI offloading to support hardware triggers.
> > >
> > > This allows an arbitrary hardware trigger to be used to start a SPI
> > > transfer that was previously set up with spi_optimize_message().
> > >
> > > A new struct spi_offload_trigger is introduced that can be used to
> > > configure any type of trigger. It has a type discriminator and a union
> > > to allow it to be extended in the future. Two trigger types are defined
> > > to start with. One is a trigger that indicates that the SPI peripheral
> > > is ready to read or write data. The other is a periodic trigger to
> > > repeat a SPI message at a fixed rate.
> > >
> > > There is also a spi_offload_hw_trigger_validate() function that works
> > > similar to clk_round_rate(). It basically asks the question of if we
> > > enabled the hardware trigger what would the actual parameters be. This
> > > can be used to test if the requested trigger type is actually supported
> > > by the hardware and for periodic triggers, it can be used to find the
> > > actual rate that the hardware is capable of.
> > >
> > > Signed-off-by: David Lechner <dlechner@baylibre.com>
> > > ---
> > >
> > > In previous versions, we locked the SPI bus when the hardware trigger
> > > was enabled, but we found this to be too restrictive. In one use case,
> > > to avoid a race condition, we need to enable the SPI offload via a
> > > hardware trigger, then write a SPI message to the peripheral to place
> > > it into a mode that will generate the trigger. If we did it the other
> > > way around, we could miss the first trigger.
> > >
> > > Another likely use case will be enabling two offloads/triggers at one
> > > time on the same device, e.g. a read trigger and a write trigger. So
> > > the exclusive bus lock for a single trigger would be too restrictive in
> > > this case too.
> > >
> > > So for now, I'm going with Nuno's suggestion to leave any locking up to
> > > the individual controller driver. If we do find we need something more
> > > generic in the future, we could add a new spi_bus_lock_exclusive() API
> > > that causes spi_bus_lock() to fail instead of waiting and add "locked"
> > > versions of trigger enable functions. This would allow a peripheral to
> > > claim exclusive use of the bus indefinitely while still being able to
> > > do any SPI messaging that it needs.
> > >
> > > v4 changes:
> > > * Added new struct spi_offload_trigger that is a generic struct for any
> > > hardware trigger rather than returning a struct clk.
> > > * Added new spi_offload_hw_trigger_validate() function.
> > > * Dropped extra locking since it was too restrictive.
> > >
> > > v3 changes:
> > > * renamed enable/disable functions to spi_offload_hw_trigger_*mode*_...
> > > * added spi_offload_hw_trigger_get_clk() function
> > > * fixed missing EXPORT_SYMBOL_GPL
> > >
> > > v2 changes:
> > > * This is split out from "spi: add core support for controllers with
> > > offload capabilities".
> > > * Added locking for offload trigger to claim exclusive use of the SPI
> > > bus.
> > > ---
> > > drivers/spi/spi-offload.c | 266 ++++++++++++++++++++++++++++++++++++++++
> > > include/linux/spi/spi-offload.h | 78 ++++++++++++
> > > 2 files changed, 344 insertions(+)
> > >
> > > diff --git a/drivers/spi/spi-offload.c b/drivers/spi/spi-offload.c
> > > index c344cbf50bdb..2a1f9587f27a 100644
> > > --- a/drivers/spi/spi-offload.c
> > > +++ b/drivers/spi/spi-offload.c
> > > @@ -9,12 +9,26 @@
> > > #include <linux/cleanup.h>
> > > #include <linux/device.h>
> > > #include <linux/export.h>
> > > +#include <linux/list.h>
> > > #include <linux/mutex.h>
> > > +#include <linux/of.h>
> > > #include <linux/property.h>
> > > #include <linux/spi/spi-offload.h>
> > > #include <linux/spi/spi.h>
> > > #include <linux/types.h>
> > >
> > > +struct spi_offload_trigger {
> > > + struct list_head list;
> > > + struct device dev;
> > > + /* synchronizes calling ops and driver registration */
> > > + struct mutex lock;
> > > + const struct spi_offload_trigger_ops *ops;
> > > + void *priv;
> > > +};
> > > +
> > > +static LIST_HEAD(spi_offload_triggers);
> > > +static DEFINE_MUTEX(spi_offload_triggers_lock);
> > > +
> > > /**
> > > * devm_spi_offload_alloc() - Allocate offload instances
> > > * @dev: Device for devm purposes
> > > @@ -102,3 +116,255 @@ struct spi_offload *devm_spi_offload_get(struct device
> > > *dev,
> > > return offload;
> > > }
> > > EXPORT_SYMBOL_GPL(devm_spi_offload_get);
> > > +
> > > +static void spi_offload_trigger_release(void *data)
> > > +{
> > > + struct spi_offload_trigger *trigger = data;
> > > +
> > > + guard(mutex)(&trigger->lock);
> > > + if (trigger->priv && trigger->ops->release)
> > > + trigger->ops->release(trigger->priv);
> > > +
> > > + put_device(&trigger->dev);
> > > +}
> > > +
> > > +struct spi_offload_trigger
> > > +*devm_spi_offload_trigger_get(struct device *dev,
> > > + struct spi_offload *offload,
> > > + enum spi_offload_trigger_type type)
> > > +{
> > > + struct spi_offload_trigger *trigger;
> > > + struct fwnode_reference_args args;
> > > + bool match = false;
> > > + int ret;
> > > +
> > > + ret = fwnode_property_get_reference_args(dev_fwnode(offload-
> > > > provider_dev),
> > > + "trigger-sources",
> > > + "#trigger-source-cells", 0,
> > > 0,
> > > + &args);
> > > + if (ret)
> > > + return ERR_PTR(ret);
> > > +
> > > + struct fwnode_handle *trigger_fwnode __free(fwnode_handle) =
> > > args.fwnode;
> > > +
> > > + guard(mutex)(&spi_offload_triggers_lock);
> > > +
> > > + list_for_each_entry(trigger, &spi_offload_triggers, list) {
> > > + if (trigger->dev.fwnode != args.fwnode)
> > > + continue;
> > > +
> > > + match = trigger->ops->match(trigger->priv, type, args.args,
> > > args.nargs);
> > > + if (match)
> > > + break;
> > > + }
> > > +
> > > + if (!match)
> > > + return ERR_PTR(-EPROBE_DEFER);
> > > +
> > > + guard(mutex)(&trigger->lock);
> > > +
> > > + if (!trigger->priv)
> > > + return ERR_PTR(-ENODEV);
> >
> > This is a bit odd tbh. Not a real deal breaker for me but the typical pattern I
> > would
> > expect is for methods of the trigger to get a struct spi_offload_trigger opaque
> > pointer. Then we provide a get_private kind of API for the private data. I guess
> > you
> > want to avoid that but IMO it makes for neater API instead of getting void
> > pointers.
>
> I was just trying to save a step of an extra call to get *priv
> in each callback implementation, but yeah, no problem to change
> it to something more "normal" looking.
Yeah, I figured that but I guess any of these paths are fastpaths anyways...
>
> >
> > Another thing is, can the above actually happen? We have the
> > spi_offload_triggers_lock grabbed and we got a match so the trigger should not be
> > able to go away (should block on the same lock).
>
> The problem is that it could have gone away before we took the lock.
>
> It could happen like this:
>
> * Trigger driver registers trigger - sets *priv.
> * SPI peripheral driver gets reference to trigger.
> * Trigger driver unregisters trigger - removes *priv.
> * SPI peripheral tries to call trigger function.
>
Ah I see... we're using scoped_guard() in the unregister path.
- Nuno Sá
>
next prev parent reply other threads:[~2024-10-25 6:29 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-23 20:59 [PATCH RFC v4 00/15] spi: axi-spi-engine: add offload support David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 01/15] pwm: core: export pwm_get_state_hw() David Lechner
2024-10-29 8:05 ` Uwe Kleine-König
2024-10-29 15:30 ` David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 02/15] spi: add basic support for SPI offloading David Lechner
2024-10-24 13:27 ` Nuno Sá
2024-10-24 14:49 ` David Lechner
2024-10-25 12:59 ` Nuno Sá
2024-10-25 16:39 ` Nuno Sá
2024-10-26 15:05 ` Jonathan Cameron
2024-11-11 17:14 ` David Lechner
2024-11-11 19:02 ` David Lechner
2024-10-30 15:55 ` Mark Brown
2024-10-23 20:59 ` [PATCH RFC v4 03/15] spi: offload: add support for hardware triggers David Lechner
2024-10-24 14:04 ` Nuno Sá
2024-10-24 15:02 ` David Lechner
2024-10-25 6:29 ` Nuno Sá [this message]
2024-10-26 15:14 ` Jonathan Cameron
2024-10-28 13:53 ` Nuno Sá
2024-10-23 20:59 ` [PATCH RFC v4 04/15] spi: dt-bindings: add trigger-source.yaml David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 05/15] spi: dt-bindings: add PWM SPI offload trigger David Lechner
2024-10-26 15:18 ` Jonathan Cameron
2024-10-27 0:20 ` David Lechner
2024-10-27 20:24 ` Conor Dooley
2024-10-31 18:16 ` Conor Dooley
2024-11-11 17:31 ` David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 06/15] spi: offload-trigger: add PWM trigger driver David Lechner
2024-10-25 12:07 ` Nuno Sá
2024-10-25 16:28 ` David Lechner
2024-10-28 13:47 ` Nuno Sá
2024-10-23 20:59 ` [PATCH RFC v4 07/15] spi: add offload TX/RX streaming APIs David Lechner
2024-10-25 12:24 ` Nuno Sá
2024-10-23 20:59 ` [PATCH RFC v4 08/15] spi: dt-bindings: axi-spi-engine: add SPI offload properties David Lechner
2024-10-25 12:26 ` Nuno Sá
2024-10-23 20:59 ` [PATCH RFC v4 09/15] spi: axi-spi-engine: implement offload support David Lechner
2024-10-25 13:09 ` Nuno Sá
2024-10-25 16:35 ` David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 10/15] iio: buffer-dmaengine: document iio_dmaengine_buffer_setup_ext David Lechner
2024-10-26 15:29 ` Jonathan Cameron
2024-10-23 20:59 ` [PATCH RFC v4 11/15] iio: buffer-dmaengine: add devm_iio_dmaengine_buffer_setup_ext2() David Lechner
2024-10-25 13:24 ` Nuno Sá
2024-10-25 16:42 ` David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 12/15] iio: adc: ad7944: don't use storagebits for sizing David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 13/15] iio: adc: ad7944: add support for SPI offload David Lechner
2024-10-26 15:51 ` Jonathan Cameron
2024-10-23 20:59 ` [PATCH RFC v4 14/15] dt-bindings: iio: adc: adi,ad4695: add SPI offload properties David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 15/15] iio: adc: ad4695: Add support for SPI offload David Lechner
2024-10-26 16:00 ` Jonathan Cameron
2024-10-27 0:01 ` David Lechner
2024-10-27 9:12 ` Jonathan Cameron
2024-10-27 19:52 ` David Lechner
2024-10-28 16:39 ` Jonathan Cameron
2024-10-27 0:05 ` David Lechner
2024-10-27 9:15 ` Jonathan Cameron
2024-10-24 14:12 ` [PATCH RFC v4 00/15] spi: axi-spi-engine: add offload support Nuno Sá
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=1715db810c98d5e8acc01d8348603c2426a4df4e.camel@gmail.com \
--to=noname.nuno@gmail.com \
--cc=Michael.Hennerich@analog.com \
--cc=broonie@kernel.org \
--cc=conor+dt@kernel.org \
--cc=david@protonic.nl \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=kernel@martin.sperl.org \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
--cc=ukleinek@kernel.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;
as well as URLs for NNTP newsgroup(s).