← Hardware

Hardware 06 · Interrupt · Linux v6.18.37

RISC-V PLIC와 AIA: source에서 S-mode handler까지

장치 interrupt source가 PLIC gateway, pending, context enable, threshold, claim/complete를 거쳐 supervisor external interrupt로 전달되는 경로를 먼저 확인하고, APLIC·IMSIC 기반 AIA가 무엇을 바꾸는지 비교합니다.

분석 범위
보드에서 Linux까지 8단계
그림
4 boundary diagrams
원본 코드
2 excerpts · 29 lines
기준
Linux v6.18.37

RISC-V 인터럽트를 어디서부터 추적할까요

기대한 supervisor external interrupt 트랩이 발생하지 않을 때 장치 source, PLIC context, hart의 위임 설정을 어떤 순서로 확인할 것인가?

인터럽트 전달과 완료 조건

구분확인할 내용
입력장치의 level/edge interrupt source, 해당 hart의 PLIC context 또는 AIA interrupt file, M-mode의 위임 설정
출력S-mode trap 진입 후 Linux가 hwirq를 claim하고 등록된 driver handler를 실행한 뒤 complete 또는 EOI 처리
소유권장치 원인은 peripheral driver, priority/enable/claim은 PLIC context, privilege delegation은 firmware, virq dispatch는 Linux IRQ core가 소유
완료 조건claim한 source ID를 같은 context의 complete register에 기록하고 장치 원인이 제거되어 gateway가 새 요청을 정상 수용함

인터럽트 소스에서 S-mode까지

그림 1. 단계별 확인 지점
01
1. Device source
START
02
2. Priority and pending
CHECKPOINT
03
3. Context enable
CHECKPOINT
04
4. Hart local interrupt
CHECKPOINT
05
5. Claim and complete
CHECKPOINT
06
6. Firmware description
CHECKPOINT
07
7. Linux domains
CHECKPOINT
08
8. AIA path
LINUX / RESULT
01

1. Device source

PLIC에 들어가는 source가 level인지 edge-latched인지 SoC manual에서 확인합니다. PLIC gateway는 source별 요청을 한 번에 하나만 전달하므로 장치 원인 제거와 complete 순서가 중요합니다.

02

2. Priority and pending

priority 0은 사실상 disabled이며 pending bit는 gateway가 요청을 수락했음을 뜻합니다. pending만 있고 context에 전달되지 않으면 priority와 threshold를 비교합니다.

03

3. Context enable

PLIC context는 hart와 privilege mode의 조합입니다. 같은 hart라도 M-mode context와 S-mode context의 enable bitmap, threshold, claim register 주소가 다릅니다.

04

4. Hart local interrupt

PLIC의 S-mode context가 요청하면 mip.SEIP에 반영됩니다. 위임된 외부 인터럽트는 sie.SEIE가 켜져 있어야 전달됩니다. 현재 S-mode라면 sstatus.SIE도 켜져 있어야 하고, U-mode 실행 중에는 sstatus.SIE를 검사하지 않습니다. 트랩에 진입하면 scause의 Interrupt 비트와 원인 코드 9를 함께 확인합니다.

05

5. Claim and complete

claim read는 가장 높은 priority ID를 반환하고 해당 요청을 servicing 상태로 옮깁니다. handler가 장치 원인을 제거한 뒤 같은 ID를 complete에 써야 gateway가 다음 요청을 받습니다.

06

6. Firmware description

Device Tree의 interrupt-controller, #interrupt-cells, interrupts-extended가 source와 hart context를 연결합니다. SBI 또는 M-mode firmware가 delegation과 hart startup을 준비해야 합니다.

07

7. Linux domains

riscv-intc가 hart-local cause를 처리하고 chained PLIC handler가 claim ID를 PLIC irqdomain으로 넘깁니다. Linux virq는 source ID와 같다고 가정하면 안 됩니다.

08

8. AIA path

AIA에서는 APLIC이 wired source를 direct delivery 또는 MSI로 변환하고 IMSIC이 hart별 interrupt file에서 message를 받습니다. PLIC의 claim/complete와 다른 pending/enable/EOI 모델을 별도로 검증합니다.

PLIC와 AIA의 전달 경로

그림 2. Interrupt 실행 경로왼쪽부터 신호나 데이터를 처리하는 순서를 나타냅니다. 각 단계에서 확인할 항목을 함께 표시했습니다.
01 Device source
02 PLIC gateway
03 pending+priority
04 context enable/threshold
05 SEIP
06 S-mode trap
07 claim
08 driver+complete
그림 3. firmware와 Linux 번호 공간의 연결왼쪽부터 신호나 데이터를 처리하는 순서를 나타냅니다. 각 단계에서 확인할 항목을 함께 표시했습니다.
01 Device source
02 APLIC sourcecfg
03 MSI message
04 IMSIC interrupt file
05 SEIP
06 TOPEI/EOI
07 Linux handler
그림 4. 입력, 소유권, 완료 조건의 경계완료 조건을 충족하기 전에는 다음 계층이 해당 자원을 재사용하면 안 됩니다.
INPUT

장치의 level/edge interrupt source, 해당 hart의 PLIC context 또는 AIA interrupt file, M-mode의 위임 설정

OWNER

장치 원인은 peripheral driver, priority/enable/claim은 PLIC context, privilege delegation은 firmware, virq dispatch는 Linux IRQ core가 소유

OUTPUT

S-mode trap 진입 후 Linux가 hwirq를 claim하고 등록된 driver handler를 실행한 뒤 complete 또는 EOI 처리

DONE

claim한 source ID를 같은 context의 complete register에 기록하고 장치 원인이 제거되어 gateway가 새 요청을 정상 수용함

RISC-V trap, PLIC와 AIA를 단계별로 읽는 상세 매뉴얼

hart의 mip/mie와 trap entry, PLIC gateway·pending·enable·priority·threshold·claim/complete context를 먼저 읽고, AIA의 APLIC wired source와 IMSIC interrupt file/MSI delivery를 별도 구조로 비교합니다.

공개적으로 다시 확인할 수 있는 자료

공개되지 않은 제조사 레지스터나 제품별 오프셋은 추측하지 않았습니다. 아래의 제조사 자료, 아키텍처 문서, Linux v6.18.37 원본으로 확인할 수 있는 내용만 다뤘습니다.

읽어야 할 상태와 해석

같은 동작에 대한 활성화 상태, 처리 상태, 자원 소유권, 완료 레지스터 값을 함께 기록합니다. 읽으면 지워지는 비트나 1을 써야 지워지는 비트가 있는지는 먼저 데이터시트에서 확인합니다.

레지스터/상태소유 블록검증할 의미
SOURCE_PRIORITY[n]PLIC globalsource 우선순위, 0이면 전달 안 됨
PENDING[n]PLIC globalgateway가 접수한 source pending
ENABLE[context][n]PLIC context특정 hart/privilege로 전달 허용
THRESHOLD[context]PLIC contextpriority가 threshold보다 커야 전달
CLAIM/COMPLETE[context]PLIC context가장 높은 ID 수신과 처리 완료 통지
mideleg / sie / sip / sstatusRISC-V CSRSEIP 위임, enable, pending, global mask
APLIC_SOURCECFG / TARGETAIA APLICwired source mode와 hart/EIID target
IMSIC_EIDELIVERY / EITHRESHOLD / EIEAIA IMSIChart-local message delivery와 enable

Linux v6.18.37 원본 코드와 줄별 해설

라인 번호를 고정해 외우는 대신 함수 선언을 기준으로 Linux v6.18.37 tree에서 다시 찾았습니다. 로컬 tree에 있는 파일은 실제 코드를 직접 싣고, 나머지는 원본 파일과 읽을 함수 좌표를 연결했습니다.

Linux v6.18.37 · 실제 원본 코드

PLIC context에서 source ID를 claim하고 irqdomain으로 전달하는 chained handler

drivers/irqchip/irq-sifive-plic.c:383-404

plic_irq_eoi()

RISC-V AIA: Supervisor-level interrupt traps

383static void plic_handle_irq(struct irq_desc *desc)
384{
385	struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
386	struct irq_chip *chip = irq_desc_get_chip(desc);
387	void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
388	irq_hw_number_t hwirq;
389 
390	WARN_ON_ONCE(!handler->present);
391 
392	chained_irq_enter(chip, desc);
393 
394	while ((hwirq = readl(claim))) {
395		int err = generic_handle_domain_irq(handler->priv->irqdomain,
396						    hwirq);
397		if (unlikely(err)) {
398			pr_warn_ratelimited("%pfwP: can't find mapping for hwirq %lu\n",
399					    handler->priv->fwnode, hwirq);
400		}
401	}
402 
403	chained_irq_exit(chip, desc);
404}

Linux v6.18.37 · 실제 원본 코드

scause에서 interrupt 플래그를 분리해 원인 코드를 전달하는 RISC-V 진입 함수

drivers/irqchip/irq-riscv-intc.c:29-35

riscv_intc_aia_irq()

29static void riscv_intc_irq(struct pt_regs *regs)
30{
31	unsigned long cause = regs->cause & ~CAUSE_IRQ_FLAG;
32 
33	if (generic_handle_domain_irq(intc_domain, cause))
34		pr_warn_ratelimited("Failed to handle interrupt (cause: %ld)\n", cause);
35}

Linux v6.18.37 · 원본 좌표

drivers/irqchip/irq-riscv-imsic-platform.c

AIA IMSIC platform device와 MSI irqdomain을 구성하는 Linux 구현

읽을 함수
imsic_platform_probe()
읽을 함수
imsic_irq_domain_alloc()

Linux v6.18.37 원본 파일 열기 →

실제 장비에서 확인하는 순서

  1. Source

    실행 장치 raw status와 interrupt output 조건을 읽고 가능하면 핀 또는 SoC trace를 측정합니다.

    통과 기준 source 원인이 실제로 set되고 mask를 통과해야 합니다.

  2. PLIC global

    실행 source ID의 priority와 pending bit를 사건 전후로 기록합니다.

    통과 기준 priority는 threshold보다 크고 assert 뒤 pending이 보여야 합니다.

  3. Context

    실행 실행 중인 hart/S-mode에 해당하는 enable bitmap, threshold, claim 주소를 확인합니다.

    통과 기준 다른 context를 읽는 실수를 배제하고 claim이 기대 ID를 반환해야 합니다.

  4. CSR

    실행 trap entry에서 scause, sepc, sstatus, sie, sip를 기록합니다.

    통과 기준 interrupt bit와 supervisor external cause가 일치하고 SEIP가 전달되어야 합니다.

  5. Completion

    실행 장치 clear와 complete write 전후로 pending과 재진입 횟수를 비교합니다.

    통과 기준 level source가 deassert되고 같은 ID가 이유 없이 즉시 재claim되지 않아야 합니다.

처음 끊긴 경계로 원인을 좁히기

관측 결과우선 의심할 경계다음 증거
장치 status 없음peripheral clock/reset/configuration 문제장치 raw status
status 있음, PLIC pending 없음source ID/배선/gateway mode 문제SoC interrupt map과 pending bit
pending 있음, SEIP 없음priority 0, enable/threshold, 잘못된 contextPLIC context register
SEIP 있음, S-mode trap 없음mideleg/sie/sstatus 또는 M-mode firmware 문제CSR dump와 firmware 설정
claim 반복 또는 정지장치 clear/complete 순서, 다른 context complete, AIA/PLIC 모델 혼동claim log와 device status

핵심은 마지막 오류 메시지가 아니라 처음 기대값과 달라진 checkpoint입니다. 그보다 아래의 통과 증거와 그 지점의 실패 증거를 한 묶음으로 남겨야 수정의 효과도 검증할 수 있습니다.