The Interrupt Path
One event in a device becomes one run of a handler. On the way it is remembered in three places, and the handler must settle with all three, in the right order.
You Will Be Able To
- Follow one interrupt from a device through the controller to the handler, and back.
- Say what the device, the controller and the processor each remember, and which action removes each record.
- Explain why one event can run a handler twice, and tell an external interrupt from a core-local one.
- Lesson
- S6.1
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
You set the timer of the board for one sensor reading. Two records appear in the log, with almost the same time on them. The timer fired once.
Before You Start
You need S0.8: what an interrupt is, the saved PC and the return. You need Level, Edge and Write-1-to-Clear: the sticky flag, the enable bit and the clear.
You need The Interrupt Controller: the gateway, the threshold, the claim and the completion. This page uses those words and does not teach them again.
| Question | |
|---|---|
| 1 | What is an interrupt, and which three blocks does it pass through on this chip? |
| 2 | What does each of the three blocks remember about one event? |
| 3 | Which four actions does a handler take, and in which order? |
| 4 | How can one event run the handler twice? |
| 5 | Which interrupts of ap_soc do not pass through the controller? |
The Idea: One Event, Remembered Three Times
An interrupt is a jump that a device forces on the processor. The processor finishes its instruction, saves the PC and runs a handler. The handler serves the device and returns, and the interrupted program carries on. That is all of S0.8, where one timer had one wire to the processor.
On a real chip the request passes through three blocks, and each one keeps its own record of it. The device keeps a sticky flag: "this happened". The interrupt controller keeps a pending bit: "source 3 is asking". The processor keeps the saved PC: "I was in the middle of something".
None of the three knows about the other two. So nothing removes all three records at once. The handler has to remove each one with its own action, at its own address. The whole way there and back is the path of an interrupt.
The order of the actions matters. Get it wrong one way and the same event runs the handler again. Get it wrong the other way and the device can never interrupt again. Both faults pass a quick test.
New Words
| Word | Meaning |
|---|---|
| Path of an interrupt | The way of one interrupt: from the event in a device to the handler, and the handler's actions back. |
| External interrupt | An interrupt from a device, which reaches the processor through the interrupt controller. |
| Core-local interrupt | An interrupt with a wire of its own to the processor. It does not pass through the controller. |
| Handler entry | The moment the processor makes the forced jump into the handler. Entry for short. |
| In service | Claimed and not yet completed. The gateway of that source stays closed. |
| Repeated delivery | One event that runs the handler more than once. |
One Picture: Three Records and Four Actions
- Peripheral
- Bus
- Processor
- Memory
- The device remembers
The timer reaches its count. That moment is an edge cause, so the timer saves it in a sticky flag. With its enable bit at 1, the flag drives the request wire.
- The controller remembers
The gateway of source 3 passes one request and sets the pending bit. The source competes, wins, and the wire to the processor goes to 1.
- The processor remembers
Between two instructions the processor saves the PC and jumps to the handler. This is the forced jump of S0.8.
- Action 1: claim
The handler reads the claim and complete word and gets 3. The pending bit is cleared, and the gateway stays closed. The timer has noticed nothing: its flag is still 1.
- Action 2: clear the device
The handler does the work the event asked for. Then it writes 1 to the clear register of the timer. The flag and the request wire fall.
- Action 3: complete
The handler writes 3 to the claim and complete word. The gateway of source 3 opens. The request wire is 0, so nothing new is passed.
- Action 4: return
The last instruction of the handler puts the saved PC back. The interrupted program carries on, unaware.
Read the diagram as text
- Timer and PWM: record 1: a sticky flag, behind an enable bit
- ap_plic: record 2: a pending bit, then a closed gateway
- Processor: record 3: the saved PC
- Handler: the program that settles all three records
- Timer and PWM → ap_plic: request wire of source 3
- ap_plic → Processor: one interrupt wire
- Processor → Handler: forced jump
- Handler → ap_plic: claim, then completion
- Handler → Timer and PWM: clear write
- Handler → Processor: return
The path of an external interrupt in ap_soc. The lab below has the timer and the gateway of source 3. Test code stands in for the processor and the handler.
Read the picture once more as a list. Three records are made, left to right. Four actions remove them: claim, clear the device, complete, return.
The Smallest Example, Worked by Hand
One timer event, from start to finish. The timer is source 3. Its priority is above the threshold, and its source is enabled in the controller. Fill in each row from the row above it.
| Stage | What happens | Timer flag | Pending bit | In service | Wire to the processor |
|---|---|---|---|---|---|
| Match | The timer reaches its count. Its enable bit is still 0. | 1 | 0 | 0 | 0 |
| Unmask | Software sets the enable bit of the timer. The request wire rises. | 1 | 0 | 0 | 0 |
| Entry | The gateway has passed the request. The processor jumps to the handler. | 1 | 1 | 0 | 1 |
| Claim | The handler reads the claim and complete word. It gets 3. | 1 | 0 | 1 | 0 |
| Clear | The handler writes 1 to the clear register of the timer. | 0 | 0 | 1 | 0 |
| Complete | The handler writes 3 to the claim and complete word. | 0 | 0 | 0 | 0 |
| Return | The handler returns. | 0 | 0 | 0 | 0 |
Look at the row marked "Claim". The wire to the processor has fallen, and the pending bit is 0. It looks as if the interrupt is over. It is not: the timer flag is still 1, and only the closed gateway keeps it from asking again.
The Rules
Rule 1: Each block keeps its own record of an event.
The device keeps a flag, the controller a pending bit and a closed gateway, the processor the saved PC.
Rule 2: Each record is removed by its own action.
A write to the device, a claim and a completion at the controller, a return in the processor.
Rule 3: A claim says which device. It does not serve the device.
After the claim the flag in the device is still set.
Rule 4: Clear the device before the completion.
A completion opens the gateway. If the request wire is still 1, the gateway passes the same event again.
Rule 5: Complete once, with the ID that was claimed.
Without it the gateway of that source stays closed, and the device can never interrupt again.
Rule 6: A core-local interrupt is not claimed.
It has its own wire to the processor. Its handler settles with the device and returns.
Sources for These Rules
Interrupts, the saved address and the return: Harris and Harris, Digital Design and Computer Architecture, ARM edition. See sections 6.6.3 and 9.3.7.
The edge on which the clear write counts: Arm AMBA APB Protocol Specification, IHI 0024 issue E, section 3.1.
Rules 3 to 5 are read from our own controller and timer, and every row on this page is recorded from them. The claim and the completion follow the RISC-V PLIC design. Our reference library does not hold that specification, so treat its names as RISC-V facts.
Rule 6 and the words "external" and "core-local" follow RISC-V practice. They are RISC-V facts too.
The routes, the source numbers and the register addresses are our own choices for ap_soc.
Now Run It
The lab at the end of this page has the interrupt part of the timer and the gateway of source 3. Test code, a bus-functional model, stands in for the processor and the handler. So the lab shows records 1 and 2, and not the saved PC.
match raw=1 en=0 pending=0 busy=0 irq=0
unmask raw=1 en=1 pending=0 busy=0 irq=0
entry raw=1 en=1 pending=1 busy=0 irq=1
claim id=3
claimed raw=1 en=1 pending=0 busy=1 irq=0
bus sel=1 access=0 ready=1 data=00000001 raw_before=1
clear-setup raw=1 en=1 pending=0 busy=1 irq=0
bus sel=1 access=1 ready=1 data=00000001 raw_before=1
clear-access raw=0 en=1 pending=0 busy=1 irq=0
complete raw=0 en=1 pending=0 busy=0 irq=0
return raw=0 en=1 pending=0 busy=0 irq=0Rule 1 at work: the row "entry" has raw=1 and pending=1 together. Two blocks hold a record of one event. Rule 3 at work: the claim returns id=3, and the row "claimed" still has raw=1.
Rule 2 at work: raw falls in the row "clear-access", after a write to the timer. The row "complete" then has busy=0, after a write to the controller. In the row "return" every record the lab can see is 0.
- Cycle 3
The counter matches now. The timer stores that event at this edge.
- Cycle 6
The saved cause can reach the gateway. The gateway records pending at this edge.
- Cycle 7
The handler reads the ID. The gateway takes this request out of the pending set.
- Cycle 10
The device accepts the clear. Look one column later for the change in raw.
- Cycle 11
The handler releases the gateway. The device request is already low.
Contiguous rising-edge samples from the timer lab with SHOW_EDGES enabled. Entry and return instructions are outside this model.
The waveform is the same run at every clock edge. The match lasts one clock. The flag named raw lasts from the match until the clear, however long the handler takes.
This lab leaves things out. There is no processor, so nothing is saved and nothing jumps. The controller is one gateway: the priorities and the threshold of the last page are not here.
The Two Routes in ap_soc
Six devices of ap_soc send external interrupts. Each has a fixed source ID. The handler learns the ID from the claim.
| Source ID | Device | What the handler does first |
|---|---|---|
| 1 | UART0 | Reads the status of UART0 |
| 2 | GPIO | Reads masked status of GPIO |
| 3 | Timer and PWM | Serves the match, then clears the flag |
| 4 | SPI | Reads the status of the transfer |
| 5 | DMA | Checks whether the copy finished or failed |
| 6 | Watchdog | Its early warning: checks the health of the system |
| 7 to 16 | no device | Spare inputs |
A claim that returns 0 found nothing to serve.
Two requests take another route. A block named ap_clint, beside the processor, holds a second timer and a software interrupt bit. Each has a wire of its own to the processor. These are core-local interrupts. Their handler never reads the claim word: there is nothing to claim.
So "the timer interrupted" can mean two different blocks on this chip. Timer and PWM is a device on APB, and it is source 3. The timer in ap_clint is core-local. A handler that settles with the wrong one leaves the real request standing.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The device keeps its own flag | The controller remembers everything | A device works the same with any controller, or with none. The handler must write to two blocks. |
| The handler clears the device | The claim clears the device too | The controller needs no knowledge of any device register. The order of the two writes becomes the handler's duty. |
| The gateway stays closed until completion | It opens again at the claim | A request that is still high cannot ask twice while it is being served. A handler that forgets to complete shuts the source. |
| A core-local route for the processor's own timer | Send every interrupt through the controller | The most frequent interrupt of a system costs no claim and no completion. The processor needs more than one interrupt wire. |
The Trap: One Event, Two Records in the Log
Back to the two records in the log. The handler claimed, wrote its record and completed. It forgot the write to the timer. Here are the last two rows of that run.
complete raw=1 en=1 pending=0 busy=0 irq=0
return raw=1 en=1 pending=1 busy=0 irq=1At "complete" the gateway opens, and raw is still 1. By "return" the pending bit is 1 and the wire is 1 again. The processor jumps straight back into the handler, which writes a second record. This is repeated delivery, and rule 4 is the cure.
It is easy to miss, for a reason the hand table showed. Between the claim and the completion the wire is 0 and the pending bit is 0. Everything a probe on the controller can see looks finished. Before you blame the timer for firing twice, read its flag.
At Scale
This section looks ahead. On a chip with a hundred devices the path is the same, and the questions are about time.
Where the time goes
Split the delay of an event into four parts: flag to pending, pending to entry, entry to claim, claim to clear. Each part has a different owner.
A time stamp near the device
A time taken at handler entry says when software noticed. Only a time taken in the device says when the event happened.
Inside the processor
The processor has enable bits of its own: Interrupts at the Core. It can take one interrupt inside another: Interrupt Entry and Return.
Who should do the work
A handler is one way to serve a device. Drivers compares it with polling and with DMA.
Try It
Run the lab. For each printed row, say which of the three records changed, and which action changed it.
Set CLEAR_FIRST to 0, so that the handler never clears the timer. Predict the row marked "return" before you run it.
Set SHOW_EDGES to 1. The lab then prints every clock edge, which is what the waveform on this page is drawn from.
Names Printed by the Lab
| Name | Meaning |
|---|---|
| raw | Record 1: the sticky flag of the timer. |
| en | The interrupt enable bit of the timer. |
| pending | Record 2: the pending bit of source 3 in the controller. |
| busy | The source is in service: claimed and not yet completed. |
| irq | The wire from the controller to the processor. |
| raw_before | In a row that starts with "bus": the flag just before that clock edge of the clear write. |
match raw=1 en=0 pending=0 busy=0 irq=0 unmask raw=1 en=1 pending=0 busy=0 irq=0 entry raw=1 en=1 pending=1 busy=0 irq=1 claim id=3 claimed raw=1 en=1 pending=0 busy=1 irq=0 bus sel=1 access=0 ready=1 data=00000001 raw_before=1 clear-setup raw=1 en=1 pending=0 busy=1 irq=0 bus sel=1 access=1 ready=1 data=00000001 raw_before=1 clear-access raw=0 en=1 pending=0 busy=1 irq=0 complete raw=0 en=1 pending=0 busy=0 irq=0 return raw=0 en=1 pending=0 busy=0 irq=0Read included file: irq_lab_bus.svh
// APB register slice. One clock, aligned 32-bit transfers, all lanes enabled.
// Both peripheral models are always ready. Drive on falling edges.
logic clk = 0, rst_n = 0;
always #5 clk = ~clk;
logic select = 0, access_phase = 0, writing = 0;
logic [31:0] address = 0, data = 0;
wire accepted = select && access_phase && writing; // Completed write.
task automatic setup_write(input logic [31:0] addr, value);
@(negedge clk);
select = 1;
writing = 1;
address = addr;
data = value;
endtask
task automatic access_write;
@(negedge clk);
access_phase = 1;
endtask
task automatic end_write;
@(negedge clk);
select = 0;
access_phase = 0;
endtask
task automatic write_word(input logic [31:0] addr, value);
setup_write(addr, value);
access_write();
end_write();
endtask
Read included file: timer_irq_slice.sv
// One-shot compare and one external gateway for the S6 timer experiment.
// claim_read and complete_write are already-decoded register transactions.
module timer_irq_slice #(parameter logic [3:0] DEADLINE = 3) (
input logic clk, rst_n, accepted,
input logic [31:0] address, data,
input logic claim_read, complete_write,
input logic [4:0] complete_id,
output logic raw, enabled, pending, in_service, irq,
output logic [4:0] claim_id
);
localparam logic [31:0] CLEAR = 32'h1000_2014, ENABLE = 32'h1000_2018;
localparam logic [4:0] TIMER_ID = 3;
logic [3:0] count;
logic armed;
assign irq = pending;
assign claim_id = pending ? TIMER_ID : 0;
always_ff @(posedge clk) begin
if (!rst_n) begin
count <= 0;
armed <= 1;
raw <= 0;
enabled <= 0;
pending <= 0;
in_service <= 0;
end else begin
if (armed) begin
if (count == DEADLINE) armed <= 0;
else count <= count + 1'b1;
end
if (armed && count == DEADLINE) raw <= 1;
else if (accepted && address == CLEAR && data[0]) raw <= 0;
if (accepted && address == ENABLE) enabled <= data[0];
if (claim_read && pending) begin
pending <= 0;
in_service <= 1;
end else if (complete_write && complete_id == TIMER_ID) begin
in_service <= 0;
end else if (raw && enabled && !in_service) pending <= 1;
end
end
endmodule
Check Yourself
Common Mistakes
Wrong Write to the controller to silence a device.
The flag lives in the device. A claim and a completion never touch it.
Fix Clear the flag in the device, and see its request fall.
Wrong Complete first and clear the device afterwards.
The gateway opens while the request is still high, and passes the same event again.
Fix Claim, serve and clear the device, then complete.
Wrong Look for a core-local interrupt in the claim word.
It never passed through the controller.
Fix Find out which route the request took before choosing what to write.
Remember
- An interrupt is a jump forced by a device. On a real chip it passes through the device, the controller and the processor.
- Each of the three keeps its own record: a sticky flag, a pending bit with a closed gateway, and the saved PC.
- The handler removes them with four actions, in order: claim, clear the device, complete, return.
- Completing before the device is cleared delivers the same event twice.
- A core-local interrupt, such as the timer in ap_clint, never passes through the controller.