linux/drivers/spi
Lukas Wunner 8259bf667a
spi: bcm2835: Speed up TX-only DMA transfers by clearing RX FIFO
The BCM2835 SPI driver currently sets the SPI_CONTROLLER_MUST_RX flag.
When performing a TX-only transfer, this flag causes the SPI core to
allocate and DMA-map a dummy buffer into which the RX FIFO contents are
copied.  The dummy buffer is necessary because the chip is not capable
of disabling the receiver or automatically throwing away received data.
Not reading the RX FIFO isn't an option either since transmission is
halted once it's full.

Avoid the overhead induced by the dummy buffer by preallocating a
reusable DMA transaction which cyclically clears the RX FIFO.  The
transaction requires very little CPU time to submit and generates no
interrupts while running.  Specifics are provided in kerneldoc comments.

With a ks8851 Ethernet chip attached to the SPI controller, I am seeing
a 30 us reduction in ping time with this commit (1.819 ms vs. 1.849 ms,
average of 100,000 packets) as well as a 2% reduction in CPU time
(75:08 vs. 76:39 for transmission of 5 GByte over the SPI bus).

The commit uses the TX DMA interrupt to signal completion of a transfer.
This interrupt is raised once all bytes have been written to the
TX FIFO and it is then necessary to busy-wait for the TX FIFO to become
empty before the transfer can be finalized.  As an alternative approach,
I have explored using the SPI controller's DONE interrupt to detect
completion.  This interrupt is signaled when the TX FIFO becomes empty,
avoiding the need to busy-wait.  However latency deteriorates compared
to the present commit and surprisingly, CPU time is slightly higher as
well:

It turns out that in 45% of the cases, no busy-waiting is needed at all
and in 76% of the cases, less than 10 busy-wait iterations are
sufficient for the TX FIFO to drain.  This was measured on an RT kernel.
On a vanilla kernel, wakeup latency is worse and thus fewer iterations
are needed.  The measurements were made with an SPI clock of 20 MHz,
they may differ slightly for slower or faster clock speeds.

Previously we always used the RX DMA interrupt to signal completion of a
transfer.  Using the TX DMA interrupt now introduces a race condition:
TX DMA is always started before RX DMA so that bytes are already clocked
out while RX DMA is still being set up.  But if a TX-only transfer is
very short, then the TX DMA interrupt may occur before RX DMA is set up.
If the interrupt happens to occur on the same CPU, setup of RX DMA may
even be delayed until after the interrupt was handled.

I've solved this by having the TX DMA callback clear the RX FIFO while
busy-waiting for the TX FIFO to drain, thus avoiding a dependency on
setup of RX DMA.  Additionally, I am using a lock-free mechanism with
two flags, tx_dma_active and rx_dma_active plus memory barriers to
terminate RX DMA either by the TX DMA callback or immediately after
setting it up, whichever wins the race.  I've explored an alternative
approach which temporarily disables the TX DMA callback until RX DMA
has been set up (using tasklet_disable(), local_bh_disable() or
local_irq_save()), but the performance was minimally worse.

[Nathan Chancellor contributed a DMA mapping fixup for an early version
of this commit, hence his Signed-off-by.]

Tested-by: Nuno Sá <nuno.sa@analog.com>
Tested-by: Noralf Trønnes <noralf@tronnes.org>
Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Acked-by: Stefan Wahren <wahrenst@gmx.net>
Acked-by: Martin Sperl <kernel@martin.sperl.org>
Cc: Robert Jarzmik <robert.jarzmik@free.fr>
Link: https://lore.kernel.org/r/874949385f28251e2dcaa9494e39a27b50e9f9e4.1568187525.git.lukas@wunner.de
Signed-off-by: Mark Brown <broonie@kernel.org>
2019-09-11 15:57:30 +01:00
..
atmel-quadspi.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
internals.h
Kconfig spi: npcm-fiu: add NPCM FIU controller driver 2019-08-30 12:42:07 +01:00
Makefile spi: npcm-fiu: add NPCM FIU controller driver 2019-08-30 12:42:07 +01:00
spi-altera.c spi: altera: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:14:50 +01:00
spi-armada-3700.c spi: a3700: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:15:09 +01:00
spi-at91-usart.c spi: at91-usart: add DMA support 2019-05-08 17:45:26 +09:00
spi-ath79.c spi: ath79: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:15:29 +01:00
spi-atmel.c spi: atmel: add tracing to custom .transfer_one_message callback 2019-08-02 12:16:02 +01:00
spi-au1550.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-axi-spi-engine.c spi: spi-axi: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:15:49 +01:00
spi-bcm63xx-hsspi.c spi: bcm63xx-hsspi: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:16:47 +01:00
spi-bcm63xx.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-bcm2835.c spi: bcm2835: Speed up TX-only DMA transfers by clearing RX FIFO 2019-09-11 15:57:30 +01:00
spi-bcm2835aux.c spi: bcm2835aux: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:16:08 +01:00
spi-bcm-qspi.c spi: bcm-qspi: Make BSPI default mode 2019-08-30 12:42:20 +01:00
spi-bcm-qspi.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 459 2019-06-19 17:09:09 +02:00
spi-bitbang-txrx.h
spi-bitbang.c Linux 5.2-rc4 2019-06-10 18:52:53 +01:00
spi-brcmstb-qspi.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 459 2019-06-19 17:09:09 +02:00
spi-butterfly.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-cadence.c spi: cadence: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:17:07 +01:00
spi-cavium-octeon.c spi: octeon: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:17:27 +01:00
spi-cavium-thunderx.c treewide: Add SPDX license identifier for more missed files 2019-05-21 10:50:45 +02:00
spi-cavium.c
spi-cavium.h
spi-clps711x.c spi: clps711x: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:17:36 +01:00
spi-coldfire-qspi.c spi: coldfire-qspi: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:17:56 +01:00
spi-davinci.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-dln2.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 372 2019-06-05 17:37:10 +02:00
spi-dw-mid.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 288 2019-06-05 17:36:37 +02:00
spi-dw-mmio.c spi: dw-mmio: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:18:05 +01:00
spi-dw-pci.c Merge branch 'spi-5.3' into spi-5.4 2019-08-23 12:00:22 +01:00
spi-dw.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 288 2019-06-05 17:36:37 +02:00
spi-dw.h
spi-efm32.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-ep93xx.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-falcon.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-fsl-cpm.c spi: fsl-spi: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:24:26 +01:00
spi-fsl-cpm.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
spi-fsl-dspi.c spi: spi-fsl-dspi: Fix race condition in TCFQ/EOQ interrupt 2019-09-03 12:40:05 +01:00
spi-fsl-espi.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
spi-fsl-lib.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
spi-fsl-lib.h spi: fsl: Convert to use CS GPIO descriptors 2019-08-28 14:10:41 +01:00
spi-fsl-lpspi.c spi: fsl-lpspi: Clean up fsl_lpspi_probe 2019-04-08 14:08:53 +07:00
spi-fsl-qspi.c Merge branch 'spi-5.3' into spi-5.4 2019-08-23 12:00:22 +01:00
spi-fsl-spi.c spi: fsl: Convert to use CS GPIO descriptors 2019-08-28 14:10:41 +01:00
spi-fsl-spi.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
spi-geni-qcom.c spi: spi-geni-qcom: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:18:25 +01:00
spi-gpio.c spi-gpio: Use PTR_ERR_OR_ZERO() in spi_gpio_request() 2019-09-09 11:05:39 +01:00
spi-img-spfi.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 422 2019-06-05 17:37:15 +02:00
spi-imx.c Merge branch 'spi-5.1' into spi-5.2 for stm32 2019-03-15 17:06:34 +00:00
spi-iproc-qspi.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 174 2019-05-30 11:26:41 -07:00
spi-jcore.c treewide: Add SPDX license identifier for more missed files 2019-05-21 10:50:45 +02:00
spi-lantiq-ssc.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-lm70llp.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-loopback-test.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-lp8841-rtc.c spi: lp-8841: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:18:35 +01:00
spi-mem.c spi-mem: fix kernel-doc for spi_mem_dirmap_{read|write}() 2019-04-08 14:06:51 +07:00
spi-meson-spicc.c spi: meson-spicc: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:18:55 +01:00
spi-meson-spifc.c spi: spi-meson-spifc: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:19:14 +01:00
spi-mpc52xx-psc.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
spi-mpc52xx.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 428 2019-06-05 17:37:16 +02:00
spi-mpc512x-psc.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
spi-mt65xx.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-mt7621.c spi: mt7621: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:19:34 +01:00
spi-mxic.c spi: mxic: simplify getting .driver_data 2019-03-20 17:20:29 +00:00
spi-mxs.c spi: mxs: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:19:54 +01:00
spi-npcm-fiu.c spi: npcm-fiu: remove set but not used variable 'retlen' 2019-09-05 18:19:45 +01:00
spi-npcm-pspi.c spi: npcm: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:20:03 +01:00
spi-nuc900.c spi: nuc900: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:20:23 +01:00
spi-nxp-fspi.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-oc-tiny.c spi: oc-tiny: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:20:33 +01:00
spi-omap2-mcspi.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-omap-100k.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-omap-uwire.c
spi-orion.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-pic32-sqi.c spi: pic32-sqi: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:20:52 +01:00
spi-pic32.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-pl022.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-ppc4xx.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-pxa2xx-dma.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-pxa2xx-pci.c treewide: Add SPDX license identifier for more missed files 2019-05-21 10:50:45 +02:00
spi-pxa2xx.c spi: pxa2xx: Add support for Intel Tiger Lake 2019-08-02 12:14:23 +01:00
spi-pxa2xx.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-qcom-qspi.c spi: spi-qcom-qspi: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:21:12 +01:00
spi-qup.c Merge branch 'spi-5.3' into spi-next 2019-07-04 17:35:07 +01:00
spi-rb4xx.c spi: rb4xx: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:21:21 +01:00
spi-rockchip.c Linux 5.2-rc4 2019-06-10 18:52:53 +01:00
spi-rspi.c spi: rspi: Fix handling of QSPI code when transmit and receive 2019-05-02 10:38:44 +09:00
spi-s3c24xx-fiq.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-s3c24xx-fiq.S treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-s3c24xx.c spi: s3c24xx: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:21:31 +01:00
spi-s3c64xx.c
spi-sc18is602.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-sh-hspi.c spi: sh-hspi: Replace spi_master by spi_controller 2019-02-08 13:04:19 +00:00
spi-sh-msiof.c spi: sh-msiof: Use devm_platform_ioremap_resource() helper 2019-08-07 14:24:47 +01:00
spi-sh-sci.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-sh.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-sifive.c spi: sifive: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:21:41 +01:00
spi-sirf.c spi: sirf: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:22:00 +01:00
spi-slave-mt27xx.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-slave-system-control.c
spi-slave-time.c
spi-sprd-adi.c spi: sprd: adi: Change hwlock to be optional 2019-07-26 12:24:47 +01:00
spi-sprd.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-st-ssc4.c spi: st-ssc4: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:22:10 +01:00
spi-stm32-qspi.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-stm32.c spi: stm32: return the get_irq error 2019-05-02 10:38:41 +09:00
spi-sun4i.c spi: sun4i: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:22:29 +01:00
spi-sun6i.c spi: sun6i: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:22:49 +01:00
spi-synquacer.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-tegra20-sflash.c spi: tegra: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:22:58 +01:00
spi-tegra20-slink.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 201 2019-05-30 11:29:52 -07:00
spi-tegra114.c Linux 5.2-rc4 2019-06-10 18:52:53 +01:00
spi-test.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
spi-ti-qspi.c spi: Remove dev_err() usage after platform_get_irq() 2019-08-02 12:15:43 +01:00
spi-tle62x0.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-topcliff-pch.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 285 2019-06-05 17:36:37 +02:00
spi-txx9.c
spi-uniphier.c spi: uniphier: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:23:08 +01:00
spi-xcomm.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 149 2019-05-30 11:25:18 -07:00
spi-xilinx.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-xlp.c spi: xlp: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:23:28 +01:00
spi-xtensa-xtfpga.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00
spi-zynq-qspi.c spi: zynq-qspi: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:24:07 +01:00
spi-zynqmp-gqspi.c spi: zynqmp: use devm_platform_ioremap_resource() to simplify code 2019-09-04 17:23:48 +01:00
spi.c spi: Guarantee cacheline alignment of driver-private data 2019-09-11 15:53:11 +01:00
spidev.c Linux 5.2-rc4 2019-06-10 18:52:53 +01:00