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.

Five questions this page answers
Question
1Why can the engine read stale data, and why can the hart?
2What must software do before START, and after DONE?
3What does a coherent port do on a read, and on a write?
4When is cleaning cheaper than a coherent port?
5With 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

New words on this page
WordMeaning
Cache maintenanceInstructions or register writes by which software tells a cache to write back or drop lines. The lab calls it upkeep.
Clean a cache lineWrite the line back to memory if it is dirty, and keep it as a clean copy.
Invalidate a cache lineDrop the line from the cache, so that the next read fetches it from memory again.
Coherent portA port through which a manager without a cache reaches memory. Its reads ask the caches first, and its writes remove their copies.
Snoop hitA read through the coherent port that finds a dirty copy in a cache. The cache supplies the data.

One Picture: Two Paths to SRAM

Two Paths to SRAM, and the Copy in the MiddleStep through the picture. The hart reaches SRAM through its cache. The engine reaches SRAM directly, and that is the problem.
  • Processor
  • Memory
  • Peripheral
  • Bus
Two Paths to SRAM, and the Copy in the MiddleStep through the picture. The hart reaches SRAM through its cache. The engine reaches SRAM directly, and that is the problem. component Hart writes the buffer,reads the result component Data cache write-back: new wordsstay here component SRAM still holds the oldbuffer component CRC engine a bus manager, as inS9.8 component Coherent port asks the cachebefore SRAM storeswrite backreads SRAMreadssnoopif no dirty copy
  1. component Hartwrites the buffer, reads the result
    • stores → Data cache
  2. component Data cachewrite-back: new words stay here
    • write back → SRAM
  3. component SRAMstill holds the old buffer
  4. component CRC enginea bus manager, as in S9.8
    • reads SRAM → SRAM
    • reads → Coherent port
  5. component Coherent portasks the cache before SRAM
    • snoop → Data cache
    • if no dirty copy → SRAM
  1. 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.

  2. The engine reads SRAM

    Started now, the engine reads the old words. Its CRC is the CRC of the old buffer.

  3. Fix 1: clean first

    Software writes every dirty line back before START. Then SRAM holds the new words.

  4. 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.

Eight words, three ways
WayMaintenanceEngineTotalRight?
No maintenance08 + 1 = 99No: old words, and an old result
Clean, then invalidate2 × (1 + 4) + 1 = 11920Yes
Coherent port08 × 2 + 1 = 1717Yes

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

  1. 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.

  2. 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.

  3. Rule 3: Clean before the manager reads.

    Every dirty line of the buffer goes back to memory before START.

  4. Rule 4: Invalidate before the hart reads.

    The old copy of the result line is dropped, so the read fetches the new word.

  5. Rule 5: A coherent port does both in hardware.

    Its reads ask the cache first. Its writes remove the cached copies.

  6. 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.

Printed by the lab on Icarus. A model: the costs are choices of the lab.
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 33

Rules 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:

The same lab with WRITTEN = 4.
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 33

Cleaning 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 choiceThe alternativeWhat the choice buys, and costs
Maintenance by softwareA coherent portNo extra hardware. Every driver must do it right, every time.
A coherent portMaintenance by softwareNo step to forget. Each access costs a lookup, and the port and its snoops cost area.
The engine keeps no cache of shared dataGive the engine its own cacheNothing 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 page1 engine, 1 cache
A phone chipdozens of engines, several levels of cache

This section looks ahead. Large chips have many managers that share memory with many caches.

  1. Coherent interconnects

    Large chips give many engines a coherent port into one interconnect. See how a read crosses a larger chip.

  2. 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.

  3. 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.

  4. 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
NameMeaning
no upkeep, clean, inval., coherent portThe three ways: nothing done, the driver cleans and invalidates, and a port that snoops.
engine crcThe CRC that the engine computed, and whether it is right.
stale wordsWords that the engine read from SRAM while the cache held a newer copy.
hart readsThe result word as the hart reads it after DONE.
snoop hitsReads through the port that found a dirty copy in the cache.
upkeep, engine, totalCycles of cache maintenance by software, cycles of the engine, and the two together.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 33

Check Yourself

Predict the outputIn this run the buffer has 64 words, all written by the hart. What is the last line? Its form is: coherent port | engine crc 59f21ae9 right | stale words 0 | hart reads 59f21ae9 right | snoop hits .. | upkeep . | engine ... | total ...
// The engine of S9.8 as a bus manager, beside a hart with a write-back data cache.
// A model, as test code: ap_soc v1.0 has no data cache. The cache holds one line for each line of SRAM, so
// nothing is ever evicted. The costs below are choices of this lab, not measurements.
module tb;
  localparam int WORDS = 64;          // words in the buffer that the engine reads
  localparam int WRITTEN = 64;        // new words that the hart writes first, from word 0
  localparam int LINE = 4;            // words in a cache line
  localparam int LOOKUP = 1;          // cycles to look a line up, for a clean or an invalidate
  localparam int SNOOP_READ = 2;      // cycles of a read that the coherent port snoops (else 1)
  localparam int RESULT = 64;         // the word where the engine writes its CRC

  logic [31:0] mem [0:127], cache [0:127];  // SRAM, and the copy that the cache holds of each word
  bit valid [0:31], dirty [0:31];            // for each line
  logic [31:0] crc, seen, right;
  int upkeep, engine, stale, hits;

  function automatic logic [31:0] crc_word(input logic [31:0] c, input logic [31:0] w);
    c = c ^ w;
    for (int b = 0; b < 32; b++) c = {c[30:0], 1'b0} ^ (c[31] ? 32'h04c11db7 : 32'h0);
    return c;
  endfunction
  function automatic logic [31:0] old_word(input int i);   // what SRAM held before: the buffer of S9.8
    return (32'(i + 1) * 32'h9e3779b9) ^ (32'(i) << 7);
  endfunction
  // ---- the hart's side of the cache: write-allocate, write-back
  task automatic fill(input int l);
    if (!valid[l]) begin for (int k = 0; k < LINE; k++) cache[l * LINE + k] = mem[l * LINE + k];  valid[l] = 1; end
  endtask
  task automatic hart_read(input int i, output logic [31:0] v);
    fill(i / LINE);  v = cache[i];
  endtask
  task automatic hart_write(input int i, input logic [31:0] v);
    fill(i / LINE);  cache[i] = v;  dirty[i / LINE] = 1;
  endtask

  task automatic run(input int mode);   // 0 no upkeep, 1 clean before and invalidate after, 2 coherent port
    logic [31:0] w;
    for (int i = 0; i < 128; i++) begin mem[i] = (i < 64) ? old_word(i) : 32'h0;  cache[i] = 32'h0; end
    for (int l = 0; l < 32; l++) begin valid[l] = 0;  dirty[l] = 0; end
    hart_read(RESULT, w);                                   // an old, clean copy of the result word
    for (int i = 0; i < WRITTEN; i++) hart_write(i, old_word(i) ^ 32'h5a5a5a5a);
    upkeep = 0;  engine = 0;  stale = 0;  hits = 0;  crc = 32'hffffffff;
    if (mode == 1)                                          // the driver cleans every line of the buffer
      for (int l = 0; l < (WORDS + LINE - 1) / LINE; l++) begin
        upkeep += LOOKUP;
        if (dirty[l]) begin
          for (int k = 0; k < LINE; k++) mem[l * LINE + k] = cache[l * LINE + k];
          dirty[l] = 0;  upkeep += LINE;
        end
      end
    for (int i = 0; i < WORDS; i++) begin                   // START: the engine reads the buffer
      if (mode == 2) begin                                  // the port asks the cache first
        engine += SNOOP_READ;
        if (valid[i / LINE] && dirty[i / LINE]) begin w = cache[i];  hits++; end else w = mem[i];
      end else begin                                        // straight to SRAM
        engine += 1;  w = mem[i];
        if (valid[i / LINE] && dirty[i / LINE] && cache[i] != mem[i]) stale++;
      end
      crc = crc_word(crc, w);
    end
    mem[RESULT] = crc;  engine += 1;                        // the engine writes its result: DONE
    if (mode == 2) valid[RESULT / LINE] = 0;                // the port removes the hart's copy
    if (mode == 1) begin upkeep += LOOKUP;  valid[RESULT / LINE] = 0; end   // the driver invalidates it
    hart_read(RESULT, seen);
  endtask

  initial begin
    right = 32'hffffffff;
    for (int i = 0; i < WORDS; i++) right = crc_word(right, (i < WRITTEN) ? old_word(i) ^ 32'h5a5a5a5a : old_word(i));
    $display("words %0d | written by the hart %0d | lines %0d | right crc %h", WORDS, WRITTEN, (WORDS + LINE - 1) / LINE, right);
    for (int m = 0; m < 3; m++) begin
      run(m);
      $display("%s | engine crc %h %s | stale words %0d | hart reads %h %s | snoop hits %0d | upkeep %0d | engine %0d | total %0d",
        m == 0 ? "no upkeep    " : m == 1 ? "clean, inval." : "coherent port", crc, crc == right ? "right" : "WRONG", stale,
        seen, seen == right ? "right" : "WRONG", hits, upkeep, engine, upkeep + engine);
    end
  end
endmodule
ConceptWhy can the engine read stale data?
ConceptWhat must software do after DONE, before the hart reads the result?
ConceptWhat does a coherent port do on a read?
ConceptWhen is cleaning cheaper than a coherent port?

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.

Practice

Report a problem with this page