Skip to contentSkip to conversation

Hexagon: Fix virt boot

PATCH Superseded Brian Cain 2 participants 7 messages Nov 9

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

cover Hexagon: Fix virt boot

The virt machine -bios option didn't work for h2 loadlinux. Refactor virt to use the logic in hex-board which works for loadlinux boots on the machines which use hex-board.

Brian Cain (3):
hw/hexagon: share the Hexagon VM guest boot code hw/hexagon/virt: fix firmware boot hw/hexagon: fix FDT address and size cells

 include/hw/hexagon/hex-board.h |  35 +++++++
 hw/hexagon/hex-board.c         | 151 ++++++++++++++++++++++++++--
 hw/hexagon/qcom_dsp.c          | 178 +++------------------------------
 hw/hexagon/virt.c              |  79 ++++++++++++---
 tests/qtest/boot-serial-test.c |   5 +-
 5 files changed, 261 insertions(+), 187 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 hw/hexagon: share the Hexagon VM guest boot code

The DSP subsystem machines boot a guest kernel through the hypervisor firmware. Move that code, the DT nodes, guest timer, into hex-board so that it can be used by virt.

No functional change, this will be used by a subsequent commit.

---
 include/hw/hexagon/hex-board.h |  35 +++++++
 hw/hexagon/hex-board.c         | 138 +++++++++++++++++++++++++
 hw/hexagon/qcom_dsp.c          | 178 +++------------------------------
 3 files changed, 187 insertions(+), 164 deletions(-)
include/hw/hexagon/hex-board.h +35 -0
index 93a31cb2ecc..5cdea79ea84 100644
@@ -53,6 +53,41 @@ uint64_t hex_board_load_elf(const char *filename,
                             uint64_t (*translate)(void *opaque, uint64_t addr),
                             void *opaque, uint64_t *high);
 
+/* The most RAM that a guest of the Hexagon VM host maps for its kernel. */
+#define HEX_BOARD_MAX_KERNEL_RAM (896 * MiB)
+
+/* Add a /memory node for @size bytes of RAM at @base. */
+void hex_board_fdt_add_memory(MachineState *ms, hwaddr base, hwaddr size);
+
+/*
+ * Add the node that tells a guest's timer driver which interrupt to expect.
+ * The Hexagon VM host provides the timer itself through hypercalls, so
+ * nothing backs the window at @base.
+ */
+void hex_board_fdt_add_vm_timer(MachineState *ms, hwaddr base, hwaddr size,
+                                int irq);
+
+/*
+ * Boot a guest kernel through the Hexagon VM host firmware (H2).  Load the
+ * firmware ELF @firmware as it is linked and the kernel ELF named by -kernel
+ * relocated by @kernel_addr.  Place the device tree, whose size is
+ * @fdt_size, after the kernel, and the -initrd after that, in the RAM
+ * that spans from @ddr_base for the machine's RAM size.  Write a boot stub
+ * at @stub_base that passes the device tree address in r1:r0 and enters the
+ * firmware, and return @stub_base, where CPU 0 starts.  The device tree
+ * address is returned in @fdt_addr.  Exits on failure.
+ */
+uint64_t hex_board_load_vm_guest(MachineState *ms, const char *firmware,
+                                 hwaddr ddr_base, hwaddr kernel_addr,
+                                 hwaddr stub_base, int fdt_size,
+                                 hwaddr *fdt_addr);
+
+/*
+ * Reject -initrd unless the guest boots through firmware, which is when
+ * @firmware is set and -kernel is given.
+ */
+void hex_board_check_initrd(MachineState *ms, bool firmware);
+
 /* Copy the device tree to @addr and refresh its rng-seed on reset. */
 void hex_board_load_fdt(MachineState *ms, int fdt_size, hwaddr addr);
 
hw/hexagon/hex-board.c +138 -0

150 lines not on this page · raw message

hw/hexagon/qcom_dsp.c +14 -164

227 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] hw/hexagon: share the Hexagon VM guest boot code

Quoted 4 lines · Brian Cain

The DSP subsystem machines boot a guest kernel through the hypervisor firmware. Move that code, the DT nodes, guest timer, into hex-board so that it can be used by virt.

Quoted 9 lines

No functional change, this will be used by a subsequent commit.

---
 include/hw/hexagon/hex-board.h |  35 +++++++
 hw/hexagon/hex-board.c         | 138 +++++++++++++++++++++++++
 hw/hexagon/qcom_dsp.c          | 178 +++------------------------------
 3 files changed, 187 insertions(+), 164 deletions(-)
author
#3 lore raw

PATCH 2/3 hw/hexagon/virt: fix firmware boot

In reply to Brian Cain

-bios on virt only loaded a raw image of at most 64 KiB at address zero. This is useless for a typical case like the h2 hypervisor, usually ~hundreds of kb.

Boot a kernel given with -kernel through the firmware given with -bios the way the DSP subsystem machines do: relocate the kernel into the window at 0xa0000000, place the device tree and initrd after it, and enter the firmware through a boot stub that passes the device tree in r1:r0.

Fixes 88a8bc7f43ff ("hw/hexagon: Define hexagon "virt" machine")
---
 hw/hexagon/virt.c              | 79 +++++++++++++++++++++++++++-------
 tests/qtest/boot-serial-test.c |  5 ++-
 2 files changed, 67 insertions(+), 17 deletions(-)
hw/hexagon/virt.c +64 -15
index d8e91770595..a38c5972d5f 100644
@@ -7,11 +7,13 @@
 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
+#include "elf.h"
 #include "hw/hexagon/virt.h"
 #include "hw/hexagon/hexagon.h"
 #include "hw/hexagon/hex-board.h"
 #include "hw/hexagon/hex-subsys.h"
 #include "hw/core/loader.h"
+#include "qemu/datadir.h"
 #include "qemu/error-report.h"
 #include "qemu/target-info.h"
 #include "qemu/units.h"
@@ -22,6 +24,8 @@ enum {
     VIRT_UART0,
     VIRT_MMIO,
     VIRT_FDT,
+    VIRT_BOOT,
+    VIRT_GPT,
 };
 
 /*
@@ -30,11 +34,17 @@ enum {
  */
 static const int VIRTIO_IRQ_BASE = 16;
 static const int VIRT_UART0_IRQ = 15;
+static const int VIRT_GPT_IRQ = 12;
+
+/* Where a guest kernel loaded through the Hexagon VM host is placed. */
+#define VIRT_KERNEL_ADDR 0xa0000000
 
 static const MemMapEntry base_memmap[] = {
-    [VIRT_UART0] = { 0x10000000, 0x00000200 },
+    [VIRT_UART0] = { 0x10000000, 0x00001000 },
     [VIRT_MMIO] = { 0x11000000, 0x00001000 },
     [VIRT_FDT] = { 0x99800000, 0x00400000 },
+    [VIRT_BOOT] = { 0x99c00000, 0x00000200 },
+    [VIRT_GPT] = { 0xab000000, 0x00001000 },
 };
 
 
@@ -43,20 +53,35 @@ static uint64_t load_kernel(const HexagonVirtMachineState *vms)
     return hex_board_load_elf(MACHINE(vms)->kernel_filename, NULL, NULL, NULL);
 }
 
-static uint64_t load_bios(HexagonVirtMachineState *vms)
+static bool is_elf(const char *filename)
 {
-    MachineState *ms = MACHINE(vms);
-    uint64_t bios_addr = 0x0;  /* Load BIOS at reset vector address 0x0 */
-    int bios_size;
+    uint8_t magic[SELFMAG];
+    int fd = qemu_open_old(filename, O_RDONLY | O_BINARY);
+    bool elf;
 
-    bios_size = load_image_targphys(ms->firmware ?: "",
-                                    bios_addr, 64 * 1024, NULL);
-    if (bios_size < 0) {
-        error_report("Could not load BIOS '%s'", ms->firmware ?: "");
+    if (fd < 0) {
+        return false;
+    }
+    elf = read(fd, magic, sizeof(magic)) == sizeof(magic) &&
+          memcmp(magic, ELFMAG, SELFMAG) == 0;
+    qemu_close(fd);
+    return elf;
+}
+
+/*
+ * An ELF firmware starts at its entry point.  Any other firmware is a raw
+ * image that starts at the reset vector, address zero, where it is loaded.
+ */
+static uint64_t load_bios(MachineState *ms, const char *filename)
+{
+    if (is_elf(filename)) {
+        return hex_board_load_elf(filename, NULL, NULL, NULL);
+    }
+    if (load_image_targphys(filename, 0, ms->ram_size, NULL) < 0) {
+        error_report("Could not load BIOS '%s'", filename);
         exit(1);
     }
-
-    return bios_addr;  /* Return entry point at address 0x0 */
+    return 0;
 }
 
 static void virt_init(MachineState *ms)
@@ -66,9 +91,23 @@ static void virt_init(MachineState *ms)
     int32_t l2vic_phandle;
     uint64_t start = 0;
     bool have_start = false;
+    hwaddr fdt_addr = base_memmap[VIRT_FDT].base;
+    g_autofree char *firmware = NULL;
+    bool vm_guest;
+
+    if (ms->firmware) {
+        firmware = qemu_find_file(QEMU_FILE_TYPE_BIOS, ms->firmware);
+        if (!firmware) {
+            error_report("Could not find BIOS '%s'", ms->firmware);
+            exit(1);
+        }
+    }
+    hex_board_check_initrd(ms, firmware);
+    vm_guest = ms->kernel_filename && firmware;
 
     hex_board_create_fdt(ms, "hexagon-virt,qemu", "qcom,sm8150",
                          &vms->fdt_size);
+    qemu_fdt_setprop_string(ms->fdt, "/soc", "compatible", "simple-bus");
     qemu_fdt_setprop_string(ms->fdt, "/chosen", "bootargs", ms->kernel_cmdline);
 
     hex_subsys_create(&vms->parent_obj, m_cfg, v68_rev);
@@ -80,11 +119,21 @@ static void virt_init(MachineState *ms)
                             base_memmap[VIRT_MMIO].size, VIRTIO_IRQ_BASE,
                             VIRTIO_DEV_COUNT, l2vic_phandle);
 
-    if (ms->kernel_filename) {
+    if (vm_guest) {
+        start = hex_board_load_vm_guest(ms, firmware, 0, VIRT_KERNEL_ADDR,
+                                        base_memmap[VIRT_BOOT].base,
+                                        vms->fdt_size, &fdt_addr);
+        hex_board_fdt_add_memory(ms, VIRT_KERNEL_ADDR,
+                                 MIN(ms->ram_size - VIRT_KERNEL_ADDR,
+                                     HEX_BOARD_MAX_KERNEL_RAM));
+        hex_board_fdt_add_vm_timer(ms, base_memmap[VIRT_GPT].base,
+                                   base_memmap[VIRT_GPT].size, VIRT_GPT_IRQ);
+        have_start = true;
+    } else if (ms->kernel_filename) {
         start = load_kernel(vms);
         have_start = true;
-    } else if (ms->firmware) {
-        start = load_bios(vms);
+    } else if (firmware) {
+        start = load_bios(ms, firmware);
         have_start = true;
     }
     hex_board_create_cpus(ms, have_start, start);
@@ -94,7 +143,7 @@ static void virt_init(MachineState *ms)
                           base_memmap[VIRT_UART0].size, VIRT_UART0_IRQ,
                           l2vic_phandle);
 
-    hex_board_load_fdt(ms, vms->fdt_size, base_memmap[VIRT_FDT].base);
+    hex_board_load_fdt(ms, vms->fdt_size, fdt_addr);
 }
 
 
tests/qtest/boot-serial-test.c +3 -2
index 37fee7a91c4..e5d3e155d15 100644
@@ -146,7 +146,7 @@ static const uint8_t bios_hexagon[] = {
     0x00, 0x40, 0x00, 0x01,                 /* immext(#0x10000000) */
     0x00, 0xc0, 0x00, 0x78,                 /* r0 = ##0x10000000 */
     0x54, 0xc0, 0x00, 0x3c,                 /* memb(r0+#0) = #0x54 Write 'T' */
-    0xf8, 0xff, 0xff, 0x59                  /* jump 0x0 ; Loop back to start */
+    0xfa, 0xff, 0xff, 0x59                  /* jump _start */
 };
 
 typedef struct testdef {
@@ -201,7 +201,8 @@ static const testdef_t tests[] = {
     { "arm", "microbit", "", "T", sizeof(kernel_nrf51), kernel_nrf51 },
     { "arm", "stm32vldiscovery", "", "T",
       sizeof(kernel_stm32vldiscovery), kernel_stm32vldiscovery },
-    { "hexagon", "virt", "", "TT", sizeof(bios_hexagon), NULL, bios_hexagon },
+    { "hexagon", "virt", "", "TT", sizeof(bios_hexagon), NULL,
+      bios_hexagon },
 
     { NULL }
 };
Signature · 1 line
2.34.1
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] hw/hexagon/virt: fix firmware boot

Quoted 9 lines · Brian Cain

-bios on virt only loaded a raw image of at most 64 KiB at address zero. This is useless for a typical case like the h2 hypervisor, usually ~hundreds of kb.

Boot a kernel given with -kernel through the firmware given with -bios the way the DSP subsystem machines do: relocate the kernel into the window at 0xa0000000, place the device tree and initrd after it, and enter the firmware through a boot stub that passes the device tree in r1:r0.

Quoted 7 lines
Fixes 88a8bc7f43ff ("hw/hexagon: Define hexagon "virt" machine")
---
 hw/hexagon/virt.c              | 79 +++++++++++++++++++++++++++-------
 tests/qtest/boot-serial-test.c |  5 ++-
 2 files changed, 67 insertions(+), 17 deletions(-)
author
#5 lore raw

PATCH 3/3 hw/hexagon: fix FDT address and size cells

In reply to Brian Cain

Hexagon MMUs support 64-bit physical addresses. Describe the root and /soc buses with two address cells and two size cells so their child reg properties can represent the full address and size range.

---
 hw/hexagon/hex-board.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
hw/hexagon/hex-board.c +7 -6
index c3b8c647e4d..f2d8b76f6c3 100644
@@ -43,13 +43,13 @@ void hex_board_create_fdt(MachineState *ms, const char *model,
     ms->fdt = fdt;
 
     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
-    qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x1);
+    qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
     qemu_fdt_setprop_string(fdt, "/", "model", model);
     qemu_fdt_setprop_string(fdt, "/", "compatible", compatible);
 
     qemu_fdt_add_subnode(fdt, "/soc");
     qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
-    qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x1);
+    qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
     qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
 
     qemu_fdt_add_subnode(fdt, "/chosen");
@@ -78,7 +78,7 @@ int32_t hex_board_fdt_add_l2vic(MachineState *ms,
     qemu_fdt_setprop_cell(ms->fdt, nodename, "#interrupt-cells", 2);
     qemu_fdt_setprop(ms->fdt, nodename, "compatible", compat, sizeof(compat));
     qemu_fdt_setprop_cells(ms->fdt, nodename, "reg", 0, cfg->l2vic_base,
-                           cfg->l2vic_size);
+                           0, cfg->l2vic_size);
     qemu_fdt_setprop(ms->fdt, nodename, "interrupt-controller", NULL, 0);
     qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", l2vic_phandle);
 
@@ -95,7 +95,8 @@ void hex_board_fdt_add_hvx(MachineState *ms,
         qemu_fdt_setprop_string(ms->fdt, "/soc/vtcm", "compatible",
                                 "qcom,hexagon_vtcm");
         qemu_fdt_setprop_cells(ms->fdt, "/soc/vtcm", "reg", 0,
-                               t->vtcm_base << 16, t->vtcm_size_kb * 1024);
+                               t->vtcm_base << 16, 0,
+                               t->vtcm_size_kb * 1024);
     }
 
     if (t->ext_contexts > 0) {
@@ -175,7 +176,7 @@ void hex_board_create_uart(MachineState *ms, hwaddr base, hwaddr size,
     qemu_fdt_add_subnode(ms->fdt, nodename);
     /* Can't use setprop_string because of the embedded NUL */
     qemu_fdt_setprop(ms->fdt, nodename, "compatible", compat, sizeof(compat));
-    qemu_fdt_setprop_cells(ms->fdt, nodename, "reg", 0, base, size);
+    qemu_fdt_setprop_cells(ms->fdt, nodename, "reg", 0, base, 0, size);
     fdt_set_irq(ms, nodename, irq);
     qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                           l2vic_phandle);
@@ -211,7 +212,7 @@ void hex_board_create_virtio(MachineState *ms, hwaddr base, hwaddr size,
         qemu_fdt_add_subnode(ms->fdt, nodename);
         qemu_fdt_setprop_string(ms->fdt, nodename, "compatible",
                                 "virtio,mmio");
-        qemu_fdt_setprop_cells(ms->fdt, nodename, "reg", 0, slot, size);
+        qemu_fdt_setprop_cells(ms->fdt, nodename, "reg", 0, slot, 0, size);
         fdt_set_irq(ms, nodename, irq);
         qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                               l2vic_phandle);
Signature · 1 line
2.34.1
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] hw/hexagon: fix FDT address and size cells

Quoted 9 lines · Brian Cain

Hexagon MMUs support 64-bit physical addresses. Describe the root and /soc buses with two address cells and two size cells so their child reg properties can represent the full address and size range.

---
 hw/hexagon/hex-board.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)