Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

8.7 DMA and non-coherent masters

A DMA master is non-coherent when its accesses do not snoop the PE caches, and the PE caches are not invalidated by its writes. Typical cases are a device on an ACE-Lite or AXI port without I/O coherency, a debug or trace sink, a power-management controller, or a pre-silicon accelerator model that writes DRAM directly. For such a master, the data it needs, or the data it produced, must reach the Point of Coherency (8.4), because the PoC is the only place both sides are guaranteed to see the same copy. Hardware keeps coherent masters coherent, so the sequences here are unnecessary for them (8.2). Doing them anyway is safe but slow.

Shareability does not help on its own. Mapping a buffer Outer Shareable on the PE says which PE-side observers must agree. It does not make a master that does not snoop see the PE’s dirty lines.

To-device: the PE writes, the device reads

// x0 = buffer start, x1 = buffer end; buffer written by the PE through a WB mapping
    mrs     x3, ctr_el0
    ubfx    x3, x3, #16, #4        // DminLine: log2(words)
    mov     x2, #4
    lsl     x2, x2, x3             // line size in bytes
    sub     x3, x2, #1
    bic     x0, x0, x3             // align start down
1:  dc      cvac, x0               // clean to PoC
    add     x0, x0, x2
    cmp     x0, x1
    b.lo    1b
    dsb     sy                     // cleans complete at PoC
    str     w4, [x5]               // doorbell: Device-nGnRE register
  • DC CVAC (clean, not invalidate) is enough. The PE keeps its copy, and the line just becomes clean.
  • The DSB must come before the doorbell. A DMB would order the doorbell store after the clean as seen by PE-side observers. The device, though, reads DRAM, not the PE’s view of the clean. The architecture only guarantees the clean has completed to the PoC after a DSB. Use DSB SY, or DSB OSH if the master is known to be inside the Outer Shareable domain. Linux’s dcache_clean_poc ends in dsb sy.
  • The doorbell is the same pattern as 7.10, with the clean replacing the plain ordering step.

From-device: the device writes, the PE reads

This direction needs maintenance on both sides of the transfer:

Before starting the DMA (buffer ownership -> device):
    DC CIVAC (or DC CVAC) over the buffer   // no dirty line may be evicted later
    DSB SY
    start DMA (doorbell)

After the DMA completes (interrupt or status read, ownership -> PE):
    DC IVAC over the buffer                  // drop lines fetched during the DMA
    DSB SY
    read the buffer

Each half fixes a different failure:

Missing stepFailure
Clean before the DMAA dirty line for the buffer, left from earlier PE use, is evicted during or after the DMA and overwrites the device’s data in DRAM. Corruption is intermittent and line-sized.
Invalidate after the DMAThe PE reads a stale line. It could be one that was never invalidated, or one the PE speculatively or prefetch-allocated during the transfer. A cacheable Normal mapping lets the PE allocate the line at any time (14.2). An invalidate before the DMA alone does not prevent this.

The post-DMA invalidate is not optional even when the pre-DMA step was a clean+invalidate. Without it, nothing stops the line from being refilled while the device writes. Linux’s arm64 DMA layer does this: arch_sync_dma_for_device() cleans the buffer for every direction, and arch_sync_dma_for_cpu() invalidates it for DMA_FROM_DEVICE and DMA_BIDIRECTIONAL.

Why “invalidate before read” can lose data

DC IVAC discards a line, including any dirty data in it. That is correct only if every byte of the line belongs to the device’s buffer and the PE has no unwritten data there. Three common ways it goes wrong:

  1. Partial lines at the buffer edges. If the buffer does not start and end on a cache-line boundary (and, to be safe, a CTR_EL0.CWG boundary, since a writeback may be that large), the first and last lines also hold neighbouring data. Examples are a struct field, a stack slot, or another buffer’s header. Invalidating them discards the PE’s recent writes to that data. The fixes, in order of preference: allocate DMA buffers aligned and padded to CWG; or use DC CIVAC for the partial edge lines. The second is what Linux’s dcache_inval_poc does. It keeps the neighbour’s data but writes the PE’s stale copy of the buffer’s edge bytes back over the device’s data if the device has already written them. So edge lines must not be touched by both sides at once, whatever the instruction.
  2. Invalidate instead of clean before the DMA. Using DC IVAC in the pre-DMA step, to “save the writeback”, loses any PE writes to the buffer that were still dirty. That is harmless only if the PE really never wrote it.
  3. Virtualized guests. A guest’s DC IVAC is not guaranteed to discard data. With EL2 enabled and stage 2 active, the AArch64.DC pseudocode performs an EL1 DC IVAC as a clean and invalidate. This stops a guest from discarding data the hypervisor or another guest wrote to the same PA. A guest driver that relies on the invalidate to drop data is broken under a hypervisor.

DC IVAC is also the only data-cache maintenance by VA whose correct use depends on who owns each byte of the line. That ownership is a software contract, and the hardware cannot check it.

Security note: Invalidating lines you do not own is a data-integrity primitive: it rolls memory back to the DRAM copy. This is why DC IVAC is not available at EL0 and why the stage 2 upgrade exists. For the same reason, Secure firmware sharing a buffer with the Normal world must not DC IVAC across the buffer boundary into Secure data.

The alternative: Non-cacheable buffers

Mapping the DMA buffer Normal Non-cacheable on the PE avoids CMOs but has costs:

  • Every PE access goes to memory. That is fine for descriptors, but slow for bulk data the PE processes.
  • Any other cacheable alias of the same PA, such as a firmware flat map or the Linux linear map, can still allocate lines through speculation. That is a mismatched-attribute alias (6.6). Remove or unmap the cacheable alias, or clean+invalidate after changing the attributes.
  • Ordering still needs barriers. Non-cacheable stores can sit in a write buffer, so a DSB or an ordered doorbell sequence is still required (7.9).

A common split is Non-cacheable for descriptor rings and cacheable plus CMOs for payload buffers.

The SMMU’s coherency attributes

When a master sits behind an SMMUv3 (11.5), the SMMU decides what memory attributes its transactions have downstream. Two separate questions arise.

1. Are the device’s own DMA transactions coherent? With translation enabled, the output attributes come from the translation tables. Stage 1 uses the CD’s MAIR-indexed attributes; stage 2 uses the STE’s stage 2 attributes. The two are combined as for the PE, or the stage 2 attributes override them when STE.S2FWB is set and SMMU_IDR3.FWB reports support. For bypass or stage-1-bypass cases, the STE fields MTCFG/MemAttr, SHCFG and ALLOCCFG override or supply the attributes. A transaction marked Inner/Outer Write-Back Shareable is snooped only if the path from the SMMU to the interconnect is I/O-coherent, for example an ACE-Lite or CHI RN-I port into the home nodes. Marking a device mapping cacheable on a non-coherent path does not make it coherent. The firmware description has to match the hardware: IORT’s memory access properties (CCA) in ACPI, or the dma-coherent property in device tree.

2. Are the SMMU’s own accesses coherent? These are its stream table, CD and translation table walks, and its command, event and PRI queues. SMMU_IDR0.COHACC says whether the SMMU can make coherent accesses at all. If it can, SMMU_CR1 sets the cacheability and shareability of table and queue accesses (TABLE_IC/OC/SH, QUEUE_IC/OC/SH). CD.IR0/OR0/SH0 and STE.S2IR0/S2OR0/S2SH0 set the attributes of stage 1 and stage 2 table walks. If COHACC == 0, or the attributes are set to Non-cacheable, the software that writes STEs, CDs, page tables and commands must clean them to the PoC before telling the SMMU (before CMD_CFGI_STE or the SMMU_CMDQ_PROD update). Without that, the SMMU reads stale configuration, and the bug shows up as wrong translations or C_BAD_STE events that disappear when you add a debug print.

Bring-up note: If a device “works with caches off” or “works after a DC CIVAC of the whole buffer”, check the coherency description before adding CMOs. Find out whether the port is actually I/O-coherent in this SoC configuration, whether the IORT or DT says so, and whether the SMMU’s attributes for that stream are WB-Shareable. A mismatch in either direction is a bug. Declaring coherency the path lacks causes stale data. Declaring non-coherency when the path is coherent costs performance, and the CMOs on cacheable aliases are still correct.

Validation note: A minimal DMA round-trip test fills a buffer with a known pattern on the PE, cleans it, DMAs it to a second buffer, invalidates the second buffer, and compares. Run it under several perturbations: misalign the buffers by 8 to 56 bytes, have a second PE prefetch-read the destination during the transfer, and remove each CMO in turn. Every removal should produce a detectable failure on a cache-modelling FVP or on silicon. A test that still passes with a CMO removed shows the master is coherent, the cache model is off, or the buffer never gets cached, not that the code is right. PMU L1D_CACHE_WB (0x15) and BUS_ACCESS (0x19) show whether the clean caused writebacks. Interconnect counters, such as CMN HN-F snoop events, show whether the device’s reads snooped. See 8.10.

Checkpoint

Q8.7.1 A driver for a non-coherent device does DC IVAC over the receive buffer, then starts the DMA, then reads the buffer when the completion interrupt arrives. Occasionally it reads old data. Which change is necessary and sufficient, assuming the buffer is line-aligned and the PE never writes it?

  • A. Replace the DC IVAC with DC CIVAC before the DMA
  • B. Add a DC IVAC and DSB SY over the buffer after the completion, before reading
  • C. Add a DMB SY between the interrupt acknowledgement and the first load
  • D. Map the buffer Outer Shareable instead of Inner Shareable
Answer

B. While the DMA ran, the PE could speculatively or prefetch-allocate lines of the buffer from DRAM. Those lines hold pre-DMA data, and the pre-DMA invalidate cannot prevent that. An invalidate after completion removes them. Option A guards against dirty lines being evicted onto the DMA data, but the PE never wrote the buffer, so that cannot cause stale reads. Shareability (option D) does not make a master that does not snoop coherent.

Read more: Arm ARM (DDI0487), Data cache maintenance instructions; Speculative accesses to Normal memory

Q8.7.2 A receive buffer starts 16 bytes into a 64-byte cache line. The first 16 bytes are a lock word that another PE is actively using. The driver does DC IVAC on every line of the buffer after the DMA. What can happen?

  • A. Nothing, because DC IVAC affects only the bytes of the buffer
  • B. The DC IVAC is UNDEFINED on a misaligned VA
  • C. The lock word is cleaned to DRAM first, because DC IVAC always cleans dirty data
  • D. A recent update to the lock word that is still dirty in a cache is discarded, so the lock is rolled back to its DRAM value
Answer

D. Cache maintenance works on whole lines. DC IVAC invalidates the entire line containing the VA, including dirty bytes outside the buffer. The VA needs no alignment (so B is wrong). DC IVAC does not clean (so C is wrong), except when the implementation or a stage 2 regime upgrades it to clean and invalidate, which you cannot rely on at EL3 or EL2. Align and pad DMA buffers to CWG, or use DC CIVAC on edge lines.

Read more: Arm ARM (DDI0487), DC IVAC; CTR_EL0.CWG

Q8.7.3 An SMMUv3 reports SMMU_IDR0.COHACC == 0. Software writes a new STE with normal cacheable stores, issues CMD_CFGI_STE and CMD_SYNC, and the device then faults with a bad-STE event. What is the most likely missing step?

  • A. Cleaning the STE (and any CD or tables it points to) to the PoC, followed by a DSB, before issuing the command
  • B. A TLBI VMALLE1IS before CMD_CFGI_STE
  • C. Setting SMMU_CR1.TABLE_SH to Inner Shareable
  • D. An ISB after writing the STE
Answer

A. With COHACC == 0, the SMMU’s table and queue reads do not snoop PE caches, so the STE must be in memory at the PoC before the SMMU is told to fetch it. Setting SMMU_CR1 attributes (option C) cannot create coherency the SMMU does not support. The command queue entries themselves must also be visible, by the same reasoning. A PE TLBI does not affect SMMU configuration caches. An ISB has nothing to do with memory visibility to another master.

Read more: SMMUv3 (IHI0070), SMMU_IDR0.COHACC; SMMU_CR1; Structure and queue coherency