@samitouri / QOSamiQemu / commits / 5b9e32dd5c

target/riscv: move custom_csrs logic to tcg-cpu.c

We have a couple of CPUs that has a set of custom CSRs that uses TCG specific APIs. Move the related code to tcg-cpu.c and do not set .custom_csrs if we're not in a TCG build. What we'll end up doing, sooner or later, is punting all these CPUs to tcg-cpu.c since they're all TCG specific and KVM has nothing to do with them. Another time. Signed-off-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Message-ID: <20260703180538.3346781-12-daniel.barboza@oss.qualcomm.com> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

Daniel Henrique Barboza committed Jul 3, 2026 at 15:05 UTC 5b9e32dd5cd4942e3bee9e8c494019e1d06987d2
3 files changed +28 -21
target/riscv/cpu.c
+5 -21
@@ -544,19 +544,6 @@ static void set_satp_mode_default_map(RISCVCPU *cpu)
544 }
545 #endif
546
547 -#ifndef CONFIG_USER_ONLY
548 -static void riscv_register_custom_csrs(RISCVCPU *cpu, const RISCVCSR *csr_list)
549 -{
550 - for (size_t i = 0; csr_list[i].csr_ops.name; i++) {
551 - int csrno = csr_list[i].csrno;
552 - const riscv_csr_operations *csr_ops = &csr_list[i].csr_ops;
553 - if (!csr_list[i].insertion_test || csr_list[i].insertion_test(cpu)) {
554 - riscv_set_csr_ops(csrno, csr_ops);
555 - }
556 - }
557 -}
558 -#endif
559 -
547 /* Used by csr.c and the KVM driver */
548 target_ulong riscv_new_csr_seed(target_ulong new_value,
549 target_ulong write_mask)
@@ -1268,11 +1255,6 @@ static void riscv_cpu_init(Object *obj)
1255 if (mcc->def->vext_spec != RISCV_PROFILE_ATTR_UNUSED) {
1256 cpu->env.vext_ver = mcc->def->vext_spec;
1257 }
1271 -#ifndef CONFIG_USER_ONLY
1272 - if (mcc->def->custom_csrs) {
1273 - riscv_register_custom_csrs(cpu, mcc->def->custom_csrs);
1274 - }
1275 -#endif
1258
1259 accel_cpu_instance_init(CPU(obj));
1260 }
@@ -2846,10 +2828,12 @@ static void riscv_cpu_class_base_init(ObjectClass *c, const void *data)
2828
2829 riscv_cpu_cfg_merge(&mcc->def->cfg, &def->cfg);
2830
2831 +#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
2832 if (def->custom_csrs) {
2833 assert(!mcc->def->custom_csrs);
2834 mcc->def->custom_csrs = def->custom_csrs;
2835 }
2836 +#endif
2837 }
2838
2839 if (!object_class_is_abstract(c)) {
@@ -3202,7 +3186,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
3186 .cfg.mvendorid = THEAD_VENDOR_ID,
3187
3188 .cfg.max_satp_mode = VM_1_10_SV39,
3205 -#ifndef CONFIG_USER_ONLY
3189 +#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
3190 .custom_csrs = th_csr_list,
3191 #endif
3192 ),
@@ -3248,7 +3232,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
3232
3233 .cfg.marchid = 0x8d143000,
3234 .cfg.mvendorid = THEAD_VENDOR_ID,
3251 -#ifndef CONFIG_USER_ONLY
3235 +#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
3236 .custom_csrs = th_csr_list,
3237 #endif
3238 ),
@@ -3454,7 +3438,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
3438 .cfg.ext_xmipscmov = true,
3439 .cfg.marchid = 0x8000000000000201,
3440 .cfg.mvendorid = MIPS_VENDOR_ID,
3457 -#ifndef CONFIG_USER_ONLY
3441 +#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
3442 .custom_csrs = mips_csr_list,
3443 #endif
3444 ),
target/riscv/cpu.h
+2
@@ -591,7 +591,9 @@ typedef struct RISCVCPUDef {
591 int32_t vext_spec;
592 RISCVCPUConfig cfg;
593 bool bare;
594 +#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
595 const RISCVCSR *custom_csrs;
596 +#endif
597 /* This is just a setter for env->num_triggers. */
598 uint32_t num_triggers;
599 } RISCVCPUDef;
target/riscv/tcg/tcg-cpu.c
+21
@@ -38,6 +38,7 @@
38 #include "system/tcg.h"
39 #include "exec/icount.h"
40 #include "target/riscv/tcg/debug.h"
41 +#include "target/riscv/tcg/csr.h"
42 #endif
43
44 /* Hash that stores user set extensions */
@@ -1650,10 +1651,30 @@ static bool riscv_cpu_has_max_extensions(Object *cpu_obj)
1651 return object_dynamic_cast(cpu_obj, TYPE_RISCV_CPU_MAX) != NULL;
1652 }
1653
1654 +#ifndef CONFIG_USER_ONLY
1655 +static void riscv_register_custom_csrs(RISCVCPU *cpu, const RISCVCSR *csr_list)
1656 +{
1657 + for (size_t i = 0; csr_list[i].csr_ops.name; i++) {
1658 + int csrno = csr_list[i].csrno;
1659 + const riscv_csr_operations *csr_ops = &csr_list[i].csr_ops;
1660 + if (!csr_list[i].insertion_test || csr_list[i].insertion_test(cpu)) {
1661 + riscv_set_csr_ops(csrno, csr_ops);
1662 + }
1663 + }
1664 +}
1665 +#endif
1666 +
1667 static void riscv_tcg_cpu_instance_init(CPUState *cs)
1668 {
1669 RISCVCPU *cpu = RISCV_CPU(cs);
1670 Object *obj = OBJECT(cpu);
1671 +#ifndef CONFIG_USER_ONLY
1672 + RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(obj);
1673 +
1674 + if (mcc->def->custom_csrs) {
1675 + riscv_register_custom_csrs(cpu, mcc->def->custom_csrs);
1676 + }
1677 +#endif
1678
1679 misa_ext_user_opts = g_hash_table_new(NULL, g_direct_equal);
1680 multi_ext_user_opts = g_hash_table_new(NULL, g_direct_equal);