Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Course
  • Reading Paths
  • Concept Map
  • How to Read the Diagrams
  • Part 0: What Is a Protocol?10
  • Part 1: Timing and Clocking11
  • Part 2: Encoding and Framing12
  • Part 3: Handshakes13
  • Part 4: Flow Control17
  • Part 5: Transactions19
  • Part 6: Concurrency and Performance17
  • Part 7: Ordering13
  • Part 8: Sharing and Arbitration12
  • Part 9: Deadlock and Liveness13
  • Part 10: Reliability and Recovery20
  • Part 11: Bring-Up and Link Management10
  • Part 12: Power8
  • Part 13: Shared State and Coherence9
    • Why Coherence
    • Invalidate or Update
    • MSI, MESI and MOESI
    • Snooping or Directory
    • Ownership and Forwarding
    • Coherence and Consistency
    • I/O Coherence and Cache Maintenance
    • Protocols Compared: Shared State and Coherence
    • Drills: Shared State and Coherence
  • Part 14: Security and Isolation7
  • Part 15: Specifying and Verifying11
  • Part 16: Protocols Dissected17
  • Part 17: Capstones5
  • Part 18: Interview Preparation6
Loading lesson

‹Part 13: Shared State and Coherence
  1. Topics
  2. First Principles of Hardware Protocols
  3. Part 13: Shared State and Coherence
  4. I/O Coherence and Cache Maintenance

Part 13 · Shared State and Coherence · Senior (S)

I/O Coherence and Cache Maintenance

DMA engines and accelerators read and write memory that processors cache. Either the device's accesses are coherent with the caches (I/O coherence), or software must clean and invalidate cache lines around every transfer. Both approaches, and the bugs each one invites.

On this page

    Note

    Level: Senior (S) · Answers: What if there are several copies of one value?

    Before this: MSI, MESI and MOESI

    A driver fills a buffer with a packet and tells the network card to send it. The card reads the buffer from memory with DMA. But the packet's last few bytes are still in the processor's cache, dirty, not yet written to memory. The card reads stale bytes from memory and sends a corrupted packet. The processor's caches are coherent with each other; the card was never part of that agreement.

    How do devices that access memory directly stay consistent with processor caches?

    The obvious answer, and why it breaks

    The obvious answer is that DMA goes to memory, so it sees memory's contents. But memory is not the latest value when a processor cache holds the line dirty, and a processor that cached a line before a device wrote it keeps reading its stale copy afterwards. A device outside the coherence protocol sees the same problem as an uncoordinated cache (lesson 13.1), in both directions.

    The idea: two ways to include the device

    Approach

    How

    Cost

    I/O coherent device

    The device's reads and writes are snooped against processor caches: a read gets dirty data from a cache, a write invalidates cached copies. The device itself caches nothing.

    Snoop traffic and latency on every device access; interconnect support

    Fully coherent device

    The device has its own cache that takes part in the protocol like a core's

    Most hardware; for accelerators that reuse data

    Non-coherent device with cache maintenance

    Software cleans (writes back) lines before the device reads them and invalidates lines before the CPU reads what the device wrote

    Software complexity; maintenance time per transfer; easy to get wrong

    Two flowcharts. For a device read: the CPU writes the buffer, cleans its lines to memory, issues a barrier and starts the DMA, and the device reads memory. For a device write: invalidate the buffer's lines before and after the DMA; the device writes memory and signals completion; after a barrier the CPU reads fresh data from memory.
    With a non-coherent device, software cleans before the device reads and invalidates around the device's writes, with barriers so the maintenance completes first.

    Cache maintenance pitfalls

    • Buffers that share a line with other data: invalidating the buffer discards the neighbouring data's dirty bytes. DMA buffers must be aligned to and sized in whole cache lines.

    • Speculative prefetch: the CPU may bring buffer lines back into the cache while the device is writing them; invalidate after the DMA too, not only before.

    • Missing barriers: the maintenance must complete before the device is started, and before the CPU reads the result.

    • Wrong point of coherence: cleaning only to an inner cache level does not make the data visible to a device that reads memory.

    How real protocols chose

    Protocol

    I/O coherence

    AMBA ACE-Lite

    No snoop channels of its own: its reads and writes are coherent with caches without the device caching anything (fact A23)

    AMBA ACE and CHI

    Fully coherent agents with caches (fact A23)

    PCI Express

    DMA is normally snooped by the host's coherence logic; a request may set the No Snoop attribute when software has made sure no snoop is needed (fact P21)

    Arm processors

    Cache maintenance instructions to the Point of Coherency (DC CVAC clean, DC IVAC invalidate, DC CIVAC both) for non-coherent devices (fact A26)

    What breaks

    A camera driver (illustrative) allocated a frame buffer that was not line-aligned; its last line shared a cache line with a driver status word. After each frame, the driver invalidated the buffer's lines, discarding a recent status update that had not been written back. The status word reverted to an old value now and then. Aligning and padding DMA buffers to whole lines fixed it.

    Why this matters

    Senior lens. Decide coherence per device early: I/O coherent through the interconnect, fully coherent, or non-coherent with maintenance. If maintenance is required, put it in one well-tested layer of the driver framework, insist on line-aligned buffers, and invalidate after DMA as well as before. Coherence bugs at the device boundary look like random data corruption, and they are among the most expensive to debug.

    Interview

    Asked: "How do you keep DMA buffers consistent with CPU caches?"

    Weak answer: Disable the cache.

    Strong answer: Either the hardware does it, or software does. With an I/O coherent interconnect, such as a device on ACE-Lite or normal snooped PCIe DMA, the device's reads get dirty data from caches and its writes invalidate cached copies, so software does nothing special. With a non-coherent device, software cleans the buffer's lines to the point of coherency before the device reads, and invalidates them before and after the device writes, with barriers so the maintenance completes in order. Buffers must be line-aligned and sized, or maintenance damages neighbouring data.

    Follow-ups: (1) Why invalidate after the DMA as well as before? (2) What does ACE-Lite give a device?

    Check yourself A non-coherent device is about to read a buffer the CPU just wrote. What must software do?

    Clean the buffer's lines to the point of coherency, then a barrier, then start the device.

    Check yourself Why must DMA buffers be aligned to cache lines on a non-coherent system?

    Invalidating a line that also holds other data discards that data's unwritten changes.

    Key takeaways

    • Devices outside the coherence protocol see stale data in both directions.

    • I/O coherent interconnects snoop device accesses; otherwise software cleans and invalidates around each transfer.

    • Maintenance needs line-aligned buffers, barriers, and invalidation after DMA as well as before.

    Keep going

    Next: Protocols Compared: Shared State and Coherence. Part 13 side by side: how real protocols keep copies coherent.

    Related topics

    • Part 13 overview: Shared State and Coherence

    • Architecture course: ACE and CHI

    Previous

    186 / 234

    Next: Protocols Compared: Shared State and Coherence

    Up next

    Up nextProtocols Compared: Shared State and Coherence

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Why Coherence
    • Invalidate or Update
    • MSI, MESI and MOESI