From: Chao Liu <[email protected]>

Load a raw U-Boot SPL image supplied with -bios at address zero and
start it in non-secure EL2. SPL loads U-Boot proper from SPI NOR, and
QEMU provides PSCI and SCMI services through SMC calls.

Restore the SPI boot-source value in SRAM and the HPTIMER enable state
on reset before entering SPL.

Signed-off-by: Chao Liu <[email protected]>
Signed-off-by: Yifan Wang <[email protected]>
---
 hw/arm/rock-5b.c | 98 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 83 insertions(+), 15 deletions(-)

diff --git a/hw/arm/rock-5b.c b/hw/arm/rock-5b.c
index abc898c50c..c097e17c5c 100644
--- a/hw/arm/rock-5b.c
+++ b/hw/arm/rock-5b.c
@@ -10,6 +10,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/datadir.h"
 #include "qemu/error-report.h"
 #include "qemu/module.h"
 #include "qemu/units.h"
@@ -17,15 +18,18 @@
 #include "hw/arm/boot.h"
 #include "hw/arm/rk3588.h"
 #include "hw/core/boards.h"
+#include "hw/core/loader.h"
 #include "hw/core/qdev-properties.h"
 #include "hw/core/qdev-properties-system.h"
 #include "hw/ssi/ssi.h"
+#include "hw/timer/rockchip-hptimer.h"
 #include "system/address-spaces.h"
 #include "system/block-backend.h"
 #include "system/blockdev.h"
 #include "system/device_tree.h"
 #include "system/kvm.h"
 #include "system/qtest.h"
+#include "system/reset.h"
 #include "system/system.h"
 
 #define TYPE_ROCK5B_MACHINE MACHINE_TYPE_NAME("rock-5b")
@@ -41,6 +45,10 @@ static const MemMapEntry rock5b_memmap[] = {
     [ROCK5B_HIGH_RAM] = { 0x100000000, 0x310000000 },
 };
 
+#define ROCK5B_SPL_ADDR   0x00000000
+#define ROCK5B_SPL_SIZE   (4 * MiB)
+#define ROCK5B_BOOTSOURCE_ADDR (RK3588_SRAM_BASE + 0x10)
+#define ROCK5B_BOOTSOURCE_ID  3 /* BROM_BOOTSOURCE_FSPI_M0 */
 #define ROCK5B_FLASH_SIZE (16 * MiB)
 /* Match the low-memory reservation made by Rockchip U-Boot for BL31. */
 #define ROCK5B_FIRMWARE_RESERVED_SIZE (2 * MiB)
@@ -58,6 +66,29 @@ struct Rock5BMachineState {
     struct arm_boot_info binfo;
 };
 
+static void rock5b_reset(void *opaque)
+{
+    Rock5BMachineState *s = opaque;
+
+    /* Restore the firmware handoff state before entering SPL at EL2. */
+    address_space_stl_le(&address_space_memory, ROCK5B_BOOTSOURCE_ADDR,
+                         ROCK5B_BOOTSOURCE_ID, MEMTXATTRS_UNSPECIFIED, NULL);
+    address_space_stl_le(&address_space_memory,
+                         RK3588_HPTIMER_BASE + ROCKCHIP_HPTIMER_CTRL,
+                         ROCKCHIP_HPTIMER_ENABLE, MEMTXATTRS_UNSPECIFIED, 
NULL);
+
+    for (unsigned int i = 0; i < RK3588_NUM_CPUS; i++) {
+        ARMCPU *cpu = &s->soc.cpu[i];
+
+        cpu_reset(CPU(cpu));
+        arm_emulate_firmware_reset(CPU(cpu), 2);
+        arm_rebuild_hflags(&cpu->env);
+        if (i == 0) {
+            cpu_set_pc(CPU(cpu), ROCK5B_SPL_ADDR);
+        }
+    }
+}
+
 /*
  * The first -serial option drives the debug console; further options
  * connect to the remaining UARTs in index order.
@@ -128,6 +159,35 @@ static void rock5b_modify_dtb(const struct arm_boot_info 
*binfo, void *fdt)
     }
 }
 
+static void rock5b_firmware_boot(Rock5BMachineState *s)
+{
+    MachineState *ms = MACHINE(s);
+
+    for (unsigned int i = 0; i < RK3588_NUM_CPUS; i++) {
+        object_property_set_int(OBJECT(&s->soc.cpu[i]), "psci-conduit",
+                                QEMU_PSCI_CONDUIT_SMC, &error_abort);
+    }
+
+    if (ms->firmware) {
+        g_autofree char *filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
+                                                  ms->firmware);
+        ssize_t size;
+
+        if (!filename) {
+            error_report("rock-5b: cannot find SPL '%s'", ms->firmware);
+            exit(EXIT_FAILURE);
+        }
+        size = load_image_targphys(filename, ROCK5B_SPL_ADDR,
+                                   ROCK5B_SPL_SIZE, &error_fatal);
+        if (size <= 0) {
+            error_report("rock-5b: SPL image is empty");
+            exit(EXIT_FAILURE);
+        }
+    }
+    /* Restore the firmware handoff state after device reset. */
+    qemu_register_reset(rock5b_reset, s);
+}
+
 /*
  * arm_load_kernel() enters Linux at EL2, keeps the SMC PSCI conduit and
  * registers the CPU reset handlers.
@@ -157,20 +217,6 @@ static void rock5b_init(MachineState *ms)
         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",
@@ -197,7 +243,29 @@ static void rock5b_init(MachineState *ms)
 
     rock5b_create_flash(s);
 
-    rock5b_direct_boot(s);
+    if (ms->kernel_filename) {
+        if (ms->firmware) {
+            error_report("rock-5b: -bios and -kernel cannot be combined");
+            exit(EXIT_FAILURE);
+        }
+        if (!ms->dtb) {
+            error_report("rock-5b: direct Linux boot requires -dtb");
+            exit(EXIT_FAILURE);
+        }
+        rock5b_direct_boot(s);
+    } else {
+        if (!ms->firmware && !qtest_enabled()) {
+            error_report("rock-5b: supply a U-Boot SPL image with -bios "
+                         "or a Linux kernel with -kernel");
+            exit(EXIT_FAILURE);
+        }
+        if (ms->dtb) {
+            error_report("rock-5b: -dtb requires -kernel; U-Boot proper "
+                         "passes the device tree from the FIT");
+            exit(EXIT_FAILURE);
+        }
+        rock5b_firmware_boot(s);
+    }
 }
 
 static void rock5b_class_init(ObjectClass *oc, const void *data)
-- 
2.53.0


Reply via email to