An interrupt test on a chip under bring-up enables two sources and asks the board for a GPIO edge. The edge comes, and the interrupt never does. Find the first transfer the chip got wrong.
This chip is not the one the other problems use: it is a build under bring-up, and one block of it misbehaves. The firmware is the kit's usual pattern. It ended with fail code 2: no GPIO interrupt within its limit of 3,000 polls.
#include "drivers/uart.h"
#include "drivers/gpio.h"
#include "irq.h"
#include "tb.h"
static volatile unsigned got;
static void on_uart(void) { (void)reg_read32(UART0_RXDATA); got |= 1; }
static void on_gpio(void) { gpio_ack(1u << 8); got |= 2; }
static int wait_for(unsigned bit)
{
for (unsigned t = 0; t < 3000; t++) if (got & bit) return 1;
return 0;
}
int main(void)
{
reg_write32(SYSCTL_PINMUX, 2u << 2);
irq_enable(UART0_IRQ, on_uart); /* ap_plic source 1 */
irq_enable(GPIO_IRQ, on_gpio); /* ap_plic source 2 */
uart_enable_rx_irq();
gpio_rising_irq(1u << 8);
tb_event(5); /* the board pulses IO8 */
if (!wait_for(2)) return 2;
tb_event(3); /* the board sends a byte */
if (!wait_for(1)) return 1;
tb_puts("both interrupts\n");
return 0;
}
The kit's irq_enable(id, handler) writes the handler into its table and 1 into PLIC_PRIORITY(id). Then lw a3, 0(a4) reads PLIC_ENABLE, sll and or set bit id, and sw a5, 0(a4) writes it back. The full specification holds its listing.
The trace below shows 23 of 802 retired loads and stores, cycles 191 to 354. Name the first transfer that went wrong: its cycle, the wrong part, its address and its register or variable. Then choose the cause.
Full specification
80000124 <irq_enable>:
80000124: addi a4,a0,-1
80000128: li a5,15
8000012c: bltu a5,a4,80000134 <irq_enable+0x10>
80000130: bnez a1,80000148 <irq_enable+0x24>
80000134: lui a0,0x10
80000138: addi sp,sp,-16
8000013c: addi a0,a0,-3
80000140: sw ra,12(sp)
80000144: jal 80000270 <tb_fail>
80000148: lui a5,0x80000
8000014c: slli a4,a0,0x2
80000150: addi a5,a5,1008
80000154: add a5,a5,a4
80000158: lui a4,0x3000
8000015c: add a4,a0,a4
80000160: sw a1,0(a5)
80000164: slli a4,a4,0x2
80000168: li a5,1
8000016c: sw a5,0(a4)
80000170: lui a4,0xc002
80000174: lw a3,0(a4)
80000178: sll a5,a5,a0
8000017c: or a5,a5,a3
80000180: sw a5,0(a4)
80000184: lui a5,0x1
80000188: addi a5,a5,-2048
8000018c: csrs mie,a5
80000190: csrsi mstatus,8
80000194: ret
The register list names every address the answer may use. Addresses are in hex without 0x in the trace; write them with or without 0x in the form.
Data in the trace is the word as the bus carried it. A load's data is what came back; a store's is what was written.
Diagnose the Failing Run
Recorded Console and Result
EVENT 311: 00000005 verdict=fail reason=status_fail status=bad00002 cycles=27347 traps=0 interrupts=0 mismatches=0
Retired Memory Transfers
| Cycle | PC (hex) | Operation | Address (hex) | Data (hex) |
|---|---|---|---|---|
| 191 | 8000007c | store | 10004020 | 00000002 |
| 196 | 800002f0 | store | 8000fffc | 80000084 |
| 201 | 800002fc | store | 8000fff8 | 00000000 |
| 204 | 80000304 | store | 10004010 | 00000008 |
| 225 | 80000160 | store | 800003f4 | 800002b8 |
| 230 | 8000016c | store | 0c000004 | 00000001 |
| 232 | 80000174 | load | 0c002000 | 00000000 |
| 235 | 80000180 | store | 0c002000 | 00000002 |
| 267 | 80000160 | store | 800003f8 | 800002d0 |
| 272 | 8000016c | store | 0c000008 | 00000001 |
| 274 | 80000174 | load | 0c002000 | 00000004 |
| 277 | 80000180 | store | 0c002000 | 00000004 |
| 294 | 8000032c | store | 10000008 | 00000002 |
| 297 | 80000334 | store | 1000101c | 00000000 |
| 301 | 80000340 | store | 10001020 | 00000100 |
| 303 | 80000344 | store | 10001024 | 00000100 |
| 305 | 80000348 | store | 10001018 | 00000100 |
| 312 | 800002b0 | store | 10004028 | 00000005 |
| 318 | 8000035c | load | 80000434 | 00000000 |
| 327 | 8000035c | load | 80000434 | 00000000 |
| 336 | 8000035c | load | 80000434 | 00000000 |
| 345 | 8000035c | load | 80000434 | 00000000 |
| 354 | 8000035c | load | 80000434 | 00000000 |
Given Files
Use the register map to identify the transfer in the recording.
tb.h
#ifndef AP_TB_H
#define AP_TB_H
_Noreturn void tb_pass(void);
_Noreturn void tb_fail(unsigned code);
void tb_putc(char byte);
void tb_puts(const char *text);
void tb_put_hex(unsigned value);
void tb_event(unsigned id);
/* Slot zero is the single bench-to-firmware argument word. */
unsigned tb_arg(unsigned n);
void tb_wait_event(unsigned id);
#endif
irq.h
#ifndef AP_IRQ_H
#define AP_IRQ_H
typedef void (*irq_handler)(void);
unsigned plic_claim(void);
void plic_complete(unsigned id);
void irq_enable(unsigned id, irq_handler handler);
unsigned long long mtime_read(void);
void mtime_set_compare(unsigned long long deadline);
void msip_enable(irq_handler handler);
void mtime_enable(irq_handler handler);
#endif
drivers/gpio.h
#ifndef AP_GPIO_H
#define AP_GPIO_H
#include "hal.h"
static inline void gpio_drive(unsigned outputs, unsigned value)
{
reg_write32(GPIO_OUT, value);
reg_write32(GPIO_DIR, outputs);
}
static inline unsigned gpio_read(void)
{
return reg_read32(GPIO_IN);
}
/* Select the rising-edge inputs for this test and discard stale edges. */
static inline void gpio_rising_irq(unsigned pins)
{
reg_write32(GPIO_LEVEL_MODE, 0);
reg_write32(GPIO_ACTIVE_HIGH, pins);
reg_write32(GPIO_STATUS, pins);
reg_write32(GPIO_IRQ_ENABLE, pins);
}
static inline void gpio_ack(unsigned pins)
{
reg_write32(GPIO_STATUS, pins);
}
#endif
ap_plic · register manual
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x004 + 4 × (n − 1), n = 1 to 16 | PRIORITY | 2:0 | level | rw | 0x0 | block | Priority of source n, 0 to 7. 0 never competes. Priority. |
0x001000 | PENDING | 16:1 | sources | ro | block | Pending bits; bit number = source ID. One bit for each source. | |
0x002000 | ENABLE | 16:1 | sources | rw | 0x0000 | block | Enable bits; bit number = source ID. One bit for each source. |
0x200000 | THRESHOLD | 2:0 | level | rw | 0x0 | block | The core sees a request only above this priority. Threshold. |
0x200004 | CLAIM | 4:0 | id | rw | 0x00 | block | Read: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID. |
## ap_plic (base 0x0C00_0000, clk_sys, PD_MAIN) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x004` + 4 × (n − 1), n = 1 to 16 | PRIORITY | 2:0 | level | rw | `0x0` | block | Priority of source n, 0 to 7. 0 never competes. Priority. | | `0x001000` | PENDING | 16:1 | sources | ro | | block | Pending bits; bit number = source ID. One bit for each source. | | `0x002000` | ENABLE | 16:1 | sources | rw | `0x0000` | block | Enable bits; bit number = source ID. One bit for each source. | | `0x200000` | THRESHOLD | 2:0 | level | rw | `0x0` | block | The core sees a request only above this priority. Threshold. | | `0x200004` | CLAIM | 4:0 | id | rw | `0x00` | block | Read: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID. |