Reset Domain Crossing
Software restarts one timer and leaves the rest of the chip running. The timer forgets everything, as a reset should. Its neighbour does not forget, and that is where the trouble starts.
You Will Be Able To
- Find a reset domain crossing, also between two blocks on one clock.
- Name the two hazards of a reset domain crossing, and say which one a simulator can show.
- Put the steps of a safe local reset in order: drain, isolate, reset, release.
- Lesson
- S5.6
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
A driver restarts a timer that has misbehaved, and the timer comes back healthy. The bus does not: one read never finishes, and no later read can start. Both blocks have a running clock. What is the bus waiting for?
Before You Start
You need Reset Architecture: a reset is asserted with no clock. You need Reset Sources and the Reset Cause Register: a reset domain, and a reset that reaches only part of the chip.
You need APB, a Few Signals at a Time: a read has a setup cycle and an access cycle. The device says when it is ready. The bridge that drives those cycles is in Two Buses and the Bridge.
The timer on this page is read over the bus like any peripheral. The Anatomy of a Peripheral shows what such a block is made of. Read it first if a peripheral behind a bus is new to you.
Metastability is explained in Planning Clock Domains.
| Question | |
|---|---|
| 1 | What is a reset domain crossing? |
| 2 | Can two blocks on one clock have a reset domain crossing between them? |
| 3 | What are the two hazards of a reset domain crossing? |
| 4 | In the lab, why does waiting longer not finish the stuck read? |
| 5 | In which order do drain, isolate and reset come? |
The Idea: One Side Forgets and the Other Runs On
A chip can reset one block and leave its neighbours running. That is useful: a driver can restart a device without a reboot. It also creates a border. On one side are flip-flops that the reset clears. On the other are flip-flops that keep running.
A signal that goes from the first side to the second is a reset domain crossing. The two sides may share a clock. The crossing is still there, because the reset is asserted with no clock.
Such a crossing has two hazards, and they are different in kind.
The first is electrical. The reset can change the signal at any moment. The running side may sample it at just that moment, and a flip-flop there can go metastable. This is the hazard of a clock domain crossing, on one clock.
The second is a progress hazard. The running side may be in the middle of a job that needs something the reset side knew. The reset wipes that knowledge. The signal that arrives is a clean 0, sampled cleanly, and the job still never finishes.
The cure for both is an order of events. Finish the work in progress and start no more. Hold the crossing at a safe value. Then, and only then, reset.
New Words
| Word | Meaning |
|---|---|
| Reset domain crossing (RDC) | A signal from flip-flops that a reset clears to flip-flops that the same reset does not clear. |
| Local reset of a block | A reset that restarts one block while the rest of the chip runs on. |
| Live side | The side of a crossing that is not reset and keeps running. |
| Progress hazard | A fault in which every value is clean and some job can still never finish. |
| Drain | Start no new work, and let the work already accepted finish. |
| Isolate a crossing | Hold a crossing signal at a safe value while its sender is in reset. |
| Stand-in | A small block that plays the part of a real one in a lab, with rules of its own. |
One Picture: The Crossing and Its Two Hazards
- Bus
- Control / FSM
- Peripheral
- Clock / reset
- Two blocks, one clock
The bridge reads the timer over the bus. Both use the same clock. A check of clock domains finds nothing to worry about here.
- The timer has its own reset
Software can restart the timer alone. That reset clears the flip-flops of the timer and none of the bridge. They are two reset domains.
- The crossing
The answer of the timer leaves one reset domain and enters the other. That wire is a reset domain crossing.
- Hazard one: a change at any moment
The reset is asserted with no clock. The answer can change at any moment, also right beside a clock edge of the bridge.
- Hazard two: a forgotten read
The bridge is in the middle of a read. The reset wipes what the timer knew about that read. The bridge goes on waiting for an answer.
- The cure, in order
First let the read in progress finish, and start no new one. Then hold the answer at a safe value. Only then reset the timer.
Read the diagram as text
- Bridge: keeps running: no reset reaches it here
- Isolation gate: can hold the answer at a safe value
- Timer: has a reset of its own
- Reset controller: resets the timer when software asks
- One clock: the same clock for bridge and timer
- Bridge → Timer: read request
- Timer → Isolation gate: ready and data
- Isolation gate → Bridge: answer
- Reset controller → Timer: timer reset
- Reset controller → Isolation gate: isolate
- One clock → Bridge: clock
- One clock → Timer: clock
The timer and the bridge of the lab. The isolation gate is drawn as one box. In the lab it is a few lines of test code.
The Smallest Example, Worked by Hand
The lab has a bridge and a timer on one clock. The timer is a stand-in with one rule of its own. In the setup cycle of a read it saves its count and sets a bit called pending. It reports ready only while pending is 1. In the access cycle it gives up the saved count and clears pending.
Now software asks for a restart of the timer. The reset lands between the setup cycle and the end of the access cycle.
| Moment | Bridge (not reset) | Lab timer (reset) | Ready |
|---|---|---|---|
| Setup cycle | Selects the timer. Enable is 0. | Saves its count. Sets pending. | 0 |
| Just after | Moves to the access cycle. Enable is 1. | Pending is 1. | 1 |
| Timer reset, before the next edge | Still in the access cycle. Waits for ready. | Pending is cleared. | 0 |
| Every later edge | Still in the access cycle. | Sets pending only in a setup cycle. None comes. | 0 |
Look at the last row from both sides. The timer would answer if it saw a setup cycle. The bridge would send a setup cycle if this read finished. Each waits for the other. A longer wait changes nothing.
This failure rests on the rule of our lab timer. A timer that answered every access cycle would let this read finish, perhaps with a count of 0. So the lab shows one progress hazard, in one design. It is not a law that a read cut by a reset hangs.
The safe order uses the same parts. First the read finishes: that is the drain. Then the timer is cut off from the bus: it sees no select, and its data is held at 0. That is the isolation. Then the timer is reset. No read is in progress, so nobody waits.
- Sample 0
The setup cycle of the read. The bridge has selected the timer. Enable is still 0.
- Sample 1
Just after that clock edge. The lab timer has saved a count and set its pending bit. The bridge is now in the access cycle, and ready is 1.
- Sample 2
The timer is reset before the next edge. Its pending bit is gone, so ready is 0. The bridge still waits in the access cycle.
- Sample 3
The reset is released and two more clock edges pass. Nothing has moved. Nothing will.
Drawn from the first four rows of the lab. The bridge is not reset. The timer is.
The Rules
Rule 1: A reset domain crossing is found by resets, not by clocks.
Ask of every signal: can its sender be reset while its receiver runs?
Rule 2: The live side must not sample a crossing while its sender is being reset.
An asynchronous assertion can change it at any moment. This is the electrical hazard.
Rule 3: A reset must not wipe state that the live side still waits for.
This is the progress hazard. No synchronizer cures it.
Rule 4: Drain first.
Stop every block that can start new work at the target. Wait for the work already accepted to finish.
Rule 5: Then isolate, then reset.
The crossing is held at a safe value before the reset is asserted.
Rule 6: Release the reset first, then the isolation.
The sender must be in a known state again before the live side listens to it.
Sources for These Rules
Sampling a signal while it changes, and metastability: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 3.5.4.
The setup and access cycles of an APB read, and the ready signal: Arm AMBA APB Protocol Specification, IHI 0024 issue E, sections 3.3 and 4.1.
Reset domain crossing as a topic, and rules 3 to 6, are common design practice. No book or standard in our reference library covers them, so none is quoted.
The timer that saves a count in the setup cycle, the restart command and the order of the second trial are choices of our lab.
Now Run It
The lab at the end of this page runs the read twice. Test code plays the processor: it writes a restart command to a control register, then reads the timer. Test code also plays the reset controller, so it can choose the moment of the reset.
setup sel=1 enable=0 armed=1 pending=0 hready=0
reserved sel=1 enable=1 pending=1 hready=1
reset isolated=0 pending=0 enable=1 hready=0 sel=1 local_reset=1
after release sel=1 enable=1 pending=0 hready=0
setup sel=1 enable=0 armed=1 pending=0 hready=0
reserved sel=1 enable=1 pending=1 hready=1
drain pending=1 hready=1 read=00000005
reset isolated=1 pending=0 enable=0 hready=1 sel=0 local_reset=1
next read=00000004 hready=1Rule 3 broken, in rows three and four. At the reset, enable=1 and pending=0: the bridge waits and the timer has forgotten. After the release the row is the same, and hready is still 0.
Rule 4 at work, in the row that starts with "drain". The read finishes first: hready=1 and the count is 5.
Rule 5 at work, in the next row. With isolated=1 the timer is reset while sel=0 and enable=0. No read is in progress. Rule 6 at work, in the last row: the next read finishes and returns 4. The count is small because the reset started the timer again.
This lab leaves things out, and the first is the most important. It does not show the electrical hazard at all. A simulator of 0 and 1 gives a clean 0 when the timer is reset. It cannot show a flip-flop of the bridge caught by a changing signal. The second: the stuck read rests on the rule of the stand-in timer, as said above.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Drain before a local reset | Reset at once | No job is cut in half. The reset must wait, and a block that is truly stuck may never drain. |
| Isolate the crossing | Add a synchronizer on it | The live side sees a steady, safe value. A synchronizer would only deliver the wrong value cleanly. |
| Reset both sides together | A local reset | No crossing at all. More of the chip restarts, and more state is lost. |
| A timeout on the live side | Wait for ever | A way out when the target cannot drain. The read ends with an error, and software must handle it. |
The Trap: The Clock Check Said Nothing
Back to the driver that restarted its timer. The review had checked every clock domain crossing on the chip. The timer and the bridge share a clock, so the list had no entry for them. Nobody asked the other question: which of the two can be reset alone?
The second trap is the fix that seems obvious. Put two flip-flops on the ready signal. They would deliver the 0 a little later, and just as clean. The read would hang in the same place. A synchronizer answers the electrical hazard. It has no answer for a forgotten read.
The third trap is at the end of a recovery. Software sees a timeout and writes the same value again. The first write may have taken effect before its answer was lost. Now it has happened twice.
At Scale
This section looks ahead. One timer and one bridge become every block that software or a watchdog can restart alone.
Find every listener
For each reset, a tool lists the signals that leave its domain and reach flip-flops that keep running. People then decide what protects each one.
Close every door
The processor may be idle while a debugger can still start a read at the same target. Every block that can start a read must stop.
A way out for a dead block
A block that is truly stuck never drains. A part that stays alive ends its transfers with an error, so that the reset can go ahead.
The same order at a power boundary
A block that is switched off needs the same steps: drain, isolate, then off. Power at Chip Level is the next page.
Try It
Run the lab. It makes the same software request twice. The first time the reset comes in the middle of a read. The second time the read is allowed to finish first.
In the first trial, wait for ten clock edges after the release in place of two. Does the read finish?
In the second trial, remove the line that sets "isolated" to 1. Which row changes?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| sel, enable | The select and the enable of the APB read of the timer. Enable 0 is the setup cycle. Enable 1 is the access cycle. |
| armed | Software has asked for a restart of the timer. The reset controller has not applied it yet. |
| pending | A bit inside the lab timer. 1 means: I have saved a count for the read in progress. |
| hready | The transfer is complete, as the bus model that plays the processor sees it. |
| local_reset | 1 while the reset of the timer is asserted. |
| isolated | 1 while the timer is cut off from the bus: it sees no select, and its data is held at 0. |
| read= | The count that a read returned, in hexadecimal. |
setup sel=1 enable=0 armed=1 pending=0 hready=0 reserved sel=1 enable=1 pending=1 hready=1 reset isolated=0 pending=0 enable=1 hready=0 sel=1 local_reset=1 after release sel=1 enable=1 pending=0 hready=0 setup sel=1 enable=0 armed=1 pending=0 hready=0 reserved sel=1 enable=1 pending=1 hready=1 drain pending=1 hready=1 read=00000005 reset isolated=1 pending=0 enable=0 hready=1 sel=0 local_reset=1 next read=00000004 hready=1Read included file: bridge.sv
`timescale 1ns/1ns
// ahb_apb_bridge.sv: an AHB subordinate that turns each accepted transfer into one APB transfer
// (read-only). The address phase is registered when HSEL, HREADY and a NONSEQ or SEQ transfer are
// all present. During SETUP and unfinished ACCESS the bridge keeps HREADYOUT low. A successful APB
// completion ends the AHB data phase with OKAY and HRDATA = PRDATA. A failed APB completion starts
// the two-cycle ERROR (HREADYOUT low then high, HRESP high in both). HWDATA and HWSTRB are data-phase signals the manager holds
// through the wait states, so PWDATA and PSTRB are driven from them directly; PADDR is the aligned
// word address (IHI 0024E 2.1.1 leaves an unaligned PADDR unpredictable). HPROT maps onto PPROT:
// privileged from HPROT[1], instruction from HPROT[0] = 0, and the access is Secure.
// A write therefore costs one setup and one access cycle: two cycles of data phase with no APB wait.
module ahb_apb_bridge (
input logic hclk,
input logic hresetn,
input logic hsel,
input logic [31:0] haddr,
input logic [1:0] htrans,
input logic hwrite,
input logic [3:0] hprot,
input logic [31:0] hwdata,
input logic [3:0] hwstrb,
input logic hready,
output logic hreadyout,
output logic hresp,
output logic [31:0] hrdata,
output logic psel,
output logic penable,
output logic pwrite,
output logic [31:0] paddr,
output logic [31:0] pwdata,
output logic [3:0] pstrb,
output logic [2:0] pprot,
input logic pready,
input logic [31:0] prdata,
input logic pslverr);
localparam int IDLE = 0, SETUP = 1, ACCESS = 2, ERR1 = 3, ERR2 = 4;
bit trace = 0;
int st = IDLE;
logic wr = 0;
logic [31:0] a = 0;
logic [2:0] prot = 0;
wire take = hsel && hready && htrans[1];
assign psel = (st == SETUP || st == ACCESS);
assign penable = (st == ACCESS);
assign pwrite = wr;
assign paddr = {a[31:2], 2'b00}; // APB leaves an unaligned PADDR unpredictable: the word, and PSTRB picks the bytes
assign pwdata = wr ? hwdata : 32'h0;
assign pstrb = wr ? hwstrb : 4'h0;
assign pprot = prot;
assign hreadyout = (st == IDLE || st == ERR2) ? 1'b1 : (st == ACCESS) ? (pready === 1'b1 && pslverr !== 1'b1) : 1'b0;
assign hresp = (st == ERR1 || st == ERR2);
assign hrdata = (st == ACCESS) ? prdata : 32'h0;
always @(posedge hclk or negedge hresetn) begin
if (!hresetn) st <= IDLE;
else begin
case (st)
SETUP: st <= ACCESS;
ACCESS: if (pready === 1'b1) begin
if (trace) $display("apb e%0d: %0s 0x%03h%0s", ($time - 5) / 10, wr ? "write" : "read", a[11:0], pslverr === 1'b1 ? " PSLVERR" : "");
st <= (pslverr === 1'b1) ? ERR1 : IDLE;
end
ERR1: st <= ERR2;
default: st <= IDLE;
endcase
// A new address phase can be taken whenever HREADY is 1: in IDLE, in the last cycle of ERROR,
// or at the edge an access completes with OKAY.
if (take && (st == IDLE || st == ERR2 || (st == ACCESS && pready === 1'b1 && pslverr !== 1'b1))) begin
wr <= hwrite; a <= haddr; prot <= {~hprot[0], 1'b0, hprot[1]}; st <= SETUP;
end
end
end
endmodule
Read included file: reset_bus.svh
// One aligned word at a time. The manager stands in for the future ap_rv32.
logic clk = 0, bus_reset_n;
always #5 clk = ~clk;
logic [31:0] addr = 0, wdata = 0;
logic write_req = 0, request = 0;
wire sel, enable, writing, ready, error, pready, slave_error;
wire [31:0] paddr, pwdata, prdata, rdata;
ahb_apb_bridge bridge (
.hclk(clk), .hresetn(bus_reset_n), .hsel(addr[31:16] == 16'h1000),
.haddr(addr), .htrans({request,1'b0}), .hwrite(write_req), .hprot(4'b0011),
.hwdata(wdata), .hwstrb(4'hf), .hready(ready), .hreadyout(ready),
.hresp(error), .hrdata(rdata), .psel(sel), .penable(enable), .pwrite(writing),
.paddr(paddr), .pwdata(pwdata), .pstrb(), .pprot(), .pready(pready),
.prdata(prdata), .pslverr(slave_error)
);
task automatic issue(input bit wr, input logic [31:0] address, data);
@(negedge clk);
write_req = wr;
addr = address;
request = 1;
@(negedge clk);
request = 0;
addr = 0;
wdata = data;
endtask
Read included file: ap_sysctl_reset_port.sv
// Reset-service words within the ap_soc laboratory system-control page.
// All manager accesses to this port are aligned, full-word APB transfers.
// The cause storage has a separate reset owner and enters through cause.
module ap_sysctl_reset_port (
input logic clk, bus_reset_n,
input logic sel, enable, writing,
input logic [31:0] paddr, pwdata,
input logic [4:0] cause,
input logic service_slot,
output logic page_selected, access_error, armed,
output logic [31:0] read_data
);
localparam logic [31:0] CAUSE = 32'h1000_4004, COMMAND = 32'h1000_4008;
wire cause_read = !writing && paddr == CAUSE;
wire command_write = writing && paddr == COMMAND;
assign page_selected = sel && paddr[31:12] == CAUSE[31:12];
assign read_data = cause_read ? {27'b0,cause} : 32'b0;
assign access_error = page_selected && enable && !(cause_read || command_write);
always_ff @(posedge clk or negedge bus_reset_n) begin
if (!bus_reset_n)
armed <= 0;
else if (page_selected && enable && command_write)
armed <= pwdata[0];
else if (service_slot)
armed <= 0;
end
endmodule
Read included file: rdc_timer_port.sv
// The timer runs in the same clock domain as the bridge but has its own reset.
// A read reserves the count in setup and consumes the reservation in access.
module rdc_timer_port (
input logic clk, timer_reset_n, selected, enable,
output logic pending, output logic [31:0] snapshot
);
logic [31:0] ticks;
always_ff @(posedge clk or negedge timer_reset_n) begin
if (!timer_reset_n) begin
ticks <= 0;
pending <= 0;
snapshot <= 0;
end else begin
ticks <= ticks + 1'b1;
if (selected && !enable) begin
snapshot <= ticks;
pending <= 1;
end else if (selected && enable && pending) begin
pending <= 0;
end
end
end
endmodule
Check Yourself
Common Mistakes
Wrong Check the clocks and call the crossing safe.
A reset is asserted with no clock, also between two blocks on one clock.
Fix Ask which side can be reset while the other runs.
Wrong Put a synchronizer on the crossing and stop there.
It delivers a clean value. It cannot bring back what the reset wiped.
Fix Drain the work in progress, then isolate, then reset.
Wrong Isolate as soon as software asks for the reset.
A read already accepted still needs its answer.
Fix Let that read finish first.
Remember
- A reset domain crossing goes from flip-flops that a reset clears to flip-flops that keep running. One clock is no protection.
- It has two hazards: a signal sampled while it changes, and a job that can never finish.
- A simulator of 0 and 1 shows only the second, and here only through a rule of the lab timer.
- The safe order is drain, isolate, reset. Then release the reset, then the isolation.