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.
| Question | |
|---|---|
| 1 | Which two states does every interrupt controller keep for a source? |
| 2 | How does a handler learn which device asked, on each of the four controllers? |
| 3 | What must a handler do at the end, on each of the four? |
| 4 | Which way do priority numbers run on ap_plic, and which way on the Arm controllers? |
| 5 | What 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.
| Question | PLIC | GIC | NVIC | CLIC |
|---|---|---|---|---|
| Where is it found? | RISC-V chips, and ap_soc | Arm application processors | Arm Cortex-M microcontrollers | Small RISC-V processors |
| How does the handler learn which device asked? | It reads the claim register | It reads an acknowledge register | No read: the processor jumps through a table, one entry for each source | A 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 register | It writes the ID to an end-of-interrupt register | It returns. The return is the end. | It returns |
| Which priority number is more urgent? | The larger one | The smaller one | The smaller one | The 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
| Word | Meaning |
|---|---|
| GIC | Generic Interrupt Controller: the interrupt controller of Arm application processors. An Arm name. |
| NVIC | Nested Vectored Interrupt Controller: the interrupt controller inside an Arm Cortex-M processor. An Arm name. |
| CLIC | Core-Local Interrupt Controller: a RISC-V design that sits beside one processor. A RISC-V name. |
| Active state | A source whose interrupt was taken and not yet ended. The Interrupt Path called it in service. |
| Acknowledge | The 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 mask | A register that holds a limit. A source may interrupt only if its priority is more urgent than the limit. |
| Vector table | A table of handler addresses, one for each source. The processor jumps through it without asking software. |
| Tail-chaining | Going from one handler straight into the next, without restoring the interrupted program in between. An Arm word. |
| Porting | Moving 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.
- Peripheral
- Memory
- Control / FSM
- Bus
- Processor
- An event is remembered
A byte arrives. The controller sets pending for that source. The device need not hold its wire up.
- The priority check
A pending source asks only if the priority check lets it through. Until then the event waits. It is not lost.
- Taken
The handler takes the interrupt. Pending is cleared and active is set. From now on this source cannot interrupt again.
- 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.
- 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.
| Controller | Priority number of the source | The limit register holds | Does the request pass? |
|---|---|---|---|
| ap_plic | 5 | threshold 5 | no |
| ap_plic | 5 | threshold 4 | yes: the number must be above the threshold |
| The lab of this page | 3 | mask 3 | no |
| The lab of this page | 3 | mask 4 | yes: 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.
| What happens | Pending | Active | Request to the processor |
|---|---|---|---|
| A byte arrives | 1 | 0 | 1 |
| The handler takes the interrupt | 0 | 1 | 0 |
| A second byte arrives during the handler | 1 | 1 | 0 |
| The handler ends the interrupt | 1 | 0 | 1 |
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
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.
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.
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.
Rule 4: Serving the device and ending the interrupt are two actions.
Emptying a device changes the device. It tells the controller nothing.
Rule 5: Priority numbers have a direction, and controllers disagree on it.
Never copy a priority number from one family to another.
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.
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=0Rule 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.
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 choice | The alternative | What 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.
entry frame=1 saves=1 restores=0
handoff frame=1 saves=1 restores=0
resume frame=0 saves=1 restores=1In 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.
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=1The 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 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.
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.
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.
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.
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
| Name | Meaning |
|---|---|
| mask | The priority mask. A source passes only if its priority number is below it. The source here has the number 3. |
| pending | An event is remembered and not yet taken. |
| request | The wire to the processor. |
| active | An interrupt was taken and not yet ended. |
| frame, saves, restores | A count for tail-chaining: is the interrupted program saved now, and how often was it saved and restored. |
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=1Read 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
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.