@samitouri / QOSamiQemu / commits / b6cdd8f13e

hw/riscv/spike.c: add intc_phandles array

The clint FDT generation uses a cells array (clint_cells) that are populated in the middle of the loop that creates the CPU socket FDT. This is completely fine but it differs from the other boards that creates the clint cells array right before creating the clint FDT. 'virt' and 'sifive_u' store the intc phandles in a intc_phandles array during FDT CPU socket creation, and this array is used to create the clint FDT cells. Standardize the clint FDT creation for spike doing the same here, allowing us to move everything to a common helper later. Signed-off-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Message-ID: <20260615203734.954428-7-daniel.barboza@oss.qualcomm.com> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

Daniel Henrique Barboza committed Jun 15, 2026 at 17:37 UTC b6cdd8f13e74d3855cd18c0419fdadb502cb24e6
1 file changed +16 -10
hw/riscv/spike.c
+16 -10
@@ -59,7 +59,7 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap,
59 int cpu, socket;
60 MachineState *ms = MACHINE(s);
61 uint32_t *clint_cells;
62 - uint32_t cpu_phandle, intc_phandle, phandle = 1;
62 + uint32_t cpu_phandle, phandle = 1;
63 char *clint_name, *clust_name;
64 char *core_name, *cpu_name, *intc_name;
65 static const char * const clint_compat[2] = {
@@ -84,6 +84,8 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap,
84 qemu_fdt_add_subnode(fdt, "/cpus/cpu-map");
85
86 for (socket = (riscv_socket_count(ms) - 1); socket >= 0; socket--) {
87 + g_autofree uint32_t *intc_phandles = g_new0(uint32_t,
88 + s->soc[socket].num_harts);
89 hwaddr memaddr = memmap[SPIKE_DRAM].base +
90 riscv_socket_mem_offset(ms, socket);
91 uint64_t memsize = riscv_socket_mem_size(ms, socket);
@@ -91,8 +93,6 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap,
93 clust_name = g_strdup_printf("/cpus/cpu-map/cluster%d", socket);
94 qemu_fdt_add_subnode(fdt, clust_name);
95
94 - clint_cells = g_new0(uint32_t, s->soc[socket].num_harts * 4);
95 -
96 for (cpu = s->soc[socket].num_harts - 1; cpu >= 0; cpu--) {
97 cpu_phandle = phandle++;
98
@@ -113,20 +113,17 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap,
113 riscv_socket_fdt_write_id(ms, cpu_name, socket);
114 qemu_fdt_setprop_cell(fdt, cpu_name, "phandle", cpu_phandle);
115
116 + intc_phandles[cpu] = phandle++;
117 +
118 intc_name = g_strdup_printf("%s/interrupt-controller", cpu_name);
119 qemu_fdt_add_subnode(fdt, intc_name);
118 - intc_phandle = phandle++;
119 - qemu_fdt_setprop_cell(fdt, intc_name, "phandle", intc_phandle);
120 + qemu_fdt_setprop_cell(fdt, intc_name, "phandle",
121 + intc_phandles[cpu]);
122 qemu_fdt_setprop_string(fdt, intc_name, "compatible",
123 "riscv,cpu-intc");
124 qemu_fdt_setprop(fdt, intc_name, "interrupt-controller", NULL, 0);
125 qemu_fdt_setprop_cell(fdt, intc_name, "#interrupt-cells", 1);
126
125 - clint_cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
126 - clint_cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
127 - clint_cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
128 - clint_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
129 -
127 core_name = g_strdup_printf("%s/core%d", clust_name, cpu);
128 qemu_fdt_add_subnode(fdt, core_name);
129 qemu_fdt_setprop_cell(fdt, core_name, "cpu", cpu_phandle);
@@ -139,6 +136,15 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap,
136 create_fdt_socket_memory(fdt, memaddr, memsize, socket,
137 riscv_numa_enabled(ms));
138
139 + clint_cells = g_new0(uint32_t, s->soc[socket].num_harts * 4);
140 +
141 + for (cpu = 0; cpu < s->soc[socket].num_harts; cpu++) {
142 + clint_cells[cpu * 4 + 0] = cpu_to_be32(intc_phandles[cpu]);
143 + clint_cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
144 + clint_cells[cpu * 4 + 2] = cpu_to_be32(intc_phandles[cpu]);
145 + clint_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
146 + }
147 +
148 clint_addr = memmap[SPIKE_CLINT].base +
149 (memmap[SPIKE_CLINT].size * socket);
150 clint_name = g_strdup_printf("/soc/clint@%lx", clint_addr);