Skip to content

Commit 8ded2d0

Browse files
committed
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. Make use of 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) Signed-off-by: Lukas Wunner <[email protected]> Cc: Martin Sperl <[email protected]> Cc: Noralf Trønnes <[email protected]> Cc: Florian Kauer <[email protected]>
1 parent 05c1efd commit 8ded2d0

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
@@ -53,11 +53,14 @@
5353
* @ddev: DMA device
5454
* @base: base address of register map
5555
* @dma_parms: DMA parameters (to convey 1 GByte max segment size to clients)
56+
* @zero_page: bus address of zero page (to detect transactions copying from
57+
* zero page and avoid accessing memory if so)
5658
*/
5759
struct bcm2835_dmadev {
5860
struct dma_device ddev;
5961
void __iomem *base;
6062
struct device_dma_parameters dma_parms;
63+
dma_addr_t zero_page;
6164
};
6265

6366
struct bcm2835_dma_cb {
@@ -705,6 +708,7 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
705708
size_t period_len, enum dma_transfer_direction direction,
706709
unsigned long flags)
707710
{
711+
struct bcm2835_dmadev *od = to_bcm2835_dma_dev(chan->device);
708712
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
709713
struct bcm2835_desc *d;
710714
dma_addr_t src, dst;
@@ -755,6 +759,10 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
755759
dst = c->cfg.dst_addr;
756760
src = buf_addr;
757761
info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
762+
763+
/* non-lite channels can write zeroes w/o accessing memory */
764+
if (buf_addr == od->zero_page && !c->is_lite_channel)
765+
info |= BCM2835_DMA_S_IGNORE;
758766
}
759767

760768
/* calculate number of frames */
@@ -857,6 +865,9 @@ static void bcm2835_dma_free(struct bcm2835_dmadev *od)
857865
list_del(&c->vc.chan.device_node);
858866
tasklet_kill(&c->vc.task);
859867
}
868+
869+
dma_unmap_page_attrs(od->ddev.dev, od->zero_page, PAGE_SIZE,
870+
DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
860871
}
861872

862873
static const struct of_device_id bcm2835_dma_of_match[] = {
@@ -943,6 +954,14 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
943954

944955
platform_set_drvdata(pdev, od);
945956

957+
od->zero_page = dma_map_page_attrs(od->ddev.dev, ZERO_PAGE(0), 0,
958+
PAGE_SIZE, DMA_TO_DEVICE,
959+
DMA_ATTR_SKIP_CPU_SYNC);
960+
if (dma_mapping_error(od->ddev.dev, od->zero_page)) {
961+
dev_err(&pdev->dev, "Failed to map zero page\n");
962+
return -ENOMEM;
963+
}
964+
946965
/* Request DMA channel mask from device tree */
947966
if (of_property_read_u32(pdev->dev.of_node,
948967
"brcm,dma-channel-mask",

0 commit comments

Comments
 (0)