Skip to contentSkip to conversation

MIPS Bootloader helper

PATCH Superseded Jiaxun Yang 3 participants 28 messages Dec 15, 2020 – Jan 27, 2021 Arm/Amlogic Meson SoC support

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover MIPS Bootloader helper

v2:
A big reconstruction. rewrite helpers with CPU feature and sepreate changesets.

Jiaxun Yang (8):
hw/mips: Make bootloader addresses unsgined hw/mips/malta: Use address translation helper to calculate
bootloader_run_addr
hw/mips: Use address translation helper to handle ENVP_ADDR hw/mips: Add a bootloader helper
hw/mips: Use bl_gen_kernel_jump to generate bootloaders target/mips/addr: Add translation helpers for KSEG1 hw/mips/malta: Use bootloader helper to set BAR resgiters hw/mips/boston: Use bootloader helper to set GCRs

 hw/mips/bootloader.c         | 157 ++++++++++++++++++++++++++++++++
 hw/mips/boston.c             |  62 +++----------
 hw/mips/fuloong2e.c          |  48 +++-------
 hw/mips/malta.c              | 171 ++++++++++++-----------------------
 hw/mips/meson.build          |   2 +-
 include/hw/mips/bootloader.h |  48 ++++++++++
 target/mips/addr.c           |  10 ++
 target/mips/cpu.h            |   2 +
 8 files changed, 306 insertions(+), 194 deletions(-)
 create mode 100644 hw/mips/bootloader.c
 create mode 100644 include/hw/mips/bootloader.h
Signature · 1 line
2.29.2
#1 lore raw

PATCH v2 1/8 hw/mips: Make bootloader addresses unsgined

Address should be unsigned anyway, otherwise it may carry calculations wrongly.

---
 hw/mips/fuloong2e.c | 12 ++++++------
 hw/mips/malta.c     | 22 +++++++++++-----------
 2 files changed, 17 insertions(+), 17 deletions(-)
hw/mips/fuloong2e.c +6 -6
index 45c596f4fe..fc4d7f21ed 100644
@@ -107,9 +107,9 @@ static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t *prom_buf, int index,
     va_end(ap);
 }
 
-static int64_t load_kernel(CPUMIPSState *env)
+static uint64_t load_kernel(CPUMIPSState *env)
 {
-    int64_t kernel_entry, kernel_high, initrd_size;
+    uint64_t kernel_entry, kernel_high, initrd_size;
     int index = 0;
     long kernel_size;
     ram_addr_t initrd_offset;
@@ -118,8 +118,8 @@ static int64_t load_kernel(CPUMIPSState *env)
 
     kernel_size = load_elf(loaderparams.kernel_filename, NULL,
                            cpu_mips_kseg0_to_phys, NULL,
-                           (uint64_t *)&kernel_entry, NULL,
-                           (uint64_t *)&kernel_high, NULL,
+                           &kernel_entry, NULL,
+                           &kernel_high, NULL,
                            0, EM_MIPS, 1, 0);
     if (kernel_size < 0) {
         error_report("could not load kernel '%s': %s",
@@ -180,7 +180,7 @@ static int64_t load_kernel(CPUMIPSState *env)
 }
 
 static void write_bootloader(CPUMIPSState *env, uint8_t *base,
-                             int64_t kernel_addr)
+                             uint64_t kernel_addr)
 {
     uint32_t *p;
 
@@ -294,7 +294,7 @@ static void mips_fuloong2e_init(MachineState *machine)
     MemoryRegion *bios = g_new(MemoryRegion, 1);
     long bios_size;
     uint8_t *spd_data;
-    int64_t kernel_entry;
+    uint64_t kernel_entry;
     PCIDevice *pci_dev;
     PCIBus *pci_bus;
     ISABus *isa_bus;
hw/mips/malta.c +11 -11
index 366f4fdfcd..7db009a3e9 100644
@@ -616,8 +616,8 @@ static void network_init(PCIBus *pci_bus)
     }
 }
 
-static void write_bootloader_nanomips(uint8_t *base, int64_t run_addr,
-                                      int64_t kernel_entry)
+static void write_bootloader_nanomips(uint8_t *base, uint64_t run_addr,
+                                      uint64_t kernel_entry)
 {
     uint16_t *p;
 
@@ -840,8 +840,8 @@ static void write_bootloader_nanomips(uint8_t *base, int64_t run_addr,
  *   a2 - 32-bit address of the environment variables table
  *   a3 - RAM size in bytes
  */
-static void write_bootloader(uint8_t *base, int64_t run_addr,
-                             int64_t kernel_entry)
+static void write_bootloader(uint8_t *base, uint64_t run_addr,
+                             uint64_t kernel_entry)
 {
     uint32_t *p;
 
@@ -1003,7 +1003,7 @@ static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t *prom_buf, int index,
                                         const char *string, ...)
 {
     va_list ap;
-    int32_t table_addr;
+    uint32_t table_addr;
 
     if (index >= ENVP_NB_ENTRIES) {
         return;
@@ -1014,7 +1014,7 @@ static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t *prom_buf, int index,
         return;
     }
 
-    table_addr = sizeof(int32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE;
+    table_addr = sizeof(uint32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE;
     prom_buf[index] = tswap32(ENVP_ADDR + table_addr);
 
     va_start(ap, string);
@@ -1023,9 +1023,9 @@ static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t *prom_buf, int index,
 }
 
 /* Kernel */
-static int64_t load_kernel(void)
+static uint64_t load_kernel(void)
 {
-    int64_t kernel_entry, kernel_high, initrd_size;
+    uint64_t kernel_entry, kernel_high, initrd_size;
     long kernel_size;
     ram_addr_t initrd_offset;
     int big_endian;
@@ -1042,8 +1042,8 @@ static int64_t load_kernel(void)
 
     kernel_size = load_elf(loaderparams.kernel_filename, NULL,
                            cpu_mips_kseg0_to_phys, NULL,
-                           (uint64_t *)&kernel_entry, NULL,
-                           (uint64_t *)&kernel_high, NULL, big_endian, EM_MIPS,
+                           &kernel_entry, NULL,
+                           &kernel_high, NULL, big_endian, EM_MIPS,
                            1, 0);
     if (kernel_size < 0) {
         error_report("could not load kernel '%s': %s",
@@ -1234,7 +1234,7 @@ void mips_malta_init(MachineState *machine)
     MemoryRegion *bios, *bios_copy = g_new(MemoryRegion, 1);
     const size_t smbus_eeprom_size = 8 * 256;
     uint8_t *smbus_eeprom_buf = g_malloc0(smbus_eeprom_size);
-    int64_t kernel_entry, bootloader_run_addr;
+    uint64_t kernel_entry, bootloader_run_addr;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     qemu_irq cbus_irq, i8259_irq;
Signature · 1 line
2.29.2
#3 lore raw

Re: [PATCH v2 1/8] hw/mips: Make bootloader addresses unsgined

Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Jiaxun Yang

Address should be unsigned anyway, otherwise it may carry calculations wrongly.

---
 hw/mips/fuloong2e.c | 12 ++++++------
 hw/mips/malta.c     | 22 +++++++++++-----------
 2 files changed, 17 insertions(+), 17 deletions(-)

Can you send a follow-up patch for hw/mips/mipssim.c?

#4 lore raw

Re: [PATCH v2 1/8] hw/mips: Make bootloader addresses unsgined

Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Jiaxun Yang

Address should be unsigned anyway, otherwise it may carry calculations wrongly.

---
 hw/mips/fuloong2e.c | 12 ++++++------
 hw/mips/malta.c     | 22 +++++++++++-----------
 2 files changed, 17 insertions(+), 17 deletions(-)
Quoted 5 lines
Quoted 1 line

Can you send a follow-up patch for hw/mips/mipssim.c?

Bah as it is trivial, I will squash in your patch.

#5 lore raw

PATCH v2 2/8 hw/mips/malta: Use address translation helper to calculate bootloader_run_addr

In reply to Jiaxun Yang

So it will sign extend adresses properly.

---
 hw/mips/malta.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/mips/malta.c +2 -2
index 7db009a3e9..1fbb8a3220 100644
@@ -1302,9 +1302,9 @@ void mips_malta_init(MachineState *machine)
         /* For KVM we reserve 1MB of RAM for running bootloader */
         if (kvm_enabled()) {
             ram_low_size -= 0x100000;
-            bootloader_run_addr = 0x40000000 + ram_low_size;
+            bootloader_run_addr = cpu_mips_kvm_um_phys_to_kseg0(NULL, ram_low_size);
         } else {
-            bootloader_run_addr = 0xbfc00000;
+            bootloader_run_addr = cpu_mips_phys_to_kseg0(NULL, RESET_ADDRESS);
         }
 
         /* Write a small bootloader to the flash location. */
Signature · 1 line
2.29.2
#7 lore raw

PATCH v2 3/8 hw/mips: Use address translation helper to handle ENVP_ADDR

In reply to Jiaxun Yang

It will signed extend vaddr properly.

---
 hw/mips/fuloong2e.c | 24 +++++++++---------
 hw/mips/malta.c     | 62 ++++++++++++++++++++++-----------------------
 2 files changed, 43 insertions(+), 43 deletions(-)
hw/mips/fuloong2e.c +12 -12
index fc4d7f21ed..64cd4b98ca 100644
@@ -49,7 +49,8 @@
 
 #define DEBUG_FULOONG2E_INIT
 
-#define ENVP_ADDR               0x80002000l
+#define ENVP_PADDR              0x2000
+#define ENVP_VADDR              cpu_mips_phys_to_kseg0(NULL, ENVP_PADDR)
 #define ENVP_NB_ENTRIES         16
 #define ENVP_ENTRY_SIZE         256
 
@@ -100,7 +101,7 @@ static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t *prom_buf, int index,
     }
 
     table_addr = sizeof(int32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE;
-    prom_buf[index] = tswap32(ENVP_ADDR + table_addr);
+    prom_buf[index] = tswap32(ENVP_VADDR + table_addr);
 
     va_start(ap, string);
     vsnprintf((char *)prom_buf + table_addr, ENVP_ENTRY_SIZE, string, ap);
@@ -172,8 +173,7 @@ static uint64_t load_kernel(CPUMIPSState *env)
     prom_set(prom_buf, index++, "modetty0=38400n8r");
     prom_set(prom_buf, index++, NULL);
 
-    rom_add_blob_fixed("prom", prom_buf, prom_size,
-                       cpu_mips_kseg0_to_phys(NULL, ENVP_ADDR));
+    rom_add_blob_fixed("prom", prom_buf, prom_size, ENVP_PADDR);
 
     g_free(prom_buf);
     return kernel_entry;
@@ -199,14 +199,14 @@ static void write_bootloader(CPUMIPSState *env, uint8_t *base,
     stl_p(p++, 0x3c040000);
     /* ori a0, a0, 2 */
     stl_p(p++, 0x34840002);
-    /* lui a1, high(ENVP_ADDR) */
-    stl_p(p++, 0x3c050000 | ((ENVP_ADDR >> 16) & 0xffff));
-    /* ori a1, a0, low(ENVP_ADDR) */
-    stl_p(p++, 0x34a50000 | (ENVP_ADDR & 0xffff));
-    /* lui a2, high(ENVP_ADDR + 8) */
-    stl_p(p++, 0x3c060000 | (((ENVP_ADDR + 8) >> 16) & 0xffff));
-    /* ori a2, a2, low(ENVP_ADDR + 8) */
-    stl_p(p++, 0x34c60000 | ((ENVP_ADDR + 8) & 0xffff));
+    /* lui a1, high(ENVP_VADDR) */
+    stl_p(p++, 0x3c050000 | ((ENVP_VADDR >> 16) & 0xffff));
+    /* ori a1, a0, low(ENVP_VADDR) */
+    stl_p(p++, 0x34a50000 | (ENVP_VADDR & 0xffff));
+    /* lui a2, high(ENVP_VADDR + 8) */
+    stl_p(p++, 0x3c060000 | (((ENVP_VADDR + 8) >> 16) & 0xffff));
+    /* ori a2, a2, low(ENVP_VADDR + 8) */
+    stl_p(p++, 0x34c60000 | ((ENVP_VADDR + 8) & 0xffff));
     /* lui a3, high(env->ram_size) */
     stl_p(p++, 0x3c070000 | (loaderparams.ram_size >> 16));
     /* ori a3, a3, low(env->ram_size) */
hw/mips/malta.c +31 -31

100 lines not on this page · raw message

Signature · 1 line
2.29.2
#9 lore raw

Re: [PATCH v2 3/8] hw/mips: Use address translation helper to handle ENVP_ADDR

Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Jiaxun Yang

It will signed extend vaddr properly.

---
 hw/mips/fuloong2e.c | 24 +++++++++---------
 hw/mips/malta.c     | 62 ++++++++++++++++++++++-----------------------
 2 files changed, 43 insertions(+), 43 deletions(-)
#10 lore raw

PATCH v2 4/8 hw/mips: Add a bootloader helper

In reply to Jiaxun Yang

Add a bootloader helper to generate simple bootloaders for kernel. It can help us reduce inline hex hack and also keep MIPS release 6 compatibility easier.

---
 hw/mips/bootloader.c         | 157 +++++++++++++++++++++++++++++++++++
 hw/mips/meson.build          |   2 +-
 include/hw/mips/bootloader.h |  48 +++++++++++
 3 files changed, 206 insertions(+), 1 deletion(-)
 create mode 100644 hw/mips/bootloader.c
 create mode 100644 include/hw/mips/bootloader.h
hw/mips/bootloader.c new file +157 -0
new file mode 100644
index 0000000000..97916ffd24
@@ -0,0 +1,157 @@
+/*
+ * Utility for QEMU MIPS to generate it's simple bootloader
+ *
+ * Instructions used here are carefully selected to keep compatibility with
+ * MIPS Release 6.
+ *
+ * Copyright (C) 2020 Jiaxun Yang <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/bitops.h"
+#include "cpu.h"
+#include "hw/mips/bootloader.h"
+
+/* Base types */
+static void bl_gen_nop(uint32_t **p)
+{
+    stl_p(*p, 0);
+    *p = *p + 1;
+}
+
+static void bl_gen_r_type(uint32_t **p, uint8_t opcode, bl_reg rs, bl_reg rt,
+                            bl_reg rd, uint8_t shift, uint8_t funct)
+{
+    uint32_t insn = 0;
+
+    insn = deposit32(insn, 26, 6, opcode);
+    insn = deposit32(insn, 21, 5, rs);
+    insn = deposit32(insn, 16, 5, rt);
+    insn = deposit32(insn, 11, 5, rd);
+    insn = deposit32(insn, 6, 5, shift);
+    insn = deposit32(insn, 0, 6, funct);
+
+    stl_p(*p, insn);
+    *p = *p + 1;
+}
+
+static void bl_gen_i_type(uint32_t **p, uint8_t opcode, bl_reg rs, bl_reg rt,
+                            uint16_t imm)
+{
+    uint32_t insn = 0;
+
+    insn = deposit32(insn, 26, 6, opcode);
+    insn = deposit32(insn, 21, 5, rs);
+    insn = deposit32(insn, 16, 5, rt);
+    insn = deposit32(insn, 0, 16, imm);
+
+    stl_p(*p, insn);
+    *p = *p + 1;
+}
+
+static bool bootcpu_supports_isa(uint64_t isa_mask)
+{
+    return cpu_supports_isa(&MIPS_CPU(first_cpu)->env, isa_mask);
+}
+
+/* Single instructions */
+static void bl_gen_dsll(uint32_t **p, bl_reg rd, bl_reg rt, uint8_t sa)
+{
+    if (bootcpu_supports_isa(ISA_MIPS3)) {
+        bl_gen_r_type(p, 0, 0, rt, rd, sa, 0x38);
+    } else {
+        g_assert_not_reached(); /* unsupported */
+    }
+}
+
+static void bl_gen_jalr(uint32_t **p, bl_reg rs)
+{
+    bl_gen_r_type(p, 0, rs, 0, BL_REG_RA, 0, 0x9);
+}
+
+static void bl_gen_lui(uint32_t **p, bl_reg rt, uint16_t imm)
+{
+    /* R6: It's a alias of AUI with RS = 0 */
+    bl_gen_i_type(p, 0xf, 0, rt, imm);
+}
+
+static void bl_gen_ori(uint32_t **p, bl_reg rt, bl_reg rs, uint16_t imm)
+{
+    bl_gen_i_type(p, 0xd, rs, rt, imm);
+}
+
+static void bl_gen_sw(uint32_t **p, bl_reg rt, uint8_t base, uint16_t offset)
+{
+    bl_gen_i_type(p, 0x2b, base, rt, offset);
+}
+
+static void bl_gen_sd(uint32_t **p, bl_reg rt, uint8_t base, uint16_t offset)
+{
+    if (bootcpu_supports_isa(ISA_MIPS3)) {
+        bl_gen_i_type(p, 0x3f, base, rt, offset);
+    } else {
+        g_assert_not_reached(); /* unsupported */
+    }
+}
+
+/* Pseudo instructions */
+static void bl_gen_li(uint32_t **p, bl_reg rt, uint32_t imm)
+{
+    bl_gen_lui(p, rt, extract32(imm, 16, 16));
+    bl_gen_ori(p, rt, rt, extract32(imm, 0, 16));
+}
+
+static void bl_gen_dli(uint32_t **p, bl_reg rt, uint64_t imm)
+{
+    bl_gen_li(p, rt, extract64(imm, 32, 32));
+    bl_gen_dsll(p, rt, rt, 16);
+    bl_gen_ori(p, rt, rt, extract64(imm, 16, 16));
+    bl_gen_dsll(p, rt, rt, 16);
+    bl_gen_ori(p, rt, rt, extract64(imm, 0, 16));
+}
+
+static void bl_gen_load_ulong(uint32_t **p, bl_reg rt, target_ulong imm)
+{
+    if (bootcpu_supports_isa(ISA_MIPS3)) {
+        bl_gen_dli(p, rt, imm); /* 64bit */
+    } else {
+        bl_gen_li(p, rt, imm); /* 32bit */
+    }
+}
+
+/* Helpers */
+void bl_gen_jump_to(uint32_t **p, target_ulong jump_addr)
+{
+    bl_gen_load_ulong(p, BL_REG_T9, jump_addr);
+    bl_gen_jalr(p, BL_REG_T9);
+    bl_gen_nop(p); /* delay slot */
+}
+
+void bl_gen_jump_kernel(uint32_t **p, target_ulong sp, target_ulong a0,
+                        target_ulong a1, target_ulong a2, target_ulong a3,
+                        target_ulong kernel_addr)
+{
+    bl_gen_load_ulong(p, BL_REG_SP, sp);
+    bl_gen_load_ulong(p, BL_REG_A0, a0);
+    bl_gen_load_ulong(p, BL_REG_A1, a1);
+    bl_gen_load_ulong(p, BL_REG_A2, a2);
+    bl_gen_load_ulong(p, BL_REG_A3, a3);
+
+    bl_gen_jump_to(p, kernel_addr);
+}
+
+void bl_gen_write_u32(uint32_t **p, uint32_t val, target_ulong addr)
+{
+    bl_gen_li(p, BL_REG_K0, val);
+    bl_gen_load_ulong(p, BL_REG_K1, addr);
+    bl_gen_sw(p, BL_REG_K0, BL_REG_K1, 0x0);
+}
+
+void bl_gen_write_u64(uint32_t **p, uint64_t val, target_ulong addr)
+{
+    bl_gen_dli(p, BL_REG_K0, val);
+    bl_gen_load_ulong(p, BL_REG_K1, addr);
+    bl_gen_sd(p, BL_REG_K0, BL_REG_K1, 0x0);
+}
hw/mips/meson.build +1 -1
index 77b4d8f365..fdb783ff55 100644
@@ -1,5 +1,5 @@
 mips_ss = ss.source_set()
-mips_ss.add(files('mips_int.c'))
+mips_ss.add(files('bootloader.c', 'mips_int.c'))
 mips_ss.add(when: 'CONFIG_FULOONG', if_true: files('fuloong2e.c'))
 mips_ss.add(when: 'CONFIG_JAZZ', if_true: files('jazz.c'))
 mips_ss.add(when: 'CONFIG_MALTA', if_true: files('gt64xxx_pci.c', 'malta.c'))
include/hw/mips/bootloader.h new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.29.2
#11 lore raw

PATCH v2 5/8 hw/mips: Use bl_gen_kernel_jump to generate bootloaders

In reply to Jiaxun Yang

Replace embedded binary with generated code.

---
 hw/mips/boston.c    | 17 ++---------------
 hw/mips/fuloong2e.c | 28 ++++------------------------
 hw/mips/malta.c     | 41 ++++++++++-------------------------------
 3 files changed, 16 insertions(+), 70 deletions(-)
hw/mips/boston.c +2 -15
index c3b94c68e1..b622222c7d 100644
@@ -27,6 +27,7 @@
 #include "hw/ide/ahci.h"
 #include "hw/loader.h"
 #include "hw/loader-fit.h"
+#include "hw/mips/bootloader.h"
 #include "hw/mips/cps.h"
 #include "hw/pci-host/xilinx-pcie.h"
 #include "hw/qdev-clock.h"
@@ -324,21 +325,7 @@ static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr,
      * a2/$6 = 0
      * a3/$7 = 0
      */
-    stl_p(p++, 0x2404fffe);                     /* li   $4, -2 */
-                                                /* lui  $5, hi(fdt_addr) */
-    stl_p(p++, 0x3c050000 | ((fdt_addr >> 16) & 0xffff));
-    if (fdt_addr & 0xffff) {                    /* ori  $5, lo(fdt_addr) */
-        stl_p(p++, 0x34a50000 | (fdt_addr & 0xffff));
-    }
-    stl_p(p++, 0x34060000);                     /* li   $6, 0 */
-    stl_p(p++, 0x34070000);                     /* li   $7, 0 */
-
-    /* Load kernel entry address & jump to it */
-                                                /* lui  $25, hi(kernel_entry) */
-    stl_p(p++, 0x3c190000 | ((kernel_entry >> 16) & 0xffff));
-                                                /* ori  $25, lo(kernel_entry) */
-    stl_p(p++, 0x37390000 | (kernel_entry & 0xffff));
-    stl_p(p++, 0x03200009);                     /* jr   $25 */
+    bl_gen_jump_kernel(&p, 0, (int32_t)-2, fdt_addr, 0, 0, kernel_entry);
 }
 
 static const void *boston_fdt_filter(void *opaque, const void *fdt_orig,
hw/mips/fuloong2e.c +4 -24
index 64cd4b98ca..9b0eb8a314 100644
@@ -33,6 +33,7 @@
 #include "hw/i2c/smbus_eeprom.h"
 #include "hw/block/flash.h"
 #include "hw/mips/mips.h"
+#include "hw/mips/bootloader.h"
 #include "hw/mips/cpudevs.h"
 #include "hw/pci/pci.h"
 #include "qemu/log.h"
@@ -195,30 +196,9 @@ static void write_bootloader(CPUMIPSState *env, uint8_t *base,
     /* Second part of the bootloader */
     p = (uint32_t *)(base + 0x040);
 
-    /* lui a0, 0 */
-    stl_p(p++, 0x3c040000);
-    /* ori a0, a0, 2 */
-    stl_p(p++, 0x34840002);
-    /* lui a1, high(ENVP_VADDR) */
-    stl_p(p++, 0x3c050000 | ((ENVP_VADDR >> 16) & 0xffff));
-    /* ori a1, a0, low(ENVP_VADDR) */
-    stl_p(p++, 0x34a50000 | (ENVP_VADDR & 0xffff));
-    /* lui a2, high(ENVP_VADDR + 8) */
-    stl_p(p++, 0x3c060000 | (((ENVP_VADDR + 8) >> 16) & 0xffff));
-    /* ori a2, a2, low(ENVP_VADDR + 8) */
-    stl_p(p++, 0x34c60000 | ((ENVP_VADDR + 8) & 0xffff));
-    /* lui a3, high(env->ram_size) */
-    stl_p(p++, 0x3c070000 | (loaderparams.ram_size >> 16));
-    /* ori a3, a3, low(env->ram_size) */
-    stl_p(p++, 0x34e70000 | (loaderparams.ram_size & 0xffff));
-    /* lui ra, high(kernel_addr) */
-    stl_p(p++, 0x3c1f0000 | ((kernel_addr >> 16) & 0xffff));
-    /* ori ra, ra, low(kernel_addr) */
-    stl_p(p++, 0x37ff0000 | (kernel_addr & 0xffff));
-    /* jr ra */
-    stl_p(p++, 0x03e00008);
-    /* nop */
-    stl_p(p++, 0x00000000);
+    bl_gen_jump_kernel(&p, ENVP_VADDR - 64, 2, ENVP_VADDR,
+                       ENVP_VADDR + 8, loaderparams.ram_size,
+                       kernel_addr);
 }
 
 static void main_cpu_reset(void *opaque)
hw/mips/malta.c +10 -31

65 lines not on this page · raw message

Signature · 1 line
2.29.2
#12 lore raw

Re: [PATCH v2 5/8] hw/mips: Use bl_gen_kernel_jump to generate bootloaders

+Alex

Quoted 30 lines · Jiaxun Yang

Replace embedded binary with generated code.

---
 hw/mips/boston.c    | 17 ++---------------
 hw/mips/fuloong2e.c | 28 ++++------------------------
 hw/mips/malta.c     | 41 ++++++++++-------------------------------
 3 files changed, 16 insertions(+), 70 deletions(-)
hw/mips/boston.c +1 -5
index c3b94c68e1..b622222c7d 100644
@@ -27,6 +27,7 @@
 #include "hw/ide/ahci.h"
 #include "hw/loader.h"
 #include "hw/loader-fit.h"
+#include "hw/mips/bootloader.h"
 #include "hw/mips/cps.h"
 #include "hw/pci-host/xilinx-pcie.h"
 #include "hw/qdev-clock.h"
@@ -324,21 +325,7 @@ static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr,
      * a2/$6 = 0
      * a3/$7 = 0
      */
-    stl_p(p++, 0x2404fffe);                     /* li   $4, -2 */
-                                                /* lui  $5, hi(fdt_addr) */
-    stl_p(p++, 0x3c050000 | ((fdt_addr >> 16) & 0xffff));
-    if (fdt_addr & 0xffff) {                    /* ori  $5, lo(fdt_addr) */
-        stl_p(p++, 0x34a50000 | (fdt_addr & 0xffff));

hw/mips/boston.c static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr, · line 341

Quoted 10 lines
hw/mips/boston.c +0 -10
-    }
-    stl_p(p++, 0x34060000);                     /* li   $6, 0 */
-    stl_p(p++, 0x34070000);                     /* li   $7, 0 */
-
-    /* Load kernel entry address & jump to it */
-                                                /* lui  $25, hi(kernel_entry) */
-    stl_p(p++, 0x3c190000 | ((kernel_entry >> 16) & 0xffff));
-                                                /* ori  $25, lo(kernel_entry) */
-    stl_p(p++, 0x37390000 | (kernel_entry & 0xffff));
-    stl_p(p++, 0x03200009);                     /* jr   $25 */

Eh, no delay slot NOP :)

Quoted 3 lines
hw/mips/boston.c +1 -0
+    bl_gen_jump_kernel(&p, 0, (int32_t)-2, fdt_addr, 0, 0, kernel_entry);
 }
 

...

Quoted 54 lines
hw/mips/malta.c +2 -24
index 9afc0b427b..ffd67b8293 100644
@@ -37,6 +37,7 @@
 #include "hw/i2c/smbus_eeprom.h"
 #include "hw/block/flash.h"
 #include "hw/mips/mips.h"
+#include "hw/mips/bootloader.h"
 #include "hw/mips/cpudevs.h"
 #include "hw/pci/pci.h"
 #include "sysemu/sysemu.h"
@@ -844,6 +845,7 @@ static void write_bootloader_nanomips(uint8_t *base, uint64_t run_addr,
 static void write_bootloader(uint8_t *base, uint64_t run_addr,
                              uint64_t kernel_entry)
 {
+    target_ulong a0;
     uint32_t *p;
 
     /* Small bootloader */
@@ -872,30 +874,6 @@ static void write_bootloader(uint8_t *base, uint64_t run_addr,
     /* Second part of the bootloader */
     p = (uint32_t *) (base + 0x580);
 
-    if (semihosting_get_argc()) {
-        /* Preserve a0 content as arguments have been passed */
-        stl_p(p++, 0x00000000);              /* nop */
-    } else {
-        stl_p(p++, 0x24040002);              /* addiu a0, zero, 2 */
-    }
-
-    /* lui sp, high(ENVP_VADDR) */
-    stl_p(p++, 0x3c1d0000 | (((ENVP_VADDR - 64) >> 16) & 0xffff));
-    /* ori sp, sp, low(ENVP_VADDR) */
-    stl_p(p++, 0x37bd0000 | ((ENVP_VADDR - 64) & 0xffff));
-    /* lui a1, high(ENVP_VADDR) */
-    stl_p(p++, 0x3c050000 | ((ENVP_VADDR >> 16) & 0xffff));
-    /* ori a1, a1, low(ENVP_VADDR) */
-    stl_p(p++, 0x34a50000 | (ENVP_VADDR & 0xffff));
-    /* lui a2, high(ENVP_VADDR + 8) */
-    stl_p(p++, 0x3c060000 | (((ENVP_VADDR + 8) >> 16) & 0xffff));
-    /* ori a2, a2, low(ENVP_VADDR + 8) */
-    stl_p(p++, 0x34c60000 | ((ENVP_VADDR + 8) & 0xffff));
-    /* lui a3, high(ram_low_size) */
-    stl_p(p++, 0x3c070000 | (loaderparams.ram_low_size >> 16));
-    /* ori a3, a3, low(ram_low_size) */
-    stl_p(p++, 0x34e70000 | (loaderparams.ram_low_size & 0xffff));
-
     /* Load BAR registers as done by YAMON */
     stl_p(p++, 0x3c09b400);                  /* lui t1, 0xb400 */
 
@@ -947,13 +925,14 @@ static void write_bootloader(uint8_t *base, uint64_t run_addr,
 #endif
     stl_p(p++, 0xad280088);                  /* sw t0, 0x0088(t1) */

hw/mips/malta.c static void write_bootloader(uint8_t *base, uint64_t run_addr, · line 929

Quoted 10 lines
hw/mips/malta.c +2 -7
 
-    /* Jump to kernel code */
-    stl_p(p++, 0x3c1f0000 |
-          ((kernel_entry >> 16) & 0xffff));  /* lui ra, high(kernel_entry) */
-    stl_p(p++, 0x37ff0000 |
-          (kernel_entry & 0xffff));          /* ori ra, ra, low(kernel_entry) */
-    stl_p(p++, 0x03e00009);                  /* jalr ra */
-    stl_p(p++, 0x00000000);                  /* nop */
+    if (semihosting_get_argc()) {
+        a0 = 0;

I never used semihosting with Malta, but it seems you are clearing $a0 content.

Quoted 10 lines
hw/mips/malta.c +6 -0
+    } else {
+        a0 = 2;
+    }
+    bl_gen_jump_kernel(&p, ENVP_VADDR - 64, a0, ENVP_VADDR,
+                       ENVP_VADDR + 8, loaderparams.ram_low_size,
+                       kernel_entry);
 
     /* YAMON subroutines */
     p = (uint32_t *) (base + 0x800);
#13 lore raw

Re: [PATCH v2 5/8] hw/mips: Use bl_gen_kernel_jump to generate bootloaders

Quoted 105 lines · Philippe Mathieu-Daudé

+Alex

Quoted 30 lines · Jiaxun Yang

Replace embedded binary with generated code.

---
  hw/mips/boston.c    | 17 ++---------------
  hw/mips/fuloong2e.c | 28 ++++------------------------
  hw/mips/malta.c     | 41 ++++++++++-------------------------------
  3 files changed, 16 insertions(+), 70 deletions(-)
hw/mips/boston.c +1 -5
index c3b94c68e1..b622222c7d 100644
@@ -27,6 +27,7 @@
  #include "hw/ide/ahci.h"
  #include "hw/loader.h"
  #include "hw/loader-fit.h"
+#include "hw/mips/bootloader.h"
  #include "hw/mips/cps.h"
  #include "hw/pci-host/xilinx-pcie.h"
  #include "hw/qdev-clock.h"
@@ -324,21 +325,7 @@ static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr,
       * a2/$6 = 0
       * a3/$7 = 0
       */
-    stl_p(p++, 0x2404fffe);                     /* li   $4, -2 */
-                                                /* lui  $5, hi(fdt_addr) */
-    stl_p(p++, 0x3c050000 | ((fdt_addr >> 16) & 0xffff));
-    if (fdt_addr & 0xffff) {                    /* ori  $5, lo(fdt_addr) */
-        stl_p(p++, 0x34a50000 | (fdt_addr & 0xffff));

hw/mips/boston.c static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr, · line 341

Quoted 10 lines
hw/mips/boston.c +0 -10
-    }
-    stl_p(p++, 0x34060000);                     /* li   $6, 0 */
-    stl_p(p++, 0x34070000);                     /* li   $7, 0 */
-
-    /* Load kernel entry address & jump to it */
-                                                /* lui  $25, hi(kernel_entry) */
-    stl_p(p++, 0x3c190000 | ((kernel_entry >> 16) & 0xffff));
-                                                /* ori  $25, lo(kernel_entry) */
-    stl_p(p++, 0x37390000 | (kernel_entry & 0xffff));
-    stl_p(p++, 0x03200009);                     /* jr   $25 */

Eh, no delay slot NOP :)

Quoted 3 lines
hw/mips/boston.c +1 -0
+    bl_gen_jump_kernel(&p, 0, (int32_t)-2, fdt_addr, 0, 0, kernel_entry);
  }
 

...

Quoted 55 lines
hw/mips/malta.c +2 -24
index 9afc0b427b..ffd67b8293 100644
@@ -37,6 +37,7 @@
  #include "hw/i2c/smbus_eeprom.h"
  #include "hw/block/flash.h"
  #include "hw/mips/mips.h"
+#include "hw/mips/bootloader.h"
  #include "hw/mips/cpudevs.h"
  #include "hw/pci/pci.h"
  #include "sysemu/sysemu.h"
@@ -844,6 +845,7 @@ static void write_bootloader_nanomips(uint8_t *base, uint64_t run_addr,
  static void write_bootloader(uint8_t *base, uint64_t run_addr,
                               uint64_t kernel_entry)
  {
+    target_ulong a0;
      uint32_t *p;
 
      /* Small bootloader */
@@ -872,30 +874,6 @@ static void write_bootloader(uint8_t *base, uint64_t run_addr,
      /* Second part of the bootloader */
      p = (uint32_t *) (base + 0x580);
 
-    if (semihosting_get_argc()) {
-        /* Preserve a0 content as arguments have been passed */
-        stl_p(p++, 0x00000000);              /* nop */
-    } else {
-        stl_p(p++, 0x24040002);              /* addiu a0, zero, 2 */
-    }
-
-    /* lui sp, high(ENVP_VADDR) */
-    stl_p(p++, 0x3c1d0000 | (((ENVP_VADDR - 64) >> 16) & 0xffff));
-    /* ori sp, sp, low(ENVP_VADDR) */
-    stl_p(p++, 0x37bd0000 | ((ENVP_VADDR - 64) & 0xffff));
-    /* lui a1, high(ENVP_VADDR) */
-    stl_p(p++, 0x3c050000 | ((ENVP_VADDR >> 16) & 0xffff));
-    /* ori a1, a1, low(ENVP_VADDR) */
-    stl_p(p++, 0x34a50000 | (ENVP_VADDR & 0xffff));
-    /* lui a2, high(ENVP_VADDR + 8) */
-    stl_p(p++, 0x3c060000 | (((ENVP_VADDR + 8) >> 16) & 0xffff));
-    /* ori a2, a2, low(ENVP_VADDR + 8) */
-    stl_p(p++, 0x34c60000 | ((ENVP_VADDR + 8) & 0xffff));
-    /* lui a3, high(ram_low_size) */
-    stl_p(p++, 0x3c070000 | (loaderparams.ram_low_size >> 16));
-    /* ori a3, a3, low(ram_low_size) */
-    stl_p(p++, 0x34e70000 | (loaderparams.ram_low_size & 0xffff));
-
      /* Load BAR registers as done by YAMON */
      stl_p(p++, 0x3c09b400);                  /* lui t1, 0xb400 */
 
@@ -947,13 +925,14 @@ static void write_bootloader(uint8_t *base, uint64_t run_addr,
  #endif
      stl_p(p++, 0xad280088);                  /* sw t0, 0x0088(t1) */
 
Quoted 11 lines

hw/mips/malta.c static void write_bootloader(uint8_t *base, uint64_t run_addr, · line 929

Quoted 9 lines
hw/mips/malta.c +2 -7
-    /* Jump to kernel code */
-    stl_p(p++, 0x3c1f0000 |
-          ((kernel_entry >> 16) & 0xffff));  /* lui ra, high(kernel_entry) */
-    stl_p(p++, 0x37ff0000 |
-          (kernel_entry & 0xffff));          /* ori ra, ra, low(kernel_entry) */
-    stl_p(p++, 0x03e00009);                  /* jalr ra */
-    stl_p(p++, 0x00000000);                  /* nop */
+    if (semihosting_get_argc()) {
+        a0 = 0;

I never used semihosting with Malta, but it seems you are clearing $a0 content.

That's what original code did.
I guess when semihosting is enabled, arguments are going to be passed by semi-syscall instead of boot registers?

Thanks.

- Jiaxun

Quoted 11 lines
Quoted 11 lines
hw/mips/malta.c +6 -0
+    } else {
+        a0 = 2;
+    }
+    bl_gen_jump_kernel(&p, ENVP_VADDR - 64, a0, ENVP_VADDR,
+                       ENVP_VADDR + 8, loaderparams.ram_low_size,
+                       kernel_entry);
 
      /* YAMON subroutines */
      p = (uint32_t *) (base + 0x800);
#14 lore raw

PATCH v2 6/8 target/mips/addr: Add translation helpers for KSEG1

In reply to Jiaxun Yang

It's useful for bootloader to do IO opreations.

---
 target/mips/addr.c | 10 ++++++++++
 target/mips/cpu.h  |  2 ++
 2 files changed, 12 insertions(+)
target/mips/addr.c +10 -0
index 27a6036c45..86f1c129c9 100644
@@ -40,6 +40,16 @@ uint64_t cpu_mips_kvm_um_phys_to_kseg0(void *opaque, uint64_t addr)
     return addr | 0x40000000ll;
 }
 
+uint64_t cpu_mips_kseg1_to_phys(void *opaque, uint64_t addr)
+{
+    return addr & 0x1fffffffll;
+}
+
+uint64_t cpu_mips_phys_to_kseg1(void *opaque, uint64_t addr)
+{
+    return (addr & 0x1fffffffll) | 0xffffffffa0000000ll;
+}
+
 bool mips_um_ksegs_enabled(void)
 {
     return mips_um_ksegs;
target/mips/cpu.h +2 -0
index 3ac21d0e9c..0bb1218a57 100644
@@ -1303,6 +1303,8 @@ uint64_t cpu_mips_kseg0_to_phys(void *opaque, uint64_t addr);
 uint64_t cpu_mips_phys_to_kseg0(void *opaque, uint64_t addr);
 
 uint64_t cpu_mips_kvm_um_phys_to_kseg0(void *opaque, uint64_t addr);
+uint64_t cpu_mips_kseg1_to_phys(void *opaque, uint64_t addr);
+uint64_t cpu_mips_phys_to_kseg1(void *opaque, uint64_t addr);
 bool mips_um_ksegs_enabled(void);
 void mips_um_ksegs_enable(void);
 
Signature · 1 line
2.29.2
#16 lore raw

Re: [PATCH v2 6/8] target/mips/addr: Add translation helpers for KSEG1

Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Jiaxun Yang

It's useful for bootloader to do IO opreations.

---
 target/mips/addr.c | 10 ++++++++++
 target/mips/cpu.h  |  2 ++
 2 files changed, 12 insertions(+)
Reviewed-by
#17 lore raw

Re: [PATCH v2 6/8] target/mips/addr: Add translation helpers for KSEG1

Quoted 15 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Jiaxun Yang

It's useful for bootloader to do IO opreations.

---
 target/mips/addr.c | 10 ++++++++++
 target/mips/cpu.h  |  2 ++
 2 files changed, 12 insertions(+)
#18 lore raw

PATCH v2 7/8 hw/mips/malta: Use bootloader helper to set BAR resgiters

In reply to Jiaxun Yang

Translate embedded assembly into IO writes which is more readable.

---
 hw/mips/malta.c | 68 ++++++++++++++-----------------------------------
 1 file changed, 19 insertions(+), 49 deletions(-)
hw/mips/malta.c +19 -49
index ffd67b8293..2799bc36c7 100644
@@ -875,55 +875,25 @@ static void write_bootloader(uint8_t *base, uint64_t run_addr,
     p = (uint32_t *) (base + 0x580);
 
     /* Load BAR registers as done by YAMON */
-    stl_p(p++, 0x3c09b400);                  /* lui t1, 0xb400 */
-
-#ifdef TARGET_WORDS_BIGENDIAN
-    stl_p(p++, 0x3c08df00);                  /* lui t0, 0xdf00 */
-#else
-    stl_p(p++, 0x340800df);                  /* ori t0, r0, 0x00df */
-#endif
-    stl_p(p++, 0xad280068);                  /* sw t0, 0x0068(t1) */
-
-    stl_p(p++, 0x3c09bbe0);                  /* lui t1, 0xbbe0 */
-
-#ifdef TARGET_WORDS_BIGENDIAN
-    stl_p(p++, 0x3c08c000);                  /* lui t0, 0xc000 */
-#else
-    stl_p(p++, 0x340800c0);                  /* ori t0, r0, 0x00c0 */
-#endif
-    stl_p(p++, 0xad280048);                  /* sw t0, 0x0048(t1) */
-#ifdef TARGET_WORDS_BIGENDIAN
-    stl_p(p++, 0x3c084000);                  /* lui t0, 0x4000 */
-#else
-    stl_p(p++, 0x34080040);                  /* ori t0, r0, 0x0040 */
-#endif
-    stl_p(p++, 0xad280050);                  /* sw t0, 0x0050(t1) */
-
-#ifdef TARGET_WORDS_BIGENDIAN
-    stl_p(p++, 0x3c088000);                  /* lui t0, 0x8000 */
-#else
-    stl_p(p++, 0x34080080);                  /* ori t0, r0, 0x0080 */
-#endif
-    stl_p(p++, 0xad280058);                  /* sw t0, 0x0058(t1) */
-#ifdef TARGET_WORDS_BIGENDIAN
-    stl_p(p++, 0x3c083f00);                  /* lui t0, 0x3f00 */
-#else
-    stl_p(p++, 0x3408003f);                  /* ori t0, r0, 0x003f */
-#endif
-    stl_p(p++, 0xad280060);                  /* sw t0, 0x0060(t1) */
-
-#ifdef TARGET_WORDS_BIGENDIAN
-    stl_p(p++, 0x3c08c100);                  /* lui t0, 0xc100 */
-#else
-    stl_p(p++, 0x340800c1);                  /* ori t0, r0, 0x00c1 */
-#endif
-    stl_p(p++, 0xad280080);                  /* sw t0, 0x0080(t1) */
-#ifdef TARGET_WORDS_BIGENDIAN
-    stl_p(p++, 0x3c085e00);                  /* lui t0, 0x5e00 */
-#else
-    stl_p(p++, 0x3408005e);                  /* ori t0, r0, 0x005e */
-#endif
-    stl_p(p++, 0xad280088);                  /* sw t0, 0x0088(t1) */
+    /* move GT64120 registers from 0x14000000 to 0x1be00000 */
+    bl_gen_write_u32(&p, cpu_to_be32(0xdf000000),
+                        cpu_mips_phys_to_kseg1(NULL, 0x14000068));
+
+    /* setup MEM-to-PCI0 mapping */
+    /* setup PCI0 io window to 0x18000000-0x181fffff */
+    bl_gen_write_u32(&p, cpu_to_be32(0xc0000000),
+                        cpu_mips_phys_to_kseg1(NULL, 0x1be00048));
+    bl_gen_write_u32(&p, cpu_to_be32(0x40000000),
+                        cpu_mips_phys_to_kseg1(NULL, 0x1be00050));
+    /* setup PCI0 mem windows */
+    bl_gen_write_u32(&p, cpu_to_be32(0x80000000),
+                        cpu_mips_phys_to_kseg1(NULL, 0x1be00058));
+    bl_gen_write_u32(&p, cpu_to_be32(0x3f000000),
+                        cpu_mips_phys_to_kseg1(NULL, 0x1be00060));
+    bl_gen_write_u32(&p, cpu_to_be32(0xc1000000),
+                        cpu_mips_phys_to_kseg1(NULL, 0x1be00080));
+    bl_gen_write_u32(&p, cpu_to_be32(0x5e000000),
+                        cpu_mips_phys_to_kseg1(NULL, 0x1be00088));
 
     if (semihosting_get_argc()) {
         a0 = 0;
Signature · 1 line
2.29.2
#20 lore raw

PATCH v2 8/8 hw/mips/boston: Use bootloader helper to set GCRs

In reply to Jiaxun Yang

Translate embedded assembly into IO writes which is more readable.

Also hardcode cm_base at boot time instead of reading from CP0.

---
 hw/mips/boston.c | 45 ++++++++++++---------------------------------
 1 file changed, 12 insertions(+), 33 deletions(-)
hw/mips/boston.c +12 -33
index b622222c7d..9f08aa7285 100644
@@ -281,42 +281,21 @@ static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr,
     const uint32_t gic_base = 0x16120000;
     const uint32_t cpc_base = 0x16200000;
 
-    /* Move CM GCRs */
     if (is_64b) {
-        stl_p(p++, 0x40287803);                 /* dmfc0 $8, CMGCRBase */
-        stl_p(p++, 0x00084138);                 /* dsll $8, $8, 4 */
+        bl_gen_write_u64(&p, cm_base,
+                    cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS));
+        bl_gen_write_u64(&p, gic_base | GCR_GIC_BASE_GICEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_GIC_BASE_OFS));
+        bl_gen_write_u64(&p, cpc_base | GCR_CPC_BASE_CPCEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_CPC_BASE_OFS));
     } else {
-        stl_p(p++, 0x40087803);                 /* mfc0 $8, CMGCRBase */
-        stl_p(p++, 0x00084100);                 /* sll  $8, $8, 4 */
+        bl_gen_write_u32(&p, cm_base,
+                    cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS));
+        bl_gen_write_u32(&p, gic_base | GCR_GIC_BASE_GICEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_GIC_BASE_OFS));
+        bl_gen_write_u32(&p, cpc_base | GCR_CPC_BASE_CPCEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_CPC_BASE_OFS));
     }
-    stl_p(p++, 0x3c09a000);                     /* lui  $9, 0xa000 */
-    stl_p(p++, 0x01094025);                     /* or   $8, $9 */
-    stl_p(p++, 0x3c0a0000 | (cm_base >> 16));   /* lui  $10, cm_base >> 16 */
-    if (is_64b) {
-        stl_p(p++, 0xfd0a0008);                 /* sd   $10, 0x8($8) */
-    } else {
-        stl_p(p++, 0xad0a0008);                 /* sw   $10, 0x8($8) */
-    }
-    stl_p(p++, 0x012a4025);                     /* or   $8, $10 */
-
-    /* Move & enable GIC GCRs */
-    stl_p(p++, 0x3c090000 | (gic_base >> 16));  /* lui  $9, gic_base >> 16 */
-    stl_p(p++, 0x35290001);                     /* ori  $9, 0x1 */
-    if (is_64b) {
-        stl_p(p++, 0xfd090080);                 /* sd   $9, 0x80($8) */
-    } else {
-        stl_p(p++, 0xad090080);                 /* sw   $9, 0x80($8) */
-    }
-
-    /* Move & enable CPC GCRs */
-    stl_p(p++, 0x3c090000 | (cpc_base >> 16));  /* lui  $9, cpc_base >> 16 */
-    stl_p(p++, 0x35290001);                     /* ori  $9, 0x1 */
-    if (is_64b) {
-        stl_p(p++, 0xfd090088);                 /* sd   $9, 0x88($8) */
-    } else {
-        stl_p(p++, 0xad090088);                 /* sw   $9, 0x88($8) */
-    }
-
     /*
      * Setup argument registers to follow the UHI boot protocol:
      *
Signature · 1 line
2.29.2
#21 lore raw

Re: [PATCH v2 8/8] hw/mips/boston: Use bootloader helper to set GCRs

Quoted 28 lines · Jiaxun Yang

Translate embedded assembly into IO writes which is more readable.

Also hardcode cm_base at boot time instead of reading from CP0.

---
 hw/mips/boston.c | 45 ++++++++++++---------------------------------
 1 file changed, 12 insertions(+), 33 deletions(-)
hw/mips/boston.c +6 -3
index b622222c7d..9f08aa7285 100644
@@ -281,42 +281,21 @@ static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr,
     const uint32_t gic_base = 0x16120000;
     const uint32_t cpc_base = 0x16200000;
 
-    /* Move CM GCRs */
     if (is_64b) {
-        stl_p(p++, 0x40287803);                 /* dmfc0 $8, CMGCRBase */
-        stl_p(p++, 0x00084138);                 /* dsll $8, $8, 4 */
+        bl_gen_write_u64(&p, cm_base,
+                    cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS));
+        bl_gen_write_u64(&p, gic_base | GCR_GIC_BASE_GICEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_GIC_BASE_OFS));
+        bl_gen_write_u64(&p, cpc_base | GCR_CPC_BASE_CPCEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_CPC_BASE_OFS));

hw/mips/boston.c static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr, · line 298

Quoted 10 lines
hw/mips/boston.c +6 -2
     } else {
-        stl_p(p++, 0x40087803);                 /* mfc0 $8, CMGCRBase */
-        stl_p(p++, 0x00084100);                 /* sll  $8, $8, 4 */
+        bl_gen_write_u32(&p, cm_base,
+                    cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS));
+        bl_gen_write_u32(&p, gic_base | GCR_GIC_BASE_GICEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_GIC_BASE_OFS));
+        bl_gen_write_u32(&p, cpc_base | GCR_CPC_BASE_CPCEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_CPC_BASE_OFS));
     }

What about simplifying adding bl_gen_write_target_ulong() or bl_gen_write_ulong()?

#22 lore raw

Re: [PATCH v2 8/8] hw/mips/boston: Use bootloader helper to set GCRs

Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 33 lines · Jiaxun Yang

Translate embedded assembly into IO writes which is more readable.

Also hardcode cm_base at boot time instead of reading from CP0.

---
 hw/mips/boston.c | 45 ++++++++++++---------------------------------
 1 file changed, 12 insertions(+), 33 deletions(-)
hw/mips/boston.c +8 -5
index b622222c7d..9f08aa7285 100644
@@ -281,42 +281,21 @@ static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr,
     const uint32_t gic_base = 0x16120000;
     const uint32_t cpc_base = 0x16200000;
 
-    /* Move CM GCRs */
     if (is_64b) {
-        stl_p(p++, 0x40287803);                 /* dmfc0 $8, CMGCRBase */
-        stl_p(p++, 0x00084138);                 /* dsll $8, $8, 4 */
+        bl_gen_write_u64(&p, cm_base,
+                    cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS));
+        bl_gen_write_u64(&p, gic_base | GCR_GIC_BASE_GICEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_GIC_BASE_OFS));
+        bl_gen_write_u64(&p, cpc_base | GCR_CPC_BASE_CPCEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_CPC_BASE_OFS));
     } else {
-        stl_p(p++, 0x40087803);                 /* mfc0 $8, CMGCRBase */
-        stl_p(p++, 0x00084100);                 /* sll  $8, $8, 4 */
+        bl_gen_write_u32(&p, cm_base,
+                    cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS));
Quoted 8 lines

hw/mips/boston.c static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr, · line 298

Quoted 5 lines
hw/mips/boston.c +4 -0
+        bl_gen_write_u32(&p, gic_base | GCR_GIC_BASE_GICEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_GIC_BASE_OFS));
+        bl_gen_write_u32(&p, cpc_base | GCR_CPC_BASE_CPCEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_CPC_BASE_OFS));
     }

What about simplifying adding bl_gen_write_target_ulong() or bl_gen_write_ulong()?

bl_gen_store_ulong() similarly to bl_gen_load_ulong()?

#23 lore raw

Re: [PATCH v2 8/8] hw/mips/boston: Use bootloader helper to set GCRs

Quoted 44 lines · Philippe Mathieu-Daudé
Quoted 34 lines · Philippe Mathieu-Daudé
Quoted 33 lines · Jiaxun Yang

Translate embedded assembly into IO writes which is more readable.

Also hardcode cm_base at boot time instead of reading from CP0.

---
 hw/mips/boston.c | 45 ++++++++++++---------------------------------
 1 file changed, 12 insertions(+), 33 deletions(-)
hw/mips/boston.c +8 -5
index b622222c7d..9f08aa7285 100644
@@ -281,42 +281,21 @@ static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr,
     const uint32_t gic_base = 0x16120000;
     const uint32_t cpc_base = 0x16200000;
 
-    /* Move CM GCRs */
     if (is_64b) {
-        stl_p(p++, 0x40287803);                 /* dmfc0 $8, CMGCRBase */
-        stl_p(p++, 0x00084138);                 /* dsll $8, $8, 4 */
+        bl_gen_write_u64(&p, cm_base,
+                    cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS));
+        bl_gen_write_u64(&p, gic_base | GCR_GIC_BASE_GICEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_GIC_BASE_OFS));
+        bl_gen_write_u64(&p, cpc_base | GCR_CPC_BASE_CPCEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_CPC_BASE_OFS));
     } else {
-        stl_p(p++, 0x40087803);                 /* mfc0 $8, CMGCRBase */
-        stl_p(p++, 0x00084100);                 /* sll  $8, $8, 4 */
+        bl_gen_write_u32(&p, cm_base,
+                    cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS));
Quoted 8 lines

hw/mips/boston.c static void gen_firmware(uint32_t *p, hwaddr kernel_entry, hwaddr fdt_addr, · line 298

Quoted 5 lines
hw/mips/boston.c +4 -0
+        bl_gen_write_u32(&p, gic_base | GCR_GIC_BASE_GICEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_GIC_BASE_OFS));
+        bl_gen_write_u32(&p, cpc_base | GCR_CPC_BASE_CPCEN_MSK,
+                    cpu_mips_phys_to_kseg1(NULL, cm_base + GCR_CPC_BASE_OFS));
     }

What about simplifying adding bl_gen_write_target_ulong() or bl_gen_write_ulong()?

Quoted 1 line

bl_gen_store_ulong() similarly to bl_gen_load_ulong()?

bl_gen_write_ulong(). Anyway, can be done later, so:

#24 lore raw
In reply to Jiaxun Yang
Quoted 3 lines · Jiaxun Yang

v2:
A big reconstruction. rewrite helpers with CPU feature and sepreate changesets.

ping?

Quoted 24 lines

Jiaxun Yang (8):
hw/mips: Make bootloader addresses unsgined hw/mips/malta: Use address translation helper to calculate
bootloader_run_addr
hw/mips: Use address translation helper to handle ENVP_ADDR hw/mips: Add a bootloader helper
hw/mips: Use bl_gen_kernel_jump to generate bootloaders target/mips/addr: Add translation helpers for KSEG1 hw/mips/malta: Use bootloader helper to set BAR resgiters hw/mips/boston: Use bootloader helper to set GCRs

  hw/mips/bootloader.c         | 157 ++++++++++++++++++++++++++++++++
  hw/mips/boston.c             |  62 +++----------
  hw/mips/fuloong2e.c          |  48 +++-------
  hw/mips/malta.c              | 171 ++++++++++++-----------------------
  hw/mips/meson.build          |   2 +-
  include/hw/mips/bootloader.h |  48 ++++++++++
  target/mips/addr.c           |  10 ++
  target/mips/cpu.h            |   2 +
  8 files changed, 306 insertions(+), 194 deletions(-)

create mode 100644 hw/mips/bootloader.c
create mode 100644 include/hw/mips/bootloader.h

Tested-by on the v2 series
#25 lore raw
In reply to Jiaxun Yang
Quoted 15 lines · Jiaxun Yang

v2:
A big reconstruction. rewrite helpers with CPU feature and sepreate changesets.

Jiaxun Yang (8):
hw/mips: Make bootloader addresses unsgined hw/mips/malta: Use address translation helper to calculate
bootloader_run_addr
hw/mips: Use address translation helper to handle ENVP_ADDR hw/mips: Add a bootloader helper
hw/mips: Use bl_gen_kernel_jump to generate bootloaders target/mips/addr: Add translation helpers for KSEG1 hw/mips/malta: Use bootloader helper to set BAR resgiters hw/mips/boston: Use bootloader helper to set GCRs

Quoted 12 lines
 hw/mips/bootloader.c         | 157 ++++++++++++++++++++++++++++++++
 hw/mips/boston.c             |  62 +++----------
 hw/mips/fuloong2e.c          |  48 +++-------
 hw/mips/malta.c              | 171 ++++++++++++-----------------------
 hw/mips/meson.build          |   2 +-
 include/hw/mips/bootloader.h |  48 ++++++++++
 target/mips/addr.c           |  10 ++
 target/mips/cpu.h            |   2 +
 8 files changed, 306 insertions(+), 194 deletions(-)
 create mode 100644 hw/mips/bootloader.c
 create mode 100644 include/hw/mips/bootloader.h
#26 lore raw
In reply to Jiaxun Yang
Quoted 15 lines · Jiaxun Yang

v2:
A big reconstruction. rewrite helpers with CPU feature and sepreate changesets.

Jiaxun Yang (8):
hw/mips: Make bootloader addresses unsgined hw/mips/malta: Use address translation helper to calculate
bootloader_run_addr
hw/mips: Use address translation helper to handle ENVP_ADDR hw/mips: Add a bootloader helper
hw/mips: Use bl_gen_kernel_jump to generate bootloaders target/mips/addr: Add translation helpers for KSEG1 hw/mips/malta: Use bootloader helper to set BAR resgiters hw/mips/boston: Use bootloader helper to set GCRs

Quoted 11 lines
 hw/mips/bootloader.c         | 157 ++++++++++++++++++++++++++++++++
 hw/mips/boston.c             |  62 +++----------
 hw/mips/fuloong2e.c          |  48 +++-------
 hw/mips/malta.c              | 171 ++++++++++++-----------------------
 hw/mips/meson.build          |   2 +-
 include/hw/mips/bootloader.h |  48 ++++++++++
 target/mips/addr.c           |  10 ++
 target/mips/cpu.h            |   2 +
 8 files changed, 306 insertions(+), 194 deletions(-)
 create mode 100644 hw/mips/bootloader.c
 create mode 100644 include/hw/mips/bootloader.h

Patches 1-3 queued to mips-next.

#27 lore raw
Quoted 28 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Jiaxun Yang

v2:
A big reconstruction. rewrite helpers with CPU feature and sepreate changesets.

Jiaxun Yang (8):
hw/mips: Make bootloader addresses unsgined hw/mips/malta: Use address translation helper to calculate
bootloader_run_addr
hw/mips: Use address translation helper to handle ENVP_ADDR hw/mips: Add a bootloader helper
hw/mips: Use bl_gen_kernel_jump to generate bootloaders target/mips/addr: Add translation helpers for KSEG1 hw/mips/malta: Use bootloader helper to set BAR resgiters hw/mips/boston: Use bootloader helper to set GCRs

Quoted 11 lines
 hw/mips/bootloader.c         | 157 ++++++++++++++++++++++++++++++++
 hw/mips/boston.c             |  62 +++----------
 hw/mips/fuloong2e.c          |  48 +++-------
 hw/mips/malta.c              | 171 ++++++++++++-----------------------
 hw/mips/meson.build          |   2 +-
 include/hw/mips/bootloader.h |  48 ++++++++++
 target/mips/addr.c           |  10 ++
 target/mips/cpu.h            |   2 +
 8 files changed, 306 insertions(+), 194 deletions(-)
 create mode 100644 hw/mips/bootloader.c
 create mode 100644 include/hw/mips/bootloader.h
Quoted 1 line

Patches 1-3 queued to mips-next.

Patch 6 queued to mips-next.