From: Chao Liu <[email protected]> Add initial support for the Radxa ROCK 5B board, which uses the RK3588 SoC and 128 MiB to 16 GiB of DRAM. Support direct Linux booting.
The first -serial option drives UART2, the debug console on the ROCK 5B header. Further -serial options connect to UART1 and UART3 through UART9 in that order. Signed-off-by: Chao Liu <[email protected]> Signed-off-by: Yifan Wang <[email protected]> --- MAINTAINERS | 1 + hw/arm/Kconfig | 6 ++ hw/arm/meson.build | 1 + hw/arm/rock-5b.c | 200 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 hw/arm/rock-5b.c diff --git a/MAINTAINERS b/MAINTAINERS index c3d3388975..544d93c01d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1065,6 +1065,7 @@ M: Yifan Wang <[email protected]> L: [email protected] S: Maintained F: hw/arm/rk3588.c +F: hw/arm/rock-5b.c F: hw/misc/rockchip_cru.c F: hw/misc/rockchip_otp.c F: hw/misc/rk3588-scmci.c diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig index eaa55eae29..40f7de823c 100644 --- a/hw/arm/Kconfig +++ b/hw/arm/Kconfig @@ -783,3 +783,9 @@ config RK3588 select REGISTER select ROCKCHIP_SFC select UNIMP + +config ROCK5B + bool + default y + depends on TCG && AARCH64 + select RK3588 diff --git a/hw/arm/meson.build b/hw/arm/meson.build index 70c835814f..9af1e01037 100644 --- a/hw/arm/meson.build +++ b/hw/arm/meson.build @@ -121,5 +121,6 @@ arm_common_ss.add(when: ['CONFIG_AXIADO_EVK', 'TARGET_AARCH64'], if_true: files( arm_common_ss.add(files('boot.c')) arm_common_ss.add(when: 'CONFIG_RK3588', if_true: files('rk3588.c')) +arm_common_ss.add(when: 'CONFIG_ROCK5B', if_true: files('rock-5b.c')) hw_common_arch += {'arm': arm_common_ss} diff --git a/hw/arm/rock-5b.c b/hw/arm/rock-5b.c new file mode 100644 index 0000000000..7906d53175 --- /dev/null +++ b/hw/arm/rock-5b.c @@ -0,0 +1,200 @@ +/* + * Radxa ROCK 5B Board emulation + * + * Copyright (C) 2026 Process Mission + * + * Authors: + * Chao Liu <[email protected]> + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "qemu/module.h" +#include "qemu/units.h" +#include "qapi/error.h" +#include "hw/arm/boot.h" +#include "hw/arm/rk3588.h" +#include "hw/core/boards.h" +#include "hw/core/qdev-properties-system.h" +#include "system/address-spaces.h" +#include "system/device_tree.h" +#include "system/kvm.h" +#include "system/qtest.h" +#include "system/system.h" + +#define TYPE_ROCK5B_MACHINE MACHINE_TYPE_NAME("rock-5b") +OBJECT_DECLARE_SIMPLE_TYPE(Rock5BMachineState, ROCK5B_MACHINE) + +enum { + ROCK5B_LOW_RAM, + ROCK5B_HIGH_RAM, +}; + +static const MemMapEntry rock5b_memmap[] = { + [ROCK5B_LOW_RAM] = { 0x000000000, 0x0f0000000 }, + [ROCK5B_HIGH_RAM] = { 0x100000000, 0x310000000 }, +}; + +/* Match the low-memory reservation made by Rockchip U-Boot for BL31. */ +#define ROCK5B_FIRMWARE_RESERVED_SIZE (2 * MiB) +/* UART2 drives the board's debug header. */ +#define ROCK5B_CONSOLE_UART 2 + +struct Rock5BMachineState { + MachineState parent_obj; + + RK3588SoCState soc; + MemoryRegion low_ram; + MemoryRegion high_ram; + uint64_t low_ram_size; + uint64_t high_ram_size; + struct arm_boot_info binfo; +}; + +/* + * The first -serial option drives the debug console; further options + * connect to the remaining UARTs in index order. + */ +static void rock5b_connect_serial(Rock5BMachineState *s) +{ + unsigned int hd = 1; + + /* UART0 in the PMU power domain is not modeled. */ + for (unsigned int i = 1; i < RK3588_NUM_UARTS; i++) { + g_autofree char *prop = g_strdup_printf("uart%u-chardev", i); + + qdev_prop_set_chr(DEVICE(&s->soc), prop, + serial_hd(i == ROCK5B_CONSOLE_UART ? 0 : hd++)); + } +} + +/* + * arm_load_dtb() replaces the memory nodes with a single node for low RAM; + * add the RAM above the peripheral hole. + */ +static void rock5b_modify_dtb(const struct arm_boot_info *binfo, void *fdt) +{ + const Rock5BMachineState *s = container_of(binfo, Rock5BMachineState, + binfo); + hwaddr base = rock5b_memmap[ROCK5B_HIGH_RAM].base; + g_autofree char *node = NULL; + uint32_t acells, scells; + + if (!s->high_ram_size) { + return; + } + + acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells", NULL, + &error_fatal); + scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells", NULL, + &error_fatal); + node = g_strdup_printf("/memory@%" HWADDR_PRIx, base); + qemu_fdt_add_subnode(fdt, node); + qemu_fdt_setprop_string(fdt, node, "device_type", "memory"); + if (qemu_fdt_setprop_sized_cells(fdt, node, "reg", acells, base, + scells, s->high_ram_size) < 0) { + error_report("rock-5b: device tree cells cannot describe RAM " + "above 4 GiB"); + exit(EXIT_FAILURE); + } +} + +/* + * arm_load_kernel() enters Linux at EL2, keeps the SMC PSCI conduit and + * registers the CPU reset handlers. + */ +static void rock5b_direct_boot(Rock5BMachineState *s) +{ + s->binfo.ram_size = s->low_ram_size - ROCK5B_FIRMWARE_RESERVED_SIZE; + s->binfo.loader_start = ROCK5B_FIRMWARE_RESERVED_SIZE; + s->binfo.board_id = -1; + s->binfo.psci_conduit = QEMU_PSCI_CONDUIT_SMC; + s->binfo.modify_dtb = rock5b_modify_dtb; + arm_load_kernel(&s->soc.cpu[0], MACHINE(s), &s->binfo); +} + +static void rock5b_init(MachineState *ms) +{ + Rock5BMachineState *s = ROCK5B_MACHINE(ms); + + if (ms->ram_size < 128 * MiB || ms->ram_size > 16 * GiB) { + error_report("rock-5b: memory size must be between " + "128 MiB and 16 GiB"); + exit(EXIT_FAILURE); + } + + if (kvm_enabled()) { + error_report("rock-5b: KVM is not supported"); + exit(EXIT_FAILURE); + } + + if (ms->firmware) { + error_report("rock-5b: -bios is not supported"); + exit(EXIT_FAILURE); + } + if (ms->kernel_filename) { + if (!ms->dtb) { + error_report("rock-5b: direct Linux boot requires -dtb"); + exit(EXIT_FAILURE); + } + } else if (!qtest_enabled()) { + error_report("rock-5b: supply a Linux kernel with -kernel"); + exit(EXIT_FAILURE); + } + + /* Initialize the SoC object. */ + object_initialize_child(OBJECT(s), "soc", &s->soc, TYPE_RK3588_SOC); + object_property_set_link(OBJECT(&s->soc), "memory", + OBJECT(get_system_memory()), &error_abort); + rock5b_connect_serial(s); + qdev_realize(DEVICE(&s->soc), NULL, &error_fatal); + + s->low_ram_size = MIN(ms->ram_size, + rock5b_memmap[ROCK5B_LOW_RAM].size); + s->high_ram_size = ms->ram_size - s->low_ram_size; + memory_region_init_alias(&s->low_ram, OBJECT(s), "rock-5b.low-ram", + ms->ram, 0, s->low_ram_size); + memory_region_add_subregion(get_system_memory(), + rock5b_memmap[ROCK5B_LOW_RAM].base, + &s->low_ram); + if (s->high_ram_size) { + memory_region_init_alias(&s->high_ram, OBJECT(s), + "rock-5b.high-ram", ms->ram, + s->low_ram_size, s->high_ram_size); + memory_region_add_subregion(get_system_memory(), + rock5b_memmap[ROCK5B_HIGH_RAM].base, + &s->high_ram); + } + + rock5b_direct_boot(s); +} + +static void rock5b_class_init(ObjectClass *oc, const void *data) +{ + MachineClass *mc = MACHINE_CLASS(oc); + + mc->desc = "Radxa ROCK 5B"; + mc->init = rock5b_init; + mc->default_ram_size = 2 * GiB; + mc->default_ram_id = "rock-5b.ram"; + mc->min_cpus = RK3588_NUM_CPUS; + mc->default_cpus = RK3588_NUM_CPUS; + mc->max_cpus = RK3588_NUM_CPUS; + mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a55"); +} + +static const TypeInfo rock5b_typeinfo = { + .name = TYPE_ROCK5B_MACHINE, + .parent = TYPE_MACHINE, + .instance_size = sizeof(Rock5BMachineState), + .class_init = rock5b_class_init, +}; + +static void rock5b_register_types(void) +{ + type_register_static(&rock5b_typeinfo); +} + +type_init(rock5b_register_types) -- 2.53.0
