Other Interrupt Controllers: GIC, NVIC, CLIC

Your handler worked on one chip. On the next chip the second byte waits for ever. Every interrupt controller does the same job, and they disagree on a few details that a handler depends on.

You Will Be Able To

  • Name the two states that every interrupt controller keeps for a source.
  • Compare how a handler takes and ends an interrupt on a PLIC, a GIC, an NVIC and a CLIC.
  • Check the direction of priority numbers before moving software from one controller to another.
Lesson
S6.5
Time
24 min
Runs on
Your browser
Check
6 questions

You move a UART driver to a new chip. It emptied the UART on the old board, and it empties the same UART today. Yet the second byte never interrupts. What did the old handler do for its controller that the new one still expects?

Before You Start

You need The Interrupt Controller: priority, threshold, claim and completion on ap_plic. You need The Interrupt Path: the device and the controller each keep a record.

This page compares ap_plic with three controllers of other chips. It does not teach any of the three in full.

Five questions this page answers
Question
1Which two states does every interrupt controller keep for a source?
2How does a handler learn which device asked, on each of the four controllers?
3What must a handler do at the end, on each of the four?
4Which way do priority numbers run on ap_plic, and which way on the Arm controllers?
5What does the lab on this page model, and what does it not model?

The Idea: One Job, Four Dialects

Every interrupt controller does the job you saw on ap_plic. It remembers which devices ask, lets the most urgent one through, and waits to be told that the handler is done. Chips of other families do the same job with other controllers.

Three of them have names you will meet. The GIC (Generic Interrupt Controller) serves Arm application processors. The NVIC (Nested Vectored Interrupt Controller) sits inside Arm Cortex-M processors. The CLIC (Core-Local Interrupt Controller) is a RISC-V design for small processors.

Four controllers, the same four questions
QuestionPLICGICNVICCLIC
Where is it found?RISC-V chips, and ap_socArm application processorsArm Cortex-M microcontrollersSmall RISC-V processors
How does the handler learn which device asked?It reads the claim registerIt reads an acknowledge registerNo read: the processor jumps through a table, one entry for each sourceA table entry for each source that is set up that way
What does the handler do at the end?It writes the ID to the completion registerIt writes the ID to an end-of-interrupt registerIt returns. The return is the end.It returns
Which priority number is more urgent?The larger oneThe smaller oneThe smaller oneThe larger one

The PLIC column is ap_plic as built. The other three columns are well-known facts from the public documents of Arm and of RISC-V. Those documents are not in our reference library, so nothing in these columns is checked against them yet.

Read down any one column and it is a complete, sensible design. The trouble comes when you read across. A handler carries the habits of one column to another chip. Two rows cause most of the damage: what to do at the end, and which number is more urgent.

So this page does not ask you to learn three more controllers. It asks you to learn the four questions, and to ask them of any controller before you write its handler.

New Words

New words on this page
WordMeaning
GICGeneric Interrupt Controller: the interrupt controller of Arm application processors. An Arm name.
NVICNested Vectored Interrupt Controller: the interrupt controller inside an Arm Cortex-M processor. An Arm name.
CLICCore-Local Interrupt Controller: a RISC-V design that sits beside one processor. A RISC-V name.
Active stateA source whose interrupt was taken and not yet ended. The Interrupt Path called it in service.
AcknowledgeThe read with which a handler takes an interrupt on a GIC. It returns the ID, as a claim does on a PLIC.
End of interrupt (EOI)The write with which a handler tells a GIC that it is done, as a completion does on a PLIC.
Priority maskA register that holds a limit. A source may interrupt only if its priority is more urgent than the limit.
Vector tableA table of handler addresses, one for each source. The processor jumps through it without asking software.
Tail-chainingGoing from one handler straight into the next, without restoring the interrupted program in between. An Arm word.
PortingMoving software to another chip.

One Picture: Pending and Active

Under the four dialects there is one machine. For each source a controller keeps two states.

Two States for One SourceStep through the picture. It follows two events of one device through the two states that every controller keeps.
  • Peripheral
  • Memory
  • Control / FSM
  • Bus
  • Processor
Two States for One SourceStep through the picture. It follows two events of one device through the two states that every controller keeps. component Device a byte has arrived component Pending remembered, not yettaken component Priority check may this oneinterrupt now? component Active taken, not yet ended component Handler takes, serves, ends eventpending and notactiverequesttake: pending becomesactiveend: active is cleared
  1. component Devicea byte has arrived
    • event → Pending
  2. component Pendingremembered, not yet taken
    • pending and not active → Priority check
  3. component Priority checkmay this one interrupt now?
    • request → Handler
  4. component Handlertakes, serves, ends
    • take: pending becomes active → Active
    • end: active is cleared → Active
  5. component Activetaken, not yet ended
  1. An event is remembered

    A byte arrives. The controller sets pending for that source. The device need not hold its wire up.

  2. The priority check

    A pending source asks only if the priority check lets it through. Until then the event waits. It is not lost.

  3. Taken

    The handler takes the interrupt. Pending is cleared and active is set. From now on this source cannot interrupt again.

  4. A second event during service

    Another byte arrives while the handler runs. Pending is set again, so pending and active are 1 together. New work waits behind the handler.

  5. Ended

    The handler ends the interrupt and active is cleared. The pending event may now ask. Without an end, it never will.

Read the diagram as text
  • Device: a byte has arrived
  • Pending: remembered, not yet taken
  • Priority check: may this one interrupt now?
  • Active: taken, not yet ended
  • Handler: takes, serves, ends
  • Device → Pending: event
  • Pending → Priority check: pending and not active
  • Priority check → Handler: request
  • Handler → Active: take: pending becomes active
  • Handler → Active: end: active is cleared

One source in a generic controller. PLIC, GIC, NVIC and CLIC all keep these two states. They differ in how software takes an interrupt and how it ends one.

The Smallest Example, Worked by Hand

First the direction of the numbers. ap_plic compares a priority with a threshold. The lab of this page compares a priority number with a priority mask, in the way of the Arm controllers. The two run in opposite directions.

One blocked source, two directions
ControllerPriority number of the sourceThe limit register holdsDoes the request pass?
ap_plic5threshold 5no
ap_plic5threshold 4yes: the number must be above the threshold
The lab of this page3mask 3no
The lab of this page3mask 4yes: the number must be below the mask

The two ap_plic rows are recorded in the lab of The Interrupt Controller. The other two are recorded below.

On both, an equal number is blocked. But to let a blocked source through you lower the limit on ap_plic, and raise it here. And on ap_plic the number 7 is the most urgent priority, while on an Arm controller a small number is.

Now the two states. One source, with the mask already open. Fill in each row from the row above it.

Two events of one device, by hand
What happensPendingActiveRequest to the processor
A byte arrives101
The handler takes the interrupt010
A second byte arrives during the handler110
The handler ends the interrupt101

The request is 1 only when the source is pending and not active. In the third row both are 1, and the request is 0. The second byte must wait for the end.

The Rules

  1. Rule 1: Every controller keeps two states for a source: pending and active.

    Both can be 1 at once: new work behind a handler that is still running.

  2. Rule 2: Taking an interrupt moves it from pending to active.

    A claim on a PLIC, an acknowledge on a GIC, the jump itself on an NVIC.

  3. Rule 3: Something must end the active state.

    A completion write on a PLIC, an end-of-interrupt write on a GIC, the return itself on an NVIC.

  4. Rule 4: Serving the device and ending the interrupt are two actions.

    Emptying a device changes the device. It tells the controller nothing.

  5. Rule 5: Priority numbers have a direction, and controllers disagree on it.

    Never copy a priority number from one family to another.

  6. Rule 6: Ask the four questions of any controller before you write its handler.

    Where is it, who names the source, what ends it, which number is urgent.

Sources for These Rules

A device interrupt, a handler and a return: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 9.3.7.

Rules 1 to 4 are shown by the lab of this page, which is our own generic model, and by ap_plic.

PLIC column and PLIC examples: ap_plic as built. See The Interrupt Controller for its sources.

GIC facts on this page are Arm facts. They are the acknowledge read, the end-of-interrupt write, a smaller number being more urgent, and a controller in two parts. Their source is the public Arm GIC architecture specification.

NVIC facts on this page are Arm facts. They are entry through a vector table, the return as the end, a smaller number being more urgent, and tail-chaining. Their source is the public Arm architecture manual for Cortex-M processors.

CLIC facts on this page are RISC-V facts. They are a controller beside each processor, a table entry for chosen sources, and a larger number being more urgent. Their source is the RISC-V CLIC specification, which was still a draft when this page was written.

None of these three documents is in our reference library. The facts are the well-known ones, they are few on purpose, and they are not yet checked against the text.

Now Run It

Be clear about what the lab is. It is a generic model: one source, one pending bit, one active bit and a priority mask. It is not a GIC, an NVIC or a CLIC, and it has none of their registers. Test code, a bus-functional model, plays the handler: it says "take" and "end" on two wires.

The first five rows of the lab on Icarus. Each row is printed just after one rising clock edge.
arrival mask=3 pending=1 request=0 active=0
unmask mask=4 pending=1 request=1 active=0
ack mask=4 pending=0 request=0 active=1
second mask=4 pending=1 request=0 active=1
end mask=4 pending=1 request=1 active=0

Rule 5 at work: in the row "arrival" the mask is 3, the priority number of the source is 3, and request=0. In the row "unmask" the mask is 4 and request=1. The event was not lost in between: pending=1 in both rows.

Rule 2 at work: in the row "ack" pending falls and active rises. Rule 1 at work: in the row "second" pending=1 and active=1 together.

Rule 3 at work: in the row "end" active falls, and request rises at once for the byte that was waiting.

Two Events, One Take and One End
01234clkmask34pendingrequestactive
Scroll horizontally ↔

The same five rows as a waveform. Column 3 is the take and column 5 is the end.

What the lab leaves out is most of a real controller. There is one source, so nothing competes and no ID is needed. There is no processor, no jump and no vector table. The device has no flag of its own.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
The handler reads an ID (PLIC, GIC)The processor jumps through a vector table (NVIC, CLIC)One entry point serves any number of devices, and the controller can sit outside the processor. Each interrupt costs a read before the real work starts.
An explicit write ends the interrupt (PLIC, GIC)The return ends it (NVIC)The controller can be a separate block that many processors share. A handler that forgets the write shuts its source.
A small number is the urgent one (Arm)A large number is the urgent one (PLIC, CLIC)Neither is better. It only has to be known.

The last three rows of the lab belong to one more idea, tail-chaining. It is an Arm Cortex-M fact. Suppose a handler returns and another interrupt is already waiting. The processor does not restore the interrupted program only to save it again. It goes straight into the next handler.

A count kept by a few lines of test code. Is the interrupted program saved now? How often was it saved and restored? It is a count for the idea, not a model of a Cortex-M.
entry frame=1 saves=1 restores=0
handoff frame=1 saves=1 restores=0
resume frame=0 saves=1 restores=1

In the row "handoff" a request is waiting, so nothing is restored: saves=1 and restores=0. Two handlers have run by the row "resume", and the program was saved once and restored once.

The Trap: The Handler That Came from Another Chip

Back to the driver of the hook. It was written for a controller on which the return ends the interrupt. Its handler takes the interrupt, empties the UART and returns. On the new chip the controller waits for an explicit end. Here is that handler on the lab.

The same lab with the step "end" replaced by a plain return: the last three rows.
second mask=4 pending=1 request=0 active=1
return mask=4 pending=1 request=0 active=1
later mask=4 pending=1 request=0 active=1

The row "second" is as before: a new byte, pending=1, active=1. Then the handler returns. In the row "return" active is still 1, and in the row "later" it still is. The request stays 0. The second byte waits for ever, exactly as in the hook.

The device is innocent and the first byte was handled perfectly, which is why this fault survives a quick test. Rule 4 names it: the driver served the device and told the controller nothing.

The other half of the trap is quieter. The old code set the UART to priority 1, because 1 was nearly the most urgent on the old chip. On ap_plic, 1 is the least urgent priority that can interrupt at all. Nothing fails. The UART is just served last, and bytes are lost under load.

At Scale

This page1 source, 2 states
A server chipa thousand sources, dozens of processors

This section looks ahead. It names the parts of the bigger controllers that this page left out. All of it is Arm or RISC-V fact, from their public documents, and is not yet checked against them.

  1. A GIC in two parts

    A GIC has one part for the whole chip, the distributor, which sends each source to a processor. It has another part for each processor, named the CPU interface, where the handler acknowledges and ends. CPU means processor.

  2. Groups

    A GIC sorts its sources into groups that can be enabled one by one. A source that is enabled inside a disabled group stays silent.

  3. Two steps at the end

    A GIC can split the end into two writes: one to let other interrupts through again, one to end the active state. Skip the second and the source is shut.

  4. Interrupts as messages

    On large chips a device can ask by writing to a register, not by a wire. RISC-V has a newer family of controllers that works this way too.

Try It

Run the lab. For each of the first five rows, say which rule set or cleared pending and active.

Change the mask in the step "end" from 4 to 3. Predict the row before you run it.

Delete the step "end". The second byte now waits for ever: this is the trap run of this page.

Names Printed by the Lab
NameMeaning
maskThe priority mask. A source passes only if its priority number is below it. The source here has the number 3.
pendingAn event is remembered and not yet taken.
requestThe wire to the processor.
activeAn interrupt was taken and not yet ended.
frame, saves, restoresA count for tail-chaining: is the interrupted program saved now, and how often was it saved and restored.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: arrival mask=3 pending=1 request=0 active=0 unmask mask=4 pending=1 request=1 active=0 ack mask=4 pending=0 request=0 active=1 second mask=4 pending=1 request=0 active=1 end mask=4 pending=1 request=1 active=0 entry frame=1 saves=1 restores=0 handoff frame=1 saves=1 restores=0 resume frame=0 saves=1 restores=1
Read included file: soc-s411-irq-slot.sv
// One edge-latched UART source. Local model, combined priority drop and retire.
module ap_irq_slot(
  input logic clk, reset, event_pulse, ack, finish,
  input logic [2:0] priority_mask,
  output logic pending, active, request
);
  localparam logic [2:0] UART_PRIORITY = 3;
  assign request = pending && !active && UART_PRIORITY < priority_mask;
  always_ff @(posedge clk) begin
    if (reset) begin
      pending <= 0;
      active <= 0;
    end else begin
      if (finish) active <= 0;
      if (ack && request) begin
        active <= 1;
        pending <= 0;
      end
      if (event_pulse) pending <= 1;
    end
  end
endmodule

Check Yourself

Predict the outputIn this run the handler ends the interrupt, and in the same clock the mask goes back to 3. A second byte is waiting. What is the last row the lab prints?
module tb;
  bit clk, reset;
  bit event_pulse;              // from the device: 1 for one clock when a byte arrives
  bit ack;                      // from the handler: "I take this interrupt"
  bit finish;                   // from the handler: "I have ended this interrupt"
  bit [2:0] priority_mask = 3;  // only a priority number BELOW the mask is let through
  wire pending;                 // an event is remembered and not yet taken
  wire active;                  // an interrupt was taken and not yet ended
  wire request;                 // the wire to the processor
  always #5 clk = !clk;
  ap_irq_slot slot(.*);         // one source, with the fixed priority number 3
  // One clock: drive the inputs, pass one rising edge, print the three outputs.
  task automatic step(input string name, input bit arrival, take, retire,
                      input bit [2:0] mask);
    @(negedge clk);
    event_pulse = arrival; ack = take; finish = retire; priority_mask = mask;
    @(posedge clk); #1;
    $display("%s mask=%0d pending=%b request=%b active=%b",
             name, priority_mask, pending, request, active);
  endtask
  initial begin
    reset = 1; @(posedge clk); @(negedge clk); reset = 0;
    step("arrival", 1,0,0,3);   // a byte arrives; the mask is 3
    step("unmask",  0,0,0,4);   // software raises the mask to 4
    step("ack",     0,1,0,4);   // the handler takes the interrupt
    step("second",  1,0,0,4);   // a second byte arrives during the handler
    step("end",     0,0,1,3);   // the handler ends the interrupt, with the mask at 3 again
    $finish;
  end
endmodule
Read included file: soc-s411-irq-slot.sv
// One edge-latched UART source. Local model, combined priority drop and retire.
module ap_irq_slot(
  input logic clk, reset, event_pulse, ack, finish,
  input logic [2:0] priority_mask,
  output logic pending, active, request
);
  localparam logic [2:0] UART_PRIORITY = 3;
  assign request = pending && !active && UART_PRIORITY < priority_mask;
  always_ff @(posedge clk) begin
    if (reset) begin
      pending <= 0;
      active <= 0;
    end else begin
      if (finish) active <= 0;
      if (ack && request) begin
        active <= 1;
        pending <= 0;
      end
      if (event_pulse) pending <= 1;
    end
  end
endmodule
ConceptWhich two states does every interrupt controller keep for a source?
ConceptOn which of the four does the handler learn the device without reading a register?
ConceptA handler is moved from an NVIC to a PLIC. What is it most likely to be missing?
ConceptA source has the priority number 3. Which setting lets it interrupt?
ConceptWhat is the lab on this page?

Common Mistakes

Wrong Take pending as proof that a handler has started.

A priority check can hold a pending source back.

Fix Look for the take: a claim, an acknowledge or the jump.

Wrong Expect that emptying the device ends the interrupt.

The device and the controller keep separate records.

Fix Serve the device, then end the interrupt in the way of that controller.

Wrong Copy priority numbers from one controller to another.

On some a large number is urgent, on others a small one.

Fix Look up the direction, then choose the numbers again.

Remember

  • Every interrupt controller keeps a source pending until it is taken, and active until it is ended.
  • A PLIC and a GIC give the handler an ID to read and expect a write at the end. An NVIC jumps through a vector table, and the return is the end.
  • Priority numbers run in opposite directions: large is urgent on a PLIC, small is urgent on the Arm controllers.
  • Before porting a handler, ask who names the source, what ends the interrupt, and which number is urgent.

Practice

Report a problem with this page