Bursts, Locks and the Waiting Request
An arbiter that chooses again at every cycle is fair, and sometimes wrong. Some transfers belong together, and one pair must never be split.
You Will Be Able To
- Define a beat and a burst, and weigh keeping a burst together against splitting it.
- Show how two correct managers lose an update, and how a locked sequence prevents it.
- Say when the bus is locked and unlocked, and what an input stage must keep.
- Lesson
- S4.13
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
Two blocks each add to one counter in memory. Every transfer they make is legal, and the arbiter is perfectly fair. At the end the counter is too small. One addition has vanished.
Before You Start
You need the arbiter, the grant and round robin from Many Managers. You need the address phase, the data phase and HTRANS from S0.5.
| Question | |
|---|---|
| 1 | What are a beat and a burst? |
| 2 | What may an arbiter do in the middle of a burst, and what does each choice cost? |
| 3 | How can two managers that each work correctly lose an update to a shared word? |
| 4 | What does HMASTLOCK promise, and when is the bus locked and unlocked? |
| 5 | Where does a request wait while another manager holds the port, and what must be kept? |
The Idea: Some Transfers Belong Together
The arbiter of the last lesson treated every transfer alone. After each one it chose again. Three things complicate that picture.
First, a manager often wants a run of words, one after the other. Such a run is a burst, and each transfer in it is a beat. An arbiter can let the burst finish, or let another manager in between two beats. Either is allowed. Each has a price.
Second, some pairs of transfers must not be separated at all. To add to a word in memory, a manager reads it and writes it back. That is a read-modify-write. If another manager writes the word between the two halves, one of the two changes is lost. A manager can ask the bus to keep such a pair together. The pair is then a locked sequence.
Third, while one manager holds the port, the other still has a request. On AHB a request is on the wires for one address phase only. So something must keep it until its turn comes. That something is the input stage.
New Words
| Word | Meaning |
|---|---|
| Burst | A run of transfers to neighbouring addresses, made by one manager as one piece of work. |
| Beat | One transfer of a burst. |
| Read-modify-write | Reading a word, changing the value, and writing it back: two transfers that depend on each other. |
| Locked sequence | Transfers that no other manager may get between. |
| HMASTLOCK | The AHB wire with which a manager marks the transfers of a locked sequence. |
| Input stage | Registers in front of an arbiter. They keep one manager's request until it is served. |
On AHB the first beat of a burst is a NONSEQ transfer. Every later beat uses another value of HTRANS, called SEQ. That is how a subordinate and an arbiter can tell that a beat continues a burst.
One Picture: One Port and a Place to Wait
- Processor
- Peripheral
- Bus
- Memory
- Two sequences
Each manager needs two transfers to add to the counter: read it, then write the new value. Both start in the same cycle.
- Choosing at every cycle
Round robin serves the read of one, then the read of the other. Both have now read the same old value. One addition will be lost.
- A lock
The processor raises HMASTLOCK with its read. Once that read is served, the arbiter grants the port to nobody else.
- The request that waits
DMA showed its request for one cycle and moved on. Input stage 1 keeps that request, for as long as the lock lasts.
- Unlock
The processor writes, then lowers HMASTLOCK. One cycle later the port is free, and the waiting read of DMA is served. It reads the new value.
Read the diagram as text
- Processor: adds 1 to the counter: a read, then a write
- DMA: adds 10 to the same counter: a read, then a write
- Input stage 0: a register: keeps the processor's request until it is served
- Input stage 1: a register: keeps the request of DMA until it is served
- Arbiter: round robin, and it remembers who holds a lock
- SRAM port: one transfer in a cycle; one of its words is the counter
- Processor → Input stage 0: request, HMASTLOCK
- DMA → Input stage 1: request, HMASTLOCK
- Input stage 0 → Arbiter: waiting request
- Input stage 1 → Arbiter: waiting request
- Arbiter → SRAM port: the winner
Each arrow stands for a group of wires. The way back from SRAM is not drawn.
The arbiter in this picture is the round-robin arbiter of the last lesson. It has one more register, which says who holds a lock.
The Smallest Example, Worked by Hand: A Lost Addition
The counter holds 5. The processor adds 1 and DMA adds 10, so the right result is 16. Round robin serves one transfer in each cycle, and chooses again each time.
| Cycle | Processor | DMA | Counter afterwards |
|---|---|---|---|
| 1 | reads 5 | 5 | |
| 2 | reads 5 | 5 | |
| 3 | writes 5 + 1 = 6 | 6 | |
| 4 | writes 5 + 10 = 15 | 15 |
The counter ends at 15. DMA computed its sum from a value that was already out of date. Now let each manager lock its read and its write together.
| Step | Processor | DMA | Counter afterwards |
|---|---|---|---|
| 1 | reads 5 | waits | 5 |
| 2 | writes 6 | waits | 6 |
| 3 | reads 6 | 6 | |
| 4 | writes 6 + 10 = 16 | 16 |
The same reasoning gives the price of a burst. DMA has a burst of four beats to SRAM. One read of the processor arrives when the second beat is due.
| The arbiter | The read waits | The burst |
|---|---|---|
| keeps its grant until the last beat | three cycles | stays whole |
| chooses again after every beat | no cycle | is split after its first beat, and ends one cycle later |
The Rules
Rule 1: A burst may be kept whole or split between beats.
Keeping it whole makes other managers wait for its full length. Splitting it serves them sooner, and the burst must be picked up again.
Rule 2: A locked sequence is never split.
The manager raises HMASTLOCK for its transfers. Until the sequence ends, no other manager is served at that subordinate.
Rule 3: The bus is locked after a cycle with HMASTLOCK at 1 and HREADY at 1.
It is unlocked after a cycle with HMASTLOCK at 0 and HREADY at 1. So unlocking takes one cycle after the last locked transfer.
Rule 4: A locked sequence stays with one subordinate, and stays short.
All its transfers go to the same region. While it lasts, every other manager at that subordinate waits.
Rule 5: A request that waits is kept whole.
The input stage keeps the address, the direction and the lock. Forget any of them, and the wrong transfer is served later, or none.
Sources for These Rules
Arm AMBA AHB Protocol Specification, document IHI 0033, issue C. Locked transfers, and the two conditions of rule 3: section 3.3. The same section recommends an idle cycle after a locked transfer, and requires one region. That section also requires every subordinate that several managers can reach to obey HMASTLOCK.
Bursts: section 3.6. An interconnect may end a burst early to let another manager in, and a subordinate must cope with that: section 3.6.2. The transfer type SEQ: section 3.2.
The specification does not say how an arbiter treats a burst, and does not describe an input stage. Both are design practice. The lab follows rule 3 on a simplified port, where a request is taken and served in one step.
Now Run It
The lab at the end of this page is the hand example as a circuit. Two managers, both test code, add 1 and 10 to a counter that starts at 5. Each shows a request for one cycle, and waits until the SRAM port has served it. Here both raise hmastlock, the lab's name for HMASTLOCK.
e0 waiting: cpu=none dma=none | hmastlock=00 locked=nobody | served: nobody | counter=5
e1 waiting: cpu=none dma=none | hmastlock=11 locked=nobody | served: nobody | counter=5
e2 waiting: cpu=READ dma=READ | hmastlock=11 locked=nobody | served: cpu READ 5 | counter=5
e3 waiting: cpu=none dma=READ | hmastlock=11 locked=cpu | served: nobody | counter=5
e4 waiting: cpu=WRITE dma=READ | hmastlock=11 locked=cpu | served: cpu WRITE 6 | counter=5
e5 waiting: cpu=none dma=READ | hmastlock=01 locked=cpu | served: nobody | counter=6
e6 waiting: cpu=none dma=READ | hmastlock=01 locked=nobody | served: dma READ 6 | counter=6
e7 waiting: cpu=none dma=none | hmastlock=01 locked=dma | served: nobody | counter=6
e8 waiting: cpu=none dma=WRITE | hmastlock=01 locked=dma | served: dma WRITE 16 | counter=6
e9 waiting: cpu=none dma=none | hmastlock=00 locked=dma | served: nobody | counter=16
e10 waiting: cpu=none dma=none | hmastlock=00 locked=nobody | served: nobody | counter=16
counter=16 | finished: cpu=1 dma=1Rule 3 at work: e2 serves the read of the cpu with its hmastlock at 1, and e3 sees the port locked. Rule 2 at work: at e3 the dma has a READ waiting and it is the dma's turn, yet nobody is served.
Rule 5 at work: the READ of the dma stays in the "waiting" column from e2 to e6. The dma showed it for one cycle, before e2. Rule 3 again: e5 sees the cpu's hmastlock at 0, and e6 is the first edge with the port free. The counter ends at 16.
The lab has two constants, LOCK and KEEP. Each of the next two runs changes one of them.
| Run | Last line of the lab |
|---|---|
| LOCK 1, KEEP 1 (the run above) | counter=16 | finished: cpu=1 dma=1 |
| LOCK 0: no locked sequence | counter=15 | finished: cpu=1 dma=1 |
| KEEP 0: the input stage forgets a request that is not served at once | counter=6 | finished: cpu=1 dma=0 |
Recorded from the lab. "finished" is 1 when a manager has completed its read and its write.
With no lock the result is 15, as in the hand example. With no memory in the input stage, the read of the dma is forgotten while the port is locked. The dma never finishes, and the counter stays at 6.
For the burst, the lab of the last lesson gains one constant, called HOLD_BURST. With it at 1, the arbiter keeps its grant while more beats follow.
e1 cpu wants ROM, dma wants SRAM | granted: cpu and dma | served: cpu and dma
e2 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma
e3 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma
e4 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma
e5 cpu wants SRAM, dma wants nothing | granted: cpu | served: cpuThe read of SRAM waits from e2 to e4 and is served at e5: a longest wait of 3 edges. With HOLD_BURST at 0 the same run gives the cpu a longest wait of 0, and the dma one of 1.
The Burst Lab
// Two managers and two destinations. Each manager works through a list of requests, one at a time.
// An arbiter chooses when both managers ask for the same thing in the same cycle.
module arbiter #(parameter ROTATE = 1) (
input logic clk,
input logic req_cpu, req_dma, // who asks now
input logic ready, // 1: one transfer can be served in this cycle
input logic more_cpu, more_dma, // 1: the transfer being served is not the last beat of its burst
output logic grant_cpu, grant_dma // who may go
);
logic last_dma = 1; // who was served last. At the start, the cpu wins a tie
wire tie = req_cpu && req_dma;
wire cpu_first = !ROTATE || last_dma; // fixed priority: always the cpu. Round robin: whoever did not go last
logic kept_cpu = 0, kept_dma = 0; // 1: the grant is kept for the next beat of this manager's burst
assign grant_cpu = req_cpu && !kept_dma && (kept_cpu || !tie || cpu_first);
assign grant_dma = req_dma && !kept_cpu && (kept_dma || !tie || !cpu_first);
always @(posedge clk) if (ready && (grant_cpu || grant_dma)) begin
last_dma <= grant_dma;
kept_cpu <= grant_cpu && more_cpu;
kept_dma <= grant_dma && more_dma;
end
endmodule
module tb;
logic clk = 0;
always #5 clk = ~clk;
localparam ROTATE = 1; // 1: round robin. 0: fixed priority, the cpu first
localparam MATRIX = 1; // 1: a bus matrix, an arbiter at each destination. 0: one shared bus
localparam HOLD_BURST = 1; // 1: the arbiter keeps its grant until a burst is over
// ---- The managers: test code. Each has a list of requests, and asks for one at a time ----
localparam NONE = 0, ROM = 1, SRAM = 2;
logic [1:0] cpu_list [0:9], dma_list [0:9];
logic dma_more [0:9]; // 1: another beat of the same burst follows this request
int cpu_i = 0, dma_i = 0; // how far each manager has come in its list
wire [1:0] cpu_wants = cpu_list[cpu_i], dma_wants = dma_list[dma_i];
// ---- The destinations ----
int edge_no = 0; // counts the rising clock edges
wire rom_ready = 1;
wire ram_ready = (edge_no != -1); // in this run SRAM is always ready
// ---- A bus matrix: each destination has its own arbiter, and sees only the requests meant for it ----
wire rom_cpu, rom_dma, ram_cpu, ram_dma; // the four grants
arbiter #(ROTATE) at_rom (clk, cpu_wants == ROM, dma_wants == ROM, rom_ready, 1'b0, 1'b0, rom_cpu, rom_dma);
arbiter #(ROTATE) at_ram (clk, cpu_wants == SRAM, dma_wants == SRAM, ram_ready, 1'b0, HOLD_BURST && dma_more[dma_i], ram_cpu, ram_dma);
// ---- A shared bus: one arbiter for everything. Only one manager may use the bus in a cycle ----
wire bus_cpu, bus_dma;
wire [1:0] bus_wants = bus_cpu ? cpu_wants : dma_wants; // where the manager with the bus wants to go
wire bus_ready = (bus_wants == SRAM) ? ram_ready : rom_ready;
arbiter #(ROTATE) at_bus (clk, cpu_wants != NONE, dma_wants != NONE, bus_ready, 1'b0, 1'b0, bus_cpu, bus_dma);
// Granted: the manager may go. Served: its destination was also ready, so the transfer happened.
wire cpu_grant = MATRIX ? (rom_cpu || ram_cpu) : bus_cpu;
wire dma_grant = MATRIX ? (rom_dma || ram_dma) : bus_dma;
wire cpu_served = MATRIX ? ((rom_cpu && rom_ready) || (ram_cpu && ram_ready)) : (bus_cpu && bus_ready);
wire dma_served = MATRIX ? ((rom_dma && rom_ready) || (ram_dma && ram_ready)) : (bus_dma && bus_ready);
// Counting and printing. A function turns bits into a name, for printing only.
int cpu_count = 0, dma_count = 0; // transfers served
int cpu_wait = 0, dma_wait = 0; // edges waited in a row
int cpu_longest = 0, dma_longest = 0; // the longest such wait
function string place(input logic [1:0] w);
return (w == ROM) ? "ROM" : (w == SRAM) ? "SRAM" : "nothing";
endfunction
function string who(input logic c, input logic d);
return (c && d) ? "cpu and dma" : c ? "cpu" : d ? "dma" : "nobody";
endfunction
always @(posedge clk) begin
$display("e%0d cpu wants %s, dma wants %s | granted: %s | served: %s", edge_no, place(cpu_wants), place(dma_wants),
who(cpu_grant, dma_grant), who(cpu_served, dma_served));
// A manager moves on to its next request when this one is served ("<=" takes effect after the edge).
if ((cpu_served || cpu_wants == NONE) && cpu_i < 9) cpu_i <= cpu_i + 1;
if ((dma_served || dma_wants == NONE) && dma_i < 9) dma_i <= dma_i + 1;
if (cpu_served) cpu_count++;
if (dma_served) dma_count++;
cpu_wait = (cpu_wants != NONE && !cpu_served) ? cpu_wait + 1 : 0;
dma_wait = (dma_wants != NONE && !dma_served) ? dma_wait + 1 : 0;
if (cpu_wait > cpu_longest) cpu_longest = cpu_wait;
if (dma_wait > dma_longest) dma_longest = dma_wait;
edge_no <= edge_no + 1;
end
initial begin
for (int i = 0; i < 10; i++) begin cpu_list[i] = NONE; dma_list[i] = NONE; dma_more[i] = 0; end
// Entry 0 is the idle cycle that edge 0 sees. The requests start at entry 1.
cpu_list[1] = ROM;
cpu_list[2] = SRAM;
dma_list[1] = SRAM; dma_more[1] = 1;
dma_list[2] = SRAM; dma_more[2] = 1;
dma_list[3] = SRAM; dma_more[3] = 1;
dma_list[4] = SRAM; dma_more[4] = 0;
repeat (8) @(posedge clk);
#1 $display("cpu: served %0d, longest wait %0d | dma: served %0d, longest wait %0d", cpu_count, cpu_longest, dma_count, dma_longest);
$finish; // $finish ends the simulation
end
endmoduleThese labs leave things out. A request is served in one cycle, with no separate data phase. The SRAM is always ready. A real input stage also has to take the write data, which arrives one phase after the address.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Keep a burst whole | Choose again after every beat | The burst is over soonest, and memory works at its best. An urgent request waits for the whole burst. |
| A lock wire on the bus | Trust every manager to stay away | A read-modify-write is safe from any other manager. One manager can now hold a subordinate, so the sequence must be short. |
| An input stage that keeps the request | Make the manager show its request until it is served | The manager follows the plain bus rules, and need not know that an arbiter exists. It costs a register set for each manager. |
Could software do without the lock, by being careful? Not here. A program can stop itself from being interrupted. It cannot stop a DMA block, which is hardware and takes its turn when the arbiter gives it.
The Trap: Every Transfer Was Legal
A count of received bytes is sometimes ten too low. The team checks the bus with a protocol checker, a tool that tests every transfer against the bus rules. It reports no error, for days.
It cannot report one. Each read and each write is correct by itself. The fault is in the order of four correct transfers, as in the first table above. Only a check that knows the two sequences belong together can see it.
When a shared word goes wrong now and then, write down who reads it and who writes it. Look for a read by one manager between the read and the write of another.
At Scale
This section looks ahead. One lock on one port is easy to follow. A large chip has many managers, long bursts and deadlines.
The longest turn
The longest burst and the longest locked sequence decide how long the others can wait. The lesson on quality of service turns that into a number.
A lighter tool than a lock
A lock stops everybody. Newer buses also offer an exclusive transfer: the write goes ahead only if nobody touched the word since the read. The multicore lessons use it.
The signals
The Protocols track shows HMASTLOCK on real waveforms, in Many managers, arbitration and locking.
Try It
Run the lab. In each row, say why the request in the "waiting" column was or was not served.
Set LOCK to 0. Predict the last line from the hand example, then run.
Set LOCK back to 1 and KEEP to 0. Find the edge where the read of the dma disappears.
With LOCK and KEEP at 1, make the dma add 0. Say whether the lock still costs the dma any cycles.
e0 waiting: cpu=none dma=none | hmastlock=00 locked=nobody | served: nobody | counter=5 e1 waiting: cpu=none dma=none | hmastlock=11 locked=nobody | served: nobody | counter=5 e2 waiting: cpu=READ dma=READ | hmastlock=11 locked=nobody | served: cpu READ 5 | counter=5 e3 waiting: cpu=none dma=READ | hmastlock=11 locked=cpu | served: nobody | counter=5 e4 waiting: cpu=WRITE dma=READ | hmastlock=11 locked=cpu | served: cpu WRITE 6 | counter=5 e5 waiting: cpu=none dma=READ | hmastlock=01 locked=cpu | served: nobody | counter=6 e6 waiting: cpu=none dma=READ | hmastlock=01 locked=nobody | served: dma READ 6 | counter=6 e7 waiting: cpu=none dma=none | hmastlock=01 locked=dma | served: nobody | counter=6 e8 waiting: cpu=none dma=WRITE | hmastlock=01 locked=dma | served: dma WRITE 16 | counter=6 e9 waiting: cpu=none dma=none | hmastlock=00 locked=dma | served: nobody | counter=16 e10 waiting: cpu=none dma=none | hmastlock=00 locked=nobody | served: nobody | counter=16 counter=16 | finished: cpu=1 dma=1Check Yourself
Common Mistakes
Wrong Treat a read and a later write of a shared word as two separate jobs.
Another manager can change the word between them.
Fix Lock the pair, or use another scheme that detects the change.
Wrong Expect the port to be free in the cycle after the last locked transfer.
The bus is unlocked after a cycle with HMASTLOCK at 0.
Fix Count one more cycle before another manager is served.
Wrong Keep only "this manager wants the port" while a request waits.
By then the manager no longer shows the address or the direction.
Fix Keep the whole request in the input stage until it is served.
Remember
- A burst is a run of beats; an arbiter may keep it whole or split it, and each choice has a cost.
- A read-modify-write by two managers can lose an update even when every transfer is legal.
- A locked sequence, marked with HMASTLOCK, is never split.
- The bus is locked after a cycle with HMASTLOCK and HREADY at 1, and unlocked one cycle after HMASTLOCK falls.
- A request that waits is kept whole in an input stage until it is served.