An Engine That Shares Memory: Clean, Invalidate or Snoop
Give the processor a write-back cache, and the engine of S9.8 reads old data. Software can clean before and invalidate after, or a coherent port can snoop each access. Each has a price.
You Will Be Able To
- Explain why a bus manager that reads memory directly can see stale data, in both directions.
- Count the cost of cleaning and invalidating, and say when it is cheaper than a coherent port.
- Say what a coherent port does on a read and on a write, and what it costs.
- Lesson
- S9.9
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
The CRC engine passed every test. Then the core got a data cache, and the engine returned the CRC of yesterday's buffer.
The engine was not broken: it read exactly what SRAM held. So what went wrong?
Before You Start
You need Accelerators, and the Capstone Step: the CRC engine as a bus manager.
You need Cache Coherence: a stale copy, a dirty copy and snooping. The holder of a dirty copy answers in place of memory.
It helps to have read Write Policy: a write-back cache keeps a store and marks the line dirty.
| Question | |
|---|---|
| 1 | Why can the engine read stale data, and why can the hart? |
| 2 | What must software do before START, and after DONE? |
| 3 | What does a coherent port do on a read, and on a write? |
| 4 | When is cleaning cheaper than a coherent port? |
| 5 | With 64 words all written, what does the coherent port cost in total? |
The Idea: Two Paths to One Memory
A write-back cache keeps new data close to the hart. SRAM gets it later, when the line is written back. Until then SRAM holds old data.
The engine of S9.8 is a bus manager. It reads SRAM directly, not through the cache of the hart. So it can read old words while the new ones sit in the cache. The way back has the same problem. The engine writes its result into SRAM, and the hart may still hold an old copy of that word.
There are two cures. Software can do cache maintenance: write the dirty lines back before START, and drop the old copy before it reads the result. Or the hardware can give the engine a coherent port. It asks the cache first, as the caches of S9.3 ask each other.
Both work. They cost in different places, and the better one depends on how much of the buffer is dirty.
New Words
| Word | Meaning |
|---|---|
| Cache maintenance | Instructions or register writes by which software tells a cache to write back or drop lines. The lab calls it upkeep. |
| Clean a cache line | Write the line back to memory if it is dirty, and keep it as a clean copy. |
| Invalidate a cache line | Drop the line from the cache, so that the next read fetches it from memory again. |
| Coherent port | A port through which a manager without a cache reaches memory. Its reads ask the caches first, and its writes remove their copies. |
| Snoop hit | A read through the coherent port that finds a dirty copy in a cache. The cache supplies the data. |
One Picture: Two Paths to SRAM
- Processor
- Memory
- Peripheral
- Bus
- New words in the cache
The hart writes the buffer. A write-back cache keeps the new words and leaves the old ones in SRAM.
- The engine reads SRAM
Started now, the engine reads the old words. Its CRC is the CRC of the old buffer.
- Fix 1: clean first
Software writes every dirty line back before START. Then SRAM holds the new words.
- Fix 2: a coherent port
The engine reads through a port that asks the cache first. A dirty copy answers in place of SRAM.
Read the diagram as text
- Hart: writes the buffer, reads the result
- Data cache: write-back: new words stay here
- SRAM: still holds the old buffer
- CRC engine: a bus manager, as in S9.8
- Coherent port: asks the cache before SRAM
- Hart → Data cache: stores
- Data cache → SRAM: write back
- CRC engine → SRAM: reads SRAM
- CRC engine → Coherent port: reads
- Coherent port → Data cache: snoop
- Coherent port → SRAM: if no dirty copy
The model of the lab. ap_soc v1.0 has no data cache; this is what changes when a core of the chip gets one.
The Smallest Example, Worked by Hand
Take a buffer of 8 words: 2 lines of 4. The hart has written all 8, so both lines are dirty. The lab uses these costs. A clean or an invalidate takes 1 cycle, and writing a dirty line back 4 more. The engine reads a word in 1 cycle, or 2 through the port. Its result takes 1.
| Way | Maintenance | Engine | Total | Right? |
|---|---|---|---|---|
| No maintenance | 0 | 8 + 1 = 9 | 9 | No: old words, and an old result |
| Clean, then invalidate | 2 × (1 + 4) + 1 = 11 | 9 | 20 | Yes |
| Coherent port | 0 | 8 × 2 + 1 = 17 | 17 | Yes |
The fastest way is wrong. Of the right ways, the port wins here, because every line was dirty and had to move anyway.
Now suppose the hart wrote only 1 of the 2 lines. Cleaning costs 2 lookups, 4 for the one write-back, and 1 invalidate: 7, so 16 in total. The port still costs 17 on every word.
The Rules
Rule 1: A manager that reads memory directly can see stale data.
New data may sit dirty in a write-back cache. Memory gets it only on a write-back.
Rule 2: What the manager writes can be stale for the hart.
The hart may hold an old copy of that word in its cache.
Rule 3: Clean before the manager reads.
Every dirty line of the buffer goes back to memory before START.
Rule 4: Invalidate before the hart reads.
The old copy of the result line is dropped, so the read fetches the new word.
Rule 5: A coherent port does both in hardware.
Its reads ask the cache first. Its writes remove the cached copies.
Rule 6: Maintenance costs per dirty line; a port costs per access.
Few dirty lines favour cleaning. Many favour the port.
Sources for These Rules
A DMA and the processor can see different values: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 6.9.
An I/O coherent manager has no cache. Its reads look in the caches first. Its writes clean and invalidate their copies: Arm AMBA AXI Protocol Specification, IHI 0022 issue L, A8.4.
Stale copies, snooping and the dirty holder that answers: Patterson and Hennessy, Computer Organization and Design, ARM edition, sections 5.10 and 5.12.
The cache, its line size and every cost in cycles are choices of this lab, not measurements. ap_soc v1.0 has no data cache.
Now Run It
The lab at the end of this page runs the three ways on 16 words, all written by the hart. Before that, the hart had read the result word, so its cache holds an old copy of it.
words 16 | written by the hart 16 | lines 4 | right crc 5c2651a1
no upkeep | engine crc a2fdb57f WRONG | stale words 16 | hart reads 00000000 WRONG | snoop hits 0 | upkeep 0 | engine 17 | total 17
clean, inval. | engine crc 5c2651a1 right | stale words 0 | hart reads 5c2651a1 right | snoop hits 0 | upkeep 21 | engine 17 | total 38
coherent port | engine crc 5c2651a1 right | stale words 0 | hart reads 5c2651a1 right | snoop hits 16 | upkeep 0 | engine 33 | total 33Rules 1 and 2 in the first row. All 16 words are stale, and the engine returns a2fdb57f. That is the CRC of the old buffer: the same number that S9.8 printed. The hart then reads 00000000, its old copy.
Rules 3 and 4 in the second row. Four lines cleaned at 5 cycles each, and one invalidate: 21 cycles. Both results are right.
Rule 5 in the third row. 16 snoop hits, no maintenance, and 33 engine cycles: two for each word.
Now rule 6. The same lab with only 4 new words, one dirty line:
words 16 | written by the hart 4 | lines 4 | right crc 950b6b29
no upkeep | engine crc a2fdb57f WRONG | stale words 4 | hart reads 00000000 WRONG | snoop hits 0 | upkeep 0 | engine 17 | total 17
clean, inval. | engine crc 950b6b29 right | stale words 0 | hart reads 950b6b29 right | snoop hits 0 | upkeep 9 | engine 17 | total 26
coherent port | engine crc 950b6b29 right | stale words 0 | hart reads 950b6b29 right | snoop hits 4 | upkeep 0 | engine 33 | total 33Cleaning now costs 9 cycles, 26 in total. The port still costs 33: it pays on every word, dirty or not.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Maintenance by software | A coherent port | No extra hardware. Every driver must do it right, every time. |
| A coherent port | Maintenance by software | No step to forget. Each access costs a lookup, and the port and its snoops cost area. |
| The engine keeps no cache of shared data | Give the engine its own cache | Nothing to snoop in the engine. Each word it needs crosses the port again. |
The Trap: The Driver That Cleaned and Forgot
A driver cleaned the buffer before START, and the CRC came out right. The value that the firmware read was still wrong, now and then.
The driver never invalidated the result line. The hart had read that word earlier, so the cache held an old, clean copy. Clean is not enough: the way back needs an invalidate.
The second trap is to invalidate a dirty line in place of cleaning it. The new words are then dropped, and the engine reads old ones anyway.
The third trap is a test without a cache. The engine passes, and the fault appears only when the cache is turned on.
At Scale
This section looks ahead. Large chips have many managers that share memory with many caches.
Coherent interconnects
Large chips give many engines a coherent port into one interconnect. See how a read crosses a larger chip.
Addresses, too
An engine on a big chip may use the addresses of a program, not of memory. An IOMMU (I/O memory management unit) translates and checks them. See I/O Memory Management.
A last-level cache
A shared cache in front of memory can catch the engine's reads, so that fewer go out to the slow memory.
Buffers that are never cached
Some drivers map a buffer as never cached. Nothing needs maintenance, and every access by the hart is slow.
Try It
Run the lab. Which way gives the wrong CRC, and which CRC does it give? Compare it with the CRC of the lab of S9.8.
Set WRITTEN to 4. Which way is cheaper now, and why?
In the task run, delete the invalidate of the result line in mode 1. What does the hart read?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| no upkeep, clean, inval., coherent port | The three ways: nothing done, the driver cleans and invalidates, and a port that snoops. |
| engine crc | The CRC that the engine computed, and whether it is right. |
| stale words | Words that the engine read from SRAM while the cache held a newer copy. |
| hart reads | The result word as the hart reads it after DONE. |
| snoop hits | Reads through the port that found a dirty copy in the cache. |
| upkeep, engine, total | Cycles of cache maintenance by software, cycles of the engine, and the two together. |
words 16 | written by the hart 16 | lines 4 | right crc 5c2651a1 no upkeep | engine crc a2fdb57f WRONG | stale words 16 | hart reads 00000000 WRONG | snoop hits 0 | upkeep 0 | engine 17 | total 17 clean, inval. | engine crc 5c2651a1 right | stale words 0 | hart reads 5c2651a1 right | snoop hits 0 | upkeep 21 | engine 17 | total 38 coherent port | engine crc 5c2651a1 right | stale words 0 | hart reads 5c2651a1 right | snoop hits 16 | upkeep 0 | engine 33 | total 33Check Yourself
Common Mistakes
Wrong Clean the buffer and forget the result.
The hart may hold an old copy of the result line.
Fix Clean before START, invalidate before the hart reads the result.
Wrong Invalidate a dirty buffer before START.
Its new words are dropped, not written back.
Fix Clean first; invalidate only lines that the engine will write.
Wrong Test the engine with the cache turned off.
Stale data appears only with a write-back cache.
Fix Test with the cache on, and with buffers that are dirty and clean.
Remember
- A manager that reads memory directly misses new data in a write-back cache.
- What it writes can be stale for the hart, which may hold an old copy.
- Software cleans before and invalidates after; the cost grows with the dirty lines.
- A coherent port snoops every access; it costs on every word and needs no driver step.