diff mbox series

[1/2] arm64: dts: qcom: sc7180: Switch SPI to use GPIO for CS

Message ID 20200624170746.1.I997a428f58ef9d48b37a27a028360f34e66c00ec@changeid (mailing list archive)
State New, archived
Headers show
Series [1/2] arm64: dts: qcom: sc7180: Switch SPI to use GPIO for CS | expand

Commit Message

Douglas Anderson June 25, 2020, 12:08 a.m. UTC
The geni SPI protocol appears to have been designed without taking
Linux needs into account.  In all the normal flows it takes care of
setting chip select itself.  However, Linux likes to manage the chip
select so it can do fancy things.

Back when we first landed the geni SPI driver we worked around this
by:
- Always setting the FRAGMENTATION bit in transfers, which (I believe)
  tells the SPI controller not to mess with the chip select during
  transfers.
- Controlling the chip select manually by sending the SPI controller
  commands to assert or deassert the chip select.

Our workaround was fine and it's been working OK, but it's really
quite inefficient.  A normal SPI transaction now needs to do:
1. Start a command to set the chip select.
2. Wait for an interrupt from controller saying chip select done.
3. Do a transfer.
4. Start a command to unset the chip select.
5. Wait for an interrupt from controller saying chip select done.

Things are made a bit worse because the Linux GPIO framework assumes
that setting a chip select is quick.  Thus the SPI core can be seen to
tell us to set our chip select even when it's already in the right
state and we were dutifully kicking off commands and waiting for
interrupts.  While we could optimize that particular case, we'd still
be left with the slowness when we actually needed to toggle the chip
select.

All the chip select lines can actually be muxed to be GPIOs and
there's really no downside in doing so.  Now Linux can assert and
deassert the chip select at will with a simple MMIO write.

The SPI driver will still have the ability to set the chip select, but
not we just won't use it.

With this change I tested reading the firmware off the EC connected to
a ChromeOS device (flashrom -p ec -r ...).  I saw about a 25% speedup
in total runtime of the command and a 30% reduction in interrupts
generated (measured by /proc/interrupts).

Signed-off-by: Douglas Anderson <dianders@chromium.org>
---

 arch/arm64/boot/dts/qcom/sc7180.dtsi | 57 ++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 8 deletions(-)

Comments

Stephen Boyd June 25, 2020, 1:25 a.m. UTC | #1
Quoting Douglas Anderson (2020-06-24 17:08:04)
> The geni SPI protocol appears to have been designed without taking
> Linux needs into account.  In all the normal flows it takes care of
> setting chip select itself.  However, Linux likes to manage the chip
> select so it can do fancy things.
> 
> Back when we first landed the geni SPI driver we worked around this
> by:
> - Always setting the FRAGMENTATION bit in transfers, which (I believe)
>   tells the SPI controller not to mess with the chip select during
>   transfers.
> - Controlling the chip select manually by sending the SPI controller
>   commands to assert or deassert the chip select.
> 
> Our workaround was fine and it's been working OK, but it's really
> quite inefficient.  A normal SPI transaction now needs to do:
> 1. Start a command to set the chip select.
> 2. Wait for an interrupt from controller saying chip select done.
> 3. Do a transfer.
> 4. Start a command to unset the chip select.
> 5. Wait for an interrupt from controller saying chip select done.

I thought the GENI hardware was supposed to be able to queue commands up
and then plow through them to interrupt the CPU when it finished. If
that was supported would there be any problems? I assume we could remove
the wait for CS disable and interrupt on step 5 and also the wait for
CS/interrupt on step 2 because it is bundled with the transfer in step
3.

Where is the delay? On step 2 where we wait to transfer or at step 5
when we wait for CS to be dropped, or both?

> 
> Things are made a bit worse because the Linux GPIO framework assumes
> that setting a chip select is quick.  Thus the SPI core can be seen to
> tell us to set our chip select even when it's already in the right
> state and we were dutifully kicking off commands and waiting for
> interrupts.  While we could optimize that particular case, we'd still
> be left with the slowness when we actually needed to toggle the chip
> select.

One thing to note is that the GPIO driver doesn't tell us that it has
actually asserted/deasserted the GPIO. It writes to the controller and
moves on so we don't know when it has actually gone into effect.
Hopefully moving to GPIO mode doesn't mean we get weird problems where
CS isn't asserted yet and a transfer starts wiggling the lines.

> 
> All the chip select lines can actually be muxed to be GPIOs and
> there's really no downside in doing so.  Now Linux can assert and
> deassert the chip select at will with a simple MMIO write.
> 
> The SPI driver will still have the ability to set the chip select, but
> not we just won't use it.

s/not/now/?

> 
> With this change I tested reading the firmware off the EC connected to
> a ChromeOS device (flashrom -p ec -r ...).  I saw about a 25% speedup
> in total runtime of the command and a 30% reduction in interrupts
> generated (measured by /proc/interrupts).

I see nothing wrong with specifying the CS gpios in DT. Seems like that
should always be there and then the driver should decide to use GPIO
mode or not. So

Reviewed-by: Stephen Boyd <swboyd@chromium.org>

for that part.

> 
> Signed-off-by: Douglas Anderson <dianders@chromium.org>
> ---
> 
>  arch/arm64/boot/dts/qcom/sc7180.dtsi | 57 ++++++++++++++++++++++++----
>  1 file changed, 49 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/arm64/boot/dts/qcom/sc7180.dtsi b/arch/arm64/boot/dts/qcom/sc7180.dtsi
> index 3a8076c8bdbf..74c8503b560e 100644
> --- a/arch/arm64/boot/dts/qcom/sc7180.dtsi
> +++ b/arch/arm64/boot/dts/qcom/sc7180.dtsi
> @@ -1204,65 +1213,97 @@ pinmux {
>                         qup_spi0_default: qup-spi0-default {
>                                 pinmux {
>                                         pins = "gpio34", "gpio35",
> -                                              "gpio36", "gpio37";
> +                                              "gpio36";
>                                         function = "qup00";
>                                 };
> +                               pinmux-cs {
> +                                       pins = "gpio37";
> +                                       function = "gpio";
> +                               };
>                         };
>  
>                         qup_spi1_default: qup-spi1-default {
>                                 pinmux {
>                                         pins = "gpio0", "gpio1",
> -                                              "gpio2", "gpio3";
> +                                              "gpio2";
>                                         function = "qup01";
>                                 };
> +                               pinmux-cs {
> +                                       pins = "gpio3";
> +                                       function = "gpio";
> +                               };
>                         };
>  
>                         qup_spi3_default: qup-spi3-default {
>                                 pinmux {
>                                         pins = "gpio38", "gpio39",
> -                                              "gpio40", "gpio41";
> +                                              "gpio40";
>                                         function = "qup03";
>                                 };
> +                               pinmux-cs {
> +                                       pins = "gpio41";
> +                                       function = "gpio";
> +                               };
>                         };
>  
>                         qup_spi5_default: qup-spi5-default {
>                                 pinmux {
>                                         pins = "gpio25", "gpio26",
> -                                              "gpio27", "gpio28";
> +                                              "gpio27";
>                                         function = "qup05";
>                                 };
> +                               pinmux-cs {
> +                                       pins = "gpio28";
> +                                       function = "gpio";
> +                               };
>                         };
>  
>                         qup_spi6_default: qup-spi6-default {
>                                 pinmux {
>                                         pins = "gpio59", "gpio60",
> -                                              "gpio61", "gpio62";
> +                                              "gpio61";
>                                         function = "qup10";
>                                 };
> +                               pinmux-cs {
> +                                       pins = "gpio62";
> +                                       function = "gpio";
> +                               };
>                         };
>  
>                         qup_spi8_default: qup-spi8-default {
>                                 pinmux {
>                                         pins = "gpio42", "gpio43",
> -                                              "gpio44", "gpio45";
> +                                              "gpio44";
>                                         function = "qup12";
>                                 };
> +                               pinmux-cs {
> +                                       pins = "gpio45";
> +                                       function = "gpio";
> +                               };
>                         };
>  
>                         qup_spi10_default: qup-spi10-default {
>                                 pinmux {
>                                         pins = "gpio86", "gpio87",
> -                                              "gpio88", "gpio89";
> +                                              "gpio88";
>                                         function = "qup14";
>                                 };
> +                               pinmux-cs {
> +                                       pins = "gpio89";
> +                                       function = "gpio";
> +                               };
>                         };
>  
>                         qup_spi11_default: qup-spi11-default {
>                                 pinmux {
>                                         pins = "gpio53", "gpio54",
> -                                              "gpio55", "gpio56";
> +                                              "gpio55";
>                                         function = "qup15";
>                                 };
> +                               pinmux-cs {
> +                                       pins = "gpio56";
> +                                       function = "gpio";
> +                               };
>                         };

Perhaps we should have qup-spiN-default and qup-spiN-cs-gpio though?
That way the driver can properly mux the pin to be gpio mode if it wants
to. I don't see a way to mux the pin to be "qup" until the driver
decides it doesn't want that.
Douglas Anderson June 25, 2020, 3:35 a.m. UTC | #2
Hi,

On Wed, Jun 24, 2020 at 6:25 PM Stephen Boyd <swboyd@chromium.org> wrote:
>
> Quoting Douglas Anderson (2020-06-24 17:08:04)
> > The geni SPI protocol appears to have been designed without taking
> > Linux needs into account.  In all the normal flows it takes care of
> > setting chip select itself.  However, Linux likes to manage the chip
> > select so it can do fancy things.
> >
> > Back when we first landed the geni SPI driver we worked around this
> > by:
> > - Always setting the FRAGMENTATION bit in transfers, which (I believe)
> >   tells the SPI controller not to mess with the chip select during
> >   transfers.
> > - Controlling the chip select manually by sending the SPI controller
> >   commands to assert or deassert the chip select.
> >
> > Our workaround was fine and it's been working OK, but it's really
> > quite inefficient.  A normal SPI transaction now needs to do:
> > 1. Start a command to set the chip select.
> > 2. Wait for an interrupt from controller saying chip select done.
> > 3. Do a transfer.
> > 4. Start a command to unset the chip select.
> > 5. Wait for an interrupt from controller saying chip select done.
>
> I thought the GENI hardware was supposed to be able to queue commands up
> and then plow through them to interrupt the CPU when it finished. If
> that was supported would there be any problems? I assume we could remove
> the wait for CS disable and interrupt on step 5 and also the wait for
> CS/interrupt on step 2 because it is bundled with the transfer in step
> 3.

Do you have any details or pointers to documentation on said ability
to queue commands?  I don't think I've ever heard of it.

How exactly would it work, anyway?  There's no sequence number or
anything in GENI and there's a single "DONE" interrupt that signals
both "CS Done" and "Transfer Done", so without some really good docs
I'd have a really hard time figuring out how this is supposed to work.


> Where is the delay? On step 2 where we wait to transfer or at step 5
> when we wait for CS to be dropped, or both?

Presumably every CS change takes the same amount of time?  ...so I'd
say "both".  If it really matters I can try to measure.


> > Things are made a bit worse because the Linux GPIO framework assumes
> > that setting a chip select is quick.  Thus the SPI core can be seen to
> > tell us to set our chip select even when it's already in the right
> > state and we were dutifully kicking off commands and waiting for
> > interrupts.  While we could optimize that particular case, we'd still
> > be left with the slowness when we actually needed to toggle the chip
> > select.
>
> One thing to note is that the GPIO driver doesn't tell us that it has
> actually asserted/deasserted the GPIO. It writes to the controller and
> moves on so we don't know when it has actually gone into effect.
> Hopefully moving to GPIO mode doesn't mean we get weird problems where
> CS isn't asserted yet and a transfer starts wiggling the lines.

cs-gpios is a pretty normal Linux concept and not something I
invented.  It's documented to work just fine and I can't see this as
being a real problem.


> > All the chip select lines can actually be muxed to be GPIOs and
> > there's really no downside in doing so.  Now Linux can assert and
> > deassert the chip select at will with a simple MMIO write.
> >
> > The SPI driver will still have the ability to set the chip select, but
> > not we just won't use it.
>
> s/not/now/?

Sigh.  I always make that typo.  I can spin if need be to fix.


> > With this change I tested reading the firmware off the EC connected to
> > a ChromeOS device (flashrom -p ec -r ...).  I saw about a 25% speedup
> > in total runtime of the command and a 30% reduction in interrupts
> > generated (measured by /proc/interrupts).
>
> I see nothing wrong with specifying the CS gpios in DT. Seems like that
> should always be there and then the driver should decide to use GPIO
> mode or not. So
>
> Reviewed-by: Stephen Boyd <swboyd@chromium.org>
>
> for that part.

I appreciate the tag, but I'm not sure it's working the way you're
thinking it does?  See:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/spi/spi-controller.yaml

From there, when you specify spi-gpios it's saying: definitely use
this GPIO as the chip select, don't use your native one.  The bindings
explicitly show how you would specify the native GPIO.

If we wanted the SPI controller to decide one way or the other on its
own, I guess we'd need an entirely new property saying that if it
wanted to control its chip select with GPIO then here's the GPIO and
then we'd need to provide a pinmux config for that.  That feels
overkill to me since I really see no reason not to use it as a GPIO.

It's kinda like saying: if an SoC provided two different ways to set a
pin, one of which always delayed 50 us to assert and one that asserted
instantly, do we really need to write drivers to support both modes?
No.  The mode which delays 50 us is useless and there was no good
reason for the SoC to invent that mode and no reason to try to support
it in software.


> > Signed-off-by: Douglas Anderson <dianders@chromium.org>
> > ---
> >
> >  arch/arm64/boot/dts/qcom/sc7180.dtsi | 57 ++++++++++++++++++++++++----
> >  1 file changed, 49 insertions(+), 8 deletions(-)
> >
> > diff --git a/arch/arm64/boot/dts/qcom/sc7180.dtsi b/arch/arm64/boot/dts/qcom/sc7180.dtsi
> > index 3a8076c8bdbf..74c8503b560e 100644
> > --- a/arch/arm64/boot/dts/qcom/sc7180.dtsi
> > +++ b/arch/arm64/boot/dts/qcom/sc7180.dtsi
> > @@ -1204,65 +1213,97 @@ pinmux {
> >                         qup_spi0_default: qup-spi0-default {
> >                                 pinmux {
> >                                         pins = "gpio34", "gpio35",
> > -                                              "gpio36", "gpio37";
> > +                                              "gpio36";
> >                                         function = "qup00";
> >                                 };
> > +                               pinmux-cs {
> > +                                       pins = "gpio37";
> > +                                       function = "gpio";
> > +                               };
> >                         };
> >
> >                         qup_spi1_default: qup-spi1-default {
> >                                 pinmux {
> >                                         pins = "gpio0", "gpio1",
> > -                                              "gpio2", "gpio3";
> > +                                              "gpio2";
> >                                         function = "qup01";
> >                                 };
> > +                               pinmux-cs {
> > +                                       pins = "gpio3";
> > +                                       function = "gpio";
> > +                               };
> >                         };
> >
> >                         qup_spi3_default: qup-spi3-default {
> >                                 pinmux {
> >                                         pins = "gpio38", "gpio39",
> > -                                              "gpio40", "gpio41";
> > +                                              "gpio40";
> >                                         function = "qup03";
> >                                 };
> > +                               pinmux-cs {
> > +                                       pins = "gpio41";
> > +                                       function = "gpio";
> > +                               };
> >                         };
> >
> >                         qup_spi5_default: qup-spi5-default {
> >                                 pinmux {
> >                                         pins = "gpio25", "gpio26",
> > -                                              "gpio27", "gpio28";
> > +                                              "gpio27";
> >                                         function = "qup05";
> >                                 };
> > +                               pinmux-cs {
> > +                                       pins = "gpio28";
> > +                                       function = "gpio";
> > +                               };
> >                         };
> >
> >                         qup_spi6_default: qup-spi6-default {
> >                                 pinmux {
> >                                         pins = "gpio59", "gpio60",
> > -                                              "gpio61", "gpio62";
> > +                                              "gpio61";
> >                                         function = "qup10";
> >                                 };
> > +                               pinmux-cs {
> > +                                       pins = "gpio62";
> > +                                       function = "gpio";
> > +                               };
> >                         };
> >
> >                         qup_spi8_default: qup-spi8-default {
> >                                 pinmux {
> >                                         pins = "gpio42", "gpio43",
> > -                                              "gpio44", "gpio45";
> > +                                              "gpio44";
> >                                         function = "qup12";
> >                                 };
> > +                               pinmux-cs {
> > +                                       pins = "gpio45";
> > +                                       function = "gpio";
> > +                               };
> >                         };
> >
> >                         qup_spi10_default: qup-spi10-default {
> >                                 pinmux {
> >                                         pins = "gpio86", "gpio87",
> > -                                              "gpio88", "gpio89";
> > +                                              "gpio88";
> >                                         function = "qup14";
> >                                 };
> > +                               pinmux-cs {
> > +                                       pins = "gpio89";
> > +                                       function = "gpio";
> > +                               };
> >                         };
> >
> >                         qup_spi11_default: qup-spi11-default {
> >                                 pinmux {
> >                                         pins = "gpio53", "gpio54",
> > -                                              "gpio55", "gpio56";
> > +                                              "gpio55";
> >                                         function = "qup15";
> >                                 };
> > +                               pinmux-cs {
> > +                                       pins = "gpio56";
> > +                                       function = "gpio";
> > +                               };
> >                         };
>
> Perhaps we should have qup-spiN-default and qup-spiN-cs-gpio though?
> That way the driver can properly mux the pin to be gpio mode if it wants
> to. I don't see a way to mux the pin to be "qup" until the driver
> decides it doesn't want that.

This seems like extra complexity.  Why would the SPI controller ever
need to control the GPIO itself.  If we ever do come up with a reason
we can solve it then?

-Doug
Douglas Anderson June 25, 2020, 6:48 p.m. UTC | #3
Hi,

On Wed, Jun 24, 2020 at 11:54 PM Stephen Boyd <swboyd@chromium.org> wrote:
>
> Quoting Doug Anderson (2020-06-24 20:35:16)
> > Hi,
> >
> > On Wed, Jun 24, 2020 at 6:25 PM Stephen Boyd <swboyd@chromium.org> wrote:
> > >
> > > Quoting Douglas Anderson (2020-06-24 17:08:04)
> > > > The geni SPI protocol appears to have been designed without taking
> > > > Linux needs into account.  In all the normal flows it takes care of
> > > > setting chip select itself.  However, Linux likes to manage the chip
> > > > select so it can do fancy things.
> > > >
> > > > Back when we first landed the geni SPI driver we worked around this
> > > > by:
> > > > - Always setting the FRAGMENTATION bit in transfers, which (I believe)
> > > >   tells the SPI controller not to mess with the chip select during
> > > >   transfers.
> > > > - Controlling the chip select manually by sending the SPI controller
> > > >   commands to assert or deassert the chip select.
> > > >
> > > > Our workaround was fine and it's been working OK, but it's really
> > > > quite inefficient.  A normal SPI transaction now needs to do:
> > > > 1. Start a command to set the chip select.
> > > > 2. Wait for an interrupt from controller saying chip select done.
> > > > 3. Do a transfer.
> > > > 4. Start a command to unset the chip select.
> > > > 5. Wait for an interrupt from controller saying chip select done.
> > >
> > > I thought the GENI hardware was supposed to be able to queue commands up
> > > and then plow through them to interrupt the CPU when it finished. If
> > > that was supported would there be any problems? I assume we could remove
> > > the wait for CS disable and interrupt on step 5 and also the wait for
> > > CS/interrupt on step 2 because it is bundled with the transfer in step
> > > 3.
> >
> > Do you have any details or pointers to documentation on said ability
> > to queue commands?  I don't think I've ever heard of it.
> >
> > How exactly would it work, anyway?  There's no sequence number or
> > anything in GENI and there's a single "DONE" interrupt that signals
> > both "CS Done" and "Transfer Done", so without some really good docs
> > I'd have a really hard time figuring out how this is supposed to work.
>
> The driver is written to do the simplest thing possible, FIFOs and lots
> of interrupts. There are three different modes: PIO, DMA, and GSI/GPI
> (Generic Software Interface/Generic Programming Interface (those are
> ridiculous acronyms)). I don't know if the DMA mode gains anything,
> probably not except for large transfers but then why not use GSI? The
> performance gain, as far as I can tell, is to use GSI/GPI to program
> sequences into the serial engine (SE) and then kick them off and wait.
> The other side benefit of this is that it lets us share the same QUP
> between different EEs (execution environments) in the system.
>
> I hope someone at qcom can help to upstream support for this if
> performance is important. Otherwise we're left to stick band-aids on a
> driver that is polling a FIFO (sadness). I googled for the google pixel4
> kernel and found this[1] so maybe that can be used as a reference to
> support GSI mode. It looks like CS is stuck into the GSI queue thing
> (see go_flags and FRAGMENTATION[3]).

Definitely I'd be interested to see patches to make SPI work faster.
I'm going to try to characterize how slow things still are and then
will probably try to convince Qualcomm to help since I didn't see lots
of other obvious speedups.  Maybe, as you say, GSI/GPI is magic.

One thing I realized thinking through all this, though.  If we just
naively queue chip select commands without waiting for completion
we'll break Linux's assumptions.  Specifically Linux assumes that when
the SPI driver's set_cs() command returns that the chip select has
actually been set.  If a SPI client drivers sometimes need chip select
to be asserted for a minimum amount of time and so they need to know
that set_cs() actually did its job.  Checkout "struct spi_transfer"
and elements like "delay_usecs".  I know cros_ec uses some of these
fancier features.  See do_cros_ec_pkt_xfer_spi() for reference.

Ah, I guess if the GSI/GPI mode is fancy enough, though, maybe we
could implement one layer higher in the SPI core and provide a
transfer_one_message() instead of transfer_one().  Then we'd be fully
in charge of doing all of the delays ourselves.  So if we could queue
up a "set cs, optional delay, do transfer, optional delay, set cs,
optional delay, ..." then maybe it'd be OK.  I guess if there could be
multiple execution environments using the SPI we'd absolutely need to
implement this anyway since other EEs using the QUP would screw up the
standard timeout calculations.  Even if we did this, though, we still
might have trouble with multiple EEs for some fancier Linux drivers
without more work.  Specifically Linux drivers are allowed to call
spi_bus_lock() and then assume that they have full control of the bus
even across multiple messages.  I guess we'd need some way to make
sure that other EEs aren't running and then lock them out?  We also
would have to figure out how to handle the Linux API which allows you
to mess with lots of SPI parameters (including transfer speed) in the
middle of a message.


So I guess the summary of this is:

- Using GSI mode is a reason to not use GPIOs for chip select assuming
that GSI mode can actually support everything we need and is actually
faster.

- If there are multiple EEs then (mabye?) we can't use GPIOs for chip
select even ignoring GSI mode.  I don't know enough about multiple EEs
and what kind of sharing is supported to know for sure.  Is it
expected that multiple EEs are talking to the same physical peripheral
or are they using the same QUP but w/ a different chip select?  Are
pin mux settings forced to be the same for all EEs?


I guess the question is: are we interested in a short term speedup
while we wait for a better solution?  Maybe the right solution is to
just override this to use GPIOs only on boards known not to support
multiple EEs instead of overriding it in the overall SoC device tree
file?


> > > Where is the delay? On step 2 where we wait to transfer or at step 5
> > > when we wait for CS to be dropped, or both?
> >
> > Presumably every CS change takes the same amount of time?  ...so I'd
> > say "both".  If it really matters I can try to measure.
>
> Sure. You say you got a 25% speedup so I'm curious where the time
> improvement came from and what the raw numbers were before and after
> this patch.

In all cases I did 3 runs, so providing the average and raw:

# flashrom -p ec -r /tmp/foo.bin

IRQs before: 55796 ((55258 + 55613 + 56519) / 3)
IRQs after: 37512 ((37654 + 36312 + 38571) / 3)

Time before: 11.991 ((12.049 + 11.985 + 11.938) / 3)
Time after: 9.111 ((8.894 + 9.428 + 9.012) / 3)

# time flashrom -p ec -w /tmp/foo.bin

IRQs before: 52568 ((52343 + 52666 + 52696) / 3)
IRQs after: 36507 ((36051 + 35745 + 37727) / 3)

Time before: 13.015 ((12.940 + 13.004 + 13.100) / 3)
Time after: 9.325 ((8.934 + 9.520 +9.522) / 3)


I also gathered a bunch of timings.  Timings at boot are pretty wild
because so many things are happening at once, but it calms down a bit
later on.  Here are two patches I used for timings which also include
some sample timings in their description:

https://crrev.com/c/2267286
https://crrev.com/c/2267290

The quick summary is: even assuming no other overhead at all it takes
about 8 - 9 us just to assert the chip select.  The IRQ overhead isn't
a ton usually (2 - 4 us) but it takes a little while (~10us with lots
of variation) before we start running again on the task that was
waiting.

One thing I did note in the commit message is that I can also get a
speedup (though not as impressive) without configuring the CS to be a
GPIO by just caching the chip select state in the driver and avoiding
setting the chip select when it's already correct.  I still have this
patch, though it doesn't do anything if we switch the CS to GPIO.  I
can still post it if needed.  I stuck it at
<https://crrev.com/c/2267278> for now.  That only gave a 15% speedup,
not 25%.


> > > > Things are made a bit worse because the Linux GPIO framework assumes
> > > > that setting a chip select is quick.  Thus the SPI core can be seen to
> > > > tell us to set our chip select even when it's already in the right
> > > > state and we were dutifully kicking off commands and waiting for
> > > > interrupts.  While we could optimize that particular case, we'd still
> > > > be left with the slowness when we actually needed to toggle the chip
> > > > select.
> > >
> > > One thing to note is that the GPIO driver doesn't tell us that it has
> > > actually asserted/deasserted the GPIO. It writes to the controller and
> > > moves on so we don't know when it has actually gone into effect.
> > > Hopefully moving to GPIO mode doesn't mean we get weird problems where
> > > CS isn't asserted yet and a transfer starts wiggling the lines.
> >
> > cs-gpios is a pretty normal Linux concept and not something I
> > invented.  It's documented to work just fine and I can't see this as
> > being a real problem.
>
> This isn't just cs-gpios though. It's a general problem for the gpio
> controller on qcom platforms. I take it that it's worked well in the
> past so you feel confident that cs-gpios is solid. That's fair.

Hrm.  Is the qcom gpio driver somehow different from other platforms?
I think most code assumes that if it asserts a GPIO that it's
asserted, since it might be doing timing based on setting that GPIO.
I mean there are certainly things like pin rise times and also the
time it takes for the MMIO write to go over the bus, but I don't think
we need to worry about them here.


> > > > With this change I tested reading the firmware off the EC connected to
> > > > a ChromeOS device (flashrom -p ec -r ...).  I saw about a 25% speedup
> > > > in total runtime of the command and a 30% reduction in interrupts
> > > > generated (measured by /proc/interrupts).
> > >
> > > I see nothing wrong with specifying the CS gpios in DT. Seems like that
> > > should always be there and then the driver should decide to use GPIO
> > > mode or not. So
> > >
> > > Reviewed-by: Stephen Boyd <swboyd@chromium.org>
> > >
> > > for that part.
> >
> > I appreciate the tag, but I'm not sure it's working the way you're
> > thinking it does?  See:
> >
> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/spi/spi-controller.yaml
> >
> > From there, when you specify spi-gpios it's saying: definitely use
> > this GPIO as the chip select, don't use your native one.  The bindings
> > explicitly show how you would specify the native GPIO.
> >
> > If we wanted the SPI controller to decide one way or the other on its
> > own, I guess we'd need an entirely new property saying that if it
> > wanted to control its chip select with GPIO then here's the GPIO and
> > then we'd need to provide a pinmux config for that.  That feels
> > overkill to me since I really see no reason not to use it as a GPIO.
>
> In the case of GPI or DMA mode or not either of those there seems to be
> quite a bit of overkill^Wconfigurability. Maybe the device node should
> get a different compatible string in these cases so that we can
> differentiate what is the intended mode of operation of the QUP.
> Otherwise I don't see how GSI could be supported when cs-gpios is
> present.
>
> The thing that shouldn't be happening is needing to change the dts file
> at the SoC level to make the driver do something different when that
> something different can be decided by the OS. More succinctly, from a DT
> perspective something is wrong if we're tweaking this node to change
> driver behavior.

I agree that GSI doesn't seem like it could be supported when cs-gpios
is present.

I guess I'm not quite as wedded to the immutability of the dts.  While
it's nice to not totally churn the dts and we're supposed to keep in
mind that the dts is supposed to be mostly stable, it doesn't feel
horrible to me to say that if you wanted to enable GSI that you'd have
to change the dts.

How about if we look at it this way: wouldn't it be possible to design
a board where the chip select of the SPI was over on some totally
different GPIO that's not part of a QUP?  The generic SPI bindings
(spi-controller.yaml) explicitly call this out as an OK thing to do,
saying that you could even have some devices using the native
controller chip select and some devices using a GPIO and that's OK.

Assuming we accept that as a valid use case then the SPI driver needs
to be able to work in a non-GSI mode if it's doing a transfer that's
using a GPIO chip select, right?  So if the dts specifies cs-gpios it
will still need to work.


Describing the SPI connection using cs-gpios instead of the native cs
is also not false.  The chip select line _is_ hooked up to a GPIO.
That same line can also be muxed to be the native chip select but that
doesn't make it untrue that it can also be thought of as a GPIO.

I mean, we could also certainly add an entry like this:

  qcom,native-cs-gpio = <...>

...and then we could add a second pinmux config in addition to
"default" called "native-cs-gpio" or something.  Then, if the driver
detected that "native-cs-gpio" (the GPIO and the pinconf) were there
and the driver hadn't yet implemented GSI and the driver knew there
weren't multiple execution environments then it could change the
pinmux and just set the GPIO in its set_cs() function.  That would be
more pristine from a device tree perspective (we're describing
_everything_), but it would also be a heck of a lot more code and
complexity.


Maybe yet another way to think about this whole thing: presumably we
would need firmware changes if we wanted to support sharing a SPI port
across multiple execution environments, right?  The dts describes not
just hardware but also (to some extent) firmware.  When we have
firmware that supports multiple EEs and it's all working then that
would be a good time to change the dts to describe the device+firmware
combo.


So to wrap it all up w/ a bow:

* I can post the SPI change that avoids setting the CS if it was
already right so everyone can get a (smaller) perf boost right away.
Simple and clean.

* We make the change to use cs-gpios just in board-specific device
trees for boards known not to support multiple EEs.  If/when firmware
changes are needed to support multiple EEs we'll update the dts.

* Geni SPI driver should always support both cs-gpios and native cs,
so we don't have to worry about incompatible changes.  Both are valid
ways to describe the device (assuming multiple EEs aren't used).

* If/when we get GSI code we won't be able to use it without changing
away from cs-gpios but cs-gpios should still be functional.


> > It's kinda like saying: if an SoC provided two different ways to set a
> > pin, one of which always delayed 50 us to assert and one that asserted
> > instantly, do we really need to write drivers to support both modes?
> > No.  The mode which delays 50 us is useless and there was no good
> > reason for the SoC to invent that mode and no reason to try to support
> > it in software.
> >
>
> [1] https://android.googlesource.com/kernel/msm/+/refs/heads/android-msm-coral-4.14-android10-c2f2/drivers/spi/spi-geni-qcom.c
>
> P.S. I see that android "fixed"[2] a race condition in the interrupt
> routine. Sounds familiar...
>
> [2] https://android.googlesource.com/kernel/msm/+/4ba87b2911be7a83e710c0211f2c1e512d3cd196
> [3] https://android.googlesource.com/kernel/msm/+/refs/heads/android-msm-coral-4.14-android10-c2f2/drivers/spi/spi-geni-qcom.c#655

It makes me incredibly disappointed to see fixes like this that
weren't posted upstream.

-Doug
diff mbox series

Patch

diff --git a/arch/arm64/boot/dts/qcom/sc7180.dtsi b/arch/arm64/boot/dts/qcom/sc7180.dtsi
index 3a8076c8bdbf..74c8503b560e 100644
--- a/arch/arm64/boot/dts/qcom/sc7180.dtsi
+++ b/arch/arm64/boot/dts/qcom/sc7180.dtsi
@@ -10,6 +10,7 @@ 
 #include <dt-bindings/clock/qcom,gpucc-sc7180.h>
 #include <dt-bindings/clock/qcom,rpmh.h>
 #include <dt-bindings/clock/qcom,videocc-sc7180.h>
+#include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/interconnect/qcom,sc7180.h>
 #include <dt-bindings/interrupt-controller/arm-gic.h>
 #include <dt-bindings/phy/phy-qcom-qusb2.h>
@@ -570,6 +571,7 @@  spi0: spi@880000 {
 				pinctrl-names = "default";
 				pinctrl-0 = <&qup_spi0_default>;
 				interrupts = <GIC_SPI 601 IRQ_TYPE_LEVEL_HIGH>;
+				cs-gpios = <&tlmm 37 GPIO_ACTIVE_LOW>;
 				#address-cells = <1>;
 				#size-cells = <0>;
 				status = "disabled";
@@ -607,6 +609,7 @@  spi1: spi@884000 {
 				pinctrl-names = "default";
 				pinctrl-0 = <&qup_spi1_default>;
 				interrupts = <GIC_SPI 602 IRQ_TYPE_LEVEL_HIGH>;
+				cs-gpios = <&tlmm 3 GPIO_ACTIVE_LOW>;
 				#address-cells = <1>;
 				#size-cells = <0>;
 				status = "disabled";
@@ -668,6 +671,7 @@  spi3: spi@88c000 {
 				pinctrl-names = "default";
 				pinctrl-0 = <&qup_spi3_default>;
 				interrupts = <GIC_SPI 604 IRQ_TYPE_LEVEL_HIGH>;
+				cs-gpios = <&tlmm 41 GPIO_ACTIVE_LOW>;
 				#address-cells = <1>;
 				#size-cells = <0>;
 				status = "disabled";
@@ -729,6 +733,7 @@  spi5: spi@894000 {
 				pinctrl-names = "default";
 				pinctrl-0 = <&qup_spi5_default>;
 				interrupts = <GIC_SPI 606 IRQ_TYPE_LEVEL_HIGH>;
+				cs-gpios = <&tlmm 28 GPIO_ACTIVE_LOW>;
 				#address-cells = <1>;
 				#size-cells = <0>;
 				status = "disabled";
@@ -779,6 +784,7 @@  spi6: spi@a80000 {
 				pinctrl-names = "default";
 				pinctrl-0 = <&qup_spi6_default>;
 				interrupts = <GIC_SPI 353 IRQ_TYPE_LEVEL_HIGH>;
+				cs-gpios = <&tlmm 62 GPIO_ACTIVE_LOW>;
 				#address-cells = <1>;
 				#size-cells = <0>;
 				status = "disabled";
@@ -840,6 +846,7 @@  spi8: spi@a88000 {
 				pinctrl-names = "default";
 				pinctrl-0 = <&qup_spi8_default>;
 				interrupts = <GIC_SPI 355 IRQ_TYPE_LEVEL_HIGH>;
+				cs-gpios = <&tlmm 45 GPIO_ACTIVE_LOW>;
 				#address-cells = <1>;
 				#size-cells = <0>;
 				status = "disabled";
@@ -901,6 +908,7 @@  spi10: spi@a90000 {
 				pinctrl-names = "default";
 				pinctrl-0 = <&qup_spi10_default>;
 				interrupts = <GIC_SPI 357 IRQ_TYPE_LEVEL_HIGH>;
+				cs-gpios = <&tlmm 89 GPIO_ACTIVE_LOW>;
 				#address-cells = <1>;
 				#size-cells = <0>;
 				status = "disabled";
@@ -938,6 +946,7 @@  spi11: spi@a94000 {
 				pinctrl-names = "default";
 				pinctrl-0 = <&qup_spi11_default>;
 				interrupts = <GIC_SPI 358 IRQ_TYPE_LEVEL_HIGH>;
+				cs-gpios = <&tlmm 56 GPIO_ACTIVE_LOW>;
 				#address-cells = <1>;
 				#size-cells = <0>;
 				status = "disabled";
@@ -1204,65 +1213,97 @@  pinmux {
 			qup_spi0_default: qup-spi0-default {
 				pinmux {
 					pins = "gpio34", "gpio35",
-					       "gpio36", "gpio37";
+					       "gpio36";
 					function = "qup00";
 				};
+				pinmux-cs {
+					pins = "gpio37";
+					function = "gpio";
+				};
 			};
 
 			qup_spi1_default: qup-spi1-default {
 				pinmux {
 					pins = "gpio0", "gpio1",
-					       "gpio2", "gpio3";
+					       "gpio2";
 					function = "qup01";
 				};
+				pinmux-cs {
+					pins = "gpio3";
+					function = "gpio";
+				};
 			};
 
 			qup_spi3_default: qup-spi3-default {
 				pinmux {
 					pins = "gpio38", "gpio39",
-					       "gpio40", "gpio41";
+					       "gpio40";
 					function = "qup03";
 				};
+				pinmux-cs {
+					pins = "gpio41";
+					function = "gpio";
+				};
 			};
 
 			qup_spi5_default: qup-spi5-default {
 				pinmux {
 					pins = "gpio25", "gpio26",
-					       "gpio27", "gpio28";
+					       "gpio27";
 					function = "qup05";
 				};
+				pinmux-cs {
+					pins = "gpio28";
+					function = "gpio";
+				};
 			};
 
 			qup_spi6_default: qup-spi6-default {
 				pinmux {
 					pins = "gpio59", "gpio60",
-					       "gpio61", "gpio62";
+					       "gpio61";
 					function = "qup10";
 				};
+				pinmux-cs {
+					pins = "gpio62";
+					function = "gpio";
+				};
 			};
 
 			qup_spi8_default: qup-spi8-default {
 				pinmux {
 					pins = "gpio42", "gpio43",
-					       "gpio44", "gpio45";
+					       "gpio44";
 					function = "qup12";
 				};
+				pinmux-cs {
+					pins = "gpio45";
+					function = "gpio";
+				};
 			};
 
 			qup_spi10_default: qup-spi10-default {
 				pinmux {
 					pins = "gpio86", "gpio87",
-					       "gpio88", "gpio89";
+					       "gpio88";
 					function = "qup14";
 				};
+				pinmux-cs {
+					pins = "gpio89";
+					function = "gpio";
+				};
 			};
 
 			qup_spi11_default: qup-spi11-default {
 				pinmux {
 					pins = "gpio53", "gpio54",
-					       "gpio55", "gpio56";
+					       "gpio55";
 					function = "qup15";
 				};
+				pinmux-cs {
+					pins = "gpio56";
+					function = "gpio";
+				};
 			};
 
 			qup_uart0_default: qup-uart0-default {