Skip to content

Commit bf75703

Browse files
l1kbroonie
authored andcommitted
dmaengine: bcm2835: Avoid accessing memory when copying zeroes
The BCM2835 DMA controller is capable of synthesizing zeroes instead of copying them from a source address. The feature is enabled by setting the SRC_IGNORE bit in the Transfer Information field of a Control Block: "Do not perform source reads. In addition, destination writes will zero all the write strobes. This is used for fast cache fill operations." https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf The feature is only available on 8 of the 16 channels. The others are so-called "lite" channels with a limited feature set and performance. Enable the feature if a cyclic transaction copies from the zero page. This reduces traffic on the memory bus. A forthcoming use case is the BCM2835 SPI driver, which will cyclically copy from the zero page to the TX FIFO. The idea to use SRC_IGNORE was taken from an ancient GitHub conversation between Martin and Noralf: msperl/spi-bcm2835#13 (comment) Tested-by: Nuno Sá <[email protected]> Tested-by: Noralf Trønnes <[email protected]> Signed-off-by: Lukas Wunner <[email protected]> Acked-by: Vinod Koul <[email protected]> Acked-by: Stefan Wahren <[email protected]> Acked-by: Martin Sperl <[email protected]> Cc: Florian Kauer <[email protected]> Link: https://lore.kernel.org/r/b2286c904408745192e4beb3de3c88f73e4a7210.1568187525.git.lukas@wunner.de Signed-off-by: Mark Brown <[email protected]>
1 parent 571e31f commit bf75703

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

drivers/dma/bcm2835-dma.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@
4242
* @ddev: DMA device
4343
* @base: base address of register map
4444
* @dma_parms: DMA parameters (to convey 1 GByte max segment size to clients)
45+
* @zero_page: bus address of zero page (to detect transactions copying from
46+
* zero page and avoid accessing memory if so)
4547
*/
4648
struct bcm2835_dmadev {
4749
struct dma_device ddev;
4850
void __iomem *base;
4951
struct device_dma_parameters dma_parms;
52+
dma_addr_t zero_page;
5053
};
5154

5255
struct bcm2835_dma_cb {
@@ -693,6 +696,7 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
693696
size_t period_len, enum dma_transfer_direction direction,
694697
unsigned long flags)
695698
{
699+
struct bcm2835_dmadev *od = to_bcm2835_dma_dev(chan->device);
696700
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
697701
struct bcm2835_desc *d;
698702
dma_addr_t src, dst;
@@ -743,6 +747,10 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
743747
dst = c->cfg.dst_addr;
744748
src = buf_addr;
745749
info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
750+
751+
/* non-lite channels can write zeroes w/o accessing memory */
752+
if (buf_addr == od->zero_page && !c->is_lite_channel)
753+
info |= BCM2835_DMA_S_IGNORE;
746754
}
747755

748756
/* calculate number of frames */
@@ -845,6 +853,9 @@ static void bcm2835_dma_free(struct bcm2835_dmadev *od)
845853
list_del(&c->vc.chan.device_node);
846854
tasklet_kill(&c->vc.task);
847855
}
856+
857+
dma_unmap_page_attrs(od->ddev.dev, od->zero_page, PAGE_SIZE,
858+
DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
848859
}
849860

850861
static const struct of_device_id bcm2835_dma_of_match[] = {
@@ -927,6 +938,14 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
927938

928939
platform_set_drvdata(pdev, od);
929940

941+
od->zero_page = dma_map_page_attrs(od->ddev.dev, ZERO_PAGE(0), 0,
942+
PAGE_SIZE, DMA_TO_DEVICE,
943+
DMA_ATTR_SKIP_CPU_SYNC);
944+
if (dma_mapping_error(od->ddev.dev, od->zero_page)) {
945+
dev_err(&pdev->dev, "Failed to map zero page\n");
946+
return -ENOMEM;
947+
}
948+
930949
/* Request DMA channel mask from device tree */
931950
if (of_property_read_u32(pdev->dev.of_node,
932951
"brcm,dma-channel-mask",

0 commit comments

Comments
 (0)