QEMU RISC-V AIA support
Subsystems (2)
v6 cover QEMU RISC-V AIA support
From: Anup Patel <[email protected]>
The advanced interrupt architecture (AIA) extends the per-HART local interrupt support. Along with this, it also adds IMSIC (MSI contrllor) and Advanced PLIC (wired interrupt controller).
The latest AIA draft specification can be found here:
https://github.com/riscv/riscv-aia/releases/download/0.2-draft.28/riscv-interrupts-028.pdf
This series adds RISC-V AIA support in QEMU which includes emulating all AIA local CSRs, APLIC, and IMSIC. Only AIA local interrupt filtering is not implemented because we don't have any local interrupt greater than 12.
To enable AIA in QEMU, use one of the following:
1) Only AIA local interrupt CSRs: Pass "x-aia=true" as CPU paramenter in the QEMU command-line
2) Only APLIC for virt machine: Pass "aia=aplic" as machine parameter in the QEMU command-line
3) Both APLIC and IMSIC for virt machine: Pass "aia=aplic-imsic" as machine parameter in the QEMU command-line
4) Both APLIC and IMSIC with 2 guest files for virt machine: Pass "aia=aplic-imsic,aia-guests=2" as machine parameter in the QEMU command-line
To test series, we require OpenSBI and Linux with AIA support which can be found in riscv_aia_v1 branch at:
https://github.com/avpatel/opensbi.git
https://github.com/avpatel/linux.git
This series can be found riscv_aia_v6 branch at:
https://github.com/avpatel/qemu.git
Changes since v5:
- Moved VSTOPI_NUM_SRCS define to top of the file in PATCH13
- Fixed typo in PATCH16
Changes since v4:
- Changed IRQ_LOCAL_MAX to 16 in PATCH2
- Fixed typo in PATCH10
- Replaced TARGET_LONG_BITS with riscv_cpu_mxl_bits(env) in PATCH11
- Replaced TARGET_LONG_BITS with riscv_cpu_mxl_bits(env) in PATCH14
- Replaced TARGET_LONG_BITS with riscv_cpu_mxl_bits(env) in PATCH15
- Replaced TARGET_LONG_BITS with xlen passed via ireg callback in PATCH20
- Retrict maximum IMSIC guest files per-HART of virt machine to 7 in PATCH21.
- Added separate PATCH23 to increase maximum number of allowed CPUs for virt machine
Changes since v3:
- Replaced "aplic,xyz" and "imsic,xyz" DT properties with "riscv,xyz" DT properties because "aplic" and "imsic" are not valid vendor names required by Linux DT schema checker.
Changes since v2:
- Update PATCH4 to check and inject interrupt after V=1 when transitioning from V=0 to V=1
Changes since v1:
- Revamped whole series and created more granular patches
- Added HGEIE and HGEIP CSR emulation for H-extension
- Added APLIC emulation
- Added IMSIC emulation
Anup Patel (23):
target/riscv: Fix trap cause for RV32 HS-mode CSR access from RV64
HS-mode
target/riscv: Implement SGEIP bit in hip and hie CSRs target/riscv: Implement hgeie and hgeip CSRs
target/riscv: Improve delivery of guest external interrupts target/riscv: Allow setting CPU feature from machine/device emulation target/riscv: Add AIA cpu feature
target/riscv: Add defines for AIA CSRs
target/riscv: Allow AIA device emulation to set ireg rmw callback target/riscv: Implement AIA local interrupt priorities target/riscv: Implement AIA CSRs for 64 local interrupts on RV32 target/riscv: Implement AIA hvictl and hviprioX CSRs target/riscv: Implement AIA interrupt filtering CSRs target/riscv: Implement AIA mtopi, stopi, and vstopi CSRs target/riscv: Implement AIA xiselect and xireg CSRs target/riscv: Implement AIA IMSIC interface CSRs
hw/riscv: virt: Use AIA INTC compatible string when available target/riscv: Allow users to force enable AIA CSRs in HART hw/intc: Add RISC-V AIA APLIC device emulation
hw/riscv: virt: Add optional AIA APLIC support to virt machine hw/intc: Add RISC-V AIA IMSIC device emulation
hw/riscv: virt: Add optional AIA IMSIC support to virt machine docs/system: riscv: Document AIA options for virt machine hw/riscv: virt: Increase maximum number of allowed CPUs
docs/system/riscv/virt.rst | 16 + hw/intc/Kconfig | 6 + hw/intc/meson.build | 2 + hw/intc/riscv_aplic.c | 970 +++++++++++++++++++++++++ hw/intc/riscv_imsic.c | 447 ++++++++++++ hw/riscv/Kconfig | 2 + hw/riscv/virt.c | 708 +++++++++++++++--- include/hw/intc/riscv_aplic.h | 79 ++ include/hw/intc/riscv_imsic.h | 68 ++ include/hw/riscv/virt.h | 42 +- target/riscv/cpu.c | 97 ++- target/riscv/cpu.h | 72 +- target/riscv/cpu_bits.h | 131 ++++ target/riscv/cpu_helper.c | 293 +++++++- target/riscv/csr.c | 1279 ++++++++++++++++++++++++++++++--- target/riscv/machine.c | 24 +- 16 files changed, 3938 insertions(+), 298 deletions(-) create mode 100644 hw/intc/riscv_aplic.c create mode 100644 hw/intc/riscv_imsic.c create mode 100644 include/hw/intc/riscv_aplic.h create mode 100644 include/hw/intc/riscv_imsic.h
Signature · 1 line
2.25.1
PATCH v6 1/23 target/riscv: Fix trap cause for RV32 HS-mode CSR access from RV64 HS-mode
From: Anup Patel <[email protected]>
We should be returning illegal instruction trap when RV64 HS-mode tries to access RV32 HS-mode CSR.
--- target/riscv/csr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -1
@@ -182,7 +182,7 @@ static RISCVException hmode(CPURISCVState *env, int csrno)static RISCVException hmode32 (CPURISCVState *env,int csrno) {if (riscv_cpu_mxl (env) !=MXL_RV32 ) { -if (riscv_cpu_virt_enabled (env)) { +if (!riscv_cpu_virt_enabled (env)) {return RISCV_EXCP_ILLEGAL_INST ; }else {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;
Signature · 1 line
2.25.1
Re: [PATCH v6 01/23] target/riscv: Fix trap cause for RV32 HS-mode CSR access from RV64 HS-mode
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:37寫道:
Quoted 28 lines
From: Anup Patel <[email protected]>
We should be returning illegal instruction trap when RV64 HS-mode tries to access RV32 HS-mode CSR.
Fixes d6f20dacea51 ("target/riscv: Fix 32-bit HS mode access permissions")Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>Reviewed-by Bin Meng <[email protected]>--- target/riscv/csr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/csr.c +1
-1@@ -182,7 +182,7 @@ static RISCVException hmode(CPURISCVState *env, int csrno) static RISCVException hmode32(CPURISCVState *env, int csrno) {if (riscv_cpu_mxl (env) !=MXL_RV32 ) { -if (riscv_cpu_virt_enabled (env)) { +if (!riscv_cpu_virt_enabled (env)) {return RISCV_EXCP_ILLEGAL_INST ; }else {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
Quoted 5 lines
--
2.25.1
PATCH v6 2/23 target/riscv: Implement SGEIP bit in hip and hie CSRs
In reply to Anup PatelFrom: Anup Patel <[email protected]>
A hypervisor can optionally take guest external interrupts using SGEIP bit of hip and hie CSRs.
--- target/riscv/cpu.c | 3 ++- target/riscv/cpu_bits.h | 3 +++ target/riscv/csr.c | 18 +++++++++++------- 3 files changed, 16 insertions(+), 8 deletions(-)
target/riscv/cpu.c +2 -1
@@ -372,6 +372,7 @@ static void riscv_cpu_reset(DeviceState *dev) env->mstatus =set_field (env->mstatus,MSTATUS64_UXL , env->misa_mxl); } env->mcause =0 ; + env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false;/* mmte is supposed to have pm.current hardwired to 1 */
@@ -610,7 +611,7 @@ static void riscv_cpu_init(Object *obj)cpu_set_cpustate_pointers (cpu);#ifndef CONFIG_USER_ONLY -qdev_init_gpio_in (DEVICE (cpu), riscv_cpu_set_irq,12 ); +qdev_init_gpio_in (DEVICE (cpu), riscv_cpu_set_irq,IRQ_LOCAL_MAX );#endif /* CONFIG_USER_ONLY */ }
target/riscv/cpu_bits.h +3 -0
@@ -534,6 +534,8 @@ typedef enum RISCVException {#define IRQ_S_EXT 9#define IRQ_VS_EXT 10#define IRQ_M_EXT 11 +#define IRQ_S_GEXT 12 +#define IRQ_LOCAL_MAX 16/* mip masks */ #define MIP_USIP (1 << IRQ_U_SOFT)
@@ -548,6 +550,7 @@ typedef enum RISCVException {#define MIP_SEIP (1 << IRQ_S_EXT)#define MIP_VSEIP (1 << IRQ_VS_EXT)#define MIP_MEIP (1 << IRQ_M_EXT) +#define MIP_SGEIP (1 << IRQ_S_GEXT)/* sip masks */ #define SIP_SSIP MIP_SSIP
target/riscv/csr.c +11 -7
@@ -446,12 +446,13 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,#define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP)#define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP)#define VS_MODE_INTERRUPTS (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP) +#define HS_MODE_INTERRUPTS (MIP_SGEIP | VS_MODE_INTERRUPTS)static const target_ulong delegable_ints =S_MODE_INTERRUPTS |VS_MODE_INTERRUPTS ;static const target_ulong vs_delegable_ints =VS_MODE_INTERRUPTS ;static const target_ulong all_ints =M_MODE_INTERRUPTS |S_MODE_INTERRUPTS | -VS_MODE_INTERRUPTS ; +HS_MODE_INTERRUPTS ;#define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \ (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \ (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | \
@@ -712,7 +713,7 @@ static RISCVException write_mideleg(CPURISCVState *env, int csrno, { env->mideleg = (env->mideleg & ~delegable_ints) | (val & delegable_ints);if (riscv_has_ext (env,RVH )) { - env->mideleg |=VS_MODE_INTERRUPTS ; + env->mideleg |=HS_MODE_INTERRUPTS ; }return RISCV_EXCP_NONE ; }
@@ -728,6 +729,9 @@ static RISCVException write_mie(CPURISCVState *env, int csrno, target_ulong val) { env->mie = (env->mie & ~all_ints) | (val & all_ints); +if (!riscv_has_ext (env,RVH )) { + env->mie &= ~MIP_SGEIP ; + }return RISCV_EXCP_NONE ; }
@@ -1023,7 +1027,7 @@ static RISCVException rmw_sip(CPURISCVState *env, int csrno, }if (ret_value) { - *ret_value &= env->mideleg; + *ret_value &= env->mideleg &S_MODE_INTERRUPTS ; }return ret; }
@@ -1141,7 +1145,7 @@ static RISCVException rmw_hvip(CPURISCVState *env, int csrno, write_mask & hvip_writable_mask);if (ret_value) { - *ret_value &= hvip_writable_mask; + *ret_value &=VS_MODE_INTERRUPTS ; }return ret; }
@@ -1154,7 +1158,7 @@ static RISCVException rmw_hip(CPURISCVState *env, int csrno, write_mask & hip_writable_mask);if (ret_value) { - *ret_value &= hip_writable_mask; + *ret_value &=HS_MODE_INTERRUPTS ; }return ret; }
@@ -1162,14 +1166,14 @@ static RISCVException rmw_hip(CPURISCVState *env, int csrno,static RISCVException read_hie (CPURISCVState *env,int csrno,target_ulong *val) { - *val = env->mie &VS_MODE_INTERRUPTS ; + *val = env->mie &HS_MODE_INTERRUPTS ;return RISCV_EXCP_NONE ; }static RISCVException write_hie (CPURISCVState *env,int csrno,target_ulong val) { -target_ulong newval = (env->mie & ~VS_MODE_INTERRUPTS ) | (val &VS_MODE_INTERRUPTS ); +target_ulong newval = (env->mie & ~HS_MODE_INTERRUPTS ) | (val &HS_MODE_INTERRUPTS );return write_mie (env,CSR_MIE , newval); }
Signature · 1 line
2.25.1
Re: [PATCH v6 02/23] target/riscv: Implement SGEIP bit in hip and hie CSRs
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:36寫道:
Quoted 148 lines
From: Anup Patel <[email protected]>
A hypervisor can optionally take guest external interrupts using SGEIP bit of hip and hie CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 3 ++- target/riscv/cpu_bits.h | 3 +++ target/riscv/csr.c | 18 +++++++++++------- 3 files changed, 16 insertions(+), 8 deletions(-)target/riscv/cpu.c +1
-0@@ -372,6 +372,7 @@ static void riscv_cpu_reset(DeviceState *dev) env->mstatus =set_field (env->mstatus,MSTATUS64_UXL , env->misa_mxl); } env->mcause =0 ; + env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false;/* mmte is supposed to have pm.current hardwired to 1 */target/riscv/cpu.c +1
-1@@ -610,7 +611,7 @@ static void riscv_cpu_init(Object *obj)cpu_set_cpustate_pointers (cpu);#ifndef CONFIG_USER_ONLY -qdev_init_gpio_in (DEVICE (cpu), riscv_cpu_set_irq,12 ); +qdev_init_gpio_in (DEVICE (cpu), riscv_cpu_set_irq,IRQ_LOCAL_MAX );#endif /* CONFIG_USER_ONLY */ }target/riscv/cpu_bits.h +3
-0@@ -534,6 +534,8 @@ typedef enum RISCVException {#define IRQ_S_EXT 9#define IRQ_VS_EXT 10#define IRQ_M_EXT 11 +#define IRQ_S_GEXT 12 +#define IRQ_LOCAL_MAX 16/* mip masks */ #define MIP_USIP (1 << IRQ_U_SOFT)@@ -548,6 +550,7 @@ typedef enum RISCVException {#define MIP_SEIP (1 << IRQ_S_EXT)#define MIP_VSEIP (1 << IRQ_VS_EXT)#define MIP_MEIP (1 << IRQ_M_EXT) +#define MIP_SGEIP (1 << IRQ_S_GEXT)/* sip masks */ #define SIP_SSIP MIP_SSIPtarget/riscv/csr.c +1
-0@@ -446,12 +446,13 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,#define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP)#define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP)#define VS_MODE_INTERRUPTS (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP) +#define HS_MODE_INTERRUPTS (MIP_SGEIP | VS_MODE_INTERRUPTS)static const target_ulong delegable_ints =S_MODE_INTERRUPTS |VS_MODE_INTERRUPTS ;static const target_ulong vs_delegable_ints =VS_MODE_INTERRUPTS ; static const target_ulong all_ints = M_MODE_INTERRUPTS |
S_MODE_INTERRUPTS|target/riscv/csr.c +2
-2-VS_MODE_INTERRUPTS ; +HS_MODE_INTERRUPTS ;#define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \ (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \ (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | \@@ -712,7 +713,7 @@ static RISCVException write_mideleg(CPURISCVState *env, int csrno, { env->mideleg = (env->mideleg & ~delegable_ints) | (val & delegable_ints);if (riscv_has_ext (env,RVH )) { - env->mideleg |=VS_MODE_INTERRUPTS ; + env->mideleg |=HS_MODE_INTERRUPTS ; }return RISCV_EXCP_NONE;}
target/riscv/csr.c +4
-1@@ -728,6 +729,9 @@ static RISCVException write_mie(CPURISCVState *env, int csrno, target_ulong val) { env->mie = (env->mie & ~all_ints) | (val & all_ints); +if (!riscv_has_ext (env,RVH )) { + env->mie &= ~MIP_SGEIP ; + }return RISCV_EXCP_NONE ; }@@ -1023,7 +1027,7 @@ static RISCVException rmw_sip(CPURISCVState *env, int csrno, }if (ret_value) { - *ret_value &= env->mideleg; + *ret_value &= env->mideleg &S_MODE_INTERRUPTS ; }return ret;}
target/riscv/csr.c +1
-1@@ -1141,7 +1145,7 @@ static RISCVException rmw_hvip(CPURISCVState *env,int csrno, write_mask & hvip_writable_mask);if (ret_value) { - *ret_value &= hvip_writable_mask; + *ret_value &=VS_MODE_INTERRUPTS ; }return ret;}
target/riscv/csr.c +1
-1@@ -1154,7 +1158,7 @@ static RISCVException rmw_hip(CPURISCVState *env,int csrno, write_mask & hip_writable_mask);if (ret_value) { - *ret_value &= hip_writable_mask; + *ret_value &=HS_MODE_INTERRUPTS ; }return ret;}
target/riscv/csr.c +2
-2@@ -1162,14 +1166,14 @@ static RISCVException rmw_hip(CPURISCVState *env,int csrno, static RISCVException read_hie(CPURISCVState *env, int csrno, target_ulong *val) { - *val = env->mie &VS_MODE_INTERRUPTS ; + *val = env->mie &HS_MODE_INTERRUPTS ;return RISCV_EXCP_NONE ; }static RISCVException write_hie (CPURISCVState *env,int csrno,target_ulong val) { -target_ulong newval = (env->mie & ~VS_MODE_INTERRUPTS ) | (val & VS_MODE_INTERRUPTS); +target_ulong newval = (env->mie & ~HS_MODE_INTERRUPTS ) | (val &HS_MODE_INTERRUPTS );return write_mie(env, CSR_MIE, newval);
Quoted 7 lines
}
--
2.25.1
PATCH v6 3/23 target/riscv: Implement hgeie and hgeip CSRs
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The hgeie and hgeip CSRs are required for emulating an external interrupt controller capable of injecting virtual external interrupt to Guest/VM running at VS-level.
--- target/riscv/cpu.c | 61 ++++++++++++++++++++++++++++----------- target/riscv/cpu.h | 5 ++++ target/riscv/cpu_bits.h | 1 + target/riscv/cpu_helper.c | 37 ++++++++++++++++++++++-- target/riscv/csr.c | 43 ++++++++++++++++++--------- target/riscv/machine.c | 6 ++-- 6 files changed, 118 insertions(+), 35 deletions(-)
target/riscv/cpu.c +44 -17
@@ -582,23 +582,49 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)static void riscv_cpu_set_irq (void *opaque,int irq,int level) {RISCVCPU *cpu =RISCV_CPU (opaque); +CPURISCVState *env = &cpu->env; -switch (irq) { -case IRQ_U_SOFT : -case IRQ_S_SOFT : -case IRQ_VS_SOFT : -case IRQ_M_SOFT : -case IRQ_U_TIMER : -case IRQ_S_TIMER : -case IRQ_VS_TIMER : -case IRQ_M_TIMER : -case IRQ_U_EXT : -case IRQ_S_EXT : -case IRQ_VS_EXT : -case IRQ_M_EXT : -riscv_cpu_update_mip (cpu,1 << irq,BOOL_TO_MASK (level)); -break ; -default : +if (irq <IRQ_LOCAL_MAX ) { +switch (irq) { +case IRQ_U_SOFT : +case IRQ_S_SOFT : +case IRQ_VS_SOFT : +case IRQ_M_SOFT : +case IRQ_U_TIMER : +case IRQ_S_TIMER : +case IRQ_VS_TIMER : +case IRQ_M_TIMER : +case IRQ_U_EXT : +case IRQ_S_EXT : +case IRQ_VS_EXT : +case IRQ_M_EXT : +riscv_cpu_update_mip (cpu,1 << irq,BOOL_TO_MASK (level)); +break ; +default : +g_assert_not_reached (); + } + }else if (irq < (IRQ_LOCAL_MAX +IRQ_LOCAL_GUEST_MAX )) { +/* Require H-extension for handling guest local interrupts */ +if (!riscv_has_ext (env,RVH )) { +g_assert_not_reached (); + } + +/* Compute bit position in HGEIP CSR */ + irq = irq -IRQ_LOCAL_MAX +1 ; +if (env->geilen < irq) { +g_assert_not_reached (); + } + +/* Update HGEIP CSR */ + env->hgeip &= ~((target_ulong )1 << irq); +if (level) { + env->hgeip |= (target_ulong )1 << irq; + } + +/* Update mip.SGEIP bit */ +riscv_cpu_update_mip (cpu,MIP_SGEIP , +BOOL_TO_MASK (!!(env->hgeie & env->hgeip))); + }else {g_assert_not_reached (); } }
@@ -611,7 +637,8 @@ static void riscv_cpu_init(Object *obj)cpu_set_cpustate_pointers (cpu);#ifndef CONFIG_USER_ONLY -qdev_init_gpio_in (DEVICE (cpu), riscv_cpu_set_irq,IRQ_LOCAL_MAX ); +qdev_init_gpio_in (DEVICE (cpu), riscv_cpu_set_irq, +IRQ_LOCAL_MAX +IRQ_LOCAL_GUEST_MAX );#endif /* CONFIG_USER_ONLY */ }
target/riscv/cpu.h +5 -0
@@ -151,6 +151,7 @@ struct CPURISCVState {target_ulong priv;/* This contains QEMU specific information about the virt state. */ target_ulong virt; +target_ulong geilen;target_ulong resetvec;target_ulong mhartid;
@@ -188,6 +189,8 @@ struct CPURISCVState {target_ulong htval;target_ulong htinst;target_ulong hgatp; +target_ulong hgeie; +target_ulong hgeip;uint64_t htimedelta;/* Virtual CSRs */
@@ -355,6 +358,8 @@ int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,int riscv_cpu_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg);int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg);bool riscv_cpu_fp_enabled (CPURISCVState *env); +target_ulong riscv_cpu_get_geilen (CPURISCVState *env); +void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen);bool riscv_cpu_vector_enabled (CPURISCVState *env);bool riscv_cpu_virt_enabled (CPURISCVState *env);void riscv_cpu_set_virt_enabled (CPURISCVState *env,bool enable);
target/riscv/cpu_bits.h +1 -0
@@ -536,6 +536,7 @@ typedef enum RISCVException {#define IRQ_M_EXT 11#define IRQ_S_GEXT 12#define IRQ_LOCAL_MAX 16 +#define IRQ_LOCAL_GUEST_MAX (TARGET_LONG_BITS - 1)/* mip masks */ #define MIP_USIP (1 << IRQ_U_SOFT)
target/riscv/cpu_helper.c +34 -3
@@ -158,7 +158,11 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *env)target_ulong mstatus_mie =get_field (env->mstatus,MSTATUS_MIE );target_ulong mstatus_sie =get_field (env->mstatus,MSTATUS_SIE ); -target_ulong pending = env->mip & env->mie; +target_ulong vsgemask = + (target_ulong )1 <<get_field (env->hstatus,HSTATUS_VGEIN ); +target_ulong vsgein = (env->hgeip & vsgemask) ?MIP_VSEIP :0 ; + +target_ulong pending = (env->mip | vsgein) & env->mie;target_ulong mie = env->priv <PRV_M || (env->priv ==PRV_M && mstatus_mie);
@@ -278,6 +282,28 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) } } +target_ulong riscv_cpu_get_geilen (CPURISCVState *env) +{ +if (!riscv_has_ext (env,RVH )) { +return 0 ; + } + +return env->geilen; +} + +void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen) +{ +if (!riscv_has_ext (env,RVH )) { +return ; + } + +if (geilen > (TARGET_LONG_BITS -1 )) { +return ; + } + + env->geilen = geilen; +} +bool riscv_cpu_virt_enabled (CPURISCVState *env) {if (!riscv_has_ext (env,RVH )) {
@@ -321,9 +347,14 @@ uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value) {CPURISCVState *env = &cpu->env;CPUState *cs =CPU (cpu); -uint32_t old = env->mip; +uint32_t gein, vsgein =0 , old = env->mip;bool locked = false; +if (riscv_cpu_virt_enabled (env)) { + gein =get_field (env->hstatus,HSTATUS_VGEIN ); + vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ; + } +if (!qemu_mutex_iothread_locked ()) { locked = true;qemu_mutex_lock_iothread ();
@@ -331,7 +362,7 @@ uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value) env->mip = (env->mip & ~mask) | (value & mask); -if (env->mip) { +if (env->mip | vsgein) {cpu_interrupt (cs,CPU_INTERRUPT_HARD ); }else {cpu_reset_interrupt (cs,CPU_INTERRUPT_HARD );
target/riscv/csr.c +30 -13
107 lines not on this page · raw message
target/riscv/machine.c +4 -2
@@ -78,8 +78,8 @@ static bool hyper_needed(void *opaque)static const VMStateDescription vmstate_hyper = { .name ="cpu/hyper" , - .version_id =1 , - .minimum_version_id =1 , + .version_id =2 , + .minimum_version_id =2 , .needed = hyper_needed, .fields = (VMStateField []) {VMSTATE_UINTTL (env.hstatus,RISCVCPU ),
@@ -89,6 +89,8 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.htval,RISCVCPU ),VMSTATE_UINTTL (env.htinst,RISCVCPU ),VMSTATE_UINTTL (env.hgatp,RISCVCPU ), +VMSTATE_UINTTL (env.hgeie,RISCVCPU ), +VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), VMSTATE_UINT64(env.vsstatus, RISCVCPU),
Signature · 1 line
2.25.1
Re: [PATCH v6 03/23] target/riscv: Implement hgeie and hgeip CSRs
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:45寫道:
Quoted 388 lines
From: Anup Patel <[email protected]>
The hgeie and hgeip CSRs are required for emulating an external interrupt controller capable of injecting virtual external interrupt to Guest/VM running at VS-level.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 61 ++++++++++++++++++++++++++++----------- target/riscv/cpu.h | 5 ++++ target/riscv/cpu_bits.h | 1 + target/riscv/cpu_helper.c | 37 ++++++++++++++++++++++-- target/riscv/csr.c | 43 ++++++++++++++++++--------- target/riscv/machine.c | 6 ++-- 6 files changed, 118 insertions(+), 35 deletions(-)target/riscv/cpu.c +42
-16@@ -582,23 +582,49 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)static void riscv_cpu_set_irq (void *opaque,int irq,int level) {RISCVCPU *cpu =RISCV_CPU (opaque); +CPURISCVState *env = &cpu->env; -switch (irq) { -case IRQ_U_SOFT : -case IRQ_S_SOFT : -case IRQ_VS_SOFT : -case IRQ_M_SOFT : -case IRQ_U_TIMER : -case IRQ_S_TIMER : -case IRQ_VS_TIMER : -case IRQ_M_TIMER : -case IRQ_U_EXT : -case IRQ_S_EXT : -case IRQ_VS_EXT : -case IRQ_M_EXT : -riscv_cpu_update_mip (cpu,1 << irq,BOOL_TO_MASK (level)); -break ; -default : +if (irq <IRQ_LOCAL_MAX ) { +switch (irq) { +case IRQ_U_SOFT : +case IRQ_S_SOFT : +case IRQ_VS_SOFT : +case IRQ_M_SOFT : +case IRQ_U_TIMER : +case IRQ_S_TIMER : +case IRQ_VS_TIMER : +case IRQ_M_TIMER : +case IRQ_U_EXT : +case IRQ_S_EXT : +case IRQ_VS_EXT : +case IRQ_M_EXT : +riscv_cpu_update_mip (cpu,1 << irq,BOOL_TO_MASK (level)); +break ; +default : +g_assert_not_reached (); + } + }else if (irq < (IRQ_LOCAL_MAX +IRQ_LOCAL_GUEST_MAX )) { +/* Require H-extension for handling guest local interrupts */ +if (!riscv_has_ext (env,RVH )) { +g_assert_not_reached (); + } + +/* Compute bit position in HGEIP CSR */ + irq = irq -IRQ_LOCAL_MAX +1 ; +if (env->geilen < irq) { +g_assert_not_reached (); + } + +/* Update HGEIP CSR */ + env->hgeip &= ~((target_ulong )1 << irq); +if (level) { + env->hgeip |= (target_ulong )1 << irq; + } + +/* Update mip.SGEIP bit */ +riscv_cpu_update_mip (cpu,MIP_SGEIP , +BOOL_TO_MASK (!!(env->hgeie & env->hgeip))); + }else {g_assert_not_reached (); }}
target/riscv/cpu.c +2
-1@@ -611,7 +637,8 @@ static void riscv_cpu_init(Object *obj)cpu_set_cpustate_pointers (cpu);#ifndef CONFIG_USER_ONLY -qdev_init_gpio_in (DEVICE (cpu), riscv_cpu_set_irq,IRQ_LOCAL_MAX ); +qdev_init_gpio_in (DEVICE (cpu), riscv_cpu_set_irq, +IRQ_LOCAL_MAX +IRQ_LOCAL_GUEST_MAX );#endif /* CONFIG_USER_ONLY */ }target/riscv/cpu.h +5
-0@@ -151,6 +151,7 @@ struct CPURISCVState {target_ulong priv;/* This contains QEMU specific information about the virt state. */ target_ulong virt; +target_ulong geilen;target_ulong resetvec;target_ulong mhartid;@@ -188,6 +189,8 @@ struct CPURISCVState {target_ulong htval;target_ulong htinst;target_ulong hgatp; +target_ulong hgeie; +target_ulong hgeip;uint64_t htimedelta;/* Virtual CSRs */ @@ -355,6 +358,8 @@ int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, int riscv_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg);bool riscv_cpu_fp_enabled (CPURISCVState *env); +target_ulong riscv_cpu_get_geilen (CPURISCVState *env); +void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen);bool riscv_cpu_vector_enabled (CPURISCVState *env);bool riscv_cpu_virt_enabled (CPURISCVState *env);void
riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable);target/riscv/cpu_bits.h +1
-0@@ -536,6 +536,7 @@ typedef enum RISCVException {#define IRQ_M_EXT 11#define IRQ_S_GEXT 12#define IRQ_LOCAL_MAX 16 +#define IRQ_LOCAL_GUEST_MAX (TARGET_LONG_BITS - 1)/* mip masks */ #define MIP_USIP (1 << IRQ_U_SOFT)target/riscv/cpu_helper.c +5
-1@@ -158,7 +158,11 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *env)target_ulong mstatus_mie =get_field (env->mstatus,MSTATUS_MIE );target_ulong mstatus_sie =get_field (env->mstatus,MSTATUS_SIE ); -target_ulong pending = env->mip & env->mie; +target_ulong vsgemask = + (target_ulong )1 <<get_field (env->hstatus,HSTATUS_VGEIN ); +target_ulong vsgein = (env->hgeip & vsgemask) ?MIP_VSEIP :0 ; + +target_ulong pending = (env->mip | vsgein) & env->mie; target_ulong mie = env->priv < PRV_M ||(env->priv == PRV_M && mstatus_mie);target/riscv/cpu_helper.c +22
-0@@ -278,6 +282,28 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) } } +target_ulong riscv_cpu_get_geilen (CPURISCVState *env) +{ +if (!riscv_has_ext (env,RVH )) { +return 0 ; + } + +return env->geilen; +} + +void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen) +{ +if (!riscv_has_ext (env,RVH )) { +return ; + } + +if (geilen > (TARGET_LONG_BITS -1 )) { +return ; + } + + env->geilen = geilen; +} +bool riscv_cpu_virt_enabled (CPURISCVState *env) {if (!riscv_has_ext(env, RVH)) {target/riscv/cpu_helper.c +6
-1@@ -321,9 +347,14 @@ uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value) {CPURISCVState *env = &cpu->env;CPUState *cs =CPU (cpu); -uint32_t old = env->mip; +uint32_t gein, vsgein =0 , old = env->mip;bool locked = false; +if (riscv_cpu_virt_enabled (env)) { + gein =get_field (env->hstatus,HSTATUS_VGEIN ); + vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ; + } +if (!qemu_mutex_iothread_locked ()) { locked = true;qemu_mutex_lock_iothread();target/riscv/cpu_helper.c +1
-1@@ -331,7 +362,7 @@ uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value) env->mip = (env->mip & ~mask) | (value & mask); -if (env->mip) { +if (env->mip | vsgein) {cpu_interrupt (cs,CPU_INTERRUPT_HARD ); }else {cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);target/riscv/csr.c +0
-0@@ -832,7 +832,7 @@ static RISCVException rmw_mip(CPURISCVState *env, int csrno,RISCVCPU *cpu =env_archcpu (env);/* Allow software control of delegable interrupts not claimed by hardware */
target_ulong mask = write_mask & delegable_ints & ~env->miclaim;target/riscv/csr.c +6
-1-uint32_t old_mip; +uint32_t gin, old_mip;if (mask) { old_mip =riscv_cpu_update_mip (cpu, mask, (new_value & mask));@@ -840,6 +840,11 @@ static RISCVException rmw_mip(CPURISCVState *env, int csrno, old_mip = env->mip; } +if (csrno !=CSR_HVIP ) { + gin =get_field (env->hstatus,HSTATUS_VGEIN ); + old_mip |= (env->hgeip & ((target_ulong )1 << gin)) ?MIP_VSEIP :0 ; + } +if (ret_value) {*ret_value = old_mip; }target/riscv/csr.c +1
-1@@ -1002,7 +1007,7 @@ static RISCVException rmw_vsip(CPURISCVState *env, int csrno, target_ulong new_value, target_ulong write_mask) { /* Shift the S bits to their VS bit location in mip */ - int ret = rmw_mip(env, 0, ret_value, new_value << 1, + int ret = rmw_mip(env, csrno, ret_value, new_value << 1, (write_mask << 1) & vsip_writable_mask &
env->hideleg);if (ret_value) {target/riscv/csr.c +2
-2@@ -1022,7 +1027,7 @@ static RISCVException rmw_sip(CPURISCVState *env,int csrno, if (riscv_cpu_virt_enabled(env)) { ret =rmw_vsip (env,CSR_VSIP , ret_value, new_value, write_mask); } else { - ret =rmw_mip (env,CSR_MSTATUS , ret_value, new_value, + ret =rmw_mip (env, csrno, ret_value, new_value, write_mask & env->mideleg & sip_writable_mask); }@@ -1141,7 +1146,7 @@ static RISCVException rmw_hvip(CPURISCVState *env,int csrno, target_ulong *ret_value, target_ulong new_value, target_ulong write_mask) { - int ret = rmw_mip(env, 0, ret_value, new_value, + int ret = rmw_mip(env, csrno, ret_value, new_value, write_mask & hvip_writable_mask);if (ret_value) {target/riscv/csr.c +1
-1@@ -1154,7 +1159,7 @@ static RISCVException rmw_hip(CPURISCVState *env,int csrno, target_ulong *ret_value, target_ulong new_value, target_ulong write_mask) { - int ret = rmw_mip(env, 0, ret_value, new_value, + int ret = rmw_mip(env, csrno, ret_value, new_value, write_mask & hip_writable_mask);if (ret_value) {target/riscv/csr.c +15
-3@@ -1191,15 +1196,27 @@ static RISCVException write_hcounteren(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } -static RISCVException write_hgeie(CPURISCVState *env, int csrno, - target_ulong val) +static RISCVException read_hgeie(CPURISCVState *env, int csrno, + target_ulong *val) {if (val) { -qemu_log_mask (LOG_UNIMP ,"No support for a non-zero GEILEN." ); + *val = env->hgeie; }return RISCV_EXCP_NONE ; } +static RISCVException write_hgeie(CPURISCVState *env, int csrno, + target_ulong val) +{ +/* Only GEILEN:1 bits implemented and BIT0 is never implemented */ + val &= ((((target_ulong )1 ) << env->geilen) -1 ) <<1 ; + env->hgeie = val; +/* Update mip.SGEIP bit */ +riscv_cpu_update_mip (env_archcpu (env),MIP_SGEIP , +BOOL_TO_MASK (!!(env->hgeie & env->hgeip))); +return RISCV_EXCP_NONE ; +} +static RISCVException read_htval (CPURISCVState *env,int csrno,target_ulong *val){
target/riscv/csr.c +3
-3@@ -1227,11 +1244,11 @@ static RISCVException write_htinst(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } -static RISCVException write_hgeip (CPURISCVState *env,int csrno, -target_ulong val) +static RISCVException read_hgeip (CPURISCVState *env,int csrno, +target_ulong *val) {if (val) { -qemu_log_mask (LOG_UNIMP ,"No support for a non-zero GEILEN." ); + *val = env->hgeip; }return RISCV_EXCP_NONE ;}
target/riscv/csr.c +1
-1@@ -1922,10 +1939,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HIP ] = {"hip" , hmode,NULL ,NULL , rmw_hip }, [CSR_HIE ] = {"hie" , hmode, read_hie, write_hie }, [CSR_HCOUNTEREN ] = {"hcounteren" , hmode, read_hcounteren, write_hcounteren }, - [CSR_HGEIE ] = {"hgeie" , hmode, read_zero, write_hgeie }, + [CSR_HGEIE ] = {"hgeie" , hmode, read_hgeie, write_hgeie }, [CSR_HTVAL ] = {"htval" , hmode, read_htval,
write_htval},
[CSR_HTINST] = { "htinst", hmode,read_htinst,write_htinst},
- [CSR_HGEIP] = { "hgeip", hmode,read_zero,write_hgeip},
+ [CSR_HGEIP] = { "hgeip", hmode,read_hgeip, NULL
},
[CSR_HGATP] = { "hgatp", hmode,read_hgatp,
write_hgatp},
[CSR_HTIMEDELTA] = { "htimedelta", hmode,read_htimedelta,write_htimedelta},
[CSR_HTIMEDELTAH] = { "htimedeltah", hmode32,read_htimedeltah,write_htimedeltah},target/riscv/machine.c +4
-3@@ -78,8 +78,8 @@ static bool hyper_needed(void *opaque)static const VMStateDescription vmstate_hyper = { .name ="cpu/hyper" , - .version_id =1 , - .minimum_version_id =1 , + .version_id =2 , + .minimum_version_id =2 , .needed = hyper_needed, .fields = (VMStateField []) {VMSTATE_UINTTL (env.hstatus,RISCVCPU ),@@ -89,6 +89,8 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.htval,RISCVCPU ),VMSTATE_UINTTL (env.htinst,RISCVCPU ),VMSTATE_UINTTL (env.hgatp,RISCVCPU ), +VMSTATE_UINTTL (env.hgeie,RISCVCPU ), +VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), VMSTATE_UINT64(env.vsstatus, RISCVCPU), --
Quoted 4 lines
2.25.1
PATCH v6 4/23 target/riscv: Improve delivery of guest external interrupts
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The guest external interrupts from an interrupt controller are delivered only when the Guest/VM is running (i.e. V=1). This means any guest external interrupt which is triggered while the Guest/VM is not running (i.e. V=0) will be missed on QEMU resulting in Guest with sluggish response to serial console input and other I/O events.
To solve this, we check and inject interrupt after setting V=1.
--- target/riscv/cpu_helper.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
target/riscv/cpu_helper.c +13 -0
@@ -325,6 +325,19 @@ void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable) } env->virt =set_field (env->virt,VIRT_ONOFF , enable); + +if (enable) { +/* +* The guest external interrupts from an interrupt controller are +* delivered only when the Guest/VM is running (i.e. V=1). This means +* any guest external interrupt which is triggered while the Guest/VM +* is not running (i.e. V=0) will be missed on QEMU resulting in guest +* with sluggish response to serial console input and other I/O events. +* +* To solve this, we check and inject interrupt after setting V=1. +*/ +riscv_cpu_update_mip (env_archcpu (env),0 ,0 ); + } }bool riscv_cpu_two_stage_lookup (int mmu_idx)
Signature · 1 line
2.25.1
Re: [PATCH v6 04/23] target/riscv: Improve delivery of guest external interrupts
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:48寫道:
Quoted 47 lines
From: Anup Patel <[email protected]>
The guest external interrupts from an interrupt controller are delivered only when the Guest/VM is running (i.e. V=1). This means any guest external interrupt which is triggered while the Guest/VM is not running (i.e. V=0) will be missed on QEMU resulting in Guest with sluggish response to serial console input and other I/O events.
To solve this, we check and inject interrupt after setting V=1.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu_helper.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)target/riscv/cpu_helper.c +5
-0@@ -325,6 +325,19 @@ void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable) } env->virt =set_field (env->virt,VIRT_ONOFF , enable); + +if (enable) { +/* +* The guest external interrupts from an interrupt controller are +* delivered only when the Guest/VM is running (i.e. V=1). This means
+ * any guest external interrupt which is triggered while the
Guest/VM
+ * is not running (i.e. V=0) will be missed on QEMU resulting in
guest
+ * with sluggish response to serial console input and other I/O
events.target/riscv/cpu_helper.c +5
-1+* +* To solve this, we check and inject interrupt after setting V=1. +*/ +riscv_cpu_update_mip (env_archcpu (env),0 ,0 ); + } }bool riscv_cpu_two_stage_lookup (int mmu_idx) --
Quoted 4 lines
2.25.1
PATCH v6 5/23 target/riscv: Allow setting CPU feature from machine/device emulation
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The machine or device emulation should be able to force set certain CPU features because:
1) We can have certain CPU features which are in-general optional but implemented by RISC-V CPUs on the machine.
2) We can have devices which require a certain CPU feature. For example, AIA IMSIC devices expect AIA CSRs implemented by RISC-V CPUs.
--- target/riscv/cpu.c | 11 +++-------- target/riscv/cpu.h | 5 +++++ 2 files changed, 8 insertions(+), 8 deletions(-)
target/riscv/cpu.c +3 -8
@@ -124,11 +124,6 @@ static void set_vext_version(CPURISCVState *env, int vext_ver) env->vext_ver = vext_ver; } -static void set_feature (CPURISCVState *env,int feature) -{ - env->features |= (1ULL << feature); -} -static void set_resetvec (CPURISCVState *env,target_ulong resetvec) {#ifndef CONFIG_USER_ONLY
@@ -434,18 +429,18 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) }if (cpu->cfg.mmu) { -set_feature (env,RISCV_FEATURE_MMU ); +riscv_set_feature (env,RISCV_FEATURE_MMU ); }if (cpu->cfg.pmp) { -set_feature (env,RISCV_FEATURE_PMP ); +riscv_set_feature (env,RISCV_FEATURE_PMP );/* * Enhanced PMP should only be available * on harts with PMP support */ if (cpu->cfg.epmp) { -set_feature (env,RISCV_FEATURE_EPMP ); +riscv_set_feature (env,RISCV_FEATURE_EPMP ); } }
target/riscv/cpu.h +5 -0
@@ -344,6 +344,11 @@ static inline bool riscv_feature(CPURISCVState *env, int feature)return env->features & (1ULL << feature); } +static inline void riscv_set_feature (CPURISCVState *env,int feature) +{ + env->features |= (1ULL << feature); +} +#include "cpu_user.h" extern const char *const riscv_int_regnames[];
Signature · 1 line
2.25.1
Re: [PATCH v6 05/23] target/riscv: Allow setting CPU feature from machine/device emulation
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:36寫道:
Quoted 74 lines
From: Anup Patel <[email protected]>
The machine or device emulation should be able to force set certain CPU features because:
1) We can have certain CPU features which are in-general optional but implemented by RISC-V CPUs on the machine.
2) We can have devices which require a certain CPU feature. For example, AIA IMSIC devices expect AIA CSRs implemented by RISC-V CPUs.Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 11 +++-------- target/riscv/cpu.h | 5 +++++ 2 files changed, 8 insertions(+), 8 deletions(-)target/riscv/cpu.c +0
-5@@ -124,11 +124,6 @@ static void set_vext_version(CPURISCVState *env, int vext_ver) env->vext_ver = vext_ver; } -static void set_feature (CPURISCVState *env,int feature) -{ - env->features |= (1ULL << feature); -} -static void set_resetvec (CPURISCVState *env,target_ulong resetvec) {#ifndef
CONFIG_USER_ONLYtarget/riscv/cpu.c +3
-3@@ -434,18 +429,18 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) }if (cpu->cfg.mmu) { -set_feature (env,RISCV_FEATURE_MMU ); +riscv_set_feature (env,RISCV_FEATURE_MMU ); }if (cpu->cfg.pmp) { -set_feature (env,RISCV_FEATURE_PMP ); +riscv_set_feature (env,RISCV_FEATURE_PMP );/* * Enhanced PMP should only be available * on harts with PMP support */ if (cpu->cfg.epmp) { -set_feature (env,RISCV_FEATURE_EPMP ); +riscv_set_feature (env,RISCV_FEATURE_EPMP ); } }target/riscv/cpu.h +5
-0@@ -344,6 +344,11 @@ static inline bool riscv_feature(CPURISCVState *env, int feature)return env->features & (1ULL << feature); } +static inline void riscv_set_feature (CPURISCVState *env,int feature) +{ + env->features |= (1ULL << feature); +} +#include "cpu_user.h"
Quoted 6 lines
extern const char * const riscv_int_regnames[];
--
2.25.1
PATCH v6 6/23 target/riscv: Add AIA cpu feature
In reply to Anup PatelFrom: Anup Patel <[email protected]>
We define a CPU feature for AIA CSR support in RISC-V CPUs which can be set by machine/device emulation. The RISC-V CSR emulation will also check this feature for emulating AIA CSRs.
--- target/riscv/cpu.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/riscv/cpu.h +2 -1
@@ -75,7 +75,8 @@ enum {RISCV_FEATURE_MMU ,RISCV_FEATURE_PMP ,RISCV_FEATURE_EPMP , - RISCV_FEATURE_MISA +RISCV_FEATURE_MISA , + RISCV_FEATURE_AIA };#define PRIV_VERSION_1_10_0 0x00011000
Signature · 1 line
2.25.1
Re: [PATCH v6 06/23] target/riscv: Add AIA cpu feature
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:36寫道:
Quoted 29 lines
From: Anup Patel <[email protected]>
We define a CPU feature for AIA CSR support in RISC-V CPUs which can be set by machine/device emulation. The RISC-V CSR emulation will also check this feature for emulating AIA CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)target/riscv/cpu.h +2
-2@@ -75,7 +75,8 @@ enum {RISCV_FEATURE_MMU ,RISCV_FEATURE_PMP ,RISCV_FEATURE_EPMP , -RISCV_FEATURE_MISA +RISCV_FEATURE_MISA , + RISCV_FEATURE_AIA };#define PRIV_VERSION_1_10_0 0x00011000 --
Quoted 4 lines
2.25.1
PATCH v6 7/23 target/riscv: Add defines for AIA CSRs
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The RISC-V AIA specification extends RISC-V local interrupts and introduces new CSRs. This patch adds defines for the new AIA CSRs.
--- target/riscv/cpu_bits.h | 127 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)
target/riscv/cpu_bits.h +127 -0
@@ -168,6 +168,31 @@#define CSR_MTVAL 0x343#define CSR_MIP 0x344 +/* Machine-Level Window to Indirectly Accessed Registers (AIA) */ +#define CSR_MISELECT 0x350 +#define CSR_MIREG 0x351 + +/* Machine-Level Interrupts (AIA) */ +#define CSR_MTOPI 0xfb0 + +/* Machine-Level IMSIC Interface (AIA) */ +#define CSR_MSETEIPNUM 0x358 +#define CSR_MCLREIPNUM 0x359 +#define CSR_MSETEIENUM 0x35a +#define CSR_MCLREIENUM 0x35b +#define CSR_MTOPEI 0x35c + +/* Virtual Interrupts for Supervisor Level (AIA) */ +#define CSR_MVIEN 0x308 +#define CSR_MVIP 0x309 + +/* Machine-Level High-Half CSRs (AIA) */ +#define CSR_MIDELEGH 0x313 +#define CSR_MIEH 0x314 +#define CSR_MVIENH 0x318 +#define CSR_MVIPH 0x319 +#define CSR_MIPH 0x354 +/* Supervisor Trap Setup */ #define CSR_SSTATUS 0x100#define CSR_SEDELEG 0x102
@@ -187,6 +212,24 @@#define CSR_SPTBR 0x180#define CSR_SATP 0x180 +/* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */ +#define CSR_SISELECT 0x150 +#define CSR_SIREG 0x151 + +/* Supervisor-Level Interrupts (AIA) */ +#define CSR_STOPI 0xdb0 + +/* Supervisor-Level IMSIC Interface (AIA) */ +#define CSR_SSETEIPNUM 0x158 +#define CSR_SCLREIPNUM 0x159 +#define CSR_SSETEIENUM 0x15a +#define CSR_SCLREIENUM 0x15b +#define CSR_STOPEI 0x15c + +/* Supervisor-Level High-Half CSRs (AIA) */ +#define CSR_SIEH 0x114 +#define CSR_SIPH 0x154 +/* Hpervisor CSRs */ #define CSR_HSTATUS 0x600#define CSR_HEDELEG 0x602
@@ -217,6 +260,35 @@#define CSR_MTINST 0x34a#define CSR_MTVAL2 0x34b +/* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */ +#define CSR_HVIEN 0x608 +#define CSR_HVICTL 0x609 +#define CSR_HVIPRIO1 0x646 +#define CSR_HVIPRIO2 0x647 + +/* VS-Level Window to Indirectly Accessed Registers (H-extension with AIA) */ +#define CSR_VSISELECT 0x250 +#define CSR_VSIREG 0x251 + +/* VS-Level Interrupts (H-extension with AIA) */ +#define CSR_VSTOPI 0xeb0 + +/* VS-Level IMSIC Interface (H-extension with AIA) */ +#define CSR_VSSETEIPNUM 0x258 +#define CSR_VSCLREIPNUM 0x259 +#define CSR_VSSETEIENUM 0x25a +#define CSR_VSCLREIENUM 0x25b +#define CSR_VSTOPEI 0x25c + +/* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ +#define CSR_HIDELEGH 0x613 +#define CSR_HVIENH 0x618 +#define CSR_HVIPH 0x655 +#define CSR_HVIPRIO1H 0x656 +#define CSR_HVIPRIO2H 0x657 +#define CSR_VSIEH 0x214 +#define CSR_VSIPH 0x254 +/* Enhanced Physical Memory Protection (ePMP) */ #define CSR_MSECCFG 0x747#define CSR_MSECCFGH 0x757
@@ -629,4 +701,59 @@ typedef enum RISCVException {#define UMTE_U_PM_INSN U_PM_INSN#define UMTE_MASK (UMTE_U_PM_ENABLE | MMTE_U_PM_CURRENT | UMTE_U_PM_INSN) +/* MISELECT, SISELECT, and VSISELECT bits (AIA) */ +#define ISELECT_IPRIO0 0x30 +#define ISELECT_IPRIO15 0x3f +#define ISELECT_IMSIC_EIDELIVERY 0x70 +#define ISELECT_IMSIC_EITHRESHOLD 0x72 +#define ISELECT_IMSIC_EIP0 0x80 +#define ISELECT_IMSIC_EIP63 0xbf +#define ISELECT_IMSIC_EIE0 0xc0 +#define ISELECT_IMSIC_EIE63 0xff +#define ISELECT_IMSIC_FIRST ISELECT_IMSIC_EIDELIVERY +#define ISELECT_IMSIC_LAST ISELECT_IMSIC_EIE63 +#define ISELECT_MASK 0x1ff + +/* Dummy [M|S|VS]ISELECT value for emulating [M|S|VS]TOPEI CSRs */ +#define ISELECT_IMSIC_TOPEI (ISELECT_MASK + 1) + +/* IMSIC bits (AIA) */ +#define IMSIC_TOPEI_IID_SHIFT 16 +#define IMSIC_TOPEI_IID_MASK 0x7ff +#define IMSIC_TOPEI_IPRIO_MASK 0x7ff +#define IMSIC_EIPx_BITS 32 +#define IMSIC_EIEx_BITS 32 + +/* MTOPI and STOPI bits (AIA) */ +#define TOPI_IID_SHIFT 16 +#define TOPI_IID_MASK 0xfff +#define TOPI_IPRIO_MASK 0xff + +/* Interrupt priority bits (AIA) */ +#define IPRIO_IRQ_BITS 8 +#define IPRIO_MMAXIPRIO 255 +#define IPRIO_DEFAULT_MMAXIPRIO 15 +#define IPRIO_DEFAULT_VS (IPRIO_DEFAULT_MMAXIPRIO - 4) +#define IPRIO_DEFAULT_SGEXT (IPRIO_DEFAULT_MMAXIPRIO - 5) +#define IPRIO_DEFAULT_S (IPRIO_DEFAULT_MMAXIPRIO - 6) +#define IPRIO_DEFAULT_M (IPRIO_DEFAULT_MMAXIPRIO - 7) +#define IPRIO_DEFAULT_U (_i) (((_i) >> 4) & 0x3) +#define IPRIO_DEFAULT_L (_i) ((_i) & 0xf) +#define IPRIO_DEFAULT_16_23 (_i) \ + (IPRIO_DEFAULT_MMAXIPRIO - (IPRIO_DEFAULT_L(_i) >> 1)) +#define IPRIO_DEFAULT_24_31 (_i) \ + (IPRIO_DEFAULT_MMAXIPRIO - (4 + (IPRIO_DEFAULT_L(_i) >> 1))) +#define IPRIO_DEFAULT_32_47 (_i) \ + (IPRIO_DEFAULT_MMAXIPRIO - (IPRIO_DEFAULT_L(_i) >> 2)) +#define IPRIO_DEFAULT_48_63 (_i) \ + (IPRIO_DEFAULT_MMAXIPRIO - (8 + (IPRIO_DEFAULT_L(_i) >> 2))) + +/* HVICTL bits (AIA) */ +#define HVICTL_VTI 0x40000000 +#define HVICTL_IID 0x0fff0000 +#define HVICTL_IPRIOM 0x00000100 +#define HVICTL_IPRIO 0x000000ff +#define HVICTL_VALID_MASK \ + (HVICTL_VTI | HVICTL_IID | HVICTL_IPRIOM | HVICTL_IPRIO) +#endif
Signature · 1 line
2.25.1
Re: [PATCH v6 07/23] target/riscv: Add defines for AIA CSRs
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:52寫道:
Quoted 170 lines
From: Anup Patel <[email protected]>
The RISC-V AIA specification extends RISC-V local interrupts and introduces new CSRs. This patch adds defines for the new AIA CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu_bits.h | 127 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)target/riscv/cpu_bits.h +50
-0@@ -168,6 +168,31 @@#define CSR_MTVAL 0x343#define CSR_MIP 0x344 +/* Machine-Level Window to Indirectly Accessed Registers (AIA) */ +#define CSR_MISELECT 0x350 +#define CSR_MIREG 0x351 + +/* Machine-Level Interrupts (AIA) */ +#define CSR_MTOPI 0xfb0 + +/* Machine-Level IMSIC Interface (AIA) */ +#define CSR_MSETEIPNUM 0x358 +#define CSR_MCLREIPNUM 0x359 +#define CSR_MSETEIENUM 0x35a +#define CSR_MCLREIENUM 0x35b +#define CSR_MTOPEI 0x35c + +/* Virtual Interrupts for Supervisor Level (AIA) */ +#define CSR_MVIEN 0x308 +#define CSR_MVIP 0x309 + +/* Machine-Level High-Half CSRs (AIA) */ +#define CSR_MIDELEGH 0x313 +#define CSR_MIEH 0x314 +#define CSR_MVIENH 0x318 +#define CSR_MVIPH 0x319 +#define CSR_MIPH 0x354 +/* Supervisor Trap Setup */ #define CSR_SSTATUS 0x100#define CSR_SEDELEG 0x102@@ -187,6 +212,24 @@#define CSR_SPTBR 0x180#define CSR_SATP 0x180 +/* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */ +#define CSR_SISELECT 0x150 +#define CSR_SIREG 0x151 + +/* Supervisor-Level Interrupts (AIA) */ +#define CSR_STOPI 0xdb0 + +/* Supervisor-Level IMSIC Interface (AIA) */ +#define CSR_SSETEIPNUM 0x158 +#define CSR_SCLREIPNUM 0x159 +#define CSR_SSETEIENUM 0x15a +#define CSR_SCLREIENUM 0x15b +#define CSR_STOPEI 0x15c + +/* Supervisor-Level High-Half CSRs (AIA) */ +#define CSR_SIEH 0x114 +#define CSR_SIPH 0x154 +/* Hpervisor CSRs */ #define CSR_HSTATUS 0x600#define CSR_HEDELEG 0x602@@ -217,6 +260,35 @@#define CSR_MTINST 0x34a#define CSR_MTVAL2 0x34b +/* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */ +#define CSR_HVIEN 0x608 +#define CSR_HVICTL 0x609 +#define CSR_HVIPRIO1 0x646 +#define CSR_HVIPRIO2 0x647 + +/* VS-Level Window to Indirectly Accessed Registers (H-extension with AIA) */
target/riscv/cpu_bits.h +77
-0+#define CSR_VSISELECT 0x250 +#define CSR_VSIREG 0x251 + +/* VS-Level Interrupts (H-extension with AIA) */ +#define CSR_VSTOPI 0xeb0 + +/* VS-Level IMSIC Interface (H-extension with AIA) */ +#define CSR_VSSETEIPNUM 0x258 +#define CSR_VSCLREIPNUM 0x259 +#define CSR_VSSETEIENUM 0x25a +#define CSR_VSCLREIENUM 0x25b +#define CSR_VSTOPEI 0x25c + +/* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ +#define CSR_HIDELEGH 0x613 +#define CSR_HVIENH 0x618 +#define CSR_HVIPH 0x655 +#define CSR_HVIPRIO1H 0x656 +#define CSR_HVIPRIO2H 0x657 +#define CSR_VSIEH 0x214 +#define CSR_VSIPH 0x254 +/* Enhanced Physical Memory Protection (ePMP) */ #define CSR_MSECCFG 0x747#define CSR_MSECCFGH 0x757@@ -629,4 +701,59 @@ typedef enum RISCVException {#define UMTE_U_PM_INSN U_PM_INSN#define UMTE_MASK (UMTE_U_PM_ENABLE | MMTE_U_PM_CURRENT | UMTE_U_PM_INSN) +/* MISELECT, SISELECT, and VSISELECT bits (AIA) */ +#define ISELECT_IPRIO0 0x30 +#define ISELECT_IPRIO15 0x3f +#define ISELECT_IMSIC_EIDELIVERY 0x70 +#define ISELECT_IMSIC_EITHRESHOLD 0x72 +#define ISELECT_IMSIC_EIP0 0x80 +#define ISELECT_IMSIC_EIP63 0xbf +#define ISELECT_IMSIC_EIE0 0xc0 +#define ISELECT_IMSIC_EIE63 0xff +#define ISELECT_IMSIC_FIRST ISELECT_IMSIC_EIDELIVERY +#define ISELECT_IMSIC_LAST ISELECT_IMSIC_EIE63 +#define ISELECT_MASK 0x1ff + +/* Dummy [M|S|VS]ISELECT value for emulating [M|S|VS]TOPEI CSRs */ +#define ISELECT_IMSIC_TOPEI (ISELECT_MASK + 1) + +/* IMSIC bits (AIA) */ +#define IMSIC_TOPEI_IID_SHIFT 16 +#define IMSIC_TOPEI_IID_MASK 0x7ff +#define IMSIC_TOPEI_IPRIO_MASK 0x7ff +#define IMSIC_EIPx_BITS 32 +#define IMSIC_EIEx_BITS 32 + +/* MTOPI and STOPI bits (AIA) */ +#define TOPI_IID_SHIFT 16 +#define TOPI_IID_MASK 0xfff +#define TOPI_IPRIO_MASK 0xff + +/* Interrupt priority bits (AIA) */ +#define IPRIO_IRQ_BITS 8 +#define IPRIO_MMAXIPRIO 255 +#define IPRIO_DEFAULT_MMAXIPRIO 15 +#define IPRIO_DEFAULT_VS (IPRIO_DEFAULT_MMAXIPRIO - 4) +#define IPRIO_DEFAULT_SGEXT (IPRIO_DEFAULT_MMAXIPRIO - 5) +#define IPRIO_DEFAULT_S (IPRIO_DEFAULT_MMAXIPRIO - 6) +#define IPRIO_DEFAULT_M (IPRIO_DEFAULT_MMAXIPRIO - 7) +#define IPRIO_DEFAULT_U (_i) (((_i) >> 4) & 0x3) +#define IPRIO_DEFAULT_L (_i) ((_i) & 0xf) +#define IPRIO_DEFAULT_16_23 (_i) \ + (IPRIO_DEFAULT_MMAXIPRIO - (IPRIO_DEFAULT_L(_i) >> 1)) +#define IPRIO_DEFAULT_24_31 (_i) \ + (IPRIO_DEFAULT_MMAXIPRIO - (4 + (IPRIO_DEFAULT_L(_i) >> 1))) +#define IPRIO_DEFAULT_32_47 (_i) \ + (IPRIO_DEFAULT_MMAXIPRIO - (IPRIO_DEFAULT_L(_i) >> 2)) +#define IPRIO_DEFAULT_48_63 (_i) \ + (IPRIO_DEFAULT_MMAXIPRIO - (8 + (IPRIO_DEFAULT_L(_i) >> 2))) + +/* HVICTL bits (AIA) */ +#define HVICTL_VTI 0x40000000 +#define HVICTL_IID 0x0fff0000 +#define HVICTL_IPRIOM 0x00000100 +#define HVICTL_IPRIO 0x000000ff +#define HVICTL_VALID_MASK \ + (HVICTL_VTI | HVICTL_IID | HVICTL_IPRIOM | HVICTL_IPRIO) +
Quoted 6 lines
#endif
--
2.25.1
PATCH v6 8/23 target/riscv: Allow AIA device emulation to set ireg rmw callback
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The AIA device emulation (such as AIA IMSIC) should be able to set (or provide) AIA ireg read-modify-write callback for each privilege level of a RISC-V HART.
--- target/riscv/cpu.h | 23 +++++++++++++++++++++++ target/riscv/cpu_helper.c | 14 ++++++++++++++ 2 files changed, 37 insertions(+)
target/riscv/cpu.h +23 -0
@@ -242,6 +242,22 @@ struct CPURISCVState {uint64_t (*rdtime_fn)(uint32_t );uint32_t rdtime_fn_arg; +/* machine specific AIA ireg read-modify-write callback */ +#define AIA_MAKE_IREG (__isel, __priv, __virt, __vgein, __xlen) \ + ((((__xlen) & 0xff) << 24) | \ + (((__vgein) & 0x3f) << 20) | \ + (((__virt) & 0x1) << 18) | \ + (((__priv) & 0x3) << 16) | \ + (__isel & 0xffff)) +#define AIA_IREG_ISEL (__ireg) ((__ireg) & 0xffff) +#define AIA_IREG_PRIV (__ireg) (((__ireg) >> 16) & 0x3) +#define AIA_IREG_VIRT (__ireg) (((__ireg) >> 18) & 0x1) +#define AIA_IREG_VGEIN (__ireg) (((__ireg) >> 20) & 0x3f) +#define AIA_IREG_XLEN (__ireg) (((__ireg) >> 24) & 0xff) +int (*aia_ireg_rmw_fn[4 ])(void *arg,target_ulong reg, +target_ulong *val,target_ulong new_val,target_ulong write_mask); +void *aia_ireg_rmw_fn_arg[4 ]; +/* True if in debugger mode. */ bool debugger;
@@ -397,6 +413,13 @@ uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value);#define BOOL_TO_MASK (x) (-!!(x))/* helper for riscv_cpu_update_mip value */ void riscv_cpu_set_rdtime_fn (CPURISCVState *env,uint64_t (*fn)(uint32_t ),uint32_t arg); +void riscv_cpu_set_aia_ireg_rmw_fn (CPURISCVState *env,uint32_t priv, +int (*rmw_fn)(void *arg, +target_ulong reg, +target_ulong *val, +target_ulong new_val, +target_ulong write_mask), +void *rmw_fn_arg);#endif void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv);
target/riscv/cpu_helper.c +14 -0
@@ -395,6 +395,20 @@ void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(uint32_t), env->rdtime_fn_arg = arg; } +void riscv_cpu_set_aia_ireg_rmw_fn (CPURISCVState *env,uint32_t priv, +int (*rmw_fn)(void *arg, +target_ulong reg, +target_ulong *val, +target_ulong new_val, +target_ulong write_mask), +void *rmw_fn_arg) +{ +if (priv <=PRV_M ) { + env->aia_ireg_rmw_fn[priv] = rmw_fn; + env->aia_ireg_rmw_fn_arg[priv] = rmw_fn_arg; + } +} +void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv) {if (newpriv >PRV_M ) {
Signature · 1 line
2.25.1
Re: [PATCH v6 08/23] target/riscv: Allow AIA device emulation to set ireg rmw callback
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:36寫道:
Quoted 83 lines
From: Anup Patel <[email protected]>
The AIA device emulation (such as AIA IMSIC) should be able to set (or provide) AIA ireg read-modify-write callback for each privilege level of a RISC-V HART.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.h | 23 +++++++++++++++++++++++ target/riscv/cpu_helper.c | 14 ++++++++++++++ 2 files changed, 37 insertions(+)target/riscv/cpu.h +23
-0@@ -242,6 +242,22 @@ struct CPURISCVState {uint64_t (*rdtime_fn)(uint32_t );uint32_t rdtime_fn_arg; +/* machine specific AIA ireg read-modify-write callback */ +#define AIA_MAKE_IREG (__isel, __priv, __virt, __vgein, __xlen) \ + ((((__xlen) & 0xff) << 24) | \ + (((__vgein) & 0x3f) << 20) | \ + (((__virt) & 0x1) << 18) | \ + (((__priv) & 0x3) << 16) | \ + (__isel & 0xffff)) +#define AIA_IREG_ISEL (__ireg) ((__ireg) & 0xffff) +#define AIA_IREG_PRIV (__ireg) (((__ireg) >> 16) & 0x3) +#define AIA_IREG_VIRT (__ireg) (((__ireg) >> 18) & 0x1) +#define AIA_IREG_VGEIN (__ireg) (((__ireg) >> 20) & 0x3f) +#define AIA_IREG_XLEN (__ireg) (((__ireg) >> 24) & 0xff) +int (*aia_ireg_rmw_fn[4 ])(void *arg,target_ulong reg, +target_ulong *val,target_ulong new_val,target_ulong write_mask); +void *aia_ireg_rmw_fn_arg[4 ]; +/* True if in debugger mode. */ bool debugger;@@ -397,6 +413,13 @@ uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value);#define BOOL_TO_MASK (x) (-!!(x))/* helper for riscv_cpu_update_mip value */ void riscv_cpu_set_rdtime_fn (CPURISCVState *env,uint64_t (*fn)(uint32_t ),uint32_t arg); +void riscv_cpu_set_aia_ireg_rmw_fn (CPURISCVState *env,uint32_t priv, +int (*rmw_fn)(void *arg, +target_ulong reg, +target_ulong *val, +target_ulong new_val, +target_ulong write_mask), +void *rmw_fn_arg);#endif void
riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv);target/riscv/cpu_helper.c +14
-0@@ -395,6 +395,20 @@ void riscv_cpu_set_rdtime_fn(CPURISCVState *env,uint64_t (*fn)(uint32_t ), env->rdtime_fn_arg = arg; } +void riscv_cpu_set_aia_ireg_rmw_fn (CPURISCVState *env,uint32_t priv, +int (*rmw_fn)(void *arg, +target_ulong reg, +target_ulong *val, +target_ulong new_val, +target_ulong write_mask), +void *rmw_fn_arg) +{ +if (priv <=PRV_M ) { + env->aia_ireg_rmw_fn[priv] = rmw_fn; + env->aia_ireg_rmw_fn_arg[priv] = rmw_fn_arg; + } +} +void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv) {if (newpriv > PRV_M) {
Quoted 5 lines
--
2.25.1
PATCH v6 9/23 target/riscv: Implement AIA local interrupt priorities
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The AIA spec defines programmable 8-bit priority for each local interrupt at M-level, S-level and VS-level so we extend local interrupt processing to consider AIA interrupt priorities. The AIA CSRs which help software configure local interrupt priorities will be added by subsequent patches.
--- target/riscv/cpu.c | 19 ++++ target/riscv/cpu.h | 12 ++ target/riscv/cpu_helper.c | 231 ++++++++++++++++++++++++++++++++++---- target/riscv/machine.c | 3 + 4 files changed, 244 insertions(+), 21 deletions(-)
target/riscv/cpu.c +19 -0
@@ -348,6 +348,10 @@ void restore_state_to_opc(CPURISCVState *env, TranslationBlock *tb,static void riscv_cpu_reset (DeviceState *dev) { +#ifndef CONFIG_USER_ONLY +uint8_t iprio; +int i, irq, rdzero; +#endif CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (cs);RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (cpu);
@@ -370,6 +374,21 @@ static void riscv_cpu_reset(DeviceState *dev) env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false; + +/* Initialized default priorities of local interrupts. */ +for (i =0 ; i <ARRAY_SIZE (env->miprio); i++) { + iprio =riscv_cpu_default_priority (i); + env->miprio[i] = (i ==IRQ_M_EXT ) ?0 : iprio; + env->siprio[i] = (i ==IRQ_S_EXT ) ?0 : iprio; + env->hviprio[i] =0 ; + } + i =0 ; +while (!riscv_cpu_hviprio_index2irq (i, &irq, &rdzero)) { +if (!rdzero) { + env->hviprio[irq] = env->miprio[irq]; + } + i++; + }/* mmte is supposed to have pm.current hardwired to 1 */ env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif
target/riscv/cpu.h +12 -0
@@ -182,6 +182,10 @@ struct CPURISCVState {target_ulong mcause;target_ulong mtval;/* since: priv-1.10.0 */ +/* Machine and Supervisor interrupt priorities */ +uint8_t miprio[64 ]; +uint8_t siprio[64 ]; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;
@@ -194,6 +198,9 @@ struct CPURISCVState {target_ulong hgeip;uint64_t htimedelta; +/* Hypervisor controlled virtual interrupt priorities */ +uint8_t hviprio[64 ]; +/* Virtual CSRs */ /* * For RV32 this is 32-bit vsstatus and 32-bit vsstatush.
@@ -379,6 +386,11 @@ int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, int cpuid, void *opaque);int riscv_cpu_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg);int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg); +int riscv_cpu_hviprio_index2irq (int index,int *out_irq,int *out_rdzero); +uint8_t riscv_cpu_default_priority (int irq); +int riscv_cpu_mirq_pending (CPURISCVState *env); +int riscv_cpu_sirq_pending (CPURISCVState *env); +int riscv_cpu_vsirq_pending (CPURISCVState *env);bool riscv_cpu_fp_enabled (CPURISCVState *env);target_ulong riscv_cpu_get_geilen (CPURISCVState *env);void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen);
target/riscv/cpu_helper.c +210 -21
246 lines not on this page · raw message
target/riscv/machine.c +3 -0
@@ -92,6 +92,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), +VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ),VMSTATE_UINT64 (env.vsstatus,RISCVCPU ), VMSTATE_UINTTL(env.vstvec, RISCVCPU),
@@ -173,6 +174,8 @@ const VMStateDescription vmstate_riscv_cpu = { .fields = (VMStateField[]) {VMSTATE_UINTTL_ARRAY (env.gpr,RISCVCPU ,32 ),VMSTATE_UINT64_ARRAY (env.fpr,RISCVCPU ,32 ), +VMSTATE_UINT8_ARRAY (env.miprio,RISCVCPU ,64 ), +VMSTATE_UINT8_ARRAY (env.siprio,RISCVCPU ,64 ),VMSTATE_UINTTL (env.pc,RISCVCPU ),VMSTATE_UINTTL (env.load_res,RISCVCPU ),VMSTATE_UINTTL (env.load_val,RISCVCPU ),
Signature · 1 line
2.25.1
Re: [PATCH v6 09/23] target/riscv: Implement AIA local interrupt priorities
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:38寫道:
Quoted 245 lines
From: Anup Patel <[email protected]>
The AIA spec defines programmable 8-bit priority for each local interrupt at M-level, S-level and VS-level so we extend local interrupt processing to consider AIA interrupt priorities. The AIA CSRs which help software configure local interrupt priorities will be added by subsequent patches.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 19 ++++ target/riscv/cpu.h | 12 ++ target/riscv/cpu_helper.c | 231 ++++++++++++++++++++++++++++++++++---- target/riscv/machine.c | 3 + 4 files changed, 244 insertions(+), 21 deletions(-)target/riscv/cpu.c +4
-0@@ -348,6 +348,10 @@ void restore_state_to_opc(CPURISCVState *env,TranslationBlock *tb, static void riscv_cpu_reset(DeviceState *dev) { +#ifndef CONFIG_USER_ONLY + uint8_t iprio; +int i, irq, rdzero; +#endif CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (cs);RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu);target/riscv/cpu.c +15
-0@@ -370,6 +374,21 @@ static void riscv_cpu_reset(DeviceState *dev) env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false; + +/* Initialized default priorities of local interrupts. */ +for (i =0 ; i <ARRAY_SIZE (env->miprio); i++) { + iprio =riscv_cpu_default_priority (i); + env->miprio[i] = (i ==IRQ_M_EXT ) ?0 : iprio; + env->siprio[i] = (i ==IRQ_S_EXT ) ?0 : iprio; + env->hviprio[i] =0 ; + } + i =0 ; +while (!riscv_cpu_hviprio_index2irq (i, &irq, &rdzero)) { +if (!rdzero) { + env->hviprio[irq] = env->miprio[irq]; + } + i++; + }/* mmte is supposed to have pm.current hardwired to 1 */ env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif target/riscv/cpu.h +12
-0@@ -182,6 +182,10 @@ struct CPURISCVState {target_ulong mcause;target_ulong mtval;/* since: priv-1.10.0 */ +/* Machine and Supervisor interrupt priorities */ +uint8_t miprio[64 ]; +uint8_t siprio[64 ]; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;@@ -194,6 +198,9 @@ struct CPURISCVState {target_ulong hgeip;uint64_t htimedelta; +/* Hypervisor controlled virtual interrupt priorities */ +uint8_t hviprio[64 ]; +/* Virtual CSRs */ /* * For RV32 this is 32-bit vsstatus and 32-bit vsstatush. @@ -379,6 +386,11 @@ int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, int cpuid, void *opaque);int riscv_cpu_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg);int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg); +int riscv_cpu_hviprio_index2irq (int index,int *out_irq,int *out_rdzero); +uint8_t riscv_cpu_default_priority (int irq); +int riscv_cpu_mirq_pending (CPURISCVState *env); +int riscv_cpu_sirq_pending (CPURISCVState *env); +int riscv_cpu_vsirq_pending (CPURISCVState *env);bool riscv_cpu_fp_enabled (CPURISCVState *env);target_ulong riscv_cpu_get_geilen (CPURISCVState *env);void
riscv_cpu_set_geilen(CPURISCVState *env, target_ulong geilen);target/riscv/cpu_helper.c +116
-20@@ -151,36 +151,225 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, }#ifndef CONFIG_USER_ONLY -static int riscv_cpu_local_irq_pending (CPURISCVState *env) + +/* +* The HS-mode is allowed to configure priority only for the +* following VS-mode local interrupts: +* +* 0 (Reserved interrupt, reads as zero) +* 1 Supervisor software interrupt +* 4 (Reserved interrupt, reads as zero) +* 5 Supervisor timer interrupt +* 8 (Reserved interrupt, reads as zero) +* 13 (Reserved interrupt) +* 14 " +* 15 " +* 16 " +* 18 Debug/trace interrupt +* 20 (Reserved interrupt) +* 22 " +* 24 " +* 26 " +* 28 " +* 30 (Reserved for standard reporting of bus or system errors) +*/ + +static int hviprio_index2irq[] = + {0 ,1 ,4 ,5 ,8 ,13 ,14 ,15 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 }; +static int hviprio_index2rdzero[] = + {1 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; + +int riscv_cpu_hviprio_index2irq (int index,int *out_irq,int *out_rdzero) { -target_ulong virt_enabled =riscv_cpu_virt_enabled (env); +if (index <0 ||ARRAY_SIZE (hviprio_index2irq) <= index) { +return -EINVAL ; + } + +if (out_irq) { + *out_irq = hviprio_index2irq[index]; + } -target_ulong mstatus_mie =get_field (env->mstatus,MSTATUS_MIE ); -target_ulong mstatus_sie =get_field (env->mstatus,MSTATUS_SIE ); +if (out_rdzero) { + *out_rdzero = hviprio_index2rdzero[index]; + } -target_ulong vsgemask = - (target_ulong )1 <<get_field (env->hstatus,HSTATUS_VGEIN ); -target_ulong vsgein = (env->hgeip & vsgemask) ?MIP_VSEIP :0 ; +return 0 ; +} -target_ulong pending = (env->mip | vsgein) & env->mie; +uint8_t riscv_cpu_default_priority (int irq) +{ +int u, l; +uint8_t iprio =IPRIO_MMAXIPRIO ; -target_ulong mie = env->priv <PRV_M || - (env->priv ==PRV_M && mstatus_mie); -target_ulong sie = env->priv <PRV_S || - (env->priv ==PRV_S && mstatus_sie); -target_ulong hsie = virt_enabled || sie; -target_ulong vsie = virt_enabled && sie; +if (irq <0 || irq >63 ) { +return iprio; + } -target_ulong irqs = - (pending & ~env->mideleg & -mie) | - (pending & env->mideleg & ~env->hideleg & -hsie) | - (pending & env->mideleg & env->hideleg & -vsie); +/* +* Default priorities of local interrupts are defined in the +* RISC-V Advanced Interrupt Architecture specification. +* +* ---------------------------------------------------------------- +* Default | +* Priority | Major Interrupt Numbers +* ---------------------------------------------------------------- +* Highest | 63 (3f), 62 (3e), 31 (1f), 30 (1e), 61 (3d), 60 (3c), +* | 59 (3b), 58 (3a), 29 (1d), 28 (1c), 57 (39), 56 (38), +* | 55 (37), 54 (36), 27 (1b), 26 (1a), 53 (35), 52 (34), +* | 51 (33), 50 (32), 25 (19), 24 (18), 49 (31), 48 (30) +* | +* | 11 (0b), 3 (03), 7 (07) +* | 9 (09), 1 (01), 5 (05) +* | 12 (0c) +* | 10 (0a), 2 (02), 6 (06) +* | +* | 47 (2f), 46 (2e), 23 (17), 22 (16), 45 (2d), 44 (2c), +* | 43 (2b), 42 (2a), 21 (15), 20 (14), 41 (29), 40 (28), +* | 39 (27), 38 (26), 19 (13), 18 (12), 37 (25), 36 (24), +* Lowest | 35 (23), 34 (22), 17 (11), 16 (10), 33 (21), 32 (20) +* ---------------------------------------------------------------- +*/ -if (irqs) { -return ctz64 (irqs);/* since non-zero */ + u =IPRIO_DEFAULT_U (irq); + l =IPRIO_DEFAULT_L (irq); +if (u ==0 ) { +if (irq ==IRQ_VS_EXT || irq ==IRQ_VS_TIMER || + irq ==IRQ_VS_SOFT ) { + iprio =IPRIO_DEFAULT_VS ; + }else if (irq ==IRQ_S_GEXT ) { + iprio =IPRIO_DEFAULT_SGEXT ; + }else if (irq ==IRQ_S_EXT || irq ==IRQ_S_TIMER || + irq ==IRQ_S_SOFT ) { + iprio =IPRIO_DEFAULT_S ; + }else if (irq ==IRQ_M_EXT || irq ==IRQ_M_TIMER || + irq ==IRQ_M_SOFT ) { + iprio =IPRIO_DEFAULT_M ; + }else { + iprio =IPRIO_DEFAULT_VS ; + } + }else if (u ==1 ) { +if (l <8 ) { + iprio =IPRIO_DEFAULT_16_23 (irq); + }else { + iprio =IPRIO_DEFAULT_24_31 (irq); + } + }else if (u ==2 ) { + iprio =IPRIO_DEFAULT_32_47 (irq); + }else if (u ==3 ) { + iprio =IPRIO_DEFAULT_48_63 (irq); + } + +return iprio; +} + +static int riscv_cpu_pending_to_irq (CPURISCVState *env, +uint64_t pending,uint8_t *iprio) +{ +int irq, best_irq =RISCV_EXCP_NONE ; +unsigned int prio, best_prio =UINT_MAX ; + +if (!pending) { +return RISCV_EXCP_NONE ; + } + + irq =ctz64 (pending); +if (!riscv_feature (env,RISCV_FEATURE_AIA )) {
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 287
Quoted 10 lines
target/riscv/cpu_helper.c +9
-0+return irq; + } + + pending = pending >> irq; +while (pending) { + prio = iprio[irq]; +if (!prio) { + prio = (riscv_cpu_default_priority (irq) <IPRIO_DEFAULT_M ) ? +1 :IPRIO_MMAXIPRIO ;
Hi Anup,
I have a question regarding configuring priorities of major interrupts.
riscv_cpu_default_priority() for M-mode external interrupt would return IPRIO_DEFAULT_M (8).
but the comparison here is < IPRIO_DEFAULT_M,
so if M-mode external interrupt is pending, the prio would be assigned to IPRIO_MMAXIPRIO (255).
Is this the expected behavior?
Also, why is IPRIO_DEFAULT_M been compared?
Should IPRIO_DEFAULT_S and IPRIO_DEFAULT_VS be used for S-mode and VS-mode interrupts?
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 3 lines
target/riscv/cpu_helper.c +2
-0+ } +if ((pending &0x1 ) && (prio < best_prio)) {
If both interrupt 63 and 62 are pending-and-enabled,
with their iprio values both assigned to zero.
Interrupt 62 would be picked, because best_prio will be 1 when interrupt 62 is been checked.
As the default priority order for interrupt 63 is also less than IPRIO_DEFAULT_M (8),
so prio would be assigned to 1 as well.
But as prio is not less than best_prio, so best_irq would still be interrupt 62.
However, according to the default priority table in AIA spec, interrupt 63 should have a higher priority than interrupt 62:
Interrupt 63 has the highest default priority, and interrupt 32 has the
lowest default priority.
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 10 lines
target/riscv/cpu_helper.c +9
-0+ best_irq = irq; + best_prio = prio; + } + irq++; + pending = pending >>1 ; + } + +return best_irq; +}
Yet I have another question for major interrupts priorities.
According to AIA spec (v0.2-draft.28):
==========
The priority number for a machine-level external interrupt (bits 31:24 of register iprio2)
must also be read-only zero.
When an interrupt’s priority number in the array is zero (either read-only zero or set to zero),
its priority is the default order from Section 6.1.
Setting an interrupt’s priority number instead to a nonzero value p gives that interrupt nominally
the same priority as a machine-level external interrupt with priority number p.
For a major interrupt that defaults to a higher priority than machine external interrupts,
setting its priority number to a nonzero value lowers its priority. For a major interrupt that defaults to a lower priority than machine external interrupts,
setting its priority number to a nonzero value raises its priority.
==========
From the above sentences,
The M-mode external interrupt has iprio value of 0, so the default priority order is applied.
What are the priorities for the major interrupt interrupts which are higher/lower than
M-mode external interrupt when their iprio values are not 0?
Which interrupt will be picked when:
M-mode external interrupt is pending-and-enabled,
Interrupt with higher default priority than M-mode external interrupt is
pending-and-enabled, and
Interrupt with lower default priority than M-mode external interrupt is
pending-and-enabled/
From the above riscv_cpu_pending_to_irq(),
If both interrupt 63 and interrupt 18's iprio values are assigned to the value of 10,
and interrupt 63, interrupt 18 and M-mode external interrupts are pending-and-enabled.
Interrupt 18 would be picked.
Is this the correct expectation?
I'm a bit confused with the correct priority order of the interrupts with/without non-zero value iprio and the default priority.
Thanks,
Frank Chang
Quoted 109 lines
target/riscv/cpu_helper.c +46
-1+ +static uint64_t riscv_cpu_all_pending(CPURISCVState *env) +{ + uint32_t gein = get_field(env->hstatus, HSTATUS_VGEIN); + uint64_t vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0; + + return (env->mip | vsgein) & env->mie; +} + +int riscv_cpu_mirq_pending(CPURISCVState *env) +{ + uint64_t irqs = riscv_cpu_all_pending(env) & ~env->mideleg & + ~(MIP_SGEIP | MIP_VSSIP | MIP_VSTIP | MIP_VSEIP); + + return riscv_cpu_pending_to_irq(env, irqs, env->miprio); +} + +int riscv_cpu_sirq_pending(CPURISCVState *env) +{ + uint64_t irqs = riscv_cpu_all_pending(env) & env->mideleg & + ~(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP); + + return riscv_cpu_pending_to_irq(env, irqs, env->siprio); +} + +int riscv_cpu_vsirq_pending(CPURISCVState *env) +{ + uint64_t irqs = riscv_cpu_all_pending(env) & env->mideleg & + (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP); + + return riscv_cpu_pending_to_irq(env, irqs >> 1, env->hviprio); +} + +static int riscv_cpu_local_irq_pending(CPURISCVState *env) +{ + int virq; + uint64_t irqs, pending, mie, hsie, vsie; + + /* Determine interrupt enable state of all privilege modes */ + if (riscv_cpu_virt_enabled(env)) { + mie = 1; + hsie = 1; + vsie = (env->priv < PRV_S) || + (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_SIE)); } else { -return RISCV_EXCP_NONE ;/* indicates no pending interrupt */ + mie = (env->priv < PRV_M) || + (env->priv == PRV_M && get_field(env->mstatus,
MSTATUS_MIE));target/riscv/cpu_helper.c +2
-0+ hsie = (env->priv <PRV_S ) || + (env->priv == PRV_S && get_field(env->mstatus,
MSTATUS_SIE));target/riscv/cpu_helper.c +26
-0+ vsie =0 ; } + +/* Determine all pending interrupts */ + pending =riscv_cpu_all_pending (env); + +/* Check M-mode interrupts */ + irqs = pending & ~env->mideleg & -mie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); + } + +/* Check HS-mode interrupts */ + irqs = pending & env->mideleg & ~env->hideleg & -hsie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); + } + +/* Check VS-mode interrupts */ + irqs = pending & env->mideleg & env->hideleg & -vsie; +if (irqs) { + virq =riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +return (virq <=0 ) ? virq : virq +1 ; + } + +/* Indicate no pending interrupt */ +return RISCV_EXCP_NONE ; }bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request)target/riscv/machine.c +3
-1@@ -92,6 +92,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), +VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ),VMSTATE_UINT64 (env.vsstatus,RISCVCPU ), VMSTATE_UINTTL(env.vstvec, RISCVCPU),@@ -173,6 +174,8 @@ const VMStateDescription vmstate_riscv_cpu = { .fields = (VMStateField[]) {VMSTATE_UINTTL_ARRAY (env.gpr,RISCVCPU ,32 ),VMSTATE_UINT64_ARRAY (env.fpr,RISCVCPU ,32 ), +VMSTATE_UINT8_ARRAY (env.miprio,RISCVCPU ,64 ), +VMSTATE_UINT8_ARRAY (env.siprio,RISCVCPU ,64 ),VMSTATE_UINTTL (env.pc,RISCVCPU ),VMSTATE_UINTTL (env.load_res,RISCVCPU ),VMSTATE_UINTTL (env.load_val,RISCVCPU ), --2.25.1
Re: [PATCH v6 09/23] target/riscv: Implement AIA local interrupt priorities
Quoted 20 lines · Frank Chang
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:38寫道:
Quoted 18 lines
From: Anup Patel <[email protected]>
The AIA spec defines programmable 8-bit priority for each local interrupt at M-level, S-level and VS-level so we extend local interrupt processing to consider AIA interrupt priorities. The AIA CSRs which help software configure local interrupt priorities will be added by subsequent patches.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 19 ++++ target/riscv/cpu.h | 12 ++ target/riscv/cpu_helper.c | 231 ++++++++++++++++++++++++++++++++++---- target/riscv/machine.c | 3 + 4 files changed, 244 insertions(+), 21 deletions(-)
Quoted 5 lines
target/riscv/cpu.c void restore_state_to_opc(CPURISCVState *env,
Quoted 5 lines
target/riscv/cpu.c +0
-0@@ -348,6 +348,10 @@ void restore_state_to_opc(CPURISCVState *env,
TranslationBlock *tb,
Quoted 51 lines
Quoted 51 lines
target/riscv/cpu.c +19
-0static void riscv_cpu_reset (DeviceState *dev) { +#ifndef CONFIG_USER_ONLY +uint8_t iprio; +int i, irq, rdzero; +#endif CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (cs);RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (cpu);@@ -370,6 +374,21 @@ static void riscv_cpu_reset(DeviceState *dev) env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false; + +/* Initialized default priorities of local interrupts. */ +for (i =0 ; i <ARRAY_SIZE (env->miprio); i++) { + iprio =riscv_cpu_default_priority (i); + env->miprio[i] = (i ==IRQ_M_EXT ) ?0 : iprio; + env->siprio[i] = (i ==IRQ_S_EXT ) ?0 : iprio; + env->hviprio[i] =0 ; + } + i =0 ; +while (!riscv_cpu_hviprio_index2irq (i, &irq, &rdzero)) { +if (!rdzero) { + env->hviprio[irq] = env->miprio[irq]; + } + i++; + }/* mmte is supposed to have pm.current hardwired to 1 */ env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif target/riscv/cpu.h +4
-0@@ -182,6 +182,10 @@ struct CPURISCVState {target_ulong mcause;target_ulong mtval;/* since: priv-1.10.0 */ +/* Machine and Supervisor interrupt priorities */ +uint8_t miprio[64 ]; +uint8_t siprio[64 ]; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;@@ -194,6 +198,9 @@ struct CPURISCVState {target_ulong hgeip;uint64_t htimedelta;
Quoted 7 lines
target/riscv/cpu.h int
Quoted 7 lines
target/riscv/cpu.h +3
-0+/* Hypervisor controlled virtual interrupt priorities */ +uint8_t hviprio[64 ]; +/* Virtual CSRs */ /* * For RV32 this is 32-bit vsstatus and 32-bit vsstatush. @@ -379,6 +386,11 @@ int
riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
Quoted 2 lines
target/riscv/cpu.h int · line 387
Quoted 2 lines
target/riscv/cpu.h +0
-0int cpuid, void *opaque); int riscv_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int
reg);
Quoted 2 lines
target/riscv/cpu.h int · line 389
Quoted 2 lines
target/riscv/cpu.h +1
-0int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg); +int riscv_cpu_hviprio_index2irq(int index, int *out_irq, int
*out_rdzero);
Quoted 4 lines
Quoted 4 lines
target/riscv/cpu.h +4
-0+uint8_t riscv_cpu_default_priority (int irq); +int riscv_cpu_mirq_pending (CPURISCVState *env); +int riscv_cpu_sirq_pending (CPURISCVState *env); +int riscv_cpu_vsirq_pending (CPURISCVState *env);
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env,
Quoted 8 lines
target/riscv/cpu.h +0
-0bool riscv_cpu_fp_enabled (CPURISCVState *env);target_ulong riscv_cpu_get_geilen (CPURISCVState *env);void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen);target/riscv/cpu_helper.c +0
-0@@ -151,36 +151,225 @@ void cpu_get_tb_cpu_state(CPURISCVState *env,
target_ulong *pc,
Quoted 25 lines
Quoted 25 lines
target/riscv/cpu_helper.c +21
-1}#ifndef CONFIG_USER_ONLY -static int riscv_cpu_local_irq_pending (CPURISCVState *env) + +/* +* The HS-mode is allowed to configure priority only for the +* following VS-mode local interrupts: +* +* 0 (Reserved interrupt, reads as zero) +* 1 Supervisor software interrupt +* 4 (Reserved interrupt, reads as zero) +* 5 Supervisor timer interrupt +* 8 (Reserved interrupt, reads as zero) +* 13 (Reserved interrupt) +* 14 " +* 15 " +* 16 " +* 18 Debug/trace interrupt +* 20 (Reserved interrupt) +* 22 " +* 24 " +* 26 " +* 28 " +* 30 (Reserved for standard reporting of bus or system errors)
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+*/ + +static int hviprio_index2irq[] = + {0 ,1 ,4 ,5 ,8 ,13 ,14 ,15 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 }; +static int hviprio_index2rdzero[] = + {1 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; + +int riscv_cpu_hviprio_index2irq(int index, int *out_irq, int
*out_rdzero)
Quoted 10 lines
Quoted 10 lines
target/riscv/cpu_helper.c +7
-1{ -target_ulong virt_enabled =riscv_cpu_virt_enabled (env); +if (index <0 ||ARRAY_SIZE (hviprio_index2irq) <= index) { +return -EINVAL ; + } + +if (out_irq) { + *out_irq = hviprio_index2irq[index]; + }
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +3
-4-target_ulong mstatus_mie =get_field (env->mstatus,MSTATUS_MIE ); -target_ulong mstatus_sie =get_field (env->mstatus,MSTATUS_SIE ); +if (out_rdzero) { + *out_rdzero = hviprio_index2rdzero[index]; + } -target_ulong vsgemask = - (target_ulong)1 << get_field(env->hstatus,
HSTATUS_VGEIN);
Quoted 108 lines
Quoted 94 lines
target/riscv/cpu_helper.c +76
-14-target_ulong vsgein = (env->hgeip & vsgemask) ?MIP_VSEIP :0 ; +return 0 ; +} -target_ulong pending = (env->mip | vsgein) & env->mie; +uint8_t riscv_cpu_default_priority (int irq) +{ +int u, l; +uint8_t iprio =IPRIO_MMAXIPRIO ; -target_ulong mie = env->priv <PRV_M || - (env->priv ==PRV_M && mstatus_mie); -target_ulong sie = env->priv <PRV_S || - (env->priv ==PRV_S && mstatus_sie); -target_ulong hsie = virt_enabled || sie; -target_ulong vsie = virt_enabled && sie; +if (irq <0 || irq >63 ) { +return iprio; + } -target_ulong irqs = - (pending & ~env->mideleg & -mie) | - (pending & env->mideleg & ~env->hideleg & -hsie) | - (pending & env->mideleg & env->hideleg & -vsie); +/* +* Default priorities of local interrupts are defined in the +* RISC-V Advanced Interrupt Architecture specification. +* +* ---------------------------------------------------------------- +* Default | +* Priority | Major Interrupt Numbers +* ---------------------------------------------------------------- +* Highest | 63 (3f), 62 (3e), 31 (1f), 30 (1e), 61 (3d), 60 (3c), +* | 59 (3b), 58 (3a), 29 (1d), 28 (1c), 57 (39), 56 (38), +* | 55 (37), 54 (36), 27 (1b), 26 (1a), 53 (35), 52 (34), +* | 51 (33), 50 (32), 25 (19), 24 (18), 49 (31), 48 (30) +* | +* | 11 (0b), 3 (03), 7 (07) +* | 9 (09), 1 (01), 5 (05) +* | 12 (0c) +* | 10 (0a), 2 (02), 6 (06) +* | +* | 47 (2f), 46 (2e), 23 (17), 22 (16), 45 (2d), 44 (2c), +* | 43 (2b), 42 (2a), 21 (15), 20 (14), 41 (29), 40 (28), +* | 39 (27), 38 (26), 19 (13), 18 (12), 37 (25), 36 (24), +* Lowest | 35 (23), 34 (22), 17 (11), 16 (10), 33 (21), 32 (20) +* ---------------------------------------------------------------- +*/ -if (irqs) { -return ctz64 (irqs);/* since non-zero */ + u =IPRIO_DEFAULT_U (irq); + l =IPRIO_DEFAULT_L (irq); +if (u ==0 ) { +if (irq ==IRQ_VS_EXT || irq ==IRQ_VS_TIMER || + irq ==IRQ_VS_SOFT ) { + iprio =IPRIO_DEFAULT_VS ; + }else if (irq ==IRQ_S_GEXT ) { + iprio =IPRIO_DEFAULT_SGEXT ; + }else if (irq ==IRQ_S_EXT || irq ==IRQ_S_TIMER || + irq ==IRQ_S_SOFT ) { + iprio =IPRIO_DEFAULT_S ; + }else if (irq ==IRQ_M_EXT || irq ==IRQ_M_TIMER || + irq ==IRQ_M_SOFT ) { + iprio =IPRIO_DEFAULT_M ; + }else { + iprio =IPRIO_DEFAULT_VS ; + } + }else if (u ==1 ) { +if (l <8 ) { + iprio =IPRIO_DEFAULT_16_23 (irq); + }else { + iprio =IPRIO_DEFAULT_24_31 (irq); + } + }else if (u ==2 ) { + iprio =IPRIO_DEFAULT_32_47 (irq); + }else if (u ==3 ) { + iprio =IPRIO_DEFAULT_48_63 (irq); + } + +return iprio; +} + +static int riscv_cpu_pending_to_irq (CPURISCVState *env, +uint64_t pending,uint8_t *iprio) +{ +int irq, best_irq =RISCV_EXCP_NONE ; +unsigned int prio, best_prio =UINT_MAX ; + +if (!pending) { +return RISCV_EXCP_NONE ; + } + + irq =ctz64 (pending);target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 285
Quoted 10 lines
target/riscv/cpu_helper.c +10
-0+if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return irq; + } + + pending = pending >> irq; +while (pending) { + prio = iprio[irq]; +if (!prio) { + prio = (riscv_cpu_default_priority (irq) <IPRIO_DEFAULT_M ) ? +1 :IPRIO_MMAXIPRIO ;Hi Anup,
Quoted 3 lines
I have a question regarding configuring priorities of major interrupts.
riscv_cpu_default_priority()for M-mode external interrupt would return
IPRIO_DEFAULT_M (8).
Quoted 2 lines
but the comparison here is <
IPRIO_DEFAULT_M,
so if M-mode external interrupt is pending, the prio would be assigned to
IPRIO_MMAXIPRIO (255).
Quoted 1 line
Is this the expected behavior?
Actually, for irq == IRQ_M_EXT the prio should be IPRIO_DEFAULT_M same for irq == IRQ_S_EXT.
Goot catch, I will fix this in next revision.
Quoted 3 lines
Also, why is
IPRIO_DEFAULT_Mbeen compared?
ShouldIPRIO_DEFAULT_SandIPRIO_DEFAULT_VSbe used for S-mode and
VS-mode interrupts?
For M-mode, we should compare IPRIO_DEFAULT_M. For S/VS-mode, we should compare IPRIO_DEFAULT_S but the priority registers used for S-mode and VS-mode are different.
Quoted 9 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 287
Quoted 4 lines
target/riscv/cpu_helper.c +2
-0+ } +if ((pending &0x1 ) && (prio < best_prio)) {If both interrupt 63 and 62 are pending-and-enabled,
with their iprio values both assigned to zero.
Interrupt 62 would be picked, becausebest_priowill be 1 when interrupt
62 is been checked.
Quoted 1 line
As the default priority order for interrupt 63 is also less than
IPRIO_DEFAULT_M (8),
Quoted 2 lines
so prio would be assigned to 1 as well.
But as prio is not less thanbest_prio, sobest_irqwould still be
interrupt 62.
Quoted 4 lines
However, according to the default priority table in AIA spec, interrupt 63 should have a higher priority than interrupt 62:
Interrupt 63 has the highest default priority, and interrupt 32 has the
lowest default priority.
Argh, the comparision "prio < best_prio" should have been "prio <= best_prio".
I am certainly aware of the fact that if two interrupts have same priority then higher interrupt number should be preferred. This is silly mistake on my part.
I really appreciate your detailed review. Thanks.
Quoted 15 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 296
Quoted 11 lines
target/riscv/cpu_helper.c +9
-0+ best_irq = irq; + best_prio = prio; + } + irq++; + pending = pending >>1 ; + } + +return best_irq; +}Yet I have another question for major interrupts priorities.
Quoted 3 lines
According to AIA spec (v0.2-draft.28):
==========
The priority number for a machine-level external interrupt (bits 31:24 of
register iprio2)
Quoted 3 lines
must also be read-only zero.
When an interrupt’s priority number in the array is zero (either
read-only zero or set to zero),
Quoted 2 lines
its priority is the default order from Section 6.1.
Setting an interrupt’s priority number instead to a nonzero value p gives
that interrupt nominally
Quoted 1 line
the same priority as a machine-level external interrupt with priority
number p.
Quoted 1 line
For a major interrupt that defaults to a higher priority than machine
external interrupts,
Quoted 2 lines
setting its priority number to a nonzero value lowers its priority. For a major interrupt that defaults to a lower priority than machine
external interrupts,
Quoted 5 lines
setting its priority number to a nonzero value raises its priority.
==========From the above sentences,
The M-mode external interrupt has iprio value of 0, so the default
priority order is applied.
Yes, that's correct and same logic applies to S/VS-mode external interrupts.
Quoted 2 lines
What are the priorities for the major interrupt interrupts which are
higher/lower than
Quoted 1 line
M-mode external interrupt when their iprio values are not 0?
For a major interrupt X (which is not an external interrupt) having iprio value 0,
we take iprio of X as 1 or 255 based of comparison of default priority of X with default priority of external interrupt.
(Refer, documentation of "mtopi" (Section 6.3.2) and "stopi" (Section 6.5.2))
Quoted 4 lines
Which interrupt will be picked when:
M-mode external interrupt is pending-and-enabled, Interrupt with higher default priority than M-mode external interrupt
is pending-and-enabled, and
Quoted 1 line
Interrupt with lower default priority than M-mode external interrupt is
pending-and-enabled/
Quoted 3 lines
From the above
riscv_cpu_pending_to_irq(),
If both interrupt 63 and interrupt 18's iprio values are assigned to the
value of 10,
You mean both these interrupt have iprio 0, right ?
Quoted 1 line
and interrupt 63, interrupt 18 and M-mode external interrupts are
pending-and-enabled.
Quoted 2 lines
Interrupt 18 would be picked.
Is this the correct expectation?
If both 63 and 18 have iprio 0 then interrupt 63 would be picked because 63 has higher default priority compared to default priorities of both M-mode external interrupt and interrupt 18.
Quoted 1 line
I'm a bit confused with the correct priority order of the interrupts
with/without non-zero value iprio and the default priority.
Whenever iprio of an interrupt X is 0, the default priority of interrupt X compared against default external interrupt priority and based on this comparison 1 (highest) or 255 (lowest) priority is used for interrupt X.
In other, a saturated priority is used on interrupt X when it's iprio is set to zero.
Regards,
Anup
Quoted 41 lines
Thanks,
Frank ChangQuoted 37 lines
target/riscv/cpu_helper.c +36
-0+ +static uint64_t riscv_cpu_all_pending (CPURISCVState *env) +{ +uint32_t gein =get_field (env->hstatus,HSTATUS_VGEIN ); +uint64_t vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ; + +return (env->mip | vsgein) & env->mie; +} + +int riscv_cpu_mirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & ~env->mideleg & + ~(MIP_SGEIP |MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); +} + +int riscv_cpu_sirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & env->mideleg & + ~(MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); +} + +int riscv_cpu_vsirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & env->mideleg & + (MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +} + +static int riscv_cpu_local_irq_pending (CPURISCVState *env) +{ +int virq;
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+uint64_t irqs, pending, mie, hsie, vsie; + +/* Determine interrupt enable state of all privilege modes */ + if (riscv_cpu_virt_enabled(env)) { + mie = 1; + hsie = 1; + vsie = (env->priv < PRV_S) || + (env->priv == PRV_S && get_field(env->mstatus,
MSTATUS_SIE));
Quoted 4 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 4 lines
target/riscv/cpu_helper.c +2
-1} else { -return RISCV_EXCP_NONE ;/* indicates no pending interrupt */ + mie = (env->priv < PRV_M) || + (env->priv == PRV_M && get_field(env->mstatus,
MSTATUS_MIE));
Quoted 2 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 345
Quoted 2 lines
target/riscv/cpu_helper.c +2
-0+ hsie = (env->priv <PRV_S ) || + (env->priv == PRV_S && get_field(env->mstatus,
MSTATUS_SIE));
Quoted 55 lines
Quoted 55 lines
target/riscv/cpu_helper.c +26
-0+ vsie =0 ; } + +/* Determine all pending interrupts */ + pending =riscv_cpu_all_pending (env); + +/* Check M-mode interrupts */ + irqs = pending & ~env->mideleg & -mie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); + } + +/* Check HS-mode interrupts */ + irqs = pending & env->mideleg & ~env->hideleg & -hsie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); + } + +/* Check VS-mode interrupts */ + irqs = pending & env->mideleg & env->hideleg & -vsie; +if (irqs) { + virq =riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +return (virq <=0 ) ? virq : virq +1 ; + } + +/* Indicate no pending interrupt */ +return RISCV_EXCP_NONE ; }bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request)target/riscv/machine.c +3
-1@@ -92,6 +92,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), +VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ),VMSTATE_UINT64 (env.vsstatus,RISCVCPU ), VMSTATE_UINTTL(env.vstvec, RISCVCPU),@@ -173,6 +174,8 @@ const VMStateDescription vmstate_riscv_cpu = { .fields = (VMStateField[]) {VMSTATE_UINTTL_ARRAY (env.gpr,RISCVCPU ,32 ),VMSTATE_UINT64_ARRAY (env.fpr,RISCVCPU ,32 ), +VMSTATE_UINT8_ARRAY (env.miprio,RISCVCPU ,64 ), +VMSTATE_UINT8_ARRAY (env.siprio,RISCVCPU ,64 ),VMSTATE_UINTTL (env.pc,RISCVCPU ),VMSTATE_UINTTL (env.load_res,RISCVCPU ),VMSTATE_UINTTL (env.load_val,RISCVCPU ), --2.25.1
Re: [PATCH v6 09/23] target/riscv: Implement AIA local interrupt priorities
Quoted 392 lines · Anup Patel
Quoted 6 lines · Frank Chang
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:38寫道:
Quoted 4 lines
From: Anup Patel <[email protected]>
The AIA spec defines programmable 8-bit priority for each local
interrupt
Quoted 3 lines
Quoted 3 lines
at M-level, S-level and VS-level so we extend local interrupt processing to consider AIA interrupt priorities. The AIA CSRs which help software configure local interrupt priorities will be added by subsequent
patches.
Quoted 11 lines
Quoted 11 lines
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 19 ++++ target/riscv/cpu.h | 12 ++ target/riscv/cpu_helper.c | 231 ++++++++++++++++++++++++++++++++++---- target/riscv/machine.c | 3 + 4 files changed, 244 insertions(+), 21 deletions(-)Quoted 5 lines
target/riscv/cpu.c void restore_state_to_opc(CPURISCVState *env,
Quoted 5 lines
target/riscv/cpu.c +0
-0@@ -348,6 +348,10 @@ void restore_state_to_opc(CPURISCVState *env,TranslationBlock *tb,
Quoted 51 lines
Quoted 51 lines
target/riscv/cpu.c +19
-0static void riscv_cpu_reset (DeviceState *dev) { +#ifndef CONFIG_USER_ONLY +uint8_t iprio; +int i, irq, rdzero; +#endif CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (cs);RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (cpu);@@ -370,6 +374,21 @@ static void riscv_cpu_reset(DeviceState *dev) env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false; + +/* Initialized default priorities of local interrupts. */ +for (i =0 ; i <ARRAY_SIZE (env->miprio); i++) { + iprio =riscv_cpu_default_priority (i); + env->miprio[i] = (i ==IRQ_M_EXT ) ?0 : iprio; + env->siprio[i] = (i ==IRQ_S_EXT ) ?0 : iprio; + env->hviprio[i] =0 ; + } + i =0 ; +while (!riscv_cpu_hviprio_index2irq (i, &irq, &rdzero)) { +if (!rdzero) { + env->hviprio[irq] = env->miprio[irq]; + } + i++; + }/* mmte is supposed to have pm.current hardwired to 1 */ env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif target/riscv/cpu.h +4
-0@@ -182,6 +182,10 @@ struct CPURISCVState {target_ulong mcause;target_ulong mtval;/* since: priv-1.10.0 */ +/* Machine and Supervisor interrupt priorities */ +uint8_t miprio[64 ]; +uint8_t siprio[64 ]; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;@@ -194,6 +198,9 @@ struct CPURISCVState {target_ulong hgeip;uint64_t htimedelta;Quoted 7 lines
target/riscv/cpu.h int
Quoted 7 lines
target/riscv/cpu.h +3
-0+/* Hypervisor controlled virtual interrupt priorities */ +uint8_t hviprio[64 ]; +/* Virtual CSRs */ /* * For RV32 this is 32-bit vsstatus and 32-bit vsstatush. @@ -379,6 +386,11 @@ intriscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
Quoted 2 lines
target/riscv/cpu.h int · line 387
Quoted 2 lines
target/riscv/cpu.h +0
-0int cpuid, void *opaque); int riscv_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, intreg);
Quoted 2 lines
target/riscv/cpu.h int · line 389
Quoted 2 lines
target/riscv/cpu.h +1
-0int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg); +int riscv_cpu_hviprio_index2irq(int index, int *out_irq, int
*out_rdzero);Quoted 4 lines
Quoted 4 lines
target/riscv/cpu.h +4
-0+uint8_t riscv_cpu_default_priority (int irq); +int riscv_cpu_mirq_pending (CPURISCVState *env); +int riscv_cpu_sirq_pending (CPURISCVState *env); +int riscv_cpu_vsirq_pending (CPURISCVState *env);Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env,
Quoted 8 lines
target/riscv/cpu.h +0
-0bool riscv_cpu_fp_enabled (CPURISCVState *env);target_ulong riscv_cpu_get_geilen (CPURISCVState *env);void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen);target/riscv/cpu_helper.c +0
-0@@ -151,36 +151,225 @@ void cpu_get_tb_cpu_state(CPURISCVState *env,
target_ulong*pc,Quoted 25 lines
Quoted 25 lines
target/riscv/cpu_helper.c +21
-1}#ifndef CONFIG_USER_ONLY -static int riscv_cpu_local_irq_pending (CPURISCVState *env) + +/* +* The HS-mode is allowed to configure priority only for the +* following VS-mode local interrupts: +* +* 0 (Reserved interrupt, reads as zero) +* 1 Supervisor software interrupt +* 4 (Reserved interrupt, reads as zero) +* 5 Supervisor timer interrupt +* 8 (Reserved interrupt, reads as zero) +* 13 (Reserved interrupt) +* 14 " +* 15 " +* 16 " +* 18 Debug/trace interrupt +* 20 (Reserved interrupt) +* 22 " +* 24 " +* 26 " +* 28 " +* 30 (Reserved for standard reporting of bus or system errors) Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+*/ + +static int hviprio_index2irq[] = + {0 ,1 ,4 ,5 ,8 ,13 ,14 ,15 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 }; +static int hviprio_index2rdzero[] = + {1 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; + +int riscv_cpu_hviprio_index2irq(int index, int *out_irq, int
*out_rdzero)Quoted 10 lines
Quoted 10 lines
target/riscv/cpu_helper.c +7
-1{ -target_ulong virt_enabled =riscv_cpu_virt_enabled (env); +if (index <0 ||ARRAY_SIZE (hviprio_index2irq) <= index) { +return -EINVAL ; + } + +if (out_irq) { + *out_irq = hviprio_index2irq[index]; + }Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +3
-4-target_ulong mstatus_mie =get_field (env->mstatus,MSTATUS_MIE ); -target_ulong mstatus_sie =get_field (env->mstatus,MSTATUS_SIE ); +if (out_rdzero) { + *out_rdzero = hviprio_index2rdzero[index]; + } -target_ulong vsgemask = - (target_ulong)1 << get_field(env->hstatus,
HSTATUS_VGEIN);Quoted 20 lines
Quoted 20 lines
target/riscv/cpu_helper.c +9
-8-target_ulong vsgein = (env->hgeip & vsgemask) ?MIP_VSEIP :0 ; +return 0 ; +} -target_ulong pending = (env->mip | vsgein) & env->mie; +uint8_t riscv_cpu_default_priority (int irq) +{ +int u, l; +uint8_t iprio =IPRIO_MMAXIPRIO ; -target_ulong mie = env->priv <PRV_M || - (env->priv ==PRV_M && mstatus_mie); -target_ulong sie = env->priv <PRV_S || - (env->priv ==PRV_S && mstatus_sie); -target_ulong hsie = virt_enabled || sie; -target_ulong vsie = virt_enabled && sie; +if (irq <0 || irq >63 ) { +return iprio; + }Quoted 13 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 13 lines
target/riscv/cpu_helper.c +9
-4-target_ulong irqs = - (pending & ~env->mideleg & -mie) | - (pending & env->mideleg & ~env->hideleg & -hsie) | - (pending & env->mideleg & env->hideleg & -vsie); +/* +* Default priorities of local interrupts are defined in the +* RISC-V Advanced Interrupt Architecture specification. +* +* ---------------------------------------------------------------- +* Default | +* Priority | Major Interrupt Numbers +* ---------------------------------------------------------------- +* Highest | 63 (3f), 62 (3e), 31 (1f), 30 (1e), 61 (3d), 60 (3c),
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 217
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 59 (3b), 58 (3a), 29 (1d), 28 (1c), 57 (39), 56 (38),
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 218
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 55 (37), 54 (36), 27 (1b), 26 (1a), 53 (35), 52 (34),
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+* | 51 (33), 50 (32), 25 (19), 24 (18), 49 (31), 48 (30) +* | +* | 11 (0b), 3 (03), 7 (07) +* | 9 (09), 1 (01), 5 (05) +* | 12 (0c) +* | 10 (0a), 2 (02), 6 (06) +* | +* | 47 (2f), 46 (2e), 23 (17), 22 (16), 45 (2d), 44 (2c),
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 227
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 43 (2b), 42 (2a), 21 (15), 20 (14), 41 (29), 40 (28),
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 228
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 39 (27), 38 (26), 19 (13), 18 (12), 37 (25), 36 (24),
Quoted 50 lines
Quoted 50 lines
target/riscv/cpu_helper.c +47
-2+* Lowest | 35 (23), 34 (22), 17 (11), 16 (10), 33 (21), 32 (20) +* ---------------------------------------------------------------- +*/ -if (irqs) { -return ctz64 (irqs);/* since non-zero */ + u =IPRIO_DEFAULT_U (irq); + l =IPRIO_DEFAULT_L (irq); +if (u ==0 ) { +if (irq ==IRQ_VS_EXT || irq ==IRQ_VS_TIMER || + irq ==IRQ_VS_SOFT ) { + iprio =IPRIO_DEFAULT_VS ; + }else if (irq ==IRQ_S_GEXT ) { + iprio =IPRIO_DEFAULT_SGEXT ; + }else if (irq ==IRQ_S_EXT || irq ==IRQ_S_TIMER || + irq ==IRQ_S_SOFT ) { + iprio =IPRIO_DEFAULT_S ; + }else if (irq ==IRQ_M_EXT || irq ==IRQ_M_TIMER || + irq ==IRQ_M_SOFT ) { + iprio =IPRIO_DEFAULT_M ; + }else { + iprio =IPRIO_DEFAULT_VS ; + } + }else if (u ==1 ) { +if (l <8 ) { + iprio =IPRIO_DEFAULT_16_23 (irq); + }else { + iprio =IPRIO_DEFAULT_24_31 (irq); + } + }else if (u ==2 ) { + iprio =IPRIO_DEFAULT_32_47 (irq); + }else if (u ==3 ) { + iprio =IPRIO_DEFAULT_48_63 (irq); + } + +return iprio; +} + +static int riscv_cpu_pending_to_irq (CPURISCVState *env, +uint64_t pending,uint8_t *iprio) +{ +int irq, best_irq =RISCV_EXCP_NONE ; +unsigned int prio, best_prio =UINT_MAX ; + +if (!pending) { +return RISCV_EXCP_NONE ; + } + + irq =ctz64 (pending); +if (!riscv_feature (env,RISCV_FEATURE_AIA )) {Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+return irq; + } + + pending = pending >> irq; +while (pending) { + prio = iprio[irq]; +if (!prio) { + prio = (riscv_cpu_default_priority (irq) <IPRIO_DEFAULT_M )?
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 285
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+ 1 : IPRIO_MMAXIPRIO;Hi Anup,
I have a question regarding configuring priorities of major interrupts.
riscv_cpu_default_priority()for M-mode external interrupt would return
IPRIO_DEFAULT_M(8).Quoted 2 lines
but the comparison here is <
IPRIO_DEFAULT_M,
so if M-mode external interrupt is pending, the prio would be assignedto
IPRIO_MMAXIPRIO(255).Quoted 1 line
Is this the expected behavior?
Actually, for irq ==
IRQ_M_EXTthe prio should beIPRIO_DEFAULT_Msame for irq ==IRQ_S_EXT.Goot catch, I will fix this in next revision.
Quoted 3 lines
Also, why is
IPRIO_DEFAULT_Mbeen compared?
ShouldIPRIO_DEFAULT_SandIPRIO_DEFAULT_VSbe used for S-mode andVS-mode interrupts?
For M-mode, we should compare
IPRIO_DEFAULT_M. For S/VS-mode, we should compareIPRIO_DEFAULT_Sbut the priority registers used for S-mode and VS-mode are different.Quoted 9 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 287
Quoted 4 lines
target/riscv/cpu_helper.c +2
-0+ } +if ((pending &0x1 ) && (prio < best_prio)) {If both interrupt 63 and 62 are pending-and-enabled,
with their iprio values both assigned to zero.
Interrupt 62 would be picked, becausebest_priowill be 1 when interrupt62 is been checked.
Quoted 1 line
As the default priority order for interrupt 63 is also less than
IPRIO_DEFAULT_M(8),Quoted 2 lines
so prio would be assigned to 1 as well.
But as prio is not less thanbest_prio, sobest_irqwould still beinterrupt 62.
Quoted 4 lines
However, according to the default priority table in AIA spec, interrupt 63 should have a higher priority than interrupt 62:
Interrupt 63 has the highest default priority, and interrupt 32 hasthe lowest default priority.
Argh, the comparision "prio <
best_prio" should have been "prio <=best_prio".I am certainly aware of the fact that if two interrupts have same priority then higher interrupt number should be preferred. This is silly mistake on my part.
I really appreciate your detailed review. Thanks.
Quoted 15 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 296
Quoted 11 lines
target/riscv/cpu_helper.c +9
-0+ best_irq = irq; + best_prio = prio; + } + irq++; + pending = pending >>1 ; + } + +return best_irq; +}Yet I have another question for major interrupts priorities.
Quoted 3 lines
According to AIA spec (v0.2-draft.28):
==========
The priority number for a machine-level external interrupt (bits 31:24of register iprio2)
Quoted 3 lines
must also be read-only zero.
When an interrupt’s priority number in the array is zero (either
read-only zero or set to zero),
Quoted 2 lines
its priority is the default order from Section 6.1. Setting an interrupt’s priority number instead to a nonzero value p
gives that interrupt nominally
Quoted 1 line
the same priority as a machine-level external interrupt with priority
number p.
Quoted 1 line
For a major interrupt that defaults to a higher priority than machine
external interrupts,
Quoted 2 lines
setting its priority number to a nonzero value lowers its priority. For a major interrupt that defaults to a lower priority than machine
external interrupts,
Quoted 5 lines
setting its priority number to a nonzero value raises its priority.
==========From the above sentences,
The M-mode external interrupt has iprio value of 0, so the defaultpriority order is applied.
Yes, that's correct and same logic applies to S/VS-mode external interrupts.
Quoted 2 lines
What are the priorities for the major interrupt interrupts which are
higher/lower than
Quoted 1 line
M-mode external interrupt when their iprio values are not 0?
For a major interrupt X (which is not an external interrupt) having iprio value 0,
we take iprio of X as 1 or 255 based of comparison of default priority of X with default priority of external interrupt.
(Refer, documentation of "mtopi" (Section 6.3.2) and "stopi" (Section 6.5.2))Quoted 4 lines
Which interrupt will be picked when:
M-mode external interrupt is pending-and-enabled, Interrupt with higher default priority than M-mode external interruptis pending-and-enabled, and
Quoted 9 lines
Quoted 1 line
Interrupt with lower default priority than M-mode external interrupt
is pending-and-enabled/
Quoted 3 lines
From the above
riscv_cpu_pending_to_irq(),
If both interrupt 63 and interrupt 18's iprio values are assigned to thevalue of 10,
You mean both these interrupt have iprio 0, right ?
I mean if both interrupt 63 and interrupt 18's iprio values are assigned with value 10.
If interrupt 63, interrupt 18, and M-mode external interrupt are pending-and-enabled.
Will M-mode external interrupt be picked as it has the default priority: 8, which is less than 10 of interrupt 63 and interrupt 18?
What if, interrupt 63 has iprio value of 10 and interrupt 18 has iprio value of 6.
If interrupt 63, interrupt 18, and M-mode external interrupt are pending-and-enabled.
Will interrupt 18 be picked because it has the smallest iprio value (i.e. higher interrupt priority)?
Can I say that for M-mode major interrupts:
If interrupt X's iprio value is zero, its interrupt priority will be 1 or 255,
base on its default priority which is higher (1) or less (255) than M-mode external interrupt.
And if interrupt X's iprio value is not zero, its interrupt priority will be the assigned iprio value.
So it's possible for a lower default priority interrupt (e.g. 18) to have higher priority than M-mode external interrupt
if its iprio value is less than 8?
The overall interrupt priority order is compared between the values of:
* If iprio value is zero: 1 or 255
* If iprio value is not zero: the iprio value been assigned
* M-mode external interrupt: the default priority value: 8?
and the interrupt with the smallest value will be picked.
Quoted 12 lines
Quoted 1 line
and interrupt 63, interrupt 18 and M-mode external interrupts are
pending-and-enabled.
Quoted 2 lines
Interrupt 18 would be picked.
Is this the correct expectation?If both 63 and 18 have iprio 0 then interrupt 63 would be picked because 63 has higher default priority compared to default priorities of both M-mode external interrupt and interrupt 18.
Quoted 1 line
I'm a bit confused with the correct priority order of the interrupts
with/without non-zero value iprio and the default priority.
Quoted 8 lines
Whenever iprio of an interrupt X is 0, the default priority of interrupt X compared against default external interrupt priority and based on this comparison 1 (highest) or 255 (lowest) priority is used for interrupt X.
In other, a saturated priority is used on interrupt X when it's iprio is set to zero.
Thanks for the detailed explanation.
Regards,
Frank Chang
Quoted 118 lines
Regards,
AnupQuoted 41 lines
Thanks,
Frank ChangQuoted 37 lines
target/riscv/cpu_helper.c +36
-0+ +static uint64_t riscv_cpu_all_pending (CPURISCVState *env) +{ +uint32_t gein =get_field (env->hstatus,HSTATUS_VGEIN ); +uint64_t vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ; + +return (env->mip | vsgein) & env->mie; +} + +int riscv_cpu_mirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & ~env->mideleg & + ~(MIP_SGEIP |MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); +} + +int riscv_cpu_sirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & env->mideleg & + ~(MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); +} + +int riscv_cpu_vsirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & env->mideleg & + (MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +} + +static int riscv_cpu_local_irq_pending (CPURISCVState *env) +{ +int virq;Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+uint64_t irqs, pending, mie, hsie, vsie; + +/* Determine interrupt enable state of all privilege modes */ + if (riscv_cpu_virt_enabled(env)) { + mie = 1; + hsie = 1; + vsie = (env->priv < PRV_S) || + (env->priv == PRV_S && get_field(env->mstatus,
MSTATUS_SIE));Quoted 4 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 4 lines
target/riscv/cpu_helper.c +2
-1} else { -return RISCV_EXCP_NONE ;/* indicates no pending interrupt */ + mie = (env->priv < PRV_M) || + (env->priv == PRV_M && get_field(env->mstatus,
MSTATUS_MIE));Quoted 2 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 345
Quoted 2 lines
target/riscv/cpu_helper.c +2
-0+ hsie = (env->priv <PRV_S ) || + (env->priv == PRV_S && get_field(env->mstatus,
MSTATUS_SIE));Quoted 55 lines
Quoted 55 lines
target/riscv/cpu_helper.c +26
-0+ vsie =0 ; } + +/* Determine all pending interrupts */ + pending =riscv_cpu_all_pending (env); + +/* Check M-mode interrupts */ + irqs = pending & ~env->mideleg & -mie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); + } + +/* Check HS-mode interrupts */ + irqs = pending & env->mideleg & ~env->hideleg & -hsie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); + } + +/* Check VS-mode interrupts */ + irqs = pending & env->mideleg & env->hideleg & -vsie; +if (irqs) { + virq =riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +return (virq <=0 ) ? virq : virq +1 ; + } + +/* Indicate no pending interrupt */ +return RISCV_EXCP_NONE ; }bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request)target/riscv/machine.c +3
-1@@ -92,6 +92,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), +VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ),VMSTATE_UINT64 (env.vsstatus,RISCVCPU ), VMSTATE_UINTTL(env.vstvec, RISCVCPU),@@ -173,6 +174,8 @@ const VMStateDescription vmstate_riscv_cpu = { .fields = (VMStateField[]) {VMSTATE_UINTTL_ARRAY (env.gpr,RISCVCPU ,32 ),VMSTATE_UINT64_ARRAY (env.fpr,RISCVCPU ,32 ), +VMSTATE_UINT8_ARRAY (env.miprio,RISCVCPU ,64 ), +VMSTATE_UINT8_ARRAY (env.siprio,RISCVCPU ,64 ),VMSTATE_UINTTL (env.pc,RISCVCPU ),VMSTATE_UINTTL (env.load_res,RISCVCPU ),VMSTATE_UINTTL (env.load_val,RISCVCPU ), --2.25.1
Re: [PATCH v6 09/23] target/riscv: Implement AIA local interrupt priorities
Quoted 364 lines · Frank Chang
Quoted 362 lines · Anup Patel
Quoted 258 lines · Frank Chang
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:38寫道:
Quoted 242 lines
From: Anup Patel <[email protected]>
The AIA spec defines programmable 8-bit priority for each local interrupt at M-level, S-level and VS-level so we extend local interrupt processing to consider AIA interrupt priorities. The AIA CSRs which help software configure local interrupt priorities will be added by subsequent patches.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 19 ++++ target/riscv/cpu.h | 12 ++ target/riscv/cpu_helper.c | 231 ++++++++++++++++++++++++++++++++++---- target/riscv/machine.c | 3 + 4 files changed, 244 insertions(+), 21 deletions(-)target/riscv/cpu.c +19
-0@@ -348,6 +348,10 @@ void restore_state_to_opc(CPURISCVState *env, TranslationBlock *tb,static void riscv_cpu_reset (DeviceState *dev) { +#ifndef CONFIG_USER_ONLY +uint8_t iprio; +int i, irq, rdzero; +#endif CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (cs);RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (cpu);@@ -370,6 +374,21 @@ static void riscv_cpu_reset(DeviceState *dev) env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false; + +/* Initialized default priorities of local interrupts. */ +for (i =0 ; i <ARRAY_SIZE (env->miprio); i++) { + iprio =riscv_cpu_default_priority (i); + env->miprio[i] = (i ==IRQ_M_EXT ) ?0 : iprio; + env->siprio[i] = (i ==IRQ_S_EXT ) ?0 : iprio; + env->hviprio[i] =0 ; + } + i =0 ; +while (!riscv_cpu_hviprio_index2irq (i, &irq, &rdzero)) { +if (!rdzero) { + env->hviprio[irq] = env->miprio[irq]; + } + i++; + }/* mmte is supposed to have pm.current hardwired to 1 */ env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif target/riscv/cpu.h +12
-0@@ -182,6 +182,10 @@ struct CPURISCVState {target_ulong mcause;target_ulong mtval;/* since: priv-1.10.0 */ +/* Machine and Supervisor interrupt priorities */ +uint8_t miprio[64 ]; +uint8_t siprio[64 ]; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;@@ -194,6 +198,9 @@ struct CPURISCVState {target_ulong hgeip;uint64_t htimedelta; +/* Hypervisor controlled virtual interrupt priorities */ +uint8_t hviprio[64 ]; +/* Virtual CSRs */ /* * For RV32 this is 32-bit vsstatus and 32-bit vsstatush. @@ -379,6 +386,11 @@ int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, int cpuid, void *opaque);int riscv_cpu_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg);int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg); +int riscv_cpu_hviprio_index2irq (int index,int *out_irq,int *out_rdzero); +uint8_t riscv_cpu_default_priority (int irq); +int riscv_cpu_mirq_pending (CPURISCVState *env); +int riscv_cpu_sirq_pending (CPURISCVState *env); +int riscv_cpu_vsirq_pending (CPURISCVState *env);bool riscv_cpu_fp_enabled (CPURISCVState *env);target_ulong riscv_cpu_get_geilen (CPURISCVState *env);void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen);target/riscv/cpu_helper.c +115
-20@@ -151,36 +151,225 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, }#ifndef CONFIG_USER_ONLY -static int riscv_cpu_local_irq_pending (CPURISCVState *env) + +/* +* The HS-mode is allowed to configure priority only for the +* following VS-mode local interrupts: +* +* 0 (Reserved interrupt, reads as zero) +* 1 Supervisor software interrupt +* 4 (Reserved interrupt, reads as zero) +* 5 Supervisor timer interrupt +* 8 (Reserved interrupt, reads as zero) +* 13 (Reserved interrupt) +* 14 " +* 15 " +* 16 " +* 18 Debug/trace interrupt +* 20 (Reserved interrupt) +* 22 " +* 24 " +* 26 " +* 28 " +* 30 (Reserved for standard reporting of bus or system errors) +*/ + +static int hviprio_index2irq[] = + {0 ,1 ,4 ,5 ,8 ,13 ,14 ,15 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 }; +static int hviprio_index2rdzero[] = + {1 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; + +int riscv_cpu_hviprio_index2irq (int index,int *out_irq,int *out_rdzero) { -target_ulong virt_enabled =riscv_cpu_virt_enabled (env); +if (index <0 ||ARRAY_SIZE (hviprio_index2irq) <= index) { +return -EINVAL ; + } + +if (out_irq) { + *out_irq = hviprio_index2irq[index]; + } -target_ulong mstatus_mie =get_field (env->mstatus,MSTATUS_MIE ); -target_ulong mstatus_sie =get_field (env->mstatus,MSTATUS_SIE ); +if (out_rdzero) { + *out_rdzero = hviprio_index2rdzero[index]; + } -target_ulong vsgemask = - (target_ulong )1 <<get_field (env->hstatus,HSTATUS_VGEIN ); -target_ulong vsgein = (env->hgeip & vsgemask) ?MIP_VSEIP :0 ; +return 0 ; +} -target_ulong pending = (env->mip | vsgein) & env->mie; +uint8_t riscv_cpu_default_priority (int irq) +{ +int u, l; +uint8_t iprio =IPRIO_MMAXIPRIO ; -target_ulong mie = env->priv <PRV_M || - (env->priv ==PRV_M && mstatus_mie); -target_ulong sie = env->priv <PRV_S || - (env->priv ==PRV_S && mstatus_sie); -target_ulong hsie = virt_enabled || sie; -target_ulong vsie = virt_enabled && sie; +if (irq <0 || irq >63 ) { +return iprio; + } -target_ulong irqs = - (pending & ~env->mideleg & -mie) | - (pending & env->mideleg & ~env->hideleg & -hsie) | - (pending & env->mideleg & env->hideleg & -vsie); +/* +* Default priorities of local interrupts are defined in the +* RISC-V Advanced Interrupt Architecture specification. +* +* ---------------------------------------------------------------- +* Default | +* Priority | Major Interrupt Numbers +* ---------------------------------------------------------------- +* Highest | 63 (3f), 62 (3e), 31 (1f), 30 (1e), 61 (3d), 60 (3c), +* | 59 (3b), 58 (3a), 29 (1d), 28 (1c), 57 (39), 56 (38), +* | 55 (37), 54 (36), 27 (1b), 26 (1a), 53 (35), 52 (34), +* | 51 (33), 50 (32), 25 (19), 24 (18), 49 (31), 48 (30) +* | +* | 11 (0b), 3 (03), 7 (07) +* | 9 (09), 1 (01), 5 (05) +* | 12 (0c) +* | 10 (0a), 2 (02), 6 (06) +* | +* | 47 (2f), 46 (2e), 23 (17), 22 (16), 45 (2d), 44 (2c), +* | 43 (2b), 42 (2a), 21 (15), 20 (14), 41 (29), 40 (28), +* | 39 (27), 38 (26), 19 (13), 18 (12), 37 (25), 36 (24), +* Lowest | 35 (23), 34 (22), 17 (11), 16 (10), 33 (21), 32 (20) +* ---------------------------------------------------------------- +*/ -if (irqs) { -return ctz64 (irqs);/* since non-zero */ + u =IPRIO_DEFAULT_U (irq); + l =IPRIO_DEFAULT_L (irq); +if (u ==0 ) { +if (irq ==IRQ_VS_EXT || irq ==IRQ_VS_TIMER || + irq ==IRQ_VS_SOFT ) { + iprio =IPRIO_DEFAULT_VS ; + }else if (irq ==IRQ_S_GEXT ) { + iprio =IPRIO_DEFAULT_SGEXT ; + }else if (irq ==IRQ_S_EXT || irq ==IRQ_S_TIMER || + irq ==IRQ_S_SOFT ) { + iprio =IPRIO_DEFAULT_S ; + }else if (irq ==IRQ_M_EXT || irq ==IRQ_M_TIMER || + irq ==IRQ_M_SOFT ) { + iprio =IPRIO_DEFAULT_M ; + }else { + iprio =IPRIO_DEFAULT_VS ; + } + }else if (u ==1 ) { +if (l <8 ) { + iprio =IPRIO_DEFAULT_16_23 (irq); + }else { + iprio =IPRIO_DEFAULT_24_31 (irq); + } + }else if (u ==2 ) { + iprio =IPRIO_DEFAULT_32_47 (irq); + }else if (u ==3 ) { + iprio =IPRIO_DEFAULT_48_63 (irq); + } + +return iprio; +} + +static int riscv_cpu_pending_to_irq (CPURISCVState *env, +uint64_t pending,uint8_t *iprio) +{ +int irq, best_irq =RISCV_EXCP_NONE ; +unsigned int prio, best_prio =UINT_MAX ; + +if (!pending) { +return RISCV_EXCP_NONE ; + } + + irq =ctz64 (pending);target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 285
Quoted 10 lines
target/riscv/cpu_helper.c +10
-0+if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return irq; + } + + pending = pending >> irq; +while (pending) { + prio = iprio[irq]; +if (!prio) { + prio = (riscv_cpu_default_priority (irq) <IPRIO_DEFAULT_M ) ? +1 :IPRIO_MMAXIPRIO ;Hi Anup,
Quoted 6 lines
I have a question regarding configuring priorities of major interrupts.
riscv_cpu_default_priority()for M-mode external interrupt would returnIPRIO_DEFAULT_M(8). but the comparison here is <IPRIO_DEFAULT_M,
so if M-mode external interrupt is pending, the prio would be assigned toIPRIO_MMAXIPRIO(255). Is this the expected behavior?Actually, for irq ==
IRQ_M_EXTthe prio should beIPRIO_DEFAULT_Msame for irq ==IRQ_S_EXT.Goot catch, I will fix this in next revision.
Quoted 3 lines
Also, why is
IPRIO_DEFAULT_Mbeen compared?
ShouldIPRIO_DEFAULT_SandIPRIO_DEFAULT_VSbe used for S-mode and VS-mode interrupts?For M-mode, we should compare
IPRIO_DEFAULT_M. For S/VS-mode, we should compareIPRIO_DEFAULT_Sbut the priority registers used for S-mode and VS-mode are different.Quoted 13 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 287
Quoted 4 lines
target/riscv/cpu_helper.c +2
-0+ } +if ((pending &0x1 ) && (prio < best_prio)) {If both interrupt 63 and 62 are pending-and-enabled,
with their iprio values both assigned to zero.
Interrupt 62 would be picked, becausebest_priowill be 1 when interrupt 62 is been checked. As the default priority order for interrupt 63 is also less thanIPRIO_DEFAULT_M(8), so prio would be assigned to 1 as well.
But as prio is not less thanbest_prio, sobest_irqwould still be interrupt 62.Quoted 3 lines
However, according to the default priority table in AIA spec,
interrupt 63 should have a higher priority than interrupt 62:
Interrupt 63 has the highest default priority, and interrupt 32 has the lowest default priority.Argh, the comparision "prio <
best_prio" should have been "prio <=best_prio".I am certainly aware of the fact that if two interrupts have same priority then higher interrupt number should be preferred. This is silly mistake on my part.
I really appreciate your detailed review. Thanks.
Quoted 30 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 296
Quoted 11 lines
target/riscv/cpu_helper.c +9
-0+ best_irq = irq; + best_prio = prio; + } + irq++; + pending = pending >>1 ; + } + +return best_irq; +}Yet I have another question for major interrupts priorities.
According to AIA spec (v0.2-draft.28):
==========
The priority number for a machine-level external interrupt (bits 31:24 of register iprio2) must also be read-only zero.When an interrupt’s priority number in the array is zero (either read-only zero or set to zero), its priority is the default order from Section 6.1.
Setting an interrupt’s priority number instead to a nonzero value p gives that interrupt nominally the same priority as a machine-level external interrupt with priority number p. For a major interrupt that defaults to a higher priority than machine external interrupts, setting its priority number to a nonzero value lowers its priority.
For a major interrupt that defaults to a lower priority than machine external interrupts, setting its priority number to a nonzero value raises its priority.
==========Quoted 2 lines
From the above sentences,
The M-mode external interrupt has iprio value of 0, so the default priority order is applied.Yes, that's correct and same logic applies to S/VS-mode external interrupts.
Quoted 3 lines
What are the priorities for the major interrupt interrupts which are higher/lower than M-mode external interrupt when their iprio values are not 0?
For a major interrupt X (which is not an external interrupt) having iprio value 0, we take iprio of X as 1 or 255 based of comparison of default priority of X with default priority of external interrupt.
(Refer, documentation of "mtopi" (Section 6.3.2) and "stopi" (Section 6.5.2))Quoted 8 lines
Which interrupt will be picked when:
M-mode external interrupt is pending-and-enabled,
Interrupt with higher default priority than M-mode external interrupt is pending-and-enabled, and Interrupt with lower default priority than M-mode external interrupt is pending-and-enabled/From the above
riscv_cpu_pending_to_irq(),
If both interrupt 63 and interrupt 18's iprio values are assigned to the value of 10,
Quoted 7 lines
Quoted 1 line
You mean both these interrupt have iprio 0, right ?
I mean if both interrupt 63 and interrupt 18's iprio values are assigned with value 10. If interrupt 63, interrupt 18, and M-mode external interrupt are pending-and-enabled. Will M-mode external interrupt be picked as it has the default priority: 8, which is less than 10 of interrupt 63 and interrupt 18?
Yes, in this case "mtopi" CSR read will return M-mode external interrupt.
Quoted 4 lines
What if, interrupt 63 has iprio value of 10 and interrupt 18 has iprio value of 6.
If interrupt 63, interrupt 18, and M-mode external interrupt are pending-and-enabled. Will interrupt 18 be picked because it has the smallest iprio value (i.e. higher interrupt priority)?
Yes, in this case "mtopi" CSR read will return interrupt 18.
Quoted 5 lines
Can I say that for M-mode major interrupts:
If interrupt X's iprio value is zero, its interrupt priority will be 1 or 255, base on its default priority which is higher (1) or less (255) than M-mode external interrupt.
Yes, this is correct form M-mode (and "mtopi" CSR) perspective.
For S-mode (and "stopi" or "vstopi" CSR) perspective, it will be compared against S-mode external interrupt default priority. (Note: I will fix this in next revision)
Quoted 2 lines
And if interrupt X's iprio value is not zero, its interrupt priority will be the assigned iprio value.
Yes, this is correct.
Quoted 2 lines
So it's possible for a lower default priority interrupt (e.g. 18) to have higher priority than M-mode external interrupt if its iprio value is less than 8?
Yes, this is correct. The fact that iprio of major interrupts are programmable it allow software to override the default priority assignement.
Quoted 4 lines
The overall interrupt priority order is compared between the values of:
* If iprio value is zero: 1 or 255
* If iprio value is not zero: the iprio value been assigned
This two statements are correct.
Quoted 2 lines
* M-mode external interrupt: the default priority value: 8?
and the interrupt with the smallest value will be picked.
I would like to clarify that it is QEMU implementation that has chosen to assign default priorities in 1 to 15 range hence we have M-mode external interrupt default priority as 8. Some other implementation may chose a different default priority assignments but it should comply with the table in the "Section 6.1" of the AIA specification.
Regards,
Anup
Quoted 141 lines
Quoted 12 lines
Quoted 5 lines
and interrupt 63, interrupt 18 and M-mode external interrupts are pending-and-enabled. Interrupt 18 would be picked.
Is this the correct expectation?If both 63 and 18 have iprio 0 then interrupt 63 would be picked because 63 has higher default priority compared to default priorities of both M-mode external interrupt and interrupt 18.
Quoted 1 line
I'm a bit confused with the correct priority order of the interrupts with/without non-zero value iprio and the default priority.
Quoted 7 lines
Whenever iprio of an interrupt X is 0, the default priority of interrupt X compared against default external interrupt priority and based on this comparison 1 (highest) or 255 (lowest) priority is used for interrupt X.
In other, a saturated priority is used on interrupt X when it's iprio is set to zero.
Thanks for the detailed explanation.
Regards,
Frank ChangQuoted 115 lines
Regards,
AnupQuoted 110 lines
Thanks,
Frank ChangQuoted 106 lines
target/riscv/cpu_helper.c +74
-1+ +static uint64_t riscv_cpu_all_pending (CPURISCVState *env) +{ +uint32_t gein =get_field (env->hstatus,HSTATUS_VGEIN ); +uint64_t vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ; + +return (env->mip | vsgein) & env->mie; +} + +int riscv_cpu_mirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & ~env->mideleg & + ~(MIP_SGEIP |MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); +} + +int riscv_cpu_sirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & env->mideleg & + ~(MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); +} + +int riscv_cpu_vsirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & env->mideleg & + (MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +} + +static int riscv_cpu_local_irq_pending (CPURISCVState *env) +{ +int virq; +uint64_t irqs, pending, mie, hsie, vsie; + +/* Determine interrupt enable state of all privilege modes */ +if (riscv_cpu_virt_enabled (env)) { + mie =1 ; + hsie =1 ; + vsie = (env->priv <PRV_S ) || + (env->priv ==PRV_S &&get_field (env->mstatus,MSTATUS_SIE )); }else { -return RISCV_EXCP_NONE ;/* indicates no pending interrupt */ + mie = (env->priv <PRV_M ) || + (env->priv ==PRV_M &&get_field (env->mstatus,MSTATUS_MIE )); + hsie = (env->priv <PRV_S ) || + (env->priv ==PRV_S &&get_field (env->mstatus,MSTATUS_SIE )); + vsie =0 ; } + +/* Determine all pending interrupts */ + pending =riscv_cpu_all_pending (env); + +/* Check M-mode interrupts */ + irqs = pending & ~env->mideleg & -mie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); + } + +/* Check HS-mode interrupts */ + irqs = pending & env->mideleg & ~env->hideleg & -hsie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); + } + +/* Check VS-mode interrupts */ + irqs = pending & env->mideleg & env->hideleg & -vsie; +if (irqs) { + virq =riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +return (virq <=0 ) ? virq : virq +1 ; + } + +/* Indicate no pending interrupt */ +return RISCV_EXCP_NONE ; }bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request)target/riscv/machine.c +3
-1@@ -92,6 +92,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), +VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ),VMSTATE_UINT64 (env.vsstatus,RISCVCPU ), VMSTATE_UINTTL(env.vstvec, RISCVCPU),@@ -173,6 +174,8 @@ const VMStateDescription vmstate_riscv_cpu = { .fields = (VMStateField[]) {VMSTATE_UINTTL_ARRAY (env.gpr,RISCVCPU ,32 ),VMSTATE_UINT64_ARRAY (env.fpr,RISCVCPU ,32 ), +VMSTATE_UINT8_ARRAY (env.miprio,RISCVCPU ,64 ), +VMSTATE_UINT8_ARRAY (env.siprio,RISCVCPU ,64 ),VMSTATE_UINTTL (env.pc,RISCVCPU ),VMSTATE_UINTTL (env.load_res,RISCVCPU ),VMSTATE_UINTTL (env.load_val,RISCVCPU ), --2.25.1
Re: [PATCH v6 09/23] target/riscv: Implement AIA local interrupt priorities
Quoted 484 lines · Anup Patel
Quoted 6 lines · Frank Chang
Quoted 4 lines · Anup Patel
On Mon, Jan 10, 2022 at 6:38 PM Frank Chang <[email protected]>
Quoted 6 lines · wrote:
Quoted 6 lines
Quoted 6 lines
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:38寫道:
Quoted 4 lines
From: Anup Patel <[email protected]>
The AIA spec defines programmable 8-bit priority for each local
interrupt
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
at M-level, S-level and VS-level so we extend local interrupt
processing
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
to consider AIA interrupt priorities. The AIA CSRs which help
software
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
configure local interrupt priorities will be added by subsequent
patches.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 19 ++++ target/riscv/cpu.h | 12 ++ target/riscv/cpu_helper.c | 231++++++++++++++++++++++++++++++++++----
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/cpu.c void restore_state_to_opc(CPURISCVState *env,
Quoted 8 lines
target/riscv/machine.c | 3 + 4 files changed, 244 insertions(+), 21 deletions(-)target/riscv/cpu.c +0
-0@@ -348,6 +348,10 @@ void restore_state_to_opc(CPURISCVState *env,TranslationBlock *tb,
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
target/riscv/cpu.c +19
-0static void riscv_cpu_reset (DeviceState *dev) { +#ifndef CONFIG_USER_ONLY +uint8_t iprio; +int i, irq, rdzero; +#endif CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (cs);RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (cpu);@@ -370,6 +374,21 @@ static void riscv_cpu_reset(DeviceState *dev) env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false; + +/* Initialized default priorities of local interrupts. */ +for (i =0 ; i <ARRAY_SIZE (env->miprio); i++) { + iprio =riscv_cpu_default_priority (i); + env->miprio[i] = (i ==IRQ_M_EXT ) ?0 : iprio; + env->siprio[i] = (i ==IRQ_S_EXT ) ?0 : iprio; + env->hviprio[i] =0 ; + } + i =0 ; +while (!riscv_cpu_hviprio_index2irq (i, &irq, &rdzero)) { +if (!rdzero) { + env->hviprio[irq] = env->miprio[irq]; + } + i++; + }/* mmte is supposed to have pm.current hardwired to 1 */ env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif target/riscv/cpu.h +4
-0@@ -182,6 +182,10 @@ struct CPURISCVState {target_ulong mcause;target_ulong mtval;/* since: priv-1.10.0 */ +/* Machine and Supervisor interrupt priorities */ +uint8_t miprio[64 ]; +uint8_t siprio[64 ]; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;@@ -194,6 +198,9 @@ struct CPURISCVState {target_ulong hgeip;uint64_t htimedelta;Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
target/riscv/cpu.h int
Quoted 7 lines
target/riscv/cpu.h +3
-0+/* Hypervisor controlled virtual interrupt priorities */ +uint8_t hviprio[64 ]; +/* Virtual CSRs */ /* * For RV32 this is 32-bit vsstatus and 32-bit vsstatush. @@ -379,6 +386,11 @@ intriscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
target/riscv/cpu.h int · line 387
Quoted 2 lines
target/riscv/cpu.h +0
-0int cpuid, void *opaque); int riscv_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, intreg);
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu.h int · line 388
Quoted 1 line
target/riscv/cpu.h +0
-0int riscv_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, intreg);
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu.h int · line 389
Quoted 1 line
target/riscv/cpu.h +1
-0+int riscv_cpu_hviprio_index2irq(int index, int *out_irq, int
*out_rdzero);Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
target/riscv/cpu.h +4
-0+uint8_t riscv_cpu_default_priority (int irq); +int riscv_cpu_mirq_pending (CPURISCVState *env); +int riscv_cpu_sirq_pending (CPURISCVState *env); +int riscv_cpu_vsirq_pending (CPURISCVState *env);Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env,
Quoted 8 lines
target/riscv/cpu.h +0
-0bool riscv_cpu_fp_enabled (CPURISCVState *env);target_ulong riscv_cpu_get_geilen (CPURISCVState *env);void riscv_cpu_set_geilen (CPURISCVState *env,target_ulong geilen);target/riscv/cpu_helper.c +0
-0@@ -151,36 +151,225 @@ void cpu_get_tb_cpu_state(CPURISCVState *env,
target_ulong*pc,Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
target/riscv/cpu_helper.c +21
-1}#ifndef CONFIG_USER_ONLY -static int riscv_cpu_local_irq_pending (CPURISCVState *env) + +/* +* The HS-mode is allowed to configure priority only for the +* following VS-mode local interrupts: +* +* 0 (Reserved interrupt, reads as zero) +* 1 Supervisor software interrupt +* 4 (Reserved interrupt, reads as zero) +* 5 Supervisor timer interrupt +* 8 (Reserved interrupt, reads as zero) +* 13 (Reserved interrupt) +* 14 " +* 15 " +* 16 " +* 18 Debug/trace interrupt +* 20 (Reserved interrupt) +* 22 " +* 24 " +* 26 " +* 28 " +* 30 (Reserved for standard reporting of bus or system errors) Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+*/ + +static int hviprio_index2irq[] = + {0 ,1 ,4 ,5 ,8 ,13 ,14 ,15 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 }; +static int hviprio_index2rdzero[] = + {1 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; + +int riscv_cpu_hviprio_index2irq(int index, int *out_irq, int
*out_rdzero)Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
target/riscv/cpu_helper.c +7
-1{ -target_ulong virt_enabled =riscv_cpu_virt_enabled (env); +if (index <0 ||ARRAY_SIZE (hviprio_index2irq) <= index) { +return -EINVAL ; + } + +if (out_irq) { + *out_irq = hviprio_index2irq[index]; + }Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +3
-4-target_ulong mstatus_mie =get_field (env->mstatus,MSTATUS_MIE ); -target_ulong mstatus_sie =get_field (env->mstatus,MSTATUS_SIE ); +if (out_rdzero) { + *out_rdzero = hviprio_index2rdzero[index]; + } -target_ulong vsgemask = - (target_ulong)1 << get_field(env->hstatus,
HSTATUS_VGEIN);Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
target/riscv/cpu_helper.c +9
-8-target_ulong vsgein = (env->hgeip & vsgemask) ?MIP_VSEIP :0 ; +return 0 ; +} -target_ulong pending = (env->mip | vsgein) & env->mie; +uint8_t riscv_cpu_default_priority (int irq) +{ +int u, l; +uint8_t iprio =IPRIO_MMAXIPRIO ; -target_ulong mie = env->priv <PRV_M || - (env->priv ==PRV_M && mstatus_mie); -target_ulong sie = env->priv <PRV_S || - (env->priv ==PRV_S && mstatus_sie); -target_ulong hsie = virt_enabled || sie; -target_ulong vsie = virt_enabled && sie; +if (irq <0 || irq >63 ) { +return iprio; + }Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 9 lines
target/riscv/cpu_helper.c +5
-4-target_ulong irqs = - (pending & ~env->mideleg & -mie) | - (pending & env->mideleg & ~env->hideleg & -hsie) | - (pending & env->mideleg & env->hideleg & -vsie); +/* +* Default priorities of local interrupts are defined in the +* RISC-V Advanced Interrupt Architecture specification. +* +* ----------------------------------------------------------------
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 3 lines
target/riscv/cpu_helper.c +3
-0+* Default | +* Priority | Major Interrupt Numbers +* ----------------------------------------------------------------
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 216
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* Highest | 63 (3f), 62 (3e), 31 (1f), 30 (1e), 61 (3d), 60 (3c),
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 217
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 59 (3b), 58 (3a), 29 (1d), 28 (1c), 57 (39), 56 (38),
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 218
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 55 (37), 54 (36), 27 (1b), 26 (1a), 53 (35), 52 (34),
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 219
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 51 (33), 50 (32), 25 (19), 24 (18), 49 (31), 48 (30)
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 7 lines
target/riscv/cpu_helper.c +7
-0+* | +* | 11 (0b), 3 (03), 7 (07) +* | 9 (09), 1 (01), 5 (05) +* | 12 (0c) +* | 10 (0a), 2 (02), 6 (06) +* | +* | 47 (2f), 46 (2e), 23 (17), 22 (16), 45 (2d), 44 (2c),
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 227
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 43 (2b), 42 (2a), 21 (15), 20 (14), 41 (29), 40 (28),
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 228
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* | 39 (27), 38 (26), 19 (13), 18 (12), 37 (25), 36 (24),
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 229
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* Lowest | 35 (23), 34 (22), 17 (11), 16 (10), 33 (21), 32 (20)
Quoted 1 line
Quoted 1 line
Quoted 1 line
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 230
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+* ----------------------------------------------------------------
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
target/riscv/cpu_helper.c +27
-2+*/ -if (irqs) { -return ctz64 (irqs);/* since non-zero */ + u =IPRIO_DEFAULT_U (irq); + l =IPRIO_DEFAULT_L (irq); +if (u ==0 ) { +if (irq ==IRQ_VS_EXT || irq ==IRQ_VS_TIMER || + irq ==IRQ_VS_SOFT ) { + iprio =IPRIO_DEFAULT_VS ; + }else if (irq ==IRQ_S_GEXT ) { + iprio =IPRIO_DEFAULT_SGEXT ; + }else if (irq ==IRQ_S_EXT || irq ==IRQ_S_TIMER || + irq ==IRQ_S_SOFT ) { + iprio =IPRIO_DEFAULT_S ; + }else if (irq ==IRQ_M_EXT || irq ==IRQ_M_TIMER || + irq ==IRQ_M_SOFT ) { + iprio =IPRIO_DEFAULT_M ; + }else { + iprio =IPRIO_DEFAULT_VS ; + } + }else if (u ==1 ) { +if (l <8 ) { + iprio =IPRIO_DEFAULT_16_23 (irq); + }else { + iprio =IPRIO_DEFAULT_24_31 (irq); + } + }else if (u ==2 ) { + iprio =IPRIO_DEFAULT_32_47 (irq); + }else if (u ==3 ) {Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+ iprio =IPRIO_DEFAULT_48_63 (irq); + } + +return iprio; +} + +static int riscv_cpu_pending_to_irq (CPURISCVState *env, + uint64_t pending, uint8_t*iprio)
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
target/riscv/cpu_helper.c +10
-0+{ +int irq, best_irq =RISCV_EXCP_NONE ; +unsigned int prio, best_prio =UINT_MAX ; + +if (!pending) { +return RISCV_EXCP_NONE ; + } + + irq =ctz64 (pending); +if (!riscv_feature (env,RISCV_FEATURE_AIA )) {Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+return irq; + } + + pending = pending >> irq; +while (pending) { + prio = iprio[irq]; +if (!prio) { + prio = (riscv_cpu_default_priority(irq) <
IPRIO_DEFAULT_M) ?Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 285
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+ 1 : IPRIO_MMAXIPRIO;Hi Anup,
I have a question regarding configuring priorities of major
interrupts.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
riscv_cpu_default_priority()for M-mode external interrupt wouldreturn
IPRIO_DEFAULT_M(8).Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
but the comparison here is <
IPRIO_DEFAULT_M,
so if M-mode external interrupt is pending, the prio would beassigned to
IPRIO_MMAXIPRIO(255).Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Is this the expected behavior?
Actually, for irq ==
IRQ_M_EXTthe prio should beIPRIO_DEFAULT_Msame for irq ==IRQ_S_EXT.Goot catch, I will fix this in next revision.
Quoted 3 lines
Also, why is
IPRIO_DEFAULT_Mbeen compared?
ShouldIPRIO_DEFAULT_SandIPRIO_DEFAULT_VSbe used for S-mode andVS-mode interrupts?
Quoted 8 lines
Quoted 8 lines
For M-mode, we should compare
IPRIO_DEFAULT_M. For S/VS-mode, we should compareIPRIO_DEFAULT_Sbut the priority registers used for S-mode and VS-mode are different.Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
target/riscv/cpu_helper.c
Quoted 2 lines
target/riscv/cpu_helper.c +2
-0+ } +if ((pending &0x1 ) && (prio < best_prio)) {If both interrupt 63 and 62 are pending-and-enabled, with their iprio values both assigned to zero. Interrupt 62 would be picked, because
best_priowill be 1 wheninterrupt 62 is been checked.
Quoted 1 line
Quoted 1 line
Quoted 1 line
As the default priority order for interrupt 63 is also less than
IPRIO_DEFAULT_M(8),Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
so prio would be assigned to 1 as well.
But as prio is not less thanbest_prio, sobest_irqwould still beinterrupt 62.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
However, according to the default priority table in AIA spec, interrupt 63 should have a higher priority than interrupt 62:
Interrupt 63 has the highest default priority, and interrupt 32 hasthe lowest default priority.
Quoted 25 lines
Quoted 25 lines
Argh, the comparision "prio <
best_prio" should have been "prio <=best_prio".I am certainly aware of the fact that if two interrupts have same priority then higher interrupt number should be preferred. This is silly mistake on my part.
I really appreciate your detailed review. Thanks.
Quoted 15 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, · line 296
Quoted 11 lines
target/riscv/cpu_helper.c +9
-0+ best_irq = irq; + best_prio = prio; + } + irq++; + pending = pending >>1 ; + } + +return best_irq; +}Yet I have another question for major interrupts priorities.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
According to AIA spec (v0.2-draft.28):
==========
The priority number for a machine-level external interrupt (bits31:24 of register iprio2)
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
must also be read-only zero.
When an interrupt’s priority number in the array is zero (either
read-only zero or set to zero),
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
its priority is the default order from Section 6.1. Setting an interrupt’s priority number instead to a nonzero value p
gives that interrupt nominally
Quoted 1 line
Quoted 1 line
Quoted 1 line
the same priority as a machine-level external interrupt with priority
number p.
Quoted 1 line
Quoted 1 line
Quoted 1 line
For a major interrupt that defaults to a higher priority than machine
external interrupts,
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
setting its priority number to a nonzero value lowers its priority. For a major interrupt that defaults to a lower priority than machine
external interrupts,
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
setting its priority number to a nonzero value raises its priority.
==========From the above sentences,
The M-mode external interrupt has iprio value of 0, so the defaultpriority order is applied.
Quoted 2 lines
Quoted 2 lines
Yes, that's correct and same logic applies to S/VS-mode external
interrupts.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
What are the priorities for the major interrupt interrupts which are
higher/lower than
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
M-mode external interrupt when their iprio values are not 0?
For a major interrupt X (which is not an external interrupt) having
iprio value 0,
Quoted 1 line
Quoted 1 line
we take iprio of X as 1 or 255 based of comparison of default priority
of X
Quoted 2 lines
Quoted 2 lines
with default priority of external interrupt.
(Refer, documentation of "mtopi" (Section 6.3.2) and "stopi" (Section6.5.2))
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Which interrupt will be picked when:
M-mode external interrupt is pending-and-enabled, Interrupt with higher default priority than M-mode externalinterrupt is pending-and-enabled, and
Quoted 1 line
Quoted 1 line
Quoted 1 line
Interrupt with lower default priority than M-mode external
interrupt is pending-and-enabled/
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
From the above
riscv_cpu_pending_to_irq(),
If both interrupt 63 and interrupt 18's iprio values are assigned tothe value of 10,
Quoted 5 lines
Quoted 2 lines
You mean both these interrupt have iprio 0, right ?
I mean if both interrupt 63 and interrupt 18's iprio values are assigned
with value 10.
Quoted 1 line
If interrupt 63, interrupt 18, and M-mode external interrupt are
pending-and-enabled.
Quoted 1 line
Will M-mode external interrupt be picked as it has the default priority:
8,
Quoted 1 line
which is less than 10 of interrupt 63 and interrupt 18?
Yes, in this case "mtopi" CSR read will return M-mode external interrupt.
Quoted 2 lines
What if, interrupt 63 has iprio value of 10 and interrupt 18 has iprio
value of 6.
Quoted 1 line
If interrupt 63, interrupt 18, and M-mode external interrupt are
pending-and-enabled.
Quoted 1 line
Will interrupt 18 be picked because it has the smallest iprio value
(i.e. higher interrupt priority)?
Yes, in this case "mtopi" CSR read will return interrupt 18.
Quoted 4 lines
Can I say that for M-mode major interrupts:
If interrupt X's iprio value is zero, its interrupt priority will be 1
or 255,
Quoted 1 line
base on its default priority which is higher (1) or less (255) than
M-mode external interrupt.
Yes, this is correct form M-mode (and "mtopi" CSR) perspective.
For S-mode (and "stopi" or "vstopi" CSR) perspective, it will be compared against S-mode external interrupt default priority. (Note: I will fix this in next revision)
Quoted 2 lines
And if interrupt X's iprio value is not zero, its interrupt priority
will be the assigned iprio value.
Yes, this is correct.
Quoted 1 line
So it's possible for a lower default priority interrupt (e.g. 18) to
have higher priority than M-mode external interrupt
Quoted 1 line
if its iprio value is less than 8?
Yes, this is correct. The fact that iprio of major interrupts are programmable
it allow software to override the default priority assignement.Quoted 4 lines
The overall interrupt priority order is compared between the values of:
* If iprio value is zero: 1 or 255
* If iprio value is not zero: the iprio value been assignedThis two statements are correct.
Quoted 2 lines
* M-mode external interrupt: the default priority value: 8?
and the interrupt with the smallest value will be picked.I would like to clarify that it is QEMU implementation that has chosen to assign default priorities in 1 to 15 range hence we have M-mode external interrupt default priority as 8. Some other implementation may chose a different default priority assignments but it should comply with the table in the "Section 6.1" of the AIA specification.
Quoted 3 lines
Regards,
Anup
Thanks for the detailed explanation :)
Regards,
Frank Chang
Quoted 150 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
and interrupt 63, interrupt 18 and M-mode external interrupts are
pending-and-enabled.
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Interrupt 18 would be picked.
Is this the correct expectation?If both 63 and 18 have iprio 0 then interrupt 63 would be picked because 63 has higher default priority compared to default priorities of both M-mode external interrupt and interrupt 18.
Quoted 1 line
I'm a bit confused with the correct priority order of the interrupts
with/without non-zero value iprio and the default priority.
Quoted 25 lines
Quoted 8 lines
Whenever iprio of an interrupt X is 0, the default priority of interrupt X compared against default external interrupt priority and based on this comparison 1 (highest) or 255 (lowest) priority is used for interrupt X.
In other, a saturated priority is used on interrupt X when it's iprio is set to zero.
Thanks for the detailed explanation.
Regards,
Frank ChangQuoted 10 lines
Regards,
AnupQuoted 5 lines
Thanks,
Frank ChangQuoted 1 line
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
target/riscv/cpu_helper.c +5
-0+ +static uint64_t riscv_cpu_all_pending (CPURISCVState *env) +{ +uint32_t gein =get_field (env->hstatus,HSTATUS_VGEIN ); +uint64_t vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ;Quoted 8 lines
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+ +return (env->mip | vsgein) & env->mie; +} + +int riscv_cpu_mirq_pending (CPURISCVState *env) +{ + uint64_t irqs = riscv_cpu_all_pending(env) & ~env->mideleg & + ~(MIP_SGEIP | MIP_VSSIP | MIP_VSTIP |
MIP_VSEIP);Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
target/riscv/cpu_helper.c +23
-0+ +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); +} + +int riscv_cpu_sirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & env->mideleg & + ~(MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); +} + +int riscv_cpu_vsirq_pending (CPURISCVState *env) +{ +uint64_t irqs =riscv_cpu_all_pending (env) & env->mideleg & + (MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ); + +return riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +} + +static int riscv_cpu_local_irq_pending (CPURISCVState *env) +{ +int virq;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+uint64_t irqs, pending, mie, hsie, vsie; + +/* Determine interrupt enable state of all privilege modes */ + if (riscv_cpu_virt_enabled(env)) { + mie = 1; + hsie = 1; + vsie = (env->priv < PRV_S) || + (env->priv == PRV_S && get_field(env->mstatus,
MSTATUS_SIE));Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 4 lines
} else {target/riscv/cpu_helper.c +2
-1-return RISCV_EXCP_NONE ;/* indicates no pending interrupt */ + mie = (env->priv <PRV_M ) || + (env->priv == PRV_M && get_field(env->mstatus,
MSTATUS_MIE));Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
target/riscv/cpu_helper.c
Quoted 2 lines
target/riscv/cpu_helper.c +2
-0+ hsie = (env->priv <PRV_S ) || + (env->priv == PRV_S && get_field(env->mstatus,
MSTATUS_SIE));Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
target/riscv/cpu_helper.c +13
-0+ vsie =0 ; } + +/* Determine all pending interrupts */ + pending =riscv_cpu_all_pending (env); + +/* Check M-mode interrupts */ + irqs = pending & ~env->mideleg & -mie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); + } + +/* Check HS-mode interrupts */ + irqs = pending & env->mideleg & ~env->hideleg & -hsie;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
Quoted 8 lines
target/riscv/cpu_helper.c +8
-0+if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); + } + +/* Check VS-mode interrupts */ + irqs = pending & env->mideleg & env->hideleg & -vsie; +if (irqs) { + virq = riscv_cpu_pending_to_irq(env, irqs >> 1,
env->hviprio);Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
target/riscv/cpu_helper.c +5
-0+return (virq <=0 ) ? virq : virq +1 ; + } + +/* Indicate no pending interrupt */ +return RISCV_EXCP_NONE ; }bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request)target/riscv/machine.c +3
-1@@ -92,6 +92,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), +VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ),VMSTATE_UINT64 (env.vsstatus,RISCVCPU ), VMSTATE_UINTTL(env.vstvec, RISCVCPU),@@ -173,6 +174,8 @@ const VMStateDescription vmstate_riscv_cpu = { .fields = (VMStateField[]) {VMSTATE_UINTTL_ARRAY (env.gpr,RISCVCPU ,32 ),VMSTATE_UINT64_ARRAY (env.fpr,RISCVCPU ,32 ), +VMSTATE_UINT8_ARRAY (env.miprio,RISCVCPU ,64 ), +VMSTATE_UINT8_ARRAY (env.siprio,RISCVCPU ,64 ),VMSTATE_UINTTL (env.pc,RISCVCPU ),VMSTATE_UINTTL (env.load_res,RISCVCPU ),VMSTATE_UINTTL (env.load_val,RISCVCPU ), --2.25.1
Re: [PATCH v6 09/23] target/riscv: Implement AIA local interrupt priorities
In reply to Anup PatelAnup Patel <[email protected]> 於 2021年12月30日 週四 下午8:38寫道:
Quoted 206 lines
From: Anup Patel <[email protected]>
The AIA spec defines programmable 8-bit priority for each local interrupt at M-level, S-level and VS-level so we extend local interrupt processing to consider AIA interrupt priorities. The AIA CSRs which help software configure local interrupt priorities will be added by subsequent patches.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 19 ++++ target/riscv/cpu.h | 12 ++ target/riscv/cpu_helper.c | 231 ++++++++++++++++++++++++++++++++++---- target/riscv/machine.c | 3 + 4 files changed, 244 insertions(+), 21 deletions(-)target/riscv/cpu.c +4
-0@@ -348,6 +348,10 @@ void restore_state_to_opc(CPURISCVState *env,TranslationBlock *tb, static void riscv_cpu_reset(DeviceState *dev) { +#ifndef CONFIG_USER_ONLY + uint8_t iprio; +int i, irq, rdzero; +#endif CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (cs);RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu);target/riscv/cpu.c +15
-0@@ -370,6 +374,21 @@ static void riscv_cpu_reset(DeviceState *dev) env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; env->two_stage_lookup = false; + +/* Initialized default priorities of local interrupts. */ +for (i =0 ; i <ARRAY_SIZE (env->miprio); i++) { + iprio =riscv_cpu_default_priority (i); + env->miprio[i] = (i ==IRQ_M_EXT ) ?0 : iprio; + env->siprio[i] = (i ==IRQ_S_EXT ) ?0 : iprio; + env->hviprio[i] =0 ; + } + i =0 ; +while (!riscv_cpu_hviprio_index2irq (i, &irq, &rdzero)) { +if (!rdzero) { + env->hviprio[irq] = env->miprio[irq]; + } + i++; + }/* mmte is supposed to have pm.current hardwired to 1 */ env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif target/riscv/cpu.h +12
-0@@ -182,6 +182,10 @@ struct CPURISCVState {target_ulong mcause;target_ulong mtval;/* since: priv-1.10.0 */ +/* Machine and Supervisor interrupt priorities */ +uint8_t miprio[64 ]; +uint8_t siprio[64 ]; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;@@ -194,6 +198,9 @@ struct CPURISCVState {target_ulong hgeip;uint64_t htimedelta; +/* Hypervisor controlled virtual interrupt priorities */ +uint8_t hviprio[64 ]; +/* Virtual CSRs */ /* * For RV32 this is 32-bit vsstatus and 32-bit vsstatush. @@ -379,6 +386,11 @@ int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, int cpuid, void *opaque);int riscv_cpu_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg);int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg); +int riscv_cpu_hviprio_index2irq (int index,int *out_irq,int *out_rdzero); +uint8_t riscv_cpu_default_priority (int irq); +int riscv_cpu_mirq_pending (CPURISCVState *env); +int riscv_cpu_sirq_pending (CPURISCVState *env); +int riscv_cpu_vsirq_pending (CPURISCVState *env);bool riscv_cpu_fp_enabled (CPURISCVState *env);target_ulong riscv_cpu_get_geilen (CPURISCVState *env);void
riscv_cpu_set_geilen(CPURISCVState *env, target_ulong geilen);target/riscv/cpu_helper.c +77
-20@@ -151,36 +151,225 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, }#ifndef CONFIG_USER_ONLY -static int riscv_cpu_local_irq_pending (CPURISCVState *env) + +/* +* The HS-mode is allowed to configure priority only for the +* following VS-mode local interrupts: +* +* 0 (Reserved interrupt, reads as zero) +* 1 Supervisor software interrupt +* 4 (Reserved interrupt, reads as zero) +* 5 Supervisor timer interrupt +* 8 (Reserved interrupt, reads as zero) +* 13 (Reserved interrupt) +* 14 " +* 15 " +* 16 " +* 18 Debug/trace interrupt +* 20 (Reserved interrupt) +* 22 " +* 24 " +* 26 " +* 28 " +* 30 (Reserved for standard reporting of bus or system errors) +*/ + +static int hviprio_index2irq[] = + {0 ,1 ,4 ,5 ,8 ,13 ,14 ,15 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 }; +static int hviprio_index2rdzero[] = + {1 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; + +int riscv_cpu_hviprio_index2irq (int index,int *out_irq,int *out_rdzero) { -target_ulong virt_enabled =riscv_cpu_virt_enabled (env); +if (index <0 ||ARRAY_SIZE (hviprio_index2irq) <= index) { +return -EINVAL ; + } + +if (out_irq) { + *out_irq = hviprio_index2irq[index]; + } -target_ulong mstatus_mie =get_field (env->mstatus,MSTATUS_MIE ); -target_ulong mstatus_sie =get_field (env->mstatus,MSTATUS_SIE ); +if (out_rdzero) { + *out_rdzero = hviprio_index2rdzero[index]; + } -target_ulong vsgemask = - (target_ulong )1 <<get_field (env->hstatus,HSTATUS_VGEIN ); -target_ulong vsgein = (env->hgeip & vsgemask) ?MIP_VSEIP :0 ; +return 0 ; +} -target_ulong pending = (env->mip | vsgein) & env->mie; +uint8_t riscv_cpu_default_priority (int irq) +{ +int u, l; +uint8_t iprio =IPRIO_MMAXIPRIO ; -target_ulong mie = env->priv <PRV_M || - (env->priv ==PRV_M && mstatus_mie); -target_ulong sie = env->priv <PRV_S || - (env->priv ==PRV_S && mstatus_sie); -target_ulong hsie = virt_enabled || sie; -target_ulong vsie = virt_enabled && sie; +if (irq <0 || irq >63 ) { +return iprio; + } -target_ulong irqs = - (pending & ~env->mideleg & -mie) | - (pending & env->mideleg & ~env->hideleg & -hsie) | - (pending & env->mideleg & env->hideleg & -vsie); +/* +* Default priorities of local interrupts are defined in the +* RISC-V Advanced Interrupt Architecture specification. +* +* ---------------------------------------------------------------- +* Default | +* Priority | Major Interrupt Numbers +* ---------------------------------------------------------------- +* Highest | 63 (3f), 62 (3e), 31 (1f), 30 (1e), 61 (3d), 60 (3c), +* | 59 (3b), 58 (3a), 29 (1d), 28 (1c), 57 (39), 56 (38), +* | 55 (37), 54 (36), 27 (1b), 26 (1a), 53 (35), 52 (34), +* | 51 (33), 50 (32), 25 (19), 24 (18), 49 (31), 48 (30) +* | +* | 11 (0b), 3 (03), 7 (07) +* | 9 (09), 1 (01), 5 (05) +* | 12 (0c) +* | 10 (0a), 2 (02), 6 (06) +* | +* | 47 (2f), 46 (2e), 23 (17), 22 (16), 45 (2d), 44 (2c), +* | 43 (2b), 42 (2a), 21 (15), 20 (14), 41 (29), 40 (28), +* | 39 (27), 38 (26), 19 (13), 18 (12), 37 (25), 36 (24), +* Lowest | 35 (23), 34 (22), 17 (11), 16 (10), 33 (21), 32 (20) +* ---------------------------------------------------------------- +*/ -if (irqs) { -return ctz64 (irqs);/* since non-zero */ + u =IPRIO_DEFAULT_U (irq); + l =IPRIO_DEFAULT_L (irq); +if (u ==0 ) { +if (irq ==IRQ_VS_EXT || irq ==IRQ_VS_TIMER || + irq ==IRQ_VS_SOFT ) {
target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, · line 248
Quoted 10 lines
target/riscv/cpu_helper.c +9
-0+ iprio =IPRIO_DEFAULT_VS ; + }else if (irq ==IRQ_S_GEXT ) { + iprio =IPRIO_DEFAULT_SGEXT ; + }else if (irq ==IRQ_S_EXT || irq ==IRQ_S_TIMER || + irq ==IRQ_S_SOFT ) { + iprio =IPRIO_DEFAULT_S ; + }else if (irq ==IRQ_M_EXT || irq ==IRQ_M_TIMER || + irq ==IRQ_M_SOFT ) { + iprio =IPRIO_DEFAULT_M ;
RISC-V Priv spec defines the priority order:
Multiple simultaneous interrupts destined for the same privilege mode are handled in the following decreasing priority order: MEI, MSI, MTI,
SEI, SSI, STI, UEI,
USI, UTI.
So the priority order should be: MEI (11) > MSI (3) > MTI (7).
But if MEI, MSI and MTI are all pending-and-enabled,
with all of their iprio assigned to zero (i.e. the default priority orders are applied).riscv_cpu_pending_to_irq() would pick MSI, instead of the correct MEI, because they all have the same priority order: IPRIO_DEFAULT_M.
Regards,
Frank Chang
Quoted 159 lines
target/riscv/cpu_helper.c +98
-1+ } else { + iprio = IPRIO_DEFAULT_VS; + } + } else if (u == 1) { + if (l < 8) { + iprio = IPRIO_DEFAULT_16_23(irq); + } else { + iprio = IPRIO_DEFAULT_24_31(irq); + } + } else if (u == 2) { + iprio = IPRIO_DEFAULT_32_47(irq); + } else if (u == 3) { + iprio = IPRIO_DEFAULT_48_63(irq); + } + + return iprio; +} + +static int riscv_cpu_pending_to_irq(CPURISCVState *env, + uint64_t pending, uint8_t *iprio) +{ + int irq, best_irq = RISCV_EXCP_NONE; + unsigned int prio, best_prio = UINT_MAX; + + if (!pending) { + return RISCV_EXCP_NONE; + } + + irq = ctz64(pending); + if (!riscv_feature(env, RISCV_FEATURE_AIA)) { + return irq; + } + + pending = pending >> irq; + while (pending) { + prio = iprio[irq]; + if (!prio) { + prio = (riscv_cpu_default_priority(irq) < IPRIO_DEFAULT_M) ? + 1 : IPRIO_MMAXIPRIO; + } + if ((pending & 0x1) && (prio < best_prio)) { + best_irq = irq; + best_prio = prio; + } + irq++; + pending = pending >> 1; + } + + return best_irq; +} + +static uint64_t riscv_cpu_all_pending(CPURISCVState *env) +{ + uint32_t gein = get_field(env->hstatus, HSTATUS_VGEIN); + uint64_t vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0; + + return (env->mip | vsgein) & env->mie; +} + +int riscv_cpu_mirq_pending(CPURISCVState *env) +{ + uint64_t irqs = riscv_cpu_all_pending(env) & ~env->mideleg & + ~(MIP_SGEIP | MIP_VSSIP | MIP_VSTIP | MIP_VSEIP); + + return riscv_cpu_pending_to_irq(env, irqs, env->miprio); +} + +int riscv_cpu_sirq_pending(CPURISCVState *env) +{ + uint64_t irqs = riscv_cpu_all_pending(env) & env->mideleg & + ~(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP); + + return riscv_cpu_pending_to_irq(env, irqs, env->siprio); +} + +int riscv_cpu_vsirq_pending(CPURISCVState *env) +{ + uint64_t irqs = riscv_cpu_all_pending(env) & env->mideleg & + (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP); + + return riscv_cpu_pending_to_irq(env, irqs >> 1, env->hviprio); +} + +static int riscv_cpu_local_irq_pending(CPURISCVState *env) +{ + int virq; + uint64_t irqs, pending, mie, hsie, vsie; + + /* Determine interrupt enable state of all privilege modes */ + if (riscv_cpu_virt_enabled(env)) { + mie = 1; + hsie = 1; + vsie = (env->priv < PRV_S) || + (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_SIE)); } else { -return RISCV_EXCP_NONE ;/* indicates no pending interrupt */ + mie = (env->priv < PRV_M) || + (env->priv == PRV_M && get_field(env->mstatus, MSTATUS_MIE)); + hsie = (env->priv < PRV_S) || + (env->priv == PRV_S && get_field(env->mstatus,
MSTATUS_SIE));target/riscv/cpu_helper.c +26
-0+ vsie =0 ; } + +/* Determine all pending interrupts */ + pending =riscv_cpu_all_pending (env); + +/* Check M-mode interrupts */ + irqs = pending & ~env->mideleg & -mie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->miprio); + } + +/* Check HS-mode interrupts */ + irqs = pending & env->mideleg & ~env->hideleg & -hsie; +if (irqs) { +return riscv_cpu_pending_to_irq (env, irqs, env->siprio); + } + +/* Check VS-mode interrupts */ + irqs = pending & env->mideleg & env->hideleg & -vsie; +if (irqs) { + virq =riscv_cpu_pending_to_irq (env, irqs >>1 , env->hviprio); +return (virq <=0 ) ? virq : virq +1 ; + } + +/* Indicate no pending interrupt */ +return RISCV_EXCP_NONE ; }bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request)target/riscv/machine.c +3
-1@@ -92,6 +92,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), +VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ),VMSTATE_UINT64 (env.vsstatus,RISCVCPU ), VMSTATE_UINTTL(env.vstvec, RISCVCPU),@@ -173,6 +174,8 @@ const VMStateDescription vmstate_riscv_cpu = { .fields = (VMStateField[]) {VMSTATE_UINTTL_ARRAY (env.gpr,RISCVCPU ,32 ),VMSTATE_UINT64_ARRAY (env.fpr,RISCVCPU ,32 ), +VMSTATE_UINT8_ARRAY (env.miprio,RISCVCPU ,64 ), +VMSTATE_UINT8_ARRAY (env.siprio,RISCVCPU ,64 ),VMSTATE_UINTTL (env.pc,RISCVCPU ),VMSTATE_UINTTL (env.load_res,RISCVCPU ),VMSTATE_UINTTL (env.load_val,RISCVCPU ), --2.25.1
PATCH v6 10/23 target/riscv: Implement AIA CSRs for 64 local interrupts on RV32
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The AIA specification adds new CSRs for RV32 so that RISC-V hart can support 64 local interrupts on both RV32 and RV64.
--- target/riscv/cpu.h | 14 +- target/riscv/cpu_helper.c | 10 +- target/riscv/csr.c | 560 +++++++++++++++++++++++++++++++------- target/riscv/machine.c | 10 +- 4 files changed, 474 insertions(+), 120 deletions(-)
target/riscv/cpu.h +7 -7
@@ -162,12 +162,12 @@ struct CPURISCVState {*/ uint64_t mstatus; -target_ulong mip; +uint64_t mip; -uint32_t miclaim; +uint64_t miclaim; -target_ulong mie; -target_ulong mideleg; +uint64_t mie; +uint64_t mideleg;target_ulong satp;/* since: priv-1.10.0 */ target_ulong stval;
@@ -189,7 +189,7 @@ struct CPURISCVState {/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg; -target_ulong hideleg; +uint64_t hideleg;target_ulong hcounteren;target_ulong htval;target_ulong htinst;
@@ -420,8 +420,8 @@ void riscv_cpu_list(void);#ifndef CONFIG_USER_ONLY bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request);void riscv_cpu_swap_hypervisor_regs (CPURISCVState *env); -int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint32_t interrupts); -uint32_t riscv_cpu_update_mip (RISCVCPU *cpu,uint32_t mask,uint32_t value); +int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint64_t interrupts); +uint64_t riscv_cpu_update_mip (RISCVCPU *cpu,uint64_t mask,uint64_t value);#define BOOL_TO_MASK (x) (-!!(x))/* helper for riscv_cpu_update_mip value */ void riscv_cpu_set_rdtime_fn (CPURISCVState *env,uint64_t (*fn)(uint32_t ),uint32_t arg);
target/riscv/cpu_helper.c +5 -5
@@ -534,7 +534,7 @@ bool riscv_cpu_two_stage_lookup(int mmu_idx)return mmu_idx &TB_FLAGS_PRIV_HYP_ACCESS_MASK ; } -int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint32_t interrupts) +int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint64_t interrupts) {CPURISCVState *env = &cpu->env;if (env->miclaim & interrupts) {
@@ -545,11 +545,11 @@ int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts) } } -uint32_t riscv_cpu_update_mip (RISCVCPU *cpu,uint32_t mask,uint32_t value) +uint64_t riscv_cpu_update_mip (RISCVCPU *cpu,uint64_t mask,uint64_t value) {CPURISCVState *env = &cpu->env;CPUState *cs =CPU (cpu); -uint32_t gein, vsgein =0 , old = env->mip; +uint64_t gein, vsgein =0 , old = env->mip;bool locked = false;if (riscv_cpu_virt_enabled (env)) {
@@ -1252,7 +1252,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)*/ bool async = !!(cs->exception_index &RISCV_EXCP_INT_FLAG );target_ulong cause = cs->exception_index &RISCV_EXCP_INT_MASK ; -target_ulong deleg = async ? env->mideleg : env->medeleg; +uint64_t deleg = async ? env->mideleg : env->medeleg;bool write_tval = false;target_ulong tval =0 ;target_ulong htval =0 ;
@@ -1317,7 +1317,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) {/* handle the trap in S-mode */ if (riscv_has_ext (env,RVH )) { -target_ulong hdeleg = async ? env->hideleg : env->hedeleg; +uint64_t hdeleg = async ? env->hideleg : env->hedeleg;if (env->two_stage_lookup && write_tval) {/*
target/riscv/csr.c +457 -103
734 lines not on this page · raw message
target/riscv/machine.c +5 -5
@@ -84,7 +84,7 @@ static const VMStateDescription vmstate_hyper = { .fields = (VMStateField[]) {VMSTATE_UINTTL (env.hstatus,RISCVCPU ),VMSTATE_UINTTL (env.hedeleg,RISCVCPU ), -VMSTATE_UINTTL (env.hideleg,RISCVCPU ), +VMSTATE_UINT64 (env.hideleg,RISCVCPU ),VMSTATE_UINTTL (env.hcounteren,RISCVCPU ),VMSTATE_UINTTL (env.htval,RISCVCPU ),VMSTATE_UINTTL (env.htinst,RISCVCPU ),
@@ -194,10 +194,10 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.resetvec,RISCVCPU ),VMSTATE_UINTTL (env.mhartid,RISCVCPU ),VMSTATE_UINT64 (env.mstatus,RISCVCPU ), -VMSTATE_UINTTL (env.mip,RISCVCPU ), -VMSTATE_UINT32 (env.miclaim,RISCVCPU ), -VMSTATE_UINTTL (env.mie,RISCVCPU ), -VMSTATE_UINTTL (env.mideleg,RISCVCPU ), +VMSTATE_UINT64 (env.mip,RISCVCPU ), +VMSTATE_UINT64 (env.miclaim,RISCVCPU ), +VMSTATE_UINT64 (env.mie,RISCVCPU ), +VMSTATE_UINT64 (env.mideleg,RISCVCPU ),VMSTATE_UINTTL (env.satp,RISCVCPU ),VMSTATE_UINTTL (env.stval,RISCVCPU ), VMSTATE_UINTTL(env.medeleg, RISCVCPU),
Signature · 1 line
2.25.1
Re: [PATCH v6 10/23] target/riscv: Implement AIA CSRs for 64 local interrupts on RV32
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:55寫道:
Quoted 957 lines
From: Anup Patel <[email protected]>
The AIA specification adds new CSRs for RV32 so that RISC-V hart can support 64 local interrupts on both RV32 and RV64.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.h | 14 +- target/riscv/cpu_helper.c | 10 +- target/riscv/csr.c | 560 +++++++++++++++++++++++++++++++------- target/riscv/machine.c | 10 +- 4 files changed, 474 insertions(+), 120 deletions(-)target/riscv/cpu.h +7
-7@@ -162,12 +162,12 @@ struct CPURISCVState {*/ uint64_t mstatus; -target_ulong mip; +uint64_t mip; -uint32_t miclaim; +uint64_t miclaim; -target_ulong mie; -target_ulong mideleg; +uint64_t mie; +uint64_t mideleg;target_ulong satp;/* since: priv-1.10.0 */ target_ulong stval;@@ -189,7 +189,7 @@ struct CPURISCVState {/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg; -target_ulong hideleg; +uint64_t hideleg;target_ulong hcounteren;target_ulong htval;target_ulong htinst;@@ -420,8 +420,8 @@ void riscv_cpu_list(void);#ifndef CONFIG_USER_ONLY bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request);void riscv_cpu_swap_hypervisor_regs (CPURISCVState *env); -int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint32_t interrupts); -uint32_t riscv_cpu_update_mip (RISCVCPU *cpu,uint32_t mask,uint32_t value); +int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint64_t interrupts); +uint64_t riscv_cpu_update_mip (RISCVCPU *cpu,uint64_t mask,uint64_t value);#define BOOL_TO_MASK (x) (-!!(x))/* helper for riscv_cpu_update_mip value */
void riscv_cpu_set_rdtime_fn(CPURISCVState *env,uint64_t(*fn)(uint32_t),uint32_t arg);target/riscv/cpu_helper.c +3
-3@@ -534,7 +534,7 @@ bool riscv_cpu_two_stage_lookup(int mmu_idx)return mmu_idx &TB_FLAGS_PRIV_HYP_ACCESS_MASK ; } -int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint32_t interrupts) +int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint64_t interrupts) {CPURISCVState *env = &cpu->env;if (env->miclaim & interrupts) {@@ -545,11 +545,11 @@ int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts) } } -uint32_t riscv_cpu_update_mip (RISCVCPU *cpu,uint32_t mask,uint32_t value) +uint64_t riscv_cpu_update_mip (RISCVCPU *cpu,uint64_t mask,uint64_t value) {CPURISCVState *env = &cpu->env;CPUState *cs =CPU (cpu); -uint32_t gein, vsgein =0 , old = env->mip; +uint64_t gein, vsgein =0 , old = env->mip;bool locked = false; if (riscv_cpu_virt_enabled(env)) {target/riscv/cpu_helper.c +2
-2@@ -1252,7 +1252,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)*/ bool async = !!(cs->exception_index &RISCV_EXCP_INT_FLAG );target_ulong cause = cs->exception_index &RISCV_EXCP_INT_MASK ; -target_ulong deleg = async ? env->mideleg : env->medeleg; +uint64_t deleg = async ? env->mideleg : env->medeleg;bool write_tval = false;target_ulong tval =0 ;target_ulong htval =0 ;@@ -1317,7 +1317,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) {/* handle the trap in S-mode */ if (riscv_has_ext (env,RVH )) { -target_ulong hdeleg = async ? env->hideleg : env->hedeleg; +uint64_t hdeleg = async ? env->hideleg : env->hedeleg;if (env->two_stage_lookup && write_tval) {/* target/riscv/csr.c +9
-0@@ -154,6 +154,15 @@ static RISCVException any32(CPURISCVState *env, int csrno) } +static int aia_any32 (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return any32 (env, csrno); +} +static RISCVException smode (CPURISCVState *env,int csrno) {if (riscv_has_ext(env, RVS)) {target/riscv/csr.c +18
-0@@ -163,6 +172,24 @@ static RISCVException smode(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +static int smode32 (CPURISCVState *env,int csrno) +{ +if (riscv_cpu_mxl (env) !=MXL_RV32 ) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return smode (env, csrno); +} + +static int aia_smode32 (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return smode32 (env, csrno); +} +static RISCVException hmode (CPURISCVState *env,int csrno) {if (riscv_has_ext(env, RVS) &&target/riscv/csr.c +9
-0@@ -203,6 +230,15 @@ static RISCVException pointer_masking(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +static int aia_hmode32 (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return hmode32 (env, csrno); +} +static RISCVException pmp (CPURISCVState *env,int csrno) {if (riscv_feature(env, RISCV_FEATURE_PMP)) {target/riscv/csr.c +5
-7@@ -443,15 +479,15 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,/* Machine constants */ -#define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP) -#define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP) -#define VS_MODE_INTERRUPTS (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP) -#define HS_MODE_INTERRUPTS (MIP_SGEIP | VS_MODE_INTERRUPTS) +#define M_MODE_INTERRUPTS ((uint64_t)(MIP_MSIP | MIP_MTIP | MIP_MEIP)) +#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP)) +#define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP)) +#define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS)) -static const target_ulong delegable_ints =S_MODE_INTERRUPTS | +static const uint64_t delegable_ints =S_MODE_INTERRUPTS |VS_MODE_INTERRUPTS ; -static const target_ulong vs_delegable_ints =VS_MODE_INTERRUPTS ; -static const target_ulong all_ints = M_MODE_INTERRUPTS |
S_MODE_INTERRUPTS|target/riscv/csr.c +86
-17+static const uint64_t vs_delegable_ints =VS_MODE_INTERRUPTS ; +static const uint64_t all_ints =M_MODE_INTERRUPTS |S_MODE_INTERRUPTS |HS_MODE_INTERRUPTS ;#define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \ (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \@@ -701,40 +737,107 @@ static RISCVException write_medeleg(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } -static RISCVException read_mideleg (CPURISCVState *env,int csrno, -target_ulong *val) +static RISCVException rmw_mideleg64 (CPURISCVState *env,int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) { - *val = env->mideleg; -return RISCV_EXCP_NONE ; -} +uint64_t mask = wr_mask & delegable_ints; + +if (ret_val) { + *ret_val = env->mideleg; + } + + env->mideleg = (env->mideleg & ~mask) | (new_val & mask); -static RISCVException write_mideleg (CPURISCVState *env,int csrno, -target_ulong val) -{ - env->mideleg = (env->mideleg & ~delegable_ints) | (val & delegable_ints);if (riscv_has_ext (env,RVH )) { env->mideleg |=HS_MODE_INTERRUPTS ; } +return RISCV_EXCP_NONE ; } -static RISCVException read_mie (CPURISCVState *env,int csrno, -target_ulong *val) +static RISCVException rmw_mideleg (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) { - *val = env->mie; -return RISCV_EXCP_NONE ; +uint64_t rval; +RISCVException ret; + + ret =rmw_mideleg64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; + } + +return ret; } -static RISCVException write_mie (CPURISCVState *env,int csrno, -target_ulong val) +static RISCVException rmw_midelegh (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val, +target_ulong wr_mask) { - env->mie = (env->mie & ~all_ints) | (val & all_ints); +uint64_t rval; +RISCVException ret; + + ret =rmw_mideleg64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; + } + +return ret; +} + +static RISCVException rmw_mie64 (CPURISCVState *env,int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) +{ +uint64_t mask = wr_mask & all_ints; + +if (ret_val) { + *ret_val = env->mie; + } + + env->mie = (env->mie & ~mask) | (new_val & mask); +if (!riscv_has_ext (env,RVH )) { - env->mie &= ~MIP_SGEIP ; + env->mie &= ~((uint64_t )MIP_SGEIP ); } +return RISCV_EXCP_NONE ; } +static RISCVException rmw_mie (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_mie64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; + } + +return ret; +} + +static RISCVException rmw_mieh (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_mie64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; + } + +return ret; +} +static RISCVException read_mtvec(CPURISCVState *env, int csrno,
target_ulong*val)
{target/riscv/csr.c +3
-3@@ -825,17 +928,17 @@ static RISCVException write_mtval(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } -static RISCVException rmw_mip (CPURISCVState *env,int csrno, -target_ulong *ret_value, -target_ulong new_value,target_ulong write_mask) +static RISCVException rmw_mip64(CPURISCVState *env, int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) {RISCVCPU *cpu =env_archcpu (env);/* Allow software control of delegable interrupts not claimed by hardware */
target/riscv/csr.c +36
-5-target_ulong mask = write_mask & delegable_ints & ~env->miclaim; -uint32_t gin, old_mip; +uint64_t old_mip, mask = wr_mask & delegable_ints & ~env->miclaim; +uint32_t gin;if (mask) { - old_mip =riscv_cpu_update_mip (cpu, mask, (new_value & mask)); + old_mip =riscv_cpu_update_mip (cpu, mask, (new_val & mask)); }else { old_mip = env->mip; }@@ -845,13 +948,44 @@ static RISCVException rmw_mip(CPURISCVState *env, int csrno, old_mip |= (env->hgeip & ((target_ulong)1 << gin)) ? MIP_VSEIP :0 ; } -if (ret_value) { - *ret_value = old_mip; +if (ret_val) { + *ret_val = old_mip; }return RISCV_EXCP_NONE ; } +static RISCVException rmw_mip (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_mip64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; + } + +return ret; +} + +static RISCVException rmw_miph (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_mip64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; + } + +return ret; +} +/* Supervisor Trap Setup */ static RISCVException read_sstatus(CPURISCVState *env, int csrno,
target_ulong*val)target/riscv/csr.c +91
-24@@ -871,45 +1005,112 @@ static RISCVException write_sstatus(CPURISCVState *env, int csrno, return write_mstatus(env, CSR_MSTATUS, newval); } -static RISCVException read_vsie (CPURISCVState *env,int csrno, -target_ulong *val) +static RISCVException rmw_vsie64 (CPURISCVState *env,int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) { -/* Shift the VS bits to their S bit location in vsie */ - *val = (env->mie & env->hideleg &VS_MODE_INTERRUPTS ) >>1 ; -return RISCV_EXCP_NONE ; +RISCVException ret; +uint64_t rval, vsbits, mask = env->hideleg &VS_MODE_INTERRUPTS ; + +/* Bring VS-level bits to correct position */ + vsbits = new_val & (VS_MODE_INTERRUPTS >>1 ); + new_val &= ~(VS_MODE_INTERRUPTS >>1 ); + new_val |= vsbits <<1 ; + vsbits = wr_mask & (VS_MODE_INTERRUPTS >>1 ); + wr_mask &= ~(VS_MODE_INTERRUPTS >>1 ); + wr_mask |= vsbits <<1 ; + + ret =rmw_mie64 (env, csrno, &rval, new_val, wr_mask & mask); +if (ret_val) { + rval &= mask; + vsbits = rval &VS_MODE_INTERRUPTS ; + rval &= ~VS_MODE_INTERRUPTS ; + *ret_val = rval | (vsbits >>1 ); + } + +return ret; } -static RISCVException read_sie (CPURISCVState *env,int csrno, -target_ulong *val) +static RISCVException rmw_vsie (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) { -if (riscv_cpu_virt_enabled (env)) { -read_vsie (env,CSR_VSIE , val); - }else { - *val = env->mie & env->mideleg; +uint64_t rval; +RISCVException ret; + + ret =rmw_vsie64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; } -return RISCV_EXCP_NONE ; + +return ret; } -static RISCVException write_vsie (CPURISCVState *env,int csrno, -target_ulong val) +static RISCVException rmw_vsieh (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) { -/* Shift the S bits to their VS bit location in mie */ -target_ulong newval = (env->mie & ~VS_MODE_INTERRUPTS ) | - ((val <<1 ) & env->hideleg & VS_MODE_INTERRUPTS); -return write_mie (env,CSR_MIE , newval); +uint64_t rval; +RISCVException ret; + + ret =rmw_vsie64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; + } + +return ret; } -static int write_sie (CPURISCVState *env,int csrno,target_ulong val) +static RISCVException rmw_sie64 (CPURISCVState *env,int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) { +RISCVException ret; +uint64_t mask = env->mideleg &S_MODE_INTERRUPTS ; +if (riscv_cpu_virt_enabled (env)) { -write_vsie (env,CSR_VSIE , val); + ret =rmw_vsie64 (env,CSR_VSIE , ret_val, new_val, wr_mask); }else { -target_ulong newval = (env->mie & ~S_MODE_INTERRUPTS ) | - (val &S_MODE_INTERRUPTS ); -write_mie (env,CSR_MIE , newval); + ret =rmw_mie64 (env, csrno, ret_val, new_val, wr_mask & mask); } -return RISCV_EXCP_NONE ; +if (ret_val) { + *ret_val &= mask; + } + +return ret; +} + +static RISCVException rmw_sie (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_sie64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; + } + +return ret; +} + +static RISCVException rmw_sieh (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_sie64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; + } + +return ret;}
static RISCVException read_stvec(CPURISCVState *env, int csrno,
target/riscv/csr.c +159
-31@@ -1002,38 +1203,111 @@ static RISCVException write_stval(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } +static RISCVException rmw_vsip64 (CPURISCVState *env,int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) +{ +RISCVException ret; +uint64_t rval, vsbits, mask = env->hideleg & vsip_writable_mask; + +/* Bring VS-level bits to correct position */ + vsbits = new_val & (VS_MODE_INTERRUPTS >>1 ); + new_val &= ~(VS_MODE_INTERRUPTS >>1 ); + new_val |= vsbits <<1 ; + vsbits = wr_mask & (VS_MODE_INTERRUPTS >>1 ); + wr_mask &= ~(VS_MODE_INTERRUPTS >>1 ); + wr_mask |= vsbits <<1 ; + + ret =rmw_mip64 (env, csrno, &rval, new_val, wr_mask & mask); +if (ret_val) { + rval &= mask; + vsbits = rval &VS_MODE_INTERRUPTS ; + rval &= ~VS_MODE_INTERRUPTS ; + *ret_val = rval | (vsbits >>1 ); + } + +return ret; +} + static RISCVException rmw_vsip(CPURISCVState *env, int csrno, -target_ulong *ret_value, -target_ulong new_value,target_ulong write_mask) + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_mask) { -/* Shift the S bits to their VS bit location in mip */ -int ret =rmw_mip (env, csrno, ret_value, new_value <<1 , - (write_mask <<1 ) & vsip_writable_mask & env->hideleg); +uint64_t rval; +RISCVException ret; -if (ret_value) { - *ret_value &=VS_MODE_INTERRUPTS ; -/* Shift the VS bits to their S bit location in vsip */ - *ret_value >>=1 ; + ret =rmw_vsip64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; } +return ret; } -static RISCVException rmw_sip (CPURISCVState *env,int csrno, -target_ulong *ret_value, -target_ulong new_value,target_ulong write_mask) +static RISCVException rmw_vsiph(CPURISCVState *env, int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) { -int ret; +uint64_t rval; +RISCVException ret; + + ret =rmw_vsip64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; + } + +return ret; +} + +static RISCVException rmw_sip64 (CPURISCVState *env,int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) +{ +RISCVException ret; +uint64_t mask = env->mideleg & sip_writable_mask;if (riscv_cpu_virt_enabled (env)) { - ret =rmw_vsip (env,CSR_VSIP , ret_value, new_value, write_mask); + ret =rmw_vsip64 (env,CSR_VSIP , ret_val, new_val, wr_mask); }else { - ret =rmw_mip (env, csrno, ret_value, new_value, - write_mask & env->mideleg & sip_writable_mask); + ret =rmw_mip64 (env, csrno, ret_val, new_val, wr_mask & mask); } -if (ret_value) { - *ret_value &= env->mideleg &S_MODE_INTERRUPTS ; +if (ret_val) { + *ret_val &= env->mideleg &S_MODE_INTERRUPTS ; + } + +return ret; +} + +static RISCVException rmw_sip (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_sip64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; } + +return ret; +} + +static RISCVException rmw_siph (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_sip64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; + } +return ret; }@@ -1128,30 +1402,94 @@ static RISCVException write_hedeleg(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } -static RISCVException read_hideleg (CPURISCVState *env,int csrno, -target_ulong *val) +static RISCVException rmw_hideleg64 (CPURISCVState *env,int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) { - *val = env->hideleg; +uint64_t mask = wr_mask & vs_delegable_ints; + +if (ret_val) { + *ret_val = env->hideleg & vs_delegable_ints; + } + + env->hideleg = (env->hideleg & ~mask) | (new_val & mask);return RISCV_EXCP_NONE ; } -static RISCVException write_hideleg (CPURISCVState *env,int csrno, -target_ulong val) +static RISCVException rmw_hideleg (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) { - env->hideleg = val & vs_delegable_ints; -return RISCV_EXCP_NONE ; +uint64_t rval; +RISCVException ret; + + ret =rmw_hideleg64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; + } + +return ret; +} + +static RISCVException rmw_hidelegh (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) +{ +uint64_t rval; +RISCVException ret; + + ret =rmw_hideleg64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; + } + +return ret; +} + +static RISCVException rmw_hvip64 (CPURISCVState *env,int csrno, +uint64_t *ret_val, +uint64_t new_val,uint64_t wr_mask) +{ +RISCVException ret; + + ret =rmw_mip64 (env, csrno, ret_val, new_val, + wr_mask & hvip_writable_mask); +if (ret_val) { + *ret_val &=VS_MODE_INTERRUPTS ; + } + +return ret; } static RISCVException rmw_hvip(CPURISCVState *env, int csrno, -target_ulong *ret_value, -target_ulong new_value,target_ulong write_mask) + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_mask) { -int ret =rmw_mip (env, csrno, ret_value, new_value, - write_mask & hvip_writable_mask); + uint64_t rval; +RISCVException ret; -if (ret_value) { - *ret_value &=VS_MODE_INTERRUPTS ; + ret =rmw_hvip64 (env, csrno, &rval, new_val, wr_mask); +if (ret_val) { + *ret_val = rval; + } + +return ret; +} + +static RISCVException rmw_hviph (CPURISCVState *env,int csrno, +target_ulong *ret_val, + target_ulong new_val, target_ulong
wr_mask)target/riscv/csr.c +20
-10+{ +uint64_t rval; +RISCVException ret; + + ret =rmw_hvip64 (env, csrno, &rval, + ((uint64_t )new_val) <<32 , ((uint64_t )wr_mask) <<32 ); +if (ret_val) { + *ret_val = rval >>32 ; } +return ret; }@@ -1168,18 +1506,19 @@ static RISCVException rmw_hip(CPURISCVState *env,int csrno, return ret; } -static RISCVException read_hie (CPURISCVState *env,int csrno, -target_ulong *val) +static RISCVException rmw_hie (CPURISCVState *env,int csrno, +target_ulong *ret_val, +target_ulong new_val,target_ulong wr_mask) { - *val = env->mie &HS_MODE_INTERRUPTS ; -return RISCV_EXCP_NONE ; -} +uint64_t rval; +RISCVException ret; -static RISCVException write_hie (CPURISCVState *env,int csrno, -target_ulong val) -{ -target_ulong newval = (env->mie & ~HS_MODE_INTERRUPTS ) | (val & HS_MODE_INTERRUPTS); -return write_mie (env,CSR_MIE , newval); + ret =rmw_mie64 (env, csrno, &rval, new_val, wr_mask &HS_MODE_INTERRUPTS ); +if (ret_val) { + *ret_val = rval &HS_MODE_INTERRUPTS ; + } + +return ret;}
static RISCVException read_hcounteren(CPURISCVState *env, int csrno,
target/riscv/csr.c +1
-1@@ -1901,9 +2240,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {/* Machine Trap Setup */ [CSR_MSTATUS ] = {"mstatus" , any, read_mstatus, write_mstatus }, [CSR_MISA ] = {"misa" , any, read_misa, write_misa }, - [CSR_MIDELEG ] = {"mideleg" , any, read_mideleg, write_mideleg }, + [CSR_MIDELEG ] = {"mideleg" , any,NULL ,NULL , rmw_mideleg }, [CSR_MEDELEG ] = {"medeleg" , any, read_medeleg,
write_medeleg},target/riscv/csr.c +1
-1- [CSR_MIE ] = {"mie" , any, read_mie, write_mie }, + [CSR_MIE] = { "mie", any, NULL, NULL, rmw_mie }, [CSR_MTVEC] = { "mtvec", any, read_mtvec, write_mtvec }, [CSR_MCOUNTEREN] = { "mcounteren", any, read_mcounteren,
write_mcounteren},target/riscv/csr.c +6
-1@@ -1916,9 +2255,14 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, [CSR_MIP ] = {"mip" , any,NULL ,NULL , rmw_mip }, + /* Machine-Level High-Half CSRs (AIA) */ + [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh }, + [CSR_MIEH] = { "mieh", aia_any32, NULL, NULL, rmw_mieh }, + [CSR_MIPH ] = {"miph" , aia_any32,NULL ,NULL , rmw_miph }, + /* Supervisor Trap Setup */ [CSR_SSTATUS ] = {"sstatus" , smode, read_sstatus, write_sstatus }, - [CSR_SIE ] = {"sie" , smode, read_sie, write_sie }, + [CSR_SIE] = { "sie", smode, NULL, NULL, rmw_sie },[
CSR_STVEC] = { "stvec", smode,read_stvec,write_stvec},
[CSR_SCOUNTEREN] = { "scounteren", smode,read_scounteren,write_scounteren},target/riscv/csr.c +5
-1@@ -1932,12 +2276,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {/* Supervisor Protection and Translation */ [CSR_SATP ] = {"satp" , smode, read_satp, write_satp }, + /* Supervisor-Level High-Half CSRs (AIA) */ + [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, + [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph }, + [CSR_HSTATUS ] = {"hstatus" , hmode, read_hstatus, write_hstatus }, [CSR_HEDELEG ] = {"hedeleg" , hmode, read_hedeleg, write_hedeleg }, - [CSR_HIDELEG ] = {"hideleg" , hmode, read_hideleg, write_hideleg }, + [CSR_HIDELEG ] = {"hideleg" , hmode,NULL ,NULL , rmw_hideleg }, [CSR_HVIP ] = {"hvip" , hmode,NULL ,NULL , rmw_hvip },[
CSR_HIP] = { "hip", hmode, NULL, NULL,
rmw_hip},target/riscv/csr.c +1
-1- [CSR_HIE ] = {"hie" , hmode, read_hie, write_hie }, + [CSR_HIE] = { "hie", hmode, NULL, NULL, rmw_hie }, [CSR_HCOUNTEREN] = { "hcounteren", hmode, read_hcounteren,
write_hcounteren},
[CSR_HGEIE] = { "hgeie", hmode,read_hgeie,
write_hgeie},
[CSR_HTVAL] = { "htval", hmode,read_htval,
write_htval},target/riscv/csr.c +1
-1@@ -1949,7 +2297,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_VSSTATUS ] = {"vsstatus" , hmode, read_vsstatus, write_vsstatus }, [CSR_VSIP ] = {"vsip" , hmode,NULL ,NULL , rmw_vsip }, - [CSR_VSIE] = { "vsie", hmode, read_vsie, write_vsie }, + [CSR_VSIE ] = {"vsie" , hmode,NULL ,NULL ,
rmw_vsie},
[CSR_VSTVEC] = { "vstvec", hmode,read_vstvec,write_vstvec},
[CSR_VSSCRATCH] = { "vsscratch", hmode,read_vsscratch,
write_vsscratch},
[CSR_VSEPC] = { "vsepc", hmode,read_vsepc,
write_vsepc},target/riscv/csr.c +3
-0@@ -1960,6 +2308,12 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL2] = { "mtval2", hmode, read_mtval2, write_mtval2 }, [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ + [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL, rmw_hidelegh }, + [CSR_HVIPH] = { "hviph", aia_hmode32, NULL, NULL,
rmw_hviph},
+ [CSR_VSIEH] = { "vsieh",aia_hmode32, NULL, NULL,rmw_vsieh},
+ [CSR_VSIPH] = { "vsiph",aia_hmode32, NULL, NULL,rmw_vsiph},target/riscv/csr.c +1
-0+ /* Physical Memory Protection */ [CSR_MSECCFG] = { "mseccfg", epmp, read_mseccfg, write_mseccfg }, [CSR_PMPCFG0] = { "pmpcfg0", pmp, read_pmpcfg, write_pmpcfg },target/riscv/machine.c +5
-6@@ -84,7 +84,7 @@ static const VMStateDescription vmstate_hyper = { .fields = (VMStateField[]) {VMSTATE_UINTTL (env.hstatus,RISCVCPU ),VMSTATE_UINTTL (env.hedeleg,RISCVCPU ), -VMSTATE_UINTTL (env.hideleg,RISCVCPU ), +VMSTATE_UINT64 (env.hideleg,RISCVCPU ),VMSTATE_UINTTL (env.hcounteren,RISCVCPU ),VMSTATE_UINTTL (env.htval,RISCVCPU ),VMSTATE_UINTTL (env.htinst,RISCVCPU ),@@ -194,10 +194,10 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.resetvec,RISCVCPU ),VMSTATE_UINTTL (env.mhartid,RISCVCPU ),VMSTATE_UINT64 (env.mstatus,RISCVCPU ), -VMSTATE_UINTTL (env.mip,RISCVCPU ), -VMSTATE_UINT32 (env.miclaim,RISCVCPU ), -VMSTATE_UINTTL (env.mie,RISCVCPU ), -VMSTATE_UINTTL (env.mideleg,RISCVCPU ), +VMSTATE_UINT64 (env.mip,RISCVCPU ), +VMSTATE_UINT64 (env.miclaim,RISCVCPU ), +VMSTATE_UINT64 (env.mie,RISCVCPU ), +VMSTATE_UINT64 (env.mideleg,RISCVCPU ),VMSTATE_UINTTL (env.satp,RISCVCPU ),VMSTATE_UINTTL (env.stval,RISCVCPU ), VMSTATE_UINTTL(env.medeleg, RISCVCPU), --
Quoted 4 lines
2.25.1
PATCH v6 11/23 target/riscv: Implement AIA hvictl and hviprioX CSRs
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The AIA hvictl and hviprioX CSRs allow hypervisor to control interrupts visible at VS-level. This patch implements AIA hvictl and hviprioX CSRs.
--- target/riscv/cpu.h | 2 + target/riscv/csr.c | 126 +++++++++++++++++++++++++++++++++++++++++ target/riscv/machine.c | 2 + 3 files changed, 130 insertions(+)
target/riscv/cpu.h +2 -0
@@ -199,6 +199,7 @@ struct CPURISCVState {uint64_t htimedelta;/* Hypervisor controlled virtual interrupt priorities */ +target_ulong hvictl;uint8_t hviprio[64 ];/* Virtual CSRs */
@@ -475,6 +476,7 @@ static inline RISCVMXL riscv_cpu_mxl(CPURISCVState *env) return env->misa_mxl; }#endif +#define riscv_cpu_mxl_bits (env) (1UL << (4 + riscv_cpu_mxl(env)))/* * Encode LMUL to lmul as follows:
target/riscv/csr.c +126 -0
159 lines not on this page · raw message
target/riscv/machine.c +2 -0
@@ -92,6 +92,8 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), + +VMSTATE_UINTTL (env.hvictl,RISCVCPU ),VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ), VMSTATE_UINT64(env.vsstatus, RISCVCPU),
Signature · 1 line
2.25.1
Re: [PATCH v6 11/23] target/riscv: Implement AIA hvictl and hviprioX CSRs
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:41寫道:
Quoted 93 lines
From: Anup Patel <[email protected]>
The AIA hvictl and hviprioX CSRs allow hypervisor to control interrupts visible at VS-level. This patch implements AIA hvictl and hviprioX CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.h | 2 + target/riscv/csr.c | 126 +++++++++++++++++++++++++++++++++++++++++ target/riscv/machine.c | 2 + 3 files changed, 130 insertions(+)target/riscv/cpu.h +2
-0@@ -199,6 +199,7 @@ struct CPURISCVState {uint64_t htimedelta;/* Hypervisor controlled virtual interrupt priorities */ +target_ulong hvictl;uint8_t hviprio[64 ];/* Virtual CSRs */ @@ -475,6 +476,7 @@ static inline RISCVMXL riscv_cpu_mxl(CPURISCVState *env) return env->misa_mxl; }#endif +#define riscv_cpu_mxl_bits (env) (1UL << (4 + riscv_cpu_mxl(env)))/* * Encode LMUL to lmul as follows:
target/riscv/csr.c +9
-0@@ -230,6 +230,15 @@ static RISCVException pointer_masking(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +static int aia_hmode (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return hmode (env, csrno); +} +static int aia_hmode32 (CPURISCVState *env,int csrno) {if (!riscv_feature(env, RISCV_FEATURE_AIA)) {target/riscv/csr.c +3
-0@@ -1070,6 +1079,9 @@ static RISCVException rmw_sie64(CPURISCVState *env,int csrno, uint64_t mask = env->mideleg & S_MODE_INTERRUPTS;if (riscv_cpu_virt_enabled (env)) { +if (env->hvictl &HVICTL_VTI ) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + } ret =rmw_vsie64 (env,CSR_VSIE , ret_val, new_val, wr_mask); }else {ret = rmw_mie64(env, csrno, ret_val, new_val, wr_mask & mask);target/riscv/csr.c +3
-0@@ -1268,6 +1280,9 @@ static RISCVException rmw_sip64(CPURISCVState *env,int csrno, uint64_t mask = env->mideleg & sip_writable_mask;if (riscv_cpu_virt_enabled (env)) { +if (env->hvictl &HVICTL_VTI ) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + } ret =rmw_vsip64 (env,CSR_VSIP , ret_val, new_val, wr_mask); }else {ret = rmw_mip64(env, csrno, ret_val, new_val, wr_mask & mask);target/riscv/csr.c +9
-0@@ -1654,6 +1669,110 @@ static RISCVException write_htimedeltah(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } +static int read_hvictl(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val = env->hvictl; +return RISCV_EXCP_NONE ; +} + +static int write_hvictl(CPURISCVState *env, int csrno, target_ulong val) +{ + env->hvictl = val &HVICTL_VALID_MASK ;
target/riscv/csr.c static RISCVException · line 1691
Quoted 10 lines
target/riscv/csr.c +9
-0+return RISCV_EXCP_NONE ; +} + +static int read_hvipriox (CPURISCVState *env,int first_index, +uint8_t *iprio,target_ulong *val) +{ +int i, irq, rdzero, num_irqs =4 * (riscv_cpu_mxl_bits (env) /32 ); + +/* First index has to be multiple of numbe of irqs per register */
typo: number
Quoted 18 lines
target/riscv/csr.c +16
-0+if (first_index % num_irqs) { +return (riscv_cpu_virt_enabled (env)) ? +RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; + } + +/* Fill-up return value */ + *val =0 ; +for (i =0 ; i < num_irqs; i++) { +if (riscv_cpu_hviprio_index2irq (first_index + i, &irq, &rdzero)) { +continue ; + } +if (rdzero) { +continue ; + } + *val |= ((target_ulong )iprio[irq]) << (i *8 ); + }
target/riscv/csr.c static RISCVException write_htimedeltah(CPURISCVState *env, int csrno, · line 1715
Quoted 10 lines
target/riscv/csr.c +10
-0+ +return RISCV_EXCP_NONE ; +} + +static int write_hvipriox (CPURISCVState *env,int first_index, +uint8_t *iprio,target_ulong val) +{ +int i, irq, rdzero, num_irqs =4 * (riscv_cpu_mxl_bits (env) /32 ); + +/* First index has to be multiple of numbe of irqs per register */
typo: number
Quoted 111 lines
target/riscv/csr.c +3
-0+if (first_index % num_irqs) { +return (riscv_cpu_virt_enabled (env)) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT :
RISCV_EXCP_ILLEGAL_INST;target/riscv/csr.c +28
-0+ } + +/* Fill-up priority arrary */ +for (i =0 ; i < num_irqs; i++) { +if (riscv_cpu_hviprio_index2irq (first_index + i, &irq, &rdzero)) { +continue ; + } +if (rdzero) { + iprio[irq] =0 ; + }else { + iprio[irq] = (val >> (i *8 )) &0xff ; + } + } + +return RISCV_EXCP_NONE ; +} + +static int read_hviprio1 (CPURISCVState *env,int csrno,target_ulong *val) +{ +return read_hvipriox (env,0 , env->hviprio, val); +} + +static int write_hviprio1 (CPURISCVState *env,int csrno,target_ulong val) +{ +return write_hvipriox (env,0 , env->hviprio, val); +} + +static int read_hviprio1h(CPURISCVState *env, int csrno, target_ulong*val)
target/riscv/csr.c +5
-0+{ +return read_hvipriox (env,4 , env->hviprio, val); +} + +static int write_hviprio1h(CPURISCVState *env, int csrno, target_ulongval)
target/riscv/csr.c +15
-0+{ +return write_hvipriox (env,4 , env->hviprio, val); +} + +static int read_hviprio2 (CPURISCVState *env,int csrno,target_ulong *val) +{ +return read_hvipriox (env,8 , env->hviprio, val); +} + +static int write_hviprio2 (CPURISCVState *env,int csrno,target_ulong val) +{ +return write_hvipriox (env,8 , env->hviprio, val); +} + +static int read_hviprio2h(CPURISCVState *env, int csrno, target_ulong*val)
target/riscv/csr.c +5
-0+{ +return read_hvipriox (env,12 , env->hviprio, val); +} + +static int write_hviprio2h(CPURISCVState *env, int csrno, target_ulongval)
target/riscv/csr.c +9
-0+{ +return write_hvipriox (env,12 , env->hviprio, val); +} +/* Virtual CSR Registers */ static RISCVException read_vsstatus (CPURISCVState *env,int csrno,target_ulong *val)@@ -2308,9 +2427,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL2] = { "mtval2", hmode, read_mtval2, write_mtval2 }, [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst }, +/* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */ + [CSR_HVICTL] = { "hvictl", aia_hmode, read_hvictl, write_hvictl }, + [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, write_hviprio1 }, + [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, write_hviprio2 }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL,
rmw_hidelegh},
[CSR_HVIPH] = { "hviph",aia_hmode32, NULL, NULL,rmw_hviph},
+ [CSR_HVIPRIO1H] = { "hviprio1h",aia_hmode32,read_hviprio1h,write_hviprio1h},
+ [CSR_HVIPRIO2H] = { "hviprio2h",aia_hmode32,read_hviprio2h,write_hviprio2h},
[CSR_VSIEH] = { "vsieh",aia_hmode32, NULL, NULL,rmw_vsieh},
[CSR_VSIPH] = { "vsiph",aia_hmode32, NULL, NULL,rmw_vsiph},target/riscv/machine.c +2
-1@@ -92,6 +92,8 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), + +VMSTATE_UINTTL (env.hvictl,RISCVCPU ),VMSTATE_UINT8_ARRAY (env.hviprio,RISCVCPU ,64 ), VMSTATE_UINT64(env.vsstatus, RISCVCPU), --
Quoted 4 lines
2.25.1
Otherwise,
Re: [PATCH v6 11/23] target/riscv: Implement AIA hvictl and hviprioX CSRs
Quoted 95 lines · Frank Chang
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:41寫道:
Quoted 93 lines
From: Anup Patel <[email protected]>
The AIA hvictl and hviprioX CSRs allow hypervisor to control interrupts visible at VS-level. This patch implements AIA hvictl and hviprioX CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.h | 2 + target/riscv/csr.c | 126 +++++++++++++++++++++++++++++++++++++++++ target/riscv/machine.c | 2 + 3 files changed, 130 insertions(+)target/riscv/cpu.h +2
-0@@ -199,6 +199,7 @@ struct CPURISCVState {uint64_t htimedelta;/* Hypervisor controlled virtual interrupt priorities */ +target_ulong hvictl;uint8_t hviprio[64 ];/* Virtual CSRs */ @@ -475,6 +476,7 @@ static inline RISCVMXL riscv_cpu_mxl(CPURISCVState *env) return env->misa_mxl; }#endif +#define riscv_cpu_mxl_bits (env) (1UL << (4 + riscv_cpu_mxl(env)))/* * Encode LMUL to lmul as follows: target/riscv/csr.c +28
-0@@ -230,6 +230,15 @@ static RISCVException pointer_masking(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +static int aia_hmode (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return hmode (env, csrno); +} +static int aia_hmode32 (CPURISCVState *env,int csrno) {if (!riscv_feature (env,RISCV_FEATURE_AIA )) {@@ -1070,6 +1079,9 @@ static RISCVException rmw_sie64(CPURISCVState *env, int csrno,uint64_t mask = env->mideleg &S_MODE_INTERRUPTS ;if (riscv_cpu_virt_enabled (env)) { +if (env->hvictl &HVICTL_VTI ) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + } ret =rmw_vsie64 (env,CSR_VSIE , ret_val, new_val, wr_mask); }else { ret =rmw_mie64 (env, csrno, ret_val, new_val, wr_mask & mask);@@ -1268,6 +1280,9 @@ static RISCVException rmw_sip64(CPURISCVState *env, int csrno,uint64_t mask = env->mideleg & sip_writable_mask;if (riscv_cpu_virt_enabled (env)) { +if (env->hvictl &HVICTL_VTI ) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + } ret =rmw_vsip64 (env,CSR_VSIP , ret_val, new_val, wr_mask); }else { ret =rmw_mip64 (env, csrno, ret_val, new_val, wr_mask & mask);@@ -1654,6 +1669,110 @@ static RISCVException write_htimedeltah(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static int read_hvictl (CPURISCVState *env,int csrno,target_ulong *val) +{ + *val = env->hvictl; +return RISCV_EXCP_NONE ; +} + +static int write_hvictl (CPURISCVState *env,int csrno,target_ulong val) +{ + env->hvictl = val &HVICTL_VALID_MASK ; +return RISCV_EXCP_NONE ; +} + +static int read_hvipriox(CPURISCVState *env, int first_index,
Quoted 8 lines
target/riscv/csr.c static RISCVException write_htimedeltah(CPURISCVState *env, int csrno, · line 1689
Quoted 5 lines
target/riscv/csr.c +5
-0+ uint8_t *iprio, target_ulong *val) +{ + int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32); + +/* First index has to be multiple of numbe of irqs per register */ typo: number
Okay, I will update.
Quoted 23 lines
Quoted 23 lines
target/riscv/csr.c +21
-0+if (first_index % num_irqs) { +return (riscv_cpu_virt_enabled (env)) ? +RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; + } + +/* Fill-up return value */ + *val =0 ; +for (i =0 ; i < num_irqs; i++) { +if (riscv_cpu_hviprio_index2irq (first_index + i, &irq, &rdzero)) { +continue ; + } +if (rdzero) { +continue ; + } + *val |= ((target_ulong )iprio[irq]) << (i *8 ); + } + +return RISCV_EXCP_NONE ; +} + +static int write_hvipriox(CPURISCVState *env, int first_index,
Quoted 8 lines
target/riscv/csr.c static RISCVException write_htimedeltah(CPURISCVState *env, int csrno, · line 1715
Quoted 5 lines
target/riscv/csr.c +5
-0+ uint8_t *iprio, target_ulong val) +{ + int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32); + +/* First index has to be multiple of numbe of irqs per register */ typo: number
Okay, I will update.
Quoted 94 lines
Quoted 94 lines
target/riscv/csr.c +67
-0+if (first_index % num_irqs) { +return (riscv_cpu_virt_enabled (env)) ? +RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; + } + +/* Fill-up priority arrary */ +for (i =0 ; i < num_irqs; i++) { +if (riscv_cpu_hviprio_index2irq (first_index + i, &irq, &rdzero)) { +continue ; + } +if (rdzero) { + iprio[irq] =0 ; + }else { + iprio[irq] = (val >> (i *8 )) &0xff ; + } + } + +return RISCV_EXCP_NONE ; +} + +static int read_hviprio1 (CPURISCVState *env,int csrno,target_ulong *val) +{ +return read_hvipriox (env,0 , env->hviprio, val); +} + +static int write_hviprio1 (CPURISCVState *env,int csrno,target_ulong val) +{ +return write_hvipriox (env,0 , env->hviprio, val); +} + +static int read_hviprio1h (CPURISCVState *env,int csrno,target_ulong *val) +{ +return read_hvipriox (env,4 , env->hviprio, val); +} + +static int write_hviprio1h (CPURISCVState *env,int csrno,target_ulong val) +{ +return write_hvipriox (env,4 , env->hviprio, val); +} + +static int read_hviprio2 (CPURISCVState *env,int csrno,target_ulong *val) +{ +return read_hvipriox (env,8 , env->hviprio, val); +} + +static int write_hviprio2 (CPURISCVState *env,int csrno,target_ulong val) +{ +return write_hvipriox (env,8 , env->hviprio, val); +} + +static int read_hviprio2h (CPURISCVState *env,int csrno,target_ulong *val) +{ +return read_hvipriox (env,12 , env->hviprio, val); +} + +static int write_hviprio2h (CPURISCVState *env,int csrno,target_ulong val) +{ +return write_hvipriox (env,12 , env->hviprio, val); +} +/* Virtual CSR Registers */ static RISCVException read_vsstatus (CPURISCVState *env,int csrno,target_ulong *val)@@ -2308,9 +2427,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL2] = { "mtval2", hmode, read_mtval2, write_mtval2 }, [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst }, + /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */ + [CSR_HVICTL] = { "hvictl", aia_hmode, read_hvictl, write_hvictl }, + [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, write_hviprio1 }, + [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, write_hviprio2 }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL, rmw_hidelegh }, [CSR_HVIPH] = { "hviph", aia_hmode32, NULL, NULL, rmw_hviph }, + [CSR_HVIPRIO1H] = { "hviprio1h", aia_hmode32, read_hviprio1h, write_hviprio1h }, + [CSR_HVIPRIO2H] = { "hviprio2h", aia_hmode32, read_hviprio2h, write_hviprio2h }, [CSR_VSIEH] = { "vsieh", aia_hmode32, NULL, NULL, rmw_vsieh }, [CSR_VSIPH] = { "vsiph", aia_hmode32, NULL, NULL, rmw_vsiph },target/riscv/machine.c +2
-0@@ -92,6 +92,8 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.hgeie,RISCVCPU ),VMSTATE_UINTTL (env.hgeip,RISCVCPU ),VMSTATE_UINT64 (env.htimedelta,RISCVCPU ), + +VMSTATE_UINTTL (env.hvictl,RISCVCPU ), VMSTATE_UINT8_ARRAY(env.hviprio, RISCVCPU, 64),
Quoted 8 lines
Quoted 5 lines
target/riscv/machine.c +0
-1VMSTATE_UINT64(env.vsstatus, RISCVCPU), --2.25.1
Otherwise,
Reviewed-by Frank Chang <[email protected]>
Regards,
Anup
PATCH v6 12/23 target/riscv: Implement AIA interrupt filtering CSRs
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The AIA specificaiton adds interrupt filtering support for M-mode and HS-mode. Using AIA interrupt filtering M-mode and H-mode can take local interrupt 13 or above and selectively inject same local interrupt to lower privilege modes.
At the moment, we don't have any local interrupts above 12 so we add dummy implementation (i.e. read zero and ignore write) of AIA interrupt filtering CSRs.
--- target/riscv/csr.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
target/riscv/csr.c +23 -0
@@ -154,6 +154,15 @@ static RISCVException any32(CPURISCVState *env, int csrno) } +static int aia_any (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return any (env, csrno); +} +static int aia_any32 (CPURISCVState *env,int csrno) {if (!riscv_feature (env,RISCV_FEATURE_AIA )) {
@@ -553,6 +562,12 @@ static RISCVException read_zero(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException write_ignore (CPURISCVState *env,int csrno, +target_ulong val) +{ +return RISCV_EXCP_NONE ; +} +static RISCVException read_mhartid (CPURISCVState *env,int csrno,target_ulong *val) {
@@ -2374,9 +2389,15 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, + /* Virtual Interrupts for Supervisor Level (AIA) */ + [CSR_MVIEN] = { "mvien", aia_any, read_zero, write_ignore }, + [CSR_MVIP] = { "mvip", aia_any, read_zero, write_ignore }, + /* Machine-Level High-Half CSRs (AIA) */ [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh }, [CSR_MIEH] = { "mieh", aia_any32, NULL, NULL, rmw_mieh }, + [CSR_MVIENH] = { "mvienh", aia_any32, read_zero, write_ignore }, + [CSR_MVIPH] = { "mviph", aia_any32, read_zero, write_ignore }, [CSR_MIPH] = { "miph", aia_any32, NULL, NULL, rmw_miph }, /* Supervisor Trap Setup */
@@ -2428,12 +2449,14 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst }, /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */ + [CSR_HVIEN] = { "hvien", aia_hmode, read_zero, write_ignore }, [CSR_HVICTL] = { "hvictl", aia_hmode, read_hvictl, write_hvictl }, [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, write_hviprio1 }, [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, write_hviprio2 }, /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL, rmw_hidelegh }, + [CSR_HVIENH] = { "hvienh", aia_hmode32, read_zero, write_ignore }, [CSR_HVIPH] = { "hviph", aia_hmode32, NULL, NULL, rmw_hviph }, [CSR_HVIPRIO1H] = { "hviprio1h", aia_hmode32, read_hviprio1h, write_hviprio1h }, [CSR_HVIPRIO2H] = { "hviprio2h", aia_hmode32, read_hviprio2h, write_hviprio2h },
Signature · 1 line
2.25.1
Re: [PATCH v6 12/23] target/riscv: Implement AIA interrupt filtering CSRs
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:41寫道:
Quoted 91 lines
From: Anup Patel <[email protected]>
The AIA specificaiton adds interrupt filtering support for M-mode and HS-mode. Using AIA interrupt filtering M-mode and H-mode can take local interrupt 13 or above and selectively inject same local interrupt to lower privilege modes.
At the moment, we don't have any local interrupts above 12 so we add dummy implementation (i.e. read zero and ignore write) of AIA interrupt filtering CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/csr.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)target/riscv/csr.c +9
-0@@ -154,6 +154,15 @@ static RISCVException any32(CPURISCVState *env, int csrno) } +static int aia_any (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return any (env, csrno); +} +static int aia_any32 (CPURISCVState *env,int csrno) {if (!riscv_feature(env, RISCV_FEATURE_AIA)) {target/riscv/csr.c +6
-0@@ -553,6 +562,12 @@ static RISCVException read_zero(CPURISCVState *env,int csrno, return RISCV_EXCP_NONE; } +static RISCVException write_ignore (CPURISCVState *env,int csrno, +target_ulong val) +{ +return RISCV_EXCP_NONE ; +} +static RISCVException read_mhartid (CPURISCVState *env,int csrno,target_ulong *val){
target/riscv/csr.c +7
-0@@ -2374,9 +2389,15 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, + /* Virtual Interrupts for Supervisor Level (AIA) */ + [CSR_MVIEN] = { "mvien", aia_any, read_zero, write_ignore }, + [CSR_MVIP] = { "mvip", aia_any, read_zero, write_ignore }, + /* Machine-Level High-Half CSRs (AIA) */ [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh }, [CSR_MIEH] = { "mieh", aia_any32, NULL, NULL, rmw_mieh }, + [CSR_MVIENH] = { "mvienh", aia_any32, read_zero, write_ignore }, + [CSR_MVIPH] = { "mviph", aia_any32, read_zero, write_ignore }, [CSR_MIPH] = { "miph", aia_any32, NULL, NULL, rmw_miph }, /* Supervisor Trap Setup */@@ -2428,12 +2449,14 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst },/* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */ + [CSR_HVIEN] = { "hvien", aia_hmode, read_zero, write_ignore }, [CSR_HVICTL] = { "hvictl", aia_hmode, read_hvictl, write_hvictl }, [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, write_hviprio1 }, [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, write_hviprio2 }, /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL,
rmw_hidelegh},
+ [CSR_HVIENH] = { "hvienh",aia_hmode32,read_zero,write_ignore},
[CSR_HVIPH] = { "hviph",aia_hmode32, NULL, NULL,rmw_hviph},
Quoted 9 lines
[CSR_HVIPRIO1H] = { "hviprio1h", aia_hmode32, read_hviprio1h, write_hviprio1h }, [CSR_HVIPRIO2H] = { "hviprio2h", aia_hmode32, read_hviprio2h,
write_hviprio2h},
--
2.25.1
PATCH v6 13/23 target/riscv: Implement AIA mtopi, stopi, and vstopi CSRs
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The AIA specification introduces new [m|s|vs]topi CSRs for reporting pending local IRQ number and associated IRQ priority.
--- target/riscv/csr.c | 156 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+)
target/riscv/csr.c +156 -0
@@ -190,6 +190,15 @@ static int smode32(CPURISCVState *env, int csrno)return smode (env, csrno); } +static int aia_smode (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return smode (env, csrno); +} +static int aia_smode32 (CPURISCVState *env,int csrno) {if (!riscv_feature (env,RISCV_FEATURE_AIA )) {
@@ -502,6 +511,8 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,#define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP))#define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS)) +#define VSTOPI_NUM_SRCS 5 +static const uint64_t delegable_ints =S_MODE_INTERRUPTS |VS_MODE_INTERRUPTS ;static const uint64_t vs_delegable_ints =VS_MODE_INTERRUPTS ;
@@ -862,6 +873,28 @@ static RISCVException rmw_mieh(CPURISCVState *env, int csrno,return ret; } +static int read_mtopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq; +uint8_t iprio; + + irq =riscv_cpu_mirq_pending (env); +if (irq <=0 || irq >63 ) { + *val =0 ; + }else { + iprio = env->miprio[irq]; +if (!iprio) { +if (riscv_cpu_default_priority (irq) >IPRIO_DEFAULT_M ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + *val = (irq &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException read_mtvec (CPURISCVState *env,int csrno,target_ulong *val) {
@@ -1391,6 +1424,120 @@ static RISCVException write_satp(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static int read_vstopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq, ret; +target_ulong topei; +uint64_t vseip, vsgein; +uint32_t iid, iprio, hviid, hviprio, gein; +uint32_t s, scount =0 , siid[VSTOPI_NUM_SRCS ], siprio[VSTOPI_NUM_SRCS ]; + + gein =get_field (env->hstatus,HSTATUS_VGEIN ); + hviid =get_field (env->hvictl,HVICTL_IID ); + hviprio =get_field (env->hvictl,HVICTL_IPRIO ); + +if (gein) { + vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ; + vseip = env->mie & (env->mip | vsgein) &MIP_VSEIP ; +if (gein <= env->geilen && vseip) { + siid[scount] =IRQ_S_EXT ; + siprio[scount] =IPRIO_MMAXIPRIO +1 ; +if (env->aia_ireg_rmw_fn[PRV_S ]) { +/* +* Call machine specific IMSIC register emulation for +* reading TOPEI. +*/ + ret = env->aia_ireg_rmw_fn[PRV_S ]( + env->aia_ireg_rmw_fn_arg[PRV_S ], +AIA_MAKE_IREG (ISELECT_IMSIC_TOPEI ,PRV_S , true, gein, +riscv_cpu_mxl_bits (env)), + &topei,0 ,0 ); +if (!ret && topei) { + siprio[scount] = topei &IMSIC_TOPEI_IPRIO_MASK ; + } + } + scount++; + } + }else { +if (hviid ==IRQ_S_EXT && hviprio) { + siid[scount] =IRQ_S_EXT ; + siprio[scount] = hviprio; + scount++; + } + } + +if (env->hvictl &HVICTL_VTI ) { +if (hviid !=IRQ_S_EXT ) { + siid[scount] = hviid; + siprio[scount] = hviprio; + scount++; + } + }else { + irq =riscv_cpu_vsirq_pending (env); +if (irq !=IRQ_S_EXT &&0 < irq && irq <=63 ) { + siid[scount] = irq; + siprio[scount] = env->hviprio[irq]; + scount++; + } + } + + iid =0 ; + iprio =UINT_MAX ; +for (s =0 ; s < scount; s++) { +if (siprio[s] < iprio) { + iid = siid[s]; + iprio = siprio[s]; + } + } + +if (iid) { +if (env->hvictl &HVICTL_IPRIOM ) { +if (iprio >IPRIO_MMAXIPRIO ) { + iprio =IPRIO_MMAXIPRIO ; + } +if (!iprio) { +if (riscv_cpu_default_priority (iid) >IPRIO_DEFAULT_S ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + }else { + iprio =1 ; + } + }else { + iprio =0 ; + } + + *val = (iid &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; +return RISCV_EXCP_NONE ; +} + +static int read_stopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq; +uint8_t iprio; + +if (riscv_cpu_virt_enabled (env)) { +return read_vstopi (env,CSR_VSTOPI , val); + } + + irq =riscv_cpu_sirq_pending (env); +if (irq <=0 || irq >63 ) { + *val =0 ; + }else { + iprio = env->siprio[irq]; +if (!iprio) { +if (riscv_cpu_default_priority (irq) >IPRIO_DEFAULT_S ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + *val = (irq &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; + } + +return RISCV_EXCP_NONE ; +} +/* Hypervisor Extensions */ static RISCVException read_hstatus (CPURISCVState *env,int csrno,target_ulong *val)
@@ -2389,6 +2536,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, + /* Machine-Level Interrupts (AIA) */ + [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi }, + /* Virtual Interrupts for Supervisor Level (AIA) */ [CSR_MVIEN] = { "mvien", aia_any, read_zero, write_ignore }, [CSR_MVIP] = { "mvip", aia_any, read_zero, write_ignore },
@@ -2416,6 +2566,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Supervisor Protection and Translation */ [CSR_SATP] = { "satp", smode, read_satp, write_satp }, + /* Supervisor-Level Interrupts (AIA) */ + [CSR_STOPI] = { "stopi", aia_smode, read_stopi }, + /* Supervisor-Level High-Half CSRs (AIA) */ [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph },
@@ -2454,6 +2607,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, write_hviprio1 }, [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, write_hviprio2 }, + /* VS-Level Interrupts (H-extension with AIA) */ + [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL, rmw_hidelegh }, [CSR_HVIENH] = { "hvienh", aia_hmode32, read_zero, write_ignore },
Signature · 1 line
2.25.1
Re: [PATCH v6 13/23] target/riscv: Implement AIA mtopi, stopi, and vstopi CSRs
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:47寫道:
Quoted 32 lines
From: Anup Patel <[email protected]>
The AIA specification introduces new [m|s|vs]topi CSRs for reporting pending local IRQ number and associated IRQ priority.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- target/riscv/csr.c | 156 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+)target/riscv/csr.c +9
-0@@ -190,6 +190,15 @@ static int smode32(CPURISCVState *env, int csrno)return smode (env, csrno); } +static int aia_smode (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return smode (env, csrno); +} +static int aia_smode32 (CPURISCVState *env,int csrno) {if (!riscv_feature (env,RISCV_FEATURE_AIA )) {
target/riscv/csr.c static RISCVException read_timeh(CPURISCVState *env, · line 516
Quoted 6 lines
target/riscv/csr.c +1
-0@@ -502,6 +511,8 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,#define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP))#define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS)) +#define VSTOPI_NUM_SRCS 5
Nit:VSTOPI_NUM_SRCS can be reduced to 2 as AIA spec says:
The list of candidate interrupts can be reduced to two finalists
relatively.
But it's fine to keep it to 5 if you think it has better readability.
Quoted 188 lines
target/riscv/csr.c +1
-0+ static const uint64_t delegable_ints = S_MODE_INTERRUPTS |VS_MODE_INTERRUPTS;static const
uint64_tvs_delegable_ints=VS_MODE_INTERRUPTS;target/riscv/csr.c +22
-0@@ -862,6 +873,28 @@ static RISCVException rmw_mieh(CPURISCVState *env,int csrno, return ret; } +static int read_mtopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq; +uint8_t iprio; + + irq =riscv_cpu_mirq_pending (env); +if (irq <=0 || irq >63 ) { + *val =0 ; + }else { + iprio = env->miprio[irq]; +if (!iprio) { +if (riscv_cpu_default_priority (irq) >IPRIO_DEFAULT_M ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + *val = (irq &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException read_mtvec (CPURISCVState *env,int csrno,target_ulong *val){
target/riscv/csr.c +114
-0@@ -1391,6 +1424,120 @@ static RISCVException write_satp(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } +static int read_vstopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq, ret; +target_ulong topei; +uint64_t vseip, vsgein; +uint32_t iid, iprio, hviid, hviprio, gein; +uint32_t s, scount =0 , siid[VSTOPI_NUM_SRCS ], siprio[VSTOPI_NUM_SRCS ]; + + gein =get_field (env->hstatus,HSTATUS_VGEIN ); + hviid =get_field (env->hvictl,HVICTL_IID ); + hviprio =get_field (env->hvictl,HVICTL_IPRIO ); + +if (gein) { + vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ; + vseip = env->mie & (env->mip | vsgein) &MIP_VSEIP ; +if (gein <= env->geilen && vseip) { + siid[scount] =IRQ_S_EXT ; + siprio[scount] =IPRIO_MMAXIPRIO +1 ; +if (env->aia_ireg_rmw_fn[PRV_S ]) { +/* +* Call machine specific IMSIC register emulation for +* reading TOPEI. +*/ + ret = env->aia_ireg_rmw_fn[PRV_S ]( + env->aia_ireg_rmw_fn_arg[PRV_S ], +AIA_MAKE_IREG (ISELECT_IMSIC_TOPEI ,PRV_S , true, gein, +riscv_cpu_mxl_bits (env)), + &topei,0 ,0 ); +if (!ret && topei) { + siprio[scount] = topei &IMSIC_TOPEI_IPRIO_MASK ; + } + } + scount++; + } + }else { +if (hviid ==IRQ_S_EXT && hviprio) { + siid[scount] =IRQ_S_EXT ; + siprio[scount] = hviprio; + scount++; + } + } + +if (env->hvictl &HVICTL_VTI ) { +if (hviid !=IRQ_S_EXT ) { + siid[scount] = hviid; + siprio[scount] = hviprio; + scount++; + } + }else { + irq =riscv_cpu_vsirq_pending (env); +if (irq !=IRQ_S_EXT &&0 < irq && irq <=63 ) { + siid[scount] = irq; + siprio[scount] = env->hviprio[irq]; + scount++; + } + } + + iid =0 ; + iprio =UINT_MAX ; +for (s =0 ; s < scount; s++) { +if (siprio[s] < iprio) { + iid = siid[s]; + iprio = siprio[s]; + } + } + +if (iid) { +if (env->hvictl &HVICTL_IPRIOM ) { +if (iprio >IPRIO_MMAXIPRIO ) { + iprio =IPRIO_MMAXIPRIO ; + } +if (!iprio) { +if (riscv_cpu_default_priority (iid) >IPRIO_DEFAULT_S ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + }else { + iprio =1 ; + } + }else { + iprio =0 ; + } + + *val = (iid &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; +return RISCV_EXCP_NONE ; +} + +static int read_stopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq; +uint8_t iprio; + +if (riscv_cpu_virt_enabled (env)) { +return read_vstopi (env,CSR_VSTOPI , val); + } + + irq =riscv_cpu_sirq_pending (env); +if (irq <=0 || irq >63 ) { + *val =0 ; + }else { + iprio = env->siprio[irq]; +if (!iprio) { +if (riscv_cpu_default_priority (irq) >IPRIO_DEFAULT_S ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + *val = (irq &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; + } + +return RISCV_EXCP_NONE ; +} +/* Hypervisor Extensions */
static RISCVException read_hstatus(CPURISCVState *env, int csrno,
target_ulong*val)target/riscv/csr.c +9
-0@@ -2389,6 +2536,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, + /* Machine-Level Interrupts (AIA) */ + [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi }, + /* Virtual Interrupts for Supervisor Level (AIA) */ [CSR_MVIEN] = { "mvien", aia_any, read_zero, write_ignore }, [CSR_MVIP] = { "mvip", aia_any, read_zero, write_ignore },@@ -2416,6 +2566,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Supervisor Protection and Translation */ [CSR_SATP] = { "satp", smode, read_satp, write_satp }, + /* Supervisor-Level Interrupts (AIA) */ + [CSR_STOPI] = { "stopi", aia_smode, read_stopi }, + /* Supervisor-Level High-Half CSRs (AIA) */ [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph },@@ -2454,6 +2607,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, write_hviprio1 }, [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, write_hviprio2 }, + /* VS-Level Interrupts (H-extension with AIA) */ + [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */
Quoted 9 lines
[
CSR_HIDELEGH] = { "hidelegh",aia_hmode32, NULL, NULL,rmw_hidelegh},
[CSR_HVIENH] = { "hvienh",aia_hmode32,read_zero,write_ignore},
--
2.25.1
Re: [PATCH v6 13/23] target/riscv: Implement AIA mtopi, stopi, and vstopi CSRs
Quoted 38 lines · Frank Chang
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:47寫道:
Quoted 36 lines
From: Anup Patel <[email protected]>
The AIA specification introduces new [m|s|vs]topi CSRs for reporting pending local IRQ number and associated IRQ priority.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- target/riscv/csr.c | 156 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+)target/riscv/csr.c +9
-0@@ -190,6 +190,15 @@ static int smode32(CPURISCVState *env, int csrno)return smode (env, csrno); } +static int aia_smode (CPURISCVState *env,int csrno) +{ +if (!riscv_feature (env,RISCV_FEATURE_AIA )) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return smode (env, csrno); +} +static int aia_smode32 (CPURISCVState *env,int csrno) {if (!riscv_feature (env,RISCV_FEATURE_AIA )) {@@ -502,6 +511,8 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,#define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP))#define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS))
Quoted 7 lines
target/riscv/csr.c static RISCVException read_timeh(CPURISCVState *env, int csrno, · line 514
Quoted 1 line
target/riscv/csr.c +1
-0+#define VSTOPI_NUM_SRCS 5Nit:
VSTOPI_NUM_SRCScan be reduced to 2 as AIA spec says:
The list of candidate interrupts can be reduced to two finalists relatively.
But it's fine to keep it to 5 if you think it has better readability.
Let's keep is 5 for now. This is little high to ensure that we don't run out space in the candidate array.
Regards,
Anup
Quoted 192 lines
Quoted 187 lines
target/riscv/csr.c +146
-1+static const uint64_t delegable_ints =S_MODE_INTERRUPTS |VS_MODE_INTERRUPTS ;static const uint64_t vs_delegable_ints =VS_MODE_INTERRUPTS ;@@ -862,6 +873,28 @@ static RISCVException rmw_mieh(CPURISCVState *env, int csrno,return ret; } +static int read_mtopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq; +uint8_t iprio; + + irq =riscv_cpu_mirq_pending (env); +if (irq <=0 || irq >63 ) { + *val =0 ; + }else { + iprio = env->miprio[irq]; +if (!iprio) { +if (riscv_cpu_default_priority (irq) >IPRIO_DEFAULT_M ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + *val = (irq &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException read_mtvec (CPURISCVState *env,int csrno,target_ulong *val) {@@ -1391,6 +1424,120 @@ static RISCVException write_satp(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static int read_vstopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq, ret; +target_ulong topei; +uint64_t vseip, vsgein; +uint32_t iid, iprio, hviid, hviprio, gein; +uint32_t s, scount =0 , siid[VSTOPI_NUM_SRCS ], siprio[VSTOPI_NUM_SRCS ]; + + gein =get_field (env->hstatus,HSTATUS_VGEIN ); + hviid =get_field (env->hvictl,HVICTL_IID ); + hviprio =get_field (env->hvictl,HVICTL_IPRIO ); + +if (gein) { + vsgein = (env->hgeip & (1ULL << gein)) ?MIP_VSEIP :0 ; + vseip = env->mie & (env->mip | vsgein) &MIP_VSEIP ; +if (gein <= env->geilen && vseip) { + siid[scount] =IRQ_S_EXT ; + siprio[scount] =IPRIO_MMAXIPRIO +1 ; +if (env->aia_ireg_rmw_fn[PRV_S ]) { +/* +* Call machine specific IMSIC register emulation for +* reading TOPEI. +*/ + ret = env->aia_ireg_rmw_fn[PRV_S ]( + env->aia_ireg_rmw_fn_arg[PRV_S ], +AIA_MAKE_IREG (ISELECT_IMSIC_TOPEI ,PRV_S , true, gein, +riscv_cpu_mxl_bits (env)), + &topei,0 ,0 ); +if (!ret && topei) { + siprio[scount] = topei &IMSIC_TOPEI_IPRIO_MASK ; + } + } + scount++; + } + }else { +if (hviid ==IRQ_S_EXT && hviprio) { + siid[scount] =IRQ_S_EXT ; + siprio[scount] = hviprio; + scount++; + } + } + +if (env->hvictl &HVICTL_VTI ) { +if (hviid !=IRQ_S_EXT ) { + siid[scount] = hviid; + siprio[scount] = hviprio; + scount++; + } + }else { + irq =riscv_cpu_vsirq_pending (env); +if (irq !=IRQ_S_EXT &&0 < irq && irq <=63 ) { + siid[scount] = irq; + siprio[scount] = env->hviprio[irq]; + scount++; + } + } + + iid =0 ; + iprio =UINT_MAX ; +for (s =0 ; s < scount; s++) { +if (siprio[s] < iprio) { + iid = siid[s]; + iprio = siprio[s]; + } + } + +if (iid) { +if (env->hvictl &HVICTL_IPRIOM ) { +if (iprio >IPRIO_MMAXIPRIO ) { + iprio =IPRIO_MMAXIPRIO ; + } +if (!iprio) { +if (riscv_cpu_default_priority (iid) >IPRIO_DEFAULT_S ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + }else { + iprio =1 ; + } + }else { + iprio =0 ; + } + + *val = (iid &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; +return RISCV_EXCP_NONE ; +} + +static int read_stopi (CPURISCVState *env,int csrno,target_ulong *val) +{ +int irq; +uint8_t iprio; + +if (riscv_cpu_virt_enabled (env)) { +return read_vstopi (env,CSR_VSTOPI , val); + } + + irq =riscv_cpu_sirq_pending (env); +if (irq <=0 || irq >63 ) { + *val =0 ; + }else { + iprio = env->siprio[irq]; +if (!iprio) { +if (riscv_cpu_default_priority (irq) >IPRIO_DEFAULT_S ) { + iprio =IPRIO_MMAXIPRIO ; + } + } + *val = (irq &TOPI_IID_MASK ) <<TOPI_IID_SHIFT ; + *val |= iprio; + } + +return RISCV_EXCP_NONE ; +} +/* Hypervisor Extensions */ static RISCVException read_hstatus (CPURISCVState *env,int csrno,target_ulong *val)@@ -2389,6 +2536,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, + /* Machine-Level Interrupts (AIA) */ + [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi }, + /* Virtual Interrupts for Supervisor Level (AIA) */ [CSR_MVIEN] = { "mvien", aia_any, read_zero, write_ignore }, [CSR_MVIP] = { "mvip", aia_any, read_zero, write_ignore },@@ -2416,6 +2566,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Supervisor Protection and Translation */ [CSR_SATP] = { "satp", smode, read_satp, write_satp }, + /* Supervisor-Level Interrupts (AIA) */ + [CSR_STOPI] = { "stopi", aia_smode, read_stopi }, + /* Supervisor-Level High-Half CSRs (AIA) */ [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph },@@ -2454,6 +2607,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, write_hviprio1 }, [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, write_hviprio2 }, + /* VS-Level Interrupts (H-extension with AIA) */ + [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL, rmw_hidelegh }, [CSR_HVIENH] = { "hvienh", aia_hmode32, read_zero, write_ignore }, --Quoted 3 lines
2.25.1
Reviewed-by Frank Chang <[email protected]>
PATCH v6 14/23 target/riscv: Implement AIA xiselect and xireg CSRs
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The AIA specification defines [m|s|vs]iselect and [m|s|vs]ireg CSRs which allow indirect access to interrupt priority arrays and per-HART IMSIC registers. This patch implements AIA xiselect and xireg CSRs.
--- target/riscv/cpu.h | 7 ++ target/riscv/csr.c | 175 +++++++++++++++++++++++++++++++++++++++++ target/riscv/machine.c | 3 + 3 files changed, 185 insertions(+)
target/riscv/cpu.h +7 -0
@@ -186,6 +186,10 @@ struct CPURISCVState {uint8_t miprio[64 ];uint8_t siprio[64 ]; +/* AIA CSRs */ +target_ulong miselect; +target_ulong siselect; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;
@@ -215,6 +219,9 @@ struct CPURISCVState {target_ulong vstval;target_ulong vsatp; +/* AIA VS-mode CSRs */ +target_ulong vsiselect; +target_ulong mtval2;target_ulong mtinst;
target/riscv/csr.c +175 -0
199 lines not on this page · raw message
target/riscv/machine.c +3 -0
@@ -103,6 +103,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.vscause,RISCVCPU ),VMSTATE_UINTTL (env.vstval,RISCVCPU ),VMSTATE_UINTTL (env.vsatp,RISCVCPU ), +VMSTATE_UINTTL (env.vsiselect,RISCVCPU ),VMSTATE_UINTTL (env.mtval2,RISCVCPU ), VMSTATE_UINTTL(env.mtinst, RISCVCPU),
@@ -210,6 +211,8 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.mepc,RISCVCPU ),VMSTATE_UINTTL (env.mcause,RISCVCPU ),VMSTATE_UINTTL (env.mtval,RISCVCPU ), +VMSTATE_UINTTL (env.miselect,RISCVCPU ), +VMSTATE_UINTTL (env.siselect,RISCVCPU ),VMSTATE_UINTTL (env.scounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcounteren,RISCVCPU ), VMSTATE_UINTTL(env.sscratch, RISCVCPU),
Signature · 1 line
2.25.1
Re: [PATCH v6 14/23] target/riscv: Implement AIA xiselect and xireg CSRs
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:51寫道:
Quoted 281 lines
From: Anup Patel <[email protected]>
The AIA specification defines [m|s|vs]iselect and [m|s|vs]ireg CSRs which allow indirect access to interrupt priority arrays and per-HART IMSIC registers. This patch implements AIA xiselect and xireg CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- target/riscv/cpu.h | 7 ++ target/riscv/csr.c | 175 +++++++++++++++++++++++++++++++++++++++++ target/riscv/machine.c | 3 + 3 files changed, 185 insertions(+)target/riscv/cpu.h +7
-0@@ -186,6 +186,10 @@ struct CPURISCVState {uint8_t miprio[64 ];uint8_t siprio[64 ]; +/* AIA CSRs */ +target_ulong miselect; +target_ulong siselect; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;@@ -215,6 +219,9 @@ struct CPURISCVState {target_ulong vstval;target_ulong vsatp; +/* AIA VS-mode CSRs */ +target_ulong vsiselect; +target_ulong mtval2;target_ulong mtinst;target/riscv/csr.c +149
-0@@ -895,6 +895,169 @@ static int read_mtopi(CPURISCVState *env, int csrno, target_ulong *val)return RISCV_EXCP_NONE ; } +static int aia_xlate_vs_csrno (CPURISCVState *env,int csrno) +{ +if (!riscv_cpu_virt_enabled (env)) { +return csrno; + } + +switch (csrno) { +case CSR_SISELECT : +return CSR_VSISELECT ; +case CSR_SIREG : +return CSR_VSIREG ; +default : +return csrno; + }; +} + +static int rmw_xiselect (CPURISCVState *env,int csrno,target_ulong *val, +target_ulong new_val,target_ulong wr_mask) +{ +target_ulong *iselect; + +/* Translate CSR number for VS-mode */ + csrno =aia_xlate_vs_csrno (env, csrno); + +/* Find the iselect CSR based on CSR number */ +switch (csrno) { +case CSR_MISELECT : + iselect = &env->miselect; +break ; +case CSR_SISELECT : + iselect = &env->siselect; +break ; +case CSR_VSISELECT : + iselect = &env->vsiselect; +break ; +default : +return RISCV_EXCP_ILLEGAL_INST ; + }; + +if (val) { + *val = *iselect; + } + + wr_mask &=ISELECT_MASK ; +if (wr_mask) { + *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask); + } + +return RISCV_EXCP_NONE ; +} + +static int rmw_iprio (target_ulong xlen, +target_ulong iselect,uint8_t *iprio, +target_ulong *val,target_ulong new_val, +target_ulong wr_mask,int ext_irq_no) +{ +int i, firq, nirqs; +target_ulong old_val; + +if (iselect <ISELECT_IPRIO0 ||ISELECT_IPRIO15 < iselect) { +return -EINVAL ; + } +if (xlen !=32 && iselect &0x1 ) { +return -EINVAL ; + } + + nirqs =4 * (xlen /32 ); + firq = ((iselect -ISELECT_IPRIO0 ) / (xlen /32 )) * (nirqs); + + old_val =0 ; +for (i =0 ; i < nirqs; i++) { + old_val |= ((target_ulong )iprio[firq + i]) << (IPRIO_IRQ_BITS * i); + } + +if (val) { + *val = old_val; + } + +if (wr_mask) { + new_val = (old_val & ~wr_mask) | (new_val & wr_mask); +for (i =0 ; i < nirqs; i++) { +/* +* M-level and S-level external IRQ priority always read-only +* zero. This means default priority order is always preferred +* for M-level and S-level external IRQs. +*/ +if ((firq + i) == ext_irq_no) { +continue ; + } + iprio[firq + i] = (new_val >> (IPRIO_IRQ_BITS * i)) &0xff ; + } + } + +return 0 ; +} + +static int rmw_xireg (CPURISCVState *env,int csrno,target_ulong *val, +target_ulong new_val,target_ulong wr_mask) +{ +bool virt; +uint8_t *iprio; +int ret = -EINVAL ; +target_ulong priv, isel, vgein; + +/* Translate CSR number for VS-mode */ + csrno =aia_xlate_vs_csrno (env, csrno); + +/* Decode register details from CSR number */ + virt = false; +switch (csrno) { +case CSR_MIREG : + iprio = env->miprio; + isel = env->miselect; + priv =PRV_M ; +break ; +case CSR_SIREG : + iprio = env->siprio; + isel = env->siselect; + priv =PRV_S ; +break ; +case CSR_VSIREG : + iprio = env->hviprio; + isel = env->vsiselect; + priv =PRV_S ; + virt = true; +break ; +default : + goto done; + }; + +/* Find the selected guest interrupt file */ + vgein = (virt) ?get_field (env->hstatus,HSTATUS_VGEIN ) :0 ; + +if (ISELECT_IPRIO0 <= isel && isel <=ISELECT_IPRIO15 ) { +/* Local interrupt priority registers not available for VS-mode */ +if (!virt) { + ret =rmw_iprio (riscv_cpu_mxl_bits (env), + isel, iprio, val, new_val, wr_mask, + (priv ==PRV_M ) ?IRQ_M_EXT :IRQ_S_EXT ); + } + }else if (ISELECT_IMSIC_FIRST <= isel && isel <=ISELECT_IMSIC_LAST ) { +/* IMSIC registers only available when machine implements it. */ +if (env->aia_ireg_rmw_fn[priv]) { +/* Selected guest interrupt file should not be zero */ +if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } +/* Call machine specific IMSIC register emulation */ + ret =env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
target/riscv/csr.c +2
-0+ AIA_MAKE_IREG(isel, priv, virt, vgein, +
riscv_cpu_mxl_bits(env)),target/riscv/csr.c +8
-0+ val, new_val, wr_mask); + } + } + +done: +if (ret) { +return (riscv_cpu_virt_enabled (env) && virt) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT :
RISCV_EXCP_ILLEGAL_INST;target/riscv/csr.c +13
-0+ } +return RISCV_EXCP_NONE ; +} +static RISCVException read_mtvec (CPURISCVState *env,int csrno,target_ulong *val) {@@ -2536,6 +2699,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, + /* Machine-Level Window to Indirectly Accessed Registers (AIA) */ + [CSR_MISELECT] = { "miselect", aia_any, NULL, NULL, rmw_xiselect }, + [CSR_MIREG] = { "mireg", aia_any, NULL, NULL, rmw_xireg }, + /* Machine-Level Interrupts (AIA) */ [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi },@@ -2566,6 +2733,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Supervisor Protection and Translation */ [CSR_SATP] = { "satp", smode, read_satp, write_satp }, + /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */ + [CSR_SISELECT] = { "siselect", aia_smode, NULL, NULL, rmw_xiselect }, + [CSR_SIREG] = { "sireg", aia_smode, NULL, NULL, rmw_xireg }, + /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPI] = { "stopi", aia_smode, read_stopi },@@ -2607,6 +2778,10 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, write_hviprio1 }, [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, write_hviprio2 }, +/* VS-Level Window to Indirectly Accessed Registers (H-extension with AIA) */
+ [CSR_VSISELECT] = { "vsiselect",aia_hmode, NULL, NULL,rmw_xiselect},
+ [CSR_VSIREG] = { "vsireg",aia_hmode, NULL, NULL,rmw_xireg},target/riscv/csr.c +1
-0+ /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi },target/riscv/machine.c +3
-1@@ -103,6 +103,7 @@ static const VMStateDescription vmstate_hyper = {VMSTATE_UINTTL (env.vscause,RISCVCPU ),VMSTATE_UINTTL (env.vstval,RISCVCPU ),VMSTATE_UINTTL (env.vsatp,RISCVCPU ), +VMSTATE_UINTTL (env.vsiselect,RISCVCPU ),VMSTATE_UINTTL (env.mtval2,RISCVCPU ), VMSTATE_UINTTL(env.mtinst, RISCVCPU),@@ -210,6 +211,8 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.mepc,RISCVCPU ),VMSTATE_UINTTL (env.mcause,RISCVCPU ),VMSTATE_UINTTL (env.mtval,RISCVCPU ), +VMSTATE_UINTTL (env.miselect,RISCVCPU ), +VMSTATE_UINTTL (env.siselect,RISCVCPU ),VMSTATE_UINTTL (env.scounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcounteren,RISCVCPU ), VMSTATE_UINTTL(env.sscratch, RISCVCPU), --
Quoted 4 lines
2.25.1
PATCH v6 15/23 target/riscv: Implement AIA IMSIC interface CSRs
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The AIA specification defines IMSIC interface CSRs for easy access to the per-HART IMSIC registers without using indirect xiselect and xireg CSRs. This patch implements the AIA IMSIC interface CSRs.
--- target/riscv/csr.c | 202 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+)
target/riscv/csr.c +202 -0
@@ -906,6 +906,16 @@ static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno)return CSR_VSISELECT ;case CSR_SIREG :return CSR_VSIREG ; +case CSR_SSETEIPNUM : +return CSR_VSSETEIPNUM ; +case CSR_SCLREIPNUM : +return CSR_VSCLREIPNUM ; +case CSR_SSETEIENUM : +return CSR_VSSETEIENUM ; +case CSR_SCLREIENUM : +return CSR_VSCLREIENUM ; +case CSR_STOPEI : +return CSR_VSTOPEI ;default :return csrno; };
@@ -1058,6 +1068,177 @@ done:return RISCV_EXCP_NONE ; } +static int rmw_xsetclreinum (CPURISCVState *env,int csrno,target_ulong *val, +target_ulong new_val,target_ulong wr_mask) +{ +int ret = -EINVAL ; +bool set, pend, virt; +target_ulong priv, isel, vgein, xlen, nval, wmask; + +/* Translate CSR number for VS-mode */ + csrno =aia_xlate_vs_csrno (env, csrno); + +/* Decode register details from CSR number */ + virt = set = pend = false; +switch (csrno) { +case CSR_MSETEIPNUM : + priv =PRV_M ; + set = true; +break ; +case CSR_MCLREIPNUM : + priv =PRV_M ; + pend = true; +break ; +case CSR_MSETEIENUM : + priv =PRV_M ; + set = true; +break ; +case CSR_MCLREIENUM : + priv =PRV_M ; +break ; +case CSR_SSETEIPNUM : + priv =PRV_S ; + set = true; + pend = true; +break ; +case CSR_SCLREIPNUM : + priv =PRV_S ; + pend = true; +break ; +case CSR_SSETEIENUM : + priv =PRV_S ; + set = true; +break ; +case CSR_SCLREIENUM : + priv =PRV_S ; +break ; +case CSR_VSSETEIPNUM : + priv =PRV_S ; + virt = true; + set = true; + pend = true; +break ; +case CSR_VSCLREIPNUM : + priv =PRV_S ; + virt = true; + pend = true; +break ; +case CSR_VSSETEIENUM : + priv =PRV_S ; + virt = true; + set = true; +break ; +case CSR_VSCLREIENUM : + priv =PRV_S ; + virt = true; +break ; +default : + goto done; + }; + +/* IMSIC CSRs only available when machine implements IMSIC. */ +if (!env->aia_ireg_rmw_fn[priv]) { + goto done; + } + +/* Find the selected guest interrupt file */ + vgein = (virt) ?get_field (env->hstatus,HSTATUS_VGEIN ) :0 ; + +/* Selected guest interrupt file should be valid */ +if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } + +/* Set/Clear CSRs always read zero */ +if (val) { + *val =0 ; + } + +if (wr_mask) { +/* Get interrupt number */ + new_val &= wr_mask; + +/* Find target interrupt pending/enable register */ + xlen =riscv_cpu_mxl_bits (env); + isel = (new_val / xlen); + isel *= (xlen / IMSIC_EIPx_BITS); + isel += (pend) ?ISELECT_IMSIC_EIP0 :ISELECT_IMSIC_EIE0 ; + +/* Find the interrupt bit to be set/clear */ + wmask = ((target_ulong )1 ) << (new_val % xlen); + nval = (set) ? wmask :0 ; + +/* Call machine specific IMSIC register emulation */ + ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], +AIA_MAKE_IREG (isel, priv, virt, + vgein, xlen), +NULL , nval, wmask); + }else { + ret =0 ; + } + +done: +if (ret) { +return (riscv_cpu_virt_enabled (env) && virt) ? +RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; + } +return RISCV_EXCP_NONE ; +} + +static int rmw_xtopei (CPURISCVState *env,int csrno,target_ulong *val, +target_ulong new_val,target_ulong wr_mask) +{ +bool virt; +int ret = -EINVAL ; +target_ulong priv, vgein; + +/* Translate CSR number for VS-mode */ + csrno =aia_xlate_vs_csrno (env, csrno); + +/* Decode register details from CSR number */ + virt = false; +switch (csrno) { +case CSR_MTOPEI : + priv =PRV_M ; +break ; +case CSR_STOPEI : + priv =PRV_S ; +break ; +case CSR_VSTOPEI : + priv =PRV_S ; + virt = true; +break ; +default : + goto done; + }; + +/* IMSIC CSRs only available when machine implements IMSIC. */ +if (!env->aia_ireg_rmw_fn[priv]) { + goto done; + } + +/* Find the selected guest interrupt file */ + vgein = (virt) ?get_field (env->hstatus,HSTATUS_VGEIN ) :0 ; + +/* Selected guest interrupt file should be valid */ +if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } + +/* Call machine specific IMSIC register emulation for TOPEI */ + ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], +AIA_MAKE_IREG (ISELECT_IMSIC_TOPEI , priv, virt, vgein, +riscv_cpu_mxl_bits (env)), + val, new_val, wr_mask); + +done: +if (ret) { +return (riscv_cpu_virt_enabled (env) && virt) ? +RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; + } +return RISCV_EXCP_NONE ; +} +static RISCVException read_mtvec (CPURISCVState *env,int csrno,target_ulong *val) {
@@ -2706,6 +2887,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Machine-Level Interrupts (AIA) */ [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi }, + /* Machine-Level IMSIC Interface (AIA) */ + [CSR_MSETEIPNUM] = { "mseteipnum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MCLREIPNUM] = { "mclreipnum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MSETEIENUM] = { "mseteienum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MCLREIENUM] = { "mclreienum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MTOPEI] = { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, + /* Virtual Interrupts for Supervisor Level (AIA) */ [CSR_MVIEN] = { "mvien", aia_any, read_zero, write_ignore }, [CSR_MVIP] = { "mvip", aia_any, read_zero, write_ignore },
@@ -2740,6 +2928,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPI] = { "stopi", aia_smode, read_stopi }, + /* Supervisor-Level IMSIC Interface (AIA) */ + [CSR_SSETEIPNUM] = { "sseteipnum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SCLREIPNUM] = { "sclreipnum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SSETEIENUM] = { "sseteienum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SCLREIENUM] = { "sclreienum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_STOPEI] = { "stopei", aia_smode, NULL, NULL, rmw_xtopei }, + /* Supervisor-Level High-Half CSRs (AIA) */ [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph },
@@ -2785,6 +2980,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi }, + /* VS-Level IMSIC Interface (H-extension with AIA) */ + [CSR_VSSETEIPNUM] = { "vsseteipnum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSCLREIPNUM] = { "vsclreipnum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSSETEIENUM] = { "vsseteienum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSCLREIENUM] = { "vsclreienum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSTOPEI] = { "vstopei", aia_hmode, NULL, NULL, rmw_xtopei }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL, rmw_hidelegh }, [CSR_HVIENH] = { "hvienh", aia_hmode32, read_zero, write_ignore },
Signature · 1 line
2.25.1
Re: [PATCH v6 15/23] target/riscv: Implement AIA IMSIC interface CSRs
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:53寫道:
Quoted 46 lines
From: Anup Patel <[email protected]>
The AIA specification defines IMSIC interface CSRs for easy access to the per-HART IMSIC registers without using indirect xiselect and xireg CSRs. This patch implements the AIA IMSIC interface CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- target/riscv/csr.c | 202 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+)target/riscv/csr.c +10
-0@@ -906,6 +906,16 @@ static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno)return CSR_VSISELECT ;case CSR_SIREG :return CSR_VSIREG ; +case CSR_SSETEIPNUM : +return CSR_VSSETEIPNUM ; +case CSR_SCLREIPNUM : +return CSR_VSCLREIPNUM ; +case CSR_SSETEIENUM : +return CSR_VSSETEIENUM ; +case CSR_SCLREIENUM : +return CSR_VSCLREIENUM ; +case CSR_STOPEI : +return CSR_VSTOPEI ;default :return csrno;};target/riscv/csr.c +7
-0@@ -1058,6 +1068,177 @@ done:return RISCV_EXCP_NONE ; } +static int rmw_xsetclreinum (CPURISCVState *env,int csrno,target_ulong *val, +target_ulong new_val,target_ulong wr_mask) +{ +int ret = -EINVAL ; +bool set, pend, virt; +target_ulong priv, isel, vgein, xlen, nval, wmask; +
target/riscv/csr.c done: · line 1088
Quoted 10 lines
target/riscv/csr.c +9
-0+/* Translate CSR number for VS-mode */ + csrno =aia_xlate_vs_csrno (env, csrno); + +/* Decode register details from CSR number */ + virt = set = pend = false; +switch (csrno) { +case CSR_MSETEIPNUM : + priv =PRV_M ; + set = true;
Missing: *pend = true;* here?
Frank Chang
Quoted 223 lines
target/riscv/csr.c +151
-0+break ; +case CSR_MCLREIPNUM : + priv =PRV_M ; + pend = true; +break ; +case CSR_MSETEIENUM : + priv =PRV_M ; + set = true; +break ; +case CSR_MCLREIENUM : + priv =PRV_M ; +break ; +case CSR_SSETEIPNUM : + priv =PRV_S ; + set = true; + pend = true; +break ; +case CSR_SCLREIPNUM : + priv =PRV_S ; + pend = true; +break ; +case CSR_SSETEIENUM : + priv =PRV_S ; + set = true; +break ; +case CSR_SCLREIENUM : + priv =PRV_S ; +break ; +case CSR_VSSETEIPNUM : + priv =PRV_S ; + virt = true; + set = true; + pend = true; +break ; +case CSR_VSCLREIPNUM : + priv =PRV_S ; + virt = true; + pend = true; +break ; +case CSR_VSSETEIENUM : + priv =PRV_S ; + virt = true; + set = true; +break ; +case CSR_VSCLREIENUM : + priv =PRV_S ; + virt = true; +break ; +default : + goto done; + }; + +/* IMSIC CSRs only available when machine implements IMSIC. */ +if (!env->aia_ireg_rmw_fn[priv]) { + goto done; + } + +/* Find the selected guest interrupt file */ + vgein = (virt) ?get_field (env->hstatus,HSTATUS_VGEIN ) :0 ; + +/* Selected guest interrupt file should be valid */ +if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } + +/* Set/Clear CSRs always read zero */ +if (val) { + *val =0 ; + } + +if (wr_mask) { +/* Get interrupt number */ + new_val &= wr_mask; + +/* Find target interrupt pending/enable register */ + xlen =riscv_cpu_mxl_bits (env); + isel = (new_val / xlen); + isel *= (xlen / IMSIC_EIPx_BITS); + isel += (pend) ?ISELECT_IMSIC_EIP0 :ISELECT_IMSIC_EIE0 ; + +/* Find the interrupt bit to be set/clear */ + wmask = ((target_ulong )1 ) << (new_val % xlen); + nval = (set) ? wmask :0 ; + +/* Call machine specific IMSIC register emulation */ + ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], +AIA_MAKE_IREG (isel, priv, virt, + vgein, xlen), +NULL , nval, wmask); + }else { + ret =0 ; + } + +done: +if (ret) { +return (riscv_cpu_virt_enabled (env) && virt) ? +RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; + } +return RISCV_EXCP_NONE ; +} + +static int rmw_xtopei (CPURISCVState *env,int csrno,target_ulong *val, +target_ulong new_val,target_ulong wr_mask) +{ +bool virt; +int ret = -EINVAL ; +target_ulong priv, vgein; + +/* Translate CSR number for VS-mode */ + csrno =aia_xlate_vs_csrno (env, csrno); + +/* Decode register details from CSR number */ + virt = false; +switch (csrno) { +case CSR_MTOPEI : + priv =PRV_M ; +break ; +case CSR_STOPEI : + priv =PRV_S ; +break ; +case CSR_VSTOPEI : + priv =PRV_S ; + virt = true; +break ; +default : + goto done; + }; + +/* IMSIC CSRs only available when machine implements IMSIC. */ +if (!env->aia_ireg_rmw_fn[priv]) { + goto done; + } + +/* Find the selected guest interrupt file */ + vgein = (virt) ?get_field (env->hstatus,HSTATUS_VGEIN ) :0 ; + +/* Selected guest interrupt file should be valid */ +if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } + +/* Call machine specific IMSIC register emulation for TOPEI */ + ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], +AIA_MAKE_IREG (ISELECT_IMSIC_TOPEI , priv, virt, vgein, +riscv_cpu_mxl_bits (env)), + val, new_val, wr_mask); + +done: +if (ret) { +return (riscv_cpu_virt_enabled (env) && virt) ? + RISCV_EXCP_VIRT_INSTRUCTION_FAULT :
RISCV_EXCP_ILLEGAL_INST;target/riscv/csr.c +9
-0+ } +return RISCV_EXCP_NONE ; +} +static RISCVException read_mtvec (CPURISCVState *env,int csrno,target_ulong *val) {@@ -2706,6 +2887,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Machine-Level Interrupts (AIA) */ [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi }, + /* Machine-Level IMSIC Interface (AIA) */ + [CSR_MSETEIPNUM] = { "mseteipnum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MCLREIPNUM] = { "mclreipnum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MSETEIENUM] = { "mseteienum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MCLREIENUM] = { "mclreienum", aia_any, NULL, NULL,
rmw_xsetclreinum},target/riscv/csr.c +7
-0+ [CSR_MTOPEI] = { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, + /* Virtual Interrupts for Supervisor Level (AIA) */ [CSR_MVIEN] = { "mvien", aia_any, read_zero, write_ignore }, [CSR_MVIP] = { "mvip", aia_any, read_zero, write_ignore },@@ -2740,6 +2928,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPI] = { "stopi", aia_smode, read_stopi }, + /* Supervisor-Level IMSIC Interface (AIA) */ + [CSR_SSETEIPNUM] = { "sseteipnum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SCLREIPNUM] = { "sclreipnum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SSETEIENUM] = { "sseteienum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SCLREIENUM] = { "sclreienum", aia_smode, NULL, NULL,
rmw_xsetclreinum},
+ [CSR_STOPEI] = { "stopei",aia_smode, NULL, NULL,rmw_xtopei
},target/riscv/csr.c +6
-0+ /* Supervisor-Level High-Half CSRs (AIA) */ [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph },@@ -2785,6 +2980,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi }, + /* VS-Level IMSIC Interface (H-extension with AIA) */ + [CSR_VSSETEIPNUM] = { "vsseteipnum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSCLREIPNUM] = { "vsclreipnum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSSETEIENUM] = { "vsseteienum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSCLREIENUM] = { "vsclreienum", aia_hmode, NULL, NULL,
rmw_xsetclreinum},
+ [CSR_VSTOPEI] = { "vstopei",aia_hmode, NULL, NULL,rmw_xtopei},
+
/* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */
[CSR_HIDELEGH] = { "hidelegh",aia_hmode32, NULL, NULL,rmw_hidelegh},
[CSR_HVIENH] = { "hvienh",aia_hmode32,read_zero,write_ignore},
--
2.25.1
Re: [PATCH v6 15/23] target/riscv: Implement AIA IMSIC interface CSRs
Quoted 51 lines · Frank Chang
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:53寫道:
Quoted 49 lines
From: Anup Patel <[email protected]>
The AIA specification defines IMSIC interface CSRs for easy access to the per-HART IMSIC registers without using indirect xiselect and xireg CSRs. This patch implements the AIA IMSIC interface CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- target/riscv/csr.c | 202 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+)target/riscv/csr.c +21
-0@@ -906,6 +906,16 @@ static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno)return CSR_VSISELECT ;case CSR_SIREG :return CSR_VSIREG ; +case CSR_SSETEIPNUM : +return CSR_VSSETEIPNUM ; +case CSR_SCLREIPNUM : +return CSR_VSCLREIPNUM ; +case CSR_SSETEIENUM : +return CSR_VSSETEIENUM ; +case CSR_SCLREIENUM : +return CSR_VSCLREIENUM ; +case CSR_STOPEI : +return CSR_VSTOPEI ;default :return csrno; };@@ -1058,6 +1068,177 @@ done:return RISCV_EXCP_NONE ; } +static int rmw_xsetclreinum (CPURISCVState *env,int csrno,target_ulong *val, +target_ulong new_val,target_ulong wr_mask) +{ +int ret = -EINVAL ; +bool set, pend, virt; +target_ulong priv, isel, vgein, xlen, nval, wmask; + +/* Translate CSR number for VS-mode */ + csrno =aia_xlate_vs_csrno (env, csrno); + +/* Decode register details from CSR number */
Quoted 8 lines
target/riscv/csr.c done: · line 1086
Quoted 5 lines
target/riscv/csr.c +5
-0+ virt = set = pend = false; +switch (csrno) { +case CSR_MSETEIPNUM : + priv =PRV_M ; + set = true;Missing: pend = true; here?
Ahh, good catch. I will fix this in the next revision.
Thanks,
Anup
Quoted 208 lines
Frank Chang
Quoted 205 lines
target/riscv/csr.c +176
-1+break ; +case CSR_MCLREIPNUM : + priv =PRV_M ; + pend = true; +break ; +case CSR_MSETEIENUM : + priv =PRV_M ; + set = true; +break ; +case CSR_MCLREIENUM : + priv =PRV_M ; +break ; +case CSR_SSETEIPNUM : + priv =PRV_S ; + set = true; + pend = true; +break ; +case CSR_SCLREIPNUM : + priv =PRV_S ; + pend = true; +break ; +case CSR_SSETEIENUM : + priv =PRV_S ; + set = true; +break ; +case CSR_SCLREIENUM : + priv =PRV_S ; +break ; +case CSR_VSSETEIPNUM : + priv =PRV_S ; + virt = true; + set = true; + pend = true; +break ; +case CSR_VSCLREIPNUM : + priv =PRV_S ; + virt = true; + pend = true; +break ; +case CSR_VSSETEIENUM : + priv =PRV_S ; + virt = true; + set = true; +break ; +case CSR_VSCLREIENUM : + priv =PRV_S ; + virt = true; +break ; +default : + goto done; + }; + +/* IMSIC CSRs only available when machine implements IMSIC. */ +if (!env->aia_ireg_rmw_fn[priv]) { + goto done; + } + +/* Find the selected guest interrupt file */ + vgein = (virt) ?get_field (env->hstatus,HSTATUS_VGEIN ) :0 ; + +/* Selected guest interrupt file should be valid */ +if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } + +/* Set/Clear CSRs always read zero */ +if (val) { + *val =0 ; + } + +if (wr_mask) { +/* Get interrupt number */ + new_val &= wr_mask; + +/* Find target interrupt pending/enable register */ + xlen =riscv_cpu_mxl_bits (env); + isel = (new_val / xlen); + isel *= (xlen / IMSIC_EIPx_BITS); + isel += (pend) ?ISELECT_IMSIC_EIP0 :ISELECT_IMSIC_EIE0 ; + +/* Find the interrupt bit to be set/clear */ + wmask = ((target_ulong )1 ) << (new_val % xlen); + nval = (set) ? wmask :0 ; + +/* Call machine specific IMSIC register emulation */ + ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], +AIA_MAKE_IREG (isel, priv, virt, + vgein, xlen), +NULL , nval, wmask); + }else { + ret =0 ; + } + +done: +if (ret) { +return (riscv_cpu_virt_enabled (env) && virt) ? +RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; + } +return RISCV_EXCP_NONE ; +} + +static int rmw_xtopei (CPURISCVState *env,int csrno,target_ulong *val, +target_ulong new_val,target_ulong wr_mask) +{ +bool virt; +int ret = -EINVAL ; +target_ulong priv, vgein; + +/* Translate CSR number for VS-mode */ + csrno =aia_xlate_vs_csrno (env, csrno); + +/* Decode register details from CSR number */ + virt = false; +switch (csrno) { +case CSR_MTOPEI : + priv =PRV_M ; +break ; +case CSR_STOPEI : + priv =PRV_S ; +break ; +case CSR_VSTOPEI : + priv =PRV_S ; + virt = true; +break ; +default : + goto done; + }; + +/* IMSIC CSRs only available when machine implements IMSIC. */ +if (!env->aia_ireg_rmw_fn[priv]) { + goto done; + } + +/* Find the selected guest interrupt file */ + vgein = (virt) ?get_field (env->hstatus,HSTATUS_VGEIN ) :0 ; + +/* Selected guest interrupt file should be valid */ +if (virt && (!vgein || env->geilen < vgein)) { + goto done; + } + +/* Call machine specific IMSIC register emulation for TOPEI */ + ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], +AIA_MAKE_IREG (ISELECT_IMSIC_TOPEI , priv, virt, vgein, +riscv_cpu_mxl_bits (env)), + val, new_val, wr_mask); + +done: +if (ret) { +return (riscv_cpu_virt_enabled (env) && virt) ? +RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; + } +return RISCV_EXCP_NONE ; +} +static RISCVException read_mtvec (CPURISCVState *env,int csrno,target_ulong *val) {@@ -2706,6 +2887,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Machine-Level Interrupts (AIA) */ [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi }, + /* Machine-Level IMSIC Interface (AIA) */ + [CSR_MSETEIPNUM] = { "mseteipnum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MCLREIPNUM] = { "mclreipnum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MSETEIENUM] = { "mseteienum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MCLREIENUM] = { "mclreienum", aia_any, NULL, NULL, rmw_xsetclreinum }, + [CSR_MTOPEI] = { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, + /* Virtual Interrupts for Supervisor Level (AIA) */ [CSR_MVIEN] = { "mvien", aia_any, read_zero, write_ignore }, [CSR_MVIP] = { "mvip", aia_any, read_zero, write_ignore },@@ -2740,6 +2928,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* Supervisor-Level Interrupts (AIA) */ [CSR_STOPI] = { "stopi", aia_smode, read_stopi }, + /* Supervisor-Level IMSIC Interface (AIA) */ + [CSR_SSETEIPNUM] = { "sseteipnum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SCLREIPNUM] = { "sclreipnum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SSETEIENUM] = { "sseteienum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_SCLREIENUM] = { "sclreienum", aia_smode, NULL, NULL, rmw_xsetclreinum }, + [CSR_STOPEI] = { "stopei", aia_smode, NULL, NULL, rmw_xtopei }, + /* Supervisor-Level High-Half CSRs (AIA) */ [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph },@@ -2785,6 +2980,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { /* VS-Level Interrupts (H-extension with AIA) */ [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi }, + /* VS-Level IMSIC Interface (H-extension with AIA) */ + [CSR_VSSETEIPNUM] = { "vsseteipnum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSCLREIPNUM] = { "vsclreipnum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSSETEIENUM] = { "vsseteienum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSCLREIENUM] = { "vsclreienum", aia_hmode, NULL, NULL, rmw_xsetclreinum }, + [CSR_VSTOPEI] = { "vstopei", aia_hmode, NULL, NULL, rmw_xtopei }, + /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL, rmw_hidelegh }, [CSR_HVIENH] = { "hvienh", aia_hmode32, read_zero, write_ignore }, --2.25.1
PATCH v6 16/23 hw/riscv: virt: Use AIA INTC compatible string when available
In reply to Anup PatelFrom: Anup Patel <[email protected]>
We should use the AIA INTC compatible string in the CPU INTC DT nodes when the CPUs support AIA feature. This will allow Linux INTC driver to use AIA local interrupt CSRs.
--- hw/riscv/virt.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
hw/riscv/virt.c +11 -2
@@ -211,8 +211,17 @@ static void create_fdt_socket_cpus(RISCVVirtState *s, int socket,qemu_fdt_add_subnode (mc->fdt, intc_name);qemu_fdt_setprop_cell (mc->fdt, intc_name,"phandle" , intc_phandles[cpu]); -qemu_fdt_setprop_string (mc->fdt, intc_name,"compatible" , -"riscv,cpu-intc" ); +if (riscv_feature (&s->soc[socket].harts[cpu].env, +RISCV_FEATURE_AIA )) { +static const char *const compat[2 ] = { +"riscv,cpu-intc-aia" ,"riscv,cpu-intc" + }; +qemu_fdt_setprop_string_array (mc->fdt, intc_name,"compatible" , + (char **)&compat,ARRAY_SIZE (compat)); + }else { +qemu_fdt_setprop_string (mc->fdt, intc_name,"compatible" , +"riscv,cpu-intc" ); + }qemu_fdt_setprop (mc->fdt, intc_name,"interrupt-controller" ,NULL ,0 );qemu_fdt_setprop_cell (mc->fdt, intc_name,"#interrupt-cells" ,1 );
Signature · 1 line
2.25.1
Re: [PATCH v6 16/23] hw/riscv: virt: Use AIA INTC compatible string when available
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:59寫道:
Quoted 41 lines
From: Anup Patel <[email protected]>
We should use the AIA INTC compatible string in the CPU INTC DT nodes when the CPUs support AIA feature. This will allow Linux INTC driver to use AIA local interrupt CSRs.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- hw/riscv/virt.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)hw/riscv/virt.c +11
-2@@ -211,8 +211,17 @@ static void create_fdt_socket_cpus(RISCVVirtState *s,int socket, qemu_fdt_add_subnode(mc->fdt, intc_name);qemu_fdt_setprop_cell (mc->fdt, intc_name,"phandle" , intc_phandles[cpu]); -qemu_fdt_setprop_string (mc->fdt, intc_name,"compatible" , -"riscv,cpu-intc" ); +if (riscv_feature (&s->soc[socket].harts[cpu].env, +RISCV_FEATURE_AIA )) { +static const char *const compat[2 ] = { +"riscv,cpu-intc-aia" ,"riscv,cpu-intc" + }; +qemu_fdt_setprop_string_array (mc->fdt, intc_name,"compatible" , + (char **)&compat,ARRAY_SIZE (compat)); + }else { +qemu_fdt_setprop_string (mc->fdt, intc_name,"compatible" , +"riscv,cpu-intc" ); + }qemu_fdt_setprop(mc->fdt, intc_name, "interrupt-controller", NULL, 0); qemu_fdt_setprop_cell(mc->fdt, intc_name, "#interrupt-cells", 1);
Quoted 5 lines
--
2.25.1
PATCH v6 17/23 target/riscv: Allow users to force enable AIA CSRs in HART
In reply to Anup PatelFrom: Anup Patel <[email protected]>
We add "x-aia" command-line option for RISC-V HART using which allows users to force enable CPU AIA CSRs without changing the interrupt controller available in RISC-V machine.
--- target/riscv/cpu.c | 5 +++++ target/riscv/cpu.h | 1 + 2 files changed, 6 insertions(+)
target/riscv/cpu.c +5 -0
@@ -463,6 +463,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) } } +if (cpu->cfg.aia) { +riscv_set_feature (env,RISCV_FEATURE_AIA ); + } +set_resetvec (env, cpu->cfg.resetvec);/* Validate that MISA_MXL is set properly. */
@@ -691,6 +695,7 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("x-j" ,RISCVCPU , cfg.ext_j, false),/* ePMP 0.9.3 */ DEFINE_PROP_BOOL ("x-epmp" ,RISCVCPU , cfg.epmp, false), +DEFINE_PROP_BOOL ("x-aia" ,RISCVCPU , cfg.aia, false),DEFINE_PROP_UINT64 ("resetvec" ,RISCVCPU , cfg.resetvec,DEFAULT_RSTVEC ), DEFINE_PROP_END_OF_LIST(),
target/riscv/cpu.h +1 -0
@@ -362,6 +362,7 @@ struct RISCVCPU {bool mmu;bool pmp;bool epmp; +bool aia;uint64_t resetvec; } cfg; };
Signature · 1 line
2.25.1
Re: [PATCH v6 17/23] target/riscv: Allow users to force enable AIA CSRs in HART
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:57寫道:
Quoted 52 lines
From: Anup Patel <[email protected]>
We add "x-aia" command-line option for RISC-V HART using which allows users to force enable CPU AIA CSRs without changing the interrupt controller available in RISC-V machine.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 5 +++++ target/riscv/cpu.h | 1 + 2 files changed, 6 insertions(+)target/riscv/cpu.c +4
-0@@ -463,6 +463,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) } } +if (cpu->cfg.aia) { +riscv_set_feature (env,RISCV_FEATURE_AIA ); + } +set_resetvec (env, cpu->cfg.resetvec);/* Validate that MISA_MXL is set properly. */target/riscv/cpu.c +1
-0@@ -691,6 +695,7 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("x-j" ,RISCVCPU , cfg.ext_j, false),/* ePMP 0.9.3 */ DEFINE_PROP_BOOL ("x-epmp" ,RISCVCPU , cfg.epmp, false), +DEFINE_PROP_BOOL ("x-aia" ,RISCVCPU , cfg.aia, false),DEFINE_PROP_UINT64 ("resetvec" ,RISCVCPU , cfg.resetvec, DEFAULT_RSTVEC),DEFINE_PROP_END_OF_LIST(),target/riscv/cpu.h +1
-1@@ -362,6 +362,7 @@ struct RISCVCPU {bool mmu;bool pmp;bool epmp; +bool aia;uint64_t resetvec; } cfg; }; --
Quoted 4 lines
2.25.1
PATCH v6 18/23 hw/intc: Add RISC-V AIA APLIC device emulation
In reply to Anup PatelFrom: Anup Patel <[email protected]>
The RISC-V AIA (Advanced Interrupt Architecture) defines a new interrupt controller for wired interrupts called APLIC (Advanced Platform Level Interrupt Controller). The APLIC is capabable of forwarding wired interupts to RISC-V HARTs directly or as MSIs (Message Signaled Interupts).
This patch adds device emulation for RISC-V AIA APLIC.
--- hw/intc/Kconfig | 3 + hw/intc/meson.build | 1 + hw/intc/riscv_aplic.c | 970 ++++++++++++++++++++++++++++++++++ include/hw/intc/riscv_aplic.h | 79 +++ 4 files changed, 1053 insertions(+) create mode 100644 hw/intc/riscv_aplic.c create mode 100644 include/hw/intc/riscv_aplic.h
hw/intc/Kconfig +3 -0
@@ -70,6 +70,9 @@ config LOONGSON_LIOINTC config RISCV_ACLINT bool +config RISCV_APLIC + bool + config SIFIVE_PLIC bool
hw/intc/meson.build +1 -0
@@ -50,6 +50,7 @@ specific_ss.add(when: 'CONFIG_S390_FLIC', if_true: files('s390_flic.c')) specific_ss.add(when: 'CONFIG_S390_FLIC_KVM', if_true: files('s390_flic_kvm.c')) specific_ss.add(when: 'CONFIG_SH_INTC', if_true: files('sh_intc.c')) specific_ss.add(when: 'CONFIG_RISCV_ACLINT', if_true: files('riscv_aclint.c')) +specific_ss.add(when: 'CONFIG_RISCV_APLIC', if_true: files('riscv_aplic.c')) specific_ss.add(when: 'CONFIG_SIFIVE_PLIC', if_true: files('sifive_plic.c')) specific_ss.add(when: 'CONFIG_XICS', if_true: files('xics.c')) specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_XICS'],
hw/intc/riscv_aplic.c new file +970 -0
970 lines not on this page · raw message
include/hw/intc/riscv_aplic.h new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v6 18/23] hw/intc: Add RISC-V AIA APLIC device emulation
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:55寫道:
Quoted 263 lines
From: Anup Patel <[email protected]>
The RISC-V AIA (Advanced Interrupt Architecture) defines a new interrupt controller for wired interrupts called APLIC (Advanced Platform Level Interrupt Controller). The APLIC is capabable of forwarding wired interupts to RISC-V HARTs directly or as MSIs (Message Signaled Interupts).
This patch adds device emulation for RISC-V AIA APLIC.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- hw/intc/Kconfig | 3 + hw/intc/meson.build | 1 + hw/intc/riscv_aplic.c | 970 ++++++++++++++++++++++++++++++++++ include/hw/intc/riscv_aplic.h | 79 +++ 4 files changed, 1053 insertions(+) create mode 100644 hw/intc/riscv_aplic.c create mode 100644 include/hw/intc/riscv_aplic.hhw/intc/Kconfig +3
-0@@ -70,6 +70,9 @@ config LOONGSON_LIOINTC config RISCV_ACLINT bool +config RISCV_APLIC + bool + config SIFIVE_PLIC boolhw/intc/meson.build +1
-0@@ -50,6 +50,7 @@ specific_ss.add(when: 'CONFIG_S390_FLIC', if_true: files('s390_flic.c')) specific_ss.add(when: 'CONFIG_S390_FLIC_KVM', if_true: files('s390_flic_kvm.c')) specific_ss.add(when: 'CONFIG_SH_INTC', if_true: files('sh_intc.c')) specific_ss.add(when: 'CONFIG_RISCV_ACLINT', if_true: files('riscv_aclint.c')) +specific_ss.add(when: 'CONFIG_RISCV_APLIC', if_true:
files('riscv_aplic.c'))
specific_ss.add(when: 'CONFIG_SIFIVE_PLIC',if_true:files('sifive_plic.c'))
specific_ss.add(when: 'CONFIG_XICS', if_true: files('xics.c'))specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_XICS'],hw/intc/riscv_aplic.c new file +12
-0@@ -0,0 +1,970 @@ +/* +* RISC-V APLIC (Advanced Platform Level Interrupt Controller) +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */hw/intc/riscv_aplic.c new file +185
-0+ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "qemu/module.h" +#include "qemu/error-report.h" +#include "qemu/bswap.h" +#include "exec/address-spaces.h" +#include "hw/sysbus.h" +#include "hw/pci/msi.h" +#include "hw/boards.h" +#include "hw/qdev-properties.h" +#include "hw/intc/riscv_aplic.h" +#include "hw/irq.h" +#include "target/riscv/cpu.h" +#include "sysemu/sysemu.h" +#include "migration/vmstate.h" + +#define APLIC_MAX_IDC (1UL << 14) +#define APLIC_MAX_SOURCE 1024 +#define APLIC_MIN_IPRIO_BITS 1 +#define APLIC_MAX_IPRIO_BITS 8 +#define APLIC_MAX_CHILDREN 1024 + +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_DOMAINCFG_IE (1 << 8) +#define APLIC_DOMAINCFG_DM (1 << 2) +#define APLIC_DOMAINCFG_BE (1 << 0) + +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_D (1 << 10) +#define APLIC_SOURCECFG_CHILDIDX_MASK 0x000003ff +#define APLIC_SOURCECFG_SM_MASK 0x00000007 +#define APLIC_SOURCECFG_SM_INACTIVE 0x0 +#define APLIC_SOURCECFG_SM_DETACH 0x1 +#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4 +#define APLIC_SOURCECFG_SM_EDGE_FALL 0x5 +#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6 +#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 + +#define APLIC_MMSICFGADDR 0x1bc0 +#define APLIC_MMSICFGADDRH 0x1bc4 +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#define APLIC_xMSICFGADDRH_L (1UL << 31) +#define APLIC_xMSICFGADDRH_HHXS_MASK 0x1f +#define APLIC_xMSICFGADDRH_HHXS_SHIFT 24 +#define APLIC_xMSICFGADDRH_LHXS_MASK 0x7 +#define APLIC_xMSICFGADDRH_LHXS_SHIFT 20 +#define APLIC_xMSICFGADDRH_HHXW_MASK 0x7 +#define APLIC_xMSICFGADDRH_HHXW_SHIFT 16 +#define APLIC_xMSICFGADDRH_LHXW_MASK 0xf +#define APLIC_xMSICFGADDRH_LHXW_SHIFT 12 +#define APLIC_xMSICFGADDRH_BAPPN_MASK 0xfff + +#define APLIC_xMSICFGADDR_PPN_SHIFT 12 + +#define APLIC_xMSICFGADDR_PPN_HART (__lhxs) \ + ((1UL << (__lhxs)) - 1) + +#define APLIC_xMSICFGADDR_PPN_LHX_MASK (__lhxw) \ + ((1UL << (__lhxw)) - 1) +#define APLIC_xMSICFGADDR_PPN_LHX_SHIFT (__lhxs) \ + ((__lhxs)) +#define APLIC_xMSICFGADDR_PPN_LHX (__lhxw, __lhxs) \ + (APLIC_xMSICFGADDR_PPN_LHX_MASK(__lhxw) << \ + APLIC_xMSICFGADDR_PPN_LHX_SHIFT(__lhxs)) + +#define APLIC_xMSICFGADDR_PPN_HHX_MASK (__hhxw) \ + ((1UL << (__hhxw)) - 1) +#define APLIC_xMSICFGADDR_PPN_HHX_SHIFT (__hhxs) \ + ((__hhxs) + APLIC_xMSICFGADDR_PPN_SHIFT) +#define APLIC_xMSICFGADDR_PPN_HHX (__hhxw, __hhxs) \ + (APLIC_xMSICFGADDR_PPN_HHX_MASK(__hhxw) << \ + APLIC_xMSICFGADDR_PPN_HHX_SHIFT(__hhxs)) + +#define APLIC_xMSICFGADDRH_VALID_MASK \ + (APLIC_xMSICFGADDRH_L | \ + (APLIC_xMSICFGADDRH_HHXS_MASK << APLIC_xMSICFGADDRH_HHXS_SHIFT) | \ + (APLIC_xMSICFGADDRH_LHXS_MASK << APLIC_xMSICFGADDRH_LHXS_SHIFT) | \ + (APLIC_xMSICFGADDRH_HHXW_MASK << APLIC_xMSICFGADDRH_HHXW_SHIFT) | \ + (APLIC_xMSICFGADDRH_LHXW_MASK << APLIC_xMSICFGADDRH_LHXW_SHIFT) | \ + APLIC_xMSICFGADDRH_BAPPN_MASK) + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIENUM 0x1fdc + +#define APLIC_SETIPNUM_LE 0x2000 +#define APLIC_SETIPNUM_BE 0x2004 + +#define APLIC_ISTATE_PENDING (1U << 0) +#define APLIC_ISTATE_ENABLED (1U << 1) +#define APLIC_ISTATE_ENPEND (APLIC_ISTATE_ENABLED | \ + APLIC_ISTATE_PENDING) +#define APLIC_ISTATE_INPUT (1U << 8) + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_HART_IDX_SHIFT 18 +#define APLIC_TARGET_HART_IDX_MASK 0x3fff +#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_GUEST_IDX_MASK 0x3f +#define APLIC_TARGET_IPRIO_MASK 0xff +#define APLIC_TARGET_EIID_MASK 0x7ff + +#define APLIC_IDC_BASE 0x4000 +#define APLIC_IDC_SIZE 32 + +#define APLIC_IDC_IDELIVERY 0x00 + +#define APLIC_IDC_IFORCE 0x04 + +#define APLIC_IDC_ITHRESHOLD 0x08 + +#define APLIC_IDC_TOPI 0x18 +#define APLIC_IDC_TOPI_ID_SHIFT 16 +#define APLIC_IDC_TOPI_ID_MASK 0x3ff +#define APLIC_IDC_TOPI_PRIO_MASK 0xff + +#define APLIC_IDC_CLAIMI 0x1c + +static uint32_t riscv_aplic_read_input_word (RISCVAPLICState *aplic, +uint32_t word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_INPUT ) ?1 :0 ) << i; + } + +return ret; +} + +static uint32_t riscv_aplic_read_pending_word (RISCVAPLICState *aplic, +uint32_t word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_PENDING ) ?1 :0 ) << i; + } + +return ret; +} + +static void riscv_aplic_set_pending_raw (RISCVAPLICState *aplic, +uint32_t irq,bool pending) +{ +if (pending) { + aplic->state[irq] |=APLIC_ISTATE_PENDING ; + }else { + aplic->state[irq] &= ~APLIC_ISTATE_PENDING ; + } +} + +static void riscv_aplic_set_pending (RISCVAPLICState *aplic, +uint32_t irq,bool pending) +{ +uint32_t sourcecfg, sm; + +if ((irq <=0 ) || (aplic->num_irqs <= irq)) { +return ; + } +
hw/intc/riscv_aplic.c line 212
Quoted 10 lines
hw/intc/riscv_aplic.c new file +10
-0+ sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { +return ; + } + + sm = sourcecfg &APLIC_SOURCECFG_SM_MASK ; +if ((sm ==APLIC_SOURCECFG_SM_INACTIVE ) || + (!aplic->msimode && ((sm ==APLIC_SOURCECFG_SM_LEVEL_HIGH ) || + (sm ==APLIC_SOURCECFG_SM_LEVEL_LOW )))) { +return ;
Hi Anup,
According to riscv-interrupts-028 spec:
If the source mode is Level1 or Level0 and the interrupt domain is configured
in direct delivery mode (domaincfg.DM = 0):
* The pending bit is not cleared by a claim of the interrupt at the PLIC, nor can it be cleared by a write to an in clrip register or to
clripnum.
Also, if the source mode is Level1 or Level0 and the interrupt domain is configured
in MSI delivery mode (domaincfg.DM = 1):
* The pending bit cannot be cleared by a write to an in clrip register or
to clripnum.
It seems that when pending is false, riscv_aplic_set_pending_raw() should not be called
for both direct delivery mode and MIS delivery mode.
Regards,
Frank Chang
Quoted 864 lines
hw/intc/riscv_aplic.c new file +131
-0+ } + +riscv_aplic_set_pending_raw (aplic, irq, pending); +} + +static void riscv_aplic_set_pending_word (RISCVAPLICState *aplic, +uint32_t word,uint32_t value, +bool pending) +{ +uint32_t i, irq; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + +if (value & (1U << i)) { +riscv_aplic_set_pending (aplic, irq, pending); + } + } +} + +static uint32_t riscv_aplic_read_enabled_word (RISCVAPLICState *aplic, +int word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_ENABLED ) ?1 :0 ) << i; + } + +return ret; +} + +static void riscv_aplic_set_enabled_raw (RISCVAPLICState *aplic, +uint32_t irq,bool enabled) +{ +if (enabled) { + aplic->state[irq] |=APLIC_ISTATE_ENABLED ; + }else { + aplic->state[irq] &= ~APLIC_ISTATE_ENABLED ; + } +} + +static void riscv_aplic_set_enabled (RISCVAPLICState *aplic, +uint32_t irq,bool enabled) +{ +uint32_t sourcecfg, sm; + +if ((irq <=0 ) || (aplic->num_irqs <= irq)) { +return ; + } + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { +return ; + } + + sm = sourcecfg &APLIC_SOURCECFG_SM_MASK ; +if (sm ==APLIC_SOURCECFG_SM_INACTIVE ) { +return ; + } + +riscv_aplic_set_enabled_raw (aplic, irq, enabled); +} + +static void riscv_aplic_set_enabled_word (RISCVAPLICState *aplic, +uint32_t word,uint32_t value, +bool enabled) +{ +uint32_t i, irq; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + +if (value & (1U << i)) { +riscv_aplic_set_enabled (aplic, irq, enabled); + } + } +} + +static void riscv_aplic_msi_send (RISCVAPLICState *aplic, +uint32_t hart_idx,uint32_t guest_idx, +uint32_t eiid) +{ +uint64_t addr; +MemTxResult result; +RISCVAPLICState *aplic_m; +uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH; + + aplic_m = aplic; +while (aplic_m && !aplic_m->mmode) { + aplic_m = aplic_m->parent; + } +if (!aplic_m) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: m-level APLIC not found\n" , + __func__); +return ; + } + +if (aplic->mmode) { + msicfgaddr = aplic_m->mmsicfgaddr; + msicfgaddrH = aplic_m->mmsicfgaddrH; + }else { + msicfgaddr = aplic_m->smsicfgaddr; + msicfgaddrH = aplic_m->smsicfgaddrH; + } + + lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) & + APLIC_xMSICFGADDRH_LHXS_MASK; + lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) & + APLIC_xMSICFGADDRH_LHXW_MASK; + hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) & + APLIC_xMSICFGADDRH_HHXS_MASK; + hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) & + APLIC_xMSICFGADDRH_HHXW_MASK; + + group_idx = hart_idx >> lhxw; + hart_idx &=APLIC_xMSICFGADDR_PPN_LHX_MASK (lhxw); + + addr = msicfgaddr; + addr |= ((uint64_t)(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK)) <<32;
+ addr |= ((uint64_t)(group_idx &APLIC_xMSICFGADDR_PPN_HHX_MASK(hhxw))) <<hw/intc/riscv_aplic.c new file +2
-0+APLIC_xMSICFGADDR_PPN_HHX_SHIFT (hhxs); + addr |= ((uint64_t )(hart_idx &APLIC_xMSICFGADDR_PPN_LHX_MASK (lhxw)))<<
hw/intc/riscv_aplic.c new file +14
-0+APLIC_xMSICFGADDR_PPN_LHX_SHIFT (lhxs); + addr |= (uint64_t )(guest_idx &APLIC_xMSICFGADDR_PPN_HART (lhxs)); + addr <<= APLIC_xMSICFGADDR_PPN_SHIFT; + +address_space_stl_le (&address_space_memory, addr, + eiid,MEMTXATTRS_UNSPECIFIED , &result); +if (result !=MEMTX_OK ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: MSI write failed for " +"hart_index=%d guest_index=%d eiid=%d\n" , + __func__, hart_idx, guest_idx, eiid); + } +} + +static void riscv_aplic_msi_irq_update(RISCVAPLICState *aplic, uint32_tirq)
hw/intc/riscv_aplic.c new file +9
-0+{ +uint32_t hart_idx, guest_idx, eiid; + +if (!aplic->msimode || (aplic->num_irqs <= irq) || + !(aplic->domaincfg &APLIC_DOMAINCFG_IE )) { +return ; + } + +if ((aplic->state[irq] &APLIC_ISTATE_ENPEND ) !=APLIC_ISTATE_ENPEND ){
hw/intc/riscv_aplic.c new file +82
-0+return ; + } + +riscv_aplic_set_pending_raw (aplic, irq, false); + + hart_idx = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + hart_idx &=APLIC_TARGET_HART_IDX_MASK ; +if (aplic->mmode) { +/* M-level APLIC ignores guest_index */ + guest_idx =0 ; + }else { + guest_idx = aplic->target[irq] >>APLIC_TARGET_GUEST_IDX_SHIFT ; + guest_idx &=APLIC_TARGET_GUEST_IDX_MASK ; + } + eiid = aplic->target[irq] &APLIC_TARGET_EIID_MASK ; +riscv_aplic_msi_send (aplic, hart_idx, guest_idx, eiid); +} + +static uint32_t riscv_aplic_idc_topi (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t best_irq, best_iprio; +uint32_t irq, iprio, ihartidx, ithres; + +if (aplic->num_harts <= idc) { +return 0 ; + } + + ithres = aplic->ithreshold[idc]; +if (!ithres) { +return 0 ; + } + + best_irq = best_iprio =UINT32_MAX ; +for (irq =1 ; irq < aplic->num_irqs; irq++) { +if ((aplic->state[irq] &APLIC_ISTATE_ENPEND ) != +APLIC_ISTATE_ENPEND ) { +continue ; + } + + ihartidx = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + ihartidx &=APLIC_TARGET_HART_IDX_MASK ; +if (ihartidx != idc) { +continue ; + } + + iprio = aplic->target[irq] & aplic->iprio_mask; +if (iprio >= ithres) { +continue ; + } + +if (iprio < best_iprio) { + best_irq = irq; + best_iprio = iprio; + } + } + +if (best_irq < aplic->num_irqs && best_iprio <= aplic->iprio_mask) { +return (best_irq <<APLIC_IDC_TOPI_ID_SHIFT ) | best_iprio; + } + +return 0 ; +} + +static void riscv_aplic_idc_update (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t topi; + +if (aplic->msimode || aplic->num_harts <= idc) { +return ; + } + + topi =riscv_aplic_idc_topi (aplic, idc); +if ((aplic->domaincfg &APLIC_DOMAINCFG_IE ) && + aplic->idelivery[idc] && + (aplic->iforce[idc] || topi)) { +qemu_irq_raise (aplic->external_irqs[idc]); + }else { +qemu_irq_lower (aplic->external_irqs[idc]); + } +} + +static uint32_t riscv_aplic_idc_claimi(RISCVAPLICState *aplic, uint32_tidc)
hw/intc/riscv_aplic.c new file +102
-0+{ +uint32_t irq, state, sm, topi =riscv_aplic_idc_topi (aplic, idc); + +if (!topi) { + aplic->iforce[idc] =0 ; +return 0 ; + } + + irq = (topi >>APLIC_IDC_TOPI_ID_SHIFT ) &APLIC_IDC_TOPI_ID_MASK ; + sm = aplic->sourcecfg[irq] &APLIC_SOURCECFG_SM_MASK ; + state = aplic->state[irq]; +riscv_aplic_set_pending_raw (aplic, irq, false); +if ((sm ==APLIC_SOURCECFG_SM_LEVEL_HIGH ) && + (state &APLIC_ISTATE_INPUT )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + }else if ((sm ==APLIC_SOURCECFG_SM_LEVEL_LOW ) && + !(state &APLIC_ISTATE_INPUT )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + } +riscv_aplic_idc_update (aplic, idc); + +return topi; +} + +static void riscv_aplic_request (void *opaque,int irq,int level) +{ +bool update = false; +RISCVAPLICState *aplic = opaque; +uint32_t sourcecfg, childidx, state, idc; + +assert ((0 < irq) && (irq < aplic->num_irqs)); + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { + childidx = sourcecfg &APLIC_SOURCECFG_CHILDIDX_MASK ; +if (childidx < aplic->num_children) { +riscv_aplic_request (aplic->children[childidx], irq, level); + } +return ; + } + + state = aplic->state[irq]; +switch (sourcecfg &APLIC_SOURCECFG_SM_MASK ) { +case APLIC_SOURCECFG_SM_EDGE_RISE : +if ((level >0 ) && !(state &APLIC_ISTATE_INPUT ) && + !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_EDGE_FALL : +if ((level <=0 ) && (state &APLIC_ISTATE_INPUT ) && + !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_LEVEL_HIGH : +if ((level >0 ) && !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_LEVEL_LOW : +if ((level <=0 ) && !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +default : +break ; + } + +if (level <=0 ) { + aplic->state[irq] &= ~APLIC_ISTATE_INPUT ; + }else { + aplic->state[irq] |=APLIC_ISTATE_INPUT ; + } + +if (update) { +if (aplic->msimode) { +riscv_aplic_msi_irq_update (aplic, irq); + }else { + idc = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + idc &=APLIC_TARGET_HART_IDX_MASK ; +riscv_aplic_idc_update (aplic, idc); + } + } +} + +static uint64_t riscv_aplic_read (void *opaque,hwaddr addr,unsigned size) +{ +uint32_t irq, word, idc; +RISCVAPLICState *aplic = opaque; + +/* Reads must be 4 byte words */ +if ((addr &0x3 ) !=0 ) { + goto err; + } + +if (addr ==APLIC_DOMAINCFG ) { + return aplic->domaincfg | (aplic->msimode ? APLIC_DOMAINCFG_DM :0);
hw/intc/riscv_aplic.c new file +59
-0+ } else if ((APLIC_SOURCECFG_BASE <= addr) && + (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_SOURCECFG_BASE ) >>2 ) +1 ; +return aplic->sourcecfg[irq]; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDR )) { +return aplic->mmsicfgaddr; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDRH )) { +return aplic->mmsicfgaddrH; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDR )) { +/* Registers SMSICFGADDR and SMSICFGADDRH are implemented only if: +* (a) the interrupt domain is at machine level +* (b) the domain’s harts implement supervisor mode +* (c) the domain has one or more child supervisor-level domains +* that support MSI delivery mode (domaincfg.DM is not read- +* only zero in at least one of the supervisor-level child +* domains). +*/ +return (aplic->num_children) ? aplic->smsicfgaddr :0 ; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDRH )) { +return (aplic->num_children) ? aplic->smsicfgaddrH :0 ; + }else if ((APLIC_SETIP_BASE <= addr) && + (addr < (APLIC_SETIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIP_BASE ) >>2 ; +return riscv_aplic_read_pending_word (aplic, word); + }else if (addr ==APLIC_SETIPNUM ) { +return 0 ; + }else if ((APLIC_CLRIP_BASE <= addr) && + (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_CLRIP_BASE ) >>2 ; +return riscv_aplic_read_input_word (aplic, word); + }else if (addr ==APLIC_CLRIPNUM ) { +return 0 ; + }else if ((APLIC_SETIE_BASE <= addr) && + (addr < (APLIC_SETIE_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIE_BASE ) >>2 ; +return riscv_aplic_read_enabled_word (aplic, word); + }else if (addr ==APLIC_SETIENUM ) { +return 0 ; + }else if ((APLIC_CLRIE_BASE <= addr) && + (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words *4 ))) { +return 0 ; + }else if (addr ==APLIC_CLRIENUM ) { +return 0 ; + }else if (addr ==APLIC_SETIPNUM_LE ) { +return 0 ; + }else if (addr ==APLIC_SETIPNUM_BE ) { +return 0 ; + }else if (addr ==APLIC_GENMSI ) { +return (aplic->msimode) ? aplic->genmsi :0 ; + }else if ((APLIC_TARGET_BASE <= addr) && + (addr < (APLIC_TARGET_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_TARGET_BASE ) >>2 ) +1 ; +return aplic->target[irq]; + } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) && + (addr < (APLIC_IDC_BASE + aplic->num_harts *
APLIC_IDC_SIZE))) {hw/intc/riscv_aplic.c new file +134
-0+ idc = (addr - APLIC_IDC_BASE) / APLIC_IDC_SIZE; + switch (addr - (APLIC_IDC_BASE + idc * APLIC_IDC_SIZE)) { + case APLIC_IDC_IDELIVERY: + return aplic->idelivery[idc]; + case APLIC_IDC_IFORCE: + return aplic->iforce[idc]; + case APLIC_IDC_ITHRESHOLD: + return aplic->ithreshold[idc]; + case APLIC_IDC_TOPI: + return riscv_aplic_idc_topi(aplic, idc); + case APLIC_IDC_CLAIMI: + return riscv_aplic_idc_claimi(aplic, idc); + default: + goto err; + }; + } + +err: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Invalid register read 0x%" HWADDR_PRIx "\n", + __func__, addr); + return 0; +} + +static void riscv_aplic_write(void *opaque, hwaddr addr, uint64_t value, + unsigned size) +{ + RISCVAPLICState *aplic = opaque; + uint32_t irq, word, idc = UINT32_MAX; + + /* Writes must be 4 byte words */ + if ((addr & 0x3) != 0) { + goto err; + } + + if (addr == APLIC_DOMAINCFG) { + /* Only IE bit writeable at the moment */ + value &= APLIC_DOMAINCFG_IE; + aplic->domaincfg = value; + } else if ((APLIC_SOURCECFG_BASE <= addr) && + (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs - 1) * 4))) { + irq = ((addr - APLIC_SOURCECFG_BASE) >> 2) + 1; + if (!aplic->num_children && (value & APLIC_SOURCECFG_D)) { + value = 0; + } + if (value & APLIC_SOURCECFG_D) { + value &= (APLIC_SOURCECFG_D | APLIC_SOURCECFG_CHILDIDX_MASK); + } else { + value &= (APLIC_SOURCECFG_D | APLIC_SOURCECFG_SM_MASK); + } + aplic->sourcecfg[irq] = value; + if ((aplic->sourcecfg[irq] & APLIC_SOURCECFG_D) || + (aplic->sourcecfg[irq] == 0)) { + riscv_aplic_set_pending_raw(aplic, irq, false); + riscv_aplic_set_enabled_raw(aplic, irq, false); + } + } else if (aplic->mmode && aplic->msimode && + (addr == APLIC_MMSICFGADDR)) { + if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->mmsicfgaddr = value; + } + } else if (aplic->mmode && aplic->msimode && + (addr == APLIC_MMSICFGADDRH)) { + if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->mmsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK; + } + } else if (aplic->mmode && aplic->msimode && + (addr == APLIC_SMSICFGADDR)) { + /* Registers SMSICFGADDR and SMSICFGADDRH are implemented only if: + * (a) the interrupt domain is at machine level + * (b) the domain’s harts implement supervisor mode + * (c) the domain has one or more child supervisor-level domains + * that support MSI delivery mode (domaincfg.DM is not read- + * only zero in at least one of the supervisor-level child + * domains). + */ + if (aplic->num_children && + !(aplic->smsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->smsicfgaddr = value; + } + } else if (aplic->mmode && aplic->msimode && + (addr == APLIC_SMSICFGADDRH)) { + if (aplic->num_children && + !(aplic->smsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->smsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK; + } + } else if ((APLIC_SETIP_BASE <= addr) && + (addr < (APLIC_SETIP_BASE + aplic->bitfield_words * 4))) { + word = (addr - APLIC_SETIP_BASE) >> 2; + riscv_aplic_set_pending_word(aplic, word, value, true); + } else if (addr == APLIC_SETIPNUM) { + riscv_aplic_set_pending(aplic, value, true); + } else if ((APLIC_CLRIP_BASE <= addr) && + (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words * 4))) { + word = (addr - APLIC_CLRIP_BASE) >> 2; + riscv_aplic_set_pending_word(aplic, word, value, false); + } else if (addr == APLIC_CLRIPNUM) { + riscv_aplic_set_pending(aplic, value, false); + } else if ((APLIC_SETIE_BASE <= addr) && + (addr < (APLIC_SETIE_BASE + aplic->bitfield_words * 4))) { + word = (addr - APLIC_SETIE_BASE) >> 2; + riscv_aplic_set_enabled_word(aplic, word, value, true); + } else if (addr == APLIC_SETIENUM) { + riscv_aplic_set_enabled(aplic, value, true); + } else if ((APLIC_CLRIE_BASE <= addr) && + (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words * 4))) { + word = (addr - APLIC_CLRIE_BASE) >> 2; + riscv_aplic_set_enabled_word(aplic, word, value, false); + } else if (addr == APLIC_CLRIENUM) { + riscv_aplic_set_enabled(aplic, value, false); + } else if (addr == APLIC_SETIPNUM_LE) { + riscv_aplic_set_pending(aplic, value, true); + } else if (addr == APLIC_SETIPNUM_BE) { + riscv_aplic_set_pending(aplic, bswap32(value), true); + } else if (addr == APLIC_GENMSI) { + if (aplic->msimode) { + aplic->genmsi = value & ~(APLIC_TARGET_GUEST_IDX_MASK << + APLIC_TARGET_GUEST_IDX_SHIFT); + riscv_aplic_msi_send(aplic, + value >> APLIC_TARGET_HART_IDX_SHIFT, + 0, + value & APLIC_TARGET_EIID_MASK); + } + } else if ((APLIC_TARGET_BASE <= addr) && + (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) { + irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1; + if (aplic->msimode) { + aplic->target[irq] = value; + } else { + aplic->target[irq] = (value & ~APLIC_TARGET_IPRIO_MASK) | + (value & aplic->iprio_mask); + } + } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) && + (addr < (APLIC_IDC_BASE + aplic->num_harts *
APLIC_IDC_SIZE))) {hw/intc/riscv_aplic.c new file +64
-0+ idc = (addr -APLIC_IDC_BASE ) /APLIC_IDC_SIZE ; +switch (addr - (APLIC_IDC_BASE + idc *APLIC_IDC_SIZE )) { +case APLIC_IDC_IDELIVERY : + aplic->idelivery[idc] = value &0x1 ; +break ; +case APLIC_IDC_IFORCE : + aplic->iforce[idc] = value &0x1 ; +break ; +case APLIC_IDC_ITHRESHOLD : + aplic->ithreshold[idc] = value & aplic->iprio_mask; +break ; +default : + goto err; + }; + }else { + goto err; + } + +if (aplic->msimode) { +for (irq =1 ; irq < aplic->num_irqs; irq++) { +riscv_aplic_msi_irq_update (aplic, irq); + } + }else { +if (idc ==UINT32_MAX ) { +for (idc =0 ; idc < aplic->num_harts; idc++) { +riscv_aplic_idc_update (aplic, idc); + } + }else { +riscv_aplic_idc_update (aplic, idc); + } + } + +return ; + +err: +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Invalid register write 0x%" HWADDR_PRIx"\n" , + __func__, addr); +} + +static const MemoryRegionOps riscv_aplic_ops = { + .read = riscv_aplic_read, + .write = riscv_aplic_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .valid = { + .min_access_size =4 , + .max_access_size =4 + } +}; + +static void riscv_aplic_realize (DeviceState *dev,Error **errp) +{ +uint32_t i; +RISCVAPLICState *aplic =RISCV_APLIC (dev); + + aplic->bitfield_words = (aplic->num_irqs +31 ) >>5 ; + aplic->sourcecfg =g_new0 (uint32_t, aplic->num_irqs); + aplic->state =g_new (uint32_t, aplic->num_irqs); + aplic->target =g_new0 (uint32_t, aplic->num_irqs); + aplic->idelivery =g_new0 (uint32_t, aplic->num_harts); + aplic->iforce =g_new0 (uint32_t, aplic->num_harts); + aplic->ithreshold =g_new0 (uint32_t, aplic->num_harts); + + memory_region_init_io(&aplic->mmio, OBJECT(dev), &riscv_aplic_ops,aplic,
hw/intc/riscv_aplic.c new file +13
-0+ TYPE_RISCV_APLIC, aplic->aperture_size); +sysbus_init_mmio (SYS_BUS_DEVICE (dev), &aplic->mmio); + +/* Only root APLICs have hardware IRQ lines. All non-root APLICs +* have IRQ lines delegated by their parent APLIC. +*/ +if (!aplic->parent) { +qdev_init_gpio_in (dev, riscv_aplic_request, aplic->num_irqs); + } + +/* Create output IRQ lines for non-MSI mode */ +if (!aplic->msimode) { + aplic->external_irqs = g_malloc(sizeof(qemu_irq) *
aplic->num_harts);hw/intc/riscv_aplic.c new file +5
-0+qdev_init_gpio_out (dev, aplic->external_irqs, aplic->num_harts); + +/* Claim the CPU interrupt to be triggered by this APLIC */ +for (i =0 ; i < aplic->num_harts; i++) { + RISCVCPU *cpu = RISCV_CPU(qemu_get_cpu(aplic->hartid_base +i));
hw/intc/riscv_aplic.c new file +14
-0+if (riscv_cpu_claim_interrupts (cpu, + (aplic->mmode) ?MIP_MEIP :MIP_SEIP ) <0 ) { +error_report ("%s already claimed" , + (aplic->mmode) ?"MEIP" :"SEIP" ); +exit (1 ); + } + } + } + + msi_nonbroken = true; +} + +static Property riscv_aplic_properties[] = { + DEFINE_PROP_UINT32("aperture-size", RISCVAPLICState, aperture_size,0),
hw/intc/riscv_aplic.c new file +123
-0+DEFINE_PROP_UINT32 ("hartid-base" , RISCVAPLICState, hartid_base,0 ), +DEFINE_PROP_UINT32 ("num-harts" , RISCVAPLICState, num_harts,0 ), +DEFINE_PROP_UINT32 ("iprio-mask" , RISCVAPLICState, iprio_mask,0 ), +DEFINE_PROP_UINT32 ("num-irqs" , RISCVAPLICState, num_irqs,0 ), +DEFINE_PROP_BOOL ("msimode" , RISCVAPLICState, msimode,0 ), +DEFINE_PROP_BOOL ("mmode" , RISCVAPLICState, mmode,0 ), + DEFINE_PROP_END_OF_LIST(), +}; + +static const VMStateDescription vmstate_riscv_aplic = { + .name ="riscv_aplic" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT32 (domaincfg, RISCVAPLICState), +VMSTATE_UINT32 (mmsicfgaddr, RISCVAPLICState), +VMSTATE_UINT32 (mmsicfgaddrH, RISCVAPLICState), +VMSTATE_UINT32 (smsicfgaddr, RISCVAPLICState), +VMSTATE_UINT32 (smsicfgaddrH, RISCVAPLICState), +VMSTATE_UINT32 (genmsi, RISCVAPLICState), +VMSTATE_VARRAY_UINT32 (sourcecfg, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (state, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (target, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (idelivery, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (iforce, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (ithreshold, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_END_OF_LIST () + } +}; + +static void riscv_aplic_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + +device_class_set_props (dc, riscv_aplic_properties); + dc->realize = riscv_aplic_realize; + dc->vmsd = &vmstate_riscv_aplic; +} + +static const TypeInfo riscv_aplic_info = { + .name =TYPE_RISCV_APLIC , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (RISCVAPLICState), + .class_init = riscv_aplic_class_init, +}; + +static void riscv_aplic_register_types (void ) +{ +type_register_static (&riscv_aplic_info); +} + +type_init (riscv_aplic_register_types) + +/* +* Add a APLIC device to another APLIC device as child for +* interrupt delegation. +*/ +void riscv_aplic_add_child(DeviceState *parent, DeviceState *child) +{ +RISCVAPLICState *caplic, *paplic; + +assert (parent && child); + caplic =RISCV_APLIC (child); + paplic =RISCV_APLIC (parent); + +assert (paplic->num_irqs == caplic->num_irqs); +assert (paplic->num_children <=QEMU_APLIC_MAX_CHILDREN ); + + caplic->parent = paplic; + paplic->children[paplic->num_children] = caplic; + paplic->num_children++; +} + +/* +* Create APLIC device. +*/ +DeviceState *riscv_aplic_create (hwaddr addr,hwaddr size, +uint32_t hartid_base,uint32_t num_harts,uint32_t num_sources, +uint32_t iprio_bits,bool msimode,bool mmode,DeviceState *parent) +{ +DeviceState *dev =qdev_new (TYPE_RISCV_APLIC ); +uint32_t i; + +assert (num_harts <APLIC_MAX_IDC ); +assert ((APLIC_IDC_BASE + (num_harts *APLIC_IDC_SIZE )) <= size); +assert (num_sources <APLIC_MAX_SOURCE ); +assert (APLIC_MIN_IPRIO_BITS <= iprio_bits); +assert (iprio_bits <=APLIC_MAX_IPRIO_BITS ); + +qdev_prop_set_uint32 (dev,"aperture-size" , size); +qdev_prop_set_uint32 (dev,"hartid-base" , hartid_base); +qdev_prop_set_uint32 (dev,"num-harts" , num_harts); +qdev_prop_set_uint32 (dev,"iprio-mask" , ((1U << iprio_bits) -1 )); +qdev_prop_set_uint32 (dev,"num-irqs" , num_sources +1 ); +qdev_prop_set_bit (dev,"msimode" , msimode); +qdev_prop_set_bit (dev,"mmode" , mmode); + +sysbus_realize_and_unref (SYS_BUS_DEVICE (dev), &error_fatal); +sysbus_mmio_map (SYS_BUS_DEVICE (dev),0 , addr); + +if (parent) { +riscv_aplic_add_child (parent, dev); + } + +if (!msimode) { +for (i =0 ; i < num_harts; i++) { +CPUState *cpu =qemu_get_cpu (hartid_base + i); + + qdev_connect_gpio_out_named(dev, NULL, i, + qdev_get_gpio_in(DEVICE(cpu), + (mmode) ? IRQ_M_EXT :
IRQ_S_EXT));hw/intc/riscv_aplic.c new file +5
-0+ } + } + +return dev; +}include/hw/intc/riscv_aplic.h new file +12
-0@@ -0,0 +1,79 @@ +/* +* RISC-V APLIC (Advanced Platform Level Interrupt Controller) interface +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */include/hw/intc/riscv_aplic.h new file +62
-1+ +#ifndef HW_RISCV_APLIC_H +#define HW_RISCV_APLIC_H + +#include "hw/sysbus.h" +#include "qom/object.h" + +#define TYPE_RISCV_APLIC "riscv.aplic" + +typedef struct RISCVAPLICState RISCVAPLICState ; +DECLARE_INSTANCE_CHECKER(RISCVAPLICState, RISCV_APLIC, TYPE_RISCV_APLIC) + +#define APLIC_MIN_SIZE 0x4000 +#define APLIC_SIZE_ALIGN (__x) (((__x) + (APLIC_MIN_SIZE - 1)) & \ + ~(APLIC_MIN_SIZE - 1)) +#define APLIC_SIZE (__num_harts) (APLIC_MIN_SIZE + \ + APLIC_SIZE_ALIGN(32 * (__num_harts))) + +struct RISCVAPLICState { +/*< private >*/ +SysBusDevice parent_obj; +qemu_irq *external_irqs; + +/*< public >*/ +MemoryRegion mmio; +uint32_t bitfield_words; +uint32_t domaincfg; +uint32_t mmsicfgaddr; +uint32_t mmsicfgaddrH; +uint32_t smsicfgaddr; +uint32_t smsicfgaddrH; +uint32_t genmsi; +uint32_t *sourcecfg; +uint32_t *state; +uint32_t *target; +uint32_t *idelivery; +uint32_t *iforce; +uint32_t *ithreshold; + +/* topology */ +#define QEMU_APLIC_MAX_CHILDREN 16 +struct RISCVAPLICState *parent; +struct RISCVAPLICState *children[QEMU_APLIC_MAX_CHILDREN ]; +uint16_t num_children; + +/* config */ +uint32_t aperture_size; +uint32_t hartid_base; +uint32_t num_harts; +uint32_t iprio_mask; +uint32_t num_irqs; +bool msimode; +bool mmode; +}; + +void riscv_aplic_add_child (DeviceState *parent,DeviceState *child); + +DeviceState *riscv_aplic_create (hwaddr addr,hwaddr size, +uint32_t hartid_base,uint32_t num_harts,uint32_t num_sources, +uint32_t iprio_bits,bool msimode,bool mmode,DeviceState *parent); + +#endif --2.25.1
Re: [PATCH v6 18/23] hw/intc: Add RISC-V AIA APLIC device emulation
Quoted 279 lines · Frank Chang
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:55寫道:
Quoted 256 lines
From: Anup Patel <[email protected]>
The RISC-V AIA (Advanced Interrupt Architecture) defines a new interrupt controller for wired interrupts called APLIC (Advanced Platform Level Interrupt Controller). The APLIC is capabable of forwarding wired interupts to RISC-V HARTs directly or as MSIs (Message Signaled Interupts).
This patch adds device emulation for RISC-V AIA APLIC.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- hw/intc/Kconfig | 3 + hw/intc/meson.build | 1 + hw/intc/riscv_aplic.c | 970 ++++++++++++++++++++++++++++++++++ include/hw/intc/riscv_aplic.h | 79 +++ 4 files changed, 1053 insertions(+) create mode 100644 hw/intc/riscv_aplic.c create mode 100644 include/hw/intc/riscv_aplic.hhw/intc/Kconfig +3
-0@@ -70,6 +70,9 @@ config LOONGSON_LIOINTC config RISCV_ACLINT bool +config RISCV_APLIC + bool + config SIFIVE_PLIC boolhw/intc/meson.build +1
-0@@ -50,6 +50,7 @@ specific_ss.add(when: 'CONFIG_S390_FLIC', if_true: files('s390_flic.c')) specific_ss.add(when: 'CONFIG_S390_FLIC_KVM', if_true: files('s390_flic_kvm.c')) specific_ss.add(when: 'CONFIG_SH_INTC', if_true: files('sh_intc.c')) specific_ss.add(when: 'CONFIG_RISCV_ACLINT', if_true: files('riscv_aclint.c')) +specific_ss.add(when: 'CONFIG_RISCV_APLIC', if_true: files('riscv_aplic.c')) specific_ss.add(when: 'CONFIG_SIFIVE_PLIC', if_true: files('sifive_plic.c')) specific_ss.add(when: 'CONFIG_XICS', if_true: files('xics.c')) specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_XICS'],hw/intc/riscv_aplic.c new file +202
-0@@ -0,0 +1,970 @@ +/* +* RISC-V APLIC (Advanced Platform Level Interrupt Controller) +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "qemu/module.h" +#include "qemu/error-report.h" +#include "qemu/bswap.h" +#include "exec/address-spaces.h" +#include "hw/sysbus.h" +#include "hw/pci/msi.h" +#include "hw/boards.h" +#include "hw/qdev-properties.h" +#include "hw/intc/riscv_aplic.h" +#include "hw/irq.h" +#include "target/riscv/cpu.h" +#include "sysemu/sysemu.h" +#include "migration/vmstate.h" + +#define APLIC_MAX_IDC (1UL << 14) +#define APLIC_MAX_SOURCE 1024 +#define APLIC_MIN_IPRIO_BITS 1 +#define APLIC_MAX_IPRIO_BITS 8 +#define APLIC_MAX_CHILDREN 1024 + +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_DOMAINCFG_IE (1 << 8) +#define APLIC_DOMAINCFG_DM (1 << 2) +#define APLIC_DOMAINCFG_BE (1 << 0) + +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_D (1 << 10) +#define APLIC_SOURCECFG_CHILDIDX_MASK 0x000003ff +#define APLIC_SOURCECFG_SM_MASK 0x00000007 +#define APLIC_SOURCECFG_SM_INACTIVE 0x0 +#define APLIC_SOURCECFG_SM_DETACH 0x1 +#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4 +#define APLIC_SOURCECFG_SM_EDGE_FALL 0x5 +#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6 +#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 + +#define APLIC_MMSICFGADDR 0x1bc0 +#define APLIC_MMSICFGADDRH 0x1bc4 +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#define APLIC_xMSICFGADDRH_L (1UL << 31) +#define APLIC_xMSICFGADDRH_HHXS_MASK 0x1f +#define APLIC_xMSICFGADDRH_HHXS_SHIFT 24 +#define APLIC_xMSICFGADDRH_LHXS_MASK 0x7 +#define APLIC_xMSICFGADDRH_LHXS_SHIFT 20 +#define APLIC_xMSICFGADDRH_HHXW_MASK 0x7 +#define APLIC_xMSICFGADDRH_HHXW_SHIFT 16 +#define APLIC_xMSICFGADDRH_LHXW_MASK 0xf +#define APLIC_xMSICFGADDRH_LHXW_SHIFT 12 +#define APLIC_xMSICFGADDRH_BAPPN_MASK 0xfff + +#define APLIC_xMSICFGADDR_PPN_SHIFT 12 + +#define APLIC_xMSICFGADDR_PPN_HART (__lhxs) \ + ((1UL << (__lhxs)) - 1) + +#define APLIC_xMSICFGADDR_PPN_LHX_MASK (__lhxw) \ + ((1UL << (__lhxw)) - 1) +#define APLIC_xMSICFGADDR_PPN_LHX_SHIFT (__lhxs) \ + ((__lhxs)) +#define APLIC_xMSICFGADDR_PPN_LHX (__lhxw, __lhxs) \ + (APLIC_xMSICFGADDR_PPN_LHX_MASK(__lhxw) << \ + APLIC_xMSICFGADDR_PPN_LHX_SHIFT(__lhxs)) + +#define APLIC_xMSICFGADDR_PPN_HHX_MASK (__hhxw) \ + ((1UL << (__hhxw)) - 1) +#define APLIC_xMSICFGADDR_PPN_HHX_SHIFT (__hhxs) \ + ((__hhxs) + APLIC_xMSICFGADDR_PPN_SHIFT) +#define APLIC_xMSICFGADDR_PPN_HHX (__hhxw, __hhxs) \ + (APLIC_xMSICFGADDR_PPN_HHX_MASK(__hhxw) << \ + APLIC_xMSICFGADDR_PPN_HHX_SHIFT(__hhxs)) + +#define APLIC_xMSICFGADDRH_VALID_MASK \ + (APLIC_xMSICFGADDRH_L | \ + (APLIC_xMSICFGADDRH_HHXS_MASK << APLIC_xMSICFGADDRH_HHXS_SHIFT) | \ + (APLIC_xMSICFGADDRH_LHXS_MASK << APLIC_xMSICFGADDRH_LHXS_SHIFT) | \ + (APLIC_xMSICFGADDRH_HHXW_MASK << APLIC_xMSICFGADDRH_HHXW_SHIFT) | \ + (APLIC_xMSICFGADDRH_LHXW_MASK << APLIC_xMSICFGADDRH_LHXW_SHIFT) | \ + APLIC_xMSICFGADDRH_BAPPN_MASK) + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIENUM 0x1fdc + +#define APLIC_SETIPNUM_LE 0x2000 +#define APLIC_SETIPNUM_BE 0x2004 + +#define APLIC_ISTATE_PENDING (1U << 0) +#define APLIC_ISTATE_ENABLED (1U << 1) +#define APLIC_ISTATE_ENPEND (APLIC_ISTATE_ENABLED | \ + APLIC_ISTATE_PENDING) +#define APLIC_ISTATE_INPUT (1U << 8) + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_HART_IDX_SHIFT 18 +#define APLIC_TARGET_HART_IDX_MASK 0x3fff +#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_GUEST_IDX_MASK 0x3f +#define APLIC_TARGET_IPRIO_MASK 0xff +#define APLIC_TARGET_EIID_MASK 0x7ff + +#define APLIC_IDC_BASE 0x4000 +#define APLIC_IDC_SIZE 32 + +#define APLIC_IDC_IDELIVERY 0x00 + +#define APLIC_IDC_IFORCE 0x04 + +#define APLIC_IDC_ITHRESHOLD 0x08 + +#define APLIC_IDC_TOPI 0x18 +#define APLIC_IDC_TOPI_ID_SHIFT 16 +#define APLIC_IDC_TOPI_ID_MASK 0x3ff +#define APLIC_IDC_TOPI_PRIO_MASK 0xff + +#define APLIC_IDC_CLAIMI 0x1c + +static uint32_t riscv_aplic_read_input_word (RISCVAPLICState *aplic, +uint32_t word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_INPUT ) ?1 :0 ) << i; + } + +return ret; +} + +static uint32_t riscv_aplic_read_pending_word (RISCVAPLICState *aplic, +uint32_t word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_PENDING ) ?1 :0 ) << i; + } + +return ret; +} + +static void riscv_aplic_set_pending_raw (RISCVAPLICState *aplic, +uint32_t irq,bool pending) +{ +if (pending) { + aplic->state[irq] |=APLIC_ISTATE_PENDING ; + }else { + aplic->state[irq] &= ~APLIC_ISTATE_PENDING ; + } +} + +static void riscv_aplic_set_pending (RISCVAPLICState *aplic, +uint32_t irq,bool pending) +{ +uint32_t sourcecfg, sm; + +if ((irq <=0 ) || (aplic->num_irqs <= irq)) { +return ; + } +hw/intc/riscv_aplic.c line 212
Quoted 10 lines
hw/intc/riscv_aplic.c new file +10
-0+ sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { +return ; + } + + sm = sourcecfg &APLIC_SOURCECFG_SM_MASK ; +if ((sm ==APLIC_SOURCECFG_SM_INACTIVE ) || + (!aplic->msimode && ((sm ==APLIC_SOURCECFG_SM_LEVEL_HIGH ) || + (sm ==APLIC_SOURCECFG_SM_LEVEL_LOW )))) { +return ;Hi Anup,
According to riscv-interrupts-028 spec:
If the source mode is Level1 or Level0 and the interrupt domain is configured in direct delivery mode (domaincfg.DM = 0):
* The pending bit is not cleared by a claim of the interrupt at the PLIC, nor can it be cleared by a write to an in clrip register or to clripnum.
Quoted 6 lines
Also, if the source mode is Level1 or Level0 and the interrupt domain is configured in MSI delivery mode (domaincfg.DM = 1):
* The pending bit cannot be cleared by a write to an in clrip register or to clripnum.It seems that when pending is false,
riscv_aplic_set_pending_raw()should not be called for both direct delivery mode and MIS delivery mode.
For MSI mode (domaincfg.DM = 1), the riscv_aplic_set_pending_raw() can be called for write to a setip or setipnum register but it cannot be called for write to an in clrip register or to clripnum. This is based on below text from AIA spec:
If the source mode is Level1 or Level0 and the interrupt domain is configured in MSI delivery mode (domaincfg.DM = 1):
o The pending bit is set to one by a low-to-high transition in the rectified input
value. The pending bit may also be set by a relevant write to a setip or setipnum
register when the rectified input value is high, but not when the rectified input
value is low.
o The pending bit is cleared whenever the rectified input value is low, or when the interrupt is forwarded by MSI. The pending bit cannot be cleared by a write to an in clrip register or to clripnum.
I think check in riscv_aplic_set_pending() can be updated as below:
if ((sm == APLIC_SOURCECFG_SM_INACTIVE) ||
((!aplic->msimode || (aplic->msimode && !pending)) &&
((sm == APLIC_SOURCECFG_SM_LEVEL_HIGH) ||
(sm == APLIC_SOURCECFG_SM_LEVEL_LOW)))) {
return;
}Regards,
Anup
Quoted 852 lines
Regards,
Frank ChangQuoted 848 lines
hw/intc/riscv_aplic.c new file +758
-0+ } + +riscv_aplic_set_pending_raw (aplic, irq, pending); +} + +static void riscv_aplic_set_pending_word (RISCVAPLICState *aplic, +uint32_t word,uint32_t value, +bool pending) +{ +uint32_t i, irq; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + +if (value & (1U << i)) { +riscv_aplic_set_pending (aplic, irq, pending); + } + } +} + +static uint32_t riscv_aplic_read_enabled_word (RISCVAPLICState *aplic, +int word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_ENABLED ) ?1 :0 ) << i; + } + +return ret; +} + +static void riscv_aplic_set_enabled_raw (RISCVAPLICState *aplic, +uint32_t irq,bool enabled) +{ +if (enabled) { + aplic->state[irq] |=APLIC_ISTATE_ENABLED ; + }else { + aplic->state[irq] &= ~APLIC_ISTATE_ENABLED ; + } +} + +static void riscv_aplic_set_enabled (RISCVAPLICState *aplic, +uint32_t irq,bool enabled) +{ +uint32_t sourcecfg, sm; + +if ((irq <=0 ) || (aplic->num_irqs <= irq)) { +return ; + } + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { +return ; + } + + sm = sourcecfg &APLIC_SOURCECFG_SM_MASK ; +if (sm ==APLIC_SOURCECFG_SM_INACTIVE ) { +return ; + } + +riscv_aplic_set_enabled_raw (aplic, irq, enabled); +} + +static void riscv_aplic_set_enabled_word (RISCVAPLICState *aplic, +uint32_t word,uint32_t value, +bool enabled) +{ +uint32_t i, irq; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + +if (value & (1U << i)) { +riscv_aplic_set_enabled (aplic, irq, enabled); + } + } +} + +static void riscv_aplic_msi_send (RISCVAPLICState *aplic, +uint32_t hart_idx,uint32_t guest_idx, +uint32_t eiid) +{ +uint64_t addr; +MemTxResult result; +RISCVAPLICState *aplic_m; +uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH; + + aplic_m = aplic; +while (aplic_m && !aplic_m->mmode) { + aplic_m = aplic_m->parent; + } +if (!aplic_m) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: m-level APLIC not found\n" , + __func__); +return ; + } + +if (aplic->mmode) { + msicfgaddr = aplic_m->mmsicfgaddr; + msicfgaddrH = aplic_m->mmsicfgaddrH; + }else { + msicfgaddr = aplic_m->smsicfgaddr; + msicfgaddrH = aplic_m->smsicfgaddrH; + } + + lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) & + APLIC_xMSICFGADDRH_LHXS_MASK; + lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) & + APLIC_xMSICFGADDRH_LHXW_MASK; + hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) & + APLIC_xMSICFGADDRH_HHXS_MASK; + hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) & + APLIC_xMSICFGADDRH_HHXW_MASK; + + group_idx = hart_idx >> lhxw; + hart_idx &=APLIC_xMSICFGADDR_PPN_LHX_MASK (lhxw); + + addr = msicfgaddr; + addr |= ((uint64_t )(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK)) <<32 ; + addr |= ((uint64_t )(group_idx &APLIC_xMSICFGADDR_PPN_HHX_MASK (hhxw))) << +APLIC_xMSICFGADDR_PPN_HHX_SHIFT (hhxs); + addr |= ((uint64_t )(hart_idx &APLIC_xMSICFGADDR_PPN_LHX_MASK (lhxw))) << +APLIC_xMSICFGADDR_PPN_LHX_SHIFT (lhxs); + addr |= (uint64_t )(guest_idx &APLIC_xMSICFGADDR_PPN_HART (lhxs)); + addr <<= APLIC_xMSICFGADDR_PPN_SHIFT; + +address_space_stl_le (&address_space_memory, addr, + eiid,MEMTXATTRS_UNSPECIFIED , &result); +if (result !=MEMTX_OK ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: MSI write failed for " +"hart_index=%d guest_index=%d eiid=%d\n" , + __func__, hart_idx, guest_idx, eiid); + } +} + +static void riscv_aplic_msi_irq_update (RISCVAPLICState *aplic,uint32_t irq) +{ +uint32_t hart_idx, guest_idx, eiid; + +if (!aplic->msimode || (aplic->num_irqs <= irq) || + !(aplic->domaincfg &APLIC_DOMAINCFG_IE )) { +return ; + } + +if ((aplic->state[irq] &APLIC_ISTATE_ENPEND ) !=APLIC_ISTATE_ENPEND ) { +return ; + } + +riscv_aplic_set_pending_raw (aplic, irq, false); + + hart_idx = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + hart_idx &=APLIC_TARGET_HART_IDX_MASK ; +if (aplic->mmode) { +/* M-level APLIC ignores guest_index */ + guest_idx =0 ; + }else { + guest_idx = aplic->target[irq] >>APLIC_TARGET_GUEST_IDX_SHIFT ; + guest_idx &=APLIC_TARGET_GUEST_IDX_MASK ; + } + eiid = aplic->target[irq] &APLIC_TARGET_EIID_MASK ; +riscv_aplic_msi_send (aplic, hart_idx, guest_idx, eiid); +} + +static uint32_t riscv_aplic_idc_topi (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t best_irq, best_iprio; +uint32_t irq, iprio, ihartidx, ithres; + +if (aplic->num_harts <= idc) { +return 0 ; + } + + ithres = aplic->ithreshold[idc]; +if (!ithres) { +return 0 ; + } + + best_irq = best_iprio =UINT32_MAX ; +for (irq =1 ; irq < aplic->num_irqs; irq++) { +if ((aplic->state[irq] &APLIC_ISTATE_ENPEND ) != +APLIC_ISTATE_ENPEND ) { +continue ; + } + + ihartidx = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + ihartidx &=APLIC_TARGET_HART_IDX_MASK ; +if (ihartidx != idc) { +continue ; + } + + iprio = aplic->target[irq] & aplic->iprio_mask; +if (iprio >= ithres) { +continue ; + } + +if (iprio < best_iprio) { + best_irq = irq; + best_iprio = iprio; + } + } + +if (best_irq < aplic->num_irqs && best_iprio <= aplic->iprio_mask) { +return (best_irq <<APLIC_IDC_TOPI_ID_SHIFT ) | best_iprio; + } + +return 0 ; +} + +static void riscv_aplic_idc_update (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t topi; + +if (aplic->msimode || aplic->num_harts <= idc) { +return ; + } + + topi =riscv_aplic_idc_topi (aplic, idc); +if ((aplic->domaincfg &APLIC_DOMAINCFG_IE ) && + aplic->idelivery[idc] && + (aplic->iforce[idc] || topi)) { +qemu_irq_raise (aplic->external_irqs[idc]); + }else { +qemu_irq_lower (aplic->external_irqs[idc]); + } +} + +static uint32_t riscv_aplic_idc_claimi (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t irq, state, sm, topi =riscv_aplic_idc_topi (aplic, idc); + +if (!topi) { + aplic->iforce[idc] =0 ; +return 0 ; + } + + irq = (topi >>APLIC_IDC_TOPI_ID_SHIFT ) &APLIC_IDC_TOPI_ID_MASK ; + sm = aplic->sourcecfg[irq] &APLIC_SOURCECFG_SM_MASK ; + state = aplic->state[irq]; +riscv_aplic_set_pending_raw (aplic, irq, false); +if ((sm ==APLIC_SOURCECFG_SM_LEVEL_HIGH ) && + (state &APLIC_ISTATE_INPUT )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + }else if ((sm ==APLIC_SOURCECFG_SM_LEVEL_LOW ) && + !(state &APLIC_ISTATE_INPUT )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + } +riscv_aplic_idc_update (aplic, idc); + +return topi; +} + +static void riscv_aplic_request (void *opaque,int irq,int level) +{ +bool update = false; +RISCVAPLICState *aplic = opaque; +uint32_t sourcecfg, childidx, state, idc; + +assert ((0 < irq) && (irq < aplic->num_irqs)); + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { + childidx = sourcecfg &APLIC_SOURCECFG_CHILDIDX_MASK ; +if (childidx < aplic->num_children) { +riscv_aplic_request (aplic->children[childidx], irq, level); + } +return ; + } + + state = aplic->state[irq]; +switch (sourcecfg &APLIC_SOURCECFG_SM_MASK ) { +case APLIC_SOURCECFG_SM_EDGE_RISE : +if ((level >0 ) && !(state &APLIC_ISTATE_INPUT ) && + !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_EDGE_FALL : +if ((level <=0 ) && (state &APLIC_ISTATE_INPUT ) && + !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_LEVEL_HIGH : +if ((level >0 ) && !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_LEVEL_LOW : +if ((level <=0 ) && !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +default : +break ; + } + +if (level <=0 ) { + aplic->state[irq] &= ~APLIC_ISTATE_INPUT ; + }else { + aplic->state[irq] |=APLIC_ISTATE_INPUT ; + } + +if (update) { +if (aplic->msimode) { +riscv_aplic_msi_irq_update (aplic, irq); + }else { + idc = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + idc &=APLIC_TARGET_HART_IDX_MASK ; +riscv_aplic_idc_update (aplic, idc); + } + } +} + +static uint64_t riscv_aplic_read (void *opaque,hwaddr addr,unsigned size) +{ +uint32_t irq, word, idc; +RISCVAPLICState *aplic = opaque; + +/* Reads must be 4 byte words */ +if ((addr &0x3 ) !=0 ) { + goto err; + } + +if (addr ==APLIC_DOMAINCFG ) { +return aplic->domaincfg | (aplic->msimode ?APLIC_DOMAINCFG_DM :0 ); + }else if ((APLIC_SOURCECFG_BASE <= addr) && + (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_SOURCECFG_BASE ) >>2 ) +1 ; +return aplic->sourcecfg[irq]; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDR )) { +return aplic->mmsicfgaddr; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDRH )) { +return aplic->mmsicfgaddrH; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDR )) { +/* Registers SMSICFGADDR and SMSICFGADDRH are implemented only if: +* (a) the interrupt domain is at machine level +* (b) the domain’s harts implement supervisor mode +* (c) the domain has one or more child supervisor-level domains +* that support MSI delivery mode (domaincfg.DM is not read- +* only zero in at least one of the supervisor-level child +* domains). +*/ +return (aplic->num_children) ? aplic->smsicfgaddr :0 ; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDRH )) { +return (aplic->num_children) ? aplic->smsicfgaddrH :0 ; + }else if ((APLIC_SETIP_BASE <= addr) && + (addr < (APLIC_SETIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIP_BASE ) >>2 ; +return riscv_aplic_read_pending_word (aplic, word); + }else if (addr ==APLIC_SETIPNUM ) { +return 0 ; + }else if ((APLIC_CLRIP_BASE <= addr) && + (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_CLRIP_BASE ) >>2 ; +return riscv_aplic_read_input_word (aplic, word); + }else if (addr ==APLIC_CLRIPNUM ) { +return 0 ; + }else if ((APLIC_SETIE_BASE <= addr) && + (addr < (APLIC_SETIE_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIE_BASE ) >>2 ; +return riscv_aplic_read_enabled_word (aplic, word); + }else if (addr ==APLIC_SETIENUM ) { +return 0 ; + }else if ((APLIC_CLRIE_BASE <= addr) && + (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words *4 ))) { +return 0 ; + }else if (addr ==APLIC_CLRIENUM ) { +return 0 ; + }else if (addr ==APLIC_SETIPNUM_LE ) { +return 0 ; + }else if (addr ==APLIC_SETIPNUM_BE ) { +return 0 ; + }else if (addr ==APLIC_GENMSI ) { +return (aplic->msimode) ? aplic->genmsi :0 ; + }else if ((APLIC_TARGET_BASE <= addr) && + (addr < (APLIC_TARGET_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_TARGET_BASE ) >>2 ) +1 ; +return aplic->target[irq]; + }else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) && + (addr < (APLIC_IDC_BASE + aplic->num_harts *APLIC_IDC_SIZE ))) { + idc = (addr -APLIC_IDC_BASE ) /APLIC_IDC_SIZE ; +switch (addr - (APLIC_IDC_BASE + idc *APLIC_IDC_SIZE )) { +case APLIC_IDC_IDELIVERY : +return aplic->idelivery[idc]; +case APLIC_IDC_IFORCE : +return aplic->iforce[idc]; +case APLIC_IDC_ITHRESHOLD : +return aplic->ithreshold[idc]; +case APLIC_IDC_TOPI : +return riscv_aplic_idc_topi (aplic, idc); +case APLIC_IDC_CLAIMI : +return riscv_aplic_idc_claimi (aplic, idc); +default : + goto err; + }; + } + +err: +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Invalid register read 0x%" HWADDR_PRIx"\n" , + __func__, addr); +return 0 ; +} + +static void riscv_aplic_write (void *opaque,hwaddr addr,uint64_t value, +unsigned size) +{ +RISCVAPLICState *aplic = opaque; +uint32_t irq, word, idc =UINT32_MAX ; + +/* Writes must be 4 byte words */ +if ((addr &0x3 ) !=0 ) { + goto err; + } + +if (addr ==APLIC_DOMAINCFG ) { +/* Only IE bit writeable at the moment */ + value &=APLIC_DOMAINCFG_IE ; + aplic->domaincfg = value; + }else if ((APLIC_SOURCECFG_BASE <= addr) && + (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_SOURCECFG_BASE ) >>2 ) +1 ; +if (!aplic->num_children && (value &APLIC_SOURCECFG_D )) { + value =0 ; + } +if (value &APLIC_SOURCECFG_D ) { + value &= (APLIC_SOURCECFG_D |APLIC_SOURCECFG_CHILDIDX_MASK ); + }else { + value &= (APLIC_SOURCECFG_D |APLIC_SOURCECFG_SM_MASK ); + } + aplic->sourcecfg[irq] = value; +if ((aplic->sourcecfg[irq] &APLIC_SOURCECFG_D ) || + (aplic->sourcecfg[irq] ==0 )) { +riscv_aplic_set_pending_raw (aplic, irq, false); +riscv_aplic_set_enabled_raw (aplic, irq, false); + } + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDR )) { +if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->mmsicfgaddr = value; + } + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDRH )) { +if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->mmsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK; + } + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDR )) { +/* Registers SMSICFGADDR and SMSICFGADDRH are implemented only if: +* (a) the interrupt domain is at machine level +* (b) the domain’s harts implement supervisor mode +* (c) the domain has one or more child supervisor-level domains +* that support MSI delivery mode (domaincfg.DM is not read- +* only zero in at least one of the supervisor-level child +* domains). +*/ +if (aplic->num_children && + !(aplic->smsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->smsicfgaddr = value; + } + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDRH )) { +if (aplic->num_children && + !(aplic->smsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->smsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK; + } + }else if ((APLIC_SETIP_BASE <= addr) && + (addr < (APLIC_SETIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIP_BASE ) >>2 ; +riscv_aplic_set_pending_word (aplic, word, value, true); + }else if (addr ==APLIC_SETIPNUM ) { +riscv_aplic_set_pending (aplic, value, true); + }else if ((APLIC_CLRIP_BASE <= addr) && + (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_CLRIP_BASE ) >>2 ; +riscv_aplic_set_pending_word (aplic, word, value, false); + }else if (addr ==APLIC_CLRIPNUM ) { +riscv_aplic_set_pending (aplic, value, false); + }else if ((APLIC_SETIE_BASE <= addr) && + (addr < (APLIC_SETIE_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIE_BASE ) >>2 ; +riscv_aplic_set_enabled_word (aplic, word, value, true); + }else if (addr ==APLIC_SETIENUM ) { +riscv_aplic_set_enabled (aplic, value, true); + }else if ((APLIC_CLRIE_BASE <= addr) && + (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_CLRIE_BASE ) >>2 ; +riscv_aplic_set_enabled_word (aplic, word, value, false); + }else if (addr ==APLIC_CLRIENUM ) { +riscv_aplic_set_enabled (aplic, value, false); + }else if (addr ==APLIC_SETIPNUM_LE ) { +riscv_aplic_set_pending (aplic, value, true); + }else if (addr ==APLIC_SETIPNUM_BE ) { +riscv_aplic_set_pending (aplic,bswap32 (value), true); + }else if (addr ==APLIC_GENMSI ) { +if (aplic->msimode) { + aplic->genmsi = value & ~(APLIC_TARGET_GUEST_IDX_MASK << +APLIC_TARGET_GUEST_IDX_SHIFT ); +riscv_aplic_msi_send (aplic, + value >>APLIC_TARGET_HART_IDX_SHIFT , +0 , + value &APLIC_TARGET_EIID_MASK ); + } + }else if ((APLIC_TARGET_BASE <= addr) && + (addr < (APLIC_TARGET_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_TARGET_BASE ) >>2 ) +1 ; +if (aplic->msimode) { + aplic->target[irq] = value; + }else { + aplic->target[irq] = (value & ~APLIC_TARGET_IPRIO_MASK ) | + (value & aplic->iprio_mask); + } + }else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) && + (addr < (APLIC_IDC_BASE + aplic->num_harts *APLIC_IDC_SIZE ))) { + idc = (addr -APLIC_IDC_BASE ) /APLIC_IDC_SIZE ; +switch (addr - (APLIC_IDC_BASE + idc *APLIC_IDC_SIZE )) { +case APLIC_IDC_IDELIVERY : + aplic->idelivery[idc] = value &0x1 ; +break ; +case APLIC_IDC_IFORCE : + aplic->iforce[idc] = value &0x1 ; +break ; +case APLIC_IDC_ITHRESHOLD : + aplic->ithreshold[idc] = value & aplic->iprio_mask; +break ; +default : + goto err; + }; + }else { + goto err; + } + +if (aplic->msimode) { +for (irq =1 ; irq < aplic->num_irqs; irq++) { +riscv_aplic_msi_irq_update (aplic, irq); + } + }else { +if (idc ==UINT32_MAX ) { +for (idc =0 ; idc < aplic->num_harts; idc++) { +riscv_aplic_idc_update (aplic, idc); + } + }else { +riscv_aplic_idc_update (aplic, idc); + } + } + +return ; + +err: +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Invalid register write 0x%" HWADDR_PRIx"\n" , + __func__, addr); +} + +static const MemoryRegionOps riscv_aplic_ops = { + .read = riscv_aplic_read, + .write = riscv_aplic_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .valid = { + .min_access_size =4 , + .max_access_size =4 + } +}; + +static void riscv_aplic_realize (DeviceState *dev,Error **errp) +{ +uint32_t i; +RISCVAPLICState *aplic =RISCV_APLIC (dev); + + aplic->bitfield_words = (aplic->num_irqs +31 ) >>5 ; + aplic->sourcecfg =g_new0 (uint32_t, aplic->num_irqs); + aplic->state =g_new (uint32_t, aplic->num_irqs); + aplic->target =g_new0 (uint32_t, aplic->num_irqs); + aplic->idelivery =g_new0 (uint32_t, aplic->num_harts); + aplic->iforce =g_new0 (uint32_t, aplic->num_harts); + aplic->ithreshold =g_new0 (uint32_t, aplic->num_harts); + +memory_region_init_io (&aplic->mmio,OBJECT (dev), &riscv_aplic_ops, aplic, +TYPE_RISCV_APLIC , aplic->aperture_size); +sysbus_init_mmio (SYS_BUS_DEVICE (dev), &aplic->mmio); + +/* Only root APLICs have hardware IRQ lines. All non-root APLICs +* have IRQ lines delegated by their parent APLIC. +*/ +if (!aplic->parent) { +qdev_init_gpio_in (dev, riscv_aplic_request, aplic->num_irqs); + } + +/* Create output IRQ lines for non-MSI mode */ +if (!aplic->msimode) { + aplic->external_irqs =g_malloc (sizeof (qemu_irq) * aplic->num_harts); +qdev_init_gpio_out (dev, aplic->external_irqs, aplic->num_harts); + +/* Claim the CPU interrupt to be triggered by this APLIC */ +for (i =0 ; i < aplic->num_harts; i++) { +RISCVCPU *cpu =RISCV_CPU (qemu_get_cpu (aplic->hartid_base + i)); +if (riscv_cpu_claim_interrupts (cpu, + (aplic->mmode) ?MIP_MEIP :MIP_SEIP ) <0 ) { +error_report ("%s already claimed" , + (aplic->mmode) ?"MEIP" :"SEIP" ); +exit (1 ); + } + } + } + + msi_nonbroken = true; +} + +static Property riscv_aplic_properties[] = { +DEFINE_PROP_UINT32 ("aperture-size" , RISCVAPLICState, aperture_size,0 ), +DEFINE_PROP_UINT32 ("hartid-base" , RISCVAPLICState, hartid_base,0 ), +DEFINE_PROP_UINT32 ("num-harts" , RISCVAPLICState, num_harts,0 ), +DEFINE_PROP_UINT32 ("iprio-mask" , RISCVAPLICState, iprio_mask,0 ), +DEFINE_PROP_UINT32 ("num-irqs" , RISCVAPLICState, num_irqs,0 ), +DEFINE_PROP_BOOL ("msimode" , RISCVAPLICState, msimode,0 ), +DEFINE_PROP_BOOL ("mmode" , RISCVAPLICState, mmode,0 ), +DEFINE_PROP_END_OF_LIST (), +}; + +static const VMStateDescription vmstate_riscv_aplic = { + .name ="riscv_aplic" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT32 (domaincfg, RISCVAPLICState), +VMSTATE_UINT32 (mmsicfgaddr, RISCVAPLICState), +VMSTATE_UINT32 (mmsicfgaddrH, RISCVAPLICState), +VMSTATE_UINT32 (smsicfgaddr, RISCVAPLICState), +VMSTATE_UINT32 (smsicfgaddrH, RISCVAPLICState), +VMSTATE_UINT32 (genmsi, RISCVAPLICState), +VMSTATE_VARRAY_UINT32 (sourcecfg, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (state, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (target, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (idelivery, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (iforce, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (ithreshold, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_END_OF_LIST () + } +}; + +static void riscv_aplic_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + +device_class_set_props (dc, riscv_aplic_properties); + dc->realize = riscv_aplic_realize; + dc->vmsd = &vmstate_riscv_aplic; +} + +static const TypeInfo riscv_aplic_info = { + .name =TYPE_RISCV_APLIC , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (RISCVAPLICState), + .class_init = riscv_aplic_class_init, +}; + +static void riscv_aplic_register_types (void ) +{ +type_register_static (&riscv_aplic_info); +} + +type_init (riscv_aplic_register_types) + +/* +* Add a APLIC device to another APLIC device as child for +* interrupt delegation. +*/ +void riscv_aplic_add_child(DeviceState *parent, DeviceState *child) +{ +RISCVAPLICState *caplic, *paplic; + +assert (parent && child); + caplic =RISCV_APLIC (child); + paplic =RISCV_APLIC (parent); + +assert (paplic->num_irqs == caplic->num_irqs); +assert (paplic->num_children <=QEMU_APLIC_MAX_CHILDREN ); + + caplic->parent = paplic; + paplic->children[paplic->num_children] = caplic; + paplic->num_children++; +} + +/* +* Create APLIC device. +*/ +DeviceState *riscv_aplic_create (hwaddr addr,hwaddr size, +uint32_t hartid_base,uint32_t num_harts,uint32_t num_sources, +uint32_t iprio_bits,bool msimode,bool mmode,DeviceState *parent) +{ +DeviceState *dev =qdev_new (TYPE_RISCV_APLIC ); +uint32_t i; + +assert (num_harts <APLIC_MAX_IDC ); +assert ((APLIC_IDC_BASE + (num_harts *APLIC_IDC_SIZE )) <= size); +assert (num_sources <APLIC_MAX_SOURCE ); +assert (APLIC_MIN_IPRIO_BITS <= iprio_bits); +assert (iprio_bits <=APLIC_MAX_IPRIO_BITS ); + +qdev_prop_set_uint32 (dev,"aperture-size" , size); +qdev_prop_set_uint32 (dev,"hartid-base" , hartid_base); +qdev_prop_set_uint32 (dev,"num-harts" , num_harts); +qdev_prop_set_uint32 (dev,"iprio-mask" , ((1U << iprio_bits) -1 )); +qdev_prop_set_uint32 (dev,"num-irqs" , num_sources +1 ); +qdev_prop_set_bit (dev,"msimode" , msimode); +qdev_prop_set_bit (dev,"mmode" , mmode); + +sysbus_realize_and_unref (SYS_BUS_DEVICE (dev), &error_fatal); +sysbus_mmio_map (SYS_BUS_DEVICE (dev),0 , addr); + +if (parent) { +riscv_aplic_add_child (parent, dev); + } + +if (!msimode) { +for (i =0 ; i < num_harts; i++) { +CPUState *cpu =qemu_get_cpu (hartid_base + i); + +qdev_connect_gpio_out_named (dev,NULL , i, +qdev_get_gpio_in (DEVICE (cpu), + (mmode) ?IRQ_M_EXT :IRQ_S_EXT )); + } + } + +return dev; +}include/hw/intc/riscv_aplic.h new file +79
-1@@ -0,0 +1,79 @@ +/* +* RISC-V APLIC (Advanced Platform Level Interrupt Controller) interface +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef HW_RISCV_APLIC_H +#define HW_RISCV_APLIC_H + +#include "hw/sysbus.h" +#include "qom/object.h" + +#define TYPE_RISCV_APLIC "riscv.aplic" + +typedef struct RISCVAPLICState RISCVAPLICState ; +DECLARE_INSTANCE_CHECKER(RISCVAPLICState, RISCV_APLIC, TYPE_RISCV_APLIC) + +#define APLIC_MIN_SIZE 0x4000 +#define APLIC_SIZE_ALIGN (__x) (((__x) + (APLIC_MIN_SIZE - 1)) & \ + ~(APLIC_MIN_SIZE - 1)) +#define APLIC_SIZE (__num_harts) (APLIC_MIN_SIZE + \ + APLIC_SIZE_ALIGN(32 * (__num_harts))) + +struct RISCVAPLICState { +/*< private >*/ +SysBusDevice parent_obj; +qemu_irq *external_irqs; + +/*< public >*/ +MemoryRegion mmio; +uint32_t bitfield_words; +uint32_t domaincfg; +uint32_t mmsicfgaddr; +uint32_t mmsicfgaddrH; +uint32_t smsicfgaddr; +uint32_t smsicfgaddrH; +uint32_t genmsi; +uint32_t *sourcecfg; +uint32_t *state; +uint32_t *target; +uint32_t *idelivery; +uint32_t *iforce; +uint32_t *ithreshold; + +/* topology */ +#define QEMU_APLIC_MAX_CHILDREN 16 +struct RISCVAPLICState *parent; +struct RISCVAPLICState *children[QEMU_APLIC_MAX_CHILDREN ]; +uint16_t num_children; + +/* config */ +uint32_t aperture_size; +uint32_t hartid_base; +uint32_t num_harts; +uint32_t iprio_mask; +uint32_t num_irqs; +bool msimode; +bool mmode; +}; + +void riscv_aplic_add_child (DeviceState *parent,DeviceState *child); + +DeviceState *riscv_aplic_create (hwaddr addr,hwaddr size, +uint32_t hartid_base,uint32_t num_harts,uint32_t num_sources, +uint32_t iprio_bits,bool msimode,bool mmode,DeviceState *parent); + +#endif --2.25.1
Re: [PATCH v6 18/23] hw/intc: Add RISC-V AIA APLIC device emulation
In reply to Anup PatelAnup Patel <[email protected]> 於 2021年12月30日 週四 下午8:55寫道:
Quoted 456 lines
From: Anup Patel <[email protected]>
The RISC-V AIA (Advanced Interrupt Architecture) defines a new interrupt controller for wired interrupts called APLIC (Advanced Platform Level Interrupt Controller). The APLIC is capabable of forwarding wired interupts to RISC-V HARTs directly or as MSIs (Message Signaled Interupts).
This patch adds device emulation for RISC-V AIA APLIC.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- hw/intc/Kconfig | 3 + hw/intc/meson.build | 1 + hw/intc/riscv_aplic.c | 970 ++++++++++++++++++++++++++++++++++ include/hw/intc/riscv_aplic.h | 79 +++ 4 files changed, 1053 insertions(+) create mode 100644 hw/intc/riscv_aplic.c create mode 100644 include/hw/intc/riscv_aplic.hhw/intc/Kconfig +3
-0@@ -70,6 +70,9 @@ config LOONGSON_LIOINTC config RISCV_ACLINT bool +config RISCV_APLIC + bool + config SIFIVE_PLIC boolhw/intc/meson.build +1
-0@@ -50,6 +50,7 @@ specific_ss.add(when: 'CONFIG_S390_FLIC', if_true: files('s390_flic.c')) specific_ss.add(when: 'CONFIG_S390_FLIC_KVM', if_true: files('s390_flic_kvm.c')) specific_ss.add(when: 'CONFIG_SH_INTC', if_true: files('sh_intc.c')) specific_ss.add(when: 'CONFIG_RISCV_ACLINT', if_true: files('riscv_aclint.c')) +specific_ss.add(when: 'CONFIG_RISCV_APLIC', if_true:
files('riscv_aplic.c'))
specific_ss.add(when: 'CONFIG_SIFIVE_PLIC',if_true:files('sifive_plic.c'))
specific_ss.add(when: 'CONFIG_XICS', if_true: files('xics.c'))specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_XICS'],hw/intc/riscv_aplic.c new file +12
-0@@ -0,0 +1,970 @@ +/* +* RISC-V APLIC (Advanced Platform Level Interrupt Controller) +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */hw/intc/riscv_aplic.c new file +326
-0+ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "qemu/module.h" +#include "qemu/error-report.h" +#include "qemu/bswap.h" +#include "exec/address-spaces.h" +#include "hw/sysbus.h" +#include "hw/pci/msi.h" +#include "hw/boards.h" +#include "hw/qdev-properties.h" +#include "hw/intc/riscv_aplic.h" +#include "hw/irq.h" +#include "target/riscv/cpu.h" +#include "sysemu/sysemu.h" +#include "migration/vmstate.h" + +#define APLIC_MAX_IDC (1UL << 14) +#define APLIC_MAX_SOURCE 1024 +#define APLIC_MIN_IPRIO_BITS 1 +#define APLIC_MAX_IPRIO_BITS 8 +#define APLIC_MAX_CHILDREN 1024 + +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_DOMAINCFG_IE (1 << 8) +#define APLIC_DOMAINCFG_DM (1 << 2) +#define APLIC_DOMAINCFG_BE (1 << 0) + +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_D (1 << 10) +#define APLIC_SOURCECFG_CHILDIDX_MASK 0x000003ff +#define APLIC_SOURCECFG_SM_MASK 0x00000007 +#define APLIC_SOURCECFG_SM_INACTIVE 0x0 +#define APLIC_SOURCECFG_SM_DETACH 0x1 +#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4 +#define APLIC_SOURCECFG_SM_EDGE_FALL 0x5 +#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6 +#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 + +#define APLIC_MMSICFGADDR 0x1bc0 +#define APLIC_MMSICFGADDRH 0x1bc4 +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#define APLIC_xMSICFGADDRH_L (1UL << 31) +#define APLIC_xMSICFGADDRH_HHXS_MASK 0x1f +#define APLIC_xMSICFGADDRH_HHXS_SHIFT 24 +#define APLIC_xMSICFGADDRH_LHXS_MASK 0x7 +#define APLIC_xMSICFGADDRH_LHXS_SHIFT 20 +#define APLIC_xMSICFGADDRH_HHXW_MASK 0x7 +#define APLIC_xMSICFGADDRH_HHXW_SHIFT 16 +#define APLIC_xMSICFGADDRH_LHXW_MASK 0xf +#define APLIC_xMSICFGADDRH_LHXW_SHIFT 12 +#define APLIC_xMSICFGADDRH_BAPPN_MASK 0xfff + +#define APLIC_xMSICFGADDR_PPN_SHIFT 12 + +#define APLIC_xMSICFGADDR_PPN_HART (__lhxs) \ + ((1UL << (__lhxs)) - 1) + +#define APLIC_xMSICFGADDR_PPN_LHX_MASK (__lhxw) \ + ((1UL << (__lhxw)) - 1) +#define APLIC_xMSICFGADDR_PPN_LHX_SHIFT (__lhxs) \ + ((__lhxs)) +#define APLIC_xMSICFGADDR_PPN_LHX (__lhxw, __lhxs) \ + (APLIC_xMSICFGADDR_PPN_LHX_MASK(__lhxw) << \ + APLIC_xMSICFGADDR_PPN_LHX_SHIFT(__lhxs)) + +#define APLIC_xMSICFGADDR_PPN_HHX_MASK (__hhxw) \ + ((1UL << (__hhxw)) - 1) +#define APLIC_xMSICFGADDR_PPN_HHX_SHIFT (__hhxs) \ + ((__hhxs) + APLIC_xMSICFGADDR_PPN_SHIFT) +#define APLIC_xMSICFGADDR_PPN_HHX (__hhxw, __hhxs) \ + (APLIC_xMSICFGADDR_PPN_HHX_MASK(__hhxw) << \ + APLIC_xMSICFGADDR_PPN_HHX_SHIFT(__hhxs)) + +#define APLIC_xMSICFGADDRH_VALID_MASK \ + (APLIC_xMSICFGADDRH_L | \ + (APLIC_xMSICFGADDRH_HHXS_MASK << APLIC_xMSICFGADDRH_HHXS_SHIFT) | \ + (APLIC_xMSICFGADDRH_LHXS_MASK << APLIC_xMSICFGADDRH_LHXS_SHIFT) | \ + (APLIC_xMSICFGADDRH_HHXW_MASK << APLIC_xMSICFGADDRH_HHXW_SHIFT) | \ + (APLIC_xMSICFGADDRH_LHXW_MASK << APLIC_xMSICFGADDRH_LHXW_SHIFT) | \ + APLIC_xMSICFGADDRH_BAPPN_MASK) + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIENUM 0x1fdc + +#define APLIC_SETIPNUM_LE 0x2000 +#define APLIC_SETIPNUM_BE 0x2004 + +#define APLIC_ISTATE_PENDING (1U << 0) +#define APLIC_ISTATE_ENABLED (1U << 1) +#define APLIC_ISTATE_ENPEND (APLIC_ISTATE_ENABLED | \ + APLIC_ISTATE_PENDING) +#define APLIC_ISTATE_INPUT (1U << 8) + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_HART_IDX_SHIFT 18 +#define APLIC_TARGET_HART_IDX_MASK 0x3fff +#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_GUEST_IDX_MASK 0x3f +#define APLIC_TARGET_IPRIO_MASK 0xff +#define APLIC_TARGET_EIID_MASK 0x7ff + +#define APLIC_IDC_BASE 0x4000 +#define APLIC_IDC_SIZE 32 + +#define APLIC_IDC_IDELIVERY 0x00 + +#define APLIC_IDC_IFORCE 0x04 + +#define APLIC_IDC_ITHRESHOLD 0x08 + +#define APLIC_IDC_TOPI 0x18 +#define APLIC_IDC_TOPI_ID_SHIFT 16 +#define APLIC_IDC_TOPI_ID_MASK 0x3ff +#define APLIC_IDC_TOPI_PRIO_MASK 0xff + +#define APLIC_IDC_CLAIMI 0x1c + +static uint32_t riscv_aplic_read_input_word (RISCVAPLICState *aplic, +uint32_t word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_INPUT ) ?1 :0 ) << i; + } + +return ret; +} + +static uint32_t riscv_aplic_read_pending_word (RISCVAPLICState *aplic, +uint32_t word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_PENDING ) ?1 :0 ) << i; + } + +return ret; +} + +static void riscv_aplic_set_pending_raw (RISCVAPLICState *aplic, +uint32_t irq,bool pending) +{ +if (pending) { + aplic->state[irq] |=APLIC_ISTATE_PENDING ; + }else { + aplic->state[irq] &= ~APLIC_ISTATE_PENDING ; + } +} + +static void riscv_aplic_set_pending (RISCVAPLICState *aplic, +uint32_t irq,bool pending) +{ +uint32_t sourcecfg, sm; + +if ((irq <=0 ) || (aplic->num_irqs <= irq)) { +return ; + } + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { +return ; + } + + sm = sourcecfg &APLIC_SOURCECFG_SM_MASK ; +if ((sm ==APLIC_SOURCECFG_SM_INACTIVE ) || + (!aplic->msimode && ((sm ==APLIC_SOURCECFG_SM_LEVEL_HIGH ) || + (sm ==APLIC_SOURCECFG_SM_LEVEL_LOW )))) { +return ; + } + +riscv_aplic_set_pending_raw (aplic, irq, pending); +} + +static void riscv_aplic_set_pending_word (RISCVAPLICState *aplic, +uint32_t word,uint32_t value, +bool pending) +{ +uint32_t i, irq; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + +if (value & (1U << i)) { +riscv_aplic_set_pending (aplic, irq, pending); + } + } +} + +static uint32_t riscv_aplic_read_enabled_word (RISCVAPLICState *aplic, +int word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_ENABLED ) ?1 :0 ) << i; + } + +return ret; +} + +static void riscv_aplic_set_enabled_raw (RISCVAPLICState *aplic, +uint32_t irq,bool enabled) +{ +if (enabled) { + aplic->state[irq] |=APLIC_ISTATE_ENABLED ; + }else { + aplic->state[irq] &= ~APLIC_ISTATE_ENABLED ; + } +} + +static void riscv_aplic_set_enabled (RISCVAPLICState *aplic, +uint32_t irq,bool enabled) +{ +uint32_t sourcecfg, sm; + +if ((irq <=0 ) || (aplic->num_irqs <= irq)) { +return ; + } + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { +return ; + } + + sm = sourcecfg &APLIC_SOURCECFG_SM_MASK ; +if (sm ==APLIC_SOURCECFG_SM_INACTIVE ) { +return ; + } + +riscv_aplic_set_enabled_raw (aplic, irq, enabled); +} + +static void riscv_aplic_set_enabled_word (RISCVAPLICState *aplic, +uint32_t word,uint32_t value, +bool enabled) +{ +uint32_t i, irq; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + +if (value & (1U << i)) { +riscv_aplic_set_enabled (aplic, irq, enabled); + } + } +} + +static void riscv_aplic_msi_send (RISCVAPLICState *aplic, +uint32_t hart_idx,uint32_t guest_idx, +uint32_t eiid) +{ +uint64_t addr; +MemTxResult result; +RISCVAPLICState *aplic_m; +uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH; + + aplic_m = aplic; +while (aplic_m && !aplic_m->mmode) { + aplic_m = aplic_m->parent; + } +if (!aplic_m) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: m-level APLIC not found\n" , + __func__); +return ; + } + +if (aplic->mmode) { + msicfgaddr = aplic_m->mmsicfgaddr; + msicfgaddrH = aplic_m->mmsicfgaddrH; + }else { + msicfgaddr = aplic_m->smsicfgaddr; + msicfgaddrH = aplic_m->smsicfgaddrH; + } + + lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) & + APLIC_xMSICFGADDRH_LHXS_MASK; + lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) & + APLIC_xMSICFGADDRH_LHXW_MASK; + hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) & + APLIC_xMSICFGADDRH_HHXS_MASK; + hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) & + APLIC_xMSICFGADDRH_HHXW_MASK; + + group_idx = hart_idx >> lhxw; + hart_idx &=APLIC_xMSICFGADDR_PPN_LHX_MASK (lhxw); + + addr = msicfgaddr; + addr |= ((uint64_t)(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK)) <<32;
+ addr |= ((uint64_t)(group_idx &APLIC_xMSICFGADDR_PPN_HHX_MASK(hhxw))) <<hw/intc/riscv_aplic.c new file +2
-0+APLIC_xMSICFGADDR_PPN_HHX_SHIFT (hhxs); + addr |= ((uint64_t )(hart_idx &APLIC_xMSICFGADDR_PPN_LHX_MASK (lhxw)))<<
hw/intc/riscv_aplic.c new file +14
-0+APLIC_xMSICFGADDR_PPN_LHX_SHIFT (lhxs); + addr |= (uint64_t )(guest_idx &APLIC_xMSICFGADDR_PPN_HART (lhxs)); + addr <<= APLIC_xMSICFGADDR_PPN_SHIFT; + +address_space_stl_le (&address_space_memory, addr, + eiid,MEMTXATTRS_UNSPECIFIED , &result); +if (result !=MEMTX_OK ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: MSI write failed for " +"hart_index=%d guest_index=%d eiid=%d\n" , + __func__, hart_idx, guest_idx, eiid); + } +} + +static void riscv_aplic_msi_irq_update(RISCVAPLICState *aplic, uint32_tirq)
hw/intc/riscv_aplic.c new file +9
-0+{ +uint32_t hart_idx, guest_idx, eiid; + +if (!aplic->msimode || (aplic->num_irqs <= irq) || + !(aplic->domaincfg &APLIC_DOMAINCFG_IE )) { +return ; + } + +if ((aplic->state[irq] &APLIC_ISTATE_ENPEND ) !=APLIC_ISTATE_ENPEND ){
hw/intc/riscv_aplic.c new file +21
-0+return ; + } + +riscv_aplic_set_pending_raw (aplic, irq, false); + + hart_idx = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + hart_idx &=APLIC_TARGET_HART_IDX_MASK ; +if (aplic->mmode) { +/* M-level APLIC ignores guest_index */ + guest_idx =0 ; + }else { + guest_idx = aplic->target[irq] >>APLIC_TARGET_GUEST_IDX_SHIFT ; + guest_idx &=APLIC_TARGET_GUEST_IDX_MASK ; + } + eiid = aplic->target[irq] &APLIC_TARGET_EIID_MASK ; +riscv_aplic_msi_send (aplic, hart_idx, guest_idx, eiid); +} + +static uint32_t riscv_aplic_idc_topi (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t best_irq, best_iprio;
hw/intc/riscv_aplic.c line 400
Quoted 10 lines
hw/intc/riscv_aplic.c new file +10
-0+uint32_t irq, iprio, ihartidx, ithres; + +if (aplic->num_harts <= idc) { +return 0 ; + } + + ithres = aplic->ithreshold[idc]; +if (!ithres) { +return 0 ; + }
Hi Anup,
I'm curious why topi register returns 0 when ithreshold register is 0.
According to spec:
A read of topi returns zero either if no interrupt that is targeted to this hart is both pending and
enabled, or if ithreshold is not zero and no pending-and-enabled interrupt targeted to this hart
has a priority number less than the value of ithreshold.
Am I missing anything?
Regards,
Frank Chang
+
Quoted 670 lines
hw/intc/riscv_aplic.c new file +50
-0+ best_irq = best_iprio =UINT32_MAX ; +for (irq =1 ; irq < aplic->num_irqs; irq++) { +if ((aplic->state[irq] &APLIC_ISTATE_ENPEND ) != +APLIC_ISTATE_ENPEND ) { +continue ; + } + + ihartidx = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + ihartidx &=APLIC_TARGET_HART_IDX_MASK ; +if (ihartidx != idc) { +continue ; + } + + iprio = aplic->target[irq] & aplic->iprio_mask; +if (iprio >= ithres) { +continue ; + } + +if (iprio < best_iprio) { + best_irq = irq; + best_iprio = iprio; + } + } + +if (best_irq < aplic->num_irqs && best_iprio <= aplic->iprio_mask) { +return (best_irq <<APLIC_IDC_TOPI_ID_SHIFT ) | best_iprio; + } + +return 0 ; +} + +static void riscv_aplic_idc_update (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t topi; + +if (aplic->msimode || aplic->num_harts <= idc) { +return ; + } + + topi =riscv_aplic_idc_topi (aplic, idc); +if ((aplic->domaincfg &APLIC_DOMAINCFG_IE ) && + aplic->idelivery[idc] && + (aplic->iforce[idc] || topi)) { +qemu_irq_raise (aplic->external_irqs[idc]); + }else { +qemu_irq_lower (aplic->external_irqs[idc]); + } +} + +static uint32_t riscv_aplic_idc_claimi(RISCVAPLICState *aplic, uint32_tidc)
hw/intc/riscv_aplic.c new file +102
-0+{ +uint32_t irq, state, sm, topi =riscv_aplic_idc_topi (aplic, idc); + +if (!topi) { + aplic->iforce[idc] =0 ; +return 0 ; + } + + irq = (topi >>APLIC_IDC_TOPI_ID_SHIFT ) &APLIC_IDC_TOPI_ID_MASK ; + sm = aplic->sourcecfg[irq] &APLIC_SOURCECFG_SM_MASK ; + state = aplic->state[irq]; +riscv_aplic_set_pending_raw (aplic, irq, false); +if ((sm ==APLIC_SOURCECFG_SM_LEVEL_HIGH ) && + (state &APLIC_ISTATE_INPUT )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + }else if ((sm ==APLIC_SOURCECFG_SM_LEVEL_LOW ) && + !(state &APLIC_ISTATE_INPUT )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + } +riscv_aplic_idc_update (aplic, idc); + +return topi; +} + +static void riscv_aplic_request (void *opaque,int irq,int level) +{ +bool update = false; +RISCVAPLICState *aplic = opaque; +uint32_t sourcecfg, childidx, state, idc; + +assert ((0 < irq) && (irq < aplic->num_irqs)); + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { + childidx = sourcecfg &APLIC_SOURCECFG_CHILDIDX_MASK ; +if (childidx < aplic->num_children) { +riscv_aplic_request (aplic->children[childidx], irq, level); + } +return ; + } + + state = aplic->state[irq]; +switch (sourcecfg &APLIC_SOURCECFG_SM_MASK ) { +case APLIC_SOURCECFG_SM_EDGE_RISE : +if ((level >0 ) && !(state &APLIC_ISTATE_INPUT ) && + !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_EDGE_FALL : +if ((level <=0 ) && (state &APLIC_ISTATE_INPUT ) && + !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_LEVEL_HIGH : +if ((level >0 ) && !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_LEVEL_LOW : +if ((level <=0 ) && !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +default : +break ; + } + +if (level <=0 ) { + aplic->state[irq] &= ~APLIC_ISTATE_INPUT ; + }else { + aplic->state[irq] |=APLIC_ISTATE_INPUT ; + } + +if (update) { +if (aplic->msimode) { +riscv_aplic_msi_irq_update (aplic, irq); + }else { + idc = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + idc &=APLIC_TARGET_HART_IDX_MASK ; +riscv_aplic_idc_update (aplic, idc); + } + } +} + +static uint64_t riscv_aplic_read (void *opaque,hwaddr addr,unsigned size) +{ +uint32_t irq, word, idc; +RISCVAPLICState *aplic = opaque; + +/* Reads must be 4 byte words */ +if ((addr &0x3 ) !=0 ) { + goto err; + } + +if (addr ==APLIC_DOMAINCFG ) { + return aplic->domaincfg | (aplic->msimode ? APLIC_DOMAINCFG_DM :0);
hw/intc/riscv_aplic.c new file +59
-0+ } else if ((APLIC_SOURCECFG_BASE <= addr) && + (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_SOURCECFG_BASE ) >>2 ) +1 ; +return aplic->sourcecfg[irq]; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDR )) { +return aplic->mmsicfgaddr; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDRH )) { +return aplic->mmsicfgaddrH; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDR )) { +/* Registers SMSICFGADDR and SMSICFGADDRH are implemented only if: +* (a) the interrupt domain is at machine level +* (b) the domain’s harts implement supervisor mode +* (c) the domain has one or more child supervisor-level domains +* that support MSI delivery mode (domaincfg.DM is not read- +* only zero in at least one of the supervisor-level child +* domains). +*/ +return (aplic->num_children) ? aplic->smsicfgaddr :0 ; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDRH )) { +return (aplic->num_children) ? aplic->smsicfgaddrH :0 ; + }else if ((APLIC_SETIP_BASE <= addr) && + (addr < (APLIC_SETIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIP_BASE ) >>2 ; +return riscv_aplic_read_pending_word (aplic, word); + }else if (addr ==APLIC_SETIPNUM ) { +return 0 ; + }else if ((APLIC_CLRIP_BASE <= addr) && + (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_CLRIP_BASE ) >>2 ; +return riscv_aplic_read_input_word (aplic, word); + }else if (addr ==APLIC_CLRIPNUM ) { +return 0 ; + }else if ((APLIC_SETIE_BASE <= addr) && + (addr < (APLIC_SETIE_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIE_BASE ) >>2 ; +return riscv_aplic_read_enabled_word (aplic, word); + }else if (addr ==APLIC_SETIENUM ) { +return 0 ; + }else if ((APLIC_CLRIE_BASE <= addr) && + (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words *4 ))) { +return 0 ; + }else if (addr ==APLIC_CLRIENUM ) { +return 0 ; + }else if (addr ==APLIC_SETIPNUM_LE ) { +return 0 ; + }else if (addr ==APLIC_SETIPNUM_BE ) { +return 0 ; + }else if (addr ==APLIC_GENMSI ) { +return (aplic->msimode) ? aplic->genmsi :0 ; + }else if ((APLIC_TARGET_BASE <= addr) && + (addr < (APLIC_TARGET_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_TARGET_BASE ) >>2 ) +1 ; +return aplic->target[irq]; + } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) && + (addr < (APLIC_IDC_BASE + aplic->num_harts *
APLIC_IDC_SIZE))) {hw/intc/riscv_aplic.c new file +134
-0+ idc = (addr - APLIC_IDC_BASE) / APLIC_IDC_SIZE; + switch (addr - (APLIC_IDC_BASE + idc * APLIC_IDC_SIZE)) { + case APLIC_IDC_IDELIVERY: + return aplic->idelivery[idc]; + case APLIC_IDC_IFORCE: + return aplic->iforce[idc]; + case APLIC_IDC_ITHRESHOLD: + return aplic->ithreshold[idc]; + case APLIC_IDC_TOPI: + return riscv_aplic_idc_topi(aplic, idc); + case APLIC_IDC_CLAIMI: + return riscv_aplic_idc_claimi(aplic, idc); + default: + goto err; + }; + } + +err: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Invalid register read 0x%" HWADDR_PRIx "\n", + __func__, addr); + return 0; +} + +static void riscv_aplic_write(void *opaque, hwaddr addr, uint64_t value, + unsigned size) +{ + RISCVAPLICState *aplic = opaque; + uint32_t irq, word, idc = UINT32_MAX; + + /* Writes must be 4 byte words */ + if ((addr & 0x3) != 0) { + goto err; + } + + if (addr == APLIC_DOMAINCFG) { + /* Only IE bit writeable at the moment */ + value &= APLIC_DOMAINCFG_IE; + aplic->domaincfg = value; + } else if ((APLIC_SOURCECFG_BASE <= addr) && + (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs - 1) * 4))) { + irq = ((addr - APLIC_SOURCECFG_BASE) >> 2) + 1; + if (!aplic->num_children && (value & APLIC_SOURCECFG_D)) { + value = 0; + } + if (value & APLIC_SOURCECFG_D) { + value &= (APLIC_SOURCECFG_D | APLIC_SOURCECFG_CHILDIDX_MASK); + } else { + value &= (APLIC_SOURCECFG_D | APLIC_SOURCECFG_SM_MASK); + } + aplic->sourcecfg[irq] = value; + if ((aplic->sourcecfg[irq] & APLIC_SOURCECFG_D) || + (aplic->sourcecfg[irq] == 0)) { + riscv_aplic_set_pending_raw(aplic, irq, false); + riscv_aplic_set_enabled_raw(aplic, irq, false); + } + } else if (aplic->mmode && aplic->msimode && + (addr == APLIC_MMSICFGADDR)) { + if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->mmsicfgaddr = value; + } + } else if (aplic->mmode && aplic->msimode && + (addr == APLIC_MMSICFGADDRH)) { + if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->mmsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK; + } + } else if (aplic->mmode && aplic->msimode && + (addr == APLIC_SMSICFGADDR)) { + /* Registers SMSICFGADDR and SMSICFGADDRH are implemented only if: + * (a) the interrupt domain is at machine level + * (b) the domain’s harts implement supervisor mode + * (c) the domain has one or more child supervisor-level domains + * that support MSI delivery mode (domaincfg.DM is not read- + * only zero in at least one of the supervisor-level child + * domains). + */ + if (aplic->num_children && + !(aplic->smsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->smsicfgaddr = value; + } + } else if (aplic->mmode && aplic->msimode && + (addr == APLIC_SMSICFGADDRH)) { + if (aplic->num_children && + !(aplic->smsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->smsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK; + } + } else if ((APLIC_SETIP_BASE <= addr) && + (addr < (APLIC_SETIP_BASE + aplic->bitfield_words * 4))) { + word = (addr - APLIC_SETIP_BASE) >> 2; + riscv_aplic_set_pending_word(aplic, word, value, true); + } else if (addr == APLIC_SETIPNUM) { + riscv_aplic_set_pending(aplic, value, true); + } else if ((APLIC_CLRIP_BASE <= addr) && + (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words * 4))) { + word = (addr - APLIC_CLRIP_BASE) >> 2; + riscv_aplic_set_pending_word(aplic, word, value, false); + } else if (addr == APLIC_CLRIPNUM) { + riscv_aplic_set_pending(aplic, value, false); + } else if ((APLIC_SETIE_BASE <= addr) && + (addr < (APLIC_SETIE_BASE + aplic->bitfield_words * 4))) { + word = (addr - APLIC_SETIE_BASE) >> 2; + riscv_aplic_set_enabled_word(aplic, word, value, true); + } else if (addr == APLIC_SETIENUM) { + riscv_aplic_set_enabled(aplic, value, true); + } else if ((APLIC_CLRIE_BASE <= addr) && + (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words * 4))) { + word = (addr - APLIC_CLRIE_BASE) >> 2; + riscv_aplic_set_enabled_word(aplic, word, value, false); + } else if (addr == APLIC_CLRIENUM) { + riscv_aplic_set_enabled(aplic, value, false); + } else if (addr == APLIC_SETIPNUM_LE) { + riscv_aplic_set_pending(aplic, value, true); + } else if (addr == APLIC_SETIPNUM_BE) { + riscv_aplic_set_pending(aplic, bswap32(value), true); + } else if (addr == APLIC_GENMSI) { + if (aplic->msimode) { + aplic->genmsi = value & ~(APLIC_TARGET_GUEST_IDX_MASK << + APLIC_TARGET_GUEST_IDX_SHIFT); + riscv_aplic_msi_send(aplic, + value >> APLIC_TARGET_HART_IDX_SHIFT, + 0, + value & APLIC_TARGET_EIID_MASK); + } + } else if ((APLIC_TARGET_BASE <= addr) && + (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) { + irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1; + if (aplic->msimode) { + aplic->target[irq] = value; + } else { + aplic->target[irq] = (value & ~APLIC_TARGET_IPRIO_MASK) | + (value & aplic->iprio_mask); + } + } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) && + (addr < (APLIC_IDC_BASE + aplic->num_harts *
APLIC_IDC_SIZE))) {hw/intc/riscv_aplic.c new file +64
-0+ idc = (addr -APLIC_IDC_BASE ) /APLIC_IDC_SIZE ; +switch (addr - (APLIC_IDC_BASE + idc *APLIC_IDC_SIZE )) { +case APLIC_IDC_IDELIVERY : + aplic->idelivery[idc] = value &0x1 ; +break ; +case APLIC_IDC_IFORCE : + aplic->iforce[idc] = value &0x1 ; +break ; +case APLIC_IDC_ITHRESHOLD : + aplic->ithreshold[idc] = value & aplic->iprio_mask; +break ; +default : + goto err; + }; + }else { + goto err; + } + +if (aplic->msimode) { +for (irq =1 ; irq < aplic->num_irqs; irq++) { +riscv_aplic_msi_irq_update (aplic, irq); + } + }else { +if (idc ==UINT32_MAX ) { +for (idc =0 ; idc < aplic->num_harts; idc++) { +riscv_aplic_idc_update (aplic, idc); + } + }else { +riscv_aplic_idc_update (aplic, idc); + } + } + +return ; + +err: +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Invalid register write 0x%" HWADDR_PRIx"\n" , + __func__, addr); +} + +static const MemoryRegionOps riscv_aplic_ops = { + .read = riscv_aplic_read, + .write = riscv_aplic_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .valid = { + .min_access_size =4 , + .max_access_size =4 + } +}; + +static void riscv_aplic_realize (DeviceState *dev,Error **errp) +{ +uint32_t i; +RISCVAPLICState *aplic =RISCV_APLIC (dev); + + aplic->bitfield_words = (aplic->num_irqs +31 ) >>5 ; + aplic->sourcecfg =g_new0 (uint32_t, aplic->num_irqs); + aplic->state =g_new (uint32_t, aplic->num_irqs); + aplic->target =g_new0 (uint32_t, aplic->num_irqs); + aplic->idelivery =g_new0 (uint32_t, aplic->num_harts); + aplic->iforce =g_new0 (uint32_t, aplic->num_harts); + aplic->ithreshold =g_new0 (uint32_t, aplic->num_harts); + + memory_region_init_io(&aplic->mmio, OBJECT(dev), &riscv_aplic_ops,aplic,
hw/intc/riscv_aplic.c new file +13
-0+ TYPE_RISCV_APLIC, aplic->aperture_size); +sysbus_init_mmio (SYS_BUS_DEVICE (dev), &aplic->mmio); + +/* Only root APLICs have hardware IRQ lines. All non-root APLICs +* have IRQ lines delegated by their parent APLIC. +*/ +if (!aplic->parent) { +qdev_init_gpio_in (dev, riscv_aplic_request, aplic->num_irqs); + } + +/* Create output IRQ lines for non-MSI mode */ +if (!aplic->msimode) { + aplic->external_irqs = g_malloc(sizeof(qemu_irq) *
aplic->num_harts);hw/intc/riscv_aplic.c new file +5
-0+qdev_init_gpio_out (dev, aplic->external_irqs, aplic->num_harts); + +/* Claim the CPU interrupt to be triggered by this APLIC */ +for (i =0 ; i < aplic->num_harts; i++) { + RISCVCPU *cpu = RISCV_CPU(qemu_get_cpu(aplic->hartid_base +i));
hw/intc/riscv_aplic.c new file +14
-0+if (riscv_cpu_claim_interrupts (cpu, + (aplic->mmode) ?MIP_MEIP :MIP_SEIP ) <0 ) { +error_report ("%s already claimed" , + (aplic->mmode) ?"MEIP" :"SEIP" ); +exit (1 ); + } + } + } + + msi_nonbroken = true; +} + +static Property riscv_aplic_properties[] = { + DEFINE_PROP_UINT32("aperture-size", RISCVAPLICState, aperture_size,0),
hw/intc/riscv_aplic.c new file +123
-0+DEFINE_PROP_UINT32 ("hartid-base" , RISCVAPLICState, hartid_base,0 ), +DEFINE_PROP_UINT32 ("num-harts" , RISCVAPLICState, num_harts,0 ), +DEFINE_PROP_UINT32 ("iprio-mask" , RISCVAPLICState, iprio_mask,0 ), +DEFINE_PROP_UINT32 ("num-irqs" , RISCVAPLICState, num_irqs,0 ), +DEFINE_PROP_BOOL ("msimode" , RISCVAPLICState, msimode,0 ), +DEFINE_PROP_BOOL ("mmode" , RISCVAPLICState, mmode,0 ), + DEFINE_PROP_END_OF_LIST(), +}; + +static const VMStateDescription vmstate_riscv_aplic = { + .name ="riscv_aplic" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT32 (domaincfg, RISCVAPLICState), +VMSTATE_UINT32 (mmsicfgaddr, RISCVAPLICState), +VMSTATE_UINT32 (mmsicfgaddrH, RISCVAPLICState), +VMSTATE_UINT32 (smsicfgaddr, RISCVAPLICState), +VMSTATE_UINT32 (smsicfgaddrH, RISCVAPLICState), +VMSTATE_UINT32 (genmsi, RISCVAPLICState), +VMSTATE_VARRAY_UINT32 (sourcecfg, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (state, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (target, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (idelivery, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (iforce, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (ithreshold, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_END_OF_LIST () + } +}; + +static void riscv_aplic_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + +device_class_set_props (dc, riscv_aplic_properties); + dc->realize = riscv_aplic_realize; + dc->vmsd = &vmstate_riscv_aplic; +} + +static const TypeInfo riscv_aplic_info = { + .name =TYPE_RISCV_APLIC , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (RISCVAPLICState), + .class_init = riscv_aplic_class_init, +}; + +static void riscv_aplic_register_types (void ) +{ +type_register_static (&riscv_aplic_info); +} + +type_init (riscv_aplic_register_types) + +/* +* Add a APLIC device to another APLIC device as child for +* interrupt delegation. +*/ +void riscv_aplic_add_child(DeviceState *parent, DeviceState *child) +{ +RISCVAPLICState *caplic, *paplic; + +assert (parent && child); + caplic =RISCV_APLIC (child); + paplic =RISCV_APLIC (parent); + +assert (paplic->num_irqs == caplic->num_irqs); +assert (paplic->num_children <=QEMU_APLIC_MAX_CHILDREN ); + + caplic->parent = paplic; + paplic->children[paplic->num_children] = caplic; + paplic->num_children++; +} + +/* +* Create APLIC device. +*/ +DeviceState *riscv_aplic_create (hwaddr addr,hwaddr size, +uint32_t hartid_base,uint32_t num_harts,uint32_t num_sources, +uint32_t iprio_bits,bool msimode,bool mmode,DeviceState *parent) +{ +DeviceState *dev =qdev_new (TYPE_RISCV_APLIC ); +uint32_t i; + +assert (num_harts <APLIC_MAX_IDC ); +assert ((APLIC_IDC_BASE + (num_harts *APLIC_IDC_SIZE )) <= size); +assert (num_sources <APLIC_MAX_SOURCE ); +assert (APLIC_MIN_IPRIO_BITS <= iprio_bits); +assert (iprio_bits <=APLIC_MAX_IPRIO_BITS ); + +qdev_prop_set_uint32 (dev,"aperture-size" , size); +qdev_prop_set_uint32 (dev,"hartid-base" , hartid_base); +qdev_prop_set_uint32 (dev,"num-harts" , num_harts); +qdev_prop_set_uint32 (dev,"iprio-mask" , ((1U << iprio_bits) -1 )); +qdev_prop_set_uint32 (dev,"num-irqs" , num_sources +1 ); +qdev_prop_set_bit (dev,"msimode" , msimode); +qdev_prop_set_bit (dev,"mmode" , mmode); + +sysbus_realize_and_unref (SYS_BUS_DEVICE (dev), &error_fatal); +sysbus_mmio_map (SYS_BUS_DEVICE (dev),0 , addr); + +if (parent) { +riscv_aplic_add_child (parent, dev); + } + +if (!msimode) { +for (i =0 ; i < num_harts; i++) { +CPUState *cpu =qemu_get_cpu (hartid_base + i); + + qdev_connect_gpio_out_named(dev, NULL, i, + qdev_get_gpio_in(DEVICE(cpu), + (mmode) ? IRQ_M_EXT :
IRQ_S_EXT));hw/intc/riscv_aplic.c new file +5
-0+ } + } + +return dev; +}include/hw/intc/riscv_aplic.h new file +12
-0@@ -0,0 +1,79 @@ +/* +* RISC-V APLIC (Advanced Platform Level Interrupt Controller) interface +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */include/hw/intc/riscv_aplic.h new file +62
-1+ +#ifndef HW_RISCV_APLIC_H +#define HW_RISCV_APLIC_H + +#include "hw/sysbus.h" +#include "qom/object.h" + +#define TYPE_RISCV_APLIC "riscv.aplic" + +typedef struct RISCVAPLICState RISCVAPLICState ; +DECLARE_INSTANCE_CHECKER(RISCVAPLICState, RISCV_APLIC, TYPE_RISCV_APLIC) + +#define APLIC_MIN_SIZE 0x4000 +#define APLIC_SIZE_ALIGN (__x) (((__x) + (APLIC_MIN_SIZE - 1)) & \ + ~(APLIC_MIN_SIZE - 1)) +#define APLIC_SIZE (__num_harts) (APLIC_MIN_SIZE + \ + APLIC_SIZE_ALIGN(32 * (__num_harts))) + +struct RISCVAPLICState { +/*< private >*/ +SysBusDevice parent_obj; +qemu_irq *external_irqs; + +/*< public >*/ +MemoryRegion mmio; +uint32_t bitfield_words; +uint32_t domaincfg; +uint32_t mmsicfgaddr; +uint32_t mmsicfgaddrH; +uint32_t smsicfgaddr; +uint32_t smsicfgaddrH; +uint32_t genmsi; +uint32_t *sourcecfg; +uint32_t *state; +uint32_t *target; +uint32_t *idelivery; +uint32_t *iforce; +uint32_t *ithreshold; + +/* topology */ +#define QEMU_APLIC_MAX_CHILDREN 16 +struct RISCVAPLICState *parent; +struct RISCVAPLICState *children[QEMU_APLIC_MAX_CHILDREN ]; +uint16_t num_children; + +/* config */ +uint32_t aperture_size; +uint32_t hartid_base; +uint32_t num_harts; +uint32_t iprio_mask; +uint32_t num_irqs; +bool msimode; +bool mmode; +}; + +void riscv_aplic_add_child (DeviceState *parent,DeviceState *child); + +DeviceState *riscv_aplic_create (hwaddr addr,hwaddr size, +uint32_t hartid_base,uint32_t num_harts,uint32_t num_sources, +uint32_t iprio_bits,bool msimode,bool mmode,DeviceState *parent); + +#endif --2.25.1
Re: [PATCH v6 18/23] hw/intc: Add RISC-V AIA APLIC device emulation
Quoted 460 lines · Frank Chang
Anup Patel <[email protected]> 於 2021年12月30日 週四 下午8:55寫道:
Quoted 444 lines
From: Anup Patel <[email protected]>
The RISC-V AIA (Advanced Interrupt Architecture) defines a new interrupt controller for wired interrupts called APLIC (Advanced Platform Level Interrupt Controller). The APLIC is capabable of forwarding wired interupts to RISC-V HARTs directly or as MSIs (Message Signaled Interupts).
This patch adds device emulation for RISC-V AIA APLIC.
Signed-off-by Anup Patel <[email protected]>Signed-off-by Anup Patel <[email protected]>--- hw/intc/Kconfig | 3 + hw/intc/meson.build | 1 + hw/intc/riscv_aplic.c | 970 ++++++++++++++++++++++++++++++++++ include/hw/intc/riscv_aplic.h | 79 +++ 4 files changed, 1053 insertions(+) create mode 100644 hw/intc/riscv_aplic.c create mode 100644 include/hw/intc/riscv_aplic.hhw/intc/Kconfig +3
-0@@ -70,6 +70,9 @@ config LOONGSON_LIOINTC config RISCV_ACLINT bool +config RISCV_APLIC + bool + config SIFIVE_PLIC boolhw/intc/meson.build +1
-0@@ -50,6 +50,7 @@ specific_ss.add(when: 'CONFIG_S390_FLIC', if_true: files('s390_flic.c')) specific_ss.add(when: 'CONFIG_S390_FLIC_KVM', if_true: files('s390_flic_kvm.c')) specific_ss.add(when: 'CONFIG_SH_INTC', if_true: files('sh_intc.c')) specific_ss.add(when: 'CONFIG_RISCV_ACLINT', if_true: files('riscv_aclint.c')) +specific_ss.add(when: 'CONFIG_RISCV_APLIC', if_true: files('riscv_aplic.c')) specific_ss.add(when: 'CONFIG_SIFIVE_PLIC', if_true: files('sifive_plic.c')) specific_ss.add(when: 'CONFIG_XICS', if_true: files('xics.c')) specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_XICS'],hw/intc/riscv_aplic.c new file +390
-0@@ -0,0 +1,970 @@ +/* +* RISC-V APLIC (Advanced Platform Level Interrupt Controller) +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "qemu/module.h" +#include "qemu/error-report.h" +#include "qemu/bswap.h" +#include "exec/address-spaces.h" +#include "hw/sysbus.h" +#include "hw/pci/msi.h" +#include "hw/boards.h" +#include "hw/qdev-properties.h" +#include "hw/intc/riscv_aplic.h" +#include "hw/irq.h" +#include "target/riscv/cpu.h" +#include "sysemu/sysemu.h" +#include "migration/vmstate.h" + +#define APLIC_MAX_IDC (1UL << 14) +#define APLIC_MAX_SOURCE 1024 +#define APLIC_MIN_IPRIO_BITS 1 +#define APLIC_MAX_IPRIO_BITS 8 +#define APLIC_MAX_CHILDREN 1024 + +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_DOMAINCFG_IE (1 << 8) +#define APLIC_DOMAINCFG_DM (1 << 2) +#define APLIC_DOMAINCFG_BE (1 << 0) + +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_D (1 << 10) +#define APLIC_SOURCECFG_CHILDIDX_MASK 0x000003ff +#define APLIC_SOURCECFG_SM_MASK 0x00000007 +#define APLIC_SOURCECFG_SM_INACTIVE 0x0 +#define APLIC_SOURCECFG_SM_DETACH 0x1 +#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4 +#define APLIC_SOURCECFG_SM_EDGE_FALL 0x5 +#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6 +#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 + +#define APLIC_MMSICFGADDR 0x1bc0 +#define APLIC_MMSICFGADDRH 0x1bc4 +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#define APLIC_xMSICFGADDRH_L (1UL << 31) +#define APLIC_xMSICFGADDRH_HHXS_MASK 0x1f +#define APLIC_xMSICFGADDRH_HHXS_SHIFT 24 +#define APLIC_xMSICFGADDRH_LHXS_MASK 0x7 +#define APLIC_xMSICFGADDRH_LHXS_SHIFT 20 +#define APLIC_xMSICFGADDRH_HHXW_MASK 0x7 +#define APLIC_xMSICFGADDRH_HHXW_SHIFT 16 +#define APLIC_xMSICFGADDRH_LHXW_MASK 0xf +#define APLIC_xMSICFGADDRH_LHXW_SHIFT 12 +#define APLIC_xMSICFGADDRH_BAPPN_MASK 0xfff + +#define APLIC_xMSICFGADDR_PPN_SHIFT 12 + +#define APLIC_xMSICFGADDR_PPN_HART (__lhxs) \ + ((1UL << (__lhxs)) - 1) + +#define APLIC_xMSICFGADDR_PPN_LHX_MASK (__lhxw) \ + ((1UL << (__lhxw)) - 1) +#define APLIC_xMSICFGADDR_PPN_LHX_SHIFT (__lhxs) \ + ((__lhxs)) +#define APLIC_xMSICFGADDR_PPN_LHX (__lhxw, __lhxs) \ + (APLIC_xMSICFGADDR_PPN_LHX_MASK(__lhxw) << \ + APLIC_xMSICFGADDR_PPN_LHX_SHIFT(__lhxs)) + +#define APLIC_xMSICFGADDR_PPN_HHX_MASK (__hhxw) \ + ((1UL << (__hhxw)) - 1) +#define APLIC_xMSICFGADDR_PPN_HHX_SHIFT (__hhxs) \ + ((__hhxs) + APLIC_xMSICFGADDR_PPN_SHIFT) +#define APLIC_xMSICFGADDR_PPN_HHX (__hhxw, __hhxs) \ + (APLIC_xMSICFGADDR_PPN_HHX_MASK(__hhxw) << \ + APLIC_xMSICFGADDR_PPN_HHX_SHIFT(__hhxs)) + +#define APLIC_xMSICFGADDRH_VALID_MASK \ + (APLIC_xMSICFGADDRH_L | \ + (APLIC_xMSICFGADDRH_HHXS_MASK << APLIC_xMSICFGADDRH_HHXS_SHIFT) | \ + (APLIC_xMSICFGADDRH_LHXS_MASK << APLIC_xMSICFGADDRH_LHXS_SHIFT) | \ + (APLIC_xMSICFGADDRH_HHXW_MASK << APLIC_xMSICFGADDRH_HHXW_SHIFT) | \ + (APLIC_xMSICFGADDRH_LHXW_MASK << APLIC_xMSICFGADDRH_LHXW_SHIFT) | \ + APLIC_xMSICFGADDRH_BAPPN_MASK) + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIENUM 0x1fdc + +#define APLIC_SETIPNUM_LE 0x2000 +#define APLIC_SETIPNUM_BE 0x2004 + +#define APLIC_ISTATE_PENDING (1U << 0) +#define APLIC_ISTATE_ENABLED (1U << 1) +#define APLIC_ISTATE_ENPEND (APLIC_ISTATE_ENABLED | \ + APLIC_ISTATE_PENDING) +#define APLIC_ISTATE_INPUT (1U << 8) + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_HART_IDX_SHIFT 18 +#define APLIC_TARGET_HART_IDX_MASK 0x3fff +#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_GUEST_IDX_MASK 0x3f +#define APLIC_TARGET_IPRIO_MASK 0xff +#define APLIC_TARGET_EIID_MASK 0x7ff + +#define APLIC_IDC_BASE 0x4000 +#define APLIC_IDC_SIZE 32 + +#define APLIC_IDC_IDELIVERY 0x00 + +#define APLIC_IDC_IFORCE 0x04 + +#define APLIC_IDC_ITHRESHOLD 0x08 + +#define APLIC_IDC_TOPI 0x18 +#define APLIC_IDC_TOPI_ID_SHIFT 16 +#define APLIC_IDC_TOPI_ID_MASK 0x3ff +#define APLIC_IDC_TOPI_PRIO_MASK 0xff + +#define APLIC_IDC_CLAIMI 0x1c + +static uint32_t riscv_aplic_read_input_word (RISCVAPLICState *aplic, +uint32_t word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_INPUT ) ?1 :0 ) << i; + } + +return ret; +} + +static uint32_t riscv_aplic_read_pending_word (RISCVAPLICState *aplic, +uint32_t word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_PENDING ) ?1 :0 ) << i; + } + +return ret; +} + +static void riscv_aplic_set_pending_raw (RISCVAPLICState *aplic, +uint32_t irq,bool pending) +{ +if (pending) { + aplic->state[irq] |=APLIC_ISTATE_PENDING ; + }else { + aplic->state[irq] &= ~APLIC_ISTATE_PENDING ; + } +} + +static void riscv_aplic_set_pending (RISCVAPLICState *aplic, +uint32_t irq,bool pending) +{ +uint32_t sourcecfg, sm; + +if ((irq <=0 ) || (aplic->num_irqs <= irq)) { +return ; + } + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { +return ; + } + + sm = sourcecfg &APLIC_SOURCECFG_SM_MASK ; +if ((sm ==APLIC_SOURCECFG_SM_INACTIVE ) || + (!aplic->msimode && ((sm ==APLIC_SOURCECFG_SM_LEVEL_HIGH ) || + (sm ==APLIC_SOURCECFG_SM_LEVEL_LOW )))) { +return ; + } + +riscv_aplic_set_pending_raw (aplic, irq, pending); +} + +static void riscv_aplic_set_pending_word (RISCVAPLICState *aplic, +uint32_t word,uint32_t value, +bool pending) +{ +uint32_t i, irq; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + +if (value & (1U << i)) { +riscv_aplic_set_pending (aplic, irq, pending); + } + } +} + +static uint32_t riscv_aplic_read_enabled_word (RISCVAPLICState *aplic, +int word) +{ +uint32_t i, irq, ret =0 ; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + + ret |= ((aplic->state[irq] &APLIC_ISTATE_ENABLED ) ?1 :0 ) << i; + } + +return ret; +} + +static void riscv_aplic_set_enabled_raw (RISCVAPLICState *aplic, +uint32_t irq,bool enabled) +{ +if (enabled) { + aplic->state[irq] |=APLIC_ISTATE_ENABLED ; + }else { + aplic->state[irq] &= ~APLIC_ISTATE_ENABLED ; + } +} + +static void riscv_aplic_set_enabled (RISCVAPLICState *aplic, +uint32_t irq,bool enabled) +{ +uint32_t sourcecfg, sm; + +if ((irq <=0 ) || (aplic->num_irqs <= irq)) { +return ; + } + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { +return ; + } + + sm = sourcecfg &APLIC_SOURCECFG_SM_MASK ; +if (sm ==APLIC_SOURCECFG_SM_INACTIVE ) { +return ; + } + +riscv_aplic_set_enabled_raw (aplic, irq, enabled); +} + +static void riscv_aplic_set_enabled_word (RISCVAPLICState *aplic, +uint32_t word,uint32_t value, +bool enabled) +{ +uint32_t i, irq; + +for (i =0 ; i <32 ; i++) { + irq = word *32 + i; +if (!irq || aplic->num_irqs <= irq) { +continue ; + } + +if (value & (1U << i)) { +riscv_aplic_set_enabled (aplic, irq, enabled); + } + } +} + +static void riscv_aplic_msi_send (RISCVAPLICState *aplic, +uint32_t hart_idx,uint32_t guest_idx, +uint32_t eiid) +{ +uint64_t addr; +MemTxResult result; +RISCVAPLICState *aplic_m; +uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH; + + aplic_m = aplic; +while (aplic_m && !aplic_m->mmode) { + aplic_m = aplic_m->parent; + } +if (!aplic_m) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: m-level APLIC not found\n" , + __func__); +return ; + } + +if (aplic->mmode) { + msicfgaddr = aplic_m->mmsicfgaddr; + msicfgaddrH = aplic_m->mmsicfgaddrH; + }else { + msicfgaddr = aplic_m->smsicfgaddr; + msicfgaddrH = aplic_m->smsicfgaddrH; + } + + lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) & + APLIC_xMSICFGADDRH_LHXS_MASK; + lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) & + APLIC_xMSICFGADDRH_LHXW_MASK; + hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) & + APLIC_xMSICFGADDRH_HHXS_MASK; + hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) & + APLIC_xMSICFGADDRH_HHXW_MASK; + + group_idx = hart_idx >> lhxw; + hart_idx &=APLIC_xMSICFGADDR_PPN_LHX_MASK (lhxw); + + addr = msicfgaddr; + addr |= ((uint64_t )(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK)) <<32 ; + addr |= ((uint64_t )(group_idx &APLIC_xMSICFGADDR_PPN_HHX_MASK (hhxw))) << +APLIC_xMSICFGADDR_PPN_HHX_SHIFT (hhxs); + addr |= ((uint64_t )(hart_idx &APLIC_xMSICFGADDR_PPN_LHX_MASK (lhxw))) << +APLIC_xMSICFGADDR_PPN_LHX_SHIFT (lhxs); + addr |= (uint64_t )(guest_idx &APLIC_xMSICFGADDR_PPN_HART (lhxs)); + addr <<= APLIC_xMSICFGADDR_PPN_SHIFT; + +address_space_stl_le (&address_space_memory, addr, + eiid,MEMTXATTRS_UNSPECIFIED , &result); +if (result !=MEMTX_OK ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: MSI write failed for " +"hart_index=%d guest_index=%d eiid=%d\n" , + __func__, hart_idx, guest_idx, eiid); + } +} + +static void riscv_aplic_msi_irq_update (RISCVAPLICState *aplic,uint32_t irq) +{ +uint32_t hart_idx, guest_idx, eiid; + +if (!aplic->msimode || (aplic->num_irqs <= irq) || + !(aplic->domaincfg &APLIC_DOMAINCFG_IE )) { +return ; + } + +if ((aplic->state[irq] &APLIC_ISTATE_ENPEND ) !=APLIC_ISTATE_ENPEND ) { +return ; + } + +riscv_aplic_set_pending_raw (aplic, irq, false); + + hart_idx = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + hart_idx &=APLIC_TARGET_HART_IDX_MASK ; +if (aplic->mmode) { +/* M-level APLIC ignores guest_index */ + guest_idx =0 ; + }else { + guest_idx = aplic->target[irq] >>APLIC_TARGET_GUEST_IDX_SHIFT ; + guest_idx &=APLIC_TARGET_GUEST_IDX_MASK ; + } + eiid = aplic->target[irq] &APLIC_TARGET_EIID_MASK ; +riscv_aplic_msi_send (aplic, hart_idx, guest_idx, eiid); +} + +static uint32_t riscv_aplic_idc_topi (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t best_irq, best_iprio;hw/intc/riscv_aplic.c line 400
Quoted 10 lines
hw/intc/riscv_aplic.c new file +10
-0+uint32_t irq, iprio, ihartidx, ithres; + +if (aplic->num_harts <= idc) { +return 0 ; + } + + ithres = aplic->ithreshold[idc]; +if (!ithres) { +return 0 ; + }Hi Anup,
Quoted 8 lines
I'm curious why topi register returns 0 when ithreshold register is 0.
According to spec:
A read of topi returns zero either if no interrupt that is targeted to this hart is both pending and enabled, or if ithreshold is not zero and no pending-and-enabled interrupt targeted to this hart has a priority number less than the value of ithreshold.Am I missing anything?
Your observation is correct. Thanks for catching this.
I had referred to very old draft when implementing APLIC IDC structures. I will fix this in next patch revision.
Regards,
Anup
Quoted 663 lines
Regards,
Frank ChangQuoted 659 lines
hw/intc/riscv_aplic.c new file +570
-0+ + best_irq = best_iprio =UINT32_MAX ; +for (irq =1 ; irq < aplic->num_irqs; irq++) { +if ((aplic->state[irq] &APLIC_ISTATE_ENPEND ) != +APLIC_ISTATE_ENPEND ) { +continue ; + } + + ihartidx = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + ihartidx &=APLIC_TARGET_HART_IDX_MASK ; +if (ihartidx != idc) { +continue ; + } + + iprio = aplic->target[irq] & aplic->iprio_mask; +if (iprio >= ithres) { +continue ; + } + +if (iprio < best_iprio) { + best_irq = irq; + best_iprio = iprio; + } + } + +if (best_irq < aplic->num_irqs && best_iprio <= aplic->iprio_mask) { +return (best_irq <<APLIC_IDC_TOPI_ID_SHIFT ) | best_iprio; + } + +return 0 ; +} + +static void riscv_aplic_idc_update (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t topi; + +if (aplic->msimode || aplic->num_harts <= idc) { +return ; + } + + topi =riscv_aplic_idc_topi (aplic, idc); +if ((aplic->domaincfg &APLIC_DOMAINCFG_IE ) && + aplic->idelivery[idc] && + (aplic->iforce[idc] || topi)) { +qemu_irq_raise (aplic->external_irqs[idc]); + }else { +qemu_irq_lower (aplic->external_irqs[idc]); + } +} + +static uint32_t riscv_aplic_idc_claimi (RISCVAPLICState *aplic,uint32_t idc) +{ +uint32_t irq, state, sm, topi =riscv_aplic_idc_topi (aplic, idc); + +if (!topi) { + aplic->iforce[idc] =0 ; +return 0 ; + } + + irq = (topi >>APLIC_IDC_TOPI_ID_SHIFT ) &APLIC_IDC_TOPI_ID_MASK ; + sm = aplic->sourcecfg[irq] &APLIC_SOURCECFG_SM_MASK ; + state = aplic->state[irq]; +riscv_aplic_set_pending_raw (aplic, irq, false); +if ((sm ==APLIC_SOURCECFG_SM_LEVEL_HIGH ) && + (state &APLIC_ISTATE_INPUT )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + }else if ((sm ==APLIC_SOURCECFG_SM_LEVEL_LOW ) && + !(state &APLIC_ISTATE_INPUT )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + } +riscv_aplic_idc_update (aplic, idc); + +return topi; +} + +static void riscv_aplic_request (void *opaque,int irq,int level) +{ +bool update = false; +RISCVAPLICState *aplic = opaque; +uint32_t sourcecfg, childidx, state, idc; + +assert ((0 < irq) && (irq < aplic->num_irqs)); + + sourcecfg = aplic->sourcecfg[irq]; +if (sourcecfg &APLIC_SOURCECFG_D ) { + childidx = sourcecfg &APLIC_SOURCECFG_CHILDIDX_MASK ; +if (childidx < aplic->num_children) { +riscv_aplic_request (aplic->children[childidx], irq, level); + } +return ; + } + + state = aplic->state[irq]; +switch (sourcecfg &APLIC_SOURCECFG_SM_MASK ) { +case APLIC_SOURCECFG_SM_EDGE_RISE : +if ((level >0 ) && !(state &APLIC_ISTATE_INPUT ) && + !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_EDGE_FALL : +if ((level <=0 ) && (state &APLIC_ISTATE_INPUT ) && + !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_LEVEL_HIGH : +if ((level >0 ) && !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +case APLIC_SOURCECFG_SM_LEVEL_LOW : +if ((level <=0 ) && !(state &APLIC_ISTATE_PENDING )) { +riscv_aplic_set_pending_raw (aplic, irq, true); + update = true; + } +break ; +default : +break ; + } + +if (level <=0 ) { + aplic->state[irq] &= ~APLIC_ISTATE_INPUT ; + }else { + aplic->state[irq] |=APLIC_ISTATE_INPUT ; + } + +if (update) { +if (aplic->msimode) { +riscv_aplic_msi_irq_update (aplic, irq); + }else { + idc = aplic->target[irq] >>APLIC_TARGET_HART_IDX_SHIFT ; + idc &=APLIC_TARGET_HART_IDX_MASK ; +riscv_aplic_idc_update (aplic, idc); + } + } +} + +static uint64_t riscv_aplic_read (void *opaque,hwaddr addr,unsigned size) +{ +uint32_t irq, word, idc; +RISCVAPLICState *aplic = opaque; + +/* Reads must be 4 byte words */ +if ((addr &0x3 ) !=0 ) { + goto err; + } + +if (addr ==APLIC_DOMAINCFG ) { +return aplic->domaincfg | (aplic->msimode ?APLIC_DOMAINCFG_DM :0 ); + }else if ((APLIC_SOURCECFG_BASE <= addr) && + (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_SOURCECFG_BASE ) >>2 ) +1 ; +return aplic->sourcecfg[irq]; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDR )) { +return aplic->mmsicfgaddr; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDRH )) { +return aplic->mmsicfgaddrH; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDR )) { +/* Registers SMSICFGADDR and SMSICFGADDRH are implemented only if: +* (a) the interrupt domain is at machine level +* (b) the domain’s harts implement supervisor mode +* (c) the domain has one or more child supervisor-level domains +* that support MSI delivery mode (domaincfg.DM is not read- +* only zero in at least one of the supervisor-level child +* domains). +*/ +return (aplic->num_children) ? aplic->smsicfgaddr :0 ; + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDRH )) { +return (aplic->num_children) ? aplic->smsicfgaddrH :0 ; + }else if ((APLIC_SETIP_BASE <= addr) && + (addr < (APLIC_SETIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIP_BASE ) >>2 ; +return riscv_aplic_read_pending_word (aplic, word); + }else if (addr ==APLIC_SETIPNUM ) { +return 0 ; + }else if ((APLIC_CLRIP_BASE <= addr) && + (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_CLRIP_BASE ) >>2 ; +return riscv_aplic_read_input_word (aplic, word); + }else if (addr ==APLIC_CLRIPNUM ) { +return 0 ; + }else if ((APLIC_SETIE_BASE <= addr) && + (addr < (APLIC_SETIE_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIE_BASE ) >>2 ; +return riscv_aplic_read_enabled_word (aplic, word); + }else if (addr ==APLIC_SETIENUM ) { +return 0 ; + }else if ((APLIC_CLRIE_BASE <= addr) && + (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words *4 ))) { +return 0 ; + }else if (addr ==APLIC_CLRIENUM ) { +return 0 ; + }else if (addr ==APLIC_SETIPNUM_LE ) { +return 0 ; + }else if (addr ==APLIC_SETIPNUM_BE ) { +return 0 ; + }else if (addr ==APLIC_GENMSI ) { +return (aplic->msimode) ? aplic->genmsi :0 ; + }else if ((APLIC_TARGET_BASE <= addr) && + (addr < (APLIC_TARGET_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_TARGET_BASE ) >>2 ) +1 ; +return aplic->target[irq]; + }else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) && + (addr < (APLIC_IDC_BASE + aplic->num_harts *APLIC_IDC_SIZE ))) { + idc = (addr -APLIC_IDC_BASE ) /APLIC_IDC_SIZE ; +switch (addr - (APLIC_IDC_BASE + idc *APLIC_IDC_SIZE )) { +case APLIC_IDC_IDELIVERY : +return aplic->idelivery[idc]; +case APLIC_IDC_IFORCE : +return aplic->iforce[idc]; +case APLIC_IDC_ITHRESHOLD : +return aplic->ithreshold[idc]; +case APLIC_IDC_TOPI : +return riscv_aplic_idc_topi (aplic, idc); +case APLIC_IDC_CLAIMI : +return riscv_aplic_idc_claimi (aplic, idc); +default : + goto err; + }; + } + +err: +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Invalid register read 0x%" HWADDR_PRIx"\n" , + __func__, addr); +return 0 ; +} + +static void riscv_aplic_write (void *opaque,hwaddr addr,uint64_t value, +unsigned size) +{ +RISCVAPLICState *aplic = opaque; +uint32_t irq, word, idc =UINT32_MAX ; + +/* Writes must be 4 byte words */ +if ((addr &0x3 ) !=0 ) { + goto err; + } + +if (addr ==APLIC_DOMAINCFG ) { +/* Only IE bit writeable at the moment */ + value &=APLIC_DOMAINCFG_IE ; + aplic->domaincfg = value; + }else if ((APLIC_SOURCECFG_BASE <= addr) && + (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_SOURCECFG_BASE ) >>2 ) +1 ; +if (!aplic->num_children && (value &APLIC_SOURCECFG_D )) { + value =0 ; + } +if (value &APLIC_SOURCECFG_D ) { + value &= (APLIC_SOURCECFG_D |APLIC_SOURCECFG_CHILDIDX_MASK ); + }else { + value &= (APLIC_SOURCECFG_D |APLIC_SOURCECFG_SM_MASK ); + } + aplic->sourcecfg[irq] = value; +if ((aplic->sourcecfg[irq] &APLIC_SOURCECFG_D ) || + (aplic->sourcecfg[irq] ==0 )) { +riscv_aplic_set_pending_raw (aplic, irq, false); +riscv_aplic_set_enabled_raw (aplic, irq, false); + } + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDR )) { +if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->mmsicfgaddr = value; + } + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_MMSICFGADDRH )) { +if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->mmsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK; + } + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDR )) { +/* Registers SMSICFGADDR and SMSICFGADDRH are implemented only if: +* (a) the interrupt domain is at machine level +* (b) the domain’s harts implement supervisor mode +* (c) the domain has one or more child supervisor-level domains +* that support MSI delivery mode (domaincfg.DM is not read- +* only zero in at least one of the supervisor-level child +* domains). +*/ +if (aplic->num_children && + !(aplic->smsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->smsicfgaddr = value; + } + }else if (aplic->mmode && aplic->msimode && + (addr ==APLIC_SMSICFGADDRH )) { +if (aplic->num_children && + !(aplic->smsicfgaddrH & APLIC_xMSICFGADDRH_L)) { + aplic->smsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK; + } + }else if ((APLIC_SETIP_BASE <= addr) && + (addr < (APLIC_SETIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIP_BASE ) >>2 ; +riscv_aplic_set_pending_word (aplic, word, value, true); + }else if (addr ==APLIC_SETIPNUM ) { +riscv_aplic_set_pending (aplic, value, true); + }else if ((APLIC_CLRIP_BASE <= addr) && + (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_CLRIP_BASE ) >>2 ; +riscv_aplic_set_pending_word (aplic, word, value, false); + }else if (addr ==APLIC_CLRIPNUM ) { +riscv_aplic_set_pending (aplic, value, false); + }else if ((APLIC_SETIE_BASE <= addr) && + (addr < (APLIC_SETIE_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_SETIE_BASE ) >>2 ; +riscv_aplic_set_enabled_word (aplic, word, value, true); + }else if (addr ==APLIC_SETIENUM ) { +riscv_aplic_set_enabled (aplic, value, true); + }else if ((APLIC_CLRIE_BASE <= addr) && + (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words *4 ))) { + word = (addr -APLIC_CLRIE_BASE ) >>2 ; +riscv_aplic_set_enabled_word (aplic, word, value, false); + }else if (addr ==APLIC_CLRIENUM ) { +riscv_aplic_set_enabled (aplic, value, false); + }else if (addr ==APLIC_SETIPNUM_LE ) { +riscv_aplic_set_pending (aplic, value, true); + }else if (addr ==APLIC_SETIPNUM_BE ) { +riscv_aplic_set_pending (aplic,bswap32 (value), true); + }else if (addr ==APLIC_GENMSI ) { +if (aplic->msimode) { + aplic->genmsi = value & ~(APLIC_TARGET_GUEST_IDX_MASK << +APLIC_TARGET_GUEST_IDX_SHIFT ); +riscv_aplic_msi_send (aplic, + value >>APLIC_TARGET_HART_IDX_SHIFT , +0 , + value &APLIC_TARGET_EIID_MASK ); + } + }else if ((APLIC_TARGET_BASE <= addr) && + (addr < (APLIC_TARGET_BASE + (aplic->num_irqs -1 ) *4 ))) { + irq = ((addr -APLIC_TARGET_BASE ) >>2 ) +1 ; +if (aplic->msimode) { + aplic->target[irq] = value; + }else { + aplic->target[irq] = (value & ~APLIC_TARGET_IPRIO_MASK ) | + (value & aplic->iprio_mask); + } + }else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) && + (addr < (APLIC_IDC_BASE + aplic->num_harts *APLIC_IDC_SIZE ))) { + idc = (addr -APLIC_IDC_BASE ) /APLIC_IDC_SIZE ; +switch (addr - (APLIC_IDC_BASE + idc *APLIC_IDC_SIZE )) { +case APLIC_IDC_IDELIVERY : + aplic->idelivery[idc] = value &0x1 ; +break ; +case APLIC_IDC_IFORCE : + aplic->iforce[idc] = value &0x1 ; +break ; +case APLIC_IDC_ITHRESHOLD : + aplic->ithreshold[idc] = value & aplic->iprio_mask; +break ; +default : + goto err; + }; + }else { + goto err; + } + +if (aplic->msimode) { +for (irq =1 ; irq < aplic->num_irqs; irq++) { +riscv_aplic_msi_irq_update (aplic, irq); + } + }else { +if (idc ==UINT32_MAX ) { +for (idc =0 ; idc < aplic->num_harts; idc++) { +riscv_aplic_idc_update (aplic, idc); + } + }else { +riscv_aplic_idc_update (aplic, idc); + } + } + +return ; + +err: +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Invalid register write 0x%" HWADDR_PRIx"\n" , + __func__, addr); +} + +static const MemoryRegionOps riscv_aplic_ops = { + .read = riscv_aplic_read, + .write = riscv_aplic_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .valid = { + .min_access_size =4 , + .max_access_size =4 + } +}; + +static void riscv_aplic_realize (DeviceState *dev,Error **errp) +{ +uint32_t i; +RISCVAPLICState *aplic =RISCV_APLIC (dev); + + aplic->bitfield_words = (aplic->num_irqs +31 ) >>5 ; + aplic->sourcecfg =g_new0 (uint32_t, aplic->num_irqs); + aplic->state =g_new (uint32_t, aplic->num_irqs); + aplic->target =g_new0 (uint32_t, aplic->num_irqs); + aplic->idelivery =g_new0 (uint32_t, aplic->num_harts); + aplic->iforce =g_new0 (uint32_t, aplic->num_harts); + aplic->ithreshold =g_new0 (uint32_t, aplic->num_harts); + +memory_region_init_io (&aplic->mmio,OBJECT (dev), &riscv_aplic_ops, aplic, +TYPE_RISCV_APLIC , aplic->aperture_size); +sysbus_init_mmio (SYS_BUS_DEVICE (dev), &aplic->mmio); + +/* Only root APLICs have hardware IRQ lines. All non-root APLICs +* have IRQ lines delegated by their parent APLIC. +*/ +if (!aplic->parent) { +qdev_init_gpio_in (dev, riscv_aplic_request, aplic->num_irqs); + } + +/* Create output IRQ lines for non-MSI mode */ +if (!aplic->msimode) { + aplic->external_irqs =g_malloc (sizeof (qemu_irq) * aplic->num_harts); +qdev_init_gpio_out (dev, aplic->external_irqs, aplic->num_harts); + +/* Claim the CPU interrupt to be triggered by this APLIC */ +for (i =0 ; i < aplic->num_harts; i++) { +RISCVCPU *cpu =RISCV_CPU (qemu_get_cpu (aplic->hartid_base + i)); +if (riscv_cpu_claim_interrupts (cpu, + (aplic->mmode) ?MIP_MEIP :MIP_SEIP ) <0 ) { +error_report ("%s already claimed" , + (aplic->mmode) ?"MEIP" :"SEIP" ); +exit (1 ); + } + } + } + + msi_nonbroken = true; +} + +static Property riscv_aplic_properties[] = { +DEFINE_PROP_UINT32 ("aperture-size" , RISCVAPLICState, aperture_size,0 ), +DEFINE_PROP_UINT32 ("hartid-base" , RISCVAPLICState, hartid_base,0 ), +DEFINE_PROP_UINT32 ("num-harts" , RISCVAPLICState, num_harts,0 ), +DEFINE_PROP_UINT32 ("iprio-mask" , RISCVAPLICState, iprio_mask,0 ), +DEFINE_PROP_UINT32 ("num-irqs" , RISCVAPLICState, num_irqs,0 ), +DEFINE_PROP_BOOL ("msimode" , RISCVAPLICState, msimode,0 ), +DEFINE_PROP_BOOL ("mmode" , RISCVAPLICState, mmode,0 ), +DEFINE_PROP_END_OF_LIST (), +}; + +static const VMStateDescription vmstate_riscv_aplic = { + .name ="riscv_aplic" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT32 (domaincfg, RISCVAPLICState), +VMSTATE_UINT32 (mmsicfgaddr, RISCVAPLICState), +VMSTATE_UINT32 (mmsicfgaddrH, RISCVAPLICState), +VMSTATE_UINT32 (smsicfgaddr, RISCVAPLICState), +VMSTATE_UINT32 (smsicfgaddrH, RISCVAPLICState), +VMSTATE_UINT32 (genmsi, RISCVAPLICState), +VMSTATE_VARRAY_UINT32 (sourcecfg, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (state, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (target, RISCVAPLICState, + num_irqs,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (idelivery, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (iforce, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_VARRAY_UINT32 (ithreshold, RISCVAPLICState, + num_harts,0 , + vmstate_info_uint32, uint32_t), +VMSTATE_END_OF_LIST () + } +}; + +static void riscv_aplic_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + +device_class_set_props (dc, riscv_aplic_properties); + dc->realize = riscv_aplic_realize; + dc->vmsd = &vmstate_riscv_aplic; +} + +static const TypeInfo riscv_aplic_info = { + .name =TYPE_RISCV_APLIC , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (RISCVAPLICState), + .class_init = riscv_aplic_class_init, +}; + +static void riscv_aplic_register_types (void ) +{ +type_register_static (&riscv_aplic_info); +} + +type_init (riscv_aplic_register_types) + +/* +* Add a APLIC device to another APLIC device as child for +* interrupt delegation. +*/ +void riscv_aplic_add_child(DeviceState *parent, DeviceState *child) +{ +RISCVAPLICState *caplic, *paplic; + +assert (parent && child); + caplic =RISCV_APLIC (child); + paplic =RISCV_APLIC (parent); + +assert (paplic->num_irqs == caplic->num_irqs); +assert (paplic->num_children <=QEMU_APLIC_MAX_CHILDREN ); + + caplic->parent = paplic; + paplic->children[paplic->num_children] = caplic; + paplic->num_children++; +} + +/* +* Create APLIC device. +*/ +DeviceState *riscv_aplic_create (hwaddr addr,hwaddr size, +uint32_t hartid_base,uint32_t num_harts,uint32_t num_sources, +uint32_t iprio_bits,bool msimode,bool mmode,DeviceState *parent) +{ +DeviceState *dev =qdev_new (TYPE_RISCV_APLIC ); +uint32_t i; + +assert (num_harts <APLIC_MAX_IDC ); +assert ((APLIC_IDC_BASE + (num_harts *APLIC_IDC_SIZE )) <= size); +assert (num_sources <APLIC_MAX_SOURCE ); +assert (APLIC_MIN_IPRIO_BITS <= iprio_bits); +assert (iprio_bits <=APLIC_MAX_IPRIO_BITS ); + +qdev_prop_set_uint32 (dev,"aperture-size" , size); +qdev_prop_set_uint32 (dev,"hartid-base" , hartid_base); +qdev_prop_set_uint32 (dev,"num-harts" , num_harts); +qdev_prop_set_uint32 (dev,"iprio-mask" , ((1U << iprio_bits) -1 )); +qdev_prop_set_uint32 (dev,"num-irqs" , num_sources +1 ); +qdev_prop_set_bit (dev,"msimode" , msimode); +qdev_prop_set_bit (dev,"mmode" , mmode); + +sysbus_realize_and_unref (SYS_BUS_DEVICE (dev), &error_fatal); +sysbus_mmio_map (SYS_BUS_DEVICE (dev),0 , addr); + +if (parent) { +riscv_aplic_add_child (parent, dev); + } + +if (!msimode) { +for (i =0 ; i < num_harts; i++) { +CPUState *cpu =qemu_get_cpu (hartid_base + i); + +qdev_connect_gpio_out_named (dev,NULL , i, +qdev_get_gpio_in (DEVICE (cpu), + (mmode) ?IRQ_M_EXT :IRQ_S_EXT )); + } + } + +return dev; +}include/hw/intc/riscv_aplic.h new file +79
-1@@ -0,0 +1,79 @@ +/* +* RISC-V APLIC (Advanced Platform Level Interrupt Controller) interface +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef HW_RISCV_APLIC_H +#define HW_RISCV_APLIC_H + +#include "hw/sysbus.h" +#include "qom/object.h" + +#define TYPE_RISCV_APLIC "riscv.aplic" + +typedef struct RISCVAPLICState RISCVAPLICState ; +DECLARE_INSTANCE_CHECKER(RISCVAPLICState, RISCV_APLIC, TYPE_RISCV_APLIC) + +#define APLIC_MIN_SIZE 0x4000 +#define APLIC_SIZE_ALIGN (__x) (((__x) + (APLIC_MIN_SIZE - 1)) & \ + ~(APLIC_MIN_SIZE - 1)) +#define APLIC_SIZE (__num_harts) (APLIC_MIN_SIZE + \ + APLIC_SIZE_ALIGN(32 * (__num_harts))) + +struct RISCVAPLICState { +/*< private >*/ +SysBusDevice parent_obj; +qemu_irq *external_irqs; + +/*< public >*/ +MemoryRegion mmio; +uint32_t bitfield_words; +uint32_t domaincfg; +uint32_t mmsicfgaddr; +uint32_t mmsicfgaddrH; +uint32_t smsicfgaddr; +uint32_t smsicfgaddrH; +uint32_t genmsi; +uint32_t *sourcecfg; +uint32_t *state; +uint32_t *target; +uint32_t *idelivery; +uint32_t *iforce; +uint32_t *ithreshold; + +/* topology */ +#define QEMU_APLIC_MAX_CHILDREN 16 +struct RISCVAPLICState *parent; +struct RISCVAPLICState *children[QEMU_APLIC_MAX_CHILDREN ]; +uint16_t num_children; + +/* config */ +uint32_t aperture_size; +uint32_t hartid_base; +uint32_t num_harts; +uint32_t iprio_mask; +uint32_t num_irqs; +bool msimode; +bool mmode; +}; + +void riscv_aplic_add_child (DeviceState *parent,DeviceState *child); + +DeviceState *riscv_aplic_create (hwaddr addr,hwaddr size, +uint32_t hartid_base,uint32_t num_harts,uint32_t num_sources, +uint32_t iprio_bits,bool msimode,bool mmode,DeviceState *parent); + +#endif --2.25.1