﻿# DMA cache maintenance: the cache line can contain more than your sensor buffer

Keep DMA ownership and cache-line boundaries together before embedded inference reads a sample. Invalidation can affect unrelated dirty data sharing the same line.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/edge-dma-cache-line-neighbor-ownership/
Language: en

## The sample is correct but neighboring state changes

A DMA sensor buffer feeds an embedded classifier. After cache invalidation, the new sample becomes visible but an adjacent application field loses its recent CPU update. The maintainer checked the buffer’s starting alignment and assumed the operation touched only its requested bytes. Cache maintenance operates at cache-line granularity. Diagnose the full affected span and ownership of neighboring bytes before blaming preprocessing or a model kernel. Correct sensor values alone do not prove the handoff preserved other live application state.

## Identify the actual coherence arrangement

The [Zephyr caching guide](https://docs.zephyrproject.org/latest/hardware/cache/guide.html) explains that external bus masters such as DMA can require explicit coherence management. Establish whether the selected memory and driver path are coherent, uncached or require maintenance. Do not infer that CPU-core coherence automatically covers the peripheral. Equally, do not add maintenance blindly where another supported layer already owns it. Preserve which component performs the operation and the event that establishes the transfer has reached the required completion boundary.

## Direction and ownership determine the operation

The guide distinguishes flushing modified CPU data before another bus master reads it from invalidating stale CPU cache before reading peripheral-written data. This is not a universal instruction to clean every receive buffer afterward. A late writeback of an old dirty line can conflict with newer external data. Follow the platform’s complete handoff sequence, including any required preparation before DMA and ordering guarantees. Keep the buffer unavailable to unsynchronized CPU modification during the device-owned phase.

## Isolate both boundaries of the maintained range

The Zephyr guide warns that unrelated data packed into a line can be destroyed during invalidation. Reserve a suitable complete span, accounting for both beginning and end, or use a supported uncached placement. The [CMSIS D-cache reference](https://arm-software.github.io/CMSIS_6/latest/Core/group__Dcache__functions__m7.html) specifies a 32-byte address boundary for its documented functions. That requirement belongs to that API and target family; it is not a universal constant for every processor. Verify the actual line and range contract instead of copying the number into all firmware.

## Propose sample and neighbor integrity checks

Use harmless recognizable DMA payloads and adjacent CPU-owned markers in a controlled fixture. Compare the supported isolated layout with a diagnostic layout that exposes shared-line risk without accessing unowned memory. Check actual transfer completion, sample contents and marker preservation through repeated handoffs. Acceptance requires correct data on both sides and documented maintenance ownership. No DMA transfer or cache-control operation ran here; this fixture defines a correctness investigation, not a measured bandwidth result or permission to alter production memory attributes.

## Keep software-view synchronization separate

[Embree host/device views](/journal/fg2-embree-sycl-explicit-host-device-buffer-coherence/) address another synchronization model. [Device-buffer ownership](/journal/fg2-onnxruntime-io-binding-device-buffer-lifetime/) explains why a pointer needs a valid lifetime. DMA cache maintenance adds cache-line granularity and memory-attribute requirements to that reasoning. Record transfer direction, actual memory path and neighbor ownership so a classifier’s input error can be investigated without assuming every shared address is automatically coherent or every range operation preserves unrelated bytes.
