Interrupt Latency
The timer fired on time, and the handler came late. The time between has several parts, and each part belongs to someone else.
You Will Be Able To
- Name the two moments a latency is measured between.
- Split a latency into delivery, the wait for entry and the fetch, and name the owner of each.
- Add waits in a row, and take the longer of waits that run side by side.
- Lesson
- S6.15
- Time
- 22 min
- Runs on
- Your browser
- Check
- 6 questions
A motor controller must answer its timer within ten clock periods. On the bench it takes five. In the field it sometimes takes nine, and nothing is broken. Which part of the wait grew?
Before You Start
You need the enable bits: pending, the two gates and a masked request. You need Interrupt Entry and Return: the forced jump to a slot of the handler table.
You need S0.5: one read on AHB has an address phase and then a data phase.
| Question | |
|---|---|
| 1 | Between which two moments is an interrupt latency measured? |
| 2 | Which parts does it consist of, and who owns each part? |
| 3 | How do two waits add when they run one after the other, and when they run side by side? |
| 4 | How many clock periods does the lab need from the event to the first handler word? |
| 5 | Why can a faster memory for the handler leave the latency unchanged? |
The Idea: A Sum of Parts with Different Owners
The latency of an interrupt is the time from an event to the answer of the processor. It is never one number that a processor simply has. It is a chain of waits, and each link belongs to a different part of the chip, or to the program.
First the request must reach the processor. That is hardware outside the processor: the device, its wires, and for most devices the interrupt controller. Then the processor must be willing. It finishes the instruction in progress, and it waits for any gate that the program has closed. Then the first instruction of the handler must be read from memory, over the bus.
Two things make the sum harder than it looks. Some waits come one after the other, and those add. Other waits run side by side, and of those only the longest counts. A reader who adds everything gets a number that is too large, and then fixes the wrong part.
The second thing is the end point. The first handler instruction is not yet help for the device. So every latency number must name its two moments, or it means nothing.
New Words
| Word | Meaning |
|---|---|
| Latency of an interrupt | The time from an event in a device to a named moment in its handler. On this page: to the first word of the handler. |
| Delivery time | The part from the event until the pending bit in the processor is 1. |
| Wait for entry | The part from pending until the forced jump. The processor finishes its instruction, and waits for any closed gate. |
| Masked time | How long the program keeps a gate closed while a request is pending. |
| Fetch time | The part from the forced jump until the first word of the handler has arrived from memory. |
| Service latency | A latency that ends later: at the first useful action of the handler on the device. |
| Worst case of a latency | The longest value the latency can take, as opposed to the usual one. |
One Picture: Event, Pending, Entry, First Word
- Peripheral
- Memory
- Processor
- Bus
- Control / FSM
- The event
A timer reaches its deadline, or a byte arrives. The clock that matters to the product starts here.
- Delivery
The request travels to the processor: through flip-flops, and for a device also through the interrupt controller. It ends when the pending bit is 1.
- The wait for entry
The processor finishes the instruction in progress. If the program has closed a gate, it also waits for the gate. Both waits run at the same time.
- The fetch
After the forced jump, the first instruction of the handler must come from memory: one read on the bus.
- What comes after
The handler saves registers before it touches the device. A deadline often means this last moment, not the one before.
Read the diagram as text
- Event: something happens in a device
- Pending: the processor can see the request
- Entry: the forced jump
- First handler word: the first instruction has arrived
- Service: the handler does its real work
- Event → Pending: delivery: device and controller
- Pending → Entry: wait for entry: program and processor
- Entry → First handler word: fetch: bus and memory
- First handler word → Service: saves: the handler
Four parts in a row, and four owners. A measured latency must say which two moments it runs between.
The Smallest Example, Worked by Hand
Count in clock edges, and call the edge of the event e0. The timer needs one edge to show its request. The instruction in progress needs two more edges. The read of the first handler word needs two edges: an address phase, then a data phase.
| Moment | Edge | Part that ends here | Length |
|---|---|---|---|
| Event | e0 | ||
| Pending | e1 | delivery | 1 |
| Entry | e3 | wait for entry: the instruction in progress | 2 |
| First handler word | e5 | fetch | 2 |
The three parts follow each other, so they add: 1 + 2 + 2 = 5 clock periods.
Now the program is in a stretch that keeps the global enable closed for six edges after pending. The instruction in progress still needs two. These two waits start together and run side by side. Entry needs both to be over, so it comes after the longer one.
| Moment | Edge | How it is found |
|---|---|---|
| Event | e0 | |
| Pending | e1 | delivery: 1 |
| Instruction done | e3 | 1 + 2 |
| Gate open again | e7 | 1 + 6 |
| Entry | e7 | the later of e3 and e7 |
| First handler word | e9 | e7 + 2 |
The latency is 9 clock periods. Adding every wait would give 1 + 2 + 6 + 2 = 11, which is wrong.
The Rules
Rule 1: A latency is measured between two named moments.
Say where it starts and where it ends. Event to pending, event to first handler word and event to service are three different numbers.
Rule 2: Parts that follow each other add.
Delivery, then the wait for entry, then the fetch.
Rule 3: Entry needs three things at once: a pending request, two open gates and an instruction that has just ended.
So of the waits that run side by side, only the longest counts.
Rule 4: Each part has an owner.
Delivery belongs to the device and the controller. Masked time belongs to the program. The instruction belongs to the processor. The fetch belongs to the bus and the memory.
Rule 5: A deadline is checked against the worst case.
The usual latency says little. The longest masked time and the slowest fetch decide.
Rule 6: The first handler word is not yet service.
The handler still saves registers and reaches the device. A deadline on the device includes that time.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 4.9, for the forced jump at the end of an instruction.
The two phases of the fetch: Arm AMBA AHB Protocol Specification, IHI 0033 issue C, section 3.1: a basic transfer has an address phase and a data phase.
The parts and their names are our own way of splitting the time. Other texts split it differently. The one-edge delivery, the two-edge instruction and the two-edge fetch are values of this lab, not of any real processor.
Rule 3 rests on the gates of RISC-V. They are not checked against the privileged specification of RISC-V: our reference library does not hold it.
Now Run It
The lab at the end of this page has the timer inside ap_clint, with a deadline of 5 counts. Test code, a bus-functional model, stands in for the processor. It waits as a processor would, forces the PC to the timer slot, and reads one word over the fast bus.
e0 event: the count reaches the deadline
e1 pending: the processor can see the request
e3 entry: the PC is forced to 0000011c
e5 first word of the handler=00b52023
latency=5 clock periodsRule 2 at work: the rows stand at e0, e1, e3 and e5. Delivery is 1, the wait for entry is 2, and the fetch is 2.
Rule 1 at work: the last row says which latency this is. It ends at the first word of the handler. The word 00b52023 is one instruction, and nothing has been saved yet.
This lab leaves things out. Its processor is a model with a fixed wait of two edges for the instruction in progress. A real processor needs a different time for each instruction. It may also need a period or two to turn round before the fetch.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Take an interrupt only between two instructions | Break into an instruction | Nothing half-done has to be saved. The latency includes the rest of the longest instruction. |
| Let the program close a gate | Interrupts can never be masked | Shared data can be updated in peace. Every masked stretch is added to the worst case of every interrupt. |
| The handler table in fast memory on the chip | The handler in slow memory | A short fetch that is the same every time. Fast memory is small, and costs area. |
| A request wire straight to the processor for the timer | The timer goes through the interrupt controller | One edge of delivery. The processor needs a wire and a pending bit for it. |
The Trap: The Fast Memory That Bought Nothing
Back to the motor controller that sometimes takes nine periods. The team blamed the fetch. They moved the handler into the fastest memory on the chip. The bench still showed five, and the field still showed nine.
e0 event: the count reaches the deadline
e1 pending: the processor can see the request
e7 entry: the PC is forced to 0000011c
e9 first word of the handler=00b52023
latency=9 clock periodsLook at where the time went. From entry to the first word is still two edges: e7 to e9. The whole growth is before entry, from e1 to e7. That is masked time, and rule 4 says who owns it: the program.
Somewhere a routine closed the gate, did a long piece of work, and opened it again. The bench never ran that routine at the wrong moment. Rule 5 is the lesson: the worst case decides, and the worst case hides in code that masks.
This is why a trace should record entry and the first word as two separate edges. With only the end points, a masked stretch and a slow memory look the same.
At Scale
This section looks ahead. On a large chip every part of the sum grows, and each grows for its own reason.
Delivery through a controller
A device request passes the interrupt controller, where a more urgent source may go first. The Interrupt Controller shows the choice.
A fetch that must wait for the bus
When another manager holds the bus, the fetch starts late. The worksheet of this page has such a bus.
Handlers that mask each other
A handler runs with the gate closed. So the longest handler is part of the worst case of every other interrupt.
Choosing how to serve a device
Drivers: Polling, Interrupts or DMA uses this latency as the cost of one visit.
Try It
Run the lab. Check the four edges against the first hand table.
Set MASKED_WAIT to 6. Predict the edge of the entry and the latency, then compare with the second hand table.
Set MASKED_WAIT to 2 and BOUNDARY_WAIT to 2. Explain why the latency is still 5.
e0 event: the count reaches the deadline e1 pending: the processor can see the request e3 entry: the PC is forced to 0000011c e5 first word of the handler=00b52023 latency=5 clock periodsRead included file: ahb_pkg.sv
`timescale 1ns/1ns
// ahb_pkg.sv: the encodings and the arithmetic of a burst (read-only).
// The memory, the driver, the checker and the lesson text all use these functions, so they never
// disagree about the address of a beat or the lanes it uses.
package ahb_pkg;
localparam logic [1:0] IDLE = 2'b00, BUSY = 2'b01, NONSEQ = 2'b10, SEQ = 2'b11;
localparam logic [2:0] SINGLE = 3'b000, INCR = 3'b001, WRAP4 = 3'b010, INCR4 = 3'b011,
WRAP8 = 3'b100, INCR8 = 3'b101, WRAP16 = 3'b110, INCR16 = 3'b111;
localparam logic OKAY = 1'b0, ERROR = 1'b1;
function automatic string trans_name(input logic [1:0] t);
case (t) IDLE: return "IDLE"; BUSY: return "BUSY"; NONSEQ: return "NONSEQ"; default: return "SEQ"; endcase
endfunction
function automatic string burst_name(input logic [2:0] b);
case (b)
SINGLE: return "SINGLE"; INCR: return "INCR"; WRAP4: return "WRAP4"; INCR4: return "INCR4";
WRAP8: return "WRAP8"; INCR8: return "INCR8"; WRAP16: return "WRAP16"; default: return "INCR16";
endcase
endfunction
function automatic string resp_name(input logic r);
return r ? "ERROR" : "OKAY";
endfunction
// s when c is 1, else the empty string (a string-typed result prints the same on every simulator).
function automatic string opt(input bit c, input string s);
if (c) return s;
return "";
endfunction
// Beats in a burst: 1 for SINGLE, 4, 8 or 16 for the fixed bursts, 0 for INCR (undefined length).
function automatic int beats_of(input logic [2:0] b);
case (b) SINGLE: return 1; INCR: return 0; WRAP4, INCR4: return 4; WRAP8, INCR8: return 8; default: return 16; endcase
endfunction
function automatic bit is_wrap(input logic [2:0] b);
return b == WRAP4 || b == WRAP8 || b == WRAP16;
endfunction
// The address of the beat after the one at a: the size in bytes further on; a wrapping burst
// returns to the start of its window (beats x size bytes, aligned to that total) at the window's end.
function automatic int unsigned next_addr(input int unsigned a, input logic [2:0] size, input logic [2:0] b);
int unsigned nb = 1 << size;
int unsigned window = nb * beats_of(b);
int unsigned base;
if (!is_wrap(b)) return a + nb;
base = (a / window) * window;
if (a + nb >= base + window) return base;
return a + nb;
endfunction
// The address of beat n of a burst that starts at start.
function automatic int unsigned beat_addr(input int unsigned start, input logic [2:0] size, input logic [2:0] b, input int n);
int unsigned a = start;
for (int k = 0; k < n; k++) a = next_addr(a, size, b);
return a;
endfunction
// The lanes a transfer at address a uses on a bus of bus_bytes bytes, packed as {upper, lower}.
// Every AHB transfer is aligned to its size, so it uses size bytes from its own lane upwards.
function automatic logic [15:0] beat_lanes(input int unsigned a, input logic [2:0] size, input int bus_bytes);
int lower = a % bus_bytes;
int upper = lower + (1 << size) - 1;
if (upper > bus_bytes - 1) upper = bus_bytes - 1;
return {upper[7:0], lower[7:0]};
endfunction
// The strobe of a transfer: a 1 for every lane it uses (bit l is lane l).
function automatic logic [63:0] beat_strb(input int unsigned a, input logic [2:0] size, input int bus_bytes);
logic [15:0] lanes = beat_lanes(a, size, bus_bytes);
logic [63:0] s = 0;
for (int l = lanes[7:0]; l <= lanes[15:8]; l++) s[l] = 1'b1;
return s;
endfunction
function automatic bit aligned(input int unsigned a, input logic [2:0] size);
return (a % (1 << size)) == 0;
endfunction
// True when an incrementing burst of n beats (n is used only for INCR) crosses a 1 KB boundary.
function automatic bit crosses_1k(input int unsigned start, input logic [2:0] size, input logic [2:0] b, input int n);
int beats = beats_of(b) ? beats_of(b) : n;
if (is_wrap(b) || beats < 2) return 0;
return (start / 1024) != ((start + beats * (1 << size) - 1) / 1024);
endfunction
endpackage
Read included file: irq_word_bfm.svh
// Single aligned words, always-ready target, one outstanding request.
// htrans[1] marks address acceptance; active marks its following data phase.
import ahb_pkg::IDLE;
import ahb_pkg::NONSEQ;
logic clk = 0, reset_n = 0;
always #5 clk = !clk;
integer edge_no = 0;
logic [1:0] htrans = IDLE;
logic hwrite = 0, active = 0, write_en = 0;
logic [31:0] haddr = 0, addr = 0, wdata = 0;
wire [31:0] rdata;
always @(posedge clk) begin
edge_no <= edge_no + 1;
active <= htrans[1];
if (htrans[1]) begin
addr <= haddr;
write_en <= hwrite;
end
end
task automatic word(input bit wr, input logic [31:0] target, payload,
output logic [31:0] result);
@(negedge clk);
htrans = NONSEQ;
haddr = target;
hwrite = wr;
@(negedge clk);
htrans = IDLE;
wdata = payload;
@(posedge clk);
result = rdata;
#1;
endtask
Read included file: ap_clint_timer.sv
// Timer slice of ap_soc's local interrupt block. One hart, one timebase tick
// per clk and a registered interrupt output. Word strobes arrive at commitment.
module ap_clint_timer #(parameter logic [63:0] RESET_COMPARE = '1) (
input logic clk, reset_n, write_cmp_low, write_cmp_high,
input logic [31:0] wdata,
output logic [63:0] mtime, mtimecmp,
output logic mtip
);
always_ff @(posedge clk) begin
if (!reset_n) begin
mtime <= 0;
mtimecmp <= RESET_COMPARE;
mtip <= 0;
end else begin
mtime <= mtime + 1'b1;
if (write_cmp_low) mtimecmp[31:0] <= wdata;
if (write_cmp_high) mtimecmp[63:32] <= wdata;
mtip <= mtime >= mtimecmp;
end
end
endmodule
Check Yourself
Common Mistakes
Wrong Add every wait to get the latency.
A closed gate and the instruction in progress run side by side.
Fix Add parts that follow each other. Of parts side by side, take the longest.
Wrong Quote a latency without its two moments.
Event to first word and event to service differ by the saves of the handler.
Fix Name the start and the end with every number.
Wrong Speed up the fetch when the time is lost before entry.
The fetch starts only after the forced jump.
Fix Record entry and first word apart, and fix the part that is long.
Remember
- A latency is measured between two named moments.
- Delivery, the wait for entry and the fetch follow each other, and add.
- A closed gate and the instruction in progress run side by side: the longer one counts.
- Each part has an owner. Find the long part before changing anything.