Interrupt Tests End to End
An interrupt crosses four places that four teams built: the device, the interrupt map, ap_plic and the core. A chip test proves the whole journey for every source, handles edge and level requests correctly, and keeps its handlers honest.
You Will Be Able To
- Trace an interrupt from its device to the completion that ends it.
- Prove that every source reaches the handler of its own number.
- Tell an edge source from a level source and write the handler each needs.
- Turn an interrupt that never comes into a fail code.
- Lesson
- V4.7
- Time
- 28 min
- Runs on
- Your browser
- Check
- 5 questions
Every block raises its interrupt in its own bench. The core takes interrupts in its own bench. On the chip, the timer's interrupt arrives as the DMA's, and a handler that only counts interrupts is perfectly happy.
- Pads
- Peripheral
- Processor
- Data
- Interrupt
- Source
The GPIO block latches a rising edge on pin 8 and raises its request.
- ap_plic
Source 2 is pending. If it is enabled and its priority is above the threshold, ap_plic asks the core.
- Core
With machine interrupts enabled, the core traps to the kit's entry stub.
- Handler
The dispatcher claims source 2, calls its handler, which clears the device's condition, and completes the claim.
- Back to main
mret returns to the interrupted instruction. One interrupt, one handler, one completion.
Read the diagram as text
- Pad IO8: the board pulses it
- GPIO: edge latched in STATUS
- ap_plic: source 2: pending, priority, enable
- Core: mie, mstatus: trap to mtvec
- Handler: claim, clear the device, complete
- main: resumes where it was
- Pad IO8 → GPIO: edge
- GPIO → ap_plic: request
- ap_plic → Core: external interrupt
- Core → Handler: trap
- Handler → main: mret
ap_soc v1.5: GPIO is ap_plic source 2 (irq.csv). The lab below raises every device source.
Before You Start
Read Interrupt Handlers in C and The Interrupt Path first. Interrupt Registers in a Peripheral explains level, edge and write-1-to-clear.
| Question | |
|---|---|
| 1 | Why does a chip need interrupt tests when every block tested its own? |
| 2 | Which places does one interrupt cross? |
| 3 | How does a test prove that a source reached its own handler? |
| 4 | What must a handler do for a level source that it need not do for an edge? |
| 5 | What happens to a test when an interrupt never comes? |
The Idea: The Journey Belongs to the Chip
A device raises a request. The interrupt map wires it to an input of ap_plic. ap_plic decides by enable, priority and threshold whether to ask the core. The core traps, and a handler claims the source, clears the device and completes. Each piece was built and tested by someone else; the wiring between them exists only on the chip.
An interrupt test proves the journey. For every source it makes the device ask, and it checks that the right handler ran, once. "An interrupt came" is not enough. It must be this source's interrupt, under this source's number.
It also proves the handler's side of the work. An edge request is latched once and cleared by a write. A level request is the device's own state, and it asks until the cause is gone. Handlers that get this wrong storm, or lose requests.
New Words
| Word | Meaning |
|---|---|
| Source number | The input of ap_plic a device is wired to (irq.csv). UART0 1, GPIO 2, timer 3, SPI 4, DMA 5, ap_pwrctl 9. |
| Claim | A read of ap_plic's CLAIM register: the winning source, which stops it asking. |
| Complete | A write of the claimed number back to CLAIM: the source may ask again. |
| Edge source | A request latched on a change of a pin, cleared by writing its status bit. |
| Level source | A request that lasts as long as its cause, such as a pin held high. |
| Interrupt storm | The same request taken again and again, because the handler never removes its cause. |
The Smallest Example: Map and Prove by Hand
irq.csv is the chip's interrupt map. Read it, then write down what a test must see for each source.
| Source | Device | How a test raises it | The handler must |
|---|---|---|---|
| 1 | UART0 | The board sends a byte | read RXDATA |
| 2 | GPIO | The board pulses IO8 | write 1 to STATUS bit 8 |
| 3 | Timer and PWM | A compare match | stop the timer, clear the match |
| 4 | SPI | A frame ends | write 1 to STATUS bit 1 |
| 5 | DMA | A copy ends | write 1 to STATUS bit 1 |
| 9 | ap_pwrctl | A software wake of PD_PER | write 1 to the WAKE_CAUSE bits |
Source numbers from tools/soc/ap_soc/irq.csv; registers from the chip's manual.
The Rules
Rule 1: Every source, one at a time.
Raise each device's request alone, so a crossed wire cannot hide.
Rule 2: Each handler says who it is.
The test checks which handler ran, not only that one did.
Rule 3: Clear the cause before you complete.
For a level source, remove or mask the cause; a write to STATUS does not end a level.
Rule 4: Every wait for an interrupt has a limit.
An interrupt that never comes must end the test with a code.
Rule 5: A handler that ran without cause is a failure.
Report it at once, from the handler; main may never run again.
Sources for These Rules
The interrupt map: tools/soc/ap_soc/irq.csv. ap_plic's registers: the generated header (PLIC_PRIORITY, PENDING, ENABLE, THRESHOLD, CLAIM). The kit's dispatcher claims once and completes the same number: tools/soc/fwkit/irq.c.
Claim and completion: the RISC-V PLIC specification (library key RV-PLIC), chapters 8 and 9. A claimed source is not forwarded again until its completion: section 1.5. A smaller ID wins a tie of priorities: section 1.4. ap_plic is a small PLIC of our design. Level and edge: the chip's GPIO registers.
The kit's dispatcher handles interrupt causes 7 (timer) and 11 (external). Any other interrupt becomes fail code 0xFFFF: tools/soc/fwkit/irq.c.
The fault in the lab is a seeded fault of stage E9: docs/soc/fault-table.md.
Now Run It: Six Sources, Two Chips
The lab raises each device source in turn and prints which handler ran. UART0 asks for a byte from the board, GPIO for a pulse. The timer asks on a match, SPI and the DMA when their work ends, and ap_pwrctl on a software wake.
EVENT 490: 00000003
CONSOLE 1074: handler 00000001
EVENT 1095: 00000005
CONSOLE 1528: handler 00000002
CONSOLE 2028: handler 00000003
CONSOLE 2594: handler 00000004
CONSOLE 3063: handler 00000005
CONSOLE 3593: handler 00000009
interrupts=6 irq_external=6 ext_uart=1 ext_gpio=1 ext_timer=1Six interrupts, six handlers, each the one of its source: rules 1 and 2. Then the same image ran on a chip with one seeded fault. Its interrupt map gives input 3 the DMA's request instead of the timer's.
EVENT 490: 00000003
CONSOLE 1074: handler 00000001
EVENT 1095: 00000005
CONSOLE 1528: handler 00000002
CONSOLE 17784: handler 00000002
CONSOLE 34214: handler 00000004
reason=cycle_limit interrupts=1939The timer's request went nowhere, so the wait for it ran out and printed the handler that ran last, 2. Later the DMA's request reached input 3 and input 5 at once. The timer's handler cleared the timer, not the DMA, and input 3 asked again: 1939 interrupts until the cycle limit. Rule 5 would have ended that run at the first wrong handler.
Level against edge, on GPIO pin 8 held high by the board. In level mode, a handler that only writes STATUS was taken 164 times until the cycle limit: rule 3. A handler that disabled the pin was taken once, and so was the pin in edge mode.
static volatile unsigned taken;
static void on_pin(void)
{
gpio_ack(1u << 8); /* the pad is still high: the request stays */
taken = taken + 1;
}Why It Is Built This Way
A test could raise all sources at once and count to six. It would pass on a chip with two crossed wires, because six interrupts still arrive. One source at a time, with handlers that name themselves, costs a few hundred cycles more and leaves no room for that.
The kit's dispatcher claims once, calls the handler of the claimed number and completes it. That keeps the claim and the completion right in every test. The test's own handlers only clear their device and record what happened.
The order inside a handler is the claim-and-complete discipline. The claim clears the source's pending bit, and its gateway forwards nothing more from that source until the completion. So the device must be quiet before the completion is written. Complete a level source while its cause is still there, and it asks again at once: the storm in the lab. Forget the completion, and the source goes silent for the rest of the test.
Know what the lab test leaves out. It raises one source at a time, with every priority at 1 and the threshold at 0. So it never shows which of two pending sources is claimed first, or that a source at the threshold must wait. A complete interrupt test checks both. This chip's seeded faults include none that only those checks would find. So its graded problem cannot demand them, though the reference makes them.
The kit has a gap here too. It gives the machine timer and external interrupts a handler, but not the software interrupt of ap_clint (MSIP). A test that enables that interrupt ends with fail code 0xFFFF when it arrives, so this module does not test it.
The Trap: The Handler That Counted Everything
A first interrupt test has one counter. Every handler adds one to it, and main waits until it reaches the number of sources. On a chip with a crossed wire it passes: the wrong handler ran, and it counted. On a chip with a missing wire it hangs, and the report says "cycle limit".
The cure is two habits: each handler records its own source, and each wait has a limit and a code. The practice problems build both. One shares a GPIO source between an edge and a level; one faces a hidden set of faulty chips.
At Scale
This section looks ahead. ap_soc has six device sources and one core.
Hundreds of sources
Large chips have hundreds; tests are generated from the interrupt map, one per source, like the register sweeps of module V2.
Many cores
An interrupt can be routed to several cores; tests prove the routing table and that only one core claims.
Other controllers
Arm GIC and vectored controllers differ in detail, but the journey and the rules are the same.
Try It
Run the slice: it checks each raised source against irq.csv and the handler that ran.
Swap two handlers in the list. Which lines turn WRONG?
The predict question below feeds it the first three sources of the faulty chip.
raised 1 (UART0): handler 1, right raised 2 (GPIO): handler 2, right raised 3 (Timer and PWM): handler 3, right raised 4 (SPI): handler 4, right raised 5 (DMA): handler 5, right raised 9 (ap_pwrctl): handler 9, rightCheck Yourself
Common Mistakes
Wrong Count interrupts with one shared counter.
A crossed wire still delivers the right number of interrupts.
Fix Let each handler record its own source, and check it.
Wrong Clear a level request by writing its status bit.
The cause is still there, so the request comes straight back.
Fix Remove the cause, or mask the source until it is gone.
Wrong Wait for an interrupt with no limit.
A missing wire becomes a hang.
Fix Wait with a limit and fail with the source's code.
Remember
- An interrupt crosses the device, the map, ap_plic and the core: the journey is the chip's.
- Raise each source alone and check that its own handler ran.
- Edge requests are cleared by a write; level requests by removing their cause.
- Every wait for an interrupt has a limit; a handler without cause fails the test.