hw/riscv/virt.c: use s->memmap in create_fdt_sockets() path

create_fdt_sockets() and all its fdt helpers (create_fdt_socket_aplic(),
create_fdt_imsic(), create_fdt_socket_plic(), create_fdt_socket_aclint()
and create_fdt_socket_memory()) can use s->memmap from their
RISCVVirtState pointer instead of having an extra memmap argument.

Signed-off-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-ID: <20250429125811.224803-7-dbarboza@ventanamicro.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
This commit is contained in:
Daniel Henrique Barboza 2025-04-29 09:58:08 -03:00 committed by Alistair Francis
parent 658e501969
commit 04c4f8d1ee

View file

@ -301,14 +301,13 @@ static void create_fdt_socket_cpus(RISCVVirtState *s, int socket,
} }
} }
static void create_fdt_socket_memory(RISCVVirtState *s, static void create_fdt_socket_memory(RISCVVirtState *s, int socket)
const MemMapEntry *memmap, int socket)
{ {
g_autofree char *mem_name = NULL; g_autofree char *mem_name = NULL;
uint64_t addr, size; uint64_t addr, size;
MachineState *ms = MACHINE(s); MachineState *ms = MACHINE(s);
addr = memmap[VIRT_DRAM].base + riscv_socket_mem_offset(ms, socket); addr = s->memmap[VIRT_DRAM].base + riscv_socket_mem_offset(ms, socket);
size = riscv_socket_mem_size(ms, socket); size = riscv_socket_mem_size(ms, socket);
mem_name = g_strdup_printf("/memory@%lx", (long)addr); mem_name = g_strdup_printf("/memory@%lx", (long)addr);
qemu_fdt_add_subnode(ms->fdt, mem_name); qemu_fdt_add_subnode(ms->fdt, mem_name);
@ -319,7 +318,7 @@ static void create_fdt_socket_memory(RISCVVirtState *s,
} }
static void create_fdt_socket_clint(RISCVVirtState *s, static void create_fdt_socket_clint(RISCVVirtState *s,
const MemMapEntry *memmap, int socket, int socket,
uint32_t *intc_phandles) uint32_t *intc_phandles)
{ {
int cpu; int cpu;
@ -340,21 +339,22 @@ static void create_fdt_socket_clint(RISCVVirtState *s,
clint_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); clint_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
} }
clint_addr = memmap[VIRT_CLINT].base + (memmap[VIRT_CLINT].size * socket); clint_addr = s->memmap[VIRT_CLINT].base +
(s->memmap[VIRT_CLINT].size * socket);
clint_name = g_strdup_printf("/soc/clint@%lx", clint_addr); clint_name = g_strdup_printf("/soc/clint@%lx", clint_addr);
qemu_fdt_add_subnode(ms->fdt, clint_name); qemu_fdt_add_subnode(ms->fdt, clint_name);
qemu_fdt_setprop_string_array(ms->fdt, clint_name, "compatible", qemu_fdt_setprop_string_array(ms->fdt, clint_name, "compatible",
(char **)&clint_compat, (char **)&clint_compat,
ARRAY_SIZE(clint_compat)); ARRAY_SIZE(clint_compat));
qemu_fdt_setprop_cells(ms->fdt, clint_name, "reg", qemu_fdt_setprop_cells(ms->fdt, clint_name, "reg",
0x0, clint_addr, 0x0, memmap[VIRT_CLINT].size); 0x0, clint_addr, 0x0, s->memmap[VIRT_CLINT].size);
qemu_fdt_setprop(ms->fdt, clint_name, "interrupts-extended", qemu_fdt_setprop(ms->fdt, clint_name, "interrupts-extended",
clint_cells, s->soc[socket].num_harts * sizeof(uint32_t) * 4); clint_cells, s->soc[socket].num_harts * sizeof(uint32_t) * 4);
riscv_socket_fdt_write_id(ms, clint_name, socket); riscv_socket_fdt_write_id(ms, clint_name, socket);
} }
static void create_fdt_socket_aclint(RISCVVirtState *s, static void create_fdt_socket_aclint(RISCVVirtState *s,
const MemMapEntry *memmap, int socket, int socket,
uint32_t *intc_phandles) uint32_t *intc_phandles)
{ {
int cpu; int cpu;
@ -381,8 +381,10 @@ static void create_fdt_socket_aclint(RISCVVirtState *s,
aclint_cells_size = s->soc[socket].num_harts * sizeof(uint32_t) * 2; aclint_cells_size = s->soc[socket].num_harts * sizeof(uint32_t) * 2;
if (s->aia_type != VIRT_AIA_TYPE_APLIC_IMSIC) { if (s->aia_type != VIRT_AIA_TYPE_APLIC_IMSIC) {
addr = memmap[VIRT_CLINT].base + (memmap[VIRT_CLINT].size * socket); addr = s->memmap[VIRT_CLINT].base +
(s->memmap[VIRT_CLINT].size * socket);
name = g_strdup_printf("/soc/mswi@%lx", addr); name = g_strdup_printf("/soc/mswi@%lx", addr);
qemu_fdt_add_subnode(ms->fdt, name); qemu_fdt_add_subnode(ms->fdt, name);
qemu_fdt_setprop_string(ms->fdt, name, "compatible", qemu_fdt_setprop_string(ms->fdt, name, "compatible",
"riscv,aclint-mswi"); "riscv,aclint-mswi");
@ -397,13 +399,13 @@ static void create_fdt_socket_aclint(RISCVVirtState *s,
} }
if (s->aia_type == VIRT_AIA_TYPE_APLIC_IMSIC) { if (s->aia_type == VIRT_AIA_TYPE_APLIC_IMSIC) {
addr = memmap[VIRT_CLINT].base + addr = s->memmap[VIRT_CLINT].base +
(RISCV_ACLINT_DEFAULT_MTIMER_SIZE * socket); (RISCV_ACLINT_DEFAULT_MTIMER_SIZE * socket);
size = RISCV_ACLINT_DEFAULT_MTIMER_SIZE; size = RISCV_ACLINT_DEFAULT_MTIMER_SIZE;
} else { } else {
addr = memmap[VIRT_CLINT].base + RISCV_ACLINT_SWI_SIZE + addr = s->memmap[VIRT_CLINT].base + RISCV_ACLINT_SWI_SIZE +
(memmap[VIRT_CLINT].size * socket); (s->memmap[VIRT_CLINT].size * socket);
size = memmap[VIRT_CLINT].size - RISCV_ACLINT_SWI_SIZE; size = s->memmap[VIRT_CLINT].size - RISCV_ACLINT_SWI_SIZE;
} }
name = g_strdup_printf("/soc/mtimer@%lx", addr); name = g_strdup_printf("/soc/mtimer@%lx", addr);
qemu_fdt_add_subnode(ms->fdt, name); qemu_fdt_add_subnode(ms->fdt, name);
@ -420,14 +422,15 @@ static void create_fdt_socket_aclint(RISCVVirtState *s,
g_free(name); g_free(name);
if (s->aia_type != VIRT_AIA_TYPE_APLIC_IMSIC) { if (s->aia_type != VIRT_AIA_TYPE_APLIC_IMSIC) {
addr = memmap[VIRT_ACLINT_SSWI].base + addr = s->memmap[VIRT_ACLINT_SSWI].base +
(memmap[VIRT_ACLINT_SSWI].size * socket); (s->memmap[VIRT_ACLINT_SSWI].size * socket);
name = g_strdup_printf("/soc/sswi@%lx", addr); name = g_strdup_printf("/soc/sswi@%lx", addr);
qemu_fdt_add_subnode(ms->fdt, name); qemu_fdt_add_subnode(ms->fdt, name);
qemu_fdt_setprop_string(ms->fdt, name, "compatible", qemu_fdt_setprop_string(ms->fdt, name, "compatible",
"riscv,aclint-sswi"); "riscv,aclint-sswi");
qemu_fdt_setprop_cells(ms->fdt, name, "reg", qemu_fdt_setprop_cells(ms->fdt, name, "reg",
0x0, addr, 0x0, memmap[VIRT_ACLINT_SSWI].size); 0x0, addr, 0x0, s->memmap[VIRT_ACLINT_SSWI].size);
qemu_fdt_setprop(ms->fdt, name, "interrupts-extended", qemu_fdt_setprop(ms->fdt, name, "interrupts-extended",
aclint_sswi_cells, aclint_cells_size); aclint_sswi_cells, aclint_cells_size);
qemu_fdt_setprop(ms->fdt, name, "interrupt-controller", NULL, 0); qemu_fdt_setprop(ms->fdt, name, "interrupt-controller", NULL, 0);
@ -438,7 +441,7 @@ static void create_fdt_socket_aclint(RISCVVirtState *s,
} }
static void create_fdt_socket_plic(RISCVVirtState *s, static void create_fdt_socket_plic(RISCVVirtState *s,
const MemMapEntry *memmap, int socket, int socket,
uint32_t *phandle, uint32_t *intc_phandles, uint32_t *phandle, uint32_t *intc_phandles,
uint32_t *plic_phandles) uint32_t *plic_phandles)
{ {
@ -452,7 +455,8 @@ static void create_fdt_socket_plic(RISCVVirtState *s,
}; };
plic_phandles[socket] = (*phandle)++; plic_phandles[socket] = (*phandle)++;
plic_addr = memmap[VIRT_PLIC].base + (memmap[VIRT_PLIC].size * socket); plic_addr = s->memmap[VIRT_PLIC].base +
(s->memmap[VIRT_PLIC].size * socket);
plic_name = g_strdup_printf("/soc/plic@%lx", plic_addr); plic_name = g_strdup_printf("/soc/plic@%lx", plic_addr);
qemu_fdt_add_subnode(ms->fdt, plic_name); qemu_fdt_add_subnode(ms->fdt, plic_name);
qemu_fdt_setprop_cell(ms->fdt, plic_name, qemu_fdt_setprop_cell(ms->fdt, plic_name,
@ -491,7 +495,7 @@ static void create_fdt_socket_plic(RISCVVirtState *s,
} }
qemu_fdt_setprop_cells(ms->fdt, plic_name, "reg", qemu_fdt_setprop_cells(ms->fdt, plic_name, "reg",
0x0, plic_addr, 0x0, memmap[VIRT_PLIC].size); 0x0, plic_addr, 0x0, s->memmap[VIRT_PLIC].size);
qemu_fdt_setprop_cell(ms->fdt, plic_name, "riscv,ndev", qemu_fdt_setprop_cell(ms->fdt, plic_name, "riscv,ndev",
VIRT_IRQCHIP_NUM_SOURCES - 1); VIRT_IRQCHIP_NUM_SOURCES - 1);
riscv_socket_fdt_write_id(ms, plic_name, socket); riscv_socket_fdt_write_id(ms, plic_name, socket);
@ -500,8 +504,8 @@ static void create_fdt_socket_plic(RISCVVirtState *s,
if (!socket) { if (!socket) {
platform_bus_add_all_fdt_nodes(ms->fdt, plic_name, platform_bus_add_all_fdt_nodes(ms->fdt, plic_name,
memmap[VIRT_PLATFORM_BUS].base, s->memmap[VIRT_PLATFORM_BUS].base,
memmap[VIRT_PLATFORM_BUS].size, s->memmap[VIRT_PLATFORM_BUS].size,
VIRT_PLATFORM_BUS_IRQ); VIRT_PLATFORM_BUS_IRQ);
} }
} }
@ -588,7 +592,7 @@ static void create_fdt_one_imsic(RISCVVirtState *s, hwaddr base_addr,
qemu_fdt_setprop_cell(ms->fdt, imsic_name, "phandle", msi_phandle); qemu_fdt_setprop_cell(ms->fdt, imsic_name, "phandle", msi_phandle);
} }
static void create_fdt_imsic(RISCVVirtState *s, const MemMapEntry *memmap, static void create_fdt_imsic(RISCVVirtState *s,
uint32_t *phandle, uint32_t *intc_phandles, uint32_t *phandle, uint32_t *intc_phandles,
uint32_t *msi_m_phandle, uint32_t *msi_s_phandle) uint32_t *msi_m_phandle, uint32_t *msi_s_phandle)
{ {
@ -597,12 +601,12 @@ static void create_fdt_imsic(RISCVVirtState *s, const MemMapEntry *memmap,
if (!kvm_enabled()) { if (!kvm_enabled()) {
/* M-level IMSIC node */ /* M-level IMSIC node */
create_fdt_one_imsic(s, memmap[VIRT_IMSIC_M].base, intc_phandles, create_fdt_one_imsic(s, s->memmap[VIRT_IMSIC_M].base, intc_phandles,
*msi_m_phandle, true, 0); *msi_m_phandle, true, 0);
} }
/* S-level IMSIC node */ /* S-level IMSIC node */
create_fdt_one_imsic(s, memmap[VIRT_IMSIC_S].base, intc_phandles, create_fdt_one_imsic(s, s->memmap[VIRT_IMSIC_S].base, intc_phandles,
*msi_s_phandle, false, *msi_s_phandle, false,
imsic_num_bits(s->aia_guests + 1)); imsic_num_bits(s->aia_guests + 1));
@ -679,7 +683,7 @@ static void create_fdt_one_aplic(RISCVVirtState *s, int socket,
} }
static void create_fdt_socket_aplic(RISCVVirtState *s, static void create_fdt_socket_aplic(RISCVVirtState *s,
const MemMapEntry *memmap, int socket, int socket,
uint32_t msi_m_phandle, uint32_t msi_m_phandle,
uint32_t msi_s_phandle, uint32_t msi_s_phandle,
uint32_t *phandle, uint32_t *phandle,
@ -696,18 +700,19 @@ static void create_fdt_socket_aplic(RISCVVirtState *s,
if (!kvm_enabled()) { if (!kvm_enabled()) {
/* M-level APLIC node */ /* M-level APLIC node */
aplic_addr = memmap[VIRT_APLIC_M].base + aplic_addr = s->memmap[VIRT_APLIC_M].base +
(memmap[VIRT_APLIC_M].size * socket); (s->memmap[VIRT_APLIC_M].size * socket);
create_fdt_one_aplic(s, socket, aplic_addr, memmap[VIRT_APLIC_M].size, create_fdt_one_aplic(s, socket, aplic_addr,
s->memmap[VIRT_APLIC_M].size,
msi_m_phandle, intc_phandles, msi_m_phandle, intc_phandles,
aplic_m_phandle, aplic_s_phandle, aplic_m_phandle, aplic_s_phandle,
true, num_harts); true, num_harts);
} }
/* S-level APLIC node */ /* S-level APLIC node */
aplic_addr = memmap[VIRT_APLIC_S].base + aplic_addr = s->memmap[VIRT_APLIC_S].base +
(memmap[VIRT_APLIC_S].size * socket); (s->memmap[VIRT_APLIC_S].size * socket);
create_fdt_one_aplic(s, socket, aplic_addr, memmap[VIRT_APLIC_S].size, create_fdt_one_aplic(s, socket, aplic_addr, s->memmap[VIRT_APLIC_S].size,
msi_s_phandle, intc_phandles, msi_s_phandle, intc_phandles,
aplic_s_phandle, 0, aplic_s_phandle, 0,
false, num_harts); false, num_harts);
@ -715,8 +720,8 @@ static void create_fdt_socket_aplic(RISCVVirtState *s,
if (!socket) { if (!socket) {
g_autofree char *aplic_name = fdt_get_aplic_nodename(aplic_addr); g_autofree char *aplic_name = fdt_get_aplic_nodename(aplic_addr);
platform_bus_add_all_fdt_nodes(ms->fdt, aplic_name, platform_bus_add_all_fdt_nodes(ms->fdt, aplic_name,
memmap[VIRT_PLATFORM_BUS].base, s->memmap[VIRT_PLATFORM_BUS].base,
memmap[VIRT_PLATFORM_BUS].size, s->memmap[VIRT_PLATFORM_BUS].size,
VIRT_PLATFORM_BUS_IRQ); VIRT_PLATFORM_BUS_IRQ);
} }
@ -734,7 +739,7 @@ static void create_fdt_pmu(RISCVVirtState *s)
riscv_pmu_generate_fdt_node(ms->fdt, hart.pmu_avail_ctrs, pmu_name); riscv_pmu_generate_fdt_node(ms->fdt, hart.pmu_avail_ctrs, pmu_name);
} }
static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap, static void create_fdt_sockets(RISCVVirtState *s,
uint32_t *phandle, uint32_t *phandle,
uint32_t *irq_mmio_phandle, uint32_t *irq_mmio_phandle,
uint32_t *irq_pcie_phandle, uint32_t *irq_pcie_phandle,
@ -770,20 +775,20 @@ static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
create_fdt_socket_cpus(s, socket, clust_name, phandle, create_fdt_socket_cpus(s, socket, clust_name, phandle,
&intc_phandles[phandle_pos]); &intc_phandles[phandle_pos]);
create_fdt_socket_memory(s, memmap, socket); create_fdt_socket_memory(s, socket);
if (virt_aclint_allowed() && s->have_aclint) { if (virt_aclint_allowed() && s->have_aclint) {
create_fdt_socket_aclint(s, memmap, socket, create_fdt_socket_aclint(s, socket,
&intc_phandles[phandle_pos]); &intc_phandles[phandle_pos]);
} else if (tcg_enabled()) { } else if (tcg_enabled()) {
create_fdt_socket_clint(s, memmap, socket, create_fdt_socket_clint(s, socket,
&intc_phandles[phandle_pos]); &intc_phandles[phandle_pos]);
} }
} }
if (s->aia_type == VIRT_AIA_TYPE_APLIC_IMSIC) { if (s->aia_type == VIRT_AIA_TYPE_APLIC_IMSIC) {
create_fdt_imsic(s, memmap, phandle, intc_phandles, create_fdt_imsic(s, phandle, intc_phandles,
&msi_m_phandle, &msi_s_phandle); &msi_m_phandle, &msi_s_phandle);
*msi_pcie_phandle = msi_s_phandle; *msi_pcie_phandle = msi_s_phandle;
} }
@ -792,7 +797,7 @@ static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
* mode, we'll use only one APLIC instance. * mode, we'll use only one APLIC instance.
*/ */
if (!virt_use_emulated_aplic(s->aia_type)) { if (!virt_use_emulated_aplic(s->aia_type)) {
create_fdt_socket_aplic(s, memmap, 0, create_fdt_socket_aplic(s, 0,
msi_m_phandle, msi_s_phandle, phandle, msi_m_phandle, msi_s_phandle, phandle,
&intc_phandles[0], xplic_phandles, &intc_phandles[0], xplic_phandles,
ms->smp.cpus); ms->smp.cpus);
@ -806,11 +811,11 @@ static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
phandle_pos -= s->soc[socket].num_harts; phandle_pos -= s->soc[socket].num_harts;
if (s->aia_type == VIRT_AIA_TYPE_NONE) { if (s->aia_type == VIRT_AIA_TYPE_NONE) {
create_fdt_socket_plic(s, memmap, socket, phandle, create_fdt_socket_plic(s, socket, phandle,
&intc_phandles[phandle_pos], &intc_phandles[phandle_pos],
xplic_phandles); xplic_phandles);
} else { } else {
create_fdt_socket_aplic(s, memmap, socket, create_fdt_socket_aplic(s, socket,
msi_m_phandle, msi_s_phandle, phandle, msi_m_phandle, msi_s_phandle, phandle,
&intc_phandles[phandle_pos], &intc_phandles[phandle_pos],
xplic_phandles, xplic_phandles,
@ -1126,7 +1131,7 @@ static void finalize_fdt(RISCVVirtState *s)
uint32_t irq_pcie_phandle = 1, irq_virtio_phandle = 1; uint32_t irq_pcie_phandle = 1, irq_virtio_phandle = 1;
uint32_t iommu_sys_phandle = 1; uint32_t iommu_sys_phandle = 1;
create_fdt_sockets(s, s->memmap, &phandle, &irq_mmio_phandle, create_fdt_sockets(s, &phandle, &irq_mmio_phandle,
&irq_pcie_phandle, &irq_virtio_phandle, &irq_pcie_phandle, &irq_virtio_phandle,
&msi_pcie_phandle); &msi_pcie_phandle);