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.

Five questions this page answers
Question
1What is an interrupt, and which three blocks does it pass through on this chip?
2What does each of the three blocks remember about one event?
3Which four actions does a handler take, and in which order?
4How can one event run the handler twice?
5Which 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

New words on this page
WordMeaning
Path of an interruptThe way of one interrupt: from the event in a device to the handler, and the handler's actions back.
External interruptAn interrupt from a device, which reaches the processor through the interrupt controller.
Core-local interruptAn interrupt with a wire of its own to the processor. It does not pass through the controller.
Handler entryThe moment the processor makes the forced jump into the handler. Entry for short.
In serviceClaimed and not yet completed. The gateway of that source stays closed.
Repeated deliveryOne event that runs the handler more than once.

One Picture: Three Records and Four Actions

One Event, Three Records, Four ActionsStep through the picture. It follows one timer event to the handler, and then the handler's way back.
  • Peripheral
  • Bus
  • Processor
  • Memory
One Event, Three Records, Four ActionsStep through the picture. It follows one timer event to the handler, and then the handler's way back. component Timer and PWM record 1: a stickyflag, behind an enablebit component ap_plic record 2: a pendingbit, then a closedgateway component Processor record 3: the savedPC component Handler the program thatsettles all threerecords request wire ofsource 3one interrupt wireforced jumpclaim, then completionclear writereturn
  1. component Timer and PWMrecord 1: a sticky flag, behind an enable bit
    • request wire of source 3 → ap_plic
  2. component ap_plicrecord 2: a pending bit, then a closed gateway
    • one interrupt wire → Processor
  3. component Processorrecord 3: the saved PC
    • forced jump → Handler
  4. component Handlerthe program that settles all three records
    • claim, then completion → ap_plic
    • clear write → Timer and PWM
    • return → Processor
  1. 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.

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

  3. The processor remembers

    Between two instructions the processor saves the PC and jumps to the handler. This is the forced jump of S0.8.

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

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

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

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

Seven stages, by hand
StageWhat happensTimer flagPending bitIn serviceWire to the processor
MatchThe timer reaches its count. Its enable bit is still 0.1000
UnmaskSoftware sets the enable bit of the timer. The request wire rises.1000
EntryThe gateway has passed the request. The processor jumps to the handler.1101
ClaimThe handler reads the claim and complete word. It gets 3.1010
ClearThe handler writes 1 to the clear register of the timer.0010
CompleteThe handler writes 3 to the claim and complete word.0000
ReturnThe handler returns.0000

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

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

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

  3. Rule 3: A claim says which device. It does not serve the device.

    After the claim the flag in the device is still set.

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

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

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

Printed by the lab after each stage. The two rows that start with "bus" are the clear write, sampled at its two clock edges. The names are listed with the lab.
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=0

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

From Counter Match to Gateway CompletionStep through one service. Each column samples the signals just before a rising edge.
0123456789101112clkmatchrawenabledpendingirqclaimbusyclearcomplete
Scroll horizontally ↔
  1. Cycle 3

    The counter matches now. The timer stores that event at this edge.

  2. Cycle 6

    The saved cause can reach the gateway. The gateway records pending at this edge.

  3. Cycle 7

    The handler reads the ID. The gateway takes this request out of the pending set.

  4. Cycle 10

    The device accepts the clear. Look one column later for the change in raw.

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

External interrupt sources of ap_soc
Source IDDeviceWhat the handler does first
1UART0Reads the status of UART0
2GPIOReads masked status of GPIO
3Timer and PWMServes the match, then clears the flag
4SPIReads the status of the transfer
5DMAChecks whether the copy finished or failed
6WatchdogIts early warning: checks the health of the system
7 to 16no deviceSpare 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 choiceThe alternativeWhat the choice buys, and costs
The device keeps its own flagThe controller remembers everythingA device works the same with any controller, or with none. The handler must write to two blocks.
The handler clears the deviceThe claim clears the device tooThe controller needs no knowledge of any device register. The order of the two writes becomes the handler's duty.
The gateway stays closed until completionIt opens again at the claimA 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 timerSend every interrupt through the controllerThe 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.

The same lab with CLEAR_FIRST at 0: the handler never clears the timer.
complete raw=1 en=1 pending=0 busy=0 irq=0
return raw=1 en=1 pending=1 busy=0 irq=1

At "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 page1 device, 1 handler
A large chiphundreds of sources

This section looks ahead. On a chip with a hundred devices the path is the same, and the questions are about time.

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

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

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

  4. 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
NameMeaning
rawRecord 1: the sticky flag of the timer.
enThe interrupt enable bit of the timer.
pendingRecord 2: the pending bit of source 3 in the controller.
busyThe source is in service: claimed and not yet completed.
irqThe wire from the controller to the processor.
raw_beforeIn a row that starts with "bus": the flag just before that clock edge of the clear write.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=0
Read 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

Predict the outputIn this run the handler clears the timer, then completes with ID 2 by mistake. What is the last row the lab prints?
`timescale 1ns/1ns
module tb;
  `include "irq_lab_bus.svh"
  localparam bit CLEAR_FIRST = 1;
  localparam bit SHOW_EDGES = 0;
  logic claim_read = 0, complete_write = 0;   // the handler's read and write of the claim and complete word
  logic [4:0] complete_id = 0;                // the ID that a completion writes
  wire [4:0] claim_id;                        // the ID that a claim returns
  wire raw, enabled, pending, in_service, irq; // the records: device flag, device enable, controller, wire
  timer_irq_slice dut (.*);
  always @(posedge clk) begin
    if (SHOW_EDGES && rst_n)
      $display("edge match=%b raw=%b enabled=%b pending=%b irq=%b claim=%b busy=%b clear=%b complete=%b",
               dut.armed && dut.count == dut.DEADLINE, raw, enabled, pending, irq,
               claim_read, in_service, accepted && address == 32'h1000_2014, complete_write);
    if (select && address == 32'h1000_2014) begin
      $display("bus sel=%b access=%b ready=1 data=%h raw_before=%b", select, access_phase, data, raw);
    end
  end
  task automatic report(input string step);
    $display("%s raw=%b en=%b pending=%b busy=%b irq=%b",
             step, raw, enabled, pending, in_service, irq);
  endtask
  initial begin
    repeat (2) @(negedge clk);
    rst_n = 1;
    wait (raw);                       // the timer reaches its count and sets its flag
    report("match");
    write_word(32'h1000_2018, 1);     // software sets the timer's interrupt enable bit
    report("unmask");
    wait (irq);                       // the wire to the processor rises: the handler is entered
    @(negedge clk);
    report("entry");
    // BFM samples the claim word on the accepted register read.
    claim_read = 1;
    @(posedge clk);
    complete_id = claim_id;
    $display("claim id=%0d", complete_id);
    @(negedge clk);
    claim_read = 0;
    report("claimed");
    if (CLEAR_FIRST) begin
      setup_write(32'h1000_2014, 1);  // the handler writes 1 to the timer's clear register
      @(posedge clk);
      #1 report("clear-setup");
      access_write();
      @(posedge clk);
      #1 report("clear-access");
      end_write();
    end
    complete_id = 2;
    complete_write = 1;               // the handler completes with the ID it claimed
    @(negedge clk);
    complete_write = 0;
    report("complete");
    @(negedge clk);
    report("return");
    $finish;
  end
endmodule
Read 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
ConceptOn ap_soc, which three blocks does an external interrupt pass through, in order?
ConceptWhich record does a claim remove?
ConceptIn which order does a handler act?
ConceptA handler completes without clearing the device. What happens next?
ConceptWhich of these is a core-local interrupt on ap_soc?

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.

Practice

Report a problem with this page